1 /**********************************************************************
2  * Author: Cavium, Inc.
3  *
4  * Contact: support@cavium.com
5  *          Please include "LiquidIO" in the subject.
6  *
7  * Copyright (c) 2003-2015 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  * This file may also be available under a different license from Cavium.
20  * Contact Cavium, Inc. for more information
21  **********************************************************************/
22 
23 /*!  \file  octeon_network.h
24  *   \brief Host NIC Driver: Structure and Macro definitions used by NIC Module.
25  */
26 
27 #ifndef __OCTEON_NETWORK_H__
28 #define __OCTEON_NETWORK_H__
29 #include <linux/version.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/ptp_clock_kernel.h>
32 
33 /** LiquidIO per-interface network private data */
34 struct lio {
35 	/** State of the interface. Rx/Tx happens only in the RUNNING state.  */
36 	atomic_t ifstate;
37 
38 	/** Octeon Interface index number. This device will be represented as
39 	 *  oct<ifidx> in the system.
40 	 */
41 	int ifidx;
42 
43 	/** Octeon Input queue to use to transmit for this network interface. */
44 	int txq;
45 
46 	/** Octeon Output queue from which pkts arrive
47 	 * for this network interface.
48 	 */
49 	int rxq;
50 
51 	/** Guards the glist */
52 	spinlock_t lock;
53 
54 	/** Linked list of gather components */
55 	struct list_head glist;
56 
57 	/** Pointer to the NIC properties for the Octeon device this network
58 	 *  interface is associated with.
59 	 */
60 	struct octdev_props *octprops;
61 
62 	/** Pointer to the octeon device structure. */
63 	struct octeon_device *oct_dev;
64 
65 	struct net_device *netdev;
66 
67 	/** Link information sent by the core application for this interface. */
68 	struct oct_link_info linfo;
69 
70 	/** Size of Tx queue for this octeon device. */
71 	u32 tx_qsize;
72 
73 	/** Size of Rx queue for this octeon device. */
74 	u32 rx_qsize;
75 
76 	/** Size of MTU this octeon device. */
77 	u32 mtu;
78 
79 	/** msg level flag per interface. */
80 	u32 msg_enable;
81 
82 	/** Copy of Interface capabilities: TSO, TSO6, LRO, Chescksums . */
83 	u64 dev_capability;
84 
85 	/** Copy of beacaon reg in phy */
86 	u32 phy_beacon_val;
87 
88 	/** Copy of ctrl reg in phy */
89 	u32 led_ctrl_val;
90 
91 	/* PTP clock information */
92 	struct ptp_clock_info ptp_info;
93 	struct ptp_clock *ptp_clock;
94 	s64 ptp_adjust;
95 
96 	/* for atomic access to Octeon PTP reg and data struct */
97 	spinlock_t ptp_lock;
98 
99 	/* Interface info */
100 	u32	intf_open;
101 
102 	/* work queue for  txq status */
103 	struct cavium_wq	txq_status_wq;
104 
105 };
106 
107 #define LIO_SIZE         (sizeof(struct lio))
108 #define GET_LIO(netdev)  ((struct lio *)netdev_priv(netdev))
109 
110 /**
111  * \brief Enable or disable feature
112  * @param netdev    pointer to network device
113  * @param cmd       Command that just requires acknowledgment
114  */
115 int liquidio_set_feature(struct net_device *netdev, int cmd);
116 
117 /**
118  * \brief Link control command completion callback
119  * @param nctrl_ptr pointer to control packet structure
120  *
121  * This routine is called by the callback function when a ctrl pkt sent to
122  * core app completes. The nctrl_ptr contains a copy of the command type
123  * and data sent to the core app. This routine is only called if the ctrl
124  * pkt was sent successfully to the core app.
125  */
126 void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr);
127 
128 /**
129  * \brief Register ethtool operations
130  * @param netdev    pointer to network device
131  */
132 void liquidio_set_ethtool_ops(struct net_device *netdev);
133 
134 static inline void
135 *recv_buffer_alloc(struct octeon_device *oct __attribute__((unused)),
136 		   u32 q_no __attribute__((unused)), u32 size)
137 {
138 #define SKB_ADJ_MASK  0x3F
139 #define SKB_ADJ       (SKB_ADJ_MASK + 1)
140 
141 	struct sk_buff *skb = dev_alloc_skb(size + SKB_ADJ);
142 
143 	if ((unsigned long)skb->data & SKB_ADJ_MASK) {
144 		u32 r = SKB_ADJ - ((unsigned long)skb->data & SKB_ADJ_MASK);
145 
146 		skb_reserve(skb, r);
147 	}
148 
149 	return (void *)skb;
150 }
151 
152 static inline void recv_buffer_free(void *buffer)
153 {
154 	dev_kfree_skb_any((struct sk_buff *)buffer);
155 }
156 
157 #define lio_dma_alloc(oct, size, dma_addr) \
158 	dma_alloc_coherent(&oct->pci_dev->dev, size, dma_addr, GFP_KERNEL)
159 #define lio_dma_free(oct, size, virt_addr, dma_addr) \
160 	dma_free_coherent(&oct->pci_dev->dev, size, virt_addr, dma_addr)
161 
162 #define   get_rbd(ptr)      (((struct sk_buff *)(ptr))->data)
163 
164 static inline u64
165 lio_map_ring_info(struct octeon_droq *droq, u32 i)
166 {
167 	dma_addr_t dma_addr;
168 	struct octeon_device *oct = droq->oct_dev;
169 
170 	dma_addr = dma_map_single(&oct->pci_dev->dev, &droq->info_list[i],
171 				  OCT_DROQ_INFO_SIZE, DMA_FROM_DEVICE);
172 
173 	BUG_ON(dma_mapping_error(&oct->pci_dev->dev, dma_addr));
174 
175 	return (u64)dma_addr;
176 }
177 
178 static inline void
179 lio_unmap_ring_info(struct pci_dev *pci_dev,
180 		    u64 info_ptr, u32 size)
181 {
182 	dma_unmap_single(&pci_dev->dev, info_ptr, size, DMA_FROM_DEVICE);
183 }
184 
185 static inline u64
186 lio_map_ring(struct pci_dev *pci_dev,
187 	     void *buf, u32 size)
188 {
189 	dma_addr_t dma_addr;
190 
191 	dma_addr = dma_map_single(&pci_dev->dev, get_rbd(buf), size,
192 				  DMA_FROM_DEVICE);
193 
194 	BUG_ON(dma_mapping_error(&pci_dev->dev, dma_addr));
195 
196 	return (u64)dma_addr;
197 }
198 
199 static inline void
200 lio_unmap_ring(struct pci_dev *pci_dev,
201 	       u64 buf_ptr, u32 size)
202 {
203 	dma_unmap_single(&pci_dev->dev,
204 			 buf_ptr, size,
205 			 DMA_FROM_DEVICE);
206 }
207 
208 static inline void *octeon_fast_packet_alloc(struct octeon_device *oct,
209 					     struct octeon_droq *droq,
210 					     u32 q_no, u32 size)
211 {
212 	return recv_buffer_alloc(oct, q_no, size);
213 }
214 
215 static inline void octeon_fast_packet_next(struct octeon_droq *droq,
216 					   struct sk_buff *nicbuf,
217 					   int copy_len,
218 					   int idx)
219 {
220 	memcpy(skb_put(nicbuf, copy_len),
221 	       get_rbd(droq->recv_buf_list[idx].buffer), copy_len);
222 }
223 
224 #endif
225