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