blk-mq: always free hctx after request queue is freed
[linux/fpc-iii.git] / drivers / scsi / qla2xxx / qla_target.c
blob582d1663f9711f46213c7a2f44304fab62f985d0
1 /*
2 * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4 * based on qla2x00t.c code:
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.
11 * Forward port and refactoring to modern qla2xxx and target/configfs
13 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
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.
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.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #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>
42 #include "qla_def.h"
43 #include "qla_target.h"
45 static int ql2xtgt_tape_enable;
46 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
50 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51 module_param(qlini_mode, charp, S_IRUGO);
52 MODULE_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 "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
59 "when ready "
60 "\"enabled\" (default) - initiator mode will always stay enabled.");
62 static int ql_dm_tgt_ex_pct = 0;
63 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
64 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
65 "For Dual Mode (qlini_mode=dual), this parameter determines "
66 "the percentage of exchanges/cmds FW will allocate resources "
67 "for Target mode.");
69 int ql2xuctrlirq = 1;
70 module_param(ql2xuctrlirq, int, 0644);
71 MODULE_PARM_DESC(ql2xuctrlirq,
72 "User to control IRQ placement via smp_affinity."
73 "Valid with qlini_mode=disabled."
74 "1(default): enable");
76 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
78 static int qla_sam_status = SAM_STAT_BUSY;
79 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
82 * From scsi/fc/fc_fcp.h
84 enum fcp_resp_rsp_codes {
85 FCP_TMF_CMPL = 0,
86 FCP_DATA_LEN_INVALID = 1,
87 FCP_CMND_FIELDS_INVALID = 2,
88 FCP_DATA_PARAM_MISMATCH = 3,
89 FCP_TMF_REJECTED = 4,
90 FCP_TMF_FAILED = 5,
91 FCP_TMF_INVALID_LUN = 9,
95 * fc_pri_ta from scsi/fc/fc_fcp.h
97 #define FCP_PTA_SIMPLE 0 /* simple task attribute */
98 #define FCP_PTA_HEADQ 1 /* head of queue task attribute */
99 #define FCP_PTA_ORDERED 2 /* ordered task attribute */
100 #define FCP_PTA_ACA 4 /* auto. contingent allegiance */
101 #define FCP_PTA_MASK 7 /* mask for task attribute field */
102 #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */
103 #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */
106 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
107 * must be called under HW lock and could unlock/lock it inside.
108 * It isn't an issue, since in the current implementation on the time when
109 * those functions are called:
111 * - Either context is IRQ and only IRQ handler can modify HW data,
112 * including rings related fields,
114 * - Or access to target mode variables from struct qla_tgt doesn't
115 * cross those functions boundaries, except tgt_stop, which
116 * additionally protected by irq_cmd_count.
118 /* Predefs for callbacks handed to qla2xxx LLD */
119 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
120 struct atio_from_isp *pkt, uint8_t);
121 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
122 response_t *pkt);
123 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
124 int fn, void *iocb, int flags);
125 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
126 *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
127 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
128 struct atio_from_isp *atio, uint16_t status, int qfull);
129 static void qlt_disable_vha(struct scsi_qla_host *vha);
130 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
131 static void qlt_send_notify_ack(struct qla_qpair *qpair,
132 struct imm_ntfy_from_isp *ntfy,
133 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
134 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
135 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
136 struct imm_ntfy_from_isp *imm, int ha_locked);
137 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
138 fc_port_t *fcport, bool local);
139 void qlt_unreg_sess(struct fc_port *sess);
140 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
141 struct abts_recv_from_24xx *);
142 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
143 uint16_t);
144 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
145 static inline uint32_t qlt_make_handle(struct qla_qpair *);
148 * Global Variables
150 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
151 struct kmem_cache *qla_tgt_plogi_cachep;
152 static mempool_t *qla_tgt_mgmt_cmd_mempool;
153 static struct workqueue_struct *qla_tgt_wq;
154 static DEFINE_MUTEX(qla_tgt_mutex);
155 static LIST_HEAD(qla_tgt_glist);
157 static const char *prot_op_str(u32 prot_op)
159 switch (prot_op) {
160 case TARGET_PROT_NORMAL: return "NORMAL";
161 case TARGET_PROT_DIN_INSERT: return "DIN_INSERT";
162 case TARGET_PROT_DOUT_INSERT: return "DOUT_INSERT";
163 case TARGET_PROT_DIN_STRIP: return "DIN_STRIP";
164 case TARGET_PROT_DOUT_STRIP: return "DOUT_STRIP";
165 case TARGET_PROT_DIN_PASS: return "DIN_PASS";
166 case TARGET_PROT_DOUT_PASS: return "DOUT_PASS";
167 default: return "UNKNOWN";
171 /* This API intentionally takes dest as a parameter, rather than returning
172 * int value to avoid caller forgetting to issue wmb() after the store */
173 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
175 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
176 *dest = atomic_inc_return(&base_vha->generation_tick);
177 /* memory barrier */
178 wmb();
181 /* Might release hw lock, then reaquire!! */
182 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
184 /* Send marker if required */
185 if (unlikely(vha->marker_needed != 0)) {
186 int rc = qla2x00_issue_marker(vha, vha_locked);
187 if (rc != QLA_SUCCESS) {
188 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
189 "qla_target(%d): issue_marker() failed\n",
190 vha->vp_idx);
192 return rc;
194 return QLA_SUCCESS;
197 static inline
198 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
199 uint8_t *d_id)
201 struct scsi_qla_host *host;
202 uint32_t key = 0;
204 if ((vha->d_id.b.area == d_id[1]) && (vha->d_id.b.domain == d_id[0]) &&
205 (vha->d_id.b.al_pa == d_id[2]))
206 return vha;
208 key = (uint32_t)d_id[0] << 16;
209 key |= (uint32_t)d_id[1] << 8;
210 key |= (uint32_t)d_id[2];
212 host = btree_lookup32(&vha->hw->tgt.host_map, key);
213 if (!host)
214 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
215 "Unable to find host %06x\n", key);
217 return host;
220 static inline
221 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
222 uint16_t vp_idx)
224 struct qla_hw_data *ha = vha->hw;
226 if (vha->vp_idx == vp_idx)
227 return vha;
229 BUG_ON(ha->tgt.tgt_vp_map == NULL);
230 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
231 return ha->tgt.tgt_vp_map[vp_idx].vha;
233 return NULL;
236 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
238 unsigned long flags;
240 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
242 vha->hw->tgt.num_pend_cmds++;
243 if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
244 vha->qla_stats.stat_max_pend_cmds =
245 vha->hw->tgt.num_pend_cmds;
246 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
248 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
250 unsigned long flags;
252 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
253 vha->hw->tgt.num_pend_cmds--;
254 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
258 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
259 struct atio_from_isp *atio, uint8_t ha_locked)
261 struct qla_tgt_sess_op *u;
262 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
263 unsigned long flags;
265 if (tgt->tgt_stop) {
266 ql_dbg(ql_dbg_async, vha, 0x502c,
267 "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
268 vha->vp_idx);
269 goto out_term;
272 u = kzalloc(sizeof(*u), GFP_ATOMIC);
273 if (u == NULL)
274 goto out_term;
276 u->vha = vha;
277 memcpy(&u->atio, atio, sizeof(*atio));
278 INIT_LIST_HEAD(&u->cmd_list);
280 spin_lock_irqsave(&vha->cmd_list_lock, flags);
281 list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
282 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
284 schedule_delayed_work(&vha->unknown_atio_work, 1);
286 out:
287 return;
289 out_term:
290 qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
291 goto out;
294 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
295 uint8_t ha_locked)
297 struct qla_tgt_sess_op *u, *t;
298 scsi_qla_host_t *host;
299 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
300 unsigned long flags;
301 uint8_t queued = 0;
303 list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
304 if (u->aborted) {
305 ql_dbg(ql_dbg_async, vha, 0x502e,
306 "Freeing unknown %s %p, because of Abort\n",
307 "ATIO_TYPE7", u);
308 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
309 &u->atio, ha_locked, 0);
310 goto abort;
313 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
314 if (host != NULL) {
315 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
316 "Requeuing unknown ATIO_TYPE7 %p\n", u);
317 qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
318 } else if (tgt->tgt_stop) {
319 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
320 "Freeing unknown %s %p, because tgt is being stopped\n",
321 "ATIO_TYPE7", u);
322 qlt_send_term_exchange(vha->hw->base_qpair, NULL,
323 &u->atio, ha_locked, 0);
324 } else {
325 ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
326 "Reschedule u %p, vha %p, host %p\n", u, vha, host);
327 if (!queued) {
328 queued = 1;
329 schedule_delayed_work(&vha->unknown_atio_work,
332 continue;
335 abort:
336 spin_lock_irqsave(&vha->cmd_list_lock, flags);
337 list_del(&u->cmd_list);
338 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
339 kfree(u);
343 void qlt_unknown_atio_work_fn(struct work_struct *work)
345 struct scsi_qla_host *vha = container_of(to_delayed_work(work),
346 struct scsi_qla_host, unknown_atio_work);
348 qlt_try_to_dequeue_unknown_atios(vha, 0);
351 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
352 struct atio_from_isp *atio, uint8_t ha_locked)
354 ql_dbg(ql_dbg_tgt, vha, 0xe072,
355 "%s: qla_target(%d): type %x ox_id %04x\n",
356 __func__, vha->vp_idx, atio->u.raw.entry_type,
357 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
359 switch (atio->u.raw.entry_type) {
360 case ATIO_TYPE7:
362 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
363 atio->u.isp24.fcp_hdr.d_id);
364 if (unlikely(NULL == host)) {
365 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
366 "qla_target(%d): Received ATIO_TYPE7 "
367 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
368 atio->u.isp24.fcp_hdr.d_id[0],
369 atio->u.isp24.fcp_hdr.d_id[1],
370 atio->u.isp24.fcp_hdr.d_id[2]);
373 qlt_queue_unknown_atio(vha, atio, ha_locked);
374 break;
376 if (unlikely(!list_empty(&vha->unknown_atio_list)))
377 qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
379 qlt_24xx_atio_pkt(host, atio, ha_locked);
380 break;
383 case IMMED_NOTIFY_TYPE:
385 struct scsi_qla_host *host = vha;
386 struct imm_ntfy_from_isp *entry =
387 (struct imm_ntfy_from_isp *)atio;
389 qlt_issue_marker(vha, ha_locked);
391 if ((entry->u.isp24.vp_index != 0xFF) &&
392 (entry->u.isp24.nport_handle != 0xFFFF)) {
393 host = qlt_find_host_by_vp_idx(vha,
394 entry->u.isp24.vp_index);
395 if (unlikely(!host)) {
396 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
397 "qla_target(%d): Received "
398 "ATIO (IMMED_NOTIFY_TYPE) "
399 "with unknown vp_index %d\n",
400 vha->vp_idx, entry->u.isp24.vp_index);
401 break;
404 qlt_24xx_atio_pkt(host, atio, ha_locked);
405 break;
408 case VP_RPT_ID_IOCB_TYPE:
409 qla24xx_report_id_acquisition(vha,
410 (struct vp_rpt_id_entry_24xx *)atio);
411 break;
413 case ABTS_RECV_24XX:
415 struct abts_recv_from_24xx *entry =
416 (struct abts_recv_from_24xx *)atio;
417 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
418 entry->vp_index);
419 unsigned long flags;
421 if (unlikely(!host)) {
422 ql_dbg(ql_dbg_tgt, vha, 0xe00a,
423 "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
424 "received, with unknown vp_index %d\n",
425 vha->vp_idx, entry->vp_index);
426 break;
428 if (!ha_locked)
429 spin_lock_irqsave(&host->hw->hardware_lock, flags);
430 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
431 if (!ha_locked)
432 spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
433 break;
436 /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
438 default:
439 ql_dbg(ql_dbg_tgt, vha, 0xe040,
440 "qla_target(%d): Received unknown ATIO atio "
441 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
442 break;
445 return false;
448 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
449 struct rsp_que *rsp, response_t *pkt)
451 switch (pkt->entry_type) {
452 case CTIO_CRC2:
453 ql_dbg(ql_dbg_tgt, vha, 0xe073,
454 "qla_target(%d):%s: CRC2 Response pkt\n",
455 vha->vp_idx, __func__);
456 /* fall through */
457 case CTIO_TYPE7:
459 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
460 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
461 entry->vp_index);
462 if (unlikely(!host)) {
463 ql_dbg(ql_dbg_tgt, vha, 0xe041,
464 "qla_target(%d): Response pkt (CTIO_TYPE7) "
465 "received, with unknown vp_index %d\n",
466 vha->vp_idx, entry->vp_index);
467 break;
469 qlt_response_pkt(host, rsp, pkt);
470 break;
473 case IMMED_NOTIFY_TYPE:
475 struct scsi_qla_host *host = vha;
476 struct imm_ntfy_from_isp *entry =
477 (struct imm_ntfy_from_isp *)pkt;
479 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
480 if (unlikely(!host)) {
481 ql_dbg(ql_dbg_tgt, vha, 0xe042,
482 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
483 "received, with unknown vp_index %d\n",
484 vha->vp_idx, entry->u.isp24.vp_index);
485 break;
487 qlt_response_pkt(host, rsp, pkt);
488 break;
491 case NOTIFY_ACK_TYPE:
493 struct scsi_qla_host *host = vha;
494 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
496 if (0xFF != entry->u.isp24.vp_index) {
497 host = qlt_find_host_by_vp_idx(vha,
498 entry->u.isp24.vp_index);
499 if (unlikely(!host)) {
500 ql_dbg(ql_dbg_tgt, vha, 0xe043,
501 "qla_target(%d): Response "
502 "pkt (NOTIFY_ACK_TYPE) "
503 "received, with unknown "
504 "vp_index %d\n", vha->vp_idx,
505 entry->u.isp24.vp_index);
506 break;
509 qlt_response_pkt(host, rsp, pkt);
510 break;
513 case ABTS_RECV_24XX:
515 struct abts_recv_from_24xx *entry =
516 (struct abts_recv_from_24xx *)pkt;
517 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
518 entry->vp_index);
519 if (unlikely(!host)) {
520 ql_dbg(ql_dbg_tgt, vha, 0xe044,
521 "qla_target(%d): Response pkt "
522 "(ABTS_RECV_24XX) received, with unknown "
523 "vp_index %d\n", vha->vp_idx, entry->vp_index);
524 break;
526 qlt_response_pkt(host, rsp, pkt);
527 break;
530 case ABTS_RESP_24XX:
532 struct abts_resp_to_24xx *entry =
533 (struct abts_resp_to_24xx *)pkt;
534 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
535 entry->vp_index);
536 if (unlikely(!host)) {
537 ql_dbg(ql_dbg_tgt, vha, 0xe045,
538 "qla_target(%d): Response pkt "
539 "(ABTS_RECV_24XX) received, with unknown "
540 "vp_index %d\n", vha->vp_idx, entry->vp_index);
541 break;
543 qlt_response_pkt(host, rsp, pkt);
544 break;
546 default:
547 qlt_response_pkt(vha, rsp, pkt);
548 break;
554 * All qlt_plogi_ack_t operations are protected by hardware_lock
556 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
557 struct imm_ntfy_from_isp *ntfy, int type)
559 struct qla_work_evt *e;
560 e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
561 if (!e)
562 return QLA_FUNCTION_FAILED;
564 e->u.nack.fcport = fcport;
565 e->u.nack.type = type;
566 memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
567 return qla2x00_post_work(vha, e);
570 static
571 void qla2x00_async_nack_sp_done(void *s, int res)
573 struct srb *sp = (struct srb *)s;
574 struct scsi_qla_host *vha = sp->vha;
575 unsigned long flags;
577 ql_dbg(ql_dbg_disc, vha, 0x20f2,
578 "Async done-%s res %x %8phC type %d\n",
579 sp->name, res, sp->fcport->port_name, sp->type);
581 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
582 sp->fcport->flags &= ~FCF_ASYNC_SENT;
583 sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
585 switch (sp->type) {
586 case SRB_NACK_PLOGI:
587 sp->fcport->login_gen++;
588 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
589 sp->fcport->logout_on_delete = 1;
590 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
591 sp->fcport->send_els_logo = 0;
592 break;
594 case SRB_NACK_PRLI:
595 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
596 sp->fcport->deleted = 0;
597 sp->fcport->send_els_logo = 0;
599 if (!sp->fcport->login_succ &&
600 !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
601 sp->fcport->login_succ = 1;
603 vha->fcport_count++;
604 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
605 qla24xx_sched_upd_fcport(sp->fcport);
606 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
607 } else {
608 sp->fcport->login_retry = 0;
609 sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
610 sp->fcport->deleted = 0;
611 sp->fcport->logout_on_delete = 1;
613 break;
615 case SRB_NACK_LOGO:
616 sp->fcport->login_gen++;
617 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
618 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
619 break;
621 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
623 sp->free(sp);
626 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
627 struct imm_ntfy_from_isp *ntfy, int type)
629 int rval = QLA_FUNCTION_FAILED;
630 srb_t *sp;
631 char *c = NULL;
633 fcport->flags |= FCF_ASYNC_SENT;
634 switch (type) {
635 case SRB_NACK_PLOGI:
636 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
637 c = "PLOGI";
638 break;
639 case SRB_NACK_PRLI:
640 fcport->fw_login_state = DSC_LS_PRLI_PEND;
641 fcport->deleted = 0;
642 c = "PRLI";
643 break;
644 case SRB_NACK_LOGO:
645 fcport->fw_login_state = DSC_LS_LOGO_PEND;
646 c = "LOGO";
647 break;
650 sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
651 if (!sp)
652 goto done;
654 sp->type = type;
655 sp->name = "nack";
657 sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
658 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
660 sp->u.iocb_cmd.u.nack.ntfy = ntfy;
661 sp->done = qla2x00_async_nack_sp_done;
663 ql_dbg(ql_dbg_disc, vha, 0x20f4,
664 "Async-%s %8phC hndl %x %s\n",
665 sp->name, fcport->port_name, sp->handle, c);
667 rval = qla2x00_start_sp(sp);
668 if (rval != QLA_SUCCESS)
669 goto done_free_sp;
671 return rval;
673 done_free_sp:
674 sp->free(sp);
675 done:
676 fcport->flags &= ~FCF_ASYNC_SENT;
677 return rval;
680 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
682 fc_port_t *t;
683 unsigned long flags;
685 switch (e->u.nack.type) {
686 case SRB_NACK_PRLI:
687 t = e->u.nack.fcport;
688 flush_work(&t->del_work);
689 flush_work(&t->free_work);
690 mutex_lock(&vha->vha_tgt.tgt_mutex);
691 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
692 mutex_unlock(&vha->vha_tgt.tgt_mutex);
693 if (t) {
694 ql_log(ql_log_info, vha, 0xd034,
695 "%s create sess success %p", __func__, t);
696 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
697 /* create sess has an extra kref */
698 vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
699 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
701 break;
703 qla24xx_async_notify_ack(vha, e->u.nack.fcport,
704 (struct imm_ntfy_from_isp*)e->u.nack.iocb, e->u.nack.type);
707 void qla24xx_delete_sess_fn(struct work_struct *work)
709 fc_port_t *fcport = container_of(work, struct fc_port, del_work);
710 struct qla_hw_data *ha = fcport->vha->hw;
711 unsigned long flags;
713 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
715 if (fcport->se_sess) {
716 ha->tgt.tgt_ops->shutdown_sess(fcport);
717 ha->tgt.tgt_ops->put_sess(fcport);
718 } else {
719 qlt_unreg_sess(fcport);
721 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
725 * Called from qla2x00_reg_remote_port()
727 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
729 struct qla_hw_data *ha = vha->hw;
730 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
731 struct fc_port *sess = fcport;
732 unsigned long flags;
734 if (!vha->hw->tgt.tgt_ops)
735 return;
737 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
738 if (tgt->tgt_stop) {
739 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
740 return;
743 if (fcport->disc_state == DSC_DELETE_PEND) {
744 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
745 return;
748 if (!sess->se_sess) {
749 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
751 mutex_lock(&vha->vha_tgt.tgt_mutex);
752 sess = qlt_create_sess(vha, fcport, false);
753 mutex_unlock(&vha->vha_tgt.tgt_mutex);
755 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
756 } else {
757 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
758 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
759 return;
762 if (!kref_get_unless_zero(&sess->sess_kref)) {
763 ql_dbg(ql_dbg_disc, vha, 0x2107,
764 "%s: kref_get fail sess %8phC \n",
765 __func__, sess->port_name);
766 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
767 return;
770 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
771 "qla_target(%u): %ssession for port %8phC "
772 "(loop ID %d) reappeared\n", vha->vp_idx,
773 sess->local ? "local " : "", sess->port_name, sess->loop_id);
775 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
776 "Reappeared sess %p\n", sess);
778 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
779 fcport->loop_id,
780 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
783 if (sess && sess->local) {
784 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
785 "qla_target(%u): local session for "
786 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
787 fcport->port_name, sess->loop_id);
788 sess->local = 0;
790 ha->tgt.tgt_ops->put_sess(sess);
791 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
795 * This is a zero-base ref-counting solution, since hardware_lock
796 * guarantees that ref_count is not modified concurrently.
797 * Upon successful return content of iocb is undefined
799 static struct qlt_plogi_ack_t *
800 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
801 struct imm_ntfy_from_isp *iocb)
803 struct qlt_plogi_ack_t *pla;
805 list_for_each_entry(pla, &vha->plogi_ack_list, list) {
806 if (pla->id.b24 == id->b24) {
807 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
808 "%s %d %8phC Term INOT due to new INOT",
809 __func__, __LINE__,
810 pla->iocb.u.isp24.port_name);
811 qlt_send_term_imm_notif(vha, &pla->iocb, 1);
812 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
813 return pla;
817 pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
818 if (!pla) {
819 ql_dbg(ql_dbg_async, vha, 0x5088,
820 "qla_target(%d): Allocation of plogi_ack failed\n",
821 vha->vp_idx);
822 return NULL;
825 memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
826 pla->id = *id;
827 list_add_tail(&pla->list, &vha->plogi_ack_list);
829 return pla;
832 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
833 struct qlt_plogi_ack_t *pla)
835 struct imm_ntfy_from_isp *iocb = &pla->iocb;
836 port_id_t port_id;
837 uint16_t loop_id;
838 fc_port_t *fcport = pla->fcport;
840 BUG_ON(!pla->ref_count);
841 pla->ref_count--;
843 if (pla->ref_count)
844 return;
846 ql_dbg(ql_dbg_disc, vha, 0x5089,
847 "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
848 " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
849 iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
850 iocb->u.isp24.port_id[0],
851 le16_to_cpu(iocb->u.isp24.nport_handle),
852 iocb->u.isp24.exchange_address, iocb->ox_id);
854 port_id.b.domain = iocb->u.isp24.port_id[2];
855 port_id.b.area = iocb->u.isp24.port_id[1];
856 port_id.b.al_pa = iocb->u.isp24.port_id[0];
857 port_id.b.rsvd_1 = 0;
859 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
861 fcport->loop_id = loop_id;
862 fcport->d_id = port_id;
863 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
864 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
865 else
866 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
868 list_for_each_entry(fcport, &vha->vp_fcports, list) {
869 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
870 fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
871 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
872 fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
875 list_del(&pla->list);
876 kmem_cache_free(qla_tgt_plogi_cachep, pla);
879 void
880 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
881 struct fc_port *sess, enum qlt_plogi_link_t link)
883 struct imm_ntfy_from_isp *iocb = &pla->iocb;
884 /* Inc ref_count first because link might already be pointing at pla */
885 pla->ref_count++;
887 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
888 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
889 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
890 sess, link, sess->port_name,
891 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
892 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
893 pla->ref_count, pla, link);
895 if (link == QLT_PLOGI_LINK_CONFLICT) {
896 switch (sess->disc_state) {
897 case DSC_DELETED:
898 case DSC_DELETE_PEND:
899 pla->ref_count--;
900 return;
901 default:
902 break;
906 if (sess->plogi_link[link])
907 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
909 if (link == QLT_PLOGI_LINK_SAME_WWN)
910 pla->fcport = sess;
912 sess->plogi_link[link] = pla;
915 typedef struct {
916 /* These fields must be initialized by the caller */
917 port_id_t id;
919 * number of cmds dropped while we were waiting for
920 * initiator to ack LOGO initialize to 1 if LOGO is
921 * triggered by a command, otherwise, to 0
923 int cmd_count;
925 /* These fields are used by callee */
926 struct list_head list;
927 } qlt_port_logo_t;
929 static void
930 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
932 qlt_port_logo_t *tmp;
933 int res;
935 mutex_lock(&vha->vha_tgt.tgt_mutex);
937 list_for_each_entry(tmp, &vha->logo_list, list) {
938 if (tmp->id.b24 == logo->id.b24) {
939 tmp->cmd_count += logo->cmd_count;
940 mutex_unlock(&vha->vha_tgt.tgt_mutex);
941 return;
945 list_add_tail(&logo->list, &vha->logo_list);
947 mutex_unlock(&vha->vha_tgt.tgt_mutex);
949 res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
951 mutex_lock(&vha->vha_tgt.tgt_mutex);
952 list_del(&logo->list);
953 mutex_unlock(&vha->vha_tgt.tgt_mutex);
955 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
956 "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
957 logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
958 logo->cmd_count, res);
961 void qlt_free_session_done(struct work_struct *work)
963 struct fc_port *sess = container_of(work, struct fc_port,
964 free_work);
965 struct qla_tgt *tgt = sess->tgt;
966 struct scsi_qla_host *vha = sess->vha;
967 struct qla_hw_data *ha = vha->hw;
968 unsigned long flags;
969 bool logout_started = false;
970 scsi_qla_host_t *base_vha;
971 struct qlt_plogi_ack_t *own =
972 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
974 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
975 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
976 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
977 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
978 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
979 sess->logout_on_delete, sess->keep_nport_handle,
980 sess->send_els_logo);
982 if (!IS_SW_RESV_ADDR(sess->d_id)) {
983 if (sess->send_els_logo) {
984 qlt_port_logo_t logo;
986 logo.id = sess->d_id;
987 logo.cmd_count = 0;
988 if (!own)
989 qlt_send_first_logo(vha, &logo);
990 sess->send_els_logo = 0;
993 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
994 int rc;
996 if (!own ||
997 (own &&
998 (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
999 rc = qla2x00_post_async_logout_work(vha, sess,
1000 NULL);
1001 if (rc != QLA_SUCCESS)
1002 ql_log(ql_log_warn, vha, 0xf085,
1003 "Schedule logo failed sess %p rc %d\n",
1004 sess, rc);
1005 else
1006 logout_started = true;
1007 } else if (own && (own->iocb.u.isp24.status_subcode ==
1008 ELS_PRLI) && ha->flags.rida_fmt2) {
1009 rc = qla2x00_post_async_prlo_work(vha, sess,
1010 NULL);
1011 if (rc != QLA_SUCCESS)
1012 ql_log(ql_log_warn, vha, 0xf085,
1013 "Schedule PRLO failed sess %p rc %d\n",
1014 sess, rc);
1015 else
1016 logout_started = true;
1022 * Release the target session for FC Nexus from fabric module code.
1024 if (sess->se_sess != NULL)
1025 ha->tgt.tgt_ops->free_session(sess);
1027 if (logout_started) {
1028 bool traced = false;
1030 while (!READ_ONCE(sess->logout_completed)) {
1031 if (!traced) {
1032 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
1033 "%s: waiting for sess %p logout\n",
1034 __func__, sess);
1035 traced = true;
1037 msleep(100);
1040 ql_dbg(ql_dbg_disc, vha, 0xf087,
1041 "%s: sess %p logout completed\n", __func__, sess);
1044 if (sess->logo_ack_needed) {
1045 sess->logo_ack_needed = 0;
1046 qla24xx_async_notify_ack(vha, sess,
1047 (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1050 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1051 if (sess->se_sess) {
1052 sess->se_sess = NULL;
1053 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1054 tgt->sess_count--;
1057 sess->disc_state = DSC_DELETED;
1058 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1059 sess->deleted = QLA_SESS_DELETED;
1061 if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1062 vha->fcport_count--;
1063 sess->login_succ = 0;
1066 qla2x00_clear_loop_id(sess);
1068 if (sess->conflict) {
1069 sess->conflict->login_pause = 0;
1070 sess->conflict = NULL;
1071 if (!test_bit(UNLOADING, &vha->dpc_flags))
1072 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1076 struct qlt_plogi_ack_t *con =
1077 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1078 struct imm_ntfy_from_isp *iocb;
1079 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1081 if (con) {
1082 iocb = &con->iocb;
1083 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1084 "se_sess %p / sess %p port %8phC is gone,"
1085 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1086 sess->se_sess, sess, sess->port_name,
1087 own ? "releasing own PLOGI" : "no own PLOGI pending",
1088 own ? own->ref_count : -1,
1089 iocb->u.isp24.port_name, con->ref_count);
1090 qlt_plogi_ack_unref(vha, con);
1091 sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1092 } else {
1093 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1094 "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1095 sess->se_sess, sess, sess->port_name,
1096 own ? "releasing own PLOGI" :
1097 "no own PLOGI pending",
1098 own ? own->ref_count : -1);
1101 if (own) {
1102 sess->fw_login_state = DSC_LS_PLOGI_PEND;
1103 qlt_plogi_ack_unref(vha, own);
1104 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1108 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1110 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1111 "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1112 sess, sess->port_name, vha->fcport_count);
1114 if (tgt && (tgt->sess_count == 0))
1115 wake_up_all(&tgt->waitQ);
1117 if (vha->fcport_count == 0)
1118 wake_up_all(&vha->fcport_waitQ);
1120 base_vha = pci_get_drvdata(ha->pdev);
1122 sess->free_pending = 0;
1124 if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
1125 return;
1127 if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1128 switch (vha->host->active_mode) {
1129 case MODE_INITIATOR:
1130 case MODE_DUAL:
1131 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1132 qla2xxx_wake_dpc(vha);
1133 break;
1134 case MODE_TARGET:
1135 default:
1136 /* no-op */
1137 break;
1142 /* ha->tgt.sess_lock supposed to be held on entry */
1143 void qlt_unreg_sess(struct fc_port *sess)
1145 struct scsi_qla_host *vha = sess->vha;
1146 unsigned long flags;
1148 ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1149 "%s sess %p for deletion %8phC\n",
1150 __func__, sess, sess->port_name);
1152 spin_lock_irqsave(&sess->vha->work_lock, flags);
1153 if (sess->free_pending) {
1154 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1155 return;
1157 sess->free_pending = 1;
1158 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1160 if (sess->se_sess)
1161 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1163 qla2x00_mark_device_lost(vha, sess, 0, 0);
1165 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1166 sess->disc_state = DSC_DELETE_PEND;
1167 sess->last_rscn_gen = sess->rscn_gen;
1168 sess->last_login_gen = sess->login_gen;
1170 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1171 !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1172 sess->nvme_flag |= NVME_FLAG_DELETING;
1173 schedule_work(&sess->nvme_del_work);
1174 } else {
1175 INIT_WORK(&sess->free_work, qlt_free_session_done);
1176 schedule_work(&sess->free_work);
1179 EXPORT_SYMBOL(qlt_unreg_sess);
1181 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1183 struct qla_hw_data *ha = vha->hw;
1184 struct fc_port *sess = NULL;
1185 uint16_t loop_id;
1186 int res = 0;
1187 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1188 unsigned long flags;
1190 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1191 if (loop_id == 0xFFFF) {
1192 /* Global event */
1193 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1194 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1195 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1196 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1197 } else {
1198 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1199 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1200 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1203 ql_dbg(ql_dbg_tgt, vha, 0xe000,
1204 "Using sess for qla_tgt_reset: %p\n", sess);
1205 if (!sess) {
1206 res = -ESRCH;
1207 return res;
1210 ql_dbg(ql_dbg_tgt, vha, 0xe047,
1211 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1212 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1213 mcmd, loop_id);
1215 return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1218 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1220 if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1221 sess->logout_on_delete = 0;
1222 sess->logo_ack_needed = 0;
1223 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1224 sess->scan_state = 0;
1228 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1230 struct qla_tgt *tgt = sess->tgt;
1231 unsigned long flags;
1232 u16 sec;
1234 switch (sess->disc_state) {
1235 case DSC_DELETE_PEND:
1236 return;
1237 case DSC_DELETED:
1238 if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
1239 wake_up_all(&tgt->waitQ);
1240 if (sess->vha->fcport_count == 0)
1241 wake_up_all(&sess->vha->fcport_waitQ);
1243 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1244 !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1245 return;
1246 break;
1247 case DSC_UPD_FCPORT:
1249 * This port is not done reporting to upper layer.
1250 * let it finish
1252 sess->next_disc_state = DSC_DELETE_PEND;
1253 sec = jiffies_to_msecs(jiffies -
1254 sess->jiffies_at_registration)/1000;
1255 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1256 sess->sec_since_registration = sec;
1257 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1258 "%s %8phC : Slow Rport registration(%d Sec)\n",
1259 __func__, sess->port_name, sec);
1261 return;
1262 default:
1263 break;
1266 spin_lock_irqsave(&sess->vha->work_lock, flags);
1267 if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1268 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1269 return;
1271 sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1272 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1274 sess->disc_state = DSC_DELETE_PEND;
1276 qla24xx_chk_fcp_state(sess);
1278 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1279 "Scheduling sess %p for deletion %8phC\n",
1280 sess, sess->port_name);
1282 INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1283 WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1286 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1288 struct fc_port *sess;
1289 scsi_qla_host_t *vha = tgt->vha;
1291 list_for_each_entry(sess, &vha->vp_fcports, list) {
1292 if (sess->se_sess)
1293 qlt_schedule_sess_for_deletion(sess);
1296 /* At this point tgt could be already dead */
1299 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
1300 uint16_t *loop_id)
1302 struct qla_hw_data *ha = vha->hw;
1303 dma_addr_t gid_list_dma;
1304 struct gid_list_info *gid_list;
1305 char *id_iter;
1306 int res, rc, i;
1307 uint16_t entries;
1309 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1310 &gid_list_dma, GFP_KERNEL);
1311 if (!gid_list) {
1312 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1313 "qla_target(%d): DMA Alloc failed of %u\n",
1314 vha->vp_idx, qla2x00_gid_list_size(ha));
1315 return -ENOMEM;
1318 /* Get list of logged in devices */
1319 rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1320 if (rc != QLA_SUCCESS) {
1321 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1322 "qla_target(%d): get_id_list() failed: %x\n",
1323 vha->vp_idx, rc);
1324 res = -EBUSY;
1325 goto out_free_id_list;
1328 id_iter = (char *)gid_list;
1329 res = -ENOENT;
1330 for (i = 0; i < entries; i++) {
1331 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
1332 if ((gid->al_pa == s_id[2]) &&
1333 (gid->area == s_id[1]) &&
1334 (gid->domain == s_id[0])) {
1335 *loop_id = le16_to_cpu(gid->loop_id);
1336 res = 0;
1337 break;
1339 id_iter += ha->gid_list_info_size;
1342 out_free_id_list:
1343 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1344 gid_list, gid_list_dma);
1345 return res;
1349 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1350 * Caller must put it.
1352 static struct fc_port *qlt_create_sess(
1353 struct scsi_qla_host *vha,
1354 fc_port_t *fcport,
1355 bool local)
1357 struct qla_hw_data *ha = vha->hw;
1358 struct fc_port *sess = fcport;
1359 unsigned long flags;
1361 if (vha->vha_tgt.qla_tgt->tgt_stop)
1362 return NULL;
1364 if (fcport->se_sess) {
1365 if (!kref_get_unless_zero(&sess->sess_kref)) {
1366 ql_dbg(ql_dbg_disc, vha, 0x20f6,
1367 "%s: kref_get_unless_zero failed for %8phC\n",
1368 __func__, sess->port_name);
1369 return NULL;
1371 return fcport;
1373 sess->tgt = vha->vha_tgt.qla_tgt;
1374 sess->local = local;
1377 * Under normal circumstances we want to logout from firmware when
1378 * session eventually ends and release corresponding nport handle.
1379 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1380 * code will adjust these flags as necessary.
1382 sess->logout_on_delete = 1;
1383 sess->keep_nport_handle = 0;
1384 sess->logout_completed = 0;
1386 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1387 &fcport->port_name[0], sess) < 0) {
1388 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1389 "(%d) %8phC check_initiator_node_acl failed\n",
1390 vha->vp_idx, fcport->port_name);
1391 return NULL;
1392 } else {
1393 kref_init(&fcport->sess_kref);
1395 * Take an extra reference to ->sess_kref here to handle
1396 * fc_port access across ->tgt.sess_lock reaquire.
1398 if (!kref_get_unless_zero(&sess->sess_kref)) {
1399 ql_dbg(ql_dbg_disc, vha, 0x20f7,
1400 "%s: kref_get_unless_zero failed for %8phC\n",
1401 __func__, sess->port_name);
1402 return NULL;
1405 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1406 if (!IS_SW_RESV_ADDR(sess->d_id))
1407 vha->vha_tgt.qla_tgt->sess_count++;
1409 qlt_do_generation_tick(vha, &sess->generation);
1410 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1413 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1414 "Adding sess %p se_sess %p to tgt %p sess_count %d\n",
1415 sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1416 vha->vha_tgt.qla_tgt->sess_count);
1418 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1419 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1420 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1421 vha->vp_idx, local ? "local " : "", fcport->port_name,
1422 fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1423 sess->d_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
1425 return sess;
1429 * max_gen - specifies maximum session generation
1430 * at which this deletion requestion is still valid
1432 void
1433 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1435 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1436 struct fc_port *sess = fcport;
1437 unsigned long flags;
1439 if (!vha->hw->tgt.tgt_ops)
1440 return;
1442 if (!tgt)
1443 return;
1445 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1446 if (tgt->tgt_stop) {
1447 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1448 return;
1450 if (!sess->se_sess) {
1451 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1452 return;
1455 if (max_gen - sess->generation < 0) {
1456 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1457 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1458 "Ignoring stale deletion request for se_sess %p / sess %p"
1459 " for port %8phC, req_gen %d, sess_gen %d\n",
1460 sess->se_sess, sess, sess->port_name, max_gen,
1461 sess->generation);
1462 return;
1465 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1467 sess->local = 1;
1468 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1469 qlt_schedule_sess_for_deletion(sess);
1472 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1474 struct qla_hw_data *ha = tgt->ha;
1475 unsigned long flags;
1476 int res;
1478 * We need to protect against race, when tgt is freed before or
1479 * inside wake_up()
1481 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1482 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1483 "tgt %p, sess_count=%d\n",
1484 tgt, tgt->sess_count);
1485 res = (tgt->sess_count == 0);
1486 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1488 return res;
1491 /* Called by tcm_qla2xxx configfs code */
1492 int qlt_stop_phase1(struct qla_tgt *tgt)
1494 struct scsi_qla_host *vha = tgt->vha;
1495 struct qla_hw_data *ha = tgt->ha;
1496 unsigned long flags;
1498 mutex_lock(&ha->optrom_mutex);
1499 mutex_lock(&qla_tgt_mutex);
1501 if (tgt->tgt_stop || tgt->tgt_stopped) {
1502 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1503 "Already in tgt->tgt_stop or tgt_stopped state\n");
1504 mutex_unlock(&qla_tgt_mutex);
1505 mutex_unlock(&ha->optrom_mutex);
1506 return -EPERM;
1509 ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1510 vha->host_no, vha);
1512 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1513 * Lock is needed, because we still can get an incoming packet.
1515 mutex_lock(&vha->vha_tgt.tgt_mutex);
1516 tgt->tgt_stop = 1;
1517 qlt_clear_tgt_db(tgt);
1518 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1519 mutex_unlock(&qla_tgt_mutex);
1521 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1522 "Waiting for sess works (tgt %p)", tgt);
1523 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1524 while (!list_empty(&tgt->sess_works_list)) {
1525 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1526 flush_scheduled_work();
1527 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1529 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1531 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1532 "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1534 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1536 /* Big hammer */
1537 if (!ha->flags.host_shutting_down &&
1538 (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1539 qlt_disable_vha(vha);
1541 /* Wait for sessions to clear out (just in case) */
1542 wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1543 mutex_unlock(&ha->optrom_mutex);
1545 return 0;
1547 EXPORT_SYMBOL(qlt_stop_phase1);
1549 /* Called by tcm_qla2xxx configfs code */
1550 void qlt_stop_phase2(struct qla_tgt *tgt)
1552 scsi_qla_host_t *vha = tgt->vha;
1554 if (tgt->tgt_stopped) {
1555 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1556 "Already in tgt->tgt_stopped state\n");
1557 dump_stack();
1558 return;
1560 if (!tgt->tgt_stop) {
1561 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1562 "%s: phase1 stop is not completed\n", __func__);
1563 dump_stack();
1564 return;
1567 mutex_lock(&vha->vha_tgt.tgt_mutex);
1568 tgt->tgt_stop = 0;
1569 tgt->tgt_stopped = 1;
1570 mutex_unlock(&vha->vha_tgt.tgt_mutex);
1572 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1573 tgt);
1575 switch (vha->qlini_mode) {
1576 case QLA2XXX_INI_MODE_EXCLUSIVE:
1577 vha->flags.online = 1;
1578 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1579 break;
1580 default:
1581 break;
1584 EXPORT_SYMBOL(qlt_stop_phase2);
1586 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1587 static void qlt_release(struct qla_tgt *tgt)
1589 scsi_qla_host_t *vha = tgt->vha;
1590 void *node;
1591 u64 key = 0;
1592 u16 i;
1593 struct qla_qpair_hint *h;
1594 struct qla_hw_data *ha = vha->hw;
1596 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stop &&
1597 !tgt->tgt_stopped)
1598 qlt_stop_phase1(tgt);
1600 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
1601 qlt_stop_phase2(tgt);
1603 for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1604 unsigned long flags;
1606 h = &tgt->qphints[i];
1607 if (h->qpair) {
1608 spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1609 list_del(&h->hint_elem);
1610 spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1611 h->qpair = NULL;
1614 kfree(tgt->qphints);
1615 mutex_lock(&qla_tgt_mutex);
1616 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1617 mutex_unlock(&qla_tgt_mutex);
1619 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1620 btree_remove64(&tgt->lun_qpair_map, key);
1622 btree_destroy64(&tgt->lun_qpair_map);
1624 if (vha->vp_idx)
1625 if (ha->tgt.tgt_ops &&
1626 ha->tgt.tgt_ops->remove_target &&
1627 vha->vha_tgt.target_lport_ptr)
1628 ha->tgt.tgt_ops->remove_target(vha);
1630 vha->vha_tgt.qla_tgt = NULL;
1632 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1633 "Release of tgt %p finished\n", tgt);
1635 kfree(tgt);
1638 /* ha->hardware_lock supposed to be held on entry */
1639 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1640 const void *param, unsigned int param_size)
1642 struct qla_tgt_sess_work_param *prm;
1643 unsigned long flags;
1645 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1646 if (!prm) {
1647 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1648 "qla_target(%d): Unable to create session "
1649 "work, command will be refused", 0);
1650 return -ENOMEM;
1653 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1654 "Scheduling work (type %d, prm %p)"
1655 " to find session for param %p (size %d, tgt %p)\n",
1656 type, prm, param, param_size, tgt);
1658 prm->type = type;
1659 memcpy(&prm->tm_iocb, param, param_size);
1661 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1662 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1663 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1665 schedule_work(&tgt->sess_work);
1667 return 0;
1671 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1673 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1674 struct imm_ntfy_from_isp *ntfy,
1675 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1676 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1678 struct scsi_qla_host *vha = qpair->vha;
1679 struct qla_hw_data *ha = vha->hw;
1680 request_t *pkt;
1681 struct nack_to_isp *nack;
1683 if (!ha->flags.fw_started)
1684 return;
1686 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1688 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1689 if (!pkt) {
1690 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1691 "qla_target(%d): %s failed: unable to allocate "
1692 "request packet\n", vha->vp_idx, __func__);
1693 return;
1696 if (vha->vha_tgt.qla_tgt != NULL)
1697 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1699 pkt->entry_type = NOTIFY_ACK_TYPE;
1700 pkt->entry_count = 1;
1702 nack = (struct nack_to_isp *)pkt;
1703 nack->ox_id = ntfy->ox_id;
1705 nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1706 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1707 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1708 nack->u.isp24.flags = ntfy->u.isp24.flags &
1709 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1711 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1712 nack->u.isp24.status = ntfy->u.isp24.status;
1713 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1714 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1715 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1716 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1717 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1718 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1719 nack->u.isp24.srr_reject_code = srr_reject_code;
1720 nack->u.isp24.srr_reject_code_expl = srr_explan;
1721 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1723 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1724 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1725 vha->vp_idx, nack->u.isp24.status);
1727 /* Memory Barrier */
1728 wmb();
1729 qla2x00_start_iocbs(vha, qpair->req);
1732 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1734 struct scsi_qla_host *vha = mcmd->vha;
1735 struct qla_hw_data *ha = vha->hw;
1736 struct abts_resp_to_24xx *resp;
1737 uint32_t f_ctl, h;
1738 uint8_t *p;
1739 int rc;
1740 struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1741 struct qla_qpair *qpair = mcmd->qpair;
1743 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1744 "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1745 ha, mcmd->fc_tm_rsp);
1747 rc = qlt_check_reserve_free_req(qpair, 1);
1748 if (rc) {
1749 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1750 "qla_target(%d): %s failed: unable to allocate request packet\n",
1751 vha->vp_idx, __func__);
1752 return -EAGAIN;
1755 resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1756 memset(resp, 0, sizeof(*resp));
1758 h = qlt_make_handle(qpair);
1759 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1761 * CTIO type 7 from the firmware doesn't provide a way to
1762 * know the initiator's LOOP ID, hence we can't find
1763 * the session and, so, the command.
1765 return -EAGAIN;
1766 } else {
1767 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1770 resp->handle = MAKE_HANDLE(qpair->req->id, h);
1771 resp->entry_type = ABTS_RESP_24XX;
1772 resp->entry_count = 1;
1773 resp->nport_handle = abts->nport_handle;
1774 resp->vp_index = vha->vp_idx;
1775 resp->sof_type = abts->sof_type;
1776 resp->exchange_address = abts->exchange_address;
1777 resp->fcp_hdr_le = abts->fcp_hdr_le;
1778 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1779 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1780 F_CTL_SEQ_INITIATIVE);
1781 p = (uint8_t *)&f_ctl;
1782 resp->fcp_hdr_le.f_ctl[0] = *p++;
1783 resp->fcp_hdr_le.f_ctl[1] = *p++;
1784 resp->fcp_hdr_le.f_ctl[2] = *p;
1786 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1787 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1788 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1789 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1790 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1791 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1793 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1794 if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1795 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1796 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1797 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1798 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1799 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1800 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1801 } else {
1802 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1803 resp->payload.ba_rjt.reason_code =
1804 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1805 /* Other bytes are zero */
1808 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1810 /* Memory Barrier */
1811 wmb();
1812 if (qpair->reqq_start_iocbs)
1813 qpair->reqq_start_iocbs(qpair);
1814 else
1815 qla2x00_start_iocbs(vha, qpair->req);
1817 return rc;
1821 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1823 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1824 struct abts_recv_from_24xx *abts, uint32_t status,
1825 bool ids_reversed)
1827 struct scsi_qla_host *vha = qpair->vha;
1828 struct qla_hw_data *ha = vha->hw;
1829 struct abts_resp_to_24xx *resp;
1830 uint32_t f_ctl;
1831 uint8_t *p;
1833 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1834 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1835 ha, abts, status);
1837 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1838 NULL);
1839 if (!resp) {
1840 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1841 "qla_target(%d): %s failed: unable to allocate "
1842 "request packet", vha->vp_idx, __func__);
1843 return;
1846 resp->entry_type = ABTS_RESP_24XX;
1847 resp->handle = QLA_TGT_SKIP_HANDLE;
1848 resp->entry_count = 1;
1849 resp->nport_handle = abts->nport_handle;
1850 resp->vp_index = vha->vp_idx;
1851 resp->sof_type = abts->sof_type;
1852 resp->exchange_address = abts->exchange_address;
1853 resp->fcp_hdr_le = abts->fcp_hdr_le;
1854 f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1855 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1856 F_CTL_SEQ_INITIATIVE);
1857 p = (uint8_t *)&f_ctl;
1858 resp->fcp_hdr_le.f_ctl[0] = *p++;
1859 resp->fcp_hdr_le.f_ctl[1] = *p++;
1860 resp->fcp_hdr_le.f_ctl[2] = *p;
1861 if (ids_reversed) {
1862 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1863 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1864 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1865 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1866 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1867 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1868 } else {
1869 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1870 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1871 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1872 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1873 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1874 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1876 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1877 if (status == FCP_TMF_CMPL) {
1878 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1879 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1880 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1881 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1882 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1883 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1884 } else {
1885 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1886 resp->payload.ba_rjt.reason_code =
1887 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1888 /* Other bytes are zero */
1891 vha->vha_tgt.qla_tgt->abts_resp_expected++;
1893 /* Memory Barrier */
1894 wmb();
1895 if (qpair->reqq_start_iocbs)
1896 qpair->reqq_start_iocbs(qpair);
1897 else
1898 qla2x00_start_iocbs(vha, qpair->req);
1902 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1904 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1905 struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1907 struct ctio7_to_24xx *ctio;
1908 u16 tmp;
1909 struct abts_recv_from_24xx *entry;
1911 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1912 if (ctio == NULL) {
1913 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1914 "qla_target(%d): %s failed: unable to allocate "
1915 "request packet\n", vha->vp_idx, __func__);
1916 return;
1919 if (mcmd)
1920 /* abts from remote port */
1921 entry = &mcmd->orig_iocb.abts;
1922 else
1923 /* abts from this driver. */
1924 entry = (struct abts_recv_from_24xx *)pkt;
1927 * We've got on entrance firmware's response on by us generated
1928 * ABTS response. So, in it ID fields are reversed.
1931 ctio->entry_type = CTIO_TYPE7;
1932 ctio->entry_count = 1;
1933 ctio->nport_handle = entry->nport_handle;
1934 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1935 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1936 ctio->vp_index = vha->vp_idx;
1937 ctio->exchange_addr = entry->exchange_addr_to_abort;
1938 tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1940 if (mcmd) {
1941 ctio->initiator_id[0] = entry->fcp_hdr_le.s_id[0];
1942 ctio->initiator_id[1] = entry->fcp_hdr_le.s_id[1];
1943 ctio->initiator_id[2] = entry->fcp_hdr_le.s_id[2];
1945 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1946 tmp |= (mcmd->abort_io_attr << 9);
1947 else if (qpair->retry_term_cnt & 1)
1948 tmp |= (0x4 << 9);
1949 } else {
1950 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1951 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1952 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1954 if (qpair->retry_term_cnt & 1)
1955 tmp |= (0x4 << 9);
1957 ctio->u.status1.flags = cpu_to_le16(tmp);
1958 ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1960 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1961 "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1962 le16_to_cpu(ctio->u.status1.flags),
1963 le16_to_cpu(ctio->u.status1.ox_id),
1964 (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1966 /* Memory Barrier */
1967 wmb();
1968 if (qpair->reqq_start_iocbs)
1969 qpair->reqq_start_iocbs(qpair);
1970 else
1971 qla2x00_start_iocbs(vha, qpair->req);
1973 if (mcmd)
1974 qlt_build_abts_resp_iocb(mcmd);
1975 else
1976 qlt_24xx_send_abts_resp(qpair,
1977 (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1981 /* drop cmds for the given lun
1982 * XXX only looks for cmds on the port through which lun reset was recieved
1983 * XXX does not go through the list of other port (which may have cmds
1984 * for the same lun)
1986 static void abort_cmds_for_lun(struct scsi_qla_host *vha,
1987 u64 lun, uint8_t *s_id)
1989 struct qla_tgt_sess_op *op;
1990 struct qla_tgt_cmd *cmd;
1991 uint32_t key;
1992 unsigned long flags;
1994 key = sid_to_key(s_id);
1995 spin_lock_irqsave(&vha->cmd_list_lock, flags);
1996 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1997 uint32_t op_key;
1998 u64 op_lun;
2000 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2001 op_lun = scsilun_to_int(
2002 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2003 if (op_key == key && op_lun == lun)
2004 op->aborted = true;
2007 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2008 uint32_t op_key;
2009 u64 op_lun;
2011 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2012 op_lun = scsilun_to_int(
2013 (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2014 if (op_key == key && op_lun == lun)
2015 op->aborted = true;
2018 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2019 uint32_t cmd_key;
2020 u64 cmd_lun;
2022 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2023 cmd_lun = scsilun_to_int(
2024 (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2025 if (cmd_key == key && cmd_lun == lun)
2026 cmd->aborted = 1;
2028 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2031 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2032 uint64_t unpacked_lun)
2034 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2035 struct qla_qpair_hint *h = NULL;
2037 if (vha->flags.qpairs_available) {
2038 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2039 if (!h)
2040 h = &tgt->qphints[0];
2041 } else {
2042 h = &tgt->qphints[0];
2045 return h;
2048 static void qlt_do_tmr_work(struct work_struct *work)
2050 struct qla_tgt_mgmt_cmd *mcmd =
2051 container_of(work, struct qla_tgt_mgmt_cmd, work);
2052 struct qla_hw_data *ha = mcmd->vha->hw;
2053 int rc = EIO;
2054 uint32_t tag;
2055 unsigned long flags;
2057 switch (mcmd->tmr_func) {
2058 case QLA_TGT_ABTS:
2059 tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
2060 break;
2061 default:
2062 tag = 0;
2063 break;
2066 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2067 mcmd->tmr_func, tag);
2069 if (rc != 0) {
2070 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2071 switch (mcmd->tmr_func) {
2072 case QLA_TGT_ABTS:
2073 mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2074 qlt_build_abts_resp_iocb(mcmd);
2075 break;
2076 case QLA_TGT_LUN_RESET:
2077 case QLA_TGT_CLEAR_TS:
2078 case QLA_TGT_ABORT_TS:
2079 case QLA_TGT_CLEAR_ACA:
2080 case QLA_TGT_TARGET_RESET:
2081 qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2082 qla_sam_status);
2083 break;
2085 case QLA_TGT_ABORT_ALL:
2086 case QLA_TGT_NEXUS_LOSS_SESS:
2087 case QLA_TGT_NEXUS_LOSS:
2088 qlt_send_notify_ack(mcmd->qpair,
2089 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2090 break;
2092 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2094 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2095 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
2096 mcmd->vha->vp_idx, rc);
2097 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2101 /* ha->hardware_lock supposed to be held on entry */
2102 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2103 struct abts_recv_from_24xx *abts, struct fc_port *sess)
2105 struct qla_hw_data *ha = vha->hw;
2106 struct qla_tgt_mgmt_cmd *mcmd;
2107 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2109 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2110 "qla_target(%d): task abort (tag=%d)\n",
2111 vha->vp_idx, abts->exchange_addr_to_abort);
2113 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2114 if (mcmd == NULL) {
2115 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2116 "qla_target(%d): %s: Allocation of ABORT cmd failed",
2117 vha->vp_idx, __func__);
2118 return -ENOMEM;
2120 memset(mcmd, 0, sizeof(*mcmd));
2121 mcmd->cmd_type = TYPE_TGT_TMCMD;
2122 mcmd->sess = sess;
2123 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2124 mcmd->reset_count = ha->base_qpair->chip_reset;
2125 mcmd->tmr_func = QLA_TGT_ABTS;
2126 mcmd->qpair = h->qpair;
2127 mcmd->vha = vha;
2130 * LUN is looked up by target-core internally based on the passed
2131 * abts->exchange_addr_to_abort tag.
2133 mcmd->se_cmd.cpuid = h->cpuid;
2135 if (ha->tgt.tgt_ops->find_cmd_by_tag) {
2136 struct qla_tgt_cmd *abort_cmd;
2138 abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2139 abts->exchange_addr_to_abort);
2140 if (abort_cmd && abort_cmd->qpair) {
2141 mcmd->qpair = abort_cmd->qpair;
2142 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2143 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2144 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2148 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2149 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2151 return 0;
2155 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2157 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2158 struct abts_recv_from_24xx *abts)
2160 struct qla_hw_data *ha = vha->hw;
2161 struct fc_port *sess;
2162 uint32_t tag = abts->exchange_addr_to_abort;
2163 uint8_t s_id[3];
2164 int rc;
2165 unsigned long flags;
2167 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2168 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2169 "qla_target(%d): ABTS: Abort Sequence not "
2170 "supported\n", vha->vp_idx);
2171 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2172 false);
2173 return;
2176 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2177 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2178 "qla_target(%d): ABTS: Unknown Exchange "
2179 "Address received\n", vha->vp_idx);
2180 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2181 false);
2182 return;
2185 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2186 "qla_target(%d): task abort (s_id=%x:%x:%x, "
2187 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
2188 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
2189 le32_to_cpu(abts->fcp_hdr_le.parameter));
2191 s_id[0] = abts->fcp_hdr_le.s_id[2];
2192 s_id[1] = abts->fcp_hdr_le.s_id[1];
2193 s_id[2] = abts->fcp_hdr_le.s_id[0];
2195 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2196 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2197 if (!sess) {
2198 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2199 "qla_target(%d): task abort for non-existent session\n",
2200 vha->vp_idx);
2201 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2203 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2204 false);
2205 return;
2207 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2210 if (sess->deleted) {
2211 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2212 false);
2213 return;
2216 rc = __qlt_24xx_handle_abts(vha, abts, sess);
2217 if (rc != 0) {
2218 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2219 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2220 vha->vp_idx, rc);
2221 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2222 false);
2223 return;
2228 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2230 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2231 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2233 struct scsi_qla_host *ha = mcmd->vha;
2234 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2235 struct ctio7_to_24xx *ctio;
2236 uint16_t temp;
2238 ql_dbg(ql_dbg_tgt, ha, 0xe008,
2239 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2240 ha, atio, resp_code);
2243 ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2244 if (ctio == NULL) {
2245 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2246 "qla_target(%d): %s failed: unable to allocate "
2247 "request packet\n", ha->vp_idx, __func__);
2248 return;
2251 ctio->entry_type = CTIO_TYPE7;
2252 ctio->entry_count = 1;
2253 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2254 ctio->nport_handle = mcmd->sess->loop_id;
2255 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2256 ctio->vp_index = ha->vp_idx;
2257 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2258 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2259 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2260 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2261 temp = (atio->u.isp24.attr << 9)|
2262 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2263 ctio->u.status1.flags = cpu_to_le16(temp);
2264 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2265 ctio->u.status1.ox_id = cpu_to_le16(temp);
2266 ctio->u.status1.scsi_status =
2267 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2268 ctio->u.status1.response_len = cpu_to_le16(8);
2269 ctio->u.status1.sense_data[0] = resp_code;
2271 /* Memory Barrier */
2272 wmb();
2273 if (qpair->reqq_start_iocbs)
2274 qpair->reqq_start_iocbs(qpair);
2275 else
2276 qla2x00_start_iocbs(ha, qpair->req);
2279 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2281 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2283 EXPORT_SYMBOL(qlt_free_mcmd);
2286 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2287 * reacquire
2289 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2290 uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2292 struct atio_from_isp *atio = &cmd->atio;
2293 struct ctio7_to_24xx *ctio;
2294 uint16_t temp;
2295 struct scsi_qla_host *vha = cmd->vha;
2297 ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2298 "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2299 "sense_key=%02x, asc=%02x, ascq=%02x",
2300 vha, atio, scsi_status, sense_key, asc, ascq);
2302 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2303 if (!ctio) {
2304 ql_dbg(ql_dbg_async, vha, 0x3067,
2305 "qla2x00t(%ld): %s failed: unable to allocate request packet",
2306 vha->host_no, __func__);
2307 goto out;
2310 ctio->entry_type = CTIO_TYPE7;
2311 ctio->entry_count = 1;
2312 ctio->handle = QLA_TGT_SKIP_HANDLE;
2313 ctio->nport_handle = cmd->sess->loop_id;
2314 ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2315 ctio->vp_index = vha->vp_idx;
2316 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2317 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2318 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2319 ctio->exchange_addr = atio->u.isp24.exchange_addr;
2320 temp = (atio->u.isp24.attr << 9) |
2321 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2322 ctio->u.status1.flags = cpu_to_le16(temp);
2323 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2324 ctio->u.status1.ox_id = cpu_to_le16(temp);
2325 ctio->u.status1.scsi_status =
2326 cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2327 ctio->u.status1.response_len = cpu_to_le16(18);
2328 ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2330 if (ctio->u.status1.residual != 0)
2331 ctio->u.status1.scsi_status |=
2332 cpu_to_le16(SS_RESIDUAL_UNDER);
2334 /* Response code and sense key */
2335 put_unaligned_le32(((0x70 << 24) | (sense_key << 8)),
2336 (&ctio->u.status1.sense_data)[0]);
2337 /* Additional sense length */
2338 put_unaligned_le32(0x0a, (&ctio->u.status1.sense_data)[1]);
2339 /* ASC and ASCQ */
2340 put_unaligned_le32(((asc << 24) | (ascq << 16)),
2341 (&ctio->u.status1.sense_data)[3]);
2343 /* Memory Barrier */
2344 wmb();
2346 if (qpair->reqq_start_iocbs)
2347 qpair->reqq_start_iocbs(qpair);
2348 else
2349 qla2x00_start_iocbs(vha, qpair->req);
2351 out:
2352 return;
2355 /* callback from target fabric module code */
2356 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2358 struct scsi_qla_host *vha = mcmd->sess->vha;
2359 struct qla_hw_data *ha = vha->hw;
2360 unsigned long flags;
2361 struct qla_qpair *qpair = mcmd->qpair;
2362 bool free_mcmd = true;
2364 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2365 "TM response mcmd (%p) status %#x state %#x",
2366 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2368 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2370 if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2372 * Either the port is not online or this request was from
2373 * previous life, just abort the processing.
2375 ql_dbg(ql_dbg_async, vha, 0xe100,
2376 "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2377 vha->flags.online, qla2x00_reset_active(vha),
2378 mcmd->reset_count, qpair->chip_reset);
2379 ha->tgt.tgt_ops->free_mcmd(mcmd);
2380 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2381 return;
2384 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2385 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2386 case ELS_LOGO:
2387 case ELS_PRLO:
2388 case ELS_TPRLO:
2389 ql_dbg(ql_dbg_disc, vha, 0x2106,
2390 "TM response logo %phC status %#x state %#x",
2391 mcmd->sess->port_name, mcmd->fc_tm_rsp,
2392 mcmd->flags);
2393 qlt_schedule_sess_for_deletion(mcmd->sess);
2394 break;
2395 default:
2396 qlt_send_notify_ack(vha->hw->base_qpair,
2397 &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2398 break;
2400 } else {
2401 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2402 qlt_build_abts_resp_iocb(mcmd);
2403 free_mcmd = false;
2404 } else
2405 qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2406 mcmd->fc_tm_rsp);
2409 * Make the callback for ->free_mcmd() to queue_work() and invoke
2410 * target_put_sess_cmd() to drop cmd_kref to 1. The final
2411 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2412 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2413 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2414 * qlt_xmit_tm_rsp() returns here..
2416 if (free_mcmd)
2417 ha->tgt.tgt_ops->free_mcmd(mcmd);
2419 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2421 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2423 /* No locks */
2424 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2426 struct qla_tgt_cmd *cmd = prm->cmd;
2428 BUG_ON(cmd->sg_cnt == 0);
2430 prm->sg = (struct scatterlist *)cmd->sg;
2431 prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2432 cmd->sg_cnt, cmd->dma_data_direction);
2433 if (unlikely(prm->seg_cnt == 0))
2434 goto out_err;
2436 prm->cmd->sg_mapped = 1;
2438 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2440 * If greater than four sg entries then we need to allocate
2441 * the continuation entries
2443 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2444 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2445 QLA_TGT_DATASEGS_PER_CMD_24XX,
2446 QLA_TGT_DATASEGS_PER_CONT_24XX);
2447 } else {
2448 /* DIF */
2449 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2450 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2451 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2452 prm->tot_dsds = prm->seg_cnt;
2453 } else
2454 prm->tot_dsds = prm->seg_cnt;
2456 if (cmd->prot_sg_cnt) {
2457 prm->prot_sg = cmd->prot_sg;
2458 prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2459 cmd->prot_sg, cmd->prot_sg_cnt,
2460 cmd->dma_data_direction);
2461 if (unlikely(prm->prot_seg_cnt == 0))
2462 goto out_err;
2464 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2465 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2466 /* Dif Bundling not support here */
2467 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2468 cmd->blk_sz);
2469 prm->tot_dsds += prm->prot_seg_cnt;
2470 } else
2471 prm->tot_dsds += prm->prot_seg_cnt;
2475 return 0;
2477 out_err:
2478 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2479 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2480 0, prm->cmd->sg_cnt);
2481 return -1;
2484 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2486 struct qla_hw_data *ha;
2487 struct qla_qpair *qpair;
2488 if (!cmd->sg_mapped)
2489 return;
2491 qpair = cmd->qpair;
2493 dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2494 cmd->dma_data_direction);
2495 cmd->sg_mapped = 0;
2497 if (cmd->prot_sg_cnt)
2498 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2499 cmd->dma_data_direction);
2501 if (!cmd->ctx)
2502 return;
2503 ha = vha->hw;
2504 if (cmd->ctx_dsd_alloced)
2505 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2507 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2510 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2511 uint32_t req_cnt)
2513 uint32_t cnt;
2514 struct req_que *req = qpair->req;
2516 if (req->cnt < (req_cnt + 2)) {
2517 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2518 RD_REG_DWORD_RELAXED(req->req_q_out));
2520 if (req->ring_index < cnt)
2521 req->cnt = cnt - req->ring_index;
2522 else
2523 req->cnt = req->length - (req->ring_index - cnt);
2525 if (unlikely(req->cnt < (req_cnt + 2)))
2526 return -EAGAIN;
2529 req->cnt -= req_cnt;
2531 return 0;
2535 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2537 static inline void *qlt_get_req_pkt(struct req_que *req)
2539 /* Adjust ring index. */
2540 req->ring_index++;
2541 if (req->ring_index == req->length) {
2542 req->ring_index = 0;
2543 req->ring_ptr = req->ring;
2544 } else {
2545 req->ring_ptr++;
2547 return (cont_entry_t *)req->ring_ptr;
2550 /* ha->hardware_lock supposed to be held on entry */
2551 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2553 uint32_t h;
2554 int index;
2555 uint8_t found = 0;
2556 struct req_que *req = qpair->req;
2558 h = req->current_outstanding_cmd;
2560 for (index = 1; index < req->num_outstanding_cmds; index++) {
2561 h++;
2562 if (h == req->num_outstanding_cmds)
2563 h = 1;
2565 if (h == QLA_TGT_SKIP_HANDLE)
2566 continue;
2568 if (!req->outstanding_cmds[h]) {
2569 found = 1;
2570 break;
2574 if (found) {
2575 req->current_outstanding_cmd = h;
2576 } else {
2577 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2578 "qla_target(%d): Ran out of empty cmd slots\n",
2579 qpair->vha->vp_idx);
2580 h = QLA_TGT_NULL_HANDLE;
2583 return h;
2586 /* ha->hardware_lock supposed to be held on entry */
2587 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2588 struct qla_tgt_prm *prm)
2590 uint32_t h;
2591 struct ctio7_to_24xx *pkt;
2592 struct atio_from_isp *atio = &prm->cmd->atio;
2593 uint16_t temp;
2595 pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2596 prm->pkt = pkt;
2597 memset(pkt, 0, sizeof(*pkt));
2599 pkt->entry_type = CTIO_TYPE7;
2600 pkt->entry_count = (uint8_t)prm->req_cnt;
2601 pkt->vp_index = prm->cmd->vp_idx;
2603 h = qlt_make_handle(qpair);
2604 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2606 * CTIO type 7 from the firmware doesn't provide a way to
2607 * know the initiator's LOOP ID, hence we can't find
2608 * the session and, so, the command.
2610 return -EAGAIN;
2611 } else
2612 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2614 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
2615 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2616 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2617 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2618 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2619 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2620 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2621 pkt->exchange_addr = atio->u.isp24.exchange_addr;
2622 temp = atio->u.isp24.attr << 9;
2623 pkt->u.status0.flags |= cpu_to_le16(temp);
2624 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2625 pkt->u.status0.ox_id = cpu_to_le16(temp);
2626 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2628 return 0;
2632 * ha->hardware_lock supposed to be held on entry. We have already made sure
2633 * that there is sufficient amount of request entries to not drop it.
2635 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2637 int cnt;
2638 uint32_t *dword_ptr;
2640 /* Build continuation packets */
2641 while (prm->seg_cnt > 0) {
2642 cont_a64_entry_t *cont_pkt64 =
2643 (cont_a64_entry_t *)qlt_get_req_pkt(
2644 prm->cmd->qpair->req);
2647 * Make sure that from cont_pkt64 none of
2648 * 64-bit specific fields used for 32-bit
2649 * addressing. Cast to (cont_entry_t *) for
2650 * that.
2653 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2655 cont_pkt64->entry_count = 1;
2656 cont_pkt64->sys_define = 0;
2658 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2659 dword_ptr = (uint32_t *)&cont_pkt64->dseg_0_address;
2661 /* Load continuation entry data segments */
2662 for (cnt = 0;
2663 cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2664 cnt++, prm->seg_cnt--) {
2665 *dword_ptr++ =
2666 cpu_to_le32(lower_32_bits
2667 (sg_dma_address(prm->sg)));
2668 *dword_ptr++ = cpu_to_le32(upper_32_bits
2669 (sg_dma_address(prm->sg)));
2670 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2672 prm->sg = sg_next(prm->sg);
2678 * ha->hardware_lock supposed to be held on entry. We have already made sure
2679 * that there is sufficient amount of request entries to not drop it.
2681 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2683 int cnt;
2684 uint32_t *dword_ptr;
2685 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2687 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2689 /* Setup packet address segment pointer */
2690 dword_ptr = pkt24->u.status0.dseg_0_address;
2692 /* Set total data segment count */
2693 if (prm->seg_cnt)
2694 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2696 if (prm->seg_cnt == 0) {
2697 /* No data transfer */
2698 *dword_ptr++ = 0;
2699 *dword_ptr = 0;
2700 return;
2703 /* If scatter gather */
2705 /* Load command entry data segments */
2706 for (cnt = 0;
2707 (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2708 cnt++, prm->seg_cnt--) {
2709 *dword_ptr++ =
2710 cpu_to_le32(lower_32_bits(sg_dma_address(prm->sg)));
2712 *dword_ptr++ = cpu_to_le32(upper_32_bits(
2713 sg_dma_address(prm->sg)));
2715 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
2717 prm->sg = sg_next(prm->sg);
2720 qlt_load_cont_data_segments(prm);
2723 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2725 return cmd->bufflen > 0;
2728 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2730 struct qla_tgt_cmd *cmd;
2731 struct scsi_qla_host *vha;
2733 /* asc 0x10=dif error */
2734 if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2735 cmd = prm->cmd;
2736 vha = cmd->vha;
2737 /* ASCQ */
2738 switch (prm->sense_buffer[13]) {
2739 case 1:
2740 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2741 "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2742 "se_cmd=%p tag[%x]",
2743 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2744 cmd->atio.u.isp24.exchange_addr);
2745 break;
2746 case 2:
2747 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2748 "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2749 "se_cmd=%p tag[%x]",
2750 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2751 cmd->atio.u.isp24.exchange_addr);
2752 break;
2753 case 3:
2754 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2755 "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2756 "se_cmd=%p tag[%x]",
2757 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2758 cmd->atio.u.isp24.exchange_addr);
2759 break;
2760 default:
2761 ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2762 "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2763 "se_cmd=%p tag[%x]",
2764 cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2765 cmd->atio.u.isp24.exchange_addr);
2766 break;
2768 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2773 * Called without ha->hardware_lock held
2775 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2776 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2777 uint32_t *full_req_cnt)
2779 struct se_cmd *se_cmd = &cmd->se_cmd;
2780 struct qla_qpair *qpair = cmd->qpair;
2782 prm->cmd = cmd;
2783 prm->tgt = cmd->tgt;
2784 prm->pkt = NULL;
2785 prm->rq_result = scsi_status;
2786 prm->sense_buffer = &cmd->sense_buffer[0];
2787 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2788 prm->sg = NULL;
2789 prm->seg_cnt = -1;
2790 prm->req_cnt = 1;
2791 prm->residual = 0;
2792 prm->add_status_pkt = 0;
2793 prm->prot_sg = NULL;
2794 prm->prot_seg_cnt = 0;
2795 prm->tot_dsds = 0;
2797 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2798 if (qlt_pci_map_calc_cnt(prm) != 0)
2799 return -EAGAIN;
2802 *full_req_cnt = prm->req_cnt;
2804 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2805 prm->residual = se_cmd->residual_count;
2806 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2807 "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2808 prm->residual, se_cmd->tag,
2809 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2810 cmd->bufflen, prm->rq_result);
2811 prm->rq_result |= SS_RESIDUAL_UNDER;
2812 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2813 prm->residual = se_cmd->residual_count;
2814 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2815 "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2816 prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2817 se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2818 prm->rq_result |= SS_RESIDUAL_OVER;
2821 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2823 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2824 * ignored in *xmit_response() below
2826 if (qlt_has_data(cmd)) {
2827 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2828 (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2829 (prm->rq_result != 0))) {
2830 prm->add_status_pkt = 1;
2831 (*full_req_cnt)++;
2836 return 0;
2839 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2840 int sending_sense)
2842 if (cmd->qpair->enable_class_2)
2843 return 0;
2845 if (sending_sense)
2846 return cmd->conf_compl_supported;
2847 else
2848 return cmd->qpair->enable_explicit_conf &&
2849 cmd->conf_compl_supported;
2852 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2853 struct qla_tgt_prm *prm)
2855 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2856 (uint32_t)sizeof(ctio->u.status1.sense_data));
2857 ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2858 if (qlt_need_explicit_conf(prm->cmd, 0)) {
2859 ctio->u.status0.flags |= cpu_to_le16(
2860 CTIO7_FLAGS_EXPLICIT_CONFORM |
2861 CTIO7_FLAGS_CONFORM_REQ);
2863 ctio->u.status0.residual = cpu_to_le32(prm->residual);
2864 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2865 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2866 int i;
2868 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2869 if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2870 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2871 "Skipping EXPLICIT_CONFORM and "
2872 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2873 "non GOOD status\n");
2874 goto skip_explict_conf;
2876 ctio->u.status1.flags |= cpu_to_le16(
2877 CTIO7_FLAGS_EXPLICIT_CONFORM |
2878 CTIO7_FLAGS_CONFORM_REQ);
2880 skip_explict_conf:
2881 ctio->u.status1.flags &=
2882 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2883 ctio->u.status1.flags |=
2884 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2885 ctio->u.status1.scsi_status |=
2886 cpu_to_le16(SS_SENSE_LEN_VALID);
2887 ctio->u.status1.sense_length =
2888 cpu_to_le16(prm->sense_buffer_len);
2889 for (i = 0; i < prm->sense_buffer_len/4; i++)
2890 ((uint32_t *)ctio->u.status1.sense_data)[i] =
2891 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2893 qlt_print_dif_err(prm);
2895 } else {
2896 ctio->u.status1.flags &=
2897 ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2898 ctio->u.status1.flags |=
2899 cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2900 ctio->u.status1.sense_length = 0;
2901 memset(ctio->u.status1.sense_data, 0,
2902 sizeof(ctio->u.status1.sense_data));
2905 /* Sense with len > 24, is it possible ??? */
2908 static inline int
2909 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2911 switch (se_cmd->prot_op) {
2912 case TARGET_PROT_DOUT_INSERT:
2913 case TARGET_PROT_DIN_STRIP:
2914 if (ql2xenablehba_err_chk >= 1)
2915 return 1;
2916 break;
2917 case TARGET_PROT_DOUT_PASS:
2918 case TARGET_PROT_DIN_PASS:
2919 if (ql2xenablehba_err_chk >= 2)
2920 return 1;
2921 break;
2922 case TARGET_PROT_DIN_INSERT:
2923 case TARGET_PROT_DOUT_STRIP:
2924 return 1;
2925 default:
2926 break;
2928 return 0;
2931 static inline int
2932 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2934 switch (se_cmd->prot_op) {
2935 case TARGET_PROT_DIN_INSERT:
2936 case TARGET_PROT_DOUT_INSERT:
2937 case TARGET_PROT_DIN_STRIP:
2938 case TARGET_PROT_DOUT_STRIP:
2939 case TARGET_PROT_DIN_PASS:
2940 case TARGET_PROT_DOUT_PASS:
2941 return 1;
2942 default:
2943 return 0;
2945 return 0;
2949 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2951 static void
2952 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2953 uint16_t *pfw_prot_opts)
2955 struct se_cmd *se_cmd = &cmd->se_cmd;
2956 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2957 scsi_qla_host_t *vha = cmd->tgt->vha;
2958 struct qla_hw_data *ha = vha->hw;
2959 uint32_t t32 = 0;
2962 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2963 * have been immplemented by TCM, before AppTag is avail.
2964 * Look for modesense_handlers[]
2966 ctx->app_tag = 0;
2967 ctx->app_tag_mask[0] = 0x0;
2968 ctx->app_tag_mask[1] = 0x0;
2970 if (IS_PI_UNINIT_CAPABLE(ha)) {
2971 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2972 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2973 *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2974 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2975 *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2978 t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2980 switch (se_cmd->prot_type) {
2981 case TARGET_DIF_TYPE0_PROT:
2983 * No check for ql2xenablehba_err_chk, as it
2984 * would be an I/O error if hba tag generation
2985 * is not done.
2987 ctx->ref_tag = cpu_to_le32(lba);
2988 /* enable ALL bytes of the ref tag */
2989 ctx->ref_tag_mask[0] = 0xff;
2990 ctx->ref_tag_mask[1] = 0xff;
2991 ctx->ref_tag_mask[2] = 0xff;
2992 ctx->ref_tag_mask[3] = 0xff;
2993 break;
2994 case TARGET_DIF_TYPE1_PROT:
2996 * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2997 * REF tag, and 16 bit app tag.
2999 ctx->ref_tag = cpu_to_le32(lba);
3000 if (!qla_tgt_ref_mask_check(se_cmd) ||
3001 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3002 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3003 break;
3005 /* enable ALL bytes of the ref tag */
3006 ctx->ref_tag_mask[0] = 0xff;
3007 ctx->ref_tag_mask[1] = 0xff;
3008 ctx->ref_tag_mask[2] = 0xff;
3009 ctx->ref_tag_mask[3] = 0xff;
3010 break;
3011 case TARGET_DIF_TYPE2_PROT:
3013 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3014 * tag has to match LBA in CDB + N
3016 ctx->ref_tag = cpu_to_le32(lba);
3017 if (!qla_tgt_ref_mask_check(se_cmd) ||
3018 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3019 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3020 break;
3022 /* enable ALL bytes of the ref tag */
3023 ctx->ref_tag_mask[0] = 0xff;
3024 ctx->ref_tag_mask[1] = 0xff;
3025 ctx->ref_tag_mask[2] = 0xff;
3026 ctx->ref_tag_mask[3] = 0xff;
3027 break;
3028 case TARGET_DIF_TYPE3_PROT:
3029 /* For TYPE 3 protection: 16 bit GUARD only */
3030 *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3031 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3032 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3033 break;
3037 static inline int
3038 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3040 uint32_t *cur_dsd;
3041 uint32_t transfer_length = 0;
3042 uint32_t data_bytes;
3043 uint32_t dif_bytes;
3044 uint8_t bundling = 1;
3045 struct crc_context *crc_ctx_pkt = NULL;
3046 struct qla_hw_data *ha;
3047 struct ctio_crc2_to_fw *pkt;
3048 dma_addr_t crc_ctx_dma;
3049 uint16_t fw_prot_opts = 0;
3050 struct qla_tgt_cmd *cmd = prm->cmd;
3051 struct se_cmd *se_cmd = &cmd->se_cmd;
3052 uint32_t h;
3053 struct atio_from_isp *atio = &prm->cmd->atio;
3054 struct qla_tc_param tc;
3055 uint16_t t16;
3056 scsi_qla_host_t *vha = cmd->vha;
3058 ha = vha->hw;
3060 pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3061 prm->pkt = pkt;
3062 memset(pkt, 0, sizeof(*pkt));
3064 ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3065 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3066 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3067 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3069 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3070 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3071 bundling = 0;
3073 /* Compute dif len and adjust data len to incude protection */
3074 data_bytes = cmd->bufflen;
3075 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
3077 switch (se_cmd->prot_op) {
3078 case TARGET_PROT_DIN_INSERT:
3079 case TARGET_PROT_DOUT_STRIP:
3080 transfer_length = data_bytes;
3081 if (cmd->prot_sg_cnt)
3082 data_bytes += dif_bytes;
3083 break;
3084 case TARGET_PROT_DIN_STRIP:
3085 case TARGET_PROT_DOUT_INSERT:
3086 case TARGET_PROT_DIN_PASS:
3087 case TARGET_PROT_DOUT_PASS:
3088 transfer_length = data_bytes + dif_bytes;
3089 break;
3090 default:
3091 BUG();
3092 break;
3095 if (!qlt_hba_err_chk_enabled(se_cmd))
3096 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3097 /* HBA error checking enabled */
3098 else if (IS_PI_UNINIT_CAPABLE(ha)) {
3099 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3100 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3101 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3102 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3103 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3106 switch (se_cmd->prot_op) {
3107 case TARGET_PROT_DIN_INSERT:
3108 case TARGET_PROT_DOUT_INSERT:
3109 fw_prot_opts |= PO_MODE_DIF_INSERT;
3110 break;
3111 case TARGET_PROT_DIN_STRIP:
3112 case TARGET_PROT_DOUT_STRIP:
3113 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3114 break;
3115 case TARGET_PROT_DIN_PASS:
3116 case TARGET_PROT_DOUT_PASS:
3117 fw_prot_opts |= PO_MODE_DIF_PASS;
3118 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3119 break;
3120 default:/* Normal Request */
3121 fw_prot_opts |= PO_MODE_DIF_PASS;
3122 break;
3125 /* ---- PKT ---- */
3126 /* Update entry type to indicate Command Type CRC_2 IOCB */
3127 pkt->entry_type = CTIO_CRC2;
3128 pkt->entry_count = 1;
3129 pkt->vp_index = cmd->vp_idx;
3131 h = qlt_make_handle(qpair);
3132 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3134 * CTIO type 7 from the firmware doesn't provide a way to
3135 * know the initiator's LOOP ID, hence we can't find
3136 * the session and, so, the command.
3138 return -EAGAIN;
3139 } else
3140 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3142 pkt->handle = MAKE_HANDLE(qpair->req->id, h);
3143 pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3144 pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3145 pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3146 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3147 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3148 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3149 pkt->exchange_addr = atio->u.isp24.exchange_addr;
3151 /* silence compile warning */
3152 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3153 pkt->ox_id = cpu_to_le16(t16);
3155 t16 = (atio->u.isp24.attr << 9);
3156 pkt->flags |= cpu_to_le16(t16);
3157 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3159 /* Set transfer direction */
3160 if (cmd->dma_data_direction == DMA_TO_DEVICE)
3161 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3162 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3163 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3165 pkt->dseg_count = prm->tot_dsds;
3166 /* Fibre channel byte count */
3167 pkt->transfer_length = cpu_to_le32(transfer_length);
3169 /* ----- CRC context -------- */
3171 /* Allocate CRC context from global pool */
3172 crc_ctx_pkt = cmd->ctx =
3173 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3175 if (!crc_ctx_pkt)
3176 goto crc_queuing_error;
3178 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3179 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3181 /* Set handle */
3182 crc_ctx_pkt->handle = pkt->handle;
3184 qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3186 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
3187 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
3188 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
3190 if (!bundling) {
3191 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
3192 } else {
3194 * Configure Bundling if we need to fetch interlaving
3195 * protection PCI accesses
3197 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3198 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3199 crc_ctx_pkt->u.bundling.dseg_count =
3200 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3201 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
3204 /* Finish the common fields of CRC pkt */
3205 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
3206 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
3207 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3208 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3210 memset((uint8_t *)&tc, 0 , sizeof(tc));
3211 tc.vha = vha;
3212 tc.blk_sz = cmd->blk_sz;
3213 tc.bufflen = cmd->bufflen;
3214 tc.sg = cmd->sg;
3215 tc.prot_sg = cmd->prot_sg;
3216 tc.ctx = crc_ctx_pkt;
3217 tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3219 /* Walks data segments */
3220 pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3222 if (!bundling && prm->prot_seg_cnt) {
3223 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3224 prm->tot_dsds, &tc))
3225 goto crc_queuing_error;
3226 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3227 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3228 goto crc_queuing_error;
3230 if (bundling && prm->prot_seg_cnt) {
3231 /* Walks dif segments */
3232 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3234 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
3235 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3236 prm->prot_seg_cnt, cmd))
3237 goto crc_queuing_error;
3239 return QLA_SUCCESS;
3241 crc_queuing_error:
3242 /* Cleanup will be performed by the caller */
3243 qpair->req->outstanding_cmds[h] = NULL;
3245 return QLA_FUNCTION_FAILED;
3249 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3250 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3252 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3253 uint8_t scsi_status)
3255 struct scsi_qla_host *vha = cmd->vha;
3256 struct qla_qpair *qpair = cmd->qpair;
3257 struct ctio7_to_24xx *pkt;
3258 struct qla_tgt_prm prm;
3259 uint32_t full_req_cnt = 0;
3260 unsigned long flags = 0;
3261 int res;
3263 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3264 (cmd->sess && cmd->sess->deleted)) {
3265 cmd->state = QLA_TGT_STATE_PROCESSED;
3266 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3267 return 0;
3270 ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3271 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3272 (xmit_type & QLA_TGT_XMIT_STATUS) ?
3273 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3274 &cmd->se_cmd, qpair->id);
3276 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3277 &full_req_cnt);
3278 if (unlikely(res != 0)) {
3279 return res;
3282 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3284 if (xmit_type == QLA_TGT_XMIT_STATUS)
3285 qpair->tgt_counters.core_qla_snd_status++;
3286 else
3287 qpair->tgt_counters.core_qla_que_buf++;
3289 if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3291 * Either the port is not online or this request was from
3292 * previous life, just abort the processing.
3294 cmd->state = QLA_TGT_STATE_PROCESSED;
3295 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3296 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3297 "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3298 vha->flags.online, qla2x00_reset_active(vha),
3299 cmd->reset_count, qpair->chip_reset);
3300 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3301 return 0;
3304 /* Does F/W have an IOCBs for this request */
3305 res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3306 if (unlikely(res))
3307 goto out_unmap_unlock;
3309 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3310 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3311 else
3312 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3313 if (unlikely(res != 0)) {
3314 qpair->req->cnt += full_req_cnt;
3315 goto out_unmap_unlock;
3318 pkt = (struct ctio7_to_24xx *)prm.pkt;
3320 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3321 pkt->u.status0.flags |=
3322 cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3323 CTIO7_FLAGS_STATUS_MODE_0);
3325 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3326 qlt_load_data_segments(&prm);
3328 if (prm.add_status_pkt == 0) {
3329 if (xmit_type & QLA_TGT_XMIT_STATUS) {
3330 pkt->u.status0.scsi_status =
3331 cpu_to_le16(prm.rq_result);
3332 pkt->u.status0.residual =
3333 cpu_to_le32(prm.residual);
3334 pkt->u.status0.flags |= cpu_to_le16(
3335 CTIO7_FLAGS_SEND_STATUS);
3336 if (qlt_need_explicit_conf(cmd, 0)) {
3337 pkt->u.status0.flags |=
3338 cpu_to_le16(
3339 CTIO7_FLAGS_EXPLICIT_CONFORM |
3340 CTIO7_FLAGS_CONFORM_REQ);
3344 } else {
3346 * We have already made sure that there is sufficient
3347 * amount of request entries to not drop HW lock in
3348 * req_pkt().
3350 struct ctio7_to_24xx *ctio =
3351 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3352 qpair->req);
3354 ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3355 "Building additional status packet 0x%p.\n",
3356 ctio);
3359 * T10Dif: ctio_crc2_to_fw overlay ontop of
3360 * ctio7_to_24xx
3362 memcpy(ctio, pkt, sizeof(*ctio));
3363 /* reset back to CTIO7 */
3364 ctio->entry_count = 1;
3365 ctio->entry_type = CTIO_TYPE7;
3366 ctio->dseg_count = 0;
3367 ctio->u.status1.flags &= ~cpu_to_le16(
3368 CTIO7_FLAGS_DATA_IN);
3370 /* Real finish is ctio_m1's finish */
3371 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3372 pkt->u.status0.flags |= cpu_to_le16(
3373 CTIO7_FLAGS_DONT_RET_CTIO);
3375 /* qlt_24xx_init_ctio_to_isp will correct
3376 * all neccessary fields that's part of CTIO7.
3377 * There should be no residual of CTIO-CRC2 data.
3379 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3380 &prm);
3382 } else
3383 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3386 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3387 spin_lock(&cmd->cmd_lock);
3388 cmd->cmd_sent_to_fw = 1;
3389 spin_unlock(&cmd->cmd_lock);
3390 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3392 /* Memory Barrier */
3393 wmb();
3394 if (qpair->reqq_start_iocbs)
3395 qpair->reqq_start_iocbs(qpair);
3396 else
3397 qla2x00_start_iocbs(vha, qpair->req);
3398 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3400 return 0;
3402 out_unmap_unlock:
3403 qlt_unmap_sg(vha, cmd);
3404 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3406 return res;
3408 EXPORT_SYMBOL(qlt_xmit_response);
3410 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3412 struct ctio7_to_24xx *pkt;
3413 struct scsi_qla_host *vha = cmd->vha;
3414 struct qla_tgt *tgt = cmd->tgt;
3415 struct qla_tgt_prm prm;
3416 unsigned long flags = 0;
3417 int res = 0;
3418 struct qla_qpair *qpair = cmd->qpair;
3420 memset(&prm, 0, sizeof(prm));
3421 prm.cmd = cmd;
3422 prm.tgt = tgt;
3423 prm.sg = NULL;
3424 prm.req_cnt = 1;
3426 /* Calculate number of entries and segments required */
3427 if (qlt_pci_map_calc_cnt(&prm) != 0)
3428 return -EAGAIN;
3430 if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3431 (cmd->sess && cmd->sess->deleted)) {
3433 * Either the port is not online or this request was from
3434 * previous life, just abort the processing.
3436 cmd->state = QLA_TGT_STATE_NEED_DATA;
3437 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
3438 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3439 "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3440 vha->flags.online, qla2x00_reset_active(vha),
3441 cmd->reset_count, qpair->chip_reset);
3442 return 0;
3445 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3446 /* Does F/W have an IOCBs for this request */
3447 res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3448 if (res != 0)
3449 goto out_unlock_free_unmap;
3450 if (cmd->se_cmd.prot_op)
3451 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3452 else
3453 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3455 if (unlikely(res != 0)) {
3456 qpair->req->cnt += prm.req_cnt;
3457 goto out_unlock_free_unmap;
3460 pkt = (struct ctio7_to_24xx *)prm.pkt;
3461 pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3462 CTIO7_FLAGS_STATUS_MODE_0);
3464 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3465 qlt_load_data_segments(&prm);
3467 cmd->state = QLA_TGT_STATE_NEED_DATA;
3468 spin_lock(&cmd->cmd_lock);
3469 cmd->cmd_sent_to_fw = 1;
3470 spin_unlock(&cmd->cmd_lock);
3471 cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3473 /* Memory Barrier */
3474 wmb();
3475 if (qpair->reqq_start_iocbs)
3476 qpair->reqq_start_iocbs(qpair);
3477 else
3478 qla2x00_start_iocbs(vha, qpair->req);
3479 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3481 return res;
3483 out_unlock_free_unmap:
3484 qlt_unmap_sg(vha, cmd);
3485 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3487 return res;
3489 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3493 * it is assumed either hardware_lock or qpair lock is held.
3495 static void
3496 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3497 struct ctio_crc_from_fw *sts)
3499 uint8_t *ap = &sts->actual_dif[0];
3500 uint8_t *ep = &sts->expected_dif[0];
3501 uint64_t lba = cmd->se_cmd.t_task_lba;
3502 uint8_t scsi_status, sense_key, asc, ascq;
3503 unsigned long flags;
3504 struct scsi_qla_host *vha = cmd->vha;
3506 cmd->trc_flags |= TRC_DIF_ERR;
3508 cmd->a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
3509 cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
3510 cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3512 cmd->e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
3513 cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
3514 cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3516 ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3517 "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3519 scsi_status = sense_key = asc = ascq = 0;
3521 /* check appl tag */
3522 if (cmd->e_app_tag != cmd->a_app_tag) {
3523 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3524 "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3525 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3526 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3527 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3528 cmd->atio.u.isp24.fcp_hdr.ox_id);
3530 cmd->dif_err_code = DIF_ERR_APP;
3531 scsi_status = SAM_STAT_CHECK_CONDITION;
3532 sense_key = ABORTED_COMMAND;
3533 asc = 0x10;
3534 ascq = 0x2;
3537 /* check ref tag */
3538 if (cmd->e_ref_tag != cmd->a_ref_tag) {
3539 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3540 "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard[%x|%x] cmd=%p ox_id[%04x] ",
3541 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3542 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3543 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3544 cmd->atio.u.isp24.fcp_hdr.ox_id);
3546 cmd->dif_err_code = DIF_ERR_REF;
3547 scsi_status = SAM_STAT_CHECK_CONDITION;
3548 sense_key = ABORTED_COMMAND;
3549 asc = 0x10;
3550 ascq = 0x3;
3551 goto out;
3554 /* check guard */
3555 if (cmd->e_guard != cmd->a_guard) {
3556 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3557 "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3558 cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3559 cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3560 cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3561 cmd->atio.u.isp24.fcp_hdr.ox_id);
3563 cmd->dif_err_code = DIF_ERR_GRD;
3564 scsi_status = SAM_STAT_CHECK_CONDITION;
3565 sense_key = ABORTED_COMMAND;
3566 asc = 0x10;
3567 ascq = 0x1;
3569 out:
3570 switch (cmd->state) {
3571 case QLA_TGT_STATE_NEED_DATA:
3572 /* handle_data will load DIF error code */
3573 cmd->state = QLA_TGT_STATE_DATA_IN;
3574 vha->hw->tgt.tgt_ops->handle_data(cmd);
3575 break;
3576 default:
3577 spin_lock_irqsave(&cmd->cmd_lock, flags);
3578 if (cmd->aborted) {
3579 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3580 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3581 break;
3583 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3585 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3586 ascq);
3587 /* assume scsi status gets out on the wire.
3588 * Will not wait for completion.
3590 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3591 break;
3595 /* If hardware_lock held on entry, might drop it, then reaquire */
3596 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3597 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3598 struct imm_ntfy_from_isp *ntfy)
3600 struct nack_to_isp *nack;
3601 struct qla_hw_data *ha = vha->hw;
3602 request_t *pkt;
3603 int ret = 0;
3605 ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3606 "Sending TERM ELS CTIO (ha=%p)\n", ha);
3608 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3609 if (pkt == NULL) {
3610 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3611 "qla_target(%d): %s failed: unable to allocate "
3612 "request packet\n", vha->vp_idx, __func__);
3613 return -ENOMEM;
3616 pkt->entry_type = NOTIFY_ACK_TYPE;
3617 pkt->entry_count = 1;
3618 pkt->handle = QLA_TGT_SKIP_HANDLE;
3620 nack = (struct nack_to_isp *)pkt;
3621 nack->ox_id = ntfy->ox_id;
3623 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3624 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3625 nack->u.isp24.flags = ntfy->u.isp24.flags &
3626 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
3629 /* terminate */
3630 nack->u.isp24.flags |=
3631 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3633 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3634 nack->u.isp24.status = ntfy->u.isp24.status;
3635 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3636 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3637 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3638 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3639 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3640 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3642 qla2x00_start_iocbs(vha, vha->req);
3643 return ret;
3646 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3647 struct imm_ntfy_from_isp *imm, int ha_locked)
3649 unsigned long flags = 0;
3650 int rc;
3652 if (ha_locked) {
3653 rc = __qlt_send_term_imm_notif(vha, imm);
3655 #if 0 /* Todo */
3656 if (rc == -ENOMEM)
3657 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3658 #else
3659 if (rc) {
3661 #endif
3662 goto done;
3665 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
3666 rc = __qlt_send_term_imm_notif(vha, imm);
3668 #if 0 /* Todo */
3669 if (rc == -ENOMEM)
3670 qlt_alloc_qfull_cmd(vha, imm, 0, 0);
3671 #endif
3673 done:
3674 if (!ha_locked)
3675 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
3679 * If hardware_lock held on entry, might drop it, then reaquire
3680 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3682 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3683 struct qla_tgt_cmd *cmd,
3684 struct atio_from_isp *atio)
3686 struct scsi_qla_host *vha = qpair->vha;
3687 struct ctio7_to_24xx *ctio24;
3688 struct qla_hw_data *ha = vha->hw;
3689 request_t *pkt;
3690 int ret = 0;
3691 uint16_t temp;
3693 ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3695 if (cmd)
3696 vha = cmd->vha;
3698 pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3699 if (pkt == NULL) {
3700 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3701 "qla_target(%d): %s failed: unable to allocate "
3702 "request packet\n", vha->vp_idx, __func__);
3703 return -ENOMEM;
3706 if (cmd != NULL) {
3707 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3708 ql_dbg(ql_dbg_tgt, vha, 0xe051,
3709 "qla_target(%d): Terminating cmd %p with "
3710 "incorrect state %d\n", vha->vp_idx, cmd,
3711 cmd->state);
3712 } else
3713 ret = 1;
3716 qpair->tgt_counters.num_term_xchg_sent++;
3717 pkt->entry_count = 1;
3718 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3720 ctio24 = (struct ctio7_to_24xx *)pkt;
3721 ctio24->entry_type = CTIO_TYPE7;
3722 ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3723 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3724 ctio24->vp_index = vha->vp_idx;
3725 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
3726 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
3727 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
3728 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3729 temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3730 CTIO7_FLAGS_TERMINATE;
3731 ctio24->u.status1.flags = cpu_to_le16(temp);
3732 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3733 ctio24->u.status1.ox_id = cpu_to_le16(temp);
3735 /* Memory Barrier */
3736 wmb();
3737 if (qpair->reqq_start_iocbs)
3738 qpair->reqq_start_iocbs(qpair);
3739 else
3740 qla2x00_start_iocbs(vha, qpair->req);
3741 return ret;
3744 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3745 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3746 int ul_abort)
3748 struct scsi_qla_host *vha;
3749 unsigned long flags = 0;
3750 int rc;
3752 /* why use different vha? NPIV */
3753 if (cmd)
3754 vha = cmd->vha;
3755 else
3756 vha = qpair->vha;
3758 if (ha_locked) {
3759 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3760 if (rc == -ENOMEM)
3761 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3762 goto done;
3764 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3765 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3766 if (rc == -ENOMEM)
3767 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3769 done:
3770 if (cmd && !ul_abort && !cmd->aborted) {
3771 if (cmd->sg_mapped)
3772 qlt_unmap_sg(vha, cmd);
3773 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3776 if (!ha_locked)
3777 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3779 return;
3782 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3784 struct list_head free_list;
3785 struct qla_tgt_cmd *cmd, *tcmd;
3787 vha->hw->tgt.leak_exchg_thresh_hold =
3788 (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3790 cmd = tcmd = NULL;
3791 if (!list_empty(&vha->hw->tgt.q_full_list)) {
3792 INIT_LIST_HEAD(&free_list);
3793 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3795 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3796 list_del(&cmd->cmd_list);
3797 /* This cmd was never sent to TCM. There is no need
3798 * to schedule free or call free_cmd
3800 qlt_free_cmd(cmd);
3801 vha->hw->tgt.num_qfull_cmds_alloc--;
3804 vha->hw->tgt.num_qfull_cmds_dropped = 0;
3807 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3809 uint32_t total_leaked;
3811 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3813 if (vha->hw->tgt.leak_exchg_thresh_hold &&
3814 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3816 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3817 "Chip reset due to exchange starvation: %d/%d.\n",
3818 total_leaked, vha->hw->cur_fw_xcb_count);
3820 if (IS_P3P_TYPE(vha->hw))
3821 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3822 else
3823 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3824 qla2xxx_wake_dpc(vha);
3829 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3831 struct qla_tgt *tgt = cmd->tgt;
3832 struct scsi_qla_host *vha = tgt->vha;
3833 struct se_cmd *se_cmd = &cmd->se_cmd;
3834 unsigned long flags;
3836 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3837 "qla_target(%d): terminating exchange for aborted cmd=%p "
3838 "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3839 se_cmd->tag);
3841 spin_lock_irqsave(&cmd->cmd_lock, flags);
3842 if (cmd->aborted) {
3843 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3845 * It's normal to see 2 calls in this path:
3846 * 1) XFER Rdy completion + CMD_T_ABORT
3847 * 2) TCM TMR - drain_state_list
3849 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3850 "multiple abort. %p transport_state %x, t_state %x, "
3851 "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3852 cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3853 return EIO;
3855 cmd->aborted = 1;
3856 cmd->trc_flags |= TRC_ABORT;
3857 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3859 qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3860 return 0;
3862 EXPORT_SYMBOL(qlt_abort_cmd);
3864 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3866 struct fc_port *sess = cmd->sess;
3868 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3869 "%s: se_cmd[%p] ox_id %04x\n",
3870 __func__, &cmd->se_cmd,
3871 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3873 BUG_ON(cmd->cmd_in_wq);
3875 if (cmd->sg_mapped)
3876 qlt_unmap_sg(cmd->vha, cmd);
3878 if (!cmd->q_full)
3879 qlt_decr_num_pend_cmds(cmd->vha);
3881 BUG_ON(cmd->sg_mapped);
3882 cmd->jiffies_at_free = get_jiffies_64();
3883 if (unlikely(cmd->free_sg))
3884 kfree(cmd->sg);
3886 if (!sess || !sess->se_sess) {
3887 WARN_ON(1);
3888 return;
3890 cmd->jiffies_at_free = get_jiffies_64();
3891 target_free_tag(sess->se_sess, &cmd->se_cmd);
3893 EXPORT_SYMBOL(qlt_free_cmd);
3896 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3898 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3899 struct qla_tgt_cmd *cmd, uint32_t status)
3901 int term = 0;
3902 struct scsi_qla_host *vha = qpair->vha;
3904 if (cmd->se_cmd.prot_op)
3905 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3906 "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3907 "se_cmd=%p tag[%x] op %#x/%s",
3908 cmd->lba, cmd->lba,
3909 cmd->num_blks, &cmd->se_cmd,
3910 cmd->atio.u.isp24.exchange_addr,
3911 cmd->se_cmd.prot_op,
3912 prot_op_str(cmd->se_cmd.prot_op));
3914 if (ctio != NULL) {
3915 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3916 term = !(c->flags &
3917 cpu_to_le16(OF_TERM_EXCH));
3918 } else
3919 term = 1;
3921 if (term)
3922 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3924 return term;
3928 /* ha->hardware_lock supposed to be held on entry */
3929 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3930 struct rsp_que *rsp, uint32_t handle, void *ctio)
3932 void *cmd = NULL;
3933 struct req_que *req;
3934 int qid = GET_QID(handle);
3935 uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3937 if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3938 return NULL;
3940 if (qid == rsp->req->id) {
3941 req = rsp->req;
3942 } else if (vha->hw->req_q_map[qid]) {
3943 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3944 "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3945 vha->vp_idx, rsp->id, handle);
3946 req = vha->hw->req_q_map[qid];
3947 } else {
3948 return NULL;
3951 h &= QLA_CMD_HANDLE_MASK;
3953 if (h != QLA_TGT_NULL_HANDLE) {
3954 if (unlikely(h >= req->num_outstanding_cmds)) {
3955 ql_dbg(ql_dbg_tgt, vha, 0xe052,
3956 "qla_target(%d): Wrong handle %x received\n",
3957 vha->vp_idx, handle);
3958 return NULL;
3961 cmd = (void *) req->outstanding_cmds[h];
3962 if (unlikely(cmd == NULL)) {
3963 ql_dbg(ql_dbg_async, vha, 0xe053,
3964 "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3965 vha->vp_idx, handle, req->id, rsp->id);
3966 return NULL;
3968 req->outstanding_cmds[h] = NULL;
3969 } else if (ctio != NULL) {
3970 /* We can't get loop ID from CTIO7 */
3971 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3972 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3973 "support NULL handles\n", vha->vp_idx);
3974 return NULL;
3977 return cmd;
3980 /* hardware_lock should be held by caller. */
3981 void
3982 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
3984 struct qla_hw_data *ha = vha->hw;
3986 if (cmd->sg_mapped)
3987 qlt_unmap_sg(vha, cmd);
3989 /* TODO: fix debug message type and ids. */
3990 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3991 ql_dbg(ql_dbg_io, vha, 0xff00,
3992 "HOST-ABORT: state=PROCESSED.\n");
3993 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3994 cmd->write_data_transferred = 0;
3995 cmd->state = QLA_TGT_STATE_DATA_IN;
3997 ql_dbg(ql_dbg_io, vha, 0xff01,
3998 "HOST-ABORT: state=DATA_IN.\n");
4000 ha->tgt.tgt_ops->handle_data(cmd);
4001 return;
4002 } else {
4003 ql_dbg(ql_dbg_io, vha, 0xff03,
4004 "HOST-ABORT: state=BAD(%d).\n",
4005 cmd->state);
4006 dump_stack();
4009 cmd->trc_flags |= TRC_FLUSH;
4010 ha->tgt.tgt_ops->free_cmd(cmd);
4014 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4016 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
4017 struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
4019 struct qla_hw_data *ha = vha->hw;
4020 struct se_cmd *se_cmd;
4021 struct qla_tgt_cmd *cmd;
4022 struct qla_qpair *qpair = rsp->qpair;
4024 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
4025 /* That could happen only in case of an error/reset/abort */
4026 if (status != CTIO_SUCCESS) {
4027 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
4028 "Intermediate CTIO received"
4029 " (status %x)\n", status);
4031 return;
4034 cmd = (struct qla_tgt_cmd *)qlt_ctio_to_cmd(vha, rsp, handle, ctio);
4035 if (cmd == NULL)
4036 return;
4038 se_cmd = &cmd->se_cmd;
4039 cmd->cmd_sent_to_fw = 0;
4041 qlt_unmap_sg(vha, cmd);
4043 if (unlikely(status != CTIO_SUCCESS)) {
4044 switch (status & 0xFFFF) {
4045 case CTIO_INVALID_RX_ID:
4046 if (printk_ratelimit())
4047 dev_info(&vha->hw->pdev->dev,
4048 "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4049 vha->vp_idx, cmd->atio.u.isp24.attr,
4050 ((cmd->ctio_flags >> 9) & 0xf),
4051 cmd->ctio_flags);
4053 break;
4054 case CTIO_LIP_RESET:
4055 case CTIO_TARGET_RESET:
4056 case CTIO_ABORTED:
4057 /* driver request abort via Terminate exchange */
4058 case CTIO_TIMEOUT:
4059 /* They are OK */
4060 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4061 "qla_target(%d): CTIO with "
4062 "status %#x received, state %x, se_cmd %p, "
4063 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4064 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4065 status, cmd->state, se_cmd);
4066 break;
4068 case CTIO_PORT_LOGGED_OUT:
4069 case CTIO_PORT_UNAVAILABLE:
4071 int logged_out =
4072 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4074 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4075 "qla_target(%d): CTIO with %s status %x "
4076 "received (state %x, se_cmd %p)\n", vha->vp_idx,
4077 logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4078 status, cmd->state, se_cmd);
4080 if (logged_out && cmd->sess) {
4082 * Session is already logged out, but we need
4083 * to notify initiator, who's not aware of this
4085 cmd->sess->send_els_logo = 1;
4086 ql_dbg(ql_dbg_disc, vha, 0x20f8,
4087 "%s %d %8phC post del sess\n",
4088 __func__, __LINE__, cmd->sess->port_name);
4090 qlt_schedule_sess_for_deletion(cmd->sess);
4092 break;
4094 case CTIO_DIF_ERROR: {
4095 struct ctio_crc_from_fw *crc =
4096 (struct ctio_crc_from_fw *)ctio;
4097 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4098 "qla_target(%d): CTIO with DIF_ERROR status %x "
4099 "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4100 "expect_dif[0x%llx]\n",
4101 vha->vp_idx, status, cmd->state, se_cmd,
4102 *((u64 *)&crc->actual_dif[0]),
4103 *((u64 *)&crc->expected_dif[0]));
4105 qlt_handle_dif_error(qpair, cmd, ctio);
4106 return;
4108 default:
4109 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4110 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4111 vha->vp_idx, status, cmd->state, se_cmd);
4112 break;
4116 /* "cmd->aborted" means
4117 * cmd is already aborted/terminated, we don't
4118 * need to terminate again. The exchange is already
4119 * cleaned up/freed at FW level. Just cleanup at driver
4120 * level.
4122 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4123 (!cmd->aborted)) {
4124 cmd->trc_flags |= TRC_CTIO_ERR;
4125 if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4126 return;
4130 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4131 cmd->trc_flags |= TRC_CTIO_DONE;
4132 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4133 cmd->state = QLA_TGT_STATE_DATA_IN;
4135 if (status == CTIO_SUCCESS)
4136 cmd->write_data_transferred = 1;
4138 ha->tgt.tgt_ops->handle_data(cmd);
4139 return;
4140 } else if (cmd->aborted) {
4141 cmd->trc_flags |= TRC_CTIO_ABORTED;
4142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4143 "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4144 } else {
4145 cmd->trc_flags |= TRC_CTIO_STRANGE;
4146 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4147 "qla_target(%d): A command in state (%d) should "
4148 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4151 if (unlikely(status != CTIO_SUCCESS) &&
4152 !cmd->aborted) {
4153 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4154 dump_stack();
4157 ha->tgt.tgt_ops->free_cmd(cmd);
4160 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4161 uint8_t task_codes)
4163 int fcp_task_attr;
4165 switch (task_codes) {
4166 case ATIO_SIMPLE_QUEUE:
4167 fcp_task_attr = TCM_SIMPLE_TAG;
4168 break;
4169 case ATIO_HEAD_OF_QUEUE:
4170 fcp_task_attr = TCM_HEAD_TAG;
4171 break;
4172 case ATIO_ORDERED_QUEUE:
4173 fcp_task_attr = TCM_ORDERED_TAG;
4174 break;
4175 case ATIO_ACA_QUEUE:
4176 fcp_task_attr = TCM_ACA_TAG;
4177 break;
4178 case ATIO_UNTAGGED:
4179 fcp_task_attr = TCM_SIMPLE_TAG;
4180 break;
4181 default:
4182 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4183 "qla_target: unknown task code %x, use ORDERED instead\n",
4184 task_codes);
4185 fcp_task_attr = TCM_ORDERED_TAG;
4186 break;
4189 return fcp_task_attr;
4192 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *,
4193 uint8_t *);
4195 * Process context for I/O path into tcm_qla2xxx code
4197 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4199 scsi_qla_host_t *vha = cmd->vha;
4200 struct qla_hw_data *ha = vha->hw;
4201 struct fc_port *sess = cmd->sess;
4202 struct atio_from_isp *atio = &cmd->atio;
4203 unsigned char *cdb;
4204 unsigned long flags;
4205 uint32_t data_length;
4206 int ret, fcp_task_attr, data_dir, bidi = 0;
4207 struct qla_qpair *qpair = cmd->qpair;
4209 cmd->cmd_in_wq = 0;
4210 cmd->trc_flags |= TRC_DO_WORK;
4212 if (cmd->aborted) {
4213 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4214 "cmd with tag %u is aborted\n",
4215 cmd->atio.u.isp24.exchange_addr);
4216 goto out_term;
4219 spin_lock_init(&cmd->cmd_lock);
4220 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4221 cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4223 if (atio->u.isp24.fcp_cmnd.rddata &&
4224 atio->u.isp24.fcp_cmnd.wrdata) {
4225 bidi = 1;
4226 data_dir = DMA_TO_DEVICE;
4227 } else if (atio->u.isp24.fcp_cmnd.rddata)
4228 data_dir = DMA_FROM_DEVICE;
4229 else if (atio->u.isp24.fcp_cmnd.wrdata)
4230 data_dir = DMA_TO_DEVICE;
4231 else
4232 data_dir = DMA_NONE;
4234 fcp_task_attr = qlt_get_fcp_task_attr(vha,
4235 atio->u.isp24.fcp_cmnd.task_attr);
4236 data_length = get_datalen_for_atio(atio);
4238 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4239 fcp_task_attr, data_dir, bidi);
4240 if (ret != 0)
4241 goto out_term;
4243 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
4245 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4246 ha->tgt.tgt_ops->put_sess(sess);
4247 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4248 return;
4250 out_term:
4251 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4253 * cmd has not sent to target yet, so pass NULL as the second
4254 * argument to qlt_send_term_exchange() and free the memory here.
4256 cmd->trc_flags |= TRC_DO_WORK_ERR;
4257 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4258 qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4260 qlt_decr_num_pend_cmds(vha);
4261 target_free_tag(sess->se_sess, &cmd->se_cmd);
4262 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4264 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4265 ha->tgt.tgt_ops->put_sess(sess);
4266 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4269 static void qlt_do_work(struct work_struct *work)
4271 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4272 scsi_qla_host_t *vha = cmd->vha;
4273 unsigned long flags;
4275 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4276 list_del(&cmd->cmd_list);
4277 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4279 __qlt_do_work(cmd);
4282 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4284 unsigned long flags;
4285 struct qla_hw_data *ha = vha->hw;
4286 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4287 void *node;
4288 u64 key = 0;
4290 ql_log(ql_log_info, vha, 0x706c,
4291 "User update Number of Active Qpairs %d\n",
4292 ha->tgt.num_act_qpairs);
4294 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4296 btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4297 btree_remove64(&tgt->lun_qpair_map, key);
4299 ha->base_qpair->lun_cnt = 0;
4300 for (key = 0; key < ha->max_qpairs; key++)
4301 if (ha->queue_pair_map[key])
4302 ha->queue_pair_map[key]->lun_cnt = 0;
4304 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4307 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4308 struct qla_tgt_cmd *cmd)
4310 struct qla_qpair *qpair, *qp;
4311 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4312 struct qla_qpair_hint *h;
4314 if (vha->flags.qpairs_available) {
4315 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4316 if (unlikely(!h)) {
4317 /* spread lun to qpair ratio evently */
4318 int lcnt = 0, rc;
4319 struct scsi_qla_host *base_vha =
4320 pci_get_drvdata(vha->hw->pdev);
4322 qpair = vha->hw->base_qpair;
4323 if (qpair->lun_cnt == 0) {
4324 qpair->lun_cnt++;
4325 h = qla_qpair_to_hint(tgt, qpair);
4326 BUG_ON(!h);
4327 rc = btree_insert64(&tgt->lun_qpair_map,
4328 cmd->unpacked_lun, h, GFP_ATOMIC);
4329 if (rc) {
4330 qpair->lun_cnt--;
4331 ql_log(ql_log_info, vha, 0xd037,
4332 "Unable to insert lun %llx into lun_qpair_map\n",
4333 cmd->unpacked_lun);
4335 goto out;
4336 } else {
4337 lcnt = qpair->lun_cnt;
4340 h = NULL;
4341 list_for_each_entry(qp, &base_vha->qp_list,
4342 qp_list_elem) {
4343 if (qp->lun_cnt == 0) {
4344 qp->lun_cnt++;
4345 h = qla_qpair_to_hint(tgt, qp);
4346 BUG_ON(!h);
4347 rc = btree_insert64(&tgt->lun_qpair_map,
4348 cmd->unpacked_lun, h, GFP_ATOMIC);
4349 if (rc) {
4350 qp->lun_cnt--;
4351 ql_log(ql_log_info, vha, 0xd038,
4352 "Unable to insert lun %llx into lun_qpair_map\n",
4353 cmd->unpacked_lun);
4355 qpair = qp;
4356 goto out;
4357 } else {
4358 if (qp->lun_cnt < lcnt) {
4359 lcnt = qp->lun_cnt;
4360 qpair = qp;
4361 continue;
4365 BUG_ON(!qpair);
4366 qpair->lun_cnt++;
4367 h = qla_qpair_to_hint(tgt, qpair);
4368 BUG_ON(!h);
4369 rc = btree_insert64(&tgt->lun_qpair_map,
4370 cmd->unpacked_lun, h, GFP_ATOMIC);
4371 if (rc) {
4372 qpair->lun_cnt--;
4373 ql_log(ql_log_info, vha, 0xd039,
4374 "Unable to insert lun %llx into lun_qpair_map\n",
4375 cmd->unpacked_lun);
4378 } else {
4379 h = &tgt->qphints[0];
4381 out:
4382 cmd->qpair = h->qpair;
4383 cmd->se_cmd.cpuid = h->cpuid;
4386 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4387 struct fc_port *sess,
4388 struct atio_from_isp *atio)
4390 struct se_session *se_sess = sess->se_sess;
4391 struct qla_tgt_cmd *cmd;
4392 int tag, cpu;
4394 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
4395 if (tag < 0)
4396 return NULL;
4398 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4399 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4400 cmd->cmd_type = TYPE_TGT_CMD;
4401 memcpy(&cmd->atio, atio, sizeof(*atio));
4402 cmd->state = QLA_TGT_STATE_NEW;
4403 cmd->tgt = vha->vha_tgt.qla_tgt;
4404 qlt_incr_num_pend_cmds(vha);
4405 cmd->vha = vha;
4406 cmd->se_cmd.map_tag = tag;
4407 cmd->se_cmd.map_cpu = cpu;
4408 cmd->sess = sess;
4409 cmd->loop_id = sess->loop_id;
4410 cmd->conf_compl_supported = sess->conf_compl_supported;
4412 cmd->trc_flags = 0;
4413 cmd->jiffies_at_alloc = get_jiffies_64();
4415 cmd->unpacked_lun = scsilun_to_int(
4416 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4417 qlt_assign_qpair(vha, cmd);
4418 cmd->reset_count = vha->hw->base_qpair->chip_reset;
4419 cmd->vp_idx = vha->vp_idx;
4421 return cmd;
4424 /* ha->hardware_lock supposed to be held on entry */
4425 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4426 struct atio_from_isp *atio)
4428 struct qla_hw_data *ha = vha->hw;
4429 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4430 struct fc_port *sess;
4431 struct qla_tgt_cmd *cmd;
4432 unsigned long flags;
4433 port_id_t id;
4435 if (unlikely(tgt->tgt_stop)) {
4436 ql_dbg(ql_dbg_io, vha, 0x3061,
4437 "New command while device %p is shutting down\n", tgt);
4438 return -ENODEV;
4441 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
4442 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
4443 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
4444 if (IS_SW_RESV_ADDR(id))
4445 return -EBUSY;
4447 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4448 if (unlikely(!sess))
4449 return -EFAULT;
4451 /* Another WWN used to have our s_id. Our PLOGI scheduled its
4452 * session deletion, but it's still in sess_del_work wq */
4453 if (sess->deleted) {
4454 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4455 "New command while old session %p is being deleted\n",
4456 sess);
4457 return -EFAULT;
4461 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4463 if (!kref_get_unless_zero(&sess->sess_kref)) {
4464 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4465 "%s: kref_get fail, %8phC oxid %x \n",
4466 __func__, sess->port_name,
4467 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4468 return -EFAULT;
4471 cmd = qlt_get_tag(vha, sess, atio);
4472 if (!cmd) {
4473 ql_dbg(ql_dbg_io, vha, 0x3062,
4474 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4475 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4476 ha->tgt.tgt_ops->put_sess(sess);
4477 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4478 return -EBUSY;
4481 cmd->cmd_in_wq = 1;
4482 cmd->trc_flags |= TRC_NEW_CMD;
4484 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4485 list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4486 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4488 INIT_WORK(&cmd->work, qlt_do_work);
4489 if (vha->flags.qpairs_available) {
4490 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4491 } else if (ha->msix_count) {
4492 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4493 queue_work_on(smp_processor_id(), qla_tgt_wq,
4494 &cmd->work);
4495 else
4496 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4497 &cmd->work);
4498 } else {
4499 queue_work(qla_tgt_wq, &cmd->work);
4502 return 0;
4505 /* ha->hardware_lock supposed to be held on entry */
4506 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4507 int fn, void *iocb, int flags)
4509 struct scsi_qla_host *vha = sess->vha;
4510 struct qla_hw_data *ha = vha->hw;
4511 struct qla_tgt_mgmt_cmd *mcmd;
4512 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4513 struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4515 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4516 if (!mcmd) {
4517 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4518 "qla_target(%d): Allocation of management "
4519 "command failed, some commands and their data could "
4520 "leak\n", vha->vp_idx);
4521 return -ENOMEM;
4523 memset(mcmd, 0, sizeof(*mcmd));
4524 mcmd->sess = sess;
4526 if (iocb) {
4527 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4528 sizeof(mcmd->orig_iocb.imm_ntfy));
4530 mcmd->tmr_func = fn;
4531 mcmd->flags = flags;
4532 mcmd->reset_count = ha->base_qpair->chip_reset;
4533 mcmd->qpair = h->qpair;
4534 mcmd->vha = vha;
4535 mcmd->se_cmd.cpuid = h->cpuid;
4536 mcmd->unpacked_lun = lun;
4538 switch (fn) {
4539 case QLA_TGT_LUN_RESET:
4540 case QLA_TGT_CLEAR_TS:
4541 case QLA_TGT_ABORT_TS:
4542 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4543 /* fall through */
4544 case QLA_TGT_CLEAR_ACA:
4545 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4546 mcmd->qpair = h->qpair;
4547 mcmd->se_cmd.cpuid = h->cpuid;
4548 break;
4550 case QLA_TGT_TARGET_RESET:
4551 case QLA_TGT_NEXUS_LOSS_SESS:
4552 case QLA_TGT_NEXUS_LOSS:
4553 case QLA_TGT_ABORT_ALL:
4554 default:
4555 /* no-op */
4556 break;
4559 INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4560 queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4561 &mcmd->work);
4563 return 0;
4566 /* ha->hardware_lock supposed to be held on entry */
4567 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4569 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4570 struct qla_hw_data *ha = vha->hw;
4571 struct fc_port *sess;
4572 u64 unpacked_lun;
4573 int fn;
4574 unsigned long flags;
4576 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4578 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4579 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4580 a->u.isp24.fcp_hdr.s_id);
4581 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4583 unpacked_lun =
4584 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4586 if (sess == NULL || sess->deleted)
4587 return -EFAULT;
4589 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4592 /* ha->hardware_lock supposed to be held on entry */
4593 static int __qlt_abort_task(struct scsi_qla_host *vha,
4594 struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4596 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4597 struct qla_hw_data *ha = vha->hw;
4598 struct qla_tgt_mgmt_cmd *mcmd;
4599 u64 unpacked_lun;
4600 int rc;
4602 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4603 if (mcmd == NULL) {
4604 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4605 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4606 vha->vp_idx, __func__);
4607 return -ENOMEM;
4609 memset(mcmd, 0, sizeof(*mcmd));
4611 mcmd->sess = sess;
4612 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4613 sizeof(mcmd->orig_iocb.imm_ntfy));
4615 unpacked_lun =
4616 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4617 mcmd->reset_count = ha->base_qpair->chip_reset;
4618 mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4619 mcmd->qpair = ha->base_qpair;
4621 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4622 le16_to_cpu(iocb->u.isp2x.seq_id));
4623 if (rc != 0) {
4624 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4625 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4626 vha->vp_idx, rc);
4627 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4628 return -EFAULT;
4631 return 0;
4634 /* ha->hardware_lock supposed to be held on entry */
4635 static int qlt_abort_task(struct scsi_qla_host *vha,
4636 struct imm_ntfy_from_isp *iocb)
4638 struct qla_hw_data *ha = vha->hw;
4639 struct fc_port *sess;
4640 int loop_id;
4641 unsigned long flags;
4643 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4645 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4646 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4647 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4649 if (sess == NULL) {
4650 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4651 "qla_target(%d): task abort for unexisting "
4652 "session\n", vha->vp_idx);
4653 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4654 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4657 return __qlt_abort_task(vha, iocb, sess);
4660 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4662 if (rc != MBS_COMMAND_COMPLETE) {
4663 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4664 "%s: se_sess %p / sess %p from"
4665 " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4666 " LOGO failed: %#x\n",
4667 __func__,
4668 fcport->se_sess,
4669 fcport,
4670 fcport->port_name, fcport->loop_id,
4671 fcport->d_id.b.domain, fcport->d_id.b.area,
4672 fcport->d_id.b.al_pa, rc);
4675 fcport->logout_completed = 1;
4679 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4681 * Schedules sessions with matching port_id/loop_id but different wwn for
4682 * deletion. Returns existing session with matching wwn if present.
4683 * Null otherwise.
4685 struct fc_port *
4686 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4687 port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4689 struct fc_port *sess = NULL, *other_sess;
4690 uint64_t other_wwn;
4692 *conflict_sess = NULL;
4694 list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4696 other_wwn = wwn_to_u64(other_sess->port_name);
4698 if (wwn == other_wwn) {
4699 WARN_ON(sess);
4700 sess = other_sess;
4701 continue;
4704 /* find other sess with nport_id collision */
4705 if (port_id.b24 == other_sess->d_id.b24) {
4706 if (loop_id != other_sess->loop_id) {
4707 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4708 "Invalidating sess %p loop_id %d wwn %llx.\n",
4709 other_sess, other_sess->loop_id, other_wwn);
4712 * logout_on_delete is set by default, but another
4713 * session that has the same s_id/loop_id combo
4714 * might have cleared it when requested this session
4715 * deletion, so don't touch it
4717 qlt_schedule_sess_for_deletion(other_sess);
4718 } else {
4720 * Another wwn used to have our s_id/loop_id
4721 * kill the session, but don't free the loop_id
4723 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4724 "Invalidating sess %p loop_id %d wwn %llx.\n",
4725 other_sess, other_sess->loop_id, other_wwn);
4727 other_sess->keep_nport_handle = 1;
4728 if (other_sess->disc_state != DSC_DELETED)
4729 *conflict_sess = other_sess;
4730 qlt_schedule_sess_for_deletion(other_sess);
4732 continue;
4735 /* find other sess with nport handle collision */
4736 if ((loop_id == other_sess->loop_id) &&
4737 (loop_id != FC_NO_LOOP_ID)) {
4738 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4739 "Invalidating sess %p loop_id %d wwn %llx.\n",
4740 other_sess, other_sess->loop_id, other_wwn);
4742 /* Same loop_id but different s_id
4743 * Ok to kill and logout */
4744 qlt_schedule_sess_for_deletion(other_sess);
4748 return sess;
4751 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4752 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4754 struct qla_tgt_sess_op *op;
4755 struct qla_tgt_cmd *cmd;
4756 uint32_t key;
4757 int count = 0;
4758 unsigned long flags;
4760 key = (((u32)s_id->b.domain << 16) |
4761 ((u32)s_id->b.area << 8) |
4762 ((u32)s_id->b.al_pa));
4764 spin_lock_irqsave(&vha->cmd_list_lock, flags);
4765 list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4766 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4768 if (op_key == key) {
4769 op->aborted = true;
4770 count++;
4774 list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4775 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4776 if (op_key == key) {
4777 op->aborted = true;
4778 count++;
4782 list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4783 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4784 if (cmd_key == key) {
4785 cmd->aborted = 1;
4786 count++;
4789 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4791 return count;
4794 static int qlt_handle_login(struct scsi_qla_host *vha,
4795 struct imm_ntfy_from_isp *iocb)
4797 struct fc_port *sess = NULL, *conflict_sess = NULL;
4798 uint64_t wwn;
4799 port_id_t port_id;
4800 uint16_t loop_id, wd3_lo;
4801 int res = 0;
4802 struct qlt_plogi_ack_t *pla;
4803 unsigned long flags;
4805 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4807 port_id.b.domain = iocb->u.isp24.port_id[2];
4808 port_id.b.area = iocb->u.isp24.port_id[1];
4809 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4810 port_id.b.rsvd_1 = 0;
4812 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4814 /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4815 abort_cmds_for_s_id(vha, &port_id);
4817 if (wwn) {
4818 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4819 sess = qlt_find_sess_invalidate_other(vha, wwn,
4820 port_id, loop_id, &conflict_sess);
4821 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4822 } else {
4823 ql_dbg(ql_dbg_disc, vha, 0xffff,
4824 "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4825 __func__, __LINE__, loop_id, port_id.b24);
4826 qlt_send_term_imm_notif(vha, iocb, 1);
4827 goto out;
4830 if (IS_SW_RESV_ADDR(port_id)) {
4831 res = 1;
4832 goto out;
4835 pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4836 if (!pla) {
4837 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4838 "%s %d %8phC Term INOT due to mem alloc fail",
4839 __func__, __LINE__,
4840 iocb->u.isp24.port_name);
4841 qlt_send_term_imm_notif(vha, iocb, 1);
4842 goto out;
4845 if (conflict_sess) {
4846 conflict_sess->login_gen++;
4847 qlt_plogi_ack_link(vha, pla, conflict_sess,
4848 QLT_PLOGI_LINK_CONFLICT);
4851 if (!sess) {
4852 pla->ref_count++;
4853 ql_dbg(ql_dbg_disc, vha, 0xffff,
4854 "%s %d %8phC post new sess\n",
4855 __func__, __LINE__, iocb->u.isp24.port_name);
4856 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4857 qla24xx_post_newsess_work(vha, &port_id,
4858 iocb->u.isp24.port_name,
4859 iocb->u.isp24.u.plogi.node_name,
4860 pla, FC4_TYPE_UNKNOWN);
4861 else
4862 qla24xx_post_newsess_work(vha, &port_id,
4863 iocb->u.isp24.port_name, NULL,
4864 pla, FC4_TYPE_UNKNOWN);
4866 goto out;
4869 if (sess->disc_state == DSC_UPD_FCPORT) {
4870 u16 sec;
4873 * Remote port registration is still going on from
4874 * previous login. Allow it to finish before we
4875 * accept the new login.
4877 sess->next_disc_state = DSC_DELETE_PEND;
4878 sec = jiffies_to_msecs(jiffies -
4879 sess->jiffies_at_registration) / 1000;
4880 if (sess->sec_since_registration < sec && sec &&
4881 !(sec % 5)) {
4882 sess->sec_since_registration = sec;
4883 ql_dbg(ql_dbg_disc, vha, 0xffff,
4884 "%s %8phC - Slow Rport registration (%d Sec)\n",
4885 __func__, sess->port_name, sec);
4888 if (!conflict_sess)
4889 kmem_cache_free(qla_tgt_plogi_cachep, pla);
4891 qlt_send_term_imm_notif(vha, iocb, 1);
4892 goto out;
4895 qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4896 sess->d_id = port_id;
4897 sess->login_gen++;
4899 if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4900 sess->fw_login_state = DSC_LS_PRLI_PEND;
4901 sess->local = 0;
4902 sess->loop_id = loop_id;
4903 sess->d_id = port_id;
4904 sess->fw_login_state = DSC_LS_PRLI_PEND;
4905 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4907 if (wd3_lo & BIT_7)
4908 sess->conf_compl_supported = 1;
4910 if ((wd3_lo & BIT_4) == 0)
4911 sess->port_type = FCT_INITIATOR;
4912 else
4913 sess->port_type = FCT_TARGET;
4915 } else
4916 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4919 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4920 "%s %d %8phC DS %d\n",
4921 __func__, __LINE__, sess->port_name, sess->disc_state);
4923 switch (sess->disc_state) {
4924 case DSC_DELETED:
4925 qlt_plogi_ack_unref(vha, pla);
4926 break;
4928 default:
4930 * Under normal circumstances we want to release nport handle
4931 * during LOGO process to avoid nport handle leaks inside FW.
4932 * The exception is when LOGO is done while another PLOGI with
4933 * the same nport handle is waiting as might be the case here.
4934 * Note: there is always a possibily of a race where session
4935 * deletion has already started for other reasons (e.g. ACL
4936 * removal) and now PLOGI arrives:
4937 * 1. if PLOGI arrived in FW after nport handle has been freed,
4938 * FW must have assigned this PLOGI a new/same handle and we
4939 * can proceed ACK'ing it as usual when session deletion
4940 * completes.
4941 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4942 * bit reached it, the handle has now been released. We'll
4943 * get an error when we ACK this PLOGI. Nothing will be sent
4944 * back to initiator. Initiator should eventually retry
4945 * PLOGI and situation will correct itself.
4947 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4948 (sess->d_id.b24 == port_id.b24));
4950 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4951 "%s %d %8phC post del sess\n",
4952 __func__, __LINE__, sess->port_name);
4955 qlt_schedule_sess_for_deletion(sess);
4956 break;
4958 out:
4959 return res;
4963 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4965 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4966 struct imm_ntfy_from_isp *iocb)
4968 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4969 struct qla_hw_data *ha = vha->hw;
4970 struct fc_port *sess = NULL, *conflict_sess = NULL;
4971 uint64_t wwn;
4972 port_id_t port_id;
4973 uint16_t loop_id;
4974 uint16_t wd3_lo;
4975 int res = 0;
4976 unsigned long flags;
4978 wwn = wwn_to_u64(iocb->u.isp24.port_name);
4980 port_id.b.domain = iocb->u.isp24.port_id[2];
4981 port_id.b.area = iocb->u.isp24.port_id[1];
4982 port_id.b.al_pa = iocb->u.isp24.port_id[0];
4983 port_id.b.rsvd_1 = 0;
4985 loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4987 ql_dbg(ql_dbg_disc, vha, 0xf026,
4988 "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4989 vha->vp_idx, iocb->u.isp24.port_id[2],
4990 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4991 iocb->u.isp24.status_subcode, loop_id,
4992 iocb->u.isp24.port_name);
4994 /* res = 1 means ack at the end of thread
4995 * res = 0 means ack async/later.
4997 switch (iocb->u.isp24.status_subcode) {
4998 case ELS_PLOGI:
4999 res = qlt_handle_login(vha, iocb);
5000 break;
5002 case ELS_PRLI:
5003 if (N2N_TOPO(ha)) {
5004 sess = qla2x00_find_fcport_by_wwpn(vha,
5005 iocb->u.isp24.port_name, 1);
5007 if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5008 ql_dbg(ql_dbg_disc, vha, 0xffff,
5009 "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5010 __func__, __LINE__,
5011 iocb->u.isp24.port_name);
5012 qlt_send_term_imm_notif(vha, iocb, 1);
5013 break;
5016 res = qlt_handle_login(vha, iocb);
5017 break;
5020 if (IS_SW_RESV_ADDR(port_id)) {
5021 res = 1;
5022 break;
5025 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5027 if (wwn) {
5028 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5029 sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5030 loop_id, &conflict_sess);
5031 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5034 if (conflict_sess) {
5035 switch (conflict_sess->disc_state) {
5036 case DSC_DELETED:
5037 case DSC_DELETE_PEND:
5038 break;
5039 default:
5040 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5041 "PRLI with conflicting sess %p port %8phC\n",
5042 conflict_sess, conflict_sess->port_name);
5043 conflict_sess->fw_login_state =
5044 DSC_LS_PORT_UNAVAIL;
5045 qlt_send_term_imm_notif(vha, iocb, 1);
5046 res = 0;
5047 break;
5051 if (sess != NULL) {
5052 bool delete = false;
5053 int sec;
5054 spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5055 switch (sess->fw_login_state) {
5056 case DSC_LS_PLOGI_PEND:
5057 case DSC_LS_PLOGI_COMP:
5058 case DSC_LS_PRLI_COMP:
5059 break;
5060 default:
5061 delete = true;
5062 break;
5065 switch (sess->disc_state) {
5066 case DSC_UPD_FCPORT:
5067 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5068 flags);
5070 sec = jiffies_to_msecs(jiffies -
5071 sess->jiffies_at_registration)/1000;
5072 if (sess->sec_since_registration < sec && sec &&
5073 !(sec % 5)) {
5074 sess->sec_since_registration = sec;
5075 ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5076 "%s %8phC : Slow Rport registration(%d Sec)\n",
5077 __func__, sess->port_name, sec);
5079 qlt_send_term_imm_notif(vha, iocb, 1);
5080 return 0;
5082 case DSC_LOGIN_PEND:
5083 case DSC_GPDB:
5084 case DSC_LOGIN_COMPLETE:
5085 case DSC_ADISC:
5086 delete = false;
5087 break;
5088 default:
5089 break;
5092 if (delete) {
5093 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5094 flags);
5096 * Impatient initiator sent PRLI before last
5097 * PLOGI could finish. Will force him to re-try,
5098 * while last one finishes.
5100 ql_log(ql_log_warn, sess->vha, 0xf095,
5101 "sess %p PRLI received, before plogi ack.\n",
5102 sess);
5103 qlt_send_term_imm_notif(vha, iocb, 1);
5104 res = 0;
5105 break;
5109 * This shouldn't happen under normal circumstances,
5110 * since we have deleted the old session during PLOGI
5112 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5113 "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5114 sess->loop_id, sess, iocb->u.isp24.nport_handle);
5116 sess->local = 0;
5117 sess->loop_id = loop_id;
5118 sess->d_id = port_id;
5119 sess->fw_login_state = DSC_LS_PRLI_PEND;
5121 if (wd3_lo & BIT_7)
5122 sess->conf_compl_supported = 1;
5124 if ((wd3_lo & BIT_4) == 0)
5125 sess->port_type = FCT_INITIATOR;
5126 else
5127 sess->port_type = FCT_TARGET;
5129 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5131 res = 1; /* send notify ack */
5133 /* Make session global (not used in fabric mode) */
5134 if (ha->current_topology != ISP_CFG_F) {
5135 if (sess) {
5136 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5137 "%s %d %8phC post nack\n",
5138 __func__, __LINE__, sess->port_name);
5139 qla24xx_post_nack_work(vha, sess, iocb,
5140 SRB_NACK_PRLI);
5141 res = 0;
5142 } else {
5143 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5144 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5145 qla2xxx_wake_dpc(vha);
5147 } else {
5148 if (sess) {
5149 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5150 "%s %d %8phC post nack\n",
5151 __func__, __LINE__, sess->port_name);
5152 qla24xx_post_nack_work(vha, sess, iocb,
5153 SRB_NACK_PRLI);
5154 res = 0;
5157 break;
5159 case ELS_TPRLO:
5160 if (le16_to_cpu(iocb->u.isp24.flags) &
5161 NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5162 loop_id = 0xFFFF;
5163 qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5164 res = 1;
5165 break;
5167 /* fall through */
5168 case ELS_LOGO:
5169 case ELS_PRLO:
5170 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5171 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5172 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5174 if (sess) {
5175 sess->login_gen++;
5176 sess->fw_login_state = DSC_LS_LOGO_PEND;
5177 sess->logo_ack_needed = 1;
5178 memcpy(sess->iocb, iocb, IOCB_SIZE);
5181 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5183 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5184 "%s: logo %llx res %d sess %p ",
5185 __func__, wwn, res, sess);
5186 if (res == 0) {
5188 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5189 * for LOGO_ACK & sess delete
5191 BUG_ON(!sess);
5192 res = 0;
5193 } else {
5194 /* cmd did not go to upper layer. */
5195 if (sess) {
5196 qlt_schedule_sess_for_deletion(sess);
5197 res = 0;
5199 /* else logo will be ack */
5201 break;
5202 case ELS_PDISC:
5203 case ELS_ADISC:
5205 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5206 if (tgt->link_reinit_iocb_pending) {
5207 qlt_send_notify_ack(ha->base_qpair,
5208 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5209 tgt->link_reinit_iocb_pending = 0;
5212 sess = qla2x00_find_fcport_by_wwpn(vha,
5213 iocb->u.isp24.port_name, 1);
5214 if (sess) {
5215 ql_dbg(ql_dbg_disc, vha, 0x20fd,
5216 "sess %p lid %d|%d DS %d LS %d\n",
5217 sess, sess->loop_id, loop_id,
5218 sess->disc_state, sess->fw_login_state);
5221 res = 1; /* send notify ack */
5222 break;
5225 case ELS_FLOGI: /* should never happen */
5226 default:
5227 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5228 "qla_target(%d): Unsupported ELS command %x "
5229 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5230 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5231 break;
5234 ql_dbg(ql_dbg_disc, vha, 0xf026,
5235 "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5236 vha->vp_idx, iocb->u.isp24.status_subcode, res);
5238 return res;
5242 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5244 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5245 struct imm_ntfy_from_isp *iocb)
5247 struct qla_hw_data *ha = vha->hw;
5248 uint32_t add_flags = 0;
5249 int send_notify_ack = 1;
5250 uint16_t status;
5252 status = le16_to_cpu(iocb->u.isp2x.status);
5253 switch (status) {
5254 case IMM_NTFY_LIP_RESET:
5256 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5257 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5258 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5259 iocb->u.isp24.status_subcode);
5261 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5262 send_notify_ack = 0;
5263 break;
5266 case IMM_NTFY_LIP_LINK_REINIT:
5268 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5269 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5270 "qla_target(%d): LINK REINIT (loop %#x, "
5271 "subcode %x)\n", vha->vp_idx,
5272 le16_to_cpu(iocb->u.isp24.nport_handle),
5273 iocb->u.isp24.status_subcode);
5274 if (tgt->link_reinit_iocb_pending) {
5275 qlt_send_notify_ack(ha->base_qpair,
5276 &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5278 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5279 tgt->link_reinit_iocb_pending = 1;
5281 * QLogic requires to wait after LINK REINIT for possible
5282 * PDISC or ADISC ELS commands
5284 send_notify_ack = 0;
5285 break;
5288 case IMM_NTFY_PORT_LOGOUT:
5289 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5290 "qla_target(%d): Port logout (loop "
5291 "%#x, subcode %x)\n", vha->vp_idx,
5292 le16_to_cpu(iocb->u.isp24.nport_handle),
5293 iocb->u.isp24.status_subcode);
5295 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5296 send_notify_ack = 0;
5297 /* The sessions will be cleared in the callback, if needed */
5298 break;
5300 case IMM_NTFY_GLBL_TPRLO:
5301 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5302 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5303 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5304 send_notify_ack = 0;
5305 /* The sessions will be cleared in the callback, if needed */
5306 break;
5308 case IMM_NTFY_PORT_CONFIG:
5309 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5310 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5311 status);
5312 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5313 send_notify_ack = 0;
5314 /* The sessions will be cleared in the callback, if needed */
5315 break;
5317 case IMM_NTFY_GLBL_LOGO:
5318 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5319 "qla_target(%d): Link failure detected\n",
5320 vha->vp_idx);
5321 /* I_T nexus loss */
5322 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5323 send_notify_ack = 0;
5324 break;
5326 case IMM_NTFY_IOCB_OVERFLOW:
5327 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5328 "qla_target(%d): Cannot provide requested "
5329 "capability (IOCB overflowed the immediate notify "
5330 "resource count)\n", vha->vp_idx);
5331 break;
5333 case IMM_NTFY_ABORT_TASK:
5334 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5335 "qla_target(%d): Abort Task (S %08x I %#x -> "
5336 "L %#x)\n", vha->vp_idx,
5337 le16_to_cpu(iocb->u.isp2x.seq_id),
5338 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5339 le16_to_cpu(iocb->u.isp2x.lun));
5340 if (qlt_abort_task(vha, iocb) == 0)
5341 send_notify_ack = 0;
5342 break;
5344 case IMM_NTFY_RESOURCE:
5345 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5346 "qla_target(%d): Out of resources, host %ld\n",
5347 vha->vp_idx, vha->host_no);
5348 break;
5350 case IMM_NTFY_MSG_RX:
5351 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5352 "qla_target(%d): Immediate notify task %x\n",
5353 vha->vp_idx, iocb->u.isp2x.task_flags);
5354 break;
5356 case IMM_NTFY_ELS:
5357 if (qlt_24xx_handle_els(vha, iocb) == 0)
5358 send_notify_ack = 0;
5359 break;
5360 default:
5361 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5362 "qla_target(%d): Received unknown immediate "
5363 "notify status %x\n", vha->vp_idx, status);
5364 break;
5367 if (send_notify_ack)
5368 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5369 0, 0);
5373 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5374 * This function sends busy to ISP 2xxx or 24xx.
5376 static int __qlt_send_busy(struct qla_qpair *qpair,
5377 struct atio_from_isp *atio, uint16_t status)
5379 struct scsi_qla_host *vha = qpair->vha;
5380 struct ctio7_to_24xx *ctio24;
5381 struct qla_hw_data *ha = vha->hw;
5382 request_t *pkt;
5383 struct fc_port *sess = NULL;
5384 unsigned long flags;
5385 u16 temp;
5386 port_id_t id;
5388 id.b.al_pa = atio->u.isp24.fcp_hdr.s_id[2];
5389 id.b.area = atio->u.isp24.fcp_hdr.s_id[1];
5390 id.b.domain = atio->u.isp24.fcp_hdr.s_id[0];
5391 id.b.rsvd_1 = 0;
5393 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5394 sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5395 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5396 if (!sess) {
5397 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5398 return 0;
5400 /* Sending marker isn't necessary, since we called from ISR */
5402 pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5403 if (!pkt) {
5404 ql_dbg(ql_dbg_io, vha, 0x3063,
5405 "qla_target(%d): %s failed: unable to allocate "
5406 "request packet", vha->vp_idx, __func__);
5407 return -ENOMEM;
5410 qpair->tgt_counters.num_q_full_sent++;
5411 pkt->entry_count = 1;
5412 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5414 ctio24 = (struct ctio7_to_24xx *)pkt;
5415 ctio24->entry_type = CTIO_TYPE7;
5416 ctio24->nport_handle = sess->loop_id;
5417 ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5418 ctio24->vp_index = vha->vp_idx;
5419 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
5420 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
5421 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
5422 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5423 temp = (atio->u.isp24.attr << 9) |
5424 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5425 CTIO7_FLAGS_DONT_RET_CTIO;
5426 ctio24->u.status1.flags = cpu_to_le16(temp);
5428 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5429 * if the explicit conformation is used.
5431 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
5432 ctio24->u.status1.scsi_status = cpu_to_le16(status);
5434 ctio24->u.status1.residual = get_datalen_for_atio(atio);
5436 if (ctio24->u.status1.residual != 0)
5437 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
5439 /* Memory Barrier */
5440 wmb();
5441 if (qpair->reqq_start_iocbs)
5442 qpair->reqq_start_iocbs(qpair);
5443 else
5444 qla2x00_start_iocbs(vha, qpair->req);
5445 return 0;
5449 * This routine is used to allocate a command for either a QFull condition
5450 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5451 * out previously.
5453 static void
5454 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5455 struct atio_from_isp *atio, uint16_t status, int qfull)
5457 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5458 struct qla_hw_data *ha = vha->hw;
5459 struct fc_port *sess;
5460 struct se_session *se_sess;
5461 struct qla_tgt_cmd *cmd;
5462 int tag, cpu;
5463 unsigned long flags;
5465 if (unlikely(tgt->tgt_stop)) {
5466 ql_dbg(ql_dbg_io, vha, 0x300a,
5467 "New command while device %p is shutting down\n", tgt);
5468 return;
5471 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5472 vha->hw->tgt.num_qfull_cmds_dropped++;
5473 if (vha->hw->tgt.num_qfull_cmds_dropped >
5474 vha->qla_stats.stat_max_qfull_cmds_dropped)
5475 vha->qla_stats.stat_max_qfull_cmds_dropped =
5476 vha->hw->tgt.num_qfull_cmds_dropped;
5478 ql_dbg(ql_dbg_io, vha, 0x3068,
5479 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5480 vha->vp_idx, __func__,
5481 vha->hw->tgt.num_qfull_cmds_dropped);
5483 qlt_chk_exch_leak_thresh_hold(vha);
5484 return;
5487 sess = ha->tgt.tgt_ops->find_sess_by_s_id
5488 (vha, atio->u.isp24.fcp_hdr.s_id);
5489 if (!sess)
5490 return;
5492 se_sess = sess->se_sess;
5494 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
5495 if (tag < 0)
5496 return;
5498 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5499 if (!cmd) {
5500 ql_dbg(ql_dbg_io, vha, 0x3009,
5501 "qla_target(%d): %s: Allocation of cmd failed\n",
5502 vha->vp_idx, __func__);
5504 vha->hw->tgt.num_qfull_cmds_dropped++;
5505 if (vha->hw->tgt.num_qfull_cmds_dropped >
5506 vha->qla_stats.stat_max_qfull_cmds_dropped)
5507 vha->qla_stats.stat_max_qfull_cmds_dropped =
5508 vha->hw->tgt.num_qfull_cmds_dropped;
5510 qlt_chk_exch_leak_thresh_hold(vha);
5511 return;
5514 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
5516 qlt_incr_num_pend_cmds(vha);
5517 INIT_LIST_HEAD(&cmd->cmd_list);
5518 memcpy(&cmd->atio, atio, sizeof(*atio));
5520 cmd->tgt = vha->vha_tgt.qla_tgt;
5521 cmd->vha = vha;
5522 cmd->reset_count = ha->base_qpair->chip_reset;
5523 cmd->q_full = 1;
5524 cmd->qpair = ha->base_qpair;
5525 cmd->se_cmd.map_cpu = cpu;
5527 if (qfull) {
5528 cmd->q_full = 1;
5529 /* NOTE: borrowing the state field to carry the status */
5530 cmd->state = status;
5531 } else
5532 cmd->term_exchg = 1;
5534 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5535 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5537 vha->hw->tgt.num_qfull_cmds_alloc++;
5538 if (vha->hw->tgt.num_qfull_cmds_alloc >
5539 vha->qla_stats.stat_max_qfull_cmds_alloc)
5540 vha->qla_stats.stat_max_qfull_cmds_alloc =
5541 vha->hw->tgt.num_qfull_cmds_alloc;
5542 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5546 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5548 struct scsi_qla_host *vha = qpair->vha;
5549 struct qla_hw_data *ha = vha->hw;
5550 unsigned long flags;
5551 struct qla_tgt_cmd *cmd, *tcmd;
5552 struct list_head free_list, q_full_list;
5553 int rc = 0;
5555 if (list_empty(&ha->tgt.q_full_list))
5556 return 0;
5558 INIT_LIST_HEAD(&free_list);
5559 INIT_LIST_HEAD(&q_full_list);
5561 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5562 if (list_empty(&ha->tgt.q_full_list)) {
5563 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5564 return 0;
5567 list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5568 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5570 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5571 list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5572 if (cmd->q_full)
5573 /* cmd->state is a borrowed field to hold status */
5574 rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5575 else if (cmd->term_exchg)
5576 rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5578 if (rc == -ENOMEM)
5579 break;
5581 if (cmd->q_full)
5582 ql_dbg(ql_dbg_io, vha, 0x3006,
5583 "%s: busy sent for ox_id[%04x]\n", __func__,
5584 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5585 else if (cmd->term_exchg)
5586 ql_dbg(ql_dbg_io, vha, 0x3007,
5587 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5588 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5589 else
5590 ql_dbg(ql_dbg_io, vha, 0x3008,
5591 "%s: Unexpected cmd in QFull list %p\n", __func__,
5592 cmd);
5594 list_del(&cmd->cmd_list);
5595 list_add_tail(&cmd->cmd_list, &free_list);
5597 /* piggy back on hardware_lock for protection */
5598 vha->hw->tgt.num_qfull_cmds_alloc--;
5600 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5602 cmd = NULL;
5604 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5605 list_del(&cmd->cmd_list);
5606 /* This cmd was never sent to TCM. There is no need
5607 * to schedule free or call free_cmd
5609 qlt_free_cmd(cmd);
5612 if (!list_empty(&q_full_list)) {
5613 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5614 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5615 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5618 return rc;
5621 static void
5622 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5623 uint16_t status)
5625 int rc = 0;
5626 struct scsi_qla_host *vha = qpair->vha;
5628 rc = __qlt_send_busy(qpair, atio, status);
5629 if (rc == -ENOMEM)
5630 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5633 static int
5634 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5635 struct atio_from_isp *atio, uint8_t ha_locked)
5637 struct qla_hw_data *ha = vha->hw;
5638 unsigned long flags;
5640 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5641 return 0;
5643 if (!ha_locked)
5644 spin_lock_irqsave(&ha->hardware_lock, flags);
5645 qlt_send_busy(qpair, atio, qla_sam_status);
5646 if (!ha_locked)
5647 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5649 return 1;
5652 /* ha->hardware_lock supposed to be held on entry */
5653 /* called via callback from qla2xxx */
5654 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5655 struct atio_from_isp *atio, uint8_t ha_locked)
5657 struct qla_hw_data *ha = vha->hw;
5658 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5659 int rc;
5660 unsigned long flags = 0;
5662 if (unlikely(tgt == NULL)) {
5663 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5664 "ATIO pkt, but no tgt (ha %p)", ha);
5665 return;
5668 * In tgt_stop mode we also should allow all requests to pass.
5669 * Otherwise, some commands can stuck.
5672 tgt->atio_irq_cmd_count++;
5674 switch (atio->u.raw.entry_type) {
5675 case ATIO_TYPE7:
5676 if (unlikely(atio->u.isp24.exchange_addr ==
5677 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5678 ql_dbg(ql_dbg_io, vha, 0x3065,
5679 "qla_target(%d): ATIO_TYPE7 "
5680 "received with UNKNOWN exchange address, "
5681 "sending QUEUE_FULL\n", vha->vp_idx);
5682 if (!ha_locked)
5683 spin_lock_irqsave(&ha->hardware_lock, flags);
5684 qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5685 if (!ha_locked)
5686 spin_unlock_irqrestore(&ha->hardware_lock,
5687 flags);
5688 break;
5691 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5692 rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5693 atio, ha_locked);
5694 if (rc != 0) {
5695 tgt->atio_irq_cmd_count--;
5696 return;
5698 rc = qlt_handle_cmd_for_atio(vha, atio);
5699 } else {
5700 rc = qlt_handle_task_mgmt(vha, atio);
5702 if (unlikely(rc != 0)) {
5703 if (!ha_locked)
5704 spin_lock_irqsave(&ha->hardware_lock, flags);
5705 switch (rc) {
5706 case -ENODEV:
5707 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5708 "qla_target: Unable to send command to target\n");
5709 break;
5710 case -EBADF:
5711 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5712 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5713 qlt_send_term_exchange(ha->base_qpair, NULL,
5714 atio, 1, 0);
5715 break;
5716 case -EBUSY:
5717 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5718 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5719 vha->vp_idx);
5720 qlt_send_busy(ha->base_qpair, atio,
5721 tc_sam_status);
5722 break;
5723 default:
5724 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5725 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5726 vha->vp_idx);
5727 qlt_send_busy(ha->base_qpair, atio,
5728 qla_sam_status);
5729 break;
5731 if (!ha_locked)
5732 spin_unlock_irqrestore(&ha->hardware_lock,
5733 flags);
5735 break;
5737 case IMMED_NOTIFY_TYPE:
5739 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5740 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5741 "qla_target(%d): Received ATIO packet %x "
5742 "with error status %x\n", vha->vp_idx,
5743 atio->u.raw.entry_type,
5744 atio->u.isp2x.entry_status);
5745 break;
5747 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5749 if (!ha_locked)
5750 spin_lock_irqsave(&ha->hardware_lock, flags);
5751 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5752 if (!ha_locked)
5753 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5754 break;
5757 default:
5758 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5759 "qla_target(%d): Received unknown ATIO atio "
5760 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5761 break;
5764 tgt->atio_irq_cmd_count--;
5768 * qpair lock is assume to be held
5769 * rc = 0 : send terminate & abts respond
5770 * rc != 0: do not send term & abts respond
5772 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5773 struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5775 struct qla_hw_data *ha = vha->hw;
5776 int rc = 0;
5779 * Detect unresolved exchange. If the same ABTS is unable
5780 * to terminate an existing command and the same ABTS loops
5781 * between FW & Driver, then force FW dump. Under 1 jiff,
5782 * we should see multiple loops.
5784 if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5785 qpair->retry_term_jiff == jiffies) {
5786 /* found existing exchange */
5787 qpair->retry_term_cnt++;
5788 if (qpair->retry_term_cnt >= 5) {
5789 rc = EIO;
5790 qpair->retry_term_cnt = 0;
5791 ql_log(ql_log_warn, vha, 0xffff,
5792 "Unable to send ABTS Respond. Dumping firmware.\n");
5793 ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5794 vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5796 if (qpair == ha->base_qpair)
5797 ha->isp_ops->fw_dump(vha, 1);
5798 else
5799 ha->isp_ops->fw_dump(vha, 0);
5801 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5802 qla2xxx_wake_dpc(vha);
5804 } else if (qpair->retry_term_jiff != jiffies) {
5805 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5806 qpair->retry_term_cnt = 0;
5807 qpair->retry_term_jiff = jiffies;
5810 return rc;
5814 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5815 struct rsp_que *rsp, response_t *pkt)
5817 struct abts_resp_from_24xx_fw *entry =
5818 (struct abts_resp_from_24xx_fw *)pkt;
5819 u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5820 struct qla_tgt_mgmt_cmd *mcmd;
5821 struct qla_hw_data *ha = vha->hw;
5823 mcmd = (struct qla_tgt_mgmt_cmd *)qlt_ctio_to_cmd(vha, rsp,
5824 pkt->handle, pkt);
5825 if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5826 ql_dbg(ql_dbg_async, vha, 0xe064,
5827 "qla_target(%d): ABTS Comp without mcmd\n",
5828 vha->vp_idx);
5829 return;
5832 if (mcmd)
5833 vha = mcmd->vha;
5834 vha->vha_tgt.qla_tgt->abts_resp_expected--;
5836 ql_dbg(ql_dbg_tgt, vha, 0xe038,
5837 "ABTS_RESP_24XX: compl_status %x\n",
5838 entry->compl_status);
5840 if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5841 if ((entry->error_subcode1 == 0x1E) &&
5842 (entry->error_subcode2 == 0)) {
5843 if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5844 ha->tgt.tgt_ops->free_mcmd(mcmd);
5845 return;
5847 qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5848 pkt, mcmd);
5849 } else {
5850 ql_dbg(ql_dbg_tgt, vha, 0xe063,
5851 "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5852 vha->vp_idx, entry->compl_status,
5853 entry->error_subcode1,
5854 entry->error_subcode2);
5855 ha->tgt.tgt_ops->free_mcmd(mcmd);
5857 } else {
5858 ha->tgt.tgt_ops->free_mcmd(mcmd);
5862 /* ha->hardware_lock supposed to be held on entry */
5863 /* called via callback from qla2xxx */
5864 static void qlt_response_pkt(struct scsi_qla_host *vha,
5865 struct rsp_que *rsp, response_t *pkt)
5867 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5869 if (unlikely(tgt == NULL)) {
5870 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5871 "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5872 vha->vp_idx, pkt->entry_type, vha->hw);
5873 return;
5877 * In tgt_stop mode we also should allow all requests to pass.
5878 * Otherwise, some commands can stuck.
5881 switch (pkt->entry_type) {
5882 case CTIO_CRC2:
5883 case CTIO_TYPE7:
5885 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5886 qlt_do_ctio_completion(vha, rsp, entry->handle,
5887 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5888 entry);
5889 break;
5892 case ACCEPT_TGT_IO_TYPE:
5894 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5895 int rc;
5896 if (atio->u.isp2x.status !=
5897 cpu_to_le16(ATIO_CDB_VALID)) {
5898 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5899 "qla_target(%d): ATIO with error "
5900 "status %x received\n", vha->vp_idx,
5901 le16_to_cpu(atio->u.isp2x.status));
5902 break;
5905 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5906 if (rc != 0)
5907 return;
5909 rc = qlt_handle_cmd_for_atio(vha, atio);
5910 if (unlikely(rc != 0)) {
5911 switch (rc) {
5912 case -ENODEV:
5913 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5914 "qla_target: Unable to send command to target\n");
5915 break;
5916 case -EBADF:
5917 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5918 "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5919 qlt_send_term_exchange(rsp->qpair, NULL,
5920 atio, 1, 0);
5921 break;
5922 case -EBUSY:
5923 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5924 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5925 vha->vp_idx);
5926 qlt_send_busy(rsp->qpair, atio,
5927 tc_sam_status);
5928 break;
5929 default:
5930 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5931 "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5932 vha->vp_idx);
5933 qlt_send_busy(rsp->qpair, atio,
5934 qla_sam_status);
5935 break;
5939 break;
5941 case CONTINUE_TGT_IO_TYPE:
5943 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5944 qlt_do_ctio_completion(vha, rsp, entry->handle,
5945 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5946 entry);
5947 break;
5950 case CTIO_A64_TYPE:
5952 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5953 qlt_do_ctio_completion(vha, rsp, entry->handle,
5954 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5955 entry);
5956 break;
5959 case IMMED_NOTIFY_TYPE:
5960 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5961 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5962 break;
5964 case NOTIFY_ACK_TYPE:
5965 if (tgt->notify_ack_expected > 0) {
5966 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5967 ql_dbg(ql_dbg_tgt, vha, 0xe036,
5968 "NOTIFY_ACK seq %08x status %x\n",
5969 le16_to_cpu(entry->u.isp2x.seq_id),
5970 le16_to_cpu(entry->u.isp2x.status));
5971 tgt->notify_ack_expected--;
5972 if (entry->u.isp2x.status !=
5973 cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5974 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5975 "qla_target(%d): NOTIFY_ACK "
5976 "failed %x\n", vha->vp_idx,
5977 le16_to_cpu(entry->u.isp2x.status));
5979 } else {
5980 ql_dbg(ql_dbg_tgt, vha, 0xe062,
5981 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5982 vha->vp_idx);
5984 break;
5986 case ABTS_RECV_24XX:
5987 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5988 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5989 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5990 break;
5992 case ABTS_RESP_24XX:
5993 if (tgt->abts_resp_expected > 0) {
5994 qlt_handle_abts_completion(vha, rsp, pkt);
5995 } else {
5996 ql_dbg(ql_dbg_tgt, vha, 0xe064,
5997 "qla_target(%d): Unexpected ABTS_RESP_24XX "
5998 "received\n", vha->vp_idx);
6000 break;
6002 default:
6003 ql_dbg(ql_dbg_tgt, vha, 0xe065,
6004 "qla_target(%d): Received unknown response pkt "
6005 "type %x\n", vha->vp_idx, pkt->entry_type);
6006 break;
6012 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6014 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6015 uint16_t *mailbox)
6017 struct qla_hw_data *ha = vha->hw;
6018 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6019 int login_code;
6021 if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6022 return;
6024 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6025 IS_QLA2100(ha))
6026 return;
6028 * In tgt_stop mode we also should allow all requests to pass.
6029 * Otherwise, some commands can stuck.
6033 switch (code) {
6034 case MBA_RESET: /* Reset */
6035 case MBA_SYSTEM_ERR: /* System Error */
6036 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
6037 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
6038 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6039 "qla_target(%d): System error async event %#x "
6040 "occurred", vha->vp_idx, code);
6041 break;
6042 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
6043 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6044 break;
6046 case MBA_LOOP_UP:
6048 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6049 "qla_target(%d): Async LOOP_UP occurred "
6050 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6051 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6052 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6053 if (tgt->link_reinit_iocb_pending) {
6054 qlt_send_notify_ack(ha->base_qpair,
6055 (void *)&tgt->link_reinit_iocb,
6056 0, 0, 0, 0, 0, 0);
6057 tgt->link_reinit_iocb_pending = 0;
6059 break;
6062 case MBA_LIP_OCCURRED:
6063 case MBA_LOOP_DOWN:
6064 case MBA_LIP_RESET:
6065 case MBA_RSCN_UPDATE:
6066 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6067 "qla_target(%d): Async event %#x occurred "
6068 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6069 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6070 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6071 break;
6073 case MBA_REJECTED_FCP_CMD:
6074 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6075 "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6076 vha->vp_idx,
6077 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6078 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6080 if (le16_to_cpu(mailbox[3]) == 1) {
6081 /* exchange starvation. */
6082 vha->hw->exch_starvation++;
6083 if (vha->hw->exch_starvation > 5) {
6084 ql_log(ql_log_warn, vha, 0xd03a,
6085 "Exchange starvation-. Resetting RISC\n");
6087 vha->hw->exch_starvation = 0;
6088 if (IS_P3P_TYPE(vha->hw))
6089 set_bit(FCOE_CTX_RESET_NEEDED,
6090 &vha->dpc_flags);
6091 else
6092 set_bit(ISP_ABORT_NEEDED,
6093 &vha->dpc_flags);
6094 qla2xxx_wake_dpc(vha);
6097 break;
6099 case MBA_PORT_UPDATE:
6100 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6101 "qla_target(%d): Port update async event %#x "
6102 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6103 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6104 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
6105 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
6107 login_code = le16_to_cpu(mailbox[2]);
6108 if (login_code == 0x4) {
6109 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6110 "Async MB 2: Got PLOGI Complete\n");
6111 vha->hw->exch_starvation = 0;
6112 } else if (login_code == 0x7)
6113 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6114 "Async MB 2: Port Logged Out\n");
6115 break;
6116 default:
6117 break;
6122 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6123 uint16_t loop_id)
6125 fc_port_t *fcport, *tfcp, *del;
6126 int rc;
6127 unsigned long flags;
6128 u8 newfcport = 0;
6130 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6131 if (!fcport) {
6132 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6133 "qla_target(%d): Allocation of tmp FC port failed",
6134 vha->vp_idx);
6135 return NULL;
6138 fcport->loop_id = loop_id;
6140 rc = qla24xx_gpdb_wait(vha, fcport, 0);
6141 if (rc != QLA_SUCCESS) {
6142 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6143 "qla_target(%d): Failed to retrieve fcport "
6144 "information -- get_port_database() returned %x "
6145 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6146 kfree(fcport);
6147 return NULL;
6150 del = NULL;
6151 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6152 tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6154 if (tfcp) {
6155 tfcp->d_id = fcport->d_id;
6156 tfcp->port_type = fcport->port_type;
6157 tfcp->supported_classes = fcport->supported_classes;
6158 tfcp->flags |= fcport->flags;
6159 tfcp->scan_state = QLA_FCPORT_FOUND;
6161 del = fcport;
6162 fcport = tfcp;
6163 } else {
6164 if (vha->hw->current_topology == ISP_CFG_F)
6165 fcport->flags |= FCF_FABRIC_DEVICE;
6167 list_add_tail(&fcport->list, &vha->vp_fcports);
6168 if (!IS_SW_RESV_ADDR(fcport->d_id))
6169 vha->fcport_count++;
6170 fcport->login_gen++;
6171 fcport->disc_state = DSC_LOGIN_COMPLETE;
6172 fcport->login_succ = 1;
6173 newfcport = 1;
6176 fcport->deleted = 0;
6177 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6179 switch (vha->host->active_mode) {
6180 case MODE_INITIATOR:
6181 case MODE_DUAL:
6182 if (newfcport) {
6183 if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6184 qla24xx_sched_upd_fcport(fcport);
6185 } else {
6186 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6187 "%s %d %8phC post gpsc fcp_cnt %d\n",
6188 __func__, __LINE__, fcport->port_name, vha->fcport_count);
6189 qla24xx_post_gpsc_work(vha, fcport);
6192 break;
6194 case MODE_TARGET:
6195 default:
6196 break;
6198 if (del)
6199 qla2x00_free_fcport(del);
6201 return fcport;
6204 /* Must be called under tgt_mutex */
6205 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6206 uint8_t *s_id)
6208 struct fc_port *sess = NULL;
6209 fc_port_t *fcport = NULL;
6210 int rc, global_resets;
6211 uint16_t loop_id = 0;
6213 if ((s_id[0] == 0xFF) && (s_id[1] == 0xFC)) {
6215 * This is Domain Controller, so it should be
6216 * OK to drop SCSI commands from it.
6218 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6219 "Unable to find initiator with S_ID %x:%x:%x",
6220 s_id[0], s_id[1], s_id[2]);
6221 return NULL;
6224 mutex_lock(&vha->vha_tgt.tgt_mutex);
6226 retry:
6227 global_resets =
6228 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6230 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6231 if (rc != 0) {
6232 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6234 ql_log(ql_log_info, vha, 0xf071,
6235 "qla_target(%d): Unable to find "
6236 "initiator with S_ID %x:%x:%x",
6237 vha->vp_idx, s_id[0], s_id[1],
6238 s_id[2]);
6240 if (rc == -ENOENT) {
6241 qlt_port_logo_t logo;
6242 sid_to_portid(s_id, &logo.id);
6243 logo.cmd_count = 1;
6244 qlt_send_first_logo(vha, &logo);
6247 return NULL;
6250 fcport = qlt_get_port_database(vha, loop_id);
6251 if (!fcport) {
6252 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6253 return NULL;
6256 if (global_resets !=
6257 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6258 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6259 "qla_target(%d): global reset during session discovery "
6260 "(counter was %d, new %d), retrying", vha->vp_idx,
6261 global_resets,
6262 atomic_read(&vha->vha_tgt.
6263 qla_tgt->tgt_global_resets_count));
6264 goto retry;
6267 sess = qlt_create_sess(vha, fcport, true);
6269 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6271 return sess;
6274 static void qlt_abort_work(struct qla_tgt *tgt,
6275 struct qla_tgt_sess_work_param *prm)
6277 struct scsi_qla_host *vha = tgt->vha;
6278 struct qla_hw_data *ha = vha->hw;
6279 struct fc_port *sess = NULL;
6280 unsigned long flags = 0, flags2 = 0;
6281 uint32_t be_s_id;
6282 uint8_t s_id[3];
6283 int rc;
6285 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6287 if (tgt->tgt_stop)
6288 goto out_term2;
6290 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
6291 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
6292 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
6294 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
6295 (unsigned char *)&be_s_id);
6296 if (!sess) {
6297 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6299 sess = qlt_make_local_sess(vha, s_id);
6300 /* sess has got an extra creation ref */
6302 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6303 if (!sess)
6304 goto out_term2;
6305 } else {
6306 if (sess->deleted) {
6307 sess = NULL;
6308 goto out_term2;
6311 if (!kref_get_unless_zero(&sess->sess_kref)) {
6312 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6313 "%s: kref_get fail %8phC \n",
6314 __func__, sess->port_name);
6315 sess = NULL;
6316 goto out_term2;
6320 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6321 ha->tgt.tgt_ops->put_sess(sess);
6322 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6324 if (rc != 0)
6325 goto out_term;
6326 return;
6328 out_term2:
6329 if (sess)
6330 ha->tgt.tgt_ops->put_sess(sess);
6331 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6333 out_term:
6334 spin_lock_irqsave(&ha->hardware_lock, flags);
6335 qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6336 FCP_TMF_REJECTED, false);
6337 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6340 static void qlt_tmr_work(struct qla_tgt *tgt,
6341 struct qla_tgt_sess_work_param *prm)
6343 struct atio_from_isp *a = &prm->tm_iocb2;
6344 struct scsi_qla_host *vha = tgt->vha;
6345 struct qla_hw_data *ha = vha->hw;
6346 struct fc_port *sess;
6347 unsigned long flags;
6348 uint8_t *s_id = NULL; /* to hide compiler warnings */
6349 int rc;
6350 u64 unpacked_lun;
6351 int fn;
6352 void *iocb;
6354 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6356 if (tgt->tgt_stop)
6357 goto out_term2;
6359 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6360 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6361 if (!sess) {
6362 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6364 sess = qlt_make_local_sess(vha, s_id);
6365 /* sess has got an extra creation ref */
6367 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6368 if (!sess)
6369 goto out_term2;
6370 } else {
6371 if (sess->deleted) {
6372 goto out_term2;
6375 if (!kref_get_unless_zero(&sess->sess_kref)) {
6376 ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6377 "%s: kref_get fail %8phC\n",
6378 __func__, sess->port_name);
6379 goto out_term2;
6383 iocb = a;
6384 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6385 unpacked_lun =
6386 scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6388 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6389 ha->tgt.tgt_ops->put_sess(sess);
6390 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6392 if (rc != 0)
6393 goto out_term;
6394 return;
6396 out_term2:
6397 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6398 out_term:
6399 qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6402 static void qlt_sess_work_fn(struct work_struct *work)
6404 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6405 struct scsi_qla_host *vha = tgt->vha;
6406 unsigned long flags;
6408 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6410 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6411 while (!list_empty(&tgt->sess_works_list)) {
6412 struct qla_tgt_sess_work_param *prm = list_entry(
6413 tgt->sess_works_list.next, typeof(*prm),
6414 sess_works_list_entry);
6417 * This work can be scheduled on several CPUs at time, so we
6418 * must delete the entry to eliminate double processing
6420 list_del(&prm->sess_works_list_entry);
6422 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6424 switch (prm->type) {
6425 case QLA_TGT_SESS_WORK_ABORT:
6426 qlt_abort_work(tgt, prm);
6427 break;
6428 case QLA_TGT_SESS_WORK_TM:
6429 qlt_tmr_work(tgt, prm);
6430 break;
6431 default:
6432 BUG_ON(1);
6433 break;
6436 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6438 kfree(prm);
6440 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6443 /* Must be called under tgt_host_action_mutex */
6444 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6446 struct qla_tgt *tgt;
6447 int rc, i;
6448 struct qla_qpair_hint *h;
6450 if (!QLA_TGT_MODE_ENABLED())
6451 return 0;
6453 if (!IS_TGT_MODE_CAPABLE(ha)) {
6454 ql_log(ql_log_warn, base_vha, 0xe070,
6455 "This adapter does not support target mode.\n");
6456 return 0;
6459 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6460 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6462 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6464 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6465 if (!tgt) {
6466 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6467 "Unable to allocate struct qla_tgt\n");
6468 return -ENOMEM;
6471 tgt->qphints = kcalloc(ha->max_qpairs + 1,
6472 sizeof(struct qla_qpair_hint),
6473 GFP_KERNEL);
6474 if (!tgt->qphints) {
6475 kfree(tgt);
6476 ql_log(ql_log_warn, base_vha, 0x0197,
6477 "Unable to allocate qpair hints.\n");
6478 return -ENOMEM;
6481 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6482 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6484 rc = btree_init64(&tgt->lun_qpair_map);
6485 if (rc) {
6486 kfree(tgt->qphints);
6487 kfree(tgt);
6488 ql_log(ql_log_info, base_vha, 0x0198,
6489 "Unable to initialize lun_qpair_map btree\n");
6490 return -EIO;
6492 h = &tgt->qphints[0];
6493 h->qpair = ha->base_qpair;
6494 INIT_LIST_HEAD(&h->hint_elem);
6495 h->cpuid = ha->base_qpair->cpuid;
6496 list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6498 for (i = 0; i < ha->max_qpairs; i++) {
6499 unsigned long flags;
6501 struct qla_qpair *qpair = ha->queue_pair_map[i];
6502 h = &tgt->qphints[i + 1];
6503 INIT_LIST_HEAD(&h->hint_elem);
6504 if (qpair) {
6505 h->qpair = qpair;
6506 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6507 list_add_tail(&h->hint_elem, &qpair->hints_list);
6508 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6509 h->cpuid = qpair->cpuid;
6513 tgt->ha = ha;
6514 tgt->vha = base_vha;
6515 init_waitqueue_head(&tgt->waitQ);
6516 INIT_LIST_HEAD(&tgt->del_sess_list);
6517 spin_lock_init(&tgt->sess_work_lock);
6518 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6519 INIT_LIST_HEAD(&tgt->sess_works_list);
6520 atomic_set(&tgt->tgt_global_resets_count, 0);
6522 base_vha->vha_tgt.qla_tgt = tgt;
6524 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6525 "qla_target(%d): using 64 Bit PCI addressing",
6526 base_vha->vp_idx);
6527 /* 3 is reserved */
6528 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6530 mutex_lock(&qla_tgt_mutex);
6531 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6532 mutex_unlock(&qla_tgt_mutex);
6534 if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6535 ha->tgt.tgt_ops->add_target(base_vha);
6537 return 0;
6540 /* Must be called under tgt_host_action_mutex */
6541 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6543 if (!vha->vha_tgt.qla_tgt)
6544 return 0;
6546 if (vha->fc_vport) {
6547 qlt_release(vha->vha_tgt.qla_tgt);
6548 return 0;
6551 /* free left over qfull cmds */
6552 qlt_init_term_exchange(vha);
6554 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6555 vha->host_no, ha);
6556 qlt_release(vha->vha_tgt.qla_tgt);
6558 return 0;
6561 void qlt_remove_target_resources(struct qla_hw_data *ha)
6563 struct scsi_qla_host *node;
6564 u32 key = 0;
6566 btree_for_each_safe32(&ha->tgt.host_map, key, node)
6567 btree_remove32(&ha->tgt.host_map, key);
6569 btree_destroy32(&ha->tgt.host_map);
6572 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6573 unsigned char *b)
6575 int i;
6577 pr_debug("qla2xxx HW vha->node_name: ");
6578 for (i = 0; i < WWN_SIZE; i++)
6579 pr_debug("%02x ", vha->node_name[i]);
6580 pr_debug("\n");
6581 pr_debug("qla2xxx HW vha->port_name: ");
6582 for (i = 0; i < WWN_SIZE; i++)
6583 pr_debug("%02x ", vha->port_name[i]);
6584 pr_debug("\n");
6586 pr_debug("qla2xxx passed configfs WWPN: ");
6587 put_unaligned_be64(wwpn, b);
6588 for (i = 0; i < WWN_SIZE; i++)
6589 pr_debug("%02x ", b[i]);
6590 pr_debug("\n");
6594 * qla_tgt_lport_register - register lport with external module
6596 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6597 * @phys_wwpn: physical port WWPN
6598 * @npiv_wwpn: NPIV WWPN
6599 * @npiv_wwnn: NPIV WWNN
6600 * @callback: lport initialization callback for tcm_qla2xxx code
6602 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6603 u64 npiv_wwpn, u64 npiv_wwnn,
6604 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6606 struct qla_tgt *tgt;
6607 struct scsi_qla_host *vha;
6608 struct qla_hw_data *ha;
6609 struct Scsi_Host *host;
6610 unsigned long flags;
6611 int rc;
6612 u8 b[WWN_SIZE];
6614 mutex_lock(&qla_tgt_mutex);
6615 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6616 vha = tgt->vha;
6617 ha = vha->hw;
6619 host = vha->host;
6620 if (!host)
6621 continue;
6623 if (!(host->hostt->supported_mode & MODE_TARGET))
6624 continue;
6626 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6627 continue;
6629 spin_lock_irqsave(&ha->hardware_lock, flags);
6630 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6631 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6632 host->host_no);
6633 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6634 continue;
6636 if (tgt->tgt_stop) {
6637 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6638 host->host_no);
6639 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6640 continue;
6642 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6644 if (!scsi_host_get(host)) {
6645 ql_dbg(ql_dbg_tgt, vha, 0xe068,
6646 "Unable to scsi_host_get() for"
6647 " qla2xxx scsi_host\n");
6648 continue;
6650 qlt_lport_dump(vha, phys_wwpn, b);
6652 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6653 scsi_host_put(host);
6654 continue;
6656 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6657 if (rc != 0)
6658 scsi_host_put(host);
6660 mutex_unlock(&qla_tgt_mutex);
6661 return rc;
6663 mutex_unlock(&qla_tgt_mutex);
6665 return -ENODEV;
6667 EXPORT_SYMBOL(qlt_lport_register);
6670 * qla_tgt_lport_deregister - Degister lport
6672 * @vha: Registered scsi_qla_host pointer
6674 void qlt_lport_deregister(struct scsi_qla_host *vha)
6676 struct qla_hw_data *ha = vha->hw;
6677 struct Scsi_Host *sh = vha->host;
6679 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6681 vha->vha_tgt.target_lport_ptr = NULL;
6682 ha->tgt.tgt_ops = NULL;
6684 * Release the Scsi_Host reference for the underlying qla2xxx host
6686 scsi_host_put(sh);
6688 EXPORT_SYMBOL(qlt_lport_deregister);
6690 /* Must be called under HW lock */
6691 void qlt_set_mode(struct scsi_qla_host *vha)
6693 switch (vha->qlini_mode) {
6694 case QLA2XXX_INI_MODE_DISABLED:
6695 case QLA2XXX_INI_MODE_EXCLUSIVE:
6696 vha->host->active_mode = MODE_TARGET;
6697 break;
6698 case QLA2XXX_INI_MODE_ENABLED:
6699 vha->host->active_mode = MODE_INITIATOR;
6700 break;
6701 case QLA2XXX_INI_MODE_DUAL:
6702 vha->host->active_mode = MODE_DUAL;
6703 break;
6704 default:
6705 break;
6709 /* Must be called under HW lock */
6710 static void qlt_clear_mode(struct scsi_qla_host *vha)
6712 switch (vha->qlini_mode) {
6713 case QLA2XXX_INI_MODE_DISABLED:
6714 vha->host->active_mode = MODE_UNKNOWN;
6715 break;
6716 case QLA2XXX_INI_MODE_EXCLUSIVE:
6717 vha->host->active_mode = MODE_INITIATOR;
6718 break;
6719 case QLA2XXX_INI_MODE_ENABLED:
6720 case QLA2XXX_INI_MODE_DUAL:
6721 vha->host->active_mode = MODE_INITIATOR;
6722 break;
6723 default:
6724 break;
6729 * qla_tgt_enable_vha - NO LOCK HELD
6731 * host_reset, bring up w/ Target Mode Enabled
6733 void
6734 qlt_enable_vha(struct scsi_qla_host *vha)
6736 struct qla_hw_data *ha = vha->hw;
6737 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6738 unsigned long flags;
6739 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6741 if (!tgt) {
6742 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6743 "Unable to locate qla_tgt pointer from"
6744 " struct qla_hw_data\n");
6745 dump_stack();
6746 return;
6748 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6749 return;
6751 spin_lock_irqsave(&ha->hardware_lock, flags);
6752 tgt->tgt_stopped = 0;
6753 qlt_set_mode(vha);
6754 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6756 mutex_lock(&ha->optrom_mutex);
6757 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6758 "%s.\n", __func__);
6759 if (vha->vp_idx) {
6760 qla24xx_disable_vp(vha);
6761 qla24xx_enable_vp(vha);
6762 } else {
6763 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6764 qla2xxx_wake_dpc(base_vha);
6765 qla2x00_wait_for_hba_online(base_vha);
6767 mutex_unlock(&ha->optrom_mutex);
6769 EXPORT_SYMBOL(qlt_enable_vha);
6772 * qla_tgt_disable_vha - NO LOCK HELD
6774 * Disable Target Mode and reset the adapter
6776 static void qlt_disable_vha(struct scsi_qla_host *vha)
6778 struct qla_hw_data *ha = vha->hw;
6779 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6780 unsigned long flags;
6782 if (!tgt) {
6783 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6784 "Unable to locate qla_tgt pointer from"
6785 " struct qla_hw_data\n");
6786 dump_stack();
6787 return;
6790 spin_lock_irqsave(&ha->hardware_lock, flags);
6791 qlt_clear_mode(vha);
6792 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6794 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6795 qla2xxx_wake_dpc(vha);
6796 qla2x00_wait_for_hba_online(vha);
6800 * Called from qla_init.c:qla24xx_vport_create() contex to setup
6801 * the target mode specific struct scsi_qla_host and struct qla_hw_data
6802 * members.
6804 void
6805 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6807 vha->vha_tgt.qla_tgt = NULL;
6809 mutex_init(&vha->vha_tgt.tgt_mutex);
6810 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6812 qlt_clear_mode(vha);
6815 * NOTE: Currently the value is kept the same for <24xx and
6816 * >=24xx ISPs. If it is necessary to change it,
6817 * the check should be added for specific ISPs,
6818 * assigning the value appropriately.
6820 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6822 qlt_add_target(ha, vha);
6826 qlt_rff_id(struct scsi_qla_host *vha)
6828 u8 fc4_feature = 0;
6830 * FC-4 Feature bit 0 indicates target functionality to the name server.
6832 if (qla_tgt_mode_enabled(vha)) {
6833 fc4_feature = BIT_0;
6834 } else if (qla_ini_mode_enabled(vha)) {
6835 fc4_feature = BIT_1;
6836 } else if (qla_dual_mode_enabled(vha))
6837 fc4_feature = BIT_0 | BIT_1;
6839 return fc4_feature;
6843 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6844 * @ha: HA context
6846 * Beginning of ATIO ring has initialization control block already built
6847 * by nvram config routine.
6849 * Returns 0 on success.
6851 void
6852 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6854 struct qla_hw_data *ha = vha->hw;
6855 uint16_t cnt;
6856 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6858 if (qla_ini_mode_enabled(vha))
6859 return;
6861 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6862 pkt->u.raw.signature = ATIO_PROCESSED;
6863 pkt++;
6869 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6870 * @ha: SCSI driver HA context
6872 void
6873 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6875 struct qla_hw_data *ha = vha->hw;
6876 struct atio_from_isp *pkt;
6877 int cnt, i;
6879 if (!ha->flags.fw_started)
6880 return;
6882 while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6883 fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6884 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6885 cnt = pkt->u.raw.entry_count;
6887 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6889 * This packet is corrupted. The header + payload
6890 * can not be trusted. There is no point in passing
6891 * it further up.
6893 ql_log(ql_log_warn, vha, 0xd03c,
6894 "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6895 pkt->u.isp24.fcp_hdr.s_id,
6896 be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6897 le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);
6899 adjust_corrupted_atio(pkt);
6900 qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6901 ha_locked, 0);
6902 } else {
6903 qlt_24xx_atio_pkt_all_vps(vha,
6904 (struct atio_from_isp *)pkt, ha_locked);
6907 for (i = 0; i < cnt; i++) {
6908 ha->tgt.atio_ring_index++;
6909 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6910 ha->tgt.atio_ring_index = 0;
6911 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6912 } else
6913 ha->tgt.atio_ring_ptr++;
6915 pkt->u.raw.signature = ATIO_PROCESSED;
6916 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6918 wmb();
6921 /* Adjust ring index */
6922 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6925 void
6926 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6928 struct qla_hw_data *ha = vha->hw;
6929 struct qla_msix_entry *msix = &ha->msix_entries[2];
6930 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6932 if (!QLA_TGT_MODE_ENABLED())
6933 return;
6935 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
6936 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
6937 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
6939 if (ha->flags.msix_enabled) {
6940 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6941 if (IS_QLA2071(ha)) {
6942 /* 4 ports Baker: Enable Interrupt Handshake */
6943 icb->msix_atio = 0;
6944 icb->firmware_options_2 |= BIT_26;
6945 } else {
6946 icb->msix_atio = cpu_to_le16(msix->entry);
6947 icb->firmware_options_2 &= ~BIT_26;
6949 ql_dbg(ql_dbg_init, vha, 0xf072,
6950 "Registering ICB vector 0x%x for atio que.\n",
6951 msix->entry);
6953 } else {
6954 /* INTx|MSI */
6955 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6956 icb->msix_atio = 0;
6957 icb->firmware_options_2 |= BIT_26;
6958 ql_dbg(ql_dbg_init, vha, 0xf072,
6959 "%s: Use INTx for ATIOQ.\n", __func__);
6964 void
6965 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6967 struct qla_hw_data *ha = vha->hw;
6968 u32 tmp;
6970 if (!QLA_TGT_MODE_ENABLED())
6971 return;
6973 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6974 if (!ha->tgt.saved_set) {
6975 /* We save only once */
6976 ha->tgt.saved_exchange_count = nv->exchange_count;
6977 ha->tgt.saved_firmware_options_1 =
6978 nv->firmware_options_1;
6979 ha->tgt.saved_firmware_options_2 =
6980 nv->firmware_options_2;
6981 ha->tgt.saved_firmware_options_3 =
6982 nv->firmware_options_3;
6983 ha->tgt.saved_set = 1;
6986 if (qla_tgt_mode_enabled(vha))
6987 nv->exchange_count = cpu_to_le16(0xFFFF);
6988 else /* dual */
6989 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6991 /* Enable target mode */
6992 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6994 /* Disable ini mode, if requested */
6995 if (qla_tgt_mode_enabled(vha))
6996 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6998 /* Disable Full Login after LIP */
6999 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7000 /* Enable initial LIP */
7001 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7002 if (ql2xtgt_tape_enable)
7003 /* Enable FC Tape support */
7004 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7005 else
7006 /* Disable FC Tape support */
7007 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7009 /* Disable Full Login after LIP */
7010 nv->host_p &= cpu_to_le32(~BIT_10);
7013 * clear BIT 15 explicitly as we have seen at least
7014 * a couple of instances where this was set and this
7015 * was causing the firmware to not be initialized.
7017 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7018 /* Enable target PRLI control */
7019 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7021 if (IS_QLA25XX(ha)) {
7022 /* Change Loop-prefer to Pt-Pt */
7023 tmp = ~(BIT_4|BIT_5|BIT_6);
7024 nv->firmware_options_2 &= cpu_to_le32(tmp);
7025 tmp = P2P << 4;
7026 nv->firmware_options_2 |= cpu_to_le32(tmp);
7028 } else {
7029 if (ha->tgt.saved_set) {
7030 nv->exchange_count = ha->tgt.saved_exchange_count;
7031 nv->firmware_options_1 =
7032 ha->tgt.saved_firmware_options_1;
7033 nv->firmware_options_2 =
7034 ha->tgt.saved_firmware_options_2;
7035 nv->firmware_options_3 =
7036 ha->tgt.saved_firmware_options_3;
7038 return;
7041 if (ha->base_qpair->enable_class_2) {
7042 if (vha->flags.init_done)
7043 fc_host_supported_classes(vha->host) =
7044 FC_COS_CLASS2 | FC_COS_CLASS3;
7046 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7047 } else {
7048 if (vha->flags.init_done)
7049 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7051 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7055 void
7056 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7057 struct init_cb_24xx *icb)
7059 struct qla_hw_data *ha = vha->hw;
7061 if (!QLA_TGT_MODE_ENABLED())
7062 return;
7064 if (ha->tgt.node_name_set) {
7065 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7066 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7070 void
7071 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7073 struct qla_hw_data *ha = vha->hw;
7074 u32 tmp;
7076 if (!QLA_TGT_MODE_ENABLED())
7077 return;
7079 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7080 if (!ha->tgt.saved_set) {
7081 /* We save only once */
7082 ha->tgt.saved_exchange_count = nv->exchange_count;
7083 ha->tgt.saved_firmware_options_1 =
7084 nv->firmware_options_1;
7085 ha->tgt.saved_firmware_options_2 =
7086 nv->firmware_options_2;
7087 ha->tgt.saved_firmware_options_3 =
7088 nv->firmware_options_3;
7089 ha->tgt.saved_set = 1;
7092 if (qla_tgt_mode_enabled(vha))
7093 nv->exchange_count = cpu_to_le16(0xFFFF);
7094 else /* dual */
7095 nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7097 /* Enable target mode */
7098 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7100 /* Disable ini mode, if requested */
7101 if (qla_tgt_mode_enabled(vha))
7102 nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7103 /* Disable Full Login after LIP */
7104 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7105 /* Enable initial LIP */
7106 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7108 * clear BIT 15 explicitly as we have seen at
7109 * least a couple of instances where this was set
7110 * and this was causing the firmware to not be
7111 * initialized.
7113 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7114 if (ql2xtgt_tape_enable)
7115 /* Enable FC tape support */
7116 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7117 else
7118 /* Disable FC tape support */
7119 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7121 /* Disable Full Login after LIP */
7122 nv->host_p &= cpu_to_le32(~BIT_10);
7123 /* Enable target PRLI control */
7124 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7126 /* Change Loop-prefer to Pt-Pt */
7127 tmp = ~(BIT_4|BIT_5|BIT_6);
7128 nv->firmware_options_2 &= cpu_to_le32(tmp);
7129 tmp = P2P << 4;
7130 nv->firmware_options_2 |= cpu_to_le32(tmp);
7131 } else {
7132 if (ha->tgt.saved_set) {
7133 nv->exchange_count = ha->tgt.saved_exchange_count;
7134 nv->firmware_options_1 =
7135 ha->tgt.saved_firmware_options_1;
7136 nv->firmware_options_2 =
7137 ha->tgt.saved_firmware_options_2;
7138 nv->firmware_options_3 =
7139 ha->tgt.saved_firmware_options_3;
7141 return;
7144 if (ha->base_qpair->enable_class_2) {
7145 if (vha->flags.init_done)
7146 fc_host_supported_classes(vha->host) =
7147 FC_COS_CLASS2 | FC_COS_CLASS3;
7149 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7150 } else {
7151 if (vha->flags.init_done)
7152 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7154 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7158 void
7159 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7160 struct init_cb_81xx *icb)
7162 struct qla_hw_data *ha = vha->hw;
7164 if (!QLA_TGT_MODE_ENABLED())
7165 return;
7167 if (ha->tgt.node_name_set) {
7168 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7169 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7173 void
7174 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7176 if (!QLA_TGT_MODE_ENABLED())
7177 return;
7179 ha->msix_count += 1; /* For ATIO Q */
7183 void
7184 qlt_modify_vp_config(struct scsi_qla_host *vha,
7185 struct vp_config_entry_24xx *vpmod)
7187 /* enable target mode. Bit5 = 1 => disable */
7188 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7189 vpmod->options_idx1 &= ~BIT_5;
7191 /* Disable ini mode, if requested. bit4 = 1 => disable */
7192 if (qla_tgt_mode_enabled(vha))
7193 vpmod->options_idx1 &= ~BIT_4;
7196 void
7197 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7199 int rc;
7201 if (!QLA_TGT_MODE_ENABLED())
7202 return;
7204 if ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
7205 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7206 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7207 } else {
7208 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7209 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7212 mutex_init(&base_vha->vha_tgt.tgt_mutex);
7213 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7215 INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7216 INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7217 qlt_unknown_atio_work_fn);
7219 qlt_clear_mode(base_vha);
7221 rc = btree_init32(&ha->tgt.host_map);
7222 if (rc)
7223 ql_log(ql_log_info, base_vha, 0xd03d,
7224 "Unable to initialize ha->host_map btree\n");
7226 qlt_update_vp_map(base_vha, SET_VP_IDX);
7229 irqreturn_t
7230 qla83xx_msix_atio_q(int irq, void *dev_id)
7232 struct rsp_que *rsp;
7233 scsi_qla_host_t *vha;
7234 struct qla_hw_data *ha;
7235 unsigned long flags;
7237 rsp = (struct rsp_que *) dev_id;
7238 ha = rsp->hw;
7239 vha = pci_get_drvdata(ha->pdev);
7241 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7243 qlt_24xx_process_atio_queue(vha, 0);
7245 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7247 return IRQ_HANDLED;
7250 static void
7251 qlt_handle_abts_recv_work(struct work_struct *work)
7253 struct qla_tgt_sess_op *op = container_of(work,
7254 struct qla_tgt_sess_op, work);
7255 scsi_qla_host_t *vha = op->vha;
7256 struct qla_hw_data *ha = vha->hw;
7257 unsigned long flags;
7259 if (qla2x00_reset_active(vha) ||
7260 (op->chip_reset != ha->base_qpair->chip_reset))
7261 return;
7263 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7264 qlt_24xx_process_atio_queue(vha, 0);
7265 spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7267 spin_lock_irqsave(&ha->hardware_lock, flags);
7268 qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7269 spin_unlock_irqrestore(&ha->hardware_lock, flags);
7271 kfree(op);
7274 void
7275 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7276 response_t *pkt)
7278 struct qla_tgt_sess_op *op;
7280 op = kzalloc(sizeof(*op), GFP_ATOMIC);
7282 if (!op) {
7283 /* do not reach for ATIO queue here. This is best effort err
7284 * recovery at this point.
7286 qlt_response_pkt_all_vps(vha, rsp, pkt);
7287 return;
7290 memcpy(&op->atio, pkt, sizeof(*pkt));
7291 op->vha = vha;
7292 op->chip_reset = vha->hw->base_qpair->chip_reset;
7293 op->rsp = rsp;
7294 INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7295 queue_work(qla_tgt_wq, &op->work);
7296 return;
7300 qlt_mem_alloc(struct qla_hw_data *ha)
7302 if (!QLA_TGT_MODE_ENABLED())
7303 return 0;
7305 ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7306 sizeof(struct qla_tgt_vp_map),
7307 GFP_KERNEL);
7308 if (!ha->tgt.tgt_vp_map)
7309 return -ENOMEM;
7311 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7312 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7313 &ha->tgt.atio_dma, GFP_KERNEL);
7314 if (!ha->tgt.atio_ring) {
7315 kfree(ha->tgt.tgt_vp_map);
7316 return -ENOMEM;
7318 return 0;
7321 void
7322 qlt_mem_free(struct qla_hw_data *ha)
7324 if (!QLA_TGT_MODE_ENABLED())
7325 return;
7327 if (ha->tgt.atio_ring) {
7328 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7329 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7330 ha->tgt.atio_dma);
7332 kfree(ha->tgt.tgt_vp_map);
7335 /* vport_slock to be held by the caller */
7336 void
7337 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7339 void *slot;
7340 u32 key;
7341 int rc;
7343 if (!QLA_TGT_MODE_ENABLED())
7344 return;
7346 key = vha->d_id.b24;
7348 switch (cmd) {
7349 case SET_VP_IDX:
7350 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7351 break;
7352 case SET_AL_PA:
7353 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7354 if (!slot) {
7355 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7356 "Save vha in host_map %p %06x\n", vha, key);
7357 rc = btree_insert32(&vha->hw->tgt.host_map,
7358 key, vha, GFP_ATOMIC);
7359 if (rc)
7360 ql_log(ql_log_info, vha, 0xd03e,
7361 "Unable to insert s_id into host_map: %06x\n",
7362 key);
7363 return;
7365 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7366 "replace existing vha in host_map %p %06x\n", vha, key);
7367 btree_update32(&vha->hw->tgt.host_map, key, vha);
7368 break;
7369 case RESET_VP_IDX:
7370 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7371 break;
7372 case RESET_AL_PA:
7373 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7374 "clear vha in host_map %p %06x\n", vha, key);
7375 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7376 if (slot)
7377 btree_remove32(&vha->hw->tgt.host_map, key);
7378 vha->d_id.b24 = 0;
7379 break;
7383 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7386 if (!vha->d_id.b24) {
7387 vha->d_id = id;
7388 qlt_update_vp_map(vha, SET_AL_PA);
7389 } else if (vha->d_id.b24 != id.b24) {
7390 qlt_update_vp_map(vha, RESET_AL_PA);
7391 vha->d_id = id;
7392 qlt_update_vp_map(vha, SET_AL_PA);
7396 static int __init qlt_parse_ini_mode(void)
7398 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7399 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7400 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7401 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7402 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7403 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7404 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7405 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7406 else
7407 return false;
7409 return true;
7412 int __init qlt_init(void)
7414 int ret;
7416 if (!qlt_parse_ini_mode()) {
7417 ql_log(ql_log_fatal, NULL, 0xe06b,
7418 "qlt_parse_ini_mode() failed\n");
7419 return -EINVAL;
7422 if (!QLA_TGT_MODE_ENABLED())
7423 return 0;
7425 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7426 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7427 qla_tgt_mgmt_cmd), 0, NULL);
7428 if (!qla_tgt_mgmt_cmd_cachep) {
7429 ql_log(ql_log_fatal, NULL, 0xd04b,
7430 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7431 return -ENOMEM;
7434 qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7435 sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7436 0, NULL);
7438 if (!qla_tgt_plogi_cachep) {
7439 ql_log(ql_log_fatal, NULL, 0xe06d,
7440 "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7441 ret = -ENOMEM;
7442 goto out_mgmt_cmd_cachep;
7445 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7446 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7447 if (!qla_tgt_mgmt_cmd_mempool) {
7448 ql_log(ql_log_fatal, NULL, 0xe06e,
7449 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7450 ret = -ENOMEM;
7451 goto out_plogi_cachep;
7454 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7455 if (!qla_tgt_wq) {
7456 ql_log(ql_log_fatal, NULL, 0xe06f,
7457 "alloc_workqueue for qla_tgt_wq failed\n");
7458 ret = -ENOMEM;
7459 goto out_cmd_mempool;
7462 * Return 1 to signal that initiator-mode is being disabled
7464 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7466 out_cmd_mempool:
7467 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7468 out_plogi_cachep:
7469 kmem_cache_destroy(qla_tgt_plogi_cachep);
7470 out_mgmt_cmd_cachep:
7471 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7472 return ret;
7475 void qlt_exit(void)
7477 if (!QLA_TGT_MODE_ENABLED())
7478 return;
7480 destroy_workqueue(qla_tgt_wq);
7481 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7482 kmem_cache_destroy(qla_tgt_plogi_cachep);
7483 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);