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 return net_transmit_via ( pkb, aoe->netdev );
122 * Handle AoE response
125 * @v aoehdr AoE header
126 * @ret rc Return status code
128 static int aoe_rx_response ( struct aoe_session *aoe, struct aoehdr *aoehdr,
130 struct aoecmd *aoecmd = aoehdr->arg.command;
131 struct ata_command *command = aoe->command;
132 unsigned int rx_data_len;
134 unsigned int data_len;
137 if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecmd ) ) )
139 rx_data_len = ( len - sizeof ( *aoehdr ) - sizeof ( *aoecmd ) );
141 /* Check for fatal errors */
142 if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
143 aoe_done ( aoe, -EIO );
147 /* Calculate count and data_len for this subcommand */
148 count = command->cb.count.native;
149 if ( count > AOE_MAX_COUNT )
150 count = AOE_MAX_COUNT;
151 data_len = count * ATA_SECTOR_SIZE;
153 /* Merge into overall ATA status */
154 aoe->status |= aoecmd->cmd_stat;
156 /* Copy data payload */
157 if ( command->data_in ) {
158 if ( rx_data_len > data_len )
159 rx_data_len = data_len;
160 copy_to_user ( command->data_in, aoe->command_offset,
161 aoecmd->data, rx_data_len );
164 /* Update ATA command and offset */
165 aoe->command_offset += data_len;
166 command->cb.lba.native += count;
167 command->cb.count.native -= count;
169 /* Check for operation complete */
170 if ( ! command->cb.count.native ) {
175 /* Transmit next portion of request */
176 aoe_send_command ( aoe );
182 * Process incoming AoE packets
184 * @v pkb Packet buffer
185 * @ret rc Return status code
188 static int aoe_rx ( struct pk_buff *pkb ) {
189 struct aoehdr *aoehdr = pkb->data;
190 unsigned int len = pkb_len ( pkb );
191 struct aoe_session *aoe;
195 if ( len < sizeof ( *aoehdr ) ) {
199 if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
200 rc = -EPROTONOSUPPORT;
203 if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
204 /* Ignore AoE requests that we happen to see */
208 /* Demultiplex amongst active AoE sessions */
209 list_for_each_entry ( aoe, &aoe_sessions, list ) {
210 if ( ntohs ( aoehdr->major ) != aoe->major )
212 if ( aoehdr->minor != aoe->minor )
214 if ( ntohl ( aoehdr->tag ) != aoe->tag )
217 #warning "Need a way to get the MAC address for future reference"
219 rc = aoe_rx_response ( aoe, aoehdr, len );
229 * Perform AoE network-layer routing
231 * @v pkb Packet buffer
232 * @ret source Network-layer source address
233 * @ret dest Network-layer destination address
234 * @ret rc Return status code
236 static int aoe_route ( const struct pk_buff *pkb __unused,
237 struct net_header *nethdr ) {
239 #warning "Need a way to find out the MAC address"
240 nethdr->flags = PKT_FL_BROADCAST;
245 struct net_protocol aoe_protocol = {
247 .net_proto = htons ( ETH_P_AOE ),
248 .rx_process = aoe_rx,
252 NET_PROTOCOL ( aoe_protocol );
259 void aoe_open ( struct aoe_session *aoe ) {
260 list_add ( &aoe->list, &aoe_sessions );
268 void aoe_close ( struct aoe_session *aoe ) {
269 list_del ( &aoe->list );
273 * Issue ATA command via an open AoE session
276 * @v command ATA command
277 * @ret aop Asynchronous operation
279 * Only one command may be issued concurrently per session. This call
280 * is non-blocking; use async_wait() to wait for the command to
283 struct async_operation * aoe_issue ( struct aoe_session *aoe,
284 struct ata_command *command ) {
285 aoe->command = command;
287 aoe->command_offset = 0;
288 aoe_send_command ( aoe );