URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [eth/] [v2_0/] [doc/] [driver_doc] - Rev 174
Compare with Previous | Blame | View Log
This file provides a simple description of how to write a low-level,hardware dependent ethernet driver.The basic idea is that there is a high-level driver (which is onlycode/functions) that is part of the stack. There will be one or morelow-level driver tied to the actual network hardware. Each of thesedrivers contains one or more driver instances. The principal idea isthat the low-level drivers know nothing of the details of the stack thatwill be using them. Thus, the same driver can be used by the eCossupported TCP/IP stack, or any other, with no changes.A driver instance is contained within a "struct eth_drv_sc".struct eth_hwr_funs {// Initialize hardware (including startup)void (*start)(struct eth_drv_sc *sc,unsigned char *enaddr);// Shut down hardwarevoid (*stop)(struct eth_drv_sc *sc);// Control interfaceint (*control)(struct eth_drv_sc *sc,unsigned long cmd,void *data,int len);// Query interface - can a packet be sent?int (*can_send)(struct eth_drv_sc *sc);// Send a packet of datavoid (*send)(struct eth_drv_sc *sc,struct eth_drv_sg *sg_list,int sg_len,int total_len,unsigned long key);// Receive [unload] a packet of datavoid (*recv)(struct eth_drv_sc *sc,struct eth_drv_sg *sg_list,int sg_len);// Deliver data to/from device from/to stack memory space// (moves lots of memcpy()s out of DSRs into thread)void (*deliver)(struct eth_drv_sc *sc);// Poll for interrupts/device servicevoid (*poll)(struct eth_drv_sc *sc);// Get interrupt information from hardware driverint (*int_vector)(struct eth_drv_sc *sc);// Logical driver interfacestruct eth_drv_funs *eth_drv, *eth_drv_old;};struct eth_drv_sc {struct eth_hwr_funs *funs;void *driver_private;const char *dev_name;struct arpcom sc_arpcom; /* ethernet common */};You create one of these instances using the "ETH_DRV_SC()" macro whichsets up the structure, including the prototypes for the functions, etc.By doing things this way, if the internal design of the ethernet driverschanges (e.g. we need to add a new low-level implementation function),existing drivers will no longer compile until updated. This is muchbetter than to have all of the definitions in the low-level driversthemselves and have them be [quietly] broken if the interfaces change.The "magic" which gets the drivers started [and indeed, linked] issimilar to what is used for the I/O subsystem. [Note: I may try andmake it part of the I/O subsystem later.] This is done using the"NETDEVTAB_ENTRY()" macro, which defines an initialization functionand the basic data structures for the low-level driver.typedef struct cyg_netdevtab_entry {const char *name;bool (*init)(struct cyg_netdevtab_entry *tab);void *device_instance;unsigned long status;} cyg_netdevtab_entry_t;The "device_instance" entry here would point to the "structeth_drv_sc" entry previously defined. This allows the network driversetup to work with any class of driver, not just ethernet drivers. Inthe future, there will surely be serial PPP drivers, etc. These willuse the "NETDEVTAB" setup to create the basic driver, but they willmost likely be built on top of other high-level device driver layers.So, the bottom line is that a hardware driver will have a template(boilerplate) which looks like this:#include <cyg/infra/cyg_type.h>#include <cyg/hal/hal_arch.h>#include <cyg/infra/diag.h>#include <cyg/hal/drv_api.h>#include <cyg/io/eth/netdev.h>#include <cyg/io/eth/eth_drv.h>ETH_DRV_SC(DRV_sc,0, // No driver specific data needed"eth0", // Name for this interfaceHRDWR_start,HRDWR_stop,HRDWR_control,HRDWR_can_sendHRDWR_send,HRDWR_recv);NETDEVTAB_ENTRY(DRV_netdev,"DRV",DRV_HRDWR_init,&DRV_sc);This, along with the referenced functions, completely define the driver.Extensibility note: if one needed the same low-level driver to handlemultiple similar hardware interfaces, you would need multiple invocationsof the "ETH_DRV_SC()/NETDEVTAB_ENTRY()" macros. You would add a pointerto some instance specific data, e.g. containing base addresses, interruptnumbers, etc, where the "0, // No driver specific data" is currently.Now a quick waltz through the functions. This discussion will use thegeneric names from above.static bool DRV_HDWR_init(struct cyg_netdevtab_entry *tab)==========================================================This function is called as part of system initialization. Its primaryfunction is to decide if the hardware [as indicated viatab->device_instance] is working and if the interface needs to be madeavailable in the system. If this is the case, this function needs tofinish with a call to the ethernet driver function:eth_drv_init((struct eth_drv_sc *)tab->device_instance,unsigned char *enaddr);where 'enaddr' is a pointer to the ethernet station address for thisunit. Note: the ethernet station address is supposed to be aworld-unique, 48 bit address for this particular ethernet interface.Typically it is provided by the board/hardware manufacturer in ROM.In many packages it is possible for the ESA to be set from RedBoot,(perhaps from 'fconfig' data), hard-coded from CDL, or from an EPROM.A driver should choose a run-time specified ESA (e.g. from RedBoot)preferentially, otherwise (in order) it should use a CDL specifiedESA if one has been set, otherwise an EPROM set ESA, or otherwisefail. See the cl/cs8900a eth driver for an example.static voidHRDWR_start(struct eth_drv_sc *sc, unsigned char *enaddr, int flags)====================================================================This function is called, perhaps much later than system initializationtime, when the system (an application) is ready for the interface tobecome active. The purpose of this function is to set up the hardwareinterface to start accepting packets from the network and be able tosend packets out. Note: this function will be called whenever theup/down state of the logical interface changes, e.g. when the IP addresschanges. This may occur more than one time, so this function needs tobe prepared for that case.FUTURE: the "flags" field (currently unused) may be used to tell thefunction how to start up, e.g. whether interrupts will be used,selection of "promiscuous" mode etc.static void HRDWR_stop(struct eth_drv_sc *sc)=============================================This function is the inverse of "start". It should shut down thehardware and keep it from interacting with the physical network.static intHRDWR_control(struct eth_drv_sc *sc, unsigned long key, void *data, int len)============================================================================This function is used to perform low-level "control" operations on theinterface. These operations would be initiated via 'ioctl()' in the BSDstack, and would be anything that would require the hardware setup topossibly change (i.e. cannot be performed totally by theplatform-independent layers).Current operations:ETH_DRV_SET_MAC_ADDRESS:This function sets the ethernet station address (ESA or MAC) for thedevice. Normally this address is kept in non-volatile memory and isunique in the world. This function must at least set the interface touse the new address. It may also update the NVM as appropriate.This function should return zero if the specified operation wascompleted successfully. It should return non-zero if the operationcould not be performed, for any reason.static int HRDWR_can_send(struct eth_drv_sc *sc)================================================This function is called to determine if it is possible to start thetransmission of a packet on the interface. Some interfaces will allowmultiple packets to be "queued" and this function allows for the highestpossible utilization of that mode.Return the number of packets which could be accepted at this time, zeroimplies that the interface is saturated/busy.static voidHRDWR_send(struct eth_drv_sc *sc, struct eth_drv_sg *sg_list, int sg_len,int total_len, unsigned long key)=========================================================================This function is used to send a packet of data to the network. It isthe responsibility of this function to somehow hand the data over to thehardware interface. This will most likely require copying, but just theaddress/length values could be used by smart hardware.NOTE: All data in/out of the driver is specified via a "scatter-gather"list. This is just an array of address/length pairs which describesections of data to move (in the order given by the array).Once the data has been successfully sent by the interface (or if anerror occurs), the driver should call 'eth_drv_tx_done()' using thespecified 'key'. Only then will the upper layers release the resourcesfor that packet and start another transmission.FUTURE: This function may be extended so that the data need not becopied by having the function return a "disposition" code (done, sendpending, etc). At this point, you should move the data to some "safe"location before returning.static voidHRDWR_recv(struct eth_drv_sc *sc, struct eth_drv_sg *sg_list, int sg_len)=========================================================================This function is actually a call back, only invoked after theupper-level functioneth_drv_recv(struct eth_drv_sc *sc, int total_len)has been called. This upper level function is called by the hardwaredriver when it knows that a packet of data is available on theinterface. The 'eth_drv_recv()' function then arranges network buffersand structures for the data and then calls "HRDWR_recv()" to actuallymove the data from the interface.static voidHRDWR_deliver(struct eth_drv_sc *sc)=========================================================================This function is actually a call back, and notifies the driver that deliveryis happening. This allows it to actually do the copy of packet data to/fromthe hardware from/to the packet buffer. And once that's done, then do thingslike unmask its interrupts, and free any relevant resources so it can processfurther packets.In general it will be called from the user thread responsible for deliveringnetwork packets.static voidHRDWR_poll(struct eth_drv_sc *sc)=========================================================================This function is used when in a non-interrupt driven system, e.g. wheninterrupts are completely disabled. This allows the driver time to checkwhether anything needs doing either for transmission, or to check ifanything has been received, or if any other processing needs doing..static intHRDWR_int_vector(struct eth_drv_sc *sc)=========================================================================This function returns the interrupt vector number used for RX interrupts.This is so the common GDB stubs infrastructure can detect when to checkfor incoming ctrl-c's when doing debugging over ethernet.Upper layer functions - called by drivers=========================================These functions are defined by the upper layers (machine independent) ofthe networking driver support. They are present to hide the interfacesto the actual networking stack so that the hardware drivers may possiblybe used by any network stack implementation.These functions require a pointer to a "struct eth_drv_sc" table whichdescribes the interface at a logical level. It is assumed that thedriver [lowest level hardware support] will keep track of this pointerso it may be passed "up" as appropriate.struct eth_drv_sc {struct eth_drv_funs *funs; // Pointer to hardware functions (see above)void *driver_private; // Device specific dataconst char *dev_name;struct arpcom sc_arpcom; // ethernet common};This structure is created, one per logical interface, via ETH_DRV_SC macro.void eth_drv_init(struct eth_drv_sc *sc, unsigned char *enaddr)===============================================================This function establishes the device at initialization time. Thehardware should be totally intialized (not "started") when this functionis called.void eth_drv_tx_done(struct eth_drv_sc *sc, unsigned long key, int status)==========================================================================This function is called when a packet completes transmission on theinterface. The 'key' value must be one of the keys provided to"HRDWR_send()" above. The value 'status' should be non-zero (currentlyundefined) to indicate that an error occurred during the transmission.void eth_drv_recv(struct eth_drv_sc *sc, int len)=================================================This function is called to indicate that a packet of length 'len' hasarrived at the interface. The callback "HRDWR_recv()" functiondescribed above will be used to actually unload the data from theinterface into buffers used by the machine independent layers.Calling graph for Transmit and Receive--------------------------------------It may be worth clarifying further the flow of control in the transmitand receive cases, where the hardware driver does use interrupts and soDSRs to tell the "foreground" when something asynchronous has occurred.Transmit:Foreground task calls into network stack to send a packet (or thestack decides to send a packet in response to incoming traffic).The driver calls the HRDWR_can_send() function in the hardware driver.HRDWR_can_send() returns the number of available "slots" in which itcan store a pending transmit packet.If it cannot send at this time, the packet is queued outside thehardware driver for later; in this case, the hardware is already busytransmitting, so expect an interrupt as described below for completionof the packet currently outgoing.If it can send right now, HRDWR_send() is called.HRDWR_send() copies the data into special hardware buffers, orinstructs the hardware to "send that".It also remembers the key that is associated with this tx request.these calls return....Asynchronously, the hardware makes an interrupt to say "transmit isdone"; the ISR quietens the interrupt source in the hardware andrequests that the associated DSR be run.The DSR realizes that a transmit request has completed, and callseth_drv_tx_done() with the same key that it remembered for this tx.eth_drv_tx_done() uses the key to find the resources associated withthis transmit request; thus the stack knows that the transmit hascompleted and its resources can be freed.eth_drv_tx_done() also enquires whether HRDWR_can_send() now says"yes, we can send" and if so, dequeues a further transmit requestwhich may have been queued as described above. If so:HRDWR_send() copies the data into the hardware buffers, orinstructs the hardware to "send that" and remembers the new key.these calls return to the DSR and thus to the foreground....Receive:...Asynchronously, the hardware makes an interrupt to say "there is readydata in a receive buffer"; the ISR quietens the interrupt source inthe hardware and requests that the associated DSR be run.The DSR realizes that there is data ready and calls eth_drv_recv()with the length of the data that is available.eth_drv_recv() prepares a set of scatter-gather buffers that canaccommodate that data.It then calls back into the hardware driver routine HRDWR_recv().HRDWR_recv() must copy the data from the hardware's buffers intothe scatter-gather buffers provided, and return.eth_drv_recv() sends the new packet up the network stack and returns.Back in the DSR now, the driver cleans the receive buffer and returnsit to the hardware's control, available to receive another packet fromthe network.The DSR returns to the foreground....
