]>
Commit | Line | Data |
---|---|---|
2d70c103 NB |
1 | /* |
2 | * qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx | |
3 | * | |
4 | * based on qla2x00t.c code: | |
5 | * | |
6 | * Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net> | |
7 | * Copyright (C) 2004 - 2005 Leonid Stoljar | |
8 | * Copyright (C) 2006 Nathaniel Clark <nate@misrule.us> | |
9 | * Copyright (C) 2006 - 2010 ID7 Ltd. | |
10 | * | |
11 | * Forward port and refactoring to modern qla2xxx and target/configfs | |
12 | * | |
4c76251e | 13 | * Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org> |
2d70c103 NB |
14 | * |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * as published by the Free Software Foundation, version 2 | |
18 | * of the License. | |
19 | * | |
20 | * This program is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | * GNU General Public License for more details. | |
24 | */ | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/types.h> | |
2d70c103 NB |
29 | #include <linux/blkdev.h> |
30 | #include <linux/interrupt.h> | |
31 | #include <linux/pci.h> | |
32 | #include <linux/delay.h> | |
33 | #include <linux/list.h> | |
34 | #include <linux/workqueue.h> | |
35 | #include <asm/unaligned.h> | |
36 | #include <scsi/scsi.h> | |
37 | #include <scsi/scsi_host.h> | |
38 | #include <scsi/scsi_tcq.h> | |
39 | #include <target/target_core_base.h> | |
40 | #include <target/target_core_fabric.h> | |
41 | ||
42 | #include "qla_def.h" | |
43 | #include "qla_target.h" | |
44 | ||
d154f350 AE |
45 | static int ql2xtgt_tape_enable; |
46 | module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR); | |
47 | MODULE_PARM_DESC(ql2xtgt_tape_enable, | |
48 | "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER."); | |
49 | ||
2d70c103 NB |
50 | static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED; |
51 | module_param(qlini_mode, charp, S_IRUGO); | |
52 | MODULE_PARM_DESC(qlini_mode, | |
53 | "Determines when initiator mode will be enabled. Possible values: " | |
54 | "\"exclusive\" - initiator mode will be enabled on load, " | |
55 | "disabled on enabling target mode and then on disabling target mode " | |
56 | "enabled back; " | |
57 | "\"disabled\" - initiator mode will never be enabled; " | |
58 | "\"enabled\" (default) - initiator mode will always stay enabled."); | |
59 | ||
aa230bc5 | 60 | int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE; |
2d70c103 | 61 | |
33e79977 QT |
62 | static int temp_sam_status = SAM_STAT_BUSY; |
63 | ||
2d70c103 NB |
64 | /* |
65 | * From scsi/fc/fc_fcp.h | |
66 | */ | |
67 | enum fcp_resp_rsp_codes { | |
68 | FCP_TMF_CMPL = 0, | |
69 | FCP_DATA_LEN_INVALID = 1, | |
70 | FCP_CMND_FIELDS_INVALID = 2, | |
71 | FCP_DATA_PARAM_MISMATCH = 3, | |
72 | FCP_TMF_REJECTED = 4, | |
73 | FCP_TMF_FAILED = 5, | |
74 | FCP_TMF_INVALID_LUN = 9, | |
75 | }; | |
76 | ||
77 | /* | |
78 | * fc_pri_ta from scsi/fc/fc_fcp.h | |
79 | */ | |
80 | #define FCP_PTA_SIMPLE 0 /* simple task attribute */ | |
81 | #define FCP_PTA_HEADQ 1 /* head of queue task attribute */ | |
82 | #define FCP_PTA_ORDERED 2 /* ordered task attribute */ | |
6efb3c0a | 83 | #define FCP_PTA_ACA 4 /* auto. contingent allegiance */ |
2d70c103 NB |
84 | #define FCP_PTA_MASK 7 /* mask for task attribute field */ |
85 | #define FCP_PRI_SHIFT 3 /* priority field starts in bit 3 */ | |
86 | #define FCP_PRI_RESVD_MASK 0x80 /* reserved bits in priority field */ | |
87 | ||
88 | /* | |
89 | * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which | |
90 | * must be called under HW lock and could unlock/lock it inside. | |
91 | * It isn't an issue, since in the current implementation on the time when | |
92 | * those functions are called: | |
93 | * | |
94 | * - Either context is IRQ and only IRQ handler can modify HW data, | |
95 | * including rings related fields, | |
96 | * | |
97 | * - Or access to target mode variables from struct qla_tgt doesn't | |
98 | * cross those functions boundaries, except tgt_stop, which | |
99 | * additionally protected by irq_cmd_count. | |
100 | */ | |
101 | /* Predefs for callbacks handed to qla2xxx LLD */ | |
102 | static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha, | |
103 | struct atio_from_isp *pkt); | |
104 | static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt); | |
105 | static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun, | |
106 | int fn, void *iocb, int flags); | |
107 | static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd | |
108 | *cmd, struct atio_from_isp *atio, int ha_locked); | |
109 | static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha, | |
110 | struct qla_tgt_srr_imm *imm, int ha_lock); | |
b6a029e1 AE |
111 | static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, |
112 | struct qla_tgt_cmd *cmd); | |
33e79977 QT |
113 | static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha, |
114 | struct atio_from_isp *atio, uint16_t status, int qfull); | |
2d70c103 NB |
115 | /* |
116 | * Global Variables | |
117 | */ | |
2d70c103 NB |
118 | static struct kmem_cache *qla_tgt_mgmt_cmd_cachep; |
119 | static mempool_t *qla_tgt_mgmt_cmd_mempool; | |
120 | static struct workqueue_struct *qla_tgt_wq; | |
121 | static DEFINE_MUTEX(qla_tgt_mutex); | |
122 | static LIST_HEAD(qla_tgt_glist); | |
123 | ||
124 | /* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */ | |
125 | static 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!! */ | |
140 | static 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 | ||
155 | static inline | |
156 | struct 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 | ||
176 | static inline | |
177 | struct 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 | ||
33e79977 QT |
192 | static 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 | } | |
204 | static 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 | ||
2d70c103 NB |
213 | void qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha, |
214 | struct atio_from_isp *atio) | |
215 | { | |
f83adb61 QT |
216 | 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 | ||
2d70c103 NB |
221 | 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 | ||
272 | void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt) | |
273 | { | |
274 | switch (pkt->entry_type) { | |
f83adb61 QT |
275 | 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__); | |
2d70c103 NB |
279 | 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 | ||
376 | static 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 */ | |
405 | void 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 | } | |
418 | EXPORT_SYMBOL(qlt_unreg_sess); | |
419 | ||
420 | /* ha->hardware_lock supposed to be held on entry */ | |
421 | static 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, | |
7b833558 OK |
473 | "scsi(%ld): resetting (session %p from port %8phC mcmd %x, " |
474 | "loop_id %d)\n", vha->host_no, sess, sess->port_name, | |
2d70c103 NB |
475 | 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 */ | |
485 | static 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, | |
7b833558 | 505 | "qla_target(%d): session for port %8phC (loop ID %d) scheduled for " |
2d70c103 | 506 | "deletion in %u secs (expires: %lu) immed: %d\n", |
7b833558 OK |
507 | sess->vha->vp_idx, sess->port_name, sess->loop_id, dev_loss_tmo, |
508 | sess->expires, immediate); | |
2d70c103 NB |
509 | |
510 | if (immediate) | |
511 | schedule_delayed_work(&tgt->sess_del_work, 0); | |
512 | else | |
513 | schedule_delayed_work(&tgt->sess_del_work, | |
63832aab | 514 | sess->expires - jiffies); |
2d70c103 NB |
515 | } |
516 | ||
517 | /* ha->hardware_lock supposed to be held on entry */ | |
518 | static 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 | ||
528 | static 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 | ||
571 | out_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 | ||
2d70c103 NB |
577 | /* ha->hardware_lock supposed to be held on entry */ |
578 | static 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 | ||
586 | static 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; | |
63832aab | 593 | unsigned long flags, elapsed; |
2d70c103 NB |
594 | |
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); | |
63832aab SU |
599 | elapsed = jiffies; |
600 | if (time_after_eq(elapsed, sess->expires)) { | |
2d70c103 NB |
601 | qlt_undelete_sess(sess); |
602 | ||
08234e3a JE |
603 | 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); | |
2d70c103 NB |
608 | } else { |
609 | schedule_delayed_work(&tgt->sess_del_work, | |
63832aab | 610 | sess->expires - elapsed); |
2d70c103 NB |
611 | 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 | */ | |
621 | static 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); | |
0e8cd71c | 633 | list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list, |
2d70c103 NB |
634 | 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); | |
c8292d1d RD |
649 | ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id, |
650 | (fcport->flags & FCF_CONF_COMP_SUPPORTED)); | |
651 | ||
2d70c103 NB |
652 | 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, | |
7b833558 OK |
664 | "qla_target(%u): session allocation failed, all commands " |
665 | "from port %8phC will be refused", vha->vp_idx, | |
666 | fcport->port_name); | |
2d70c103 NB |
667 | |
668 | return NULL; | |
669 | } | |
0e8cd71c | 670 | sess->tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
671 | 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", | |
0e8cd71c | 678 | sess, vha->vha_tgt.qla_tgt); |
2d70c103 NB |
679 | |
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 | ||
c8292d1d | 700 | sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED); |
2d70c103 NB |
701 | 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); | |
0e8cd71c SK |
705 | list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list); |
706 | vha->vha_tgt.qla_tgt->sess_count++; | |
2d70c103 NB |
707 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
708 | ||
709 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b, | |
7b833558 OK |
710 | "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 "); | |
2d70c103 NB |
715 | |
716 | return sess; | |
717 | } | |
718 | ||
719 | /* | |
720 | * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port() | |
721 | */ | |
722 | void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport) | |
723 | { | |
724 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 725 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
726 | 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 | ||
0e8cd71c SK |
735 | if (qla_ini_mode_enabled(vha)) |
736 | return; | |
737 | ||
2d70c103 NB |
738 | 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 | ||
0e8cd71c | 747 | mutex_lock(&vha->vha_tgt.tgt_mutex); |
2d70c103 | 748 | sess = qlt_create_sess(vha, fcport, false); |
0e8cd71c | 749 | mutex_unlock(&vha->vha_tgt.tgt_mutex); |
2d70c103 NB |
750 | |
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, | |
7b833558 OK |
759 | "qla_target(%u): %ssession for port %8phC " |
760 | "(loop ID %d) reappeared\n", vha->vp_idx, | |
761 | sess->local ? "local " : "", sess->port_name, | |
2d70c103 NB |
762 | sess->loop_id); |
763 | ||
764 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007, | |
765 | "Reappeared sess %p\n", sess); | |
766 | } | |
c8292d1d RD |
767 | ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id, |
768 | (fcport->flags & FCF_CONF_COMP_SUPPORTED)); | |
2d70c103 NB |
769 | } |
770 | ||
771 | if (sess && sess->local) { | |
772 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d, | |
773 | "qla_target(%u): local session for " | |
7b833558 OK |
774 | "port %8phC (loop ID %d) became global\n", vha->vp_idx, |
775 | fcport->port_name, sess->loop_id); | |
2d70c103 NB |
776 | sess->local = 0; |
777 | } | |
2d70c103 | 778 | ha->tgt.tgt_ops->put_sess(sess); |
08234e3a | 779 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2d70c103 NB |
780 | } |
781 | ||
782 | void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport) | |
783 | { | |
784 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 785 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
786 | 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 | ||
813 | static 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 */ | |
3c231bda | 833 | int qlt_stop_phase1(struct qla_tgt *tgt) |
2d70c103 NB |
834 | { |
835 | struct scsi_qla_host *vha = tgt->vha; | |
836 | struct qla_hw_data *ha = tgt->ha; | |
837 | unsigned long flags; | |
838 | ||
3c231bda NB |
839 | 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 | } | |
2d70c103 NB |
854 | 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"); | |
3c231bda NB |
857 | mutex_unlock(&qla_tgt_mutex); |
858 | return -EPERM; | |
2d70c103 NB |
859 | } |
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 | */ | |
0e8cd71c | 867 | mutex_lock(&vha->vha_tgt.tgt_mutex); |
2d70c103 NB |
868 | 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); | |
0e8cd71c | 872 | mutex_unlock(&vha->vha_tgt.tgt_mutex); |
3c231bda | 873 | mutex_unlock(&qla_tgt_mutex); |
2d70c103 | 874 | |
43829731 | 875 | flush_delayed_work(&tgt->sess_del_work); |
2d70c103 NB |
876 | |
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)); | |
3c231bda | 900 | return 0; |
2d70c103 NB |
901 | } |
902 | EXPORT_SYMBOL(qlt_stop_phase1); | |
903 | ||
904 | /* Called by tcm_qla2xxx configfs code */ | |
905 | void qlt_stop_phase2(struct qla_tgt *tgt) | |
906 | { | |
907 | struct qla_hw_data *ha = tgt->ha; | |
0e8cd71c | 908 | scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev); |
2d70c103 NB |
909 | unsigned long flags; |
910 | ||
911 | if (tgt->tgt_stopped) { | |
0e8cd71c | 912 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f, |
2d70c103 NB |
913 | "Already in tgt->tgt_stopped state\n"); |
914 | dump_stack(); | |
915 | return; | |
916 | } | |
917 | ||
0e8cd71c | 918 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b, |
2d70c103 NB |
919 | "Waiting for %d IRQ commands to complete (tgt %p)", |
920 | tgt->irq_cmd_count, tgt); | |
921 | ||
0e8cd71c | 922 | mutex_lock(&vha->vha_tgt.tgt_mutex); |
2d70c103 NB |
923 | 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); | |
0e8cd71c | 932 | mutex_unlock(&vha->vha_tgt.tgt_mutex); |
2d70c103 | 933 | |
0e8cd71c | 934 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished", |
2d70c103 NB |
935 | tgt); |
936 | } | |
937 | EXPORT_SYMBOL(qlt_stop_phase2); | |
938 | ||
939 | /* Called from qlt_remove_target() -> qla2x00_remove_one() */ | |
fa492630 | 940 | static void qlt_release(struct qla_tgt *tgt) |
2d70c103 | 941 | { |
0e8cd71c | 942 | scsi_qla_host_t *vha = tgt->vha; |
2d70c103 | 943 | |
0e8cd71c | 944 | if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped) |
2d70c103 NB |
945 | qlt_stop_phase2(tgt); |
946 | ||
0e8cd71c | 947 | vha->vha_tgt.qla_tgt = NULL; |
2d70c103 | 948 | |
0e8cd71c | 949 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d, |
2d70c103 NB |
950 | "Release of tgt %p finished\n", tgt); |
951 | ||
952 | kfree(tgt); | |
953 | } | |
954 | ||
955 | /* ha->hardware_lock supposed to be held on entry */ | |
956 | static 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 | */ | |
990 | static 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 | ||
0e8cd71c SK |
1013 | if (vha->vha_tgt.qla_tgt != NULL) |
1014 | vha->vha_tgt.qla_tgt->notify_ack_expected++; | |
2d70c103 NB |
1015 | |
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; | |
aa230bc5 | 1030 | nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle; |
2d70c103 NB |
1031 | 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 | */ | |
1049 | static 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 | ||
b6a029e1 | 1066 | resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL); |
2d70c103 NB |
1067 | 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 | ||
0e8cd71c | 1118 | vha->vha_tgt.qla_tgt->abts_resp_expected++; |
2d70c103 NB |
1119 | |
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 | */ | |
1126 | static 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 | ||
b6a029e1 | 1137 | ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL); |
2d70c103 NB |
1138 | 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); | |
33a5fcee | 1163 | ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id); |
2d70c103 NB |
1164 | |
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 */ | |
1172 | static 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; | |
06e97b48 | 1176 | struct se_session *se_sess = sess->se_sess; |
2d70c103 | 1177 | struct qla_tgt_mgmt_cmd *mcmd; |
06e97b48 SH |
1178 | struct se_cmd *se_cmd; |
1179 | u32 lun = 0; | |
2d70c103 | 1180 | int rc; |
06e97b48 SH |
1181 | 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; | |
2d70c103 NB |
1197 | |
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)); | |
80187f8e | 1213 | mcmd->reset_count = vha->hw->chip_reset; |
2d70c103 | 1214 | |
06e97b48 | 1215 | rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK, |
2d70c103 NB |
1216 | 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 | */ | |
1231 | static 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); | |
0e8cd71c | 1271 | rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt, |
2d70c103 NB |
1272 | 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 | */ | |
1293 | static 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; | |
33a5fcee | 1298 | uint16_t temp; |
2d70c103 NB |
1299 | |
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); | |
33a5fcee QT |
1329 | temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id); |
1330 | ctio->u.status1.ox_id = cpu_to_le16(temp); | |
2d70c103 NB |
1331 | 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); | |
e4b11b89 | 1334 | ctio->u.status1.sense_data[0] = resp_code; |
2d70c103 NB |
1335 | |
1336 | qla2x00_start_iocbs(ha, ha->req); | |
1337 | } | |
1338 | ||
1339 | void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd) | |
1340 | { | |
1341 | mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool); | |
1342 | } | |
1343 | EXPORT_SYMBOL(qlt_free_mcmd); | |
1344 | ||
1345 | /* callback from target fabric module code */ | |
1346 | void 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); | |
b6a029e1 AE |
1357 | |
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 | ||
2d70c103 NB |
1372 | 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 | } | |
1394 | EXPORT_SYMBOL(qlt_xmit_tm_rsp); | |
1395 | ||
1396 | /* No locks */ | |
1397 | static 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 | ||
f83adb61 QT |
1411 | 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 | } | |
2d70c103 | 1447 | |
2d70c103 NB |
1448 | return 0; |
1449 | ||
1450 | out_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 | ||
1457 | static 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; | |
f83adb61 QT |
1465 | |
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); | |
2d70c103 NB |
1475 | } |
1476 | ||
1477 | static int qlt_check_reserve_free_req(struct scsi_qla_host *vha, | |
1478 | uint32_t req_cnt) | |
1479 | { | |
d29fb736 | 1480 | uint32_t cnt, cnt_in; |
2d70c103 NB |
1481 | |
1482 | if (vha->req->cnt < (req_cnt + 2)) { | |
75554b68 | 1483 | cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out); |
d29fb736 | 1484 | cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in); |
2d70c103 | 1485 | |
2d70c103 NB |
1486 | 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))) { | |
667024a3 | 1494 | ql_dbg(ql_dbg_io, vha, 0x305a, |
d29fb736 SK |
1495 | "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); | |
2d70c103 NB |
1498 | 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 | */ | |
1508 | static 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 */ | |
1522 | static 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; | |
8d93f550 | 1531 | if (h > DEFAULT_OUTSTANDING_COMMANDS) |
2d70c103 NB |
1532 | h = 1; /* 0 is QLA_TGT_NULL_HANDLE */ |
1533 | if (h == ha->tgt.current_handle) { | |
667024a3 | 1534 | ql_dbg(ql_dbg_io, vha, 0x305b, |
2d70c103 NB |
1535 | "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 */ | |
1551 | static 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; | |
33a5fcee | 1558 | uint16_t temp; |
2d70c103 NB |
1559 | |
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); | |
33a5fcee QT |
1587 | temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id); |
1588 | pkt->u.status0.ox_id = cpu_to_le16(temp); | |
2d70c103 NB |
1589 | pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset); |
1590 | ||
2d70c103 NB |
1591 | 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 | */ | |
1598 | static 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 | ||
2d70c103 NB |
1648 | 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 | */ | |
1657 | static 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 | ||
2d70c103 NB |
1665 | 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 */ | |
2d70c103 NB |
1682 | |
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 | ||
2d70c103 NB |
1696 | prm->sg = sg_next(prm->sg); |
1697 | } | |
1698 | ||
1699 | qlt_load_cont_data_segments(prm, vha); | |
1700 | } | |
1701 | ||
1702 | static 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 | */ | |
1710 | static 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; | |
1726 | ||
1727 | qlt_send_term_exchange(vha, cmd, &cmd->atio, 0); | |
1728 | ||
1729 | /* !! At this point cmd could be already freed !! */ | |
1730 | return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED; | |
1731 | } | |
1732 | ||
2d70c103 NB |
1733 | prm->cmd = cmd; |
1734 | prm->tgt = tgt; | |
1735 | prm->rq_result = scsi_status; | |
1736 | prm->sense_buffer = &cmd->sense_buffer[0]; | |
1737 | prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER; | |
1738 | prm->sg = NULL; | |
1739 | prm->seg_cnt = -1; | |
1740 | prm->req_cnt = 1; | |
1741 | prm->add_status_pkt = 0; | |
1742 | ||
2d70c103 NB |
1743 | /* Send marker if required */ |
1744 | if (qlt_issue_marker(vha, 0) != QLA_SUCCESS) | |
1745 | return -EFAULT; | |
1746 | ||
2d70c103 NB |
1747 | if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) { |
1748 | if (qlt_pci_map_calc_cnt(prm) != 0) | |
1749 | return -EAGAIN; | |
1750 | } | |
1751 | ||
1752 | *full_req_cnt = prm->req_cnt; | |
1753 | ||
1754 | if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { | |
1755 | prm->residual = se_cmd->residual_count; | |
667024a3 | 1756 | ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c, |
2d70c103 NB |
1757 | "Residual underflow: %d (tag %d, " |
1758 | "op %x, bufflen %d, rq_result %x)\n", prm->residual, | |
1759 | cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0, | |
1760 | cmd->bufflen, prm->rq_result); | |
1761 | prm->rq_result |= SS_RESIDUAL_UNDER; | |
1762 | } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { | |
1763 | prm->residual = se_cmd->residual_count; | |
667024a3 | 1764 | ql_dbg(ql_dbg_io, vha, 0x305d, |
2d70c103 NB |
1765 | "Residual overflow: %d (tag %d, " |
1766 | "op %x, bufflen %d, rq_result %x)\n", prm->residual, | |
1767 | cmd->tag, se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0, | |
1768 | cmd->bufflen, prm->rq_result); | |
1769 | prm->rq_result |= SS_RESIDUAL_OVER; | |
1770 | } | |
1771 | ||
1772 | if (xmit_type & QLA_TGT_XMIT_STATUS) { | |
1773 | /* | |
1774 | * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be | |
1775 | * ignored in *xmit_response() below | |
1776 | */ | |
1777 | if (qlt_has_data(cmd)) { | |
1778 | if (QLA_TGT_SENSE_VALID(prm->sense_buffer) || | |
1779 | (IS_FWI2_CAPABLE(ha) && | |
1780 | (prm->rq_result != 0))) { | |
1781 | prm->add_status_pkt = 1; | |
1782 | (*full_req_cnt)++; | |
1783 | } | |
1784 | } | |
1785 | } | |
1786 | ||
2d70c103 NB |
1787 | return 0; |
1788 | } | |
1789 | ||
1790 | static inline int qlt_need_explicit_conf(struct qla_hw_data *ha, | |
1791 | struct qla_tgt_cmd *cmd, int sending_sense) | |
1792 | { | |
1793 | if (ha->tgt.enable_class_2) | |
1794 | return 0; | |
1795 | ||
1796 | if (sending_sense) | |
1797 | return cmd->conf_compl_supported; | |
1798 | else | |
1799 | return ha->tgt.enable_explicit_conf && | |
1800 | cmd->conf_compl_supported; | |
1801 | } | |
1802 | ||
1803 | #ifdef CONFIG_QLA_TGT_DEBUG_SRR | |
1804 | /* | |
1805 | * Original taken from the XFS code | |
1806 | */ | |
1807 | static unsigned long qlt_srr_random(void) | |
1808 | { | |
1809 | static int Inited; | |
1810 | static unsigned long RandomValue; | |
1811 | static DEFINE_SPINLOCK(lock); | |
1812 | /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */ | |
1813 | register long rv; | |
1814 | register long lo; | |
1815 | register long hi; | |
1816 | unsigned long flags; | |
1817 | ||
1818 | spin_lock_irqsave(&lock, flags); | |
1819 | if (!Inited) { | |
1820 | RandomValue = jiffies; | |
1821 | Inited = 1; | |
1822 | } | |
1823 | rv = RandomValue; | |
1824 | hi = rv / 127773; | |
1825 | lo = rv % 127773; | |
1826 | rv = 16807 * lo - 2836 * hi; | |
1827 | if (rv <= 0) | |
1828 | rv += 2147483647; | |
1829 | RandomValue = rv; | |
1830 | spin_unlock_irqrestore(&lock, flags); | |
1831 | return rv; | |
1832 | } | |
1833 | ||
1834 | static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type) | |
1835 | { | |
1836 | #if 0 /* This is not a real status packets lost, so it won't lead to SRR */ | |
1837 | if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200) | |
1838 | == 50) { | |
1839 | *xmit_type &= ~QLA_TGT_XMIT_STATUS; | |
1840 | ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015, | |
1841 | "Dropping cmd %p (tag %d) status", cmd, cmd->tag); | |
1842 | } | |
1843 | #endif | |
1844 | /* | |
1845 | * It's currently not possible to simulate SRRs for FCP_WRITE without | |
1846 | * a physical link layer failure, so don't even try here.. | |
1847 | */ | |
1848 | if (cmd->dma_data_direction != DMA_FROM_DEVICE) | |
1849 | return; | |
1850 | ||
1851 | if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) && | |
1852 | ((qlt_srr_random() % 100) == 20)) { | |
1853 | int i, leave = 0; | |
1854 | unsigned int tot_len = 0; | |
1855 | ||
1856 | while (leave == 0) | |
1857 | leave = qlt_srr_random() % cmd->sg_cnt; | |
1858 | ||
1859 | for (i = 0; i < leave; i++) | |
1860 | tot_len += cmd->sg[i].length; | |
1861 | ||
1862 | ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016, | |
1863 | "Cutting cmd %p (tag %d) buffer" | |
1864 | " tail to len %d, sg_cnt %d (cmd->bufflen %d," | |
1865 | " cmd->sg_cnt %d)", cmd, cmd->tag, tot_len, leave, | |
1866 | cmd->bufflen, cmd->sg_cnt); | |
1867 | ||
1868 | cmd->bufflen = tot_len; | |
1869 | cmd->sg_cnt = leave; | |
1870 | } | |
1871 | ||
1872 | if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) { | |
1873 | unsigned int offset = qlt_srr_random() % cmd->bufflen; | |
1874 | ||
1875 | ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017, | |
1876 | "Cutting cmd %p (tag %d) buffer head " | |
1877 | "to offset %d (cmd->bufflen %d)", cmd, cmd->tag, offset, | |
1878 | cmd->bufflen); | |
1879 | if (offset == 0) | |
1880 | *xmit_type &= ~QLA_TGT_XMIT_DATA; | |
1881 | else if (qlt_set_data_offset(cmd, offset)) { | |
1882 | ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018, | |
1883 | "qlt_set_data_offset() failed (tag %d)", cmd->tag); | |
1884 | } | |
1885 | } | |
1886 | } | |
1887 | #else | |
1888 | static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type) | |
1889 | {} | |
1890 | #endif | |
1891 | ||
1892 | static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio, | |
1893 | struct qla_tgt_prm *prm) | |
1894 | { | |
1895 | prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len, | |
1896 | (uint32_t)sizeof(ctio->u.status1.sense_data)); | |
1897 | ctio->u.status0.flags |= | |
1898 | __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS); | |
1899 | if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) { | |
1900 | ctio->u.status0.flags |= __constant_cpu_to_le16( | |
1901 | CTIO7_FLAGS_EXPLICIT_CONFORM | | |
1902 | CTIO7_FLAGS_CONFORM_REQ); | |
1903 | } | |
1904 | ctio->u.status0.residual = cpu_to_le32(prm->residual); | |
1905 | ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result); | |
1906 | if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) { | |
1907 | int i; | |
1908 | ||
1909 | if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) { | |
1910 | if (prm->cmd->se_cmd.scsi_status != 0) { | |
1911 | ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017, | |
1912 | "Skipping EXPLICIT_CONFORM and " | |
1913 | "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ " | |
1914 | "non GOOD status\n"); | |
1915 | goto skip_explict_conf; | |
1916 | } | |
1917 | ctio->u.status1.flags |= __constant_cpu_to_le16( | |
1918 | CTIO7_FLAGS_EXPLICIT_CONFORM | | |
1919 | CTIO7_FLAGS_CONFORM_REQ); | |
1920 | } | |
1921 | skip_explict_conf: | |
1922 | ctio->u.status1.flags &= | |
1923 | ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0); | |
1924 | ctio->u.status1.flags |= | |
1925 | __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1); | |
1926 | ctio->u.status1.scsi_status |= | |
1927 | __constant_cpu_to_le16(SS_SENSE_LEN_VALID); | |
1928 | ctio->u.status1.sense_length = | |
1929 | cpu_to_le16(prm->sense_buffer_len); | |
1930 | for (i = 0; i < prm->sense_buffer_len/4; i++) | |
1931 | ((uint32_t *)ctio->u.status1.sense_data)[i] = | |
1932 | cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]); | |
1933 | #if 0 | |
1934 | if (unlikely((prm->sense_buffer_len % 4) != 0)) { | |
1935 | static int q; | |
1936 | if (q < 10) { | |
1937 | ql_dbg(ql_dbg_tgt, vha, 0xe04f, | |
1938 | "qla_target(%d): %d bytes of sense " | |
1939 | "lost", prm->tgt->ha->vp_idx, | |
1940 | prm->sense_buffer_len % 4); | |
1941 | q++; | |
1942 | } | |
1943 | } | |
1944 | #endif | |
1945 | } else { | |
1946 | ctio->u.status1.flags &= | |
1947 | ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0); | |
1948 | ctio->u.status1.flags |= | |
1949 | __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1); | |
1950 | ctio->u.status1.sense_length = 0; | |
1951 | memset(ctio->u.status1.sense_data, 0, | |
1952 | sizeof(ctio->u.status1.sense_data)); | |
1953 | } | |
1954 | ||
1955 | /* Sense with len > 24, is it possible ??? */ | |
1956 | } | |
1957 | ||
f83adb61 QT |
1958 | |
1959 | ||
1960 | /* diff */ | |
1961 | static inline int | |
1962 | qlt_hba_err_chk_enabled(struct se_cmd *se_cmd) | |
1963 | { | |
1964 | /* | |
1965 | * Uncomment when corresponding SCSI changes are done. | |
1966 | * | |
1967 | if (!sp->cmd->prot_chk) | |
1968 | return 0; | |
1969 | * | |
1970 | */ | |
1971 | switch (se_cmd->prot_op) { | |
1972 | case TARGET_PROT_DOUT_INSERT: | |
1973 | case TARGET_PROT_DIN_STRIP: | |
1974 | if (ql2xenablehba_err_chk >= 1) | |
1975 | return 1; | |
1976 | break; | |
1977 | case TARGET_PROT_DOUT_PASS: | |
1978 | case TARGET_PROT_DIN_PASS: | |
1979 | if (ql2xenablehba_err_chk >= 2) | |
1980 | return 1; | |
1981 | break; | |
1982 | case TARGET_PROT_DIN_INSERT: | |
1983 | case TARGET_PROT_DOUT_STRIP: | |
1984 | return 1; | |
1985 | default: | |
1986 | break; | |
1987 | } | |
1988 | return 0; | |
1989 | } | |
1990 | ||
1991 | /* | |
1992 | * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command | |
1993 | * | |
1994 | */ | |
1995 | static inline void | |
1996 | qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx) | |
1997 | { | |
1998 | uint32_t lba = 0xffffffff & se_cmd->t_task_lba; | |
1999 | ||
2000 | /* wait til Mode Sense/Select cmd, modepage Ah, subpage 2 | |
2001 | * have been immplemented by TCM, before AppTag is avail. | |
2002 | * Look for modesense_handlers[] | |
2003 | */ | |
c7ee3bd4 | 2004 | ctx->app_tag = 0; |
f83adb61 QT |
2005 | ctx->app_tag_mask[0] = 0x0; |
2006 | ctx->app_tag_mask[1] = 0x0; | |
2007 | ||
2008 | switch (se_cmd->prot_type) { | |
2009 | case TARGET_DIF_TYPE0_PROT: | |
2010 | /* | |
2011 | * No check for ql2xenablehba_err_chk, as it would be an | |
2012 | * I/O error if hba tag generation is not done. | |
2013 | */ | |
2014 | ctx->ref_tag = cpu_to_le32(lba); | |
2015 | ||
2016 | if (!qlt_hba_err_chk_enabled(se_cmd)) | |
2017 | break; | |
2018 | ||
2019 | /* enable ALL bytes of the ref tag */ | |
2020 | ctx->ref_tag_mask[0] = 0xff; | |
2021 | ctx->ref_tag_mask[1] = 0xff; | |
2022 | ctx->ref_tag_mask[2] = 0xff; | |
2023 | ctx->ref_tag_mask[3] = 0xff; | |
2024 | break; | |
2025 | /* | |
2026 | * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and | |
2027 | * 16 bit app tag. | |
2028 | */ | |
2029 | case TARGET_DIF_TYPE1_PROT: | |
2030 | ctx->ref_tag = cpu_to_le32(lba); | |
2031 | ||
2032 | if (!qlt_hba_err_chk_enabled(se_cmd)) | |
2033 | break; | |
2034 | ||
2035 | /* enable ALL bytes of the ref tag */ | |
2036 | ctx->ref_tag_mask[0] = 0xff; | |
2037 | ctx->ref_tag_mask[1] = 0xff; | |
2038 | ctx->ref_tag_mask[2] = 0xff; | |
2039 | ctx->ref_tag_mask[3] = 0xff; | |
2040 | break; | |
2041 | /* | |
2042 | * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to | |
2043 | * match LBA in CDB + N | |
2044 | */ | |
2045 | case TARGET_DIF_TYPE2_PROT: | |
2046 | ctx->ref_tag = cpu_to_le32(lba); | |
2047 | ||
2048 | if (!qlt_hba_err_chk_enabled(se_cmd)) | |
2049 | break; | |
2050 | ||
2051 | /* enable ALL bytes of the ref tag */ | |
2052 | ctx->ref_tag_mask[0] = 0xff; | |
2053 | ctx->ref_tag_mask[1] = 0xff; | |
2054 | ctx->ref_tag_mask[2] = 0xff; | |
2055 | ctx->ref_tag_mask[3] = 0xff; | |
2056 | break; | |
2057 | ||
2058 | /* For Type 3 protection: 16 bit GUARD only */ | |
2059 | case TARGET_DIF_TYPE3_PROT: | |
2060 | ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] = | |
2061 | ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00; | |
2062 | break; | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | ||
2067 | static inline int | |
2068 | qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha) | |
2069 | { | |
2070 | uint32_t *cur_dsd; | |
2071 | int sgc; | |
2072 | uint32_t transfer_length = 0; | |
2073 | uint32_t data_bytes; | |
2074 | uint32_t dif_bytes; | |
2075 | uint8_t bundling = 1; | |
2076 | uint8_t *clr_ptr; | |
2077 | struct crc_context *crc_ctx_pkt = NULL; | |
2078 | struct qla_hw_data *ha; | |
2079 | struct ctio_crc2_to_fw *pkt; | |
2080 | dma_addr_t crc_ctx_dma; | |
2081 | uint16_t fw_prot_opts = 0; | |
2082 | struct qla_tgt_cmd *cmd = prm->cmd; | |
2083 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
2084 | uint32_t h; | |
2085 | struct atio_from_isp *atio = &prm->cmd->atio; | |
c7ee3bd4 | 2086 | uint16_t t16; |
f83adb61 QT |
2087 | |
2088 | sgc = 0; | |
2089 | ha = vha->hw; | |
2090 | ||
2091 | pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr; | |
2092 | prm->pkt = pkt; | |
2093 | memset(pkt, 0, sizeof(*pkt)); | |
2094 | ||
2095 | ql_dbg(ql_dbg_tgt, vha, 0xe071, | |
2096 | "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n", | |
2097 | vha->vp_idx, __func__, se_cmd, se_cmd->prot_op, | |
2098 | prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba); | |
2099 | ||
2100 | if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) || | |
2101 | (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP)) | |
2102 | bundling = 0; | |
2103 | ||
2104 | /* Compute dif len and adjust data len to incude protection */ | |
2105 | data_bytes = cmd->bufflen; | |
2106 | dif_bytes = (data_bytes / cmd->blk_sz) * 8; | |
2107 | ||
2108 | switch (se_cmd->prot_op) { | |
2109 | case TARGET_PROT_DIN_INSERT: | |
2110 | case TARGET_PROT_DOUT_STRIP: | |
2111 | transfer_length = data_bytes; | |
2112 | data_bytes += dif_bytes; | |
2113 | break; | |
2114 | ||
2115 | case TARGET_PROT_DIN_STRIP: | |
2116 | case TARGET_PROT_DOUT_INSERT: | |
2117 | case TARGET_PROT_DIN_PASS: | |
2118 | case TARGET_PROT_DOUT_PASS: | |
2119 | transfer_length = data_bytes + dif_bytes; | |
2120 | break; | |
2121 | ||
2122 | default: | |
2123 | BUG(); | |
2124 | break; | |
2125 | } | |
2126 | ||
2127 | if (!qlt_hba_err_chk_enabled(se_cmd)) | |
2128 | fw_prot_opts |= 0x10; /* Disable Guard tag checking */ | |
2129 | /* HBA error checking enabled */ | |
2130 | else if (IS_PI_UNINIT_CAPABLE(ha)) { | |
2131 | if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) || | |
2132 | (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)) | |
2133 | fw_prot_opts |= PO_DIS_VALD_APP_ESC; | |
2134 | else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT) | |
2135 | fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC; | |
2136 | } | |
2137 | ||
2138 | switch (se_cmd->prot_op) { | |
2139 | case TARGET_PROT_DIN_INSERT: | |
2140 | case TARGET_PROT_DOUT_INSERT: | |
2141 | fw_prot_opts |= PO_MODE_DIF_INSERT; | |
2142 | break; | |
2143 | case TARGET_PROT_DIN_STRIP: | |
2144 | case TARGET_PROT_DOUT_STRIP: | |
2145 | fw_prot_opts |= PO_MODE_DIF_REMOVE; | |
2146 | break; | |
2147 | case TARGET_PROT_DIN_PASS: | |
2148 | case TARGET_PROT_DOUT_PASS: | |
2149 | fw_prot_opts |= PO_MODE_DIF_PASS; | |
2150 | /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */ | |
2151 | break; | |
2152 | default:/* Normal Request */ | |
2153 | fw_prot_opts |= PO_MODE_DIF_PASS; | |
2154 | break; | |
2155 | } | |
2156 | ||
2157 | ||
2158 | /* ---- PKT ---- */ | |
2159 | /* Update entry type to indicate Command Type CRC_2 IOCB */ | |
2160 | pkt->entry_type = CTIO_CRC2; | |
2161 | pkt->entry_count = 1; | |
2162 | pkt->vp_index = vha->vp_idx; | |
2163 | ||
2164 | h = qlt_make_handle(vha); | |
2165 | if (unlikely(h == QLA_TGT_NULL_HANDLE)) { | |
2166 | /* | |
2167 | * CTIO type 7 from the firmware doesn't provide a way to | |
2168 | * know the initiator's LOOP ID, hence we can't find | |
2169 | * the session and, so, the command. | |
2170 | */ | |
2171 | return -EAGAIN; | |
2172 | } else | |
2173 | ha->tgt.cmds[h-1] = prm->cmd; | |
2174 | ||
2175 | ||
2176 | pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK; | |
2177 | pkt->nport_handle = prm->cmd->loop_id; | |
2178 | pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT); | |
2179 | pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2]; | |
2180 | pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1]; | |
2181 | pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0]; | |
2182 | pkt->exchange_addr = atio->u.isp24.exchange_addr; | |
c7ee3bd4 QT |
2183 | |
2184 | /* silence compile warning */ | |
2185 | t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id); | |
2186 | pkt->ox_id = cpu_to_le16(t16); | |
2187 | ||
2188 | t16 = (atio->u.isp24.attr << 9); | |
2189 | pkt->flags |= cpu_to_le16(t16); | |
f83adb61 QT |
2190 | pkt->relative_offset = cpu_to_le32(prm->cmd->offset); |
2191 | ||
2192 | /* Set transfer direction */ | |
2193 | if (cmd->dma_data_direction == DMA_TO_DEVICE) | |
2194 | pkt->flags = __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN); | |
2195 | else if (cmd->dma_data_direction == DMA_FROM_DEVICE) | |
2196 | pkt->flags = __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT); | |
2197 | ||
2198 | ||
2199 | pkt->dseg_count = prm->tot_dsds; | |
2200 | /* Fibre channel byte count */ | |
2201 | pkt->transfer_length = cpu_to_le32(transfer_length); | |
2202 | ||
2203 | ||
2204 | /* ----- CRC context -------- */ | |
2205 | ||
2206 | /* Allocate CRC context from global pool */ | |
2207 | crc_ctx_pkt = cmd->ctx = | |
2208 | dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma); | |
2209 | ||
2210 | if (!crc_ctx_pkt) | |
2211 | goto crc_queuing_error; | |
2212 | ||
2213 | /* Zero out CTX area. */ | |
2214 | clr_ptr = (uint8_t *)crc_ctx_pkt; | |
2215 | memset(clr_ptr, 0, sizeof(*crc_ctx_pkt)); | |
2216 | ||
2217 | crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma; | |
2218 | INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list); | |
2219 | ||
2220 | /* Set handle */ | |
2221 | crc_ctx_pkt->handle = pkt->handle; | |
2222 | ||
2223 | qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt); | |
2224 | ||
2225 | pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma)); | |
2226 | pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma)); | |
2227 | pkt->crc_context_len = CRC_CONTEXT_LEN_FW; | |
2228 | ||
2229 | ||
2230 | if (!bundling) { | |
2231 | cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address; | |
2232 | } else { | |
2233 | /* | |
2234 | * Configure Bundling if we need to fetch interlaving | |
2235 | * protection PCI accesses | |
2236 | */ | |
2237 | fw_prot_opts |= PO_ENABLE_DIF_BUNDLING; | |
2238 | crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes); | |
2239 | crc_ctx_pkt->u.bundling.dseg_count = | |
2240 | cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt); | |
2241 | cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address; | |
2242 | } | |
2243 | ||
2244 | /* Finish the common fields of CRC pkt */ | |
2245 | crc_ctx_pkt->blk_size = cpu_to_le16(cmd->blk_sz); | |
2246 | crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts); | |
2247 | crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes); | |
2248 | crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0); | |
2249 | ||
2250 | ||
2251 | /* Walks data segments */ | |
2252 | pkt->flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DSD_PTR); | |
2253 | ||
2254 | if (!bundling && prm->prot_seg_cnt) { | |
2255 | if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd, | |
2256 | prm->tot_dsds, cmd)) | |
2257 | goto crc_queuing_error; | |
2258 | } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd, | |
2259 | (prm->tot_dsds - prm->prot_seg_cnt), cmd)) | |
2260 | goto crc_queuing_error; | |
2261 | ||
2262 | if (bundling && prm->prot_seg_cnt) { | |
2263 | /* Walks dif segments */ | |
c7ee3bd4 | 2264 | pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA; |
f83adb61 QT |
2265 | |
2266 | cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address; | |
2267 | if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd, | |
2268 | prm->prot_seg_cnt, cmd)) | |
2269 | goto crc_queuing_error; | |
2270 | } | |
2271 | return QLA_SUCCESS; | |
2272 | ||
2273 | crc_queuing_error: | |
2274 | /* Cleanup will be performed by the caller */ | |
2275 | ||
2276 | return QLA_FUNCTION_FAILED; | |
2277 | } | |
2278 | ||
2279 | ||
2d70c103 NB |
2280 | /* |
2281 | * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and * | |
2282 | * QLA_TGT_XMIT_STATUS for >= 24xx silicon | |
2283 | */ | |
2284 | int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type, | |
2285 | uint8_t scsi_status) | |
2286 | { | |
2287 | struct scsi_qla_host *vha = cmd->vha; | |
2288 | struct qla_hw_data *ha = vha->hw; | |
2289 | struct ctio7_to_24xx *pkt; | |
2290 | struct qla_tgt_prm prm; | |
2291 | uint32_t full_req_cnt = 0; | |
2292 | unsigned long flags = 0; | |
2293 | int res; | |
2294 | ||
2295 | memset(&prm, 0, sizeof(prm)); | |
2296 | qlt_check_srr_debug(cmd, &xmit_type); | |
2297 | ||
2298 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018, | |
f83adb61 QT |
2299 | "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n", |
2300 | (xmit_type & QLA_TGT_XMIT_STATUS) ? | |
2301 | 1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction, | |
2302 | &cmd->se_cmd); | |
2d70c103 NB |
2303 | |
2304 | res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status, | |
2305 | &full_req_cnt); | |
2306 | if (unlikely(res != 0)) { | |
2307 | if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED) | |
2308 | return 0; | |
2309 | ||
2310 | return res; | |
2311 | } | |
2312 | ||
2313 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
2314 | ||
b6a029e1 AE |
2315 | if (qla2x00_reset_active(vha) || cmd->reset_count != ha->chip_reset) { |
2316 | /* | |
2317 | * Either a chip reset is active or this request was from | |
2318 | * previous life, just abort the processing. | |
2319 | */ | |
2320 | cmd->state = QLA_TGT_STATE_PROCESSED; | |
2321 | qlt_abort_cmd_on_host_reset(cmd->vha, cmd); | |
2322 | ql_dbg(ql_dbg_async, vha, 0xe101, | |
2323 | "RESET-RSP active/old-count/new-count = %d/%d/%d.\n", | |
2324 | qla2x00_reset_active(vha), cmd->reset_count, | |
2325 | ha->chip_reset); | |
2326 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
2327 | return 0; | |
2328 | } | |
2329 | ||
2d70c103 NB |
2330 | /* Does F/W have an IOCBs for this request */ |
2331 | res = qlt_check_reserve_free_req(vha, full_req_cnt); | |
2332 | if (unlikely(res)) | |
2333 | goto out_unmap_unlock; | |
2334 | ||
f83adb61 QT |
2335 | if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA)) |
2336 | res = qlt_build_ctio_crc2_pkt(&prm, vha); | |
2337 | else | |
2338 | res = qlt_24xx_build_ctio_pkt(&prm, vha); | |
2d70c103 NB |
2339 | if (unlikely(res != 0)) |
2340 | goto out_unmap_unlock; | |
2341 | ||
2342 | ||
2343 | pkt = (struct ctio7_to_24xx *)prm.pkt; | |
2344 | ||
2345 | if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) { | |
2346 | pkt->u.status0.flags |= | |
2347 | __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN | | |
2348 | CTIO7_FLAGS_STATUS_MODE_0); | |
2349 | ||
f83adb61 QT |
2350 | if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) |
2351 | qlt_load_data_segments(&prm, vha); | |
2d70c103 NB |
2352 | |
2353 | if (prm.add_status_pkt == 0) { | |
2354 | if (xmit_type & QLA_TGT_XMIT_STATUS) { | |
2355 | pkt->u.status0.scsi_status = | |
2356 | cpu_to_le16(prm.rq_result); | |
2357 | pkt->u.status0.residual = | |
2358 | cpu_to_le32(prm.residual); | |
2359 | pkt->u.status0.flags |= __constant_cpu_to_le16( | |
2360 | CTIO7_FLAGS_SEND_STATUS); | |
2361 | if (qlt_need_explicit_conf(ha, cmd, 0)) { | |
2362 | pkt->u.status0.flags |= | |
2363 | __constant_cpu_to_le16( | |
2364 | CTIO7_FLAGS_EXPLICIT_CONFORM | | |
2365 | CTIO7_FLAGS_CONFORM_REQ); | |
2366 | } | |
2367 | } | |
2368 | ||
2369 | } else { | |
2370 | /* | |
2371 | * We have already made sure that there is sufficient | |
2372 | * amount of request entries to not drop HW lock in | |
2373 | * req_pkt(). | |
2374 | */ | |
2375 | struct ctio7_to_24xx *ctio = | |
2376 | (struct ctio7_to_24xx *)qlt_get_req_pkt(vha); | |
2377 | ||
667024a3 AE |
2378 | ql_dbg(ql_dbg_io, vha, 0x305e, |
2379 | "Building additional status packet 0x%p.\n", | |
2380 | ctio); | |
2d70c103 | 2381 | |
f83adb61 QT |
2382 | /* |
2383 | * T10Dif: ctio_crc2_to_fw overlay ontop of | |
2384 | * ctio7_to_24xx | |
2385 | */ | |
2d70c103 | 2386 | memcpy(ctio, pkt, sizeof(*ctio)); |
f83adb61 | 2387 | /* reset back to CTIO7 */ |
2d70c103 | 2388 | ctio->entry_count = 1; |
f83adb61 | 2389 | ctio->entry_type = CTIO_TYPE7; |
2d70c103 NB |
2390 | ctio->dseg_count = 0; |
2391 | ctio->u.status1.flags &= ~__constant_cpu_to_le16( | |
2392 | CTIO7_FLAGS_DATA_IN); | |
2393 | ||
2394 | /* Real finish is ctio_m1's finish */ | |
2395 | pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK; | |
2396 | pkt->u.status0.flags |= __constant_cpu_to_le16( | |
2397 | CTIO7_FLAGS_DONT_RET_CTIO); | |
f83adb61 QT |
2398 | |
2399 | /* qlt_24xx_init_ctio_to_isp will correct | |
2400 | * all neccessary fields that's part of CTIO7. | |
2401 | * There should be no residual of CTIO-CRC2 data. | |
2402 | */ | |
2d70c103 NB |
2403 | qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio, |
2404 | &prm); | |
2405 | pr_debug("Status CTIO7: %p\n", ctio); | |
2406 | } | |
2407 | } else | |
2408 | qlt_24xx_init_ctio_to_isp(pkt, &prm); | |
2409 | ||
2410 | ||
2411 | cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */ | |
2412 | ||
2d70c103 NB |
2413 | qla2x00_start_iocbs(vha, vha->req); |
2414 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
2415 | ||
2416 | return 0; | |
2417 | ||
2418 | out_unmap_unlock: | |
2419 | if (cmd->sg_mapped) | |
2420 | qlt_unmap_sg(vha, cmd); | |
2421 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
2422 | ||
2423 | return res; | |
2424 | } | |
2425 | EXPORT_SYMBOL(qlt_xmit_response); | |
2426 | ||
2427 | int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd) | |
2428 | { | |
2429 | struct ctio7_to_24xx *pkt; | |
2430 | struct scsi_qla_host *vha = cmd->vha; | |
2431 | struct qla_hw_data *ha = vha->hw; | |
2432 | struct qla_tgt *tgt = cmd->tgt; | |
2433 | struct qla_tgt_prm prm; | |
2434 | unsigned long flags; | |
2435 | int res = 0; | |
2436 | ||
2437 | memset(&prm, 0, sizeof(prm)); | |
2438 | prm.cmd = cmd; | |
2439 | prm.tgt = tgt; | |
2440 | prm.sg = NULL; | |
2441 | prm.req_cnt = 1; | |
2442 | ||
2443 | /* Send marker if required */ | |
2444 | if (qlt_issue_marker(vha, 0) != QLA_SUCCESS) | |
2445 | return -EIO; | |
2446 | ||
2d70c103 NB |
2447 | /* Calculate number of entries and segments required */ |
2448 | if (qlt_pci_map_calc_cnt(&prm) != 0) | |
2449 | return -EAGAIN; | |
2450 | ||
2451 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
2452 | ||
b6a029e1 AE |
2453 | if (qla2x00_reset_active(vha) || cmd->reset_count != ha->chip_reset) { |
2454 | /* | |
2455 | * Either a chip reset is active or this request was from | |
2456 | * previous life, just abort the processing. | |
2457 | */ | |
2458 | cmd->state = QLA_TGT_STATE_NEED_DATA; | |
2459 | qlt_abort_cmd_on_host_reset(cmd->vha, cmd); | |
2460 | ql_dbg(ql_dbg_async, vha, 0xe102, | |
2461 | "RESET-XFR active/old-count/new-count = %d/%d/%d.\n", | |
2462 | qla2x00_reset_active(vha), cmd->reset_count, | |
2463 | ha->chip_reset); | |
2464 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
2465 | return 0; | |
2466 | } | |
2467 | ||
2d70c103 NB |
2468 | /* Does F/W have an IOCBs for this request */ |
2469 | res = qlt_check_reserve_free_req(vha, prm.req_cnt); | |
2470 | if (res != 0) | |
2471 | goto out_unlock_free_unmap; | |
f83adb61 QT |
2472 | if (cmd->se_cmd.prot_op) |
2473 | res = qlt_build_ctio_crc2_pkt(&prm, vha); | |
2474 | else | |
2475 | res = qlt_24xx_build_ctio_pkt(&prm, vha); | |
2d70c103 | 2476 | |
2d70c103 NB |
2477 | if (unlikely(res != 0)) |
2478 | goto out_unlock_free_unmap; | |
2479 | pkt = (struct ctio7_to_24xx *)prm.pkt; | |
2480 | pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT | | |
2481 | CTIO7_FLAGS_STATUS_MODE_0); | |
f83adb61 QT |
2482 | |
2483 | if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) | |
2484 | qlt_load_data_segments(&prm, vha); | |
2d70c103 NB |
2485 | |
2486 | cmd->state = QLA_TGT_STATE_NEED_DATA; | |
2487 | ||
2488 | qla2x00_start_iocbs(vha, vha->req); | |
2489 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
2490 | ||
2491 | return res; | |
2492 | ||
2493 | out_unlock_free_unmap: | |
2494 | if (cmd->sg_mapped) | |
2495 | qlt_unmap_sg(vha, cmd); | |
2496 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
2497 | ||
2498 | return res; | |
2499 | } | |
2500 | EXPORT_SYMBOL(qlt_rdy_to_xfer); | |
2501 | ||
f83adb61 QT |
2502 | |
2503 | /* | |
2504 | * Checks the guard or meta-data for the type of error | |
2505 | * detected by the HBA. | |
2506 | */ | |
2507 | static inline int | |
2508 | qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd, | |
2509 | struct ctio_crc_from_fw *sts) | |
2510 | { | |
2511 | uint8_t *ap = &sts->actual_dif[0]; | |
2512 | uint8_t *ep = &sts->expected_dif[0]; | |
2513 | uint32_t e_ref_tag, a_ref_tag; | |
2514 | uint16_t e_app_tag, a_app_tag; | |
2515 | uint16_t e_guard, a_guard; | |
2516 | uint64_t lba = cmd->se_cmd.t_task_lba; | |
2517 | ||
2518 | a_guard = be16_to_cpu(*(uint16_t *)(ap + 0)); | |
2519 | a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2)); | |
2520 | a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4)); | |
2521 | ||
2522 | e_guard = be16_to_cpu(*(uint16_t *)(ep + 0)); | |
2523 | e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2)); | |
2524 | e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4)); | |
2525 | ||
2526 | ql_dbg(ql_dbg_tgt, vha, 0xe075, | |
2527 | "iocb(s) %p Returned STATUS.\n", sts); | |
2528 | ||
2529 | ql_dbg(ql_dbg_tgt, vha, 0xf075, | |
fc385045 | 2530 | "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", |
f83adb61 QT |
2531 | cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba, |
2532 | a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard); | |
2533 | ||
2534 | /* | |
2535 | * Ignore sector if: | |
2536 | * For type 3: ref & app tag is all 'f's | |
2537 | * For type 0,1,2: app tag is all 'f's | |
2538 | */ | |
2539 | if ((a_app_tag == 0xffff) && | |
2540 | ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) || | |
2541 | (a_ref_tag == 0xffffffff))) { | |
2542 | uint32_t blocks_done; | |
2543 | ||
2544 | /* 2TB boundary case covered automatically with this */ | |
2545 | blocks_done = e_ref_tag - (uint32_t)lba + 1; | |
2546 | cmd->se_cmd.bad_sector = e_ref_tag; | |
2547 | cmd->se_cmd.pi_err = 0; | |
2548 | ql_dbg(ql_dbg_tgt, vha, 0xf074, | |
2549 | "need to return scsi good\n"); | |
2550 | ||
2551 | /* Update protection tag */ | |
2552 | if (cmd->prot_sg_cnt) { | |
2553 | uint32_t i, j = 0, k = 0, num_ent; | |
2554 | struct scatterlist *sg, *sgl; | |
2555 | ||
2556 | ||
2557 | sgl = cmd->prot_sg; | |
2558 | ||
2559 | /* Patch the corresponding protection tags */ | |
2560 | for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) { | |
2561 | num_ent = sg_dma_len(sg) / 8; | |
2562 | if (k + num_ent < blocks_done) { | |
2563 | k += num_ent; | |
2564 | continue; | |
2565 | } | |
2566 | j = blocks_done - k - 1; | |
2567 | k = blocks_done; | |
2568 | break; | |
2569 | } | |
2570 | ||
2571 | if (k != blocks_done) { | |
2572 | ql_log(ql_log_warn, vha, 0xf076, | |
2573 | "unexpected tag values tag:lba=%u:%llu)\n", | |
2574 | e_ref_tag, (unsigned long long)lba); | |
2575 | goto out; | |
2576 | } | |
2577 | ||
2578 | #if 0 | |
2579 | struct sd_dif_tuple *spt; | |
2580 | /* TODO: | |
2581 | * This section came from initiator. Is it valid here? | |
2582 | * should ulp be override with actual val??? | |
2583 | */ | |
2584 | spt = page_address(sg_page(sg)) + sg->offset; | |
2585 | spt += j; | |
2586 | ||
2587 | spt->app_tag = 0xffff; | |
2588 | if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3) | |
2589 | spt->ref_tag = 0xffffffff; | |
2590 | #endif | |
2591 | } | |
2592 | ||
2593 | return 0; | |
2594 | } | |
2595 | ||
2596 | /* check guard */ | |
2597 | if (e_guard != a_guard) { | |
2598 | cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED; | |
2599 | cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba; | |
2600 | ||
2601 | ql_log(ql_log_warn, vha, 0xe076, | |
2602 | "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", | |
2603 | cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba, | |
2604 | a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, | |
2605 | a_guard, e_guard, cmd); | |
2606 | goto out; | |
2607 | } | |
2608 | ||
2609 | /* check ref tag */ | |
2610 | if (e_ref_tag != a_ref_tag) { | |
2611 | cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED; | |
2612 | cmd->se_cmd.bad_sector = e_ref_tag; | |
2613 | ||
2614 | ql_log(ql_log_warn, vha, 0xe077, | |
2615 | "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", | |
2616 | cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba, | |
2617 | a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, | |
2618 | a_guard, e_guard, cmd); | |
2619 | goto out; | |
2620 | } | |
2621 | ||
2622 | /* check appl tag */ | |
2623 | if (e_app_tag != a_app_tag) { | |
2624 | cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED; | |
2625 | cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba; | |
2626 | ||
2627 | ql_log(ql_log_warn, vha, 0xe078, | |
2628 | "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", | |
2629 | cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba, | |
2630 | a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, | |
2631 | a_guard, e_guard, cmd); | |
2632 | goto out; | |
2633 | } | |
2634 | out: | |
2635 | return 1; | |
2636 | } | |
2637 | ||
2638 | ||
2d70c103 NB |
2639 | /* If hardware_lock held on entry, might drop it, then reaquire */ |
2640 | /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */ | |
2641 | static int __qlt_send_term_exchange(struct scsi_qla_host *vha, | |
2642 | struct qla_tgt_cmd *cmd, | |
2643 | struct atio_from_isp *atio) | |
2644 | { | |
2645 | struct ctio7_to_24xx *ctio24; | |
2646 | struct qla_hw_data *ha = vha->hw; | |
2647 | request_t *pkt; | |
2648 | int ret = 0; | |
33a5fcee | 2649 | uint16_t temp; |
2d70c103 NB |
2650 | |
2651 | ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha); | |
2652 | ||
b6a029e1 | 2653 | pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL); |
2d70c103 NB |
2654 | if (pkt == NULL) { |
2655 | ql_dbg(ql_dbg_tgt, vha, 0xe050, | |
2656 | "qla_target(%d): %s failed: unable to allocate " | |
2657 | "request packet\n", vha->vp_idx, __func__); | |
2658 | return -ENOMEM; | |
2659 | } | |
2660 | ||
2661 | if (cmd != NULL) { | |
2662 | if (cmd->state < QLA_TGT_STATE_PROCESSED) { | |
2663 | ql_dbg(ql_dbg_tgt, vha, 0xe051, | |
2664 | "qla_target(%d): Terminating cmd %p with " | |
2665 | "incorrect state %d\n", vha->vp_idx, cmd, | |
2666 | cmd->state); | |
2667 | } else | |
2668 | ret = 1; | |
2669 | } | |
2670 | ||
2671 | pkt->entry_count = 1; | |
2672 | pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK; | |
2673 | ||
2674 | ctio24 = (struct ctio7_to_24xx *)pkt; | |
2675 | ctio24->entry_type = CTIO_TYPE7; | |
2676 | ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED; | |
2677 | ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT); | |
2678 | ctio24->vp_index = vha->vp_idx; | |
2679 | ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2]; | |
2680 | ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1]; | |
2681 | ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0]; | |
2682 | ctio24->exchange_addr = atio->u.isp24.exchange_addr; | |
2683 | ctio24->u.status1.flags = (atio->u.isp24.attr << 9) | | |
2684 | __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 | | |
2685 | CTIO7_FLAGS_TERMINATE); | |
33a5fcee QT |
2686 | temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id); |
2687 | ctio24->u.status1.ox_id = cpu_to_le16(temp); | |
2d70c103 NB |
2688 | |
2689 | /* Most likely, it isn't needed */ | |
2690 | ctio24->u.status1.residual = get_unaligned((uint32_t *) | |
2691 | &atio->u.isp24.fcp_cmnd.add_cdb[ | |
2692 | atio->u.isp24.fcp_cmnd.add_cdb_len]); | |
2693 | if (ctio24->u.status1.residual != 0) | |
2694 | ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER; | |
2695 | ||
2696 | qla2x00_start_iocbs(vha, vha->req); | |
2697 | return ret; | |
2698 | } | |
2699 | ||
2700 | static void qlt_send_term_exchange(struct scsi_qla_host *vha, | |
2701 | struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked) | |
2702 | { | |
2703 | unsigned long flags; | |
2704 | int rc; | |
2705 | ||
2706 | if (qlt_issue_marker(vha, ha_locked) < 0) | |
2707 | return; | |
2708 | ||
2709 | if (ha_locked) { | |
2710 | rc = __qlt_send_term_exchange(vha, cmd, atio); | |
33e79977 QT |
2711 | if (rc == -ENOMEM) |
2712 | qlt_alloc_qfull_cmd(vha, atio, 0, 0); | |
2d70c103 NB |
2713 | goto done; |
2714 | } | |
2715 | spin_lock_irqsave(&vha->hw->hardware_lock, flags); | |
2716 | rc = __qlt_send_term_exchange(vha, cmd, atio); | |
33e79977 QT |
2717 | if (rc == -ENOMEM) |
2718 | qlt_alloc_qfull_cmd(vha, atio, 0, 0); | |
2d70c103 NB |
2719 | spin_unlock_irqrestore(&vha->hw->hardware_lock, flags); |
2720 | done: | |
7b898542 QT |
2721 | /* |
2722 | * Terminate exchange will tell fw to release any active CTIO | |
2723 | * that's in FW posession and cleanup the exchange. | |
2724 | * | |
2725 | * "cmd->state == QLA_TGT_STATE_ABORTED" means CTIO is still | |
2726 | * down at FW. Free the cmd later when CTIO comes back later | |
2727 | * w/aborted(0x2) status. | |
2728 | * | |
2729 | * "cmd->state != QLA_TGT_STATE_ABORTED" means CTIO is already | |
2730 | * back w/some err. Free the cmd now. | |
2731 | */ | |
2732 | if ((rc == 1) && (cmd->state != QLA_TGT_STATE_ABORTED)) { | |
2d70c103 NB |
2733 | if (!ha_locked && !in_interrupt()) |
2734 | msleep(250); /* just in case */ | |
2735 | ||
f83adb61 QT |
2736 | if (cmd->sg_mapped) |
2737 | qlt_unmap_sg(vha, cmd); | |
2d70c103 NB |
2738 | vha->hw->tgt.tgt_ops->free_cmd(cmd); |
2739 | } | |
7b898542 | 2740 | return; |
2d70c103 NB |
2741 | } |
2742 | ||
33e79977 QT |
2743 | static void qlt_init_term_exchange(struct scsi_qla_host *vha) |
2744 | { | |
2745 | struct list_head free_list; | |
2746 | struct qla_tgt_cmd *cmd, *tcmd; | |
2747 | ||
2748 | vha->hw->tgt.leak_exchg_thresh_hold = | |
2749 | (vha->hw->fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT; | |
2750 | ||
2751 | cmd = tcmd = NULL; | |
2752 | if (!list_empty(&vha->hw->tgt.q_full_list)) { | |
2753 | INIT_LIST_HEAD(&free_list); | |
2754 | list_splice_init(&vha->hw->tgt.q_full_list, &free_list); | |
2755 | ||
2756 | list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) { | |
2757 | list_del(&cmd->cmd_list); | |
2758 | /* This cmd was never sent to TCM. There is no need | |
2759 | * to schedule free or call free_cmd | |
2760 | */ | |
2761 | qlt_free_cmd(cmd); | |
2762 | vha->hw->tgt.num_qfull_cmds_alloc--; | |
2763 | } | |
2764 | } | |
2765 | vha->hw->tgt.num_qfull_cmds_dropped = 0; | |
2766 | } | |
2767 | ||
2768 | static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha) | |
2769 | { | |
2770 | uint32_t total_leaked; | |
2771 | ||
2772 | total_leaked = vha->hw->tgt.num_qfull_cmds_dropped; | |
2773 | ||
2774 | if (vha->hw->tgt.leak_exchg_thresh_hold && | |
2775 | (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) { | |
2776 | ||
2777 | ql_dbg(ql_dbg_tgt, vha, 0xe079, | |
2778 | "Chip reset due to exchange starvation: %d/%d.\n", | |
2779 | total_leaked, vha->hw->fw_xcb_count); | |
2780 | ||
2781 | if (IS_P3P_TYPE(vha->hw)) | |
2782 | set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags); | |
2783 | else | |
2784 | set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags); | |
2785 | qla2xxx_wake_dpc(vha); | |
2786 | } | |
2787 | ||
2788 | } | |
2789 | ||
2d70c103 NB |
2790 | void qlt_free_cmd(struct qla_tgt_cmd *cmd) |
2791 | { | |
51a07f84 NB |
2792 | struct qla_tgt_sess *sess = cmd->sess; |
2793 | ||
f83adb61 QT |
2794 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074, |
2795 | "%s: se_cmd[%p] ox_id %04x\n", | |
2796 | __func__, &cmd->se_cmd, | |
2797 | be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id)); | |
2d70c103 | 2798 | |
33e79977 QT |
2799 | if (!cmd->q_full) |
2800 | qlt_decr_num_pend_cmds(cmd->vha); | |
2801 | ||
f83adb61 | 2802 | BUG_ON(cmd->sg_mapped); |
2d70c103 NB |
2803 | if (unlikely(cmd->free_sg)) |
2804 | kfree(cmd->sg); | |
51a07f84 NB |
2805 | |
2806 | if (!sess || !sess->se_sess) { | |
2807 | WARN_ON(1); | |
2808 | return; | |
2809 | } | |
2810 | percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag); | |
2d70c103 NB |
2811 | } |
2812 | EXPORT_SYMBOL(qlt_free_cmd); | |
2813 | ||
2814 | /* ha->hardware_lock supposed to be held on entry */ | |
2815 | static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha, | |
2816 | struct qla_tgt_cmd *cmd, void *ctio) | |
2817 | { | |
2818 | struct qla_tgt_srr_ctio *sc; | |
0e8cd71c | 2819 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
2820 | struct qla_tgt_srr_imm *imm; |
2821 | ||
2822 | tgt->ctio_srr_id++; | |
2823 | ||
2824 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019, | |
2825 | "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx); | |
2826 | ||
2827 | if (!ctio) { | |
2828 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055, | |
2829 | "qla_target(%d): SRR CTIO, but ctio is NULL\n", | |
2830 | vha->vp_idx); | |
2831 | return -EINVAL; | |
2832 | } | |
2833 | ||
2834 | sc = kzalloc(sizeof(*sc), GFP_ATOMIC); | |
2835 | if (sc != NULL) { | |
2836 | sc->cmd = cmd; | |
2837 | /* IRQ is already OFF */ | |
2838 | spin_lock(&tgt->srr_lock); | |
2839 | sc->srr_id = tgt->ctio_srr_id; | |
2840 | list_add_tail(&sc->srr_list_entry, | |
2841 | &tgt->srr_ctio_list); | |
2842 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a, | |
2843 | "CTIO SRR %p added (id %d)\n", sc, sc->srr_id); | |
2844 | if (tgt->imm_srr_id == tgt->ctio_srr_id) { | |
2845 | int found = 0; | |
2846 | list_for_each_entry(imm, &tgt->srr_imm_list, | |
2847 | srr_list_entry) { | |
2848 | if (imm->srr_id == sc->srr_id) { | |
2849 | found = 1; | |
2850 | break; | |
2851 | } | |
2852 | } | |
2853 | if (found) { | |
2854 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b, | |
2855 | "Scheduling srr work\n"); | |
2856 | schedule_work(&tgt->srr_work); | |
2857 | } else { | |
2858 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056, | |
2859 | "qla_target(%d): imm_srr_id " | |
2860 | "== ctio_srr_id (%d), but there is no " | |
2861 | "corresponding SRR IMM, deleting CTIO " | |
2862 | "SRR %p\n", vha->vp_idx, | |
2863 | tgt->ctio_srr_id, sc); | |
2864 | list_del(&sc->srr_list_entry); | |
2865 | spin_unlock(&tgt->srr_lock); | |
2866 | ||
2867 | kfree(sc); | |
2868 | return -EINVAL; | |
2869 | } | |
2870 | } | |
2871 | spin_unlock(&tgt->srr_lock); | |
2872 | } else { | |
2873 | struct qla_tgt_srr_imm *ti; | |
2874 | ||
2875 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057, | |
2876 | "qla_target(%d): Unable to allocate SRR CTIO entry\n", | |
2877 | vha->vp_idx); | |
2878 | spin_lock(&tgt->srr_lock); | |
2879 | list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list, | |
2880 | srr_list_entry) { | |
2881 | if (imm->srr_id == tgt->ctio_srr_id) { | |
2882 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c, | |
2883 | "IMM SRR %p deleted (id %d)\n", | |
2884 | imm, imm->srr_id); | |
2885 | list_del(&imm->srr_list_entry); | |
2886 | qlt_reject_free_srr_imm(vha, imm, 1); | |
2887 | } | |
2888 | } | |
2889 | spin_unlock(&tgt->srr_lock); | |
2890 | ||
2891 | return -ENOMEM; | |
2892 | } | |
2893 | ||
2894 | return 0; | |
2895 | } | |
2896 | ||
2897 | /* | |
2898 | * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire | |
2899 | */ | |
2900 | static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio, | |
2901 | struct qla_tgt_cmd *cmd, uint32_t status) | |
2902 | { | |
2903 | int term = 0; | |
2904 | ||
2905 | if (ctio != NULL) { | |
2906 | struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio; | |
2907 | term = !(c->flags & | |
2908 | __constant_cpu_to_le16(OF_TERM_EXCH)); | |
2909 | } else | |
2910 | term = 1; | |
2911 | ||
2912 | if (term) | |
2913 | qlt_send_term_exchange(vha, cmd, &cmd->atio, 1); | |
2914 | ||
2915 | return term; | |
2916 | } | |
2917 | ||
2918 | /* ha->hardware_lock supposed to be held on entry */ | |
2919 | static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha, | |
2920 | uint32_t handle) | |
2921 | { | |
2922 | struct qla_hw_data *ha = vha->hw; | |
2923 | ||
2924 | handle--; | |
2925 | if (ha->tgt.cmds[handle] != NULL) { | |
2926 | struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle]; | |
2927 | ha->tgt.cmds[handle] = NULL; | |
2928 | return cmd; | |
2929 | } else | |
2930 | return NULL; | |
2931 | } | |
2932 | ||
2933 | /* ha->hardware_lock supposed to be held on entry */ | |
2934 | static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha, | |
2935 | uint32_t handle, void *ctio) | |
2936 | { | |
2937 | struct qla_tgt_cmd *cmd = NULL; | |
2938 | ||
2939 | /* Clear out internal marks */ | |
2940 | handle &= ~(CTIO_COMPLETION_HANDLE_MARK | | |
2941 | CTIO_INTERMEDIATE_HANDLE_MARK); | |
2942 | ||
2943 | if (handle != QLA_TGT_NULL_HANDLE) { | |
667024a3 | 2944 | if (unlikely(handle == QLA_TGT_SKIP_HANDLE)) |
2d70c103 | 2945 | return NULL; |
667024a3 | 2946 | |
2d70c103 | 2947 | /* handle-1 is actually used */ |
8d93f550 | 2948 | if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) { |
2d70c103 NB |
2949 | ql_dbg(ql_dbg_tgt, vha, 0xe052, |
2950 | "qla_target(%d): Wrong handle %x received\n", | |
2951 | vha->vp_idx, handle); | |
2952 | return NULL; | |
2953 | } | |
2954 | cmd = qlt_get_cmd(vha, handle); | |
2955 | if (unlikely(cmd == NULL)) { | |
2956 | ql_dbg(ql_dbg_tgt, vha, 0xe053, | |
2957 | "qla_target(%d): Suspicious: unable to " | |
2958 | "find the command with handle %x\n", vha->vp_idx, | |
2959 | handle); | |
2960 | return NULL; | |
2961 | } | |
2962 | } else if (ctio != NULL) { | |
2963 | /* We can't get loop ID from CTIO7 */ | |
2964 | ql_dbg(ql_dbg_tgt, vha, 0xe054, | |
2965 | "qla_target(%d): Wrong CTIO received: QLA24xx doesn't " | |
2966 | "support NULL handles\n", vha->vp_idx); | |
2967 | return NULL; | |
2968 | } | |
2969 | ||
2970 | return cmd; | |
2971 | } | |
2972 | ||
c0cb4496 AE |
2973 | /* hardware_lock should be held by caller. */ |
2974 | static void | |
2975 | qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd) | |
2976 | { | |
2977 | struct qla_hw_data *ha = vha->hw; | |
2978 | uint32_t handle; | |
2979 | ||
2980 | if (cmd->sg_mapped) | |
2981 | qlt_unmap_sg(vha, cmd); | |
2982 | ||
2983 | handle = qlt_make_handle(vha); | |
2984 | ||
2985 | /* TODO: fix debug message type and ids. */ | |
2986 | if (cmd->state == QLA_TGT_STATE_PROCESSED) { | |
2987 | ql_dbg(ql_dbg_io, vha, 0xff00, | |
2988 | "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle); | |
2989 | } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) { | |
2990 | cmd->write_data_transferred = 0; | |
2991 | cmd->state = QLA_TGT_STATE_DATA_IN; | |
2992 | ||
2993 | ql_dbg(ql_dbg_io, vha, 0xff01, | |
2994 | "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle); | |
2995 | ||
2996 | ha->tgt.tgt_ops->handle_data(cmd); | |
2997 | return; | |
2998 | } else if (cmd->state == QLA_TGT_STATE_ABORTED) { | |
2999 | ql_dbg(ql_dbg_io, vha, 0xff02, | |
3000 | "HOST-ABORT: handle=%d, state=ABORTED.\n", handle); | |
3001 | } else { | |
3002 | ql_dbg(ql_dbg_io, vha, 0xff03, | |
3003 | "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle, | |
3004 | cmd->state); | |
3005 | dump_stack(); | |
3006 | } | |
3007 | ||
3008 | ha->tgt.tgt_ops->free_cmd(cmd); | |
3009 | } | |
3010 | ||
3011 | void | |
3012 | qlt_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 | ||
2d70c103 NB |
3047 | /* |
3048 | * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire | |
3049 | */ | |
3050 | static 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 | ||
2d70c103 NB |
3058 | 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); | |
092e1dc3 | 3069 | if (cmd == NULL) |
2d70c103 | 3070 | return; |
092e1dc3 | 3071 | |
2d70c103 NB |
3072 | se_cmd = &cmd->se_cmd; |
3073 | tfo = se_cmd->se_tfo; | |
3074 | ||
3075 | if (cmd->sg_mapped) | |
3076 | qlt_unmap_sg(vha, cmd); | |
3077 | ||
3078 | if (unlikely(status != CTIO_SUCCESS)) { | |
3079 | switch (status & 0xFFFF) { | |
3080 | case CTIO_LIP_RESET: | |
3081 | case CTIO_TARGET_RESET: | |
3082 | case CTIO_ABORTED: | |
7b898542 | 3083 | /* driver request abort via Terminate exchange */ |
2d70c103 NB |
3084 | case CTIO_TIMEOUT: |
3085 | case CTIO_INVALID_RX_ID: | |
3086 | /* They are OK */ | |
3087 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058, | |
3088 | "qla_target(%d): CTIO with " | |
3089 | "status %#x received, state %x, se_cmd %p, " | |
3090 | "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, " | |
3091 | "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx, | |
3092 | status, cmd->state, se_cmd); | |
3093 | break; | |
3094 | ||
3095 | case CTIO_PORT_LOGGED_OUT: | |
3096 | case CTIO_PORT_UNAVAILABLE: | |
3097 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059, | |
3098 | "qla_target(%d): CTIO with PORT LOGGED " | |
3099 | "OUT (29) or PORT UNAVAILABLE (28) status %x " | |
3100 | "received (state %x, se_cmd %p)\n", vha->vp_idx, | |
3101 | status, cmd->state, se_cmd); | |
3102 | break; | |
3103 | ||
3104 | case CTIO_SRR_RECEIVED: | |
3105 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a, | |
3106 | "qla_target(%d): CTIO with SRR_RECEIVED" | |
3107 | " status %x received (state %x, se_cmd %p)\n", | |
3108 | vha->vp_idx, status, cmd->state, se_cmd); | |
3109 | if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0) | |
3110 | break; | |
3111 | else | |
3112 | return; | |
3113 | ||
f83adb61 QT |
3114 | case CTIO_DIF_ERROR: { |
3115 | struct ctio_crc_from_fw *crc = | |
3116 | (struct ctio_crc_from_fw *)ctio; | |
3117 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073, | |
3118 | "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n", | |
3119 | vha->vp_idx, status, cmd->state, se_cmd, | |
3120 | *((u64 *)&crc->actual_dif[0]), | |
3121 | *((u64 *)&crc->expected_dif[0])); | |
3122 | ||
3123 | if (qlt_handle_dif_error(vha, cmd, ctio)) { | |
3124 | if (cmd->state == QLA_TGT_STATE_NEED_DATA) { | |
3125 | /* scsi Write/xfer rdy complete */ | |
3126 | goto skip_term; | |
3127 | } else { | |
3128 | /* scsi read/xmit respond complete | |
3129 | * call handle dif to send scsi status | |
3130 | * rather than terminate exchange. | |
3131 | */ | |
3132 | cmd->state = QLA_TGT_STATE_PROCESSED; | |
3133 | ha->tgt.tgt_ops->handle_dif_err(cmd); | |
3134 | return; | |
3135 | } | |
3136 | } else { | |
3137 | /* Need to generate a SCSI good completion. | |
3138 | * because FW did not send scsi status. | |
3139 | */ | |
3140 | status = 0; | |
3141 | goto skip_term; | |
3142 | } | |
3143 | break; | |
3144 | } | |
2d70c103 NB |
3145 | default: |
3146 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b, | |
f83adb61 | 3147 | "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n", |
2d70c103 NB |
3148 | vha->vp_idx, status, cmd->state, se_cmd); |
3149 | break; | |
3150 | } | |
3151 | ||
7b898542 QT |
3152 | |
3153 | /* "cmd->state == QLA_TGT_STATE_ABORTED" means | |
3154 | * cmd is already aborted/terminated, we don't | |
3155 | * need to terminate again. The exchange is already | |
3156 | * cleaned up/freed at FW level. Just cleanup at driver | |
3157 | * level. | |
3158 | */ | |
3159 | if ((cmd->state != QLA_TGT_STATE_NEED_DATA) && | |
33e79977 | 3160 | (cmd->state != QLA_TGT_STATE_ABORTED)) { |
2d70c103 NB |
3161 | if (qlt_term_ctio_exchange(vha, ctio, cmd, status)) |
3162 | return; | |
7b898542 | 3163 | } |
2d70c103 | 3164 | } |
f83adb61 | 3165 | skip_term: |
2d70c103 NB |
3166 | |
3167 | if (cmd->state == QLA_TGT_STATE_PROCESSED) { | |
667024a3 | 3168 | ; |
2d70c103 NB |
3169 | } else if (cmd->state == QLA_TGT_STATE_NEED_DATA) { |
3170 | int rx_status = 0; | |
3171 | ||
3172 | cmd->state = QLA_TGT_STATE_DATA_IN; | |
3173 | ||
3174 | if (unlikely(status != CTIO_SUCCESS)) | |
3175 | rx_status = -EIO; | |
3176 | else | |
3177 | cmd->write_data_transferred = 1; | |
3178 | ||
2d70c103 NB |
3179 | ha->tgt.tgt_ops->handle_data(cmd); |
3180 | return; | |
3181 | } else if (cmd->state == QLA_TGT_STATE_ABORTED) { | |
3182 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e, | |
3183 | "Aborted command %p (tag %d) finished\n", cmd, cmd->tag); | |
3184 | } else { | |
3185 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c, | |
3186 | "qla_target(%d): A command in state (%d) should " | |
3187 | "not return a CTIO complete\n", vha->vp_idx, cmd->state); | |
3188 | } | |
3189 | ||
7b898542 QT |
3190 | if (unlikely(status != CTIO_SUCCESS) && |
3191 | (cmd->state != QLA_TGT_STATE_ABORTED)) { | |
2d70c103 NB |
3192 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n"); |
3193 | dump_stack(); | |
3194 | } | |
3195 | ||
33e79977 | 3196 | |
2d70c103 NB |
3197 | ha->tgt.tgt_ops->free_cmd(cmd); |
3198 | } | |
3199 | ||
2d70c103 NB |
3200 | static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha, |
3201 | uint8_t task_codes) | |
3202 | { | |
3203 | int fcp_task_attr; | |
3204 | ||
3205 | switch (task_codes) { | |
3206 | case ATIO_SIMPLE_QUEUE: | |
3207 | fcp_task_attr = MSG_SIMPLE_TAG; | |
3208 | break; | |
3209 | case ATIO_HEAD_OF_QUEUE: | |
3210 | fcp_task_attr = MSG_HEAD_TAG; | |
3211 | break; | |
3212 | case ATIO_ORDERED_QUEUE: | |
3213 | fcp_task_attr = MSG_ORDERED_TAG; | |
3214 | break; | |
3215 | case ATIO_ACA_QUEUE: | |
3216 | fcp_task_attr = MSG_ACA_TAG; | |
3217 | break; | |
3218 | case ATIO_UNTAGGED: | |
3219 | fcp_task_attr = MSG_SIMPLE_TAG; | |
3220 | break; | |
3221 | default: | |
3222 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d, | |
3223 | "qla_target: unknown task code %x, use ORDERED instead\n", | |
3224 | task_codes); | |
3225 | fcp_task_attr = MSG_ORDERED_TAG; | |
3226 | break; | |
3227 | } | |
3228 | ||
3229 | return fcp_task_attr; | |
3230 | } | |
3231 | ||
3232 | static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *, | |
3233 | uint8_t *); | |
3234 | /* | |
3235 | * Process context for I/O path into tcm_qla2xxx code | |
3236 | */ | |
51a07f84 | 3237 | static void __qlt_do_work(struct qla_tgt_cmd *cmd) |
2d70c103 | 3238 | { |
2d70c103 NB |
3239 | scsi_qla_host_t *vha = cmd->vha; |
3240 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 3241 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
51a07f84 | 3242 | struct qla_tgt_sess *sess = cmd->sess; |
2d70c103 NB |
3243 | struct atio_from_isp *atio = &cmd->atio; |
3244 | unsigned char *cdb; | |
3245 | unsigned long flags; | |
3246 | uint32_t data_length; | |
3247 | int ret, fcp_task_attr, data_dir, bidi = 0; | |
3248 | ||
3249 | if (tgt->tgt_stop) | |
3250 | goto out_term; | |
3251 | ||
2d70c103 NB |
3252 | cdb = &atio->u.isp24.fcp_cmnd.cdb[0]; |
3253 | cmd->tag = atio->u.isp24.exchange_addr; | |
3254 | cmd->unpacked_lun = scsilun_to_int( | |
3255 | (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun); | |
3256 | ||
3257 | if (atio->u.isp24.fcp_cmnd.rddata && | |
3258 | atio->u.isp24.fcp_cmnd.wrdata) { | |
3259 | bidi = 1; | |
3260 | data_dir = DMA_TO_DEVICE; | |
3261 | } else if (atio->u.isp24.fcp_cmnd.rddata) | |
3262 | data_dir = DMA_FROM_DEVICE; | |
3263 | else if (atio->u.isp24.fcp_cmnd.wrdata) | |
3264 | data_dir = DMA_TO_DEVICE; | |
3265 | else | |
3266 | data_dir = DMA_NONE; | |
3267 | ||
3268 | fcp_task_attr = qlt_get_fcp_task_attr(vha, | |
3269 | atio->u.isp24.fcp_cmnd.task_attr); | |
3270 | data_length = be32_to_cpu(get_unaligned((uint32_t *) | |
3271 | &atio->u.isp24.fcp_cmnd.add_cdb[ | |
3272 | atio->u.isp24.fcp_cmnd.add_cdb_len])); | |
3273 | ||
51a07f84 NB |
3274 | ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length, |
3275 | fcp_task_attr, data_dir, bidi); | |
2d70c103 NB |
3276 | if (ret != 0) |
3277 | goto out_term; | |
3278 | /* | |
3279 | * Drop extra session reference from qla_tgt_handle_cmd_for_atio*( | |
3280 | */ | |
08234e3a | 3281 | spin_lock_irqsave(&ha->hardware_lock, flags); |
2d70c103 | 3282 | ha->tgt.tgt_ops->put_sess(sess); |
08234e3a | 3283 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2d70c103 NB |
3284 | return; |
3285 | ||
3286 | out_term: | |
667024a3 | 3287 | ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd); |
2d70c103 | 3288 | /* |
fae9eaf8 RD |
3289 | * cmd has not sent to target yet, so pass NULL as the second |
3290 | * argument to qlt_send_term_exchange() and free the memory here. | |
2d70c103 NB |
3291 | */ |
3292 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3293 | qlt_send_term_exchange(vha, NULL, &cmd->atio, 1); | |
33e79977 QT |
3294 | |
3295 | qlt_decr_num_pend_cmds(vha); | |
51a07f84 NB |
3296 | percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag); |
3297 | ha->tgt.tgt_ops->put_sess(sess); | |
3298 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
3299 | } | |
3300 | ||
3301 | static void qlt_do_work(struct work_struct *work) | |
3302 | { | |
3303 | struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work); | |
3304 | ||
3305 | __qlt_do_work(cmd); | |
3306 | } | |
3307 | ||
3308 | static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha, | |
3309 | struct qla_tgt_sess *sess, | |
3310 | struct atio_from_isp *atio) | |
3311 | { | |
3312 | struct se_session *se_sess = sess->se_sess; | |
3313 | struct qla_tgt_cmd *cmd; | |
3314 | int tag; | |
3315 | ||
3316 | tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING); | |
3317 | if (tag < 0) | |
3318 | return NULL; | |
3319 | ||
3320 | cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag]; | |
3321 | memset(cmd, 0, sizeof(struct qla_tgt_cmd)); | |
3322 | ||
3323 | memcpy(&cmd->atio, atio, sizeof(*atio)); | |
3324 | cmd->state = QLA_TGT_STATE_NEW; | |
3325 | cmd->tgt = vha->vha_tgt.qla_tgt; | |
33e79977 | 3326 | qlt_incr_num_pend_cmds(vha); |
51a07f84 NB |
3327 | cmd->vha = vha; |
3328 | cmd->se_cmd.map_tag = tag; | |
3329 | cmd->sess = sess; | |
3330 | cmd->loop_id = sess->loop_id; | |
3331 | cmd->conf_compl_supported = sess->conf_compl_supported; | |
3332 | ||
3333 | return cmd; | |
3334 | } | |
3335 | ||
3336 | static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *, | |
3337 | uint16_t); | |
3338 | ||
3339 | static void qlt_create_sess_from_atio(struct work_struct *work) | |
3340 | { | |
3341 | struct qla_tgt_sess_op *op = container_of(work, | |
3342 | struct qla_tgt_sess_op, work); | |
3343 | scsi_qla_host_t *vha = op->vha; | |
3344 | struct qla_hw_data *ha = vha->hw; | |
3345 | struct qla_tgt_sess *sess; | |
3346 | struct qla_tgt_cmd *cmd; | |
3347 | unsigned long flags; | |
3348 | uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id; | |
3349 | ||
3350 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022, | |
3351 | "qla_target(%d): Unable to find wwn login" | |
3352 | " (s_id %x:%x:%x), trying to create it manually\n", | |
3353 | vha->vp_idx, s_id[0], s_id[1], s_id[2]); | |
3354 | ||
3355 | if (op->atio.u.raw.entry_count > 1) { | |
3356 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023, | |
3357 | "Dropping multy entry atio %p\n", &op->atio); | |
3358 | goto out_term; | |
3359 | } | |
3360 | ||
3361 | mutex_lock(&vha->vha_tgt.tgt_mutex); | |
3362 | sess = qlt_make_local_sess(vha, s_id); | |
3363 | /* sess has an extra creation ref. */ | |
3364 | mutex_unlock(&vha->vha_tgt.tgt_mutex); | |
3365 | ||
3366 | if (!sess) | |
3367 | goto out_term; | |
3368 | /* | |
3369 | * Now obtain a pre-allocated session tag using the original op->atio | |
3370 | * packet header, and dispatch into __qlt_do_work() using the existing | |
3371 | * process context. | |
3372 | */ | |
3373 | cmd = qlt_get_tag(vha, sess, &op->atio); | |
3374 | if (!cmd) { | |
3375 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3376 | qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY); | |
2d70c103 | 3377 | ha->tgt.tgt_ops->put_sess(sess); |
51a07f84 NB |
3378 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
3379 | kfree(op); | |
3380 | return; | |
3381 | } | |
3382 | /* | |
3383 | * __qlt_do_work() will call ha->tgt.tgt_ops->put_sess() to release | |
3384 | * the extra reference taken above by qlt_make_local_sess() | |
3385 | */ | |
3386 | __qlt_do_work(cmd); | |
3387 | kfree(op); | |
3388 | return; | |
3389 | ||
3390 | out_term: | |
3391 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3392 | qlt_send_term_exchange(vha, NULL, &op->atio, 1); | |
08234e3a | 3393 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
51a07f84 NB |
3394 | kfree(op); |
3395 | ||
2d70c103 NB |
3396 | } |
3397 | ||
3398 | /* ha->hardware_lock supposed to be held on entry */ | |
3399 | static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha, | |
3400 | struct atio_from_isp *atio) | |
3401 | { | |
51a07f84 | 3402 | struct qla_hw_data *ha = vha->hw; |
0e8cd71c | 3403 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
51a07f84 | 3404 | struct qla_tgt_sess *sess; |
2d70c103 NB |
3405 | struct qla_tgt_cmd *cmd; |
3406 | ||
3407 | if (unlikely(tgt->tgt_stop)) { | |
667024a3 | 3408 | ql_dbg(ql_dbg_io, vha, 0x3061, |
2d70c103 NB |
3409 | "New command while device %p is shutting down\n", tgt); |
3410 | return -EFAULT; | |
3411 | } | |
3412 | ||
51a07f84 NB |
3413 | sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id); |
3414 | if (unlikely(!sess)) { | |
3415 | struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op), | |
3416 | GFP_ATOMIC); | |
3417 | if (!op) | |
3418 | return -ENOMEM; | |
3419 | ||
3420 | memcpy(&op->atio, atio, sizeof(*atio)); | |
78c2106a | 3421 | op->vha = vha; |
51a07f84 NB |
3422 | INIT_WORK(&op->work, qlt_create_sess_from_atio); |
3423 | queue_work(qla_tgt_wq, &op->work); | |
3424 | return 0; | |
3425 | } | |
3426 | /* | |
3427 | * Do kref_get() before returning + dropping qla_hw_data->hardware_lock. | |
3428 | */ | |
3429 | kref_get(&sess->se_sess->sess_kref); | |
3430 | ||
3431 | cmd = qlt_get_tag(vha, sess, atio); | |
2d70c103 | 3432 | if (!cmd) { |
667024a3 | 3433 | ql_dbg(ql_dbg_io, vha, 0x3062, |
2d70c103 | 3434 | "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx); |
51a07f84 | 3435 | ha->tgt.tgt_ops->put_sess(sess); |
2d70c103 NB |
3436 | return -ENOMEM; |
3437 | } | |
3438 | ||
b6a029e1 AE |
3439 | cmd->reset_count = vha->hw->chip_reset; |
3440 | ||
2d70c103 NB |
3441 | INIT_WORK(&cmd->work, qlt_do_work); |
3442 | queue_work(qla_tgt_wq, &cmd->work); | |
3443 | return 0; | |
3444 | ||
3445 | } | |
3446 | ||
3447 | /* ha->hardware_lock supposed to be held on entry */ | |
3448 | static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun, | |
3449 | int fn, void *iocb, int flags) | |
3450 | { | |
3451 | struct scsi_qla_host *vha = sess->vha; | |
3452 | struct qla_hw_data *ha = vha->hw; | |
3453 | struct qla_tgt_mgmt_cmd *mcmd; | |
3454 | int res; | |
3455 | uint8_t tmr_func; | |
3456 | ||
3457 | mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC); | |
3458 | if (!mcmd) { | |
3459 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009, | |
3460 | "qla_target(%d): Allocation of management " | |
3461 | "command failed, some commands and their data could " | |
3462 | "leak\n", vha->vp_idx); | |
3463 | return -ENOMEM; | |
3464 | } | |
3465 | memset(mcmd, 0, sizeof(*mcmd)); | |
3466 | mcmd->sess = sess; | |
3467 | ||
3468 | if (iocb) { | |
3469 | memcpy(&mcmd->orig_iocb.imm_ntfy, iocb, | |
3470 | sizeof(mcmd->orig_iocb.imm_ntfy)); | |
3471 | } | |
3472 | mcmd->tmr_func = fn; | |
3473 | mcmd->flags = flags; | |
b6a029e1 | 3474 | mcmd->reset_count = vha->hw->chip_reset; |
2d70c103 NB |
3475 | |
3476 | switch (fn) { | |
3477 | case QLA_TGT_CLEAR_ACA: | |
3478 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000, | |
3479 | "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx); | |
3480 | tmr_func = TMR_CLEAR_ACA; | |
3481 | break; | |
3482 | ||
3483 | case QLA_TGT_TARGET_RESET: | |
3484 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001, | |
3485 | "qla_target(%d): TARGET_RESET received\n", | |
3486 | sess->vha->vp_idx); | |
3487 | tmr_func = TMR_TARGET_WARM_RESET; | |
3488 | break; | |
3489 | ||
3490 | case QLA_TGT_LUN_RESET: | |
3491 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002, | |
3492 | "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx); | |
3493 | tmr_func = TMR_LUN_RESET; | |
3494 | break; | |
3495 | ||
3496 | case QLA_TGT_CLEAR_TS: | |
3497 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003, | |
3498 | "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx); | |
3499 | tmr_func = TMR_CLEAR_TASK_SET; | |
3500 | break; | |
3501 | ||
3502 | case QLA_TGT_ABORT_TS: | |
3503 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004, | |
3504 | "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx); | |
3505 | tmr_func = TMR_ABORT_TASK_SET; | |
3506 | break; | |
3507 | #if 0 | |
3508 | case QLA_TGT_ABORT_ALL: | |
3509 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005, | |
3510 | "qla_target(%d): Doing ABORT_ALL_TASKS\n", | |
3511 | sess->vha->vp_idx); | |
3512 | tmr_func = 0; | |
3513 | break; | |
3514 | ||
3515 | case QLA_TGT_ABORT_ALL_SESS: | |
3516 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006, | |
3517 | "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n", | |
3518 | sess->vha->vp_idx); | |
3519 | tmr_func = 0; | |
3520 | break; | |
3521 | ||
3522 | case QLA_TGT_NEXUS_LOSS_SESS: | |
3523 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007, | |
3524 | "qla_target(%d): Doing NEXUS_LOSS_SESS\n", | |
3525 | sess->vha->vp_idx); | |
3526 | tmr_func = 0; | |
3527 | break; | |
3528 | ||
3529 | case QLA_TGT_NEXUS_LOSS: | |
3530 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008, | |
3531 | "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx); | |
3532 | tmr_func = 0; | |
3533 | break; | |
3534 | #endif | |
3535 | default: | |
3536 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a, | |
3537 | "qla_target(%d): Unknown task mgmt fn 0x%x\n", | |
3538 | sess->vha->vp_idx, fn); | |
3539 | mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool); | |
3540 | return -ENOSYS; | |
3541 | } | |
3542 | ||
3543 | res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0); | |
3544 | if (res != 0) { | |
3545 | ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b, | |
3546 | "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n", | |
3547 | sess->vha->vp_idx, res); | |
3548 | mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool); | |
3549 | return -EFAULT; | |
3550 | } | |
3551 | ||
3552 | return 0; | |
3553 | } | |
3554 | ||
3555 | /* ha->hardware_lock supposed to be held on entry */ | |
3556 | static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb) | |
3557 | { | |
3558 | struct atio_from_isp *a = (struct atio_from_isp *)iocb; | |
3559 | struct qla_hw_data *ha = vha->hw; | |
3560 | struct qla_tgt *tgt; | |
3561 | struct qla_tgt_sess *sess; | |
3562 | uint32_t lun, unpacked_lun; | |
3563 | int lun_size, fn; | |
3564 | ||
0e8cd71c | 3565 | tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
3566 | |
3567 | lun = a->u.isp24.fcp_cmnd.lun; | |
3568 | lun_size = sizeof(a->u.isp24.fcp_cmnd.lun); | |
3569 | fn = a->u.isp24.fcp_cmnd.task_mgmt_flags; | |
3570 | sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, | |
3571 | a->u.isp24.fcp_hdr.s_id); | |
3572 | unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun); | |
3573 | ||
3574 | if (!sess) { | |
3575 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024, | |
3576 | "qla_target(%d): task mgmt fn 0x%x for " | |
3577 | "non-existant session\n", vha->vp_idx, fn); | |
3578 | return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb, | |
3579 | sizeof(struct atio_from_isp)); | |
3580 | } | |
3581 | ||
3582 | return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0); | |
3583 | } | |
3584 | ||
3585 | /* ha->hardware_lock supposed to be held on entry */ | |
3586 | static int __qlt_abort_task(struct scsi_qla_host *vha, | |
3587 | struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess) | |
3588 | { | |
3589 | struct atio_from_isp *a = (struct atio_from_isp *)iocb; | |
3590 | struct qla_hw_data *ha = vha->hw; | |
3591 | struct qla_tgt_mgmt_cmd *mcmd; | |
3592 | uint32_t lun, unpacked_lun; | |
3593 | int rc; | |
3594 | ||
3595 | mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC); | |
3596 | if (mcmd == NULL) { | |
3597 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f, | |
3598 | "qla_target(%d): %s: Allocation of ABORT cmd failed\n", | |
3599 | vha->vp_idx, __func__); | |
3600 | return -ENOMEM; | |
3601 | } | |
3602 | memset(mcmd, 0, sizeof(*mcmd)); | |
3603 | ||
3604 | mcmd->sess = sess; | |
3605 | memcpy(&mcmd->orig_iocb.imm_ntfy, iocb, | |
3606 | sizeof(mcmd->orig_iocb.imm_ntfy)); | |
3607 | ||
3608 | lun = a->u.isp24.fcp_cmnd.lun; | |
3609 | unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun); | |
80187f8e | 3610 | mcmd->reset_count = vha->hw->chip_reset; |
2d70c103 NB |
3611 | |
3612 | rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK, | |
3613 | le16_to_cpu(iocb->u.isp2x.seq_id)); | |
3614 | if (rc != 0) { | |
3615 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060, | |
3616 | "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n", | |
3617 | vha->vp_idx, rc); | |
3618 | mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool); | |
3619 | return -EFAULT; | |
3620 | } | |
3621 | ||
3622 | return 0; | |
3623 | } | |
3624 | ||
3625 | /* ha->hardware_lock supposed to be held on entry */ | |
3626 | static int qlt_abort_task(struct scsi_qla_host *vha, | |
3627 | struct imm_ntfy_from_isp *iocb) | |
3628 | { | |
3629 | struct qla_hw_data *ha = vha->hw; | |
3630 | struct qla_tgt_sess *sess; | |
3631 | int loop_id; | |
3632 | ||
3633 | loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb); | |
3634 | ||
3635 | sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id); | |
3636 | if (sess == NULL) { | |
3637 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025, | |
3638 | "qla_target(%d): task abort for unexisting " | |
3639 | "session\n", vha->vp_idx); | |
0e8cd71c | 3640 | return qlt_sched_sess_work(vha->vha_tgt.qla_tgt, |
2d70c103 NB |
3641 | QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb)); |
3642 | } | |
3643 | ||
3644 | return __qlt_abort_task(vha, iocb, sess); | |
3645 | } | |
3646 | ||
3647 | /* | |
3648 | * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire | |
3649 | */ | |
3650 | static int qlt_24xx_handle_els(struct scsi_qla_host *vha, | |
3651 | struct imm_ntfy_from_isp *iocb) | |
3652 | { | |
2d70c103 NB |
3653 | int res = 0; |
3654 | ||
3655 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026, | |
7b833558 OK |
3656 | "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n", |
3657 | vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode); | |
2d70c103 NB |
3658 | |
3659 | switch (iocb->u.isp24.status_subcode) { | |
3660 | case ELS_PLOGI: | |
3661 | case ELS_FLOGI: | |
3662 | case ELS_PRLI: | |
3663 | case ELS_LOGO: | |
3664 | case ELS_PRLO: | |
3665 | res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS); | |
3666 | break; | |
3667 | case ELS_PDISC: | |
3668 | case ELS_ADISC: | |
3669 | { | |
0e8cd71c | 3670 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
3671 | if (tgt->link_reinit_iocb_pending) { |
3672 | qlt_send_notify_ack(vha, &tgt->link_reinit_iocb, | |
3673 | 0, 0, 0, 0, 0, 0); | |
3674 | tgt->link_reinit_iocb_pending = 0; | |
3675 | } | |
3676 | res = 1; /* send notify ack */ | |
3677 | break; | |
3678 | } | |
3679 | ||
3680 | default: | |
3681 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061, | |
3682 | "qla_target(%d): Unsupported ELS command %x " | |
3683 | "received\n", vha->vp_idx, iocb->u.isp24.status_subcode); | |
3684 | res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS); | |
3685 | break; | |
3686 | } | |
3687 | ||
3688 | return res; | |
3689 | } | |
3690 | ||
3691 | static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset) | |
3692 | { | |
3693 | struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL; | |
3694 | size_t first_offset = 0, rem_offset = offset, tmp = 0; | |
3695 | int i, sg_srr_cnt, bufflen = 0; | |
3696 | ||
3697 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023, | |
3698 | "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, " | |
3699 | "cmd->sg_cnt: %u, direction: %d\n", | |
3700 | cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction); | |
3701 | ||
3702 | /* | |
3703 | * FIXME: Reject non zero SRR relative offset until we can test | |
3704 | * this code properly. | |
3705 | */ | |
3706 | pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset); | |
3707 | return -1; | |
3708 | ||
3709 | if (!cmd->sg || !cmd->sg_cnt) { | |
3710 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055, | |
3711 | "Missing cmd->sg or zero cmd->sg_cnt in" | |
3712 | " qla_tgt_set_data_offset\n"); | |
3713 | return -EINVAL; | |
3714 | } | |
3715 | /* | |
3716 | * Walk the current cmd->sg list until we locate the new sg_srr_start | |
3717 | */ | |
3718 | for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) { | |
3719 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024, | |
3720 | "sg[%d]: %p page: %p, length: %d, offset: %d\n", | |
3721 | i, sg, sg_page(sg), sg->length, sg->offset); | |
3722 | ||
3723 | if ((sg->length + tmp) > offset) { | |
3724 | first_offset = rem_offset; | |
3725 | sg_srr_start = sg; | |
3726 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025, | |
3727 | "Found matching sg[%d], using %p as sg_srr_start, " | |
3728 | "and using first_offset: %zu\n", i, sg, | |
3729 | first_offset); | |
3730 | break; | |
3731 | } | |
3732 | tmp += sg->length; | |
3733 | rem_offset -= sg->length; | |
3734 | } | |
3735 | ||
3736 | if (!sg_srr_start) { | |
3737 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056, | |
3738 | "Unable to locate sg_srr_start for offset: %u\n", offset); | |
3739 | return -EINVAL; | |
3740 | } | |
3741 | sg_srr_cnt = (cmd->sg_cnt - i); | |
3742 | ||
3743 | sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL); | |
3744 | if (!sg_srr) { | |
3745 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057, | |
3746 | "Unable to allocate sgp\n"); | |
3747 | return -ENOMEM; | |
3748 | } | |
3749 | sg_init_table(sg_srr, sg_srr_cnt); | |
3750 | sgp = &sg_srr[0]; | |
3751 | /* | |
3752 | * Walk the remaining list for sg_srr_start, mapping to the newly | |
3753 | * allocated sg_srr taking first_offset into account. | |
3754 | */ | |
3755 | for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) { | |
3756 | if (first_offset) { | |
3757 | sg_set_page(sgp, sg_page(sg), | |
3758 | (sg->length - first_offset), first_offset); | |
3759 | first_offset = 0; | |
3760 | } else { | |
3761 | sg_set_page(sgp, sg_page(sg), sg->length, 0); | |
3762 | } | |
3763 | bufflen += sgp->length; | |
3764 | ||
3765 | sgp = sg_next(sgp); | |
3766 | if (!sgp) | |
3767 | break; | |
3768 | } | |
3769 | ||
3770 | cmd->sg = sg_srr; | |
3771 | cmd->sg_cnt = sg_srr_cnt; | |
3772 | cmd->bufflen = bufflen; | |
3773 | cmd->offset += offset; | |
3774 | cmd->free_sg = 1; | |
3775 | ||
3776 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg); | |
3777 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n", | |
3778 | cmd->sg_cnt); | |
3779 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n", | |
3780 | cmd->bufflen); | |
3781 | ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n", | |
3782 | cmd->offset); | |
3783 | ||
3784 | if (cmd->sg_cnt < 0) | |
3785 | BUG(); | |
3786 | ||
3787 | if (cmd->bufflen < 0) | |
3788 | BUG(); | |
3789 | ||
3790 | return 0; | |
3791 | } | |
3792 | ||
3793 | static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd, | |
3794 | uint32_t srr_rel_offs, int *xmit_type) | |
3795 | { | |
3796 | int res = 0, rel_offs; | |
3797 | ||
3798 | rel_offs = srr_rel_offs - cmd->offset; | |
3799 | ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d", | |
3800 | srr_rel_offs, rel_offs); | |
3801 | ||
3802 | *xmit_type = QLA_TGT_XMIT_ALL; | |
3803 | ||
3804 | if (rel_offs < 0) { | |
3805 | ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062, | |
3806 | "qla_target(%d): SRR rel_offs (%d) < 0", | |
3807 | cmd->vha->vp_idx, rel_offs); | |
3808 | res = -1; | |
3809 | } else if (rel_offs == cmd->bufflen) | |
3810 | *xmit_type = QLA_TGT_XMIT_STATUS; | |
3811 | else if (rel_offs > 0) | |
3812 | res = qlt_set_data_offset(cmd, rel_offs); | |
3813 | ||
3814 | return res; | |
3815 | } | |
3816 | ||
3817 | /* No locks, thread context */ | |
3818 | static void qlt_handle_srr(struct scsi_qla_host *vha, | |
3819 | struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm) | |
3820 | { | |
3821 | struct imm_ntfy_from_isp *ntfy = | |
3822 | (struct imm_ntfy_from_isp *)&imm->imm_ntfy; | |
3823 | struct qla_hw_data *ha = vha->hw; | |
3824 | struct qla_tgt_cmd *cmd = sctio->cmd; | |
3825 | struct se_cmd *se_cmd = &cmd->se_cmd; | |
3826 | unsigned long flags; | |
3827 | int xmit_type = 0, resp = 0; | |
3828 | uint32_t offset; | |
3829 | uint16_t srr_ui; | |
3830 | ||
3831 | offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs); | |
3832 | srr_ui = ntfy->u.isp24.srr_ui; | |
3833 | ||
3834 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n", | |
3835 | cmd, srr_ui); | |
3836 | ||
3837 | switch (srr_ui) { | |
3838 | case SRR_IU_STATUS: | |
3839 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3840 | qlt_send_notify_ack(vha, ntfy, | |
3841 | 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0); | |
3842 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
3843 | xmit_type = QLA_TGT_XMIT_STATUS; | |
3844 | resp = 1; | |
3845 | break; | |
3846 | case SRR_IU_DATA_IN: | |
3847 | if (!cmd->sg || !cmd->sg_cnt) { | |
3848 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063, | |
3849 | "Unable to process SRR_IU_DATA_IN due to" | |
3850 | " missing cmd->sg, state: %d\n", cmd->state); | |
3851 | dump_stack(); | |
3852 | goto out_reject; | |
3853 | } | |
3854 | if (se_cmd->scsi_status != 0) { | |
3855 | ql_dbg(ql_dbg_tgt, vha, 0xe02a, | |
3856 | "Rejecting SRR_IU_DATA_IN with non GOOD " | |
3857 | "scsi_status\n"); | |
3858 | goto out_reject; | |
3859 | } | |
3860 | cmd->bufflen = se_cmd->data_length; | |
3861 | ||
3862 | if (qlt_has_data(cmd)) { | |
3863 | if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0) | |
3864 | goto out_reject; | |
3865 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3866 | qlt_send_notify_ack(vha, ntfy, | |
3867 | 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0); | |
3868 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
3869 | resp = 1; | |
3870 | } else { | |
3871 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064, | |
3872 | "qla_target(%d): SRR for in data for cmd " | |
3873 | "without them (tag %d, SCSI status %d), " | |
3874 | "reject", vha->vp_idx, cmd->tag, | |
3875 | cmd->se_cmd.scsi_status); | |
3876 | goto out_reject; | |
3877 | } | |
3878 | break; | |
3879 | case SRR_IU_DATA_OUT: | |
3880 | if (!cmd->sg || !cmd->sg_cnt) { | |
3881 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065, | |
3882 | "Unable to process SRR_IU_DATA_OUT due to" | |
3883 | " missing cmd->sg\n"); | |
3884 | dump_stack(); | |
3885 | goto out_reject; | |
3886 | } | |
3887 | if (se_cmd->scsi_status != 0) { | |
3888 | ql_dbg(ql_dbg_tgt, vha, 0xe02b, | |
3889 | "Rejecting SRR_IU_DATA_OUT" | |
3890 | " with non GOOD scsi_status\n"); | |
3891 | goto out_reject; | |
3892 | } | |
3893 | cmd->bufflen = se_cmd->data_length; | |
3894 | ||
3895 | if (qlt_has_data(cmd)) { | |
3896 | if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0) | |
3897 | goto out_reject; | |
3898 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3899 | qlt_send_notify_ack(vha, ntfy, | |
3900 | 0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0); | |
3901 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
3902 | if (xmit_type & QLA_TGT_XMIT_DATA) | |
3903 | qlt_rdy_to_xfer(cmd); | |
3904 | } else { | |
3905 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066, | |
3906 | "qla_target(%d): SRR for out data for cmd " | |
3907 | "without them (tag %d, SCSI status %d), " | |
3908 | "reject", vha->vp_idx, cmd->tag, | |
3909 | cmd->se_cmd.scsi_status); | |
3910 | goto out_reject; | |
3911 | } | |
3912 | break; | |
3913 | default: | |
3914 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067, | |
3915 | "qla_target(%d): Unknown srr_ui value %x", | |
3916 | vha->vp_idx, srr_ui); | |
3917 | goto out_reject; | |
3918 | } | |
3919 | ||
3920 | /* Transmit response in case of status and data-in cases */ | |
3921 | if (resp) | |
3922 | qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status); | |
3923 | ||
3924 | return; | |
3925 | ||
3926 | out_reject: | |
3927 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3928 | qlt_send_notify_ack(vha, ntfy, 0, 0, 0, | |
3929 | NOTIFY_ACK_SRR_FLAGS_REJECT, | |
3930 | NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM, | |
3931 | NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL); | |
3932 | if (cmd->state == QLA_TGT_STATE_NEED_DATA) { | |
3933 | cmd->state = QLA_TGT_STATE_DATA_IN; | |
3934 | dump_stack(); | |
3935 | } else | |
3936 | qlt_send_term_exchange(vha, cmd, &cmd->atio, 1); | |
3937 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
3938 | } | |
3939 | ||
3940 | static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha, | |
3941 | struct qla_tgt_srr_imm *imm, int ha_locked) | |
3942 | { | |
3943 | struct qla_hw_data *ha = vha->hw; | |
3944 | unsigned long flags = 0; | |
3945 | ||
3946 | if (!ha_locked) | |
3947 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
3948 | ||
3949 | qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0, | |
3950 | NOTIFY_ACK_SRR_FLAGS_REJECT, | |
3951 | NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM, | |
3952 | NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL); | |
3953 | ||
3954 | if (!ha_locked) | |
3955 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
3956 | ||
3957 | kfree(imm); | |
3958 | } | |
3959 | ||
3960 | static void qlt_handle_srr_work(struct work_struct *work) | |
3961 | { | |
3962 | struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work); | |
3963 | struct scsi_qla_host *vha = tgt->vha; | |
3964 | struct qla_tgt_srr_ctio *sctio; | |
3965 | unsigned long flags; | |
3966 | ||
3967 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n", | |
3968 | tgt); | |
3969 | ||
3970 | restart: | |
3971 | spin_lock_irqsave(&tgt->srr_lock, flags); | |
3972 | list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) { | |
3973 | struct qla_tgt_srr_imm *imm, *i, *ti; | |
3974 | struct qla_tgt_cmd *cmd; | |
3975 | struct se_cmd *se_cmd; | |
3976 | ||
3977 | imm = NULL; | |
3978 | list_for_each_entry_safe(i, ti, &tgt->srr_imm_list, | |
3979 | srr_list_entry) { | |
3980 | if (i->srr_id == sctio->srr_id) { | |
3981 | list_del(&i->srr_list_entry); | |
3982 | if (imm) { | |
3983 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068, | |
3984 | "qla_target(%d): There must be " | |
3985 | "only one IMM SRR per CTIO SRR " | |
3986 | "(IMM SRR %p, id %d, CTIO %p\n", | |
3987 | vha->vp_idx, i, i->srr_id, sctio); | |
3988 | qlt_reject_free_srr_imm(tgt->vha, i, 0); | |
3989 | } else | |
3990 | imm = i; | |
3991 | } | |
3992 | } | |
3993 | ||
3994 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a, | |
3995 | "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio, | |
3996 | sctio->srr_id); | |
3997 | ||
3998 | if (imm == NULL) { | |
3999 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b, | |
4000 | "Not found matching IMM for SRR CTIO (id %d)\n", | |
4001 | sctio->srr_id); | |
4002 | continue; | |
4003 | } else | |
4004 | list_del(&sctio->srr_list_entry); | |
4005 | ||
4006 | spin_unlock_irqrestore(&tgt->srr_lock, flags); | |
4007 | ||
4008 | cmd = sctio->cmd; | |
4009 | /* | |
4010 | * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow | |
4011 | * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in() | |
4012 | * logic.. | |
4013 | */ | |
4014 | cmd->offset = 0; | |
4015 | if (cmd->free_sg) { | |
4016 | kfree(cmd->sg); | |
4017 | cmd->sg = NULL; | |
4018 | cmd->free_sg = 0; | |
4019 | } | |
4020 | se_cmd = &cmd->se_cmd; | |
4021 | ||
4022 | cmd->sg_cnt = se_cmd->t_data_nents; | |
4023 | cmd->sg = se_cmd->t_data_sg; | |
4024 | ||
4025 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c, | |
4026 | "SRR cmd %p (se_cmd %p, tag %d, op %x), " | |
4027 | "sg_cnt=%d, offset=%d", cmd, &cmd->se_cmd, cmd->tag, | |
6f58c780 DGW |
4028 | se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0, |
4029 | cmd->sg_cnt, cmd->offset); | |
2d70c103 NB |
4030 | |
4031 | qlt_handle_srr(vha, sctio, imm); | |
4032 | ||
4033 | kfree(imm); | |
4034 | kfree(sctio); | |
4035 | goto restart; | |
4036 | } | |
4037 | spin_unlock_irqrestore(&tgt->srr_lock, flags); | |
4038 | } | |
4039 | ||
4040 | /* ha->hardware_lock supposed to be held on entry */ | |
4041 | static void qlt_prepare_srr_imm(struct scsi_qla_host *vha, | |
4042 | struct imm_ntfy_from_isp *iocb) | |
4043 | { | |
4044 | struct qla_tgt_srr_imm *imm; | |
0e8cd71c | 4045 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
4046 | struct qla_tgt_srr_ctio *sctio; |
4047 | ||
4048 | tgt->imm_srr_id++; | |
4049 | ||
4050 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02d, "qla_target(%d): SRR received\n", | |
4051 | vha->vp_idx); | |
4052 | ||
4053 | imm = kzalloc(sizeof(*imm), GFP_ATOMIC); | |
4054 | if (imm != NULL) { | |
4055 | memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy)); | |
4056 | ||
4057 | /* IRQ is already OFF */ | |
4058 | spin_lock(&tgt->srr_lock); | |
4059 | imm->srr_id = tgt->imm_srr_id; | |
4060 | list_add_tail(&imm->srr_list_entry, | |
4061 | &tgt->srr_imm_list); | |
4062 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e, | |
4063 | "IMM NTFY SRR %p added (id %d, ui %x)\n", | |
4064 | imm, imm->srr_id, iocb->u.isp24.srr_ui); | |
4065 | if (tgt->imm_srr_id == tgt->ctio_srr_id) { | |
4066 | int found = 0; | |
4067 | list_for_each_entry(sctio, &tgt->srr_ctio_list, | |
4068 | srr_list_entry) { | |
4069 | if (sctio->srr_id == imm->srr_id) { | |
4070 | found = 1; | |
4071 | break; | |
4072 | } | |
4073 | } | |
4074 | if (found) { | |
4075 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s", | |
4076 | "Scheduling srr work\n"); | |
4077 | schedule_work(&tgt->srr_work); | |
4078 | } else { | |
4079 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030, | |
4080 | "qla_target(%d): imm_srr_id " | |
4081 | "== ctio_srr_id (%d), but there is no " | |
4082 | "corresponding SRR CTIO, deleting IMM " | |
4083 | "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id, | |
4084 | imm); | |
4085 | list_del(&imm->srr_list_entry); | |
4086 | ||
4087 | kfree(imm); | |
4088 | ||
4089 | spin_unlock(&tgt->srr_lock); | |
4090 | goto out_reject; | |
4091 | } | |
4092 | } | |
4093 | spin_unlock(&tgt->srr_lock); | |
4094 | } else { | |
4095 | struct qla_tgt_srr_ctio *ts; | |
4096 | ||
4097 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069, | |
4098 | "qla_target(%d): Unable to allocate SRR IMM " | |
4099 | "entry, SRR request will be rejected\n", vha->vp_idx); | |
4100 | ||
4101 | /* IRQ is already OFF */ | |
4102 | spin_lock(&tgt->srr_lock); | |
4103 | list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list, | |
4104 | srr_list_entry) { | |
4105 | if (sctio->srr_id == tgt->imm_srr_id) { | |
4106 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031, | |
4107 | "CTIO SRR %p deleted (id %d)\n", | |
4108 | sctio, sctio->srr_id); | |
4109 | list_del(&sctio->srr_list_entry); | |
4110 | qlt_send_term_exchange(vha, sctio->cmd, | |
4111 | &sctio->cmd->atio, 1); | |
4112 | kfree(sctio); | |
4113 | } | |
4114 | } | |
4115 | spin_unlock(&tgt->srr_lock); | |
4116 | goto out_reject; | |
4117 | } | |
4118 | ||
4119 | return; | |
4120 | ||
4121 | out_reject: | |
4122 | qlt_send_notify_ack(vha, iocb, 0, 0, 0, | |
4123 | NOTIFY_ACK_SRR_FLAGS_REJECT, | |
4124 | NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM, | |
4125 | NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL); | |
4126 | } | |
4127 | ||
4128 | /* | |
4129 | * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire | |
4130 | */ | |
4131 | static void qlt_handle_imm_notify(struct scsi_qla_host *vha, | |
4132 | struct imm_ntfy_from_isp *iocb) | |
4133 | { | |
4134 | struct qla_hw_data *ha = vha->hw; | |
4135 | uint32_t add_flags = 0; | |
4136 | int send_notify_ack = 1; | |
4137 | uint16_t status; | |
4138 | ||
4139 | status = le16_to_cpu(iocb->u.isp2x.status); | |
4140 | switch (status) { | |
4141 | case IMM_NTFY_LIP_RESET: | |
4142 | { | |
4143 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032, | |
4144 | "qla_target(%d): LIP reset (loop %#x), subcode %x\n", | |
4145 | vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle), | |
4146 | iocb->u.isp24.status_subcode); | |
4147 | ||
4148 | if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0) | |
4149 | send_notify_ack = 0; | |
4150 | break; | |
4151 | } | |
4152 | ||
4153 | case IMM_NTFY_LIP_LINK_REINIT: | |
4154 | { | |
0e8cd71c | 4155 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
4156 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033, |
4157 | "qla_target(%d): LINK REINIT (loop %#x, " | |
4158 | "subcode %x)\n", vha->vp_idx, | |
4159 | le16_to_cpu(iocb->u.isp24.nport_handle), | |
4160 | iocb->u.isp24.status_subcode); | |
4161 | if (tgt->link_reinit_iocb_pending) { | |
4162 | qlt_send_notify_ack(vha, &tgt->link_reinit_iocb, | |
4163 | 0, 0, 0, 0, 0, 0); | |
4164 | } | |
4165 | memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb)); | |
4166 | tgt->link_reinit_iocb_pending = 1; | |
4167 | /* | |
4168 | * QLogic requires to wait after LINK REINIT for possible | |
4169 | * PDISC or ADISC ELS commands | |
4170 | */ | |
4171 | send_notify_ack = 0; | |
4172 | break; | |
4173 | } | |
4174 | ||
4175 | case IMM_NTFY_PORT_LOGOUT: | |
4176 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034, | |
4177 | "qla_target(%d): Port logout (loop " | |
4178 | "%#x, subcode %x)\n", vha->vp_idx, | |
4179 | le16_to_cpu(iocb->u.isp24.nport_handle), | |
4180 | iocb->u.isp24.status_subcode); | |
4181 | ||
4182 | if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0) | |
4183 | send_notify_ack = 0; | |
4184 | /* The sessions will be cleared in the callback, if needed */ | |
4185 | break; | |
4186 | ||
4187 | case IMM_NTFY_GLBL_TPRLO: | |
4188 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035, | |
4189 | "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status); | |
4190 | if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0) | |
4191 | send_notify_ack = 0; | |
4192 | /* The sessions will be cleared in the callback, if needed */ | |
4193 | break; | |
4194 | ||
4195 | case IMM_NTFY_PORT_CONFIG: | |
4196 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036, | |
4197 | "qla_target(%d): Port config changed (%x)\n", vha->vp_idx, | |
4198 | status); | |
4199 | if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0) | |
4200 | send_notify_ack = 0; | |
4201 | /* The sessions will be cleared in the callback, if needed */ | |
4202 | break; | |
4203 | ||
4204 | case IMM_NTFY_GLBL_LOGO: | |
4205 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a, | |
4206 | "qla_target(%d): Link failure detected\n", | |
4207 | vha->vp_idx); | |
4208 | /* I_T nexus loss */ | |
4209 | if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0) | |
4210 | send_notify_ack = 0; | |
4211 | break; | |
4212 | ||
4213 | case IMM_NTFY_IOCB_OVERFLOW: | |
4214 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b, | |
4215 | "qla_target(%d): Cannot provide requested " | |
4216 | "capability (IOCB overflowed the immediate notify " | |
4217 | "resource count)\n", vha->vp_idx); | |
4218 | break; | |
4219 | ||
4220 | case IMM_NTFY_ABORT_TASK: | |
4221 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037, | |
4222 | "qla_target(%d): Abort Task (S %08x I %#x -> " | |
4223 | "L %#x)\n", vha->vp_idx, | |
4224 | le16_to_cpu(iocb->u.isp2x.seq_id), | |
4225 | GET_TARGET_ID(ha, (struct atio_from_isp *)iocb), | |
4226 | le16_to_cpu(iocb->u.isp2x.lun)); | |
4227 | if (qlt_abort_task(vha, iocb) == 0) | |
4228 | send_notify_ack = 0; | |
4229 | break; | |
4230 | ||
4231 | case IMM_NTFY_RESOURCE: | |
4232 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c, | |
4233 | "qla_target(%d): Out of resources, host %ld\n", | |
4234 | vha->vp_idx, vha->host_no); | |
4235 | break; | |
4236 | ||
4237 | case IMM_NTFY_MSG_RX: | |
4238 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038, | |
4239 | "qla_target(%d): Immediate notify task %x\n", | |
4240 | vha->vp_idx, iocb->u.isp2x.task_flags); | |
4241 | if (qlt_handle_task_mgmt(vha, iocb) == 0) | |
4242 | send_notify_ack = 0; | |
4243 | break; | |
4244 | ||
4245 | case IMM_NTFY_ELS: | |
4246 | if (qlt_24xx_handle_els(vha, iocb) == 0) | |
4247 | send_notify_ack = 0; | |
4248 | break; | |
4249 | ||
4250 | case IMM_NTFY_SRR: | |
4251 | qlt_prepare_srr_imm(vha, iocb); | |
4252 | send_notify_ack = 0; | |
4253 | break; | |
4254 | ||
4255 | default: | |
4256 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d, | |
4257 | "qla_target(%d): Received unknown immediate " | |
4258 | "notify status %x\n", vha->vp_idx, status); | |
4259 | break; | |
4260 | } | |
4261 | ||
4262 | if (send_notify_ack) | |
4263 | qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0); | |
4264 | } | |
4265 | ||
4266 | /* | |
4267 | * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire | |
4268 | * This function sends busy to ISP 2xxx or 24xx. | |
4269 | */ | |
33e79977 | 4270 | static int __qlt_send_busy(struct scsi_qla_host *vha, |
2d70c103 NB |
4271 | struct atio_from_isp *atio, uint16_t status) |
4272 | { | |
4273 | struct ctio7_to_24xx *ctio24; | |
4274 | struct qla_hw_data *ha = vha->hw; | |
4275 | request_t *pkt; | |
4276 | struct qla_tgt_sess *sess = NULL; | |
4277 | ||
4278 | sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, | |
4279 | atio->u.isp24.fcp_hdr.s_id); | |
4280 | if (!sess) { | |
4281 | qlt_send_term_exchange(vha, NULL, atio, 1); | |
33e79977 | 4282 | return 0; |
2d70c103 NB |
4283 | } |
4284 | /* Sending marker isn't necessary, since we called from ISR */ | |
4285 | ||
4286 | pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL); | |
4287 | if (!pkt) { | |
667024a3 | 4288 | ql_dbg(ql_dbg_io, vha, 0x3063, |
2d70c103 NB |
4289 | "qla_target(%d): %s failed: unable to allocate " |
4290 | "request packet", vha->vp_idx, __func__); | |
33e79977 | 4291 | return -ENOMEM; |
2d70c103 NB |
4292 | } |
4293 | ||
4294 | pkt->entry_count = 1; | |
4295 | pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK; | |
4296 | ||
4297 | ctio24 = (struct ctio7_to_24xx *)pkt; | |
4298 | ctio24->entry_type = CTIO_TYPE7; | |
4299 | ctio24->nport_handle = sess->loop_id; | |
4300 | ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT); | |
4301 | ctio24->vp_index = vha->vp_idx; | |
4302 | ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2]; | |
4303 | ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1]; | |
4304 | ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0]; | |
4305 | ctio24->exchange_addr = atio->u.isp24.exchange_addr; | |
4306 | ctio24->u.status1.flags = (atio->u.isp24.attr << 9) | | |
4307 | __constant_cpu_to_le16( | |
4308 | CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS | | |
4309 | CTIO7_FLAGS_DONT_RET_CTIO); | |
4310 | /* | |
4311 | * CTIO from fw w/o se_cmd doesn't provide enough info to retry it, | |
4312 | * if the explicit conformation is used. | |
4313 | */ | |
4314 | ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id); | |
4315 | ctio24->u.status1.scsi_status = cpu_to_le16(status); | |
2d70c103 | 4316 | qla2x00_start_iocbs(vha, vha->req); |
33e79977 QT |
4317 | return 0; |
4318 | } | |
4319 | ||
4320 | /* | |
4321 | * This routine is used to allocate a command for either a QFull condition | |
4322 | * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go | |
4323 | * out previously. | |
4324 | */ | |
4325 | static void | |
4326 | qlt_alloc_qfull_cmd(struct scsi_qla_host *vha, | |
4327 | struct atio_from_isp *atio, uint16_t status, int qfull) | |
4328 | { | |
4329 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; | |
4330 | struct qla_hw_data *ha = vha->hw; | |
4331 | struct qla_tgt_sess *sess; | |
4332 | struct se_session *se_sess; | |
4333 | struct qla_tgt_cmd *cmd; | |
4334 | int tag; | |
4335 | ||
4336 | if (unlikely(tgt->tgt_stop)) { | |
4337 | ql_dbg(ql_dbg_io, vha, 0x300a, | |
4338 | "New command while device %p is shutting down\n", tgt); | |
4339 | return; | |
4340 | } | |
4341 | ||
4342 | if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) { | |
4343 | vha->hw->tgt.num_qfull_cmds_dropped++; | |
4344 | if (vha->hw->tgt.num_qfull_cmds_dropped > | |
4345 | vha->hw->qla_stats.stat_max_qfull_cmds_dropped) | |
4346 | vha->hw->qla_stats.stat_max_qfull_cmds_dropped = | |
4347 | vha->hw->tgt.num_qfull_cmds_dropped; | |
4348 | ||
4349 | ql_dbg(ql_dbg_io, vha, 0x3068, | |
4350 | "qla_target(%d): %s: QFull CMD dropped[%d]\n", | |
4351 | vha->vp_idx, __func__, | |
4352 | vha->hw->tgt.num_qfull_cmds_dropped); | |
4353 | ||
4354 | qlt_chk_exch_leak_thresh_hold(vha); | |
4355 | return; | |
4356 | } | |
4357 | ||
4358 | sess = ha->tgt.tgt_ops->find_sess_by_s_id | |
4359 | (vha, atio->u.isp24.fcp_hdr.s_id); | |
4360 | if (!sess) | |
4361 | return; | |
4362 | ||
4363 | se_sess = sess->se_sess; | |
4364 | ||
4365 | tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING); | |
4366 | if (tag < 0) | |
4367 | return; | |
4368 | ||
4369 | cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag]; | |
4370 | if (!cmd) { | |
4371 | ql_dbg(ql_dbg_io, vha, 0x3009, | |
4372 | "qla_target(%d): %s: Allocation of cmd failed\n", | |
4373 | vha->vp_idx, __func__); | |
4374 | ||
4375 | vha->hw->tgt.num_qfull_cmds_dropped++; | |
4376 | if (vha->hw->tgt.num_qfull_cmds_dropped > | |
4377 | vha->hw->qla_stats.stat_max_qfull_cmds_dropped) | |
4378 | vha->hw->qla_stats.stat_max_qfull_cmds_dropped = | |
4379 | vha->hw->tgt.num_qfull_cmds_dropped; | |
4380 | ||
4381 | qlt_chk_exch_leak_thresh_hold(vha); | |
4382 | return; | |
4383 | } | |
4384 | ||
4385 | memset(cmd, 0, sizeof(struct qla_tgt_cmd)); | |
4386 | ||
4387 | qlt_incr_num_pend_cmds(vha); | |
4388 | INIT_LIST_HEAD(&cmd->cmd_list); | |
4389 | memcpy(&cmd->atio, atio, sizeof(*atio)); | |
4390 | ||
4391 | cmd->tgt = vha->vha_tgt.qla_tgt; | |
4392 | cmd->vha = vha; | |
4393 | cmd->reset_count = vha->hw->chip_reset; | |
4394 | cmd->q_full = 1; | |
4395 | ||
4396 | if (qfull) { | |
4397 | cmd->q_full = 1; | |
4398 | /* NOTE: borrowing the state field to carry the status */ | |
4399 | cmd->state = status; | |
4400 | } else | |
4401 | cmd->term_exchg = 1; | |
4402 | ||
4403 | list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list); | |
4404 | ||
4405 | vha->hw->tgt.num_qfull_cmds_alloc++; | |
4406 | if (vha->hw->tgt.num_qfull_cmds_alloc > | |
4407 | vha->hw->qla_stats.stat_max_qfull_cmds_alloc) | |
4408 | vha->hw->qla_stats.stat_max_qfull_cmds_alloc = | |
4409 | vha->hw->tgt.num_qfull_cmds_alloc; | |
4410 | } | |
4411 | ||
4412 | int | |
4413 | qlt_free_qfull_cmds(struct scsi_qla_host *vha) | |
4414 | { | |
4415 | struct qla_hw_data *ha = vha->hw; | |
4416 | unsigned long flags; | |
4417 | struct qla_tgt_cmd *cmd, *tcmd; | |
4418 | struct list_head free_list; | |
4419 | int rc = 0; | |
4420 | ||
4421 | if (list_empty(&ha->tgt.q_full_list)) | |
4422 | return 0; | |
4423 | ||
4424 | INIT_LIST_HEAD(&free_list); | |
4425 | ||
4426 | spin_lock_irqsave(&vha->hw->hardware_lock, flags); | |
4427 | ||
4428 | if (list_empty(&ha->tgt.q_full_list)) { | |
4429 | spin_unlock_irqrestore(&vha->hw->hardware_lock, flags); | |
4430 | return 0; | |
4431 | } | |
4432 | ||
4433 | list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) { | |
4434 | if (cmd->q_full) | |
4435 | /* cmd->state is a borrowed field to hold status */ | |
4436 | rc = __qlt_send_busy(vha, &cmd->atio, cmd->state); | |
4437 | else if (cmd->term_exchg) | |
4438 | rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio); | |
4439 | ||
4440 | if (rc == -ENOMEM) | |
4441 | break; | |
4442 | ||
4443 | if (cmd->q_full) | |
4444 | ql_dbg(ql_dbg_io, vha, 0x3006, | |
4445 | "%s: busy sent for ox_id[%04x]\n", __func__, | |
4446 | be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id)); | |
4447 | else if (cmd->term_exchg) | |
4448 | ql_dbg(ql_dbg_io, vha, 0x3007, | |
4449 | "%s: Term exchg sent for ox_id[%04x]\n", __func__, | |
4450 | be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id)); | |
4451 | else | |
4452 | ql_dbg(ql_dbg_io, vha, 0x3008, | |
4453 | "%s: Unexpected cmd in QFull list %p\n", __func__, | |
4454 | cmd); | |
4455 | ||
4456 | list_del(&cmd->cmd_list); | |
4457 | list_add_tail(&cmd->cmd_list, &free_list); | |
4458 | ||
4459 | /* piggy back on hardware_lock for protection */ | |
4460 | vha->hw->tgt.num_qfull_cmds_alloc--; | |
4461 | } | |
4462 | spin_unlock_irqrestore(&vha->hw->hardware_lock, flags); | |
4463 | ||
4464 | cmd = NULL; | |
4465 | ||
4466 | list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) { | |
4467 | list_del(&cmd->cmd_list); | |
4468 | /* This cmd was never sent to TCM. There is no need | |
4469 | * to schedule free or call free_cmd | |
4470 | */ | |
4471 | qlt_free_cmd(cmd); | |
4472 | } | |
4473 | return rc; | |
4474 | } | |
4475 | ||
4476 | static void | |
4477 | qlt_send_busy(struct scsi_qla_host *vha, | |
4478 | struct atio_from_isp *atio, uint16_t status) | |
4479 | { | |
4480 | int rc = 0; | |
4481 | ||
4482 | rc = __qlt_send_busy(vha, atio, status); | |
4483 | if (rc == -ENOMEM) | |
4484 | qlt_alloc_qfull_cmd(vha, atio, status, 1); | |
4485 | } | |
4486 | ||
4487 | static int | |
4488 | qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, | |
4489 | struct atio_from_isp *atio) | |
4490 | { | |
4491 | struct qla_hw_data *ha = vha->hw; | |
4492 | uint16_t status; | |
4493 | ||
4494 | if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha)) | |
4495 | return 0; | |
4496 | ||
4497 | status = temp_sam_status; | |
4498 | qlt_send_busy(vha, atio, status); | |
4499 | return 1; | |
2d70c103 NB |
4500 | } |
4501 | ||
4502 | /* ha->hardware_lock supposed to be held on entry */ | |
4503 | /* called via callback from qla2xxx */ | |
4504 | static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha, | |
4505 | struct atio_from_isp *atio) | |
4506 | { | |
4507 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 4508 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
4509 | int rc; |
4510 | ||
4511 | if (unlikely(tgt == NULL)) { | |
667024a3 | 4512 | ql_dbg(ql_dbg_io, vha, 0x3064, |
2d70c103 NB |
4513 | "ATIO pkt, but no tgt (ha %p)", ha); |
4514 | return; | |
4515 | } | |
2d70c103 NB |
4516 | /* |
4517 | * In tgt_stop mode we also should allow all requests to pass. | |
4518 | * Otherwise, some commands can stuck. | |
4519 | */ | |
4520 | ||
4521 | tgt->irq_cmd_count++; | |
4522 | ||
4523 | switch (atio->u.raw.entry_type) { | |
4524 | case ATIO_TYPE7: | |
2d70c103 NB |
4525 | if (unlikely(atio->u.isp24.exchange_addr == |
4526 | ATIO_EXCHANGE_ADDRESS_UNKNOWN)) { | |
667024a3 | 4527 | ql_dbg(ql_dbg_io, vha, 0x3065, |
2d70c103 NB |
4528 | "qla_target(%d): ATIO_TYPE7 " |
4529 | "received with UNKNOWN exchange address, " | |
4530 | "sending QUEUE_FULL\n", vha->vp_idx); | |
4531 | qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL); | |
4532 | break; | |
4533 | } | |
33e79977 QT |
4534 | |
4535 | ||
4536 | ||
4537 | if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) { | |
4538 | rc = qlt_chk_qfull_thresh_hold(vha, atio); | |
4539 | if (rc != 0) { | |
4540 | tgt->irq_cmd_count--; | |
4541 | return; | |
4542 | } | |
2d70c103 | 4543 | rc = qlt_handle_cmd_for_atio(vha, atio); |
33e79977 | 4544 | } else { |
2d70c103 | 4545 | rc = qlt_handle_task_mgmt(vha, atio); |
33e79977 | 4546 | } |
2d70c103 NB |
4547 | if (unlikely(rc != 0)) { |
4548 | if (rc == -ESRCH) { | |
4549 | #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */ | |
4550 | qlt_send_busy(vha, atio, SAM_STAT_BUSY); | |
4551 | #else | |
4552 | qlt_send_term_exchange(vha, NULL, atio, 1); | |
4553 | #endif | |
4554 | } else { | |
4555 | if (tgt->tgt_stop) { | |
4556 | ql_dbg(ql_dbg_tgt, vha, 0xe059, | |
4557 | "qla_target: Unable to send " | |
4558 | "command to target for req, " | |
4559 | "ignoring.\n"); | |
4560 | } else { | |
4561 | ql_dbg(ql_dbg_tgt, vha, 0xe05a, | |
4562 | "qla_target(%d): Unable to send " | |
4563 | "command to target, sending BUSY " | |
4564 | "status.\n", vha->vp_idx); | |
4565 | qlt_send_busy(vha, atio, SAM_STAT_BUSY); | |
4566 | } | |
4567 | } | |
4568 | } | |
4569 | break; | |
4570 | ||
4571 | case IMMED_NOTIFY_TYPE: | |
4572 | { | |
4573 | if (unlikely(atio->u.isp2x.entry_status != 0)) { | |
4574 | ql_dbg(ql_dbg_tgt, vha, 0xe05b, | |
4575 | "qla_target(%d): Received ATIO packet %x " | |
4576 | "with error status %x\n", vha->vp_idx, | |
4577 | atio->u.raw.entry_type, | |
4578 | atio->u.isp2x.entry_status); | |
4579 | break; | |
4580 | } | |
4581 | ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO"); | |
4582 | qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio); | |
4583 | break; | |
4584 | } | |
4585 | ||
4586 | default: | |
4587 | ql_dbg(ql_dbg_tgt, vha, 0xe05c, | |
4588 | "qla_target(%d): Received unknown ATIO atio " | |
4589 | "type %x\n", vha->vp_idx, atio->u.raw.entry_type); | |
4590 | break; | |
4591 | } | |
4592 | ||
4593 | tgt->irq_cmd_count--; | |
4594 | } | |
4595 | ||
4596 | /* ha->hardware_lock supposed to be held on entry */ | |
4597 | /* called via callback from qla2xxx */ | |
4598 | static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt) | |
4599 | { | |
4600 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 4601 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
4602 | |
4603 | if (unlikely(tgt == NULL)) { | |
4604 | ql_dbg(ql_dbg_tgt, vha, 0xe05d, | |
4605 | "qla_target(%d): Response pkt %x received, but no " | |
4606 | "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha); | |
4607 | return; | |
4608 | } | |
4609 | ||
2d70c103 NB |
4610 | /* |
4611 | * In tgt_stop mode we also should allow all requests to pass. | |
4612 | * Otherwise, some commands can stuck. | |
4613 | */ | |
4614 | ||
4615 | tgt->irq_cmd_count++; | |
4616 | ||
4617 | switch (pkt->entry_type) { | |
f83adb61 | 4618 | case CTIO_CRC2: |
2d70c103 NB |
4619 | case CTIO_TYPE7: |
4620 | { | |
4621 | struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt; | |
2d70c103 NB |
4622 | qlt_do_ctio_completion(vha, entry->handle, |
4623 | le16_to_cpu(entry->status)|(pkt->entry_status << 16), | |
4624 | entry); | |
4625 | break; | |
4626 | } | |
4627 | ||
4628 | case ACCEPT_TGT_IO_TYPE: | |
4629 | { | |
4630 | struct atio_from_isp *atio = (struct atio_from_isp *)pkt; | |
4631 | int rc; | |
2d70c103 NB |
4632 | if (atio->u.isp2x.status != |
4633 | __constant_cpu_to_le16(ATIO_CDB_VALID)) { | |
4634 | ql_dbg(ql_dbg_tgt, vha, 0xe05e, | |
4635 | "qla_target(%d): ATIO with error " | |
4636 | "status %x received\n", vha->vp_idx, | |
4637 | le16_to_cpu(atio->u.isp2x.status)); | |
4638 | break; | |
4639 | } | |
2d70c103 | 4640 | |
33e79977 QT |
4641 | rc = qlt_chk_qfull_thresh_hold(vha, atio); |
4642 | if (rc != 0) { | |
4643 | tgt->irq_cmd_count--; | |
4644 | return; | |
4645 | } | |
4646 | ||
2d70c103 NB |
4647 | rc = qlt_handle_cmd_for_atio(vha, atio); |
4648 | if (unlikely(rc != 0)) { | |
4649 | if (rc == -ESRCH) { | |
4650 | #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */ | |
4651 | qlt_send_busy(vha, atio, 0); | |
4652 | #else | |
4653 | qlt_send_term_exchange(vha, NULL, atio, 1); | |
4654 | #endif | |
4655 | } else { | |
4656 | if (tgt->tgt_stop) { | |
4657 | ql_dbg(ql_dbg_tgt, vha, 0xe05f, | |
4658 | "qla_target: Unable to send " | |
4659 | "command to target, sending TERM " | |
4660 | "EXCHANGE for rsp\n"); | |
4661 | qlt_send_term_exchange(vha, NULL, | |
4662 | atio, 1); | |
4663 | } else { | |
4664 | ql_dbg(ql_dbg_tgt, vha, 0xe060, | |
4665 | "qla_target(%d): Unable to send " | |
4666 | "command to target, sending BUSY " | |
4667 | "status\n", vha->vp_idx); | |
4668 | qlt_send_busy(vha, atio, 0); | |
4669 | } | |
4670 | } | |
4671 | } | |
4672 | } | |
4673 | break; | |
4674 | ||
4675 | case CONTINUE_TGT_IO_TYPE: | |
4676 | { | |
4677 | struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt; | |
2d70c103 NB |
4678 | qlt_do_ctio_completion(vha, entry->handle, |
4679 | le16_to_cpu(entry->status)|(pkt->entry_status << 16), | |
4680 | entry); | |
4681 | break; | |
4682 | } | |
4683 | ||
4684 | case CTIO_A64_TYPE: | |
4685 | { | |
4686 | struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt; | |
2d70c103 NB |
4687 | qlt_do_ctio_completion(vha, entry->handle, |
4688 | le16_to_cpu(entry->status)|(pkt->entry_status << 16), | |
4689 | entry); | |
4690 | break; | |
4691 | } | |
4692 | ||
4693 | case IMMED_NOTIFY_TYPE: | |
4694 | ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n"); | |
4695 | qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt); | |
4696 | break; | |
4697 | ||
4698 | case NOTIFY_ACK_TYPE: | |
4699 | if (tgt->notify_ack_expected > 0) { | |
4700 | struct nack_to_isp *entry = (struct nack_to_isp *)pkt; | |
4701 | ql_dbg(ql_dbg_tgt, vha, 0xe036, | |
4702 | "NOTIFY_ACK seq %08x status %x\n", | |
4703 | le16_to_cpu(entry->u.isp2x.seq_id), | |
4704 | le16_to_cpu(entry->u.isp2x.status)); | |
4705 | tgt->notify_ack_expected--; | |
4706 | if (entry->u.isp2x.status != | |
4707 | __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) { | |
4708 | ql_dbg(ql_dbg_tgt, vha, 0xe061, | |
4709 | "qla_target(%d): NOTIFY_ACK " | |
4710 | "failed %x\n", vha->vp_idx, | |
4711 | le16_to_cpu(entry->u.isp2x.status)); | |
4712 | } | |
4713 | } else { | |
4714 | ql_dbg(ql_dbg_tgt, vha, 0xe062, | |
4715 | "qla_target(%d): Unexpected NOTIFY_ACK received\n", | |
4716 | vha->vp_idx); | |
4717 | } | |
4718 | break; | |
4719 | ||
4720 | case ABTS_RECV_24XX: | |
4721 | ql_dbg(ql_dbg_tgt, vha, 0xe037, | |
4722 | "ABTS_RECV_24XX: instance %d\n", vha->vp_idx); | |
4723 | qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt); | |
4724 | break; | |
4725 | ||
4726 | case ABTS_RESP_24XX: | |
4727 | if (tgt->abts_resp_expected > 0) { | |
4728 | struct abts_resp_from_24xx_fw *entry = | |
4729 | (struct abts_resp_from_24xx_fw *)pkt; | |
4730 | ql_dbg(ql_dbg_tgt, vha, 0xe038, | |
4731 | "ABTS_RESP_24XX: compl_status %x\n", | |
4732 | entry->compl_status); | |
4733 | tgt->abts_resp_expected--; | |
4734 | if (le16_to_cpu(entry->compl_status) != | |
4735 | ABTS_RESP_COMPL_SUCCESS) { | |
4736 | if ((entry->error_subcode1 == 0x1E) && | |
4737 | (entry->error_subcode2 == 0)) { | |
4738 | /* | |
4739 | * We've got a race here: aborted | |
4740 | * exchange not terminated, i.e. | |
4741 | * response for the aborted command was | |
4742 | * sent between the abort request was | |
4743 | * received and processed. | |
4744 | * Unfortunately, the firmware has a | |
4745 | * silly requirement that all aborted | |
4746 | * exchanges must be explicitely | |
4747 | * terminated, otherwise it refuses to | |
4748 | * send responses for the abort | |
4749 | * requests. So, we have to | |
4750 | * (re)terminate the exchange and retry | |
4751 | * the abort response. | |
4752 | */ | |
4753 | qlt_24xx_retry_term_exchange(vha, | |
4754 | entry); | |
4755 | } else | |
4756 | ql_dbg(ql_dbg_tgt, vha, 0xe063, | |
4757 | "qla_target(%d): ABTS_RESP_24XX " | |
4758 | "failed %x (subcode %x:%x)", | |
4759 | vha->vp_idx, entry->compl_status, | |
4760 | entry->error_subcode1, | |
4761 | entry->error_subcode2); | |
4762 | } | |
4763 | } else { | |
4764 | ql_dbg(ql_dbg_tgt, vha, 0xe064, | |
4765 | "qla_target(%d): Unexpected ABTS_RESP_24XX " | |
4766 | "received\n", vha->vp_idx); | |
4767 | } | |
4768 | break; | |
4769 | ||
4770 | default: | |
4771 | ql_dbg(ql_dbg_tgt, vha, 0xe065, | |
4772 | "qla_target(%d): Received unknown response pkt " | |
4773 | "type %x\n", vha->vp_idx, pkt->entry_type); | |
4774 | break; | |
4775 | } | |
4776 | ||
4777 | tgt->irq_cmd_count--; | |
4778 | } | |
4779 | ||
4780 | /* | |
4781 | * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire | |
4782 | */ | |
4783 | void qlt_async_event(uint16_t code, struct scsi_qla_host *vha, | |
4784 | uint16_t *mailbox) | |
4785 | { | |
4786 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 4787 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
4f1d0f19 | 4788 | int login_code; |
2d70c103 | 4789 | |
2d70c103 NB |
4790 | if (!ha->tgt.tgt_ops) |
4791 | return; | |
4792 | ||
4793 | if (unlikely(tgt == NULL)) { | |
4794 | ql_dbg(ql_dbg_tgt, vha, 0xe03a, | |
4795 | "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha); | |
4796 | return; | |
4797 | } | |
4798 | ||
4799 | if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) && | |
4800 | IS_QLA2100(ha)) | |
4801 | return; | |
4802 | /* | |
4803 | * In tgt_stop mode we also should allow all requests to pass. | |
4804 | * Otherwise, some commands can stuck. | |
4805 | */ | |
4806 | ||
4807 | tgt->irq_cmd_count++; | |
4808 | ||
4809 | switch (code) { | |
4810 | case MBA_RESET: /* Reset */ | |
4811 | case MBA_SYSTEM_ERR: /* System Error */ | |
4812 | case MBA_REQ_TRANSFER_ERR: /* Request Transfer Error */ | |
4813 | case MBA_RSP_TRANSFER_ERR: /* Response Transfer Error */ | |
4814 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a, | |
4815 | "qla_target(%d): System error async event %#x " | |
6efb3c0a | 4816 | "occurred", vha->vp_idx, code); |
2d70c103 NB |
4817 | break; |
4818 | case MBA_WAKEUP_THRES: /* Request Queue Wake-up. */ | |
4819 | set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags); | |
4820 | break; | |
4821 | ||
4822 | case MBA_LOOP_UP: | |
4823 | { | |
4824 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b, | |
6efb3c0a | 4825 | "qla_target(%d): Async LOOP_UP occurred " |
4f1d0f19 AC |
4826 | "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, |
4827 | le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]), | |
4828 | le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3])); | |
2d70c103 NB |
4829 | if (tgt->link_reinit_iocb_pending) { |
4830 | qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb, | |
4831 | 0, 0, 0, 0, 0, 0); | |
4832 | tgt->link_reinit_iocb_pending = 0; | |
4833 | } | |
4834 | break; | |
4835 | } | |
4836 | ||
4837 | case MBA_LIP_OCCURRED: | |
4838 | case MBA_LOOP_DOWN: | |
4839 | case MBA_LIP_RESET: | |
4840 | case MBA_RSCN_UPDATE: | |
4841 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c, | |
6efb3c0a | 4842 | "qla_target(%d): Async event %#x occurred " |
4f1d0f19 AC |
4843 | "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code, |
4844 | le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]), | |
4845 | le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3])); | |
2d70c103 NB |
4846 | break; |
4847 | ||
4848 | case MBA_PORT_UPDATE: | |
4849 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d, | |
4850 | "qla_target(%d): Port update async event %#x " | |
6efb3c0a | 4851 | "occurred: updating the ports database (m[0]=%x, m[1]=%x, " |
4f1d0f19 AC |
4852 | "m[2]=%x, m[3]=%x)", vha->vp_idx, code, |
4853 | le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]), | |
4854 | le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3])); | |
4855 | ||
4856 | login_code = le16_to_cpu(mailbox[2]); | |
4857 | if (login_code == 0x4) | |
2d70c103 NB |
4858 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e, |
4859 | "Async MB 2: Got PLOGI Complete\n"); | |
4f1d0f19 | 4860 | else if (login_code == 0x7) |
2d70c103 NB |
4861 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f, |
4862 | "Async MB 2: Port Logged Out\n"); | |
4863 | break; | |
4864 | ||
4865 | default: | |
2d70c103 NB |
4866 | break; |
4867 | } | |
4868 | ||
4869 | tgt->irq_cmd_count--; | |
4870 | } | |
4871 | ||
4872 | static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha, | |
4873 | uint16_t loop_id) | |
4874 | { | |
4875 | fc_port_t *fcport; | |
4876 | int rc; | |
4877 | ||
4878 | fcport = kzalloc(sizeof(*fcport), GFP_KERNEL); | |
4879 | if (!fcport) { | |
4880 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f, | |
4881 | "qla_target(%d): Allocation of tmp FC port failed", | |
4882 | vha->vp_idx); | |
4883 | return NULL; | |
4884 | } | |
4885 | ||
2d70c103 NB |
4886 | fcport->loop_id = loop_id; |
4887 | ||
4888 | rc = qla2x00_get_port_database(vha, fcport, 0); | |
4889 | if (rc != QLA_SUCCESS) { | |
4890 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070, | |
4891 | "qla_target(%d): Failed to retrieve fcport " | |
4892 | "information -- get_port_database() returned %x " | |
4893 | "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id); | |
4894 | kfree(fcport); | |
4895 | return NULL; | |
4896 | } | |
4897 | ||
4898 | return fcport; | |
4899 | } | |
4900 | ||
4901 | /* Must be called under tgt_mutex */ | |
4902 | static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha, | |
4903 | uint8_t *s_id) | |
4904 | { | |
2d70c103 NB |
4905 | struct qla_tgt_sess *sess = NULL; |
4906 | fc_port_t *fcport = NULL; | |
4907 | int rc, global_resets; | |
4908 | uint16_t loop_id = 0; | |
4909 | ||
4910 | retry: | |
0e8cd71c SK |
4911 | global_resets = |
4912 | atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count); | |
2d70c103 NB |
4913 | |
4914 | rc = qla24xx_get_loop_id(vha, s_id, &loop_id); | |
4915 | if (rc != 0) { | |
4916 | if ((s_id[0] == 0xFF) && | |
4917 | (s_id[1] == 0xFC)) { | |
4918 | /* | |
4919 | * This is Domain Controller, so it should be | |
4920 | * OK to drop SCSI commands from it. | |
4921 | */ | |
4922 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042, | |
4923 | "Unable to find initiator with S_ID %x:%x:%x", | |
4924 | s_id[0], s_id[1], s_id[2]); | |
4925 | } else | |
4926 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071, | |
4927 | "qla_target(%d): Unable to find " | |
4928 | "initiator with S_ID %x:%x:%x", | |
4929 | vha->vp_idx, s_id[0], s_id[1], | |
4930 | s_id[2]); | |
4931 | return NULL; | |
4932 | } | |
4933 | ||
4934 | fcport = qlt_get_port_database(vha, loop_id); | |
4935 | if (!fcport) | |
4936 | return NULL; | |
4937 | ||
4938 | if (global_resets != | |
0e8cd71c | 4939 | atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) { |
2d70c103 NB |
4940 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043, |
4941 | "qla_target(%d): global reset during session discovery " | |
4942 | "(counter was %d, new %d), retrying", vha->vp_idx, | |
4943 | global_resets, | |
0e8cd71c SK |
4944 | atomic_read(&vha->vha_tgt. |
4945 | qla_tgt->tgt_global_resets_count)); | |
2d70c103 NB |
4946 | goto retry; |
4947 | } | |
4948 | ||
4949 | sess = qlt_create_sess(vha, fcport, true); | |
4950 | ||
4951 | kfree(fcport); | |
4952 | return sess; | |
4953 | } | |
4954 | ||
4955 | static void qlt_abort_work(struct qla_tgt *tgt, | |
4956 | struct qla_tgt_sess_work_param *prm) | |
4957 | { | |
4958 | struct scsi_qla_host *vha = tgt->vha; | |
4959 | struct qla_hw_data *ha = vha->hw; | |
4960 | struct qla_tgt_sess *sess = NULL; | |
4961 | unsigned long flags; | |
4962 | uint32_t be_s_id; | |
4963 | uint8_t s_id[3]; | |
4964 | int rc; | |
4965 | ||
4966 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
4967 | ||
4968 | if (tgt->tgt_stop) | |
4969 | goto out_term; | |
4970 | ||
4971 | s_id[0] = prm->abts.fcp_hdr_le.s_id[2]; | |
4972 | s_id[1] = prm->abts.fcp_hdr_le.s_id[1]; | |
4973 | s_id[2] = prm->abts.fcp_hdr_le.s_id[0]; | |
4974 | ||
4975 | sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, | |
4976 | (unsigned char *)&be_s_id); | |
4977 | if (!sess) { | |
4978 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
4979 | ||
0e8cd71c | 4980 | mutex_lock(&vha->vha_tgt.tgt_mutex); |
2d70c103 NB |
4981 | sess = qlt_make_local_sess(vha, s_id); |
4982 | /* sess has got an extra creation ref */ | |
0e8cd71c | 4983 | mutex_unlock(&vha->vha_tgt.tgt_mutex); |
2d70c103 NB |
4984 | |
4985 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
4986 | if (!sess) | |
4987 | goto out_term; | |
4988 | } else { | |
4989 | kref_get(&sess->se_sess->sess_kref); | |
4990 | } | |
4991 | ||
4992 | if (tgt->tgt_stop) | |
4993 | goto out_term; | |
4994 | ||
4995 | rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess); | |
4996 | if (rc != 0) | |
4997 | goto out_term; | |
2d70c103 NB |
4998 | |
4999 | ha->tgt.tgt_ops->put_sess(sess); | |
08234e3a | 5000 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2d70c103 NB |
5001 | return; |
5002 | ||
5003 | out_term: | |
5004 | qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false); | |
2d70c103 NB |
5005 | if (sess) |
5006 | ha->tgt.tgt_ops->put_sess(sess); | |
08234e3a | 5007 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2d70c103 NB |
5008 | } |
5009 | ||
5010 | static void qlt_tmr_work(struct qla_tgt *tgt, | |
5011 | struct qla_tgt_sess_work_param *prm) | |
5012 | { | |
5013 | struct atio_from_isp *a = &prm->tm_iocb2; | |
5014 | struct scsi_qla_host *vha = tgt->vha; | |
5015 | struct qla_hw_data *ha = vha->hw; | |
5016 | struct qla_tgt_sess *sess = NULL; | |
5017 | unsigned long flags; | |
5018 | uint8_t *s_id = NULL; /* to hide compiler warnings */ | |
5019 | int rc; | |
5020 | uint32_t lun, unpacked_lun; | |
5021 | int lun_size, fn; | |
5022 | void *iocb; | |
5023 | ||
5024 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
5025 | ||
5026 | if (tgt->tgt_stop) | |
5027 | goto out_term; | |
5028 | ||
5029 | s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id; | |
5030 | sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id); | |
5031 | if (!sess) { | |
5032 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
5033 | ||
0e8cd71c | 5034 | mutex_lock(&vha->vha_tgt.tgt_mutex); |
2d70c103 NB |
5035 | sess = qlt_make_local_sess(vha, s_id); |
5036 | /* sess has got an extra creation ref */ | |
0e8cd71c | 5037 | mutex_unlock(&vha->vha_tgt.tgt_mutex); |
2d70c103 NB |
5038 | |
5039 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
5040 | if (!sess) | |
5041 | goto out_term; | |
5042 | } else { | |
5043 | kref_get(&sess->se_sess->sess_kref); | |
5044 | } | |
5045 | ||
5046 | iocb = a; | |
5047 | lun = a->u.isp24.fcp_cmnd.lun; | |
5048 | lun_size = sizeof(lun); | |
5049 | fn = a->u.isp24.fcp_cmnd.task_mgmt_flags; | |
5050 | unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun); | |
5051 | ||
5052 | rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0); | |
5053 | if (rc != 0) | |
5054 | goto out_term; | |
2d70c103 NB |
5055 | |
5056 | ha->tgt.tgt_ops->put_sess(sess); | |
08234e3a | 5057 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2d70c103 NB |
5058 | return; |
5059 | ||
5060 | out_term: | |
5061 | qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1); | |
2d70c103 NB |
5062 | if (sess) |
5063 | ha->tgt.tgt_ops->put_sess(sess); | |
08234e3a | 5064 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
2d70c103 NB |
5065 | } |
5066 | ||
5067 | static void qlt_sess_work_fn(struct work_struct *work) | |
5068 | { | |
5069 | struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work); | |
5070 | struct scsi_qla_host *vha = tgt->vha; | |
5071 | unsigned long flags; | |
5072 | ||
5073 | ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt); | |
5074 | ||
5075 | spin_lock_irqsave(&tgt->sess_work_lock, flags); | |
5076 | while (!list_empty(&tgt->sess_works_list)) { | |
5077 | struct qla_tgt_sess_work_param *prm = list_entry( | |
5078 | tgt->sess_works_list.next, typeof(*prm), | |
5079 | sess_works_list_entry); | |
5080 | ||
5081 | /* | |
5082 | * This work can be scheduled on several CPUs at time, so we | |
5083 | * must delete the entry to eliminate double processing | |
5084 | */ | |
5085 | list_del(&prm->sess_works_list_entry); | |
5086 | ||
5087 | spin_unlock_irqrestore(&tgt->sess_work_lock, flags); | |
5088 | ||
5089 | switch (prm->type) { | |
5090 | case QLA_TGT_SESS_WORK_ABORT: | |
5091 | qlt_abort_work(tgt, prm); | |
5092 | break; | |
5093 | case QLA_TGT_SESS_WORK_TM: | |
5094 | qlt_tmr_work(tgt, prm); | |
5095 | break; | |
5096 | default: | |
5097 | BUG_ON(1); | |
5098 | break; | |
5099 | } | |
5100 | ||
5101 | spin_lock_irqsave(&tgt->sess_work_lock, flags); | |
5102 | ||
5103 | kfree(prm); | |
5104 | } | |
5105 | spin_unlock_irqrestore(&tgt->sess_work_lock, flags); | |
5106 | } | |
5107 | ||
5108 | /* Must be called under tgt_host_action_mutex */ | |
5109 | int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha) | |
5110 | { | |
5111 | struct qla_tgt *tgt; | |
5112 | ||
5113 | if (!QLA_TGT_MODE_ENABLED()) | |
5114 | return 0; | |
5115 | ||
33c36c0a AE |
5116 | if (!IS_TGT_MODE_CAPABLE(ha)) { |
5117 | ql_log(ql_log_warn, base_vha, 0xe070, | |
5118 | "This adapter does not support target mode.\n"); | |
5119 | return 0; | |
5120 | } | |
5121 | ||
2d70c103 | 5122 | ql_dbg(ql_dbg_tgt, base_vha, 0xe03b, |
0e8cd71c | 5123 | "Registering target for host %ld(%p).\n", base_vha->host_no, ha); |
2d70c103 | 5124 | |
0e8cd71c | 5125 | BUG_ON(base_vha->vha_tgt.qla_tgt != NULL); |
2d70c103 NB |
5126 | |
5127 | tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL); | |
5128 | if (!tgt) { | |
5129 | ql_dbg(ql_dbg_tgt, base_vha, 0xe066, | |
5130 | "Unable to allocate struct qla_tgt\n"); | |
5131 | return -ENOMEM; | |
5132 | } | |
5133 | ||
5134 | if (!(base_vha->host->hostt->supported_mode & MODE_TARGET)) | |
5135 | base_vha->host->hostt->supported_mode |= MODE_TARGET; | |
5136 | ||
5137 | tgt->ha = ha; | |
5138 | tgt->vha = base_vha; | |
5139 | init_waitqueue_head(&tgt->waitQ); | |
5140 | INIT_LIST_HEAD(&tgt->sess_list); | |
5141 | INIT_LIST_HEAD(&tgt->del_sess_list); | |
5142 | INIT_DELAYED_WORK(&tgt->sess_del_work, | |
5143 | (void (*)(struct work_struct *))qlt_del_sess_work_fn); | |
5144 | spin_lock_init(&tgt->sess_work_lock); | |
5145 | INIT_WORK(&tgt->sess_work, qlt_sess_work_fn); | |
5146 | INIT_LIST_HEAD(&tgt->sess_works_list); | |
5147 | spin_lock_init(&tgt->srr_lock); | |
5148 | INIT_LIST_HEAD(&tgt->srr_ctio_list); | |
5149 | INIT_LIST_HEAD(&tgt->srr_imm_list); | |
5150 | INIT_WORK(&tgt->srr_work, qlt_handle_srr_work); | |
5151 | atomic_set(&tgt->tgt_global_resets_count, 0); | |
5152 | ||
0e8cd71c | 5153 | base_vha->vha_tgt.qla_tgt = tgt; |
2d70c103 NB |
5154 | |
5155 | ql_dbg(ql_dbg_tgt, base_vha, 0xe067, | |
5156 | "qla_target(%d): using 64 Bit PCI addressing", | |
5157 | base_vha->vp_idx); | |
5158 | tgt->tgt_enable_64bit_addr = 1; | |
5159 | /* 3 is reserved */ | |
5160 | tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3); | |
5161 | tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX; | |
5162 | tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX; | |
5163 | ||
ddb95145 NB |
5164 | if (base_vha->fc_vport) |
5165 | return 0; | |
5166 | ||
2d70c103 NB |
5167 | mutex_lock(&qla_tgt_mutex); |
5168 | list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist); | |
5169 | mutex_unlock(&qla_tgt_mutex); | |
5170 | ||
5171 | return 0; | |
5172 | } | |
5173 | ||
5174 | /* Must be called under tgt_host_action_mutex */ | |
5175 | int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha) | |
5176 | { | |
0e8cd71c | 5177 | if (!vha->vha_tgt.qla_tgt) |
2d70c103 NB |
5178 | return 0; |
5179 | ||
ddb95145 NB |
5180 | if (vha->fc_vport) { |
5181 | qlt_release(vha->vha_tgt.qla_tgt); | |
5182 | return 0; | |
5183 | } | |
33e79977 QT |
5184 | |
5185 | /* free left over qfull cmds */ | |
5186 | qlt_init_term_exchange(vha); | |
5187 | ||
2d70c103 | 5188 | mutex_lock(&qla_tgt_mutex); |
0e8cd71c | 5189 | list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry); |
2d70c103 NB |
5190 | mutex_unlock(&qla_tgt_mutex); |
5191 | ||
5192 | ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)", | |
5193 | vha->host_no, ha); | |
0e8cd71c | 5194 | qlt_release(vha->vha_tgt.qla_tgt); |
2d70c103 NB |
5195 | |
5196 | return 0; | |
5197 | } | |
5198 | ||
5199 | static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn, | |
5200 | unsigned char *b) | |
5201 | { | |
5202 | int i; | |
5203 | ||
5204 | pr_debug("qla2xxx HW vha->node_name: "); | |
5205 | for (i = 0; i < WWN_SIZE; i++) | |
5206 | pr_debug("%02x ", vha->node_name[i]); | |
5207 | pr_debug("\n"); | |
5208 | pr_debug("qla2xxx HW vha->port_name: "); | |
5209 | for (i = 0; i < WWN_SIZE; i++) | |
5210 | pr_debug("%02x ", vha->port_name[i]); | |
5211 | pr_debug("\n"); | |
5212 | ||
5213 | pr_debug("qla2xxx passed configfs WWPN: "); | |
5214 | put_unaligned_be64(wwpn, b); | |
5215 | for (i = 0; i < WWN_SIZE; i++) | |
5216 | pr_debug("%02x ", b[i]); | |
5217 | pr_debug("\n"); | |
5218 | } | |
5219 | ||
5220 | /** | |
5221 | * qla_tgt_lport_register - register lport with external module | |
5222 | * | |
5223 | * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops | |
5224 | * @wwpn: Passwd FC target WWPN | |
5225 | * @callback: lport initialization callback for tcm_qla2xxx code | |
5226 | * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data | |
5227 | */ | |
49a47f2c NB |
5228 | int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn, |
5229 | u64 npiv_wwpn, u64 npiv_wwnn, | |
5230 | int (*callback)(struct scsi_qla_host *, void *, u64, u64)) | |
2d70c103 NB |
5231 | { |
5232 | struct qla_tgt *tgt; | |
5233 | struct scsi_qla_host *vha; | |
5234 | struct qla_hw_data *ha; | |
5235 | struct Scsi_Host *host; | |
5236 | unsigned long flags; | |
5237 | int rc; | |
5238 | u8 b[WWN_SIZE]; | |
5239 | ||
5240 | mutex_lock(&qla_tgt_mutex); | |
5241 | list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) { | |
5242 | vha = tgt->vha; | |
5243 | ha = vha->hw; | |
5244 | ||
5245 | host = vha->host; | |
5246 | if (!host) | |
5247 | continue; | |
5248 | ||
2d70c103 NB |
5249 | if (!(host->hostt->supported_mode & MODE_TARGET)) |
5250 | continue; | |
5251 | ||
5252 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
49a47f2c | 5253 | if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) { |
2d70c103 NB |
5254 | pr_debug("MODE_TARGET already active on qla2xxx(%d)\n", |
5255 | host->host_no); | |
5256 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
5257 | continue; | |
5258 | } | |
ddb95145 NB |
5259 | if (tgt->tgt_stop) { |
5260 | pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n", | |
5261 | host->host_no); | |
5262 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
5263 | continue; | |
5264 | } | |
2d70c103 NB |
5265 | spin_unlock_irqrestore(&ha->hardware_lock, flags); |
5266 | ||
5267 | if (!scsi_host_get(host)) { | |
5268 | ql_dbg(ql_dbg_tgt, vha, 0xe068, | |
5269 | "Unable to scsi_host_get() for" | |
5270 | " qla2xxx scsi_host\n"); | |
5271 | continue; | |
5272 | } | |
49a47f2c | 5273 | qlt_lport_dump(vha, phys_wwpn, b); |
2d70c103 NB |
5274 | |
5275 | if (memcmp(vha->port_name, b, WWN_SIZE)) { | |
5276 | scsi_host_put(host); | |
5277 | continue; | |
5278 | } | |
49a47f2c NB |
5279 | rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn); |
5280 | if (rc != 0) | |
5281 | scsi_host_put(host); | |
5282 | ||
ddb95145 | 5283 | mutex_unlock(&qla_tgt_mutex); |
2d70c103 NB |
5284 | return rc; |
5285 | } | |
5286 | mutex_unlock(&qla_tgt_mutex); | |
5287 | ||
5288 | return -ENODEV; | |
5289 | } | |
5290 | EXPORT_SYMBOL(qlt_lport_register); | |
5291 | ||
5292 | /** | |
5293 | * qla_tgt_lport_deregister - Degister lport | |
5294 | * | |
5295 | * @vha: Registered scsi_qla_host pointer | |
5296 | */ | |
5297 | void qlt_lport_deregister(struct scsi_qla_host *vha) | |
5298 | { | |
5299 | struct qla_hw_data *ha = vha->hw; | |
5300 | struct Scsi_Host *sh = vha->host; | |
5301 | /* | |
5302 | * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data | |
5303 | */ | |
0e8cd71c | 5304 | vha->vha_tgt.target_lport_ptr = NULL; |
2d70c103 NB |
5305 | ha->tgt.tgt_ops = NULL; |
5306 | /* | |
5307 | * Release the Scsi_Host reference for the underlying qla2xxx host | |
5308 | */ | |
5309 | scsi_host_put(sh); | |
5310 | } | |
5311 | EXPORT_SYMBOL(qlt_lport_deregister); | |
5312 | ||
5313 | /* Must be called under HW lock */ | |
5314 | void qlt_set_mode(struct scsi_qla_host *vha) | |
5315 | { | |
5316 | struct qla_hw_data *ha = vha->hw; | |
5317 | ||
5318 | switch (ql2x_ini_mode) { | |
5319 | case QLA2XXX_INI_MODE_DISABLED: | |
5320 | case QLA2XXX_INI_MODE_EXCLUSIVE: | |
5321 | vha->host->active_mode = MODE_TARGET; | |
5322 | break; | |
5323 | case QLA2XXX_INI_MODE_ENABLED: | |
5324 | vha->host->active_mode |= MODE_TARGET; | |
5325 | break; | |
5326 | default: | |
5327 | break; | |
5328 | } | |
5329 | ||
5330 | if (ha->tgt.ini_mode_force_reverse) | |
5331 | qla_reverse_ini_mode(vha); | |
5332 | } | |
5333 | ||
5334 | /* Must be called under HW lock */ | |
5335 | void qlt_clear_mode(struct scsi_qla_host *vha) | |
5336 | { | |
5337 | struct qla_hw_data *ha = vha->hw; | |
5338 | ||
5339 | switch (ql2x_ini_mode) { | |
5340 | case QLA2XXX_INI_MODE_DISABLED: | |
5341 | vha->host->active_mode = MODE_UNKNOWN; | |
5342 | break; | |
5343 | case QLA2XXX_INI_MODE_EXCLUSIVE: | |
5344 | vha->host->active_mode = MODE_INITIATOR; | |
5345 | break; | |
5346 | case QLA2XXX_INI_MODE_ENABLED: | |
5347 | vha->host->active_mode &= ~MODE_TARGET; | |
5348 | break; | |
5349 | default: | |
5350 | break; | |
5351 | } | |
5352 | ||
5353 | if (ha->tgt.ini_mode_force_reverse) | |
5354 | qla_reverse_ini_mode(vha); | |
5355 | } | |
5356 | ||
5357 | /* | |
5358 | * qla_tgt_enable_vha - NO LOCK HELD | |
5359 | * | |
5360 | * host_reset, bring up w/ Target Mode Enabled | |
5361 | */ | |
5362 | void | |
5363 | qlt_enable_vha(struct scsi_qla_host *vha) | |
5364 | { | |
5365 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 5366 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 | 5367 | unsigned long flags; |
0e8cd71c | 5368 | scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev); |
2d70c103 NB |
5369 | |
5370 | if (!tgt) { | |
5371 | ql_dbg(ql_dbg_tgt, vha, 0xe069, | |
5372 | "Unable to locate qla_tgt pointer from" | |
5373 | " struct qla_hw_data\n"); | |
5374 | dump_stack(); | |
5375 | return; | |
5376 | } | |
5377 | ||
5378 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
5379 | tgt->tgt_stopped = 0; | |
5380 | qlt_set_mode(vha); | |
5381 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
5382 | ||
0e8cd71c SK |
5383 | if (vha->vp_idx) { |
5384 | qla24xx_disable_vp(vha); | |
5385 | qla24xx_enable_vp(vha); | |
5386 | } else { | |
5387 | set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags); | |
5388 | qla2xxx_wake_dpc(base_vha); | |
5389 | qla2x00_wait_for_hba_online(base_vha); | |
5390 | } | |
2d70c103 NB |
5391 | } |
5392 | EXPORT_SYMBOL(qlt_enable_vha); | |
5393 | ||
5394 | /* | |
5395 | * qla_tgt_disable_vha - NO LOCK HELD | |
5396 | * | |
5397 | * Disable Target Mode and reset the adapter | |
5398 | */ | |
5399 | void | |
5400 | qlt_disable_vha(struct scsi_qla_host *vha) | |
5401 | { | |
5402 | struct qla_hw_data *ha = vha->hw; | |
0e8cd71c | 5403 | struct qla_tgt *tgt = vha->vha_tgt.qla_tgt; |
2d70c103 NB |
5404 | unsigned long flags; |
5405 | ||
5406 | if (!tgt) { | |
5407 | ql_dbg(ql_dbg_tgt, vha, 0xe06a, | |
5408 | "Unable to locate qla_tgt pointer from" | |
5409 | " struct qla_hw_data\n"); | |
5410 | dump_stack(); | |
5411 | return; | |
5412 | } | |
5413 | ||
5414 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
5415 | qlt_clear_mode(vha); | |
5416 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
5417 | ||
5418 | set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags); | |
5419 | qla2xxx_wake_dpc(vha); | |
5420 | qla2x00_wait_for_hba_online(vha); | |
5421 | } | |
5422 | ||
5423 | /* | |
5424 | * Called from qla_init.c:qla24xx_vport_create() contex to setup | |
5425 | * the target mode specific struct scsi_qla_host and struct qla_hw_data | |
5426 | * members. | |
5427 | */ | |
5428 | void | |
5429 | qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha) | |
5430 | { | |
5431 | if (!qla_tgt_mode_enabled(vha)) | |
5432 | return; | |
5433 | ||
0e8cd71c SK |
5434 | vha->vha_tgt.qla_tgt = NULL; |
5435 | ||
5436 | mutex_init(&vha->vha_tgt.tgt_mutex); | |
5437 | mutex_init(&vha->vha_tgt.tgt_host_action_mutex); | |
2d70c103 NB |
5438 | |
5439 | qlt_clear_mode(vha); | |
5440 | ||
5441 | /* | |
5442 | * NOTE: Currently the value is kept the same for <24xx and | |
5443 | * >=24xx ISPs. If it is necessary to change it, | |
5444 | * the check should be added for specific ISPs, | |
5445 | * assigning the value appropriately. | |
5446 | */ | |
5447 | ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX; | |
0e8cd71c SK |
5448 | |
5449 | qlt_add_target(ha, vha); | |
2d70c103 NB |
5450 | } |
5451 | ||
5452 | void | |
5453 | qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req) | |
5454 | { | |
5455 | /* | |
5456 | * FC-4 Feature bit 0 indicates target functionality to the name server. | |
5457 | */ | |
5458 | if (qla_tgt_mode_enabled(vha)) { | |
5459 | if (qla_ini_mode_enabled(vha)) | |
5460 | ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1; | |
5461 | else | |
5462 | ct_req->req.rff_id.fc4_feature = BIT_0; | |
5463 | } else if (qla_ini_mode_enabled(vha)) { | |
5464 | ct_req->req.rff_id.fc4_feature = BIT_1; | |
5465 | } | |
5466 | } | |
5467 | ||
5468 | /* | |
5469 | * qlt_init_atio_q_entries() - Initializes ATIO queue entries. | |
5470 | * @ha: HA context | |
5471 | * | |
5472 | * Beginning of ATIO ring has initialization control block already built | |
5473 | * by nvram config routine. | |
5474 | * | |
5475 | * Returns 0 on success. | |
5476 | */ | |
5477 | void | |
5478 | qlt_init_atio_q_entries(struct scsi_qla_host *vha) | |
5479 | { | |
5480 | struct qla_hw_data *ha = vha->hw; | |
5481 | uint16_t cnt; | |
5482 | struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring; | |
5483 | ||
5484 | if (!qla_tgt_mode_enabled(vha)) | |
5485 | return; | |
5486 | ||
5487 | for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) { | |
5488 | pkt->u.raw.signature = ATIO_PROCESSED; | |
5489 | pkt++; | |
5490 | } | |
5491 | ||
5492 | } | |
5493 | ||
5494 | /* | |
5495 | * qlt_24xx_process_atio_queue() - Process ATIO queue entries. | |
5496 | * @ha: SCSI driver HA context | |
5497 | */ | |
5498 | void | |
5499 | qlt_24xx_process_atio_queue(struct scsi_qla_host *vha) | |
5500 | { | |
5501 | struct qla_hw_data *ha = vha->hw; | |
2d70c103 NB |
5502 | struct atio_from_isp *pkt; |
5503 | int cnt, i; | |
5504 | ||
5505 | if (!vha->flags.online) | |
5506 | return; | |
5507 | ||
5508 | while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) { | |
5509 | pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr; | |
5510 | cnt = pkt->u.raw.entry_count; | |
5511 | ||
5512 | qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt); | |
5513 | ||
5514 | for (i = 0; i < cnt; i++) { | |
5515 | ha->tgt.atio_ring_index++; | |
5516 | if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) { | |
5517 | ha->tgt.atio_ring_index = 0; | |
5518 | ha->tgt.atio_ring_ptr = ha->tgt.atio_ring; | |
5519 | } else | |
5520 | ha->tgt.atio_ring_ptr++; | |
5521 | ||
5522 | pkt->u.raw.signature = ATIO_PROCESSED; | |
5523 | pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr; | |
5524 | } | |
5525 | wmb(); | |
5526 | } | |
5527 | ||
5528 | /* Adjust ring index */ | |
aa230bc5 | 5529 | WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index); |
2d70c103 NB |
5530 | } |
5531 | ||
5532 | void | |
aa230bc5 | 5533 | qlt_24xx_config_rings(struct scsi_qla_host *vha) |
2d70c103 NB |
5534 | { |
5535 | struct qla_hw_data *ha = vha->hw; | |
aa230bc5 AE |
5536 | if (!QLA_TGT_MODE_ENABLED()) |
5537 | return; | |
2d70c103 | 5538 | |
aa230bc5 AE |
5539 | WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0); |
5540 | WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0); | |
5541 | RD_REG_DWORD(ISP_ATIO_Q_OUT(vha)); | |
5542 | ||
5543 | if (IS_ATIO_MSIX_CAPABLE(ha)) { | |
5544 | struct qla_msix_entry *msix = &ha->msix_entries[2]; | |
5545 | struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb; | |
5546 | ||
5547 | icb->msix_atio = cpu_to_le16(msix->entry); | |
5548 | ql_dbg(ql_dbg_init, vha, 0xf072, | |
5549 | "Registering ICB vector 0x%x for atio que.\n", | |
5550 | msix->entry); | |
2d70c103 NB |
5551 | } |
5552 | } | |
5553 | ||
5554 | void | |
5555 | qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv) | |
5556 | { | |
5557 | struct qla_hw_data *ha = vha->hw; | |
5558 | ||
5559 | if (qla_tgt_mode_enabled(vha)) { | |
5560 | if (!ha->tgt.saved_set) { | |
5561 | /* We save only once */ | |
5562 | ha->tgt.saved_exchange_count = nv->exchange_count; | |
5563 | ha->tgt.saved_firmware_options_1 = | |
5564 | nv->firmware_options_1; | |
5565 | ha->tgt.saved_firmware_options_2 = | |
5566 | nv->firmware_options_2; | |
5567 | ha->tgt.saved_firmware_options_3 = | |
5568 | nv->firmware_options_3; | |
5569 | ha->tgt.saved_set = 1; | |
5570 | } | |
5571 | ||
5572 | nv->exchange_count = __constant_cpu_to_le16(0xFFFF); | |
5573 | ||
5574 | /* Enable target mode */ | |
5575 | nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4); | |
5576 | ||
5577 | /* Disable ini mode, if requested */ | |
5578 | if (!qla_ini_mode_enabled(vha)) | |
5579 | nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5); | |
5580 | ||
5581 | /* Disable Full Login after LIP */ | |
5582 | nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13); | |
5583 | /* Enable initial LIP */ | |
5584 | nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9); | |
d154f350 AE |
5585 | if (ql2xtgt_tape_enable) |
5586 | /* Enable FC Tape support */ | |
5587 | nv->firmware_options_2 |= cpu_to_le32(BIT_12); | |
5588 | else | |
5589 | /* Disable FC Tape support */ | |
5590 | nv->firmware_options_2 &= cpu_to_le32(~BIT_12); | |
5591 | ||
2d70c103 NB |
5592 | /* Disable Full Login after LIP */ |
5593 | nv->host_p &= __constant_cpu_to_le32(~BIT_10); | |
5594 | /* Enable target PRLI control */ | |
5595 | nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14); | |
5596 | } else { | |
5597 | if (ha->tgt.saved_set) { | |
5598 | nv->exchange_count = ha->tgt.saved_exchange_count; | |
5599 | nv->firmware_options_1 = | |
5600 | ha->tgt.saved_firmware_options_1; | |
5601 | nv->firmware_options_2 = | |
5602 | ha->tgt.saved_firmware_options_2; | |
5603 | nv->firmware_options_3 = | |
5604 | ha->tgt.saved_firmware_options_3; | |
5605 | } | |
5606 | return; | |
5607 | } | |
5608 | ||
5609 | /* out-of-order frames reassembly */ | |
5610 | nv->firmware_options_3 |= BIT_6|BIT_9; | |
5611 | ||
5612 | if (ha->tgt.enable_class_2) { | |
5613 | if (vha->flags.init_done) | |
5614 | fc_host_supported_classes(vha->host) = | |
5615 | FC_COS_CLASS2 | FC_COS_CLASS3; | |
5616 | ||
5617 | nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8); | |
5618 | } else { | |
5619 | if (vha->flags.init_done) | |
5620 | fc_host_supported_classes(vha->host) = FC_COS_CLASS3; | |
5621 | ||
5622 | nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8); | |
5623 | } | |
5624 | } | |
5625 | ||
5626 | void | |
5627 | qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha, | |
5628 | struct init_cb_24xx *icb) | |
5629 | { | |
5630 | struct qla_hw_data *ha = vha->hw; | |
5631 | ||
5632 | if (ha->tgt.node_name_set) { | |
5633 | memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE); | |
5634 | icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14); | |
5635 | } | |
5636 | } | |
5637 | ||
aa230bc5 AE |
5638 | void |
5639 | qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv) | |
5640 | { | |
5641 | struct qla_hw_data *ha = vha->hw; | |
5642 | ||
5643 | if (!QLA_TGT_MODE_ENABLED()) | |
5644 | return; | |
5645 | ||
5646 | if (qla_tgt_mode_enabled(vha)) { | |
5647 | if (!ha->tgt.saved_set) { | |
5648 | /* We save only once */ | |
5649 | ha->tgt.saved_exchange_count = nv->exchange_count; | |
5650 | ha->tgt.saved_firmware_options_1 = | |
5651 | nv->firmware_options_1; | |
5652 | ha->tgt.saved_firmware_options_2 = | |
5653 | nv->firmware_options_2; | |
5654 | ha->tgt.saved_firmware_options_3 = | |
5655 | nv->firmware_options_3; | |
5656 | ha->tgt.saved_set = 1; | |
5657 | } | |
5658 | ||
5659 | nv->exchange_count = __constant_cpu_to_le16(0xFFFF); | |
5660 | ||
5661 | /* Enable target mode */ | |
5662 | nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4); | |
5663 | ||
5664 | /* Disable ini mode, if requested */ | |
5665 | if (!qla_ini_mode_enabled(vha)) | |
5666 | nv->firmware_options_1 |= | |
5667 | __constant_cpu_to_le32(BIT_5); | |
5668 | ||
5669 | /* Disable Full Login after LIP */ | |
5670 | nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13); | |
5671 | /* Enable initial LIP */ | |
5672 | nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9); | |
d154f350 AE |
5673 | if (ql2xtgt_tape_enable) |
5674 | /* Enable FC tape support */ | |
5675 | nv->firmware_options_2 |= cpu_to_le32(BIT_12); | |
5676 | else | |
5677 | /* Disable FC tape support */ | |
5678 | nv->firmware_options_2 &= cpu_to_le32(~BIT_12); | |
5679 | ||
aa230bc5 AE |
5680 | /* Disable Full Login after LIP */ |
5681 | nv->host_p &= __constant_cpu_to_le32(~BIT_10); | |
5682 | /* Enable target PRLI control */ | |
5683 | nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14); | |
5684 | } else { | |
5685 | if (ha->tgt.saved_set) { | |
5686 | nv->exchange_count = ha->tgt.saved_exchange_count; | |
5687 | nv->firmware_options_1 = | |
5688 | ha->tgt.saved_firmware_options_1; | |
5689 | nv->firmware_options_2 = | |
5690 | ha->tgt.saved_firmware_options_2; | |
5691 | nv->firmware_options_3 = | |
5692 | ha->tgt.saved_firmware_options_3; | |
5693 | } | |
5694 | return; | |
5695 | } | |
5696 | ||
5697 | /* out-of-order frames reassembly */ | |
5698 | nv->firmware_options_3 |= BIT_6|BIT_9; | |
5699 | ||
5700 | if (ha->tgt.enable_class_2) { | |
5701 | if (vha->flags.init_done) | |
5702 | fc_host_supported_classes(vha->host) = | |
5703 | FC_COS_CLASS2 | FC_COS_CLASS3; | |
5704 | ||
5705 | nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8); | |
5706 | } else { | |
5707 | if (vha->flags.init_done) | |
5708 | fc_host_supported_classes(vha->host) = FC_COS_CLASS3; | |
5709 | ||
5710 | nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8); | |
5711 | } | |
5712 | } | |
5713 | ||
5714 | void | |
5715 | qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha, | |
5716 | struct init_cb_81xx *icb) | |
5717 | { | |
5718 | struct qla_hw_data *ha = vha->hw; | |
5719 | ||
5720 | if (!QLA_TGT_MODE_ENABLED()) | |
5721 | return; | |
5722 | ||
5723 | if (ha->tgt.node_name_set) { | |
5724 | memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE); | |
5725 | icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14); | |
5726 | } | |
5727 | } | |
5728 | ||
5729 | void | |
5730 | qlt_83xx_iospace_config(struct qla_hw_data *ha) | |
5731 | { | |
5732 | if (!QLA_TGT_MODE_ENABLED()) | |
5733 | return; | |
5734 | ||
5735 | ha->msix_count += 1; /* For ATIO Q */ | |
5736 | } | |
5737 | ||
2d70c103 NB |
5738 | int |
5739 | qlt_24xx_process_response_error(struct scsi_qla_host *vha, | |
5740 | struct sts_entry_24xx *pkt) | |
5741 | { | |
5742 | switch (pkt->entry_type) { | |
5743 | case ABTS_RECV_24XX: | |
5744 | case ABTS_RESP_24XX: | |
5745 | case CTIO_TYPE7: | |
5746 | case NOTIFY_ACK_TYPE: | |
f83adb61 | 5747 | case CTIO_CRC2: |
2d70c103 NB |
5748 | return 1; |
5749 | default: | |
5750 | return 0; | |
5751 | } | |
5752 | } | |
5753 | ||
5754 | void | |
5755 | qlt_modify_vp_config(struct scsi_qla_host *vha, | |
5756 | struct vp_config_entry_24xx *vpmod) | |
5757 | { | |
5758 | if (qla_tgt_mode_enabled(vha)) | |
5759 | vpmod->options_idx1 &= ~BIT_5; | |
5760 | /* Disable ini mode, if requested */ | |
5761 | if (!qla_ini_mode_enabled(vha)) | |
5762 | vpmod->options_idx1 &= ~BIT_4; | |
5763 | } | |
5764 | ||
5765 | void | |
5766 | qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha) | |
5767 | { | |
5768 | if (!QLA_TGT_MODE_ENABLED()) | |
5769 | return; | |
5770 | ||
aa230bc5 AE |
5771 | if (ha->mqenable || IS_QLA83XX(ha)) { |
5772 | ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in; | |
5773 | ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out; | |
5774 | } else { | |
5775 | ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in; | |
5776 | ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out; | |
5777 | } | |
5778 | ||
0e8cd71c SK |
5779 | mutex_init(&base_vha->vha_tgt.tgt_mutex); |
5780 | mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex); | |
2d70c103 NB |
5781 | qlt_clear_mode(base_vha); |
5782 | } | |
5783 | ||
aa230bc5 AE |
5784 | irqreturn_t |
5785 | qla83xx_msix_atio_q(int irq, void *dev_id) | |
5786 | { | |
5787 | struct rsp_que *rsp; | |
5788 | scsi_qla_host_t *vha; | |
5789 | struct qla_hw_data *ha; | |
5790 | unsigned long flags; | |
5791 | ||
5792 | rsp = (struct rsp_que *) dev_id; | |
5793 | ha = rsp->hw; | |
5794 | vha = pci_get_drvdata(ha->pdev); | |
5795 | ||
5796 | spin_lock_irqsave(&ha->hardware_lock, flags); | |
5797 | ||
5798 | qlt_24xx_process_atio_queue(vha); | |
5799 | qla24xx_process_response_queue(vha, rsp); | |
5800 | ||
5801 | spin_unlock_irqrestore(&ha->hardware_lock, flags); | |
5802 | ||
5803 | return IRQ_HANDLED; | |
5804 | } | |
5805 | ||
2d70c103 NB |
5806 | int |
5807 | qlt_mem_alloc(struct qla_hw_data *ha) | |
5808 | { | |
5809 | if (!QLA_TGT_MODE_ENABLED()) | |
5810 | return 0; | |
5811 | ||
5812 | ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) * | |
5813 | MAX_MULTI_ID_FABRIC, GFP_KERNEL); | |
5814 | if (!ha->tgt.tgt_vp_map) | |
5815 | return -ENOMEM; | |
5816 | ||
5817 | ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev, | |
5818 | (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp), | |
5819 | &ha->tgt.atio_dma, GFP_KERNEL); | |
5820 | if (!ha->tgt.atio_ring) { | |
5821 | kfree(ha->tgt.tgt_vp_map); | |
5822 | return -ENOMEM; | |
5823 | } | |
5824 | return 0; | |
5825 | } | |
5826 | ||
5827 | void | |
5828 | qlt_mem_free(struct qla_hw_data *ha) | |
5829 | { | |
5830 | if (!QLA_TGT_MODE_ENABLED()) | |
5831 | return; | |
5832 | ||
5833 | if (ha->tgt.atio_ring) { | |
5834 | dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) * | |
5835 | sizeof(struct atio_from_isp), ha->tgt.atio_ring, | |
5836 | ha->tgt.atio_dma); | |
5837 | } | |
5838 | kfree(ha->tgt.tgt_vp_map); | |
5839 | } | |
5840 | ||
5841 | /* vport_slock to be held by the caller */ | |
5842 | void | |
5843 | qlt_update_vp_map(struct scsi_qla_host *vha, int cmd) | |
5844 | { | |
5845 | if (!QLA_TGT_MODE_ENABLED()) | |
5846 | return; | |
5847 | ||
5848 | switch (cmd) { | |
5849 | case SET_VP_IDX: | |
5850 | vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha; | |
5851 | break; | |
5852 | case SET_AL_PA: | |
5853 | vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx; | |
5854 | break; | |
5855 | case RESET_VP_IDX: | |
5856 | vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL; | |
5857 | break; | |
5858 | case RESET_AL_PA: | |
5859 | vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0; | |
5860 | break; | |
5861 | } | |
5862 | } | |
5863 | ||
5864 | static int __init qlt_parse_ini_mode(void) | |
5865 | { | |
5866 | if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0) | |
5867 | ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE; | |
5868 | else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0) | |
5869 | ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED; | |
5870 | else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0) | |
5871 | ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED; | |
5872 | else | |
5873 | return false; | |
5874 | ||
5875 | return true; | |
5876 | } | |
5877 | ||
5878 | int __init qlt_init(void) | |
5879 | { | |
5880 | int ret; | |
5881 | ||
5882 | if (!qlt_parse_ini_mode()) { | |
5883 | ql_log(ql_log_fatal, NULL, 0xe06b, | |
5884 | "qlt_parse_ini_mode() failed\n"); | |
5885 | return -EINVAL; | |
5886 | } | |
5887 | ||
5888 | if (!QLA_TGT_MODE_ENABLED()) | |
5889 | return 0; | |
5890 | ||
2d70c103 NB |
5891 | qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep", |
5892 | sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct | |
5893 | qla_tgt_mgmt_cmd), 0, NULL); | |
5894 | if (!qla_tgt_mgmt_cmd_cachep) { | |
5895 | ql_log(ql_log_fatal, NULL, 0xe06d, | |
5896 | "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n"); | |
51a07f84 | 5897 | return -ENOMEM; |
2d70c103 NB |
5898 | } |
5899 | ||
5900 | qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab, | |
5901 | mempool_free_slab, qla_tgt_mgmt_cmd_cachep); | |
5902 | if (!qla_tgt_mgmt_cmd_mempool) { | |
5903 | ql_log(ql_log_fatal, NULL, 0xe06e, | |
5904 | "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n"); | |
5905 | ret = -ENOMEM; | |
5906 | goto out_mgmt_cmd_cachep; | |
5907 | } | |
5908 | ||
5909 | qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0); | |
5910 | if (!qla_tgt_wq) { | |
5911 | ql_log(ql_log_fatal, NULL, 0xe06f, | |
5912 | "alloc_workqueue for qla_tgt_wq failed\n"); | |
5913 | ret = -ENOMEM; | |
5914 | goto out_cmd_mempool; | |
5915 | } | |
5916 | /* | |
5917 | * Return 1 to signal that initiator-mode is being disabled | |
5918 | */ | |
5919 | return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0; | |
5920 | ||
5921 | out_cmd_mempool: | |
5922 | mempool_destroy(qla_tgt_mgmt_cmd_mempool); | |
5923 | out_mgmt_cmd_cachep: | |
5924 | kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep); | |
2d70c103 NB |
5925 | return ret; |
5926 | } | |
5927 | ||
5928 | void qlt_exit(void) | |
5929 | { | |
5930 | if (!QLA_TGT_MODE_ENABLED()) | |
5931 | return; | |
5932 | ||
5933 | destroy_workqueue(qla_tgt_wq); | |
5934 | mempool_destroy(qla_tgt_mgmt_cmd_mempool); | |
5935 | kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep); | |
2d70c103 | 5936 | } |