source: mainline/uspace/lib/c/include/nic/nic.h@ cd66f3c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd66f3c was cf9cb36, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Decouple libnic from libnet.

  • Property mode set to 100644
File size: 13.3 KB
RevLine 
[cf9cb36]1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * Copyright (c) 2011 Radim Vansa
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33
34/** @file
35 * NIC interface definitions.
36 */
37
38#ifndef LIBC_NIC_H_
39#define LIBC_NIC_H_
40
41#include <nic/eth_phys.h>
42#include <bool.h>
43
44/** Ethernet address length. */
45#define ETH_ADDR 6
46
47/** MAC printing format */
48#define PRIMAC "%02x:%02x:%02x:%02x:%02x:%02x"
49
50/** MAC arguments */
51#define ARGSMAC(__a) \
52 (__a)[0], (__a)[1], (__a)[2], (__a)[3], (__a)[4], (__a)[5]
53
54/* Compare MAC address with specific value */
55#define MAC_EQUALS_VALUE(__a, __a0, __a1, __a2, __a3, __a4, __a5) \
56 ((__a)[0] == (__a0) && (__a)[1] == (__a1) && (__a)[2] == (__a2) \
57 && (__a)[3] == (__a3) && (__a)[4] == (__a4) && (__a)[5] == (__a5))
58
59#define MAC_IS_ZERO(__x) \
60 MAC_EQUALS_VALUE(__x, 0, 0, 0, 0, 0, 0)
61
62/** Max length of any hw nic address (currently only eth) */
63#define NIC_MAX_ADDRESS_LENGTH 16
64
65/** Invalid device identifier. */
66#define NIC_DEVICE_INVALID_ID (-1)
67
68#define NIC_VENDOR_MAX_LENGTH 64
69#define NIC_MODEL_MAX_LENGTH 64
70#define NIC_PART_NUMBER_MAX_LENGTH 64
71#define NIC_SERIAL_NUMBER_MAX_LENGTH 64
72
73#define NIC_DEFECTIVE_LONG 0x0001
74#define NIC_DEFECTIVE_SHORT 0x0002
75#define NIC_DEFECTIVE_BAD_CRC 0x0010
76#define NIC_DEFECTIVE_BAD_IPV4_CHECKSUM 0x0020
77#define NIC_DEFECTIVE_BAD_IPV6_CHECKSUM 0x0040
78#define NIC_DEFECTIVE_BAD_TCP_CHECKSUM 0x0080
79#define NIC_DEFECTIVE_BAD_UDP_CHECKSUM 0x0100
80
81/**
82 * The bitmap uses single bit for each of the 2^12 = 4096 possible VLAN tags.
83 * This means its size is 4096/8 = 512 bytes.
84 */
85#define NIC_VLAN_BITMAP_SIZE 512
86
87#define NIC_DEVICE_PRINT_FMT "%x"
88
89/** Device identifier type. */
90typedef int nic_device_id_t;
91
92/**
93 * Structure covering the MAC address.
94 */
95typedef struct nic_address {
96 uint8_t address[ETH_ADDR];
97} nic_address_t;
98
99/** Device state. */
100typedef enum nic_device_state {
101 /**
102 * Device present and stopped. Moving device to this state means to discard
103 * all settings and WOL virtues, rebooting the NIC to state as if the
104 * computer just booted (or the NIC was just inserted in case of removable
105 * NIC).
106 */
107 NIC_STATE_STOPPED,
108 /**
109 * If the NIC is in this state no packets (frames) are transmitted nor
110 * received. However, the settings are not restarted. You can use this state
111 * to temporarily disable transmition/reception or atomically (with respect
112 * to incoming/outcoming packets) change frames acceptance etc.
113 */
114 NIC_STATE_DOWN,
115 /** Device is normally operating. */
116 NIC_STATE_ACTIVE,
117 /** Just a constant to limit the state numbers */
118 NIC_STATE_MAX,
119} nic_device_state_t;
120
121/**
122 * Channel operating mode used on the medium.
123 */
124typedef enum {
125 NIC_CM_UNKNOWN,
126 NIC_CM_FULL_DUPLEX,
127 NIC_CM_HALF_DUPLEX,
128 NIC_CM_SIMPLEX
129} nic_channel_mode_t;
130
131/**
132 * Role for the device (used e.g. for 1000Gb ethernet)
133 */
134typedef enum {
135 NIC_ROLE_UNKNOWN,
136 NIC_ROLE_AUTO,
137 NIC_ROLE_MASTER,
138 NIC_ROLE_SLAVE
139} nic_role_t;
140
141/**
142 * Current state of the cable in the device
143 */
144typedef enum {
145 NIC_CS_UNKNOWN,
146 NIC_CS_PLUGGED,
147 NIC_CS_UNPLUGGED
148} nic_cable_state_t;
149
150/**
151 * Result of the requested operation
152 */
153typedef enum {
154 /** Successfully disabled */
155 NIC_RESULT_DISABLED,
156 /** Successfully enabled */
157 NIC_RESULT_ENABLED,
158 /** Not supported at all */
159 NIC_RESULT_NOT_SUPPORTED,
160 /** Temporarily not available */
161 NIC_RESULT_NOT_AVAILABLE,
162 /** Result extensions */
163 NIC_RESULT_FIRST_EXTENSION
164} nic_result_t;
165
166/** Device usage statistics. */
167typedef struct nic_device_stats {
168 /** Total packets received (accepted). */
169 unsigned long receive_packets;
170 /** Total packets transmitted. */
171 unsigned long send_packets;
172 /** Total bytes received (accepted). */
173 unsigned long receive_bytes;
174 /** Total bytes transmitted. */
175 unsigned long send_bytes;
176 /** Bad packets received counter. */
177 unsigned long receive_errors;
178 /** Packet transmition problems counter. */
179 unsigned long send_errors;
180 /** Number of frames dropped due to insufficient space in RX buffers */
181 unsigned long receive_dropped;
182 /** Number of frames dropped due to insufficient space in TX buffers */
183 unsigned long send_dropped;
184 /** Total multicast packets received (accepted). */
185 unsigned long receive_multicast;
186 /** Total broadcast packets received (accepted). */
187 unsigned long receive_broadcast;
188 /** The number of collisions due to congestion on the medium. */
189 unsigned long collisions;
190 /** Unicast packets received but not accepted (filtered) */
191 unsigned long receive_filtered_unicast;
192 /** Multicast packets received but not accepted (filtered) */
193 unsigned long receive_filtered_multicast;
194 /** Broadcast packets received but not accepted (filtered) */
195 unsigned long receive_filtered_broadcast;
196
197 /* detailed receive_errors */
198
199 /** Received packet length error counter. */
200 unsigned long receive_length_errors;
201 /** Receiver buffer overflow counter. */
202 unsigned long receive_over_errors;
203 /** Received packet with crc error counter. */
204 unsigned long receive_crc_errors;
205 /** Received frame alignment error counter. */
206 unsigned long receive_frame_errors;
207 /** Receiver fifo overrun counter. */
208 unsigned long receive_fifo_errors;
209 /** Receiver missed packet counter. */
210 unsigned long receive_missed_errors;
211
212 /* detailed send_errors */
213
214 /** Transmitter aborted counter. */
215 unsigned long send_aborted_errors;
216 /** Transmitter carrier errors counter. */
217 unsigned long send_carrier_errors;
218 /** Transmitter fifo overrun counter. */
219 unsigned long send_fifo_errors;
220 /** Transmitter carrier errors counter. */
221 unsigned long send_heartbeat_errors;
222 /** Transmitter window errors counter. */
223 unsigned long send_window_errors;
224
225 /* for cslip etc */
226
227 /** Total compressed packets received. */
228 unsigned long receive_compressed;
229 /** Total compressed packet transmitted. */
230 unsigned long send_compressed;
231} nic_device_stats_t;
232
233/** Errors corresponding to those in the nic_device_stats_t */
234typedef enum {
235 NIC_SEC_BUFFER_FULL,
236 NIC_SEC_ABORTED,
237 NIC_SEC_CARRIER_LOST,
238 NIC_SEC_FIFO_OVERRUN,
239 NIC_SEC_HEARTBEAT,
240 NIC_SEC_WINDOW_ERROR,
241 /* Error encountered during TX but with other type of error */
242 NIC_SEC_OTHER
243} nic_send_error_cause_t;
244
245/** Errors corresponding to those in the nic_device_stats_t */
246typedef enum {
247 NIC_REC_BUFFER_FULL,
248 NIC_REC_LENGTH,
249 NIC_REC_BUFFER_OVERFLOW,
250 NIC_REC_CRC,
251 NIC_REC_FRAME_ALIGNMENT,
252 NIC_REC_FIFO_OVERRUN,
253 NIC_REC_MISSED,
254 /* Error encountered during RX but with other type of error */
255 NIC_REC_OTHER
256} nic_receive_error_cause_t;
257
258/**
259 * Information about the NIC that never changes - name, vendor, model,
260 * capabilites and so on.
261 */
262typedef struct nic_device_info {
263 /* Device identification */
264 char vendor_name[NIC_VENDOR_MAX_LENGTH];
265 char model_name[NIC_MODEL_MAX_LENGTH];
266 char part_number[NIC_PART_NUMBER_MAX_LENGTH];
267 char serial_number[NIC_SERIAL_NUMBER_MAX_LENGTH];
268 uint16_t vendor_id;
269 uint16_t device_id;
270 uint16_t subsystem_vendor_id;
271 uint16_t subsystem_id;
272 /* Device capabilities */
273 uint16_t ethernet_support[ETH_PHYS_LAYERS];
274
275 /** The mask of all modes which the device can advertise
276 *
277 * see ETH_AUTONEG_ macros in nic/eth_phys.h of libc
278 */
279 uint32_t autoneg_support;
280} nic_device_info_t;
281
282/**
283 * Type of the ethernet frame
284 */
285typedef enum nic_frame_type {
286 NIC_FRAME_UNICAST,
287 NIC_FRAME_MULTICAST,
288 NIC_FRAME_BROADCAST
289} nic_frame_type_t;
290
291/**
292 * Specifies which unicast frames is the NIC receiving.
293 */
294typedef enum nic_unicast_mode {
295 NIC_UNICAST_UNKNOWN,
296 /** No unicast frames are received */
297 NIC_UNICAST_BLOCKED,
298 /** Only the frames with this NIC's MAC as destination are received */
299 NIC_UNICAST_DEFAULT,
300 /**
301 * Both frames with this NIC's MAC and those specified in the list are
302 * received
303 */
304 NIC_UNICAST_LIST,
305 /** All unicast frames are received */
306 NIC_UNICAST_PROMISC
307} nic_unicast_mode_t;
308
309typedef enum nic_multicast_mode {
310 NIC_MULTICAST_UNKNOWN,
311 /** No multicast frames are received */
312 NIC_MULTICAST_BLOCKED,
313 /** Frames with multicast addresses specified in this list are received */
314 NIC_MULTICAST_LIST,
315 /** All multicast frames are received */
316 NIC_MULTICAST_PROMISC
317} nic_multicast_mode_t;
318
319typedef enum nic_broadcast_mode {
320 NIC_BROADCAST_UNKNOWN,
321 /** Broadcast frames are dropped */
322 NIC_BROADCAST_BLOCKED,
323 /** Broadcast frames are received */
324 NIC_BROADCAST_ACCEPTED
325} nic_broadcast_mode_t;
326
327/**
328 * Structure covering the bitmap with VLAN tags.
329 */
330typedef struct nic_vlan_mask {
331 uint8_t bitmap[NIC_VLAN_BITMAP_SIZE];
332} nic_vlan_mask_t;
333
334/* WOL virtue identifier */
335typedef unsigned int nic_wv_id_t;
336
337/**
338 * Structure passed as argument for virtue NIC_WV_MAGIC_PACKET.
339 */
340typedef struct nic_wv_magic_packet_data {
341 uint8_t password[6];
342} nic_wv_magic_packet_data_t;
343
344/**
345 * Structure passed as argument for virtue NIC_WV_DIRECTED_IPV4
346 */
347typedef struct nic_wv_ipv4_data {
348 uint8_t address[4];
349} nic_wv_ipv4_data_t;
350
351/**
352 * Structure passed as argument for virtue NIC_WV_DIRECTED_IPV6
353 */
354typedef struct nic_wv_ipv6_data {
355 uint8_t address[16];
356} nic_wv_ipv6_data_t;
357
358/**
359 * WOL virtue types defining the interpretation of data passed to the virtue.
360 * Those tagged with S can have only single virtue active at one moment, those
361 * tagged with M can have multiple ones.
362 */
363typedef enum nic_wv_type {
364 /**
365 * Used for deletion of the virtue - in this case the mask, data and length
366 * arguments are ignored.
367 */
368 NIC_WV_NONE,
369 /** S
370 * Enabled <=> wakeup upon link change
371 */
372 NIC_WV_LINK_CHANGE,
373 /** S
374 * If this virtue is set up, wakeup can be issued by a magic packet frame.
375 * If the data argument is not NULL, it must contain
376 * nic_wv_magic_packet_data structure with the SecureOn password.
377 */
378 NIC_WV_MAGIC_PACKET,
379 /** M
380 * If the virtue is set up, wakeup can be issued by a frame targeted to
381 * device with MAC address specified in data. The data must contain
382 * nic_address_t structure.
383 */
384 NIC_WV_DESTINATION,
385 /** S
386 * Enabled <=> wakeup upon receiving broadcast frame
387 */
388 NIC_WV_BROADCAST,
389 /** S
390 * Enabled <=> wakeup upon receiving ARP Request
391 */
392 NIC_WV_ARP_REQUEST,
393 /** M
394 * If enabled, the wakeup is issued upon receiving frame with an IPv4 packet
395 * with IPv4 address specified in data. The data must contain
396 * nic_wv_ipv4_data structure.
397 */
398 NIC_WV_DIRECTED_IPV4,
399 /** M
400 * If enabled, the wakeup is issued upon receiving frame with an IPv4 packet
401 * with IPv6 address specified in data. The data must contain
402 * nic_wv_ipv6_data structure.
403 */
404 NIC_WV_DIRECTED_IPV6,
405 /** M
406 * First length/2 bytes in the argument are interpreted as mask, second
407 * length/2 bytes are interpreted as content.
408 * If enabled, the wakeup is issued upon receiving frame where the bytes
409 * with non-zero value in the mask equal to those in the content.
410 */
411 NIC_WV_FULL_MATCH,
412 /**
413 * Dummy value, do not use.
414 */
415 NIC_WV_MAX
416} nic_wv_type_t;
417
418/**
419 * Specifies the interrupt/polling mode used by the driver and NIC
420 */
421typedef enum nic_poll_mode {
422 /**
423 * NIC issues interrupts upon events.
424 */
425 NIC_POLL_IMMEDIATE,
426 /**
427 * Some uspace app calls nic_poll_now(...) in order to check the NIC state
428 * - no interrupts are received from the NIC.
429 */
430 NIC_POLL_ON_DEMAND,
431 /**
432 * The driver itself issues a poll request in a periodic manner. It is
433 * allowed to use hardware timer if the NIC supports it.
434 */
435 NIC_POLL_PERIODIC,
436 /**
437 * The driver itself issued a poll request in a periodic manner. The driver
438 * must create software timer, internal hardware timer of NIC must not be
439 * used even if the NIC supports it.
440 */
441 NIC_POLL_SOFTWARE_PERIODIC
442} nic_poll_mode_t;
443
444/**
445 * Says if this virtue type is a multi-virtue (there can be multiple virtues of
446 * this type at once).
447 *
448 * @param type
449 *
450 * @return true or false
451 */
452static inline int nic_wv_is_multi(nic_wv_type_t type) {
453 switch (type) {
454 case NIC_WV_FULL_MATCH:
455 case NIC_WV_DESTINATION:
456 case NIC_WV_DIRECTED_IPV4:
457 case NIC_WV_DIRECTED_IPV6:
458 return true;
459 default:
460 return false;
461 }
462}
463
464static inline const char *nic_device_state_to_string(nic_device_state_t state)
465{
466 switch (state) {
467 case NIC_STATE_STOPPED:
468 return "stopped";
469 case NIC_STATE_DOWN:
470 return "down";
471 case NIC_STATE_ACTIVE:
472 return "active";
473 default:
474 return "undefined";
475 }
476}
477
478#endif
479
480/** @}
481 */
Note: See TracBrowser for help on using the repository browser.