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