1ff7914a39a5d268a1313d881dd58714420a17fe
[tpg/acess2.git] / UDI / drivers / net_ne2000 / ne2000_core.c
1 /*
2  * UDI Ne2000 NIC Driver
3  * By John Hodge (thePowersGang)
4  *
5  * ne2000_core.c
6  * - UDI initialisation 
7  */
8 #include "ne2000_common.h"
9
10 enum {
11         NE2K_META_BUS = 1,
12         NE2K_META_NIC,
13 };
14 enum {
15         NE2K_OPS_DEV = 1,
16         NE2K_OPS_CTRL,
17         NE2K_OPS_TX,
18         NE2K_OPS_RX,
19         NE2K_OPS_IRQ,
20 };
21 enum {
22         NE2K_CB_BUS_BIND = 1,
23         NE2K_CB_INTR,
24         NE2K_CB_INTR_EVENT,
25 };
26
27 #define NE2K_NUM_INTR_EVENT_CBS 4
28
29 // === GLOBALS ===
30 // --- Programmed IO ---
31 #include "ne2000_pio.h"
32
33 // === CODE ===
34 // --- Management
35 void ne2k_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
36 {
37         ne2k_rdata_t    *rdata = UDI_GCB(cb)->context;
38
39         rdata->rx_next_page = NE2K_RX_FIRST_PG;
40         
41         udi_usage_res(cb);
42 }
43 void ne2k_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
44 {
45         ne2k_rdata_t    *rdata = UDI_GCB(cb)->context;
46         udi_instance_attr_list_t *attr_list = cb->attr_list;
47         
48         switch(enumeration_level)
49         {
50         case UDI_ENUMERATE_START:
51         case UDI_ENUMERATE_START_RESCAN:
52                 // Emit the ND binding
53                 DPT_SET_ATTR32(attr_list, "if_num", 0);
54                 attr_list ++;
55                 DPT_SET_ATTR_STRING(attr_list, "if_media", "eth", 3);
56                 attr_list ++;
57                 NE2K_SET_ATTR_STRFMT(attr_list, "identifier", 2*6+1, "%02X%02X%02X%02X%02X%02X",
58                         rdata->macaddr[0], rdata->macaddr[1], rdata->macaddr[2],
59                         rdata->macaddr[3], rdata->macaddr[4], rdata->macaddr[5] );
60                 attr_list ++;
61                 cb->attr_valid_length = attr_list - cb->attr_list;
62                 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, NE2K_OPS_CTRL);
63                 break;
64         case UDI_ENUMERATE_NEXT:
65                 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
66                 break;
67         }
68 }
69 void ne2k_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
70 {
71 }
72 void ne2k_final_cleanup_req(udi_mgmt_cb_t *cb)
73 {
74 }
75 // --- Bus
76 void ne2k_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
77 {
78         udi_cb_t        *gcb = UDI_GCB(cb);
79         ne2k_rdata_t    *rdata = gcb->context;
80         
81         switch(cb->event)
82         {
83         case UDI_CHANNEL_CLOSED:
84                 break;
85         case UDI_CHANNEL_BOUND: {
86                 rdata->active_cb = gcb;
87                 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
88                 udi_bus_bind_req( bus_bind_cb );
89                 // continue at ne2k_bus_dev_bus_bind_ack
90                 return; }
91         }
92 }
93 void ne2k_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
94         udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
95 {
96         udi_cb_t        *gcb = UDI_GCB(cb);
97         ne2k_rdata_t    *rdata = gcb->context;
98         
99         // Set up PIO handles
100         rdata->init.pio_index = -1;
101         ne2k_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
102 }
103 void ne2k_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
104 {
105         ne2k_rdata_t    *rdata = gcb->context;
106         
107         if( rdata->init.pio_index != (udi_index_t)-1 )
108         {
109                 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
110                 udi_debug_printf("PIO %i = %p\n", rdata->init.pio_index, new_pio_handle);
111         }
112         rdata->init.pio_index ++;
113         if( rdata->init.pio_index < NE2K_NUM_PIO_OPS )
114         {
115                 udi_pio_map(ne2k_bus_dev_bind__pio_map, gcb,
116                         UDI_PCI_BAR_0, 0, 0x20,
117                         ne2k_pio_ops[rdata->init.pio_index].trans_list,
118                         ne2k_pio_ops[rdata->init.pio_index].list_length,
119                         UDI_PIO_LITTLE_ENDIAN, 0, 0
120                         );
121                 return ;
122         }
123         
124         // Next: Bind interrupt
125         // - spawn_idx = Interrupt number (0)
126         udi_channel_spawn(ne2k_bus_dev_bind__intr_chanel, gcb, gcb->channel,
127                 0, NE2K_OPS_IRQ, rdata);
128         // V V V V
129 }
130 void ne2k_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel)
131 {
132         ne2k_rdata_t    *rdata = gcb->context;
133         
134         rdata->interrupt_channel = new_channel;
135         
136         udi_cb_alloc(ne2k_bus_dev_bind__intr_attach, gcb, NE2K_CB_INTR, gcb->channel);
137         // V V V V
138 }
139 void ne2k_bus_dev_bind__intr_attach(udi_cb_t *gcb, udi_cb_t *new_cb)
140 {
141         ne2k_rdata_t    *rdata = gcb->context;
142         if( !new_cb )
143         {
144                 // Oh...
145                 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
146                         UDI_STAT_RESOURCE_UNAVAIL );
147                 return ;
148         }
149         udi_intr_attach_cb_t    *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
150         intr_cb->interrupt_idx = 0;
151         intr_cb->min_event_pend = 2;
152         intr_cb->preprocessing_handle = rdata->pio_handles[NE2K_PIO_IRQACK];
153         udi_intr_attach_req(intr_cb);
154         // continued in ne2k_bus_dev_intr_attach_ack
155 }
156 void ne2k_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
157 {
158 }
159 void ne2k_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
160 {
161         udi_cb_t        *gcb = UDI_GCB(intr_attach_cb);
162         ne2k_rdata_t    *rdata = gcb->context;
163         // continuing from ne2k_bus_dev_bind__intr_attach
164         if( status != UDI_OK ) {
165                 // TODO: Error
166                 udi_cb_free( UDI_GCB(intr_attach_cb) );
167                 return ;
168         }
169         
170         rdata->intr_attach_cb = intr_attach_cb;
171         
172         rdata->init.n_intr_event_cb = 0;
173         udi_cb_alloc(ne2k_bus_dev_bind__intr_event_cb, gcb, NE2K_CB_INTR_EVENT, rdata->interrupt_channel);
174         // V V V V
175 }
176 void ne2k_bus_dev_bind__intr_event_cb(udi_cb_t *gcb, udi_cb_t *new_cb)
177 {
178         ne2k_rdata_t    *rdata = gcb->context;
179         
180         udi_intr_event_cb_t *intr_event_cb = UDI_MCB(new_cb, udi_intr_event_cb_t);
181         udi_intr_event_rdy(intr_event_cb);
182         rdata->init.n_intr_event_cb ++;
183         
184         if( rdata->init.n_intr_event_cb < NE2K_NUM_INTR_EVENT_CBS )
185         {
186                 udi_cb_alloc(ne2k_bus_dev_bind__intr_event_cb, gcb,
187                         NE2K_CB_INTR_EVENT, rdata->interrupt_channel);
188                 // A A A A
189                 return ;
190         }
191
192         udi_pio_trans(ne2k_bus_dev_bind__card_reset, gcb,
193                 rdata->pio_handles[NE2K_PIO_RESET], 0, NULL, &rdata->macaddr);
194         // V V V V
195 }
196
197 void ne2k_bus_dev_bind__card_reset(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t result)
198 {       
199         ne2k_rdata_t    *rdata = gcb->context;
200         // Done! (Finally)
201         udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t), UDI_OK );
202         // = = = =
203 }
204 void ne2k_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
205 {
206 }
207 // --- ND Common
208 void ne2k_nd_ctrl_channel_event_ind(udi_channel_event_cb_t *cb)
209 {
210 }
211 void ne2k_nd_ctrl_bind_req(udi_nic_bind_cb_t *cb, udi_index_t tx_chan_index, udi_index_t rx_chan_index)
212 {
213         udi_cb_t        *gcb = UDI_GCB(cb);
214         ne2k_rdata_t    *rdata = gcb->context;
215         rdata->init.rx_chan_index = rx_chan_index;
216         udi_channel_spawn(ne2k_nd_ctrl_bind__tx_chan_ok, gcb, gcb->channel, tx_chan_index, NE2K_OPS_TX, rdata);
217         // V V V V
218 }
219 void ne2k_nd_ctrl_bind__tx_chan_ok(udi_cb_t *gcb, udi_channel_t new_channel)
220 {
221         ne2k_rdata_t    *rdata = gcb->context;
222         rdata->tx_channel = new_channel;
223         udi_channel_spawn(ne2k_nd_ctrl_bind__rx_chan_ok, gcb, gcb->channel,
224                 rdata->init.rx_chan_index, NE2K_OPS_RX, rdata);
225         // V V V V
226 }
227 void ne2k_nd_ctrl_bind__rx_chan_ok(udi_cb_t *gcb, udi_channel_t new_channel)
228 {
229         ne2k_rdata_t    *rdata = gcb->context;
230         rdata->rx_channel = new_channel;
231         
232         udi_nic_bind_cb_t *cb = UDI_MCB(gcb, udi_nic_bind_cb_t);
233         cb->media_type = UDI_NIC_ETHER;
234         cb->min_pdu_size = 0;
235         cb->max_pdu_size = 0;
236         cb->rx_hw_threshold = 2;
237         cb->capabilities = 0;
238         cb->max_perfect_multicast = 0;
239         cb->max_total_multicast = 0;
240         cb->mac_addr_len = 6;
241         memcpy(cb->mac_addr, rdata->macaddr, 6);
242         udi_nsr_bind_ack( cb, UDI_OK );
243         // = = = =
244 }
245 void ne2k_nd_ctrl_unbind_req(udi_nic_cb_t *cb)
246 {
247 }
248 void ne2k_nd_ctrl_enable_req(udi_nic_cb_t *cb)
249 {
250         udi_cb_t        *gcb = UDI_GCB(cb);
251         ne2k_rdata_t    *rdata = gcb->context;
252         udi_pio_trans(ne2k_nd_ctrl_enable_req__trans_done, gcb,
253                 rdata->pio_handles[NE2K_PIO_ENABLE], 0, NULL, NULL);
254 }
255 void ne2k_nd_ctrl_enable_req__trans_done(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res)
256 {
257         udi_nsr_enable_ack( UDI_MCB(gcb, udi_nic_cb_t), status );
258 }
259 void ne2k_nd_ctrl_disable_req(udi_nic_cb_t *cb)
260 {
261 }
262 void ne2k_nd_ctrl_ctrl_req(udi_nic_ctrl_cb_t *cb)
263 {
264 }
265 void ne2k_nd_ctrl_info_req(udi_nic_info_cb_t *cb, udi_boolean_t reset_statistics)
266 {
267 }
268 // --- IRQ
269 void ne2k_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
270 {
271 }
272 void ne2k_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
273 {
274         udi_debug_printf("ne2k_bus_irq_intr_event_ind: flags=%x, intr_result=%x\n",
275                 flags, cb->intr_result);
276         if( cb->intr_result & 0x01 )
277         {
278                 ne2k_intr__rx_ok( UDI_GCB(cb) );
279         }
280         if( cb->intr_result & 0x40 )
281         {
282                 // ne2k_intr__rdma( UDI_GCB(cb) );
283         }
284         // TODO: TX IRQs
285         udi_intr_event_rdy(cb);
286 }
287
288 // === Definition structures ===
289 udi_mgmt_ops_t  ne2k_mgmt_ops = {
290         ne2k_usage_ind,
291         ne2k_enumerate_req,
292         ne2k_devmgmt_req,
293         ne2k_final_cleanup_req
294 };
295 udi_ubit8_t     ne2k_mgmt_op_flags[4] = {0,0,0,0};
296 udi_bus_device_ops_t    ne2k_bus_dev_ops = {
297         ne2k_bus_dev_channel_event_ind,
298         ne2k_bus_dev_bus_bind_ack,
299         ne2k_bus_dev_bus_unbind_ack,
300         ne2k_bus_dev_intr_attach_ack,
301         ne2k_bus_dev_intr_detach_ack
302 };
303 udi_ubit8_t     ne2k_bus_dev_ops_flags[5] = {0};
304 udi_nd_ctrl_ops_t       ne2k_nd_ctrl_ops = {
305         ne2k_nd_ctrl_channel_event_ind,
306         ne2k_nd_ctrl_bind_req,
307         ne2k_nd_ctrl_unbind_req,
308         ne2k_nd_ctrl_enable_req,
309         ne2k_nd_ctrl_disable_req,
310         ne2k_nd_ctrl_ctrl_req,
311         ne2k_nd_ctrl_info_req
312 };
313 udi_ubit8_t     ne2k_nd_ctrl_ops_flags[7] = {0};
314 udi_nd_tx_ops_t ne2k_nd_tx_ops = {
315         ne2k_nd_tx_channel_event_ind,
316         ne2k_nd_tx_tx_req,
317         ne2k_nd_tx_exp_tx_req
318 };
319 udi_ubit8_t     ne2k_nd_tx_ops_flags[3] = {0};
320 udi_nd_rx_ops_t ne2k_nd_rx_ops = {
321         ne2k_nd_rx_channel_event_ind,
322         ne2k_nd_rx_rx_rdy
323 };
324 udi_ubit8_t     ne2k_nd_rx_ops_flags[2] = {0};
325 udi_intr_handler_ops_t  ne2k_bus_irq_ops = {
326         ne2k_bus_irq_channel_event_ind,
327         ne2k_bus_irq_intr_event_ind
328 };
329 udi_ubit8_t     ne2k_bus_irq_ops_flags[2] = {0};
330
331 udi_primary_init_t      ne2k_pri_init = {
332         .mgmt_ops = &ne2k_mgmt_ops,
333         .mgmt_op_flags = ne2k_mgmt_op_flags,
334         .mgmt_scratch_requirement = 0,
335         .enumeration_attr_list_length = 4,
336         .rdata_size = sizeof(ne2k_rdata_t),
337         .child_data_size = 0,
338         .per_parent_paths = 0
339 };
340 udi_ops_init_t  ne2k_ops_list[] = {
341         {
342                 NE2K_OPS_DEV, NE2K_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
343                 0,
344                 (udi_ops_vector_t*)&ne2k_bus_dev_ops,
345                 ne2k_bus_dev_ops_flags
346         },
347         {
348                 NE2K_OPS_CTRL, NE2K_META_NIC, UDI_ND_CTRL_OPS_NUM,
349                 0,
350                 (udi_ops_vector_t*)&ne2k_nd_ctrl_ops,
351                 ne2k_nd_ctrl_ops_flags
352         },
353         {
354                 NE2K_OPS_TX, NE2K_META_NIC, UDI_ND_TX_OPS_NUM,
355                 0,
356                 (udi_ops_vector_t*)&ne2k_nd_tx_ops,
357                 ne2k_nd_tx_ops_flags
358         },
359         {
360                 NE2K_OPS_RX, NE2K_META_NIC, UDI_ND_RX_OPS_NUM,
361                 0,
362                 (udi_ops_vector_t*)&ne2k_nd_rx_ops,
363                 ne2k_nd_rx_ops_flags
364         },
365         {
366                 NE2K_OPS_IRQ, NE2K_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
367                 0,
368                 (udi_ops_vector_t*)&ne2k_bus_irq_ops,
369                 ne2k_bus_irq_ops_flags
370         },
371         {0}
372 };
373 udi_cb_init_t ne2k_cb_init_list[] = {
374         {NE2K_CB_BUS_BIND, NE2K_META_BUS, UDI_BUS_BIND_CB_NUM, 0, 0,NULL},
375         {NE2K_CB_INTR, NE2K_META_BUS, UDI_BUS_INTR_ATTACH_CB_NUM, 0, 0,NULL},
376         {NE2K_CB_INTR_EVENT, NE2K_META_BUS, UDI_BUS_INTR_EVENT_CB_NUM, 0, 0,NULL},
377         {0}
378 };
379 const udi_init_t        udi_init_info = {
380         .primary_init_info = &ne2k_pri_init,
381         .ops_init_list = ne2k_ops_list,
382         .cb_init_list = ne2k_cb_init_list,
383 };

UCC git Repository :: git.ucc.asn.au