summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/qlogic/qed/qed_chain.c
blob: b83d17b14e858c81019fcf70a58838c1463177b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
/* Copyright (c) 2020 Marvell International Ltd. */

#include <linux/dma-mapping.h>
#include <linux/qed/qed_chain.h>
#include <linux/vmalloc.h>

#include "qed_dev_api.h"

static void qed_chain_init(struct qed_chain *chain,
			   const struct qed_chain_init_params *params,
			   u32 page_cnt)
{
	memset(chain, 0, sizeof(*chain));

	chain->elem_size = params->elem_size;
	chain->intended_use = params->intended_use;
	chain->mode = params->mode;
	chain->cnt_type = params->cnt_type;

	chain->elem_per_page = ELEMS_PER_PAGE(params->elem_size,
					      params->page_size);
	chain->usable_per_page = USABLE_ELEMS_PER_PAGE(params->elem_size,
						       params->page_size,
						       params->mode);
	chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(params->elem_size,
						       params->mode);

	chain->elem_per_page_mask = chain->elem_per_page - 1;
	chain->next_page_mask = chain->usable_per_page &
				chain->elem_per_page_mask;

	chain->page_size = params->page_size;
	chain->page_cnt = page_cnt;
	chain->capacity = chain->usable_per_page * page_cnt;
	chain->size = chain->elem_per_page * page_cnt;

	if (params->ext_pbl_virt) {
		chain->pbl_sp.table_virt = params->ext_pbl_virt;
		chain->pbl_sp.table_phys = params->ext_pbl_phys;

		chain->b_external_pbl = true;
	}
}

static void qed_chain_init_next_ptr_elem(const struct qed_chain *chain,
					 void *virt_curr, void *virt_next,
					 dma_addr_t phys_next)
{
	struct qed_chain_next *next;
	u32 size;

	size = chain->elem_size * chain->usable_per_page;
	next = virt_curr + size;

	DMA_REGPAIR_LE(next->next_phys, phys_next);
	next->next_virt = virt_next;
}

static void qed_chain_init_mem(struct qed_chain *chain, void *virt_addr,
			       dma_addr_t phys_addr)
{
	chain->p_virt_addr = virt_addr;
	chain->p_phys_addr = phys_addr;
}

static void qed_chain_free_next_ptr(struct qed_dev *cdev,
				    struct qed_chain *chain)
{
	struct device *dev = &cdev->pdev->dev;
	struct qed_chain_next *next;
	dma_addr_t phys, phys_next;
	void *virt, *virt_next;
	u32 size, i;

	size = chain->elem_size * chain->usable_per_page;
	virt = chain->p_virt_addr;
	phys = chain->p_phys_addr;

	for (i = 0; i < chain->page_cnt; i++) {
		if (!virt)
			break;

		next = virt + size;
		virt_next = next->next_virt;
		phys_next = HILO_DMA_REGPAIR(next->next_phys);

		dma_free_coherent(dev, chain->page_size, virt, phys);

		virt = virt_next;
		phys = phys_next;
	}
}

static void qed_chain_free_single(struct qed_dev *cdev,
				  struct qed_chain *chain)
{
	if (!chain->p_virt_addr)
		return;

	dma_free_coherent(&cdev->pdev->dev, chain->page_size,
			  chain->p_virt_addr, chain->p_phys_addr);
}

static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *chain)
{
	struct device *dev = &cdev->pdev->dev;
	struct addr_tbl_entry *entry;
	u32 i;

	if (!chain->pbl.pp_addr_tbl)
		return;

	for (i = 0; i < chain->page_cnt; i++) {
		entry = chain->pbl.pp_addr_tbl + i;
		if (!entry->virt_addr)
			break;

		dma_free_coherent(dev, chain->page_size, entry->virt_addr,
				  entry->dma_map);
	}

	if (!chain->b_external_pbl)
		dma_free_coherent(dev, chain->pbl_sp.table_size,
				  chain->pbl_sp.table_virt,
				  chain->pbl_sp.table_phys);

	vfree(chain->pbl.pp_addr_tbl);
	chain->pbl.pp_addr_tbl = NULL;
}

/**
 * qed_chain_free() - Free chain DMA memory.
 *
 * @cdev: Main device structure.
 * @chain: Chain to free.
 */
void qed_chain_free(struct qed_dev *cdev, struct qed_chain *chain)
{
	switch (chain->mode) {
	case QED_CHAIN_MODE_NEXT_PTR:
		qed_chain_free_next_ptr(cdev, chain);
		break;
	case QED_CHAIN_MODE_SINGLE:
		qed_chain_free_single(cdev, chain);
		break;
	case QED_CHAIN_MODE_PBL:
		qed_chain_free_pbl(cdev, chain);
		break;
	default:
		return;
	}

	qed_chain_init_mem(chain, NULL, 0);
}

static int
qed_chain_alloc_sanity_check(struct qed_dev *cdev,
			     const struct qed_chain_init_params *params,
			     u32 page_cnt)
{
	u64 chain_size;

	chain_size = ELEMS_PER_PAGE(params->elem_size, params->page_size);
	chain_size *= page_cnt;

	if (!chain_size)
		return -EINVAL;

	/* The actual chain size can be larger than the maximal possible value
	 * after rounding up the requested elements number to pages, and after
	 * taking into account the unusuable elements (next-ptr elements).
	 * The size of a "u16" chain can be (U16_MAX + 1) since the chain
	 * size/capacity fields are of u32 type.
	 */
	switch (params->cnt_type) {
	case QED_CHAIN_CNT_TYPE_U16:
		if (chain_size > U16_MAX + 1)
			break;

		return 0;
	case QED_CHAIN_CNT_TYPE_U32:
		if (chain_size > U32_MAX)
			break;

		return 0;
	default:
		return -EINVAL;
	}

	DP_NOTICE(cdev,
		  "The actual chain size (0x%llx) is larger than the maximal possible value\n",
		  chain_size);

	return -EINVAL;
}

static int qed_chain_alloc_next_ptr(struct qed_dev *cdev,
				    struct qed_chain *chain)
{
	struct device *dev = &cdev->pdev->dev;
	void *virt, *virt_prev = NULL;
	dma_addr_t phys;
	u32 i;

	for (i = 0; i < chain->page_cnt; i++) {
		virt = dma_alloc_coherent(dev, chain->page_size, &phys,
					  GFP_KERNEL);
		if (!virt)
			return -ENOMEM;

		if (i == 0) {
			qed_chain_init_mem(chain, virt, phys);
			qed_chain_reset(chain);
		} else {
			qed_chain_init_next_ptr_elem(chain, virt_prev, virt,
						     phys);
		}

		virt_prev = virt;
	}

	/* Last page's next element should point to the beginning of the
	 * chain.
	 */
	qed_chain_init_next_ptr_elem(chain, virt_prev, chain->p_virt_addr,
				     chain->p_phys_addr);

	return 0;
}

static int qed_chain_alloc_single(struct qed_dev *cdev,
				  struct qed_chain *chain)
{
	dma_addr_t phys;
	void *virt;

	virt = dma_alloc_coherent(&cdev->pdev->dev, chain->page_size,
				  &phys, GFP_KERNEL);
	if (!virt)
		return -ENOMEM;

	qed_chain_init_mem(chain, virt, phys);
	qed_chain_reset(chain);

	return 0;
}

static int qed_chain_alloc_pbl(struct qed_dev *cdev, struct qed_chain *chain)
{
	struct device *dev = &cdev->pdev->dev;
	struct addr_tbl_entry *addr_tbl;
	dma_addr_t phys, pbl_phys;
	__le64 *pbl_virt;
	u32 page_cnt, i;
	size_t size;
	void *virt;

	page_cnt = chain->page_cnt;

	size = array_size(page_cnt, sizeof(*addr_tbl));
	if (unlikely(size == SIZE_MAX))
		return -EOVERFLOW;

	addr_tbl = vzalloc(size);
	if (!addr_tbl)
		return -ENOMEM;

	chain->pbl.pp_addr_tbl = addr_tbl;

	if (chain->b_external_pbl) {
		pbl_virt = chain->pbl_sp.table_virt;
		goto alloc_pages;
	}

	size = array_size(page_cnt, sizeof(*pbl_virt));
	if (unlikely(size == SIZE_MAX))
		return -EOVERFLOW;

	pbl_virt = dma_alloc_coherent(dev, size, &pbl_phys, GFP_KERNEL);
	if (!pbl_virt)
		return -ENOMEM;

	chain->pbl_sp.table_virt = pbl_virt;
	chain->pbl_sp.table_phys = pbl_phys;
	chain->pbl_sp.table_size = size;

alloc_pages:
	for (i = 0; i < page_cnt; i++) {
		virt = dma_alloc_coherent(dev, chain->page_size, &phys,
					  GFP_KERNEL);
		if (!virt)
			return -ENOMEM;

		if (i == 0) {
			qed_chain_init_mem(chain, virt, phys);
			qed_chain_reset(chain);
		}

		/* Fill the PBL table with the physical address of the page */
		pbl_virt[i] = cpu_to_le64(phys);

		/* Keep the virtual address of the page */
		addr_tbl[i].virt_addr = virt;
		addr_tbl[i].dma_map = phys;
	}

	return 0;
}

/**
 * qed_chain_alloc() - Allocate and initialize a chain.
 *
 * @cdev: Main device structure.
 * @chain: Chain to be processed.
 * @params: Chain initialization parameters.
 *
 * Return: 0 on success, negative errno otherwise.
 */
int qed_chain_alloc(struct qed_dev *cdev, struct qed_chain *chain,
		    struct qed_chain_init_params *params)
{
	u32 page_cnt;
	int rc;

	if (!params->page_size)
		params->page_size = QED_CHAIN_PAGE_SIZE;

	if (params->mode == QED_CHAIN_MODE_SINGLE)
		page_cnt = 1;
	else
		page_cnt = QED_CHAIN_PAGE_CNT(params->num_elems,
					      params->elem_size,
					      params->page_size,
					      params->mode);

	rc = qed_chain_alloc_sanity_check(cdev, params, page_cnt);
	if (rc) {
		DP_NOTICE(cdev,
			  "Cannot allocate a chain with the given arguments:\n");
		DP_NOTICE(cdev,
			  "[use_mode %d, mode %d, cnt_type %d, num_elems %d, elem_size %zu, page_size %u]\n",
			  params->intended_use, params->mode, params->cnt_type,
			  params->num_elems, params->elem_size,
			  params->page_size);
		return rc;
	}

	qed_chain_init(chain, params, page_cnt);

	switch (params->mode) {
	case QED_CHAIN_MODE_NEXT_PTR:
		rc = qed_chain_alloc_next_ptr(cdev, chain);
		break;
	case QED_CHAIN_MODE_SINGLE:
		rc = qed_chain_alloc_single(cdev, chain);
		break;
	case QED_CHAIN_MODE_PBL:
		rc = qed_chain_alloc_pbl(cdev, chain);
		break;
	default:
		return -EINVAL;
	}

	if (!rc)
		return 0;

	qed_chain_free(cdev, chain);

	return rc;
}