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