fp@1335: /****************************************************************************** fp@1335: * fp@1336: * $Id$ fp@1335: * fp@1363: * Copyright (C) 2008 Olav Zarges, imc Messsysteme GmbH fp@2527: * 2013 Florian Pose fp@1335: * fp@1335: * This file is part of the IgH EtherCAT Master. fp@1335: * fp@1363: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1363: * modify it under the terms of the GNU General Public License version 2, as fp@1363: * published by the Free Software Foundation. fp@1335: * fp@1363: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1363: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1363: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1363: * Public License for more details. fp@1335: * fp@1363: * You should have received a copy of the GNU General Public License along fp@1363: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1335: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1335: * fp@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1335: * fp@1335: *****************************************************************************/ fp@1335: fp@2527: /** \file fp@2527: * EtherCAT FoE state machines. fp@2527: */ fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: #include "globals.h" fp@1335: #include "master.h" fp@1335: #include "mailbox.h" fp@1335: #include "fsm_foe.h" fp@1335: #include "foe.h" fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: /** Maximum time in ms to wait for responses when reading out the dictionary. fp@1335: */ fp@1335: #define EC_FSM_FOE_TIMEOUT 3000 fp@1335: fp@1465: /** Mailbox type FoE. fp@1465: */ fp@1335: #define EC_MBOX_TYPE_FILEACCESS 0x04 fp@1335: fp@1465: /** Size of the FoE header. fp@1465: */ fp@1343: #define EC_FOE_HEADER_SIZE 6 fp@1343: // uint8_t OpCode fp@1343: // uint8_t reserved fp@1343: // uint32_t PacketNo, Password, ErrorCode fp@1343: fp@1343: //#define DEBUG_FOE fp@1343: fp@1343: /*****************************************************************************/ fp@1343: fp@1416: /** FoE OpCodes. fp@1416: */ fp@1343: enum { fp@1416: EC_FOE_OPCODE_RRQ = 1, /**< Read request. */ fp@1416: EC_FOE_OPCODE_WRQ = 2, /**< Write request. */ fp@1416: EC_FOE_OPCODE_DATA = 3, /**< Data. */ fp@1416: EC_FOE_OPCODE_ACK = 4, /**< Acknowledge. */ fp@1416: EC_FOE_OPCODE_ERR = 5, /**< Error. */ fp@1416: EC_FOE_OPCODE_BUSY = 6 /**< Busy. */ fp@1343: } ec_foe_opcode_t; fp@1343: fp@1343: /*****************************************************************************/ fp@1343: fp@2498: int ec_foe_prepare_data_send(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: int ec_foe_prepare_wrq_send(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: int ec_foe_prepare_rrq_send(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: int ec_foe_prepare_send_ack(ec_fsm_foe_t *, ec_datagram_t *); fp@1343: fp@1343: void ec_foe_set_tx_error(ec_fsm_foe_t *, uint32_t); fp@1343: void ec_foe_set_rx_error(ec_fsm_foe_t *, uint32_t); fp@1343: fp@2498: void ec_fsm_foe_end(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: void ec_fsm_foe_error(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: fp@2498: void ec_fsm_foe_state_wrq_sent(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: void ec_fsm_foe_state_rrq_sent(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: fp@2498: void ec_fsm_foe_state_ack_check(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: void ec_fsm_foe_state_ack_read(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: fp@2498: void ec_fsm_foe_state_data_sent(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: fp@2498: void ec_fsm_foe_state_data_check(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: void ec_fsm_foe_state_data_read(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: void ec_fsm_foe_state_sent_ack(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: fp@2498: void ec_fsm_foe_write_start(ec_fsm_foe_t *, ec_datagram_t *); fp@2498: void ec_fsm_foe_read_start(ec_fsm_foe_t *, ec_datagram_t *); fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1901: /** Constructor. fp@1901: */ fp@2498: void ec_fsm_foe_init( fp@2498: ec_fsm_foe_t *fsm /**< finite state machine */ fp@2498: ) fp@2498: { fp@2498: fsm->state = NULL; fp@2498: fsm->datagram = NULL; fp@1901: } fp@1901: fp@1901: /*****************************************************************************/ fp@1901: fp@1901: /** Destructor. fp@1901: */ fp@1335: void ec_fsm_foe_clear(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1335: { fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1901: /** Executes the current state of the state machine. fp@2498: * fp@2498: * \return 1, if the datagram was used, else 0. fp@2498: */ fp@2498: int ec_fsm_foe_exec( fp@2498: ec_fsm_foe_t *fsm, /**< finite state machine */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@2498: int datagram_used = 0; fp@2498: fp@2498: if (fsm->datagram && fp@2498: (fsm->datagram->state == EC_DATAGRAM_INIT || fp@2498: fsm->datagram->state == EC_DATAGRAM_QUEUED || fp@2498: fsm->datagram->state == EC_DATAGRAM_SENT)) { fp@2498: // datagram not received yet fp@2498: return datagram_used; fp@2498: } fp@2498: fp@2498: fsm->state(fsm, datagram); fp@2498: fp@2498: datagram_used = fp@2498: fsm->state != ec_fsm_foe_end && fsm->state != ec_fsm_foe_error; fp@2498: fp@2498: if (datagram_used) { fp@2498: fsm->datagram = datagram; fp@2498: } else { fp@2498: fsm->datagram = NULL; fp@2498: } fp@2498: fp@2498: return datagram_used; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1901: /** Returns, if the state machine terminated with success. fp@1901: * \return non-zero if successful. fp@1901: */ fp@2498: int ec_fsm_foe_success(const ec_fsm_foe_t *fsm /**< Finite state machine */) fp@1335: { fp@1335: return fsm->state == ec_fsm_foe_end; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Prepares an FoE transfer. fp@1465: */ fp@1335: void ec_fsm_foe_transfer( fp@1335: ec_fsm_foe_t *fsm, /**< State machine. */ fp@1335: ec_slave_t *slave, /**< EtherCAT slave. */ fp@1335: ec_foe_request_t *request /**< Sdo request. */ fp@1335: ) fp@1335: { fp@1335: fsm->slave = slave; fp@1335: fsm->request = request; fp@2498: fp@1335: if (request->dir == EC_DIR_OUTPUT) { fp@2498: fsm->tx_buffer = fsm->request->buffer; fp@2498: fsm->tx_buffer_size = fsm->request->data_size; fp@2498: fsm->tx_buffer_offset = 0; fp@2498: fp@2498: fsm->tx_filename = fsm->request->file_name; fp@2498: fsm->tx_filename_len = strlen(fsm->tx_filename); fp@2498: fp@2498: fsm->state = ec_fsm_foe_write_start; fp@1335: } fp@1335: else { fp@2498: fsm->rx_buffer = fsm->request->buffer; fp@2498: fsm->rx_buffer_size = fsm->request->buffer_size; fp@2498: fp@2498: fsm->rx_filename = fsm->request->file_name; fp@2498: fsm->rx_filename_len = strlen(fsm->rx_filename); fp@2498: fp@2498: fsm->state = ec_fsm_foe_read_start; fp@1335: } fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1901: /** State: ERROR. fp@1901: */ fp@2498: void ec_fsm_foe_error( fp@2498: ec_fsm_foe_t *fsm, /**< finite state machine */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@1335: { fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1335: #endif fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1901: /** State: END. fp@1901: */ fp@2498: void ec_fsm_foe_end( fp@2498: ec_fsm_foe_t *fsm, /**< finite state machine */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@1335: { fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1335: #endif fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1901: fp@1901: /** Sends a file or the next fragment. fp@2522: * fp@2522: * \return Zero on success, otherwise a negative error code. fp@1901: */ fp@2498: int ec_foe_prepare_data_send( fp@2522: ec_fsm_foe_t *fsm, /**< Finite state machine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@1446: { fp@1446: size_t remaining_size, current_size; fp@1446: uint8_t *data; fp@1335: fp@1335: remaining_size = fsm->tx_buffer_size - fsm->tx_buffer_offset; fp@1335: fp@1338: if (remaining_size < fsm->slave->configured_tx_mailbox_size fp@1337: - EC_MBOX_HEADER_SIZE - EC_FOE_HEADER_SIZE) { fp@1335: current_size = remaining_size; fp@1335: fsm->tx_last_packet = 1; fp@1337: } else { fp@1338: current_size = fsm->slave->configured_tx_mailbox_size fp@1337: - EC_MBOX_HEADER_SIZE - EC_FOE_HEADER_SIZE; fp@1335: } fp@1335: fp@1446: data = ec_slave_mbox_prepare_send(fsm->slave, fp@2498: datagram, EC_MBOX_TYPE_FILEACCESS, fp@1446: current_size + EC_FOE_HEADER_SIZE); fp@2498: if (IS_ERR(data)) { fp@1335: return -1; fp@2498: } fp@2498: fp@2501: EC_WRITE_U16(data, EC_FOE_OPCODE_DATA); // OpCode = DataBlock req. fp@2498: EC_WRITE_U32(data + 2, fsm->tx_packet_no); // PacketNo, Password fp@1343: fp@1343: memcpy(data + EC_FOE_HEADER_SIZE, fp@1343: fsm->tx_buffer + fsm->tx_buffer_offset, current_size); fp@1335: fsm->tx_current_size = current_size; fp@1335: fp@1335: return 0; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1901: fp@1901: /** Prepare a write request (WRQ) with filename fp@2522: * fp@2522: * \return Zero on success, otherwise a negative error code. fp@1901: */ fp@2498: int ec_foe_prepare_wrq_send( fp@2498: ec_fsm_foe_t *fsm, /**< Finite state machine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@1446: { fp@1335: size_t current_size; fp@1335: uint8_t *data; fp@1335: fp@1335: fsm->tx_buffer_offset = 0; fp@1335: fsm->tx_current_size = 0; fp@1335: fsm->tx_packet_no = 0; fp@1343: fsm->tx_last_packet = 0; fp@1335: fp@1335: current_size = fsm->tx_filename_len; fp@1335: fp@2498: data = ec_slave_mbox_prepare_send(fsm->slave, datagram, fp@1446: EC_MBOX_TYPE_FILEACCESS, current_size + EC_FOE_HEADER_SIZE); fp@2498: if (IS_ERR(data)) { fp@1335: return -1; fp@2498: } fp@1335: fp@1336: EC_WRITE_U16( data, EC_FOE_OPCODE_WRQ); // fsm write request fp@1335: EC_WRITE_U32( data + 2, fsm->tx_packet_no ); fp@1335: fp@1335: memcpy(data + EC_FOE_HEADER_SIZE, fsm->tx_filename, current_size); fp@1335: fp@1335: return 0; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Initializes the FoE write state machine. fp@1465: */ fp@2498: void ec_fsm_foe_write_start( fp@2498: ec_fsm_foe_t *fsm, /**< finite state machine */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@1335: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1335: fsm->tx_buffer_offset = 0; fp@1335: fsm->tx_current_size = 0; fp@1335: fsm->tx_packet_no = 0; fp@1343: fsm->tx_last_packet = 0; fp@1343: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1335: #endif fp@1335: fp@1335: if (!(slave->sii.mailbox_protocols & EC_MBOX_FOE)) { fp@1335: ec_foe_set_tx_error(fsm, FOE_MBOX_PROT_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Slave does not support FoE!\n"); fp@1335: return; fp@1335: } fp@1335: fp@2498: if (ec_foe_prepare_wrq_send(fsm, datagram)) { fp@1335: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1335: return; fp@1335: } fp@1335: fp@1335: fsm->state = ec_fsm_foe_state_wrq_sent; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Check for acknowledge. fp@1465: */ fp@1465: void ec_fsm_foe_state_ack_check( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@2498: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to receive FoE mailbox check datagram: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (fsm->datagram->working_counter != 1) { fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE mailbox check datagram" fp@1921: " failed: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (!ec_slave_mbox_check(fsm->datagram)) { fp@2498: // slave did not put anything in the mailbox yet fp@2528: unsigned long diff_ms = (fsm->datagram->jiffies_received - fp@2528: fsm->jiffies_start) * 1000 / HZ; fp@1335: if (diff_ms >= EC_FSM_FOE_TIMEOUT) { fp@1343: ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Timeout while waiting for ack response.\n"); fp@1335: return; fp@1335: } fp@1343: fp@1335: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: return; fp@1335: } fp@1335: fp@1335: // Fetch response fp@1335: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@1335: fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: fsm->state = ec_fsm_foe_state_ack_read; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Acknowledge a read operation. fp@1465: */ fp@1465: void ec_fsm_foe_state_ack_read( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: uint8_t *data, mbox_prot; fp@1336: uint8_t opCode; fp@1335: size_t rec_size; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@2498: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to receive FoE ack response datagram: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (fsm->datagram->working_counter != 1) { fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE ack response failed: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2502: data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size); fp@2498: if (IS_ERR(data)) { fp@1343: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (mbox_prot != EC_MBOX_TYPE_FILEACCESS) { // FoE fp@1343: ec_foe_set_tx_error(fsm, FOE_MBOX_PROT_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Received mailbox protocol 0x%02X as response.\n", fp@1921: mbox_prot); fp@1335: return; fp@1335: } fp@1335: fp@1336: opCode = EC_READ_U8(data); fp@1336: fp@1336: if (opCode == EC_FOE_OPCODE_BUSY) { fp@1343: // slave not ready fp@2498: if (ec_foe_prepare_data_send(fsm, datagram)) { fp@1343: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Slave is busy.\n"); fp@1343: return; fp@1335: } fp@1335: fsm->state = ec_fsm_foe_state_data_sent; fp@1335: return; fp@1335: } fp@1335: fp@1336: if (opCode == EC_FOE_OPCODE_ACK) { fp@1335: fsm->tx_packet_no++; fp@1335: fsm->tx_buffer_offset += fsm->tx_current_size; fp@1335: fp@1335: if (fsm->tx_last_packet) { fp@1343: fsm->state = ec_fsm_foe_end; fp@1343: return; fp@1335: } fp@1335: fp@2498: if (ec_foe_prepare_data_send(fsm, datagram)) { fp@1343: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1343: return; fp@1343: } fp@1335: fsm->state = ec_fsm_foe_state_data_sent; fp@1343: return; fp@1343: } fp@1343: ec_foe_set_tx_error(fsm, FOE_ACK_ERROR); fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1465: fp@1465: /** State: WRQ SENT. fp@1465: * fp@1465: * Checks is the previous transmit datagram succeded and sends the next fp@1465: * fragment, if necessary. fp@1465: */ fp@1465: void ec_fsm_foe_state_wrq_sent( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@2498: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to send FoE WRQ: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (fsm->datagram->working_counter != 1) { fp@1343: // slave did not put anything in the mailbox yet fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE WRQ failed: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: fsm->jiffies_start = fsm->datagram->jiffies_sent; fp@1335: fp@2502: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@1335: fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: fsm->state = ec_fsm_foe_state_ack_check; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1465: fp@1465: /** State: WRQ SENT. fp@1465: * fp@1465: * Checks is the previous transmit datagram succeded and sends the next fp@1465: * fragment, if necessary. fp@1465: */ fp@1465: void ec_fsm_foe_state_data_sent( fp@2498: ec_fsm_foe_t *fsm, /**< Foe statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@1335: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_tx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to receive FoE ack response datagram: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (fsm->datagram->working_counter != 1) { fp@1343: ec_foe_set_tx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE data send failed: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2502: ec_slave_mbox_prepare_check(slave, datagram); fp@1335: fsm->jiffies_start = jiffies; fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: fsm->state = ec_fsm_foe_state_ack_check; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1901: fp@1901: /** Prepare a read request (RRQ) with filename fp@2522: * fp@2522: * \return Zero on success, otherwise a negative error code. fp@1901: */ fp@2498: int ec_foe_prepare_rrq_send( fp@2498: ec_fsm_foe_t *fsm, /**< Finite state machine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@1446: { fp@1335: size_t current_size; fp@1335: uint8_t *data; fp@1335: fp@1335: current_size = fsm->rx_filename_len; fp@1335: fp@2498: data = ec_slave_mbox_prepare_send(fsm->slave, datagram, fp@1446: EC_MBOX_TYPE_FILEACCESS, current_size + EC_FOE_HEADER_SIZE); fp@2498: if (IS_ERR(data)) { fp@1335: return -1; fp@2498: } fp@1335: fp@1336: EC_WRITE_U16(data, EC_FOE_OPCODE_RRQ); // fsm read request fp@1336: EC_WRITE_U32(data + 2, 0x00000000); // no passwd fp@1335: memcpy(data + EC_FOE_HEADER_SIZE, fsm->rx_filename, current_size); fp@1335: fp@1336: if (fsm->slave->master->debug_level) { fp@1921: EC_SLAVE_DBG(fsm->slave, 1, "FoE Read Request:\n"); fp@1336: ec_print_data(data, current_size + EC_FOE_HEADER_SIZE); fp@1336: } fp@1336: fp@1335: return 0; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Prepare to send an acknowledge. fp@2522: * fp@2522: * \return Zero on success, otherwise a negative error code. fp@1465: */ fp@1465: int ec_foe_prepare_send_ack( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@1465: ) fp@1446: { fp@1335: uint8_t *data; fp@1335: fp@2498: data = ec_slave_mbox_prepare_send(fsm->slave, datagram, fp@1446: EC_MBOX_TYPE_FILEACCESS, EC_FOE_HEADER_SIZE); fp@2498: if (IS_ERR(data)) { fp@1335: return -1; fp@2498: } fp@1335: fp@1446: EC_WRITE_U16(data, EC_FOE_OPCODE_ACK); fp@2498: EC_WRITE_U32(data + 2, fsm->rx_expected_packet_no); fp@1335: fp@1335: return 0; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1465: fp@1465: /** State: RRQ SENT. fp@1465: * fp@2498: * Checks is the previous transmit datagram succeeded and sends the next fp@1465: * fragment, if necessary. fp@1465: */ fp@1465: void ec_fsm_foe_state_rrq_sent( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@2498: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to send FoE RRQ: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (fsm->datagram->working_counter != 1) { fp@1343: // slave did not put anything in the mailbox yet fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE RRQ failed: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: fsm->jiffies_start = fsm->datagram->jiffies_sent; fp@1335: fp@2502: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@1335: fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: fsm->state = ec_fsm_foe_state_data_check; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Starting state for read operations. fp@1465: */ fp@1465: void ec_fsm_foe_read_start( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@1465: ) fp@1335: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1335: fsm->rx_buffer_offset = 0; fp@1335: fsm->rx_expected_packet_no = 1; fp@1335: fsm->rx_last_packet = 0; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@1335: if (!(slave->sii.mailbox_protocols & EC_MBOX_FOE)) { fp@1343: ec_foe_set_tx_error(fsm, FOE_MBOX_PROT_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Slave does not support FoE!\n"); fp@1335: return; fp@1335: } fp@1335: fp@2498: if (ec_foe_prepare_rrq_send(fsm, datagram)) { fp@1335: ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); fp@1335: return; fp@1335: } fp@1335: fp@1335: fsm->state = ec_fsm_foe_state_rrq_sent; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Check for data. fp@1465: */ fp@1465: void ec_fsm_foe_state_data_check( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@2498: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1335: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to send FoE DATA READ: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (fsm->datagram->working_counter != 1) { fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE DATA READ: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (!ec_slave_mbox_check(fsm->datagram)) { fp@2528: unsigned long diff_ms = (fsm->datagram->jiffies_received - fp@2528: fsm->jiffies_start) * 1000 / HZ; fp@1335: if (diff_ms >= EC_FSM_FOE_TIMEOUT) { fp@1343: ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Timeout while waiting for ack response.\n"); fp@1335: return; fp@1335: } fp@1335: fp@1335: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: return; fp@1335: } fp@1335: fp@1335: // Fetch response fp@1335: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@1335: fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: fsm->state = ec_fsm_foe_state_data_read; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Start reading data. fp@1465: */ fp@1465: void ec_fsm_foe_state_data_read( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@1465: ) fp@1343: { fp@1343: size_t rec_size; fp@1336: uint8_t *data, opCode, packet_no, mbox_prot; fp@1335: fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1335: #endif fp@1336: fp@2498: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to receive FoE DATA READ datagram: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (fsm->datagram->working_counter != 1) { fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE DATA READ failed: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2502: data = ec_slave_mbox_fetch(slave, fsm->datagram, &mbox_prot, &rec_size); fp@2498: if (IS_ERR(data)) { fp@1343: ec_foe_set_rx_error(fsm, FOE_MBOX_FETCH_ERROR); fp@1343: return; fp@1343: } fp@1343: fp@1343: if (mbox_prot != EC_MBOX_TYPE_FILEACCESS) { // FoE fp@1921: EC_SLAVE_ERR(slave, "Received mailbox protocol 0x%02X as response.\n", fp@1921: mbox_prot); fp@1335: ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); fp@1335: return; fp@1335: } fp@1335: fp@1336: opCode = EC_READ_U8(data); fp@1336: fp@1336: if (opCode == EC_FOE_OPCODE_BUSY) { fp@2498: if (ec_foe_prepare_send_ack(fsm, datagram)) { fp@1335: ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); fp@1335: } fp@1335: return; fp@1335: } fp@1335: fp@1336: if (opCode == EC_FOE_OPCODE_ERR) { fp@1336: fsm->request->error_code = EC_READ_U32(data + 2); fp@1921: EC_SLAVE_ERR(slave, "Received FoE Error Request (code 0x%08x).\n", fp@1921: fsm->request->error_code); fp@1336: if (rec_size > 6) { fp@2257: uint8_t text[256]; fp@1336: strncpy(text, data + 6, min(rec_size - 6, sizeof(text))); fp@1921: EC_SLAVE_ERR(slave, "FoE Error Text: %s\n", text); fp@1336: } fp@1336: ec_foe_set_rx_error(fsm, FOE_OPCODE_ERROR); fp@1336: return; fp@1336: } fp@1336: fp@1336: if (opCode != EC_FOE_OPCODE_DATA) { fp@1921: EC_SLAVE_ERR(slave, "Received OPCODE %x, expected %x.\n", fp@1921: opCode, EC_FOE_OPCODE_DATA); fp@1336: fsm->request->error_code = 0x00000000; fp@1336: ec_foe_set_rx_error(fsm, FOE_OPCODE_ERROR); fp@1335: return; fp@1335: } fp@1335: fp@1335: packet_no = EC_READ_U16(data + 2); fp@1335: if (packet_no != fsm->rx_expected_packet_no) { fp@1921: EC_SLAVE_ERR(slave, "Received unexpected packet number.\n"); fp@1335: ec_foe_set_rx_error(fsm, FOE_PACKETNO_ERROR); fp@1335: return; fp@1335: } fp@1335: fp@1335: rec_size -= EC_FOE_HEADER_SIZE; fp@1335: fp@1336: if (fsm->rx_buffer_size >= fsm->rx_buffer_offset + rec_size) { fp@1336: memcpy(fsm->rx_buffer + fsm->rx_buffer_offset, fp@1336: data + EC_FOE_HEADER_SIZE, rec_size); fp@1335: fsm->rx_buffer_offset += rec_size; fp@1335: } fp@1335: fp@1336: fsm->rx_last_packet = fp@1336: (rec_size + EC_MBOX_HEADER_SIZE + EC_FOE_HEADER_SIZE fp@2502: != slave->configured_rx_mailbox_size); fp@1335: fp@1335: if (fsm->rx_last_packet || fp@1338: (slave->configured_rx_mailbox_size - EC_MBOX_HEADER_SIZE fp@1343: - EC_FOE_HEADER_SIZE + fsm->rx_buffer_offset) fp@1343: <= fsm->rx_buffer_size) { fp@1343: // either it was the last packet or a new packet will fit into the fp@1343: // delivered buffer fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "last_packet=true\n"); fp@1343: #endif fp@2498: if (ec_foe_prepare_send_ack(fsm, datagram)) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RX_DATA_ACK_ERROR); fp@1343: return; fp@1343: } fp@1343: fp@1343: fsm->state = ec_fsm_foe_state_sent_ack; fp@1335: } fp@1335: else { fp@1343: // no more data fits into the delivered buffer fp@1343: // ... wait for new read request fp@2527: EC_SLAVE_ERR(slave, "Data do not fit in receive buffer!\n"); fp@2527: printk(" rx_buffer_size = %d\n", fsm->rx_buffer_size); fp@2527: printk("rx_buffer_offset = %d\n", fsm->rx_buffer_offset); fp@2527: printk(" rec_size = %zd\n", rec_size); fp@2527: printk(" rx_mailbox_size = %d\n", slave->configured_rx_mailbox_size); fp@2527: printk(" rx_last_packet = %d\n", fsm->rx_last_packet); fp@1343: fsm->request->result = FOE_READY; fp@1335: } fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Sent an acknowledge. fp@1465: */ fp@1465: void ec_fsm_foe_state_sent_ack( fp@2498: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@2498: ec_datagram_t *datagram /**< Datagram to use. */ fp@2498: ) fp@2498: { fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@2527: EC_SLAVE_DBG(fsm->slave, 0, "%s()\n", __func__); fp@1343: #endif fp@1343: fp@2498: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1335: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Failed to send FoE ACK: "); fp@2498: ec_datagram_print_state(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: if (fsm->datagram->working_counter != 1) { fp@1343: // slave did not put anything into the mailbox yet fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1921: EC_SLAVE_ERR(slave, "Reception of FoE ACK failed: "); fp@2498: ec_datagram_print_wc_error(fsm->datagram); fp@2498: return; fp@2498: } fp@2498: fp@2498: fsm->jiffies_start = fsm->datagram->jiffies_sent; fp@1335: fp@2502: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@1335: fp@1335: if (fsm->rx_last_packet) { fp@1343: fsm->rx_expected_packet_no = 0; fp@1343: fsm->request->data_size = fsm->rx_buffer_offset; fp@1343: fsm->state = ec_fsm_foe_end; fp@1335: } fp@1335: else { fp@1343: fsm->rx_expected_packet_no++; fp@1335: fsm->retries = EC_FSM_RETRIES; fp@1335: fsm->state = ec_fsm_foe_state_data_check; fp@1335: } fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Set an error code and go to the send error state. fp@1465: */ fp@1465: void ec_foe_set_tx_error( fp@1465: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@1465: uint32_t errorcode /**< FoE error code. */ fp@1465: ) fp@1336: { fp@1343: fsm->request->result = errorcode; fp@1343: fsm->state = ec_fsm_foe_error; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1465: /** Set an error code and go to the receive error state. fp@1465: */ fp@1465: void ec_foe_set_rx_error( fp@1465: ec_fsm_foe_t *fsm, /**< FoE statemachine. */ fp@1465: uint32_t errorcode /**< FoE error code. */ fp@1465: ) fp@1336: { fp@1343: fsm->request->result = errorcode; fp@1343: fsm->state = ec_fsm_foe_error; fp@1343: } fp@1343: fp@1343: /*****************************************************************************/