xref: /linux/drivers/net/ethernet/cavium/liquidio/octeon_main.h (revision f6f3bac08ff9855d803081a353a1fafaa8845739)
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 details.
17  ***********************************************************************/
18 /*! \file octeon_main.h
19  *  \brief Host Driver: This file is included by all host driver source files
20  *  to include common definitions.
21  */
22 
23 #ifndef _OCTEON_MAIN_H_
24 #define  _OCTEON_MAIN_H_
25 
26 #include <linux/sched/signal.h>
27 
28 #if BITS_PER_LONG == 32
29 #define CVM_CAST64(v) ((long long)(v))
30 #elif BITS_PER_LONG == 64
31 #define CVM_CAST64(v) ((long long)(long)(v))
32 #else
33 #error "Unknown system architecture"
34 #endif
35 
36 #define DRV_NAME "LiquidIO"
37 
38 struct octeon_device_priv {
39 	/** Tasklet structures for this device. */
40 	struct tasklet_struct droq_tasklet;
41 	unsigned long napi_mask;
42 };
43 
44 /** This structure is used by NIC driver to store information required
45  * to free the sk_buff when the packet has been fetched by Octeon.
46  * Bytes offset below assume worst-case of a 64-bit system.
47  */
48 struct octnet_buf_free_info {
49 	/** Bytes 1-8.  Pointer to network device private structure. */
50 	struct lio *lio;
51 
52 	/** Bytes 9-16.  Pointer to sk_buff. */
53 	struct sk_buff *skb;
54 
55 	/** Bytes 17-24.  Pointer to gather list. */
56 	struct octnic_gather *g;
57 
58 	/** Bytes 25-32. Physical address of skb->data or gather list. */
59 	u64 dptr;
60 
61 	/** Bytes 33-47. Piggybacked soft command, if any */
62 	struct octeon_soft_command *sc;
63 };
64 
65 /* BQL-related functions */
66 int octeon_report_sent_bytes_to_bql(void *buf, int reqtype);
67 void octeon_update_tx_completion_counters(void *buf, int reqtype,
68 					  unsigned int *pkts_compl,
69 					  unsigned int *bytes_compl);
70 void octeon_report_tx_completion_to_bql(void *txq, unsigned int pkts_compl,
71 					unsigned int bytes_compl);
72 void octeon_pf_changed_vf_macaddr(struct octeon_device *oct, u8 *mac);
73 /** Swap 8B blocks */
74 static inline void octeon_swap_8B_data(u64 *data, u32 blocks)
75 {
76 	while (blocks) {
77 		cpu_to_be64s(data);
78 		blocks--;
79 		data++;
80 	}
81 }
82 
83 /**
84  * \brief unmaps a PCI BAR
85  * @param oct Pointer to Octeon device
86  * @param baridx bar index
87  */
88 static inline void octeon_unmap_pci_barx(struct octeon_device *oct, int baridx)
89 {
90 	dev_dbg(&oct->pci_dev->dev, "Freeing PCI mapped regions for Bar%d\n",
91 		baridx);
92 
93 	if (oct->mmio[baridx].done)
94 		iounmap(oct->mmio[baridx].hw_addr);
95 
96 	if (oct->mmio[baridx].start)
97 		pci_release_region(oct->pci_dev, baridx * 2);
98 }
99 
100 /**
101  * \brief maps a PCI BAR
102  * @param oct Pointer to Octeon device
103  * @param baridx bar index
104  * @param max_map_len maximum length of mapped memory
105  */
106 static inline int octeon_map_pci_barx(struct octeon_device *oct,
107 				      int baridx, int max_map_len)
108 {
109 	u32 mapped_len = 0;
110 
111 	if (pci_request_region(oct->pci_dev, baridx * 2, DRV_NAME)) {
112 		dev_err(&oct->pci_dev->dev, "pci_request_region failed for bar %d\n",
113 			baridx);
114 		return 1;
115 	}
116 
117 	oct->mmio[baridx].start = pci_resource_start(oct->pci_dev, baridx * 2);
118 	oct->mmio[baridx].len = pci_resource_len(oct->pci_dev, baridx * 2);
119 
120 	mapped_len = oct->mmio[baridx].len;
121 	if (!mapped_len)
122 		goto err_release_region;
123 
124 	if (max_map_len && (mapped_len > max_map_len))
125 		mapped_len = max_map_len;
126 
127 	oct->mmio[baridx].hw_addr =
128 		ioremap(oct->mmio[baridx].start, mapped_len);
129 	oct->mmio[baridx].mapped_len = mapped_len;
130 
131 	dev_dbg(&oct->pci_dev->dev, "BAR%d start: 0x%llx mapped %u of %u bytes\n",
132 		baridx, oct->mmio[baridx].start, mapped_len,
133 		oct->mmio[baridx].len);
134 
135 	if (!oct->mmio[baridx].hw_addr) {
136 		dev_err(&oct->pci_dev->dev, "error ioremap for bar %d\n",
137 			baridx);
138 		goto err_release_region;
139 	}
140 	oct->mmio[baridx].done = 1;
141 
142 	return 0;
143 
144 err_release_region:
145 	pci_release_region(oct->pci_dev, baridx * 2);
146 	return 1;
147 }
148 
149 /* input parameter:
150  * sc: pointer to a soft request
151  * timeout: milli sec which an application wants to wait for the
152 	    response of the request.
153  *          0: the request will wait until its response gets back
154  *	       from the firmware within LIO_SC_MAX_TMO_MS milli sec.
155  *	       It the response does not return within
156  *	       LIO_SC_MAX_TMO_MS milli sec, lio_process_ordered_list()
157  *	       will move the request to zombie response list.
158  *
159  * return value:
160  * 0: got the response from firmware for the sc request.
161  * errno -EINTR: user abort the command.
162  * errno -ETIME: user spefified timeout value has been expired.
163  * errno -EBUSY: the response of the request does not return in
164  *               resonable time (LIO_SC_MAX_TMO_MS).
165  *               the sc wll be move to zombie response list by
166  *               lio_process_ordered_list()
167  *
168  * A request with non-zero return value, the sc->caller_is_done
169  *  will be marked 1.
170  * When getting a request with zero return value, the requestor
171  *  should mark sc->caller_is_done with 1 after examing the
172  *  response of sc.
173  * lio_process_ordered_list() will free the soft command on behalf
174  * of the soft command requestor.
175  * This is to fix the possible race condition of both timeout process
176  * and lio_process_ordered_list()/callback function to free a
177  * sc strucutre.
178  */
179 static inline int
180 wait_for_sc_completion_timeout(struct octeon_device *oct_dev,
181 			       struct octeon_soft_command *sc,
182 			       unsigned long timeout)
183 {
184 	int errno = 0;
185 	long timeout_jiff;
186 
187 	if (timeout)
188 		timeout_jiff = msecs_to_jiffies(timeout);
189 	else
190 		timeout_jiff = MAX_SCHEDULE_TIMEOUT;
191 
192 	timeout_jiff =
193 		wait_for_completion_interruptible_timeout(&sc->complete,
194 							  timeout_jiff);
195 	if (timeout_jiff == 0) {
196 		dev_err(&oct_dev->pci_dev->dev, "%s: sc is timeout\n",
197 			__func__);
198 		WRITE_ONCE(sc->caller_is_done, true);
199 		errno = -ETIME;
200 	} else if (timeout_jiff == -ERESTARTSYS) {
201 		dev_err(&oct_dev->pci_dev->dev, "%s: sc is interrupted\n",
202 			__func__);
203 		WRITE_ONCE(sc->caller_is_done, true);
204 		errno = -EINTR;
205 	} else  if (sc->sc_status == OCTEON_REQUEST_TIMEOUT) {
206 		dev_err(&oct_dev->pci_dev->dev, "%s: sc has fatal timeout\n",
207 			__func__);
208 		WRITE_ONCE(sc->caller_is_done, true);
209 		errno = -EBUSY;
210 	}
211 
212 	return errno;
213 }
214 
215 #ifndef ROUNDUP4
216 #define ROUNDUP4(val) (((val) + 3) & 0xfffffffc)
217 #endif
218 
219 #ifndef ROUNDUP8
220 #define ROUNDUP8(val) (((val) + 7) & 0xfffffff8)
221 #endif
222 
223 #ifndef ROUNDUP16
224 #define ROUNDUP16(val) (((val) + 15) & 0xfffffff0)
225 #endif
226 
227 #ifndef ROUNDUP128
228 #define ROUNDUP128(val) (((val) + 127) & 0xffffff80)
229 #endif
230 
231 #endif /* _OCTEON_MAIN_H_ */
232