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