blob: 124e8de290b37356db6266162208fd444e3e54da [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++;
218 if (vha->hw->tgt.num_pend_cmds > vha->hw->qla_stats.stat_max_pend_cmds)
219 vha->hw->qla_stats.stat_max_pend_cmds =
220 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 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400640void qlt_unreg_sess(struct qla_tgt_sess *sess)
641{
642 struct scsi_qla_host *vha = sess->vha;
643
644 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
645
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400646 if (!list_empty(&sess->del_list_entry))
647 list_del_init(&sess->del_list_entry);
648 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400649
650 INIT_WORK(&sess->free_work, qlt_free_session_done);
651 schedule_work(&sess->free_work);
652}
653EXPORT_SYMBOL(qlt_unreg_sess);
654
Quinn Tran75601512015-12-17 14:57:04 -0500655
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400656static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
657{
658 struct qla_hw_data *ha = vha->hw;
659 struct qla_tgt_sess *sess = NULL;
660 uint32_t unpacked_lun, lun = 0;
661 uint16_t loop_id;
662 int res = 0;
663 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
664 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -0500665 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400666
667 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
668 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400669 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -0400670 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -0500671 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400672 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -0500673 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400674#if 0 /* FIXME: do we need to choose a session here? */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400675 if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
676 sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
677 typeof(*sess), sess_list_entry);
678 switch (mcmd) {
679 case QLA_TGT_NEXUS_LOSS_SESS:
680 mcmd = QLA_TGT_NEXUS_LOSS;
681 break;
682 case QLA_TGT_ABORT_ALL_SESS:
683 mcmd = QLA_TGT_ABORT_ALL;
684 break;
685 case QLA_TGT_NEXUS_LOSS:
686 case QLA_TGT_ABORT_ALL:
687 break;
688 default:
689 ql_dbg(ql_dbg_tgt, vha, 0xe046,
690 "qla_target(%d): Not allowed "
691 "command %x in %s", vha->vp_idx,
692 mcmd, __func__);
693 sess = NULL;
694 break;
695 }
696 } else
697 sess = NULL;
698#endif
699 } else {
Quinn Tran75601512015-12-17 14:57:04 -0500700 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400701 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -0500702 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400703 }
704
705 ql_dbg(ql_dbg_tgt, vha, 0xe000,
706 "Using sess for qla_tgt_reset: %p\n", sess);
707 if (!sess) {
708 res = -ESRCH;
709 return res;
710 }
711
712 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400713 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
714 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400715 mcmd, loop_id);
716
717 lun = a->u.isp24.fcp_cmnd.lun;
718 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
719
720 return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
721 iocb, QLA24XX_MGMT_SEND_NACK);
722}
723
Quinn Tran75601512015-12-17 14:57:04 -0500724/* ha->tgt.sess_lock supposed to be held on entry */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400725static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
726 bool immediate)
727{
728 struct qla_tgt *tgt = sess->tgt;
729 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
730
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400731 if (sess->deleted) {
732 /* Upgrade to unconditional deletion in case it was temporary */
733 if (immediate && sess->deleted == QLA_SESS_DELETION_PENDING)
734 list_del(&sess->del_list_entry);
735 else
736 return;
737 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400738
739 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
740 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400741
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400742 if (immediate) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400743 dev_loss_tmo = 0;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400744 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
745 list_add(&sess->del_list_entry, &tgt->del_sess_list);
746 } else {
747 sess->deleted = QLA_SESS_DELETION_PENDING;
748 list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
749 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400750
751 sess->expires = jiffies + dev_loss_tmo * HZ;
752
753 ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
Alexei Potashnikdf673272015-07-14 16:00:46 -0400754 "qla_target(%d): session for port %8phC (loop ID %d s_id %02x:%02x:%02x)"
755 " scheduled for deletion in %u secs (expires: %lu) immed: %d, logout: %d, gen: %#x\n",
756 sess->vha->vp_idx, sess->port_name, sess->loop_id,
757 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
758 dev_loss_tmo, sess->expires, immediate, sess->logout_on_delete,
759 sess->generation);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400760
761 if (immediate)
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400762 mod_delayed_work(system_wq, &tgt->sess_del_work, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400763 else
764 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530765 sess->expires - jiffies);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400766}
767
Quinn Tran75601512015-12-17 14:57:04 -0500768/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -0400769static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400770{
771 struct qla_tgt_sess *sess;
772
773 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
774 qlt_schedule_sess_for_deletion(sess, true);
775
776 /* At this point tgt could be already dead */
777}
778
779static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
780 uint16_t *loop_id)
781{
782 struct qla_hw_data *ha = vha->hw;
783 dma_addr_t gid_list_dma;
784 struct gid_list_info *gid_list;
785 char *id_iter;
786 int res, rc, i;
787 uint16_t entries;
788
789 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
790 &gid_list_dma, GFP_KERNEL);
791 if (!gid_list) {
792 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
793 "qla_target(%d): DMA Alloc failed of %u\n",
794 vha->vp_idx, qla2x00_gid_list_size(ha));
795 return -ENOMEM;
796 }
797
798 /* Get list of logged in devices */
799 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
800 if (rc != QLA_SUCCESS) {
801 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
802 "qla_target(%d): get_id_list() failed: %x\n",
803 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500804 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400805 goto out_free_id_list;
806 }
807
808 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500809 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400810 for (i = 0; i < entries; i++) {
811 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
812 if ((gid->al_pa == s_id[2]) &&
813 (gid->area == s_id[1]) &&
814 (gid->domain == s_id[0])) {
815 *loop_id = le16_to_cpu(gid->loop_id);
816 res = 0;
817 break;
818 }
819 id_iter += ha->gid_list_info_size;
820 }
821
822out_free_id_list:
823 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
824 gid_list, gid_list_dma);
825 return res;
826}
827
Quinn Tran75601512015-12-17 14:57:04 -0500828/* ha->tgt.sess_lock supposed to be held on entry */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400829static void qlt_undelete_sess(struct qla_tgt_sess *sess)
830{
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400831 BUG_ON(sess->deleted != QLA_SESS_DELETION_PENDING);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400832
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400833 list_del_init(&sess->del_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400834 sess->deleted = 0;
835}
836
837static void qlt_del_sess_work_fn(struct delayed_work *work)
838{
839 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
840 sess_del_work);
841 struct scsi_qla_host *vha = tgt->vha;
842 struct qla_hw_data *ha = vha->hw;
843 struct qla_tgt_sess *sess;
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530844 unsigned long flags, elapsed;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400845
Quinn Tran75601512015-12-17 14:57:04 -0500846 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400847 while (!list_empty(&tgt->del_sess_list)) {
848 sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
849 del_list_entry);
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530850 elapsed = jiffies;
851 if (time_after_eq(elapsed, sess->expires)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400852 /* No turning back */
853 list_del_init(&sess->del_list_entry);
854 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400855
Jörn Engel08234e32013-06-12 16:27:54 -0400856 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
857 "Timeout: sess %p about to be deleted\n",
858 sess);
859 ha->tgt.tgt_ops->shutdown_sess(sess);
860 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400861 } else {
862 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530863 sess->expires - elapsed);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400864 break;
865 }
866 }
Quinn Tran75601512015-12-17 14:57:04 -0500867 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400868}
869
870/*
871 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
872 * Caller must put it.
873 */
874static struct qla_tgt_sess *qlt_create_sess(
875 struct scsi_qla_host *vha,
876 fc_port_t *fcport,
877 bool local)
878{
879 struct qla_hw_data *ha = vha->hw;
880 struct qla_tgt_sess *sess;
881 unsigned long flags;
882 unsigned char be_sid[3];
883
884 /* Check to avoid double sessions */
Quinn Tran75601512015-12-17 14:57:04 -0500885 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800886 list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400887 sess_list_entry) {
888 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
889 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
890 "Double sess %p found (s_id %x:%x:%x, "
891 "loop_id %d), updating to d_id %x:%x:%x, "
892 "loop_id %d", sess, sess->s_id.b.domain,
893 sess->s_id.b.al_pa, sess->s_id.b.area,
894 sess->loop_id, fcport->d_id.b.domain,
895 fcport->d_id.b.al_pa, fcport->d_id.b.area,
896 fcport->loop_id);
897
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400898 /* Cannot undelete at this point */
899 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
Quinn Tran75601512015-12-17 14:57:04 -0500900 spin_unlock_irqrestore(&ha->tgt.sess_lock,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400901 flags);
902 return NULL;
903 }
904
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400905 if (sess->deleted)
906 qlt_undelete_sess(sess);
907
908 kref_get(&sess->se_sess->sess_kref);
Roland Dreierc8292d12012-10-11 13:41:32 -0700909 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
910 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
911
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400912 if (sess->local && !local)
913 sess->local = 0;
Alexei Potashnikdf673272015-07-14 16:00:46 -0400914
915 qlt_do_generation_tick(vha, &sess->generation);
916
Quinn Tran75601512015-12-17 14:57:04 -0500917 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400918
919 return sess;
920 }
921 }
Quinn Tran75601512015-12-17 14:57:04 -0500922 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400923
924 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
925 if (!sess) {
926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400927 "qla_target(%u): session allocation failed, all commands "
928 "from port %8phC will be refused", vha->vp_idx,
929 fcport->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400930
931 return NULL;
932 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800933 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400934 sess->vha = vha;
935 sess->s_id = fcport->d_id;
936 sess->loop_id = fcport->loop_id;
937 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400938 INIT_LIST_HEAD(&sess->del_list_entry);
939
940 /* Under normal circumstances we want to logout from firmware when
941 * session eventually ends and release corresponding nport handle.
942 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
943 * code will adjust these flags as necessary. */
944 sess->logout_on_delete = 1;
945 sess->keep_nport_handle = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400946
947 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
948 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800949 sess, vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400950
951 be_sid[0] = sess->s_id.b.domain;
952 be_sid[1] = sess->s_id.b.area;
953 be_sid[2] = sess->s_id.b.al_pa;
954 /*
955 * Determine if this fc_port->port_name is allowed to access
956 * target mode using explict NodeACLs+MappedLUNs, or using
957 * TPG demo mode. If this is successful a target mode FC nexus
958 * is created.
959 */
960 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
961 &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
962 kfree(sess);
963 return NULL;
964 }
965 /*
966 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
Quinn Tran75601512015-12-17 14:57:04 -0500967 * access across ->tgt.sess_lock reaquire.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400968 */
969 kref_get(&sess->se_sess->sess_kref);
970
Roland Dreierc8292d12012-10-11 13:41:32 -0700971 sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400972 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
973 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
974
Quinn Tran75601512015-12-17 14:57:04 -0500975 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800976 list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
977 vha->vha_tgt.qla_tgt->sess_count++;
Alexei Potashnikdf673272015-07-14 16:00:46 -0400978 qlt_do_generation_tick(vha, &sess->generation);
Quinn Tran75601512015-12-17 14:57:04 -0500979 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400980
981 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400982 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
983 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
984 vha->vp_idx, local ? "local " : "", fcport->port_name,
985 fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
986 sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400987
988 return sess;
989}
990
991/*
Alexei Potashnikdf673272015-07-14 16:00:46 -0400992 * Called from qla2x00_reg_remote_port()
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400993 */
994void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
995{
996 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800997 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400998 struct qla_tgt_sess *sess;
999 unsigned long flags;
1000
1001 if (!vha->hw->tgt.tgt_ops)
1002 return;
1003
1004 if (!tgt || (fcport->port_type != FCT_INITIATOR))
1005 return;
1006
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001007 if (qla_ini_mode_enabled(vha))
1008 return;
1009
Quinn Tran75601512015-12-17 14:57:04 -05001010 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001011 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001012 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001013 return;
1014 }
1015 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
1016 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001017 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001018
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001019 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001020 sess = qlt_create_sess(vha, fcport, false);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001021 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001022
Quinn Tran75601512015-12-17 14:57:04 -05001023 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001024 } else if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1025 /* Point of no return */
Quinn Tran75601512015-12-17 14:57:04 -05001026 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001027 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001028 } else {
1029 kref_get(&sess->se_sess->sess_kref);
1030
1031 if (sess->deleted) {
1032 qlt_undelete_sess(sess);
1033
1034 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001035 "qla_target(%u): %ssession for port %8phC "
1036 "(loop ID %d) reappeared\n", vha->vp_idx,
1037 sess->local ? "local " : "", sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001038 sess->loop_id);
1039
1040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
1041 "Reappeared sess %p\n", sess);
1042 }
Roland Dreierc8292d12012-10-11 13:41:32 -07001043 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
1044 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001045 }
1046
1047 if (sess && sess->local) {
1048 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
1049 "qla_target(%u): local session for "
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001050 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
1051 fcport->port_name, sess->loop_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001052 sess->local = 0;
1053 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001054 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05001055 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001056}
1057
Alexei Potashnikdf673272015-07-14 16:00:46 -04001058/*
1059 * max_gen - specifies maximum session generation
1060 * at which this deletion requestion is still valid
1061 */
1062void
1063qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001064{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001065 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001066 struct qla_tgt_sess *sess;
Quinn Tran75601512015-12-17 14:57:04 -05001067 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001068
1069 if (!vha->hw->tgt.tgt_ops)
1070 return;
1071
Roland Dreierb2032fd2015-07-14 16:00:42 -04001072 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001073 return;
1074
Quinn Tran75601512015-12-17 14:57:04 -05001075 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001076 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001077 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001078 return;
1079 }
1080 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
1081 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001082 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001083 return;
1084 }
1085
Alexei Potashnikdf673272015-07-14 16:00:46 -04001086 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001087 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001088 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1089 "Ignoring stale deletion request for se_sess %p / sess %p"
1090 " for port %8phC, req_gen %d, sess_gen %d\n",
1091 sess->se_sess, sess, sess->port_name, max_gen,
1092 sess->generation);
1093 return;
1094 }
1095
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001096 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1097
1098 sess->local = 1;
1099 qlt_schedule_sess_for_deletion(sess, false);
Quinn Tran75601512015-12-17 14:57:04 -05001100 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001101}
1102
1103static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1104{
1105 struct qla_hw_data *ha = tgt->ha;
1106 unsigned long flags;
1107 int res;
1108 /*
1109 * We need to protect against race, when tgt is freed before or
1110 * inside wake_up()
1111 */
1112 spin_lock_irqsave(&ha->hardware_lock, flags);
1113 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1114 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
1115 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
1116 res = (tgt->sess_count == 0);
1117 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1118
1119 return res;
1120}
1121
1122/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001123int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001124{
1125 struct scsi_qla_host *vha = tgt->vha;
1126 struct qla_hw_data *ha = tgt->ha;
1127 unsigned long flags;
1128
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001129 mutex_lock(&qla_tgt_mutex);
1130 if (!vha->fc_vport) {
1131 struct Scsi_Host *sh = vha->host;
1132 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1133 bool npiv_vports;
1134
1135 spin_lock_irqsave(sh->host_lock, flags);
1136 npiv_vports = (fc_host->npiv_vports_inuse);
1137 spin_unlock_irqrestore(sh->host_lock, flags);
1138
1139 if (npiv_vports) {
1140 mutex_unlock(&qla_tgt_mutex);
1141 return -EPERM;
1142 }
1143 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001144 if (tgt->tgt_stop || tgt->tgt_stopped) {
1145 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1146 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001147 mutex_unlock(&qla_tgt_mutex);
1148 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001149 }
1150
1151 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1152 vha->host_no, vha);
1153 /*
1154 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1155 * Lock is needed, because we still can get an incoming packet.
1156 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001157 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001158 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001159 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001160 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001161 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001162 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001163 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001164
Tejun Heo43829732012-08-20 14:51:24 -07001165 flush_delayed_work(&tgt->sess_del_work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001166
1167 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1168 "Waiting for sess works (tgt %p)", tgt);
1169 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1170 while (!list_empty(&tgt->sess_works_list)) {
1171 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1172 flush_scheduled_work();
1173 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1174 }
1175 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1176
1177 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1178 "Waiting for tgt %p: list_empty(sess_list)=%d "
1179 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
1180 tgt->sess_count);
1181
1182 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1183
1184 /* Big hammer */
1185 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
1186 qlt_disable_vha(vha);
1187
1188 /* Wait for sessions to clear out (just in case) */
1189 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001190 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001191}
1192EXPORT_SYMBOL(qlt_stop_phase1);
1193
1194/* Called by tcm_qla2xxx configfs code */
1195void qlt_stop_phase2(struct qla_tgt *tgt)
1196{
1197 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001198 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001199 unsigned long flags;
1200
1201 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001202 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001203 "Already in tgt->tgt_stopped state\n");
1204 dump_stack();
1205 return;
1206 }
1207
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001208 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001209 "Waiting for %d IRQ commands to complete (tgt %p)",
1210 tgt->irq_cmd_count, tgt);
1211
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001212 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001213 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05001214 while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001215 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1216 udelay(2);
1217 spin_lock_irqsave(&ha->hardware_lock, flags);
1218 }
1219 tgt->tgt_stop = 0;
1220 tgt->tgt_stopped = 1;
1221 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001222 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001223
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001224 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001225 tgt);
1226}
1227EXPORT_SYMBOL(qlt_stop_phase2);
1228
1229/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001230static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001231{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001232 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001233
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001234 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001235 qlt_stop_phase2(tgt);
1236
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001237 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001238
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001239 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001240 "Release of tgt %p finished\n", tgt);
1241
1242 kfree(tgt);
1243}
1244
1245/* ha->hardware_lock supposed to be held on entry */
1246static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1247 const void *param, unsigned int param_size)
1248{
1249 struct qla_tgt_sess_work_param *prm;
1250 unsigned long flags;
1251
1252 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1253 if (!prm) {
1254 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1255 "qla_target(%d): Unable to create session "
1256 "work, command will be refused", 0);
1257 return -ENOMEM;
1258 }
1259
1260 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1261 "Scheduling work (type %d, prm %p)"
1262 " to find session for param %p (size %d, tgt %p)\n",
1263 type, prm, param, param_size, tgt);
1264
1265 prm->type = type;
1266 memcpy(&prm->tm_iocb, param, param_size);
1267
1268 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1269 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1270 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1271
1272 schedule_work(&tgt->sess_work);
1273
1274 return 0;
1275}
1276
1277/*
1278 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1279 */
1280static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1281 struct imm_ntfy_from_isp *ntfy,
1282 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1283 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1284{
1285 struct qla_hw_data *ha = vha->hw;
1286 request_t *pkt;
1287 struct nack_to_isp *nack;
1288
1289 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1290
1291 /* Send marker if required */
1292 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1293 return;
1294
1295 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1296 if (!pkt) {
1297 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1298 "qla_target(%d): %s failed: unable to allocate "
1299 "request packet\n", vha->vp_idx, __func__);
1300 return;
1301 }
1302
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001303 if (vha->vha_tgt.qla_tgt != NULL)
1304 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001305
1306 pkt->entry_type = NOTIFY_ACK_TYPE;
1307 pkt->entry_count = 1;
1308
1309 nack = (struct nack_to_isp *)pkt;
1310 nack->ox_id = ntfy->ox_id;
1311
1312 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1313 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1314 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001315 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001316 }
1317 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1318 nack->u.isp24.status = ntfy->u.isp24.status;
1319 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001320 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001321 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1322 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1323 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1324 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1325 nack->u.isp24.srr_reject_code = srr_reject_code;
1326 nack->u.isp24.srr_reject_code_expl = srr_explan;
1327 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1328
1329 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1330 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1331 vha->vp_idx, nack->u.isp24.status);
1332
Himanshu Madhani63163e02014-09-25 06:14:59 -04001333 /* Memory Barrier */
1334 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001335 qla2x00_start_iocbs(vha, vha->req);
1336}
1337
1338/*
1339 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1340 */
1341static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1342 struct abts_recv_from_24xx *abts, uint32_t status,
1343 bool ids_reversed)
1344{
1345 struct qla_hw_data *ha = vha->hw;
1346 struct abts_resp_to_24xx *resp;
1347 uint32_t f_ctl;
1348 uint8_t *p;
1349
1350 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1351 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1352 ha, abts, status);
1353
1354 /* Send marker if required */
1355 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1356 return;
1357
Arun Easib6a029e2014-09-25 06:14:52 -04001358 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001359 if (!resp) {
1360 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1361 "qla_target(%d): %s failed: unable to allocate "
1362 "request packet", vha->vp_idx, __func__);
1363 return;
1364 }
1365
1366 resp->entry_type = ABTS_RESP_24XX;
1367 resp->entry_count = 1;
1368 resp->nport_handle = abts->nport_handle;
1369 resp->vp_index = vha->vp_idx;
1370 resp->sof_type = abts->sof_type;
1371 resp->exchange_address = abts->exchange_address;
1372 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001373 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001374 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1375 F_CTL_SEQ_INITIATIVE);
1376 p = (uint8_t *)&f_ctl;
1377 resp->fcp_hdr_le.f_ctl[0] = *p++;
1378 resp->fcp_hdr_le.f_ctl[1] = *p++;
1379 resp->fcp_hdr_le.f_ctl[2] = *p;
1380 if (ids_reversed) {
1381 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1382 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1383 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1384 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1385 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1386 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1387 } else {
1388 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1389 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1390 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1391 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1392 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1393 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1394 }
1395 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1396 if (status == FCP_TMF_CMPL) {
1397 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1398 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1399 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1400 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1401 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1402 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1403 } else {
1404 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1405 resp->payload.ba_rjt.reason_code =
1406 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1407 /* Other bytes are zero */
1408 }
1409
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001410 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001411
Himanshu Madhani63163e02014-09-25 06:14:59 -04001412 /* Memory Barrier */
1413 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001414 qla2x00_start_iocbs(vha, vha->req);
1415}
1416
1417/*
1418 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1419 */
1420static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1421 struct abts_resp_from_24xx_fw *entry)
1422{
1423 struct ctio7_to_24xx *ctio;
1424
1425 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1426 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1427 /* Send marker if required */
1428 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1429 return;
1430
Arun Easib6a029e2014-09-25 06:14:52 -04001431 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001432 if (ctio == NULL) {
1433 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1434 "qla_target(%d): %s failed: unable to allocate "
1435 "request packet\n", vha->vp_idx, __func__);
1436 return;
1437 }
1438
1439 /*
1440 * We've got on entrance firmware's response on by us generated
1441 * ABTS response. So, in it ID fields are reversed.
1442 */
1443
1444 ctio->entry_type = CTIO_TYPE7;
1445 ctio->entry_count = 1;
1446 ctio->nport_handle = entry->nport_handle;
1447 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001448 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001449 ctio->vp_index = vha->vp_idx;
1450 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1451 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1452 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1453 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001454 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1455 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001456 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001457
Himanshu Madhani63163e02014-09-25 06:14:59 -04001458 /* Memory Barrier */
1459 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001460 qla2x00_start_iocbs(vha, vha->req);
1461
1462 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1463 FCP_TMF_CMPL, true);
1464}
1465
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001466static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1467{
1468 struct qla_tgt_sess_op *op;
1469 struct qla_tgt_cmd *cmd;
1470
1471 spin_lock(&vha->cmd_list_lock);
1472
1473 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1474 if (tag == op->atio.u.isp24.exchange_addr) {
1475 op->aborted = true;
1476 spin_unlock(&vha->cmd_list_lock);
1477 return 1;
1478 }
1479 }
1480
1481 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1482 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001483 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001484 spin_unlock(&vha->cmd_list_lock);
1485 return 1;
1486 }
1487 }
1488
1489 spin_unlock(&vha->cmd_list_lock);
1490 return 0;
1491}
1492
1493/* drop cmds for the given lun
1494 * XXX only looks for cmds on the port through which lun reset was recieved
1495 * XXX does not go through the list of other port (which may have cmds
1496 * for the same lun)
1497 */
1498static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1499 uint32_t lun, uint8_t *s_id)
1500{
1501 struct qla_tgt_sess_op *op;
1502 struct qla_tgt_cmd *cmd;
1503 uint32_t key;
1504
1505 key = sid_to_key(s_id);
1506 spin_lock(&vha->cmd_list_lock);
1507 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1508 uint32_t op_key;
1509 uint32_t op_lun;
1510
1511 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1512 op_lun = scsilun_to_int(
1513 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1514 if (op_key == key && op_lun == lun)
1515 op->aborted = true;
1516 }
1517 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1518 uint32_t cmd_key;
1519 uint32_t cmd_lun;
1520
1521 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1522 cmd_lun = scsilun_to_int(
1523 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1524 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001525 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001526 }
1527 spin_unlock(&vha->cmd_list_lock);
1528}
1529
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001530/* ha->hardware_lock supposed to be held on entry */
1531static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1532 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1533{
1534 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001535 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001536 struct qla_tgt_mgmt_cmd *mcmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001537 struct se_cmd *se_cmd;
1538 u32 lun = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001539 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001540 bool found_lun = false;
1541
1542 spin_lock(&se_sess->sess_cmd_lock);
1543 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1544 struct qla_tgt_cmd *cmd =
1545 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
Bart Van Assche649ee052015-04-14 13:26:44 +02001546 if (se_cmd->tag == abts->exchange_addr_to_abort) {
Steve Hodgson06e97b42012-11-16 08:06:17 -08001547 lun = cmd->unpacked_lun;
1548 found_lun = true;
1549 break;
1550 }
1551 }
1552 spin_unlock(&se_sess->sess_cmd_lock);
1553
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001554 /* cmd not in LIO lists, look in qla list */
1555 if (!found_lun) {
1556 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1557 /* send TASK_ABORT response immediately */
1558 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1559 return 0;
1560 } else {
1561 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1562 "unable to find cmd in driver or LIO for tag 0x%x\n",
1563 abts->exchange_addr_to_abort);
1564 return -ENOENT;
1565 }
1566 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001567
1568 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1569 "qla_target(%d): task abort (tag=%d)\n",
1570 vha->vp_idx, abts->exchange_addr_to_abort);
1571
1572 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1573 if (mcmd == NULL) {
1574 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1575 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1576 vha->vp_idx, __func__);
1577 return -ENOMEM;
1578 }
1579 memset(mcmd, 0, sizeof(*mcmd));
1580
1581 mcmd->sess = sess;
1582 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Arun Easi80187f82014-09-25 06:14:53 -04001583 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001584
Steve Hodgson06e97b42012-11-16 08:06:17 -08001585 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001586 abts->exchange_addr_to_abort);
1587 if (rc != 0) {
1588 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1589 "qla_target(%d): tgt_ops->handle_tmr()"
1590 " failed: %d", vha->vp_idx, rc);
1591 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1592 return -EFAULT;
1593 }
1594
1595 return 0;
1596}
1597
1598/*
1599 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1600 */
1601static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1602 struct abts_recv_from_24xx *abts)
1603{
1604 struct qla_hw_data *ha = vha->hw;
1605 struct qla_tgt_sess *sess;
1606 uint32_t tag = abts->exchange_addr_to_abort;
1607 uint8_t s_id[3];
1608 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001609 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001610
1611 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1612 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1613 "qla_target(%d): ABTS: Abort Sequence not "
1614 "supported\n", vha->vp_idx);
1615 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1616 return;
1617 }
1618
1619 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1620 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1621 "qla_target(%d): ABTS: Unknown Exchange "
1622 "Address received\n", vha->vp_idx);
1623 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1624 return;
1625 }
1626
1627 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1628 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1629 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1630 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1631 le32_to_cpu(abts->fcp_hdr_le.parameter));
1632
1633 s_id[0] = abts->fcp_hdr_le.s_id[2];
1634 s_id[1] = abts->fcp_hdr_le.s_id[1];
1635 s_id[2] = abts->fcp_hdr_le.s_id[0];
1636
Quinn Tran75601512015-12-17 14:57:04 -05001637 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001638 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1639 if (!sess) {
1640 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1641 "qla_target(%d): task abort for non-existant session\n",
1642 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001643 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001644 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001645
1646 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1647
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001648 if (rc != 0) {
1649 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1650 false);
1651 }
1652 return;
1653 }
Quinn Tran75601512015-12-17 14:57:04 -05001654 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1655
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001656
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001657 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1658 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1659 return;
1660 }
1661
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001662 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1663 if (rc != 0) {
1664 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1665 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1666 vha->vp_idx, rc);
1667 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1668 return;
1669 }
1670}
1671
1672/*
1673 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1674 */
1675static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1676 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1677{
1678 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1679 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001680 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001681
1682 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1683 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1684 ha, atio, resp_code);
1685
1686 /* Send marker if required */
1687 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1688 return;
1689
1690 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1691 if (ctio == NULL) {
1692 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1693 "qla_target(%d): %s failed: unable to allocate "
1694 "request packet\n", ha->vp_idx, __func__);
1695 return;
1696 }
1697
1698 ctio->entry_type = CTIO_TYPE7;
1699 ctio->entry_count = 1;
1700 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1701 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07001702 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001703 ctio->vp_index = ha->vp_idx;
1704 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1705 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1706 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1707 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1708 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07001709 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001710 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1711 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001712 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07001713 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1714 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07001715 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001716
Himanshu Madhani63163e02014-09-25 06:14:59 -04001717 /* Memory Barrier */
1718 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001719 qla2x00_start_iocbs(ha, ha->req);
1720}
1721
1722void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1723{
1724 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1725}
1726EXPORT_SYMBOL(qlt_free_mcmd);
1727
1728/* callback from target fabric module code */
1729void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1730{
1731 struct scsi_qla_host *vha = mcmd->sess->vha;
1732 struct qla_hw_data *ha = vha->hw;
1733 unsigned long flags;
1734
1735 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1736 "TM response mcmd (%p) status %#x state %#x",
1737 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1738
1739 spin_lock_irqsave(&ha->hardware_lock, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04001740
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001741 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04001742 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001743 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04001744 * previous life, just abort the processing.
1745 */
1746 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001747 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
1748 vha->flags.online, qla2x00_reset_active(vha),
1749 mcmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04001750 ha->tgt.tgt_ops->free_mcmd(mcmd);
1751 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1752 return;
1753 }
1754
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001755 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1756 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1757 0, 0, 0, 0, 0, 0);
1758 else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05001759 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001760 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1761 mcmd->fc_tm_rsp, false);
1762 else
1763 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1764 mcmd->fc_tm_rsp);
1765 }
1766 /*
1767 * Make the callback for ->free_mcmd() to queue_work() and invoke
1768 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1769 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1770 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1771 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1772 * qlt_xmit_tm_rsp() returns here..
1773 */
1774 ha->tgt.tgt_ops->free_mcmd(mcmd);
1775 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1776}
1777EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1778
1779/* No locks */
1780static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1781{
1782 struct qla_tgt_cmd *cmd = prm->cmd;
1783
1784 BUG_ON(cmd->sg_cnt == 0);
1785
1786 prm->sg = (struct scatterlist *)cmd->sg;
1787 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1788 cmd->sg_cnt, cmd->dma_data_direction);
1789 if (unlikely(prm->seg_cnt == 0))
1790 goto out_err;
1791
1792 prm->cmd->sg_mapped = 1;
1793
Quinn Tranf83adb62014-04-11 16:54:43 -04001794 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
1795 /*
1796 * If greater than four sg entries then we need to allocate
1797 * the continuation entries
1798 */
1799 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1800 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1801 prm->tgt->datasegs_per_cmd,
1802 prm->tgt->datasegs_per_cont);
1803 } else {
1804 /* DIF */
1805 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1806 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1807 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
1808 prm->tot_dsds = prm->seg_cnt;
1809 } else
1810 prm->tot_dsds = prm->seg_cnt;
1811
1812 if (cmd->prot_sg_cnt) {
1813 prm->prot_sg = cmd->prot_sg;
1814 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
1815 cmd->prot_sg, cmd->prot_sg_cnt,
1816 cmd->dma_data_direction);
1817 if (unlikely(prm->prot_seg_cnt == 0))
1818 goto out_err;
1819
1820 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1821 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1822 /* Dif Bundling not support here */
1823 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
1824 cmd->blk_sz);
1825 prm->tot_dsds += prm->prot_seg_cnt;
1826 } else
1827 prm->tot_dsds += prm->prot_seg_cnt;
1828 }
1829 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001830
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001831 return 0;
1832
1833out_err:
1834 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1835 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1836 0, prm->cmd->sg_cnt);
1837 return -1;
1838}
1839
Joern Engelf9b67212014-09-16 16:23:18 -04001840static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001841{
1842 struct qla_hw_data *ha = vha->hw;
1843
Joern Engelf9b67212014-09-16 16:23:18 -04001844 if (!cmd->sg_mapped)
1845 return;
1846
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001847 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1848 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04001849
1850 if (cmd->prot_sg_cnt)
1851 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
1852 cmd->dma_data_direction);
1853
1854 if (cmd->ctx_dsd_alloced)
1855 qla2x00_clean_dsd_pool(ha, NULL, cmd);
1856
1857 if (cmd->ctx)
1858 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001859}
1860
1861static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1862 uint32_t req_cnt)
1863{
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001864 uint32_t cnt, cnt_in;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001865
1866 if (vha->req->cnt < (req_cnt + 2)) {
Arun Easi75554b62014-09-25 06:14:45 -04001867 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001868 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001869
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001870 if (vha->req->ring_index < cnt)
1871 vha->req->cnt = cnt - vha->req->ring_index;
1872 else
1873 vha->req->cnt = vha->req->length -
1874 (vha->req->ring_index - cnt);
Arnd Bergmannbc7095a2016-03-15 22:40:31 +01001875
1876 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1877 ql_dbg(ql_dbg_io, vha, 0x305a,
1878 "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",
1879 vha->vp_idx, vha->req->ring_index,
1880 vha->req->cnt, req_cnt, cnt, cnt_in,
1881 vha->req->length);
1882 return -EAGAIN;
1883 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001884 }
1885
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001886 vha->req->cnt -= req_cnt;
1887
1888 return 0;
1889}
1890
1891/*
1892 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1893 */
1894static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1895{
1896 /* Adjust ring index. */
1897 vha->req->ring_index++;
1898 if (vha->req->ring_index == vha->req->length) {
1899 vha->req->ring_index = 0;
1900 vha->req->ring_ptr = vha->req->ring;
1901 } else {
1902 vha->req->ring_ptr++;
1903 }
1904 return (cont_entry_t *)vha->req->ring_ptr;
1905}
1906
1907/* ha->hardware_lock supposed to be held on entry */
1908static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1909{
1910 struct qla_hw_data *ha = vha->hw;
1911 uint32_t h;
1912
1913 h = ha->tgt.current_handle;
1914 /* always increment cmd handle */
1915 do {
1916 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001917 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001918 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1919 if (h == ha->tgt.current_handle) {
Arun Easi667024a2014-09-25 06:14:47 -04001920 ql_dbg(ql_dbg_io, vha, 0x305b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001921 "qla_target(%d): Ran out of "
1922 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1923 h = QLA_TGT_NULL_HANDLE;
1924 break;
1925 }
1926 } while ((h == QLA_TGT_NULL_HANDLE) ||
1927 (h == QLA_TGT_SKIP_HANDLE) ||
1928 (ha->tgt.cmds[h-1] != NULL));
1929
1930 if (h != QLA_TGT_NULL_HANDLE)
1931 ha->tgt.current_handle = h;
1932
1933 return h;
1934}
1935
1936/* ha->hardware_lock supposed to be held on entry */
1937static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1938 struct scsi_qla_host *vha)
1939{
1940 uint32_t h;
1941 struct ctio7_to_24xx *pkt;
1942 struct qla_hw_data *ha = vha->hw;
1943 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001944 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001945
1946 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1947 prm->pkt = pkt;
1948 memset(pkt, 0, sizeof(*pkt));
1949
1950 pkt->entry_type = CTIO_TYPE7;
1951 pkt->entry_count = (uint8_t)prm->req_cnt;
1952 pkt->vp_index = vha->vp_idx;
1953
1954 h = qlt_make_handle(vha);
1955 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1956 /*
1957 * CTIO type 7 from the firmware doesn't provide a way to
1958 * know the initiator's LOOP ID, hence we can't find
1959 * the session and, so, the command.
1960 */
1961 return -EAGAIN;
1962 } else
1963 ha->tgt.cmds[h-1] = prm->cmd;
1964
1965 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1966 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07001967 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001968 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1969 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1970 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1971 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1972 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001973 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1974 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001975 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1976
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001977 return 0;
1978}
1979
1980/*
1981 * ha->hardware_lock supposed to be held on entry. We have already made sure
1982 * that there is sufficient amount of request entries to not drop it.
1983 */
1984static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1985 struct scsi_qla_host *vha)
1986{
1987 int cnt;
1988 uint32_t *dword_ptr;
1989 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1990
1991 /* Build continuation packets */
1992 while (prm->seg_cnt > 0) {
1993 cont_a64_entry_t *cont_pkt64 =
1994 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
1995
1996 /*
1997 * Make sure that from cont_pkt64 none of
1998 * 64-bit specific fields used for 32-bit
1999 * addressing. Cast to (cont_entry_t *) for
2000 * that.
2001 */
2002
2003 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2004
2005 cont_pkt64->entry_count = 1;
2006 cont_pkt64->sys_define = 0;
2007
2008 if (enable_64bit_addressing) {
2009 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2010 dword_ptr =
2011 (uint32_t *)&cont_pkt64->dseg_0_address;
2012 } else {
2013 cont_pkt64->entry_type = CONTINUE_TYPE;
2014 dword_ptr =
2015 (uint32_t *)&((cont_entry_t *)
2016 cont_pkt64)->dseg_0_address;
2017 }
2018
2019 /* Load continuation entry data segments */
2020 for (cnt = 0;
2021 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2022 cnt++, prm->seg_cnt--) {
2023 *dword_ptr++ =
2024 cpu_to_le32(pci_dma_lo32
2025 (sg_dma_address(prm->sg)));
2026 if (enable_64bit_addressing) {
2027 *dword_ptr++ =
2028 cpu_to_le32(pci_dma_hi32
2029 (sg_dma_address
2030 (prm->sg)));
2031 }
2032 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2033
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002034 prm->sg = sg_next(prm->sg);
2035 }
2036 }
2037}
2038
2039/*
2040 * ha->hardware_lock supposed to be held on entry. We have already made sure
2041 * that there is sufficient amount of request entries to not drop it.
2042 */
2043static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2044 struct scsi_qla_host *vha)
2045{
2046 int cnt;
2047 uint32_t *dword_ptr;
2048 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2049 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2050
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002051 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2052
2053 /* Setup packet address segment pointer */
2054 dword_ptr = pkt24->u.status0.dseg_0_address;
2055
2056 /* Set total data segment count */
2057 if (prm->seg_cnt)
2058 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2059
2060 if (prm->seg_cnt == 0) {
2061 /* No data transfer */
2062 *dword_ptr++ = 0;
2063 *dword_ptr = 0;
2064 return;
2065 }
2066
2067 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002068
2069 /* Load command entry data segments */
2070 for (cnt = 0;
2071 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2072 cnt++, prm->seg_cnt--) {
2073 *dword_ptr++ =
2074 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2075 if (enable_64bit_addressing) {
2076 *dword_ptr++ =
2077 cpu_to_le32(pci_dma_hi32(
2078 sg_dma_address(prm->sg)));
2079 }
2080 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2081
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002082 prm->sg = sg_next(prm->sg);
2083 }
2084
2085 qlt_load_cont_data_segments(prm, vha);
2086}
2087
2088static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2089{
2090 return cmd->bufflen > 0;
2091}
2092
2093/*
2094 * Called without ha->hardware_lock held
2095 */
2096static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2097 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2098 uint32_t *full_req_cnt)
2099{
2100 struct qla_tgt *tgt = cmd->tgt;
2101 struct scsi_qla_host *vha = tgt->vha;
2102 struct qla_hw_data *ha = vha->hw;
2103 struct se_cmd *se_cmd = &cmd->se_cmd;
2104
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002105 prm->cmd = cmd;
2106 prm->tgt = tgt;
2107 prm->rq_result = scsi_status;
2108 prm->sense_buffer = &cmd->sense_buffer[0];
2109 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2110 prm->sg = NULL;
2111 prm->seg_cnt = -1;
2112 prm->req_cnt = 1;
2113 prm->add_status_pkt = 0;
2114
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002115 /* Send marker if required */
2116 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2117 return -EFAULT;
2118
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002119 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2120 if (qlt_pci_map_calc_cnt(prm) != 0)
2121 return -EAGAIN;
2122 }
2123
2124 *full_req_cnt = prm->req_cnt;
2125
2126 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2127 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002128 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002129 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2130 prm->residual, se_cmd->tag,
2131 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2132 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002133 prm->rq_result |= SS_RESIDUAL_UNDER;
2134 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2135 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002136 ql_dbg(ql_dbg_io, vha, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002137 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2138 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2139 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002140 prm->rq_result |= SS_RESIDUAL_OVER;
2141 }
2142
2143 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2144 /*
2145 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2146 * ignored in *xmit_response() below
2147 */
2148 if (qlt_has_data(cmd)) {
2149 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2150 (IS_FWI2_CAPABLE(ha) &&
2151 (prm->rq_result != 0))) {
2152 prm->add_status_pkt = 1;
2153 (*full_req_cnt)++;
2154 }
2155 }
2156 }
2157
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002158 return 0;
2159}
2160
2161static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2162 struct qla_tgt_cmd *cmd, int sending_sense)
2163{
2164 if (ha->tgt.enable_class_2)
2165 return 0;
2166
2167 if (sending_sense)
2168 return cmd->conf_compl_supported;
2169 else
2170 return ha->tgt.enable_explicit_conf &&
2171 cmd->conf_compl_supported;
2172}
2173
2174#ifdef CONFIG_QLA_TGT_DEBUG_SRR
2175/*
2176 * Original taken from the XFS code
2177 */
2178static unsigned long qlt_srr_random(void)
2179{
2180 static int Inited;
2181 static unsigned long RandomValue;
2182 static DEFINE_SPINLOCK(lock);
2183 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
2184 register long rv;
2185 register long lo;
2186 register long hi;
2187 unsigned long flags;
2188
2189 spin_lock_irqsave(&lock, flags);
2190 if (!Inited) {
2191 RandomValue = jiffies;
2192 Inited = 1;
2193 }
2194 rv = RandomValue;
2195 hi = rv / 127773;
2196 lo = rv % 127773;
2197 rv = 16807 * lo - 2836 * hi;
2198 if (rv <= 0)
2199 rv += 2147483647;
2200 RandomValue = rv;
2201 spin_unlock_irqrestore(&lock, flags);
2202 return rv;
2203}
2204
2205static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
2206{
2207#if 0 /* This is not a real status packets lost, so it won't lead to SRR */
2208 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
2209 == 50) {
2210 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
2211 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
Bart Van Assche649ee052015-04-14 13:26:44 +02002212 "Dropping cmd %p (tag %d) status", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002213 }
2214#endif
2215 /*
2216 * It's currently not possible to simulate SRRs for FCP_WRITE without
2217 * a physical link layer failure, so don't even try here..
2218 */
2219 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
2220 return;
2221
2222 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
2223 ((qlt_srr_random() % 100) == 20)) {
2224 int i, leave = 0;
2225 unsigned int tot_len = 0;
2226
2227 while (leave == 0)
2228 leave = qlt_srr_random() % cmd->sg_cnt;
2229
2230 for (i = 0; i < leave; i++)
2231 tot_len += cmd->sg[i].length;
2232
2233 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
2234 "Cutting cmd %p (tag %d) buffer"
2235 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
Bart Van Assche649ee052015-04-14 13:26:44 +02002236 " cmd->sg_cnt %d)", cmd, se_cmd->tag, tot_len, leave,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002237 cmd->bufflen, cmd->sg_cnt);
2238
2239 cmd->bufflen = tot_len;
2240 cmd->sg_cnt = leave;
2241 }
2242
2243 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
2244 unsigned int offset = qlt_srr_random() % cmd->bufflen;
2245
2246 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
2247 "Cutting cmd %p (tag %d) buffer head "
Bart Van Assche649ee052015-04-14 13:26:44 +02002248 "to offset %d (cmd->bufflen %d)", cmd, se_cmd->tag, offset,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002249 cmd->bufflen);
2250 if (offset == 0)
2251 *xmit_type &= ~QLA_TGT_XMIT_DATA;
2252 else if (qlt_set_data_offset(cmd, offset)) {
2253 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
Bart Van Assche649ee052015-04-14 13:26:44 +02002254 "qlt_set_data_offset() failed (tag %d)", se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002255 }
2256 }
2257}
2258#else
2259static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
2260{}
2261#endif
2262
2263static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2264 struct qla_tgt_prm *prm)
2265{
2266 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2267 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002268 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002269 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002270 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002271 CTIO7_FLAGS_EXPLICIT_CONFORM |
2272 CTIO7_FLAGS_CONFORM_REQ);
2273 }
2274 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2275 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2276 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2277 int i;
2278
2279 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
2280 if (prm->cmd->se_cmd.scsi_status != 0) {
2281 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2282 "Skipping EXPLICIT_CONFORM and "
2283 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2284 "non GOOD status\n");
2285 goto skip_explict_conf;
2286 }
Bart Van Asschead950362015-07-09 07:24:08 -07002287 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002288 CTIO7_FLAGS_EXPLICIT_CONFORM |
2289 CTIO7_FLAGS_CONFORM_REQ);
2290 }
2291skip_explict_conf:
2292 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002293 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002294 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002295 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002296 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002297 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002298 ctio->u.status1.sense_length =
2299 cpu_to_le16(prm->sense_buffer_len);
2300 for (i = 0; i < prm->sense_buffer_len/4; i++)
2301 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2302 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2303#if 0
2304 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
2305 static int q;
2306 if (q < 10) {
2307 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
2308 "qla_target(%d): %d bytes of sense "
2309 "lost", prm->tgt->ha->vp_idx,
2310 prm->sense_buffer_len % 4);
2311 q++;
2312 }
2313 }
2314#endif
2315 } else {
2316 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002317 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002318 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002319 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002320 ctio->u.status1.sense_length = 0;
2321 memset(ctio->u.status1.sense_data, 0,
2322 sizeof(ctio->u.status1.sense_data));
2323 }
2324
2325 /* Sense with len > 24, is it possible ??? */
2326}
2327
Quinn Tranf83adb62014-04-11 16:54:43 -04002328
2329
2330/* diff */
2331static inline int
2332qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2333{
2334 /*
2335 * Uncomment when corresponding SCSI changes are done.
2336 *
2337 if (!sp->cmd->prot_chk)
2338 return 0;
2339 *
2340 */
2341 switch (se_cmd->prot_op) {
2342 case TARGET_PROT_DOUT_INSERT:
2343 case TARGET_PROT_DIN_STRIP:
2344 if (ql2xenablehba_err_chk >= 1)
2345 return 1;
2346 break;
2347 case TARGET_PROT_DOUT_PASS:
2348 case TARGET_PROT_DIN_PASS:
2349 if (ql2xenablehba_err_chk >= 2)
2350 return 1;
2351 break;
2352 case TARGET_PROT_DIN_INSERT:
2353 case TARGET_PROT_DOUT_STRIP:
2354 return 1;
2355 default:
2356 break;
2357 }
2358 return 0;
2359}
2360
2361/*
2362 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
2363 *
2364 */
2365static inline void
2366qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
2367{
2368 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2369
2370 /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
2371 * have been immplemented by TCM, before AppTag is avail.
2372 * Look for modesense_handlers[]
2373 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002374 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002375 ctx->app_tag_mask[0] = 0x0;
2376 ctx->app_tag_mask[1] = 0x0;
2377
2378 switch (se_cmd->prot_type) {
2379 case TARGET_DIF_TYPE0_PROT:
2380 /*
2381 * No check for ql2xenablehba_err_chk, as it would be an
2382 * I/O error if hba tag generation is not done.
2383 */
2384 ctx->ref_tag = cpu_to_le32(lba);
2385
2386 if (!qlt_hba_err_chk_enabled(se_cmd))
2387 break;
2388
2389 /* enable ALL bytes of the ref tag */
2390 ctx->ref_tag_mask[0] = 0xff;
2391 ctx->ref_tag_mask[1] = 0xff;
2392 ctx->ref_tag_mask[2] = 0xff;
2393 ctx->ref_tag_mask[3] = 0xff;
2394 break;
2395 /*
2396 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
2397 * 16 bit app tag.
2398 */
2399 case TARGET_DIF_TYPE1_PROT:
2400 ctx->ref_tag = cpu_to_le32(lba);
2401
2402 if (!qlt_hba_err_chk_enabled(se_cmd))
2403 break;
2404
2405 /* enable ALL bytes of the ref tag */
2406 ctx->ref_tag_mask[0] = 0xff;
2407 ctx->ref_tag_mask[1] = 0xff;
2408 ctx->ref_tag_mask[2] = 0xff;
2409 ctx->ref_tag_mask[3] = 0xff;
2410 break;
2411 /*
2412 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
2413 * match LBA in CDB + N
2414 */
2415 case TARGET_DIF_TYPE2_PROT:
2416 ctx->ref_tag = cpu_to_le32(lba);
2417
2418 if (!qlt_hba_err_chk_enabled(se_cmd))
2419 break;
2420
2421 /* enable ALL bytes of the ref tag */
2422 ctx->ref_tag_mask[0] = 0xff;
2423 ctx->ref_tag_mask[1] = 0xff;
2424 ctx->ref_tag_mask[2] = 0xff;
2425 ctx->ref_tag_mask[3] = 0xff;
2426 break;
2427
2428 /* For Type 3 protection: 16 bit GUARD only */
2429 case TARGET_DIF_TYPE3_PROT:
2430 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2431 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2432 break;
2433 }
2434}
2435
2436
2437static inline int
2438qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2439{
2440 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002441 uint32_t transfer_length = 0;
2442 uint32_t data_bytes;
2443 uint32_t dif_bytes;
2444 uint8_t bundling = 1;
2445 uint8_t *clr_ptr;
2446 struct crc_context *crc_ctx_pkt = NULL;
2447 struct qla_hw_data *ha;
2448 struct ctio_crc2_to_fw *pkt;
2449 dma_addr_t crc_ctx_dma;
2450 uint16_t fw_prot_opts = 0;
2451 struct qla_tgt_cmd *cmd = prm->cmd;
2452 struct se_cmd *se_cmd = &cmd->se_cmd;
2453 uint32_t h;
2454 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002455 uint16_t t16;
Quinn Tranf83adb62014-04-11 16:54:43 -04002456
Quinn Tranf83adb62014-04-11 16:54:43 -04002457 ha = vha->hw;
2458
2459 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2460 prm->pkt = pkt;
2461 memset(pkt, 0, sizeof(*pkt));
2462
2463 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2464 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2465 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2466 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2467
2468 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2469 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2470 bundling = 0;
2471
2472 /* Compute dif len and adjust data len to incude protection */
2473 data_bytes = cmd->bufflen;
2474 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2475
2476 switch (se_cmd->prot_op) {
2477 case TARGET_PROT_DIN_INSERT:
2478 case TARGET_PROT_DOUT_STRIP:
2479 transfer_length = data_bytes;
2480 data_bytes += dif_bytes;
2481 break;
2482
2483 case TARGET_PROT_DIN_STRIP:
2484 case TARGET_PROT_DOUT_INSERT:
2485 case TARGET_PROT_DIN_PASS:
2486 case TARGET_PROT_DOUT_PASS:
2487 transfer_length = data_bytes + dif_bytes;
2488 break;
2489
2490 default:
2491 BUG();
2492 break;
2493 }
2494
2495 if (!qlt_hba_err_chk_enabled(se_cmd))
2496 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2497 /* HBA error checking enabled */
2498 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2499 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2500 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2501 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2502 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2503 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2504 }
2505
2506 switch (se_cmd->prot_op) {
2507 case TARGET_PROT_DIN_INSERT:
2508 case TARGET_PROT_DOUT_INSERT:
2509 fw_prot_opts |= PO_MODE_DIF_INSERT;
2510 break;
2511 case TARGET_PROT_DIN_STRIP:
2512 case TARGET_PROT_DOUT_STRIP:
2513 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2514 break;
2515 case TARGET_PROT_DIN_PASS:
2516 case TARGET_PROT_DOUT_PASS:
2517 fw_prot_opts |= PO_MODE_DIF_PASS;
2518 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2519 break;
2520 default:/* Normal Request */
2521 fw_prot_opts |= PO_MODE_DIF_PASS;
2522 break;
2523 }
2524
2525
2526 /* ---- PKT ---- */
2527 /* Update entry type to indicate Command Type CRC_2 IOCB */
2528 pkt->entry_type = CTIO_CRC2;
2529 pkt->entry_count = 1;
2530 pkt->vp_index = vha->vp_idx;
2531
2532 h = qlt_make_handle(vha);
2533 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2534 /*
2535 * CTIO type 7 from the firmware doesn't provide a way to
2536 * know the initiator's LOOP ID, hence we can't find
2537 * the session and, so, the command.
2538 */
2539 return -EAGAIN;
2540 } else
2541 ha->tgt.cmds[h-1] = prm->cmd;
2542
2543
2544 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2545 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002546 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002547 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2548 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2549 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2550 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002551
2552 /* silence compile warning */
2553 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2554 pkt->ox_id = cpu_to_le16(t16);
2555
2556 t16 = (atio->u.isp24.attr << 9);
2557 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002558 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2559
2560 /* Set transfer direction */
2561 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002562 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002563 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002564 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002565
2566
2567 pkt->dseg_count = prm->tot_dsds;
2568 /* Fibre channel byte count */
2569 pkt->transfer_length = cpu_to_le32(transfer_length);
2570
2571
2572 /* ----- CRC context -------- */
2573
2574 /* Allocate CRC context from global pool */
2575 crc_ctx_pkt = cmd->ctx =
2576 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2577
2578 if (!crc_ctx_pkt)
2579 goto crc_queuing_error;
2580
2581 /* Zero out CTX area. */
2582 clr_ptr = (uint8_t *)crc_ctx_pkt;
2583 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2584
2585 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2586 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2587
2588 /* Set handle */
2589 crc_ctx_pkt->handle = pkt->handle;
2590
2591 qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
2592
2593 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2594 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2595 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2596
2597
2598 if (!bundling) {
2599 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2600 } else {
2601 /*
2602 * Configure Bundling if we need to fetch interlaving
2603 * protection PCI accesses
2604 */
2605 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2606 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2607 crc_ctx_pkt->u.bundling.dseg_count =
2608 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2609 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2610 }
2611
2612 /* Finish the common fields of CRC pkt */
2613 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2614 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2615 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002616 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002617
2618
2619 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07002620 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04002621
2622 if (!bundling && prm->prot_seg_cnt) {
2623 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2624 prm->tot_dsds, cmd))
2625 goto crc_queuing_error;
2626 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2627 (prm->tot_dsds - prm->prot_seg_cnt), cmd))
2628 goto crc_queuing_error;
2629
2630 if (bundling && prm->prot_seg_cnt) {
2631 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002632 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04002633
2634 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2635 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2636 prm->prot_seg_cnt, cmd))
2637 goto crc_queuing_error;
2638 }
2639 return QLA_SUCCESS;
2640
2641crc_queuing_error:
2642 /* Cleanup will be performed by the caller */
2643
2644 return QLA_FUNCTION_FAILED;
2645}
2646
2647
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002648/*
2649 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2650 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2651 */
2652int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2653 uint8_t scsi_status)
2654{
2655 struct scsi_qla_host *vha = cmd->vha;
2656 struct qla_hw_data *ha = vha->hw;
2657 struct ctio7_to_24xx *pkt;
2658 struct qla_tgt_prm prm;
2659 uint32_t full_req_cnt = 0;
2660 unsigned long flags = 0;
2661 int res;
2662
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002663 spin_lock_irqsave(&ha->hardware_lock, flags);
2664 if (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
2665 cmd->state = QLA_TGT_STATE_PROCESSED;
2666 if (cmd->sess->logout_completed)
2667 /* no need to terminate. FW already freed exchange. */
2668 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2669 else
Quinn Trana07100e2015-12-07 19:48:57 -05002670 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002671 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2672 return 0;
2673 }
2674 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2675
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002676 memset(&prm, 0, sizeof(prm));
2677 qlt_check_srr_debug(cmd, &xmit_type);
2678
2679 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
Quinn Tranf83adb62014-04-11 16:54:43 -04002680 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
2681 (xmit_type & QLA_TGT_XMIT_STATUS) ?
2682 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
2683 &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002684
2685 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2686 &full_req_cnt);
2687 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002688 return res;
2689 }
2690
2691 spin_lock_irqsave(&ha->hardware_lock, flags);
2692
Himanshu Madhanice1025c2015-12-17 14:56:58 -05002693 if (xmit_type == QLA_TGT_XMIT_STATUS)
2694 vha->tgt_counters.core_qla_snd_status++;
2695 else
2696 vha->tgt_counters.core_qla_que_buf++;
2697
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002698 if (!vha->flags.online || cmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002699 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002700 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002701 * previous life, just abort the processing.
2702 */
2703 cmd->state = QLA_TGT_STATE_PROCESSED;
2704 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2705 ql_dbg(ql_dbg_async, vha, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002706 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
2707 vha->flags.online, qla2x00_reset_active(vha),
2708 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002709 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2710 return 0;
2711 }
2712
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002713 /* Does F/W have an IOCBs for this request */
2714 res = qlt_check_reserve_free_req(vha, full_req_cnt);
2715 if (unlikely(res))
2716 goto out_unmap_unlock;
2717
Quinn Tranf83adb62014-04-11 16:54:43 -04002718 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
2719 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2720 else
2721 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Quinn Tran810e30b2015-06-10 11:05:20 -04002722 if (unlikely(res != 0)) {
2723 vha->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002724 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04002725 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002726
2727 pkt = (struct ctio7_to_24xx *)prm.pkt;
2728
2729 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2730 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002731 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002732 CTIO7_FLAGS_STATUS_MODE_0);
2733
Quinn Tranf83adb62014-04-11 16:54:43 -04002734 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2735 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002736
2737 if (prm.add_status_pkt == 0) {
2738 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2739 pkt->u.status0.scsi_status =
2740 cpu_to_le16(prm.rq_result);
2741 pkt->u.status0.residual =
2742 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07002743 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002744 CTIO7_FLAGS_SEND_STATUS);
2745 if (qlt_need_explicit_conf(ha, cmd, 0)) {
2746 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002747 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002748 CTIO7_FLAGS_EXPLICIT_CONFORM |
2749 CTIO7_FLAGS_CONFORM_REQ);
2750 }
2751 }
2752
2753 } else {
2754 /*
2755 * We have already made sure that there is sufficient
2756 * amount of request entries to not drop HW lock in
2757 * req_pkt().
2758 */
2759 struct ctio7_to_24xx *ctio =
2760 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2761
Arun Easi667024a2014-09-25 06:14:47 -04002762 ql_dbg(ql_dbg_io, vha, 0x305e,
2763 "Building additional status packet 0x%p.\n",
2764 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002765
Quinn Tranf83adb62014-04-11 16:54:43 -04002766 /*
2767 * T10Dif: ctio_crc2_to_fw overlay ontop of
2768 * ctio7_to_24xx
2769 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002770 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04002771 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002772 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04002773 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002774 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07002775 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002776 CTIO7_FLAGS_DATA_IN);
2777
2778 /* Real finish is ctio_m1's finish */
2779 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07002780 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002781 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04002782
2783 /* qlt_24xx_init_ctio_to_isp will correct
2784 * all neccessary fields that's part of CTIO7.
2785 * There should be no residual of CTIO-CRC2 data.
2786 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002787 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2788 &prm);
2789 pr_debug("Status CTIO7: %p\n", ctio);
2790 }
2791 } else
2792 qlt_24xx_init_ctio_to_isp(pkt, &prm);
2793
2794
2795 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04002796 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002797
Himanshu Madhani63163e02014-09-25 06:14:59 -04002798 /* Memory Barrier */
2799 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002800 qla2x00_start_iocbs(vha, vha->req);
2801 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2802
2803 return 0;
2804
2805out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04002806 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002807 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2808
2809 return res;
2810}
2811EXPORT_SYMBOL(qlt_xmit_response);
2812
2813int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2814{
2815 struct ctio7_to_24xx *pkt;
2816 struct scsi_qla_host *vha = cmd->vha;
2817 struct qla_hw_data *ha = vha->hw;
2818 struct qla_tgt *tgt = cmd->tgt;
2819 struct qla_tgt_prm prm;
2820 unsigned long flags;
2821 int res = 0;
2822
2823 memset(&prm, 0, sizeof(prm));
2824 prm.cmd = cmd;
2825 prm.tgt = tgt;
2826 prm.sg = NULL;
2827 prm.req_cnt = 1;
2828
2829 /* Send marker if required */
2830 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2831 return -EIO;
2832
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002833 /* Calculate number of entries and segments required */
2834 if (qlt_pci_map_calc_cnt(&prm) != 0)
2835 return -EAGAIN;
2836
2837 spin_lock_irqsave(&ha->hardware_lock, flags);
2838
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002839 if (!vha->flags.online || (cmd->reset_count != ha->chip_reset) ||
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002840 (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)) {
Arun Easib6a029e2014-09-25 06:14:52 -04002841 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002842 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002843 * previous life, just abort the processing.
2844 */
2845 cmd->state = QLA_TGT_STATE_NEED_DATA;
2846 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2847 ql_dbg(ql_dbg_async, vha, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002848 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2849 vha->flags.online, qla2x00_reset_active(vha),
2850 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002851 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2852 return 0;
2853 }
2854
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002855 /* Does F/W have an IOCBs for this request */
2856 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2857 if (res != 0)
2858 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04002859 if (cmd->se_cmd.prot_op)
2860 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2861 else
2862 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002863
Quinn Tran810e30b2015-06-10 11:05:20 -04002864 if (unlikely(res != 0)) {
2865 vha->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002866 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04002867 }
2868
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002869 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07002870 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002871 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002872
2873 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2874 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002875
2876 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04002877 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002878
Himanshu Madhani63163e02014-09-25 06:14:59 -04002879 /* Memory Barrier */
2880 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002881 qla2x00_start_iocbs(vha, vha->req);
2882 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2883
2884 return res;
2885
2886out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04002887 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002888 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2889
2890 return res;
2891}
2892EXPORT_SYMBOL(qlt_rdy_to_xfer);
2893
Quinn Tranf83adb62014-04-11 16:54:43 -04002894
2895/*
2896 * Checks the guard or meta-data for the type of error
2897 * detected by the HBA.
2898 */
2899static inline int
2900qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
2901 struct ctio_crc_from_fw *sts)
2902{
2903 uint8_t *ap = &sts->actual_dif[0];
2904 uint8_t *ep = &sts->expected_dif[0];
2905 uint32_t e_ref_tag, a_ref_tag;
2906 uint16_t e_app_tag, a_app_tag;
2907 uint16_t e_guard, a_guard;
2908 uint64_t lba = cmd->se_cmd.t_task_lba;
2909
2910 a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
2911 a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
2912 a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
2913
2914 e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
2915 e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
2916 e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
2917
2918 ql_dbg(ql_dbg_tgt, vha, 0xe075,
2919 "iocb(s) %p Returned STATUS.\n", sts);
2920
2921 ql_dbg(ql_dbg_tgt, vha, 0xf075,
Hans Wennborgfc385042014-08-05 21:43:29 -07002922 "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 -04002923 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2924 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
2925
2926 /*
2927 * Ignore sector if:
2928 * For type 3: ref & app tag is all 'f's
2929 * For type 0,1,2: app tag is all 'f's
2930 */
2931 if ((a_app_tag == 0xffff) &&
2932 ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
2933 (a_ref_tag == 0xffffffff))) {
2934 uint32_t blocks_done;
2935
2936 /* 2TB boundary case covered automatically with this */
2937 blocks_done = e_ref_tag - (uint32_t)lba + 1;
2938 cmd->se_cmd.bad_sector = e_ref_tag;
2939 cmd->se_cmd.pi_err = 0;
2940 ql_dbg(ql_dbg_tgt, vha, 0xf074,
2941 "need to return scsi good\n");
2942
2943 /* Update protection tag */
2944 if (cmd->prot_sg_cnt) {
Bart Van Assche52c82822015-07-09 07:23:26 -07002945 uint32_t i, k = 0, num_ent;
Quinn Tranf83adb62014-04-11 16:54:43 -04002946 struct scatterlist *sg, *sgl;
2947
2948
2949 sgl = cmd->prot_sg;
2950
2951 /* Patch the corresponding protection tags */
2952 for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
2953 num_ent = sg_dma_len(sg) / 8;
2954 if (k + num_ent < blocks_done) {
2955 k += num_ent;
2956 continue;
2957 }
Quinn Tranf83adb62014-04-11 16:54:43 -04002958 k = blocks_done;
2959 break;
2960 }
2961
2962 if (k != blocks_done) {
2963 ql_log(ql_log_warn, vha, 0xf076,
2964 "unexpected tag values tag:lba=%u:%llu)\n",
2965 e_ref_tag, (unsigned long long)lba);
2966 goto out;
2967 }
2968
2969#if 0
2970 struct sd_dif_tuple *spt;
2971 /* TODO:
2972 * This section came from initiator. Is it valid here?
2973 * should ulp be override with actual val???
2974 */
2975 spt = page_address(sg_page(sg)) + sg->offset;
2976 spt += j;
2977
2978 spt->app_tag = 0xffff;
2979 if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
2980 spt->ref_tag = 0xffffffff;
2981#endif
2982 }
2983
2984 return 0;
2985 }
2986
2987 /* check guard */
2988 if (e_guard != a_guard) {
2989 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
2990 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
2991
2992 ql_log(ql_log_warn, vha, 0xe076,
2993 "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",
2994 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2995 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
2996 a_guard, e_guard, cmd);
2997 goto out;
2998 }
2999
3000 /* check ref tag */
3001 if (e_ref_tag != a_ref_tag) {
3002 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
3003 cmd->se_cmd.bad_sector = e_ref_tag;
3004
3005 ql_log(ql_log_warn, vha, 0xe077,
3006 "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",
3007 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3008 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3009 a_guard, e_guard, cmd);
3010 goto out;
3011 }
3012
3013 /* check appl tag */
3014 if (e_app_tag != a_app_tag) {
3015 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
3016 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3017
3018 ql_log(ql_log_warn, vha, 0xe078,
3019 "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",
3020 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3021 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3022 a_guard, e_guard, cmd);
3023 goto out;
3024 }
3025out:
3026 return 1;
3027}
3028
3029
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003030/* If hardware_lock held on entry, might drop it, then reaquire */
3031/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003032static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3033 struct imm_ntfy_from_isp *ntfy)
3034{
3035 struct nack_to_isp *nack;
3036 struct qla_hw_data *ha = vha->hw;
3037 request_t *pkt;
3038 int ret = 0;
3039
3040 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3041 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3042
3043 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3044 if (pkt == NULL) {
3045 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3046 "qla_target(%d): %s failed: unable to allocate "
3047 "request packet\n", vha->vp_idx, __func__);
3048 return -ENOMEM;
3049 }
3050
3051 pkt->entry_type = NOTIFY_ACK_TYPE;
3052 pkt->entry_count = 1;
3053 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3054
3055 nack = (struct nack_to_isp *)pkt;
3056 nack->ox_id = ntfy->ox_id;
3057
3058 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3059 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3060 nack->u.isp24.flags = ntfy->u.isp24.flags &
3061 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3062 }
3063
3064 /* terminate */
3065 nack->u.isp24.flags |=
3066 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3067
3068 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3069 nack->u.isp24.status = ntfy->u.isp24.status;
3070 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3071 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3072 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3073 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3074 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3075 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3076
3077 qla2x00_start_iocbs(vha, vha->req);
3078 return ret;
3079}
3080
3081static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3082 struct imm_ntfy_from_isp *imm, int ha_locked)
3083{
3084 unsigned long flags = 0;
3085 int rc;
3086
3087 if (qlt_issue_marker(vha, ha_locked) < 0)
3088 return;
3089
3090 if (ha_locked) {
3091 rc = __qlt_send_term_imm_notif(vha, imm);
3092
3093#if 0 /* Todo */
3094 if (rc == -ENOMEM)
3095 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3096#endif
3097 goto done;
3098 }
3099
3100 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3101 rc = __qlt_send_term_imm_notif(vha, imm);
3102
3103#if 0 /* Todo */
3104 if (rc == -ENOMEM)
3105 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3106#endif
3107
3108done:
3109 if (!ha_locked)
3110 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3111}
3112
3113/* If hardware_lock held on entry, might drop it, then reaquire */
3114/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003115static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3116 struct qla_tgt_cmd *cmd,
3117 struct atio_from_isp *atio)
3118{
3119 struct ctio7_to_24xx *ctio24;
3120 struct qla_hw_data *ha = vha->hw;
3121 request_t *pkt;
3122 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003123 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003124
3125 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3126
Arun Easib6a029e2014-09-25 06:14:52 -04003127 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003128 if (pkt == NULL) {
3129 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3130 "qla_target(%d): %s failed: unable to allocate "
3131 "request packet\n", vha->vp_idx, __func__);
3132 return -ENOMEM;
3133 }
3134
3135 if (cmd != NULL) {
3136 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3137 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3138 "qla_target(%d): Terminating cmd %p with "
3139 "incorrect state %d\n", vha->vp_idx, cmd,
3140 cmd->state);
3141 } else
3142 ret = 1;
3143 }
3144
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003145 vha->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003146 pkt->entry_count = 1;
3147 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3148
3149 ctio24 = (struct ctio7_to_24xx *)pkt;
3150 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003151 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003152 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003153 ctio24->vp_index = vha->vp_idx;
3154 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3155 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3156 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3157 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3158 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07003159 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003160 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003161 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3162 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003163
3164 /* Most likely, it isn't needed */
3165 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3166 &atio->u.isp24.fcp_cmnd.add_cdb[
3167 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3168 if (ctio24->u.status1.residual != 0)
3169 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3170
Himanshu Madhani63163e02014-09-25 06:14:59 -04003171 /* Memory Barrier */
3172 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003173 qla2x00_start_iocbs(vha, vha->req);
3174 return ret;
3175}
3176
3177static void qlt_send_term_exchange(struct scsi_qla_host *vha,
Quinn Trana07100e2015-12-07 19:48:57 -05003178 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3179 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003180{
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003181 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003182 int rc;
3183
3184 if (qlt_issue_marker(vha, ha_locked) < 0)
3185 return;
3186
3187 if (ha_locked) {
3188 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003189 if (rc == -ENOMEM)
3190 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003191 goto done;
3192 }
3193 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3194 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003195 if (rc == -ENOMEM)
3196 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003197
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003198done:
Quinn Trana07100e2015-12-07 19:48:57 -05003199 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003200 if (cmd->sg_mapped)
3201 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003202 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3203 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003204
3205 if (!ha_locked)
3206 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3207
Quinn Tran7b898542014-04-11 16:54:44 -04003208 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003209}
3210
Quinn Tran33e79972014-09-25 06:14:55 -04003211static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3212{
3213 struct list_head free_list;
3214 struct qla_tgt_cmd *cmd, *tcmd;
3215
3216 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003217 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003218
3219 cmd = tcmd = NULL;
3220 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3221 INIT_LIST_HEAD(&free_list);
3222 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3223
3224 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3225 list_del(&cmd->cmd_list);
3226 /* This cmd was never sent to TCM. There is no need
3227 * to schedule free or call free_cmd
3228 */
3229 qlt_free_cmd(cmd);
3230 vha->hw->tgt.num_qfull_cmds_alloc--;
3231 }
3232 }
3233 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3234}
3235
3236static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3237{
3238 uint32_t total_leaked;
3239
3240 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3241
3242 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3243 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3244
3245 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3246 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003247 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003248
3249 if (IS_P3P_TYPE(vha->hw))
3250 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3251 else
3252 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3253 qla2xxx_wake_dpc(vha);
3254 }
3255
3256}
3257
Quinn Trana07100e2015-12-07 19:48:57 -05003258int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003259{
3260 struct qla_tgt *tgt = cmd->tgt;
3261 struct scsi_qla_host *vha = tgt->vha;
3262 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003263 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003264
3265 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3266 "qla_target(%d): terminating exchange for aborted cmd=%p "
3267 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3268 se_cmd->tag);
3269
Quinn Trana07100e2015-12-07 19:48:57 -05003270 spin_lock_irqsave(&cmd->cmd_lock, flags);
3271 if (cmd->aborted) {
3272 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3273 /*
3274 * It's normal to see 2 calls in this path:
3275 * 1) XFER Rdy completion + CMD_T_ABORT
3276 * 2) TCM TMR - drain_state_list
3277 */
3278 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
3279 "multiple abort. %p transport_state %x, t_state %x,"
3280 " se_cmd_flags %x \n", cmd, cmd->se_cmd.transport_state,
3281 cmd->se_cmd.t_state,cmd->se_cmd.se_cmd_flags);
3282 return EIO;
3283 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003284 cmd->aborted = 1;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003285 cmd->cmd_flags |= BIT_6;
Quinn Trana07100e2015-12-07 19:48:57 -05003286 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003287
Quinn Trana07100e2015-12-07 19:48:57 -05003288 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3289 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003290}
3291EXPORT_SYMBOL(qlt_abort_cmd);
3292
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003293void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3294{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003295 struct qla_tgt_sess *sess = cmd->sess;
3296
Quinn Tranf83adb62014-04-11 16:54:43 -04003297 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3298 "%s: se_cmd[%p] ox_id %04x\n",
3299 __func__, &cmd->se_cmd,
3300 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003301
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003302 BUG_ON(cmd->cmd_in_wq);
3303
Quinn Trana07100e2015-12-07 19:48:57 -05003304 if (cmd->sg_mapped)
3305 qlt_unmap_sg(cmd->vha, cmd);
3306
Quinn Tran33e79972014-09-25 06:14:55 -04003307 if (!cmd->q_full)
3308 qlt_decr_num_pend_cmds(cmd->vha);
3309
Quinn Tranf83adb62014-04-11 16:54:43 -04003310 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003311 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003312 if (unlikely(cmd->free_sg))
3313 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003314
3315 if (!sess || !sess->se_sess) {
3316 WARN_ON(1);
3317 return;
3318 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003319 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003320 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003321}
3322EXPORT_SYMBOL(qlt_free_cmd);
3323
3324/* ha->hardware_lock supposed to be held on entry */
3325static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
3326 struct qla_tgt_cmd *cmd, void *ctio)
3327{
3328 struct qla_tgt_srr_ctio *sc;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003329 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003330 struct qla_tgt_srr_imm *imm;
3331
3332 tgt->ctio_srr_id++;
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003333 cmd->cmd_flags |= BIT_15;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003334
3335 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
3336 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
3337
3338 if (!ctio) {
3339 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
3340 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
3341 vha->vp_idx);
3342 return -EINVAL;
3343 }
3344
3345 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
3346 if (sc != NULL) {
3347 sc->cmd = cmd;
3348 /* IRQ is already OFF */
3349 spin_lock(&tgt->srr_lock);
3350 sc->srr_id = tgt->ctio_srr_id;
3351 list_add_tail(&sc->srr_list_entry,
3352 &tgt->srr_ctio_list);
3353 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
3354 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
3355 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3356 int found = 0;
3357 list_for_each_entry(imm, &tgt->srr_imm_list,
3358 srr_list_entry) {
3359 if (imm->srr_id == sc->srr_id) {
3360 found = 1;
3361 break;
3362 }
3363 }
3364 if (found) {
3365 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
3366 "Scheduling srr work\n");
3367 schedule_work(&tgt->srr_work);
3368 } else {
3369 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
3370 "qla_target(%d): imm_srr_id "
3371 "== ctio_srr_id (%d), but there is no "
3372 "corresponding SRR IMM, deleting CTIO "
3373 "SRR %p\n", vha->vp_idx,
3374 tgt->ctio_srr_id, sc);
3375 list_del(&sc->srr_list_entry);
3376 spin_unlock(&tgt->srr_lock);
3377
3378 kfree(sc);
3379 return -EINVAL;
3380 }
3381 }
3382 spin_unlock(&tgt->srr_lock);
3383 } else {
3384 struct qla_tgt_srr_imm *ti;
3385
3386 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
3387 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
3388 vha->vp_idx);
3389 spin_lock(&tgt->srr_lock);
3390 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
3391 srr_list_entry) {
3392 if (imm->srr_id == tgt->ctio_srr_id) {
3393 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
3394 "IMM SRR %p deleted (id %d)\n",
3395 imm, imm->srr_id);
3396 list_del(&imm->srr_list_entry);
3397 qlt_reject_free_srr_imm(vha, imm, 1);
3398 }
3399 }
3400 spin_unlock(&tgt->srr_lock);
3401
3402 return -ENOMEM;
3403 }
3404
3405 return 0;
3406}
3407
3408/*
3409 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3410 */
3411static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3412 struct qla_tgt_cmd *cmd, uint32_t status)
3413{
3414 int term = 0;
3415
3416 if (ctio != NULL) {
3417 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3418 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003419 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003420 } else
3421 term = 1;
3422
3423 if (term)
Quinn Trana07100e2015-12-07 19:48:57 -05003424 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003425
3426 return term;
3427}
3428
3429/* ha->hardware_lock supposed to be held on entry */
3430static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3431 uint32_t handle)
3432{
3433 struct qla_hw_data *ha = vha->hw;
3434
3435 handle--;
3436 if (ha->tgt.cmds[handle] != NULL) {
3437 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3438 ha->tgt.cmds[handle] = NULL;
3439 return cmd;
3440 } else
3441 return NULL;
3442}
3443
3444/* ha->hardware_lock supposed to be held on entry */
3445static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3446 uint32_t handle, void *ctio)
3447{
3448 struct qla_tgt_cmd *cmd = NULL;
3449
3450 /* Clear out internal marks */
3451 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3452 CTIO_INTERMEDIATE_HANDLE_MARK);
3453
3454 if (handle != QLA_TGT_NULL_HANDLE) {
Arun Easi667024a2014-09-25 06:14:47 -04003455 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003456 return NULL;
Arun Easi667024a2014-09-25 06:14:47 -04003457
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003458 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05003459 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003460 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3461 "qla_target(%d): Wrong handle %x received\n",
3462 vha->vp_idx, handle);
3463 return NULL;
3464 }
3465 cmd = qlt_get_cmd(vha, handle);
3466 if (unlikely(cmd == NULL)) {
3467 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3468 "qla_target(%d): Suspicious: unable to "
3469 "find the command with handle %x\n", vha->vp_idx,
3470 handle);
3471 return NULL;
3472 }
3473 } else if (ctio != NULL) {
3474 /* We can't get loop ID from CTIO7 */
3475 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3476 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3477 "support NULL handles\n", vha->vp_idx);
3478 return NULL;
3479 }
3480
3481 return cmd;
3482}
3483
Arun Easic0cb4492014-09-25 06:14:51 -04003484/* hardware_lock should be held by caller. */
3485static void
3486qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3487{
3488 struct qla_hw_data *ha = vha->hw;
3489 uint32_t handle;
3490
3491 if (cmd->sg_mapped)
3492 qlt_unmap_sg(vha, cmd);
3493
3494 handle = qlt_make_handle(vha);
3495
3496 /* TODO: fix debug message type and ids. */
3497 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3498 ql_dbg(ql_dbg_io, vha, 0xff00,
3499 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3500 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3501 cmd->write_data_transferred = 0;
3502 cmd->state = QLA_TGT_STATE_DATA_IN;
3503
3504 ql_dbg(ql_dbg_io, vha, 0xff01,
3505 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3506
3507 ha->tgt.tgt_ops->handle_data(cmd);
3508 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003509 } else {
3510 ql_dbg(ql_dbg_io, vha, 0xff03,
3511 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3512 cmd->state);
3513 dump_stack();
3514 }
3515
Quinn Trane5fdee82015-06-10 11:05:21 -04003516 cmd->cmd_flags |= BIT_17;
Arun Easic0cb4492014-09-25 06:14:51 -04003517 ha->tgt.tgt_ops->free_cmd(cmd);
3518}
3519
3520void
3521qlt_host_reset_handler(struct qla_hw_data *ha)
3522{
3523 struct qla_tgt_cmd *cmd;
3524 unsigned long flags;
3525 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3526 scsi_qla_host_t *vha = NULL;
3527 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3528 uint32_t i;
3529
3530 if (!base_vha->hw->tgt.tgt_ops)
3531 return;
3532
3533 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3534 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3535 "Target mode disabled\n");
3536 return;
3537 }
3538
3539 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3540 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3541 base_vha->dpc_flags);
3542
3543 spin_lock_irqsave(&ha->hardware_lock, flags);
3544 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3545 cmd = qlt_get_cmd(base_vha, i);
3546 if (!cmd)
3547 continue;
3548 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3549 vha = cmd->vha;
3550 qlt_abort_cmd_on_host_reset(vha, cmd);
3551 }
3552 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3553}
3554
3555
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003556/*
3557 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3558 */
3559static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3560 uint32_t status, void *ctio)
3561{
3562 struct qla_hw_data *ha = vha->hw;
3563 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003564 struct qla_tgt_cmd *cmd;
3565
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003566 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3567 /* That could happen only in case of an error/reset/abort */
3568 if (status != CTIO_SUCCESS) {
3569 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3570 "Intermediate CTIO received"
3571 " (status %x)\n", status);
3572 }
3573 return;
3574 }
3575
3576 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003577 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003578 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003579
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003580 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003581 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003582
Joern Engelf9b67212014-09-16 16:23:18 -04003583 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003584
3585 if (unlikely(status != CTIO_SUCCESS)) {
3586 switch (status & 0xFFFF) {
3587 case CTIO_LIP_RESET:
3588 case CTIO_TARGET_RESET:
3589 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003590 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003591 case CTIO_TIMEOUT:
3592 case CTIO_INVALID_RX_ID:
3593 /* They are OK */
3594 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3595 "qla_target(%d): CTIO with "
3596 "status %#x received, state %x, se_cmd %p, "
3597 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3598 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3599 status, cmd->state, se_cmd);
3600 break;
3601
3602 case CTIO_PORT_LOGGED_OUT:
3603 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003604 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003605 int logged_out =
3606 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3607
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003608 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003609 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003610 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003611 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003612 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003613
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003614 if (logged_out && cmd->sess) {
3615 /*
3616 * Session is already logged out, but we need
3617 * to notify initiator, who's not aware of this
3618 */
3619 cmd->sess->logout_on_delete = 0;
3620 cmd->sess->send_els_logo = 1;
3621 qlt_schedule_sess_for_deletion(cmd->sess, true);
3622 }
3623 break;
3624 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003625 case CTIO_SRR_RECEIVED:
3626 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
3627 "qla_target(%d): CTIO with SRR_RECEIVED"
3628 " status %x received (state %x, se_cmd %p)\n",
3629 vha->vp_idx, status, cmd->state, se_cmd);
3630 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
3631 break;
3632 else
3633 return;
3634
Quinn Tranf83adb62014-04-11 16:54:43 -04003635 case CTIO_DIF_ERROR: {
3636 struct ctio_crc_from_fw *crc =
3637 (struct ctio_crc_from_fw *)ctio;
3638 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3639 "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
3640 vha->vp_idx, status, cmd->state, se_cmd,
3641 *((u64 *)&crc->actual_dif[0]),
3642 *((u64 *)&crc->expected_dif[0]));
3643
3644 if (qlt_handle_dif_error(vha, cmd, ctio)) {
3645 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3646 /* scsi Write/xfer rdy complete */
3647 goto skip_term;
3648 } else {
3649 /* scsi read/xmit respond complete
3650 * call handle dif to send scsi status
3651 * rather than terminate exchange.
3652 */
3653 cmd->state = QLA_TGT_STATE_PROCESSED;
3654 ha->tgt.tgt_ops->handle_dif_err(cmd);
3655 return;
3656 }
3657 } else {
3658 /* Need to generate a SCSI good completion.
3659 * because FW did not send scsi status.
3660 */
3661 status = 0;
3662 goto skip_term;
3663 }
3664 break;
3665 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003666 default:
3667 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003668 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003669 vha->vp_idx, status, cmd->state, se_cmd);
3670 break;
3671 }
3672
Quinn Tran7b898542014-04-11 16:54:44 -04003673
Quinn Tran193b50b2015-12-17 14:57:03 -05003674 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003675 * cmd is already aborted/terminated, we don't
3676 * need to terminate again. The exchange is already
3677 * cleaned up/freed at FW level. Just cleanup at driver
3678 * level.
3679 */
3680 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003681 (!cmd->aborted)) {
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003682 cmd->cmd_flags |= BIT_13;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003683 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3684 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003685 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003686 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003687skip_term:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003688
3689 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Trane5fdee82015-06-10 11:05:21 -04003690 cmd->cmd_flags |= BIT_12;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003691 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003692 cmd->state = QLA_TGT_STATE_DATA_IN;
3693
Bart Van Assche52c82822015-07-09 07:23:26 -07003694 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003695 cmd->write_data_transferred = 1;
3696
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003697 ha->tgt.tgt_ops->handle_data(cmd);
3698 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003699 } else if (cmd->aborted) {
Quinn Trane5fdee82015-06-10 11:05:21 -04003700 cmd->cmd_flags |= BIT_18;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003701 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003702 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003703 } else {
Quinn Trane5fdee82015-06-10 11:05:21 -04003704 cmd->cmd_flags |= BIT_19;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003705 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3706 "qla_target(%d): A command in state (%d) should "
3707 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3708 }
3709
Quinn Tran7b898542014-04-11 16:54:44 -04003710 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003711 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003712 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3713 dump_stack();
3714 }
3715
3716 ha->tgt.tgt_ops->free_cmd(cmd);
3717}
3718
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003719static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3720 uint8_t task_codes)
3721{
3722 int fcp_task_attr;
3723
3724 switch (task_codes) {
3725 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003726 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003727 break;
3728 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003729 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003730 break;
3731 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003732 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003733 break;
3734 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003735 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003736 break;
3737 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003738 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003739 break;
3740 default:
3741 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3742 "qla_target: unknown task code %x, use ORDERED instead\n",
3743 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003744 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003745 break;
3746 }
3747
3748 return fcp_task_attr;
3749}
3750
3751static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
3752 uint8_t *);
3753/*
3754 * Process context for I/O path into tcm_qla2xxx code
3755 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003756static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003757{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003758 scsi_qla_host_t *vha = cmd->vha;
3759 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003760 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003761 struct qla_tgt_sess *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003762 struct atio_from_isp *atio = &cmd->atio;
3763 unsigned char *cdb;
3764 unsigned long flags;
3765 uint32_t data_length;
3766 int ret, fcp_task_attr, data_dir, bidi = 0;
3767
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003768 cmd->cmd_in_wq = 0;
3769 cmd->cmd_flags |= BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003770 if (tgt->tgt_stop)
3771 goto out_term;
3772
Quinn Tran193b50b2015-12-17 14:57:03 -05003773 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003774 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3775 "cmd with tag %u is aborted\n",
3776 cmd->atio.u.isp24.exchange_addr);
3777 goto out_term;
3778 }
3779
Quinn Trana07100e2015-12-07 19:48:57 -05003780 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003781 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02003782 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003783 cmd->unpacked_lun = scsilun_to_int(
3784 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3785
3786 if (atio->u.isp24.fcp_cmnd.rddata &&
3787 atio->u.isp24.fcp_cmnd.wrdata) {
3788 bidi = 1;
3789 data_dir = DMA_TO_DEVICE;
3790 } else if (atio->u.isp24.fcp_cmnd.rddata)
3791 data_dir = DMA_FROM_DEVICE;
3792 else if (atio->u.isp24.fcp_cmnd.wrdata)
3793 data_dir = DMA_TO_DEVICE;
3794 else
3795 data_dir = DMA_NONE;
3796
3797 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3798 atio->u.isp24.fcp_cmnd.task_attr);
3799 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3800 &atio->u.isp24.fcp_cmnd.add_cdb[
3801 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3802
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003803 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3804 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003805 if (ret != 0)
3806 goto out_term;
3807 /*
3808 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3809 */
Quinn Tran75601512015-12-17 14:57:04 -05003810 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003811 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05003812 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003813 return;
3814
3815out_term:
Arun Easi667024a2014-09-25 06:14:47 -04003816 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003817 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07003818 * cmd has not sent to target yet, so pass NULL as the second
3819 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003820 */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003821 cmd->cmd_flags |= BIT_2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003822 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003823 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04003824
3825 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003826 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Jörn Engel08234e32013-06-12 16:27:54 -04003827 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tran75601512015-12-17 14:57:04 -05003828
3829 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
3830 ha->tgt.tgt_ops->put_sess(sess);
3831 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003832}
3833
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003834static void qlt_do_work(struct work_struct *work)
3835{
3836 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003837 scsi_qla_host_t *vha = cmd->vha;
3838 unsigned long flags;
3839
3840 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3841 list_del(&cmd->cmd_list);
3842 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003843
3844 __qlt_do_work(cmd);
3845}
3846
3847static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
3848 struct qla_tgt_sess *sess,
3849 struct atio_from_isp *atio)
3850{
3851 struct se_session *se_sess = sess->se_sess;
3852 struct qla_tgt_cmd *cmd;
3853 int tag;
3854
3855 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
3856 if (tag < 0)
3857 return NULL;
3858
3859 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
3860 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
3861
3862 memcpy(&cmd->atio, atio, sizeof(*atio));
3863 cmd->state = QLA_TGT_STATE_NEW;
3864 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04003865 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003866 cmd->vha = vha;
3867 cmd->se_cmd.map_tag = tag;
3868 cmd->sess = sess;
3869 cmd->loop_id = sess->loop_id;
3870 cmd->conf_compl_supported = sess->conf_compl_supported;
3871
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04003872 cmd->cmd_flags = 0;
3873 cmd->jiffies_at_alloc = get_jiffies_64();
3874
3875 cmd->reset_count = vha->hw->chip_reset;
3876
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003877 return cmd;
3878}
3879
3880static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
3881 uint16_t);
3882
3883static void qlt_create_sess_from_atio(struct work_struct *work)
3884{
3885 struct qla_tgt_sess_op *op = container_of(work,
3886 struct qla_tgt_sess_op, work);
3887 scsi_qla_host_t *vha = op->vha;
3888 struct qla_hw_data *ha = vha->hw;
3889 struct qla_tgt_sess *sess;
3890 struct qla_tgt_cmd *cmd;
3891 unsigned long flags;
3892 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
3893
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003894 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3895 list_del(&op->cmd_list);
3896 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
3897
3898 if (op->aborted) {
3899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
3900 "sess_op with tag %u is aborted\n",
3901 op->atio.u.isp24.exchange_addr);
3902 goto out_term;
3903 }
3904
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003905 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003906 "qla_target(%d): Unable to find wwn login"
3907 " (s_id %x:%x:%x), trying to create it manually\n",
3908 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003909
3910 if (op->atio.u.raw.entry_count > 1) {
3911 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003912 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003913 goto out_term;
3914 }
3915
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003916 sess = qlt_make_local_sess(vha, s_id);
3917 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003918
3919 if (!sess)
3920 goto out_term;
3921 /*
3922 * Now obtain a pre-allocated session tag using the original op->atio
3923 * packet header, and dispatch into __qlt_do_work() using the existing
3924 * process context.
3925 */
3926 cmd = qlt_get_tag(vha, sess, &op->atio);
3927 if (!cmd) {
3928 spin_lock_irqsave(&ha->hardware_lock, flags);
3929 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
3930 ha->tgt.tgt_ops->put_sess(sess);
3931 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3932 kfree(op);
3933 return;
3934 }
3935 /*
3936 * __qlt_do_work() will call ha->tgt.tgt_ops->put_sess() to release
3937 * the extra reference taken above by qlt_make_local_sess()
3938 */
3939 __qlt_do_work(cmd);
3940 kfree(op);
3941 return;
3942
3943out_term:
3944 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003945 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003946 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3947 kfree(op);
3948
3949}
3950
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003951/* ha->hardware_lock supposed to be held on entry */
3952static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
3953 struct atio_from_isp *atio)
3954{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003955 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003956 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003957 struct qla_tgt_sess *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003958 struct qla_tgt_cmd *cmd;
3959
3960 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04003961 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003962 "New command while device %p is shutting down\n", tgt);
3963 return -EFAULT;
3964 }
3965
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003966 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
3967 if (unlikely(!sess)) {
3968 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
3969 GFP_ATOMIC);
3970 if (!op)
3971 return -ENOMEM;
3972
3973 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04003974 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003975
3976 spin_lock(&vha->cmd_list_lock);
3977 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
3978 spin_unlock(&vha->cmd_list_lock);
3979
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003980 INIT_WORK(&op->work, qlt_create_sess_from_atio);
3981 queue_work(qla_tgt_wq, &op->work);
3982 return 0;
3983 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04003984
3985 /* Another WWN used to have our s_id. Our PLOGI scheduled its
3986 * session deletion, but it's still in sess_del_work wq */
3987 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
3988 ql_dbg(ql_dbg_io, vha, 0x3061,
3989 "New command while old session %p is being deleted\n",
3990 sess);
3991 return -EFAULT;
3992 }
3993
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003994 /*
3995 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
3996 */
3997 kref_get(&sess->se_sess->sess_kref);
3998
3999 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004000 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004001 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004002 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004003 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004004 return -ENOMEM;
4005 }
4006
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004007 cmd->cmd_in_wq = 1;
4008 cmd->cmd_flags |= BIT_0;
Quinn Tran5327c7d2016-02-10 18:59:14 -05004009 cmd->se_cmd.cpuid = ha->msix_count ?
4010 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004011
4012 spin_lock(&vha->cmd_list_lock);
4013 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4014 spin_unlock(&vha->cmd_list_lock);
4015
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004016 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Tranfb3269b2015-12-17 14:57:06 -05004017 if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004018 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4019 queue_work_on(smp_processor_id(), qla_tgt_wq,
4020 &cmd->work);
4021 else
4022 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4023 &cmd->work);
4024 } else {
4025 queue_work(qla_tgt_wq, &cmd->work);
4026 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004027 return 0;
4028
4029}
4030
4031/* ha->hardware_lock supposed to be held on entry */
4032static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
4033 int fn, void *iocb, int flags)
4034{
4035 struct scsi_qla_host *vha = sess->vha;
4036 struct qla_hw_data *ha = vha->hw;
4037 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004038 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004039 int res;
4040 uint8_t tmr_func;
4041
4042 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4043 if (!mcmd) {
4044 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4045 "qla_target(%d): Allocation of management "
4046 "command failed, some commands and their data could "
4047 "leak\n", vha->vp_idx);
4048 return -ENOMEM;
4049 }
4050 memset(mcmd, 0, sizeof(*mcmd));
4051 mcmd->sess = sess;
4052
4053 if (iocb) {
4054 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4055 sizeof(mcmd->orig_iocb.imm_ntfy));
4056 }
4057 mcmd->tmr_func = fn;
4058 mcmd->flags = flags;
Arun Easib6a029e2014-09-25 06:14:52 -04004059 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004060
4061 switch (fn) {
4062 case QLA_TGT_CLEAR_ACA:
4063 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
4064 "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
4065 tmr_func = TMR_CLEAR_ACA;
4066 break;
4067
4068 case QLA_TGT_TARGET_RESET:
4069 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
4070 "qla_target(%d): TARGET_RESET received\n",
4071 sess->vha->vp_idx);
4072 tmr_func = TMR_TARGET_WARM_RESET;
4073 break;
4074
4075 case QLA_TGT_LUN_RESET:
4076 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
4077 "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
4078 tmr_func = TMR_LUN_RESET;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004079 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004080 break;
4081
4082 case QLA_TGT_CLEAR_TS:
4083 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
4084 "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
4085 tmr_func = TMR_CLEAR_TASK_SET;
4086 break;
4087
4088 case QLA_TGT_ABORT_TS:
4089 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
4090 "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
4091 tmr_func = TMR_ABORT_TASK_SET;
4092 break;
4093#if 0
4094 case QLA_TGT_ABORT_ALL:
4095 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
4096 "qla_target(%d): Doing ABORT_ALL_TASKS\n",
4097 sess->vha->vp_idx);
4098 tmr_func = 0;
4099 break;
4100
4101 case QLA_TGT_ABORT_ALL_SESS:
4102 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
4103 "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
4104 sess->vha->vp_idx);
4105 tmr_func = 0;
4106 break;
4107
4108 case QLA_TGT_NEXUS_LOSS_SESS:
4109 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
4110 "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
4111 sess->vha->vp_idx);
4112 tmr_func = 0;
4113 break;
4114
4115 case QLA_TGT_NEXUS_LOSS:
4116 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
4117 "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
4118 tmr_func = 0;
4119 break;
4120#endif
4121 default:
4122 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
4123 "qla_target(%d): Unknown task mgmt fn 0x%x\n",
4124 sess->vha->vp_idx, fn);
4125 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4126 return -ENOSYS;
4127 }
4128
4129 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
4130 if (res != 0) {
4131 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4132 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4133 sess->vha->vp_idx, res);
4134 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4135 return -EFAULT;
4136 }
4137
4138 return 0;
4139}
4140
4141/* ha->hardware_lock supposed to be held on entry */
4142static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4143{
4144 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4145 struct qla_hw_data *ha = vha->hw;
4146 struct qla_tgt *tgt;
4147 struct qla_tgt_sess *sess;
4148 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004149 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004150 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004151
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004152 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004153
4154 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004155 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004156
4157 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004158 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4159 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004160 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4161
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004162 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4163
4164 if (!sess) {
4165 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4166 "qla_target(%d): task mgmt fn 0x%x for "
4167 "non-existant session\n", vha->vp_idx, fn);
4168 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4169 sizeof(struct atio_from_isp));
4170 }
4171
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004172 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)
4173 return -EFAULT;
4174
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004175 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4176}
4177
4178/* ha->hardware_lock supposed to be held on entry */
4179static int __qlt_abort_task(struct scsi_qla_host *vha,
4180 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
4181{
4182 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4183 struct qla_hw_data *ha = vha->hw;
4184 struct qla_tgt_mgmt_cmd *mcmd;
4185 uint32_t lun, unpacked_lun;
4186 int rc;
4187
4188 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4189 if (mcmd == NULL) {
4190 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4191 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4192 vha->vp_idx, __func__);
4193 return -ENOMEM;
4194 }
4195 memset(mcmd, 0, sizeof(*mcmd));
4196
4197 mcmd->sess = sess;
4198 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4199 sizeof(mcmd->orig_iocb.imm_ntfy));
4200
4201 lun = a->u.isp24.fcp_cmnd.lun;
4202 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
Arun Easi80187f82014-09-25 06:14:53 -04004203 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004204
4205 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
4206 le16_to_cpu(iocb->u.isp2x.seq_id));
4207 if (rc != 0) {
4208 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4209 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4210 vha->vp_idx, rc);
4211 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4212 return -EFAULT;
4213 }
4214
4215 return 0;
4216}
4217
4218/* ha->hardware_lock supposed to be held on entry */
4219static int qlt_abort_task(struct scsi_qla_host *vha,
4220 struct imm_ntfy_from_isp *iocb)
4221{
4222 struct qla_hw_data *ha = vha->hw;
4223 struct qla_tgt_sess *sess;
4224 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004225 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004226
4227 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4228
Quinn Tran75601512015-12-17 14:57:04 -05004229 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004230 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004231 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4232
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004233 if (sess == NULL) {
4234 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4235 "qla_target(%d): task abort for unexisting "
4236 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004237 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004238 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4239 }
4240
4241 return __qlt_abort_task(vha, iocb, sess);
4242}
4243
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004244void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4245{
4246 if (fcport->tgt_session) {
4247 if (rc != MBS_COMMAND_COMPLETE) {
Alexei Potashnikdf673272015-07-14 16:00:46 -04004248 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004249 "%s: se_sess %p / sess %p from"
4250 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4251 " LOGO failed: %#x\n",
4252 __func__,
4253 fcport->tgt_session->se_sess,
4254 fcport->tgt_session,
4255 fcport->port_name, fcport->loop_id,
4256 fcport->d_id.b.domain, fcport->d_id.b.area,
4257 fcport->d_id.b.al_pa, rc);
4258 }
4259
4260 fcport->tgt_session->logout_completed = 1;
4261 }
4262}
4263
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004264/*
4265* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4266*
4267* Schedules sessions with matching port_id/loop_id but different wwn for
4268* deletion. Returns existing session with matching wwn if present.
4269* Null otherwise.
4270*/
4271static struct qla_tgt_sess *
4272qlt_find_sess_invalidate_other(struct qla_tgt *tgt, uint64_t wwn,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004273 port_id_t port_id, uint16_t loop_id, struct qla_tgt_sess **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004274{
4275 struct qla_tgt_sess *sess = NULL, *other_sess;
4276 uint64_t other_wwn;
4277
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004278 *conflict_sess = NULL;
4279
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004280 list_for_each_entry(other_sess, &tgt->sess_list, sess_list_entry) {
4281
4282 other_wwn = wwn_to_u64(other_sess->port_name);
4283
4284 if (wwn == other_wwn) {
4285 WARN_ON(sess);
4286 sess = other_sess;
4287 continue;
4288 }
4289
4290 /* find other sess with nport_id collision */
4291 if (port_id.b24 == other_sess->s_id.b24) {
4292 if (loop_id != other_sess->loop_id) {
4293 ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000c,
4294 "Invalidating sess %p loop_id %d wwn %llx.\n",
4295 other_sess, other_sess->loop_id, other_wwn);
4296
4297 /*
4298 * logout_on_delete is set by default, but another
4299 * session that has the same s_id/loop_id combo
4300 * might have cleared it when requested this session
4301 * deletion, so don't touch it
4302 */
4303 qlt_schedule_sess_for_deletion(other_sess, true);
4304 } else {
4305 /*
4306 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004307 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004308 */
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004309 other_sess->keep_nport_handle = 1;
4310 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004311 qlt_schedule_sess_for_deletion(other_sess,
4312 true);
4313 }
4314 continue;
4315 }
4316
4317 /* find other sess with nport handle collision */
4318 if (loop_id == other_sess->loop_id) {
4319 ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000d,
4320 "Invalidating sess %p loop_id %d wwn %llx.\n",
4321 other_sess, other_sess->loop_id, other_wwn);
4322
4323 /* Same loop_id but different s_id
4324 * Ok to kill and logout */
4325 qlt_schedule_sess_for_deletion(other_sess, true);
4326 }
4327 }
4328
4329 return sess;
4330}
4331
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004332/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4333static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4334{
4335 struct qla_tgt_sess_op *op;
4336 struct qla_tgt_cmd *cmd;
4337 uint32_t key;
4338 int count = 0;
4339
4340 key = (((u32)s_id->b.domain << 16) |
4341 ((u32)s_id->b.area << 8) |
4342 ((u32)s_id->b.al_pa));
4343
4344 spin_lock(&vha->cmd_list_lock);
4345 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4346 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4347 if (op_key == key) {
4348 op->aborted = true;
4349 count++;
4350 }
4351 }
4352 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4353 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4354 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004355 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004356 count++;
4357 }
4358 }
4359 spin_unlock(&vha->cmd_list_lock);
4360
4361 return count;
4362}
4363
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004364/*
4365 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4366 */
4367static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4368 struct imm_ntfy_from_isp *iocb)
4369{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004370 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004371 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004372 struct qla_tgt_sess *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004373 uint64_t wwn;
4374 port_id_t port_id;
4375 uint16_t loop_id;
4376 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004377 int res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004378 qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004379 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004380
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004381 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4382
4383 port_id.b.domain = iocb->u.isp24.port_id[2];
4384 port_id.b.area = iocb->u.isp24.port_id[1];
4385 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4386 port_id.b.rsvd_1 = 0;
4387
4388 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4389
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004390 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004391 "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
4392 vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004393
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004394 /* res = 1 means ack at the end of thread
4395 * res = 0 means ack async/later.
4396 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004397 switch (iocb->u.isp24.status_subcode) {
4398 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004399
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004400 /* Mark all stale commands in qla_tgt_wq for deletion */
4401 abort_cmds_for_s_id(vha, &port_id);
4402
Quinn Tran75601512015-12-17 14:57:04 -05004403 if (wwn) {
4404 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004405 sess = qlt_find_sess_invalidate_other(tgt, wwn,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004406 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004407 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4408 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004409
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004410 if (IS_SW_RESV_ADDR(port_id) || (!sess && !conflict_sess)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004411 res = 1;
4412 break;
4413 }
4414
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004415 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4416 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004417 qlt_send_term_imm_notif(vha, iocb, 1);
4418
4419 res = 0;
4420 break;
4421 }
4422
4423 res = 0;
4424
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004425 if (conflict_sess)
4426 qlt_plogi_ack_link(vha, pla, conflict_sess,
4427 QLT_PLOGI_LINK_CONFLICT);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004428
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004429 if (!sess)
4430 break;
4431
4432 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004433 /*
4434 * Under normal circumstances we want to release nport handle
4435 * during LOGO process to avoid nport handle leaks inside FW.
4436 * The exception is when LOGO is done while another PLOGI with
4437 * the same nport handle is waiting as might be the case here.
4438 * Note: there is always a possibily of a race where session
4439 * deletion has already started for other reasons (e.g. ACL
4440 * removal) and now PLOGI arrives:
4441 * 1. if PLOGI arrived in FW after nport handle has been freed,
4442 * FW must have assigned this PLOGI a new/same handle and we
4443 * can proceed ACK'ing it as usual when session deletion
4444 * completes.
4445 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4446 * bit reached it, the handle has now been released. We'll
4447 * get an error when we ACK this PLOGI. Nothing will be sent
4448 * back to initiator. Initiator should eventually retry
4449 * PLOGI and situation will correct itself.
4450 */
4451 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4452 (sess->s_id.b24 == port_id.b24));
4453 qlt_schedule_sess_for_deletion(sess, true);
4454 break;
4455
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004456 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004457 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4458
Quinn Tran75601512015-12-17 14:57:04 -05004459 if (wwn) {
4460 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004461 sess = qlt_find_sess_invalidate_other(tgt, wwn, port_id,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004462 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004463 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4464 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004465
4466 if (conflict_sess) {
4467 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4468 "PRLI with conflicting sess %p port %8phC\n",
4469 conflict_sess, conflict_sess->port_name);
4470 qlt_send_term_imm_notif(vha, iocb, 1);
4471 res = 0;
4472 break;
4473 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004474
4475 if (sess != NULL) {
4476 if (sess->deleted) {
4477 /*
4478 * Impatient initiator sent PRLI before last
4479 * PLOGI could finish. Will force him to re-try,
4480 * while last one finishes.
4481 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004482 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004483 "sess %p PRLI received, before plogi ack.\n",
4484 sess);
4485 qlt_send_term_imm_notif(vha, iocb, 1);
4486 res = 0;
4487 break;
4488 }
4489
4490 /*
4491 * This shouldn't happen under normal circumstances,
4492 * since we have deleted the old session during PLOGI
4493 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004494 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004495 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4496 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4497
4498 sess->local = 0;
4499 sess->loop_id = loop_id;
4500 sess->s_id = port_id;
4501
4502 if (wd3_lo & BIT_7)
4503 sess->conf_compl_supported = 1;
4504
Alexei Potashnikdf673272015-07-14 16:00:46 -04004505 }
4506 res = 1; /* send notify ack */
4507
4508 /* Make session global (not used in fabric mode) */
4509 if (ha->current_topology != ISP_CFG_F) {
4510 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4511 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4512 qla2xxx_wake_dpc(vha);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004513 } else {
4514 /* todo: else - create sess here. */
4515 res = 1; /* send notify ack */
4516 }
4517
4518 break;
4519
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004520 case ELS_LOGO:
4521 case ELS_PRLO:
4522 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4523 break;
4524 case ELS_PDISC:
4525 case ELS_ADISC:
4526 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004527 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004528 if (tgt->link_reinit_iocb_pending) {
4529 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4530 0, 0, 0, 0, 0, 0);
4531 tgt->link_reinit_iocb_pending = 0;
4532 }
4533 res = 1; /* send notify ack */
4534 break;
4535 }
4536
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004537 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004538 default:
4539 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4540 "qla_target(%d): Unsupported ELS command %x "
4541 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4542 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4543 break;
4544 }
4545
4546 return res;
4547}
4548
4549static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
4550{
Bart Van Assche5897cb22015-06-04 15:57:20 -07004551#if 1
4552 /*
4553 * FIXME: Reject non zero SRR relative offset until we can test
4554 * this code properly.
4555 */
4556 pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
4557 return -1;
4558#else
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004559 struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
4560 size_t first_offset = 0, rem_offset = offset, tmp = 0;
4561 int i, sg_srr_cnt, bufflen = 0;
4562
4563 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
4564 "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
4565 "cmd->sg_cnt: %u, direction: %d\n",
4566 cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
4567
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004568 if (!cmd->sg || !cmd->sg_cnt) {
4569 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
4570 "Missing cmd->sg or zero cmd->sg_cnt in"
4571 " qla_tgt_set_data_offset\n");
4572 return -EINVAL;
4573 }
4574 /*
4575 * Walk the current cmd->sg list until we locate the new sg_srr_start
4576 */
4577 for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
4578 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
4579 "sg[%d]: %p page: %p, length: %d, offset: %d\n",
4580 i, sg, sg_page(sg), sg->length, sg->offset);
4581
4582 if ((sg->length + tmp) > offset) {
4583 first_offset = rem_offset;
4584 sg_srr_start = sg;
4585 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
4586 "Found matching sg[%d], using %p as sg_srr_start, "
4587 "and using first_offset: %zu\n", i, sg,
4588 first_offset);
4589 break;
4590 }
4591 tmp += sg->length;
4592 rem_offset -= sg->length;
4593 }
4594
4595 if (!sg_srr_start) {
4596 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
4597 "Unable to locate sg_srr_start for offset: %u\n", offset);
4598 return -EINVAL;
4599 }
4600 sg_srr_cnt = (cmd->sg_cnt - i);
4601
4602 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
4603 if (!sg_srr) {
4604 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
4605 "Unable to allocate sgp\n");
4606 return -ENOMEM;
4607 }
4608 sg_init_table(sg_srr, sg_srr_cnt);
4609 sgp = &sg_srr[0];
4610 /*
4611 * Walk the remaining list for sg_srr_start, mapping to the newly
4612 * allocated sg_srr taking first_offset into account.
4613 */
4614 for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
4615 if (first_offset) {
4616 sg_set_page(sgp, sg_page(sg),
4617 (sg->length - first_offset), first_offset);
4618 first_offset = 0;
4619 } else {
4620 sg_set_page(sgp, sg_page(sg), sg->length, 0);
4621 }
4622 bufflen += sgp->length;
4623
4624 sgp = sg_next(sgp);
4625 if (!sgp)
4626 break;
4627 }
4628
4629 cmd->sg = sg_srr;
4630 cmd->sg_cnt = sg_srr_cnt;
4631 cmd->bufflen = bufflen;
4632 cmd->offset += offset;
4633 cmd->free_sg = 1;
4634
4635 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
4636 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
4637 cmd->sg_cnt);
4638 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
4639 cmd->bufflen);
4640 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
4641 cmd->offset);
4642
4643 if (cmd->sg_cnt < 0)
4644 BUG();
4645
4646 if (cmd->bufflen < 0)
4647 BUG();
4648
4649 return 0;
Bart Van Assche5897cb22015-06-04 15:57:20 -07004650#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004651}
4652
4653static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
4654 uint32_t srr_rel_offs, int *xmit_type)
4655{
4656 int res = 0, rel_offs;
4657
4658 rel_offs = srr_rel_offs - cmd->offset;
4659 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
4660 srr_rel_offs, rel_offs);
4661
4662 *xmit_type = QLA_TGT_XMIT_ALL;
4663
4664 if (rel_offs < 0) {
4665 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
4666 "qla_target(%d): SRR rel_offs (%d) < 0",
4667 cmd->vha->vp_idx, rel_offs);
4668 res = -1;
4669 } else if (rel_offs == cmd->bufflen)
4670 *xmit_type = QLA_TGT_XMIT_STATUS;
4671 else if (rel_offs > 0)
4672 res = qlt_set_data_offset(cmd, rel_offs);
4673
4674 return res;
4675}
4676
4677/* No locks, thread context */
4678static void qlt_handle_srr(struct scsi_qla_host *vha,
4679 struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
4680{
4681 struct imm_ntfy_from_isp *ntfy =
4682 (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
4683 struct qla_hw_data *ha = vha->hw;
4684 struct qla_tgt_cmd *cmd = sctio->cmd;
4685 struct se_cmd *se_cmd = &cmd->se_cmd;
4686 unsigned long flags;
4687 int xmit_type = 0, resp = 0;
4688 uint32_t offset;
4689 uint16_t srr_ui;
4690
4691 offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
4692 srr_ui = ntfy->u.isp24.srr_ui;
4693
4694 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
4695 cmd, srr_ui);
4696
4697 switch (srr_ui) {
4698 case SRR_IU_STATUS:
4699 spin_lock_irqsave(&ha->hardware_lock, flags);
4700 qlt_send_notify_ack(vha, ntfy,
4701 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4702 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4703 xmit_type = QLA_TGT_XMIT_STATUS;
4704 resp = 1;
4705 break;
4706 case SRR_IU_DATA_IN:
4707 if (!cmd->sg || !cmd->sg_cnt) {
4708 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
4709 "Unable to process SRR_IU_DATA_IN due to"
4710 " missing cmd->sg, state: %d\n", cmd->state);
4711 dump_stack();
4712 goto out_reject;
4713 }
4714 if (se_cmd->scsi_status != 0) {
4715 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
4716 "Rejecting SRR_IU_DATA_IN with non GOOD "
4717 "scsi_status\n");
4718 goto out_reject;
4719 }
4720 cmd->bufflen = se_cmd->data_length;
4721
4722 if (qlt_has_data(cmd)) {
4723 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
4724 goto out_reject;
4725 spin_lock_irqsave(&ha->hardware_lock, flags);
4726 qlt_send_notify_ack(vha, ntfy,
4727 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4728 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4729 resp = 1;
4730 } else {
4731 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
Bart Van Assche649ee052015-04-14 13:26:44 +02004732 "qla_target(%d): SRR for in data for cmd without them (tag %lld, SCSI status %d), reject",
4733 vha->vp_idx, se_cmd->tag,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004734 cmd->se_cmd.scsi_status);
4735 goto out_reject;
4736 }
4737 break;
4738 case SRR_IU_DATA_OUT:
4739 if (!cmd->sg || !cmd->sg_cnt) {
4740 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
4741 "Unable to process SRR_IU_DATA_OUT due to"
4742 " missing cmd->sg\n");
4743 dump_stack();
4744 goto out_reject;
4745 }
4746 if (se_cmd->scsi_status != 0) {
4747 ql_dbg(ql_dbg_tgt, vha, 0xe02b,
4748 "Rejecting SRR_IU_DATA_OUT"
4749 " with non GOOD scsi_status\n");
4750 goto out_reject;
4751 }
4752 cmd->bufflen = se_cmd->data_length;
4753
4754 if (qlt_has_data(cmd)) {
4755 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
4756 goto out_reject;
4757 spin_lock_irqsave(&ha->hardware_lock, flags);
4758 qlt_send_notify_ack(vha, ntfy,
4759 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4760 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004761 if (xmit_type & QLA_TGT_XMIT_DATA) {
4762 cmd->cmd_flags |= BIT_8;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004763 qlt_rdy_to_xfer(cmd);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004764 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004765 } else {
4766 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
Bart Van Assche649ee052015-04-14 13:26:44 +02004767 "qla_target(%d): SRR for out data for cmd without them (tag %lld, SCSI status %d), reject",
4768 vha->vp_idx, se_cmd->tag, cmd->se_cmd.scsi_status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004769 goto out_reject;
4770 }
4771 break;
4772 default:
4773 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
4774 "qla_target(%d): Unknown srr_ui value %x",
4775 vha->vp_idx, srr_ui);
4776 goto out_reject;
4777 }
4778
4779 /* Transmit response in case of status and data-in cases */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004780 if (resp) {
4781 cmd->cmd_flags |= BIT_7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004782 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004783 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004784
4785 return;
4786
4787out_reject:
4788 spin_lock_irqsave(&ha->hardware_lock, flags);
4789 qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
4790 NOTIFY_ACK_SRR_FLAGS_REJECT,
4791 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4792 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4793 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4794 cmd->state = QLA_TGT_STATE_DATA_IN;
4795 dump_stack();
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004796 } else {
4797 cmd->cmd_flags |= BIT_9;
Quinn Trana07100e2015-12-07 19:48:57 -05004798 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004799 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004800 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4801}
4802
4803static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
4804 struct qla_tgt_srr_imm *imm, int ha_locked)
4805{
4806 struct qla_hw_data *ha = vha->hw;
4807 unsigned long flags = 0;
4808
Bart Van Assche8d163662015-07-09 07:25:46 -07004809#ifndef __CHECKER__
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004810 if (!ha_locked)
4811 spin_lock_irqsave(&ha->hardware_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -07004812#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004813
4814 qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
4815 NOTIFY_ACK_SRR_FLAGS_REJECT,
4816 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4817 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4818
Bart Van Assche8d163662015-07-09 07:25:46 -07004819#ifndef __CHECKER__
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004820 if (!ha_locked)
4821 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -07004822#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004823
4824 kfree(imm);
4825}
4826
4827static void qlt_handle_srr_work(struct work_struct *work)
4828{
4829 struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
4830 struct scsi_qla_host *vha = tgt->vha;
4831 struct qla_tgt_srr_ctio *sctio;
4832 unsigned long flags;
4833
4834 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
4835 tgt);
4836
4837restart:
4838 spin_lock_irqsave(&tgt->srr_lock, flags);
4839 list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
4840 struct qla_tgt_srr_imm *imm, *i, *ti;
4841 struct qla_tgt_cmd *cmd;
4842 struct se_cmd *se_cmd;
4843
4844 imm = NULL;
4845 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
4846 srr_list_entry) {
4847 if (i->srr_id == sctio->srr_id) {
4848 list_del(&i->srr_list_entry);
4849 if (imm) {
4850 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
4851 "qla_target(%d): There must be "
4852 "only one IMM SRR per CTIO SRR "
4853 "(IMM SRR %p, id %d, CTIO %p\n",
4854 vha->vp_idx, i, i->srr_id, sctio);
4855 qlt_reject_free_srr_imm(tgt->vha, i, 0);
4856 } else
4857 imm = i;
4858 }
4859 }
4860
4861 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
4862 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
4863 sctio->srr_id);
4864
4865 if (imm == NULL) {
4866 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
4867 "Not found matching IMM for SRR CTIO (id %d)\n",
4868 sctio->srr_id);
4869 continue;
4870 } else
4871 list_del(&sctio->srr_list_entry);
4872
4873 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4874
4875 cmd = sctio->cmd;
4876 /*
4877 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
4878 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
4879 * logic..
4880 */
4881 cmd->offset = 0;
4882 if (cmd->free_sg) {
4883 kfree(cmd->sg);
4884 cmd->sg = NULL;
4885 cmd->free_sg = 0;
4886 }
4887 se_cmd = &cmd->se_cmd;
4888
4889 cmd->sg_cnt = se_cmd->t_data_nents;
4890 cmd->sg = se_cmd->t_data_sg;
4891
4892 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
Bart Van Assche649ee052015-04-14 13:26:44 +02004893 "SRR cmd %p (se_cmd %p, tag %lld, op %x), sg_cnt=%d, offset=%d",
4894 cmd, &cmd->se_cmd, se_cmd->tag, se_cmd->t_task_cdb ?
4895 se_cmd->t_task_cdb[0] : 0, cmd->sg_cnt, cmd->offset);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004896
4897 qlt_handle_srr(vha, sctio, imm);
4898
4899 kfree(imm);
4900 kfree(sctio);
4901 goto restart;
4902 }
4903 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4904}
4905
4906/* ha->hardware_lock supposed to be held on entry */
4907static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
4908 struct imm_ntfy_from_isp *iocb)
4909{
4910 struct qla_tgt_srr_imm *imm;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004911 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004912 struct qla_tgt_srr_ctio *sctio;
4913
4914 tgt->imm_srr_id++;
4915
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004916 ql_log(ql_log_warn, vha, 0xf02d, "qla_target(%d): SRR received\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004917 vha->vp_idx);
4918
4919 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
4920 if (imm != NULL) {
4921 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
4922
4923 /* IRQ is already OFF */
4924 spin_lock(&tgt->srr_lock);
4925 imm->srr_id = tgt->imm_srr_id;
4926 list_add_tail(&imm->srr_list_entry,
4927 &tgt->srr_imm_list);
4928 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
4929 "IMM NTFY SRR %p added (id %d, ui %x)\n",
4930 imm, imm->srr_id, iocb->u.isp24.srr_ui);
4931 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
4932 int found = 0;
4933 list_for_each_entry(sctio, &tgt->srr_ctio_list,
4934 srr_list_entry) {
4935 if (sctio->srr_id == imm->srr_id) {
4936 found = 1;
4937 break;
4938 }
4939 }
4940 if (found) {
4941 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
4942 "Scheduling srr work\n");
4943 schedule_work(&tgt->srr_work);
4944 } else {
4945 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
4946 "qla_target(%d): imm_srr_id "
4947 "== ctio_srr_id (%d), but there is no "
4948 "corresponding SRR CTIO, deleting IMM "
4949 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
4950 imm);
4951 list_del(&imm->srr_list_entry);
4952
4953 kfree(imm);
4954
4955 spin_unlock(&tgt->srr_lock);
4956 goto out_reject;
4957 }
4958 }
4959 spin_unlock(&tgt->srr_lock);
4960 } else {
4961 struct qla_tgt_srr_ctio *ts;
4962
4963 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
4964 "qla_target(%d): Unable to allocate SRR IMM "
4965 "entry, SRR request will be rejected\n", vha->vp_idx);
4966
4967 /* IRQ is already OFF */
4968 spin_lock(&tgt->srr_lock);
4969 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
4970 srr_list_entry) {
4971 if (sctio->srr_id == tgt->imm_srr_id) {
4972 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
4973 "CTIO SRR %p deleted (id %d)\n",
4974 sctio, sctio->srr_id);
4975 list_del(&sctio->srr_list_entry);
4976 qlt_send_term_exchange(vha, sctio->cmd,
Quinn Trana07100e2015-12-07 19:48:57 -05004977 &sctio->cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004978 kfree(sctio);
4979 }
4980 }
4981 spin_unlock(&tgt->srr_lock);
4982 goto out_reject;
4983 }
4984
4985 return;
4986
4987out_reject:
4988 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
4989 NOTIFY_ACK_SRR_FLAGS_REJECT,
4990 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4991 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4992}
4993
4994/*
4995 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4996 */
4997static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4998 struct imm_ntfy_from_isp *iocb)
4999{
5000 struct qla_hw_data *ha = vha->hw;
5001 uint32_t add_flags = 0;
5002 int send_notify_ack = 1;
5003 uint16_t status;
5004
5005 status = le16_to_cpu(iocb->u.isp2x.status);
5006 switch (status) {
5007 case IMM_NTFY_LIP_RESET:
5008 {
5009 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5010 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5011 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5012 iocb->u.isp24.status_subcode);
5013
5014 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5015 send_notify_ack = 0;
5016 break;
5017 }
5018
5019 case IMM_NTFY_LIP_LINK_REINIT:
5020 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005021 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005022 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5023 "qla_target(%d): LINK REINIT (loop %#x, "
5024 "subcode %x)\n", vha->vp_idx,
5025 le16_to_cpu(iocb->u.isp24.nport_handle),
5026 iocb->u.isp24.status_subcode);
5027 if (tgt->link_reinit_iocb_pending) {
5028 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
5029 0, 0, 0, 0, 0, 0);
5030 }
5031 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5032 tgt->link_reinit_iocb_pending = 1;
5033 /*
5034 * QLogic requires to wait after LINK REINIT for possible
5035 * PDISC or ADISC ELS commands
5036 */
5037 send_notify_ack = 0;
5038 break;
5039 }
5040
5041 case IMM_NTFY_PORT_LOGOUT:
5042 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5043 "qla_target(%d): Port logout (loop "
5044 "%#x, subcode %x)\n", vha->vp_idx,
5045 le16_to_cpu(iocb->u.isp24.nport_handle),
5046 iocb->u.isp24.status_subcode);
5047
5048 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5049 send_notify_ack = 0;
5050 /* The sessions will be cleared in the callback, if needed */
5051 break;
5052
5053 case IMM_NTFY_GLBL_TPRLO:
5054 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5055 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5056 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5057 send_notify_ack = 0;
5058 /* The sessions will be cleared in the callback, if needed */
5059 break;
5060
5061 case IMM_NTFY_PORT_CONFIG:
5062 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5063 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5064 status);
5065 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5066 send_notify_ack = 0;
5067 /* The sessions will be cleared in the callback, if needed */
5068 break;
5069
5070 case IMM_NTFY_GLBL_LOGO:
5071 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5072 "qla_target(%d): Link failure detected\n",
5073 vha->vp_idx);
5074 /* I_T nexus loss */
5075 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5076 send_notify_ack = 0;
5077 break;
5078
5079 case IMM_NTFY_IOCB_OVERFLOW:
5080 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5081 "qla_target(%d): Cannot provide requested "
5082 "capability (IOCB overflowed the immediate notify "
5083 "resource count)\n", vha->vp_idx);
5084 break;
5085
5086 case IMM_NTFY_ABORT_TASK:
5087 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5088 "qla_target(%d): Abort Task (S %08x I %#x -> "
5089 "L %#x)\n", vha->vp_idx,
5090 le16_to_cpu(iocb->u.isp2x.seq_id),
5091 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5092 le16_to_cpu(iocb->u.isp2x.lun));
5093 if (qlt_abort_task(vha, iocb) == 0)
5094 send_notify_ack = 0;
5095 break;
5096
5097 case IMM_NTFY_RESOURCE:
5098 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5099 "qla_target(%d): Out of resources, host %ld\n",
5100 vha->vp_idx, vha->host_no);
5101 break;
5102
5103 case IMM_NTFY_MSG_RX:
5104 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5105 "qla_target(%d): Immediate notify task %x\n",
5106 vha->vp_idx, iocb->u.isp2x.task_flags);
5107 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5108 send_notify_ack = 0;
5109 break;
5110
5111 case IMM_NTFY_ELS:
5112 if (qlt_24xx_handle_els(vha, iocb) == 0)
5113 send_notify_ack = 0;
5114 break;
5115
5116 case IMM_NTFY_SRR:
5117 qlt_prepare_srr_imm(vha, iocb);
5118 send_notify_ack = 0;
5119 break;
5120
5121 default:
5122 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5123 "qla_target(%d): Received unknown immediate "
5124 "notify status %x\n", vha->vp_idx, status);
5125 break;
5126 }
5127
5128 if (send_notify_ack)
5129 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
5130}
5131
5132/*
5133 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5134 * This function sends busy to ISP 2xxx or 24xx.
5135 */
Quinn Tran33e79972014-09-25 06:14:55 -04005136static int __qlt_send_busy(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005137 struct atio_from_isp *atio, uint16_t status)
5138{
5139 struct ctio7_to_24xx *ctio24;
5140 struct qla_hw_data *ha = vha->hw;
5141 request_t *pkt;
5142 struct qla_tgt_sess *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005143 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005144
Quinn Tran75601512015-12-17 14:57:04 -05005145 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005146 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5147 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05005148 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005149 if (!sess) {
Quinn Trana07100e2015-12-07 19:48:57 -05005150 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005151 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005152 }
5153 /* Sending marker isn't necessary, since we called from ISR */
5154
5155 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
5156 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005157 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005158 "qla_target(%d): %s failed: unable to allocate "
5159 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005160 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005161 }
5162
Himanshu Madhanice1025c2015-12-17 14:56:58 -05005163 vha->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005164 pkt->entry_count = 1;
5165 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5166
5167 ctio24 = (struct ctio7_to_24xx *)pkt;
5168 ctio24->entry_type = CTIO_TYPE7;
5169 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005170 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005171 ctio24->vp_index = vha->vp_idx;
5172 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5173 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5174 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5175 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5176 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07005177 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005178 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5179 CTIO7_FLAGS_DONT_RET_CTIO);
5180 /*
5181 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5182 * if the explicit conformation is used.
5183 */
5184 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5185 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005186 /* Memory Barrier */
5187 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005188 qla2x00_start_iocbs(vha, vha->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005189 return 0;
5190}
5191
5192/*
5193 * This routine is used to allocate a command for either a QFull condition
5194 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5195 * out previously.
5196 */
5197static void
5198qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5199 struct atio_from_isp *atio, uint16_t status, int qfull)
5200{
5201 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5202 struct qla_hw_data *ha = vha->hw;
5203 struct qla_tgt_sess *sess;
5204 struct se_session *se_sess;
5205 struct qla_tgt_cmd *cmd;
5206 int tag;
5207
5208 if (unlikely(tgt->tgt_stop)) {
5209 ql_dbg(ql_dbg_io, vha, 0x300a,
5210 "New command while device %p is shutting down\n", tgt);
5211 return;
5212 }
5213
5214 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5215 vha->hw->tgt.num_qfull_cmds_dropped++;
5216 if (vha->hw->tgt.num_qfull_cmds_dropped >
5217 vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
5218 vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
5219 vha->hw->tgt.num_qfull_cmds_dropped;
5220
5221 ql_dbg(ql_dbg_io, vha, 0x3068,
5222 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5223 vha->vp_idx, __func__,
5224 vha->hw->tgt.num_qfull_cmds_dropped);
5225
5226 qlt_chk_exch_leak_thresh_hold(vha);
5227 return;
5228 }
5229
5230 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5231 (vha, atio->u.isp24.fcp_hdr.s_id);
5232 if (!sess)
5233 return;
5234
5235 se_sess = sess->se_sess;
5236
5237 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5238 if (tag < 0)
5239 return;
5240
5241 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5242 if (!cmd) {
5243 ql_dbg(ql_dbg_io, vha, 0x3009,
5244 "qla_target(%d): %s: Allocation of cmd failed\n",
5245 vha->vp_idx, __func__);
5246
5247 vha->hw->tgt.num_qfull_cmds_dropped++;
5248 if (vha->hw->tgt.num_qfull_cmds_dropped >
5249 vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
5250 vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
5251 vha->hw->tgt.num_qfull_cmds_dropped;
5252
5253 qlt_chk_exch_leak_thresh_hold(vha);
5254 return;
5255 }
5256
5257 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5258
5259 qlt_incr_num_pend_cmds(vha);
5260 INIT_LIST_HEAD(&cmd->cmd_list);
5261 memcpy(&cmd->atio, atio, sizeof(*atio));
5262
5263 cmd->tgt = vha->vha_tgt.qla_tgt;
5264 cmd->vha = vha;
5265 cmd->reset_count = vha->hw->chip_reset;
5266 cmd->q_full = 1;
5267
5268 if (qfull) {
5269 cmd->q_full = 1;
5270 /* NOTE: borrowing the state field to carry the status */
5271 cmd->state = status;
5272 } else
5273 cmd->term_exchg = 1;
5274
5275 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5276
5277 vha->hw->tgt.num_qfull_cmds_alloc++;
5278 if (vha->hw->tgt.num_qfull_cmds_alloc >
5279 vha->hw->qla_stats.stat_max_qfull_cmds_alloc)
5280 vha->hw->qla_stats.stat_max_qfull_cmds_alloc =
5281 vha->hw->tgt.num_qfull_cmds_alloc;
5282}
5283
5284int
5285qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5286{
5287 struct qla_hw_data *ha = vha->hw;
5288 unsigned long flags;
5289 struct qla_tgt_cmd *cmd, *tcmd;
5290 struct list_head free_list;
5291 int rc = 0;
5292
5293 if (list_empty(&ha->tgt.q_full_list))
5294 return 0;
5295
5296 INIT_LIST_HEAD(&free_list);
5297
5298 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5299
5300 if (list_empty(&ha->tgt.q_full_list)) {
5301 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5302 return 0;
5303 }
5304
5305 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5306 if (cmd->q_full)
5307 /* cmd->state is a borrowed field to hold status */
5308 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5309 else if (cmd->term_exchg)
5310 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5311
5312 if (rc == -ENOMEM)
5313 break;
5314
5315 if (cmd->q_full)
5316 ql_dbg(ql_dbg_io, vha, 0x3006,
5317 "%s: busy sent for ox_id[%04x]\n", __func__,
5318 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5319 else if (cmd->term_exchg)
5320 ql_dbg(ql_dbg_io, vha, 0x3007,
5321 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5322 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5323 else
5324 ql_dbg(ql_dbg_io, vha, 0x3008,
5325 "%s: Unexpected cmd in QFull list %p\n", __func__,
5326 cmd);
5327
5328 list_del(&cmd->cmd_list);
5329 list_add_tail(&cmd->cmd_list, &free_list);
5330
5331 /* piggy back on hardware_lock for protection */
5332 vha->hw->tgt.num_qfull_cmds_alloc--;
5333 }
5334 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5335
5336 cmd = NULL;
5337
5338 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5339 list_del(&cmd->cmd_list);
5340 /* This cmd was never sent to TCM. There is no need
5341 * to schedule free or call free_cmd
5342 */
5343 qlt_free_cmd(cmd);
5344 }
5345 return rc;
5346}
5347
5348static void
5349qlt_send_busy(struct scsi_qla_host *vha,
5350 struct atio_from_isp *atio, uint16_t status)
5351{
5352 int rc = 0;
5353
5354 rc = __qlt_send_busy(vha, atio, status);
5355 if (rc == -ENOMEM)
5356 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5357}
5358
5359static int
5360qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
5361 struct atio_from_isp *atio)
5362{
5363 struct qla_hw_data *ha = vha->hw;
5364 uint16_t status;
5365
5366 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5367 return 0;
5368
5369 status = temp_sam_status;
5370 qlt_send_busy(vha, atio, status);
5371 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005372}
5373
5374/* ha->hardware_lock supposed to be held on entry */
5375/* called via callback from qla2xxx */
5376static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005377 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005378{
5379 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005380 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005381 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005382 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005383
5384 if (unlikely(tgt == NULL)) {
Arun Easi667024a2014-09-25 06:14:47 -04005385 ql_dbg(ql_dbg_io, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005386 "ATIO pkt, but no tgt (ha %p)", ha);
5387 return;
5388 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005389 /*
5390 * In tgt_stop mode we also should allow all requests to pass.
5391 * Otherwise, some commands can stuck.
5392 */
5393
Quinn Tran2f424b92015-12-17 14:57:07 -05005394 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005395
5396 switch (atio->u.raw.entry_type) {
5397 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005398 if (unlikely(atio->u.isp24.exchange_addr ==
5399 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005400 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005401 "qla_target(%d): ATIO_TYPE7 "
5402 "received with UNKNOWN exchange address, "
5403 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005404 if (!ha_locked)
5405 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005406 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005407 if (!ha_locked)
5408 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005409 break;
5410 }
Quinn Tran33e79972014-09-25 06:14:55 -04005411
5412
5413
5414 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5415 rc = qlt_chk_qfull_thresh_hold(vha, atio);
5416 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005417 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005418 return;
5419 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005420 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005421 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005422 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005423 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005424 if (unlikely(rc != 0)) {
5425 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005426 if (!ha_locked)
5427 spin_lock_irqsave
5428 (&ha->hardware_lock, flags);
5429
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005430#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5431 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5432#else
Quinn Trana07100e2015-12-07 19:48:57 -05005433 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005434#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005435
5436 if (!ha_locked)
5437 spin_unlock_irqrestore
5438 (&ha->hardware_lock, flags);
5439
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005440 } else {
5441 if (tgt->tgt_stop) {
5442 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5443 "qla_target: Unable to send "
5444 "command to target for req, "
5445 "ignoring.\n");
5446 } else {
5447 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5448 "qla_target(%d): Unable to send "
5449 "command to target, sending BUSY "
5450 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005451 if (!ha_locked)
5452 spin_lock_irqsave(
5453 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005454 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005455 if (!ha_locked)
5456 spin_unlock_irqrestore(
5457 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005458 }
5459 }
5460 }
5461 break;
5462
5463 case IMMED_NOTIFY_TYPE:
5464 {
5465 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5466 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5467 "qla_target(%d): Received ATIO packet %x "
5468 "with error status %x\n", vha->vp_idx,
5469 atio->u.raw.entry_type,
5470 atio->u.isp2x.entry_status);
5471 break;
5472 }
5473 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005474
5475 if (!ha_locked)
5476 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005477 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005478 if (!ha_locked)
5479 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005480 break;
5481 }
5482
5483 default:
5484 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5485 "qla_target(%d): Received unknown ATIO atio "
5486 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5487 break;
5488 }
5489
Quinn Tran2f424b92015-12-17 14:57:07 -05005490 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005491}
5492
5493/* ha->hardware_lock supposed to be held on entry */
5494/* called via callback from qla2xxx */
5495static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5496{
5497 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005498 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005499
5500 if (unlikely(tgt == NULL)) {
5501 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5502 "qla_target(%d): Response pkt %x received, but no "
5503 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5504 return;
5505 }
5506
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005507 /*
5508 * In tgt_stop mode we also should allow all requests to pass.
5509 * Otherwise, some commands can stuck.
5510 */
5511
5512 tgt->irq_cmd_count++;
5513
5514 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005515 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005516 case CTIO_TYPE7:
5517 {
5518 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005519 qlt_do_ctio_completion(vha, entry->handle,
5520 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5521 entry);
5522 break;
5523 }
5524
5525 case ACCEPT_TGT_IO_TYPE:
5526 {
5527 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5528 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005529 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005530 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005531 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5532 "qla_target(%d): ATIO with error "
5533 "status %x received\n", vha->vp_idx,
5534 le16_to_cpu(atio->u.isp2x.status));
5535 break;
5536 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005537
Quinn Tran33e79972014-09-25 06:14:55 -04005538 rc = qlt_chk_qfull_thresh_hold(vha, atio);
5539 if (rc != 0) {
5540 tgt->irq_cmd_count--;
5541 return;
5542 }
5543
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005544 rc = qlt_handle_cmd_for_atio(vha, atio);
5545 if (unlikely(rc != 0)) {
5546 if (rc == -ESRCH) {
5547#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5548 qlt_send_busy(vha, atio, 0);
5549#else
Quinn Trana07100e2015-12-07 19:48:57 -05005550 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005551#endif
5552 } else {
5553 if (tgt->tgt_stop) {
5554 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5555 "qla_target: Unable to send "
5556 "command to target, sending TERM "
5557 "EXCHANGE for rsp\n");
5558 qlt_send_term_exchange(vha, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005559 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005560 } else {
5561 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5562 "qla_target(%d): Unable to send "
5563 "command to target, sending BUSY "
5564 "status\n", vha->vp_idx);
5565 qlt_send_busy(vha, atio, 0);
5566 }
5567 }
5568 }
5569 }
5570 break;
5571
5572 case CONTINUE_TGT_IO_TYPE:
5573 {
5574 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005575 qlt_do_ctio_completion(vha, entry->handle,
5576 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5577 entry);
5578 break;
5579 }
5580
5581 case CTIO_A64_TYPE:
5582 {
5583 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005584 qlt_do_ctio_completion(vha, entry->handle,
5585 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5586 entry);
5587 break;
5588 }
5589
5590 case IMMED_NOTIFY_TYPE:
5591 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5592 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5593 break;
5594
5595 case NOTIFY_ACK_TYPE:
5596 if (tgt->notify_ack_expected > 0) {
5597 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5598 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5599 "NOTIFY_ACK seq %08x status %x\n",
5600 le16_to_cpu(entry->u.isp2x.seq_id),
5601 le16_to_cpu(entry->u.isp2x.status));
5602 tgt->notify_ack_expected--;
5603 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005604 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005605 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5606 "qla_target(%d): NOTIFY_ACK "
5607 "failed %x\n", vha->vp_idx,
5608 le16_to_cpu(entry->u.isp2x.status));
5609 }
5610 } else {
5611 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5612 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5613 vha->vp_idx);
5614 }
5615 break;
5616
5617 case ABTS_RECV_24XX:
5618 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5619 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5620 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5621 break;
5622
5623 case ABTS_RESP_24XX:
5624 if (tgt->abts_resp_expected > 0) {
5625 struct abts_resp_from_24xx_fw *entry =
5626 (struct abts_resp_from_24xx_fw *)pkt;
5627 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5628 "ABTS_RESP_24XX: compl_status %x\n",
5629 entry->compl_status);
5630 tgt->abts_resp_expected--;
5631 if (le16_to_cpu(entry->compl_status) !=
5632 ABTS_RESP_COMPL_SUCCESS) {
5633 if ((entry->error_subcode1 == 0x1E) &&
5634 (entry->error_subcode2 == 0)) {
5635 /*
5636 * We've got a race here: aborted
5637 * exchange not terminated, i.e.
5638 * response for the aborted command was
5639 * sent between the abort request was
5640 * received and processed.
5641 * Unfortunately, the firmware has a
5642 * silly requirement that all aborted
5643 * exchanges must be explicitely
5644 * terminated, otherwise it refuses to
5645 * send responses for the abort
5646 * requests. So, we have to
5647 * (re)terminate the exchange and retry
5648 * the abort response.
5649 */
5650 qlt_24xx_retry_term_exchange(vha,
5651 entry);
5652 } else
5653 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5654 "qla_target(%d): ABTS_RESP_24XX "
5655 "failed %x (subcode %x:%x)",
5656 vha->vp_idx, entry->compl_status,
5657 entry->error_subcode1,
5658 entry->error_subcode2);
5659 }
5660 } else {
5661 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5662 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5663 "received\n", vha->vp_idx);
5664 }
5665 break;
5666
5667 default:
5668 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5669 "qla_target(%d): Received unknown response pkt "
5670 "type %x\n", vha->vp_idx, pkt->entry_type);
5671 break;
5672 }
5673
5674 tgt->irq_cmd_count--;
5675}
5676
5677/*
5678 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5679 */
5680void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5681 uint16_t *mailbox)
5682{
5683 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005684 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005685 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005686
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005687 if (!ha->tgt.tgt_ops)
5688 return;
5689
5690 if (unlikely(tgt == NULL)) {
5691 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5692 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5693 return;
5694 }
5695
5696 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5697 IS_QLA2100(ha))
5698 return;
5699 /*
5700 * In tgt_stop mode we also should allow all requests to pass.
5701 * Otherwise, some commands can stuck.
5702 */
5703
5704 tgt->irq_cmd_count++;
5705
5706 switch (code) {
5707 case MBA_RESET: /* Reset */
5708 case MBA_SYSTEM_ERR: /* System Error */
5709 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5710 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5711 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5712 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005713 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005714 break;
5715 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5716 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5717 break;
5718
5719 case MBA_LOOP_UP:
5720 {
5721 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005722 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005723 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5724 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5725 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005726 if (tgt->link_reinit_iocb_pending) {
5727 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5728 0, 0, 0, 0, 0, 0);
5729 tgt->link_reinit_iocb_pending = 0;
5730 }
5731 break;
5732 }
5733
5734 case MBA_LIP_OCCURRED:
5735 case MBA_LOOP_DOWN:
5736 case MBA_LIP_RESET:
5737 case MBA_RSCN_UPDATE:
5738 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005739 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005740 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5741 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5742 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005743 break;
5744
5745 case MBA_PORT_UPDATE:
5746 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5747 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005748 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005749 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5750 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5751 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5752
5753 login_code = le16_to_cpu(mailbox[2]);
5754 if (login_code == 0x4)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005755 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5756 "Async MB 2: Got PLOGI Complete\n");
Alan Cox4f1d0f12012-07-04 16:35:35 +01005757 else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005758 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5759 "Async MB 2: Port Logged Out\n");
5760 break;
5761
5762 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005763 break;
5764 }
5765
5766 tgt->irq_cmd_count--;
5767}
5768
5769static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5770 uint16_t loop_id)
5771{
5772 fc_port_t *fcport;
5773 int rc;
5774
5775 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5776 if (!fcport) {
5777 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5778 "qla_target(%d): Allocation of tmp FC port failed",
5779 vha->vp_idx);
5780 return NULL;
5781 }
5782
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005783 fcport->loop_id = loop_id;
5784
5785 rc = qla2x00_get_port_database(vha, fcport, 0);
5786 if (rc != QLA_SUCCESS) {
5787 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5788 "qla_target(%d): Failed to retrieve fcport "
5789 "information -- get_port_database() returned %x "
5790 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5791 kfree(fcport);
5792 return NULL;
5793 }
5794
5795 return fcport;
5796}
5797
5798/* Must be called under tgt_mutex */
5799static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
5800 uint8_t *s_id)
5801{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005802 struct qla_tgt_sess *sess = NULL;
5803 fc_port_t *fcport = NULL;
5804 int rc, global_resets;
5805 uint16_t loop_id = 0;
5806
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005807 mutex_lock(&vha->vha_tgt.tgt_mutex);
5808
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005809retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005810 global_resets =
5811 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005812
5813 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5814 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005815 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5816
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005817 if ((s_id[0] == 0xFF) &&
5818 (s_id[1] == 0xFC)) {
5819 /*
5820 * This is Domain Controller, so it should be
5821 * OK to drop SCSI commands from it.
5822 */
5823 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5824 "Unable to find initiator with S_ID %x:%x:%x",
5825 s_id[0], s_id[1], s_id[2]);
5826 } else
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005827 ql_log(ql_log_info, vha, 0xf071,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005828 "qla_target(%d): Unable to find "
5829 "initiator with S_ID %x:%x:%x",
5830 vha->vp_idx, s_id[0], s_id[1],
5831 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005832
5833 if (rc == -ENOENT) {
5834 qlt_port_logo_t logo;
5835 sid_to_portid(s_id, &logo.id);
5836 logo.cmd_count = 1;
5837 qlt_send_first_logo(vha, &logo);
5838 }
5839
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005840 return NULL;
5841 }
5842
5843 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005844 if (!fcport) {
5845 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005846 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005847 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005848
5849 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005850 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005851 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5852 "qla_target(%d): global reset during session discovery "
5853 "(counter was %d, new %d), retrying", vha->vp_idx,
5854 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005855 atomic_read(&vha->vha_tgt.
5856 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005857 goto retry;
5858 }
5859
5860 sess = qlt_create_sess(vha, fcport, true);
5861
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005862 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5863
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005864 kfree(fcport);
5865 return sess;
5866}
5867
5868static void qlt_abort_work(struct qla_tgt *tgt,
5869 struct qla_tgt_sess_work_param *prm)
5870{
5871 struct scsi_qla_host *vha = tgt->vha;
5872 struct qla_hw_data *ha = vha->hw;
5873 struct qla_tgt_sess *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005874 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005875 uint32_t be_s_id;
5876 uint8_t s_id[3];
5877 int rc;
5878
Quinn Tran75601512015-12-17 14:57:04 -05005879 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005880
5881 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005882 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005883
5884 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5885 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5886 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5887
5888 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5889 (unsigned char *)&be_s_id);
5890 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005891 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005892
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005893 sess = qlt_make_local_sess(vha, s_id);
5894 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005895
Quinn Tran75601512015-12-17 14:57:04 -05005896 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005897 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005898 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005899 } else {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005900 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
5901 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005902 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005903 }
5904
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005905 kref_get(&sess->se_sess->sess_kref);
5906 }
5907
Quinn Tran75601512015-12-17 14:57:04 -05005908 spin_lock_irqsave(&ha->hardware_lock, flags);
5909
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005910 if (tgt->tgt_stop)
5911 goto out_term;
5912
5913 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5914 if (rc != 0)
5915 goto out_term;
Quinn Tran75601512015-12-17 14:57:04 -05005916 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005917
5918 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005919 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005920 return;
5921
Quinn Tran75601512015-12-17 14:57:04 -05005922out_term2:
5923 spin_lock_irqsave(&ha->hardware_lock, flags);
5924
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005925out_term:
5926 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
Quinn Tran75601512015-12-17 14:57:04 -05005927 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5928
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005929 if (sess)
5930 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005931 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005932}
5933
5934static void qlt_tmr_work(struct qla_tgt *tgt,
5935 struct qla_tgt_sess_work_param *prm)
5936{
5937 struct atio_from_isp *a = &prm->tm_iocb2;
5938 struct scsi_qla_host *vha = tgt->vha;
5939 struct qla_hw_data *ha = vha->hw;
5940 struct qla_tgt_sess *sess = NULL;
5941 unsigned long flags;
5942 uint8_t *s_id = NULL; /* to hide compiler warnings */
5943 int rc;
5944 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005945 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005946 void *iocb;
5947
Quinn Tran75601512015-12-17 14:57:04 -05005948 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005949
5950 if (tgt->tgt_stop)
5951 goto out_term;
5952
5953 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5954 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5955 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005956 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005957
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005958 sess = qlt_make_local_sess(vha, s_id);
5959 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005960
Quinn Tran75601512015-12-17 14:57:04 -05005961 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005962 if (!sess)
5963 goto out_term;
5964 } else {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005965 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
5966 sess = NULL;
5967 goto out_term;
5968 }
5969
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005970 kref_get(&sess->se_sess->sess_kref);
5971 }
5972
5973 iocb = a;
5974 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005975 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5976 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5977
5978 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5979 if (rc != 0)
5980 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005981
5982 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005983 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005984 return;
5985
5986out_term:
Quinn Trana07100e2015-12-07 19:48:57 -05005987 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005988 if (sess)
5989 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005990 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005991}
5992
5993static void qlt_sess_work_fn(struct work_struct *work)
5994{
5995 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5996 struct scsi_qla_host *vha = tgt->vha;
5997 unsigned long flags;
5998
5999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6000
6001 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6002 while (!list_empty(&tgt->sess_works_list)) {
6003 struct qla_tgt_sess_work_param *prm = list_entry(
6004 tgt->sess_works_list.next, typeof(*prm),
6005 sess_works_list_entry);
6006
6007 /*
6008 * This work can be scheduled on several CPUs at time, so we
6009 * must delete the entry to eliminate double processing
6010 */
6011 list_del(&prm->sess_works_list_entry);
6012
6013 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6014
6015 switch (prm->type) {
6016 case QLA_TGT_SESS_WORK_ABORT:
6017 qlt_abort_work(tgt, prm);
6018 break;
6019 case QLA_TGT_SESS_WORK_TM:
6020 qlt_tmr_work(tgt, prm);
6021 break;
6022 default:
6023 BUG_ON(1);
6024 break;
6025 }
6026
6027 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6028
6029 kfree(prm);
6030 }
6031 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6032}
6033
6034/* Must be called under tgt_host_action_mutex */
6035int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6036{
6037 struct qla_tgt *tgt;
6038
6039 if (!QLA_TGT_MODE_ENABLED())
6040 return 0;
6041
Arun Easi33c36c02013-01-30 03:34:41 -05006042 if (!IS_TGT_MODE_CAPABLE(ha)) {
6043 ql_log(ql_log_warn, base_vha, 0xe070,
6044 "This adapter does not support target mode.\n");
6045 return 0;
6046 }
6047
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006048 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006049 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006050
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006051 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006052
6053 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6054 if (!tgt) {
6055 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6056 "Unable to allocate struct qla_tgt\n");
6057 return -ENOMEM;
6058 }
6059
6060 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6061 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6062
6063 tgt->ha = ha;
6064 tgt->vha = base_vha;
6065 init_waitqueue_head(&tgt->waitQ);
6066 INIT_LIST_HEAD(&tgt->sess_list);
6067 INIT_LIST_HEAD(&tgt->del_sess_list);
6068 INIT_DELAYED_WORK(&tgt->sess_del_work,
6069 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
6070 spin_lock_init(&tgt->sess_work_lock);
6071 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6072 INIT_LIST_HEAD(&tgt->sess_works_list);
6073 spin_lock_init(&tgt->srr_lock);
6074 INIT_LIST_HEAD(&tgt->srr_ctio_list);
6075 INIT_LIST_HEAD(&tgt->srr_imm_list);
6076 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
6077 atomic_set(&tgt->tgt_global_resets_count, 0);
6078
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006079 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006080
6081 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6082 "qla_target(%d): using 64 Bit PCI addressing",
6083 base_vha->vp_idx);
6084 tgt->tgt_enable_64bit_addr = 1;
6085 /* 3 is reserved */
6086 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6087 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
6088 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
6089
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006090 if (base_vha->fc_vport)
6091 return 0;
6092
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006093 mutex_lock(&qla_tgt_mutex);
6094 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6095 mutex_unlock(&qla_tgt_mutex);
6096
6097 return 0;
6098}
6099
6100/* Must be called under tgt_host_action_mutex */
6101int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6102{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006103 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006104 return 0;
6105
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006106 if (vha->fc_vport) {
6107 qlt_release(vha->vha_tgt.qla_tgt);
6108 return 0;
6109 }
Quinn Tran33e79972014-09-25 06:14:55 -04006110
6111 /* free left over qfull cmds */
6112 qlt_init_term_exchange(vha);
6113
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006114 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006115 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006116 mutex_unlock(&qla_tgt_mutex);
6117
6118 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6119 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006120 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006121
6122 return 0;
6123}
6124
6125static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6126 unsigned char *b)
6127{
6128 int i;
6129
6130 pr_debug("qla2xxx HW vha->node_name: ");
6131 for (i = 0; i < WWN_SIZE; i++)
6132 pr_debug("%02x ", vha->node_name[i]);
6133 pr_debug("\n");
6134 pr_debug("qla2xxx HW vha->port_name: ");
6135 for (i = 0; i < WWN_SIZE; i++)
6136 pr_debug("%02x ", vha->port_name[i]);
6137 pr_debug("\n");
6138
6139 pr_debug("qla2xxx passed configfs WWPN: ");
6140 put_unaligned_be64(wwpn, b);
6141 for (i = 0; i < WWN_SIZE; i++)
6142 pr_debug("%02x ", b[i]);
6143 pr_debug("\n");
6144}
6145
6146/**
6147 * qla_tgt_lport_register - register lport with external module
6148 *
6149 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6150 * @wwpn: Passwd FC target WWPN
6151 * @callback: lport initialization callback for tcm_qla2xxx code
6152 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6153 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006154int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6155 u64 npiv_wwpn, u64 npiv_wwnn,
6156 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006157{
6158 struct qla_tgt *tgt;
6159 struct scsi_qla_host *vha;
6160 struct qla_hw_data *ha;
6161 struct Scsi_Host *host;
6162 unsigned long flags;
6163 int rc;
6164 u8 b[WWN_SIZE];
6165
6166 mutex_lock(&qla_tgt_mutex);
6167 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6168 vha = tgt->vha;
6169 ha = vha->hw;
6170
6171 host = vha->host;
6172 if (!host)
6173 continue;
6174
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006175 if (!(host->hostt->supported_mode & MODE_TARGET))
6176 continue;
6177
6178 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006179 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006180 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6181 host->host_no);
6182 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6183 continue;
6184 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006185 if (tgt->tgt_stop) {
6186 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6187 host->host_no);
6188 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6189 continue;
6190 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006191 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6192
6193 if (!scsi_host_get(host)) {
6194 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6195 "Unable to scsi_host_get() for"
6196 " qla2xxx scsi_host\n");
6197 continue;
6198 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006199 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006200
6201 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6202 scsi_host_put(host);
6203 continue;
6204 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006205 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6206 if (rc != 0)
6207 scsi_host_put(host);
6208
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006209 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006210 return rc;
6211 }
6212 mutex_unlock(&qla_tgt_mutex);
6213
6214 return -ENODEV;
6215}
6216EXPORT_SYMBOL(qlt_lport_register);
6217
6218/**
6219 * qla_tgt_lport_deregister - Degister lport
6220 *
6221 * @vha: Registered scsi_qla_host pointer
6222 */
6223void qlt_lport_deregister(struct scsi_qla_host *vha)
6224{
6225 struct qla_hw_data *ha = vha->hw;
6226 struct Scsi_Host *sh = vha->host;
6227 /*
6228 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6229 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006230 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006231 ha->tgt.tgt_ops = NULL;
6232 /*
6233 * Release the Scsi_Host reference for the underlying qla2xxx host
6234 */
6235 scsi_host_put(sh);
6236}
6237EXPORT_SYMBOL(qlt_lport_deregister);
6238
6239/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006240static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006241{
6242 struct qla_hw_data *ha = vha->hw;
6243
6244 switch (ql2x_ini_mode) {
6245 case QLA2XXX_INI_MODE_DISABLED:
6246 case QLA2XXX_INI_MODE_EXCLUSIVE:
6247 vha->host->active_mode = MODE_TARGET;
6248 break;
6249 case QLA2XXX_INI_MODE_ENABLED:
6250 vha->host->active_mode |= MODE_TARGET;
6251 break;
6252 default:
6253 break;
6254 }
6255
6256 if (ha->tgt.ini_mode_force_reverse)
6257 qla_reverse_ini_mode(vha);
6258}
6259
6260/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006261static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006262{
6263 struct qla_hw_data *ha = vha->hw;
6264
6265 switch (ql2x_ini_mode) {
6266 case QLA2XXX_INI_MODE_DISABLED:
6267 vha->host->active_mode = MODE_UNKNOWN;
6268 break;
6269 case QLA2XXX_INI_MODE_EXCLUSIVE:
6270 vha->host->active_mode = MODE_INITIATOR;
6271 break;
6272 case QLA2XXX_INI_MODE_ENABLED:
6273 vha->host->active_mode &= ~MODE_TARGET;
6274 break;
6275 default:
6276 break;
6277 }
6278
6279 if (ha->tgt.ini_mode_force_reverse)
6280 qla_reverse_ini_mode(vha);
6281}
6282
6283/*
6284 * qla_tgt_enable_vha - NO LOCK HELD
6285 *
6286 * host_reset, bring up w/ Target Mode Enabled
6287 */
6288void
6289qlt_enable_vha(struct scsi_qla_host *vha)
6290{
6291 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006292 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006293 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006294 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Trancdb898c2015-12-17 14:57:05 -05006295 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006296
6297 if (!tgt) {
6298 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6299 "Unable to locate qla_tgt pointer from"
6300 " struct qla_hw_data\n");
6301 dump_stack();
6302 return;
6303 }
6304
6305 spin_lock_irqsave(&ha->hardware_lock, flags);
6306 tgt->tgt_stopped = 0;
6307 qlt_set_mode(vha);
6308 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6309
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006310 if (vha->vp_idx) {
6311 qla24xx_disable_vp(vha);
6312 qla24xx_enable_vp(vha);
6313 } else {
Quinn Trancdb898c2015-12-17 14:57:05 -05006314 if (ha->msix_entries) {
6315 ql_dbg(ql_dbg_tgt, vha, 0xffff,
6316 "%s: host%ld : vector %d cpu %d\n",
6317 __func__, vha->host_no,
6318 ha->msix_entries[rspq_ent].vector,
6319 ha->msix_entries[rspq_ent].cpuid);
6320
6321 ha->tgt.rspq_vector_cpuid =
6322 ha->msix_entries[rspq_ent].cpuid;
6323 }
6324
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006325 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6326 qla2xxx_wake_dpc(base_vha);
6327 qla2x00_wait_for_hba_online(base_vha);
6328 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006329}
6330EXPORT_SYMBOL(qlt_enable_vha);
6331
6332/*
6333 * qla_tgt_disable_vha - NO LOCK HELD
6334 *
6335 * Disable Target Mode and reset the adapter
6336 */
Joern Engel55a90662014-09-16 16:23:15 -04006337static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006338{
6339 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006340 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006341 unsigned long flags;
6342
6343 if (!tgt) {
6344 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6345 "Unable to locate qla_tgt pointer from"
6346 " struct qla_hw_data\n");
6347 dump_stack();
6348 return;
6349 }
6350
6351 spin_lock_irqsave(&ha->hardware_lock, flags);
6352 qlt_clear_mode(vha);
6353 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6354
6355 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6356 qla2xxx_wake_dpc(vha);
6357 qla2x00_wait_for_hba_online(vha);
6358}
6359
6360/*
6361 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6362 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6363 * members.
6364 */
6365void
6366qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6367{
6368 if (!qla_tgt_mode_enabled(vha))
6369 return;
6370
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006371 vha->vha_tgt.qla_tgt = NULL;
6372
6373 mutex_init(&vha->vha_tgt.tgt_mutex);
6374 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006375
6376 qlt_clear_mode(vha);
6377
6378 /*
6379 * NOTE: Currently the value is kept the same for <24xx and
6380 * >=24xx ISPs. If it is necessary to change it,
6381 * the check should be added for specific ISPs,
6382 * assigning the value appropriately.
6383 */
6384 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006385
6386 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006387}
6388
6389void
6390qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6391{
6392 /*
6393 * FC-4 Feature bit 0 indicates target functionality to the name server.
6394 */
6395 if (qla_tgt_mode_enabled(vha)) {
6396 if (qla_ini_mode_enabled(vha))
6397 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6398 else
6399 ct_req->req.rff_id.fc4_feature = BIT_0;
6400 } else if (qla_ini_mode_enabled(vha)) {
6401 ct_req->req.rff_id.fc4_feature = BIT_1;
6402 }
6403}
6404
6405/*
6406 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6407 * @ha: HA context
6408 *
6409 * Beginning of ATIO ring has initialization control block already built
6410 * by nvram config routine.
6411 *
6412 * Returns 0 on success.
6413 */
6414void
6415qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6416{
6417 struct qla_hw_data *ha = vha->hw;
6418 uint16_t cnt;
6419 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6420
6421 if (!qla_tgt_mode_enabled(vha))
6422 return;
6423
6424 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6425 pkt->u.raw.signature = ATIO_PROCESSED;
6426 pkt++;
6427 }
6428
6429}
6430
6431/*
6432 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6433 * @ha: SCSI driver HA context
6434 */
6435void
Quinn Tran2f424b92015-12-17 14:57:07 -05006436qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006437{
6438 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006439 struct atio_from_isp *pkt;
6440 int cnt, i;
6441
6442 if (!vha->flags.online)
6443 return;
6444
6445 while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
6446 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6447 cnt = pkt->u.raw.entry_count;
6448
Quinn Tran2f424b92015-12-17 14:57:07 -05006449 qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt,
6450 ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006451
6452 for (i = 0; i < cnt; i++) {
6453 ha->tgt.atio_ring_index++;
6454 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6455 ha->tgt.atio_ring_index = 0;
6456 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6457 } else
6458 ha->tgt.atio_ring_ptr++;
6459
6460 pkt->u.raw.signature = ATIO_PROCESSED;
6461 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6462 }
6463 wmb();
6464 }
6465
6466 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006467 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Quinn Tran3761f3e2015-06-10 11:05:19 -04006468 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006469}
6470
6471void
Arun Easiaa230bc2013-01-30 03:34:39 -05006472qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006473{
6474 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006475 if (!QLA_TGT_MODE_ENABLED())
6476 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006477
Arun Easiaa230bc2013-01-30 03:34:39 -05006478 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6479 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6480 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6481
6482 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6483 struct qla_msix_entry *msix = &ha->msix_entries[2];
6484 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6485
6486 icb->msix_atio = cpu_to_le16(msix->entry);
6487 ql_dbg(ql_dbg_init, vha, 0xf072,
6488 "Registering ICB vector 0x%x for atio que.\n",
6489 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006490 }
6491}
6492
6493void
6494qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6495{
6496 struct qla_hw_data *ha = vha->hw;
6497
6498 if (qla_tgt_mode_enabled(vha)) {
6499 if (!ha->tgt.saved_set) {
6500 /* We save only once */
6501 ha->tgt.saved_exchange_count = nv->exchange_count;
6502 ha->tgt.saved_firmware_options_1 =
6503 nv->firmware_options_1;
6504 ha->tgt.saved_firmware_options_2 =
6505 nv->firmware_options_2;
6506 ha->tgt.saved_firmware_options_3 =
6507 nv->firmware_options_3;
6508 ha->tgt.saved_set = 1;
6509 }
6510
Bart Van Asschead950362015-07-09 07:24:08 -07006511 nv->exchange_count = cpu_to_le16(0xFFFF);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006512
6513 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006514 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006515
6516 /* Disable ini mode, if requested */
6517 if (!qla_ini_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006518 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006519
6520 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006521 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006522 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006523 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006524 if (ql2xtgt_tape_enable)
6525 /* Enable FC Tape support */
6526 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6527 else
6528 /* Disable FC Tape support */
6529 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6530
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006531 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006532 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006533 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006534 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006535 } else {
6536 if (ha->tgt.saved_set) {
6537 nv->exchange_count = ha->tgt.saved_exchange_count;
6538 nv->firmware_options_1 =
6539 ha->tgt.saved_firmware_options_1;
6540 nv->firmware_options_2 =
6541 ha->tgt.saved_firmware_options_2;
6542 nv->firmware_options_3 =
6543 ha->tgt.saved_firmware_options_3;
6544 }
6545 return;
6546 }
6547
6548 /* out-of-order frames reassembly */
6549 nv->firmware_options_3 |= BIT_6|BIT_9;
6550
6551 if (ha->tgt.enable_class_2) {
6552 if (vha->flags.init_done)
6553 fc_host_supported_classes(vha->host) =
6554 FC_COS_CLASS2 | FC_COS_CLASS3;
6555
Bart Van Asschead950362015-07-09 07:24:08 -07006556 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006557 } else {
6558 if (vha->flags.init_done)
6559 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6560
Bart Van Asschead950362015-07-09 07:24:08 -07006561 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006562 }
6563}
6564
6565void
6566qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6567 struct init_cb_24xx *icb)
6568{
6569 struct qla_hw_data *ha = vha->hw;
6570
Quinn Tran481ce732015-12-17 14:57:08 -05006571 if (!QLA_TGT_MODE_ENABLED())
6572 return;
6573
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006574 if (ha->tgt.node_name_set) {
6575 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006576 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006577 }
Quinn Tran481ce732015-12-17 14:57:08 -05006578
6579 /* disable ZIO at start time. */
6580 if (!vha->flags.init_done) {
6581 uint32_t tmp;
6582 tmp = le32_to_cpu(icb->firmware_options_2);
6583 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6584 icb->firmware_options_2 = cpu_to_le32(tmp);
6585 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006586}
6587
Arun Easiaa230bc2013-01-30 03:34:39 -05006588void
6589qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6590{
6591 struct qla_hw_data *ha = vha->hw;
6592
6593 if (!QLA_TGT_MODE_ENABLED())
6594 return;
6595
6596 if (qla_tgt_mode_enabled(vha)) {
6597 if (!ha->tgt.saved_set) {
6598 /* We save only once */
6599 ha->tgt.saved_exchange_count = nv->exchange_count;
6600 ha->tgt.saved_firmware_options_1 =
6601 nv->firmware_options_1;
6602 ha->tgt.saved_firmware_options_2 =
6603 nv->firmware_options_2;
6604 ha->tgt.saved_firmware_options_3 =
6605 nv->firmware_options_3;
6606 ha->tgt.saved_set = 1;
6607 }
6608
Bart Van Asschead950362015-07-09 07:24:08 -07006609 nv->exchange_count = cpu_to_le16(0xFFFF);
Arun Easiaa230bc2013-01-30 03:34:39 -05006610
6611 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006612 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006613
6614 /* Disable ini mode, if requested */
6615 if (!qla_ini_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006616 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006617
6618 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006619 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006620 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006621 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006622 if (ql2xtgt_tape_enable)
6623 /* Enable FC tape support */
6624 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6625 else
6626 /* Disable FC tape support */
6627 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6628
Arun Easiaa230bc2013-01-30 03:34:39 -05006629 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006630 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006631 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006632 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006633 } else {
6634 if (ha->tgt.saved_set) {
6635 nv->exchange_count = ha->tgt.saved_exchange_count;
6636 nv->firmware_options_1 =
6637 ha->tgt.saved_firmware_options_1;
6638 nv->firmware_options_2 =
6639 ha->tgt.saved_firmware_options_2;
6640 nv->firmware_options_3 =
6641 ha->tgt.saved_firmware_options_3;
6642 }
6643 return;
6644 }
6645
6646 /* out-of-order frames reassembly */
6647 nv->firmware_options_3 |= BIT_6|BIT_9;
6648
6649 if (ha->tgt.enable_class_2) {
6650 if (vha->flags.init_done)
6651 fc_host_supported_classes(vha->host) =
6652 FC_COS_CLASS2 | FC_COS_CLASS3;
6653
Bart Van Asschead950362015-07-09 07:24:08 -07006654 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006655 } else {
6656 if (vha->flags.init_done)
6657 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6658
Bart Van Asschead950362015-07-09 07:24:08 -07006659 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006660 }
6661}
6662
6663void
6664qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6665 struct init_cb_81xx *icb)
6666{
6667 struct qla_hw_data *ha = vha->hw;
6668
6669 if (!QLA_TGT_MODE_ENABLED())
6670 return;
6671
6672 if (ha->tgt.node_name_set) {
6673 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006674 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006675 }
Quinn Tran481ce732015-12-17 14:57:08 -05006676
6677 /* disable ZIO at start time. */
6678 if (!vha->flags.init_done) {
6679 uint32_t tmp;
6680 tmp = le32_to_cpu(icb->firmware_options_2);
6681 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6682 icb->firmware_options_2 = cpu_to_le32(tmp);
6683 }
6684
Arun Easiaa230bc2013-01-30 03:34:39 -05006685}
6686
6687void
6688qlt_83xx_iospace_config(struct qla_hw_data *ha)
6689{
6690 if (!QLA_TGT_MODE_ENABLED())
6691 return;
6692
6693 ha->msix_count += 1; /* For ATIO Q */
6694}
6695
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006696int
6697qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6698 struct sts_entry_24xx *pkt)
6699{
6700 switch (pkt->entry_type) {
6701 case ABTS_RECV_24XX:
6702 case ABTS_RESP_24XX:
6703 case CTIO_TYPE7:
6704 case NOTIFY_ACK_TYPE:
Quinn Tranf83adb62014-04-11 16:54:43 -04006705 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006706 return 1;
6707 default:
6708 return 0;
6709 }
6710}
6711
6712void
6713qlt_modify_vp_config(struct scsi_qla_host *vha,
6714 struct vp_config_entry_24xx *vpmod)
6715{
6716 if (qla_tgt_mode_enabled(vha))
6717 vpmod->options_idx1 &= ~BIT_5;
6718 /* Disable ini mode, if requested */
6719 if (!qla_ini_mode_enabled(vha))
6720 vpmod->options_idx1 &= ~BIT_4;
6721}
6722
6723void
6724qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6725{
6726 if (!QLA_TGT_MODE_ENABLED())
6727 return;
6728
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04006729 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006730 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6731 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6732 } else {
6733 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6734 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6735 }
6736
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006737 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6738 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006739 qlt_clear_mode(base_vha);
6740}
6741
Arun Easiaa230bc2013-01-30 03:34:39 -05006742irqreturn_t
6743qla83xx_msix_atio_q(int irq, void *dev_id)
6744{
6745 struct rsp_que *rsp;
6746 scsi_qla_host_t *vha;
6747 struct qla_hw_data *ha;
6748 unsigned long flags;
6749
6750 rsp = (struct rsp_que *) dev_id;
6751 ha = rsp->hw;
6752 vha = pci_get_drvdata(ha->pdev);
6753
Quinn Tran2f424b92015-12-17 14:57:07 -05006754 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006755
Quinn Tran2f424b92015-12-17 14:57:07 -05006756 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006757
Quinn Tran2f424b92015-12-17 14:57:07 -05006758 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006759
6760 return IRQ_HANDLED;
6761}
6762
Quinn Tran2f424b92015-12-17 14:57:07 -05006763static void
6764qlt_handle_abts_recv_work(struct work_struct *work)
6765{
6766 struct qla_tgt_sess_op *op = container_of(work,
6767 struct qla_tgt_sess_op, work);
6768 scsi_qla_host_t *vha = op->vha;
6769 struct qla_hw_data *ha = vha->hw;
6770 unsigned long flags;
6771
6772 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6773 return;
6774
6775 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6776 qlt_24xx_process_atio_queue(vha, 0);
6777 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6778
6779 spin_lock_irqsave(&ha->hardware_lock, flags);
6780 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6781 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6782}
6783
6784void
6785qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6786{
6787 struct qla_tgt_sess_op *op;
6788
6789 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6790
6791 if (!op) {
6792 /* do not reach for ATIO queue here. This is best effort err
6793 * recovery at this point.
6794 */
6795 qlt_response_pkt_all_vps(vha, pkt);
6796 return;
6797 }
6798
6799 memcpy(&op->atio, pkt, sizeof(*pkt));
6800 op->vha = vha;
6801 op->chip_reset = vha->hw->chip_reset;
6802 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6803 queue_work(qla_tgt_wq, &op->work);
6804 return;
6805}
6806
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006807int
6808qlt_mem_alloc(struct qla_hw_data *ha)
6809{
6810 if (!QLA_TGT_MODE_ENABLED())
6811 return 0;
6812
6813 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6814 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6815 if (!ha->tgt.tgt_vp_map)
6816 return -ENOMEM;
6817
6818 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6819 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6820 &ha->tgt.atio_dma, GFP_KERNEL);
6821 if (!ha->tgt.atio_ring) {
6822 kfree(ha->tgt.tgt_vp_map);
6823 return -ENOMEM;
6824 }
6825 return 0;
6826}
6827
6828void
6829qlt_mem_free(struct qla_hw_data *ha)
6830{
6831 if (!QLA_TGT_MODE_ENABLED())
6832 return;
6833
6834 if (ha->tgt.atio_ring) {
6835 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6836 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6837 ha->tgt.atio_dma);
6838 }
6839 kfree(ha->tgt.tgt_vp_map);
6840}
6841
6842/* vport_slock to be held by the caller */
6843void
6844qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6845{
6846 if (!QLA_TGT_MODE_ENABLED())
6847 return;
6848
6849 switch (cmd) {
6850 case SET_VP_IDX:
6851 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6852 break;
6853 case SET_AL_PA:
6854 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
6855 break;
6856 case RESET_VP_IDX:
6857 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6858 break;
6859 case RESET_AL_PA:
6860 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
6861 break;
6862 }
6863}
6864
6865static int __init qlt_parse_ini_mode(void)
6866{
6867 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6868 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6869 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6870 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6871 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6872 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
6873 else
6874 return false;
6875
6876 return true;
6877}
6878
6879int __init qlt_init(void)
6880{
6881 int ret;
6882
6883 if (!qlt_parse_ini_mode()) {
6884 ql_log(ql_log_fatal, NULL, 0xe06b,
6885 "qlt_parse_ini_mode() failed\n");
6886 return -EINVAL;
6887 }
6888
6889 if (!QLA_TGT_MODE_ENABLED())
6890 return 0;
6891
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006892 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6893 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6894 qla_tgt_mgmt_cmd), 0, NULL);
6895 if (!qla_tgt_mgmt_cmd_cachep) {
6896 ql_log(ql_log_fatal, NULL, 0xe06d,
6897 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07006898 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006899 }
6900
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006901 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
6902 sizeof(qlt_plogi_ack_t),
6903 __alignof__(qlt_plogi_ack_t),
6904 0, NULL);
6905
6906 if (!qla_tgt_plogi_cachep) {
6907 ql_log(ql_log_fatal, NULL, 0xe06d,
6908 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6909 ret = -ENOMEM;
6910 goto out_mgmt_cmd_cachep;
6911 }
6912
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006913 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6914 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6915 if (!qla_tgt_mgmt_cmd_mempool) {
6916 ql_log(ql_log_fatal, NULL, 0xe06e,
6917 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6918 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006919 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006920 }
6921
6922 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6923 if (!qla_tgt_wq) {
6924 ql_log(ql_log_fatal, NULL, 0xe06f,
6925 "alloc_workqueue for qla_tgt_wq failed\n");
6926 ret = -ENOMEM;
6927 goto out_cmd_mempool;
6928 }
6929 /*
6930 * Return 1 to signal that initiator-mode is being disabled
6931 */
6932 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6933
6934out_cmd_mempool:
6935 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006936out_plogi_cachep:
6937 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006938out_mgmt_cmd_cachep:
6939 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006940 return ret;
6941}
6942
6943void qlt_exit(void)
6944{
6945 if (!QLA_TGT_MODE_ENABLED())
6946 return;
6947
6948 destroy_workqueue(qla_tgt_wq);
6949 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006950 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006951 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006952}