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
qla24xx_calc_iocbs(scsi_qla_host_t * vha,uint16_t dsds)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
qla2x00_debounce_register(volatile uint16_t __iomem * addr)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
qla2x00_poll(struct rsp_que * rsp)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 *
host_to_fcp_swap(uint8_t * fcp,uint32_t bsize)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
host_to_adap(uint8_t * src,uint8_t * dst,uint32_t bsize)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
qla2x00_clean_dsd_pool(struct qla_hw_data * ha,struct crc_context * ctx)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
qla2x00_set_fcport_disc_state(fc_port_t * fcport,int state)109 qla2x00_set_fcport_disc_state(fc_port_t *fcport, int state)
110 {
111 int old_val;
112 uint8_t shiftbits, mask;
113 uint8_t port_dstate_str_sz;
114
115 /* This will have to change when the max no. of states > 16 */
116 shiftbits = 4;
117 mask = (1 << shiftbits) - 1;
118
119 port_dstate_str_sz = sizeof(port_dstate_str) / sizeof(char *);
120 fcport->disc_state = state;
121 while (1) {
122 old_val = atomic_read(&fcport->shadow_disc_state);
123 if (old_val == atomic_cmpxchg(&fcport->shadow_disc_state,
124 old_val, (old_val << shiftbits) | state)) {
125 ql_dbg(ql_dbg_disc, fcport->vha, 0x2134,
126 "FCPort %8phC disc_state transition: %s to %s - portid=%06x.\n",
127 fcport->port_name, (old_val & mask) < port_dstate_str_sz ?
128 port_dstate_str[old_val & mask] : "Unknown",
129 port_dstate_str[state], fcport->d_id.b24);
130 return;
131 }
132 }
133 }
134
135 static inline int
qla2x00_hba_err_chk_enabled(srb_t * sp)136 qla2x00_hba_err_chk_enabled(srb_t *sp)
137 {
138 /*
139 * Uncomment when corresponding SCSI changes are done.
140 *
141 if (!sp->cmd->prot_chk)
142 return 0;
143 *
144 */
145 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
146 case SCSI_PROT_READ_STRIP:
147 case SCSI_PROT_WRITE_INSERT:
148 if (ql2xenablehba_err_chk >= 1)
149 return 1;
150 break;
151 case SCSI_PROT_READ_PASS:
152 case SCSI_PROT_WRITE_PASS:
153 if (ql2xenablehba_err_chk >= 2)
154 return 1;
155 break;
156 case SCSI_PROT_READ_INSERT:
157 case SCSI_PROT_WRITE_STRIP:
158 return 1;
159 }
160 return 0;
161 }
162
163 static inline int
qla2x00_reset_active(scsi_qla_host_t * vha)164 qla2x00_reset_active(scsi_qla_host_t *vha)
165 {
166 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
167
168 /* Test appropriate base-vha and vha flags. */
169 return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
170 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
171 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
172 test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
173 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
174 }
175
176 static inline int
qla2x00_chip_is_down(scsi_qla_host_t * vha)177 qla2x00_chip_is_down(scsi_qla_host_t *vha)
178 {
179 return (qla2x00_reset_active(vha) || !vha->hw->flags.fw_started);
180 }
181
qla2xxx_init_sp(srb_t * sp,scsi_qla_host_t * vha,struct qla_qpair * qpair,fc_port_t * fcport)182 static void qla2xxx_init_sp(srb_t *sp, scsi_qla_host_t *vha,
183 struct qla_qpair *qpair, fc_port_t *fcport)
184 {
185 memset(sp, 0, sizeof(*sp));
186 sp->fcport = fcport;
187 sp->iocbs = 1;
188 sp->vha = vha;
189 sp->qpair = qpair;
190 sp->cmd_type = TYPE_SRB;
191 INIT_LIST_HEAD(&sp->elem);
192 }
193
194 static inline srb_t *
qla2xxx_get_qpair_sp(scsi_qla_host_t * vha,struct qla_qpair * qpair,fc_port_t * fcport,gfp_t flag)195 qla2xxx_get_qpair_sp(scsi_qla_host_t *vha, struct qla_qpair *qpair,
196 fc_port_t *fcport, gfp_t flag)
197 {
198 srb_t *sp = NULL;
199 uint8_t bail;
200
201 QLA_QPAIR_MARK_BUSY(qpair, bail);
202 if (unlikely(bail))
203 return NULL;
204
205 sp = mempool_alloc(qpair->srb_mempool, flag);
206 if (sp)
207 qla2xxx_init_sp(sp, vha, qpair, fcport);
208 else
209 QLA_QPAIR_MARK_NOT_BUSY(qpair);
210 return sp;
211 }
212
213 void qla2xxx_rel_done_warning(srb_t *sp, int res);
214 void qla2xxx_rel_free_warning(srb_t *sp);
215
216 static inline void
qla2xxx_rel_qpair_sp(struct qla_qpair * qpair,srb_t * sp)217 qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
218 {
219 sp->qpair = NULL;
220 sp->done = qla2xxx_rel_done_warning;
221 sp->free = qla2xxx_rel_free_warning;
222 mempool_free(sp, qpair->srb_mempool);
223 QLA_QPAIR_MARK_NOT_BUSY(qpair);
224 }
225
226 static inline srb_t *
qla2x00_get_sp(scsi_qla_host_t * vha,fc_port_t * fcport,gfp_t flag)227 qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
228 {
229 srb_t *sp = NULL;
230 uint8_t bail;
231 struct qla_qpair *qpair;
232
233 QLA_VHA_MARK_BUSY(vha, bail);
234 if (unlikely(bail))
235 return NULL;
236
237 qpair = vha->hw->base_qpair;
238 sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, flag);
239 if (!sp)
240 goto done;
241
242 sp->vha = vha;
243 done:
244 if (!sp)
245 QLA_VHA_MARK_NOT_BUSY(vha);
246 return sp;
247 }
248
249 static inline void
qla2x00_rel_sp(srb_t * sp)250 qla2x00_rel_sp(srb_t *sp)
251 {
252 QLA_VHA_MARK_NOT_BUSY(sp->vha);
253 qla2xxx_rel_qpair_sp(sp->qpair, sp);
254 }
255
256 static inline int
qla2x00_gid_list_size(struct qla_hw_data * ha)257 qla2x00_gid_list_size(struct qla_hw_data *ha)
258 {
259 if (IS_QLAFX00(ha))
260 return sizeof(uint32_t) * 32;
261 else
262 return sizeof(struct gid_list_info) * ha->max_fibre_devices;
263 }
264
265 static inline void
qla2x00_handle_mbx_completion(struct qla_hw_data * ha,int status)266 qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
267 {
268 if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
269 (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
270 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
271 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
272 complete(&ha->mbx_intr_comp);
273 }
274 }
275
276 static inline void
qla2x00_set_retry_delay_timestamp(fc_port_t * fcport,uint16_t retry_delay)277 qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
278 {
279 if (retry_delay)
280 fcport->retry_delay_timestamp = jiffies +
281 (retry_delay * HZ / 10);
282 }
283
284 static inline bool
qla_is_exch_offld_enabled(struct scsi_qla_host * vha)285 qla_is_exch_offld_enabled(struct scsi_qla_host *vha)
286 {
287 if (qla_ini_mode_enabled(vha) &&
288 (vha->ql2xiniexchg > FW_DEF_EXCHANGES_CNT))
289 return true;
290 else if (qla_tgt_mode_enabled(vha) &&
291 (vha->ql2xexchoffld > FW_DEF_EXCHANGES_CNT))
292 return true;
293 else if (qla_dual_mode_enabled(vha) &&
294 ((vha->ql2xiniexchg + vha->ql2xexchoffld) > FW_DEF_EXCHANGES_CNT))
295 return true;
296 else
297 return false;
298 }
299
300 static inline void
qla_cpu_update(struct qla_qpair * qpair,uint16_t cpuid)301 qla_cpu_update(struct qla_qpair *qpair, uint16_t cpuid)
302 {
303 qpair->cpuid = cpuid;
304
305 if (!list_empty(&qpair->hints_list)) {
306 struct qla_qpair_hint *h;
307
308 list_for_each_entry(h, &qpair->hints_list, hint_elem)
309 h->cpuid = qpair->cpuid;
310 }
311 }
312
313 static inline struct qla_qpair_hint *
qla_qpair_to_hint(struct qla_tgt * tgt,struct qla_qpair * qpair)314 qla_qpair_to_hint(struct qla_tgt *tgt, struct qla_qpair *qpair)
315 {
316 struct qla_qpair_hint *h;
317 u16 i;
318
319 for (i = 0; i < tgt->ha->max_qpairs + 1; i++) {
320 h = &tgt->qphints[i];
321 if (h->qpair == qpair)
322 return h;
323 }
324
325 return NULL;
326 }
327
328 static inline void
qla_83xx_start_iocbs(struct qla_qpair * qpair)329 qla_83xx_start_iocbs(struct qla_qpair *qpair)
330 {
331 struct req_que *req = qpair->req;
332
333 req->ring_index++;
334 if (req->ring_index == req->length) {
335 req->ring_index = 0;
336 req->ring_ptr = req->ring;
337 } else
338 req->ring_ptr++;
339
340 WRT_REG_DWORD(req->req_q_in, req->ring_index);
341 }
342
343 static inline int
qla2xxx_get_fc4_priority(struct scsi_qla_host * vha)344 qla2xxx_get_fc4_priority(struct scsi_qla_host *vha)
345 {
346 uint32_t data;
347
348 data =
349 ((uint8_t *)vha->hw->nvram)[NVRAM_DUAL_FCP_NVME_FLAG_OFFSET];
350
351
352 return ((data >> 6) & BIT_0);
353 }
354