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/pkbuff.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->aop, 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;
74 struct aoehdr *aoehdr;
75 struct aoecmd *aoecmd;
77 unsigned int data_out_len;
79 /* Calculate count and data_out_len for this subcommand */
80 count = command->cb.count.native;
81 if ( count > AOE_MAX_COUNT )
82 count = AOE_MAX_COUNT;
83 data_out_len = ( command->data_out ? ( count * ATA_SECTOR_SIZE ) : 0 );
85 /* Create outgoing packet buffer */
86 pkb = alloc_pkb ( ETH_HLEN + sizeof ( *aoehdr ) + sizeof ( *aoecmd ) +
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_tx ( pkb, aoe->netdev, &aoe_protocol, aoe->target );
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 * @v netdev Network device
212 * @v ll_source Link-layer source address
213 * @ret rc Return status code
216 static int aoe_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
217 const void *ll_source ) {
218 struct aoehdr *aoehdr = pkb->data;
219 unsigned int len = pkb_len ( pkb );
220 struct aoe_session *aoe;
224 if ( len < sizeof ( *aoehdr ) ) {
228 if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
229 rc = -EPROTONOSUPPORT;
232 if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
233 /* Ignore AoE requests that we happen to see */
237 /* Demultiplex amongst active AoE sessions */
238 list_for_each_entry ( aoe, &aoe_sessions, list ) {
239 if ( ntohs ( aoehdr->major ) != aoe->major )
241 if ( aoehdr->minor != aoe->minor )
243 if ( ntohl ( aoehdr->tag ) != aoe->tag )
245 memcpy ( aoe->target, ll_source, sizeof ( aoe->target ) );
246 rc = aoe_rx_response ( aoe, aoehdr, len );
256 struct net_protocol aoe_protocol __net_protocol = {
258 .net_proto = htons ( ETH_P_AOE ),
267 void aoe_open ( struct aoe_session *aoe ) {
268 memcpy ( aoe->target, ethernet_protocol.ll_broadcast,
269 sizeof ( aoe->target ) );
270 aoe->tag = AOE_TAG_MAGIC;
271 aoe->timer.expired = aoe_timer_expired;
272 list_add ( &aoe->list, &aoe_sessions );
280 void aoe_close ( struct aoe_session *aoe ) {
281 list_del ( &aoe->list );
285 * Issue ATA command via an open AoE session
288 * @v command ATA command
289 * @ret aop Asynchronous operation
291 * Only one command may be issued concurrently per session. This call
292 * is non-blocking; use async_wait() to wait for the command to
295 struct async_operation * aoe_issue ( struct aoe_session *aoe,
296 struct ata_command *command ) {
297 aoe->command = command;
299 aoe->command_offset = 0;
300 aoe_send_command ( aoe );