Merge tag 'iommu-updates-v5.13' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / scsi / qla2xxx / qla_target.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4  *
5  *  based on qla2x00t.c code:
6  *
7  *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
8  *  Copyright (C) 2004 - 2005 Leonid Stoljar
9  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
10  *  Copyright (C) 2006 - 2010 ID7 Ltd.
11  *
12  *  Forward port and refactoring to modern qla2xxx and target/configfs
13  *
14  *  Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15  */
16
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/blkdev.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
25 #include <linux/workqueue.h>
26 #include <asm/unaligned.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
30
31 #include "qla_def.h"
32 #include "qla_target.h"
33
34 static int ql2xtgt_tape_enable;
35 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(ql2xtgt_tape_enable,
37                 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
38
39 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
40 module_param(qlini_mode, charp, S_IRUGO);
41 MODULE_PARM_DESC(qlini_mode,
42         "Determines when initiator mode will be enabled. Possible values: "
43         "\"exclusive\" - initiator mode will be enabled on load, "
44         "disabled on enabling target mode and then on disabling target mode "
45         "enabled back; "
46         "\"disabled\" - initiator mode will never be enabled; "
47         "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
48         "when ready "
49         "\"enabled\" (default) - initiator mode will always stay enabled.");
50
51 static int ql_dm_tgt_ex_pct = 0;
52 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
53 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
54         "For Dual Mode (qlini_mode=dual), this parameter determines "
55         "the percentage of exchanges/cmds FW will allocate resources "
56         "for Target mode.");
57
58 int ql2xuctrlirq = 1;
59 module_param(ql2xuctrlirq, int, 0644);
60 MODULE_PARM_DESC(ql2xuctrlirq,
61     "User to control IRQ placement via smp_affinity."
62     "Valid with qlini_mode=disabled."
63     "1(default): enable");
64
65 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
66
67 static int qla_sam_status = SAM_STAT_BUSY;
68 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
69
70 /*
71  * From scsi/fc/fc_fcp.h
72  */
73 enum fcp_resp_rsp_codes {
74         FCP_TMF_CMPL = 0,
75         FCP_DATA_LEN_INVALID = 1,
76         FCP_CMND_FIELDS_INVALID = 2,
77         FCP_DATA_PARAM_MISMATCH = 3,
78         FCP_TMF_REJECTED = 4,
79         FCP_TMF_FAILED = 5,
80         FCP_TMF_INVALID_LUN = 9,
81 };
82
83 /*
84  * fc_pri_ta from scsi/fc/fc_fcp.h
85  */
86 #define FCP_PTA_SIMPLE      0   /* simple task attribute */
87 #define FCP_PTA_HEADQ       1   /* head of queue task attribute */
88 #define FCP_PTA_ORDERED     2   /* ordered task attribute */
89 #define FCP_PTA_ACA         4   /* auto. contingent allegiance */
90 #define FCP_PTA_MASK        7   /* mask for task attribute field */
91 #define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
92 #define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */
93
94 /*
95  * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
96  * must be called under HW lock and could unlock/lock it inside.
97  * It isn't an issue, since in the current implementation on the time when
98  * those functions are called:
99  *
100  *   - Either context is IRQ and only IRQ handler can modify HW data,
101  *     including rings related fields,
102  *
103  *   - Or access to target mode variables from struct qla_tgt doesn't
104  *     cross those functions boundaries, except tgt_stop, which
105  *     additionally protected by irq_cmd_count.
106  */
107 /* Predefs for callbacks handed to qla2xxx LLD */
108 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
109         struct atio_from_isp *pkt, uint8_t);
110 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
111         response_t *pkt);
112 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
113         int fn, void *iocb, int flags);
114 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
115         *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
116 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
117         struct atio_from_isp *atio, uint16_t status, int qfull);
118 static void qlt_disable_vha(struct scsi_qla_host *vha);
119 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
120 static void qlt_send_notify_ack(struct qla_qpair *qpair,
121         struct imm_ntfy_from_isp *ntfy,
122         uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
123         uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
124 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
125         struct imm_ntfy_from_isp *imm, int ha_locked);
126 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
127         fc_port_t *fcport, bool local);
128 void qlt_unreg_sess(struct fc_port *sess);
129 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
130         struct abts_recv_from_24xx *);
131 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
132     uint16_t);
133 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
134 static inline uint32_t qlt_make_handle(struct qla_qpair *);
135
136 /*
137  * Global Variables
138  */
139 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
140 struct kmem_cache *qla_tgt_plogi_cachep;
141 static mempool_t *qla_tgt_mgmt_cmd_mempool;
142 static struct workqueue_struct *qla_tgt_wq;
143 static DEFINE_MUTEX(qla_tgt_mutex);
144 static LIST_HEAD(qla_tgt_glist);
145
146 static const char *prot_op_str(u32 prot_op)
147 {
148         switch (prot_op) {
149         case TARGET_PROT_NORMAL:        return "NORMAL";
150         case TARGET_PROT_DIN_INSERT:    return "DIN_INSERT";
151         case TARGET_PROT_DOUT_INSERT:   return "DOUT_INSERT";
152         case TARGET_PROT_DIN_STRIP:     return "DIN_STRIP";
153         case TARGET_PROT_DOUT_STRIP:    return "DOUT_STRIP";
154         case TARGET_PROT_DIN_PASS:      return "DIN_PASS";
155         case TARGET_PROT_DOUT_PASS:     return "DOUT_PASS";
156         default:                        return "UNKNOWN";
157         }
158 }
159
160 /* This API intentionally takes dest as a parameter, rather than returning
161  * int value to avoid caller forgetting to issue wmb() after the store */
162 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
163 {
164         scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
165         *dest = atomic_inc_return(&base_vha->generation_tick);
166         /* memory barrier */
167         wmb();
168 }
169
170 /* Might release hw lock, then reaquire!! */
171 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
172 {
173         /* Send marker if required */
174         if (unlikely(vha->marker_needed != 0)) {
175                 int rc = qla2x00_issue_marker(vha, vha_locked);
176
177                 if (rc != QLA_SUCCESS) {
178                         ql_dbg(ql_dbg_tgt, vha, 0xe03d,
179                             "qla_target(%d): issue_marker() failed\n",
180                             vha->vp_idx);
181                 }
182                 return rc;
183         }
184         return QLA_SUCCESS;
185 }
186
187 static inline
188 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
189                                             be_id_t d_id)
190 {
191         struct scsi_qla_host *host;
192         uint32_t key;
193
194         if (vha->d_id.b.area == d_id.area &&
195             vha->d_id.b.domain == d_id.domain &&
196             vha->d_id.b.al_pa == d_id.al_pa)
197                 return vha;
198
199         key = be_to_port_id(d_id).b24;
200
201         host = btree_lookup32(&vha->hw->tgt.host_map, key);
202         if (!host)
203                 ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
204                     "Unable to find host %06x\n", key);
205
206         return host;
207 }
208
209 static inline
210 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
211         uint16_t vp_idx)
212 {
213         struct qla_hw_data *ha = vha->hw;
214
215         if (vha->vp_idx == vp_idx)
216                 return vha;
217
218         BUG_ON(ha->tgt.tgt_vp_map == NULL);
219         if (likely(test_bit(vp_idx, ha->vp_idx_map)))
220                 return ha->tgt.tgt_vp_map[vp_idx].vha;
221
222         return NULL;
223 }
224
225 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
226 {
227         unsigned long flags;
228
229         spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
230
231         vha->hw->tgt.num_pend_cmds++;
232         if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
233                 vha->qla_stats.stat_max_pend_cmds =
234                         vha->hw->tgt.num_pend_cmds;
235         spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
236 }
237 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
238 {
239         unsigned long flags;
240
241         spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
242         vha->hw->tgt.num_pend_cmds--;
243         spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
244 }
245
246
247 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
248         struct atio_from_isp *atio, uint8_t ha_locked)
249 {
250         struct qla_tgt_sess_op *u;
251         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
252         unsigned long flags;
253
254         if (tgt->tgt_stop) {
255                 ql_dbg(ql_dbg_async, vha, 0x502c,
256                     "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
257                     vha->vp_idx);
258                 goto out_term;
259         }
260
261         u = kzalloc(sizeof(*u), GFP_ATOMIC);
262         if (u == NULL)
263                 goto out_term;
264
265         u->vha = vha;
266         memcpy(&u->atio, atio, sizeof(*atio));
267         INIT_LIST_HEAD(&u->cmd_list);
268
269         spin_lock_irqsave(&vha->cmd_list_lock, flags);
270         list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
271         spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
272
273         schedule_delayed_work(&vha->unknown_atio_work, 1);
274
275 out:
276         return;
277
278 out_term:
279         qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
280         goto out;
281 }
282
283 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
284         uint8_t ha_locked)
285 {
286         struct qla_tgt_sess_op *u, *t;
287         scsi_qla_host_t *host;
288         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
289         unsigned long flags;
290         uint8_t queued = 0;
291
292         list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
293                 if (u->aborted) {
294                         ql_dbg(ql_dbg_async, vha, 0x502e,
295                             "Freeing unknown %s %p, because of Abort\n",
296                             "ATIO_TYPE7", u);
297                         qlt_send_term_exchange(vha->hw->base_qpair, NULL,
298                             &u->atio, ha_locked, 0);
299                         goto abort;
300                 }
301
302                 host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
303                 if (host != NULL) {
304                         ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
305                             "Requeuing unknown ATIO_TYPE7 %p\n", u);
306                         qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
307                 } else if (tgt->tgt_stop) {
308                         ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
309                             "Freeing unknown %s %p, because tgt is being stopped\n",
310                             "ATIO_TYPE7", u);
311                         qlt_send_term_exchange(vha->hw->base_qpair, NULL,
312                             &u->atio, ha_locked, 0);
313                 } else {
314                         ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
315                             "Reschedule u %p, vha %p, host %p\n", u, vha, host);
316                         if (!queued) {
317                                 queued = 1;
318                                 schedule_delayed_work(&vha->unknown_atio_work,
319                                     1);
320                         }
321                         continue;
322                 }
323
324 abort:
325                 spin_lock_irqsave(&vha->cmd_list_lock, flags);
326                 list_del(&u->cmd_list);
327                 spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
328                 kfree(u);
329         }
330 }
331
332 void qlt_unknown_atio_work_fn(struct work_struct *work)
333 {
334         struct scsi_qla_host *vha = container_of(to_delayed_work(work),
335             struct scsi_qla_host, unknown_atio_work);
336
337         qlt_try_to_dequeue_unknown_atios(vha, 0);
338 }
339
340 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
341         struct atio_from_isp *atio, uint8_t ha_locked)
342 {
343         ql_dbg(ql_dbg_tgt, vha, 0xe072,
344                 "%s: qla_target(%d): type %x ox_id %04x\n",
345                 __func__, vha->vp_idx, atio->u.raw.entry_type,
346                 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
347
348         switch (atio->u.raw.entry_type) {
349         case ATIO_TYPE7:
350         {
351                 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
352                     atio->u.isp24.fcp_hdr.d_id);
353                 if (unlikely(NULL == host)) {
354                         ql_dbg(ql_dbg_tgt, vha, 0xe03e,
355                             "qla_target(%d): Received ATIO_TYPE7 "
356                             "with unknown d_id %x:%x:%x\n", vha->vp_idx,
357                             atio->u.isp24.fcp_hdr.d_id.domain,
358                             atio->u.isp24.fcp_hdr.d_id.area,
359                             atio->u.isp24.fcp_hdr.d_id.al_pa);
360
361
362                         qlt_queue_unknown_atio(vha, atio, ha_locked);
363                         break;
364                 }
365                 if (unlikely(!list_empty(&vha->unknown_atio_list)))
366                         qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
367
368                 qlt_24xx_atio_pkt(host, atio, ha_locked);
369                 break;
370         }
371
372         case IMMED_NOTIFY_TYPE:
373         {
374                 struct scsi_qla_host *host = vha;
375                 struct imm_ntfy_from_isp *entry =
376                     (struct imm_ntfy_from_isp *)atio;
377
378                 qlt_issue_marker(vha, ha_locked);
379
380                 if ((entry->u.isp24.vp_index != 0xFF) &&
381                     (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
382                         host = qlt_find_host_by_vp_idx(vha,
383                             entry->u.isp24.vp_index);
384                         if (unlikely(!host)) {
385                                 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
386                                     "qla_target(%d): Received "
387                                     "ATIO (IMMED_NOTIFY_TYPE) "
388                                     "with unknown vp_index %d\n",
389                                     vha->vp_idx, entry->u.isp24.vp_index);
390                                 break;
391                         }
392                 }
393                 qlt_24xx_atio_pkt(host, atio, ha_locked);
394                 break;
395         }
396
397         case VP_RPT_ID_IOCB_TYPE:
398                 qla24xx_report_id_acquisition(vha,
399                         (struct vp_rpt_id_entry_24xx *)atio);
400                 break;
401
402         case ABTS_RECV_24XX:
403         {
404                 struct abts_recv_from_24xx *entry =
405                         (struct abts_recv_from_24xx *)atio;
406                 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
407                         entry->vp_index);
408                 unsigned long flags;
409
410                 if (unlikely(!host)) {
411                         ql_dbg(ql_dbg_tgt, vha, 0xe00a,
412                             "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
413                             "received, with unknown vp_index %d\n",
414                             vha->vp_idx, entry->vp_index);
415                         break;
416                 }
417                 if (!ha_locked)
418                         spin_lock_irqsave(&host->hw->hardware_lock, flags);
419                 qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
420                 if (!ha_locked)
421                         spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
422                 break;
423         }
424
425         /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
426
427         default:
428                 ql_dbg(ql_dbg_tgt, vha, 0xe040,
429                     "qla_target(%d): Received unknown ATIO atio "
430                     "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
431                 break;
432         }
433
434         return false;
435 }
436
437 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
438         struct rsp_que *rsp, response_t *pkt)
439 {
440         switch (pkt->entry_type) {
441         case CTIO_CRC2:
442                 ql_dbg(ql_dbg_tgt, vha, 0xe073,
443                         "qla_target(%d):%s: CRC2 Response pkt\n",
444                         vha->vp_idx, __func__);
445                 fallthrough;
446         case CTIO_TYPE7:
447         {
448                 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
449                 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
450                     entry->vp_index);
451                 if (unlikely(!host)) {
452                         ql_dbg(ql_dbg_tgt, vha, 0xe041,
453                             "qla_target(%d): Response pkt (CTIO_TYPE7) "
454                             "received, with unknown vp_index %d\n",
455                             vha->vp_idx, entry->vp_index);
456                         break;
457                 }
458                 qlt_response_pkt(host, rsp, pkt);
459                 break;
460         }
461
462         case IMMED_NOTIFY_TYPE:
463         {
464                 struct scsi_qla_host *host;
465                 struct imm_ntfy_from_isp *entry =
466                     (struct imm_ntfy_from_isp *)pkt;
467
468                 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
469                 if (unlikely(!host)) {
470                         ql_dbg(ql_dbg_tgt, vha, 0xe042,
471                             "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
472                             "received, with unknown vp_index %d\n",
473                             vha->vp_idx, entry->u.isp24.vp_index);
474                         break;
475                 }
476                 qlt_response_pkt(host, rsp, pkt);
477                 break;
478         }
479
480         case NOTIFY_ACK_TYPE:
481         {
482                 struct scsi_qla_host *host = vha;
483                 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
484
485                 if (0xFF != entry->u.isp24.vp_index) {
486                         host = qlt_find_host_by_vp_idx(vha,
487                             entry->u.isp24.vp_index);
488                         if (unlikely(!host)) {
489                                 ql_dbg(ql_dbg_tgt, vha, 0xe043,
490                                     "qla_target(%d): Response "
491                                     "pkt (NOTIFY_ACK_TYPE) "
492                                     "received, with unknown "
493                                     "vp_index %d\n", vha->vp_idx,
494                                     entry->u.isp24.vp_index);
495                                 break;
496                         }
497                 }
498                 qlt_response_pkt(host, rsp, pkt);
499                 break;
500         }
501
502         case ABTS_RECV_24XX:
503         {
504                 struct abts_recv_from_24xx *entry =
505                     (struct abts_recv_from_24xx *)pkt;
506                 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
507                     entry->vp_index);
508                 if (unlikely(!host)) {
509                         ql_dbg(ql_dbg_tgt, vha, 0xe044,
510                             "qla_target(%d): Response pkt "
511                             "(ABTS_RECV_24XX) received, with unknown "
512                             "vp_index %d\n", vha->vp_idx, entry->vp_index);
513                         break;
514                 }
515                 qlt_response_pkt(host, rsp, pkt);
516                 break;
517         }
518
519         case ABTS_RESP_24XX:
520         {
521                 struct abts_resp_to_24xx *entry =
522                     (struct abts_resp_to_24xx *)pkt;
523                 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
524                     entry->vp_index);
525                 if (unlikely(!host)) {
526                         ql_dbg(ql_dbg_tgt, vha, 0xe045,
527                             "qla_target(%d): Response pkt "
528                             "(ABTS_RECV_24XX) received, with unknown "
529                             "vp_index %d\n", vha->vp_idx, entry->vp_index);
530                         break;
531                 }
532                 qlt_response_pkt(host, rsp, pkt);
533                 break;
534         }
535         default:
536                 qlt_response_pkt(vha, rsp, pkt);
537                 break;
538         }
539
540 }
541
542 /*
543  * All qlt_plogi_ack_t operations are protected by hardware_lock
544  */
545 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
546         struct imm_ntfy_from_isp *ntfy, int type)
547 {
548         struct qla_work_evt *e;
549
550         e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
551         if (!e)
552                 return QLA_FUNCTION_FAILED;
553
554         e->u.nack.fcport = fcport;
555         e->u.nack.type = type;
556         memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
557         return qla2x00_post_work(vha, e);
558 }
559
560 static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
561 {
562         struct scsi_qla_host *vha = sp->vha;
563         unsigned long flags;
564
565         ql_dbg(ql_dbg_disc, vha, 0x20f2,
566             "Async done-%s res %x %8phC  type %d\n",
567             sp->name, res, sp->fcport->port_name, sp->type);
568
569         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
570         sp->fcport->flags &= ~FCF_ASYNC_SENT;
571         sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
572
573         switch (sp->type) {
574         case SRB_NACK_PLOGI:
575                 sp->fcport->login_gen++;
576                 sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
577                 sp->fcport->logout_on_delete = 1;
578                 sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
579                 sp->fcport->send_els_logo = 0;
580                 break;
581
582         case SRB_NACK_PRLI:
583                 sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
584                 sp->fcport->deleted = 0;
585                 sp->fcport->send_els_logo = 0;
586
587                 if (!sp->fcport->login_succ &&
588                     !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
589                         sp->fcport->login_succ = 1;
590
591                         vha->fcport_count++;
592                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
593                         qla24xx_sched_upd_fcport(sp->fcport);
594                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
595                 } else {
596                         sp->fcport->login_retry = 0;
597                         qla2x00_set_fcport_disc_state(sp->fcport,
598                             DSC_LOGIN_COMPLETE);
599                         sp->fcport->deleted = 0;
600                         sp->fcport->logout_on_delete = 1;
601                 }
602                 break;
603
604         case SRB_NACK_LOGO:
605                 sp->fcport->login_gen++;
606                 sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
607                 qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
608                 break;
609         }
610         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
611
612         sp->free(sp);
613 }
614
615 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
616         struct imm_ntfy_from_isp *ntfy, int type)
617 {
618         int rval = QLA_FUNCTION_FAILED;
619         srb_t *sp;
620         char *c = NULL;
621
622         fcport->flags |= FCF_ASYNC_SENT;
623         switch (type) {
624         case SRB_NACK_PLOGI:
625                 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
626                 c = "PLOGI";
627                 break;
628         case SRB_NACK_PRLI:
629                 fcport->fw_login_state = DSC_LS_PRLI_PEND;
630                 fcport->deleted = 0;
631                 c = "PRLI";
632                 break;
633         case SRB_NACK_LOGO:
634                 fcport->fw_login_state = DSC_LS_LOGO_PEND;
635                 c = "LOGO";
636                 break;
637         }
638
639         sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
640         if (!sp)
641                 goto done;
642
643         sp->type = type;
644         sp->name = "nack";
645
646         sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
647         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
648
649         sp->u.iocb_cmd.u.nack.ntfy = ntfy;
650         sp->done = qla2x00_async_nack_sp_done;
651
652         ql_dbg(ql_dbg_disc, vha, 0x20f4,
653             "Async-%s %8phC hndl %x %s\n",
654             sp->name, fcport->port_name, sp->handle, c);
655
656         rval = qla2x00_start_sp(sp);
657         if (rval != QLA_SUCCESS)
658                 goto done_free_sp;
659
660         return rval;
661
662 done_free_sp:
663         sp->free(sp);
664 done:
665         fcport->flags &= ~FCF_ASYNC_SENT;
666         return rval;
667 }
668
669 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
670 {
671         fc_port_t *t;
672
673         switch (e->u.nack.type) {
674         case SRB_NACK_PRLI:
675                 t = e->u.nack.fcport;
676                 flush_work(&t->del_work);
677                 flush_work(&t->free_work);
678                 mutex_lock(&vha->vha_tgt.tgt_mutex);
679                 t = qlt_create_sess(vha, e->u.nack.fcport, 0);
680                 mutex_unlock(&vha->vha_tgt.tgt_mutex);
681                 if (t) {
682                         ql_log(ql_log_info, vha, 0xd034,
683                             "%s create sess success %p", __func__, t);
684                         /* create sess has an extra kref */
685                         vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
686                 }
687                 break;
688         }
689         qla24xx_async_notify_ack(vha, e->u.nack.fcport,
690             (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
691 }
692
693 void qla24xx_delete_sess_fn(struct work_struct *work)
694 {
695         fc_port_t *fcport = container_of(work, struct fc_port, del_work);
696         struct qla_hw_data *ha = fcport->vha->hw;
697
698         if (fcport->se_sess) {
699                 ha->tgt.tgt_ops->shutdown_sess(fcport);
700                 ha->tgt.tgt_ops->put_sess(fcport);
701         } else {
702                 qlt_unreg_sess(fcport);
703         }
704 }
705
706 /*
707  * Called from qla2x00_reg_remote_port()
708  */
709 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
710 {
711         struct qla_hw_data *ha = vha->hw;
712         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
713         struct fc_port *sess = fcport;
714         unsigned long flags;
715
716         if (!vha->hw->tgt.tgt_ops)
717                 return;
718
719         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
720         if (tgt->tgt_stop) {
721                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
722                 return;
723         }
724
725         if (fcport->disc_state == DSC_DELETE_PEND) {
726                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
727                 return;
728         }
729
730         if (!sess->se_sess) {
731                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
732
733                 mutex_lock(&vha->vha_tgt.tgt_mutex);
734                 sess = qlt_create_sess(vha, fcport, false);
735                 mutex_unlock(&vha->vha_tgt.tgt_mutex);
736
737                 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
738         } else {
739                 if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
740                         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
741                         return;
742                 }
743
744                 if (!kref_get_unless_zero(&sess->sess_kref)) {
745                         ql_dbg(ql_dbg_disc, vha, 0x2107,
746                             "%s: kref_get fail sess %8phC \n",
747                             __func__, sess->port_name);
748                         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
749                         return;
750                 }
751
752                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
753                     "qla_target(%u): %ssession for port %8phC "
754                     "(loop ID %d) reappeared\n", vha->vp_idx,
755                     sess->local ? "local " : "", sess->port_name, sess->loop_id);
756
757                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
758                     "Reappeared sess %p\n", sess);
759
760                 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
761                     fcport->loop_id,
762                     (fcport->flags & FCF_CONF_COMP_SUPPORTED));
763         }
764
765         if (sess && sess->local) {
766                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
767                     "qla_target(%u): local session for "
768                     "port %8phC (loop ID %d) became global\n", vha->vp_idx,
769                     fcport->port_name, sess->loop_id);
770                 sess->local = 0;
771         }
772         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
773
774         ha->tgt.tgt_ops->put_sess(sess);
775 }
776
777 /*
778  * This is a zero-base ref-counting solution, since hardware_lock
779  * guarantees that ref_count is not modified concurrently.
780  * Upon successful return content of iocb is undefined
781  */
782 static struct qlt_plogi_ack_t *
783 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
784                        struct imm_ntfy_from_isp *iocb)
785 {
786         struct qlt_plogi_ack_t *pla;
787
788         lockdep_assert_held(&vha->hw->hardware_lock);
789
790         list_for_each_entry(pla, &vha->plogi_ack_list, list) {
791                 if (pla->id.b24 == id->b24) {
792                         ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
793                             "%s %d %8phC Term INOT due to new INOT",
794                             __func__, __LINE__,
795                             pla->iocb.u.isp24.port_name);
796                         qlt_send_term_imm_notif(vha, &pla->iocb, 1);
797                         memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
798                         return pla;
799                 }
800         }
801
802         pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
803         if (!pla) {
804                 ql_dbg(ql_dbg_async, vha, 0x5088,
805                        "qla_target(%d): Allocation of plogi_ack failed\n",
806                        vha->vp_idx);
807                 return NULL;
808         }
809
810         memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
811         pla->id = *id;
812         list_add_tail(&pla->list, &vha->plogi_ack_list);
813
814         return pla;
815 }
816
817 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
818     struct qlt_plogi_ack_t *pla)
819 {
820         struct imm_ntfy_from_isp *iocb = &pla->iocb;
821         port_id_t port_id;
822         uint16_t loop_id;
823         fc_port_t *fcport = pla->fcport;
824
825         BUG_ON(!pla->ref_count);
826         pla->ref_count--;
827
828         if (pla->ref_count)
829                 return;
830
831         ql_dbg(ql_dbg_disc, vha, 0x5089,
832             "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
833             " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
834             iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
835             iocb->u.isp24.port_id[0],
836             le16_to_cpu(iocb->u.isp24.nport_handle),
837             iocb->u.isp24.exchange_address, iocb->ox_id);
838
839         port_id.b.domain = iocb->u.isp24.port_id[2];
840         port_id.b.area   = iocb->u.isp24.port_id[1];
841         port_id.b.al_pa  = iocb->u.isp24.port_id[0];
842         port_id.b.rsvd_1 = 0;
843
844         loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
845
846         fcport->loop_id = loop_id;
847         fcport->d_id = port_id;
848         if (iocb->u.isp24.status_subcode == ELS_PLOGI)
849                 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
850         else
851                 qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
852
853         list_for_each_entry(fcport, &vha->vp_fcports, list) {
854                 if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
855                         fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
856                 if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
857                         fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
858         }
859
860         list_del(&pla->list);
861         kmem_cache_free(qla_tgt_plogi_cachep, pla);
862 }
863
864 void
865 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
866     struct fc_port *sess, enum qlt_plogi_link_t link)
867 {
868         struct imm_ntfy_from_isp *iocb = &pla->iocb;
869         /* Inc ref_count first because link might already be pointing at pla */
870         pla->ref_count++;
871
872         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
873                 "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
874                 " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
875                 sess, link, sess->port_name,
876                 iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
877                 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
878                 pla->ref_count, pla, link);
879
880         if (link == QLT_PLOGI_LINK_CONFLICT) {
881                 switch (sess->disc_state) {
882                 case DSC_DELETED:
883                 case DSC_DELETE_PEND:
884                         pla->ref_count--;
885                         return;
886                 default:
887                         break;
888                 }
889         }
890
891         if (sess->plogi_link[link])
892                 qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
893
894         if (link == QLT_PLOGI_LINK_SAME_WWN)
895                 pla->fcport = sess;
896
897         sess->plogi_link[link] = pla;
898 }
899
900 typedef struct {
901         /* These fields must be initialized by the caller */
902         port_id_t id;
903         /*
904          * number of cmds dropped while we were waiting for
905          * initiator to ack LOGO initialize to 1 if LOGO is
906          * triggered by a command, otherwise, to 0
907          */
908         int cmd_count;
909
910         /* These fields are used by callee */
911         struct list_head list;
912 } qlt_port_logo_t;
913
914 static void
915 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
916 {
917         qlt_port_logo_t *tmp;
918         int res;
919
920         mutex_lock(&vha->vha_tgt.tgt_mutex);
921
922         list_for_each_entry(tmp, &vha->logo_list, list) {
923                 if (tmp->id.b24 == logo->id.b24) {
924                         tmp->cmd_count += logo->cmd_count;
925                         mutex_unlock(&vha->vha_tgt.tgt_mutex);
926                         return;
927                 }
928         }
929
930         list_add_tail(&logo->list, &vha->logo_list);
931
932         mutex_unlock(&vha->vha_tgt.tgt_mutex);
933
934         res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
935
936         mutex_lock(&vha->vha_tgt.tgt_mutex);
937         list_del(&logo->list);
938         mutex_unlock(&vha->vha_tgt.tgt_mutex);
939
940         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
941             "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
942             logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
943             logo->cmd_count, res);
944 }
945
946 void qlt_free_session_done(struct work_struct *work)
947 {
948         struct fc_port *sess = container_of(work, struct fc_port,
949             free_work);
950         struct qla_tgt *tgt = sess->tgt;
951         struct scsi_qla_host *vha = sess->vha;
952         struct qla_hw_data *ha = vha->hw;
953         unsigned long flags;
954         bool logout_started = false;
955         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
956         struct qlt_plogi_ack_t *own =
957                 sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
958
959         ql_dbg(ql_dbg_disc, vha, 0xf084,
960                 "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
961                 " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
962                 __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
963                 sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
964                 sess->logout_on_delete, sess->keep_nport_handle,
965                 sess->send_els_logo);
966
967         if (!IS_SW_RESV_ADDR(sess->d_id)) {
968                 qla2x00_mark_device_lost(vha, sess, 0);
969
970                 if (sess->send_els_logo) {
971                         qlt_port_logo_t logo;
972
973                         logo.id = sess->d_id;
974                         logo.cmd_count = 0;
975                         if (!own)
976                                 qlt_send_first_logo(vha, &logo);
977                         sess->send_els_logo = 0;
978                 }
979
980                 if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
981                         int rc;
982
983                         if (!own ||
984                              (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
985                                 rc = qla2x00_post_async_logout_work(vha, sess,
986                                     NULL);
987                                 if (rc != QLA_SUCCESS)
988                                         ql_log(ql_log_warn, vha, 0xf085,
989                                             "Schedule logo failed sess %p rc %d\n",
990                                             sess, rc);
991                                 else
992                                         logout_started = true;
993                         } else if (own && (own->iocb.u.isp24.status_subcode ==
994                                 ELS_PRLI) && ha->flags.rida_fmt2) {
995                                 rc = qla2x00_post_async_prlo_work(vha, sess,
996                                     NULL);
997                                 if (rc != QLA_SUCCESS)
998                                         ql_log(ql_log_warn, vha, 0xf085,
999                                             "Schedule PRLO failed sess %p rc %d\n",
1000                                             sess, rc);
1001                                 else
1002                                         logout_started = true;
1003                         }
1004                 } /* if sess->logout_on_delete */
1005
1006                 if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1007                     !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1008                         sess->nvme_flag |= NVME_FLAG_DELETING;
1009                         qla_nvme_unregister_remote_port(sess);
1010                 }
1011         }
1012
1013         /*
1014          * Release the target session for FC Nexus from fabric module code.
1015          */
1016         if (sess->se_sess != NULL)
1017                 ha->tgt.tgt_ops->free_session(sess);
1018
1019         if (logout_started) {
1020                 bool traced = false;
1021                 u16 cnt = 0;
1022
1023                 while (!READ_ONCE(sess->logout_completed)) {
1024                         if (!traced) {
1025                                 ql_dbg(ql_dbg_disc, vha, 0xf086,
1026                                         "%s: waiting for sess %p logout\n",
1027                                         __func__, sess);
1028                                 traced = true;
1029                         }
1030                         msleep(100);
1031                         cnt++;
1032                         /*
1033                          * Driver timeout is set to 22 Sec, update count value to loop
1034                          * long enough for log-out to complete before advancing. Otherwise,
1035                          * straddling logout can interfere with re-login attempt.
1036                          */
1037                         if (cnt > 230)
1038                                 break;
1039                 }
1040
1041                 ql_dbg(ql_dbg_disc, vha, 0xf087,
1042                     "%s: sess %p logout completed\n", __func__, sess);
1043         }
1044
1045         if (sess->logo_ack_needed) {
1046                 sess->logo_ack_needed = 0;
1047                 qla24xx_async_notify_ack(vha, sess,
1048                         (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1049         }
1050
1051         spin_lock_irqsave(&vha->work_lock, flags);
1052         sess->flags &= ~FCF_ASYNC_SENT;
1053         spin_unlock_irqrestore(&vha->work_lock, flags);
1054
1055         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1056         if (sess->se_sess) {
1057                 sess->se_sess = NULL;
1058                 if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1059                         tgt->sess_count--;
1060         }
1061
1062         qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1063         sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1064         sess->deleted = QLA_SESS_DELETED;
1065
1066         if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1067                 vha->fcport_count--;
1068                 sess->login_succ = 0;
1069         }
1070
1071         qla2x00_clear_loop_id(sess);
1072
1073         if (sess->conflict) {
1074                 sess->conflict->login_pause = 0;
1075                 sess->conflict = NULL;
1076                 if (!test_bit(UNLOADING, &vha->dpc_flags))
1077                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1078         }
1079
1080         {
1081                 struct qlt_plogi_ack_t *con =
1082                     sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1083                 struct imm_ntfy_from_isp *iocb;
1084
1085                 own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1086
1087                 if (con) {
1088                         iocb = &con->iocb;
1089                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1090                                  "se_sess %p / sess %p port %8phC is gone,"
1091                                  " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1092                                  sess->se_sess, sess, sess->port_name,
1093                                  own ? "releasing own PLOGI" : "no own PLOGI pending",
1094                                  own ? own->ref_count : -1,
1095                                  iocb->u.isp24.port_name, con->ref_count);
1096                         qlt_plogi_ack_unref(vha, con);
1097                         sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1098                 } else {
1099                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1100                             "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1101                             sess->se_sess, sess, sess->port_name,
1102                             own ? "releasing own PLOGI" :
1103                             "no own PLOGI pending",
1104                             own ? own->ref_count : -1);
1105                 }
1106
1107                 if (own) {
1108                         sess->fw_login_state = DSC_LS_PLOGI_PEND;
1109                         qlt_plogi_ack_unref(vha, own);
1110                         sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1111                 }
1112         }
1113
1114         sess->explicit_logout = 0;
1115         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1116         sess->free_pending = 0;
1117
1118         qla2x00_dfs_remove_rport(vha, sess);
1119
1120         ql_dbg(ql_dbg_disc, vha, 0xf001,
1121             "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1122                 sess, sess->port_name, vha->fcport_count);
1123
1124         if (tgt && (tgt->sess_count == 0))
1125                 wake_up_all(&tgt->waitQ);
1126
1127         if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1128             !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1129             (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1130                 switch (vha->host->active_mode) {
1131                 case MODE_INITIATOR:
1132                 case MODE_DUAL:
1133                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1134                         qla2xxx_wake_dpc(vha);
1135                         break;
1136                 case MODE_TARGET:
1137                 default:
1138                         /* no-op */
1139                         break;
1140                 }
1141         }
1142
1143         if (vha->fcport_count == 0)
1144                 wake_up_all(&vha->fcport_waitQ);
1145 }
1146
1147 /* ha->tgt.sess_lock supposed to be held on entry */
1148 void qlt_unreg_sess(struct fc_port *sess)
1149 {
1150         struct scsi_qla_host *vha = sess->vha;
1151         unsigned long flags;
1152
1153         ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1154             "%s sess %p for deletion %8phC\n",
1155             __func__, sess, sess->port_name);
1156
1157         spin_lock_irqsave(&sess->vha->work_lock, flags);
1158         if (sess->free_pending) {
1159                 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1160                 return;
1161         }
1162         sess->free_pending = 1;
1163         /*
1164          * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1165          * management from being sent.
1166          */
1167         sess->flags |= FCF_ASYNC_SENT;
1168         spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1169
1170         if (sess->se_sess)
1171                 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1172
1173         sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1174         qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1175         sess->last_rscn_gen = sess->rscn_gen;
1176         sess->last_login_gen = sess->login_gen;
1177
1178         queue_work(sess->vha->hw->wq, &sess->free_work);
1179 }
1180 EXPORT_SYMBOL(qlt_unreg_sess);
1181
1182 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1183 {
1184         struct qla_hw_data *ha = vha->hw;
1185         struct fc_port *sess = NULL;
1186         uint16_t loop_id;
1187         int res = 0;
1188         struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1189         unsigned long flags;
1190
1191         loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1192         if (loop_id == 0xFFFF) {
1193                 /* Global event */
1194                 atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1195                 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1196                 qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1197                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1198         } else {
1199                 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1200                 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1201                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1202         }
1203
1204         ql_dbg(ql_dbg_tgt, vha, 0xe000,
1205             "Using sess for qla_tgt_reset: %p\n", sess);
1206         if (!sess) {
1207                 res = -ESRCH;
1208                 return res;
1209         }
1210
1211         ql_dbg(ql_dbg_tgt, vha, 0xe047,
1212             "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1213             "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1214             mcmd, loop_id);
1215
1216         return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1217 }
1218
1219 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1220 {
1221         if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1222                 sess->logout_on_delete = 0;
1223                 sess->logo_ack_needed = 0;
1224                 sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1225         }
1226 }
1227
1228 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1229 {
1230         struct qla_tgt *tgt = sess->tgt;
1231         unsigned long flags;
1232         u16 sec;
1233
1234         switch (sess->disc_state) {
1235         case DSC_DELETE_PEND:
1236                 return;
1237         case DSC_DELETED:
1238                 if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1239                         !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1240                         if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1241                                 wake_up_all(&tgt->waitQ);
1242
1243                         if (sess->vha->fcport_count == 0)
1244                                 wake_up_all(&sess->vha->fcport_waitQ);
1245                         return;
1246                 }
1247                 break;
1248         case DSC_UPD_FCPORT:
1249                 /*
1250                  * This port is not done reporting to upper layer.
1251                  * let it finish
1252                  */
1253                 sess->next_disc_state = DSC_DELETE_PEND;
1254                 sec = jiffies_to_msecs(jiffies -
1255                     sess->jiffies_at_registration)/1000;
1256                 if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1257                         sess->sec_since_registration = sec;
1258                         ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1259                             "%s %8phC : Slow Rport registration(%d Sec)\n",
1260                             __func__, sess->port_name, sec);
1261                 }
1262                 return;
1263         default:
1264                 break;
1265         }
1266
1267         spin_lock_irqsave(&sess->vha->work_lock, flags);
1268         if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1269                 spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1270                 return;
1271         }
1272         sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1273         spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1274
1275         sess->prli_pend_timer = 0;
1276         qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1277
1278         qla24xx_chk_fcp_state(sess);
1279
1280         ql_dbg(ql_log_warn, sess->vha, 0xe001,
1281             "Scheduling sess %p for deletion %8phC\n",
1282             sess, sess->port_name);
1283
1284         WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1285 }
1286
1287 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1288 {
1289         struct fc_port *sess;
1290         scsi_qla_host_t *vha = tgt->vha;
1291
1292         list_for_each_entry(sess, &vha->vp_fcports, list) {
1293                 if (sess->se_sess)
1294                         qlt_schedule_sess_for_deletion(sess);
1295         }
1296
1297         /* At this point tgt could be already dead */
1298 }
1299
1300 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1301         uint16_t *loop_id)
1302 {
1303         struct qla_hw_data *ha = vha->hw;
1304         dma_addr_t gid_list_dma;
1305         struct gid_list_info *gid_list, *gid;
1306         int res, rc, i;
1307         uint16_t entries;
1308
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;
1316         }
1317
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;
1326         }
1327
1328         gid = gid_list;
1329         res = -ENOENT;
1330         for (i = 0; i < entries; i++) {
1331                 if (gid->al_pa == s_id.al_pa &&
1332                     gid->area == s_id.area &&
1333                     gid->domain == s_id.domain) {
1334                         *loop_id = le16_to_cpu(gid->loop_id);
1335                         res = 0;
1336                         break;
1337                 }
1338                 gid = (void *)gid + ha->gid_list_info_size;
1339         }
1340
1341 out_free_id_list:
1342         dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1343             gid_list, gid_list_dma);
1344         return res;
1345 }
1346
1347 /*
1348  * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1349  * Caller must put it.
1350  */
1351 static struct fc_port *qlt_create_sess(
1352         struct scsi_qla_host *vha,
1353         fc_port_t *fcport,
1354         bool local)
1355 {
1356         struct qla_hw_data *ha = vha->hw;
1357         struct fc_port *sess = fcport;
1358         unsigned long flags;
1359
1360         if (vha->vha_tgt.qla_tgt->tgt_stop)
1361                 return NULL;
1362
1363         if (fcport->se_sess) {
1364                 if (!kref_get_unless_zero(&sess->sess_kref)) {
1365                         ql_dbg(ql_dbg_disc, vha, 0x20f6,
1366                             "%s: kref_get_unless_zero failed for %8phC\n",
1367                             __func__, sess->port_name);
1368                         return NULL;
1369                 }
1370                 return fcport;
1371         }
1372         sess->tgt = vha->vha_tgt.qla_tgt;
1373         sess->local = local;
1374
1375         /*
1376          * Under normal circumstances we want to logout from firmware when
1377          * session eventually ends and release corresponding nport handle.
1378          * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1379          * code will adjust these flags as necessary.
1380          */
1381         sess->logout_on_delete = 1;
1382         sess->keep_nport_handle = 0;
1383         sess->logout_completed = 0;
1384
1385         if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1386             &fcport->port_name[0], sess) < 0) {
1387                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1388                     "(%d) %8phC check_initiator_node_acl failed\n",
1389                     vha->vp_idx, fcport->port_name);
1390                 return NULL;
1391         } else {
1392                 kref_init(&fcport->sess_kref);
1393                 /*
1394                  * Take an extra reference to ->sess_kref here to handle
1395                  * fc_port access across ->tgt.sess_lock reaquire.
1396                  */
1397                 if (!kref_get_unless_zero(&sess->sess_kref)) {
1398                         ql_dbg(ql_dbg_disc, vha, 0x20f7,
1399                             "%s: kref_get_unless_zero failed for %8phC\n",
1400                             __func__, sess->port_name);
1401                         return NULL;
1402                 }
1403
1404                 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1405                 if (!IS_SW_RESV_ADDR(sess->d_id))
1406                         vha->vha_tgt.qla_tgt->sess_count++;
1407
1408                 qlt_do_generation_tick(vha, &sess->generation);
1409                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1410         }
1411
1412         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1413             "Adding sess %p se_sess %p  to tgt %p sess_count %d\n",
1414             sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1415             vha->vha_tgt.qla_tgt->sess_count);
1416
1417         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1418             "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1419             "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1420             vha->vp_idx, local ?  "local " : "", fcport->port_name,
1421             fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1422             sess->d_id.b.al_pa, sess->conf_compl_supported ?  "" : "not ");
1423
1424         return sess;
1425 }
1426
1427 /*
1428  * max_gen - specifies maximum session generation
1429  * at which this deletion requestion is still valid
1430  */
1431 void
1432 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1433 {
1434         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1435         struct fc_port *sess = fcport;
1436         unsigned long flags;
1437
1438         if (!vha->hw->tgt.tgt_ops)
1439                 return;
1440
1441         if (!tgt)
1442                 return;
1443
1444         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1445         if (tgt->tgt_stop) {
1446                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1447                 return;
1448         }
1449         if (!sess->se_sess) {
1450                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1451                 return;
1452         }
1453
1454         if (max_gen - sess->generation < 0) {
1455                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1456                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1457                     "Ignoring stale deletion request for se_sess %p / sess %p"
1458                     " for port %8phC, req_gen %d, sess_gen %d\n",
1459                     sess->se_sess, sess, sess->port_name, max_gen,
1460                     sess->generation);
1461                 return;
1462         }
1463
1464         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1465
1466         sess->local = 1;
1467         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1468         qlt_schedule_sess_for_deletion(sess);
1469 }
1470
1471 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1472 {
1473         struct qla_hw_data *ha = tgt->ha;
1474         unsigned long flags;
1475         int res;
1476         /*
1477          * We need to protect against race, when tgt is freed before or
1478          * inside wake_up()
1479          */
1480         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1481         ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1482             "tgt %p, sess_count=%d\n",
1483             tgt, tgt->sess_count);
1484         res = (tgt->sess_count == 0);
1485         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1486
1487         return res;
1488 }
1489
1490 /* Called by tcm_qla2xxx configfs code */
1491 int qlt_stop_phase1(struct qla_tgt *tgt)
1492 {
1493         struct scsi_qla_host *vha = tgt->vha;
1494         struct qla_hw_data *ha = tgt->ha;
1495         unsigned long flags;
1496
1497         mutex_lock(&ha->optrom_mutex);
1498         mutex_lock(&qla_tgt_mutex);
1499
1500         if (tgt->tgt_stop || tgt->tgt_stopped) {
1501                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1502                     "Already in tgt->tgt_stop or tgt_stopped state\n");
1503                 mutex_unlock(&qla_tgt_mutex);
1504                 mutex_unlock(&ha->optrom_mutex);
1505                 return -EPERM;
1506         }
1507
1508         ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1509             vha->host_no, vha);
1510         /*
1511          * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1512          * Lock is needed, because we still can get an incoming packet.
1513          */
1514         mutex_lock(&vha->vha_tgt.tgt_mutex);
1515         tgt->tgt_stop = 1;
1516         qlt_clear_tgt_db(tgt);
1517         mutex_unlock(&vha->vha_tgt.tgt_mutex);
1518         mutex_unlock(&qla_tgt_mutex);
1519
1520         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1521             "Waiting for sess works (tgt %p)", tgt);
1522         spin_lock_irqsave(&tgt->sess_work_lock, flags);
1523         while (!list_empty(&tgt->sess_works_list)) {
1524                 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1525                 flush_scheduled_work();
1526                 spin_lock_irqsave(&tgt->sess_work_lock, flags);
1527         }
1528         spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1529
1530         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1531             "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1532
1533         wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1534
1535         /* Big hammer */
1536         if (!ha->flags.host_shutting_down &&
1537             (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1538                 qlt_disable_vha(vha);
1539
1540         /* Wait for sessions to clear out (just in case) */
1541         wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1542         mutex_unlock(&ha->optrom_mutex);
1543
1544         return 0;
1545 }
1546 EXPORT_SYMBOL(qlt_stop_phase1);
1547
1548 /* Called by tcm_qla2xxx configfs code */
1549 void qlt_stop_phase2(struct qla_tgt *tgt)
1550 {
1551         scsi_qla_host_t *vha = tgt->vha;
1552
1553         if (tgt->tgt_stopped) {
1554                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1555                     "Already in tgt->tgt_stopped state\n");
1556                 dump_stack();
1557                 return;
1558         }
1559         if (!tgt->tgt_stop) {
1560                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1561                     "%s: phase1 stop is not completed\n", __func__);
1562                 dump_stack();
1563                 return;
1564         }
1565
1566         mutex_lock(&vha->vha_tgt.tgt_mutex);
1567         tgt->tgt_stop = 0;
1568         tgt->tgt_stopped = 1;
1569         mutex_unlock(&vha->vha_tgt.tgt_mutex);
1570
1571         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1572             tgt);
1573
1574         switch (vha->qlini_mode) {
1575         case QLA2XXX_INI_MODE_EXCLUSIVE:
1576                 vha->flags.online = 1;
1577                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1578                 break;
1579         default:
1580                 break;
1581         }
1582 }
1583 EXPORT_SYMBOL(qlt_stop_phase2);
1584
1585 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1586 static void qlt_release(struct qla_tgt *tgt)
1587 {
1588         scsi_qla_host_t *vha = tgt->vha;
1589         void *node;
1590         u64 key = 0;
1591         u16 i;
1592         struct qla_qpair_hint *h;
1593         struct qla_hw_data *ha = vha->hw;
1594
1595         if (!tgt->tgt_stop && !tgt->tgt_stopped)
1596                 qlt_stop_phase1(tgt);
1597
1598         if (!tgt->tgt_stopped)
1599                 qlt_stop_phase2(tgt);
1600
1601         for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1602                 unsigned long flags;
1603
1604                 h = &tgt->qphints[i];
1605                 if (h->qpair) {
1606                         spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1607                         list_del(&h->hint_elem);
1608                         spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1609                         h->qpair = NULL;
1610                 }
1611         }
1612         kfree(tgt->qphints);
1613         mutex_lock(&qla_tgt_mutex);
1614         list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1615         mutex_unlock(&qla_tgt_mutex);
1616
1617         btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1618                 btree_remove64(&tgt->lun_qpair_map, key);
1619
1620         btree_destroy64(&tgt->lun_qpair_map);
1621
1622         if (vha->vp_idx)
1623                 if (ha->tgt.tgt_ops &&
1624                     ha->tgt.tgt_ops->remove_target &&
1625                     vha->vha_tgt.target_lport_ptr)
1626                         ha->tgt.tgt_ops->remove_target(vha);
1627
1628         vha->vha_tgt.qla_tgt = NULL;
1629
1630         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1631             "Release of tgt %p finished\n", tgt);
1632
1633         kfree(tgt);
1634 }
1635
1636 /* ha->hardware_lock supposed to be held on entry */
1637 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1638         const void *param, unsigned int param_size)
1639 {
1640         struct qla_tgt_sess_work_param *prm;
1641         unsigned long flags;
1642
1643         prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1644         if (!prm) {
1645                 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1646                     "qla_target(%d): Unable to create session "
1647                     "work, command will be refused", 0);
1648                 return -ENOMEM;
1649         }
1650
1651         ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1652             "Scheduling work (type %d, prm %p)"
1653             " to find session for param %p (size %d, tgt %p)\n",
1654             type, prm, param, param_size, tgt);
1655
1656         prm->type = type;
1657         memcpy(&prm->tm_iocb, param, param_size);
1658
1659         spin_lock_irqsave(&tgt->sess_work_lock, flags);
1660         list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1661         spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1662
1663         schedule_work(&tgt->sess_work);
1664
1665         return 0;
1666 }
1667
1668 /*
1669  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1670  */
1671 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1672         struct imm_ntfy_from_isp *ntfy,
1673         uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1674         uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1675 {
1676         struct scsi_qla_host *vha = qpair->vha;
1677         struct qla_hw_data *ha = vha->hw;
1678         request_t *pkt;
1679         struct nack_to_isp *nack;
1680
1681         if (!ha->flags.fw_started)
1682                 return;
1683
1684         ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1685
1686         pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1687         if (!pkt) {
1688                 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1689                     "qla_target(%d): %s failed: unable to allocate "
1690                     "request packet\n", vha->vp_idx, __func__);
1691                 return;
1692         }
1693
1694         if (vha->vha_tgt.qla_tgt != NULL)
1695                 vha->vha_tgt.qla_tgt->notify_ack_expected++;
1696
1697         pkt->entry_type = NOTIFY_ACK_TYPE;
1698         pkt->entry_count = 1;
1699
1700         nack = (struct nack_to_isp *)pkt;
1701         nack->ox_id = ntfy->ox_id;
1702
1703         nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1704         nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1705         if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1706                 nack->u.isp24.flags = ntfy->u.isp24.flags &
1707                         cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1708         }
1709         nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1710         nack->u.isp24.status = ntfy->u.isp24.status;
1711         nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1712         nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1713         nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1714         nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1715         nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1716         nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1717         nack->u.isp24.srr_reject_code = srr_reject_code;
1718         nack->u.isp24.srr_reject_code_expl = srr_explan;
1719         nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1720
1721         ql_dbg(ql_dbg_tgt, vha, 0xe005,
1722             "qla_target(%d): Sending 24xx Notify Ack %d\n",
1723             vha->vp_idx, nack->u.isp24.status);
1724
1725         /* Memory Barrier */
1726         wmb();
1727         qla2x00_start_iocbs(vha, qpair->req);
1728 }
1729
1730 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1731 {
1732         struct scsi_qla_host *vha = mcmd->vha;
1733         struct qla_hw_data *ha = vha->hw;
1734         struct abts_resp_to_24xx *resp;
1735         __le32 f_ctl;
1736         uint32_t h;
1737         uint8_t *p;
1738         int rc;
1739         struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1740         struct qla_qpair *qpair = mcmd->qpair;
1741
1742         ql_dbg(ql_dbg_tgt, vha, 0xe006,
1743             "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1744             ha, mcmd->fc_tm_rsp);
1745
1746         rc = qlt_check_reserve_free_req(qpair, 1);
1747         if (rc) {
1748                 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1749                     "qla_target(%d): %s failed: unable to allocate request packet\n",
1750                     vha->vp_idx, __func__);
1751                 return -EAGAIN;
1752         }
1753
1754         resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1755         memset(resp, 0, sizeof(*resp));
1756
1757         h = qlt_make_handle(qpair);
1758         if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1759                 /*
1760                  * CTIO type 7 from the firmware doesn't provide a way to
1761                  * know the initiator's LOOP ID, hence we can't find
1762                  * the session and, so, the command.
1763                  */
1764                 return -EAGAIN;
1765         } else {
1766                 qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1767         }
1768
1769         resp->handle = make_handle(qpair->req->id, h);
1770         resp->entry_type = ABTS_RESP_24XX;
1771         resp->entry_count = 1;
1772         resp->nport_handle = abts->nport_handle;
1773         resp->vp_index = vha->vp_idx;
1774         resp->sof_type = abts->sof_type;
1775         resp->exchange_address = abts->exchange_address;
1776         resp->fcp_hdr_le = abts->fcp_hdr_le;
1777         f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1778             F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1779             F_CTL_SEQ_INITIATIVE);
1780         p = (uint8_t *)&f_ctl;
1781         resp->fcp_hdr_le.f_ctl[0] = *p++;
1782         resp->fcp_hdr_le.f_ctl[1] = *p++;
1783         resp->fcp_hdr_le.f_ctl[2] = *p;
1784
1785         resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1786         resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1787
1788         resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1789         if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1790                 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1791                 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1792                 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1793                 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1794                 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1795                 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1796         } else {
1797                 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1798                 resp->payload.ba_rjt.reason_code =
1799                         BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1800                 /* Other bytes are zero */
1801         }
1802
1803         vha->vha_tgt.qla_tgt->abts_resp_expected++;
1804
1805         /* Memory Barrier */
1806         wmb();
1807         if (qpair->reqq_start_iocbs)
1808                 qpair->reqq_start_iocbs(qpair);
1809         else
1810                 qla2x00_start_iocbs(vha, qpair->req);
1811
1812         return rc;
1813 }
1814
1815 /*
1816  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1817  */
1818 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1819         struct abts_recv_from_24xx *abts, uint32_t status,
1820         bool ids_reversed)
1821 {
1822         struct scsi_qla_host *vha = qpair->vha;
1823         struct qla_hw_data *ha = vha->hw;
1824         struct abts_resp_to_24xx *resp;
1825         __le32 f_ctl;
1826         uint8_t *p;
1827
1828         ql_dbg(ql_dbg_tgt, vha, 0xe006,
1829             "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1830             ha, abts, status);
1831
1832         resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1833             NULL);
1834         if (!resp) {
1835                 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1836                     "qla_target(%d): %s failed: unable to allocate "
1837                     "request packet", vha->vp_idx, __func__);
1838                 return;
1839         }
1840
1841         resp->entry_type = ABTS_RESP_24XX;
1842         resp->handle = QLA_TGT_SKIP_HANDLE;
1843         resp->entry_count = 1;
1844         resp->nport_handle = abts->nport_handle;
1845         resp->vp_index = vha->vp_idx;
1846         resp->sof_type = abts->sof_type;
1847         resp->exchange_address = abts->exchange_address;
1848         resp->fcp_hdr_le = abts->fcp_hdr_le;
1849         f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1850             F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1851             F_CTL_SEQ_INITIATIVE);
1852         p = (uint8_t *)&f_ctl;
1853         resp->fcp_hdr_le.f_ctl[0] = *p++;
1854         resp->fcp_hdr_le.f_ctl[1] = *p++;
1855         resp->fcp_hdr_le.f_ctl[2] = *p;
1856         if (ids_reversed) {
1857                 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1858                 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1859         } else {
1860                 resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1861                 resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1862         }
1863         resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1864         if (status == FCP_TMF_CMPL) {
1865                 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1866                 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1867                 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1868                 resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1869                 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1870                 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1871         } else {
1872                 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1873                 resp->payload.ba_rjt.reason_code =
1874                         BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1875                 /* Other bytes are zero */
1876         }
1877
1878         vha->vha_tgt.qla_tgt->abts_resp_expected++;
1879
1880         /* Memory Barrier */
1881         wmb();
1882         if (qpair->reqq_start_iocbs)
1883                 qpair->reqq_start_iocbs(qpair);
1884         else
1885                 qla2x00_start_iocbs(vha, qpair->req);
1886 }
1887
1888 /*
1889  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1890  */
1891 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1892     struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1893 {
1894         struct ctio7_to_24xx *ctio;
1895         u16 tmp;
1896         struct abts_recv_from_24xx *entry;
1897
1898         ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1899         if (ctio == NULL) {
1900                 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1901                     "qla_target(%d): %s failed: unable to allocate "
1902                     "request packet\n", vha->vp_idx, __func__);
1903                 return;
1904         }
1905
1906         if (mcmd)
1907                 /* abts from remote port */
1908                 entry = &mcmd->orig_iocb.abts;
1909         else
1910                 /* abts from this driver.  */
1911                 entry = (struct abts_recv_from_24xx *)pkt;
1912
1913         /*
1914          * We've got on entrance firmware's response on by us generated
1915          * ABTS response. So, in it ID fields are reversed.
1916          */
1917
1918         ctio->entry_type = CTIO_TYPE7;
1919         ctio->entry_count = 1;
1920         ctio->nport_handle = entry->nport_handle;
1921         ctio->handle = QLA_TGT_SKIP_HANDLE |    CTIO_COMPLETION_HANDLE_MARK;
1922         ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1923         ctio->vp_index = vha->vp_idx;
1924         ctio->exchange_addr = entry->exchange_addr_to_abort;
1925         tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1926
1927         if (mcmd) {
1928                 ctio->initiator_id = entry->fcp_hdr_le.s_id;
1929
1930                 if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1931                         tmp |= (mcmd->abort_io_attr << 9);
1932                 else if (qpair->retry_term_cnt & 1)
1933                         tmp |= (0x4 << 9);
1934         } else {
1935                 ctio->initiator_id = entry->fcp_hdr_le.d_id;
1936
1937                 if (qpair->retry_term_cnt & 1)
1938                         tmp |= (0x4 << 9);
1939         }
1940         ctio->u.status1.flags = cpu_to_le16(tmp);
1941         ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1942
1943         ql_dbg(ql_dbg_tgt, vha, 0xe007,
1944             "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1945             le16_to_cpu(ctio->u.status1.flags),
1946             le16_to_cpu(ctio->u.status1.ox_id),
1947             (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1948
1949         /* Memory Barrier */
1950         wmb();
1951         if (qpair->reqq_start_iocbs)
1952                 qpair->reqq_start_iocbs(qpair);
1953         else
1954                 qla2x00_start_iocbs(vha, qpair->req);
1955
1956         if (mcmd)
1957                 qlt_build_abts_resp_iocb(mcmd);
1958         else
1959                 qlt_24xx_send_abts_resp(qpair,
1960                     (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1961
1962 }
1963
1964 /* drop cmds for the given lun
1965  * XXX only looks for cmds on the port through which lun reset was recieved
1966  * XXX does not go through the list of other port (which may have cmds
1967  *     for the same lun)
1968  */
1969 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
1970 {
1971         struct qla_tgt_sess_op *op;
1972         struct qla_tgt_cmd *cmd;
1973         uint32_t key;
1974         unsigned long flags;
1975
1976         key = sid_to_key(s_id);
1977         spin_lock_irqsave(&vha->cmd_list_lock, flags);
1978         list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1979                 uint32_t op_key;
1980                 u64 op_lun;
1981
1982                 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1983                 op_lun = scsilun_to_int(
1984                         (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1985                 if (op_key == key && op_lun == lun)
1986                         op->aborted = true;
1987         }
1988
1989         list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1990                 uint32_t op_key;
1991                 u64 op_lun;
1992
1993                 op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1994                 op_lun = scsilun_to_int(
1995                         (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1996                 if (op_key == key && op_lun == lun)
1997                         op->aborted = true;
1998         }
1999
2000         list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2001                 uint32_t cmd_key;
2002                 u64 cmd_lun;
2003
2004                 cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2005                 cmd_lun = scsilun_to_int(
2006                         (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2007                 if (cmd_key == key && cmd_lun == lun)
2008                         cmd->aborted = 1;
2009         }
2010         spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2011 }
2012
2013 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2014     uint64_t unpacked_lun)
2015 {
2016         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2017         struct qla_qpair_hint *h = NULL;
2018
2019         if (vha->flags.qpairs_available) {
2020                 h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2021                 if (!h)
2022                         h = &tgt->qphints[0];
2023         } else {
2024                 h = &tgt->qphints[0];
2025         }
2026
2027         return h;
2028 }
2029
2030 static void qlt_do_tmr_work(struct work_struct *work)
2031 {
2032         struct qla_tgt_mgmt_cmd *mcmd =
2033                 container_of(work, struct qla_tgt_mgmt_cmd, work);
2034         struct qla_hw_data *ha = mcmd->vha->hw;
2035         int rc;
2036         uint32_t tag;
2037         unsigned long flags;
2038
2039         switch (mcmd->tmr_func) {
2040         case QLA_TGT_ABTS:
2041                 tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2042                 break;
2043         default:
2044                 tag = 0;
2045                 break;
2046         }
2047
2048         rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2049             mcmd->tmr_func, tag);
2050
2051         if (rc != 0) {
2052                 spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2053                 switch (mcmd->tmr_func) {
2054                 case QLA_TGT_ABTS:
2055                         mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2056                         qlt_build_abts_resp_iocb(mcmd);
2057                         break;
2058                 case QLA_TGT_LUN_RESET:
2059                 case QLA_TGT_CLEAR_TS:
2060                 case QLA_TGT_ABORT_TS:
2061                 case QLA_TGT_CLEAR_ACA:
2062                 case QLA_TGT_TARGET_RESET:
2063                         qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2064                             qla_sam_status);
2065                         break;
2066
2067                 case QLA_TGT_ABORT_ALL:
2068                 case QLA_TGT_NEXUS_LOSS_SESS:
2069                 case QLA_TGT_NEXUS_LOSS:
2070                         qlt_send_notify_ack(mcmd->qpair,
2071                             &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2072                         break;
2073                 }
2074                 spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2075
2076                 ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2077                     "qla_target(%d):  tgt_ops->handle_tmr() failed: %d\n",
2078                     mcmd->vha->vp_idx, rc);
2079                 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2080         }
2081 }
2082
2083 /* ha->hardware_lock supposed to be held on entry */
2084 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2085         struct abts_recv_from_24xx *abts, struct fc_port *sess)
2086 {
2087         struct qla_hw_data *ha = vha->hw;
2088         struct qla_tgt_mgmt_cmd *mcmd;
2089         struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2090         struct qla_tgt_cmd *abort_cmd;
2091
2092         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2093             "qla_target(%d): task abort (tag=%d)\n",
2094             vha->vp_idx, abts->exchange_addr_to_abort);
2095
2096         mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2097         if (mcmd == NULL) {
2098                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2099                     "qla_target(%d): %s: Allocation of ABORT cmd failed",
2100                     vha->vp_idx, __func__);
2101                 return -ENOMEM;
2102         }
2103         memset(mcmd, 0, sizeof(*mcmd));
2104         mcmd->cmd_type = TYPE_TGT_TMCMD;
2105         mcmd->sess = sess;
2106         memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2107         mcmd->reset_count = ha->base_qpair->chip_reset;
2108         mcmd->tmr_func = QLA_TGT_ABTS;
2109         mcmd->qpair = h->qpair;
2110         mcmd->vha = vha;
2111
2112         /*
2113          * LUN is looked up by target-core internally based on the passed
2114          * abts->exchange_addr_to_abort tag.
2115          */
2116         mcmd->se_cmd.cpuid = h->cpuid;
2117
2118         abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2119                                 le32_to_cpu(abts->exchange_addr_to_abort));
2120         if (!abort_cmd)
2121                 return -EIO;
2122         mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2123
2124         if (abort_cmd->qpair) {
2125                 mcmd->qpair = abort_cmd->qpair;
2126                 mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2127                 mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2128                 mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2129         }
2130
2131         INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2132         queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2133
2134         return 0;
2135 }
2136
2137 /*
2138  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2139  */
2140 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2141         struct abts_recv_from_24xx *abts)
2142 {
2143         struct qla_hw_data *ha = vha->hw;
2144         struct fc_port *sess;
2145         uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2146         be_id_t s_id;
2147         int rc;
2148         unsigned long flags;
2149
2150         if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2151                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2152                     "qla_target(%d): ABTS: Abort Sequence not "
2153                     "supported\n", vha->vp_idx);
2154                 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2155                     false);
2156                 return;
2157         }
2158
2159         if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2160                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2161                     "qla_target(%d): ABTS: Unknown Exchange "
2162                     "Address received\n", vha->vp_idx);
2163                 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2164                     false);
2165                 return;
2166         }
2167
2168         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2169             "qla_target(%d): task abort (s_id=%x:%x:%x, "
2170             "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2171             abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2172             le32_to_cpu(abts->fcp_hdr_le.parameter));
2173
2174         s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2175
2176         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2177         sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2178         if (!sess) {
2179                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2180                     "qla_target(%d): task abort for non-existent session\n",
2181                     vha->vp_idx);
2182                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2183
2184                 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2185                             false);
2186                 return;
2187         }
2188         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2189
2190
2191         if (sess->deleted) {
2192                 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2193                     false);
2194                 return;
2195         }
2196
2197         rc = __qlt_24xx_handle_abts(vha, abts, sess);
2198         if (rc != 0) {
2199                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2200                     "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2201                     vha->vp_idx, rc);
2202                 qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2203                     false);
2204                 return;
2205         }
2206 }
2207
2208 /*
2209  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2210  */
2211 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2212         struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2213 {
2214         struct scsi_qla_host *ha = mcmd->vha;
2215         struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2216         struct ctio7_to_24xx *ctio;
2217         uint16_t temp;
2218
2219         ql_dbg(ql_dbg_tgt, ha, 0xe008,
2220             "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2221             ha, atio, resp_code);
2222
2223
2224         ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2225         if (ctio == NULL) {
2226                 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2227                     "qla_target(%d): %s failed: unable to allocate "
2228                     "request packet\n", ha->vp_idx, __func__);
2229                 return;
2230         }
2231
2232         ctio->entry_type = CTIO_TYPE7;
2233         ctio->entry_count = 1;
2234         ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2235         ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2236         ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2237         ctio->vp_index = ha->vp_idx;
2238         ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2239         ctio->exchange_addr = atio->u.isp24.exchange_addr;
2240         temp = (atio->u.isp24.attr << 9)|
2241                 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2242         ctio->u.status1.flags = cpu_to_le16(temp);
2243         temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2244         ctio->u.status1.ox_id = cpu_to_le16(temp);
2245         ctio->u.status1.scsi_status =
2246             cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2247         ctio->u.status1.response_len = cpu_to_le16(8);
2248         ctio->u.status1.sense_data[0] = resp_code;
2249
2250         /* Memory Barrier */
2251         wmb();
2252         if (qpair->reqq_start_iocbs)
2253                 qpair->reqq_start_iocbs(qpair);
2254         else
2255                 qla2x00_start_iocbs(ha, qpair->req);
2256 }
2257
2258 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2259 {
2260         mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2261 }
2262 EXPORT_SYMBOL(qlt_free_mcmd);
2263
2264 /*
2265  * ha->hardware_lock supposed to be held on entry. Might drop it, then
2266  * reacquire
2267  */
2268 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2269     uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2270 {
2271         struct atio_from_isp *atio = &cmd->atio;
2272         struct ctio7_to_24xx *ctio;
2273         uint16_t temp;
2274         struct scsi_qla_host *vha = cmd->vha;
2275
2276         ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2277             "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2278             "sense_key=%02x, asc=%02x, ascq=%02x",
2279             vha, atio, scsi_status, sense_key, asc, ascq);
2280
2281         ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2282         if (!ctio) {
2283                 ql_dbg(ql_dbg_async, vha, 0x3067,
2284                     "qla2x00t(%ld): %s failed: unable to allocate request packet",
2285                     vha->host_no, __func__);
2286                 goto out;
2287         }
2288
2289         ctio->entry_type = CTIO_TYPE7;
2290         ctio->entry_count = 1;
2291         ctio->handle = QLA_TGT_SKIP_HANDLE;
2292         ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2293         ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2294         ctio->vp_index = vha->vp_idx;
2295         ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2296         ctio->exchange_addr = atio->u.isp24.exchange_addr;
2297         temp = (atio->u.isp24.attr << 9) |
2298             CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2299         ctio->u.status1.flags = cpu_to_le16(temp);
2300         temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2301         ctio->u.status1.ox_id = cpu_to_le16(temp);
2302         ctio->u.status1.scsi_status =
2303             cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2304         ctio->u.status1.response_len = cpu_to_le16(18);
2305         ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2306
2307         if (ctio->u.status1.residual != 0)
2308                 ctio->u.status1.scsi_status |=
2309                     cpu_to_le16(SS_RESIDUAL_UNDER);
2310
2311         /* Fixed format sense data. */
2312         ctio->u.status1.sense_data[0] = 0x70;
2313         ctio->u.status1.sense_data[2] = sense_key;
2314         /* Additional sense length */
2315         ctio->u.status1.sense_data[7] = 0xa;
2316         /* ASC and ASCQ */
2317         ctio->u.status1.sense_data[12] = asc;
2318         ctio->u.status1.sense_data[13] = ascq;
2319
2320         /* Memory Barrier */
2321         wmb();
2322
2323         if (qpair->reqq_start_iocbs)
2324                 qpair->reqq_start_iocbs(qpair);
2325         else
2326                 qla2x00_start_iocbs(vha, qpair->req);
2327
2328 out:
2329         return;
2330 }
2331
2332 /* callback from target fabric module code */
2333 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2334 {
2335         struct scsi_qla_host *vha = mcmd->sess->vha;
2336         struct qla_hw_data *ha = vha->hw;
2337         unsigned long flags;
2338         struct qla_qpair *qpair = mcmd->qpair;
2339         bool free_mcmd = true;
2340
2341         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2342             "TM response mcmd (%p) status %#x state %#x",
2343             mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2344
2345         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2346
2347         if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2348                 /*
2349                  * Either the port is not online or this request was from
2350                  * previous life, just abort the processing.
2351                  */
2352                 ql_dbg(ql_dbg_async, vha, 0xe100,
2353                         "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2354                         vha->flags.online, qla2x00_reset_active(vha),
2355                         mcmd->reset_count, qpair->chip_reset);
2356                 ha->tgt.tgt_ops->free_mcmd(mcmd);
2357                 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2358                 return;
2359         }
2360
2361         if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2362                 switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2363                 case ELS_LOGO:
2364                 case ELS_PRLO:
2365                 case ELS_TPRLO:
2366                         ql_dbg(ql_dbg_disc, vha, 0x2106,
2367                             "TM response logo %8phC status %#x state %#x",
2368                             mcmd->sess->port_name, mcmd->fc_tm_rsp,
2369                             mcmd->flags);
2370                         qlt_schedule_sess_for_deletion(mcmd->sess);
2371                         break;
2372                 default:
2373                         qlt_send_notify_ack(vha->hw->base_qpair,
2374                             &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2375                         break;
2376                 }
2377         } else {
2378                 if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2379                         qlt_build_abts_resp_iocb(mcmd);
2380                         free_mcmd = false;
2381                 } else
2382                         qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2383                             mcmd->fc_tm_rsp);
2384         }
2385         /*
2386          * Make the callback for ->free_mcmd() to queue_work() and invoke
2387          * target_put_sess_cmd() to drop cmd_kref to 1.  The final
2388          * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2389          * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2390          * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2391          * qlt_xmit_tm_rsp() returns here..
2392          */
2393         if (free_mcmd)
2394                 ha->tgt.tgt_ops->free_mcmd(mcmd);
2395
2396         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2397 }
2398 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2399
2400 /* No locks */
2401 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2402 {
2403         struct qla_tgt_cmd *cmd = prm->cmd;
2404
2405         BUG_ON(cmd->sg_cnt == 0);
2406
2407         prm->sg = (struct scatterlist *)cmd->sg;
2408         prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2409             cmd->sg_cnt, cmd->dma_data_direction);
2410         if (unlikely(prm->seg_cnt == 0))
2411                 goto out_err;
2412
2413         prm->cmd->sg_mapped = 1;
2414
2415         if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2416                 /*
2417                  * If greater than four sg entries then we need to allocate
2418                  * the continuation entries
2419                  */
2420                 if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2421                         prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2422                         QLA_TGT_DATASEGS_PER_CMD_24XX,
2423                         QLA_TGT_DATASEGS_PER_CONT_24XX);
2424         } else {
2425                 /* DIF */
2426                 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2427                     (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2428                         prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2429                         prm->tot_dsds = prm->seg_cnt;
2430                 } else
2431                         prm->tot_dsds = prm->seg_cnt;
2432
2433                 if (cmd->prot_sg_cnt) {
2434                         prm->prot_sg      = cmd->prot_sg;
2435                         prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2436                                 cmd->prot_sg, cmd->prot_sg_cnt,
2437                                 cmd->dma_data_direction);
2438                         if (unlikely(prm->prot_seg_cnt == 0))
2439                                 goto out_err;
2440
2441                         if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2442                             (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2443                                 /* Dif Bundling not support here */
2444                                 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2445                                                                 cmd->blk_sz);
2446                                 prm->tot_dsds += prm->prot_seg_cnt;
2447                         } else
2448                                 prm->tot_dsds += prm->prot_seg_cnt;
2449                 }
2450         }
2451
2452         return 0;
2453
2454 out_err:
2455         ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2456             "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2457             0, prm->cmd->sg_cnt);
2458         return -1;
2459 }
2460
2461 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2462 {
2463         struct qla_hw_data *ha;
2464         struct qla_qpair *qpair;
2465
2466         if (!cmd->sg_mapped)
2467                 return;
2468
2469         qpair = cmd->qpair;
2470
2471         dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2472             cmd->dma_data_direction);
2473         cmd->sg_mapped = 0;
2474
2475         if (cmd->prot_sg_cnt)
2476                 dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2477                         cmd->dma_data_direction);
2478
2479         if (!cmd->ctx)
2480                 return;
2481         ha = vha->hw;
2482         if (cmd->ctx_dsd_alloced)
2483                 qla2x00_clean_dsd_pool(ha, cmd->ctx);
2484
2485         dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2486 }
2487
2488 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2489         uint32_t req_cnt)
2490 {
2491         uint32_t cnt;
2492         struct req_que *req = qpair->req;
2493
2494         if (req->cnt < (req_cnt + 2)) {
2495                 cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2496                     rd_reg_dword_relaxed(req->req_q_out));
2497
2498                 if  (req->ring_index < cnt)
2499                         req->cnt = cnt - req->ring_index;
2500                 else
2501                         req->cnt = req->length - (req->ring_index - cnt);
2502
2503                 if (unlikely(req->cnt < (req_cnt + 2)))
2504                         return -EAGAIN;
2505         }
2506
2507         req->cnt -= req_cnt;
2508
2509         return 0;
2510 }
2511
2512 /*
2513  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2514  */
2515 static inline void *qlt_get_req_pkt(struct req_que *req)
2516 {
2517         /* Adjust ring index. */
2518         req->ring_index++;
2519         if (req->ring_index == req->length) {
2520                 req->ring_index = 0;
2521                 req->ring_ptr = req->ring;
2522         } else {
2523                 req->ring_ptr++;
2524         }
2525         return (cont_entry_t *)req->ring_ptr;
2526 }
2527
2528 /* ha->hardware_lock supposed to be held on entry */
2529 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2530 {
2531         uint32_t h;
2532         int index;
2533         uint8_t found = 0;
2534         struct req_que *req = qpair->req;
2535
2536         h = req->current_outstanding_cmd;
2537
2538         for (index = 1; index < req->num_outstanding_cmds; index++) {
2539                 h++;
2540                 if (h == req->num_outstanding_cmds)
2541                         h = 1;
2542
2543                 if (h == QLA_TGT_SKIP_HANDLE)
2544                         continue;
2545
2546                 if (!req->outstanding_cmds[h]) {
2547                         found = 1;
2548                         break;
2549                 }
2550         }
2551
2552         if (found) {
2553                 req->current_outstanding_cmd = h;
2554         } else {
2555                 ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2556                     "qla_target(%d): Ran out of empty cmd slots\n",
2557                     qpair->vha->vp_idx);
2558                 h = QLA_TGT_NULL_HANDLE;
2559         }
2560
2561         return h;
2562 }
2563
2564 /* ha->hardware_lock supposed to be held on entry */
2565 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2566         struct qla_tgt_prm *prm)
2567 {
2568         uint32_t h;
2569         struct ctio7_to_24xx *pkt;
2570         struct atio_from_isp *atio = &prm->cmd->atio;
2571         uint16_t temp;
2572
2573         pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2574         prm->pkt = pkt;
2575         memset(pkt, 0, sizeof(*pkt));
2576
2577         pkt->entry_type = CTIO_TYPE7;
2578         pkt->entry_count = (uint8_t)prm->req_cnt;
2579         pkt->vp_index = prm->cmd->vp_idx;
2580
2581         h = qlt_make_handle(qpair);
2582         if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2583                 /*
2584                  * CTIO type 7 from the firmware doesn't provide a way to
2585                  * know the initiator's LOOP ID, hence we can't find
2586                  * the session and, so, the command.
2587                  */
2588                 return -EAGAIN;
2589         } else
2590                 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2591
2592         pkt->handle = make_handle(qpair->req->id, h);
2593         pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2594         pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2595         pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2596         pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2597         pkt->exchange_addr = atio->u.isp24.exchange_addr;
2598         temp = atio->u.isp24.attr << 9;
2599         pkt->u.status0.flags |= cpu_to_le16(temp);
2600         temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2601         pkt->u.status0.ox_id = cpu_to_le16(temp);
2602         pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2603
2604         return 0;
2605 }
2606
2607 /*
2608  * ha->hardware_lock supposed to be held on entry. We have already made sure
2609  * that there is sufficient amount of request entries to not drop it.
2610  */
2611 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2612 {
2613         int cnt;
2614         struct dsd64 *cur_dsd;
2615
2616         /* Build continuation packets */
2617         while (prm->seg_cnt > 0) {
2618                 cont_a64_entry_t *cont_pkt64 =
2619                         (cont_a64_entry_t *)qlt_get_req_pkt(
2620                            prm->cmd->qpair->req);
2621
2622                 /*
2623                  * Make sure that from cont_pkt64 none of
2624                  * 64-bit specific fields used for 32-bit
2625                  * addressing. Cast to (cont_entry_t *) for
2626                  * that.
2627                  */
2628
2629                 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2630
2631                 cont_pkt64->entry_count = 1;
2632                 cont_pkt64->sys_define = 0;
2633
2634                 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2635                 cur_dsd = cont_pkt64->dsd;
2636
2637                 /* Load continuation entry data segments */
2638                 for (cnt = 0;
2639                     cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2640                     cnt++, prm->seg_cnt--) {
2641                         append_dsd64(&cur_dsd, prm->sg);
2642                         prm->sg = sg_next(prm->sg);
2643                 }
2644         }
2645 }
2646
2647 /*
2648  * ha->hardware_lock supposed to be held on entry. We have already made sure
2649  * that there is sufficient amount of request entries to not drop it.
2650  */
2651 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2652 {
2653         int cnt;
2654         struct dsd64 *cur_dsd;
2655         struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2656
2657         pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2658
2659         /* Setup packet address segment pointer */
2660         cur_dsd = &pkt24->u.status0.dsd;
2661
2662         /* Set total data segment count */
2663         if (prm->seg_cnt)
2664                 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2665
2666         if (prm->seg_cnt == 0) {
2667                 /* No data transfer */
2668                 cur_dsd->address = 0;
2669                 cur_dsd->length = 0;
2670                 return;
2671         }
2672
2673         /* If scatter gather */
2674
2675         /* Load command entry data segments */
2676         for (cnt = 0;
2677             (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2678             cnt++, prm->seg_cnt--) {
2679                 append_dsd64(&cur_dsd, prm->sg);
2680                 prm->sg = sg_next(prm->sg);
2681         }
2682
2683         qlt_load_cont_data_segments(prm);
2684 }
2685
2686 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2687 {
2688         return cmd->bufflen > 0;
2689 }
2690
2691 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2692 {
2693         struct qla_tgt_cmd *cmd;
2694         struct scsi_qla_host *vha;
2695
2696         /* asc 0x10=dif error */
2697         if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2698                 cmd = prm->cmd;
2699                 vha = cmd->vha;
2700                 /* ASCQ */
2701                 switch (prm->sense_buffer[13]) {
2702                 case 1:
2703                         ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2704                             "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2705                             "se_cmd=%p tag[%x]",
2706                             cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2707                             cmd->atio.u.isp24.exchange_addr);
2708                         break;
2709                 case 2:
2710                         ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2711                             "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2712                             "se_cmd=%p tag[%x]",
2713                             cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2714                             cmd->atio.u.isp24.exchange_addr);
2715                         break;
2716                 case 3:
2717                         ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2718                             "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2719                             "se_cmd=%p tag[%x]",
2720                             cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2721                             cmd->atio.u.isp24.exchange_addr);
2722                         break;
2723                 default:
2724                         ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2725                             "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2726                             "se_cmd=%p tag[%x]",
2727                             cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2728                             cmd->atio.u.isp24.exchange_addr);
2729                         break;
2730                 }
2731                 ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2732         }
2733 }
2734
2735 /*
2736  * Called without ha->hardware_lock held
2737  */
2738 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2739         struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2740         uint32_t *full_req_cnt)
2741 {
2742         struct se_cmd *se_cmd = &cmd->se_cmd;
2743         struct qla_qpair *qpair = cmd->qpair;
2744
2745         prm->cmd = cmd;
2746         prm->tgt = cmd->tgt;
2747         prm->pkt = NULL;
2748         prm->rq_result = scsi_status;
2749         prm->sense_buffer = &cmd->sense_buffer[0];
2750         prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2751         prm->sg = NULL;
2752         prm->seg_cnt = -1;
2753         prm->req_cnt = 1;
2754         prm->residual = 0;
2755         prm->add_status_pkt = 0;
2756         prm->prot_sg = NULL;
2757         prm->prot_seg_cnt = 0;
2758         prm->tot_dsds = 0;
2759
2760         if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2761                 if  (qlt_pci_map_calc_cnt(prm) != 0)
2762                         return -EAGAIN;
2763         }
2764
2765         *full_req_cnt = prm->req_cnt;
2766
2767         if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2768                 prm->residual = se_cmd->residual_count;
2769                 ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2770                     "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2771                        prm->residual, se_cmd->tag,
2772                        se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2773                        cmd->bufflen, prm->rq_result);
2774                 prm->rq_result |= SS_RESIDUAL_UNDER;
2775         } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2776                 prm->residual = se_cmd->residual_count;
2777                 ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2778                     "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2779                        prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2780                        se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2781                 prm->rq_result |= SS_RESIDUAL_OVER;
2782         }
2783
2784         if (xmit_type & QLA_TGT_XMIT_STATUS) {
2785                 /*
2786                  * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2787                  * ignored in *xmit_response() below
2788                  */
2789                 if (qlt_has_data(cmd)) {
2790                         if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2791                             (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2792                             (prm->rq_result != 0))) {
2793                                 prm->add_status_pkt = 1;
2794                                 (*full_req_cnt)++;
2795                         }
2796                 }
2797         }
2798
2799         return 0;
2800 }
2801
2802 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2803     int sending_sense)
2804 {
2805         if (cmd->qpair->enable_class_2)
2806                 return 0;
2807
2808         if (sending_sense)
2809                 return cmd->conf_compl_supported;
2810         else
2811                 return cmd->qpair->enable_explicit_conf &&
2812                     cmd->conf_compl_supported;
2813 }
2814
2815 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2816         struct qla_tgt_prm *prm)
2817 {
2818         prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2819             (uint32_t)sizeof(ctio->u.status1.sense_data));
2820         ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2821         if (qlt_need_explicit_conf(prm->cmd, 0)) {
2822                 ctio->u.status0.flags |= cpu_to_le16(
2823                     CTIO7_FLAGS_EXPLICIT_CONFORM |
2824                     CTIO7_FLAGS_CONFORM_REQ);
2825         }
2826         ctio->u.status0.residual = cpu_to_le32(prm->residual);
2827         ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2828         if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2829                 int i;
2830
2831                 if (qlt_need_explicit_conf(prm->cmd, 1)) {
2832                         if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2833                                 ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2834                                     "Skipping EXPLICIT_CONFORM and "
2835                                     "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2836                                     "non GOOD status\n");
2837                                 goto skip_explict_conf;
2838                         }
2839                         ctio->u.status1.flags |= cpu_to_le16(
2840                             CTIO7_FLAGS_EXPLICIT_CONFORM |
2841                             CTIO7_FLAGS_CONFORM_REQ);
2842                 }
2843 skip_explict_conf:
2844                 ctio->u.status1.flags &=
2845                     ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2846                 ctio->u.status1.flags |=
2847                     cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2848                 ctio->u.status1.scsi_status |=
2849                     cpu_to_le16(SS_SENSE_LEN_VALID);
2850                 ctio->u.status1.sense_length =
2851                     cpu_to_le16(prm->sense_buffer_len);
2852                 for (i = 0; i < prm->sense_buffer_len/4; i++) {
2853                         uint32_t v;
2854
2855                         v = get_unaligned_be32(
2856                                         &((uint32_t *)prm->sense_buffer)[i]);
2857                         put_unaligned_le32(v,
2858                                 &((uint32_t *)ctio->u.status1.sense_data)[i]);
2859                 }
2860                 qlt_print_dif_err(prm);
2861
2862         } else {
2863                 ctio->u.status1.flags &=
2864                     ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2865                 ctio->u.status1.flags |=
2866                     cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2867                 ctio->u.status1.sense_length = 0;
2868                 memset(ctio->u.status1.sense_data, 0,
2869                     sizeof(ctio->u.status1.sense_data));
2870         }
2871
2872         /* Sense with len > 24, is it possible ??? */
2873 }
2874
2875 static inline int
2876 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2877 {
2878         switch (se_cmd->prot_op) {
2879         case TARGET_PROT_DOUT_INSERT:
2880         case TARGET_PROT_DIN_STRIP:
2881                 if (ql2xenablehba_err_chk >= 1)
2882                         return 1;
2883                 break;
2884         case TARGET_PROT_DOUT_PASS:
2885         case TARGET_PROT_DIN_PASS:
2886                 if (ql2xenablehba_err_chk >= 2)
2887                         return 1;
2888                 break;
2889         case TARGET_PROT_DIN_INSERT:
2890         case TARGET_PROT_DOUT_STRIP:
2891                 return 1;
2892         default:
2893                 break;
2894         }
2895         return 0;
2896 }
2897
2898 static inline int
2899 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2900 {
2901         switch (se_cmd->prot_op) {
2902         case TARGET_PROT_DIN_INSERT:
2903         case TARGET_PROT_DOUT_INSERT:
2904         case TARGET_PROT_DIN_STRIP:
2905         case TARGET_PROT_DOUT_STRIP:
2906         case TARGET_PROT_DIN_PASS:
2907         case TARGET_PROT_DOUT_PASS:
2908             return 1;
2909         default:
2910             return 0;
2911         }
2912         return 0;
2913 }
2914
2915 /*
2916  * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2917  */
2918 static void
2919 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2920     uint16_t *pfw_prot_opts)
2921 {
2922         struct se_cmd *se_cmd = &cmd->se_cmd;
2923         uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2924         scsi_qla_host_t *vha = cmd->tgt->vha;
2925         struct qla_hw_data *ha = vha->hw;
2926         uint32_t t32 = 0;
2927
2928         /*
2929          * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2930          * have been immplemented by TCM, before AppTag is avail.
2931          * Look for modesense_handlers[]
2932          */
2933         ctx->app_tag = 0;
2934         ctx->app_tag_mask[0] = 0x0;
2935         ctx->app_tag_mask[1] = 0x0;
2936
2937         if (IS_PI_UNINIT_CAPABLE(ha)) {
2938                 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2939                     (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2940                         *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2941                 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2942                         *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2943         }
2944
2945         t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2946
2947         switch (se_cmd->prot_type) {
2948         case TARGET_DIF_TYPE0_PROT:
2949                 /*
2950                  * No check for ql2xenablehba_err_chk, as it
2951                  * would be an I/O error if hba tag generation
2952                  * is not done.
2953                  */
2954                 ctx->ref_tag = cpu_to_le32(lba);
2955                 /* enable ALL bytes of the ref tag */
2956                 ctx->ref_tag_mask[0] = 0xff;
2957                 ctx->ref_tag_mask[1] = 0xff;
2958                 ctx->ref_tag_mask[2] = 0xff;
2959                 ctx->ref_tag_mask[3] = 0xff;
2960                 break;
2961         case TARGET_DIF_TYPE1_PROT:
2962             /*
2963              * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2964              * REF tag, and 16 bit app tag.
2965              */
2966             ctx->ref_tag = cpu_to_le32(lba);
2967             if (!qla_tgt_ref_mask_check(se_cmd) ||
2968                 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2969                     *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2970                     break;
2971             }
2972             /* enable ALL bytes of the ref tag */
2973             ctx->ref_tag_mask[0] = 0xff;
2974             ctx->ref_tag_mask[1] = 0xff;
2975             ctx->ref_tag_mask[2] = 0xff;
2976             ctx->ref_tag_mask[3] = 0xff;
2977             break;
2978         case TARGET_DIF_TYPE2_PROT:
2979             /*
2980              * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2981              * tag has to match LBA in CDB + N
2982              */
2983             ctx->ref_tag = cpu_to_le32(lba);
2984             if (!qla_tgt_ref_mask_check(se_cmd) ||
2985                 !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2986                     *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2987                     break;
2988             }
2989             /* enable ALL bytes of the ref tag */
2990             ctx->ref_tag_mask[0] = 0xff;
2991             ctx->ref_tag_mask[1] = 0xff;
2992             ctx->ref_tag_mask[2] = 0xff;
2993             ctx->ref_tag_mask[3] = 0xff;
2994             break;
2995         case TARGET_DIF_TYPE3_PROT:
2996             /* For TYPE 3 protection: 16 bit GUARD only */
2997             *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2998             ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2999                 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3000             break;
3001         }
3002 }
3003
3004 static inline int
3005 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3006 {
3007         struct dsd64            *cur_dsd;
3008         uint32_t                transfer_length = 0;
3009         uint32_t                data_bytes;
3010         uint32_t                dif_bytes;
3011         uint8_t                 bundling = 1;
3012         struct crc_context      *crc_ctx_pkt = NULL;
3013         struct qla_hw_data      *ha;
3014         struct ctio_crc2_to_fw  *pkt;
3015         dma_addr_t              crc_ctx_dma;
3016         uint16_t                fw_prot_opts = 0;
3017         struct qla_tgt_cmd      *cmd = prm->cmd;
3018         struct se_cmd           *se_cmd = &cmd->se_cmd;
3019         uint32_t h;
3020         struct atio_from_isp *atio = &prm->cmd->atio;
3021         struct qla_tc_param     tc;
3022         uint16_t t16;
3023         scsi_qla_host_t *vha = cmd->vha;
3024
3025         ha = vha->hw;
3026
3027         pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3028         prm->pkt = pkt;
3029         memset(pkt, 0, sizeof(*pkt));
3030
3031         ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3032                 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3033                 cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3034                 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3035
3036         if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3037             (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3038                 bundling = 0;
3039
3040         /* Compute dif len and adjust data len to incude protection */
3041         data_bytes = cmd->bufflen;
3042         dif_bytes  = (data_bytes / cmd->blk_sz) * 8;
3043
3044         switch (se_cmd->prot_op) {
3045         case TARGET_PROT_DIN_INSERT:
3046         case TARGET_PROT_DOUT_STRIP:
3047                 transfer_length = data_bytes;
3048                 if (cmd->prot_sg_cnt)
3049                         data_bytes += dif_bytes;
3050                 break;
3051         case TARGET_PROT_DIN_STRIP:
3052         case TARGET_PROT_DOUT_INSERT:
3053         case TARGET_PROT_DIN_PASS:
3054         case TARGET_PROT_DOUT_PASS:
3055                 transfer_length = data_bytes + dif_bytes;
3056                 break;
3057         default:
3058                 BUG();
3059                 break;
3060         }
3061
3062         if (!qlt_hba_err_chk_enabled(se_cmd))
3063                 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3064         /* HBA error checking enabled */
3065         else if (IS_PI_UNINIT_CAPABLE(ha)) {
3066                 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3067                     (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3068                         fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3069                 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3070                         fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3071         }
3072
3073         switch (se_cmd->prot_op) {
3074         case TARGET_PROT_DIN_INSERT:
3075         case TARGET_PROT_DOUT_INSERT:
3076                 fw_prot_opts |= PO_MODE_DIF_INSERT;
3077                 break;
3078         case TARGET_PROT_DIN_STRIP:
3079         case TARGET_PROT_DOUT_STRIP:
3080                 fw_prot_opts |= PO_MODE_DIF_REMOVE;
3081                 break;
3082         case TARGET_PROT_DIN_PASS:
3083         case TARGET_PROT_DOUT_PASS:
3084                 fw_prot_opts |= PO_MODE_DIF_PASS;
3085                 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3086                 break;
3087         default:/* Normal Request */
3088                 fw_prot_opts |= PO_MODE_DIF_PASS;
3089                 break;
3090         }
3091
3092         /* ---- PKT ---- */
3093         /* Update entry type to indicate Command Type CRC_2 IOCB */
3094         pkt->entry_type  = CTIO_CRC2;
3095         pkt->entry_count = 1;
3096         pkt->vp_index = cmd->vp_idx;
3097
3098         h = qlt_make_handle(qpair);
3099         if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3100                 /*
3101                  * CTIO type 7 from the firmware doesn't provide a way to
3102                  * know the initiator's LOOP ID, hence we can't find
3103                  * the session and, so, the command.
3104                  */
3105                 return -EAGAIN;
3106         } else
3107                 qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3108
3109         pkt->handle  = make_handle(qpair->req->id, h);
3110         pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3111         pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3112         pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3113         pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3114         pkt->exchange_addr   = atio->u.isp24.exchange_addr;
3115
3116         /* silence compile warning */
3117         t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3118         pkt->ox_id  = cpu_to_le16(t16);
3119
3120         t16 = (atio->u.isp24.attr << 9);
3121         pkt->flags |= cpu_to_le16(t16);
3122         pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3123
3124         /* Set transfer direction */
3125         if (cmd->dma_data_direction == DMA_TO_DEVICE)
3126                 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3127         else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3128                 pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3129
3130         pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3131         /* Fibre channel byte count */
3132         pkt->transfer_length = cpu_to_le32(transfer_length);
3133
3134         /* ----- CRC context -------- */
3135
3136         /* Allocate CRC context from global pool */
3137         crc_ctx_pkt = cmd->ctx =
3138             dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3139
3140         if (!crc_ctx_pkt)
3141                 goto crc_queuing_error;
3142
3143         crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3144         INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3145
3146         /* Set handle */
3147         crc_ctx_pkt->handle = pkt->handle;
3148
3149         qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3150
3151         put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3152         pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3153
3154         if (!bundling) {
3155                 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3156         } else {
3157                 /*
3158                  * Configure Bundling if we need to fetch interlaving
3159                  * protection PCI accesses
3160                  */
3161                 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3162                 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3163                 crc_ctx_pkt->u.bundling.dseg_count =
3164                         cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3165                 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3166         }
3167
3168         /* Finish the common fields of CRC pkt */
3169         crc_ctx_pkt->blk_size   = cpu_to_le16(cmd->blk_sz);
3170         crc_ctx_pkt->prot_opts  = cpu_to_le16(fw_prot_opts);
3171         crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3172         crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3173
3174         memset((uint8_t *)&tc, 0 , sizeof(tc));
3175         tc.vha = vha;
3176         tc.blk_sz = cmd->blk_sz;
3177         tc.bufflen = cmd->bufflen;
3178         tc.sg = cmd->sg;
3179         tc.prot_sg = cmd->prot_sg;
3180         tc.ctx = crc_ctx_pkt;
3181         tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3182
3183         /* Walks data segments */
3184         pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3185
3186         if (!bundling && prm->prot_seg_cnt) {
3187                 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3188                         prm->tot_dsds, &tc))
3189                         goto crc_queuing_error;
3190         } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3191                 (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3192                 goto crc_queuing_error;
3193
3194         if (bundling && prm->prot_seg_cnt) {
3195                 /* Walks dif segments */
3196                 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3197
3198                 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3199                 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3200                         prm->prot_seg_cnt, cmd))
3201                         goto crc_queuing_error;
3202         }
3203         return QLA_SUCCESS;
3204
3205 crc_queuing_error:
3206         /* Cleanup will be performed by the caller */
3207         qpair->req->outstanding_cmds[h] = NULL;
3208
3209         return QLA_FUNCTION_FAILED;
3210 }
3211
3212 /*
3213  * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3214  * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3215  */
3216 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3217         uint8_t scsi_status)
3218 {
3219         struct scsi_qla_host *vha = cmd->vha;
3220         struct qla_qpair *qpair = cmd->qpair;
3221         struct ctio7_to_24xx *pkt;
3222         struct qla_tgt_prm prm;
3223         uint32_t full_req_cnt = 0;
3224         unsigned long flags = 0;
3225         int res;
3226
3227         if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3228             (cmd->sess && cmd->sess->deleted)) {
3229                 cmd->state = QLA_TGT_STATE_PROCESSED;
3230                 return 0;
3231         }
3232
3233         ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3234             "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3235             (xmit_type & QLA_TGT_XMIT_STATUS) ?
3236             1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3237             &cmd->se_cmd, qpair->id);
3238
3239         res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3240             &full_req_cnt);
3241         if (unlikely(res != 0)) {
3242                 return res;
3243         }
3244
3245         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3246
3247         if (xmit_type == QLA_TGT_XMIT_STATUS)
3248                 qpair->tgt_counters.core_qla_snd_status++;
3249         else
3250                 qpair->tgt_counters.core_qla_que_buf++;
3251
3252         if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3253                 /*
3254                  * Either the port is not online or this request was from
3255                  * previous life, just abort the processing.
3256                  */
3257                 cmd->state = QLA_TGT_STATE_PROCESSED;
3258                 ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3259                         "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3260                         vha->flags.online, qla2x00_reset_active(vha),
3261                         cmd->reset_count, qpair->chip_reset);
3262                 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3263                 return 0;
3264         }
3265
3266         /* Does F/W have an IOCBs for this request */
3267         res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3268         if (unlikely(res))
3269                 goto out_unmap_unlock;
3270
3271         if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3272                 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3273         else
3274                 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3275         if (unlikely(res != 0)) {
3276                 qpair->req->cnt += full_req_cnt;
3277                 goto out_unmap_unlock;
3278         }
3279
3280         pkt = (struct ctio7_to_24xx *)prm.pkt;
3281
3282         if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3283                 pkt->u.status0.flags |=
3284                     cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3285                         CTIO7_FLAGS_STATUS_MODE_0);
3286
3287                 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3288                         qlt_load_data_segments(&prm);
3289
3290                 if (prm.add_status_pkt == 0) {
3291                         if (xmit_type & QLA_TGT_XMIT_STATUS) {
3292                                 pkt->u.status0.scsi_status =
3293                                     cpu_to_le16(prm.rq_result);
3294                                 pkt->u.status0.residual =
3295                                     cpu_to_le32(prm.residual);
3296                                 pkt->u.status0.flags |= cpu_to_le16(
3297                                     CTIO7_FLAGS_SEND_STATUS);
3298                                 if (qlt_need_explicit_conf(cmd, 0)) {
3299                                         pkt->u.status0.flags |=
3300                                             cpu_to_le16(
3301                                                 CTIO7_FLAGS_EXPLICIT_CONFORM |
3302                                                 CTIO7_FLAGS_CONFORM_REQ);
3303                                 }
3304                         }
3305
3306                 } else {
3307                         /*
3308                          * We have already made sure that there is sufficient
3309                          * amount of request entries to not drop HW lock in
3310                          * req_pkt().
3311                          */
3312                         struct ctio7_to_24xx *ctio =
3313                                 (struct ctio7_to_24xx *)qlt_get_req_pkt(
3314                                     qpair->req);
3315
3316                         ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3317                             "Building additional status packet 0x%p.\n",
3318                             ctio);
3319
3320                         /*
3321                          * T10Dif: ctio_crc2_to_fw overlay ontop of
3322                          * ctio7_to_24xx
3323                          */
3324                         memcpy(ctio, pkt, sizeof(*ctio));
3325                         /* reset back to CTIO7 */
3326                         ctio->entry_count = 1;
3327                         ctio->entry_type = CTIO_TYPE7;
3328                         ctio->dseg_count = 0;
3329                         ctio->u.status1.flags &= ~cpu_to_le16(
3330                             CTIO7_FLAGS_DATA_IN);
3331
3332                         /* Real finish is ctio_m1's finish */
3333                         pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3334                         pkt->u.status0.flags |= cpu_to_le16(
3335                             CTIO7_FLAGS_DONT_RET_CTIO);
3336
3337                         /* qlt_24xx_init_ctio_to_isp will correct
3338                          * all neccessary fields that's part of CTIO7.
3339                          * There should be no residual of CTIO-CRC2 data.
3340                          */
3341                         qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3342                             &prm);
3343                 }
3344         } else
3345                 qlt_24xx_init_ctio_to_isp(pkt, &prm);
3346
3347
3348         cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3349         cmd->cmd_sent_to_fw = 1;
3350         cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3351
3352         /* Memory Barrier */
3353         wmb();
3354         if (qpair->reqq_start_iocbs)
3355                 qpair->reqq_start_iocbs(qpair);
3356         else
3357                 qla2x00_start_iocbs(vha, qpair->req);
3358         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3359
3360         return 0;
3361
3362 out_unmap_unlock:
3363         qlt_unmap_sg(vha, cmd);
3364         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3365
3366         return res;
3367 }
3368 EXPORT_SYMBOL(qlt_xmit_response);
3369
3370 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3371 {
3372         struct ctio7_to_24xx *pkt;
3373         struct scsi_qla_host *vha = cmd->vha;
3374         struct qla_tgt *tgt = cmd->tgt;
3375         struct qla_tgt_prm prm;
3376         unsigned long flags = 0;
3377         int res = 0;
3378         struct qla_qpair *qpair = cmd->qpair;
3379
3380         memset(&prm, 0, sizeof(prm));
3381         prm.cmd = cmd;
3382         prm.tgt = tgt;
3383         prm.sg = NULL;
3384         prm.req_cnt = 1;
3385
3386         /* Calculate number of entries and segments required */
3387         if (qlt_pci_map_calc_cnt(&prm) != 0)
3388                 return -EAGAIN;
3389
3390         if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3391             (cmd->sess && cmd->sess->deleted)) {
3392                 /*
3393                  * Either the port is not online or this request was from
3394                  * previous life, just abort the processing.
3395                  */
3396                 cmd->aborted = 1;
3397                 cmd->write_data_transferred = 0;
3398                 cmd->state = QLA_TGT_STATE_DATA_IN;
3399                 vha->hw->tgt.tgt_ops->handle_data(cmd);
3400                 ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3401                         "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3402                         vha->flags.online, qla2x00_reset_active(vha),
3403                         cmd->reset_count, qpair->chip_reset);
3404                 return 0;
3405         }
3406
3407         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3408         /* Does F/W have an IOCBs for this request */
3409         res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3410         if (res != 0)
3411                 goto out_unlock_free_unmap;
3412         if (cmd->se_cmd.prot_op)
3413                 res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3414         else
3415                 res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3416
3417         if (unlikely(res != 0)) {
3418                 qpair->req->cnt += prm.req_cnt;
3419                 goto out_unlock_free_unmap;
3420         }
3421
3422         pkt = (struct ctio7_to_24xx *)prm.pkt;
3423         pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3424             CTIO7_FLAGS_STATUS_MODE_0);
3425
3426         if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3427                 qlt_load_data_segments(&prm);
3428
3429         cmd->state = QLA_TGT_STATE_NEED_DATA;
3430         cmd->cmd_sent_to_fw = 1;
3431         cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3432
3433         /* Memory Barrier */
3434         wmb();
3435         if (qpair->reqq_start_iocbs)
3436                 qpair->reqq_start_iocbs(qpair);
3437         else
3438                 qla2x00_start_iocbs(vha, qpair->req);
3439         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3440
3441         return res;
3442
3443 out_unlock_free_unmap:
3444         qlt_unmap_sg(vha, cmd);
3445         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3446
3447         return res;
3448 }
3449 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3450
3451
3452 /*
3453  * it is assumed either hardware_lock or qpair lock is held.
3454  */
3455 static void
3456 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3457         struct ctio_crc_from_fw *sts)
3458 {
3459         uint8_t         *ap = &sts->actual_dif[0];
3460         uint8_t         *ep = &sts->expected_dif[0];
3461         uint64_t        lba = cmd->se_cmd.t_task_lba;
3462         uint8_t scsi_status, sense_key, asc, ascq;
3463         unsigned long flags;
3464         struct scsi_qla_host *vha = cmd->vha;
3465
3466         cmd->trc_flags |= TRC_DIF_ERR;
3467
3468         cmd->a_guard   = get_unaligned_be16(ap + 0);
3469         cmd->a_app_tag = get_unaligned_be16(ap + 2);
3470         cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3471
3472         cmd->e_guard   = get_unaligned_be16(ep + 0);
3473         cmd->e_app_tag = get_unaligned_be16(ep + 2);
3474         cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3475
3476         ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3477             "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3478
3479         scsi_status = sense_key = asc = ascq = 0;
3480
3481         /* check appl tag */
3482         if (cmd->e_app_tag != cmd->a_app_tag) {
3483                 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3484                     "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]",
3485                     cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3486                     cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3487                     cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3488                     cmd->atio.u.isp24.fcp_hdr.ox_id);
3489
3490                 cmd->dif_err_code = DIF_ERR_APP;
3491                 scsi_status = SAM_STAT_CHECK_CONDITION;
3492                 sense_key = ABORTED_COMMAND;
3493                 asc = 0x10;
3494                 ascq = 0x2;
3495         }
3496
3497         /* check ref tag */
3498         if (cmd->e_ref_tag != cmd->a_ref_tag) {
3499                 ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3500                     "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] ",
3501                     cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3502                     cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3503                     cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3504                     cmd->atio.u.isp24.fcp_hdr.ox_id);
3505
3506                 cmd->dif_err_code = DIF_ERR_REF;
3507                 scsi_status = SAM_STAT_CHECK_CONDITION;
3508                 sense_key = ABORTED_COMMAND;
3509                 asc = 0x10;
3510                 ascq = 0x3;
3511                 goto out;
3512         }
3513
3514         /* check guard */
3515         if (cmd->e_guard != cmd->a_guard) {
3516                 ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3517                     "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]",
3518                     cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3519                     cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3520                     cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3521                     cmd->atio.u.isp24.fcp_hdr.ox_id);
3522
3523                 cmd->dif_err_code = DIF_ERR_GRD;
3524                 scsi_status = SAM_STAT_CHECK_CONDITION;
3525                 sense_key = ABORTED_COMMAND;
3526                 asc = 0x10;
3527                 ascq = 0x1;
3528         }
3529 out:
3530         switch (cmd->state) {
3531         case QLA_TGT_STATE_NEED_DATA:
3532                 /* handle_data will load DIF error code  */
3533                 cmd->state = QLA_TGT_STATE_DATA_IN;
3534                 vha->hw->tgt.tgt_ops->handle_data(cmd);
3535                 break;
3536         default:
3537                 spin_lock_irqsave(&cmd->cmd_lock, flags);
3538                 if (cmd->aborted) {
3539                         spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3540                         vha->hw->tgt.tgt_ops->free_cmd(cmd);
3541                         break;
3542                 }
3543                 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3544
3545                 qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3546                     ascq);
3547                 /* assume scsi status gets out on the wire.
3548                  * Will not wait for completion.
3549                  */
3550                 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3551                 break;
3552         }
3553 }
3554
3555 /* If hardware_lock held on entry, might drop it, then reaquire */
3556 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3557 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3558         struct imm_ntfy_from_isp *ntfy)
3559 {
3560         struct nack_to_isp *nack;
3561         struct qla_hw_data *ha = vha->hw;
3562         request_t *pkt;
3563         int ret = 0;
3564
3565         ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3566             "Sending TERM ELS CTIO (ha=%p)\n", ha);
3567
3568         pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3569         if (pkt == NULL) {
3570                 ql_dbg(ql_dbg_tgt, vha, 0xe080,
3571                     "qla_target(%d): %s failed: unable to allocate "
3572                     "request packet\n", vha->vp_idx, __func__);
3573                 return -ENOMEM;
3574         }
3575
3576         pkt->entry_type = NOTIFY_ACK_TYPE;
3577         pkt->entry_count = 1;
3578         pkt->handle = QLA_TGT_SKIP_HANDLE;
3579
3580         nack = (struct nack_to_isp *)pkt;
3581         nack->ox_id = ntfy->ox_id;
3582
3583         nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3584         if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3585                 nack->u.isp24.flags = ntfy->u.isp24.flags &
3586                         cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3587         }
3588
3589         /* terminate */
3590         nack->u.isp24.flags |=
3591                 __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3592
3593         nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3594         nack->u.isp24.status = ntfy->u.isp24.status;
3595         nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3596         nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3597         nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3598         nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3599         nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3600         nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3601
3602         qla2x00_start_iocbs(vha, vha->req);
3603         return ret;
3604 }
3605
3606 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3607         struct imm_ntfy_from_isp *imm, int ha_locked)
3608 {
3609         int rc;
3610
3611         WARN_ON_ONCE(!ha_locked);
3612         rc = __qlt_send_term_imm_notif(vha, imm);
3613         pr_debug("rc = %d\n", rc);
3614 }
3615
3616 /*
3617  * If hardware_lock held on entry, might drop it, then reaquire
3618  * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3619  */
3620 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3621         struct qla_tgt_cmd *cmd,
3622         struct atio_from_isp *atio)
3623 {
3624         struct scsi_qla_host *vha = qpair->vha;
3625         struct ctio7_to_24xx *ctio24;
3626         struct qla_hw_data *ha = vha->hw;
3627         request_t *pkt;
3628         int ret = 0;
3629         uint16_t temp;
3630
3631         ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3632
3633         if (cmd)
3634                 vha = cmd->vha;
3635
3636         pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3637         if (pkt == NULL) {
3638                 ql_dbg(ql_dbg_tgt, vha, 0xe050,
3639                     "qla_target(%d): %s failed: unable to allocate "
3640                     "request packet\n", vha->vp_idx, __func__);
3641                 return -ENOMEM;
3642         }
3643
3644         if (cmd != NULL) {
3645                 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3646                         ql_dbg(ql_dbg_tgt, vha, 0xe051,
3647                             "qla_target(%d): Terminating cmd %p with "
3648                             "incorrect state %d\n", vha->vp_idx, cmd,
3649                             cmd->state);
3650                 } else
3651                         ret = 1;
3652         }
3653
3654         qpair->tgt_counters.num_term_xchg_sent++;
3655         pkt->entry_count = 1;
3656         pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3657
3658         ctio24 = (struct ctio7_to_24xx *)pkt;
3659         ctio24->entry_type = CTIO_TYPE7;
3660         ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3661         ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3662         ctio24->vp_index = vha->vp_idx;
3663         ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3664         ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3665         temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3666                 CTIO7_FLAGS_TERMINATE;
3667         ctio24->u.status1.flags = cpu_to_le16(temp);
3668         temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3669         ctio24->u.status1.ox_id = cpu_to_le16(temp);
3670
3671         /* Memory Barrier */
3672         wmb();
3673         if (qpair->reqq_start_iocbs)
3674                 qpair->reqq_start_iocbs(qpair);
3675         else
3676                 qla2x00_start_iocbs(vha, qpair->req);
3677         return ret;
3678 }
3679
3680 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3681         struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3682         int ul_abort)
3683 {
3684         struct scsi_qla_host *vha;
3685         unsigned long flags = 0;
3686         int rc;
3687
3688         /* why use different vha? NPIV */
3689         if (cmd)
3690                 vha = cmd->vha;
3691         else
3692                 vha = qpair->vha;
3693
3694         if (ha_locked) {
3695                 rc = __qlt_send_term_exchange(qpair, cmd, atio);
3696                 if (rc == -ENOMEM)
3697                         qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3698                 goto done;
3699         }
3700         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3701         rc = __qlt_send_term_exchange(qpair, cmd, atio);
3702         if (rc == -ENOMEM)
3703                 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3704
3705 done:
3706         if (cmd && !ul_abort && !cmd->aborted) {
3707                 if (cmd->sg_mapped)
3708                         qlt_unmap_sg(vha, cmd);
3709                 vha->hw->tgt.tgt_ops->free_cmd(cmd);
3710         }
3711
3712         if (!ha_locked)
3713                 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3714
3715         return;
3716 }
3717
3718 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3719 {
3720         struct list_head free_list;
3721         struct qla_tgt_cmd *cmd, *tcmd;
3722
3723         vha->hw->tgt.leak_exchg_thresh_hold =
3724             (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3725
3726         cmd = tcmd = NULL;
3727         if (!list_empty(&vha->hw->tgt.q_full_list)) {
3728                 INIT_LIST_HEAD(&free_list);
3729                 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3730
3731                 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3732                         list_del(&cmd->cmd_list);
3733                         /* This cmd was never sent to TCM.  There is no need
3734                          * to schedule free or call free_cmd
3735                          */
3736                         qlt_free_cmd(cmd);
3737                         vha->hw->tgt.num_qfull_cmds_alloc--;
3738                 }
3739         }
3740         vha->hw->tgt.num_qfull_cmds_dropped = 0;
3741 }
3742
3743 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3744 {
3745         uint32_t total_leaked;
3746
3747         total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3748
3749         if (vha->hw->tgt.leak_exchg_thresh_hold &&
3750             (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3751
3752                 ql_dbg(ql_dbg_tgt, vha, 0xe079,
3753                     "Chip reset due to exchange starvation: %d/%d.\n",
3754                     total_leaked, vha->hw->cur_fw_xcb_count);
3755
3756                 if (IS_P3P_TYPE(vha->hw))
3757                         set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3758                 else
3759                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3760                 qla2xxx_wake_dpc(vha);
3761         }
3762
3763 }
3764
3765 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3766 {
3767         struct qla_tgt *tgt = cmd->tgt;
3768         struct scsi_qla_host *vha = tgt->vha;
3769         struct se_cmd *se_cmd = &cmd->se_cmd;
3770         unsigned long flags;
3771
3772         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3773             "qla_target(%d): terminating exchange for aborted cmd=%p "
3774             "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3775             se_cmd->tag);
3776
3777         spin_lock_irqsave(&cmd->cmd_lock, flags);
3778         if (cmd->aborted) {
3779                 spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3780                 /*
3781                  * It's normal to see 2 calls in this path:
3782                  *  1) XFER Rdy completion + CMD_T_ABORT
3783                  *  2) TCM TMR - drain_state_list
3784                  */
3785                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3786                     "multiple abort. %p transport_state %x, t_state %x, "
3787                     "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3788                     cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3789                 return -EIO;
3790         }
3791         cmd->aborted = 1;
3792         cmd->trc_flags |= TRC_ABORT;
3793         spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3794
3795         qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3796         return 0;
3797 }
3798 EXPORT_SYMBOL(qlt_abort_cmd);
3799
3800 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3801 {
3802         struct fc_port *sess = cmd->sess;
3803
3804         ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3805             "%s: se_cmd[%p] ox_id %04x\n",
3806             __func__, &cmd->se_cmd,
3807             be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3808
3809         BUG_ON(cmd->cmd_in_wq);
3810
3811         if (cmd->sg_mapped)
3812                 qlt_unmap_sg(cmd->vha, cmd);
3813
3814         if (!cmd->q_full)
3815                 qlt_decr_num_pend_cmds(cmd->vha);
3816
3817         BUG_ON(cmd->sg_mapped);
3818         cmd->jiffies_at_free = get_jiffies_64();
3819         if (unlikely(cmd->free_sg))
3820                 kfree(cmd->sg);
3821
3822         if (!sess || !sess->se_sess) {
3823                 WARN_ON(1);
3824                 return;
3825         }
3826         cmd->jiffies_at_free = get_jiffies_64();
3827         cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3828 }
3829 EXPORT_SYMBOL(qlt_free_cmd);
3830
3831 /*
3832  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3833  */
3834 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3835         struct qla_tgt_cmd *cmd, uint32_t status)
3836 {
3837         int term = 0;
3838         struct scsi_qla_host *vha = qpair->vha;
3839
3840         if (cmd->se_cmd.prot_op)
3841                 ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3842                     "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3843                     "se_cmd=%p tag[%x] op %#x/%s",
3844                      cmd->lba, cmd->lba,
3845                      cmd->num_blks, &cmd->se_cmd,
3846                      cmd->atio.u.isp24.exchange_addr,
3847                      cmd->se_cmd.prot_op,
3848                      prot_op_str(cmd->se_cmd.prot_op));
3849
3850         if (ctio != NULL) {
3851                 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3852
3853                 term = !(c->flags &
3854                     cpu_to_le16(OF_TERM_EXCH));
3855         } else
3856                 term = 1;
3857
3858         if (term)
3859                 qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3860
3861         return term;
3862 }
3863
3864
3865 /* ha->hardware_lock supposed to be held on entry */
3866 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3867         struct rsp_que *rsp, uint32_t handle, void *ctio)
3868 {
3869         void *cmd = NULL;
3870         struct req_que *req;
3871         int qid = GET_QID(handle);
3872         uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3873
3874         if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3875                 return NULL;
3876
3877         if (qid == rsp->req->id) {
3878                 req = rsp->req;
3879         } else if (vha->hw->req_q_map[qid]) {
3880                 ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3881                     "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3882                     vha->vp_idx, rsp->id, handle);
3883                 req = vha->hw->req_q_map[qid];
3884         } else {
3885                 return NULL;
3886         }
3887
3888         h &= QLA_CMD_HANDLE_MASK;
3889
3890         if (h != QLA_TGT_NULL_HANDLE) {
3891                 if (unlikely(h >= req->num_outstanding_cmds)) {
3892                         ql_dbg(ql_dbg_tgt, vha, 0xe052,
3893                             "qla_target(%d): Wrong handle %x received\n",
3894                             vha->vp_idx, handle);
3895                         return NULL;
3896                 }
3897
3898                 cmd = req->outstanding_cmds[h];
3899                 if (unlikely(cmd == NULL)) {
3900                         ql_dbg(ql_dbg_async, vha, 0xe053,
3901                             "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3902                                 vha->vp_idx, handle, req->id, rsp->id);
3903                         return NULL;
3904                 }
3905                 req->outstanding_cmds[h] = NULL;
3906         } else if (ctio != NULL) {
3907                 /* We can't get loop ID from CTIO7 */
3908                 ql_dbg(ql_dbg_tgt, vha, 0xe054,
3909                     "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3910                     "support NULL handles\n", vha->vp_idx);
3911                 return NULL;
3912         }
3913
3914         return cmd;
3915 }
3916
3917 /*
3918  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3919  */
3920 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3921     struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3922 {
3923         struct qla_hw_data *ha = vha->hw;
3924         struct se_cmd *se_cmd;
3925         struct qla_tgt_cmd *cmd;
3926         struct qla_qpair *qpair = rsp->qpair;
3927
3928         if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3929                 /* That could happen only in case of an error/reset/abort */
3930                 if (status != CTIO_SUCCESS) {
3931                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3932                             "Intermediate CTIO received"
3933                             " (status %x)\n", status);
3934                 }
3935                 return;
3936         }
3937
3938         cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3939         if (cmd == NULL)
3940                 return;
3941
3942         se_cmd = &cmd->se_cmd;
3943         cmd->cmd_sent_to_fw = 0;
3944
3945         qlt_unmap_sg(vha, cmd);
3946
3947         if (unlikely(status != CTIO_SUCCESS)) {
3948                 switch (status & 0xFFFF) {
3949                 case CTIO_INVALID_RX_ID:
3950                         if (printk_ratelimit())
3951                                 dev_info(&vha->hw->pdev->dev,
3952                                     "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
3953                                     vha->vp_idx, cmd->atio.u.isp24.attr,
3954                                     ((cmd->ctio_flags >> 9) & 0xf),
3955                                     cmd->ctio_flags);
3956
3957                         break;
3958                 case CTIO_LIP_RESET:
3959                 case CTIO_TARGET_RESET:
3960                 case CTIO_ABORTED:
3961                         /* driver request abort via Terminate exchange */
3962                 case CTIO_TIMEOUT:
3963                         /* They are OK */
3964                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3965                             "qla_target(%d): CTIO with "
3966                             "status %#x received, state %x, se_cmd %p, "
3967                             "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3968                             "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3969                             status, cmd->state, se_cmd);
3970                         break;
3971
3972                 case CTIO_PORT_LOGGED_OUT:
3973                 case CTIO_PORT_UNAVAILABLE:
3974                 {
3975                         int logged_out =
3976                                 (status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
3977
3978                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3979                             "qla_target(%d): CTIO with %s status %x "
3980                             "received (state %x, se_cmd %p)\n", vha->vp_idx,
3981                             logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3982                             status, cmd->state, se_cmd);
3983
3984                         if (logged_out && cmd->sess) {
3985                                 /*
3986                                  * Session is already logged out, but we need
3987                                  * to notify initiator, who's not aware of this
3988                                  */
3989                                 cmd->sess->send_els_logo = 1;
3990                                 ql_dbg(ql_dbg_disc, vha, 0x20f8,
3991                                     "%s %d %8phC post del sess\n",
3992                                     __func__, __LINE__, cmd->sess->port_name);
3993
3994                                 qlt_schedule_sess_for_deletion(cmd->sess);
3995                         }
3996                         break;
3997                 }
3998                 case CTIO_DIF_ERROR: {
3999                         struct ctio_crc_from_fw *crc =
4000                                 (struct ctio_crc_from_fw *)ctio;
4001                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4002                             "qla_target(%d): CTIO with DIF_ERROR status %x "
4003                             "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4004                             "expect_dif[0x%llx]\n",
4005                             vha->vp_idx, status, cmd->state, se_cmd,
4006                             *((u64 *)&crc->actual_dif[0]),
4007                             *((u64 *)&crc->expected_dif[0]));
4008
4009                         qlt_handle_dif_error(qpair, cmd, ctio);
4010                         return;
4011                 }
4012                 default:
4013                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4014                             "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4015                             vha->vp_idx, status, cmd->state, se_cmd);
4016                         break;
4017                 }
4018
4019
4020                 /* "cmd->aborted" means
4021                  * cmd is already aborted/terminated, we don't
4022                  * need to terminate again.  The exchange is already
4023                  * cleaned up/freed at FW level.  Just cleanup at driver
4024                  * level.
4025                  */
4026                 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4027                     (!cmd->aborted)) {
4028                         cmd->trc_flags |= TRC_CTIO_ERR;
4029                         if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4030                                 return;
4031                 }
4032         }
4033
4034         if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4035                 cmd->trc_flags |= TRC_CTIO_DONE;
4036         } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4037                 cmd->state = QLA_TGT_STATE_DATA_IN;
4038
4039                 if (status == CTIO_SUCCESS)
4040                         cmd->write_data_transferred = 1;
4041
4042                 ha->tgt.tgt_ops->handle_data(cmd);
4043                 return;
4044         } else if (cmd->aborted) {
4045                 cmd->trc_flags |= TRC_CTIO_ABORTED;
4046                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4047                   "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4048         } else {
4049                 cmd->trc_flags |= TRC_CTIO_STRANGE;
4050                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4051                     "qla_target(%d): A command in state (%d) should "
4052                     "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4053         }
4054
4055         if (unlikely(status != CTIO_SUCCESS) &&
4056                 !cmd->aborted) {
4057                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4058                 dump_stack();
4059         }
4060
4061         ha->tgt.tgt_ops->free_cmd(cmd);
4062 }
4063
4064 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4065         uint8_t task_codes)
4066 {
4067         int fcp_task_attr;
4068
4069         switch (task_codes) {
4070         case ATIO_SIMPLE_QUEUE:
4071                 fcp_task_attr = TCM_SIMPLE_TAG;
4072                 break;
4073         case ATIO_HEAD_OF_QUEUE:
4074                 fcp_task_attr = TCM_HEAD_TAG;
4075                 break;
4076         case ATIO_ORDERED_QUEUE:
4077                 fcp_task_attr = TCM_ORDERED_TAG;
4078                 break;
4079         case ATIO_ACA_QUEUE:
4080                 fcp_task_attr = TCM_ACA_TAG;
4081                 break;
4082         case ATIO_UNTAGGED:
4083                 fcp_task_attr = TCM_SIMPLE_TAG;
4084                 break;
4085         default:
4086                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4087                     "qla_target: unknown task code %x, use ORDERED instead\n",
4088                     task_codes);
4089                 fcp_task_attr = TCM_ORDERED_TAG;
4090                 break;
4091         }
4092
4093         return fcp_task_attr;
4094 }
4095
4096 /*
4097  * Process context for I/O path into tcm_qla2xxx code
4098  */
4099 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4100 {
4101         scsi_qla_host_t *vha = cmd->vha;
4102         struct qla_hw_data *ha = vha->hw;
4103         struct fc_port *sess = cmd->sess;
4104         struct atio_from_isp *atio = &cmd->atio;
4105         unsigned char *cdb;
4106         unsigned long flags;
4107         uint32_t data_length;
4108         int ret, fcp_task_attr, data_dir, bidi = 0;
4109         struct qla_qpair *qpair = cmd->qpair;
4110
4111         cmd->cmd_in_wq = 0;
4112         cmd->trc_flags |= TRC_DO_WORK;
4113
4114         if (cmd->aborted) {
4115                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4116                     "cmd with tag %u is aborted\n",
4117                     cmd->atio.u.isp24.exchange_addr);
4118                 goto out_term;
4119         }
4120
4121         spin_lock_init(&cmd->cmd_lock);
4122         cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4123         cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4124
4125         if (atio->u.isp24.fcp_cmnd.rddata &&
4126             atio->u.isp24.fcp_cmnd.wrdata) {
4127                 bidi = 1;
4128                 data_dir = DMA_TO_DEVICE;
4129         } else if (atio->u.isp24.fcp_cmnd.rddata)
4130                 data_dir = DMA_FROM_DEVICE;
4131         else if (atio->u.isp24.fcp_cmnd.wrdata)
4132                 data_dir = DMA_TO_DEVICE;
4133         else
4134                 data_dir = DMA_NONE;
4135
4136         fcp_task_attr = qlt_get_fcp_task_attr(vha,
4137             atio->u.isp24.fcp_cmnd.task_attr);
4138         data_length = get_datalen_for_atio(atio);
4139
4140         ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4141                                           fcp_task_attr, data_dir, bidi);
4142         if (ret != 0)
4143                 goto out_term;
4144         /*
4145          * Drop extra session reference from qlt_handle_cmd_for_atio().
4146          */
4147         ha->tgt.tgt_ops->put_sess(sess);
4148         return;
4149
4150 out_term:
4151         ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4152         /*
4153          * cmd has not sent to target yet, so pass NULL as the second
4154          * argument to qlt_send_term_exchange() and free the memory here.
4155          */
4156         cmd->trc_flags |= TRC_DO_WORK_ERR;
4157         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4158         qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4159
4160         qlt_decr_num_pend_cmds(vha);
4161         cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4162         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4163
4164         ha->tgt.tgt_ops->put_sess(sess);
4165 }
4166
4167 static void qlt_do_work(struct work_struct *work)
4168 {
4169         struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4170         scsi_qla_host_t *vha = cmd->vha;
4171         unsigned long flags;
4172
4173         spin_lock_irqsave(&vha->cmd_list_lock, flags);
4174         list_del(&cmd->cmd_list);
4175         spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4176
4177         __qlt_do_work(cmd);
4178 }
4179
4180 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4181 {
4182         unsigned long flags;
4183         struct qla_hw_data *ha = vha->hw;
4184         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4185         void *node;
4186         u64 key = 0;
4187
4188         ql_log(ql_log_info, vha, 0x706c,
4189             "User update Number of Active Qpairs %d\n",
4190             ha->tgt.num_act_qpairs);
4191
4192         spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4193
4194         btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4195                 btree_remove64(&tgt->lun_qpair_map, key);
4196
4197         ha->base_qpair->lun_cnt = 0;
4198         for (key = 0; key < ha->max_qpairs; key++)
4199                 if (ha->queue_pair_map[key])
4200                         ha->queue_pair_map[key]->lun_cnt = 0;
4201
4202         spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4203 }
4204
4205 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4206         struct qla_tgt_cmd *cmd)
4207 {
4208         struct qla_qpair *qpair, *qp;
4209         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4210         struct qla_qpair_hint *h;
4211
4212         if (vha->flags.qpairs_available) {
4213                 h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4214                 if (unlikely(!h)) {
4215                         /* spread lun to qpair ratio evently */
4216                         int lcnt = 0, rc;
4217                         struct scsi_qla_host *base_vha =
4218                                 pci_get_drvdata(vha->hw->pdev);
4219
4220                         qpair = vha->hw->base_qpair;
4221                         if (qpair->lun_cnt == 0) {
4222                                 qpair->lun_cnt++;
4223                                 h = qla_qpair_to_hint(tgt, qpair);
4224                                 BUG_ON(!h);
4225                                 rc = btree_insert64(&tgt->lun_qpair_map,
4226                                         cmd->unpacked_lun, h, GFP_ATOMIC);
4227                                 if (rc) {
4228                                         qpair->lun_cnt--;
4229                                         ql_log(ql_log_info, vha, 0xd037,
4230                                             "Unable to insert lun %llx into lun_qpair_map\n",
4231                                             cmd->unpacked_lun);
4232                                 }
4233                                 goto out;
4234                         } else {
4235                                 lcnt = qpair->lun_cnt;
4236                         }
4237
4238                         h = NULL;
4239                         list_for_each_entry(qp, &base_vha->qp_list,
4240                             qp_list_elem) {
4241                                 if (qp->lun_cnt == 0) {
4242                                         qp->lun_cnt++;
4243                                         h = qla_qpair_to_hint(tgt, qp);
4244                                         BUG_ON(!h);
4245                                         rc = btree_insert64(&tgt->lun_qpair_map,
4246                                             cmd->unpacked_lun, h, GFP_ATOMIC);
4247                                         if (rc) {
4248                                                 qp->lun_cnt--;
4249                                                 ql_log(ql_log_info, vha, 0xd038,
4250                                                         "Unable to insert lun %llx into lun_qpair_map\n",
4251                                                         cmd->unpacked_lun);
4252                                         }
4253                                         qpair = qp;
4254                                         goto out;
4255                                 } else {
4256                                         if (qp->lun_cnt < lcnt) {
4257                                                 lcnt = qp->lun_cnt;
4258                                                 qpair = qp;
4259                                                 continue;
4260                                         }
4261                                 }
4262                         }
4263                         BUG_ON(!qpair);
4264                         qpair->lun_cnt++;
4265                         h = qla_qpair_to_hint(tgt, qpair);
4266                         BUG_ON(!h);
4267                         rc = btree_insert64(&tgt->lun_qpair_map,
4268                                 cmd->unpacked_lun, h, GFP_ATOMIC);
4269                         if (rc) {
4270                                 qpair->lun_cnt--;
4271                                 ql_log(ql_log_info, vha, 0xd039,
4272                                    "Unable to insert lun %llx into lun_qpair_map\n",
4273                                    cmd->unpacked_lun);
4274                         }
4275                 }
4276         } else {
4277                 h = &tgt->qphints[0];
4278         }
4279 out:
4280         cmd->qpair = h->qpair;
4281         cmd->se_cmd.cpuid = h->cpuid;
4282 }
4283
4284 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4285                                        struct fc_port *sess,
4286                                        struct atio_from_isp *atio)
4287 {
4288         struct qla_tgt_cmd *cmd;
4289
4290         cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4291         if (!cmd)
4292                 return NULL;
4293
4294         cmd->cmd_type = TYPE_TGT_CMD;
4295         memcpy(&cmd->atio, atio, sizeof(*atio));
4296         INIT_LIST_HEAD(&cmd->sess_cmd_list);
4297         cmd->state = QLA_TGT_STATE_NEW;
4298         cmd->tgt = vha->vha_tgt.qla_tgt;
4299         qlt_incr_num_pend_cmds(vha);
4300         cmd->vha = vha;
4301         cmd->sess = sess;
4302         cmd->loop_id = sess->loop_id;
4303         cmd->conf_compl_supported = sess->conf_compl_supported;
4304
4305         cmd->trc_flags = 0;
4306         cmd->jiffies_at_alloc = get_jiffies_64();
4307
4308         cmd->unpacked_lun = scsilun_to_int(
4309             (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4310         qlt_assign_qpair(vha, cmd);
4311         cmd->reset_count = vha->hw->base_qpair->chip_reset;
4312         cmd->vp_idx = vha->vp_idx;
4313
4314         return cmd;
4315 }
4316
4317 /* ha->hardware_lock supposed to be held on entry */
4318 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4319         struct atio_from_isp *atio)
4320 {
4321         struct qla_hw_data *ha = vha->hw;
4322         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4323         struct fc_port *sess;
4324         struct qla_tgt_cmd *cmd;
4325         unsigned long flags;
4326         port_id_t id;
4327
4328         if (unlikely(tgt->tgt_stop)) {
4329                 ql_dbg(ql_dbg_io, vha, 0x3061,
4330                     "New command while device %p is shutting down\n", tgt);
4331                 return -ENODEV;
4332         }
4333
4334         id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4335         if (IS_SW_RESV_ADDR(id))
4336                 return -EBUSY;
4337
4338         sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4339         if (unlikely(!sess))
4340                 return -EFAULT;
4341
4342         /* Another WWN used to have our s_id. Our PLOGI scheduled its
4343          * session deletion, but it's still in sess_del_work wq */
4344         if (sess->deleted) {
4345                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4346                     "New command while old session %p is being deleted\n",
4347                     sess);
4348                 return -EFAULT;
4349         }
4350
4351         /*
4352          * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4353          */
4354         if (!kref_get_unless_zero(&sess->sess_kref)) {
4355                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4356                     "%s: kref_get fail, %8phC oxid %x \n",
4357                     __func__, sess->port_name,
4358                      be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4359                 return -EFAULT;
4360         }
4361
4362         cmd = qlt_get_tag(vha, sess, atio);
4363         if (!cmd) {
4364                 ql_dbg(ql_dbg_io, vha, 0x3062,
4365                     "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4366                 ha->tgt.tgt_ops->put_sess(sess);
4367                 return -EBUSY;
4368         }
4369
4370         cmd->cmd_in_wq = 1;
4371         cmd->trc_flags |= TRC_NEW_CMD;
4372
4373         spin_lock_irqsave(&vha->cmd_list_lock, flags);
4374         list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4375         spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4376
4377         INIT_WORK(&cmd->work, qlt_do_work);
4378         if (vha->flags.qpairs_available) {
4379                 queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4380         } else if (ha->msix_count) {
4381                 if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4382                         queue_work_on(smp_processor_id(), qla_tgt_wq,
4383                             &cmd->work);
4384                 else
4385                         queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4386                             &cmd->work);
4387         } else {
4388                 queue_work(qla_tgt_wq, &cmd->work);
4389         }
4390
4391         return 0;
4392 }
4393
4394 /* ha->hardware_lock supposed to be held on entry */
4395 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4396         int fn, void *iocb, int flags)
4397 {
4398         struct scsi_qla_host *vha = sess->vha;
4399         struct qla_hw_data *ha = vha->hw;
4400         struct qla_tgt_mgmt_cmd *mcmd;
4401         struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4402         struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4403
4404         mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4405         if (!mcmd) {
4406                 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4407                     "qla_target(%d): Allocation of management "
4408                     "command failed, some commands and their data could "
4409                     "leak\n", vha->vp_idx);
4410                 return -ENOMEM;
4411         }
4412         memset(mcmd, 0, sizeof(*mcmd));
4413         mcmd->sess = sess;
4414
4415         if (iocb) {
4416                 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4417                     sizeof(mcmd->orig_iocb.imm_ntfy));
4418         }
4419         mcmd->tmr_func = fn;
4420         mcmd->flags = flags;
4421         mcmd->reset_count = ha->base_qpair->chip_reset;
4422         mcmd->qpair = h->qpair;
4423         mcmd->vha = vha;
4424         mcmd->se_cmd.cpuid = h->cpuid;
4425         mcmd->unpacked_lun = lun;
4426
4427         switch (fn) {
4428         case QLA_TGT_LUN_RESET:
4429         case QLA_TGT_CLEAR_TS:
4430         case QLA_TGT_ABORT_TS:
4431                 abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4432                 fallthrough;
4433         case QLA_TGT_CLEAR_ACA:
4434                 h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4435                 mcmd->qpair = h->qpair;
4436                 mcmd->se_cmd.cpuid = h->cpuid;
4437                 break;
4438
4439         case QLA_TGT_TARGET_RESET:
4440         case QLA_TGT_NEXUS_LOSS_SESS:
4441         case QLA_TGT_NEXUS_LOSS:
4442         case QLA_TGT_ABORT_ALL:
4443         default:
4444                 /* no-op */
4445                 break;
4446         }
4447
4448         INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4449         queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4450             &mcmd->work);
4451
4452         return 0;
4453 }
4454
4455 /* ha->hardware_lock supposed to be held on entry */
4456 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4457 {
4458         struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4459         struct qla_hw_data *ha = vha->hw;
4460         struct fc_port *sess;
4461         u64 unpacked_lun;
4462         int fn;
4463         unsigned long flags;
4464
4465         fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4466
4467         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4468         sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4469             a->u.isp24.fcp_hdr.s_id);
4470         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4471
4472         unpacked_lun =
4473             scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4474
4475         if (sess == NULL || sess->deleted)
4476                 return -EFAULT;
4477
4478         return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4479 }
4480
4481 /* ha->hardware_lock supposed to be held on entry */
4482 static int __qlt_abort_task(struct scsi_qla_host *vha,
4483         struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4484 {
4485         struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4486         struct qla_hw_data *ha = vha->hw;
4487         struct qla_tgt_mgmt_cmd *mcmd;
4488         u64 unpacked_lun;
4489         int rc;
4490
4491         mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4492         if (mcmd == NULL) {
4493                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4494                     "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4495                     vha->vp_idx, __func__);
4496                 return -ENOMEM;
4497         }
4498         memset(mcmd, 0, sizeof(*mcmd));
4499
4500         mcmd->sess = sess;
4501         memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4502             sizeof(mcmd->orig_iocb.imm_ntfy));
4503
4504         unpacked_lun =
4505             scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4506         mcmd->reset_count = ha->base_qpair->chip_reset;
4507         mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4508         mcmd->qpair = ha->base_qpair;
4509
4510         rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4511             le16_to_cpu(iocb->u.isp2x.seq_id));
4512         if (rc != 0) {
4513                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4514                     "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4515                     vha->vp_idx, rc);
4516                 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4517                 return -EFAULT;
4518         }
4519
4520         return 0;
4521 }
4522
4523 /* ha->hardware_lock supposed to be held on entry */
4524 static int qlt_abort_task(struct scsi_qla_host *vha,
4525         struct imm_ntfy_from_isp *iocb)
4526 {
4527         struct qla_hw_data *ha = vha->hw;
4528         struct fc_port *sess;
4529         int loop_id;
4530         unsigned long flags;
4531
4532         loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4533
4534         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4535         sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4536         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4537
4538         if (sess == NULL) {
4539                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4540                     "qla_target(%d): task abort for unexisting "
4541                     "session\n", vha->vp_idx);
4542                 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4543                     QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4544         }
4545
4546         return __qlt_abort_task(vha, iocb, sess);
4547 }
4548
4549 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4550 {
4551         if (rc != MBS_COMMAND_COMPLETE) {
4552                 ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4553                         "%s: se_sess %p / sess %p from"
4554                         " port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4555                         " LOGO failed: %#x\n",
4556                         __func__,
4557                         fcport->se_sess,
4558                         fcport,
4559                         fcport->port_name, fcport->loop_id,
4560                         fcport->d_id.b.domain, fcport->d_id.b.area,
4561                         fcport->d_id.b.al_pa, rc);
4562         }
4563
4564         fcport->logout_completed = 1;
4565 }
4566
4567 /*
4568 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4569 *
4570 * Schedules sessions with matching port_id/loop_id but different wwn for
4571 * deletion. Returns existing session with matching wwn if present.
4572 * Null otherwise.
4573 */
4574 struct fc_port *
4575 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4576     port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4577 {
4578         struct fc_port *sess = NULL, *other_sess;
4579         uint64_t other_wwn;
4580
4581         *conflict_sess = NULL;
4582
4583         list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4584
4585                 other_wwn = wwn_to_u64(other_sess->port_name);
4586
4587                 if (wwn == other_wwn) {
4588                         WARN_ON(sess);
4589                         sess = other_sess;
4590                         continue;
4591                 }
4592
4593                 /* find other sess with nport_id collision */
4594                 if (port_id.b24 == other_sess->d_id.b24) {
4595                         if (loop_id != other_sess->loop_id) {
4596                                 ql_dbg(ql_dbg_disc, vha, 0x1000c,
4597                                     "Invalidating sess %p loop_id %d wwn %llx.\n",
4598                                     other_sess, other_sess->loop_id, other_wwn);
4599
4600                                 /*
4601                                  * logout_on_delete is set by default, but another
4602                                  * session that has the same s_id/loop_id combo
4603                                  * might have cleared it when requested this session
4604                                  * deletion, so don't touch it
4605                                  */
4606                                 qlt_schedule_sess_for_deletion(other_sess);
4607                         } else {
4608                                 /*
4609                                  * Another wwn used to have our s_id/loop_id
4610                                  * kill the session, but don't free the loop_id
4611                                  */
4612                                 ql_dbg(ql_dbg_disc, vha, 0xf01b,
4613                                     "Invalidating sess %p loop_id %d wwn %llx.\n",
4614                                     other_sess, other_sess->loop_id, other_wwn);
4615
4616                                 other_sess->keep_nport_handle = 1;
4617                                 if (other_sess->disc_state != DSC_DELETED)
4618                                         *conflict_sess = other_sess;
4619                                 qlt_schedule_sess_for_deletion(other_sess);
4620                         }
4621                         continue;
4622                 }
4623
4624                 /* find other sess with nport handle collision */
4625                 if ((loop_id == other_sess->loop_id) &&
4626                         (loop_id != FC_NO_LOOP_ID)) {
4627                         ql_dbg(ql_dbg_disc, vha, 0x1000d,
4628                                "Invalidating sess %p loop_id %d wwn %llx.\n",
4629                                other_sess, other_sess->loop_id, other_wwn);
4630
4631                         /* Same loop_id but different s_id
4632                          * Ok to kill and logout */
4633                         qlt_schedule_sess_for_deletion(other_sess);
4634                 }
4635         }
4636
4637         return sess;
4638 }
4639
4640 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4641 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4642 {
4643         struct qla_tgt_sess_op *op;
4644         struct qla_tgt_cmd *cmd;
4645         uint32_t key;
4646         int count = 0;
4647         unsigned long flags;
4648
4649         key = (((u32)s_id->b.domain << 16) |
4650                ((u32)s_id->b.area   <<  8) |
4651                ((u32)s_id->b.al_pa));
4652
4653         spin_lock_irqsave(&vha->cmd_list_lock, flags);
4654         list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
4655                 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4656
4657                 if (op_key == key) {
4658                         op->aborted = true;
4659                         count++;
4660                 }
4661         }
4662
4663         list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4664                 uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4665
4666                 if (op_key == key) {
4667                         op->aborted = true;
4668                         count++;
4669                 }
4670         }
4671
4672         list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4673                 uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4674
4675                 if (cmd_key == key) {
4676                         cmd->aborted = 1;
4677                         count++;
4678                 }
4679         }
4680         spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4681
4682         return count;
4683 }
4684
4685 static int qlt_handle_login(struct scsi_qla_host *vha,
4686     struct imm_ntfy_from_isp *iocb)
4687 {
4688         struct fc_port *sess = NULL, *conflict_sess = NULL;
4689         uint64_t wwn;
4690         port_id_t port_id;
4691         uint16_t loop_id, wd3_lo;
4692         int res = 0;
4693         struct qlt_plogi_ack_t *pla;
4694         unsigned long flags;
4695
4696         lockdep_assert_held(&vha->hw->hardware_lock);
4697
4698         wwn = wwn_to_u64(iocb->u.isp24.port_name);
4699
4700         port_id.b.domain = iocb->u.isp24.port_id[2];
4701         port_id.b.area   = iocb->u.isp24.port_id[1];
4702         port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4703         port_id.b.rsvd_1 = 0;
4704
4705         loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4706
4707         /* Mark all stale commands sitting in qla_tgt_wq for deletion */
4708         abort_cmds_for_s_id(vha, &port_id);
4709
4710         if (wwn) {
4711                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4712                 sess = qlt_find_sess_invalidate_other(vha, wwn,
4713                     port_id, loop_id, &conflict_sess);
4714                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4715         } else {
4716                 ql_dbg(ql_dbg_disc, vha, 0xffff,
4717                     "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4718                     __func__, __LINE__, loop_id, port_id.b24);
4719                 qlt_send_term_imm_notif(vha, iocb, 1);
4720                 goto out;
4721         }
4722
4723         if (IS_SW_RESV_ADDR(port_id)) {
4724                 res = 1;
4725                 goto out;
4726         }
4727
4728         pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4729         if (!pla) {
4730                 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4731                     "%s %d %8phC Term INOT due to mem alloc fail",
4732                     __func__, __LINE__,
4733                     iocb->u.isp24.port_name);
4734                 qlt_send_term_imm_notif(vha, iocb, 1);
4735                 goto out;
4736         }
4737
4738         if (conflict_sess) {
4739                 conflict_sess->login_gen++;
4740                 qlt_plogi_ack_link(vha, pla, conflict_sess,
4741                     QLT_PLOGI_LINK_CONFLICT);
4742         }
4743
4744         if (!sess) {
4745                 pla->ref_count++;
4746                 ql_dbg(ql_dbg_disc, vha, 0xffff,
4747                     "%s %d %8phC post new sess\n",
4748                     __func__, __LINE__, iocb->u.isp24.port_name);
4749                 if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4750                         qla24xx_post_newsess_work(vha, &port_id,
4751                             iocb->u.isp24.port_name,
4752                             iocb->u.isp24.u.plogi.node_name,
4753                             pla, 0);
4754                 else
4755                         qla24xx_post_newsess_work(vha, &port_id,
4756                             iocb->u.isp24.port_name, NULL,
4757                             pla, 0);
4758
4759                 goto out;
4760         }
4761
4762         if (sess->disc_state == DSC_UPD_FCPORT) {
4763                 u16 sec;
4764
4765                 /*
4766                  * Remote port registration is still going on from
4767                  * previous login. Allow it to finish before we
4768                  * accept the new login.
4769                  */
4770                 sess->next_disc_state = DSC_DELETE_PEND;
4771                 sec = jiffies_to_msecs(jiffies -
4772                     sess->jiffies_at_registration) / 1000;
4773                 if (sess->sec_since_registration < sec && sec &&
4774                     !(sec % 5)) {
4775                         sess->sec_since_registration = sec;
4776                         ql_dbg(ql_dbg_disc, vha, 0xffff,
4777                             "%s %8phC - Slow Rport registration (%d Sec)\n",
4778                             __func__, sess->port_name, sec);
4779                 }
4780
4781                 if (!conflict_sess) {
4782                         list_del(&pla->list);
4783                         kmem_cache_free(qla_tgt_plogi_cachep, pla);
4784                 }
4785
4786                 qlt_send_term_imm_notif(vha, iocb, 1);
4787                 goto out;
4788         }
4789
4790         qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4791         sess->d_id = port_id;
4792         sess->login_gen++;
4793
4794         if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4795                 sess->fw_login_state = DSC_LS_PRLI_PEND;
4796                 sess->local = 0;
4797                 sess->loop_id = loop_id;
4798                 sess->d_id = port_id;
4799                 sess->fw_login_state = DSC_LS_PRLI_PEND;
4800                 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4801
4802                 if (wd3_lo & BIT_7)
4803                         sess->conf_compl_supported = 1;
4804
4805                 if ((wd3_lo & BIT_4) == 0)
4806                         sess->port_type = FCT_INITIATOR;
4807                 else
4808                         sess->port_type = FCT_TARGET;
4809
4810         } else
4811                 sess->fw_login_state = DSC_LS_PLOGI_PEND;
4812
4813
4814         ql_dbg(ql_dbg_disc, vha, 0x20f9,
4815             "%s %d %8phC  DS %d\n",
4816             __func__, __LINE__, sess->port_name, sess->disc_state);
4817
4818         switch (sess->disc_state) {
4819         case DSC_DELETED:
4820         case DSC_LOGIN_PEND:
4821                 qlt_plogi_ack_unref(vha, pla);
4822                 break;
4823
4824         default:
4825                 /*
4826                  * Under normal circumstances we want to release nport handle
4827                  * during LOGO process to avoid nport handle leaks inside FW.
4828                  * The exception is when LOGO is done while another PLOGI with
4829                  * the same nport handle is waiting as might be the case here.
4830                  * Note: there is always a possibily of a race where session
4831                  * deletion has already started for other reasons (e.g. ACL
4832                  * removal) and now PLOGI arrives:
4833                  * 1. if PLOGI arrived in FW after nport handle has been freed,
4834                  *    FW must have assigned this PLOGI a new/same handle and we
4835                  *    can proceed ACK'ing it as usual when session deletion
4836                  *    completes.
4837                  * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4838                  *    bit reached it, the handle has now been released. We'll
4839                  *    get an error when we ACK this PLOGI. Nothing will be sent
4840                  *    back to initiator. Initiator should eventually retry
4841                  *    PLOGI and situation will correct itself.
4842                  */
4843                 sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4844                     (sess->d_id.b24 == port_id.b24));
4845
4846                 ql_dbg(ql_dbg_disc, vha, 0x20f9,
4847                     "%s %d %8phC post del sess\n",
4848                     __func__, __LINE__, sess->port_name);
4849
4850
4851                 qlt_schedule_sess_for_deletion(sess);
4852                 break;
4853         }
4854 out:
4855         return res;
4856 }
4857
4858 /*
4859  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4860  */
4861 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4862         struct imm_ntfy_from_isp *iocb)
4863 {
4864         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4865         struct qla_hw_data *ha = vha->hw;
4866         struct fc_port *sess = NULL, *conflict_sess = NULL;
4867         uint64_t wwn;
4868         port_id_t port_id;
4869         uint16_t loop_id;
4870         uint16_t wd3_lo;
4871         int res = 0;
4872         unsigned long flags;
4873
4874         lockdep_assert_held(&ha->hardware_lock);
4875
4876         wwn = wwn_to_u64(iocb->u.isp24.port_name);
4877
4878         port_id.b.domain = iocb->u.isp24.port_id[2];
4879         port_id.b.area   = iocb->u.isp24.port_id[1];
4880         port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4881         port_id.b.rsvd_1 = 0;
4882
4883         loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4884
4885         ql_dbg(ql_dbg_disc, vha, 0xf026,
4886             "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4887             vha->vp_idx, iocb->u.isp24.port_id[2],
4888                 iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4889                    iocb->u.isp24.status_subcode, loop_id,
4890                 iocb->u.isp24.port_name);
4891
4892         /* res = 1 means ack at the end of thread
4893          * res = 0 means ack async/later.
4894          */
4895         switch (iocb->u.isp24.status_subcode) {
4896         case ELS_PLOGI:
4897                 res = qlt_handle_login(vha, iocb);
4898                 break;
4899
4900         case ELS_PRLI:
4901                 if (N2N_TOPO(ha)) {
4902                         sess = qla2x00_find_fcport_by_wwpn(vha,
4903                             iocb->u.isp24.port_name, 1);
4904
4905                         if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
4906                                 ql_dbg(ql_dbg_disc, vha, 0xffff,
4907                                     "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
4908                                     __func__, __LINE__,
4909                                     iocb->u.isp24.port_name);
4910                                 qlt_send_term_imm_notif(vha, iocb, 1);
4911                                 break;
4912                         }
4913
4914                         res = qlt_handle_login(vha, iocb);
4915                         break;
4916                 }
4917
4918                 if (IS_SW_RESV_ADDR(port_id)) {
4919                         res = 1;
4920                         break;
4921                 }
4922
4923                 wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4924
4925                 if (wwn) {
4926                         spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4927                         sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
4928                                 loop_id, &conflict_sess);
4929                         spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
4930                 }
4931
4932                 if (conflict_sess) {
4933                         switch (conflict_sess->disc_state) {
4934                         case DSC_DELETED:
4935                         case DSC_DELETE_PEND:
4936                                 break;
4937                         default:
4938                                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
4939                                     "PRLI with conflicting sess %p port %8phC\n",
4940                                     conflict_sess, conflict_sess->port_name);
4941                                 conflict_sess->fw_login_state =
4942                                     DSC_LS_PORT_UNAVAIL;
4943                                 qlt_send_term_imm_notif(vha, iocb, 1);
4944                                 res = 0;
4945                                 break;
4946                         }
4947                 }
4948
4949                 if (sess != NULL) {
4950                         bool delete = false;
4951                         int sec;
4952
4953                         spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4954                         switch (sess->fw_login_state) {
4955                         case DSC_LS_PLOGI_PEND:
4956                         case DSC_LS_PLOGI_COMP:
4957                         case DSC_LS_PRLI_COMP:
4958                                 break;
4959                         default:
4960                                 delete = true;
4961                                 break;
4962                         }
4963
4964                         switch (sess->disc_state) {
4965                         case DSC_UPD_FCPORT:
4966                                 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4967                                     flags);
4968
4969                                 sec = jiffies_to_msecs(jiffies -
4970                                     sess->jiffies_at_registration)/1000;
4971                                 if (sess->sec_since_registration < sec && sec &&
4972                                     !(sec % 5)) {
4973                                         sess->sec_since_registration = sec;
4974                                         ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
4975                                             "%s %8phC : Slow Rport registration(%d Sec)\n",
4976                                             __func__, sess->port_name, sec);
4977                                 }
4978                                 qlt_send_term_imm_notif(vha, iocb, 1);
4979                                 return 0;
4980
4981                         case DSC_LOGIN_PEND:
4982                         case DSC_GPDB:
4983                         case DSC_LOGIN_COMPLETE:
4984                         case DSC_ADISC:
4985                                 delete = false;
4986                                 break;
4987                         default:
4988                                 break;
4989                         }
4990
4991                         if (delete) {
4992                                 spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
4993                                     flags);
4994                                 /*
4995                                  * Impatient initiator sent PRLI before last
4996                                  * PLOGI could finish. Will force him to re-try,
4997                                  * while last one finishes.
4998                                  */
4999                                 ql_log(ql_log_warn, sess->vha, 0xf095,
5000                                     "sess %p PRLI received, before plogi ack.\n",
5001                                     sess);
5002                                 qlt_send_term_imm_notif(vha, iocb, 1);
5003                                 res = 0;
5004                                 break;
5005                         }
5006
5007                         /*
5008                          * This shouldn't happen under normal circumstances,
5009                          * since we have deleted the old session during PLOGI
5010                          */
5011                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5012                             "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5013                             sess->loop_id, sess, iocb->u.isp24.nport_handle);
5014
5015                         sess->local = 0;
5016                         sess->loop_id = loop_id;
5017                         sess->d_id = port_id;
5018                         sess->fw_login_state = DSC_LS_PRLI_PEND;
5019
5020                         if (wd3_lo & BIT_7)
5021                                 sess->conf_compl_supported = 1;
5022
5023                         if ((wd3_lo & BIT_4) == 0)
5024                                 sess->port_type = FCT_INITIATOR;
5025                         else
5026                                 sess->port_type = FCT_TARGET;
5027
5028                         spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5029                 }
5030                 res = 1; /* send notify ack */
5031
5032                 /* Make session global (not used in fabric mode) */
5033                 if (ha->current_topology != ISP_CFG_F) {
5034                         if (sess) {
5035                                 ql_dbg(ql_dbg_disc, vha, 0x20fa,
5036                                     "%s %d %8phC post nack\n",
5037                                     __func__, __LINE__, sess->port_name);
5038                                 qla24xx_post_nack_work(vha, sess, iocb,
5039                                         SRB_NACK_PRLI);
5040                                 res = 0;
5041                         } else {
5042                                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5043                                 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5044                                 qla2xxx_wake_dpc(vha);
5045                         }
5046                 } else {
5047                         if (sess) {
5048                                 ql_dbg(ql_dbg_disc, vha, 0x20fb,
5049                                     "%s %d %8phC post nack\n",
5050                                     __func__, __LINE__, sess->port_name);
5051                                 qla24xx_post_nack_work(vha, sess, iocb,
5052                                         SRB_NACK_PRLI);
5053                                 res = 0;
5054                         }
5055                 }
5056                 break;
5057
5058         case ELS_TPRLO:
5059                 if (le16_to_cpu(iocb->u.isp24.flags) &
5060                         NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5061                         loop_id = 0xFFFF;
5062                         qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5063                         res = 1;
5064                         break;
5065                 }
5066                 fallthrough;
5067         case ELS_LOGO:
5068         case ELS_PRLO:
5069                 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5070                 sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5071                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5072
5073                 if (sess) {
5074                         sess->login_gen++;
5075                         sess->fw_login_state = DSC_LS_LOGO_PEND;
5076                         sess->logo_ack_needed = 1;
5077                         memcpy(sess->iocb, iocb, IOCB_SIZE);
5078                 }
5079
5080                 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5081
5082                 ql_dbg(ql_dbg_disc, vha, 0x20fc,
5083                     "%s: logo %llx res %d sess %p ",
5084                     __func__, wwn, res, sess);
5085                 if (res == 0) {
5086                         /*
5087                          * cmd went upper layer, look for qlt_xmit_tm_rsp()
5088                          * for LOGO_ACK & sess delete
5089                          */
5090                         BUG_ON(!sess);
5091                         res = 0;
5092                 } else {
5093                         /* cmd did not go to upper layer. */
5094                         if (sess) {
5095                                 qlt_schedule_sess_for_deletion(sess);
5096                                 res = 0;
5097                         }
5098                         /* else logo will be ack */
5099                 }
5100                 break;
5101         case ELS_PDISC:
5102         case ELS_ADISC:
5103         {
5104                 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5105
5106                 if (tgt->link_reinit_iocb_pending) {
5107                         qlt_send_notify_ack(ha->base_qpair,
5108                             &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5109                         tgt->link_reinit_iocb_pending = 0;
5110                 }
5111
5112                 sess = qla2x00_find_fcport_by_wwpn(vha,
5113                     iocb->u.isp24.port_name, 1);
5114                 if (sess) {
5115                         ql_dbg(ql_dbg_disc, vha, 0x20fd,
5116                                 "sess %p lid %d|%d DS %d LS %d\n",
5117                                 sess, sess->loop_id, loop_id,
5118                                 sess->disc_state, sess->fw_login_state);
5119                 }
5120
5121                 res = 1; /* send notify ack */
5122                 break;
5123         }
5124
5125         case ELS_FLOGI: /* should never happen */
5126         default:
5127                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5128                     "qla_target(%d): Unsupported ELS command %x "
5129                     "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5130                 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5131                 break;
5132         }
5133
5134         ql_dbg(ql_dbg_disc, vha, 0xf026,
5135             "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5136             vha->vp_idx, iocb->u.isp24.status_subcode, res);
5137
5138         return res;
5139 }
5140
5141 /*
5142  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5143  */
5144 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5145         struct imm_ntfy_from_isp *iocb)
5146 {
5147         struct qla_hw_data *ha = vha->hw;
5148         uint32_t add_flags = 0;
5149         int send_notify_ack = 1;
5150         uint16_t status;
5151
5152         lockdep_assert_held(&ha->hardware_lock);
5153
5154         status = le16_to_cpu(iocb->u.isp2x.status);
5155         switch (status) {
5156         case IMM_NTFY_LIP_RESET:
5157         {
5158                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5159                     "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5160                     vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5161                     iocb->u.isp24.status_subcode);
5162
5163                 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5164                         send_notify_ack = 0;
5165                 break;
5166         }
5167
5168         case IMM_NTFY_LIP_LINK_REINIT:
5169         {
5170                 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5171
5172                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5173                     "qla_target(%d): LINK REINIT (loop %#x, "
5174                     "subcode %x)\n", vha->vp_idx,
5175                     le16_to_cpu(iocb->u.isp24.nport_handle),
5176                     iocb->u.isp24.status_subcode);
5177                 if (tgt->link_reinit_iocb_pending) {
5178                         qlt_send_notify_ack(ha->base_qpair,
5179                             &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5180                 }
5181                 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5182                 tgt->link_reinit_iocb_pending = 1;
5183                 /*
5184                  * QLogic requires to wait after LINK REINIT for possible
5185                  * PDISC or ADISC ELS commands
5186                  */
5187                 send_notify_ack = 0;
5188                 break;
5189         }
5190
5191         case IMM_NTFY_PORT_LOGOUT:
5192                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5193                     "qla_target(%d): Port logout (loop "
5194                     "%#x, subcode %x)\n", vha->vp_idx,
5195                     le16_to_cpu(iocb->u.isp24.nport_handle),
5196                     iocb->u.isp24.status_subcode);
5197
5198                 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5199                         send_notify_ack = 0;
5200                 /* The sessions will be cleared in the callback, if needed */
5201                 break;
5202
5203         case IMM_NTFY_GLBL_TPRLO:
5204                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5205                     "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5206                 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5207                         send_notify_ack = 0;
5208                 /* The sessions will be cleared in the callback, if needed */
5209                 break;
5210
5211         case IMM_NTFY_PORT_CONFIG:
5212                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5213                     "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5214                     status);
5215                 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5216                         send_notify_ack = 0;
5217                 /* The sessions will be cleared in the callback, if needed */
5218                 break;
5219
5220         case IMM_NTFY_GLBL_LOGO:
5221                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5222                     "qla_target(%d): Link failure detected\n",
5223                     vha->vp_idx);
5224                 /* I_T nexus loss */
5225                 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5226                         send_notify_ack = 0;
5227                 break;
5228
5229         case IMM_NTFY_IOCB_OVERFLOW:
5230                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5231                     "qla_target(%d): Cannot provide requested "
5232                     "capability (IOCB overflowed the immediate notify "
5233                     "resource count)\n", vha->vp_idx);
5234                 break;
5235
5236         case IMM_NTFY_ABORT_TASK:
5237                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5238                     "qla_target(%d): Abort Task (S %08x I %#x -> "
5239                     "L %#x)\n", vha->vp_idx,
5240                     le16_to_cpu(iocb->u.isp2x.seq_id),
5241                     GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5242                     le16_to_cpu(iocb->u.isp2x.lun));
5243                 if (qlt_abort_task(vha, iocb) == 0)
5244                         send_notify_ack = 0;
5245                 break;
5246
5247         case IMM_NTFY_RESOURCE:
5248                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5249                     "qla_target(%d): Out of resources, host %ld\n",
5250                     vha->vp_idx, vha->host_no);
5251                 break;
5252
5253         case IMM_NTFY_MSG_RX:
5254                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5255                     "qla_target(%d): Immediate notify task %x\n",
5256                     vha->vp_idx, iocb->u.isp2x.task_flags);
5257                 break;
5258
5259         case IMM_NTFY_ELS:
5260                 if (qlt_24xx_handle_els(vha, iocb) == 0)
5261                         send_notify_ack = 0;
5262                 break;
5263         default:
5264                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5265                     "qla_target(%d): Received unknown immediate "
5266                     "notify status %x\n", vha->vp_idx, status);
5267                 break;
5268         }
5269
5270         if (send_notify_ack)
5271                 qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5272                     0, 0);
5273 }
5274
5275 /*
5276  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5277  * This function sends busy to ISP 2xxx or 24xx.
5278  */
5279 static int __qlt_send_busy(struct qla_qpair *qpair,
5280         struct atio_from_isp *atio, uint16_t status)
5281 {
5282         struct scsi_qla_host *vha = qpair->vha;
5283         struct ctio7_to_24xx *ctio24;
5284         struct qla_hw_data *ha = vha->hw;
5285         request_t *pkt;
5286         struct fc_port *sess = NULL;
5287         unsigned long flags;
5288         u16 temp;
5289         port_id_t id;
5290
5291         id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5292
5293         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5294         sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5295         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5296         if (!sess) {
5297                 qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5298                 return 0;
5299         }
5300         /* Sending marker isn't necessary, since we called from ISR */
5301
5302         pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5303         if (!pkt) {
5304                 ql_dbg(ql_dbg_io, vha, 0x3063,
5305                     "qla_target(%d): %s failed: unable to allocate "
5306                     "request packet", vha->vp_idx, __func__);
5307                 return -ENOMEM;
5308         }
5309
5310         qpair->tgt_counters.num_q_full_sent++;
5311         pkt->entry_count = 1;
5312         pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5313
5314         ctio24 = (struct ctio7_to_24xx *)pkt;
5315         ctio24->entry_type = CTIO_TYPE7;
5316         ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5317         ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5318         ctio24->vp_index = vha->vp_idx;
5319         ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5320         ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5321         temp = (atio->u.isp24.attr << 9) |
5322                 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5323                 CTIO7_FLAGS_DONT_RET_CTIO;
5324         ctio24->u.status1.flags = cpu_to_le16(temp);
5325         /*
5326          * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5327          * if the explicit conformation is used.
5328          */
5329         ctio24->u.status1.ox_id =
5330                 cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5331         ctio24->u.status1.scsi_status = cpu_to_le16(status);
5332
5333         ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5334
5335         if (ctio24->u.status1.residual != 0)
5336                 ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5337
5338         /* Memory Barrier */
5339         wmb();
5340         if (qpair->reqq_start_iocbs)
5341                 qpair->reqq_start_iocbs(qpair);
5342         else
5343                 qla2x00_start_iocbs(vha, qpair->req);
5344         return 0;
5345 }
5346
5347 /*
5348  * This routine is used to allocate a command for either a QFull condition
5349  * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5350  * out previously.
5351  */
5352 static void
5353 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5354         struct atio_from_isp *atio, uint16_t status, int qfull)
5355 {
5356         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5357         struct qla_hw_data *ha = vha->hw;
5358         struct fc_port *sess;
5359         struct qla_tgt_cmd *cmd;
5360         unsigned long flags;
5361
5362         if (unlikely(tgt->tgt_stop)) {
5363                 ql_dbg(ql_dbg_io, vha, 0x300a,
5364                         "New command while device %p is shutting down\n", tgt);
5365                 return;
5366         }
5367
5368         if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5369                 vha->hw->tgt.num_qfull_cmds_dropped++;
5370                 if (vha->hw->tgt.num_qfull_cmds_dropped >
5371                         vha->qla_stats.stat_max_qfull_cmds_dropped)
5372                         vha->qla_stats.stat_max_qfull_cmds_dropped =
5373                                 vha->hw->tgt.num_qfull_cmds_dropped;
5374
5375                 ql_dbg(ql_dbg_io, vha, 0x3068,
5376                         "qla_target(%d): %s: QFull CMD dropped[%d]\n",
5377                         vha->vp_idx, __func__,
5378                         vha->hw->tgt.num_qfull_cmds_dropped);
5379
5380                 qlt_chk_exch_leak_thresh_hold(vha);
5381                 return;
5382         }
5383
5384         sess = ha->tgt.tgt_ops->find_sess_by_s_id
5385                 (vha, atio->u.isp24.fcp_hdr.s_id);
5386         if (!sess)
5387                 return;
5388
5389         cmd = ha->tgt.tgt_ops->get_cmd(sess);
5390         if (!cmd) {
5391                 ql_dbg(ql_dbg_io, vha, 0x3009,
5392                         "qla_target(%d): %s: Allocation of cmd failed\n",
5393                         vha->vp_idx, __func__);
5394
5395                 vha->hw->tgt.num_qfull_cmds_dropped++;
5396                 if (vha->hw->tgt.num_qfull_cmds_dropped >
5397                         vha->qla_stats.stat_max_qfull_cmds_dropped)
5398                         vha->qla_stats.stat_max_qfull_cmds_dropped =
5399                                 vha->hw->tgt.num_qfull_cmds_dropped;
5400
5401                 qlt_chk_exch_leak_thresh_hold(vha);
5402                 return;
5403         }
5404
5405         qlt_incr_num_pend_cmds(vha);
5406         INIT_LIST_HEAD(&cmd->cmd_list);
5407         memcpy(&cmd->atio, atio, sizeof(*atio));
5408
5409         cmd->tgt = vha->vha_tgt.qla_tgt;
5410         cmd->vha = vha;
5411         cmd->reset_count = ha->base_qpair->chip_reset;
5412         cmd->q_full = 1;
5413         cmd->qpair = ha->base_qpair;
5414
5415         if (qfull) {
5416                 cmd->q_full = 1;
5417                 /* NOTE: borrowing the state field to carry the status */
5418                 cmd->state = status;
5419         } else
5420                 cmd->term_exchg = 1;
5421
5422         spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5423         list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5424
5425         vha->hw->tgt.num_qfull_cmds_alloc++;
5426         if (vha->hw->tgt.num_qfull_cmds_alloc >
5427                 vha->qla_stats.stat_max_qfull_cmds_alloc)
5428                 vha->qla_stats.stat_max_qfull_cmds_alloc =
5429                         vha->hw->tgt.num_qfull_cmds_alloc;
5430         spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5431 }
5432
5433 int
5434 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5435 {
5436         struct scsi_qla_host *vha = qpair->vha;
5437         struct qla_hw_data *ha = vha->hw;
5438         unsigned long flags;
5439         struct qla_tgt_cmd *cmd, *tcmd;
5440         struct list_head free_list, q_full_list;
5441         int rc = 0;
5442
5443         if (list_empty(&ha->tgt.q_full_list))
5444                 return 0;
5445
5446         INIT_LIST_HEAD(&free_list);
5447         INIT_LIST_HEAD(&q_full_list);
5448
5449         spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5450         if (list_empty(&ha->tgt.q_full_list)) {
5451                 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5452                 return 0;
5453         }
5454
5455         list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5456         spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5457
5458         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5459         list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5460                 if (cmd->q_full)
5461                         /* cmd->state is a borrowed field to hold status */
5462                         rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5463                 else if (cmd->term_exchg)
5464                         rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5465
5466                 if (rc == -ENOMEM)
5467                         break;
5468
5469                 if (cmd->q_full)
5470                         ql_dbg(ql_dbg_io, vha, 0x3006,
5471                             "%s: busy sent for ox_id[%04x]\n", __func__,
5472                             be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5473                 else if (cmd->term_exchg)
5474                         ql_dbg(ql_dbg_io, vha, 0x3007,
5475                             "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5476                             be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5477                 else
5478                         ql_dbg(ql_dbg_io, vha, 0x3008,
5479                             "%s: Unexpected cmd in QFull list %p\n", __func__,
5480                             cmd);
5481
5482                 list_del(&cmd->cmd_list);
5483                 list_add_tail(&cmd->cmd_list, &free_list);
5484
5485                 /* piggy back on hardware_lock for protection */
5486                 vha->hw->tgt.num_qfull_cmds_alloc--;
5487         }
5488         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5489
5490         cmd = NULL;
5491
5492         list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5493                 list_del(&cmd->cmd_list);
5494                 /* This cmd was never sent to TCM.  There is no need
5495                  * to schedule free or call free_cmd
5496                  */
5497                 qlt_free_cmd(cmd);
5498         }
5499
5500         if (!list_empty(&q_full_list)) {
5501                 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5502                 list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5503                 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5504         }
5505
5506         return rc;
5507 }
5508
5509 static void
5510 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5511     uint16_t status)
5512 {
5513         int rc = 0;
5514         struct scsi_qla_host *vha = qpair->vha;
5515
5516         rc = __qlt_send_busy(qpair, atio, status);
5517         if (rc == -ENOMEM)
5518                 qlt_alloc_qfull_cmd(vha, atio, status, 1);
5519 }
5520
5521 static int
5522 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5523         struct atio_from_isp *atio, uint8_t ha_locked)
5524 {
5525         struct qla_hw_data *ha = vha->hw;
5526         unsigned long flags;
5527
5528         if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5529                 return 0;
5530
5531         if (!ha_locked)
5532                 spin_lock_irqsave(&ha->hardware_lock, flags);
5533         qlt_send_busy(qpair, atio, qla_sam_status);
5534         if (!ha_locked)
5535                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5536
5537         return 1;
5538 }
5539
5540 /* ha->hardware_lock supposed to be held on entry */
5541 /* called via callback from qla2xxx */
5542 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5543         struct atio_from_isp *atio, uint8_t ha_locked)
5544 {
5545         struct qla_hw_data *ha = vha->hw;
5546         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5547         int rc;
5548         unsigned long flags = 0;
5549
5550         if (unlikely(tgt == NULL)) {
5551                 ql_dbg(ql_dbg_tgt, vha, 0x3064,
5552                     "ATIO pkt, but no tgt (ha %p)", ha);
5553                 return;
5554         }
5555         /*
5556          * In tgt_stop mode we also should allow all requests to pass.
5557          * Otherwise, some commands can stuck.
5558          */
5559
5560         tgt->atio_irq_cmd_count++;
5561
5562         switch (atio->u.raw.entry_type) {
5563         case ATIO_TYPE7:
5564                 if (unlikely(atio->u.isp24.exchange_addr ==
5565                              cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5566                         ql_dbg(ql_dbg_io, vha, 0x3065,
5567                             "qla_target(%d): ATIO_TYPE7 "
5568                             "received with UNKNOWN exchange address, "
5569                             "sending QUEUE_FULL\n", vha->vp_idx);
5570                         if (!ha_locked)
5571                                 spin_lock_irqsave(&ha->hardware_lock, flags);
5572                         qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5573                         if (!ha_locked)
5574                                 spin_unlock_irqrestore(&ha->hardware_lock,
5575                                     flags);
5576                         break;
5577                 }
5578
5579                 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5580                         rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5581                             atio, ha_locked);
5582                         if (rc != 0) {
5583                                 tgt->atio_irq_cmd_count--;
5584                                 return;
5585                         }
5586                         rc = qlt_handle_cmd_for_atio(vha, atio);
5587                 } else {
5588                         rc = qlt_handle_task_mgmt(vha, atio);
5589                 }
5590                 if (unlikely(rc != 0)) {
5591                         if (!ha_locked)
5592                                 spin_lock_irqsave(&ha->hardware_lock, flags);
5593                         switch (rc) {
5594                         case -ENODEV:
5595                                 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5596                                     "qla_target: Unable to send command to target\n");
5597                                 break;
5598                         case -EBADF:
5599                                 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5600                                     "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5601                                 qlt_send_term_exchange(ha->base_qpair, NULL,
5602                                     atio, 1, 0);
5603                                 break;
5604                         case -EBUSY:
5605                                 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5606                                     "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5607                                     vha->vp_idx);
5608                                 qlt_send_busy(ha->base_qpair, atio,
5609                                     tc_sam_status);
5610                                 break;
5611                         default:
5612                                 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5613                                     "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5614                                     vha->vp_idx);
5615                                 qlt_send_busy(ha->base_qpair, atio,
5616                                     qla_sam_status);
5617                                 break;
5618                         }
5619                         if (!ha_locked)
5620                                 spin_unlock_irqrestore(&ha->hardware_lock,
5621                                     flags);
5622                 }
5623                 break;
5624
5625         case IMMED_NOTIFY_TYPE:
5626         {
5627                 if (unlikely(atio->u.isp2x.entry_status != 0)) {
5628                         ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5629                             "qla_target(%d): Received ATIO packet %x "
5630                             "with error status %x\n", vha->vp_idx,
5631                             atio->u.raw.entry_type,
5632                             atio->u.isp2x.entry_status);
5633                         break;
5634                 }
5635                 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5636
5637                 if (!ha_locked)
5638                         spin_lock_irqsave(&ha->hardware_lock, flags);
5639                 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5640                 if (!ha_locked)
5641                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
5642                 break;
5643         }
5644
5645         default:
5646                 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5647                     "qla_target(%d): Received unknown ATIO atio "
5648                     "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5649                 break;
5650         }
5651
5652         tgt->atio_irq_cmd_count--;
5653 }
5654
5655 /*
5656  * qpair lock is assume to be held
5657  * rc = 0 : send terminate & abts respond
5658  * rc != 0: do not send term & abts respond
5659  */
5660 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5661     struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5662 {
5663         struct qla_hw_data *ha = vha->hw;
5664         int rc = 0;
5665
5666         /*
5667          * Detect unresolved exchange. If the same ABTS is unable
5668          * to terminate an existing command and the same ABTS loops
5669          * between FW & Driver, then force FW dump. Under 1 jiff,
5670          * we should see multiple loops.
5671          */
5672         if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5673             qpair->retry_term_jiff == jiffies) {
5674                 /* found existing exchange */
5675                 qpair->retry_term_cnt++;
5676                 if (qpair->retry_term_cnt >= 5) {
5677                         rc = -EIO;
5678                         qpair->retry_term_cnt = 0;
5679                         ql_log(ql_log_warn, vha, 0xffff,
5680                             "Unable to send ABTS Respond. Dumping firmware.\n");
5681                         ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5682                             vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5683
5684                         if (qpair == ha->base_qpair)
5685                                 ha->isp_ops->fw_dump(vha);
5686                         else
5687                                 qla2xxx_dump_fw(vha);
5688
5689                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5690                         qla2xxx_wake_dpc(vha);
5691                 }
5692         } else if (qpair->retry_term_jiff != jiffies) {
5693                 qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5694                 qpair->retry_term_cnt = 0;
5695                 qpair->retry_term_jiff = jiffies;
5696         }
5697
5698         return rc;
5699 }
5700
5701
5702 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5703         struct rsp_que *rsp, response_t *pkt)
5704 {
5705         struct abts_resp_from_24xx_fw *entry =
5706                 (struct abts_resp_from_24xx_fw *)pkt;
5707         u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5708         struct qla_tgt_mgmt_cmd *mcmd;
5709         struct qla_hw_data *ha = vha->hw;
5710
5711         mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5712         if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5713                 ql_dbg(ql_dbg_async, vha, 0xe064,
5714                     "qla_target(%d): ABTS Comp without mcmd\n",
5715                     vha->vp_idx);
5716                 return;
5717         }
5718
5719         if (mcmd)
5720                 vha  = mcmd->vha;
5721         vha->vha_tgt.qla_tgt->abts_resp_expected--;
5722
5723         ql_dbg(ql_dbg_tgt, vha, 0xe038,
5724             "ABTS_RESP_24XX: compl_status %x\n",
5725             entry->compl_status);
5726
5727         if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5728                 if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5729                     le32_to_cpu(entry->error_subcode2) == 0) {
5730                         if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5731                                 ha->tgt.tgt_ops->free_mcmd(mcmd);
5732                                 return;
5733                         }
5734                         qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5735                             pkt, mcmd);
5736                 } else {
5737                         ql_dbg(ql_dbg_tgt, vha, 0xe063,
5738                             "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5739                             vha->vp_idx, entry->compl_status,
5740                             entry->error_subcode1,
5741                             entry->error_subcode2);
5742                         ha->tgt.tgt_ops->free_mcmd(mcmd);
5743                 }
5744         } else if (mcmd) {
5745                 ha->tgt.tgt_ops->free_mcmd(mcmd);
5746         }
5747 }
5748
5749 /* ha->hardware_lock supposed to be held on entry */
5750 /* called via callback from qla2xxx */
5751 static void qlt_response_pkt(struct scsi_qla_host *vha,
5752         struct rsp_que *rsp, response_t *pkt)
5753 {
5754         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5755
5756         if (unlikely(tgt == NULL)) {
5757                 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5758                     "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5759                     vha->vp_idx, pkt->entry_type, vha->hw);
5760                 return;
5761         }
5762
5763         /*
5764          * In tgt_stop mode we also should allow all requests to pass.
5765          * Otherwise, some commands can stuck.
5766          */
5767
5768         switch (pkt->entry_type) {
5769         case CTIO_CRC2:
5770         case CTIO_TYPE7:
5771         {
5772                 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5773
5774                 qlt_do_ctio_completion(vha, rsp, entry->handle,
5775                     le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5776                     entry);
5777                 break;
5778         }
5779
5780         case ACCEPT_TGT_IO_TYPE:
5781         {
5782                 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5783                 int rc;
5784
5785                 if (atio->u.isp2x.status !=
5786                     cpu_to_le16(ATIO_CDB_VALID)) {
5787                         ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5788                             "qla_target(%d): ATIO with error "
5789                             "status %x received\n", vha->vp_idx,
5790                             le16_to_cpu(atio->u.isp2x.status));
5791                         break;
5792                 }
5793
5794                 rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5795                 if (rc != 0)
5796                         return;
5797
5798                 rc = qlt_handle_cmd_for_atio(vha, atio);
5799                 if (unlikely(rc != 0)) {
5800                         switch (rc) {
5801                         case -ENODEV:
5802                                 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5803                                     "qla_target: Unable to send command to target\n");
5804                                 break;
5805                         case -EBADF:
5806                                 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5807                                     "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5808                                 qlt_send_term_exchange(rsp->qpair, NULL,
5809                                     atio, 1, 0);
5810                                 break;
5811                         case -EBUSY:
5812                                 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5813                                     "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5814                                     vha->vp_idx);
5815                                 qlt_send_busy(rsp->qpair, atio,
5816                                     tc_sam_status);
5817                                 break;
5818                         default:
5819                                 ql_dbg(ql_dbg_tgt, vha, 0xe060,
5820                                     "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5821                                     vha->vp_idx);
5822                                 qlt_send_busy(rsp->qpair, atio,
5823                                     qla_sam_status);
5824                                 break;
5825                         }
5826                 }
5827         }
5828         break;
5829
5830         case CONTINUE_TGT_IO_TYPE:
5831         {
5832                 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5833
5834                 qlt_do_ctio_completion(vha, rsp, entry->handle,
5835                     le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5836                     entry);
5837                 break;
5838         }
5839
5840         case CTIO_A64_TYPE:
5841         {
5842                 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5843
5844                 qlt_do_ctio_completion(vha, rsp, entry->handle,
5845                     le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5846                     entry);
5847                 break;
5848         }
5849
5850         case IMMED_NOTIFY_TYPE:
5851                 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5852                 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5853                 break;
5854
5855         case NOTIFY_ACK_TYPE:
5856                 if (tgt->notify_ack_expected > 0) {
5857                         struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5858
5859                         ql_dbg(ql_dbg_tgt, vha, 0xe036,
5860                             "NOTIFY_ACK seq %08x status %x\n",
5861                             le16_to_cpu(entry->u.isp2x.seq_id),
5862                             le16_to_cpu(entry->u.isp2x.status));
5863                         tgt->notify_ack_expected--;
5864                         if (entry->u.isp2x.status !=
5865                             cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5866                                 ql_dbg(ql_dbg_tgt, vha, 0xe061,
5867                                     "qla_target(%d): NOTIFY_ACK "
5868                                     "failed %x\n", vha->vp_idx,
5869                                     le16_to_cpu(entry->u.isp2x.status));
5870                         }
5871                 } else {
5872                         ql_dbg(ql_dbg_tgt, vha, 0xe062,
5873                             "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5874                             vha->vp_idx);
5875                 }
5876                 break;
5877
5878         case ABTS_RECV_24XX:
5879                 ql_dbg(ql_dbg_tgt, vha, 0xe037,
5880                     "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5881                 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5882                 break;
5883
5884         case ABTS_RESP_24XX:
5885                 if (tgt->abts_resp_expected > 0) {
5886                         qlt_handle_abts_completion(vha, rsp, pkt);
5887                 } else {
5888                         ql_dbg(ql_dbg_tgt, vha, 0xe064,
5889                             "qla_target(%d): Unexpected ABTS_RESP_24XX "
5890                             "received\n", vha->vp_idx);
5891                 }
5892                 break;
5893
5894         default:
5895                 ql_dbg(ql_dbg_tgt, vha, 0xe065,
5896                     "qla_target(%d): Received unknown response pkt "
5897                     "type %x\n", vha->vp_idx, pkt->entry_type);
5898                 break;
5899         }
5900
5901 }
5902
5903 /*
5904  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5905  */
5906 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
5907         uint16_t *mailbox)
5908 {
5909         struct qla_hw_data *ha = vha->hw;
5910         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5911         int login_code;
5912
5913         if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5914                 return;
5915
5916         if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
5917             IS_QLA2100(ha))
5918                 return;
5919         /*
5920          * In tgt_stop mode we also should allow all requests to pass.
5921          * Otherwise, some commands can stuck.
5922          */
5923
5924
5925         switch (code) {
5926         case MBA_RESET:                 /* Reset */
5927         case MBA_SYSTEM_ERR:            /* System Error */
5928         case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
5929         case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
5930                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
5931                     "qla_target(%d): System error async event %#x "
5932                     "occurred", vha->vp_idx, code);
5933                 break;
5934         case MBA_WAKEUP_THRES:          /* Request Queue Wake-up. */
5935                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5936                 break;
5937
5938         case MBA_LOOP_UP:
5939         {
5940                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5941                     "qla_target(%d): Async LOOP_UP occurred "
5942                     "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
5943                     mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5944                 if (tgt->link_reinit_iocb_pending) {
5945                         qlt_send_notify_ack(ha->base_qpair,
5946                             &tgt->link_reinit_iocb,
5947                             0, 0, 0, 0, 0, 0);
5948                         tgt->link_reinit_iocb_pending = 0;
5949                 }
5950                 break;
5951         }
5952
5953         case MBA_LIP_OCCURRED:
5954         case MBA_LOOP_DOWN:
5955         case MBA_LIP_RESET:
5956         case MBA_RSCN_UPDATE:
5957                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5958                     "qla_target(%d): Async event %#x occurred "
5959                     "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5960                     mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5961                 break;
5962
5963         case MBA_REJECTED_FCP_CMD:
5964                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
5965                     "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
5966                     vha->vp_idx,
5967                     mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5968
5969                 if (mailbox[3] == 1) {
5970                         /* exchange starvation. */
5971                         vha->hw->exch_starvation++;
5972                         if (vha->hw->exch_starvation > 5) {
5973                                 ql_log(ql_log_warn, vha, 0xd03a,
5974                                     "Exchange starvation-. Resetting RISC\n");
5975
5976                                 vha->hw->exch_starvation = 0;
5977                                 if (IS_P3P_TYPE(vha->hw))
5978                                         set_bit(FCOE_CTX_RESET_NEEDED,
5979                                             &vha->dpc_flags);
5980                                 else
5981                                         set_bit(ISP_ABORT_NEEDED,
5982                                             &vha->dpc_flags);
5983                                 qla2xxx_wake_dpc(vha);
5984                         }
5985                 }
5986                 break;
5987
5988         case MBA_PORT_UPDATE:
5989                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
5990                     "qla_target(%d): Port update async event %#x "
5991                     "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5992                     "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
5993                     mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
5994
5995                 login_code = mailbox[2];
5996                 if (login_code == 0x4) {
5997                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
5998                             "Async MB 2: Got PLOGI Complete\n");
5999                         vha->hw->exch_starvation = 0;
6000                 } else if (login_code == 0x7)
6001                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6002                             "Async MB 2: Port Logged Out\n");
6003                 break;
6004         default:
6005                 break;
6006         }
6007
6008 }
6009
6010 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6011         uint16_t loop_id)
6012 {
6013         fc_port_t *fcport, *tfcp, *del;
6014         int rc;
6015         unsigned long flags;
6016         u8 newfcport = 0;
6017
6018         fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6019         if (!fcport) {
6020                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6021                     "qla_target(%d): Allocation of tmp FC port failed",
6022                     vha->vp_idx);
6023                 return NULL;
6024         }
6025
6026         fcport->loop_id = loop_id;
6027
6028         rc = qla24xx_gpdb_wait(vha, fcport, 0);
6029         if (rc != QLA_SUCCESS) {
6030                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6031                     "qla_target(%d): Failed to retrieve fcport "
6032                     "information -- get_port_database() returned %x "
6033                     "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6034                 kfree(fcport);
6035                 return NULL;
6036         }
6037
6038         del = NULL;
6039         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6040         tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6041
6042         if (tfcp) {
6043                 tfcp->d_id = fcport->d_id;
6044                 tfcp->port_type = fcport->port_type;
6045                 tfcp->supported_classes = fcport->supported_classes;
6046                 tfcp->flags |= fcport->flags;
6047                 tfcp->scan_state = QLA_FCPORT_FOUND;
6048
6049                 del = fcport;
6050                 fcport = tfcp;
6051         } else {
6052                 if (vha->hw->current_topology == ISP_CFG_F)
6053                         fcport->flags |= FCF_FABRIC_DEVICE;
6054
6055                 list_add_tail(&fcport->list, &vha->vp_fcports);
6056                 if (!IS_SW_RESV_ADDR(fcport->d_id))
6057                    vha->fcport_count++;
6058                 fcport->login_gen++;
6059                 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6060                 fcport->login_succ = 1;
6061                 newfcport = 1;
6062         }
6063
6064         fcport->deleted = 0;
6065         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6066
6067         switch (vha->host->active_mode) {
6068         case MODE_INITIATOR:
6069         case MODE_DUAL:
6070                 if (newfcport) {
6071                         if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6072                                 qla24xx_sched_upd_fcport(fcport);
6073                         } else {
6074                                 ql_dbg(ql_dbg_disc, vha, 0x20ff,
6075                                    "%s %d %8phC post gpsc fcp_cnt %d\n",
6076                                    __func__, __LINE__, fcport->port_name, vha->fcport_count);
6077                                 qla24xx_post_gpsc_work(vha, fcport);
6078                         }
6079                 }
6080                 break;
6081
6082         case MODE_TARGET:
6083         default:
6084                 break;
6085         }
6086         if (del)
6087                 qla2x00_free_fcport(del);
6088
6089         return fcport;
6090 }
6091
6092 /* Must be called under tgt_mutex */
6093 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6094                                            be_id_t s_id)
6095 {
6096         struct fc_port *sess = NULL;
6097         fc_port_t *fcport = NULL;
6098         int rc, global_resets;
6099         uint16_t loop_id = 0;
6100
6101         if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6102                 /*
6103                  * This is Domain Controller, so it should be
6104                  * OK to drop SCSI commands from it.
6105                  */
6106                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6107                     "Unable to find initiator with S_ID %x:%x:%x",
6108                     s_id.domain, s_id.area, s_id.al_pa);
6109                 return NULL;
6110         }
6111
6112         mutex_lock(&vha->vha_tgt.tgt_mutex);
6113
6114 retry:
6115         global_resets =
6116             atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6117
6118         rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6119         if (rc != 0) {
6120                 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6121
6122                 ql_log(ql_log_info, vha, 0xf071,
6123                     "qla_target(%d): Unable to find "
6124                     "initiator with S_ID %x:%x:%x",
6125                     vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6126
6127                 if (rc == -ENOENT) {
6128                         qlt_port_logo_t logo;
6129
6130                         logo.id = be_to_port_id(s_id);
6131                         logo.cmd_count = 1;
6132                         qlt_send_first_logo(vha, &logo);
6133                 }
6134
6135                 return NULL;
6136         }
6137
6138         fcport = qlt_get_port_database(vha, loop_id);
6139         if (!fcport) {
6140                 mutex_unlock(&vha->vha_tgt.tgt_mutex);
6141                 return NULL;
6142         }
6143
6144         if (global_resets !=
6145             atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6146                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6147                     "qla_target(%d): global reset during session discovery "
6148                     "(counter was %d, new %d), retrying", vha->vp_idx,
6149                     global_resets,
6150                     atomic_read(&vha->vha_tgt.
6151                         qla_tgt->tgt_global_resets_count));
6152                 goto retry;
6153         }
6154
6155         sess = qlt_create_sess(vha, fcport, true);
6156
6157         mutex_unlock(&vha->vha_tgt.tgt_mutex);
6158
6159         return sess;
6160 }
6161
6162 static void qlt_abort_work(struct qla_tgt *tgt,
6163         struct qla_tgt_sess_work_param *prm)
6164 {
6165         struct scsi_qla_host *vha = tgt->vha;
6166         struct qla_hw_data *ha = vha->hw;
6167         struct fc_port *sess = NULL;
6168         unsigned long flags = 0, flags2 = 0;
6169         be_id_t s_id;
6170         int rc;
6171
6172         spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6173
6174         if (tgt->tgt_stop)
6175                 goto out_term2;
6176
6177         s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6178
6179         sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6180         if (!sess) {
6181                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6182
6183                 sess = qlt_make_local_sess(vha, s_id);
6184                 /* sess has got an extra creation ref */
6185
6186                 spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6187                 if (!sess)
6188                         goto out_term2;
6189         } else {
6190                 if (sess->deleted) {
6191                         sess = NULL;
6192                         goto out_term2;
6193                 }
6194
6195                 if (!kref_get_unless_zero(&sess->sess_kref)) {
6196                         ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6197                             "%s: kref_get fail %8phC \n",
6198                              __func__, sess->port_name);
6199                         sess = NULL;
6200                         goto out_term2;
6201                 }
6202         }
6203
6204         rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6205         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6206
6207         ha->tgt.tgt_ops->put_sess(sess);
6208
6209         if (rc != 0)
6210                 goto out_term;
6211         return;
6212
6213 out_term2:
6214         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6215
6216 out_term:
6217         spin_lock_irqsave(&ha->hardware_lock, flags);
6218         qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6219             FCP_TMF_REJECTED, false);
6220         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6221 }
6222
6223 static void qlt_tmr_work(struct qla_tgt *tgt,
6224         struct qla_tgt_sess_work_param *prm)
6225 {
6226         struct atio_from_isp *a = &prm->tm_iocb2;
6227         struct scsi_qla_host *vha = tgt->vha;
6228         struct qla_hw_data *ha = vha->hw;
6229         struct fc_port *sess;
6230         unsigned long flags;
6231         be_id_t s_id;
6232         int rc;
6233         u64 unpacked_lun;
6234         int fn;
6235         void *iocb;
6236
6237         spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6238
6239         if (tgt->tgt_stop)
6240                 goto out_term2;
6241
6242         s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6243         sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6244         if (!sess) {
6245                 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6246
6247                 sess = qlt_make_local_sess(vha, s_id);
6248                 /* sess has got an extra creation ref */
6249
6250                 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6251                 if (!sess)
6252                         goto out_term2;
6253         } else {
6254                 if (sess->deleted) {
6255                         goto out_term2;
6256                 }
6257
6258                 if (!kref_get_unless_zero(&sess->sess_kref)) {
6259                         ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6260                             "%s: kref_get fail %8phC\n",
6261                              __func__, sess->port_name);
6262                         goto out_term2;
6263                 }
6264         }
6265
6266         iocb = a;
6267         fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6268         unpacked_lun =
6269             scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6270
6271         rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6272         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6273
6274         ha->tgt.tgt_ops->put_sess(sess);
6275
6276         if (rc != 0)
6277                 goto out_term;
6278         return;
6279
6280 out_term2:
6281         spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6282 out_term:
6283         qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6284 }
6285
6286 static void qlt_sess_work_fn(struct work_struct *work)
6287 {
6288         struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6289         struct scsi_qla_host *vha = tgt->vha;
6290         unsigned long flags;
6291
6292         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6293
6294         spin_lock_irqsave(&tgt->sess_work_lock, flags);
6295         while (!list_empty(&tgt->sess_works_list)) {
6296                 struct qla_tgt_sess_work_param *prm = list_entry(
6297                     tgt->sess_works_list.next, typeof(*prm),
6298                     sess_works_list_entry);
6299
6300                 /*
6301                  * This work can be scheduled on several CPUs at time, so we
6302                  * must delete the entry to eliminate double processing
6303                  */
6304                 list_del(&prm->sess_works_list_entry);
6305
6306                 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6307
6308                 switch (prm->type) {
6309                 case QLA_TGT_SESS_WORK_ABORT:
6310                         qlt_abort_work(tgt, prm);
6311                         break;
6312                 case QLA_TGT_SESS_WORK_TM:
6313                         qlt_tmr_work(tgt, prm);
6314                         break;
6315                 default:
6316                         BUG_ON(1);
6317                         break;
6318                 }
6319
6320                 spin_lock_irqsave(&tgt->sess_work_lock, flags);
6321
6322                 kfree(prm);
6323         }
6324         spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6325 }
6326
6327 /* Must be called under tgt_host_action_mutex */
6328 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6329 {
6330         struct qla_tgt *tgt;
6331         int rc, i;
6332         struct qla_qpair_hint *h;
6333
6334         if (!QLA_TGT_MODE_ENABLED())
6335                 return 0;
6336
6337         if (!IS_TGT_MODE_CAPABLE(ha)) {
6338                 ql_log(ql_log_warn, base_vha, 0xe070,
6339                     "This adapter does not support target mode.\n");
6340                 return 0;
6341         }
6342
6343         ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6344             "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6345
6346         BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6347
6348         tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6349         if (!tgt) {
6350                 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6351                     "Unable to allocate struct qla_tgt\n");
6352                 return -ENOMEM;
6353         }
6354
6355         tgt->qphints = kcalloc(ha->max_qpairs + 1,
6356                                sizeof(struct qla_qpair_hint),
6357                                GFP_KERNEL);
6358         if (!tgt->qphints) {
6359                 kfree(tgt);
6360                 ql_log(ql_log_warn, base_vha, 0x0197,
6361                     "Unable to allocate qpair hints.\n");
6362                 return -ENOMEM;
6363         }
6364
6365         if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6366                 base_vha->host->hostt->supported_mode |= MODE_TARGET;
6367
6368         rc = btree_init64(&tgt->lun_qpair_map);
6369         if (rc) {
6370                 kfree(tgt->qphints);
6371                 kfree(tgt);
6372                 ql_log(ql_log_info, base_vha, 0x0198,
6373                         "Unable to initialize lun_qpair_map btree\n");
6374                 return -EIO;
6375         }
6376         h = &tgt->qphints[0];
6377         h->qpair = ha->base_qpair;
6378         INIT_LIST_HEAD(&h->hint_elem);
6379         h->cpuid = ha->base_qpair->cpuid;
6380         list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6381
6382         for (i = 0; i < ha->max_qpairs; i++) {
6383                 unsigned long flags;
6384
6385                 struct qla_qpair *qpair = ha->queue_pair_map[i];
6386
6387                 h = &tgt->qphints[i + 1];
6388                 INIT_LIST_HEAD(&h->hint_elem);
6389                 if (qpair) {
6390                         h->qpair = qpair;
6391                         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6392                         list_add_tail(&h->hint_elem, &qpair->hints_list);
6393                         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6394                         h->cpuid = qpair->cpuid;
6395                 }
6396         }
6397
6398         tgt->ha = ha;
6399         tgt->vha = base_vha;
6400         init_waitqueue_head(&tgt->waitQ);
6401         INIT_LIST_HEAD(&tgt->del_sess_list);
6402         spin_lock_init(&tgt->sess_work_lock);
6403         INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6404         INIT_LIST_HEAD(&tgt->sess_works_list);
6405         atomic_set(&tgt->tgt_global_resets_count, 0);
6406
6407         base_vha->vha_tgt.qla_tgt = tgt;
6408
6409         ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6410                 "qla_target(%d): using 64 Bit PCI addressing",
6411                 base_vha->vp_idx);
6412         /* 3 is reserved */
6413         tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6414
6415         mutex_lock(&qla_tgt_mutex);
6416         list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6417         mutex_unlock(&qla_tgt_mutex);
6418
6419         if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6420                 ha->tgt.tgt_ops->add_target(base_vha);
6421
6422         return 0;
6423 }
6424
6425 /* Must be called under tgt_host_action_mutex */
6426 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6427 {
6428         if (!vha->vha_tgt.qla_tgt)
6429                 return 0;
6430
6431         if (vha->fc_vport) {
6432                 qlt_release(vha->vha_tgt.qla_tgt);
6433                 return 0;
6434         }
6435
6436         /* free left over qfull cmds */
6437         qlt_init_term_exchange(vha);
6438
6439         ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6440             vha->host_no, ha);
6441         qlt_release(vha->vha_tgt.qla_tgt);
6442
6443         return 0;
6444 }
6445
6446 void qlt_remove_target_resources(struct qla_hw_data *ha)
6447 {
6448         struct scsi_qla_host *node;
6449         u32 key = 0;
6450
6451         btree_for_each_safe32(&ha->tgt.host_map, key, node)
6452                 btree_remove32(&ha->tgt.host_map, key);
6453
6454         btree_destroy32(&ha->tgt.host_map);
6455 }
6456
6457 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6458         unsigned char *b)
6459 {
6460         pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6461         pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6462         put_unaligned_be64(wwpn, b);
6463         pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6464 }
6465
6466 /**
6467  * qlt_lport_register - register lport with external module
6468  *
6469  * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6470  * @phys_wwpn: physical port WWPN
6471  * @npiv_wwpn: NPIV WWPN
6472  * @npiv_wwnn: NPIV WWNN
6473  * @callback:  lport initialization callback for tcm_qla2xxx code
6474  */
6475 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6476                        u64 npiv_wwpn, u64 npiv_wwnn,
6477                        int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6478 {
6479         struct qla_tgt *tgt;
6480         struct scsi_qla_host *vha;
6481         struct qla_hw_data *ha;
6482         struct Scsi_Host *host;
6483         unsigned long flags;
6484         int rc;
6485         u8 b[WWN_SIZE];
6486
6487         mutex_lock(&qla_tgt_mutex);
6488         list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6489                 vha = tgt->vha;
6490                 ha = vha->hw;
6491
6492                 host = vha->host;
6493                 if (!host)
6494                         continue;
6495
6496                 if (!(host->hostt->supported_mode & MODE_TARGET))
6497                         continue;
6498
6499                 if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6500                         continue;
6501
6502                 spin_lock_irqsave(&ha->hardware_lock, flags);
6503                 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6504                         pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6505                             host->host_no);
6506                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6507                         continue;
6508                 }
6509                 if (tgt->tgt_stop) {
6510                         pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6511                                  host->host_no);
6512                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6513                         continue;
6514                 }
6515                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6516
6517                 if (!scsi_host_get(host)) {
6518                         ql_dbg(ql_dbg_tgt, vha, 0xe068,
6519                             "Unable to scsi_host_get() for"
6520                             " qla2xxx scsi_host\n");
6521                         continue;
6522                 }
6523                 qlt_lport_dump(vha, phys_wwpn, b);
6524
6525                 if (memcmp(vha->port_name, b, WWN_SIZE)) {
6526                         scsi_host_put(host);
6527                         continue;
6528                 }
6529                 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6530                 if (rc != 0)
6531                         scsi_host_put(host);
6532
6533                 mutex_unlock(&qla_tgt_mutex);
6534                 return rc;
6535         }
6536         mutex_unlock(&qla_tgt_mutex);
6537
6538         return -ENODEV;
6539 }
6540 EXPORT_SYMBOL(qlt_lport_register);
6541
6542 /**
6543  * qlt_lport_deregister - Degister lport
6544  *
6545  * @vha:  Registered scsi_qla_host pointer
6546  */
6547 void qlt_lport_deregister(struct scsi_qla_host *vha)
6548 {
6549         struct qla_hw_data *ha = vha->hw;
6550         struct Scsi_Host *sh = vha->host;
6551         /*
6552          * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6553          */
6554         vha->vha_tgt.target_lport_ptr = NULL;
6555         ha->tgt.tgt_ops = NULL;
6556         /*
6557          * Release the Scsi_Host reference for the underlying qla2xxx host
6558          */
6559         scsi_host_put(sh);
6560 }
6561 EXPORT_SYMBOL(qlt_lport_deregister);
6562
6563 /* Must be called under HW lock */
6564 void qlt_set_mode(struct scsi_qla_host *vha)
6565 {
6566         switch (vha->qlini_mode) {
6567         case QLA2XXX_INI_MODE_DISABLED:
6568         case QLA2XXX_INI_MODE_EXCLUSIVE:
6569                 vha->host->active_mode = MODE_TARGET;
6570                 break;
6571         case QLA2XXX_INI_MODE_ENABLED:
6572                 vha->host->active_mode = MODE_INITIATOR;
6573                 break;
6574         case QLA2XXX_INI_MODE_DUAL:
6575                 vha->host->active_mode = MODE_DUAL;
6576                 break;
6577         default:
6578                 break;
6579         }
6580 }
6581
6582 /* Must be called under HW lock */
6583 static void qlt_clear_mode(struct scsi_qla_host *vha)
6584 {
6585         switch (vha->qlini_mode) {
6586         case QLA2XXX_INI_MODE_DISABLED:
6587                 vha->host->active_mode = MODE_UNKNOWN;
6588                 break;
6589         case QLA2XXX_INI_MODE_EXCLUSIVE:
6590                 vha->host->active_mode = MODE_INITIATOR;
6591                 break;
6592         case QLA2XXX_INI_MODE_ENABLED:
6593         case QLA2XXX_INI_MODE_DUAL:
6594                 vha->host->active_mode = MODE_INITIATOR;
6595                 break;
6596         default:
6597                 break;
6598         }
6599 }
6600
6601 /*
6602  * qla_tgt_enable_vha - NO LOCK HELD
6603  *
6604  * host_reset, bring up w/ Target Mode Enabled
6605  */
6606 void
6607 qlt_enable_vha(struct scsi_qla_host *vha)
6608 {
6609         struct qla_hw_data *ha = vha->hw;
6610         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6611         unsigned long flags;
6612         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6613
6614         if (!tgt) {
6615                 ql_dbg(ql_dbg_tgt, vha, 0xe069,
6616                     "Unable to locate qla_tgt pointer from"
6617                     " struct qla_hw_data\n");
6618                 dump_stack();
6619                 return;
6620         }
6621         if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6622                 return;
6623
6624         if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6625                 ha->tgt.num_act_qpairs = ha->max_qpairs;
6626         spin_lock_irqsave(&ha->hardware_lock, flags);
6627         tgt->tgt_stopped = 0;
6628         qlt_set_mode(vha);
6629         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6630
6631         mutex_lock(&ha->optrom_mutex);
6632         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6633             "%s.\n", __func__);
6634         if (vha->vp_idx) {
6635                 qla24xx_disable_vp(vha);
6636                 qla24xx_enable_vp(vha);
6637         } else {
6638                 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6639                 qla2xxx_wake_dpc(base_vha);
6640                 WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6641                              QLA_SUCCESS);
6642         }
6643         mutex_unlock(&ha->optrom_mutex);
6644 }
6645 EXPORT_SYMBOL(qlt_enable_vha);
6646
6647 /*
6648  * qla_tgt_disable_vha - NO LOCK HELD
6649  *
6650  * Disable Target Mode and reset the adapter
6651  */
6652 static void qlt_disable_vha(struct scsi_qla_host *vha)
6653 {
6654         struct qla_hw_data *ha = vha->hw;
6655         struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6656         unsigned long flags;
6657
6658         if (!tgt) {
6659                 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6660                     "Unable to locate qla_tgt pointer from"
6661                     " struct qla_hw_data\n");
6662                 dump_stack();
6663                 return;
6664         }
6665
6666         spin_lock_irqsave(&ha->hardware_lock, flags);
6667         qlt_clear_mode(vha);
6668         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6669
6670         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6671         qla2xxx_wake_dpc(vha);
6672
6673         /*
6674          * We are expecting the offline state.
6675          * QLA_FUNCTION_FAILED means that adapter is offline.
6676          */
6677         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6678                 ql_dbg(ql_dbg_tgt, vha, 0xe081,
6679                        "adapter is offline\n");
6680 }
6681
6682 /*
6683  * Called from qla_init.c:qla24xx_vport_create() contex to setup
6684  * the target mode specific struct scsi_qla_host and struct qla_hw_data
6685  * members.
6686  */
6687 void
6688 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6689 {
6690         vha->vha_tgt.qla_tgt = NULL;
6691
6692         mutex_init(&vha->vha_tgt.tgt_mutex);
6693         mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6694
6695         qlt_clear_mode(vha);
6696
6697         /*
6698          * NOTE: Currently the value is kept the same for <24xx and
6699          * >=24xx ISPs. If it is necessary to change it,
6700          * the check should be added for specific ISPs,
6701          * assigning the value appropriately.
6702          */
6703         ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6704
6705         qlt_add_target(ha, vha);
6706 }
6707
6708 u8
6709 qlt_rff_id(struct scsi_qla_host *vha)
6710 {
6711         u8 fc4_feature = 0;
6712         /*
6713          * FC-4 Feature bit 0 indicates target functionality to the name server.
6714          */
6715         if (qla_tgt_mode_enabled(vha)) {
6716                 fc4_feature = BIT_0;
6717         } else if (qla_ini_mode_enabled(vha)) {
6718                 fc4_feature = BIT_1;
6719         } else if (qla_dual_mode_enabled(vha))
6720                 fc4_feature = BIT_0 | BIT_1;
6721
6722         return fc4_feature;
6723 }
6724
6725 /*
6726  * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6727  * @ha: HA context
6728  *
6729  * Beginning of ATIO ring has initialization control block already built
6730  * by nvram config routine.
6731  *
6732  * Returns 0 on success.
6733  */
6734 void
6735 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6736 {
6737         struct qla_hw_data *ha = vha->hw;
6738         uint16_t cnt;
6739         struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6740
6741         if (qla_ini_mode_enabled(vha))
6742                 return;
6743
6744         for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6745                 pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6746                 pkt++;
6747         }
6748
6749 }
6750
6751 /*
6752  * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6753  * @ha: SCSI driver HA context
6754  */
6755 void
6756 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6757 {
6758         struct qla_hw_data *ha = vha->hw;
6759         struct atio_from_isp *pkt;
6760         int cnt, i;
6761
6762         if (!ha->flags.fw_started)
6763                 return;
6764
6765         while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6766             fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6767                 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6768                 cnt = pkt->u.raw.entry_count;
6769
6770                 if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6771                         /*
6772                          * This packet is corrupted. The header + payload
6773                          * can not be trusted. There is no point in passing
6774                          * it further up.
6775                          */
6776                         ql_log(ql_log_warn, vha, 0xd03c,
6777                             "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6778                             &pkt->u.isp24.fcp_hdr.s_id,
6779                             be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6780                             pkt->u.isp24.exchange_addr, pkt);
6781
6782                         adjust_corrupted_atio(pkt);
6783                         qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6784                             ha_locked, 0);
6785                 } else {
6786                         qlt_24xx_atio_pkt_all_vps(vha,
6787                             (struct atio_from_isp *)pkt, ha_locked);
6788                 }
6789
6790                 for (i = 0; i < cnt; i++) {
6791                         ha->tgt.atio_ring_index++;
6792                         if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6793                                 ha->tgt.atio_ring_index = 0;
6794                                 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6795                         } else
6796                                 ha->tgt.atio_ring_ptr++;
6797
6798                         pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6799                         pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6800                 }
6801                 wmb();
6802         }
6803
6804         /* Adjust ring index */
6805         wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6806 }
6807
6808 void
6809 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6810 {
6811         struct qla_hw_data *ha = vha->hw;
6812         struct qla_msix_entry *msix = &ha->msix_entries[2];
6813         struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6814
6815         if (!QLA_TGT_MODE_ENABLED())
6816                 return;
6817
6818         wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6819         wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6820         rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6821
6822         if (ha->flags.msix_enabled) {
6823                 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6824                         if (IS_QLA2071(ha)) {
6825                                 /* 4 ports Baker: Enable Interrupt Handshake */
6826                                 icb->msix_atio = 0;
6827                                 icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6828                         } else {
6829                                 icb->msix_atio = cpu_to_le16(msix->entry);
6830                                 icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6831                         }
6832                         ql_dbg(ql_dbg_init, vha, 0xf072,
6833                             "Registering ICB vector 0x%x for atio que.\n",
6834                             msix->entry);
6835                 }
6836         } else {
6837                 /* INTx|MSI */
6838                 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6839                         icb->msix_atio = 0;
6840                         icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6841                         ql_dbg(ql_dbg_init, vha, 0xf072,
6842                             "%s: Use INTx for ATIOQ.\n", __func__);
6843                 }
6844         }
6845 }
6846
6847 void
6848 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6849 {
6850         struct qla_hw_data *ha = vha->hw;
6851         u32 tmp;
6852
6853         if (!QLA_TGT_MODE_ENABLED())
6854                 return;
6855
6856         if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6857                 if (!ha->tgt.saved_set) {
6858                         /* We save only once */
6859                         ha->tgt.saved_exchange_count = nv->exchange_count;
6860                         ha->tgt.saved_firmware_options_1 =
6861                             nv->firmware_options_1;
6862                         ha->tgt.saved_firmware_options_2 =
6863                             nv->firmware_options_2;
6864                         ha->tgt.saved_firmware_options_3 =
6865                             nv->firmware_options_3;
6866                         ha->tgt.saved_set = 1;
6867                 }
6868
6869                 if (qla_tgt_mode_enabled(vha))
6870                         nv->exchange_count = cpu_to_le16(0xFFFF);
6871                 else                    /* dual */
6872                         nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6873
6874                 /* Enable target mode */
6875                 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6876
6877                 /* Disable ini mode, if requested */
6878                 if (qla_tgt_mode_enabled(vha))
6879                         nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6880
6881                 /* Disable Full Login after LIP */
6882                 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6883                 /* Enable initial LIP */
6884                 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6885                 if (ql2xtgt_tape_enable)
6886                         /* Enable FC Tape support */
6887                         nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6888                 else
6889                         /* Disable FC Tape support */
6890                         nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
6891
6892                 /* Disable Full Login after LIP */
6893                 nv->host_p &= cpu_to_le32(~BIT_10);
6894
6895                 /*
6896                  * clear BIT 15 explicitly as we have seen at least
6897                  * a couple of instances where this was set and this
6898                  * was causing the firmware to not be initialized.
6899                  */
6900                 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6901                 /* Enable target PRLI control */
6902                 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6903
6904                 if (IS_QLA25XX(ha)) {
6905                         /* Change Loop-prefer to Pt-Pt */
6906                         tmp = ~(BIT_4|BIT_5|BIT_6);
6907                         nv->firmware_options_2 &= cpu_to_le32(tmp);
6908                         tmp = P2P << 4;
6909                         nv->firmware_options_2 |= cpu_to_le32(tmp);
6910                 }
6911         } else {
6912                 if (ha->tgt.saved_set) {
6913                         nv->exchange_count = ha->tgt.saved_exchange_count;
6914                         nv->firmware_options_1 =
6915                             ha->tgt.saved_firmware_options_1;
6916                         nv->firmware_options_2 =
6917                             ha->tgt.saved_firmware_options_2;
6918                         nv->firmware_options_3 =
6919                             ha->tgt.saved_firmware_options_3;
6920                 }
6921                 return;
6922         }
6923
6924         if (ha->base_qpair->enable_class_2) {
6925                 if (vha->flags.init_done)
6926                         fc_host_supported_classes(vha->host) =
6927                                 FC_COS_CLASS2 | FC_COS_CLASS3;
6928
6929                 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6930         } else {
6931                 if (vha->flags.init_done)
6932                         fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
6933
6934                 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6935         }
6936 }
6937
6938 void
6939 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
6940         struct init_cb_24xx *icb)
6941 {
6942         struct qla_hw_data *ha = vha->hw;
6943
6944         if (!QLA_TGT_MODE_ENABLED())
6945                 return;
6946
6947         if (ha->tgt.node_name_set) {
6948                 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6949                 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6950         }
6951 }
6952
6953 void
6954 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
6955 {
6956         struct qla_hw_data *ha = vha->hw;
6957         u32 tmp;
6958
6959         if (!QLA_TGT_MODE_ENABLED())
6960                 return;
6961
6962         if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6963                 if (!ha->tgt.saved_set) {
6964                         /* We save only once */
6965                         ha->tgt.saved_exchange_count = nv->exchange_count;
6966                         ha->tgt.saved_firmware_options_1 =
6967                             nv->firmware_options_1;
6968                         ha->tgt.saved_firmware_options_2 =
6969                             nv->firmware_options_2;
6970                         ha->tgt.saved_firmware_options_3 =
6971                             nv->firmware_options_3;
6972                         ha->tgt.saved_set = 1;
6973                 }
6974
6975                 if (qla_tgt_mode_enabled(vha))
6976                         nv->exchange_count = cpu_to_le16(0xFFFF);
6977                 else                    /* dual */
6978                         nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6979
6980                 /* Enable target mode */
6981                 nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6982
6983                 /* Disable ini mode, if requested */
6984                 if (qla_tgt_mode_enabled(vha))
6985                         nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6986                 /* Disable Full Login after LIP */
6987                 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6988                 /* Enable initial LIP */
6989                 nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6990                 /*
6991                  * clear BIT 15 explicitly as we have seen at
6992                  * least a couple of instances where this was set
6993                  * and this was causing the firmware to not be
6994                  * initialized.
6995                  */
6996                 nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6997                 if (ql2xtgt_tape_enable)
6998                         /* Enable FC tape support */
6999                         nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7000                 else
7001                         /* Disable FC tape support */
7002                         nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7003
7004                 /* Disable Full Login after LIP */
7005                 nv->host_p &= cpu_to_le32(~BIT_10);
7006                 /* Enable target PRLI control */
7007                 nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7008
7009                 /* Change Loop-prefer to Pt-Pt */
7010                 tmp = ~(BIT_4|BIT_5|BIT_6);
7011                 nv->firmware_options_2 &= cpu_to_le32(tmp);
7012                 tmp = P2P << 4;
7013                 nv->firmware_options_2 |= cpu_to_le32(tmp);
7014         } else {
7015                 if (ha->tgt.saved_set) {
7016                         nv->exchange_count = ha->tgt.saved_exchange_count;
7017                         nv->firmware_options_1 =
7018                             ha->tgt.saved_firmware_options_1;
7019                         nv->firmware_options_2 =
7020                             ha->tgt.saved_firmware_options_2;
7021                         nv->firmware_options_3 =
7022                             ha->tgt.saved_firmware_options_3;
7023                 }
7024                 return;
7025         }
7026
7027         if (ha->base_qpair->enable_class_2) {
7028                 if (vha->flags.init_done)
7029                         fc_host_supported_classes(vha->host) =
7030                                 FC_COS_CLASS2 | FC_COS_CLASS3;
7031
7032                 nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7033         } else {
7034                 if (vha->flags.init_done)
7035                         fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7036
7037                 nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7038         }
7039 }
7040
7041 void
7042 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7043         struct init_cb_81xx *icb)
7044 {
7045         struct qla_hw_data *ha = vha->hw;
7046
7047         if (!QLA_TGT_MODE_ENABLED())
7048                 return;
7049
7050         if (ha->tgt.node_name_set) {
7051                 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7052                 icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7053         }
7054 }
7055
7056 void
7057 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7058 {
7059         if (!QLA_TGT_MODE_ENABLED())
7060                 return;
7061
7062         ha->msix_count += 1; /* For ATIO Q */
7063 }
7064
7065
7066 void
7067 qlt_modify_vp_config(struct scsi_qla_host *vha,
7068         struct vp_config_entry_24xx *vpmod)
7069 {
7070         /* enable target mode.  Bit5 = 1 => disable */
7071         if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7072                 vpmod->options_idx1 &= ~BIT_5;
7073
7074         /* Disable ini mode, if requested.  bit4 = 1 => disable */
7075         if (qla_tgt_mode_enabled(vha))
7076                 vpmod->options_idx1 &= ~BIT_4;
7077 }
7078
7079 void
7080 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7081 {
7082         int rc;
7083
7084         if (!QLA_TGT_MODE_ENABLED())
7085                 return;
7086
7087         if  ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
7088             IS_QLA28XX(ha)) {
7089                 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7090                 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7091         } else {
7092                 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7093                 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7094         }
7095
7096         mutex_init(&base_vha->vha_tgt.tgt_mutex);
7097         mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7098
7099         INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7100         INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7101             qlt_unknown_atio_work_fn);
7102
7103         qlt_clear_mode(base_vha);
7104
7105         rc = btree_init32(&ha->tgt.host_map);
7106         if (rc)
7107                 ql_log(ql_log_info, base_vha, 0xd03d,
7108                     "Unable to initialize ha->host_map btree\n");
7109
7110         qlt_update_vp_map(base_vha, SET_VP_IDX);
7111 }
7112
7113 irqreturn_t
7114 qla83xx_msix_atio_q(int irq, void *dev_id)
7115 {
7116         struct rsp_que *rsp;
7117         scsi_qla_host_t *vha;
7118         struct qla_hw_data *ha;
7119         unsigned long flags;
7120
7121         rsp = (struct rsp_que *) dev_id;
7122         ha = rsp->hw;
7123         vha = pci_get_drvdata(ha->pdev);
7124
7125         spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7126
7127         qlt_24xx_process_atio_queue(vha, 0);
7128
7129         spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7130
7131         return IRQ_HANDLED;
7132 }
7133
7134 static void
7135 qlt_handle_abts_recv_work(struct work_struct *work)
7136 {
7137         struct qla_tgt_sess_op *op = container_of(work,
7138                 struct qla_tgt_sess_op, work);
7139         scsi_qla_host_t *vha = op->vha;
7140         struct qla_hw_data *ha = vha->hw;
7141         unsigned long flags;
7142
7143         if (qla2x00_reset_active(vha) ||
7144             (op->chip_reset != ha->base_qpair->chip_reset))
7145                 return;
7146
7147         spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7148         qlt_24xx_process_atio_queue(vha, 0);
7149         spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7150
7151         spin_lock_irqsave(&ha->hardware_lock, flags);
7152         qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7153         spin_unlock_irqrestore(&ha->hardware_lock, flags);
7154
7155         kfree(op);
7156 }
7157
7158 void
7159 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7160     response_t *pkt)
7161 {
7162         struct qla_tgt_sess_op *op;
7163
7164         op = kzalloc(sizeof(*op), GFP_ATOMIC);
7165
7166         if (!op) {
7167                 /* do not reach for ATIO queue here.  This is best effort err
7168                  * recovery at this point.
7169                  */
7170                 qlt_response_pkt_all_vps(vha, rsp, pkt);
7171                 return;
7172         }
7173
7174         memcpy(&op->atio, pkt, sizeof(*pkt));
7175         op->vha = vha;
7176         op->chip_reset = vha->hw->base_qpair->chip_reset;
7177         op->rsp = rsp;
7178         INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7179         queue_work(qla_tgt_wq, &op->work);
7180         return;
7181 }
7182
7183 int
7184 qlt_mem_alloc(struct qla_hw_data *ha)
7185 {
7186         if (!QLA_TGT_MODE_ENABLED())
7187                 return 0;
7188
7189         ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7190                                      sizeof(struct qla_tgt_vp_map),
7191                                      GFP_KERNEL);
7192         if (!ha->tgt.tgt_vp_map)
7193                 return -ENOMEM;
7194
7195         ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7196             (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7197             &ha->tgt.atio_dma, GFP_KERNEL);
7198         if (!ha->tgt.atio_ring) {
7199                 kfree(ha->tgt.tgt_vp_map);
7200                 return -ENOMEM;
7201         }
7202         return 0;
7203 }
7204
7205 void
7206 qlt_mem_free(struct qla_hw_data *ha)
7207 {
7208         if (!QLA_TGT_MODE_ENABLED())
7209                 return;
7210
7211         if (ha->tgt.atio_ring) {
7212                 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7213                     sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7214                     ha->tgt.atio_dma);
7215         }
7216         ha->tgt.atio_ring = NULL;
7217         ha->tgt.atio_dma = 0;
7218         kfree(ha->tgt.tgt_vp_map);
7219         ha->tgt.tgt_vp_map = NULL;
7220 }
7221
7222 /* vport_slock to be held by the caller */
7223 void
7224 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7225 {
7226         void *slot;
7227         u32 key;
7228         int rc;
7229
7230         if (!QLA_TGT_MODE_ENABLED())
7231                 return;
7232
7233         key = vha->d_id.b24;
7234
7235         switch (cmd) {
7236         case SET_VP_IDX:
7237                 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7238                 break;
7239         case SET_AL_PA:
7240                 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7241                 if (!slot) {
7242                         ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7243                             "Save vha in host_map %p %06x\n", vha, key);
7244                         rc = btree_insert32(&vha->hw->tgt.host_map,
7245                                 key, vha, GFP_ATOMIC);
7246                         if (rc)
7247                                 ql_log(ql_log_info, vha, 0xd03e,
7248                                     "Unable to insert s_id into host_map: %06x\n",
7249                                     key);
7250                         return;
7251                 }
7252                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7253                     "replace existing vha in host_map %p %06x\n", vha, key);
7254                 btree_update32(&vha->hw->tgt.host_map, key, vha);
7255                 break;
7256         case RESET_VP_IDX:
7257                 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7258                 break;
7259         case RESET_AL_PA:
7260                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7261                    "clear vha in host_map %p %06x\n", vha, key);
7262                 slot = btree_lookup32(&vha->hw->tgt.host_map, key);
7263                 if (slot)
7264                         btree_remove32(&vha->hw->tgt.host_map, key);
7265                 vha->d_id.b24 = 0;
7266                 break;
7267         }
7268 }
7269
7270 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7271 {
7272
7273         if (!vha->d_id.b24) {
7274                 vha->d_id = id;
7275                 qlt_update_vp_map(vha, SET_AL_PA);
7276         } else if (vha->d_id.b24 != id.b24) {
7277                 qlt_update_vp_map(vha, RESET_AL_PA);
7278                 vha->d_id = id;
7279                 qlt_update_vp_map(vha, SET_AL_PA);
7280         }
7281 }
7282
7283 static int __init qlt_parse_ini_mode(void)
7284 {
7285         if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7286                 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7287         else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7288                 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7289         else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7290                 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7291         else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7292                 ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7293         else
7294                 return false;
7295
7296         return true;
7297 }
7298
7299 int __init qlt_init(void)
7300 {
7301         int ret;
7302
7303         BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7304         BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7305
7306         if (!qlt_parse_ini_mode()) {
7307                 ql_log(ql_log_fatal, NULL, 0xe06b,
7308                     "qlt_parse_ini_mode() failed\n");
7309                 return -EINVAL;
7310         }
7311
7312         if (!QLA_TGT_MODE_ENABLED())
7313                 return 0;
7314
7315         qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7316             sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7317             qla_tgt_mgmt_cmd), 0, NULL);
7318         if (!qla_tgt_mgmt_cmd_cachep) {
7319                 ql_log(ql_log_fatal, NULL, 0xd04b,
7320                     "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7321                 return -ENOMEM;
7322         }
7323
7324         qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7325             sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7326             0, NULL);
7327
7328         if (!qla_tgt_plogi_cachep) {
7329                 ql_log(ql_log_fatal, NULL, 0xe06d,
7330                     "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7331                 ret = -ENOMEM;
7332                 goto out_mgmt_cmd_cachep;
7333         }
7334
7335         qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7336             mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7337         if (!qla_tgt_mgmt_cmd_mempool) {
7338                 ql_log(ql_log_fatal, NULL, 0xe06e,
7339                     "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7340                 ret = -ENOMEM;
7341                 goto out_plogi_cachep;
7342         }
7343
7344         qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7345         if (!qla_tgt_wq) {
7346                 ql_log(ql_log_fatal, NULL, 0xe06f,
7347                     "alloc_workqueue for qla_tgt_wq failed\n");
7348                 ret = -ENOMEM;
7349                 goto out_cmd_mempool;
7350         }
7351         /*
7352          * Return 1 to signal that initiator-mode is being disabled
7353          */
7354         return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7355
7356 out_cmd_mempool:
7357         mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7358 out_plogi_cachep:
7359         kmem_cache_destroy(qla_tgt_plogi_cachep);
7360 out_mgmt_cmd_cachep:
7361         kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7362         return ret;
7363 }
7364
7365 void qlt_exit(void)
7366 {
7367         if (!QLA_TGT_MODE_ENABLED())
7368                 return;
7369
7370         destroy_workqueue(qla_tgt_wq);
7371         mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7372         kmem_cache_destroy(qla_tgt_plogi_cachep);
7373         kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7374 }