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.

mt25218.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**************************************************************************
  2. Etherboot - BOOTP/TFTP Bootstrap Program
  3. Skeleton NIC driver for Etherboot
  4. ***************************************************************************/
  5. /*
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2, or (at
  9. * your option) any later version.
  10. */
  11. #include <errno.h>
  12. #include <gpxe/pci.h>
  13. #include <gpxe/iobuf.h>
  14. #include <gpxe/netdevice.h>
  15. #include <gpxe/infiniband.h>
  16. /* to get some global routines like printf */
  17. #include "etherboot.h"
  18. /* to get the interface to the body of the program */
  19. #include "nic.h"
  20. #include "mt25218_imp.c"
  21. struct mlx_nic {
  22. /** Queue pair handle */
  23. udqp_t ipoib_qph;
  24. /** Broadcast Address Vector */
  25. ud_av_t bcast_av;
  26. /** Send completion queue */
  27. cq_t snd_cqh;
  28. /** Receive completion queue */
  29. cq_t rcv_cqh;
  30. };
  31. /**
  32. * Open network device
  33. *
  34. * @v netdev Network device
  35. * @ret rc Return status code
  36. */
  37. static int mlx_open ( struct net_device *netdev ) {
  38. ( void ) netdev;
  39. return 0;
  40. }
  41. /**
  42. * Close network device
  43. *
  44. * @v netdev Network device
  45. */
  46. static void mlx_close ( struct net_device *netdev ) {
  47. ( void ) netdev;
  48. }
  49. #warning "Broadcast address?"
  50. static uint8_t ib_broadcast[IB_ALEN] = { 0xff, };
  51. /**
  52. * Transmit packet
  53. *
  54. * @v netdev Network device
  55. * @v iobuf I/O buffer
  56. * @ret rc Return status code
  57. */
  58. static int mlx_transmit ( struct net_device *netdev,
  59. struct io_buffer *iobuf ) {
  60. struct mlx_nic *mlx = netdev->priv;
  61. ud_send_wqe_t snd_wqe;
  62. int rc;
  63. snd_wqe = alloc_send_wqe ( mlx->ipoib_qph );
  64. if ( ! snd_wqe ) {
  65. DBGC ( mlx, "MLX %p out of TX WQEs\n", mlx );
  66. return -ENOBUFS;
  67. }
  68. prep_send_wqe_buf ( mlx->ipoib_qph, mlx->bcast_av, snd_wqe,
  69. iobuf->data, 0, iob_len ( iobuf ), 0 );
  70. if ( ( rc = post_send_req ( mlx->ipoib_qph, snd_wqe, 1 ) ) != 0 ) {
  71. DBGC ( mlx, "MLX %p could not post TX WQE %p: %s\n",
  72. mlx, snd_wqe, strerror ( rc ) );
  73. free_wqe ( snd_wqe );
  74. return rc;
  75. }
  76. return 0;
  77. }
  78. /**
  79. * Handle TX completion
  80. *
  81. * @v netdev Network device
  82. * @v ib_cqe Completion queue entry
  83. */
  84. static void mlx_tx_complete ( struct net_device *netdev,
  85. struct ib_cqe_st *ib_cqe ) {
  86. netdev_tx_complete_next_err ( netdev,
  87. ( ib_cqe->is_error ? -EIO : 0 ) );
  88. }
  89. /**
  90. * Handle RX completion
  91. *
  92. * @v netdev Network device
  93. * @v ib_cqe Completion queue entry
  94. */
  95. static void mlx_rx_complete ( struct net_device *netdev,
  96. struct ib_cqe_st *ib_cqe ) {
  97. unsigned int len;
  98. struct io_buffer *iobuf;
  99. void *buf;
  100. /* Check for errors */
  101. if ( ib_cqe->is_error ) {
  102. netdev_rx_err ( netdev, NULL, -EIO );
  103. return;
  104. }
  105. /* Allocate I/O buffer */
  106. len = ( ib_cqe->count - GRH_SIZE );
  107. iobuf = alloc_iob ( len );
  108. if ( ! iobuf ) {
  109. netdev_rx_err ( netdev, NULL, -ENOMEM );
  110. return;
  111. }
  112. /* Fill I/O buffer */
  113. buf = get_rcv_wqe_buf ( ib_cqe->wqe, 1 );
  114. memcpy ( iob_put ( iobuf, len ), buf, len );
  115. /* Hand off to network stack */
  116. netdev_rx ( netdev, iobuf );
  117. }
  118. /**
  119. * Poll completion queue
  120. *
  121. * @v netdev Network device
  122. * @v cq Completion queue
  123. * @v handler Completion handler
  124. */
  125. static void mlx_poll_cq ( struct net_device *netdev, cq_t cq,
  126. void ( * handler ) ( struct net_device *netdev,
  127. struct ib_cqe_st *ib_cqe ) ) {
  128. struct mlx_nic *mlx = netdev->priv;
  129. struct ib_cqe_st ib_cqe;
  130. uint8_t num_cqes;
  131. while ( 1 ) {
  132. /* Poll for single completion queue entry */
  133. ib_poll_cq ( cq, &ib_cqe, &num_cqes );
  134. /* Return if no entries in the queue */
  135. if ( ! num_cqes )
  136. return;
  137. DBGC ( mlx, "MLX %p cpl in %p: err %x send %x "
  138. "wqe %p count %lx\n", mlx, cq, ib_cqe.is_error,
  139. ib_cqe.is_send, ib_cqe.wqe, ib_cqe.count );
  140. /* Handle TX/RX completion */
  141. handler ( netdev, &ib_cqe );
  142. /* Free associated work queue entry */
  143. free_wqe ( ib_cqe.wqe );
  144. }
  145. }
  146. /**
  147. * Poll for completed and received packets
  148. *
  149. * @v netdev Network device
  150. */
  151. static void mlx_poll ( struct net_device *netdev ) {
  152. struct mlx_nic *mlx = netdev->priv;
  153. int rc;
  154. if ( ( rc = poll_error_buf() ) != 0 ) {
  155. DBG ( "poll_error_buf() failed: %s\n", strerror ( rc ) );
  156. return;
  157. }
  158. /* Drain event queue. We can ignore events, since we're going
  159. * to just poll all completion queues anyway.
  160. */
  161. if ( ( rc = drain_eq() ) != 0 ) {
  162. DBG ( "drain_eq() failed: %s\n", strerror ( rc ) );
  163. return;
  164. }
  165. /* Poll completion queues */
  166. mlx_poll_cq ( netdev, mlx->snd_cqh, mlx_tx_complete );
  167. mlx_poll_cq ( netdev, mlx->rcv_cqh, mlx_rx_complete );
  168. }
  169. /**
  170. * Enable or disable interrupts
  171. *
  172. * @v netdev Network device
  173. * @v enable Interrupts should be enabled
  174. */
  175. static void mlx_irq ( struct net_device *netdev, int enable ) {
  176. ( void ) netdev;
  177. ( void ) enable;
  178. }
  179. static struct net_device_operations mlx_operations = {
  180. .open = mlx_open,
  181. .close = mlx_close,
  182. .transmit = mlx_transmit,
  183. .poll = mlx_poll,
  184. .irq = mlx_irq,
  185. };
  186. /**
  187. * Remove PCI device
  188. *
  189. * @v pci PCI device
  190. */
  191. static void mlx_remove ( struct pci_device *pci ) {
  192. struct net_device *netdev = pci_get_drvdata ( pci );
  193. unregister_netdev ( netdev );
  194. ib_driver_close ( 0 );
  195. netdev_nullify ( netdev );
  196. netdev_put ( netdev );
  197. }
  198. /**
  199. * Probe PCI device
  200. *
  201. * @v pci PCI device
  202. * @v id PCI ID
  203. * @ret rc Return status code
  204. */
  205. static int mlx_probe ( struct pci_device *pci,
  206. const struct pci_device_id *id __unused ) {
  207. struct net_device *netdev;
  208. struct mlx_nic *mlx;
  209. struct ib_mac *mac;
  210. udqp_t qph;
  211. int rc;
  212. /* Allocate net device */
  213. netdev = alloc_ibdev ( sizeof ( *mlx ) );
  214. if ( ! netdev )
  215. return -ENOMEM;
  216. netdev_init ( netdev, &mlx_operations );
  217. mlx = netdev->priv;
  218. pci_set_drvdata ( pci, netdev );
  219. netdev->dev = &pci->dev;
  220. memset ( mlx, 0, sizeof ( *mlx ) );
  221. /* Fix up PCI device */
  222. adjust_pci_device ( pci );
  223. /* Initialise hardware */
  224. if ( ( rc = ib_driver_init ( pci, &qph ) ) != 0 )
  225. goto err_ipoib_init;
  226. mlx->ipoib_qph = qph;
  227. mlx->bcast_av = ib_data.bcast_av;
  228. mlx->snd_cqh = ib_data.ipoib_snd_cq;
  229. mlx->rcv_cqh = ib_data.ipoib_rcv_cq;
  230. mac = ( ( struct ib_mac * ) netdev->ll_addr );
  231. mac->qpn = htonl ( ib_get_qpn ( mlx->ipoib_qph ) );
  232. memcpy ( &mac->gid, ib_data.port_gid.raw, sizeof ( mac->gid ) );
  233. /* Register network device */
  234. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  235. goto err_register_netdev;
  236. return 0;
  237. err_register_netdev:
  238. err_ipoib_init:
  239. ib_driver_close ( 0 );
  240. netdev_nullify ( netdev );
  241. netdev_put ( netdev );
  242. return rc;
  243. }
  244. static struct pci_device_id mlx_nics[] = {
  245. PCI_ROM ( 0x15b3, 0x6282, "MT25218", "MT25218 HCA driver" ),
  246. PCI_ROM ( 0x15b3, 0x6274, "MT25204", "MT25204 HCA driver" ),
  247. };
  248. struct pci_driver mlx_driver __pci_driver = {
  249. .ids = mlx_nics,
  250. .id_count = ( sizeof ( mlx_nics ) / sizeof ( mlx_nics[0] ) ),
  251. .probe = mlx_probe,
  252. .remove = mlx_remove,
  253. };