123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
-
-
- #include <stddef.h>
- #include <string.h>
- #include <vsprintf.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <gpxe/list.h>
- #include <gpxe/if_ether.h>
- #include <gpxe/ethernet.h>
- #include <gpxe/pkbuff.h>
- #include <gpxe/uaccess.h>
- #include <gpxe/ata.h>
- #include <gpxe/netdevice.h>
- #include <gpxe/async.h>
- #include <gpxe/aoe.h>
-
-
-
- struct net_protocol aoe_protocol;
-
-
- static LIST_HEAD ( aoe_sessions );
-
-
- static void aoe_done ( struct aoe_session *aoe, int rc ) {
-
-
- aoe->command->cb.cmd_stat = aoe->status;
- aoe->command = NULL;
-
-
- async_done ( &aoe->async, rc );
- }
-
-
- static int aoe_send_command ( struct aoe_session *aoe ) {
- struct ata_command *command = aoe->command;
- struct pk_buff *pkb;
- struct aoehdr *aoehdr;
- struct aoecmd *aoecmd;
- unsigned int count;
- unsigned int data_out_len;
-
-
- if ( ! aoe->netdev ) {
- aoe_done ( aoe, -ENETUNREACH );
- return -ENETUNREACH;
- }
-
-
- count = command->cb.count.native;
- if ( count > AOE_MAX_COUNT )
- count = AOE_MAX_COUNT;
- data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
-
-
- pkb = alloc_pkb ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
- data_out_len );
- if ( ! pkb )
- return -ENOMEM;
- pkb_reserve ( pkb, ETH_HLEN );
- aoehdr = pkb_put ( pkb, sizeof ( *aoehdr ) );
- aoecmd = pkb_put ( pkb, sizeof ( *aoecmd ) );
- memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
-
-
- aoehdr->ver_flags = AOE_VERSION;
- aoehdr->major = htons ( aoe->major );
- aoehdr->minor = aoe->minor;
- aoehdr->tag = htonl ( ++aoe->tag );
-
-
- linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
- aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
- ( command->cb.device & ATA_DEV_SLAVE ) |
- ( data_out_len ? AOE_FL_WRITE : 0 ) );
- aoecmd->err_feat = command->cb.err_feat.bytes.cur;
- aoecmd->count = count;
- aoecmd->cmd_stat = command->cb.cmd_stat;
- aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
- if ( ! command->cb.lba48 )
- aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
-
-
- copy_from_user ( pkb_put ( pkb, data_out_len ), command->data_out,
- aoe->command_offset, data_out_len );
-
-
- start_timer ( &aoe->timer );
- return net_tx ( pkb, aoe->netdev, &aoe_protocol, aoe->target );
- }
-
-
- static void aoe_timer_expired ( struct retry_timer *timer, int fail ) {
- struct aoe_session *aoe =
- container_of ( timer, struct aoe_session, timer );
-
- if ( fail ) {
- aoe_done ( aoe, -ETIMEDOUT );
- } else {
- aoe_send_command ( aoe );
- }
- }
-
-
- static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
- unsigned int len ) {
- struct aoecmd *aoecmd = aoehdr->arg.command;
- struct ata_command *command = aoe->command;
- unsigned int rx_data_len;
- unsigned int count;
- unsigned int data_len;
-
-
- if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) ) {
-
- return -EINVAL;
- }
- rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
-
-
-
- stop_timer ( &aoe->timer );
-
-
- if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
- aoe_done ( aoe, -EIO );
- return 0;
- }
-
-
- count = command->cb.count.native;
- if ( count > AOE_MAX_COUNT )
- count = AOE_MAX_COUNT;
- data_len = count * ATA_SECTOR_SIZE;
-
-
- aoe->status |= aoecmd->cmd_stat;
-
-
- if ( command->data_in ) {
- if ( rx_data_len > data_len )
- rx_data_len = data_len;
- copy_to_user ( command->data_in, aoe->command_offset,
- aoecmd->data, rx_data_len );
- }
-
-
- aoe->command_offset += data_len;
- command->cb.lba.native += count;
- command->cb.count.native -= count;
-
-
- if ( ! command->cb.count.native ) {
- aoe_done ( aoe, 0 );
- return 0;
- }
-
-
- aoe_send_command ( aoe );
-
- return 0;
- }
-
-
- static int aoe_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
- const void *ll_source ) {
- struct aoehdr *aoehdr = pkb->data;
- unsigned int len = pkb_len ( pkb );
- struct aoe_session *aoe;
- int rc = 0;
-
-
- if ( len < sizeof ( *aoehdr ) ) {
- rc = -EINVAL;
- goto done;
- }
- if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
- rc = -EPROTONOSUPPORT;
- goto done;
- }
- if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
-
- goto done;
- }
-
-
- list_for_each_entry ( aoe, &aoe_sessions, list ) {
- if ( ntohs ( aoehdr->major ) != aoe->major )
- continue;
- if ( aoehdr->minor != aoe->minor )
- continue;
- if ( ntohl ( aoehdr->tag ) != aoe->tag )
- continue;
- memcpy ( aoe->target, ll_source, sizeof ( aoe->target ) );
- rc = aoe_rx_response ( aoe, aoehdr, len );
- break;
- }
-
- done:
- free_pkb ( pkb );
- return rc;
- }
-
-
- struct net_protocol aoe_protocol __net_protocol = {
- .name = "AoE",
- .net_proto = htons ( ETH_P_AOE ),
- .rx = aoe_rx,
- };
-
-
- static void aoe_forget_netdev ( struct reference *ref ) {
- struct aoe_session *aoe
- = container_of ( ref, struct aoe_session, netdev_ref );
-
- aoe->netdev = NULL;
- ref_del ( &aoe->netdev_ref );
- }
-
-
- void aoe_open ( struct aoe_session *aoe ) {
- memcpy ( aoe->target, ethernet_protocol.ll_broadcast,
- sizeof ( aoe->target ) );
- aoe->tag = AOE_TAG_MAGIC;
- aoe->timer.expired = aoe_timer_expired;
- aoe->netdev_ref.forget = aoe_forget_netdev;
- ref_add ( &aoe->netdev_ref, &aoe->netdev->references );
- list_add ( &aoe->list, &aoe_sessions );
- }
-
-
- void aoe_close ( struct aoe_session *aoe ) {
- if ( aoe->netdev )
- ref_del ( &aoe->netdev_ref );
- list_del ( &aoe->list );
- }
-
-
- int aoe_issue ( struct aoe_session *aoe, struct ata_command *command,
- struct async *parent ) {
- aoe->command = command;
- aoe->status = 0;
- aoe->command_offset = 0;
- aoe_send_command ( aoe );
- async_init ( &aoe->async, &default_async_operations, parent );
- return 0;
- }
|