These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / net / aoe.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stddef.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <byteswap.h>
33 #include <ipxe/list.h>
34 #include <ipxe/if_ether.h>
35 #include <ipxe/iobuf.h>
36 #include <ipxe/uaccess.h>
37 #include <ipxe/netdevice.h>
38 #include <ipxe/features.h>
39 #include <ipxe/interface.h>
40 #include <ipxe/xfer.h>
41 #include <ipxe/uri.h>
42 #include <ipxe/open.h>
43 #include <ipxe/ata.h>
44 #include <ipxe/device.h>
45 #include <ipxe/aoe.h>
46
47 /** @file
48  *
49  * AoE protocol
50  *
51  */
52
53 FEATURE ( FEATURE_PROTOCOL, "AoE", DHCP_EB_FEATURE_AOE, 1 );
54
55 struct net_protocol aoe_protocol __net_protocol;
56
57 /******************************************************************************
58  *
59  * AoE devices and commands
60  *
61  ******************************************************************************
62  */
63
64 /** List of all AoE devices */
65 static LIST_HEAD ( aoe_devices );
66
67 /** List of active AoE commands */
68 static LIST_HEAD ( aoe_commands );
69
70 /** An AoE device */
71 struct aoe_device {
72         /** Reference counter */
73         struct refcnt refcnt;
74
75         /** Network device */
76         struct net_device *netdev;
77         /** ATA command issuing interface */
78         struct interface ata;
79
80         /** Major number */
81         uint16_t major;
82         /** Minor number */
83         uint8_t minor;
84         /** Target MAC address */
85         uint8_t target[MAX_LL_ADDR_LEN];
86
87         /** Saved timeout value */
88         unsigned long timeout;
89
90         /** Configuration command interface */
91         struct interface config;
92         /** Device is configued */
93         int configured;
94 };
95
96 /** An AoE command */
97 struct aoe_command {
98         /** Reference count */
99         struct refcnt refcnt;
100         /** AOE device */
101         struct aoe_device *aoedev;
102         /** List of active commands */
103         struct list_head list;
104
105         /** ATA command interface */
106         struct interface ata;
107
108         /** ATA command */
109         struct ata_cmd command;
110         /** Command type */
111         struct aoe_command_type *type;
112         /** Command tag */
113         uint32_t tag;
114
115         /** Retransmission timer */
116         struct retry_timer timer;
117 };
118
119 /** An AoE command type */
120 struct aoe_command_type {
121         /**
122          * Calculate length of AoE command IU
123          *
124          * @v aoecmd            AoE command
125          * @ret len             Length of command IU
126          */
127         size_t ( * cmd_len ) ( struct aoe_command *aoecmd );
128         /**
129          * Build AoE command IU
130          *
131          * @v aoecmd            AoE command
132          * @v data              Command IU
133          * @v len               Length of command IU
134          */
135         void ( * cmd ) ( struct aoe_command *aoecmd, void *data, size_t len );
136         /**
137          * Handle AoE response IU
138          *
139          * @v aoecmd            AoE command
140          * @v data              Response IU
141          * @v len               Length of response IU
142          * @v ll_source         Link-layer source address
143          * @ret rc              Return status code
144          */
145         int ( * rsp ) ( struct aoe_command *aoecmd, const void *data,
146                         size_t len, const void *ll_source );
147 };
148
149 /**
150  * Get reference to AoE device
151  *
152  * @v aoedev            AoE device
153  * @ret aoedev          AoE device
154  */
155 static inline __attribute__ (( always_inline )) struct aoe_device *
156 aoedev_get ( struct aoe_device *aoedev ) {
157         ref_get ( &aoedev->refcnt );
158         return aoedev;
159 }
160
161 /**
162  * Drop reference to AoE device
163  *
164  * @v aoedev            AoE device
165  */
166 static inline __attribute__ (( always_inline )) void
167 aoedev_put ( struct aoe_device *aoedev ) {
168         ref_put ( &aoedev->refcnt );
169 }
170
171 /**
172  * Get reference to AoE command
173  *
174  * @v aoecmd            AoE command
175  * @ret aoecmd          AoE command
176  */
177 static inline __attribute__ (( always_inline )) struct aoe_command *
178 aoecmd_get ( struct aoe_command *aoecmd ) {
179         ref_get ( &aoecmd->refcnt );
180         return aoecmd;
181 }
182
183 /**
184  * Drop reference to AoE command
185  *
186  * @v aoecmd            AoE command
187  */
188 static inline __attribute__ (( always_inline )) void
189 aoecmd_put ( struct aoe_command *aoecmd ) {
190         ref_put ( &aoecmd->refcnt );
191 }
192
193 /**
194  * Name AoE device
195  *
196  * @v aoedev            AoE device
197  * @ret name            AoE device name
198  */
199 static const char * aoedev_name ( struct aoe_device *aoedev ) {
200         static char buf[16];
201
202         snprintf ( buf, sizeof ( buf ), "%s/e%d.%d", aoedev->netdev->name,
203                    aoedev->major, aoedev->minor );
204         return buf;
205 }
206
207 /**
208  * Free AoE command
209  *
210  * @v refcnt            Reference counter
211  */
212 static void aoecmd_free ( struct refcnt *refcnt ) {
213         struct aoe_command *aoecmd =
214                 container_of ( refcnt, struct aoe_command, refcnt );
215
216         assert ( ! timer_running ( &aoecmd->timer ) );
217         assert ( list_empty ( &aoecmd->list ) );
218
219         aoedev_put ( aoecmd->aoedev );
220         free ( aoecmd );
221 }
222
223 /**
224  * Close AoE command
225  *
226  * @v aoecmd            AoE command
227  * @v rc                Reason for close
228  */
229 static void aoecmd_close ( struct aoe_command *aoecmd, int rc ) {
230         struct aoe_device *aoedev = aoecmd->aoedev;
231
232         /* Stop timer */
233         stop_timer ( &aoecmd->timer );
234
235         /* Preserve the timeout value for subsequent commands */
236         aoedev->timeout = aoecmd->timer.timeout;
237
238         /* Remove from list of commands */
239         if ( ! list_empty ( &aoecmd->list ) ) {
240                 list_del ( &aoecmd->list );
241                 INIT_LIST_HEAD ( &aoecmd->list );
242                 aoecmd_put ( aoecmd );
243         }
244
245         /* Shut down interfaces */
246         intf_shutdown ( &aoecmd->ata, rc );
247 }
248
249 /**
250  * Transmit AoE command request
251  *
252  * @v aoecmd            AoE command
253  * @ret rc              Return status code
254  */
255 static int aoecmd_tx ( struct aoe_command *aoecmd ) {
256         struct aoe_device *aoedev = aoecmd->aoedev;
257         struct net_device *netdev = aoedev->netdev;
258         struct io_buffer *iobuf;
259         struct aoehdr *aoehdr;
260         size_t cmd_len;
261         int rc;
262
263         /* Sanity check */
264         assert ( netdev != NULL );
265
266         /* If we are transmitting anything that requires a response,
267          * start the retransmission timer.  Do this before attempting
268          * to allocate the I/O buffer, in case allocation itself
269          * fails.
270          */
271         start_timer ( &aoecmd->timer );
272
273         /* Create outgoing I/O buffer */
274         cmd_len = aoecmd->type->cmd_len ( aoecmd );
275         iobuf = alloc_iob ( MAX_LL_HEADER_LEN + cmd_len );
276         if ( ! iobuf )
277                 return -ENOMEM;
278         iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
279         aoehdr = iob_put ( iobuf, cmd_len );
280
281         /* Fill AoE header */
282         memset ( aoehdr, 0, sizeof ( *aoehdr ) );
283         aoehdr->ver_flags = AOE_VERSION;
284         aoehdr->major = htons ( aoedev->major );
285         aoehdr->minor = aoedev->minor;
286         aoehdr->tag = htonl ( aoecmd->tag );
287         aoecmd->type->cmd ( aoecmd, iobuf->data, iob_len ( iobuf ) );
288
289         /* Send packet */
290         if ( ( rc = net_tx ( iobuf, netdev, &aoe_protocol, aoedev->target,
291                              netdev->ll_addr ) ) != 0 ) {
292                 DBGC ( aoedev, "AoE %s/%08x could not transmit: %s\n",
293                        aoedev_name ( aoedev ), aoecmd->tag,
294                        strerror ( rc ) );
295                 return rc;
296         }
297
298         return 0;
299 }
300
301 /**
302  * Receive AoE command response
303  *
304  * @v aoecmd            AoE command
305  * @v iobuf             I/O buffer
306  * @v ll_source         Link-layer source address
307  * @ret rc              Return status code
308  */
309 static int aoecmd_rx ( struct aoe_command *aoecmd, struct io_buffer *iobuf,
310                        const void *ll_source ) {
311         struct aoe_device *aoedev = aoecmd->aoedev;
312         struct aoehdr *aoehdr = iobuf->data;
313         int rc;
314
315         /* Sanity check */
316         if ( iob_len ( iobuf ) < sizeof ( *aoehdr ) ) {
317                 DBGC ( aoedev, "AoE %s/%08x received underlength response "
318                        "(%zd bytes)\n", aoedev_name ( aoedev ),
319                        aoecmd->tag, iob_len ( iobuf ) );
320                 rc = -EINVAL;
321                 goto done;
322         }
323         if ( ( ntohs ( aoehdr->major ) != aoedev->major ) ||
324              ( aoehdr->minor != aoedev->minor ) ) {
325                 DBGC ( aoedev, "AoE %s/%08x received response for incorrect "
326                        "device e%d.%d\n", aoedev_name ( aoedev ), aoecmd->tag,
327                        ntohs ( aoehdr->major ), aoehdr->minor );
328                 rc = -EINVAL;
329                 goto done;
330         }
331
332         /* Catch command failures */
333         if ( aoehdr->ver_flags & AOE_FL_ERROR ) {
334                 DBGC ( aoedev, "AoE %s/%08x terminated in error\n",
335                        aoedev_name ( aoedev ), aoecmd->tag );
336                 aoecmd_close ( aoecmd, -EIO );
337                 rc = -EIO;
338                 goto done;
339         }
340
341         /* Hand off to command completion handler */
342         if ( ( rc = aoecmd->type->rsp ( aoecmd, iobuf->data, iob_len ( iobuf ),
343                                         ll_source ) ) != 0 )
344                 goto done;
345
346  done:
347         /* Free I/O buffer */
348         free_iob ( iobuf );
349
350         /* Terminate command */
351         aoecmd_close ( aoecmd, rc );
352
353         return rc;
354 }
355
356 /**
357  * Handle AoE retry timer expiry
358  *
359  * @v timer             AoE retry timer
360  * @v fail              Failure indicator
361  */
362 static void aoecmd_expired ( struct retry_timer *timer, int fail ) {
363         struct aoe_command *aoecmd =
364                 container_of ( timer, struct aoe_command, timer );
365
366         if ( fail ) {
367                 aoecmd_close ( aoecmd, -ETIMEDOUT );
368         } else {
369                 aoecmd_tx ( aoecmd );
370         }
371 }
372
373 /**
374  * Calculate length of AoE ATA command IU
375  *
376  * @v aoecmd            AoE command
377  * @ret len             Length of command IU
378  */
379 static size_t aoecmd_ata_cmd_len ( struct aoe_command *aoecmd ) {
380         struct ata_cmd *command = &aoecmd->command;
381
382         return ( sizeof ( struct aoehdr ) + sizeof ( struct aoeata ) +
383                  command->data_out_len );
384 }
385
386 /**
387  * Build AoE ATA command IU
388  *
389  * @v aoecmd            AoE command
390  * @v data              Command IU
391  * @v len               Length of command IU
392  */
393 static void aoecmd_ata_cmd ( struct aoe_command *aoecmd,
394                              void *data, size_t len ) {
395         struct aoe_device *aoedev = aoecmd->aoedev;
396         struct ata_cmd *command = &aoecmd->command;
397         struct aoehdr *aoehdr = data;
398         struct aoeata *aoeata = &aoehdr->payload[0].ata;
399
400         /* Sanity check */
401         linker_assert ( AOE_FL_DEV_HEAD == ATA_DEV_SLAVE, __fix_ata_h__ );
402         assert ( len == ( sizeof ( *aoehdr ) + sizeof ( *aoeata ) +
403                           command->data_out_len ) );
404
405         /* Build IU */
406         aoehdr->command = AOE_CMD_ATA;
407         memset ( aoeata, 0, sizeof ( *aoeata ) );
408         aoeata->aflags = ( ( command->cb.lba48 ? AOE_FL_EXTENDED : 0 ) |
409                            ( command->cb.device & ATA_DEV_SLAVE ) |
410                            ( command->data_out_len ? AOE_FL_WRITE : 0 ) );
411         aoeata->err_feat = command->cb.err_feat.bytes.cur;
412         aoeata->count = command->cb.count.native;
413         aoeata->cmd_stat = command->cb.cmd_stat;
414         aoeata->lba.u64 = cpu_to_le64 ( command->cb.lba.native );
415         if ( ! command->cb.lba48 )
416                 aoeata->lba.bytes[3] |=
417                         ( command->cb.device & ATA_DEV_MASK );
418         copy_from_user ( aoeata->data, command->data_out, 0,
419                          command->data_out_len );
420
421         DBGC2 ( aoedev, "AoE %s/%08x ATA cmd %02x:%02x:%02x:%02x:%08llx",
422                 aoedev_name ( aoedev ), aoecmd->tag, aoeata->aflags,
423                 aoeata->err_feat, aoeata->count, aoeata->cmd_stat,
424                 aoeata->lba.u64 );
425         if ( command->data_out_len )
426                 DBGC2 ( aoedev, " out %04zx", command->data_out_len );
427         if ( command->data_in_len )
428                 DBGC2 ( aoedev, " in %04zx", command->data_in_len );
429         DBGC2 ( aoedev, "\n" );
430 }
431
432 /**
433  * Handle AoE ATA response IU
434  *
435  * @v aoecmd            AoE command
436  * @v data              Response IU
437  * @v len               Length of response IU
438  * @v ll_source         Link-layer source address
439  * @ret rc              Return status code
440  */
441 static int aoecmd_ata_rsp ( struct aoe_command *aoecmd, const void *data,
442                             size_t len, const void *ll_source __unused ) {
443         struct aoe_device *aoedev = aoecmd->aoedev;
444         struct ata_cmd *command = &aoecmd->command;
445         const struct aoehdr *aoehdr = data;
446         const struct aoeata *aoeata = &aoehdr->payload[0].ata;
447         size_t data_len;
448
449         /* Sanity check */
450         if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoeata ) ) ) {
451                 DBGC ( aoedev, "AoE %s/%08x received underlength ATA response "
452                        "(%zd bytes)\n", aoedev_name ( aoedev ),
453                        aoecmd->tag, len );
454                 return -EINVAL;
455         }
456         data_len = ( len - ( sizeof ( *aoehdr ) + sizeof ( *aoeata ) ) );
457         DBGC2 ( aoedev, "AoE %s/%08x ATA rsp %02x in %04zx\n",
458                 aoedev_name ( aoedev ), aoecmd->tag, aoeata->cmd_stat,
459                 data_len );
460
461         /* Check for command failure */
462         if ( aoeata->cmd_stat & ATA_STAT_ERR ) {
463                 DBGC ( aoedev, "AoE %s/%08x status %02x\n",
464                        aoedev_name ( aoedev ), aoecmd->tag, aoeata->cmd_stat );
465                 return -EIO;
466         }
467
468         /* Check data-in length is sufficient.  (There may be trailing
469          * garbage due to Ethernet minimum-frame-size padding.)
470          */
471         if ( data_len < command->data_in_len ) {
472                 DBGC ( aoedev, "AoE %s/%08x data-in underrun (received %zd, "
473                        "expected %zd)\n", aoedev_name ( aoedev ), aoecmd->tag,
474                        data_len, command->data_in_len );
475                 return -ERANGE;
476         }
477
478         /* Copy out data payload */
479         copy_to_user ( command->data_in, 0, aoeata->data,
480                        command->data_in_len );
481
482         return 0;
483 }
484
485 /** AoE ATA command */
486 static struct aoe_command_type aoecmd_ata = {
487         .cmd_len = aoecmd_ata_cmd_len,
488         .cmd = aoecmd_ata_cmd,
489         .rsp = aoecmd_ata_rsp,
490 };
491
492 /**
493  * Calculate length of AoE configuration command IU
494  *
495  * @v aoecmd            AoE command
496  * @ret len             Length of command IU
497  */
498 static size_t aoecmd_cfg_cmd_len ( struct aoe_command *aoecmd __unused ) {
499         return ( sizeof ( struct aoehdr ) + sizeof ( struct aoecfg ) );
500 }
501
502 /**
503  * Build AoE configuration command IU
504  *
505  * @v aoecmd            AoE command
506  * @v data              Command IU
507  * @v len               Length of command IU
508  */
509 static void aoecmd_cfg_cmd ( struct aoe_command *aoecmd,
510                              void *data, size_t len ) {
511         struct aoe_device *aoedev = aoecmd->aoedev;
512         struct aoehdr *aoehdr = data;
513         struct aoecfg *aoecfg = &aoehdr->payload[0].cfg;
514
515         /* Sanity check */
516         assert ( len == ( sizeof ( *aoehdr ) + sizeof ( *aoecfg ) ) );
517
518         /* Build IU */
519         aoehdr->command = AOE_CMD_CONFIG;
520         memset ( aoecfg, 0, sizeof ( *aoecfg ) );
521
522         DBGC ( aoedev, "AoE %s/%08x CONFIG cmd\n",
523                aoedev_name ( aoedev ), aoecmd->tag );
524 }
525
526 /**
527  * Handle AoE configuration response IU
528  *
529  * @v aoecmd            AoE command
530  * @v data              Response IU
531  * @v len               Length of response IU
532  * @v ll_source         Link-layer source address
533  * @ret rc              Return status code
534  */
535 static int aoecmd_cfg_rsp ( struct aoe_command *aoecmd, const void *data,
536                             size_t len, const void *ll_source ) {
537         struct aoe_device *aoedev = aoecmd->aoedev;
538         struct ll_protocol *ll_protocol = aoedev->netdev->ll_protocol;
539         const struct aoehdr *aoehdr = data;
540         const struct aoecfg *aoecfg = &aoehdr->payload[0].cfg;
541
542         /* Sanity check */
543         if ( len < ( sizeof ( *aoehdr ) + sizeof ( *aoecfg ) ) ) {
544                 DBGC ( aoedev, "AoE %s/%08x received underlength "
545                        "configuration response (%zd bytes)\n",
546                        aoedev_name ( aoedev ), aoecmd->tag, len );
547                 return -EINVAL;
548         }
549         DBGC ( aoedev, "AoE %s/%08x CONFIG rsp buf %04x fw %04x scnt %02x\n",
550                aoedev_name ( aoedev ), aoecmd->tag, ntohs ( aoecfg->bufcnt ),
551                aoecfg->fwver, aoecfg->scnt );
552
553         /* Record target MAC address */
554         memcpy ( aoedev->target, ll_source, ll_protocol->ll_addr_len );
555         DBGC ( aoedev, "AoE %s has MAC address %s\n",
556                aoedev_name ( aoedev ), ll_protocol->ntoa ( aoedev->target ) );
557
558         return 0;
559 }
560
561 /** AoE configuration command */
562 static struct aoe_command_type aoecmd_cfg = {
563         .cmd_len = aoecmd_cfg_cmd_len,
564         .cmd = aoecmd_cfg_cmd,
565         .rsp = aoecmd_cfg_rsp,
566 };
567
568 /** AoE command ATA interface operations */
569 static struct interface_operation aoecmd_ata_op[] = {
570         INTF_OP ( intf_close, struct aoe_command *, aoecmd_close ),
571 };
572
573 /** AoE command ATA interface descriptor */
574 static struct interface_descriptor aoecmd_ata_desc =
575         INTF_DESC ( struct aoe_command, ata, aoecmd_ata_op );
576
577 /**
578  * Identify AoE command by tag
579  *
580  * @v tag               Command tag
581  * @ret aoecmd          AoE command, or NULL
582  */
583 static struct aoe_command * aoecmd_find_tag ( uint32_t tag ) {
584         struct aoe_command *aoecmd;
585
586         list_for_each_entry ( aoecmd, &aoe_commands, list ) {
587                 if ( aoecmd->tag == tag )
588                         return aoecmd;
589         }
590         return NULL;
591 }
592
593 /**
594  * Choose an AoE command tag
595  *
596  * @ret tag             New tag, or negative error
597  */
598 static int aoecmd_new_tag ( void ) {
599         static uint16_t tag_idx;
600         unsigned int i;
601
602         for ( i = 0 ; i < 65536 ; i++ ) {
603                 tag_idx++;
604                 if ( aoecmd_find_tag ( tag_idx ) == NULL )
605                         return ( AOE_TAG_MAGIC | tag_idx );
606         }
607         return -EADDRINUSE;
608 }
609
610 /**
611  * Create AoE command
612  *
613  * @v aoedev            AoE device
614  * @v type              AoE command type
615  * @ret aoecmd          AoE command
616  */
617 static struct aoe_command * aoecmd_create ( struct aoe_device *aoedev,
618                                             struct aoe_command_type *type ) {
619         struct aoe_command *aoecmd;
620         int tag;
621
622         /* Allocate command tag */
623         tag = aoecmd_new_tag();
624         if ( tag < 0 )
625                 return NULL;
626
627         /* Allocate and initialise structure */
628         aoecmd = zalloc ( sizeof ( *aoecmd ) );
629         if ( ! aoecmd )
630                 return NULL;
631         ref_init ( &aoecmd->refcnt, aoecmd_free );
632         list_add ( &aoecmd->list, &aoe_commands );
633         intf_init ( &aoecmd->ata, &aoecmd_ata_desc, &aoecmd->refcnt );
634         timer_init ( &aoecmd->timer, aoecmd_expired, &aoecmd->refcnt );
635         aoecmd->aoedev = aoedev_get ( aoedev );
636         aoecmd->type = type;
637         aoecmd->tag = tag;
638
639         /* Preserve timeout from last completed command */
640         aoecmd->timer.timeout = aoedev->timeout;
641
642         /* Return already mortalised.  (Reference is held by command list.) */
643         return aoecmd;
644 }
645
646 /**
647  * Issue AoE ATA command
648  *
649  * @v aoedev            AoE device
650  * @v parent            Parent interface
651  * @v command           ATA command
652  * @ret tag             Command tag, or negative error
653  */
654 static int aoedev_ata_command ( struct aoe_device *aoedev,
655                                 struct interface *parent,
656                                 struct ata_cmd *command ) {
657         struct net_device *netdev = aoedev->netdev;
658         struct aoe_command *aoecmd;
659
660         /* Fail immediately if net device is closed */
661         if ( ! netdev_is_open ( netdev ) ) {
662                 DBGC ( aoedev, "AoE %s cannot issue command while net device "
663                        "is closed\n", aoedev_name ( aoedev ) );
664                 return -EWOULDBLOCK;
665         }
666
667         /* Create command */
668         aoecmd = aoecmd_create ( aoedev, &aoecmd_ata );
669         if ( ! aoecmd )
670                 return -ENOMEM;
671         memcpy ( &aoecmd->command, command, sizeof ( aoecmd->command ) );
672
673         /* Attempt to send command.  Allow failures to be handled by
674          * the retry timer.
675          */
676         aoecmd_tx ( aoecmd );
677
678         /* Attach to parent interface, leave reference with command
679          * list, and return.
680          */
681         intf_plug_plug ( &aoecmd->ata, parent );
682         return aoecmd->tag;
683 }
684
685 /**
686  * Issue AoE configuration command
687  *
688  * @v aoedev            AoE device
689  * @v parent            Parent interface
690  * @ret tag             Command tag, or negative error
691  */
692 static int aoedev_cfg_command ( struct aoe_device *aoedev,
693                                 struct interface *parent ) {
694         struct aoe_command *aoecmd;
695
696         /* Create command */
697         aoecmd = aoecmd_create ( aoedev, &aoecmd_cfg );
698         if ( ! aoecmd )
699                 return -ENOMEM;
700
701         /* Attempt to send command.  Allow failures to be handled by
702          * the retry timer.
703          */
704         aoecmd_tx ( aoecmd );
705
706         /* Attach to parent interface, leave reference with command
707          * list, and return.
708          */
709         intf_plug_plug ( &aoecmd->ata, parent );
710         return aoecmd->tag;
711 }
712
713 /**
714  * Free AoE device
715  *
716  * @v refcnt            Reference count
717  */
718 static void aoedev_free ( struct refcnt *refcnt ) {
719         struct aoe_device *aoedev =
720                 container_of ( refcnt, struct aoe_device, refcnt );
721
722         netdev_put ( aoedev->netdev );
723         free ( aoedev );
724 }
725
726 /**
727  * Close AoE device
728  *
729  * @v aoedev            AoE device
730  * @v rc                Reason for close
731  */
732 static void aoedev_close ( struct aoe_device *aoedev, int rc ) {
733         struct aoe_command *aoecmd;
734         struct aoe_command *tmp;
735
736         /* Shut down interfaces */
737         intf_shutdown ( &aoedev->ata, rc );
738         intf_shutdown ( &aoedev->config, rc );
739
740         /* Shut down any active commands */
741         list_for_each_entry_safe ( aoecmd, tmp, &aoe_commands, list ) {
742                 if ( aoecmd->aoedev != aoedev )
743                         continue;
744                 aoecmd_get ( aoecmd );
745                 aoecmd_close ( aoecmd, rc );
746                 aoecmd_put ( aoecmd );
747         }
748 }
749
750 /**
751  * Check AoE device flow-control window
752  *
753  * @v aoedev            AoE device
754  * @ret len             Length of window
755  */
756 static size_t aoedev_window ( struct aoe_device *aoedev ) {
757         return ( aoedev->configured ? ~( ( size_t ) 0 ) : 0 );
758 }
759
760 /**
761  * Handle AoE device configuration completion
762  *
763  * @v aoedev            AoE device
764  * @v rc                Reason for completion
765  */
766 static void aoedev_config_done ( struct aoe_device *aoedev, int rc ) {
767
768         /* Shut down interface */
769         intf_shutdown ( &aoedev->config, rc );
770
771         /* Close device on failure */
772         if ( rc != 0 ) {
773                 aoedev_close ( aoedev, rc );
774                 return;
775         }
776
777         /* Mark device as configured */
778         aoedev->configured = 1;
779         xfer_window_changed ( &aoedev->ata );
780 }
781
782 /**
783  * Identify device underlying AoE device
784  *
785  * @v aoedev            AoE device
786  * @ret device          Underlying device
787  */
788 static struct device * aoedev_identify_device ( struct aoe_device *aoedev ) {
789         return aoedev->netdev->dev;
790 }
791
792 /**
793  * Describe AoE device in an ACPI table
794  *
795  * @v aoedev            AoE device
796  * @v acpi              ACPI table
797  * @v len               Length of ACPI table
798  * @ret rc              Return status code
799  */
800 static int aoedev_describe ( struct aoe_device *aoedev,
801                              struct acpi_description_header *acpi,
802                              size_t len ) {
803         struct abft_table *abft =
804                 container_of ( acpi, struct abft_table, acpi );
805
806         /* Sanity check */
807         if ( len < sizeof ( *abft ) )
808                 return -ENOBUFS;
809
810         /* Populate table */
811         abft->acpi.signature = cpu_to_le32 ( ABFT_SIG );
812         abft->acpi.length = cpu_to_le32 ( sizeof ( *abft ) );
813         abft->acpi.revision = 1;
814         abft->shelf = cpu_to_le16 ( aoedev->major );
815         abft->slot = aoedev->minor;
816         memcpy ( abft->mac, aoedev->netdev->ll_addr, sizeof ( abft->mac ) );
817
818         return 0;
819 }
820
821 /** AoE device ATA interface operations */
822 static struct interface_operation aoedev_ata_op[] = {
823         INTF_OP ( ata_command, struct aoe_device *, aoedev_ata_command ),
824         INTF_OP ( xfer_window, struct aoe_device *, aoedev_window ),
825         INTF_OP ( intf_close, struct aoe_device *, aoedev_close ),
826         INTF_OP ( acpi_describe, struct aoe_device *, aoedev_describe ),
827         INTF_OP ( identify_device, struct aoe_device *,
828                   aoedev_identify_device ),
829 };
830
831 /** AoE device ATA interface descriptor */
832 static struct interface_descriptor aoedev_ata_desc =
833         INTF_DESC ( struct aoe_device, ata, aoedev_ata_op );
834
835 /** AoE device configuration interface operations */
836 static struct interface_operation aoedev_config_op[] = {
837         INTF_OP ( intf_close, struct aoe_device *, aoedev_config_done ),
838 };
839
840 /** AoE device configuration interface descriptor */
841 static struct interface_descriptor aoedev_config_desc =
842         INTF_DESC ( struct aoe_device, config, aoedev_config_op );
843
844 /**
845  * Open AoE device
846  *
847  * @v parent            Parent interface
848  * @v netdev            Network device
849  * @v major             Device major number
850  * @v minor             Device minor number
851  * @ret rc              Return status code
852  */
853 static int aoedev_open ( struct interface *parent, struct net_device *netdev,
854                          unsigned int major, unsigned int minor ) {
855         struct aoe_device *aoedev;
856         int rc;
857
858         /* Allocate and initialise structure */
859         aoedev = zalloc ( sizeof ( *aoedev ) );
860         if ( ! aoedev ) {
861                 rc = -ENOMEM;
862                 goto err_zalloc;
863         }
864         ref_init ( &aoedev->refcnt, aoedev_free );
865         intf_init ( &aoedev->ata, &aoedev_ata_desc, &aoedev->refcnt );
866         intf_init ( &aoedev->config, &aoedev_config_desc, &aoedev->refcnt );
867         aoedev->netdev = netdev_get ( netdev );
868         aoedev->major = major;
869         aoedev->minor = minor;
870         memcpy ( aoedev->target, netdev->ll_broadcast,
871                  netdev->ll_protocol->ll_addr_len );
872
873         /* Initiate configuration */
874         if ( ( rc = aoedev_cfg_command ( aoedev, &aoedev->config ) ) < 0 ) {
875                 DBGC ( aoedev, "AoE %s could not initiate configuration: %s\n",
876                        aoedev_name ( aoedev ), strerror ( rc ) );
877                 goto err_config;
878         }
879
880         /* Attach ATA device to parent interface */
881         if ( ( rc = ata_open ( parent, &aoedev->ata, ATA_DEV_MASTER,
882                                AOE_MAX_COUNT ) ) != 0 ) {
883                 DBGC ( aoedev, "AoE %s could not create ATA device: %s\n",
884                        aoedev_name ( aoedev ), strerror ( rc ) );
885                 goto err_ata_open;
886         }
887
888         /* Mortalise self and return */
889         ref_put ( &aoedev->refcnt );
890         return 0;
891
892  err_ata_open:
893  err_config:
894         aoedev_close ( aoedev, rc );
895         ref_put ( &aoedev->refcnt );
896  err_zalloc:
897         return rc;
898 }
899
900 /******************************************************************************
901  *
902  * AoE network protocol
903  *
904  ******************************************************************************
905  */
906
907 /**
908  * Process incoming AoE packets
909  *
910  * @v iobuf             I/O buffer
911  * @v netdev            Network device
912  * @v ll_dest           Link-layer destination address
913  * @v ll_source         Link-layer source address
914  * @v flags             Packet flags
915  * @ret rc              Return status code
916  */
917 static int aoe_rx ( struct io_buffer *iobuf,
918                     struct net_device *netdev __unused,
919                     const void *ll_dest __unused,
920                     const void *ll_source,
921                     unsigned int flags __unused ) {
922         struct aoehdr *aoehdr = iobuf->data;
923         struct aoe_command *aoecmd;
924         int rc;
925
926         /* Sanity check */
927         if ( iob_len ( iobuf ) < sizeof ( *aoehdr ) ) {
928                 DBG ( "AoE received underlength packet (%zd bytes)\n",
929                       iob_len ( iobuf ) );
930                 rc = -EINVAL;
931                 goto err_sanity;
932         }
933         if ( ( aoehdr->ver_flags & AOE_VERSION_MASK ) != AOE_VERSION ) {
934                 DBG ( "AoE received packet for unsupported protocol version "
935                       "%02x\n", ( aoehdr->ver_flags & AOE_VERSION_MASK ) );
936                 rc = -EPROTONOSUPPORT;
937                 goto err_sanity;
938         }
939         if ( ! ( aoehdr->ver_flags & AOE_FL_RESPONSE ) ) {
940                 DBG ( "AoE received request packet\n" );
941                 rc = -EOPNOTSUPP;
942                 goto err_sanity;
943         }
944
945         /* Demultiplex amongst active AoE commands */
946         aoecmd = aoecmd_find_tag ( ntohl ( aoehdr->tag ) );
947         if ( ! aoecmd ) {
948                 DBG ( "AoE received packet for unused tag %08x\n",
949                       ntohl ( aoehdr->tag ) );
950                 rc = -ENOENT;
951                 goto err_demux;
952         }
953
954         /* Pass received frame to command */
955         aoecmd_get ( aoecmd );
956         if ( ( rc = aoecmd_rx ( aoecmd, iob_disown ( iobuf ),
957                                 ll_source ) ) != 0 )
958                 goto err_rx;
959
960  err_rx:
961         aoecmd_put ( aoecmd );
962  err_demux:
963  err_sanity:
964         free_iob ( iobuf );
965         return rc;
966 }
967
968 /** AoE protocol */
969 struct net_protocol aoe_protocol __net_protocol = {
970         .name = "AoE",
971         .net_proto = htons ( ETH_P_AOE ),
972         .rx = aoe_rx,
973 };
974
975 /******************************************************************************
976  *
977  * AoE URIs
978  *
979  ******************************************************************************
980  */
981
982 /**
983  * Parse AoE URI
984  *
985  * @v uri               URI
986  * @ret major           Major device number
987  * @ret minor           Minor device number
988  * @ret rc              Return status code
989  *
990  * An AoE URI has the form "aoe:e<major>.<minor>".
991  */
992 static int aoe_parse_uri ( struct uri *uri, unsigned int *major,
993                            unsigned int *minor ) {
994         const char *ptr;
995         char *end;
996
997         /* Check for URI with opaque portion */
998         if ( ! uri->opaque )
999                 return -EINVAL;
1000         ptr = uri->opaque;
1001
1002         /* Check for initial 'e' */
1003         if ( *ptr != 'e' )
1004                 return -EINVAL;
1005         ptr++;
1006
1007         /* Parse major device number */
1008         *major = strtoul ( ptr, &end, 10 );
1009         if ( *end != '.' )
1010                 return -EINVAL;
1011         ptr = ( end + 1 );
1012
1013         /* Parse minor device number */
1014         *minor = strtoul ( ptr, &end, 10 );
1015         if ( *end )
1016                 return -EINVAL;
1017
1018         return 0;
1019 }
1020
1021 /**
1022  * Open AoE URI
1023  *
1024  * @v parent            Parent interface
1025  * @v uri               URI
1026  * @ret rc              Return status code
1027  */
1028 static int aoe_open ( struct interface *parent, struct uri *uri ) {
1029         struct net_device *netdev;
1030         unsigned int major;
1031         unsigned int minor;
1032         int rc;
1033
1034         /* Identify network device.  This is something of a hack, but
1035          * the AoE URI scheme that has been in use for some time now
1036          * provides no way to specify a particular device.
1037          */
1038         netdev = last_opened_netdev();
1039         if ( ! netdev ) {
1040                 DBG ( "AoE cannot identify network device\n" );
1041                 return -ENODEV;
1042         }
1043
1044         /* Parse URI */
1045         if ( ( rc = aoe_parse_uri ( uri, &major, &minor ) ) != 0 ) {
1046                 DBG ( "AoE cannot parse URI\n" );
1047                 return rc;
1048         }
1049
1050         /* Open AoE device */
1051         if ( ( rc = aoedev_open ( parent, netdev, major, minor ) ) != 0 )
1052                 return rc;
1053
1054         return 0;
1055 }
1056
1057 /** AoE URI opener */
1058 struct uri_opener aoe_uri_opener __uri_opener = {
1059         .scheme = "aoe",
1060         .open = aoe_open,
1061 };