blob: f3287e1bc554d9f6c3fb573ee31259233eb7690b [file] [log] [blame]
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001/*
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 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -070013 * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040014 *
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>
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040029#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
Arun Easid154f352014-09-25 06:14:48 -040045static int ql2xtgt_tape_enable;
46module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040050static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51module_param(qlini_mode, charp, S_IRUGO);
52MODULE_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
Arun Easiaa230bc2013-01-30 03:34:39 -050060int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040061
Quinn Tran33e79972014-09-25 06:14:55 -040062static int temp_sam_status = SAM_STAT_BUSY;
63
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040064/*
65 * From scsi/fc/fc_fcp.h
66 */
67enum 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 */
Masanari Iida6efb3c0a2012-10-26 22:10:54 +090083#define FCP_PTA_ACA 4 /* auto. contingent allegiance */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040084#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 */
102static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
103 struct atio_from_isp *pkt);
104static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
105static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
106 int fn, void *iocb, int flags);
107static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
108 *cmd, struct atio_from_isp *atio, int ha_locked);
109static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha,
110 struct qla_tgt_srr_imm *imm, int ha_lock);
Arun Easib6a029e2014-09-25 06:14:52 -0400111static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
112 struct qla_tgt_cmd *cmd);
Quinn Tran33e79972014-09-25 06:14:55 -0400113static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
114 struct atio_from_isp *atio, uint16_t status, int qfull);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400115/*
116 * Global Variables
117 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400118static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
119static mempool_t *qla_tgt_mgmt_cmd_mempool;
120static struct workqueue_struct *qla_tgt_wq;
121static DEFINE_MUTEX(qla_tgt_mutex);
122static LIST_HEAD(qla_tgt_glist);
123
124/* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
125static struct qla_tgt_sess *qlt_find_sess_by_port_name(
126 struct qla_tgt *tgt,
127 const uint8_t *port_name)
128{
129 struct qla_tgt_sess *sess;
130
131 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
132 if (!memcmp(sess->port_name, port_name, WWN_SIZE))
133 return sess;
134 }
135
136 return NULL;
137}
138
139/* Might release hw lock, then reaquire!! */
140static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
141{
142 /* Send marker if required */
143 if (unlikely(vha->marker_needed != 0)) {
144 int rc = qla2x00_issue_marker(vha, vha_locked);
145 if (rc != QLA_SUCCESS) {
146 ql_dbg(ql_dbg_tgt, vha, 0xe03d,
147 "qla_target(%d): issue_marker() failed\n",
148 vha->vp_idx);
149 }
150 return rc;
151 }
152 return QLA_SUCCESS;
153}
154
155static inline
156struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
157 uint8_t *d_id)
158{
159 struct qla_hw_data *ha = vha->hw;
160 uint8_t vp_idx;
161
162 if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
163 return NULL;
164
165 if (vha->d_id.b.al_pa == d_id[2])
166 return vha;
167
168 BUG_ON(ha->tgt.tgt_vp_map == NULL);
169 vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
170 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
171 return ha->tgt.tgt_vp_map[vp_idx].vha;
172
173 return NULL;
174}
175
176static inline
177struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
178 uint16_t vp_idx)
179{
180 struct qla_hw_data *ha = vha->hw;
181
182 if (vha->vp_idx == vp_idx)
183 return vha;
184
185 BUG_ON(ha->tgt.tgt_vp_map == NULL);
186 if (likely(test_bit(vp_idx, ha->vp_idx_map)))
187 return ha->tgt.tgt_vp_map[vp_idx].vha;
188
189 return NULL;
190}
191
Quinn Tran33e79972014-09-25 06:14:55 -0400192static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
193{
194 unsigned long flags;
195
196 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
197
198 vha->hw->tgt.num_pend_cmds++;
199 if (vha->hw->tgt.num_pend_cmds > vha->hw->qla_stats.stat_max_pend_cmds)
200 vha->hw->qla_stats.stat_max_pend_cmds =
201 vha->hw->tgt.num_pend_cmds;
202 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
203}
204static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
205{
206 unsigned long flags;
207
208 spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
209 vha->hw->tgt.num_pend_cmds--;
210 spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
211}
212
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400213void qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
214 struct atio_from_isp *atio)
215{
Quinn Tranf83adb62014-04-11 16:54:43 -0400216 ql_dbg(ql_dbg_tgt, vha, 0xe072,
217 "%s: qla_target(%d): type %x ox_id %04x\n",
218 __func__, vha->vp_idx, atio->u.raw.entry_type,
219 be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
220
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400221 switch (atio->u.raw.entry_type) {
222 case ATIO_TYPE7:
223 {
224 struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
225 atio->u.isp24.fcp_hdr.d_id);
226 if (unlikely(NULL == host)) {
227 ql_dbg(ql_dbg_tgt, vha, 0xe03e,
228 "qla_target(%d): Received ATIO_TYPE7 "
229 "with unknown d_id %x:%x:%x\n", vha->vp_idx,
230 atio->u.isp24.fcp_hdr.d_id[0],
231 atio->u.isp24.fcp_hdr.d_id[1],
232 atio->u.isp24.fcp_hdr.d_id[2]);
233 break;
234 }
235 qlt_24xx_atio_pkt(host, atio);
236 break;
237 }
238
239 case IMMED_NOTIFY_TYPE:
240 {
241 struct scsi_qla_host *host = vha;
242 struct imm_ntfy_from_isp *entry =
243 (struct imm_ntfy_from_isp *)atio;
244
245 if ((entry->u.isp24.vp_index != 0xFF) &&
246 (entry->u.isp24.nport_handle != 0xFFFF)) {
247 host = qlt_find_host_by_vp_idx(vha,
248 entry->u.isp24.vp_index);
249 if (unlikely(!host)) {
250 ql_dbg(ql_dbg_tgt, vha, 0xe03f,
251 "qla_target(%d): Received "
252 "ATIO (IMMED_NOTIFY_TYPE) "
253 "with unknown vp_index %d\n",
254 vha->vp_idx, entry->u.isp24.vp_index);
255 break;
256 }
257 }
258 qlt_24xx_atio_pkt(host, atio);
259 break;
260 }
261
262 default:
263 ql_dbg(ql_dbg_tgt, vha, 0xe040,
264 "qla_target(%d): Received unknown ATIO atio "
265 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
266 break;
267 }
268
269 return;
270}
271
272void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
273{
274 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -0400275 case CTIO_CRC2:
276 ql_dbg(ql_dbg_tgt, vha, 0xe073,
277 "qla_target(%d):%s: CRC2 Response pkt\n",
278 vha->vp_idx, __func__);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400279 case CTIO_TYPE7:
280 {
281 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
282 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
283 entry->vp_index);
284 if (unlikely(!host)) {
285 ql_dbg(ql_dbg_tgt, vha, 0xe041,
286 "qla_target(%d): Response pkt (CTIO_TYPE7) "
287 "received, with unknown vp_index %d\n",
288 vha->vp_idx, entry->vp_index);
289 break;
290 }
291 qlt_response_pkt(host, pkt);
292 break;
293 }
294
295 case IMMED_NOTIFY_TYPE:
296 {
297 struct scsi_qla_host *host = vha;
298 struct imm_ntfy_from_isp *entry =
299 (struct imm_ntfy_from_isp *)pkt;
300
301 host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
302 if (unlikely(!host)) {
303 ql_dbg(ql_dbg_tgt, vha, 0xe042,
304 "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
305 "received, with unknown vp_index %d\n",
306 vha->vp_idx, entry->u.isp24.vp_index);
307 break;
308 }
309 qlt_response_pkt(host, pkt);
310 break;
311 }
312
313 case NOTIFY_ACK_TYPE:
314 {
315 struct scsi_qla_host *host = vha;
316 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
317
318 if (0xFF != entry->u.isp24.vp_index) {
319 host = qlt_find_host_by_vp_idx(vha,
320 entry->u.isp24.vp_index);
321 if (unlikely(!host)) {
322 ql_dbg(ql_dbg_tgt, vha, 0xe043,
323 "qla_target(%d): Response "
324 "pkt (NOTIFY_ACK_TYPE) "
325 "received, with unknown "
326 "vp_index %d\n", vha->vp_idx,
327 entry->u.isp24.vp_index);
328 break;
329 }
330 }
331 qlt_response_pkt(host, pkt);
332 break;
333 }
334
335 case ABTS_RECV_24XX:
336 {
337 struct abts_recv_from_24xx *entry =
338 (struct abts_recv_from_24xx *)pkt;
339 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
340 entry->vp_index);
341 if (unlikely(!host)) {
342 ql_dbg(ql_dbg_tgt, vha, 0xe044,
343 "qla_target(%d): Response pkt "
344 "(ABTS_RECV_24XX) received, with unknown "
345 "vp_index %d\n", vha->vp_idx, entry->vp_index);
346 break;
347 }
348 qlt_response_pkt(host, pkt);
349 break;
350 }
351
352 case ABTS_RESP_24XX:
353 {
354 struct abts_resp_to_24xx *entry =
355 (struct abts_resp_to_24xx *)pkt;
356 struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
357 entry->vp_index);
358 if (unlikely(!host)) {
359 ql_dbg(ql_dbg_tgt, vha, 0xe045,
360 "qla_target(%d): Response pkt "
361 "(ABTS_RECV_24XX) received, with unknown "
362 "vp_index %d\n", vha->vp_idx, entry->vp_index);
363 break;
364 }
365 qlt_response_pkt(host, pkt);
366 break;
367 }
368
369 default:
370 qlt_response_pkt(vha, pkt);
371 break;
372 }
373
374}
375
376static void qlt_free_session_done(struct work_struct *work)
377{
378 struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
379 free_work);
380 struct qla_tgt *tgt = sess->tgt;
381 struct scsi_qla_host *vha = sess->vha;
382 struct qla_hw_data *ha = vha->hw;
383
384 BUG_ON(!tgt);
385 /*
386 * Release the target session for FC Nexus from fabric module code.
387 */
388 if (sess->se_sess != NULL)
389 ha->tgt.tgt_ops->free_session(sess);
390
391 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
392 "Unregistration of sess %p finished\n", sess);
393
394 kfree(sess);
395 /*
396 * We need to protect against race, when tgt is freed before or
397 * inside wake_up()
398 */
399 tgt->sess_count--;
400 if (tgt->sess_count == 0)
401 wake_up_all(&tgt->waitQ);
402}
403
404/* ha->hardware_lock supposed to be held on entry */
405void qlt_unreg_sess(struct qla_tgt_sess *sess)
406{
407 struct scsi_qla_host *vha = sess->vha;
408
409 vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
410
411 list_del(&sess->sess_list_entry);
412 if (sess->deleted)
413 list_del(&sess->del_list_entry);
414
415 INIT_WORK(&sess->free_work, qlt_free_session_done);
416 schedule_work(&sess->free_work);
417}
418EXPORT_SYMBOL(qlt_unreg_sess);
419
420/* ha->hardware_lock supposed to be held on entry */
421static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
422{
423 struct qla_hw_data *ha = vha->hw;
424 struct qla_tgt_sess *sess = NULL;
425 uint32_t unpacked_lun, lun = 0;
426 uint16_t loop_id;
427 int res = 0;
428 struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
429 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
430
431 loop_id = le16_to_cpu(n->u.isp24.nport_handle);
432 if (loop_id == 0xFFFF) {
433#if 0 /* FIXME: Re-enable Global event handling.. */
434 /* Global event */
435 atomic_inc(&ha->tgt.qla_tgt->tgt_global_resets_count);
436 qlt_clear_tgt_db(ha->tgt.qla_tgt, 1);
437 if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
438 sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
439 typeof(*sess), sess_list_entry);
440 switch (mcmd) {
441 case QLA_TGT_NEXUS_LOSS_SESS:
442 mcmd = QLA_TGT_NEXUS_LOSS;
443 break;
444 case QLA_TGT_ABORT_ALL_SESS:
445 mcmd = QLA_TGT_ABORT_ALL;
446 break;
447 case QLA_TGT_NEXUS_LOSS:
448 case QLA_TGT_ABORT_ALL:
449 break;
450 default:
451 ql_dbg(ql_dbg_tgt, vha, 0xe046,
452 "qla_target(%d): Not allowed "
453 "command %x in %s", vha->vp_idx,
454 mcmd, __func__);
455 sess = NULL;
456 break;
457 }
458 } else
459 sess = NULL;
460#endif
461 } else {
462 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
463 }
464
465 ql_dbg(ql_dbg_tgt, vha, 0xe000,
466 "Using sess for qla_tgt_reset: %p\n", sess);
467 if (!sess) {
468 res = -ESRCH;
469 return res;
470 }
471
472 ql_dbg(ql_dbg_tgt, vha, 0xe047,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400473 "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
474 "loop_id %d)\n", vha->host_no, sess, sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400475 mcmd, loop_id);
476
477 lun = a->u.isp24.fcp_cmnd.lun;
478 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
479
480 return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
481 iocb, QLA24XX_MGMT_SEND_NACK);
482}
483
484/* ha->hardware_lock supposed to be held on entry */
485static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
486 bool immediate)
487{
488 struct qla_tgt *tgt = sess->tgt;
489 uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
490
491 if (sess->deleted)
492 return;
493
494 ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
495 "Scheduling sess %p for deletion\n", sess);
496 list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
497 sess->deleted = 1;
498
499 if (immediate)
500 dev_loss_tmo = 0;
501
502 sess->expires = jiffies + dev_loss_tmo * HZ;
503
504 ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400505 "qla_target(%d): session for port %8phC (loop ID %d) scheduled for "
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400506 "deletion in %u secs (expires: %lu) immed: %d\n",
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400507 sess->vha->vp_idx, sess->port_name, sess->loop_id, dev_loss_tmo,
508 sess->expires, immediate);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400509
510 if (immediate)
511 schedule_delayed_work(&tgt->sess_del_work, 0);
512 else
513 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530514 sess->expires - jiffies);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400515}
516
517/* ha->hardware_lock supposed to be held on entry */
518static void qlt_clear_tgt_db(struct qla_tgt *tgt, bool local_only)
519{
520 struct qla_tgt_sess *sess;
521
522 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
523 qlt_schedule_sess_for_deletion(sess, true);
524
525 /* At this point tgt could be already dead */
526}
527
528static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
529 uint16_t *loop_id)
530{
531 struct qla_hw_data *ha = vha->hw;
532 dma_addr_t gid_list_dma;
533 struct gid_list_info *gid_list;
534 char *id_iter;
535 int res, rc, i;
536 uint16_t entries;
537
538 gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
539 &gid_list_dma, GFP_KERNEL);
540 if (!gid_list) {
541 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
542 "qla_target(%d): DMA Alloc failed of %u\n",
543 vha->vp_idx, qla2x00_gid_list_size(ha));
544 return -ENOMEM;
545 }
546
547 /* Get list of logged in devices */
548 rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
549 if (rc != QLA_SUCCESS) {
550 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
551 "qla_target(%d): get_id_list() failed: %x\n",
552 vha->vp_idx, rc);
553 res = -1;
554 goto out_free_id_list;
555 }
556
557 id_iter = (char *)gid_list;
558 res = -1;
559 for (i = 0; i < entries; i++) {
560 struct gid_list_info *gid = (struct gid_list_info *)id_iter;
561 if ((gid->al_pa == s_id[2]) &&
562 (gid->area == s_id[1]) &&
563 (gid->domain == s_id[0])) {
564 *loop_id = le16_to_cpu(gid->loop_id);
565 res = 0;
566 break;
567 }
568 id_iter += ha->gid_list_info_size;
569 }
570
571out_free_id_list:
572 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
573 gid_list, gid_list_dma);
574 return res;
575}
576
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400577/* ha->hardware_lock supposed to be held on entry */
578static void qlt_undelete_sess(struct qla_tgt_sess *sess)
579{
580 BUG_ON(!sess->deleted);
581
582 list_del(&sess->del_list_entry);
583 sess->deleted = 0;
584}
585
586static void qlt_del_sess_work_fn(struct delayed_work *work)
587{
588 struct qla_tgt *tgt = container_of(work, struct qla_tgt,
589 sess_del_work);
590 struct scsi_qla_host *vha = tgt->vha;
591 struct qla_hw_data *ha = vha->hw;
592 struct qla_tgt_sess *sess;
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530593 unsigned long flags, elapsed;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400594
595 spin_lock_irqsave(&ha->hardware_lock, flags);
596 while (!list_empty(&tgt->del_sess_list)) {
597 sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
598 del_list_entry);
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530599 elapsed = jiffies;
600 if (time_after_eq(elapsed, sess->expires)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400601 qlt_undelete_sess(sess);
602
Jörn Engel08234e32013-06-12 16:27:54 -0400603 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
604 "Timeout: sess %p about to be deleted\n",
605 sess);
606 ha->tgt.tgt_ops->shutdown_sess(sess);
607 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400608 } else {
609 schedule_delayed_work(&tgt->sess_del_work,
Shivaram Upadhyayula63832aa2013-12-10 16:06:40 +0530610 sess->expires - elapsed);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400611 break;
612 }
613 }
614 spin_unlock_irqrestore(&ha->hardware_lock, flags);
615}
616
617/*
618 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
619 * Caller must put it.
620 */
621static struct qla_tgt_sess *qlt_create_sess(
622 struct scsi_qla_host *vha,
623 fc_port_t *fcport,
624 bool local)
625{
626 struct qla_hw_data *ha = vha->hw;
627 struct qla_tgt_sess *sess;
628 unsigned long flags;
629 unsigned char be_sid[3];
630
631 /* Check to avoid double sessions */
632 spin_lock_irqsave(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800633 list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400634 sess_list_entry) {
635 if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
636 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
637 "Double sess %p found (s_id %x:%x:%x, "
638 "loop_id %d), updating to d_id %x:%x:%x, "
639 "loop_id %d", sess, sess->s_id.b.domain,
640 sess->s_id.b.al_pa, sess->s_id.b.area,
641 sess->loop_id, fcport->d_id.b.domain,
642 fcport->d_id.b.al_pa, fcport->d_id.b.area,
643 fcport->loop_id);
644
645 if (sess->deleted)
646 qlt_undelete_sess(sess);
647
648 kref_get(&sess->se_sess->sess_kref);
Roland Dreierc8292d12012-10-11 13:41:32 -0700649 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
650 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
651
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400652 if (sess->local && !local)
653 sess->local = 0;
654 spin_unlock_irqrestore(&ha->hardware_lock, flags);
655
656 return sess;
657 }
658 }
659 spin_unlock_irqrestore(&ha->hardware_lock, flags);
660
661 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
662 if (!sess) {
663 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400664 "qla_target(%u): session allocation failed, all commands "
665 "from port %8phC will be refused", vha->vp_idx,
666 fcport->port_name);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400667
668 return NULL;
669 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800670 sess->tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400671 sess->vha = vha;
672 sess->s_id = fcport->d_id;
673 sess->loop_id = fcport->loop_id;
674 sess->local = local;
675
676 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
677 "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800678 sess, vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400679
680 be_sid[0] = sess->s_id.b.domain;
681 be_sid[1] = sess->s_id.b.area;
682 be_sid[2] = sess->s_id.b.al_pa;
683 /*
684 * Determine if this fc_port->port_name is allowed to access
685 * target mode using explict NodeACLs+MappedLUNs, or using
686 * TPG demo mode. If this is successful a target mode FC nexus
687 * is created.
688 */
689 if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
690 &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
691 kfree(sess);
692 return NULL;
693 }
694 /*
695 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
696 * access across ->hardware_lock reaquire.
697 */
698 kref_get(&sess->se_sess->sess_kref);
699
Roland Dreierc8292d12012-10-11 13:41:32 -0700700 sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400701 BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
702 memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
703
704 spin_lock_irqsave(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800705 list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
706 vha->vha_tgt.qla_tgt->sess_count++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400707 spin_unlock_irqrestore(&ha->hardware_lock, flags);
708
709 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400710 "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
711 "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
712 vha->vp_idx, local ? "local " : "", fcport->port_name,
713 fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
714 sess->s_id.b.al_pa, sess->conf_compl_supported ? "" : "not ");
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400715
716 return sess;
717}
718
719/*
720 * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
721 */
722void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
723{
724 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800725 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400726 struct qla_tgt_sess *sess;
727 unsigned long flags;
728
729 if (!vha->hw->tgt.tgt_ops)
730 return;
731
732 if (!tgt || (fcport->port_type != FCT_INITIATOR))
733 return;
734
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800735 if (qla_ini_mode_enabled(vha))
736 return;
737
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400738 spin_lock_irqsave(&ha->hardware_lock, flags);
739 if (tgt->tgt_stop) {
740 spin_unlock_irqrestore(&ha->hardware_lock, flags);
741 return;
742 }
743 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
744 if (!sess) {
745 spin_unlock_irqrestore(&ha->hardware_lock, flags);
746
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800747 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400748 sess = qlt_create_sess(vha, fcport, false);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800749 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400750
751 spin_lock_irqsave(&ha->hardware_lock, flags);
752 } else {
753 kref_get(&sess->se_sess->sess_kref);
754
755 if (sess->deleted) {
756 qlt_undelete_sess(sess);
757
758 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400759 "qla_target(%u): %ssession for port %8phC "
760 "(loop ID %d) reappeared\n", vha->vp_idx,
761 sess->local ? "local " : "", sess->port_name,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400762 sess->loop_id);
763
764 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
765 "Reappeared sess %p\n", sess);
766 }
Roland Dreierc8292d12012-10-11 13:41:32 -0700767 ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
768 (fcport->flags & FCF_CONF_COMP_SUPPORTED));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400769 }
770
771 if (sess && sess->local) {
772 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
773 "qla_target(%u): local session for "
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -0400774 "port %8phC (loop ID %d) became global\n", vha->vp_idx,
775 fcport->port_name, sess->loop_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400776 sess->local = 0;
777 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400778 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -0400779 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400780}
781
782void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport)
783{
784 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800785 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400786 struct qla_tgt_sess *sess;
787 unsigned long flags;
788
789 if (!vha->hw->tgt.tgt_ops)
790 return;
791
792 if (!tgt || (fcport->port_type != FCT_INITIATOR))
793 return;
794
795 spin_lock_irqsave(&ha->hardware_lock, flags);
796 if (tgt->tgt_stop) {
797 spin_unlock_irqrestore(&ha->hardware_lock, flags);
798 return;
799 }
800 sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
801 if (!sess) {
802 spin_unlock_irqrestore(&ha->hardware_lock, flags);
803 return;
804 }
805
806 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
807
808 sess->local = 1;
809 qlt_schedule_sess_for_deletion(sess, false);
810 spin_unlock_irqrestore(&ha->hardware_lock, flags);
811}
812
813static inline int test_tgt_sess_count(struct qla_tgt *tgt)
814{
815 struct qla_hw_data *ha = tgt->ha;
816 unsigned long flags;
817 int res;
818 /*
819 * We need to protect against race, when tgt is freed before or
820 * inside wake_up()
821 */
822 spin_lock_irqsave(&ha->hardware_lock, flags);
823 ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
824 "tgt %p, empty(sess_list)=%d sess_count=%d\n",
825 tgt, list_empty(&tgt->sess_list), tgt->sess_count);
826 res = (tgt->sess_count == 0);
827 spin_unlock_irqrestore(&ha->hardware_lock, flags);
828
829 return res;
830}
831
832/* Called by tcm_qla2xxx configfs code */
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800833int qlt_stop_phase1(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400834{
835 struct scsi_qla_host *vha = tgt->vha;
836 struct qla_hw_data *ha = tgt->ha;
837 unsigned long flags;
838
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800839 mutex_lock(&qla_tgt_mutex);
840 if (!vha->fc_vport) {
841 struct Scsi_Host *sh = vha->host;
842 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
843 bool npiv_vports;
844
845 spin_lock_irqsave(sh->host_lock, flags);
846 npiv_vports = (fc_host->npiv_vports_inuse);
847 spin_unlock_irqrestore(sh->host_lock, flags);
848
849 if (npiv_vports) {
850 mutex_unlock(&qla_tgt_mutex);
851 return -EPERM;
852 }
853 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400854 if (tgt->tgt_stop || tgt->tgt_stopped) {
855 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
856 "Already in tgt->tgt_stop or tgt_stopped state\n");
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800857 mutex_unlock(&qla_tgt_mutex);
858 return -EPERM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400859 }
860
861 ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
862 vha->host_no, vha);
863 /*
864 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
865 * Lock is needed, because we still can get an incoming packet.
866 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800867 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400868 spin_lock_irqsave(&ha->hardware_lock, flags);
869 tgt->tgt_stop = 1;
870 qlt_clear_tgt_db(tgt, true);
871 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800872 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800873 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400874
Tejun Heo43829732012-08-20 14:51:24 -0700875 flush_delayed_work(&tgt->sess_del_work);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400876
877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
878 "Waiting for sess works (tgt %p)", tgt);
879 spin_lock_irqsave(&tgt->sess_work_lock, flags);
880 while (!list_empty(&tgt->sess_works_list)) {
881 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
882 flush_scheduled_work();
883 spin_lock_irqsave(&tgt->sess_work_lock, flags);
884 }
885 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
886
887 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
888 "Waiting for tgt %p: list_empty(sess_list)=%d "
889 "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
890 tgt->sess_count);
891
892 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
893
894 /* Big hammer */
895 if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
896 qlt_disable_vha(vha);
897
898 /* Wait for sessions to clear out (just in case) */
899 wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
Nicholas Bellinger3c231bd2014-02-19 17:50:22 -0800900 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400901}
902EXPORT_SYMBOL(qlt_stop_phase1);
903
904/* Called by tcm_qla2xxx configfs code */
905void qlt_stop_phase2(struct qla_tgt *tgt)
906{
907 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800908 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400909 unsigned long flags;
910
911 if (tgt->tgt_stopped) {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800912 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400913 "Already in tgt->tgt_stopped state\n");
914 dump_stack();
915 return;
916 }
917
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800918 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400919 "Waiting for %d IRQ commands to complete (tgt %p)",
920 tgt->irq_cmd_count, tgt);
921
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800922 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400923 spin_lock_irqsave(&ha->hardware_lock, flags);
924 while (tgt->irq_cmd_count != 0) {
925 spin_unlock_irqrestore(&ha->hardware_lock, flags);
926 udelay(2);
927 spin_lock_irqsave(&ha->hardware_lock, flags);
928 }
929 tgt->tgt_stop = 0;
930 tgt->tgt_stopped = 1;
931 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800932 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400933
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800934 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400935 tgt);
936}
937EXPORT_SYMBOL(qlt_stop_phase2);
938
939/* Called from qlt_remove_target() -> qla2x00_remove_one() */
Saurav Kashyapfa492632012-11-21 02:40:29 -0500940static void qlt_release(struct qla_tgt *tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400941{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800942 scsi_qla_host_t *vha = tgt->vha;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400943
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800944 if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400945 qlt_stop_phase2(tgt);
946
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800947 vha->vha_tgt.qla_tgt = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400948
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800949 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400950 "Release of tgt %p finished\n", tgt);
951
952 kfree(tgt);
953}
954
955/* ha->hardware_lock supposed to be held on entry */
956static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
957 const void *param, unsigned int param_size)
958{
959 struct qla_tgt_sess_work_param *prm;
960 unsigned long flags;
961
962 prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
963 if (!prm) {
964 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
965 "qla_target(%d): Unable to create session "
966 "work, command will be refused", 0);
967 return -ENOMEM;
968 }
969
970 ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
971 "Scheduling work (type %d, prm %p)"
972 " to find session for param %p (size %d, tgt %p)\n",
973 type, prm, param, param_size, tgt);
974
975 prm->type = type;
976 memcpy(&prm->tm_iocb, param, param_size);
977
978 spin_lock_irqsave(&tgt->sess_work_lock, flags);
979 list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
980 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
981
982 schedule_work(&tgt->sess_work);
983
984 return 0;
985}
986
987/*
988 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
989 */
990static void qlt_send_notify_ack(struct scsi_qla_host *vha,
991 struct imm_ntfy_from_isp *ntfy,
992 uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
993 uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
994{
995 struct qla_hw_data *ha = vha->hw;
996 request_t *pkt;
997 struct nack_to_isp *nack;
998
999 ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1000
1001 /* Send marker if required */
1002 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1003 return;
1004
1005 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1006 if (!pkt) {
1007 ql_dbg(ql_dbg_tgt, vha, 0xe049,
1008 "qla_target(%d): %s failed: unable to allocate "
1009 "request packet\n", vha->vp_idx, __func__);
1010 return;
1011 }
1012
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001013 if (vha->vha_tgt.qla_tgt != NULL)
1014 vha->vha_tgt.qla_tgt->notify_ack_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001015
1016 pkt->entry_type = NOTIFY_ACK_TYPE;
1017 pkt->entry_count = 1;
1018
1019 nack = (struct nack_to_isp *)pkt;
1020 nack->ox_id = ntfy->ox_id;
1021
1022 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1023 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1024 nack->u.isp24.flags = ntfy->u.isp24.flags &
1025 __constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1026 }
1027 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1028 nack->u.isp24.status = ntfy->u.isp24.status;
1029 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
Arun Easiaa230bc2013-01-30 03:34:39 -05001030 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001031 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1032 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1033 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1034 nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1035 nack->u.isp24.srr_reject_code = srr_reject_code;
1036 nack->u.isp24.srr_reject_code_expl = srr_explan;
1037 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1038
1039 ql_dbg(ql_dbg_tgt, vha, 0xe005,
1040 "qla_target(%d): Sending 24xx Notify Ack %d\n",
1041 vha->vp_idx, nack->u.isp24.status);
1042
1043 qla2x00_start_iocbs(vha, vha->req);
1044}
1045
1046/*
1047 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1048 */
1049static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1050 struct abts_recv_from_24xx *abts, uint32_t status,
1051 bool ids_reversed)
1052{
1053 struct qla_hw_data *ha = vha->hw;
1054 struct abts_resp_to_24xx *resp;
1055 uint32_t f_ctl;
1056 uint8_t *p;
1057
1058 ql_dbg(ql_dbg_tgt, vha, 0xe006,
1059 "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1060 ha, abts, status);
1061
1062 /* Send marker if required */
1063 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1064 return;
1065
Arun Easib6a029e2014-09-25 06:14:52 -04001066 resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001067 if (!resp) {
1068 ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1069 "qla_target(%d): %s failed: unable to allocate "
1070 "request packet", vha->vp_idx, __func__);
1071 return;
1072 }
1073
1074 resp->entry_type = ABTS_RESP_24XX;
1075 resp->entry_count = 1;
1076 resp->nport_handle = abts->nport_handle;
1077 resp->vp_index = vha->vp_idx;
1078 resp->sof_type = abts->sof_type;
1079 resp->exchange_address = abts->exchange_address;
1080 resp->fcp_hdr_le = abts->fcp_hdr_le;
1081 f_ctl = __constant_cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1082 F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1083 F_CTL_SEQ_INITIATIVE);
1084 p = (uint8_t *)&f_ctl;
1085 resp->fcp_hdr_le.f_ctl[0] = *p++;
1086 resp->fcp_hdr_le.f_ctl[1] = *p++;
1087 resp->fcp_hdr_le.f_ctl[2] = *p;
1088 if (ids_reversed) {
1089 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1090 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1091 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1092 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1093 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1094 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1095 } else {
1096 resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1097 resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1098 resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1099 resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1100 resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1101 resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1102 }
1103 resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1104 if (status == FCP_TMF_CMPL) {
1105 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1106 resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1107 resp->payload.ba_acct.low_seq_cnt = 0x0000;
1108 resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1109 resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1110 resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1111 } else {
1112 resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1113 resp->payload.ba_rjt.reason_code =
1114 BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1115 /* Other bytes are zero */
1116 }
1117
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001118 vha->vha_tgt.qla_tgt->abts_resp_expected++;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001119
1120 qla2x00_start_iocbs(vha, vha->req);
1121}
1122
1123/*
1124 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1125 */
1126static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1127 struct abts_resp_from_24xx_fw *entry)
1128{
1129 struct ctio7_to_24xx *ctio;
1130
1131 ql_dbg(ql_dbg_tgt, vha, 0xe007,
1132 "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1133 /* Send marker if required */
1134 if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1135 return;
1136
Arun Easib6a029e2014-09-25 06:14:52 -04001137 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001138 if (ctio == NULL) {
1139 ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1140 "qla_target(%d): %s failed: unable to allocate "
1141 "request packet\n", vha->vp_idx, __func__);
1142 return;
1143 }
1144
1145 /*
1146 * We've got on entrance firmware's response on by us generated
1147 * ABTS response. So, in it ID fields are reversed.
1148 */
1149
1150 ctio->entry_type = CTIO_TYPE7;
1151 ctio->entry_count = 1;
1152 ctio->nport_handle = entry->nport_handle;
1153 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1154 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1155 ctio->vp_index = vha->vp_idx;
1156 ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1157 ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1158 ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1159 ctio->exchange_addr = entry->exchange_addr_to_abort;
1160 ctio->u.status1.flags =
1161 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1162 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001163 ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001164
1165 qla2x00_start_iocbs(vha, vha->req);
1166
1167 qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1168 FCP_TMF_CMPL, true);
1169}
1170
1171/* ha->hardware_lock supposed to be held on entry */
1172static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1173 struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1174{
1175 struct qla_hw_data *ha = vha->hw;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001176 struct se_session *se_sess = sess->se_sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001177 struct qla_tgt_mgmt_cmd *mcmd;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001178 struct se_cmd *se_cmd;
1179 u32 lun = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001180 int rc;
Steve Hodgson06e97b42012-11-16 08:06:17 -08001181 bool found_lun = false;
1182
1183 spin_lock(&se_sess->sess_cmd_lock);
1184 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1185 struct qla_tgt_cmd *cmd =
1186 container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1187 if (cmd->tag == abts->exchange_addr_to_abort) {
1188 lun = cmd->unpacked_lun;
1189 found_lun = true;
1190 break;
1191 }
1192 }
1193 spin_unlock(&se_sess->sess_cmd_lock);
1194
1195 if (!found_lun)
1196 return -ENOENT;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001197
1198 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1199 "qla_target(%d): task abort (tag=%d)\n",
1200 vha->vp_idx, abts->exchange_addr_to_abort);
1201
1202 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1203 if (mcmd == NULL) {
1204 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1205 "qla_target(%d): %s: Allocation of ABORT cmd failed",
1206 vha->vp_idx, __func__);
1207 return -ENOMEM;
1208 }
1209 memset(mcmd, 0, sizeof(*mcmd));
1210
1211 mcmd->sess = sess;
1212 memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
Arun Easi80187f82014-09-25 06:14:53 -04001213 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001214
Steve Hodgson06e97b42012-11-16 08:06:17 -08001215 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001216 abts->exchange_addr_to_abort);
1217 if (rc != 0) {
1218 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1219 "qla_target(%d): tgt_ops->handle_tmr()"
1220 " failed: %d", vha->vp_idx, rc);
1221 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1222 return -EFAULT;
1223 }
1224
1225 return 0;
1226}
1227
1228/*
1229 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1230 */
1231static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1232 struct abts_recv_from_24xx *abts)
1233{
1234 struct qla_hw_data *ha = vha->hw;
1235 struct qla_tgt_sess *sess;
1236 uint32_t tag = abts->exchange_addr_to_abort;
1237 uint8_t s_id[3];
1238 int rc;
1239
1240 if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1241 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1242 "qla_target(%d): ABTS: Abort Sequence not "
1243 "supported\n", vha->vp_idx);
1244 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1245 return;
1246 }
1247
1248 if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1249 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1250 "qla_target(%d): ABTS: Unknown Exchange "
1251 "Address received\n", vha->vp_idx);
1252 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1253 return;
1254 }
1255
1256 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1257 "qla_target(%d): task abort (s_id=%x:%x:%x, "
1258 "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1259 abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1260 le32_to_cpu(abts->fcp_hdr_le.parameter));
1261
1262 s_id[0] = abts->fcp_hdr_le.s_id[2];
1263 s_id[1] = abts->fcp_hdr_le.s_id[1];
1264 s_id[2] = abts->fcp_hdr_le.s_id[0];
1265
1266 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1267 if (!sess) {
1268 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1269 "qla_target(%d): task abort for non-existant session\n",
1270 vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001271 rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001272 QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1273 if (rc != 0) {
1274 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1275 false);
1276 }
1277 return;
1278 }
1279
1280 rc = __qlt_24xx_handle_abts(vha, abts, sess);
1281 if (rc != 0) {
1282 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1283 "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1284 vha->vp_idx, rc);
1285 qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1286 return;
1287 }
1288}
1289
1290/*
1291 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1292 */
1293static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1294 struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1295{
1296 struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1297 struct ctio7_to_24xx *ctio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001298 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001299
1300 ql_dbg(ql_dbg_tgt, ha, 0xe008,
1301 "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1302 ha, atio, resp_code);
1303
1304 /* Send marker if required */
1305 if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1306 return;
1307
1308 ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1309 if (ctio == NULL) {
1310 ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1311 "qla_target(%d): %s failed: unable to allocate "
1312 "request packet\n", ha->vp_idx, __func__);
1313 return;
1314 }
1315
1316 ctio->entry_type = CTIO_TYPE7;
1317 ctio->entry_count = 1;
1318 ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1319 ctio->nport_handle = mcmd->sess->loop_id;
1320 ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1321 ctio->vp_index = ha->vp_idx;
1322 ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1323 ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1324 ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1325 ctio->exchange_addr = atio->u.isp24.exchange_addr;
1326 ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1327 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1328 CTIO7_FLAGS_SEND_STATUS);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001329 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1330 ctio->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001331 ctio->u.status1.scsi_status =
1332 __constant_cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1333 ctio->u.status1.response_len = __constant_cpu_to_le16(8);
Roland Dreiere4b11b82012-09-18 15:10:56 -07001334 ctio->u.status1.sense_data[0] = resp_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001335
1336 qla2x00_start_iocbs(ha, ha->req);
1337}
1338
1339void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1340{
1341 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1342}
1343EXPORT_SYMBOL(qlt_free_mcmd);
1344
1345/* callback from target fabric module code */
1346void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1347{
1348 struct scsi_qla_host *vha = mcmd->sess->vha;
1349 struct qla_hw_data *ha = vha->hw;
1350 unsigned long flags;
1351
1352 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1353 "TM response mcmd (%p) status %#x state %#x",
1354 mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1355
1356 spin_lock_irqsave(&ha->hardware_lock, flags);
Arun Easib6a029e2014-09-25 06:14:52 -04001357
1358 if (qla2x00_reset_active(vha) || mcmd->reset_count != ha->chip_reset) {
1359 /*
1360 * Either a chip reset is active or this request was from
1361 * previous life, just abort the processing.
1362 */
1363 ql_dbg(ql_dbg_async, vha, 0xe100,
1364 "RESET-TMR active/old-count/new-count = %d/%d/%d.\n",
1365 qla2x00_reset_active(vha), mcmd->reset_count,
1366 ha->chip_reset);
1367 ha->tgt.tgt_ops->free_mcmd(mcmd);
1368 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1369 return;
1370 }
1371
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001372 if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1373 qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1374 0, 0, 0, 0, 0, 0);
1375 else {
1376 if (mcmd->se_cmd.se_tmr_req->function == TMR_ABORT_TASK)
1377 qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1378 mcmd->fc_tm_rsp, false);
1379 else
1380 qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1381 mcmd->fc_tm_rsp);
1382 }
1383 /*
1384 * Make the callback for ->free_mcmd() to queue_work() and invoke
1385 * target_put_sess_cmd() to drop cmd_kref to 1. The final
1386 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1387 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1388 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1389 * qlt_xmit_tm_rsp() returns here..
1390 */
1391 ha->tgt.tgt_ops->free_mcmd(mcmd);
1392 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1393}
1394EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1395
1396/* No locks */
1397static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1398{
1399 struct qla_tgt_cmd *cmd = prm->cmd;
1400
1401 BUG_ON(cmd->sg_cnt == 0);
1402
1403 prm->sg = (struct scatterlist *)cmd->sg;
1404 prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1405 cmd->sg_cnt, cmd->dma_data_direction);
1406 if (unlikely(prm->seg_cnt == 0))
1407 goto out_err;
1408
1409 prm->cmd->sg_mapped = 1;
1410
Quinn Tranf83adb62014-04-11 16:54:43 -04001411 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
1412 /*
1413 * If greater than four sg entries then we need to allocate
1414 * the continuation entries
1415 */
1416 if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1417 prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1418 prm->tgt->datasegs_per_cmd,
1419 prm->tgt->datasegs_per_cont);
1420 } else {
1421 /* DIF */
1422 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1423 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1424 prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
1425 prm->tot_dsds = prm->seg_cnt;
1426 } else
1427 prm->tot_dsds = prm->seg_cnt;
1428
1429 if (cmd->prot_sg_cnt) {
1430 prm->prot_sg = cmd->prot_sg;
1431 prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
1432 cmd->prot_sg, cmd->prot_sg_cnt,
1433 cmd->dma_data_direction);
1434 if (unlikely(prm->prot_seg_cnt == 0))
1435 goto out_err;
1436
1437 if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1438 (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1439 /* Dif Bundling not support here */
1440 prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
1441 cmd->blk_sz);
1442 prm->tot_dsds += prm->prot_seg_cnt;
1443 } else
1444 prm->tot_dsds += prm->prot_seg_cnt;
1445 }
1446 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001447
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001448 return 0;
1449
1450out_err:
1451 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1452 "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1453 0, prm->cmd->sg_cnt);
1454 return -1;
1455}
1456
1457static inline void qlt_unmap_sg(struct scsi_qla_host *vha,
1458 struct qla_tgt_cmd *cmd)
1459{
1460 struct qla_hw_data *ha = vha->hw;
1461
1462 BUG_ON(!cmd->sg_mapped);
1463 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1464 cmd->sg_mapped = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04001465
1466 if (cmd->prot_sg_cnt)
1467 pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
1468 cmd->dma_data_direction);
1469
1470 if (cmd->ctx_dsd_alloced)
1471 qla2x00_clean_dsd_pool(ha, NULL, cmd);
1472
1473 if (cmd->ctx)
1474 dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001475}
1476
1477static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1478 uint32_t req_cnt)
1479{
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001480 uint32_t cnt, cnt_in;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001481
1482 if (vha->req->cnt < (req_cnt + 2)) {
Arun Easi75554b62014-09-25 06:14:45 -04001483 cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001484 cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001485
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001486 if (vha->req->ring_index < cnt)
1487 vha->req->cnt = cnt - vha->req->ring_index;
1488 else
1489 vha->req->cnt = vha->req->length -
1490 (vha->req->ring_index - cnt);
1491 }
1492
1493 if (unlikely(vha->req->cnt < (req_cnt + 2))) {
Arun Easi667024a2014-09-25 06:14:47 -04001494 ql_dbg(ql_dbg_io, vha, 0x305a,
Saurav Kashyapd29fb732014-09-25 06:14:49 -04001495 "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",
1496 vha->vp_idx, vha->req->ring_index,
1497 vha->req->cnt, req_cnt, cnt, cnt_in, vha->req->length);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001498 return -EAGAIN;
1499 }
1500 vha->req->cnt -= req_cnt;
1501
1502 return 0;
1503}
1504
1505/*
1506 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1507 */
1508static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1509{
1510 /* Adjust ring index. */
1511 vha->req->ring_index++;
1512 if (vha->req->ring_index == vha->req->length) {
1513 vha->req->ring_index = 0;
1514 vha->req->ring_ptr = vha->req->ring;
1515 } else {
1516 vha->req->ring_ptr++;
1517 }
1518 return (cont_entry_t *)vha->req->ring_ptr;
1519}
1520
1521/* ha->hardware_lock supposed to be held on entry */
1522static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1523{
1524 struct qla_hw_data *ha = vha->hw;
1525 uint32_t h;
1526
1527 h = ha->tgt.current_handle;
1528 /* always increment cmd handle */
1529 do {
1530 ++h;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001531 if (h > DEFAULT_OUTSTANDING_COMMANDS)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001532 h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1533 if (h == ha->tgt.current_handle) {
Arun Easi667024a2014-09-25 06:14:47 -04001534 ql_dbg(ql_dbg_io, vha, 0x305b,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001535 "qla_target(%d): Ran out of "
1536 "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1537 h = QLA_TGT_NULL_HANDLE;
1538 break;
1539 }
1540 } while ((h == QLA_TGT_NULL_HANDLE) ||
1541 (h == QLA_TGT_SKIP_HANDLE) ||
1542 (ha->tgt.cmds[h-1] != NULL));
1543
1544 if (h != QLA_TGT_NULL_HANDLE)
1545 ha->tgt.current_handle = h;
1546
1547 return h;
1548}
1549
1550/* ha->hardware_lock supposed to be held on entry */
1551static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1552 struct scsi_qla_host *vha)
1553{
1554 uint32_t h;
1555 struct ctio7_to_24xx *pkt;
1556 struct qla_hw_data *ha = vha->hw;
1557 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tran33a5fce2014-06-24 00:22:29 -04001558 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001559
1560 pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1561 prm->pkt = pkt;
1562 memset(pkt, 0, sizeof(*pkt));
1563
1564 pkt->entry_type = CTIO_TYPE7;
1565 pkt->entry_count = (uint8_t)prm->req_cnt;
1566 pkt->vp_index = vha->vp_idx;
1567
1568 h = qlt_make_handle(vha);
1569 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1570 /*
1571 * CTIO type 7 from the firmware doesn't provide a way to
1572 * know the initiator's LOOP ID, hence we can't find
1573 * the session and, so, the command.
1574 */
1575 return -EAGAIN;
1576 } else
1577 ha->tgt.cmds[h-1] = prm->cmd;
1578
1579 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1580 pkt->nport_handle = prm->cmd->loop_id;
1581 pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1582 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1583 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1584 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1585 pkt->exchange_addr = atio->u.isp24.exchange_addr;
1586 pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
Quinn Tran33a5fce2014-06-24 00:22:29 -04001587 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1588 pkt->u.status0.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001589 pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1590
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001591 return 0;
1592}
1593
1594/*
1595 * ha->hardware_lock supposed to be held on entry. We have already made sure
1596 * that there is sufficient amount of request entries to not drop it.
1597 */
1598static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1599 struct scsi_qla_host *vha)
1600{
1601 int cnt;
1602 uint32_t *dword_ptr;
1603 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1604
1605 /* Build continuation packets */
1606 while (prm->seg_cnt > 0) {
1607 cont_a64_entry_t *cont_pkt64 =
1608 (cont_a64_entry_t *)qlt_get_req_pkt(vha);
1609
1610 /*
1611 * Make sure that from cont_pkt64 none of
1612 * 64-bit specific fields used for 32-bit
1613 * addressing. Cast to (cont_entry_t *) for
1614 * that.
1615 */
1616
1617 memset(cont_pkt64, 0, sizeof(*cont_pkt64));
1618
1619 cont_pkt64->entry_count = 1;
1620 cont_pkt64->sys_define = 0;
1621
1622 if (enable_64bit_addressing) {
1623 cont_pkt64->entry_type = CONTINUE_A64_TYPE;
1624 dword_ptr =
1625 (uint32_t *)&cont_pkt64->dseg_0_address;
1626 } else {
1627 cont_pkt64->entry_type = CONTINUE_TYPE;
1628 dword_ptr =
1629 (uint32_t *)&((cont_entry_t *)
1630 cont_pkt64)->dseg_0_address;
1631 }
1632
1633 /* Load continuation entry data segments */
1634 for (cnt = 0;
1635 cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
1636 cnt++, prm->seg_cnt--) {
1637 *dword_ptr++ =
1638 cpu_to_le32(pci_dma_lo32
1639 (sg_dma_address(prm->sg)));
1640 if (enable_64bit_addressing) {
1641 *dword_ptr++ =
1642 cpu_to_le32(pci_dma_hi32
1643 (sg_dma_address
1644 (prm->sg)));
1645 }
1646 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1647
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001648 prm->sg = sg_next(prm->sg);
1649 }
1650 }
1651}
1652
1653/*
1654 * ha->hardware_lock supposed to be held on entry. We have already made sure
1655 * that there is sufficient amount of request entries to not drop it.
1656 */
1657static void qlt_load_data_segments(struct qla_tgt_prm *prm,
1658 struct scsi_qla_host *vha)
1659{
1660 int cnt;
1661 uint32_t *dword_ptr;
1662 int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1663 struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
1664
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001665 pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
1666
1667 /* Setup packet address segment pointer */
1668 dword_ptr = pkt24->u.status0.dseg_0_address;
1669
1670 /* Set total data segment count */
1671 if (prm->seg_cnt)
1672 pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
1673
1674 if (prm->seg_cnt == 0) {
1675 /* No data transfer */
1676 *dword_ptr++ = 0;
1677 *dword_ptr = 0;
1678 return;
1679 }
1680
1681 /* If scatter gather */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001682
1683 /* Load command entry data segments */
1684 for (cnt = 0;
1685 (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
1686 cnt++, prm->seg_cnt--) {
1687 *dword_ptr++ =
1688 cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
1689 if (enable_64bit_addressing) {
1690 *dword_ptr++ =
1691 cpu_to_le32(pci_dma_hi32(
1692 sg_dma_address(prm->sg)));
1693 }
1694 *dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1695
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001696 prm->sg = sg_next(prm->sg);
1697 }
1698
1699 qlt_load_cont_data_segments(prm, vha);
1700}
1701
1702static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
1703{
1704 return cmd->bufflen > 0;
1705}
1706
1707/*
1708 * Called without ha->hardware_lock held
1709 */
1710static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
1711 struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
1712 uint32_t *full_req_cnt)
1713{
1714 struct qla_tgt *tgt = cmd->tgt;
1715 struct scsi_qla_host *vha = tgt->vha;
1716 struct qla_hw_data *ha = vha->hw;
1717 struct se_cmd *se_cmd = &cmd->se_cmd;
1718
1719 if (unlikely(cmd->aborted)) {
1720 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
1721 "qla_target(%d): terminating exchange "
1722 "for aborted cmd=%p (se_cmd=%p, tag=%d)", vha->vp_idx, cmd,
1723 se_cmd, cmd->tag);
1724
1725 cmd->state = QLA_TGT_STATE_ABORTED;
Saurav Kashyape07f8f62014-09-25 06:14:58 -04001726 cmd->cmd_flags |= BIT_6;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001727
1728 qlt_send_term_exchange(vha, cmd, &cmd->atio, 0);
1729
1730 /* !! At this point cmd could be already freed !! */
1731 return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED;
1732 }
1733
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001734 prm->cmd = cmd;
1735 prm->tgt = tgt;
1736 prm->rq_result = scsi_status;
1737 prm->sense_buffer = &cmd->sense_buffer[0];
1738 prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
1739 prm->sg = NULL;
1740 prm->seg_cnt = -1;
1741 prm->req_cnt = 1;
1742 prm->add_status_pkt = 0;
1743
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001744 /* Send marker if required */
1745 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
1746 return -EFAULT;
1747
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001748 if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
1749 if (qlt_pci_map_calc_cnt(prm) != 0)
1750 return -EAGAIN;
1751 }
1752
1753 *full_req_cnt = prm->req_cnt;
1754
1755 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1756 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04001757 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001758 "Residual underflow: %d (tag %d, "
1759 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1760 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1761 cmd->bufflen, prm->rq_result);
1762 prm->rq_result |= SS_RESIDUAL_UNDER;
1763 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1764 prm->residual = se_cmd->residual_count;
Arun Easi667024a2014-09-25 06:14:47 -04001765 ql_dbg(ql_dbg_io, vha, 0x305d,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001766 "Residual overflow: %d (tag %d, "
1767 "op %x, bufflen %d, rq_result %x)\n", prm->residual,
1768 cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1769 cmd->bufflen, prm->rq_result);
1770 prm->rq_result |= SS_RESIDUAL_OVER;
1771 }
1772
1773 if (xmit_type & QLA_TGT_XMIT_STATUS) {
1774 /*
1775 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
1776 * ignored in *xmit_response() below
1777 */
1778 if (qlt_has_data(cmd)) {
1779 if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
1780 (IS_FWI2_CAPABLE(ha) &&
1781 (prm->rq_result != 0))) {
1782 prm->add_status_pkt = 1;
1783 (*full_req_cnt)++;
1784 }
1785 }
1786 }
1787
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001788 return 0;
1789}
1790
1791static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
1792 struct qla_tgt_cmd *cmd, int sending_sense)
1793{
1794 if (ha->tgt.enable_class_2)
1795 return 0;
1796
1797 if (sending_sense)
1798 return cmd->conf_compl_supported;
1799 else
1800 return ha->tgt.enable_explicit_conf &&
1801 cmd->conf_compl_supported;
1802}
1803
1804#ifdef CONFIG_QLA_TGT_DEBUG_SRR
1805/*
1806 * Original taken from the XFS code
1807 */
1808static unsigned long qlt_srr_random(void)
1809{
1810 static int Inited;
1811 static unsigned long RandomValue;
1812 static DEFINE_SPINLOCK(lock);
1813 /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
1814 register long rv;
1815 register long lo;
1816 register long hi;
1817 unsigned long flags;
1818
1819 spin_lock_irqsave(&lock, flags);
1820 if (!Inited) {
1821 RandomValue = jiffies;
1822 Inited = 1;
1823 }
1824 rv = RandomValue;
1825 hi = rv / 127773;
1826 lo = rv % 127773;
1827 rv = 16807 * lo - 2836 * hi;
1828 if (rv <= 0)
1829 rv += 2147483647;
1830 RandomValue = rv;
1831 spin_unlock_irqrestore(&lock, flags);
1832 return rv;
1833}
1834
1835static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1836{
1837#if 0 /* This is not a real status packets lost, so it won't lead to SRR */
1838 if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
1839 == 50) {
1840 *xmit_type &= ~QLA_TGT_XMIT_STATUS;
1841 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
1842 "Dropping cmd %p (tag %d) status", cmd, cmd->tag);
1843 }
1844#endif
1845 /*
1846 * It's currently not possible to simulate SRRs for FCP_WRITE without
1847 * a physical link layer failure, so don't even try here..
1848 */
1849 if (cmd->dma_data_direction != DMA_FROM_DEVICE)
1850 return;
1851
1852 if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
1853 ((qlt_srr_random() % 100) == 20)) {
1854 int i, leave = 0;
1855 unsigned int tot_len = 0;
1856
1857 while (leave == 0)
1858 leave = qlt_srr_random() % cmd->sg_cnt;
1859
1860 for (i = 0; i < leave; i++)
1861 tot_len += cmd->sg[i].length;
1862
1863 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
1864 "Cutting cmd %p (tag %d) buffer"
1865 " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
1866 " cmd->sg_cnt %d)", cmd, cmd->tag, tot_len, leave,
1867 cmd->bufflen, cmd->sg_cnt);
1868
1869 cmd->bufflen = tot_len;
1870 cmd->sg_cnt = leave;
1871 }
1872
1873 if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
1874 unsigned int offset = qlt_srr_random() % cmd->bufflen;
1875
1876 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
1877 "Cutting cmd %p (tag %d) buffer head "
1878 "to offset %d (cmd->bufflen %d)", cmd, cmd->tag, offset,
1879 cmd->bufflen);
1880 if (offset == 0)
1881 *xmit_type &= ~QLA_TGT_XMIT_DATA;
1882 else if (qlt_set_data_offset(cmd, offset)) {
1883 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
1884 "qlt_set_data_offset() failed (tag %d)", cmd->tag);
1885 }
1886 }
1887}
1888#else
1889static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1890{}
1891#endif
1892
1893static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
1894 struct qla_tgt_prm *prm)
1895{
1896 prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
1897 (uint32_t)sizeof(ctio->u.status1.sense_data));
1898 ctio->u.status0.flags |=
1899 __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
1900 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
1901 ctio->u.status0.flags |= __constant_cpu_to_le16(
1902 CTIO7_FLAGS_EXPLICIT_CONFORM |
1903 CTIO7_FLAGS_CONFORM_REQ);
1904 }
1905 ctio->u.status0.residual = cpu_to_le32(prm->residual);
1906 ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
1907 if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
1908 int i;
1909
1910 if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
1911 if (prm->cmd->se_cmd.scsi_status != 0) {
1912 ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
1913 "Skipping EXPLICIT_CONFORM and "
1914 "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
1915 "non GOOD status\n");
1916 goto skip_explict_conf;
1917 }
1918 ctio->u.status1.flags |= __constant_cpu_to_le16(
1919 CTIO7_FLAGS_EXPLICIT_CONFORM |
1920 CTIO7_FLAGS_CONFORM_REQ);
1921 }
1922skip_explict_conf:
1923 ctio->u.status1.flags &=
1924 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1925 ctio->u.status1.flags |=
1926 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1927 ctio->u.status1.scsi_status |=
1928 __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
1929 ctio->u.status1.sense_length =
1930 cpu_to_le16(prm->sense_buffer_len);
1931 for (i = 0; i < prm->sense_buffer_len/4; i++)
1932 ((uint32_t *)ctio->u.status1.sense_data)[i] =
1933 cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
1934#if 0
1935 if (unlikely((prm->sense_buffer_len % 4) != 0)) {
1936 static int q;
1937 if (q < 10) {
1938 ql_dbg(ql_dbg_tgt, vha, 0xe04f,
1939 "qla_target(%d): %d bytes of sense "
1940 "lost", prm->tgt->ha->vp_idx,
1941 prm->sense_buffer_len % 4);
1942 q++;
1943 }
1944 }
1945#endif
1946 } else {
1947 ctio->u.status1.flags &=
1948 ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1949 ctio->u.status1.flags |=
1950 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1951 ctio->u.status1.sense_length = 0;
1952 memset(ctio->u.status1.sense_data, 0,
1953 sizeof(ctio->u.status1.sense_data));
1954 }
1955
1956 /* Sense with len > 24, is it possible ??? */
1957}
1958
Quinn Tranf83adb62014-04-11 16:54:43 -04001959
1960
1961/* diff */
1962static inline int
1963qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
1964{
1965 /*
1966 * Uncomment when corresponding SCSI changes are done.
1967 *
1968 if (!sp->cmd->prot_chk)
1969 return 0;
1970 *
1971 */
1972 switch (se_cmd->prot_op) {
1973 case TARGET_PROT_DOUT_INSERT:
1974 case TARGET_PROT_DIN_STRIP:
1975 if (ql2xenablehba_err_chk >= 1)
1976 return 1;
1977 break;
1978 case TARGET_PROT_DOUT_PASS:
1979 case TARGET_PROT_DIN_PASS:
1980 if (ql2xenablehba_err_chk >= 2)
1981 return 1;
1982 break;
1983 case TARGET_PROT_DIN_INSERT:
1984 case TARGET_PROT_DOUT_STRIP:
1985 return 1;
1986 default:
1987 break;
1988 }
1989 return 0;
1990}
1991
1992/*
1993 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
1994 *
1995 */
1996static inline void
1997qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
1998{
1999 uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2000
2001 /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
2002 * have been immplemented by TCM, before AppTag is avail.
2003 * Look for modesense_handlers[]
2004 */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002005 ctx->app_tag = 0;
Quinn Tranf83adb62014-04-11 16:54:43 -04002006 ctx->app_tag_mask[0] = 0x0;
2007 ctx->app_tag_mask[1] = 0x0;
2008
2009 switch (se_cmd->prot_type) {
2010 case TARGET_DIF_TYPE0_PROT:
2011 /*
2012 * No check for ql2xenablehba_err_chk, as it would be an
2013 * I/O error if hba tag generation is not done.
2014 */
2015 ctx->ref_tag = cpu_to_le32(lba);
2016
2017 if (!qlt_hba_err_chk_enabled(se_cmd))
2018 break;
2019
2020 /* enable ALL bytes of the ref tag */
2021 ctx->ref_tag_mask[0] = 0xff;
2022 ctx->ref_tag_mask[1] = 0xff;
2023 ctx->ref_tag_mask[2] = 0xff;
2024 ctx->ref_tag_mask[3] = 0xff;
2025 break;
2026 /*
2027 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
2028 * 16 bit app tag.
2029 */
2030 case TARGET_DIF_TYPE1_PROT:
2031 ctx->ref_tag = cpu_to_le32(lba);
2032
2033 if (!qlt_hba_err_chk_enabled(se_cmd))
2034 break;
2035
2036 /* enable ALL bytes of the ref tag */
2037 ctx->ref_tag_mask[0] = 0xff;
2038 ctx->ref_tag_mask[1] = 0xff;
2039 ctx->ref_tag_mask[2] = 0xff;
2040 ctx->ref_tag_mask[3] = 0xff;
2041 break;
2042 /*
2043 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
2044 * match LBA in CDB + N
2045 */
2046 case TARGET_DIF_TYPE2_PROT:
2047 ctx->ref_tag = cpu_to_le32(lba);
2048
2049 if (!qlt_hba_err_chk_enabled(se_cmd))
2050 break;
2051
2052 /* enable ALL bytes of the ref tag */
2053 ctx->ref_tag_mask[0] = 0xff;
2054 ctx->ref_tag_mask[1] = 0xff;
2055 ctx->ref_tag_mask[2] = 0xff;
2056 ctx->ref_tag_mask[3] = 0xff;
2057 break;
2058
2059 /* For Type 3 protection: 16 bit GUARD only */
2060 case TARGET_DIF_TYPE3_PROT:
2061 ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2062 ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2063 break;
2064 }
2065}
2066
2067
2068static inline int
2069qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2070{
2071 uint32_t *cur_dsd;
2072 int sgc;
2073 uint32_t transfer_length = 0;
2074 uint32_t data_bytes;
2075 uint32_t dif_bytes;
2076 uint8_t bundling = 1;
2077 uint8_t *clr_ptr;
2078 struct crc_context *crc_ctx_pkt = NULL;
2079 struct qla_hw_data *ha;
2080 struct ctio_crc2_to_fw *pkt;
2081 dma_addr_t crc_ctx_dma;
2082 uint16_t fw_prot_opts = 0;
2083 struct qla_tgt_cmd *cmd = prm->cmd;
2084 struct se_cmd *se_cmd = &cmd->se_cmd;
2085 uint32_t h;
2086 struct atio_from_isp *atio = &prm->cmd->atio;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002087 uint16_t t16;
Quinn Tranf83adb62014-04-11 16:54:43 -04002088
2089 sgc = 0;
2090 ha = vha->hw;
2091
2092 pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2093 prm->pkt = pkt;
2094 memset(pkt, 0, sizeof(*pkt));
2095
2096 ql_dbg(ql_dbg_tgt, vha, 0xe071,
2097 "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2098 vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2099 prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2100
2101 if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2102 (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2103 bundling = 0;
2104
2105 /* Compute dif len and adjust data len to incude protection */
2106 data_bytes = cmd->bufflen;
2107 dif_bytes = (data_bytes / cmd->blk_sz) * 8;
2108
2109 switch (se_cmd->prot_op) {
2110 case TARGET_PROT_DIN_INSERT:
2111 case TARGET_PROT_DOUT_STRIP:
2112 transfer_length = data_bytes;
2113 data_bytes += dif_bytes;
2114 break;
2115
2116 case TARGET_PROT_DIN_STRIP:
2117 case TARGET_PROT_DOUT_INSERT:
2118 case TARGET_PROT_DIN_PASS:
2119 case TARGET_PROT_DOUT_PASS:
2120 transfer_length = data_bytes + dif_bytes;
2121 break;
2122
2123 default:
2124 BUG();
2125 break;
2126 }
2127
2128 if (!qlt_hba_err_chk_enabled(se_cmd))
2129 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2130 /* HBA error checking enabled */
2131 else if (IS_PI_UNINIT_CAPABLE(ha)) {
2132 if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2133 (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2134 fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2135 else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2136 fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2137 }
2138
2139 switch (se_cmd->prot_op) {
2140 case TARGET_PROT_DIN_INSERT:
2141 case TARGET_PROT_DOUT_INSERT:
2142 fw_prot_opts |= PO_MODE_DIF_INSERT;
2143 break;
2144 case TARGET_PROT_DIN_STRIP:
2145 case TARGET_PROT_DOUT_STRIP:
2146 fw_prot_opts |= PO_MODE_DIF_REMOVE;
2147 break;
2148 case TARGET_PROT_DIN_PASS:
2149 case TARGET_PROT_DOUT_PASS:
2150 fw_prot_opts |= PO_MODE_DIF_PASS;
2151 /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2152 break;
2153 default:/* Normal Request */
2154 fw_prot_opts |= PO_MODE_DIF_PASS;
2155 break;
2156 }
2157
2158
2159 /* ---- PKT ---- */
2160 /* Update entry type to indicate Command Type CRC_2 IOCB */
2161 pkt->entry_type = CTIO_CRC2;
2162 pkt->entry_count = 1;
2163 pkt->vp_index = vha->vp_idx;
2164
2165 h = qlt_make_handle(vha);
2166 if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2167 /*
2168 * CTIO type 7 from the firmware doesn't provide a way to
2169 * know the initiator's LOOP ID, hence we can't find
2170 * the session and, so, the command.
2171 */
2172 return -EAGAIN;
2173 } else
2174 ha->tgt.cmds[h-1] = prm->cmd;
2175
2176
2177 pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
2178 pkt->nport_handle = prm->cmd->loop_id;
2179 pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2180 pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2181 pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2182 pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2183 pkt->exchange_addr = atio->u.isp24.exchange_addr;
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002184
2185 /* silence compile warning */
2186 t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2187 pkt->ox_id = cpu_to_le16(t16);
2188
2189 t16 = (atio->u.isp24.attr << 9);
2190 pkt->flags |= cpu_to_le16(t16);
Quinn Tranf83adb62014-04-11 16:54:43 -04002191 pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2192
2193 /* Set transfer direction */
2194 if (cmd->dma_data_direction == DMA_TO_DEVICE)
2195 pkt->flags = __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2196 else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2197 pkt->flags = __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2198
2199
2200 pkt->dseg_count = prm->tot_dsds;
2201 /* Fibre channel byte count */
2202 pkt->transfer_length = cpu_to_le32(transfer_length);
2203
2204
2205 /* ----- CRC context -------- */
2206
2207 /* Allocate CRC context from global pool */
2208 crc_ctx_pkt = cmd->ctx =
2209 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2210
2211 if (!crc_ctx_pkt)
2212 goto crc_queuing_error;
2213
2214 /* Zero out CTX area. */
2215 clr_ptr = (uint8_t *)crc_ctx_pkt;
2216 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2217
2218 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2219 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2220
2221 /* Set handle */
2222 crc_ctx_pkt->handle = pkt->handle;
2223
2224 qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
2225
2226 pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2227 pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2228 pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2229
2230
2231 if (!bundling) {
2232 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2233 } else {
2234 /*
2235 * Configure Bundling if we need to fetch interlaving
2236 * protection PCI accesses
2237 */
2238 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2239 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2240 crc_ctx_pkt->u.bundling.dseg_count =
2241 cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2242 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2243 }
2244
2245 /* Finish the common fields of CRC pkt */
2246 crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz);
2247 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
2248 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2249 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
2250
2251
2252 /* Walks data segments */
2253 pkt->flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
2254
2255 if (!bundling && prm->prot_seg_cnt) {
2256 if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2257 prm->tot_dsds, cmd))
2258 goto crc_queuing_error;
2259 } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2260 (prm->tot_dsds - prm->prot_seg_cnt), cmd))
2261 goto crc_queuing_error;
2262
2263 if (bundling && prm->prot_seg_cnt) {
2264 /* Walks dif segments */
Quinn Tranc7ee3bd2014-06-02 07:02:16 -04002265 pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
Quinn Tranf83adb62014-04-11 16:54:43 -04002266
2267 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2268 if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2269 prm->prot_seg_cnt, cmd))
2270 goto crc_queuing_error;
2271 }
2272 return QLA_SUCCESS;
2273
2274crc_queuing_error:
2275 /* Cleanup will be performed by the caller */
2276
2277 return QLA_FUNCTION_FAILED;
2278}
2279
2280
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002281/*
2282 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2283 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2284 */
2285int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2286 uint8_t scsi_status)
2287{
2288 struct scsi_qla_host *vha = cmd->vha;
2289 struct qla_hw_data *ha = vha->hw;
2290 struct ctio7_to_24xx *pkt;
2291 struct qla_tgt_prm prm;
2292 uint32_t full_req_cnt = 0;
2293 unsigned long flags = 0;
2294 int res;
2295
2296 memset(&prm, 0, sizeof(prm));
2297 qlt_check_srr_debug(cmd, &xmit_type);
2298
2299 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
Quinn Tranf83adb62014-04-11 16:54:43 -04002300 "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
2301 (xmit_type & QLA_TGT_XMIT_STATUS) ?
2302 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
2303 &cmd->se_cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002304
2305 res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2306 &full_req_cnt);
2307 if (unlikely(res != 0)) {
2308 if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED)
2309 return 0;
2310
2311 return res;
2312 }
2313
2314 spin_lock_irqsave(&ha->hardware_lock, flags);
2315
Arun Easib6a029e2014-09-25 06:14:52 -04002316 if (qla2x00_reset_active(vha) || cmd->reset_count != ha->chip_reset) {
2317 /*
2318 * Either a chip reset is active or this request was from
2319 * previous life, just abort the processing.
2320 */
2321 cmd->state = QLA_TGT_STATE_PROCESSED;
2322 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2323 ql_dbg(ql_dbg_async, vha, 0xe101,
2324 "RESET-RSP active/old-count/new-count = %d/%d/%d.\n",
2325 qla2x00_reset_active(vha), cmd->reset_count,
2326 ha->chip_reset);
2327 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2328 return 0;
2329 }
2330
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002331 /* Does F/W have an IOCBs for this request */
2332 res = qlt_check_reserve_free_req(vha, full_req_cnt);
2333 if (unlikely(res))
2334 goto out_unmap_unlock;
2335
Quinn Tranf83adb62014-04-11 16:54:43 -04002336 if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
2337 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2338 else
2339 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002340 if (unlikely(res != 0))
2341 goto out_unmap_unlock;
2342
2343
2344 pkt = (struct ctio7_to_24xx *)prm.pkt;
2345
2346 if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2347 pkt->u.status0.flags |=
2348 __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN |
2349 CTIO7_FLAGS_STATUS_MODE_0);
2350
Quinn Tranf83adb62014-04-11 16:54:43 -04002351 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2352 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002353
2354 if (prm.add_status_pkt == 0) {
2355 if (xmit_type & QLA_TGT_XMIT_STATUS) {
2356 pkt->u.status0.scsi_status =
2357 cpu_to_le16(prm.rq_result);
2358 pkt->u.status0.residual =
2359 cpu_to_le32(prm.residual);
2360 pkt->u.status0.flags |= __constant_cpu_to_le16(
2361 CTIO7_FLAGS_SEND_STATUS);
2362 if (qlt_need_explicit_conf(ha, cmd, 0)) {
2363 pkt->u.status0.flags |=
2364 __constant_cpu_to_le16(
2365 CTIO7_FLAGS_EXPLICIT_CONFORM |
2366 CTIO7_FLAGS_CONFORM_REQ);
2367 }
2368 }
2369
2370 } else {
2371 /*
2372 * We have already made sure that there is sufficient
2373 * amount of request entries to not drop HW lock in
2374 * req_pkt().
2375 */
2376 struct ctio7_to_24xx *ctio =
2377 (struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2378
Arun Easi667024a2014-09-25 06:14:47 -04002379 ql_dbg(ql_dbg_io, vha, 0x305e,
2380 "Building additional status packet 0x%p.\n",
2381 ctio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002382
Quinn Tranf83adb62014-04-11 16:54:43 -04002383 /*
2384 * T10Dif: ctio_crc2_to_fw overlay ontop of
2385 * ctio7_to_24xx
2386 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002387 memcpy(ctio, pkt, sizeof(*ctio));
Quinn Tranf83adb62014-04-11 16:54:43 -04002388 /* reset back to CTIO7 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002389 ctio->entry_count = 1;
Quinn Tranf83adb62014-04-11 16:54:43 -04002390 ctio->entry_type = CTIO_TYPE7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002391 ctio->dseg_count = 0;
2392 ctio->u.status1.flags &= ~__constant_cpu_to_le16(
2393 CTIO7_FLAGS_DATA_IN);
2394
2395 /* Real finish is ctio_m1's finish */
2396 pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
2397 pkt->u.status0.flags |= __constant_cpu_to_le16(
2398 CTIO7_FLAGS_DONT_RET_CTIO);
Quinn Tranf83adb62014-04-11 16:54:43 -04002399
2400 /* qlt_24xx_init_ctio_to_isp will correct
2401 * all neccessary fields that's part of CTIO7.
2402 * There should be no residual of CTIO-CRC2 data.
2403 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002404 qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2405 &prm);
2406 pr_debug("Status CTIO7: %p\n", ctio);
2407 }
2408 } else
2409 qlt_24xx_init_ctio_to_isp(pkt, &prm);
2410
2411
2412 cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
Quinn Trand564a372014-09-25 06:14:57 -04002413 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002414
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002415 qla2x00_start_iocbs(vha, vha->req);
2416 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2417
2418 return 0;
2419
2420out_unmap_unlock:
2421 if (cmd->sg_mapped)
2422 qlt_unmap_sg(vha, cmd);
2423 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2424
2425 return res;
2426}
2427EXPORT_SYMBOL(qlt_xmit_response);
2428
2429int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2430{
2431 struct ctio7_to_24xx *pkt;
2432 struct scsi_qla_host *vha = cmd->vha;
2433 struct qla_hw_data *ha = vha->hw;
2434 struct qla_tgt *tgt = cmd->tgt;
2435 struct qla_tgt_prm prm;
2436 unsigned long flags;
2437 int res = 0;
2438
2439 memset(&prm, 0, sizeof(prm));
2440 prm.cmd = cmd;
2441 prm.tgt = tgt;
2442 prm.sg = NULL;
2443 prm.req_cnt = 1;
2444
2445 /* Send marker if required */
2446 if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2447 return -EIO;
2448
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002449 /* Calculate number of entries and segments required */
2450 if (qlt_pci_map_calc_cnt(&prm) != 0)
2451 return -EAGAIN;
2452
2453 spin_lock_irqsave(&ha->hardware_lock, flags);
2454
Arun Easib6a029e2014-09-25 06:14:52 -04002455 if (qla2x00_reset_active(vha) || cmd->reset_count != ha->chip_reset) {
2456 /*
2457 * Either a chip reset is active or this request was from
2458 * previous life, just abort the processing.
2459 */
2460 cmd->state = QLA_TGT_STATE_NEED_DATA;
2461 qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2462 ql_dbg(ql_dbg_async, vha, 0xe102,
2463 "RESET-XFR active/old-count/new-count = %d/%d/%d.\n",
2464 qla2x00_reset_active(vha), cmd->reset_count,
2465 ha->chip_reset);
2466 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2467 return 0;
2468 }
2469
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002470 /* Does F/W have an IOCBs for this request */
2471 res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2472 if (res != 0)
2473 goto out_unlock_free_unmap;
Quinn Tranf83adb62014-04-11 16:54:43 -04002474 if (cmd->se_cmd.prot_op)
2475 res = qlt_build_ctio_crc2_pkt(&prm, vha);
2476 else
2477 res = qlt_24xx_build_ctio_pkt(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002478
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002479 if (unlikely(res != 0))
2480 goto out_unlock_free_unmap;
2481 pkt = (struct ctio7_to_24xx *)prm.pkt;
2482 pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2483 CTIO7_FLAGS_STATUS_MODE_0);
Quinn Tranf83adb62014-04-11 16:54:43 -04002484
2485 if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2486 qlt_load_data_segments(&prm, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002487
2488 cmd->state = QLA_TGT_STATE_NEED_DATA;
Quinn Trand564a372014-09-25 06:14:57 -04002489 cmd->cmd_sent_to_fw = 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002490
2491 qla2x00_start_iocbs(vha, vha->req);
2492 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2493
2494 return res;
2495
2496out_unlock_free_unmap:
2497 if (cmd->sg_mapped)
2498 qlt_unmap_sg(vha, cmd);
2499 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2500
2501 return res;
2502}
2503EXPORT_SYMBOL(qlt_rdy_to_xfer);
2504
Quinn Tranf83adb62014-04-11 16:54:43 -04002505
2506/*
2507 * Checks the guard or meta-data for the type of error
2508 * detected by the HBA.
2509 */
2510static inline int
2511qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
2512 struct ctio_crc_from_fw *sts)
2513{
2514 uint8_t *ap = &sts->actual_dif[0];
2515 uint8_t *ep = &sts->expected_dif[0];
2516 uint32_t e_ref_tag, a_ref_tag;
2517 uint16_t e_app_tag, a_app_tag;
2518 uint16_t e_guard, a_guard;
2519 uint64_t lba = cmd->se_cmd.t_task_lba;
2520
2521 a_guard = be16_to_cpu(*(uint16_t *)(ap + 0));
2522 a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
2523 a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
2524
2525 e_guard = be16_to_cpu(*(uint16_t *)(ep + 0));
2526 e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
2527 e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
2528
2529 ql_dbg(ql_dbg_tgt, vha, 0xe075,
2530 "iocb(s) %p Returned STATUS.\n", sts);
2531
2532 ql_dbg(ql_dbg_tgt, vha, 0xf075,
Hans Wennborgfc385042014-08-05 21:43:29 -07002533 "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",
Quinn Tranf83adb62014-04-11 16:54:43 -04002534 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2535 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
2536
2537 /*
2538 * Ignore sector if:
2539 * For type 3: ref & app tag is all 'f's
2540 * For type 0,1,2: app tag is all 'f's
2541 */
2542 if ((a_app_tag == 0xffff) &&
2543 ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
2544 (a_ref_tag == 0xffffffff))) {
2545 uint32_t blocks_done;
2546
2547 /* 2TB boundary case covered automatically with this */
2548 blocks_done = e_ref_tag - (uint32_t)lba + 1;
2549 cmd->se_cmd.bad_sector = e_ref_tag;
2550 cmd->se_cmd.pi_err = 0;
2551 ql_dbg(ql_dbg_tgt, vha, 0xf074,
2552 "need to return scsi good\n");
2553
2554 /* Update protection tag */
2555 if (cmd->prot_sg_cnt) {
2556 uint32_t i, j = 0, k = 0, num_ent;
2557 struct scatterlist *sg, *sgl;
2558
2559
2560 sgl = cmd->prot_sg;
2561
2562 /* Patch the corresponding protection tags */
2563 for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
2564 num_ent = sg_dma_len(sg) / 8;
2565 if (k + num_ent < blocks_done) {
2566 k += num_ent;
2567 continue;
2568 }
2569 j = blocks_done - k - 1;
2570 k = blocks_done;
2571 break;
2572 }
2573
2574 if (k != blocks_done) {
2575 ql_log(ql_log_warn, vha, 0xf076,
2576 "unexpected tag values tag:lba=%u:%llu)\n",
2577 e_ref_tag, (unsigned long long)lba);
2578 goto out;
2579 }
2580
2581#if 0
2582 struct sd_dif_tuple *spt;
2583 /* TODO:
2584 * This section came from initiator. Is it valid here?
2585 * should ulp be override with actual val???
2586 */
2587 spt = page_address(sg_page(sg)) + sg->offset;
2588 spt += j;
2589
2590 spt->app_tag = 0xffff;
2591 if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
2592 spt->ref_tag = 0xffffffff;
2593#endif
2594 }
2595
2596 return 0;
2597 }
2598
2599 /* check guard */
2600 if (e_guard != a_guard) {
2601 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
2602 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
2603
2604 ql_log(ql_log_warn, vha, 0xe076,
2605 "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",
2606 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2607 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
2608 a_guard, e_guard, cmd);
2609 goto out;
2610 }
2611
2612 /* check ref tag */
2613 if (e_ref_tag != a_ref_tag) {
2614 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
2615 cmd->se_cmd.bad_sector = e_ref_tag;
2616
2617 ql_log(ql_log_warn, vha, 0xe077,
2618 "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",
2619 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2620 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
2621 a_guard, e_guard, cmd);
2622 goto out;
2623 }
2624
2625 /* check appl tag */
2626 if (e_app_tag != a_app_tag) {
2627 cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
2628 cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
2629
2630 ql_log(ql_log_warn, vha, 0xe078,
2631 "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",
2632 cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2633 a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
2634 a_guard, e_guard, cmd);
2635 goto out;
2636 }
2637out:
2638 return 1;
2639}
2640
2641
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002642/* If hardware_lock held on entry, might drop it, then reaquire */
2643/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
2644static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
2645 struct qla_tgt_cmd *cmd,
2646 struct atio_from_isp *atio)
2647{
2648 struct ctio7_to_24xx *ctio24;
2649 struct qla_hw_data *ha = vha->hw;
2650 request_t *pkt;
2651 int ret = 0;
Quinn Tran33a5fce2014-06-24 00:22:29 -04002652 uint16_t temp;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002653
2654 ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
2655
Arun Easib6a029e2014-09-25 06:14:52 -04002656 pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002657 if (pkt == NULL) {
2658 ql_dbg(ql_dbg_tgt, vha, 0xe050,
2659 "qla_target(%d): %s failed: unable to allocate "
2660 "request packet\n", vha->vp_idx, __func__);
2661 return -ENOMEM;
2662 }
2663
2664 if (cmd != NULL) {
2665 if (cmd->state < QLA_TGT_STATE_PROCESSED) {
2666 ql_dbg(ql_dbg_tgt, vha, 0xe051,
2667 "qla_target(%d): Terminating cmd %p with "
2668 "incorrect state %d\n", vha->vp_idx, cmd,
2669 cmd->state);
2670 } else
2671 ret = 1;
2672 }
2673
2674 pkt->entry_count = 1;
2675 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2676
2677 ctio24 = (struct ctio7_to_24xx *)pkt;
2678 ctio24->entry_type = CTIO_TYPE7;
2679 ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED;
2680 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2681 ctio24->vp_index = vha->vp_idx;
2682 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2683 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2684 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2685 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
2686 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
2687 __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
2688 CTIO7_FLAGS_TERMINATE);
Quinn Tran33a5fce2014-06-24 00:22:29 -04002689 temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2690 ctio24->u.status1.ox_id = cpu_to_le16(temp);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002691
2692 /* Most likely, it isn't needed */
2693 ctio24->u.status1.residual = get_unaligned((uint32_t *)
2694 &atio->u.isp24.fcp_cmnd.add_cdb[
2695 atio->u.isp24.fcp_cmnd.add_cdb_len]);
2696 if (ctio24->u.status1.residual != 0)
2697 ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
2698
2699 qla2x00_start_iocbs(vha, vha->req);
2700 return ret;
2701}
2702
2703static void qlt_send_term_exchange(struct scsi_qla_host *vha,
2704 struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked)
2705{
2706 unsigned long flags;
2707 int rc;
2708
2709 if (qlt_issue_marker(vha, ha_locked) < 0)
2710 return;
2711
2712 if (ha_locked) {
2713 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04002714 if (rc == -ENOMEM)
2715 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002716 goto done;
2717 }
2718 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2719 rc = __qlt_send_term_exchange(vha, cmd, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04002720 if (rc == -ENOMEM)
2721 qlt_alloc_qfull_cmd(vha, atio, 0, 0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002722 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Quinn Trand564a372014-09-25 06:14:57 -04002723
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002724done:
Quinn Trand564a372014-09-25 06:14:57 -04002725 if (cmd && ((cmd->state != QLA_TGT_STATE_ABORTED) ||
2726 !cmd->cmd_sent_to_fw)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002727 if (!ha_locked && !in_interrupt())
2728 msleep(250); /* just in case */
2729
Quinn Tranf83adb62014-04-11 16:54:43 -04002730 if (cmd->sg_mapped)
2731 qlt_unmap_sg(vha, cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002732 vha->hw->tgt.tgt_ops->free_cmd(cmd);
2733 }
Quinn Tran7b898542014-04-11 16:54:44 -04002734 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002735}
2736
Quinn Tran33e79972014-09-25 06:14:55 -04002737static void qlt_init_term_exchange(struct scsi_qla_host *vha)
2738{
2739 struct list_head free_list;
2740 struct qla_tgt_cmd *cmd, *tcmd;
2741
2742 vha->hw->tgt.leak_exchg_thresh_hold =
2743 (vha->hw->fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
2744
2745 cmd = tcmd = NULL;
2746 if (!list_empty(&vha->hw->tgt.q_full_list)) {
2747 INIT_LIST_HEAD(&free_list);
2748 list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
2749
2750 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
2751 list_del(&cmd->cmd_list);
2752 /* This cmd was never sent to TCM. There is no need
2753 * to schedule free or call free_cmd
2754 */
2755 qlt_free_cmd(cmd);
2756 vha->hw->tgt.num_qfull_cmds_alloc--;
2757 }
2758 }
2759 vha->hw->tgt.num_qfull_cmds_dropped = 0;
2760}
2761
2762static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
2763{
2764 uint32_t total_leaked;
2765
2766 total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
2767
2768 if (vha->hw->tgt.leak_exchg_thresh_hold &&
2769 (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
2770
2771 ql_dbg(ql_dbg_tgt, vha, 0xe079,
2772 "Chip reset due to exchange starvation: %d/%d.\n",
2773 total_leaked, vha->hw->fw_xcb_count);
2774
2775 if (IS_P3P_TYPE(vha->hw))
2776 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
2777 else
2778 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2779 qla2xxx_wake_dpc(vha);
2780 }
2781
2782}
2783
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002784void qlt_free_cmd(struct qla_tgt_cmd *cmd)
2785{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002786 struct qla_tgt_sess *sess = cmd->sess;
2787
Quinn Tranf83adb62014-04-11 16:54:43 -04002788 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
2789 "%s: se_cmd[%p] ox_id %04x\n",
2790 __func__, &cmd->se_cmd,
2791 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002792
Saurav Kashyape07f8f62014-09-25 06:14:58 -04002793 BUG_ON(cmd->cmd_in_wq);
2794
Quinn Tran33e79972014-09-25 06:14:55 -04002795 if (!cmd->q_full)
2796 qlt_decr_num_pend_cmds(cmd->vha);
2797
Quinn Tranf83adb62014-04-11 16:54:43 -04002798 BUG_ON(cmd->sg_mapped);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04002799 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002800 if (unlikely(cmd->free_sg))
2801 kfree(cmd->sg);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002802
2803 if (!sess || !sess->se_sess) {
2804 WARN_ON(1);
2805 return;
2806 }
Saurav Kashyape07f8f62014-09-25 06:14:58 -04002807 cmd->jiffies_at_free = get_jiffies_64();
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07002808 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002809}
2810EXPORT_SYMBOL(qlt_free_cmd);
2811
2812/* ha->hardware_lock supposed to be held on entry */
2813static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
2814 struct qla_tgt_cmd *cmd, void *ctio)
2815{
2816 struct qla_tgt_srr_ctio *sc;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002817 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002818 struct qla_tgt_srr_imm *imm;
2819
2820 tgt->ctio_srr_id++;
Saurav Kashyape07f8f62014-09-25 06:14:58 -04002821 cmd->cmd_flags |= BIT_15;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002822
2823 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
2824 "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
2825
2826 if (!ctio) {
2827 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
2828 "qla_target(%d): SRR CTIO, but ctio is NULL\n",
2829 vha->vp_idx);
2830 return -EINVAL;
2831 }
2832
2833 sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
2834 if (sc != NULL) {
2835 sc->cmd = cmd;
2836 /* IRQ is already OFF */
2837 spin_lock(&tgt->srr_lock);
2838 sc->srr_id = tgt->ctio_srr_id;
2839 list_add_tail(&sc->srr_list_entry,
2840 &tgt->srr_ctio_list);
2841 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
2842 "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
2843 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
2844 int found = 0;
2845 list_for_each_entry(imm, &tgt->srr_imm_list,
2846 srr_list_entry) {
2847 if (imm->srr_id == sc->srr_id) {
2848 found = 1;
2849 break;
2850 }
2851 }
2852 if (found) {
2853 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
2854 "Scheduling srr work\n");
2855 schedule_work(&tgt->srr_work);
2856 } else {
2857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
2858 "qla_target(%d): imm_srr_id "
2859 "== ctio_srr_id (%d), but there is no "
2860 "corresponding SRR IMM, deleting CTIO "
2861 "SRR %p\n", vha->vp_idx,
2862 tgt->ctio_srr_id, sc);
2863 list_del(&sc->srr_list_entry);
2864 spin_unlock(&tgt->srr_lock);
2865
2866 kfree(sc);
2867 return -EINVAL;
2868 }
2869 }
2870 spin_unlock(&tgt->srr_lock);
2871 } else {
2872 struct qla_tgt_srr_imm *ti;
2873
2874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
2875 "qla_target(%d): Unable to allocate SRR CTIO entry\n",
2876 vha->vp_idx);
2877 spin_lock(&tgt->srr_lock);
2878 list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
2879 srr_list_entry) {
2880 if (imm->srr_id == tgt->ctio_srr_id) {
2881 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
2882 "IMM SRR %p deleted (id %d)\n",
2883 imm, imm->srr_id);
2884 list_del(&imm->srr_list_entry);
2885 qlt_reject_free_srr_imm(vha, imm, 1);
2886 }
2887 }
2888 spin_unlock(&tgt->srr_lock);
2889
2890 return -ENOMEM;
2891 }
2892
2893 return 0;
2894}
2895
2896/*
2897 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2898 */
2899static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
2900 struct qla_tgt_cmd *cmd, uint32_t status)
2901{
2902 int term = 0;
2903
2904 if (ctio != NULL) {
2905 struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
2906 term = !(c->flags &
2907 __constant_cpu_to_le16(OF_TERM_EXCH));
2908 } else
2909 term = 1;
2910
2911 if (term)
2912 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
2913
2914 return term;
2915}
2916
2917/* ha->hardware_lock supposed to be held on entry */
2918static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
2919 uint32_t handle)
2920{
2921 struct qla_hw_data *ha = vha->hw;
2922
2923 handle--;
2924 if (ha->tgt.cmds[handle] != NULL) {
2925 struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
2926 ha->tgt.cmds[handle] = NULL;
2927 return cmd;
2928 } else
2929 return NULL;
2930}
2931
2932/* ha->hardware_lock supposed to be held on entry */
2933static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
2934 uint32_t handle, void *ctio)
2935{
2936 struct qla_tgt_cmd *cmd = NULL;
2937
2938 /* Clear out internal marks */
2939 handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
2940 CTIO_INTERMEDIATE_HANDLE_MARK);
2941
2942 if (handle != QLA_TGT_NULL_HANDLE) {
Arun Easi667024a2014-09-25 06:14:47 -04002943 if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002944 return NULL;
Arun Easi667024a2014-09-25 06:14:47 -04002945
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002946 /* handle-1 is actually used */
Chad Dupuis8d93f552013-01-30 03:34:37 -05002947 if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002948 ql_dbg(ql_dbg_tgt, vha, 0xe052,
2949 "qla_target(%d): Wrong handle %x received\n",
2950 vha->vp_idx, handle);
2951 return NULL;
2952 }
2953 cmd = qlt_get_cmd(vha, handle);
2954 if (unlikely(cmd == NULL)) {
2955 ql_dbg(ql_dbg_tgt, vha, 0xe053,
2956 "qla_target(%d): Suspicious: unable to "
2957 "find the command with handle %x\n", vha->vp_idx,
2958 handle);
2959 return NULL;
2960 }
2961 } else if (ctio != NULL) {
2962 /* We can't get loop ID from CTIO7 */
2963 ql_dbg(ql_dbg_tgt, vha, 0xe054,
2964 "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
2965 "support NULL handles\n", vha->vp_idx);
2966 return NULL;
2967 }
2968
2969 return cmd;
2970}
2971
Arun Easic0cb4492014-09-25 06:14:51 -04002972/* hardware_lock should be held by caller. */
2973static void
2974qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2975{
2976 struct qla_hw_data *ha = vha->hw;
2977 uint32_t handle;
2978
2979 if (cmd->sg_mapped)
2980 qlt_unmap_sg(vha, cmd);
2981
2982 handle = qlt_make_handle(vha);
2983
2984 /* TODO: fix debug message type and ids. */
2985 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
2986 ql_dbg(ql_dbg_io, vha, 0xff00,
2987 "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
2988 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
2989 cmd->write_data_transferred = 0;
2990 cmd->state = QLA_TGT_STATE_DATA_IN;
2991
2992 ql_dbg(ql_dbg_io, vha, 0xff01,
2993 "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
2994
2995 ha->tgt.tgt_ops->handle_data(cmd);
2996 return;
2997 } else if (cmd->state == QLA_TGT_STATE_ABORTED) {
2998 ql_dbg(ql_dbg_io, vha, 0xff02,
2999 "HOST-ABORT: handle=%d, state=ABORTED.\n", handle);
3000 } else {
3001 ql_dbg(ql_dbg_io, vha, 0xff03,
3002 "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3003 cmd->state);
3004 dump_stack();
3005 }
3006
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003007 cmd->cmd_flags |= BIT_12;
Arun Easic0cb4492014-09-25 06:14:51 -04003008 ha->tgt.tgt_ops->free_cmd(cmd);
3009}
3010
3011void
3012qlt_host_reset_handler(struct qla_hw_data *ha)
3013{
3014 struct qla_tgt_cmd *cmd;
3015 unsigned long flags;
3016 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3017 scsi_qla_host_t *vha = NULL;
3018 struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3019 uint32_t i;
3020
3021 if (!base_vha->hw->tgt.tgt_ops)
3022 return;
3023
3024 if (!tgt || qla_ini_mode_enabled(base_vha)) {
3025 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3026 "Target mode disabled\n");
3027 return;
3028 }
3029
3030 ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3031 "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3032 base_vha->dpc_flags);
3033
3034 spin_lock_irqsave(&ha->hardware_lock, flags);
3035 for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3036 cmd = qlt_get_cmd(base_vha, i);
3037 if (!cmd)
3038 continue;
3039 /* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3040 vha = cmd->vha;
3041 qlt_abort_cmd_on_host_reset(vha, cmd);
3042 }
3043 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3044}
3045
3046
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003047/*
3048 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3049 */
3050static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3051 uint32_t status, void *ctio)
3052{
3053 struct qla_hw_data *ha = vha->hw;
3054 struct se_cmd *se_cmd;
3055 struct target_core_fabric_ops *tfo;
3056 struct qla_tgt_cmd *cmd;
3057
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003058 if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3059 /* That could happen only in case of an error/reset/abort */
3060 if (status != CTIO_SUCCESS) {
3061 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3062 "Intermediate CTIO received"
3063 " (status %x)\n", status);
3064 }
3065 return;
3066 }
3067
3068 cmd = qlt_ctio_to_cmd(vha, handle, ctio);
Roland Dreier092e1dc2012-06-11 18:23:15 -07003069 if (cmd == NULL)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003070 return;
Roland Dreier092e1dc2012-06-11 18:23:15 -07003071
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003072 se_cmd = &cmd->se_cmd;
3073 tfo = se_cmd->se_tfo;
Quinn Trand564a372014-09-25 06:14:57 -04003074 cmd->cmd_sent_to_fw = 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003075
3076 if (cmd->sg_mapped)
3077 qlt_unmap_sg(vha, cmd);
3078
3079 if (unlikely(status != CTIO_SUCCESS)) {
3080 switch (status & 0xFFFF) {
3081 case CTIO_LIP_RESET:
3082 case CTIO_TARGET_RESET:
3083 case CTIO_ABORTED:
Quinn Tran7b898542014-04-11 16:54:44 -04003084 /* driver request abort via Terminate exchange */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003085 case CTIO_TIMEOUT:
3086 case CTIO_INVALID_RX_ID:
3087 /* They are OK */
3088 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3089 "qla_target(%d): CTIO with "
3090 "status %#x received, state %x, se_cmd %p, "
3091 "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3092 "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3093 status, cmd->state, se_cmd);
3094 break;
3095
3096 case CTIO_PORT_LOGGED_OUT:
3097 case CTIO_PORT_UNAVAILABLE:
3098 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3099 "qla_target(%d): CTIO with PORT LOGGED "
3100 "OUT (29) or PORT UNAVAILABLE (28) status %x "
3101 "received (state %x, se_cmd %p)\n", vha->vp_idx,
3102 status, cmd->state, se_cmd);
3103 break;
3104
3105 case CTIO_SRR_RECEIVED:
3106 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
3107 "qla_target(%d): CTIO with SRR_RECEIVED"
3108 " status %x received (state %x, se_cmd %p)\n",
3109 vha->vp_idx, status, cmd->state, se_cmd);
3110 if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
3111 break;
3112 else
3113 return;
3114
Quinn Tranf83adb62014-04-11 16:54:43 -04003115 case CTIO_DIF_ERROR: {
3116 struct ctio_crc_from_fw *crc =
3117 (struct ctio_crc_from_fw *)ctio;
3118 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3119 "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
3120 vha->vp_idx, status, cmd->state, se_cmd,
3121 *((u64 *)&crc->actual_dif[0]),
3122 *((u64 *)&crc->expected_dif[0]));
3123
3124 if (qlt_handle_dif_error(vha, cmd, ctio)) {
3125 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3126 /* scsi Write/xfer rdy complete */
3127 goto skip_term;
3128 } else {
3129 /* scsi read/xmit respond complete
3130 * call handle dif to send scsi status
3131 * rather than terminate exchange.
3132 */
3133 cmd->state = QLA_TGT_STATE_PROCESSED;
3134 ha->tgt.tgt_ops->handle_dif_err(cmd);
3135 return;
3136 }
3137 } else {
3138 /* Need to generate a SCSI good completion.
3139 * because FW did not send scsi status.
3140 */
3141 status = 0;
3142 goto skip_term;
3143 }
3144 break;
3145 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003146 default:
3147 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
Quinn Tranf83adb62014-04-11 16:54:43 -04003148 "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003149 vha->vp_idx, status, cmd->state, se_cmd);
3150 break;
3151 }
3152
Quinn Tran7b898542014-04-11 16:54:44 -04003153
3154 /* "cmd->state == QLA_TGT_STATE_ABORTED" means
3155 * cmd is already aborted/terminated, we don't
3156 * need to terminate again. The exchange is already
3157 * cleaned up/freed at FW level. Just cleanup at driver
3158 * level.
3159 */
3160 if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
Quinn Tran33e79972014-09-25 06:14:55 -04003161 (cmd->state != QLA_TGT_STATE_ABORTED)) {
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003162 cmd->cmd_flags |= BIT_13;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003163 if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3164 return;
Quinn Tran7b898542014-04-11 16:54:44 -04003165 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003166 }
Quinn Tranf83adb62014-04-11 16:54:43 -04003167skip_term:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003168
3169 if (cmd->state == QLA_TGT_STATE_PROCESSED) {
Arun Easi667024a2014-09-25 06:14:47 -04003170 ;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003171 } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3172 int rx_status = 0;
3173
3174 cmd->state = QLA_TGT_STATE_DATA_IN;
3175
3176 if (unlikely(status != CTIO_SUCCESS))
3177 rx_status = -EIO;
3178 else
3179 cmd->write_data_transferred = 1;
3180
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003181 ha->tgt.tgt_ops->handle_data(cmd);
3182 return;
3183 } else if (cmd->state == QLA_TGT_STATE_ABORTED) {
3184 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3185 "Aborted command %p (tag %d) finished\n", cmd, cmd->tag);
3186 } else {
3187 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3188 "qla_target(%d): A command in state (%d) should "
3189 "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3190 }
3191
Quinn Tran7b898542014-04-11 16:54:44 -04003192 if (unlikely(status != CTIO_SUCCESS) &&
3193 (cmd->state != QLA_TGT_STATE_ABORTED)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003194 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3195 dump_stack();
3196 }
3197
Quinn Tran33e79972014-09-25 06:14:55 -04003198
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003199 ha->tgt.tgt_ops->free_cmd(cmd);
3200}
3201
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003202static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3203 uint8_t task_codes)
3204{
3205 int fcp_task_attr;
3206
3207 switch (task_codes) {
3208 case ATIO_SIMPLE_QUEUE:
3209 fcp_task_attr = MSG_SIMPLE_TAG;
3210 break;
3211 case ATIO_HEAD_OF_QUEUE:
3212 fcp_task_attr = MSG_HEAD_TAG;
3213 break;
3214 case ATIO_ORDERED_QUEUE:
3215 fcp_task_attr = MSG_ORDERED_TAG;
3216 break;
3217 case ATIO_ACA_QUEUE:
3218 fcp_task_attr = MSG_ACA_TAG;
3219 break;
3220 case ATIO_UNTAGGED:
3221 fcp_task_attr = MSG_SIMPLE_TAG;
3222 break;
3223 default:
3224 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3225 "qla_target: unknown task code %x, use ORDERED instead\n",
3226 task_codes);
3227 fcp_task_attr = MSG_ORDERED_TAG;
3228 break;
3229 }
3230
3231 return fcp_task_attr;
3232}
3233
3234static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
3235 uint8_t *);
3236/*
3237 * Process context for I/O path into tcm_qla2xxx code
3238 */
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003239static void __qlt_do_work(struct qla_tgt_cmd *cmd)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003240{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003241 scsi_qla_host_t *vha = cmd->vha;
3242 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003243 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003244 struct qla_tgt_sess *sess = cmd->sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003245 struct atio_from_isp *atio = &cmd->atio;
3246 unsigned char *cdb;
3247 unsigned long flags;
3248 uint32_t data_length;
3249 int ret, fcp_task_attr, data_dir, bidi = 0;
3250
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003251 cmd->cmd_in_wq = 0;
3252 cmd->cmd_flags |= BIT_1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003253 if (tgt->tgt_stop)
3254 goto out_term;
3255
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003256 cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
3257 cmd->tag = atio->u.isp24.exchange_addr;
3258 cmd->unpacked_lun = scsilun_to_int(
3259 (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3260
3261 if (atio->u.isp24.fcp_cmnd.rddata &&
3262 atio->u.isp24.fcp_cmnd.wrdata) {
3263 bidi = 1;
3264 data_dir = DMA_TO_DEVICE;
3265 } else if (atio->u.isp24.fcp_cmnd.rddata)
3266 data_dir = DMA_FROM_DEVICE;
3267 else if (atio->u.isp24.fcp_cmnd.wrdata)
3268 data_dir = DMA_TO_DEVICE;
3269 else
3270 data_dir = DMA_NONE;
3271
3272 fcp_task_attr = qlt_get_fcp_task_attr(vha,
3273 atio->u.isp24.fcp_cmnd.task_attr);
3274 data_length = be32_to_cpu(get_unaligned((uint32_t *)
3275 &atio->u.isp24.fcp_cmnd.add_cdb[
3276 atio->u.isp24.fcp_cmnd.add_cdb_len]));
3277
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003278 ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3279 fcp_task_attr, data_dir, bidi);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003280 if (ret != 0)
3281 goto out_term;
3282 /*
3283 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3284 */
Jörn Engel08234e32013-06-12 16:27:54 -04003285 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003286 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04003287 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003288 return;
3289
3290out_term:
Arun Easi667024a2014-09-25 06:14:47 -04003291 ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003292 /*
Roland Dreierfae9eaf2012-06-11 18:23:16 -07003293 * cmd has not sent to target yet, so pass NULL as the second
3294 * argument to qlt_send_term_exchange() and free the memory here.
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003295 */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003296 cmd->cmd_flags |= BIT_2;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003297 spin_lock_irqsave(&ha->hardware_lock, flags);
3298 qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
Quinn Tran33e79972014-09-25 06:14:55 -04003299
3300 qlt_decr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003301 percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3302 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04003303 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003304}
3305
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003306static void qlt_do_work(struct work_struct *work)
3307{
3308 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
3309
3310 __qlt_do_work(cmd);
3311}
3312
3313static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
3314 struct qla_tgt_sess *sess,
3315 struct atio_from_isp *atio)
3316{
3317 struct se_session *se_sess = sess->se_sess;
3318 struct qla_tgt_cmd *cmd;
3319 int tag;
3320
3321 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
3322 if (tag < 0)
3323 return NULL;
3324
3325 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
3326 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
3327
3328 memcpy(&cmd->atio, atio, sizeof(*atio));
3329 cmd->state = QLA_TGT_STATE_NEW;
3330 cmd->tgt = vha->vha_tgt.qla_tgt;
Quinn Tran33e79972014-09-25 06:14:55 -04003331 qlt_incr_num_pend_cmds(vha);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003332 cmd->vha = vha;
3333 cmd->se_cmd.map_tag = tag;
3334 cmd->sess = sess;
3335 cmd->loop_id = sess->loop_id;
3336 cmd->conf_compl_supported = sess->conf_compl_supported;
3337
3338 return cmd;
3339}
3340
3341static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
3342 uint16_t);
3343
3344static void qlt_create_sess_from_atio(struct work_struct *work)
3345{
3346 struct qla_tgt_sess_op *op = container_of(work,
3347 struct qla_tgt_sess_op, work);
3348 scsi_qla_host_t *vha = op->vha;
3349 struct qla_hw_data *ha = vha->hw;
3350 struct qla_tgt_sess *sess;
3351 struct qla_tgt_cmd *cmd;
3352 unsigned long flags;
3353 uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
3354
3355 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
3356 "qla_target(%d): Unable to find wwn login"
3357 " (s_id %x:%x:%x), trying to create it manually\n",
3358 vha->vp_idx, s_id[0], s_id[1], s_id[2]);
3359
3360 if (op->atio.u.raw.entry_count > 1) {
3361 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
3362 "Dropping multy entry atio %p\n", &op->atio);
3363 goto out_term;
3364 }
3365
3366 mutex_lock(&vha->vha_tgt.tgt_mutex);
3367 sess = qlt_make_local_sess(vha, s_id);
3368 /* sess has an extra creation ref. */
3369 mutex_unlock(&vha->vha_tgt.tgt_mutex);
3370
3371 if (!sess)
3372 goto out_term;
3373 /*
3374 * Now obtain a pre-allocated session tag using the original op->atio
3375 * packet header, and dispatch into __qlt_do_work() using the existing
3376 * process context.
3377 */
3378 cmd = qlt_get_tag(vha, sess, &op->atio);
3379 if (!cmd) {
3380 spin_lock_irqsave(&ha->hardware_lock, flags);
3381 qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
3382 ha->tgt.tgt_ops->put_sess(sess);
3383 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3384 kfree(op);
3385 return;
3386 }
3387 /*
3388 * __qlt_do_work() will call ha->tgt.tgt_ops->put_sess() to release
3389 * the extra reference taken above by qlt_make_local_sess()
3390 */
3391 __qlt_do_work(cmd);
3392 kfree(op);
3393 return;
3394
3395out_term:
3396 spin_lock_irqsave(&ha->hardware_lock, flags);
3397 qlt_send_term_exchange(vha, NULL, &op->atio, 1);
3398 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3399 kfree(op);
3400
3401}
3402
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003403/* ha->hardware_lock supposed to be held on entry */
3404static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
3405 struct atio_from_isp *atio)
3406{
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003407 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003408 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003409 struct qla_tgt_sess *sess;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003410 struct qla_tgt_cmd *cmd;
3411
3412 if (unlikely(tgt->tgt_stop)) {
Arun Easi667024a2014-09-25 06:14:47 -04003413 ql_dbg(ql_dbg_io, vha, 0x3061,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003414 "New command while device %p is shutting down\n", tgt);
3415 return -EFAULT;
3416 }
3417
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003418 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
3419 if (unlikely(!sess)) {
3420 struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
3421 GFP_ATOMIC);
3422 if (!op)
3423 return -ENOMEM;
3424
3425 memcpy(&op->atio, atio, sizeof(*atio));
Himanshu Madhani78c21062014-09-25 06:14:44 -04003426 op->vha = vha;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003427 INIT_WORK(&op->work, qlt_create_sess_from_atio);
3428 queue_work(qla_tgt_wq, &op->work);
3429 return 0;
3430 }
3431 /*
3432 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
3433 */
3434 kref_get(&sess->se_sess->sess_kref);
3435
3436 cmd = qlt_get_tag(vha, sess, atio);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003437 if (!cmd) {
Arun Easi667024a2014-09-25 06:14:47 -04003438 ql_dbg(ql_dbg_io, vha, 0x3062,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003439 "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07003440 ha->tgt.tgt_ops->put_sess(sess);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003441 return -ENOMEM;
3442 }
3443
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003444 cmd->cmd_flags = 0;
3445 cmd->jiffies_at_alloc = get_jiffies_64();
3446
Arun Easib6a029e2014-09-25 06:14:52 -04003447 cmd->reset_count = vha->hw->chip_reset;
3448
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003449 cmd->cmd_in_wq = 1;
3450 cmd->cmd_flags |= BIT_0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003451 INIT_WORK(&cmd->work, qlt_do_work);
3452 queue_work(qla_tgt_wq, &cmd->work);
3453 return 0;
3454
3455}
3456
3457/* ha->hardware_lock supposed to be held on entry */
3458static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
3459 int fn, void *iocb, int flags)
3460{
3461 struct scsi_qla_host *vha = sess->vha;
3462 struct qla_hw_data *ha = vha->hw;
3463 struct qla_tgt_mgmt_cmd *mcmd;
3464 int res;
3465 uint8_t tmr_func;
3466
3467 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
3468 if (!mcmd) {
3469 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
3470 "qla_target(%d): Allocation of management "
3471 "command failed, some commands and their data could "
3472 "leak\n", vha->vp_idx);
3473 return -ENOMEM;
3474 }
3475 memset(mcmd, 0, sizeof(*mcmd));
3476 mcmd->sess = sess;
3477
3478 if (iocb) {
3479 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
3480 sizeof(mcmd->orig_iocb.imm_ntfy));
3481 }
3482 mcmd->tmr_func = fn;
3483 mcmd->flags = flags;
Arun Easib6a029e2014-09-25 06:14:52 -04003484 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003485
3486 switch (fn) {
3487 case QLA_TGT_CLEAR_ACA:
3488 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
3489 "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
3490 tmr_func = TMR_CLEAR_ACA;
3491 break;
3492
3493 case QLA_TGT_TARGET_RESET:
3494 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
3495 "qla_target(%d): TARGET_RESET received\n",
3496 sess->vha->vp_idx);
3497 tmr_func = TMR_TARGET_WARM_RESET;
3498 break;
3499
3500 case QLA_TGT_LUN_RESET:
3501 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
3502 "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
3503 tmr_func = TMR_LUN_RESET;
3504 break;
3505
3506 case QLA_TGT_CLEAR_TS:
3507 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
3508 "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
3509 tmr_func = TMR_CLEAR_TASK_SET;
3510 break;
3511
3512 case QLA_TGT_ABORT_TS:
3513 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
3514 "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
3515 tmr_func = TMR_ABORT_TASK_SET;
3516 break;
3517#if 0
3518 case QLA_TGT_ABORT_ALL:
3519 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
3520 "qla_target(%d): Doing ABORT_ALL_TASKS\n",
3521 sess->vha->vp_idx);
3522 tmr_func = 0;
3523 break;
3524
3525 case QLA_TGT_ABORT_ALL_SESS:
3526 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
3527 "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
3528 sess->vha->vp_idx);
3529 tmr_func = 0;
3530 break;
3531
3532 case QLA_TGT_NEXUS_LOSS_SESS:
3533 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
3534 "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
3535 sess->vha->vp_idx);
3536 tmr_func = 0;
3537 break;
3538
3539 case QLA_TGT_NEXUS_LOSS:
3540 ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
3541 "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
3542 tmr_func = 0;
3543 break;
3544#endif
3545 default:
3546 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
3547 "qla_target(%d): Unknown task mgmt fn 0x%x\n",
3548 sess->vha->vp_idx, fn);
3549 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
3550 return -ENOSYS;
3551 }
3552
3553 res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
3554 if (res != 0) {
3555 ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
3556 "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
3557 sess->vha->vp_idx, res);
3558 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
3559 return -EFAULT;
3560 }
3561
3562 return 0;
3563}
3564
3565/* ha->hardware_lock supposed to be held on entry */
3566static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
3567{
3568 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
3569 struct qla_hw_data *ha = vha->hw;
3570 struct qla_tgt *tgt;
3571 struct qla_tgt_sess *sess;
3572 uint32_t lun, unpacked_lun;
3573 int lun_size, fn;
3574
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003575 tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003576
3577 lun = a->u.isp24.fcp_cmnd.lun;
3578 lun_size = sizeof(a->u.isp24.fcp_cmnd.lun);
3579 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
3580 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3581 a->u.isp24.fcp_hdr.s_id);
3582 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
3583
3584 if (!sess) {
3585 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
3586 "qla_target(%d): task mgmt fn 0x%x for "
3587 "non-existant session\n", vha->vp_idx, fn);
3588 return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
3589 sizeof(struct atio_from_isp));
3590 }
3591
3592 return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
3593}
3594
3595/* ha->hardware_lock supposed to be held on entry */
3596static int __qlt_abort_task(struct scsi_qla_host *vha,
3597 struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
3598{
3599 struct atio_from_isp *a = (struct atio_from_isp *)iocb;
3600 struct qla_hw_data *ha = vha->hw;
3601 struct qla_tgt_mgmt_cmd *mcmd;
3602 uint32_t lun, unpacked_lun;
3603 int rc;
3604
3605 mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
3606 if (mcmd == NULL) {
3607 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
3608 "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
3609 vha->vp_idx, __func__);
3610 return -ENOMEM;
3611 }
3612 memset(mcmd, 0, sizeof(*mcmd));
3613
3614 mcmd->sess = sess;
3615 memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
3616 sizeof(mcmd->orig_iocb.imm_ntfy));
3617
3618 lun = a->u.isp24.fcp_cmnd.lun;
3619 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
Arun Easi80187f82014-09-25 06:14:53 -04003620 mcmd->reset_count = vha->hw->chip_reset;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003621
3622 rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
3623 le16_to_cpu(iocb->u.isp2x.seq_id));
3624 if (rc != 0) {
3625 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
3626 "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
3627 vha->vp_idx, rc);
3628 mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
3629 return -EFAULT;
3630 }
3631
3632 return 0;
3633}
3634
3635/* ha->hardware_lock supposed to be held on entry */
3636static int qlt_abort_task(struct scsi_qla_host *vha,
3637 struct imm_ntfy_from_isp *iocb)
3638{
3639 struct qla_hw_data *ha = vha->hw;
3640 struct qla_tgt_sess *sess;
3641 int loop_id;
3642
3643 loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
3644
3645 sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
3646 if (sess == NULL) {
3647 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
3648 "qla_target(%d): task abort for unexisting "
3649 "session\n", vha->vp_idx);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003650 return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003651 QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
3652 }
3653
3654 return __qlt_abort_task(vha, iocb, sess);
3655}
3656
3657/*
3658 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3659 */
3660static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
3661 struct imm_ntfy_from_isp *iocb)
3662{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003663 int res = 0;
3664
3665 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04003666 "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
3667 vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003668
3669 switch (iocb->u.isp24.status_subcode) {
3670 case ELS_PLOGI:
3671 case ELS_FLOGI:
3672 case ELS_PRLI:
3673 case ELS_LOGO:
3674 case ELS_PRLO:
3675 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
3676 break;
3677 case ELS_PDISC:
3678 case ELS_ADISC:
3679 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08003680 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003681 if (tgt->link_reinit_iocb_pending) {
3682 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3683 0, 0, 0, 0, 0, 0);
3684 tgt->link_reinit_iocb_pending = 0;
3685 }
3686 res = 1; /* send notify ack */
3687 break;
3688 }
3689
3690 default:
3691 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
3692 "qla_target(%d): Unsupported ELS command %x "
3693 "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
3694 res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
3695 break;
3696 }
3697
3698 return res;
3699}
3700
3701static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
3702{
3703 struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
3704 size_t first_offset = 0, rem_offset = offset, tmp = 0;
3705 int i, sg_srr_cnt, bufflen = 0;
3706
3707 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
3708 "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
3709 "cmd->sg_cnt: %u, direction: %d\n",
3710 cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
3711
3712 /*
3713 * FIXME: Reject non zero SRR relative offset until we can test
3714 * this code properly.
3715 */
3716 pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
3717 return -1;
3718
3719 if (!cmd->sg || !cmd->sg_cnt) {
3720 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
3721 "Missing cmd->sg or zero cmd->sg_cnt in"
3722 " qla_tgt_set_data_offset\n");
3723 return -EINVAL;
3724 }
3725 /*
3726 * Walk the current cmd->sg list until we locate the new sg_srr_start
3727 */
3728 for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
3729 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
3730 "sg[%d]: %p page: %p, length: %d, offset: %d\n",
3731 i, sg, sg_page(sg), sg->length, sg->offset);
3732
3733 if ((sg->length + tmp) > offset) {
3734 first_offset = rem_offset;
3735 sg_srr_start = sg;
3736 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
3737 "Found matching sg[%d], using %p as sg_srr_start, "
3738 "and using first_offset: %zu\n", i, sg,
3739 first_offset);
3740 break;
3741 }
3742 tmp += sg->length;
3743 rem_offset -= sg->length;
3744 }
3745
3746 if (!sg_srr_start) {
3747 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
3748 "Unable to locate sg_srr_start for offset: %u\n", offset);
3749 return -EINVAL;
3750 }
3751 sg_srr_cnt = (cmd->sg_cnt - i);
3752
3753 sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
3754 if (!sg_srr) {
3755 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
3756 "Unable to allocate sgp\n");
3757 return -ENOMEM;
3758 }
3759 sg_init_table(sg_srr, sg_srr_cnt);
3760 sgp = &sg_srr[0];
3761 /*
3762 * Walk the remaining list for sg_srr_start, mapping to the newly
3763 * allocated sg_srr taking first_offset into account.
3764 */
3765 for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
3766 if (first_offset) {
3767 sg_set_page(sgp, sg_page(sg),
3768 (sg->length - first_offset), first_offset);
3769 first_offset = 0;
3770 } else {
3771 sg_set_page(sgp, sg_page(sg), sg->length, 0);
3772 }
3773 bufflen += sgp->length;
3774
3775 sgp = sg_next(sgp);
3776 if (!sgp)
3777 break;
3778 }
3779
3780 cmd->sg = sg_srr;
3781 cmd->sg_cnt = sg_srr_cnt;
3782 cmd->bufflen = bufflen;
3783 cmd->offset += offset;
3784 cmd->free_sg = 1;
3785
3786 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
3787 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
3788 cmd->sg_cnt);
3789 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
3790 cmd->bufflen);
3791 ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
3792 cmd->offset);
3793
3794 if (cmd->sg_cnt < 0)
3795 BUG();
3796
3797 if (cmd->bufflen < 0)
3798 BUG();
3799
3800 return 0;
3801}
3802
3803static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
3804 uint32_t srr_rel_offs, int *xmit_type)
3805{
3806 int res = 0, rel_offs;
3807
3808 rel_offs = srr_rel_offs - cmd->offset;
3809 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
3810 srr_rel_offs, rel_offs);
3811
3812 *xmit_type = QLA_TGT_XMIT_ALL;
3813
3814 if (rel_offs < 0) {
3815 ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
3816 "qla_target(%d): SRR rel_offs (%d) < 0",
3817 cmd->vha->vp_idx, rel_offs);
3818 res = -1;
3819 } else if (rel_offs == cmd->bufflen)
3820 *xmit_type = QLA_TGT_XMIT_STATUS;
3821 else if (rel_offs > 0)
3822 res = qlt_set_data_offset(cmd, rel_offs);
3823
3824 return res;
3825}
3826
3827/* No locks, thread context */
3828static void qlt_handle_srr(struct scsi_qla_host *vha,
3829 struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
3830{
3831 struct imm_ntfy_from_isp *ntfy =
3832 (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
3833 struct qla_hw_data *ha = vha->hw;
3834 struct qla_tgt_cmd *cmd = sctio->cmd;
3835 struct se_cmd *se_cmd = &cmd->se_cmd;
3836 unsigned long flags;
3837 int xmit_type = 0, resp = 0;
3838 uint32_t offset;
3839 uint16_t srr_ui;
3840
3841 offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
3842 srr_ui = ntfy->u.isp24.srr_ui;
3843
3844 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
3845 cmd, srr_ui);
3846
3847 switch (srr_ui) {
3848 case SRR_IU_STATUS:
3849 spin_lock_irqsave(&ha->hardware_lock, flags);
3850 qlt_send_notify_ack(vha, ntfy,
3851 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3852 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3853 xmit_type = QLA_TGT_XMIT_STATUS;
3854 resp = 1;
3855 break;
3856 case SRR_IU_DATA_IN:
3857 if (!cmd->sg || !cmd->sg_cnt) {
3858 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
3859 "Unable to process SRR_IU_DATA_IN due to"
3860 " missing cmd->sg, state: %d\n", cmd->state);
3861 dump_stack();
3862 goto out_reject;
3863 }
3864 if (se_cmd->scsi_status != 0) {
3865 ql_dbg(ql_dbg_tgt, vha, 0xe02a,
3866 "Rejecting SRR_IU_DATA_IN with non GOOD "
3867 "scsi_status\n");
3868 goto out_reject;
3869 }
3870 cmd->bufflen = se_cmd->data_length;
3871
3872 if (qlt_has_data(cmd)) {
3873 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3874 goto out_reject;
3875 spin_lock_irqsave(&ha->hardware_lock, flags);
3876 qlt_send_notify_ack(vha, ntfy,
3877 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3878 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3879 resp = 1;
3880 } else {
3881 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
3882 "qla_target(%d): SRR for in data for cmd "
3883 "without them (tag %d, SCSI status %d), "
3884 "reject", vha->vp_idx, cmd->tag,
3885 cmd->se_cmd.scsi_status);
3886 goto out_reject;
3887 }
3888 break;
3889 case SRR_IU_DATA_OUT:
3890 if (!cmd->sg || !cmd->sg_cnt) {
3891 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
3892 "Unable to process SRR_IU_DATA_OUT due to"
3893 " missing cmd->sg\n");
3894 dump_stack();
3895 goto out_reject;
3896 }
3897 if (se_cmd->scsi_status != 0) {
3898 ql_dbg(ql_dbg_tgt, vha, 0xe02b,
3899 "Rejecting SRR_IU_DATA_OUT"
3900 " with non GOOD scsi_status\n");
3901 goto out_reject;
3902 }
3903 cmd->bufflen = se_cmd->data_length;
3904
3905 if (qlt_has_data(cmd)) {
3906 if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3907 goto out_reject;
3908 spin_lock_irqsave(&ha->hardware_lock, flags);
3909 qlt_send_notify_ack(vha, ntfy,
3910 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3911 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003912 if (xmit_type & QLA_TGT_XMIT_DATA) {
3913 cmd->cmd_flags |= BIT_8;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003914 qlt_rdy_to_xfer(cmd);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003915 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003916 } else {
3917 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
3918 "qla_target(%d): SRR for out data for cmd "
3919 "without them (tag %d, SCSI status %d), "
3920 "reject", vha->vp_idx, cmd->tag,
3921 cmd->se_cmd.scsi_status);
3922 goto out_reject;
3923 }
3924 break;
3925 default:
3926 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
3927 "qla_target(%d): Unknown srr_ui value %x",
3928 vha->vp_idx, srr_ui);
3929 goto out_reject;
3930 }
3931
3932 /* Transmit response in case of status and data-in cases */
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003933 if (resp) {
3934 cmd->cmd_flags |= BIT_7;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003935 qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003936 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003937
3938 return;
3939
3940out_reject:
3941 spin_lock_irqsave(&ha->hardware_lock, flags);
3942 qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
3943 NOTIFY_ACK_SRR_FLAGS_REJECT,
3944 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3945 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3946 if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3947 cmd->state = QLA_TGT_STATE_DATA_IN;
3948 dump_stack();
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003949 } else {
3950 cmd->cmd_flags |= BIT_9;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003951 qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
Saurav Kashyape07f8f62014-09-25 06:14:58 -04003952 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003953 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3954}
3955
3956static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
3957 struct qla_tgt_srr_imm *imm, int ha_locked)
3958{
3959 struct qla_hw_data *ha = vha->hw;
3960 unsigned long flags = 0;
3961
3962 if (!ha_locked)
3963 spin_lock_irqsave(&ha->hardware_lock, flags);
3964
3965 qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
3966 NOTIFY_ACK_SRR_FLAGS_REJECT,
3967 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3968 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3969
3970 if (!ha_locked)
3971 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3972
3973 kfree(imm);
3974}
3975
3976static void qlt_handle_srr_work(struct work_struct *work)
3977{
3978 struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
3979 struct scsi_qla_host *vha = tgt->vha;
3980 struct qla_tgt_srr_ctio *sctio;
3981 unsigned long flags;
3982
3983 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
3984 tgt);
3985
3986restart:
3987 spin_lock_irqsave(&tgt->srr_lock, flags);
3988 list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
3989 struct qla_tgt_srr_imm *imm, *i, *ti;
3990 struct qla_tgt_cmd *cmd;
3991 struct se_cmd *se_cmd;
3992
3993 imm = NULL;
3994 list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
3995 srr_list_entry) {
3996 if (i->srr_id == sctio->srr_id) {
3997 list_del(&i->srr_list_entry);
3998 if (imm) {
3999 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
4000 "qla_target(%d): There must be "
4001 "only one IMM SRR per CTIO SRR "
4002 "(IMM SRR %p, id %d, CTIO %p\n",
4003 vha->vp_idx, i, i->srr_id, sctio);
4004 qlt_reject_free_srr_imm(tgt->vha, i, 0);
4005 } else
4006 imm = i;
4007 }
4008 }
4009
4010 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
4011 "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
4012 sctio->srr_id);
4013
4014 if (imm == NULL) {
4015 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
4016 "Not found matching IMM for SRR CTIO (id %d)\n",
4017 sctio->srr_id);
4018 continue;
4019 } else
4020 list_del(&sctio->srr_list_entry);
4021
4022 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4023
4024 cmd = sctio->cmd;
4025 /*
4026 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
4027 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
4028 * logic..
4029 */
4030 cmd->offset = 0;
4031 if (cmd->free_sg) {
4032 kfree(cmd->sg);
4033 cmd->sg = NULL;
4034 cmd->free_sg = 0;
4035 }
4036 se_cmd = &cmd->se_cmd;
4037
4038 cmd->sg_cnt = se_cmd->t_data_nents;
4039 cmd->sg = se_cmd->t_data_sg;
4040
4041 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
4042 "SRR cmd %p (se_cmd %p, tag %d, op %x), "
4043 "sg_cnt=%d, offset=%d", cmd, &cmd->se_cmd, cmd->tag,
Dr. Greg Wettstein6f58c782014-02-24 13:59:53 -06004044 se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
4045 cmd->sg_cnt, cmd->offset);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004046
4047 qlt_handle_srr(vha, sctio, imm);
4048
4049 kfree(imm);
4050 kfree(sctio);
4051 goto restart;
4052 }
4053 spin_unlock_irqrestore(&tgt->srr_lock, flags);
4054}
4055
4056/* ha->hardware_lock supposed to be held on entry */
4057static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
4058 struct imm_ntfy_from_isp *iocb)
4059{
4060 struct qla_tgt_srr_imm *imm;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004061 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004062 struct qla_tgt_srr_ctio *sctio;
4063
4064 tgt->imm_srr_id++;
4065
Saurav Kashyape07f8f62014-09-25 06:14:58 -04004066 ql_log(ql_log_warn, vha, 0xf02d, "qla_target(%d): SRR received\n",
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004067 vha->vp_idx);
4068
4069 imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
4070 if (imm != NULL) {
4071 memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
4072
4073 /* IRQ is already OFF */
4074 spin_lock(&tgt->srr_lock);
4075 imm->srr_id = tgt->imm_srr_id;
4076 list_add_tail(&imm->srr_list_entry,
4077 &tgt->srr_imm_list);
4078 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
4079 "IMM NTFY SRR %p added (id %d, ui %x)\n",
4080 imm, imm->srr_id, iocb->u.isp24.srr_ui);
4081 if (tgt->imm_srr_id == tgt->ctio_srr_id) {
4082 int found = 0;
4083 list_for_each_entry(sctio, &tgt->srr_ctio_list,
4084 srr_list_entry) {
4085 if (sctio->srr_id == imm->srr_id) {
4086 found = 1;
4087 break;
4088 }
4089 }
4090 if (found) {
4091 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
4092 "Scheduling srr work\n");
4093 schedule_work(&tgt->srr_work);
4094 } else {
4095 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
4096 "qla_target(%d): imm_srr_id "
4097 "== ctio_srr_id (%d), but there is no "
4098 "corresponding SRR CTIO, deleting IMM "
4099 "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
4100 imm);
4101 list_del(&imm->srr_list_entry);
4102
4103 kfree(imm);
4104
4105 spin_unlock(&tgt->srr_lock);
4106 goto out_reject;
4107 }
4108 }
4109 spin_unlock(&tgt->srr_lock);
4110 } else {
4111 struct qla_tgt_srr_ctio *ts;
4112
4113 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
4114 "qla_target(%d): Unable to allocate SRR IMM "
4115 "entry, SRR request will be rejected\n", vha->vp_idx);
4116
4117 /* IRQ is already OFF */
4118 spin_lock(&tgt->srr_lock);
4119 list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
4120 srr_list_entry) {
4121 if (sctio->srr_id == tgt->imm_srr_id) {
4122 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
4123 "CTIO SRR %p deleted (id %d)\n",
4124 sctio, sctio->srr_id);
4125 list_del(&sctio->srr_list_entry);
4126 qlt_send_term_exchange(vha, sctio->cmd,
4127 &sctio->cmd->atio, 1);
4128 kfree(sctio);
4129 }
4130 }
4131 spin_unlock(&tgt->srr_lock);
4132 goto out_reject;
4133 }
4134
4135 return;
4136
4137out_reject:
4138 qlt_send_notify_ack(vha, iocb, 0, 0, 0,
4139 NOTIFY_ACK_SRR_FLAGS_REJECT,
4140 NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4141 NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4142}
4143
4144/*
4145 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4146 */
4147static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4148 struct imm_ntfy_from_isp *iocb)
4149{
4150 struct qla_hw_data *ha = vha->hw;
4151 uint32_t add_flags = 0;
4152 int send_notify_ack = 1;
4153 uint16_t status;
4154
4155 status = le16_to_cpu(iocb->u.isp2x.status);
4156 switch (status) {
4157 case IMM_NTFY_LIP_RESET:
4158 {
4159 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4160 "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4161 vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4162 iocb->u.isp24.status_subcode);
4163
4164 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4165 send_notify_ack = 0;
4166 break;
4167 }
4168
4169 case IMM_NTFY_LIP_LINK_REINIT:
4170 {
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004171 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004172 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4173 "qla_target(%d): LINK REINIT (loop %#x, "
4174 "subcode %x)\n", vha->vp_idx,
4175 le16_to_cpu(iocb->u.isp24.nport_handle),
4176 iocb->u.isp24.status_subcode);
4177 if (tgt->link_reinit_iocb_pending) {
4178 qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4179 0, 0, 0, 0, 0, 0);
4180 }
4181 memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4182 tgt->link_reinit_iocb_pending = 1;
4183 /*
4184 * QLogic requires to wait after LINK REINIT for possible
4185 * PDISC or ADISC ELS commands
4186 */
4187 send_notify_ack = 0;
4188 break;
4189 }
4190
4191 case IMM_NTFY_PORT_LOGOUT:
4192 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4193 "qla_target(%d): Port logout (loop "
4194 "%#x, subcode %x)\n", vha->vp_idx,
4195 le16_to_cpu(iocb->u.isp24.nport_handle),
4196 iocb->u.isp24.status_subcode);
4197
4198 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4199 send_notify_ack = 0;
4200 /* The sessions will be cleared in the callback, if needed */
4201 break;
4202
4203 case IMM_NTFY_GLBL_TPRLO:
4204 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4205 "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4206 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4207 send_notify_ack = 0;
4208 /* The sessions will be cleared in the callback, if needed */
4209 break;
4210
4211 case IMM_NTFY_PORT_CONFIG:
4212 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4213 "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4214 status);
4215 if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4216 send_notify_ack = 0;
4217 /* The sessions will be cleared in the callback, if needed */
4218 break;
4219
4220 case IMM_NTFY_GLBL_LOGO:
4221 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4222 "qla_target(%d): Link failure detected\n",
4223 vha->vp_idx);
4224 /* I_T nexus loss */
4225 if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4226 send_notify_ack = 0;
4227 break;
4228
4229 case IMM_NTFY_IOCB_OVERFLOW:
4230 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4231 "qla_target(%d): Cannot provide requested "
4232 "capability (IOCB overflowed the immediate notify "
4233 "resource count)\n", vha->vp_idx);
4234 break;
4235
4236 case IMM_NTFY_ABORT_TASK:
4237 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4238 "qla_target(%d): Abort Task (S %08x I %#x -> "
4239 "L %#x)\n", vha->vp_idx,
4240 le16_to_cpu(iocb->u.isp2x.seq_id),
4241 GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4242 le16_to_cpu(iocb->u.isp2x.lun));
4243 if (qlt_abort_task(vha, iocb) == 0)
4244 send_notify_ack = 0;
4245 break;
4246
4247 case IMM_NTFY_RESOURCE:
4248 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4249 "qla_target(%d): Out of resources, host %ld\n",
4250 vha->vp_idx, vha->host_no);
4251 break;
4252
4253 case IMM_NTFY_MSG_RX:
4254 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4255 "qla_target(%d): Immediate notify task %x\n",
4256 vha->vp_idx, iocb->u.isp2x.task_flags);
4257 if (qlt_handle_task_mgmt(vha, iocb) == 0)
4258 send_notify_ack = 0;
4259 break;
4260
4261 case IMM_NTFY_ELS:
4262 if (qlt_24xx_handle_els(vha, iocb) == 0)
4263 send_notify_ack = 0;
4264 break;
4265
4266 case IMM_NTFY_SRR:
4267 qlt_prepare_srr_imm(vha, iocb);
4268 send_notify_ack = 0;
4269 break;
4270
4271 default:
4272 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4273 "qla_target(%d): Received unknown immediate "
4274 "notify status %x\n", vha->vp_idx, status);
4275 break;
4276 }
4277
4278 if (send_notify_ack)
4279 qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4280}
4281
4282/*
4283 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4284 * This function sends busy to ISP 2xxx or 24xx.
4285 */
Quinn Tran33e79972014-09-25 06:14:55 -04004286static int __qlt_send_busy(struct scsi_qla_host *vha,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004287 struct atio_from_isp *atio, uint16_t status)
4288{
4289 struct ctio7_to_24xx *ctio24;
4290 struct qla_hw_data *ha = vha->hw;
4291 request_t *pkt;
4292 struct qla_tgt_sess *sess = NULL;
4293
4294 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4295 atio->u.isp24.fcp_hdr.s_id);
4296 if (!sess) {
4297 qlt_send_term_exchange(vha, NULL, atio, 1);
Quinn Tran33e79972014-09-25 06:14:55 -04004298 return 0;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004299 }
4300 /* Sending marker isn't necessary, since we called from ISR */
4301
4302 pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
4303 if (!pkt) {
Arun Easi667024a2014-09-25 06:14:47 -04004304 ql_dbg(ql_dbg_io, vha, 0x3063,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004305 "qla_target(%d): %s failed: unable to allocate "
4306 "request packet", vha->vp_idx, __func__);
Quinn Tran33e79972014-09-25 06:14:55 -04004307 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004308 }
4309
4310 pkt->entry_count = 1;
4311 pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
4312
4313 ctio24 = (struct ctio7_to_24xx *)pkt;
4314 ctio24->entry_type = CTIO_TYPE7;
4315 ctio24->nport_handle = sess->loop_id;
4316 ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
4317 ctio24->vp_index = vha->vp_idx;
4318 ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
4319 ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
4320 ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
4321 ctio24->exchange_addr = atio->u.isp24.exchange_addr;
4322 ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
4323 __constant_cpu_to_le16(
4324 CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
4325 CTIO7_FLAGS_DONT_RET_CTIO);
4326 /*
4327 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
4328 * if the explicit conformation is used.
4329 */
4330 ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
4331 ctio24->u.status1.scsi_status = cpu_to_le16(status);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004332 qla2x00_start_iocbs(vha, vha->req);
Quinn Tran33e79972014-09-25 06:14:55 -04004333 return 0;
4334}
4335
4336/*
4337 * This routine is used to allocate a command for either a QFull condition
4338 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
4339 * out previously.
4340 */
4341static void
4342qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
4343 struct atio_from_isp *atio, uint16_t status, int qfull)
4344{
4345 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4346 struct qla_hw_data *ha = vha->hw;
4347 struct qla_tgt_sess *sess;
4348 struct se_session *se_sess;
4349 struct qla_tgt_cmd *cmd;
4350 int tag;
4351
4352 if (unlikely(tgt->tgt_stop)) {
4353 ql_dbg(ql_dbg_io, vha, 0x300a,
4354 "New command while device %p is shutting down\n", tgt);
4355 return;
4356 }
4357
4358 if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
4359 vha->hw->tgt.num_qfull_cmds_dropped++;
4360 if (vha->hw->tgt.num_qfull_cmds_dropped >
4361 vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
4362 vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
4363 vha->hw->tgt.num_qfull_cmds_dropped;
4364
4365 ql_dbg(ql_dbg_io, vha, 0x3068,
4366 "qla_target(%d): %s: QFull CMD dropped[%d]\n",
4367 vha->vp_idx, __func__,
4368 vha->hw->tgt.num_qfull_cmds_dropped);
4369
4370 qlt_chk_exch_leak_thresh_hold(vha);
4371 return;
4372 }
4373
4374 sess = ha->tgt.tgt_ops->find_sess_by_s_id
4375 (vha, atio->u.isp24.fcp_hdr.s_id);
4376 if (!sess)
4377 return;
4378
4379 se_sess = sess->se_sess;
4380
4381 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4382 if (tag < 0)
4383 return;
4384
4385 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4386 if (!cmd) {
4387 ql_dbg(ql_dbg_io, vha, 0x3009,
4388 "qla_target(%d): %s: Allocation of cmd failed\n",
4389 vha->vp_idx, __func__);
4390
4391 vha->hw->tgt.num_qfull_cmds_dropped++;
4392 if (vha->hw->tgt.num_qfull_cmds_dropped >
4393 vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
4394 vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
4395 vha->hw->tgt.num_qfull_cmds_dropped;
4396
4397 qlt_chk_exch_leak_thresh_hold(vha);
4398 return;
4399 }
4400
4401 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4402
4403 qlt_incr_num_pend_cmds(vha);
4404 INIT_LIST_HEAD(&cmd->cmd_list);
4405 memcpy(&cmd->atio, atio, sizeof(*atio));
4406
4407 cmd->tgt = vha->vha_tgt.qla_tgt;
4408 cmd->vha = vha;
4409 cmd->reset_count = vha->hw->chip_reset;
4410 cmd->q_full = 1;
4411
4412 if (qfull) {
4413 cmd->q_full = 1;
4414 /* NOTE: borrowing the state field to carry the status */
4415 cmd->state = status;
4416 } else
4417 cmd->term_exchg = 1;
4418
4419 list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
4420
4421 vha->hw->tgt.num_qfull_cmds_alloc++;
4422 if (vha->hw->tgt.num_qfull_cmds_alloc >
4423 vha->hw->qla_stats.stat_max_qfull_cmds_alloc)
4424 vha->hw->qla_stats.stat_max_qfull_cmds_alloc =
4425 vha->hw->tgt.num_qfull_cmds_alloc;
4426}
4427
4428int
4429qlt_free_qfull_cmds(struct scsi_qla_host *vha)
4430{
4431 struct qla_hw_data *ha = vha->hw;
4432 unsigned long flags;
4433 struct qla_tgt_cmd *cmd, *tcmd;
4434 struct list_head free_list;
4435 int rc = 0;
4436
4437 if (list_empty(&ha->tgt.q_full_list))
4438 return 0;
4439
4440 INIT_LIST_HEAD(&free_list);
4441
4442 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
4443
4444 if (list_empty(&ha->tgt.q_full_list)) {
4445 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
4446 return 0;
4447 }
4448
4449 list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
4450 if (cmd->q_full)
4451 /* cmd->state is a borrowed field to hold status */
4452 rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
4453 else if (cmd->term_exchg)
4454 rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
4455
4456 if (rc == -ENOMEM)
4457 break;
4458
4459 if (cmd->q_full)
4460 ql_dbg(ql_dbg_io, vha, 0x3006,
4461 "%s: busy sent for ox_id[%04x]\n", __func__,
4462 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
4463 else if (cmd->term_exchg)
4464 ql_dbg(ql_dbg_io, vha, 0x3007,
4465 "%s: Term exchg sent for ox_id[%04x]\n", __func__,
4466 be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
4467 else
4468 ql_dbg(ql_dbg_io, vha, 0x3008,
4469 "%s: Unexpected cmd in QFull list %p\n", __func__,
4470 cmd);
4471
4472 list_del(&cmd->cmd_list);
4473 list_add_tail(&cmd->cmd_list, &free_list);
4474
4475 /* piggy back on hardware_lock for protection */
4476 vha->hw->tgt.num_qfull_cmds_alloc--;
4477 }
4478 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
4479
4480 cmd = NULL;
4481
4482 list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
4483 list_del(&cmd->cmd_list);
4484 /* This cmd was never sent to TCM. There is no need
4485 * to schedule free or call free_cmd
4486 */
4487 qlt_free_cmd(cmd);
4488 }
4489 return rc;
4490}
4491
4492static void
4493qlt_send_busy(struct scsi_qla_host *vha,
4494 struct atio_from_isp *atio, uint16_t status)
4495{
4496 int rc = 0;
4497
4498 rc = __qlt_send_busy(vha, atio, status);
4499 if (rc == -ENOMEM)
4500 qlt_alloc_qfull_cmd(vha, atio, status, 1);
4501}
4502
4503static int
4504qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
4505 struct atio_from_isp *atio)
4506{
4507 struct qla_hw_data *ha = vha->hw;
4508 uint16_t status;
4509
4510 if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
4511 return 0;
4512
4513 status = temp_sam_status;
4514 qlt_send_busy(vha, atio, status);
4515 return 1;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004516}
4517
4518/* ha->hardware_lock supposed to be held on entry */
4519/* called via callback from qla2xxx */
4520static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
4521 struct atio_from_isp *atio)
4522{
4523 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004524 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004525 int rc;
4526
4527 if (unlikely(tgt == NULL)) {
Arun Easi667024a2014-09-25 06:14:47 -04004528 ql_dbg(ql_dbg_io, vha, 0x3064,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004529 "ATIO pkt, but no tgt (ha %p)", ha);
4530 return;
4531 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004532 /*
4533 * In tgt_stop mode we also should allow all requests to pass.
4534 * Otherwise, some commands can stuck.
4535 */
4536
4537 tgt->irq_cmd_count++;
4538
4539 switch (atio->u.raw.entry_type) {
4540 case ATIO_TYPE7:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004541 if (unlikely(atio->u.isp24.exchange_addr ==
4542 ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
Arun Easi667024a2014-09-25 06:14:47 -04004543 ql_dbg(ql_dbg_io, vha, 0x3065,
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004544 "qla_target(%d): ATIO_TYPE7 "
4545 "received with UNKNOWN exchange address, "
4546 "sending QUEUE_FULL\n", vha->vp_idx);
4547 qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
4548 break;
4549 }
Quinn Tran33e79972014-09-25 06:14:55 -04004550
4551
4552
4553 if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
4554 rc = qlt_chk_qfull_thresh_hold(vha, atio);
4555 if (rc != 0) {
4556 tgt->irq_cmd_count--;
4557 return;
4558 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004559 rc = qlt_handle_cmd_for_atio(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04004560 } else {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004561 rc = qlt_handle_task_mgmt(vha, atio);
Quinn Tran33e79972014-09-25 06:14:55 -04004562 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004563 if (unlikely(rc != 0)) {
4564 if (rc == -ESRCH) {
4565#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
4566 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
4567#else
4568 qlt_send_term_exchange(vha, NULL, atio, 1);
4569#endif
4570 } else {
4571 if (tgt->tgt_stop) {
4572 ql_dbg(ql_dbg_tgt, vha, 0xe059,
4573 "qla_target: Unable to send "
4574 "command to target for req, "
4575 "ignoring.\n");
4576 } else {
4577 ql_dbg(ql_dbg_tgt, vha, 0xe05a,
4578 "qla_target(%d): Unable to send "
4579 "command to target, sending BUSY "
4580 "status.\n", vha->vp_idx);
4581 qlt_send_busy(vha, atio, SAM_STAT_BUSY);
4582 }
4583 }
4584 }
4585 break;
4586
4587 case IMMED_NOTIFY_TYPE:
4588 {
4589 if (unlikely(atio->u.isp2x.entry_status != 0)) {
4590 ql_dbg(ql_dbg_tgt, vha, 0xe05b,
4591 "qla_target(%d): Received ATIO packet %x "
4592 "with error status %x\n", vha->vp_idx,
4593 atio->u.raw.entry_type,
4594 atio->u.isp2x.entry_status);
4595 break;
4596 }
4597 ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
4598 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
4599 break;
4600 }
4601
4602 default:
4603 ql_dbg(ql_dbg_tgt, vha, 0xe05c,
4604 "qla_target(%d): Received unknown ATIO atio "
4605 "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
4606 break;
4607 }
4608
4609 tgt->irq_cmd_count--;
4610}
4611
4612/* ha->hardware_lock supposed to be held on entry */
4613/* called via callback from qla2xxx */
4614static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
4615{
4616 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004617 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004618
4619 if (unlikely(tgt == NULL)) {
4620 ql_dbg(ql_dbg_tgt, vha, 0xe05d,
4621 "qla_target(%d): Response pkt %x received, but no "
4622 "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
4623 return;
4624 }
4625
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004626 /*
4627 * In tgt_stop mode we also should allow all requests to pass.
4628 * Otherwise, some commands can stuck.
4629 */
4630
4631 tgt->irq_cmd_count++;
4632
4633 switch (pkt->entry_type) {
Quinn Tranf83adb62014-04-11 16:54:43 -04004634 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004635 case CTIO_TYPE7:
4636 {
4637 struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004638 qlt_do_ctio_completion(vha, entry->handle,
4639 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
4640 entry);
4641 break;
4642 }
4643
4644 case ACCEPT_TGT_IO_TYPE:
4645 {
4646 struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
4647 int rc;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004648 if (atio->u.isp2x.status !=
4649 __constant_cpu_to_le16(ATIO_CDB_VALID)) {
4650 ql_dbg(ql_dbg_tgt, vha, 0xe05e,
4651 "qla_target(%d): ATIO with error "
4652 "status %x received\n", vha->vp_idx,
4653 le16_to_cpu(atio->u.isp2x.status));
4654 break;
4655 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004656
Quinn Tran33e79972014-09-25 06:14:55 -04004657 rc = qlt_chk_qfull_thresh_hold(vha, atio);
4658 if (rc != 0) {
4659 tgt->irq_cmd_count--;
4660 return;
4661 }
4662
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004663 rc = qlt_handle_cmd_for_atio(vha, atio);
4664 if (unlikely(rc != 0)) {
4665 if (rc == -ESRCH) {
4666#if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
4667 qlt_send_busy(vha, atio, 0);
4668#else
4669 qlt_send_term_exchange(vha, NULL, atio, 1);
4670#endif
4671 } else {
4672 if (tgt->tgt_stop) {
4673 ql_dbg(ql_dbg_tgt, vha, 0xe05f,
4674 "qla_target: Unable to send "
4675 "command to target, sending TERM "
4676 "EXCHANGE for rsp\n");
4677 qlt_send_term_exchange(vha, NULL,
4678 atio, 1);
4679 } else {
4680 ql_dbg(ql_dbg_tgt, vha, 0xe060,
4681 "qla_target(%d): Unable to send "
4682 "command to target, sending BUSY "
4683 "status\n", vha->vp_idx);
4684 qlt_send_busy(vha, atio, 0);
4685 }
4686 }
4687 }
4688 }
4689 break;
4690
4691 case CONTINUE_TGT_IO_TYPE:
4692 {
4693 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004694 qlt_do_ctio_completion(vha, entry->handle,
4695 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
4696 entry);
4697 break;
4698 }
4699
4700 case CTIO_A64_TYPE:
4701 {
4702 struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004703 qlt_do_ctio_completion(vha, entry->handle,
4704 le16_to_cpu(entry->status)|(pkt->entry_status << 16),
4705 entry);
4706 break;
4707 }
4708
4709 case IMMED_NOTIFY_TYPE:
4710 ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
4711 qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
4712 break;
4713
4714 case NOTIFY_ACK_TYPE:
4715 if (tgt->notify_ack_expected > 0) {
4716 struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
4717 ql_dbg(ql_dbg_tgt, vha, 0xe036,
4718 "NOTIFY_ACK seq %08x status %x\n",
4719 le16_to_cpu(entry->u.isp2x.seq_id),
4720 le16_to_cpu(entry->u.isp2x.status));
4721 tgt->notify_ack_expected--;
4722 if (entry->u.isp2x.status !=
4723 __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
4724 ql_dbg(ql_dbg_tgt, vha, 0xe061,
4725 "qla_target(%d): NOTIFY_ACK "
4726 "failed %x\n", vha->vp_idx,
4727 le16_to_cpu(entry->u.isp2x.status));
4728 }
4729 } else {
4730 ql_dbg(ql_dbg_tgt, vha, 0xe062,
4731 "qla_target(%d): Unexpected NOTIFY_ACK received\n",
4732 vha->vp_idx);
4733 }
4734 break;
4735
4736 case ABTS_RECV_24XX:
4737 ql_dbg(ql_dbg_tgt, vha, 0xe037,
4738 "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
4739 qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
4740 break;
4741
4742 case ABTS_RESP_24XX:
4743 if (tgt->abts_resp_expected > 0) {
4744 struct abts_resp_from_24xx_fw *entry =
4745 (struct abts_resp_from_24xx_fw *)pkt;
4746 ql_dbg(ql_dbg_tgt, vha, 0xe038,
4747 "ABTS_RESP_24XX: compl_status %x\n",
4748 entry->compl_status);
4749 tgt->abts_resp_expected--;
4750 if (le16_to_cpu(entry->compl_status) !=
4751 ABTS_RESP_COMPL_SUCCESS) {
4752 if ((entry->error_subcode1 == 0x1E) &&
4753 (entry->error_subcode2 == 0)) {
4754 /*
4755 * We've got a race here: aborted
4756 * exchange not terminated, i.e.
4757 * response for the aborted command was
4758 * sent between the abort request was
4759 * received and processed.
4760 * Unfortunately, the firmware has a
4761 * silly requirement that all aborted
4762 * exchanges must be explicitely
4763 * terminated, otherwise it refuses to
4764 * send responses for the abort
4765 * requests. So, we have to
4766 * (re)terminate the exchange and retry
4767 * the abort response.
4768 */
4769 qlt_24xx_retry_term_exchange(vha,
4770 entry);
4771 } else
4772 ql_dbg(ql_dbg_tgt, vha, 0xe063,
4773 "qla_target(%d): ABTS_RESP_24XX "
4774 "failed %x (subcode %x:%x)",
4775 vha->vp_idx, entry->compl_status,
4776 entry->error_subcode1,
4777 entry->error_subcode2);
4778 }
4779 } else {
4780 ql_dbg(ql_dbg_tgt, vha, 0xe064,
4781 "qla_target(%d): Unexpected ABTS_RESP_24XX "
4782 "received\n", vha->vp_idx);
4783 }
4784 break;
4785
4786 default:
4787 ql_dbg(ql_dbg_tgt, vha, 0xe065,
4788 "qla_target(%d): Received unknown response pkt "
4789 "type %x\n", vha->vp_idx, pkt->entry_type);
4790 break;
4791 }
4792
4793 tgt->irq_cmd_count--;
4794}
4795
4796/*
4797 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4798 */
4799void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
4800 uint16_t *mailbox)
4801{
4802 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004803 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Alan Cox4f1d0f12012-07-04 16:35:35 +01004804 int login_code;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004805
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004806 if (!ha->tgt.tgt_ops)
4807 return;
4808
4809 if (unlikely(tgt == NULL)) {
4810 ql_dbg(ql_dbg_tgt, vha, 0xe03a,
4811 "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
4812 return;
4813 }
4814
4815 if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
4816 IS_QLA2100(ha))
4817 return;
4818 /*
4819 * In tgt_stop mode we also should allow all requests to pass.
4820 * Otherwise, some commands can stuck.
4821 */
4822
4823 tgt->irq_cmd_count++;
4824
4825 switch (code) {
4826 case MBA_RESET: /* Reset */
4827 case MBA_SYSTEM_ERR: /* System Error */
4828 case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */
4829 case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */
4830 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
4831 "qla_target(%d): System error async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09004832 "occurred", vha->vp_idx, code);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004833 break;
4834 case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */
4835 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4836 break;
4837
4838 case MBA_LOOP_UP:
4839 {
4840 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09004841 "qla_target(%d): Async LOOP_UP occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01004842 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
4843 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4844 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004845 if (tgt->link_reinit_iocb_pending) {
4846 qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
4847 0, 0, 0, 0, 0, 0);
4848 tgt->link_reinit_iocb_pending = 0;
4849 }
4850 break;
4851 }
4852
4853 case MBA_LIP_OCCURRED:
4854 case MBA_LOOP_DOWN:
4855 case MBA_LIP_RESET:
4856 case MBA_RSCN_UPDATE:
4857 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09004858 "qla_target(%d): Async event %#x occurred "
Alan Cox4f1d0f12012-07-04 16:35:35 +01004859 "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
4860 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4861 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004862 break;
4863
4864 case MBA_PORT_UPDATE:
4865 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
4866 "qla_target(%d): Port update async event %#x "
Masanari Iida6efb3c0a2012-10-26 22:10:54 +09004867 "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
Alan Cox4f1d0f12012-07-04 16:35:35 +01004868 "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
4869 le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4870 le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4871
4872 login_code = le16_to_cpu(mailbox[2]);
4873 if (login_code == 0x4)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004874 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
4875 "Async MB 2: Got PLOGI Complete\n");
Alan Cox4f1d0f12012-07-04 16:35:35 +01004876 else if (login_code == 0x7)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004877 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
4878 "Async MB 2: Port Logged Out\n");
4879 break;
4880
4881 default:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004882 break;
4883 }
4884
4885 tgt->irq_cmd_count--;
4886}
4887
4888static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
4889 uint16_t loop_id)
4890{
4891 fc_port_t *fcport;
4892 int rc;
4893
4894 fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
4895 if (!fcport) {
4896 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
4897 "qla_target(%d): Allocation of tmp FC port failed",
4898 vha->vp_idx);
4899 return NULL;
4900 }
4901
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004902 fcport->loop_id = loop_id;
4903
4904 rc = qla2x00_get_port_database(vha, fcport, 0);
4905 if (rc != QLA_SUCCESS) {
4906 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
4907 "qla_target(%d): Failed to retrieve fcport "
4908 "information -- get_port_database() returned %x "
4909 "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
4910 kfree(fcport);
4911 return NULL;
4912 }
4913
4914 return fcport;
4915}
4916
4917/* Must be called under tgt_mutex */
4918static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
4919 uint8_t *s_id)
4920{
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004921 struct qla_tgt_sess *sess = NULL;
4922 fc_port_t *fcport = NULL;
4923 int rc, global_resets;
4924 uint16_t loop_id = 0;
4925
4926retry:
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004927 global_resets =
4928 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004929
4930 rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
4931 if (rc != 0) {
4932 if ((s_id[0] == 0xFF) &&
4933 (s_id[1] == 0xFC)) {
4934 /*
4935 * This is Domain Controller, so it should be
4936 * OK to drop SCSI commands from it.
4937 */
4938 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
4939 "Unable to find initiator with S_ID %x:%x:%x",
4940 s_id[0], s_id[1], s_id[2]);
4941 } else
4942 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071,
4943 "qla_target(%d): Unable to find "
4944 "initiator with S_ID %x:%x:%x",
4945 vha->vp_idx, s_id[0], s_id[1],
4946 s_id[2]);
4947 return NULL;
4948 }
4949
4950 fcport = qlt_get_port_database(vha, loop_id);
4951 if (!fcport)
4952 return NULL;
4953
4954 if (global_resets !=
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004955 atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004956 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
4957 "qla_target(%d): global reset during session discovery "
4958 "(counter was %d, new %d), retrying", vha->vp_idx,
4959 global_resets,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004960 atomic_read(&vha->vha_tgt.
4961 qla_tgt->tgt_global_resets_count));
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004962 goto retry;
4963 }
4964
4965 sess = qlt_create_sess(vha, fcport, true);
4966
4967 kfree(fcport);
4968 return sess;
4969}
4970
4971static void qlt_abort_work(struct qla_tgt *tgt,
4972 struct qla_tgt_sess_work_param *prm)
4973{
4974 struct scsi_qla_host *vha = tgt->vha;
4975 struct qla_hw_data *ha = vha->hw;
4976 struct qla_tgt_sess *sess = NULL;
4977 unsigned long flags;
4978 uint32_t be_s_id;
4979 uint8_t s_id[3];
4980 int rc;
4981
4982 spin_lock_irqsave(&ha->hardware_lock, flags);
4983
4984 if (tgt->tgt_stop)
4985 goto out_term;
4986
4987 s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
4988 s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
4989 s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
4990
4991 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4992 (unsigned char *)&be_s_id);
4993 if (!sess) {
4994 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4995
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004996 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004997 sess = qlt_make_local_sess(vha, s_id);
4998 /* sess has got an extra creation ref */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08004999 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005000
5001 spin_lock_irqsave(&ha->hardware_lock, flags);
5002 if (!sess)
5003 goto out_term;
5004 } else {
5005 kref_get(&sess->se_sess->sess_kref);
5006 }
5007
5008 if (tgt->tgt_stop)
5009 goto out_term;
5010
5011 rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5012 if (rc != 0)
5013 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005014
5015 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04005016 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005017 return;
5018
5019out_term:
5020 qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005021 if (sess)
5022 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04005023 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005024}
5025
5026static void qlt_tmr_work(struct qla_tgt *tgt,
5027 struct qla_tgt_sess_work_param *prm)
5028{
5029 struct atio_from_isp *a = &prm->tm_iocb2;
5030 struct scsi_qla_host *vha = tgt->vha;
5031 struct qla_hw_data *ha = vha->hw;
5032 struct qla_tgt_sess *sess = NULL;
5033 unsigned long flags;
5034 uint8_t *s_id = NULL; /* to hide compiler warnings */
5035 int rc;
5036 uint32_t lun, unpacked_lun;
5037 int lun_size, fn;
5038 void *iocb;
5039
5040 spin_lock_irqsave(&ha->hardware_lock, flags);
5041
5042 if (tgt->tgt_stop)
5043 goto out_term;
5044
5045 s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5046 sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5047 if (!sess) {
5048 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5049
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005050 mutex_lock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005051 sess = qlt_make_local_sess(vha, s_id);
5052 /* sess has got an extra creation ref */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005053 mutex_unlock(&vha->vha_tgt.tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005054
5055 spin_lock_irqsave(&ha->hardware_lock, flags);
5056 if (!sess)
5057 goto out_term;
5058 } else {
5059 kref_get(&sess->se_sess->sess_kref);
5060 }
5061
5062 iocb = a;
5063 lun = a->u.isp24.fcp_cmnd.lun;
5064 lun_size = sizeof(lun);
5065 fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5066 unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5067
5068 rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5069 if (rc != 0)
5070 goto out_term;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005071
5072 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04005073 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005074 return;
5075
5076out_term:
5077 qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005078 if (sess)
5079 ha->tgt.tgt_ops->put_sess(sess);
Jörn Engel08234e32013-06-12 16:27:54 -04005080 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005081}
5082
5083static void qlt_sess_work_fn(struct work_struct *work)
5084{
5085 struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5086 struct scsi_qla_host *vha = tgt->vha;
5087 unsigned long flags;
5088
5089 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5090
5091 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5092 while (!list_empty(&tgt->sess_works_list)) {
5093 struct qla_tgt_sess_work_param *prm = list_entry(
5094 tgt->sess_works_list.next, typeof(*prm),
5095 sess_works_list_entry);
5096
5097 /*
5098 * This work can be scheduled on several CPUs at time, so we
5099 * must delete the entry to eliminate double processing
5100 */
5101 list_del(&prm->sess_works_list_entry);
5102
5103 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5104
5105 switch (prm->type) {
5106 case QLA_TGT_SESS_WORK_ABORT:
5107 qlt_abort_work(tgt, prm);
5108 break;
5109 case QLA_TGT_SESS_WORK_TM:
5110 qlt_tmr_work(tgt, prm);
5111 break;
5112 default:
5113 BUG_ON(1);
5114 break;
5115 }
5116
5117 spin_lock_irqsave(&tgt->sess_work_lock, flags);
5118
5119 kfree(prm);
5120 }
5121 spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5122}
5123
5124/* Must be called under tgt_host_action_mutex */
5125int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5126{
5127 struct qla_tgt *tgt;
5128
5129 if (!QLA_TGT_MODE_ENABLED())
5130 return 0;
5131
Arun Easi33c36c02013-01-30 03:34:41 -05005132 if (!IS_TGT_MODE_CAPABLE(ha)) {
5133 ql_log(ql_log_warn, base_vha, 0xe070,
5134 "This adapter does not support target mode.\n");
5135 return 0;
5136 }
5137
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005138 ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005139 "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005140
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005141 BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005142
5143 tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5144 if (!tgt) {
5145 ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5146 "Unable to allocate struct qla_tgt\n");
5147 return -ENOMEM;
5148 }
5149
5150 if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5151 base_vha->host->hostt->supported_mode |= MODE_TARGET;
5152
5153 tgt->ha = ha;
5154 tgt->vha = base_vha;
5155 init_waitqueue_head(&tgt->waitQ);
5156 INIT_LIST_HEAD(&tgt->sess_list);
5157 INIT_LIST_HEAD(&tgt->del_sess_list);
5158 INIT_DELAYED_WORK(&tgt->sess_del_work,
5159 (void (*)(struct work_struct *))qlt_del_sess_work_fn);
5160 spin_lock_init(&tgt->sess_work_lock);
5161 INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
5162 INIT_LIST_HEAD(&tgt->sess_works_list);
5163 spin_lock_init(&tgt->srr_lock);
5164 INIT_LIST_HEAD(&tgt->srr_ctio_list);
5165 INIT_LIST_HEAD(&tgt->srr_imm_list);
5166 INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
5167 atomic_set(&tgt->tgt_global_resets_count, 0);
5168
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005169 base_vha->vha_tgt.qla_tgt = tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005170
5171 ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
5172 "qla_target(%d): using 64 Bit PCI addressing",
5173 base_vha->vp_idx);
5174 tgt->tgt_enable_64bit_addr = 1;
5175 /* 3 is reserved */
5176 tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
5177 tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
5178 tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
5179
Nicholas Bellingerddb95142014-02-19 17:51:25 -08005180 if (base_vha->fc_vport)
5181 return 0;
5182
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005183 mutex_lock(&qla_tgt_mutex);
5184 list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
5185 mutex_unlock(&qla_tgt_mutex);
5186
5187 return 0;
5188}
5189
5190/* Must be called under tgt_host_action_mutex */
5191int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
5192{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005193 if (!vha->vha_tgt.qla_tgt)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005194 return 0;
5195
Nicholas Bellingerddb95142014-02-19 17:51:25 -08005196 if (vha->fc_vport) {
5197 qlt_release(vha->vha_tgt.qla_tgt);
5198 return 0;
5199 }
Quinn Tran33e79972014-09-25 06:14:55 -04005200
5201 /* free left over qfull cmds */
5202 qlt_init_term_exchange(vha);
5203
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005204 mutex_lock(&qla_tgt_mutex);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005205 list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005206 mutex_unlock(&qla_tgt_mutex);
5207
5208 ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
5209 vha->host_no, ha);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005210 qlt_release(vha->vha_tgt.qla_tgt);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005211
5212 return 0;
5213}
5214
5215static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
5216 unsigned char *b)
5217{
5218 int i;
5219
5220 pr_debug("qla2xxx HW vha->node_name: ");
5221 for (i = 0; i < WWN_SIZE; i++)
5222 pr_debug("%02x ", vha->node_name[i]);
5223 pr_debug("\n");
5224 pr_debug("qla2xxx HW vha->port_name: ");
5225 for (i = 0; i < WWN_SIZE; i++)
5226 pr_debug("%02x ", vha->port_name[i]);
5227 pr_debug("\n");
5228
5229 pr_debug("qla2xxx passed configfs WWPN: ");
5230 put_unaligned_be64(wwpn, b);
5231 for (i = 0; i < WWN_SIZE; i++)
5232 pr_debug("%02x ", b[i]);
5233 pr_debug("\n");
5234}
5235
5236/**
5237 * qla_tgt_lport_register - register lport with external module
5238 *
5239 * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
5240 * @wwpn: Passwd FC target WWPN
5241 * @callback: lport initialization callback for tcm_qla2xxx code
5242 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
5243 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08005244int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
5245 u64 npiv_wwpn, u64 npiv_wwnn,
5246 int (*callback)(struct scsi_qla_host *, void *, u64, u64))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005247{
5248 struct qla_tgt *tgt;
5249 struct scsi_qla_host *vha;
5250 struct qla_hw_data *ha;
5251 struct Scsi_Host *host;
5252 unsigned long flags;
5253 int rc;
5254 u8 b[WWN_SIZE];
5255
5256 mutex_lock(&qla_tgt_mutex);
5257 list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
5258 vha = tgt->vha;
5259 ha = vha->hw;
5260
5261 host = vha->host;
5262 if (!host)
5263 continue;
5264
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005265 if (!(host->hostt->supported_mode & MODE_TARGET))
5266 continue;
5267
5268 spin_lock_irqsave(&ha->hardware_lock, flags);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08005269 if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005270 pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
5271 host->host_no);
5272 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5273 continue;
5274 }
Nicholas Bellingerddb95142014-02-19 17:51:25 -08005275 if (tgt->tgt_stop) {
5276 pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
5277 host->host_no);
5278 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5279 continue;
5280 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005281 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5282
5283 if (!scsi_host_get(host)) {
5284 ql_dbg(ql_dbg_tgt, vha, 0xe068,
5285 "Unable to scsi_host_get() for"
5286 " qla2xxx scsi_host\n");
5287 continue;
5288 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08005289 qlt_lport_dump(vha, phys_wwpn, b);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005290
5291 if (memcmp(vha->port_name, b, WWN_SIZE)) {
5292 scsi_host_put(host);
5293 continue;
5294 }
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08005295 rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
5296 if (rc != 0)
5297 scsi_host_put(host);
5298
Nicholas Bellingerddb95142014-02-19 17:51:25 -08005299 mutex_unlock(&qla_tgt_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005300 return rc;
5301 }
5302 mutex_unlock(&qla_tgt_mutex);
5303
5304 return -ENODEV;
5305}
5306EXPORT_SYMBOL(qlt_lport_register);
5307
5308/**
5309 * qla_tgt_lport_deregister - Degister lport
5310 *
5311 * @vha: Registered scsi_qla_host pointer
5312 */
5313void qlt_lport_deregister(struct scsi_qla_host *vha)
5314{
5315 struct qla_hw_data *ha = vha->hw;
5316 struct Scsi_Host *sh = vha->host;
5317 /*
5318 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
5319 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005320 vha->vha_tgt.target_lport_ptr = NULL;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005321 ha->tgt.tgt_ops = NULL;
5322 /*
5323 * Release the Scsi_Host reference for the underlying qla2xxx host
5324 */
5325 scsi_host_put(sh);
5326}
5327EXPORT_SYMBOL(qlt_lport_deregister);
5328
5329/* Must be called under HW lock */
5330void qlt_set_mode(struct scsi_qla_host *vha)
5331{
5332 struct qla_hw_data *ha = vha->hw;
5333
5334 switch (ql2x_ini_mode) {
5335 case QLA2XXX_INI_MODE_DISABLED:
5336 case QLA2XXX_INI_MODE_EXCLUSIVE:
5337 vha->host->active_mode = MODE_TARGET;
5338 break;
5339 case QLA2XXX_INI_MODE_ENABLED:
5340 vha->host->active_mode |= MODE_TARGET;
5341 break;
5342 default:
5343 break;
5344 }
5345
5346 if (ha->tgt.ini_mode_force_reverse)
5347 qla_reverse_ini_mode(vha);
5348}
5349
5350/* Must be called under HW lock */
5351void qlt_clear_mode(struct scsi_qla_host *vha)
5352{
5353 struct qla_hw_data *ha = vha->hw;
5354
5355 switch (ql2x_ini_mode) {
5356 case QLA2XXX_INI_MODE_DISABLED:
5357 vha->host->active_mode = MODE_UNKNOWN;
5358 break;
5359 case QLA2XXX_INI_MODE_EXCLUSIVE:
5360 vha->host->active_mode = MODE_INITIATOR;
5361 break;
5362 case QLA2XXX_INI_MODE_ENABLED:
5363 vha->host->active_mode &= ~MODE_TARGET;
5364 break;
5365 default:
5366 break;
5367 }
5368
5369 if (ha->tgt.ini_mode_force_reverse)
5370 qla_reverse_ini_mode(vha);
5371}
5372
5373/*
5374 * qla_tgt_enable_vha - NO LOCK HELD
5375 *
5376 * host_reset, bring up w/ Target Mode Enabled
5377 */
5378void
5379qlt_enable_vha(struct scsi_qla_host *vha)
5380{
5381 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005382 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005383 unsigned long flags;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005384 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005385
5386 if (!tgt) {
5387 ql_dbg(ql_dbg_tgt, vha, 0xe069,
5388 "Unable to locate qla_tgt pointer from"
5389 " struct qla_hw_data\n");
5390 dump_stack();
5391 return;
5392 }
5393
5394 spin_lock_irqsave(&ha->hardware_lock, flags);
5395 tgt->tgt_stopped = 0;
5396 qlt_set_mode(vha);
5397 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5398
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005399 if (vha->vp_idx) {
5400 qla24xx_disable_vp(vha);
5401 qla24xx_enable_vp(vha);
5402 } else {
5403 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
5404 qla2xxx_wake_dpc(base_vha);
5405 qla2x00_wait_for_hba_online(base_vha);
5406 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005407}
5408EXPORT_SYMBOL(qlt_enable_vha);
5409
5410/*
5411 * qla_tgt_disable_vha - NO LOCK HELD
5412 *
5413 * Disable Target Mode and reset the adapter
5414 */
5415void
5416qlt_disable_vha(struct scsi_qla_host *vha)
5417{
5418 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005419 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005420 unsigned long flags;
5421
5422 if (!tgt) {
5423 ql_dbg(ql_dbg_tgt, vha, 0xe06a,
5424 "Unable to locate qla_tgt pointer from"
5425 " struct qla_hw_data\n");
5426 dump_stack();
5427 return;
5428 }
5429
5430 spin_lock_irqsave(&ha->hardware_lock, flags);
5431 qlt_clear_mode(vha);
5432 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5433
5434 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5435 qla2xxx_wake_dpc(vha);
5436 qla2x00_wait_for_hba_online(vha);
5437}
5438
5439/*
5440 * Called from qla_init.c:qla24xx_vport_create() contex to setup
5441 * the target mode specific struct scsi_qla_host and struct qla_hw_data
5442 * members.
5443 */
5444void
5445qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
5446{
5447 if (!qla_tgt_mode_enabled(vha))
5448 return;
5449
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005450 vha->vha_tgt.qla_tgt = NULL;
5451
5452 mutex_init(&vha->vha_tgt.tgt_mutex);
5453 mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005454
5455 qlt_clear_mode(vha);
5456
5457 /*
5458 * NOTE: Currently the value is kept the same for <24xx and
5459 * >=24xx ISPs. If it is necessary to change it,
5460 * the check should be added for specific ISPs,
5461 * assigning the value appropriately.
5462 */
5463 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005464
5465 qlt_add_target(ha, vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005466}
5467
5468void
5469qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
5470{
5471 /*
5472 * FC-4 Feature bit 0 indicates target functionality to the name server.
5473 */
5474 if (qla_tgt_mode_enabled(vha)) {
5475 if (qla_ini_mode_enabled(vha))
5476 ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
5477 else
5478 ct_req->req.rff_id.fc4_feature = BIT_0;
5479 } else if (qla_ini_mode_enabled(vha)) {
5480 ct_req->req.rff_id.fc4_feature = BIT_1;
5481 }
5482}
5483
5484/*
5485 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
5486 * @ha: HA context
5487 *
5488 * Beginning of ATIO ring has initialization control block already built
5489 * by nvram config routine.
5490 *
5491 * Returns 0 on success.
5492 */
5493void
5494qlt_init_atio_q_entries(struct scsi_qla_host *vha)
5495{
5496 struct qla_hw_data *ha = vha->hw;
5497 uint16_t cnt;
5498 struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
5499
5500 if (!qla_tgt_mode_enabled(vha))
5501 return;
5502
5503 for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
5504 pkt->u.raw.signature = ATIO_PROCESSED;
5505 pkt++;
5506 }
5507
5508}
5509
5510/*
5511 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
5512 * @ha: SCSI driver HA context
5513 */
5514void
5515qlt_24xx_process_atio_queue(struct scsi_qla_host *vha)
5516{
5517 struct qla_hw_data *ha = vha->hw;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005518 struct atio_from_isp *pkt;
5519 int cnt, i;
5520
5521 if (!vha->flags.online)
5522 return;
5523
5524 while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
5525 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
5526 cnt = pkt->u.raw.entry_count;
5527
5528 qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt);
5529
5530 for (i = 0; i < cnt; i++) {
5531 ha->tgt.atio_ring_index++;
5532 if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
5533 ha->tgt.atio_ring_index = 0;
5534 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
5535 } else
5536 ha->tgt.atio_ring_ptr++;
5537
5538 pkt->u.raw.signature = ATIO_PROCESSED;
5539 pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
5540 }
5541 wmb();
5542 }
5543
5544 /* Adjust ring index */
Arun Easiaa230bc2013-01-30 03:34:39 -05005545 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005546}
5547
5548void
Arun Easiaa230bc2013-01-30 03:34:39 -05005549qlt_24xx_config_rings(struct scsi_qla_host *vha)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005550{
5551 struct qla_hw_data *ha = vha->hw;
Arun Easiaa230bc2013-01-30 03:34:39 -05005552 if (!QLA_TGT_MODE_ENABLED())
5553 return;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005554
Arun Easiaa230bc2013-01-30 03:34:39 -05005555 WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
5556 WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
5557 RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
5558
5559 if (IS_ATIO_MSIX_CAPABLE(ha)) {
5560 struct qla_msix_entry *msix = &ha->msix_entries[2];
5561 struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
5562
5563 icb->msix_atio = cpu_to_le16(msix->entry);
5564 ql_dbg(ql_dbg_init, vha, 0xf072,
5565 "Registering ICB vector 0x%x for atio que.\n",
5566 msix->entry);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005567 }
5568}
5569
5570void
5571qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
5572{
5573 struct qla_hw_data *ha = vha->hw;
5574
5575 if (qla_tgt_mode_enabled(vha)) {
5576 if (!ha->tgt.saved_set) {
5577 /* We save only once */
5578 ha->tgt.saved_exchange_count = nv->exchange_count;
5579 ha->tgt.saved_firmware_options_1 =
5580 nv->firmware_options_1;
5581 ha->tgt.saved_firmware_options_2 =
5582 nv->firmware_options_2;
5583 ha->tgt.saved_firmware_options_3 =
5584 nv->firmware_options_3;
5585 ha->tgt.saved_set = 1;
5586 }
5587
5588 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
5589
5590 /* Enable target mode */
5591 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
5592
5593 /* Disable ini mode, if requested */
5594 if (!qla_ini_mode_enabled(vha))
5595 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5);
5596
5597 /* Disable Full Login after LIP */
5598 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
5599 /* Enable initial LIP */
5600 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04005601 if (ql2xtgt_tape_enable)
5602 /* Enable FC Tape support */
5603 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
5604 else
5605 /* Disable FC Tape support */
5606 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
5607
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005608 /* Disable Full Login after LIP */
5609 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
5610 /* Enable target PRLI control */
5611 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
5612 } else {
5613 if (ha->tgt.saved_set) {
5614 nv->exchange_count = ha->tgt.saved_exchange_count;
5615 nv->firmware_options_1 =
5616 ha->tgt.saved_firmware_options_1;
5617 nv->firmware_options_2 =
5618 ha->tgt.saved_firmware_options_2;
5619 nv->firmware_options_3 =
5620 ha->tgt.saved_firmware_options_3;
5621 }
5622 return;
5623 }
5624
5625 /* out-of-order frames reassembly */
5626 nv->firmware_options_3 |= BIT_6|BIT_9;
5627
5628 if (ha->tgt.enable_class_2) {
5629 if (vha->flags.init_done)
5630 fc_host_supported_classes(vha->host) =
5631 FC_COS_CLASS2 | FC_COS_CLASS3;
5632
5633 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
5634 } else {
5635 if (vha->flags.init_done)
5636 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
5637
5638 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
5639 }
5640}
5641
5642void
5643qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
5644 struct init_cb_24xx *icb)
5645{
5646 struct qla_hw_data *ha = vha->hw;
5647
5648 if (ha->tgt.node_name_set) {
5649 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
5650 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
5651 }
5652}
5653
Arun Easiaa230bc2013-01-30 03:34:39 -05005654void
5655qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
5656{
5657 struct qla_hw_data *ha = vha->hw;
5658
5659 if (!QLA_TGT_MODE_ENABLED())
5660 return;
5661
5662 if (qla_tgt_mode_enabled(vha)) {
5663 if (!ha->tgt.saved_set) {
5664 /* We save only once */
5665 ha->tgt.saved_exchange_count = nv->exchange_count;
5666 ha->tgt.saved_firmware_options_1 =
5667 nv->firmware_options_1;
5668 ha->tgt.saved_firmware_options_2 =
5669 nv->firmware_options_2;
5670 ha->tgt.saved_firmware_options_3 =
5671 nv->firmware_options_3;
5672 ha->tgt.saved_set = 1;
5673 }
5674
5675 nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
5676
5677 /* Enable target mode */
5678 nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
5679
5680 /* Disable ini mode, if requested */
5681 if (!qla_ini_mode_enabled(vha))
5682 nv->firmware_options_1 |=
5683 __constant_cpu_to_le32(BIT_5);
5684
5685 /* Disable Full Login after LIP */
5686 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
5687 /* Enable initial LIP */
5688 nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
Arun Easid154f352014-09-25 06:14:48 -04005689 if (ql2xtgt_tape_enable)
5690 /* Enable FC tape support */
5691 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
5692 else
5693 /* Disable FC tape support */
5694 nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
5695
Arun Easiaa230bc2013-01-30 03:34:39 -05005696 /* Disable Full Login after LIP */
5697 nv->host_p &= __constant_cpu_to_le32(~BIT_10);
5698 /* Enable target PRLI control */
5699 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
5700 } else {
5701 if (ha->tgt.saved_set) {
5702 nv->exchange_count = ha->tgt.saved_exchange_count;
5703 nv->firmware_options_1 =
5704 ha->tgt.saved_firmware_options_1;
5705 nv->firmware_options_2 =
5706 ha->tgt.saved_firmware_options_2;
5707 nv->firmware_options_3 =
5708 ha->tgt.saved_firmware_options_3;
5709 }
5710 return;
5711 }
5712
5713 /* out-of-order frames reassembly */
5714 nv->firmware_options_3 |= BIT_6|BIT_9;
5715
5716 if (ha->tgt.enable_class_2) {
5717 if (vha->flags.init_done)
5718 fc_host_supported_classes(vha->host) =
5719 FC_COS_CLASS2 | FC_COS_CLASS3;
5720
5721 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
5722 } else {
5723 if (vha->flags.init_done)
5724 fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
5725
5726 nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
5727 }
5728}
5729
5730void
5731qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
5732 struct init_cb_81xx *icb)
5733{
5734 struct qla_hw_data *ha = vha->hw;
5735
5736 if (!QLA_TGT_MODE_ENABLED())
5737 return;
5738
5739 if (ha->tgt.node_name_set) {
5740 memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
5741 icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
5742 }
5743}
5744
5745void
5746qlt_83xx_iospace_config(struct qla_hw_data *ha)
5747{
5748 if (!QLA_TGT_MODE_ENABLED())
5749 return;
5750
5751 ha->msix_count += 1; /* For ATIO Q */
5752}
5753
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005754int
5755qlt_24xx_process_response_error(struct scsi_qla_host *vha,
5756 struct sts_entry_24xx *pkt)
5757{
5758 switch (pkt->entry_type) {
5759 case ABTS_RECV_24XX:
5760 case ABTS_RESP_24XX:
5761 case CTIO_TYPE7:
5762 case NOTIFY_ACK_TYPE:
Quinn Tranf83adb62014-04-11 16:54:43 -04005763 case CTIO_CRC2:
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005764 return 1;
5765 default:
5766 return 0;
5767 }
5768}
5769
5770void
5771qlt_modify_vp_config(struct scsi_qla_host *vha,
5772 struct vp_config_entry_24xx *vpmod)
5773{
5774 if (qla_tgt_mode_enabled(vha))
5775 vpmod->options_idx1 &= ~BIT_5;
5776 /* Disable ini mode, if requested */
5777 if (!qla_ini_mode_enabled(vha))
5778 vpmod->options_idx1 &= ~BIT_4;
5779}
5780
5781void
5782qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
5783{
5784 if (!QLA_TGT_MODE_ENABLED())
5785 return;
5786
Arun Easiaa230bc2013-01-30 03:34:39 -05005787 if (ha->mqenable || IS_QLA83XX(ha)) {
5788 ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
5789 ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
5790 } else {
5791 ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
5792 ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
5793 }
5794
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08005795 mutex_init(&base_vha->vha_tgt.tgt_mutex);
5796 mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005797 qlt_clear_mode(base_vha);
5798}
5799
Arun Easiaa230bc2013-01-30 03:34:39 -05005800irqreturn_t
5801qla83xx_msix_atio_q(int irq, void *dev_id)
5802{
5803 struct rsp_que *rsp;
5804 scsi_qla_host_t *vha;
5805 struct qla_hw_data *ha;
5806 unsigned long flags;
5807
5808 rsp = (struct rsp_que *) dev_id;
5809 ha = rsp->hw;
5810 vha = pci_get_drvdata(ha->pdev);
5811
5812 spin_lock_irqsave(&ha->hardware_lock, flags);
5813
5814 qlt_24xx_process_atio_queue(vha);
5815 qla24xx_process_response_queue(vha, rsp);
5816
5817 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5818
5819 return IRQ_HANDLED;
5820}
5821
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005822int
5823qlt_mem_alloc(struct qla_hw_data *ha)
5824{
5825 if (!QLA_TGT_MODE_ENABLED())
5826 return 0;
5827
5828 ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
5829 MAX_MULTI_ID_FABRIC, GFP_KERNEL);
5830 if (!ha->tgt.tgt_vp_map)
5831 return -ENOMEM;
5832
5833 ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
5834 (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
5835 &ha->tgt.atio_dma, GFP_KERNEL);
5836 if (!ha->tgt.atio_ring) {
5837 kfree(ha->tgt.tgt_vp_map);
5838 return -ENOMEM;
5839 }
5840 return 0;
5841}
5842
5843void
5844qlt_mem_free(struct qla_hw_data *ha)
5845{
5846 if (!QLA_TGT_MODE_ENABLED())
5847 return;
5848
5849 if (ha->tgt.atio_ring) {
5850 dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
5851 sizeof(struct atio_from_isp), ha->tgt.atio_ring,
5852 ha->tgt.atio_dma);
5853 }
5854 kfree(ha->tgt.tgt_vp_map);
5855}
5856
5857/* vport_slock to be held by the caller */
5858void
5859qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
5860{
5861 if (!QLA_TGT_MODE_ENABLED())
5862 return;
5863
5864 switch (cmd) {
5865 case SET_VP_IDX:
5866 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
5867 break;
5868 case SET_AL_PA:
5869 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
5870 break;
5871 case RESET_VP_IDX:
5872 vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
5873 break;
5874 case RESET_AL_PA:
5875 vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
5876 break;
5877 }
5878}
5879
5880static int __init qlt_parse_ini_mode(void)
5881{
5882 if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
5883 ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
5884 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
5885 ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
5886 else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
5887 ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
5888 else
5889 return false;
5890
5891 return true;
5892}
5893
5894int __init qlt_init(void)
5895{
5896 int ret;
5897
5898 if (!qlt_parse_ini_mode()) {
5899 ql_log(ql_log_fatal, NULL, 0xe06b,
5900 "qlt_parse_ini_mode() failed\n");
5901 return -EINVAL;
5902 }
5903
5904 if (!QLA_TGT_MODE_ENABLED())
5905 return 0;
5906
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005907 qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
5908 sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
5909 qla_tgt_mgmt_cmd), 0, NULL);
5910 if (!qla_tgt_mgmt_cmd_cachep) {
5911 ql_log(ql_log_fatal, NULL, 0xe06d,
5912 "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07005913 return -ENOMEM;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005914 }
5915
5916 qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
5917 mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
5918 if (!qla_tgt_mgmt_cmd_mempool) {
5919 ql_log(ql_log_fatal, NULL, 0xe06e,
5920 "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
5921 ret = -ENOMEM;
5922 goto out_mgmt_cmd_cachep;
5923 }
5924
5925 qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
5926 if (!qla_tgt_wq) {
5927 ql_log(ql_log_fatal, NULL, 0xe06f,
5928 "alloc_workqueue for qla_tgt_wq failed\n");
5929 ret = -ENOMEM;
5930 goto out_cmd_mempool;
5931 }
5932 /*
5933 * Return 1 to signal that initiator-mode is being disabled
5934 */
5935 return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
5936
5937out_cmd_mempool:
5938 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
5939out_mgmt_cmd_cachep:
5940 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005941 return ret;
5942}
5943
5944void qlt_exit(void)
5945{
5946 if (!QLA_TGT_MODE_ENABLED())
5947 return;
5948
5949 destroy_workqueue(qla_tgt_wq);
5950 mempool_destroy(qla_tgt_mgmt_cmd_mempool);
5951 kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005952}