]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/scsi/qla2xxx/qla_inline.h
scsi: qla2xxx: Fix race condition for resource cleanup
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / qla2xxx / qla_inline.h
CommitLineData
1da177e4 1/*
fa90c54f 2 * QLogic Fibre Channel HBA Driver
bd21eaf9 3 * Copyright (c) 2003-2014 QLogic Corporation
1da177e4 4 *
fa90c54f 5 * See LICENSE.qla2xxx for copyright and licensing details.
1da177e4
LT
6 */
7
f83adb61 8#include "qla_target.h"
8ae6d9c7
GM
9/**
10 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
11 * Continuation Type 1 IOCBs to allocate.
12 *
2db6228d 13 * @vha: HA context
8ae6d9c7
GM
14 * @dsds: number of data segment decriptors needed
15 *
16 * Returns the number of IOCB entries needed to store @dsds.
17 */
18static inline uint16_t
19qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
20{
21 uint16_t iocbs;
22
23 iocbs = 1;
24 if (dsds > 1) {
25 iocbs += (dsds - 1) / 5;
26 if ((dsds - 1) % 5)
27 iocbs++;
28 }
29 return iocbs;
30}
31
1da177e4
LT
32/*
33 * qla2x00_debounce_register
34 * Debounce register.
35 *
36 * Input:
37 * port = register address.
38 *
39 * Returns:
40 * register value.
41 */
42static __inline__ uint16_t
fa2a1ce5 43qla2x00_debounce_register(volatile uint16_t __iomem *addr)
1da177e4
LT
44{
45 volatile uint16_t first;
46 volatile uint16_t second;
47
48 do {
49 first = RD_REG_WORD(addr);
50 barrier();
51 cpu_relax();
52 second = RD_REG_WORD(addr);
53 } while (first != second);
54
55 return (first);
56}
57
fa2a1ce5 58static inline void
e315cd28 59qla2x00_poll(struct rsp_que *rsp)
1da177e4 60{
e315cd28 61 struct qla_hw_data *ha = rsp->hw;
b3a8aa90 62
7ec0effd 63 if (IS_P3P_TYPE(ha))
a9083016
GM
64 qla82xx_poll(0, rsp);
65 else
66 ha->isp_ops->intr_handler(0, rsp);
1da177e4
LT
67}
68
2b6c0cee
AV
69static inline uint8_t *
70host_to_fcp_swap(uint8_t *fcp, uint32_t bsize)
71{
72 uint32_t *ifcp = (uint32_t *) fcp;
73 uint32_t *ofcp = (uint32_t *) fcp;
74 uint32_t iter = bsize >> 2;
75
76 for (; iter ; iter--)
77 *ofcp++ = swab32(*ifcp++);
78
79 return fcp;
80}
3d71644c 81
8ae6d9c7
GM
82static inline void
83host_to_adap(uint8_t *src, uint8_t *dst, uint32_t bsize)
84{
85 uint32_t *isrc = (uint32_t *) src;
1f8deefe 86 __le32 *odest = (__le32 *) dst;
8ae6d9c7
GM
87 uint32_t iter = bsize >> 2;
88
da08ef5c
JC
89 for ( ; iter--; isrc++)
90 *odest++ = cpu_to_le32(*isrc);
8ae6d9c7
GM
91}
92
5f16b331
CD
93static inline void
94qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
95{
96 int i;
97
98 if (IS_FWI2_CAPABLE(ha))
99 return;
100
101 for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
102 set_bit(i, ha->loop_id_map);
103 set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
104 set_bit(BROADCAST, ha->loop_id_map);
105}
106
3d71644c 107static inline int
e315cd28 108qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
3d71644c 109{
e315cd28 110 struct qla_hw_data *ha = vha->hw;
e428924c 111 if (IS_FWI2_CAPABLE(ha))
3d71644c
AV
112 return (loop_id > NPH_LAST_HANDLE);
113
e315cd28 114 return ((loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
3d71644c 115 loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST);
17d98630 116}
bad75002 117
5f16b331
CD
118static inline void
119qla2x00_clear_loop_id(fc_port_t *fcport) {
120 struct qla_hw_data *ha = fcport->vha->hw;
121
122 if (fcport->loop_id == FC_NO_LOOP_ID ||
123 qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
124 return;
125
126 clear_bit(fcport->loop_id, ha->loop_id_map);
127 fcport->loop_id = FC_NO_LOOP_ID;
128}
129
bad75002 130static inline void
d5ff0eed 131qla2x00_clean_dsd_pool(struct qla_hw_data *ha, struct crc_context *ctx)
bad75002 132{
d5ff0eed 133 struct dsd_dma *dsd, *tdsd;
bad75002
AE
134
135 /* clean up allocated prev pool */
d5ff0eed
JC
136 list_for_each_entry_safe(dsd, tdsd, &ctx->dsd_list, list) {
137 dma_pool_free(ha->dl_dma_pool, dsd->dsd_addr,
138 dsd->dsd_list_dma);
139 list_del(&dsd->list);
140 kfree(dsd);
bad75002 141 }
9ba56b95 142 INIT_LIST_HEAD(&ctx->dsd_list);
bad75002 143}
ec426e10
CD
144
145static inline void
146qla2x00_set_fcport_state(fc_port_t *fcport, int state)
147{
148 int old_state;
149
150 old_state = atomic_read(&fcport->state);
151 atomic_set(&fcport->state, state);
152
153 /* Don't print state transitions during initial allocation of fcport */
154 if (old_state && old_state != state) {
7c3df132 155 ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
726b8548
QT
156 "FCPort %8phC state transitioned from %s to %s - "
157 "portid=%02x%02x%02x.\n", fcport->port_name,
ec426e10
CD
158 port_state_str[old_state], port_state_str[state],
159 fcport->d_id.b.domain, fcport->d_id.b.area,
7c3df132 160 fcport->d_id.b.al_pa);
ec426e10
CD
161 }
162}
8cb2049c
AE
163
164static inline int
e02587d7 165qla2x00_hba_err_chk_enabled(srb_t *sp)
8cb2049c 166{
e02587d7
AE
167 /*
168 * Uncomment when corresponding SCSI changes are done.
169 *
170 if (!sp->cmd->prot_chk)
171 return 0;
172 *
173 */
9ba56b95 174 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
8cb2049c
AE
175 case SCSI_PROT_READ_STRIP:
176 case SCSI_PROT_WRITE_INSERT:
177 if (ql2xenablehba_err_chk >= 1)
178 return 1;
179 break;
180 case SCSI_PROT_READ_PASS:
181 case SCSI_PROT_WRITE_PASS:
182 if (ql2xenablehba_err_chk >= 2)
183 return 1;
184 break;
185 case SCSI_PROT_READ_INSERT:
186 case SCSI_PROT_WRITE_STRIP:
187 return 1;
188 }
189 return 0;
190}
d051a5aa
AV
191
192static inline int
193qla2x00_reset_active(scsi_qla_host_t *vha)
194{
195 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
196
197 /* Test appropriate base-vha and vha flags. */
198 return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
199 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
200 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
201 test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
202 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
203}
9ba56b95 204
22ebde16
QT
205static inline int
206qla2x00_chip_is_down(scsi_qla_host_t *vha)
207{
208 return (qla2x00_reset_active(vha) || !vha->hw->flags.fw_started);
209}
210
d7459527 211static inline srb_t *
6a629468
QT
212qla2xxx_get_qpair_sp(scsi_qla_host_t *vha, struct qla_qpair *qpair,
213 fc_port_t *fcport, gfp_t flag)
d7459527
MH
214{
215 srb_t *sp = NULL;
216 uint8_t bail;
217
218 QLA_QPAIR_MARK_BUSY(qpair, bail);
219 if (unlikely(bail))
220 return NULL;
221
222 sp = mempool_alloc(qpair->srb_mempool, flag);
223 if (!sp)
224 goto done;
225
226 memset(sp, 0, sizeof(*sp));
227 sp->fcport = fcport;
228 sp->iocbs = 1;
6a629468
QT
229 sp->vha = vha;
230 sp->qpair = qpair;
231 sp->cmd_type = TYPE_SRB;
e3dde080
QT
232 INIT_LIST_HEAD(&sp->elem);
233
d7459527
MH
234done:
235 if (!sp)
236 QLA_QPAIR_MARK_NOT_BUSY(qpair);
237 return sp;
238}
239
240static inline void
241qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
242{
243 mempool_free(sp, qpair->srb_mempool);
244 QLA_QPAIR_MARK_NOT_BUSY(qpair);
245}
246
9ba56b95
GM
247static inline srb_t *
248qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
249{
250 srb_t *sp = NULL;
9ba56b95 251 uint8_t bail;
6a629468 252 struct qla_qpair *qpair;
9ba56b95
GM
253
254 QLA_VHA_MARK_BUSY(vha, bail);
255 if (unlikely(bail))
256 return NULL;
257
6a629468
QT
258 qpair = vha->hw->base_qpair;
259 sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, flag);
9ba56b95
GM
260 if (!sp)
261 goto done;
262
726b8548 263 sp->vha = vha;
9ba56b95
GM
264done:
265 if (!sp)
266 QLA_VHA_MARK_NOT_BUSY(vha);
267 return sp;
268}
269
b00ee7d7 270static inline void
25ff6af1 271qla2x00_rel_sp(srb_t *sp)
b00ee7d7 272{
25ff6af1 273 QLA_VHA_MARK_NOT_BUSY(sp->vha);
6a629468 274 qla2xxx_rel_qpair_sp(sp->qpair, sp);
b00ee7d7
CD
275}
276
9ba56b95
GM
277static inline void
278qla2x00_init_timer(srb_t *sp, unsigned long tmo)
279{
8e5f4ba0 280 timer_setup(&sp->u.iocb_cmd.timer, qla2x00_sp_timeout, 0);
9ba56b95 281 sp->u.iocb_cmd.timer.expires = jiffies + tmo * HZ;
9ba56b95 282 sp->free = qla2x00_sp_free;
2853192e 283 init_completion(&sp->comp);
25ff6af1 284 if (IS_QLAFX00(sp->vha->hw) && (sp->type == SRB_FXIOCB_DCMD))
8ae6d9c7 285 init_completion(&sp->u.iocb_cmd.u.fxiocb.fxiocb_comp);
e74e7d95 286 add_timer(&sp->u.iocb_cmd.timer);
9ba56b95 287}
642ef983
CD
288
289static inline int
290qla2x00_gid_list_size(struct qla_hw_data *ha)
291{
8ae6d9c7
GM
292 if (IS_QLAFX00(ha))
293 return sizeof(uint32_t) * 32;
294 else
295 return sizeof(struct gid_list_info) * ha->max_fibre_devices;
642ef983 296}
3c290d0b 297
36439832 298static inline void
299qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
300{
301 if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
302 (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
303 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
304 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
305 complete(&ha->mbx_intr_comp);
306 }
307}
e05fe292
CD
308
309static inline void
310qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
311{
312 if (retry_delay)
313 fcport->retry_delay_timestamp = jiffies +
314 (retry_delay * HZ / 10);
315}
99e1b683
QT
316
317static inline bool
318qla_is_exch_offld_enabled(struct scsi_qla_host *vha)
319{
320 if (qla_ini_mode_enabled(vha) &&
321 (ql2xiniexchg > FW_DEF_EXCHANGES_CNT))
322 return true;
323 else if (qla_tgt_mode_enabled(vha) &&
324 (ql2xexchoffld > FW_DEF_EXCHANGES_CNT))
325 return true;
326 else if (qla_dual_mode_enabled(vha) &&
327 ((ql2xiniexchg + ql2xexchoffld) > FW_DEF_EXCHANGES_CNT))
328 return true;
329 else
330 return false;
331}
e326d22a
QT
332
333static inline void
334qla_cpu_update(struct qla_qpair *qpair, uint16_t cpuid)
335{
336 qpair->cpuid = cpuid;
337
338 if (!list_empty(&qpair->hints_list)) {
339 struct qla_qpair_hint *h;
340
341 list_for_each_entry(h, &qpair->hints_list, hint_elem)
342 h->cpuid = qpair->cpuid;
343 }
344}
345
346static inline struct qla_qpair_hint *
347qla_qpair_to_hint(struct qla_tgt *tgt, struct qla_qpair *qpair)
348{
349 struct qla_qpair_hint *h;
350 u16 i;
351
352 for (i = 0; i < tgt->ha->max_qpairs + 1; i++) {
353 h = &tgt->qphints[i];
354 if (h->qpair == qpair)
355 return h;
356 }
357
358 return NULL;
359}
8abfa9e2
QT
360
361static inline void
362qla_83xx_start_iocbs(struct qla_qpair *qpair)
363{
364 struct req_que *req = qpair->req;
365
366 req->ring_index++;
367 if (req->ring_index == req->length) {
368 req->ring_index = 0;
369 req->ring_ptr = req->ring;
370 } else
371 req->ring_ptr++;
372
373 WRT_REG_DWORD(req->req_q_in, req->ring_index);
374}