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