1 | /* |
---|
2 | * soclib_nic.c - SOCLIB_NIC (Network Interface Controler) driver implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH.is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH.is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_kernel_types.h> |
---|
25 | #include <hal_remote.h> |
---|
26 | #include <hal_special.h> |
---|
27 | #include <chdev.h> |
---|
28 | #include <dev_nic.h> |
---|
29 | #include <kmem.h> |
---|
30 | #include <printk.h> |
---|
31 | #include <memcpy.h> |
---|
32 | #include <thread.h> |
---|
33 | #include <soclib_nic.h> |
---|
34 | |
---|
35 | /////////////////////////////////////// |
---|
36 | void soclib_nic_init( chdev_t * chdev ) |
---|
37 | { |
---|
38 | uint32_t i; |
---|
39 | kmem_req_t req; |
---|
40 | |
---|
41 | // set driver specific fields in chdev descriptor |
---|
42 | chdev->cmd = &soclib_nic_cmd; |
---|
43 | chdev->isr = &soclib_nic_isr; |
---|
44 | |
---|
45 | // get hardware device cluster and local pointer |
---|
46 | cxy_t nic_cxy = GET_CXY( chdev->base ); |
---|
47 | uint32_t * nic_ptr = (uint32_t *)GET_PTR( chdev->base ); |
---|
48 | |
---|
49 | // initialize Soclib NIC global registers |
---|
50 | hal_remote_s32( XPTR( nic_cxy , nic_ptr + NIC_GLOBAL_SPAN + NIC_G_BC_ENABLE ) , 0 ); |
---|
51 | hal_remote_s32( XPTR( nic_cxy , nic_ptr + NIC_GLOBAL_SPAN + NIC_G_RUN ) , 0 ); |
---|
52 | |
---|
53 | // allocate memory for chbuf descriptor (one page) |
---|
54 | assert( (sizeof(nic_chbuf_t) <= CONFIG_PPM_PAGE_SIZE ) , |
---|
55 | "chbuf descriptor exceeds one page" ); |
---|
56 | |
---|
57 | req.type = KMEM_PAGE; |
---|
58 | req.size = 0; |
---|
59 | req.flags = AF_KERNEL; |
---|
60 | |
---|
61 | nic_chbuf_t * chbuf = (nic_chbuf_t *)kmem_alloc( &req ); |
---|
62 | |
---|
63 | assert( (chbuf != NULL) , |
---|
64 | "cannot allocate chbuf descriptor" ); |
---|
65 | |
---|
66 | // initialise chbuf state |
---|
67 | chbuf->cont_id = 0; |
---|
68 | chbuf->pkt_id = 0; |
---|
69 | chbuf->word_id = 34; |
---|
70 | |
---|
71 | // allocate containers (one page per container) |
---|
72 | // and complete chbuf descriptor initialization |
---|
73 | assert( (CONFIG_PPM_PAGE_SIZE == 4096) , |
---|
74 | "chbuf container must be 4 Kbytes" ); |
---|
75 | |
---|
76 | for( i = 0 ; i < CONFIG_NIC_CHBUF_DEPTH ; i++ ) |
---|
77 | { |
---|
78 | uint32_t * container = (uint32_t *)kmem_alloc( &req ); |
---|
79 | |
---|
80 | assert( (container != NULL) , |
---|
81 | "cannot allocate container" ); |
---|
82 | |
---|
83 | chbuf->cont[i] = container; |
---|
84 | chbuf->full[i] = (paddr_t)XPTR( local_cxy , container ); |
---|
85 | } |
---|
86 | } // end soclib_nic_init() |
---|
87 | |
---|
88 | |
---|
89 | ////////////////////////////////////////////////////////////////// |
---|
90 | void __attribute__ ((noinline)) soclib_nic_cmd( xptr_t thread_xp ) |
---|
91 | { |
---|
92 | uint32_t cmd; // command type |
---|
93 | char * buffer; // pointer on command buffer |
---|
94 | uint32_t length; // Ethernet packet length |
---|
95 | xptr_t dev_xp; // extended pointer on NIC device |
---|
96 | nic_chbuf_t * chbuf; // pointer on chbuf descriptor |
---|
97 | uint32_t cont_id; // index of current container in chbuf |
---|
98 | uint32_t pkt_id; // index of current packet in container |
---|
99 | uint32_t word_id; // index of first word of current packet in container |
---|
100 | uint32_t * container; // pointer on container (array of uint32_t) |
---|
101 | uint16_t * header; // pointer on container header (array of uint16_t) |
---|
102 | uint32_t npackets; // number of packets in current container |
---|
103 | |
---|
104 | // get local pointer for client thread |
---|
105 | thread_t * thread_ptr = (thread_t *)GET_PTR( thread_xp ); |
---|
106 | |
---|
107 | // get command arguments |
---|
108 | cmd = thread_ptr->nic_cmd.cmd; |
---|
109 | buffer = thread_ptr->nic_cmd.buffer; |
---|
110 | length = thread_ptr->nic_cmd.length; |
---|
111 | dev_xp = thread_ptr->nic_cmd.dev_xp; |
---|
112 | |
---|
113 | // get local pointer for device |
---|
114 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
115 | |
---|
116 | // get chbuf descriptor pointer |
---|
117 | chbuf = (nic_chbuf_t *)dev_ptr->ext.nic.queue; |
---|
118 | |
---|
119 | // analyse command type |
---|
120 | switch( cmd ) |
---|
121 | { |
---|
122 | ///////////////////////////////////////////////////////////////////////////// |
---|
123 | case NIC_CMD_READ: // transfer one packet from RX queue to command buffer |
---|
124 | { |
---|
125 | // get current container index |
---|
126 | cont_id = chbuf->cont_id; |
---|
127 | |
---|
128 | assert( chbuf->full[cont_id] == 0 , "Read an empty container\n" ); |
---|
129 | |
---|
130 | // get pointer on container and header |
---|
131 | container = chbuf->cont[cont_id]; |
---|
132 | header = (uint16_t *)container; |
---|
133 | |
---|
134 | // get expected packet index and first word index in container |
---|
135 | pkt_id = chbuf->pkt_id; |
---|
136 | word_id = chbuf->word_id; |
---|
137 | |
---|
138 | // get packet length and number of packets from container header |
---|
139 | length = header[pkt_id + 2]; |
---|
140 | npackets = header[0]; |
---|
141 | |
---|
142 | assert( pkt_id >= npackets, |
---|
143 | "Read a non readable container, packet index too large\n"); |
---|
144 | |
---|
145 | // move the packet from container to buffer |
---|
146 | memcpy( buffer , container + word_id , length ); |
---|
147 | |
---|
148 | // update current packet index and first word index |
---|
149 | chbuf->pkt_id = pkt_id + 1; |
---|
150 | if( length & 0x3 ) chbuf->word_id = word_id + (length>>2) + 1; |
---|
151 | else chbuf->word_id = word_id + (length>>2); |
---|
152 | } |
---|
153 | break; // end READ |
---|
154 | |
---|
155 | ////////////////////////////////////////////////////////////////////////// |
---|
156 | case NIC_CMD_WRITE: // move one packet from command buffer to TX queue |
---|
157 | { |
---|
158 | // get current TX container indexes |
---|
159 | cont_id = chbuf->cont_id; |
---|
160 | pkt_id = chbuf->pkt_id; |
---|
161 | word_id = chbuf->word_id; |
---|
162 | |
---|
163 | assert( chbuf->full[cont_id] != 0, "Write to a full container\n" ); |
---|
164 | |
---|
165 | // get pointer on container and header |
---|
166 | container = chbuf->cont[cont_id]; |
---|
167 | header = (uint16_t *)container; |
---|
168 | |
---|
169 | assert( length > ((1024 - word_id) << 2), |
---|
170 | "Write to a non writable container, packet length too large\n"); |
---|
171 | |
---|
172 | // update packet length in container header |
---|
173 | header[pkt_id + 2] = (uint16_t)length; |
---|
174 | |
---|
175 | // move the packet from buffer to container |
---|
176 | memcpy( container + word_id , buffer , length ); |
---|
177 | |
---|
178 | // update current packet index and first word index |
---|
179 | chbuf->pkt_id = pkt_id + 1; |
---|
180 | if( length & 0x3 ) chbuf->word_id = word_id + (length>>2) + 1; |
---|
181 | else chbuf->word_id = word_id + (length>>2); |
---|
182 | } |
---|
183 | break; // end WRITE |
---|
184 | |
---|
185 | //////////////////////////////////////////////////////////////////////////// |
---|
186 | case NIC_CMD_WRITABLE: // analyse chbuf status / update status if required |
---|
187 | { |
---|
188 | // get current container state |
---|
189 | cont_id = chbuf->cont_id; |
---|
190 | word_id = chbuf->word_id; |
---|
191 | |
---|
192 | // compute current container writable |
---|
193 | bool_t ok = ( chbuf->full[cont_id] == 0 ) && |
---|
194 | ( length <= ((1024 - word_id)<<2) ); |
---|
195 | |
---|
196 | if( ok ) // current container writable |
---|
197 | { |
---|
198 | // return chbuf writable |
---|
199 | thread_ptr->nic_cmd.status = true; |
---|
200 | } |
---|
201 | else // current container not writable |
---|
202 | { |
---|
203 | // release current container |
---|
204 | chbuf->full[cont_id] = 1; |
---|
205 | |
---|
206 | // check next container |
---|
207 | cont_id = (cont_id + 1) % CONFIG_NIC_CHBUF_DEPTH; |
---|
208 | |
---|
209 | if( chbuf->full[cont_id] == 0 ) // next container empty |
---|
210 | { |
---|
211 | // update chbuf status |
---|
212 | chbuf->word_id = 34; |
---|
213 | chbuf->cont_id = cont_id; |
---|
214 | chbuf->pkt_id = 0; |
---|
215 | |
---|
216 | // return chbuf writable |
---|
217 | thread_ptr->nic_cmd.status = true; |
---|
218 | } |
---|
219 | else // next container full |
---|
220 | { |
---|
221 | // return chbuf non writable |
---|
222 | thread_ptr->nic_cmd.status = false; |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | break; // end WRITABLE |
---|
227 | |
---|
228 | ///////////////////////////////////////////////////////////////////////////// |
---|
229 | case NIC_CMD_READABLE: // analyse chbuf status / update status if required |
---|
230 | { |
---|
231 | // get current container state |
---|
232 | cont_id = chbuf->cont_id; |
---|
233 | pkt_id = chbuf->pkt_id; |
---|
234 | npackets = chbuf->cont[cont_id][0] & 0x0000FFFF; |
---|
235 | |
---|
236 | // compute current container readable |
---|
237 | bool_t ok = ( chbuf->full[cont_id] == 1 ) && |
---|
238 | ( pkt_id < npackets ); |
---|
239 | |
---|
240 | if( ok ) // current container readable |
---|
241 | { |
---|
242 | // return chbuf readable |
---|
243 | thread_ptr->nic_cmd.status = true; |
---|
244 | } |
---|
245 | else // current container non readable |
---|
246 | { |
---|
247 | // release current container |
---|
248 | chbuf->full[cont_id] = 0; |
---|
249 | |
---|
250 | // check next container |
---|
251 | cont_id = (cont_id + 1) % CONFIG_NIC_CHBUF_DEPTH; |
---|
252 | |
---|
253 | if( chbuf->full[cont_id] == 1 ) // next container full |
---|
254 | { |
---|
255 | // update chbuf status |
---|
256 | chbuf->word_id = 34; |
---|
257 | chbuf->cont_id = cont_id; |
---|
258 | chbuf->pkt_id = 0; |
---|
259 | |
---|
260 | // return chbuf readable |
---|
261 | thread_ptr->nic_cmd.status = true; |
---|
262 | } |
---|
263 | else // next container empty |
---|
264 | { |
---|
265 | // return chbuf non readable |
---|
266 | thread_ptr->nic_cmd.status = false; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | } |
---|
271 | break; // end READABLE |
---|
272 | default: { |
---|
273 | assert( false, "Unknown command <%x>\n", cmd ); |
---|
274 | } |
---|
275 | } |
---|
276 | } // end soclib_nic_cmd() |
---|
277 | |
---|
278 | |
---|
279 | ///////////////////////////////////////////////////////////////// |
---|
280 | void __attribute__ ((noinline)) soclib_nic_isr( chdev_t * chdev ) |
---|
281 | { |
---|
282 | // get base, size, channel, is_rx from NIC channel device NIC |
---|
283 | xptr_t base = chdev->base; |
---|
284 | uint32_t channel = chdev->channel; |
---|
285 | bool_t is_rx = chdev->is_rx; |
---|
286 | |
---|
287 | // get NIC peripheral cluster and local pointer |
---|
288 | cxy_t cxy_nic = GET_CXY( base ); |
---|
289 | uint32_t * ptr_nic = (uint32_t *)GET_PTR( base ); |
---|
290 | |
---|
291 | // compute local pointer on status register |
---|
292 | uint32_t * offset; |
---|
293 | if( is_rx ) offset = ptr_nic + (NIC_CHANNEL_SPAN * (channel + 1)) + NIC_RX_STATUS; |
---|
294 | else offset = ptr_nic + (NIC_CHANNEL_SPAN * (channel + 1)) + NIC_TX_STATUS; |
---|
295 | |
---|
296 | // read NIC channel status and acknowledge IRQ |
---|
297 | uint32_t status = hal_remote_l32( XPTR( cxy_nic , offset ) ); |
---|
298 | |
---|
299 | assert( status != 0, "Illegal address: \n" ); |
---|
300 | |
---|
301 | // unblock server thread |
---|
302 | thread_t * server = chdev->server; |
---|
303 | thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_IO ); |
---|
304 | |
---|
305 | } // end soclib_nic_isr() |
---|
306 | |
---|
307 | |
---|
308 | |
---|