xref: /linux/drivers/net/ethernet/cavium/liquidio/octeon_network.h (revision fcc8487d477a3452a1d0ccbdd4c5e0e1e3cb8bed)
1 /**********************************************************************
2  * Author: Cavium, Inc.
3  *
4  * Contact: support@cavium.com
5  *          Please include "LiquidIO" in the subject.
6  *
7  * Copyright (c) 2003-2016 Cavium, Inc.
8  *
9  * This file is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License, Version 2, as
11  * published by the Free Software Foundation.
12  *
13  * This file is distributed in the hope that it will be useful, but
14  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16  * NONINFRINGEMENT.  See the GNU General Public License for more
17  * details.
18  **********************************************************************/
19 
20 /*!  \file  octeon_network.h
21  *   \brief Host NIC Driver: Structure and Macro definitions used by NIC Module.
22  */
23 
24 #ifndef __OCTEON_NETWORK_H__
25 #define __OCTEON_NETWORK_H__
26 #include <linux/ptp_clock_kernel.h>
27 
28 #define LIO_MAX_MTU_SIZE (OCTNET_MAX_FRM_SIZE - OCTNET_FRM_HEADER_SIZE)
29 #define LIO_MIN_MTU_SIZE ETH_MIN_MTU
30 
31 /* Bit mask values for lio->ifstate */
32 #define   LIO_IFSTATE_DROQ_OPS             0x01
33 #define   LIO_IFSTATE_REGISTERED           0x02
34 #define   LIO_IFSTATE_RUNNING              0x04
35 #define   LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
36 
37 struct oct_nic_stats_resp {
38 	u64     rh;
39 	struct oct_link_stats stats;
40 	u64     status;
41 };
42 
43 struct oct_nic_stats_ctrl {
44 	struct completion complete;
45 	struct net_device *netdev;
46 };
47 
48 /** LiquidIO per-interface network private data */
49 struct lio {
50 	/** State of the interface. Rx/Tx happens only in the RUNNING state.  */
51 	atomic_t ifstate;
52 
53 	/** Octeon Interface index number. This device will be represented as
54 	 *  oct<ifidx> in the system.
55 	 */
56 	int ifidx;
57 
58 	/** Octeon Input queue to use to transmit for this network interface. */
59 	int txq;
60 
61 	/** Octeon Output queue from which pkts arrive
62 	 * for this network interface.
63 	 */
64 	int rxq;
65 
66 	/** Guards each glist */
67 	spinlock_t *glist_lock;
68 
69 	/** Array of gather component linked lists */
70 	struct list_head *glist;
71 	void **glists_virt_base;
72 	dma_addr_t *glists_dma_base;
73 	u32 glist_entry_size;
74 
75 	/** Pointer to the NIC properties for the Octeon device this network
76 	 *  interface is associated with.
77 	 */
78 	struct octdev_props *octprops;
79 
80 	/** Pointer to the octeon device structure. */
81 	struct octeon_device *oct_dev;
82 
83 	struct net_device *netdev;
84 
85 	/** Link information sent by the core application for this interface. */
86 	struct oct_link_info linfo;
87 
88 	/** counter of link changes */
89 	u64 link_changes;
90 
91 	/** Size of Tx queue for this octeon device. */
92 	u32 tx_qsize;
93 
94 	/** Size of Rx queue for this octeon device. */
95 	u32 rx_qsize;
96 
97 	/** Size of MTU this octeon device. */
98 	u32 mtu;
99 
100 	/** msg level flag per interface. */
101 	u32 msg_enable;
102 
103 	/** Copy of Interface capabilities: TSO, TSO6, LRO, Chescksums . */
104 	u64 dev_capability;
105 
106 	/* Copy of transmit encapsulation capabilities:
107 	 * TSO, TSO6, Checksums for this device for Kernel
108 	 * 3.10.0 onwards
109 	 */
110 	u64 enc_dev_capability;
111 
112 	/** Copy of beacaon reg in phy */
113 	u32 phy_beacon_val;
114 
115 	/** Copy of ctrl reg in phy */
116 	u32 led_ctrl_val;
117 
118 	/* PTP clock information */
119 	struct ptp_clock_info ptp_info;
120 	struct ptp_clock *ptp_clock;
121 	s64 ptp_adjust;
122 
123 	/* for atomic access to Octeon PTP reg and data struct */
124 	spinlock_t ptp_lock;
125 
126 	/* Interface info */
127 	u32	intf_open;
128 
129 	/* work queue for  txq status */
130 	struct cavium_wq	txq_status_wq;
131 
132 	/* work queue for  rxq oom status */
133 	struct cavium_wq	rxq_status_wq;
134 
135 	/* work queue for  link status */
136 	struct cavium_wq	link_status_wq;
137 
138 	int netdev_uc_count;
139 };
140 
141 #define LIO_SIZE         (sizeof(struct lio))
142 #define GET_LIO(netdev)  ((struct lio *)netdev_priv(netdev))
143 
144 #define LIO_MAX_CORES                12
145 
146 /**
147  * \brief Enable or disable feature
148  * @param netdev    pointer to network device
149  * @param cmd       Command that just requires acknowledgment
150  * @param param1    Parameter to command
151  */
152 int liquidio_set_feature(struct net_device *netdev, int cmd, u16 param1);
153 
154 int setup_rx_oom_poll_fn(struct net_device *netdev);
155 
156 void cleanup_rx_oom_poll_fn(struct net_device *netdev);
157 
158 /**
159  * \brief Link control command completion callback
160  * @param nctrl_ptr pointer to control packet structure
161  *
162  * This routine is called by the callback function when a ctrl pkt sent to
163  * core app completes. The nctrl_ptr contains a copy of the command type
164  * and data sent to the core app. This routine is only called if the ctrl
165  * pkt was sent successfully to the core app.
166  */
167 void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr);
168 
169 /**
170  * \brief Register ethtool operations
171  * @param netdev    pointer to network device
172  */
173 void liquidio_set_ethtool_ops(struct net_device *netdev);
174 
175 #define SKB_ADJ_MASK  0x3F
176 #define SKB_ADJ       (SKB_ADJ_MASK + 1)
177 
178 #define MIN_SKB_SIZE       256 /* 8 bytes and more - 8 bytes for PTP */
179 #define LIO_RXBUFFER_SZ    2048
180 
181 static inline void
182 *recv_buffer_alloc(struct octeon_device *oct,
183 		   struct octeon_skb_page_info *pg_info)
184 {
185 	struct page *page;
186 	struct sk_buff *skb;
187 	struct octeon_skb_page_info *skb_pg_info;
188 
189 	page = alloc_page(GFP_ATOMIC | __GFP_COLD);
190 	if (unlikely(!page))
191 		return NULL;
192 
193 	skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
194 	if (unlikely(!skb)) {
195 		__free_page(page);
196 		pg_info->page = NULL;
197 		return NULL;
198 	}
199 
200 	if ((unsigned long)skb->data & SKB_ADJ_MASK) {
201 		u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
202 
203 		skb_reserve(skb, r);
204 	}
205 
206 	skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
207 	/* Get DMA info */
208 	pg_info->dma = dma_map_page(&oct->pci_dev->dev, page, 0,
209 				    PAGE_SIZE, DMA_FROM_DEVICE);
210 
211 	/* Mapping failed!! */
212 	if (dma_mapping_error(&oct->pci_dev->dev, pg_info->dma)) {
213 		__free_page(page);
214 		dev_kfree_skb_any((struct sk_buff *)skb);
215 		pg_info->page = NULL;
216 		return NULL;
217 	}
218 
219 	pg_info->page = page;
220 	pg_info->page_offset = 0;
221 	skb_pg_info->page = page;
222 	skb_pg_info->page_offset = 0;
223 	skb_pg_info->dma = pg_info->dma;
224 
225 	return (void *)skb;
226 }
227 
228 static inline void
229 *recv_buffer_fast_alloc(u32 size)
230 {
231 	struct sk_buff *skb;
232 	struct octeon_skb_page_info *skb_pg_info;
233 
234 	skb = dev_alloc_skb(size + SKB_ADJ);
235 	if (unlikely(!skb))
236 		return NULL;
237 
238 	if ((unsigned long)skb->data & SKB_ADJ_MASK) {
239 		u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
240 
241 		skb_reserve(skb, r);
242 	}
243 
244 	skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
245 	skb_pg_info->page = NULL;
246 	skb_pg_info->page_offset = 0;
247 	skb_pg_info->dma = 0;
248 
249 	return skb;
250 }
251 
252 static inline int
253 recv_buffer_recycle(struct octeon_device *oct, void *buf)
254 {
255 	struct octeon_skb_page_info *pg_info = buf;
256 
257 	if (!pg_info->page) {
258 		dev_err(&oct->pci_dev->dev, "%s: pg_info->page NULL\n",
259 			__func__);
260 		return -ENOMEM;
261 	}
262 
263 	if (unlikely(page_count(pg_info->page) != 1) ||
264 	    unlikely(page_to_nid(pg_info->page)	!= numa_node_id())) {
265 		dma_unmap_page(&oct->pci_dev->dev,
266 			       pg_info->dma, (PAGE_SIZE << 0),
267 			       DMA_FROM_DEVICE);
268 		pg_info->dma = 0;
269 		pg_info->page = NULL;
270 		pg_info->page_offset = 0;
271 		return -ENOMEM;
272 	}
273 
274 	/* Flip to other half of the buffer */
275 	if (pg_info->page_offset == 0)
276 		pg_info->page_offset = LIO_RXBUFFER_SZ;
277 	else
278 		pg_info->page_offset = 0;
279 	page_ref_inc(pg_info->page);
280 
281 	return 0;
282 }
283 
284 static inline void
285 *recv_buffer_reuse(struct octeon_device *oct, void *buf)
286 {
287 	struct octeon_skb_page_info *pg_info = buf, *skb_pg_info;
288 	struct sk_buff *skb;
289 
290 	skb = dev_alloc_skb(MIN_SKB_SIZE + SKB_ADJ);
291 	if (unlikely(!skb)) {
292 		dma_unmap_page(&oct->pci_dev->dev,
293 			       pg_info->dma, (PAGE_SIZE << 0),
294 			       DMA_FROM_DEVICE);
295 		return NULL;
296 	}
297 
298 	if ((unsigned long)skb->data & SKB_ADJ_MASK) {
299 		u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
300 
301 		skb_reserve(skb, r);
302 	}
303 
304 	skb_pg_info = ((struct octeon_skb_page_info *)(skb->cb));
305 	skb_pg_info->page = pg_info->page;
306 	skb_pg_info->page_offset = pg_info->page_offset;
307 	skb_pg_info->dma = pg_info->dma;
308 
309 	return skb;
310 }
311 
312 static inline void
313 recv_buffer_destroy(void *buffer, struct octeon_skb_page_info *pg_info)
314 {
315 	struct sk_buff *skb = (struct sk_buff *)buffer;
316 
317 	put_page(pg_info->page);
318 	pg_info->dma = 0;
319 	pg_info->page = NULL;
320 	pg_info->page_offset = 0;
321 
322 	if (skb)
323 		dev_kfree_skb_any(skb);
324 }
325 
326 static inline void recv_buffer_free(void *buffer)
327 {
328 	struct sk_buff *skb = (struct sk_buff *)buffer;
329 	struct octeon_skb_page_info *pg_info;
330 
331 	pg_info = ((struct octeon_skb_page_info *)(skb->cb));
332 
333 	if (pg_info->page) {
334 		put_page(pg_info->page);
335 		pg_info->dma = 0;
336 		pg_info->page = NULL;
337 		pg_info->page_offset = 0;
338 	}
339 
340 	dev_kfree_skb_any((struct sk_buff *)buffer);
341 }
342 
343 static inline void
344 recv_buffer_fast_free(void *buffer)
345 {
346 	dev_kfree_skb_any((struct sk_buff *)buffer);
347 }
348 
349 static inline void tx_buffer_free(void *buffer)
350 {
351 	dev_kfree_skb_any((struct sk_buff *)buffer);
352 }
353 
354 #define lio_dma_alloc(oct, size, dma_addr) \
355 	dma_alloc_coherent(&(oct)->pci_dev->dev, size, dma_addr, GFP_KERNEL)
356 #define lio_dma_free(oct, size, virt_addr, dma_addr) \
357 	dma_free_coherent(&(oct)->pci_dev->dev, size, virt_addr, dma_addr)
358 
359 static inline void *
360 lio_alloc_info_buffer(struct octeon_device *oct,
361 		      struct octeon_droq *droq)
362 {
363 	void *virt_ptr;
364 
365 	virt_ptr = lio_dma_alloc(oct, (droq->max_count * OCT_DROQ_INFO_SIZE),
366 				 &droq->info_list_dma);
367 	if (virt_ptr) {
368 		droq->info_alloc_size = droq->max_count * OCT_DROQ_INFO_SIZE;
369 		droq->info_base_addr = virt_ptr;
370 	}
371 
372 	return virt_ptr;
373 }
374 
375 static inline void lio_free_info_buffer(struct octeon_device *oct,
376 					struct octeon_droq *droq)
377 {
378 	lio_dma_free(oct, droq->info_alloc_size, droq->info_base_addr,
379 		     droq->info_list_dma);
380 }
381 
382 static inline
383 void *get_rbd(struct sk_buff *skb)
384 {
385 	struct octeon_skb_page_info *pg_info;
386 	unsigned char *va;
387 
388 	pg_info = ((struct octeon_skb_page_info *)(skb->cb));
389 	va = page_address(pg_info->page) + pg_info->page_offset;
390 
391 	return va;
392 }
393 
394 static inline u64
395 lio_map_ring_info(struct octeon_droq *droq, u32 i)
396 {
397 	return droq->info_list_dma + (i * sizeof(struct octeon_droq_info));
398 }
399 
400 static inline u64
401 lio_map_ring(void *buf)
402 {
403 	dma_addr_t dma_addr;
404 
405 	struct sk_buff *skb = (struct sk_buff *)buf;
406 	struct octeon_skb_page_info *pg_info;
407 
408 	pg_info = ((struct octeon_skb_page_info *)(skb->cb));
409 	if (!pg_info->page) {
410 		pr_err("%s: pg_info->page NULL\n", __func__);
411 		WARN_ON(1);
412 	}
413 
414 	/* Get DMA info */
415 	dma_addr = pg_info->dma;
416 	if (!pg_info->dma) {
417 		pr_err("%s: ERROR it should be already available\n",
418 		       __func__);
419 		WARN_ON(1);
420 	}
421 	dma_addr += pg_info->page_offset;
422 
423 	return (u64)dma_addr;
424 }
425 
426 static inline void
427 lio_unmap_ring(struct pci_dev *pci_dev,
428 	       u64 buf_ptr)
429 
430 {
431 	dma_unmap_page(&pci_dev->dev,
432 		       buf_ptr, (PAGE_SIZE << 0),
433 		       DMA_FROM_DEVICE);
434 }
435 
436 static inline void *octeon_fast_packet_alloc(u32 size)
437 {
438 	return recv_buffer_fast_alloc(size);
439 }
440 
441 static inline void octeon_fast_packet_next(struct octeon_droq *droq,
442 					   struct sk_buff *nicbuf,
443 					   int copy_len,
444 					   int idx)
445 {
446 	memcpy(skb_put(nicbuf, copy_len),
447 	       get_rbd(droq->recv_buf_list[idx].buffer), copy_len);
448 }
449 
450 /**
451  * \brief check interface state
452  * @param lio per-network private data
453  * @param state_flag flag state to check
454  */
455 static inline int ifstate_check(struct lio *lio, int state_flag)
456 {
457 	return atomic_read(&lio->ifstate) & state_flag;
458 }
459 
460 /**
461  * \brief set interface state
462  * @param lio per-network private data
463  * @param state_flag flag state to set
464  */
465 static inline void ifstate_set(struct lio *lio, int state_flag)
466 {
467 	atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
468 }
469 
470 /**
471  * \brief clear interface state
472  * @param lio per-network private data
473  * @param state_flag flag state to clear
474  */
475 static inline void ifstate_reset(struct lio *lio, int state_flag)
476 {
477 	atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
478 }
479 
480 #endif
481