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