Blame view

kernel/linux-rt-4.4.41/include/linux/qed/qed_chain.h 13.5 KB
5113f6f70   김현기   kernel add
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  /* QLogic qed NIC Driver
   * Copyright (c) 2015 QLogic Corporation
   *
   * This software is available under the terms of the GNU General Public License
   * (GPL) Version 2, available from the file COPYING in the main directory of
   * this source tree.
   */
  
  #ifndef _QED_CHAIN_H
  #define _QED_CHAIN_H
  
  #include <linux/types.h>
  #include <asm/byteorder.h>
  #include <linux/kernel.h>
  #include <linux/list.h>
  #include <linux/slab.h>
  #include <linux/qed/common_hsi.h>
  
  /* dma_addr_t manip */
  #define DMA_LO_LE(x)            cpu_to_le32(lower_32_bits(x))
  #define DMA_HI_LE(x)            cpu_to_le32(upper_32_bits(x))
  
  #define HILO_GEN(hi, lo, type)  ((((type)(hi)) << 32) + (lo))
  #define HILO_DMA(hi, lo)        HILO_GEN(hi, lo, dma_addr_t)
  #define HILO_64(hi, lo) HILO_GEN((le32_to_cpu(hi)), (le32_to_cpu(lo)), u64)
  #define HILO_DMA_REGPAIR(regpair)       (HILO_DMA(regpair.hi, regpair.lo))
  #define HILO_64_REGPAIR(regpair)        (HILO_64(regpair.hi, regpair.lo))
  
  enum qed_chain_mode {
  	/* Each Page contains a next pointer at its end */
  	QED_CHAIN_MODE_NEXT_PTR,
  
  	/* Chain is a single page (next ptr) is unrequired */
  	QED_CHAIN_MODE_SINGLE,
  
  	/* Page pointers are located in a side list */
  	QED_CHAIN_MODE_PBL,
  };
  
  enum qed_chain_use_mode {
  	QED_CHAIN_USE_TO_PRODUCE,		/* Chain starts empty */
  	QED_CHAIN_USE_TO_CONSUME,		/* Chain starts full */
  	QED_CHAIN_USE_TO_CONSUME_PRODUCE,	/* Chain starts empty */
  };
  
  struct qed_chain_next {
  	struct regpair	next_phys;
  	void		*next_virt;
  };
  
  struct qed_chain_pbl {
  	dma_addr_t	p_phys_table;
  	void		*p_virt_table;
  	u16		prod_page_idx;
  	u16		cons_page_idx;
  };
  
  struct qed_chain {
  	void			*p_virt_addr;
  	dma_addr_t		p_phys_addr;
  	void			*p_prod_elem;
  	void			*p_cons_elem;
  	u16			page_cnt;
  	enum qed_chain_mode	mode;
  	enum qed_chain_use_mode intended_use; /* used to produce/consume */
  	u16			capacity; /*< number of _usable_ elements */
  	u16			size; /* number of elements */
  	u16			prod_idx;
  	u16			cons_idx;
  	u16			elem_per_page;
  	u16			elem_per_page_mask;
  	u16			elem_unusable;
  	u16			usable_per_page;
  	u16			elem_size;
  	u16			next_page_mask;
  	struct qed_chain_pbl	pbl;
  };
  
  #define QED_CHAIN_PBL_ENTRY_SIZE        (8)
  #define QED_CHAIN_PAGE_SIZE             (0x1000)
  #define ELEMS_PER_PAGE(elem_size)       (QED_CHAIN_PAGE_SIZE / (elem_size))
  
  #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)     \
  	((mode == QED_CHAIN_MODE_NEXT_PTR) ?	     \
  	 (1 + ((sizeof(struct qed_chain_next) - 1) / \
  	       (elem_size))) : 0)
  
  #define USABLE_ELEMS_PER_PAGE(elem_size, mode) \
  	((u32)(ELEMS_PER_PAGE(elem_size) -     \
  	       UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
  
  #define QED_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode) \
  	DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
  
  /* Accessors */
  static inline u16 qed_chain_get_prod_idx(struct qed_chain *p_chain)
  {
  	return p_chain->prod_idx;
  }
  
  static inline u16 qed_chain_get_cons_idx(struct qed_chain *p_chain)
  {
  	return p_chain->cons_idx;
  }
  
  static inline u16 qed_chain_get_elem_left(struct qed_chain *p_chain)
  {
  	u16 used;
  
  	/* we don't need to trancate upon assignmet, as we assign u32->u16 */
  	used = ((u32)0x10000u + (u32)(p_chain->prod_idx)) -
  		(u32)p_chain->cons_idx;
  	if (p_chain->mode == QED_CHAIN_MODE_NEXT_PTR)
  		used -= p_chain->prod_idx / p_chain->elem_per_page -
  			p_chain->cons_idx / p_chain->elem_per_page;
  
  	return p_chain->capacity - used;
  }
  
  static inline u8 qed_chain_is_full(struct qed_chain *p_chain)
  {
  	return qed_chain_get_elem_left(p_chain) == p_chain->capacity;
  }
  
  static inline u8 qed_chain_is_empty(struct qed_chain *p_chain)
  {
  	return qed_chain_get_elem_left(p_chain) == 0;
  }
  
  static inline u16 qed_chain_get_elem_per_page(
  	struct qed_chain *p_chain)
  {
  	return p_chain->elem_per_page;
  }
  
  static inline u16 qed_chain_get_usable_per_page(
  	struct qed_chain *p_chain)
  {
  	return p_chain->usable_per_page;
  }
  
  static inline u16 qed_chain_get_unusable_per_page(
  	struct qed_chain *p_chain)
  {
  	return p_chain->elem_unusable;
  }
  
  static inline u16 qed_chain_get_size(struct qed_chain *p_chain)
  {
  	return p_chain->size;
  }
  
  static inline dma_addr_t
  qed_chain_get_pbl_phys(struct qed_chain *p_chain)
  {
  	return p_chain->pbl.p_phys_table;
  }
  
  /**
   * @brief qed_chain_advance_page -
   *
   * Advance the next element accros pages for a linked chain
   *
   * @param p_chain
   * @param p_next_elem
   * @param idx_to_inc
   * @param page_to_inc
   */
  static inline void
  qed_chain_advance_page(struct qed_chain *p_chain,
  		       void **p_next_elem,
  		       u16 *idx_to_inc,
  		       u16 *page_to_inc)
  
  {
  	switch (p_chain->mode) {
  	case QED_CHAIN_MODE_NEXT_PTR:
  	{
  		struct qed_chain_next *p_next = *p_next_elem;
  		*p_next_elem = p_next->next_virt;
  		*idx_to_inc += p_chain->elem_unusable;
  		break;
  	}
  	case QED_CHAIN_MODE_SINGLE:
  		*p_next_elem = p_chain->p_virt_addr;
  		break;
  
  	case QED_CHAIN_MODE_PBL:
  		/* It is assumed pages are sequential, next element needs
  		 * to change only when passing going back to first from last.
  		 */
  		if (++(*page_to_inc) == p_chain->page_cnt) {
  			*page_to_inc = 0;
  			*p_next_elem = p_chain->p_virt_addr;
  		}
  	}
  }
  
  #define is_unusable_idx(p, idx)	\
  	(((p)->idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
  
  #define is_unusable_next_idx(p, idx) \
  	((((p)->idx + 1) & (p)->elem_per_page_mask) == (p)->usable_per_page)
  
  #define test_ans_skip(p, idx)				\
  	do {						\
  		if (is_unusable_idx(p, idx)) {		\
  			(p)->idx += (p)->elem_unusable;	\
  		}					\
  	} while (0)
  
  /**
   * @brief qed_chain_return_multi_produced -
   *
   * A chain in which the driver "Produces" elements should use this API
   * to indicate previous produced elements are now consumed.
   *
   * @param p_chain
   * @param num
   */
  static inline void
  qed_chain_return_multi_produced(struct qed_chain *p_chain,
  				u16 num)
  {
  	p_chain->cons_idx += num;
  	test_ans_skip(p_chain, cons_idx);
  }
  
  /**
   * @brief qed_chain_return_produced -
   *
   * A chain in which the driver "Produces" elements should use this API
   * to indicate previous produced elements are now consumed.
   *
   * @param p_chain
   */
  static inline void qed_chain_return_produced(struct qed_chain *p_chain)
  {
  	p_chain->cons_idx++;
  	test_ans_skip(p_chain, cons_idx);
  }
  
  /**
   * @brief qed_chain_produce -
   *
   * A chain in which the driver "Produces" elements should use this to get
   * a pointer to the next element which can be "Produced". It's driver
   * responsibility to validate that the chain has room for new element.
   *
   * @param p_chain
   *
   * @return void*, a pointer to next element
   */
  static inline void *qed_chain_produce(struct qed_chain *p_chain)
  {
  	void *ret = NULL;
  
  	if ((p_chain->prod_idx & p_chain->elem_per_page_mask) ==
  	    p_chain->next_page_mask) {
  		qed_chain_advance_page(p_chain, &p_chain->p_prod_elem,
  				       &p_chain->prod_idx,
  				       &p_chain->pbl.prod_page_idx);
  	}
  
  	ret = p_chain->p_prod_elem;
  	p_chain->prod_idx++;
  	p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
  					p_chain->elem_size);
  
  	return ret;
  }
  
  /**
   * @brief qed_chain_get_capacity -
   *
   * Get the maximum number of BDs in chain
   *
   * @param p_chain
   * @param num
   *
   * @return u16, number of unusable BDs
   */
  static inline u16 qed_chain_get_capacity(struct qed_chain *p_chain)
  {
  	return p_chain->capacity;
  }
  
  /**
   * @brief qed_chain_recycle_consumed -
   *
   * Returns an element which was previously consumed;
   * Increments producers so they could be written to FW.
   *
   * @param p_chain
   */
  static inline void
  qed_chain_recycle_consumed(struct qed_chain *p_chain)
  {
  	test_ans_skip(p_chain, prod_idx);
  	p_chain->prod_idx++;
  }
  
  /**
   * @brief qed_chain_consume -
   *
   * A Chain in which the driver utilizes data written by a different source
   * (i.e., FW) should use this to access passed buffers.
   *
   * @param p_chain
   *
   * @return void*, a pointer to the next buffer written
   */
  static inline void *qed_chain_consume(struct qed_chain *p_chain)
  {
  	void *ret = NULL;
  
  	if ((p_chain->cons_idx & p_chain->elem_per_page_mask) ==
  	    p_chain->next_page_mask) {
  		qed_chain_advance_page(p_chain, &p_chain->p_cons_elem,
  				       &p_chain->cons_idx,
  				       &p_chain->pbl.cons_page_idx);
  	}
  
  	ret = p_chain->p_cons_elem;
  	p_chain->cons_idx++;
  	p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
  					p_chain->elem_size);
  
  	return ret;
  }
  
  /**
   * @brief qed_chain_reset - Resets the chain to its start state
   *
   * @param p_chain pointer to a previously allocted chain
   */
  static inline void qed_chain_reset(struct qed_chain *p_chain)
  {
  	int i;
  
  	p_chain->prod_idx	= 0;
  	p_chain->cons_idx	= 0;
  	p_chain->p_cons_elem	= p_chain->p_virt_addr;
  	p_chain->p_prod_elem	= p_chain->p_virt_addr;
  
  	if (p_chain->mode == QED_CHAIN_MODE_PBL) {
  		p_chain->pbl.prod_page_idx	= p_chain->page_cnt - 1;
  		p_chain->pbl.cons_page_idx	= p_chain->page_cnt - 1;
  	}
  
  	switch (p_chain->intended_use) {
  	case QED_CHAIN_USE_TO_CONSUME_PRODUCE:
  	case QED_CHAIN_USE_TO_PRODUCE:
  		/* Do nothing */
  		break;
  
  	case QED_CHAIN_USE_TO_CONSUME:
  		/* produce empty elements */
  		for (i = 0; i < p_chain->capacity; i++)
  			qed_chain_recycle_consumed(p_chain);
  		break;
  	}
  }
  
  /**
   * @brief qed_chain_init - Initalizes a basic chain struct
   *
   * @param p_chain
   * @param p_virt_addr
   * @param p_phys_addr	physical address of allocated buffer's beginning
   * @param page_cnt	number of pages in the allocated buffer
   * @param elem_size	size of each element in the chain
   * @param intended_use
   * @param mode
   */
  static inline void qed_chain_init(struct qed_chain *p_chain,
  				  void *p_virt_addr,
  				  dma_addr_t p_phys_addr,
  				  u16 page_cnt,
  				  u8 elem_size,
  				  enum qed_chain_use_mode intended_use,
  				  enum qed_chain_mode mode)
  {
  	/* chain fixed parameters */
  	p_chain->p_virt_addr	= p_virt_addr;
  	p_chain->p_phys_addr	= p_phys_addr;
  	p_chain->elem_size	= elem_size;
  	p_chain->page_cnt	= page_cnt;
  	p_chain->mode		= mode;
  
  	p_chain->intended_use		= intended_use;
  	p_chain->elem_per_page		= ELEMS_PER_PAGE(elem_size);
  	p_chain->usable_per_page =
  		USABLE_ELEMS_PER_PAGE(elem_size, mode);
  	p_chain->capacity		= p_chain->usable_per_page * page_cnt;
  	p_chain->size			= p_chain->elem_per_page * page_cnt;
  	p_chain->elem_per_page_mask	= p_chain->elem_per_page - 1;
  
  	p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
  
  	p_chain->next_page_mask = (p_chain->usable_per_page &
  				   p_chain->elem_per_page_mask);
  
  	if (mode == QED_CHAIN_MODE_NEXT_PTR) {
  		struct qed_chain_next	*p_next;
  		u16			i;
  
  		for (i = 0; i < page_cnt - 1; i++) {
  			/* Increment mem_phy to the next page. */
  			p_phys_addr += QED_CHAIN_PAGE_SIZE;
  
  			/* Initialize the physical address of the next page. */
  			p_next = (struct qed_chain_next *)((u8 *)p_virt_addr +
  							   elem_size *
  							   p_chain->
  							   usable_per_page);
  
  			p_next->next_phys.lo	= DMA_LO_LE(p_phys_addr);
  			p_next->next_phys.hi	= DMA_HI_LE(p_phys_addr);
  
  			/* Initialize the virtual address of the next page. */
  			p_next->next_virt = (void *)((u8 *)p_virt_addr +
  						     QED_CHAIN_PAGE_SIZE);
  
  			/* Move to the next page. */
  			p_virt_addr = p_next->next_virt;
  		}
  
  		/* Last page's next should point to beginning of the chain */
  		p_next = (struct qed_chain_next *)((u8 *)p_virt_addr +
  						   elem_size *
  						   p_chain->usable_per_page);
  
  		p_next->next_phys.lo	= DMA_LO_LE(p_chain->p_phys_addr);
  		p_next->next_phys.hi	= DMA_HI_LE(p_chain->p_phys_addr);
  		p_next->next_virt	= p_chain->p_virt_addr;
  	}
  	qed_chain_reset(p_chain);
  }
  
  /**
   * @brief qed_chain_pbl_init - Initalizes a basic pbl chain
   *        struct
   * @param p_chain
   * @param p_virt_addr	virtual address of allocated buffer's beginning
   * @param p_phys_addr	physical address of allocated buffer's beginning
   * @param page_cnt	number of pages in the allocated buffer
   * @param elem_size	size of each element in the chain
   * @param use_mode
   * @param p_phys_pbl	pointer to a pre-allocated side table
   *                      which will hold physical page addresses.
   * @param p_virt_pbl	pointer to a pre allocated side table
   *                      which will hold virtual page addresses.
   */
  static inline void
  qed_chain_pbl_init(struct qed_chain *p_chain,
  		   void *p_virt_addr,
  		   dma_addr_t p_phys_addr,
  		   u16 page_cnt,
  		   u8 elem_size,
  		   enum qed_chain_use_mode use_mode,
  		   dma_addr_t p_phys_pbl,
  		   dma_addr_t *p_virt_pbl)
  {
  	dma_addr_t *p_pbl_dma = p_virt_pbl;
  	int i;
  
  	qed_chain_init(p_chain, p_virt_addr, p_phys_addr, page_cnt,
  		       elem_size, use_mode, QED_CHAIN_MODE_PBL);
  
  	p_chain->pbl.p_phys_table = p_phys_pbl;
  	p_chain->pbl.p_virt_table = p_virt_pbl;
  
  	/* Fill the PBL with physical addresses*/
  	for (i = 0; i < page_cnt; i++) {
  		*p_pbl_dma = p_phys_addr;
  		p_phys_addr += QED_CHAIN_PAGE_SIZE;
  		p_pbl_dma++;
  	}
  }
  
  /**
   * @brief qed_chain_set_prod - sets the prod to the given
   *        value
   *
   * @param prod_idx
   * @param p_prod_elem
   */
  static inline void qed_chain_set_prod(struct qed_chain *p_chain,
  				      u16 prod_idx,
  				      void *p_prod_elem)
  {
  	p_chain->prod_idx	= prod_idx;
  	p_chain->p_prod_elem	= p_prod_elem;
  }
  
  /**
   * @brief qed_chain_get_elem -
   *
   * get a pointer to an element represented by absolute idx
   *
   * @param p_chain
   * @assumption p_chain->size is a power of 2
   *
   * @return void*, a pointer to next element
   */
  static inline void *qed_chain_sge_get_elem(struct qed_chain *p_chain,
  					   u16 idx)
  {
  	void *ret = NULL;
  
  	if (idx >= p_chain->size)
  		return NULL;
  
  	ret = (u8 *)p_chain->p_virt_addr + p_chain->elem_size * idx;
  
  	return ret;
  }
  
  /**
   * @brief qed_chain_sge_inc_cons_prod
   *
   * for sge chains, producer isn't increased serially, the ring
   * is expected to be full at all times. Once elements are
   * consumed, they are immediately produced.
   *
   * @param p_chain
   * @param cnt
   *
   * @return inline void
   */
  static inline void
  qed_chain_sge_inc_cons_prod(struct qed_chain *p_chain,
  			    u16 cnt)
  {
  	p_chain->prod_idx += cnt;
  	p_chain->cons_idx += cnt;
  }
  
  #endif