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/pkbuff.h>
28 #include <gpxe/uaccess.h>
30 #include <gpxe/netdevice.h>
31 #include <gpxe/async.h>
40 struct net_protocol aoe_protocol;
42 /** List of all AoE sessions */
43 static LIST_HEAD ( aoe_sessions );
46 * Mark current AoE command complete
49 * @v rc Return status code
51 static void aoe_done ( struct aoe_session *aoe, int rc ) {
53 /* Record overall command status */
54 aoe->command->cb.cmd_stat = aoe->status;
57 /* Mark async operation as complete */
58 async_done ( &aoe->aop, rc );
65 * @ret rc Return status code
67 * This transmits an AoE command packet. It does not wait for a
70 static int aoe_send_command ( struct aoe_session *aoe ) {
71 struct ata_command *command = aoe->command;
73 struct aoehdr *aoehdr;
74 struct aoecmd *aoecmd;
76 unsigned int data_out_len;
78 /* Calculate count and data_out_len for this subcommand */
79 count = command->cb.count.native;
80 if ( count > AOE_MAX_COUNT )
81 count = AOE_MAX_COUNT;
82 data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
84 /* Create outgoing packet buffer */
85 pkb = alloc_pkb ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
89 pkb->net_protocol = &aoe_protocol;
90 pkb_reserve ( pkb, ETH_HLEN );
91 aoehdr = pkb_put ( pkb, sizeof ( *aoehdr ) );
92 aoecmd = pkb_put ( pkb, sizeof ( *aoecmd ) );
93 memset ( aoehdr, 0, ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) );
96 aoehdr->ver_flags = AOE_VERSION;
97 aoehdr->major = htons ( aoe->major );
98 aoehdr->minor = aoe->minor;
99 aoehdr->tag = htonl ( ++aoe->tag );
101 /* Fill AoE command */
102 linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
103 aoecmd->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
104 ( command->cb.device & ATA_DEV_SLAVE ) |
105 ( data_out_len ? AOE_FL_WRITE : 0 ) );
106 aoecmd->err_feat = command->cb.err_feat.bytes.cur;
107 aoecmd->count = count;
108 aoecmd->cmd_stat = command->cb.cmd_stat;
109 aoecmd->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
110 if ( ! command->cb.lba48 )
111 aoecmd->lba.bytes[3] |= ( command->cb.device & ATA_DEV_MASK );
113 /* Fill data payload */
114 copy_from_user ( pkb_put ( pkb, data_out_len ), command->data_out,
115 aoe->command_offset, data_out_len );
118 start_timer ( &aoe->timer );
119 return net_transmit_via ( pkb, aoe->netdev );
123 * Handle AoE retry timer expiry
125 * @v timer AoE retry timer
126 * @v fail Failure indicator
128 static void aoe_timer_expired ( struct retry_timer *timer, int fail ) {
129 struct aoe_session *aoe =
130 container_of ( timer, struct aoe_session, timer );
133 aoe_done ( aoe, -ETIMEDOUT );
135 aoe_send_command ( aoe );
140 * Handle AoE response
143 * @v aoehdr AoE header
144 * @ret rc Return status code
146 static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
148 struct aoecmd *aoecmd = aoehdr->arg.command;
149 struct ata_command *command = aoe->command;
150 unsigned int rx_data_len;
152 unsigned int data_len;
155 if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) ) {
156 /* Ignore packet; allow timer to trigger retransmit */
159 rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
161 /* Stop retry timer. After this point, every code path must
162 * either terminate the AoE operation via aoe_done(), or
163 * transmit a new packet.
165 stop_timer ( &aoe->timer );
167 /* Check for fatal errors */
168 if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
169 aoe_done ( aoe, -EIO );
173 /* Calculate count and data_len for this subcommand */
174 count = command->cb.count.native;
175 if ( count > AOE_MAX_COUNT )
176 count = AOE_MAX_COUNT;
177 data_len = count * ATA_SECTOR_SIZE;
179 /* Merge into overall ATA status */
180 aoe->status |= aoecmd->cmd_stat;
182 /* Copy data payload */
183 if ( command->data_in ) {
184 if ( rx_data_len > data_len )
185 rx_data_len = data_len;
186 copy_to_user ( command->data_in, aoe->command_offset,
187 aoecmd->data, rx_data_len );
190 /* Update ATA command and offset */
191 aoe->command_offset += data_len;
192 command->cb.lba.native += count;
193 command->cb.count.native -= count;
195 /* Check for operation complete */
196 if ( ! command->cb.count.native ) {
201 /* Transmit next portion of request */
202 aoe_send_command ( aoe );
208 * Process incoming AoE packets
210 * @v pkb Packet buffer
211 * @ret rc Return status code
214 static int aoe_rx ( struct pk_buff *pkb ) {
215 struct aoehdr *aoehdr = pkb->data;
216 unsigned int len = pkb_len ( pkb );
217 struct ethhdr *ethhdr = pkb_push ( pkb, sizeof ( *ethhdr ) );
218 struct aoe_session *aoe;
222 if ( len < sizeof ( *aoehdr ) ) {
226 if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
227 rc = -EPROTONOSUPPORT;
230 if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
231 /* Ignore AoE requests that we happen to see */
235 /* Demultiplex amongst active AoE sessions */
236 list_for_each_entry ( aoe, &aoe_sessions, list ) {
237 if ( ntohs ( aoehdr->major ) != aoe->major )
239 if ( aoehdr->minor != aoe->minor )
241 if ( ntohl ( aoehdr->tag ) != aoe->tag )
243 memcpy ( aoe->target, ethhdr->h_source,
244 sizeof ( aoe->target ) );
245 rc = aoe_rx_response ( aoe, aoehdr, len );
255 * Perform AoE network-layer routing
257 * @v pkb Packet buffer
258 * @ret source Network-layer source address
259 * @ret dest Network-layer destination address
260 * @ret rc Return status code
262 static int aoe_route ( const struct pk_buff *pkb __unused,
263 struct net_header *nethdr ) {
264 struct aoehdr *aoehdr = pkb->data;
265 struct aoe_session *aoe;
267 list_for_each_entry ( aoe, &aoe_sessions, list ) {
268 if ( ( ntohs ( aoehdr->major ) == aoe->major ) &&
269 ( aoehdr->minor == aoe->minor ) ) {
270 nethdr->flags = PKT_FL_RAW_ADDR;
271 memcpy ( nethdr->dest_net_addr, aoe->target,
272 sizeof ( aoe->target ) );
277 return -EHOSTUNREACH;
281 struct net_protocol aoe_protocol = {
283 .net_proto = htons ( ETH_P_AOE ),
284 .rx_process = aoe_rx,
288 NET_PROTOCOL ( aoe_protocol );
295 void aoe_open ( struct aoe_session *aoe ) {
296 memset ( aoe->target, 0xff, sizeof ( aoe->target ) );
297 aoe->timer.expired = aoe_timer_expired;
298 list_add ( &aoe->list, &aoe_sessions );
306 void aoe_close ( struct aoe_session *aoe ) {
307 list_del ( &aoe->list );
311 * Issue ATA command via an open AoE session
314 * @v command ATA command
315 * @ret aop Asynchronous operation
317 * Only one command may be issued concurrently per session. This call
318 * is non-blocking; use async_wait() to wait for the command to
321 struct async_operation * aoe_issue ( struct aoe_session *aoe,
322 struct ata_command *command ) {
323 aoe->command = command;
325 aoe->command_offset = 0;
326 aoe_send_command ( aoe );