Subversion Repositories HelenOS

Rev

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