source: trunk/hal/tsar_mips32/drivers/soclib_nic.c @ 527

Last change on this file since 527 was 507, checked in by viala@…, 6 years ago

[soclib_nic] Missing default case fixed by adding an assert.

hal/tsar_mips32/drivers/soclib_nic.c: In function 'soclib_nic_cmd':
hal/tsar_mips32/drivers/soclib_nic.c:121:5: warning:

switch missing default case [-Wswitch-default]

switch( cmd )
~

File size: 11.0 KB
Line 
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 <spinlock.h>
30#include <kmem.h>
31#include <printk.h>
32#include <memcpy.h>
33#include <thread.h>
34#include <soclib_nic.h>
35
36///////////////////////////////////////
37void soclib_nic_init( chdev_t * chdev )
38{
39    uint32_t    i;
40    kmem_req_t  req;
41
42    // set driver specific fields in chdev descriptor
43    chdev->cmd = &soclib_nic_cmd;
44    chdev->isr = &soclib_nic_isr;
45   
46    // get hardware device cluster and local pointer
47    cxy_t      nic_cxy  = GET_CXY( chdev->base );
48    uint32_t * nic_ptr  = (uint32_t *)GET_PTR( chdev->base );
49
50    // initialize Soclib NIC global registers
51    hal_remote_sw( XPTR( nic_cxy , nic_ptr + NIC_GLOBAL_SPAN + NIC_G_BC_ENABLE ) , 0 );
52    hal_remote_sw( XPTR( nic_cxy , nic_ptr + NIC_GLOBAL_SPAN + NIC_G_RUN       ) , 0 );
53
54    // allocate memory for chbuf descriptor (one page)
55    assert( (sizeof(nic_chbuf_t) <= CONFIG_PPM_PAGE_SIZE ) ,
56            "chbuf descriptor exceeds one page" );
57
58    req.type   = KMEM_PAGE;
59    req.size   = 0;
60    req.flags  = AF_KERNEL;
61
62    nic_chbuf_t * chbuf = (nic_chbuf_t *)kmem_alloc( &req );
63
64    assert( (chbuf != NULL) ,
65             "cannot allocate chbuf descriptor" );
66
67    // initialise chbuf state
68    chbuf->cont_id  = 0;
69    chbuf->pkt_id   = 0;
70    chbuf->word_id  = 34;
71   
72    // allocate containers (one page per container)
73    // and complete chbuf descriptor initialization
74    assert( (CONFIG_PPM_PAGE_SIZE == 4096) ,
75            "chbuf container must be 4 Kbytes" );
76
77    for( i = 0 ; i < CONFIG_NIC_CHBUF_DEPTH ; i++ )
78    {
79        uint32_t * container = (uint32_t *)kmem_alloc( &req );   
80
81        assert( (container != NULL) ,
82                "cannot allocate container" );
83       
84        chbuf->cont[i] = container;
85        chbuf->full[i] = (paddr_t)XPTR( local_cxy , container );
86    }
87} // end soclib_nic_init()
88
89
90//////////////////////////////////////////////////////////////////
91void __attribute__ ((noinline)) soclib_nic_cmd( xptr_t thread_xp )
92{
93    uint32_t       cmd;          // command type   
94    char         * buffer;       // pointer on command buffer   
95    uint32_t       length;       // Ethernet packet length
96    xptr_t         dev_xp;       // extended pointer on NIC device
97    nic_chbuf_t  * chbuf;        // pointer on chbuf descriptor
98    uint32_t       cont_id;      // index of current container in chbuf
99    uint32_t       pkt_id;       // index of current packet in container
100    uint32_t       word_id;      // index of first word of current packet in container
101    uint32_t     * container;    // pointer on container (array of uint32_t)
102    uint16_t     * header;       // pointer on container header (array of uint16_t)
103    uint32_t       npackets;     // number of packets in current container
104
105    // get local pointer for client thread
106    thread_t * thread_ptr = (thread_t *)GET_PTR( thread_xp );
107
108    // get command arguments
109    cmd    = thread_ptr->nic_cmd.cmd;
110    buffer = thread_ptr->nic_cmd.buffer;
111    length = thread_ptr->nic_cmd.length;
112    dev_xp = thread_ptr->nic_cmd.dev_xp;
113
114    // get local pointer for device
115    chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
116
117    // get chbuf descriptor pointer
118    chbuf = (nic_chbuf_t *)dev_ptr->ext.nic.queue;
119
120    // analyse command type
121    switch( cmd )
122    {
123        /////////////////////////////////////////////////////////////////////////////
124        case NIC_CMD_READ:   // transfer one packet from RX queue to command buffer
125        {
126            // get current container index
127            cont_id = chbuf->cont_id;
128           
129            assert( chbuf->full[cont_id] == 0 , "Read an empty container\n" );
130
131            // get pointer on container and header
132            container = chbuf->cont[cont_id];
133            header    = (uint16_t *)container;
134
135            // get  expected packet index and first word index in container
136            pkt_id  = chbuf->pkt_id;
137            word_id = chbuf->word_id;
138
139            // get packet length and number of packets from container header
140            length    = header[pkt_id + 2];
141            npackets  = header[0];
142
143            assert( pkt_id >= npackets,
144                "Read a non readable container, packet index too large\n");
145
146            // move the packet from container to buffer
147            memcpy( buffer , container + word_id , length );
148
149            // update current packet index and first word index
150            chbuf->pkt_id  = pkt_id + 1;
151            if( length & 0x3 ) chbuf->word_id = word_id + (length>>2) + 1;
152            else               chbuf->word_id = word_id + (length>>2);
153        }
154        break;    // end READ
155           
156        //////////////////////////////////////////////////////////////////////////
157        case NIC_CMD_WRITE:  // move one packet from command buffer to TX queue
158        {
159            // get current TX container indexes
160            cont_id = chbuf->cont_id;
161            pkt_id  = chbuf->pkt_id;
162            word_id = chbuf->word_id;
163
164            assert( chbuf->full[cont_id] != 0, "Write to a full container\n" );
165
166            // get pointer on container and header
167            container = chbuf->cont[cont_id];
168            header    = (uint16_t *)container;
169
170            assert( length > ((1024 - word_id) << 2),
171                "Write to a non writable container, packet length too large\n");
172
173            // update packet length in container header
174            header[pkt_id + 2] = (uint16_t)length;
175
176            // move the packet from buffer to container
177            memcpy( container + word_id , buffer , length );
178
179            // update current packet index and first word index
180            chbuf->pkt_id  = pkt_id + 1;
181            if( length & 0x3 ) chbuf->word_id = word_id + (length>>2) + 1;
182            else               chbuf->word_id = word_id + (length>>2);
183        }
184        break;  // end WRITE
185
186        ////////////////////////////////////////////////////////////////////////////
187        case NIC_CMD_WRITABLE:  // analyse chbuf status / update status if required
188        {
189            // get current container state
190            cont_id = chbuf->cont_id;
191            word_id = chbuf->word_id;
192
193            // compute current container writable
194            bool_t ok = ( chbuf->full[cont_id] == 0 ) &&
195                        ( length <= ((1024 - word_id)<<2) );
196
197            if( ok )                // current container writable
198            {
199                // return chbuf writable
200                thread_ptr->nic_cmd.status = true;
201            }
202            else                    // current container not writable
203            {
204                // release current container
205                chbuf->full[cont_id] = 1;
206
207                // check next container
208                cont_id = (cont_id + 1) % CONFIG_NIC_CHBUF_DEPTH;
209
210                if( chbuf->full[cont_id] == 0 ) // next container empty
211                {
212                    // update chbuf status
213                    chbuf->word_id = 34;
214                    chbuf->cont_id = cont_id;
215                    chbuf->pkt_id  = 0;
216                     
217                    // return chbuf writable
218                    thread_ptr->nic_cmd.status = true;
219                }
220                else                            // next container full     
221                {
222                    // return chbuf non writable
223                    thread_ptr->nic_cmd.status = false;
224                }
225            }
226        }
227        break;  // end WRITABLE
228
229        /////////////////////////////////////////////////////////////////////////////
230        case NIC_CMD_READABLE:  // analyse chbuf status / update status if required
231        {
232            // get current container state
233            cont_id  = chbuf->cont_id;
234            pkt_id   = chbuf->pkt_id;
235            npackets = chbuf->cont[cont_id][0] & 0x0000FFFF; 
236           
237            // compute current container readable
238            bool_t ok = ( chbuf->full[cont_id] == 1 ) &&
239                        ( pkt_id < npackets );
240
241            if( ok )                    // current container readable
242            {
243                // return chbuf readable     
244                thread_ptr->nic_cmd.status = true;
245            }
246            else                        // current container non readable
247            {
248                // release current container
249                chbuf->full[cont_id] = 0;
250
251                // check next container
252                cont_id = (cont_id + 1) % CONFIG_NIC_CHBUF_DEPTH;
253
254                if( chbuf->full[cont_id] == 1 ) // next container full
255                {
256                    // update chbuf status
257                    chbuf->word_id = 34;
258                    chbuf->cont_id = cont_id;
259                    chbuf->pkt_id  = 0;
260                     
261                    // return chbuf readable
262                    thread_ptr->nic_cmd.status = true;
263                }
264                else                            // next container empty   
265                {
266                    // return chbuf non readable
267                    thread_ptr->nic_cmd.status = false;
268                }
269            }
270   
271        }
272        break;  // end READABLE
273        default: {
274            assert( false, "Unknown command <%x>\n", cmd );
275        }
276    }
277} // end soclib_nic_cmd()
278
279
280/////////////////////////////////////////////////////////////////
281void __attribute__ ((noinline)) soclib_nic_isr( chdev_t * chdev )
282{
283    // get base, size, channel, is_rx from NIC channel device NIC
284    xptr_t     base    = chdev->base;
285    uint32_t   channel = chdev->channel;
286    bool_t     is_rx   = chdev->is_rx;
287
288    // get NIC peripheral cluster and local pointer
289    cxy_t      cxy_nic = GET_CXY( base );
290    uint32_t * ptr_nic = (uint32_t *)GET_PTR( base );
291
292    // compute local pointer on status register
293    uint32_t * offset;
294    if( is_rx ) offset = ptr_nic + (NIC_CHANNEL_SPAN * (channel + 1)) + NIC_RX_STATUS;
295    else        offset = ptr_nic + (NIC_CHANNEL_SPAN * (channel + 1)) + NIC_TX_STATUS;
296
297    // read NIC channel status and acknowledge IRQ
298    uint32_t status = hal_remote_lw( XPTR( cxy_nic , offset ) );
299
300    assert( status != 0, "Illegal address: \n" );
301
302    // unblock server thread
303    thread_t * server = chdev->server;
304    thread_unblock( XPTR( local_cxy , server ) , THREAD_BLOCKED_IO );
305
306} // end soclib_nic_isr()
307
308
309
Note: See TracBrowser for help on using the repository browser.