Subversion Repositories HelenOS

Rev

Rev 4728 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup dp8390
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  \todo
  35.  */
  36.  
  37. #include <assert.h>
  38. #include <async.h>
  39. #include <ddi.h>
  40. #include <errno.h>
  41. #include <malloc.h>
  42. //#include <stdio.h>
  43. #include <ipc/ipc.h>
  44. #include <ipc/services.h>
  45.  
  46. #include "../../err.h"
  47. #include "../../messages.h"
  48. #include "../../modules.h"
  49.  
  50. #include "../../structures/packet/packet_client.h"
  51. #include "../../structures/measured_strings.h"
  52.  
  53. #include "../../include/device.h"
  54. #include "../../include/nil_interface.h"
  55.  
  56. #include "../netif.h"
  57. #include "../netif_module.h"
  58.  
  59. #include "dp8390.h"
  60. #include "dp8390_drv.h"
  61. #include "dp8390_port.h"
  62.  
  63. #define NAME    "dp8390 network interface"
  64.  
  65. #define IRQ_GET_DEVICE( call )          ( device_id_t ) IPC_GET_METHOD( * call )
  66. #define IPC_GET_ISR( call )             ( int ) IPC_GET_ARG2( * call )
  67.  
  68. static irq_cmd_t    dp8390_cmds[] = {
  69.     {   .cmd = CMD_PIO_READ_8,
  70.         .addr = NULL,
  71.         .dstarg = 2
  72.     },
  73.     {
  74.         .cmd = CMD_PREDICATE,
  75.         .value = 1,
  76.         .srcarg = 2
  77.     },
  78.     {
  79.         .cmd = CMD_ACCEPT
  80.     }
  81. };
  82.  
  83. static irq_code_t   dp8390_code = {
  84.     sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ),
  85.     dp8390_cmds
  86. };
  87.  
  88. netif_globals_t netif_globals;
  89.  
  90. void    module_print_name( void );
  91.  
  92. void    irq_handler( ipc_callid_t iid, ipc_call_t * call );
  93. int change_state( device_ref device, device_state_t state );
  94.  
  95. int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  96.     return ENOTSUP;
  97. }
  98.  
  99. int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
  100.     ERROR_DECLARE;
  101.  
  102.     device_ref      device;
  103.     eth_stat_t *    de_stat;
  104.  
  105.     if( ! stats ) return EBADMEM;
  106.     ERROR_PROPAGATE( find_device( device_id, & device ));
  107.     de_stat = & (( dpeth_t * ) device->specific )->de_stat;
  108.     null_device_stats( stats );
  109.     stats->receive_errors = de_stat->ets_recvErr;
  110.     stats->send_errors = de_stat->ets_sendErr;
  111.     stats->receive_crc_errors = de_stat->ets_CRCerr;
  112.     stats->receive_frame_errors = de_stat->ets_frameAll;
  113.     stats->receive_missed_errors = de_stat->ets_missedP;
  114.     stats->receive_packets = de_stat->ets_packetR;
  115.     stats->send_packets = de_stat->ets_packetT;
  116.     stats->collisions = de_stat->ets_collision;
  117.     stats->send_aborted_errors = de_stat->ets_transAb;
  118.     stats->send_carrier_errors = de_stat->ets_carrSense;
  119.     stats->send_heartbeat_errors = de_stat->ets_CDheartbeat;
  120.     stats->send_window_errors = de_stat->ets_OWC;
  121.     return EOK;
  122. }
  123.  
  124. void module_print_name( void ){
  125.     printf( "%s", NAME );
  126. }
  127.  
  128. int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
  129.     ERROR_DECLARE;
  130.  
  131.     device_ref  device;
  132.  
  133.     if( ! address ) return EBADMEM;
  134.     ERROR_PROPAGATE( find_device( device_id, & device ));
  135.     address->value = ( char * ) ( & (( dpeth_t * ) device->specific )->de_address );
  136.     address->length = CONVERT_SIZE( ether_addr_t, char, 1 );
  137.     return EOK;
  138. }
  139.  
  140. void irq_handler( ipc_callid_t iid, ipc_call_t * call )
  141. {
  142.     device_ref  device;
  143.     dpeth_t *   dep;
  144.     packet_t    received;
  145.     device_id_t device_id;
  146.     int         phone;
  147.  
  148.     device_id = IRQ_GET_DEVICE( call );
  149.     fibril_rwlock_write_lock( & netif_globals.lock );
  150.     if( find_device( device_id, & device ) != EOK ){
  151.         fibril_rwlock_write_unlock( & netif_globals.lock );
  152.         return;
  153.     }
  154.     dep = ( dpeth_t * ) device->specific;
  155.     if ( dep->de_mode != DEM_ENABLED){
  156.         fibril_rwlock_write_unlock( & netif_globals.lock );
  157.         return;
  158.     }
  159.     assert( dep->de_flags & DEF_ENABLED);
  160.     dep->de_int_pending = 0;
  161. //  TODO remove debug print:
  162.     printf( "I%d: 0x%x\n", device_id, IPC_GET_ISR( call ));
  163.     dp_check_ints( dep, IPC_GET_ISR( call ));
  164.     if( dep->received_queue ){
  165.         received = dep->received_queue;
  166.         phone = device->nil_phone;
  167.         dep->received_queue = NULL;
  168.         fibril_rwlock_write_unlock( & netif_globals.lock );
  169. //  TODO remove debug dump:
  170.     uint8_t *   data;
  171.     data = packet_get_data( received );
  172.     printf( "Receiving packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id( received ), packet_get_data_length( received ), data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ], data[ 16 ], data[ 17 ], data[ 18 ], data[ 19 ], data[ 20 ], data[ 21 ], data[ 22 ], data[ 23 ], data[ 24 ], data[ 25 ], data[ 26 ], data[ 27 ], data[ 28 ], data[ 29 ], data[ 30 ], data[ 31 ], data[ 32 ], data[ 33 ], data[ 34 ], data[ 35 ], data[ 36 ], data[ 37 ], data[ 38 ], data[ 39 ], data[ 40 ], data[ 41 ], data[ 42 ], data[ 43 ], data[ 44 ], data[ 45 ], data[ 46 ], data[ 47 ], data[ 48 ], data[ 49 ], data[ 50 ], data[ 51 ], data[ 52 ], data[ 53 ], data[ 54 ], data[ 55 ], data[ 56 ], data[ 57 ], data[ 58 ], data[ 59 ] );
  173.         nil_received_msg( phone, device_id, received, NULL );
  174.     }else{
  175.         fibril_rwlock_write_unlock( & netif_globals.lock );
  176.     }
  177.     ipc_answer_0( iid, EOK );
  178. }
  179.  
  180. int netif_probe_message( device_id_t device_id, int irq, int io ){
  181.     ERROR_DECLARE;
  182.  
  183.     device_ref  device;
  184.     dpeth_t *   dep;
  185.  
  186.     device = ( device_ref ) malloc( sizeof( device_t ));
  187.     if( ! device ) return ENOMEM;
  188.     dep = ( dpeth_t * ) malloc( sizeof( dpeth_t ));
  189.     if( ! dep ){
  190.         free( device );
  191.         return ENOMEM;
  192.     }
  193.     bzero( device, sizeof( device_t ));
  194.     bzero( dep, sizeof( dpeth_t ));
  195.     device->device_id = device_id;
  196.     device->nil_phone = -1;
  197.     device->specific = ( void * ) dep;
  198.     device->state = NETIF_STOPPED;
  199.     dep->de_irq = irq;
  200.     dep->de_mode = DEM_DISABLED;
  201.     //TODO address?
  202.     if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dep->de_base_port ))
  203.     || ERROR_OCCURRED( do_probe( dep ))){
  204.         free( dep );
  205.         free( device );
  206.         return ERROR_CODE;
  207.     }
  208.     if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){
  209.         free( dep );
  210.         free( device );
  211.         return ERROR_CODE;
  212.     }
  213.     return EOK;
  214. }
  215.  
  216. int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
  217.     ERROR_DECLARE;
  218.  
  219.     device_ref  device;
  220.     dpeth_t *   dep;
  221.     packet_t    next;
  222.  
  223.     ERROR_PROPAGATE( find_device( device_id, & device ));
  224.     if( device->state != NETIF_ACTIVE ){
  225.         netif_pq_release( packet_get_id( packet ));
  226.         return EFORWARD;
  227.     }
  228.     dep = ( dpeth_t * ) device->specific;
  229.     // process packet queue
  230.     do{
  231.         next = pq_detach( packet );
  232. //      TODO remove debug dump:
  233.         uint8_t *   data;
  234.         data = packet_get_data( packet );
  235.         printf( "Sending packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id( packet ), packet_get_data_length( packet ), data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ], data[ 16 ], data[ 17 ], data[ 18 ], data[ 19 ], data[ 20 ], data[ 21 ], data[ 22 ], data[ 23 ], data[ 24 ], data[ 25 ], data[ 26 ], data[ 27 ], data[ 28 ], data[ 29 ], data[ 30 ], data[ 31 ], data[ 32 ], data[ 33 ], data[ 34 ], data[ 35 ], data[ 36 ], data[ 37 ], data[ 38 ], data[ 39 ], data[ 40 ], data[ 41 ], data[ 42 ], data[ 43 ], data[ 44 ], data[ 45 ], data[ 46 ], data[ 47 ], data[ 48 ], data[ 49 ], data[ 50 ], data[ 51 ], data[ 52 ], data[ 53 ], data[ 54 ], data[ 55 ], data[ 56 ], data[ 57 ], data[ 58 ], data[ 59 ] );
  236.  
  237.         if( do_pwrite( dep, packet, FALSE ) != EBUSY ){
  238.             netif_pq_release( packet_get_id( packet ));
  239.         }
  240.         packet = next;
  241.     }while( packet );
  242.     return EOK;
  243. }
  244.  
  245. int netif_start_message( device_ref device ){
  246.     ERROR_DECLARE;
  247.  
  248.     dpeth_t *   dep;
  249.  
  250.     if( device->state != NETIF_ACTIVE ){
  251.         dep = ( dpeth_t * ) device->specific;
  252.         dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR );
  253.         dp8390_cmds[ 2 ].addr = dp8390_cmds[ 0 ].addr;
  254.         ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, device->device_id, & dp8390_code ));
  255.         if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){
  256.             ipc_unregister_irq( dep->de_irq, device->device_id );
  257.             return ERROR_CODE;
  258.         }
  259.         return change_state( device, NETIF_ACTIVE );
  260.     }
  261.     return EOK;
  262. }
  263.  
  264. int netif_stop_message( device_ref device ){
  265.     dpeth_t *   dep;
  266.  
  267.     if( device->state != NETIF_STOPPED ){
  268.         dep = ( dpeth_t * ) device->specific;
  269.         do_stop( dep );
  270.         ipc_unregister_irq( dep->de_irq, device->device_id );
  271.         return change_state( device, NETIF_STOPPED );
  272.     }
  273.     return EOK;
  274. }
  275.  
  276. int change_state( device_ref device, device_state_t state ){
  277.     device->state = state;
  278.     printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
  279.     return state;
  280. }
  281.  
  282. int netif_initialize( void ){
  283.     ipcarg_t    phonehash;
  284.  
  285.     async_set_interrupt_received( irq_handler );
  286.  
  287.     return REGISTER_ME( SERVICE_DP8390, & phonehash );
  288. }
  289.  
  290. /** @}
  291.  */
  292.