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