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