blob: 925d9b858b241ec5d0245cc00b62f4a97aa994ce [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3 *
4 * based on qla2x00t.c code:
5 *
6 * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7 * Copyright (C) 2004 - 2005 Leonid Stoljar
8 * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9 * Copyright (C) 2006 - 2010 ID7 Ltd.
10 *
11 * Forward port and refactoring to modern qla2xxx and target/configfs
12 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, version 2
18 * of the License.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#include <linux/blkdev.h>
30#include <linux/interrupt.h>
31#include <linux/pci.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/workqueue.h>
35#include <asm/unaligned.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_tcq.h>
39#include <target/target_core_base.h>
40#include <target/target_core_fabric.h>
41
42#include "qla_def.h"
43#include "qla_target.h"
44
Arun Easid154f352014-09-25 06:14:48 -040045static int ql2xtgt_tape_enable;
46module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040050static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51module_param(qlini_mode, charp, S_IRUGO);
52MODULE_PARM_DESC(qlini_mode,
53 "Determines when initiator mode will be enabled. Possible values: "
54 "\"exclusive\" - initiator mode will be enabled on load, "
55 "disabled on enabling target mode and then on disabling target mode "
56 "enabled back; "
57 "\"disabled\" - initiator mode will never be enabled; "
Quinn Tranead03852017-01-19 22:28:01 -080058 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040060 "\"enabled\" (default) - initiator mode will always stay enabled.");
61
Quinn Tranead03852017-01-19 22:28:01 -080062static int ql_dm_tgt_ex_pct = 50;
63module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
68
Arun Easiaa230bc2013-01-30 03:34:39 -050069int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040070
Quinn Tran33e79972014-09-25 06:14:55 -040071static int temp_sam_status = SAM_STAT_BUSY;
72
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040073/*
74 * From scsi/fc/fc_fcp.h
75 */
76enum fcp_resp_rsp_codes {
77 FCP_TMF_CMPL = 0,
78 FCP_DATA_LEN_INVALID = 1,
79 FCP_CMND_FIELDS_INVALID = 2,
80 FCP_DATA_PARAM_MISMATCH = 3,
81 FCP_TMF_REJECTED = 4,
82 FCP_TMF_FAILED = 5,
83 FCP_TMF_INVALID_LUN = 9,
84};
85
86/*
87 * fc_pri_ta from scsi/fc/fc_fcp.h
88 */
89#define FCP_PTA_SIMPLE 0 /* simple task attribute */
90#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
91#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +090092#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040093#define FCP_PTA_MASK 7 /* mask for task attribute field */
94#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
95#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
96
97/*
98 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
99 * must be called under HW lock and could unlock/lock it inside.
100 * It isn't an issue, since in the current implementation on the time when
101 * those functions are called:
102 *
103 * - Either context is IRQ and only IRQ handler can modify HW data,
104 * including rings related fields,
105 *
106 * - Or access to target mode variables from struct qla_tgt doesn't
107 * cross those functions boundaries, except tgt_stop, which
108 * additionally protected by irq_cmd_count.
109 */
110/* Predefs for callbacks handed to qla2xxx LLD */
111static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500112 struct atio_from_isp *pkt, uint8_t);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400113static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
Quinn Tran5d964832017-01-19 22:27:59 -0800114static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400115 int fn, void *iocb, int flags);
116static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
Quinn Trana07100e2015-12-07 19:48:57 -0500117 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
Arun Easib6a029e2014-09-25 06:14:52 -0400118static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
119 struct qla_tgt_cmd *cmd);
Quinn Tran33e79972014-09-25 06:14:55 -0400120static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
121 struct atio_from_isp *atio, uint16_t status, int qfull);
Joern Engel55a90662014-09-16 16:23:15 -0400122static void qlt_disable_vha(struct scsi_qla_host *vha);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400123static void qlt_clear_tgt_db(struct qla_tgt *tgt);
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400124static void qlt_send_notify_ack(struct scsi_qla_host *vha,
125 struct imm_ntfy_from_isp *ntfy,
126 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
127 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500128static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
129 struct imm_ntfy_from_isp *imm, int ha_locked);
Quinn Tran726b8542017-01-19 22:28:00 -0800130static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
131 fc_port_t *fcport, bool local);
132void qlt_unreg_sess(struct fc_port *sess);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700133static void qlt_24xx_handle_abts(struct scsi_qla_host *,
134 struct abts_recv_from_24xx *);
135
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400136/*
137 * Global Variables
138 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400139static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500140static struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400141static mempool_t *qla_tgt_mgmt_cmd_mempool;
142static struct workqueue_struct *qla_tgt_wq;
143static DEFINE_MUTEX(qla_tgt_mutex);
144static LIST_HEAD(qla_tgt_glist);
145
Alexei Potashnikdf673272015-07-14 16:00:46 -0400146/* This API intentionally takes dest as a parameter, rather than returning
147 * int value to avoid caller forgetting to issue wmb() after the store */
148void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
149{
150 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
151 *dest = atomic_inc_return(&base_vha->generation_tick);
152 /* memory barrier */
153 wmb();
154}
155
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400156/* Might release hw lock, then reaquire!! */
157static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
158{
159 /* Send marker if required */
160 if (unlikely(vha->marker_needed != 0)) {
161 int rc = qla2x00_issue_marker(vha, vha_locked);
162 if (rc != QLA_SUCCESS) {
163 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
164 "qla_target(%d): issue_marker() failed\n",
165 vha->vp_idx);
166 }
167 return rc;
168 }
169 return QLA_SUCCESS;
170}
171
172static inline
173struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
174 uint8_t *d_id)
175{
176 struct qla_hw_data *ha = vha->hw;
177 uint8_t vp_idx;
178
179 if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
180 return NULL;
181
182 if (vha->d_id.b.al_pa == d_id[2])
183 return vha;
184
185 BUG_ON(ha->tgt.tgt_vp_map == NULL);
186 vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
187 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
188 return ha->tgt.tgt_vp_map[vp_idx].vha;
189
190 return NULL;
191}
192
193static inline
194struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
195 uint16_t vp_idx)
196{
197 struct qla_hw_data *ha = vha->hw;
198
199 if (vha->vp_idx == vp_idx)
200 return vha;
201
202 BUG_ON(ha->tgt.tgt_vp_map == NULL);
203 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
204 return ha->tgt.tgt_vp_map[vp_idx].vha;
205
206 return NULL;
207}
208
Quinn Tran33e79972014-09-25 06:14:55 -0400209static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
210{
211 unsigned long flags;
212
213 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
214
215 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400216 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
217 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400218 vha->hw->tgt.num_pend_cmds;
219 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
220}
221static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
222{
223 unsigned long flags;
224
225 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
226 vha->hw->tgt.num_pend_cmds--;
227 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
228}
229
Quinn Tran41dc5292017-01-19 22:28:03 -0800230
231static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
232 struct atio_from_isp *atio, uint8_t ha_locked)
233{
234 struct qla_tgt_sess_op *u;
235 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
236 unsigned long flags;
237
238 if (tgt->tgt_stop) {
239 ql_dbg(ql_dbg_async, vha, 0xffff,
240 "qla_target(%d): dropping unknown ATIO_TYPE7, "
241 "because tgt is being stopped", vha->vp_idx);
242 goto out_term;
243 }
244
245 u = kzalloc(sizeof(*u), GFP_ATOMIC);
246 if (u == NULL) {
247 ql_dbg(ql_dbg_async, vha, 0xffff,
248 "Alloc of struct unknown_atio (size %zd) failed", sizeof(*u));
249 /* It should be harmless and on the next retry should work well */
250 goto out_term;
251 }
252
253 u->vha = vha;
254 memcpy(&u->atio, atio, sizeof(*atio));
255 INIT_LIST_HEAD(&u->cmd_list);
256
257 spin_lock_irqsave(&vha->cmd_list_lock, flags);
258 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
259 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
260
261 schedule_delayed_work(&vha->unknown_atio_work, 1);
262
263out:
264 return;
265
266out_term:
267 qlt_send_term_exchange(vha, NULL, atio, ha_locked, 0);
268 goto out;
269}
270
271static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
272 uint8_t ha_locked)
273{
274 struct qla_tgt_sess_op *u, *t;
275 scsi_qla_host_t *host;
276 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
277 unsigned long flags;
278 uint8_t queued = 0;
279
280 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
281 if (u->aborted) {
282 ql_dbg(ql_dbg_async, vha, 0xffff,
283 "Freeing unknown %s %p, because of Abort",
284 "ATIO_TYPE7", u);
285 qlt_send_term_exchange(vha, NULL, &u->atio,
286 ha_locked, 0);
287 goto abort;
288 }
289
290 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
291 if (host != NULL) {
292 ql_dbg(ql_dbg_async, vha, 0xffff,
293 "Requeuing unknown ATIO_TYPE7 %p", u);
294 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
295 } else if (tgt->tgt_stop) {
296 ql_dbg(ql_dbg_async, vha, 0xffff,
297 "Freeing unknown %s %p, because tgt is being stopped",
298 "ATIO_TYPE7", u);
299 qlt_send_term_exchange(vha, NULL, &u->atio,
300 ha_locked, 0);
301 } else {
302 ql_dbg(ql_dbg_async, vha, 0xffff,
303 "u %p, vha %p, host %p, sched again..", u,
304 vha, host);
305 if (!queued) {
306 queued = 1;
307 schedule_delayed_work(&vha->unknown_atio_work,
308 1);
309 }
310 continue;
311 }
312
313abort:
314 spin_lock_irqsave(&vha->cmd_list_lock, flags);
315 list_del(&u->cmd_list);
316 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
317 kfree(u);
318 }
319}
320
321void qlt_unknown_atio_work_fn(struct work_struct *work)
322{
323 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
324 struct scsi_qla_host, unknown_atio_work);
325
326 qlt_try_to_dequeue_unknown_atios(vha, 0);
327}
328
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500329static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500330 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400331{
Quinn Tranf83adb62014-04-11 16:54:43 -0400332 ql_dbg(ql_dbg_tgt, vha, 0xe072,
333 "%s: qla_target(%d): type %x ox_id %04x\n",
334 __func__, vha->vp_idx, atio->u.raw.entry_type,
335 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
336
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400337 switch (atio->u.raw.entry_type) {
338 case ATIO_TYPE7:
339 {
340 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
341 atio->u.isp24.fcp_hdr.d_id);
342 if (unlikely(NULL == host)) {
343 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
344 "qla_target(%d): Received ATIO_TYPE7 "
345 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
346 atio->u.isp24.fcp_hdr.d_id[0],
347 atio->u.isp24.fcp_hdr.d_id[1],
348 atio->u.isp24.fcp_hdr.d_id[2]);
Quinn Tran41dc5292017-01-19 22:28:03 -0800349
350
351 qlt_queue_unknown_atio(vha, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400352 break;
353 }
Quinn Tran41dc5292017-01-19 22:28:03 -0800354 if (unlikely(!list_empty(&vha->unknown_atio_list)))
355 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
356
Quinn Tran2f424b92015-12-17 14:57:07 -0500357 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400358 break;
359 }
360
361 case IMMED_NOTIFY_TYPE:
362 {
363 struct scsi_qla_host *host = vha;
364 struct imm_ntfy_from_isp *entry =
365 (struct imm_ntfy_from_isp *)atio;
366
367 if ((entry->u.isp24.vp_index != 0xFF) &&
368 (entry->u.isp24.nport_handle != 0xFFFF)) {
369 host = qlt_find_host_by_vp_idx(vha,
370 entry->u.isp24.vp_index);
371 if (unlikely(!host)) {
372 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
373 "qla_target(%d): Received "
374 "ATIO (IMMED_NOTIFY_TYPE) "
375 "with unknown vp_index %d\n",
376 vha->vp_idx, entry->u.isp24.vp_index);
377 break;
378 }
379 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500380 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400381 break;
382 }
383
Quinn Tran41dc5292017-01-19 22:28:03 -0800384 case VP_RPT_ID_IOCB_TYPE:
385 qla24xx_report_id_acquisition(vha,
386 (struct vp_rpt_id_entry_24xx *)atio);
387 break;
388
389 case ABTS_RECV_24XX:
390 {
391 struct abts_recv_from_24xx *entry =
392 (struct abts_recv_from_24xx *)atio;
393 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
394 entry->vp_index);
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700395 unsigned long flags;
396
Quinn Tran41dc5292017-01-19 22:28:03 -0800397 if (unlikely(!host)) {
398 ql_dbg(ql_dbg_tgt, vha, 0xffff,
399 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
400 "received, with unknown vp_index %d\n",
401 vha->vp_idx, entry->vp_index);
402 break;
403 }
Quinn Tran8f6fc8d2017-03-15 09:48:46 -0700404 if (!ha_locked)
405 spin_lock_irqsave(&host->hw->hardware_lock, flags);
406 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
407 if (!ha_locked)
408 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
Quinn Tran41dc5292017-01-19 22:28:03 -0800409 break;
Quinn Tran41dc5292017-01-19 22:28:03 -0800410 }
411
412 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
413
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400414 default:
415 ql_dbg(ql_dbg_tgt, vha, 0xe040,
416 "qla_target(%d): Received unknown ATIO atio "
417 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
418 break;
419 }
420
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500421 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400422}
423
424void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
425{
426 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400427 case CTIO_CRC2:
428 ql_dbg(ql_dbg_tgt, vha, 0xe073,
429 "qla_target(%d):%s: CRC2 Response pkt\n",
430 vha->vp_idx, __func__);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400431 case CTIO_TYPE7:
432 {
433 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
434 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
435 entry->vp_index);
436 if (unlikely(!host)) {
437 ql_dbg(ql_dbg_tgt, vha, 0xe041,
438 "qla_target(%d): Response pkt (CTIO_TYPE7) "
439 "received, with unknown vp_index %d\n",
440 vha->vp_idx, entry->vp_index);
441 break;
442 }
443 qlt_response_pkt(host, pkt);
444 break;
445 }
446
447 case IMMED_NOTIFY_TYPE:
448 {
449 struct scsi_qla_host *host = vha;
450 struct imm_ntfy_from_isp *entry =
451 (struct imm_ntfy_from_isp *)pkt;
452
453 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
454 if (unlikely(!host)) {
455 ql_dbg(ql_dbg_tgt, vha, 0xe042,
456 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
457 "received, with unknown vp_index %d\n",
458 vha->vp_idx, entry->u.isp24.vp_index);
459 break;
460 }
461 qlt_response_pkt(host, pkt);
462 break;
463 }
464
465 case NOTIFY_ACK_TYPE:
466 {
467 struct scsi_qla_host *host = vha;
468 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
469
470 if (0xFF != entry->u.isp24.vp_index) {
471 host = qlt_find_host_by_vp_idx(vha,
472 entry->u.isp24.vp_index);
473 if (unlikely(!host)) {
474 ql_dbg(ql_dbg_tgt, vha, 0xe043,
475 "qla_target(%d): Response "
476 "pkt (NOTIFY_ACK_TYPE) "
477 "received, with unknown "
478 "vp_index %d\n", vha->vp_idx,
479 entry->u.isp24.vp_index);
480 break;
481 }
482 }
483 qlt_response_pkt(host, pkt);
484 break;
485 }
486
487 case ABTS_RECV_24XX:
488 {
489 struct abts_recv_from_24xx *entry =
490 (struct abts_recv_from_24xx *)pkt;
491 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
492 entry->vp_index);
493 if (unlikely(!host)) {
494 ql_dbg(ql_dbg_tgt, vha, 0xe044,
495 "qla_target(%d): Response pkt "
496 "(ABTS_RECV_24XX) received, with unknown "
497 "vp_index %d\n", vha->vp_idx, entry->vp_index);
498 break;
499 }
500 qlt_response_pkt(host, pkt);
501 break;
502 }
503
504 case ABTS_RESP_24XX:
505 {
506 struct abts_resp_to_24xx *entry =
507 (struct abts_resp_to_24xx *)pkt;
508 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
509 entry->vp_index);
510 if (unlikely(!host)) {
511 ql_dbg(ql_dbg_tgt, vha, 0xe045,
512 "qla_target(%d): Response pkt "
513 "(ABTS_RECV_24XX) received, with unknown "
514 "vp_index %d\n", vha->vp_idx, entry->vp_index);
515 break;
516 }
517 qlt_response_pkt(host, pkt);
518 break;
519 }
520
521 default:
522 qlt_response_pkt(vha, pkt);
523 break;
524 }
525
526}
527
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500528/*
529 * All qlt_plogi_ack_t operations are protected by hardware_lock
530 */
Quinn Tran726b8542017-01-19 22:28:00 -0800531static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
532 struct imm_ntfy_from_isp *ntfy, int type)
533{
534 struct qla_work_evt *e;
535 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
536 if (!e)
537 return QLA_FUNCTION_FAILED;
538
539 e->u.nack.fcport = fcport;
540 e->u.nack.type = type;
541 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
542 return qla2x00_post_work(vha, e);
543}
544
545static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800546void qla2x00_async_nack_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800547{
Quinn Tran726b8542017-01-19 22:28:00 -0800548 struct srb *sp = (struct srb *)s;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800549 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800550 unsigned long flags;
551
552 ql_dbg(ql_dbg_disc, vha, 0xffff,
553 "Async done-%s res %x %8phC type %d\n",
554 sp->name, res, sp->fcport->port_name, sp->type);
555
556 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
557 sp->fcport->flags &= ~FCF_ASYNC_SENT;
558 sp->fcport->chip_reset = vha->hw->chip_reset;
559
560 switch (sp->type) {
561 case SRB_NACK_PLOGI:
562 sp->fcport->login_gen++;
563 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
564 sp->fcport->logout_on_delete = 1;
Quinn Tran5b334692017-03-15 09:48:48 -0700565 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
Quinn Tran726b8542017-01-19 22:28:00 -0800566 break;
567
568 case SRB_NACK_PRLI:
569 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
570 sp->fcport->deleted = 0;
571
572 if (!sp->fcport->login_succ &&
573 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
574 sp->fcport->login_succ = 1;
575
576 vha->fcport_count++;
577
578 if (!IS_IIDMA_CAPABLE(vha->hw) ||
579 !vha->hw->flags.gpsc_supported) {
580 ql_dbg(ql_dbg_disc, vha, 0xffff,
581 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
582 __func__, __LINE__,
583 sp->fcport->port_name,
584 vha->fcport_count);
585
586 qla24xx_post_upd_fcport_work(vha, sp->fcport);
587 } else {
588 ql_dbg(ql_dbg_disc, vha, 0xffff,
589 "%s %d %8phC post gpsc fcp_cnt %d\n",
590 __func__, __LINE__,
591 sp->fcport->port_name,
592 vha->fcport_count);
593
594 qla24xx_post_gpsc_work(vha, sp->fcport);
595 }
596 }
597 break;
598
599 case SRB_NACK_LOGO:
600 sp->fcport->login_gen++;
601 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
602 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
603 break;
604 }
605 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
606
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800607 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800608}
609
610int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
611 struct imm_ntfy_from_isp *ntfy, int type)
612{
613 int rval = QLA_FUNCTION_FAILED;
614 srb_t *sp;
615 char *c = NULL;
616
617 fcport->flags |= FCF_ASYNC_SENT;
618 switch (type) {
619 case SRB_NACK_PLOGI:
620 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
621 c = "PLOGI";
622 break;
623 case SRB_NACK_PRLI:
624 fcport->fw_login_state = DSC_LS_PRLI_PEND;
625 c = "PRLI";
626 break;
627 case SRB_NACK_LOGO:
628 fcport->fw_login_state = DSC_LS_LOGO_PEND;
629 c = "LOGO";
630 break;
631 }
632
633 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
634 if (!sp)
635 goto done;
636
637 sp->type = type;
638 sp->name = "nack";
639
640 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
641
642 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
643
644 sp->done = qla2x00_async_nack_sp_done;
645
646 rval = qla2x00_start_sp(sp);
647 if (rval != QLA_SUCCESS)
648 goto done_free_sp;
649
650 ql_dbg(ql_dbg_disc, vha, 0xffff,
651 "Async-%s %8phC hndl %x %s\n",
652 sp->name, fcport->port_name, sp->handle, c);
653
654 return rval;
655
656done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800657 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800658done:
659 fcport->flags &= ~FCF_ASYNC_SENT;
660 return rval;
661}
662
663void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
664{
665 fc_port_t *t;
666 unsigned long flags;
667
668 switch (e->u.nack.type) {
669 case SRB_NACK_PRLI:
670 mutex_lock(&vha->vha_tgt.tgt_mutex);
671 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
672 mutex_unlock(&vha->vha_tgt.tgt_mutex);
673 if (t) {
674 ql_log(ql_log_info, vha, 0xffff,
675 "%s create sess success %p", __func__, t);
676 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
677 /* create sess has an extra kref */
678 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
679 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
680 }
681 break;
682 }
683 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
684 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
685}
686
687void qla24xx_delete_sess_fn(struct work_struct *work)
688{
689 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
690 struct qla_hw_data *ha = fcport->vha->hw;
691 unsigned long flags;
692
693 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
694
695 if (fcport->se_sess) {
696 ha->tgt.tgt_ops->shutdown_sess(fcport);
697 ha->tgt.tgt_ops->put_sess(fcport);
698 } else {
699 qlt_unreg_sess(fcport);
700 }
701 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
702}
703
704/*
705 * Called from qla2x00_reg_remote_port()
706 */
707void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
708{
709 struct qla_hw_data *ha = vha->hw;
710 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
711 struct fc_port *sess = fcport;
712 unsigned long flags;
713
714 if (!vha->hw->tgt.tgt_ops)
715 return;
716
717 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
718 if (tgt->tgt_stop) {
719 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
720 return;
721 }
722
723 if (fcport->disc_state == DSC_DELETE_PEND) {
724 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
725 return;
726 }
727
728 if (!sess->se_sess) {
729 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
730
731 mutex_lock(&vha->vha_tgt.tgt_mutex);
732 sess = qlt_create_sess(vha, fcport, false);
733 mutex_unlock(&vha->vha_tgt.tgt_mutex);
734
735 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
736 } else {
737 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
738 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
739 return;
740 }
741
742 if (!kref_get_unless_zero(&sess->sess_kref)) {
743 ql_dbg(ql_dbg_disc, vha, 0xffff,
744 "%s: kref_get fail sess %8phC \n",
745 __func__, sess->port_name);
746 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
747 return;
748 }
749
750 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
751 "qla_target(%u): %ssession for port %8phC "
752 "(loop ID %d) reappeared\n", vha->vp_idx,
753 sess->local ? "local " : "", sess->port_name, sess->loop_id);
754
755 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
756 "Reappeared sess %p\n", sess);
757
758 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
759 fcport->loop_id,
760 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
761 }
762
763 if (sess && sess->local) {
764 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
765 "qla_target(%u): local session for "
766 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
767 fcport->port_name, sess->loop_id);
768 sess->local = 0;
769 }
770 ha->tgt.tgt_ops->put_sess(sess);
771 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
772}
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500773
774/*
775 * This is a zero-base ref-counting solution, since hardware_lock
776 * guarantees that ref_count is not modified concurrently.
777 * Upon successful return content of iocb is undefined
778 */
Quinn Tran5d964832017-01-19 22:27:59 -0800779static struct qlt_plogi_ack_t *
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500780qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
781 struct imm_ntfy_from_isp *iocb)
782{
Quinn Tran5d964832017-01-19 22:27:59 -0800783 struct qlt_plogi_ack_t *pla;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500784
785 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
786 if (pla->id.b24 == id->b24) {
787 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
Quinn Tran5d964832017-01-19 22:27:59 -0800788 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500789 return pla;
790 }
791 }
792
793 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
794 if (!pla) {
795 ql_dbg(ql_dbg_async, vha, 0x5088,
796 "qla_target(%d): Allocation of plogi_ack failed\n",
797 vha->vp_idx);
798 return NULL;
799 }
800
Quinn Tran5d964832017-01-19 22:27:59 -0800801 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500802 pla->id = *id;
803 list_add_tail(&pla->list, &vha->plogi_ack_list);
804
805 return pla;
806}
807
Quinn Tran726b8542017-01-19 22:28:00 -0800808void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -0800809 struct qlt_plogi_ack_t *pla)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500810{
Quinn Tran5d964832017-01-19 22:27:59 -0800811 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Quinn Tran726b8542017-01-19 22:28:00 -0800812 port_id_t port_id;
813 uint16_t loop_id;
814 fc_port_t *fcport = pla->fcport;
815
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500816 BUG_ON(!pla->ref_count);
817 pla->ref_count--;
818
819 if (pla->ref_count)
820 return;
821
Quinn Tran726b8542017-01-19 22:28:00 -0800822 ql_dbg(ql_dbg_disc, vha, 0x5089,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500823 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
Quinn Tran5d964832017-01-19 22:27:59 -0800824 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
825 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
826 iocb->u.isp24.port_id[0],
827 le16_to_cpu(iocb->u.isp24.nport_handle),
828 iocb->u.isp24.exchange_address, iocb->ox_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800829
830 port_id.b.domain = iocb->u.isp24.port_id[2];
831 port_id.b.area = iocb->u.isp24.port_id[1];
832 port_id.b.al_pa = iocb->u.isp24.port_id[0];
833 port_id.b.rsvd_1 = 0;
834
835 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
836
837 fcport->loop_id = loop_id;
838 fcport->d_id = port_id;
839 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
840
841 list_for_each_entry(fcport, &vha->vp_fcports, list) {
842 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
843 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
844 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
845 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
846 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500847
848 list_del(&pla->list);
849 kmem_cache_free(qla_tgt_plogi_cachep, pla);
850}
851
Quinn Tran726b8542017-01-19 22:28:00 -0800852void
Quinn Tran5d964832017-01-19 22:27:59 -0800853qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
854 struct fc_port *sess, enum qlt_plogi_link_t link)
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500855{
Quinn Tran5d964832017-01-19 22:27:59 -0800856 struct imm_ntfy_from_isp *iocb = &pla->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500857 /* Inc ref_count first because link might already be pointing at pla */
858 pla->ref_count++;
859
Quinn Tran726b8542017-01-19 22:28:00 -0800860 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
861 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
862 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
863 sess, link, sess->port_name,
864 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
865 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
866 pla->ref_count, pla, link);
867
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500868 if (sess->plogi_link[link])
869 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
870
Quinn Tran726b8542017-01-19 22:28:00 -0800871 if (link == QLT_PLOGI_LINK_SAME_WWN)
872 pla->fcport = sess;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500873
874 sess->plogi_link[link] = pla;
875}
876
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500877typedef struct {
878 /* These fields must be initialized by the caller */
879 port_id_t id;
880 /*
881 * number of cmds dropped while we were waiting for
882 * initiator to ack LOGO initialize to 1 if LOGO is
883 * triggered by a command, otherwise, to 0
884 */
885 int cmd_count;
886
887 /* These fields are used by callee */
888 struct list_head list;
889} qlt_port_logo_t;
890
891static void
892qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
893{
894 qlt_port_logo_t *tmp;
895 int res;
896
897 mutex_lock(&vha->vha_tgt.tgt_mutex);
898
899 list_for_each_entry(tmp, &vha->logo_list, list) {
900 if (tmp->id.b24 == logo->id.b24) {
901 tmp->cmd_count += logo->cmd_count;
902 mutex_unlock(&vha->vha_tgt.tgt_mutex);
903 return;
904 }
905 }
906
907 list_add_tail(&logo->list, &vha->logo_list);
908
909 mutex_unlock(&vha->vha_tgt.tgt_mutex);
910
911 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
912
913 mutex_lock(&vha->vha_tgt.tgt_mutex);
914 list_del(&logo->list);
915 mutex_unlock(&vha->vha_tgt.tgt_mutex);
916
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500917 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
918 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
919 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
920 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500921}
922
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400923static void qlt_free_session_done(struct work_struct *work)
924{
Quinn Tran5d964832017-01-19 22:27:59 -0800925 struct fc_port *sess = container_of(work, struct fc_port,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400926 free_work);
927 struct qla_tgt *tgt = sess->tgt;
928 struct scsi_qla_host *vha = sess->vha;
929 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400930 unsigned long flags;
931 bool logout_started = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800932 struct event_arg ea;
Quinn Tran41dc5292017-01-19 22:28:03 -0800933 scsi_qla_host_t *base_vha;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400934
935 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
936 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500937 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400938 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
Quinn Tran37cacc02017-01-19 22:27:58 -0800939 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400940 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500941 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400942
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400943
Quinn Tran726b8542017-01-19 22:28:00 -0800944 if (!IS_SW_RESV_ADDR(sess->d_id)) {
Quinn Tran41dc5292017-01-19 22:28:03 -0800945 if (sess->send_els_logo) {
946 qlt_port_logo_t logo;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500947
Quinn Tran41dc5292017-01-19 22:28:03 -0800948 logo.id = sess->d_id;
949 logo.cmd_count = 0;
950 qlt_send_first_logo(vha, &logo);
951 }
952
953 if (sess->logout_on_delete) {
954 int rc;
955
956 rc = qla2x00_post_async_logout_work(vha, sess, NULL);
957 if (rc != QLA_SUCCESS)
958 ql_log(ql_log_warn, vha, 0xf085,
959 "Schedule logo failed sess %p rc %d\n",
960 sess, rc);
961 else
962 logout_started = true;
963 }
Quinn Tran726b8542017-01-19 22:28:00 -0800964 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400965
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400966 /*
967 * Release the target session for FC Nexus from fabric module code.
968 */
969 if (sess->se_sess != NULL)
970 ha->tgt.tgt_ops->free_session(sess);
971
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400972 if (logout_started) {
973 bool traced = false;
974
975 while (!ACCESS_ONCE(sess->logout_completed)) {
976 if (!traced) {
977 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
978 "%s: waiting for sess %p logout\n",
979 __func__, sess);
980 traced = true;
981 }
982 msleep(100);
983 }
984
Quinn Tran726b8542017-01-19 22:28:00 -0800985 ql_dbg(ql_dbg_disc, vha, 0xf087,
Quinn Tran41dc5292017-01-19 22:28:03 -0800986 "%s: sess %p logout completed\n",__func__, sess);
987 }
988
989 if (sess->logo_ack_needed) {
990 sess->logo_ack_needed = 0;
991 qla24xx_async_notify_ack(vha, sess,
992 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400993 }
994
Quinn Tran726b8542017-01-19 22:28:00 -0800995 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
996 if (sess->se_sess) {
997 sess->se_sess = NULL;
998 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
999 tgt->sess_count--;
1000 }
1001
1002 sess->disc_state = DSC_DELETED;
1003 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1004 sess->deleted = QLA_SESS_DELETED;
1005 sess->login_retry = vha->hw->login_retry_count;
1006
1007 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1008 vha->fcport_count--;
1009 sess->login_succ = 0;
1010 }
1011
1012 if (sess->chip_reset != sess->vha->hw->chip_reset)
1013 qla2x00_clear_loop_id(sess);
1014
1015 if (sess->conflict) {
1016 sess->conflict->login_pause = 0;
1017 sess->conflict = NULL;
1018 if (!test_bit(UNLOADING, &vha->dpc_flags))
1019 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1020 }
1021
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001022 {
Quinn Tran5d964832017-01-19 22:27:59 -08001023 struct qlt_plogi_ack_t *own =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001024 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
Quinn Tran5d964832017-01-19 22:27:59 -08001025 struct qlt_plogi_ack_t *con =
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001026 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
Quinn Tran5d964832017-01-19 22:27:59 -08001027 struct imm_ntfy_from_isp *iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001028
1029 if (con) {
Quinn Tran5d964832017-01-19 22:27:59 -08001030 iocb = &con->iocb;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001031 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
Quinn Tran5d964832017-01-19 22:27:59 -08001032 "se_sess %p / sess %p port %8phC is gone,"
1033 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1034 sess->se_sess, sess, sess->port_name,
1035 own ? "releasing own PLOGI" : "no own PLOGI pending",
1036 own ? own->ref_count : -1,
1037 iocb->u.isp24.port_name, con->ref_count);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001038 qlt_plogi_ack_unref(vha, con);
Quinn Tran726b8542017-01-19 22:28:00 -08001039 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001040 } else {
1041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1042 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1043 sess->se_sess, sess, sess->port_name,
1044 own ? "releasing own PLOGI" :
1045 "no own PLOGI pending",
1046 own ? own->ref_count : -1);
1047 }
1048
Quinn Tran726b8542017-01-19 22:28:00 -08001049 if (own) {
1050 sess->fw_login_state = DSC_LS_PLOGI_PEND;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001051 qlt_plogi_ack_unref(vha, own);
Quinn Tran726b8542017-01-19 22:28:00 -08001052 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1053 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05001054 }
Quinn Tran5d964832017-01-19 22:27:59 -08001055 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1056
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
Quinn Tran726b8542017-01-19 22:28:00 -08001058 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1059 sess, sess->port_name, vha->fcport_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001060
Quinn Tran726b8542017-01-19 22:28:00 -08001061 if (tgt && (tgt->sess_count == 0))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001062 wake_up_all(&tgt->waitQ);
Quinn Tran726b8542017-01-19 22:28:00 -08001063
1064 if (vha->fcport_count == 0)
1065 wake_up_all(&vha->fcport_waitQ);
1066
Quinn Tran41dc5292017-01-19 22:28:03 -08001067 base_vha = pci_get_drvdata(ha->pdev);
1068 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1069 return;
1070
Quinn Tran726b8542017-01-19 22:28:00 -08001071 if (!tgt || !tgt->tgt_stop) {
1072 memset(&ea, 0, sizeof(ea));
1073 ea.event = FCME_DELETE_DONE;
1074 ea.fcport = sess;
1075 qla2x00_fcport_event_handler(vha, &ea);
1076 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001077}
1078
Quinn Tran75601512015-12-17 14:57:04 -05001079/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08001080void qlt_unreg_sess(struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001081{
1082 struct scsi_qla_host *vha = sess->vha;
1083
Quinn Tran5d964832017-01-19 22:27:59 -08001084 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1085 "%s sess %p for deletion %8phC\n",
1086 __func__, sess, sess->port_name);
1087
Quinn Tran36c78452016-02-04 11:45:18 -05001088 if (sess->se_sess)
1089 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001090
Quinn Tran5d964832017-01-19 22:27:59 -08001091 qla2x00_mark_device_lost(vha, sess, 1, 1);
1092
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001093 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Quinn Tran726b8542017-01-19 22:28:00 -08001094 sess->disc_state = DSC_DELETE_PEND;
1095 sess->last_rscn_gen = sess->rscn_gen;
1096 sess->last_login_gen = sess->login_gen;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001097
1098 INIT_WORK(&sess->free_work, qlt_free_session_done);
1099 schedule_work(&sess->free_work);
1100}
Quinn Tran5d964832017-01-19 22:27:59 -08001101EXPORT_SYMBOL(qlt_unreg_sess);
Quinn Tran75601512015-12-17 14:57:04 -05001102
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001103static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1104{
1105 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001106 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001107 uint16_t loop_id;
1108 int res = 0;
1109 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -05001110 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001111
1112 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1113 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001114 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -04001115 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -05001116 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -04001117 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001118 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001119 } else {
Quinn Tran75601512015-12-17 14:57:04 -05001120 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001121 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05001122 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001123 }
1124
1125 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1126 "Using sess for qla_tgt_reset: %p\n", sess);
1127 if (!sess) {
1128 res = -ESRCH;
1129 return res;
1130 }
1131
1132 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001133 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1134 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001135 mcmd, loop_id);
1136
Quinn Tranbb1181c2016-12-23 18:06:05 -08001137 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001138}
1139
Quinn Tran726b8542017-01-19 22:28:00 -08001140static void qla24xx_chk_fcp_state(struct fc_port *sess)
1141{
1142 if (sess->chip_reset != sess->vha->hw->chip_reset) {
1143 sess->logout_on_delete = 0;
1144 sess->logo_ack_needed = 0;
1145 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1146 sess->scan_state = 0;
1147 }
1148}
1149
Quinn Tran75601512015-12-17 14:57:04 -05001150/* ha->tgt.sess_lock supposed to be held on entry */
Quinn Tran726b8542017-01-19 22:28:00 -08001151void qlt_schedule_sess_for_deletion(struct fc_port *sess,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001152 bool immediate)
1153{
1154 struct qla_tgt *tgt = sess->tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001155
Quinn Tran726b8542017-01-19 22:28:00 -08001156 if (sess->disc_state == DSC_DELETE_PEND)
1157 return;
1158
1159 if (sess->disc_state == DSC_DELETED) {
1160 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1161 wake_up_all(&tgt->waitQ);
1162 if (sess->vha->fcport_count == 0)
1163 wake_up_all(&sess->vha->fcport_waitQ);
1164
1165 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1166 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001167 return;
1168 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001169
Quinn Tran726b8542017-01-19 22:28:00 -08001170 sess->disc_state = DSC_DELETE_PEND;
1171
1172 if (sess->deleted == QLA_SESS_DELETED)
1173 sess->logout_on_delete = 0;
1174
1175 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1176 qla24xx_chk_fcp_state(sess);
1177
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001178 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1179 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001180
Quinn Tran726b8542017-01-19 22:28:00 -08001181 schedule_work(&sess->del_work);
1182}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001183
Quinn Tran726b8542017-01-19 22:28:00 -08001184void qlt_schedule_sess_for_deletion_lock(struct fc_port *sess)
1185{
1186 unsigned long flags;
1187 struct qla_hw_data *ha = sess->vha->hw;
1188 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1189 qlt_schedule_sess_for_deletion(sess, 1);
1190 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001191}
1192
Quinn Tran75601512015-12-17 14:57:04 -05001193/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -04001194static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001195{
Quinn Tran5d964832017-01-19 22:27:59 -08001196 struct fc_port *sess;
1197 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001198
Quinn Tran5d964832017-01-19 22:27:59 -08001199 list_for_each_entry(sess, &vha->vp_fcports, list) {
1200 if (sess->se_sess)
Quinn Tran726b8542017-01-19 22:28:00 -08001201 qlt_schedule_sess_for_deletion(sess, 1);
Quinn Tran5d964832017-01-19 22:27:59 -08001202 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001203
1204 /* At this point tgt could be already dead */
1205}
1206
1207static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1208 uint16_t *loop_id)
1209{
1210 struct qla_hw_data *ha = vha->hw;
1211 dma_addr_t gid_list_dma;
1212 struct gid_list_info *gid_list;
1213 char *id_iter;
1214 int res, rc, i;
1215 uint16_t entries;
1216
1217 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1218 &gid_list_dma, GFP_KERNEL);
1219 if (!gid_list) {
1220 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1221 "qla_target(%d): DMA Alloc failed of %u\n",
1222 vha->vp_idx, qla2x00_gid_list_size(ha));
1223 return -ENOMEM;
1224 }
1225
1226 /* Get list of logged in devices */
1227 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
1228 if (rc != QLA_SUCCESS) {
1229 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1230 "qla_target(%d): get_id_list() failed: %x\n",
1231 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001232 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001233 goto out_free_id_list;
1234 }
1235
1236 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05001237 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001238 for (i = 0; i < entries; i++) {
1239 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1240 if ((gid->al_pa == s_id[2]) &&
1241 (gid->area == s_id[1]) &&
1242 (gid->domain == s_id[0])) {
1243 *loop_id = le16_to_cpu(gid->loop_id);
1244 res = 0;
1245 break;
1246 }
1247 id_iter += ha->gid_list_info_size;
1248 }
1249
1250out_free_id_list:
1251 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1252 gid_list, gid_list_dma);
1253 return res;
1254}
1255
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001256/*
1257 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1258 * Caller must put it.
1259 */
Quinn Tran5d964832017-01-19 22:27:59 -08001260static struct fc_port *qlt_create_sess(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001261 struct scsi_qla_host *vha,
1262 fc_port_t *fcport,
1263 bool local)
1264{
1265 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -08001266 struct fc_port *sess = fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001267 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001268
Quinn Tran726b8542017-01-19 22:28:00 -08001269 if (vha->vha_tgt.qla_tgt->tgt_stop)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001270 return NULL;
Quinn Tran726b8542017-01-19 22:28:00 -08001271
1272 if (fcport->se_sess) {
1273 if (!kref_get_unless_zero(&sess->sess_kref)) {
1274 ql_dbg(ql_dbg_disc, vha, 0xffff,
1275 "%s: kref_get_unless_zero failed for %8phC\n",
1276 __func__, sess->port_name);
1277 return NULL;
1278 }
1279 return fcport;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001280 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001281 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001282 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001283
Quinn Tran726b8542017-01-19 22:28:00 -08001284 /*
1285 * Under normal circumstances we want to logout from firmware when
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001286 * session eventually ends and release corresponding nport handle.
1287 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
Quinn Tran726b8542017-01-19 22:28:00 -08001288 * code will adjust these flags as necessary.
1289 */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001290 sess->logout_on_delete = 1;
1291 sess->keep_nport_handle = 0;
Quinn Tran726b8542017-01-19 22:28:00 -08001292 sess->logout_completed = 0;
1293
1294 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1295 &fcport->port_name[0], sess) < 0) {
1296 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
1297 "(%d) %8phC check_initiator_node_acl failed\n",
1298 vha->vp_idx, fcport->port_name);
1299 return NULL;
1300 } else {
1301 kref_init(&fcport->sess_kref);
1302 /*
1303 * Take an extra reference to ->sess_kref here to handle
1304 * fc_port access across ->tgt.sess_lock reaquire.
1305 */
1306 if (!kref_get_unless_zero(&sess->sess_kref)) {
1307 ql_dbg(ql_dbg_disc, vha, 0xffff,
1308 "%s: kref_get_unless_zero failed for %8phC\n",
1309 __func__, sess->port_name);
1310 return NULL;
1311 }
1312
1313 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1314 if (!IS_SW_RESV_ADDR(sess->d_id))
1315 vha->vha_tgt.qla_tgt->sess_count++;
1316
1317 qlt_do_generation_tick(vha, &sess->generation);
1318 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1319 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001320
1321 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
Quinn Tran726b8542017-01-19 22:28:00 -08001322 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1323 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1324 vha->vha_tgt.qla_tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001325
1326 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001327 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1328 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1329 vha->vp_idx, local ? "local " : "", fcport->port_name,
Quinn Tran37cacc02017-01-19 22:27:58 -08001330 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1331 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001332
1333 return sess;
1334}
1335
1336/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001337 * max_gen - specifies maximum session generation
1338 * at which this deletion requestion is still valid
1339 */
1340void
1341qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001342{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001343 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08001344 struct fc_port *sess = fcport;
Quinn Tran75601512015-12-17 14:57:04 -05001345 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001346
1347 if (!vha->hw->tgt.tgt_ops)
1348 return;
1349
Roland Dreierb2032fd2015-07-14 16:00:42 -04001350 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001351 return;
1352
Quinn Tran75601512015-12-17 14:57:04 -05001353 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001354 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001355 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001356 return;
1357 }
Quinn Tran5d964832017-01-19 22:27:59 -08001358 if (!sess->se_sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001359 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001360 return;
1361 }
1362
Alexei Potashnikdf673272015-07-14 16:00:46 -04001363 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001364 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001365 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1366 "Ignoring stale deletion request for se_sess %p / sess %p"
1367 " for port %8phC, req_gen %d, sess_gen %d\n",
1368 sess->se_sess, sess, sess->port_name, max_gen,
1369 sess->generation);
1370 return;
1371 }
1372
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001373 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1374
1375 sess->local = 1;
1376 qlt_schedule_sess_for_deletion(sess, false);
Quinn Tran75601512015-12-17 14:57:04 -05001377 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001378}
1379
1380static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1381{
1382 struct qla_hw_data *ha = tgt->ha;
1383 unsigned long flags;
1384 int res;
1385 /*
1386 * We need to protect against race, when tgt is freed before or
1387 * inside wake_up()
1388 */
Quinn Tran726b8542017-01-19 22:28:00 -08001389 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001390 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
Quinn Tran5d964832017-01-19 22:27:59 -08001391 "tgt %p, sess_count=%d\n",
1392 tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001393 res = (tgt->sess_count == 0);
Quinn Tran726b8542017-01-19 22:28:00 -08001394 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001395
1396 return res;
1397}
1398
1399/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001400int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001401{
1402 struct scsi_qla_host *vha = tgt->vha;
1403 struct qla_hw_data *ha = tgt->ha;
1404 unsigned long flags;
1405
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001406 mutex_lock(&qla_tgt_mutex);
1407 if (!vha->fc_vport) {
1408 struct Scsi_Host *sh = vha->host;
1409 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1410 bool npiv_vports;
1411
1412 spin_lock_irqsave(sh->host_lock, flags);
1413 npiv_vports = (fc_host->npiv_vports_inuse);
1414 spin_unlock_irqrestore(sh->host_lock, flags);
1415
1416 if (npiv_vports) {
1417 mutex_unlock(&qla_tgt_mutex);
1418 return -EPERM;
1419 }
1420 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001421 if (tgt->tgt_stop || tgt->tgt_stopped) {
1422 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1423 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001424 mutex_unlock(&qla_tgt_mutex);
1425 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001426 }
1427
1428 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1429 vha->host_no, vha);
1430 /*
1431 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1432 * Lock is needed, because we still can get an incoming packet.
1433 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001434 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001435 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001436 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001437 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001438 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001439 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001440 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001441
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001442 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1443 "Waiting for sess works (tgt %p)", tgt);
1444 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1445 while (!list_empty(&tgt->sess_works_list)) {
1446 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1447 flush_scheduled_work();
1448 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1449 }
1450 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1451
1452 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
Quinn Tran5d964832017-01-19 22:27:59 -08001453 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001454
1455 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1456
1457 /* Big hammer */
Quinn Tranead03852017-01-19 22:28:01 -08001458 if (!ha->flags.host_shutting_down &&
1459 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001460 qlt_disable_vha(vha);
1461
1462 /* Wait for sessions to clear out (just in case) */
1463 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001464 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001465}
1466EXPORT_SYMBOL(qlt_stop_phase1);
1467
1468/* Called by tcm_qla2xxx configfs code */
1469void qlt_stop_phase2(struct qla_tgt *tgt)
1470{
1471 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001472 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001473 unsigned long flags;
1474
1475 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001476 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001477 "Already in tgt->tgt_stopped state\n");
1478 dump_stack();
1479 return;
1480 }
1481
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001482 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001483 "Waiting for %d IRQ commands to complete (tgt %p)",
1484 tgt->irq_cmd_count, tgt);
1485
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001486 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001487 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05001488 while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001489 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1490 udelay(2);
1491 spin_lock_irqsave(&ha->hardware_lock, flags);
1492 }
1493 tgt->tgt_stop = 0;
1494 tgt->tgt_stopped = 1;
1495 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001496 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001497
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001498 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001499 tgt);
1500}
1501EXPORT_SYMBOL(qlt_stop_phase2);
1502
1503/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001504static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001505{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001506 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001507
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001508 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001509 qlt_stop_phase2(tgt);
1510
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001511 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001512
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001513 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001514 "Release of tgt %p finished\n", tgt);
1515
1516 kfree(tgt);
1517}
1518
1519/* ha->hardware_lock supposed to be held on entry */
1520static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1521 const void *param, unsigned int param_size)
1522{
1523 struct qla_tgt_sess_work_param *prm;
1524 unsigned long flags;
1525
1526 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1527 if (!prm) {
1528 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1529 "qla_target(%d): Unable to create session "
1530 "work, command will be refused", 0);
1531 return -ENOMEM;
1532 }
1533
1534 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1535 "Scheduling work (type %d, prm %p)"
1536 " to find session for param %p (size %d, tgt %p)\n",
1537 type, prm, param, param_size, tgt);
1538
1539 prm->type = type;
1540 memcpy(&prm->tm_iocb, param, param_size);
1541
1542 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1543 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1544 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1545
1546 schedule_work(&tgt->sess_work);
1547
1548 return 0;
1549}
1550
1551/*
1552 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1553 */
1554static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1555 struct imm_ntfy_from_isp *ntfy,
1556 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1557 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1558{
1559 struct qla_hw_data *ha = vha->hw;
1560 request_t *pkt;
1561 struct nack_to_isp *nack;
1562
1563 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1564
1565 /* Send marker if required */
1566 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1567 return;
1568
1569 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1570 if (!pkt) {
1571 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1572 "qla_target(%d): %s failed: unable to allocate "
1573 "request packet\n", vha->vp_idx, __func__);
1574 return;
1575 }
1576
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001577 if (vha->vha_tgt.qla_tgt != NULL)
1578 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001579
1580 pkt->entry_type = NOTIFY_ACK_TYPE;
1581 pkt->entry_count = 1;
1582
1583 nack = (struct nack_to_isp *)pkt;
1584 nack->ox_id = ntfy->ox_id;
1585
Quinn Tran726b8542017-01-19 22:28:00 -08001586 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001587 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1588 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1589 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001590 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001591 }
1592 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1593 nack->u.isp24.status = ntfy->u.isp24.status;
1594 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001595 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001596 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1597 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1598 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1599 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1600 nack->u.isp24.srr_reject_code = srr_reject_code;
1601 nack->u.isp24.srr_reject_code_expl = srr_explan;
1602 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1603
1604 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1605 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1606 vha->vp_idx, nack->u.isp24.status);
1607
Himanshu Madhani63163e02014-09-25 06:14:59 -04001608 /* Memory Barrier */
1609 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001610 qla2x00_start_iocbs(vha, vha->req);
1611}
1612
1613/*
1614 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1615 */
1616static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1617 struct abts_recv_from_24xx *abts, uint32_t status,
1618 bool ids_reversed)
1619{
1620 struct qla_hw_data *ha = vha->hw;
1621 struct abts_resp_to_24xx *resp;
1622 uint32_t f_ctl;
1623 uint8_t *p;
1624
1625 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1626 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1627 ha, abts, status);
1628
1629 /* Send marker if required */
1630 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1631 return;
1632
Arun Easib6a029e2014-09-25 06:14:52 -04001633 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001634 if (!resp) {
1635 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1636 "qla_target(%d): %s failed: unable to allocate "
1637 "request packet", vha->vp_idx, __func__);
1638 return;
1639 }
1640
1641 resp->entry_type = ABTS_RESP_24XX;
1642 resp->entry_count = 1;
1643 resp->nport_handle = abts->nport_handle;
1644 resp->vp_index = vha->vp_idx;
1645 resp->sof_type = abts->sof_type;
1646 resp->exchange_address = abts->exchange_address;
1647 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001648 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001649 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1650 F_CTL_SEQ_INITIATIVE);
1651 p = (uint8_t *)&f_ctl;
1652 resp->fcp_hdr_le.f_ctl[0] = *p++;
1653 resp->fcp_hdr_le.f_ctl[1] = *p++;
1654 resp->fcp_hdr_le.f_ctl[2] = *p;
1655 if (ids_reversed) {
1656 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1657 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1658 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1659 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1660 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1661 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1662 } else {
1663 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1664 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1665 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1666 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1667 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1668 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1669 }
1670 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1671 if (status == FCP_TMF_CMPL) {
1672 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1673 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1674 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1675 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1676 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1677 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1678 } else {
1679 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1680 resp->payload.ba_rjt.reason_code =
1681 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1682 /* Other bytes are zero */
1683 }
1684
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001685 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001686
Himanshu Madhani63163e02014-09-25 06:14:59 -04001687 /* Memory Barrier */
1688 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001689 qla2x00_start_iocbs(vha, vha->req);
1690}
1691
1692/*
1693 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1694 */
1695static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1696 struct abts_resp_from_24xx_fw *entry)
1697{
1698 struct ctio7_to_24xx *ctio;
1699
1700 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1701 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1702 /* Send marker if required */
1703 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1704 return;
1705
Arun Easib6a029e2014-09-25 06:14:52 -04001706 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001707 if (ctio == NULL) {
1708 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1709 "qla_target(%d): %s failed: unable to allocate "
1710 "request packet\n", vha->vp_idx, __func__);
1711 return;
1712 }
1713
1714 /*
1715 * We've got on entrance firmware's response on by us generated
1716 * ABTS response. So, in it ID fields are reversed.
1717 */
1718
1719 ctio->entry_type = CTIO_TYPE7;
1720 ctio->entry_count = 1;
1721 ctio->nport_handle = entry->nport_handle;
1722 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001723 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001724 ctio->vp_index = vha->vp_idx;
1725 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1726 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1727 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1728 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001729 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1730 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001731 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001732
Himanshu Madhani63163e02014-09-25 06:14:59 -04001733 /* Memory Barrier */
1734 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001735 qla2x00_start_iocbs(vha, vha->req);
1736
1737 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1738 FCP_TMF_CMPL, true);
1739}
1740
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001741static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1742{
1743 struct qla_tgt_sess_op *op;
1744 struct qla_tgt_cmd *cmd;
1745
1746 spin_lock(&vha->cmd_list_lock);
1747
1748 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1749 if (tag == op->atio.u.isp24.exchange_addr) {
1750 op->aborted = true;
1751 spin_unlock(&vha->cmd_list_lock);
1752 return 1;
1753 }
1754 }
1755
Quinn Tran41dc5292017-01-19 22:28:03 -08001756 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1757 if (tag == op->atio.u.isp24.exchange_addr) {
1758 op->aborted = true;
1759 spin_unlock(&vha->cmd_list_lock);
1760 return 1;
1761 }
1762 }
1763
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001764 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1765 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001766 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001767 spin_unlock(&vha->cmd_list_lock);
1768 return 1;
1769 }
1770 }
1771
1772 spin_unlock(&vha->cmd_list_lock);
1773 return 0;
1774}
1775
1776/* drop cmds for the given lun
1777 * XXX only looks for cmds on the port through which lun reset was recieved
1778 * XXX does not go through the list of other port (which may have cmds
1779 * for the same lun)
1780 */
1781static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1782 uint32_t lun, uint8_t *s_id)
1783{
1784 struct qla_tgt_sess_op *op;
1785 struct qla_tgt_cmd *cmd;
1786 uint32_t key;
1787
1788 key = sid_to_key(s_id);
1789 spin_lock(&vha->cmd_list_lock);
1790 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1791 uint32_t op_key;
1792 uint32_t op_lun;
1793
1794 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1795 op_lun = scsilun_to_int(
1796 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1797 if (op_key == key && op_lun == lun)
1798 op->aborted = true;
1799 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001800
1801 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1802 uint32_t op_key;
1803 u64 op_lun;
1804
1805 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1806 op_lun = scsilun_to_int(
1807 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1808 if (op_key == key && op_lun == lun)
1809 op->aborted = true;
1810 }
1811
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001812 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1813 uint32_t cmd_key;
1814 uint32_t cmd_lun;
1815
1816 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1817 cmd_lun = scsilun_to_int(
1818 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1819 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001820 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001821 }
1822 spin_unlock(&vha->cmd_list_lock);
1823}
1824
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001825/* ha->hardware_lock supposed to be held on entry */
1826static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08001827 struct abts_recv_from_24xx *abts, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001828{
1829 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001830 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001831 struct qla_tgt_mgmt_cmd *mcmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001832 struct se_cmd *se_cmd;
1833 u32 lun = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001834 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001835 bool found_lun = false;
Quinn Tran5d964832017-01-19 22:27:59 -08001836 unsigned long flags;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001837
Quinn Tran5d964832017-01-19 22:27:59 -08001838 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
Steve Hodgson06e97b42012-11-16 08:06:17 -08001839 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1840 struct qla_tgt_cmd *cmd =
1841 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
Bart Van Assche649ee052015-04-14 13:26:44 +02001842 if (se_cmd->tag == abts->exchange_addr_to_abort) {
Steve Hodgson06e97b42012-11-16 08:06:17 -08001843 lun = cmd->unpacked_lun;
1844 found_lun = true;
1845 break;
1846 }
1847 }
Quinn Tran5d964832017-01-19 22:27:59 -08001848 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Steve Hodgson06e97b42012-11-16 08:06:17 -08001849
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001850 /* cmd not in LIO lists, look in qla list */
1851 if (!found_lun) {
1852 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1853 /* send TASK_ABORT response immediately */
1854 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1855 return 0;
1856 } else {
1857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1858 "unable to find cmd in driver or LIO for tag 0x%x\n",
1859 abts->exchange_addr_to_abort);
1860 return -ENOENT;
1861 }
1862 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001863
1864 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1865 "qla_target(%d): task abort (tag=%d)\n",
1866 vha->vp_idx, abts->exchange_addr_to_abort);
1867
1868 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1869 if (mcmd == NULL) {
1870 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1871 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1872 vha->vp_idx, __func__);
1873 return -ENOMEM;
1874 }
1875 memset(mcmd, 0, sizeof(*mcmd));
1876
1877 mcmd->sess = sess;
1878 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Arun Easi80187f82014-09-25 06:14:53 -04001879 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001880 mcmd->tmr_func = QLA_TGT_ABTS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001881
Quinn Tranbe92fc32017-01-19 22:27:54 -08001882 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001883 abts->exchange_addr_to_abort);
1884 if (rc != 0) {
1885 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1886 "qla_target(%d): tgt_ops->handle_tmr()"
1887 " failed: %d", vha->vp_idx, rc);
1888 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1889 return -EFAULT;
1890 }
1891
1892 return 0;
1893}
1894
1895/*
1896 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1897 */
1898static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1899 struct abts_recv_from_24xx *abts)
1900{
1901 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08001902 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001903 uint32_t tag = abts->exchange_addr_to_abort;
1904 uint8_t s_id[3];
1905 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001906 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001907
1908 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1909 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1910 "qla_target(%d): ABTS: Abort Sequence not "
1911 "supported\n", vha->vp_idx);
1912 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1913 return;
1914 }
1915
1916 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1917 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1918 "qla_target(%d): ABTS: Unknown Exchange "
1919 "Address received\n", vha->vp_idx);
1920 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1921 return;
1922 }
1923
1924 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1925 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1926 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1927 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1928 le32_to_cpu(abts->fcp_hdr_le.parameter));
1929
1930 s_id[0] = abts->fcp_hdr_le.s_id[2];
1931 s_id[1] = abts->fcp_hdr_le.s_id[1];
1932 s_id[2] = abts->fcp_hdr_le.s_id[0];
1933
Quinn Tran75601512015-12-17 14:57:04 -05001934 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001935 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1936 if (!sess) {
1937 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1938 "qla_target(%d): task abort for non-existant session\n",
1939 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001940 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001941 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001942
1943 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1944
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001945 if (rc != 0) {
1946 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1947 false);
1948 }
1949 return;
1950 }
Quinn Tran75601512015-12-17 14:57:04 -05001951 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1952
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001953
Quinn Tran726b8542017-01-19 22:28:00 -08001954 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001955 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1956 return;
1957 }
1958
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001959 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1960 if (rc != 0) {
1961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1962 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1963 vha->vp_idx, rc);
1964 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1965 return;
1966 }
1967}
1968
1969/*
1970 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1971 */
1972static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1973 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1974{
1975 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1976 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001977 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001978
1979 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1980 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1981 ha, atio, resp_code);
1982
1983 /* Send marker if required */
1984 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1985 return;
1986
1987 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1988 if (ctio == NULL) {
1989 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1990 "qla_target(%d): %s failed: unable to allocate "
1991 "request packet\n", ha->vp_idx, __func__);
1992 return;
1993 }
1994
1995 ctio->entry_type = CTIO_TYPE7;
1996 ctio->entry_count = 1;
1997 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1998 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07001999 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002000 ctio->vp_index = ha->vp_idx;
2001 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2002 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2003 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2004 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2005 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07002006 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002007 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2008 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002009 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07002010 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2011 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07002012 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002013
Himanshu Madhani63163e02014-09-25 06:14:59 -04002014 /* Memory Barrier */
2015 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002016 qla2x00_start_iocbs(ha, ha->req);
2017}
2018
2019void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2020{
2021 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2022}
2023EXPORT_SYMBOL(qlt_free_mcmd);
2024
2025/* callback from target fabric module code */
2026void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2027{
2028 struct scsi_qla_host *vha = mcmd->sess->vha;
2029 struct qla_hw_data *ha = vha->hw;
2030 unsigned long flags;
2031
2032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2033 "TM response mcmd (%p) status %#x state %#x",
2034 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2035
2036 spin_lock_irqsave(&ha->hardware_lock, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04002037
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002038 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002039 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002040 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002041 * previous life, just abort the processing.
2042 */
2043 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002044 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2045 vha->flags.online, qla2x00_reset_active(vha),
2046 mcmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002047 ha->tgt.tgt_ops->free_mcmd(mcmd);
2048 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2049 return;
2050 }
2051
Quinn Tran726b8542017-01-19 22:28:00 -08002052 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2053 if (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
Quinn Tran41dc5292017-01-19 22:28:03 -08002054 ELS_LOGO ||
2055 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2056 ELS_PRLO ||
2057 mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode ==
2058 ELS_TPRLO) {
Quinn Tran726b8542017-01-19 22:28:00 -08002059 ql_dbg(ql_dbg_disc, vha, 0xffff,
2060 "TM response logo %phC status %#x state %#x",
2061 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2062 mcmd->flags);
2063 qlt_schedule_sess_for_deletion_lock(mcmd->sess);
2064 } else {
2065 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
2066 0, 0, 0, 0, 0, 0);
2067 }
2068 } else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05002069 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002070 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
2071 mcmd->fc_tm_rsp, false);
2072 else
2073 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
2074 mcmd->fc_tm_rsp);
2075 }
2076 /*
2077 * Make the callback for ->free_mcmd() to queue_work() and invoke
2078 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2079 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2080 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2081 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2082 * qlt_xmit_tm_rsp() returns here..
2083 */
2084 ha->tgt.tgt_ops->free_mcmd(mcmd);
2085 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2086}
2087EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2088
2089/* No locks */
2090static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2091{
2092 struct qla_tgt_cmd *cmd = prm->cmd;
2093
2094 BUG_ON(cmd->sg_cnt == 0);
2095
2096 prm->sg = (struct scatterlist *)cmd->sg;
2097 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
2098 cmd->sg_cnt, cmd->dma_data_direction);
2099 if (unlikely(prm->seg_cnt == 0))
2100 goto out_err;
2101
2102 prm->cmd->sg_mapped = 1;
2103
Quinn Tranf83adb62014-04-11 16:54:43 -04002104 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2105 /*
2106 * If greater than four sg entries then we need to allocate
2107 * the continuation entries
2108 */
2109 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
2110 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2111 prm->tgt->datasegs_per_cmd,
2112 prm->tgt->datasegs_per_cont);
2113 } else {
2114 /* DIF */
2115 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2116 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2117 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2118 prm->tot_dsds = prm->seg_cnt;
2119 } else
2120 prm->tot_dsds = prm->seg_cnt;
2121
2122 if (cmd->prot_sg_cnt) {
2123 prm->prot_sg = cmd->prot_sg;
2124 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
2125 cmd->prot_sg, cmd->prot_sg_cnt,
2126 cmd->dma_data_direction);
2127 if (unlikely(prm->prot_seg_cnt == 0))
2128 goto out_err;
2129
2130 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2131 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2132 /* Dif Bundling not support here */
2133 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2134 cmd->blk_sz);
2135 prm->tot_dsds += prm->prot_seg_cnt;
2136 } else
2137 prm->tot_dsds += prm->prot_seg_cnt;
2138 }
2139 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002140
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002141 return 0;
2142
2143out_err:
2144 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
2145 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2146 0, prm->cmd->sg_cnt);
2147 return -1;
2148}
2149
Joern Engelf9b67212014-09-16 16:23:18 -04002150static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002151{
2152 struct qla_hw_data *ha = vha->hw;
2153
Joern Engelf9b67212014-09-16 16:23:18 -04002154 if (!cmd->sg_mapped)
2155 return;
2156
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002157 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
2158 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002159
2160 if (cmd->prot_sg_cnt)
2161 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
2162 cmd->dma_data_direction);
2163
2164 if (cmd->ctx_dsd_alloced)
2165 qla2x00_clean_dsd_pool(ha, NULL, cmd);
2166
2167 if (cmd->ctx)
2168 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002169}
2170
2171static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
2172 uint32_t req_cnt)
2173{
Saurav Kashyapd29fb732014-09-25 06:14:49 -04002174 uint32_t cnt, cnt_in;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002175
2176 if (vha->req->cnt < (req_cnt + 2)) {
Arun Easi75554b62014-09-25 06:14:45 -04002177 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
Saurav Kashyapd29fb732014-09-25 06:14:49 -04002178 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002179
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002180 if (vha->req->ring_index < cnt)
2181 vha->req->cnt = cnt - vha->req->ring_index;
2182 else
2183 vha->req->cnt = vha->req->length -
2184 (vha->req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01002185
2186 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
2187 ql_dbg(ql_dbg_io, vha, 0x305a,
2188 "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",
2189 vha->vp_idx, vha->req->ring_index,
2190 vha->req->cnt, req_cnt, cnt, cnt_in,
2191 vha->req->length);
2192 return -EAGAIN;
2193 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002194 }
2195
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002196 vha->req->cnt -= req_cnt;
2197
2198 return 0;
2199}
2200
2201/*
2202 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2203 */
2204static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
2205{
2206 /* Adjust ring index. */
2207 vha->req->ring_index++;
2208 if (vha->req->ring_index == vha->req->length) {
2209 vha->req->ring_index = 0;
2210 vha->req->ring_ptr = vha->req->ring;
2211 } else {
2212 vha->req->ring_ptr++;
2213 }
2214 return (cont_entry_t *)vha->req->ring_ptr;
2215}
2216
2217/* ha->hardware_lock supposed to be held on entry */
2218static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
2219{
2220 struct qla_hw_data *ha = vha->hw;
2221 uint32_t h;
2222
2223 h = ha->tgt.current_handle;
2224 /* always increment cmd handle */
2225 do {
2226 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002227 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002228 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
2229 if (h == ha->tgt.current_handle) {
Arun Easi667024a2014-09-25 06:14:47 -04002230 ql_dbg(ql_dbg_io, vha, 0x305b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002231 "qla_target(%d): Ran out of "
2232 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
2233 h = QLA_TGT_NULL_HANDLE;
2234 break;
2235 }
2236 } while ((h == QLA_TGT_NULL_HANDLE) ||
2237 (h == QLA_TGT_SKIP_HANDLE) ||
2238 (ha->tgt.cmds[h-1] != NULL));
2239
2240 if (h != QLA_TGT_NULL_HANDLE)
2241 ha->tgt.current_handle = h;
2242
2243 return h;
2244}
2245
2246/* ha->hardware_lock supposed to be held on entry */
2247static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
2248 struct scsi_qla_host *vha)
2249{
2250 uint32_t h;
2251 struct ctio7_to_24xx *pkt;
2252 struct qla_hw_data *ha = vha->hw;
2253 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002254 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002255
2256 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
2257 prm->pkt = pkt;
2258 memset(pkt, 0, sizeof(*pkt));
2259
2260 pkt->entry_type = CTIO_TYPE7;
2261 pkt->entry_count = (uint8_t)prm->req_cnt;
2262 pkt->vp_index = vha->vp_idx;
2263
2264 h = qlt_make_handle(vha);
2265 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2266 /*
2267 * CTIO type 7 from the firmware doesn't provide a way to
2268 * know the initiator's LOOP ID, hence we can't find
2269 * the session and, so, the command.
2270 */
2271 return -EAGAIN;
2272 } else
2273 ha->tgt.cmds[h-1] = prm->cmd;
2274
2275 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2276 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002277 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002278 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2279 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2280 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2281 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2282 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002283 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2284 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002285 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2286
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002287 return 0;
2288}
2289
2290/*
2291 * ha->hardware_lock supposed to be held on entry. We have already made sure
2292 * that there is sufficient amount of request entries to not drop it.
2293 */
2294static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
2295 struct scsi_qla_host *vha)
2296{
2297 int cnt;
2298 uint32_t *dword_ptr;
2299 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2300
2301 /* Build continuation packets */
2302 while (prm->seg_cnt > 0) {
2303 cont_a64_entry_t *cont_pkt64 =
2304 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2305
2306 /*
2307 * Make sure that from cont_pkt64 none of
2308 * 64-bit specific fields used for 32-bit
2309 * addressing. Cast to (cont_entry_t *) for
2310 * that.
2311 */
2312
2313 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2314
2315 cont_pkt64->entry_count = 1;
2316 cont_pkt64->sys_define = 0;
2317
2318 if (enable_64bit_addressing) {
2319 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2320 dword_ptr =
2321 (uint32_t *)&cont_pkt64->dseg_0_address;
2322 } else {
2323 cont_pkt64->entry_type = CONTINUE_TYPE;
2324 dword_ptr =
2325 (uint32_t *)&((cont_entry_t *)
2326 cont_pkt64)->dseg_0_address;
2327 }
2328
2329 /* Load continuation entry data segments */
2330 for (cnt = 0;
2331 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2332 cnt++, prm->seg_cnt--) {
2333 *dword_ptr++ =
2334 cpu_to_le32(pci_dma_lo32
2335 (sg_dma_address(prm->sg)));
2336 if (enable_64bit_addressing) {
2337 *dword_ptr++ =
2338 cpu_to_le32(pci_dma_hi32
2339 (sg_dma_address
2340 (prm->sg)));
2341 }
2342 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2343
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002344 prm->sg = sg_next(prm->sg);
2345 }
2346 }
2347}
2348
2349/*
2350 * ha->hardware_lock supposed to be held on entry. We have already made sure
2351 * that there is sufficient amount of request entries to not drop it.
2352 */
2353static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2354 struct scsi_qla_host *vha)
2355{
2356 int cnt;
2357 uint32_t *dword_ptr;
2358 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2359 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2360
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002361 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2362
2363 /* Setup packet address segment pointer */
2364 dword_ptr = pkt24->u.status0.dseg_0_address;
2365
2366 /* Set total data segment count */
2367 if (prm->seg_cnt)
2368 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2369
2370 if (prm->seg_cnt == 0) {
2371 /* No data transfer */
2372 *dword_ptr++ = 0;
2373 *dword_ptr = 0;
2374 return;
2375 }
2376
2377 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002378
2379 /* Load command entry data segments */
2380 for (cnt = 0;
2381 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2382 cnt++, prm->seg_cnt--) {
2383 *dword_ptr++ =
2384 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2385 if (enable_64bit_addressing) {
2386 *dword_ptr++ =
2387 cpu_to_le32(pci_dma_hi32(
2388 sg_dma_address(prm->sg)));
2389 }
2390 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2391
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002392 prm->sg = sg_next(prm->sg);
2393 }
2394
2395 qlt_load_cont_data_segments(prm, vha);
2396}
2397
2398static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2399{
2400 return cmd->bufflen > 0;
2401}
2402
2403/*
2404 * Called without ha->hardware_lock held
2405 */
2406static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2407 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2408 uint32_t *full_req_cnt)
2409{
2410 struct qla_tgt *tgt = cmd->tgt;
2411 struct scsi_qla_host *vha = tgt->vha;
2412 struct qla_hw_data *ha = vha->hw;
2413 struct se_cmd *se_cmd = &cmd->se_cmd;
2414
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002415 prm->cmd = cmd;
2416 prm->tgt = tgt;
2417 prm->rq_result = scsi_status;
2418 prm->sense_buffer = &cmd->sense_buffer[0];
2419 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2420 prm->sg = NULL;
2421 prm->seg_cnt = -1;
2422 prm->req_cnt = 1;
2423 prm->add_status_pkt = 0;
2424
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002425 /* Send marker if required */
2426 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2427 return -EFAULT;
2428
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002429 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2430 if (qlt_pci_map_calc_cnt(prm) != 0)
2431 return -EAGAIN;
2432 }
2433
2434 *full_req_cnt = prm->req_cnt;
2435
2436 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2437 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002438 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002439 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2440 prm->residual, se_cmd->tag,
2441 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2442 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002443 prm->rq_result |= SS_RESIDUAL_UNDER;
2444 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2445 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002446 ql_dbg(ql_dbg_io, vha, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002447 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2448 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2449 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002450 prm->rq_result |= SS_RESIDUAL_OVER;
2451 }
2452
2453 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2454 /*
2455 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2456 * ignored in *xmit_response() below
2457 */
2458 if (qlt_has_data(cmd)) {
2459 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2460 (IS_FWI2_CAPABLE(ha) &&
2461 (prm->rq_result != 0))) {
2462 prm->add_status_pkt = 1;
2463 (*full_req_cnt)++;
2464 }
2465 }
2466 }
2467
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002468 return 0;
2469}
2470
2471static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2472 struct qla_tgt_cmd *cmd, int sending_sense)
2473{
2474 if (ha->tgt.enable_class_2)
2475 return 0;
2476
2477 if (sending_sense)
2478 return cmd->conf_compl_supported;
2479 else
2480 return ha->tgt.enable_explicit_conf &&
2481 cmd->conf_compl_supported;
2482}
2483
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002484static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2485 struct qla_tgt_prm *prm)
2486{
2487 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2488 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002489 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002490 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002491 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002492 CTIO7_FLAGS_EXPLICIT_CONFORM |
2493 CTIO7_FLAGS_CONFORM_REQ);
2494 }
2495 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2496 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2497 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2498 int i;
2499
2500 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002501 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002502 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2503 "Skipping EXPLICIT_CONFORM and "
2504 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2505 "non GOOD status\n");
2506 goto skip_explict_conf;
2507 }
Bart Van Asschead950362015-07-09 07:24:08 -07002508 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002509 CTIO7_FLAGS_EXPLICIT_CONFORM |
2510 CTIO7_FLAGS_CONFORM_REQ);
2511 }
2512skip_explict_conf:
2513 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002514 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002515 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002516 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002517 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002518 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002519 ctio->u.status1.sense_length =
2520 cpu_to_le16(prm->sense_buffer_len);
2521 for (i = 0; i < prm->sense_buffer_len/4; i++)
2522 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2523 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2524#if 0
2525 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
2526 static int q;
2527 if (q < 10) {
2528 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
2529 "qla_target(%d): %d bytes of sense "
2530 "lost", prm->tgt->ha->vp_idx,
2531 prm->sense_buffer_len % 4);
2532 q++;
2533 }
2534 }
2535#endif
2536 } else {
2537 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002538 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002539 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002540 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002541 ctio->u.status1.sense_length = 0;
2542 memset(ctio->u.status1.sense_data, 0,
2543 sizeof(ctio->u.status1.sense_data));
2544 }
2545
2546 /* Sense with len > 24, is it possible ??? */
2547}
2548
Quinn Tranf83adb62014-04-11 16:54:43 -04002549
2550
2551/* diff */
2552static inline int
2553qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2554{
2555 /*
2556 * Uncomment when corresponding SCSI changes are done.
2557 *
2558 if (!sp->cmd->prot_chk)
2559 return 0;
2560 *
2561 */
2562 switch (se_cmd->prot_op) {
2563 case TARGET_PROT_DOUT_INSERT:
2564 case TARGET_PROT_DIN_STRIP:
2565 if (ql2xenablehba_err_chk >= 1)
2566 return 1;
2567 break;
2568 case TARGET_PROT_DOUT_PASS:
2569 case TARGET_PROT_DIN_PASS:
2570 if (ql2xenablehba_err_chk >= 2)
2571 return 1;
2572 break;
2573 case TARGET_PROT_DIN_INSERT:
2574 case TARGET_PROT_DOUT_STRIP:
2575 return 1;
2576 default:
2577 break;
2578 }
2579 return 0;
2580}
2581
2582/*
2583 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
2584 *
2585 */
2586static inline void
2587qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
2588{
2589 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2590
2591 /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
2592 * have been immplemented by TCM, before AppTag is avail.
2593 * Look for modesense_handlers[]
2594 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002595 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002596 ctx->app_tag_mask[0] = 0x0;
2597 ctx->app_tag_mask[1] = 0x0;
2598
2599 switch (se_cmd->prot_type) {
2600 case TARGET_DIF_TYPE0_PROT:
2601 /*
2602 * No check for ql2xenablehba_err_chk, as it would be an
2603 * I/O error if hba tag generation is not done.
2604 */
2605 ctx->ref_tag = cpu_to_le32(lba);
2606
2607 if (!qlt_hba_err_chk_enabled(se_cmd))
2608 break;
2609
2610 /* enable ALL bytes of the ref tag */
2611 ctx->ref_tag_mask[0] = 0xff;
2612 ctx->ref_tag_mask[1] = 0xff;
2613 ctx->ref_tag_mask[2] = 0xff;
2614 ctx->ref_tag_mask[3] = 0xff;
2615 break;
2616 /*
2617 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
2618 * 16 bit app tag.
2619 */
2620 case TARGET_DIF_TYPE1_PROT:
2621 ctx->ref_tag = cpu_to_le32(lba);
2622
2623 if (!qlt_hba_err_chk_enabled(se_cmd))
2624 break;
2625
2626 /* enable ALL bytes of the ref tag */
2627 ctx->ref_tag_mask[0] = 0xff;
2628 ctx->ref_tag_mask[1] = 0xff;
2629 ctx->ref_tag_mask[2] = 0xff;
2630 ctx->ref_tag_mask[3] = 0xff;
2631 break;
2632 /*
2633 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
2634 * match LBA in CDB + N
2635 */
2636 case TARGET_DIF_TYPE2_PROT:
2637 ctx->ref_tag = cpu_to_le32(lba);
2638
2639 if (!qlt_hba_err_chk_enabled(se_cmd))
2640 break;
2641
2642 /* enable ALL bytes of the ref tag */
2643 ctx->ref_tag_mask[0] = 0xff;
2644 ctx->ref_tag_mask[1] = 0xff;
2645 ctx->ref_tag_mask[2] = 0xff;
2646 ctx->ref_tag_mask[3] = 0xff;
2647 break;
2648
2649 /* For Type 3 protection: 16 bit GUARD only */
2650 case TARGET_DIF_TYPE3_PROT:
2651 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2652 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2653 break;
2654 }
2655}
2656
2657
2658static inline int
2659qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2660{
2661 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002662 uint32_t transfer_length = 0;
2663 uint32_t data_bytes;
2664 uint32_t dif_bytes;
2665 uint8_t bundling = 1;
2666 uint8_t *clr_ptr;
2667 struct crc_context *crc_ctx_pkt = NULL;
2668 struct qla_hw_data *ha;
2669 struct ctio_crc2_to_fw *pkt;
2670 dma_addr_t crc_ctx_dma;
2671 uint16_t fw_prot_opts = 0;
2672 struct qla_tgt_cmd *cmd = prm->cmd;
2673 struct se_cmd *se_cmd = &cmd->se_cmd;
2674 uint32_t h;
2675 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002676 uint16_t t16;
Quinn Tranf83adb62014-04-11 16:54:43 -04002677
Quinn Tranf83adb62014-04-11 16:54:43 -04002678 ha = vha->hw;
2679
2680 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2681 prm->pkt = pkt;
2682 memset(pkt, 0, sizeof(*pkt));
2683
2684 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2685 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2686 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2687 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2688
2689 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2690 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2691 bundling = 0;
2692
2693 /* Compute dif len and adjust data len to incude protection */
2694 data_bytes = cmd->bufflen;
2695 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2696
2697 switch (se_cmd->prot_op) {
2698 case TARGET_PROT_DIN_INSERT:
2699 case TARGET_PROT_DOUT_STRIP:
2700 transfer_length = data_bytes;
2701 data_bytes += dif_bytes;
2702 break;
2703
2704 case TARGET_PROT_DIN_STRIP:
2705 case TARGET_PROT_DOUT_INSERT:
2706 case TARGET_PROT_DIN_PASS:
2707 case TARGET_PROT_DOUT_PASS:
2708 transfer_length = data_bytes + dif_bytes;
2709 break;
2710
2711 default:
2712 BUG();
2713 break;
2714 }
2715
2716 if (!qlt_hba_err_chk_enabled(se_cmd))
2717 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2718 /* HBA error checking enabled */
2719 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2720 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2721 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2722 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2723 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2724 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2725 }
2726
2727 switch (se_cmd->prot_op) {
2728 case TARGET_PROT_DIN_INSERT:
2729 case TARGET_PROT_DOUT_INSERT:
2730 fw_prot_opts |= PO_MODE_DIF_INSERT;
2731 break;
2732 case TARGET_PROT_DIN_STRIP:
2733 case TARGET_PROT_DOUT_STRIP:
2734 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2735 break;
2736 case TARGET_PROT_DIN_PASS:
2737 case TARGET_PROT_DOUT_PASS:
2738 fw_prot_opts |= PO_MODE_DIF_PASS;
2739 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2740 break;
2741 default:/* Normal Request */
2742 fw_prot_opts |= PO_MODE_DIF_PASS;
2743 break;
2744 }
2745
2746
2747 /* ---- PKT ---- */
2748 /* Update entry type to indicate Command Type CRC_2 IOCB */
2749 pkt->entry_type = CTIO_CRC2;
2750 pkt->entry_count = 1;
2751 pkt->vp_index = vha->vp_idx;
2752
2753 h = qlt_make_handle(vha);
2754 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2755 /*
2756 * CTIO type 7 from the firmware doesn't provide a way to
2757 * know the initiator's LOOP ID, hence we can't find
2758 * the session and, so, the command.
2759 */
2760 return -EAGAIN;
2761 } else
2762 ha->tgt.cmds[h-1] = prm->cmd;
2763
2764
2765 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2766 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002767 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002768 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2769 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2770 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2771 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002772
2773 /* silence compile warning */
2774 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2775 pkt->ox_id = cpu_to_le16(t16);
2776
2777 t16 = (atio->u.isp24.attr << 9);
2778 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002779 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2780
2781 /* Set transfer direction */
2782 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002783 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002784 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002785 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002786
2787
2788 pkt->dseg_count = prm->tot_dsds;
2789 /* Fibre channel byte count */
2790 pkt->transfer_length = cpu_to_le32(transfer_length);
2791
2792
2793 /* ----- CRC context -------- */
2794
2795 /* Allocate CRC context from global pool */
2796 crc_ctx_pkt = cmd->ctx =
2797 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2798
2799 if (!crc_ctx_pkt)
2800 goto crc_queuing_error;
2801
2802 /* Zero out CTX area. */
2803 clr_ptr = (uint8_t *)crc_ctx_pkt;
2804 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2805
2806 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2807 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2808
2809 /* Set handle */
2810 crc_ctx_pkt->handle = pkt->handle;
2811
2812 qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
2813
2814 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2815 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2816 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2817
2818
2819 if (!bundling) {
2820 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2821 } else {
2822 /*
2823 * Configure Bundling if we need to fetch interlaving
2824 * protection PCI accesses
2825 */
2826 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2827 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2828 crc_ctx_pkt->u.bundling.dseg_count =
2829 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2830 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2831 }
2832
2833 /* Finish the common fields of CRC pkt */
2834 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2835 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2836 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002837 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002838
2839
2840 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07002841 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04002842
2843 if (!bundling && prm->prot_seg_cnt) {
2844 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2845 prm->tot_dsds, cmd))
2846 goto crc_queuing_error;
2847 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2848 (prm->tot_dsds - prm->prot_seg_cnt), cmd))
2849 goto crc_queuing_error;
2850
2851 if (bundling && prm->prot_seg_cnt) {
2852 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002853 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04002854
2855 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2856 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2857 prm->prot_seg_cnt, cmd))
2858 goto crc_queuing_error;
2859 }
2860 return QLA_SUCCESS;
2861
2862crc_queuing_error:
2863 /* Cleanup will be performed by the caller */
2864
2865 return QLA_FUNCTION_FAILED;
2866}
2867
2868
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002869/*
2870 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2871 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2872 */
2873int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2874 uint8_t scsi_status)
2875{
2876 struct scsi_qla_host *vha = cmd->vha;
2877 struct qla_hw_data *ha = vha->hw;
2878 struct ctio7_to_24xx *pkt;
2879 struct qla_tgt_prm prm;
2880 uint32_t full_req_cnt = 0;
2881 unsigned long flags = 0;
2882 int res;
2883
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002884 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08002885 if (cmd->sess && cmd->sess->deleted) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002886 cmd->state = QLA_TGT_STATE_PROCESSED;
2887 if (cmd->sess->logout_completed)
2888 /* no need to terminate. FW already freed exchange. */
2889 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2890 else
Quinn Trana07100e2015-12-07 19:48:57 -05002891 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002892 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2893 return 0;
2894 }
2895 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2896
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002897 memset(&prm, 0, sizeof(prm));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002898
2899 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
Quinn Tranf83adb62014-04-11 16:54:43 -04002900 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
2901 (xmit_type & QLA_TGT_XMIT_STATUS) ?
2902 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
2903 &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002904
2905 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2906 &full_req_cnt);
2907 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002908 return res;
2909 }
2910
2911 spin_lock_irqsave(&ha->hardware_lock, flags);
2912
Himanshu Madhanice1025c2015-12-17 14:56:58 -05002913 if (xmit_type == QLA_TGT_XMIT_STATUS)
2914 vha->tgt_counters.core_qla_snd_status++;
2915 else
2916 vha->tgt_counters.core_qla_que_buf++;
2917
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002918 if (!vha->flags.online || cmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002919 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002920 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002921 * previous life, just abort the processing.
2922 */
2923 cmd->state = QLA_TGT_STATE_PROCESSED;
2924 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2925 ql_dbg(ql_dbg_async, vha, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002926 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
2927 vha->flags.online, qla2x00_reset_active(vha),
2928 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002929 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2930 return 0;
2931 }
2932
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002933 /* Does F/W have an IOCBs for this request */
2934 res = qlt_check_reserve_free_req(vha, full_req_cnt);
2935 if (unlikely(res))
2936 goto out_unmap_unlock;
2937
Quinn Tranf83adb62014-04-11 16:54:43 -04002938 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
2939 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2940 else
2941 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Quinn Tran810e30b2015-06-10 11:05:20 -04002942 if (unlikely(res != 0)) {
2943 vha->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002944 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04002945 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002946
2947 pkt = (struct ctio7_to_24xx *)prm.pkt;
2948
2949 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2950 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002951 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002952 CTIO7_FLAGS_STATUS_MODE_0);
2953
Quinn Tranf83adb62014-04-11 16:54:43 -04002954 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2955 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002956
2957 if (prm.add_status_pkt == 0) {
2958 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2959 pkt->u.status0.scsi_status =
2960 cpu_to_le16(prm.rq_result);
2961 pkt->u.status0.residual =
2962 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07002963 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002964 CTIO7_FLAGS_SEND_STATUS);
2965 if (qlt_need_explicit_conf(ha, cmd, 0)) {
2966 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002967 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002968 CTIO7_FLAGS_EXPLICIT_CONFORM |
2969 CTIO7_FLAGS_CONFORM_REQ);
2970 }
2971 }
2972
2973 } else {
2974 /*
2975 * We have already made sure that there is sufficient
2976 * amount of request entries to not drop HW lock in
2977 * req_pkt().
2978 */
2979 struct ctio7_to_24xx *ctio =
2980 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2981
Arun Easi667024a2014-09-25 06:14:47 -04002982 ql_dbg(ql_dbg_io, vha, 0x305e,
2983 "Building additional status packet 0x%p.\n",
2984 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002985
Quinn Tranf83adb62014-04-11 16:54:43 -04002986 /*
2987 * T10Dif: ctio_crc2_to_fw overlay ontop of
2988 * ctio7_to_24xx
2989 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002990 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04002991 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002992 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04002993 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002994 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07002995 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002996 CTIO7_FLAGS_DATA_IN);
2997
2998 /* Real finish is ctio_m1's finish */
2999 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07003000 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003001 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04003002
3003 /* qlt_24xx_init_ctio_to_isp will correct
3004 * all neccessary fields that's part of CTIO7.
3005 * There should be no residual of CTIO-CRC2 data.
3006 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003007 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3008 &prm);
3009 pr_debug("Status CTIO7: %p\n", ctio);
3010 }
3011 } else
3012 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3013
3014
3015 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04003016 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003017
Himanshu Madhani63163e02014-09-25 06:14:59 -04003018 /* Memory Barrier */
3019 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003020 qla2x00_start_iocbs(vha, vha->req);
3021 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3022
3023 return 0;
3024
3025out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04003026 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003027 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3028
3029 return res;
3030}
3031EXPORT_SYMBOL(qlt_xmit_response);
3032
3033int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3034{
3035 struct ctio7_to_24xx *pkt;
3036 struct scsi_qla_host *vha = cmd->vha;
3037 struct qla_hw_data *ha = vha->hw;
3038 struct qla_tgt *tgt = cmd->tgt;
3039 struct qla_tgt_prm prm;
3040 unsigned long flags;
3041 int res = 0;
3042
3043 memset(&prm, 0, sizeof(prm));
3044 prm.cmd = cmd;
3045 prm.tgt = tgt;
3046 prm.sg = NULL;
3047 prm.req_cnt = 1;
3048
3049 /* Send marker if required */
3050 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
3051 return -EIO;
3052
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003053 /* Calculate number of entries and segments required */
3054 if (qlt_pci_map_calc_cnt(&prm) != 0)
3055 return -EAGAIN;
3056
3057 spin_lock_irqsave(&ha->hardware_lock, flags);
3058
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003059 if (!vha->flags.online || (cmd->reset_count != ha->chip_reset) ||
Quinn Tran726b8542017-01-19 22:28:00 -08003060 (cmd->sess && cmd->sess->deleted)) {
Arun Easib6a029e2014-09-25 06:14:52 -04003061 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003062 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04003063 * previous life, just abort the processing.
3064 */
3065 cmd->state = QLA_TGT_STATE_NEED_DATA;
3066 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3067 ql_dbg(ql_dbg_async, vha, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05003068 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3069 vha->flags.online, qla2x00_reset_active(vha),
3070 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04003071 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3072 return 0;
3073 }
3074
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003075 /* Does F/W have an IOCBs for this request */
3076 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
3077 if (res != 0)
3078 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04003079 if (cmd->se_cmd.prot_op)
3080 res = qlt_build_ctio_crc2_pkt(&prm, vha);
3081 else
3082 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003083
Quinn Tran810e30b2015-06-10 11:05:20 -04003084 if (unlikely(res != 0)) {
3085 vha->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003086 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04003087 }
3088
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003089 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07003090 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003091 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04003092
3093 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3094 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003095
3096 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04003097 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003098
Himanshu Madhani63163e02014-09-25 06:14:59 -04003099 /* Memory Barrier */
3100 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003101 qla2x00_start_iocbs(vha, vha->req);
3102 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3103
3104 return res;
3105
3106out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04003107 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003108 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3109
3110 return res;
3111}
3112EXPORT_SYMBOL(qlt_rdy_to_xfer);
3113
Quinn Tranf83adb62014-04-11 16:54:43 -04003114
3115/*
3116 * Checks the guard or meta-data for the type of error
3117 * detected by the HBA.
3118 */
3119static inline int
3120qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
3121 struct ctio_crc_from_fw *sts)
3122{
3123 uint8_t *ap = &sts->actual_dif[0];
3124 uint8_t *ep = &sts->expected_dif[0];
3125 uint32_t e_ref_tag, a_ref_tag;
3126 uint16_t e_app_tag, a_app_tag;
3127 uint16_t e_guard, a_guard;
3128 uint64_t lba = cmd->se_cmd.t_task_lba;
3129
3130 a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3131 a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3132 a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3133
3134 e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3135 e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3136 e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3137
3138 ql_dbg(ql_dbg_tgt, vha, 0xe075,
3139 "iocb(s) %p Returned STATUS.\n", sts);
3140
3141 ql_dbg(ql_dbg_tgt, vha, 0xf075,
Hans Wennborgfc385042014-08-05 21:43:29 -07003142 "dif check TGT cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x]\n",
Quinn Tranf83adb62014-04-11 16:54:43 -04003143 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3144 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
3145
3146 /*
3147 * Ignore sector if:
3148 * For type 3: ref & app tag is all 'f's
3149 * For type 0,1,2: app tag is all 'f's
3150 */
3151 if ((a_app_tag == 0xffff) &&
3152 ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
3153 (a_ref_tag == 0xffffffff))) {
3154 uint32_t blocks_done;
3155
3156 /* 2TB boundary case covered automatically with this */
3157 blocks_done = e_ref_tag - (uint32_t)lba + 1;
3158 cmd->se_cmd.bad_sector = e_ref_tag;
3159 cmd->se_cmd.pi_err = 0;
3160 ql_dbg(ql_dbg_tgt, vha, 0xf074,
3161 "need to return scsi good\n");
3162
3163 /* Update protection tag */
3164 if (cmd->prot_sg_cnt) {
Bart Van Assche52c82822015-07-09 07:23:26 -07003165 uint32_t i, k = 0, num_ent;
Quinn Tranf83adb62014-04-11 16:54:43 -04003166 struct scatterlist *sg, *sgl;
3167
3168
3169 sgl = cmd->prot_sg;
3170
3171 /* Patch the corresponding protection tags */
3172 for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
3173 num_ent = sg_dma_len(sg) / 8;
3174 if (k + num_ent < blocks_done) {
3175 k += num_ent;
3176 continue;
3177 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003178 k = blocks_done;
3179 break;
3180 }
3181
3182 if (k != blocks_done) {
3183 ql_log(ql_log_warn, vha, 0xf076,
3184 "unexpected tag values tag:lba=%u:%llu)\n",
3185 e_ref_tag, (unsigned long long)lba);
3186 goto out;
3187 }
3188
3189#if 0
3190 struct sd_dif_tuple *spt;
3191 /* TODO:
3192 * This section came from initiator. Is it valid here?
3193 * should ulp be override with actual val???
3194 */
3195 spt = page_address(sg_page(sg)) + sg->offset;
3196 spt += j;
3197
3198 spt->app_tag = 0xffff;
3199 if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
3200 spt->ref_tag = 0xffffffff;
3201#endif
3202 }
3203
3204 return 0;
3205 }
3206
3207 /* check guard */
3208 if (e_guard != a_guard) {
3209 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
3210 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3211
3212 ql_log(ql_log_warn, vha, 0xe076,
3213 "Guard ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
3214 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3215 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3216 a_guard, e_guard, cmd);
3217 goto out;
3218 }
3219
3220 /* check ref tag */
3221 if (e_ref_tag != a_ref_tag) {
3222 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
3223 cmd->se_cmd.bad_sector = e_ref_tag;
3224
3225 ql_log(ql_log_warn, vha, 0xe077,
3226 "Ref Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
3227 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3228 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3229 a_guard, e_guard, cmd);
3230 goto out;
3231 }
3232
3233 /* check appl tag */
3234 if (e_app_tag != a_app_tag) {
3235 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
3236 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3237
3238 ql_log(ql_log_warn, vha, 0xe078,
3239 "App Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
3240 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3241 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3242 a_guard, e_guard, cmd);
3243 goto out;
3244 }
3245out:
3246 return 1;
3247}
3248
3249
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003250/* If hardware_lock held on entry, might drop it, then reaquire */
3251/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003252static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3253 struct imm_ntfy_from_isp *ntfy)
3254{
3255 struct nack_to_isp *nack;
3256 struct qla_hw_data *ha = vha->hw;
3257 request_t *pkt;
3258 int ret = 0;
3259
3260 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3261 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3262
3263 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3264 if (pkt == NULL) {
3265 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3266 "qla_target(%d): %s failed: unable to allocate "
3267 "request packet\n", vha->vp_idx, __func__);
3268 return -ENOMEM;
3269 }
3270
3271 pkt->entry_type = NOTIFY_ACK_TYPE;
3272 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003273 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003274
3275 nack = (struct nack_to_isp *)pkt;
3276 nack->ox_id = ntfy->ox_id;
3277
3278 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3279 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3280 nack->u.isp24.flags = ntfy->u.isp24.flags &
3281 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3282 }
3283
3284 /* terminate */
3285 nack->u.isp24.flags |=
3286 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3287
3288 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3289 nack->u.isp24.status = ntfy->u.isp24.status;
3290 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3291 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3292 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3293 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3294 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3295 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3296
3297 qla2x00_start_iocbs(vha, vha->req);
3298 return ret;
3299}
3300
3301static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3302 struct imm_ntfy_from_isp *imm, int ha_locked)
3303{
3304 unsigned long flags = 0;
3305 int rc;
3306
3307 if (qlt_issue_marker(vha, ha_locked) < 0)
3308 return;
3309
3310 if (ha_locked) {
3311 rc = __qlt_send_term_imm_notif(vha, imm);
3312
3313#if 0 /* Todo */
3314 if (rc == -ENOMEM)
3315 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003316#else
3317 if (rc) {
3318 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003319#endif
3320 goto done;
3321 }
3322
3323 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3324 rc = __qlt_send_term_imm_notif(vha, imm);
3325
3326#if 0 /* Todo */
3327 if (rc == -ENOMEM)
3328 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3329#endif
3330
3331done:
3332 if (!ha_locked)
3333 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3334}
3335
3336/* If hardware_lock held on entry, might drop it, then reaquire */
3337/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003338static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3339 struct qla_tgt_cmd *cmd,
3340 struct atio_from_isp *atio)
3341{
3342 struct ctio7_to_24xx *ctio24;
3343 struct qla_hw_data *ha = vha->hw;
3344 request_t *pkt;
3345 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003346 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003347
3348 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3349
Arun Easib6a029e2014-09-25 06:14:52 -04003350 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003351 if (pkt == NULL) {
3352 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3353 "qla_target(%d): %s failed: unable to allocate "
3354 "request packet\n", vha->vp_idx, __func__);
3355 return -ENOMEM;
3356 }
3357
3358 if (cmd != NULL) {
3359 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3360 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3361 "qla_target(%d): Terminating cmd %p with "
3362 "incorrect state %d\n", vha->vp_idx, cmd,
3363 cmd->state);
3364 } else
3365 ret = 1;
3366 }
3367
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003368 vha->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003369 pkt->entry_count = 1;
3370 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3371
3372 ctio24 = (struct ctio7_to_24xx *)pkt;
3373 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003374 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003375 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003376 ctio24->vp_index = vha->vp_idx;
3377 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3378 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3379 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3380 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3381 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07003382 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003383 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003384 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3385 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003386
3387 /* Most likely, it isn't needed */
3388 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3389 &atio->u.isp24.fcp_cmnd.add_cdb[
3390 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3391 if (ctio24->u.status1.residual != 0)
3392 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3393
Himanshu Madhani63163e02014-09-25 06:14:59 -04003394 /* Memory Barrier */
3395 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003396 qla2x00_start_iocbs(vha, vha->req);
3397 return ret;
3398}
3399
3400static void qlt_send_term_exchange(struct scsi_qla_host *vha,
Quinn Trana07100e2015-12-07 19:48:57 -05003401 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3402 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003403{
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003404 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003405 int rc;
3406
3407 if (qlt_issue_marker(vha, ha_locked) < 0)
3408 return;
3409
3410 if (ha_locked) {
3411 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003412 if (rc == -ENOMEM)
3413 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003414 goto done;
3415 }
3416 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3417 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003418 if (rc == -ENOMEM)
3419 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003420
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003421done:
Quinn Trana07100e2015-12-07 19:48:57 -05003422 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003423 if (cmd->sg_mapped)
3424 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003425 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3426 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003427
3428 if (!ha_locked)
3429 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3430
Quinn Tran7b898542014-04-11 16:54:44 -04003431 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003432}
3433
Quinn Tran33e79972014-09-25 06:14:55 -04003434static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3435{
3436 struct list_head free_list;
3437 struct qla_tgt_cmd *cmd, *tcmd;
3438
3439 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003440 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003441
3442 cmd = tcmd = NULL;
3443 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3444 INIT_LIST_HEAD(&free_list);
3445 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3446
3447 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3448 list_del(&cmd->cmd_list);
3449 /* This cmd was never sent to TCM. There is no need
3450 * to schedule free or call free_cmd
3451 */
3452 qlt_free_cmd(cmd);
3453 vha->hw->tgt.num_qfull_cmds_alloc--;
3454 }
3455 }
3456 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3457}
3458
3459static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3460{
3461 uint32_t total_leaked;
3462
3463 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3464
3465 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3466 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3467
3468 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3469 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003470 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003471
3472 if (IS_P3P_TYPE(vha->hw))
3473 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3474 else
3475 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3476 qla2xxx_wake_dpc(vha);
3477 }
3478
3479}
3480
Quinn Trana07100e2015-12-07 19:48:57 -05003481int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003482{
3483 struct qla_tgt *tgt = cmd->tgt;
3484 struct scsi_qla_host *vha = tgt->vha;
3485 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003486 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003487
3488 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3489 "qla_target(%d): terminating exchange for aborted cmd=%p "
3490 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3491 se_cmd->tag);
3492
Quinn Trana07100e2015-12-07 19:48:57 -05003493 spin_lock_irqsave(&cmd->cmd_lock, flags);
3494 if (cmd->aborted) {
3495 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3496 /*
3497 * It's normal to see 2 calls in this path:
3498 * 1) XFER Rdy completion + CMD_T_ABORT
3499 * 2) TCM TMR - drain_state_list
3500 */
3501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
3502 "multiple abort. %p transport_state %x, t_state %x,"
3503 " se_cmd_flags %x \n", cmd, cmd->se_cmd.transport_state,
3504 cmd->se_cmd.t_state,cmd->se_cmd.se_cmd_flags);
3505 return EIO;
3506 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003507 cmd->aborted = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003508 cmd->trc_flags |= TRC_ABORT;
Quinn Trana07100e2015-12-07 19:48:57 -05003509 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003510
Quinn Trana07100e2015-12-07 19:48:57 -05003511 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3512 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003513}
3514EXPORT_SYMBOL(qlt_abort_cmd);
3515
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003516void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3517{
Quinn Tran5d964832017-01-19 22:27:59 -08003518 struct fc_port *sess = cmd->sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003519
Quinn Tranf83adb62014-04-11 16:54:43 -04003520 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3521 "%s: se_cmd[%p] ox_id %04x\n",
3522 __func__, &cmd->se_cmd,
3523 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003524
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003525 BUG_ON(cmd->cmd_in_wq);
3526
Quinn Trana07100e2015-12-07 19:48:57 -05003527 if (cmd->sg_mapped)
3528 qlt_unmap_sg(cmd->vha, cmd);
3529
Quinn Tran33e79972014-09-25 06:14:55 -04003530 if (!cmd->q_full)
3531 qlt_decr_num_pend_cmds(cmd->vha);
3532
Quinn Tranf83adb62014-04-11 16:54:43 -04003533 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003534 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003535 if (unlikely(cmd->free_sg))
3536 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003537
3538 if (!sess || !sess->se_sess) {
3539 WARN_ON(1);
3540 return;
3541 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003542 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003543 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003544}
3545EXPORT_SYMBOL(qlt_free_cmd);
3546
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003547/*
3548 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3549 */
3550static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3551 struct qla_tgt_cmd *cmd, uint32_t status)
3552{
3553 int term = 0;
3554
3555 if (ctio != NULL) {
3556 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3557 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003558 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003559 } else
3560 term = 1;
3561
3562 if (term)
Quinn Trana07100e2015-12-07 19:48:57 -05003563 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003564
3565 return term;
3566}
3567
3568/* ha->hardware_lock supposed to be held on entry */
3569static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3570 uint32_t handle)
3571{
3572 struct qla_hw_data *ha = vha->hw;
3573
3574 handle--;
3575 if (ha->tgt.cmds[handle] != NULL) {
3576 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3577 ha->tgt.cmds[handle] = NULL;
3578 return cmd;
3579 } else
3580 return NULL;
3581}
3582
3583/* ha->hardware_lock supposed to be held on entry */
3584static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3585 uint32_t handle, void *ctio)
3586{
3587 struct qla_tgt_cmd *cmd = NULL;
3588
3589 /* Clear out internal marks */
3590 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3591 CTIO_INTERMEDIATE_HANDLE_MARK);
3592
3593 if (handle != QLA_TGT_NULL_HANDLE) {
Arun Easi667024a2014-09-25 06:14:47 -04003594 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003595 return NULL;
Arun Easi667024a2014-09-25 06:14:47 -04003596
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003597 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05003598 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003599 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3600 "qla_target(%d): Wrong handle %x received\n",
3601 vha->vp_idx, handle);
3602 return NULL;
3603 }
3604 cmd = qlt_get_cmd(vha, handle);
3605 if (unlikely(cmd == NULL)) {
3606 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3607 "qla_target(%d): Suspicious: unable to "
3608 "find the command with handle %x\n", vha->vp_idx,
3609 handle);
3610 return NULL;
3611 }
3612 } else if (ctio != NULL) {
3613 /* We can't get loop ID from CTIO7 */
3614 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3615 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3616 "support NULL handles\n", vha->vp_idx);
3617 return NULL;
3618 }
3619
3620 return cmd;
3621}
3622
Arun Easic0cb4492014-09-25 06:14:51 -04003623/* hardware_lock should be held by caller. */
3624static void
3625qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3626{
3627 struct qla_hw_data *ha = vha->hw;
3628 uint32_t handle;
3629
3630 if (cmd->sg_mapped)
3631 qlt_unmap_sg(vha, cmd);
3632
3633 handle = qlt_make_handle(vha);
3634
3635 /* TODO: fix debug message type and ids. */
3636 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3637 ql_dbg(ql_dbg_io, vha, 0xff00,
3638 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3639 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3640 cmd->write_data_transferred = 0;
3641 cmd->state = QLA_TGT_STATE_DATA_IN;
3642
3643 ql_dbg(ql_dbg_io, vha, 0xff01,
3644 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3645
3646 ha->tgt.tgt_ops->handle_data(cmd);
3647 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003648 } else {
3649 ql_dbg(ql_dbg_io, vha, 0xff03,
3650 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3651 cmd->state);
3652 dump_stack();
3653 }
3654
Quinn Tran1eb42f92017-01-19 22:27:55 -08003655 cmd->trc_flags |= TRC_FLUSH;
Arun Easic0cb4492014-09-25 06:14:51 -04003656 ha->tgt.tgt_ops->free_cmd(cmd);
3657}
3658
3659void
3660qlt_host_reset_handler(struct qla_hw_data *ha)
3661{
3662 struct qla_tgt_cmd *cmd;
3663 unsigned long flags;
3664 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3665 scsi_qla_host_t *vha = NULL;
3666 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3667 uint32_t i;
3668
3669 if (!base_vha->hw->tgt.tgt_ops)
3670 return;
3671
3672 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3673 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3674 "Target mode disabled\n");
3675 return;
3676 }
3677
3678 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3679 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3680 base_vha->dpc_flags);
3681
3682 spin_lock_irqsave(&ha->hardware_lock, flags);
3683 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3684 cmd = qlt_get_cmd(base_vha, i);
3685 if (!cmd)
3686 continue;
3687 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3688 vha = cmd->vha;
3689 qlt_abort_cmd_on_host_reset(vha, cmd);
3690 }
3691 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3692}
3693
3694
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003695/*
3696 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3697 */
3698static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3699 uint32_t status, void *ctio)
3700{
3701 struct qla_hw_data *ha = vha->hw;
3702 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003703 struct qla_tgt_cmd *cmd;
3704
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003705 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3706 /* That could happen only in case of an error/reset/abort */
3707 if (status != CTIO_SUCCESS) {
3708 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3709 "Intermediate CTIO received"
3710 " (status %x)\n", status);
3711 }
3712 return;
3713 }
3714
3715 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003716 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003717 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003718
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003719 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003720 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003721
Joern Engelf9b67212014-09-16 16:23:18 -04003722 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003723
3724 if (unlikely(status != CTIO_SUCCESS)) {
3725 switch (status & 0xFFFF) {
3726 case CTIO_LIP_RESET:
3727 case CTIO_TARGET_RESET:
3728 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003729 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003730 case CTIO_TIMEOUT:
3731 case CTIO_INVALID_RX_ID:
3732 /* They are OK */
3733 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3734 "qla_target(%d): CTIO with "
3735 "status %#x received, state %x, se_cmd %p, "
3736 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3737 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3738 status, cmd->state, se_cmd);
3739 break;
3740
3741 case CTIO_PORT_LOGGED_OUT:
3742 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003743 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003744 int logged_out =
3745 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3746
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003747 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003748 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003749 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003750 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003751 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003752
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003753 if (logged_out && cmd->sess) {
3754 /*
3755 * Session is already logged out, but we need
3756 * to notify initiator, who's not aware of this
3757 */
3758 cmd->sess->logout_on_delete = 0;
3759 cmd->sess->send_els_logo = 1;
Quinn Tran726b8542017-01-19 22:28:00 -08003760 ql_dbg(ql_dbg_disc, vha, 0xffff,
3761 "%s %d %8phC post del sess\n",
3762 __func__, __LINE__, cmd->sess->port_name);
3763
3764 qlt_schedule_sess_for_deletion_lock(cmd->sess);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003765 }
3766 break;
3767 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003768 case CTIO_DIF_ERROR: {
3769 struct ctio_crc_from_fw *crc =
3770 (struct ctio_crc_from_fw *)ctio;
3771 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3772 "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
3773 vha->vp_idx, status, cmd->state, se_cmd,
3774 *((u64 *)&crc->actual_dif[0]),
3775 *((u64 *)&crc->expected_dif[0]));
3776
3777 if (qlt_handle_dif_error(vha, cmd, ctio)) {
3778 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3779 /* scsi Write/xfer rdy complete */
3780 goto skip_term;
3781 } else {
3782 /* scsi read/xmit respond complete
3783 * call handle dif to send scsi status
3784 * rather than terminate exchange.
3785 */
3786 cmd->state = QLA_TGT_STATE_PROCESSED;
3787 ha->tgt.tgt_ops->handle_dif_err(cmd);
3788 return;
3789 }
3790 } else {
3791 /* Need to generate a SCSI good completion.
3792 * because FW did not send scsi status.
3793 */
3794 status = 0;
3795 goto skip_term;
3796 }
3797 break;
3798 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003799 default:
3800 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003801 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003802 vha->vp_idx, status, cmd->state, se_cmd);
3803 break;
3804 }
3805
Quinn Tran7b898542014-04-11 16:54:44 -04003806
Quinn Tran193b50b2015-12-17 14:57:03 -05003807 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003808 * cmd is already aborted/terminated, we don't
3809 * need to terminate again. The exchange is already
3810 * cleaned up/freed at FW level. Just cleanup at driver
3811 * level.
3812 */
3813 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003814 (!cmd->aborted)) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003815 cmd->trc_flags |= TRC_CTIO_ERR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003816 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3817 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003818 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003819 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003820skip_term:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003821
3822 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003823 cmd->trc_flags |= TRC_CTIO_DONE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003824 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003825 cmd->state = QLA_TGT_STATE_DATA_IN;
3826
Bart Van Assche52c82822015-07-09 07:23:26 -07003827 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003828 cmd->write_data_transferred = 1;
3829
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003830 ha->tgt.tgt_ops->handle_data(cmd);
3831 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003832 } else if (cmd->aborted) {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003833 cmd->trc_flags |= TRC_CTIO_ABORTED;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003834 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003835 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003836 } else {
Quinn Tran1eb42f92017-01-19 22:27:55 -08003837 cmd->trc_flags |= TRC_CTIO_STRANGE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003838 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3839 "qla_target(%d): A command in state (%d) should "
3840 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3841 }
3842
Quinn Tran7b898542014-04-11 16:54:44 -04003843 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003844 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003845 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3846 dump_stack();
3847 }
3848
3849 ha->tgt.tgt_ops->free_cmd(cmd);
3850}
3851
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003852static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3853 uint8_t task_codes)
3854{
3855 int fcp_task_attr;
3856
3857 switch (task_codes) {
3858 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003859 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003860 break;
3861 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003862 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003863 break;
3864 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003865 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003866 break;
3867 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003868 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003869 break;
3870 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003871 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003872 break;
3873 default:
3874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3875 "qla_target: unknown task code %x, use ORDERED instead\n",
3876 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003877 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003878 break;
3879 }
3880
3881 return fcp_task_attr;
3882}
3883
Quinn Tran5d964832017-01-19 22:27:59 -08003884static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003885 uint8_t *);
3886/*
3887 * Process context for I/O path into tcm_qla2xxx code
3888 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003889static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003890{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003891 scsi_qla_host_t *vha = cmd->vha;
3892 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003893 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08003894 struct fc_port *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003895 struct atio_from_isp *atio = &cmd->atio;
3896 unsigned char *cdb;
3897 unsigned long flags;
3898 uint32_t data_length;
3899 int ret, fcp_task_attr, data_dir, bidi = 0;
3900
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003901 cmd->cmd_in_wq = 0;
Quinn Tran1eb42f92017-01-19 22:27:55 -08003902 cmd->trc_flags |= TRC_DO_WORK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003903 if (tgt->tgt_stop)
3904 goto out_term;
3905
Quinn Tran193b50b2015-12-17 14:57:03 -05003906 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003907 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3908 "cmd with tag %u is aborted\n",
3909 cmd->atio.u.isp24.exchange_addr);
3910 goto out_term;
3911 }
3912
Quinn Trana07100e2015-12-07 19:48:57 -05003913 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003914 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02003915 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003916 cmd->unpacked_lun = scsilun_to_int(
3917 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3918
3919 if (atio->u.isp24.fcp_cmnd.rddata &&
3920 atio->u.isp24.fcp_cmnd.wrdata) {
3921 bidi = 1;
3922 data_dir = DMA_TO_DEVICE;
3923 } else if (atio->u.isp24.fcp_cmnd.rddata)
3924 data_dir = DMA_FROM_DEVICE;
3925 else if (atio->u.isp24.fcp_cmnd.wrdata)
3926 data_dir = DMA_TO_DEVICE;
3927 else
3928 data_dir = DMA_NONE;
3929
3930 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3931 atio->u.isp24.fcp_cmnd.task_attr);
3932 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3933 &atio->u.isp24.fcp_cmnd.add_cdb[
3934 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3935
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003936 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3937 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003938 if (ret != 0)
3939 goto out_term;
3940 /*
3941 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3942 */
Quinn Tran75601512015-12-17 14:57:04 -05003943 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08003944 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05003945 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003946 return;
3947
3948out_term:
Arun Easi667024a2014-09-25 06:14:47 -04003949 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003950 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07003951 * cmd has not sent to target yet, so pass NULL as the second
3952 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003953 */
Quinn Tran1eb42f92017-01-19 22:27:55 -08003954 cmd->trc_flags |= TRC_DO_WORK_ERR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003955 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003956 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04003957
3958 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003959 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Jörn Engel08234e32013-06-12 16:27:54 -04003960 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tran75601512015-12-17 14:57:04 -05003961
3962 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Quinn Tran5d964832017-01-19 22:27:59 -08003963 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05003964 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003965}
3966
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003967static void qlt_do_work(struct work_struct *work)
3968{
3969 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003970 scsi_qla_host_t *vha = cmd->vha;
3971 unsigned long flags;
3972
3973 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3974 list_del(&cmd->cmd_list);
3975 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003976
3977 __qlt_do_work(cmd);
3978}
3979
3980static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08003981 struct fc_port *sess,
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003982 struct atio_from_isp *atio)
3983{
3984 struct se_session *se_sess = sess->se_sess;
3985 struct qla_tgt_cmd *cmd;
3986 int tag;
3987
3988 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
3989 if (tag < 0)
3990 return NULL;
3991
3992 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
3993 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
3994
3995 memcpy(&cmd->atio, atio, sizeof(*atio));
3996 cmd->state = QLA_TGT_STATE_NEW;
3997 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04003998 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003999 cmd->vha = vha;
4000 cmd->se_cmd.map_tag = tag;
4001 cmd->sess = sess;
4002 cmd->loop_id = sess->loop_id;
4003 cmd->conf_compl_supported = sess->conf_compl_supported;
4004
Quinn Tran1eb42f92017-01-19 22:27:55 -08004005 cmd->trc_flags = 0;
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04004006 cmd->jiffies_at_alloc = get_jiffies_64();
4007
4008 cmd->reset_count = vha->hw->chip_reset;
4009
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004010 return cmd;
4011}
4012
4013static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
4014 uint16_t);
4015
4016static void qlt_create_sess_from_atio(struct work_struct *work)
4017{
4018 struct qla_tgt_sess_op *op = container_of(work,
4019 struct qla_tgt_sess_op, work);
4020 scsi_qla_host_t *vha = op->vha;
4021 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004022 struct fc_port *sess;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004023 struct qla_tgt_cmd *cmd;
4024 unsigned long flags;
4025 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
4026
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004027 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4028 list_del(&op->cmd_list);
4029 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4030
4031 if (op->aborted) {
4032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
4033 "sess_op with tag %u is aborted\n",
4034 op->atio.u.isp24.exchange_addr);
4035 goto out_term;
4036 }
4037
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004038 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004039 "qla_target(%d): Unable to find wwn login"
4040 " (s_id %x:%x:%x), trying to create it manually\n",
4041 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004042
4043 if (op->atio.u.raw.entry_count > 1) {
4044 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004045 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004046 goto out_term;
4047 }
4048
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004049 sess = qlt_make_local_sess(vha, s_id);
4050 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004051
4052 if (!sess)
4053 goto out_term;
4054 /*
4055 * Now obtain a pre-allocated session tag using the original op->atio
4056 * packet header, and dispatch into __qlt_do_work() using the existing
4057 * process context.
4058 */
4059 cmd = qlt_get_tag(vha, sess, &op->atio);
4060 if (!cmd) {
4061 spin_lock_irqsave(&ha->hardware_lock, flags);
4062 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
Quinn Tran5d964832017-01-19 22:27:59 -08004063 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004064 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4065 kfree(op);
4066 return;
4067 }
Quinn Tran726b8542017-01-19 22:28:00 -08004068
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004069 /*
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004070 * __qlt_do_work() will call qlt_put_sess() to release
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004071 * the extra reference taken above by qlt_make_local_sess()
4072 */
4073 __qlt_do_work(cmd);
4074 kfree(op);
4075 return;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004076out_term:
4077 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05004078 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004079 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4080 kfree(op);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004081}
4082
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004083/* ha->hardware_lock supposed to be held on entry */
4084static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4085 struct atio_from_isp *atio)
4086{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004087 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004088 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004089 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004090 struct qla_tgt_cmd *cmd;
Quinn Tran5d964832017-01-19 22:27:59 -08004091 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004092
4093 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04004094 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004095 "New command while device %p is shutting down\n", tgt);
4096 return -EFAULT;
4097 }
4098
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004099 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4100 if (unlikely(!sess)) {
4101 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
4102 GFP_ATOMIC);
4103 if (!op)
4104 return -ENOMEM;
4105
4106 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04004107 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004108
4109 spin_lock(&vha->cmd_list_lock);
4110 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
4111 spin_unlock(&vha->cmd_list_lock);
4112
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004113 INIT_WORK(&op->work, qlt_create_sess_from_atio);
4114 queue_work(qla_tgt_wq, &op->work);
4115 return 0;
4116 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004117
4118 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4119 * session deletion, but it's still in sess_del_work wq */
Quinn Tran726b8542017-01-19 22:28:00 -08004120 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004121 ql_dbg(ql_dbg_io, vha, 0x3061,
4122 "New command while old session %p is being deleted\n",
4123 sess);
4124 return -EFAULT;
4125 }
4126
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004127 /*
4128 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4129 */
Quinn Tran726b8542017-01-19 22:28:00 -08004130 if (!kref_get_unless_zero(&sess->sess_kref)) {
4131 ql_dbg(ql_dbg_tgt, vha, 0xffff,
4132 "%s: kref_get fail, %8phC oxid %x \n",
4133 __func__, sess->port_name,
4134 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4135 return -EFAULT;
4136 }
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004137
4138 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004139 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004140 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004141 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Quinn Tran5d964832017-01-19 22:27:59 -08004142 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4143 ha->tgt.tgt_ops->put_sess(sess);
4144 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004145 return -ENOMEM;
4146 }
4147
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004148 cmd->cmd_in_wq = 1;
Quinn Tran1eb42f92017-01-19 22:27:55 -08004149 cmd->trc_flags |= TRC_NEW_CMD;
Quinn Tran5327c7d2016-02-10 18:59:14 -05004150 cmd->se_cmd.cpuid = ha->msix_count ?
4151 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004152
Quinn Tran726b8542017-01-19 22:28:00 -08004153 spin_lock_irqsave(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004154 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
Quinn Tran726b8542017-01-19 22:28:00 -08004155 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004156
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004157 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Tranfb3269b2015-12-17 14:57:06 -05004158 if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004159 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4160 queue_work_on(smp_processor_id(), qla_tgt_wq,
4161 &cmd->work);
4162 else
4163 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4164 &cmd->work);
4165 } else {
4166 queue_work(qla_tgt_wq, &cmd->work);
4167 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004168 return 0;
4169
4170}
4171
4172/* ha->hardware_lock supposed to be held on entry */
Quinn Tran5d964832017-01-19 22:27:59 -08004173static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004174 int fn, void *iocb, int flags)
4175{
4176 struct scsi_qla_host *vha = sess->vha;
4177 struct qla_hw_data *ha = vha->hw;
4178 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004179 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004180 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004181
4182 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4183 if (!mcmd) {
4184 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4185 "qla_target(%d): Allocation of management "
4186 "command failed, some commands and their data could "
4187 "leak\n", vha->vp_idx);
4188 return -ENOMEM;
4189 }
4190 memset(mcmd, 0, sizeof(*mcmd));
4191 mcmd->sess = sess;
4192
4193 if (iocb) {
4194 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4195 sizeof(mcmd->orig_iocb.imm_ntfy));
4196 }
4197 mcmd->tmr_func = fn;
4198 mcmd->flags = flags;
Arun Easib6a029e2014-09-25 06:14:52 -04004199 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004200
4201 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004202 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004203 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4204 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004205 }
4206
Quinn Tranbe92fc32017-01-19 22:27:54 -08004207 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004208 if (res != 0) {
4209 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4210 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4211 sess->vha->vp_idx, res);
4212 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4213 return -EFAULT;
4214 }
4215
4216 return 0;
4217}
4218
4219/* ha->hardware_lock supposed to be held on entry */
4220static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4221{
4222 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4223 struct qla_hw_data *ha = vha->hw;
4224 struct qla_tgt *tgt;
Quinn Tran5d964832017-01-19 22:27:59 -08004225 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004226 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004227 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004228 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004229
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004230 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004231
4232 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004233 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004234
4235 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004236 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4237 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004238 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4239
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004240 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4241
4242 if (!sess) {
4243 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4244 "qla_target(%d): task mgmt fn 0x%x for "
4245 "non-existant session\n", vha->vp_idx, fn);
4246 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4247 sizeof(struct atio_from_isp));
4248 }
4249
Quinn Tran726b8542017-01-19 22:28:00 -08004250 if (sess->deleted)
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004251 return -EFAULT;
4252
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004253 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4254}
4255
4256/* ha->hardware_lock supposed to be held on entry */
4257static int __qlt_abort_task(struct scsi_qla_host *vha,
Quinn Tran5d964832017-01-19 22:27:59 -08004258 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004259{
4260 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4261 struct qla_hw_data *ha = vha->hw;
4262 struct qla_tgt_mgmt_cmd *mcmd;
4263 uint32_t lun, unpacked_lun;
4264 int rc;
4265
4266 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4267 if (mcmd == NULL) {
4268 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4269 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4270 vha->vp_idx, __func__);
4271 return -ENOMEM;
4272 }
4273 memset(mcmd, 0, sizeof(*mcmd));
4274
4275 mcmd->sess = sess;
4276 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4277 sizeof(mcmd->orig_iocb.imm_ntfy));
4278
4279 lun = a->u.isp24.fcp_cmnd.lun;
4280 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
Arun Easi80187f82014-09-25 06:14:53 -04004281 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004282 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004283
Quinn Tranbe92fc32017-01-19 22:27:54 -08004284 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004285 le16_to_cpu(iocb->u.isp2x.seq_id));
4286 if (rc != 0) {
4287 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4288 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4289 vha->vp_idx, rc);
4290 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4291 return -EFAULT;
4292 }
4293
4294 return 0;
4295}
4296
4297/* ha->hardware_lock supposed to be held on entry */
4298static int qlt_abort_task(struct scsi_qla_host *vha,
4299 struct imm_ntfy_from_isp *iocb)
4300{
4301 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004302 struct fc_port *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004303 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004304 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004305
4306 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4307
Quinn Tran75601512015-12-17 14:57:04 -05004308 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004309 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004310 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4311
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004312 if (sess == NULL) {
4313 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4314 "qla_target(%d): task abort for unexisting "
4315 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004316 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004317 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4318 }
4319
4320 return __qlt_abort_task(vha, iocb, sess);
4321}
4322
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004323void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4324{
Quinn Tran726b8542017-01-19 22:28:00 -08004325 if (rc != MBS_COMMAND_COMPLETE) {
4326 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4327 "%s: se_sess %p / sess %p from"
4328 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4329 " LOGO failed: %#x\n",
4330 __func__,
4331 fcport->se_sess,
4332 fcport,
4333 fcport->port_name, fcport->loop_id,
4334 fcport->d_id.b.domain, fcport->d_id.b.area,
4335 fcport->d_id.b.al_pa, rc);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004336 }
Quinn Tran726b8542017-01-19 22:28:00 -08004337
4338 fcport->logout_completed = 1;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004339}
4340
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004341/*
4342* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4343*
4344* Schedules sessions with matching port_id/loop_id but different wwn for
4345* deletion. Returns existing session with matching wwn if present.
4346* Null otherwise.
4347*/
Quinn Tran726b8542017-01-19 22:28:00 -08004348struct fc_port *
4349qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
Quinn Tran5d964832017-01-19 22:27:59 -08004350 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004351{
Quinn Tran5d964832017-01-19 22:27:59 -08004352 struct fc_port *sess = NULL, *other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004353 uint64_t other_wwn;
4354
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004355 *conflict_sess = NULL;
4356
Quinn Tran5d964832017-01-19 22:27:59 -08004357 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004358
4359 other_wwn = wwn_to_u64(other_sess->port_name);
4360
4361 if (wwn == other_wwn) {
4362 WARN_ON(sess);
4363 sess = other_sess;
4364 continue;
4365 }
4366
4367 /* find other sess with nport_id collision */
Quinn Tran37cacc02017-01-19 22:27:58 -08004368 if (port_id.b24 == other_sess->d_id.b24) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004369 if (loop_id != other_sess->loop_id) {
Quinn Tran726b8542017-01-19 22:28:00 -08004370 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004371 "Invalidating sess %p loop_id %d wwn %llx.\n",
4372 other_sess, other_sess->loop_id, other_wwn);
4373
4374 /*
4375 * logout_on_delete is set by default, but another
4376 * session that has the same s_id/loop_id combo
4377 * might have cleared it when requested this session
4378 * deletion, so don't touch it
4379 */
4380 qlt_schedule_sess_for_deletion(other_sess, true);
4381 } else {
4382 /*
4383 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004384 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004385 */
Quinn Tran726b8542017-01-19 22:28:00 -08004386 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
4387 "Invalidating sess %p loop_id %d wwn %llx.\n",
4388 other_sess, other_sess->loop_id, other_wwn);
4389
4390
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004391 other_sess->keep_nport_handle = 1;
4392 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004393 qlt_schedule_sess_for_deletion(other_sess,
4394 true);
4395 }
4396 continue;
4397 }
4398
4399 /* find other sess with nport handle collision */
Quinn Tran726b8542017-01-19 22:28:00 -08004400 if ((loop_id == other_sess->loop_id) &&
4401 (loop_id != FC_NO_LOOP_ID)) {
4402 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004403 "Invalidating sess %p loop_id %d wwn %llx.\n",
4404 other_sess, other_sess->loop_id, other_wwn);
4405
4406 /* Same loop_id but different s_id
4407 * Ok to kill and logout */
4408 qlt_schedule_sess_for_deletion(other_sess, true);
4409 }
4410 }
4411
4412 return sess;
4413}
4414
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004415/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4416static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4417{
4418 struct qla_tgt_sess_op *op;
4419 struct qla_tgt_cmd *cmd;
4420 uint32_t key;
4421 int count = 0;
4422
4423 key = (((u32)s_id->b.domain << 16) |
4424 ((u32)s_id->b.area << 8) |
4425 ((u32)s_id->b.al_pa));
4426
4427 spin_lock(&vha->cmd_list_lock);
4428 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4429 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
Quinn Tran41dc5292017-01-19 22:28:03 -08004430
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004431 if (op_key == key) {
4432 op->aborted = true;
4433 count++;
4434 }
4435 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004436
4437 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4438 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4439 if (op_key == key) {
4440 op->aborted = true;
4441 count++;
4442 }
4443 }
4444
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004445 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4446 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4447 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004448 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004449 count++;
4450 }
4451 }
4452 spin_unlock(&vha->cmd_list_lock);
4453
4454 return count;
4455}
4456
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004457/*
4458 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4459 */
4460static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4461 struct imm_ntfy_from_isp *iocb)
4462{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004463 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004464 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004465 struct fc_port *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004466 uint64_t wwn;
4467 port_id_t port_id;
4468 uint16_t loop_id;
4469 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004470 int res = 0;
Quinn Tran5d964832017-01-19 22:27:59 -08004471 struct qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004472 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004473
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004474 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4475
4476 port_id.b.domain = iocb->u.isp24.port_id[2];
4477 port_id.b.area = iocb->u.isp24.port_id[1];
4478 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4479 port_id.b.rsvd_1 = 0;
4480
4481 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4482
Quinn Tran726b8542017-01-19 22:28:00 -08004483 ql_dbg(ql_dbg_disc, vha, 0xf026,
4484 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4485 vha->vp_idx, iocb->u.isp24.port_id[2],
4486 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4487 iocb->u.isp24.status_subcode, loop_id,
4488 iocb->u.isp24.port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004489
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004490 /* res = 1 means ack at the end of thread
4491 * res = 0 means ack async/later.
4492 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004493 switch (iocb->u.isp24.status_subcode) {
4494 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004495
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004496 /* Mark all stale commands in qla_tgt_wq for deletion */
4497 abort_cmds_for_s_id(vha, &port_id);
4498
Quinn Tran75601512015-12-17 14:57:04 -05004499 if (wwn) {
4500 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004501 sess = qlt_find_sess_invalidate_other(vha, wwn,
4502 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004503 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4504 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004505
Quinn Tran726b8542017-01-19 22:28:00 -08004506 if (IS_SW_RESV_ADDR(port_id)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004507 res = 1;
4508 break;
4509 }
4510
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004511 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4512 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004513 qlt_send_term_imm_notif(vha, iocb, 1);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004514 break;
4515 }
4516
4517 res = 0;
4518
Quinn Tran726b8542017-01-19 22:28:00 -08004519 if (conflict_sess) {
4520 conflict_sess->login_gen++;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004521 qlt_plogi_ack_link(vha, pla, conflict_sess,
Quinn Tran726b8542017-01-19 22:28:00 -08004522 QLT_PLOGI_LINK_CONFLICT);
4523 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004524
Quinn Tran726b8542017-01-19 22:28:00 -08004525 if (!sess) {
4526 pla->ref_count++;
4527 qla24xx_post_newsess_work(vha, &port_id,
4528 iocb->u.isp24.port_name, pla);
4529 res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004530 break;
Quinn Tran726b8542017-01-19 22:28:00 -08004531 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004532
4533 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Quinn Tran726b8542017-01-19 22:28:00 -08004534 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4535 sess->d_id = port_id;
4536 sess->login_gen++;
4537
4538 switch (sess->disc_state) {
4539 case DSC_DELETED:
4540 qlt_plogi_ack_unref(vha, pla);
4541 break;
4542
4543 default:
4544 /*
4545 * Under normal circumstances we want to release nport handle
4546 * during LOGO process to avoid nport handle leaks inside FW.
4547 * The exception is when LOGO is done while another PLOGI with
4548 * the same nport handle is waiting as might be the case here.
4549 * Note: there is always a possibily of a race where session
4550 * deletion has already started for other reasons (e.g. ACL
4551 * removal) and now PLOGI arrives:
4552 * 1. if PLOGI arrived in FW after nport handle has been freed,
4553 * FW must have assigned this PLOGI a new/same handle and we
4554 * can proceed ACK'ing it as usual when session deletion
4555 * completes.
4556 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4557 * bit reached it, the handle has now been released. We'll
4558 * get an error when we ACK this PLOGI. Nothing will be sent
4559 * back to initiator. Initiator should eventually retry
4560 * PLOGI and situation will correct itself.
4561 */
4562 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4563 (sess->d_id.b24 == port_id.b24));
4564
4565 ql_dbg(ql_dbg_disc, vha, 0xffff,
4566 "%s %d %8phC post del sess\n",
4567 __func__, __LINE__, sess->port_name);
4568
4569
4570 qlt_schedule_sess_for_deletion_lock(sess);
4571 break;
4572 }
4573
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004574 break;
4575
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004576 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004577 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4578
Quinn Tran75601512015-12-17 14:57:04 -05004579 if (wwn) {
4580 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004581 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4582 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004583 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4584 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004585
4586 if (conflict_sess) {
4587 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4588 "PRLI with conflicting sess %p port %8phC\n",
4589 conflict_sess, conflict_sess->port_name);
4590 qlt_send_term_imm_notif(vha, iocb, 1);
4591 res = 0;
4592 break;
4593 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004594
4595 if (sess != NULL) {
Quinn Tran726b8542017-01-19 22:28:00 -08004596 if (sess->fw_login_state == DSC_LS_PLOGI_PEND) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004597 /*
4598 * Impatient initiator sent PRLI before last
4599 * PLOGI could finish. Will force him to re-try,
4600 * while last one finishes.
4601 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004602 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004603 "sess %p PRLI received, before plogi ack.\n",
4604 sess);
4605 qlt_send_term_imm_notif(vha, iocb, 1);
4606 res = 0;
4607 break;
4608 }
4609
4610 /*
4611 * This shouldn't happen under normal circumstances,
4612 * since we have deleted the old session during PLOGI
4613 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004614 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004615 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4616 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4617
4618 sess->local = 0;
4619 sess->loop_id = loop_id;
Quinn Tran37cacc02017-01-19 22:27:58 -08004620 sess->d_id = port_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004621 sess->fw_login_state = DSC_LS_PRLI_PEND;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004622
4623 if (wd3_lo & BIT_7)
4624 sess->conf_compl_supported = 1;
4625
Quinn Tran726b8542017-01-19 22:28:00 -08004626 if ((wd3_lo & BIT_4) == 0)
4627 sess->port_type = FCT_INITIATOR;
4628 else
4629 sess->port_type = FCT_TARGET;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004630 }
4631 res = 1; /* send notify ack */
4632
4633 /* Make session global (not used in fabric mode) */
4634 if (ha->current_topology != ISP_CFG_F) {
4635 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4636 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4637 qla2xxx_wake_dpc(vha);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004638 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08004639 if (sess) {
4640 ql_dbg(ql_dbg_disc, vha, 0xffff,
4641 "%s %d %8phC post nack\n",
4642 __func__, __LINE__, sess->port_name);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004643
Quinn Tran726b8542017-01-19 22:28:00 -08004644 qla24xx_post_nack_work(vha, sess, iocb,
4645 SRB_NACK_PRLI);
4646 res = 0;
4647 }
4648 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004649 break;
4650
Quinn Tran41dc5292017-01-19 22:28:03 -08004651
4652 case ELS_TPRLO:
4653 if (le16_to_cpu(iocb->u.isp24.flags) &
4654 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
4655 loop_id = 0xFFFF;
4656 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
4657 res = 1;
4658 break;
4659 }
4660 /* drop through */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004661 case ELS_LOGO:
4662 case ELS_PRLO:
Quinn Tran726b8542017-01-19 22:28:00 -08004663 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4664 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
4665 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4666
4667 if (sess) {
4668 sess->login_gen++;
4669 sess->fw_login_state = DSC_LS_LOGO_PEND;
Quinn Tran726b8542017-01-19 22:28:00 -08004670 sess->logo_ack_needed = 1;
4671 memcpy(sess->iocb, iocb, IOCB_SIZE);
4672 }
4673
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004674 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
Quinn Tran726b8542017-01-19 22:28:00 -08004675
4676 ql_dbg(ql_dbg_disc, vha, 0xffff,
4677 "%s: logo %llx res %d sess %p ",
4678 __func__, wwn, res, sess);
4679 if (res == 0) {
Quinn Tran41dc5292017-01-19 22:28:03 -08004680 /*
4681 * cmd went upper layer, look for qlt_xmit_tm_rsp()
4682 * for LOGO_ACK & sess delete
4683 */
Quinn Tran726b8542017-01-19 22:28:00 -08004684 BUG_ON(!sess);
4685 res = 0;
4686 } else {
Quinn Tran41dc5292017-01-19 22:28:03 -08004687 /* cmd did not go to upper layer. */
Quinn Tran726b8542017-01-19 22:28:00 -08004688 if (sess) {
4689 qlt_schedule_sess_for_deletion_lock(sess);
4690 res = 0;
4691 }
4692 /* else logo will be ack */
4693 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004694 break;
4695 case ELS_PDISC:
4696 case ELS_ADISC:
4697 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004698 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004699 if (tgt->link_reinit_iocb_pending) {
4700 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4701 0, 0, 0, 0, 0, 0);
4702 tgt->link_reinit_iocb_pending = 0;
4703 }
Quinn Tran726b8542017-01-19 22:28:00 -08004704
4705 sess = qla2x00_find_fcport_by_wwpn(vha,
4706 iocb->u.isp24.port_name, 1);
4707 if (sess) {
4708 ql_dbg(ql_dbg_disc, vha, 0xffff,
4709 "sess %p lid %d|%d DS %d LS %d\n",
4710 sess, sess->loop_id, loop_id,
4711 sess->disc_state, sess->fw_login_state);
4712 }
4713
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004714 res = 1; /* send notify ack */
4715 break;
4716 }
4717
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004718 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004719 default:
4720 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4721 "qla_target(%d): Unsupported ELS command %x "
4722 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4723 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4724 break;
4725 }
4726
4727 return res;
4728}
4729
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004730/*
4731 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4732 */
4733static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4734 struct imm_ntfy_from_isp *iocb)
4735{
4736 struct qla_hw_data *ha = vha->hw;
4737 uint32_t add_flags = 0;
4738 int send_notify_ack = 1;
4739 uint16_t status;
4740
4741 status = le16_to_cpu(iocb->u.isp2x.status);
4742 switch (status) {
4743 case IMM_NTFY_LIP_RESET:
4744 {
4745 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4746 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4747 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4748 iocb->u.isp24.status_subcode);
4749
4750 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4751 send_notify_ack = 0;
4752 break;
4753 }
4754
4755 case IMM_NTFY_LIP_LINK_REINIT:
4756 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004757 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004758 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4759 "qla_target(%d): LINK REINIT (loop %#x, "
4760 "subcode %x)\n", vha->vp_idx,
4761 le16_to_cpu(iocb->u.isp24.nport_handle),
4762 iocb->u.isp24.status_subcode);
4763 if (tgt->link_reinit_iocb_pending) {
4764 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4765 0, 0, 0, 0, 0, 0);
4766 }
4767 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4768 tgt->link_reinit_iocb_pending = 1;
4769 /*
4770 * QLogic requires to wait after LINK REINIT for possible
4771 * PDISC or ADISC ELS commands
4772 */
4773 send_notify_ack = 0;
4774 break;
4775 }
4776
4777 case IMM_NTFY_PORT_LOGOUT:
4778 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4779 "qla_target(%d): Port logout (loop "
4780 "%#x, subcode %x)\n", vha->vp_idx,
4781 le16_to_cpu(iocb->u.isp24.nport_handle),
4782 iocb->u.isp24.status_subcode);
4783
4784 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4785 send_notify_ack = 0;
4786 /* The sessions will be cleared in the callback, if needed */
4787 break;
4788
4789 case IMM_NTFY_GLBL_TPRLO:
4790 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4791 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4792 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4793 send_notify_ack = 0;
4794 /* The sessions will be cleared in the callback, if needed */
4795 break;
4796
4797 case IMM_NTFY_PORT_CONFIG:
4798 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4799 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4800 status);
4801 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4802 send_notify_ack = 0;
4803 /* The sessions will be cleared in the callback, if needed */
4804 break;
4805
4806 case IMM_NTFY_GLBL_LOGO:
4807 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4808 "qla_target(%d): Link failure detected\n",
4809 vha->vp_idx);
4810 /* I_T nexus loss */
4811 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4812 send_notify_ack = 0;
4813 break;
4814
4815 case IMM_NTFY_IOCB_OVERFLOW:
4816 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4817 "qla_target(%d): Cannot provide requested "
4818 "capability (IOCB overflowed the immediate notify "
4819 "resource count)\n", vha->vp_idx);
4820 break;
4821
4822 case IMM_NTFY_ABORT_TASK:
4823 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4824 "qla_target(%d): Abort Task (S %08x I %#x -> "
4825 "L %#x)\n", vha->vp_idx,
4826 le16_to_cpu(iocb->u.isp2x.seq_id),
4827 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4828 le16_to_cpu(iocb->u.isp2x.lun));
4829 if (qlt_abort_task(vha, iocb) == 0)
4830 send_notify_ack = 0;
4831 break;
4832
4833 case IMM_NTFY_RESOURCE:
4834 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4835 "qla_target(%d): Out of resources, host %ld\n",
4836 vha->vp_idx, vha->host_no);
4837 break;
4838
4839 case IMM_NTFY_MSG_RX:
4840 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4841 "qla_target(%d): Immediate notify task %x\n",
4842 vha->vp_idx, iocb->u.isp2x.task_flags);
4843 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4844 send_notify_ack = 0;
4845 break;
4846
4847 case IMM_NTFY_ELS:
4848 if (qlt_24xx_handle_els(vha, iocb) == 0)
4849 send_notify_ack = 0;
4850 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004851 default:
4852 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4853 "qla_target(%d): Received unknown immediate "
4854 "notify status %x\n", vha->vp_idx, status);
4855 break;
4856 }
4857
4858 if (send_notify_ack)
4859 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4860}
4861
4862/*
4863 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4864 * This function sends busy to ISP 2xxx or 24xx.
4865 */
Quinn Tran33e79972014-09-25 06:14:55 -04004866static int __qlt_send_busy(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004867 struct atio_from_isp *atio, uint16_t status)
4868{
4869 struct ctio7_to_24xx *ctio24;
4870 struct qla_hw_data *ha = vha->hw;
4871 request_t *pkt;
Quinn Tran5d964832017-01-19 22:27:59 -08004872 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05004873 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004874
Quinn Tran75601512015-12-17 14:57:04 -05004875 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004876 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4877 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004878 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004879 if (!sess) {
Quinn Trana07100e2015-12-07 19:48:57 -05004880 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04004881 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004882 }
4883 /* Sending marker isn't necessary, since we called from ISR */
4884
4885 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
4886 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04004887 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004888 "qla_target(%d): %s failed: unable to allocate "
4889 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04004890 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004891 }
4892
Himanshu Madhanice1025c2015-12-17 14:56:58 -05004893 vha->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004894 pkt->entry_count = 1;
4895 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
4896
4897 ctio24 = (struct ctio7_to_24xx *)pkt;
4898 ctio24->entry_type = CTIO_TYPE7;
4899 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07004900 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004901 ctio24->vp_index = vha->vp_idx;
4902 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
4903 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
4904 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
4905 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
4906 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07004907 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004908 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
4909 CTIO7_FLAGS_DONT_RET_CTIO);
4910 /*
4911 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
4912 * if the explicit conformation is used.
4913 */
4914 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
4915 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04004916 /* Memory Barrier */
4917 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004918 qla2x00_start_iocbs(vha, vha->req);
Quinn Tran33e79972014-09-25 06:14:55 -04004919 return 0;
4920}
4921
4922/*
4923 * This routine is used to allocate a command for either a QFull condition
4924 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
4925 * out previously.
4926 */
4927static void
4928qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
4929 struct atio_from_isp *atio, uint16_t status, int qfull)
4930{
4931 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4932 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08004933 struct fc_port *sess;
Quinn Tran33e79972014-09-25 06:14:55 -04004934 struct se_session *se_sess;
4935 struct qla_tgt_cmd *cmd;
4936 int tag;
4937
4938 if (unlikely(tgt->tgt_stop)) {
4939 ql_dbg(ql_dbg_io, vha, 0x300a,
4940 "New command while device %p is shutting down\n", tgt);
4941 return;
4942 }
4943
4944 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
4945 vha->hw->tgt.num_qfull_cmds_dropped++;
4946 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04004947 vha->qla_stats.stat_max_qfull_cmds_dropped)
4948 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04004949 vha->hw->tgt.num_qfull_cmds_dropped;
4950
4951 ql_dbg(ql_dbg_io, vha, 0x3068,
4952 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
4953 vha->vp_idx, __func__,
4954 vha->hw->tgt.num_qfull_cmds_dropped);
4955
4956 qlt_chk_exch_leak_thresh_hold(vha);
4957 return;
4958 }
4959
4960 sess = ha->tgt.tgt_ops->find_sess_by_s_id
4961 (vha, atio->u.isp24.fcp_hdr.s_id);
4962 if (!sess)
4963 return;
4964
4965 se_sess = sess->se_sess;
4966
4967 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4968 if (tag < 0)
4969 return;
4970
4971 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4972 if (!cmd) {
4973 ql_dbg(ql_dbg_io, vha, 0x3009,
4974 "qla_target(%d): %s: Allocation of cmd failed\n",
4975 vha->vp_idx, __func__);
4976
4977 vha->hw->tgt.num_qfull_cmds_dropped++;
4978 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04004979 vha->qla_stats.stat_max_qfull_cmds_dropped)
4980 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04004981 vha->hw->tgt.num_qfull_cmds_dropped;
4982
4983 qlt_chk_exch_leak_thresh_hold(vha);
4984 return;
4985 }
4986
4987 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4988
4989 qlt_incr_num_pend_cmds(vha);
4990 INIT_LIST_HEAD(&cmd->cmd_list);
4991 memcpy(&cmd->atio, atio, sizeof(*atio));
4992
4993 cmd->tgt = vha->vha_tgt.qla_tgt;
4994 cmd->vha = vha;
4995 cmd->reset_count = vha->hw->chip_reset;
4996 cmd->q_full = 1;
4997
4998 if (qfull) {
4999 cmd->q_full = 1;
5000 /* NOTE: borrowing the state field to carry the status */
5001 cmd->state = status;
5002 } else
5003 cmd->term_exchg = 1;
5004
5005 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5006
5007 vha->hw->tgt.num_qfull_cmds_alloc++;
5008 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005009 vha->qla_stats.stat_max_qfull_cmds_alloc)
5010 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005011 vha->hw->tgt.num_qfull_cmds_alloc;
5012}
5013
5014int
5015qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5016{
5017 struct qla_hw_data *ha = vha->hw;
5018 unsigned long flags;
5019 struct qla_tgt_cmd *cmd, *tcmd;
5020 struct list_head free_list;
5021 int rc = 0;
5022
5023 if (list_empty(&ha->tgt.q_full_list))
5024 return 0;
5025
5026 INIT_LIST_HEAD(&free_list);
5027
5028 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5029
5030 if (list_empty(&ha->tgt.q_full_list)) {
5031 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5032 return 0;
5033 }
5034
5035 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5036 if (cmd->q_full)
5037 /* cmd->state is a borrowed field to hold status */
5038 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5039 else if (cmd->term_exchg)
5040 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5041
5042 if (rc == -ENOMEM)
5043 break;
5044
5045 if (cmd->q_full)
5046 ql_dbg(ql_dbg_io, vha, 0x3006,
5047 "%s: busy sent for ox_id[%04x]\n", __func__,
5048 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5049 else if (cmd->term_exchg)
5050 ql_dbg(ql_dbg_io, vha, 0x3007,
5051 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5052 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5053 else
5054 ql_dbg(ql_dbg_io, vha, 0x3008,
5055 "%s: Unexpected cmd in QFull list %p\n", __func__,
5056 cmd);
5057
5058 list_del(&cmd->cmd_list);
5059 list_add_tail(&cmd->cmd_list, &free_list);
5060
5061 /* piggy back on hardware_lock for protection */
5062 vha->hw->tgt.num_qfull_cmds_alloc--;
5063 }
5064 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5065
5066 cmd = NULL;
5067
5068 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5069 list_del(&cmd->cmd_list);
5070 /* This cmd was never sent to TCM. There is no need
5071 * to schedule free or call free_cmd
5072 */
5073 qlt_free_cmd(cmd);
5074 }
5075 return rc;
5076}
5077
5078static void
5079qlt_send_busy(struct scsi_qla_host *vha,
5080 struct atio_from_isp *atio, uint16_t status)
5081{
5082 int rc = 0;
5083
5084 rc = __qlt_send_busy(vha, atio, status);
5085 if (rc == -ENOMEM)
5086 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5087}
5088
5089static int
5090qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
Quinn Tran8b666802017-03-15 09:48:45 -07005091 struct atio_from_isp *atio, bool ha_locked)
Quinn Tran33e79972014-09-25 06:14:55 -04005092{
5093 struct qla_hw_data *ha = vha->hw;
5094 uint16_t status;
Quinn Tran8b666802017-03-15 09:48:45 -07005095 unsigned long flags;
Quinn Tran33e79972014-09-25 06:14:55 -04005096
5097 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5098 return 0;
5099
Quinn Tran8b666802017-03-15 09:48:45 -07005100 if (!ha_locked)
5101 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran33e79972014-09-25 06:14:55 -04005102 status = temp_sam_status;
5103 qlt_send_busy(vha, atio, status);
Quinn Tran8b666802017-03-15 09:48:45 -07005104 if (!ha_locked)
5105 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5106
Quinn Tran33e79972014-09-25 06:14:55 -04005107 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005108}
5109
5110/* ha->hardware_lock supposed to be held on entry */
5111/* called via callback from qla2xxx */
5112static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005113 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005114{
5115 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005116 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005117 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005118 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005119
5120 if (unlikely(tgt == NULL)) {
Arun Easi667024a2014-09-25 06:14:47 -04005121 ql_dbg(ql_dbg_io, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005122 "ATIO pkt, but no tgt (ha %p)", ha);
5123 return;
5124 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005125 /*
5126 * In tgt_stop mode we also should allow all requests to pass.
5127 * Otherwise, some commands can stuck.
5128 */
5129
Quinn Tran2f424b92015-12-17 14:57:07 -05005130 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005131
5132 switch (atio->u.raw.entry_type) {
5133 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005134 if (unlikely(atio->u.isp24.exchange_addr ==
5135 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005136 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005137 "qla_target(%d): ATIO_TYPE7 "
5138 "received with UNKNOWN exchange address, "
5139 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005140 if (!ha_locked)
5141 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005142 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005143 if (!ha_locked)
5144 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005145 break;
5146 }
Quinn Tran33e79972014-09-25 06:14:55 -04005147
5148
5149
5150 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
Quinn Tran8b666802017-03-15 09:48:45 -07005151 rc = qlt_chk_qfull_thresh_hold(vha, atio, ha_locked);
Quinn Tran33e79972014-09-25 06:14:55 -04005152 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005153 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005154 return;
5155 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005156 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005157 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005158 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005159 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005160 if (unlikely(rc != 0)) {
5161 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005162 if (!ha_locked)
5163 spin_lock_irqsave
5164 (&ha->hardware_lock, flags);
5165
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005166#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5167 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5168#else
Quinn Trana07100e2015-12-07 19:48:57 -05005169 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005170#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005171
5172 if (!ha_locked)
5173 spin_unlock_irqrestore
5174 (&ha->hardware_lock, flags);
5175
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005176 } else {
5177 if (tgt->tgt_stop) {
5178 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5179 "qla_target: Unable to send "
5180 "command to target for req, "
5181 "ignoring.\n");
5182 } else {
5183 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5184 "qla_target(%d): Unable to send "
5185 "command to target, sending BUSY "
5186 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005187 if (!ha_locked)
5188 spin_lock_irqsave(
5189 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005190 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005191 if (!ha_locked)
5192 spin_unlock_irqrestore(
5193 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005194 }
5195 }
5196 }
5197 break;
5198
5199 case IMMED_NOTIFY_TYPE:
5200 {
5201 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5202 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5203 "qla_target(%d): Received ATIO packet %x "
5204 "with error status %x\n", vha->vp_idx,
5205 atio->u.raw.entry_type,
5206 atio->u.isp2x.entry_status);
5207 break;
5208 }
5209 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005210
5211 if (!ha_locked)
5212 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005213 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005214 if (!ha_locked)
5215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005216 break;
5217 }
5218
5219 default:
5220 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5221 "qla_target(%d): Received unknown ATIO atio "
5222 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5223 break;
5224 }
5225
Quinn Tran2f424b92015-12-17 14:57:07 -05005226 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005227}
5228
5229/* ha->hardware_lock supposed to be held on entry */
5230/* called via callback from qla2xxx */
5231static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5232{
5233 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005234 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005235
5236 if (unlikely(tgt == NULL)) {
5237 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5238 "qla_target(%d): Response pkt %x received, but no "
5239 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5240 return;
5241 }
5242
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005243 /*
5244 * In tgt_stop mode we also should allow all requests to pass.
5245 * Otherwise, some commands can stuck.
5246 */
5247
5248 tgt->irq_cmd_count++;
5249
5250 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005251 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005252 case CTIO_TYPE7:
5253 {
5254 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005255 qlt_do_ctio_completion(vha, entry->handle,
5256 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5257 entry);
5258 break;
5259 }
5260
5261 case ACCEPT_TGT_IO_TYPE:
5262 {
5263 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5264 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005265 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005266 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005267 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5268 "qla_target(%d): ATIO with error "
5269 "status %x received\n", vha->vp_idx,
5270 le16_to_cpu(atio->u.isp2x.status));
5271 break;
5272 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005273
Quinn Tran8b666802017-03-15 09:48:45 -07005274 rc = qlt_chk_qfull_thresh_hold(vha, atio, true);
Quinn Tran33e79972014-09-25 06:14:55 -04005275 if (rc != 0) {
5276 tgt->irq_cmd_count--;
5277 return;
5278 }
5279
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005280 rc = qlt_handle_cmd_for_atio(vha, atio);
5281 if (unlikely(rc != 0)) {
5282 if (rc == -ESRCH) {
5283#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5284 qlt_send_busy(vha, atio, 0);
5285#else
Quinn Trana07100e2015-12-07 19:48:57 -05005286 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005287#endif
5288 } else {
5289 if (tgt->tgt_stop) {
5290 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5291 "qla_target: Unable to send "
5292 "command to target, sending TERM "
5293 "EXCHANGE for rsp\n");
5294 qlt_send_term_exchange(vha, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005295 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005296 } else {
5297 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5298 "qla_target(%d): Unable to send "
5299 "command to target, sending BUSY "
5300 "status\n", vha->vp_idx);
5301 qlt_send_busy(vha, atio, 0);
5302 }
5303 }
5304 }
5305 }
5306 break;
5307
5308 case CONTINUE_TGT_IO_TYPE:
5309 {
5310 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005311 qlt_do_ctio_completion(vha, entry->handle,
5312 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5313 entry);
5314 break;
5315 }
5316
5317 case CTIO_A64_TYPE:
5318 {
5319 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005320 qlt_do_ctio_completion(vha, entry->handle,
5321 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5322 entry);
5323 break;
5324 }
5325
5326 case IMMED_NOTIFY_TYPE:
5327 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5328 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5329 break;
5330
5331 case NOTIFY_ACK_TYPE:
5332 if (tgt->notify_ack_expected > 0) {
5333 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5334 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5335 "NOTIFY_ACK seq %08x status %x\n",
5336 le16_to_cpu(entry->u.isp2x.seq_id),
5337 le16_to_cpu(entry->u.isp2x.status));
5338 tgt->notify_ack_expected--;
5339 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005340 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005341 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5342 "qla_target(%d): NOTIFY_ACK "
5343 "failed %x\n", vha->vp_idx,
5344 le16_to_cpu(entry->u.isp2x.status));
5345 }
5346 } else {
5347 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5348 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5349 vha->vp_idx);
5350 }
5351 break;
5352
5353 case ABTS_RECV_24XX:
5354 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5355 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5356 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5357 break;
5358
5359 case ABTS_RESP_24XX:
5360 if (tgt->abts_resp_expected > 0) {
5361 struct abts_resp_from_24xx_fw *entry =
5362 (struct abts_resp_from_24xx_fw *)pkt;
5363 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5364 "ABTS_RESP_24XX: compl_status %x\n",
5365 entry->compl_status);
5366 tgt->abts_resp_expected--;
5367 if (le16_to_cpu(entry->compl_status) !=
5368 ABTS_RESP_COMPL_SUCCESS) {
5369 if ((entry->error_subcode1 == 0x1E) &&
5370 (entry->error_subcode2 == 0)) {
5371 /*
5372 * We've got a race here: aborted
5373 * exchange not terminated, i.e.
5374 * response for the aborted command was
5375 * sent between the abort request was
5376 * received and processed.
5377 * Unfortunately, the firmware has a
5378 * silly requirement that all aborted
5379 * exchanges must be explicitely
5380 * terminated, otherwise it refuses to
5381 * send responses for the abort
5382 * requests. So, we have to
5383 * (re)terminate the exchange and retry
5384 * the abort response.
5385 */
5386 qlt_24xx_retry_term_exchange(vha,
5387 entry);
5388 } else
5389 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5390 "qla_target(%d): ABTS_RESP_24XX "
5391 "failed %x (subcode %x:%x)",
5392 vha->vp_idx, entry->compl_status,
5393 entry->error_subcode1,
5394 entry->error_subcode2);
5395 }
5396 } else {
5397 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5398 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5399 "received\n", vha->vp_idx);
5400 }
5401 break;
5402
5403 default:
5404 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5405 "qla_target(%d): Received unknown response pkt "
5406 "type %x\n", vha->vp_idx, pkt->entry_type);
5407 break;
5408 }
5409
5410 tgt->irq_cmd_count--;
5411}
5412
5413/*
5414 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5415 */
5416void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5417 uint16_t *mailbox)
5418{
5419 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005420 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005421 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005422
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005423 if (!ha->tgt.tgt_ops)
5424 return;
5425
5426 if (unlikely(tgt == NULL)) {
5427 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5428 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5429 return;
5430 }
5431
5432 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5433 IS_QLA2100(ha))
5434 return;
5435 /*
5436 * In tgt_stop mode we also should allow all requests to pass.
5437 * Otherwise, some commands can stuck.
5438 */
5439
5440 tgt->irq_cmd_count++;
5441
5442 switch (code) {
5443 case MBA_RESET: /* Reset */
5444 case MBA_SYSTEM_ERR: /* System Error */
5445 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5446 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5447 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5448 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005449 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005450 break;
5451 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5452 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5453 break;
5454
5455 case MBA_LOOP_UP:
5456 {
5457 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005458 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005459 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5460 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5461 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005462 if (tgt->link_reinit_iocb_pending) {
5463 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5464 0, 0, 0, 0, 0, 0);
5465 tgt->link_reinit_iocb_pending = 0;
5466 }
5467 break;
5468 }
5469
5470 case MBA_LIP_OCCURRED:
5471 case MBA_LOOP_DOWN:
5472 case MBA_LIP_RESET:
5473 case MBA_RSCN_UPDATE:
5474 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005475 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005476 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5477 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5478 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005479 break;
5480
Quinn Tranead03852017-01-19 22:28:01 -08005481 case MBA_REJECTED_FCP_CMD:
5482 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
5483 "qla_target(%d): Async event LS_REJECT occurred "
5484 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5485 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5486 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5487
5488 if (le16_to_cpu(mailbox[3]) == 1) {
5489 /* exchange starvation. */
5490 vha->hw->exch_starvation++;
5491 if (vha->hw->exch_starvation > 5) {
5492 ql_log(ql_log_warn, vha, 0xffff,
5493 "Exchange starvation-. Resetting RISC\n");
5494
5495 vha->hw->exch_starvation = 0;
5496 if (IS_P3P_TYPE(vha->hw))
5497 set_bit(FCOE_CTX_RESET_NEEDED,
5498 &vha->dpc_flags);
5499 else
5500 set_bit(ISP_ABORT_NEEDED,
5501 &vha->dpc_flags);
5502 qla2xxx_wake_dpc(vha);
5503 }
5504 }
5505 break;
5506
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005507 case MBA_PORT_UPDATE:
5508 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5509 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005510 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005511 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5512 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5513 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5514
5515 login_code = le16_to_cpu(mailbox[2]);
Quinn Tranead03852017-01-19 22:28:01 -08005516 if (login_code == 0x4) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005517 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5518 "Async MB 2: Got PLOGI Complete\n");
Quinn Tranead03852017-01-19 22:28:01 -08005519 vha->hw->exch_starvation = 0;
5520 } else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005521 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5522 "Async MB 2: Port Logged Out\n");
5523 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005524 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005525 break;
5526 }
5527
5528 tgt->irq_cmd_count--;
5529}
5530
5531static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5532 uint16_t loop_id)
5533{
Quinn Tran726b8542017-01-19 22:28:00 -08005534 fc_port_t *fcport, *tfcp, *del;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005535 int rc;
Quinn Tran726b8542017-01-19 22:28:00 -08005536 unsigned long flags;
5537 u8 newfcport = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005538
5539 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5540 if (!fcport) {
5541 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5542 "qla_target(%d): Allocation of tmp FC port failed",
5543 vha->vp_idx);
5544 return NULL;
5545 }
5546
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005547 fcport->loop_id = loop_id;
5548
5549 rc = qla2x00_get_port_database(vha, fcport, 0);
5550 if (rc != QLA_SUCCESS) {
5551 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5552 "qla_target(%d): Failed to retrieve fcport "
5553 "information -- get_port_database() returned %x "
5554 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5555 kfree(fcport);
5556 return NULL;
5557 }
5558
Quinn Tran726b8542017-01-19 22:28:00 -08005559 del = NULL;
5560 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5561 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
5562
5563 if (tfcp) {
5564 tfcp->d_id = fcport->d_id;
5565 tfcp->port_type = fcport->port_type;
5566 tfcp->supported_classes = fcport->supported_classes;
5567 tfcp->flags |= fcport->flags;
5568
5569 del = fcport;
5570 fcport = tfcp;
5571 } else {
5572 if (vha->hw->current_topology == ISP_CFG_F)
5573 fcport->flags |= FCF_FABRIC_DEVICE;
5574
5575 list_add_tail(&fcport->list, &vha->vp_fcports);
5576 if (!IS_SW_RESV_ADDR(fcport->d_id))
5577 vha->fcport_count++;
5578 fcport->login_gen++;
5579 fcport->disc_state = DSC_LOGIN_COMPLETE;
5580 fcport->login_succ = 1;
5581 newfcport = 1;
5582 }
5583
5584 fcport->deleted = 0;
5585 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5586
5587 switch (vha->host->active_mode) {
5588 case MODE_INITIATOR:
5589 case MODE_DUAL:
5590 if (newfcport) {
5591 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
5592 ql_dbg(ql_dbg_disc, vha, 0xffff,
5593 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
5594 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5595 qla24xx_post_upd_fcport_work(vha, fcport);
5596 } else {
5597 ql_dbg(ql_dbg_disc, vha, 0xffff,
5598 "%s %d %8phC post gpsc fcp_cnt %d\n",
5599 __func__, __LINE__, fcport->port_name, vha->fcport_count);
5600 qla24xx_post_gpsc_work(vha, fcport);
5601 }
5602 }
5603 break;
5604
5605 case MODE_TARGET:
5606 default:
5607 break;
5608 }
5609 if (del)
5610 qla2x00_free_fcport(del);
5611
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005612 return fcport;
5613}
5614
5615/* Must be called under tgt_mutex */
Quinn Tran5d964832017-01-19 22:27:59 -08005616static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005617 uint8_t *s_id)
5618{
Quinn Tran5d964832017-01-19 22:27:59 -08005619 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005620 fc_port_t *fcport = NULL;
5621 int rc, global_resets;
5622 uint16_t loop_id = 0;
5623
Quinn Tran726b8542017-01-19 22:28:00 -08005624 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
5625 /*
5626 * This is Domain Controller, so it should be
5627 * OK to drop SCSI commands from it.
5628 */
5629 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5630 "Unable to find initiator with S_ID %x:%x:%x",
5631 s_id[0], s_id[1], s_id[2]);
5632 return NULL;
5633 }
5634
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005635 mutex_lock(&vha->vha_tgt.tgt_mutex);
5636
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005637retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005638 global_resets =
5639 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005640
5641 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5642 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005643 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5644
Quinn Tran726b8542017-01-19 22:28:00 -08005645 ql_log(ql_log_info, vha, 0xf071,
5646 "qla_target(%d): Unable to find "
5647 "initiator with S_ID %x:%x:%x",
5648 vha->vp_idx, s_id[0], s_id[1],
5649 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005650
5651 if (rc == -ENOENT) {
5652 qlt_port_logo_t logo;
5653 sid_to_portid(s_id, &logo.id);
5654 logo.cmd_count = 1;
5655 qlt_send_first_logo(vha, &logo);
5656 }
5657
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005658 return NULL;
5659 }
5660
5661 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005662 if (!fcport) {
5663 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005664 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005665 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005666
5667 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005668 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005669 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5670 "qla_target(%d): global reset during session discovery "
5671 "(counter was %d, new %d), retrying", vha->vp_idx,
5672 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005673 atomic_read(&vha->vha_tgt.
5674 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005675 goto retry;
5676 }
5677
5678 sess = qlt_create_sess(vha, fcport, true);
5679
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005680 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5681
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005682 return sess;
5683}
5684
5685static void qlt_abort_work(struct qla_tgt *tgt,
5686 struct qla_tgt_sess_work_param *prm)
5687{
5688 struct scsi_qla_host *vha = tgt->vha;
5689 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005690 struct fc_port *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005691 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005692 uint32_t be_s_id;
5693 uint8_t s_id[3];
5694 int rc;
5695
Quinn Tran75601512015-12-17 14:57:04 -05005696 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005697
5698 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005699 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005700
5701 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5702 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5703 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5704
5705 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5706 (unsigned char *)&be_s_id);
5707 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005708 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005709
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005710 sess = qlt_make_local_sess(vha, s_id);
5711 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005712
Quinn Tran75601512015-12-17 14:57:04 -05005713 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005714 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005715 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005716 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005717 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005718 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005719 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005720 }
5721
Quinn Tran726b8542017-01-19 22:28:00 -08005722 if (!kref_get_unless_zero(&sess->sess_kref)) {
5723 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
5724 "%s: kref_get fail %8phC \n",
5725 __func__, sess->port_name);
5726 sess = NULL;
5727 goto out_term2;
5728 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005729 }
5730
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005731 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005732 ha->tgt.tgt_ops->put_sess(sess);
5733 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
5734
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005735 if (rc != 0)
5736 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005737 return;
5738
Quinn Tran75601512015-12-17 14:57:04 -05005739out_term2:
Quinn Tran726b8542017-01-19 22:28:00 -08005740 if (sess)
5741 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005742 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005743
5744out_term:
5745 spin_lock_irqsave(&ha->hardware_lock, flags);
5746 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
5747 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005748}
5749
5750static void qlt_tmr_work(struct qla_tgt *tgt,
5751 struct qla_tgt_sess_work_param *prm)
5752{
5753 struct atio_from_isp *a = &prm->tm_iocb2;
5754 struct scsi_qla_host *vha = tgt->vha;
5755 struct qla_hw_data *ha = vha->hw;
Quinn Tran5d964832017-01-19 22:27:59 -08005756 struct fc_port *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005757 unsigned long flags;
5758 uint8_t *s_id = NULL; /* to hide compiler warnings */
5759 int rc;
5760 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005761 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005762 void *iocb;
5763
Quinn Tran75601512015-12-17 14:57:04 -05005764 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005765
5766 if (tgt->tgt_stop)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005767 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005768
5769 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5770 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5771 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005772 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005773
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005774 sess = qlt_make_local_sess(vha, s_id);
5775 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005776
Quinn Tran75601512015-12-17 14:57:04 -05005777 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005778 if (!sess)
Quinn Tranf159b3c2017-03-15 09:48:47 -07005779 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005780 } else {
Quinn Tran726b8542017-01-19 22:28:00 -08005781 if (sess->deleted) {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005782 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07005783 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005784 }
5785
Quinn Tran726b8542017-01-19 22:28:00 -08005786 if (!kref_get_unless_zero(&sess->sess_kref)) {
5787 ql_dbg(ql_dbg_tgt_tmr, vha, 0xffff,
5788 "%s: kref_get fail %8phC\n",
5789 __func__, sess->port_name);
5790 sess = NULL;
Quinn Tranf159b3c2017-03-15 09:48:47 -07005791 goto out_term2;
Quinn Tran726b8542017-01-19 22:28:00 -08005792 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005793 }
5794
5795 iocb = a;
5796 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005797 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5798 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5799
5800 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
Quinn Tranf159b3c2017-03-15 09:48:47 -07005801 ha->tgt.tgt_ops->put_sess(sess);
5802 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5803
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005804 if (rc != 0)
5805 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005806 return;
5807
Quinn Tranf159b3c2017-03-15 09:48:47 -07005808out_term2:
5809 if (sess)
5810 ha->tgt.tgt_ops->put_sess(sess);
5811 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005812out_term:
Quinn Trana07100e2015-12-07 19:48:57 -05005813 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005814}
5815
5816static void qlt_sess_work_fn(struct work_struct *work)
5817{
5818 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5819 struct scsi_qla_host *vha = tgt->vha;
5820 unsigned long flags;
5821
5822 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5823
5824 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5825 while (!list_empty(&tgt->sess_works_list)) {
5826 struct qla_tgt_sess_work_param *prm = list_entry(
5827 tgt->sess_works_list.next, typeof(*prm),
5828 sess_works_list_entry);
5829
5830 /*
5831 * This work can be scheduled on several CPUs at time, so we
5832 * must delete the entry to eliminate double processing
5833 */
5834 list_del(&prm->sess_works_list_entry);
5835
5836 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5837
5838 switch (prm->type) {
5839 case QLA_TGT_SESS_WORK_ABORT:
5840 qlt_abort_work(tgt, prm);
5841 break;
5842 case QLA_TGT_SESS_WORK_TM:
5843 qlt_tmr_work(tgt, prm);
5844 break;
5845 default:
5846 BUG_ON(1);
5847 break;
5848 }
5849
5850 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5851
5852 kfree(prm);
5853 }
5854 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5855}
5856
5857/* Must be called under tgt_host_action_mutex */
5858int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5859{
5860 struct qla_tgt *tgt;
5861
5862 if (!QLA_TGT_MODE_ENABLED())
5863 return 0;
5864
Arun Easi33c36c02013-01-30 03:34:41 -05005865 if (!IS_TGT_MODE_CAPABLE(ha)) {
5866 ql_log(ql_log_warn, base_vha, 0xe070,
5867 "This adapter does not support target mode.\n");
5868 return 0;
5869 }
5870
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005871 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005872 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005873
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005874 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005875
5876 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5877 if (!tgt) {
5878 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5879 "Unable to allocate struct qla_tgt\n");
5880 return -ENOMEM;
5881 }
5882
5883 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5884 base_vha->host->hostt->supported_mode |= MODE_TARGET;
5885
5886 tgt->ha = ha;
5887 tgt->vha = base_vha;
5888 init_waitqueue_head(&tgt->waitQ);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005889 INIT_LIST_HEAD(&tgt->del_sess_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005890 spin_lock_init(&tgt->sess_work_lock);
5891 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
5892 INIT_LIST_HEAD(&tgt->sess_works_list);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005893 atomic_set(&tgt->tgt_global_resets_count, 0);
5894
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005895 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005896
5897 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
5898 "qla_target(%d): using 64 Bit PCI addressing",
5899 base_vha->vp_idx);
5900 tgt->tgt_enable_64bit_addr = 1;
5901 /* 3 is reserved */
5902 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
5903 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
5904 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
5905
Nicholas Bellingerddb95142014-02-19 17:51:25 -08005906 if (base_vha->fc_vport)
5907 return 0;
5908
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005909 mutex_lock(&qla_tgt_mutex);
5910 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
5911 mutex_unlock(&qla_tgt_mutex);
5912
5913 return 0;
5914}
5915
5916/* Must be called under tgt_host_action_mutex */
5917int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
5918{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005919 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005920 return 0;
5921
Nicholas Bellingerddb95142014-02-19 17:51:25 -08005922 if (vha->fc_vport) {
5923 qlt_release(vha->vha_tgt.qla_tgt);
5924 return 0;
5925 }
Quinn Tran33e79972014-09-25 06:14:55 -04005926
5927 /* free left over qfull cmds */
5928 qlt_init_term_exchange(vha);
5929
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005930 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005931 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005932 mutex_unlock(&qla_tgt_mutex);
5933
5934 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
5935 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005936 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005937
5938 return 0;
5939}
5940
5941static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
5942 unsigned char *b)
5943{
5944 int i;
5945
5946 pr_debug("qla2xxx HW vha->node_name: ");
5947 for (i = 0; i < WWN_SIZE; i++)
5948 pr_debug("%02x ", vha->node_name[i]);
5949 pr_debug("\n");
5950 pr_debug("qla2xxx HW vha->port_name: ");
5951 for (i = 0; i < WWN_SIZE; i++)
5952 pr_debug("%02x ", vha->port_name[i]);
5953 pr_debug("\n");
5954
5955 pr_debug("qla2xxx passed configfs WWPN: ");
5956 put_unaligned_be64(wwpn, b);
5957 for (i = 0; i < WWN_SIZE; i++)
5958 pr_debug("%02x ", b[i]);
5959 pr_debug("\n");
5960}
5961
5962/**
5963 * qla_tgt_lport_register - register lport with external module
5964 *
5965 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
5966 * @wwpn: Passwd FC target WWPN
5967 * @callback: lport initialization callback for tcm_qla2xxx code
5968 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
5969 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08005970int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
5971 u64 npiv_wwpn, u64 npiv_wwnn,
5972 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005973{
5974 struct qla_tgt *tgt;
5975 struct scsi_qla_host *vha;
5976 struct qla_hw_data *ha;
5977 struct Scsi_Host *host;
5978 unsigned long flags;
5979 int rc;
5980 u8 b[WWN_SIZE];
5981
5982 mutex_lock(&qla_tgt_mutex);
5983 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
5984 vha = tgt->vha;
5985 ha = vha->hw;
5986
5987 host = vha->host;
5988 if (!host)
5989 continue;
5990
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005991 if (!(host->hostt->supported_mode & MODE_TARGET))
5992 continue;
5993
5994 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08005995 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005996 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
5997 host->host_no);
5998 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5999 continue;
6000 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006001 if (tgt->tgt_stop) {
6002 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6003 host->host_no);
6004 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6005 continue;
6006 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006007 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6008
6009 if (!scsi_host_get(host)) {
6010 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6011 "Unable to scsi_host_get() for"
6012 " qla2xxx scsi_host\n");
6013 continue;
6014 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006015 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006016
6017 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6018 scsi_host_put(host);
6019 continue;
6020 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006021 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6022 if (rc != 0)
6023 scsi_host_put(host);
6024
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006025 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006026 return rc;
6027 }
6028 mutex_unlock(&qla_tgt_mutex);
6029
6030 return -ENODEV;
6031}
6032EXPORT_SYMBOL(qlt_lport_register);
6033
6034/**
6035 * qla_tgt_lport_deregister - Degister lport
6036 *
6037 * @vha: Registered scsi_qla_host pointer
6038 */
6039void qlt_lport_deregister(struct scsi_qla_host *vha)
6040{
6041 struct qla_hw_data *ha = vha->hw;
6042 struct Scsi_Host *sh = vha->host;
6043 /*
6044 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6045 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006046 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006047 ha->tgt.tgt_ops = NULL;
6048 /*
6049 * Release the Scsi_Host reference for the underlying qla2xxx host
6050 */
6051 scsi_host_put(sh);
6052}
6053EXPORT_SYMBOL(qlt_lport_deregister);
6054
6055/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006056static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006057{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006058 switch (ql2x_ini_mode) {
6059 case QLA2XXX_INI_MODE_DISABLED:
6060 case QLA2XXX_INI_MODE_EXCLUSIVE:
6061 vha->host->active_mode = MODE_TARGET;
6062 break;
6063 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006064 vha->host->active_mode = MODE_UNKNOWN;
6065 break;
6066 case QLA2XXX_INI_MODE_DUAL:
6067 vha->host->active_mode = MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006068 break;
6069 default:
6070 break;
6071 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006072}
6073
6074/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006075static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006076{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006077 switch (ql2x_ini_mode) {
6078 case QLA2XXX_INI_MODE_DISABLED:
6079 vha->host->active_mode = MODE_UNKNOWN;
6080 break;
6081 case QLA2XXX_INI_MODE_EXCLUSIVE:
6082 vha->host->active_mode = MODE_INITIATOR;
6083 break;
6084 case QLA2XXX_INI_MODE_ENABLED:
Quinn Tranead03852017-01-19 22:28:01 -08006085 case QLA2XXX_INI_MODE_DUAL:
6086 vha->host->active_mode = MODE_INITIATOR;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006087 break;
6088 default:
6089 break;
6090 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006091}
6092
6093/*
6094 * qla_tgt_enable_vha - NO LOCK HELD
6095 *
6096 * host_reset, bring up w/ Target Mode Enabled
6097 */
6098void
6099qlt_enable_vha(struct scsi_qla_host *vha)
6100{
6101 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006102 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006103 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006104 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Trancdb898c2015-12-17 14:57:05 -05006105 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006106
6107 if (!tgt) {
6108 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6109 "Unable to locate qla_tgt pointer from"
6110 " struct qla_hw_data\n");
6111 dump_stack();
6112 return;
6113 }
6114
6115 spin_lock_irqsave(&ha->hardware_lock, flags);
6116 tgt->tgt_stopped = 0;
6117 qlt_set_mode(vha);
6118 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6119
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006120 if (vha->vp_idx) {
6121 qla24xx_disable_vp(vha);
6122 qla24xx_enable_vp(vha);
6123 } else {
Quinn Trancdb898c2015-12-17 14:57:05 -05006124 if (ha->msix_entries) {
6125 ql_dbg(ql_dbg_tgt, vha, 0xffff,
6126 "%s: host%ld : vector %d cpu %d\n",
6127 __func__, vha->host_no,
6128 ha->msix_entries[rspq_ent].vector,
6129 ha->msix_entries[rspq_ent].cpuid);
6130
6131 ha->tgt.rspq_vector_cpuid =
6132 ha->msix_entries[rspq_ent].cpuid;
6133 }
6134
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006135 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6136 qla2xxx_wake_dpc(base_vha);
6137 qla2x00_wait_for_hba_online(base_vha);
6138 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006139}
6140EXPORT_SYMBOL(qlt_enable_vha);
6141
6142/*
6143 * qla_tgt_disable_vha - NO LOCK HELD
6144 *
6145 * Disable Target Mode and reset the adapter
6146 */
Joern Engel55a90662014-09-16 16:23:15 -04006147static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006148{
6149 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006150 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006151 unsigned long flags;
6152
6153 if (!tgt) {
6154 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6155 "Unable to locate qla_tgt pointer from"
6156 " struct qla_hw_data\n");
6157 dump_stack();
6158 return;
6159 }
6160
6161 spin_lock_irqsave(&ha->hardware_lock, flags);
6162 qlt_clear_mode(vha);
6163 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6164
6165 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6166 qla2xxx_wake_dpc(vha);
6167 qla2x00_wait_for_hba_online(vha);
6168}
6169
6170/*
6171 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6172 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6173 * members.
6174 */
6175void
6176qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6177{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006178 vha->vha_tgt.qla_tgt = NULL;
6179
6180 mutex_init(&vha->vha_tgt.tgt_mutex);
6181 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006182
6183 qlt_clear_mode(vha);
6184
6185 /*
6186 * NOTE: Currently the value is kept the same for <24xx and
6187 * >=24xx ISPs. If it is necessary to change it,
6188 * the check should be added for specific ISPs,
6189 * assigning the value appropriately.
6190 */
6191 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006192
6193 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006194}
6195
6196void
6197qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6198{
6199 /*
6200 * FC-4 Feature bit 0 indicates target functionality to the name server.
6201 */
6202 if (qla_tgt_mode_enabled(vha)) {
Quinn Tran726b8542017-01-19 22:28:00 -08006203 ct_req->req.rff_id.fc4_feature = BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006204 } else if (qla_ini_mode_enabled(vha)) {
6205 ct_req->req.rff_id.fc4_feature = BIT_1;
Quinn Tran726b8542017-01-19 22:28:00 -08006206 } else if (qla_dual_mode_enabled(vha))
6207 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006208}
6209
6210/*
6211 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6212 * @ha: HA context
6213 *
6214 * Beginning of ATIO ring has initialization control block already built
6215 * by nvram config routine.
6216 *
6217 * Returns 0 on success.
6218 */
6219void
6220qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6221{
6222 struct qla_hw_data *ha = vha->hw;
6223 uint16_t cnt;
6224 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6225
Quinn Tranead03852017-01-19 22:28:01 -08006226 if (qla_ini_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006227 return;
6228
6229 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6230 pkt->u.raw.signature = ATIO_PROCESSED;
6231 pkt++;
6232 }
6233
6234}
6235
6236/*
6237 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6238 * @ha: SCSI driver HA context
6239 */
6240void
Quinn Tran2f424b92015-12-17 14:57:07 -05006241qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006242{
6243 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006244 struct atio_from_isp *pkt;
6245 int cnt, i;
6246
6247 if (!vha->flags.online)
6248 return;
6249
Quinn Tran5f355092016-12-23 18:06:11 -08006250 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6251 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006252 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6253 cnt = pkt->u.raw.entry_count;
6254
Quinn Tran5f355092016-12-23 18:06:11 -08006255 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6256 /*
6257 * This packet is corrupted. The header + payload
6258 * can not be trusted. There is no point in passing
6259 * it further up.
6260 */
6261 ql_log(ql_log_warn, vha, 0xffff,
6262 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6263 pkt->u.isp24.fcp_hdr.s_id,
6264 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6265 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6266
6267 adjust_corrupted_atio(pkt);
6268 qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
6269 } else {
6270 qlt_24xx_atio_pkt_all_vps(vha,
6271 (struct atio_from_isp *)pkt, ha_locked);
6272 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006273
6274 for (i = 0; i < cnt; i++) {
6275 ha->tgt.atio_ring_index++;
6276 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6277 ha->tgt.atio_ring_index = 0;
6278 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6279 } else
6280 ha->tgt.atio_ring_ptr++;
6281
6282 pkt->u.raw.signature = ATIO_PROCESSED;
6283 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6284 }
6285 wmb();
6286 }
6287
6288 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006289 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Quinn Tran3761f3e2015-06-10 11:05:19 -04006290 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006291}
6292
6293void
Arun Easiaa230bc2013-01-30 03:34:39 -05006294qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006295{
6296 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006297 if (!QLA_TGT_MODE_ENABLED())
6298 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006299
Arun Easiaa230bc2013-01-30 03:34:39 -05006300 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6301 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6302 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6303
6304 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6305 struct qla_msix_entry *msix = &ha->msix_entries[2];
6306 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6307
6308 icb->msix_atio = cpu_to_le16(msix->entry);
6309 ql_dbg(ql_dbg_init, vha, 0xf072,
6310 "Registering ICB vector 0x%x for atio que.\n",
6311 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006312 }
6313}
6314
6315void
6316qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6317{
6318 struct qla_hw_data *ha = vha->hw;
Quinn Tranead03852017-01-19 22:28:01 -08006319 u32 tmp;
6320 u16 t;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006321
Quinn Tranead03852017-01-19 22:28:01 -08006322 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006323 if (!ha->tgt.saved_set) {
6324 /* We save only once */
6325 ha->tgt.saved_exchange_count = nv->exchange_count;
6326 ha->tgt.saved_firmware_options_1 =
6327 nv->firmware_options_1;
6328 ha->tgt.saved_firmware_options_2 =
6329 nv->firmware_options_2;
6330 ha->tgt.saved_firmware_options_3 =
6331 nv->firmware_options_3;
6332 ha->tgt.saved_set = 1;
6333 }
6334
Quinn Tranead03852017-01-19 22:28:01 -08006335 if (qla_tgt_mode_enabled(vha)) {
6336 nv->exchange_count = cpu_to_le16(0xFFFF);
6337 } else { /* dual */
6338 if (ql_dm_tgt_ex_pct > 100) {
6339 ql_dm_tgt_ex_pct = 50;
6340 } else if (ql_dm_tgt_ex_pct == 100) {
6341 /* leave some for FW */
6342 ql_dm_tgt_ex_pct = 95;
6343 }
6344
6345 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6346 tmp = tmp/100;
6347 if (tmp > 0xffff)
6348 tmp = 0xffff;
6349
6350 t = tmp & 0xffff;
6351 nv->exchange_count = cpu_to_le16(t);
6352 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006353
6354 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006355 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006356
6357 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006358 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006359 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006360
6361 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006362 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006363 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006364 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006365 if (ql2xtgt_tape_enable)
6366 /* Enable FC Tape support */
6367 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6368 else
6369 /* Disable FC Tape support */
6370 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6371
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006372 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006373 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006374
6375 /*
6376 * clear BIT 15 explicitly as we have seen at least
6377 * a couple of instances where this was set and this
6378 * was causing the firmware to not be initialized.
6379 */
6380 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006381 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006382 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006383 } else {
6384 if (ha->tgt.saved_set) {
6385 nv->exchange_count = ha->tgt.saved_exchange_count;
6386 nv->firmware_options_1 =
6387 ha->tgt.saved_firmware_options_1;
6388 nv->firmware_options_2 =
6389 ha->tgt.saved_firmware_options_2;
6390 nv->firmware_options_3 =
6391 ha->tgt.saved_firmware_options_3;
6392 }
6393 return;
6394 }
6395
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006396 if (ha->tgt.enable_class_2) {
6397 if (vha->flags.init_done)
6398 fc_host_supported_classes(vha->host) =
6399 FC_COS_CLASS2 | FC_COS_CLASS3;
6400
Bart Van Asschead950362015-07-09 07:24:08 -07006401 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006402 } else {
6403 if (vha->flags.init_done)
6404 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6405
Bart Van Asschead950362015-07-09 07:24:08 -07006406 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006407 }
6408}
6409
6410void
6411qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6412 struct init_cb_24xx *icb)
6413{
6414 struct qla_hw_data *ha = vha->hw;
6415
Quinn Tran481ce732015-12-17 14:57:08 -05006416 if (!QLA_TGT_MODE_ENABLED())
6417 return;
6418
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006419 if (ha->tgt.node_name_set) {
6420 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006421 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006422 }
Quinn Tran481ce732015-12-17 14:57:08 -05006423
6424 /* disable ZIO at start time. */
6425 if (!vha->flags.init_done) {
6426 uint32_t tmp;
6427 tmp = le32_to_cpu(icb->firmware_options_2);
6428 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6429 icb->firmware_options_2 = cpu_to_le32(tmp);
6430 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006431}
6432
Arun Easiaa230bc2013-01-30 03:34:39 -05006433void
6434qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6435{
6436 struct qla_hw_data *ha = vha->hw;
Quinn Tranead03852017-01-19 22:28:01 -08006437 u32 tmp;
6438 u16 t;
Arun Easiaa230bc2013-01-30 03:34:39 -05006439
6440 if (!QLA_TGT_MODE_ENABLED())
6441 return;
6442
Quinn Tranead03852017-01-19 22:28:01 -08006443 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006444 if (!ha->tgt.saved_set) {
6445 /* We save only once */
6446 ha->tgt.saved_exchange_count = nv->exchange_count;
6447 ha->tgt.saved_firmware_options_1 =
6448 nv->firmware_options_1;
6449 ha->tgt.saved_firmware_options_2 =
6450 nv->firmware_options_2;
6451 ha->tgt.saved_firmware_options_3 =
6452 nv->firmware_options_3;
6453 ha->tgt.saved_set = 1;
6454 }
6455
Quinn Tranead03852017-01-19 22:28:01 -08006456 if (qla_tgt_mode_enabled(vha)) {
6457 nv->exchange_count = cpu_to_le16(0xFFFF);
6458 } else { /* dual */
6459 if (ql_dm_tgt_ex_pct > 100) {
6460 ql_dm_tgt_ex_pct = 50;
6461 } else if (ql_dm_tgt_ex_pct == 100) {
6462 /* leave some for FW */
6463 ql_dm_tgt_ex_pct = 95;
6464 }
6465
6466 tmp = ha->orig_fw_xcb_count * ql_dm_tgt_ex_pct;
6467 tmp = tmp/100;
6468 if (tmp > 0xffff)
6469 tmp = 0xffff;
6470 t = tmp & 0xffff;
6471 nv->exchange_count = cpu_to_le16(t);
6472 }
Arun Easiaa230bc2013-01-30 03:34:39 -05006473
6474 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006475 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006476
6477 /* Disable ini mode, if requested */
Quinn Tran726b8542017-01-19 22:28:00 -08006478 if (qla_tgt_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006479 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006480 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006481 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006482 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006483 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006484 /*
6485 * clear BIT 15 explicitly as we have seen at
6486 * least a couple of instances where this was set
6487 * and this was causing the firmware to not be
6488 * initialized.
6489 */
6490 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006491 if (ql2xtgt_tape_enable)
6492 /* Enable FC tape support */
6493 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6494 else
6495 /* Disable FC tape support */
6496 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6497
Arun Easiaa230bc2013-01-30 03:34:39 -05006498 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006499 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006500 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006501 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006502 } else {
6503 if (ha->tgt.saved_set) {
6504 nv->exchange_count = ha->tgt.saved_exchange_count;
6505 nv->firmware_options_1 =
6506 ha->tgt.saved_firmware_options_1;
6507 nv->firmware_options_2 =
6508 ha->tgt.saved_firmware_options_2;
6509 nv->firmware_options_3 =
6510 ha->tgt.saved_firmware_options_3;
6511 }
6512 return;
6513 }
6514
Arun Easiaa230bc2013-01-30 03:34:39 -05006515 if (ha->tgt.enable_class_2) {
6516 if (vha->flags.init_done)
6517 fc_host_supported_classes(vha->host) =
6518 FC_COS_CLASS2 | FC_COS_CLASS3;
6519
Bart Van Asschead950362015-07-09 07:24:08 -07006520 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006521 } else {
6522 if (vha->flags.init_done)
6523 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6524
Bart Van Asschead950362015-07-09 07:24:08 -07006525 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006526 }
6527}
6528
6529void
6530qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6531 struct init_cb_81xx *icb)
6532{
6533 struct qla_hw_data *ha = vha->hw;
6534
6535 if (!QLA_TGT_MODE_ENABLED())
6536 return;
6537
6538 if (ha->tgt.node_name_set) {
6539 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006540 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006541 }
Quinn Tran481ce732015-12-17 14:57:08 -05006542
6543 /* disable ZIO at start time. */
6544 if (!vha->flags.init_done) {
6545 uint32_t tmp;
6546 tmp = le32_to_cpu(icb->firmware_options_2);
6547 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6548 icb->firmware_options_2 = cpu_to_le32(tmp);
6549 }
6550
Arun Easiaa230bc2013-01-30 03:34:39 -05006551}
6552
6553void
6554qlt_83xx_iospace_config(struct qla_hw_data *ha)
6555{
6556 if (!QLA_TGT_MODE_ENABLED())
6557 return;
6558
6559 ha->msix_count += 1; /* For ATIO Q */
6560}
6561
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006562int
6563qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6564 struct sts_entry_24xx *pkt)
6565{
6566 switch (pkt->entry_type) {
6567 case ABTS_RECV_24XX:
6568 case ABTS_RESP_24XX:
6569 case CTIO_TYPE7:
6570 case NOTIFY_ACK_TYPE:
Quinn Tranf83adb62014-04-11 16:54:43 -04006571 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006572 return 1;
6573 default:
6574 return 0;
6575 }
6576}
6577
6578void
6579qlt_modify_vp_config(struct scsi_qla_host *vha,
6580 struct vp_config_entry_24xx *vpmod)
6581{
Quinn Tranead03852017-01-19 22:28:01 -08006582 /* enable target mode. Bit5 = 1 => disable */
6583 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006584 vpmod->options_idx1 &= ~BIT_5;
Quinn Tran726b8542017-01-19 22:28:00 -08006585
Quinn Tranead03852017-01-19 22:28:01 -08006586 /* Disable ini mode, if requested. bit4 = 1 => disable */
Quinn Tran726b8542017-01-19 22:28:00 -08006587 if (qla_tgt_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006588 vpmod->options_idx1 &= ~BIT_4;
6589}
6590
6591void
6592qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6593{
6594 if (!QLA_TGT_MODE_ENABLED())
6595 return;
6596
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04006597 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006598 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6599 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6600 } else {
6601 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6602 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6603 }
6604
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006605 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6606 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Quinn Tran41dc5292017-01-19 22:28:03 -08006607
6608 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
6609 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
6610 qlt_unknown_atio_work_fn);
6611
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006612 qlt_clear_mode(base_vha);
6613}
6614
Arun Easiaa230bc2013-01-30 03:34:39 -05006615irqreturn_t
6616qla83xx_msix_atio_q(int irq, void *dev_id)
6617{
6618 struct rsp_que *rsp;
6619 scsi_qla_host_t *vha;
6620 struct qla_hw_data *ha;
6621 unsigned long flags;
6622
6623 rsp = (struct rsp_que *) dev_id;
6624 ha = rsp->hw;
6625 vha = pci_get_drvdata(ha->pdev);
6626
Quinn Tran2f424b92015-12-17 14:57:07 -05006627 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006628
Quinn Tran2f424b92015-12-17 14:57:07 -05006629 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006630
Quinn Tran2f424b92015-12-17 14:57:07 -05006631 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006632
6633 return IRQ_HANDLED;
6634}
6635
Quinn Tran2f424b92015-12-17 14:57:07 -05006636static void
6637qlt_handle_abts_recv_work(struct work_struct *work)
6638{
6639 struct qla_tgt_sess_op *op = container_of(work,
6640 struct qla_tgt_sess_op, work);
6641 scsi_qla_host_t *vha = op->vha;
6642 struct qla_hw_data *ha = vha->hw;
6643 unsigned long flags;
6644
6645 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6646 return;
6647
6648 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6649 qlt_24xx_process_atio_queue(vha, 0);
6650 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6651
6652 spin_lock_irqsave(&ha->hardware_lock, flags);
6653 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6654 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tranae940f22017-03-15 09:48:44 -07006655
6656 kfree(op);
Quinn Tran2f424b92015-12-17 14:57:07 -05006657}
6658
6659void
6660qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6661{
6662 struct qla_tgt_sess_op *op;
6663
6664 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6665
6666 if (!op) {
6667 /* do not reach for ATIO queue here. This is best effort err
6668 * recovery at this point.
6669 */
6670 qlt_response_pkt_all_vps(vha, pkt);
6671 return;
6672 }
6673
6674 memcpy(&op->atio, pkt, sizeof(*pkt));
6675 op->vha = vha;
6676 op->chip_reset = vha->hw->chip_reset;
6677 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6678 queue_work(qla_tgt_wq, &op->work);
6679 return;
6680}
6681
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006682int
6683qlt_mem_alloc(struct qla_hw_data *ha)
6684{
6685 if (!QLA_TGT_MODE_ENABLED())
6686 return 0;
6687
6688 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6689 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6690 if (!ha->tgt.tgt_vp_map)
6691 return -ENOMEM;
6692
6693 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6694 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6695 &ha->tgt.atio_dma, GFP_KERNEL);
6696 if (!ha->tgt.atio_ring) {
6697 kfree(ha->tgt.tgt_vp_map);
6698 return -ENOMEM;
6699 }
6700 return 0;
6701}
6702
6703void
6704qlt_mem_free(struct qla_hw_data *ha)
6705{
6706 if (!QLA_TGT_MODE_ENABLED())
6707 return;
6708
6709 if (ha->tgt.atio_ring) {
6710 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6711 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6712 ha->tgt.atio_dma);
6713 }
6714 kfree(ha->tgt.tgt_vp_map);
6715}
6716
6717/* vport_slock to be held by the caller */
6718void
6719qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6720{
6721 if (!QLA_TGT_MODE_ENABLED())
6722 return;
6723
6724 switch (cmd) {
6725 case SET_VP_IDX:
6726 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6727 break;
6728 case SET_AL_PA:
6729 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
6730 break;
6731 case RESET_VP_IDX:
6732 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6733 break;
6734 case RESET_AL_PA:
6735 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
6736 break;
6737 }
6738}
6739
6740static int __init qlt_parse_ini_mode(void)
6741{
6742 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6743 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6744 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6745 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6746 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6747 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
Quinn Tranead03852017-01-19 22:28:01 -08006748 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
6749 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006750 else
6751 return false;
6752
6753 return true;
6754}
6755
6756int __init qlt_init(void)
6757{
6758 int ret;
6759
6760 if (!qlt_parse_ini_mode()) {
6761 ql_log(ql_log_fatal, NULL, 0xe06b,
6762 "qlt_parse_ini_mode() failed\n");
6763 return -EINVAL;
6764 }
6765
6766 if (!QLA_TGT_MODE_ENABLED())
6767 return 0;
6768
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006769 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6770 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6771 qla_tgt_mgmt_cmd), 0, NULL);
6772 if (!qla_tgt_mgmt_cmd_cachep) {
6773 ql_log(ql_log_fatal, NULL, 0xe06d,
6774 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07006775 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006776 }
6777
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006778 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
Quinn Tran5d964832017-01-19 22:27:59 -08006779 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
6780 0, NULL);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006781
6782 if (!qla_tgt_plogi_cachep) {
6783 ql_log(ql_log_fatal, NULL, 0xe06d,
6784 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6785 ret = -ENOMEM;
6786 goto out_mgmt_cmd_cachep;
6787 }
6788
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006789 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6790 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6791 if (!qla_tgt_mgmt_cmd_mempool) {
6792 ql_log(ql_log_fatal, NULL, 0xe06e,
6793 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6794 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006795 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006796 }
6797
6798 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6799 if (!qla_tgt_wq) {
6800 ql_log(ql_log_fatal, NULL, 0xe06f,
6801 "alloc_workqueue for qla_tgt_wq failed\n");
6802 ret = -ENOMEM;
6803 goto out_cmd_mempool;
6804 }
6805 /*
6806 * Return 1 to signal that initiator-mode is being disabled
6807 */
6808 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6809
6810out_cmd_mempool:
6811 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006812out_plogi_cachep:
6813 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006814out_mgmt_cmd_cachep:
6815 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006816 return ret;
6817}
6818
6819void qlt_exit(void)
6820{
6821 if (!QLA_TGT_MODE_ENABLED())
6822 return;
6823
6824 destroy_workqueue(qla_tgt_wq);
6825 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006826 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006827 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006828}