00001 #ifndef _GPXE_NETDEVICE_H
00002 #define _GPXE_NETDEVICE_H
00003
00004
00005
00006
00007
00008
00009
00010 FILE_LICENCE ( GPL2_OR_LATER );
00011
00012 #include <stdint.h>
00013 #include <gpxe/list.h>
00014 #include <gpxe/tables.h>
00015 #include <gpxe/refcnt.h>
00016 #include <gpxe/settings.h>
00017
00018 struct io_buffer;
00019 struct net_device;
00020 struct net_protocol;
00021 struct ll_protocol;
00022 struct device;
00023
00024
00025
00026
00027
00028 #define MAX_HW_ADDR_LEN 8
00029
00030
00031
00032
00033
00034 #define MAX_LL_ADDR_LEN 20
00035
00036
00037
00038
00039
00040
00041
00042
00043 #define MAX_LL_HEADER_LEN 32
00044
00045
00046 #define MAX_NET_ADDR_LEN 4
00047
00048
00049
00050
00051
00052 struct net_protocol {
00053
00054 const char *name;
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
00065 const void *ll_source );
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 const char * ( *ntoa ) ( const void * net_addr );
00079
00080
00081
00082
00083 uint16_t net_proto;
00084
00085 uint8_t net_addr_len;
00086 };
00087
00088
00089
00090
00091
00092 struct ll_protocol {
00093
00094 const char *name;
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 int ( * push ) ( struct net_device *netdev, struct io_buffer *iobuf,
00106 const void *ll_dest, const void *ll_source,
00107 uint16_t net_proto );
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 int ( * pull ) ( struct net_device *netdev, struct io_buffer *iobuf,
00119 const void **ll_dest, const void **ll_source,
00120 uint16_t *net_proto );
00121
00122
00123
00124
00125
00126
00127 void ( * init_addr ) ( const void *hw_addr, void *ll_addr );
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 const char * ( * ntoa ) ( const void *ll_addr );
00141
00142
00143
00144
00145
00146
00147
00148
00149 int ( * mc_hash ) ( unsigned int af, const void *net_addr,
00150 void *ll_addr );
00151
00152
00153
00154
00155
00156
00157 int ( * eth_addr ) ( const void *ll_addr, void *eth_addr );
00158
00159
00160
00161
00162 uint16_t ll_proto;
00163
00164 uint8_t hw_addr_len;
00165
00166 uint8_t ll_addr_len;
00167
00168 uint8_t ll_header_len;
00169 };
00170
00171
00172 struct net_device_operations {
00173
00174
00175
00176
00177
00178
00179
00180
00181 int ( * open ) ( struct net_device *netdev );
00182
00183
00184
00185
00186
00187
00188
00189 void ( * close ) ( struct net_device *netdev );
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 int ( * transmit ) ( struct net_device *netdev,
00210 struct io_buffer *iobuf );
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 void ( * poll ) ( struct net_device *netdev );
00223
00224
00225
00226
00227
00228 void ( * irq ) ( struct net_device *netdev, int enable );
00229 };
00230
00231
00232 struct net_device_error {
00233
00234 int rc;
00235
00236 unsigned int count;
00237 };
00238
00239
00240 #define NETDEV_MAX_UNIQUE_ERRORS 4
00241
00242
00243 struct net_device_stats {
00244
00245 unsigned int good;
00246
00247 unsigned int bad;
00248
00249 struct net_device_error errors[NETDEV_MAX_UNIQUE_ERRORS];
00250 };
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 struct net_device {
00263
00264 struct refcnt refcnt;
00265
00266 struct list_head list;
00267
00268 struct list_head open_list;
00269
00270 char name[8];
00271
00272 struct device *dev;
00273
00274
00275 struct net_device_operations *op;
00276
00277
00278 struct ll_protocol *ll_protocol;
00279
00280
00281
00282
00283
00284
00285
00286
00287 uint8_t hw_addr[MAX_HW_ADDR_LEN];
00288
00289
00290
00291
00292
00293 uint8_t ll_addr[MAX_LL_ADDR_LEN];
00294
00295 const uint8_t *ll_broadcast;
00296
00297
00298
00299
00300
00301 unsigned int state;
00302
00303
00304
00305
00306
00307 int link_rc;
00308
00309
00310
00311
00312 size_t max_pkt_len;
00313
00314 struct list_head tx_queue;
00315
00316 struct list_head rx_queue;
00317
00318 struct net_device_stats tx_stats;
00319
00320 struct net_device_stats rx_stats;
00321
00322
00323 struct generic_settings settings;
00324
00325
00326 void *priv;
00327 };
00328
00329
00330 #define NETDEV_OPEN 0x0001
00331
00332
00333 #define NETDEV_IRQ_ENABLED 0x0002
00334
00335
00336 #define LL_PROTOCOLS __table ( struct ll_protocol, "ll_protocols" )
00337
00338
00339 #define __ll_protocol __table_entry ( LL_PROTOCOLS, 01 )
00340
00341
00342 #define NET_PROTOCOLS __table ( struct net_protocol, "net_protocols" )
00343
00344
00345 #define __net_protocol __table_entry ( NET_PROTOCOLS, 01 )
00346
00347 extern struct list_head net_devices;
00348 extern struct net_device_operations null_netdev_operations;
00349 extern struct settings_operations netdev_settings_operations;
00350
00351
00352
00353
00354
00355
00356
00357 static inline void netdev_init ( struct net_device *netdev,
00358 struct net_device_operations *op ) {
00359 netdev->op = op;
00360 }
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 static inline void netdev_nullify ( struct net_device *netdev ) {
00371 netdev->op = &null_netdev_operations;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380 static inline const char * netdev_addr ( struct net_device *netdev ) {
00381 return netdev->ll_protocol->ntoa ( netdev->ll_addr );
00382 }
00383
00384
00385 #define for_each_netdev( netdev ) \
00386 list_for_each_entry ( (netdev), &net_devices, list )
00387
00388
00389
00390
00391
00392 static inline int have_netdevs ( void ) {
00393 return ( ! list_empty ( &net_devices ) );
00394 }
00395
00396
00397
00398
00399
00400
00401
00402 static inline __attribute__ (( always_inline )) struct net_device *
00403 netdev_get ( struct net_device *netdev ) {
00404 ref_get ( &netdev->refcnt );
00405 return netdev;
00406 }
00407
00408
00409
00410
00411
00412
00413 static inline __attribute__ (( always_inline )) void
00414 netdev_put ( struct net_device *netdev ) {
00415 ref_put ( &netdev->refcnt );
00416 }
00417
00418
00419
00420
00421
00422
00423
00424 static inline __attribute__ (( always_inline )) void *
00425 netdev_priv ( struct net_device *netdev ) {
00426 return netdev->priv;
00427 }
00428
00429
00430
00431
00432
00433
00434
00435 static inline __attribute__ (( always_inline )) struct settings *
00436 netdev_settings ( struct net_device *netdev ) {
00437 return &netdev->settings.settings;
00438 }
00439
00440
00441
00442
00443
00444
00445
00446
00447 static inline __attribute__ (( always_inline )) void
00448 netdev_settings_init ( struct net_device *netdev ) {
00449 generic_settings_init ( &netdev->settings,
00450 &netdev->refcnt, netdev->name );
00451 netdev->settings.settings.op = &netdev_settings_operations;
00452 }
00453
00454
00455
00456
00457
00458
00459 static inline __attribute__ (( always_inline )) void
00460 netdev_link_up ( struct net_device *netdev ) {
00461 netdev->link_rc = 0;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470 static inline __attribute__ (( always_inline )) void
00471 netdev_link_err ( struct net_device *netdev, int rc ) {
00472 netdev->link_rc = rc;
00473 }
00474
00475
00476
00477
00478
00479
00480
00481 static inline __attribute__ (( always_inline )) int
00482 netdev_link_ok ( struct net_device *netdev ) {
00483 return ( netdev->link_rc == 0 );
00484 }
00485
00486
00487
00488
00489
00490
00491
00492 static inline __attribute__ (( always_inline )) int
00493 netdev_is_open ( struct net_device *netdev ) {
00494 return ( netdev->state & NETDEV_OPEN );
00495 }
00496
00497
00498
00499
00500
00501
00502
00503 static inline __attribute__ (( always_inline )) int
00504 netdev_irq_enabled ( struct net_device *netdev ) {
00505 return ( netdev->state & NETDEV_IRQ_ENABLED );
00506 }
00507
00508 extern void netdev_link_down ( struct net_device *netdev );
00509 extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
00510 extern void netdev_tx_complete_err ( struct net_device *netdev,
00511 struct io_buffer *iobuf, int rc );
00512 extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
00513 extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
00514 extern void netdev_rx_err ( struct net_device *netdev,
00515 struct io_buffer *iobuf, int rc );
00516 extern void netdev_poll ( struct net_device *netdev );
00517 extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
00518 extern struct net_device * alloc_netdev ( size_t priv_size );
00519 extern int register_netdev ( struct net_device *netdev );
00520 extern int netdev_open ( struct net_device *netdev );
00521 extern void netdev_close ( struct net_device *netdev );
00522 extern void unregister_netdev ( struct net_device *netdev );
00523 extern void netdev_irq ( struct net_device *netdev, int enable );
00524 extern struct net_device * find_netdev ( const char *name );
00525 extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
00526 unsigned int location );
00527 extern struct net_device * last_opened_netdev ( void );
00528 extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
00529 struct net_protocol *net_protocol, const void *ll_dest );
00530 extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
00531 uint16_t net_proto, const void *ll_source );
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541 static inline void netdev_tx_complete ( struct net_device *netdev,
00542 struct io_buffer *iobuf ) {
00543 netdev_tx_complete_err ( netdev, iobuf, 0 );
00544 }
00545
00546
00547
00548
00549
00550
00551
00552
00553 static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
00554 netdev_tx_complete_next_err ( netdev, 0 );
00555 }
00556
00557 #endif