source: trunk/kernel/fs/devfs.c @ 432

Last change on this file since 432 was 430, checked in by alain, 6 years ago

blop

File size: 15.9 KB
Line 
1/*
2 * devfs.c - DEVFS File system API implementation.
3 *
4 * Author   Mohamed Lamine Karaoui (2014,2015)
5 *          Alain Greiner (2016,2017)
6 *
7 * Copyright (c) Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <hal_types.h>
26#include <hal_special.h>
27#include <hal_uspace.h>
28#include <printk.h>
29#include <chdev.h>
30#include <dev_txt.h>
31#include <cluster.h>
32#include <vfs.h>
33#include <kmem.h>
34#include <devfs.h>
35
36/////////////////////////////////////////////////////////////////////////////////////////
37//     Extern variables
38/////////////////////////////////////////////////////////////////////////////////////////
39
40extern vfs_ctx_t            fs_context[];   // allocated in kernel_init.c
41extern chdev_directory_t    chdev_dir;      // allocated in kernel_init.c
42
43#if CONFIG_READ_DEBUG
44extern uint32_t  enter_devfs_move;
45extern uint32_t  exit_devfs_move;
46#endif
47
48///////////////////////////////
49devfs_ctx_t * devfs_ctx_alloc()
50{
51    kmem_req_t    req;
52
53        req.type    = KMEM_DEVFS_CTX;
54        req.size    = sizeof(devfs_ctx_t);
55    req.flags   = AF_KERNEL | AF_ZERO;
56
57        return (devfs_ctx_t *)kmem_alloc( &req );
58}
59
60/////////////////////////////////////////////
61void devfs_ctx_init( devfs_ctx_t * devfs_ctx,
62                     xptr_t        devfs_dev_inode_xp,
63                     xptr_t        devfs_external_inode_xp )
64{
65    devfs_ctx->dev_inode_xp      = devfs_dev_inode_xp;
66    devfs_ctx->external_inode_xp = devfs_external_inode_xp;
67
68    fs_context[FS_TYPE_DEVFS].extend = devfs_ctx;
69}
70
71/////////////////////////////////////////////////
72void devfs_ctx_destroy( devfs_ctx_t * devfs_ctx )
73{
74    kmem_req_t    req;
75
76    req.type = KMEM_DEVFS_CTX;
77    req.ptr  = devfs_ctx;
78    kmem_free( &req );
79}
80
81///////////////////////////////////////////////////
82void devfs_global_init( xptr_t   parent_inode_xp,
83                        xptr_t * devfs_dev_inode_xp,
84                        xptr_t * devfs_external_inode_xp )
85{
86    error_t  error;
87
88    devfs_dmsg("\n[DBG] %s : enter in cluster %x\n",
89               __FUNCTION__ , local_cxy );
90
91    // creates DEVFS "dev" inode in cluster IO
92    error = vfs_add_child_in_parent( LOCAL_CLUSTER->io_cxy,
93                                     INODE_TYPE_DIR,
94                                     FS_TYPE_DEVFS,
95                                     parent_inode_xp,
96                                     "dev",
97                                     NULL,
98                                     devfs_dev_inode_xp ); 
99
100    assert( (error == 0) , __FUNCTION__ , "cannot create <dev>\n" );
101
102    devfs_dmsg("\n[DBG] %s : <dev> created in cluster %x\n",
103               __FUNCTION__ , local_cxy );
104
105    // create DEVFS "external" inode in cluster IO
106    error = vfs_add_child_in_parent( LOCAL_CLUSTER->io_cxy,
107                                     INODE_TYPE_DIR,
108                                     FS_TYPE_DEVFS,
109                                     *devfs_dev_inode_xp,
110                                     "external",
111                                     NULL,
112                                     devfs_external_inode_xp ); 
113
114    assert( (error == 0) , __FUNCTION__ , "cannot create <external>\n" );
115
116    devfs_dmsg("\n[DBG] %s : <external> created in cluster %x\n",
117               __FUNCTION__ , local_cxy );
118} 
119
120///////////////////////////////////////////////////
121void devfs_local_init( xptr_t   devfs_dev_inode_xp,
122                       xptr_t   devfs_external_inode_xp,
123                       xptr_t * devfs_internal_inode_xp )
124{
125    char          node_name[16];
126    xptr_t        chdev_xp;
127    cxy_t         chdev_cxy;
128    chdev_t     * chdev_ptr;
129    xptr_t        inode_xp;
130    uint32_t      channel;
131
132    // create "internal" directory linked to "dev"
133    snprintf( node_name , 16 , "internal_%x" , local_cxy );
134    vfs_add_child_in_parent( local_cxy,
135                             INODE_TYPE_DIR,
136                             FS_TYPE_DEVFS,
137                             devfs_dev_inode_xp,
138                             node_name,
139                             NULL,
140                             devfs_internal_inode_xp );
141
142devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
143__FUNCTION__ , node_name , local_cxy );
144
145    // create MMC chdev inode
146    chdev_xp  = chdev_dir.mmc[local_cxy];
147    if( chdev_xp != XPTR_NULL)
148    {
149        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
150        vfs_add_child_in_parent( local_cxy,
151                                 INODE_TYPE_DEV,
152                                 FS_TYPE_DEVFS,
153                                 *devfs_internal_inode_xp,
154                                 chdev_ptr->name,
155                                 GET_PTR( chdev_xp ),
156                                 &inode_xp );
157
158devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
159__FUNCTION__ , chdev_ptr->name , local_cxy );
160
161    }
162
163    // create DMA chdev inodes (one DMA channel per core)
164    for( channel = 0 ; channel < LOCAL_CLUSTER->cores_nr ; channel++ )
165    {
166        chdev_xp = chdev_dir.dma[channel];
167        if( chdev_xp != XPTR_NULL)
168        {
169            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
170            vfs_add_child_in_parent( local_cxy,
171                                     INODE_TYPE_DEV,
172                                     FS_TYPE_DEVFS,
173                                     *devfs_internal_inode_xp,
174                                     chdev_ptr->name,
175                                     GET_PTR( chdev_xp ),
176                                     &inode_xp );
177
178devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
179__FUNCTION__ , chdev_ptr->name , local_cxy );
180
181        }
182    }
183
184    // create an IOB inode in cluster containing IOB chdev
185    chdev_xp = chdev_dir.iob;
186    if( chdev_xp != XPTR_NULL )
187    {
188        chdev_cxy = GET_CXY( chdev_xp );
189        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
190        if( chdev_cxy == local_cxy )
191        {
192            vfs_add_child_in_parent( local_cxy,
193                                     INODE_TYPE_DEV,
194                                     FS_TYPE_DEVFS,
195                                     devfs_external_inode_xp,
196                                     chdev_ptr->name,
197                                     GET_PTR( chdev_xp ),
198                                     &inode_xp );
199
200devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
201__FUNCTION__ , chdev_ptr->name , local_cxy );
202
203        }
204    }
205       
206    // create a PIC inode in cluster containing PIC chdev
207    chdev_xp = chdev_dir.pic;
208    if( chdev_xp != XPTR_NULL )
209    {
210        chdev_cxy = GET_CXY( chdev_xp );
211        chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
212        if( chdev_cxy == local_cxy )
213        {
214            vfs_add_child_in_parent( local_cxy,
215                                     INODE_TYPE_DEV,
216                                     FS_TYPE_DEVFS,
217                                     devfs_external_inode_xp,
218                                     chdev_ptr->name,
219                                     GET_PTR( chdev_xp ),
220                                     &inode_xp );
221
222devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
223__FUNCTION__ , chdev_ptr->name , local_cxy );
224
225        }
226    }
227
228    // create a TXT_RX inode in each cluster containing a TXT_RX chdev
229    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
230    {
231        chdev_xp = chdev_dir.txt_rx[channel];
232        if( chdev_xp != XPTR_NULL )
233        {
234            chdev_cxy = GET_CXY( chdev_xp );
235            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
236            if( chdev_cxy == local_cxy )
237            {
238                vfs_add_child_in_parent( local_cxy,
239                                         INODE_TYPE_DEV,
240                                         FS_TYPE_DEVFS,
241                                         devfs_external_inode_xp,
242                                         chdev_ptr->name,
243                                         GET_PTR( chdev_xp ),
244                                         &inode_xp );
245
246devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
247__FUNCTION__ , chdev_ptr->name , local_cxy );
248
249            }
250        }
251    }
252
253    // create a TXT_TX inode in each cluster containing a TXT_TX chdev
254    for( channel = 0 ; channel < CONFIG_MAX_TXT_CHANNELS ; channel++ )
255    {
256        chdev_xp = chdev_dir.txt_tx[channel];
257        if( chdev_xp != XPTR_NULL )
258        {
259            chdev_cxy = GET_CXY( chdev_xp );
260            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
261            if( chdev_cxy == local_cxy )
262            {
263                vfs_add_child_in_parent( local_cxy,
264                                         INODE_TYPE_DEV,
265                                         FS_TYPE_DEVFS,
266                                         devfs_external_inode_xp,
267                                         chdev_ptr->name,
268                                         GET_PTR( chdev_xp ),
269                                         &inode_xp );
270
271devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
272__FUNCTION__ , chdev_ptr->name , local_cxy );
273
274            }
275        }
276    }
277
278    // create an IOC inode in each cluster containing an IOC chdev
279    for( channel = 0 ; channel < CONFIG_MAX_IOC_CHANNELS ; channel++ )
280    {
281        chdev_xp = chdev_dir.ioc[channel];
282        if( chdev_xp != XPTR_NULL )
283        {
284            chdev_cxy = GET_CXY( chdev_xp );
285            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
286            if( chdev_cxy == local_cxy )
287            {
288                vfs_add_child_in_parent( local_cxy,
289                                         INODE_TYPE_DEV,
290                                         FS_TYPE_DEVFS,
291                                         devfs_external_inode_xp,
292                                         chdev_ptr->name,
293                                         GET_PTR( chdev_xp ),
294                                         &inode_xp );
295
296devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
297__FUNCTION__ , chdev_ptr->name , local_cxy );
298
299            }
300        }
301    }
302
303    // create a FBF inode in each cluster containing a FBF chdev
304    for( channel = 0 ; channel < CONFIG_MAX_IOC_CHANNELS ; channel++ )
305    {
306        chdev_xp = chdev_dir.fbf[channel];
307        if( chdev_xp != XPTR_NULL )
308        {
309            chdev_cxy = GET_CXY( chdev_xp );
310            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
311            if( chdev_cxy == local_cxy )
312            {
313                vfs_add_child_in_parent( local_cxy,
314                                         INODE_TYPE_DEV,
315                                         FS_TYPE_DEVFS,
316                                         devfs_external_inode_xp,
317                                         chdev_ptr->name,
318                                         GET_PTR( chdev_xp ),
319                                         &inode_xp );
320
321devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
322__FUNCTION__ , chdev_ptr->name , local_cxy );
323
324            }
325        }
326    }
327
328    // create a NIC_RX inode in each cluster containing a NIC_RX chdev
329    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
330    {
331        chdev_xp = chdev_dir.nic_rx[channel];
332        if( chdev_xp != XPTR_NULL )
333        {
334            chdev_cxy = GET_CXY( chdev_xp );
335            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
336            if( chdev_cxy == local_cxy )
337            {
338                vfs_add_child_in_parent( local_cxy,
339                                         INODE_TYPE_DEV,
340                                         FS_TYPE_DEVFS,
341                                         devfs_external_inode_xp,
342                                         chdev_ptr->name,
343                                         GET_PTR( chdev_xp ),
344                                         &inode_xp );
345
346devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
347__FUNCTION__ , chdev_ptr->name , local_cxy );
348
349            }
350        }
351    }
352
353    // create a NIC_TX inode in each cluster containing a NIC_TX chdev
354    for( channel = 0 ; channel < CONFIG_MAX_NIC_CHANNELS ; channel++ )
355    {
356        chdev_xp = chdev_dir.nic_tx[channel];
357        if( chdev_xp != XPTR_NULL )
358        {
359            chdev_cxy = GET_CXY( chdev_xp );
360            chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
361            if( chdev_cxy == local_cxy )
362            {
363                vfs_add_child_in_parent( local_cxy,
364                                         INODE_TYPE_DEV,
365                                         FS_TYPE_DEVFS,
366                                         devfs_external_inode_xp,
367                                         chdev_ptr->name,
368                                         GET_PTR( chdev_xp ),
369                                         &inode_xp );
370
371devfs_dmsg("\n[DBG] %s : created <%s> inode in cluster %x\n",
372__FUNCTION__ , chdev_ptr->name , local_cxy );
373
374            }
375        }
376    }
377}  // end devfs_local_init()
378
379//////////////////////////////////////////
380int devfs_user_move( bool_t     to_buffer,
381                     xptr_t     file_xp,
382                     void     * u_buf,
383                     uint32_t   size )
384{
385    assert( ( file_xp != XPTR_NULL ) , __FUNCTION__ , "file_xp == XPTR_NULL" );
386
387    assert( ( size < CONFIG_TXT_KBUF_SIZE ) , __FUNCTION__ , "string size too large" );
388
389    xptr_t             chdev_xp;
390    cxy_t              chdev_cxy;
391    chdev_t          * chdev_ptr;    // associated chdev type
392    uint32_t           func;         // chdev functionnal type
393    uint32_t           channel;      // chdev channel index
394    error_t            error;
395
396    char               k_buf[CONFIG_TXT_KBUF_SIZE];  // local kernel buffer
397
398devfs_dmsg("\n[DBG] %s enter / cycle %d\n", 
399__FUNCTION__ , hal_time_stamp() );
400
401#if CONFIG_READ_DEBUG
402enter_devfs_move = hal_time_stamp();
403#endif
404
405    // get extended pointer on chdev_xp
406    chdev_xp = chdev_from_file( file_xp );
407
408    // get cluster and local pointer on chdev
409    chdev_cxy  = GET_CXY( chdev_xp );
410    chdev_ptr  = (chdev_t *)GET_PTR( chdev_xp );
411
412    // get chdev functionnal type and channel
413    func    = hal_remote_lw( XPTR( chdev_cxy , &chdev_ptr->func ) );
414    channel = hal_remote_lw( XPTR( chdev_cxy , &chdev_ptr->channel ) );
415
416    // action depends on "func" and "to_buffer"
417    if( func == DEV_FUNC_TXT )
418    {
419        if( to_buffer )     // TXT read
420        { 
421            uint32_t i;
422            for( i = 0 ; i < size ; i++ )
423            {
424                error = dev_txt_read( channel , &k_buf[i] );
425
426                if( error ) 
427                {
428
429devfs_dmsg("\n[DBG] %s exit error / cycle %d\n", 
430__FUNCTION__ , hal_time_stamp() );
431
432                    return -1;
433                }
434                else
435                {
436                    hal_strcpy_to_uspace( u_buf , k_buf , size );
437                }
438             }
439
440#if CONFIG_READ_DEBUG
441exit_devfs_move = hal_time_stamp();
442#endif
443
444devfs_dmsg("\n[DBG] %s exit success / size = %d / cycle %d\n", 
445__FUNCTION__ , size , hal_time_stamp() );
446
447            return size;
448        } 
449        else                // TXT write 
450        {
451            hal_strcpy_from_uspace( k_buf , u_buf , size );
452
453            error = dev_txt_write( channel , k_buf , size );
454            if( error ) 
455            {
456
457devfs_dmsg("\n[DBG] %s exit error / cycle %d\n", 
458__FUNCTION__ , hal_time_stamp() );
459
460                return -1;
461            }
462            else
463            {
464
465devfs_dmsg("\n[DBG] %s exit success / size = %d / cycle %d\n", 
466__FUNCTION__ , size , hal_time_stamp() );
467
468                return size;
469            }
470        }
471    }
472    else
473    {
474        assert( false , __FUNCTION__ ,
475        "%s does not support direct user access", chdev_func_str(func) );
476
477        return -1;
478    }
479}  // end devfs_user_move()
480
481
Note: See TracBrowser for help on using the repository browser.