blob: f9ccf845d084a9c8407bc940bf9c70831cd6dc0e [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3 *
4 * based on qla2x00t.c code:
5 *
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
10 *
11 * Forward port and refactoring to modern qla2xxx and target/configfs
12 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
18 * of the License.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#include <linux/blkdev.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <asm/unaligned.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_tcq.h>
39#include <target/target_core_base.h>
40#include <target/target_core_fabric.h>
41
42#include "qla_def.h"
43#include "qla_target.h"
44
Arun Easid154f352014-09-25 06:14:48 -040045static int ql2xtgt_tape_enable;
46module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040050static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51module_param(qlini_mode, charp, S_IRUGO);
52MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
56 "enabled back; "
57 "\"disabled\" - initiator mode will never be enabled; "
Quinn Tranead03852017-01-19 22:28:01 -080058 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040060 "\"enabled\" (default) - initiator mode will always stay enabled.");
61
Quinn Tran99e1b682017-06-02 09:12:03 -070062static int ql_dm_tgt_ex_pct = 0;
Quinn Tranead03852017-01-19 22:28:01 -080063module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
68
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);
Quinn Tran3a33dc92017-06-02 09:12:04 -07001430 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
1431 "NPIV is in use. Can not stop target\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001432 return -EPERM;
1433 }
1434 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001435 if (tgt->tgt_stop || tgt->tgt_stopped) {
1436 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1437 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001438 mutex_unlock(&qla_tgt_mutex);
1439 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001440 }
1441
Quinn Tran3a33dc92017-06-02 09:12:04 -07001442 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001443 vha->host_no, vha);
1444 /*
1445 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1446 * Lock is needed, because we still can get an incoming packet.
1447 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001448 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001449 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001450 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001451 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001452 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001453 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001454 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001455
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001456 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1457 "Waiting for sess works (tgt %p)", tgt);
1458 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1459 while (!list_empty(&tgt->sess_works_list)) {
1460 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1461 flush_scheduled_work();
1462 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1463 }
1464 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1465
1466 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001467 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001468
1469 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1470
1471 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001472 if (!ha->flags.host_shutting_down &&
1473 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001474 qlt_disable_vha(vha);
1475
1476 /* Wait for sessions to clear out (just in case) */
1477 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001478 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001479}
1480EXPORT_SYMBOL(qlt_stop_phase1);
1481
1482/* Called by tcm_qla2xxx configfs code */
1483void qlt_stop_phase2(struct qla_tgt *tgt)
1484{
Quinn Tran3a33dc92017-06-02 09:12:04 -07001485 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001486
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 }
Quinn Tran3a33dc92017-06-02 09:12:04 -07001493 if (!tgt->tgt_stop) {
1494 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1495 "%s: phase1 stop is not completed\n", __func__);
1496 dump_stack();
1497 return;
1498 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001499
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001500 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001501 tgt->tgt_stop = 0;
1502 tgt->tgt_stopped = 1;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001503 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001504
Quinn Tran3a33dc92017-06-02 09:12:04 -07001505 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001506 tgt);
1507}
1508EXPORT_SYMBOL(qlt_stop_phase2);
1509
1510/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001511static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001512{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001513 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001514
Quinn Tran3a33dc92017-06-02 09:12:04 -07001515 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1516 !tgt->tgt_stopped)
1517 qlt_stop_phase1(tgt);
1518
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001519 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001520 qlt_stop_phase2(tgt);
1521
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001522 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001523
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001524 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001525 "Release of tgt %p finished\n", tgt);
1526
1527 kfree(tgt);
1528}
1529
1530/* ha->hardware_lock supposed to be held on entry */
1531static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1532 const void *param, unsigned int param_size)
1533{
1534 struct qla_tgt_sess_work_param *prm;
1535 unsigned long flags;
1536
1537 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1538 if (!prm) {
1539 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1540 "qla_target(%d): Unable to create session "
1541 "work, command will be refused", 0);
1542 return -ENOMEM;
1543 }
1544
1545 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1546 "Scheduling work (type %d, prm %p)"
1547 " to find session for param %p (size %d, tgt %p)\n",
1548 type, prm, param, param_size, tgt);
1549
1550 prm->type = type;
1551 memcpy(&prm->tm_iocb, param, param_size);
1552
1553 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1554 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1555 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1556
1557 schedule_work(&tgt->sess_work);
1558
1559 return 0;
1560}
1561
1562/*
1563 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1564 */
1565static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1566 struct imm_ntfy_from_isp *ntfy,
1567 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1568 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1569{
1570 struct qla_hw_data *ha = vha->hw;
1571 request_t *pkt;
1572 struct nack_to_isp *nack;
1573
Quinn Tranec7193e2017-03-15 09:48:55 -07001574 if (!ha->flags.fw_started)
1575 return;
1576
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001577 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1578
1579 /* Send marker if required */
1580 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1581 return;
1582
1583 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1584 if (!pkt) {
1585 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1586 "qla_target(%d): %s failed: unable to allocate "
1587 "request packet\n", vha->vp_idx, __func__);
1588 return;
1589 }
1590
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001591 if (vha->vha_tgt.qla_tgt != NULL)
1592 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001593
1594 pkt->entry_type = NOTIFY_ACK_TYPE;
1595 pkt->entry_count = 1;
1596
1597 nack = (struct nack_to_isp *)pkt;
1598 nack->ox_id = ntfy->ox_id;
1599
Quinn Tran726b8542017-01-19 22:28:00 -08001600 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001601 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1602 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1603 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001604 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001605 }
1606 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1607 nack->u.isp24.status = ntfy->u.isp24.status;
1608 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001609 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001610 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1611 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1612 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1613 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1614 nack->u.isp24.srr_reject_code = srr_reject_code;
1615 nack->u.isp24.srr_reject_code_expl = srr_explan;
1616 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1617
1618 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1619 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1620 vha->vp_idx, nack->u.isp24.status);
1621
Himanshu Madhani63163e02014-09-25 06:14:59 -04001622 /* Memory Barrier */
1623 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001624 qla2x00_start_iocbs(vha, vha->req);
1625}
1626
1627/*
1628 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1629 */
1630static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1631 struct abts_recv_from_24xx *abts, uint32_t status,
1632 bool ids_reversed)
1633{
1634 struct qla_hw_data *ha = vha->hw;
1635 struct abts_resp_to_24xx *resp;
1636 uint32_t f_ctl;
1637 uint8_t *p;
1638
1639 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1640 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1641 ha, abts, status);
1642
1643 /* Send marker if required */
1644 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1645 return;
1646
Arun Easib6a029e2014-09-25 06:14:52 -04001647 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001648 if (!resp) {
1649 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1650 "qla_target(%d): %s failed: unable to allocate "
1651 "request packet", vha->vp_idx, __func__);
1652 return;
1653 }
1654
1655 resp->entry_type = ABTS_RESP_24XX;
1656 resp->entry_count = 1;
1657 resp->nport_handle = abts->nport_handle;
1658 resp->vp_index = vha->vp_idx;
1659 resp->sof_type = abts->sof_type;
1660 resp->exchange_address = abts->exchange_address;
1661 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001662 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001663 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1664 F_CTL_SEQ_INITIATIVE);
1665 p = (uint8_t *)&f_ctl;
1666 resp->fcp_hdr_le.f_ctl[0] = *p++;
1667 resp->fcp_hdr_le.f_ctl[1] = *p++;
1668 resp->fcp_hdr_le.f_ctl[2] = *p;
1669 if (ids_reversed) {
1670 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1671 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1672 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1673 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1674 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1675 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1676 } else {
1677 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1678 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1679 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1680 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1681 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1682 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1683 }
1684 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1685 if (status == FCP_TMF_CMPL) {
1686 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1687 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1688 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1689 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1690 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1691 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1692 } else {
1693 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1694 resp->payload.ba_rjt.reason_code =
1695 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1696 /* Other bytes are zero */
1697 }
1698
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001699 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001700
Himanshu Madhani63163e02014-09-25 06:14:59 -04001701 /* Memory Barrier */
1702 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001703 qla2x00_start_iocbs(vha, vha->req);
1704}
1705
1706/*
1707 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1708 */
1709static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1710 struct abts_resp_from_24xx_fw *entry)
1711{
1712 struct ctio7_to_24xx *ctio;
1713
1714 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1715 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1716 /* Send marker if required */
1717 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1718 return;
1719
Arun Easib6a029e2014-09-25 06:14:52 -04001720 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001721 if (ctio == NULL) {
1722 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1723 "qla_target(%d): %s failed: unable to allocate "
1724 "request packet\n", vha->vp_idx, __func__);
1725 return;
1726 }
1727
1728 /*
1729 * We've got on entrance firmware's response on by us generated
1730 * ABTS response. So, in it ID fields are reversed.
1731 */
1732
1733 ctio->entry_type = CTIO_TYPE7;
1734 ctio->entry_count = 1;
1735 ctio->nport_handle = entry->nport_handle;
1736 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001737 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001738 ctio->vp_index = vha->vp_idx;
1739 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1740 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1741 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1742 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001743 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1744 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001745 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001746
Himanshu Madhani63163e02014-09-25 06:14:59 -04001747 /* Memory Barrier */
1748 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001749 qla2x00_start_iocbs(vha, vha->req);
1750
1751 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1752 FCP_TMF_CMPL, true);
1753}
1754
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001755static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1756{
1757 struct qla_tgt_sess_op *op;
1758 struct qla_tgt_cmd *cmd;
Quinn Tran8b631d82017-06-02 09:11:54 -07001759 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001760
Quinn Tran8b631d82017-06-02 09:11:54 -07001761 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001762 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1763 if (tag == op->atio.u.isp24.exchange_addr) {
1764 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001765 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001766 return 1;
1767 }
1768 }
1769
Quinn Tran41dc5292017-01-19 22:28:03 -08001770 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1771 if (tag == op->atio.u.isp24.exchange_addr) {
1772 op->aborted = true;
Quinn Tran8b631d82017-06-02 09:11:54 -07001773 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08001774 return 1;
1775 }
1776 }
1777
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001778 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1779 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001780 cmd->aborted = 1;
Quinn Tran8b631d82017-06-02 09:11:54 -07001781 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001782 return 1;
1783 }
1784 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001785 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001786
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001787 return 0;
1788}
1789
1790/* drop cmds for the given lun
1791 * XXX only looks for cmds on the port through which lun reset was recieved
1792 * XXX does not go through the list of other port (which may have cmds
1793 * for the same lun)
1794 */
1795static void abort_cmds_for_lun(struct scsi_qla_host *vha,
Quinn Tranf775bd12017-06-02 09:11:59 -07001796 u64 lun, uint8_t *s_id)
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001797{
1798 struct qla_tgt_sess_op *op;
1799 struct qla_tgt_cmd *cmd;
1800 uint32_t key;
Quinn Tran8b631d82017-06-02 09:11:54 -07001801 unsigned long flags;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001802
1803 key = sid_to_key(s_id);
Quinn Tran8b631d82017-06-02 09:11:54 -07001804 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001805 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1806 uint32_t op_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001807 u64 op_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001808
1809 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1810 op_lun = scsilun_to_int(
1811 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1812 if (op_key == key && op_lun == lun)
1813 op->aborted = true;
1814 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001815
1816 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1817 uint32_t op_key;
1818 u64 op_lun;
1819
1820 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1821 op_lun = scsilun_to_int(
1822 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1823 if (op_key == key && op_lun == lun)
1824 op->aborted = true;
1825 }
1826
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001827 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1828 uint32_t cmd_key;
Quinn Tranf775bd12017-06-02 09:11:59 -07001829 u64 cmd_lun;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001830
1831 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1832 cmd_lun = scsilun_to_int(
1833 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1834 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001835 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001836 }
Quinn Tran8b631d82017-06-02 09:11:54 -07001837 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001838}
1839
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001840/* ha->hardware_lock supposed to be held on entry */
1841static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001842 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001843{
1844 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001845 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001846 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07001847 struct qla_tgt_cmd *cmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001848 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001849 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001850 bool found_lun = false;
Quinn Tran5d964832017-01-19 22:27:59 -08001851 unsigned long flags;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001852
Quinn Tran5d964832017-01-19 22:27:59 -08001853 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
Steve Hodgson06e97b42012-11-16 08:06:17 -08001854 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
Bart Van Assche649ee052015-04-14 13:26:44 +02001855 if (se_cmd->tag == abts->exchange_addr_to_abort) {
Steve Hodgson06e97b42012-11-16 08:06:17 -08001856 found_lun = true;
1857 break;
1858 }
1859 }
Quinn Tran5d964832017-01-19 22:27:59 -08001860 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Steve Hodgson06e97b42012-11-16 08:06:17 -08001861
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001862 /* cmd not in LIO lists, look in qla list */
1863 if (!found_lun) {
1864 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1865 /* send TASK_ABORT response immediately */
1866 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1867 return 0;
1868 } else {
1869 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1870 "unable to find cmd in driver or LIO for tag 0x%x\n",
1871 abts->exchange_addr_to_abort);
1872 return -ENOENT;
1873 }
1874 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001875
1876 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1877 "qla_target(%d): task abort (tag=%d)\n",
1878 vha->vp_idx, abts->exchange_addr_to_abort);
1879
1880 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1881 if (mcmd == NULL) {
1882 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1883 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1884 vha->vp_idx, __func__);
1885 return -ENOMEM;
1886 }
1887 memset(mcmd, 0, sizeof(*mcmd));
1888
Quinn Tranf775bd12017-06-02 09:11:59 -07001889 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001890 mcmd->sess = sess;
1891 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Arun Easi80187f82014-09-25 06:14:53 -04001892 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001893 mcmd->tmr_func = QLA_TGT_ABTS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001894
Quinn Tranf775bd12017-06-02 09:11:59 -07001895 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, cmd->unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001896 abts->exchange_addr_to_abort);
1897 if (rc != 0) {
1898 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1899 "qla_target(%d): tgt_ops->handle_tmr()"
1900 " failed: %d", vha->vp_idx, rc);
1901 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1902 return -EFAULT;
1903 }
1904
1905 return 0;
1906}
1907
1908/*
1909 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1910 */
1911static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1912 struct abts_recv_from_24xx *abts)
1913{
1914 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001915 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001916 uint32_t tag = abts->exchange_addr_to_abort;
1917 uint8_t s_id[3];
1918 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001919 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001920
1921 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1922 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1923 "qla_target(%d): ABTS: Abort Sequence not "
1924 "supported\n", vha->vp_idx);
1925 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1926 return;
1927 }
1928
1929 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1930 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1931 "qla_target(%d): ABTS: Unknown Exchange "
1932 "Address received\n", vha->vp_idx);
1933 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1934 return;
1935 }
1936
1937 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1938 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1939 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1940 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1941 le32_to_cpu(abts->fcp_hdr_le.parameter));
1942
1943 s_id[0] = abts->fcp_hdr_le.s_id[2];
1944 s_id[1] = abts->fcp_hdr_le.s_id[1];
1945 s_id[2] = abts->fcp_hdr_le.s_id[0];
1946
Quinn Tran75601512015-12-17 14:57:04 -05001947 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001948 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1949 if (!sess) {
1950 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1951 "qla_target(%d): task abort for non-existant session\n",
1952 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001953 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001954 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001955
1956 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1957
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001958 if (rc != 0) {
1959 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1960 false);
1961 }
1962 return;
1963 }
Quinn Tran75601512015-12-17 14:57:04 -05001964 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1965
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001966
Quinn Tran726b8542017-01-19 22:28:00 -08001967 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001968 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1969 return;
1970 }
1971
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001972 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1973 if (rc != 0) {
1974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1975 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1976 vha->vp_idx, rc);
1977 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1978 return;
1979 }
1980}
1981
1982/*
1983 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1984 */
1985static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1986 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1987{
1988 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1989 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001990 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001991
1992 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1993 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1994 ha, atio, resp_code);
1995
1996 /* Send marker if required */
1997 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1998 return;
1999
2000 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
2001 if (ctio == NULL) {
2002 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2003 "qla_target(%d): %s failed: unable to allocate "
2004 "request packet\n", ha->vp_idx, __func__);
2005 return;
2006 }
2007
2008 ctio->entry_type = CTIO_TYPE7;
2009 ctio->entry_count = 1;
2010 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2011 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002012 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002013 ctio->vp_index = ha->vp_idx;
2014 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2015 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2016 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2017 ctio->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002018 temp = (atio->u.isp24.attr << 9)|
2019 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2020 ctio->u.status1.flags = cpu_to_le16(temp);
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;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002073 temp = (atio->u.isp24.attr << 9) |
2074 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2075 ctio->u.status1.flags = cpu_to_le16(temp);
Quinn Tranbe251522017-03-15 09:48:49 -07002076 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2077 ctio->u.status1.ox_id = cpu_to_le16(temp);
2078 ctio->u.status1.scsi_status =
2079 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2080 ctio->u.status1.response_len = cpu_to_le16(18);
2081 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2082
2083 if (ctio->u.status1.residual != 0)
2084 ctio->u.status1.scsi_status |=
2085 cpu_to_le16(SS_RESIDUAL_UNDER);
2086
2087 /* Response code and sense key */
2088 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2089 (&ctio->u.status1.sense_data)[0]);
2090 /* Additional sense length */
2091 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2092 /* ASC and ASCQ */
2093 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2094 (&ctio->u.status1.sense_data)[3]);
2095
2096 /* Memory Barrier */
2097 wmb();
2098
2099 qla2x00_start_iocbs(vha, vha->req);
2100out:
2101 return;
2102}
2103
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002104/* callback from target fabric module code */
2105void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2106{
2107 struct scsi_qla_host *vha = mcmd->sess->vha;
2108 struct qla_hw_data *ha = vha->hw;
2109 unsigned long flags;
2110
2111 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2112 "TM response mcmd (%p) status %#x state %#x",
2113 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2114
2115 spin_lock_irqsave(&ha->hardware_lock, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002116
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002117 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002118 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002119 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002120 * previous life, just abort the processing.
2121 */
2122 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002123 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2124 vha->flags.online, qla2x00_reset_active(vha),
2125 mcmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002126 ha->tgt.tgt_ops->free_mcmd(mcmd);
2127 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2128 return;
2129 }
2130
Quinn Tran726b8542017-01-19 22:28:00 -08002131 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2132 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002133 ELS_LOGO ||
2134 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2135 ELS_PRLO ||
2136 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2137 ELS_TPRLO) {
Quinn Tran83548fe2017-06-02 09:12:01 -07002138 ql_dbg(ql_dbg_disc, vha, 0x2106,
Quinn Tran726b8542017-01-19 22:28:00 -08002139 "TM response logo %phC status %#x state %#x",
2140 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2141 mcmd->flags);
2142 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2143 } else {
2144 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
2145 0, 0, 0, 0, 0, 0);
2146 }
2147 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002148 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002149 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
2150 mcmd->fc_tm_rsp, false);
2151 else
2152 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
2153 mcmd->fc_tm_rsp);
2154 }
2155 /*
2156 * Make the callback for ->free_mcmd() to queue_work() and invoke
2157 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2158 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2159 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2160 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2161 * qlt_xmit_tm_rsp() returns here..
2162 */
2163 ha->tgt.tgt_ops->free_mcmd(mcmd);
2164 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2165}
2166EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2167
2168/* No locks */
2169static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2170{
2171 struct qla_tgt_cmd *cmd = prm->cmd;
2172
2173 BUG_ON(cmd->sg_cnt == 0);
2174
2175 prm->sg = (struct scatterlist *)cmd->sg;
2176 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
2177 cmd->sg_cnt, cmd->dma_data_direction);
2178 if (unlikely(prm->seg_cnt == 0))
2179 goto out_err;
2180
2181 prm->cmd->sg_mapped = 1;
2182
Quinn Tranf83adb62014-04-11 16:54:43 -04002183 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2184 /*
2185 * If greater than four sg entries then we need to allocate
2186 * the continuation entries
2187 */
2188 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
2189 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2190 prm->tgt->datasegs_per_cmd,
2191 prm->tgt->datasegs_per_cont);
2192 } else {
2193 /* DIF */
2194 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2195 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2196 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2197 prm->tot_dsds = prm->seg_cnt;
2198 } else
2199 prm->tot_dsds = prm->seg_cnt;
2200
2201 if (cmd->prot_sg_cnt) {
2202 prm->prot_sg = cmd->prot_sg;
2203 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
2204 cmd->prot_sg, cmd->prot_sg_cnt,
2205 cmd->dma_data_direction);
2206 if (unlikely(prm->prot_seg_cnt == 0))
2207 goto out_err;
2208
2209 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2210 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2211 /* Dif Bundling not support here */
2212 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2213 cmd->blk_sz);
2214 prm->tot_dsds += prm->prot_seg_cnt;
2215 } else
2216 prm->tot_dsds += prm->prot_seg_cnt;
2217 }
2218 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002219
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002220 return 0;
2221
2222out_err:
2223 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
2224 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2225 0, prm->cmd->sg_cnt);
2226 return -1;
2227}
2228
Joern Engelf9b67212014-09-16 16:23:18 -04002229static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002230{
2231 struct qla_hw_data *ha = vha->hw;
2232
Joern Engelf9b67212014-09-16 16:23:18 -04002233 if (!cmd->sg_mapped)
2234 return;
2235
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002236 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2237 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002238
2239 if (cmd->prot_sg_cnt)
2240 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2241 cmd->dma_data_direction);
2242
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002243 if (!cmd->ctx)
2244 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04002245
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -07002246 if (cmd->ctx_dsd_alloced)
2247 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2248
2249 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002250}
2251
2252static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
2253 uint32_t req_cnt)
2254{
Saurav Kashyapd29fb732014-09-25 06:14:49 -04002255 uint32_t cnt, cnt_in;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002256
2257 if (vha->req->cnt < (req_cnt + 2)) {
Arun Easi75554b62014-09-25 06:14:45 -04002258 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
Saurav Kashyapd29fb732014-09-25 06:14:49 -04002259 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002260
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002261 if (vha->req->ring_index < cnt)
2262 vha->req->cnt = cnt - vha->req->ring_index;
2263 else
2264 vha->req->cnt = vha->req->length -
2265 (vha->req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002266
2267 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
2268 ql_dbg(ql_dbg_io, vha, 0x305a,
2269 "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",
2270 vha->vp_idx, vha->req->ring_index,
2271 vha->req->cnt, req_cnt, cnt, cnt_in,
2272 vha->req->length);
2273 return -EAGAIN;
2274 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002275 }
2276
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002277 vha->req->cnt -= req_cnt;
2278
2279 return 0;
2280}
2281
2282/*
2283 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2284 */
2285static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
2286{
2287 /* Adjust ring index. */
2288 vha->req->ring_index++;
2289 if (vha->req->ring_index == vha->req->length) {
2290 vha->req->ring_index = 0;
2291 vha->req->ring_ptr = vha->req->ring;
2292 } else {
2293 vha->req->ring_ptr++;
2294 }
2295 return (cont_entry_t *)vha->req->ring_ptr;
2296}
2297
2298/* ha->hardware_lock supposed to be held on entry */
2299static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
2300{
2301 struct qla_hw_data *ha = vha->hw;
2302 uint32_t h;
2303
2304 h = ha->tgt.current_handle;
2305 /* always increment cmd handle */
2306 do {
2307 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002308 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002309 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
2310 if (h == ha->tgt.current_handle) {
Arun Easi667024a2014-09-25 06:14:47 -04002311 ql_dbg(ql_dbg_io, vha, 0x305b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002312 "qla_target(%d): Ran out of "
2313 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
2314 h = QLA_TGT_NULL_HANDLE;
2315 break;
2316 }
2317 } while ((h == QLA_TGT_NULL_HANDLE) ||
2318 (h == QLA_TGT_SKIP_HANDLE) ||
2319 (ha->tgt.cmds[h-1] != NULL));
2320
2321 if (h != QLA_TGT_NULL_HANDLE)
2322 ha->tgt.current_handle = h;
2323
2324 return h;
2325}
2326
2327/* ha->hardware_lock supposed to be held on entry */
2328static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
2329 struct scsi_qla_host *vha)
2330{
2331 uint32_t h;
2332 struct ctio7_to_24xx *pkt;
2333 struct qla_hw_data *ha = vha->hw;
2334 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002335 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002336
2337 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
2338 prm->pkt = pkt;
2339 memset(pkt, 0, sizeof(*pkt));
2340
2341 pkt->entry_type = CTIO_TYPE7;
2342 pkt->entry_count = (uint8_t)prm->req_cnt;
2343 pkt->vp_index = vha->vp_idx;
2344
2345 h = qlt_make_handle(vha);
2346 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2347 /*
2348 * CTIO type 7 from the firmware doesn't provide a way to
2349 * know the initiator's LOOP ID, hence we can't find
2350 * the session and, so, the command.
2351 */
2352 return -EAGAIN;
2353 } else
Quinn Tranbe251522017-03-15 09:48:49 -07002354 ha->tgt.cmds[h - 1] = prm->cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002355
2356 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2357 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002358 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002359 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2360 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2361 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2362 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07002363 temp = atio->u.isp24.attr << 9;
2364 pkt->u.status0.flags |= cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002365 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2366 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002367 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2368
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002369 return 0;
2370}
2371
2372/*
2373 * ha->hardware_lock supposed to be held on entry. We have already made sure
2374 * that there is sufficient amount of request entries to not drop it.
2375 */
2376static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
2377 struct scsi_qla_host *vha)
2378{
2379 int cnt;
2380 uint32_t *dword_ptr;
2381 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2382
2383 /* Build continuation packets */
2384 while (prm->seg_cnt > 0) {
2385 cont_a64_entry_t *cont_pkt64 =
2386 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2387
2388 /*
2389 * Make sure that from cont_pkt64 none of
2390 * 64-bit specific fields used for 32-bit
2391 * addressing. Cast to (cont_entry_t *) for
2392 * that.
2393 */
2394
2395 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2396
2397 cont_pkt64->entry_count = 1;
2398 cont_pkt64->sys_define = 0;
2399
2400 if (enable_64bit_addressing) {
2401 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2402 dword_ptr =
2403 (uint32_t *)&cont_pkt64->dseg_0_address;
2404 } else {
2405 cont_pkt64->entry_type = CONTINUE_TYPE;
2406 dword_ptr =
2407 (uint32_t *)&((cont_entry_t *)
2408 cont_pkt64)->dseg_0_address;
2409 }
2410
2411 /* Load continuation entry data segments */
2412 for (cnt = 0;
2413 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2414 cnt++, prm->seg_cnt--) {
2415 *dword_ptr++ =
2416 cpu_to_le32(pci_dma_lo32
2417 (sg_dma_address(prm->sg)));
2418 if (enable_64bit_addressing) {
2419 *dword_ptr++ =
2420 cpu_to_le32(pci_dma_hi32
2421 (sg_dma_address
2422 (prm->sg)));
2423 }
2424 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2425
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002426 prm->sg = sg_next(prm->sg);
2427 }
2428 }
2429}
2430
2431/*
2432 * ha->hardware_lock supposed to be held on entry. We have already made sure
2433 * that there is sufficient amount of request entries to not drop it.
2434 */
2435static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2436 struct scsi_qla_host *vha)
2437{
2438 int cnt;
2439 uint32_t *dword_ptr;
2440 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2441 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2442
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002443 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2444
2445 /* Setup packet address segment pointer */
2446 dword_ptr = pkt24->u.status0.dseg_0_address;
2447
2448 /* Set total data segment count */
2449 if (prm->seg_cnt)
2450 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2451
2452 if (prm->seg_cnt == 0) {
2453 /* No data transfer */
2454 *dword_ptr++ = 0;
2455 *dword_ptr = 0;
2456 return;
2457 }
2458
2459 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002460
2461 /* Load command entry data segments */
2462 for (cnt = 0;
2463 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2464 cnt++, prm->seg_cnt--) {
2465 *dword_ptr++ =
2466 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2467 if (enable_64bit_addressing) {
2468 *dword_ptr++ =
2469 cpu_to_le32(pci_dma_hi32(
2470 sg_dma_address(prm->sg)));
2471 }
2472 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2473
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002474 prm->sg = sg_next(prm->sg);
2475 }
2476
2477 qlt_load_cont_data_segments(prm, vha);
2478}
2479
2480static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2481{
2482 return cmd->bufflen > 0;
2483}
2484
Quinn Tranbe251522017-03-15 09:48:49 -07002485static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2486{
2487 struct qla_tgt_cmd *cmd;
2488 struct scsi_qla_host *vha;
2489
2490 /* asc 0x10=dif error */
2491 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2492 cmd = prm->cmd;
2493 vha = cmd->vha;
2494 /* ASCQ */
2495 switch (prm->sense_buffer[13]) {
2496 case 1:
Quinn Tran83548fe2017-06-02 09:12:01 -07002497 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
Quinn Tranbe251522017-03-15 09:48:49 -07002498 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2499 "se_cmd=%p tag[%x]",
2500 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2501 cmd->atio.u.isp24.exchange_addr);
2502 break;
2503 case 2:
Quinn Tran83548fe2017-06-02 09:12:01 -07002504 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
Quinn Tranbe251522017-03-15 09:48:49 -07002505 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2506 "se_cmd=%p tag[%x]",
2507 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2508 cmd->atio.u.isp24.exchange_addr);
2509 break;
2510 case 3:
Quinn Tran83548fe2017-06-02 09:12:01 -07002511 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
Quinn Tranbe251522017-03-15 09:48:49 -07002512 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2513 "se_cmd=%p tag[%x]",
2514 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2515 cmd->atio.u.isp24.exchange_addr);
2516 break;
2517 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07002518 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
Quinn Tranbe251522017-03-15 09:48:49 -07002519 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2520 "se_cmd=%p tag[%x]",
2521 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2522 cmd->atio.u.isp24.exchange_addr);
2523 break;
2524 }
Quinn Tran83548fe2017-06-02 09:12:01 -07002525 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
Quinn Tranbe251522017-03-15 09:48:49 -07002526 }
2527}
2528
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002529/*
2530 * Called without ha->hardware_lock held
2531 */
2532static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2533 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2534 uint32_t *full_req_cnt)
2535{
2536 struct qla_tgt *tgt = cmd->tgt;
2537 struct scsi_qla_host *vha = tgt->vha;
2538 struct qla_hw_data *ha = vha->hw;
2539 struct se_cmd *se_cmd = &cmd->se_cmd;
2540
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002541 prm->cmd = cmd;
2542 prm->tgt = tgt;
2543 prm->rq_result = scsi_status;
2544 prm->sense_buffer = &cmd->sense_buffer[0];
2545 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2546 prm->sg = NULL;
2547 prm->seg_cnt = -1;
2548 prm->req_cnt = 1;
2549 prm->add_status_pkt = 0;
2550
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002551 /* Send marker if required */
2552 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2553 return -EFAULT;
2554
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002555 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2556 if (qlt_pci_map_calc_cnt(prm) != 0)
2557 return -EAGAIN;
2558 }
2559
2560 *full_req_cnt = prm->req_cnt;
2561
2562 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2563 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002564 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002565 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2566 prm->residual, se_cmd->tag,
2567 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2568 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002569 prm->rq_result |= SS_RESIDUAL_UNDER;
2570 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2571 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002572 ql_dbg(ql_dbg_io, vha, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002573 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2574 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2575 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002576 prm->rq_result |= SS_RESIDUAL_OVER;
2577 }
2578
2579 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2580 /*
2581 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2582 * ignored in *xmit_response() below
2583 */
2584 if (qlt_has_data(cmd)) {
2585 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2586 (IS_FWI2_CAPABLE(ha) &&
2587 (prm->rq_result != 0))) {
2588 prm->add_status_pkt = 1;
2589 (*full_req_cnt)++;
2590 }
2591 }
2592 }
2593
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002594 return 0;
2595}
2596
2597static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2598 struct qla_tgt_cmd *cmd, int sending_sense)
2599{
2600 if (ha->tgt.enable_class_2)
2601 return 0;
2602
2603 if (sending_sense)
2604 return cmd->conf_compl_supported;
2605 else
2606 return ha->tgt.enable_explicit_conf &&
2607 cmd->conf_compl_supported;
2608}
2609
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002610static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2611 struct qla_tgt_prm *prm)
2612{
2613 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2614 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002615 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002616 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002617 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002618 CTIO7_FLAGS_EXPLICIT_CONFORM |
2619 CTIO7_FLAGS_CONFORM_REQ);
2620 }
2621 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2622 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2623 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2624 int i;
2625
2626 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002627 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002628 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2629 "Skipping EXPLICIT_CONFORM and "
2630 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2631 "non GOOD status\n");
2632 goto skip_explict_conf;
2633 }
Bart Van Asschead950362015-07-09 07:24:08 -07002634 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002635 CTIO7_FLAGS_EXPLICIT_CONFORM |
2636 CTIO7_FLAGS_CONFORM_REQ);
2637 }
2638skip_explict_conf:
2639 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002640 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002641 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002642 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002643 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002644 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002645 ctio->u.status1.sense_length =
2646 cpu_to_le16(prm->sense_buffer_len);
2647 for (i = 0; i < prm->sense_buffer_len/4; i++)
2648 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2649 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
Quinn Tranbe251522017-03-15 09:48:49 -07002650
2651 qlt_print_dif_err(prm);
2652
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002653 } else {
2654 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002655 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002656 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002657 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002658 ctio->u.status1.sense_length = 0;
2659 memset(ctio->u.status1.sense_data, 0,
2660 sizeof(ctio->u.status1.sense_data));
2661 }
2662
2663 /* Sense with len > 24, is it possible ??? */
2664}
2665
Quinn Tranf83adb62014-04-11 16:54:43 -04002666static inline int
2667qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2668{
Quinn Tranf83adb62014-04-11 16:54:43 -04002669 switch (se_cmd->prot_op) {
2670 case TARGET_PROT_DOUT_INSERT:
2671 case TARGET_PROT_DIN_STRIP:
2672 if (ql2xenablehba_err_chk >= 1)
2673 return 1;
2674 break;
2675 case TARGET_PROT_DOUT_PASS:
2676 case TARGET_PROT_DIN_PASS:
2677 if (ql2xenablehba_err_chk >= 2)
2678 return 1;
2679 break;
2680 case TARGET_PROT_DIN_INSERT:
2681 case TARGET_PROT_DOUT_STRIP:
2682 return 1;
2683 default:
2684 break;
2685 }
2686 return 0;
2687}
2688
Quinn Tranbe251522017-03-15 09:48:49 -07002689static inline int
2690qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
Quinn Tranf83adb62014-04-11 16:54:43 -04002691{
Quinn Tranbe251522017-03-15 09:48:49 -07002692 switch (se_cmd->prot_op) {
2693 case TARGET_PROT_DIN_INSERT:
2694 case TARGET_PROT_DOUT_INSERT:
2695 case TARGET_PROT_DIN_STRIP:
2696 case TARGET_PROT_DOUT_STRIP:
2697 case TARGET_PROT_DIN_PASS:
2698 case TARGET_PROT_DOUT_PASS:
2699 return 1;
2700 default:
2701 return 0;
2702 }
2703 return 0;
2704}
Quinn Tranf83adb62014-04-11 16:54:43 -04002705
Quinn Tranbe251522017-03-15 09:48:49 -07002706/*
2707 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2708 */
2709static void
2710qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2711 uint16_t *pfw_prot_opts)
2712{
2713 struct se_cmd *se_cmd = &cmd->se_cmd;
2714 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2715 scsi_qla_host_t *vha = cmd->tgt->vha;
2716 struct qla_hw_data *ha = vha->hw;
2717 uint32_t t32 = 0;
2718
2719 /*
2720 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
Quinn Tranf83adb62014-04-11 16:54:43 -04002721 * have been immplemented by TCM, before AppTag is avail.
2722 * Look for modesense_handlers[]
2723 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002724 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002725 ctx->app_tag_mask[0] = 0x0;
2726 ctx->app_tag_mask[1] = 0x0;
2727
Quinn Tranbe251522017-03-15 09:48:49 -07002728 if (IS_PI_UNINIT_CAPABLE(ha)) {
2729 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2730 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2731 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2732 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2733 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2734 }
2735
2736 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2737
Quinn Tranf83adb62014-04-11 16:54:43 -04002738 switch (se_cmd->prot_type) {
2739 case TARGET_DIF_TYPE0_PROT:
2740 /*
Quinn Tranbe251522017-03-15 09:48:49 -07002741 * No check for ql2xenablehba_err_chk, as it
2742 * would be an I/O error if hba tag generation
2743 * is not done.
Quinn Tranf83adb62014-04-11 16:54:43 -04002744 */
2745 ctx->ref_tag = cpu_to_le32(lba);
Quinn Tranf83adb62014-04-11 16:54:43 -04002746 /* enable ALL bytes of the ref tag */
2747 ctx->ref_tag_mask[0] = 0xff;
2748 ctx->ref_tag_mask[1] = 0xff;
2749 ctx->ref_tag_mask[2] = 0xff;
2750 ctx->ref_tag_mask[3] = 0xff;
2751 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002752 case TARGET_DIF_TYPE1_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002753 /*
2754 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2755 * REF tag, and 16 bit app tag.
2756 */
2757 ctx->ref_tag = cpu_to_le32(lba);
2758 if (!qla_tgt_ref_mask_check(se_cmd) ||
2759 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2760 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2761 break;
2762 }
2763 /* enable ALL bytes of the ref tag */
2764 ctx->ref_tag_mask[0] = 0xff;
2765 ctx->ref_tag_mask[1] = 0xff;
2766 ctx->ref_tag_mask[2] = 0xff;
2767 ctx->ref_tag_mask[3] = 0xff;
2768 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002769 case TARGET_DIF_TYPE2_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002770 /*
2771 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2772 * tag has to match LBA in CDB + N
2773 */
2774 ctx->ref_tag = cpu_to_le32(lba);
2775 if (!qla_tgt_ref_mask_check(se_cmd) ||
2776 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2777 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2778 break;
2779 }
2780 /* enable ALL bytes of the ref tag */
2781 ctx->ref_tag_mask[0] = 0xff;
2782 ctx->ref_tag_mask[1] = 0xff;
2783 ctx->ref_tag_mask[2] = 0xff;
2784 ctx->ref_tag_mask[3] = 0xff;
2785 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002786 case TARGET_DIF_TYPE3_PROT:
Quinn Tranbe251522017-03-15 09:48:49 -07002787 /* For TYPE 3 protection: 16 bit GUARD only */
2788 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2789 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2790 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2791 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002792 }
2793}
2794
Quinn Tranf83adb62014-04-11 16:54:43 -04002795static inline int
2796qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2797{
2798 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002799 uint32_t transfer_length = 0;
2800 uint32_t data_bytes;
2801 uint32_t dif_bytes;
2802 uint8_t bundling = 1;
2803 uint8_t *clr_ptr;
2804 struct crc_context *crc_ctx_pkt = NULL;
2805 struct qla_hw_data *ha;
2806 struct ctio_crc2_to_fw *pkt;
2807 dma_addr_t crc_ctx_dma;
2808 uint16_t fw_prot_opts = 0;
2809 struct qla_tgt_cmd *cmd = prm->cmd;
2810 struct se_cmd *se_cmd = &cmd->se_cmd;
2811 uint32_t h;
2812 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranbe251522017-03-15 09:48:49 -07002813 struct qla_tc_param tc;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002814 uint16_t t16;
Quinn Tranf83adb62014-04-11 16:54:43 -04002815
Quinn Tranf83adb62014-04-11 16:54:43 -04002816 ha = vha->hw;
2817
2818 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2819 prm->pkt = pkt;
2820 memset(pkt, 0, sizeof(*pkt));
2821
2822 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2823 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2824 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2825 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2826
2827 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2828 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2829 bundling = 0;
2830
2831 /* Compute dif len and adjust data len to incude protection */
2832 data_bytes = cmd->bufflen;
2833 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2834
2835 switch (se_cmd->prot_op) {
2836 case TARGET_PROT_DIN_INSERT:
2837 case TARGET_PROT_DOUT_STRIP:
2838 transfer_length = data_bytes;
Quinn Tranbe251522017-03-15 09:48:49 -07002839 if (cmd->prot_sg_cnt)
2840 data_bytes += dif_bytes;
Quinn Tranf83adb62014-04-11 16:54:43 -04002841 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002842 case TARGET_PROT_DIN_STRIP:
2843 case TARGET_PROT_DOUT_INSERT:
2844 case TARGET_PROT_DIN_PASS:
2845 case TARGET_PROT_DOUT_PASS:
2846 transfer_length = data_bytes + dif_bytes;
2847 break;
Quinn Tranf83adb62014-04-11 16:54:43 -04002848 default:
2849 BUG();
2850 break;
2851 }
2852
2853 if (!qlt_hba_err_chk_enabled(se_cmd))
2854 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2855 /* HBA error checking enabled */
2856 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2857 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2858 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2859 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2860 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2861 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2862 }
2863
2864 switch (se_cmd->prot_op) {
2865 case TARGET_PROT_DIN_INSERT:
2866 case TARGET_PROT_DOUT_INSERT:
2867 fw_prot_opts |= PO_MODE_DIF_INSERT;
2868 break;
2869 case TARGET_PROT_DIN_STRIP:
2870 case TARGET_PROT_DOUT_STRIP:
2871 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2872 break;
2873 case TARGET_PROT_DIN_PASS:
2874 case TARGET_PROT_DOUT_PASS:
2875 fw_prot_opts |= PO_MODE_DIF_PASS;
2876 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2877 break;
2878 default:/* Normal Request */
2879 fw_prot_opts |= PO_MODE_DIF_PASS;
2880 break;
2881 }
2882
Quinn Tranf83adb62014-04-11 16:54:43 -04002883 /* ---- PKT ---- */
2884 /* Update entry type to indicate Command Type CRC_2 IOCB */
2885 pkt->entry_type = CTIO_CRC2;
2886 pkt->entry_count = 1;
2887 pkt->vp_index = vha->vp_idx;
2888
2889 h = qlt_make_handle(vha);
2890 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2891 /*
2892 * CTIO type 7 from the firmware doesn't provide a way to
2893 * know the initiator's LOOP ID, hence we can't find
2894 * the session and, so, the command.
2895 */
2896 return -EAGAIN;
2897 } else
2898 ha->tgt.cmds[h-1] = prm->cmd;
2899
Quinn Tranf83adb62014-04-11 16:54:43 -04002900 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
Quinn Tranbe251522017-03-15 09:48:49 -07002901 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
Bart Van Asschead950362015-07-09 07:24:08 -07002902 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002903 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2904 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2905 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2906 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002907
2908 /* silence compile warning */
2909 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2910 pkt->ox_id = cpu_to_le16(t16);
2911
2912 t16 = (atio->u.isp24.attr << 9);
2913 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002914 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2915
2916 /* Set transfer direction */
2917 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002918 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002919 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002920 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002921
Quinn Tranf83adb62014-04-11 16:54:43 -04002922 pkt->dseg_count = prm->tot_dsds;
2923 /* Fibre channel byte count */
2924 pkt->transfer_length = cpu_to_le32(transfer_length);
2925
Quinn Tranf83adb62014-04-11 16:54:43 -04002926 /* ----- CRC context -------- */
2927
2928 /* Allocate CRC context from global pool */
2929 crc_ctx_pkt = cmd->ctx =
2930 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2931
2932 if (!crc_ctx_pkt)
2933 goto crc_queuing_error;
2934
2935 /* Zero out CTX area. */
2936 clr_ptr = (uint8_t *)crc_ctx_pkt;
2937 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2938
2939 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2940 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2941
2942 /* Set handle */
2943 crc_ctx_pkt->handle = pkt->handle;
2944
Quinn Tranbe251522017-03-15 09:48:49 -07002945 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
Quinn Tranf83adb62014-04-11 16:54:43 -04002946
2947 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2948 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2949 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2950
Quinn Tranf83adb62014-04-11 16:54:43 -04002951 if (!bundling) {
2952 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2953 } else {
2954 /*
2955 * Configure Bundling if we need to fetch interlaving
2956 * protection PCI accesses
2957 */
2958 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2959 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2960 crc_ctx_pkt->u.bundling.dseg_count =
2961 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2962 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2963 }
2964
2965 /* Finish the common fields of CRC pkt */
2966 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2967 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2968 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002969 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002970
Quinn Tranbe251522017-03-15 09:48:49 -07002971 memset((uint8_t *)&tc, 0 , sizeof(tc));
2972 tc.vha = vha;
2973 tc.blk_sz = cmd->blk_sz;
2974 tc.bufflen = cmd->bufflen;
2975 tc.sg = cmd->sg;
2976 tc.prot_sg = cmd->prot_sg;
2977 tc.ctx = crc_ctx_pkt;
2978 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
Quinn Tranf83adb62014-04-11 16:54:43 -04002979
2980 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07002981 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04002982
2983 if (!bundling && prm->prot_seg_cnt) {
2984 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07002985 prm->tot_dsds, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04002986 goto crc_queuing_error;
2987 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07002988 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04002989 goto crc_queuing_error;
2990
2991 if (bundling && prm->prot_seg_cnt) {
2992 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002993 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04002994
2995 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2996 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
Quinn Tranbe251522017-03-15 09:48:49 -07002997 prm->prot_seg_cnt, &tc))
Quinn Tranf83adb62014-04-11 16:54:43 -04002998 goto crc_queuing_error;
2999 }
3000 return QLA_SUCCESS;
3001
3002crc_queuing_error:
3003 /* Cleanup will be performed by the caller */
Quinn Tranbe251522017-03-15 09:48:49 -07003004 vha->hw->tgt.cmds[h - 1] = NULL;
Quinn Tranf83adb62014-04-11 16:54:43 -04003005
3006 return QLA_FUNCTION_FAILED;
3007}
3008
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003009/*
3010 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3011 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3012 */
3013int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3014 uint8_t scsi_status)
3015{
3016 struct scsi_qla_host *vha = cmd->vha;
3017 struct qla_hw_data *ha = vha->hw;
3018 struct ctio7_to_24xx *pkt;
3019 struct qla_tgt_prm prm;
3020 uint32_t full_req_cnt = 0;
3021 unsigned long flags = 0;
3022 int res;
3023
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003024 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08003025 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003026 cmd->state = QLA_TGT_STATE_PROCESSED;
3027 if (cmd->sess->logout_completed)
3028 /* no need to terminate. FW already freed exchange. */
3029 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3030 else
Quinn Trana07100e2015-12-07 19:48:57 -05003031 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003032 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3033 return 0;
3034 }
3035 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3036
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003037 memset(&prm, 0, sizeof(prm));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003038
3039 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
Quinn Tranf83adb62014-04-11 16:54:43 -04003040 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
3041 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3042 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3043 &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003044
3045 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3046 &full_req_cnt);
3047 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003048 return res;
3049 }
3050
3051 spin_lock_irqsave(&ha->hardware_lock, flags);
3052
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003053 if (xmit_type == QLA_TGT_XMIT_STATUS)
3054 vha->tgt_counters.core_qla_snd_status++;
3055 else
3056 vha->tgt_counters.core_qla_que_buf++;
3057
Quinn Tranec7193e2017-03-15 09:48:55 -07003058 if (!ha->flags.fw_started || cmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04003059 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003060 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003061 * previous life, just abort the processing.
3062 */
3063 cmd->state = QLA_TGT_STATE_PROCESSED;
3064 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3065 ql_dbg(ql_dbg_async, vha, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003066 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3067 vha->flags.online, qla2x00_reset_active(vha),
3068 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003069 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3070 return 0;
3071 }
3072
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003073 /* Does F/W have an IOCBs for this request */
3074 res = qlt_check_reserve_free_req(vha, full_req_cnt);
3075 if (unlikely(res))
3076 goto out_unmap_unlock;
3077
Quinn Tranf83adb62014-04-11 16:54:43 -04003078 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3079 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3080 else
3081 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Quinn Tran810e30b2015-06-10 11:05:20 -04003082 if (unlikely(res != 0)) {
3083 vha->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003084 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04003085 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003086
3087 pkt = (struct ctio7_to_24xx *)prm.pkt;
3088
3089 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3090 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003091 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003092 CTIO7_FLAGS_STATUS_MODE_0);
3093
Quinn Tranf83adb62014-04-11 16:54:43 -04003094 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3095 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003096
3097 if (prm.add_status_pkt == 0) {
3098 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3099 pkt->u.status0.scsi_status =
3100 cpu_to_le16(prm.rq_result);
3101 pkt->u.status0.residual =
3102 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07003103 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003104 CTIO7_FLAGS_SEND_STATUS);
3105 if (qlt_need_explicit_conf(ha, cmd, 0)) {
3106 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07003107 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003108 CTIO7_FLAGS_EXPLICIT_CONFORM |
3109 CTIO7_FLAGS_CONFORM_REQ);
3110 }
3111 }
3112
3113 } else {
3114 /*
3115 * We have already made sure that there is sufficient
3116 * amount of request entries to not drop HW lock in
3117 * req_pkt().
3118 */
3119 struct ctio7_to_24xx *ctio =
3120 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
3121
Arun Easi667024a2014-09-25 06:14:47 -04003122 ql_dbg(ql_dbg_io, vha, 0x305e,
3123 "Building additional status packet 0x%p.\n",
3124 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003125
Quinn Tranf83adb62014-04-11 16:54:43 -04003126 /*
3127 * T10Dif: ctio_crc2_to_fw overlay ontop of
3128 * ctio7_to_24xx
3129 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003130 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04003131 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003132 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003133 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003134 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003135 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003136 CTIO7_FLAGS_DATA_IN);
3137
3138 /* Real finish is ctio_m1's finish */
3139 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003140 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003141 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003142
3143 /* qlt_24xx_init_ctio_to_isp will correct
3144 * all neccessary fields that's part of CTIO7.
3145 * There should be no residual of CTIO-CRC2 data.
3146 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003147 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3148 &prm);
3149 pr_debug("Status CTIO7: %p\n", ctio);
3150 }
3151 } else
3152 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3153
3154
3155 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003156 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003157
Himanshu Madhani63163e02014-09-25 06:14:59 -04003158 /* Memory Barrier */
3159 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003160 qla2x00_start_iocbs(vha, vha->req);
3161 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3162
3163 return 0;
3164
3165out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003166 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003167 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3168
3169 return res;
3170}
3171EXPORT_SYMBOL(qlt_xmit_response);
3172
3173int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3174{
3175 struct ctio7_to_24xx *pkt;
3176 struct scsi_qla_host *vha = cmd->vha;
3177 struct qla_hw_data *ha = vha->hw;
3178 struct qla_tgt *tgt = cmd->tgt;
3179 struct qla_tgt_prm prm;
3180 unsigned long flags;
3181 int res = 0;
3182
3183 memset(&prm, 0, sizeof(prm));
3184 prm.cmd = cmd;
3185 prm.tgt = tgt;
3186 prm.sg = NULL;
3187 prm.req_cnt = 1;
3188
3189 /* Send marker if required */
3190 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
3191 return -EIO;
3192
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003193 /* Calculate number of entries and segments required */
3194 if (qlt_pci_map_calc_cnt(&prm) != 0)
3195 return -EAGAIN;
3196
3197 spin_lock_irqsave(&ha->hardware_lock, flags);
3198
Quinn Tranec7193e2017-03-15 09:48:55 -07003199 if (!ha->flags.fw_started || (cmd->reset_count != ha->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003200 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003201 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003202 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003203 * previous life, just abort the processing.
3204 */
3205 cmd->state = QLA_TGT_STATE_NEED_DATA;
3206 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3207 ql_dbg(ql_dbg_async, vha, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003208 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3209 vha->flags.online, qla2x00_reset_active(vha),
3210 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003211 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3212 return 0;
3213 }
3214
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003215 /* Does F/W have an IOCBs for this request */
3216 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
3217 if (res != 0)
3218 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003219 if (cmd->se_cmd.prot_op)
3220 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3221 else
3222 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003223
Quinn Tran810e30b2015-06-10 11:05:20 -04003224 if (unlikely(res != 0)) {
3225 vha->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003226 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003227 }
3228
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003229 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003230 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003231 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003232
3233 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3234 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003235
3236 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003237 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003238
Himanshu Madhani63163e02014-09-25 06:14:59 -04003239 /* Memory Barrier */
3240 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003241 qla2x00_start_iocbs(vha, vha->req);
3242 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3243
3244 return res;
3245
3246out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003247 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003248 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3249
3250 return res;
3251}
3252EXPORT_SYMBOL(qlt_rdy_to_xfer);
3253
Quinn Tranf83adb62014-04-11 16:54:43 -04003254
3255/*
Quinn Tranbe251522017-03-15 09:48:49 -07003256 * it is assumed either hardware_lock or qpair lock is held.
Quinn Tranf83adb62014-04-11 16:54:43 -04003257 */
Quinn Tranbe251522017-03-15 09:48:49 -07003258static void
Quinn Tranf83adb62014-04-11 16:54:43 -04003259qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
Quinn Tranbe251522017-03-15 09:48:49 -07003260 struct ctio_crc_from_fw *sts)
Quinn Tranf83adb62014-04-11 16:54:43 -04003261{
3262 uint8_t *ap = &sts->actual_dif[0];
3263 uint8_t *ep = &sts->expected_dif[0];
Quinn Tranf83adb62014-04-11 16:54:43 -04003264 uint64_t lba = cmd->se_cmd.t_task_lba;
Quinn Tranbe251522017-03-15 09:48:49 -07003265 uint8_t scsi_status, sense_key, asc, ascq;
3266 unsigned long flags;
Quinn Tranf83adb62014-04-11 16:54:43 -04003267
Quinn Tranbe251522017-03-15 09:48:49 -07003268 cmd->trc_flags |= TRC_DIF_ERR;
Quinn Tranf83adb62014-04-11 16:54:43 -04003269
Quinn Tranbe251522017-03-15 09:48:49 -07003270 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3271 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3272 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003273
Quinn Tranbe251522017-03-15 09:48:49 -07003274 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3275 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3276 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
Quinn Tranf83adb62014-04-11 16:54:43 -04003277
Quinn Tranbe251522017-03-15 09:48:49 -07003278 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3279 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
Quinn Tranf83adb62014-04-11 16:54:43 -04003280
Quinn Tranbe251522017-03-15 09:48:49 -07003281 scsi_status = sense_key = asc = ascq = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04003282
Quinn Tranbe251522017-03-15 09:48:49 -07003283 /* check appl tag */
3284 if (cmd->e_app_tag != cmd->a_app_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003285 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3286 "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]",
3287 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3288 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3289 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3290 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003291
Quinn Tranbe251522017-03-15 09:48:49 -07003292 cmd->dif_err_code = DIF_ERR_APP;
3293 scsi_status = SAM_STAT_CHECK_CONDITION;
3294 sense_key = ABORTED_COMMAND;
3295 asc = 0x10;
3296 ascq = 0x2;
Quinn Tranf83adb62014-04-11 16:54:43 -04003297 }
3298
3299 /* check ref tag */
Quinn Tranbe251522017-03-15 09:48:49 -07003300 if (cmd->e_ref_tag != cmd->a_ref_tag) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003301 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3302 "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] ",
3303 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3304 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3305 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3306 cmd->atio.u.isp24.fcp_hdr.ox_id);
Quinn Tranf83adb62014-04-11 16:54:43 -04003307
Quinn Tranbe251522017-03-15 09:48:49 -07003308 cmd->dif_err_code = DIF_ERR_REF;
3309 scsi_status = SAM_STAT_CHECK_CONDITION;
3310 sense_key = ABORTED_COMMAND;
3311 asc = 0x10;
3312 ascq = 0x3;
Quinn Tranf83adb62014-04-11 16:54:43 -04003313 goto out;
3314 }
3315
Quinn Tranbe251522017-03-15 09:48:49 -07003316 /* check guard */
3317 if (cmd->e_guard != cmd->a_guard) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003318 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3319 "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]",
3320 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3321 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3322 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3323 cmd->atio.u.isp24.fcp_hdr.ox_id);
3324
Quinn Tranbe251522017-03-15 09:48:49 -07003325 cmd->dif_err_code = DIF_ERR_GRD;
3326 scsi_status = SAM_STAT_CHECK_CONDITION;
3327 sense_key = ABORTED_COMMAND;
3328 asc = 0x10;
3329 ascq = 0x1;
Quinn Tranf83adb62014-04-11 16:54:43 -04003330 }
3331out:
Quinn Tranbe251522017-03-15 09:48:49 -07003332 switch (cmd->state) {
3333 case QLA_TGT_STATE_NEED_DATA:
3334 /* handle_data will load DIF error code */
3335 cmd->state = QLA_TGT_STATE_DATA_IN;
3336 vha->hw->tgt.tgt_ops->handle_data(cmd);
3337 break;
3338 default:
3339 spin_lock_irqsave(&cmd->cmd_lock, flags);
3340 if (cmd->aborted) {
3341 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3342 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3343 break;
3344 }
3345 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Quinn Tranf83adb62014-04-11 16:54:43 -04003346
Quinn Tranbe251522017-03-15 09:48:49 -07003347 qlt_send_resp_ctio(vha, cmd, scsi_status, sense_key, asc, ascq);
3348 /* assume scsi status gets out on the wire.
3349 * Will not wait for completion.
3350 */
3351 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3352 break;
3353 }
3354}
Quinn Tranf83adb62014-04-11 16:54:43 -04003355
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003356/* If hardware_lock held on entry, might drop it, then reaquire */
3357/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003358static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3359 struct imm_ntfy_from_isp *ntfy)
3360{
3361 struct nack_to_isp *nack;
3362 struct qla_hw_data *ha = vha->hw;
3363 request_t *pkt;
3364 int ret = 0;
3365
3366 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3367 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3368
Quinn Tranec7193e2017-03-15 09:48:55 -07003369 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003370 if (pkt == NULL) {
3371 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3372 "qla_target(%d): %s failed: unable to allocate "
3373 "request packet\n", vha->vp_idx, __func__);
3374 return -ENOMEM;
3375 }
3376
3377 pkt->entry_type = NOTIFY_ACK_TYPE;
3378 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003379 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003380
3381 nack = (struct nack_to_isp *)pkt;
3382 nack->ox_id = ntfy->ox_id;
3383
3384 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3385 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3386 nack->u.isp24.flags = ntfy->u.isp24.flags &
3387 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3388 }
3389
3390 /* terminate */
3391 nack->u.isp24.flags |=
3392 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3393
3394 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3395 nack->u.isp24.status = ntfy->u.isp24.status;
3396 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3397 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3398 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3399 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3400 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3401 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3402
3403 qla2x00_start_iocbs(vha, vha->req);
3404 return ret;
3405}
3406
3407static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3408 struct imm_ntfy_from_isp *imm, int ha_locked)
3409{
3410 unsigned long flags = 0;
3411 int rc;
3412
3413 if (qlt_issue_marker(vha, ha_locked) < 0)
3414 return;
3415
3416 if (ha_locked) {
3417 rc = __qlt_send_term_imm_notif(vha, imm);
3418
3419#if 0 /* Todo */
3420 if (rc == -ENOMEM)
3421 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003422#else
3423 if (rc) {
3424 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003425#endif
3426 goto done;
3427 }
3428
3429 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3430 rc = __qlt_send_term_imm_notif(vha, imm);
3431
3432#if 0 /* Todo */
3433 if (rc == -ENOMEM)
3434 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3435#endif
3436
3437done:
3438 if (!ha_locked)
3439 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3440}
3441
Quinn Tran83548fe2017-06-02 09:12:01 -07003442/*
3443 * If hardware_lock held on entry, might drop it, then reaquire
3444 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3445 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003446static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3447 struct qla_tgt_cmd *cmd,
3448 struct atio_from_isp *atio)
3449{
3450 struct ctio7_to_24xx *ctio24;
3451 struct qla_hw_data *ha = vha->hw;
3452 request_t *pkt;
3453 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003454 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003455
Quinn Tran83548fe2017-06-02 09:12:01 -07003456 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003457
Arun Easib6a029e2014-09-25 06:14:52 -04003458 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003459 if (pkt == NULL) {
3460 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3461 "qla_target(%d): %s failed: unable to allocate "
3462 "request packet\n", vha->vp_idx, __func__);
3463 return -ENOMEM;
3464 }
3465
3466 if (cmd != NULL) {
3467 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3468 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3469 "qla_target(%d): Terminating cmd %p with "
3470 "incorrect state %d\n", vha->vp_idx, cmd,
3471 cmd->state);
3472 } else
3473 ret = 1;
3474 }
3475
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003476 vha->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003477 pkt->entry_count = 1;
3478 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3479
3480 ctio24 = (struct ctio7_to_24xx *)pkt;
3481 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003482 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003483 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003484 ctio24->vp_index = vha->vp_idx;
3485 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3486 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3487 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3488 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003489 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3490 CTIO7_FLAGS_TERMINATE;
3491 ctio24->u.status1.flags = cpu_to_le16(temp);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003492 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3493 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003494
3495 /* Most likely, it isn't needed */
3496 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3497 &atio->u.isp24.fcp_cmnd.add_cdb[
3498 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3499 if (ctio24->u.status1.residual != 0)
3500 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3501
Himanshu Madhani63163e02014-09-25 06:14:59 -04003502 /* Memory Barrier */
3503 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003504 qla2x00_start_iocbs(vha, vha->req);
3505 return ret;
3506}
3507
3508static void qlt_send_term_exchange(struct scsi_qla_host *vha,
Quinn Trana07100e2015-12-07 19:48:57 -05003509 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3510 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003511{
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003512 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003513 int rc;
3514
3515 if (qlt_issue_marker(vha, ha_locked) < 0)
3516 return;
3517
3518 if (ha_locked) {
3519 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003520 if (rc == -ENOMEM)
3521 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003522 goto done;
3523 }
3524 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3525 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003526 if (rc == -ENOMEM)
3527 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003528
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003529done:
Quinn Trana07100e2015-12-07 19:48:57 -05003530 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003531 if (cmd->sg_mapped)
3532 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003533 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3534 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003535
3536 if (!ha_locked)
3537 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3538
Quinn Tran7b898542014-04-11 16:54:44 -04003539 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003540}
3541
Quinn Tran33e79972014-09-25 06:14:55 -04003542static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3543{
3544 struct list_head free_list;
3545 struct qla_tgt_cmd *cmd, *tcmd;
3546
3547 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003548 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003549
3550 cmd = tcmd = NULL;
3551 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3552 INIT_LIST_HEAD(&free_list);
3553 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3554
3555 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3556 list_del(&cmd->cmd_list);
3557 /* This cmd was never sent to TCM. There is no need
3558 * to schedule free or call free_cmd
3559 */
3560 qlt_free_cmd(cmd);
3561 vha->hw->tgt.num_qfull_cmds_alloc--;
3562 }
3563 }
3564 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3565}
3566
3567static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3568{
3569 uint32_t total_leaked;
3570
3571 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3572
3573 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3574 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3575
3576 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3577 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003578 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003579
3580 if (IS_P3P_TYPE(vha->hw))
3581 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3582 else
3583 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3584 qla2xxx_wake_dpc(vha);
3585 }
3586
3587}
3588
Quinn Trana07100e2015-12-07 19:48:57 -05003589int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003590{
3591 struct qla_tgt *tgt = cmd->tgt;
3592 struct scsi_qla_host *vha = tgt->vha;
3593 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003594 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003595
3596 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3597 "qla_target(%d): terminating exchange for aborted cmd=%p "
3598 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3599 se_cmd->tag);
3600
Quinn Trana07100e2015-12-07 19:48:57 -05003601 spin_lock_irqsave(&cmd->cmd_lock, flags);
3602 if (cmd->aborted) {
3603 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3604 /*
3605 * It's normal to see 2 calls in this path:
3606 * 1) XFER Rdy completion + CMD_T_ABORT
3607 * 2) TCM TMR - drain_state_list
3608 */
Quinn Tran83548fe2017-06-02 09:12:01 -07003609 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3610 "multiple abort. %p transport_state %x, t_state %x, "
3611 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3612 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003613 return EIO;
3614 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003615 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003616 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003617 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003618
Quinn Trana07100e2015-12-07 19:48:57 -05003619 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3620 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003621}
3622EXPORT_SYMBOL(qlt_abort_cmd);
3623
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003624void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3625{
Quinn Tran5d964832017-01-19 22:27:59 -08003626 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003627
Quinn Tranf83adb62014-04-11 16:54:43 -04003628 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3629 "%s: se_cmd[%p] ox_id %04x\n",
3630 __func__, &cmd->se_cmd,
3631 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003632
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003633 BUG_ON(cmd->cmd_in_wq);
3634
Quinn Trana07100e2015-12-07 19:48:57 -05003635 if (cmd->sg_mapped)
3636 qlt_unmap_sg(cmd->vha, cmd);
3637
Quinn Tran33e79972014-09-25 06:14:55 -04003638 if (!cmd->q_full)
3639 qlt_decr_num_pend_cmds(cmd->vha);
3640
Quinn Tranf83adb62014-04-11 16:54:43 -04003641 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003642 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003643 if (unlikely(cmd->free_sg))
3644 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003645
3646 if (!sess || !sess->se_sess) {
3647 WARN_ON(1);
3648 return;
3649 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003650 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003651 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003652}
3653EXPORT_SYMBOL(qlt_free_cmd);
3654
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003655/*
3656 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3657 */
3658static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3659 struct qla_tgt_cmd *cmd, uint32_t status)
3660{
3661 int term = 0;
3662
Quinn Tranbe251522017-03-15 09:48:49 -07003663 if (cmd->se_cmd.prot_op)
Quinn Tran83548fe2017-06-02 09:12:01 -07003664 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
Quinn Tranbe251522017-03-15 09:48:49 -07003665 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3666 "se_cmd=%p tag[%x] op %#x/%s",
3667 cmd->lba, cmd->lba,
3668 cmd->num_blks, &cmd->se_cmd,
3669 cmd->atio.u.isp24.exchange_addr,
3670 cmd->se_cmd.prot_op,
3671 prot_op_str(cmd->se_cmd.prot_op));
3672
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003673 if (ctio != NULL) {
3674 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3675 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003676 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003677 } else
3678 term = 1;
3679
3680 if (term)
Quinn Trana07100e2015-12-07 19:48:57 -05003681 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003682
3683 return term;
3684}
3685
3686/* ha->hardware_lock supposed to be held on entry */
3687static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3688 uint32_t handle)
3689{
3690 struct qla_hw_data *ha = vha->hw;
3691
3692 handle--;
3693 if (ha->tgt.cmds[handle] != NULL) {
3694 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3695 ha->tgt.cmds[handle] = NULL;
3696 return cmd;
3697 } else
3698 return NULL;
3699}
3700
3701/* ha->hardware_lock supposed to be held on entry */
3702static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3703 uint32_t handle, void *ctio)
3704{
3705 struct qla_tgt_cmd *cmd = NULL;
3706
3707 /* Clear out internal marks */
3708 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3709 CTIO_INTERMEDIATE_HANDLE_MARK);
3710
3711 if (handle != QLA_TGT_NULL_HANDLE) {
Arun Easi667024a2014-09-25 06:14:47 -04003712 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003713 return NULL;
Arun Easi667024a2014-09-25 06:14:47 -04003714
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003715 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05003716 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003717 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3718 "qla_target(%d): Wrong handle %x received\n",
3719 vha->vp_idx, handle);
3720 return NULL;
3721 }
3722 cmd = qlt_get_cmd(vha, handle);
3723 if (unlikely(cmd == NULL)) {
3724 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3725 "qla_target(%d): Suspicious: unable to "
3726 "find the command with handle %x\n", vha->vp_idx,
3727 handle);
3728 return NULL;
3729 }
3730 } else if (ctio != NULL) {
3731 /* We can't get loop ID from CTIO7 */
3732 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3733 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3734 "support NULL handles\n", vha->vp_idx);
3735 return NULL;
3736 }
3737
3738 return cmd;
3739}
3740
Arun Easic0cb4492014-09-25 06:14:51 -04003741/* hardware_lock should be held by caller. */
3742static void
3743qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3744{
3745 struct qla_hw_data *ha = vha->hw;
3746 uint32_t handle;
3747
3748 if (cmd->sg_mapped)
3749 qlt_unmap_sg(vha, cmd);
3750
3751 handle = qlt_make_handle(vha);
3752
3753 /* TODO: fix debug message type and ids. */
3754 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3755 ql_dbg(ql_dbg_io, vha, 0xff00,
3756 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3757 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3758 cmd->write_data_transferred = 0;
3759 cmd->state = QLA_TGT_STATE_DATA_IN;
3760
3761 ql_dbg(ql_dbg_io, vha, 0xff01,
3762 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3763
3764 ha->tgt.tgt_ops->handle_data(cmd);
3765 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003766 } else {
3767 ql_dbg(ql_dbg_io, vha, 0xff03,
3768 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3769 cmd->state);
3770 dump_stack();
3771 }
3772
Quinn Tran1eb42f92017-01-19 22:27:55 -08003773 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003774 ha->tgt.tgt_ops->free_cmd(cmd);
3775}
3776
3777void
3778qlt_host_reset_handler(struct qla_hw_data *ha)
3779{
3780 struct qla_tgt_cmd *cmd;
3781 unsigned long flags;
3782 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3783 scsi_qla_host_t *vha = NULL;
3784 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3785 uint32_t i;
3786
3787 if (!base_vha->hw->tgt.tgt_ops)
3788 return;
3789
3790 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3791 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3792 "Target mode disabled\n");
3793 return;
3794 }
3795
3796 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3797 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3798 base_vha->dpc_flags);
3799
3800 spin_lock_irqsave(&ha->hardware_lock, flags);
3801 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3802 cmd = qlt_get_cmd(base_vha, i);
3803 if (!cmd)
3804 continue;
3805 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3806 vha = cmd->vha;
3807 qlt_abort_cmd_on_host_reset(vha, cmd);
3808 }
3809 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3810}
3811
3812
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003813/*
3814 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3815 */
3816static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3817 uint32_t status, void *ctio)
3818{
3819 struct qla_hw_data *ha = vha->hw;
3820 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003821 struct qla_tgt_cmd *cmd;
3822
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003823 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3824 /* That could happen only in case of an error/reset/abort */
3825 if (status != CTIO_SUCCESS) {
3826 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3827 "Intermediate CTIO received"
3828 " (status %x)\n", status);
3829 }
3830 return;
3831 }
3832
3833 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003834 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003835 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003836
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003837 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003838 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003839
Joern Engelf9b67212014-09-16 16:23:18 -04003840 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003841
3842 if (unlikely(status != CTIO_SUCCESS)) {
3843 switch (status & 0xFFFF) {
3844 case CTIO_LIP_RESET:
3845 case CTIO_TARGET_RESET:
3846 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003847 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003848 case CTIO_TIMEOUT:
3849 case CTIO_INVALID_RX_ID:
3850 /* They are OK */
3851 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3852 "qla_target(%d): CTIO with "
3853 "status %#x received, state %x, se_cmd %p, "
3854 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3855 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3856 status, cmd->state, se_cmd);
3857 break;
3858
3859 case CTIO_PORT_LOGGED_OUT:
3860 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003861 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003862 int logged_out =
3863 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3864
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003865 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003866 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003867 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003868 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003869 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003870
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003871 if (logged_out && cmd->sess) {
3872 /*
3873 * Session is already logged out, but we need
3874 * to notify initiator, who's not aware of this
3875 */
3876 cmd->sess->logout_on_delete = 0;
3877 cmd->sess->send_els_logo = 1;
Quinn Tran83548fe2017-06-02 09:12:01 -07003878 ql_dbg(ql_dbg_disc, vha, 0x20f8,
Quinn Tran726b8542017-01-19 22:28:00 -08003879 "%s %d %8phC post del sess\n",
3880 __func__, __LINE__, cmd->sess->port_name);
3881
3882 qlt_schedule_sess_for_deletion_lock(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003883 }
3884 break;
3885 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003886 case CTIO_DIF_ERROR: {
3887 struct ctio_crc_from_fw *crc =
3888 (struct ctio_crc_from_fw *)ctio;
3889 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
Quinn Tranbe251522017-03-15 09:48:49 -07003890 "qla_target(%d): CTIO with DIF_ERROR status %x "
3891 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
3892 "expect_dif[0x%llx]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003893 vha->vp_idx, status, cmd->state, se_cmd,
3894 *((u64 *)&crc->actual_dif[0]),
3895 *((u64 *)&crc->expected_dif[0]));
3896
Quinn Tranbe251522017-03-15 09:48:49 -07003897 qlt_handle_dif_error(vha, cmd, ctio);
3898 return;
Quinn Tranf83adb62014-04-11 16:54:43 -04003899 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003900 default:
3901 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003902 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003903 vha->vp_idx, status, cmd->state, se_cmd);
3904 break;
3905 }
3906
Quinn Tran7b898542014-04-11 16:54:44 -04003907
Quinn Tran193b50b2015-12-17 14:57:03 -05003908 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003909 * cmd is already aborted/terminated, we don't
3910 * need to terminate again. The exchange is already
3911 * cleaned up/freed at FW level. Just cleanup at driver
3912 * level.
3913 */
3914 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003915 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003916 cmd->trc_flags |= TRC_CTIO_ERR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003917 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3918 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003919 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003920 }
3921
3922 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003923 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003924 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003925 cmd->state = QLA_TGT_STATE_DATA_IN;
3926
Bart Van Assche52c82822015-07-09 07:23:26 -07003927 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003928 cmd->write_data_transferred = 1;
3929
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003930 ha->tgt.tgt_ops->handle_data(cmd);
3931 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003932 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003933 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003934 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003935 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003936 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003937 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3939 "qla_target(%d): A command in state (%d) should "
3940 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3941 }
3942
Quinn Tran7b898542014-04-11 16:54:44 -04003943 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003944 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3946 dump_stack();
3947 }
3948
3949 ha->tgt.tgt_ops->free_cmd(cmd);
3950}
3951
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003952static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3953 uint8_t task_codes)
3954{
3955 int fcp_task_attr;
3956
3957 switch (task_codes) {
3958 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003959 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003960 break;
3961 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003962 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003963 break;
3964 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003965 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003966 break;
3967 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003968 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003969 break;
3970 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003971 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003972 break;
3973 default:
3974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3975 "qla_target: unknown task code %x, use ORDERED instead\n",
3976 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003977 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003978 break;
3979 }
3980
3981 return fcp_task_attr;
3982}
3983
Quinn Tran5d964832017-01-19 22:27:59 -08003984static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003985 uint8_t *);
3986/*
3987 * Process context for I/O path into tcm_qla2xxx code
3988 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003989static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003990{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003991 scsi_qla_host_t *vha = cmd->vha;
3992 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003993 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08003994 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003995 struct atio_from_isp *atio = &cmd->atio;
3996 unsigned char *cdb;
3997 unsigned long flags;
3998 uint32_t data_length;
3999 int ret, fcp_task_attr, data_dir, bidi = 0;
4000
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004001 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004002 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004003 if (tgt->tgt_stop)
4004 goto out_term;
4005
Quinn Tran193b50b2015-12-17 14:57:03 -05004006 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004007 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4008 "cmd with tag %u is aborted\n",
4009 cmd->atio.u.isp24.exchange_addr);
4010 goto out_term;
4011 }
4012
Quinn Trana07100e2015-12-07 19:48:57 -05004013 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004014 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02004015 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004016 cmd->unpacked_lun = scsilun_to_int(
4017 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4018
4019 if (atio->u.isp24.fcp_cmnd.rddata &&
4020 atio->u.isp24.fcp_cmnd.wrdata) {
4021 bidi = 1;
4022 data_dir = DMA_TO_DEVICE;
4023 } else if (atio->u.isp24.fcp_cmnd.rddata)
4024 data_dir = DMA_FROM_DEVICE;
4025 else if (atio->u.isp24.fcp_cmnd.wrdata)
4026 data_dir = DMA_TO_DEVICE;
4027 else
4028 data_dir = DMA_NONE;
4029
4030 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4031 atio->u.isp24.fcp_cmnd.task_attr);
4032 data_length = be32_to_cpu(get_unaligned((uint32_t *)
4033 &atio->u.isp24.fcp_cmnd.add_cdb[
4034 atio->u.isp24.fcp_cmnd.add_cdb_len]));
4035
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004036 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4037 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004038 if (ret != 0)
4039 goto out_term;
4040 /*
4041 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4042 */
Quinn Tran75601512015-12-17 14:57:04 -05004043 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004044 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004045 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004046 return;
4047
4048out_term:
Arun Easi667024a2014-09-25 06:14:47 -04004049 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004050 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07004051 * cmd has not sent to target yet, so pass NULL as the second
4052 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004053 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08004054 cmd->trc_flags |= TRC_DO_WORK_ERR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004055 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05004056 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004057
4058 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004059 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Jörn Engel08234e32013-06-12 16:27:54 -04004060 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tran75601512015-12-17 14:57:04 -05004061
4062 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08004063 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05004064 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004065}
4066
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004067static void qlt_do_work(struct work_struct *work)
4068{
4069 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004070 scsi_qla_host_t *vha = cmd->vha;
4071 unsigned long flags;
4072
4073 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4074 list_del(&cmd->cmd_list);
4075 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004076
4077 __qlt_do_work(cmd);
4078}
4079
4080static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004081 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004082 struct atio_from_isp *atio)
4083{
4084 struct se_session *se_sess = sess->se_sess;
4085 struct qla_tgt_cmd *cmd;
4086 int tag;
4087
4088 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4089 if (tag < 0)
4090 return NULL;
4091
4092 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4093 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4094
4095 memcpy(&cmd->atio, atio, sizeof(*atio));
4096 cmd->state = QLA_TGT_STATE_NEW;
4097 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04004098 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004099 cmd->vha = vha;
4100 cmd->se_cmd.map_tag = tag;
4101 cmd->sess = sess;
4102 cmd->loop_id = sess->loop_id;
4103 cmd->conf_compl_supported = sess->conf_compl_supported;
4104
Quinn Tran1eb42f92017-01-19 22:27:55 -08004105 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004106 cmd->jiffies_at_alloc = get_jiffies_64();
4107
4108 cmd->reset_count = vha->hw->chip_reset;
4109
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004110 return cmd;
4111}
4112
4113static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
4114 uint16_t);
4115
4116static void qlt_create_sess_from_atio(struct work_struct *work)
4117{
4118 struct qla_tgt_sess_op *op = container_of(work,
4119 struct qla_tgt_sess_op, work);
4120 scsi_qla_host_t *vha = op->vha;
4121 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004122 struct fc_port *sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004123 struct qla_tgt_cmd *cmd;
4124 unsigned long flags;
4125 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4126
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004127 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4128 list_del(&op->cmd_list);
4129 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4130
4131 if (op->aborted) {
4132 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4133 "sess_op with tag %u is aborted\n",
4134 op->atio.u.isp24.exchange_addr);
4135 goto out_term;
4136 }
4137
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004138 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004139 "qla_target(%d): Unable to find wwn login"
4140 " (s_id %x:%x:%x), trying to create it manually\n",
4141 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004142
4143 if (op->atio.u.raw.entry_count > 1) {
4144 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004145 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004146 goto out_term;
4147 }
4148
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004149 sess = qlt_make_local_sess(vha, s_id);
4150 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004151
4152 if (!sess)
4153 goto out_term;
4154 /*
4155 * Now obtain a pre-allocated session tag using the original op->atio
4156 * packet header, and dispatch into __qlt_do_work() using the existing
4157 * process context.
4158 */
4159 cmd = qlt_get_tag(vha, sess, &op->atio);
4160 if (!cmd) {
4161 spin_lock_irqsave(&ha->hardware_lock, flags);
4162 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
Quinn Tran5d964832017-01-19 22:27:59 -08004163 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004164 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4165 kfree(op);
4166 return;
4167 }
Quinn Tran726b8542017-01-19 22:28:00 -08004168
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004169 /*
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004170 * __qlt_do_work() will call qlt_put_sess() to release
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004171 * the extra reference taken above by qlt_make_local_sess()
4172 */
4173 __qlt_do_work(cmd);
4174 kfree(op);
4175 return;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004176out_term:
4177 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05004178 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004179 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4180 kfree(op);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004181}
4182
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004183/* ha->hardware_lock supposed to be held on entry */
4184static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4185 struct atio_from_isp *atio)
4186{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004187 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004188 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004189 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004190 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004191 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004192
4193 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004194 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004195 "New command while device %p is shutting down\n", tgt);
4196 return -EFAULT;
4197 }
4198
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004199 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4200 if (unlikely(!sess)) {
4201 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4202 GFP_ATOMIC);
4203 if (!op)
4204 return -ENOMEM;
4205
4206 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04004207 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004208
Quinn Tran8b631d82017-06-02 09:11:54 -07004209 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004210 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
Quinn Tran8b631d82017-06-02 09:11:54 -07004211 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004212
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004213 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4214 queue_work(qla_tgt_wq, &op->work);
4215 return 0;
4216 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004217
4218 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4219 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004220 if (sess->deleted) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004221 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004222 "New command while old session %p is being deleted\n",
4223 sess);
4224 return -EFAULT;
4225 }
4226
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004227 /*
4228 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4229 */
Quinn Tran726b8542017-01-19 22:28:00 -08004230 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004231 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
Quinn Tran726b8542017-01-19 22:28:00 -08004232 "%s: kref_get fail, %8phC oxid %x \n",
4233 __func__, sess->port_name,
4234 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4235 return -EFAULT;
4236 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004237
4238 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004239 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004240 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004241 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004242 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4243 ha->tgt.tgt_ops->put_sess(sess);
4244 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004245 return -ENOMEM;
4246 }
4247
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004248 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004249 cmd->trc_flags |= TRC_NEW_CMD;
Quinn Tran5327c7d2016-02-10 18:59:14 -05004250 cmd->se_cmd.cpuid = ha->msix_count ?
4251 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004252
Quinn Tran726b8542017-01-19 22:28:00 -08004253 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004254 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004255 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004256
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004257 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Tranfb3269b2015-12-17 14:57:06 -05004258 if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004259 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4260 queue_work_on(smp_processor_id(), qla_tgt_wq,
4261 &cmd->work);
4262 else
4263 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4264 &cmd->work);
4265 } else {
4266 queue_work(qla_tgt_wq, &cmd->work);
4267 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004268 return 0;
4269
4270}
4271
4272/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004273static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004274 int fn, void *iocb, int flags)
4275{
4276 struct scsi_qla_host *vha = sess->vha;
4277 struct qla_hw_data *ha = vha->hw;
4278 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004279 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004280 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004281
4282 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4283 if (!mcmd) {
4284 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4285 "qla_target(%d): Allocation of management "
4286 "command failed, some commands and their data could "
4287 "leak\n", vha->vp_idx);
4288 return -ENOMEM;
4289 }
4290 memset(mcmd, 0, sizeof(*mcmd));
4291 mcmd->sess = sess;
4292
4293 if (iocb) {
4294 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4295 sizeof(mcmd->orig_iocb.imm_ntfy));
4296 }
4297 mcmd->tmr_func = fn;
4298 mcmd->flags = flags;
Arun Easib6a029e2014-09-25 06:14:52 -04004299 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004300
4301 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004302 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004303 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4304 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004305 }
4306
Quinn Tranbe92fc32017-01-19 22:27:54 -08004307 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004308 if (res != 0) {
4309 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4310 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4311 sess->vha->vp_idx, res);
4312 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4313 return -EFAULT;
4314 }
4315
4316 return 0;
4317}
4318
4319/* ha->hardware_lock supposed to be held on entry */
4320static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4321{
4322 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4323 struct qla_hw_data *ha = vha->hw;
4324 struct qla_tgt *tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004325 struct fc_port *sess;
Quinn Tranf775bd12017-06-02 09:11:59 -07004326 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004327 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004328 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004329
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004330 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004331
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004332 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004333
4334 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004335 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4336 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004337 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4338
Quinn Tranf775bd12017-06-02 09:11:59 -07004339 unpacked_lun =
4340 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004341
4342 if (!sess) {
4343 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4344 "qla_target(%d): task mgmt fn 0x%x for "
4345 "non-existant session\n", vha->vp_idx, fn);
4346 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4347 sizeof(struct atio_from_isp));
4348 }
4349
Quinn Tran726b8542017-01-19 22:28:00 -08004350 if (sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004351 return -EFAULT;
4352
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004353 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4354}
4355
4356/* ha->hardware_lock supposed to be held on entry */
4357static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004358 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004359{
4360 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4361 struct qla_hw_data *ha = vha->hw;
4362 struct qla_tgt_mgmt_cmd *mcmd;
Quinn Tranf775bd12017-06-02 09:11:59 -07004363 u64 unpacked_lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004364 int rc;
4365
4366 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4367 if (mcmd == NULL) {
4368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4369 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4370 vha->vp_idx, __func__);
4371 return -ENOMEM;
4372 }
4373 memset(mcmd, 0, sizeof(*mcmd));
4374
4375 mcmd->sess = sess;
4376 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4377 sizeof(mcmd->orig_iocb.imm_ntfy));
4378
Quinn Tranf775bd12017-06-02 09:11:59 -07004379 unpacked_lun =
4380 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Arun Easi80187f82014-09-25 06:14:53 -04004381 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004382 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004383
Quinn Tranbe92fc32017-01-19 22:27:54 -08004384 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004385 le16_to_cpu(iocb->u.isp2x.seq_id));
4386 if (rc != 0) {
4387 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4388 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4389 vha->vp_idx, rc);
4390 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4391 return -EFAULT;
4392 }
4393
4394 return 0;
4395}
4396
4397/* ha->hardware_lock supposed to be held on entry */
4398static int qlt_abort_task(struct scsi_qla_host *vha,
4399 struct imm_ntfy_from_isp *iocb)
4400{
4401 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004402 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004403 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004404 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004405
4406 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4407
Quinn Tran75601512015-12-17 14:57:04 -05004408 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004409 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004410 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4411
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004412 if (sess == NULL) {
4413 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4414 "qla_target(%d): task abort for unexisting "
4415 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004416 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004417 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4418 }
4419
4420 return __qlt_abort_task(vha, iocb, sess);
4421}
4422
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004423void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4424{
Quinn Tran726b8542017-01-19 22:28:00 -08004425 if (rc != MBS_COMMAND_COMPLETE) {
4426 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4427 "%s: se_sess %p / sess %p from"
4428 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4429 " LOGO failed: %#x\n",
4430 __func__,
4431 fcport->se_sess,
4432 fcport,
4433 fcport->port_name, fcport->loop_id,
4434 fcport->d_id.b.domain, fcport->d_id.b.area,
4435 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004436 }
Quinn Tran726b8542017-01-19 22:28:00 -08004437
4438 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004439}
4440
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004441/*
4442* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4443*
4444* Schedules sessions with matching port_id/loop_id but different wwn for
4445* deletion. Returns existing session with matching wwn if present.
4446* Null otherwise.
4447*/
Quinn Tran726b8542017-01-19 22:28:00 -08004448struct fc_port *
4449qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004450 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004451{
Quinn Tran5d964832017-01-19 22:27:59 -08004452 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004453 uint64_t other_wwn;
4454
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004455 *conflict_sess = NULL;
4456
Quinn Tran5d964832017-01-19 22:27:59 -08004457 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004458
4459 other_wwn = wwn_to_u64(other_sess->port_name);
4460
4461 if (wwn == other_wwn) {
4462 WARN_ON(sess);
4463 sess = other_sess;
4464 continue;
4465 }
4466
4467 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004468 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004469 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004470 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004471 "Invalidating sess %p loop_id %d wwn %llx.\n",
4472 other_sess, other_sess->loop_id, other_wwn);
4473
4474 /*
4475 * logout_on_delete is set by default, but another
4476 * session that has the same s_id/loop_id combo
4477 * might have cleared it when requested this session
4478 * deletion, so don't touch it
4479 */
4480 qlt_schedule_sess_for_deletion(other_sess, true);
4481 } else {
4482 /*
4483 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004484 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004485 */
Quinn Tran83548fe2017-06-02 09:12:01 -07004486 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
Quinn Tran726b8542017-01-19 22:28:00 -08004487 "Invalidating sess %p loop_id %d wwn %llx.\n",
4488 other_sess, other_sess->loop_id, other_wwn);
4489
4490
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004491 other_sess->keep_nport_handle = 1;
4492 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004493 qlt_schedule_sess_for_deletion(other_sess,
4494 true);
4495 }
4496 continue;
4497 }
4498
4499 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004500 if ((loop_id == other_sess->loop_id) &&
4501 (loop_id != FC_NO_LOOP_ID)) {
4502 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004503 "Invalidating sess %p loop_id %d wwn %llx.\n",
4504 other_sess, other_sess->loop_id, other_wwn);
4505
4506 /* Same loop_id but different s_id
4507 * Ok to kill and logout */
4508 qlt_schedule_sess_for_deletion(other_sess, true);
4509 }
4510 }
4511
4512 return sess;
4513}
4514
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004515/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4516static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4517{
4518 struct qla_tgt_sess_op *op;
4519 struct qla_tgt_cmd *cmd;
4520 uint32_t key;
4521 int count = 0;
Quinn Tran8b631d82017-06-02 09:11:54 -07004522 unsigned long flags;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004523
4524 key = (((u32)s_id->b.domain << 16) |
4525 ((u32)s_id->b.area << 8) |
4526 ((u32)s_id->b.al_pa));
4527
Quinn Tran8b631d82017-06-02 09:11:54 -07004528 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004529 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4530 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004531
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004532 if (op_key == key) {
4533 op->aborted = true;
4534 count++;
4535 }
4536 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004537
4538 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4539 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4540 if (op_key == key) {
4541 op->aborted = true;
4542 count++;
4543 }
4544 }
4545
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004546 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4547 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4548 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004549 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004550 count++;
4551 }
4552 }
Quinn Tran8b631d82017-06-02 09:11:54 -07004553 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004554
4555 return count;
4556}
4557
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004558/*
4559 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4560 */
4561static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4562 struct imm_ntfy_from_isp *iocb)
4563{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004564 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004565 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004566 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004567 uint64_t wwn;
4568 port_id_t port_id;
4569 uint16_t loop_id;
4570 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004571 int res = 0;
Quinn Tran5d964832017-01-19 22:27:59 -08004572 struct qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004573 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004574
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004575 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4576
4577 port_id.b.domain = iocb->u.isp24.port_id[2];
4578 port_id.b.area = iocb->u.isp24.port_id[1];
4579 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4580 port_id.b.rsvd_1 = 0;
4581
4582 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4583
Quinn Tran726b8542017-01-19 22:28:00 -08004584 ql_dbg(ql_dbg_disc, vha, 0xf026,
4585 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4586 vha->vp_idx, iocb->u.isp24.port_id[2],
4587 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4588 iocb->u.isp24.status_subcode, loop_id,
4589 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004590
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004591 /* res = 1 means ack at the end of thread
4592 * res = 0 means ack async/later.
4593 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004594 switch (iocb->u.isp24.status_subcode) {
4595 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004596
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004597 /* Mark all stale commands in qla_tgt_wq for deletion */
4598 abort_cmds_for_s_id(vha, &port_id);
4599
Quinn Tran75601512015-12-17 14:57:04 -05004600 if (wwn) {
4601 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004602 sess = qlt_find_sess_invalidate_other(vha, wwn,
4603 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004604 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4605 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004606
Quinn Tran726b8542017-01-19 22:28:00 -08004607 if (IS_SW_RESV_ADDR(port_id)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004608 res = 1;
4609 break;
4610 }
4611
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004612 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4613 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004614 qlt_send_term_imm_notif(vha, iocb, 1);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004615 break;
4616 }
4617
4618 res = 0;
4619
Quinn Tran726b8542017-01-19 22:28:00 -08004620 if (conflict_sess) {
4621 conflict_sess->login_gen++;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004622 qlt_plogi_ack_link(vha, pla, conflict_sess,
Quinn Tran726b8542017-01-19 22:28:00 -08004623 QLT_PLOGI_LINK_CONFLICT);
4624 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004625
Quinn Tran726b8542017-01-19 22:28:00 -08004626 if (!sess) {
4627 pla->ref_count++;
4628 qla24xx_post_newsess_work(vha, &port_id,
4629 iocb->u.isp24.port_name, pla);
4630 res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004631 break;
Quinn Tran726b8542017-01-19 22:28:00 -08004632 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004633
4634 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Quinn Tran726b8542017-01-19 22:28:00 -08004635 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4636 sess->d_id = port_id;
4637 sess->login_gen++;
4638
4639 switch (sess->disc_state) {
4640 case DSC_DELETED:
4641 qlt_plogi_ack_unref(vha, pla);
4642 break;
4643
4644 default:
4645 /*
4646 * Under normal circumstances we want to release nport handle
4647 * during LOGO process to avoid nport handle leaks inside FW.
4648 * The exception is when LOGO is done while another PLOGI with
4649 * the same nport handle is waiting as might be the case here.
4650 * Note: there is always a possibily of a race where session
4651 * deletion has already started for other reasons (e.g. ACL
4652 * removal) and now PLOGI arrives:
4653 * 1. if PLOGI arrived in FW after nport handle has been freed,
4654 * FW must have assigned this PLOGI a new/same handle and we
4655 * can proceed ACK'ing it as usual when session deletion
4656 * completes.
4657 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4658 * bit reached it, the handle has now been released. We'll
4659 * get an error when we ACK this PLOGI. Nothing will be sent
4660 * back to initiator. Initiator should eventually retry
4661 * PLOGI and situation will correct itself.
4662 */
4663 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4664 (sess->d_id.b24 == port_id.b24));
4665
Quinn Tran83548fe2017-06-02 09:12:01 -07004666 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4667 "%s %d %8phC post del sess\n",
4668 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004669
4670
4671 qlt_schedule_sess_for_deletion_lock(sess);
4672 break;
4673 }
4674
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004675 break;
4676
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004677 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004678 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4679
Quinn Tran75601512015-12-17 14:57:04 -05004680 if (wwn) {
4681 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004682 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4683 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004684 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4685 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004686
4687 if (conflict_sess) {
4688 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4689 "PRLI with conflicting sess %p port %8phC\n",
4690 conflict_sess, conflict_sess->port_name);
4691 qlt_send_term_imm_notif(vha, iocb, 1);
4692 res = 0;
4693 break;
4694 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004695
4696 if (sess != NULL) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004697 if (sess->fw_login_state != DSC_LS_PLOGI_PEND &&
4698 sess->fw_login_state != DSC_LS_PLOGI_COMP) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004699 /*
4700 * Impatient initiator sent PRLI before last
4701 * PLOGI could finish. Will force him to re-try,
4702 * while last one finishes.
4703 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004704 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004705 "sess %p PRLI received, before plogi ack.\n",
4706 sess);
4707 qlt_send_term_imm_notif(vha, iocb, 1);
4708 res = 0;
4709 break;
4710 }
4711
4712 /*
4713 * This shouldn't happen under normal circumstances,
4714 * since we have deleted the old session during PLOGI
4715 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004716 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004717 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4718 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4719
4720 sess->local = 0;
4721 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004722 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004723 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004724
4725 if (wd3_lo & BIT_7)
4726 sess->conf_compl_supported = 1;
4727
Quinn Tran726b8542017-01-19 22:28:00 -08004728 if ((wd3_lo & BIT_4) == 0)
4729 sess->port_type = FCT_INITIATOR;
4730 else
4731 sess->port_type = FCT_TARGET;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004732 }
4733 res = 1; /* send notify ack */
4734
4735 /* Make session global (not used in fabric mode) */
4736 if (ha->current_topology != ISP_CFG_F) {
Quinn Tranec7193e2017-03-15 09:48:55 -07004737 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004738 ql_dbg(ql_dbg_disc, vha, 0x20fa,
Quinn Tranec7193e2017-03-15 09:48:55 -07004739 "%s %d %8phC post nack\n",
4740 __func__, __LINE__, sess->port_name);
4741 qla24xx_post_nack_work(vha, sess, iocb,
4742 SRB_NACK_PRLI);
4743 res = 0;
4744 } else {
4745 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4746 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4747 qla2xxx_wake_dpc(vha);
4748 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004749 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004750 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004751 ql_dbg(ql_dbg_disc, vha, 0x20fb,
Quinn Tranec7193e2017-03-15 09:48:55 -07004752 "%s %d %8phC post nack\n",
4753 __func__, __LINE__, sess->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08004754 qla24xx_post_nack_work(vha, sess, iocb,
4755 SRB_NACK_PRLI);
4756 res = 0;
4757 }
4758 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004759 break;
4760
Quinn Tran41dc5292017-01-19 22:28:03 -08004761 case ELS_TPRLO:
4762 if (le16_to_cpu(iocb->u.isp24.flags) &
4763 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4764 loop_id = 0xFFFF;
4765 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4766 res = 1;
4767 break;
4768 }
4769 /* drop through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004770 case ELS_LOGO:
4771 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004772 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4773 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4774 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4775
4776 if (sess) {
4777 sess->login_gen++;
4778 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004779 sess->logo_ack_needed = 1;
4780 memcpy(sess->iocb, iocb, IOCB_SIZE);
4781 }
4782
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004783 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004784
Quinn Tran83548fe2017-06-02 09:12:01 -07004785 ql_dbg(ql_dbg_disc, vha, 0x20fc,
Quinn Tran726b8542017-01-19 22:28:00 -08004786 "%s: logo %llx res %d sess %p ",
4787 __func__, wwn, res, sess);
4788 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004789 /*
4790 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4791 * for LOGO_ACK & sess delete
4792 */
Quinn Tran726b8542017-01-19 22:28:00 -08004793 BUG_ON(!sess);
4794 res = 0;
4795 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004796 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004797 if (sess) {
4798 qlt_schedule_sess_for_deletion_lock(sess);
4799 res = 0;
4800 }
4801 /* else logo will be ack */
4802 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004803 break;
4804 case ELS_PDISC:
4805 case ELS_ADISC:
4806 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004807 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004808 if (tgt->link_reinit_iocb_pending) {
4809 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4810 0, 0, 0, 0, 0, 0);
4811 tgt->link_reinit_iocb_pending = 0;
4812 }
Quinn Tran726b8542017-01-19 22:28:00 -08004813
4814 sess = qla2x00_find_fcport_by_wwpn(vha,
4815 iocb->u.isp24.port_name, 1);
4816 if (sess) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004817 ql_dbg(ql_dbg_disc, vha, 0x20fd,
Quinn Tran726b8542017-01-19 22:28:00 -08004818 "sess %p lid %d|%d DS %d LS %d\n",
4819 sess, sess->loop_id, loop_id,
4820 sess->disc_state, sess->fw_login_state);
4821 }
4822
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004823 res = 1; /* send notify ack */
4824 break;
4825 }
4826
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004827 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004828 default:
4829 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4830 "qla_target(%d): Unsupported ELS command %x "
4831 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4832 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4833 break;
4834 }
4835
4836 return res;
4837}
4838
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004839/*
4840 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4841 */
4842static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4843 struct imm_ntfy_from_isp *iocb)
4844{
4845 struct qla_hw_data *ha = vha->hw;
4846 uint32_t add_flags = 0;
4847 int send_notify_ack = 1;
4848 uint16_t status;
4849
4850 status = le16_to_cpu(iocb->u.isp2x.status);
4851 switch (status) {
4852 case IMM_NTFY_LIP_RESET:
4853 {
4854 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4855 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4856 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4857 iocb->u.isp24.status_subcode);
4858
4859 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4860 send_notify_ack = 0;
4861 break;
4862 }
4863
4864 case IMM_NTFY_LIP_LINK_REINIT:
4865 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004866 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004867 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4868 "qla_target(%d): LINK REINIT (loop %#x, "
4869 "subcode %x)\n", vha->vp_idx,
4870 le16_to_cpu(iocb->u.isp24.nport_handle),
4871 iocb->u.isp24.status_subcode);
4872 if (tgt->link_reinit_iocb_pending) {
4873 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4874 0, 0, 0, 0, 0, 0);
4875 }
4876 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4877 tgt->link_reinit_iocb_pending = 1;
4878 /*
4879 * QLogic requires to wait after LINK REINIT for possible
4880 * PDISC or ADISC ELS commands
4881 */
4882 send_notify_ack = 0;
4883 break;
4884 }
4885
4886 case IMM_NTFY_PORT_LOGOUT:
4887 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4888 "qla_target(%d): Port logout (loop "
4889 "%#x, subcode %x)\n", vha->vp_idx,
4890 le16_to_cpu(iocb->u.isp24.nport_handle),
4891 iocb->u.isp24.status_subcode);
4892
4893 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4894 send_notify_ack = 0;
4895 /* The sessions will be cleared in the callback, if needed */
4896 break;
4897
4898 case IMM_NTFY_GLBL_TPRLO:
4899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4900 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4901 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4902 send_notify_ack = 0;
4903 /* The sessions will be cleared in the callback, if needed */
4904 break;
4905
4906 case IMM_NTFY_PORT_CONFIG:
4907 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4908 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4909 status);
4910 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4911 send_notify_ack = 0;
4912 /* The sessions will be cleared in the callback, if needed */
4913 break;
4914
4915 case IMM_NTFY_GLBL_LOGO:
4916 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4917 "qla_target(%d): Link failure detected\n",
4918 vha->vp_idx);
4919 /* I_T nexus loss */
4920 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4921 send_notify_ack = 0;
4922 break;
4923
4924 case IMM_NTFY_IOCB_OVERFLOW:
4925 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4926 "qla_target(%d): Cannot provide requested "
4927 "capability (IOCB overflowed the immediate notify "
4928 "resource count)\n", vha->vp_idx);
4929 break;
4930
4931 case IMM_NTFY_ABORT_TASK:
4932 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4933 "qla_target(%d): Abort Task (S %08x I %#x -> "
4934 "L %#x)\n", vha->vp_idx,
4935 le16_to_cpu(iocb->u.isp2x.seq_id),
4936 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4937 le16_to_cpu(iocb->u.isp2x.lun));
4938 if (qlt_abort_task(vha, iocb) == 0)
4939 send_notify_ack = 0;
4940 break;
4941
4942 case IMM_NTFY_RESOURCE:
4943 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4944 "qla_target(%d): Out of resources, host %ld\n",
4945 vha->vp_idx, vha->host_no);
4946 break;
4947
4948 case IMM_NTFY_MSG_RX:
4949 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4950 "qla_target(%d): Immediate notify task %x\n",
4951 vha->vp_idx, iocb->u.isp2x.task_flags);
4952 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4953 send_notify_ack = 0;
4954 break;
4955
4956 case IMM_NTFY_ELS:
4957 if (qlt_24xx_handle_els(vha, iocb) == 0)
4958 send_notify_ack = 0;
4959 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004960 default:
4961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4962 "qla_target(%d): Received unknown immediate "
4963 "notify status %x\n", vha->vp_idx, status);
4964 break;
4965 }
4966
4967 if (send_notify_ack)
4968 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4969}
4970
4971/*
4972 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4973 * This function sends busy to ISP 2xxx or 24xx.
4974 */
Quinn Tran33e79972014-09-25 06:14:55 -04004975static int __qlt_send_busy(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004976 struct atio_from_isp *atio, uint16_t status)
4977{
4978 struct ctio7_to_24xx *ctio24;
4979 struct qla_hw_data *ha = vha->hw;
4980 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08004981 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05004982 unsigned long flags;
Quinn Tranf7e761f2017-06-02 09:12:02 -07004983 u16 temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004984
Quinn Tran75601512015-12-17 14:57:04 -05004985 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004986 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4987 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004988 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004989 if (!sess) {
Quinn Trana07100e2015-12-07 19:48:57 -05004990 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004991 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004992 }
4993 /* Sending marker isn't necessary, since we called from ISR */
4994
4995 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
4996 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04004997 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004998 "qla_target(%d): %s failed: unable to allocate "
4999 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005000 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005001 }
5002
Himanshu Madhanice1025c2015-12-17 14:56:58 -05005003 vha->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005004 pkt->entry_count = 1;
5005 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5006
5007 ctio24 = (struct ctio7_to_24xx *)pkt;
5008 ctio24->entry_type = CTIO_TYPE7;
5009 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005010 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005011 ctio24->vp_index = vha->vp_idx;
5012 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5013 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5014 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5015 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranf7e761f2017-06-02 09:12:02 -07005016 temp = (atio->u.isp24.attr << 9) |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005017 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
Quinn Tranf7e761f2017-06-02 09:12:02 -07005018 CTIO7_FLAGS_DONT_RET_CTIO;
5019 ctio24->u.status1.flags = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005020 /*
5021 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5022 * if the explicit conformation is used.
5023 */
5024 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5025 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005026 /* Memory Barrier */
5027 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005028 qla2x00_start_iocbs(vha, vha->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005029 return 0;
5030}
5031
5032/*
5033 * This routine is used to allocate a command for either a QFull condition
5034 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5035 * out previously.
5036 */
5037static void
5038qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5039 struct atio_from_isp *atio, uint16_t status, int qfull)
5040{
5041 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5042 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005043 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04005044 struct se_session *se_sess;
5045 struct qla_tgt_cmd *cmd;
5046 int tag;
5047
5048 if (unlikely(tgt->tgt_stop)) {
5049 ql_dbg(ql_dbg_io, vha, 0x300a,
5050 "New command while device %p is shutting down\n", tgt);
5051 return;
5052 }
5053
5054 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5055 vha->hw->tgt.num_qfull_cmds_dropped++;
5056 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005057 vha->qla_stats.stat_max_qfull_cmds_dropped)
5058 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005059 vha->hw->tgt.num_qfull_cmds_dropped;
5060
5061 ql_dbg(ql_dbg_io, vha, 0x3068,
5062 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5063 vha->vp_idx, __func__,
5064 vha->hw->tgt.num_qfull_cmds_dropped);
5065
5066 qlt_chk_exch_leak_thresh_hold(vha);
5067 return;
5068 }
5069
5070 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5071 (vha, atio->u.isp24.fcp_hdr.s_id);
5072 if (!sess)
5073 return;
5074
5075 se_sess = sess->se_sess;
5076
5077 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5078 if (tag < 0)
5079 return;
5080
5081 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5082 if (!cmd) {
5083 ql_dbg(ql_dbg_io, vha, 0x3009,
5084 "qla_target(%d): %s: Allocation of cmd failed\n",
5085 vha->vp_idx, __func__);
5086
5087 vha->hw->tgt.num_qfull_cmds_dropped++;
5088 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005089 vha->qla_stats.stat_max_qfull_cmds_dropped)
5090 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005091 vha->hw->tgt.num_qfull_cmds_dropped;
5092
5093 qlt_chk_exch_leak_thresh_hold(vha);
5094 return;
5095 }
5096
5097 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5098
5099 qlt_incr_num_pend_cmds(vha);
5100 INIT_LIST_HEAD(&cmd->cmd_list);
5101 memcpy(&cmd->atio, atio, sizeof(*atio));
5102
5103 cmd->tgt = vha->vha_tgt.qla_tgt;
5104 cmd->vha = vha;
5105 cmd->reset_count = vha->hw->chip_reset;
5106 cmd->q_full = 1;
5107
5108 if (qfull) {
5109 cmd->q_full = 1;
5110 /* NOTE: borrowing the state field to carry the status */
5111 cmd->state = status;
5112 } else
5113 cmd->term_exchg = 1;
5114
5115 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5116
5117 vha->hw->tgt.num_qfull_cmds_alloc++;
5118 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005119 vha->qla_stats.stat_max_qfull_cmds_alloc)
5120 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005121 vha->hw->tgt.num_qfull_cmds_alloc;
5122}
5123
5124int
5125qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5126{
5127 struct qla_hw_data *ha = vha->hw;
5128 unsigned long flags;
5129 struct qla_tgt_cmd *cmd, *tcmd;
5130 struct list_head free_list;
5131 int rc = 0;
5132
5133 if (list_empty(&ha->tgt.q_full_list))
5134 return 0;
5135
5136 INIT_LIST_HEAD(&free_list);
5137
5138 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5139
5140 if (list_empty(&ha->tgt.q_full_list)) {
5141 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5142 return 0;
5143 }
5144
5145 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5146 if (cmd->q_full)
5147 /* cmd->state is a borrowed field to hold status */
5148 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5149 else if (cmd->term_exchg)
5150 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5151
5152 if (rc == -ENOMEM)
5153 break;
5154
5155 if (cmd->q_full)
5156 ql_dbg(ql_dbg_io, vha, 0x3006,
5157 "%s: busy sent for ox_id[%04x]\n", __func__,
5158 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5159 else if (cmd->term_exchg)
5160 ql_dbg(ql_dbg_io, vha, 0x3007,
5161 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5162 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5163 else
5164 ql_dbg(ql_dbg_io, vha, 0x3008,
5165 "%s: Unexpected cmd in QFull list %p\n", __func__,
5166 cmd);
5167
5168 list_del(&cmd->cmd_list);
5169 list_add_tail(&cmd->cmd_list, &free_list);
5170
5171 /* piggy back on hardware_lock for protection */
5172 vha->hw->tgt.num_qfull_cmds_alloc--;
5173 }
5174 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5175
5176 cmd = NULL;
5177
5178 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5179 list_del(&cmd->cmd_list);
5180 /* This cmd was never sent to TCM. There is no need
5181 * to schedule free or call free_cmd
5182 */
5183 qlt_free_cmd(cmd);
5184 }
5185 return rc;
5186}
5187
5188static void
5189qlt_send_busy(struct scsi_qla_host *vha,
5190 struct atio_from_isp *atio, uint16_t status)
5191{
5192 int rc = 0;
5193
5194 rc = __qlt_send_busy(vha, atio, status);
5195 if (rc == -ENOMEM)
5196 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5197}
5198
5199static int
5200qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
Quinn Tran8b666802017-03-15 09:48:45 -07005201 struct atio_from_isp *atio, bool ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005202{
5203 struct qla_hw_data *ha = vha->hw;
5204 uint16_t status;
Quinn Tran8b666802017-03-15 09:48:45 -07005205 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005206
5207 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5208 return 0;
5209
Quinn Tran8b666802017-03-15 09:48:45 -07005210 if (!ha_locked)
5211 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005212 status = temp_sam_status;
5213 qlt_send_busy(vha, atio, status);
Quinn Tran8b666802017-03-15 09:48:45 -07005214 if (!ha_locked)
5215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5216
Quinn Tran33e79972014-09-25 06:14:55 -04005217 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005218}
5219
5220/* ha->hardware_lock supposed to be held on entry */
5221/* called via callback from qla2xxx */
5222static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005223 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005224{
5225 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005226 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005227 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005228 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005229
5230 if (unlikely(tgt == NULL)) {
Quinn Tranec7193e2017-03-15 09:48:55 -07005231 ql_dbg(ql_dbg_tgt, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005232 "ATIO pkt, but no tgt (ha %p)", ha);
5233 return;
5234 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005235 /*
5236 * In tgt_stop mode we also should allow all requests to pass.
5237 * Otherwise, some commands can stuck.
5238 */
5239
Quinn Tran2f424b92015-12-17 14:57:07 -05005240 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005241
5242 switch (atio->u.raw.entry_type) {
5243 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005244 if (unlikely(atio->u.isp24.exchange_addr ==
5245 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005246 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005247 "qla_target(%d): ATIO_TYPE7 "
5248 "received with UNKNOWN exchange address, "
5249 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005250 if (!ha_locked)
5251 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005252 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005253 if (!ha_locked)
5254 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005255 break;
5256 }
Quinn Tran33e79972014-09-25 06:14:55 -04005257
5258
5259
5260 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran8b666802017-03-15 09:48:45 -07005261 rc = qlt_chk_qfull_thresh_hold(vha, atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005262 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005263 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005264 return;
5265 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005266 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005267 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005268 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005269 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005270 if (unlikely(rc != 0)) {
5271 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005272 if (!ha_locked)
5273 spin_lock_irqsave
5274 (&ha->hardware_lock, flags);
5275
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005276#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5277 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5278#else
Quinn Trana07100e2015-12-07 19:48:57 -05005279 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005280#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005281
5282 if (!ha_locked)
5283 spin_unlock_irqrestore
5284 (&ha->hardware_lock, flags);
5285
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005286 } else {
5287 if (tgt->tgt_stop) {
5288 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5289 "qla_target: Unable to send "
5290 "command to target for req, "
5291 "ignoring.\n");
5292 } else {
5293 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5294 "qla_target(%d): Unable to send "
5295 "command to target, sending BUSY "
5296 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005297 if (!ha_locked)
5298 spin_lock_irqsave(
5299 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005300 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005301 if (!ha_locked)
5302 spin_unlock_irqrestore(
5303 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005304 }
5305 }
5306 }
5307 break;
5308
5309 case IMMED_NOTIFY_TYPE:
5310 {
5311 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5312 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5313 "qla_target(%d): Received ATIO packet %x "
5314 "with error status %x\n", vha->vp_idx,
5315 atio->u.raw.entry_type,
5316 atio->u.isp2x.entry_status);
5317 break;
5318 }
5319 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005320
5321 if (!ha_locked)
5322 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005323 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005324 if (!ha_locked)
5325 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005326 break;
5327 }
5328
5329 default:
5330 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5331 "qla_target(%d): Received unknown ATIO atio "
5332 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5333 break;
5334 }
5335
Quinn Tran2f424b92015-12-17 14:57:07 -05005336 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005337}
5338
5339/* ha->hardware_lock supposed to be held on entry */
5340/* called via callback from qla2xxx */
5341static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5342{
5343 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005344 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005345
5346 if (unlikely(tgt == NULL)) {
5347 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5348 "qla_target(%d): Response pkt %x received, but no "
5349 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5350 return;
5351 }
5352
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005353 /*
5354 * In tgt_stop mode we also should allow all requests to pass.
5355 * Otherwise, some commands can stuck.
5356 */
5357
5358 tgt->irq_cmd_count++;
5359
5360 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005361 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005362 case CTIO_TYPE7:
5363 {
5364 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005365 qlt_do_ctio_completion(vha, entry->handle,
5366 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5367 entry);
5368 break;
5369 }
5370
5371 case ACCEPT_TGT_IO_TYPE:
5372 {
5373 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5374 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005375 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005376 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005377 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5378 "qla_target(%d): ATIO with error "
5379 "status %x received\n", vha->vp_idx,
5380 le16_to_cpu(atio->u.isp2x.status));
5381 break;
5382 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005383
Quinn Tran8b666802017-03-15 09:48:45 -07005384 rc = qlt_chk_qfull_thresh_hold(vha, atio, true);
Quinn Tran33e79972014-09-25 06:14:55 -04005385 if (rc != 0) {
5386 tgt->irq_cmd_count--;
5387 return;
5388 }
5389
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005390 rc = qlt_handle_cmd_for_atio(vha, atio);
5391 if (unlikely(rc != 0)) {
5392 if (rc == -ESRCH) {
5393#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5394 qlt_send_busy(vha, atio, 0);
5395#else
Quinn Trana07100e2015-12-07 19:48:57 -05005396 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005397#endif
5398 } else {
5399 if (tgt->tgt_stop) {
5400 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5401 "qla_target: Unable to send "
5402 "command to target, sending TERM "
5403 "EXCHANGE for rsp\n");
5404 qlt_send_term_exchange(vha, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005405 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005406 } else {
5407 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5408 "qla_target(%d): Unable to send "
5409 "command to target, sending BUSY "
5410 "status\n", vha->vp_idx);
5411 qlt_send_busy(vha, atio, 0);
5412 }
5413 }
5414 }
5415 }
5416 break;
5417
5418 case CONTINUE_TGT_IO_TYPE:
5419 {
5420 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005421 qlt_do_ctio_completion(vha, entry->handle,
5422 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5423 entry);
5424 break;
5425 }
5426
5427 case CTIO_A64_TYPE:
5428 {
5429 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005430 qlt_do_ctio_completion(vha, entry->handle,
5431 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5432 entry);
5433 break;
5434 }
5435
5436 case IMMED_NOTIFY_TYPE:
5437 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5438 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5439 break;
5440
5441 case NOTIFY_ACK_TYPE:
5442 if (tgt->notify_ack_expected > 0) {
5443 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5444 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5445 "NOTIFY_ACK seq %08x status %x\n",
5446 le16_to_cpu(entry->u.isp2x.seq_id),
5447 le16_to_cpu(entry->u.isp2x.status));
5448 tgt->notify_ack_expected--;
5449 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005450 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005451 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5452 "qla_target(%d): NOTIFY_ACK "
5453 "failed %x\n", vha->vp_idx,
5454 le16_to_cpu(entry->u.isp2x.status));
5455 }
5456 } else {
5457 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5458 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5459 vha->vp_idx);
5460 }
5461 break;
5462
5463 case ABTS_RECV_24XX:
5464 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5465 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5466 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5467 break;
5468
5469 case ABTS_RESP_24XX:
5470 if (tgt->abts_resp_expected > 0) {
5471 struct abts_resp_from_24xx_fw *entry =
5472 (struct abts_resp_from_24xx_fw *)pkt;
5473 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5474 "ABTS_RESP_24XX: compl_status %x\n",
5475 entry->compl_status);
5476 tgt->abts_resp_expected--;
5477 if (le16_to_cpu(entry->compl_status) !=
5478 ABTS_RESP_COMPL_SUCCESS) {
5479 if ((entry->error_subcode1 == 0x1E) &&
5480 (entry->error_subcode2 == 0)) {
5481 /*
5482 * We've got a race here: aborted
5483 * exchange not terminated, i.e.
5484 * response for the aborted command was
5485 * sent between the abort request was
5486 * received and processed.
5487 * Unfortunately, the firmware has a
5488 * silly requirement that all aborted
5489 * exchanges must be explicitely
5490 * terminated, otherwise it refuses to
5491 * send responses for the abort
5492 * requests. So, we have to
5493 * (re)terminate the exchange and retry
5494 * the abort response.
5495 */
5496 qlt_24xx_retry_term_exchange(vha,
5497 entry);
5498 } else
5499 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5500 "qla_target(%d): ABTS_RESP_24XX "
5501 "failed %x (subcode %x:%x)",
5502 vha->vp_idx, entry->compl_status,
5503 entry->error_subcode1,
5504 entry->error_subcode2);
5505 }
5506 } else {
5507 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5508 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5509 "received\n", vha->vp_idx);
5510 }
5511 break;
5512
5513 default:
5514 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5515 "qla_target(%d): Received unknown response pkt "
5516 "type %x\n", vha->vp_idx, pkt->entry_type);
5517 break;
5518 }
5519
5520 tgt->irq_cmd_count--;
5521}
5522
5523/*
5524 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5525 */
5526void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5527 uint16_t *mailbox)
5528{
5529 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005530 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005531 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005532
Quinn Tran3a33dc92017-06-02 09:12:04 -07005533 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005534 return;
5535
5536 if (unlikely(tgt == NULL)) {
5537 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5538 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5539 return;
5540 }
5541
5542 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5543 IS_QLA2100(ha))
5544 return;
5545 /*
5546 * In tgt_stop mode we also should allow all requests to pass.
5547 * Otherwise, some commands can stuck.
5548 */
5549
5550 tgt->irq_cmd_count++;
5551
5552 switch (code) {
5553 case MBA_RESET: /* Reset */
5554 case MBA_SYSTEM_ERR: /* System Error */
5555 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5556 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5557 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5558 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005559 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005560 break;
5561 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5562 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5563 break;
5564
5565 case MBA_LOOP_UP:
5566 {
5567 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005568 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005569 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5570 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5571 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005572 if (tgt->link_reinit_iocb_pending) {
5573 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5574 0, 0, 0, 0, 0, 0);
5575 tgt->link_reinit_iocb_pending = 0;
5576 }
5577 break;
5578 }
5579
5580 case MBA_LIP_OCCURRED:
5581 case MBA_LOOP_DOWN:
5582 case MBA_LIP_RESET:
5583 case MBA_RSCN_UPDATE:
5584 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005585 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005586 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5587 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5588 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005589 break;
5590
Quinn Tranead03852017-01-19 22:28:01 -08005591 case MBA_REJECTED_FCP_CMD:
Quinn Tran83548fe2017-06-02 09:12:01 -07005592 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5593 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5594 vha->vp_idx,
5595 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5596 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Quinn Tranead03852017-01-19 22:28:01 -08005597
5598 if (le16_to_cpu(mailbox[3]) == 1) {
5599 /* exchange starvation. */
5600 vha->hw->exch_starvation++;
5601 if (vha->hw->exch_starvation > 5) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005602 ql_log(ql_log_warn, vha, 0xd03a,
Quinn Tranead03852017-01-19 22:28:01 -08005603 "Exchange starvation-. Resetting RISC\n");
5604
5605 vha->hw->exch_starvation = 0;
5606 if (IS_P3P_TYPE(vha->hw))
5607 set_bit(FCOE_CTX_RESET_NEEDED,
5608 &vha->dpc_flags);
5609 else
5610 set_bit(ISP_ABORT_NEEDED,
5611 &vha->dpc_flags);
5612 qla2xxx_wake_dpc(vha);
5613 }
5614 }
5615 break;
5616
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005617 case MBA_PORT_UPDATE:
5618 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5619 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005620 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005621 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5622 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5623 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5624
5625 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005626 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005627 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5628 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005629 vha->hw->exch_starvation = 0;
5630 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005631 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5632 "Async MB 2: Port Logged Out\n");
5633 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005634 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005635 break;
5636 }
5637
5638 tgt->irq_cmd_count--;
5639}
5640
5641static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5642 uint16_t loop_id)
5643{
Quinn Tran726b8542017-01-19 22:28:00 -08005644 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005645 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005646 unsigned long flags;
5647 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005648
5649 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5650 if (!fcport) {
5651 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5652 "qla_target(%d): Allocation of tmp FC port failed",
5653 vha->vp_idx);
5654 return NULL;
5655 }
5656
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005657 fcport->loop_id = loop_id;
5658
Quinn Tran15f30a52017-03-15 09:48:52 -07005659 rc = qla24xx_gpdb_wait(vha, fcport, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005660 if (rc != QLA_SUCCESS) {
5661 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5662 "qla_target(%d): Failed to retrieve fcport "
5663 "information -- get_port_database() returned %x "
5664 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5665 kfree(fcport);
5666 return NULL;
5667 }
5668
Quinn Tran726b8542017-01-19 22:28:00 -08005669 del = NULL;
5670 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5671 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5672
5673 if (tfcp) {
5674 tfcp->d_id = fcport->d_id;
5675 tfcp->port_type = fcport->port_type;
5676 tfcp->supported_classes = fcport->supported_classes;
5677 tfcp->flags |= fcport->flags;
5678
5679 del = fcport;
5680 fcport = tfcp;
5681 } else {
5682 if (vha->hw->current_topology == ISP_CFG_F)
5683 fcport->flags |= FCF_FABRIC_DEVICE;
5684
5685 list_add_tail(&fcport->list, &vha->vp_fcports);
5686 if (!IS_SW_RESV_ADDR(fcport->d_id))
5687 vha->fcport_count++;
5688 fcport->login_gen++;
5689 fcport->disc_state = DSC_LOGIN_COMPLETE;
5690 fcport->login_succ = 1;
5691 newfcport = 1;
5692 }
5693
5694 fcport->deleted = 0;
5695 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5696
5697 switch (vha->host->active_mode) {
5698 case MODE_INITIATOR:
5699 case MODE_DUAL:
5700 if (newfcport) {
5701 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005702 ql_dbg(ql_dbg_disc, vha, 0x20fe,
Quinn Tran726b8542017-01-19 22:28:00 -08005703 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5704 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5705 qla24xx_post_upd_fcport_work(vha, fcport);
5706 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -07005707 ql_dbg(ql_dbg_disc, vha, 0x20ff,
Quinn Tran726b8542017-01-19 22:28:00 -08005708 "%s %d %8phC post gpsc fcp_cnt %d\n",
5709 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5710 qla24xx_post_gpsc_work(vha, fcport);
5711 }
5712 }
5713 break;
5714
5715 case MODE_TARGET:
5716 default:
5717 break;
5718 }
5719 if (del)
5720 qla2x00_free_fcport(del);
5721
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005722 return fcport;
5723}
5724
5725/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005726static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005727 uint8_t *s_id)
5728{
Quinn Tran5d964832017-01-19 22:27:59 -08005729 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005730 fc_port_t *fcport = NULL;
5731 int rc, global_resets;
5732 uint16_t loop_id = 0;
5733
Quinn Tran726b8542017-01-19 22:28:00 -08005734 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5735 /*
5736 * This is Domain Controller, so it should be
5737 * OK to drop SCSI commands from it.
5738 */
5739 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5740 "Unable to find initiator with S_ID %x:%x:%x",
5741 s_id[0], s_id[1], s_id[2]);
5742 return NULL;
5743 }
5744
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005745 mutex_lock(&vha->vha_tgt.tgt_mutex);
5746
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005747retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005748 global_resets =
5749 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005750
5751 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5752 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005753 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5754
Quinn Tran726b8542017-01-19 22:28:00 -08005755 ql_log(ql_log_info, vha, 0xf071,
5756 "qla_target(%d): Unable to find "
5757 "initiator with S_ID %x:%x:%x",
5758 vha->vp_idx, s_id[0], s_id[1],
5759 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005760
5761 if (rc == -ENOENT) {
5762 qlt_port_logo_t logo;
5763 sid_to_portid(s_id, &logo.id);
5764 logo.cmd_count = 1;
5765 qlt_send_first_logo(vha, &logo);
5766 }
5767
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005768 return NULL;
5769 }
5770
5771 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005772 if (!fcport) {
5773 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005774 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005775 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005776
5777 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005778 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005779 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5780 "qla_target(%d): global reset during session discovery "
5781 "(counter was %d, new %d), retrying", vha->vp_idx,
5782 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005783 atomic_read(&vha->vha_tgt.
5784 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005785 goto retry;
5786 }
5787
5788 sess = qlt_create_sess(vha, fcport, true);
5789
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005790 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5791
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005792 return sess;
5793}
5794
5795static void qlt_abort_work(struct qla_tgt *tgt,
5796 struct qla_tgt_sess_work_param *prm)
5797{
5798 struct scsi_qla_host *vha = tgt->vha;
5799 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005800 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005801 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005802 uint32_t be_s_id;
5803 uint8_t s_id[3];
5804 int rc;
5805
Quinn Tran75601512015-12-17 14:57:04 -05005806 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005807
5808 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005809 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005810
5811 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5812 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5813 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5814
5815 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5816 (unsigned char *)&be_s_id);
5817 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005818 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005819
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005820 sess = qlt_make_local_sess(vha, s_id);
5821 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005822
Quinn Tran75601512015-12-17 14:57:04 -05005823 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005824 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005825 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005826 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005827 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005828 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005829 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005830 }
5831
Quinn Tran726b8542017-01-19 22:28:00 -08005832 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005833 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
Quinn Tran726b8542017-01-19 22:28:00 -08005834 "%s: kref_get fail %8phC \n",
5835 __func__, sess->port_name);
5836 sess = NULL;
5837 goto out_term2;
5838 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005839 }
5840
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005841 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005842 ha->tgt.tgt_ops->put_sess(sess);
5843 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5844
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005845 if (rc != 0)
5846 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005847 return;
5848
Quinn Tran75601512015-12-17 14:57:04 -05005849out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08005850 if (sess)
5851 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005852 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005853
5854out_term:
5855 spin_lock_irqsave(&ha->hardware_lock, flags);
5856 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
5857 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005858}
5859
5860static void qlt_tmr_work(struct qla_tgt *tgt,
5861 struct qla_tgt_sess_work_param *prm)
5862{
5863 struct atio_from_isp *a = &prm->tm_iocb2;
5864 struct scsi_qla_host *vha = tgt->vha;
5865 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005866 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005867 unsigned long flags;
5868 uint8_t *s_id = NULL; /* to hide compiler warnings */
5869 int rc;
Quinn Tranf775bd12017-06-02 09:11:59 -07005870 u64 unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005871 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005872 void *iocb;
5873
Quinn Tran75601512015-12-17 14:57:04 -05005874 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005875
5876 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005877 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005878
5879 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5880 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5881 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005882 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005883
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005884 sess = qlt_make_local_sess(vha, s_id);
5885 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005886
Quinn Tran75601512015-12-17 14:57:04 -05005887 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005888 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005889 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005890 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005891 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005892 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07005893 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005894 }
5895
Quinn Tran726b8542017-01-19 22:28:00 -08005896 if (!kref_get_unless_zero(&sess->sess_kref)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005897 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
Quinn Tran726b8542017-01-19 22:28:00 -08005898 "%s: kref_get fail %8phC\n",
5899 __func__, sess->port_name);
5900 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07005901 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08005902 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005903 }
5904
5905 iocb = a;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005906 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tranf775bd12017-06-02 09:11:59 -07005907 unpacked_lun =
5908 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005909
5910 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005911 ha->tgt.tgt_ops->put_sess(sess);
5912 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5913
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005914 if (rc != 0)
5915 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005916 return;
5917
Quinn Tranf159b3c2017-03-15 09:48:47 -07005918out_term2:
5919 if (sess)
5920 ha->tgt.tgt_ops->put_sess(sess);
5921 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005922out_term:
Quinn Trana07100e2015-12-07 19:48:57 -05005923 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005924}
5925
5926static void qlt_sess_work_fn(struct work_struct *work)
5927{
5928 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5929 struct scsi_qla_host *vha = tgt->vha;
5930 unsigned long flags;
5931
5932 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5933
5934 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5935 while (!list_empty(&tgt->sess_works_list)) {
5936 struct qla_tgt_sess_work_param *prm = list_entry(
5937 tgt->sess_works_list.next, typeof(*prm),
5938 sess_works_list_entry);
5939
5940 /*
5941 * This work can be scheduled on several CPUs at time, so we
5942 * must delete the entry to eliminate double processing
5943 */
5944 list_del(&prm->sess_works_list_entry);
5945
5946 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5947
5948 switch (prm->type) {
5949 case QLA_TGT_SESS_WORK_ABORT:
5950 qlt_abort_work(tgt, prm);
5951 break;
5952 case QLA_TGT_SESS_WORK_TM:
5953 qlt_tmr_work(tgt, prm);
5954 break;
5955 default:
5956 BUG_ON(1);
5957 break;
5958 }
5959
5960 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5961
5962 kfree(prm);
5963 }
5964 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5965}
5966
5967/* Must be called under tgt_host_action_mutex */
5968int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5969{
5970 struct qla_tgt *tgt;
5971
5972 if (!QLA_TGT_MODE_ENABLED())
5973 return 0;
5974
Arun Easi33c36c02013-01-30 03:34:41 -05005975 if (!IS_TGT_MODE_CAPABLE(ha)) {
5976 ql_log(ql_log_warn, base_vha, 0xe070,
5977 "This adapter does not support target mode.\n");
5978 return 0;
5979 }
5980
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005981 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005982 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005983
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005984 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005985
5986 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5987 if (!tgt) {
5988 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5989 "Unable to allocate struct qla_tgt\n");
5990 return -ENOMEM;
5991 }
5992
5993 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5994 base_vha->host->hostt->supported_mode |= MODE_TARGET;
5995
5996 tgt->ha = ha;
5997 tgt->vha = base_vha;
5998 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005999 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006000 spin_lock_init(&tgt->sess_work_lock);
6001 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6002 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006003 atomic_set(&tgt->tgt_global_resets_count, 0);
6004
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006005 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006006
6007 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6008 "qla_target(%d): using 64 Bit PCI addressing",
6009 base_vha->vp_idx);
6010 tgt->tgt_enable_64bit_addr = 1;
6011 /* 3 is reserved */
6012 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6013 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
6014 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
6015
6016 mutex_lock(&qla_tgt_mutex);
6017 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6018 mutex_unlock(&qla_tgt_mutex);
6019
Quinn Tranf1443ee2017-03-15 09:48:51 -07006020 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6021 ha->tgt.tgt_ops->add_target(base_vha);
6022
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006023 return 0;
6024}
6025
6026/* Must be called under tgt_host_action_mutex */
6027int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6028{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006029 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006030 return 0;
6031
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006032 if (vha->fc_vport) {
6033 qlt_release(vha->vha_tgt.qla_tgt);
6034 return 0;
6035 }
Quinn Tran33e79972014-09-25 06:14:55 -04006036
6037 /* free left over qfull cmds */
6038 qlt_init_term_exchange(vha);
6039
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006040 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006041 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006042 mutex_unlock(&qla_tgt_mutex);
6043
6044 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6045 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006046 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006047
6048 return 0;
6049}
6050
Quinn Tran482c9dc2017-03-15 09:48:54 -07006051void qlt_remove_target_resources(struct qla_hw_data *ha)
6052{
6053 struct scsi_qla_host *node;
6054 u32 key = 0;
6055
6056 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6057 btree_remove32(&ha->tgt.host_map, key);
6058
6059 btree_destroy32(&ha->tgt.host_map);
6060}
6061
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006062static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6063 unsigned char *b)
6064{
6065 int i;
6066
6067 pr_debug("qla2xxx HW vha->node_name: ");
6068 for (i = 0; i < WWN_SIZE; i++)
6069 pr_debug("%02x ", vha->node_name[i]);
6070 pr_debug("\n");
6071 pr_debug("qla2xxx HW vha->port_name: ");
6072 for (i = 0; i < WWN_SIZE; i++)
6073 pr_debug("%02x ", vha->port_name[i]);
6074 pr_debug("\n");
6075
6076 pr_debug("qla2xxx passed configfs WWPN: ");
6077 put_unaligned_be64(wwpn, b);
6078 for (i = 0; i < WWN_SIZE; i++)
6079 pr_debug("%02x ", b[i]);
6080 pr_debug("\n");
6081}
6082
6083/**
6084 * qla_tgt_lport_register - register lport with external module
6085 *
6086 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6087 * @wwpn: Passwd FC target WWPN
6088 * @callback: lport initialization callback for tcm_qla2xxx code
6089 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6090 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006091int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6092 u64 npiv_wwpn, u64 npiv_wwnn,
6093 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006094{
6095 struct qla_tgt *tgt;
6096 struct scsi_qla_host *vha;
6097 struct qla_hw_data *ha;
6098 struct Scsi_Host *host;
6099 unsigned long flags;
6100 int rc;
6101 u8 b[WWN_SIZE];
6102
6103 mutex_lock(&qla_tgt_mutex);
6104 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6105 vha = tgt->vha;
6106 ha = vha->hw;
6107
6108 host = vha->host;
6109 if (!host)
6110 continue;
6111
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006112 if (!(host->hostt->supported_mode & MODE_TARGET))
6113 continue;
6114
6115 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006116 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006117 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6118 host->host_no);
6119 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6120 continue;
6121 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006122 if (tgt->tgt_stop) {
6123 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6124 host->host_no);
6125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6126 continue;
6127 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006128 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6129
6130 if (!scsi_host_get(host)) {
6131 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6132 "Unable to scsi_host_get() for"
6133 " qla2xxx scsi_host\n");
6134 continue;
6135 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006136 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006137
6138 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6139 scsi_host_put(host);
6140 continue;
6141 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006142 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6143 if (rc != 0)
6144 scsi_host_put(host);
6145
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006146 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006147 return rc;
6148 }
6149 mutex_unlock(&qla_tgt_mutex);
6150
6151 return -ENODEV;
6152}
6153EXPORT_SYMBOL(qlt_lport_register);
6154
6155/**
6156 * qla_tgt_lport_deregister - Degister lport
6157 *
6158 * @vha: Registered scsi_qla_host pointer
6159 */
6160void qlt_lport_deregister(struct scsi_qla_host *vha)
6161{
6162 struct qla_hw_data *ha = vha->hw;
6163 struct Scsi_Host *sh = vha->host;
6164 /*
6165 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6166 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006167 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006168 ha->tgt.tgt_ops = NULL;
6169 /*
6170 * Release the Scsi_Host reference for the underlying qla2xxx host
6171 */
6172 scsi_host_put(sh);
6173}
6174EXPORT_SYMBOL(qlt_lport_deregister);
6175
6176/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006177static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006178{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006179 switch (ql2x_ini_mode) {
6180 case QLA2XXX_INI_MODE_DISABLED:
6181 case QLA2XXX_INI_MODE_EXCLUSIVE:
6182 vha->host->active_mode = MODE_TARGET;
6183 break;
6184 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006185 vha->host->active_mode = MODE_UNKNOWN;
6186 break;
6187 case QLA2XXX_INI_MODE_DUAL:
6188 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006189 break;
6190 default:
6191 break;
6192 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006193}
6194
6195/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006196static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006197{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006198 switch (ql2x_ini_mode) {
6199 case QLA2XXX_INI_MODE_DISABLED:
6200 vha->host->active_mode = MODE_UNKNOWN;
6201 break;
6202 case QLA2XXX_INI_MODE_EXCLUSIVE:
6203 vha->host->active_mode = MODE_INITIATOR;
6204 break;
6205 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006206 case QLA2XXX_INI_MODE_DUAL:
6207 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006208 break;
6209 default:
6210 break;
6211 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006212}
6213
6214/*
6215 * qla_tgt_enable_vha - NO LOCK HELD
6216 *
6217 * host_reset, bring up w/ Target Mode Enabled
6218 */
6219void
6220qlt_enable_vha(struct scsi_qla_host *vha)
6221{
6222 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006223 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006224 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006225 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Trancdb898c2015-12-17 14:57:05 -05006226 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006227
6228 if (!tgt) {
6229 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6230 "Unable to locate qla_tgt pointer from"
6231 " struct qla_hw_data\n");
6232 dump_stack();
6233 return;
6234 }
6235
6236 spin_lock_irqsave(&ha->hardware_lock, flags);
6237 tgt->tgt_stopped = 0;
6238 qlt_set_mode(vha);
6239 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6240
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006241 if (vha->vp_idx) {
6242 qla24xx_disable_vp(vha);
6243 qla24xx_enable_vp(vha);
6244 } else {
Quinn Trancdb898c2015-12-17 14:57:05 -05006245 if (ha->msix_entries) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006246 ql_dbg(ql_dbg_tgt, vha, 0xe081,
Quinn Trancdb898c2015-12-17 14:57:05 -05006247 "%s: host%ld : vector %d cpu %d\n",
6248 __func__, vha->host_no,
6249 ha->msix_entries[rspq_ent].vector,
6250 ha->msix_entries[rspq_ent].cpuid);
6251
6252 ha->tgt.rspq_vector_cpuid =
6253 ha->msix_entries[rspq_ent].cpuid;
6254 }
6255
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006256 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6257 qla2xxx_wake_dpc(base_vha);
6258 qla2x00_wait_for_hba_online(base_vha);
6259 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006260}
6261EXPORT_SYMBOL(qlt_enable_vha);
6262
6263/*
6264 * qla_tgt_disable_vha - NO LOCK HELD
6265 *
6266 * Disable Target Mode and reset the adapter
6267 */
Joern Engel55a90662014-09-16 16:23:15 -04006268static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006269{
6270 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006271 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006272 unsigned long flags;
6273
6274 if (!tgt) {
6275 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6276 "Unable to locate qla_tgt pointer from"
6277 " struct qla_hw_data\n");
6278 dump_stack();
6279 return;
6280 }
6281
6282 spin_lock_irqsave(&ha->hardware_lock, flags);
6283 qlt_clear_mode(vha);
6284 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6285
6286 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6287 qla2xxx_wake_dpc(vha);
6288 qla2x00_wait_for_hba_online(vha);
6289}
6290
6291/*
6292 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6293 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6294 * members.
6295 */
6296void
6297qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6298{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006299 vha->vha_tgt.qla_tgt = NULL;
6300
6301 mutex_init(&vha->vha_tgt.tgt_mutex);
6302 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006303
6304 qlt_clear_mode(vha);
6305
6306 /*
6307 * NOTE: Currently the value is kept the same for <24xx and
6308 * >=24xx ISPs. If it is necessary to change it,
6309 * the check should be added for specific ISPs,
6310 * assigning the value appropriately.
6311 */
6312 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006313
6314 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006315}
6316
6317void
6318qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6319{
6320 /*
6321 * FC-4 Feature bit 0 indicates target functionality to the name server.
6322 */
6323 if (qla_tgt_mode_enabled(vha)) {
Quinn Tran726b8542017-01-19 22:28:00 -08006324 ct_req->req.rff_id.fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006325 } else if (qla_ini_mode_enabled(vha)) {
6326 ct_req->req.rff_id.fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006327 } else if (qla_dual_mode_enabled(vha))
6328 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006329}
6330
6331/*
6332 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6333 * @ha: HA context
6334 *
6335 * Beginning of ATIO ring has initialization control block already built
6336 * by nvram config routine.
6337 *
6338 * Returns 0 on success.
6339 */
6340void
6341qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6342{
6343 struct qla_hw_data *ha = vha->hw;
6344 uint16_t cnt;
6345 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6346
Quinn Tranead03852017-01-19 22:28:01 -08006347 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006348 return;
6349
6350 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6351 pkt->u.raw.signature = ATIO_PROCESSED;
6352 pkt++;
6353 }
6354
6355}
6356
6357/*
6358 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6359 * @ha: SCSI driver HA context
6360 */
6361void
Quinn Tran2f424b92015-12-17 14:57:07 -05006362qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006363{
6364 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006365 struct atio_from_isp *pkt;
6366 int cnt, i;
6367
Quinn Tranec7193e2017-03-15 09:48:55 -07006368 if (!ha->flags.fw_started)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006369 return;
6370
Quinn Tran5f355092016-12-23 18:06:11 -08006371 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6372 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006373 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6374 cnt = pkt->u.raw.entry_count;
6375
Quinn Tran5f355092016-12-23 18:06:11 -08006376 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6377 /*
6378 * This packet is corrupted. The header + payload
6379 * can not be trusted. There is no point in passing
6380 * it further up.
6381 */
Quinn Tran83548fe2017-06-02 09:12:01 -07006382 ql_log(ql_log_warn, vha, 0xd03c,
Quinn Tran5f355092016-12-23 18:06:11 -08006383 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6384 pkt->u.isp24.fcp_hdr.s_id,
6385 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6386 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6387
6388 adjust_corrupted_atio(pkt);
6389 qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
6390 } else {
6391 qlt_24xx_atio_pkt_all_vps(vha,
6392 (struct atio_from_isp *)pkt, ha_locked);
6393 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006394
6395 for (i = 0; i < cnt; i++) {
6396 ha->tgt.atio_ring_index++;
6397 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6398 ha->tgt.atio_ring_index = 0;
6399 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6400 } else
6401 ha->tgt.atio_ring_ptr++;
6402
6403 pkt->u.raw.signature = ATIO_PROCESSED;
6404 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6405 }
6406 wmb();
6407 }
6408
6409 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006410 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Quinn Tran3761f3e2015-06-10 11:05:19 -04006411 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006412}
6413
6414void
Arun Easiaa230bc2013-01-30 03:34:39 -05006415qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006416{
6417 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006418 if (!QLA_TGT_MODE_ENABLED())
6419 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006420
Arun Easiaa230bc2013-01-30 03:34:39 -05006421 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6422 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6423 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6424
6425 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6426 struct qla_msix_entry *msix = &ha->msix_entries[2];
6427 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6428
6429 icb->msix_atio = cpu_to_le16(msix->entry);
6430 ql_dbg(ql_dbg_init, vha, 0xf072,
6431 "Registering ICB vector 0x%x for atio que.\n",
6432 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006433 }
6434}
6435
6436void
6437qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6438{
6439 struct qla_hw_data *ha = vha->hw;
Quinn Tran99e1b682017-06-02 09:12:03 -07006440
6441 if (!QLA_TGT_MODE_ENABLED())
6442 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006443
Quinn Tranead03852017-01-19 22:28:01 -08006444 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006445 if (!ha->tgt.saved_set) {
6446 /* We save only once */
6447 ha->tgt.saved_exchange_count = nv->exchange_count;
6448 ha->tgt.saved_firmware_options_1 =
6449 nv->firmware_options_1;
6450 ha->tgt.saved_firmware_options_2 =
6451 nv->firmware_options_2;
6452 ha->tgt.saved_firmware_options_3 =
6453 nv->firmware_options_3;
6454 ha->tgt.saved_set = 1;
6455 }
6456
Quinn Tran99e1b682017-06-02 09:12:03 -07006457 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006458 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006459 else /* dual */
6460 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006461
6462 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006463 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006464
6465 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006466 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006467 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006468
6469 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006470 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006471 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006472 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006473 if (ql2xtgt_tape_enable)
6474 /* Enable FC Tape support */
6475 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6476 else
6477 /* Disable FC Tape support */
6478 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6479
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006480 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006481 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006482
6483 /*
6484 * clear BIT 15 explicitly as we have seen at least
6485 * a couple of instances where this was set and this
6486 * was causing the firmware to not be initialized.
6487 */
6488 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006489 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006490 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006491 } else {
6492 if (ha->tgt.saved_set) {
6493 nv->exchange_count = ha->tgt.saved_exchange_count;
6494 nv->firmware_options_1 =
6495 ha->tgt.saved_firmware_options_1;
6496 nv->firmware_options_2 =
6497 ha->tgt.saved_firmware_options_2;
6498 nv->firmware_options_3 =
6499 ha->tgt.saved_firmware_options_3;
6500 }
6501 return;
6502 }
6503
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006504 if (ha->tgt.enable_class_2) {
6505 if (vha->flags.init_done)
6506 fc_host_supported_classes(vha->host) =
6507 FC_COS_CLASS2 | FC_COS_CLASS3;
6508
Bart Van Asschead950362015-07-09 07:24:08 -07006509 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006510 } else {
6511 if (vha->flags.init_done)
6512 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6513
Bart Van Asschead950362015-07-09 07:24:08 -07006514 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006515 }
6516}
6517
6518void
6519qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6520 struct init_cb_24xx *icb)
6521{
6522 struct qla_hw_data *ha = vha->hw;
6523
Quinn Tran481ce732015-12-17 14:57:08 -05006524 if (!QLA_TGT_MODE_ENABLED())
6525 return;
6526
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006527 if (ha->tgt.node_name_set) {
6528 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006529 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006530 }
Quinn Tran481ce732015-12-17 14:57:08 -05006531
6532 /* disable ZIO at start time. */
6533 if (!vha->flags.init_done) {
6534 uint32_t tmp;
6535 tmp = le32_to_cpu(icb->firmware_options_2);
6536 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6537 icb->firmware_options_2 = cpu_to_le32(tmp);
6538 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006539}
6540
Arun Easiaa230bc2013-01-30 03:34:39 -05006541void
6542qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6543{
6544 struct qla_hw_data *ha = vha->hw;
6545
6546 if (!QLA_TGT_MODE_ENABLED())
6547 return;
6548
Quinn Tranead03852017-01-19 22:28:01 -08006549 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006550 if (!ha->tgt.saved_set) {
6551 /* We save only once */
6552 ha->tgt.saved_exchange_count = nv->exchange_count;
6553 ha->tgt.saved_firmware_options_1 =
6554 nv->firmware_options_1;
6555 ha->tgt.saved_firmware_options_2 =
6556 nv->firmware_options_2;
6557 ha->tgt.saved_firmware_options_3 =
6558 nv->firmware_options_3;
6559 ha->tgt.saved_set = 1;
6560 }
6561
Quinn Tran99e1b682017-06-02 09:12:03 -07006562 if (qla_tgt_mode_enabled(vha))
Quinn Tranead03852017-01-19 22:28:01 -08006563 nv->exchange_count = cpu_to_le16(0xFFFF);
Quinn Tran99e1b682017-06-02 09:12:03 -07006564 else /* dual */
6565 nv->exchange_count = cpu_to_le16(ql2xexchoffld);
Arun Easiaa230bc2013-01-30 03:34:39 -05006566
6567 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006568 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006569
6570 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006571 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006572 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006573 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006574 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006575 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006576 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006577 /*
6578 * clear BIT 15 explicitly as we have seen at
6579 * least a couple of instances where this was set
6580 * and this was causing the firmware to not be
6581 * initialized.
6582 */
6583 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006584 if (ql2xtgt_tape_enable)
6585 /* Enable FC tape support */
6586 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6587 else
6588 /* Disable FC tape support */
6589 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6590
Arun Easiaa230bc2013-01-30 03:34:39 -05006591 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006592 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006593 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006594 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006595 } else {
6596 if (ha->tgt.saved_set) {
6597 nv->exchange_count = ha->tgt.saved_exchange_count;
6598 nv->firmware_options_1 =
6599 ha->tgt.saved_firmware_options_1;
6600 nv->firmware_options_2 =
6601 ha->tgt.saved_firmware_options_2;
6602 nv->firmware_options_3 =
6603 ha->tgt.saved_firmware_options_3;
6604 }
6605 return;
6606 }
6607
Arun Easiaa230bc2013-01-30 03:34:39 -05006608 if (ha->tgt.enable_class_2) {
6609 if (vha->flags.init_done)
6610 fc_host_supported_classes(vha->host) =
6611 FC_COS_CLASS2 | FC_COS_CLASS3;
6612
Bart Van Asschead950362015-07-09 07:24:08 -07006613 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006614 } else {
6615 if (vha->flags.init_done)
6616 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6617
Bart Van Asschead950362015-07-09 07:24:08 -07006618 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006619 }
6620}
6621
6622void
6623qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6624 struct init_cb_81xx *icb)
6625{
6626 struct qla_hw_data *ha = vha->hw;
6627
6628 if (!QLA_TGT_MODE_ENABLED())
6629 return;
6630
6631 if (ha->tgt.node_name_set) {
6632 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006633 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006634 }
Quinn Tran481ce732015-12-17 14:57:08 -05006635
6636 /* disable ZIO at start time. */
6637 if (!vha->flags.init_done) {
6638 uint32_t tmp;
6639 tmp = le32_to_cpu(icb->firmware_options_2);
6640 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6641 icb->firmware_options_2 = cpu_to_le32(tmp);
6642 }
6643
Arun Easiaa230bc2013-01-30 03:34:39 -05006644}
6645
6646void
6647qlt_83xx_iospace_config(struct qla_hw_data *ha)
6648{
6649 if (!QLA_TGT_MODE_ENABLED())
6650 return;
6651
6652 ha->msix_count += 1; /* For ATIO Q */
6653}
6654
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006655int
6656qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6657 struct sts_entry_24xx *pkt)
6658{
6659 switch (pkt->entry_type) {
6660 case ABTS_RECV_24XX:
6661 case ABTS_RESP_24XX:
6662 case CTIO_TYPE7:
6663 case NOTIFY_ACK_TYPE:
Quinn Tranf83adb62014-04-11 16:54:43 -04006664 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006665 return 1;
6666 default:
6667 return 0;
6668 }
6669}
6670
6671void
6672qlt_modify_vp_config(struct scsi_qla_host *vha,
6673 struct vp_config_entry_24xx *vpmod)
6674{
Quinn Tranead03852017-01-19 22:28:01 -08006675 /* enable target mode. Bit5 = 1 => disable */
6676 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006677 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006678
Quinn Tranead03852017-01-19 22:28:01 -08006679 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006680 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006681 vpmod->options_idx1 &= ~BIT_4;
6682}
6683
6684void
6685qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6686{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006687 int rc;
6688
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006689 if (!QLA_TGT_MODE_ENABLED())
6690 return;
6691
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04006692 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006693 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6694 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6695 } else {
6696 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6697 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6698 }
6699
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006700 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6701 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006702
6703 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6704 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6705 qlt_unknown_atio_work_fn);
6706
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006707 qlt_clear_mode(base_vha);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006708
6709 rc = btree_init32(&ha->tgt.host_map);
6710 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006711 ql_log(ql_log_info, base_vha, 0xd03d,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006712 "Unable to initialize ha->host_map btree\n");
6713
6714 qlt_update_vp_map(base_vha, SET_VP_IDX);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006715}
6716
Arun Easiaa230bc2013-01-30 03:34:39 -05006717irqreturn_t
6718qla83xx_msix_atio_q(int irq, void *dev_id)
6719{
6720 struct rsp_que *rsp;
6721 scsi_qla_host_t *vha;
6722 struct qla_hw_data *ha;
6723 unsigned long flags;
6724
6725 rsp = (struct rsp_que *) dev_id;
6726 ha = rsp->hw;
6727 vha = pci_get_drvdata(ha->pdev);
6728
Quinn Tran2f424b92015-12-17 14:57:07 -05006729 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006730
Quinn Tran2f424b92015-12-17 14:57:07 -05006731 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006732
Quinn Tran2f424b92015-12-17 14:57:07 -05006733 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006734
6735 return IRQ_HANDLED;
6736}
6737
Quinn Tran2f424b92015-12-17 14:57:07 -05006738static void
6739qlt_handle_abts_recv_work(struct work_struct *work)
6740{
6741 struct qla_tgt_sess_op *op = container_of(work,
6742 struct qla_tgt_sess_op, work);
6743 scsi_qla_host_t *vha = op->vha;
6744 struct qla_hw_data *ha = vha->hw;
6745 unsigned long flags;
6746
6747 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6748 return;
6749
6750 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6751 qlt_24xx_process_atio_queue(vha, 0);
6752 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6753
6754 spin_lock_irqsave(&ha->hardware_lock, flags);
6755 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6756 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006757
6758 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006759}
6760
6761void
6762qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6763{
6764 struct qla_tgt_sess_op *op;
6765
6766 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6767
6768 if (!op) {
6769 /* do not reach for ATIO queue here. This is best effort err
6770 * recovery at this point.
6771 */
6772 qlt_response_pkt_all_vps(vha, pkt);
6773 return;
6774 }
6775
6776 memcpy(&op->atio, pkt, sizeof(*pkt));
6777 op->vha = vha;
6778 op->chip_reset = vha->hw->chip_reset;
6779 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6780 queue_work(qla_tgt_wq, &op->work);
6781 return;
6782}
6783
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006784int
6785qlt_mem_alloc(struct qla_hw_data *ha)
6786{
6787 if (!QLA_TGT_MODE_ENABLED())
6788 return 0;
6789
6790 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6791 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6792 if (!ha->tgt.tgt_vp_map)
6793 return -ENOMEM;
6794
6795 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6796 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6797 &ha->tgt.atio_dma, GFP_KERNEL);
6798 if (!ha->tgt.atio_ring) {
6799 kfree(ha->tgt.tgt_vp_map);
6800 return -ENOMEM;
6801 }
6802 return 0;
6803}
6804
6805void
6806qlt_mem_free(struct qla_hw_data *ha)
6807{
6808 if (!QLA_TGT_MODE_ENABLED())
6809 return;
6810
6811 if (ha->tgt.atio_ring) {
6812 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6813 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6814 ha->tgt.atio_dma);
6815 }
6816 kfree(ha->tgt.tgt_vp_map);
6817}
6818
6819/* vport_slock to be held by the caller */
6820void
6821qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6822{
Quinn Tran482c9dc2017-03-15 09:48:54 -07006823 void *slot;
6824 u32 key;
6825 int rc;
6826
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006827 if (!QLA_TGT_MODE_ENABLED())
6828 return;
6829
Quinn Tran482c9dc2017-03-15 09:48:54 -07006830 key = vha->d_id.b24;
6831
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006832 switch (cmd) {
6833 case SET_VP_IDX:
6834 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6835 break;
6836 case SET_AL_PA:
Quinn Tran482c9dc2017-03-15 09:48:54 -07006837 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6838 if (!slot) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006839 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006840 "Save vha in host_map %p %06x\n", vha, key);
6841 rc = btree_insert32(&vha->hw->tgt.host_map,
6842 key, vha, GFP_ATOMIC);
6843 if (rc)
Quinn Tran83548fe2017-06-02 09:12:01 -07006844 ql_log(ql_log_info, vha, 0xd03e,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006845 "Unable to insert s_id into host_map: %06x\n",
6846 key);
6847 return;
6848 }
Quinn Tran83548fe2017-06-02 09:12:01 -07006849 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
6850 "replace existing vha in host_map %p %06x\n", vha, key);
Quinn Tran482c9dc2017-03-15 09:48:54 -07006851 btree_update32(&vha->hw->tgt.host_map, key, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006852 break;
6853 case RESET_VP_IDX:
6854 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6855 break;
6856 case RESET_AL_PA:
Quinn Tran83548fe2017-06-02 09:12:01 -07006857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
Quinn Tran482c9dc2017-03-15 09:48:54 -07006858 "clear vha in host_map %p %06x\n", vha, key);
6859 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
6860 if (slot)
6861 btree_remove32(&vha->hw->tgt.host_map, key);
6862 vha->d_id.b24 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006863 break;
6864 }
6865}
6866
Quinn Tran482c9dc2017-03-15 09:48:54 -07006867void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
6868{
6869 unsigned long flags;
6870 struct qla_hw_data *ha = vha->hw;
6871
6872 if (!vha->d_id.b24) {
6873 spin_lock_irqsave(&ha->vport_slock, flags);
6874 vha->d_id = id;
6875 qlt_update_vp_map(vha, SET_AL_PA);
6876 spin_unlock_irqrestore(&ha->vport_slock, flags);
6877 } else if (vha->d_id.b24 != id.b24) {
6878 spin_lock_irqsave(&ha->vport_slock, flags);
6879 qlt_update_vp_map(vha, RESET_AL_PA);
6880 vha->d_id = id;
6881 qlt_update_vp_map(vha, SET_AL_PA);
6882 spin_unlock_irqrestore(&ha->vport_slock, flags);
6883 }
6884}
6885
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006886static int __init qlt_parse_ini_mode(void)
6887{
6888 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6889 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6890 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6891 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6892 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6893 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08006894 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
6895 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006896 else
6897 return false;
6898
6899 return true;
6900}
6901
6902int __init qlt_init(void)
6903{
6904 int ret;
6905
6906 if (!qlt_parse_ini_mode()) {
6907 ql_log(ql_log_fatal, NULL, 0xe06b,
6908 "qlt_parse_ini_mode() failed\n");
6909 return -EINVAL;
6910 }
6911
6912 if (!QLA_TGT_MODE_ENABLED())
6913 return 0;
6914
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006915 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6916 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6917 qla_tgt_mgmt_cmd), 0, NULL);
6918 if (!qla_tgt_mgmt_cmd_cachep) {
Quinn Tran83548fe2017-06-02 09:12:01 -07006919 ql_log(ql_log_fatal, NULL, 0xd04b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006920 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07006921 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006922 }
6923
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006924 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08006925 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
6926 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006927
6928 if (!qla_tgt_plogi_cachep) {
6929 ql_log(ql_log_fatal, NULL, 0xe06d,
6930 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6931 ret = -ENOMEM;
6932 goto out_mgmt_cmd_cachep;
6933 }
6934
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006935 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6936 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6937 if (!qla_tgt_mgmt_cmd_mempool) {
6938 ql_log(ql_log_fatal, NULL, 0xe06e,
6939 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6940 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006941 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006942 }
6943
6944 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6945 if (!qla_tgt_wq) {
6946 ql_log(ql_log_fatal, NULL, 0xe06f,
6947 "alloc_workqueue for qla_tgt_wq failed\n");
6948 ret = -ENOMEM;
6949 goto out_cmd_mempool;
6950 }
6951 /*
6952 * Return 1 to signal that initiator-mode is being disabled
6953 */
6954 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6955
6956out_cmd_mempool:
6957 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006958out_plogi_cachep:
6959 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006960out_mgmt_cmd_cachep:
6961 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006962 return ret;
6963}
6964
6965void qlt_exit(void)
6966{
6967 if (!QLA_TGT_MODE_ENABLED())
6968 return;
6969
6970 destroy_workqueue(qla_tgt_wq);
6971 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006972 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006973 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006974}