blob: fcdd5c9ed3bf46cd8827ed4fc0a4f485f7106709 [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; "
58 "\"enabled\" (default) - initiator mode will always stay enabled.");
59
Arun Easiaa230bc2013-01-30 03:34:39 -050060int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040061
Quinn Tran33e79972014-09-25 06:14:55 -040062static int temp_sam_status = SAM_STAT_BUSY;
63
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040064/*
65 * From scsi/fc/fc_fcp.h
66 */
67enum fcp_resp_rsp_codes {
68 FCP_TMF_CMPL = 0,
69 FCP_DATA_LEN_INVALID = 1,
70 FCP_CMND_FIELDS_INVALID = 2,
71 FCP_DATA_PARAM_MISMATCH = 3,
72 FCP_TMF_REJECTED = 4,
73 FCP_TMF_FAILED = 5,
74 FCP_TMF_INVALID_LUN = 9,
75};
76
77/*
78 * fc_pri_ta from scsi/fc/fc_fcp.h
79 */
80#define FCP_PTA_SIMPLE 0 /* simple task attribute */
81#define FCP_PTA_HEADQ 1 /* head of queue task attribute */
82#define FCP_PTA_ORDERED 2 /* ordered task attribute */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +090083#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040084#define FCP_PTA_MASK 7 /* mask for task attribute field */
85#define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
86#define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
87
88/*
89 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
90 * must be called under HW lock and could unlock/lock it inside.
91 * It isn't an issue, since in the current implementation on the time when
92 * those functions are called:
93 *
94 * - Either context is IRQ and only IRQ handler can modify HW data,
95 * including rings related fields,
96 *
97 * - Or access to target mode variables from struct qla_tgt doesn't
98 * cross those functions boundaries, except tgt_stop, which
99 * additionally protected by irq_cmd_count.
100 */
101/* Predefs for callbacks handed to qla2xxx LLD */
102static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500103 struct atio_from_isp *pkt, uint8_t);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400104static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
105static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
106 int fn, void *iocb, int flags);
107static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
Quinn Trana07100e2015-12-07 19:48:57 -0500108 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400109static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha,
110 struct qla_tgt_srr_imm *imm, int ha_lock);
Arun Easib6a029e2014-09-25 06:14:52 -0400111static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
112 struct qla_tgt_cmd *cmd);
Quinn Tran33e79972014-09-25 06:14:55 -0400113static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
114 struct atio_from_isp *atio, uint16_t status, int qfull);
Joern Engel55a90662014-09-16 16:23:15 -0400115static void qlt_disable_vha(struct scsi_qla_host *vha);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400116static void qlt_clear_tgt_db(struct qla_tgt *tgt);
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400117static void qlt_send_notify_ack(struct scsi_qla_host *vha,
118 struct imm_ntfy_from_isp *ntfy,
119 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
120 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500121static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
122 struct imm_ntfy_from_isp *imm, int ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400123/*
124 * Global Variables
125 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400126static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500127static struct kmem_cache *qla_tgt_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400128static mempool_t *qla_tgt_mgmt_cmd_mempool;
129static struct workqueue_struct *qla_tgt_wq;
130static DEFINE_MUTEX(qla_tgt_mutex);
131static LIST_HEAD(qla_tgt_glist);
132
Alexei Potashnikdf673272015-07-14 16:00:46 -0400133/* This API intentionally takes dest as a parameter, rather than returning
134 * int value to avoid caller forgetting to issue wmb() after the store */
135void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
136{
137 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
138 *dest = atomic_inc_return(&base_vha->generation_tick);
139 /* memory barrier */
140 wmb();
141}
142
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400143/* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
144static struct qla_tgt_sess *qlt_find_sess_by_port_name(
145 struct qla_tgt *tgt,
146 const uint8_t *port_name)
147{
148 struct qla_tgt_sess *sess;
149
150 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
151 if (!memcmp(sess->port_name, port_name, WWN_SIZE))
152 return sess;
153 }
154
155 return NULL;
156}
157
158/* Might release hw lock, then reaquire!! */
159static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
160{
161 /* Send marker if required */
162 if (unlikely(vha->marker_needed != 0)) {
163 int rc = qla2x00_issue_marker(vha, vha_locked);
164 if (rc != QLA_SUCCESS) {
165 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
166 "qla_target(%d): issue_marker() failed\n",
167 vha->vp_idx);
168 }
169 return rc;
170 }
171 return QLA_SUCCESS;
172}
173
174static inline
175struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
176 uint8_t *d_id)
177{
178 struct qla_hw_data *ha = vha->hw;
179 uint8_t vp_idx;
180
181 if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
182 return NULL;
183
184 if (vha->d_id.b.al_pa == d_id[2])
185 return vha;
186
187 BUG_ON(ha->tgt.tgt_vp_map == NULL);
188 vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
189 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
190 return ha->tgt.tgt_vp_map[vp_idx].vha;
191
192 return NULL;
193}
194
195static inline
196struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
197 uint16_t vp_idx)
198{
199 struct qla_hw_data *ha = vha->hw;
200
201 if (vha->vp_idx == vp_idx)
202 return vha;
203
204 BUG_ON(ha->tgt.tgt_vp_map == NULL);
205 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
206 return ha->tgt.tgt_vp_map[vp_idx].vha;
207
208 return NULL;
209}
210
Quinn Tran33e79972014-09-25 06:14:55 -0400211static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
212{
213 unsigned long flags;
214
215 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
216
217 vha->hw->tgt.num_pend_cmds++;
Joe Carnucciofc90ada2016-07-06 11:14:23 -0400218 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
219 vha->qla_stats.stat_max_pend_cmds =
Quinn Tran33e79972014-09-25 06:14:55 -0400220 vha->hw->tgt.num_pend_cmds;
221 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
222}
223static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
224{
225 unsigned long flags;
226
227 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
228 vha->hw->tgt.num_pend_cmds--;
229 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
230}
231
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500232static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -0500233 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400234{
Quinn Tranf83adb62014-04-11 16:54:43 -0400235 ql_dbg(ql_dbg_tgt, vha, 0xe072,
236 "%s: qla_target(%d): type %x ox_id %04x\n",
237 __func__, vha->vp_idx, atio->u.raw.entry_type,
238 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
239
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400240 switch (atio->u.raw.entry_type) {
241 case ATIO_TYPE7:
242 {
243 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
244 atio->u.isp24.fcp_hdr.d_id);
245 if (unlikely(NULL == host)) {
246 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
247 "qla_target(%d): Received ATIO_TYPE7 "
248 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
249 atio->u.isp24.fcp_hdr.d_id[0],
250 atio->u.isp24.fcp_hdr.d_id[1],
251 atio->u.isp24.fcp_hdr.d_id[2]);
252 break;
253 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500254 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400255 break;
256 }
257
258 case IMMED_NOTIFY_TYPE:
259 {
260 struct scsi_qla_host *host = vha;
261 struct imm_ntfy_from_isp *entry =
262 (struct imm_ntfy_from_isp *)atio;
263
264 if ((entry->u.isp24.vp_index != 0xFF) &&
265 (entry->u.isp24.nport_handle != 0xFFFF)) {
266 host = qlt_find_host_by_vp_idx(vha,
267 entry->u.isp24.vp_index);
268 if (unlikely(!host)) {
269 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
270 "qla_target(%d): Received "
271 "ATIO (IMMED_NOTIFY_TYPE) "
272 "with unknown vp_index %d\n",
273 vha->vp_idx, entry->u.isp24.vp_index);
274 break;
275 }
276 }
Quinn Tran2f424b92015-12-17 14:57:07 -0500277 qlt_24xx_atio_pkt(host, atio, ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400278 break;
279 }
280
281 default:
282 ql_dbg(ql_dbg_tgt, vha, 0xe040,
283 "qla_target(%d): Received unknown ATIO atio "
284 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
285 break;
286 }
287
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -0500288 return false;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400289}
290
291void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
292{
293 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400294 case CTIO_CRC2:
295 ql_dbg(ql_dbg_tgt, vha, 0xe073,
296 "qla_target(%d):%s: CRC2 Response pkt\n",
297 vha->vp_idx, __func__);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400298 case CTIO_TYPE7:
299 {
300 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
301 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
302 entry->vp_index);
303 if (unlikely(!host)) {
304 ql_dbg(ql_dbg_tgt, vha, 0xe041,
305 "qla_target(%d): Response pkt (CTIO_TYPE7) "
306 "received, with unknown vp_index %d\n",
307 vha->vp_idx, entry->vp_index);
308 break;
309 }
310 qlt_response_pkt(host, pkt);
311 break;
312 }
313
314 case IMMED_NOTIFY_TYPE:
315 {
316 struct scsi_qla_host *host = vha;
317 struct imm_ntfy_from_isp *entry =
318 (struct imm_ntfy_from_isp *)pkt;
319
320 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
321 if (unlikely(!host)) {
322 ql_dbg(ql_dbg_tgt, vha, 0xe042,
323 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
324 "received, with unknown vp_index %d\n",
325 vha->vp_idx, entry->u.isp24.vp_index);
326 break;
327 }
328 qlt_response_pkt(host, pkt);
329 break;
330 }
331
332 case NOTIFY_ACK_TYPE:
333 {
334 struct scsi_qla_host *host = vha;
335 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
336
337 if (0xFF != entry->u.isp24.vp_index) {
338 host = qlt_find_host_by_vp_idx(vha,
339 entry->u.isp24.vp_index);
340 if (unlikely(!host)) {
341 ql_dbg(ql_dbg_tgt, vha, 0xe043,
342 "qla_target(%d): Response "
343 "pkt (NOTIFY_ACK_TYPE) "
344 "received, with unknown "
345 "vp_index %d\n", vha->vp_idx,
346 entry->u.isp24.vp_index);
347 break;
348 }
349 }
350 qlt_response_pkt(host, pkt);
351 break;
352 }
353
354 case ABTS_RECV_24XX:
355 {
356 struct abts_recv_from_24xx *entry =
357 (struct abts_recv_from_24xx *)pkt;
358 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
359 entry->vp_index);
360 if (unlikely(!host)) {
361 ql_dbg(ql_dbg_tgt, vha, 0xe044,
362 "qla_target(%d): Response pkt "
363 "(ABTS_RECV_24XX) received, with unknown "
364 "vp_index %d\n", vha->vp_idx, entry->vp_index);
365 break;
366 }
367 qlt_response_pkt(host, pkt);
368 break;
369 }
370
371 case ABTS_RESP_24XX:
372 {
373 struct abts_resp_to_24xx *entry =
374 (struct abts_resp_to_24xx *)pkt;
375 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
376 entry->vp_index);
377 if (unlikely(!host)) {
378 ql_dbg(ql_dbg_tgt, vha, 0xe045,
379 "qla_target(%d): Response pkt "
380 "(ABTS_RECV_24XX) received, with unknown "
381 "vp_index %d\n", vha->vp_idx, entry->vp_index);
382 break;
383 }
384 qlt_response_pkt(host, pkt);
385 break;
386 }
387
388 default:
389 qlt_response_pkt(vha, pkt);
390 break;
391 }
392
393}
394
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500395/*
396 * All qlt_plogi_ack_t operations are protected by hardware_lock
397 */
398
399/*
400 * This is a zero-base ref-counting solution, since hardware_lock
401 * guarantees that ref_count is not modified concurrently.
402 * Upon successful return content of iocb is undefined
403 */
404static qlt_plogi_ack_t *
405qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
406 struct imm_ntfy_from_isp *iocb)
407{
408 qlt_plogi_ack_t *pla;
409
410 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
411 if (pla->id.b24 == id->b24) {
412 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
413 pla->iocb = *iocb;
414 return pla;
415 }
416 }
417
418 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
419 if (!pla) {
420 ql_dbg(ql_dbg_async, vha, 0x5088,
421 "qla_target(%d): Allocation of plogi_ack failed\n",
422 vha->vp_idx);
423 return NULL;
424 }
425
426 pla->iocb = *iocb;
427 pla->id = *id;
428 list_add_tail(&pla->list, &vha->plogi_ack_list);
429
430 return pla;
431}
432
433static void qlt_plogi_ack_unref(struct scsi_qla_host *vha, qlt_plogi_ack_t *pla)
434{
435 BUG_ON(!pla->ref_count);
436 pla->ref_count--;
437
438 if (pla->ref_count)
439 return;
440
441 ql_dbg(ql_dbg_async, vha, 0x5089,
442 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
443 " exch %#x ox_id %#x\n", pla->iocb.u.isp24.port_name,
444 pla->iocb.u.isp24.port_id[2], pla->iocb.u.isp24.port_id[1],
445 pla->iocb.u.isp24.port_id[0],
446 le16_to_cpu(pla->iocb.u.isp24.nport_handle),
447 pla->iocb.u.isp24.exchange_address, pla->iocb.ox_id);
448 qlt_send_notify_ack(vha, &pla->iocb, 0, 0, 0, 0, 0, 0);
449
450 list_del(&pla->list);
451 kmem_cache_free(qla_tgt_plogi_cachep, pla);
452}
453
454static void
455qlt_plogi_ack_link(struct scsi_qla_host *vha, qlt_plogi_ack_t *pla,
456 struct qla_tgt_sess *sess, qlt_plogi_link_t link)
457{
458 /* Inc ref_count first because link might already be pointing at pla */
459 pla->ref_count++;
460
461 if (sess->plogi_link[link])
462 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
463
464 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
465 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
466 " s_id %02x:%02x:%02x, ref=%d\n", sess, link, sess->port_name,
467 pla->iocb.u.isp24.port_name, pla->iocb.u.isp24.port_id[2],
468 pla->iocb.u.isp24.port_id[1], pla->iocb.u.isp24.port_id[0],
469 pla->ref_count);
470
471 sess->plogi_link[link] = pla;
472}
473
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500474typedef struct {
475 /* These fields must be initialized by the caller */
476 port_id_t id;
477 /*
478 * number of cmds dropped while we were waiting for
479 * initiator to ack LOGO initialize to 1 if LOGO is
480 * triggered by a command, otherwise, to 0
481 */
482 int cmd_count;
483
484 /* These fields are used by callee */
485 struct list_head list;
486} qlt_port_logo_t;
487
488static void
489qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
490{
491 qlt_port_logo_t *tmp;
492 int res;
493
494 mutex_lock(&vha->vha_tgt.tgt_mutex);
495
496 list_for_each_entry(tmp, &vha->logo_list, list) {
497 if (tmp->id.b24 == logo->id.b24) {
498 tmp->cmd_count += logo->cmd_count;
499 mutex_unlock(&vha->vha_tgt.tgt_mutex);
500 return;
501 }
502 }
503
504 list_add_tail(&logo->list, &vha->logo_list);
505
506 mutex_unlock(&vha->vha_tgt.tgt_mutex);
507
508 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
509
510 mutex_lock(&vha->vha_tgt.tgt_mutex);
511 list_del(&logo->list);
512 mutex_unlock(&vha->vha_tgt.tgt_mutex);
513
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500514 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
515 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
516 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
517 logo->cmd_count, res);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500518}
519
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400520static void qlt_free_session_done(struct work_struct *work)
521{
522 struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
523 free_work);
524 struct qla_tgt *tgt = sess->tgt;
525 struct scsi_qla_host *vha = sess->vha;
526 struct qla_hw_data *ha = vha->hw;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400527 unsigned long flags;
528 bool logout_started = false;
529 fc_port_t fcport;
530
531 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
532 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500533 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400534 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
535 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
536 sess->logout_on_delete, sess->keep_nport_handle,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500537 sess->send_els_logo);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400538
539 BUG_ON(!tgt);
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400540
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500541 if (sess->send_els_logo) {
542 qlt_port_logo_t logo;
543 logo.id = sess->s_id;
544 logo.cmd_count = 0;
545 qlt_send_first_logo(vha, &logo);
546 }
547
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400548 if (sess->logout_on_delete) {
549 int rc;
550
551 memset(&fcport, 0, sizeof(fcport));
552 fcport.loop_id = sess->loop_id;
553 fcport.d_id = sess->s_id;
554 memcpy(fcport.port_name, sess->port_name, WWN_SIZE);
555 fcport.vha = vha;
556 fcport.tgt_session = sess;
557
558 rc = qla2x00_post_async_logout_work(vha, &fcport, NULL);
559 if (rc != QLA_SUCCESS)
560 ql_log(ql_log_warn, vha, 0xf085,
561 "Schedule logo failed sess %p rc %d\n",
562 sess, rc);
563 else
564 logout_started = true;
565 }
566
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400567 /*
568 * Release the target session for FC Nexus from fabric module code.
569 */
570 if (sess->se_sess != NULL)
571 ha->tgt.tgt_ops->free_session(sess);
572
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400573 if (logout_started) {
574 bool traced = false;
575
576 while (!ACCESS_ONCE(sess->logout_completed)) {
577 if (!traced) {
578 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
579 "%s: waiting for sess %p logout\n",
580 __func__, sess);
581 traced = true;
582 }
583 msleep(100);
584 }
585
586 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf087,
587 "%s: sess %p logout completed\n",
588 __func__, sess);
589 }
590
591 spin_lock_irqsave(&ha->hardware_lock, flags);
592
Alexei Potashnikb7bd1042015-12-17 14:57:02 -0500593 {
594 qlt_plogi_ack_t *own =
595 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
596 qlt_plogi_ack_t *con =
597 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
598
599 if (con) {
600 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
601 "se_sess %p / sess %p port %8phC is gone,"
602 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
603 sess->se_sess, sess, sess->port_name,
604 own ? "releasing own PLOGI" :
605 "no own PLOGI pending",
606 own ? own->ref_count : -1,
607 con->iocb.u.isp24.port_name, con->ref_count);
608 qlt_plogi_ack_unref(vha, con);
609 } else {
610 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
611 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
612 sess->se_sess, sess, sess->port_name,
613 own ? "releasing own PLOGI" :
614 "no own PLOGI pending",
615 own ? own->ref_count : -1);
616 }
617
618 if (own)
619 qlt_plogi_ack_unref(vha, own);
620 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400621
622 list_del(&sess->sess_list_entry);
623
624 spin_unlock_irqrestore(&ha->hardware_lock, flags);
625
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400626 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
627 "Unregistration of sess %p finished\n", sess);
628
629 kfree(sess);
630 /*
631 * We need to protect against race, when tgt is freed before or
632 * inside wake_up()
633 */
634 tgt->sess_count--;
635 if (tgt->sess_count == 0)
636 wake_up_all(&tgt->waitQ);
637}
638
Quinn Tran75601512015-12-17 14:57:04 -0500639/* ha->tgt.sess_lock supposed to be held on entry */
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200640static void qlt_release_session(struct kref *kref)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400641{
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200642 struct qla_tgt_sess *sess =
643 container_of(kref, struct qla_tgt_sess, sess_kref);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400644 struct scsi_qla_host *vha = sess->vha;
645
Quinn Tran36c78452016-02-04 11:45:18 -0500646 if (sess->se_sess)
647 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400648
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400649 if (!list_empty(&sess->del_list_entry))
650 list_del_init(&sess->del_list_entry);
651 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400652
653 INIT_WORK(&sess->free_work, qlt_free_session_done);
654 schedule_work(&sess->free_work);
655}
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400656
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200657void qlt_put_sess(struct qla_tgt_sess *sess)
658{
659 if (!sess)
660 return;
661
662 assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
663 kref_put(&sess->sess_kref, qlt_release_session);
664}
665EXPORT_SYMBOL(qlt_put_sess);
Quinn Tran75601512015-12-17 14:57:04 -0500666
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400667static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
668{
669 struct qla_hw_data *ha = vha->hw;
670 struct qla_tgt_sess *sess = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400671 uint16_t loop_id;
672 int res = 0;
673 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -0500674 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400675
676 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
677 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400678 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -0400679 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -0500680 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400681 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -0500682 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400683#if 0 /* FIXME: do we need to choose a session here? */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400684 if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
685 sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
686 typeof(*sess), sess_list_entry);
687 switch (mcmd) {
688 case QLA_TGT_NEXUS_LOSS_SESS:
689 mcmd = QLA_TGT_NEXUS_LOSS;
690 break;
691 case QLA_TGT_ABORT_ALL_SESS:
692 mcmd = QLA_TGT_ABORT_ALL;
693 break;
694 case QLA_TGT_NEXUS_LOSS:
695 case QLA_TGT_ABORT_ALL:
696 break;
697 default:
698 ql_dbg(ql_dbg_tgt, vha, 0xe046,
699 "qla_target(%d): Not allowed "
700 "command %x in %s", vha->vp_idx,
701 mcmd, __func__);
702 sess = NULL;
703 break;
704 }
705 } else
706 sess = NULL;
707#endif
708 } else {
Quinn Tran75601512015-12-17 14:57:04 -0500709 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400710 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -0500711 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400712 }
713
714 ql_dbg(ql_dbg_tgt, vha, 0xe000,
715 "Using sess for qla_tgt_reset: %p\n", sess);
716 if (!sess) {
717 res = -ESRCH;
718 return res;
719 }
720
721 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400722 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
723 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400724 mcmd, loop_id);
725
Quinn Tranbb1181c2016-12-23 18:06:05 -0800726 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400727}
728
Quinn Tran75601512015-12-17 14:57:04 -0500729/* ha->tgt.sess_lock supposed to be held on entry */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400730static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
731 bool immediate)
732{
733 struct qla_tgt *tgt = sess->tgt;
734 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
735
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400736 if (sess->deleted) {
737 /* Upgrade to unconditional deletion in case it was temporary */
738 if (immediate && sess->deleted == QLA_SESS_DELETION_PENDING)
739 list_del(&sess->del_list_entry);
740 else
741 return;
742 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400743
744 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
745 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400746
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400747 if (immediate) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400748 dev_loss_tmo = 0;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400749 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
750 list_add(&sess->del_list_entry, &tgt->del_sess_list);
751 } else {
752 sess->deleted = QLA_SESS_DELETION_PENDING;
753 list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
754 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400755
756 sess->expires = jiffies + dev_loss_tmo * HZ;
757
758 ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
Alexei Potashnikdf673272015-07-14 16:00:46 -0400759 "qla_target(%d): session for port %8phC (loop ID %d s_id %02x:%02x:%02x)"
760 " scheduled for deletion in %u secs (expires: %lu) immed: %d, logout: %d, gen: %#x\n",
761 sess->vha->vp_idx, sess->port_name, sess->loop_id,
762 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
763 dev_loss_tmo, sess->expires, immediate, sess->logout_on_delete,
764 sess->generation);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400765
766 if (immediate)
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400767 mod_delayed_work(system_wq, &tgt->sess_del_work, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400768 else
769 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530770 sess->expires - jiffies);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400771}
772
Quinn Tran75601512015-12-17 14:57:04 -0500773/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -0400774static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400775{
776 struct qla_tgt_sess *sess;
777
778 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
779 qlt_schedule_sess_for_deletion(sess, true);
780
781 /* At this point tgt could be already dead */
782}
783
784static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
785 uint16_t *loop_id)
786{
787 struct qla_hw_data *ha = vha->hw;
788 dma_addr_t gid_list_dma;
789 struct gid_list_info *gid_list;
790 char *id_iter;
791 int res, rc, i;
792 uint16_t entries;
793
794 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
795 &gid_list_dma, GFP_KERNEL);
796 if (!gid_list) {
797 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
798 "qla_target(%d): DMA Alloc failed of %u\n",
799 vha->vp_idx, qla2x00_gid_list_size(ha));
800 return -ENOMEM;
801 }
802
803 /* Get list of logged in devices */
804 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
805 if (rc != QLA_SUCCESS) {
806 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
807 "qla_target(%d): get_id_list() failed: %x\n",
808 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500809 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400810 goto out_free_id_list;
811 }
812
813 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500814 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400815 for (i = 0; i < entries; i++) {
816 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
817 if ((gid->al_pa == s_id[2]) &&
818 (gid->area == s_id[1]) &&
819 (gid->domain == s_id[0])) {
820 *loop_id = le16_to_cpu(gid->loop_id);
821 res = 0;
822 break;
823 }
824 id_iter += ha->gid_list_info_size;
825 }
826
827out_free_id_list:
828 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
829 gid_list, gid_list_dma);
830 return res;
831}
832
Quinn Tran75601512015-12-17 14:57:04 -0500833/* ha->tgt.sess_lock supposed to be held on entry */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400834static void qlt_undelete_sess(struct qla_tgt_sess *sess)
835{
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400836 BUG_ON(sess->deleted != QLA_SESS_DELETION_PENDING);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400837
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400838 list_del_init(&sess->del_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400839 sess->deleted = 0;
840}
841
842static void qlt_del_sess_work_fn(struct delayed_work *work)
843{
844 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
845 sess_del_work);
846 struct scsi_qla_host *vha = tgt->vha;
847 struct qla_hw_data *ha = vha->hw;
848 struct qla_tgt_sess *sess;
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530849 unsigned long flags, elapsed;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400850
Quinn Tran75601512015-12-17 14:57:04 -0500851 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400852 while (!list_empty(&tgt->del_sess_list)) {
853 sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
854 del_list_entry);
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530855 elapsed = jiffies;
856 if (time_after_eq(elapsed, sess->expires)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400857 /* No turning back */
858 list_del_init(&sess->del_list_entry);
859 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400860
Jörn Engel08234e32013-06-12 16:27:54 -0400861 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
862 "Timeout: sess %p about to be deleted\n",
863 sess);
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200864 if (sess->se_sess)
Quinn Tran36c78452016-02-04 11:45:18 -0500865 ha->tgt.tgt_ops->shutdown_sess(sess);
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200866 qlt_put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400867 } else {
868 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530869 sess->expires - elapsed);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400870 break;
871 }
872 }
Quinn Tran75601512015-12-17 14:57:04 -0500873 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400874}
875
876/*
877 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
878 * Caller must put it.
879 */
880static struct qla_tgt_sess *qlt_create_sess(
881 struct scsi_qla_host *vha,
882 fc_port_t *fcport,
883 bool local)
884{
885 struct qla_hw_data *ha = vha->hw;
886 struct qla_tgt_sess *sess;
887 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400888
889 /* Check to avoid double sessions */
Quinn Tran75601512015-12-17 14:57:04 -0500890 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800891 list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400892 sess_list_entry) {
893 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
894 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
895 "Double sess %p found (s_id %x:%x:%x, "
896 "loop_id %d), updating to d_id %x:%x:%x, "
897 "loop_id %d", sess, sess->s_id.b.domain,
898 sess->s_id.b.al_pa, sess->s_id.b.area,
899 sess->loop_id, fcport->d_id.b.domain,
900 fcport->d_id.b.al_pa, fcport->d_id.b.area,
901 fcport->loop_id);
902
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400903 /* Cannot undelete at this point */
904 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
Quinn Tran75601512015-12-17 14:57:04 -0500905 spin_unlock_irqrestore(&ha->tgt.sess_lock,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400906 flags);
907 return NULL;
908 }
909
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400910 if (sess->deleted)
911 qlt_undelete_sess(sess);
912
Quinn Tran36c78452016-02-04 11:45:18 -0500913 if (!sess->se_sess) {
914 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
915 &sess->port_name[0], sess) < 0) {
916 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
917 return NULL;
918 }
919 }
920
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200921 kref_get(&sess->sess_kref);
Roland Dreierc8292d12012-10-11 13:41:32 -0700922 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
923 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
924
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400925 if (sess->local && !local)
926 sess->local = 0;
Alexei Potashnikdf673272015-07-14 16:00:46 -0400927
928 qlt_do_generation_tick(vha, &sess->generation);
929
Quinn Tran75601512015-12-17 14:57:04 -0500930 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400931
932 return sess;
933 }
934 }
Quinn Tran75601512015-12-17 14:57:04 -0500935 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400936
937 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
938 if (!sess) {
939 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400940 "qla_target(%u): session allocation failed, all commands "
941 "from port %8phC will be refused", vha->vp_idx,
942 fcport->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400943
944 return NULL;
945 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800946 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400947 sess->vha = vha;
948 sess->s_id = fcport->d_id;
949 sess->loop_id = fcport->loop_id;
950 sess->local = local;
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200951 kref_init(&sess->sess_kref);
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400952 INIT_LIST_HEAD(&sess->del_list_entry);
953
954 /* Under normal circumstances we want to logout from firmware when
955 * session eventually ends and release corresponding nport handle.
956 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
957 * code will adjust these flags as necessary. */
958 sess->logout_on_delete = 1;
959 sess->keep_nport_handle = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400960
961 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
962 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800963 sess, vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400964
Roland Dreierc8292d12012-10-11 13:41:32 -0700965 sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400966 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
967 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
968
Quinn Tran75601512015-12-17 14:57:04 -0500969 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800970 list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
971 vha->vha_tgt.qla_tgt->sess_count++;
Alexei Potashnikdf673272015-07-14 16:00:46 -0400972 qlt_do_generation_tick(vha, &sess->generation);
Quinn Tran75601512015-12-17 14:57:04 -0500973 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400974
975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400976 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
977 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
978 vha->vp_idx, local ? "local " : "", fcport->port_name,
979 fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
980 sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400981
Quinn Tran36c78452016-02-04 11:45:18 -0500982 /*
983 * Determine if this fc_port->port_name is allowed to access
984 * target mode using explict NodeACLs+MappedLUNs, or using
985 * TPG demo mode. If this is successful a target mode FC nexus
986 * is created.
987 */
988 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
989 &fcport->port_name[0], sess) < 0) {
990 return NULL;
991 } else {
992 /*
993 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
994 * access across ->tgt.sess_lock reaquire.
995 */
Christoph Hellwige3dc0e32016-05-02 15:45:23 +0200996 kref_get(&sess->sess_kref);
Quinn Tran36c78452016-02-04 11:45:18 -0500997 }
998
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400999 return sess;
1000}
1001
1002/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001003 * Called from qla2x00_reg_remote_port()
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001004 */
1005void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
1006{
1007 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001008 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001009 struct qla_tgt_sess *sess;
1010 unsigned long flags;
1011
1012 if (!vha->hw->tgt.tgt_ops)
1013 return;
1014
1015 if (!tgt || (fcport->port_type != FCT_INITIATOR))
1016 return;
1017
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001018 if (qla_ini_mode_enabled(vha))
1019 return;
1020
Quinn Tran75601512015-12-17 14:57:04 -05001021 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001022 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001023 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001024 return;
1025 }
1026 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
1027 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001028 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001029
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001030 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001031 sess = qlt_create_sess(vha, fcport, false);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001032 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001033
Quinn Tran75601512015-12-17 14:57:04 -05001034 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001035 } else if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1036 /* Point of no return */
Quinn Tran75601512015-12-17 14:57:04 -05001037 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001038 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001039 } else {
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02001040 kref_get(&sess->sess_kref);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001041
1042 if (sess->deleted) {
1043 qlt_undelete_sess(sess);
1044
1045 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001046 "qla_target(%u): %ssession for port %8phC "
1047 "(loop ID %d) reappeared\n", vha->vp_idx,
1048 sess->local ? "local " : "", sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001049 sess->loop_id);
1050
1051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
1052 "Reappeared sess %p\n", sess);
1053 }
Roland Dreierc8292d12012-10-11 13:41:32 -07001054 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
1055 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001056 }
1057
1058 if (sess && sess->local) {
1059 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
1060 "qla_target(%u): local session for "
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001061 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
1062 fcport->port_name, sess->loop_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001063 sess->local = 0;
1064 }
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02001065 qlt_put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05001066 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001067}
1068
Alexei Potashnikdf673272015-07-14 16:00:46 -04001069/*
1070 * max_gen - specifies maximum session generation
1071 * at which this deletion requestion is still valid
1072 */
1073void
1074qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001075{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001076 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001077 struct qla_tgt_sess *sess;
Quinn Tran75601512015-12-17 14:57:04 -05001078 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001079
1080 if (!vha->hw->tgt.tgt_ops)
1081 return;
1082
Roland Dreierb2032fd2015-07-14 16:00:42 -04001083 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001084 return;
1085
Quinn Tran75601512015-12-17 14:57:04 -05001086 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001087 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001088 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001089 return;
1090 }
1091 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
1092 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001093 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001094 return;
1095 }
1096
Alexei Potashnikdf673272015-07-14 16:00:46 -04001097 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001098 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001099 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1100 "Ignoring stale deletion request for se_sess %p / sess %p"
1101 " for port %8phC, req_gen %d, sess_gen %d\n",
1102 sess->se_sess, sess, sess->port_name, max_gen,
1103 sess->generation);
1104 return;
1105 }
1106
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001107 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1108
1109 sess->local = 1;
1110 qlt_schedule_sess_for_deletion(sess, false);
Quinn Tran75601512015-12-17 14:57:04 -05001111 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001112}
1113
1114static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1115{
1116 struct qla_hw_data *ha = tgt->ha;
1117 unsigned long flags;
1118 int res;
1119 /*
1120 * We need to protect against race, when tgt is freed before or
1121 * inside wake_up()
1122 */
1123 spin_lock_irqsave(&ha->hardware_lock, flags);
1124 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1125 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
1126 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
1127 res = (tgt->sess_count == 0);
1128 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1129
1130 return res;
1131}
1132
1133/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001134int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001135{
1136 struct scsi_qla_host *vha = tgt->vha;
1137 struct qla_hw_data *ha = tgt->ha;
1138 unsigned long flags;
1139
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001140 mutex_lock(&qla_tgt_mutex);
1141 if (!vha->fc_vport) {
1142 struct Scsi_Host *sh = vha->host;
1143 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1144 bool npiv_vports;
1145
1146 spin_lock_irqsave(sh->host_lock, flags);
1147 npiv_vports = (fc_host->npiv_vports_inuse);
1148 spin_unlock_irqrestore(sh->host_lock, flags);
1149
1150 if (npiv_vports) {
1151 mutex_unlock(&qla_tgt_mutex);
1152 return -EPERM;
1153 }
1154 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001155 if (tgt->tgt_stop || tgt->tgt_stopped) {
1156 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1157 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001158 mutex_unlock(&qla_tgt_mutex);
1159 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001160 }
1161
1162 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1163 vha->host_no, vha);
1164 /*
1165 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1166 * Lock is needed, because we still can get an incoming packet.
1167 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001168 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001169 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001170 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001171 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001172 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001173 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001174 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001175
Tejun Heo43829732012-08-20 14:51:24 -07001176 flush_delayed_work(&tgt->sess_del_work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001177
1178 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1179 "Waiting for sess works (tgt %p)", tgt);
1180 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1181 while (!list_empty(&tgt->sess_works_list)) {
1182 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1183 flush_scheduled_work();
1184 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1185 }
1186 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1187
1188 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1189 "Waiting for tgt %p: list_empty(sess_list)=%d "
1190 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
1191 tgt->sess_count);
1192
1193 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1194
1195 /* Big hammer */
1196 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
1197 qlt_disable_vha(vha);
1198
1199 /* Wait for sessions to clear out (just in case) */
1200 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001201 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001202}
1203EXPORT_SYMBOL(qlt_stop_phase1);
1204
1205/* Called by tcm_qla2xxx configfs code */
1206void qlt_stop_phase2(struct qla_tgt *tgt)
1207{
1208 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001209 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001210 unsigned long flags;
1211
1212 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001213 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001214 "Already in tgt->tgt_stopped state\n");
1215 dump_stack();
1216 return;
1217 }
1218
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001219 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001220 "Waiting for %d IRQ commands to complete (tgt %p)",
1221 tgt->irq_cmd_count, tgt);
1222
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001223 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001224 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05001225 while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001226 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1227 udelay(2);
1228 spin_lock_irqsave(&ha->hardware_lock, flags);
1229 }
1230 tgt->tgt_stop = 0;
1231 tgt->tgt_stopped = 1;
1232 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001233 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001234
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001235 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001236 tgt);
1237}
1238EXPORT_SYMBOL(qlt_stop_phase2);
1239
1240/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001241static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001242{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001243 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001244
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001245 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001246 qlt_stop_phase2(tgt);
1247
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001248 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001249
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001250 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001251 "Release of tgt %p finished\n", tgt);
1252
1253 kfree(tgt);
1254}
1255
1256/* ha->hardware_lock supposed to be held on entry */
1257static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1258 const void *param, unsigned int param_size)
1259{
1260 struct qla_tgt_sess_work_param *prm;
1261 unsigned long flags;
1262
1263 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1264 if (!prm) {
1265 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1266 "qla_target(%d): Unable to create session "
1267 "work, command will be refused", 0);
1268 return -ENOMEM;
1269 }
1270
1271 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1272 "Scheduling work (type %d, prm %p)"
1273 " to find session for param %p (size %d, tgt %p)\n",
1274 type, prm, param, param_size, tgt);
1275
1276 prm->type = type;
1277 memcpy(&prm->tm_iocb, param, param_size);
1278
1279 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1280 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1281 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1282
1283 schedule_work(&tgt->sess_work);
1284
1285 return 0;
1286}
1287
1288/*
1289 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1290 */
1291static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1292 struct imm_ntfy_from_isp *ntfy,
1293 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1294 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1295{
1296 struct qla_hw_data *ha = vha->hw;
1297 request_t *pkt;
1298 struct nack_to_isp *nack;
1299
1300 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1301
1302 /* Send marker if required */
1303 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1304 return;
1305
1306 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1307 if (!pkt) {
1308 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1309 "qla_target(%d): %s failed: unable to allocate "
1310 "request packet\n", vha->vp_idx, __func__);
1311 return;
1312 }
1313
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001314 if (vha->vha_tgt.qla_tgt != NULL)
1315 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001316
1317 pkt->entry_type = NOTIFY_ACK_TYPE;
1318 pkt->entry_count = 1;
1319
1320 nack = (struct nack_to_isp *)pkt;
1321 nack->ox_id = ntfy->ox_id;
1322
1323 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1324 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1325 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001326 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001327 }
1328 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1329 nack->u.isp24.status = ntfy->u.isp24.status;
1330 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001331 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001332 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1333 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1334 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1335 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1336 nack->u.isp24.srr_reject_code = srr_reject_code;
1337 nack->u.isp24.srr_reject_code_expl = srr_explan;
1338 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1339
1340 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1341 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1342 vha->vp_idx, nack->u.isp24.status);
1343
Himanshu Madhani63163e02014-09-25 06:14:59 -04001344 /* Memory Barrier */
1345 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001346 qla2x00_start_iocbs(vha, vha->req);
1347}
1348
1349/*
1350 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1351 */
1352static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1353 struct abts_recv_from_24xx *abts, uint32_t status,
1354 bool ids_reversed)
1355{
1356 struct qla_hw_data *ha = vha->hw;
1357 struct abts_resp_to_24xx *resp;
1358 uint32_t f_ctl;
1359 uint8_t *p;
1360
1361 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1362 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1363 ha, abts, status);
1364
1365 /* Send marker if required */
1366 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1367 return;
1368
Arun Easib6a029e2014-09-25 06:14:52 -04001369 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001370 if (!resp) {
1371 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1372 "qla_target(%d): %s failed: unable to allocate "
1373 "request packet", vha->vp_idx, __func__);
1374 return;
1375 }
1376
1377 resp->entry_type = ABTS_RESP_24XX;
1378 resp->entry_count = 1;
1379 resp->nport_handle = abts->nport_handle;
1380 resp->vp_index = vha->vp_idx;
1381 resp->sof_type = abts->sof_type;
1382 resp->exchange_address = abts->exchange_address;
1383 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001384 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001385 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1386 F_CTL_SEQ_INITIATIVE);
1387 p = (uint8_t *)&f_ctl;
1388 resp->fcp_hdr_le.f_ctl[0] = *p++;
1389 resp->fcp_hdr_le.f_ctl[1] = *p++;
1390 resp->fcp_hdr_le.f_ctl[2] = *p;
1391 if (ids_reversed) {
1392 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1393 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1394 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1395 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1396 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1397 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1398 } else {
1399 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1400 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1401 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1402 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1403 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1404 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1405 }
1406 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1407 if (status == FCP_TMF_CMPL) {
1408 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1409 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1410 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1411 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1412 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1413 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1414 } else {
1415 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1416 resp->payload.ba_rjt.reason_code =
1417 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1418 /* Other bytes are zero */
1419 }
1420
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001421 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001422
Himanshu Madhani63163e02014-09-25 06:14:59 -04001423 /* Memory Barrier */
1424 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001425 qla2x00_start_iocbs(vha, vha->req);
1426}
1427
1428/*
1429 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1430 */
1431static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1432 struct abts_resp_from_24xx_fw *entry)
1433{
1434 struct ctio7_to_24xx *ctio;
1435
1436 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1437 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1438 /* Send marker if required */
1439 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1440 return;
1441
Arun Easib6a029e2014-09-25 06:14:52 -04001442 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001443 if (ctio == NULL) {
1444 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1445 "qla_target(%d): %s failed: unable to allocate "
1446 "request packet\n", vha->vp_idx, __func__);
1447 return;
1448 }
1449
1450 /*
1451 * We've got on entrance firmware's response on by us generated
1452 * ABTS response. So, in it ID fields are reversed.
1453 */
1454
1455 ctio->entry_type = CTIO_TYPE7;
1456 ctio->entry_count = 1;
1457 ctio->nport_handle = entry->nport_handle;
1458 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001459 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001460 ctio->vp_index = vha->vp_idx;
1461 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1462 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1463 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1464 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001465 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1466 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001467 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001468
Himanshu Madhani63163e02014-09-25 06:14:59 -04001469 /* Memory Barrier */
1470 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001471 qla2x00_start_iocbs(vha, vha->req);
1472
1473 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1474 FCP_TMF_CMPL, true);
1475}
1476
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001477static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1478{
1479 struct qla_tgt_sess_op *op;
1480 struct qla_tgt_cmd *cmd;
1481
1482 spin_lock(&vha->cmd_list_lock);
1483
1484 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1485 if (tag == op->atio.u.isp24.exchange_addr) {
1486 op->aborted = true;
1487 spin_unlock(&vha->cmd_list_lock);
1488 return 1;
1489 }
1490 }
1491
1492 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1493 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001494 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001495 spin_unlock(&vha->cmd_list_lock);
1496 return 1;
1497 }
1498 }
1499
1500 spin_unlock(&vha->cmd_list_lock);
1501 return 0;
1502}
1503
1504/* drop cmds for the given lun
1505 * XXX only looks for cmds on the port through which lun reset was recieved
1506 * XXX does not go through the list of other port (which may have cmds
1507 * for the same lun)
1508 */
1509static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1510 uint32_t lun, uint8_t *s_id)
1511{
1512 struct qla_tgt_sess_op *op;
1513 struct qla_tgt_cmd *cmd;
1514 uint32_t key;
1515
1516 key = sid_to_key(s_id);
1517 spin_lock(&vha->cmd_list_lock);
1518 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1519 uint32_t op_key;
1520 uint32_t op_lun;
1521
1522 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1523 op_lun = scsilun_to_int(
1524 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1525 if (op_key == key && op_lun == lun)
1526 op->aborted = true;
1527 }
1528 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1529 uint32_t cmd_key;
1530 uint32_t cmd_lun;
1531
1532 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1533 cmd_lun = scsilun_to_int(
1534 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1535 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001536 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001537 }
1538 spin_unlock(&vha->cmd_list_lock);
1539}
1540
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001541/* ha->hardware_lock supposed to be held on entry */
1542static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1543 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1544{
1545 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001546 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001547 struct qla_tgt_mgmt_cmd *mcmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001548 struct se_cmd *se_cmd;
1549 u32 lun = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001550 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001551 bool found_lun = false;
1552
1553 spin_lock(&se_sess->sess_cmd_lock);
1554 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1555 struct qla_tgt_cmd *cmd =
1556 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
Bart Van Assche649ee052015-04-14 13:26:44 +02001557 if (se_cmd->tag == abts->exchange_addr_to_abort) {
Steve Hodgson06e97b42012-11-16 08:06:17 -08001558 lun = cmd->unpacked_lun;
1559 found_lun = true;
1560 break;
1561 }
1562 }
1563 spin_unlock(&se_sess->sess_cmd_lock);
1564
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001565 /* cmd not in LIO lists, look in qla list */
1566 if (!found_lun) {
1567 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1568 /* send TASK_ABORT response immediately */
1569 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1570 return 0;
1571 } else {
1572 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1573 "unable to find cmd in driver or LIO for tag 0x%x\n",
1574 abts->exchange_addr_to_abort);
1575 return -ENOENT;
1576 }
1577 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001578
1579 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1580 "qla_target(%d): task abort (tag=%d)\n",
1581 vha->vp_idx, abts->exchange_addr_to_abort);
1582
1583 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1584 if (mcmd == NULL) {
1585 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1586 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1587 vha->vp_idx, __func__);
1588 return -ENOMEM;
1589 }
1590 memset(mcmd, 0, sizeof(*mcmd));
1591
1592 mcmd->sess = sess;
1593 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Arun Easi80187f82014-09-25 06:14:53 -04001594 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08001595 mcmd->tmr_func = QLA_TGT_ABTS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001596
Quinn Tranbe92fc32017-01-19 22:27:54 -08001597 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001598 abts->exchange_addr_to_abort);
1599 if (rc != 0) {
1600 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1601 "qla_target(%d): tgt_ops->handle_tmr()"
1602 " failed: %d", vha->vp_idx, rc);
1603 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1604 return -EFAULT;
1605 }
1606
1607 return 0;
1608}
1609
1610/*
1611 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1612 */
1613static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1614 struct abts_recv_from_24xx *abts)
1615{
1616 struct qla_hw_data *ha = vha->hw;
1617 struct qla_tgt_sess *sess;
1618 uint32_t tag = abts->exchange_addr_to_abort;
1619 uint8_t s_id[3];
1620 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001621 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001622
1623 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1624 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1625 "qla_target(%d): ABTS: Abort Sequence not "
1626 "supported\n", vha->vp_idx);
1627 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1628 return;
1629 }
1630
1631 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1632 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1633 "qla_target(%d): ABTS: Unknown Exchange "
1634 "Address received\n", vha->vp_idx);
1635 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1636 return;
1637 }
1638
1639 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1640 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1641 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1642 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1643 le32_to_cpu(abts->fcp_hdr_le.parameter));
1644
1645 s_id[0] = abts->fcp_hdr_le.s_id[2];
1646 s_id[1] = abts->fcp_hdr_le.s_id[1];
1647 s_id[2] = abts->fcp_hdr_le.s_id[0];
1648
Quinn Tran75601512015-12-17 14:57:04 -05001649 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001650 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1651 if (!sess) {
1652 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1653 "qla_target(%d): task abort for non-existant session\n",
1654 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001655 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001656 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001657
1658 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1659
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001660 if (rc != 0) {
1661 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1662 false);
1663 }
1664 return;
1665 }
Quinn Tran75601512015-12-17 14:57:04 -05001666 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1667
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001668
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001669 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1670 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1671 return;
1672 }
1673
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001674 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1675 if (rc != 0) {
1676 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1677 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1678 vha->vp_idx, rc);
1679 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1680 return;
1681 }
1682}
1683
1684/*
1685 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1686 */
1687static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1688 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1689{
1690 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1691 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001692 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001693
1694 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1695 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1696 ha, atio, resp_code);
1697
1698 /* Send marker if required */
1699 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1700 return;
1701
1702 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1703 if (ctio == NULL) {
1704 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1705 "qla_target(%d): %s failed: unable to allocate "
1706 "request packet\n", ha->vp_idx, __func__);
1707 return;
1708 }
1709
1710 ctio->entry_type = CTIO_TYPE7;
1711 ctio->entry_count = 1;
1712 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1713 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07001714 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001715 ctio->vp_index = ha->vp_idx;
1716 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1717 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1718 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1719 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1720 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07001721 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001722 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1723 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001724 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07001725 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1726 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07001727 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001728
Himanshu Madhani63163e02014-09-25 06:14:59 -04001729 /* Memory Barrier */
1730 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001731 qla2x00_start_iocbs(ha, ha->req);
1732}
1733
1734void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1735{
1736 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1737}
1738EXPORT_SYMBOL(qlt_free_mcmd);
1739
1740/* callback from target fabric module code */
1741void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1742{
1743 struct scsi_qla_host *vha = mcmd->sess->vha;
1744 struct qla_hw_data *ha = vha->hw;
1745 unsigned long flags;
1746
1747 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1748 "TM response mcmd (%p) status %#x state %#x",
1749 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1750
1751 spin_lock_irqsave(&ha->hardware_lock, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04001752
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001753 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04001754 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001755 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04001756 * previous life, just abort the processing.
1757 */
1758 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001759 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
1760 vha->flags.online, qla2x00_reset_active(vha),
1761 mcmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04001762 ha->tgt.tgt_ops->free_mcmd(mcmd);
1763 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1764 return;
1765 }
1766
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001767 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1768 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1769 0, 0, 0, 0, 0, 0);
1770 else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05001771 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001772 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1773 mcmd->fc_tm_rsp, false);
1774 else
1775 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1776 mcmd->fc_tm_rsp);
1777 }
1778 /*
1779 * Make the callback for ->free_mcmd() to queue_work() and invoke
1780 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1781 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1782 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1783 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1784 * qlt_xmit_tm_rsp() returns here..
1785 */
1786 ha->tgt.tgt_ops->free_mcmd(mcmd);
1787 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1788}
1789EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1790
1791/* No locks */
1792static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1793{
1794 struct qla_tgt_cmd *cmd = prm->cmd;
1795
1796 BUG_ON(cmd->sg_cnt == 0);
1797
1798 prm->sg = (struct scatterlist *)cmd->sg;
1799 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1800 cmd->sg_cnt, cmd->dma_data_direction);
1801 if (unlikely(prm->seg_cnt == 0))
1802 goto out_err;
1803
1804 prm->cmd->sg_mapped = 1;
1805
Quinn Tranf83adb62014-04-11 16:54:43 -04001806 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
1807 /*
1808 * If greater than four sg entries then we need to allocate
1809 * the continuation entries
1810 */
1811 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1812 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1813 prm->tgt->datasegs_per_cmd,
1814 prm->tgt->datasegs_per_cont);
1815 } else {
1816 /* DIF */
1817 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1818 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1819 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
1820 prm->tot_dsds = prm->seg_cnt;
1821 } else
1822 prm->tot_dsds = prm->seg_cnt;
1823
1824 if (cmd->prot_sg_cnt) {
1825 prm->prot_sg = cmd->prot_sg;
1826 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
1827 cmd->prot_sg, cmd->prot_sg_cnt,
1828 cmd->dma_data_direction);
1829 if (unlikely(prm->prot_seg_cnt == 0))
1830 goto out_err;
1831
1832 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1833 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1834 /* Dif Bundling not support here */
1835 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
1836 cmd->blk_sz);
1837 prm->tot_dsds += prm->prot_seg_cnt;
1838 } else
1839 prm->tot_dsds += prm->prot_seg_cnt;
1840 }
1841 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001842
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001843 return 0;
1844
1845out_err:
1846 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1847 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1848 0, prm->cmd->sg_cnt);
1849 return -1;
1850}
1851
Joern Engelf9b67212014-09-16 16:23:18 -04001852static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001853{
1854 struct qla_hw_data *ha = vha->hw;
1855
Joern Engelf9b67212014-09-16 16:23:18 -04001856 if (!cmd->sg_mapped)
1857 return;
1858
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001859 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1860 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04001861
1862 if (cmd->prot_sg_cnt)
1863 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
1864 cmd->dma_data_direction);
1865
1866 if (cmd->ctx_dsd_alloced)
1867 qla2x00_clean_dsd_pool(ha, NULL, cmd);
1868
1869 if (cmd->ctx)
1870 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001871}
1872
1873static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1874 uint32_t req_cnt)
1875{
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001876 uint32_t cnt, cnt_in;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001877
1878 if (vha->req->cnt < (req_cnt + 2)) {
Arun Easi75554b62014-09-25 06:14:45 -04001879 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001880 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001881
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001882 if (vha->req->ring_index < cnt)
1883 vha->req->cnt = cnt - vha->req->ring_index;
1884 else
1885 vha->req->cnt = vha->req->length -
1886 (vha->req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01001887
1888 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1889 ql_dbg(ql_dbg_io, vha, 0x305a,
1890 "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",
1891 vha->vp_idx, vha->req->ring_index,
1892 vha->req->cnt, req_cnt, cnt, cnt_in,
1893 vha->req->length);
1894 return -EAGAIN;
1895 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001896 }
1897
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001898 vha->req->cnt -= req_cnt;
1899
1900 return 0;
1901}
1902
1903/*
1904 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1905 */
1906static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1907{
1908 /* Adjust ring index. */
1909 vha->req->ring_index++;
1910 if (vha->req->ring_index == vha->req->length) {
1911 vha->req->ring_index = 0;
1912 vha->req->ring_ptr = vha->req->ring;
1913 } else {
1914 vha->req->ring_ptr++;
1915 }
1916 return (cont_entry_t *)vha->req->ring_ptr;
1917}
1918
1919/* ha->hardware_lock supposed to be held on entry */
1920static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1921{
1922 struct qla_hw_data *ha = vha->hw;
1923 uint32_t h;
1924
1925 h = ha->tgt.current_handle;
1926 /* always increment cmd handle */
1927 do {
1928 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001929 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001930 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1931 if (h == ha->tgt.current_handle) {
Arun Easi667024a2014-09-25 06:14:47 -04001932 ql_dbg(ql_dbg_io, vha, 0x305b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001933 "qla_target(%d): Ran out of "
1934 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1935 h = QLA_TGT_NULL_HANDLE;
1936 break;
1937 }
1938 } while ((h == QLA_TGT_NULL_HANDLE) ||
1939 (h == QLA_TGT_SKIP_HANDLE) ||
1940 (ha->tgt.cmds[h-1] != NULL));
1941
1942 if (h != QLA_TGT_NULL_HANDLE)
1943 ha->tgt.current_handle = h;
1944
1945 return h;
1946}
1947
1948/* ha->hardware_lock supposed to be held on entry */
1949static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1950 struct scsi_qla_host *vha)
1951{
1952 uint32_t h;
1953 struct ctio7_to_24xx *pkt;
1954 struct qla_hw_data *ha = vha->hw;
1955 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001956 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001957
1958 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1959 prm->pkt = pkt;
1960 memset(pkt, 0, sizeof(*pkt));
1961
1962 pkt->entry_type = CTIO_TYPE7;
1963 pkt->entry_count = (uint8_t)prm->req_cnt;
1964 pkt->vp_index = vha->vp_idx;
1965
1966 h = qlt_make_handle(vha);
1967 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1968 /*
1969 * CTIO type 7 from the firmware doesn't provide a way to
1970 * know the initiator's LOOP ID, hence we can't find
1971 * the session and, so, the command.
1972 */
1973 return -EAGAIN;
1974 } else
1975 ha->tgt.cmds[h-1] = prm->cmd;
1976
1977 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1978 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07001979 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001980 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1981 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1982 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1983 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1984 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001985 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1986 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001987 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1988
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001989 return 0;
1990}
1991
1992/*
1993 * ha->hardware_lock supposed to be held on entry. We have already made sure
1994 * that there is sufficient amount of request entries to not drop it.
1995 */
1996static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1997 struct scsi_qla_host *vha)
1998{
1999 int cnt;
2000 uint32_t *dword_ptr;
2001 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2002
2003 /* Build continuation packets */
2004 while (prm->seg_cnt > 0) {
2005 cont_a64_entry_t *cont_pkt64 =
2006 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2007
2008 /*
2009 * Make sure that from cont_pkt64 none of
2010 * 64-bit specific fields used for 32-bit
2011 * addressing. Cast to (cont_entry_t *) for
2012 * that.
2013 */
2014
2015 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2016
2017 cont_pkt64->entry_count = 1;
2018 cont_pkt64->sys_define = 0;
2019
2020 if (enable_64bit_addressing) {
2021 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2022 dword_ptr =
2023 (uint32_t *)&cont_pkt64->dseg_0_address;
2024 } else {
2025 cont_pkt64->entry_type = CONTINUE_TYPE;
2026 dword_ptr =
2027 (uint32_t *)&((cont_entry_t *)
2028 cont_pkt64)->dseg_0_address;
2029 }
2030
2031 /* Load continuation entry data segments */
2032 for (cnt = 0;
2033 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2034 cnt++, prm->seg_cnt--) {
2035 *dword_ptr++ =
2036 cpu_to_le32(pci_dma_lo32
2037 (sg_dma_address(prm->sg)));
2038 if (enable_64bit_addressing) {
2039 *dword_ptr++ =
2040 cpu_to_le32(pci_dma_hi32
2041 (sg_dma_address
2042 (prm->sg)));
2043 }
2044 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2045
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002046 prm->sg = sg_next(prm->sg);
2047 }
2048 }
2049}
2050
2051/*
2052 * ha->hardware_lock supposed to be held on entry. We have already made sure
2053 * that there is sufficient amount of request entries to not drop it.
2054 */
2055static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2056 struct scsi_qla_host *vha)
2057{
2058 int cnt;
2059 uint32_t *dword_ptr;
2060 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2061 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2062
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002063 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2064
2065 /* Setup packet address segment pointer */
2066 dword_ptr = pkt24->u.status0.dseg_0_address;
2067
2068 /* Set total data segment count */
2069 if (prm->seg_cnt)
2070 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2071
2072 if (prm->seg_cnt == 0) {
2073 /* No data transfer */
2074 *dword_ptr++ = 0;
2075 *dword_ptr = 0;
2076 return;
2077 }
2078
2079 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002080
2081 /* Load command entry data segments */
2082 for (cnt = 0;
2083 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2084 cnt++, prm->seg_cnt--) {
2085 *dword_ptr++ =
2086 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2087 if (enable_64bit_addressing) {
2088 *dword_ptr++ =
2089 cpu_to_le32(pci_dma_hi32(
2090 sg_dma_address(prm->sg)));
2091 }
2092 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2093
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002094 prm->sg = sg_next(prm->sg);
2095 }
2096
2097 qlt_load_cont_data_segments(prm, vha);
2098}
2099
2100static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2101{
2102 return cmd->bufflen > 0;
2103}
2104
2105/*
2106 * Called without ha->hardware_lock held
2107 */
2108static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2109 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2110 uint32_t *full_req_cnt)
2111{
2112 struct qla_tgt *tgt = cmd->tgt;
2113 struct scsi_qla_host *vha = tgt->vha;
2114 struct qla_hw_data *ha = vha->hw;
2115 struct se_cmd *se_cmd = &cmd->se_cmd;
2116
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002117 prm->cmd = cmd;
2118 prm->tgt = tgt;
2119 prm->rq_result = scsi_status;
2120 prm->sense_buffer = &cmd->sense_buffer[0];
2121 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2122 prm->sg = NULL;
2123 prm->seg_cnt = -1;
2124 prm->req_cnt = 1;
2125 prm->add_status_pkt = 0;
2126
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002127 /* Send marker if required */
2128 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2129 return -EFAULT;
2130
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002131 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2132 if (qlt_pci_map_calc_cnt(prm) != 0)
2133 return -EAGAIN;
2134 }
2135
2136 *full_req_cnt = prm->req_cnt;
2137
2138 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2139 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002140 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002141 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2142 prm->residual, se_cmd->tag,
2143 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2144 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002145 prm->rq_result |= SS_RESIDUAL_UNDER;
2146 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2147 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002148 ql_dbg(ql_dbg_io, vha, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002149 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2150 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2151 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002152 prm->rq_result |= SS_RESIDUAL_OVER;
2153 }
2154
2155 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2156 /*
2157 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2158 * ignored in *xmit_response() below
2159 */
2160 if (qlt_has_data(cmd)) {
2161 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2162 (IS_FWI2_CAPABLE(ha) &&
2163 (prm->rq_result != 0))) {
2164 prm->add_status_pkt = 1;
2165 (*full_req_cnt)++;
2166 }
2167 }
2168 }
2169
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002170 return 0;
2171}
2172
2173static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2174 struct qla_tgt_cmd *cmd, int sending_sense)
2175{
2176 if (ha->tgt.enable_class_2)
2177 return 0;
2178
2179 if (sending_sense)
2180 return cmd->conf_compl_supported;
2181 else
2182 return ha->tgt.enable_explicit_conf &&
2183 cmd->conf_compl_supported;
2184}
2185
2186#ifdef CONFIG_QLA_TGT_DEBUG_SRR
2187/*
2188 * Original taken from the XFS code
2189 */
2190static unsigned long qlt_srr_random(void)
2191{
2192 static int Inited;
2193 static unsigned long RandomValue;
2194 static DEFINE_SPINLOCK(lock);
2195 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
2196 register long rv;
2197 register long lo;
2198 register long hi;
2199 unsigned long flags;
2200
2201 spin_lock_irqsave(&lock, flags);
2202 if (!Inited) {
2203 RandomValue = jiffies;
2204 Inited = 1;
2205 }
2206 rv = RandomValue;
2207 hi = rv / 127773;
2208 lo = rv % 127773;
2209 rv = 16807 * lo - 2836 * hi;
2210 if (rv <= 0)
2211 rv += 2147483647;
2212 RandomValue = rv;
2213 spin_unlock_irqrestore(&lock, flags);
2214 return rv;
2215}
2216
2217static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
2218{
2219#if 0 /* This is not a real status packets lost, so it won't lead to SRR */
2220 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
2221 == 50) {
2222 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
2223 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
Bart Van Assche649ee052015-04-14 13:26:44 +02002224 "Dropping cmd %p (tag %d) status", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002225 }
2226#endif
2227 /*
2228 * It's currently not possible to simulate SRRs for FCP_WRITE without
2229 * a physical link layer failure, so don't even try here..
2230 */
2231 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
2232 return;
2233
2234 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
2235 ((qlt_srr_random() % 100) == 20)) {
2236 int i, leave = 0;
2237 unsigned int tot_len = 0;
2238
2239 while (leave == 0)
2240 leave = qlt_srr_random() % cmd->sg_cnt;
2241
2242 for (i = 0; i < leave; i++)
2243 tot_len += cmd->sg[i].length;
2244
2245 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
2246 "Cutting cmd %p (tag %d) buffer"
2247 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
Bart Van Assche649ee052015-04-14 13:26:44 +02002248 " cmd->sg_cnt %d)", cmd, se_cmd->tag, tot_len, leave,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002249 cmd->bufflen, cmd->sg_cnt);
2250
2251 cmd->bufflen = tot_len;
2252 cmd->sg_cnt = leave;
2253 }
2254
2255 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
2256 unsigned int offset = qlt_srr_random() % cmd->bufflen;
2257
2258 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
2259 "Cutting cmd %p (tag %d) buffer head "
Bart Van Assche649ee052015-04-14 13:26:44 +02002260 "to offset %d (cmd->bufflen %d)", cmd, se_cmd->tag, offset,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002261 cmd->bufflen);
2262 if (offset == 0)
2263 *xmit_type &= ~QLA_TGT_XMIT_DATA;
2264 else if (qlt_set_data_offset(cmd, offset)) {
2265 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
Bart Van Assche649ee052015-04-14 13:26:44 +02002266 "qlt_set_data_offset() failed (tag %d)", se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002267 }
2268 }
2269}
2270#else
2271static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
2272{}
2273#endif
2274
2275static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2276 struct qla_tgt_prm *prm)
2277{
2278 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2279 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002280 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002281 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002282 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002283 CTIO7_FLAGS_EXPLICIT_CONFORM |
2284 CTIO7_FLAGS_CONFORM_REQ);
2285 }
2286 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2287 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2288 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2289 int i;
2290
2291 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
Quinn Trandf2e32c2017-01-19 22:27:53 -08002292 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002293 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2294 "Skipping EXPLICIT_CONFORM and "
2295 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2296 "non GOOD status\n");
2297 goto skip_explict_conf;
2298 }
Bart Van Asschead950362015-07-09 07:24:08 -07002299 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002300 CTIO7_FLAGS_EXPLICIT_CONFORM |
2301 CTIO7_FLAGS_CONFORM_REQ);
2302 }
2303skip_explict_conf:
2304 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002305 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002306 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002307 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002308 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002309 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002310 ctio->u.status1.sense_length =
2311 cpu_to_le16(prm->sense_buffer_len);
2312 for (i = 0; i < prm->sense_buffer_len/4; i++)
2313 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2314 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2315#if 0
2316 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
2317 static int q;
2318 if (q < 10) {
2319 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
2320 "qla_target(%d): %d bytes of sense "
2321 "lost", prm->tgt->ha->vp_idx,
2322 prm->sense_buffer_len % 4);
2323 q++;
2324 }
2325 }
2326#endif
2327 } else {
2328 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002329 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002330 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002331 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002332 ctio->u.status1.sense_length = 0;
2333 memset(ctio->u.status1.sense_data, 0,
2334 sizeof(ctio->u.status1.sense_data));
2335 }
2336
2337 /* Sense with len > 24, is it possible ??? */
2338}
2339
Quinn Tranf83adb62014-04-11 16:54:43 -04002340
2341
2342/* diff */
2343static inline int
2344qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2345{
2346 /*
2347 * Uncomment when corresponding SCSI changes are done.
2348 *
2349 if (!sp->cmd->prot_chk)
2350 return 0;
2351 *
2352 */
2353 switch (se_cmd->prot_op) {
2354 case TARGET_PROT_DOUT_INSERT:
2355 case TARGET_PROT_DIN_STRIP:
2356 if (ql2xenablehba_err_chk >= 1)
2357 return 1;
2358 break;
2359 case TARGET_PROT_DOUT_PASS:
2360 case TARGET_PROT_DIN_PASS:
2361 if (ql2xenablehba_err_chk >= 2)
2362 return 1;
2363 break;
2364 case TARGET_PROT_DIN_INSERT:
2365 case TARGET_PROT_DOUT_STRIP:
2366 return 1;
2367 default:
2368 break;
2369 }
2370 return 0;
2371}
2372
2373/*
2374 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
2375 *
2376 */
2377static inline void
2378qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
2379{
2380 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2381
2382 /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
2383 * have been immplemented by TCM, before AppTag is avail.
2384 * Look for modesense_handlers[]
2385 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002386 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002387 ctx->app_tag_mask[0] = 0x0;
2388 ctx->app_tag_mask[1] = 0x0;
2389
2390 switch (se_cmd->prot_type) {
2391 case TARGET_DIF_TYPE0_PROT:
2392 /*
2393 * No check for ql2xenablehba_err_chk, as it would be an
2394 * I/O error if hba tag generation is not done.
2395 */
2396 ctx->ref_tag = cpu_to_le32(lba);
2397
2398 if (!qlt_hba_err_chk_enabled(se_cmd))
2399 break;
2400
2401 /* enable ALL bytes of the ref tag */
2402 ctx->ref_tag_mask[0] = 0xff;
2403 ctx->ref_tag_mask[1] = 0xff;
2404 ctx->ref_tag_mask[2] = 0xff;
2405 ctx->ref_tag_mask[3] = 0xff;
2406 break;
2407 /*
2408 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
2409 * 16 bit app tag.
2410 */
2411 case TARGET_DIF_TYPE1_PROT:
2412 ctx->ref_tag = cpu_to_le32(lba);
2413
2414 if (!qlt_hba_err_chk_enabled(se_cmd))
2415 break;
2416
2417 /* enable ALL bytes of the ref tag */
2418 ctx->ref_tag_mask[0] = 0xff;
2419 ctx->ref_tag_mask[1] = 0xff;
2420 ctx->ref_tag_mask[2] = 0xff;
2421 ctx->ref_tag_mask[3] = 0xff;
2422 break;
2423 /*
2424 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
2425 * match LBA in CDB + N
2426 */
2427 case TARGET_DIF_TYPE2_PROT:
2428 ctx->ref_tag = cpu_to_le32(lba);
2429
2430 if (!qlt_hba_err_chk_enabled(se_cmd))
2431 break;
2432
2433 /* enable ALL bytes of the ref tag */
2434 ctx->ref_tag_mask[0] = 0xff;
2435 ctx->ref_tag_mask[1] = 0xff;
2436 ctx->ref_tag_mask[2] = 0xff;
2437 ctx->ref_tag_mask[3] = 0xff;
2438 break;
2439
2440 /* For Type 3 protection: 16 bit GUARD only */
2441 case TARGET_DIF_TYPE3_PROT:
2442 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2443 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2444 break;
2445 }
2446}
2447
2448
2449static inline int
2450qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2451{
2452 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002453 uint32_t transfer_length = 0;
2454 uint32_t data_bytes;
2455 uint32_t dif_bytes;
2456 uint8_t bundling = 1;
2457 uint8_t *clr_ptr;
2458 struct crc_context *crc_ctx_pkt = NULL;
2459 struct qla_hw_data *ha;
2460 struct ctio_crc2_to_fw *pkt;
2461 dma_addr_t crc_ctx_dma;
2462 uint16_t fw_prot_opts = 0;
2463 struct qla_tgt_cmd *cmd = prm->cmd;
2464 struct se_cmd *se_cmd = &cmd->se_cmd;
2465 uint32_t h;
2466 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002467 uint16_t t16;
Quinn Tranf83adb62014-04-11 16:54:43 -04002468
Quinn Tranf83adb62014-04-11 16:54:43 -04002469 ha = vha->hw;
2470
2471 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2472 prm->pkt = pkt;
2473 memset(pkt, 0, sizeof(*pkt));
2474
2475 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2476 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2477 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2478 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2479
2480 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2481 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2482 bundling = 0;
2483
2484 /* Compute dif len and adjust data len to incude protection */
2485 data_bytes = cmd->bufflen;
2486 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2487
2488 switch (se_cmd->prot_op) {
2489 case TARGET_PROT_DIN_INSERT:
2490 case TARGET_PROT_DOUT_STRIP:
2491 transfer_length = data_bytes;
2492 data_bytes += dif_bytes;
2493 break;
2494
2495 case TARGET_PROT_DIN_STRIP:
2496 case TARGET_PROT_DOUT_INSERT:
2497 case TARGET_PROT_DIN_PASS:
2498 case TARGET_PROT_DOUT_PASS:
2499 transfer_length = data_bytes + dif_bytes;
2500 break;
2501
2502 default:
2503 BUG();
2504 break;
2505 }
2506
2507 if (!qlt_hba_err_chk_enabled(se_cmd))
2508 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2509 /* HBA error checking enabled */
2510 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2511 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2512 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2513 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2514 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2515 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2516 }
2517
2518 switch (se_cmd->prot_op) {
2519 case TARGET_PROT_DIN_INSERT:
2520 case TARGET_PROT_DOUT_INSERT:
2521 fw_prot_opts |= PO_MODE_DIF_INSERT;
2522 break;
2523 case TARGET_PROT_DIN_STRIP:
2524 case TARGET_PROT_DOUT_STRIP:
2525 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2526 break;
2527 case TARGET_PROT_DIN_PASS:
2528 case TARGET_PROT_DOUT_PASS:
2529 fw_prot_opts |= PO_MODE_DIF_PASS;
2530 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2531 break;
2532 default:/* Normal Request */
2533 fw_prot_opts |= PO_MODE_DIF_PASS;
2534 break;
2535 }
2536
2537
2538 /* ---- PKT ---- */
2539 /* Update entry type to indicate Command Type CRC_2 IOCB */
2540 pkt->entry_type = CTIO_CRC2;
2541 pkt->entry_count = 1;
2542 pkt->vp_index = vha->vp_idx;
2543
2544 h = qlt_make_handle(vha);
2545 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2546 /*
2547 * CTIO type 7 from the firmware doesn't provide a way to
2548 * know the initiator's LOOP ID, hence we can't find
2549 * the session and, so, the command.
2550 */
2551 return -EAGAIN;
2552 } else
2553 ha->tgt.cmds[h-1] = prm->cmd;
2554
2555
2556 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2557 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002558 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002559 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2560 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2561 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2562 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002563
2564 /* silence compile warning */
2565 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2566 pkt->ox_id = cpu_to_le16(t16);
2567
2568 t16 = (atio->u.isp24.attr << 9);
2569 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002570 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2571
2572 /* Set transfer direction */
2573 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002574 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002575 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002576 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002577
2578
2579 pkt->dseg_count = prm->tot_dsds;
2580 /* Fibre channel byte count */
2581 pkt->transfer_length = cpu_to_le32(transfer_length);
2582
2583
2584 /* ----- CRC context -------- */
2585
2586 /* Allocate CRC context from global pool */
2587 crc_ctx_pkt = cmd->ctx =
2588 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2589
2590 if (!crc_ctx_pkt)
2591 goto crc_queuing_error;
2592
2593 /* Zero out CTX area. */
2594 clr_ptr = (uint8_t *)crc_ctx_pkt;
2595 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2596
2597 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2598 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2599
2600 /* Set handle */
2601 crc_ctx_pkt->handle = pkt->handle;
2602
2603 qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
2604
2605 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2606 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2607 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2608
2609
2610 if (!bundling) {
2611 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2612 } else {
2613 /*
2614 * Configure Bundling if we need to fetch interlaving
2615 * protection PCI accesses
2616 */
2617 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2618 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2619 crc_ctx_pkt->u.bundling.dseg_count =
2620 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2621 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2622 }
2623
2624 /* Finish the common fields of CRC pkt */
2625 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2626 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2627 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002628 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002629
2630
2631 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07002632 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04002633
2634 if (!bundling && prm->prot_seg_cnt) {
2635 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2636 prm->tot_dsds, cmd))
2637 goto crc_queuing_error;
2638 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2639 (prm->tot_dsds - prm->prot_seg_cnt), cmd))
2640 goto crc_queuing_error;
2641
2642 if (bundling && prm->prot_seg_cnt) {
2643 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002644 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04002645
2646 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2647 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2648 prm->prot_seg_cnt, cmd))
2649 goto crc_queuing_error;
2650 }
2651 return QLA_SUCCESS;
2652
2653crc_queuing_error:
2654 /* Cleanup will be performed by the caller */
2655
2656 return QLA_FUNCTION_FAILED;
2657}
2658
2659
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002660/*
2661 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2662 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2663 */
2664int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2665 uint8_t scsi_status)
2666{
2667 struct scsi_qla_host *vha = cmd->vha;
2668 struct qla_hw_data *ha = vha->hw;
2669 struct ctio7_to_24xx *pkt;
2670 struct qla_tgt_prm prm;
2671 uint32_t full_req_cnt = 0;
2672 unsigned long flags = 0;
2673 int res;
2674
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002675 spin_lock_irqsave(&ha->hardware_lock, flags);
2676 if (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
2677 cmd->state = QLA_TGT_STATE_PROCESSED;
2678 if (cmd->sess->logout_completed)
2679 /* no need to terminate. FW already freed exchange. */
2680 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2681 else
Quinn Trana07100e2015-12-07 19:48:57 -05002682 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002683 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2684 return 0;
2685 }
2686 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2687
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002688 memset(&prm, 0, sizeof(prm));
2689 qlt_check_srr_debug(cmd, &xmit_type);
2690
2691 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
Quinn Tranf83adb62014-04-11 16:54:43 -04002692 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
2693 (xmit_type & QLA_TGT_XMIT_STATUS) ?
2694 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
2695 &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002696
2697 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2698 &full_req_cnt);
2699 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002700 return res;
2701 }
2702
2703 spin_lock_irqsave(&ha->hardware_lock, flags);
2704
Himanshu Madhanice1025c2015-12-17 14:56:58 -05002705 if (xmit_type == QLA_TGT_XMIT_STATUS)
2706 vha->tgt_counters.core_qla_snd_status++;
2707 else
2708 vha->tgt_counters.core_qla_que_buf++;
2709
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002710 if (!vha->flags.online || cmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002711 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002712 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002713 * previous life, just abort the processing.
2714 */
2715 cmd->state = QLA_TGT_STATE_PROCESSED;
2716 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2717 ql_dbg(ql_dbg_async, vha, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002718 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
2719 vha->flags.online, qla2x00_reset_active(vha),
2720 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002721 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2722 return 0;
2723 }
2724
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002725 /* Does F/W have an IOCBs for this request */
2726 res = qlt_check_reserve_free_req(vha, full_req_cnt);
2727 if (unlikely(res))
2728 goto out_unmap_unlock;
2729
Quinn Tranf83adb62014-04-11 16:54:43 -04002730 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
2731 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2732 else
2733 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Quinn Tran810e30b2015-06-10 11:05:20 -04002734 if (unlikely(res != 0)) {
2735 vha->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002736 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04002737 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002738
2739 pkt = (struct ctio7_to_24xx *)prm.pkt;
2740
2741 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2742 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002743 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002744 CTIO7_FLAGS_STATUS_MODE_0);
2745
Quinn Tranf83adb62014-04-11 16:54:43 -04002746 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2747 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002748
2749 if (prm.add_status_pkt == 0) {
2750 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2751 pkt->u.status0.scsi_status =
2752 cpu_to_le16(prm.rq_result);
2753 pkt->u.status0.residual =
2754 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07002755 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002756 CTIO7_FLAGS_SEND_STATUS);
2757 if (qlt_need_explicit_conf(ha, cmd, 0)) {
2758 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002759 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002760 CTIO7_FLAGS_EXPLICIT_CONFORM |
2761 CTIO7_FLAGS_CONFORM_REQ);
2762 }
2763 }
2764
2765 } else {
2766 /*
2767 * We have already made sure that there is sufficient
2768 * amount of request entries to not drop HW lock in
2769 * req_pkt().
2770 */
2771 struct ctio7_to_24xx *ctio =
2772 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2773
Arun Easi667024a2014-09-25 06:14:47 -04002774 ql_dbg(ql_dbg_io, vha, 0x305e,
2775 "Building additional status packet 0x%p.\n",
2776 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002777
Quinn Tranf83adb62014-04-11 16:54:43 -04002778 /*
2779 * T10Dif: ctio_crc2_to_fw overlay ontop of
2780 * ctio7_to_24xx
2781 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002782 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04002783 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002784 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04002785 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002786 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07002787 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002788 CTIO7_FLAGS_DATA_IN);
2789
2790 /* Real finish is ctio_m1's finish */
2791 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07002792 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002793 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04002794
2795 /* qlt_24xx_init_ctio_to_isp will correct
2796 * all neccessary fields that's part of CTIO7.
2797 * There should be no residual of CTIO-CRC2 data.
2798 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002799 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2800 &prm);
2801 pr_debug("Status CTIO7: %p\n", ctio);
2802 }
2803 } else
2804 qlt_24xx_init_ctio_to_isp(pkt, &prm);
2805
2806
2807 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04002808 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002809
Himanshu Madhani63163e02014-09-25 06:14:59 -04002810 /* Memory Barrier */
2811 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002812 qla2x00_start_iocbs(vha, vha->req);
2813 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2814
2815 return 0;
2816
2817out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04002818 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002819 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2820
2821 return res;
2822}
2823EXPORT_SYMBOL(qlt_xmit_response);
2824
2825int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2826{
2827 struct ctio7_to_24xx *pkt;
2828 struct scsi_qla_host *vha = cmd->vha;
2829 struct qla_hw_data *ha = vha->hw;
2830 struct qla_tgt *tgt = cmd->tgt;
2831 struct qla_tgt_prm prm;
2832 unsigned long flags;
2833 int res = 0;
2834
2835 memset(&prm, 0, sizeof(prm));
2836 prm.cmd = cmd;
2837 prm.tgt = tgt;
2838 prm.sg = NULL;
2839 prm.req_cnt = 1;
2840
2841 /* Send marker if required */
2842 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2843 return -EIO;
2844
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002845 /* Calculate number of entries and segments required */
2846 if (qlt_pci_map_calc_cnt(&prm) != 0)
2847 return -EAGAIN;
2848
2849 spin_lock_irqsave(&ha->hardware_lock, flags);
2850
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002851 if (!vha->flags.online || (cmd->reset_count != ha->chip_reset) ||
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002852 (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)) {
Arun Easib6a029e2014-09-25 06:14:52 -04002853 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002854 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002855 * previous life, just abort the processing.
2856 */
2857 cmd->state = QLA_TGT_STATE_NEED_DATA;
2858 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2859 ql_dbg(ql_dbg_async, vha, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002860 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2861 vha->flags.online, qla2x00_reset_active(vha),
2862 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002863 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2864 return 0;
2865 }
2866
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002867 /* Does F/W have an IOCBs for this request */
2868 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2869 if (res != 0)
2870 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04002871 if (cmd->se_cmd.prot_op)
2872 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2873 else
2874 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002875
Quinn Tran810e30b2015-06-10 11:05:20 -04002876 if (unlikely(res != 0)) {
2877 vha->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002878 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04002879 }
2880
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002881 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07002882 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002883 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002884
2885 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2886 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002887
2888 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04002889 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002890
Himanshu Madhani63163e02014-09-25 06:14:59 -04002891 /* Memory Barrier */
2892 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002893 qla2x00_start_iocbs(vha, vha->req);
2894 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2895
2896 return res;
2897
2898out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04002899 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002900 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2901
2902 return res;
2903}
2904EXPORT_SYMBOL(qlt_rdy_to_xfer);
2905
Quinn Tranf83adb62014-04-11 16:54:43 -04002906
2907/*
2908 * Checks the guard or meta-data for the type of error
2909 * detected by the HBA.
2910 */
2911static inline int
2912qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
2913 struct ctio_crc_from_fw *sts)
2914{
2915 uint8_t *ap = &sts->actual_dif[0];
2916 uint8_t *ep = &sts->expected_dif[0];
2917 uint32_t e_ref_tag, a_ref_tag;
2918 uint16_t e_app_tag, a_app_tag;
2919 uint16_t e_guard, a_guard;
2920 uint64_t lba = cmd->se_cmd.t_task_lba;
2921
2922 a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
2923 a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
2924 a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
2925
2926 e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
2927 e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
2928 e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
2929
2930 ql_dbg(ql_dbg_tgt, vha, 0xe075,
2931 "iocb(s) %p Returned STATUS.\n", sts);
2932
2933 ql_dbg(ql_dbg_tgt, vha, 0xf075,
Hans Wennborgfc385042014-08-05 21:43:29 -07002934 "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 -04002935 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2936 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
2937
2938 /*
2939 * Ignore sector if:
2940 * For type 3: ref & app tag is all 'f's
2941 * For type 0,1,2: app tag is all 'f's
2942 */
2943 if ((a_app_tag == 0xffff) &&
2944 ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
2945 (a_ref_tag == 0xffffffff))) {
2946 uint32_t blocks_done;
2947
2948 /* 2TB boundary case covered automatically with this */
2949 blocks_done = e_ref_tag - (uint32_t)lba + 1;
2950 cmd->se_cmd.bad_sector = e_ref_tag;
2951 cmd->se_cmd.pi_err = 0;
2952 ql_dbg(ql_dbg_tgt, vha, 0xf074,
2953 "need to return scsi good\n");
2954
2955 /* Update protection tag */
2956 if (cmd->prot_sg_cnt) {
Bart Van Assche52c82822015-07-09 07:23:26 -07002957 uint32_t i, k = 0, num_ent;
Quinn Tranf83adb62014-04-11 16:54:43 -04002958 struct scatterlist *sg, *sgl;
2959
2960
2961 sgl = cmd->prot_sg;
2962
2963 /* Patch the corresponding protection tags */
2964 for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
2965 num_ent = sg_dma_len(sg) / 8;
2966 if (k + num_ent < blocks_done) {
2967 k += num_ent;
2968 continue;
2969 }
Quinn Tranf83adb62014-04-11 16:54:43 -04002970 k = blocks_done;
2971 break;
2972 }
2973
2974 if (k != blocks_done) {
2975 ql_log(ql_log_warn, vha, 0xf076,
2976 "unexpected tag values tag:lba=%u:%llu)\n",
2977 e_ref_tag, (unsigned long long)lba);
2978 goto out;
2979 }
2980
2981#if 0
2982 struct sd_dif_tuple *spt;
2983 /* TODO:
2984 * This section came from initiator. Is it valid here?
2985 * should ulp be override with actual val???
2986 */
2987 spt = page_address(sg_page(sg)) + sg->offset;
2988 spt += j;
2989
2990 spt->app_tag = 0xffff;
2991 if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
2992 spt->ref_tag = 0xffffffff;
2993#endif
2994 }
2995
2996 return 0;
2997 }
2998
2999 /* check guard */
3000 if (e_guard != a_guard) {
3001 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
3002 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3003
3004 ql_log(ql_log_warn, vha, 0xe076,
3005 "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",
3006 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3007 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3008 a_guard, e_guard, cmd);
3009 goto out;
3010 }
3011
3012 /* check ref tag */
3013 if (e_ref_tag != a_ref_tag) {
3014 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
3015 cmd->se_cmd.bad_sector = e_ref_tag;
3016
3017 ql_log(ql_log_warn, vha, 0xe077,
3018 "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",
3019 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3020 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3021 a_guard, e_guard, cmd);
3022 goto out;
3023 }
3024
3025 /* check appl tag */
3026 if (e_app_tag != a_app_tag) {
3027 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
3028 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3029
3030 ql_log(ql_log_warn, vha, 0xe078,
3031 "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",
3032 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3033 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3034 a_guard, e_guard, cmd);
3035 goto out;
3036 }
3037out:
3038 return 1;
3039}
3040
3041
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003042/* If hardware_lock held on entry, might drop it, then reaquire */
3043/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003044static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3045 struct imm_ntfy_from_isp *ntfy)
3046{
3047 struct nack_to_isp *nack;
3048 struct qla_hw_data *ha = vha->hw;
3049 request_t *pkt;
3050 int ret = 0;
3051
3052 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3053 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3054
3055 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3056 if (pkt == NULL) {
3057 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3058 "qla_target(%d): %s failed: unable to allocate "
3059 "request packet\n", vha->vp_idx, __func__);
3060 return -ENOMEM;
3061 }
3062
3063 pkt->entry_type = NOTIFY_ACK_TYPE;
3064 pkt->entry_count = 1;
Quinn Tran4f060732016-12-23 18:06:13 -08003065 pkt->handle = QLA_TGT_SKIP_HANDLE;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003066
3067 nack = (struct nack_to_isp *)pkt;
3068 nack->ox_id = ntfy->ox_id;
3069
3070 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3071 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3072 nack->u.isp24.flags = ntfy->u.isp24.flags &
3073 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3074 }
3075
3076 /* terminate */
3077 nack->u.isp24.flags |=
3078 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3079
3080 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3081 nack->u.isp24.status = ntfy->u.isp24.status;
3082 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3083 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3084 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3085 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3086 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3087 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3088
3089 qla2x00_start_iocbs(vha, vha->req);
3090 return ret;
3091}
3092
3093static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3094 struct imm_ntfy_from_isp *imm, int ha_locked)
3095{
3096 unsigned long flags = 0;
3097 int rc;
3098
3099 if (qlt_issue_marker(vha, ha_locked) < 0)
3100 return;
3101
3102 if (ha_locked) {
3103 rc = __qlt_send_term_imm_notif(vha, imm);
3104
3105#if 0 /* Todo */
3106 if (rc == -ENOMEM)
3107 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
Bart Van Assche91f42b32016-03-30 15:25:21 -07003108#else
3109 if (rc) {
3110 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003111#endif
3112 goto done;
3113 }
3114
3115 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3116 rc = __qlt_send_term_imm_notif(vha, imm);
3117
3118#if 0 /* Todo */
3119 if (rc == -ENOMEM)
3120 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3121#endif
3122
3123done:
3124 if (!ha_locked)
3125 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3126}
3127
3128/* If hardware_lock held on entry, might drop it, then reaquire */
3129/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003130static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3131 struct qla_tgt_cmd *cmd,
3132 struct atio_from_isp *atio)
3133{
3134 struct ctio7_to_24xx *ctio24;
3135 struct qla_hw_data *ha = vha->hw;
3136 request_t *pkt;
3137 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003138 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003139
3140 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3141
Arun Easib6a029e2014-09-25 06:14:52 -04003142 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003143 if (pkt == NULL) {
3144 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3145 "qla_target(%d): %s failed: unable to allocate "
3146 "request packet\n", vha->vp_idx, __func__);
3147 return -ENOMEM;
3148 }
3149
3150 if (cmd != NULL) {
3151 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3152 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3153 "qla_target(%d): Terminating cmd %p with "
3154 "incorrect state %d\n", vha->vp_idx, cmd,
3155 cmd->state);
3156 } else
3157 ret = 1;
3158 }
3159
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003160 vha->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003161 pkt->entry_count = 1;
3162 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3163
3164 ctio24 = (struct ctio7_to_24xx *)pkt;
3165 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003166 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003167 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003168 ctio24->vp_index = vha->vp_idx;
3169 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3170 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3171 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3172 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3173 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07003174 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003175 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003176 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3177 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003178
3179 /* Most likely, it isn't needed */
3180 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3181 &atio->u.isp24.fcp_cmnd.add_cdb[
3182 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3183 if (ctio24->u.status1.residual != 0)
3184 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3185
Himanshu Madhani63163e02014-09-25 06:14:59 -04003186 /* Memory Barrier */
3187 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003188 qla2x00_start_iocbs(vha, vha->req);
3189 return ret;
3190}
3191
3192static void qlt_send_term_exchange(struct scsi_qla_host *vha,
Quinn Trana07100e2015-12-07 19:48:57 -05003193 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3194 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003195{
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003196 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003197 int rc;
3198
3199 if (qlt_issue_marker(vha, ha_locked) < 0)
3200 return;
3201
3202 if (ha_locked) {
3203 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003204 if (rc == -ENOMEM)
3205 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003206 goto done;
3207 }
3208 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3209 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003210 if (rc == -ENOMEM)
3211 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003212
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003213done:
Quinn Trana07100e2015-12-07 19:48:57 -05003214 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003215 if (cmd->sg_mapped)
3216 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003217 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3218 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003219
3220 if (!ha_locked)
3221 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3222
Quinn Tran7b898542014-04-11 16:54:44 -04003223 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003224}
3225
Quinn Tran33e79972014-09-25 06:14:55 -04003226static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3227{
3228 struct list_head free_list;
3229 struct qla_tgt_cmd *cmd, *tcmd;
3230
3231 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003232 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003233
3234 cmd = tcmd = NULL;
3235 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3236 INIT_LIST_HEAD(&free_list);
3237 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3238
3239 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3240 list_del(&cmd->cmd_list);
3241 /* This cmd was never sent to TCM. There is no need
3242 * to schedule free or call free_cmd
3243 */
3244 qlt_free_cmd(cmd);
3245 vha->hw->tgt.num_qfull_cmds_alloc--;
3246 }
3247 }
3248 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3249}
3250
3251static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3252{
3253 uint32_t total_leaked;
3254
3255 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3256
3257 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3258 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3259
3260 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3261 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003262 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003263
3264 if (IS_P3P_TYPE(vha->hw))
3265 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3266 else
3267 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3268 qla2xxx_wake_dpc(vha);
3269 }
3270
3271}
3272
Quinn Trana07100e2015-12-07 19:48:57 -05003273int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003274{
3275 struct qla_tgt *tgt = cmd->tgt;
3276 struct scsi_qla_host *vha = tgt->vha;
3277 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003278 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003279
3280 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3281 "qla_target(%d): terminating exchange for aborted cmd=%p "
3282 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3283 se_cmd->tag);
3284
Quinn Trana07100e2015-12-07 19:48:57 -05003285 spin_lock_irqsave(&cmd->cmd_lock, flags);
3286 if (cmd->aborted) {
3287 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3288 /*
3289 * It's normal to see 2 calls in this path:
3290 * 1) XFER Rdy completion + CMD_T_ABORT
3291 * 2) TCM TMR - drain_state_list
3292 */
3293 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
3294 "multiple abort. %p transport_state %x, t_state %x,"
3295 " se_cmd_flags %x \n", cmd, cmd->se_cmd.transport_state,
3296 cmd->se_cmd.t_state,cmd->se_cmd.se_cmd_flags);
3297 return EIO;
3298 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003299 cmd->aborted = 1;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003300 cmd->cmd_flags |= BIT_6;
Quinn Trana07100e2015-12-07 19:48:57 -05003301 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003302
Quinn Trana07100e2015-12-07 19:48:57 -05003303 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3304 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003305}
3306EXPORT_SYMBOL(qlt_abort_cmd);
3307
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003308void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3309{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003310 struct qla_tgt_sess *sess = cmd->sess;
3311
Quinn Tranf83adb62014-04-11 16:54:43 -04003312 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3313 "%s: se_cmd[%p] ox_id %04x\n",
3314 __func__, &cmd->se_cmd,
3315 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003316
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003317 BUG_ON(cmd->cmd_in_wq);
3318
Quinn Trana07100e2015-12-07 19:48:57 -05003319 if (cmd->sg_mapped)
3320 qlt_unmap_sg(cmd->vha, cmd);
3321
Quinn Tran33e79972014-09-25 06:14:55 -04003322 if (!cmd->q_full)
3323 qlt_decr_num_pend_cmds(cmd->vha);
3324
Quinn Tranf83adb62014-04-11 16:54:43 -04003325 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003326 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003327 if (unlikely(cmd->free_sg))
3328 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003329
3330 if (!sess || !sess->se_sess) {
3331 WARN_ON(1);
3332 return;
3333 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003334 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003335 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003336}
3337EXPORT_SYMBOL(qlt_free_cmd);
3338
3339/* ha->hardware_lock supposed to be held on entry */
3340static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
3341 struct qla_tgt_cmd *cmd, void *ctio)
3342{
3343 struct qla_tgt_srr_ctio *sc;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003344 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003345 struct qla_tgt_srr_imm *imm;
3346
3347 tgt->ctio_srr_id++;
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003348 cmd->cmd_flags |= BIT_15;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003349
3350 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
3351 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
3352
3353 if (!ctio) {
3354 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
3355 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
3356 vha->vp_idx);
3357 return -EINVAL;
3358 }
3359
3360 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
3361 if (sc != NULL) {
3362 sc->cmd = cmd;
3363 /* IRQ is already OFF */
3364 spin_lock(&tgt->srr_lock);
3365 sc->srr_id = tgt->ctio_srr_id;
3366 list_add_tail(&sc->srr_list_entry,
3367 &tgt->srr_ctio_list);
3368 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
3369 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
3370 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3371 int found = 0;
3372 list_for_each_entry(imm, &tgt->srr_imm_list,
3373 srr_list_entry) {
3374 if (imm->srr_id == sc->srr_id) {
3375 found = 1;
3376 break;
3377 }
3378 }
3379 if (found) {
3380 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
3381 "Scheduling srr work\n");
3382 schedule_work(&tgt->srr_work);
3383 } else {
3384 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
3385 "qla_target(%d): imm_srr_id "
3386 "== ctio_srr_id (%d), but there is no "
3387 "corresponding SRR IMM, deleting CTIO "
3388 "SRR %p\n", vha->vp_idx,
3389 tgt->ctio_srr_id, sc);
3390 list_del(&sc->srr_list_entry);
3391 spin_unlock(&tgt->srr_lock);
3392
3393 kfree(sc);
3394 return -EINVAL;
3395 }
3396 }
3397 spin_unlock(&tgt->srr_lock);
3398 } else {
3399 struct qla_tgt_srr_imm *ti;
3400
3401 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
3402 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
3403 vha->vp_idx);
3404 spin_lock(&tgt->srr_lock);
3405 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
3406 srr_list_entry) {
3407 if (imm->srr_id == tgt->ctio_srr_id) {
3408 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
3409 "IMM SRR %p deleted (id %d)\n",
3410 imm, imm->srr_id);
3411 list_del(&imm->srr_list_entry);
3412 qlt_reject_free_srr_imm(vha, imm, 1);
3413 }
3414 }
3415 spin_unlock(&tgt->srr_lock);
3416
3417 return -ENOMEM;
3418 }
3419
3420 return 0;
3421}
3422
3423/*
3424 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3425 */
3426static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3427 struct qla_tgt_cmd *cmd, uint32_t status)
3428{
3429 int term = 0;
3430
3431 if (ctio != NULL) {
3432 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3433 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003434 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003435 } else
3436 term = 1;
3437
3438 if (term)
Quinn Trana07100e2015-12-07 19:48:57 -05003439 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003440
3441 return term;
3442}
3443
3444/* ha->hardware_lock supposed to be held on entry */
3445static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3446 uint32_t handle)
3447{
3448 struct qla_hw_data *ha = vha->hw;
3449
3450 handle--;
3451 if (ha->tgt.cmds[handle] != NULL) {
3452 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3453 ha->tgt.cmds[handle] = NULL;
3454 return cmd;
3455 } else
3456 return NULL;
3457}
3458
3459/* ha->hardware_lock supposed to be held on entry */
3460static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3461 uint32_t handle, void *ctio)
3462{
3463 struct qla_tgt_cmd *cmd = NULL;
3464
3465 /* Clear out internal marks */
3466 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3467 CTIO_INTERMEDIATE_HANDLE_MARK);
3468
3469 if (handle != QLA_TGT_NULL_HANDLE) {
Arun Easi667024a2014-09-25 06:14:47 -04003470 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003471 return NULL;
Arun Easi667024a2014-09-25 06:14:47 -04003472
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003473 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05003474 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003475 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3476 "qla_target(%d): Wrong handle %x received\n",
3477 vha->vp_idx, handle);
3478 return NULL;
3479 }
3480 cmd = qlt_get_cmd(vha, handle);
3481 if (unlikely(cmd == NULL)) {
3482 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3483 "qla_target(%d): Suspicious: unable to "
3484 "find the command with handle %x\n", vha->vp_idx,
3485 handle);
3486 return NULL;
3487 }
3488 } else if (ctio != NULL) {
3489 /* We can't get loop ID from CTIO7 */
3490 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3491 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3492 "support NULL handles\n", vha->vp_idx);
3493 return NULL;
3494 }
3495
3496 return cmd;
3497}
3498
Arun Easic0cb4492014-09-25 06:14:51 -04003499/* hardware_lock should be held by caller. */
3500static void
3501qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3502{
3503 struct qla_hw_data *ha = vha->hw;
3504 uint32_t handle;
3505
3506 if (cmd->sg_mapped)
3507 qlt_unmap_sg(vha, cmd);
3508
3509 handle = qlt_make_handle(vha);
3510
3511 /* TODO: fix debug message type and ids. */
3512 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3513 ql_dbg(ql_dbg_io, vha, 0xff00,
3514 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3515 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3516 cmd->write_data_transferred = 0;
3517 cmd->state = QLA_TGT_STATE_DATA_IN;
3518
3519 ql_dbg(ql_dbg_io, vha, 0xff01,
3520 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3521
3522 ha->tgt.tgt_ops->handle_data(cmd);
3523 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003524 } else {
3525 ql_dbg(ql_dbg_io, vha, 0xff03,
3526 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3527 cmd->state);
3528 dump_stack();
3529 }
3530
Quinn Trane5fdee82015-06-10 11:05:21 -04003531 cmd->cmd_flags |= BIT_17;
Arun Easic0cb4492014-09-25 06:14:51 -04003532 ha->tgt.tgt_ops->free_cmd(cmd);
3533}
3534
3535void
3536qlt_host_reset_handler(struct qla_hw_data *ha)
3537{
3538 struct qla_tgt_cmd *cmd;
3539 unsigned long flags;
3540 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3541 scsi_qla_host_t *vha = NULL;
3542 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3543 uint32_t i;
3544
3545 if (!base_vha->hw->tgt.tgt_ops)
3546 return;
3547
3548 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3549 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3550 "Target mode disabled\n");
3551 return;
3552 }
3553
3554 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3555 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3556 base_vha->dpc_flags);
3557
3558 spin_lock_irqsave(&ha->hardware_lock, flags);
3559 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3560 cmd = qlt_get_cmd(base_vha, i);
3561 if (!cmd)
3562 continue;
3563 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3564 vha = cmd->vha;
3565 qlt_abort_cmd_on_host_reset(vha, cmd);
3566 }
3567 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3568}
3569
3570
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003571/*
3572 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3573 */
3574static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3575 uint32_t status, void *ctio)
3576{
3577 struct qla_hw_data *ha = vha->hw;
3578 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003579 struct qla_tgt_cmd *cmd;
3580
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003581 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3582 /* That could happen only in case of an error/reset/abort */
3583 if (status != CTIO_SUCCESS) {
3584 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3585 "Intermediate CTIO received"
3586 " (status %x)\n", status);
3587 }
3588 return;
3589 }
3590
3591 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003592 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003593 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003594
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003595 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003596 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003597
Joern Engelf9b67212014-09-16 16:23:18 -04003598 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003599
3600 if (unlikely(status != CTIO_SUCCESS)) {
3601 switch (status & 0xFFFF) {
3602 case CTIO_LIP_RESET:
3603 case CTIO_TARGET_RESET:
3604 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003605 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003606 case CTIO_TIMEOUT:
3607 case CTIO_INVALID_RX_ID:
3608 /* They are OK */
3609 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3610 "qla_target(%d): CTIO with "
3611 "status %#x received, state %x, se_cmd %p, "
3612 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3613 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3614 status, cmd->state, se_cmd);
3615 break;
3616
3617 case CTIO_PORT_LOGGED_OUT:
3618 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003619 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003620 int logged_out =
3621 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3622
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003623 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003624 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003625 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003626 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003627 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003628
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003629 if (logged_out && cmd->sess) {
3630 /*
3631 * Session is already logged out, but we need
3632 * to notify initiator, who's not aware of this
3633 */
3634 cmd->sess->logout_on_delete = 0;
3635 cmd->sess->send_els_logo = 1;
3636 qlt_schedule_sess_for_deletion(cmd->sess, true);
3637 }
3638 break;
3639 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003640 case CTIO_SRR_RECEIVED:
3641 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
3642 "qla_target(%d): CTIO with SRR_RECEIVED"
3643 " status %x received (state %x, se_cmd %p)\n",
3644 vha->vp_idx, status, cmd->state, se_cmd);
3645 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
3646 break;
3647 else
3648 return;
3649
Quinn Tranf83adb62014-04-11 16:54:43 -04003650 case CTIO_DIF_ERROR: {
3651 struct ctio_crc_from_fw *crc =
3652 (struct ctio_crc_from_fw *)ctio;
3653 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3654 "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
3655 vha->vp_idx, status, cmd->state, se_cmd,
3656 *((u64 *)&crc->actual_dif[0]),
3657 *((u64 *)&crc->expected_dif[0]));
3658
3659 if (qlt_handle_dif_error(vha, cmd, ctio)) {
3660 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3661 /* scsi Write/xfer rdy complete */
3662 goto skip_term;
3663 } else {
3664 /* scsi read/xmit respond complete
3665 * call handle dif to send scsi status
3666 * rather than terminate exchange.
3667 */
3668 cmd->state = QLA_TGT_STATE_PROCESSED;
3669 ha->tgt.tgt_ops->handle_dif_err(cmd);
3670 return;
3671 }
3672 } else {
3673 /* Need to generate a SCSI good completion.
3674 * because FW did not send scsi status.
3675 */
3676 status = 0;
3677 goto skip_term;
3678 }
3679 break;
3680 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003681 default:
3682 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003683 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003684 vha->vp_idx, status, cmd->state, se_cmd);
3685 break;
3686 }
3687
Quinn Tran7b898542014-04-11 16:54:44 -04003688
Quinn Tran193b50b2015-12-17 14:57:03 -05003689 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003690 * cmd is already aborted/terminated, we don't
3691 * need to terminate again. The exchange is already
3692 * cleaned up/freed at FW level. Just cleanup at driver
3693 * level.
3694 */
3695 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003696 (!cmd->aborted)) {
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003697 cmd->cmd_flags |= BIT_13;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003698 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3699 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003700 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003701 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003702skip_term:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003703
3704 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Trane5fdee82015-06-10 11:05:21 -04003705 cmd->cmd_flags |= BIT_12;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003706 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003707 cmd->state = QLA_TGT_STATE_DATA_IN;
3708
Bart Van Assche52c82822015-07-09 07:23:26 -07003709 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003710 cmd->write_data_transferred = 1;
3711
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003712 ha->tgt.tgt_ops->handle_data(cmd);
3713 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003714 } else if (cmd->aborted) {
Quinn Trane5fdee82015-06-10 11:05:21 -04003715 cmd->cmd_flags |= BIT_18;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003716 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003717 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003718 } else {
Quinn Trane5fdee82015-06-10 11:05:21 -04003719 cmd->cmd_flags |= BIT_19;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003720 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3721 "qla_target(%d): A command in state (%d) should "
3722 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3723 }
3724
Quinn Tran7b898542014-04-11 16:54:44 -04003725 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003726 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003727 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3728 dump_stack();
3729 }
3730
3731 ha->tgt.tgt_ops->free_cmd(cmd);
3732}
3733
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003734static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3735 uint8_t task_codes)
3736{
3737 int fcp_task_attr;
3738
3739 switch (task_codes) {
3740 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003741 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003742 break;
3743 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003744 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003745 break;
3746 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003747 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003748 break;
3749 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003750 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003751 break;
3752 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003753 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003754 break;
3755 default:
3756 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3757 "qla_target: unknown task code %x, use ORDERED instead\n",
3758 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003759 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003760 break;
3761 }
3762
3763 return fcp_task_attr;
3764}
3765
3766static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
3767 uint8_t *);
3768/*
3769 * Process context for I/O path into tcm_qla2xxx code
3770 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003771static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003772{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003773 scsi_qla_host_t *vha = cmd->vha;
3774 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003775 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003776 struct qla_tgt_sess *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003777 struct atio_from_isp *atio = &cmd->atio;
3778 unsigned char *cdb;
3779 unsigned long flags;
3780 uint32_t data_length;
3781 int ret, fcp_task_attr, data_dir, bidi = 0;
3782
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003783 cmd->cmd_in_wq = 0;
3784 cmd->cmd_flags |= BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003785 if (tgt->tgt_stop)
3786 goto out_term;
3787
Quinn Tran193b50b2015-12-17 14:57:03 -05003788 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003789 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3790 "cmd with tag %u is aborted\n",
3791 cmd->atio.u.isp24.exchange_addr);
3792 goto out_term;
3793 }
3794
Quinn Trana07100e2015-12-07 19:48:57 -05003795 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003796 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02003797 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003798 cmd->unpacked_lun = scsilun_to_int(
3799 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3800
3801 if (atio->u.isp24.fcp_cmnd.rddata &&
3802 atio->u.isp24.fcp_cmnd.wrdata) {
3803 bidi = 1;
3804 data_dir = DMA_TO_DEVICE;
3805 } else if (atio->u.isp24.fcp_cmnd.rddata)
3806 data_dir = DMA_FROM_DEVICE;
3807 else if (atio->u.isp24.fcp_cmnd.wrdata)
3808 data_dir = DMA_TO_DEVICE;
3809 else
3810 data_dir = DMA_NONE;
3811
3812 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3813 atio->u.isp24.fcp_cmnd.task_attr);
3814 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3815 &atio->u.isp24.fcp_cmnd.add_cdb[
3816 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3817
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003818 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3819 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003820 if (ret != 0)
3821 goto out_term;
3822 /*
3823 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3824 */
Quinn Tran75601512015-12-17 14:57:04 -05003825 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02003826 qlt_put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05003827 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003828 return;
3829
3830out_term:
Arun Easi667024a2014-09-25 06:14:47 -04003831 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003832 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07003833 * cmd has not sent to target yet, so pass NULL as the second
3834 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003835 */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003836 cmd->cmd_flags |= BIT_2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003837 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003838 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04003839
3840 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003841 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Jörn Engel08234e32013-06-12 16:27:54 -04003842 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tran75601512015-12-17 14:57:04 -05003843
3844 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02003845 qlt_put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05003846 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003847}
3848
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003849static void qlt_do_work(struct work_struct *work)
3850{
3851 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003852 scsi_qla_host_t *vha = cmd->vha;
3853 unsigned long flags;
3854
3855 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3856 list_del(&cmd->cmd_list);
3857 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003858
3859 __qlt_do_work(cmd);
3860}
3861
3862static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
3863 struct qla_tgt_sess *sess,
3864 struct atio_from_isp *atio)
3865{
3866 struct se_session *se_sess = sess->se_sess;
3867 struct qla_tgt_cmd *cmd;
3868 int tag;
3869
3870 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
3871 if (tag < 0)
3872 return NULL;
3873
3874 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
3875 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
3876
3877 memcpy(&cmd->atio, atio, sizeof(*atio));
3878 cmd->state = QLA_TGT_STATE_NEW;
3879 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04003880 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003881 cmd->vha = vha;
3882 cmd->se_cmd.map_tag = tag;
3883 cmd->sess = sess;
3884 cmd->loop_id = sess->loop_id;
3885 cmd->conf_compl_supported = sess->conf_compl_supported;
3886
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04003887 cmd->cmd_flags = 0;
3888 cmd->jiffies_at_alloc = get_jiffies_64();
3889
3890 cmd->reset_count = vha->hw->chip_reset;
3891
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003892 return cmd;
3893}
3894
3895static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
3896 uint16_t);
3897
3898static void qlt_create_sess_from_atio(struct work_struct *work)
3899{
3900 struct qla_tgt_sess_op *op = container_of(work,
3901 struct qla_tgt_sess_op, work);
3902 scsi_qla_host_t *vha = op->vha;
3903 struct qla_hw_data *ha = vha->hw;
3904 struct qla_tgt_sess *sess;
3905 struct qla_tgt_cmd *cmd;
3906 unsigned long flags;
3907 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
3908
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003909 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3910 list_del(&op->cmd_list);
3911 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
3912
3913 if (op->aborted) {
3914 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
3915 "sess_op with tag %u is aborted\n",
3916 op->atio.u.isp24.exchange_addr);
3917 goto out_term;
3918 }
3919
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003920 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003921 "qla_target(%d): Unable to find wwn login"
3922 " (s_id %x:%x:%x), trying to create it manually\n",
3923 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003924
3925 if (op->atio.u.raw.entry_count > 1) {
3926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003927 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003928 goto out_term;
3929 }
3930
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003931 sess = qlt_make_local_sess(vha, s_id);
3932 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003933
3934 if (!sess)
3935 goto out_term;
3936 /*
3937 * Now obtain a pre-allocated session tag using the original op->atio
3938 * packet header, and dispatch into __qlt_do_work() using the existing
3939 * process context.
3940 */
3941 cmd = qlt_get_tag(vha, sess, &op->atio);
3942 if (!cmd) {
3943 spin_lock_irqsave(&ha->hardware_lock, flags);
3944 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02003945 qlt_put_sess(sess);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003946 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3947 kfree(op);
3948 return;
3949 }
3950 /*
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02003951 * __qlt_do_work() will call qlt_put_sess() to release
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003952 * the extra reference taken above by qlt_make_local_sess()
3953 */
3954 __qlt_do_work(cmd);
3955 kfree(op);
3956 return;
3957
3958out_term:
3959 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003960 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003961 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3962 kfree(op);
3963
3964}
3965
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003966/* ha->hardware_lock supposed to be held on entry */
3967static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
3968 struct atio_from_isp *atio)
3969{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003970 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003971 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003972 struct qla_tgt_sess *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003973 struct qla_tgt_cmd *cmd;
3974
3975 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04003976 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003977 "New command while device %p is shutting down\n", tgt);
3978 return -EFAULT;
3979 }
3980
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003981 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
3982 if (unlikely(!sess)) {
3983 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
3984 GFP_ATOMIC);
3985 if (!op)
3986 return -ENOMEM;
3987
3988 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04003989 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003990
3991 spin_lock(&vha->cmd_list_lock);
3992 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
3993 spin_unlock(&vha->cmd_list_lock);
3994
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003995 INIT_WORK(&op->work, qlt_create_sess_from_atio);
3996 queue_work(qla_tgt_wq, &op->work);
3997 return 0;
3998 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04003999
4000 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4001 * session deletion, but it's still in sess_del_work wq */
4002 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
4003 ql_dbg(ql_dbg_io, vha, 0x3061,
4004 "New command while old session %p is being deleted\n",
4005 sess);
4006 return -EFAULT;
4007 }
4008
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004009 /*
4010 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4011 */
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004012 kref_get(&sess->sess_kref);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004013
4014 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004015 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004016 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004017 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02004018 qlt_put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004019 return -ENOMEM;
4020 }
4021
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004022 cmd->cmd_in_wq = 1;
4023 cmd->cmd_flags |= BIT_0;
Quinn Tran5327c7d2016-02-10 18:59:14 -05004024 cmd->se_cmd.cpuid = ha->msix_count ?
4025 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004026
4027 spin_lock(&vha->cmd_list_lock);
4028 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4029 spin_unlock(&vha->cmd_list_lock);
4030
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004031 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Tranfb3269b2015-12-17 14:57:06 -05004032 if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004033 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4034 queue_work_on(smp_processor_id(), qla_tgt_wq,
4035 &cmd->work);
4036 else
4037 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4038 &cmd->work);
4039 } else {
4040 queue_work(qla_tgt_wq, &cmd->work);
4041 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004042 return 0;
4043
4044}
4045
4046/* ha->hardware_lock supposed to be held on entry */
4047static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
4048 int fn, void *iocb, int flags)
4049{
4050 struct scsi_qla_host *vha = sess->vha;
4051 struct qla_hw_data *ha = vha->hw;
4052 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004053 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004054 int res;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004055
4056 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4057 if (!mcmd) {
4058 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4059 "qla_target(%d): Allocation of management "
4060 "command failed, some commands and their data could "
4061 "leak\n", vha->vp_idx);
4062 return -ENOMEM;
4063 }
4064 memset(mcmd, 0, sizeof(*mcmd));
4065 mcmd->sess = sess;
4066
4067 if (iocb) {
4068 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4069 sizeof(mcmd->orig_iocb.imm_ntfy));
4070 }
4071 mcmd->tmr_func = fn;
4072 mcmd->flags = flags;
Arun Easib6a029e2014-09-25 06:14:52 -04004073 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004074
4075 switch (fn) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004076 case QLA_TGT_LUN_RESET:
Quinn Tranbe92fc32017-01-19 22:27:54 -08004077 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4078 break;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004079 }
4080
Quinn Tranbe92fc32017-01-19 22:27:54 -08004081 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, mcmd->tmr_func, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004082 if (res != 0) {
4083 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4084 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4085 sess->vha->vp_idx, res);
4086 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4087 return -EFAULT;
4088 }
4089
4090 return 0;
4091}
4092
4093/* ha->hardware_lock supposed to be held on entry */
4094static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4095{
4096 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4097 struct qla_hw_data *ha = vha->hw;
4098 struct qla_tgt *tgt;
4099 struct qla_tgt_sess *sess;
4100 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004101 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004102 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004103
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004104 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004105
4106 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004107 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004108
4109 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004110 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4111 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004112 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4113
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004114 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4115
4116 if (!sess) {
4117 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4118 "qla_target(%d): task mgmt fn 0x%x for "
4119 "non-existant session\n", vha->vp_idx, fn);
4120 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4121 sizeof(struct atio_from_isp));
4122 }
4123
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004124 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)
4125 return -EFAULT;
4126
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004127 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4128}
4129
4130/* ha->hardware_lock supposed to be held on entry */
4131static int __qlt_abort_task(struct scsi_qla_host *vha,
4132 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
4133{
4134 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4135 struct qla_hw_data *ha = vha->hw;
4136 struct qla_tgt_mgmt_cmd *mcmd;
4137 uint32_t lun, unpacked_lun;
4138 int rc;
4139
4140 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4141 if (mcmd == NULL) {
4142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4143 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4144 vha->vp_idx, __func__);
4145 return -ENOMEM;
4146 }
4147 memset(mcmd, 0, sizeof(*mcmd));
4148
4149 mcmd->sess = sess;
4150 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4151 sizeof(mcmd->orig_iocb.imm_ntfy));
4152
4153 lun = a->u.isp24.fcp_cmnd.lun;
4154 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
Arun Easi80187f82014-09-25 06:14:53 -04004155 mcmd->reset_count = vha->hw->chip_reset;
Quinn Tranbe92fc32017-01-19 22:27:54 -08004156 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004157
Quinn Tranbe92fc32017-01-19 22:27:54 -08004158 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004159 le16_to_cpu(iocb->u.isp2x.seq_id));
4160 if (rc != 0) {
4161 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4162 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4163 vha->vp_idx, rc);
4164 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4165 return -EFAULT;
4166 }
4167
4168 return 0;
4169}
4170
4171/* ha->hardware_lock supposed to be held on entry */
4172static int qlt_abort_task(struct scsi_qla_host *vha,
4173 struct imm_ntfy_from_isp *iocb)
4174{
4175 struct qla_hw_data *ha = vha->hw;
4176 struct qla_tgt_sess *sess;
4177 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004178 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004179
4180 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4181
Quinn Tran75601512015-12-17 14:57:04 -05004182 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004183 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004184 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4185
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004186 if (sess == NULL) {
4187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4188 "qla_target(%d): task abort for unexisting "
4189 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004190 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004191 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4192 }
4193
4194 return __qlt_abort_task(vha, iocb, sess);
4195}
4196
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004197void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4198{
4199 if (fcport->tgt_session) {
4200 if (rc != MBS_COMMAND_COMPLETE) {
Alexei Potashnikdf673272015-07-14 16:00:46 -04004201 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004202 "%s: se_sess %p / sess %p from"
4203 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4204 " LOGO failed: %#x\n",
4205 __func__,
4206 fcport->tgt_session->se_sess,
4207 fcport->tgt_session,
4208 fcport->port_name, fcport->loop_id,
4209 fcport->d_id.b.domain, fcport->d_id.b.area,
4210 fcport->d_id.b.al_pa, rc);
4211 }
4212
4213 fcport->tgt_session->logout_completed = 1;
4214 }
4215}
4216
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004217/*
4218* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4219*
4220* Schedules sessions with matching port_id/loop_id but different wwn for
4221* deletion. Returns existing session with matching wwn if present.
4222* Null otherwise.
4223*/
4224static struct qla_tgt_sess *
4225qlt_find_sess_invalidate_other(struct qla_tgt *tgt, uint64_t wwn,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004226 port_id_t port_id, uint16_t loop_id, struct qla_tgt_sess **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004227{
4228 struct qla_tgt_sess *sess = NULL, *other_sess;
4229 uint64_t other_wwn;
4230
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004231 *conflict_sess = NULL;
4232
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004233 list_for_each_entry(other_sess, &tgt->sess_list, sess_list_entry) {
4234
4235 other_wwn = wwn_to_u64(other_sess->port_name);
4236
4237 if (wwn == other_wwn) {
4238 WARN_ON(sess);
4239 sess = other_sess;
4240 continue;
4241 }
4242
4243 /* find other sess with nport_id collision */
4244 if (port_id.b24 == other_sess->s_id.b24) {
4245 if (loop_id != other_sess->loop_id) {
4246 ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000c,
4247 "Invalidating sess %p loop_id %d wwn %llx.\n",
4248 other_sess, other_sess->loop_id, other_wwn);
4249
4250 /*
4251 * logout_on_delete is set by default, but another
4252 * session that has the same s_id/loop_id combo
4253 * might have cleared it when requested this session
4254 * deletion, so don't touch it
4255 */
4256 qlt_schedule_sess_for_deletion(other_sess, true);
4257 } else {
4258 /*
4259 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004260 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004261 */
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004262 other_sess->keep_nport_handle = 1;
4263 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004264 qlt_schedule_sess_for_deletion(other_sess,
4265 true);
4266 }
4267 continue;
4268 }
4269
4270 /* find other sess with nport handle collision */
4271 if (loop_id == other_sess->loop_id) {
4272 ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000d,
4273 "Invalidating sess %p loop_id %d wwn %llx.\n",
4274 other_sess, other_sess->loop_id, other_wwn);
4275
4276 /* Same loop_id but different s_id
4277 * Ok to kill and logout */
4278 qlt_schedule_sess_for_deletion(other_sess, true);
4279 }
4280 }
4281
4282 return sess;
4283}
4284
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004285/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4286static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4287{
4288 struct qla_tgt_sess_op *op;
4289 struct qla_tgt_cmd *cmd;
4290 uint32_t key;
4291 int count = 0;
4292
4293 key = (((u32)s_id->b.domain << 16) |
4294 ((u32)s_id->b.area << 8) |
4295 ((u32)s_id->b.al_pa));
4296
4297 spin_lock(&vha->cmd_list_lock);
4298 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4299 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4300 if (op_key == key) {
4301 op->aborted = true;
4302 count++;
4303 }
4304 }
4305 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4306 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4307 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004308 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004309 count++;
4310 }
4311 }
4312 spin_unlock(&vha->cmd_list_lock);
4313
4314 return count;
4315}
4316
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004317/*
4318 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4319 */
4320static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4321 struct imm_ntfy_from_isp *iocb)
4322{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004323 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004324 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004325 struct qla_tgt_sess *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004326 uint64_t wwn;
4327 port_id_t port_id;
4328 uint16_t loop_id;
4329 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004330 int res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004331 qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004332 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004333
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004334 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4335
4336 port_id.b.domain = iocb->u.isp24.port_id[2];
4337 port_id.b.area = iocb->u.isp24.port_id[1];
4338 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4339 port_id.b.rsvd_1 = 0;
4340
4341 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4342
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004343 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004344 "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
4345 vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004346
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004347 /* res = 1 means ack at the end of thread
4348 * res = 0 means ack async/later.
4349 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004350 switch (iocb->u.isp24.status_subcode) {
4351 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004352
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004353 /* Mark all stale commands in qla_tgt_wq for deletion */
4354 abort_cmds_for_s_id(vha, &port_id);
4355
Quinn Tran75601512015-12-17 14:57:04 -05004356 if (wwn) {
4357 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004358 sess = qlt_find_sess_invalidate_other(tgt, wwn,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004359 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004360 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4361 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004362
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004363 if (IS_SW_RESV_ADDR(port_id) || (!sess && !conflict_sess)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004364 res = 1;
4365 break;
4366 }
4367
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004368 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4369 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004370 qlt_send_term_imm_notif(vha, iocb, 1);
4371
4372 res = 0;
4373 break;
4374 }
4375
4376 res = 0;
4377
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004378 if (conflict_sess)
4379 qlt_plogi_ack_link(vha, pla, conflict_sess,
4380 QLT_PLOGI_LINK_CONFLICT);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004381
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004382 if (!sess)
4383 break;
4384
4385 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004386 /*
4387 * Under normal circumstances we want to release nport handle
4388 * during LOGO process to avoid nport handle leaks inside FW.
4389 * The exception is when LOGO is done while another PLOGI with
4390 * the same nport handle is waiting as might be the case here.
4391 * Note: there is always a possibily of a race where session
4392 * deletion has already started for other reasons (e.g. ACL
4393 * removal) and now PLOGI arrives:
4394 * 1. if PLOGI arrived in FW after nport handle has been freed,
4395 * FW must have assigned this PLOGI a new/same handle and we
4396 * can proceed ACK'ing it as usual when session deletion
4397 * completes.
4398 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4399 * bit reached it, the handle has now been released. We'll
4400 * get an error when we ACK this PLOGI. Nothing will be sent
4401 * back to initiator. Initiator should eventually retry
4402 * PLOGI and situation will correct itself.
4403 */
4404 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4405 (sess->s_id.b24 == port_id.b24));
4406 qlt_schedule_sess_for_deletion(sess, true);
4407 break;
4408
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004409 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004410 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4411
Quinn Tran75601512015-12-17 14:57:04 -05004412 if (wwn) {
4413 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004414 sess = qlt_find_sess_invalidate_other(tgt, wwn, port_id,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004415 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004416 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4417 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004418
4419 if (conflict_sess) {
4420 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4421 "PRLI with conflicting sess %p port %8phC\n",
4422 conflict_sess, conflict_sess->port_name);
4423 qlt_send_term_imm_notif(vha, iocb, 1);
4424 res = 0;
4425 break;
4426 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004427
4428 if (sess != NULL) {
4429 if (sess->deleted) {
4430 /*
4431 * Impatient initiator sent PRLI before last
4432 * PLOGI could finish. Will force him to re-try,
4433 * while last one finishes.
4434 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004435 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004436 "sess %p PRLI received, before plogi ack.\n",
4437 sess);
4438 qlt_send_term_imm_notif(vha, iocb, 1);
4439 res = 0;
4440 break;
4441 }
4442
4443 /*
4444 * This shouldn't happen under normal circumstances,
4445 * since we have deleted the old session during PLOGI
4446 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004447 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004448 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4449 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4450
4451 sess->local = 0;
4452 sess->loop_id = loop_id;
4453 sess->s_id = port_id;
4454
4455 if (wd3_lo & BIT_7)
4456 sess->conf_compl_supported = 1;
4457
Alexei Potashnikdf673272015-07-14 16:00:46 -04004458 }
4459 res = 1; /* send notify ack */
4460
4461 /* Make session global (not used in fabric mode) */
4462 if (ha->current_topology != ISP_CFG_F) {
4463 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4464 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4465 qla2xxx_wake_dpc(vha);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004466 } else {
4467 /* todo: else - create sess here. */
4468 res = 1; /* send notify ack */
4469 }
4470
4471 break;
4472
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004473 case ELS_LOGO:
4474 case ELS_PRLO:
4475 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4476 break;
4477 case ELS_PDISC:
4478 case ELS_ADISC:
4479 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004480 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004481 if (tgt->link_reinit_iocb_pending) {
4482 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4483 0, 0, 0, 0, 0, 0);
4484 tgt->link_reinit_iocb_pending = 0;
4485 }
4486 res = 1; /* send notify ack */
4487 break;
4488 }
4489
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004490 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004491 default:
4492 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4493 "qla_target(%d): Unsupported ELS command %x "
4494 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4495 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4496 break;
4497 }
4498
4499 return res;
4500}
4501
4502static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
4503{
Bart Van Assche5897cb22015-06-04 15:57:20 -07004504#if 1
4505 /*
4506 * FIXME: Reject non zero SRR relative offset until we can test
4507 * this code properly.
4508 */
4509 pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
4510 return -1;
4511#else
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004512 struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
4513 size_t first_offset = 0, rem_offset = offset, tmp = 0;
4514 int i, sg_srr_cnt, bufflen = 0;
4515
4516 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
4517 "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
4518 "cmd->sg_cnt: %u, direction: %d\n",
4519 cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
4520
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004521 if (!cmd->sg || !cmd->sg_cnt) {
4522 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
4523 "Missing cmd->sg or zero cmd->sg_cnt in"
4524 " qla_tgt_set_data_offset\n");
4525 return -EINVAL;
4526 }
4527 /*
4528 * Walk the current cmd->sg list until we locate the new sg_srr_start
4529 */
4530 for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
4531 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
4532 "sg[%d]: %p page: %p, length: %d, offset: %d\n",
4533 i, sg, sg_page(sg), sg->length, sg->offset);
4534
4535 if ((sg->length + tmp) > offset) {
4536 first_offset = rem_offset;
4537 sg_srr_start = sg;
4538 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
4539 "Found matching sg[%d], using %p as sg_srr_start, "
4540 "and using first_offset: %zu\n", i, sg,
4541 first_offset);
4542 break;
4543 }
4544 tmp += sg->length;
4545 rem_offset -= sg->length;
4546 }
4547
4548 if (!sg_srr_start) {
4549 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
4550 "Unable to locate sg_srr_start for offset: %u\n", offset);
4551 return -EINVAL;
4552 }
4553 sg_srr_cnt = (cmd->sg_cnt - i);
4554
4555 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
4556 if (!sg_srr) {
4557 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
4558 "Unable to allocate sgp\n");
4559 return -ENOMEM;
4560 }
4561 sg_init_table(sg_srr, sg_srr_cnt);
4562 sgp = &sg_srr[0];
4563 /*
4564 * Walk the remaining list for sg_srr_start, mapping to the newly
4565 * allocated sg_srr taking first_offset into account.
4566 */
4567 for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
4568 if (first_offset) {
4569 sg_set_page(sgp, sg_page(sg),
4570 (sg->length - first_offset), first_offset);
4571 first_offset = 0;
4572 } else {
4573 sg_set_page(sgp, sg_page(sg), sg->length, 0);
4574 }
4575 bufflen += sgp->length;
4576
4577 sgp = sg_next(sgp);
4578 if (!sgp)
4579 break;
4580 }
4581
4582 cmd->sg = sg_srr;
4583 cmd->sg_cnt = sg_srr_cnt;
4584 cmd->bufflen = bufflen;
4585 cmd->offset += offset;
4586 cmd->free_sg = 1;
4587
4588 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
4589 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
4590 cmd->sg_cnt);
4591 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
4592 cmd->bufflen);
4593 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
4594 cmd->offset);
4595
4596 if (cmd->sg_cnt < 0)
4597 BUG();
4598
4599 if (cmd->bufflen < 0)
4600 BUG();
4601
4602 return 0;
Bart Van Assche5897cb22015-06-04 15:57:20 -07004603#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004604}
4605
4606static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
4607 uint32_t srr_rel_offs, int *xmit_type)
4608{
4609 int res = 0, rel_offs;
4610
4611 rel_offs = srr_rel_offs - cmd->offset;
4612 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
4613 srr_rel_offs, rel_offs);
4614
4615 *xmit_type = QLA_TGT_XMIT_ALL;
4616
4617 if (rel_offs < 0) {
4618 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
4619 "qla_target(%d): SRR rel_offs (%d) < 0",
4620 cmd->vha->vp_idx, rel_offs);
4621 res = -1;
4622 } else if (rel_offs == cmd->bufflen)
4623 *xmit_type = QLA_TGT_XMIT_STATUS;
4624 else if (rel_offs > 0)
4625 res = qlt_set_data_offset(cmd, rel_offs);
4626
4627 return res;
4628}
4629
4630/* No locks, thread context */
4631static void qlt_handle_srr(struct scsi_qla_host *vha,
4632 struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
4633{
4634 struct imm_ntfy_from_isp *ntfy =
4635 (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
4636 struct qla_hw_data *ha = vha->hw;
4637 struct qla_tgt_cmd *cmd = sctio->cmd;
4638 struct se_cmd *se_cmd = &cmd->se_cmd;
4639 unsigned long flags;
4640 int xmit_type = 0, resp = 0;
4641 uint32_t offset;
4642 uint16_t srr_ui;
4643
4644 offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
4645 srr_ui = ntfy->u.isp24.srr_ui;
4646
4647 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
4648 cmd, srr_ui);
4649
4650 switch (srr_ui) {
4651 case SRR_IU_STATUS:
4652 spin_lock_irqsave(&ha->hardware_lock, flags);
4653 qlt_send_notify_ack(vha, ntfy,
4654 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4655 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4656 xmit_type = QLA_TGT_XMIT_STATUS;
4657 resp = 1;
4658 break;
4659 case SRR_IU_DATA_IN:
4660 if (!cmd->sg || !cmd->sg_cnt) {
4661 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
4662 "Unable to process SRR_IU_DATA_IN due to"
4663 " missing cmd->sg, state: %d\n", cmd->state);
4664 dump_stack();
4665 goto out_reject;
4666 }
4667 if (se_cmd->scsi_status != 0) {
4668 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
4669 "Rejecting SRR_IU_DATA_IN with non GOOD "
4670 "scsi_status\n");
4671 goto out_reject;
4672 }
4673 cmd->bufflen = se_cmd->data_length;
4674
4675 if (qlt_has_data(cmd)) {
4676 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
4677 goto out_reject;
4678 spin_lock_irqsave(&ha->hardware_lock, flags);
4679 qlt_send_notify_ack(vha, ntfy,
4680 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4681 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4682 resp = 1;
4683 } else {
4684 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
Bart Van Assche649ee052015-04-14 13:26:44 +02004685 "qla_target(%d): SRR for in data for cmd without them (tag %lld, SCSI status %d), reject",
4686 vha->vp_idx, se_cmd->tag,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004687 cmd->se_cmd.scsi_status);
4688 goto out_reject;
4689 }
4690 break;
4691 case SRR_IU_DATA_OUT:
4692 if (!cmd->sg || !cmd->sg_cnt) {
4693 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
4694 "Unable to process SRR_IU_DATA_OUT due to"
4695 " missing cmd->sg\n");
4696 dump_stack();
4697 goto out_reject;
4698 }
4699 if (se_cmd->scsi_status != 0) {
4700 ql_dbg(ql_dbg_tgt, vha, 0xe02b,
4701 "Rejecting SRR_IU_DATA_OUT"
4702 " with non GOOD scsi_status\n");
4703 goto out_reject;
4704 }
4705 cmd->bufflen = se_cmd->data_length;
4706
4707 if (qlt_has_data(cmd)) {
4708 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
4709 goto out_reject;
4710 spin_lock_irqsave(&ha->hardware_lock, flags);
4711 qlt_send_notify_ack(vha, ntfy,
4712 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4713 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004714 if (xmit_type & QLA_TGT_XMIT_DATA) {
4715 cmd->cmd_flags |= BIT_8;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004716 qlt_rdy_to_xfer(cmd);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004717 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004718 } else {
4719 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
Bart Van Assche649ee052015-04-14 13:26:44 +02004720 "qla_target(%d): SRR for out data for cmd without them (tag %lld, SCSI status %d), reject",
4721 vha->vp_idx, se_cmd->tag, cmd->se_cmd.scsi_status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004722 goto out_reject;
4723 }
4724 break;
4725 default:
4726 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
4727 "qla_target(%d): Unknown srr_ui value %x",
4728 vha->vp_idx, srr_ui);
4729 goto out_reject;
4730 }
4731
4732 /* Transmit response in case of status and data-in cases */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004733 if (resp) {
4734 cmd->cmd_flags |= BIT_7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004735 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004736 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004737
4738 return;
4739
4740out_reject:
4741 spin_lock_irqsave(&ha->hardware_lock, flags);
4742 qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
4743 NOTIFY_ACK_SRR_FLAGS_REJECT,
4744 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4745 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4746 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4747 cmd->state = QLA_TGT_STATE_DATA_IN;
4748 dump_stack();
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004749 } else {
4750 cmd->cmd_flags |= BIT_9;
Quinn Trana07100e2015-12-07 19:48:57 -05004751 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004752 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004753 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4754}
4755
4756static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
4757 struct qla_tgt_srr_imm *imm, int ha_locked)
4758{
4759 struct qla_hw_data *ha = vha->hw;
4760 unsigned long flags = 0;
4761
Bart Van Assche8d163662015-07-09 07:25:46 -07004762#ifndef __CHECKER__
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004763 if (!ha_locked)
4764 spin_lock_irqsave(&ha->hardware_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -07004765#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004766
4767 qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
4768 NOTIFY_ACK_SRR_FLAGS_REJECT,
4769 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4770 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4771
Bart Van Assche8d163662015-07-09 07:25:46 -07004772#ifndef __CHECKER__
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004773 if (!ha_locked)
4774 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -07004775#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004776
4777 kfree(imm);
4778}
4779
4780static void qlt_handle_srr_work(struct work_struct *work)
4781{
4782 struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
4783 struct scsi_qla_host *vha = tgt->vha;
4784 struct qla_tgt_srr_ctio *sctio;
4785 unsigned long flags;
4786
4787 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
4788 tgt);
4789
4790restart:
4791 spin_lock_irqsave(&tgt->srr_lock, flags);
4792 list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
4793 struct qla_tgt_srr_imm *imm, *i, *ti;
4794 struct qla_tgt_cmd *cmd;
4795 struct se_cmd *se_cmd;
4796
4797 imm = NULL;
4798 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
4799 srr_list_entry) {
4800 if (i->srr_id == sctio->srr_id) {
4801 list_del(&i->srr_list_entry);
4802 if (imm) {
4803 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
4804 "qla_target(%d): There must be "
4805 "only one IMM SRR per CTIO SRR "
4806 "(IMM SRR %p, id %d, CTIO %p\n",
4807 vha->vp_idx, i, i->srr_id, sctio);
4808 qlt_reject_free_srr_imm(tgt->vha, i, 0);
4809 } else
4810 imm = i;
4811 }
4812 }
4813
4814 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
4815 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
4816 sctio->srr_id);
4817
4818 if (imm == NULL) {
4819 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
4820 "Not found matching IMM for SRR CTIO (id %d)\n",
4821 sctio->srr_id);
4822 continue;
4823 } else
4824 list_del(&sctio->srr_list_entry);
4825
4826 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4827
4828 cmd = sctio->cmd;
4829 /*
4830 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
4831 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
4832 * logic..
4833 */
4834 cmd->offset = 0;
4835 if (cmd->free_sg) {
4836 kfree(cmd->sg);
4837 cmd->sg = NULL;
4838 cmd->free_sg = 0;
4839 }
4840 se_cmd = &cmd->se_cmd;
4841
4842 cmd->sg_cnt = se_cmd->t_data_nents;
4843 cmd->sg = se_cmd->t_data_sg;
4844
4845 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
Bart Van Assche649ee052015-04-14 13:26:44 +02004846 "SRR cmd %p (se_cmd %p, tag %lld, op %x), sg_cnt=%d, offset=%d",
4847 cmd, &cmd->se_cmd, se_cmd->tag, se_cmd->t_task_cdb ?
4848 se_cmd->t_task_cdb[0] : 0, cmd->sg_cnt, cmd->offset);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004849
4850 qlt_handle_srr(vha, sctio, imm);
4851
4852 kfree(imm);
4853 kfree(sctio);
4854 goto restart;
4855 }
4856 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4857}
4858
4859/* ha->hardware_lock supposed to be held on entry */
4860static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
4861 struct imm_ntfy_from_isp *iocb)
4862{
4863 struct qla_tgt_srr_imm *imm;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004864 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004865 struct qla_tgt_srr_ctio *sctio;
4866
4867 tgt->imm_srr_id++;
4868
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004869 ql_log(ql_log_warn, vha, 0xf02d, "qla_target(%d): SRR received\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004870 vha->vp_idx);
4871
4872 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
4873 if (imm != NULL) {
4874 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
4875
4876 /* IRQ is already OFF */
4877 spin_lock(&tgt->srr_lock);
4878 imm->srr_id = tgt->imm_srr_id;
4879 list_add_tail(&imm->srr_list_entry,
4880 &tgt->srr_imm_list);
4881 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
4882 "IMM NTFY SRR %p added (id %d, ui %x)\n",
4883 imm, imm->srr_id, iocb->u.isp24.srr_ui);
4884 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
4885 int found = 0;
4886 list_for_each_entry(sctio, &tgt->srr_ctio_list,
4887 srr_list_entry) {
4888 if (sctio->srr_id == imm->srr_id) {
4889 found = 1;
4890 break;
4891 }
4892 }
4893 if (found) {
4894 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
4895 "Scheduling srr work\n");
4896 schedule_work(&tgt->srr_work);
4897 } else {
4898 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
4899 "qla_target(%d): imm_srr_id "
4900 "== ctio_srr_id (%d), but there is no "
4901 "corresponding SRR CTIO, deleting IMM "
4902 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
4903 imm);
4904 list_del(&imm->srr_list_entry);
4905
4906 kfree(imm);
4907
4908 spin_unlock(&tgt->srr_lock);
4909 goto out_reject;
4910 }
4911 }
4912 spin_unlock(&tgt->srr_lock);
4913 } else {
4914 struct qla_tgt_srr_ctio *ts;
4915
4916 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
4917 "qla_target(%d): Unable to allocate SRR IMM "
4918 "entry, SRR request will be rejected\n", vha->vp_idx);
4919
4920 /* IRQ is already OFF */
4921 spin_lock(&tgt->srr_lock);
4922 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
4923 srr_list_entry) {
4924 if (sctio->srr_id == tgt->imm_srr_id) {
4925 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
4926 "CTIO SRR %p deleted (id %d)\n",
4927 sctio, sctio->srr_id);
4928 list_del(&sctio->srr_list_entry);
4929 qlt_send_term_exchange(vha, sctio->cmd,
Quinn Trana07100e2015-12-07 19:48:57 -05004930 &sctio->cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004931 kfree(sctio);
4932 }
4933 }
4934 spin_unlock(&tgt->srr_lock);
4935 goto out_reject;
4936 }
4937
4938 return;
4939
4940out_reject:
4941 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
4942 NOTIFY_ACK_SRR_FLAGS_REJECT,
4943 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4944 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4945}
4946
4947/*
4948 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4949 */
4950static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4951 struct imm_ntfy_from_isp *iocb)
4952{
4953 struct qla_hw_data *ha = vha->hw;
4954 uint32_t add_flags = 0;
4955 int send_notify_ack = 1;
4956 uint16_t status;
4957
4958 status = le16_to_cpu(iocb->u.isp2x.status);
4959 switch (status) {
4960 case IMM_NTFY_LIP_RESET:
4961 {
4962 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4963 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4964 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4965 iocb->u.isp24.status_subcode);
4966
4967 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4968 send_notify_ack = 0;
4969 break;
4970 }
4971
4972 case IMM_NTFY_LIP_LINK_REINIT:
4973 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004974 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004975 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4976 "qla_target(%d): LINK REINIT (loop %#x, "
4977 "subcode %x)\n", vha->vp_idx,
4978 le16_to_cpu(iocb->u.isp24.nport_handle),
4979 iocb->u.isp24.status_subcode);
4980 if (tgt->link_reinit_iocb_pending) {
4981 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4982 0, 0, 0, 0, 0, 0);
4983 }
4984 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4985 tgt->link_reinit_iocb_pending = 1;
4986 /*
4987 * QLogic requires to wait after LINK REINIT for possible
4988 * PDISC or ADISC ELS commands
4989 */
4990 send_notify_ack = 0;
4991 break;
4992 }
4993
4994 case IMM_NTFY_PORT_LOGOUT:
4995 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4996 "qla_target(%d): Port logout (loop "
4997 "%#x, subcode %x)\n", vha->vp_idx,
4998 le16_to_cpu(iocb->u.isp24.nport_handle),
4999 iocb->u.isp24.status_subcode);
5000
5001 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5002 send_notify_ack = 0;
5003 /* The sessions will be cleared in the callback, if needed */
5004 break;
5005
5006 case IMM_NTFY_GLBL_TPRLO:
5007 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5008 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5009 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5010 send_notify_ack = 0;
5011 /* The sessions will be cleared in the callback, if needed */
5012 break;
5013
5014 case IMM_NTFY_PORT_CONFIG:
5015 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5016 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5017 status);
5018 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5019 send_notify_ack = 0;
5020 /* The sessions will be cleared in the callback, if needed */
5021 break;
5022
5023 case IMM_NTFY_GLBL_LOGO:
5024 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5025 "qla_target(%d): Link failure detected\n",
5026 vha->vp_idx);
5027 /* I_T nexus loss */
5028 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5029 send_notify_ack = 0;
5030 break;
5031
5032 case IMM_NTFY_IOCB_OVERFLOW:
5033 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5034 "qla_target(%d): Cannot provide requested "
5035 "capability (IOCB overflowed the immediate notify "
5036 "resource count)\n", vha->vp_idx);
5037 break;
5038
5039 case IMM_NTFY_ABORT_TASK:
5040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5041 "qla_target(%d): Abort Task (S %08x I %#x -> "
5042 "L %#x)\n", vha->vp_idx,
5043 le16_to_cpu(iocb->u.isp2x.seq_id),
5044 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5045 le16_to_cpu(iocb->u.isp2x.lun));
5046 if (qlt_abort_task(vha, iocb) == 0)
5047 send_notify_ack = 0;
5048 break;
5049
5050 case IMM_NTFY_RESOURCE:
5051 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5052 "qla_target(%d): Out of resources, host %ld\n",
5053 vha->vp_idx, vha->host_no);
5054 break;
5055
5056 case IMM_NTFY_MSG_RX:
5057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5058 "qla_target(%d): Immediate notify task %x\n",
5059 vha->vp_idx, iocb->u.isp2x.task_flags);
5060 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5061 send_notify_ack = 0;
5062 break;
5063
5064 case IMM_NTFY_ELS:
5065 if (qlt_24xx_handle_els(vha, iocb) == 0)
5066 send_notify_ack = 0;
5067 break;
5068
5069 case IMM_NTFY_SRR:
5070 qlt_prepare_srr_imm(vha, iocb);
5071 send_notify_ack = 0;
5072 break;
5073
5074 default:
5075 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5076 "qla_target(%d): Received unknown immediate "
5077 "notify status %x\n", vha->vp_idx, status);
5078 break;
5079 }
5080
5081 if (send_notify_ack)
5082 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
5083}
5084
5085/*
5086 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5087 * This function sends busy to ISP 2xxx or 24xx.
5088 */
Quinn Tran33e79972014-09-25 06:14:55 -04005089static int __qlt_send_busy(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005090 struct atio_from_isp *atio, uint16_t status)
5091{
5092 struct ctio7_to_24xx *ctio24;
5093 struct qla_hw_data *ha = vha->hw;
5094 request_t *pkt;
5095 struct qla_tgt_sess *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005096 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005097
Quinn Tran75601512015-12-17 14:57:04 -05005098 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005099 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5100 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05005101 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005102 if (!sess) {
Quinn Trana07100e2015-12-07 19:48:57 -05005103 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005104 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005105 }
5106 /* Sending marker isn't necessary, since we called from ISR */
5107
5108 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
5109 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005110 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005111 "qla_target(%d): %s failed: unable to allocate "
5112 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005113 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005114 }
5115
Himanshu Madhanice1025c2015-12-17 14:56:58 -05005116 vha->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005117 pkt->entry_count = 1;
5118 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5119
5120 ctio24 = (struct ctio7_to_24xx *)pkt;
5121 ctio24->entry_type = CTIO_TYPE7;
5122 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005123 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005124 ctio24->vp_index = vha->vp_idx;
5125 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5126 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5127 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5128 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5129 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07005130 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005131 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5132 CTIO7_FLAGS_DONT_RET_CTIO);
5133 /*
5134 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5135 * if the explicit conformation is used.
5136 */
5137 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5138 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005139 /* Memory Barrier */
5140 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005141 qla2x00_start_iocbs(vha, vha->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005142 return 0;
5143}
5144
5145/*
5146 * This routine is used to allocate a command for either a QFull condition
5147 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5148 * out previously.
5149 */
5150static void
5151qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5152 struct atio_from_isp *atio, uint16_t status, int qfull)
5153{
5154 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5155 struct qla_hw_data *ha = vha->hw;
5156 struct qla_tgt_sess *sess;
5157 struct se_session *se_sess;
5158 struct qla_tgt_cmd *cmd;
5159 int tag;
5160
5161 if (unlikely(tgt->tgt_stop)) {
5162 ql_dbg(ql_dbg_io, vha, 0x300a,
5163 "New command while device %p is shutting down\n", tgt);
5164 return;
5165 }
5166
5167 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5168 vha->hw->tgt.num_qfull_cmds_dropped++;
5169 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005170 vha->qla_stats.stat_max_qfull_cmds_dropped)
5171 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005172 vha->hw->tgt.num_qfull_cmds_dropped;
5173
5174 ql_dbg(ql_dbg_io, vha, 0x3068,
5175 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5176 vha->vp_idx, __func__,
5177 vha->hw->tgt.num_qfull_cmds_dropped);
5178
5179 qlt_chk_exch_leak_thresh_hold(vha);
5180 return;
5181 }
5182
5183 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5184 (vha, atio->u.isp24.fcp_hdr.s_id);
5185 if (!sess)
5186 return;
5187
5188 se_sess = sess->se_sess;
5189
5190 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5191 if (tag < 0)
5192 return;
5193
5194 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5195 if (!cmd) {
5196 ql_dbg(ql_dbg_io, vha, 0x3009,
5197 "qla_target(%d): %s: Allocation of cmd failed\n",
5198 vha->vp_idx, __func__);
5199
5200 vha->hw->tgt.num_qfull_cmds_dropped++;
5201 if (vha->hw->tgt.num_qfull_cmds_dropped >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005202 vha->qla_stats.stat_max_qfull_cmds_dropped)
5203 vha->qla_stats.stat_max_qfull_cmds_dropped =
Quinn Tran33e79972014-09-25 06:14:55 -04005204 vha->hw->tgt.num_qfull_cmds_dropped;
5205
5206 qlt_chk_exch_leak_thresh_hold(vha);
5207 return;
5208 }
5209
5210 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5211
5212 qlt_incr_num_pend_cmds(vha);
5213 INIT_LIST_HEAD(&cmd->cmd_list);
5214 memcpy(&cmd->atio, atio, sizeof(*atio));
5215
5216 cmd->tgt = vha->vha_tgt.qla_tgt;
5217 cmd->vha = vha;
5218 cmd->reset_count = vha->hw->chip_reset;
5219 cmd->q_full = 1;
5220
5221 if (qfull) {
5222 cmd->q_full = 1;
5223 /* NOTE: borrowing the state field to carry the status */
5224 cmd->state = status;
5225 } else
5226 cmd->term_exchg = 1;
5227
5228 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5229
5230 vha->hw->tgt.num_qfull_cmds_alloc++;
5231 if (vha->hw->tgt.num_qfull_cmds_alloc >
Joe Carnucciofc90ada2016-07-06 11:14:23 -04005232 vha->qla_stats.stat_max_qfull_cmds_alloc)
5233 vha->qla_stats.stat_max_qfull_cmds_alloc =
Quinn Tran33e79972014-09-25 06:14:55 -04005234 vha->hw->tgt.num_qfull_cmds_alloc;
5235}
5236
5237int
5238qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5239{
5240 struct qla_hw_data *ha = vha->hw;
5241 unsigned long flags;
5242 struct qla_tgt_cmd *cmd, *tcmd;
5243 struct list_head free_list;
5244 int rc = 0;
5245
5246 if (list_empty(&ha->tgt.q_full_list))
5247 return 0;
5248
5249 INIT_LIST_HEAD(&free_list);
5250
5251 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5252
5253 if (list_empty(&ha->tgt.q_full_list)) {
5254 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5255 return 0;
5256 }
5257
5258 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5259 if (cmd->q_full)
5260 /* cmd->state is a borrowed field to hold status */
5261 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5262 else if (cmd->term_exchg)
5263 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5264
5265 if (rc == -ENOMEM)
5266 break;
5267
5268 if (cmd->q_full)
5269 ql_dbg(ql_dbg_io, vha, 0x3006,
5270 "%s: busy sent for ox_id[%04x]\n", __func__,
5271 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5272 else if (cmd->term_exchg)
5273 ql_dbg(ql_dbg_io, vha, 0x3007,
5274 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5275 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5276 else
5277 ql_dbg(ql_dbg_io, vha, 0x3008,
5278 "%s: Unexpected cmd in QFull list %p\n", __func__,
5279 cmd);
5280
5281 list_del(&cmd->cmd_list);
5282 list_add_tail(&cmd->cmd_list, &free_list);
5283
5284 /* piggy back on hardware_lock for protection */
5285 vha->hw->tgt.num_qfull_cmds_alloc--;
5286 }
5287 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5288
5289 cmd = NULL;
5290
5291 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5292 list_del(&cmd->cmd_list);
5293 /* This cmd was never sent to TCM. There is no need
5294 * to schedule free or call free_cmd
5295 */
5296 qlt_free_cmd(cmd);
5297 }
5298 return rc;
5299}
5300
5301static void
5302qlt_send_busy(struct scsi_qla_host *vha,
5303 struct atio_from_isp *atio, uint16_t status)
5304{
5305 int rc = 0;
5306
5307 rc = __qlt_send_busy(vha, atio, status);
5308 if (rc == -ENOMEM)
5309 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5310}
5311
5312static int
5313qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
5314 struct atio_from_isp *atio)
5315{
5316 struct qla_hw_data *ha = vha->hw;
5317 uint16_t status;
5318
5319 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5320 return 0;
5321
5322 status = temp_sam_status;
5323 qlt_send_busy(vha, atio, status);
5324 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005325}
5326
5327/* ha->hardware_lock supposed to be held on entry */
5328/* called via callback from qla2xxx */
5329static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005330 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005331{
5332 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005333 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005334 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005335 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005336
5337 if (unlikely(tgt == NULL)) {
Arun Easi667024a2014-09-25 06:14:47 -04005338 ql_dbg(ql_dbg_io, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005339 "ATIO pkt, but no tgt (ha %p)", ha);
5340 return;
5341 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005342 /*
5343 * In tgt_stop mode we also should allow all requests to pass.
5344 * Otherwise, some commands can stuck.
5345 */
5346
Quinn Tran2f424b92015-12-17 14:57:07 -05005347 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005348
5349 switch (atio->u.raw.entry_type) {
5350 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005351 if (unlikely(atio->u.isp24.exchange_addr ==
5352 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005353 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005354 "qla_target(%d): ATIO_TYPE7 "
5355 "received with UNKNOWN exchange address, "
5356 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005357 if (!ha_locked)
5358 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005359 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005360 if (!ha_locked)
5361 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005362 break;
5363 }
Quinn Tran33e79972014-09-25 06:14:55 -04005364
5365
5366
5367 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5368 rc = qlt_chk_qfull_thresh_hold(vha, atio);
5369 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005370 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005371 return;
5372 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005373 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005374 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005375 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005376 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005377 if (unlikely(rc != 0)) {
5378 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005379 if (!ha_locked)
5380 spin_lock_irqsave
5381 (&ha->hardware_lock, flags);
5382
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005383#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5384 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5385#else
Quinn Trana07100e2015-12-07 19:48:57 -05005386 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005387#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005388
5389 if (!ha_locked)
5390 spin_unlock_irqrestore
5391 (&ha->hardware_lock, flags);
5392
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005393 } else {
5394 if (tgt->tgt_stop) {
5395 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5396 "qla_target: Unable to send "
5397 "command to target for req, "
5398 "ignoring.\n");
5399 } else {
5400 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5401 "qla_target(%d): Unable to send "
5402 "command to target, sending BUSY "
5403 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005404 if (!ha_locked)
5405 spin_lock_irqsave(
5406 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005407 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005408 if (!ha_locked)
5409 spin_unlock_irqrestore(
5410 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005411 }
5412 }
5413 }
5414 break;
5415
5416 case IMMED_NOTIFY_TYPE:
5417 {
5418 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5419 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5420 "qla_target(%d): Received ATIO packet %x "
5421 "with error status %x\n", vha->vp_idx,
5422 atio->u.raw.entry_type,
5423 atio->u.isp2x.entry_status);
5424 break;
5425 }
5426 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005427
5428 if (!ha_locked)
5429 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005430 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005431 if (!ha_locked)
5432 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005433 break;
5434 }
5435
5436 default:
5437 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5438 "qla_target(%d): Received unknown ATIO atio "
5439 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5440 break;
5441 }
5442
Quinn Tran2f424b92015-12-17 14:57:07 -05005443 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005444}
5445
5446/* ha->hardware_lock supposed to be held on entry */
5447/* called via callback from qla2xxx */
5448static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5449{
5450 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005451 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005452
5453 if (unlikely(tgt == NULL)) {
5454 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5455 "qla_target(%d): Response pkt %x received, but no "
5456 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5457 return;
5458 }
5459
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005460 /*
5461 * In tgt_stop mode we also should allow all requests to pass.
5462 * Otherwise, some commands can stuck.
5463 */
5464
5465 tgt->irq_cmd_count++;
5466
5467 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005468 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005469 case CTIO_TYPE7:
5470 {
5471 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005472 qlt_do_ctio_completion(vha, entry->handle,
5473 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5474 entry);
5475 break;
5476 }
5477
5478 case ACCEPT_TGT_IO_TYPE:
5479 {
5480 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5481 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005482 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005483 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005484 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5485 "qla_target(%d): ATIO with error "
5486 "status %x received\n", vha->vp_idx,
5487 le16_to_cpu(atio->u.isp2x.status));
5488 break;
5489 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005490
Quinn Tran33e79972014-09-25 06:14:55 -04005491 rc = qlt_chk_qfull_thresh_hold(vha, atio);
5492 if (rc != 0) {
5493 tgt->irq_cmd_count--;
5494 return;
5495 }
5496
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005497 rc = qlt_handle_cmd_for_atio(vha, atio);
5498 if (unlikely(rc != 0)) {
5499 if (rc == -ESRCH) {
5500#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5501 qlt_send_busy(vha, atio, 0);
5502#else
Quinn Trana07100e2015-12-07 19:48:57 -05005503 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005504#endif
5505 } else {
5506 if (tgt->tgt_stop) {
5507 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5508 "qla_target: Unable to send "
5509 "command to target, sending TERM "
5510 "EXCHANGE for rsp\n");
5511 qlt_send_term_exchange(vha, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005512 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005513 } else {
5514 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5515 "qla_target(%d): Unable to send "
5516 "command to target, sending BUSY "
5517 "status\n", vha->vp_idx);
5518 qlt_send_busy(vha, atio, 0);
5519 }
5520 }
5521 }
5522 }
5523 break;
5524
5525 case CONTINUE_TGT_IO_TYPE:
5526 {
5527 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005528 qlt_do_ctio_completion(vha, entry->handle,
5529 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5530 entry);
5531 break;
5532 }
5533
5534 case CTIO_A64_TYPE:
5535 {
5536 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005537 qlt_do_ctio_completion(vha, entry->handle,
5538 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5539 entry);
5540 break;
5541 }
5542
5543 case IMMED_NOTIFY_TYPE:
5544 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5545 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5546 break;
5547
5548 case NOTIFY_ACK_TYPE:
5549 if (tgt->notify_ack_expected > 0) {
5550 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5551 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5552 "NOTIFY_ACK seq %08x status %x\n",
5553 le16_to_cpu(entry->u.isp2x.seq_id),
5554 le16_to_cpu(entry->u.isp2x.status));
5555 tgt->notify_ack_expected--;
5556 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005557 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005558 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5559 "qla_target(%d): NOTIFY_ACK "
5560 "failed %x\n", vha->vp_idx,
5561 le16_to_cpu(entry->u.isp2x.status));
5562 }
5563 } else {
5564 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5565 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5566 vha->vp_idx);
5567 }
5568 break;
5569
5570 case ABTS_RECV_24XX:
5571 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5572 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5573 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5574 break;
5575
5576 case ABTS_RESP_24XX:
5577 if (tgt->abts_resp_expected > 0) {
5578 struct abts_resp_from_24xx_fw *entry =
5579 (struct abts_resp_from_24xx_fw *)pkt;
5580 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5581 "ABTS_RESP_24XX: compl_status %x\n",
5582 entry->compl_status);
5583 tgt->abts_resp_expected--;
5584 if (le16_to_cpu(entry->compl_status) !=
5585 ABTS_RESP_COMPL_SUCCESS) {
5586 if ((entry->error_subcode1 == 0x1E) &&
5587 (entry->error_subcode2 == 0)) {
5588 /*
5589 * We've got a race here: aborted
5590 * exchange not terminated, i.e.
5591 * response for the aborted command was
5592 * sent between the abort request was
5593 * received and processed.
5594 * Unfortunately, the firmware has a
5595 * silly requirement that all aborted
5596 * exchanges must be explicitely
5597 * terminated, otherwise it refuses to
5598 * send responses for the abort
5599 * requests. So, we have to
5600 * (re)terminate the exchange and retry
5601 * the abort response.
5602 */
5603 qlt_24xx_retry_term_exchange(vha,
5604 entry);
5605 } else
5606 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5607 "qla_target(%d): ABTS_RESP_24XX "
5608 "failed %x (subcode %x:%x)",
5609 vha->vp_idx, entry->compl_status,
5610 entry->error_subcode1,
5611 entry->error_subcode2);
5612 }
5613 } else {
5614 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5615 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5616 "received\n", vha->vp_idx);
5617 }
5618 break;
5619
5620 default:
5621 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5622 "qla_target(%d): Received unknown response pkt "
5623 "type %x\n", vha->vp_idx, pkt->entry_type);
5624 break;
5625 }
5626
5627 tgt->irq_cmd_count--;
5628}
5629
5630/*
5631 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5632 */
5633void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5634 uint16_t *mailbox)
5635{
5636 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005637 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005638 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005639
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005640 if (!ha->tgt.tgt_ops)
5641 return;
5642
5643 if (unlikely(tgt == NULL)) {
5644 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5645 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5646 return;
5647 }
5648
5649 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5650 IS_QLA2100(ha))
5651 return;
5652 /*
5653 * In tgt_stop mode we also should allow all requests to pass.
5654 * Otherwise, some commands can stuck.
5655 */
5656
5657 tgt->irq_cmd_count++;
5658
5659 switch (code) {
5660 case MBA_RESET: /* Reset */
5661 case MBA_SYSTEM_ERR: /* System Error */
5662 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5663 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5664 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5665 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005666 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005667 break;
5668 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5669 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5670 break;
5671
5672 case MBA_LOOP_UP:
5673 {
5674 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005675 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005676 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5677 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5678 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005679 if (tgt->link_reinit_iocb_pending) {
5680 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5681 0, 0, 0, 0, 0, 0);
5682 tgt->link_reinit_iocb_pending = 0;
5683 }
5684 break;
5685 }
5686
5687 case MBA_LIP_OCCURRED:
5688 case MBA_LOOP_DOWN:
5689 case MBA_LIP_RESET:
5690 case MBA_RSCN_UPDATE:
5691 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005692 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005693 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5694 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5695 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005696 break;
5697
5698 case MBA_PORT_UPDATE:
5699 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5700 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005701 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005702 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5703 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5704 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5705
5706 login_code = le16_to_cpu(mailbox[2]);
5707 if (login_code == 0x4)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005708 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5709 "Async MB 2: Got PLOGI Complete\n");
Alan Cox4f1d0f12012-07-04 16:35:35 +01005710 else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005711 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5712 "Async MB 2: Port Logged Out\n");
5713 break;
5714
5715 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005716 break;
5717 }
5718
5719 tgt->irq_cmd_count--;
5720}
5721
5722static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5723 uint16_t loop_id)
5724{
5725 fc_port_t *fcport;
5726 int rc;
5727
5728 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5729 if (!fcport) {
5730 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5731 "qla_target(%d): Allocation of tmp FC port failed",
5732 vha->vp_idx);
5733 return NULL;
5734 }
5735
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005736 fcport->loop_id = loop_id;
5737
5738 rc = qla2x00_get_port_database(vha, fcport, 0);
5739 if (rc != QLA_SUCCESS) {
5740 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5741 "qla_target(%d): Failed to retrieve fcport "
5742 "information -- get_port_database() returned %x "
5743 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5744 kfree(fcport);
5745 return NULL;
5746 }
5747
5748 return fcport;
5749}
5750
5751/* Must be called under tgt_mutex */
5752static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
5753 uint8_t *s_id)
5754{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005755 struct qla_tgt_sess *sess = NULL;
5756 fc_port_t *fcport = NULL;
5757 int rc, global_resets;
5758 uint16_t loop_id = 0;
5759
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005760 mutex_lock(&vha->vha_tgt.tgt_mutex);
5761
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005762retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005763 global_resets =
5764 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005765
5766 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5767 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005768 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5769
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005770 if ((s_id[0] == 0xFF) &&
5771 (s_id[1] == 0xFC)) {
5772 /*
5773 * This is Domain Controller, so it should be
5774 * OK to drop SCSI commands from it.
5775 */
5776 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5777 "Unable to find initiator with S_ID %x:%x:%x",
5778 s_id[0], s_id[1], s_id[2]);
5779 } else
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005780 ql_log(ql_log_info, vha, 0xf071,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005781 "qla_target(%d): Unable to find "
5782 "initiator with S_ID %x:%x:%x",
5783 vha->vp_idx, s_id[0], s_id[1],
5784 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005785
5786 if (rc == -ENOENT) {
5787 qlt_port_logo_t logo;
5788 sid_to_portid(s_id, &logo.id);
5789 logo.cmd_count = 1;
5790 qlt_send_first_logo(vha, &logo);
5791 }
5792
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005793 return NULL;
5794 }
5795
5796 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005797 if (!fcport) {
5798 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005799 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005800 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005801
5802 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005803 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005804 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5805 "qla_target(%d): global reset during session discovery "
5806 "(counter was %d, new %d), retrying", vha->vp_idx,
5807 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005808 atomic_read(&vha->vha_tgt.
5809 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005810 goto retry;
5811 }
5812
5813 sess = qlt_create_sess(vha, fcport, true);
5814
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005815 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5816
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005817 kfree(fcport);
5818 return sess;
5819}
5820
5821static void qlt_abort_work(struct qla_tgt *tgt,
5822 struct qla_tgt_sess_work_param *prm)
5823{
5824 struct scsi_qla_host *vha = tgt->vha;
5825 struct qla_hw_data *ha = vha->hw;
5826 struct qla_tgt_sess *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005827 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005828 uint32_t be_s_id;
5829 uint8_t s_id[3];
5830 int rc;
5831
Quinn Tran75601512015-12-17 14:57:04 -05005832 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005833
5834 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005835 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005836
5837 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5838 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5839 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5840
5841 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5842 (unsigned char *)&be_s_id);
5843 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005844 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005845
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005846 sess = qlt_make_local_sess(vha, s_id);
5847 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005848
Quinn Tran75601512015-12-17 14:57:04 -05005849 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005850 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005851 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005852 } else {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005853 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
5854 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005855 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005856 }
5857
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02005858 kref_get(&sess->sess_kref);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005859 }
5860
Quinn Tran75601512015-12-17 14:57:04 -05005861 spin_lock_irqsave(&ha->hardware_lock, flags);
5862
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005863 if (tgt->tgt_stop)
5864 goto out_term;
5865
5866 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5867 if (rc != 0)
5868 goto out_term;
Quinn Tran75601512015-12-17 14:57:04 -05005869 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005870
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02005871 qlt_put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005872 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005873 return;
5874
Quinn Tran75601512015-12-17 14:57:04 -05005875out_term2:
5876 spin_lock_irqsave(&ha->hardware_lock, flags);
5877
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005878out_term:
5879 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
Quinn Tran75601512015-12-17 14:57:04 -05005880 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5881
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02005882 qlt_put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005883 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005884}
5885
5886static void qlt_tmr_work(struct qla_tgt *tgt,
5887 struct qla_tgt_sess_work_param *prm)
5888{
5889 struct atio_from_isp *a = &prm->tm_iocb2;
5890 struct scsi_qla_host *vha = tgt->vha;
5891 struct qla_hw_data *ha = vha->hw;
5892 struct qla_tgt_sess *sess = NULL;
5893 unsigned long flags;
5894 uint8_t *s_id = NULL; /* to hide compiler warnings */
5895 int rc;
5896 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005897 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005898 void *iocb;
5899
Quinn Tran75601512015-12-17 14:57:04 -05005900 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005901
5902 if (tgt->tgt_stop)
5903 goto out_term;
5904
5905 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5906 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5907 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005908 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005909
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005910 sess = qlt_make_local_sess(vha, s_id);
5911 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005912
Quinn Tran75601512015-12-17 14:57:04 -05005913 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005914 if (!sess)
5915 goto out_term;
5916 } else {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005917 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
5918 sess = NULL;
5919 goto out_term;
5920 }
5921
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02005922 kref_get(&sess->sess_kref);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005923 }
5924
5925 iocb = a;
5926 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005927 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5928 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5929
5930 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5931 if (rc != 0)
5932 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005933
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02005934 qlt_put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005935 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005936 return;
5937
5938out_term:
Quinn Trana07100e2015-12-07 19:48:57 -05005939 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
Christoph Hellwige3dc0e32016-05-02 15:45:23 +02005940 qlt_put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005941 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005942}
5943
5944static void qlt_sess_work_fn(struct work_struct *work)
5945{
5946 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5947 struct scsi_qla_host *vha = tgt->vha;
5948 unsigned long flags;
5949
5950 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5951
5952 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5953 while (!list_empty(&tgt->sess_works_list)) {
5954 struct qla_tgt_sess_work_param *prm = list_entry(
5955 tgt->sess_works_list.next, typeof(*prm),
5956 sess_works_list_entry);
5957
5958 /*
5959 * This work can be scheduled on several CPUs at time, so we
5960 * must delete the entry to eliminate double processing
5961 */
5962 list_del(&prm->sess_works_list_entry);
5963
5964 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5965
5966 switch (prm->type) {
5967 case QLA_TGT_SESS_WORK_ABORT:
5968 qlt_abort_work(tgt, prm);
5969 break;
5970 case QLA_TGT_SESS_WORK_TM:
5971 qlt_tmr_work(tgt, prm);
5972 break;
5973 default:
5974 BUG_ON(1);
5975 break;
5976 }
5977
5978 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5979
5980 kfree(prm);
5981 }
5982 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5983}
5984
5985/* Must be called under tgt_host_action_mutex */
5986int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5987{
5988 struct qla_tgt *tgt;
5989
5990 if (!QLA_TGT_MODE_ENABLED())
5991 return 0;
5992
Arun Easi33c36c02013-01-30 03:34:41 -05005993 if (!IS_TGT_MODE_CAPABLE(ha)) {
5994 ql_log(ql_log_warn, base_vha, 0xe070,
5995 "This adapter does not support target mode.\n");
5996 return 0;
5997 }
5998
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005999 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006000 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006001
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006002 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006003
6004 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6005 if (!tgt) {
6006 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6007 "Unable to allocate struct qla_tgt\n");
6008 return -ENOMEM;
6009 }
6010
6011 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6012 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6013
6014 tgt->ha = ha;
6015 tgt->vha = base_vha;
6016 init_waitqueue_head(&tgt->waitQ);
6017 INIT_LIST_HEAD(&tgt->sess_list);
6018 INIT_LIST_HEAD(&tgt->del_sess_list);
6019 INIT_DELAYED_WORK(&tgt->sess_del_work,
6020 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
6021 spin_lock_init(&tgt->sess_work_lock);
6022 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6023 INIT_LIST_HEAD(&tgt->sess_works_list);
6024 spin_lock_init(&tgt->srr_lock);
6025 INIT_LIST_HEAD(&tgt->srr_ctio_list);
6026 INIT_LIST_HEAD(&tgt->srr_imm_list);
6027 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
6028 atomic_set(&tgt->tgt_global_resets_count, 0);
6029
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006030 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006031
6032 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6033 "qla_target(%d): using 64 Bit PCI addressing",
6034 base_vha->vp_idx);
6035 tgt->tgt_enable_64bit_addr = 1;
6036 /* 3 is reserved */
6037 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6038 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
6039 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
6040
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006041 if (base_vha->fc_vport)
6042 return 0;
6043
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006044 mutex_lock(&qla_tgt_mutex);
6045 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6046 mutex_unlock(&qla_tgt_mutex);
6047
6048 return 0;
6049}
6050
6051/* Must be called under tgt_host_action_mutex */
6052int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6053{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006054 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006055 return 0;
6056
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006057 if (vha->fc_vport) {
6058 qlt_release(vha->vha_tgt.qla_tgt);
6059 return 0;
6060 }
Quinn Tran33e79972014-09-25 06:14:55 -04006061
6062 /* free left over qfull cmds */
6063 qlt_init_term_exchange(vha);
6064
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006065 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006066 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006067 mutex_unlock(&qla_tgt_mutex);
6068
6069 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6070 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006071 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006072
6073 return 0;
6074}
6075
6076static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6077 unsigned char *b)
6078{
6079 int i;
6080
6081 pr_debug("qla2xxx HW vha->node_name: ");
6082 for (i = 0; i < WWN_SIZE; i++)
6083 pr_debug("%02x ", vha->node_name[i]);
6084 pr_debug("\n");
6085 pr_debug("qla2xxx HW vha->port_name: ");
6086 for (i = 0; i < WWN_SIZE; i++)
6087 pr_debug("%02x ", vha->port_name[i]);
6088 pr_debug("\n");
6089
6090 pr_debug("qla2xxx passed configfs WWPN: ");
6091 put_unaligned_be64(wwpn, b);
6092 for (i = 0; i < WWN_SIZE; i++)
6093 pr_debug("%02x ", b[i]);
6094 pr_debug("\n");
6095}
6096
6097/**
6098 * qla_tgt_lport_register - register lport with external module
6099 *
6100 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6101 * @wwpn: Passwd FC target WWPN
6102 * @callback: lport initialization callback for tcm_qla2xxx code
6103 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6104 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006105int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6106 u64 npiv_wwpn, u64 npiv_wwnn,
6107 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006108{
6109 struct qla_tgt *tgt;
6110 struct scsi_qla_host *vha;
6111 struct qla_hw_data *ha;
6112 struct Scsi_Host *host;
6113 unsigned long flags;
6114 int rc;
6115 u8 b[WWN_SIZE];
6116
6117 mutex_lock(&qla_tgt_mutex);
6118 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6119 vha = tgt->vha;
6120 ha = vha->hw;
6121
6122 host = vha->host;
6123 if (!host)
6124 continue;
6125
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006126 if (!(host->hostt->supported_mode & MODE_TARGET))
6127 continue;
6128
6129 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006130 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006131 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6132 host->host_no);
6133 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6134 continue;
6135 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006136 if (tgt->tgt_stop) {
6137 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6138 host->host_no);
6139 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6140 continue;
6141 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006142 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6143
6144 if (!scsi_host_get(host)) {
6145 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6146 "Unable to scsi_host_get() for"
6147 " qla2xxx scsi_host\n");
6148 continue;
6149 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006150 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006151
6152 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6153 scsi_host_put(host);
6154 continue;
6155 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006156 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6157 if (rc != 0)
6158 scsi_host_put(host);
6159
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006160 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006161 return rc;
6162 }
6163 mutex_unlock(&qla_tgt_mutex);
6164
6165 return -ENODEV;
6166}
6167EXPORT_SYMBOL(qlt_lport_register);
6168
6169/**
6170 * qla_tgt_lport_deregister - Degister lport
6171 *
6172 * @vha: Registered scsi_qla_host pointer
6173 */
6174void qlt_lport_deregister(struct scsi_qla_host *vha)
6175{
6176 struct qla_hw_data *ha = vha->hw;
6177 struct Scsi_Host *sh = vha->host;
6178 /*
6179 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6180 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006181 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006182 ha->tgt.tgt_ops = NULL;
6183 /*
6184 * Release the Scsi_Host reference for the underlying qla2xxx host
6185 */
6186 scsi_host_put(sh);
6187}
6188EXPORT_SYMBOL(qlt_lport_deregister);
6189
6190/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006191static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006192{
6193 struct qla_hw_data *ha = vha->hw;
6194
6195 switch (ql2x_ini_mode) {
6196 case QLA2XXX_INI_MODE_DISABLED:
6197 case QLA2XXX_INI_MODE_EXCLUSIVE:
6198 vha->host->active_mode = MODE_TARGET;
6199 break;
6200 case QLA2XXX_INI_MODE_ENABLED:
6201 vha->host->active_mode |= MODE_TARGET;
6202 break;
6203 default:
6204 break;
6205 }
6206
6207 if (ha->tgt.ini_mode_force_reverse)
6208 qla_reverse_ini_mode(vha);
6209}
6210
6211/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006212static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006213{
6214 struct qla_hw_data *ha = vha->hw;
6215
6216 switch (ql2x_ini_mode) {
6217 case QLA2XXX_INI_MODE_DISABLED:
6218 vha->host->active_mode = MODE_UNKNOWN;
6219 break;
6220 case QLA2XXX_INI_MODE_EXCLUSIVE:
6221 vha->host->active_mode = MODE_INITIATOR;
6222 break;
6223 case QLA2XXX_INI_MODE_ENABLED:
6224 vha->host->active_mode &= ~MODE_TARGET;
6225 break;
6226 default:
6227 break;
6228 }
6229
6230 if (ha->tgt.ini_mode_force_reverse)
6231 qla_reverse_ini_mode(vha);
6232}
6233
6234/*
6235 * qla_tgt_enable_vha - NO LOCK HELD
6236 *
6237 * host_reset, bring up w/ Target Mode Enabled
6238 */
6239void
6240qlt_enable_vha(struct scsi_qla_host *vha)
6241{
6242 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006243 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006244 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006245 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Trancdb898c2015-12-17 14:57:05 -05006246 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006247
6248 if (!tgt) {
6249 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6250 "Unable to locate qla_tgt pointer from"
6251 " struct qla_hw_data\n");
6252 dump_stack();
6253 return;
6254 }
6255
6256 spin_lock_irqsave(&ha->hardware_lock, flags);
6257 tgt->tgt_stopped = 0;
6258 qlt_set_mode(vha);
6259 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6260
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006261 if (vha->vp_idx) {
6262 qla24xx_disable_vp(vha);
6263 qla24xx_enable_vp(vha);
6264 } else {
Quinn Trancdb898c2015-12-17 14:57:05 -05006265 if (ha->msix_entries) {
6266 ql_dbg(ql_dbg_tgt, vha, 0xffff,
6267 "%s: host%ld : vector %d cpu %d\n",
6268 __func__, vha->host_no,
6269 ha->msix_entries[rspq_ent].vector,
6270 ha->msix_entries[rspq_ent].cpuid);
6271
6272 ha->tgt.rspq_vector_cpuid =
6273 ha->msix_entries[rspq_ent].cpuid;
6274 }
6275
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006276 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6277 qla2xxx_wake_dpc(base_vha);
6278 qla2x00_wait_for_hba_online(base_vha);
6279 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006280}
6281EXPORT_SYMBOL(qlt_enable_vha);
6282
6283/*
6284 * qla_tgt_disable_vha - NO LOCK HELD
6285 *
6286 * Disable Target Mode and reset the adapter
6287 */
Joern Engel55a90662014-09-16 16:23:15 -04006288static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006289{
6290 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006291 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006292 unsigned long flags;
6293
6294 if (!tgt) {
6295 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6296 "Unable to locate qla_tgt pointer from"
6297 " struct qla_hw_data\n");
6298 dump_stack();
6299 return;
6300 }
6301
6302 spin_lock_irqsave(&ha->hardware_lock, flags);
6303 qlt_clear_mode(vha);
6304 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6305
6306 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6307 qla2xxx_wake_dpc(vha);
6308 qla2x00_wait_for_hba_online(vha);
6309}
6310
6311/*
6312 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6313 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6314 * members.
6315 */
6316void
6317qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6318{
6319 if (!qla_tgt_mode_enabled(vha))
6320 return;
6321
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006322 vha->vha_tgt.qla_tgt = NULL;
6323
6324 mutex_init(&vha->vha_tgt.tgt_mutex);
6325 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006326
6327 qlt_clear_mode(vha);
6328
6329 /*
6330 * NOTE: Currently the value is kept the same for <24xx and
6331 * >=24xx ISPs. If it is necessary to change it,
6332 * the check should be added for specific ISPs,
6333 * assigning the value appropriately.
6334 */
6335 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006336
6337 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006338}
6339
6340void
6341qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6342{
6343 /*
6344 * FC-4 Feature bit 0 indicates target functionality to the name server.
6345 */
6346 if (qla_tgt_mode_enabled(vha)) {
6347 if (qla_ini_mode_enabled(vha))
6348 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6349 else
6350 ct_req->req.rff_id.fc4_feature = BIT_0;
6351 } else if (qla_ini_mode_enabled(vha)) {
6352 ct_req->req.rff_id.fc4_feature = BIT_1;
6353 }
6354}
6355
6356/*
6357 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6358 * @ha: HA context
6359 *
6360 * Beginning of ATIO ring has initialization control block already built
6361 * by nvram config routine.
6362 *
6363 * Returns 0 on success.
6364 */
6365void
6366qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6367{
6368 struct qla_hw_data *ha = vha->hw;
6369 uint16_t cnt;
6370 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6371
6372 if (!qla_tgt_mode_enabled(vha))
6373 return;
6374
6375 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6376 pkt->u.raw.signature = ATIO_PROCESSED;
6377 pkt++;
6378 }
6379
6380}
6381
6382/*
6383 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6384 * @ha: SCSI driver HA context
6385 */
6386void
Quinn Tran2f424b92015-12-17 14:57:07 -05006387qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006388{
6389 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006390 struct atio_from_isp *pkt;
6391 int cnt, i;
6392
6393 if (!vha->flags.online)
6394 return;
6395
Quinn Tran5f355092016-12-23 18:06:11 -08006396 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6397 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006398 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6399 cnt = pkt->u.raw.entry_count;
6400
Quinn Tran5f355092016-12-23 18:06:11 -08006401 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6402 /*
6403 * This packet is corrupted. The header + payload
6404 * can not be trusted. There is no point in passing
6405 * it further up.
6406 */
6407 ql_log(ql_log_warn, vha, 0xffff,
6408 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6409 pkt->u.isp24.fcp_hdr.s_id,
6410 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6411 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6412
6413 adjust_corrupted_atio(pkt);
6414 qlt_send_term_exchange(vha, NULL, pkt, ha_locked, 0);
6415 } else {
6416 qlt_24xx_atio_pkt_all_vps(vha,
6417 (struct atio_from_isp *)pkt, ha_locked);
6418 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006419
6420 for (i = 0; i < cnt; i++) {
6421 ha->tgt.atio_ring_index++;
6422 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6423 ha->tgt.atio_ring_index = 0;
6424 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6425 } else
6426 ha->tgt.atio_ring_ptr++;
6427
6428 pkt->u.raw.signature = ATIO_PROCESSED;
6429 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6430 }
6431 wmb();
6432 }
6433
6434 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006435 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Quinn Tran3761f3e2015-06-10 11:05:19 -04006436 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006437}
6438
6439void
Arun Easiaa230bc2013-01-30 03:34:39 -05006440qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006441{
6442 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006443 if (!QLA_TGT_MODE_ENABLED())
6444 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006445
Arun Easiaa230bc2013-01-30 03:34:39 -05006446 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6447 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6448 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6449
6450 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6451 struct qla_msix_entry *msix = &ha->msix_entries[2];
6452 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6453
6454 icb->msix_atio = cpu_to_le16(msix->entry);
6455 ql_dbg(ql_dbg_init, vha, 0xf072,
6456 "Registering ICB vector 0x%x for atio que.\n",
6457 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006458 }
6459}
6460
6461void
6462qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6463{
6464 struct qla_hw_data *ha = vha->hw;
6465
6466 if (qla_tgt_mode_enabled(vha)) {
6467 if (!ha->tgt.saved_set) {
6468 /* We save only once */
6469 ha->tgt.saved_exchange_count = nv->exchange_count;
6470 ha->tgt.saved_firmware_options_1 =
6471 nv->firmware_options_1;
6472 ha->tgt.saved_firmware_options_2 =
6473 nv->firmware_options_2;
6474 ha->tgt.saved_firmware_options_3 =
6475 nv->firmware_options_3;
6476 ha->tgt.saved_set = 1;
6477 }
6478
Bart Van Asschead950362015-07-09 07:24:08 -07006479 nv->exchange_count = cpu_to_le16(0xFFFF);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006480
6481 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006482 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006483
6484 /* Disable ini mode, if requested */
6485 if (!qla_ini_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006486 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006487
6488 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006489 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006490 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006491 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006492 if (ql2xtgt_tape_enable)
6493 /* Enable FC Tape support */
6494 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6495 else
6496 /* Disable FC Tape support */
6497 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6498
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006499 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006500 nv->host_p &= cpu_to_le32(~BIT_10);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006501
6502 /*
6503 * clear BIT 15 explicitly as we have seen at least
6504 * a couple of instances where this was set and this
6505 * was causing the firmware to not be initialized.
6506 */
6507 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006508 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006509 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006510 } else {
6511 if (ha->tgt.saved_set) {
6512 nv->exchange_count = ha->tgt.saved_exchange_count;
6513 nv->firmware_options_1 =
6514 ha->tgt.saved_firmware_options_1;
6515 nv->firmware_options_2 =
6516 ha->tgt.saved_firmware_options_2;
6517 nv->firmware_options_3 =
6518 ha->tgt.saved_firmware_options_3;
6519 }
6520 return;
6521 }
6522
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006523 if (ha->tgt.enable_class_2) {
6524 if (vha->flags.init_done)
6525 fc_host_supported_classes(vha->host) =
6526 FC_COS_CLASS2 | FC_COS_CLASS3;
6527
Bart Van Asschead950362015-07-09 07:24:08 -07006528 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006529 } else {
6530 if (vha->flags.init_done)
6531 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6532
Bart Van Asschead950362015-07-09 07:24:08 -07006533 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006534 }
6535}
6536
6537void
6538qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6539 struct init_cb_24xx *icb)
6540{
6541 struct qla_hw_data *ha = vha->hw;
6542
Quinn Tran481ce732015-12-17 14:57:08 -05006543 if (!QLA_TGT_MODE_ENABLED())
6544 return;
6545
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006546 if (ha->tgt.node_name_set) {
6547 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006548 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006549 }
Quinn Tran481ce732015-12-17 14:57:08 -05006550
6551 /* disable ZIO at start time. */
6552 if (!vha->flags.init_done) {
6553 uint32_t tmp;
6554 tmp = le32_to_cpu(icb->firmware_options_2);
6555 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6556 icb->firmware_options_2 = cpu_to_le32(tmp);
6557 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006558}
6559
Arun Easiaa230bc2013-01-30 03:34:39 -05006560void
6561qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6562{
6563 struct qla_hw_data *ha = vha->hw;
6564
6565 if (!QLA_TGT_MODE_ENABLED())
6566 return;
6567
6568 if (qla_tgt_mode_enabled(vha)) {
6569 if (!ha->tgt.saved_set) {
6570 /* We save only once */
6571 ha->tgt.saved_exchange_count = nv->exchange_count;
6572 ha->tgt.saved_firmware_options_1 =
6573 nv->firmware_options_1;
6574 ha->tgt.saved_firmware_options_2 =
6575 nv->firmware_options_2;
6576 ha->tgt.saved_firmware_options_3 =
6577 nv->firmware_options_3;
6578 ha->tgt.saved_set = 1;
6579 }
6580
Bart Van Asschead950362015-07-09 07:24:08 -07006581 nv->exchange_count = cpu_to_le16(0xFFFF);
Arun Easiaa230bc2013-01-30 03:34:39 -05006582
6583 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006584 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006585
6586 /* Disable ini mode, if requested */
6587 if (!qla_ini_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006588 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006589 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006590 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006591 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006592 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Himanshu Madhanic0f64622016-12-23 18:06:08 -08006593 /*
6594 * clear BIT 15 explicitly as we have seen at
6595 * least a couple of instances where this was set
6596 * and this was causing the firmware to not be
6597 * initialized.
6598 */
6599 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
Arun Easid154f352014-09-25 06:14:48 -04006600 if (ql2xtgt_tape_enable)
6601 /* Enable FC tape support */
6602 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6603 else
6604 /* Disable FC tape support */
6605 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6606
Arun Easiaa230bc2013-01-30 03:34:39 -05006607 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006608 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006609 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006610 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006611 } else {
6612 if (ha->tgt.saved_set) {
6613 nv->exchange_count = ha->tgt.saved_exchange_count;
6614 nv->firmware_options_1 =
6615 ha->tgt.saved_firmware_options_1;
6616 nv->firmware_options_2 =
6617 ha->tgt.saved_firmware_options_2;
6618 nv->firmware_options_3 =
6619 ha->tgt.saved_firmware_options_3;
6620 }
6621 return;
6622 }
6623
Arun Easiaa230bc2013-01-30 03:34:39 -05006624 if (ha->tgt.enable_class_2) {
6625 if (vha->flags.init_done)
6626 fc_host_supported_classes(vha->host) =
6627 FC_COS_CLASS2 | FC_COS_CLASS3;
6628
Bart Van Asschead950362015-07-09 07:24:08 -07006629 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006630 } else {
6631 if (vha->flags.init_done)
6632 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6633
Bart Van Asschead950362015-07-09 07:24:08 -07006634 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006635 }
6636}
6637
6638void
6639qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6640 struct init_cb_81xx *icb)
6641{
6642 struct qla_hw_data *ha = vha->hw;
6643
6644 if (!QLA_TGT_MODE_ENABLED())
6645 return;
6646
6647 if (ha->tgt.node_name_set) {
6648 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006649 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006650 }
Quinn Tran481ce732015-12-17 14:57:08 -05006651
6652 /* disable ZIO at start time. */
6653 if (!vha->flags.init_done) {
6654 uint32_t tmp;
6655 tmp = le32_to_cpu(icb->firmware_options_2);
6656 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6657 icb->firmware_options_2 = cpu_to_le32(tmp);
6658 }
6659
Arun Easiaa230bc2013-01-30 03:34:39 -05006660}
6661
6662void
6663qlt_83xx_iospace_config(struct qla_hw_data *ha)
6664{
6665 if (!QLA_TGT_MODE_ENABLED())
6666 return;
6667
6668 ha->msix_count += 1; /* For ATIO Q */
6669}
6670
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006671int
6672qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6673 struct sts_entry_24xx *pkt)
6674{
6675 switch (pkt->entry_type) {
6676 case ABTS_RECV_24XX:
6677 case ABTS_RESP_24XX:
6678 case CTIO_TYPE7:
6679 case NOTIFY_ACK_TYPE:
Quinn Tranf83adb62014-04-11 16:54:43 -04006680 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006681 return 1;
6682 default:
6683 return 0;
6684 }
6685}
6686
6687void
6688qlt_modify_vp_config(struct scsi_qla_host *vha,
6689 struct vp_config_entry_24xx *vpmod)
6690{
6691 if (qla_tgt_mode_enabled(vha))
6692 vpmod->options_idx1 &= ~BIT_5;
6693 /* Disable ini mode, if requested */
6694 if (!qla_ini_mode_enabled(vha))
6695 vpmod->options_idx1 &= ~BIT_4;
6696}
6697
6698void
6699qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6700{
6701 if (!QLA_TGT_MODE_ENABLED())
6702 return;
6703
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04006704 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006705 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6706 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6707 } else {
6708 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6709 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6710 }
6711
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006712 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6713 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006714 qlt_clear_mode(base_vha);
6715}
6716
Arun Easiaa230bc2013-01-30 03:34:39 -05006717irqreturn_t
6718qla83xx_msix_atio_q(int irq, void *dev_id)
6719{
6720 struct rsp_que *rsp;
6721 scsi_qla_host_t *vha;
6722 struct qla_hw_data *ha;
6723 unsigned long flags;
6724
6725 rsp = (struct rsp_que *) dev_id;
6726 ha = rsp->hw;
6727 vha = pci_get_drvdata(ha->pdev);
6728
Quinn Tran2f424b92015-12-17 14:57:07 -05006729 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006730
Quinn Tran2f424b92015-12-17 14:57:07 -05006731 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006732
Quinn Tran2f424b92015-12-17 14:57:07 -05006733 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006734
6735 return IRQ_HANDLED;
6736}
6737
Quinn Tran2f424b92015-12-17 14:57:07 -05006738static void
6739qlt_handle_abts_recv_work(struct work_struct *work)
6740{
6741 struct qla_tgt_sess_op *op = container_of(work,
6742 struct qla_tgt_sess_op, work);
6743 scsi_qla_host_t *vha = op->vha;
6744 struct qla_hw_data *ha = vha->hw;
6745 unsigned long flags;
6746
6747 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6748 return;
6749
6750 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6751 qlt_24xx_process_atio_queue(vha, 0);
6752 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6753
6754 spin_lock_irqsave(&ha->hardware_lock, flags);
6755 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6756 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6757}
6758
6759void
6760qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6761{
6762 struct qla_tgt_sess_op *op;
6763
6764 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6765
6766 if (!op) {
6767 /* do not reach for ATIO queue here. This is best effort err
6768 * recovery at this point.
6769 */
6770 qlt_response_pkt_all_vps(vha, pkt);
6771 return;
6772 }
6773
6774 memcpy(&op->atio, pkt, sizeof(*pkt));
6775 op->vha = vha;
6776 op->chip_reset = vha->hw->chip_reset;
6777 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6778 queue_work(qla_tgt_wq, &op->work);
6779 return;
6780}
6781
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006782int
6783qlt_mem_alloc(struct qla_hw_data *ha)
6784{
6785 if (!QLA_TGT_MODE_ENABLED())
6786 return 0;
6787
6788 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6789 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6790 if (!ha->tgt.tgt_vp_map)
6791 return -ENOMEM;
6792
6793 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6794 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6795 &ha->tgt.atio_dma, GFP_KERNEL);
6796 if (!ha->tgt.atio_ring) {
6797 kfree(ha->tgt.tgt_vp_map);
6798 return -ENOMEM;
6799 }
6800 return 0;
6801}
6802
6803void
6804qlt_mem_free(struct qla_hw_data *ha)
6805{
6806 if (!QLA_TGT_MODE_ENABLED())
6807 return;
6808
6809 if (ha->tgt.atio_ring) {
6810 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6811 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6812 ha->tgt.atio_dma);
6813 }
6814 kfree(ha->tgt.tgt_vp_map);
6815}
6816
6817/* vport_slock to be held by the caller */
6818void
6819qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6820{
6821 if (!QLA_TGT_MODE_ENABLED())
6822 return;
6823
6824 switch (cmd) {
6825 case SET_VP_IDX:
6826 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6827 break;
6828 case SET_AL_PA:
6829 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
6830 break;
6831 case RESET_VP_IDX:
6832 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6833 break;
6834 case RESET_AL_PA:
6835 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
6836 break;
6837 }
6838}
6839
6840static int __init qlt_parse_ini_mode(void)
6841{
6842 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6843 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6844 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6845 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6846 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6847 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
6848 else
6849 return false;
6850
6851 return true;
6852}
6853
6854int __init qlt_init(void)
6855{
6856 int ret;
6857
6858 if (!qlt_parse_ini_mode()) {
6859 ql_log(ql_log_fatal, NULL, 0xe06b,
6860 "qlt_parse_ini_mode() failed\n");
6861 return -EINVAL;
6862 }
6863
6864 if (!QLA_TGT_MODE_ENABLED())
6865 return 0;
6866
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006867 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6868 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6869 qla_tgt_mgmt_cmd), 0, NULL);
6870 if (!qla_tgt_mgmt_cmd_cachep) {
6871 ql_log(ql_log_fatal, NULL, 0xe06d,
6872 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07006873 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006874 }
6875
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006876 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
6877 sizeof(qlt_plogi_ack_t),
6878 __alignof__(qlt_plogi_ack_t),
6879 0, NULL);
6880
6881 if (!qla_tgt_plogi_cachep) {
6882 ql_log(ql_log_fatal, NULL, 0xe06d,
6883 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6884 ret = -ENOMEM;
6885 goto out_mgmt_cmd_cachep;
6886 }
6887
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006888 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6889 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6890 if (!qla_tgt_mgmt_cmd_mempool) {
6891 ql_log(ql_log_fatal, NULL, 0xe06e,
6892 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6893 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006894 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006895 }
6896
6897 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6898 if (!qla_tgt_wq) {
6899 ql_log(ql_log_fatal, NULL, 0xe06f,
6900 "alloc_workqueue for qla_tgt_wq failed\n");
6901 ret = -ENOMEM;
6902 goto out_cmd_mempool;
6903 }
6904 /*
6905 * Return 1 to signal that initiator-mode is being disabled
6906 */
6907 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6908
6909out_cmd_mempool:
6910 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006911out_plogi_cachep:
6912 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006913out_mgmt_cmd_cachep:
6914 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006915 return ret;
6916}
6917
6918void qlt_exit(void)
6919{
6920 if (!QLA_TGT_MODE_ENABLED())
6921 return;
6922
6923 destroy_workqueue(qla_tgt_wq);
6924 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006925 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006926 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006927}