2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gpxe/list.h>
26 #include <gpxe/if_ether.h>
27 #include <gpxe/ethernet.h>
28 #include <gpxe/iobuf.h>
29 #include <gpxe/uaccess.h>
31 #include <gpxe/netdevice.h>
32 #include <gpxe/async.h>
41 struct net_protocol aoe_protocol;
43 /** List of all AoE sessions */
44 static LIST_HEAD ( aoe_sessions );
47 * Mark current AoE command complete
50 * @v rc Return status code
52 static void aoe_done ( struct aoe_session *aoe, int rc ) {
54 /* Record overall command status */
55 aoe->command->cb.cmd_stat = aoe->status;
58 /* Mark async operation as complete */
59 async_done ( &aoe->async, rc );
66 * @ret rc Return status code
68 * This transmits an AoE command packet. It does not wait for a
71 static int aoe_send_command ( struct aoe_session *aoe ) {
72 struct ata_command *command = aoe->command;
73 struct io_buffer *iobuf;
74 struct aoehdr *aoehdr;
75 struct aoecmd *aoecmd;
77 unsigned int data_out_len;
79 /* Fail immediately if we have no netdev to send on */
80 if ( ! aoe->netdev ) {
81 aoe_done ( aoe, -ENETUNREACH );
85 /* Calculate count and data_out_len for this subcommand */
86 count = command->cb.count.native;
87 if ( count > AOE_MAX_COUNT )
88 count = AOE_MAX_COUNT;
89 data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
91 /* Create outgoing I/O buffer */
92 iobuf = alloc_iob ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
96 iob_reserve ( iobuf, ETH_HLEN );
97 aoehdr = iob_put ( iobuf, sizeof ( *aoehdr ) );
98 aoecmd = iob_put ( iobuf, sizeof ( *aoecmd ) );
99 memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
101 /* Fill AoE header */
102 aoehdr->ver_flags = AOE_VERSION;
103 aoehdr->major = htons ( aoe->major );
104 aoehdr->minor = aoe->minor;
105 aoehdr->tag = htonl ( ++aoe->tag );
107 /* Fill AoE command */
108 linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
109 aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
110 ( command->cb.device & ATA_DEV_SLAVE ) |
111 ( data_out_len ? AOE_FL_WRITE : 0 ) );
112 aoecmd->err_feat = command->cb.err_feat.bytes.cur;
113 aoecmd->count = count;
114 aoecmd->cmd_stat = command->cb.cmd_stat;
115 aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
116 if ( ! command->cb.lba48 )
117 aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
119 /* Fill data payload */
120 copy_from_user ( iob_put ( iobuf, data_out_len ), command->data_out,
121 aoe->command_offset, data_out_len );
124 start_timer ( &aoe->timer );
125 return net_tx ( iobuf, aoe->netdev, &aoe_protocol, aoe->target );
129 * Handle AoE retry timer expiry
131 * @v timer AoE retry timer
132 * @v fail Failure indicator
134 static void aoe_timer_expired ( struct retry_timer *timer, int fail ) {
135 struct aoe_session *aoe =
136 container_of ( timer, struct aoe_session, timer );
139 aoe_done ( aoe, -ETIMEDOUT );
141 aoe_send_command ( aoe );
146 * Handle AoE response
149 * @v aoehdr AoE header
150 * @ret rc Return status code
152 static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
154 struct aoecmd *aoecmd = aoehdr->arg.command;
155 struct ata_command *command = aoe->command;
156 unsigned int rx_data_len;
158 unsigned int data_len;
161 if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) ) {
162 /* Ignore packet; allow timer to trigger retransmit */
165 rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
167 /* Stop retry timer. After this point, every code path must
168 * either terminate the AoE operation via aoe_done(), or
169 * transmit a new packet.
171 stop_timer ( &aoe->timer );
173 /* Check for fatal errors */
174 if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
175 aoe_done ( aoe, -EIO );
179 /* Calculate count and data_len for this subcommand */
180 count = command->cb.count.native;
181 if ( count > AOE_MAX_COUNT )
182 count = AOE_MAX_COUNT;
183 data_len = count * ATA_SECTOR_SIZE;
185 /* Merge into overall ATA status */
186 aoe->status |= aoecmd->cmd_stat;
188 /* Copy data payload */
189 if ( command->data_in ) {
190 if ( rx_data_len > data_len )
191 rx_data_len = data_len;
192 copy_to_user ( command->data_in, aoe->command_offset,
193 aoecmd->data, rx_data_len );
196 /* Update ATA command and offset */
197 aoe->command_offset += data_len;
198 command->cb.lba.native += count;
199 command->cb.count.native -= count;
201 /* Check for operation complete */
202 if ( ! command->cb.count.native ) {
207 /* Transmit next portion of request */
208 aoe_send_command ( aoe );
214 * Process incoming AoE packets
216 * @v iobuf I/O buffer
217 * @v netdev Network device
218 * @v ll_source Link-layer source address
219 * @ret rc Return status code
222 static int aoe_rx ( struct io_buffer *iobuf, struct net_device *netdev __unused,
223 const void *ll_source ) {
224 struct aoehdr *aoehdr = iobuf->data;
225 unsigned int len = iob_len ( iobuf );
226 struct aoe_session *aoe;
230 if ( len < sizeof ( *aoehdr ) ) {
234 if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
235 rc = -EPROTONOSUPPORT;
238 if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
239 /* Ignore AoE requests that we happen to see */
243 /* Demultiplex amongst active AoE sessions */
244 list_for_each_entry ( aoe, &aoe_sessions, list ) {
245 if ( ntohs ( aoehdr->major ) != aoe->major )
247 if ( aoehdr->minor != aoe->minor )
249 if ( ntohl ( aoehdr->tag ) != aoe->tag )
251 memcpy ( aoe->target, ll_source, sizeof ( aoe->target ) );
252 rc = aoe_rx_response ( aoe, aoehdr, len );
262 struct net_protocol aoe_protocol __net_protocol = {
264 .net_proto = htons ( ETH_P_AOE ),
273 void aoe_open ( struct aoe_session *aoe ) {
274 memcpy ( aoe->target, ethernet_protocol.ll_broadcast,
275 sizeof ( aoe->target ) );
276 aoe->tag = AOE_TAG_MAGIC;
277 aoe->timer.expired = aoe_timer_expired;
278 list_add ( &aoe->list, &aoe_sessions );
286 void aoe_close ( struct aoe_session *aoe ) {
287 list_del ( &aoe->list );
291 * Issue ATA command via an open AoE session
294 * @v command ATA command
295 * @v parent Parent asynchronous operation
296 * @ret rc Return status code
298 * Only one command may be issued concurrently per session. This call
299 * is non-blocking; use async_wait() to wait for the command to
302 int aoe_issue ( struct aoe_session *aoe, struct ata_command *command,
303 struct async *parent ) {
304 aoe->command = command;
306 aoe->command_offset = 0;
307 aoe_send_command ( aoe );
308 async_init ( &aoe->async, &default_async_operations, parent );