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.
26 #include <gpxe/list.h>
27 #include <gpxe/if_ether.h>
28 #include <gpxe/ethernet.h>
29 #include <gpxe/iobuf.h>
30 #include <gpxe/uaccess.h>
32 #include <gpxe/netdevice.h>
33 #include <gpxe/process.h>
42 struct net_protocol aoe_protocol;
44 /** List of all AoE sessions */
45 static LIST_HEAD ( aoe_sessions );
47 static void aoe_free ( struct refcnt *refcnt ) {
48 struct aoe_session *aoe =
49 container_of ( refcnt, struct aoe_session, refcnt );
51 netdev_put ( aoe->netdev );
56 * Mark current AoE command complete
59 * @v rc Return status code
61 static void aoe_done ( struct aoe_session *aoe, int rc ) {
63 /* Record overall command status */
64 aoe->command->cb.cmd_stat = aoe->status;
67 /* Mark operation as complete */
75 * @ret rc Return status code
77 * This transmits an AoE command packet. It does not wait for a
80 static int aoe_send_command ( struct aoe_session *aoe ) {
81 struct ata_command *command = aoe->command;
82 struct io_buffer *iobuf;
83 struct aoehdr *aoehdr;
84 struct aoecmd *aoecmd;
86 unsigned int data_out_len;
88 /* Fail immediately if we have no netdev to send on */
89 if ( ! aoe->netdev ) {
90 aoe_done ( aoe, -ENETUNREACH );
94 /* Calculate count and data_out_len for this subcommand */
95 count = command->cb.count.native;
96 if ( count > AOE_MAX_COUNT )
97 count = AOE_MAX_COUNT;
98 data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
100 /* Create outgoing I/O buffer */
101 iobuf = alloc_iob ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
105 iob_reserve ( iobuf, ETH_HLEN );
106 aoehdr = iob_put ( iobuf, sizeof ( *aoehdr ) );
107 aoecmd = iob_put ( iobuf, sizeof ( *aoecmd ) );
108 memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
110 /* Fill AoE header */
111 aoehdr->ver_flags = AOE_VERSION;
112 aoehdr->major = htons ( aoe->major );
113 aoehdr->minor = aoe->minor;
114 aoehdr->tag = htonl ( ++aoe->tag );
116 /* Fill AoE command */
117 linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
118 aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
119 ( command->cb.device & ATA_DEV_SLAVE ) |
120 ( data_out_len ? AOE_FL_WRITE : 0 ) );
121 aoecmd->err_feat = command->cb.err_feat.bytes.cur;
122 aoecmd->count = count;
123 aoecmd->cmd_stat = command->cb.cmd_stat;
124 aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
125 if ( ! command->cb.lba48 )
126 aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
128 /* Fill data payload */
129 copy_from_user ( iob_put ( iobuf, data_out_len ), command->data_out,
130 aoe->command_offset, data_out_len );
133 start_timer ( &aoe->timer );
134 return net_tx ( iobuf, aoe->netdev, &aoe_protocol, aoe->target );
138 * Handle AoE retry timer expiry
140 * @v timer AoE retry timer
141 * @v fail Failure indicator
143 static void aoe_timer_expired ( struct retry_timer *timer, int fail ) {
144 struct aoe_session *aoe =
145 container_of ( timer, struct aoe_session, timer );
148 aoe_done ( aoe, -ETIMEDOUT );
150 aoe_send_command ( aoe );
155 * Handle AoE response
158 * @v aoehdr AoE header
159 * @ret rc Return status code
161 static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
163 struct aoecmd *aoecmd = aoehdr->arg.command;
164 struct ata_command *command = aoe->command;
165 unsigned int rx_data_len;
167 unsigned int data_len;
170 if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) ) {
171 /* Ignore packet; allow timer to trigger retransmit */
174 rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
176 /* Stop retry timer. After this point, every code path must
177 * either terminate the AoE operation via aoe_done(), or
178 * transmit a new packet.
180 stop_timer ( &aoe->timer );
182 /* Check for fatal errors */
183 if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
184 aoe_done ( aoe, -EIO );
188 /* Calculate count and data_len for this subcommand */
189 count = command->cb.count.native;
190 if ( count > AOE_MAX_COUNT )
191 count = AOE_MAX_COUNT;
192 data_len = count * ATA_SECTOR_SIZE;
194 /* Merge into overall ATA status */
195 aoe->status |= aoecmd->cmd_stat;
197 /* Copy data payload */
198 if ( command->data_in ) {
199 if ( rx_data_len > data_len )
200 rx_data_len = data_len;
201 copy_to_user ( command->data_in, aoe->command_offset,
202 aoecmd->data, rx_data_len );
205 /* Update ATA command and offset */
206 aoe->command_offset += data_len;
207 command->cb.lba.native += count;
208 command->cb.count.native -= count;
210 /* Check for operation complete */
211 if ( ! command->cb.count.native ) {
216 /* Transmit next portion of request */
217 aoe_send_command ( aoe );
223 * Process incoming AoE packets
225 * @v iobuf I/O buffer
226 * @v netdev Network device
227 * @v ll_source Link-layer source address
228 * @ret rc Return status code
231 static int aoe_rx ( struct io_buffer *iobuf, struct net_device *netdev __unused,
232 const void *ll_source ) {
233 struct aoehdr *aoehdr = iobuf->data;
234 unsigned int len = iob_len ( iobuf );
235 struct aoe_session *aoe;
239 if ( len < sizeof ( *aoehdr ) ) {
243 if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
244 rc = -EPROTONOSUPPORT;
247 if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
248 /* Ignore AoE requests that we happen to see */
252 /* Demultiplex amongst active AoE sessions */
253 list_for_each_entry ( aoe, &aoe_sessions, list ) {
254 if ( ntohs ( aoehdr->major ) != aoe->major )
256 if ( aoehdr->minor != aoe->minor )
258 if ( ntohl ( aoehdr->tag ) != aoe->tag )
260 memcpy ( aoe->target, ll_source, sizeof ( aoe->target ) );
261 rc = aoe_rx_response ( aoe, aoehdr, len );
271 struct net_protocol aoe_protocol __net_protocol = {
273 .net_proto = htons ( ETH_P_AOE ),
278 * Issue ATA command via an open AoE session
281 * @v command ATA command
282 * @ret rc Return status code
284 static int aoe_command ( struct ata_device *ata,
285 struct ata_command *command ) {
286 struct aoe_session *aoe =
287 container_of ( ata->backend, struct aoe_session, refcnt );
290 aoe->command = command;
292 aoe->command_offset = 0;
293 aoe_send_command ( aoe );
295 aoe->rc = -EINPROGRESS;
296 while ( aoe->rc == -EINPROGRESS )
303 static int aoe_detached_command ( struct ata_device *ata __unused,
304 struct ata_command *command __unused ) {
308 void aoe_detach ( struct ata_device *ata ) {
309 struct aoe_session *aoe =
310 container_of ( ata->backend, struct aoe_session, refcnt );
312 stop_timer ( &aoe->timer );
313 ata->command = aoe_detached_command;
314 list_del ( &aoe->list );
315 ref_put ( ata->backend );
319 static int aoe_parse_root_path ( struct aoe_session *aoe,
320 const char *root_path ) {
323 if ( strncmp ( root_path, "aoe:", 4 ) != 0 )
325 ptr = ( ( char * ) root_path + 4 );
330 aoe->major = strtoul ( ptr, &ptr, 10 );
334 aoe->minor = strtoul ( ptr, &ptr, 10 );
341 int aoe_attach ( struct ata_device *ata, struct net_device *netdev,
342 const char *root_path ) {
343 struct aoe_session *aoe;
346 /* Allocate and initialise structure */
347 aoe = zalloc ( sizeof ( *aoe ) );
350 aoe->refcnt.free = aoe_free;
351 aoe->netdev = netdev_get ( netdev );
352 memcpy ( aoe->target, ethernet_protocol.ll_broadcast,
353 sizeof ( aoe->target ) );
354 aoe->tag = AOE_TAG_MAGIC;
355 aoe->timer.expired = aoe_timer_expired;
357 /* Parse root path */
358 if ( ( rc = aoe_parse_root_path ( aoe, root_path ) ) != 0 )
361 /* Attach parent interface, transfer reference to connection
364 ata->backend = ref_get ( &aoe->refcnt );
365 ata->command = aoe_command;
366 list_add ( &aoe->list, &aoe_sessions );
370 ref_put ( &aoe->refcnt );