fp@1335: /****************************************************************************** fp@1335: * fp@1336: * $Id$ fp@1335: * fp@1363: * Copyright (C) 2008 Olav Zarges, imc Messsysteme GmbH 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@1335: /** fp@1335: \file fp@1335: EtherCAT FoE state machines. fp@1335: */ 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@1343: int ec_foe_prepare_data_send(ec_fsm_foe_t *); fp@1343: int ec_foe_prepare_wrq_send(ec_fsm_foe_t *); fp@1343: int ec_foe_prepare_rrq_send(ec_fsm_foe_t *); fp@1343: int ec_foe_prepare_send_ack(ec_fsm_foe_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@1343: void ec_fsm_foe_write(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_read(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_end(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_error(ec_fsm_foe_t *); fp@1343: fp@1343: void ec_fsm_foe_state_wrq_sent(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_state_rrq_sent(ec_fsm_foe_t *); fp@1343: fp@1343: void ec_fsm_foe_state_ack_check(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_state_ack_read(ec_fsm_foe_t *); fp@1343: fp@1343: void ec_fsm_foe_state_data_sent(ec_fsm_foe_t *); fp@1343: fp@1343: void ec_fsm_foe_state_data_check(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_state_data_read(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_state_sent_ack(ec_fsm_foe_t *); fp@1343: fp@1343: void ec_fsm_foe_write_start(ec_fsm_foe_t *); fp@1343: void ec_fsm_foe_read_start(ec_fsm_foe_t *); fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: /** fp@1335: Constructor. fp@1335: */ fp@1335: fp@1335: void ec_fsm_foe_init(ec_fsm_foe_t *fsm, /**< finite state machine */ fp@1335: ec_datagram_t *datagram /**< datagram */ fp@1335: ) fp@1335: { fp@1335: fsm->state = NULL; fp@1335: fsm->datagram = datagram; fp@1335: fsm->rx_errors = 0; fp@1335: fsm->tx_errors = 0; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: /** fp@1335: Destructor. fp@1335: */ fp@1335: 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@1335: /** fp@1335: Executes the current state of the state machine. fp@1335: \return false, if state machine has terminated fp@1335: */ fp@1335: fp@1335: int ec_fsm_foe_exec(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1335: { fp@1335: fsm->state(fsm); fp@1335: fp@1335: return fsm->state != ec_fsm_foe_end && fsm->state != ec_fsm_foe_error; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: /** fp@1335: Returns, if the state machine terminated with success. fp@1335: \return non-zero if successful. fp@1335: */ fp@1335: fp@1335: int ec_fsm_foe_success(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@1335: if (request->dir == EC_DIR_OUTPUT) { fp@1335: fsm->state = ec_fsm_foe_write; fp@1335: } fp@1335: else { fp@1335: fsm->state = ec_fsm_foe_read; fp@1335: } fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: /** fp@1335: State: ERROR. fp@1335: */ fp@1335: fp@1335: void ec_fsm_foe_error(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1335: { fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_fsm_foe_error()\n"); fp@1335: #endif fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: /** fp@1335: State: END. fp@1335: */ fp@1335: fp@1335: void ec_fsm_foe_end(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1335: { fp@1343: #ifdef DEBUG_FOE fp@1335: printk("ec_fsm_foe_end\n"); fp@1335: #endif fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: /** fp@1335: Sends a file or the next fragment. fp@1335: */ fp@1335: fp@1446: int ec_foe_prepare_data_send(ec_fsm_foe_t *fsm) 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@1446: fsm->datagram, EC_MBOX_TYPE_FILEACCESS, fp@1446: current_size + EC_FOE_HEADER_SIZE); fp@1446: if (IS_ERR(data)) fp@1335: return -1; fp@1335: fp@1343: EC_WRITE_U8 ( data, EC_FOE_OPCODE_DATA ); // OpCode = DataBlock req. fp@1343: 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@1335: /** fp@1335: Prepare a write request (WRQ) with filename fp@1335: */ fp@1335: fp@1446: int ec_foe_prepare_wrq_send(ec_fsm_foe_t *fsm) 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@1446: data = ec_slave_mbox_prepare_send(fsm->slave, fsm->datagram, fp@1446: EC_MBOX_TYPE_FILEACCESS, current_size + EC_FOE_HEADER_SIZE); fp@1446: if (IS_ERR(data)) fp@1335: return -1; 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: /** Start a write operation. fp@1465: */ fp@1465: void ec_fsm_foe_write( fp@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1335: { fp@1335: fsm->tx_buffer = fsm->request->buffer; fp@1335: fsm->tx_buffer_size = fsm->request->data_size; fp@1335: fsm->tx_buffer_offset = 0; fp@1335: fp@1343: fsm->tx_filename = fsm->request->file_name; fp@1343: fsm->tx_filename_len = strlen(fsm->tx_filename); fp@1343: fp@1343: fsm->state = ec_fsm_foe_write_start; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1465: fp@1465: /** Initializes the FoE write state machine. fp@1465: */ fp@1335: void ec_fsm_foe_write_start(ec_fsm_foe_t *fsm /**< finite state machine */) 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@1335: printk("ec_fsm_foe_write_start()\n"); 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@1335: EC_ERR("Slave %u does not support FoE!\n", slave->ring_position); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (ec_foe_prepare_wrq_send(fsm)) { 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@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1446: { fp@1335: ec_datagram_t *datagram = fsm->datagram; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_fsm_foe_ack_check()\n"); fp@1343: #endif fp@1343: fp@1335: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1335: EC_ERR("Failed to receive FoE mailbox check datagram for slave %u" fp@1335: " (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (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@1335: EC_ERR("Reception of FoE mailbox check datagram failed on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1343: return; fp@1343: } fp@1335: fp@1335: if (!ec_slave_mbox_check(datagram)) { fp@1335: unsigned long diff_ms = fp@1335: (datagram->jiffies_received - 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@1335: EC_ERR("Timeout while waiting for ack response " fp@1335: "on slave %u.\n", slave->ring_position); 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@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1465: { fp@1335: ec_datagram_t *datagram = fsm->datagram; 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@1343: printk("ec_fsm_foe_ack_read()\n"); fp@1343: #endif fp@1343: fp@1335: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1335: EC_ERR("Failed to receive FoE ack response datagram for" fp@1335: " slave %u (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (datagram->working_counter != 1) { fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1335: EC_ERR("Reception of FoE ack response failed on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1335: return; fp@1335: } fp@1335: fp@1343: if (!(data = ec_slave_mbox_fetch(fsm->slave, datagram, fp@1343: &mbox_prot, &rec_size))) { 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@1335: EC_ERR("Received mailbox protocol 0x%02X as response.\n", 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@1335: if (ec_foe_prepare_data_send(fsm)) { fp@1343: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1335: EC_ERR("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@1335: if (ec_foe_prepare_data_send(fsm)) { 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@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1446: { fp@1335: ec_datagram_t *datagram = fsm->datagram; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_foe_state_sent_wrq()\n"); fp@1343: #endif fp@1343: fp@1335: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1335: EC_ERR("Failed to send FoE WRQ for slave %u" fp@1335: " (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (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@1335: EC_ERR("Reception of FoE WRQ failed on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1343: return; fp@1335: } fp@1335: fp@1335: fsm->jiffies_start = datagram->jiffies_sent; fp@1335: fp@1335: ec_slave_mbox_prepare_check(fsm->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@1465: ec_fsm_foe_t *fsm /**< Foe statemachine. */ fp@1465: ) fp@1446: { fp@1335: ec_datagram_t *datagram = fsm->datagram; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_fsm_foe_state_data_sent()\n"); 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@1335: EC_ERR("Failed to receive FoE ack response datagram for" fp@1335: " slave %u (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); 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@1335: EC_ERR("Reception of FoE data send failed on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1335: return; fp@1335: } fp@1335: fp@1335: ec_slave_mbox_prepare_check(fsm->slave, fsm->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@1335: /** fp@1335: Prepare a read request (RRQ) with filename fp@1335: */ fp@1335: fp@1446: int ec_foe_prepare_rrq_send(ec_fsm_foe_t *fsm) 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@1446: data = ec_slave_mbox_prepare_send(fsm->slave, fsm->datagram, fp@1446: EC_MBOX_TYPE_FILEACCESS, current_size + EC_FOE_HEADER_SIZE); fp@1446: if (IS_ERR(data)) fp@1335: return -1; 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@1336: EC_DBG("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@1335: fp@1465: /** Prepare to send an acknowledge. fp@1465: */ fp@1465: int ec_foe_prepare_send_ack( fp@1465: ec_fsm_foe_t *foe /**< FoE statemachine. */ fp@1465: ) fp@1446: { fp@1335: uint8_t *data; fp@1335: fp@1446: data = ec_slave_mbox_prepare_send(foe->slave, foe->datagram, fp@1446: EC_MBOX_TYPE_FILEACCESS, EC_FOE_HEADER_SIZE); fp@1446: if (IS_ERR(data)) fp@1335: return -1; fp@1335: fp@1446: EC_WRITE_U16(data, EC_FOE_OPCODE_ACK); fp@1446: EC_WRITE_U32(data + 2, foe->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@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_rrq_sent( fp@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1465: { fp@1335: ec_datagram_t *datagram = fsm->datagram; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_foe_state_rrq_sent()\n"); fp@1343: #endif fp@1343: fp@1335: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1335: EC_ERR("Failed to send FoE RRQ for slave %u" fp@1335: " (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (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@1335: EC_ERR("Reception of FoE RRQ failed on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1343: return; fp@1335: } fp@1335: fp@1335: fsm->jiffies_start = datagram->jiffies_sent; fp@1335: fp@1335: ec_slave_mbox_prepare_check(fsm->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: /** Start a read operation. fp@1465: */ fp@1465: void ec_fsm_foe_read( fp@1465: ec_fsm_foe_t *fsm /**< FoE state machine. */ fp@1465: ) fp@1335: { fp@1343: fsm->state = ec_fsm_foe_read_start; fp@1343: fsm->rx_filename = fsm->request->file_name; fp@1343: fsm->rx_filename_len = strlen(fsm->rx_filename); fp@1343: fp@1343: fsm->rx_buffer = fsm->request->buffer; fp@1343: fsm->rx_buffer_size = fsm->request->buffer_size; 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@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1335: { fp@1343: size_t current_size; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1335: fsm->rx_buffer_offset = 0; fp@1335: fsm->rx_current_size = 0; fp@1335: fsm->rx_packet_no = 0; fp@1335: fsm->rx_expected_packet_no = 1; fp@1335: fsm->rx_last_packet = 0; fp@1335: fp@1335: current_size = fsm->rx_filename_len; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_fsm_foe_read_start()\n"); 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@1335: EC_ERR("Slave %u does not support FoE!\n", slave->ring_position); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (ec_foe_prepare_rrq_send(fsm)) { 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@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1465: { fp@1335: ec_datagram_t *datagram = fsm->datagram; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_fsm_foe_state_data_check()\n"); fp@1343: #endif fp@1343: fp@1343: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1335: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1335: EC_ERR("Failed to send FoE DATA READ for slave %u" fp@1335: " (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (datagram->working_counter != 1) { fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1335: EC_ERR("Reception of FoE DATA READ on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (!ec_slave_mbox_check(datagram)) { fp@1335: unsigned long diff_ms = fp@1335: (datagram->jiffies_received - 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@1335: EC_ERR("Timeout while waiting for ack response " fp@1335: "on slave %u.\n", slave->ring_position); 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@1335: fp@1465: /** Start reading data. fp@1465: */ fp@1465: void ec_fsm_foe_state_data_read( fp@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ 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_datagram_t *datagram = fsm->datagram; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_fsm_foe_state_data_read()\n"); fp@1335: #endif fp@1336: fp@1335: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1343: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1335: EC_ERR("Failed to receive FoE DATA READ datagram for" fp@1335: " slave %u (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (datagram->working_counter != 1) { fp@1335: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1335: EC_ERR("Reception of FoE DATA READ failed on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1335: return; fp@1335: } fp@1335: fp@1343: if (!(data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size))) { 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@1335: EC_ERR("Received mailbox protocol 0x%02X as response.\n", 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@1335: if (ec_foe_prepare_send_ack(fsm)) { 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@1343: EC_ERR("Received FoE Error Request (code 0x%08x) on slave %u.\n", fp@1336: fsm->request->error_code, slave->ring_position); fp@1336: if (rec_size > 6) { fp@1336: uint8_t text[1024]; fp@1336: strncpy(text, data + 6, min(rec_size - 6, sizeof(text))); fp@1336: EC_ERR("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@1336: EC_ERR("Received OPCODE %x, expected %x on slave %u.\n", fp@1336: opCode, EC_FOE_OPCODE_DATA, slave->ring_position); 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@1336: EC_ERR("Received unexpected packet number on slave %u.\n", fp@1336: slave->ring_position); 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@1338: != fsm->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@1343: printk ("last_packet=true\n"); fp@1343: #endif fp@1343: if (ec_foe_prepare_send_ack(fsm)) { 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@1343: printk ("ERROR: data doesn't fit in receive buffer\n"); fp@1343: printk (" rx_buffer_size = %d\n", fsm->rx_buffer_size); fp@1343: printk (" rx_buffer_offset= %d\n", fsm->rx_buffer_offset); fp@1543: printk (" rec_size = %zd\n", rec_size); fp@1343: printk (" rx_mailbox_size = %d\n", fp@1343: slave->configured_rx_mailbox_size); fp@1343: 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@1465: ec_fsm_foe_t *fsm /**< FoE statemachine. */ fp@1465: ) fp@1465: { fp@1335: ec_datagram_t *datagram = fsm->datagram; fp@1335: ec_slave_t *slave = fsm->slave; fp@1335: fp@1343: #ifdef DEBUG_FOE fp@1343: printk("ec_foe_state_sent_ack()\n"); fp@1343: #endif fp@1343: fp@1335: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1335: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1335: EC_ERR("Failed to send FoE ACK for slave %u" fp@1335: " (datagram state %u).\n", fp@1335: slave->ring_position, datagram->state); fp@1335: return; fp@1335: } fp@1335: fp@1335: if (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@1335: EC_ERR("Reception of FoE ACK failed on slave %u: ", fp@1335: slave->ring_position); fp@1335: ec_datagram_print_wc_error(datagram); fp@1343: return; fp@1335: } fp@1335: fp@1335: fsm->jiffies_start = datagram->jiffies_sent; fp@1335: fp@1335: ec_slave_mbox_prepare_check(fsm->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->tx_errors++; 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->rx_errors++; fp@1343: fsm->request->result = errorcode; fp@1343: fsm->state = ec_fsm_foe_error; fp@1343: } fp@1343: fp@1343: /*****************************************************************************/