Merge from Etherboot 5.4
[people/andreif/gpxe.git] / src / drivers / net / mlx_ipoib / mt23108_imp.c
1 typedef uint32_t __u32;
2 typedef uint16_t __u16;
3 typedef uint8_t __u8;
4
5 static int verbose_messages=0;
6 static int print_info=0;
7 static int fatal_condition=0;
8 static int fw_fatal;
9
10 #define tprintf(fmt, a...) \
11                  do {    \
12                         if ( verbose_messages ) { \
13                                 printf("%s:%d: " fmt "\n", __func__, __LINE__,  ##a); \
14                         } \
15                  } \
16                  while(0)
17
18 #define eprintf(fmt, a...) \
19                  printf("%s:%d: " fmt "\n", __func__, __LINE__,  ##a)
20
21 static void cpu_to_be_buf(void *buf, int size)
22 {
23         int dw_sz = size >> 2, i;
24
25         for (i = 0; i < dw_sz; ++i) {
26                 ((__u32 *) buf)[i] = cpu_to_be32(((__u32 *) buf)[i]);
27         }
28 }
29
30 static void be_to_cpu_buf(void *buf, int size)
31 {
32         int dw_sz = size >> 2, i;
33         u32 *p = buf;
34
35         for (i = 0; i < dw_sz; ++i) {
36                 p[i] = be32_to_cpu(p[i]);
37         }
38 }
39
40 #include "timer.h"
41 #include "cmdif_mt23108.c"
42 #include "cmdif_comm.c"
43 #include "ib_mt23108.c"
44 #include "ib_mad.c"
45 #include "ib_driver.c"
46 #include "ipoib.c"
47
48 static int probe_imp(struct pci_device *pci, struct nic *nic)
49 {
50         int rc;
51
52         if (0 && nic) {         /* just to supress warning */
53                 return 0;
54         }
55
56         fatal_condition= 0;
57         fw_fatal= 0;
58
59         tprintf("");
60         rc = ipoib_init(pci);
61         if (rc)
62                 return rc;
63
64         tprintf("");
65
66         return rc;
67 }
68
69 static int disable_imp(void)
70 {
71         int rc;
72
73         rc = ipoib_close(fw_fatal);
74
75         return rc;
76 }
77
78 static int transmit_imp(const char *dest,       /* Destination */
79                         unsigned int type,      /* Type */
80                         const char *packet,     /* Packet */
81                         unsigned int size)
82 {                               /* size */
83         int rc;
84
85         if (fatal_condition) {
86                 /* since the transmit function does not return a value
87                    we return success but do nothing to suppress error messages */
88                 return 0;
89         }
90
91         rc = ipoib_send_packet(dest, type, packet, size);
92         if (rc) {
93                 printf("*** ERROR IN SEND FLOW ***\n");
94                 printf("restarting Etherboot\n");
95                 sleep(1);
96                 longjmp(restart_etherboot, -1);
97                 /* we should not be here ... */
98                 return -1; 
99         }
100
101         return rc;
102 }
103
104 static void hd(void *where, int n)
105 {
106         int i;
107
108         while (n > 0) {
109                 printf("%X ", where);
110                 for (i = 0; i < ((n > 16) ? 16 : n); i++)
111                         printf(" %hhX", ((char *)where)[i]);
112                 printf("\n");
113                 n -= 16;
114                 where += 16;
115         }
116 }
117
118 static int poll_imp(struct nic *nic, int retrieve, unsigned int *size_p)
119 {
120         static char packet[2048];
121         static char *last_packet_p = NULL;
122         static unsigned long last_packet_size;
123         char *packet_p;
124         const int eth_header_len = 14;
125         unsigned int packet_len;
126         int is_bcast = 0;
127         __u16 prot, *ptr;
128         int rc;
129
130         if (0 && nic) {         /* just to supress warning */
131                 return -1;
132         }
133
134         if (fatal_condition) {
135                 *size_p = 0;
136                 return 0;
137         }
138
139         if (poll_error_buf()) {
140                 fatal_condition= 1;
141                 fw_fatal= 1;
142                 printf("\n *** DEVICE FATAL ERROR ***\n");
143                 goto fatal_handling;
144         }
145         else if (drain_eq()) {
146                 fatal_condition= 1;
147                 printf("\n *** FATAL ERROR ***\n");
148                 goto fatal_handling;
149         }
150
151
152         if (retrieve) {
153                 /* we actually want to read the packet */
154                 if (last_packet_p) {
155                         eprintf("");
156                         /* there is already a packet that was previously read */
157                         memcpy(nic->packet, last_packet_p, last_packet_size);
158                         *size_p = last_packet_size;
159                         last_packet_p = NULL;
160                         return 0;
161                 }
162                 packet_p = nic->packet;
163         } else {
164                 /* we don't want to read the packet,
165                    just know if there is one. so we
166                    read the packet to a local buffer and
167                    we will return that buffer when the ip layer wants
168                    another packet */
169                 if (last_packet_p) {
170                         /* there is already a packet that
171                            was not consumend */
172                         eprintf("overflow receive packets");
173                         return -1;
174                 }
175                 packet_p = packet;
176         }
177
178         rc = ipoib_read_packet(&prot, packet_p + eth_header_len, &packet_len,
179                                &is_bcast);
180         if (rc) {
181                 printf("*** FATAL IN RECEIVE FLOW ****\n");
182                 goto fatal_handling;
183         }
184
185         if (packet_len == 0) {
186                 *size_p = 0;
187                 return 0;
188         }
189
190         if (is_bcast) {
191                 int i;
192                 for (i = 0; i < 6; ++i) {
193                         packet_p[i] = 0xff;
194                 }
195         } else {
196                 packet_p[0] = MLX_ETH_BYTE0;
197                 packet_p[1] = MLX_ETH_BYTE1;
198                 packet_p[2] = MLX_ETH_BYTE2;
199                 packet_p[3] = 0;
200                 packet_p[4] = 0;
201                 packet_p[5] = 0;
202         }
203
204         memset(packet_p + 6, 0, 6);
205
206         ptr = (__u16 *) (packet_p + 12);
207         *ptr = htons(prot);
208
209         if (!retrieve) {
210                 last_packet_p = packet;
211                 last_packet_size = packet_len + eth_header_len;
212                 *size_p = 0;
213         }
214
215         *size_p = packet_len + eth_header_len;
216         tprintf("packet size=%d, prot=%x\n", *size_p, prot);
217         if (0) {
218                 hd(nic->packet, 42);
219         }
220
221         return 0;
222
223 fatal_handling:
224         printf("restarting Etherboot\n");
225         sleep(1);
226         longjmp(restart_etherboot, -1);
227         /* we should not be here ... */
228         return -1; 
229         
230 }