blob: 985231900aca4222792f37d7cd4da49e136fee06 [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
Quinn Tran36c78452016-02-04 11:45:18 -0500644 if (sess->se_sess)
645 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400646
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400647 if (!list_empty(&sess->del_list_entry))
648 list_del_init(&sess->del_list_entry);
649 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400650
651 INIT_WORK(&sess->free_work, qlt_free_session_done);
652 schedule_work(&sess->free_work);
653}
654EXPORT_SYMBOL(qlt_unreg_sess);
655
Quinn Tran75601512015-12-17 14:57:04 -0500656
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400657static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
658{
659 struct qla_hw_data *ha = vha->hw;
660 struct qla_tgt_sess *sess = NULL;
661 uint32_t unpacked_lun, lun = 0;
662 uint16_t loop_id;
663 int res = 0;
664 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
665 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Quinn Tran75601512015-12-17 14:57:04 -0500666 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400667
668 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
669 if (loop_id == 0xFFFF) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400670 /* Global event */
Roland Dreierb2032fd2015-07-14 16:00:42 -0400671 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Quinn Tran75601512015-12-17 14:57:04 -0500672 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400673 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
Quinn Tran75601512015-12-17 14:57:04 -0500674 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Roland Dreierb2032fd2015-07-14 16:00:42 -0400675#if 0 /* FIXME: do we need to choose a session here? */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400676 if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
677 sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
678 typeof(*sess), sess_list_entry);
679 switch (mcmd) {
680 case QLA_TGT_NEXUS_LOSS_SESS:
681 mcmd = QLA_TGT_NEXUS_LOSS;
682 break;
683 case QLA_TGT_ABORT_ALL_SESS:
684 mcmd = QLA_TGT_ABORT_ALL;
685 break;
686 case QLA_TGT_NEXUS_LOSS:
687 case QLA_TGT_ABORT_ALL:
688 break;
689 default:
690 ql_dbg(ql_dbg_tgt, vha, 0xe046,
691 "qla_target(%d): Not allowed "
692 "command %x in %s", vha->vp_idx,
693 mcmd, __func__);
694 sess = NULL;
695 break;
696 }
697 } else
698 sess = NULL;
699#endif
700 } else {
Quinn Tran75601512015-12-17 14:57:04 -0500701 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400702 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -0500703 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400704 }
705
706 ql_dbg(ql_dbg_tgt, vha, 0xe000,
707 "Using sess for qla_tgt_reset: %p\n", sess);
708 if (!sess) {
709 res = -ESRCH;
710 return res;
711 }
712
713 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400714 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
715 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400716 mcmd, loop_id);
717
718 lun = a->u.isp24.fcp_cmnd.lun;
719 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
720
721 return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
722 iocb, QLA24XX_MGMT_SEND_NACK);
723}
724
Quinn Tran75601512015-12-17 14:57:04 -0500725/* ha->tgt.sess_lock supposed to be held on entry */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400726static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
727 bool immediate)
728{
729 struct qla_tgt *tgt = sess->tgt;
730 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
731
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400732 if (sess->deleted) {
733 /* Upgrade to unconditional deletion in case it was temporary */
734 if (immediate && sess->deleted == QLA_SESS_DELETION_PENDING)
735 list_del(&sess->del_list_entry);
736 else
737 return;
738 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400739
740 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
741 "Scheduling sess %p for deletion\n", sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400742
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400743 if (immediate) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400744 dev_loss_tmo = 0;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400745 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
746 list_add(&sess->del_list_entry, &tgt->del_sess_list);
747 } else {
748 sess->deleted = QLA_SESS_DELETION_PENDING;
749 list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
750 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400751
752 sess->expires = jiffies + dev_loss_tmo * HZ;
753
754 ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
Alexei Potashnikdf673272015-07-14 16:00:46 -0400755 "qla_target(%d): session for port %8phC (loop ID %d s_id %02x:%02x:%02x)"
756 " scheduled for deletion in %u secs (expires: %lu) immed: %d, logout: %d, gen: %#x\n",
757 sess->vha->vp_idx, sess->port_name, sess->loop_id,
758 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
759 dev_loss_tmo, sess->expires, immediate, sess->logout_on_delete,
760 sess->generation);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400761
762 if (immediate)
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400763 mod_delayed_work(system_wq, &tgt->sess_del_work, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400764 else
765 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530766 sess->expires - jiffies);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400767}
768
Quinn Tran75601512015-12-17 14:57:04 -0500769/* ha->tgt.sess_lock supposed to be held on entry */
Joern Engelc5701042014-09-16 16:23:14 -0400770static void qlt_clear_tgt_db(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400771{
772 struct qla_tgt_sess *sess;
773
774 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
775 qlt_schedule_sess_for_deletion(sess, true);
776
777 /* At this point tgt could be already dead */
778}
779
780static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
781 uint16_t *loop_id)
782{
783 struct qla_hw_data *ha = vha->hw;
784 dma_addr_t gid_list_dma;
785 struct gid_list_info *gid_list;
786 char *id_iter;
787 int res, rc, i;
788 uint16_t entries;
789
790 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
791 &gid_list_dma, GFP_KERNEL);
792 if (!gid_list) {
793 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
794 "qla_target(%d): DMA Alloc failed of %u\n",
795 vha->vp_idx, qla2x00_gid_list_size(ha));
796 return -ENOMEM;
797 }
798
799 /* Get list of logged in devices */
800 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
801 if (rc != QLA_SUCCESS) {
802 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
803 "qla_target(%d): get_id_list() failed: %x\n",
804 vha->vp_idx, rc);
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500805 res = -EBUSY;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400806 goto out_free_id_list;
807 }
808
809 id_iter = (char *)gid_list;
Alexei Potashnik71cdc072015-12-17 14:57:01 -0500810 res = -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400811 for (i = 0; i < entries; i++) {
812 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
813 if ((gid->al_pa == s_id[2]) &&
814 (gid->area == s_id[1]) &&
815 (gid->domain == s_id[0])) {
816 *loop_id = le16_to_cpu(gid->loop_id);
817 res = 0;
818 break;
819 }
820 id_iter += ha->gid_list_info_size;
821 }
822
823out_free_id_list:
824 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
825 gid_list, gid_list_dma);
826 return res;
827}
828
Quinn Tran75601512015-12-17 14:57:04 -0500829/* ha->tgt.sess_lock supposed to be held on entry */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400830static void qlt_undelete_sess(struct qla_tgt_sess *sess)
831{
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400832 BUG_ON(sess->deleted != QLA_SESS_DELETION_PENDING);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400833
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400834 list_del_init(&sess->del_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400835 sess->deleted = 0;
836}
837
838static void qlt_del_sess_work_fn(struct delayed_work *work)
839{
840 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
841 sess_del_work);
842 struct scsi_qla_host *vha = tgt->vha;
843 struct qla_hw_data *ha = vha->hw;
844 struct qla_tgt_sess *sess;
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530845 unsigned long flags, elapsed;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400846
Quinn Tran75601512015-12-17 14:57:04 -0500847 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400848 while (!list_empty(&tgt->del_sess_list)) {
849 sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
850 del_list_entry);
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530851 elapsed = jiffies;
852 if (time_after_eq(elapsed, sess->expires)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400853 /* No turning back */
854 list_del_init(&sess->del_list_entry);
855 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400856
Jörn Engel08234e32013-06-12 16:27:54 -0400857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
858 "Timeout: sess %p about to be deleted\n",
859 sess);
Quinn Tran36c78452016-02-04 11:45:18 -0500860 if (sess->se_sess) {
861 ha->tgt.tgt_ops->shutdown_sess(sess);
862 ha->tgt.tgt_ops->put_sess(sess);
863 } else {
864 qlt_unreg_sess(sess);
865 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400866 } else {
867 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530868 sess->expires - elapsed);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400869 break;
870 }
871 }
Quinn Tran75601512015-12-17 14:57:04 -0500872 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400873}
874
875/*
876 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
877 * Caller must put it.
878 */
879static struct qla_tgt_sess *qlt_create_sess(
880 struct scsi_qla_host *vha,
881 fc_port_t *fcport,
882 bool local)
883{
884 struct qla_hw_data *ha = vha->hw;
885 struct qla_tgt_sess *sess;
886 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400887
888 /* Check to avoid double sessions */
Quinn Tran75601512015-12-17 14:57:04 -0500889 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800890 list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400891 sess_list_entry) {
892 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
893 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
894 "Double sess %p found (s_id %x:%x:%x, "
895 "loop_id %d), updating to d_id %x:%x:%x, "
896 "loop_id %d", sess, sess->s_id.b.domain,
897 sess->s_id.b.al_pa, sess->s_id.b.area,
898 sess->loop_id, fcport->d_id.b.domain,
899 fcport->d_id.b.al_pa, fcport->d_id.b.area,
900 fcport->loop_id);
901
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400902 /* Cannot undelete at this point */
903 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
Quinn Tran75601512015-12-17 14:57:04 -0500904 spin_unlock_irqrestore(&ha->tgt.sess_lock,
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400905 flags);
906 return NULL;
907 }
908
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400909 if (sess->deleted)
910 qlt_undelete_sess(sess);
911
Quinn Tran36c78452016-02-04 11:45:18 -0500912 if (!sess->se_sess) {
913 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
914 &sess->port_name[0], sess) < 0) {
915 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
916 return NULL;
917 }
918 }
919
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400920 kref_get(&sess->se_sess->sess_kref);
Roland Dreierc8292d12012-10-11 13:41:32 -0700921 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
922 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
923
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400924 if (sess->local && !local)
925 sess->local = 0;
Alexei Potashnikdf673272015-07-14 16:00:46 -0400926
927 qlt_do_generation_tick(vha, &sess->generation);
928
Quinn Tran75601512015-12-17 14:57:04 -0500929 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400930
931 return sess;
932 }
933 }
Quinn Tran75601512015-12-17 14:57:04 -0500934 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400935
936 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
937 if (!sess) {
938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400939 "qla_target(%u): session allocation failed, all commands "
940 "from port %8phC will be refused", vha->vp_idx,
941 fcport->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400942
943 return NULL;
944 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800945 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400946 sess->vha = vha;
947 sess->s_id = fcport->d_id;
948 sess->loop_id = fcport->loop_id;
949 sess->local = local;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400950 INIT_LIST_HEAD(&sess->del_list_entry);
951
952 /* Under normal circumstances we want to logout from firmware when
953 * session eventually ends and release corresponding nport handle.
954 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
955 * code will adjust these flags as necessary. */
956 sess->logout_on_delete = 1;
957 sess->keep_nport_handle = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400958
959 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
960 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800961 sess, vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400962
Roland Dreierc8292d12012-10-11 13:41:32 -0700963 sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400964 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
965 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
966
Quinn Tran75601512015-12-17 14:57:04 -0500967 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800968 list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
969 vha->vha_tgt.qla_tgt->sess_count++;
Alexei Potashnikdf673272015-07-14 16:00:46 -0400970 qlt_do_generation_tick(vha, &sess->generation);
Quinn Tran75601512015-12-17 14:57:04 -0500971 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400972
973 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400974 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
975 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
976 vha->vp_idx, local ? "local " : "", fcport->port_name,
977 fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
978 sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400979
Quinn Tran36c78452016-02-04 11:45:18 -0500980 /*
981 * Determine if this fc_port->port_name is allowed to access
982 * target mode using explict NodeACLs+MappedLUNs, or using
983 * TPG demo mode. If this is successful a target mode FC nexus
984 * is created.
985 */
986 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
987 &fcport->port_name[0], sess) < 0) {
988 return NULL;
989 } else {
990 /*
991 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
992 * access across ->tgt.sess_lock reaquire.
993 */
994 kref_get(&sess->se_sess->sess_kref);
995 }
996
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400997 return sess;
998}
999
1000/*
Alexei Potashnikdf673272015-07-14 16:00:46 -04001001 * Called from qla2x00_reg_remote_port()
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001002 */
1003void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
1004{
1005 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001006 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001007 struct qla_tgt_sess *sess;
1008 unsigned long flags;
1009
1010 if (!vha->hw->tgt.tgt_ops)
1011 return;
1012
1013 if (!tgt || (fcport->port_type != FCT_INITIATOR))
1014 return;
1015
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001016 if (qla_ini_mode_enabled(vha))
1017 return;
1018
Quinn Tran75601512015-12-17 14:57:04 -05001019 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001020 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001021 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001022 return;
1023 }
1024 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
1025 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001026 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001027
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001028 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001029 sess = qlt_create_sess(vha, fcport, false);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001030 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001031
Quinn Tran75601512015-12-17 14:57:04 -05001032 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001033 } else if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1034 /* Point of no return */
Quinn Tran75601512015-12-17 14:57:04 -05001035 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001036 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001037 } else {
1038 kref_get(&sess->se_sess->sess_kref);
1039
1040 if (sess->deleted) {
1041 qlt_undelete_sess(sess);
1042
1043 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001044 "qla_target(%u): %ssession for port %8phC "
1045 "(loop ID %d) reappeared\n", vha->vp_idx,
1046 sess->local ? "local " : "", sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001047 sess->loop_id);
1048
1049 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
1050 "Reappeared sess %p\n", sess);
1051 }
Roland Dreierc8292d12012-10-11 13:41:32 -07001052 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
1053 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001054 }
1055
1056 if (sess && sess->local) {
1057 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
1058 "qla_target(%u): local session for "
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001059 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
1060 fcport->port_name, sess->loop_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001061 sess->local = 0;
1062 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001063 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05001064 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001065}
1066
Alexei Potashnikdf673272015-07-14 16:00:46 -04001067/*
1068 * max_gen - specifies maximum session generation
1069 * at which this deletion requestion is still valid
1070 */
1071void
1072qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001073{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001074 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001075 struct qla_tgt_sess *sess;
Quinn Tran75601512015-12-17 14:57:04 -05001076 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001077
1078 if (!vha->hw->tgt.tgt_ops)
1079 return;
1080
Roland Dreierb2032fd2015-07-14 16:00:42 -04001081 if (!tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001082 return;
1083
Quinn Tran75601512015-12-17 14:57:04 -05001084 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001085 if (tgt->tgt_stop) {
Quinn Tran75601512015-12-17 14:57:04 -05001086 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001087 return;
1088 }
1089 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
1090 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05001091 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001092 return;
1093 }
1094
Alexei Potashnikdf673272015-07-14 16:00:46 -04001095 if (max_gen - sess->generation < 0) {
Quinn Tran75601512015-12-17 14:57:04 -05001096 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Alexei Potashnikdf673272015-07-14 16:00:46 -04001097 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1098 "Ignoring stale deletion request for se_sess %p / sess %p"
1099 " for port %8phC, req_gen %d, sess_gen %d\n",
1100 sess->se_sess, sess, sess->port_name, max_gen,
1101 sess->generation);
1102 return;
1103 }
1104
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001105 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1106
1107 sess->local = 1;
1108 qlt_schedule_sess_for_deletion(sess, false);
Quinn Tran75601512015-12-17 14:57:04 -05001109 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001110}
1111
1112static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1113{
1114 struct qla_hw_data *ha = tgt->ha;
1115 unsigned long flags;
1116 int res;
1117 /*
1118 * We need to protect against race, when tgt is freed before or
1119 * inside wake_up()
1120 */
1121 spin_lock_irqsave(&ha->hardware_lock, flags);
1122 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1123 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
1124 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
1125 res = (tgt->sess_count == 0);
1126 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1127
1128 return res;
1129}
1130
1131/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001132int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001133{
1134 struct scsi_qla_host *vha = tgt->vha;
1135 struct qla_hw_data *ha = tgt->ha;
1136 unsigned long flags;
1137
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001138 mutex_lock(&qla_tgt_mutex);
1139 if (!vha->fc_vport) {
1140 struct Scsi_Host *sh = vha->host;
1141 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
1142 bool npiv_vports;
1143
1144 spin_lock_irqsave(sh->host_lock, flags);
1145 npiv_vports = (fc_host->npiv_vports_inuse);
1146 spin_unlock_irqrestore(sh->host_lock, flags);
1147
1148 if (npiv_vports) {
1149 mutex_unlock(&qla_tgt_mutex);
1150 return -EPERM;
1151 }
1152 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001153 if (tgt->tgt_stop || tgt->tgt_stopped) {
1154 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1155 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001156 mutex_unlock(&qla_tgt_mutex);
1157 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001158 }
1159
1160 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1161 vha->host_no, vha);
1162 /*
1163 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1164 * Lock is needed, because we still can get an incoming packet.
1165 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001166 mutex_lock(&vha->vha_tgt.tgt_mutex);
Quinn Tran75601512015-12-17 14:57:04 -05001167 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001168 tgt->tgt_stop = 1;
Joern Engelc5701042014-09-16 16:23:14 -04001169 qlt_clear_tgt_db(tgt);
Quinn Tran75601512015-12-17 14:57:04 -05001170 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001171 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001172 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001173
Tejun Heo43829732012-08-20 14:51:24 -07001174 flush_delayed_work(&tgt->sess_del_work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001175
1176 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1177 "Waiting for sess works (tgt %p)", tgt);
1178 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1179 while (!list_empty(&tgt->sess_works_list)) {
1180 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1181 flush_scheduled_work();
1182 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1183 }
1184 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1185
1186 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1187 "Waiting for tgt %p: list_empty(sess_list)=%d "
1188 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
1189 tgt->sess_count);
1190
1191 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
1192
1193 /* Big hammer */
1194 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
1195 qlt_disable_vha(vha);
1196
1197 /* Wait for sessions to clear out (just in case) */
1198 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -08001199 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001200}
1201EXPORT_SYMBOL(qlt_stop_phase1);
1202
1203/* Called by tcm_qla2xxx configfs code */
1204void qlt_stop_phase2(struct qla_tgt *tgt)
1205{
1206 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001207 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001208 unsigned long flags;
1209
1210 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001211 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001212 "Already in tgt->tgt_stopped state\n");
1213 dump_stack();
1214 return;
1215 }
1216
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001217 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001218 "Waiting for %d IRQ commands to complete (tgt %p)",
1219 tgt->irq_cmd_count, tgt);
1220
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001221 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001222 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran2f424b92015-12-17 14:57:07 -05001223 while ((tgt->irq_cmd_count != 0) || (tgt->atio_irq_cmd_count != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001224 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1225 udelay(2);
1226 spin_lock_irqsave(&ha->hardware_lock, flags);
1227 }
1228 tgt->tgt_stop = 0;
1229 tgt->tgt_stopped = 1;
1230 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001231 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001232
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001233 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001234 tgt);
1235}
1236EXPORT_SYMBOL(qlt_stop_phase2);
1237
1238/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001239static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001240{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001241 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001242
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001243 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001244 qlt_stop_phase2(tgt);
1245
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001246 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001247
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001248 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001249 "Release of tgt %p finished\n", tgt);
1250
1251 kfree(tgt);
1252}
1253
1254/* ha->hardware_lock supposed to be held on entry */
1255static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1256 const void *param, unsigned int param_size)
1257{
1258 struct qla_tgt_sess_work_param *prm;
1259 unsigned long flags;
1260
1261 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1262 if (!prm) {
1263 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1264 "qla_target(%d): Unable to create session "
1265 "work, command will be refused", 0);
1266 return -ENOMEM;
1267 }
1268
1269 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1270 "Scheduling work (type %d, prm %p)"
1271 " to find session for param %p (size %d, tgt %p)\n",
1272 type, prm, param, param_size, tgt);
1273
1274 prm->type = type;
1275 memcpy(&prm->tm_iocb, param, param_size);
1276
1277 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1278 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1279 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1280
1281 schedule_work(&tgt->sess_work);
1282
1283 return 0;
1284}
1285
1286/*
1287 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1288 */
1289static void qlt_send_notify_ack(struct scsi_qla_host *vha,
1290 struct imm_ntfy_from_isp *ntfy,
1291 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1292 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1293{
1294 struct qla_hw_data *ha = vha->hw;
1295 request_t *pkt;
1296 struct nack_to_isp *nack;
1297
1298 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1299
1300 /* Send marker if required */
1301 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1302 return;
1303
1304 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1305 if (!pkt) {
1306 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1307 "qla_target(%d): %s failed: unable to allocate "
1308 "request packet\n", vha->vp_idx, __func__);
1309 return;
1310 }
1311
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001312 if (vha->vha_tgt.qla_tgt != NULL)
1313 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001314
1315 pkt->entry_type = NOTIFY_ACK_TYPE;
1316 pkt->entry_count = 1;
1317
1318 nack = (struct nack_to_isp *)pkt;
1319 nack->ox_id = ntfy->ox_id;
1320
1321 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1322 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1323 nack->u.isp24.flags = ntfy->u.isp24.flags &
Bart Van Asschead950362015-07-09 07:24:08 -07001324 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001325 }
1326 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1327 nack->u.isp24.status = ntfy->u.isp24.status;
1328 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001329 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001330 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1331 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1332 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1333 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1334 nack->u.isp24.srr_reject_code = srr_reject_code;
1335 nack->u.isp24.srr_reject_code_expl = srr_explan;
1336 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1337
1338 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1339 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1340 vha->vp_idx, nack->u.isp24.status);
1341
Himanshu Madhani63163e02014-09-25 06:14:59 -04001342 /* Memory Barrier */
1343 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001344 qla2x00_start_iocbs(vha, vha->req);
1345}
1346
1347/*
1348 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1349 */
1350static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1351 struct abts_recv_from_24xx *abts, uint32_t status,
1352 bool ids_reversed)
1353{
1354 struct qla_hw_data *ha = vha->hw;
1355 struct abts_resp_to_24xx *resp;
1356 uint32_t f_ctl;
1357 uint8_t *p;
1358
1359 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1360 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1361 ha, abts, status);
1362
1363 /* Send marker if required */
1364 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1365 return;
1366
Arun Easib6a029e2014-09-25 06:14:52 -04001367 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001368 if (!resp) {
1369 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1370 "qla_target(%d): %s failed: unable to allocate "
1371 "request packet", vha->vp_idx, __func__);
1372 return;
1373 }
1374
1375 resp->entry_type = ABTS_RESP_24XX;
1376 resp->entry_count = 1;
1377 resp->nport_handle = abts->nport_handle;
1378 resp->vp_index = vha->vp_idx;
1379 resp->sof_type = abts->sof_type;
1380 resp->exchange_address = abts->exchange_address;
1381 resp->fcp_hdr_le = abts->fcp_hdr_le;
Bart Van Asschead950362015-07-09 07:24:08 -07001382 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001383 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1384 F_CTL_SEQ_INITIATIVE);
1385 p = (uint8_t *)&f_ctl;
1386 resp->fcp_hdr_le.f_ctl[0] = *p++;
1387 resp->fcp_hdr_le.f_ctl[1] = *p++;
1388 resp->fcp_hdr_le.f_ctl[2] = *p;
1389 if (ids_reversed) {
1390 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1391 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1392 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1393 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1394 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1395 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1396 } else {
1397 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1398 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1399 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1400 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1401 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1402 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1403 }
1404 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1405 if (status == FCP_TMF_CMPL) {
1406 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1407 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1408 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1409 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1410 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1411 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1412 } else {
1413 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1414 resp->payload.ba_rjt.reason_code =
1415 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1416 /* Other bytes are zero */
1417 }
1418
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001419 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001420
Himanshu Madhani63163e02014-09-25 06:14:59 -04001421 /* Memory Barrier */
1422 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001423 qla2x00_start_iocbs(vha, vha->req);
1424}
1425
1426/*
1427 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1428 */
1429static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1430 struct abts_resp_from_24xx_fw *entry)
1431{
1432 struct ctio7_to_24xx *ctio;
1433
1434 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1435 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1436 /* Send marker if required */
1437 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1438 return;
1439
Arun Easib6a029e2014-09-25 06:14:52 -04001440 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001441 if (ctio == NULL) {
1442 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1443 "qla_target(%d): %s failed: unable to allocate "
1444 "request packet\n", vha->vp_idx, __func__);
1445 return;
1446 }
1447
1448 /*
1449 * We've got on entrance firmware's response on by us generated
1450 * ABTS response. So, in it ID fields are reversed.
1451 */
1452
1453 ctio->entry_type = CTIO_TYPE7;
1454 ctio->entry_count = 1;
1455 ctio->nport_handle = entry->nport_handle;
1456 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07001457 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001458 ctio->vp_index = vha->vp_idx;
1459 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1460 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1461 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1462 ctio->exchange_addr = entry->exchange_addr_to_abort;
Bart Van Asschead950362015-07-09 07:24:08 -07001463 ctio->u.status1.flags = cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1464 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001465 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001466
Himanshu Madhani63163e02014-09-25 06:14:59 -04001467 /* Memory Barrier */
1468 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001469 qla2x00_start_iocbs(vha, vha->req);
1470
1471 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1472 FCP_TMF_CMPL, true);
1473}
1474
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001475static int abort_cmd_for_tag(struct scsi_qla_host *vha, uint32_t tag)
1476{
1477 struct qla_tgt_sess_op *op;
1478 struct qla_tgt_cmd *cmd;
1479
1480 spin_lock(&vha->cmd_list_lock);
1481
1482 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1483 if (tag == op->atio.u.isp24.exchange_addr) {
1484 op->aborted = true;
1485 spin_unlock(&vha->cmd_list_lock);
1486 return 1;
1487 }
1488 }
1489
1490 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1491 if (tag == cmd->atio.u.isp24.exchange_addr) {
Quinn Tran193b50b2015-12-17 14:57:03 -05001492 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001493 spin_unlock(&vha->cmd_list_lock);
1494 return 1;
1495 }
1496 }
1497
1498 spin_unlock(&vha->cmd_list_lock);
1499 return 0;
1500}
1501
1502/* drop cmds for the given lun
1503 * XXX only looks for cmds on the port through which lun reset was recieved
1504 * XXX does not go through the list of other port (which may have cmds
1505 * for the same lun)
1506 */
1507static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1508 uint32_t lun, uint8_t *s_id)
1509{
1510 struct qla_tgt_sess_op *op;
1511 struct qla_tgt_cmd *cmd;
1512 uint32_t key;
1513
1514 key = sid_to_key(s_id);
1515 spin_lock(&vha->cmd_list_lock);
1516 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1517 uint32_t op_key;
1518 uint32_t op_lun;
1519
1520 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1521 op_lun = scsilun_to_int(
1522 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1523 if (op_key == key && op_lun == lun)
1524 op->aborted = true;
1525 }
1526 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
1527 uint32_t cmd_key;
1528 uint32_t cmd_lun;
1529
1530 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
1531 cmd_lun = scsilun_to_int(
1532 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
1533 if (cmd_key == key && cmd_lun == lun)
Quinn Tran193b50b2015-12-17 14:57:03 -05001534 cmd->aborted = 1;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001535 }
1536 spin_unlock(&vha->cmd_list_lock);
1537}
1538
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001539/* ha->hardware_lock supposed to be held on entry */
1540static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1541 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1542{
1543 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001544 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001545 struct qla_tgt_mgmt_cmd *mcmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001546 struct se_cmd *se_cmd;
1547 u32 lun = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001548 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001549 bool found_lun = false;
1550
1551 spin_lock(&se_sess->sess_cmd_lock);
1552 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1553 struct qla_tgt_cmd *cmd =
1554 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
Bart Van Assche649ee052015-04-14 13:26:44 +02001555 if (se_cmd->tag == abts->exchange_addr_to_abort) {
Steve Hodgson06e97b42012-11-16 08:06:17 -08001556 lun = cmd->unpacked_lun;
1557 found_lun = true;
1558 break;
1559 }
1560 }
1561 spin_unlock(&se_sess->sess_cmd_lock);
1562
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04001563 /* cmd not in LIO lists, look in qla list */
1564 if (!found_lun) {
1565 if (abort_cmd_for_tag(vha, abts->exchange_addr_to_abort)) {
1566 /* send TASK_ABORT response immediately */
1567 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_CMPL, false);
1568 return 0;
1569 } else {
1570 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf081,
1571 "unable to find cmd in driver or LIO for tag 0x%x\n",
1572 abts->exchange_addr_to_abort);
1573 return -ENOENT;
1574 }
1575 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001576
1577 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1578 "qla_target(%d): task abort (tag=%d)\n",
1579 vha->vp_idx, abts->exchange_addr_to_abort);
1580
1581 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1582 if (mcmd == NULL) {
1583 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1584 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1585 vha->vp_idx, __func__);
1586 return -ENOMEM;
1587 }
1588 memset(mcmd, 0, sizeof(*mcmd));
1589
1590 mcmd->sess = sess;
1591 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Arun Easi80187f82014-09-25 06:14:53 -04001592 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001593
Steve Hodgson06e97b42012-11-16 08:06:17 -08001594 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001595 abts->exchange_addr_to_abort);
1596 if (rc != 0) {
1597 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1598 "qla_target(%d): tgt_ops->handle_tmr()"
1599 " failed: %d", vha->vp_idx, rc);
1600 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1601 return -EFAULT;
1602 }
1603
1604 return 0;
1605}
1606
1607/*
1608 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1609 */
1610static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1611 struct abts_recv_from_24xx *abts)
1612{
1613 struct qla_hw_data *ha = vha->hw;
1614 struct qla_tgt_sess *sess;
1615 uint32_t tag = abts->exchange_addr_to_abort;
1616 uint8_t s_id[3];
1617 int rc;
Quinn Tran75601512015-12-17 14:57:04 -05001618 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001619
1620 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1621 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1622 "qla_target(%d): ABTS: Abort Sequence not "
1623 "supported\n", vha->vp_idx);
1624 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1625 return;
1626 }
1627
1628 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1629 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1630 "qla_target(%d): ABTS: Unknown Exchange "
1631 "Address received\n", vha->vp_idx);
1632 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1633 return;
1634 }
1635
1636 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1637 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1638 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1639 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1640 le32_to_cpu(abts->fcp_hdr_le.parameter));
1641
1642 s_id[0] = abts->fcp_hdr_le.s_id[2];
1643 s_id[1] = abts->fcp_hdr_le.s_id[1];
1644 s_id[2] = abts->fcp_hdr_le.s_id[0];
1645
Quinn Tran75601512015-12-17 14:57:04 -05001646 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001647 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1648 if (!sess) {
1649 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1650 "qla_target(%d): task abort for non-existant session\n",
1651 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001652 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001653 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
Quinn Tran75601512015-12-17 14:57:04 -05001654
1655 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1656
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001657 if (rc != 0) {
1658 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1659 false);
1660 }
1661 return;
1662 }
Quinn Tran75601512015-12-17 14:57:04 -05001663 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1664
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001665
Alexei Potashnike52a8b42015-07-14 16:00:48 -04001666 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1667 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1668 return;
1669 }
1670
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001671 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1672 if (rc != 0) {
1673 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1674 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1675 vha->vp_idx, rc);
1676 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1677 return;
1678 }
1679}
1680
1681/*
1682 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1683 */
1684static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1685 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1686{
1687 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1688 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001689 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001690
1691 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1692 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1693 ha, atio, resp_code);
1694
1695 /* Send marker if required */
1696 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1697 return;
1698
1699 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1700 if (ctio == NULL) {
1701 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1702 "qla_target(%d): %s failed: unable to allocate "
1703 "request packet\n", ha->vp_idx, __func__);
1704 return;
1705 }
1706
1707 ctio->entry_type = CTIO_TYPE7;
1708 ctio->entry_count = 1;
1709 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1710 ctio->nport_handle = mcmd->sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07001711 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001712 ctio->vp_index = ha->vp_idx;
1713 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1714 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1715 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1716 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1717 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07001718 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001719 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1720 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001721 ctio->u.status1.scsi_status =
Bart Van Asschead950362015-07-09 07:24:08 -07001722 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1723 ctio->u.status1.response_len = cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07001724 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001725
Himanshu Madhani63163e02014-09-25 06:14:59 -04001726 /* Memory Barrier */
1727 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001728 qla2x00_start_iocbs(ha, ha->req);
1729}
1730
1731void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1732{
1733 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1734}
1735EXPORT_SYMBOL(qlt_free_mcmd);
1736
1737/* callback from target fabric module code */
1738void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1739{
1740 struct scsi_qla_host *vha = mcmd->sess->vha;
1741 struct qla_hw_data *ha = vha->hw;
1742 unsigned long flags;
1743
1744 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1745 "TM response mcmd (%p) status %#x state %#x",
1746 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1747
1748 spin_lock_irqsave(&ha->hardware_lock, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04001749
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001750 if (!vha->flags.online || mcmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04001751 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001752 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04001753 * previous life, just abort the processing.
1754 */
1755 ql_dbg(ql_dbg_async, vha, 0xe100,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05001756 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
1757 vha->flags.online, qla2x00_reset_active(vha),
1758 mcmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04001759 ha->tgt.tgt_ops->free_mcmd(mcmd);
1760 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1761 return;
1762 }
1763
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001764 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1765 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1766 0, 0, 0, 0, 0, 0);
1767 else {
Swapnil Nagled7236ac2016-02-04 11:45:17 -05001768 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001769 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1770 mcmd->fc_tm_rsp, false);
1771 else
1772 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1773 mcmd->fc_tm_rsp);
1774 }
1775 /*
1776 * Make the callback for ->free_mcmd() to queue_work() and invoke
1777 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1778 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1779 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1780 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1781 * qlt_xmit_tm_rsp() returns here..
1782 */
1783 ha->tgt.tgt_ops->free_mcmd(mcmd);
1784 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1785}
1786EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1787
1788/* No locks */
1789static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1790{
1791 struct qla_tgt_cmd *cmd = prm->cmd;
1792
1793 BUG_ON(cmd->sg_cnt == 0);
1794
1795 prm->sg = (struct scatterlist *)cmd->sg;
1796 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1797 cmd->sg_cnt, cmd->dma_data_direction);
1798 if (unlikely(prm->seg_cnt == 0))
1799 goto out_err;
1800
1801 prm->cmd->sg_mapped = 1;
1802
Quinn Tranf83adb62014-04-11 16:54:43 -04001803 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
1804 /*
1805 * If greater than four sg entries then we need to allocate
1806 * the continuation entries
1807 */
1808 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1809 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1810 prm->tgt->datasegs_per_cmd,
1811 prm->tgt->datasegs_per_cont);
1812 } else {
1813 /* DIF */
1814 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1815 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1816 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
1817 prm->tot_dsds = prm->seg_cnt;
1818 } else
1819 prm->tot_dsds = prm->seg_cnt;
1820
1821 if (cmd->prot_sg_cnt) {
1822 prm->prot_sg = cmd->prot_sg;
1823 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
1824 cmd->prot_sg, cmd->prot_sg_cnt,
1825 cmd->dma_data_direction);
1826 if (unlikely(prm->prot_seg_cnt == 0))
1827 goto out_err;
1828
1829 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1830 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1831 /* Dif Bundling not support here */
1832 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
1833 cmd->blk_sz);
1834 prm->tot_dsds += prm->prot_seg_cnt;
1835 } else
1836 prm->tot_dsds += prm->prot_seg_cnt;
1837 }
1838 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001839
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001840 return 0;
1841
1842out_err:
1843 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1844 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1845 0, prm->cmd->sg_cnt);
1846 return -1;
1847}
1848
Joern Engelf9b67212014-09-16 16:23:18 -04001849static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001850{
1851 struct qla_hw_data *ha = vha->hw;
1852
Joern Engelf9b67212014-09-16 16:23:18 -04001853 if (!cmd->sg_mapped)
1854 return;
1855
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001856 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1857 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04001858
1859 if (cmd->prot_sg_cnt)
1860 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
1861 cmd->dma_data_direction);
1862
1863 if (cmd->ctx_dsd_alloced)
1864 qla2x00_clean_dsd_pool(ha, NULL, cmd);
1865
1866 if (cmd->ctx)
1867 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001868}
1869
1870static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1871 uint32_t req_cnt)
1872{
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001873 uint32_t cnt, cnt_in;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001874
1875 if (vha->req->cnt < (req_cnt + 2)) {
Arun Easi75554b62014-09-25 06:14:45 -04001876 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001877 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001878
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001879 if (vha->req->ring_index < cnt)
1880 vha->req->cnt = cnt - vha->req->ring_index;
1881 else
1882 vha->req->cnt = vha->req->length -
1883 (vha->req->ring_index - cnt);
1884 }
1885
1886 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
Arun Easi667024a2014-09-25 06:14:47 -04001887 ql_dbg(ql_dbg_io, vha, 0x305a,
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001888 "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",
1889 vha->vp_idx, vha->req->ring_index,
1890 vha->req->cnt, req_cnt, cnt, cnt_in, vha->req->length);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001891 return -EAGAIN;
1892 }
1893 vha->req->cnt -= req_cnt;
1894
1895 return 0;
1896}
1897
1898/*
1899 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1900 */
1901static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1902{
1903 /* Adjust ring index. */
1904 vha->req->ring_index++;
1905 if (vha->req->ring_index == vha->req->length) {
1906 vha->req->ring_index = 0;
1907 vha->req->ring_ptr = vha->req->ring;
1908 } else {
1909 vha->req->ring_ptr++;
1910 }
1911 return (cont_entry_t *)vha->req->ring_ptr;
1912}
1913
1914/* ha->hardware_lock supposed to be held on entry */
1915static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1916{
1917 struct qla_hw_data *ha = vha->hw;
1918 uint32_t h;
1919
1920 h = ha->tgt.current_handle;
1921 /* always increment cmd handle */
1922 do {
1923 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001924 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001925 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1926 if (h == ha->tgt.current_handle) {
Arun Easi667024a2014-09-25 06:14:47 -04001927 ql_dbg(ql_dbg_io, vha, 0x305b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001928 "qla_target(%d): Ran out of "
1929 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1930 h = QLA_TGT_NULL_HANDLE;
1931 break;
1932 }
1933 } while ((h == QLA_TGT_NULL_HANDLE) ||
1934 (h == QLA_TGT_SKIP_HANDLE) ||
1935 (ha->tgt.cmds[h-1] != NULL));
1936
1937 if (h != QLA_TGT_NULL_HANDLE)
1938 ha->tgt.current_handle = h;
1939
1940 return h;
1941}
1942
1943/* ha->hardware_lock supposed to be held on entry */
1944static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1945 struct scsi_qla_host *vha)
1946{
1947 uint32_t h;
1948 struct ctio7_to_24xx *pkt;
1949 struct qla_hw_data *ha = vha->hw;
1950 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001951 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001952
1953 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1954 prm->pkt = pkt;
1955 memset(pkt, 0, sizeof(*pkt));
1956
1957 pkt->entry_type = CTIO_TYPE7;
1958 pkt->entry_count = (uint8_t)prm->req_cnt;
1959 pkt->vp_index = vha->vp_idx;
1960
1961 h = qlt_make_handle(vha);
1962 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1963 /*
1964 * CTIO type 7 from the firmware doesn't provide a way to
1965 * know the initiator's LOOP ID, hence we can't find
1966 * the session and, so, the command.
1967 */
1968 return -EAGAIN;
1969 } else
1970 ha->tgt.cmds[h-1] = prm->cmd;
1971
1972 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1973 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07001974 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001975 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1976 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1977 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1978 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1979 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001980 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1981 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001982 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1983
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001984 return 0;
1985}
1986
1987/*
1988 * ha->hardware_lock supposed to be held on entry. We have already made sure
1989 * that there is sufficient amount of request entries to not drop it.
1990 */
1991static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1992 struct scsi_qla_host *vha)
1993{
1994 int cnt;
1995 uint32_t *dword_ptr;
1996 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1997
1998 /* Build continuation packets */
1999 while (prm->seg_cnt > 0) {
2000 cont_a64_entry_t *cont_pkt64 =
2001 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
2002
2003 /*
2004 * Make sure that from cont_pkt64 none of
2005 * 64-bit specific fields used for 32-bit
2006 * addressing. Cast to (cont_entry_t *) for
2007 * that.
2008 */
2009
2010 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2011
2012 cont_pkt64->entry_count = 1;
2013 cont_pkt64->sys_define = 0;
2014
2015 if (enable_64bit_addressing) {
2016 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2017 dword_ptr =
2018 (uint32_t *)&cont_pkt64->dseg_0_address;
2019 } else {
2020 cont_pkt64->entry_type = CONTINUE_TYPE;
2021 dword_ptr =
2022 (uint32_t *)&((cont_entry_t *)
2023 cont_pkt64)->dseg_0_address;
2024 }
2025
2026 /* Load continuation entry data segments */
2027 for (cnt = 0;
2028 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
2029 cnt++, prm->seg_cnt--) {
2030 *dword_ptr++ =
2031 cpu_to_le32(pci_dma_lo32
2032 (sg_dma_address(prm->sg)));
2033 if (enable_64bit_addressing) {
2034 *dword_ptr++ =
2035 cpu_to_le32(pci_dma_hi32
2036 (sg_dma_address
2037 (prm->sg)));
2038 }
2039 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2040
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002041 prm->sg = sg_next(prm->sg);
2042 }
2043 }
2044}
2045
2046/*
2047 * ha->hardware_lock supposed to be held on entry. We have already made sure
2048 * that there is sufficient amount of request entries to not drop it.
2049 */
2050static void qlt_load_data_segments(struct qla_tgt_prm *prm,
2051 struct scsi_qla_host *vha)
2052{
2053 int cnt;
2054 uint32_t *dword_ptr;
2055 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
2056 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2057
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002058 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2059
2060 /* Setup packet address segment pointer */
2061 dword_ptr = pkt24->u.status0.dseg_0_address;
2062
2063 /* Set total data segment count */
2064 if (prm->seg_cnt)
2065 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2066
2067 if (prm->seg_cnt == 0) {
2068 /* No data transfer */
2069 *dword_ptr++ = 0;
2070 *dword_ptr = 0;
2071 return;
2072 }
2073
2074 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002075
2076 /* Load command entry data segments */
2077 for (cnt = 0;
2078 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
2079 cnt++, prm->seg_cnt--) {
2080 *dword_ptr++ =
2081 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
2082 if (enable_64bit_addressing) {
2083 *dword_ptr++ =
2084 cpu_to_le32(pci_dma_hi32(
2085 sg_dma_address(prm->sg)));
2086 }
2087 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2088
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002089 prm->sg = sg_next(prm->sg);
2090 }
2091
2092 qlt_load_cont_data_segments(prm, vha);
2093}
2094
2095static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2096{
2097 return cmd->bufflen > 0;
2098}
2099
2100/*
2101 * Called without ha->hardware_lock held
2102 */
2103static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2104 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2105 uint32_t *full_req_cnt)
2106{
2107 struct qla_tgt *tgt = cmd->tgt;
2108 struct scsi_qla_host *vha = tgt->vha;
2109 struct qla_hw_data *ha = vha->hw;
2110 struct se_cmd *se_cmd = &cmd->se_cmd;
2111
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002112 prm->cmd = cmd;
2113 prm->tgt = tgt;
2114 prm->rq_result = scsi_status;
2115 prm->sense_buffer = &cmd->sense_buffer[0];
2116 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2117 prm->sg = NULL;
2118 prm->seg_cnt = -1;
2119 prm->req_cnt = 1;
2120 prm->add_status_pkt = 0;
2121
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002122 /* Send marker if required */
2123 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2124 return -EFAULT;
2125
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002126 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2127 if (qlt_pci_map_calc_cnt(prm) != 0)
2128 return -EAGAIN;
2129 }
2130
2131 *full_req_cnt = prm->req_cnt;
2132
2133 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2134 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002135 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
Bart Van Assche649ee052015-04-14 13:26:44 +02002136 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2137 prm->residual, se_cmd->tag,
2138 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2139 cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002140 prm->rq_result |= SS_RESIDUAL_UNDER;
2141 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2142 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04002143 ql_dbg(ql_dbg_io, vha, 0x305d,
Bart Van Assche649ee052015-04-14 13:26:44 +02002144 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2145 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2146 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002147 prm->rq_result |= SS_RESIDUAL_OVER;
2148 }
2149
2150 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2151 /*
2152 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2153 * ignored in *xmit_response() below
2154 */
2155 if (qlt_has_data(cmd)) {
2156 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2157 (IS_FWI2_CAPABLE(ha) &&
2158 (prm->rq_result != 0))) {
2159 prm->add_status_pkt = 1;
2160 (*full_req_cnt)++;
2161 }
2162 }
2163 }
2164
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002165 return 0;
2166}
2167
2168static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
2169 struct qla_tgt_cmd *cmd, int sending_sense)
2170{
2171 if (ha->tgt.enable_class_2)
2172 return 0;
2173
2174 if (sending_sense)
2175 return cmd->conf_compl_supported;
2176 else
2177 return ha->tgt.enable_explicit_conf &&
2178 cmd->conf_compl_supported;
2179}
2180
2181#ifdef CONFIG_QLA_TGT_DEBUG_SRR
2182/*
2183 * Original taken from the XFS code
2184 */
2185static unsigned long qlt_srr_random(void)
2186{
2187 static int Inited;
2188 static unsigned long RandomValue;
2189 static DEFINE_SPINLOCK(lock);
2190 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
2191 register long rv;
2192 register long lo;
2193 register long hi;
2194 unsigned long flags;
2195
2196 spin_lock_irqsave(&lock, flags);
2197 if (!Inited) {
2198 RandomValue = jiffies;
2199 Inited = 1;
2200 }
2201 rv = RandomValue;
2202 hi = rv / 127773;
2203 lo = rv % 127773;
2204 rv = 16807 * lo - 2836 * hi;
2205 if (rv <= 0)
2206 rv += 2147483647;
2207 RandomValue = rv;
2208 spin_unlock_irqrestore(&lock, flags);
2209 return rv;
2210}
2211
2212static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
2213{
2214#if 0 /* This is not a real status packets lost, so it won't lead to SRR */
2215 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
2216 == 50) {
2217 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
2218 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
Bart Van Assche649ee052015-04-14 13:26:44 +02002219 "Dropping cmd %p (tag %d) status", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002220 }
2221#endif
2222 /*
2223 * It's currently not possible to simulate SRRs for FCP_WRITE without
2224 * a physical link layer failure, so don't even try here..
2225 */
2226 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
2227 return;
2228
2229 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
2230 ((qlt_srr_random() % 100) == 20)) {
2231 int i, leave = 0;
2232 unsigned int tot_len = 0;
2233
2234 while (leave == 0)
2235 leave = qlt_srr_random() % cmd->sg_cnt;
2236
2237 for (i = 0; i < leave; i++)
2238 tot_len += cmd->sg[i].length;
2239
2240 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
2241 "Cutting cmd %p (tag %d) buffer"
2242 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
Bart Van Assche649ee052015-04-14 13:26:44 +02002243 " cmd->sg_cnt %d)", cmd, se_cmd->tag, tot_len, leave,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002244 cmd->bufflen, cmd->sg_cnt);
2245
2246 cmd->bufflen = tot_len;
2247 cmd->sg_cnt = leave;
2248 }
2249
2250 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
2251 unsigned int offset = qlt_srr_random() % cmd->bufflen;
2252
2253 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
2254 "Cutting cmd %p (tag %d) buffer head "
Bart Van Assche649ee052015-04-14 13:26:44 +02002255 "to offset %d (cmd->bufflen %d)", cmd, se_cmd->tag, offset,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002256 cmd->bufflen);
2257 if (offset == 0)
2258 *xmit_type &= ~QLA_TGT_XMIT_DATA;
2259 else if (qlt_set_data_offset(cmd, offset)) {
2260 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
Bart Van Assche649ee052015-04-14 13:26:44 +02002261 "qlt_set_data_offset() failed (tag %d)", se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002262 }
2263 }
2264}
2265#else
2266static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
2267{}
2268#endif
2269
2270static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2271 struct qla_tgt_prm *prm)
2272{
2273 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2274 (uint32_t)sizeof(ctio->u.status1.sense_data));
Bart Van Asschead950362015-07-09 07:24:08 -07002275 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002276 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
Bart Van Asschead950362015-07-09 07:24:08 -07002277 ctio->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002278 CTIO7_FLAGS_EXPLICIT_CONFORM |
2279 CTIO7_FLAGS_CONFORM_REQ);
2280 }
2281 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2282 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2283 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2284 int i;
2285
2286 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
2287 if (prm->cmd->se_cmd.scsi_status != 0) {
2288 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
2289 "Skipping EXPLICIT_CONFORM and "
2290 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2291 "non GOOD status\n");
2292 goto skip_explict_conf;
2293 }
Bart Van Asschead950362015-07-09 07:24:08 -07002294 ctio->u.status1.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002295 CTIO7_FLAGS_EXPLICIT_CONFORM |
2296 CTIO7_FLAGS_CONFORM_REQ);
2297 }
2298skip_explict_conf:
2299 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002300 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002301 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002302 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002303 ctio->u.status1.scsi_status |=
Bart Van Asschead950362015-07-09 07:24:08 -07002304 cpu_to_le16(SS_SENSE_LEN_VALID);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002305 ctio->u.status1.sense_length =
2306 cpu_to_le16(prm->sense_buffer_len);
2307 for (i = 0; i < prm->sense_buffer_len/4; i++)
2308 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2309 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2310#if 0
2311 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
2312 static int q;
2313 if (q < 10) {
2314 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
2315 "qla_target(%d): %d bytes of sense "
2316 "lost", prm->tgt->ha->vp_idx,
2317 prm->sense_buffer_len % 4);
2318 q++;
2319 }
2320 }
2321#endif
2322 } else {
2323 ctio->u.status1.flags &=
Bart Van Asschead950362015-07-09 07:24:08 -07002324 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002325 ctio->u.status1.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002326 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002327 ctio->u.status1.sense_length = 0;
2328 memset(ctio->u.status1.sense_data, 0,
2329 sizeof(ctio->u.status1.sense_data));
2330 }
2331
2332 /* Sense with len > 24, is it possible ??? */
2333}
2334
Quinn Tranf83adb62014-04-11 16:54:43 -04002335
2336
2337/* diff */
2338static inline int
2339qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2340{
2341 /*
2342 * Uncomment when corresponding SCSI changes are done.
2343 *
2344 if (!sp->cmd->prot_chk)
2345 return 0;
2346 *
2347 */
2348 switch (se_cmd->prot_op) {
2349 case TARGET_PROT_DOUT_INSERT:
2350 case TARGET_PROT_DIN_STRIP:
2351 if (ql2xenablehba_err_chk >= 1)
2352 return 1;
2353 break;
2354 case TARGET_PROT_DOUT_PASS:
2355 case TARGET_PROT_DIN_PASS:
2356 if (ql2xenablehba_err_chk >= 2)
2357 return 1;
2358 break;
2359 case TARGET_PROT_DIN_INSERT:
2360 case TARGET_PROT_DOUT_STRIP:
2361 return 1;
2362 default:
2363 break;
2364 }
2365 return 0;
2366}
2367
2368/*
2369 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
2370 *
2371 */
2372static inline void
2373qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
2374{
2375 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2376
2377 /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
2378 * have been immplemented by TCM, before AppTag is avail.
2379 * Look for modesense_handlers[]
2380 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002381 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002382 ctx->app_tag_mask[0] = 0x0;
2383 ctx->app_tag_mask[1] = 0x0;
2384
2385 switch (se_cmd->prot_type) {
2386 case TARGET_DIF_TYPE0_PROT:
2387 /*
2388 * No check for ql2xenablehba_err_chk, as it would be an
2389 * I/O error if hba tag generation is not done.
2390 */
2391 ctx->ref_tag = cpu_to_le32(lba);
2392
2393 if (!qlt_hba_err_chk_enabled(se_cmd))
2394 break;
2395
2396 /* enable ALL bytes of the ref tag */
2397 ctx->ref_tag_mask[0] = 0xff;
2398 ctx->ref_tag_mask[1] = 0xff;
2399 ctx->ref_tag_mask[2] = 0xff;
2400 ctx->ref_tag_mask[3] = 0xff;
2401 break;
2402 /*
2403 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
2404 * 16 bit app tag.
2405 */
2406 case TARGET_DIF_TYPE1_PROT:
2407 ctx->ref_tag = cpu_to_le32(lba);
2408
2409 if (!qlt_hba_err_chk_enabled(se_cmd))
2410 break;
2411
2412 /* enable ALL bytes of the ref tag */
2413 ctx->ref_tag_mask[0] = 0xff;
2414 ctx->ref_tag_mask[1] = 0xff;
2415 ctx->ref_tag_mask[2] = 0xff;
2416 ctx->ref_tag_mask[3] = 0xff;
2417 break;
2418 /*
2419 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
2420 * match LBA in CDB + N
2421 */
2422 case TARGET_DIF_TYPE2_PROT:
2423 ctx->ref_tag = cpu_to_le32(lba);
2424
2425 if (!qlt_hba_err_chk_enabled(se_cmd))
2426 break;
2427
2428 /* enable ALL bytes of the ref tag */
2429 ctx->ref_tag_mask[0] = 0xff;
2430 ctx->ref_tag_mask[1] = 0xff;
2431 ctx->ref_tag_mask[2] = 0xff;
2432 ctx->ref_tag_mask[3] = 0xff;
2433 break;
2434
2435 /* For Type 3 protection: 16 bit GUARD only */
2436 case TARGET_DIF_TYPE3_PROT:
2437 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2438 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2439 break;
2440 }
2441}
2442
2443
2444static inline int
2445qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2446{
2447 uint32_t *cur_dsd;
Quinn Tranf83adb62014-04-11 16:54:43 -04002448 uint32_t transfer_length = 0;
2449 uint32_t data_bytes;
2450 uint32_t dif_bytes;
2451 uint8_t bundling = 1;
2452 uint8_t *clr_ptr;
2453 struct crc_context *crc_ctx_pkt = NULL;
2454 struct qla_hw_data *ha;
2455 struct ctio_crc2_to_fw *pkt;
2456 dma_addr_t crc_ctx_dma;
2457 uint16_t fw_prot_opts = 0;
2458 struct qla_tgt_cmd *cmd = prm->cmd;
2459 struct se_cmd *se_cmd = &cmd->se_cmd;
2460 uint32_t h;
2461 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002462 uint16_t t16;
Quinn Tranf83adb62014-04-11 16:54:43 -04002463
Quinn Tranf83adb62014-04-11 16:54:43 -04002464 ha = vha->hw;
2465
2466 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2467 prm->pkt = pkt;
2468 memset(pkt, 0, sizeof(*pkt));
2469
2470 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2471 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2472 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2473 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2474
2475 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2476 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2477 bundling = 0;
2478
2479 /* Compute dif len and adjust data len to incude protection */
2480 data_bytes = cmd->bufflen;
2481 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2482
2483 switch (se_cmd->prot_op) {
2484 case TARGET_PROT_DIN_INSERT:
2485 case TARGET_PROT_DOUT_STRIP:
2486 transfer_length = data_bytes;
2487 data_bytes += dif_bytes;
2488 break;
2489
2490 case TARGET_PROT_DIN_STRIP:
2491 case TARGET_PROT_DOUT_INSERT:
2492 case TARGET_PROT_DIN_PASS:
2493 case TARGET_PROT_DOUT_PASS:
2494 transfer_length = data_bytes + dif_bytes;
2495 break;
2496
2497 default:
2498 BUG();
2499 break;
2500 }
2501
2502 if (!qlt_hba_err_chk_enabled(se_cmd))
2503 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2504 /* HBA error checking enabled */
2505 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2506 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2507 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2508 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2509 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2510 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2511 }
2512
2513 switch (se_cmd->prot_op) {
2514 case TARGET_PROT_DIN_INSERT:
2515 case TARGET_PROT_DOUT_INSERT:
2516 fw_prot_opts |= PO_MODE_DIF_INSERT;
2517 break;
2518 case TARGET_PROT_DIN_STRIP:
2519 case TARGET_PROT_DOUT_STRIP:
2520 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2521 break;
2522 case TARGET_PROT_DIN_PASS:
2523 case TARGET_PROT_DOUT_PASS:
2524 fw_prot_opts |= PO_MODE_DIF_PASS;
2525 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2526 break;
2527 default:/* Normal Request */
2528 fw_prot_opts |= PO_MODE_DIF_PASS;
2529 break;
2530 }
2531
2532
2533 /* ---- PKT ---- */
2534 /* Update entry type to indicate Command Type CRC_2 IOCB */
2535 pkt->entry_type = CTIO_CRC2;
2536 pkt->entry_count = 1;
2537 pkt->vp_index = vha->vp_idx;
2538
2539 h = qlt_make_handle(vha);
2540 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2541 /*
2542 * CTIO type 7 from the firmware doesn't provide a way to
2543 * know the initiator's LOOP ID, hence we can't find
2544 * the session and, so, the command.
2545 */
2546 return -EAGAIN;
2547 } else
2548 ha->tgt.cmds[h-1] = prm->cmd;
2549
2550
2551 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2552 pkt->nport_handle = prm->cmd->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07002553 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002554 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2555 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2556 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2557 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002558
2559 /* silence compile warning */
2560 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2561 pkt->ox_id = cpu_to_le16(t16);
2562
2563 t16 = (atio->u.isp24.attr << 9);
2564 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002565 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2566
2567 /* Set transfer direction */
2568 if (cmd->dma_data_direction == DMA_TO_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002569 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
Quinn Tranf83adb62014-04-11 16:54:43 -04002570 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
Bart Van Asschead950362015-07-09 07:24:08 -07002571 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
Quinn Tranf83adb62014-04-11 16:54:43 -04002572
2573
2574 pkt->dseg_count = prm->tot_dsds;
2575 /* Fibre channel byte count */
2576 pkt->transfer_length = cpu_to_le32(transfer_length);
2577
2578
2579 /* ----- CRC context -------- */
2580
2581 /* Allocate CRC context from global pool */
2582 crc_ctx_pkt = cmd->ctx =
2583 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2584
2585 if (!crc_ctx_pkt)
2586 goto crc_queuing_error;
2587
2588 /* Zero out CTX area. */
2589 clr_ptr = (uint8_t *)crc_ctx_pkt;
2590 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2591
2592 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2593 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2594
2595 /* Set handle */
2596 crc_ctx_pkt->handle = pkt->handle;
2597
2598 qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
2599
2600 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2601 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2602 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2603
2604
2605 if (!bundling) {
2606 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2607 } else {
2608 /*
2609 * Configure Bundling if we need to fetch interlaving
2610 * protection PCI accesses
2611 */
2612 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2613 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2614 crc_ctx_pkt->u.bundling.dseg_count =
2615 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2616 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2617 }
2618
2619 /* Finish the common fields of CRC pkt */
2620 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2621 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2622 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
Bart Van Asschead950362015-07-09 07:24:08 -07002623 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002624
2625
2626 /* Walks data segments */
Bart Van Asschead950362015-07-09 07:24:08 -07002627 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
Quinn Tranf83adb62014-04-11 16:54:43 -04002628
2629 if (!bundling && prm->prot_seg_cnt) {
2630 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2631 prm->tot_dsds, cmd))
2632 goto crc_queuing_error;
2633 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2634 (prm->tot_dsds - prm->prot_seg_cnt), cmd))
2635 goto crc_queuing_error;
2636
2637 if (bundling && prm->prot_seg_cnt) {
2638 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002639 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04002640
2641 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2642 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2643 prm->prot_seg_cnt, cmd))
2644 goto crc_queuing_error;
2645 }
2646 return QLA_SUCCESS;
2647
2648crc_queuing_error:
2649 /* Cleanup will be performed by the caller */
2650
2651 return QLA_FUNCTION_FAILED;
2652}
2653
2654
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002655/*
2656 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2657 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2658 */
2659int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2660 uint8_t scsi_status)
2661{
2662 struct scsi_qla_host *vha = cmd->vha;
2663 struct qla_hw_data *ha = vha->hw;
2664 struct ctio7_to_24xx *pkt;
2665 struct qla_tgt_prm prm;
2666 uint32_t full_req_cnt = 0;
2667 unsigned long flags = 0;
2668 int res;
2669
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002670 spin_lock_irqsave(&ha->hardware_lock, flags);
2671 if (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
2672 cmd->state = QLA_TGT_STATE_PROCESSED;
2673 if (cmd->sess->logout_completed)
2674 /* no need to terminate. FW already freed exchange. */
2675 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2676 else
Quinn Trana07100e2015-12-07 19:48:57 -05002677 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002678 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2679 return 0;
2680 }
2681 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2682
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002683 memset(&prm, 0, sizeof(prm));
2684 qlt_check_srr_debug(cmd, &xmit_type);
2685
2686 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
Quinn Tranf83adb62014-04-11 16:54:43 -04002687 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
2688 (xmit_type & QLA_TGT_XMIT_STATUS) ?
2689 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
2690 &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002691
2692 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2693 &full_req_cnt);
2694 if (unlikely(res != 0)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002695 return res;
2696 }
2697
2698 spin_lock_irqsave(&ha->hardware_lock, flags);
2699
Himanshu Madhanice1025c2015-12-17 14:56:58 -05002700 if (xmit_type == QLA_TGT_XMIT_STATUS)
2701 vha->tgt_counters.core_qla_snd_status++;
2702 else
2703 vha->tgt_counters.core_qla_que_buf++;
2704
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002705 if (!vha->flags.online || cmd->reset_count != ha->chip_reset) {
Arun Easib6a029e2014-09-25 06:14:52 -04002706 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002707 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002708 * previous life, just abort the processing.
2709 */
2710 cmd->state = QLA_TGT_STATE_PROCESSED;
2711 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2712 ql_dbg(ql_dbg_async, vha, 0xe101,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002713 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
2714 vha->flags.online, qla2x00_reset_active(vha),
2715 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002716 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2717 return 0;
2718 }
2719
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002720 /* Does F/W have an IOCBs for this request */
2721 res = qlt_check_reserve_free_req(vha, full_req_cnt);
2722 if (unlikely(res))
2723 goto out_unmap_unlock;
2724
Quinn Tranf83adb62014-04-11 16:54:43 -04002725 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
2726 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2727 else
2728 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Quinn Tran810e30b2015-06-10 11:05:20 -04002729 if (unlikely(res != 0)) {
2730 vha->req->cnt += full_req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002731 goto out_unmap_unlock;
Quinn Tran810e30b2015-06-10 11:05:20 -04002732 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002733
2734 pkt = (struct ctio7_to_24xx *)prm.pkt;
2735
2736 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2737 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002738 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002739 CTIO7_FLAGS_STATUS_MODE_0);
2740
Quinn Tranf83adb62014-04-11 16:54:43 -04002741 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2742 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002743
2744 if (prm.add_status_pkt == 0) {
2745 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2746 pkt->u.status0.scsi_status =
2747 cpu_to_le16(prm.rq_result);
2748 pkt->u.status0.residual =
2749 cpu_to_le32(prm.residual);
Bart Van Asschead950362015-07-09 07:24:08 -07002750 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002751 CTIO7_FLAGS_SEND_STATUS);
2752 if (qlt_need_explicit_conf(ha, cmd, 0)) {
2753 pkt->u.status0.flags |=
Bart Van Asschead950362015-07-09 07:24:08 -07002754 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002755 CTIO7_FLAGS_EXPLICIT_CONFORM |
2756 CTIO7_FLAGS_CONFORM_REQ);
2757 }
2758 }
2759
2760 } else {
2761 /*
2762 * We have already made sure that there is sufficient
2763 * amount of request entries to not drop HW lock in
2764 * req_pkt().
2765 */
2766 struct ctio7_to_24xx *ctio =
2767 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2768
Arun Easi667024a2014-09-25 06:14:47 -04002769 ql_dbg(ql_dbg_io, vha, 0x305e,
2770 "Building additional status packet 0x%p.\n",
2771 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002772
Quinn Tranf83adb62014-04-11 16:54:43 -04002773 /*
2774 * T10Dif: ctio_crc2_to_fw overlay ontop of
2775 * ctio7_to_24xx
2776 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002777 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04002778 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002779 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04002780 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002781 ctio->dseg_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07002782 ctio->u.status1.flags &= ~cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002783 CTIO7_FLAGS_DATA_IN);
2784
2785 /* Real finish is ctio_m1's finish */
2786 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
Bart Van Asschead950362015-07-09 07:24:08 -07002787 pkt->u.status0.flags |= cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002788 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04002789
2790 /* qlt_24xx_init_ctio_to_isp will correct
2791 * all neccessary fields that's part of CTIO7.
2792 * There should be no residual of CTIO-CRC2 data.
2793 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002794 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2795 &prm);
2796 pr_debug("Status CTIO7: %p\n", ctio);
2797 }
2798 } else
2799 qlt_24xx_init_ctio_to_isp(pkt, &prm);
2800
2801
2802 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04002803 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002804
Himanshu Madhani63163e02014-09-25 06:14:59 -04002805 /* Memory Barrier */
2806 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002807 qla2x00_start_iocbs(vha, vha->req);
2808 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2809
2810 return 0;
2811
2812out_unmap_unlock:
Joern Engelf9b67212014-09-16 16:23:18 -04002813 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002814 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2815
2816 return res;
2817}
2818EXPORT_SYMBOL(qlt_xmit_response);
2819
2820int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2821{
2822 struct ctio7_to_24xx *pkt;
2823 struct scsi_qla_host *vha = cmd->vha;
2824 struct qla_hw_data *ha = vha->hw;
2825 struct qla_tgt *tgt = cmd->tgt;
2826 struct qla_tgt_prm prm;
2827 unsigned long flags;
2828 int res = 0;
2829
2830 memset(&prm, 0, sizeof(prm));
2831 prm.cmd = cmd;
2832 prm.tgt = tgt;
2833 prm.sg = NULL;
2834 prm.req_cnt = 1;
2835
2836 /* Send marker if required */
2837 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2838 return -EIO;
2839
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002840 /* Calculate number of entries and segments required */
2841 if (qlt_pci_map_calc_cnt(&prm) != 0)
2842 return -EAGAIN;
2843
2844 spin_lock_irqsave(&ha->hardware_lock, flags);
2845
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002846 if (!vha->flags.online || (cmd->reset_count != ha->chip_reset) ||
Alexei Potashnika6ca8872015-07-14 16:00:44 -04002847 (cmd->sess && cmd->sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)) {
Arun Easib6a029e2014-09-25 06:14:52 -04002848 /*
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002849 * Either the port is not online or this request was from
Arun Easib6a029e2014-09-25 06:14:52 -04002850 * previous life, just abort the processing.
2851 */
2852 cmd->state = QLA_TGT_STATE_NEED_DATA;
2853 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2854 ql_dbg(ql_dbg_async, vha, 0xe102,
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05002855 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2856 vha->flags.online, qla2x00_reset_active(vha),
2857 cmd->reset_count, ha->chip_reset);
Arun Easib6a029e2014-09-25 06:14:52 -04002858 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2859 return 0;
2860 }
2861
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002862 /* Does F/W have an IOCBs for this request */
2863 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2864 if (res != 0)
2865 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04002866 if (cmd->se_cmd.prot_op)
2867 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2868 else
2869 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002870
Quinn Tran810e30b2015-06-10 11:05:20 -04002871 if (unlikely(res != 0)) {
2872 vha->req->cnt += prm.req_cnt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002873 goto out_unlock_free_unmap;
Quinn Tran810e30b2015-06-10 11:05:20 -04002874 }
2875
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002876 pkt = (struct ctio7_to_24xx *)prm.pkt;
Bart Van Asschead950362015-07-09 07:24:08 -07002877 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002878 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002879
2880 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2881 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002882
2883 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04002884 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002885
Himanshu Madhani63163e02014-09-25 06:14:59 -04002886 /* Memory Barrier */
2887 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002888 qla2x00_start_iocbs(vha, vha->req);
2889 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2890
2891 return res;
2892
2893out_unlock_free_unmap:
Joern Engelf9b67212014-09-16 16:23:18 -04002894 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002895 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2896
2897 return res;
2898}
2899EXPORT_SYMBOL(qlt_rdy_to_xfer);
2900
Quinn Tranf83adb62014-04-11 16:54:43 -04002901
2902/*
2903 * Checks the guard or meta-data for the type of error
2904 * detected by the HBA.
2905 */
2906static inline int
2907qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
2908 struct ctio_crc_from_fw *sts)
2909{
2910 uint8_t *ap = &sts->actual_dif[0];
2911 uint8_t *ep = &sts->expected_dif[0];
2912 uint32_t e_ref_tag, a_ref_tag;
2913 uint16_t e_app_tag, a_app_tag;
2914 uint16_t e_guard, a_guard;
2915 uint64_t lba = cmd->se_cmd.t_task_lba;
2916
2917 a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
2918 a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
2919 a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
2920
2921 e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
2922 e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
2923 e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
2924
2925 ql_dbg(ql_dbg_tgt, vha, 0xe075,
2926 "iocb(s) %p Returned STATUS.\n", sts);
2927
2928 ql_dbg(ql_dbg_tgt, vha, 0xf075,
Hans Wennborgfc385042014-08-05 21:43:29 -07002929 "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 -04002930 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2931 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
2932
2933 /*
2934 * Ignore sector if:
2935 * For type 3: ref & app tag is all 'f's
2936 * For type 0,1,2: app tag is all 'f's
2937 */
2938 if ((a_app_tag == 0xffff) &&
2939 ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
2940 (a_ref_tag == 0xffffffff))) {
2941 uint32_t blocks_done;
2942
2943 /* 2TB boundary case covered automatically with this */
2944 blocks_done = e_ref_tag - (uint32_t)lba + 1;
2945 cmd->se_cmd.bad_sector = e_ref_tag;
2946 cmd->se_cmd.pi_err = 0;
2947 ql_dbg(ql_dbg_tgt, vha, 0xf074,
2948 "need to return scsi good\n");
2949
2950 /* Update protection tag */
2951 if (cmd->prot_sg_cnt) {
Bart Van Assche52c82822015-07-09 07:23:26 -07002952 uint32_t i, k = 0, num_ent;
Quinn Tranf83adb62014-04-11 16:54:43 -04002953 struct scatterlist *sg, *sgl;
2954
2955
2956 sgl = cmd->prot_sg;
2957
2958 /* Patch the corresponding protection tags */
2959 for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
2960 num_ent = sg_dma_len(sg) / 8;
2961 if (k + num_ent < blocks_done) {
2962 k += num_ent;
2963 continue;
2964 }
Quinn Tranf83adb62014-04-11 16:54:43 -04002965 k = blocks_done;
2966 break;
2967 }
2968
2969 if (k != blocks_done) {
2970 ql_log(ql_log_warn, vha, 0xf076,
2971 "unexpected tag values tag:lba=%u:%llu)\n",
2972 e_ref_tag, (unsigned long long)lba);
2973 goto out;
2974 }
2975
2976#if 0
2977 struct sd_dif_tuple *spt;
2978 /* TODO:
2979 * This section came from initiator. Is it valid here?
2980 * should ulp be override with actual val???
2981 */
2982 spt = page_address(sg_page(sg)) + sg->offset;
2983 spt += j;
2984
2985 spt->app_tag = 0xffff;
2986 if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
2987 spt->ref_tag = 0xffffffff;
2988#endif
2989 }
2990
2991 return 0;
2992 }
2993
2994 /* check guard */
2995 if (e_guard != a_guard) {
2996 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
2997 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
2998
2999 ql_log(ql_log_warn, vha, 0xe076,
3000 "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",
3001 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3002 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3003 a_guard, e_guard, cmd);
3004 goto out;
3005 }
3006
3007 /* check ref tag */
3008 if (e_ref_tag != a_ref_tag) {
3009 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
3010 cmd->se_cmd.bad_sector = e_ref_tag;
3011
3012 ql_log(ql_log_warn, vha, 0xe077,
3013 "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",
3014 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3015 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3016 a_guard, e_guard, cmd);
3017 goto out;
3018 }
3019
3020 /* check appl tag */
3021 if (e_app_tag != a_app_tag) {
3022 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
3023 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
3024
3025 ql_log(ql_log_warn, vha, 0xe078,
3026 "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",
3027 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
3028 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
3029 a_guard, e_guard, cmd);
3030 goto out;
3031 }
3032out:
3033 return 1;
3034}
3035
3036
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003037/* If hardware_lock held on entry, might drop it, then reaquire */
3038/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Alexei Potashnika6ca8872015-07-14 16:00:44 -04003039static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3040 struct imm_ntfy_from_isp *ntfy)
3041{
3042 struct nack_to_isp *nack;
3043 struct qla_hw_data *ha = vha->hw;
3044 request_t *pkt;
3045 int ret = 0;
3046
3047 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3048 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3049
3050 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
3051 if (pkt == NULL) {
3052 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3053 "qla_target(%d): %s failed: unable to allocate "
3054 "request packet\n", vha->vp_idx, __func__);
3055 return -ENOMEM;
3056 }
3057
3058 pkt->entry_type = NOTIFY_ACK_TYPE;
3059 pkt->entry_count = 1;
3060 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3061
3062 nack = (struct nack_to_isp *)pkt;
3063 nack->ox_id = ntfy->ox_id;
3064
3065 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3066 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3067 nack->u.isp24.flags = ntfy->u.isp24.flags &
3068 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3069 }
3070
3071 /* terminate */
3072 nack->u.isp24.flags |=
3073 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3074
3075 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3076 nack->u.isp24.status = ntfy->u.isp24.status;
3077 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3078 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3079 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3080 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3081 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3082 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3083
3084 qla2x00_start_iocbs(vha, vha->req);
3085 return ret;
3086}
3087
3088static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3089 struct imm_ntfy_from_isp *imm, int ha_locked)
3090{
3091 unsigned long flags = 0;
3092 int rc;
3093
3094 if (qlt_issue_marker(vha, ha_locked) < 0)
3095 return;
3096
3097 if (ha_locked) {
3098 rc = __qlt_send_term_imm_notif(vha, imm);
3099
3100#if 0 /* Todo */
3101 if (rc == -ENOMEM)
3102 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3103#endif
3104 goto done;
3105 }
3106
3107 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3108 rc = __qlt_send_term_imm_notif(vha, imm);
3109
3110#if 0 /* Todo */
3111 if (rc == -ENOMEM)
3112 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3113#endif
3114
3115done:
3116 if (!ha_locked)
3117 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3118}
3119
3120/* If hardware_lock held on entry, might drop it, then reaquire */
3121/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003122static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
3123 struct qla_tgt_cmd *cmd,
3124 struct atio_from_isp *atio)
3125{
3126 struct ctio7_to_24xx *ctio24;
3127 struct qla_hw_data *ha = vha->hw;
3128 request_t *pkt;
3129 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04003130 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003131
3132 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3133
Arun Easib6a029e2014-09-25 06:14:52 -04003134 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003135 if (pkt == NULL) {
3136 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3137 "qla_target(%d): %s failed: unable to allocate "
3138 "request packet\n", vha->vp_idx, __func__);
3139 return -ENOMEM;
3140 }
3141
3142 if (cmd != NULL) {
3143 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3144 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3145 "qla_target(%d): Terminating cmd %p with "
3146 "incorrect state %d\n", vha->vp_idx, cmd,
3147 cmd->state);
3148 } else
3149 ret = 1;
3150 }
3151
Himanshu Madhanice1025c2015-12-17 14:56:58 -05003152 vha->tgt_counters.num_term_xchg_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003153 pkt->entry_count = 1;
3154 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3155
3156 ctio24 = (struct ctio7_to_24xx *)pkt;
3157 ctio24->entry_type = CTIO_TYPE7;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003158 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
Bart Van Asschead950362015-07-09 07:24:08 -07003159 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003160 ctio24->vp_index = vha->vp_idx;
3161 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3162 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3163 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3164 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3165 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07003166 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003167 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04003168 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3169 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003170
3171 /* Most likely, it isn't needed */
3172 ctio24->u.status1.residual = get_unaligned((uint32_t *)
3173 &atio->u.isp24.fcp_cmnd.add_cdb[
3174 atio->u.isp24.fcp_cmnd.add_cdb_len]);
3175 if (ctio24->u.status1.residual != 0)
3176 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
3177
Himanshu Madhani63163e02014-09-25 06:14:59 -04003178 /* Memory Barrier */
3179 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003180 qla2x00_start_iocbs(vha, vha->req);
3181 return ret;
3182}
3183
3184static void qlt_send_term_exchange(struct scsi_qla_host *vha,
Quinn Trana07100e2015-12-07 19:48:57 -05003185 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3186 int ul_abort)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003187{
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003188 unsigned long flags = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003189 int rc;
3190
3191 if (qlt_issue_marker(vha, ha_locked) < 0)
3192 return;
3193
3194 if (ha_locked) {
3195 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003196 if (rc == -ENOMEM)
3197 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003198 goto done;
3199 }
3200 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3201 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04003202 if (rc == -ENOMEM)
3203 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Quinn Trand564a372014-09-25 06:14:57 -04003204
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003205done:
Quinn Trana07100e2015-12-07 19:48:57 -05003206 if (cmd && !ul_abort && !cmd->aborted) {
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003207 if (cmd->sg_mapped)
3208 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003209 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3210 }
Himanshu Madhani6bc85dd2015-06-10 11:05:22 -04003211
3212 if (!ha_locked)
3213 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3214
Quinn Tran7b898542014-04-11 16:54:44 -04003215 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003216}
3217
Quinn Tran33e79972014-09-25 06:14:55 -04003218static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3219{
3220 struct list_head free_list;
3221 struct qla_tgt_cmd *cmd, *tcmd;
3222
3223 vha->hw->tgt.leak_exchg_thresh_hold =
Quinn Tran03e8c682015-12-17 14:56:59 -05003224 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
Quinn Tran33e79972014-09-25 06:14:55 -04003225
3226 cmd = tcmd = NULL;
3227 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3228 INIT_LIST_HEAD(&free_list);
3229 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3230
3231 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3232 list_del(&cmd->cmd_list);
3233 /* This cmd was never sent to TCM. There is no need
3234 * to schedule free or call free_cmd
3235 */
3236 qlt_free_cmd(cmd);
3237 vha->hw->tgt.num_qfull_cmds_alloc--;
3238 }
3239 }
3240 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3241}
3242
3243static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3244{
3245 uint32_t total_leaked;
3246
3247 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3248
3249 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3250 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3251
3252 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3253 "Chip reset due to exchange starvation: %d/%d.\n",
Quinn Tran03e8c682015-12-17 14:56:59 -05003254 total_leaked, vha->hw->cur_fw_xcb_count);
Quinn Tran33e79972014-09-25 06:14:55 -04003255
3256 if (IS_P3P_TYPE(vha->hw))
3257 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3258 else
3259 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3260 qla2xxx_wake_dpc(vha);
3261 }
3262
3263}
3264
Quinn Trana07100e2015-12-07 19:48:57 -05003265int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
Alexei Potashnik7359df22015-07-14 16:00:49 -04003266{
3267 struct qla_tgt *tgt = cmd->tgt;
3268 struct scsi_qla_host *vha = tgt->vha;
3269 struct se_cmd *se_cmd = &cmd->se_cmd;
Quinn Trana07100e2015-12-07 19:48:57 -05003270 unsigned long flags;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003271
3272 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3273 "qla_target(%d): terminating exchange for aborted cmd=%p "
3274 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3275 se_cmd->tag);
3276
Quinn Trana07100e2015-12-07 19:48:57 -05003277 spin_lock_irqsave(&cmd->cmd_lock, flags);
3278 if (cmd->aborted) {
3279 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3280 /*
3281 * It's normal to see 2 calls in this path:
3282 * 1) XFER Rdy completion + CMD_T_ABORT
3283 * 2) TCM TMR - drain_state_list
3284 */
3285 ql_dbg(ql_dbg_tgt_mgt, vha, 0xffff,
3286 "multiple abort. %p transport_state %x, t_state %x,"
3287 " se_cmd_flags %x \n", cmd, cmd->se_cmd.transport_state,
3288 cmd->se_cmd.t_state,cmd->se_cmd.se_cmd_flags);
3289 return EIO;
3290 }
Quinn Tran193b50b2015-12-17 14:57:03 -05003291 cmd->aborted = 1;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003292 cmd->cmd_flags |= BIT_6;
Quinn Trana07100e2015-12-07 19:48:57 -05003293 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
Alexei Potashnik7359df22015-07-14 16:00:49 -04003294
Quinn Trana07100e2015-12-07 19:48:57 -05003295 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0, 1);
3296 return 0;
Alexei Potashnik7359df22015-07-14 16:00:49 -04003297}
3298EXPORT_SYMBOL(qlt_abort_cmd);
3299
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003300void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3301{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003302 struct qla_tgt_sess *sess = cmd->sess;
3303
Quinn Tranf83adb62014-04-11 16:54:43 -04003304 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3305 "%s: se_cmd[%p] ox_id %04x\n",
3306 __func__, &cmd->se_cmd,
3307 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003308
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003309 BUG_ON(cmd->cmd_in_wq);
3310
Quinn Trana07100e2015-12-07 19:48:57 -05003311 if (cmd->sg_mapped)
3312 qlt_unmap_sg(cmd->vha, cmd);
3313
Quinn Tran33e79972014-09-25 06:14:55 -04003314 if (!cmd->q_full)
3315 qlt_decr_num_pend_cmds(cmd->vha);
3316
Quinn Tranf83adb62014-04-11 16:54:43 -04003317 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003318 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003319 if (unlikely(cmd->free_sg))
3320 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003321
3322 if (!sess || !sess->se_sess) {
3323 WARN_ON(1);
3324 return;
3325 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003326 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003327 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003328}
3329EXPORT_SYMBOL(qlt_free_cmd);
3330
3331/* ha->hardware_lock supposed to be held on entry */
3332static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
3333 struct qla_tgt_cmd *cmd, void *ctio)
3334{
3335 struct qla_tgt_srr_ctio *sc;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003336 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003337 struct qla_tgt_srr_imm *imm;
3338
3339 tgt->ctio_srr_id++;
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003340 cmd->cmd_flags |= BIT_15;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003341
3342 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
3343 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
3344
3345 if (!ctio) {
3346 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
3347 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
3348 vha->vp_idx);
3349 return -EINVAL;
3350 }
3351
3352 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
3353 if (sc != NULL) {
3354 sc->cmd = cmd;
3355 /* IRQ is already OFF */
3356 spin_lock(&tgt->srr_lock);
3357 sc->srr_id = tgt->ctio_srr_id;
3358 list_add_tail(&sc->srr_list_entry,
3359 &tgt->srr_ctio_list);
3360 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
3361 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
3362 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
3363 int found = 0;
3364 list_for_each_entry(imm, &tgt->srr_imm_list,
3365 srr_list_entry) {
3366 if (imm->srr_id == sc->srr_id) {
3367 found = 1;
3368 break;
3369 }
3370 }
3371 if (found) {
3372 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
3373 "Scheduling srr work\n");
3374 schedule_work(&tgt->srr_work);
3375 } else {
3376 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
3377 "qla_target(%d): imm_srr_id "
3378 "== ctio_srr_id (%d), but there is no "
3379 "corresponding SRR IMM, deleting CTIO "
3380 "SRR %p\n", vha->vp_idx,
3381 tgt->ctio_srr_id, sc);
3382 list_del(&sc->srr_list_entry);
3383 spin_unlock(&tgt->srr_lock);
3384
3385 kfree(sc);
3386 return -EINVAL;
3387 }
3388 }
3389 spin_unlock(&tgt->srr_lock);
3390 } else {
3391 struct qla_tgt_srr_imm *ti;
3392
3393 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
3394 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
3395 vha->vp_idx);
3396 spin_lock(&tgt->srr_lock);
3397 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
3398 srr_list_entry) {
3399 if (imm->srr_id == tgt->ctio_srr_id) {
3400 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
3401 "IMM SRR %p deleted (id %d)\n",
3402 imm, imm->srr_id);
3403 list_del(&imm->srr_list_entry);
3404 qlt_reject_free_srr_imm(vha, imm, 1);
3405 }
3406 }
3407 spin_unlock(&tgt->srr_lock);
3408
3409 return -ENOMEM;
3410 }
3411
3412 return 0;
3413}
3414
3415/*
3416 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3417 */
3418static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
3419 struct qla_tgt_cmd *cmd, uint32_t status)
3420{
3421 int term = 0;
3422
3423 if (ctio != NULL) {
3424 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3425 term = !(c->flags &
Bart Van Asschead950362015-07-09 07:24:08 -07003426 cpu_to_le16(OF_TERM_EXCH));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003427 } else
3428 term = 1;
3429
3430 if (term)
Quinn Trana07100e2015-12-07 19:48:57 -05003431 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003432
3433 return term;
3434}
3435
3436/* ha->hardware_lock supposed to be held on entry */
3437static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
3438 uint32_t handle)
3439{
3440 struct qla_hw_data *ha = vha->hw;
3441
3442 handle--;
3443 if (ha->tgt.cmds[handle] != NULL) {
3444 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
3445 ha->tgt.cmds[handle] = NULL;
3446 return cmd;
3447 } else
3448 return NULL;
3449}
3450
3451/* ha->hardware_lock supposed to be held on entry */
3452static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3453 uint32_t handle, void *ctio)
3454{
3455 struct qla_tgt_cmd *cmd = NULL;
3456
3457 /* Clear out internal marks */
3458 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
3459 CTIO_INTERMEDIATE_HANDLE_MARK);
3460
3461 if (handle != QLA_TGT_NULL_HANDLE) {
Arun Easi667024a2014-09-25 06:14:47 -04003462 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003463 return NULL;
Arun Easi667024a2014-09-25 06:14:47 -04003464
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003465 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05003466 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003467 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3468 "qla_target(%d): Wrong handle %x received\n",
3469 vha->vp_idx, handle);
3470 return NULL;
3471 }
3472 cmd = qlt_get_cmd(vha, handle);
3473 if (unlikely(cmd == NULL)) {
3474 ql_dbg(ql_dbg_tgt, vha, 0xe053,
3475 "qla_target(%d): Suspicious: unable to "
3476 "find the command with handle %x\n", vha->vp_idx,
3477 handle);
3478 return NULL;
3479 }
3480 } else if (ctio != NULL) {
3481 /* We can't get loop ID from CTIO7 */
3482 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3483 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3484 "support NULL handles\n", vha->vp_idx);
3485 return NULL;
3486 }
3487
3488 return cmd;
3489}
3490
Arun Easic0cb4492014-09-25 06:14:51 -04003491/* hardware_lock should be held by caller. */
3492static void
3493qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3494{
3495 struct qla_hw_data *ha = vha->hw;
3496 uint32_t handle;
3497
3498 if (cmd->sg_mapped)
3499 qlt_unmap_sg(vha, cmd);
3500
3501 handle = qlt_make_handle(vha);
3502
3503 /* TODO: fix debug message type and ids. */
3504 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3505 ql_dbg(ql_dbg_io, vha, 0xff00,
3506 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
3507 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3508 cmd->write_data_transferred = 0;
3509 cmd->state = QLA_TGT_STATE_DATA_IN;
3510
3511 ql_dbg(ql_dbg_io, vha, 0xff01,
3512 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3513
3514 ha->tgt.tgt_ops->handle_data(cmd);
3515 return;
Arun Easic0cb4492014-09-25 06:14:51 -04003516 } else {
3517 ql_dbg(ql_dbg_io, vha, 0xff03,
3518 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3519 cmd->state);
3520 dump_stack();
3521 }
3522
Quinn Trane5fdee82015-06-10 11:05:21 -04003523 cmd->cmd_flags |= BIT_17;
Arun Easic0cb4492014-09-25 06:14:51 -04003524 ha->tgt.tgt_ops->free_cmd(cmd);
3525}
3526
3527void
3528qlt_host_reset_handler(struct qla_hw_data *ha)
3529{
3530 struct qla_tgt_cmd *cmd;
3531 unsigned long flags;
3532 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3533 scsi_qla_host_t *vha = NULL;
3534 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3535 uint32_t i;
3536
3537 if (!base_vha->hw->tgt.tgt_ops)
3538 return;
3539
3540 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3541 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3542 "Target mode disabled\n");
3543 return;
3544 }
3545
3546 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3547 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3548 base_vha->dpc_flags);
3549
3550 spin_lock_irqsave(&ha->hardware_lock, flags);
3551 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3552 cmd = qlt_get_cmd(base_vha, i);
3553 if (!cmd)
3554 continue;
3555 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3556 vha = cmd->vha;
3557 qlt_abort_cmd_on_host_reset(vha, cmd);
3558 }
3559 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3560}
3561
3562
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003563/*
3564 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3565 */
3566static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3567 uint32_t status, void *ctio)
3568{
3569 struct qla_hw_data *ha = vha->hw;
3570 struct se_cmd *se_cmd;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003571 struct qla_tgt_cmd *cmd;
3572
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003573 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3574 /* That could happen only in case of an error/reset/abort */
3575 if (status != CTIO_SUCCESS) {
3576 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3577 "Intermediate CTIO received"
3578 " (status %x)\n", status);
3579 }
3580 return;
3581 }
3582
3583 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003584 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003585 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003586
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003587 se_cmd = &cmd->se_cmd;
Quinn Trand564a372014-09-25 06:14:57 -04003588 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003589
Joern Engelf9b67212014-09-16 16:23:18 -04003590 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003591
3592 if (unlikely(status != CTIO_SUCCESS)) {
3593 switch (status & 0xFFFF) {
3594 case CTIO_LIP_RESET:
3595 case CTIO_TARGET_RESET:
3596 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003597 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003598 case CTIO_TIMEOUT:
3599 case CTIO_INVALID_RX_ID:
3600 /* They are OK */
3601 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3602 "qla_target(%d): CTIO with "
3603 "status %#x received, state %x, se_cmd %p, "
3604 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3605 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3606 status, cmd->state, se_cmd);
3607 break;
3608
3609 case CTIO_PORT_LOGGED_OUT:
3610 case CTIO_PORT_UNAVAILABLE:
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003611 {
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003612 int logged_out =
3613 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3614
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003615 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003616 "qla_target(%d): CTIO with %s status %x "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003617 "received (state %x, se_cmd %p)\n", vha->vp_idx,
Himanshu Madhanidacb5822016-01-20 15:42:58 -08003618 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003619 status, cmd->state, se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003620
Alexei Potashnik71cdc072015-12-17 14:57:01 -05003621 if (logged_out && cmd->sess) {
3622 /*
3623 * Session is already logged out, but we need
3624 * to notify initiator, who's not aware of this
3625 */
3626 cmd->sess->logout_on_delete = 0;
3627 cmd->sess->send_els_logo = 1;
3628 qlt_schedule_sess_for_deletion(cmd->sess, true);
3629 }
3630 break;
3631 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003632 case CTIO_SRR_RECEIVED:
3633 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
3634 "qla_target(%d): CTIO with SRR_RECEIVED"
3635 " status %x received (state %x, se_cmd %p)\n",
3636 vha->vp_idx, status, cmd->state, se_cmd);
3637 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
3638 break;
3639 else
3640 return;
3641
Quinn Tranf83adb62014-04-11 16:54:43 -04003642 case CTIO_DIF_ERROR: {
3643 struct ctio_crc_from_fw *crc =
3644 (struct ctio_crc_from_fw *)ctio;
3645 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3646 "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
3647 vha->vp_idx, status, cmd->state, se_cmd,
3648 *((u64 *)&crc->actual_dif[0]),
3649 *((u64 *)&crc->expected_dif[0]));
3650
3651 if (qlt_handle_dif_error(vha, cmd, ctio)) {
3652 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3653 /* scsi Write/xfer rdy complete */
3654 goto skip_term;
3655 } else {
3656 /* scsi read/xmit respond complete
3657 * call handle dif to send scsi status
3658 * rather than terminate exchange.
3659 */
3660 cmd->state = QLA_TGT_STATE_PROCESSED;
3661 ha->tgt.tgt_ops->handle_dif_err(cmd);
3662 return;
3663 }
3664 } else {
3665 /* Need to generate a SCSI good completion.
3666 * because FW did not send scsi status.
3667 */
3668 status = 0;
3669 goto skip_term;
3670 }
3671 break;
3672 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003673 default:
3674 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003675 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003676 vha->vp_idx, status, cmd->state, se_cmd);
3677 break;
3678 }
3679
Quinn Tran7b898542014-04-11 16:54:44 -04003680
Quinn Tran193b50b2015-12-17 14:57:03 -05003681 /* "cmd->aborted" means
Quinn Tran7b898542014-04-11 16:54:44 -04003682 * cmd is already aborted/terminated, we don't
3683 * need to terminate again. The exchange is already
3684 * cleaned up/freed at FW level. Just cleanup at driver
3685 * level.
3686 */
3687 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003688 (!cmd->aborted)) {
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003689 cmd->cmd_flags |= BIT_13;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003690 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3691 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003692 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003693 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003694skip_term:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003695
3696 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Quinn Trane5fdee82015-06-10 11:05:21 -04003697 cmd->cmd_flags |= BIT_12;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003698 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003699 cmd->state = QLA_TGT_STATE_DATA_IN;
3700
Bart Van Assche52c82822015-07-09 07:23:26 -07003701 if (status == CTIO_SUCCESS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003702 cmd->write_data_transferred = 1;
3703
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003704 ha->tgt.tgt_ops->handle_data(cmd);
3705 return;
Quinn Tran193b50b2015-12-17 14:57:03 -05003706 } else if (cmd->aborted) {
Quinn Trane5fdee82015-06-10 11:05:21 -04003707 cmd->cmd_flags |= BIT_18;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003708 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
Bart Van Assche649ee052015-04-14 13:26:44 +02003709 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003710 } else {
Quinn Trane5fdee82015-06-10 11:05:21 -04003711 cmd->cmd_flags |= BIT_19;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003712 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3713 "qla_target(%d): A command in state (%d) should "
3714 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3715 }
3716
Quinn Tran7b898542014-04-11 16:54:44 -04003717 if (unlikely(status != CTIO_SUCCESS) &&
Quinn Tran193b50b2015-12-17 14:57:03 -05003718 !cmd->aborted) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003719 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3720 dump_stack();
3721 }
3722
3723 ha->tgt.tgt_ops->free_cmd(cmd);
3724}
3725
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003726static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3727 uint8_t task_codes)
3728{
3729 int fcp_task_attr;
3730
3731 switch (task_codes) {
3732 case ATIO_SIMPLE_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003733 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003734 break;
3735 case ATIO_HEAD_OF_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003736 fcp_task_attr = TCM_HEAD_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003737 break;
3738 case ATIO_ORDERED_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003739 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003740 break;
3741 case ATIO_ACA_QUEUE:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003742 fcp_task_attr = TCM_ACA_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003743 break;
3744 case ATIO_UNTAGGED:
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003745 fcp_task_attr = TCM_SIMPLE_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003746 break;
3747 default:
3748 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3749 "qla_target: unknown task code %x, use ORDERED instead\n",
3750 task_codes);
Christoph Hellwig68d81f42014-11-24 07:07:25 -08003751 fcp_task_attr = TCM_ORDERED_TAG;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003752 break;
3753 }
3754
3755 return fcp_task_attr;
3756}
3757
3758static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
3759 uint8_t *);
3760/*
3761 * Process context for I/O path into tcm_qla2xxx code
3762 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003763static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003764{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003765 scsi_qla_host_t *vha = cmd->vha;
3766 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003767 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003768 struct qla_tgt_sess *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003769 struct atio_from_isp *atio = &cmd->atio;
3770 unsigned char *cdb;
3771 unsigned long flags;
3772 uint32_t data_length;
3773 int ret, fcp_task_attr, data_dir, bidi = 0;
3774
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003775 cmd->cmd_in_wq = 0;
3776 cmd->cmd_flags |= BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003777 if (tgt->tgt_stop)
3778 goto out_term;
3779
Quinn Tran193b50b2015-12-17 14:57:03 -05003780 if (cmd->aborted) {
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003781 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
3782 "cmd with tag %u is aborted\n",
3783 cmd->atio.u.isp24.exchange_addr);
3784 goto out_term;
3785 }
3786
Quinn Trana07100e2015-12-07 19:48:57 -05003787 spin_lock_init(&cmd->cmd_lock);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003788 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
Bart Van Assche649ee052015-04-14 13:26:44 +02003789 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003790 cmd->unpacked_lun = scsilun_to_int(
3791 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3792
3793 if (atio->u.isp24.fcp_cmnd.rddata &&
3794 atio->u.isp24.fcp_cmnd.wrdata) {
3795 bidi = 1;
3796 data_dir = DMA_TO_DEVICE;
3797 } else if (atio->u.isp24.fcp_cmnd.rddata)
3798 data_dir = DMA_FROM_DEVICE;
3799 else if (atio->u.isp24.fcp_cmnd.wrdata)
3800 data_dir = DMA_TO_DEVICE;
3801 else
3802 data_dir = DMA_NONE;
3803
3804 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3805 atio->u.isp24.fcp_cmnd.task_attr);
3806 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3807 &atio->u.isp24.fcp_cmnd.add_cdb[
3808 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3809
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003810 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3811 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003812 if (ret != 0)
3813 goto out_term;
3814 /*
3815 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3816 */
Quinn Tran75601512015-12-17 14:57:04 -05003817 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003818 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05003819 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003820 return;
3821
3822out_term:
Arun Easi667024a2014-09-25 06:14:47 -04003823 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003824 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07003825 * cmd has not sent to target yet, so pass NULL as the second
3826 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003827 */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003828 cmd->cmd_flags |= BIT_2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003829 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003830 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04003831
3832 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003833 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Jörn Engel08234e32013-06-12 16:27:54 -04003834 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Quinn Tran75601512015-12-17 14:57:04 -05003835
3836 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
3837 ha->tgt.tgt_ops->put_sess(sess);
3838 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003839}
3840
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003841static void qlt_do_work(struct work_struct *work)
3842{
3843 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003844 scsi_qla_host_t *vha = cmd->vha;
3845 unsigned long flags;
3846
3847 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3848 list_del(&cmd->cmd_list);
3849 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003850
3851 __qlt_do_work(cmd);
3852}
3853
3854static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
3855 struct qla_tgt_sess *sess,
3856 struct atio_from_isp *atio)
3857{
3858 struct se_session *se_sess = sess->se_sess;
3859 struct qla_tgt_cmd *cmd;
3860 int tag;
3861
3862 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
3863 if (tag < 0)
3864 return NULL;
3865
3866 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
3867 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
3868
3869 memcpy(&cmd->atio, atio, sizeof(*atio));
3870 cmd->state = QLA_TGT_STATE_NEW;
3871 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04003872 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003873 cmd->vha = vha;
3874 cmd->se_cmd.map_tag = tag;
3875 cmd->sess = sess;
3876 cmd->loop_id = sess->loop_id;
3877 cmd->conf_compl_supported = sess->conf_compl_supported;
3878
Kanoj Sarcar9fce1252015-06-10 11:05:23 -04003879 cmd->cmd_flags = 0;
3880 cmd->jiffies_at_alloc = get_jiffies_64();
3881
3882 cmd->reset_count = vha->hw->chip_reset;
3883
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003884 return cmd;
3885}
3886
3887static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
3888 uint16_t);
3889
3890static void qlt_create_sess_from_atio(struct work_struct *work)
3891{
3892 struct qla_tgt_sess_op *op = container_of(work,
3893 struct qla_tgt_sess_op, work);
3894 scsi_qla_host_t *vha = op->vha;
3895 struct qla_hw_data *ha = vha->hw;
3896 struct qla_tgt_sess *sess;
3897 struct qla_tgt_cmd *cmd;
3898 unsigned long flags;
3899 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
3900
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003901 spin_lock_irqsave(&vha->cmd_list_lock, flags);
3902 list_del(&op->cmd_list);
3903 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
3904
3905 if (op->aborted) {
3906 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf083,
3907 "sess_op with tag %u is aborted\n",
3908 op->atio.u.isp24.exchange_addr);
3909 goto out_term;
3910 }
3911
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003912 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003913 "qla_target(%d): Unable to find wwn login"
3914 " (s_id %x:%x:%x), trying to create it manually\n",
3915 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003916
3917 if (op->atio.u.raw.entry_count > 1) {
3918 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003919 "Dropping multy entry atio %p\n", &op->atio);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003920 goto out_term;
3921 }
3922
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003923 sess = qlt_make_local_sess(vha, s_id);
3924 /* sess has an extra creation ref. */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003925
3926 if (!sess)
3927 goto out_term;
3928 /*
3929 * Now obtain a pre-allocated session tag using the original op->atio
3930 * packet header, and dispatch into __qlt_do_work() using the existing
3931 * process context.
3932 */
3933 cmd = qlt_get_tag(vha, sess, &op->atio);
3934 if (!cmd) {
3935 spin_lock_irqsave(&ha->hardware_lock, flags);
3936 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
3937 ha->tgt.tgt_ops->put_sess(sess);
3938 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3939 kfree(op);
3940 return;
3941 }
3942 /*
3943 * __qlt_do_work() will call ha->tgt.tgt_ops->put_sess() to release
3944 * the extra reference taken above by qlt_make_local_sess()
3945 */
3946 __qlt_do_work(cmd);
3947 kfree(op);
3948 return;
3949
3950out_term:
3951 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Trana07100e2015-12-07 19:48:57 -05003952 qlt_send_term_exchange(vha, NULL, &op->atio, 1, 0);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003953 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3954 kfree(op);
3955
3956}
3957
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003958/* ha->hardware_lock supposed to be held on entry */
3959static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
3960 struct atio_from_isp *atio)
3961{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003962 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003963 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003964 struct qla_tgt_sess *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003965 struct qla_tgt_cmd *cmd;
3966
3967 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04003968 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003969 "New command while device %p is shutting down\n", tgt);
3970 return -EFAULT;
3971 }
3972
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003973 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
3974 if (unlikely(!sess)) {
3975 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
3976 GFP_ATOMIC);
3977 if (!op)
3978 return -ENOMEM;
3979
3980 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04003981 op->vha = vha;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04003982
3983 spin_lock(&vha->cmd_list_lock);
3984 list_add_tail(&op->cmd_list, &vha->qla_sess_op_cmd_list);
3985 spin_unlock(&vha->cmd_list_lock);
3986
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003987 INIT_WORK(&op->work, qlt_create_sess_from_atio);
3988 queue_work(qla_tgt_wq, &op->work);
3989 return 0;
3990 }
Alexei Potashnike52a8b42015-07-14 16:00:48 -04003991
3992 /* Another WWN used to have our s_id. Our PLOGI scheduled its
3993 * session deletion, but it's still in sess_del_work wq */
3994 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
3995 ql_dbg(ql_dbg_io, vha, 0x3061,
3996 "New command while old session %p is being deleted\n",
3997 sess);
3998 return -EFAULT;
3999 }
4000
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004001 /*
4002 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4003 */
4004 kref_get(&sess->se_sess->sess_kref);
4005
4006 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004007 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04004008 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004009 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07004010 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004011 return -ENOMEM;
4012 }
4013
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004014 cmd->cmd_in_wq = 1;
4015 cmd->cmd_flags |= BIT_0;
Quinn Tran5327c7d2016-02-10 18:59:14 -05004016 cmd->se_cmd.cpuid = ha->msix_count ?
4017 ha->tgt.rspq_vector_cpuid : WORK_CPU_UNBOUND;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004018
4019 spin_lock(&vha->cmd_list_lock);
4020 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4021 spin_unlock(&vha->cmd_list_lock);
4022
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004023 INIT_WORK(&cmd->work, qlt_do_work);
Quinn Tranfb3269b2015-12-17 14:57:06 -05004024 if (ha->msix_count) {
Quinn Tranfb3269b2015-12-17 14:57:06 -05004025 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4026 queue_work_on(smp_processor_id(), qla_tgt_wq,
4027 &cmd->work);
4028 else
4029 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4030 &cmd->work);
4031 } else {
4032 queue_work(qla_tgt_wq, &cmd->work);
4033 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004034 return 0;
4035
4036}
4037
4038/* ha->hardware_lock supposed to be held on entry */
4039static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
4040 int fn, void *iocb, int flags)
4041{
4042 struct scsi_qla_host *vha = sess->vha;
4043 struct qla_hw_data *ha = vha->hw;
4044 struct qla_tgt_mgmt_cmd *mcmd;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004045 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004046 int res;
4047 uint8_t tmr_func;
4048
4049 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4050 if (!mcmd) {
4051 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4052 "qla_target(%d): Allocation of management "
4053 "command failed, some commands and their data could "
4054 "leak\n", vha->vp_idx);
4055 return -ENOMEM;
4056 }
4057 memset(mcmd, 0, sizeof(*mcmd));
4058 mcmd->sess = sess;
4059
4060 if (iocb) {
4061 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4062 sizeof(mcmd->orig_iocb.imm_ntfy));
4063 }
4064 mcmd->tmr_func = fn;
4065 mcmd->flags = flags;
Arun Easib6a029e2014-09-25 06:14:52 -04004066 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004067
4068 switch (fn) {
4069 case QLA_TGT_CLEAR_ACA:
4070 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
4071 "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
4072 tmr_func = TMR_CLEAR_ACA;
4073 break;
4074
4075 case QLA_TGT_TARGET_RESET:
4076 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
4077 "qla_target(%d): TARGET_RESET received\n",
4078 sess->vha->vp_idx);
4079 tmr_func = TMR_TARGET_WARM_RESET;
4080 break;
4081
4082 case QLA_TGT_LUN_RESET:
4083 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
4084 "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
4085 tmr_func = TMR_LUN_RESET;
Swapnil Nagle8b2f5ff2015-07-14 16:00:43 -04004086 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004087 break;
4088
4089 case QLA_TGT_CLEAR_TS:
4090 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
4091 "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
4092 tmr_func = TMR_CLEAR_TASK_SET;
4093 break;
4094
4095 case QLA_TGT_ABORT_TS:
4096 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
4097 "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
4098 tmr_func = TMR_ABORT_TASK_SET;
4099 break;
4100#if 0
4101 case QLA_TGT_ABORT_ALL:
4102 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
4103 "qla_target(%d): Doing ABORT_ALL_TASKS\n",
4104 sess->vha->vp_idx);
4105 tmr_func = 0;
4106 break;
4107
4108 case QLA_TGT_ABORT_ALL_SESS:
4109 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
4110 "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
4111 sess->vha->vp_idx);
4112 tmr_func = 0;
4113 break;
4114
4115 case QLA_TGT_NEXUS_LOSS_SESS:
4116 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
4117 "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
4118 sess->vha->vp_idx);
4119 tmr_func = 0;
4120 break;
4121
4122 case QLA_TGT_NEXUS_LOSS:
4123 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
4124 "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
4125 tmr_func = 0;
4126 break;
4127#endif
4128 default:
4129 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
4130 "qla_target(%d): Unknown task mgmt fn 0x%x\n",
4131 sess->vha->vp_idx, fn);
4132 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4133 return -ENOSYS;
4134 }
4135
4136 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
4137 if (res != 0) {
4138 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
4139 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
4140 sess->vha->vp_idx, res);
4141 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4142 return -EFAULT;
4143 }
4144
4145 return 0;
4146}
4147
4148/* ha->hardware_lock supposed to be held on entry */
4149static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4150{
4151 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4152 struct qla_hw_data *ha = vha->hw;
4153 struct qla_tgt *tgt;
4154 struct qla_tgt_sess *sess;
4155 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07004156 int fn;
Quinn Tran75601512015-12-17 14:57:04 -05004157 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004158
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004159 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004160
4161 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004162 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
Quinn Tran75601512015-12-17 14:57:04 -05004163
4164 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004165 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4166 a->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05004167 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4168
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004169 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
4170
4171 if (!sess) {
4172 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
4173 "qla_target(%d): task mgmt fn 0x%x for "
4174 "non-existant session\n", vha->vp_idx, fn);
4175 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
4176 sizeof(struct atio_from_isp));
4177 }
4178
Alexei Potashnike52a8b42015-07-14 16:00:48 -04004179 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS)
4180 return -EFAULT;
4181
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004182 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4183}
4184
4185/* ha->hardware_lock supposed to be held on entry */
4186static int __qlt_abort_task(struct scsi_qla_host *vha,
4187 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
4188{
4189 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4190 struct qla_hw_data *ha = vha->hw;
4191 struct qla_tgt_mgmt_cmd *mcmd;
4192 uint32_t lun, unpacked_lun;
4193 int rc;
4194
4195 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4196 if (mcmd == NULL) {
4197 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4198 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4199 vha->vp_idx, __func__);
4200 return -ENOMEM;
4201 }
4202 memset(mcmd, 0, sizeof(*mcmd));
4203
4204 mcmd->sess = sess;
4205 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4206 sizeof(mcmd->orig_iocb.imm_ntfy));
4207
4208 lun = a->u.isp24.fcp_cmnd.lun;
4209 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
Arun Easi80187f82014-09-25 06:14:53 -04004210 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004211
4212 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
4213 le16_to_cpu(iocb->u.isp2x.seq_id));
4214 if (rc != 0) {
4215 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4216 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4217 vha->vp_idx, rc);
4218 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4219 return -EFAULT;
4220 }
4221
4222 return 0;
4223}
4224
4225/* ha->hardware_lock supposed to be held on entry */
4226static int qlt_abort_task(struct scsi_qla_host *vha,
4227 struct imm_ntfy_from_isp *iocb)
4228{
4229 struct qla_hw_data *ha = vha->hw;
4230 struct qla_tgt_sess *sess;
4231 int loop_id;
Quinn Tran75601512015-12-17 14:57:04 -05004232 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004233
4234 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4235
Quinn Tran75601512015-12-17 14:57:04 -05004236 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004237 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
Quinn Tran75601512015-12-17 14:57:04 -05004238 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4239
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004240 if (sess == NULL) {
4241 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4242 "qla_target(%d): task abort for unexisting "
4243 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004244 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004245 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4246 }
4247
4248 return __qlt_abort_task(vha, iocb, sess);
4249}
4250
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004251void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4252{
4253 if (fcport->tgt_session) {
4254 if (rc != MBS_COMMAND_COMPLETE) {
Alexei Potashnikdf673272015-07-14 16:00:46 -04004255 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004256 "%s: se_sess %p / sess %p from"
4257 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4258 " LOGO failed: %#x\n",
4259 __func__,
4260 fcport->tgt_session->se_sess,
4261 fcport->tgt_session,
4262 fcport->port_name, fcport->loop_id,
4263 fcport->d_id.b.domain, fcport->d_id.b.area,
4264 fcport->d_id.b.al_pa, rc);
4265 }
4266
4267 fcport->tgt_session->logout_completed = 1;
4268 }
4269}
4270
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004271/*
4272* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4273*
4274* Schedules sessions with matching port_id/loop_id but different wwn for
4275* deletion. Returns existing session with matching wwn if present.
4276* Null otherwise.
4277*/
4278static struct qla_tgt_sess *
4279qlt_find_sess_invalidate_other(struct qla_tgt *tgt, uint64_t wwn,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004280 port_id_t port_id, uint16_t loop_id, struct qla_tgt_sess **conflict_sess)
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004281{
4282 struct qla_tgt_sess *sess = NULL, *other_sess;
4283 uint64_t other_wwn;
4284
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004285 *conflict_sess = NULL;
4286
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004287 list_for_each_entry(other_sess, &tgt->sess_list, sess_list_entry) {
4288
4289 other_wwn = wwn_to_u64(other_sess->port_name);
4290
4291 if (wwn == other_wwn) {
4292 WARN_ON(sess);
4293 sess = other_sess;
4294 continue;
4295 }
4296
4297 /* find other sess with nport_id collision */
4298 if (port_id.b24 == other_sess->s_id.b24) {
4299 if (loop_id != other_sess->loop_id) {
4300 ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000c,
4301 "Invalidating sess %p loop_id %d wwn %llx.\n",
4302 other_sess, other_sess->loop_id, other_wwn);
4303
4304 /*
4305 * logout_on_delete is set by default, but another
4306 * session that has the same s_id/loop_id combo
4307 * might have cleared it when requested this session
4308 * deletion, so don't touch it
4309 */
4310 qlt_schedule_sess_for_deletion(other_sess, true);
4311 } else {
4312 /*
4313 * Another wwn used to have our s_id/loop_id
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004314 * kill the session, but don't free the loop_id
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004315 */
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004316 other_sess->keep_nport_handle = 1;
4317 *conflict_sess = other_sess;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004318 qlt_schedule_sess_for_deletion(other_sess,
4319 true);
4320 }
4321 continue;
4322 }
4323
4324 /* find other sess with nport handle collision */
4325 if (loop_id == other_sess->loop_id) {
4326 ql_dbg(ql_dbg_tgt_tmr, tgt->vha, 0x1000d,
4327 "Invalidating sess %p loop_id %d wwn %llx.\n",
4328 other_sess, other_sess->loop_id, other_wwn);
4329
4330 /* Same loop_id but different s_id
4331 * Ok to kill and logout */
4332 qlt_schedule_sess_for_deletion(other_sess, true);
4333 }
4334 }
4335
4336 return sess;
4337}
4338
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004339/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4340static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4341{
4342 struct qla_tgt_sess_op *op;
4343 struct qla_tgt_cmd *cmd;
4344 uint32_t key;
4345 int count = 0;
4346
4347 key = (((u32)s_id->b.domain << 16) |
4348 ((u32)s_id->b.area << 8) |
4349 ((u32)s_id->b.al_pa));
4350
4351 spin_lock(&vha->cmd_list_lock);
4352 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4353 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4354 if (op_key == key) {
4355 op->aborted = true;
4356 count++;
4357 }
4358 }
4359 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4360 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4361 if (cmd_key == key) {
Quinn Tran193b50b2015-12-17 14:57:03 -05004362 cmd->aborted = 1;
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004363 count++;
4364 }
4365 }
4366 spin_unlock(&vha->cmd_list_lock);
4367
4368 return count;
4369}
4370
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004371/*
4372 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4373 */
4374static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4375 struct imm_ntfy_from_isp *iocb)
4376{
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004377 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004378 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004379 struct qla_tgt_sess *sess = NULL, *conflict_sess = NULL;
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004380 uint64_t wwn;
4381 port_id_t port_id;
4382 uint16_t loop_id;
4383 uint16_t wd3_lo;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004384 int res = 0;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004385 qlt_plogi_ack_t *pla;
Quinn Tran75601512015-12-17 14:57:04 -05004386 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004387
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004388 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4389
4390 port_id.b.domain = iocb->u.isp24.port_id[2];
4391 port_id.b.area = iocb->u.isp24.port_id[1];
4392 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4393 port_id.b.rsvd_1 = 0;
4394
4395 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4396
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004397 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004398 "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
4399 vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004400
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004401 /* res = 1 means ack at the end of thread
4402 * res = 0 means ack async/later.
4403 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004404 switch (iocb->u.isp24.status_subcode) {
4405 case ELS_PLOGI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004406
Alexei Potashnikdaddf5c2015-07-14 16:00:45 -04004407 /* Mark all stale commands in qla_tgt_wq for deletion */
4408 abort_cmds_for_s_id(vha, &port_id);
4409
Quinn Tran75601512015-12-17 14:57:04 -05004410 if (wwn) {
4411 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004412 sess = qlt_find_sess_invalidate_other(tgt, wwn,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004413 port_id, loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004414 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4415 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004416
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004417 if (IS_SW_RESV_ADDR(port_id) || (!sess && !conflict_sess)) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004418 res = 1;
4419 break;
4420 }
4421
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004422 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4423 if (!pla) {
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004424 qlt_send_term_imm_notif(vha, iocb, 1);
4425
4426 res = 0;
4427 break;
4428 }
4429
4430 res = 0;
4431
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004432 if (conflict_sess)
4433 qlt_plogi_ack_link(vha, pla, conflict_sess,
4434 QLT_PLOGI_LINK_CONFLICT);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004435
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004436 if (!sess)
4437 break;
4438
4439 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004440 /*
4441 * Under normal circumstances we want to release nport handle
4442 * during LOGO process to avoid nport handle leaks inside FW.
4443 * The exception is when LOGO is done while another PLOGI with
4444 * the same nport handle is waiting as might be the case here.
4445 * Note: there is always a possibily of a race where session
4446 * deletion has already started for other reasons (e.g. ACL
4447 * removal) and now PLOGI arrives:
4448 * 1. if PLOGI arrived in FW after nport handle has been freed,
4449 * FW must have assigned this PLOGI a new/same handle and we
4450 * can proceed ACK'ing it as usual when session deletion
4451 * completes.
4452 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4453 * bit reached it, the handle has now been released. We'll
4454 * get an error when we ACK this PLOGI. Nothing will be sent
4455 * back to initiator. Initiator should eventually retry
4456 * PLOGI and situation will correct itself.
4457 */
4458 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4459 (sess->s_id.b24 == port_id.b24));
4460 qlt_schedule_sess_for_deletion(sess, true);
4461 break;
4462
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004463 case ELS_PRLI:
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004464 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4465
Quinn Tran75601512015-12-17 14:57:04 -05004466 if (wwn) {
4467 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004468 sess = qlt_find_sess_invalidate_other(tgt, wwn, port_id,
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004469 loop_id, &conflict_sess);
Quinn Tran75601512015-12-17 14:57:04 -05004470 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4471 }
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05004472
4473 if (conflict_sess) {
4474 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4475 "PRLI with conflicting sess %p port %8phC\n",
4476 conflict_sess, conflict_sess->port_name);
4477 qlt_send_term_imm_notif(vha, iocb, 1);
4478 res = 0;
4479 break;
4480 }
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004481
4482 if (sess != NULL) {
4483 if (sess->deleted) {
4484 /*
4485 * Impatient initiator sent PRLI before last
4486 * PLOGI could finish. Will force him to re-try,
4487 * while last one finishes.
4488 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004489 ql_log(ql_log_warn, sess->vha, 0xf095,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004490 "sess %p PRLI received, before plogi ack.\n",
4491 sess);
4492 qlt_send_term_imm_notif(vha, iocb, 1);
4493 res = 0;
4494 break;
4495 }
4496
4497 /*
4498 * This shouldn't happen under normal circumstances,
4499 * since we have deleted the old session during PLOGI
4500 */
Alexei Potashnikdf673272015-07-14 16:00:46 -04004501 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004502 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
4503 sess->loop_id, sess, iocb->u.isp24.nport_handle);
4504
4505 sess->local = 0;
4506 sess->loop_id = loop_id;
4507 sess->s_id = port_id;
4508
4509 if (wd3_lo & BIT_7)
4510 sess->conf_compl_supported = 1;
4511
Alexei Potashnikdf673272015-07-14 16:00:46 -04004512 }
4513 res = 1; /* send notify ack */
4514
4515 /* Make session global (not used in fabric mode) */
4516 if (ha->current_topology != ISP_CFG_F) {
4517 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4518 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4519 qla2xxx_wake_dpc(vha);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004520 } else {
4521 /* todo: else - create sess here. */
4522 res = 1; /* send notify ack */
4523 }
4524
4525 break;
4526
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004527 case ELS_LOGO:
4528 case ELS_PRLO:
4529 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4530 break;
4531 case ELS_PDISC:
4532 case ELS_ADISC:
4533 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004534 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004535 if (tgt->link_reinit_iocb_pending) {
4536 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4537 0, 0, 0, 0, 0, 0);
4538 tgt->link_reinit_iocb_pending = 0;
4539 }
4540 res = 1; /* send notify ack */
4541 break;
4542 }
4543
Alexei Potashnika6ca8872015-07-14 16:00:44 -04004544 case ELS_FLOGI: /* should never happen */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004545 default:
4546 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
4547 "qla_target(%d): Unsupported ELS command %x "
4548 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
4549 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
4550 break;
4551 }
4552
4553 return res;
4554}
4555
4556static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
4557{
Bart Van Assche5897cb22015-06-04 15:57:20 -07004558#if 1
4559 /*
4560 * FIXME: Reject non zero SRR relative offset until we can test
4561 * this code properly.
4562 */
4563 pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
4564 return -1;
4565#else
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004566 struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
4567 size_t first_offset = 0, rem_offset = offset, tmp = 0;
4568 int i, sg_srr_cnt, bufflen = 0;
4569
4570 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
4571 "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
4572 "cmd->sg_cnt: %u, direction: %d\n",
4573 cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
4574
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004575 if (!cmd->sg || !cmd->sg_cnt) {
4576 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
4577 "Missing cmd->sg or zero cmd->sg_cnt in"
4578 " qla_tgt_set_data_offset\n");
4579 return -EINVAL;
4580 }
4581 /*
4582 * Walk the current cmd->sg list until we locate the new sg_srr_start
4583 */
4584 for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
4585 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
4586 "sg[%d]: %p page: %p, length: %d, offset: %d\n",
4587 i, sg, sg_page(sg), sg->length, sg->offset);
4588
4589 if ((sg->length + tmp) > offset) {
4590 first_offset = rem_offset;
4591 sg_srr_start = sg;
4592 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
4593 "Found matching sg[%d], using %p as sg_srr_start, "
4594 "and using first_offset: %zu\n", i, sg,
4595 first_offset);
4596 break;
4597 }
4598 tmp += sg->length;
4599 rem_offset -= sg->length;
4600 }
4601
4602 if (!sg_srr_start) {
4603 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
4604 "Unable to locate sg_srr_start for offset: %u\n", offset);
4605 return -EINVAL;
4606 }
4607 sg_srr_cnt = (cmd->sg_cnt - i);
4608
4609 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
4610 if (!sg_srr) {
4611 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
4612 "Unable to allocate sgp\n");
4613 return -ENOMEM;
4614 }
4615 sg_init_table(sg_srr, sg_srr_cnt);
4616 sgp = &sg_srr[0];
4617 /*
4618 * Walk the remaining list for sg_srr_start, mapping to the newly
4619 * allocated sg_srr taking first_offset into account.
4620 */
4621 for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
4622 if (first_offset) {
4623 sg_set_page(sgp, sg_page(sg),
4624 (sg->length - first_offset), first_offset);
4625 first_offset = 0;
4626 } else {
4627 sg_set_page(sgp, sg_page(sg), sg->length, 0);
4628 }
4629 bufflen += sgp->length;
4630
4631 sgp = sg_next(sgp);
4632 if (!sgp)
4633 break;
4634 }
4635
4636 cmd->sg = sg_srr;
4637 cmd->sg_cnt = sg_srr_cnt;
4638 cmd->bufflen = bufflen;
4639 cmd->offset += offset;
4640 cmd->free_sg = 1;
4641
4642 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
4643 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
4644 cmd->sg_cnt);
4645 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
4646 cmd->bufflen);
4647 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
4648 cmd->offset);
4649
4650 if (cmd->sg_cnt < 0)
4651 BUG();
4652
4653 if (cmd->bufflen < 0)
4654 BUG();
4655
4656 return 0;
Bart Van Assche5897cb22015-06-04 15:57:20 -07004657#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004658}
4659
4660static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
4661 uint32_t srr_rel_offs, int *xmit_type)
4662{
4663 int res = 0, rel_offs;
4664
4665 rel_offs = srr_rel_offs - cmd->offset;
4666 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
4667 srr_rel_offs, rel_offs);
4668
4669 *xmit_type = QLA_TGT_XMIT_ALL;
4670
4671 if (rel_offs < 0) {
4672 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
4673 "qla_target(%d): SRR rel_offs (%d) < 0",
4674 cmd->vha->vp_idx, rel_offs);
4675 res = -1;
4676 } else if (rel_offs == cmd->bufflen)
4677 *xmit_type = QLA_TGT_XMIT_STATUS;
4678 else if (rel_offs > 0)
4679 res = qlt_set_data_offset(cmd, rel_offs);
4680
4681 return res;
4682}
4683
4684/* No locks, thread context */
4685static void qlt_handle_srr(struct scsi_qla_host *vha,
4686 struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
4687{
4688 struct imm_ntfy_from_isp *ntfy =
4689 (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
4690 struct qla_hw_data *ha = vha->hw;
4691 struct qla_tgt_cmd *cmd = sctio->cmd;
4692 struct se_cmd *se_cmd = &cmd->se_cmd;
4693 unsigned long flags;
4694 int xmit_type = 0, resp = 0;
4695 uint32_t offset;
4696 uint16_t srr_ui;
4697
4698 offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
4699 srr_ui = ntfy->u.isp24.srr_ui;
4700
4701 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
4702 cmd, srr_ui);
4703
4704 switch (srr_ui) {
4705 case SRR_IU_STATUS:
4706 spin_lock_irqsave(&ha->hardware_lock, flags);
4707 qlt_send_notify_ack(vha, ntfy,
4708 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4709 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4710 xmit_type = QLA_TGT_XMIT_STATUS;
4711 resp = 1;
4712 break;
4713 case SRR_IU_DATA_IN:
4714 if (!cmd->sg || !cmd->sg_cnt) {
4715 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
4716 "Unable to process SRR_IU_DATA_IN due to"
4717 " missing cmd->sg, state: %d\n", cmd->state);
4718 dump_stack();
4719 goto out_reject;
4720 }
4721 if (se_cmd->scsi_status != 0) {
4722 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
4723 "Rejecting SRR_IU_DATA_IN with non GOOD "
4724 "scsi_status\n");
4725 goto out_reject;
4726 }
4727 cmd->bufflen = se_cmd->data_length;
4728
4729 if (qlt_has_data(cmd)) {
4730 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
4731 goto out_reject;
4732 spin_lock_irqsave(&ha->hardware_lock, flags);
4733 qlt_send_notify_ack(vha, ntfy,
4734 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4735 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4736 resp = 1;
4737 } else {
4738 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
Bart Van Assche649ee052015-04-14 13:26:44 +02004739 "qla_target(%d): SRR for in data for cmd without them (tag %lld, SCSI status %d), reject",
4740 vha->vp_idx, se_cmd->tag,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004741 cmd->se_cmd.scsi_status);
4742 goto out_reject;
4743 }
4744 break;
4745 case SRR_IU_DATA_OUT:
4746 if (!cmd->sg || !cmd->sg_cnt) {
4747 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
4748 "Unable to process SRR_IU_DATA_OUT due to"
4749 " missing cmd->sg\n");
4750 dump_stack();
4751 goto out_reject;
4752 }
4753 if (se_cmd->scsi_status != 0) {
4754 ql_dbg(ql_dbg_tgt, vha, 0xe02b,
4755 "Rejecting SRR_IU_DATA_OUT"
4756 " with non GOOD scsi_status\n");
4757 goto out_reject;
4758 }
4759 cmd->bufflen = se_cmd->data_length;
4760
4761 if (qlt_has_data(cmd)) {
4762 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
4763 goto out_reject;
4764 spin_lock_irqsave(&ha->hardware_lock, flags);
4765 qlt_send_notify_ack(vha, ntfy,
4766 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
4767 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004768 if (xmit_type & QLA_TGT_XMIT_DATA) {
4769 cmd->cmd_flags |= BIT_8;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004770 qlt_rdy_to_xfer(cmd);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004771 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004772 } else {
4773 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
Bart Van Assche649ee052015-04-14 13:26:44 +02004774 "qla_target(%d): SRR for out data for cmd without them (tag %lld, SCSI status %d), reject",
4775 vha->vp_idx, se_cmd->tag, cmd->se_cmd.scsi_status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004776 goto out_reject;
4777 }
4778 break;
4779 default:
4780 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
4781 "qla_target(%d): Unknown srr_ui value %x",
4782 vha->vp_idx, srr_ui);
4783 goto out_reject;
4784 }
4785
4786 /* Transmit response in case of status and data-in cases */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004787 if (resp) {
4788 cmd->cmd_flags |= BIT_7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004789 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004790 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004791
4792 return;
4793
4794out_reject:
4795 spin_lock_irqsave(&ha->hardware_lock, flags);
4796 qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
4797 NOTIFY_ACK_SRR_FLAGS_REJECT,
4798 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4799 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4800 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4801 cmd->state = QLA_TGT_STATE_DATA_IN;
4802 dump_stack();
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004803 } else {
4804 cmd->cmd_flags |= BIT_9;
Quinn Trana07100e2015-12-07 19:48:57 -05004805 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1, 0);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004806 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004807 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4808}
4809
4810static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
4811 struct qla_tgt_srr_imm *imm, int ha_locked)
4812{
4813 struct qla_hw_data *ha = vha->hw;
4814 unsigned long flags = 0;
4815
Bart Van Assche8d163662015-07-09 07:25:46 -07004816#ifndef __CHECKER__
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004817 if (!ha_locked)
4818 spin_lock_irqsave(&ha->hardware_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -07004819#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004820
4821 qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
4822 NOTIFY_ACK_SRR_FLAGS_REJECT,
4823 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4824 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4825
Bart Van Assche8d163662015-07-09 07:25:46 -07004826#ifndef __CHECKER__
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004827 if (!ha_locked)
4828 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -07004829#endif
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004830
4831 kfree(imm);
4832}
4833
4834static void qlt_handle_srr_work(struct work_struct *work)
4835{
4836 struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
4837 struct scsi_qla_host *vha = tgt->vha;
4838 struct qla_tgt_srr_ctio *sctio;
4839 unsigned long flags;
4840
4841 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
4842 tgt);
4843
4844restart:
4845 spin_lock_irqsave(&tgt->srr_lock, flags);
4846 list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
4847 struct qla_tgt_srr_imm *imm, *i, *ti;
4848 struct qla_tgt_cmd *cmd;
4849 struct se_cmd *se_cmd;
4850
4851 imm = NULL;
4852 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
4853 srr_list_entry) {
4854 if (i->srr_id == sctio->srr_id) {
4855 list_del(&i->srr_list_entry);
4856 if (imm) {
4857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
4858 "qla_target(%d): There must be "
4859 "only one IMM SRR per CTIO SRR "
4860 "(IMM SRR %p, id %d, CTIO %p\n",
4861 vha->vp_idx, i, i->srr_id, sctio);
4862 qlt_reject_free_srr_imm(tgt->vha, i, 0);
4863 } else
4864 imm = i;
4865 }
4866 }
4867
4868 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
4869 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
4870 sctio->srr_id);
4871
4872 if (imm == NULL) {
4873 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
4874 "Not found matching IMM for SRR CTIO (id %d)\n",
4875 sctio->srr_id);
4876 continue;
4877 } else
4878 list_del(&sctio->srr_list_entry);
4879
4880 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4881
4882 cmd = sctio->cmd;
4883 /*
4884 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
4885 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
4886 * logic..
4887 */
4888 cmd->offset = 0;
4889 if (cmd->free_sg) {
4890 kfree(cmd->sg);
4891 cmd->sg = NULL;
4892 cmd->free_sg = 0;
4893 }
4894 se_cmd = &cmd->se_cmd;
4895
4896 cmd->sg_cnt = se_cmd->t_data_nents;
4897 cmd->sg = se_cmd->t_data_sg;
4898
4899 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
Bart Van Assche649ee052015-04-14 13:26:44 +02004900 "SRR cmd %p (se_cmd %p, tag %lld, op %x), sg_cnt=%d, offset=%d",
4901 cmd, &cmd->se_cmd, se_cmd->tag, se_cmd->t_task_cdb ?
4902 se_cmd->t_task_cdb[0] : 0, cmd->sg_cnt, cmd->offset);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004903
4904 qlt_handle_srr(vha, sctio, imm);
4905
4906 kfree(imm);
4907 kfree(sctio);
4908 goto restart;
4909 }
4910 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4911}
4912
4913/* ha->hardware_lock supposed to be held on entry */
4914static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
4915 struct imm_ntfy_from_isp *iocb)
4916{
4917 struct qla_tgt_srr_imm *imm;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004918 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004919 struct qla_tgt_srr_ctio *sctio;
4920
4921 tgt->imm_srr_id++;
4922
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004923 ql_log(ql_log_warn, vha, 0xf02d, "qla_target(%d): SRR received\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004924 vha->vp_idx);
4925
4926 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
4927 if (imm != NULL) {
4928 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
4929
4930 /* IRQ is already OFF */
4931 spin_lock(&tgt->srr_lock);
4932 imm->srr_id = tgt->imm_srr_id;
4933 list_add_tail(&imm->srr_list_entry,
4934 &tgt->srr_imm_list);
4935 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
4936 "IMM NTFY SRR %p added (id %d, ui %x)\n",
4937 imm, imm->srr_id, iocb->u.isp24.srr_ui);
4938 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
4939 int found = 0;
4940 list_for_each_entry(sctio, &tgt->srr_ctio_list,
4941 srr_list_entry) {
4942 if (sctio->srr_id == imm->srr_id) {
4943 found = 1;
4944 break;
4945 }
4946 }
4947 if (found) {
4948 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
4949 "Scheduling srr work\n");
4950 schedule_work(&tgt->srr_work);
4951 } else {
4952 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
4953 "qla_target(%d): imm_srr_id "
4954 "== ctio_srr_id (%d), but there is no "
4955 "corresponding SRR CTIO, deleting IMM "
4956 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
4957 imm);
4958 list_del(&imm->srr_list_entry);
4959
4960 kfree(imm);
4961
4962 spin_unlock(&tgt->srr_lock);
4963 goto out_reject;
4964 }
4965 }
4966 spin_unlock(&tgt->srr_lock);
4967 } else {
4968 struct qla_tgt_srr_ctio *ts;
4969
4970 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
4971 "qla_target(%d): Unable to allocate SRR IMM "
4972 "entry, SRR request will be rejected\n", vha->vp_idx);
4973
4974 /* IRQ is already OFF */
4975 spin_lock(&tgt->srr_lock);
4976 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
4977 srr_list_entry) {
4978 if (sctio->srr_id == tgt->imm_srr_id) {
4979 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
4980 "CTIO SRR %p deleted (id %d)\n",
4981 sctio, sctio->srr_id);
4982 list_del(&sctio->srr_list_entry);
4983 qlt_send_term_exchange(vha, sctio->cmd,
Quinn Trana07100e2015-12-07 19:48:57 -05004984 &sctio->cmd->atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004985 kfree(sctio);
4986 }
4987 }
4988 spin_unlock(&tgt->srr_lock);
4989 goto out_reject;
4990 }
4991
4992 return;
4993
4994out_reject:
4995 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
4996 NOTIFY_ACK_SRR_FLAGS_REJECT,
4997 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4998 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4999}
5000
5001/*
5002 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5003 */
5004static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5005 struct imm_ntfy_from_isp *iocb)
5006{
5007 struct qla_hw_data *ha = vha->hw;
5008 uint32_t add_flags = 0;
5009 int send_notify_ack = 1;
5010 uint16_t status;
5011
5012 status = le16_to_cpu(iocb->u.isp2x.status);
5013 switch (status) {
5014 case IMM_NTFY_LIP_RESET:
5015 {
5016 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5017 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5018 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5019 iocb->u.isp24.status_subcode);
5020
5021 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5022 send_notify_ack = 0;
5023 break;
5024 }
5025
5026 case IMM_NTFY_LIP_LINK_REINIT:
5027 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005028 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005029 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5030 "qla_target(%d): LINK REINIT (loop %#x, "
5031 "subcode %x)\n", vha->vp_idx,
5032 le16_to_cpu(iocb->u.isp24.nport_handle),
5033 iocb->u.isp24.status_subcode);
5034 if (tgt->link_reinit_iocb_pending) {
5035 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
5036 0, 0, 0, 0, 0, 0);
5037 }
5038 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5039 tgt->link_reinit_iocb_pending = 1;
5040 /*
5041 * QLogic requires to wait after LINK REINIT for possible
5042 * PDISC or ADISC ELS commands
5043 */
5044 send_notify_ack = 0;
5045 break;
5046 }
5047
5048 case IMM_NTFY_PORT_LOGOUT:
5049 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5050 "qla_target(%d): Port logout (loop "
5051 "%#x, subcode %x)\n", vha->vp_idx,
5052 le16_to_cpu(iocb->u.isp24.nport_handle),
5053 iocb->u.isp24.status_subcode);
5054
5055 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5056 send_notify_ack = 0;
5057 /* The sessions will be cleared in the callback, if needed */
5058 break;
5059
5060 case IMM_NTFY_GLBL_TPRLO:
5061 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5062 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5063 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5064 send_notify_ack = 0;
5065 /* The sessions will be cleared in the callback, if needed */
5066 break;
5067
5068 case IMM_NTFY_PORT_CONFIG:
5069 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5070 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5071 status);
5072 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5073 send_notify_ack = 0;
5074 /* The sessions will be cleared in the callback, if needed */
5075 break;
5076
5077 case IMM_NTFY_GLBL_LOGO:
5078 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5079 "qla_target(%d): Link failure detected\n",
5080 vha->vp_idx);
5081 /* I_T nexus loss */
5082 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5083 send_notify_ack = 0;
5084 break;
5085
5086 case IMM_NTFY_IOCB_OVERFLOW:
5087 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5088 "qla_target(%d): Cannot provide requested "
5089 "capability (IOCB overflowed the immediate notify "
5090 "resource count)\n", vha->vp_idx);
5091 break;
5092
5093 case IMM_NTFY_ABORT_TASK:
5094 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5095 "qla_target(%d): Abort Task (S %08x I %#x -> "
5096 "L %#x)\n", vha->vp_idx,
5097 le16_to_cpu(iocb->u.isp2x.seq_id),
5098 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5099 le16_to_cpu(iocb->u.isp2x.lun));
5100 if (qlt_abort_task(vha, iocb) == 0)
5101 send_notify_ack = 0;
5102 break;
5103
5104 case IMM_NTFY_RESOURCE:
5105 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5106 "qla_target(%d): Out of resources, host %ld\n",
5107 vha->vp_idx, vha->host_no);
5108 break;
5109
5110 case IMM_NTFY_MSG_RX:
5111 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5112 "qla_target(%d): Immediate notify task %x\n",
5113 vha->vp_idx, iocb->u.isp2x.task_flags);
5114 if (qlt_handle_task_mgmt(vha, iocb) == 0)
5115 send_notify_ack = 0;
5116 break;
5117
5118 case IMM_NTFY_ELS:
5119 if (qlt_24xx_handle_els(vha, iocb) == 0)
5120 send_notify_ack = 0;
5121 break;
5122
5123 case IMM_NTFY_SRR:
5124 qlt_prepare_srr_imm(vha, iocb);
5125 send_notify_ack = 0;
5126 break;
5127
5128 default:
5129 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5130 "qla_target(%d): Received unknown immediate "
5131 "notify status %x\n", vha->vp_idx, status);
5132 break;
5133 }
5134
5135 if (send_notify_ack)
5136 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
5137}
5138
5139/*
5140 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5141 * This function sends busy to ISP 2xxx or 24xx.
5142 */
Quinn Tran33e79972014-09-25 06:14:55 -04005143static int __qlt_send_busy(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005144 struct atio_from_isp *atio, uint16_t status)
5145{
5146 struct ctio7_to_24xx *ctio24;
5147 struct qla_hw_data *ha = vha->hw;
5148 request_t *pkt;
5149 struct qla_tgt_sess *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005150 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005151
Quinn Tran75601512015-12-17 14:57:04 -05005152 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005153 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5154 atio->u.isp24.fcp_hdr.s_id);
Quinn Tran75601512015-12-17 14:57:04 -05005155 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005156 if (!sess) {
Quinn Trana07100e2015-12-07 19:48:57 -05005157 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Quinn Tran33e79972014-09-25 06:14:55 -04005158 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005159 }
5160 /* Sending marker isn't necessary, since we called from ISR */
5161
5162 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
5163 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04005164 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005165 "qla_target(%d): %s failed: unable to allocate "
5166 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04005167 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005168 }
5169
Himanshu Madhanice1025c2015-12-17 14:56:58 -05005170 vha->tgt_counters.num_q_full_sent++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005171 pkt->entry_count = 1;
5172 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5173
5174 ctio24 = (struct ctio7_to_24xx *)pkt;
5175 ctio24->entry_type = CTIO_TYPE7;
5176 ctio24->nport_handle = sess->loop_id;
Bart Van Asschead950362015-07-09 07:24:08 -07005177 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005178 ctio24->vp_index = vha->vp_idx;
5179 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5180 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5181 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5182 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5183 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
Bart Van Asschead950362015-07-09 07:24:08 -07005184 cpu_to_le16(
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005185 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5186 CTIO7_FLAGS_DONT_RET_CTIO);
5187 /*
5188 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5189 * if the explicit conformation is used.
5190 */
5191 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5192 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Himanshu Madhani63163e02014-09-25 06:14:59 -04005193 /* Memory Barrier */
5194 wmb();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005195 qla2x00_start_iocbs(vha, vha->req);
Quinn Tran33e79972014-09-25 06:14:55 -04005196 return 0;
5197}
5198
5199/*
5200 * This routine is used to allocate a command for either a QFull condition
5201 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5202 * out previously.
5203 */
5204static void
5205qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5206 struct atio_from_isp *atio, uint16_t status, int qfull)
5207{
5208 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5209 struct qla_hw_data *ha = vha->hw;
5210 struct qla_tgt_sess *sess;
5211 struct se_session *se_sess;
5212 struct qla_tgt_cmd *cmd;
5213 int tag;
5214
5215 if (unlikely(tgt->tgt_stop)) {
5216 ql_dbg(ql_dbg_io, vha, 0x300a,
5217 "New command while device %p is shutting down\n", tgt);
5218 return;
5219 }
5220
5221 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5222 vha->hw->tgt.num_qfull_cmds_dropped++;
5223 if (vha->hw->tgt.num_qfull_cmds_dropped >
5224 vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
5225 vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
5226 vha->hw->tgt.num_qfull_cmds_dropped;
5227
5228 ql_dbg(ql_dbg_io, vha, 0x3068,
5229 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5230 vha->vp_idx, __func__,
5231 vha->hw->tgt.num_qfull_cmds_dropped);
5232
5233 qlt_chk_exch_leak_thresh_hold(vha);
5234 return;
5235 }
5236
5237 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5238 (vha, atio->u.isp24.fcp_hdr.s_id);
5239 if (!sess)
5240 return;
5241
5242 se_sess = sess->se_sess;
5243
5244 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
5245 if (tag < 0)
5246 return;
5247
5248 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5249 if (!cmd) {
5250 ql_dbg(ql_dbg_io, vha, 0x3009,
5251 "qla_target(%d): %s: Allocation of cmd failed\n",
5252 vha->vp_idx, __func__);
5253
5254 vha->hw->tgt.num_qfull_cmds_dropped++;
5255 if (vha->hw->tgt.num_qfull_cmds_dropped >
5256 vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
5257 vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
5258 vha->hw->tgt.num_qfull_cmds_dropped;
5259
5260 qlt_chk_exch_leak_thresh_hold(vha);
5261 return;
5262 }
5263
5264 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5265
5266 qlt_incr_num_pend_cmds(vha);
5267 INIT_LIST_HEAD(&cmd->cmd_list);
5268 memcpy(&cmd->atio, atio, sizeof(*atio));
5269
5270 cmd->tgt = vha->vha_tgt.qla_tgt;
5271 cmd->vha = vha;
5272 cmd->reset_count = vha->hw->chip_reset;
5273 cmd->q_full = 1;
5274
5275 if (qfull) {
5276 cmd->q_full = 1;
5277 /* NOTE: borrowing the state field to carry the status */
5278 cmd->state = status;
5279 } else
5280 cmd->term_exchg = 1;
5281
5282 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5283
5284 vha->hw->tgt.num_qfull_cmds_alloc++;
5285 if (vha->hw->tgt.num_qfull_cmds_alloc >
5286 vha->hw->qla_stats.stat_max_qfull_cmds_alloc)
5287 vha->hw->qla_stats.stat_max_qfull_cmds_alloc =
5288 vha->hw->tgt.num_qfull_cmds_alloc;
5289}
5290
5291int
5292qlt_free_qfull_cmds(struct scsi_qla_host *vha)
5293{
5294 struct qla_hw_data *ha = vha->hw;
5295 unsigned long flags;
5296 struct qla_tgt_cmd *cmd, *tcmd;
5297 struct list_head free_list;
5298 int rc = 0;
5299
5300 if (list_empty(&ha->tgt.q_full_list))
5301 return 0;
5302
5303 INIT_LIST_HEAD(&free_list);
5304
5305 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
5306
5307 if (list_empty(&ha->tgt.q_full_list)) {
5308 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5309 return 0;
5310 }
5311
5312 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
5313 if (cmd->q_full)
5314 /* cmd->state is a borrowed field to hold status */
5315 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
5316 else if (cmd->term_exchg)
5317 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
5318
5319 if (rc == -ENOMEM)
5320 break;
5321
5322 if (cmd->q_full)
5323 ql_dbg(ql_dbg_io, vha, 0x3006,
5324 "%s: busy sent for ox_id[%04x]\n", __func__,
5325 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5326 else if (cmd->term_exchg)
5327 ql_dbg(ql_dbg_io, vha, 0x3007,
5328 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5329 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5330 else
5331 ql_dbg(ql_dbg_io, vha, 0x3008,
5332 "%s: Unexpected cmd in QFull list %p\n", __func__,
5333 cmd);
5334
5335 list_del(&cmd->cmd_list);
5336 list_add_tail(&cmd->cmd_list, &free_list);
5337
5338 /* piggy back on hardware_lock for protection */
5339 vha->hw->tgt.num_qfull_cmds_alloc--;
5340 }
5341 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
5342
5343 cmd = NULL;
5344
5345 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5346 list_del(&cmd->cmd_list);
5347 /* This cmd was never sent to TCM. There is no need
5348 * to schedule free or call free_cmd
5349 */
5350 qlt_free_cmd(cmd);
5351 }
5352 return rc;
5353}
5354
5355static void
5356qlt_send_busy(struct scsi_qla_host *vha,
5357 struct atio_from_isp *atio, uint16_t status)
5358{
5359 int rc = 0;
5360
5361 rc = __qlt_send_busy(vha, atio, status);
5362 if (rc == -ENOMEM)
5363 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5364}
5365
5366static int
5367qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
5368 struct atio_from_isp *atio)
5369{
5370 struct qla_hw_data *ha = vha->hw;
5371 uint16_t status;
5372
5373 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5374 return 0;
5375
5376 status = temp_sam_status;
5377 qlt_send_busy(vha, atio, status);
5378 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005379}
5380
5381/* ha->hardware_lock supposed to be held on entry */
5382/* called via callback from qla2xxx */
5383static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
Quinn Tran2f424b92015-12-17 14:57:07 -05005384 struct atio_from_isp *atio, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005385{
5386 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005387 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005388 int rc;
Quinn Tran2f424b92015-12-17 14:57:07 -05005389 unsigned long flags;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005390
5391 if (unlikely(tgt == NULL)) {
Arun Easi667024a2014-09-25 06:14:47 -04005392 ql_dbg(ql_dbg_io, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005393 "ATIO pkt, but no tgt (ha %p)", ha);
5394 return;
5395 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005396 /*
5397 * In tgt_stop mode we also should allow all requests to pass.
5398 * Otherwise, some commands can stuck.
5399 */
5400
Quinn Tran2f424b92015-12-17 14:57:07 -05005401 tgt->atio_irq_cmd_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005402
5403 switch (atio->u.raw.entry_type) {
5404 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005405 if (unlikely(atio->u.isp24.exchange_addr ==
5406 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04005407 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005408 "qla_target(%d): ATIO_TYPE7 "
5409 "received with UNKNOWN exchange address, "
5410 "sending QUEUE_FULL\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005411 if (!ha_locked)
5412 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005413 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
Quinn Tran2f424b92015-12-17 14:57:07 -05005414 if (!ha_locked)
5415 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005416 break;
5417 }
Quinn Tran33e79972014-09-25 06:14:55 -04005418
5419
5420
5421 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5422 rc = qlt_chk_qfull_thresh_hold(vha, atio);
5423 if (rc != 0) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005424 tgt->atio_irq_cmd_count--;
Quinn Tran33e79972014-09-25 06:14:55 -04005425 return;
5426 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005427 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005428 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005429 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04005430 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005431 if (unlikely(rc != 0)) {
5432 if (rc == -ESRCH) {
Quinn Tran2f424b92015-12-17 14:57:07 -05005433 if (!ha_locked)
5434 spin_lock_irqsave
5435 (&ha->hardware_lock, flags);
5436
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005437#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5438 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
5439#else
Quinn Trana07100e2015-12-07 19:48:57 -05005440 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005441#endif
Quinn Tran2f424b92015-12-17 14:57:07 -05005442
5443 if (!ha_locked)
5444 spin_unlock_irqrestore
5445 (&ha->hardware_lock, flags);
5446
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005447 } else {
5448 if (tgt->tgt_stop) {
5449 ql_dbg(ql_dbg_tgt, vha, 0xe059,
5450 "qla_target: Unable to send "
5451 "command to target for req, "
5452 "ignoring.\n");
5453 } else {
5454 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
5455 "qla_target(%d): Unable to send "
5456 "command to target, sending BUSY "
5457 "status.\n", vha->vp_idx);
Quinn Tran2f424b92015-12-17 14:57:07 -05005458 if (!ha_locked)
5459 spin_lock_irqsave(
5460 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005461 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
Quinn Tran2f424b92015-12-17 14:57:07 -05005462 if (!ha_locked)
5463 spin_unlock_irqrestore(
5464 &ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005465 }
5466 }
5467 }
5468 break;
5469
5470 case IMMED_NOTIFY_TYPE:
5471 {
5472 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5473 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5474 "qla_target(%d): Received ATIO packet %x "
5475 "with error status %x\n", vha->vp_idx,
5476 atio->u.raw.entry_type,
5477 atio->u.isp2x.entry_status);
5478 break;
5479 }
5480 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
Quinn Tran2f424b92015-12-17 14:57:07 -05005481
5482 if (!ha_locked)
5483 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005484 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
Quinn Tran2f424b92015-12-17 14:57:07 -05005485 if (!ha_locked)
5486 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005487 break;
5488 }
5489
5490 default:
5491 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5492 "qla_target(%d): Received unknown ATIO atio "
5493 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5494 break;
5495 }
5496
Quinn Tran2f424b92015-12-17 14:57:07 -05005497 tgt->atio_irq_cmd_count--;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005498}
5499
5500/* ha->hardware_lock supposed to be held on entry */
5501/* called via callback from qla2xxx */
5502static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
5503{
5504 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005505 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005506
5507 if (unlikely(tgt == NULL)) {
5508 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5509 "qla_target(%d): Response pkt %x received, but no "
5510 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
5511 return;
5512 }
5513
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005514 /*
5515 * In tgt_stop mode we also should allow all requests to pass.
5516 * Otherwise, some commands can stuck.
5517 */
5518
5519 tgt->irq_cmd_count++;
5520
5521 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04005522 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005523 case CTIO_TYPE7:
5524 {
5525 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005526 qlt_do_ctio_completion(vha, entry->handle,
5527 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5528 entry);
5529 break;
5530 }
5531
5532 case ACCEPT_TGT_IO_TYPE:
5533 {
5534 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5535 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005536 if (atio->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005537 cpu_to_le16(ATIO_CDB_VALID)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005538 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5539 "qla_target(%d): ATIO with error "
5540 "status %x received\n", vha->vp_idx,
5541 le16_to_cpu(atio->u.isp2x.status));
5542 break;
5543 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005544
Quinn Tran33e79972014-09-25 06:14:55 -04005545 rc = qlt_chk_qfull_thresh_hold(vha, atio);
5546 if (rc != 0) {
5547 tgt->irq_cmd_count--;
5548 return;
5549 }
5550
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005551 rc = qlt_handle_cmd_for_atio(vha, atio);
5552 if (unlikely(rc != 0)) {
5553 if (rc == -ESRCH) {
5554#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
5555 qlt_send_busy(vha, atio, 0);
5556#else
Quinn Trana07100e2015-12-07 19:48:57 -05005557 qlt_send_term_exchange(vha, NULL, atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005558#endif
5559 } else {
5560 if (tgt->tgt_stop) {
5561 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5562 "qla_target: Unable to send "
5563 "command to target, sending TERM "
5564 "EXCHANGE for rsp\n");
5565 qlt_send_term_exchange(vha, NULL,
Quinn Trana07100e2015-12-07 19:48:57 -05005566 atio, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005567 } else {
5568 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5569 "qla_target(%d): Unable to send "
5570 "command to target, sending BUSY "
5571 "status\n", vha->vp_idx);
5572 qlt_send_busy(vha, atio, 0);
5573 }
5574 }
5575 }
5576 }
5577 break;
5578
5579 case CONTINUE_TGT_IO_TYPE:
5580 {
5581 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005582 qlt_do_ctio_completion(vha, entry->handle,
5583 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5584 entry);
5585 break;
5586 }
5587
5588 case CTIO_A64_TYPE:
5589 {
5590 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005591 qlt_do_ctio_completion(vha, entry->handle,
5592 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5593 entry);
5594 break;
5595 }
5596
5597 case IMMED_NOTIFY_TYPE:
5598 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5599 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5600 break;
5601
5602 case NOTIFY_ACK_TYPE:
5603 if (tgt->notify_ack_expected > 0) {
5604 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5605 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5606 "NOTIFY_ACK seq %08x status %x\n",
5607 le16_to_cpu(entry->u.isp2x.seq_id),
5608 le16_to_cpu(entry->u.isp2x.status));
5609 tgt->notify_ack_expected--;
5610 if (entry->u.isp2x.status !=
Bart Van Asschead950362015-07-09 07:24:08 -07005611 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005612 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5613 "qla_target(%d): NOTIFY_ACK "
5614 "failed %x\n", vha->vp_idx,
5615 le16_to_cpu(entry->u.isp2x.status));
5616 }
5617 } else {
5618 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5619 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5620 vha->vp_idx);
5621 }
5622 break;
5623
5624 case ABTS_RECV_24XX:
5625 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5626 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5627 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5628 break;
5629
5630 case ABTS_RESP_24XX:
5631 if (tgt->abts_resp_expected > 0) {
5632 struct abts_resp_from_24xx_fw *entry =
5633 (struct abts_resp_from_24xx_fw *)pkt;
5634 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5635 "ABTS_RESP_24XX: compl_status %x\n",
5636 entry->compl_status);
5637 tgt->abts_resp_expected--;
5638 if (le16_to_cpu(entry->compl_status) !=
5639 ABTS_RESP_COMPL_SUCCESS) {
5640 if ((entry->error_subcode1 == 0x1E) &&
5641 (entry->error_subcode2 == 0)) {
5642 /*
5643 * We've got a race here: aborted
5644 * exchange not terminated, i.e.
5645 * response for the aborted command was
5646 * sent between the abort request was
5647 * received and processed.
5648 * Unfortunately, the firmware has a
5649 * silly requirement that all aborted
5650 * exchanges must be explicitely
5651 * terminated, otherwise it refuses to
5652 * send responses for the abort
5653 * requests. So, we have to
5654 * (re)terminate the exchange and retry
5655 * the abort response.
5656 */
5657 qlt_24xx_retry_term_exchange(vha,
5658 entry);
5659 } else
5660 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5661 "qla_target(%d): ABTS_RESP_24XX "
5662 "failed %x (subcode %x:%x)",
5663 vha->vp_idx, entry->compl_status,
5664 entry->error_subcode1,
5665 entry->error_subcode2);
5666 }
5667 } else {
5668 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5669 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5670 "received\n", vha->vp_idx);
5671 }
5672 break;
5673
5674 default:
5675 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5676 "qla_target(%d): Received unknown response pkt "
5677 "type %x\n", vha->vp_idx, pkt->entry_type);
5678 break;
5679 }
5680
5681 tgt->irq_cmd_count--;
5682}
5683
5684/*
5685 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5686 */
5687void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5688 uint16_t *mailbox)
5689{
5690 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005691 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01005692 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005693
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005694 if (!ha->tgt.tgt_ops)
5695 return;
5696
5697 if (unlikely(tgt == NULL)) {
5698 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
5699 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
5700 return;
5701 }
5702
5703 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5704 IS_QLA2100(ha))
5705 return;
5706 /*
5707 * In tgt_stop mode we also should allow all requests to pass.
5708 * Otherwise, some commands can stuck.
5709 */
5710
5711 tgt->irq_cmd_count++;
5712
5713 switch (code) {
5714 case MBA_RESET: /* Reset */
5715 case MBA_SYSTEM_ERR: /* System Error */
5716 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
5717 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
5718 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5719 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005720 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005721 break;
5722 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
5723 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5724 break;
5725
5726 case MBA_LOOP_UP:
5727 {
5728 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005729 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005730 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5731 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5732 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005733 if (tgt->link_reinit_iocb_pending) {
5734 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
5735 0, 0, 0, 0, 0, 0);
5736 tgt->link_reinit_iocb_pending = 0;
5737 }
5738 break;
5739 }
5740
5741 case MBA_LIP_OCCURRED:
5742 case MBA_LOOP_DOWN:
5743 case MBA_LIP_RESET:
5744 case MBA_RSCN_UPDATE:
5745 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005746 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005747 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5748 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5749 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005750 break;
5751
5752 case MBA_PORT_UPDATE:
5753 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5754 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09005755 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01005756 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5757 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
5758 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5759
5760 login_code = le16_to_cpu(mailbox[2]);
5761 if (login_code == 0x4)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005762 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5763 "Async MB 2: Got PLOGI Complete\n");
Alan Cox4f1d0f12012-07-04 16:35:35 +01005764 else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005765 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
5766 "Async MB 2: Port Logged Out\n");
5767 break;
5768
5769 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005770 break;
5771 }
5772
5773 tgt->irq_cmd_count--;
5774}
5775
5776static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
5777 uint16_t loop_id)
5778{
5779 fc_port_t *fcport;
5780 int rc;
5781
5782 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
5783 if (!fcport) {
5784 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
5785 "qla_target(%d): Allocation of tmp FC port failed",
5786 vha->vp_idx);
5787 return NULL;
5788 }
5789
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005790 fcport->loop_id = loop_id;
5791
5792 rc = qla2x00_get_port_database(vha, fcport, 0);
5793 if (rc != QLA_SUCCESS) {
5794 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
5795 "qla_target(%d): Failed to retrieve fcport "
5796 "information -- get_port_database() returned %x "
5797 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
5798 kfree(fcport);
5799 return NULL;
5800 }
5801
5802 return fcport;
5803}
5804
5805/* Must be called under tgt_mutex */
5806static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
5807 uint8_t *s_id)
5808{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005809 struct qla_tgt_sess *sess = NULL;
5810 fc_port_t *fcport = NULL;
5811 int rc, global_resets;
5812 uint16_t loop_id = 0;
5813
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005814 mutex_lock(&vha->vha_tgt.tgt_mutex);
5815
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005816retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005817 global_resets =
5818 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005819
5820 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
5821 if (rc != 0) {
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005822 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5823
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005824 if ((s_id[0] == 0xFF) &&
5825 (s_id[1] == 0xFC)) {
5826 /*
5827 * This is Domain Controller, so it should be
5828 * OK to drop SCSI commands from it.
5829 */
5830 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
5831 "Unable to find initiator with S_ID %x:%x:%x",
5832 s_id[0], s_id[1], s_id[2]);
5833 } else
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005834 ql_log(ql_log_info, vha, 0xf071,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005835 "qla_target(%d): Unable to find "
5836 "initiator with S_ID %x:%x:%x",
5837 vha->vp_idx, s_id[0], s_id[1],
5838 s_id[2]);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005839
5840 if (rc == -ENOENT) {
5841 qlt_port_logo_t logo;
5842 sid_to_portid(s_id, &logo.id);
5843 logo.cmd_count = 1;
5844 qlt_send_first_logo(vha, &logo);
5845 }
5846
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005847 return NULL;
5848 }
5849
5850 fcport = qlt_get_port_database(vha, loop_id);
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005851 if (!fcport) {
5852 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005853 return NULL;
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005854 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005855
5856 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005857 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005858 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
5859 "qla_target(%d): global reset during session discovery "
5860 "(counter was %d, new %d), retrying", vha->vp_idx,
5861 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005862 atomic_read(&vha->vha_tgt.
5863 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005864 goto retry;
5865 }
5866
5867 sess = qlt_create_sess(vha, fcport, true);
5868
Alexei Potashnik71cdc072015-12-17 14:57:01 -05005869 mutex_unlock(&vha->vha_tgt.tgt_mutex);
5870
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005871 kfree(fcport);
5872 return sess;
5873}
5874
5875static void qlt_abort_work(struct qla_tgt *tgt,
5876 struct qla_tgt_sess_work_param *prm)
5877{
5878 struct scsi_qla_host *vha = tgt->vha;
5879 struct qla_hw_data *ha = vha->hw;
5880 struct qla_tgt_sess *sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005881 unsigned long flags = 0, flags2 = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005882 uint32_t be_s_id;
5883 uint8_t s_id[3];
5884 int rc;
5885
Quinn Tran75601512015-12-17 14:57:04 -05005886 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005887
5888 if (tgt->tgt_stop)
Quinn Tran75601512015-12-17 14:57:04 -05005889 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005890
5891 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
5892 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
5893 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5894
5895 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5896 (unsigned char *)&be_s_id);
5897 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005898 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005899
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005900 sess = qlt_make_local_sess(vha, s_id);
5901 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005902
Quinn Tran75601512015-12-17 14:57:04 -05005903 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005904 if (!sess)
Quinn Tran75601512015-12-17 14:57:04 -05005905 goto out_term2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005906 } else {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005907 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
5908 sess = NULL;
Quinn Tran75601512015-12-17 14:57:04 -05005909 goto out_term2;
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005910 }
5911
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005912 kref_get(&sess->se_sess->sess_kref);
5913 }
5914
Quinn Tran75601512015-12-17 14:57:04 -05005915 spin_lock_irqsave(&ha->hardware_lock, flags);
5916
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005917 if (tgt->tgt_stop)
5918 goto out_term;
5919
5920 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5921 if (rc != 0)
5922 goto out_term;
Quinn Tran75601512015-12-17 14:57:04 -05005923 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005924
5925 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005926 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005927 return;
5928
Quinn Tran75601512015-12-17 14:57:04 -05005929out_term2:
5930 spin_lock_irqsave(&ha->hardware_lock, flags);
5931
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005932out_term:
5933 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
Quinn Tran75601512015-12-17 14:57:04 -05005934 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5935
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005936 if (sess)
5937 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005938 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005939}
5940
5941static void qlt_tmr_work(struct qla_tgt *tgt,
5942 struct qla_tgt_sess_work_param *prm)
5943{
5944 struct atio_from_isp *a = &prm->tm_iocb2;
5945 struct scsi_qla_host *vha = tgt->vha;
5946 struct qla_hw_data *ha = vha->hw;
5947 struct qla_tgt_sess *sess = NULL;
5948 unsigned long flags;
5949 uint8_t *s_id = NULL; /* to hide compiler warnings */
5950 int rc;
5951 uint32_t lun, unpacked_lun;
Bart Van Assche52c82822015-07-09 07:23:26 -07005952 int fn;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005953 void *iocb;
5954
Quinn Tran75601512015-12-17 14:57:04 -05005955 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005956
5957 if (tgt->tgt_stop)
5958 goto out_term;
5959
5960 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5961 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5962 if (!sess) {
Quinn Tran75601512015-12-17 14:57:04 -05005963 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005964
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005965 sess = qlt_make_local_sess(vha, s_id);
5966 /* sess has got an extra creation ref */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005967
Quinn Tran75601512015-12-17 14:57:04 -05005968 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005969 if (!sess)
5970 goto out_term;
5971 } else {
Alexei Potashnike52a8b42015-07-14 16:00:48 -04005972 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
5973 sess = NULL;
5974 goto out_term;
5975 }
5976
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005977 kref_get(&sess->se_sess->sess_kref);
5978 }
5979
5980 iocb = a;
5981 lun = a->u.isp24.fcp_cmnd.lun;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005982 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5983 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5984
5985 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5986 if (rc != 0)
5987 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005988
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 return;
5992
5993out_term:
Quinn Trana07100e2015-12-07 19:48:57 -05005994 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005995 if (sess)
5996 ha->tgt.tgt_ops->put_sess(sess);
Quinn Tran75601512015-12-17 14:57:04 -05005997 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005998}
5999
6000static void qlt_sess_work_fn(struct work_struct *work)
6001{
6002 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6003 struct scsi_qla_host *vha = tgt->vha;
6004 unsigned long flags;
6005
6006 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6007
6008 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6009 while (!list_empty(&tgt->sess_works_list)) {
6010 struct qla_tgt_sess_work_param *prm = list_entry(
6011 tgt->sess_works_list.next, typeof(*prm),
6012 sess_works_list_entry);
6013
6014 /*
6015 * This work can be scheduled on several CPUs at time, so we
6016 * must delete the entry to eliminate double processing
6017 */
6018 list_del(&prm->sess_works_list_entry);
6019
6020 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6021
6022 switch (prm->type) {
6023 case QLA_TGT_SESS_WORK_ABORT:
6024 qlt_abort_work(tgt, prm);
6025 break;
6026 case QLA_TGT_SESS_WORK_TM:
6027 qlt_tmr_work(tgt, prm);
6028 break;
6029 default:
6030 BUG_ON(1);
6031 break;
6032 }
6033
6034 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6035
6036 kfree(prm);
6037 }
6038 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6039}
6040
6041/* Must be called under tgt_host_action_mutex */
6042int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6043{
6044 struct qla_tgt *tgt;
6045
6046 if (!QLA_TGT_MODE_ENABLED())
6047 return 0;
6048
Arun Easi33c36c02013-01-30 03:34:41 -05006049 if (!IS_TGT_MODE_CAPABLE(ha)) {
6050 ql_log(ql_log_warn, base_vha, 0xe070,
6051 "This adapter does not support target mode.\n");
6052 return 0;
6053 }
6054
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006055 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006056 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006057
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006058 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006059
6060 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6061 if (!tgt) {
6062 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6063 "Unable to allocate struct qla_tgt\n");
6064 return -ENOMEM;
6065 }
6066
6067 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6068 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6069
6070 tgt->ha = ha;
6071 tgt->vha = base_vha;
6072 init_waitqueue_head(&tgt->waitQ);
6073 INIT_LIST_HEAD(&tgt->sess_list);
6074 INIT_LIST_HEAD(&tgt->del_sess_list);
6075 INIT_DELAYED_WORK(&tgt->sess_del_work,
6076 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
6077 spin_lock_init(&tgt->sess_work_lock);
6078 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6079 INIT_LIST_HEAD(&tgt->sess_works_list);
6080 spin_lock_init(&tgt->srr_lock);
6081 INIT_LIST_HEAD(&tgt->srr_ctio_list);
6082 INIT_LIST_HEAD(&tgt->srr_imm_list);
6083 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
6084 atomic_set(&tgt->tgt_global_resets_count, 0);
6085
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006086 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006087
6088 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6089 "qla_target(%d): using 64 Bit PCI addressing",
6090 base_vha->vp_idx);
6091 tgt->tgt_enable_64bit_addr = 1;
6092 /* 3 is reserved */
6093 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6094 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
6095 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
6096
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006097 if (base_vha->fc_vport)
6098 return 0;
6099
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006100 mutex_lock(&qla_tgt_mutex);
6101 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6102 mutex_unlock(&qla_tgt_mutex);
6103
6104 return 0;
6105}
6106
6107/* Must be called under tgt_host_action_mutex */
6108int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6109{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006110 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006111 return 0;
6112
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006113 if (vha->fc_vport) {
6114 qlt_release(vha->vha_tgt.qla_tgt);
6115 return 0;
6116 }
Quinn Tran33e79972014-09-25 06:14:55 -04006117
6118 /* free left over qfull cmds */
6119 qlt_init_term_exchange(vha);
6120
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006121 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006122 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006123 mutex_unlock(&qla_tgt_mutex);
6124
6125 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6126 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006127 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006128
6129 return 0;
6130}
6131
6132static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6133 unsigned char *b)
6134{
6135 int i;
6136
6137 pr_debug("qla2xxx HW vha->node_name: ");
6138 for (i = 0; i < WWN_SIZE; i++)
6139 pr_debug("%02x ", vha->node_name[i]);
6140 pr_debug("\n");
6141 pr_debug("qla2xxx HW vha->port_name: ");
6142 for (i = 0; i < WWN_SIZE; i++)
6143 pr_debug("%02x ", vha->port_name[i]);
6144 pr_debug("\n");
6145
6146 pr_debug("qla2xxx passed configfs WWPN: ");
6147 put_unaligned_be64(wwpn, b);
6148 for (i = 0; i < WWN_SIZE; i++)
6149 pr_debug("%02x ", b[i]);
6150 pr_debug("\n");
6151}
6152
6153/**
6154 * qla_tgt_lport_register - register lport with external module
6155 *
6156 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
6157 * @wwpn: Passwd FC target WWPN
6158 * @callback: lport initialization callback for tcm_qla2xxx code
6159 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6160 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006161int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6162 u64 npiv_wwpn, u64 npiv_wwnn,
6163 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006164{
6165 struct qla_tgt *tgt;
6166 struct scsi_qla_host *vha;
6167 struct qla_hw_data *ha;
6168 struct Scsi_Host *host;
6169 unsigned long flags;
6170 int rc;
6171 u8 b[WWN_SIZE];
6172
6173 mutex_lock(&qla_tgt_mutex);
6174 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6175 vha = tgt->vha;
6176 ha = vha->hw;
6177
6178 host = vha->host;
6179 if (!host)
6180 continue;
6181
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006182 if (!(host->hostt->supported_mode & MODE_TARGET))
6183 continue;
6184
6185 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006186 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006187 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6188 host->host_no);
6189 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6190 continue;
6191 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006192 if (tgt->tgt_stop) {
6193 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6194 host->host_no);
6195 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6196 continue;
6197 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006198 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6199
6200 if (!scsi_host_get(host)) {
6201 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6202 "Unable to scsi_host_get() for"
6203 " qla2xxx scsi_host\n");
6204 continue;
6205 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006206 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006207
6208 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6209 scsi_host_put(host);
6210 continue;
6211 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08006212 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6213 if (rc != 0)
6214 scsi_host_put(host);
6215
Nicholas Bellingerddb95142014-02-19 17:51:25 -08006216 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006217 return rc;
6218 }
6219 mutex_unlock(&qla_tgt_mutex);
6220
6221 return -ENODEV;
6222}
6223EXPORT_SYMBOL(qlt_lport_register);
6224
6225/**
6226 * qla_tgt_lport_deregister - Degister lport
6227 *
6228 * @vha: Registered scsi_qla_host pointer
6229 */
6230void qlt_lport_deregister(struct scsi_qla_host *vha)
6231{
6232 struct qla_hw_data *ha = vha->hw;
6233 struct Scsi_Host *sh = vha->host;
6234 /*
6235 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6236 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006237 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006238 ha->tgt.tgt_ops = NULL;
6239 /*
6240 * Release the Scsi_Host reference for the underlying qla2xxx host
6241 */
6242 scsi_host_put(sh);
6243}
6244EXPORT_SYMBOL(qlt_lport_deregister);
6245
6246/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006247static void qlt_set_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006248{
6249 struct qla_hw_data *ha = vha->hw;
6250
6251 switch (ql2x_ini_mode) {
6252 case QLA2XXX_INI_MODE_DISABLED:
6253 case QLA2XXX_INI_MODE_EXCLUSIVE:
6254 vha->host->active_mode = MODE_TARGET;
6255 break;
6256 case QLA2XXX_INI_MODE_ENABLED:
6257 vha->host->active_mode |= MODE_TARGET;
6258 break;
6259 default:
6260 break;
6261 }
6262
6263 if (ha->tgt.ini_mode_force_reverse)
6264 qla_reverse_ini_mode(vha);
6265}
6266
6267/* Must be called under HW lock */
Joern Engel55a90662014-09-16 16:23:15 -04006268static void qlt_clear_mode(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006269{
6270 struct qla_hw_data *ha = vha->hw;
6271
6272 switch (ql2x_ini_mode) {
6273 case QLA2XXX_INI_MODE_DISABLED:
6274 vha->host->active_mode = MODE_UNKNOWN;
6275 break;
6276 case QLA2XXX_INI_MODE_EXCLUSIVE:
6277 vha->host->active_mode = MODE_INITIATOR;
6278 break;
6279 case QLA2XXX_INI_MODE_ENABLED:
6280 vha->host->active_mode &= ~MODE_TARGET;
6281 break;
6282 default:
6283 break;
6284 }
6285
6286 if (ha->tgt.ini_mode_force_reverse)
6287 qla_reverse_ini_mode(vha);
6288}
6289
6290/*
6291 * qla_tgt_enable_vha - NO LOCK HELD
6292 *
6293 * host_reset, bring up w/ Target Mode Enabled
6294 */
6295void
6296qlt_enable_vha(struct scsi_qla_host *vha)
6297{
6298 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006299 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006300 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006301 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Trancdb898c2015-12-17 14:57:05 -05006302 int rspq_ent = QLA83XX_RSPQ_MSIX_ENTRY_NUMBER;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006303
6304 if (!tgt) {
6305 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6306 "Unable to locate qla_tgt pointer from"
6307 " struct qla_hw_data\n");
6308 dump_stack();
6309 return;
6310 }
6311
6312 spin_lock_irqsave(&ha->hardware_lock, flags);
6313 tgt->tgt_stopped = 0;
6314 qlt_set_mode(vha);
6315 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6316
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006317 if (vha->vp_idx) {
6318 qla24xx_disable_vp(vha);
6319 qla24xx_enable_vp(vha);
6320 } else {
Quinn Trancdb898c2015-12-17 14:57:05 -05006321 if (ha->msix_entries) {
6322 ql_dbg(ql_dbg_tgt, vha, 0xffff,
6323 "%s: host%ld : vector %d cpu %d\n",
6324 __func__, vha->host_no,
6325 ha->msix_entries[rspq_ent].vector,
6326 ha->msix_entries[rspq_ent].cpuid);
6327
6328 ha->tgt.rspq_vector_cpuid =
6329 ha->msix_entries[rspq_ent].cpuid;
6330 }
6331
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006332 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6333 qla2xxx_wake_dpc(base_vha);
6334 qla2x00_wait_for_hba_online(base_vha);
6335 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006336}
6337EXPORT_SYMBOL(qlt_enable_vha);
6338
6339/*
6340 * qla_tgt_disable_vha - NO LOCK HELD
6341 *
6342 * Disable Target Mode and reset the adapter
6343 */
Joern Engel55a90662014-09-16 16:23:15 -04006344static void qlt_disable_vha(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006345{
6346 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006347 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006348 unsigned long flags;
6349
6350 if (!tgt) {
6351 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6352 "Unable to locate qla_tgt pointer from"
6353 " struct qla_hw_data\n");
6354 dump_stack();
6355 return;
6356 }
6357
6358 spin_lock_irqsave(&ha->hardware_lock, flags);
6359 qlt_clear_mode(vha);
6360 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6361
6362 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6363 qla2xxx_wake_dpc(vha);
6364 qla2x00_wait_for_hba_online(vha);
6365}
6366
6367/*
6368 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6369 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6370 * members.
6371 */
6372void
6373qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6374{
6375 if (!qla_tgt_mode_enabled(vha))
6376 return;
6377
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006378 vha->vha_tgt.qla_tgt = NULL;
6379
6380 mutex_init(&vha->vha_tgt.tgt_mutex);
6381 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006382
6383 qlt_clear_mode(vha);
6384
6385 /*
6386 * NOTE: Currently the value is kept the same for <24xx and
6387 * >=24xx ISPs. If it is necessary to change it,
6388 * the check should be added for specific ISPs,
6389 * assigning the value appropriately.
6390 */
6391 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006392
6393 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006394}
6395
6396void
6397qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
6398{
6399 /*
6400 * FC-4 Feature bit 0 indicates target functionality to the name server.
6401 */
6402 if (qla_tgt_mode_enabled(vha)) {
6403 if (qla_ini_mode_enabled(vha))
6404 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
6405 else
6406 ct_req->req.rff_id.fc4_feature = BIT_0;
6407 } else if (qla_ini_mode_enabled(vha)) {
6408 ct_req->req.rff_id.fc4_feature = BIT_1;
6409 }
6410}
6411
6412/*
6413 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6414 * @ha: HA context
6415 *
6416 * Beginning of ATIO ring has initialization control block already built
6417 * by nvram config routine.
6418 *
6419 * Returns 0 on success.
6420 */
6421void
6422qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6423{
6424 struct qla_hw_data *ha = vha->hw;
6425 uint16_t cnt;
6426 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6427
6428 if (!qla_tgt_mode_enabled(vha))
6429 return;
6430
6431 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6432 pkt->u.raw.signature = ATIO_PROCESSED;
6433 pkt++;
6434 }
6435
6436}
6437
6438/*
6439 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6440 * @ha: SCSI driver HA context
6441 */
6442void
Quinn Tran2f424b92015-12-17 14:57:07 -05006443qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006444{
6445 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006446 struct atio_from_isp *pkt;
6447 int cnt, i;
6448
6449 if (!vha->flags.online)
6450 return;
6451
6452 while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
6453 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6454 cnt = pkt->u.raw.entry_count;
6455
Quinn Tran2f424b92015-12-17 14:57:07 -05006456 qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt,
6457 ha_locked);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006458
6459 for (i = 0; i < cnt; i++) {
6460 ha->tgt.atio_ring_index++;
6461 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6462 ha->tgt.atio_ring_index = 0;
6463 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6464 } else
6465 ha->tgt.atio_ring_ptr++;
6466
6467 pkt->u.raw.signature = ATIO_PROCESSED;
6468 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6469 }
6470 wmb();
6471 }
6472
6473 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05006474 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Quinn Tran3761f3e2015-06-10 11:05:19 -04006475 RD_REG_DWORD_RELAXED(ISP_ATIO_Q_OUT(vha));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006476}
6477
6478void
Arun Easiaa230bc2013-01-30 03:34:39 -05006479qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006480{
6481 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05006482 if (!QLA_TGT_MODE_ENABLED())
6483 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006484
Arun Easiaa230bc2013-01-30 03:34:39 -05006485 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6486 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6487 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6488
6489 if (IS_ATIO_MSIX_CAPABLE(ha)) {
6490 struct qla_msix_entry *msix = &ha->msix_entries[2];
6491 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6492
6493 icb->msix_atio = cpu_to_le16(msix->entry);
6494 ql_dbg(ql_dbg_init, vha, 0xf072,
6495 "Registering ICB vector 0x%x for atio que.\n",
6496 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006497 }
6498}
6499
6500void
6501qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6502{
6503 struct qla_hw_data *ha = vha->hw;
6504
6505 if (qla_tgt_mode_enabled(vha)) {
6506 if (!ha->tgt.saved_set) {
6507 /* We save only once */
6508 ha->tgt.saved_exchange_count = nv->exchange_count;
6509 ha->tgt.saved_firmware_options_1 =
6510 nv->firmware_options_1;
6511 ha->tgt.saved_firmware_options_2 =
6512 nv->firmware_options_2;
6513 ha->tgt.saved_firmware_options_3 =
6514 nv->firmware_options_3;
6515 ha->tgt.saved_set = 1;
6516 }
6517
Bart Van Asschead950362015-07-09 07:24:08 -07006518 nv->exchange_count = cpu_to_le16(0xFFFF);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006519
6520 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006521 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006522
6523 /* Disable ini mode, if requested */
6524 if (!qla_ini_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006525 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006526
6527 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006528 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006529 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006530 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006531 if (ql2xtgt_tape_enable)
6532 /* Enable FC Tape support */
6533 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6534 else
6535 /* Disable FC Tape support */
6536 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6537
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006538 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006539 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006540 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006541 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006542 } else {
6543 if (ha->tgt.saved_set) {
6544 nv->exchange_count = ha->tgt.saved_exchange_count;
6545 nv->firmware_options_1 =
6546 ha->tgt.saved_firmware_options_1;
6547 nv->firmware_options_2 =
6548 ha->tgt.saved_firmware_options_2;
6549 nv->firmware_options_3 =
6550 ha->tgt.saved_firmware_options_3;
6551 }
6552 return;
6553 }
6554
6555 /* out-of-order frames reassembly */
6556 nv->firmware_options_3 |= BIT_6|BIT_9;
6557
6558 if (ha->tgt.enable_class_2) {
6559 if (vha->flags.init_done)
6560 fc_host_supported_classes(vha->host) =
6561 FC_COS_CLASS2 | FC_COS_CLASS3;
6562
Bart Van Asschead950362015-07-09 07:24:08 -07006563 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006564 } else {
6565 if (vha->flags.init_done)
6566 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6567
Bart Van Asschead950362015-07-09 07:24:08 -07006568 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006569 }
6570}
6571
6572void
6573qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6574 struct init_cb_24xx *icb)
6575{
6576 struct qla_hw_data *ha = vha->hw;
6577
Quinn Tran481ce732015-12-17 14:57:08 -05006578 if (!QLA_TGT_MODE_ENABLED())
6579 return;
6580
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006581 if (ha->tgt.node_name_set) {
6582 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006583 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006584 }
Quinn Tran481ce732015-12-17 14:57:08 -05006585
6586 /* disable ZIO at start time. */
6587 if (!vha->flags.init_done) {
6588 uint32_t tmp;
6589 tmp = le32_to_cpu(icb->firmware_options_2);
6590 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6591 icb->firmware_options_2 = cpu_to_le32(tmp);
6592 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006593}
6594
Arun Easiaa230bc2013-01-30 03:34:39 -05006595void
6596qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6597{
6598 struct qla_hw_data *ha = vha->hw;
6599
6600 if (!QLA_TGT_MODE_ENABLED())
6601 return;
6602
6603 if (qla_tgt_mode_enabled(vha)) {
6604 if (!ha->tgt.saved_set) {
6605 /* We save only once */
6606 ha->tgt.saved_exchange_count = nv->exchange_count;
6607 ha->tgt.saved_firmware_options_1 =
6608 nv->firmware_options_1;
6609 ha->tgt.saved_firmware_options_2 =
6610 nv->firmware_options_2;
6611 ha->tgt.saved_firmware_options_3 =
6612 nv->firmware_options_3;
6613 ha->tgt.saved_set = 1;
6614 }
6615
Bart Van Asschead950362015-07-09 07:24:08 -07006616 nv->exchange_count = cpu_to_le16(0xFFFF);
Arun Easiaa230bc2013-01-30 03:34:39 -05006617
6618 /* Enable target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07006619 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
Arun Easiaa230bc2013-01-30 03:34:39 -05006620
6621 /* Disable ini mode, if requested */
6622 if (!qla_ini_mode_enabled(vha))
Bart Van Asschead950362015-07-09 07:24:08 -07006623 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
Arun Easiaa230bc2013-01-30 03:34:39 -05006624
6625 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006626 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Arun Easiaa230bc2013-01-30 03:34:39 -05006627 /* Enable initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006628 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04006629 if (ql2xtgt_tape_enable)
6630 /* Enable FC tape support */
6631 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6632 else
6633 /* Disable FC tape support */
6634 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6635
Arun Easiaa230bc2013-01-30 03:34:39 -05006636 /* Disable Full Login after LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006637 nv->host_p &= cpu_to_le32(~BIT_10);
Arun Easiaa230bc2013-01-30 03:34:39 -05006638 /* Enable target PRLI control */
Bart Van Asschead950362015-07-09 07:24:08 -07006639 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006640 } else {
6641 if (ha->tgt.saved_set) {
6642 nv->exchange_count = ha->tgt.saved_exchange_count;
6643 nv->firmware_options_1 =
6644 ha->tgt.saved_firmware_options_1;
6645 nv->firmware_options_2 =
6646 ha->tgt.saved_firmware_options_2;
6647 nv->firmware_options_3 =
6648 ha->tgt.saved_firmware_options_3;
6649 }
6650 return;
6651 }
6652
6653 /* out-of-order frames reassembly */
6654 nv->firmware_options_3 |= BIT_6|BIT_9;
6655
6656 if (ha->tgt.enable_class_2) {
6657 if (vha->flags.init_done)
6658 fc_host_supported_classes(vha->host) =
6659 FC_COS_CLASS2 | FC_COS_CLASS3;
6660
Bart Van Asschead950362015-07-09 07:24:08 -07006661 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006662 } else {
6663 if (vha->flags.init_done)
6664 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6665
Bart Van Asschead950362015-07-09 07:24:08 -07006666 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
Arun Easiaa230bc2013-01-30 03:34:39 -05006667 }
6668}
6669
6670void
6671qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
6672 struct init_cb_81xx *icb)
6673{
6674 struct qla_hw_data *ha = vha->hw;
6675
6676 if (!QLA_TGT_MODE_ENABLED())
6677 return;
6678
6679 if (ha->tgt.node_name_set) {
6680 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
Bart Van Asschead950362015-07-09 07:24:08 -07006681 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
Arun Easiaa230bc2013-01-30 03:34:39 -05006682 }
Quinn Tran481ce732015-12-17 14:57:08 -05006683
6684 /* disable ZIO at start time. */
6685 if (!vha->flags.init_done) {
6686 uint32_t tmp;
6687 tmp = le32_to_cpu(icb->firmware_options_2);
6688 tmp &= ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
6689 icb->firmware_options_2 = cpu_to_le32(tmp);
6690 }
6691
Arun Easiaa230bc2013-01-30 03:34:39 -05006692}
6693
6694void
6695qlt_83xx_iospace_config(struct qla_hw_data *ha)
6696{
6697 if (!QLA_TGT_MODE_ENABLED())
6698 return;
6699
6700 ha->msix_count += 1; /* For ATIO Q */
6701}
6702
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006703int
6704qlt_24xx_process_response_error(struct scsi_qla_host *vha,
6705 struct sts_entry_24xx *pkt)
6706{
6707 switch (pkt->entry_type) {
6708 case ABTS_RECV_24XX:
6709 case ABTS_RESP_24XX:
6710 case CTIO_TYPE7:
6711 case NOTIFY_ACK_TYPE:
Quinn Tranf83adb62014-04-11 16:54:43 -04006712 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006713 return 1;
6714 default:
6715 return 0;
6716 }
6717}
6718
6719void
6720qlt_modify_vp_config(struct scsi_qla_host *vha,
6721 struct vp_config_entry_24xx *vpmod)
6722{
6723 if (qla_tgt_mode_enabled(vha))
6724 vpmod->options_idx1 &= ~BIT_5;
6725 /* Disable ini mode, if requested */
6726 if (!qla_ini_mode_enabled(vha))
6727 vpmod->options_idx1 &= ~BIT_4;
6728}
6729
6730void
6731qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
6732{
6733 if (!QLA_TGT_MODE_ENABLED())
6734 return;
6735
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04006736 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Arun Easiaa230bc2013-01-30 03:34:39 -05006737 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
6738 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
6739 } else {
6740 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
6741 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
6742 }
6743
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08006744 mutex_init(&base_vha->vha_tgt.tgt_mutex);
6745 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006746 qlt_clear_mode(base_vha);
6747}
6748
Arun Easiaa230bc2013-01-30 03:34:39 -05006749irqreturn_t
6750qla83xx_msix_atio_q(int irq, void *dev_id)
6751{
6752 struct rsp_que *rsp;
6753 scsi_qla_host_t *vha;
6754 struct qla_hw_data *ha;
6755 unsigned long flags;
6756
6757 rsp = (struct rsp_que *) dev_id;
6758 ha = rsp->hw;
6759 vha = pci_get_drvdata(ha->pdev);
6760
Quinn Tran2f424b92015-12-17 14:57:07 -05006761 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006762
Quinn Tran2f424b92015-12-17 14:57:07 -05006763 qlt_24xx_process_atio_queue(vha, 0);
Arun Easiaa230bc2013-01-30 03:34:39 -05006764
Quinn Tran2f424b92015-12-17 14:57:07 -05006765 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
Arun Easiaa230bc2013-01-30 03:34:39 -05006766
6767 return IRQ_HANDLED;
6768}
6769
Quinn Tran2f424b92015-12-17 14:57:07 -05006770static void
6771qlt_handle_abts_recv_work(struct work_struct *work)
6772{
6773 struct qla_tgt_sess_op *op = container_of(work,
6774 struct qla_tgt_sess_op, work);
6775 scsi_qla_host_t *vha = op->vha;
6776 struct qla_hw_data *ha = vha->hw;
6777 unsigned long flags;
6778
6779 if (qla2x00_reset_active(vha) || (op->chip_reset != ha->chip_reset))
6780 return;
6781
6782 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
6783 qlt_24xx_process_atio_queue(vha, 0);
6784 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
6785
6786 spin_lock_irqsave(&ha->hardware_lock, flags);
6787 qlt_response_pkt_all_vps(vha, (response_t *)&op->atio);
6788 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6789}
6790
6791void
6792qlt_handle_abts_recv(struct scsi_qla_host *vha, response_t *pkt)
6793{
6794 struct qla_tgt_sess_op *op;
6795
6796 op = kzalloc(sizeof(*op), GFP_ATOMIC);
6797
6798 if (!op) {
6799 /* do not reach for ATIO queue here. This is best effort err
6800 * recovery at this point.
6801 */
6802 qlt_response_pkt_all_vps(vha, pkt);
6803 return;
6804 }
6805
6806 memcpy(&op->atio, pkt, sizeof(*pkt));
6807 op->vha = vha;
6808 op->chip_reset = vha->hw->chip_reset;
6809 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
6810 queue_work(qla_tgt_wq, &op->work);
6811 return;
6812}
6813
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006814int
6815qlt_mem_alloc(struct qla_hw_data *ha)
6816{
6817 if (!QLA_TGT_MODE_ENABLED())
6818 return 0;
6819
6820 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
6821 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
6822 if (!ha->tgt.tgt_vp_map)
6823 return -ENOMEM;
6824
6825 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
6826 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
6827 &ha->tgt.atio_dma, GFP_KERNEL);
6828 if (!ha->tgt.atio_ring) {
6829 kfree(ha->tgt.tgt_vp_map);
6830 return -ENOMEM;
6831 }
6832 return 0;
6833}
6834
6835void
6836qlt_mem_free(struct qla_hw_data *ha)
6837{
6838 if (!QLA_TGT_MODE_ENABLED())
6839 return;
6840
6841 if (ha->tgt.atio_ring) {
6842 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
6843 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
6844 ha->tgt.atio_dma);
6845 }
6846 kfree(ha->tgt.tgt_vp_map);
6847}
6848
6849/* vport_slock to be held by the caller */
6850void
6851qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
6852{
6853 if (!QLA_TGT_MODE_ENABLED())
6854 return;
6855
6856 switch (cmd) {
6857 case SET_VP_IDX:
6858 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
6859 break;
6860 case SET_AL_PA:
6861 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
6862 break;
6863 case RESET_VP_IDX:
6864 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
6865 break;
6866 case RESET_AL_PA:
6867 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
6868 break;
6869 }
6870}
6871
6872static int __init qlt_parse_ini_mode(void)
6873{
6874 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
6875 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
6876 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
6877 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
6878 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
6879 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
6880 else
6881 return false;
6882
6883 return true;
6884}
6885
6886int __init qlt_init(void)
6887{
6888 int ret;
6889
6890 if (!qlt_parse_ini_mode()) {
6891 ql_log(ql_log_fatal, NULL, 0xe06b,
6892 "qlt_parse_ini_mode() failed\n");
6893 return -EINVAL;
6894 }
6895
6896 if (!QLA_TGT_MODE_ENABLED())
6897 return 0;
6898
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006899 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
6900 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
6901 qla_tgt_mgmt_cmd), 0, NULL);
6902 if (!qla_tgt_mgmt_cmd_cachep) {
6903 ql_log(ql_log_fatal, NULL, 0xe06d,
6904 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07006905 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006906 }
6907
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006908 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
6909 sizeof(qlt_plogi_ack_t),
6910 __alignof__(qlt_plogi_ack_t),
6911 0, NULL);
6912
6913 if (!qla_tgt_plogi_cachep) {
6914 ql_log(ql_log_fatal, NULL, 0xe06d,
6915 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
6916 ret = -ENOMEM;
6917 goto out_mgmt_cmd_cachep;
6918 }
6919
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006920 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
6921 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
6922 if (!qla_tgt_mgmt_cmd_mempool) {
6923 ql_log(ql_log_fatal, NULL, 0xe06e,
6924 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
6925 ret = -ENOMEM;
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006926 goto out_plogi_cachep;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006927 }
6928
6929 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
6930 if (!qla_tgt_wq) {
6931 ql_log(ql_log_fatal, NULL, 0xe06f,
6932 "alloc_workqueue for qla_tgt_wq failed\n");
6933 ret = -ENOMEM;
6934 goto out_cmd_mempool;
6935 }
6936 /*
6937 * Return 1 to signal that initiator-mode is being disabled
6938 */
6939 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
6940
6941out_cmd_mempool:
6942 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006943out_plogi_cachep:
6944 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006945out_mgmt_cmd_cachep:
6946 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006947 return ret;
6948}
6949
6950void qlt_exit(void)
6951{
6952 if (!QLA_TGT_MODE_ENABLED())
6953 return;
6954
6955 destroy_workqueue(qla_tgt_wq);
6956 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
Alexei Potashnikb7bd1042015-12-17 14:57:02 -05006957 kmem_cache_destroy(qla_tgt_plogi_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006958 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006959}