You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

skeleton.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/netdevice.h>
  30. #include <ipxe/ethernet.h>
  31. #include <ipxe/if_ether.h>
  32. #include <ipxe/iobuf.h>
  33. #include <ipxe/malloc.h>
  34. #include <ipxe/pci.h>
  35. #include <ipxe/mii.h>
  36. #include "skeleton.h"
  37. /** @file
  38. *
  39. * Skeleton network driver
  40. *
  41. */
  42. /******************************************************************************
  43. *
  44. * MII interface
  45. *
  46. ******************************************************************************
  47. */
  48. /**
  49. * Read from MII register
  50. *
  51. * @v mii MII interface
  52. * @v reg Register address
  53. * @ret value Data read, or negative error
  54. */
  55. static int skeleton_mii_read ( struct mii_interface *mii, unsigned int reg ) {
  56. struct skeleton_nic *skel =
  57. container_of ( mii, struct skeleton_nic, mii );
  58. DBGC ( skel, "SKELETON %p does not yet support MII read\n", skel );
  59. ( void ) reg;
  60. return -ENOTSUP;
  61. }
  62. /**
  63. * Write to MII register
  64. *
  65. * @v mii MII interface
  66. * @v reg Register address
  67. * @v data Data to write
  68. * @ret rc Return status code
  69. */
  70. static int skeleton_mii_write ( struct mii_interface *mii, unsigned int reg,
  71. unsigned int data) {
  72. struct skeleton_nic *skel =
  73. container_of ( mii, struct skeleton_nic, mii );
  74. DBGC ( skel, "SKELETON %p does not yet support MII write\n", skel );
  75. ( void ) reg;
  76. ( void ) data;
  77. return -ENOTSUP;
  78. }
  79. /** Skeleton MII operations */
  80. static struct mii_operations skeleton_mii_operations = {
  81. .read = skeleton_mii_read,
  82. .write = skeleton_mii_write,
  83. };
  84. /******************************************************************************
  85. *
  86. * Device reset
  87. *
  88. ******************************************************************************
  89. */
  90. /**
  91. * Reset hardware
  92. *
  93. * @v skel Skeleton device
  94. * @ret rc Return status code
  95. */
  96. static int skeleton_reset ( struct skeleton_nic *skel ) {
  97. DBGC ( skel, "SKELETON %p does not yet support reset\n", skel );
  98. return -ENOTSUP;
  99. }
  100. /******************************************************************************
  101. *
  102. * Link state
  103. *
  104. ******************************************************************************
  105. */
  106. /**
  107. * Check link state
  108. *
  109. * @v netdev Network device
  110. */
  111. static void skeleton_check_link ( struct net_device *netdev ) {
  112. struct skeleton_nic *skel = netdev->priv;
  113. DBGC ( skel, "SKELETON %p does not yet support link state\n", skel );
  114. netdev_link_err ( netdev, -ENOTSUP );
  115. }
  116. /******************************************************************************
  117. *
  118. * Network device interface
  119. *
  120. ******************************************************************************
  121. */
  122. /**
  123. * Open network device
  124. *
  125. * @v netdev Network device
  126. * @ret rc Return status code
  127. */
  128. static int skeleton_open ( struct net_device *netdev ) {
  129. struct skeleton_nic *skel = netdev->priv;
  130. DBGC ( skel, "SKELETON %p does not yet support open\n", skel );
  131. return -ENOTSUP;
  132. }
  133. /**
  134. * Close network device
  135. *
  136. * @v netdev Network device
  137. */
  138. static void skeleton_close ( struct net_device *netdev ) {
  139. struct skeleton_nic *skel = netdev->priv;
  140. DBGC ( skel, "SKELETON %p does not yet support close\n", skel );
  141. }
  142. /**
  143. * Transmit packet
  144. *
  145. * @v netdev Network device
  146. * @v iobuf I/O buffer
  147. * @ret rc Return status code
  148. */
  149. static int skeleton_transmit ( struct net_device *netdev,
  150. struct io_buffer *iobuf ) {
  151. struct skeleton_nic *skel = netdev->priv;
  152. DBGC ( skel, "SKELETON %p does not yet support transmit\n", skel );
  153. ( void ) iobuf;
  154. return -ENOTSUP;
  155. }
  156. /**
  157. * Poll for completed and received packets
  158. *
  159. * @v netdev Network device
  160. */
  161. static void skeleton_poll ( struct net_device *netdev ) {
  162. struct skeleton_nic *skel = netdev->priv;
  163. /* Not yet implemented */
  164. ( void ) skel;
  165. }
  166. /**
  167. * Enable or disable interrupts
  168. *
  169. * @v netdev Network device
  170. * @v enable Interrupts should be enabled
  171. */
  172. static void skeleton_irq ( struct net_device *netdev, int enable ) {
  173. struct skeleton_nic *skel = netdev->priv;
  174. DBGC ( skel, "SKELETON %p does not yet support interrupts\n", skel );
  175. ( void ) enable;
  176. }
  177. /** Skeleton network device operations */
  178. static struct net_device_operations skeleton_operations = {
  179. .open = skeleton_open,
  180. .close = skeleton_close,
  181. .transmit = skeleton_transmit,
  182. .poll = skeleton_poll,
  183. .irq = skeleton_irq,
  184. };
  185. /******************************************************************************
  186. *
  187. * PCI interface
  188. *
  189. ******************************************************************************
  190. */
  191. /**
  192. * Probe PCI device
  193. *
  194. * @v pci PCI device
  195. * @ret rc Return status code
  196. */
  197. static int skeleton_probe ( struct pci_device *pci ) {
  198. struct net_device *netdev;
  199. struct skeleton_nic *skel;
  200. int rc;
  201. /* Allocate and initialise net device */
  202. netdev = alloc_etherdev ( sizeof ( *skel ) );
  203. if ( ! netdev ) {
  204. rc = -ENOMEM;
  205. goto err_alloc;
  206. }
  207. netdev_init ( netdev, &skeleton_operations );
  208. skel = netdev->priv;
  209. pci_set_drvdata ( pci, netdev );
  210. netdev->dev = &pci->dev;
  211. memset ( skel, 0, sizeof ( *skel ) );
  212. /* Fix up PCI device */
  213. adjust_pci_device ( pci );
  214. /* Map registers */
  215. skel->regs = ioremap ( pci->membase, SKELETON_BAR_SIZE );
  216. if ( ! skel->regs ) {
  217. rc = -ENODEV;
  218. goto err_ioremap;
  219. }
  220. /* Reset the NIC */
  221. if ( ( rc = skeleton_reset ( skel ) ) != 0 )
  222. goto err_reset;
  223. /* Initialise and reset MII interface */
  224. mii_init ( &skel->mii, &skeleton_mii_operations );
  225. if ( ( rc = mii_reset ( &skel->mii ) ) != 0 ) {
  226. DBGC ( skel, "SKELETON %p could not reset MII: %s\n",
  227. skel, strerror ( rc ) );
  228. goto err_mii_reset;
  229. }
  230. /* Register network device */
  231. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  232. goto err_register_netdev;
  233. /* Set initial link state */
  234. skeleton_check_link ( netdev );
  235. return 0;
  236. unregister_netdev ( netdev );
  237. err_register_netdev:
  238. err_mii_reset:
  239. skeleton_reset ( skel );
  240. err_reset:
  241. iounmap ( skel->regs );
  242. err_ioremap:
  243. netdev_nullify ( netdev );
  244. netdev_put ( netdev );
  245. err_alloc:
  246. return rc;
  247. }
  248. /**
  249. * Remove PCI device
  250. *
  251. * @v pci PCI device
  252. */
  253. static void skeleton_remove ( struct pci_device *pci ) {
  254. struct net_device *netdev = pci_get_drvdata ( pci );
  255. struct skeleton_nic *skel = netdev->priv;
  256. /* Unregister network device */
  257. unregister_netdev ( netdev );
  258. /* Reset card */
  259. skeleton_reset ( skel );
  260. /* Free network device */
  261. iounmap ( skel->regs );
  262. netdev_nullify ( netdev );
  263. netdev_put ( netdev );
  264. }
  265. /** Skeleton PCI device IDs */
  266. static struct pci_device_id skeleton_nics[] = {
  267. PCI_ROM ( 0x5ce1, 0x5ce1, "skel", "Skeleton", 0 ),
  268. };
  269. /** Skeleton PCI driver */
  270. struct pci_driver skeleton_driver __pci_driver = {
  271. .ids = skeleton_nics,
  272. .id_count = ( sizeof ( skeleton_nics ) / sizeof ( skeleton_nics[0] ) ),
  273. .probe = skeleton_probe,
  274. .remove = skeleton_remove,
  275. };