blob: 1bec8aebb7b6af1e926cb6150651170af32aa11a [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;
Quinn Tranb5d15312017-08-30 10:16:49 -0700148struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400149static mempool_t *qla_tgt_mgmt_cmd_mempool;
150static struct workqueue_struct *qla_tgt_wq;
151static DEFINE_MUTEX(qla_tgt_mutex);
152static LIST_HEAD(qla_tgt_glist);
153
Quinn Tranbe251522017-03-15 09:48:49 -0700154static const char *prot_op_str(u32 prot_op)
155{
156 switch (prot_op) {
157 case TARGET_PROT_NORMAL: return "NORMAL";
158 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
159 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
160 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
161 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
162 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
163 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
164 default: return "UNKNOWN";
165 }
166}
167
Alexei Potashnikdf673272015-07-14 16:00:46 -0400168/* This API intentionally takes dest as a parameter, rather than returning
169 * int value to avoid caller forgetting to issue wmb() after the store */
170void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
171{
172 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
173 *dest = atomic_inc_return(&base_vha->generation_tick);
174 /* memory barrier */
175 wmb();
176}
177
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400178/* Might release hw lock, then reaquire!! */
179static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
180{
181 /* Send marker if required */
182 if (unlikely(vha->marker_needed != 0)) {
183 int rc = qla2x00_issue_marker(vha, vha_locked);
184 if (rc != QLA_SUCCESS) {
185 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
186 "qla_target(%d): issue_marker() failed\n",
187 vha->vp_idx);
188 }
189 return rc;
190 }
191 return QLA_SUCCESS;
192}
193
194static inline
195struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
196 uint8_t *d_id)
197{
Quinn Tran482c9dc2017-03-15 09:48:54 -0700198 struct scsi_qla_host *host;
199 uint32_t key = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400200
Quinn Tran482c9dc2017-03-15 09:48:54 -0700201 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
202 (vha->d_id.b.al_pa == d_id[2]))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400203 return vha;
204
Quinn Tran482c9dc2017-03-15 09:48:54 -0700205 key = (uint32_t)d_id[0] << 16;
206 key |= (uint32_t)d_id[1] << 8;
207 key |= (uint32_t)d_id[2];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400208
Quinn Tran482c9dc2017-03-15 09:48:54 -0700209 host = btree_lookup32(&vha->hw->tgt.host_map, key);
210 if (!host)
Quinn Tran83548fe2017-06-02 09:12:01 -0700211 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
212 "Unable to find host %06x\n", key);
Quinn Tran482c9dc2017-03-15 09:48:54 -0700213
214 return host;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400215}
216
217static inline
218struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
219 uint16_t vp_idx)
220{
221 struct qla_hw_data *ha = vha->hw;
222
223 if (vha->vp_idx == vp_idx)
224 return vha;
225
226 BUG_ON(ha->tgt.tgt_vp_map == NULL);
227 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
228 return ha->tgt.tgt_vp_map[vp_idx].vha;
229
230 return NULL;
231}
232
Quinn Tran33e79972014-09-25 06:14:55 -0400233static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
234{
235 unsigned long flags;
236
237 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
238
239 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400240 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
241 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400242 vha->hw->tgt.num_pend_cmds;
243 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
244}
245static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
246{
247 unsigned long flags;
248
249 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
250 vha->hw->tgt.num_pend_cmds--;
251 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
252}
253
Quinn Tran41dc5292017-01-19 22:28:03 -0800254
255static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
Quinn Tran82de8022017-06-13 20:47:17 -0700256 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran41dc5292017-01-19 22:28:03 -0800257{
258 struct qla_tgt_sess_op *u;
259 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
260 unsigned long flags;
261
262 if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700263 ql_dbg(ql_dbg_async, vha, 0x502c,
264 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
265 vha->vp_idx);
Quinn Tran41dc5292017-01-19 22:28:03 -0800266 goto out_term;
267 }
268
269 u = kzalloc(sizeof(*u), GFP_ATOMIC);
Quinn Tran83548fe2017-06-02 09:12:01 -0700270 if (u == NULL)
Quinn Tran41dc5292017-01-19 22:28:03 -0800271 goto out_term;
Quinn Tran41dc5292017-01-19 22:28:03 -0800272
273 u->vha = vha;
274 memcpy(&u->atio, atio, sizeof(*atio));
275 INIT_LIST_HEAD(&u->cmd_list);
276
277 spin_lock_irqsave(&vha->cmd_list_lock, flags);
278 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
279 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
280
281 schedule_delayed_work(&vha->unknown_atio_work, 1);
282
283out:
284 return;
285
286out_term:
Quinn Tran82de8022017-06-13 20:47:17 -0700287 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800288 goto out;
289}
290
291static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
292 uint8_t ha_locked)
293{
294 struct qla_tgt_sess_op *u, *t;
295 scsi_qla_host_t *host;
296 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
297 unsigned long flags;
298 uint8_t queued = 0;
299
300 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
301 if (u->aborted) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700302 ql_dbg(ql_dbg_async, vha, 0x502e,
303 "Freeing unknown %s %p, because of Abort\n",
Quinn Tran41dc5292017-01-19 22:28:03 -0800304 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700305 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
306 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800307 goto abort;
308 }
309
310 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
311 if (host != NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700312 ql_dbg(ql_dbg_async, vha, 0x502f,
313 "Requeuing unknown ATIO_TYPE7 %p\n", u);
Quinn Tran41dc5292017-01-19 22:28:03 -0800314 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
315 } else if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700316 ql_dbg(ql_dbg_async, vha, 0x503a,
317 "Freeing unknown %s %p, because tgt is being stopped\n",
318 "ATIO_TYPE7", u);
Quinn Tran82de8022017-06-13 20:47:17 -0700319 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
320 &u->atio, ha_locked, 0);
Quinn Tran41dc5292017-01-19 22:28:03 -0800321 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700322 ql_dbg(ql_dbg_async, vha, 0x503d,
323 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
Quinn Tran41dc5292017-01-19 22:28:03 -0800324 if (!queued) {
325 queued = 1;
326 schedule_delayed_work(&vha->unknown_atio_work,
327 1);
328 }
329 continue;
330 }
331
332abort:
333 spin_lock_irqsave(&vha->cmd_list_lock, flags);
334 list_del(&u->cmd_list);
335 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
336 kfree(u);
337 }
338}
339
340void qlt_unknown_atio_work_fn(struct work_struct *work)
341{
342 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
343 struct scsi_qla_host, unknown_atio_work);
344
345 qlt_try_to_dequeue_unknown_atios(vha, 0);
346}
347
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500348static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500349 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400350{
Quinn Tranf83adb62014-04-11 16:54:43 -0400351 ql_dbg(ql_dbg_tgt, vha, 0xe072,
352 "%s: qla_target(%d): type %x ox_id %04x\n",
353 __func__, vha->vp_idx, atio->u.raw.entry_type,
354 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
355
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400356 switch (atio->u.raw.entry_type) {
357 case ATIO_TYPE7:
358 {
359 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
360 atio->u.isp24.fcp_hdr.d_id);
361 if (unlikely(NULL == host)) {
362 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
363 "qla_target(%d): Received ATIO_TYPE7 "
364 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
365 atio->u.isp24.fcp_hdr.d_id[0],
366 atio->u.isp24.fcp_hdr.d_id[1],
367 atio->u.isp24.fcp_hdr.d_id[2]);
Quinn Tran41dc5292017-01-19 22:28:03 -0800368
369
370 qlt_queue_unknown_atio(vha, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400371 break;
372 }
Quinn Tran41dc5292017-01-19 22:28:03 -0800373 if (unlikely(!list_empty(&vha->unknown_atio_list)))
374 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
375
Quinn Tran2f424b92015-12-17 14:57:07 -0500376 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400377 break;
378 }
379
380 case IMMED_NOTIFY_TYPE:
381 {
382 struct scsi_qla_host *host = vha;
383 struct imm_ntfy_from_isp *entry =
384 (struct imm_ntfy_from_isp *)atio;
385
Quinn Tran82de8022017-06-13 20:47:17 -0700386 qlt_issue_marker(vha, ha_locked);
387
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400388 if ((entry->u.isp24.vp_index != 0xFF) &&
389 (entry->u.isp24.nport_handle != 0xFFFF)) {
390 host = qlt_find_host_by_vp_idx(vha,
391 entry->u.isp24.vp_index);
392 if (unlikely(!host)) {
393 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
394 "qla_target(%d): Received "
395 "ATIO (IMMED_NOTIFY_TYPE) "
396 "with unknown vp_index %d\n",
397 vha->vp_idx, entry->u.isp24.vp_index);
398 break;
399 }
400 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500401 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400402 break;
403 }
404
Quinn Tran41dc5292017-01-19 22:28:03 -0800405 case VP_RPT_ID_IOCB_TYPE:
406 qla24xx_report_id_acquisition(vha,
407 (struct vp_rpt_id_entry_24xx *)atio);
408 break;
409
410 case ABTS_RECV_24XX:
411 {
412 struct abts_recv_from_24xx *entry =
413 (struct abts_recv_from_24xx *)atio;
414 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
415 entry->vp_index);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700416 unsigned long flags;
417
Quinn Tran41dc5292017-01-19 22:28:03 -0800418 if (unlikely(!host)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700419 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
Quinn Tran41dc5292017-01-19 22:28:03 -0800420 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
421 "received, with unknown vp_index %d\n",
422 vha->vp_idx, entry->vp_index);
423 break;
424 }
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700425 if (!ha_locked)
426 spin_lock_irqsave(&host->hw->hardware_lock, flags);
427 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
428 if (!ha_locked)
429 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -0800430 break;
Quinn Tran41dc5292017-01-19 22:28:03 -0800431 }
432
433 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
434
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400435 default:
436 ql_dbg(ql_dbg_tgt, vha, 0xe040,
437 "qla_target(%d): Received unknown ATIO atio "
438 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
439 break;
440 }
441
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500442 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400443}
444
Quinn Tran82de8022017-06-13 20:47:17 -0700445void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
446 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400447{
448 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400449 case CTIO_CRC2:
450 ql_dbg(ql_dbg_tgt, vha, 0xe073,
451 "qla_target(%d):%s: CRC2 Response pkt\n",
452 vha->vp_idx, __func__);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400453 case CTIO_TYPE7:
454 {
455 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
456 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
457 entry->vp_index);
458 if (unlikely(!host)) {
459 ql_dbg(ql_dbg_tgt, vha, 0xe041,
460 "qla_target(%d): Response pkt (CTIO_TYPE7) "
461 "received, with unknown vp_index %d\n",
462 vha->vp_idx, entry->vp_index);
463 break;
464 }
Quinn Tran82de8022017-06-13 20:47:17 -0700465 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400466 break;
467 }
468
469 case IMMED_NOTIFY_TYPE:
470 {
471 struct scsi_qla_host *host = vha;
472 struct imm_ntfy_from_isp *entry =
473 (struct imm_ntfy_from_isp *)pkt;
474
475 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
476 if (unlikely(!host)) {
477 ql_dbg(ql_dbg_tgt, vha, 0xe042,
478 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
479 "received, with unknown vp_index %d\n",
480 vha->vp_idx, entry->u.isp24.vp_index);
481 break;
482 }
Quinn Tran82de8022017-06-13 20:47:17 -0700483 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400484 break;
485 }
486
487 case NOTIFY_ACK_TYPE:
488 {
489 struct scsi_qla_host *host = vha;
490 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
491
492 if (0xFF != entry->u.isp24.vp_index) {
493 host = qlt_find_host_by_vp_idx(vha,
494 entry->u.isp24.vp_index);
495 if (unlikely(!host)) {
496 ql_dbg(ql_dbg_tgt, vha, 0xe043,
497 "qla_target(%d): Response "
498 "pkt (NOTIFY_ACK_TYPE) "
499 "received, with unknown "
500 "vp_index %d\n", vha->vp_idx,
501 entry->u.isp24.vp_index);
502 break;
503 }
504 }
Quinn Tran82de8022017-06-13 20:47:17 -0700505 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400506 break;
507 }
508
509 case ABTS_RECV_24XX:
510 {
511 struct abts_recv_from_24xx *entry =
512 (struct abts_recv_from_24xx *)pkt;
513 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
514 entry->vp_index);
515 if (unlikely(!host)) {
516 ql_dbg(ql_dbg_tgt, vha, 0xe044,
517 "qla_target(%d): Response pkt "
518 "(ABTS_RECV_24XX) received, with unknown "
519 "vp_index %d\n", vha->vp_idx, entry->vp_index);
520 break;
521 }
Quinn Tran82de8022017-06-13 20:47:17 -0700522 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400523 break;
524 }
525
526 case ABTS_RESP_24XX:
527 {
528 struct abts_resp_to_24xx *entry =
529 (struct abts_resp_to_24xx *)pkt;
530 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
531 entry->vp_index);
532 if (unlikely(!host)) {
533 ql_dbg(ql_dbg_tgt, vha, 0xe045,
534 "qla_target(%d): Response pkt "
535 "(ABTS_RECV_24XX) received, with unknown "
536 "vp_index %d\n", vha->vp_idx, entry->vp_index);
537 break;
538 }
Quinn Tran82de8022017-06-13 20:47:17 -0700539 qlt_response_pkt(host, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400540 break;
541 }
542
543 default:
Quinn Tran82de8022017-06-13 20:47:17 -0700544 qlt_response_pkt(vha, rsp, pkt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400545 break;
546 }
547
548}
549
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500550/*
551 * All qlt_plogi_ack_t operations are protected by hardware_lock
552 */
Quinn Tran726b8542017-01-19 22:28:00 -0800553static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
554 struct imm_ntfy_from_isp *ntfy, int type)
555{
556 struct qla_work_evt *e;
557 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
558 if (!e)
559 return QLA_FUNCTION_FAILED;
560
561 e->u.nack.fcport = fcport;
562 e->u.nack.type = type;
563 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
564 return qla2x00_post_work(vha, e);
565}
566
567static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800568void qla2x00_async_nack_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800569{
Quinn Tran726b8542017-01-19 22:28:00 -0800570 struct srb *sp = (struct srb *)s;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800571 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800572 unsigned long flags;
573
Quinn Tran83548fe2017-06-02 09:12:01 -0700574 ql_dbg(ql_dbg_disc, vha, 0x20f2,
575 "Async done-%s res %x %8phC type %d\n",
576 sp->name, res, sp->fcport->port_name, sp->type);
Quinn Tran726b8542017-01-19 22:28:00 -0800577
578 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
579 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -0700580 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran726b8542017-01-19 22:28:00 -0800581
582 switch (sp->type) {
583 case SRB_NACK_PLOGI:
584 sp->fcport->login_gen++;
585 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
586 sp->fcport->logout_on_delete = 1;
Quinn Tran5b334692017-03-15 09:48:48 -0700587 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
Quinn Tran35158322017-08-30 10:16:50 -0700588 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800589 break;
590
591 case SRB_NACK_PRLI:
592 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
593 sp->fcport->deleted = 0;
Quinn Tran35158322017-08-30 10:16:50 -0700594 sp->fcport->send_els_logo = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800595
596 if (!sp->fcport->login_succ &&
597 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
598 sp->fcport->login_succ = 1;
599
600 vha->fcport_count++;
601
602 if (!IS_IIDMA_CAPABLE(vha->hw) ||
603 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700604 ql_dbg(ql_dbg_disc, vha, 0x20f3,
605 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
606 __func__, __LINE__,
607 sp->fcport->port_name,
608 vha->fcport_count);
Quinn Tran726b8542017-01-19 22:28:00 -0800609
610 qla24xx_post_upd_fcport_work(vha, sp->fcport);
611 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700612 ql_dbg(ql_dbg_disc, vha, 0x20f5,
613 "%s %d %8phC post gpsc fcp_cnt %d\n",
614 __func__, __LINE__,
615 sp->fcport->port_name,
616 vha->fcport_count);
Quinn Tran726b8542017-01-19 22:28:00 -0800617
618 qla24xx_post_gpsc_work(vha, sp->fcport);
619 }
620 }
621 break;
622
623 case SRB_NACK_LOGO:
624 sp->fcport->login_gen++;
625 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
626 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
627 break;
628 }
629 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
630
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800631 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800632}
633
634int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
635 struct imm_ntfy_from_isp *ntfy, int type)
636{
637 int rval = QLA_FUNCTION_FAILED;
638 srb_t *sp;
639 char *c = NULL;
640
641 fcport->flags |= FCF_ASYNC_SENT;
642 switch (type) {
643 case SRB_NACK_PLOGI:
644 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
645 c = "PLOGI";
646 break;
647 case SRB_NACK_PRLI:
648 fcport->fw_login_state = DSC_LS_PRLI_PEND;
Quinn Tranec7193e2017-03-15 09:48:55 -0700649 fcport->deleted = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800650 c = "PRLI";
651 break;
652 case SRB_NACK_LOGO:
653 fcport->fw_login_state = DSC_LS_LOGO_PEND;
654 c = "LOGO";
655 break;
656 }
657
658 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
659 if (!sp)
660 goto done;
661
662 sp->type = type;
663 sp->name = "nack";
664
665 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
666
667 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
Quinn Tran2e01d0b2017-12-04 14:44:56 -0800668 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
Quinn Tran726b8542017-01-19 22:28:00 -0800669 sp->done = qla2x00_async_nack_sp_done;
670
671 rval = qla2x00_start_sp(sp);
672 if (rval != QLA_SUCCESS)
673 goto done_free_sp;
674
Quinn Tran83548fe2017-06-02 09:12:01 -0700675 ql_dbg(ql_dbg_disc, vha, 0x20f4,
676 "Async-%s %8phC hndl %x %s\n",
677 sp->name, fcport->port_name, sp->handle, c);
Quinn Tran726b8542017-01-19 22:28:00 -0800678
679 return rval;
680
681done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800682 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800683done:
684 fcport->flags &= ~FCF_ASYNC_SENT;
685 return rval;
686}
687
688void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
689{
690 fc_port_t *t;
691 unsigned long flags;
692
693 switch (e->u.nack.type) {
694 case SRB_NACK_PRLI:
695 mutex_lock(&vha->vha_tgt.tgt_mutex);
696 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
697 mutex_unlock(&vha->vha_tgt.tgt_mutex);
698 if (t) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700699 ql_log(ql_log_info, vha, 0xd034,
Quinn Tran726b8542017-01-19 22:28:00 -0800700 "%s create sess success %p", __func__, t);
701 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
702 /* create sess has an extra kref */
703 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
704 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
705 }
706 break;
707 }
708 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
709 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
710}
711
712void qla24xx_delete_sess_fn(struct work_struct *work)
713{
714 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
715 struct qla_hw_data *ha = fcport->vha->hw;
716 unsigned long flags;
717
718 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
719
720 if (fcport->se_sess) {
721 ha->tgt.tgt_ops->shutdown_sess(fcport);
722 ha->tgt.tgt_ops->put_sess(fcport);
723 } else {
724 qlt_unreg_sess(fcport);
725 }
726 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
727}
728
729/*
730 * Called from qla2x00_reg_remote_port()
731 */
732void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
733{
734 struct qla_hw_data *ha = vha->hw;
735 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
736 struct fc_port *sess = fcport;
737 unsigned long flags;
738
739 if (!vha->hw->tgt.tgt_ops)
740 return;
741
742 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
743 if (tgt->tgt_stop) {
744 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
745 return;
746 }
747
748 if (fcport->disc_state == DSC_DELETE_PEND) {
749 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
750 return;
751 }
752
753 if (!sess->se_sess) {
754 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
755
756 mutex_lock(&vha->vha_tgt.tgt_mutex);
757 sess = qlt_create_sess(vha, fcport, false);
758 mutex_unlock(&vha->vha_tgt.tgt_mutex);
759
760 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
761 } else {
762 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
763 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
764 return;
765 }
766
767 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700768 ql_dbg(ql_dbg_disc, vha, 0x2107,
Quinn Tran726b8542017-01-19 22:28:00 -0800769 "%s: kref_get fail sess %8phC \n",
770 __func__, sess->port_name);
771 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
772 return;
773 }
774
775 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
776 "qla_target(%u): %ssession for port %8phC "
777 "(loop ID %d) reappeared\n", vha->vp_idx,
778 sess->local ? "local " : "", sess->port_name, sess->loop_id);
779
780 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
781 "Reappeared sess %p\n", sess);
782
783 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
784 fcport->loop_id,
785 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
786 }
787
788 if (sess && sess->local) {
789 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
790 "qla_target(%u): local session for "
791 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
792 fcport->port_name, sess->loop_id);
793 sess->local = 0;
794 }
795 ha->tgt.tgt_ops->put_sess(sess);
796 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
797}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500798
799/*
800 * This is a zero-base ref-counting solution, since hardware_lock
801 * guarantees that ref_count is not modified concurrently.
802 * Upon successful return content of iocb is undefined
803 */
Quinn Tran5d964832017-01-19 22:27:59 -0800804static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500805qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
806 struct imm_ntfy_from_isp *iocb)
807{
Quinn Tran5d964832017-01-19 22:27:59 -0800808 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500809
810 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
811 if (pla->id.b24 == id->b24) {
812 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800813 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500814 return pla;
815 }
816 }
817
818 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
819 if (!pla) {
820 ql_dbg(ql_dbg_async, vha, 0x5088,
821 "qla_target(%d): Allocation of plogi_ack failed\n",
822 vha->vp_idx);
823 return NULL;
824 }
825
Quinn Tran5d964832017-01-19 22:27:59 -0800826 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500827 pla->id = *id;
828 list_add_tail(&pla->list, &vha->plogi_ack_list);
829
830 return pla;
831}
832
Quinn Tran726b8542017-01-19 22:28:00 -0800833void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800834 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500835{
Quinn Tran5d964832017-01-19 22:27:59 -0800836 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800837 port_id_t port_id;
838 uint16_t loop_id;
839 fc_port_t *fcport = pla->fcport;
840
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500841 BUG_ON(!pla->ref_count);
842 pla->ref_count--;
843
844 if (pla->ref_count)
845 return;
846
Quinn Tran726b8542017-01-19 22:28:00 -0800847 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500848 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800849 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
850 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
851 iocb->u.isp24.port_id[0],
852 le16_to_cpu(iocb->u.isp24.nport_handle),
853 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800854
855 port_id.b.domain = iocb->u.isp24.port_id[2];
856 port_id.b.area = iocb->u.isp24.port_id[1];
857 port_id.b.al_pa = iocb->u.isp24.port_id[0];
858 port_id.b.rsvd_1 = 0;
859
860 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
861
862 fcport->loop_id = loop_id;
863 fcport->d_id = port_id;
864 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
865
866 list_for_each_entry(fcport, &vha->vp_fcports, list) {
867 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
868 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
869 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
870 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
871 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500872
873 list_del(&pla->list);
874 kmem_cache_free(qla_tgt_plogi_cachep, pla);
875}
876
Quinn Tran726b8542017-01-19 22:28:00 -0800877void
Quinn Tran5d964832017-01-19 22:27:59 -0800878qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
879 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500880{
Quinn Tran5d964832017-01-19 22:27:59 -0800881 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500882 /* Inc ref_count first because link might already be pointing at pla */
883 pla->ref_count++;
884
Quinn Tran726b8542017-01-19 22:28:00 -0800885 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
886 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
887 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
888 sess, link, sess->port_name,
889 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
890 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
891 pla->ref_count, pla, link);
892
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500893 if (sess->plogi_link[link])
894 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
895
Quinn Tran726b8542017-01-19 22:28:00 -0800896 if (link == QLT_PLOGI_LINK_SAME_WWN)
897 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500898
899 sess->plogi_link[link] = pla;
900}
901
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500902typedef struct {
903 /* These fields must be initialized by the caller */
904 port_id_t id;
905 /*
906 * number of cmds dropped while we were waiting for
907 * initiator to ack LOGO initialize to 1 if LOGO is
908 * triggered by a command, otherwise, to 0
909 */
910 int cmd_count;
911
912 /* These fields are used by callee */
913 struct list_head list;
914} qlt_port_logo_t;
915
916static void
917qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
918{
919 qlt_port_logo_t *tmp;
920 int res;
921
922 mutex_lock(&vha->vha_tgt.tgt_mutex);
923
924 list_for_each_entry(tmp, &vha->logo_list, list) {
925 if (tmp->id.b24 == logo->id.b24) {
926 tmp->cmd_count += logo->cmd_count;
927 mutex_unlock(&vha->vha_tgt.tgt_mutex);
928 return;
929 }
930 }
931
932 list_add_tail(&logo->list, &vha->logo_list);
933
934 mutex_unlock(&vha->vha_tgt.tgt_mutex);
935
936 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
937
938 mutex_lock(&vha->vha_tgt.tgt_mutex);
939 list_del(&logo->list);
940 mutex_unlock(&vha->vha_tgt.tgt_mutex);
941
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
943 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
944 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
945 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500946}
947
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400948static void qlt_free_session_done(struct work_struct *work)
949{
Quinn Tran5d964832017-01-19 22:27:59 -0800950 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400951 free_work);
952 struct qla_tgt *tgt = sess->tgt;
953 struct scsi_qla_host *vha = sess->vha;
954 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400955 unsigned long flags;
956 bool logout_started = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800957 struct event_arg ea;
Quinn Tran41dc5292017-01-19 22:28:03 -0800958 scsi_qla_host_t *base_vha;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400959
960 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
961 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500962 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400963 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800964 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400965 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500966 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400967
Quinn Tran726b8542017-01-19 22:28:00 -0800968 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800969 if (sess->send_els_logo) {
970 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500971
Quinn Tran41dc5292017-01-19 22:28:03 -0800972 logo.id = sess->d_id;
973 logo.cmd_count = 0;
974 qlt_send_first_logo(vha, &logo);
975 }
976
977 if (sess->logout_on_delete) {
978 int rc;
979
980 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
981 if (rc != QLA_SUCCESS)
982 ql_log(ql_log_warn, vha, 0xf085,
983 "Schedule logo failed sess %p rc %d\n",
984 sess, rc);
985 else
986 logout_started = true;
987 }
Quinn Tran726b8542017-01-19 22:28:00 -0800988 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400989
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400990 /*
991 * Release the target session for FC Nexus from fabric module code.
992 */
993 if (sess->se_sess != NULL)
994 ha->tgt.tgt_ops->free_session(sess);
995
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400996 if (logout_started) {
997 bool traced = false;
998
Mark Rutland6aa7de02017-10-23 14:07:29 -0700999 while (!READ_ONCE(sess->logout_completed)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001000 if (!traced) {
1001 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1002 "%s: waiting for sess %p logout\n",
1003 __func__, sess);
1004 traced = true;
1005 }
1006 msleep(100);
1007 }
1008
Quinn Tran726b8542017-01-19 22:28:00 -08001009 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran41dc5292017-01-19 22:28:03 -08001010 "%s: sess %p logout completed\n",__func__, sess);
1011 }
1012
1013 if (sess->logo_ack_needed) {
1014 sess->logo_ack_needed = 0;
1015 qla24xx_async_notify_ack(vha, sess,
1016 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001017 }
1018
Quinn Tran726b8542017-01-19 22:28:00 -08001019 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1020 if (sess->se_sess) {
1021 sess->se_sess = NULL;
1022 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1023 tgt->sess_count--;
1024 }
1025
1026 sess->disc_state = DSC_DELETED;
1027 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1028 sess->deleted = QLA_SESS_DELETED;
1029 sess->login_retry = vha->hw->login_retry_count;
1030
1031 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1032 vha->fcport_count--;
1033 sess->login_succ = 0;
1034 }
1035
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001036 if (sess->chip_reset != ha->base_qpair->chip_reset)
Quinn Tran726b8542017-01-19 22:28:00 -08001037 qla2x00_clear_loop_id(sess);
1038
1039 if (sess->conflict) {
1040 sess->conflict->login_pause = 0;
1041 sess->conflict = NULL;
1042 if (!test_bit(UNLOADING, &vha->dpc_flags))
1043 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1044 }
1045
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001046 {
Quinn Tran5d964832017-01-19 22:27:59 -08001047 struct qlt_plogi_ack_t *own =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001048 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Quinn Tran5d964832017-01-19 22:27:59 -08001049 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001050 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001051 struct imm_ntfy_from_isp *iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001052
1053 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001054 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001055 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001056 "se_sess %p / sess %p port %8phC is gone,"
1057 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1058 sess->se_sess, sess, sess->port_name,
1059 own ? "releasing own PLOGI" : "no own PLOGI pending",
1060 own ? own->ref_count : -1,
1061 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001062 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001063 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001064 } else {
1065 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1066 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1067 sess->se_sess, sess, sess->port_name,
1068 own ? "releasing own PLOGI" :
1069 "no own PLOGI pending",
1070 own ? own->ref_count : -1);
1071 }
1072
Quinn Tran726b8542017-01-19 22:28:00 -08001073 if (own) {
1074 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001075 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001076 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1077 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001078 }
Quinn Tran5d964832017-01-19 22:27:59 -08001079 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1080
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001081 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001082 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1083 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001084
Quinn Tran726b8542017-01-19 22:28:00 -08001085 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001086 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001087
1088 if (vha->fcport_count == 0)
1089 wake_up_all(&vha->fcport_waitQ);
1090
Quinn Tran41dc5292017-01-19 22:28:03 -08001091 base_vha = pci_get_drvdata(ha->pdev);
1092 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1093 return;
1094
Quinn Tran726b8542017-01-19 22:28:00 -08001095 if (!tgt || !tgt->tgt_stop) {
1096 memset(&ea, 0, sizeof(ea));
1097 ea.event = FCME_DELETE_DONE;
1098 ea.fcport = sess;
1099 qla2x00_fcport_event_handler(vha, &ea);
1100 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001101}
1102
Quinn Tran75601512015-12-17 14:57:04 -05001103/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001104void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001105{
1106 struct scsi_qla_host *vha = sess->vha;
1107
Quinn Tran83548fe2017-06-02 09:12:01 -07001108 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001109 "%s sess %p for deletion %8phC\n",
1110 __func__, sess, sess->port_name);
1111
Quinn Tran36c78452016-02-04 11:45:18 -05001112 if (sess->se_sess)
1113 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001114
Quinn Tran5d964832017-01-19 22:27:59 -08001115 qla2x00_mark_device_lost(vha, sess, 1, 1);
1116
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001117 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001118 sess->disc_state = DSC_DELETE_PEND;
1119 sess->last_rscn_gen = sess->rscn_gen;
1120 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001121
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001122 if (sess->nvme_flag & NVME_FLAG_REGISTERED)
1123 schedule_work(&sess->nvme_del_work);
1124
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001125 INIT_WORK(&sess->free_work, qlt_free_session_done);
1126 schedule_work(&sess->free_work);
1127}
Quinn Tran5d964832017-01-19 22:27:59 -08001128EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001129
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001130static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1131{
1132 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001133 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001134 uint16_t loop_id;
1135 int res = 0;
1136 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001137 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001138
1139 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1140 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001141 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001142 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001143 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001144 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001145 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001146 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001147 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001148 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001149 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001150 }
1151
1152 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1153 "Using sess for qla_tgt_reset: %p\n", sess);
1154 if (!sess) {
1155 res = -ESRCH;
1156 return res;
1157 }
1158
1159 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001160 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1161 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001162 mcmd, loop_id);
1163
Quinn Tranbb1181c2016-12-23 18:06:05 -08001164 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001165}
1166
Quinn Tran726b8542017-01-19 22:28:00 -08001167static void qla24xx_chk_fcp_state(struct fc_port *sess)
1168{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001169 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
Quinn Tran726b8542017-01-19 22:28:00 -08001170 sess->logout_on_delete = 0;
1171 sess->logo_ack_needed = 0;
1172 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1173 sess->scan_state = 0;
1174 }
1175}
1176
Quinn Tran75601512015-12-17 14:57:04 -05001177/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran726b8542017-01-19 22:28:00 -08001178void qlt_schedule_sess_for_deletion(struct fc_port *sess,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001179 bool immediate)
1180{
1181 struct qla_tgt *tgt = sess->tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001182
Quinn Tran726b8542017-01-19 22:28:00 -08001183 if (sess->disc_state == DSC_DELETE_PEND)
1184 return;
1185
1186 if (sess->disc_state == DSC_DELETED) {
1187 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1188 wake_up_all(&tgt->waitQ);
1189 if (sess->vha->fcport_count == 0)
1190 wake_up_all(&sess->vha->fcport_waitQ);
1191
1192 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1193 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001194 return;
1195 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001196
Quinn Tran726b8542017-01-19 22:28:00 -08001197 sess->disc_state = DSC_DELETE_PEND;
1198
1199 if (sess->deleted == QLA_SESS_DELETED)
1200 sess->logout_on_delete = 0;
1201
1202 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1203 qla24xx_chk_fcp_state(sess);
1204
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001205 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1206 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001207
Quinn Trana01c77d2017-12-04 14:44:58 -08001208 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1209 queue_work(sess->vha->hw->wq, &sess->del_work);
Quinn Tran726b8542017-01-19 22:28:00 -08001210}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001211
Quinn Tran726b8542017-01-19 22:28:00 -08001212void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1213{
1214 unsigned long flags;
1215 struct qla_hw_data *ha = sess->vha->hw;
1216 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1217 qlt_schedule_sess_for_deletion(sess, 1);
1218 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001219}
1220
Quinn Tran75601512015-12-17 14:57:04 -05001221/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -04001222static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001223{
Quinn Tran5d964832017-01-19 22:27:59 -08001224 struct fc_port *sess;
1225 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001226
Quinn Tran5d964832017-01-19 22:27:59 -08001227 list_for_each_entry(sess, &vha->vp_fcports, list) {
1228 if (sess->se_sess)
Quinn Tran726b8542017-01-19 22:28:00 -08001229 qlt_schedule_sess_for_deletion(sess, 1);
Quinn Tran5d964832017-01-19 22:27:59 -08001230 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001231
1232 /* At this point tgt could be already dead */
1233}
1234
1235static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1236 uint16_t *loop_id)
1237{
1238 struct qla_hw_data *ha = vha->hw;
1239 dma_addr_t gid_list_dma;
1240 struct gid_list_info *gid_list;
1241 char *id_iter;
1242 int res, rc, i;
1243 uint16_t entries;
1244
1245 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1246 &gid_list_dma, GFP_KERNEL);
1247 if (!gid_list) {
1248 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1249 "qla_target(%d): DMA Alloc failed of %u\n",
1250 vha->vp_idx, qla2x00_gid_list_size(ha));
1251 return -ENOMEM;
1252 }
1253
1254 /* Get list of logged in devices */
Quinn Tran15f30a52017-03-15 09:48:52 -07001255 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001256 if (rc != QLA_SUCCESS) {
1257 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1258 "qla_target(%d): get_id_list() failed: %x\n",
1259 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001260 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001261 goto out_free_id_list;
1262 }
1263
1264 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001265 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001266 for (i = 0; i < entries; i++) {
1267 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1268 if ((gid->al_pa == s_id[2]) &&
1269 (gid->area == s_id[1]) &&
1270 (gid->domain == s_id[0])) {
1271 *loop_id = le16_to_cpu(gid->loop_id);
1272 res = 0;
1273 break;
1274 }
1275 id_iter += ha->gid_list_info_size;
1276 }
1277
1278out_free_id_list:
1279 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1280 gid_list, gid_list_dma);
1281 return res;
1282}
1283
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001284/*
1285 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1286 * Caller must put it.
1287 */
Quinn Tran5d964832017-01-19 22:27:59 -08001288static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001289 struct scsi_qla_host *vha,
1290 fc_port_t *fcport,
1291 bool local)
1292{
1293 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001294 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001295 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001296
Quinn Tran726b8542017-01-19 22:28:00 -08001297 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001298 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001299
1300 if (fcport->se_sess) {
1301 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001302 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001303 "%s: kref_get_unless_zero failed for %8phC\n",
1304 __func__, sess->port_name);
1305 return NULL;
1306 }
1307 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001308 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001309 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001310 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001311
Quinn Tran726b8542017-01-19 22:28:00 -08001312 /*
1313 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001314 * session eventually ends and release corresponding nport handle.
1315 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001316 * code will adjust these flags as necessary.
1317 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001318 sess->logout_on_delete = 1;
1319 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001320 sess->logout_completed = 0;
1321
1322 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1323 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001324 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001325 "(%d) %8phC check_initiator_node_acl failed\n",
1326 vha->vp_idx, fcport->port_name);
1327 return NULL;
1328 } else {
1329 kref_init(&fcport->sess_kref);
1330 /*
1331 * Take an extra reference to ->sess_kref here to handle
1332 * fc_port access across ->tgt.sess_lock reaquire.
1333 */
1334 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001335 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001336 "%s: kref_get_unless_zero failed for %8phC\n",
1337 __func__, sess->port_name);
1338 return NULL;
1339 }
1340
1341 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1342 if (!IS_SW_RESV_ADDR(sess->d_id))
1343 vha->vha_tgt.qla_tgt->sess_count++;
1344
1345 qlt_do_generation_tick(vha, &sess->generation);
1346 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1347 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001348
1349 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001350 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1351 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1352 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001353
1354 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001355 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1356 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1357 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001358 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1359 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001360
1361 return sess;
1362}
1363
1364/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001365 * max_gen - specifies maximum session generation
1366 * at which this deletion requestion is still valid
1367 */
1368void
1369qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001370{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001371 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001372 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001373 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001374
1375 if (!vha->hw->tgt.tgt_ops)
1376 return;
1377
Roland Dreierb2032fd2015-07-14 16:00:42 -04001378 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001379 return;
1380
Quinn Tran75601512015-12-17 14:57:04 -05001381 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001382 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001383 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001384 return;
1385 }
Quinn Tran5d964832017-01-19 22:27:59 -08001386 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001387 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001388 return;
1389 }
1390
Alexei Potashnikdf673272015-07-14 16:00:46 -04001391 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001392 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001393 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1394 "Ignoring stale deletion request for se_sess %p / sess %p"
1395 " for port %8phC, req_gen %d, sess_gen %d\n",
1396 sess->se_sess, sess, sess->port_name, max_gen,
1397 sess->generation);
1398 return;
1399 }
1400
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001401 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1402
1403 sess->local = 1;
1404 qlt_schedule_sess_for_deletion(sess, false);
Quinn Tran75601512015-12-17 14:57:04 -05001405 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001406}
1407
1408static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1409{
1410 struct qla_hw_data *ha = tgt->ha;
1411 unsigned long flags;
1412 int res;
1413 /*
1414 * We need to protect against race, when tgt is freed before or
1415 * inside wake_up()
1416 */
Quinn Tran726b8542017-01-19 22:28:00 -08001417 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001418 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001419 "tgt %p, sess_count=%d\n",
1420 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001421 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001422 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001423
1424 return res;
1425}
1426
1427/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001428int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001429{
1430 struct scsi_qla_host *vha = tgt->vha;
1431 struct qla_hw_data *ha = tgt->ha;
1432 unsigned long flags;
1433
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001434 mutex_lock(&qla_tgt_mutex);
1435 if (!vha->fc_vport) {
1436 struct Scsi_Host *sh = vha->host;
1437 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1438 bool npiv_vports;
1439
1440 spin_lock_irqsave(sh->host_lock, flags);
1441 npiv_vports = (fc_host->npiv_vports_inuse);
1442 spin_unlock_irqrestore(sh->host_lock, flags);
1443
1444 if (npiv_vports) {
1445 mutex_unlock(&qla_tgt_mutex);
Quinn Tran3a33dc92017-06-02 09:12:04 -07001446 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1447 "NPIV is in use. Can not stop target\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001448 return -EPERM;
1449 }
1450 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001451 if (tgt->tgt_stop || tgt->tgt_stopped) {
1452 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1453 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001454 mutex_unlock(&qla_tgt_mutex);
1455 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001456 }
1457
Quinn Tran3a33dc92017-06-02 09:12:04 -07001458 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001459 vha->host_no, vha);
1460 /*
1461 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1462 * Lock is needed, because we still can get an incoming packet.
1463 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001464 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001465 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001466 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001467 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001468 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001469 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001470 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001471
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001472 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1473 "Waiting for sess works (tgt %p)", tgt);
1474 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1475 while (!list_empty(&tgt->sess_works_list)) {
1476 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1477 flush_scheduled_work();
1478 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1479 }
1480 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1481
1482 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001483 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001484
Joe Carnucciob85e0952017-08-23 15:05:11 -07001485 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001486
1487 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001488 if (!ha->flags.host_shutting_down &&
1489 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001490 qlt_disable_vha(vha);
1491
1492 /* Wait for sessions to clear out (just in case) */
Joe Carnucciob85e0952017-08-23 15:05:11 -07001493 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001494 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001495}
1496EXPORT_SYMBOL(qlt_stop_phase1);
1497
1498/* Called by tcm_qla2xxx configfs code */
1499void qlt_stop_phase2(struct qla_tgt *tgt)
1500{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001501 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001502
1503 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001504 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001505 "Already in tgt->tgt_stopped state\n");
1506 dump_stack();
1507 return;
1508 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001509 if (!tgt->tgt_stop) {
1510 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1511 "%s: phase1 stop is not completed\n", __func__);
1512 dump_stack();
1513 return;
1514 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001515
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001516 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001517 tgt->tgt_stop = 0;
1518 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001519 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001520
Quinn Tran3a33dc92017-06-02 09:12:04 -07001521 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001522 tgt);
1523}
1524EXPORT_SYMBOL(qlt_stop_phase2);
1525
1526/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001527static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001528{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001529 scsi_qla_host_t *vha = tgt->vha;
Quinn Trane326d222017-06-13 20:47:18 -07001530 void *node;
1531 u64 key = 0;
1532 u16 i;
1533 struct qla_qpair_hint *h;
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001534 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001535
Quinn Tran3a33dc92017-06-02 09:12:04 -07001536 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1537 !tgt->tgt_stopped)
1538 qlt_stop_phase1(tgt);
1539
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001540 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001541 qlt_stop_phase2(tgt);
1542
Quinn Trane326d222017-06-13 20:47:18 -07001543 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1544 unsigned long flags;
1545
1546 h = &tgt->qphints[i];
1547 if (h->qpair) {
1548 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1549 list_del(&h->hint_elem);
1550 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1551 h->qpair = NULL;
1552 }
1553 }
1554 kfree(tgt->qphints);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001555 mutex_lock(&qla_tgt_mutex);
1556 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1557 mutex_unlock(&qla_tgt_mutex);
Quinn Trane326d222017-06-13 20:47:18 -07001558
1559 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1560 btree_remove64(&tgt->lun_qpair_map, key);
1561
1562 btree_destroy64(&tgt->lun_qpair_map);
1563
Sawan Chandak3be63b1e2017-12-04 14:45:04 -08001564 if (vha->vp_idx)
1565 if (ha->tgt.tgt_ops &&
1566 ha->tgt.tgt_ops->remove_target &&
1567 vha->vha_tgt.target_lport_ptr)
1568 ha->tgt.tgt_ops->remove_target(vha);
Quinn Tranbdbe24d2017-08-23 15:05:18 -07001569
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001570 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001571
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001572 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001573 "Release of tgt %p finished\n", tgt);
1574
1575 kfree(tgt);
1576}
1577
1578/* ha->hardware_lock supposed to be held on entry */
1579static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1580 const void *param, unsigned int param_size)
1581{
1582 struct qla_tgt_sess_work_param *prm;
1583 unsigned long flags;
1584
1585 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1586 if (!prm) {
1587 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1588 "qla_target(%d): Unable to create session "
1589 "work, command will be refused", 0);
1590 return -ENOMEM;
1591 }
1592
1593 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1594 "Scheduling work (type %d, prm %p)"
1595 " to find session for param %p (size %d, tgt %p)\n",
1596 type, prm, param, param_size, tgt);
1597
1598 prm->type = type;
1599 memcpy(&prm->tm_iocb, param, param_size);
1600
1601 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1602 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1603 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1604
1605 schedule_work(&tgt->sess_work);
1606
1607 return 0;
1608}
1609
1610/*
1611 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1612 */
Quinn Tran82de8022017-06-13 20:47:17 -07001613static void qlt_send_notify_ack(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001614 struct imm_ntfy_from_isp *ntfy,
1615 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1616 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1617{
Quinn Tran82de8022017-06-13 20:47:17 -07001618 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001619 struct qla_hw_data *ha = vha->hw;
1620 request_t *pkt;
1621 struct nack_to_isp *nack;
1622
Quinn Tranec7193e2017-03-15 09:48:55 -07001623 if (!ha->flags.fw_started)
1624 return;
1625
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001626 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1627
Quinn Tran82de8022017-06-13 20:47:17 -07001628 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001629 if (!pkt) {
1630 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1631 "qla_target(%d): %s failed: unable to allocate "
1632 "request packet\n", vha->vp_idx, __func__);
1633 return;
1634 }
1635
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001636 if (vha->vha_tgt.qla_tgt != NULL)
1637 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001638
1639 pkt->entry_type = NOTIFY_ACK_TYPE;
1640 pkt->entry_count = 1;
1641
1642 nack = (struct nack_to_isp *)pkt;
1643 nack->ox_id = ntfy->ox_id;
1644
Quinn Tran726b8542017-01-19 22:28:00 -08001645 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001646 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1647 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1648 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001649 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001650 }
1651 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1652 nack->u.isp24.status = ntfy->u.isp24.status;
1653 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001654 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001655 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1656 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1657 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1658 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1659 nack->u.isp24.srr_reject_code = srr_reject_code;
1660 nack->u.isp24.srr_reject_code_expl = srr_explan;
1661 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1662
1663 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1664 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1665 vha->vp_idx, nack->u.isp24.status);
1666
Himanshu Madhani63163e02014-09-25 06:14:59 -04001667 /* Memory Barrier */
1668 wmb();
Quinn Tran82de8022017-06-13 20:47:17 -07001669 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001670}
1671
1672/*
1673 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1674 */
Quinn Tran82de8022017-06-13 20:47:17 -07001675static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001676 struct abts_recv_from_24xx *abts, uint32_t status,
1677 bool ids_reversed)
1678{
Quinn Tran82de8022017-06-13 20:47:17 -07001679 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001680 struct qla_hw_data *ha = vha->hw;
1681 struct abts_resp_to_24xx *resp;
1682 uint32_t f_ctl;
1683 uint8_t *p;
1684
1685 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1686 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1687 ha, abts, status);
1688
Quinn Tran82de8022017-06-13 20:47:17 -07001689 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1690 NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001691 if (!resp) {
1692 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1693 "qla_target(%d): %s failed: unable to allocate "
1694 "request packet", vha->vp_idx, __func__);
1695 return;
1696 }
1697
1698 resp->entry_type = ABTS_RESP_24XX;
1699 resp->entry_count = 1;
1700 resp->nport_handle = abts->nport_handle;
1701 resp->vp_index = vha->vp_idx;
1702 resp->sof_type = abts->sof_type;
1703 resp->exchange_address = abts->exchange_address;
1704 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001705 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001706 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1707 F_CTL_SEQ_INITIATIVE);
1708 p = (uint8_t *)&f_ctl;
1709 resp->fcp_hdr_le.f_ctl[0] = *p++;
1710 resp->fcp_hdr_le.f_ctl[1] = *p++;
1711 resp->fcp_hdr_le.f_ctl[2] = *p;
1712 if (ids_reversed) {
1713 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1714 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1715 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1716 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1717 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1718 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1719 } else {
1720 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1721 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1722 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1723 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1724 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1725 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1726 }
1727 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1728 if (status == FCP_TMF_CMPL) {
1729 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1730 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1731 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1732 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1733 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1734 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1735 } else {
1736 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1737 resp->payload.ba_rjt.reason_code =
1738 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1739 /* Other bytes are zero */
1740 }
1741
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001742 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001743
Himanshu Madhani63163e02014-09-25 06:14:59 -04001744 /* Memory Barrier */
1745 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07001746 if (qpair->reqq_start_iocbs)
1747 qpair->reqq_start_iocbs(qpair);
1748 else
1749 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001750}
1751
1752/*
1753 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1754 */
1755static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1756 struct abts_resp_from_24xx_fw *entry)
1757{
1758 struct ctio7_to_24xx *ctio;
1759
1760 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1761 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001762
Quinn Tran82de8022017-06-13 20:47:17 -07001763 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(
1764 vha->hw->base_qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001765 if (ctio == NULL) {
1766 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1767 "qla_target(%d): %s failed: unable to allocate "
1768 "request packet\n", vha->vp_idx, __func__);
1769 return;
1770 }
1771
1772 /*
1773 * We've got on entrance firmware's response on by us generated
1774 * ABTS response. So, in it ID fields are reversed.
1775 */
1776
1777 ctio->entry_type = CTIO_TYPE7;
1778 ctio->entry_count = 1;
1779 ctio->nport_handle = entry->nport_handle;
1780 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001781 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001782 ctio->vp_index = vha->vp_idx;
1783 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1784 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1785 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1786 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001787 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1788 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001789 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001790
Himanshu Madhani63163e02014-09-25 06:14:59 -04001791 /* Memory Barrier */
1792 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001793 qla2x00_start_iocbs(vha, vha->req);
1794
Quinn Tran82de8022017-06-13 20:47:17 -07001795 qlt_24xx_send_abts_resp(vha->hw->base_qpair,
1796 (struct abts_recv_from_24xx *)entry,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001797 FCP_TMF_CMPL, true);
1798}
1799
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001800static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1801{
1802 struct qla_tgt_sess_op *op;
1803 struct qla_tgt_cmd *cmd;
Quinn Tran8b631d82017-06-02 09:11:54 -07001804 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001805
Quinn Tran8b631d82017-06-02 09:11:54 -07001806 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001807 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1808 if (tag == op->atio.u.isp24.exchange_addr) {
1809 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001810 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001811 return 1;
1812 }
1813 }
1814
Quinn Tran41dc5292017-01-19 22:28:03 -08001815 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1816 if (tag == op->atio.u.isp24.exchange_addr) {
1817 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001818 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08001819 return 1;
1820 }
1821 }
1822
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001823 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1824 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001825 cmd->aborted = 1;
Quinn Tran8b631d82017-06-02 09:11:54 -07001826 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001827 return 1;
1828 }
1829 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001830 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001831
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001832 return 0;
1833}
1834
1835/* drop cmds for the given lun
1836 * XXX only looks for cmds on the port through which lun reset was recieved
1837 * XXX does not go through the list of other port (which may have cmds
1838 * for the same lun)
1839 */
1840static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001841 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001842{
1843 struct qla_tgt_sess_op *op;
1844 struct qla_tgt_cmd *cmd;
1845 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001846 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001847
1848 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001849 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001850 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1851 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001852 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001853
1854 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1855 op_lun = scsilun_to_int(
1856 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1857 if (op_key == key && op_lun == lun)
1858 op->aborted = true;
1859 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001860
1861 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1862 uint32_t op_key;
1863 u64 op_lun;
1864
1865 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1866 op_lun = scsilun_to_int(
1867 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1868 if (op_key == key && op_lun == lun)
1869 op->aborted = true;
1870 }
1871
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001872 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1873 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001874 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001875
1876 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1877 cmd_lun = scsilun_to_int(
1878 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1879 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001880 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001881 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001882 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001883}
1884
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001885/* ha->hardware_lock supposed to be held on entry */
1886static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001887 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001888{
1889 struct qla_hw_data *ha = vha->hw;
1890 struct qla_tgt_mgmt_cmd *mcmd;
1891 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001892
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001893 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1894 /* send TASK_ABORT response immediately */
Linus Torvalds48ea2ce2017-07-13 14:27:32 -07001895 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_CMPL, false);
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001896 return 0;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001897 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001898
1899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1900 "qla_target(%d): task abort (tag=%d)\n",
1901 vha->vp_idx, abts->exchange_addr_to_abort);
1902
1903 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1904 if (mcmd == NULL) {
1905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1906 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1907 vha->vp_idx, __func__);
1908 return -ENOMEM;
1909 }
1910 memset(mcmd, 0, sizeof(*mcmd));
1911
1912 mcmd->sess = sess;
1913 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001914 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001915 mcmd->tmr_func = QLA_TGT_ABTS;
Quinn Tran82de8022017-06-13 20:47:17 -07001916 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07001917 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001918
Nicholas Bellingereb5ae232017-06-03 07:07:21 -07001919 /*
1920 * LUN is looked up by target-core internally based on the passed
1921 * abts->exchange_addr_to_abort tag.
1922 */
1923 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, 0, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001924 abts->exchange_addr_to_abort);
1925 if (rc != 0) {
1926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1927 "qla_target(%d): tgt_ops->handle_tmr()"
1928 " failed: %d", vha->vp_idx, rc);
1929 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1930 return -EFAULT;
1931 }
1932
1933 return 0;
1934}
1935
1936/*
1937 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1938 */
1939static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1940 struct abts_recv_from_24xx *abts)
1941{
1942 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001943 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001944 uint32_t tag = abts->exchange_addr_to_abort;
1945 uint8_t s_id[3];
1946 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001947 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001948
1949 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1950 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1951 "qla_target(%d): ABTS: Abort Sequence not "
1952 "supported\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07001953 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1954 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001955 return;
1956 }
1957
1958 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1959 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1960 "qla_target(%d): ABTS: Unknown Exchange "
1961 "Address received\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07001962 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1963 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001964 return;
1965 }
1966
1967 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1968 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1969 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1970 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1971 le32_to_cpu(abts->fcp_hdr_le.parameter));
1972
1973 s_id[0] = abts->fcp_hdr_le.s_id[2];
1974 s_id[1] = abts->fcp_hdr_le.s_id[1];
1975 s_id[2] = abts->fcp_hdr_le.s_id[0];
1976
Quinn Tran75601512015-12-17 14:57:04 -05001977 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001978 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1979 if (!sess) {
1980 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1981 "qla_target(%d): task abort for non-existant session\n",
1982 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001983 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001984 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001985
1986 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1987
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001988 if (rc != 0) {
Quinn Tran82de8022017-06-13 20:47:17 -07001989 qlt_24xx_send_abts_resp(ha->base_qpair, abts,
1990 FCP_TMF_REJECTED, false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001991 }
1992 return;
1993 }
Quinn Tran75601512015-12-17 14:57:04 -05001994 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1995
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001996
Quinn Tran726b8542017-01-19 22:28:00 -08001997 if (sess->deleted) {
Quinn Tran82de8022017-06-13 20:47:17 -07001998 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
1999 false);
Alexei Potashnike52a8b42015-07-14 16:00:48 -04002000 return;
2001 }
2002
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002003 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2004 if (rc != 0) {
2005 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2006 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2007 vha->vp_idx, rc);
Quinn Tran82de8022017-06-13 20:47:17 -07002008 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2009 false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002010 return;
2011 }
2012}
2013
2014/*
2015 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2016 */
Quinn Tran82de8022017-06-13 20:47:17 -07002017static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002018 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2019{
Quinn Tran72fcd4e2017-08-23 15:05:13 -07002020 struct scsi_qla_host *ha = mcmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002021 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2022 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002023 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002024
2025 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2026 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2027 ha, atio, resp_code);
2028
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002029
Quinn Tran82de8022017-06-13 20:47:17 -07002030 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002031 if (ctio == NULL) {
2032 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2033 "qla_target(%d): %s failed: unable to allocate "
2034 "request packet\n", ha->vp_idx, __func__);
2035 return;
2036 }
2037
2038 ctio->entry_type = CTIO_TYPE7;
2039 ctio->entry_count = 1;
2040 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2041 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002042 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002043 ctio->vp_index = ha->vp_idx;
2044 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2045 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2046 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2047 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002048 temp = (atio->u.isp24.attr << 9)|
2049 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2050 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002051 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2052 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002053 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002054 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2055 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002056 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002057
Himanshu Madhani63163e02014-09-25 06:14:59 -04002058 /* Memory Barrier */
2059 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002060 if (qpair->reqq_start_iocbs)
2061 qpair->reqq_start_iocbs(qpair);
2062 else
2063 qla2x00_start_iocbs(ha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002064}
2065
2066void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2067{
2068 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2069}
2070EXPORT_SYMBOL(qlt_free_mcmd);
2071
Quinn Tranbe251522017-03-15 09:48:49 -07002072/*
2073 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2074 * reacquire
2075 */
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002076void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07002077 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2078{
2079 struct atio_from_isp *atio = &cmd->atio;
2080 struct ctio7_to_24xx *ctio;
2081 uint16_t temp;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002082 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranbe251522017-03-15 09:48:49 -07002083
2084 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2085 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2086 "sense_key=%02x, asc=%02x, ascq=%02x",
2087 vha, atio, scsi_status, sense_key, asc, ascq);
2088
2089 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2090 if (!ctio) {
2091 ql_dbg(ql_dbg_async, vha, 0x3067,
2092 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2093 vha->host_no, __func__);
2094 goto out;
2095 }
2096
2097 ctio->entry_type = CTIO_TYPE7;
2098 ctio->entry_count = 1;
2099 ctio->handle = QLA_TGT_SKIP_HANDLE;
2100 ctio->nport_handle = cmd->sess->loop_id;
2101 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2102 ctio->vp_index = vha->vp_idx;
2103 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2104 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2105 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2106 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002107 temp = (atio->u.isp24.attr << 9) |
2108 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2109 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002110 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2111 ctio->u.status1.ox_id = cpu_to_le16(temp);
2112 ctio->u.status1.scsi_status =
2113 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2114 ctio->u.status1.response_len = cpu_to_le16(18);
2115 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2116
2117 if (ctio->u.status1.residual != 0)
2118 ctio->u.status1.scsi_status |=
2119 cpu_to_le16(SS_RESIDUAL_UNDER);
2120
2121 /* Response code and sense key */
2122 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2123 (&ctio->u.status1.sense_data)[0]);
2124 /* Additional sense length */
2125 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2126 /* ASC and ASCQ */
2127 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2128 (&ctio->u.status1.sense_data)[3]);
2129
2130 /* Memory Barrier */
2131 wmb();
2132
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002133 if (qpair->reqq_start_iocbs)
2134 qpair->reqq_start_iocbs(qpair);
2135 else
2136 qla2x00_start_iocbs(vha, qpair->req);
2137
Quinn Tranbe251522017-03-15 09:48:49 -07002138out:
2139 return;
2140}
2141
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002142/* callback from target fabric module code */
2143void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2144{
2145 struct scsi_qla_host *vha = mcmd->sess->vha;
2146 struct qla_hw_data *ha = vha->hw;
2147 unsigned long flags;
Quinn Tran82de8022017-06-13 20:47:17 -07002148 struct qla_qpair *qpair = mcmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002149
2150 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2151 "TM response mcmd (%p) status %#x state %#x",
2152 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2153
Quinn Tran82de8022017-06-13 20:47:17 -07002154 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002155
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002156 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002157 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002158 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002159 * previous life, just abort the processing.
2160 */
2161 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002162 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2163 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002164 mcmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002165 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002166 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002167 return;
2168 }
2169
Quinn Tran726b8542017-01-19 22:28:00 -08002170 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2171 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002172 ELS_LOGO ||
2173 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2174 ELS_PRLO ||
2175 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2176 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002177 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002178 "TM response logo %phC status %#x state %#x",
2179 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2180 mcmd->flags);
2181 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2182 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002183 qlt_send_notify_ack(vha->hw->base_qpair,
2184 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
Quinn Tran726b8542017-01-19 22:28:00 -08002185 }
2186 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002187 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Quinn Tran82de8022017-06-13 20:47:17 -07002188 qlt_24xx_send_abts_resp(qpair, &mcmd->orig_iocb.abts,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002189 mcmd->fc_tm_rsp, false);
2190 else
Quinn Tran82de8022017-06-13 20:47:17 -07002191 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002192 mcmd->fc_tm_rsp);
2193 }
2194 /*
2195 * Make the callback for ->free_mcmd() to queue_work() and invoke
2196 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2197 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2198 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2199 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2200 * qlt_xmit_tm_rsp() returns here..
2201 */
2202 ha->tgt.tgt_ops->free_mcmd(mcmd);
Quinn Tran82de8022017-06-13 20:47:17 -07002203 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002204}
2205EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2206
2207/* No locks */
2208static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2209{
2210 struct qla_tgt_cmd *cmd = prm->cmd;
2211
2212 BUG_ON(cmd->sg_cnt == 0);
2213
2214 prm->sg = (struct scatterlist *)cmd->sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002215 prm->seg_cnt = pci_map_sg(cmd->qpair->pdev, cmd->sg,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002216 cmd->sg_cnt, cmd->dma_data_direction);
2217 if (unlikely(prm->seg_cnt == 0))
2218 goto out_err;
2219
2220 prm->cmd->sg_mapped = 1;
2221
Quinn Tranf83adb62014-04-11 16:54:43 -04002222 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2223 /*
2224 * If greater than four sg entries then we need to allocate
2225 * the continuation entries
2226 */
Quinn Tranb5399f72017-06-13 20:47:27 -07002227 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
Quinn Tranf83adb62014-04-11 16:54:43 -04002228 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
Quinn Tranb5399f72017-06-13 20:47:27 -07002229 QLA_TGT_DATASEGS_PER_CMD_24XX,
2230 QLA_TGT_DATASEGS_PER_CONT_24XX);
Quinn Tranf83adb62014-04-11 16:54:43 -04002231 } else {
2232 /* DIF */
2233 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2234 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2235 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2236 prm->tot_dsds = prm->seg_cnt;
2237 } else
2238 prm->tot_dsds = prm->seg_cnt;
2239
2240 if (cmd->prot_sg_cnt) {
2241 prm->prot_sg = cmd->prot_sg;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002242 prm->prot_seg_cnt = pci_map_sg(cmd->qpair->pdev,
Quinn Tranf83adb62014-04-11 16:54:43 -04002243 cmd->prot_sg, cmd->prot_sg_cnt,
2244 cmd->dma_data_direction);
2245 if (unlikely(prm->prot_seg_cnt == 0))
2246 goto out_err;
2247
2248 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2249 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2250 /* Dif Bundling not support here */
2251 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2252 cmd->blk_sz);
2253 prm->tot_dsds += prm->prot_seg_cnt;
2254 } else
2255 prm->tot_dsds += prm->prot_seg_cnt;
2256 }
2257 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002258
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002259 return 0;
2260
2261out_err:
Quinn Tran22d84722017-06-13 20:47:25 -07002262 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002263 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2264 0, prm->cmd->sg_cnt);
2265 return -1;
2266}
2267
Joern Engelf9b67212014-09-16 16:23:18 -04002268static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002269{
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002270 struct qla_hw_data *ha;
2271 struct qla_qpair *qpair;
Joern Engelf9b67212014-09-16 16:23:18 -04002272 if (!cmd->sg_mapped)
2273 return;
2274
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002275 qpair = cmd->qpair;
2276
2277 pci_unmap_sg(qpair->pdev, cmd->sg, cmd->sg_cnt,
2278 cmd->dma_data_direction);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002279 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002280
2281 if (cmd->prot_sg_cnt)
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002282 pci_unmap_sg(qpair->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
Quinn Tranf83adb62014-04-11 16:54:43 -04002283 cmd->dma_data_direction);
2284
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002285 if (!cmd->ctx)
2286 return;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002287 ha = vha->hw;
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002288 if (cmd->ctx_dsd_alloced)
2289 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2290
2291 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002292}
2293
Quinn Tran82de8022017-06-13 20:47:17 -07002294static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002295 uint32_t req_cnt)
2296{
Quinn Trand63b3282017-06-02 09:12:07 -07002297 uint32_t cnt;
Quinn Tran82de8022017-06-13 20:47:17 -07002298 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002299
Quinn Tran82de8022017-06-13 20:47:17 -07002300 if (req->cnt < (req_cnt + 2)) {
Quinn Tranaf7bb382017-06-13 20:47:23 -07002301 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2302 RD_REG_DWORD_RELAXED(req->req_q_out));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002303
Quinn Tran82de8022017-06-13 20:47:17 -07002304 if (req->ring_index < cnt)
2305 req->cnt = cnt - req->ring_index;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002306 else
Quinn Tran82de8022017-06-13 20:47:17 -07002307 req->cnt = req->length - (req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002308
Quinn Tran82de8022017-06-13 20:47:17 -07002309 if (unlikely(req->cnt < (req_cnt + 2)))
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002310 return -EAGAIN;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002311 }
2312
Quinn Tran82de8022017-06-13 20:47:17 -07002313 req->cnt -= req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002314
2315 return 0;
2316}
2317
2318/*
2319 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2320 */
Quinn Tran82de8022017-06-13 20:47:17 -07002321static inline void *qlt_get_req_pkt(struct req_que *req)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002322{
2323 /* Adjust ring index. */
Quinn Tran82de8022017-06-13 20:47:17 -07002324 req->ring_index++;
2325 if (req->ring_index == req->length) {
2326 req->ring_index = 0;
2327 req->ring_ptr = req->ring;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002328 } else {
Quinn Tran82de8022017-06-13 20:47:17 -07002329 req->ring_ptr++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002330 }
Quinn Tran82de8022017-06-13 20:47:17 -07002331 return (cont_entry_t *)req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002332}
2333
2334/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002335static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002336{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002337 uint32_t h;
Quinn Tranc5419e22017-06-13 20:47:16 -07002338 int index;
2339 uint8_t found = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07002340 struct req_que *req = qpair->req;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002341
Quinn Tranc5419e22017-06-13 20:47:16 -07002342 h = req->current_outstanding_cmd;
2343
2344 for (index = 1; index < req->num_outstanding_cmds; index++) {
2345 h++;
2346 if (h == req->num_outstanding_cmds)
2347 h = 1;
2348
2349 if (h == QLA_TGT_SKIP_HANDLE)
2350 continue;
2351
2352 if (!req->outstanding_cmds[h]) {
2353 found = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002354 break;
2355 }
Quinn Tranc5419e22017-06-13 20:47:16 -07002356 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002357
Quinn Tranc5419e22017-06-13 20:47:16 -07002358 if (found) {
2359 req->current_outstanding_cmd = h;
2360 } else {
Quinn Tran8abfa9e2017-06-13 20:47:24 -07002361 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2362 "qla_target(%d): Ran out of empty cmd slots\n",
2363 qpair->vha->vp_idx);
Quinn Tranc5419e22017-06-13 20:47:16 -07002364 h = QLA_TGT_NULL_HANDLE;
2365 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002366
2367 return h;
2368}
2369
2370/* ha->hardware_lock supposed to be held on entry */
Quinn Tran82de8022017-06-13 20:47:17 -07002371static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2372 struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002373{
2374 uint32_t h;
2375 struct ctio7_to_24xx *pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002376 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002377 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002378
Quinn Tran82de8022017-06-13 20:47:17 -07002379 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002380 prm->pkt = pkt;
2381 memset(pkt, 0, sizeof(*pkt));
2382
2383 pkt->entry_type = CTIO_TYPE7;
2384 pkt->entry_count = (uint8_t)prm->req_cnt;
Quinn Tran22d84722017-06-13 20:47:25 -07002385 pkt->vp_index = prm->cmd->vp_idx;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002386
Quinn Tran82de8022017-06-13 20:47:17 -07002387 h = qlt_make_handle(qpair);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002388 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2389 /*
2390 * CTIO type 7 from the firmware doesn't provide a way to
2391 * know the initiator's LOOP ID, hence we can't find
2392 * the session and, so, the command.
2393 */
2394 return -EAGAIN;
Quinn Trane326d222017-06-13 20:47:18 -07002395 } else
2396 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002397
Quinn Tran82de8022017-06-13 20:47:17 -07002398 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2399 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2400 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002401 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002402 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2403 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2404 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2405 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002406 temp = atio->u.isp24.attr << 9;
2407 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002408 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2409 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002410 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2411
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002412 return 0;
2413}
2414
2415/*
2416 * ha->hardware_lock supposed to be held on entry. We have already made sure
2417 * that there is sufficient amount of request entries to not drop it.
2418 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002419static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002420{
2421 int cnt;
2422 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002423
2424 /* Build continuation packets */
2425 while (prm->seg_cnt > 0) {
2426 cont_a64_entry_t *cont_pkt64 =
Quinn Tran82de8022017-06-13 20:47:17 -07002427 (cont_a64_entry_t *)qlt_get_req_pkt(
2428 prm->cmd->qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002429
2430 /*
2431 * Make sure that from cont_pkt64 none of
2432 * 64-bit specific fields used for 32-bit
2433 * addressing. Cast to (cont_entry_t *) for
2434 * that.
2435 */
2436
2437 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2438
2439 cont_pkt64->entry_count = 1;
2440 cont_pkt64->sys_define = 0;
2441
Quinn Tran32d29b42017-06-13 20:47:26 -07002442 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2443 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002444
2445 /* Load continuation entry data segments */
2446 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002447 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002448 cnt++, prm->seg_cnt--) {
2449 *dword_ptr++ =
2450 cpu_to_le32(pci_dma_lo32
2451 (sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002452 *dword_ptr++ = cpu_to_le32(pci_dma_hi32
2453 (sg_dma_address(prm->sg)));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002454 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2455
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002456 prm->sg = sg_next(prm->sg);
2457 }
2458 }
2459}
2460
2461/*
2462 * ha->hardware_lock supposed to be held on entry. We have already made sure
2463 * that there is sufficient amount of request entries to not drop it.
2464 */
Quinn Tran32d29b42017-06-13 20:47:26 -07002465static void qlt_load_data_segments(struct qla_tgt_prm *prm)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002466{
2467 int cnt;
2468 uint32_t *dword_ptr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002469 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2470
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002471 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2472
2473 /* Setup packet address segment pointer */
2474 dword_ptr = pkt24->u.status0.dseg_0_address;
2475
2476 /* Set total data segment count */
2477 if (prm->seg_cnt)
2478 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2479
2480 if (prm->seg_cnt == 0) {
2481 /* No data transfer */
2482 *dword_ptr++ = 0;
2483 *dword_ptr = 0;
2484 return;
2485 }
2486
2487 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002488
2489 /* Load command entry data segments */
2490 for (cnt = 0;
Quinn Tranb5399f72017-06-13 20:47:27 -07002491 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002492 cnt++, prm->seg_cnt--) {
2493 *dword_ptr++ =
2494 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
Quinn Tran32d29b42017-06-13 20:47:26 -07002495
2496 *dword_ptr++ = cpu_to_le32(pci_dma_hi32(
2497 sg_dma_address(prm->sg)));
2498
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002499 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2500
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002501 prm->sg = sg_next(prm->sg);
2502 }
2503
Quinn Tran32d29b42017-06-13 20:47:26 -07002504 qlt_load_cont_data_segments(prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002505}
2506
2507static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2508{
2509 return cmd->bufflen > 0;
2510}
2511
Quinn Tranbe251522017-03-15 09:48:49 -07002512static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2513{
2514 struct qla_tgt_cmd *cmd;
2515 struct scsi_qla_host *vha;
2516
2517 /* asc 0x10=dif error */
2518 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2519 cmd = prm->cmd;
2520 vha = cmd->vha;
2521 /* ASCQ */
2522 switch (prm->sense_buffer[13]) {
2523 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002524 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002525 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2526 "se_cmd=%p tag[%x]",
2527 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2528 cmd->atio.u.isp24.exchange_addr);
2529 break;
2530 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002531 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002532 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2533 "se_cmd=%p tag[%x]",
2534 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2535 cmd->atio.u.isp24.exchange_addr);
2536 break;
2537 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002538 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002539 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2540 "se_cmd=%p tag[%x]",
2541 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2542 cmd->atio.u.isp24.exchange_addr);
2543 break;
2544 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002545 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002546 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2547 "se_cmd=%p tag[%x]",
2548 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2549 cmd->atio.u.isp24.exchange_addr);
2550 break;
2551 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002552 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002553 }
2554}
2555
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002556/*
2557 * Called without ha->hardware_lock held
2558 */
2559static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2560 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2561 uint32_t *full_req_cnt)
2562{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002563 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Tran22d84722017-06-13 20:47:25 -07002564 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002565
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002566 prm->cmd = cmd;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002567 prm->tgt = cmd->tgt;
2568 prm->pkt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002569 prm->rq_result = scsi_status;
2570 prm->sense_buffer = &cmd->sense_buffer[0];
2571 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2572 prm->sg = NULL;
2573 prm->seg_cnt = -1;
2574 prm->req_cnt = 1;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002575 prm->residual = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002576 prm->add_status_pkt = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002577 prm->prot_sg = NULL;
2578 prm->prot_seg_cnt = 0;
2579 prm->tot_dsds = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002580
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002581 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2582 if (qlt_pci_map_calc_cnt(prm) != 0)
2583 return -EAGAIN;
2584 }
2585
2586 *full_req_cnt = prm->req_cnt;
2587
2588 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2589 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002590 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002591 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2592 prm->residual, se_cmd->tag,
2593 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2594 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002595 prm->rq_result |= SS_RESIDUAL_UNDER;
2596 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2597 prm->residual = se_cmd->residual_count;
Quinn Tran22d84722017-06-13 20:47:25 -07002598 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002599 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2600 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2601 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002602 prm->rq_result |= SS_RESIDUAL_OVER;
2603 }
2604
2605 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2606 /*
2607 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2608 * ignored in *xmit_response() below
2609 */
2610 if (qlt_has_data(cmd)) {
2611 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002612 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002613 (prm->rq_result != 0))) {
2614 prm->add_status_pkt = 1;
2615 (*full_req_cnt)++;
2616 }
2617 }
2618 }
2619
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002620 return 0;
2621}
2622
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002623static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2624 int sending_sense)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002625{
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002626 if (cmd->qpair->enable_class_2)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002627 return 0;
2628
2629 if (sending_sense)
2630 return cmd->conf_compl_supported;
2631 else
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002632 return cmd->qpair->enable_explicit_conf &&
2633 cmd->conf_compl_supported;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002634}
2635
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002636static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2637 struct qla_tgt_prm *prm)
2638{
2639 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2640 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002641 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002642 if (qlt_need_explicit_conf(prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002643 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002644 CTIO7_FLAGS_EXPLICIT_CONFORM |
2645 CTIO7_FLAGS_CONFORM_REQ);
2646 }
2647 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2648 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2649 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2650 int i;
2651
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07002652 if (qlt_need_explicit_conf(prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002653 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Quinn Tran22d84722017-06-13 20:47:25 -07002654 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002655 "Skipping EXPLICIT_CONFORM and "
2656 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2657 "non GOOD status\n");
2658 goto skip_explict_conf;
2659 }
Bart Van Asschead950362015-07-09 07:24:08 -07002660 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002661 CTIO7_FLAGS_EXPLICIT_CONFORM |
2662 CTIO7_FLAGS_CONFORM_REQ);
2663 }
2664skip_explict_conf:
2665 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002666 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002667 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002668 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002669 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002670 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002671 ctio->u.status1.sense_length =
2672 cpu_to_le16(prm->sense_buffer_len);
2673 for (i = 0; i < prm->sense_buffer_len/4; i++)
2674 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2675 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002676
2677 qlt_print_dif_err(prm);
2678
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002679 } else {
2680 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002681 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002682 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002683 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002684 ctio->u.status1.sense_length = 0;
2685 memset(ctio->u.status1.sense_data, 0,
2686 sizeof(ctio->u.status1.sense_data));
2687 }
2688
2689 /* Sense with len > 24, is it possible ??? */
2690}
2691
Quinn Tranf83adb62014-04-11 16:54:43 -04002692static inline int
2693qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2694{
Quinn Tranf83adb62014-04-11 16:54:43 -04002695 switch (se_cmd->prot_op) {
2696 case TARGET_PROT_DOUT_INSERT:
2697 case TARGET_PROT_DIN_STRIP:
2698 if (ql2xenablehba_err_chk >= 1)
2699 return 1;
2700 break;
2701 case TARGET_PROT_DOUT_PASS:
2702 case TARGET_PROT_DIN_PASS:
2703 if (ql2xenablehba_err_chk >= 2)
2704 return 1;
2705 break;
2706 case TARGET_PROT_DIN_INSERT:
2707 case TARGET_PROT_DOUT_STRIP:
2708 return 1;
2709 default:
2710 break;
2711 }
2712 return 0;
2713}
2714
Quinn Tranbe251522017-03-15 09:48:49 -07002715static inline int
2716qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002717{
Quinn Tranbe251522017-03-15 09:48:49 -07002718 switch (se_cmd->prot_op) {
2719 case TARGET_PROT_DIN_INSERT:
2720 case TARGET_PROT_DOUT_INSERT:
2721 case TARGET_PROT_DIN_STRIP:
2722 case TARGET_PROT_DOUT_STRIP:
2723 case TARGET_PROT_DIN_PASS:
2724 case TARGET_PROT_DOUT_PASS:
2725 return 1;
2726 default:
2727 return 0;
2728 }
2729 return 0;
2730}
Quinn Tranf83adb62014-04-11 16:54:43 -04002731
Quinn Tranbe251522017-03-15 09:48:49 -07002732/*
2733 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2734 */
2735static void
2736qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2737 uint16_t *pfw_prot_opts)
2738{
2739 struct se_cmd *se_cmd = &cmd->se_cmd;
2740 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2741 scsi_qla_host_t *vha = cmd->tgt->vha;
2742 struct qla_hw_data *ha = vha->hw;
2743 uint32_t t32 = 0;
2744
2745 /*
2746 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002747 * have been immplemented by TCM, before AppTag is avail.
2748 * Look for modesense_handlers[]
2749 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002750 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002751 ctx->app_tag_mask[0] = 0x0;
2752 ctx->app_tag_mask[1] = 0x0;
2753
Quinn Tranbe251522017-03-15 09:48:49 -07002754 if (IS_PI_UNINIT_CAPABLE(ha)) {
2755 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2756 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2757 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2758 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2759 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2760 }
2761
2762 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2763
Quinn Tranf83adb62014-04-11 16:54:43 -04002764 switch (se_cmd->prot_type) {
2765 case TARGET_DIF_TYPE0_PROT:
2766 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002767 * No check for ql2xenablehba_err_chk, as it
2768 * would be an I/O error if hba tag generation
2769 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002770 */
2771 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002772 /* enable ALL bytes of the ref tag */
2773 ctx->ref_tag_mask[0] = 0xff;
2774 ctx->ref_tag_mask[1] = 0xff;
2775 ctx->ref_tag_mask[2] = 0xff;
2776 ctx->ref_tag_mask[3] = 0xff;
2777 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002778 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002779 /*
2780 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2781 * REF tag, and 16 bit app tag.
2782 */
2783 ctx->ref_tag = cpu_to_le32(lba);
2784 if (!qla_tgt_ref_mask_check(se_cmd) ||
2785 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2786 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2787 break;
2788 }
2789 /* enable ALL bytes of the ref tag */
2790 ctx->ref_tag_mask[0] = 0xff;
2791 ctx->ref_tag_mask[1] = 0xff;
2792 ctx->ref_tag_mask[2] = 0xff;
2793 ctx->ref_tag_mask[3] = 0xff;
2794 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002795 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002796 /*
2797 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2798 * tag has to match LBA in CDB + N
2799 */
2800 ctx->ref_tag = cpu_to_le32(lba);
2801 if (!qla_tgt_ref_mask_check(se_cmd) ||
2802 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2803 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2804 break;
2805 }
2806 /* enable ALL bytes of the ref tag */
2807 ctx->ref_tag_mask[0] = 0xff;
2808 ctx->ref_tag_mask[1] = 0xff;
2809 ctx->ref_tag_mask[2] = 0xff;
2810 ctx->ref_tag_mask[3] = 0xff;
2811 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002812 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002813 /* For TYPE 3 protection: 16 bit GUARD only */
2814 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2815 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2816 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2817 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002818 }
2819}
2820
Quinn Tranf83adb62014-04-11 16:54:43 -04002821static inline int
Quinn Tran82de8022017-06-13 20:47:17 -07002822qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
Quinn Tranf83adb62014-04-11 16:54:43 -04002823{
2824 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002825 uint32_t transfer_length = 0;
2826 uint32_t data_bytes;
2827 uint32_t dif_bytes;
2828 uint8_t bundling = 1;
2829 uint8_t *clr_ptr;
2830 struct crc_context *crc_ctx_pkt = NULL;
2831 struct qla_hw_data *ha;
2832 struct ctio_crc2_to_fw *pkt;
2833 dma_addr_t crc_ctx_dma;
2834 uint16_t fw_prot_opts = 0;
2835 struct qla_tgt_cmd *cmd = prm->cmd;
2836 struct se_cmd *se_cmd = &cmd->se_cmd;
2837 uint32_t h;
2838 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002839 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002840 uint16_t t16;
Quinn Tran82de8022017-06-13 20:47:17 -07002841 scsi_qla_host_t *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04002842
Quinn Tranf83adb62014-04-11 16:54:43 -04002843 ha = vha->hw;
2844
Quinn Tran82de8022017-06-13 20:47:17 -07002845 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
Quinn Tranf83adb62014-04-11 16:54:43 -04002846 prm->pkt = pkt;
2847 memset(pkt, 0, sizeof(*pkt));
2848
Quinn Tran22d84722017-06-13 20:47:25 -07002849 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
Quinn Tranf83adb62014-04-11 16:54:43 -04002850 "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 -07002851 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
Quinn Tranf83adb62014-04-11 16:54:43 -04002852 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2853
2854 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2855 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2856 bundling = 0;
2857
2858 /* Compute dif len and adjust data len to incude protection */
2859 data_bytes = cmd->bufflen;
2860 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2861
2862 switch (se_cmd->prot_op) {
2863 case TARGET_PROT_DIN_INSERT:
2864 case TARGET_PROT_DOUT_STRIP:
2865 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002866 if (cmd->prot_sg_cnt)
2867 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002868 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002869 case TARGET_PROT_DIN_STRIP:
2870 case TARGET_PROT_DOUT_INSERT:
2871 case TARGET_PROT_DIN_PASS:
2872 case TARGET_PROT_DOUT_PASS:
2873 transfer_length = data_bytes + dif_bytes;
2874 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002875 default:
2876 BUG();
2877 break;
2878 }
2879
2880 if (!qlt_hba_err_chk_enabled(se_cmd))
2881 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2882 /* HBA error checking enabled */
2883 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2884 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2885 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2886 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2887 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2888 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2889 }
2890
2891 switch (se_cmd->prot_op) {
2892 case TARGET_PROT_DIN_INSERT:
2893 case TARGET_PROT_DOUT_INSERT:
2894 fw_prot_opts |= PO_MODE_DIF_INSERT;
2895 break;
2896 case TARGET_PROT_DIN_STRIP:
2897 case TARGET_PROT_DOUT_STRIP:
2898 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2899 break;
2900 case TARGET_PROT_DIN_PASS:
2901 case TARGET_PROT_DOUT_PASS:
2902 fw_prot_opts |= PO_MODE_DIF_PASS;
2903 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2904 break;
2905 default:/* Normal Request */
2906 fw_prot_opts |= PO_MODE_DIF_PASS;
2907 break;
2908 }
2909
Quinn Tranf83adb62014-04-11 16:54:43 -04002910 /* ---- PKT ---- */
2911 /* Update entry type to indicate Command Type CRC_2 IOCB */
2912 pkt->entry_type = CTIO_CRC2;
2913 pkt->entry_count = 1;
Quinn Tran22d84722017-06-13 20:47:25 -07002914 pkt->vp_index = cmd->vp_idx;
Quinn Tranf83adb62014-04-11 16:54:43 -04002915
Quinn Tran82de8022017-06-13 20:47:17 -07002916 h = qlt_make_handle(qpair);
Quinn Tranf83adb62014-04-11 16:54:43 -04002917 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2918 /*
2919 * CTIO type 7 from the firmware doesn't provide a way to
2920 * know the initiator's LOOP ID, hence we can't find
2921 * the session and, so, the command.
2922 */
2923 return -EAGAIN;
2924 } else
Quinn Tran82de8022017-06-13 20:47:17 -07002925 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002926
Quinn Tran82de8022017-06-13 20:47:17 -07002927 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2928 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07002929 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002930 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002931 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2932 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2933 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2934 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002935
2936 /* silence compile warning */
2937 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2938 pkt->ox_id = cpu_to_le16(t16);
2939
2940 t16 = (atio->u.isp24.attr << 9);
2941 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002942 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2943
2944 /* Set transfer direction */
2945 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002946 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002947 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002948 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002949
Quinn Tranf83adb62014-04-11 16:54:43 -04002950 pkt->dseg_count = prm->tot_dsds;
2951 /* Fibre channel byte count */
2952 pkt->transfer_length = cpu_to_le32(transfer_length);
2953
Quinn Tranf83adb62014-04-11 16:54:43 -04002954 /* ----- CRC context -------- */
2955
2956 /* Allocate CRC context from global pool */
2957 crc_ctx_pkt = cmd->ctx =
2958 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2959
2960 if (!crc_ctx_pkt)
2961 goto crc_queuing_error;
2962
2963 /* Zero out CTX area. */
2964 clr_ptr = (uint8_t *)crc_ctx_pkt;
2965 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2966
2967 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2968 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2969
2970 /* Set handle */
2971 crc_ctx_pkt->handle = pkt->handle;
2972
Quinn Tranbe251522017-03-15 09:48:49 -07002973 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04002974
2975 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2976 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2977 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2978
Quinn Tranf83adb62014-04-11 16:54:43 -04002979 if (!bundling) {
2980 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2981 } else {
2982 /*
2983 * Configure Bundling if we need to fetch interlaving
2984 * protection PCI accesses
2985 */
2986 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2987 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2988 crc_ctx_pkt->u.bundling.dseg_count =
2989 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2990 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2991 }
2992
2993 /* Finish the common fields of CRC pkt */
2994 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2995 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2996 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002997 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002998
Quinn Tranbe251522017-03-15 09:48:49 -07002999 memset((uint8_t *)&tc, 0 , sizeof(tc));
3000 tc.vha = vha;
3001 tc.blk_sz = cmd->blk_sz;
3002 tc.bufflen = cmd->bufflen;
3003 tc.sg = cmd->sg;
3004 tc.prot_sg = cmd->prot_sg;
3005 tc.ctx = crc_ctx_pkt;
3006 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04003007
3008 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07003009 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04003010
3011 if (!bundling && prm->prot_seg_cnt) {
3012 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003013 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003014 goto crc_queuing_error;
3015 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003016 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003017 goto crc_queuing_error;
3018
3019 if (bundling && prm->prot_seg_cnt) {
3020 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04003021 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04003022
3023 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3024 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07003025 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04003026 goto crc_queuing_error;
3027 }
3028 return QLA_SUCCESS;
3029
3030crc_queuing_error:
3031 /* Cleanup will be performed by the caller */
Quinn Tran82de8022017-06-13 20:47:17 -07003032 qpair->req->outstanding_cmds[h] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003033
3034 return QLA_FUNCTION_FAILED;
3035}
3036
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003037/*
3038 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3039 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3040 */
3041int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3042 uint8_t scsi_status)
3043{
3044 struct scsi_qla_host *vha = cmd->vha;
Quinn Tran82de8022017-06-13 20:47:17 -07003045 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003046 struct ctio7_to_24xx *pkt;
3047 struct qla_tgt_prm prm;
3048 uint32_t full_req_cnt = 0;
3049 unsigned long flags = 0;
3050 int res;
3051
Quinn Tran726b8542017-01-19 22:28:00 -08003052 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003053 cmd->state = QLA_TGT_STATE_PROCESSED;
3054 if (cmd->sess->logout_completed)
3055 /* no need to terminate. FW already freed exchange. */
3056 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3057 else
Quinn Tran82de8022017-06-13 20:47:17 -07003058 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 0, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003059 return 0;
3060 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003061
Quinn Tran22d84722017-06-13 20:47:25 -07003062 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
Quinn Tran82de8022017-06-13 20:47:17 -07003063 "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 -04003064 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3065 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
Quinn Tran82de8022017-06-13 20:47:17 -07003066 &cmd->se_cmd, qpair->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003067
3068 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3069 &full_req_cnt);
3070 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003071 return res;
3072 }
3073
Quinn Tran82de8022017-06-13 20:47:17 -07003074 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003075
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003076 if (xmit_type == QLA_TGT_XMIT_STATUS)
Quinn Tran60a9ead2017-06-13 20:47:28 -07003077 qpair->tgt_counters.core_qla_snd_status++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003078 else
Quinn Tran60a9ead2017-06-13 20:47:28 -07003079 qpair->tgt_counters.core_qla_que_buf++;
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003080
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003081 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003082 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003083 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003084 * previous life, just abort the processing.
3085 */
3086 cmd->state = QLA_TGT_STATE_PROCESSED;
3087 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003088 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003089 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3090 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003091 cmd->reset_count, qpair->chip_reset);
Quinn Tran82de8022017-06-13 20:47:17 -07003092 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04003093 return 0;
3094 }
3095
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003096 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003097 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003098 if (unlikely(res))
3099 goto out_unmap_unlock;
3100
Quinn Tranf83adb62014-04-11 16:54:43 -04003101 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
Quinn Tran82de8022017-06-13 20:47:17 -07003102 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003103 else
Quinn Tran82de8022017-06-13 20:47:17 -07003104 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Quinn Tran810e30b2015-06-10 11:05:20 -04003105 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003106 qpair->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003107 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003108 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003109
3110 pkt = (struct ctio7_to_24xx *)prm.pkt;
3111
3112 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3113 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003114 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003115 CTIO7_FLAGS_STATUS_MODE_0);
3116
Quinn Tranf83adb62014-04-11 16:54:43 -04003117 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003118 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003119
3120 if (prm.add_status_pkt == 0) {
3121 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3122 pkt->u.status0.scsi_status =
3123 cpu_to_le16(prm.rq_result);
3124 pkt->u.status0.residual =
3125 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003126 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003127 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003128 if (qlt_need_explicit_conf(cmd, 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003129 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003130 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003131 CTIO7_FLAGS_EXPLICIT_CONFORM |
3132 CTIO7_FLAGS_CONFORM_REQ);
3133 }
3134 }
3135
3136 } else {
3137 /*
3138 * We have already made sure that there is sufficient
3139 * amount of request entries to not drop HW lock in
3140 * req_pkt().
3141 */
3142 struct ctio7_to_24xx *ctio =
Quinn Tran82de8022017-06-13 20:47:17 -07003143 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3144 qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003145
Quinn Tran22d84722017-06-13 20:47:25 -07003146 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
Arun Easi667024a2014-09-25 06:14:47 -04003147 "Building additional status packet 0x%p.\n",
3148 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003149
Quinn Tranf83adb62014-04-11 16:54:43 -04003150 /*
3151 * T10Dif: ctio_crc2_to_fw overlay ontop of
3152 * ctio7_to_24xx
3153 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003154 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003155 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003156 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003157 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003158 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003159 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003160 CTIO7_FLAGS_DATA_IN);
3161
3162 /* Real finish is ctio_m1's finish */
3163 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003164 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003165 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003166
3167 /* qlt_24xx_init_ctio_to_isp will correct
3168 * all neccessary fields that's part of CTIO7.
3169 * There should be no residual of CTIO-CRC2 data.
3170 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003171 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3172 &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003173 }
3174 } else
3175 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3176
3177
3178 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003179 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003180
Himanshu Madhani63163e02014-09-25 06:14:59 -04003181 /* Memory Barrier */
3182 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003183 if (qpair->reqq_start_iocbs)
3184 qpair->reqq_start_iocbs(qpair);
3185 else
3186 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003187 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003188
3189 return 0;
3190
3191out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003192 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003193 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003194
3195 return res;
3196}
3197EXPORT_SYMBOL(qlt_xmit_response);
3198
3199int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3200{
3201 struct ctio7_to_24xx *pkt;
3202 struct scsi_qla_host *vha = cmd->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003203 struct qla_tgt *tgt = cmd->tgt;
3204 struct qla_tgt_prm prm;
Quinn Tran82de8022017-06-13 20:47:17 -07003205 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003206 int res = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003207 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003208
3209 memset(&prm, 0, sizeof(prm));
3210 prm.cmd = cmd;
3211 prm.tgt = tgt;
3212 prm.sg = NULL;
3213 prm.req_cnt = 1;
3214
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003215 /* Calculate number of entries and segments required */
3216 if (qlt_pci_map_calc_cnt(&prm) != 0)
3217 return -EAGAIN;
3218
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003219 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003220 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003221 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003222 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003223 * previous life, just abort the processing.
3224 */
3225 cmd->state = QLA_TGT_STATE_NEED_DATA;
3226 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
Quinn Tran22d84722017-06-13 20:47:25 -07003227 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003228 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3229 vha->flags.online, qla2x00_reset_active(vha),
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07003230 cmd->reset_count, qpair->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003231 return 0;
3232 }
3233
Quinn Tran82de8022017-06-13 20:47:17 -07003234 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003235 /* Does F/W have an IOCBs for this request */
Quinn Tran82de8022017-06-13 20:47:17 -07003236 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003237 if (res != 0)
3238 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003239 if (cmd->se_cmd.prot_op)
Quinn Tran82de8022017-06-13 20:47:17 -07003240 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
Quinn Tranf83adb62014-04-11 16:54:43 -04003241 else
Quinn Tran82de8022017-06-13 20:47:17 -07003242 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003243
Quinn Tran810e30b2015-06-10 11:05:20 -04003244 if (unlikely(res != 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07003245 qpair->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003246 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003247 }
3248
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003249 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003250 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003251 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003252
3253 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
Quinn Tran32d29b42017-06-13 20:47:26 -07003254 qlt_load_data_segments(&prm);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003255
3256 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003257 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003258
Himanshu Madhani63163e02014-09-25 06:14:59 -04003259 /* Memory Barrier */
3260 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003261 if (qpair->reqq_start_iocbs)
3262 qpair->reqq_start_iocbs(qpair);
3263 else
3264 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran82de8022017-06-13 20:47:17 -07003265 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003266
3267 return res;
3268
3269out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003270 qlt_unmap_sg(vha, cmd);
Quinn Tran82de8022017-06-13 20:47:17 -07003271 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003272
3273 return res;
3274}
3275EXPORT_SYMBOL(qlt_rdy_to_xfer);
3276
Quinn Tranf83adb62014-04-11 16:54:43 -04003277
3278/*
Quinn Tranbe251522017-03-15 09:48:49 -07003279 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003280 */
Quinn Tranbe251522017-03-15 09:48:49 -07003281static void
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003282qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003283 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003284{
3285 uint8_t *ap = &sts->actual_dif[0];
3286 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003287 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003288 uint8_t scsi_status, sense_key, asc, ascq;
3289 unsigned long flags;
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003290 struct scsi_qla_host *vha = cmd->vha;
Quinn Tranf83adb62014-04-11 16:54:43 -04003291
Quinn Tranbe251522017-03-15 09:48:49 -07003292 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003293
Quinn Tranbe251522017-03-15 09:48:49 -07003294 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3295 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3296 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003297
Quinn Tranbe251522017-03-15 09:48:49 -07003298 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3299 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3300 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003301
Quinn Tranbe251522017-03-15 09:48:49 -07003302 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3303 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003304
Quinn Tranbe251522017-03-15 09:48:49 -07003305 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003306
Quinn Tranbe251522017-03-15 09:48:49 -07003307 /* check appl tag */
3308 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003309 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3310 "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]",
3311 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3312 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3313 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3314 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003315
Quinn Tranbe251522017-03-15 09:48:49 -07003316 cmd->dif_err_code = DIF_ERR_APP;
3317 scsi_status = SAM_STAT_CHECK_CONDITION;
3318 sense_key = ABORTED_COMMAND;
3319 asc = 0x10;
3320 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003321 }
3322
3323 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003324 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003325 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3326 "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] ",
3327 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3328 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3329 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3330 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003331
Quinn Tranbe251522017-03-15 09:48:49 -07003332 cmd->dif_err_code = DIF_ERR_REF;
3333 scsi_status = SAM_STAT_CHECK_CONDITION;
3334 sense_key = ABORTED_COMMAND;
3335 asc = 0x10;
3336 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003337 goto out;
3338 }
3339
Quinn Tranbe251522017-03-15 09:48:49 -07003340 /* check guard */
3341 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003342 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3343 "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]",
3344 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3345 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3346 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3347 cmd->atio.u.isp24.fcp_hdr.ox_id);
3348
Quinn Tranbe251522017-03-15 09:48:49 -07003349 cmd->dif_err_code = DIF_ERR_GRD;
3350 scsi_status = SAM_STAT_CHECK_CONDITION;
3351 sense_key = ABORTED_COMMAND;
3352 asc = 0x10;
3353 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003354 }
3355out:
Quinn Tranbe251522017-03-15 09:48:49 -07003356 switch (cmd->state) {
3357 case QLA_TGT_STATE_NEED_DATA:
3358 /* handle_data will load DIF error code */
3359 cmd->state = QLA_TGT_STATE_DATA_IN;
3360 vha->hw->tgt.tgt_ops->handle_data(cmd);
3361 break;
3362 default:
3363 spin_lock_irqsave(&cmd->cmd_lock, flags);
3364 if (cmd->aborted) {
3365 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3366 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3367 break;
3368 }
3369 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003370
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003371 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3372 ascq);
Quinn Tranbe251522017-03-15 09:48:49 -07003373 /* assume scsi status gets out on the wire.
3374 * Will not wait for completion.
3375 */
3376 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3377 break;
3378 }
3379}
Quinn Tranf83adb62014-04-11 16:54:43 -04003380
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003381/* If hardware_lock held on entry, might drop it, then reaquire */
3382/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003383static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3384 struct imm_ntfy_from_isp *ntfy)
3385{
3386 struct nack_to_isp *nack;
3387 struct qla_hw_data *ha = vha->hw;
3388 request_t *pkt;
3389 int ret = 0;
3390
3391 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3392 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3393
Quinn Tranec7193e2017-03-15 09:48:55 -07003394 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003395 if (pkt == NULL) {
3396 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3397 "qla_target(%d): %s failed: unable to allocate "
3398 "request packet\n", vha->vp_idx, __func__);
3399 return -ENOMEM;
3400 }
3401
3402 pkt->entry_type = NOTIFY_ACK_TYPE;
3403 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003404 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003405
3406 nack = (struct nack_to_isp *)pkt;
3407 nack->ox_id = ntfy->ox_id;
3408
3409 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3410 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3411 nack->u.isp24.flags = ntfy->u.isp24.flags &
3412 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3413 }
3414
3415 /* terminate */
3416 nack->u.isp24.flags |=
3417 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3418
3419 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3420 nack->u.isp24.status = ntfy->u.isp24.status;
3421 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3422 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3423 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3424 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3425 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3426 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3427
3428 qla2x00_start_iocbs(vha, vha->req);
3429 return ret;
3430}
3431
3432static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3433 struct imm_ntfy_from_isp *imm, int ha_locked)
3434{
3435 unsigned long flags = 0;
3436 int rc;
3437
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003438 if (ha_locked) {
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);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003444#else
3445 if (rc) {
3446 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003447#endif
3448 goto done;
3449 }
3450
3451 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3452 rc = __qlt_send_term_imm_notif(vha, imm);
3453
3454#if 0 /* Todo */
3455 if (rc == -ENOMEM)
3456 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3457#endif
3458
3459done:
3460 if (!ha_locked)
3461 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3462}
3463
Quinn Tran83548fe2017-06-02 09:12:01 -07003464/*
3465 * If hardware_lock held on entry, might drop it, then reaquire
3466 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3467 */
Quinn Tran82de8022017-06-13 20:47:17 -07003468static int __qlt_send_term_exchange(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003469 struct qla_tgt_cmd *cmd,
3470 struct atio_from_isp *atio)
3471{
Quinn Tran82de8022017-06-13 20:47:17 -07003472 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003473 struct ctio7_to_24xx *ctio24;
3474 struct qla_hw_data *ha = vha->hw;
3475 request_t *pkt;
3476 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003477 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003478
Quinn Tran83548fe2017-06-02 09:12:01 -07003479 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003480
Quinn Tran72fcd4e2017-08-23 15:05:13 -07003481 if (cmd)
3482 vha = cmd->vha;
3483
Quinn Tran82de8022017-06-13 20:47:17 -07003484 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003485 if (pkt == NULL) {
3486 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3487 "qla_target(%d): %s failed: unable to allocate "
3488 "request packet\n", vha->vp_idx, __func__);
3489 return -ENOMEM;
3490 }
3491
3492 if (cmd != NULL) {
3493 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3494 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3495 "qla_target(%d): Terminating cmd %p with "
3496 "incorrect state %d\n", vha->vp_idx, cmd,
3497 cmd->state);
3498 } else
3499 ret = 1;
3500 }
3501
Quinn Tran60a9ead2017-06-13 20:47:28 -07003502 qpair->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003503 pkt->entry_count = 1;
3504 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3505
3506 ctio24 = (struct ctio7_to_24xx *)pkt;
3507 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003508 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003509 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003510 ctio24->vp_index = vha->vp_idx;
3511 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3512 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3513 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3514 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003515 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3516 CTIO7_FLAGS_TERMINATE;
3517 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003518 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3519 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003520
3521 /* Most likely, it isn't needed */
3522 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3523 &atio->u.isp24.fcp_cmnd.add_cdb[
3524 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3525 if (ctio24->u.status1.residual != 0)
3526 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3527
Himanshu Madhani63163e02014-09-25 06:14:59 -04003528 /* Memory Barrier */
3529 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003530 if (qpair->reqq_start_iocbs)
3531 qpair->reqq_start_iocbs(qpair);
3532 else
3533 qla2x00_start_iocbs(vha, qpair->req);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003534 return ret;
3535}
3536
Quinn Tran82de8022017-06-13 20:47:17 -07003537static void qlt_send_term_exchange(struct qla_qpair *qpair,
Quinn Trana07100e2015-12-07 19:48:57 -05003538 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3539 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003540{
Quinn Tran82de8022017-06-13 20:47:17 -07003541 struct scsi_qla_host *vha;
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003542 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003543 int rc;
3544
Quinn Tran82de8022017-06-13 20:47:17 -07003545 /* why use different vha? NPIV */
3546 if (cmd)
3547 vha = cmd->vha;
3548 else
3549 vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003550
3551 if (ha_locked) {
Quinn Tran82de8022017-06-13 20:47:17 -07003552 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003553 if (rc == -ENOMEM)
3554 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003555 goto done;
3556 }
Quinn Tran82de8022017-06-13 20:47:17 -07003557 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3558 rc = __qlt_send_term_exchange(qpair, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003559 if (rc == -ENOMEM)
3560 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003561
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003562done:
Quinn Trana07100e2015-12-07 19:48:57 -05003563 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003564 if (cmd->sg_mapped)
3565 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003566 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3567 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003568
3569 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07003570 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003571
Quinn Tran7b898542014-04-11 16:54:44 -04003572 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003573}
3574
Quinn Tran33e79972014-09-25 06:14:55 -04003575static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3576{
3577 struct list_head free_list;
3578 struct qla_tgt_cmd *cmd, *tcmd;
3579
3580 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003581 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003582
3583 cmd = tcmd = NULL;
3584 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3585 INIT_LIST_HEAD(&free_list);
3586 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3587
3588 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3589 list_del(&cmd->cmd_list);
3590 /* This cmd was never sent to TCM. There is no need
3591 * to schedule free or call free_cmd
3592 */
3593 qlt_free_cmd(cmd);
3594 vha->hw->tgt.num_qfull_cmds_alloc--;
3595 }
3596 }
3597 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3598}
3599
3600static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3601{
3602 uint32_t total_leaked;
3603
3604 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3605
3606 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3607 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3608
3609 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3610 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003611 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003612
3613 if (IS_P3P_TYPE(vha->hw))
3614 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3615 else
3616 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3617 qla2xxx_wake_dpc(vha);
3618 }
3619
3620}
3621
Quinn Trana07100e2015-12-07 19:48:57 -05003622int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003623{
3624 struct qla_tgt *tgt = cmd->tgt;
3625 struct scsi_qla_host *vha = tgt->vha;
3626 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003627 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003628
3629 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3630 "qla_target(%d): terminating exchange for aborted cmd=%p "
3631 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3632 se_cmd->tag);
3633
Quinn Trana07100e2015-12-07 19:48:57 -05003634 spin_lock_irqsave(&cmd->cmd_lock, flags);
3635 if (cmd->aborted) {
3636 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3637 /*
3638 * It's normal to see 2 calls in this path:
3639 * 1) XFER Rdy completion + CMD_T_ABORT
3640 * 2) TCM TMR - drain_state_list
3641 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003642 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3643 "multiple abort. %p transport_state %x, t_state %x, "
3644 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3645 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003646 return EIO;
3647 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003648 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003649 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003650 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003651
Quinn Tran82de8022017-06-13 20:47:17 -07003652 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
Quinn Trana07100e2015-12-07 19:48:57 -05003653 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003654}
3655EXPORT_SYMBOL(qlt_abort_cmd);
3656
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003657void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3658{
Quinn Tran5d964832017-01-19 22:27:59 -08003659 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003660
Quinn Tranf83adb62014-04-11 16:54:43 -04003661 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3662 "%s: se_cmd[%p] ox_id %04x\n",
3663 __func__, &cmd->se_cmd,
3664 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003665
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003666 BUG_ON(cmd->cmd_in_wq);
3667
Quinn Trana07100e2015-12-07 19:48:57 -05003668 if (cmd->sg_mapped)
3669 qlt_unmap_sg(cmd->vha, cmd);
3670
Quinn Tran33e79972014-09-25 06:14:55 -04003671 if (!cmd->q_full)
3672 qlt_decr_num_pend_cmds(cmd->vha);
3673
Quinn Tranf83adb62014-04-11 16:54:43 -04003674 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003675 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003676 if (unlikely(cmd->free_sg))
3677 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003678
3679 if (!sess || !sess->se_sess) {
3680 WARN_ON(1);
3681 return;
3682 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003683 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003684 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003685}
3686EXPORT_SYMBOL(qlt_free_cmd);
3687
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003688/*
3689 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3690 */
Quinn Tran82de8022017-06-13 20:47:17 -07003691static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003692 struct qla_tgt_cmd *cmd, uint32_t status)
3693{
3694 int term = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003695 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003696
Quinn Tranbe251522017-03-15 09:48:49 -07003697 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003698 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003699 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3700 "se_cmd=%p tag[%x] op %#x/%s",
3701 cmd->lba, cmd->lba,
3702 cmd->num_blks, &cmd->se_cmd,
3703 cmd->atio.u.isp24.exchange_addr,
3704 cmd->se_cmd.prot_op,
3705 prot_op_str(cmd->se_cmd.prot_op));
3706
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003707 if (ctio != NULL) {
3708 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3709 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003710 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003711 } else
3712 term = 1;
3713
3714 if (term)
Quinn Tran82de8022017-06-13 20:47:17 -07003715 qlt_term_ctio_exchange(qpair, ctio, cmd, status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003716
3717 return term;
3718}
3719
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003720
3721/* ha->hardware_lock supposed to be held on entry */
3722static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
Quinn Tran82de8022017-06-13 20:47:17 -07003723 struct rsp_que *rsp, uint32_t handle, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003724{
3725 struct qla_tgt_cmd *cmd = NULL;
Quinn Tran82de8022017-06-13 20:47:17 -07003726 struct req_que *req;
3727 int qid = GET_QID(handle);
3728 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003729
Quinn Tran82de8022017-06-13 20:47:17 -07003730 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3731 return NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003732
Quinn Tran82de8022017-06-13 20:47:17 -07003733 if (qid == rsp->req->id) {
3734 req = rsp->req;
3735 } else if (vha->hw->req_q_map[qid]) {
3736 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3737 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3738 vha->vp_idx, rsp->id, handle);
3739 req = vha->hw->req_q_map[qid];
3740 } else {
3741 return NULL;
3742 }
Arun Easi667024a2014-09-25 06:14:47 -04003743
Quinn Tran82de8022017-06-13 20:47:17 -07003744 h &= QLA_CMD_HANDLE_MASK;
Quinn Tranc5419e22017-06-13 20:47:16 -07003745
Quinn Tran82de8022017-06-13 20:47:17 -07003746 if (h != QLA_TGT_NULL_HANDLE) {
Dan Carpenterfb2028a2017-07-10 11:47:40 +03003747 if (unlikely(h >= req->num_outstanding_cmds)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003748 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3749 "qla_target(%d): Wrong handle %x received\n",
3750 vha->vp_idx, handle);
3751 return NULL;
3752 }
Quinn Tran82de8022017-06-13 20:47:17 -07003753
3754 cmd = (struct qla_tgt_cmd *)req->outstanding_cmds[h];
3755 if (unlikely(cmd == NULL)) {
Quinn Tranc5419e22017-06-13 20:47:16 -07003756 ql_dbg(ql_dbg_async, vha, 0xe053,
Quinn Tran82de8022017-06-13 20:47:17 -07003757 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3758 vha->vp_idx, handle, req->id, rsp->id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003759 return NULL;
3760 }
Quinn Tran82de8022017-06-13 20:47:17 -07003761 req->outstanding_cmds[h] = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003762 } else if (ctio != NULL) {
3763 /* We can't get loop ID from CTIO7 */
3764 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3765 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3766 "support NULL handles\n", vha->vp_idx);
3767 return NULL;
3768 }
3769
3770 return cmd;
3771}
3772
Arun Easic0cb4492014-09-25 06:14:51 -04003773/* hardware_lock should be held by caller. */
Quinn Tranc5419e22017-06-13 20:47:16 -07003774void
Arun Easic0cb4492014-09-25 06:14:51 -04003775qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3776{
3777 struct qla_hw_data *ha = vha->hw;
Arun Easic0cb4492014-09-25 06:14:51 -04003778
3779 if (cmd->sg_mapped)
3780 qlt_unmap_sg(vha, cmd);
3781
Arun Easic0cb4492014-09-25 06:14:51 -04003782 /* TODO: fix debug message type and ids. */
3783 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3784 ql_dbg(ql_dbg_io, vha, 0xff00,
Quinn Tran82de8022017-06-13 20:47:17 -07003785 "HOST-ABORT: state=PROCESSED.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003786 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3787 cmd->write_data_transferred = 0;
3788 cmd->state = QLA_TGT_STATE_DATA_IN;
3789
3790 ql_dbg(ql_dbg_io, vha, 0xff01,
Quinn Tran82de8022017-06-13 20:47:17 -07003791 "HOST-ABORT: state=DATA_IN.\n");
Arun Easic0cb4492014-09-25 06:14:51 -04003792
3793 ha->tgt.tgt_ops->handle_data(cmd);
3794 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003795 } else {
3796 ql_dbg(ql_dbg_io, vha, 0xff03,
Quinn Tran82de8022017-06-13 20:47:17 -07003797 "HOST-ABORT: state=BAD(%d).\n",
Arun Easic0cb4492014-09-25 06:14:51 -04003798 cmd->state);
3799 dump_stack();
3800 }
3801
Quinn Tran1eb42f92017-01-19 22:27:55 -08003802 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003803 ha->tgt.tgt_ops->free_cmd(cmd);
3804}
3805
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003806/*
3807 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3808 */
Quinn Tran82de8022017-06-13 20:47:17 -07003809static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3810 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003811{
3812 struct qla_hw_data *ha = vha->hw;
3813 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003814 struct qla_tgt_cmd *cmd;
Quinn Tran82de8022017-06-13 20:47:17 -07003815 struct qla_qpair *qpair = rsp->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003816
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003817 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3818 /* That could happen only in case of an error/reset/abort */
3819 if (status != CTIO_SUCCESS) {
3820 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3821 "Intermediate CTIO received"
3822 " (status %x)\n", status);
3823 }
3824 return;
3825 }
3826
Quinn Tran82de8022017-06-13 20:47:17 -07003827 cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003828 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003829 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003830
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003831 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003832 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003833
Joern Engelf9b67212014-09-16 16:23:18 -04003834 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003835
3836 if (unlikely(status != CTIO_SUCCESS)) {
3837 switch (status & 0xFFFF) {
3838 case CTIO_LIP_RESET:
3839 case CTIO_TARGET_RESET:
3840 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003841 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003842 case CTIO_TIMEOUT:
3843 case CTIO_INVALID_RX_ID:
3844 /* They are OK */
3845 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3846 "qla_target(%d): CTIO with "
3847 "status %#x received, state %x, se_cmd %p, "
3848 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3849 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3850 status, cmd->state, se_cmd);
3851 break;
3852
3853 case CTIO_PORT_LOGGED_OUT:
3854 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003855 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003856 int logged_out =
3857 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3858
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003859 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003860 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003861 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003862 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003863 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003864
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003865 if (logged_out && cmd->sess) {
3866 /*
3867 * Session is already logged out, but we need
3868 * to notify initiator, who's not aware of this
3869 */
3870 cmd->sess->logout_on_delete = 0;
3871 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003872 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003873 "%s %d %8phC post del sess\n",
3874 __func__, __LINE__, cmd->sess->port_name);
3875
3876 qlt_schedule_sess_for_deletion_lock(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003877 }
3878 break;
3879 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003880 case CTIO_DIF_ERROR: {
3881 struct ctio_crc_from_fw *crc =
3882 (struct ctio_crc_from_fw *)ctio;
3883 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003884 "qla_target(%d): CTIO with DIF_ERROR status %x "
3885 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3886 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003887 vha->vp_idx, status, cmd->state, se_cmd,
3888 *((u64 *)&crc->actual_dif[0]),
3889 *((u64 *)&crc->expected_dif[0]));
3890
Quinn Tran8abfa9e2017-06-13 20:47:24 -07003891 qlt_handle_dif_error(qpair, cmd, ctio);
Quinn Tranbe251522017-03-15 09:48:49 -07003892 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003893 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003894 default:
3895 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003896 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003897 vha->vp_idx, status, cmd->state, se_cmd);
3898 break;
3899 }
3900
Quinn Tran7b898542014-04-11 16:54:44 -04003901
Quinn Tran193b50b2015-12-17 14:57:03 -05003902 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003903 * cmd is already aborted/terminated, we don't
3904 * need to terminate again. The exchange is already
3905 * cleaned up/freed at FW level. Just cleanup at driver
3906 * level.
3907 */
3908 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003909 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003910 cmd->trc_flags |= TRC_CTIO_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07003911 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003912 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003913 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003914 }
3915
3916 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003917 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003918 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003919 cmd->state = QLA_TGT_STATE_DATA_IN;
3920
Bart Van Assche52c82822015-07-09 07:23:26 -07003921 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003922 cmd->write_data_transferred = 1;
3923
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003924 ha->tgt.tgt_ops->handle_data(cmd);
3925 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003926 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003927 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003928 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003929 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003930 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003931 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003932 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3933 "qla_target(%d): A command in state (%d) should "
3934 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3935 }
3936
Quinn Tran7b898542014-04-11 16:54:44 -04003937 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003938 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003939 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3940 dump_stack();
3941 }
3942
3943 ha->tgt.tgt_ops->free_cmd(cmd);
3944}
3945
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003946static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3947 uint8_t task_codes)
3948{
3949 int fcp_task_attr;
3950
3951 switch (task_codes) {
3952 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003953 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003954 break;
3955 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003956 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003957 break;
3958 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003959 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003960 break;
3961 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003962 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003963 break;
3964 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003965 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003966 break;
3967 default:
3968 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3969 "qla_target: unknown task code %x, use ORDERED instead\n",
3970 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003971 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003972 break;
3973 }
3974
3975 return fcp_task_attr;
3976}
3977
Quinn Tran5d964832017-01-19 22:27:59 -08003978static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003979 uint8_t *);
3980/*
3981 * Process context for I/O path into tcm_qla2xxx code
3982 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003983static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003984{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003985 scsi_qla_host_t *vha = cmd->vha;
3986 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08003987 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003988 struct atio_from_isp *atio = &cmd->atio;
3989 unsigned char *cdb;
3990 unsigned long flags;
3991 uint32_t data_length;
3992 int ret, fcp_task_attr, data_dir, bidi = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07003993 struct qla_qpair *qpair = cmd->qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003994
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003995 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003996 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003997
Quinn Tran193b50b2015-12-17 14:57:03 -05003998 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4000 "cmd with tag %u is aborted\n",
4001 cmd->atio.u.isp24.exchange_addr);
4002 goto out_term;
4003 }
4004
Quinn Trana07100e2015-12-07 19:48:57 -05004005 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004006 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004007 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004008
4009 if (atio->u.isp24.fcp_cmnd.rddata &&
4010 atio->u.isp24.fcp_cmnd.wrdata) {
4011 bidi = 1;
4012 data_dir = DMA_TO_DEVICE;
4013 } else if (atio->u.isp24.fcp_cmnd.rddata)
4014 data_dir = DMA_FROM_DEVICE;
4015 else if (atio->u.isp24.fcp_cmnd.wrdata)
4016 data_dir = DMA_TO_DEVICE;
4017 else
4018 data_dir = DMA_NONE;
4019
4020 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4021 atio->u.isp24.fcp_cmnd.task_attr);
4022 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4023 &atio->u.isp24.fcp_cmnd.add_cdb[
4024 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4025
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004026 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4027 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004028 if (ret != 0)
4029 goto out_term;
4030 /*
4031 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4032 */
Quinn Tran75601512015-12-17 14:57:04 -05004033 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004034 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004035 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004036 return;
4037
4038out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004039 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004040 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004041 * cmd has not sent to target yet, so pass NULL as the second
4042 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004043 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004044 cmd->trc_flags |= TRC_DO_WORK_ERR;
Quinn Tran82de8022017-06-13 20:47:17 -07004045 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4046 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004047
4048 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004049 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Quinn Tran82de8022017-06-13 20:47:17 -07004050 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004051
4052 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004053 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004054 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004055}
4056
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004057static void qlt_do_work(struct work_struct *work)
4058{
4059 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004060 scsi_qla_host_t *vha = cmd->vha;
4061 unsigned long flags;
4062
4063 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4064 list_del(&cmd->cmd_list);
4065 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004066
4067 __qlt_do_work(cmd);
4068}
4069
Quinn Tran09620ee2017-06-13 20:47:20 -07004070void qlt_clr_qp_table(struct scsi_qla_host *vha)
4071{
4072 unsigned long flags;
4073 struct qla_hw_data *ha = vha->hw;
4074 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4075 void *node;
4076 u64 key = 0;
4077
4078 ql_log(ql_log_info, vha, 0x706c,
4079 "User update Number of Active Qpairs %d\n",
4080 ha->tgt.num_act_qpairs);
4081
4082 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4083
4084 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4085 btree_remove64(&tgt->lun_qpair_map, key);
4086
4087 ha->base_qpair->lun_cnt = 0;
4088 for (key = 0; key < ha->max_qpairs; key++)
4089 if (ha->queue_pair_map[key])
4090 ha->queue_pair_map[key]->lun_cnt = 0;
4091
4092 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4093}
4094
Quinn Trane326d222017-06-13 20:47:18 -07004095static void qlt_assign_qpair(struct scsi_qla_host *vha,
4096 struct qla_tgt_cmd *cmd)
4097{
4098 struct qla_qpair *qpair, *qp;
4099 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4100 struct qla_qpair_hint *h;
4101
4102 if (vha->flags.qpairs_available) {
4103 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4104 if (unlikely(!h)) {
4105 /* spread lun to qpair ratio evently */
4106 int lcnt = 0, rc;
4107 struct scsi_qla_host *base_vha =
4108 pci_get_drvdata(vha->hw->pdev);
4109
4110 qpair = vha->hw->base_qpair;
4111 if (qpair->lun_cnt == 0) {
4112 qpair->lun_cnt++;
4113 h = qla_qpair_to_hint(tgt, qpair);
4114 BUG_ON(!h);
4115 rc = btree_insert64(&tgt->lun_qpair_map,
4116 cmd->unpacked_lun, h, GFP_ATOMIC);
4117 if (rc) {
4118 qpair->lun_cnt--;
4119 ql_log(ql_log_info, vha, 0xd037,
4120 "Unable to insert lun %llx into lun_qpair_map\n",
4121 cmd->unpacked_lun);
4122 }
4123 goto out;
4124 } else {
4125 lcnt = qpair->lun_cnt;
4126 }
4127
4128 h = NULL;
4129 list_for_each_entry(qp, &base_vha->qp_list,
4130 qp_list_elem) {
4131 if (qp->lun_cnt == 0) {
4132 qp->lun_cnt++;
4133 h = qla_qpair_to_hint(tgt, qp);
4134 BUG_ON(!h);
4135 rc = btree_insert64(&tgt->lun_qpair_map,
4136 cmd->unpacked_lun, h, GFP_ATOMIC);
4137 if (rc) {
4138 qp->lun_cnt--;
4139 ql_log(ql_log_info, vha, 0xd038,
4140 "Unable to insert lun %llx into lun_qpair_map\n",
4141 cmd->unpacked_lun);
4142 }
4143 qpair = qp;
4144 goto out;
4145 } else {
4146 if (qp->lun_cnt < lcnt) {
4147 lcnt = qp->lun_cnt;
4148 qpair = qp;
4149 continue;
4150 }
4151 }
4152 }
4153 BUG_ON(!qpair);
4154 qpair->lun_cnt++;
4155 h = qla_qpair_to_hint(tgt, qpair);
4156 BUG_ON(!h);
4157 rc = btree_insert64(&tgt->lun_qpair_map,
4158 cmd->unpacked_lun, h, GFP_ATOMIC);
4159 if (rc) {
4160 qpair->lun_cnt--;
4161 ql_log(ql_log_info, vha, 0xd039,
4162 "Unable to insert lun %llx into lun_qpair_map\n",
4163 cmd->unpacked_lun);
4164 }
4165 }
4166 } else {
4167 h = &tgt->qphints[0];
4168 }
4169out:
4170 cmd->qpair = h->qpair;
4171 cmd->se_cmd.cpuid = h->cpuid;
4172}
4173
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004174static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004175 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004176 struct atio_from_isp *atio)
4177{
4178 struct se_session *se_sess = sess->se_sess;
4179 struct qla_tgt_cmd *cmd;
4180 int tag;
4181
4182 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4183 if (tag < 0)
4184 return NULL;
4185
4186 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4187 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
Quinn Tranc5419e22017-06-13 20:47:16 -07004188 cmd->cmd_type = TYPE_TGT_CMD;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004189 memcpy(&cmd->atio, atio, sizeof(*atio));
4190 cmd->state = QLA_TGT_STATE_NEW;
4191 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004192 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004193 cmd->vha = vha;
4194 cmd->se_cmd.map_tag = tag;
4195 cmd->sess = sess;
4196 cmd->loop_id = sess->loop_id;
4197 cmd->conf_compl_supported = sess->conf_compl_supported;
4198
Quinn Tran1eb42f92017-01-19 22:27:55 -08004199 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004200 cmd->jiffies_at_alloc = get_jiffies_64();
4201
Quinn Trane326d222017-06-13 20:47:18 -07004202 cmd->unpacked_lun = scsilun_to_int(
4203 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4204 qlt_assign_qpair(vha, cmd);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004205 cmd->reset_count = vha->hw->base_qpair->chip_reset;
Quinn Tran22d84722017-06-13 20:47:25 -07004206 cmd->vp_idx = vha->vp_idx;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004207
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004208 return cmd;
4209}
4210
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004211static void qlt_create_sess_from_atio(struct work_struct *work)
4212{
4213 struct qla_tgt_sess_op *op = container_of(work,
4214 struct qla_tgt_sess_op, work);
4215 scsi_qla_host_t *vha = op->vha;
4216 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004217 struct fc_port *sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004218 struct qla_tgt_cmd *cmd;
4219 unsigned long flags;
4220 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4221
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004222 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4223 list_del(&op->cmd_list);
4224 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4225
4226 if (op->aborted) {
4227 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4228 "sess_op with tag %u is aborted\n",
4229 op->atio.u.isp24.exchange_addr);
4230 goto out_term;
4231 }
4232
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004233 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004234 "qla_target(%d): Unable to find wwn login"
4235 " (s_id %x:%x:%x), trying to create it manually\n",
4236 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004237
4238 if (op->atio.u.raw.entry_count > 1) {
4239 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004240 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004241 goto out_term;
4242 }
4243
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004244 sess = qlt_make_local_sess(vha, s_id);
4245 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004246
4247 if (!sess)
4248 goto out_term;
4249 /*
4250 * Now obtain a pre-allocated session tag using the original op->atio
4251 * packet header, and dispatch into __qlt_do_work() using the existing
4252 * process context.
4253 */
4254 cmd = qlt_get_tag(vha, sess, &op->atio);
4255 if (!cmd) {
Quinn Tran82de8022017-06-13 20:47:17 -07004256 struct qla_qpair *qpair = ha->base_qpair;
4257
4258 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4259 qlt_send_busy(qpair, &op->atio, SAM_STAT_BUSY);
4260 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4261
4262 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004263 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran82de8022017-06-13 20:47:17 -07004264 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004265 kfree(op);
4266 return;
4267 }
Quinn Tran726b8542017-01-19 22:28:00 -08004268
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004269 /*
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004270 * __qlt_do_work() will call qlt_put_sess() to release
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004271 * the extra reference taken above by qlt_make_local_sess()
4272 */
4273 __qlt_do_work(cmd);
4274 kfree(op);
4275 return;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004276out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07004277 qlt_send_term_exchange(vha->hw->base_qpair, NULL, &op->atio, 0, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004278 kfree(op);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004279}
4280
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004281/* ha->hardware_lock supposed to be held on entry */
4282static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4283 struct atio_from_isp *atio)
4284{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004285 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004286 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004287 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004288 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004289 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004290
4291 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004292 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004293 "New command while device %p is shutting down\n", tgt);
4294 return -EFAULT;
4295 }
4296
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004297 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4298 if (unlikely(!sess)) {
4299 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4300 GFP_ATOMIC);
4301 if (!op)
4302 return -ENOMEM;
4303
4304 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04004305 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004306
Quinn Tran8b631d82017-06-02 09:11:54 -07004307 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004308 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
Quinn Tran8b631d82017-06-02 09:11:54 -07004309 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004310
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004311 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4312 queue_work(qla_tgt_wq, &op->work);
4313 return 0;
4314 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004315
4316 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4317 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004318 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004319 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004320 "New command while old session %p is being deleted\n",
4321 sess);
4322 return -EFAULT;
4323 }
4324
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004325 /*
4326 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4327 */
Quinn Tran726b8542017-01-19 22:28:00 -08004328 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004329 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004330 "%s: kref_get fail, %8phC oxid %x \n",
4331 __func__, sess->port_name,
4332 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4333 return -EFAULT;
4334 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004335
4336 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004337 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004338 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004339 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004340 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4341 ha->tgt.tgt_ops->put_sess(sess);
4342 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004343 return -ENOMEM;
4344 }
4345
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004346 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004347 cmd->trc_flags |= TRC_NEW_CMD;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004348
Quinn Tran726b8542017-01-19 22:28:00 -08004349 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004350 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004351 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004352
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004353 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Trane326d222017-06-13 20:47:18 -07004354 if (vha->flags.qpairs_available) {
4355 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4356 } else if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004357 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4358 queue_work_on(smp_processor_id(), qla_tgt_wq,
4359 &cmd->work);
4360 else
4361 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4362 &cmd->work);
4363 } else {
4364 queue_work(qla_tgt_wq, &cmd->work);
4365 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004366
Quinn Tran82de8022017-06-13 20:47:17 -07004367 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004368}
4369
4370/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004371static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004372 int fn, void *iocb, int flags)
4373{
4374 struct scsi_qla_host *vha = sess->vha;
4375 struct qla_hw_data *ha = vha->hw;
4376 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004377 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004378 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004379
4380 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4381 if (!mcmd) {
4382 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4383 "qla_target(%d): Allocation of management "
4384 "command failed, some commands and their data could "
4385 "leak\n", vha->vp_idx);
4386 return -ENOMEM;
4387 }
4388 memset(mcmd, 0, sizeof(*mcmd));
4389 mcmd->sess = sess;
4390
4391 if (iocb) {
4392 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4393 sizeof(mcmd->orig_iocb.imm_ntfy));
4394 }
4395 mcmd->tmr_func = fn;
4396 mcmd->flags = flags;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004397 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07004398 mcmd->qpair = ha->base_qpair;
Quinn Tran72fcd4e2017-08-23 15:05:13 -07004399 mcmd->vha = vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004400
4401 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004402 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004403 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4404 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004405 }
4406
Quinn Tranbe92fc32017-01-19 22:27:54 -08004407 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004408 if (res != 0) {
4409 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4410 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4411 sess->vha->vp_idx, res);
4412 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4413 return -EFAULT;
4414 }
4415
4416 return 0;
4417}
4418
4419/* ha->hardware_lock supposed to be held on entry */
4420static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4421{
4422 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4423 struct qla_hw_data *ha = vha->hw;
4424 struct qla_tgt *tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004425 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004426 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004427 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004428 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004429
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004430 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004431
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004432 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004433
4434 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004435 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4436 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004437 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4438
Quinn Tranf775bd12017-06-02 09:11:59 -07004439 unpacked_lun =
4440 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004441
4442 if (!sess) {
4443 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4444 "qla_target(%d): task mgmt fn 0x%x for "
4445 "non-existant session\n", vha->vp_idx, fn);
4446 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4447 sizeof(struct atio_from_isp));
4448 }
4449
Quinn Tran726b8542017-01-19 22:28:00 -08004450 if (sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004451 return -EFAULT;
4452
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004453 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4454}
4455
4456/* ha->hardware_lock supposed to be held on entry */
4457static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004458 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004459{
4460 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4461 struct qla_hw_data *ha = vha->hw;
4462 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004463 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004464 int rc;
4465
4466 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4467 if (mcmd == NULL) {
4468 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4469 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4470 vha->vp_idx, __func__);
4471 return -ENOMEM;
4472 }
4473 memset(mcmd, 0, sizeof(*mcmd));
4474
4475 mcmd->sess = sess;
4476 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4477 sizeof(mcmd->orig_iocb.imm_ntfy));
4478
Quinn Tranf775bd12017-06-02 09:11:59 -07004479 unpacked_lun =
4480 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07004481 mcmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004482 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Quinn Tran82de8022017-06-13 20:47:17 -07004483 mcmd->qpair = ha->base_qpair;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004484
Quinn Tranbe92fc32017-01-19 22:27:54 -08004485 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004486 le16_to_cpu(iocb->u.isp2x.seq_id));
4487 if (rc != 0) {
4488 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4489 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4490 vha->vp_idx, rc);
4491 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4492 return -EFAULT;
4493 }
4494
4495 return 0;
4496}
4497
4498/* ha->hardware_lock supposed to be held on entry */
4499static int qlt_abort_task(struct scsi_qla_host *vha,
4500 struct imm_ntfy_from_isp *iocb)
4501{
4502 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004503 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004504 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004505 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004506
4507 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4508
Quinn Tran75601512015-12-17 14:57:04 -05004509 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004510 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004511 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4512
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004513 if (sess == NULL) {
4514 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4515 "qla_target(%d): task abort for unexisting "
4516 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004517 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004518 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4519 }
4520
4521 return __qlt_abort_task(vha, iocb, sess);
4522}
4523
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004524void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4525{
Quinn Tran726b8542017-01-19 22:28:00 -08004526 if (rc != MBS_COMMAND_COMPLETE) {
4527 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4528 "%s: se_sess %p / sess %p from"
4529 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4530 " LOGO failed: %#x\n",
4531 __func__,
4532 fcport->se_sess,
4533 fcport,
4534 fcport->port_name, fcport->loop_id,
4535 fcport->d_id.b.domain, fcport->d_id.b.area,
4536 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004537 }
Quinn Tran726b8542017-01-19 22:28:00 -08004538
4539 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004540}
4541
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004542/*
4543* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4544*
4545* Schedules sessions with matching port_id/loop_id but different wwn for
4546* deletion. Returns existing session with matching wwn if present.
4547* Null otherwise.
4548*/
Quinn Tran726b8542017-01-19 22:28:00 -08004549struct fc_port *
4550qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004551 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004552{
Quinn Tran5d964832017-01-19 22:27:59 -08004553 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004554 uint64_t other_wwn;
4555
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004556 *conflict_sess = NULL;
4557
Quinn Tran5d964832017-01-19 22:27:59 -08004558 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004559
4560 other_wwn = wwn_to_u64(other_sess->port_name);
4561
4562 if (wwn == other_wwn) {
4563 WARN_ON(sess);
4564 sess = other_sess;
4565 continue;
4566 }
4567
4568 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004569 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004570 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004571 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004572 "Invalidating sess %p loop_id %d wwn %llx.\n",
4573 other_sess, other_sess->loop_id, other_wwn);
4574
4575 /*
4576 * logout_on_delete is set by default, but another
4577 * session that has the same s_id/loop_id combo
4578 * might have cleared it when requested this session
4579 * deletion, so don't touch it
4580 */
4581 qlt_schedule_sess_for_deletion(other_sess, true);
4582 } else {
4583 /*
4584 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004585 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004586 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004587 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004588 "Invalidating sess %p loop_id %d wwn %llx.\n",
4589 other_sess, other_sess->loop_id, other_wwn);
4590
4591
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004592 other_sess->keep_nport_handle = 1;
4593 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004594 qlt_schedule_sess_for_deletion(other_sess,
4595 true);
4596 }
4597 continue;
4598 }
4599
4600 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004601 if ((loop_id == other_sess->loop_id) &&
4602 (loop_id != FC_NO_LOOP_ID)) {
4603 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004604 "Invalidating sess %p loop_id %d wwn %llx.\n",
4605 other_sess, other_sess->loop_id, other_wwn);
4606
4607 /* Same loop_id but different s_id
4608 * Ok to kill and logout */
4609 qlt_schedule_sess_for_deletion(other_sess, true);
4610 }
4611 }
4612
4613 return sess;
4614}
4615
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004616/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4617static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4618{
4619 struct qla_tgt_sess_op *op;
4620 struct qla_tgt_cmd *cmd;
4621 uint32_t key;
4622 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004623 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004624
4625 key = (((u32)s_id->b.domain << 16) |
4626 ((u32)s_id->b.area << 8) |
4627 ((u32)s_id->b.al_pa));
4628
Quinn Tran8b631d82017-06-02 09:11:54 -07004629 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004630 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4631 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004632
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004633 if (op_key == key) {
4634 op->aborted = true;
4635 count++;
4636 }
4637 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004638
4639 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4640 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4641 if (op_key == key) {
4642 op->aborted = true;
4643 count++;
4644 }
4645 }
4646
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004647 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4648 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4649 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004650 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004651 count++;
4652 }
4653 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004654 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004655
4656 return count;
4657}
4658
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004659/*
4660 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4661 */
4662static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4663 struct imm_ntfy_from_isp *iocb)
4664{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004665 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004666 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004667 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004668 uint64_t wwn;
4669 port_id_t port_id;
4670 uint16_t loop_id;
4671 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004672 int res = 0;
Quinn Tran5d964832017-01-19 22:27:59 -08004673 struct qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004674 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004675
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004676 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4677
4678 port_id.b.domain = iocb->u.isp24.port_id[2];
4679 port_id.b.area = iocb->u.isp24.port_id[1];
4680 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4681 port_id.b.rsvd_1 = 0;
4682
4683 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4684
Quinn Tran726b8542017-01-19 22:28:00 -08004685 ql_dbg(ql_dbg_disc, vha, 0xf026,
4686 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4687 vha->vp_idx, iocb->u.isp24.port_id[2],
4688 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4689 iocb->u.isp24.status_subcode, loop_id,
4690 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004691
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004692 /* res = 1 means ack at the end of thread
4693 * res = 0 means ack async/later.
4694 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004695 switch (iocb->u.isp24.status_subcode) {
4696 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004697
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004698 /* Mark all stale commands in qla_tgt_wq for deletion */
4699 abort_cmds_for_s_id(vha, &port_id);
4700
Quinn Tran75601512015-12-17 14:57:04 -05004701 if (wwn) {
4702 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004703 sess = qlt_find_sess_invalidate_other(vha, wwn,
4704 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004705 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4706 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004707
Quinn Tran726b8542017-01-19 22:28:00 -08004708 if (IS_SW_RESV_ADDR(port_id)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004709 res = 1;
4710 break;
4711 }
4712
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004713 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4714 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004715 qlt_send_term_imm_notif(vha, iocb, 1);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004716 break;
4717 }
4718
4719 res = 0;
4720
Quinn Tran726b8542017-01-19 22:28:00 -08004721 if (conflict_sess) {
4722 conflict_sess->login_gen++;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004723 qlt_plogi_ack_link(vha, pla, conflict_sess,
Quinn Tran726b8542017-01-19 22:28:00 -08004724 QLT_PLOGI_LINK_CONFLICT);
4725 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004726
Quinn Tran726b8542017-01-19 22:28:00 -08004727 if (!sess) {
4728 pla->ref_count++;
4729 qla24xx_post_newsess_work(vha, &port_id,
4730 iocb->u.isp24.port_name, pla);
4731 res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004732 break;
Quinn Tran726b8542017-01-19 22:28:00 -08004733 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004734
4735 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Quinn Tran726b8542017-01-19 22:28:00 -08004736 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4737 sess->d_id = port_id;
4738 sess->login_gen++;
4739
4740 switch (sess->disc_state) {
4741 case DSC_DELETED:
4742 qlt_plogi_ack_unref(vha, pla);
4743 break;
4744
4745 default:
4746 /*
4747 * Under normal circumstances we want to release nport handle
4748 * during LOGO process to avoid nport handle leaks inside FW.
4749 * The exception is when LOGO is done while another PLOGI with
4750 * the same nport handle is waiting as might be the case here.
4751 * Note: there is always a possibily of a race where session
4752 * deletion has already started for other reasons (e.g. ACL
4753 * removal) and now PLOGI arrives:
4754 * 1. if PLOGI arrived in FW after nport handle has been freed,
4755 * FW must have assigned this PLOGI a new/same handle and we
4756 * can proceed ACK'ing it as usual when session deletion
4757 * completes.
4758 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4759 * bit reached it, the handle has now been released. We'll
4760 * get an error when we ACK this PLOGI. Nothing will be sent
4761 * back to initiator. Initiator should eventually retry
4762 * PLOGI and situation will correct itself.
4763 */
4764 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4765 (sess->d_id.b24 == port_id.b24));
4766
Quinn Tran83548fe2017-06-02 09:12:01 -07004767 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4768 "%s %d %8phC post del sess\n",
4769 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004770
4771
4772 qlt_schedule_sess_for_deletion_lock(sess);
4773 break;
4774 }
4775
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004776 break;
4777
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004778 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004779 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4780
Quinn Tran75601512015-12-17 14:57:04 -05004781 if (wwn) {
4782 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004783 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4784 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004785 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4786 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004787
4788 if (conflict_sess) {
4789 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4790 "PRLI with conflicting sess %p port %8phC\n",
4791 conflict_sess, conflict_sess->port_name);
4792 qlt_send_term_imm_notif(vha, iocb, 1);
4793 res = 0;
4794 break;
4795 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004796
4797 if (sess != NULL) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004798 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4799 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004800 /*
4801 * Impatient initiator sent PRLI before last
4802 * PLOGI could finish. Will force him to re-try,
4803 * while last one finishes.
4804 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004805 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004806 "sess %p PRLI received, before plogi ack.\n",
4807 sess);
4808 qlt_send_term_imm_notif(vha, iocb, 1);
4809 res = 0;
4810 break;
4811 }
4812
4813 /*
4814 * This shouldn't happen under normal circumstances,
4815 * since we have deleted the old session during PLOGI
4816 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004817 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004818 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4819 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4820
4821 sess->local = 0;
4822 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004823 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004824 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004825
4826 if (wd3_lo & BIT_7)
4827 sess->conf_compl_supported = 1;
4828
Quinn Tran726b8542017-01-19 22:28:00 -08004829 if ((wd3_lo & BIT_4) == 0)
4830 sess->port_type = FCT_INITIATOR;
4831 else
4832 sess->port_type = FCT_TARGET;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004833 }
4834 res = 1; /* send notify ack */
4835
4836 /* Make session global (not used in fabric mode) */
4837 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004838 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004839 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004840 "%s %d %8phC post nack\n",
4841 __func__, __LINE__, sess->port_name);
4842 qla24xx_post_nack_work(vha, sess, iocb,
4843 SRB_NACK_PRLI);
4844 res = 0;
4845 } else {
4846 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4847 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4848 qla2xxx_wake_dpc(vha);
4849 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004850 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004851 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004852 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004853 "%s %d %8phC post nack\n",
4854 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004855 qla24xx_post_nack_work(vha, sess, iocb,
4856 SRB_NACK_PRLI);
4857 res = 0;
4858 }
4859 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004860 break;
4861
Quinn Tran41dc5292017-01-19 22:28:03 -08004862 case ELS_TPRLO:
4863 if (le16_to_cpu(iocb->u.isp24.flags) &
4864 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4865 loop_id = 0xFFFF;
4866 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4867 res = 1;
4868 break;
4869 }
4870 /* drop through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004871 case ELS_LOGO:
4872 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004873 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4874 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4875 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4876
4877 if (sess) {
4878 sess->login_gen++;
4879 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004880 sess->logo_ack_needed = 1;
4881 memcpy(sess->iocb, iocb, IOCB_SIZE);
4882 }
4883
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004884 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004885
Quinn Tran83548fe2017-06-02 09:12:01 -07004886 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08004887 "%s: logo %llx res %d sess %p ",
4888 __func__, wwn, res, sess);
4889 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004890 /*
4891 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4892 * for LOGO_ACK & sess delete
4893 */
Quinn Tran726b8542017-01-19 22:28:00 -08004894 BUG_ON(!sess);
4895 res = 0;
4896 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004897 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004898 if (sess) {
4899 qlt_schedule_sess_for_deletion_lock(sess);
4900 res = 0;
4901 }
4902 /* else logo will be ack */
4903 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004904 break;
4905 case ELS_PDISC:
4906 case ELS_ADISC:
4907 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004908 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004909 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004910 qlt_send_notify_ack(ha->base_qpair,
4911 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004912 tgt->link_reinit_iocb_pending = 0;
4913 }
Quinn Tran726b8542017-01-19 22:28:00 -08004914
4915 sess = qla2x00_find_fcport_by_wwpn(vha,
4916 iocb->u.isp24.port_name, 1);
4917 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004918 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08004919 "sess %p lid %d|%d DS %d LS %d\n",
4920 sess, sess->loop_id, loop_id,
4921 sess->disc_state, sess->fw_login_state);
4922 }
4923
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004924 res = 1; /* send notify ack */
4925 break;
4926 }
4927
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004928 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004929 default:
4930 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4931 "qla_target(%d): Unsupported ELS command %x "
4932 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4933 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4934 break;
4935 }
4936
4937 return res;
4938}
4939
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004940/*
4941 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4942 */
4943static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4944 struct imm_ntfy_from_isp *iocb)
4945{
4946 struct qla_hw_data *ha = vha->hw;
4947 uint32_t add_flags = 0;
4948 int send_notify_ack = 1;
4949 uint16_t status;
4950
4951 status = le16_to_cpu(iocb->u.isp2x.status);
4952 switch (status) {
4953 case IMM_NTFY_LIP_RESET:
4954 {
4955 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4956 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4957 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4958 iocb->u.isp24.status_subcode);
4959
4960 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4961 send_notify_ack = 0;
4962 break;
4963 }
4964
4965 case IMM_NTFY_LIP_LINK_REINIT:
4966 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004967 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004968 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4969 "qla_target(%d): LINK REINIT (loop %#x, "
4970 "subcode %x)\n", vha->vp_idx,
4971 le16_to_cpu(iocb->u.isp24.nport_handle),
4972 iocb->u.isp24.status_subcode);
4973 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07004974 qlt_send_notify_ack(ha->base_qpair,
4975 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004976 }
4977 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4978 tgt->link_reinit_iocb_pending = 1;
4979 /*
4980 * QLogic requires to wait after LINK REINIT for possible
4981 * PDISC or ADISC ELS commands
4982 */
4983 send_notify_ack = 0;
4984 break;
4985 }
4986
4987 case IMM_NTFY_PORT_LOGOUT:
4988 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4989 "qla_target(%d): Port logout (loop "
4990 "%#x, subcode %x)\n", vha->vp_idx,
4991 le16_to_cpu(iocb->u.isp24.nport_handle),
4992 iocb->u.isp24.status_subcode);
4993
4994 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4995 send_notify_ack = 0;
4996 /* The sessions will be cleared in the callback, if needed */
4997 break;
4998
4999 case IMM_NTFY_GLBL_TPRLO:
5000 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5001 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5002 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5003 send_notify_ack = 0;
5004 /* The sessions will be cleared in the callback, if needed */
5005 break;
5006
5007 case IMM_NTFY_PORT_CONFIG:
5008 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5009 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5010 status);
5011 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5012 send_notify_ack = 0;
5013 /* The sessions will be cleared in the callback, if needed */
5014 break;
5015
5016 case IMM_NTFY_GLBL_LOGO:
5017 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5018 "qla_target(%d): Link failure detected\n",
5019 vha->vp_idx);
5020 /* I_T nexus loss */
5021 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5022 send_notify_ack = 0;
5023 break;
5024
5025 case IMM_NTFY_IOCB_OVERFLOW:
5026 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5027 "qla_target(%d): Cannot provide requested "
5028 "capability (IOCB overflowed the immediate notify "
5029 "resource count)\n", vha->vp_idx);
5030 break;
5031
5032 case IMM_NTFY_ABORT_TASK:
5033 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5034 "qla_target(%d): Abort Task (S %08x I %#x -> "
5035 "L %#x)\n", vha->vp_idx,
5036 le16_to_cpu(iocb->u.isp2x.seq_id),
5037 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5038 le16_to_cpu(iocb->u.isp2x.lun));
5039 if (qlt_abort_task(vha, iocb) == 0)
5040 send_notify_ack = 0;
5041 break;
5042
5043 case IMM_NTFY_RESOURCE:
5044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5045 "qla_target(%d): Out of resources, host %ld\n",
5046 vha->vp_idx, vha->host_no);
5047 break;
5048
5049 case IMM_NTFY_MSG_RX:
5050 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5051 "qla_target(%d): Immediate notify task %x\n",
5052 vha->vp_idx, iocb->u.isp2x.task_flags);
5053 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5054 send_notify_ack = 0;
5055 break;
5056
5057 case IMM_NTFY_ELS:
5058 if (qlt_24xx_handle_els(vha, iocb) == 0)
5059 send_notify_ack = 0;
5060 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005061 default:
5062 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5063 "qla_target(%d): Received unknown immediate "
5064 "notify status %x\n", vha->vp_idx, status);
5065 break;
5066 }
5067
5068 if (send_notify_ack)
Quinn Tran82de8022017-06-13 20:47:17 -07005069 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5070 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005071}
5072
5073/*
5074 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5075 * This function sends busy to ISP 2xxx or 24xx.
5076 */
Quinn Tran82de8022017-06-13 20:47:17 -07005077static int __qlt_send_busy(struct qla_qpair *qpair,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005078 struct atio_from_isp *atio, uint16_t status)
5079{
Quinn Tran82de8022017-06-13 20:47:17 -07005080 struct scsi_qla_host *vha = qpair->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005081 struct ctio7_to_24xx *ctio24;
5082 struct qla_hw_data *ha = vha->hw;
5083 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08005084 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005085 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005086 u16 temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005087
Quinn Tran75601512015-12-17 14:57:04 -05005088 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005089 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5090 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05005091 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005092 if (!sess) {
Quinn Tran82de8022017-06-13 20:47:17 -07005093 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005094 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005095 }
5096 /* Sending marker isn't necessary, since we called from ISR */
5097
Quinn Tran82de8022017-06-13 20:47:17 -07005098 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005099 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005100 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005101 "qla_target(%d): %s failed: unable to allocate "
5102 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005103 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005104 }
5105
Quinn Tran60a9ead2017-06-13 20:47:28 -07005106 qpair->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005107 pkt->entry_count = 1;
5108 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5109
5110 ctio24 = (struct ctio7_to_24xx *)pkt;
5111 ctio24->entry_type = CTIO_TYPE7;
5112 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005113 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005114 ctio24->vp_index = vha->vp_idx;
5115 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5116 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5117 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5118 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005119 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005120 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005121 CTIO7_FLAGS_DONT_RET_CTIO;
5122 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005123 /*
5124 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5125 * if the explicit conformation is used.
5126 */
5127 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5128 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005129 /* Memory Barrier */
5130 wmb();
Quinn Tran8abfa9e2017-06-13 20:47:24 -07005131 if (qpair->reqq_start_iocbs)
5132 qpair->reqq_start_iocbs(qpair);
5133 else
5134 qla2x00_start_iocbs(vha, qpair->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005135 return 0;
5136}
5137
5138/*
5139 * This routine is used to allocate a command for either a QFull condition
5140 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5141 * out previously.
5142 */
5143static void
5144qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5145 struct atio_from_isp *atio, uint16_t status, int qfull)
5146{
5147 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5148 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005149 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005150 struct se_session *se_sess;
5151 struct qla_tgt_cmd *cmd;
5152 int tag;
Quinn Tran82de8022017-06-13 20:47:17 -07005153 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005154
5155 if (unlikely(tgt->tgt_stop)) {
5156 ql_dbg(ql_dbg_io, vha, 0x300a,
5157 "New command while device %p is shutting down\n", tgt);
5158 return;
5159 }
5160
5161 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5162 vha->hw->tgt.num_qfull_cmds_dropped++;
5163 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005164 vha->qla_stats.stat_max_qfull_cmds_dropped)
5165 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005166 vha->hw->tgt.num_qfull_cmds_dropped;
5167
5168 ql_dbg(ql_dbg_io, vha, 0x3068,
5169 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5170 vha->vp_idx, __func__,
5171 vha->hw->tgt.num_qfull_cmds_dropped);
5172
5173 qlt_chk_exch_leak_thresh_hold(vha);
5174 return;
5175 }
5176
5177 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5178 (vha, atio->u.isp24.fcp_hdr.s_id);
5179 if (!sess)
5180 return;
5181
5182 se_sess = sess->se_sess;
5183
5184 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5185 if (tag < 0)
5186 return;
5187
5188 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5189 if (!cmd) {
5190 ql_dbg(ql_dbg_io, vha, 0x3009,
5191 "qla_target(%d): %s: Allocation of cmd failed\n",
5192 vha->vp_idx, __func__);
5193
5194 vha->hw->tgt.num_qfull_cmds_dropped++;
5195 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005196 vha->qla_stats.stat_max_qfull_cmds_dropped)
5197 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005198 vha->hw->tgt.num_qfull_cmds_dropped;
5199
5200 qlt_chk_exch_leak_thresh_hold(vha);
5201 return;
5202 }
5203
5204 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5205
5206 qlt_incr_num_pend_cmds(vha);
5207 INIT_LIST_HEAD(&cmd->cmd_list);
5208 memcpy(&cmd->atio, atio, sizeof(*atio));
5209
5210 cmd->tgt = vha->vha_tgt.qla_tgt;
5211 cmd->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005212 cmd->reset_count = ha->base_qpair->chip_reset;
Quinn Tran33e79972014-09-25 06:14:55 -04005213 cmd->q_full = 1;
Quinn Tran82de8022017-06-13 20:47:17 -07005214 cmd->qpair = ha->base_qpair;
Quinn Tran33e79972014-09-25 06:14:55 -04005215
5216 if (qfull) {
5217 cmd->q_full = 1;
5218 /* NOTE: borrowing the state field to carry the status */
5219 cmd->state = status;
5220 } else
5221 cmd->term_exchg = 1;
5222
Quinn Tran82de8022017-06-13 20:47:17 -07005223 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005224 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5225
5226 vha->hw->tgt.num_qfull_cmds_alloc++;
5227 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005228 vha->qla_stats.stat_max_qfull_cmds_alloc)
5229 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005230 vha->hw->tgt.num_qfull_cmds_alloc;
Quinn Tran82de8022017-06-13 20:47:17 -07005231 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005232}
5233
5234int
Quinn Tran82de8022017-06-13 20:47:17 -07005235qlt_free_qfull_cmds(struct qla_qpair *qpair)
Quinn Tran33e79972014-09-25 06:14:55 -04005236{
Quinn Tran82de8022017-06-13 20:47:17 -07005237 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005238 struct qla_hw_data *ha = vha->hw;
5239 unsigned long flags;
5240 struct qla_tgt_cmd *cmd, *tcmd;
Quinn Tran82de8022017-06-13 20:47:17 -07005241 struct list_head free_list, q_full_list;
Quinn Tran33e79972014-09-25 06:14:55 -04005242 int rc = 0;
5243
5244 if (list_empty(&ha->tgt.q_full_list))
5245 return 0;
5246
5247 INIT_LIST_HEAD(&free_list);
Quinn Tran82de8022017-06-13 20:47:17 -07005248 INIT_LIST_HEAD(&q_full_list);
Quinn Tran33e79972014-09-25 06:14:55 -04005249
Quinn Tran82de8022017-06-13 20:47:17 -07005250 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005251 if (list_empty(&ha->tgt.q_full_list)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005252 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005253 return 0;
5254 }
5255
Quinn Tran82de8022017-06-13 20:47:17 -07005256 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5257 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5258
5259 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5260 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
Quinn Tran33e79972014-09-25 06:14:55 -04005261 if (cmd->q_full)
5262 /* cmd->state is a borrowed field to hold status */
Quinn Tran82de8022017-06-13 20:47:17 -07005263 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
Quinn Tran33e79972014-09-25 06:14:55 -04005264 else if (cmd->term_exchg)
Quinn Tran82de8022017-06-13 20:47:17 -07005265 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005266
5267 if (rc == -ENOMEM)
5268 break;
5269
5270 if (cmd->q_full)
5271 ql_dbg(ql_dbg_io, vha, 0x3006,
5272 "%s: busy sent for ox_id[%04x]\n", __func__,
5273 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5274 else if (cmd->term_exchg)
5275 ql_dbg(ql_dbg_io, vha, 0x3007,
5276 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5277 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5278 else
5279 ql_dbg(ql_dbg_io, vha, 0x3008,
5280 "%s: Unexpected cmd in QFull list %p\n", __func__,
5281 cmd);
5282
5283 list_del(&cmd->cmd_list);
5284 list_add_tail(&cmd->cmd_list, &free_list);
5285
5286 /* piggy back on hardware_lock for protection */
5287 vha->hw->tgt.num_qfull_cmds_alloc--;
5288 }
Quinn Tran82de8022017-06-13 20:47:17 -07005289 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005290
5291 cmd = NULL;
5292
5293 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5294 list_del(&cmd->cmd_list);
5295 /* This cmd was never sent to TCM. There is no need
5296 * to schedule free or call free_cmd
5297 */
5298 qlt_free_cmd(cmd);
5299 }
Quinn Tran82de8022017-06-13 20:47:17 -07005300
5301 if (!list_empty(&q_full_list)) {
5302 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5303 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5304 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5305 }
5306
Quinn Tran33e79972014-09-25 06:14:55 -04005307 return rc;
5308}
5309
5310static void
Quinn Tran82de8022017-06-13 20:47:17 -07005311qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5312 uint16_t status)
Quinn Tran33e79972014-09-25 06:14:55 -04005313{
5314 int rc = 0;
Quinn Tran82de8022017-06-13 20:47:17 -07005315 struct scsi_qla_host *vha = qpair->vha;
Quinn Tran33e79972014-09-25 06:14:55 -04005316
Quinn Tran82de8022017-06-13 20:47:17 -07005317 rc = __qlt_send_busy(qpair, atio, status);
Quinn Tran33e79972014-09-25 06:14:55 -04005318 if (rc == -ENOMEM)
5319 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5320}
5321
5322static int
Quinn Tran82de8022017-06-13 20:47:17 -07005323qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5324 struct atio_from_isp *atio, uint8_t ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005325{
5326 struct qla_hw_data *ha = vha->hw;
5327 uint16_t status;
Quinn Tran8b666802017-03-15 09:48:45 -07005328 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005329
5330 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5331 return 0;
5332
Quinn Tran8b666802017-03-15 09:48:45 -07005333 if (!ha_locked)
5334 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005335 status = temp_sam_status;
Quinn Tran82de8022017-06-13 20:47:17 -07005336 qlt_send_busy(qpair, atio, status);
Quinn Tran8b666802017-03-15 09:48:45 -07005337 if (!ha_locked)
5338 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5339
Quinn Tran33e79972014-09-25 06:14:55 -04005340 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005341}
5342
5343/* ha->hardware_lock supposed to be held on entry */
5344/* called via callback from qla2xxx */
5345static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005346 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005347{
5348 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005349 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005350 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005351 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005352
5353 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005354 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005355 "ATIO pkt, but no tgt (ha %p)", ha);
5356 return;
5357 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005358 /*
5359 * In tgt_stop mode we also should allow all requests to pass.
5360 * Otherwise, some commands can stuck.
5361 */
5362
Quinn Tran2f424b92015-12-17 14:57:07 -05005363 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005364
5365 switch (atio->u.raw.entry_type) {
5366 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005367 if (unlikely(atio->u.isp24.exchange_addr ==
5368 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005369 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005370 "qla_target(%d): ATIO_TYPE7 "
5371 "received with UNKNOWN exchange address, "
5372 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005373 if (!ha_locked)
5374 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005375 qlt_send_busy(ha->base_qpair, atio,
5376 SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005377 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005378 spin_unlock_irqrestore(&ha->hardware_lock,
5379 flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005380 break;
5381 }
Quinn Tran33e79972014-09-25 06:14:55 -04005382
Quinn Tran33e79972014-09-25 06:14:55 -04005383 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran82de8022017-06-13 20:47:17 -07005384 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5385 atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005386 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005387 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005388 return;
5389 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005390 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005391 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005392 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005393 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005394 if (unlikely(rc != 0)) {
5395 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005396 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005397 spin_lock_irqsave(&ha->hardware_lock,
5398 flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05005399
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005400#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
Quinn Tran82de8022017-06-13 20:47:17 -07005401 qlt_send_busy(ha->base_qpair, atio,
5402 SAM_STAT_BUSY);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005403#else
Quinn Tran82de8022017-06-13 20:47:17 -07005404 qlt_send_term_exchange(ha->base_qpair, NULL,
5405 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005406#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005407 if (!ha_locked)
Quinn Tran82de8022017-06-13 20:47:17 -07005408 spin_unlock_irqrestore(
5409 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005410 } else {
5411 if (tgt->tgt_stop) {
5412 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5413 "qla_target: Unable to send "
5414 "command to target for req, "
5415 "ignoring.\n");
5416 } else {
5417 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5418 "qla_target(%d): Unable to send "
5419 "command to target, sending BUSY "
5420 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005421 if (!ha_locked)
5422 spin_lock_irqsave(
5423 &ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005424 qlt_send_busy(ha->base_qpair,
5425 atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005426 if (!ha_locked)
5427 spin_unlock_irqrestore(
5428 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005429 }
5430 }
5431 }
5432 break;
5433
5434 case IMMED_NOTIFY_TYPE:
5435 {
5436 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5437 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5438 "qla_target(%d): Received ATIO packet %x "
5439 "with error status %x\n", vha->vp_idx,
5440 atio->u.raw.entry_type,
5441 atio->u.isp2x.entry_status);
5442 break;
5443 }
5444 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005445
5446 if (!ha_locked)
5447 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005448 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005449 if (!ha_locked)
5450 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005451 break;
5452 }
5453
5454 default:
5455 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5456 "qla_target(%d): Received unknown ATIO atio "
5457 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5458 break;
5459 }
5460
Quinn Tran2f424b92015-12-17 14:57:07 -05005461 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005462}
5463
5464/* ha->hardware_lock supposed to be held on entry */
5465/* called via callback from qla2xxx */
Quinn Tran82de8022017-06-13 20:47:17 -07005466static void qlt_response_pkt(struct scsi_qla_host *vha,
5467 struct rsp_que *rsp, response_t *pkt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005468{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005469 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005470
5471 if (unlikely(tgt == NULL)) {
5472 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
Quinn Tran60a9ead2017-06-13 20:47:28 -07005473 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5474 vha->vp_idx, pkt->entry_type, vha->hw);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005475 return;
5476 }
5477
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005478 /*
5479 * In tgt_stop mode we also should allow all requests to pass.
5480 * Otherwise, some commands can stuck.
5481 */
5482
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005483 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005484 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005485 case CTIO_TYPE7:
5486 {
5487 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005488 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005489 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5490 entry);
5491 break;
5492 }
5493
5494 case ACCEPT_TGT_IO_TYPE:
5495 {
5496 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5497 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005498 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005499 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005500 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5501 "qla_target(%d): ATIO with error "
5502 "status %x received\n", vha->vp_idx,
5503 le16_to_cpu(atio->u.isp2x.status));
5504 break;
5505 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005506
Quinn Tran82de8022017-06-13 20:47:17 -07005507 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
Quinn Tranba68a632017-06-02 09:12:06 -07005508 if (rc != 0)
Quinn Tran33e79972014-09-25 06:14:55 -04005509 return;
Quinn Tran33e79972014-09-25 06:14:55 -04005510
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005511 rc = qlt_handle_cmd_for_atio(vha, atio);
5512 if (unlikely(rc != 0)) {
5513 if (rc == -ESRCH) {
5514#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
Quinn Tran82de8022017-06-13 20:47:17 -07005515 qlt_send_busy(rsp->qpair, atio, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005516#else
Quinn Tran82de8022017-06-13 20:47:17 -07005517 qlt_send_term_exchange(rsp->qpair, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005518#endif
5519 } else {
5520 if (tgt->tgt_stop) {
5521 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5522 "qla_target: Unable to send "
5523 "command to target, sending TERM "
5524 "EXCHANGE for rsp\n");
Quinn Tran82de8022017-06-13 20:47:17 -07005525 qlt_send_term_exchange(rsp->qpair, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005526 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005527 } else {
5528 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5529 "qla_target(%d): Unable to send "
5530 "command to target, sending BUSY "
5531 "status\n", vha->vp_idx);
Quinn Tran82de8022017-06-13 20:47:17 -07005532 qlt_send_busy(rsp->qpair, atio, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005533 }
5534 }
5535 }
5536 }
5537 break;
5538
5539 case CONTINUE_TGT_IO_TYPE:
5540 {
5541 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005542 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005543 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5544 entry);
5545 break;
5546 }
5547
5548 case CTIO_A64_TYPE:
5549 {
5550 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Quinn Tran82de8022017-06-13 20:47:17 -07005551 qlt_do_ctio_completion(vha, rsp, entry->handle,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005552 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5553 entry);
5554 break;
5555 }
5556
5557 case IMMED_NOTIFY_TYPE:
5558 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5559 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5560 break;
5561
5562 case NOTIFY_ACK_TYPE:
5563 if (tgt->notify_ack_expected > 0) {
5564 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5565 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5566 "NOTIFY_ACK seq %08x status %x\n",
5567 le16_to_cpu(entry->u.isp2x.seq_id),
5568 le16_to_cpu(entry->u.isp2x.status));
5569 tgt->notify_ack_expected--;
5570 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005571 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005572 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5573 "qla_target(%d): NOTIFY_ACK "
5574 "failed %x\n", vha->vp_idx,
5575 le16_to_cpu(entry->u.isp2x.status));
5576 }
5577 } else {
5578 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5579 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5580 vha->vp_idx);
5581 }
5582 break;
5583
5584 case ABTS_RECV_24XX:
5585 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5586 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5587 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5588 break;
5589
5590 case ABTS_RESP_24XX:
5591 if (tgt->abts_resp_expected > 0) {
5592 struct abts_resp_from_24xx_fw *entry =
5593 (struct abts_resp_from_24xx_fw *)pkt;
5594 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5595 "ABTS_RESP_24XX: compl_status %x\n",
5596 entry->compl_status);
5597 tgt->abts_resp_expected--;
5598 if (le16_to_cpu(entry->compl_status) !=
5599 ABTS_RESP_COMPL_SUCCESS) {
5600 if ((entry->error_subcode1 == 0x1E) &&
5601 (entry->error_subcode2 == 0)) {
5602 /*
5603 * We've got a race here: aborted
5604 * exchange not terminated, i.e.
5605 * response for the aborted command was
5606 * sent between the abort request was
5607 * received and processed.
5608 * Unfortunately, the firmware has a
5609 * silly requirement that all aborted
5610 * exchanges must be explicitely
5611 * terminated, otherwise it refuses to
5612 * send responses for the abort
5613 * requests. So, we have to
5614 * (re)terminate the exchange and retry
5615 * the abort response.
5616 */
5617 qlt_24xx_retry_term_exchange(vha,
5618 entry);
5619 } else
5620 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5621 "qla_target(%d): ABTS_RESP_24XX "
5622 "failed %x (subcode %x:%x)",
5623 vha->vp_idx, entry->compl_status,
5624 entry->error_subcode1,
5625 entry->error_subcode2);
5626 }
5627 } else {
5628 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5629 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5630 "received\n", vha->vp_idx);
5631 }
5632 break;
5633
5634 default:
5635 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5636 "qla_target(%d): Received unknown response pkt "
5637 "type %x\n", vha->vp_idx, pkt->entry_type);
5638 break;
5639 }
5640
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005641}
5642
5643/*
5644 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5645 */
5646void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5647 uint16_t *mailbox)
5648{
5649 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005650 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005651 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005652
Quinn Tran3a33dc92017-06-02 09:12:04 -07005653 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005654 return;
5655
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005656 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5657 IS_QLA2100(ha))
5658 return;
5659 /*
5660 * In tgt_stop mode we also should allow all requests to pass.
5661 * Otherwise, some commands can stuck.
5662 */
5663
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005664
5665 switch (code) {
5666 case MBA_RESET: /* Reset */
5667 case MBA_SYSTEM_ERR: /* System Error */
5668 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5669 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5670 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5671 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005672 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005673 break;
5674 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5675 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5676 break;
5677
5678 case MBA_LOOP_UP:
5679 {
5680 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005681 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005682 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5683 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5684 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005685 if (tgt->link_reinit_iocb_pending) {
Quinn Tran82de8022017-06-13 20:47:17 -07005686 qlt_send_notify_ack(ha->base_qpair,
5687 (void *)&tgt->link_reinit_iocb,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005688 0, 0, 0, 0, 0, 0);
5689 tgt->link_reinit_iocb_pending = 0;
5690 }
5691 break;
5692 }
5693
5694 case MBA_LIP_OCCURRED:
5695 case MBA_LOOP_DOWN:
5696 case MBA_LIP_RESET:
5697 case MBA_RSCN_UPDATE:
5698 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005699 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005700 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5701 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5702 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005703 break;
5704
Quinn Tranead03852017-01-19 22:28:01 -08005705 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005706 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5707 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5708 vha->vp_idx,
5709 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5710 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005711
5712 if (le16_to_cpu(mailbox[3]) == 1) {
5713 /* exchange starvation. */
5714 vha->hw->exch_starvation++;
5715 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005716 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005717 "Exchange starvation-. Resetting RISC\n");
5718
5719 vha->hw->exch_starvation = 0;
5720 if (IS_P3P_TYPE(vha->hw))
5721 set_bit(FCOE_CTX_RESET_NEEDED,
5722 &vha->dpc_flags);
5723 else
5724 set_bit(ISP_ABORT_NEEDED,
5725 &vha->dpc_flags);
5726 qla2xxx_wake_dpc(vha);
5727 }
5728 }
5729 break;
5730
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005731 case MBA_PORT_UPDATE:
5732 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5733 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005734 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005735 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5736 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5737 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5738
5739 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005740 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005741 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5742 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005743 vha->hw->exch_starvation = 0;
5744 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005745 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5746 "Async MB 2: Port Logged Out\n");
5747 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005748 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005749 break;
5750 }
5751
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005752}
5753
5754static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5755 uint16_t loop_id)
5756{
Quinn Tran726b8542017-01-19 22:28:00 -08005757 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005758 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005759 unsigned long flags;
5760 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005761
5762 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5763 if (!fcport) {
5764 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5765 "qla_target(%d): Allocation of tmp FC port failed",
5766 vha->vp_idx);
5767 return NULL;
5768 }
5769
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005770 fcport->loop_id = loop_id;
5771
Quinn Tran15f30a52017-03-15 09:48:52 -07005772 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005773 if (rc != QLA_SUCCESS) {
5774 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5775 "qla_target(%d): Failed to retrieve fcport "
5776 "information -- get_port_database() returned %x "
5777 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5778 kfree(fcport);
5779 return NULL;
5780 }
5781
Quinn Tran726b8542017-01-19 22:28:00 -08005782 del = NULL;
5783 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5784 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5785
5786 if (tfcp) {
5787 tfcp->d_id = fcport->d_id;
5788 tfcp->port_type = fcport->port_type;
5789 tfcp->supported_classes = fcport->supported_classes;
5790 tfcp->flags |= fcport->flags;
5791
5792 del = fcport;
5793 fcport = tfcp;
5794 } else {
5795 if (vha->hw->current_topology == ISP_CFG_F)
5796 fcport->flags |= FCF_FABRIC_DEVICE;
5797
5798 list_add_tail(&fcport->list, &vha->vp_fcports);
5799 if (!IS_SW_RESV_ADDR(fcport->d_id))
5800 vha->fcport_count++;
5801 fcport->login_gen++;
5802 fcport->disc_state = DSC_LOGIN_COMPLETE;
5803 fcport->login_succ = 1;
5804 newfcport = 1;
5805 }
5806
5807 fcport->deleted = 0;
5808 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5809
5810 switch (vha->host->active_mode) {
5811 case MODE_INITIATOR:
5812 case MODE_DUAL:
5813 if (newfcport) {
5814 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005815 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005816 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5817 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5818 qla24xx_post_upd_fcport_work(vha, fcport);
5819 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005820 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005821 "%s %d %8phC post gpsc fcp_cnt %d\n",
5822 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5823 qla24xx_post_gpsc_work(vha, fcport);
5824 }
5825 }
5826 break;
5827
5828 case MODE_TARGET:
5829 default:
5830 break;
5831 }
5832 if (del)
5833 qla2x00_free_fcport(del);
5834
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005835 return fcport;
5836}
5837
5838/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005839static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005840 uint8_t *s_id)
5841{
Quinn Tran5d964832017-01-19 22:27:59 -08005842 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005843 fc_port_t *fcport = NULL;
5844 int rc, global_resets;
5845 uint16_t loop_id = 0;
5846
Quinn Tran726b8542017-01-19 22:28:00 -08005847 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5848 /*
5849 * This is Domain Controller, so it should be
5850 * OK to drop SCSI commands from it.
5851 */
5852 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5853 "Unable to find initiator with S_ID %x:%x:%x",
5854 s_id[0], s_id[1], s_id[2]);
5855 return NULL;
5856 }
5857
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005858 mutex_lock(&vha->vha_tgt.tgt_mutex);
5859
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005860retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005861 global_resets =
5862 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005863
5864 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5865 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005866 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5867
Quinn Tran726b8542017-01-19 22:28:00 -08005868 ql_log(ql_log_info, vha, 0xf071,
5869 "qla_target(%d): Unable to find "
5870 "initiator with S_ID %x:%x:%x",
5871 vha->vp_idx, s_id[0], s_id[1],
5872 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005873
5874 if (rc == -ENOENT) {
5875 qlt_port_logo_t logo;
5876 sid_to_portid(s_id, &logo.id);
5877 logo.cmd_count = 1;
5878 qlt_send_first_logo(vha, &logo);
5879 }
5880
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005881 return NULL;
5882 }
5883
5884 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005885 if (!fcport) {
5886 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005887 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005888 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005889
5890 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005891 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005892 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5893 "qla_target(%d): global reset during session discovery "
5894 "(counter was %d, new %d), retrying", vha->vp_idx,
5895 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005896 atomic_read(&vha->vha_tgt.
5897 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005898 goto retry;
5899 }
5900
5901 sess = qlt_create_sess(vha, fcport, true);
5902
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005903 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5904
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005905 return sess;
5906}
5907
5908static void qlt_abort_work(struct qla_tgt *tgt,
5909 struct qla_tgt_sess_work_param *prm)
5910{
5911 struct scsi_qla_host *vha = tgt->vha;
5912 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005913 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005914 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005915 uint32_t be_s_id;
5916 uint8_t s_id[3];
5917 int rc;
5918
Quinn Tran75601512015-12-17 14:57:04 -05005919 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005920
5921 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005922 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005923
5924 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5925 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5926 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5927
5928 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5929 (unsigned char *)&be_s_id);
5930 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005931 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005932
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005933 sess = qlt_make_local_sess(vha, s_id);
5934 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005935
Quinn Tran75601512015-12-17 14:57:04 -05005936 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005937 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005938 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005939 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005940 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005941 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005942 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005943 }
5944
Quinn Tran726b8542017-01-19 22:28:00 -08005945 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005946 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08005947 "%s: kref_get fail %8phC \n",
5948 __func__, sess->port_name);
5949 sess = NULL;
5950 goto out_term2;
5951 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005952 }
5953
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005954 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005955 ha->tgt.tgt_ops->put_sess(sess);
5956 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5957
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005958 if (rc != 0)
5959 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005960 return;
5961
Quinn Tran75601512015-12-17 14:57:04 -05005962out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08005963 if (sess)
5964 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005965 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005966
5967out_term:
5968 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07005969 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
5970 FCP_TMF_REJECTED, false);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005971 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005972}
5973
5974static void qlt_tmr_work(struct qla_tgt *tgt,
5975 struct qla_tgt_sess_work_param *prm)
5976{
5977 struct atio_from_isp *a = &prm->tm_iocb2;
5978 struct scsi_qla_host *vha = tgt->vha;
5979 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005980 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005981 unsigned long flags;
5982 uint8_t *s_id = NULL; /* to hide compiler warnings */
5983 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07005984 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005985 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005986 void *iocb;
5987
Quinn Tran75601512015-12-17 14:57:04 -05005988 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005989
5990 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005991 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005992
5993 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5994 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5995 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005996 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005997
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005998 sess = qlt_make_local_sess(vha, s_id);
5999 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006000
Quinn Tran75601512015-12-17 14:57:04 -05006001 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006002 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07006003 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006004 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08006005 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006006 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006007 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04006008 }
6009
Quinn Tran726b8542017-01-19 22:28:00 -08006010 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006011 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08006012 "%s: kref_get fail %8phC\n",
6013 __func__, sess->port_name);
6014 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07006015 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08006016 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006017 }
6018
6019 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006020 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07006021 unpacked_lun =
6022 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006023
6024 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07006025 ha->tgt.tgt_ops->put_sess(sess);
6026 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6027
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006028 if (rc != 0)
6029 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006030 return;
6031
Quinn Tranf159b3c2017-03-15 09:48:47 -07006032out_term2:
6033 if (sess)
6034 ha->tgt.tgt_ops->put_sess(sess);
6035 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006036out_term:
Quinn Tran82de8022017-06-13 20:47:17 -07006037 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006038}
6039
6040static void qlt_sess_work_fn(struct work_struct *work)
6041{
6042 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6043 struct scsi_qla_host *vha = tgt->vha;
6044 unsigned long flags;
6045
6046 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6047
6048 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6049 while (!list_empty(&tgt->sess_works_list)) {
6050 struct qla_tgt_sess_work_param *prm = list_entry(
6051 tgt->sess_works_list.next, typeof(*prm),
6052 sess_works_list_entry);
6053
6054 /*
6055 * This work can be scheduled on several CPUs at time, so we
6056 * must delete the entry to eliminate double processing
6057 */
6058 list_del(&prm->sess_works_list_entry);
6059
6060 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6061
6062 switch (prm->type) {
6063 case QLA_TGT_SESS_WORK_ABORT:
6064 qlt_abort_work(tgt, prm);
6065 break;
6066 case QLA_TGT_SESS_WORK_TM:
6067 qlt_tmr_work(tgt, prm);
6068 break;
6069 default:
6070 BUG_ON(1);
6071 break;
6072 }
6073
6074 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6075
6076 kfree(prm);
6077 }
6078 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6079}
6080
6081/* Must be called under tgt_host_action_mutex */
6082int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6083{
6084 struct qla_tgt *tgt;
Quinn Trane326d222017-06-13 20:47:18 -07006085 int rc, i;
6086 struct qla_qpair_hint *h;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006087
6088 if (!QLA_TGT_MODE_ENABLED())
6089 return 0;
6090
Arun Easi33c36c02013-01-30 03:34:41 -05006091 if (!IS_TGT_MODE_CAPABLE(ha)) {
6092 ql_log(ql_log_warn, base_vha, 0xe070,
6093 "This adapter does not support target mode.\n");
6094 return 0;
6095 }
6096
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006097 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006098 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006099
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006100 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006101
6102 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6103 if (!tgt) {
6104 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6105 "Unable to allocate struct qla_tgt\n");
6106 return -ENOMEM;
6107 }
6108
Quinn Trane326d222017-06-13 20:47:18 -07006109 tgt->qphints = kzalloc((ha->max_qpairs + 1) *
6110 sizeof(struct qla_qpair_hint), GFP_KERNEL);
6111 if (!tgt->qphints) {
6112 kfree(tgt);
6113 ql_log(ql_log_warn, base_vha, 0x0197,
6114 "Unable to allocate qpair hints.\n");
6115 return -ENOMEM;
6116 }
6117
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006118 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6119 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6120
Quinn Trane326d222017-06-13 20:47:18 -07006121 rc = btree_init64(&tgt->lun_qpair_map);
6122 if (rc) {
6123 kfree(tgt->qphints);
6124 kfree(tgt);
6125 ql_log(ql_log_info, base_vha, 0x0198,
6126 "Unable to initialize lun_qpair_map btree\n");
6127 return -EIO;
6128 }
6129 h = &tgt->qphints[0];
6130 h->qpair = ha->base_qpair;
6131 INIT_LIST_HEAD(&h->hint_elem);
6132 h->cpuid = ha->base_qpair->cpuid;
6133 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6134
6135 for (i = 0; i < ha->max_qpairs; i++) {
6136 unsigned long flags;
6137
6138 struct qla_qpair *qpair = ha->queue_pair_map[i];
6139 h = &tgt->qphints[i + 1];
6140 INIT_LIST_HEAD(&h->hint_elem);
6141 if (qpair) {
6142 h->qpair = qpair;
6143 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6144 list_add_tail(&h->hint_elem, &qpair->hints_list);
6145 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6146 h->cpuid = qpair->cpuid;
6147 }
6148 }
6149
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006150 tgt->ha = ha;
6151 tgt->vha = base_vha;
6152 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006153 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006154 spin_lock_init(&tgt->sess_work_lock);
6155 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6156 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006157 atomic_set(&tgt->tgt_global_resets_count, 0);
6158
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006159 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006160
6161 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6162 "qla_target(%d): using 64 Bit PCI addressing",
6163 base_vha->vp_idx);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006164 /* 3 is reserved */
6165 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006166
6167 mutex_lock(&qla_tgt_mutex);
6168 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6169 mutex_unlock(&qla_tgt_mutex);
6170
Quinn Tranf1443ee2017-03-15 09:48:51 -07006171 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6172 ha->tgt.tgt_ops->add_target(base_vha);
6173
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006174 return 0;
6175}
6176
6177/* Must be called under tgt_host_action_mutex */
6178int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6179{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006180 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006181 return 0;
6182
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006183 if (vha->fc_vport) {
6184 qlt_release(vha->vha_tgt.qla_tgt);
6185 return 0;
6186 }
Quinn Tran33e79972014-09-25 06:14:55 -04006187
6188 /* free left over qfull cmds */
6189 qlt_init_term_exchange(vha);
6190
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006191 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6192 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006193 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006194
6195 return 0;
6196}
6197
Quinn Tran482c9dc2017-03-15 09:48:54 -07006198void qlt_remove_target_resources(struct qla_hw_data *ha)
6199{
6200 struct scsi_qla_host *node;
6201 u32 key = 0;
6202
6203 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6204 btree_remove32(&ha->tgt.host_map, key);
6205
6206 btree_destroy32(&ha->tgt.host_map);
6207}
6208
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006209static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6210 unsigned char *b)
6211{
6212 int i;
6213
6214 pr_debug("qla2xxx HW vha->node_name: ");
6215 for (i = 0; i < WWN_SIZE; i++)
6216 pr_debug("%02x ", vha->node_name[i]);
6217 pr_debug("\n");
6218 pr_debug("qla2xxx HW vha->port_name: ");
6219 for (i = 0; i < WWN_SIZE; i++)
6220 pr_debug("%02x ", vha->port_name[i]);
6221 pr_debug("\n");
6222
6223 pr_debug("qla2xxx passed configfs WWPN: ");
6224 put_unaligned_be64(wwpn, b);
6225 for (i = 0; i < WWN_SIZE; i++)
6226 pr_debug("%02x ", b[i]);
6227 pr_debug("\n");
6228}
6229
6230/**
6231 * qla_tgt_lport_register - register lport with external module
6232 *
6233 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6234 * @wwpn: Passwd FC target WWPN
6235 * @callback: lport initialization callback for tcm_qla2xxx code
6236 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6237 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006238int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6239 u64 npiv_wwpn, u64 npiv_wwnn,
6240 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006241{
6242 struct qla_tgt *tgt;
6243 struct scsi_qla_host *vha;
6244 struct qla_hw_data *ha;
6245 struct Scsi_Host *host;
6246 unsigned long flags;
6247 int rc;
6248 u8 b[WWN_SIZE];
6249
6250 mutex_lock(&qla_tgt_mutex);
6251 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6252 vha = tgt->vha;
6253 ha = vha->hw;
6254
6255 host = vha->host;
6256 if (!host)
6257 continue;
6258
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006259 if (!(host->hostt->supported_mode & MODE_TARGET))
6260 continue;
6261
6262 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006263 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006264 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6265 host->host_no);
6266 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6267 continue;
6268 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006269 if (tgt->tgt_stop) {
6270 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6271 host->host_no);
6272 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6273 continue;
6274 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006275 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6276
6277 if (!scsi_host_get(host)) {
6278 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6279 "Unable to scsi_host_get() for"
6280 " qla2xxx scsi_host\n");
6281 continue;
6282 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006283 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006284
6285 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6286 scsi_host_put(host);
6287 continue;
6288 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006289 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6290 if (rc != 0)
6291 scsi_host_put(host);
6292
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006293 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006294 return rc;
6295 }
6296 mutex_unlock(&qla_tgt_mutex);
6297
6298 return -ENODEV;
6299}
6300EXPORT_SYMBOL(qlt_lport_register);
6301
6302/**
6303 * qla_tgt_lport_deregister - Degister lport
6304 *
6305 * @vha: Registered scsi_qla_host pointer
6306 */
6307void qlt_lport_deregister(struct scsi_qla_host *vha)
6308{
6309 struct qla_hw_data *ha = vha->hw;
6310 struct Scsi_Host *sh = vha->host;
6311 /*
6312 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6313 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006314 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006315 ha->tgt.tgt_ops = NULL;
6316 /*
6317 * Release the Scsi_Host reference for the underlying qla2xxx host
6318 */
6319 scsi_host_put(sh);
6320}
6321EXPORT_SYMBOL(qlt_lport_deregister);
6322
6323/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006324static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006325{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006326 switch (ql2x_ini_mode) {
6327 case QLA2XXX_INI_MODE_DISABLED:
6328 case QLA2XXX_INI_MODE_EXCLUSIVE:
6329 vha->host->active_mode = MODE_TARGET;
6330 break;
6331 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006332 vha->host->active_mode = MODE_UNKNOWN;
6333 break;
6334 case QLA2XXX_INI_MODE_DUAL:
6335 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006336 break;
6337 default:
6338 break;
6339 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006340}
6341
6342/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006343static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006344{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006345 switch (ql2x_ini_mode) {
6346 case QLA2XXX_INI_MODE_DISABLED:
6347 vha->host->active_mode = MODE_UNKNOWN;
6348 break;
6349 case QLA2XXX_INI_MODE_EXCLUSIVE:
6350 vha->host->active_mode = MODE_INITIATOR;
6351 break;
6352 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006353 case QLA2XXX_INI_MODE_DUAL:
6354 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006355 break;
6356 default:
6357 break;
6358 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006359}
6360
6361/*
6362 * qla_tgt_enable_vha - NO LOCK HELD
6363 *
6364 * host_reset, bring up w/ Target Mode Enabled
6365 */
6366void
6367qlt_enable_vha(struct scsi_qla_host *vha)
6368{
6369 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006370 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006371 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006372 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006373
6374 if (!tgt) {
6375 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6376 "Unable to locate qla_tgt pointer from"
6377 " struct qla_hw_data\n");
6378 dump_stack();
6379 return;
6380 }
6381
6382 spin_lock_irqsave(&ha->hardware_lock, flags);
6383 tgt->tgt_stopped = 0;
6384 qlt_set_mode(vha);
6385 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6386
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006387 if (vha->vp_idx) {
6388 qla24xx_disable_vp(vha);
6389 qla24xx_enable_vp(vha);
6390 } else {
6391 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6392 qla2xxx_wake_dpc(base_vha);
6393 qla2x00_wait_for_hba_online(base_vha);
6394 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006395}
6396EXPORT_SYMBOL(qlt_enable_vha);
6397
6398/*
6399 * qla_tgt_disable_vha - NO LOCK HELD
6400 *
6401 * Disable Target Mode and reset the adapter
6402 */
Joern Engel55a90662014-09-16 16:23:15 -04006403static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006404{
6405 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006406 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006407 unsigned long flags;
6408
6409 if (!tgt) {
6410 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6411 "Unable to locate qla_tgt pointer from"
6412 " struct qla_hw_data\n");
6413 dump_stack();
6414 return;
6415 }
6416
6417 spin_lock_irqsave(&ha->hardware_lock, flags);
6418 qlt_clear_mode(vha);
6419 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6420
6421 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6422 qla2xxx_wake_dpc(vha);
6423 qla2x00_wait_for_hba_online(vha);
6424}
6425
6426/*
6427 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6428 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6429 * members.
6430 */
6431void
6432qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6433{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006434 vha->vha_tgt.qla_tgt = NULL;
6435
6436 mutex_init(&vha->vha_tgt.tgt_mutex);
6437 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006438
6439 qlt_clear_mode(vha);
6440
6441 /*
6442 * NOTE: Currently the value is kept the same for <24xx and
6443 * >=24xx ISPs. If it is necessary to change it,
6444 * the check should be added for specific ISPs,
6445 * assigning the value appropriately.
6446 */
6447 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006448
6449 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006450}
6451
6452void
6453qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6454{
6455 /*
6456 * FC-4 Feature bit 0 indicates target functionality to the name server.
6457 */
6458 if (qla_tgt_mode_enabled(vha)) {
Quinn Tran726b8542017-01-19 22:28:00 -08006459 ct_req->req.rff_id.fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006460 } else if (qla_ini_mode_enabled(vha)) {
6461 ct_req->req.rff_id.fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006462 } else if (qla_dual_mode_enabled(vha))
6463 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006464}
6465
6466/*
6467 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6468 * @ha: HA context
6469 *
6470 * Beginning of ATIO ring has initialization control block already built
6471 * by nvram config routine.
6472 *
6473 * Returns 0 on success.
6474 */
6475void
6476qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6477{
6478 struct qla_hw_data *ha = vha->hw;
6479 uint16_t cnt;
6480 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6481
Quinn Tranead03852017-01-19 22:28:01 -08006482 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006483 return;
6484
6485 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6486 pkt->u.raw.signature = ATIO_PROCESSED;
6487 pkt++;
6488 }
6489
6490}
6491
6492/*
6493 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6494 * @ha: SCSI driver HA context
6495 */
6496void
Quinn Tran2f424b92015-12-17 14:57:07 -05006497qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006498{
6499 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006500 struct atio_from_isp *pkt;
6501 int cnt, i;
6502
Quinn Tranec7193e2017-03-15 09:48:55 -07006503 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006504 return;
6505
Quinn Tran5f355092016-12-23 18:06:11 -08006506 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6507 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006508 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6509 cnt = pkt->u.raw.entry_count;
6510
Quinn Tran5f355092016-12-23 18:06:11 -08006511 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6512 /*
6513 * This packet is corrupted. The header + payload
6514 * can not be trusted. There is no point in passing
6515 * it further up.
6516 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006517 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006518 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6519 pkt->u.isp24.fcp_hdr.s_id,
6520 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6521 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6522
6523 adjust_corrupted_atio(pkt);
Quinn Tran82de8022017-06-13 20:47:17 -07006524 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6525 ha_locked, 0);
Quinn Tran5f355092016-12-23 18:06:11 -08006526 } else {
6527 qlt_24xx_atio_pkt_all_vps(vha,
6528 (struct atio_from_isp *)pkt, ha_locked);
6529 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006530
6531 for (i = 0; i < cnt; i++) {
6532 ha->tgt.atio_ring_index++;
6533 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6534 ha->tgt.atio_ring_index = 0;
6535 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6536 } else
6537 ha->tgt.atio_ring_ptr++;
6538
6539 pkt->u.raw.signature = ATIO_PROCESSED;
6540 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6541 }
6542 wmb();
6543 }
6544
6545 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006546 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006547}
6548
6549void
Arun Easiaa230bc2013-01-30 03:34:39 -05006550qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006551{
6552 struct qla_hw_data *ha = vha->hw;
Himanshu Madhanic9558862017-10-13 09:34:04 -07006553 struct init_cb_24xx *icb;
Arun Easiaa230bc2013-01-30 03:34:39 -05006554 if (!QLA_TGT_MODE_ENABLED())
6555 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006556
Arun Easiaa230bc2013-01-30 03:34:39 -05006557 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6558 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6559 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6560
Himanshu Madhanic9558862017-10-13 09:34:04 -07006561 icb = (struct init_cb_24xx *)ha->init_cb;
6562
6563 if ((ql2xenablemsix != 0) && IS_ATIO_MSIX_CAPABLE(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006564 struct qla_msix_entry *msix = &ha->msix_entries[2];
Arun Easiaa230bc2013-01-30 03:34:39 -05006565
6566 icb->msix_atio = cpu_to_le16(msix->entry);
6567 ql_dbg(ql_dbg_init, vha, 0xf072,
6568 "Registering ICB vector 0x%x for atio que.\n",
6569 msix->entry);
Himanshu Madhanic9558862017-10-13 09:34:04 -07006570 } else if (ql2xenablemsix == 0) {
6571 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6572 ql_dbg(ql_dbg_init, vha, 0xf07f,
6573 "Registering INTx vector for ATIO.\n");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006574 }
6575}
6576
6577void
6578qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6579{
6580 struct qla_hw_data *ha = vha->hw;
Quinn Tran99e1b682017-06-02 09:12:03 -07006581
6582 if (!QLA_TGT_MODE_ENABLED())
6583 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006584
Quinn Tranead03852017-01-19 22:28:01 -08006585 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006586 if (!ha->tgt.saved_set) {
6587 /* We save only once */
6588 ha->tgt.saved_exchange_count = nv->exchange_count;
6589 ha->tgt.saved_firmware_options_1 =
6590 nv->firmware_options_1;
6591 ha->tgt.saved_firmware_options_2 =
6592 nv->firmware_options_2;
6593 ha->tgt.saved_firmware_options_3 =
6594 nv->firmware_options_3;
6595 ha->tgt.saved_set = 1;
6596 }
6597
Quinn Tran99e1b682017-06-02 09:12:03 -07006598 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006599 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006600 else /* dual */
6601 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006602
6603 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006604 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006605
6606 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006607 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006608 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006609
6610 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006611 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006612 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006613 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006614 if (ql2xtgt_tape_enable)
6615 /* Enable FC Tape support */
6616 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6617 else
6618 /* Disable FC Tape support */
6619 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6620
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006621 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006622 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006623
6624 /*
6625 * clear BIT 15 explicitly as we have seen at least
6626 * a couple of instances where this was set and this
6627 * was causing the firmware to not be initialized.
6628 */
6629 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006630 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006631 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006632 } else {
6633 if (ha->tgt.saved_set) {
6634 nv->exchange_count = ha->tgt.saved_exchange_count;
6635 nv->firmware_options_1 =
6636 ha->tgt.saved_firmware_options_1;
6637 nv->firmware_options_2 =
6638 ha->tgt.saved_firmware_options_2;
6639 nv->firmware_options_3 =
6640 ha->tgt.saved_firmware_options_3;
6641 }
6642 return;
6643 }
6644
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006645 if (ha->base_qpair->enable_class_2) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006646 if (vha->flags.init_done)
6647 fc_host_supported_classes(vha->host) =
6648 FC_COS_CLASS2 | FC_COS_CLASS3;
6649
Bart Van Asschead950362015-07-09 07:24:08 -07006650 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006651 } else {
6652 if (vha->flags.init_done)
6653 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6654
Bart Van Asschead950362015-07-09 07:24:08 -07006655 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006656 }
6657}
6658
6659void
6660qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6661 struct init_cb_24xx *icb)
6662{
6663 struct qla_hw_data *ha = vha->hw;
6664
Quinn Tran481ce732015-12-17 14:57:08 -05006665 if (!QLA_TGT_MODE_ENABLED())
6666 return;
6667
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006668 if (ha->tgt.node_name_set) {
6669 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006670 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006671 }
Quinn Tran481ce732015-12-17 14:57:08 -05006672
6673 /* disable ZIO at start time. */
6674 if (!vha->flags.init_done) {
6675 uint32_t tmp;
6676 tmp = le32_to_cpu(icb->firmware_options_2);
6677 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6678 icb->firmware_options_2 = cpu_to_le32(tmp);
6679 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006680}
6681
Arun Easiaa230bc2013-01-30 03:34:39 -05006682void
6683qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6684{
6685 struct qla_hw_data *ha = vha->hw;
6686
6687 if (!QLA_TGT_MODE_ENABLED())
6688 return;
6689
Quinn Tranead03852017-01-19 22:28:01 -08006690 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006691 if (!ha->tgt.saved_set) {
6692 /* We save only once */
6693 ha->tgt.saved_exchange_count = nv->exchange_count;
6694 ha->tgt.saved_firmware_options_1 =
6695 nv->firmware_options_1;
6696 ha->tgt.saved_firmware_options_2 =
6697 nv->firmware_options_2;
6698 ha->tgt.saved_firmware_options_3 =
6699 nv->firmware_options_3;
6700 ha->tgt.saved_set = 1;
6701 }
6702
Quinn Tran99e1b682017-06-02 09:12:03 -07006703 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006704 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006705 else /* dual */
6706 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05006707
6708 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006709 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006710
6711 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006712 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006713 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006714 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006715 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006716 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006717 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006718 /*
6719 * clear BIT 15 explicitly as we have seen at
6720 * least a couple of instances where this was set
6721 * and this was causing the firmware to not be
6722 * initialized.
6723 */
6724 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006725 if (ql2xtgt_tape_enable)
6726 /* Enable FC tape support */
6727 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6728 else
6729 /* Disable FC tape support */
6730 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6731
Arun Easiaa230bc2013-01-30 03:34:39 -05006732 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006733 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006734 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006735 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006736 } else {
6737 if (ha->tgt.saved_set) {
6738 nv->exchange_count = ha->tgt.saved_exchange_count;
6739 nv->firmware_options_1 =
6740 ha->tgt.saved_firmware_options_1;
6741 nv->firmware_options_2 =
6742 ha->tgt.saved_firmware_options_2;
6743 nv->firmware_options_3 =
6744 ha->tgt.saved_firmware_options_3;
6745 }
6746 return;
6747 }
6748
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006749 if (ha->base_qpair->enable_class_2) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006750 if (vha->flags.init_done)
6751 fc_host_supported_classes(vha->host) =
6752 FC_COS_CLASS2 | FC_COS_CLASS3;
6753
Bart Van Asschead950362015-07-09 07:24:08 -07006754 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006755 } else {
6756 if (vha->flags.init_done)
6757 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6758
Bart Van Asschead950362015-07-09 07:24:08 -07006759 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006760 }
6761}
6762
6763void
6764qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6765 struct init_cb_81xx *icb)
6766{
6767 struct qla_hw_data *ha = vha->hw;
6768
6769 if (!QLA_TGT_MODE_ENABLED())
6770 return;
6771
6772 if (ha->tgt.node_name_set) {
6773 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006774 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006775 }
Quinn Tran481ce732015-12-17 14:57:08 -05006776
6777 /* disable ZIO at start time. */
6778 if (!vha->flags.init_done) {
6779 uint32_t tmp;
6780 tmp = le32_to_cpu(icb->firmware_options_2);
6781 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6782 icb->firmware_options_2 = cpu_to_le32(tmp);
6783 }
6784
Arun Easiaa230bc2013-01-30 03:34:39 -05006785}
6786
6787void
6788qlt_83xx_iospace_config(struct qla_hw_data *ha)
6789{
6790 if (!QLA_TGT_MODE_ENABLED())
6791 return;
6792
6793 ha->msix_count += 1; /* For ATIO Q */
6794}
6795
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006796
6797void
6798qlt_modify_vp_config(struct scsi_qla_host *vha,
6799 struct vp_config_entry_24xx *vpmod)
6800{
Quinn Tranead03852017-01-19 22:28:01 -08006801 /* enable target mode. Bit5 = 1 => disable */
6802 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006803 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006804
Quinn Tranead03852017-01-19 22:28:01 -08006805 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006806 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006807 vpmod->options_idx1 &= ~BIT_4;
6808}
6809
6810void
6811qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6812{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006813 int rc;
6814
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006815 if (!QLA_TGT_MODE_ENABLED())
6816 return;
6817
Himanshu Madhanic9558862017-10-13 09:34:04 -07006818 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006819 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6820 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6821 } else {
6822 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6823 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6824 }
6825
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006826 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6827 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006828
6829 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6830 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6831 qlt_unknown_atio_work_fn);
6832
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006833 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006834
6835 rc = btree_init32(&ha->tgt.host_map);
6836 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006837 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006838 "Unable to initialize ha->host_map btree\n");
6839
6840 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006841}
6842
Arun Easiaa230bc2013-01-30 03:34:39 -05006843irqreturn_t
6844qla83xx_msix_atio_q(int irq, void *dev_id)
6845{
6846 struct rsp_que *rsp;
6847 scsi_qla_host_t *vha;
6848 struct qla_hw_data *ha;
6849 unsigned long flags;
6850
6851 rsp = (struct rsp_que *) dev_id;
6852 ha = rsp->hw;
6853 vha = pci_get_drvdata(ha->pdev);
6854
Quinn Tran2f424b92015-12-17 14:57:07 -05006855 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006856
Quinn Tran2f424b92015-12-17 14:57:07 -05006857 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006858
Quinn Tran2f424b92015-12-17 14:57:07 -05006859 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006860
6861 return IRQ_HANDLED;
6862}
6863
Quinn Tran2f424b92015-12-17 14:57:07 -05006864static void
6865qlt_handle_abts_recv_work(struct work_struct *work)
6866{
6867 struct qla_tgt_sess_op *op = container_of(work,
6868 struct qla_tgt_sess_op, work);
6869 scsi_qla_host_t *vha = op->vha;
6870 struct qla_hw_data *ha = vha->hw;
6871 unsigned long flags;
6872
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006873 if (qla2x00_reset_active(vha) ||
6874 (op->chip_reset != ha->base_qpair->chip_reset))
Quinn Tran2f424b92015-12-17 14:57:07 -05006875 return;
6876
6877 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6878 qlt_24xx_process_atio_queue(vha, 0);
6879 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6880
6881 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran82de8022017-06-13 20:47:17 -07006882 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05006883 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006884
6885 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006886}
6887
6888void
Quinn Tran82de8022017-06-13 20:47:17 -07006889qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
6890 response_t *pkt)
Quinn Tran2f424b92015-12-17 14:57:07 -05006891{
6892 struct qla_tgt_sess_op *op;
6893
6894 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6895
6896 if (!op) {
6897 /* do not reach for ATIO queue here. This is best effort err
6898 * recovery at this point.
6899 */
Quinn Tran82de8022017-06-13 20:47:17 -07006900 qlt_response_pkt_all_vps(vha, rsp, pkt);
Quinn Tran2f424b92015-12-17 14:57:07 -05006901 return;
6902 }
6903
6904 memcpy(&op->atio, pkt, sizeof(*pkt));
6905 op->vha = vha;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006906 op->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran82de8022017-06-13 20:47:17 -07006907 op->rsp = rsp;
Quinn Tran2f424b92015-12-17 14:57:07 -05006908 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6909 queue_work(qla_tgt_wq, &op->work);
6910 return;
6911}
6912
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006913int
6914qlt_mem_alloc(struct qla_hw_data *ha)
6915{
6916 if (!QLA_TGT_MODE_ENABLED())
6917 return 0;
6918
6919 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6920 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6921 if (!ha->tgt.tgt_vp_map)
6922 return -ENOMEM;
6923
6924 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6925 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6926 &ha->tgt.atio_dma, GFP_KERNEL);
6927 if (!ha->tgt.atio_ring) {
6928 kfree(ha->tgt.tgt_vp_map);
6929 return -ENOMEM;
6930 }
6931 return 0;
6932}
6933
6934void
6935qlt_mem_free(struct qla_hw_data *ha)
6936{
6937 if (!QLA_TGT_MODE_ENABLED())
6938 return;
6939
6940 if (ha->tgt.atio_ring) {
6941 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6942 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6943 ha->tgt.atio_dma);
6944 }
6945 kfree(ha->tgt.tgt_vp_map);
6946}
6947
6948/* vport_slock to be held by the caller */
6949void
6950qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6951{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006952 void *slot;
6953 u32 key;
6954 int rc;
6955
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006956 if (!QLA_TGT_MODE_ENABLED())
6957 return;
6958
Quinn Tran482c9dc2017-03-15 09:48:54 -07006959 key = vha->d_id.b24;
6960
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006961 switch (cmd) {
6962 case SET_VP_IDX:
6963 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6964 break;
6965 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07006966 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6967 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006968 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006969 "Save vha in host_map %p %06x\n", vha, key);
6970 rc = btree_insert32(&vha->hw->tgt.host_map,
6971 key, vha, GFP_ATOMIC);
6972 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006973 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006974 "Unable to insert s_id into host_map: %06x\n",
6975 key);
6976 return;
6977 }
Quinn Tran83548fe2017-06-02 09:12:01 -07006978 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6979 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006980 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006981 break;
6982 case RESET_VP_IDX:
6983 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6984 break;
6985 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07006986 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006987 "clear vha in host_map %p %06x\n", vha, key);
6988 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6989 if (slot)
6990 btree_remove32(&vha->hw->tgt.host_map, key);
6991 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006992 break;
6993 }
6994}
6995
Quinn Tran482c9dc2017-03-15 09:48:54 -07006996void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6997{
6998 unsigned long flags;
6999 struct qla_hw_data *ha = vha->hw;
7000
7001 if (!vha->d_id.b24) {
7002 spin_lock_irqsave(&ha->vport_slock, flags);
7003 vha->d_id = id;
7004 qlt_update_vp_map(vha, SET_AL_PA);
7005 spin_unlock_irqrestore(&ha->vport_slock, flags);
7006 } else if (vha->d_id.b24 != id.b24) {
7007 spin_lock_irqsave(&ha->vport_slock, flags);
7008 qlt_update_vp_map(vha, RESET_AL_PA);
7009 vha->d_id = id;
7010 qlt_update_vp_map(vha, SET_AL_PA);
7011 spin_unlock_irqrestore(&ha->vport_slock, flags);
7012 }
7013}
7014
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007015static int __init qlt_parse_ini_mode(void)
7016{
7017 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7018 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7019 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7020 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7021 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7022 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08007023 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7024 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007025 else
7026 return false;
7027
7028 return true;
7029}
7030
7031int __init qlt_init(void)
7032{
7033 int ret;
7034
7035 if (!qlt_parse_ini_mode()) {
7036 ql_log(ql_log_fatal, NULL, 0xe06b,
7037 "qlt_parse_ini_mode() failed\n");
7038 return -EINVAL;
7039 }
7040
7041 if (!QLA_TGT_MODE_ENABLED())
7042 return 0;
7043
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007044 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7045 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7046 qla_tgt_mgmt_cmd), 0, NULL);
7047 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007048 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007049 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07007050 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007051 }
7052
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007053 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08007054 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7055 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007056
7057 if (!qla_tgt_plogi_cachep) {
7058 ql_log(ql_log_fatal, NULL, 0xe06d,
7059 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7060 ret = -ENOMEM;
7061 goto out_mgmt_cmd_cachep;
7062 }
7063
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007064 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7065 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7066 if (!qla_tgt_mgmt_cmd_mempool) {
7067 ql_log(ql_log_fatal, NULL, 0xe06e,
7068 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7069 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007070 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007071 }
7072
7073 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7074 if (!qla_tgt_wq) {
7075 ql_log(ql_log_fatal, NULL, 0xe06f,
7076 "alloc_workqueue for qla_tgt_wq failed\n");
7077 ret = -ENOMEM;
7078 goto out_cmd_mempool;
7079 }
7080 /*
7081 * Return 1 to signal that initiator-mode is being disabled
7082 */
7083 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7084
7085out_cmd_mempool:
7086 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007087out_plogi_cachep:
7088 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007089out_mgmt_cmd_cachep:
7090 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007091 return ret;
7092}
7093
7094void qlt_exit(void)
7095{
7096 if (!QLA_TGT_MODE_ENABLED())
7097 return;
7098
7099 destroy_workqueue(qla_tgt_wq);
7100 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05007101 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007102 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04007103}