blob: 4ad09584d4a8fb805d445896495955e169bcab03 [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 Tranead03852017-01-19 22:28:01 -080062static int ql_dm_tgt_ex_pct = 50;
63module_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
Arun Easiaa230bc2013-01-30 03:34:39 -050069int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040070
Quinn Tran33e79972014-09-25 06:14:55 -040071static int temp_sam_status = SAM_STAT_BUSY;
72
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040073/*
74 * From scsi/fc/fc_fcp.h
75 */
76enum fcp_resp_rsp_codes {
77 FCP_TMF_CMPL = 0,
78 FCP_DATA_LEN_INVALID = 1,
79 FCP_CMND_FIELDS_INVALID = 2,
80 FCP_DATA_PARAM_MISMATCH = 3,
81 FCP_TMF_REJECTED = 4,
82 FCP_TMF_FAILED = 5,
83 FCP_TMF_INVALID_LUN = 9,
84};
85
86/*
87 * fc_pri_ta from scsi/fc/fc_fcp.h
88 */
89#define FCP_PTA_SIMPLE 0 /* simple task attribute */
90#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
91#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +090092#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040093#define FCP_PTA_MASK 7 /* mask for task attribute field */
94#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
95#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
96
97/*
98 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
99 * must be called under HW lock and could unlock/lock it inside.
100 * It isn't an issue, since in the current implementation on the time when
101 * those functions are called:
102 *
103 * - Either context is IRQ and only IRQ handler can modify HW data,
104 * including rings related fields,
105 *
106 * - Or access to target mode variables from struct qla_tgt doesn't
107 * cross those functions boundaries, except tgt_stop, which
108 * additionally protected by irq_cmd_count.
109 */
110/* Predefs for callbacks handed to qla2xxx LLD */
111static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500112 struct atio_from_isp *pkt, uint8_t);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400113static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
Quinn Tran5d964832017-01-19 22:27:59 -0800114static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400115 int fn, void *iocb, int flags);
116static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
Quinn Trana07100e2015-12-07 19:48:57 -0500117 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
Arun Easib6a029e2014-09-25 06:14:52 -0400118static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
119 struct qla_tgt_cmd *cmd);
Quinn Tran33e79972014-09-25 06:14:55 -0400120static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
121 struct atio_from_isp *atio, uint16_t status, int qfull);
Joern Engel55a90662014-09-16 16:23:15 -0400122static void qlt_disable_vha(struct scsi_qla_host *vha);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400123static void qlt_clear_tgt_db(struct qla_tgt *tgt);
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400124static void qlt_send_notify_ack(struct scsi_qla_host *vha,
125 struct imm_ntfy_from_isp *ntfy,
126 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
127 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500128static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
129 struct imm_ntfy_from_isp *imm, int ha_locked);
Quinn Tran726b8542017-01-19 22:28:00 -0800130static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
131 fc_port_t *fcport, bool local);
132void qlt_unreg_sess(struct fc_port *sess);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700133static void qlt_24xx_handle_abts(struct scsi_qla_host *,
134 struct abts_recv_from_24xx *);
135
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400136/*
137 * Global Variables
138 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400139static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500140static struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400141static mempool_t *qla_tgt_mgmt_cmd_mempool;
142static struct workqueue_struct *qla_tgt_wq;
143static DEFINE_MUTEX(qla_tgt_mutex);
144static LIST_HEAD(qla_tgt_glist);
145
Quinn Tranbe251522017-03-15 09:48:49 -0700146static const char *prot_op_str(u32 prot_op)
147{
148 switch (prot_op) {
149 case TARGET_PROT_NORMAL: return "NORMAL";
150 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
151 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
152 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
153 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
154 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
155 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
156 default: return "UNKNOWN";
157 }
158}
159
Alexei Potashnikdf673272015-07-14 16:00:46 -0400160/* This API intentionally takes dest as a parameter, rather than returning
161 * int value to avoid caller forgetting to issue wmb() after the store */
162void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
163{
164 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
165 *dest = atomic_inc_return(&base_vha->generation_tick);
166 /* memory barrier */
167 wmb();
168}
169
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400170/* Might release hw lock, then reaquire!! */
171static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
172{
173 /* Send marker if required */
174 if (unlikely(vha->marker_needed != 0)) {
175 int rc = qla2x00_issue_marker(vha, vha_locked);
176 if (rc != QLA_SUCCESS) {
177 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
178 "qla_target(%d): issue_marker() failed\n",
179 vha->vp_idx);
180 }
181 return rc;
182 }
183 return QLA_SUCCESS;
184}
185
186static inline
187struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
188 uint8_t *d_id)
189{
Quinn Tran482c9dc2017-03-15 09:48:54 -0700190 struct scsi_qla_host *host;
191 uint32_t key = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400192
Quinn Tran482c9dc2017-03-15 09:48:54 -0700193 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
194 (vha->d_id.b.al_pa == d_id[2]))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400195 return vha;
196
Quinn Tran482c9dc2017-03-15 09:48:54 -0700197 key = (uint32_t)d_id[0] << 16;
198 key |= (uint32_t)d_id[1] << 8;
199 key |= (uint32_t)d_id[2];
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400200
Quinn Tran482c9dc2017-03-15 09:48:54 -0700201 host = btree_lookup32(&vha->hw->tgt.host_map, key);
202 if (!host)
Quinn Tran83548fe2017-06-02 09:12:01 -0700203 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
204 "Unable to find host %06x\n", key);
Quinn Tran482c9dc2017-03-15 09:48:54 -0700205
206 return host;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400207}
208
209static inline
210struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
211 uint16_t vp_idx)
212{
213 struct qla_hw_data *ha = vha->hw;
214
215 if (vha->vp_idx == vp_idx)
216 return vha;
217
218 BUG_ON(ha->tgt.tgt_vp_map == NULL);
219 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
220 return ha->tgt.tgt_vp_map[vp_idx].vha;
221
222 return NULL;
223}
224
Quinn Tran33e79972014-09-25 06:14:55 -0400225static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
226{
227 unsigned long flags;
228
229 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
230
231 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400232 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
233 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400234 vha->hw->tgt.num_pend_cmds;
235 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
236}
237static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
238{
239 unsigned long flags;
240
241 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
242 vha->hw->tgt.num_pend_cmds--;
243 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
244}
245
Quinn Tran41dc5292017-01-19 22:28:03 -0800246
247static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
248 struct atio_from_isp *atio, uint8_t ha_locked)
249{
250 struct qla_tgt_sess_op *u;
251 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
252 unsigned long flags;
253
254 if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700255 ql_dbg(ql_dbg_async, vha, 0x502c,
256 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
257 vha->vp_idx);
Quinn Tran41dc5292017-01-19 22:28:03 -0800258 goto out_term;
259 }
260
261 u = kzalloc(sizeof(*u), GFP_ATOMIC);
Quinn Tran83548fe2017-06-02 09:12:01 -0700262 if (u == NULL)
Quinn Tran41dc5292017-01-19 22:28:03 -0800263 goto out_term;
Quinn Tran41dc5292017-01-19 22:28:03 -0800264
265 u->vha = vha;
266 memcpy(&u->atio, atio, sizeof(*atio));
267 INIT_LIST_HEAD(&u->cmd_list);
268
269 spin_lock_irqsave(&vha->cmd_list_lock, flags);
270 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
271 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
272
273 schedule_delayed_work(&vha->unknown_atio_work, 1);
274
275out:
276 return;
277
278out_term:
279 qlt_send_term_exchange(vha, NULL, atio, ha_locked, 0);
280 goto out;
281}
282
283static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
284 uint8_t ha_locked)
285{
286 struct qla_tgt_sess_op *u, *t;
287 scsi_qla_host_t *host;
288 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
289 unsigned long flags;
290 uint8_t queued = 0;
291
292 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
293 if (u->aborted) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700294 ql_dbg(ql_dbg_async, vha, 0x502e,
295 "Freeing unknown %s %p, because of Abort\n",
Quinn Tran41dc5292017-01-19 22:28:03 -0800296 "ATIO_TYPE7", u);
297 qlt_send_term_exchange(vha, NULL, &u->atio,
298 ha_locked, 0);
299 goto abort;
300 }
301
302 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
303 if (host != NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700304 ql_dbg(ql_dbg_async, vha, 0x502f,
305 "Requeuing unknown ATIO_TYPE7 %p\n", u);
Quinn Tran41dc5292017-01-19 22:28:03 -0800306 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
307 } else if (tgt->tgt_stop) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700308 ql_dbg(ql_dbg_async, vha, 0x503a,
309 "Freeing unknown %s %p, because tgt is being stopped\n",
310 "ATIO_TYPE7", u);
Quinn Tran41dc5292017-01-19 22:28:03 -0800311 qlt_send_term_exchange(vha, NULL, &u->atio,
312 ha_locked, 0);
313 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700314 ql_dbg(ql_dbg_async, vha, 0x503d,
315 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
Quinn Tran41dc5292017-01-19 22:28:03 -0800316 if (!queued) {
317 queued = 1;
318 schedule_delayed_work(&vha->unknown_atio_work,
319 1);
320 }
321 continue;
322 }
323
324abort:
325 spin_lock_irqsave(&vha->cmd_list_lock, flags);
326 list_del(&u->cmd_list);
327 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
328 kfree(u);
329 }
330}
331
332void qlt_unknown_atio_work_fn(struct work_struct *work)
333{
334 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
335 struct scsi_qla_host, unknown_atio_work);
336
337 qlt_try_to_dequeue_unknown_atios(vha, 0);
338}
339
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500340static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500341 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400342{
Quinn Tranf83adb62014-04-11 16:54:43 -0400343 ql_dbg(ql_dbg_tgt, vha, 0xe072,
344 "%s: qla_target(%d): type %x ox_id %04x\n",
345 __func__, vha->vp_idx, atio->u.raw.entry_type,
346 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
347
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400348 switch (atio->u.raw.entry_type) {
349 case ATIO_TYPE7:
350 {
351 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
352 atio->u.isp24.fcp_hdr.d_id);
353 if (unlikely(NULL == host)) {
354 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
355 "qla_target(%d): Received ATIO_TYPE7 "
356 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
357 atio->u.isp24.fcp_hdr.d_id[0],
358 atio->u.isp24.fcp_hdr.d_id[1],
359 atio->u.isp24.fcp_hdr.d_id[2]);
Quinn Tran41dc5292017-01-19 22:28:03 -0800360
361
362 qlt_queue_unknown_atio(vha, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400363 break;
364 }
Quinn Tran41dc5292017-01-19 22:28:03 -0800365 if (unlikely(!list_empty(&vha->unknown_atio_list)))
366 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
367
Quinn Tran2f424b92015-12-17 14:57:07 -0500368 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400369 break;
370 }
371
372 case IMMED_NOTIFY_TYPE:
373 {
374 struct scsi_qla_host *host = vha;
375 struct imm_ntfy_from_isp *entry =
376 (struct imm_ntfy_from_isp *)atio;
377
378 if ((entry->u.isp24.vp_index != 0xFF) &&
379 (entry->u.isp24.nport_handle != 0xFFFF)) {
380 host = qlt_find_host_by_vp_idx(vha,
381 entry->u.isp24.vp_index);
382 if (unlikely(!host)) {
383 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
384 "qla_target(%d): Received "
385 "ATIO (IMMED_NOTIFY_TYPE) "
386 "with unknown vp_index %d\n",
387 vha->vp_idx, entry->u.isp24.vp_index);
388 break;
389 }
390 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500391 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400392 break;
393 }
394
Quinn Tran41dc5292017-01-19 22:28:03 -0800395 case VP_RPT_ID_IOCB_TYPE:
396 qla24xx_report_id_acquisition(vha,
397 (struct vp_rpt_id_entry_24xx *)atio);
398 break;
399
400 case ABTS_RECV_24XX:
401 {
402 struct abts_recv_from_24xx *entry =
403 (struct abts_recv_from_24xx *)atio;
404 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
405 entry->vp_index);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700406 unsigned long flags;
407
Quinn Tran41dc5292017-01-19 22:28:03 -0800408 if (unlikely(!host)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700409 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
Quinn Tran41dc5292017-01-19 22:28:03 -0800410 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
411 "received, with unknown vp_index %d\n",
412 vha->vp_idx, entry->vp_index);
413 break;
414 }
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700415 if (!ha_locked)
416 spin_lock_irqsave(&host->hw->hardware_lock, flags);
417 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
418 if (!ha_locked)
419 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -0800420 break;
Quinn Tran41dc5292017-01-19 22:28:03 -0800421 }
422
423 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
424
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400425 default:
426 ql_dbg(ql_dbg_tgt, vha, 0xe040,
427 "qla_target(%d): Received unknown ATIO atio "
428 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
429 break;
430 }
431
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500432 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400433}
434
435void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
436{
437 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400438 case CTIO_CRC2:
439 ql_dbg(ql_dbg_tgt, vha, 0xe073,
440 "qla_target(%d):%s: CRC2 Response pkt\n",
441 vha->vp_idx, __func__);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400442 case CTIO_TYPE7:
443 {
444 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
445 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
446 entry->vp_index);
447 if (unlikely(!host)) {
448 ql_dbg(ql_dbg_tgt, vha, 0xe041,
449 "qla_target(%d): Response pkt (CTIO_TYPE7) "
450 "received, with unknown vp_index %d\n",
451 vha->vp_idx, entry->vp_index);
452 break;
453 }
454 qlt_response_pkt(host, pkt);
455 break;
456 }
457
458 case IMMED_NOTIFY_TYPE:
459 {
460 struct scsi_qla_host *host = vha;
461 struct imm_ntfy_from_isp *entry =
462 (struct imm_ntfy_from_isp *)pkt;
463
464 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
465 if (unlikely(!host)) {
466 ql_dbg(ql_dbg_tgt, vha, 0xe042,
467 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
468 "received, with unknown vp_index %d\n",
469 vha->vp_idx, entry->u.isp24.vp_index);
470 break;
471 }
472 qlt_response_pkt(host, pkt);
473 break;
474 }
475
476 case NOTIFY_ACK_TYPE:
477 {
478 struct scsi_qla_host *host = vha;
479 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
480
481 if (0xFF != entry->u.isp24.vp_index) {
482 host = qlt_find_host_by_vp_idx(vha,
483 entry->u.isp24.vp_index);
484 if (unlikely(!host)) {
485 ql_dbg(ql_dbg_tgt, vha, 0xe043,
486 "qla_target(%d): Response "
487 "pkt (NOTIFY_ACK_TYPE) "
488 "received, with unknown "
489 "vp_index %d\n", vha->vp_idx,
490 entry->u.isp24.vp_index);
491 break;
492 }
493 }
494 qlt_response_pkt(host, pkt);
495 break;
496 }
497
498 case ABTS_RECV_24XX:
499 {
500 struct abts_recv_from_24xx *entry =
501 (struct abts_recv_from_24xx *)pkt;
502 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
503 entry->vp_index);
504 if (unlikely(!host)) {
505 ql_dbg(ql_dbg_tgt, vha, 0xe044,
506 "qla_target(%d): Response pkt "
507 "(ABTS_RECV_24XX) received, with unknown "
508 "vp_index %d\n", vha->vp_idx, entry->vp_index);
509 break;
510 }
511 qlt_response_pkt(host, pkt);
512 break;
513 }
514
515 case ABTS_RESP_24XX:
516 {
517 struct abts_resp_to_24xx *entry =
518 (struct abts_resp_to_24xx *)pkt;
519 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
520 entry->vp_index);
521 if (unlikely(!host)) {
522 ql_dbg(ql_dbg_tgt, vha, 0xe045,
523 "qla_target(%d): Response pkt "
524 "(ABTS_RECV_24XX) received, with unknown "
525 "vp_index %d\n", vha->vp_idx, entry->vp_index);
526 break;
527 }
528 qlt_response_pkt(host, pkt);
529 break;
530 }
531
532 default:
533 qlt_response_pkt(vha, pkt);
534 break;
535 }
536
537}
538
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500539/*
540 * All qlt_plogi_ack_t operations are protected by hardware_lock
541 */
Quinn Tran726b8542017-01-19 22:28:00 -0800542static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
543 struct imm_ntfy_from_isp *ntfy, int type)
544{
545 struct qla_work_evt *e;
546 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
547 if (!e)
548 return QLA_FUNCTION_FAILED;
549
550 e->u.nack.fcport = fcport;
551 e->u.nack.type = type;
552 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
553 return qla2x00_post_work(vha, e);
554}
555
556static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800557void qla2x00_async_nack_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800558{
Quinn Tran726b8542017-01-19 22:28:00 -0800559 struct srb *sp = (struct srb *)s;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800560 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800561 unsigned long flags;
562
Quinn Tran83548fe2017-06-02 09:12:01 -0700563 ql_dbg(ql_dbg_disc, vha, 0x20f2,
564 "Async done-%s res %x %8phC type %d\n",
565 sp->name, res, sp->fcport->port_name, sp->type);
Quinn Tran726b8542017-01-19 22:28:00 -0800566
567 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
568 sp->fcport->flags &= ~FCF_ASYNC_SENT;
569 sp->fcport->chip_reset = vha->hw->chip_reset;
570
571 switch (sp->type) {
572 case SRB_NACK_PLOGI:
573 sp->fcport->login_gen++;
574 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
575 sp->fcport->logout_on_delete = 1;
Quinn Tran5b334692017-03-15 09:48:48 -0700576 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
Quinn Tran726b8542017-01-19 22:28:00 -0800577 break;
578
579 case SRB_NACK_PRLI:
580 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
581 sp->fcport->deleted = 0;
582
583 if (!sp->fcport->login_succ &&
584 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
585 sp->fcport->login_succ = 1;
586
587 vha->fcport_count++;
588
589 if (!IS_IIDMA_CAPABLE(vha->hw) ||
590 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700591 ql_dbg(ql_dbg_disc, vha, 0x20f3,
592 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
593 __func__, __LINE__,
594 sp->fcport->port_name,
595 vha->fcport_count);
Quinn Tran726b8542017-01-19 22:28:00 -0800596
597 qla24xx_post_upd_fcport_work(vha, sp->fcport);
598 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700599 ql_dbg(ql_dbg_disc, vha, 0x20f5,
600 "%s %d %8phC post gpsc fcp_cnt %d\n",
601 __func__, __LINE__,
602 sp->fcport->port_name,
603 vha->fcport_count);
Quinn Tran726b8542017-01-19 22:28:00 -0800604
605 qla24xx_post_gpsc_work(vha, sp->fcport);
606 }
607 }
608 break;
609
610 case SRB_NACK_LOGO:
611 sp->fcport->login_gen++;
612 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
613 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
614 break;
615 }
616 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
617
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800618 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800619}
620
621int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
622 struct imm_ntfy_from_isp *ntfy, int type)
623{
624 int rval = QLA_FUNCTION_FAILED;
625 srb_t *sp;
626 char *c = NULL;
627
628 fcport->flags |= FCF_ASYNC_SENT;
629 switch (type) {
630 case SRB_NACK_PLOGI:
631 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
632 c = "PLOGI";
633 break;
634 case SRB_NACK_PRLI:
635 fcport->fw_login_state = DSC_LS_PRLI_PEND;
Quinn Tranec7193e2017-03-15 09:48:55 -0700636 fcport->deleted = 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800637 c = "PRLI";
638 break;
639 case SRB_NACK_LOGO:
640 fcport->fw_login_state = DSC_LS_LOGO_PEND;
641 c = "LOGO";
642 break;
643 }
644
645 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
646 if (!sp)
647 goto done;
648
649 sp->type = type;
650 sp->name = "nack";
651
652 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
653
654 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
655
656 sp->done = qla2x00_async_nack_sp_done;
657
658 rval = qla2x00_start_sp(sp);
659 if (rval != QLA_SUCCESS)
660 goto done_free_sp;
661
Quinn Tran83548fe2017-06-02 09:12:01 -0700662 ql_dbg(ql_dbg_disc, vha, 0x20f4,
663 "Async-%s %8phC hndl %x %s\n",
664 sp->name, fcport->port_name, sp->handle, c);
Quinn Tran726b8542017-01-19 22:28:00 -0800665
666 return rval;
667
668done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800669 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800670done:
671 fcport->flags &= ~FCF_ASYNC_SENT;
672 return rval;
673}
674
675void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
676{
677 fc_port_t *t;
678 unsigned long flags;
679
680 switch (e->u.nack.type) {
681 case SRB_NACK_PRLI:
682 mutex_lock(&vha->vha_tgt.tgt_mutex);
683 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
684 mutex_unlock(&vha->vha_tgt.tgt_mutex);
685 if (t) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700686 ql_log(ql_log_info, vha, 0xd034,
Quinn Tran726b8542017-01-19 22:28:00 -0800687 "%s create sess success %p", __func__, t);
688 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
689 /* create sess has an extra kref */
690 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
691 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
692 }
693 break;
694 }
695 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
696 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
697}
698
699void qla24xx_delete_sess_fn(struct work_struct *work)
700{
701 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
702 struct qla_hw_data *ha = fcport->vha->hw;
703 unsigned long flags;
704
705 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
706
707 if (fcport->se_sess) {
708 ha->tgt.tgt_ops->shutdown_sess(fcport);
709 ha->tgt.tgt_ops->put_sess(fcport);
710 } else {
711 qlt_unreg_sess(fcport);
712 }
713 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
714}
715
716/*
717 * Called from qla2x00_reg_remote_port()
718 */
719void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
720{
721 struct qla_hw_data *ha = vha->hw;
722 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
723 struct fc_port *sess = fcport;
724 unsigned long flags;
725
726 if (!vha->hw->tgt.tgt_ops)
727 return;
728
729 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
730 if (tgt->tgt_stop) {
731 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
732 return;
733 }
734
735 if (fcport->disc_state == DSC_DELETE_PEND) {
736 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
737 return;
738 }
739
740 if (!sess->se_sess) {
741 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
742
743 mutex_lock(&vha->vha_tgt.tgt_mutex);
744 sess = qlt_create_sess(vha, fcport, false);
745 mutex_unlock(&vha->vha_tgt.tgt_mutex);
746
747 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
748 } else {
749 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
750 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
751 return;
752 }
753
754 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700755 ql_dbg(ql_dbg_disc, vha, 0x2107,
Quinn Tran726b8542017-01-19 22:28:00 -0800756 "%s: kref_get fail sess %8phC \n",
757 __func__, sess->port_name);
758 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
759 return;
760 }
761
762 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
763 "qla_target(%u): %ssession for port %8phC "
764 "(loop ID %d) reappeared\n", vha->vp_idx,
765 sess->local ? "local " : "", sess->port_name, sess->loop_id);
766
767 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
768 "Reappeared sess %p\n", sess);
769
770 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
771 fcport->loop_id,
772 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
773 }
774
775 if (sess && sess->local) {
776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
777 "qla_target(%u): local session for "
778 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
779 fcport->port_name, sess->loop_id);
780 sess->local = 0;
781 }
782 ha->tgt.tgt_ops->put_sess(sess);
783 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
784}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500785
786/*
787 * This is a zero-base ref-counting solution, since hardware_lock
788 * guarantees that ref_count is not modified concurrently.
789 * Upon successful return content of iocb is undefined
790 */
Quinn Tran5d964832017-01-19 22:27:59 -0800791static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500792qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
793 struct imm_ntfy_from_isp *iocb)
794{
Quinn Tran5d964832017-01-19 22:27:59 -0800795 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500796
797 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
798 if (pla->id.b24 == id->b24) {
799 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800800 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500801 return pla;
802 }
803 }
804
805 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
806 if (!pla) {
807 ql_dbg(ql_dbg_async, vha, 0x5088,
808 "qla_target(%d): Allocation of plogi_ack failed\n",
809 vha->vp_idx);
810 return NULL;
811 }
812
Quinn Tran5d964832017-01-19 22:27:59 -0800813 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500814 pla->id = *id;
815 list_add_tail(&pla->list, &vha->plogi_ack_list);
816
817 return pla;
818}
819
Quinn Tran726b8542017-01-19 22:28:00 -0800820void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800821 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500822{
Quinn Tran5d964832017-01-19 22:27:59 -0800823 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800824 port_id_t port_id;
825 uint16_t loop_id;
826 fc_port_t *fcport = pla->fcport;
827
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500828 BUG_ON(!pla->ref_count);
829 pla->ref_count--;
830
831 if (pla->ref_count)
832 return;
833
Quinn Tran726b8542017-01-19 22:28:00 -0800834 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500835 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800836 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
837 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
838 iocb->u.isp24.port_id[0],
839 le16_to_cpu(iocb->u.isp24.nport_handle),
840 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800841
842 port_id.b.domain = iocb->u.isp24.port_id[2];
843 port_id.b.area = iocb->u.isp24.port_id[1];
844 port_id.b.al_pa = iocb->u.isp24.port_id[0];
845 port_id.b.rsvd_1 = 0;
846
847 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
848
849 fcport->loop_id = loop_id;
850 fcport->d_id = port_id;
851 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
852
853 list_for_each_entry(fcport, &vha->vp_fcports, list) {
854 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
855 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
856 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
857 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
858 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500859
860 list_del(&pla->list);
861 kmem_cache_free(qla_tgt_plogi_cachep, pla);
862}
863
Quinn Tran726b8542017-01-19 22:28:00 -0800864void
Quinn Tran5d964832017-01-19 22:27:59 -0800865qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
866 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500867{
Quinn Tran5d964832017-01-19 22:27:59 -0800868 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500869 /* Inc ref_count first because link might already be pointing at pla */
870 pla->ref_count++;
871
Quinn Tran726b8542017-01-19 22:28:00 -0800872 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
873 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
874 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
875 sess, link, sess->port_name,
876 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
877 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
878 pla->ref_count, pla, link);
879
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500880 if (sess->plogi_link[link])
881 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
882
Quinn Tran726b8542017-01-19 22:28:00 -0800883 if (link == QLT_PLOGI_LINK_SAME_WWN)
884 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500885
886 sess->plogi_link[link] = pla;
887}
888
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500889typedef struct {
890 /* These fields must be initialized by the caller */
891 port_id_t id;
892 /*
893 * number of cmds dropped while we were waiting for
894 * initiator to ack LOGO initialize to 1 if LOGO is
895 * triggered by a command, otherwise, to 0
896 */
897 int cmd_count;
898
899 /* These fields are used by callee */
900 struct list_head list;
901} qlt_port_logo_t;
902
903static void
904qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
905{
906 qlt_port_logo_t *tmp;
907 int res;
908
909 mutex_lock(&vha->vha_tgt.tgt_mutex);
910
911 list_for_each_entry(tmp, &vha->logo_list, list) {
912 if (tmp->id.b24 == logo->id.b24) {
913 tmp->cmd_count += logo->cmd_count;
914 mutex_unlock(&vha->vha_tgt.tgt_mutex);
915 return;
916 }
917 }
918
919 list_add_tail(&logo->list, &vha->logo_list);
920
921 mutex_unlock(&vha->vha_tgt.tgt_mutex);
922
923 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
924
925 mutex_lock(&vha->vha_tgt.tgt_mutex);
926 list_del(&logo->list);
927 mutex_unlock(&vha->vha_tgt.tgt_mutex);
928
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500929 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
930 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
931 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
932 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500933}
934
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400935static void qlt_free_session_done(struct work_struct *work)
936{
Quinn Tran5d964832017-01-19 22:27:59 -0800937 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400938 free_work);
939 struct qla_tgt *tgt = sess->tgt;
940 struct scsi_qla_host *vha = sess->vha;
941 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400942 unsigned long flags;
943 bool logout_started = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800944 struct event_arg ea;
Quinn Tran41dc5292017-01-19 22:28:03 -0800945 scsi_qla_host_t *base_vha;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400946
947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
948 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500949 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400950 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800951 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400952 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500953 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400954
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400955
Quinn Tran726b8542017-01-19 22:28:00 -0800956 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800957 if (sess->send_els_logo) {
958 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500959
Quinn Tran41dc5292017-01-19 22:28:03 -0800960 logo.id = sess->d_id;
961 logo.cmd_count = 0;
962 qlt_send_first_logo(vha, &logo);
963 }
964
965 if (sess->logout_on_delete) {
966 int rc;
967
968 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
969 if (rc != QLA_SUCCESS)
970 ql_log(ql_log_warn, vha, 0xf085,
971 "Schedule logo failed sess %p rc %d\n",
972 sess, rc);
973 else
974 logout_started = true;
975 }
Quinn Tran726b8542017-01-19 22:28:00 -0800976 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400977
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400978 /*
979 * Release the target session for FC Nexus from fabric module code.
980 */
981 if (sess->se_sess != NULL)
982 ha->tgt.tgt_ops->free_session(sess);
983
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400984 if (logout_started) {
985 bool traced = false;
986
987 while (!ACCESS_ONCE(sess->logout_completed)) {
988 if (!traced) {
989 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
990 "%s: waiting for sess %p logout\n",
991 __func__, sess);
992 traced = true;
993 }
994 msleep(100);
995 }
996
Quinn Tran726b8542017-01-19 22:28:00 -0800997 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran41dc5292017-01-19 22:28:03 -0800998 "%s: sess %p logout completed\n",__func__, sess);
999 }
1000
1001 if (sess->logo_ack_needed) {
1002 sess->logo_ack_needed = 0;
1003 qla24xx_async_notify_ack(vha, sess,
1004 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001005 }
1006
Quinn Tran726b8542017-01-19 22:28:00 -08001007 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1008 if (sess->se_sess) {
1009 sess->se_sess = NULL;
1010 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1011 tgt->sess_count--;
1012 }
1013
1014 sess->disc_state = DSC_DELETED;
1015 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1016 sess->deleted = QLA_SESS_DELETED;
1017 sess->login_retry = vha->hw->login_retry_count;
1018
1019 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1020 vha->fcport_count--;
1021 sess->login_succ = 0;
1022 }
1023
1024 if (sess->chip_reset != sess->vha->hw->chip_reset)
1025 qla2x00_clear_loop_id(sess);
1026
1027 if (sess->conflict) {
1028 sess->conflict->login_pause = 0;
1029 sess->conflict = NULL;
1030 if (!test_bit(UNLOADING, &vha->dpc_flags))
1031 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1032 }
1033
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001034 {
Quinn Tran5d964832017-01-19 22:27:59 -08001035 struct qlt_plogi_ack_t *own =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001036 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Quinn Tran5d964832017-01-19 22:27:59 -08001037 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001038 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001039 struct imm_ntfy_from_isp *iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001040
1041 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001042 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001043 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001044 "se_sess %p / sess %p port %8phC is gone,"
1045 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1046 sess->se_sess, sess, sess->port_name,
1047 own ? "releasing own PLOGI" : "no own PLOGI pending",
1048 own ? own->ref_count : -1,
1049 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001050 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001051 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001052 } else {
1053 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1054 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1055 sess->se_sess, sess, sess->port_name,
1056 own ? "releasing own PLOGI" :
1057 "no own PLOGI pending",
1058 own ? own->ref_count : -1);
1059 }
1060
Quinn Tran726b8542017-01-19 22:28:00 -08001061 if (own) {
1062 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001063 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001064 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1065 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001066 }
Quinn Tran5d964832017-01-19 22:27:59 -08001067 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1068
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001069 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001070 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1071 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001072
Quinn Tran726b8542017-01-19 22:28:00 -08001073 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001074 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001075
1076 if (vha->fcport_count == 0)
1077 wake_up_all(&vha->fcport_waitQ);
1078
Quinn Tran41dc5292017-01-19 22:28:03 -08001079 base_vha = pci_get_drvdata(ha->pdev);
1080 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1081 return;
1082
Quinn Tran726b8542017-01-19 22:28:00 -08001083 if (!tgt || !tgt->tgt_stop) {
1084 memset(&ea, 0, sizeof(ea));
1085 ea.event = FCME_DELETE_DONE;
1086 ea.fcport = sess;
1087 qla2x00_fcport_event_handler(vha, &ea);
1088 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001089}
1090
Quinn Tran75601512015-12-17 14:57:04 -05001091/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001092void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001093{
1094 struct scsi_qla_host *vha = sess->vha;
1095
Quinn Tran83548fe2017-06-02 09:12:01 -07001096 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
Quinn Tran5d964832017-01-19 22:27:59 -08001097 "%s sess %p for deletion %8phC\n",
1098 __func__, sess, sess->port_name);
1099
Quinn Tran36c78452016-02-04 11:45:18 -05001100 if (sess->se_sess)
1101 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001102
Quinn Tran5d964832017-01-19 22:27:59 -08001103 qla2x00_mark_device_lost(vha, sess, 1, 1);
1104
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001105 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001106 sess->disc_state = DSC_DELETE_PEND;
1107 sess->last_rscn_gen = sess->rscn_gen;
1108 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001109
1110 INIT_WORK(&sess->free_work, qlt_free_session_done);
1111 schedule_work(&sess->free_work);
1112}
Quinn Tran5d964832017-01-19 22:27:59 -08001113EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001114
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001115static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1116{
1117 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001118 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001119 uint16_t loop_id;
1120 int res = 0;
1121 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001122 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001123
1124 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1125 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001126 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001127 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001128 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001129 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001130 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001131 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001132 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001133 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001134 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001135 }
1136
1137 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1138 "Using sess for qla_tgt_reset: %p\n", sess);
1139 if (!sess) {
1140 res = -ESRCH;
1141 return res;
1142 }
1143
1144 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001145 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1146 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001147 mcmd, loop_id);
1148
Quinn Tranbb1181c2016-12-23 18:06:05 -08001149 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001150}
1151
Quinn Tran726b8542017-01-19 22:28:00 -08001152static void qla24xx_chk_fcp_state(struct fc_port *sess)
1153{
1154 if (sess->chip_reset != sess->vha->hw->chip_reset) {
1155 sess->logout_on_delete = 0;
1156 sess->logo_ack_needed = 0;
1157 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1158 sess->scan_state = 0;
1159 }
1160}
1161
Quinn Tran75601512015-12-17 14:57:04 -05001162/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran726b8542017-01-19 22:28:00 -08001163void qlt_schedule_sess_for_deletion(struct fc_port *sess,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001164 bool immediate)
1165{
1166 struct qla_tgt *tgt = sess->tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001167
Quinn Tran726b8542017-01-19 22:28:00 -08001168 if (sess->disc_state == DSC_DELETE_PEND)
1169 return;
1170
1171 if (sess->disc_state == DSC_DELETED) {
1172 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1173 wake_up_all(&tgt->waitQ);
1174 if (sess->vha->fcport_count == 0)
1175 wake_up_all(&sess->vha->fcport_waitQ);
1176
1177 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1178 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001179 return;
1180 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001181
Quinn Tran726b8542017-01-19 22:28:00 -08001182 sess->disc_state = DSC_DELETE_PEND;
1183
1184 if (sess->deleted == QLA_SESS_DELETED)
1185 sess->logout_on_delete = 0;
1186
1187 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1188 qla24xx_chk_fcp_state(sess);
1189
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001190 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1191 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001192
Quinn Tran726b8542017-01-19 22:28:00 -08001193 schedule_work(&sess->del_work);
1194}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001195
Quinn Tran726b8542017-01-19 22:28:00 -08001196void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1197{
1198 unsigned long flags;
1199 struct qla_hw_data *ha = sess->vha->hw;
1200 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1201 qlt_schedule_sess_for_deletion(sess, 1);
1202 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001203}
1204
Quinn Tran75601512015-12-17 14:57:04 -05001205/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -04001206static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001207{
Quinn Tran5d964832017-01-19 22:27:59 -08001208 struct fc_port *sess;
1209 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001210
Quinn Tran5d964832017-01-19 22:27:59 -08001211 list_for_each_entry(sess, &vha->vp_fcports, list) {
1212 if (sess->se_sess)
Quinn Tran726b8542017-01-19 22:28:00 -08001213 qlt_schedule_sess_for_deletion(sess, 1);
Quinn Tran5d964832017-01-19 22:27:59 -08001214 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001215
1216 /* At this point tgt could be already dead */
1217}
1218
1219static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1220 uint16_t *loop_id)
1221{
1222 struct qla_hw_data *ha = vha->hw;
1223 dma_addr_t gid_list_dma;
1224 struct gid_list_info *gid_list;
1225 char *id_iter;
1226 int res, rc, i;
1227 uint16_t entries;
1228
1229 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1230 &gid_list_dma, GFP_KERNEL);
1231 if (!gid_list) {
1232 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1233 "qla_target(%d): DMA Alloc failed of %u\n",
1234 vha->vp_idx, qla2x00_gid_list_size(ha));
1235 return -ENOMEM;
1236 }
1237
1238 /* Get list of logged in devices */
Quinn Tran15f30a52017-03-15 09:48:52 -07001239 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001240 if (rc != QLA_SUCCESS) {
1241 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1242 "qla_target(%d): get_id_list() failed: %x\n",
1243 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001244 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001245 goto out_free_id_list;
1246 }
1247
1248 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001249 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001250 for (i = 0; i < entries; i++) {
1251 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1252 if ((gid->al_pa == s_id[2]) &&
1253 (gid->area == s_id[1]) &&
1254 (gid->domain == s_id[0])) {
1255 *loop_id = le16_to_cpu(gid->loop_id);
1256 res = 0;
1257 break;
1258 }
1259 id_iter += ha->gid_list_info_size;
1260 }
1261
1262out_free_id_list:
1263 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1264 gid_list, gid_list_dma);
1265 return res;
1266}
1267
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001268/*
1269 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1270 * Caller must put it.
1271 */
Quinn Tran5d964832017-01-19 22:27:59 -08001272static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001273 struct scsi_qla_host *vha,
1274 fc_port_t *fcport,
1275 bool local)
1276{
1277 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001278 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001279 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001280
Quinn Tran726b8542017-01-19 22:28:00 -08001281 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001282 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001283
1284 if (fcport->se_sess) {
1285 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001286 ql_dbg(ql_dbg_disc, vha, 0x20f6,
Quinn Tran726b8542017-01-19 22:28:00 -08001287 "%s: kref_get_unless_zero failed for %8phC\n",
1288 __func__, sess->port_name);
1289 return NULL;
1290 }
1291 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001292 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001293 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001294 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001295
Quinn Tran726b8542017-01-19 22:28:00 -08001296 /*
1297 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001298 * session eventually ends and release corresponding nport handle.
1299 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001300 * code will adjust these flags as necessary.
1301 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001302 sess->logout_on_delete = 1;
1303 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001304 sess->logout_completed = 0;
1305
1306 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1307 &fcport->port_name[0], sess) < 0) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001308 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
Quinn Tran726b8542017-01-19 22:28:00 -08001309 "(%d) %8phC check_initiator_node_acl failed\n",
1310 vha->vp_idx, fcport->port_name);
1311 return NULL;
1312 } else {
1313 kref_init(&fcport->sess_kref);
1314 /*
1315 * Take an extra reference to ->sess_kref here to handle
1316 * fc_port access across ->tgt.sess_lock reaquire.
1317 */
1318 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001319 ql_dbg(ql_dbg_disc, vha, 0x20f7,
Quinn Tran726b8542017-01-19 22:28:00 -08001320 "%s: kref_get_unless_zero failed for %8phC\n",
1321 __func__, sess->port_name);
1322 return NULL;
1323 }
1324
1325 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1326 if (!IS_SW_RESV_ADDR(sess->d_id))
1327 vha->vha_tgt.qla_tgt->sess_count++;
1328
1329 qlt_do_generation_tick(vha, &sess->generation);
1330 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1331 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001332
1333 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001334 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1335 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1336 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001337
1338 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001339 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1340 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1341 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001342 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1343 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001344
1345 return sess;
1346}
1347
1348/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001349 * max_gen - specifies maximum session generation
1350 * at which this deletion requestion is still valid
1351 */
1352void
1353qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001354{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001355 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001356 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001357 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001358
1359 if (!vha->hw->tgt.tgt_ops)
1360 return;
1361
Roland Dreierb2032fd2015-07-14 16:00:42 -04001362 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001363 return;
1364
Quinn Tran75601512015-12-17 14:57:04 -05001365 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001366 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001367 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001368 return;
1369 }
Quinn Tran5d964832017-01-19 22:27:59 -08001370 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001371 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001372 return;
1373 }
1374
Alexei Potashnikdf673272015-07-14 16:00:46 -04001375 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001376 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001377 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1378 "Ignoring stale deletion request for se_sess %p / sess %p"
1379 " for port %8phC, req_gen %d, sess_gen %d\n",
1380 sess->se_sess, sess, sess->port_name, max_gen,
1381 sess->generation);
1382 return;
1383 }
1384
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001385 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1386
1387 sess->local = 1;
1388 qlt_schedule_sess_for_deletion(sess, false);
Quinn Tran75601512015-12-17 14:57:04 -05001389 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001390}
1391
1392static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1393{
1394 struct qla_hw_data *ha = tgt->ha;
1395 unsigned long flags;
1396 int res;
1397 /*
1398 * We need to protect against race, when tgt is freed before or
1399 * inside wake_up()
1400 */
Quinn Tran726b8542017-01-19 22:28:00 -08001401 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001402 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001403 "tgt %p, sess_count=%d\n",
1404 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001405 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001406 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001407
1408 return res;
1409}
1410
1411/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001412int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001413{
1414 struct scsi_qla_host *vha = tgt->vha;
1415 struct qla_hw_data *ha = tgt->ha;
1416 unsigned long flags;
1417
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001418 mutex_lock(&qla_tgt_mutex);
1419 if (!vha->fc_vport) {
1420 struct Scsi_Host *sh = vha->host;
1421 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1422 bool npiv_vports;
1423
1424 spin_lock_irqsave(sh->host_lock, flags);
1425 npiv_vports = (fc_host->npiv_vports_inuse);
1426 spin_unlock_irqrestore(sh->host_lock, flags);
1427
1428 if (npiv_vports) {
1429 mutex_unlock(&qla_tgt_mutex);
1430 return -EPERM;
1431 }
1432 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001433 if (tgt->tgt_stop || tgt->tgt_stopped) {
1434 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1435 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001436 mutex_unlock(&qla_tgt_mutex);
1437 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001438 }
1439
1440 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1441 vha->host_no, vha);
1442 /*
1443 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1444 * Lock is needed, because we still can get an incoming packet.
1445 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001446 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001447 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001448 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001449 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001450 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001451 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001452 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001453
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1455 "Waiting for sess works (tgt %p)", tgt);
1456 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1457 while (!list_empty(&tgt->sess_works_list)) {
1458 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1459 flush_scheduled_work();
1460 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1461 }
1462 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1463
1464 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001465 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001466
1467 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1468
1469 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001470 if (!ha->flags.host_shutting_down &&
1471 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001472 qlt_disable_vha(vha);
1473
1474 /* Wait for sessions to clear out (just in case) */
1475 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001476 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001477}
1478EXPORT_SYMBOL(qlt_stop_phase1);
1479
1480/* Called by tcm_qla2xxx configfs code */
1481void qlt_stop_phase2(struct qla_tgt *tgt)
1482{
1483 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001484 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001485 unsigned long flags;
1486
1487 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001488 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001489 "Already in tgt->tgt_stopped state\n");
1490 dump_stack();
1491 return;
1492 }
1493
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001494 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001495 "Waiting for %d IRQ commands to complete (tgt %p)",
1496 tgt->irq_cmd_count, tgt);
1497
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001498 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001499 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05001500 while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001501 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1502 udelay(2);
1503 spin_lock_irqsave(&ha->hardware_lock, flags);
1504 }
1505 tgt->tgt_stop = 0;
1506 tgt->tgt_stopped = 1;
1507 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001508 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001509
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001510 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001511 tgt);
1512}
1513EXPORT_SYMBOL(qlt_stop_phase2);
1514
1515/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001516static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001517{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001518 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001519
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001520 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001521 qlt_stop_phase2(tgt);
1522
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001523 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001524
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001525 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001526 "Release of tgt %p finished\n", tgt);
1527
1528 kfree(tgt);
1529}
1530
1531/* ha->hardware_lock supposed to be held on entry */
1532static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1533 const void *param, unsigned int param_size)
1534{
1535 struct qla_tgt_sess_work_param *prm;
1536 unsigned long flags;
1537
1538 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1539 if (!prm) {
1540 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1541 "qla_target(%d): Unable to create session "
1542 "work, command will be refused", 0);
1543 return -ENOMEM;
1544 }
1545
1546 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1547 "Scheduling work (type %d, prm %p)"
1548 " to find session for param %p (size %d, tgt %p)\n",
1549 type, prm, param, param_size, tgt);
1550
1551 prm->type = type;
1552 memcpy(&prm->tm_iocb, param, param_size);
1553
1554 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1555 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1556 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1557
1558 schedule_work(&tgt->sess_work);
1559
1560 return 0;
1561}
1562
1563/*
1564 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1565 */
1566static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1567 struct imm_ntfy_from_isp *ntfy,
1568 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1569 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1570{
1571 struct qla_hw_data *ha = vha->hw;
1572 request_t *pkt;
1573 struct nack_to_isp *nack;
1574
Quinn Tranec7193e2017-03-15 09:48:55 -07001575 if (!ha->flags.fw_started)
1576 return;
1577
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001578 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1579
1580 /* Send marker if required */
1581 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1582 return;
1583
1584 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1585 if (!pkt) {
1586 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1587 "qla_target(%d): %s failed: unable to allocate "
1588 "request packet\n", vha->vp_idx, __func__);
1589 return;
1590 }
1591
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001592 if (vha->vha_tgt.qla_tgt != NULL)
1593 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001594
1595 pkt->entry_type = NOTIFY_ACK_TYPE;
1596 pkt->entry_count = 1;
1597
1598 nack = (struct nack_to_isp *)pkt;
1599 nack->ox_id = ntfy->ox_id;
1600
Quinn Tran726b8542017-01-19 22:28:00 -08001601 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001602 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1603 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1604 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001605 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001606 }
1607 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1608 nack->u.isp24.status = ntfy->u.isp24.status;
1609 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001610 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001611 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1612 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1613 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1614 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1615 nack->u.isp24.srr_reject_code = srr_reject_code;
1616 nack->u.isp24.srr_reject_code_expl = srr_explan;
1617 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1618
1619 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1620 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1621 vha->vp_idx, nack->u.isp24.status);
1622
Himanshu Madhani63163e02014-09-25 06:14:59 -04001623 /* Memory Barrier */
1624 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001625 qla2x00_start_iocbs(vha, vha->req);
1626}
1627
1628/*
1629 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1630 */
1631static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1632 struct abts_recv_from_24xx *abts, uint32_t status,
1633 bool ids_reversed)
1634{
1635 struct qla_hw_data *ha = vha->hw;
1636 struct abts_resp_to_24xx *resp;
1637 uint32_t f_ctl;
1638 uint8_t *p;
1639
1640 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1641 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1642 ha, abts, status);
1643
1644 /* Send marker if required */
1645 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1646 return;
1647
Arun Easib6a029e2014-09-25 06:14:52 -04001648 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001649 if (!resp) {
1650 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1651 "qla_target(%d): %s failed: unable to allocate "
1652 "request packet", vha->vp_idx, __func__);
1653 return;
1654 }
1655
1656 resp->entry_type = ABTS_RESP_24XX;
1657 resp->entry_count = 1;
1658 resp->nport_handle = abts->nport_handle;
1659 resp->vp_index = vha->vp_idx;
1660 resp->sof_type = abts->sof_type;
1661 resp->exchange_address = abts->exchange_address;
1662 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001663 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001664 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1665 F_CTL_SEQ_INITIATIVE);
1666 p = (uint8_t *)&f_ctl;
1667 resp->fcp_hdr_le.f_ctl[0] = *p++;
1668 resp->fcp_hdr_le.f_ctl[1] = *p++;
1669 resp->fcp_hdr_le.f_ctl[2] = *p;
1670 if (ids_reversed) {
1671 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1672 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1673 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1674 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1675 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1676 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1677 } else {
1678 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1679 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1680 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1681 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1682 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1683 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1684 }
1685 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1686 if (status == FCP_TMF_CMPL) {
1687 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1688 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1689 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1690 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1691 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1692 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1693 } else {
1694 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1695 resp->payload.ba_rjt.reason_code =
1696 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1697 /* Other bytes are zero */
1698 }
1699
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001700 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001701
Himanshu Madhani63163e02014-09-25 06:14:59 -04001702 /* Memory Barrier */
1703 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001704 qla2x00_start_iocbs(vha, vha->req);
1705}
1706
1707/*
1708 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1709 */
1710static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1711 struct abts_resp_from_24xx_fw *entry)
1712{
1713 struct ctio7_to_24xx *ctio;
1714
1715 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1716 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1717 /* Send marker if required */
1718 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1719 return;
1720
Arun Easib6a029e2014-09-25 06:14:52 -04001721 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001722 if (ctio == NULL) {
1723 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1724 "qla_target(%d): %s failed: unable to allocate "
1725 "request packet\n", vha->vp_idx, __func__);
1726 return;
1727 }
1728
1729 /*
1730 * We've got on entrance firmware's response on by us generated
1731 * ABTS response. So, in it ID fields are reversed.
1732 */
1733
1734 ctio->entry_type = CTIO_TYPE7;
1735 ctio->entry_count = 1;
1736 ctio->nport_handle = entry->nport_handle;
1737 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001738 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001739 ctio->vp_index = vha->vp_idx;
1740 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1741 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1742 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1743 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001744 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1745 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001746 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001747
Himanshu Madhani63163e02014-09-25 06:14:59 -04001748 /* Memory Barrier */
1749 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001750 qla2x00_start_iocbs(vha, vha->req);
1751
1752 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1753 FCP_TMF_CMPL, true);
1754}
1755
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001756static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1757{
1758 struct qla_tgt_sess_op *op;
1759 struct qla_tgt_cmd *cmd;
Quinn Tran8b631d82017-06-02 09:11:54 -07001760 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001761
Quinn Tran8b631d82017-06-02 09:11:54 -07001762 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001763 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1764 if (tag == op->atio.u.isp24.exchange_addr) {
1765 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001766 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001767 return 1;
1768 }
1769 }
1770
Quinn Tran41dc5292017-01-19 22:28:03 -08001771 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1772 if (tag == op->atio.u.isp24.exchange_addr) {
1773 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001774 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08001775 return 1;
1776 }
1777 }
1778
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001779 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1780 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001781 cmd->aborted = 1;
Quinn Tran8b631d82017-06-02 09:11:54 -07001782 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001783 return 1;
1784 }
1785 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001786 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001787
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001788 return 0;
1789}
1790
1791/* drop cmds for the given lun
1792 * XXX only looks for cmds on the port through which lun reset was recieved
1793 * XXX does not go through the list of other port (which may have cmds
1794 * for the same lun)
1795 */
1796static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001797 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001798{
1799 struct qla_tgt_sess_op *op;
1800 struct qla_tgt_cmd *cmd;
1801 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001802 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001803
1804 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001805 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001806 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1807 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001808 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001809
1810 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1811 op_lun = scsilun_to_int(
1812 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1813 if (op_key == key && op_lun == lun)
1814 op->aborted = true;
1815 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001816
1817 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1818 uint32_t op_key;
1819 u64 op_lun;
1820
1821 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1822 op_lun = scsilun_to_int(
1823 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1824 if (op_key == key && op_lun == lun)
1825 op->aborted = true;
1826 }
1827
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001828 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1829 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001830 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001831
1832 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1833 cmd_lun = scsilun_to_int(
1834 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1835 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001836 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001837 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001838 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001839}
1840
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001841/* ha->hardware_lock supposed to be held on entry */
1842static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001843 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001844{
1845 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001846 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001847 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07001848 struct qla_tgt_cmd *cmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001849 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001850 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001851 bool found_lun = false;
Quinn Tran5d964832017-01-19 22:27:59 -08001852 unsigned long flags;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001853
Quinn Tran5d964832017-01-19 22:27:59 -08001854 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
Steve Hodgson06e97b42012-11-16 08:06:17 -08001855 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
Bart Van Assche649ee052015-04-14 13:26:44 +02001856 if (se_cmd->tag == abts->exchange_addr_to_abort) {
Steve Hodgson06e97b42012-11-16 08:06:17 -08001857 found_lun = true;
1858 break;
1859 }
1860 }
Quinn Tran5d964832017-01-19 22:27:59 -08001861 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Steve Hodgson06e97b42012-11-16 08:06:17 -08001862
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001863 /* cmd not in LIO lists, look in qla list */
1864 if (!found_lun) {
1865 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1866 /* send TASK_ABORT response immediately */
1867 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1868 return 0;
1869 } else {
1870 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1871 "unable to find cmd in driver or LIO for tag 0x%x\n",
1872 abts->exchange_addr_to_abort);
1873 return -ENOENT;
1874 }
1875 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001876
1877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1878 "qla_target(%d): task abort (tag=%d)\n",
1879 vha->vp_idx, abts->exchange_addr_to_abort);
1880
1881 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1882 if (mcmd == NULL) {
1883 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1884 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1885 vha->vp_idx, __func__);
1886 return -ENOMEM;
1887 }
1888 memset(mcmd, 0, sizeof(*mcmd));
1889
Quinn Tranf775bd12017-06-02 09:11:59 -07001890 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001891 mcmd->sess = sess;
1892 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Arun Easi80187f82014-09-25 06:14:53 -04001893 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001894 mcmd->tmr_func = QLA_TGT_ABTS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001895
Quinn Tranf775bd12017-06-02 09:11:59 -07001896 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, cmd->unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001897 abts->exchange_addr_to_abort);
1898 if (rc != 0) {
1899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1900 "qla_target(%d): tgt_ops->handle_tmr()"
1901 " failed: %d", vha->vp_idx, rc);
1902 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1903 return -EFAULT;
1904 }
1905
1906 return 0;
1907}
1908
1909/*
1910 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1911 */
1912static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1913 struct abts_recv_from_24xx *abts)
1914{
1915 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001916 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001917 uint32_t tag = abts->exchange_addr_to_abort;
1918 uint8_t s_id[3];
1919 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001920 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001921
1922 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1923 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1924 "qla_target(%d): ABTS: Abort Sequence not "
1925 "supported\n", vha->vp_idx);
1926 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1927 return;
1928 }
1929
1930 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1931 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1932 "qla_target(%d): ABTS: Unknown Exchange "
1933 "Address received\n", vha->vp_idx);
1934 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1935 return;
1936 }
1937
1938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1939 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1940 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1941 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1942 le32_to_cpu(abts->fcp_hdr_le.parameter));
1943
1944 s_id[0] = abts->fcp_hdr_le.s_id[2];
1945 s_id[1] = abts->fcp_hdr_le.s_id[1];
1946 s_id[2] = abts->fcp_hdr_le.s_id[0];
1947
Quinn Tran75601512015-12-17 14:57:04 -05001948 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001949 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1950 if (!sess) {
1951 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1952 "qla_target(%d): task abort for non-existant session\n",
1953 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001954 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001955 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001956
1957 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1958
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001959 if (rc != 0) {
1960 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1961 false);
1962 }
1963 return;
1964 }
Quinn Tran75601512015-12-17 14:57:04 -05001965 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1966
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001967
Quinn Tran726b8542017-01-19 22:28:00 -08001968 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001969 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1970 return;
1971 }
1972
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001973 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1974 if (rc != 0) {
1975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1976 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1977 vha->vp_idx, rc);
1978 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1979 return;
1980 }
1981}
1982
1983/*
1984 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1985 */
1986static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1987 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1988{
1989 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1990 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001991 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001992
1993 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1994 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1995 ha, atio, resp_code);
1996
1997 /* Send marker if required */
1998 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1999 return;
2000
2001 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
2002 if (ctio == NULL) {
2003 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2004 "qla_target(%d): %s failed: unable to allocate "
2005 "request packet\n", ha->vp_idx, __func__);
2006 return;
2007 }
2008
2009 ctio->entry_type = CTIO_TYPE7;
2010 ctio->entry_count = 1;
2011 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2012 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002013 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002014 ctio->vp_index = ha->vp_idx;
2015 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2016 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2017 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2018 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2019 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07002020 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002021 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2022 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002023 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002024 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2025 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002026 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002027
Himanshu Madhani63163e02014-09-25 06:14:59 -04002028 /* Memory Barrier */
2029 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002030 qla2x00_start_iocbs(ha, ha->req);
2031}
2032
2033void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2034{
2035 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2036}
2037EXPORT_SYMBOL(qlt_free_mcmd);
2038
Quinn Tranbe251522017-03-15 09:48:49 -07002039/*
2040 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2041 * reacquire
2042 */
2043void qlt_send_resp_ctio(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
2044 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2045{
2046 struct atio_from_isp *atio = &cmd->atio;
2047 struct ctio7_to_24xx *ctio;
2048 uint16_t temp;
2049
2050 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2051 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2052 "sense_key=%02x, asc=%02x, ascq=%02x",
2053 vha, atio, scsi_status, sense_key, asc, ascq);
2054
2055 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2056 if (!ctio) {
2057 ql_dbg(ql_dbg_async, vha, 0x3067,
2058 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2059 vha->host_no, __func__);
2060 goto out;
2061 }
2062
2063 ctio->entry_type = CTIO_TYPE7;
2064 ctio->entry_count = 1;
2065 ctio->handle = QLA_TGT_SKIP_HANDLE;
2066 ctio->nport_handle = cmd->sess->loop_id;
2067 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2068 ctio->vp_index = vha->vp_idx;
2069 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2070 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2071 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2072 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2073 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
2074 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
2075 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2076 ctio->u.status1.ox_id = cpu_to_le16(temp);
2077 ctio->u.status1.scsi_status =
2078 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2079 ctio->u.status1.response_len = cpu_to_le16(18);
2080 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2081
2082 if (ctio->u.status1.residual != 0)
2083 ctio->u.status1.scsi_status |=
2084 cpu_to_le16(SS_RESIDUAL_UNDER);
2085
2086 /* Response code and sense key */
2087 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2088 (&ctio->u.status1.sense_data)[0]);
2089 /* Additional sense length */
2090 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2091 /* ASC and ASCQ */
2092 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2093 (&ctio->u.status1.sense_data)[3]);
2094
2095 /* Memory Barrier */
2096 wmb();
2097
2098 qla2x00_start_iocbs(vha, vha->req);
2099out:
2100 return;
2101}
2102
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002103/* callback from target fabric module code */
2104void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2105{
2106 struct scsi_qla_host *vha = mcmd->sess->vha;
2107 struct qla_hw_data *ha = vha->hw;
2108 unsigned long flags;
2109
2110 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2111 "TM response mcmd (%p) status %#x state %#x",
2112 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2113
2114 spin_lock_irqsave(&ha->hardware_lock, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002115
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002116 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002117 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002118 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002119 * previous life, just abort the processing.
2120 */
2121 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002122 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2123 vha->flags.online, qla2x00_reset_active(vha),
2124 mcmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002125 ha->tgt.tgt_ops->free_mcmd(mcmd);
2126 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2127 return;
2128 }
2129
Quinn Tran726b8542017-01-19 22:28:00 -08002130 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2131 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002132 ELS_LOGO ||
2133 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2134 ELS_PRLO ||
2135 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2136 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002137 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002138 "TM response logo %phC status %#x state %#x",
2139 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2140 mcmd->flags);
2141 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2142 } else {
2143 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
2144 0, 0, 0, 0, 0, 0);
2145 }
2146 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002147 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002148 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
2149 mcmd->fc_tm_rsp, false);
2150 else
2151 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
2152 mcmd->fc_tm_rsp);
2153 }
2154 /*
2155 * Make the callback for ->free_mcmd() to queue_work() and invoke
2156 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2157 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2158 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2159 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2160 * qlt_xmit_tm_rsp() returns here..
2161 */
2162 ha->tgt.tgt_ops->free_mcmd(mcmd);
2163 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2164}
2165EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2166
2167/* No locks */
2168static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2169{
2170 struct qla_tgt_cmd *cmd = prm->cmd;
2171
2172 BUG_ON(cmd->sg_cnt == 0);
2173
2174 prm->sg = (struct scatterlist *)cmd->sg;
2175 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
2176 cmd->sg_cnt, cmd->dma_data_direction);
2177 if (unlikely(prm->seg_cnt == 0))
2178 goto out_err;
2179
2180 prm->cmd->sg_mapped = 1;
2181
Quinn Tranf83adb62014-04-11 16:54:43 -04002182 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2183 /*
2184 * If greater than four sg entries then we need to allocate
2185 * the continuation entries
2186 */
2187 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
2188 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2189 prm->tgt->datasegs_per_cmd,
2190 prm->tgt->datasegs_per_cont);
2191 } else {
2192 /* DIF */
2193 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2194 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2195 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2196 prm->tot_dsds = prm->seg_cnt;
2197 } else
2198 prm->tot_dsds = prm->seg_cnt;
2199
2200 if (cmd->prot_sg_cnt) {
2201 prm->prot_sg = cmd->prot_sg;
2202 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
2203 cmd->prot_sg, cmd->prot_sg_cnt,
2204 cmd->dma_data_direction);
2205 if (unlikely(prm->prot_seg_cnt == 0))
2206 goto out_err;
2207
2208 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2209 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2210 /* Dif Bundling not support here */
2211 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2212 cmd->blk_sz);
2213 prm->tot_dsds += prm->prot_seg_cnt;
2214 } else
2215 prm->tot_dsds += prm->prot_seg_cnt;
2216 }
2217 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002218
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002219 return 0;
2220
2221out_err:
2222 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
2223 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2224 0, prm->cmd->sg_cnt);
2225 return -1;
2226}
2227
Joern Engelf9b67212014-09-16 16:23:18 -04002228static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002229{
2230 struct qla_hw_data *ha = vha->hw;
2231
Joern Engelf9b67212014-09-16 16:23:18 -04002232 if (!cmd->sg_mapped)
2233 return;
2234
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002235 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2236 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002237
2238 if (cmd->prot_sg_cnt)
2239 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2240 cmd->dma_data_direction);
2241
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002242 if (!cmd->ctx)
2243 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04002244
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002245 if (cmd->ctx_dsd_alloced)
2246 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2247
2248 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002249}
2250
2251static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
2252 uint32_t req_cnt)
2253{
Saurav Kashyapd29fb732014-09-25 06:14:49 -04002254 uint32_t cnt, cnt_in;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002255
2256 if (vha->req->cnt < (req_cnt + 2)) {
Arun Easi75554b62014-09-25 06:14:45 -04002257 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
Saurav Kashyapd29fb732014-09-25 06:14:49 -04002258 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002259
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002260 if (vha->req->ring_index < cnt)
2261 vha->req->cnt = cnt - vha->req->ring_index;
2262 else
2263 vha->req->cnt = vha->req->length -
2264 (vha->req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002265
2266 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
2267 ql_dbg(ql_dbg_io, vha, 0x305a,
2268 "qla_target(%d): There is no room in the request ring: vha->req->ring_index=%d, vha->req->cnt=%d, req_cnt=%d Req-out=%d Req-in=%d Req-Length=%d\n",
2269 vha->vp_idx, vha->req->ring_index,
2270 vha->req->cnt, req_cnt, cnt, cnt_in,
2271 vha->req->length);
2272 return -EAGAIN;
2273 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002274 }
2275
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002276 vha->req->cnt -= req_cnt;
2277
2278 return 0;
2279}
2280
2281/*
2282 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2283 */
2284static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
2285{
2286 /* Adjust ring index. */
2287 vha->req->ring_index++;
2288 if (vha->req->ring_index == vha->req->length) {
2289 vha->req->ring_index = 0;
2290 vha->req->ring_ptr = vha->req->ring;
2291 } else {
2292 vha->req->ring_ptr++;
2293 }
2294 return (cont_entry_t *)vha->req->ring_ptr;
2295}
2296
2297/* ha->hardware_lock supposed to be held on entry */
2298static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
2299{
2300 struct qla_hw_data *ha = vha->hw;
2301 uint32_t h;
2302
2303 h = ha->tgt.current_handle;
2304 /* always increment cmd handle */
2305 do {
2306 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002307 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002308 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
2309 if (h == ha->tgt.current_handle) {
Arun Easi667024a2014-09-25 06:14:47 -04002310 ql_dbg(ql_dbg_io, vha, 0x305b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002311 "qla_target(%d): Ran out of "
2312 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
2313 h = QLA_TGT_NULL_HANDLE;
2314 break;
2315 }
2316 } while ((h == QLA_TGT_NULL_HANDLE) ||
2317 (h == QLA_TGT_SKIP_HANDLE) ||
2318 (ha->tgt.cmds[h-1] != NULL));
2319
2320 if (h != QLA_TGT_NULL_HANDLE)
2321 ha->tgt.current_handle = h;
2322
2323 return h;
2324}
2325
2326/* ha->hardware_lock supposed to be held on entry */
2327static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
2328 struct scsi_qla_host *vha)
2329{
2330 uint32_t h;
2331 struct ctio7_to_24xx *pkt;
2332 struct qla_hw_data *ha = vha->hw;
2333 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002334 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002335
2336 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
2337 prm->pkt = pkt;
2338 memset(pkt, 0, sizeof(*pkt));
2339
2340 pkt->entry_type = CTIO_TYPE7;
2341 pkt->entry_count = (uint8_t)prm->req_cnt;
2342 pkt->vp_index = vha->vp_idx;
2343
2344 h = qlt_make_handle(vha);
2345 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2346 /*
2347 * CTIO type 7 from the firmware doesn't provide a way to
2348 * know the initiator's LOOP ID, hence we can't find
2349 * the session and, so, the command.
2350 */
2351 return -EAGAIN;
2352 } else
Quinn Tranbe251522017-03-15 09:48:49 -07002353 ha->tgt.cmds[h - 1] = prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002354
2355 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2356 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002357 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002358 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2359 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2360 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2361 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2362 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002363 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2364 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002365 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2366
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002367 return 0;
2368}
2369
2370/*
2371 * ha->hardware_lock supposed to be held on entry. We have already made sure
2372 * that there is sufficient amount of request entries to not drop it.
2373 */
2374static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
2375 struct scsi_qla_host *vha)
2376{
2377 int cnt;
2378 uint32_t *dword_ptr;
2379 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2380
2381 /* Build continuation packets */
2382 while (prm->seg_cnt > 0) {
2383 cont_a64_entry_t *cont_pkt64 =
2384 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2385
2386 /*
2387 * Make sure that from cont_pkt64 none of
2388 * 64-bit specific fields used for 32-bit
2389 * addressing. Cast to (cont_entry_t *) for
2390 * that.
2391 */
2392
2393 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2394
2395 cont_pkt64->entry_count = 1;
2396 cont_pkt64->sys_define = 0;
2397
2398 if (enable_64bit_addressing) {
2399 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2400 dword_ptr =
2401 (uint32_t *)&cont_pkt64->dseg_0_address;
2402 } else {
2403 cont_pkt64->entry_type = CONTINUE_TYPE;
2404 dword_ptr =
2405 (uint32_t *)&((cont_entry_t *)
2406 cont_pkt64)->dseg_0_address;
2407 }
2408
2409 /* Load continuation entry data segments */
2410 for (cnt = 0;
2411 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2412 cnt++, prm->seg_cnt--) {
2413 *dword_ptr++ =
2414 cpu_to_le32(pci_dma_lo32
2415 (sg_dma_address(prm->sg)));
2416 if (enable_64bit_addressing) {
2417 *dword_ptr++ =
2418 cpu_to_le32(pci_dma_hi32
2419 (sg_dma_address
2420 (prm->sg)));
2421 }
2422 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2423
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002424 prm->sg = sg_next(prm->sg);
2425 }
2426 }
2427}
2428
2429/*
2430 * ha->hardware_lock supposed to be held on entry. We have already made sure
2431 * that there is sufficient amount of request entries to not drop it.
2432 */
2433static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2434 struct scsi_qla_host *vha)
2435{
2436 int cnt;
2437 uint32_t *dword_ptr;
2438 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2439 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2440
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002441 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2442
2443 /* Setup packet address segment pointer */
2444 dword_ptr = pkt24->u.status0.dseg_0_address;
2445
2446 /* Set total data segment count */
2447 if (prm->seg_cnt)
2448 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2449
2450 if (prm->seg_cnt == 0) {
2451 /* No data transfer */
2452 *dword_ptr++ = 0;
2453 *dword_ptr = 0;
2454 return;
2455 }
2456
2457 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002458
2459 /* Load command entry data segments */
2460 for (cnt = 0;
2461 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2462 cnt++, prm->seg_cnt--) {
2463 *dword_ptr++ =
2464 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2465 if (enable_64bit_addressing) {
2466 *dword_ptr++ =
2467 cpu_to_le32(pci_dma_hi32(
2468 sg_dma_address(prm->sg)));
2469 }
2470 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2471
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002472 prm->sg = sg_next(prm->sg);
2473 }
2474
2475 qlt_load_cont_data_segments(prm, vha);
2476}
2477
2478static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2479{
2480 return cmd->bufflen > 0;
2481}
2482
Quinn Tranbe251522017-03-15 09:48:49 -07002483static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2484{
2485 struct qla_tgt_cmd *cmd;
2486 struct scsi_qla_host *vha;
2487
2488 /* asc 0x10=dif error */
2489 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2490 cmd = prm->cmd;
2491 vha = cmd->vha;
2492 /* ASCQ */
2493 switch (prm->sense_buffer[13]) {
2494 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002495 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002496 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2497 "se_cmd=%p tag[%x]",
2498 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2499 cmd->atio.u.isp24.exchange_addr);
2500 break;
2501 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002502 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002503 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2504 "se_cmd=%p tag[%x]",
2505 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2506 cmd->atio.u.isp24.exchange_addr);
2507 break;
2508 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002509 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002510 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2511 "se_cmd=%p tag[%x]",
2512 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2513 cmd->atio.u.isp24.exchange_addr);
2514 break;
2515 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002516 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002517 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2518 "se_cmd=%p tag[%x]",
2519 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2520 cmd->atio.u.isp24.exchange_addr);
2521 break;
2522 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002523 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002524 }
2525}
2526
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002527/*
2528 * Called without ha->hardware_lock held
2529 */
2530static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2531 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2532 uint32_t *full_req_cnt)
2533{
2534 struct qla_tgt *tgt = cmd->tgt;
2535 struct scsi_qla_host *vha = tgt->vha;
2536 struct qla_hw_data *ha = vha->hw;
2537 struct se_cmd *se_cmd = &cmd->se_cmd;
2538
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002539 prm->cmd = cmd;
2540 prm->tgt = tgt;
2541 prm->rq_result = scsi_status;
2542 prm->sense_buffer = &cmd->sense_buffer[0];
2543 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2544 prm->sg = NULL;
2545 prm->seg_cnt = -1;
2546 prm->req_cnt = 1;
2547 prm->add_status_pkt = 0;
2548
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002549 /* Send marker if required */
2550 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2551 return -EFAULT;
2552
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002553 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2554 if (qlt_pci_map_calc_cnt(prm) != 0)
2555 return -EAGAIN;
2556 }
2557
2558 *full_req_cnt = prm->req_cnt;
2559
2560 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2561 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002562 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002563 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2564 prm->residual, se_cmd->tag,
2565 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2566 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002567 prm->rq_result |= SS_RESIDUAL_UNDER;
2568 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2569 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002570 ql_dbg(ql_dbg_io, vha, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002571 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2572 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2573 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002574 prm->rq_result |= SS_RESIDUAL_OVER;
2575 }
2576
2577 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2578 /*
2579 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2580 * ignored in *xmit_response() below
2581 */
2582 if (qlt_has_data(cmd)) {
2583 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2584 (IS_FWI2_CAPABLE(ha) &&
2585 (prm->rq_result != 0))) {
2586 prm->add_status_pkt = 1;
2587 (*full_req_cnt)++;
2588 }
2589 }
2590 }
2591
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002592 return 0;
2593}
2594
2595static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2596 struct qla_tgt_cmd *cmd, int sending_sense)
2597{
2598 if (ha->tgt.enable_class_2)
2599 return 0;
2600
2601 if (sending_sense)
2602 return cmd->conf_compl_supported;
2603 else
2604 return ha->tgt.enable_explicit_conf &&
2605 cmd->conf_compl_supported;
2606}
2607
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002608static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2609 struct qla_tgt_prm *prm)
2610{
2611 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2612 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002613 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002614 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002615 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002616 CTIO7_FLAGS_EXPLICIT_CONFORM |
2617 CTIO7_FLAGS_CONFORM_REQ);
2618 }
2619 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2620 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2621 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2622 int i;
2623
2624 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002625 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002626 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2627 "Skipping EXPLICIT_CONFORM and "
2628 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2629 "non GOOD status\n");
2630 goto skip_explict_conf;
2631 }
Bart Van Asschead950362015-07-09 07:24:08 -07002632 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002633 CTIO7_FLAGS_EXPLICIT_CONFORM |
2634 CTIO7_FLAGS_CONFORM_REQ);
2635 }
2636skip_explict_conf:
2637 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002638 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002639 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002640 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002641 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002642 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002643 ctio->u.status1.sense_length =
2644 cpu_to_le16(prm->sense_buffer_len);
2645 for (i = 0; i < prm->sense_buffer_len/4; i++)
2646 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2647 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002648
2649 qlt_print_dif_err(prm);
2650
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002651 } else {
2652 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002653 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002654 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002655 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002656 ctio->u.status1.sense_length = 0;
2657 memset(ctio->u.status1.sense_data, 0,
2658 sizeof(ctio->u.status1.sense_data));
2659 }
2660
2661 /* Sense with len > 24, is it possible ??? */
2662}
2663
Quinn Tranf83adb62014-04-11 16:54:43 -04002664static inline int
2665qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2666{
Quinn Tranf83adb62014-04-11 16:54:43 -04002667 switch (se_cmd->prot_op) {
2668 case TARGET_PROT_DOUT_INSERT:
2669 case TARGET_PROT_DIN_STRIP:
2670 if (ql2xenablehba_err_chk >= 1)
2671 return 1;
2672 break;
2673 case TARGET_PROT_DOUT_PASS:
2674 case TARGET_PROT_DIN_PASS:
2675 if (ql2xenablehba_err_chk >= 2)
2676 return 1;
2677 break;
2678 case TARGET_PROT_DIN_INSERT:
2679 case TARGET_PROT_DOUT_STRIP:
2680 return 1;
2681 default:
2682 break;
2683 }
2684 return 0;
2685}
2686
Quinn Tranbe251522017-03-15 09:48:49 -07002687static inline int
2688qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002689{
Quinn Tranbe251522017-03-15 09:48:49 -07002690 switch (se_cmd->prot_op) {
2691 case TARGET_PROT_DIN_INSERT:
2692 case TARGET_PROT_DOUT_INSERT:
2693 case TARGET_PROT_DIN_STRIP:
2694 case TARGET_PROT_DOUT_STRIP:
2695 case TARGET_PROT_DIN_PASS:
2696 case TARGET_PROT_DOUT_PASS:
2697 return 1;
2698 default:
2699 return 0;
2700 }
2701 return 0;
2702}
Quinn Tranf83adb62014-04-11 16:54:43 -04002703
Quinn Tranbe251522017-03-15 09:48:49 -07002704/*
2705 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2706 */
2707static void
2708qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2709 uint16_t *pfw_prot_opts)
2710{
2711 struct se_cmd *se_cmd = &cmd->se_cmd;
2712 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2713 scsi_qla_host_t *vha = cmd->tgt->vha;
2714 struct qla_hw_data *ha = vha->hw;
2715 uint32_t t32 = 0;
2716
2717 /*
2718 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002719 * have been immplemented by TCM, before AppTag is avail.
2720 * Look for modesense_handlers[]
2721 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002722 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002723 ctx->app_tag_mask[0] = 0x0;
2724 ctx->app_tag_mask[1] = 0x0;
2725
Quinn Tranbe251522017-03-15 09:48:49 -07002726 if (IS_PI_UNINIT_CAPABLE(ha)) {
2727 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2728 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2729 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2730 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2731 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2732 }
2733
2734 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2735
Quinn Tranf83adb62014-04-11 16:54:43 -04002736 switch (se_cmd->prot_type) {
2737 case TARGET_DIF_TYPE0_PROT:
2738 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002739 * No check for ql2xenablehba_err_chk, as it
2740 * would be an I/O error if hba tag generation
2741 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002742 */
2743 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002744 /* enable ALL bytes of the ref tag */
2745 ctx->ref_tag_mask[0] = 0xff;
2746 ctx->ref_tag_mask[1] = 0xff;
2747 ctx->ref_tag_mask[2] = 0xff;
2748 ctx->ref_tag_mask[3] = 0xff;
2749 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002750 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002751 /*
2752 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2753 * REF tag, and 16 bit app tag.
2754 */
2755 ctx->ref_tag = cpu_to_le32(lba);
2756 if (!qla_tgt_ref_mask_check(se_cmd) ||
2757 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2758 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2759 break;
2760 }
2761 /* enable ALL bytes of the ref tag */
2762 ctx->ref_tag_mask[0] = 0xff;
2763 ctx->ref_tag_mask[1] = 0xff;
2764 ctx->ref_tag_mask[2] = 0xff;
2765 ctx->ref_tag_mask[3] = 0xff;
2766 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002767 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002768 /*
2769 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2770 * tag has to match LBA in CDB + N
2771 */
2772 ctx->ref_tag = cpu_to_le32(lba);
2773 if (!qla_tgt_ref_mask_check(se_cmd) ||
2774 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2775 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2776 break;
2777 }
2778 /* enable ALL bytes of the ref tag */
2779 ctx->ref_tag_mask[0] = 0xff;
2780 ctx->ref_tag_mask[1] = 0xff;
2781 ctx->ref_tag_mask[2] = 0xff;
2782 ctx->ref_tag_mask[3] = 0xff;
2783 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002784 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002785 /* For TYPE 3 protection: 16 bit GUARD only */
2786 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2787 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2788 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2789 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002790 }
2791}
2792
Quinn Tranf83adb62014-04-11 16:54:43 -04002793static inline int
2794qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2795{
2796 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002797 uint32_t transfer_length = 0;
2798 uint32_t data_bytes;
2799 uint32_t dif_bytes;
2800 uint8_t bundling = 1;
2801 uint8_t *clr_ptr;
2802 struct crc_context *crc_ctx_pkt = NULL;
2803 struct qla_hw_data *ha;
2804 struct ctio_crc2_to_fw *pkt;
2805 dma_addr_t crc_ctx_dma;
2806 uint16_t fw_prot_opts = 0;
2807 struct qla_tgt_cmd *cmd = prm->cmd;
2808 struct se_cmd *se_cmd = &cmd->se_cmd;
2809 uint32_t h;
2810 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002811 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002812 uint16_t t16;
Quinn Tranf83adb62014-04-11 16:54:43 -04002813
Quinn Tranf83adb62014-04-11 16:54:43 -04002814 ha = vha->hw;
2815
2816 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2817 prm->pkt = pkt;
2818 memset(pkt, 0, sizeof(*pkt));
2819
2820 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2821 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2822 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2823 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2824
2825 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2826 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2827 bundling = 0;
2828
2829 /* Compute dif len and adjust data len to incude protection */
2830 data_bytes = cmd->bufflen;
2831 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2832
2833 switch (se_cmd->prot_op) {
2834 case TARGET_PROT_DIN_INSERT:
2835 case TARGET_PROT_DOUT_STRIP:
2836 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002837 if (cmd->prot_sg_cnt)
2838 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002839 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002840 case TARGET_PROT_DIN_STRIP:
2841 case TARGET_PROT_DOUT_INSERT:
2842 case TARGET_PROT_DIN_PASS:
2843 case TARGET_PROT_DOUT_PASS:
2844 transfer_length = data_bytes + dif_bytes;
2845 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002846 default:
2847 BUG();
2848 break;
2849 }
2850
2851 if (!qlt_hba_err_chk_enabled(se_cmd))
2852 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2853 /* HBA error checking enabled */
2854 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2855 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2856 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2857 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2858 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2859 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2860 }
2861
2862 switch (se_cmd->prot_op) {
2863 case TARGET_PROT_DIN_INSERT:
2864 case TARGET_PROT_DOUT_INSERT:
2865 fw_prot_opts |= PO_MODE_DIF_INSERT;
2866 break;
2867 case TARGET_PROT_DIN_STRIP:
2868 case TARGET_PROT_DOUT_STRIP:
2869 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2870 break;
2871 case TARGET_PROT_DIN_PASS:
2872 case TARGET_PROT_DOUT_PASS:
2873 fw_prot_opts |= PO_MODE_DIF_PASS;
2874 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2875 break;
2876 default:/* Normal Request */
2877 fw_prot_opts |= PO_MODE_DIF_PASS;
2878 break;
2879 }
2880
Quinn Tranf83adb62014-04-11 16:54:43 -04002881 /* ---- PKT ---- */
2882 /* Update entry type to indicate Command Type CRC_2 IOCB */
2883 pkt->entry_type = CTIO_CRC2;
2884 pkt->entry_count = 1;
2885 pkt->vp_index = vha->vp_idx;
2886
2887 h = qlt_make_handle(vha);
2888 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2889 /*
2890 * CTIO type 7 from the firmware doesn't provide a way to
2891 * know the initiator's LOOP ID, hence we can't find
2892 * the session and, so, the command.
2893 */
2894 return -EAGAIN;
2895 } else
2896 ha->tgt.cmds[h-1] = prm->cmd;
2897
Quinn Tranf83adb62014-04-11 16:54:43 -04002898 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07002899 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002900 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002901 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2902 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2903 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2904 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002905
2906 /* silence compile warning */
2907 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2908 pkt->ox_id = cpu_to_le16(t16);
2909
2910 t16 = (atio->u.isp24.attr << 9);
2911 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002912 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2913
2914 /* Set transfer direction */
2915 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002916 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002917 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002918 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002919
Quinn Tranf83adb62014-04-11 16:54:43 -04002920 pkt->dseg_count = prm->tot_dsds;
2921 /* Fibre channel byte count */
2922 pkt->transfer_length = cpu_to_le32(transfer_length);
2923
Quinn Tranf83adb62014-04-11 16:54:43 -04002924 /* ----- CRC context -------- */
2925
2926 /* Allocate CRC context from global pool */
2927 crc_ctx_pkt = cmd->ctx =
2928 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2929
2930 if (!crc_ctx_pkt)
2931 goto crc_queuing_error;
2932
2933 /* Zero out CTX area. */
2934 clr_ptr = (uint8_t *)crc_ctx_pkt;
2935 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2936
2937 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2938 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2939
2940 /* Set handle */
2941 crc_ctx_pkt->handle = pkt->handle;
2942
Quinn Tranbe251522017-03-15 09:48:49 -07002943 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04002944
2945 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2946 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2947 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2948
Quinn Tranf83adb62014-04-11 16:54:43 -04002949 if (!bundling) {
2950 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2951 } else {
2952 /*
2953 * Configure Bundling if we need to fetch interlaving
2954 * protection PCI accesses
2955 */
2956 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2957 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2958 crc_ctx_pkt->u.bundling.dseg_count =
2959 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2960 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2961 }
2962
2963 /* Finish the common fields of CRC pkt */
2964 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2965 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2966 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002967 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002968
Quinn Tranbe251522017-03-15 09:48:49 -07002969 memset((uint8_t *)&tc, 0 , sizeof(tc));
2970 tc.vha = vha;
2971 tc.blk_sz = cmd->blk_sz;
2972 tc.bufflen = cmd->bufflen;
2973 tc.sg = cmd->sg;
2974 tc.prot_sg = cmd->prot_sg;
2975 tc.ctx = crc_ctx_pkt;
2976 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04002977
2978 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07002979 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04002980
2981 if (!bundling && prm->prot_seg_cnt) {
2982 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07002983 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04002984 goto crc_queuing_error;
2985 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07002986 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04002987 goto crc_queuing_error;
2988
2989 if (bundling && prm->prot_seg_cnt) {
2990 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002991 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04002992
2993 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2994 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07002995 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04002996 goto crc_queuing_error;
2997 }
2998 return QLA_SUCCESS;
2999
3000crc_queuing_error:
3001 /* Cleanup will be performed by the caller */
Quinn Tranbe251522017-03-15 09:48:49 -07003002 vha->hw->tgt.cmds[h - 1] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003003
3004 return QLA_FUNCTION_FAILED;
3005}
3006
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003007/*
3008 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3009 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3010 */
3011int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3012 uint8_t scsi_status)
3013{
3014 struct scsi_qla_host *vha = cmd->vha;
3015 struct qla_hw_data *ha = vha->hw;
3016 struct ctio7_to_24xx *pkt;
3017 struct qla_tgt_prm prm;
3018 uint32_t full_req_cnt = 0;
3019 unsigned long flags = 0;
3020 int res;
3021
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003022 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08003023 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003024 cmd->state = QLA_TGT_STATE_PROCESSED;
3025 if (cmd->sess->logout_completed)
3026 /* no need to terminate. FW already freed exchange. */
3027 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3028 else
Quinn Trana07100e2015-12-07 19:48:57 -05003029 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003030 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3031 return 0;
3032 }
3033 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3034
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003035 memset(&prm, 0, sizeof(prm));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003036
3037 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
Quinn Tranf83adb62014-04-11 16:54:43 -04003038 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
3039 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3040 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3041 &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003042
3043 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3044 &full_req_cnt);
3045 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003046 return res;
3047 }
3048
3049 spin_lock_irqsave(&ha->hardware_lock, flags);
3050
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003051 if (xmit_type == QLA_TGT_XMIT_STATUS)
3052 vha->tgt_counters.core_qla_snd_status++;
3053 else
3054 vha->tgt_counters.core_qla_que_buf++;
3055
Quinn Tranec7193e2017-03-15 09:48:55 -07003056 if (!ha->flags.fw_started || cmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003057 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003058 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003059 * previous life, just abort the processing.
3060 */
3061 cmd->state = QLA_TGT_STATE_PROCESSED;
3062 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3063 ql_dbg(ql_dbg_async, vha, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003064 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3065 vha->flags.online, qla2x00_reset_active(vha),
3066 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003067 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3068 return 0;
3069 }
3070
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003071 /* Does F/W have an IOCBs for this request */
3072 res = qlt_check_reserve_free_req(vha, full_req_cnt);
3073 if (unlikely(res))
3074 goto out_unmap_unlock;
3075
Quinn Tranf83adb62014-04-11 16:54:43 -04003076 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3077 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3078 else
3079 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Quinn Tran810e30b2015-06-10 11:05:20 -04003080 if (unlikely(res != 0)) {
3081 vha->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003082 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003083 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003084
3085 pkt = (struct ctio7_to_24xx *)prm.pkt;
3086
3087 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3088 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003089 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003090 CTIO7_FLAGS_STATUS_MODE_0);
3091
Quinn Tranf83adb62014-04-11 16:54:43 -04003092 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3093 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003094
3095 if (prm.add_status_pkt == 0) {
3096 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3097 pkt->u.status0.scsi_status =
3098 cpu_to_le16(prm.rq_result);
3099 pkt->u.status0.residual =
3100 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003101 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003102 CTIO7_FLAGS_SEND_STATUS);
3103 if (qlt_need_explicit_conf(ha, cmd, 0)) {
3104 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003105 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003106 CTIO7_FLAGS_EXPLICIT_CONFORM |
3107 CTIO7_FLAGS_CONFORM_REQ);
3108 }
3109 }
3110
3111 } else {
3112 /*
3113 * We have already made sure that there is sufficient
3114 * amount of request entries to not drop HW lock in
3115 * req_pkt().
3116 */
3117 struct ctio7_to_24xx *ctio =
3118 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
3119
Arun Easi667024a2014-09-25 06:14:47 -04003120 ql_dbg(ql_dbg_io, vha, 0x305e,
3121 "Building additional status packet 0x%p.\n",
3122 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003123
Quinn Tranf83adb62014-04-11 16:54:43 -04003124 /*
3125 * T10Dif: ctio_crc2_to_fw overlay ontop of
3126 * ctio7_to_24xx
3127 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003128 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003129 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003130 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003131 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003132 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003133 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003134 CTIO7_FLAGS_DATA_IN);
3135
3136 /* Real finish is ctio_m1's finish */
3137 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003138 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003139 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003140
3141 /* qlt_24xx_init_ctio_to_isp will correct
3142 * all neccessary fields that's part of CTIO7.
3143 * There should be no residual of CTIO-CRC2 data.
3144 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003145 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3146 &prm);
3147 pr_debug("Status CTIO7: %p\n", ctio);
3148 }
3149 } else
3150 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3151
3152
3153 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003154 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003155
Himanshu Madhani63163e02014-09-25 06:14:59 -04003156 /* Memory Barrier */
3157 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003158 qla2x00_start_iocbs(vha, vha->req);
3159 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3160
3161 return 0;
3162
3163out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003164 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003165 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3166
3167 return res;
3168}
3169EXPORT_SYMBOL(qlt_xmit_response);
3170
3171int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3172{
3173 struct ctio7_to_24xx *pkt;
3174 struct scsi_qla_host *vha = cmd->vha;
3175 struct qla_hw_data *ha = vha->hw;
3176 struct qla_tgt *tgt = cmd->tgt;
3177 struct qla_tgt_prm prm;
3178 unsigned long flags;
3179 int res = 0;
3180
3181 memset(&prm, 0, sizeof(prm));
3182 prm.cmd = cmd;
3183 prm.tgt = tgt;
3184 prm.sg = NULL;
3185 prm.req_cnt = 1;
3186
3187 /* Send marker if required */
3188 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
3189 return -EIO;
3190
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003191 /* Calculate number of entries and segments required */
3192 if (qlt_pci_map_calc_cnt(&prm) != 0)
3193 return -EAGAIN;
3194
3195 spin_lock_irqsave(&ha->hardware_lock, flags);
3196
Quinn Tranec7193e2017-03-15 09:48:55 -07003197 if (!ha->flags.fw_started || (cmd->reset_count != ha->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003198 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003199 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003200 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003201 * previous life, just abort the processing.
3202 */
3203 cmd->state = QLA_TGT_STATE_NEED_DATA;
3204 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3205 ql_dbg(ql_dbg_async, vha, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003206 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3207 vha->flags.online, qla2x00_reset_active(vha),
3208 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003209 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3210 return 0;
3211 }
3212
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003213 /* Does F/W have an IOCBs for this request */
3214 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
3215 if (res != 0)
3216 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003217 if (cmd->se_cmd.prot_op)
3218 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3219 else
3220 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003221
Quinn Tran810e30b2015-06-10 11:05:20 -04003222 if (unlikely(res != 0)) {
3223 vha->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003224 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003225 }
3226
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003227 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003228 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003229 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003230
3231 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3232 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003233
3234 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003235 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003236
Himanshu Madhani63163e02014-09-25 06:14:59 -04003237 /* Memory Barrier */
3238 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003239 qla2x00_start_iocbs(vha, vha->req);
3240 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3241
3242 return res;
3243
3244out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003245 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003246 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3247
3248 return res;
3249}
3250EXPORT_SYMBOL(qlt_rdy_to_xfer);
3251
Quinn Tranf83adb62014-04-11 16:54:43 -04003252
3253/*
Quinn Tranbe251522017-03-15 09:48:49 -07003254 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003255 */
Quinn Tranbe251522017-03-15 09:48:49 -07003256static void
Quinn Tranf83adb62014-04-11 16:54:43 -04003257qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003258 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003259{
3260 uint8_t *ap = &sts->actual_dif[0];
3261 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003262 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003263 uint8_t scsi_status, sense_key, asc, ascq;
3264 unsigned long flags;
Quinn Tranf83adb62014-04-11 16:54:43 -04003265
Quinn Tranbe251522017-03-15 09:48:49 -07003266 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003267
Quinn Tranbe251522017-03-15 09:48:49 -07003268 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3269 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3270 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003271
Quinn Tranbe251522017-03-15 09:48:49 -07003272 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3273 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3274 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003275
Quinn Tranbe251522017-03-15 09:48:49 -07003276 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3277 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003278
Quinn Tranbe251522017-03-15 09:48:49 -07003279 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003280
Quinn Tranbe251522017-03-15 09:48:49 -07003281 /* check appl tag */
3282 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003283 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3284 "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]",
3285 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3286 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3287 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3288 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003289
Quinn Tranbe251522017-03-15 09:48:49 -07003290 cmd->dif_err_code = DIF_ERR_APP;
3291 scsi_status = SAM_STAT_CHECK_CONDITION;
3292 sense_key = ABORTED_COMMAND;
3293 asc = 0x10;
3294 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003295 }
3296
3297 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003298 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003299 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3300 "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] ",
3301 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3302 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3303 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3304 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003305
Quinn Tranbe251522017-03-15 09:48:49 -07003306 cmd->dif_err_code = DIF_ERR_REF;
3307 scsi_status = SAM_STAT_CHECK_CONDITION;
3308 sense_key = ABORTED_COMMAND;
3309 asc = 0x10;
3310 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003311 goto out;
3312 }
3313
Quinn Tranbe251522017-03-15 09:48:49 -07003314 /* check guard */
3315 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003316 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3317 "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]",
3318 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3319 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3320 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3321 cmd->atio.u.isp24.fcp_hdr.ox_id);
3322
Quinn Tranbe251522017-03-15 09:48:49 -07003323 cmd->dif_err_code = DIF_ERR_GRD;
3324 scsi_status = SAM_STAT_CHECK_CONDITION;
3325 sense_key = ABORTED_COMMAND;
3326 asc = 0x10;
3327 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003328 }
3329out:
Quinn Tranbe251522017-03-15 09:48:49 -07003330 switch (cmd->state) {
3331 case QLA_TGT_STATE_NEED_DATA:
3332 /* handle_data will load DIF error code */
3333 cmd->state = QLA_TGT_STATE_DATA_IN;
3334 vha->hw->tgt.tgt_ops->handle_data(cmd);
3335 break;
3336 default:
3337 spin_lock_irqsave(&cmd->cmd_lock, flags);
3338 if (cmd->aborted) {
3339 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3340 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3341 break;
3342 }
3343 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003344
Quinn Tranbe251522017-03-15 09:48:49 -07003345 qlt_send_resp_ctio(vha, cmd, scsi_status, sense_key, asc, ascq);
3346 /* assume scsi status gets out on the wire.
3347 * Will not wait for completion.
3348 */
3349 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3350 break;
3351 }
3352}
Quinn Tranf83adb62014-04-11 16:54:43 -04003353
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003354/* If hardware_lock held on entry, might drop it, then reaquire */
3355/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003356static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3357 struct imm_ntfy_from_isp *ntfy)
3358{
3359 struct nack_to_isp *nack;
3360 struct qla_hw_data *ha = vha->hw;
3361 request_t *pkt;
3362 int ret = 0;
3363
3364 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3365 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3366
Quinn Tranec7193e2017-03-15 09:48:55 -07003367 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003368 if (pkt == NULL) {
3369 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3370 "qla_target(%d): %s failed: unable to allocate "
3371 "request packet\n", vha->vp_idx, __func__);
3372 return -ENOMEM;
3373 }
3374
3375 pkt->entry_type = NOTIFY_ACK_TYPE;
3376 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003377 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003378
3379 nack = (struct nack_to_isp *)pkt;
3380 nack->ox_id = ntfy->ox_id;
3381
3382 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3383 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3384 nack->u.isp24.flags = ntfy->u.isp24.flags &
3385 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3386 }
3387
3388 /* terminate */
3389 nack->u.isp24.flags |=
3390 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3391
3392 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3393 nack->u.isp24.status = ntfy->u.isp24.status;
3394 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3395 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3396 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3397 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3398 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3399 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3400
3401 qla2x00_start_iocbs(vha, vha->req);
3402 return ret;
3403}
3404
3405static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3406 struct imm_ntfy_from_isp *imm, int ha_locked)
3407{
3408 unsigned long flags = 0;
3409 int rc;
3410
3411 if (qlt_issue_marker(vha, ha_locked) < 0)
3412 return;
3413
3414 if (ha_locked) {
3415 rc = __qlt_send_term_imm_notif(vha, imm);
3416
3417#if 0 /* Todo */
3418 if (rc == -ENOMEM)
3419 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003420#else
3421 if (rc) {
3422 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003423#endif
3424 goto done;
3425 }
3426
3427 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3428 rc = __qlt_send_term_imm_notif(vha, imm);
3429
3430#if 0 /* Todo */
3431 if (rc == -ENOMEM)
3432 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3433#endif
3434
3435done:
3436 if (!ha_locked)
3437 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3438}
3439
Quinn Tran83548fe2017-06-02 09:12:01 -07003440/*
3441 * If hardware_lock held on entry, might drop it, then reaquire
3442 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3443 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003444static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3445 struct qla_tgt_cmd *cmd,
3446 struct atio_from_isp *atio)
3447{
3448 struct ctio7_to_24xx *ctio24;
3449 struct qla_hw_data *ha = vha->hw;
3450 request_t *pkt;
3451 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003452 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003453
Quinn Tran83548fe2017-06-02 09:12:01 -07003454 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003455
Arun Easib6a029e2014-09-25 06:14:52 -04003456 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003457 if (pkt == NULL) {
3458 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3459 "qla_target(%d): %s failed: unable to allocate "
3460 "request packet\n", vha->vp_idx, __func__);
3461 return -ENOMEM;
3462 }
3463
3464 if (cmd != NULL) {
3465 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3466 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3467 "qla_target(%d): Terminating cmd %p with "
3468 "incorrect state %d\n", vha->vp_idx, cmd,
3469 cmd->state);
3470 } else
3471 ret = 1;
3472 }
3473
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003474 vha->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003475 pkt->entry_count = 1;
3476 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3477
3478 ctio24 = (struct ctio7_to_24xx *)pkt;
3479 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003480 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003481 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003482 ctio24->vp_index = vha->vp_idx;
3483 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3484 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3485 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3486 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3487 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07003488 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003489 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003490 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3491 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003492
3493 /* Most likely, it isn't needed */
3494 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3495 &atio->u.isp24.fcp_cmnd.add_cdb[
3496 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3497 if (ctio24->u.status1.residual != 0)
3498 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3499
Himanshu Madhani63163e02014-09-25 06:14:59 -04003500 /* Memory Barrier */
3501 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003502 qla2x00_start_iocbs(vha, vha->req);
3503 return ret;
3504}
3505
3506static void qlt_send_term_exchange(struct scsi_qla_host *vha,
Quinn Trana07100e2015-12-07 19:48:57 -05003507 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3508 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003509{
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003510 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003511 int rc;
3512
3513 if (qlt_issue_marker(vha, ha_locked) < 0)
3514 return;
3515
3516 if (ha_locked) {
3517 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003518 if (rc == -ENOMEM)
3519 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003520 goto done;
3521 }
3522 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3523 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003524 if (rc == -ENOMEM)
3525 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003526
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003527done:
Quinn Trana07100e2015-12-07 19:48:57 -05003528 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003529 if (cmd->sg_mapped)
3530 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003531 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3532 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003533
3534 if (!ha_locked)
3535 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3536
Quinn Tran7b898542014-04-11 16:54:44 -04003537 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003538}
3539
Quinn Tran33e79972014-09-25 06:14:55 -04003540static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3541{
3542 struct list_head free_list;
3543 struct qla_tgt_cmd *cmd, *tcmd;
3544
3545 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003546 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003547
3548 cmd = tcmd = NULL;
3549 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3550 INIT_LIST_HEAD(&free_list);
3551 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3552
3553 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3554 list_del(&cmd->cmd_list);
3555 /* This cmd was never sent to TCM. There is no need
3556 * to schedule free or call free_cmd
3557 */
3558 qlt_free_cmd(cmd);
3559 vha->hw->tgt.num_qfull_cmds_alloc--;
3560 }
3561 }
3562 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3563}
3564
3565static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3566{
3567 uint32_t total_leaked;
3568
3569 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3570
3571 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3572 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3573
3574 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3575 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003576 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003577
3578 if (IS_P3P_TYPE(vha->hw))
3579 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3580 else
3581 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3582 qla2xxx_wake_dpc(vha);
3583 }
3584
3585}
3586
Quinn Trana07100e2015-12-07 19:48:57 -05003587int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003588{
3589 struct qla_tgt *tgt = cmd->tgt;
3590 struct scsi_qla_host *vha = tgt->vha;
3591 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003592 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003593
3594 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3595 "qla_target(%d): terminating exchange for aborted cmd=%p "
3596 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3597 se_cmd->tag);
3598
Quinn Trana07100e2015-12-07 19:48:57 -05003599 spin_lock_irqsave(&cmd->cmd_lock, flags);
3600 if (cmd->aborted) {
3601 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3602 /*
3603 * It's normal to see 2 calls in this path:
3604 * 1) XFER Rdy completion + CMD_T_ABORT
3605 * 2) TCM TMR - drain_state_list
3606 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003607 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3608 "multiple abort. %p transport_state %x, t_state %x, "
3609 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3610 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003611 return EIO;
3612 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003613 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003614 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003615 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003616
Quinn Trana07100e2015-12-07 19:48:57 -05003617 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3618 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003619}
3620EXPORT_SYMBOL(qlt_abort_cmd);
3621
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003622void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3623{
Quinn Tran5d964832017-01-19 22:27:59 -08003624 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003625
Quinn Tranf83adb62014-04-11 16:54:43 -04003626 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3627 "%s: se_cmd[%p] ox_id %04x\n",
3628 __func__, &cmd->se_cmd,
3629 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003630
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003631 BUG_ON(cmd->cmd_in_wq);
3632
Quinn Trana07100e2015-12-07 19:48:57 -05003633 if (cmd->sg_mapped)
3634 qlt_unmap_sg(cmd->vha, cmd);
3635
Quinn Tran33e79972014-09-25 06:14:55 -04003636 if (!cmd->q_full)
3637 qlt_decr_num_pend_cmds(cmd->vha);
3638
Quinn Tranf83adb62014-04-11 16:54:43 -04003639 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003640 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003641 if (unlikely(cmd->free_sg))
3642 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003643
3644 if (!sess || !sess->se_sess) {
3645 WARN_ON(1);
3646 return;
3647 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003648 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003649 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003650}
3651EXPORT_SYMBOL(qlt_free_cmd);
3652
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003653/*
3654 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3655 */
3656static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3657 struct qla_tgt_cmd *cmd, uint32_t status)
3658{
3659 int term = 0;
3660
Quinn Tranbe251522017-03-15 09:48:49 -07003661 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003662 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003663 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3664 "se_cmd=%p tag[%x] op %#x/%s",
3665 cmd->lba, cmd->lba,
3666 cmd->num_blks, &cmd->se_cmd,
3667 cmd->atio.u.isp24.exchange_addr,
3668 cmd->se_cmd.prot_op,
3669 prot_op_str(cmd->se_cmd.prot_op));
3670
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003671 if (ctio != NULL) {
3672 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3673 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003674 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003675 } else
3676 term = 1;
3677
3678 if (term)
Quinn Trana07100e2015-12-07 19:48:57 -05003679 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003680
3681 return term;
3682}
3683
3684/* ha->hardware_lock supposed to be held on entry */
3685static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3686 uint32_t handle)
3687{
3688 struct qla_hw_data *ha = vha->hw;
3689
3690 handle--;
3691 if (ha->tgt.cmds[handle] != NULL) {
3692 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3693 ha->tgt.cmds[handle] = NULL;
3694 return cmd;
3695 } else
3696 return NULL;
3697}
3698
3699/* ha->hardware_lock supposed to be held on entry */
3700static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3701 uint32_t handle, void *ctio)
3702{
3703 struct qla_tgt_cmd *cmd = NULL;
3704
3705 /* Clear out internal marks */
3706 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3707 CTIO_INTERMEDIATE_HANDLE_MARK);
3708
3709 if (handle != QLA_TGT_NULL_HANDLE) {
Arun Easi667024a2014-09-25 06:14:47 -04003710 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003711 return NULL;
Arun Easi667024a2014-09-25 06:14:47 -04003712
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003713 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05003714 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003715 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3716 "qla_target(%d): Wrong handle %x received\n",
3717 vha->vp_idx, handle);
3718 return NULL;
3719 }
3720 cmd = qlt_get_cmd(vha, handle);
3721 if (unlikely(cmd == NULL)) {
3722 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3723 "qla_target(%d): Suspicious: unable to "
3724 "find the command with handle %x\n", vha->vp_idx,
3725 handle);
3726 return NULL;
3727 }
3728 } else if (ctio != NULL) {
3729 /* We can't get loop ID from CTIO7 */
3730 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3731 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3732 "support NULL handles\n", vha->vp_idx);
3733 return NULL;
3734 }
3735
3736 return cmd;
3737}
3738
Arun Easic0cb4492014-09-25 06:14:51 -04003739/* hardware_lock should be held by caller. */
3740static void
3741qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3742{
3743 struct qla_hw_data *ha = vha->hw;
3744 uint32_t handle;
3745
3746 if (cmd->sg_mapped)
3747 qlt_unmap_sg(vha, cmd);
3748
3749 handle = qlt_make_handle(vha);
3750
3751 /* TODO: fix debug message type and ids. */
3752 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3753 ql_dbg(ql_dbg_io, vha, 0xff00,
3754 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3755 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3756 cmd->write_data_transferred = 0;
3757 cmd->state = QLA_TGT_STATE_DATA_IN;
3758
3759 ql_dbg(ql_dbg_io, vha, 0xff01,
3760 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3761
3762 ha->tgt.tgt_ops->handle_data(cmd);
3763 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003764 } else {
3765 ql_dbg(ql_dbg_io, vha, 0xff03,
3766 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3767 cmd->state);
3768 dump_stack();
3769 }
3770
Quinn Tran1eb42f92017-01-19 22:27:55 -08003771 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003772 ha->tgt.tgt_ops->free_cmd(cmd);
3773}
3774
3775void
3776qlt_host_reset_handler(struct qla_hw_data *ha)
3777{
3778 struct qla_tgt_cmd *cmd;
3779 unsigned long flags;
3780 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3781 scsi_qla_host_t *vha = NULL;
3782 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3783 uint32_t i;
3784
3785 if (!base_vha->hw->tgt.tgt_ops)
3786 return;
3787
3788 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3789 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3790 "Target mode disabled\n");
3791 return;
3792 }
3793
3794 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3795 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3796 base_vha->dpc_flags);
3797
3798 spin_lock_irqsave(&ha->hardware_lock, flags);
3799 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3800 cmd = qlt_get_cmd(base_vha, i);
3801 if (!cmd)
3802 continue;
3803 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3804 vha = cmd->vha;
3805 qlt_abort_cmd_on_host_reset(vha, cmd);
3806 }
3807 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3808}
3809
3810
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003811/*
3812 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3813 */
3814static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3815 uint32_t status, void *ctio)
3816{
3817 struct qla_hw_data *ha = vha->hw;
3818 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003819 struct qla_tgt_cmd *cmd;
3820
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003821 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3822 /* That could happen only in case of an error/reset/abort */
3823 if (status != CTIO_SUCCESS) {
3824 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3825 "Intermediate CTIO received"
3826 " (status %x)\n", status);
3827 }
3828 return;
3829 }
3830
3831 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003832 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003833 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003834
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003835 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003836 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003837
Joern Engelf9b67212014-09-16 16:23:18 -04003838 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003839
3840 if (unlikely(status != CTIO_SUCCESS)) {
3841 switch (status & 0xFFFF) {
3842 case CTIO_LIP_RESET:
3843 case CTIO_TARGET_RESET:
3844 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003845 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003846 case CTIO_TIMEOUT:
3847 case CTIO_INVALID_RX_ID:
3848 /* They are OK */
3849 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3850 "qla_target(%d): CTIO with "
3851 "status %#x received, state %x, se_cmd %p, "
3852 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3853 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3854 status, cmd->state, se_cmd);
3855 break;
3856
3857 case CTIO_PORT_LOGGED_OUT:
3858 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003859 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003860 int logged_out =
3861 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3862
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003863 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003864 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003865 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003866 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003867 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003868
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003869 if (logged_out && cmd->sess) {
3870 /*
3871 * Session is already logged out, but we need
3872 * to notify initiator, who's not aware of this
3873 */
3874 cmd->sess->logout_on_delete = 0;
3875 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003876 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003877 "%s %d %8phC post del sess\n",
3878 __func__, __LINE__, cmd->sess->port_name);
3879
3880 qlt_schedule_sess_for_deletion_lock(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003881 }
3882 break;
3883 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003884 case CTIO_DIF_ERROR: {
3885 struct ctio_crc_from_fw *crc =
3886 (struct ctio_crc_from_fw *)ctio;
3887 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003888 "qla_target(%d): CTIO with DIF_ERROR status %x "
3889 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3890 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003891 vha->vp_idx, status, cmd->state, se_cmd,
3892 *((u64 *)&crc->actual_dif[0]),
3893 *((u64 *)&crc->expected_dif[0]));
3894
Quinn Tranbe251522017-03-15 09:48:49 -07003895 qlt_handle_dif_error(vha, cmd, ctio);
3896 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003897 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003898 default:
3899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003900 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003901 vha->vp_idx, status, cmd->state, se_cmd);
3902 break;
3903 }
3904
Quinn Tran7b898542014-04-11 16:54:44 -04003905
Quinn Tran193b50b2015-12-17 14:57:03 -05003906 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003907 * cmd is already aborted/terminated, we don't
3908 * need to terminate again. The exchange is already
3909 * cleaned up/freed at FW level. Just cleanup at driver
3910 * level.
3911 */
3912 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003913 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003914 cmd->trc_flags |= TRC_CTIO_ERR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003915 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3916 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003917 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003918 }
3919
3920 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003921 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003922 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003923 cmd->state = QLA_TGT_STATE_DATA_IN;
3924
Bart Van Assche52c82822015-07-09 07:23:26 -07003925 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003926 cmd->write_data_transferred = 1;
3927
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003928 ha->tgt.tgt_ops->handle_data(cmd);
3929 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003930 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003931 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003932 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003933 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003934 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003935 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003936 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3937 "qla_target(%d): A command in state (%d) should "
3938 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3939 }
3940
Quinn Tran7b898542014-04-11 16:54:44 -04003941 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003942 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003943 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3944 dump_stack();
3945 }
3946
3947 ha->tgt.tgt_ops->free_cmd(cmd);
3948}
3949
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003950static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3951 uint8_t task_codes)
3952{
3953 int fcp_task_attr;
3954
3955 switch (task_codes) {
3956 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003957 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003958 break;
3959 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003960 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003961 break;
3962 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003963 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003964 break;
3965 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003966 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003967 break;
3968 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003969 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003970 break;
3971 default:
3972 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3973 "qla_target: unknown task code %x, use ORDERED instead\n",
3974 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003975 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003976 break;
3977 }
3978
3979 return fcp_task_attr;
3980}
3981
Quinn Tran5d964832017-01-19 22:27:59 -08003982static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003983 uint8_t *);
3984/*
3985 * Process context for I/O path into tcm_qla2xxx code
3986 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003987static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003988{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003989 scsi_qla_host_t *vha = cmd->vha;
3990 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003991 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08003992 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003993 struct atio_from_isp *atio = &cmd->atio;
3994 unsigned char *cdb;
3995 unsigned long flags;
3996 uint32_t data_length;
3997 int ret, fcp_task_attr, data_dir, bidi = 0;
3998
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003999 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004000 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004001 if (tgt->tgt_stop)
4002 goto out_term;
4003
Quinn Tran193b50b2015-12-17 14:57:03 -05004004 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004005 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4006 "cmd with tag %u is aborted\n",
4007 cmd->atio.u.isp24.exchange_addr);
4008 goto out_term;
4009 }
4010
Quinn Trana07100e2015-12-07 19:48:57 -05004011 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004012 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004013 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004014 cmd->unpacked_lun = scsilun_to_int(
4015 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4016
4017 if (atio->u.isp24.fcp_cmnd.rddata &&
4018 atio->u.isp24.fcp_cmnd.wrdata) {
4019 bidi = 1;
4020 data_dir = DMA_TO_DEVICE;
4021 } else if (atio->u.isp24.fcp_cmnd.rddata)
4022 data_dir = DMA_FROM_DEVICE;
4023 else if (atio->u.isp24.fcp_cmnd.wrdata)
4024 data_dir = DMA_TO_DEVICE;
4025 else
4026 data_dir = DMA_NONE;
4027
4028 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4029 atio->u.isp24.fcp_cmnd.task_attr);
4030 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4031 &atio->u.isp24.fcp_cmnd.add_cdb[
4032 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4033
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004034 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4035 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004036 if (ret != 0)
4037 goto out_term;
4038 /*
4039 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4040 */
Quinn Tran75601512015-12-17 14:57:04 -05004041 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004042 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004043 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004044 return;
4045
4046out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004047 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004048 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004049 * cmd has not sent to target yet, so pass NULL as the second
4050 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004051 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004052 cmd->trc_flags |= TRC_DO_WORK_ERR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004053 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05004054 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004055
4056 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004057 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Jörn Engel08234e32013-06-12 16:27:54 -04004058 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004059
4060 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004061 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004062 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004063}
4064
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004065static void qlt_do_work(struct work_struct *work)
4066{
4067 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004068 scsi_qla_host_t *vha = cmd->vha;
4069 unsigned long flags;
4070
4071 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4072 list_del(&cmd->cmd_list);
4073 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004074
4075 __qlt_do_work(cmd);
4076}
4077
4078static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004079 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004080 struct atio_from_isp *atio)
4081{
4082 struct se_session *se_sess = sess->se_sess;
4083 struct qla_tgt_cmd *cmd;
4084 int tag;
4085
4086 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4087 if (tag < 0)
4088 return NULL;
4089
4090 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4091 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4092
4093 memcpy(&cmd->atio, atio, sizeof(*atio));
4094 cmd->state = QLA_TGT_STATE_NEW;
4095 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004096 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004097 cmd->vha = vha;
4098 cmd->se_cmd.map_tag = tag;
4099 cmd->sess = sess;
4100 cmd->loop_id = sess->loop_id;
4101 cmd->conf_compl_supported = sess->conf_compl_supported;
4102
Quinn Tran1eb42f92017-01-19 22:27:55 -08004103 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004104 cmd->jiffies_at_alloc = get_jiffies_64();
4105
4106 cmd->reset_count = vha->hw->chip_reset;
4107
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004108 return cmd;
4109}
4110
4111static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
4112 uint16_t);
4113
4114static void qlt_create_sess_from_atio(struct work_struct *work)
4115{
4116 struct qla_tgt_sess_op *op = container_of(work,
4117 struct qla_tgt_sess_op, work);
4118 scsi_qla_host_t *vha = op->vha;
4119 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004120 struct fc_port *sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004121 struct qla_tgt_cmd *cmd;
4122 unsigned long flags;
4123 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4124
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004125 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4126 list_del(&op->cmd_list);
4127 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4128
4129 if (op->aborted) {
4130 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4131 "sess_op with tag %u is aborted\n",
4132 op->atio.u.isp24.exchange_addr);
4133 goto out_term;
4134 }
4135
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004136 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004137 "qla_target(%d): Unable to find wwn login"
4138 " (s_id %x:%x:%x), trying to create it manually\n",
4139 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004140
4141 if (op->atio.u.raw.entry_count > 1) {
4142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004143 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004144 goto out_term;
4145 }
4146
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004147 sess = qlt_make_local_sess(vha, s_id);
4148 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004149
4150 if (!sess)
4151 goto out_term;
4152 /*
4153 * Now obtain a pre-allocated session tag using the original op->atio
4154 * packet header, and dispatch into __qlt_do_work() using the existing
4155 * process context.
4156 */
4157 cmd = qlt_get_tag(vha, sess, &op->atio);
4158 if (!cmd) {
4159 spin_lock_irqsave(&ha->hardware_lock, flags);
4160 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
Quinn Tran5d964832017-01-19 22:27:59 -08004161 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004162 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4163 kfree(op);
4164 return;
4165 }
Quinn Tran726b8542017-01-19 22:28:00 -08004166
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004167 /*
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004168 * __qlt_do_work() will call qlt_put_sess() to release
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004169 * the extra reference taken above by qlt_make_local_sess()
4170 */
4171 __qlt_do_work(cmd);
4172 kfree(op);
4173 return;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004174out_term:
4175 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05004176 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004177 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4178 kfree(op);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004179}
4180
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004181/* ha->hardware_lock supposed to be held on entry */
4182static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4183 struct atio_from_isp *atio)
4184{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004185 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004186 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004187 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004188 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004189 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004190
4191 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004192 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004193 "New command while device %p is shutting down\n", tgt);
4194 return -EFAULT;
4195 }
4196
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004197 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4198 if (unlikely(!sess)) {
4199 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4200 GFP_ATOMIC);
4201 if (!op)
4202 return -ENOMEM;
4203
4204 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04004205 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004206
Quinn Tran8b631d82017-06-02 09:11:54 -07004207 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004208 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
Quinn Tran8b631d82017-06-02 09:11:54 -07004209 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004210
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004211 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4212 queue_work(qla_tgt_wq, &op->work);
4213 return 0;
4214 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004215
4216 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4217 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004218 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004219 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004220 "New command while old session %p is being deleted\n",
4221 sess);
4222 return -EFAULT;
4223 }
4224
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004225 /*
4226 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4227 */
Quinn Tran726b8542017-01-19 22:28:00 -08004228 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004229 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004230 "%s: kref_get fail, %8phC oxid %x \n",
4231 __func__, sess->port_name,
4232 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4233 return -EFAULT;
4234 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004235
4236 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004237 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004238 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004239 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004240 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4241 ha->tgt.tgt_ops->put_sess(sess);
4242 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004243 return -ENOMEM;
4244 }
4245
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004246 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004247 cmd->trc_flags |= TRC_NEW_CMD;
Quinn Tran5327c7d2016-02-10 18:59:14 -05004248 cmd->se_cmd.cpuid = ha->msix_count ?
4249 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004250
Quinn Tran726b8542017-01-19 22:28:00 -08004251 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004252 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004253 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004254
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004255 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Tranfb3269b2015-12-17 14:57:06 -05004256 if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004257 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4258 queue_work_on(smp_processor_id(), qla_tgt_wq,
4259 &cmd->work);
4260 else
4261 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4262 &cmd->work);
4263 } else {
4264 queue_work(qla_tgt_wq, &cmd->work);
4265 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004266 return 0;
4267
4268}
4269
4270/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004271static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004272 int fn, void *iocb, int flags)
4273{
4274 struct scsi_qla_host *vha = sess->vha;
4275 struct qla_hw_data *ha = vha->hw;
4276 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004277 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004278 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004279
4280 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4281 if (!mcmd) {
4282 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4283 "qla_target(%d): Allocation of management "
4284 "command failed, some commands and their data could "
4285 "leak\n", vha->vp_idx);
4286 return -ENOMEM;
4287 }
4288 memset(mcmd, 0, sizeof(*mcmd));
4289 mcmd->sess = sess;
4290
4291 if (iocb) {
4292 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4293 sizeof(mcmd->orig_iocb.imm_ntfy));
4294 }
4295 mcmd->tmr_func = fn;
4296 mcmd->flags = flags;
Arun Easib6a029e2014-09-25 06:14:52 -04004297 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004298
4299 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004300 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004301 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4302 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004303 }
4304
Quinn Tranbe92fc32017-01-19 22:27:54 -08004305 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004306 if (res != 0) {
4307 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4308 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4309 sess->vha->vp_idx, res);
4310 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4311 return -EFAULT;
4312 }
4313
4314 return 0;
4315}
4316
4317/* ha->hardware_lock supposed to be held on entry */
4318static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4319{
4320 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4321 struct qla_hw_data *ha = vha->hw;
4322 struct qla_tgt *tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004323 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004324 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004325 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004326 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004327
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004328 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004329
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004330 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004331
4332 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004333 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4334 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004335 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4336
Quinn Tranf775bd12017-06-02 09:11:59 -07004337 unpacked_lun =
4338 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004339
4340 if (!sess) {
4341 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4342 "qla_target(%d): task mgmt fn 0x%x for "
4343 "non-existant session\n", vha->vp_idx, fn);
4344 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4345 sizeof(struct atio_from_isp));
4346 }
4347
Quinn Tran726b8542017-01-19 22:28:00 -08004348 if (sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004349 return -EFAULT;
4350
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004351 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4352}
4353
4354/* ha->hardware_lock supposed to be held on entry */
4355static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004356 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004357{
4358 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4359 struct qla_hw_data *ha = vha->hw;
4360 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004361 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004362 int rc;
4363
4364 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4365 if (mcmd == NULL) {
4366 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4367 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4368 vha->vp_idx, __func__);
4369 return -ENOMEM;
4370 }
4371 memset(mcmd, 0, sizeof(*mcmd));
4372
4373 mcmd->sess = sess;
4374 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4375 sizeof(mcmd->orig_iocb.imm_ntfy));
4376
Quinn Tranf775bd12017-06-02 09:11:59 -07004377 unpacked_lun =
4378 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Arun Easi80187f82014-09-25 06:14:53 -04004379 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004380 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004381
Quinn Tranbe92fc32017-01-19 22:27:54 -08004382 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004383 le16_to_cpu(iocb->u.isp2x.seq_id));
4384 if (rc != 0) {
4385 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4386 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4387 vha->vp_idx, rc);
4388 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4389 return -EFAULT;
4390 }
4391
4392 return 0;
4393}
4394
4395/* ha->hardware_lock supposed to be held on entry */
4396static int qlt_abort_task(struct scsi_qla_host *vha,
4397 struct imm_ntfy_from_isp *iocb)
4398{
4399 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004400 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004401 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004402 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004403
4404 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4405
Quinn Tran75601512015-12-17 14:57:04 -05004406 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004407 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004408 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4409
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004410 if (sess == NULL) {
4411 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4412 "qla_target(%d): task abort for unexisting "
4413 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004414 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004415 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4416 }
4417
4418 return __qlt_abort_task(vha, iocb, sess);
4419}
4420
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004421void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4422{
Quinn Tran726b8542017-01-19 22:28:00 -08004423 if (rc != MBS_COMMAND_COMPLETE) {
4424 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4425 "%s: se_sess %p / sess %p from"
4426 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4427 " LOGO failed: %#x\n",
4428 __func__,
4429 fcport->se_sess,
4430 fcport,
4431 fcport->port_name, fcport->loop_id,
4432 fcport->d_id.b.domain, fcport->d_id.b.area,
4433 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004434 }
Quinn Tran726b8542017-01-19 22:28:00 -08004435
4436 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004437}
4438
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004439/*
4440* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4441*
4442* Schedules sessions with matching port_id/loop_id but different wwn for
4443* deletion. Returns existing session with matching wwn if present.
4444* Null otherwise.
4445*/
Quinn Tran726b8542017-01-19 22:28:00 -08004446struct fc_port *
4447qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004448 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004449{
Quinn Tran5d964832017-01-19 22:27:59 -08004450 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004451 uint64_t other_wwn;
4452
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004453 *conflict_sess = NULL;
4454
Quinn Tran5d964832017-01-19 22:27:59 -08004455 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004456
4457 other_wwn = wwn_to_u64(other_sess->port_name);
4458
4459 if (wwn == other_wwn) {
4460 WARN_ON(sess);
4461 sess = other_sess;
4462 continue;
4463 }
4464
4465 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004466 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004467 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004468 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004469 "Invalidating sess %p loop_id %d wwn %llx.\n",
4470 other_sess, other_sess->loop_id, other_wwn);
4471
4472 /*
4473 * logout_on_delete is set by default, but another
4474 * session that has the same s_id/loop_id combo
4475 * might have cleared it when requested this session
4476 * deletion, so don't touch it
4477 */
4478 qlt_schedule_sess_for_deletion(other_sess, true);
4479 } else {
4480 /*
4481 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004482 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004483 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004484 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004485 "Invalidating sess %p loop_id %d wwn %llx.\n",
4486 other_sess, other_sess->loop_id, other_wwn);
4487
4488
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004489 other_sess->keep_nport_handle = 1;
4490 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004491 qlt_schedule_sess_for_deletion(other_sess,
4492 true);
4493 }
4494 continue;
4495 }
4496
4497 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004498 if ((loop_id == other_sess->loop_id) &&
4499 (loop_id != FC_NO_LOOP_ID)) {
4500 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004501 "Invalidating sess %p loop_id %d wwn %llx.\n",
4502 other_sess, other_sess->loop_id, other_wwn);
4503
4504 /* Same loop_id but different s_id
4505 * Ok to kill and logout */
4506 qlt_schedule_sess_for_deletion(other_sess, true);
4507 }
4508 }
4509
4510 return sess;
4511}
4512
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004513/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4514static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4515{
4516 struct qla_tgt_sess_op *op;
4517 struct qla_tgt_cmd *cmd;
4518 uint32_t key;
4519 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004520 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004521
4522 key = (((u32)s_id->b.domain << 16) |
4523 ((u32)s_id->b.area << 8) |
4524 ((u32)s_id->b.al_pa));
4525
Quinn Tran8b631d82017-06-02 09:11:54 -07004526 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004527 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4528 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004529
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004530 if (op_key == key) {
4531 op->aborted = true;
4532 count++;
4533 }
4534 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004535
4536 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4537 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4538 if (op_key == key) {
4539 op->aborted = true;
4540 count++;
4541 }
4542 }
4543
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004544 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4545 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4546 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004547 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004548 count++;
4549 }
4550 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004551 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004552
4553 return count;
4554}
4555
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004556/*
4557 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4558 */
4559static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4560 struct imm_ntfy_from_isp *iocb)
4561{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004562 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004563 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004564 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004565 uint64_t wwn;
4566 port_id_t port_id;
4567 uint16_t loop_id;
4568 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004569 int res = 0;
Quinn Tran5d964832017-01-19 22:27:59 -08004570 struct qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004571 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004572
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004573 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4574
4575 port_id.b.domain = iocb->u.isp24.port_id[2];
4576 port_id.b.area = iocb->u.isp24.port_id[1];
4577 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4578 port_id.b.rsvd_1 = 0;
4579
4580 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4581
Quinn Tran726b8542017-01-19 22:28:00 -08004582 ql_dbg(ql_dbg_disc, vha, 0xf026,
4583 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4584 vha->vp_idx, iocb->u.isp24.port_id[2],
4585 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4586 iocb->u.isp24.status_subcode, loop_id,
4587 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004588
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004589 /* res = 1 means ack at the end of thread
4590 * res = 0 means ack async/later.
4591 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004592 switch (iocb->u.isp24.status_subcode) {
4593 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004594
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004595 /* Mark all stale commands in qla_tgt_wq for deletion */
4596 abort_cmds_for_s_id(vha, &port_id);
4597
Quinn Tran75601512015-12-17 14:57:04 -05004598 if (wwn) {
4599 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004600 sess = qlt_find_sess_invalidate_other(vha, wwn,
4601 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004602 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4603 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004604
Quinn Tran726b8542017-01-19 22:28:00 -08004605 if (IS_SW_RESV_ADDR(port_id)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004606 res = 1;
4607 break;
4608 }
4609
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004610 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4611 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004612 qlt_send_term_imm_notif(vha, iocb, 1);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004613 break;
4614 }
4615
4616 res = 0;
4617
Quinn Tran726b8542017-01-19 22:28:00 -08004618 if (conflict_sess) {
4619 conflict_sess->login_gen++;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004620 qlt_plogi_ack_link(vha, pla, conflict_sess,
Quinn Tran726b8542017-01-19 22:28:00 -08004621 QLT_PLOGI_LINK_CONFLICT);
4622 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004623
Quinn Tran726b8542017-01-19 22:28:00 -08004624 if (!sess) {
4625 pla->ref_count++;
4626 qla24xx_post_newsess_work(vha, &port_id,
4627 iocb->u.isp24.port_name, pla);
4628 res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004629 break;
Quinn Tran726b8542017-01-19 22:28:00 -08004630 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004631
4632 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Quinn Tran726b8542017-01-19 22:28:00 -08004633 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4634 sess->d_id = port_id;
4635 sess->login_gen++;
4636
4637 switch (sess->disc_state) {
4638 case DSC_DELETED:
4639 qlt_plogi_ack_unref(vha, pla);
4640 break;
4641
4642 default:
4643 /*
4644 * Under normal circumstances we want to release nport handle
4645 * during LOGO process to avoid nport handle leaks inside FW.
4646 * The exception is when LOGO is done while another PLOGI with
4647 * the same nport handle is waiting as might be the case here.
4648 * Note: there is always a possibily of a race where session
4649 * deletion has already started for other reasons (e.g. ACL
4650 * removal) and now PLOGI arrives:
4651 * 1. if PLOGI arrived in FW after nport handle has been freed,
4652 * FW must have assigned this PLOGI a new/same handle and we
4653 * can proceed ACK'ing it as usual when session deletion
4654 * completes.
4655 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4656 * bit reached it, the handle has now been released. We'll
4657 * get an error when we ACK this PLOGI. Nothing will be sent
4658 * back to initiator. Initiator should eventually retry
4659 * PLOGI and situation will correct itself.
4660 */
4661 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4662 (sess->d_id.b24 == port_id.b24));
4663
Quinn Tran83548fe2017-06-02 09:12:01 -07004664 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4665 "%s %d %8phC post del sess\n",
4666 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004667
4668
4669 qlt_schedule_sess_for_deletion_lock(sess);
4670 break;
4671 }
4672
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004673 break;
4674
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004675 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004676 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4677
Quinn Tran75601512015-12-17 14:57:04 -05004678 if (wwn) {
4679 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004680 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4681 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004682 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4683 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004684
4685 if (conflict_sess) {
4686 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4687 "PRLI with conflicting sess %p port %8phC\n",
4688 conflict_sess, conflict_sess->port_name);
4689 qlt_send_term_imm_notif(vha, iocb, 1);
4690 res = 0;
4691 break;
4692 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004693
4694 if (sess != NULL) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004695 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4696 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004697 /*
4698 * Impatient initiator sent PRLI before last
4699 * PLOGI could finish. Will force him to re-try,
4700 * while last one finishes.
4701 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004702 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004703 "sess %p PRLI received, before plogi ack.\n",
4704 sess);
4705 qlt_send_term_imm_notif(vha, iocb, 1);
4706 res = 0;
4707 break;
4708 }
4709
4710 /*
4711 * This shouldn't happen under normal circumstances,
4712 * since we have deleted the old session during PLOGI
4713 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004714 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004715 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4716 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4717
4718 sess->local = 0;
4719 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004720 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004721 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004722
4723 if (wd3_lo & BIT_7)
4724 sess->conf_compl_supported = 1;
4725
Quinn Tran726b8542017-01-19 22:28:00 -08004726 if ((wd3_lo & BIT_4) == 0)
4727 sess->port_type = FCT_INITIATOR;
4728 else
4729 sess->port_type = FCT_TARGET;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004730 }
4731 res = 1; /* send notify ack */
4732
4733 /* Make session global (not used in fabric mode) */
4734 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004735 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004736 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004737 "%s %d %8phC post nack\n",
4738 __func__, __LINE__, sess->port_name);
4739 qla24xx_post_nack_work(vha, sess, iocb,
4740 SRB_NACK_PRLI);
4741 res = 0;
4742 } else {
4743 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4744 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4745 qla2xxx_wake_dpc(vha);
4746 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004747 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004748 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004749 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004750 "%s %d %8phC post nack\n",
4751 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004752 qla24xx_post_nack_work(vha, sess, iocb,
4753 SRB_NACK_PRLI);
4754 res = 0;
4755 }
4756 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004757 break;
4758
Quinn Tran41dc5292017-01-19 22:28:03 -08004759 case ELS_TPRLO:
4760 if (le16_to_cpu(iocb->u.isp24.flags) &
4761 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4762 loop_id = 0xFFFF;
4763 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4764 res = 1;
4765 break;
4766 }
4767 /* drop through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004768 case ELS_LOGO:
4769 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004770 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4771 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4772 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4773
4774 if (sess) {
4775 sess->login_gen++;
4776 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004777 sess->logo_ack_needed = 1;
4778 memcpy(sess->iocb, iocb, IOCB_SIZE);
4779 }
4780
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004781 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004782
Quinn Tran83548fe2017-06-02 09:12:01 -07004783 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08004784 "%s: logo %llx res %d sess %p ",
4785 __func__, wwn, res, sess);
4786 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004787 /*
4788 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4789 * for LOGO_ACK & sess delete
4790 */
Quinn Tran726b8542017-01-19 22:28:00 -08004791 BUG_ON(!sess);
4792 res = 0;
4793 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004794 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004795 if (sess) {
4796 qlt_schedule_sess_for_deletion_lock(sess);
4797 res = 0;
4798 }
4799 /* else logo will be ack */
4800 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004801 break;
4802 case ELS_PDISC:
4803 case ELS_ADISC:
4804 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004805 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004806 if (tgt->link_reinit_iocb_pending) {
4807 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4808 0, 0, 0, 0, 0, 0);
4809 tgt->link_reinit_iocb_pending = 0;
4810 }
Quinn Tran726b8542017-01-19 22:28:00 -08004811
4812 sess = qla2x00_find_fcport_by_wwpn(vha,
4813 iocb->u.isp24.port_name, 1);
4814 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004815 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08004816 "sess %p lid %d|%d DS %d LS %d\n",
4817 sess, sess->loop_id, loop_id,
4818 sess->disc_state, sess->fw_login_state);
4819 }
4820
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004821 res = 1; /* send notify ack */
4822 break;
4823 }
4824
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004825 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004826 default:
4827 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4828 "qla_target(%d): Unsupported ELS command %x "
4829 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4830 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4831 break;
4832 }
4833
4834 return res;
4835}
4836
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004837/*
4838 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4839 */
4840static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4841 struct imm_ntfy_from_isp *iocb)
4842{
4843 struct qla_hw_data *ha = vha->hw;
4844 uint32_t add_flags = 0;
4845 int send_notify_ack = 1;
4846 uint16_t status;
4847
4848 status = le16_to_cpu(iocb->u.isp2x.status);
4849 switch (status) {
4850 case IMM_NTFY_LIP_RESET:
4851 {
4852 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4853 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4854 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4855 iocb->u.isp24.status_subcode);
4856
4857 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4858 send_notify_ack = 0;
4859 break;
4860 }
4861
4862 case IMM_NTFY_LIP_LINK_REINIT:
4863 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004864 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004865 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4866 "qla_target(%d): LINK REINIT (loop %#x, "
4867 "subcode %x)\n", vha->vp_idx,
4868 le16_to_cpu(iocb->u.isp24.nport_handle),
4869 iocb->u.isp24.status_subcode);
4870 if (tgt->link_reinit_iocb_pending) {
4871 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4872 0, 0, 0, 0, 0, 0);
4873 }
4874 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4875 tgt->link_reinit_iocb_pending = 1;
4876 /*
4877 * QLogic requires to wait after LINK REINIT for possible
4878 * PDISC or ADISC ELS commands
4879 */
4880 send_notify_ack = 0;
4881 break;
4882 }
4883
4884 case IMM_NTFY_PORT_LOGOUT:
4885 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4886 "qla_target(%d): Port logout (loop "
4887 "%#x, subcode %x)\n", vha->vp_idx,
4888 le16_to_cpu(iocb->u.isp24.nport_handle),
4889 iocb->u.isp24.status_subcode);
4890
4891 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4892 send_notify_ack = 0;
4893 /* The sessions will be cleared in the callback, if needed */
4894 break;
4895
4896 case IMM_NTFY_GLBL_TPRLO:
4897 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4898 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4899 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4900 send_notify_ack = 0;
4901 /* The sessions will be cleared in the callback, if needed */
4902 break;
4903
4904 case IMM_NTFY_PORT_CONFIG:
4905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4906 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4907 status);
4908 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4909 send_notify_ack = 0;
4910 /* The sessions will be cleared in the callback, if needed */
4911 break;
4912
4913 case IMM_NTFY_GLBL_LOGO:
4914 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4915 "qla_target(%d): Link failure detected\n",
4916 vha->vp_idx);
4917 /* I_T nexus loss */
4918 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4919 send_notify_ack = 0;
4920 break;
4921
4922 case IMM_NTFY_IOCB_OVERFLOW:
4923 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4924 "qla_target(%d): Cannot provide requested "
4925 "capability (IOCB overflowed the immediate notify "
4926 "resource count)\n", vha->vp_idx);
4927 break;
4928
4929 case IMM_NTFY_ABORT_TASK:
4930 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4931 "qla_target(%d): Abort Task (S %08x I %#x -> "
4932 "L %#x)\n", vha->vp_idx,
4933 le16_to_cpu(iocb->u.isp2x.seq_id),
4934 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4935 le16_to_cpu(iocb->u.isp2x.lun));
4936 if (qlt_abort_task(vha, iocb) == 0)
4937 send_notify_ack = 0;
4938 break;
4939
4940 case IMM_NTFY_RESOURCE:
4941 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4942 "qla_target(%d): Out of resources, host %ld\n",
4943 vha->vp_idx, vha->host_no);
4944 break;
4945
4946 case IMM_NTFY_MSG_RX:
4947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4948 "qla_target(%d): Immediate notify task %x\n",
4949 vha->vp_idx, iocb->u.isp2x.task_flags);
4950 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4951 send_notify_ack = 0;
4952 break;
4953
4954 case IMM_NTFY_ELS:
4955 if (qlt_24xx_handle_els(vha, iocb) == 0)
4956 send_notify_ack = 0;
4957 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004958 default:
4959 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4960 "qla_target(%d): Received unknown immediate "
4961 "notify status %x\n", vha->vp_idx, status);
4962 break;
4963 }
4964
4965 if (send_notify_ack)
4966 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4967}
4968
4969/*
4970 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4971 * This function sends busy to ISP 2xxx or 24xx.
4972 */
Quinn Tran33e79972014-09-25 06:14:55 -04004973static int __qlt_send_busy(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004974 struct atio_from_isp *atio, uint16_t status)
4975{
4976 struct ctio7_to_24xx *ctio24;
4977 struct qla_hw_data *ha = vha->hw;
4978 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08004979 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05004980 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004981
Quinn Tran75601512015-12-17 14:57:04 -05004982 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004983 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4984 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004985 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004986 if (!sess) {
Quinn Trana07100e2015-12-07 19:48:57 -05004987 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004988 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004989 }
4990 /* Sending marker isn't necessary, since we called from ISR */
4991
4992 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
4993 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04004994 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004995 "qla_target(%d): %s failed: unable to allocate "
4996 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04004997 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004998 }
4999
Himanshu Madhanice1025c2015-12-17 14:56:58 -05005000 vha->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005001 pkt->entry_count = 1;
5002 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5003
5004 ctio24 = (struct ctio7_to_24xx *)pkt;
5005 ctio24->entry_type = CTIO_TYPE7;
5006 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005007 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005008 ctio24->vp_index = vha->vp_idx;
5009 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5010 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5011 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5012 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5013 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07005014 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005015 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5016 CTIO7_FLAGS_DONT_RET_CTIO);
5017 /*
5018 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5019 * if the explicit conformation is used.
5020 */
5021 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5022 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005023 /* Memory Barrier */
5024 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005025 qla2x00_start_iocbs(vha, vha->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005026 return 0;
5027}
5028
5029/*
5030 * This routine is used to allocate a command for either a QFull condition
5031 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5032 * out previously.
5033 */
5034static void
5035qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5036 struct atio_from_isp *atio, uint16_t status, int qfull)
5037{
5038 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5039 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005040 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005041 struct se_session *se_sess;
5042 struct qla_tgt_cmd *cmd;
5043 int tag;
5044
5045 if (unlikely(tgt->tgt_stop)) {
5046 ql_dbg(ql_dbg_io, vha, 0x300a,
5047 "New command while device %p is shutting down\n", tgt);
5048 return;
5049 }
5050
5051 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5052 vha->hw->tgt.num_qfull_cmds_dropped++;
5053 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005054 vha->qla_stats.stat_max_qfull_cmds_dropped)
5055 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005056 vha->hw->tgt.num_qfull_cmds_dropped;
5057
5058 ql_dbg(ql_dbg_io, vha, 0x3068,
5059 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5060 vha->vp_idx, __func__,
5061 vha->hw->tgt.num_qfull_cmds_dropped);
5062
5063 qlt_chk_exch_leak_thresh_hold(vha);
5064 return;
5065 }
5066
5067 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5068 (vha, atio->u.isp24.fcp_hdr.s_id);
5069 if (!sess)
5070 return;
5071
5072 se_sess = sess->se_sess;
5073
5074 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5075 if (tag < 0)
5076 return;
5077
5078 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5079 if (!cmd) {
5080 ql_dbg(ql_dbg_io, vha, 0x3009,
5081 "qla_target(%d): %s: Allocation of cmd failed\n",
5082 vha->vp_idx, __func__);
5083
5084 vha->hw->tgt.num_qfull_cmds_dropped++;
5085 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005086 vha->qla_stats.stat_max_qfull_cmds_dropped)
5087 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005088 vha->hw->tgt.num_qfull_cmds_dropped;
5089
5090 qlt_chk_exch_leak_thresh_hold(vha);
5091 return;
5092 }
5093
5094 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5095
5096 qlt_incr_num_pend_cmds(vha);
5097 INIT_LIST_HEAD(&cmd->cmd_list);
5098 memcpy(&cmd->atio, atio, sizeof(*atio));
5099
5100 cmd->tgt = vha->vha_tgt.qla_tgt;
5101 cmd->vha = vha;
5102 cmd->reset_count = vha->hw->chip_reset;
5103 cmd->q_full = 1;
5104
5105 if (qfull) {
5106 cmd->q_full = 1;
5107 /* NOTE: borrowing the state field to carry the status */
5108 cmd->state = status;
5109 } else
5110 cmd->term_exchg = 1;
5111
5112 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5113
5114 vha->hw->tgt.num_qfull_cmds_alloc++;
5115 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005116 vha->qla_stats.stat_max_qfull_cmds_alloc)
5117 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005118 vha->hw->tgt.num_qfull_cmds_alloc;
5119}
5120
5121int
5122qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5123{
5124 struct qla_hw_data *ha = vha->hw;
5125 unsigned long flags;
5126 struct qla_tgt_cmd *cmd, *tcmd;
5127 struct list_head free_list;
5128 int rc = 0;
5129
5130 if (list_empty(&ha->tgt.q_full_list))
5131 return 0;
5132
5133 INIT_LIST_HEAD(&free_list);
5134
5135 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5136
5137 if (list_empty(&ha->tgt.q_full_list)) {
5138 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5139 return 0;
5140 }
5141
5142 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5143 if (cmd->q_full)
5144 /* cmd->state is a borrowed field to hold status */
5145 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5146 else if (cmd->term_exchg)
5147 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5148
5149 if (rc == -ENOMEM)
5150 break;
5151
5152 if (cmd->q_full)
5153 ql_dbg(ql_dbg_io, vha, 0x3006,
5154 "%s: busy sent for ox_id[%04x]\n", __func__,
5155 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5156 else if (cmd->term_exchg)
5157 ql_dbg(ql_dbg_io, vha, 0x3007,
5158 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5159 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5160 else
5161 ql_dbg(ql_dbg_io, vha, 0x3008,
5162 "%s: Unexpected cmd in QFull list %p\n", __func__,
5163 cmd);
5164
5165 list_del(&cmd->cmd_list);
5166 list_add_tail(&cmd->cmd_list, &free_list);
5167
5168 /* piggy back on hardware_lock for protection */
5169 vha->hw->tgt.num_qfull_cmds_alloc--;
5170 }
5171 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5172
5173 cmd = NULL;
5174
5175 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5176 list_del(&cmd->cmd_list);
5177 /* This cmd was never sent to TCM. There is no need
5178 * to schedule free or call free_cmd
5179 */
5180 qlt_free_cmd(cmd);
5181 }
5182 return rc;
5183}
5184
5185static void
5186qlt_send_busy(struct scsi_qla_host *vha,
5187 struct atio_from_isp *atio, uint16_t status)
5188{
5189 int rc = 0;
5190
5191 rc = __qlt_send_busy(vha, atio, status);
5192 if (rc == -ENOMEM)
5193 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5194}
5195
5196static int
5197qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
Quinn Tran8b666802017-03-15 09:48:45 -07005198 struct atio_from_isp *atio, bool ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005199{
5200 struct qla_hw_data *ha = vha->hw;
5201 uint16_t status;
Quinn Tran8b666802017-03-15 09:48:45 -07005202 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005203
5204 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5205 return 0;
5206
Quinn Tran8b666802017-03-15 09:48:45 -07005207 if (!ha_locked)
5208 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005209 status = temp_sam_status;
5210 qlt_send_busy(vha, atio, status);
Quinn Tran8b666802017-03-15 09:48:45 -07005211 if (!ha_locked)
5212 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5213
Quinn Tran33e79972014-09-25 06:14:55 -04005214 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005215}
5216
5217/* ha->hardware_lock supposed to be held on entry */
5218/* called via callback from qla2xxx */
5219static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005220 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005221{
5222 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005223 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005224 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005225 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005226
5227 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005228 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005229 "ATIO pkt, but no tgt (ha %p)", ha);
5230 return;
5231 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005232 /*
5233 * In tgt_stop mode we also should allow all requests to pass.
5234 * Otherwise, some commands can stuck.
5235 */
5236
Quinn Tran2f424b92015-12-17 14:57:07 -05005237 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005238
5239 switch (atio->u.raw.entry_type) {
5240 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005241 if (unlikely(atio->u.isp24.exchange_addr ==
5242 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005243 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005244 "qla_target(%d): ATIO_TYPE7 "
5245 "received with UNKNOWN exchange address, "
5246 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005247 if (!ha_locked)
5248 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005249 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005250 if (!ha_locked)
5251 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005252 break;
5253 }
Quinn Tran33e79972014-09-25 06:14:55 -04005254
5255
5256
5257 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran8b666802017-03-15 09:48:45 -07005258 rc = qlt_chk_qfull_thresh_hold(vha, atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005259 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005260 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005261 return;
5262 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005263 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005264 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005265 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005266 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005267 if (unlikely(rc != 0)) {
5268 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005269 if (!ha_locked)
5270 spin_lock_irqsave
5271 (&ha->hardware_lock, flags);
5272
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005273#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5274 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5275#else
Quinn Trana07100e2015-12-07 19:48:57 -05005276 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005277#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005278
5279 if (!ha_locked)
5280 spin_unlock_irqrestore
5281 (&ha->hardware_lock, flags);
5282
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005283 } else {
5284 if (tgt->tgt_stop) {
5285 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5286 "qla_target: Unable to send "
5287 "command to target for req, "
5288 "ignoring.\n");
5289 } else {
5290 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5291 "qla_target(%d): Unable to send "
5292 "command to target, sending BUSY "
5293 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005294 if (!ha_locked)
5295 spin_lock_irqsave(
5296 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005297 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005298 if (!ha_locked)
5299 spin_unlock_irqrestore(
5300 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005301 }
5302 }
5303 }
5304 break;
5305
5306 case IMMED_NOTIFY_TYPE:
5307 {
5308 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5309 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5310 "qla_target(%d): Received ATIO packet %x "
5311 "with error status %x\n", vha->vp_idx,
5312 atio->u.raw.entry_type,
5313 atio->u.isp2x.entry_status);
5314 break;
5315 }
5316 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005317
5318 if (!ha_locked)
5319 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005320 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005321 if (!ha_locked)
5322 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005323 break;
5324 }
5325
5326 default:
5327 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5328 "qla_target(%d): Received unknown ATIO atio "
5329 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5330 break;
5331 }
5332
Quinn Tran2f424b92015-12-17 14:57:07 -05005333 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005334}
5335
5336/* ha->hardware_lock supposed to be held on entry */
5337/* called via callback from qla2xxx */
5338static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5339{
5340 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005341 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005342
5343 if (unlikely(tgt == NULL)) {
5344 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5345 "qla_target(%d): Response pkt %x received, but no "
5346 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5347 return;
5348 }
5349
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005350 /*
5351 * In tgt_stop mode we also should allow all requests to pass.
5352 * Otherwise, some commands can stuck.
5353 */
5354
5355 tgt->irq_cmd_count++;
5356
5357 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005358 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005359 case CTIO_TYPE7:
5360 {
5361 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005362 qlt_do_ctio_completion(vha, entry->handle,
5363 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5364 entry);
5365 break;
5366 }
5367
5368 case ACCEPT_TGT_IO_TYPE:
5369 {
5370 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5371 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005372 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005373 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005374 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5375 "qla_target(%d): ATIO with error "
5376 "status %x received\n", vha->vp_idx,
5377 le16_to_cpu(atio->u.isp2x.status));
5378 break;
5379 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005380
Quinn Tran8b666802017-03-15 09:48:45 -07005381 rc = qlt_chk_qfull_thresh_hold(vha, atio, true);
Quinn Tran33e79972014-09-25 06:14:55 -04005382 if (rc != 0) {
5383 tgt->irq_cmd_count--;
5384 return;
5385 }
5386
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005387 rc = qlt_handle_cmd_for_atio(vha, atio);
5388 if (unlikely(rc != 0)) {
5389 if (rc == -ESRCH) {
5390#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5391 qlt_send_busy(vha, atio, 0);
5392#else
Quinn Trana07100e2015-12-07 19:48:57 -05005393 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005394#endif
5395 } else {
5396 if (tgt->tgt_stop) {
5397 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5398 "qla_target: Unable to send "
5399 "command to target, sending TERM "
5400 "EXCHANGE for rsp\n");
5401 qlt_send_term_exchange(vha, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005402 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005403 } else {
5404 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5405 "qla_target(%d): Unable to send "
5406 "command to target, sending BUSY "
5407 "status\n", vha->vp_idx);
5408 qlt_send_busy(vha, atio, 0);
5409 }
5410 }
5411 }
5412 }
5413 break;
5414
5415 case CONTINUE_TGT_IO_TYPE:
5416 {
5417 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005418 qlt_do_ctio_completion(vha, entry->handle,
5419 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5420 entry);
5421 break;
5422 }
5423
5424 case CTIO_A64_TYPE:
5425 {
5426 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005427 qlt_do_ctio_completion(vha, entry->handle,
5428 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5429 entry);
5430 break;
5431 }
5432
5433 case IMMED_NOTIFY_TYPE:
5434 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5435 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5436 break;
5437
5438 case NOTIFY_ACK_TYPE:
5439 if (tgt->notify_ack_expected > 0) {
5440 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5441 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5442 "NOTIFY_ACK seq %08x status %x\n",
5443 le16_to_cpu(entry->u.isp2x.seq_id),
5444 le16_to_cpu(entry->u.isp2x.status));
5445 tgt->notify_ack_expected--;
5446 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005447 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005448 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5449 "qla_target(%d): NOTIFY_ACK "
5450 "failed %x\n", vha->vp_idx,
5451 le16_to_cpu(entry->u.isp2x.status));
5452 }
5453 } else {
5454 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5455 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5456 vha->vp_idx);
5457 }
5458 break;
5459
5460 case ABTS_RECV_24XX:
5461 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5462 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5463 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5464 break;
5465
5466 case ABTS_RESP_24XX:
5467 if (tgt->abts_resp_expected > 0) {
5468 struct abts_resp_from_24xx_fw *entry =
5469 (struct abts_resp_from_24xx_fw *)pkt;
5470 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5471 "ABTS_RESP_24XX: compl_status %x\n",
5472 entry->compl_status);
5473 tgt->abts_resp_expected--;
5474 if (le16_to_cpu(entry->compl_status) !=
5475 ABTS_RESP_COMPL_SUCCESS) {
5476 if ((entry->error_subcode1 == 0x1E) &&
5477 (entry->error_subcode2 == 0)) {
5478 /*
5479 * We've got a race here: aborted
5480 * exchange not terminated, i.e.
5481 * response for the aborted command was
5482 * sent between the abort request was
5483 * received and processed.
5484 * Unfortunately, the firmware has a
5485 * silly requirement that all aborted
5486 * exchanges must be explicitely
5487 * terminated, otherwise it refuses to
5488 * send responses for the abort
5489 * requests. So, we have to
5490 * (re)terminate the exchange and retry
5491 * the abort response.
5492 */
5493 qlt_24xx_retry_term_exchange(vha,
5494 entry);
5495 } else
5496 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5497 "qla_target(%d): ABTS_RESP_24XX "
5498 "failed %x (subcode %x:%x)",
5499 vha->vp_idx, entry->compl_status,
5500 entry->error_subcode1,
5501 entry->error_subcode2);
5502 }
5503 } else {
5504 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5505 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5506 "received\n", vha->vp_idx);
5507 }
5508 break;
5509
5510 default:
5511 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5512 "qla_target(%d): Received unknown response pkt "
5513 "type %x\n", vha->vp_idx, pkt->entry_type);
5514 break;
5515 }
5516
5517 tgt->irq_cmd_count--;
5518}
5519
5520/*
5521 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5522 */
5523void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5524 uint16_t *mailbox)
5525{
5526 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005527 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005528 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005529
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005530 if (!ha->tgt.tgt_ops)
5531 return;
5532
5533 if (unlikely(tgt == NULL)) {
5534 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5535 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5536 return;
5537 }
5538
5539 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5540 IS_QLA2100(ha))
5541 return;
5542 /*
5543 * In tgt_stop mode we also should allow all requests to pass.
5544 * Otherwise, some commands can stuck.
5545 */
5546
5547 tgt->irq_cmd_count++;
5548
5549 switch (code) {
5550 case MBA_RESET: /* Reset */
5551 case MBA_SYSTEM_ERR: /* System Error */
5552 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5553 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5554 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5555 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005556 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005557 break;
5558 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5559 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5560 break;
5561
5562 case MBA_LOOP_UP:
5563 {
5564 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005565 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005566 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5567 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5568 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005569 if (tgt->link_reinit_iocb_pending) {
5570 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5571 0, 0, 0, 0, 0, 0);
5572 tgt->link_reinit_iocb_pending = 0;
5573 }
5574 break;
5575 }
5576
5577 case MBA_LIP_OCCURRED:
5578 case MBA_LOOP_DOWN:
5579 case MBA_LIP_RESET:
5580 case MBA_RSCN_UPDATE:
5581 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005582 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005583 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5584 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5585 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005586 break;
5587
Quinn Tranead03852017-01-19 22:28:01 -08005588 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005589 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5590 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5591 vha->vp_idx,
5592 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5593 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005594
5595 if (le16_to_cpu(mailbox[3]) == 1) {
5596 /* exchange starvation. */
5597 vha->hw->exch_starvation++;
5598 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005599 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005600 "Exchange starvation-. Resetting RISC\n");
5601
5602 vha->hw->exch_starvation = 0;
5603 if (IS_P3P_TYPE(vha->hw))
5604 set_bit(FCOE_CTX_RESET_NEEDED,
5605 &vha->dpc_flags);
5606 else
5607 set_bit(ISP_ABORT_NEEDED,
5608 &vha->dpc_flags);
5609 qla2xxx_wake_dpc(vha);
5610 }
5611 }
5612 break;
5613
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005614 case MBA_PORT_UPDATE:
5615 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5616 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005617 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005618 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5619 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5620 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5621
5622 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005623 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005624 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5625 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005626 vha->hw->exch_starvation = 0;
5627 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005628 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5629 "Async MB 2: Port Logged Out\n");
5630 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005631 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005632 break;
5633 }
5634
5635 tgt->irq_cmd_count--;
5636}
5637
5638static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5639 uint16_t loop_id)
5640{
Quinn Tran726b8542017-01-19 22:28:00 -08005641 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005642 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005643 unsigned long flags;
5644 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005645
5646 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5647 if (!fcport) {
5648 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5649 "qla_target(%d): Allocation of tmp FC port failed",
5650 vha->vp_idx);
5651 return NULL;
5652 }
5653
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005654 fcport->loop_id = loop_id;
5655
Quinn Tran15f30a52017-03-15 09:48:52 -07005656 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005657 if (rc != QLA_SUCCESS) {
5658 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5659 "qla_target(%d): Failed to retrieve fcport "
5660 "information -- get_port_database() returned %x "
5661 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5662 kfree(fcport);
5663 return NULL;
5664 }
5665
Quinn Tran726b8542017-01-19 22:28:00 -08005666 del = NULL;
5667 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5668 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5669
5670 if (tfcp) {
5671 tfcp->d_id = fcport->d_id;
5672 tfcp->port_type = fcport->port_type;
5673 tfcp->supported_classes = fcport->supported_classes;
5674 tfcp->flags |= fcport->flags;
5675
5676 del = fcport;
5677 fcport = tfcp;
5678 } else {
5679 if (vha->hw->current_topology == ISP_CFG_F)
5680 fcport->flags |= FCF_FABRIC_DEVICE;
5681
5682 list_add_tail(&fcport->list, &vha->vp_fcports);
5683 if (!IS_SW_RESV_ADDR(fcport->d_id))
5684 vha->fcport_count++;
5685 fcport->login_gen++;
5686 fcport->disc_state = DSC_LOGIN_COMPLETE;
5687 fcport->login_succ = 1;
5688 newfcport = 1;
5689 }
5690
5691 fcport->deleted = 0;
5692 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5693
5694 switch (vha->host->active_mode) {
5695 case MODE_INITIATOR:
5696 case MODE_DUAL:
5697 if (newfcport) {
5698 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005699 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005700 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5701 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5702 qla24xx_post_upd_fcport_work(vha, fcport);
5703 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005704 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005705 "%s %d %8phC post gpsc fcp_cnt %d\n",
5706 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5707 qla24xx_post_gpsc_work(vha, fcport);
5708 }
5709 }
5710 break;
5711
5712 case MODE_TARGET:
5713 default:
5714 break;
5715 }
5716 if (del)
5717 qla2x00_free_fcport(del);
5718
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005719 return fcport;
5720}
5721
5722/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005723static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005724 uint8_t *s_id)
5725{
Quinn Tran5d964832017-01-19 22:27:59 -08005726 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005727 fc_port_t *fcport = NULL;
5728 int rc, global_resets;
5729 uint16_t loop_id = 0;
5730
Quinn Tran726b8542017-01-19 22:28:00 -08005731 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5732 /*
5733 * This is Domain Controller, so it should be
5734 * OK to drop SCSI commands from it.
5735 */
5736 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5737 "Unable to find initiator with S_ID %x:%x:%x",
5738 s_id[0], s_id[1], s_id[2]);
5739 return NULL;
5740 }
5741
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005742 mutex_lock(&vha->vha_tgt.tgt_mutex);
5743
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005744retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005745 global_resets =
5746 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005747
5748 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5749 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005750 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5751
Quinn Tran726b8542017-01-19 22:28:00 -08005752 ql_log(ql_log_info, vha, 0xf071,
5753 "qla_target(%d): Unable to find "
5754 "initiator with S_ID %x:%x:%x",
5755 vha->vp_idx, s_id[0], s_id[1],
5756 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005757
5758 if (rc == -ENOENT) {
5759 qlt_port_logo_t logo;
5760 sid_to_portid(s_id, &logo.id);
5761 logo.cmd_count = 1;
5762 qlt_send_first_logo(vha, &logo);
5763 }
5764
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005765 return NULL;
5766 }
5767
5768 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005769 if (!fcport) {
5770 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005771 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005772 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005773
5774 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005775 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5777 "qla_target(%d): global reset during session discovery "
5778 "(counter was %d, new %d), retrying", vha->vp_idx,
5779 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005780 atomic_read(&vha->vha_tgt.
5781 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005782 goto retry;
5783 }
5784
5785 sess = qlt_create_sess(vha, fcport, true);
5786
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005787 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5788
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005789 return sess;
5790}
5791
5792static void qlt_abort_work(struct qla_tgt *tgt,
5793 struct qla_tgt_sess_work_param *prm)
5794{
5795 struct scsi_qla_host *vha = tgt->vha;
5796 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005797 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005798 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005799 uint32_t be_s_id;
5800 uint8_t s_id[3];
5801 int rc;
5802
Quinn Tran75601512015-12-17 14:57:04 -05005803 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005804
5805 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005806 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005807
5808 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5809 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5810 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5811
5812 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5813 (unsigned char *)&be_s_id);
5814 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005815 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005816
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005817 sess = qlt_make_local_sess(vha, s_id);
5818 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005819
Quinn Tran75601512015-12-17 14:57:04 -05005820 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005821 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005822 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005823 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005824 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005825 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005826 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005827 }
5828
Quinn Tran726b8542017-01-19 22:28:00 -08005829 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005830 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08005831 "%s: kref_get fail %8phC \n",
5832 __func__, sess->port_name);
5833 sess = NULL;
5834 goto out_term2;
5835 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005836 }
5837
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005838 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005839 ha->tgt.tgt_ops->put_sess(sess);
5840 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5841
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005842 if (rc != 0)
5843 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005844 return;
5845
Quinn Tran75601512015-12-17 14:57:04 -05005846out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08005847 if (sess)
5848 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005849 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005850
5851out_term:
5852 spin_lock_irqsave(&ha->hardware_lock, flags);
5853 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
5854 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005855}
5856
5857static void qlt_tmr_work(struct qla_tgt *tgt,
5858 struct qla_tgt_sess_work_param *prm)
5859{
5860 struct atio_from_isp *a = &prm->tm_iocb2;
5861 struct scsi_qla_host *vha = tgt->vha;
5862 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005863 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005864 unsigned long flags;
5865 uint8_t *s_id = NULL; /* to hide compiler warnings */
5866 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07005867 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005868 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005869 void *iocb;
5870
Quinn Tran75601512015-12-17 14:57:04 -05005871 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005872
5873 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005874 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005875
5876 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5877 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5878 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005879 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005880
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005881 sess = qlt_make_local_sess(vha, s_id);
5882 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005883
Quinn Tran75601512015-12-17 14:57:04 -05005884 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005885 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005886 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005887 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005888 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005889 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07005890 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005891 }
5892
Quinn Tran726b8542017-01-19 22:28:00 -08005893 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005894 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08005895 "%s: kref_get fail %8phC\n",
5896 __func__, sess->port_name);
5897 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07005898 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08005899 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005900 }
5901
5902 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005903 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07005904 unpacked_lun =
5905 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005906
5907 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005908 ha->tgt.tgt_ops->put_sess(sess);
5909 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5910
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005911 if (rc != 0)
5912 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005913 return;
5914
Quinn Tranf159b3c2017-03-15 09:48:47 -07005915out_term2:
5916 if (sess)
5917 ha->tgt.tgt_ops->put_sess(sess);
5918 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005919out_term:
Quinn Trana07100e2015-12-07 19:48:57 -05005920 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005921}
5922
5923static void qlt_sess_work_fn(struct work_struct *work)
5924{
5925 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5926 struct scsi_qla_host *vha = tgt->vha;
5927 unsigned long flags;
5928
5929 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5930
5931 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5932 while (!list_empty(&tgt->sess_works_list)) {
5933 struct qla_tgt_sess_work_param *prm = list_entry(
5934 tgt->sess_works_list.next, typeof(*prm),
5935 sess_works_list_entry);
5936
5937 /*
5938 * This work can be scheduled on several CPUs at time, so we
5939 * must delete the entry to eliminate double processing
5940 */
5941 list_del(&prm->sess_works_list_entry);
5942
5943 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5944
5945 switch (prm->type) {
5946 case QLA_TGT_SESS_WORK_ABORT:
5947 qlt_abort_work(tgt, prm);
5948 break;
5949 case QLA_TGT_SESS_WORK_TM:
5950 qlt_tmr_work(tgt, prm);
5951 break;
5952 default:
5953 BUG_ON(1);
5954 break;
5955 }
5956
5957 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5958
5959 kfree(prm);
5960 }
5961 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5962}
5963
5964/* Must be called under tgt_host_action_mutex */
5965int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5966{
5967 struct qla_tgt *tgt;
5968
5969 if (!QLA_TGT_MODE_ENABLED())
5970 return 0;
5971
Arun Easi33c36c02013-01-30 03:34:41 -05005972 if (!IS_TGT_MODE_CAPABLE(ha)) {
5973 ql_log(ql_log_warn, base_vha, 0xe070,
5974 "This adapter does not support target mode.\n");
5975 return 0;
5976 }
5977
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005978 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005979 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005980
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005981 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005982
5983 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5984 if (!tgt) {
5985 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5986 "Unable to allocate struct qla_tgt\n");
5987 return -ENOMEM;
5988 }
5989
5990 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5991 base_vha->host->hostt->supported_mode |= MODE_TARGET;
5992
5993 tgt->ha = ha;
5994 tgt->vha = base_vha;
5995 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005996 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005997 spin_lock_init(&tgt->sess_work_lock);
5998 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
5999 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006000 atomic_set(&tgt->tgt_global_resets_count, 0);
6001
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006002 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006003
6004 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6005 "qla_target(%d): using 64 Bit PCI addressing",
6006 base_vha->vp_idx);
6007 tgt->tgt_enable_64bit_addr = 1;
6008 /* 3 is reserved */
6009 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6010 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
6011 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
6012
6013 mutex_lock(&qla_tgt_mutex);
6014 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6015 mutex_unlock(&qla_tgt_mutex);
6016
Quinn Tranf1443ee2017-03-15 09:48:51 -07006017 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6018 ha->tgt.tgt_ops->add_target(base_vha);
6019
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006020 return 0;
6021}
6022
6023/* Must be called under tgt_host_action_mutex */
6024int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6025{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006026 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006027 return 0;
6028
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006029 if (vha->fc_vport) {
6030 qlt_release(vha->vha_tgt.qla_tgt);
6031 return 0;
6032 }
Quinn Tran33e79972014-09-25 06:14:55 -04006033
6034 /* free left over qfull cmds */
6035 qlt_init_term_exchange(vha);
6036
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006037 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006038 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006039 mutex_unlock(&qla_tgt_mutex);
6040
6041 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6042 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006043 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006044
6045 return 0;
6046}
6047
Quinn Tran482c9dc2017-03-15 09:48:54 -07006048void qlt_remove_target_resources(struct qla_hw_data *ha)
6049{
6050 struct scsi_qla_host *node;
6051 u32 key = 0;
6052
6053 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6054 btree_remove32(&ha->tgt.host_map, key);
6055
6056 btree_destroy32(&ha->tgt.host_map);
6057}
6058
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006059static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6060 unsigned char *b)
6061{
6062 int i;
6063
6064 pr_debug("qla2xxx HW vha->node_name: ");
6065 for (i = 0; i < WWN_SIZE; i++)
6066 pr_debug("%02x ", vha->node_name[i]);
6067 pr_debug("\n");
6068 pr_debug("qla2xxx HW vha->port_name: ");
6069 for (i = 0; i < WWN_SIZE; i++)
6070 pr_debug("%02x ", vha->port_name[i]);
6071 pr_debug("\n");
6072
6073 pr_debug("qla2xxx passed configfs WWPN: ");
6074 put_unaligned_be64(wwpn, b);
6075 for (i = 0; i < WWN_SIZE; i++)
6076 pr_debug("%02x ", b[i]);
6077 pr_debug("\n");
6078}
6079
6080/**
6081 * qla_tgt_lport_register - register lport with external module
6082 *
6083 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6084 * @wwpn: Passwd FC target WWPN
6085 * @callback: lport initialization callback for tcm_qla2xxx code
6086 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6087 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006088int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6089 u64 npiv_wwpn, u64 npiv_wwnn,
6090 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006091{
6092 struct qla_tgt *tgt;
6093 struct scsi_qla_host *vha;
6094 struct qla_hw_data *ha;
6095 struct Scsi_Host *host;
6096 unsigned long flags;
6097 int rc;
6098 u8 b[WWN_SIZE];
6099
6100 mutex_lock(&qla_tgt_mutex);
6101 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6102 vha = tgt->vha;
6103 ha = vha->hw;
6104
6105 host = vha->host;
6106 if (!host)
6107 continue;
6108
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006109 if (!(host->hostt->supported_mode & MODE_TARGET))
6110 continue;
6111
6112 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006113 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006114 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6115 host->host_no);
6116 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6117 continue;
6118 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006119 if (tgt->tgt_stop) {
6120 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6121 host->host_no);
6122 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6123 continue;
6124 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6126
6127 if (!scsi_host_get(host)) {
6128 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6129 "Unable to scsi_host_get() for"
6130 " qla2xxx scsi_host\n");
6131 continue;
6132 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006133 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006134
6135 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6136 scsi_host_put(host);
6137 continue;
6138 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006139 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6140 if (rc != 0)
6141 scsi_host_put(host);
6142
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006143 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006144 return rc;
6145 }
6146 mutex_unlock(&qla_tgt_mutex);
6147
6148 return -ENODEV;
6149}
6150EXPORT_SYMBOL(qlt_lport_register);
6151
6152/**
6153 * qla_tgt_lport_deregister - Degister lport
6154 *
6155 * @vha: Registered scsi_qla_host pointer
6156 */
6157void qlt_lport_deregister(struct scsi_qla_host *vha)
6158{
6159 struct qla_hw_data *ha = vha->hw;
6160 struct Scsi_Host *sh = vha->host;
6161 /*
6162 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6163 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006164 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006165 ha->tgt.tgt_ops = NULL;
6166 /*
6167 * Release the Scsi_Host reference for the underlying qla2xxx host
6168 */
6169 scsi_host_put(sh);
6170}
6171EXPORT_SYMBOL(qlt_lport_deregister);
6172
6173/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006174static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006175{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006176 switch (ql2x_ini_mode) {
6177 case QLA2XXX_INI_MODE_DISABLED:
6178 case QLA2XXX_INI_MODE_EXCLUSIVE:
6179 vha->host->active_mode = MODE_TARGET;
6180 break;
6181 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006182 vha->host->active_mode = MODE_UNKNOWN;
6183 break;
6184 case QLA2XXX_INI_MODE_DUAL:
6185 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006186 break;
6187 default:
6188 break;
6189 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006190}
6191
6192/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006193static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006194{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006195 switch (ql2x_ini_mode) {
6196 case QLA2XXX_INI_MODE_DISABLED:
6197 vha->host->active_mode = MODE_UNKNOWN;
6198 break;
6199 case QLA2XXX_INI_MODE_EXCLUSIVE:
6200 vha->host->active_mode = MODE_INITIATOR;
6201 break;
6202 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006203 case QLA2XXX_INI_MODE_DUAL:
6204 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006205 break;
6206 default:
6207 break;
6208 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006209}
6210
6211/*
6212 * qla_tgt_enable_vha - NO LOCK HELD
6213 *
6214 * host_reset, bring up w/ Target Mode Enabled
6215 */
6216void
6217qlt_enable_vha(struct scsi_qla_host *vha)
6218{
6219 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006220 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006221 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006222 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Trancdb898c2015-12-17 14:57:05 -05006223 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006224
6225 if (!tgt) {
6226 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6227 "Unable to locate qla_tgt pointer from"
6228 " struct qla_hw_data\n");
6229 dump_stack();
6230 return;
6231 }
6232
6233 spin_lock_irqsave(&ha->hardware_lock, flags);
6234 tgt->tgt_stopped = 0;
6235 qlt_set_mode(vha);
6236 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6237
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006238 if (vha->vp_idx) {
6239 qla24xx_disable_vp(vha);
6240 qla24xx_enable_vp(vha);
6241 } else {
Quinn Trancdb898c2015-12-17 14:57:05 -05006242 if (ha->msix_entries) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006243 ql_dbg(ql_dbg_tgt, vha, 0xe081,
Quinn Trancdb898c2015-12-17 14:57:05 -05006244 "%s: host%ld : vector %d cpu %d\n",
6245 __func__, vha->host_no,
6246 ha->msix_entries[rspq_ent].vector,
6247 ha->msix_entries[rspq_ent].cpuid);
6248
6249 ha->tgt.rspq_vector_cpuid =
6250 ha->msix_entries[rspq_ent].cpuid;
6251 }
6252
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006253 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6254 qla2xxx_wake_dpc(base_vha);
6255 qla2x00_wait_for_hba_online(base_vha);
6256 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006257}
6258EXPORT_SYMBOL(qlt_enable_vha);
6259
6260/*
6261 * qla_tgt_disable_vha - NO LOCK HELD
6262 *
6263 * Disable Target Mode and reset the adapter
6264 */
Joern Engel55a90662014-09-16 16:23:15 -04006265static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006266{
6267 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006268 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006269 unsigned long flags;
6270
6271 if (!tgt) {
6272 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6273 "Unable to locate qla_tgt pointer from"
6274 " struct qla_hw_data\n");
6275 dump_stack();
6276 return;
6277 }
6278
6279 spin_lock_irqsave(&ha->hardware_lock, flags);
6280 qlt_clear_mode(vha);
6281 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6282
6283 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6284 qla2xxx_wake_dpc(vha);
6285 qla2x00_wait_for_hba_online(vha);
6286}
6287
6288/*
6289 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6290 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6291 * members.
6292 */
6293void
6294qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6295{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006296 vha->vha_tgt.qla_tgt = NULL;
6297
6298 mutex_init(&vha->vha_tgt.tgt_mutex);
6299 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006300
6301 qlt_clear_mode(vha);
6302
6303 /*
6304 * NOTE: Currently the value is kept the same for <24xx and
6305 * >=24xx ISPs. If it is necessary to change it,
6306 * the check should be added for specific ISPs,
6307 * assigning the value appropriately.
6308 */
6309 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006310
6311 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006312}
6313
6314void
6315qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6316{
6317 /*
6318 * FC-4 Feature bit 0 indicates target functionality to the name server.
6319 */
6320 if (qla_tgt_mode_enabled(vha)) {
Quinn Tran726b8542017-01-19 22:28:00 -08006321 ct_req->req.rff_id.fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006322 } else if (qla_ini_mode_enabled(vha)) {
6323 ct_req->req.rff_id.fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006324 } else if (qla_dual_mode_enabled(vha))
6325 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006326}
6327
6328/*
6329 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6330 * @ha: HA context
6331 *
6332 * Beginning of ATIO ring has initialization control block already built
6333 * by nvram config routine.
6334 *
6335 * Returns 0 on success.
6336 */
6337void
6338qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6339{
6340 struct qla_hw_data *ha = vha->hw;
6341 uint16_t cnt;
6342 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6343
Quinn Tranead03852017-01-19 22:28:01 -08006344 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006345 return;
6346
6347 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6348 pkt->u.raw.signature = ATIO_PROCESSED;
6349 pkt++;
6350 }
6351
6352}
6353
6354/*
6355 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6356 * @ha: SCSI driver HA context
6357 */
6358void
Quinn Tran2f424b92015-12-17 14:57:07 -05006359qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006360{
6361 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006362 struct atio_from_isp *pkt;
6363 int cnt, i;
6364
Quinn Tranec7193e2017-03-15 09:48:55 -07006365 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006366 return;
6367
Quinn Tran5f355092016-12-23 18:06:11 -08006368 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6369 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006370 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6371 cnt = pkt->u.raw.entry_count;
6372
Quinn Tran5f355092016-12-23 18:06:11 -08006373 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6374 /*
6375 * This packet is corrupted. The header + payload
6376 * can not be trusted. There is no point in passing
6377 * it further up.
6378 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006379 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006380 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6381 pkt->u.isp24.fcp_hdr.s_id,
6382 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6383 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6384
6385 adjust_corrupted_atio(pkt);
6386 qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
6387 } else {
6388 qlt_24xx_atio_pkt_all_vps(vha,
6389 (struct atio_from_isp *)pkt, ha_locked);
6390 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006391
6392 for (i = 0; i < cnt; i++) {
6393 ha->tgt.atio_ring_index++;
6394 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6395 ha->tgt.atio_ring_index = 0;
6396 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6397 } else
6398 ha->tgt.atio_ring_ptr++;
6399
6400 pkt->u.raw.signature = ATIO_PROCESSED;
6401 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6402 }
6403 wmb();
6404 }
6405
6406 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006407 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Quinn Tran3761f3e2015-06-10 11:05:19 -04006408 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006409}
6410
6411void
Arun Easiaa230bc2013-01-30 03:34:39 -05006412qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006413{
6414 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006415 if (!QLA_TGT_MODE_ENABLED())
6416 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006417
Arun Easiaa230bc2013-01-30 03:34:39 -05006418 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6419 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6420 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6421
6422 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6423 struct qla_msix_entry *msix = &ha->msix_entries[2];
6424 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6425
6426 icb->msix_atio = cpu_to_le16(msix->entry);
6427 ql_dbg(ql_dbg_init, vha, 0xf072,
6428 "Registering ICB vector 0x%x for atio que.\n",
6429 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006430 }
6431}
6432
6433void
6434qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6435{
6436 struct qla_hw_data *ha = vha->hw;
Quinn Tranead03852017-01-19 22:28:01 -08006437 u32 tmp;
6438 u16 t;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006439
Quinn Tranead03852017-01-19 22:28:01 -08006440 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006441 if (!ha->tgt.saved_set) {
6442 /* We save only once */
6443 ha->tgt.saved_exchange_count = nv->exchange_count;
6444 ha->tgt.saved_firmware_options_1 =
6445 nv->firmware_options_1;
6446 ha->tgt.saved_firmware_options_2 =
6447 nv->firmware_options_2;
6448 ha->tgt.saved_firmware_options_3 =
6449 nv->firmware_options_3;
6450 ha->tgt.saved_set = 1;
6451 }
6452
Quinn Tranead03852017-01-19 22:28:01 -08006453 if (qla_tgt_mode_enabled(vha)) {
6454 nv->exchange_count = cpu_to_le16(0xFFFF);
6455 } else { /* dual */
6456 if (ql_dm_tgt_ex_pct > 100) {
6457 ql_dm_tgt_ex_pct = 50;
6458 } else if (ql_dm_tgt_ex_pct == 100) {
6459 /* leave some for FW */
6460 ql_dm_tgt_ex_pct = 95;
6461 }
6462
6463 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6464 tmp = tmp/100;
6465 if (tmp > 0xffff)
6466 tmp = 0xffff;
6467
6468 t = tmp & 0xffff;
6469 nv->exchange_count = cpu_to_le16(t);
6470 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006471
6472 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006473 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006474
6475 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006476 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006477 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006478
6479 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006480 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006481 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006482 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006483 if (ql2xtgt_tape_enable)
6484 /* Enable FC Tape support */
6485 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6486 else
6487 /* Disable FC Tape support */
6488 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6489
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006490 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006491 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006492
6493 /*
6494 * clear BIT 15 explicitly as we have seen at least
6495 * a couple of instances where this was set and this
6496 * was causing the firmware to not be initialized.
6497 */
6498 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006499 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006500 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006501 } else {
6502 if (ha->tgt.saved_set) {
6503 nv->exchange_count = ha->tgt.saved_exchange_count;
6504 nv->firmware_options_1 =
6505 ha->tgt.saved_firmware_options_1;
6506 nv->firmware_options_2 =
6507 ha->tgt.saved_firmware_options_2;
6508 nv->firmware_options_3 =
6509 ha->tgt.saved_firmware_options_3;
6510 }
6511 return;
6512 }
6513
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006514 if (ha->tgt.enable_class_2) {
6515 if (vha->flags.init_done)
6516 fc_host_supported_classes(vha->host) =
6517 FC_COS_CLASS2 | FC_COS_CLASS3;
6518
Bart Van Asschead950362015-07-09 07:24:08 -07006519 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006520 } else {
6521 if (vha->flags.init_done)
6522 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6523
Bart Van Asschead950362015-07-09 07:24:08 -07006524 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006525 }
6526}
6527
6528void
6529qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6530 struct init_cb_24xx *icb)
6531{
6532 struct qla_hw_data *ha = vha->hw;
6533
Quinn Tran481ce732015-12-17 14:57:08 -05006534 if (!QLA_TGT_MODE_ENABLED())
6535 return;
6536
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006537 if (ha->tgt.node_name_set) {
6538 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006539 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006540 }
Quinn Tran481ce732015-12-17 14:57:08 -05006541
6542 /* disable ZIO at start time. */
6543 if (!vha->flags.init_done) {
6544 uint32_t tmp;
6545 tmp = le32_to_cpu(icb->firmware_options_2);
6546 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6547 icb->firmware_options_2 = cpu_to_le32(tmp);
6548 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006549}
6550
Arun Easiaa230bc2013-01-30 03:34:39 -05006551void
6552qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6553{
6554 struct qla_hw_data *ha = vha->hw;
Quinn Tranead03852017-01-19 22:28:01 -08006555 u32 tmp;
6556 u16 t;
Arun Easiaa230bc2013-01-30 03:34:39 -05006557
6558 if (!QLA_TGT_MODE_ENABLED())
6559 return;
6560
Quinn Tranead03852017-01-19 22:28:01 -08006561 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006562 if (!ha->tgt.saved_set) {
6563 /* We save only once */
6564 ha->tgt.saved_exchange_count = nv->exchange_count;
6565 ha->tgt.saved_firmware_options_1 =
6566 nv->firmware_options_1;
6567 ha->tgt.saved_firmware_options_2 =
6568 nv->firmware_options_2;
6569 ha->tgt.saved_firmware_options_3 =
6570 nv->firmware_options_3;
6571 ha->tgt.saved_set = 1;
6572 }
6573
Quinn Tranead03852017-01-19 22:28:01 -08006574 if (qla_tgt_mode_enabled(vha)) {
6575 nv->exchange_count = cpu_to_le16(0xFFFF);
6576 } else { /* dual */
6577 if (ql_dm_tgt_ex_pct > 100) {
6578 ql_dm_tgt_ex_pct = 50;
6579 } else if (ql_dm_tgt_ex_pct == 100) {
6580 /* leave some for FW */
6581 ql_dm_tgt_ex_pct = 95;
6582 }
6583
6584 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6585 tmp = tmp/100;
6586 if (tmp > 0xffff)
6587 tmp = 0xffff;
6588 t = tmp & 0xffff;
6589 nv->exchange_count = cpu_to_le16(t);
6590 }
Arun Easiaa230bc2013-01-30 03:34:39 -05006591
6592 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006593 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006594
6595 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006596 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006597 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006598 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006599 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006600 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006601 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006602 /*
6603 * clear BIT 15 explicitly as we have seen at
6604 * least a couple of instances where this was set
6605 * and this was causing the firmware to not be
6606 * initialized.
6607 */
6608 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006609 if (ql2xtgt_tape_enable)
6610 /* Enable FC tape support */
6611 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6612 else
6613 /* Disable FC tape support */
6614 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6615
Arun Easiaa230bc2013-01-30 03:34:39 -05006616 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006617 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006618 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006619 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006620 } else {
6621 if (ha->tgt.saved_set) {
6622 nv->exchange_count = ha->tgt.saved_exchange_count;
6623 nv->firmware_options_1 =
6624 ha->tgt.saved_firmware_options_1;
6625 nv->firmware_options_2 =
6626 ha->tgt.saved_firmware_options_2;
6627 nv->firmware_options_3 =
6628 ha->tgt.saved_firmware_options_3;
6629 }
6630 return;
6631 }
6632
Arun Easiaa230bc2013-01-30 03:34:39 -05006633 if (ha->tgt.enable_class_2) {
6634 if (vha->flags.init_done)
6635 fc_host_supported_classes(vha->host) =
6636 FC_COS_CLASS2 | FC_COS_CLASS3;
6637
Bart Van Asschead950362015-07-09 07:24:08 -07006638 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006639 } else {
6640 if (vha->flags.init_done)
6641 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6642
Bart Van Asschead950362015-07-09 07:24:08 -07006643 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006644 }
6645}
6646
6647void
6648qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6649 struct init_cb_81xx *icb)
6650{
6651 struct qla_hw_data *ha = vha->hw;
6652
6653 if (!QLA_TGT_MODE_ENABLED())
6654 return;
6655
6656 if (ha->tgt.node_name_set) {
6657 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006658 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006659 }
Quinn Tran481ce732015-12-17 14:57:08 -05006660
6661 /* disable ZIO at start time. */
6662 if (!vha->flags.init_done) {
6663 uint32_t tmp;
6664 tmp = le32_to_cpu(icb->firmware_options_2);
6665 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6666 icb->firmware_options_2 = cpu_to_le32(tmp);
6667 }
6668
Arun Easiaa230bc2013-01-30 03:34:39 -05006669}
6670
6671void
6672qlt_83xx_iospace_config(struct qla_hw_data *ha)
6673{
6674 if (!QLA_TGT_MODE_ENABLED())
6675 return;
6676
6677 ha->msix_count += 1; /* For ATIO Q */
6678}
6679
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006680int
6681qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6682 struct sts_entry_24xx *pkt)
6683{
6684 switch (pkt->entry_type) {
6685 case ABTS_RECV_24XX:
6686 case ABTS_RESP_24XX:
6687 case CTIO_TYPE7:
6688 case NOTIFY_ACK_TYPE:
Quinn Tranf83adb62014-04-11 16:54:43 -04006689 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006690 return 1;
6691 default:
6692 return 0;
6693 }
6694}
6695
6696void
6697qlt_modify_vp_config(struct scsi_qla_host *vha,
6698 struct vp_config_entry_24xx *vpmod)
6699{
Quinn Tranead03852017-01-19 22:28:01 -08006700 /* enable target mode. Bit5 = 1 => disable */
6701 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006702 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006703
Quinn Tranead03852017-01-19 22:28:01 -08006704 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006705 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006706 vpmod->options_idx1 &= ~BIT_4;
6707}
6708
6709void
6710qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6711{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006712 int rc;
6713
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006714 if (!QLA_TGT_MODE_ENABLED())
6715 return;
6716
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04006717 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006718 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6719 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6720 } else {
6721 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6722 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6723 }
6724
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006725 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6726 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006727
6728 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6729 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6730 qlt_unknown_atio_work_fn);
6731
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006732 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006733
6734 rc = btree_init32(&ha->tgt.host_map);
6735 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006736 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006737 "Unable to initialize ha->host_map btree\n");
6738
6739 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006740}
6741
Arun Easiaa230bc2013-01-30 03:34:39 -05006742irqreturn_t
6743qla83xx_msix_atio_q(int irq, void *dev_id)
6744{
6745 struct rsp_que *rsp;
6746 scsi_qla_host_t *vha;
6747 struct qla_hw_data *ha;
6748 unsigned long flags;
6749
6750 rsp = (struct rsp_que *) dev_id;
6751 ha = rsp->hw;
6752 vha = pci_get_drvdata(ha->pdev);
6753
Quinn Tran2f424b92015-12-17 14:57:07 -05006754 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006755
Quinn Tran2f424b92015-12-17 14:57:07 -05006756 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006757
Quinn Tran2f424b92015-12-17 14:57:07 -05006758 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006759
6760 return IRQ_HANDLED;
6761}
6762
Quinn Tran2f424b92015-12-17 14:57:07 -05006763static void
6764qlt_handle_abts_recv_work(struct work_struct *work)
6765{
6766 struct qla_tgt_sess_op *op = container_of(work,
6767 struct qla_tgt_sess_op, work);
6768 scsi_qla_host_t *vha = op->vha;
6769 struct qla_hw_data *ha = vha->hw;
6770 unsigned long flags;
6771
6772 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6773 return;
6774
6775 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6776 qlt_24xx_process_atio_queue(vha, 0);
6777 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6778
6779 spin_lock_irqsave(&ha->hardware_lock, flags);
6780 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6781 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006782
6783 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006784}
6785
6786void
6787qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6788{
6789 struct qla_tgt_sess_op *op;
6790
6791 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6792
6793 if (!op) {
6794 /* do not reach for ATIO queue here. This is best effort err
6795 * recovery at this point.
6796 */
6797 qlt_response_pkt_all_vps(vha, pkt);
6798 return;
6799 }
6800
6801 memcpy(&op->atio, pkt, sizeof(*pkt));
6802 op->vha = vha;
6803 op->chip_reset = vha->hw->chip_reset;
6804 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6805 queue_work(qla_tgt_wq, &op->work);
6806 return;
6807}
6808
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006809int
6810qlt_mem_alloc(struct qla_hw_data *ha)
6811{
6812 if (!QLA_TGT_MODE_ENABLED())
6813 return 0;
6814
6815 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6816 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6817 if (!ha->tgt.tgt_vp_map)
6818 return -ENOMEM;
6819
6820 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6821 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6822 &ha->tgt.atio_dma, GFP_KERNEL);
6823 if (!ha->tgt.atio_ring) {
6824 kfree(ha->tgt.tgt_vp_map);
6825 return -ENOMEM;
6826 }
6827 return 0;
6828}
6829
6830void
6831qlt_mem_free(struct qla_hw_data *ha)
6832{
6833 if (!QLA_TGT_MODE_ENABLED())
6834 return;
6835
6836 if (ha->tgt.atio_ring) {
6837 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6838 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6839 ha->tgt.atio_dma);
6840 }
6841 kfree(ha->tgt.tgt_vp_map);
6842}
6843
6844/* vport_slock to be held by the caller */
6845void
6846qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6847{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006848 void *slot;
6849 u32 key;
6850 int rc;
6851
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006852 if (!QLA_TGT_MODE_ENABLED())
6853 return;
6854
Quinn Tran482c9dc2017-03-15 09:48:54 -07006855 key = vha->d_id.b24;
6856
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006857 switch (cmd) {
6858 case SET_VP_IDX:
6859 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6860 break;
6861 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07006862 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6863 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006864 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006865 "Save vha in host_map %p %06x\n", vha, key);
6866 rc = btree_insert32(&vha->hw->tgt.host_map,
6867 key, vha, GFP_ATOMIC);
6868 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006869 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006870 "Unable to insert s_id into host_map: %06x\n",
6871 key);
6872 return;
6873 }
Quinn Tran83548fe2017-06-02 09:12:01 -07006874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6875 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006876 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006877 break;
6878 case RESET_VP_IDX:
6879 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6880 break;
6881 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07006882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006883 "clear vha in host_map %p %06x\n", vha, key);
6884 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6885 if (slot)
6886 btree_remove32(&vha->hw->tgt.host_map, key);
6887 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006888 break;
6889 }
6890}
6891
Quinn Tran482c9dc2017-03-15 09:48:54 -07006892void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6893{
6894 unsigned long flags;
6895 struct qla_hw_data *ha = vha->hw;
6896
6897 if (!vha->d_id.b24) {
6898 spin_lock_irqsave(&ha->vport_slock, flags);
6899 vha->d_id = id;
6900 qlt_update_vp_map(vha, SET_AL_PA);
6901 spin_unlock_irqrestore(&ha->vport_slock, flags);
6902 } else if (vha->d_id.b24 != id.b24) {
6903 spin_lock_irqsave(&ha->vport_slock, flags);
6904 qlt_update_vp_map(vha, RESET_AL_PA);
6905 vha->d_id = id;
6906 qlt_update_vp_map(vha, SET_AL_PA);
6907 spin_unlock_irqrestore(&ha->vport_slock, flags);
6908 }
6909}
6910
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006911static int __init qlt_parse_ini_mode(void)
6912{
6913 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6914 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6915 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6916 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6917 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6918 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08006919 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
6920 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006921 else
6922 return false;
6923
6924 return true;
6925}
6926
6927int __init qlt_init(void)
6928{
6929 int ret;
6930
6931 if (!qlt_parse_ini_mode()) {
6932 ql_log(ql_log_fatal, NULL, 0xe06b,
6933 "qlt_parse_ini_mode() failed\n");
6934 return -EINVAL;
6935 }
6936
6937 if (!QLA_TGT_MODE_ENABLED())
6938 return 0;
6939
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006940 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6941 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6942 qla_tgt_mgmt_cmd), 0, NULL);
6943 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006944 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006945 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07006946 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006947 }
6948
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006949 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08006950 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
6951 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006952
6953 if (!qla_tgt_plogi_cachep) {
6954 ql_log(ql_log_fatal, NULL, 0xe06d,
6955 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6956 ret = -ENOMEM;
6957 goto out_mgmt_cmd_cachep;
6958 }
6959
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006960 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6961 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6962 if (!qla_tgt_mgmt_cmd_mempool) {
6963 ql_log(ql_log_fatal, NULL, 0xe06e,
6964 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6965 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006966 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006967 }
6968
6969 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6970 if (!qla_tgt_wq) {
6971 ql_log(ql_log_fatal, NULL, 0xe06f,
6972 "alloc_workqueue for qla_tgt_wq failed\n");
6973 ret = -ENOMEM;
6974 goto out_cmd_mempool;
6975 }
6976 /*
6977 * Return 1 to signal that initiator-mode is being disabled
6978 */
6979 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6980
6981out_cmd_mempool:
6982 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006983out_plogi_cachep:
6984 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006985out_mgmt_cmd_cachep:
6986 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006987 return ret;
6988}
6989
6990void qlt_exit(void)
6991{
6992 if (!QLA_TGT_MODE_ENABLED())
6993 return;
6994
6995 destroy_workqueue(qla_tgt_wq);
6996 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006997 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006998 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006999}