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 int
109 qla2x00_hba_err_chk_enabled(srb_t *sp)
110 {
111 	/*
112 	 * Uncomment when corresponding SCSI changes are done.
113 	 *
114 	if (!sp->cmd->prot_chk)
115 		return 0;
116 	 *
117 	 */
118 	switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
119 	case SCSI_PROT_READ_STRIP:
120 	case SCSI_PROT_WRITE_INSERT:
121 		if (ql2xenablehba_err_chk >= 1)
122 			return 1;
123 		break;
124 	case SCSI_PROT_READ_PASS:
125 	case SCSI_PROT_WRITE_PASS:
126 		if (ql2xenablehba_err_chk >= 2)
127 			return 1;
128 		break;
129 	case SCSI_PROT_READ_INSERT:
130 	case SCSI_PROT_WRITE_STRIP:
131 		return 1;
132 	}
133 	return 0;
134 }
135 
136 static inline int
137 qla2x00_reset_active(scsi_qla_host_t *vha)
138 {
139 	scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
140 
141 	/* Test appropriate base-vha and vha flags. */
142 	return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
143 	    test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
144 	    test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
145 	    test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
146 	    test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
147 }
148 
149 static inline int
150 qla2x00_chip_is_down(scsi_qla_host_t *vha)
151 {
152 	return (qla2x00_reset_active(vha) || !vha->hw->flags.fw_started);
153 }
154 
155 static inline srb_t *
156 qla2xxx_get_qpair_sp(scsi_qla_host_t *vha, struct qla_qpair *qpair,
157     fc_port_t *fcport, gfp_t flag)
158 {
159 	srb_t *sp = NULL;
160 	uint8_t bail;
161 
162 	QLA_QPAIR_MARK_BUSY(qpair, bail);
163 	if (unlikely(bail))
164 		return NULL;
165 
166 	sp = mempool_alloc(qpair->srb_mempool, flag);
167 	if (!sp)
168 		goto done;
169 
170 	memset(sp, 0, sizeof(*sp));
171 	sp->fcport = fcport;
172 	sp->iocbs = 1;
173 	sp->vha = vha;
174 	sp->qpair = qpair;
175 	sp->cmd_type = TYPE_SRB;
176 	INIT_LIST_HEAD(&sp->elem);
177 
178 done:
179 	if (!sp)
180 		QLA_QPAIR_MARK_NOT_BUSY(qpair);
181 	return sp;
182 }
183 
184 static inline void
185 qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
186 {
187 	sp->qpair = NULL;
188 	mempool_free(sp, qpair->srb_mempool);
189 	QLA_QPAIR_MARK_NOT_BUSY(qpair);
190 }
191 
192 static inline srb_t *
193 qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
194 {
195 	srb_t *sp = NULL;
196 	uint8_t bail;
197 	struct qla_qpair *qpair;
198 
199 	QLA_VHA_MARK_BUSY(vha, bail);
200 	if (unlikely(bail))
201 		return NULL;
202 
203 	qpair = vha->hw->base_qpair;
204 	sp = qla2xxx_get_qpair_sp(vha, qpair, fcport, flag);
205 	if (!sp)
206 		goto done;
207 
208 	sp->vha = vha;
209 done:
210 	if (!sp)
211 		QLA_VHA_MARK_NOT_BUSY(vha);
212 	return sp;
213 }
214 
215 static inline void
216 qla2x00_rel_sp(srb_t *sp)
217 {
218 	QLA_VHA_MARK_NOT_BUSY(sp->vha);
219 	qla2xxx_rel_qpair_sp(sp->qpair, sp);
220 }
221 
222 static inline int
223 qla2x00_gid_list_size(struct qla_hw_data *ha)
224 {
225 	if (IS_QLAFX00(ha))
226 		return sizeof(uint32_t) * 32;
227 	else
228 		return sizeof(struct gid_list_info) * ha->max_fibre_devices;
229 }
230 
231 static inline void
232 qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
233 {
234 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
235 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
236 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
237 		clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
238 		complete(&ha->mbx_intr_comp);
239 	}
240 }
241 
242 static inline void
243 qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
244 {
245 	if (retry_delay)
246 		fcport->retry_delay_timestamp = jiffies +
247 		    (retry_delay * HZ / 10);
248 }
249 
250 static inline bool
251 qla_is_exch_offld_enabled(struct scsi_qla_host *vha)
252 {
253 	if (qla_ini_mode_enabled(vha) &&
254 	    (vha->ql2xiniexchg > FW_DEF_EXCHANGES_CNT))
255 		return true;
256 	else if (qla_tgt_mode_enabled(vha) &&
257 	    (vha->ql2xexchoffld > FW_DEF_EXCHANGES_CNT))
258 		return true;
259 	else if (qla_dual_mode_enabled(vha) &&
260 	    ((vha->ql2xiniexchg + vha->ql2xexchoffld) > FW_DEF_EXCHANGES_CNT))
261 		return true;
262 	else
263 		return false;
264 }
265 
266 static inline void
267 qla_cpu_update(struct qla_qpair *qpair, uint16_t cpuid)
268 {
269 	qpair->cpuid = cpuid;
270 
271 	if (!list_empty(&qpair->hints_list)) {
272 		struct qla_qpair_hint *h;
273 
274 		list_for_each_entry(h, &qpair->hints_list, hint_elem)
275 			h->cpuid = qpair->cpuid;
276 	}
277 }
278 
279 static inline struct qla_qpair_hint *
280 qla_qpair_to_hint(struct qla_tgt *tgt, struct qla_qpair *qpair)
281 {
282 	struct qla_qpair_hint *h;
283 	u16 i;
284 
285 	for (i = 0; i < tgt->ha->max_qpairs + 1; i++) {
286 		h = &tgt->qphints[i];
287 		if (h->qpair == qpair)
288 			return h;
289 	}
290 
291 	return NULL;
292 }
293 
294 static inline void
295 qla_83xx_start_iocbs(struct qla_qpair *qpair)
296 {
297 	struct req_que *req = qpair->req;
298 
299 	req->ring_index++;
300 	if (req->ring_index == req->length) {
301 		req->ring_index = 0;
302 		req->ring_ptr = req->ring;
303 	} else
304 		req->ring_ptr++;
305 
306 	WRT_REG_DWORD(req->req_q_in, req->ring_index);
307 }
308