GNU Linux-libre 5.4.200-gnu1
[releases.git] / drivers / scsi / qla2xxx / qla_inline.h
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7
8 #include "qla_target.h"
9 /**
10  * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
11  * Continuation Type 1 IOCBs to allocate.
12  *
13  * @vha: HA context
14  * @dsds: number of data segment decriptors needed
15  *
16  * Returns the number of IOCB entries needed to store @dsds.
17  */
18 static inline uint16_t
19 qla24xx_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
32 /*
33  * qla2x00_debounce_register
34  *      Debounce register.
35  *
36  * Input:
37  *      port = register address.
38  *
39  * Returns:
40  *      register value.
41  */
42 static __inline__ uint16_t
43 qla2x00_debounce_register(volatile uint16_t __iomem *addr)
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
58 static inline void
59 qla2x00_poll(struct rsp_que *rsp)
60 {
61         struct qla_hw_data *ha = rsp->hw;
62
63         if (IS_P3P_TYPE(ha))
64                 qla82xx_poll(0, rsp);
65         else
66                 ha->isp_ops->intr_handler(0, rsp);
67 }
68
69 static inline uint8_t *
70 host_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 }
81
82 static inline void
83 host_to_adap(uint8_t *src, uint8_t *dst, uint32_t bsize)
84 {
85         uint32_t *isrc = (uint32_t *) src;
86         __le32 *odest = (__le32 *) dst;
87         uint32_t iter = bsize >> 2;
88
89         for ( ; iter--; isrc++)
90                 *odest++ = cpu_to_le32(*isrc);
91 }
92
93 static inline void
94 qla2x00_clean_dsd_pool(struct qla_hw_data *ha, struct crc_context *ctx)
95 {
96         struct dsd_dma *dsd, *tdsd;
97
98         /* clean up allocated prev pool */
99         list_for_each_entry_safe(dsd, tdsd, &ctx->dsd_list, list) {
100                 dma_pool_free(ha->dl_dma_pool, dsd->dsd_addr,
101                     dsd->dsd_list_dma);
102                 list_del(&dsd->list);
103                 kfree(dsd);
104         }
105         INIT_LIST_HEAD(&ctx->dsd_list);
106 }
107
108 static inline void
109 qla2x00_set_fcport_disc_state(fc_port_t *fcport, int state)
110 {
111         int old_val;
112         uint8_t shiftbits, mask;
113
114         /* This will have to change when the max no. of states > 16 */
115         shiftbits = 4;
116         mask = (1 << shiftbits) - 1;
117
118         fcport->disc_state = state;
119         while (1) {
120                 old_val = atomic_read(&fcport->shadow_disc_state);
121                 if (old_val == atomic_cmpxchg(&fcport->shadow_disc_state,
122                     old_val, (old_val << shiftbits) | state)) {
123                         ql_dbg(ql_dbg_disc, fcport->vha, 0x2134,
124                             "FCPort %8phC disc_state transition: %s to %s - portid=%06x.\n",
125                             fcport->port_name, port_dstate_str[old_val & mask],
126                             port_dstate_str[state], fcport->d_id.b24);
127                         return;
128                 }
129         }
130 }
131
132 static inline int
133 qla2x00_hba_err_chk_enabled(srb_t *sp)
134 {
135         /*
136          * Uncomment when corresponding SCSI changes are done.
137          *
138         if (!sp->cmd->prot_chk)
139                 return 0;
140          *
141          */
142         switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
143         case SCSI_PROT_READ_STRIP:
144         case SCSI_PROT_WRITE_INSERT:
145                 if (ql2xenablehba_err_chk >= 1)
146                         return 1;
147                 break;
148         case SCSI_PROT_READ_PASS:
149         case SCSI_PROT_WRITE_PASS:
150                 if (ql2xenablehba_err_chk >= 2)
151                         return 1;
152                 break;
153         case SCSI_PROT_READ_INSERT:
154         case SCSI_PROT_WRITE_STRIP:
155                 return 1;
156         }
157         return 0;
158 }
159
160 static inline int
161 qla2x00_reset_active(scsi_qla_host_t *vha)
162 {
163         scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
164
165         /* Test appropriate base-vha and vha flags. */
166         return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
167             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
168             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
169             test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
170             test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
171 }
172
173 static inline int
174 qla2x00_chip_is_down(scsi_qla_host_t *vha)
175 {
176         return (qla2x00_reset_active(vha) || !vha->hw->flags.fw_started);
177 }
178
179 static void qla2xxx_init_sp(srb_t *sp, scsi_qla_host_t *vha,
180                             struct qla_qpair *qpair, fc_port_t *fcport)
181 {
182         memset(sp, 0, sizeof(*sp));
183         sp->fcport = fcport;
184         sp->iocbs = 1;
185         sp->vha = vha;
186         sp->qpair = qpair;
187         sp->cmd_type = TYPE_SRB;
188         INIT_LIST_HEAD(&sp->elem);
189 }
190
191 static inline srb_t *
192 qla2xxx_get_qpair_sp(scsi_qla_host_t *vha, struct qla_qpair *qpair,
193     fc_port_t *fcport, gfp_t flag)
194 {
195         srb_t *sp = NULL;
196         uint8_t bail;
197
198         QLA_QPAIR_MARK_BUSY(qpair, bail);
199         if (unlikely(bail))
200                 return NULL;
201
202         sp = mempool_alloc(qpair->srb_mempool, flag);
203         if (sp)
204                 qla2xxx_init_sp(sp, vha, qpair, fcport);
205         else
206                 QLA_QPAIR_MARK_NOT_BUSY(qpair);
207         return sp;
208 }
209
210 void qla2xxx_rel_done_warning(srb_t *sp, int res);
211 void qla2xxx_rel_free_warning(srb_t *sp);
212
213 static inline void
214 qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
215 {
216         sp->qpair = NULL;
217         sp->done = qla2xxx_rel_done_warning;
218         sp->free = qla2xxx_rel_free_warning;
219         mempool_free(sp, qpair->srb_mempool);
220         QLA_QPAIR_MARK_NOT_BUSY(qpair);
221 }
222
223 static inline srb_t *
224 qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
225 {
226         srb_t *sp = NULL;
227         uint8_t bail;
228         struct qla_qpair *qpair;
229
230         QLA_VHA_MARK_BUSY(vha, bail);
231         if (unlikely(bail))
232                 return NULL;
233
234         qpair = vha->hw->base_qpair;
235         sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, flag);
236         if (!sp)
237                 goto done;
238
239         sp->vha = vha;
240 done:
241         if (!sp)
242                 QLA_VHA_MARK_NOT_BUSY(vha);
243         return sp;
244 }
245
246 static inline void
247 qla2x00_rel_sp(srb_t *sp)
248 {
249         QLA_VHA_MARK_NOT_BUSY(sp->vha);
250         qla2xxx_rel_qpair_sp(sp->qpair, sp);
251 }
252
253 static inline int
254 qla2x00_gid_list_size(struct qla_hw_data *ha)
255 {
256         if (IS_QLAFX00(ha))
257                 return sizeof(uint32_t) * 32;
258         else
259                 return sizeof(struct gid_list_info) * ha->max_fibre_devices;
260 }
261
262 static inline void
263 qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
264 {
265         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
266             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
267                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
268                 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
269                 complete(&ha->mbx_intr_comp);
270         }
271 }
272
273 static inline void
274 qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
275 {
276         if (retry_delay)
277                 fcport->retry_delay_timestamp = jiffies +
278                     (retry_delay * HZ / 10);
279 }
280
281 static inline bool
282 qla_is_exch_offld_enabled(struct scsi_qla_host *vha)
283 {
284         if (qla_ini_mode_enabled(vha) &&
285             (vha->ql2xiniexchg > FW_DEF_EXCHANGES_CNT))
286                 return true;
287         else if (qla_tgt_mode_enabled(vha) &&
288             (vha->ql2xexchoffld > FW_DEF_EXCHANGES_CNT))
289                 return true;
290         else if (qla_dual_mode_enabled(vha) &&
291             ((vha->ql2xiniexchg + vha->ql2xexchoffld) > FW_DEF_EXCHANGES_CNT))
292                 return true;
293         else
294                 return false;
295 }
296
297 static inline void
298 qla_cpu_update(struct qla_qpair *qpair, uint16_t cpuid)
299 {
300         qpair->cpuid = cpuid;
301
302         if (!list_empty(&qpair->hints_list)) {
303                 struct qla_qpair_hint *h;
304
305                 list_for_each_entry(h, &qpair->hints_list, hint_elem)
306                         h->cpuid = qpair->cpuid;
307         }
308 }
309
310 static inline struct qla_qpair_hint *
311 qla_qpair_to_hint(struct qla_tgt *tgt, struct qla_qpair *qpair)
312 {
313         struct qla_qpair_hint *h;
314         u16 i;
315
316         for (i = 0; i < tgt->ha->max_qpairs + 1; i++) {
317                 h = &tgt->qphints[i];
318                 if (h->qpair == qpair)
319                         return h;
320         }
321
322         return NULL;
323 }
324
325 static inline void
326 qla_83xx_start_iocbs(struct qla_qpair *qpair)
327 {
328         struct req_que *req = qpair->req;
329
330         req->ring_index++;
331         if (req->ring_index == req->length) {
332                 req->ring_index = 0;
333                 req->ring_ptr = req->ring;
334         } else
335                 req->ring_ptr++;
336
337         WRT_REG_DWORD(req->req_q_in, req->ring_index);
338 }
339
340 static inline int
341 qla2xxx_get_fc4_priority(struct scsi_qla_host *vha)
342 {
343         uint32_t data;
344
345         data =
346             ((uint8_t *)vha->hw->nvram)[NVRAM_DUAL_FCP_NVME_FLAG_OFFSET];
347
348
349         return ((data >> 6) & BIT_0);
350 }