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

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