Include errno.h in the few drivers which use it, rather than in nic.h
[people/sha0/gpxe.git] / src / include / nic.h
1  /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation; either version 2, or (at
5  * your option) any later version.
6  */
7
8 #ifndef NIC_H
9 #define NIC_H
10
11 #include <stdint.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <byteswap.h>
15 #include <gpxe/pci.h>
16 #include <gpxe/isapnp.h>
17 #include <gpxe/isa.h>
18 #include <gpxe/eisa.h>
19 #include <gpxe/mca.h>
20 #include "dhcp.h"
21
22 typedef enum {
23         DISABLE = 0,
24         ENABLE,
25         FORCE
26 } irq_action_t;
27
28 typedef enum duplex {
29         HALF_DUPLEX = 1,
30         FULL_DUPLEX
31 } duplex_t;
32
33 /*
34  *      Structure returned from eth_probe and passed to other driver
35  *      functions.
36  */
37 struct nic {
38         struct nic_operations   *nic_op;
39         int                     flags;  /* driver specific flags */
40         unsigned char           *node_addr;
41         unsigned char           *packet;
42         unsigned int            packetlen;
43         unsigned int            ioaddr;
44         unsigned char           irqno;
45         unsigned int            mbps;
46         duplex_t                duplex;
47         struct dhcp_dev_id      dhcp_dev_id;
48         void                    *priv_data;     /* driver private data */
49 };
50
51 struct nic_operations {
52         int ( *connect ) ( struct nic * );
53         int ( *poll ) ( struct nic *, int retrieve );
54         void ( *transmit ) ( struct nic *, const char *,
55                              unsigned int, unsigned int, const char * );
56         void ( *irq ) ( struct nic *, irq_action_t );
57 };
58
59 extern struct nic nic;
60
61 static inline int eth_poll ( int retrieve ) {
62         return nic.nic_op->poll ( &nic, retrieve );
63 }
64
65 static inline void eth_transmit ( const char *dest, unsigned int type,
66                                   unsigned int size, const void *packet ) {
67         nic.nic_op->transmit ( &nic, dest, type, size, packet );
68 }
69
70 /*
71  * Function prototypes
72  *
73  */
74 extern int dummy_connect ( struct nic *nic );
75 extern void dummy_irq ( struct nic *nic, irq_action_t irq_action );
76 extern int legacy_probe ( void *hwdev,
77                           void ( * set_drvdata ) ( void *hwdev, void *priv ),
78                           struct device *dev,
79                           int ( * probe ) ( struct nic *nic, void *hwdev ),
80                           void ( * disable ) ( struct nic *nic, void *hwdev ));
81 void legacy_remove ( void *hwdev,
82                      void * ( * get_drvdata ) ( void *hwdev ),
83                      void ( * disable ) ( struct nic *nic, void *hwdev ) );
84
85 #define PCI_DRIVER(_name,_ids,_class)                                     \
86         static inline int                                                 \
87         _name ## _pci_legacy_probe ( struct pci_device *pci,              \
88                                      const struct pci_device_id *id );    \
89         static inline void                                                \
90         _name ## _pci_legacy_remove ( struct pci_device *pci );           \
91         struct pci_driver _name __pci_driver = {                          \
92                 .ids = _ids,                                              \
93                 .id_count = sizeof ( _ids ) / sizeof ( _ids[0] ),         \
94                 .probe = _name ## _pci_legacy_probe,                      \
95                 .remove = _name ## _pci_legacy_remove,                    \
96         };                                                                \
97         REQUIRE_OBJECT ( pci );
98
99 static inline void legacy_pci_set_drvdata ( void *hwdev, void *priv ) {
100         pci_set_drvdata ( hwdev, priv );
101 }
102 static inline void * legacy_pci_get_drvdata ( void *hwdev ) {
103         return pci_get_drvdata ( hwdev );
104 }
105
106 #define ISAPNP_DRIVER(_name,_ids)                                         \
107         static inline int                                                 \
108         _name ## _isapnp_legacy_probe ( struct isapnp_device *isapnp,     \
109                                         const struct isapnp_device_id *id ); \
110         static inline void                                                \
111         _name ## _isapnp_legacy_remove ( struct isapnp_device *isapnp );  \
112         struct isapnp_driver _name __isapnp_driver = {                    \
113                 .ids = _ids,                                              \
114                 .id_count = sizeof ( _ids ) / sizeof ( _ids[0] ),         \
115                 .probe = _name ## _isapnp_legacy_probe,                   \
116                 .remove = _name ## _isapnp_legacy_remove,                 \
117         };                                                                \
118         REQUIRE_OBJECT ( isapnp );
119
120 static inline void legacy_isapnp_set_drvdata ( void *hwdev, void *priv ) {
121         isapnp_set_drvdata ( hwdev, priv );
122 }
123 static inline void * legacy_isapnp_get_drvdata ( void *hwdev ) {
124         return isapnp_get_drvdata ( hwdev );
125 }
126
127 #define EISA_DRIVER(_name,_ids)                                           \
128         static inline int                                                 \
129         _name ## _eisa_legacy_probe ( struct eisa_device *eisa,           \
130                                       const struct eisa_device_id *id );  \
131         static inline void                                                \
132         _name ## _eisa_legacy_remove ( struct eisa_device *eisa );        \
133         struct eisa_driver _name __eisa_driver = {                        \
134                 .ids = _ids,                                              \
135                 .id_count = sizeof ( _ids ) / sizeof ( _ids[0] ),         \
136                 .probe = _name ## _eisa_legacy_probe,                     \
137                 .remove = _name ## _eisa_legacy_remove,                   \
138         };                                                                \
139         REQUIRE_OBJECT ( eisa );
140
141 static inline void legacy_eisa_set_drvdata ( void *hwdev, void *priv ) {
142         eisa_set_drvdata ( hwdev, priv );
143 }
144 static inline void * legacy_eisa_get_drvdata ( void *hwdev ) {
145         return eisa_get_drvdata ( hwdev );
146 }
147
148 #define MCA_DRIVER(_name,_ids)                                            \
149         static inline int                                                 \
150         _name ## _mca_legacy_probe ( struct mca_device *mca,              \
151                                      const struct mca_device_id *id );    \
152         static inline void                                                \
153         _name ## _mca_legacy_remove ( struct mca_device *mca );           \
154         struct mca_driver _name __mca_driver = {                          \
155                 .ids = _ids,                                              \
156                 .id_count = sizeof ( _ids ) / sizeof ( _ids[0] ),         \
157                 .probe = _name ## _mca_legacy_probe,                      \
158                 .remove = _name ## _mca_legacy_remove,                    \
159         };                                                                \
160         REQUIRE_OBJECT ( mca );
161
162 static inline void legacy_mca_set_drvdata ( void *hwdev, void *priv ) {
163         mca_set_drvdata ( hwdev, priv );
164 }
165 static inline void * legacy_mca_get_drvdata ( void *hwdev ) {
166         return mca_get_drvdata ( hwdev );
167 }
168
169 #define ISA_DRIVER(_name,_probe_addrs,_probe_addr,_vendor_id,_prod_id)    \
170         static inline int                                                 \
171         _name ## _isa_legacy_probe ( struct isa_device *isa );            \
172         static inline int                                                 \
173         _name ## _isa_legacy_probe_at_addr ( struct isa_device *isa ) {   \
174                 if ( ! _probe_addr ( isa->ioaddr ) )                      \
175                         return -ENODEV;                                   \
176                 return _name ## _isa_legacy_probe ( isa );                \
177         }                                                                 \
178         static inline void                                                \
179         _name ## _isa_legacy_remove ( struct isa_device *isa );           \
180         static const char _name ## _text[];                               \
181         struct isa_driver _name __isa_driver = {                          \
182                 .name = _name ## _text,                                   \
183                 .probe_addrs = _probe_addrs,                              \
184                 .addr_count = ( sizeof ( _probe_addrs ) /                 \
185                                 sizeof ( _probe_addrs[0] ) ),             \
186                 .vendor_id = _vendor_id,                                  \
187                 .prod_id = _prod_id,                                      \
188                 .probe = _name ## _isa_legacy_probe_at_addr,              \
189                 .remove = _name ## _isa_legacy_remove,                    \
190         };                                                                \
191         REQUIRE_OBJECT ( isa );
192
193 static inline void legacy_isa_set_drvdata ( void *hwdev, void *priv ) {
194         isa_set_drvdata ( hwdev, priv );
195 }
196 static inline void * legacy_isa_get_drvdata ( void *hwdev ) {
197         return isa_get_drvdata ( hwdev );
198 }
199
200 #undef DRIVER
201 #define DRIVER(_name_text,_unused2,_unused3,_name,_probe,_disable)        \
202         static const char _name ## _text[] = _name_text;                  \
203         static inline int                                                 \
204         _name ## _probe ( struct nic *nic, void *hwdev ) {                \
205                 return _probe ( nic, hwdev );                             \
206         }                                                                 \
207         static inline void                                                \
208         _name ## _disable ( struct nic *nic, void *hwdev ) {              \
209                 void ( * _unsafe_disable ) () = _disable;                 \
210                 _unsafe_disable ( nic, hwdev );                           \
211         }                                                                 \
212         static inline int                                                 \
213         _name ## _pci_legacy_probe ( struct pci_device *pci,              \
214                             const struct pci_device_id *id __unused ) {   \
215                 return legacy_probe ( pci, legacy_pci_set_drvdata,        \
216                                       &pci->dev, _name ## _probe,         \
217                                       _name ## _disable );                \
218         }                                                                 \
219         static inline void                                                \
220         _name ## _pci_legacy_remove ( struct pci_device *pci ) {          \
221                 return legacy_remove ( pci, legacy_pci_get_drvdata,       \
222                                        _name ## _disable );               \
223         }                                                                 \
224         static inline int                                                 \
225         _name ## _isapnp_legacy_probe ( struct isapnp_device *isapnp,     \
226                          const struct isapnp_device_id *id __unused ) {   \
227                 return legacy_probe ( isapnp, legacy_isapnp_set_drvdata,  \
228                                       &isapnp->dev, _name ## _probe,      \
229                                       _name ## _disable );                \
230         }                                                                 \
231         static inline void                                                \
232         _name ## _isapnp_legacy_remove ( struct isapnp_device *isapnp ) { \
233                 return legacy_remove ( isapnp, legacy_isapnp_get_drvdata, \
234                                        _name ## _disable );               \
235         }                                                                 \
236         static inline int                                                 \
237         _name ## _eisa_legacy_probe ( struct eisa_device *eisa,           \
238                              const struct eisa_device_id *id __unused ) { \
239                 return legacy_probe ( eisa, legacy_eisa_set_drvdata,      \
240                                       &eisa->dev, _name ## _probe,        \
241                                       _name ## _disable );                \
242         }                                                                 \
243         static inline void                                                \
244         _name ## _eisa_legacy_remove ( struct eisa_device *eisa ) {       \
245                 return legacy_remove ( eisa, legacy_eisa_get_drvdata,     \
246                                        _name ## _disable );               \
247         }                                                                 \
248         static inline int                                                 \
249         _name ## _mca_legacy_probe ( struct mca_device *mca,              \
250                               const struct mca_device_id *id __unused ) { \
251                 return legacy_probe ( mca, legacy_mca_set_drvdata,        \
252                                       &mca->dev, _name ## _probe,         \
253                                       _name ## _disable );                \
254         }                                                                 \
255         static inline void                                                \
256         _name ## _mca_legacy_remove ( struct mca_device *mca ) {          \
257                 return legacy_remove ( mca, legacy_mca_get_drvdata,       \
258                                        _name ## _disable );               \
259         }                                                                 \
260         static inline int                                                 \
261         _name ## _isa_legacy_probe ( struct isa_device *isa ) {           \
262                 return legacy_probe ( isa, legacy_isa_set_drvdata,        \
263                                       &isa->dev, _name ## _probe,         \
264                                       _name ## _disable );                \
265         }                                                                 \
266         static inline void                                                \
267         _name ## _isa_legacy_remove ( struct isa_device *isa ) {          \
268                 return legacy_remove ( isa, legacy_isa_get_drvdata,       \
269                                        _name ## _disable );               \
270         }
271
272 static inline void pci_fill_nic ( struct nic *nic, struct pci_device *pci ) {
273         nic->ioaddr = pci->ioaddr;
274         nic->irqno = pci->irq;
275 }
276
277 static inline void isapnp_fill_nic ( struct nic *nic,
278                                      struct isapnp_device *isapnp ) {
279         nic->ioaddr = isapnp->ioaddr;
280         nic->irqno = isapnp->irqno;
281 }
282
283 static inline void eisa_fill_nic ( struct nic *nic,
284                                    struct eisa_device *eisa ) {
285         nic->ioaddr = eisa->ioaddr;
286         nic->irqno = 0;
287 }
288
289 static inline void mca_fill_nic ( struct nic *nic,
290                                   struct mca_device *mca __unused ) {
291         /* ioaddr and irqno must be read in a device-dependent way
292          * from the POS registers
293          */
294         nic->ioaddr = 0;
295         nic->irqno = 0;
296 }
297
298 static inline void isa_fill_nic ( struct nic *nic, struct isa_device *isa ) {
299         nic->ioaddr = isa->ioaddr;
300         nic->irqno = 0;
301 }
302
303 #endif  /* NIC_H */