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