Subversion Repositories HelenOS

Rev

Rev 4307 | Rev 4332 | 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.  */
  35.  
  36. #include <assert.h>
  37. #include <async.h>
  38. #include <ddi.h>
  39. #include <errno.h>
  40. #include <malloc.h>
  41. //#include <stdio.h>
  42. #include <ipc/ipc.h>
  43. #include <ipc/services.h>
  44.  
  45. #include "../../err.h"
  46. #include "../../messages.h"
  47. #include "../../modules.h"
  48.  
  49. #include "../../structures/packet/packet_client.h"
  50. #include "../../structures/measured_strings.h"
  51.  
  52. #include "../../include/device.h"
  53. #include "../../include/nil_interface.h"
  54.  
  55. #include "../netif.h"
  56. #include "../netif_module.h"
  57.  
  58. #include "dp8390.h"
  59. #include "dp8390_drv.h"
  60. #include "dp8390_port.h"
  61.  
  62. #define NAME    "dp8390 network interface"
  63.  
  64. #define IRQ_GET_DEVICE( call )          ( device_id_t ) IPC_GET_METHOD( * call )
  65. #define IPC_GET_ISR( call )             ( int ) IPC_GET_ARG2( * call )
  66.  
  67. static irq_cmd_t    dp8390_cmds[] = {
  68.     {   .cmd = CMD_PIO_READ_8,
  69.         .addr = NULL,
  70.         .dstarg = 2
  71.     },
  72.     {
  73.         .cmd = CMD_PREDICATE,
  74.         .value = 1,
  75.         .srcarg = 2
  76.     },
  77. /*  {   .cmd = CMD_PIO_WRITE_8,
  78.         .addr = NULL,
  79.         .value = 0
  80.     },
  81. */  {
  82.         .cmd = CMD_ACCEPT
  83.     }
  84. };
  85.  
  86. static irq_code_t   dp8390_code = {
  87.     sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ),
  88.     dp8390_cmds
  89. };
  90.  
  91. netif_globals_t netif_globals;
  92.  
  93. void    module_print_name( void );
  94.  
  95. void    irq_handler( ipc_callid_t iid, ipc_call_t * call );
  96. int change_state( device_ref device, device_state_t state );
  97.  
  98. int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  99.     return ENOTSUP;
  100. }
  101.  
  102. int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
  103.     ERROR_DECLARE;
  104.  
  105.     device_ref      device;
  106.     eth_stat_t *    de_stat;
  107.  
  108.     if( ! stats ) return EBADMEM;
  109.     ERROR_PROPAGATE( find_device( device_id, & device ));
  110.     de_stat = & (( dpeth_t * ) device->specific )->de_stat;
  111.     null_device_stats( stats );
  112.     stats->rx_errors = de_stat->ets_recvErr;
  113.     stats->tx_errors = de_stat->ets_sendErr;
  114.     stats->rx_crc_errors = de_stat->ets_CRCerr;
  115.     stats->rx_frame_errors = de_stat->ets_frameAll;
  116.     stats->rx_missed_errors = de_stat->ets_missedP;
  117.     stats->rx_packets = de_stat->ets_packetR;
  118.     stats->tx_packets = de_stat->ets_packetT;
  119.     stats->collisions = de_stat->ets_collision;
  120.     stats->tx_aborted_errors = de_stat->ets_transAb;
  121.     stats->tx_carrier_errors = de_stat->ets_carrSense;
  122.     stats->tx_heartbeat_errors = de_stat->ets_CDheartbeat;
  123.     stats->tx_window_errors = de_stat->ets_OWC;
  124.     return EOK;
  125. }
  126.  
  127. void module_print_name( void ){
  128.     printf( "%s", NAME );
  129. }
  130.  
  131. int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
  132.     ERROR_DECLARE;
  133.  
  134.     device_ref  device;
  135.  
  136.     if( ! address ) return EBADMEM;
  137.     ERROR_PROPAGATE( find_device( device_id, & device ));
  138.     address->value = ( char * ) ( & (( dpeth_t * ) device->specific )->de_address );
  139.     address->length = CONVERT_SIZE( ether_addr_t, char, 1 );
  140.     return EOK;
  141. }
  142.  
  143. void irq_handler( ipc_callid_t iid, ipc_call_t * call )
  144. {
  145.     device_ref  device;
  146.     dpeth_t *   dep;
  147.     packet_t    received;
  148.     device_id_t device_id;
  149.     int         phone;
  150.  
  151. //  async_serialize_start();
  152.     device_id = IRQ_GET_DEVICE( call );
  153.     rwlock_write_lock( & netif_globals.lock );
  154.     if( find_device( device_id, & device ) != EOK ){
  155.         rwlock_write_unlock( & netif_globals.lock );
  156. //      async_serialize_end()
  157.         return;
  158.     }
  159.     dep = ( dpeth_t * ) device->specific;
  160.     if ( dep->de_mode != DEM_ENABLED){
  161.         rwlock_write_unlock( & netif_globals.lock );
  162. //      async_serialize_end()
  163.         return;
  164.     }
  165.     assert( dep->de_flags & DEF_ENABLED);
  166.     dep->de_int_pending= 0;
  167.     printf( "I%d -%d\n", device_id, IPC_GET_ISR( call ));
  168. /*  putchar( 'I' );
  169.     putchar( '0' + device_id );
  170.     putchar( '-' );
  171.     putchar( '0' + IPC_GET_ISR( call ) / 100 );
  172.     putchar( '0' + ( IPC_GET_ISR( call ) % 100 ) / 10 );
  173.     putchar( '0' + IPC_GET_ISR( call ) % 10 );
  174.     putchar( '\n' );
  175. */  dp_check_ints( dep, IPC_GET_ISR( call ));
  176.     if( dep->received_queue ){
  177.         received = dep->received_queue;
  178.         phone = device->nil_phone;
  179.         dep->received_queue = NULL;
  180.         rwlock_write_unlock( & netif_globals.lock );
  181.         nil_received_msg( phone, device_id, received, NULL );
  182.     }else{
  183.         rwlock_write_unlock( & netif_globals.lock );
  184.     }
  185.     ipc_answer_0( iid, EOK );
  186. //      async_serialize_end()
  187. }
  188.  
  189. int netif_probe_auto_message( void ){
  190.     return ENOTSUP;
  191. }
  192.  
  193. int netif_probe_message( device_id_t device_id, int irq, int io ){
  194.     ERROR_DECLARE;
  195.  
  196.     device_ref  device;
  197.     dpeth_t *   dep;
  198.  
  199.     device = ( device_ref ) malloc( sizeof( device_t ));
  200.     if( ! device ) return ENOMEM;
  201.     dep = ( dpeth_t * ) malloc( sizeof( dpeth_t ));
  202.     if( ! dep ){
  203.         free( device );
  204.         return ENOMEM;
  205.     }
  206.     bzero( device, sizeof( device_t ));
  207.     bzero( dep, sizeof( dpeth_t ));
  208.     device->device_id = device_id;
  209.     device->nil_phone = -1;
  210.     device->specific = ( void * ) dep;
  211.     device->state = NETIF_STOPPED;
  212.     dep->de_irq = irq;
  213.     dep->de_mode = DEM_DISABLED;
  214.     //TODO address?
  215.     if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dep->de_base_port ))
  216.     || ERROR_OCCURRED( do_probe( dep ))){
  217.         free( dep );
  218.         free( device );
  219.         return ERROR_CODE;
  220.     }
  221.     if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){
  222.         free( dep );
  223.         free( device );
  224.         return ERROR_CODE;
  225.     }
  226.     return EOK;
  227. }
  228.  
  229. int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
  230.     ERROR_DECLARE;
  231.  
  232.     device_ref  device;
  233.     dpeth_t *   dep;
  234.     packet_t    next;
  235.  
  236.     ERROR_PROPAGATE( find_device( device_id, & device ));
  237.     dep = ( dpeth_t * ) device->specific;
  238. //  TODO remove debug dump:
  239.     uint8_t *   data;
  240.     data = packet_get_data( packet );
  241.     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 ] );
  242.  
  243.     // process packet queue
  244.     do{
  245.         next = pq_detach( packet );
  246.         if( do_pwrite( dep, packet, FALSE ) != EBUSY ){
  247.             netif_pq_release( packet_get_id( packet ));
  248.         }
  249.         packet = next;
  250.     }while( packet );
  251.     dp8390_dump( dep );
  252.     return EOK;
  253. }
  254.  
  255. int netif_start_message( device_ref device ){
  256.     ERROR_DECLARE;
  257.  
  258.     dpeth_t *   dep;
  259.  
  260.     if( device->state != NETIF_ACTIVE ){
  261.         dep = ( dpeth_t * ) device->specific;
  262.         dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR );
  263.         dp8390_cmds[ 2 ].addr = dp8390_cmds[ 0 ].addr;
  264.         ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, device->device_id, & dp8390_code ));
  265.         if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){
  266.             ipc_unregister_irq( dep->de_irq, device->device_id );
  267.             return ERROR_CODE;
  268.         }
  269.         return change_state( device, NETIF_ACTIVE );
  270.     }
  271.     return EOK;
  272. }
  273.  
  274. int netif_stop_message( device_ref device ){
  275.     dpeth_t *   dep;
  276.  
  277.     if( device->state != NETIF_STOPPED ){
  278.         dep = ( dpeth_t * ) device->specific;
  279.         do_stop( dep );
  280.         ipc_unregister_irq( dep->de_irq, device->device_id );
  281.         return change_state( device, NETIF_STOPPED );
  282.     }
  283.     return EOK;
  284. }
  285.  
  286. int change_state( device_ref device, device_state_t state ){
  287.     device->state = state;
  288.     printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
  289.     return state;
  290. }
  291.  
  292. int netif_initialize( void ){
  293.     ipcarg_t    phonehash;
  294.  
  295.     async_set_interrupt_received( irq_handler );
  296.  
  297.     return REGISTER_ME( SERVICE_DP8390, & phonehash );
  298. }
  299.  
  300. /** @}
  301.  */
  302.