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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 arbel_send_work_queue {
  22. /** Doorbell number */
  23. unsigned int doorbell_idx;
  24. /** Work queue entries */
  25. // struct ud_send_wqe_st *wqe;
  26. union ud_send_wqe_u *wqe_u;
  27. };
  28. struct arbel {
  29. /** User Access Region */
  30. void *uar;
  31. /** Doorbell records */
  32. union db_record_st *db_rec;
  33. };
  34. struct mlx_nic {
  35. /** Queue pair handle */
  36. udqp_t ipoib_qph;
  37. /** Broadcast Address Vector */
  38. ud_av_t bcast_av;
  39. /** Send completion queue */
  40. cq_t snd_cqh;
  41. /** Receive completion queue */
  42. cq_t rcv_cqh;
  43. };
  44. /**
  45. * Open network device
  46. *
  47. * @v netdev Network device
  48. * @ret rc Return status code
  49. */
  50. static int mlx_open ( struct net_device *netdev ) {
  51. ( void ) netdev;
  52. return 0;
  53. }
  54. /**
  55. * Close network device
  56. *
  57. * @v netdev Network device
  58. */
  59. static void mlx_close ( struct net_device *netdev ) {
  60. ( void ) netdev;
  61. }
  62. #warning "Broadcast address?"
  63. static uint8_t ib_broadcast[IB_ALEN] = { 0xff, };
  64. /**
  65. * Transmit packet
  66. *
  67. * @v netdev Network device
  68. * @v iobuf I/O buffer
  69. * @ret rc Return status code
  70. */
  71. static int mlx_transmit ( struct net_device *netdev,
  72. struct io_buffer *iobuf ) {
  73. struct mlx_nic *mlx = netdev->priv;
  74. ud_send_wqe_t snd_wqe;
  75. int rc;
  76. snd_wqe = alloc_send_wqe ( mlx->ipoib_qph );
  77. if ( ! snd_wqe ) {
  78. DBGC ( mlx, "MLX %p out of TX WQEs\n", mlx );
  79. return -ENOBUFS;
  80. }
  81. prep_send_wqe_buf ( mlx->ipoib_qph, mlx->bcast_av, snd_wqe,
  82. iobuf->data, 0, iob_len ( iobuf ), 0 );
  83. if ( ( rc = post_send_req ( mlx->ipoib_qph, snd_wqe, 1 ) ) != 0 ) {
  84. DBGC ( mlx, "MLX %p could not post TX WQE %p: %s\n",
  85. mlx, snd_wqe, strerror ( rc ) );
  86. free_wqe ( snd_wqe );
  87. return rc;
  88. }
  89. return 0;
  90. }
  91. static int arbel_post_send ( struct ib_device *ibdev, struct io_buffer *iobuf,
  92. struct ib_address_vector *av,
  93. struct ib_queue_pair *qp );
  94. static struct io_buffer *tx_ring[NUM_IPOIB_SND_WQES];
  95. static int next_tx_idx = 0;
  96. static int mlx_transmit_direct ( struct net_device *netdev,
  97. struct io_buffer *iobuf ) {
  98. struct mlx_nic *mlx = netdev->priv;
  99. int rc;
  100. struct arbel arbel = {
  101. .uar = memfree_pci_dev.uar,
  102. .db_rec = dev_ib_data.uar_context_base,
  103. };
  104. struct arbel_send_work_queue arbel_send_queue = {
  105. .doorbell_idx = IPOIB_SND_QP_DB_IDX,
  106. .wqe_u = ( (struct udqp_st *) mlx->ipoib_qph )->snd_wq,
  107. };
  108. struct ib_device ibdev = {
  109. .priv = &arbel,
  110. };
  111. struct ib_queue_pair qp = {
  112. .qpn = ib_get_qpn ( mlx->ipoib_qph ),
  113. .send = {
  114. .num_wqes = NUM_IPOIB_SND_WQES,
  115. .next_idx = next_tx_idx,
  116. .iobufs = tx_ring,
  117. .priv = &arbel_send_queue,
  118. },
  119. };
  120. struct ud_av_st *bcast_av = mlx->bcast_av;
  121. struct address_vector_st *bav = &bcast_av->av;
  122. struct ib_address_vector av = {
  123. .dest_qp = bcast_av->dest_qp,
  124. .qkey = bcast_av->qkey,
  125. .dlid = MLX_EXTRACT ( bav, arbelprm_ud_address_vector_st, rlid ),
  126. .rate = ( MLX_EXTRACT ( bav, arbelprm_ud_address_vector_st, max_stat_rate ) ? 1 : 4 ),
  127. .sl = MLX_EXTRACT ( bav, arbelprm_ud_address_vector_st, sl ),
  128. .gid_present = 1,
  129. };
  130. memcpy ( &av.gid, ( ( void * ) bav ) + 16, 16 );
  131. rc = arbel_post_send ( &ibdev, iobuf, &av, &qp );
  132. next_tx_idx = qp.send.next_idx;
  133. return rc;
  134. }
  135. /**
  136. * Handle TX completion
  137. *
  138. * @v netdev Network device
  139. * @v ib_cqe Completion queue entry
  140. */
  141. static void mlx_tx_complete ( struct net_device *netdev,
  142. struct ib_cqe_st *ib_cqe ) {
  143. netdev_tx_complete_next_err ( netdev,
  144. ( ib_cqe->is_error ? -EIO : 0 ) );
  145. }
  146. /**
  147. * Handle RX completion
  148. *
  149. * @v netdev Network device
  150. * @v ib_cqe Completion queue entry
  151. */
  152. static void mlx_rx_complete ( struct net_device *netdev,
  153. struct ib_cqe_st *ib_cqe ) {
  154. unsigned int len;
  155. struct io_buffer *iobuf;
  156. void *buf;
  157. /* Check for errors */
  158. if ( ib_cqe->is_error ) {
  159. netdev_rx_err ( netdev, NULL, -EIO );
  160. return;
  161. }
  162. /* Allocate I/O buffer */
  163. len = ( ib_cqe->count - GRH_SIZE );
  164. iobuf = alloc_iob ( len );
  165. if ( ! iobuf ) {
  166. netdev_rx_err ( netdev, NULL, -ENOMEM );
  167. return;
  168. }
  169. /* Fill I/O buffer */
  170. buf = get_rcv_wqe_buf ( ib_cqe->wqe, 1 );
  171. memcpy ( iob_put ( iobuf, len ), buf, len );
  172. /* Hand off to network stack */
  173. netdev_rx ( netdev, iobuf );
  174. }
  175. /**
  176. * Poll completion queue
  177. *
  178. * @v netdev Network device
  179. * @v cq Completion queue
  180. * @v handler Completion handler
  181. */
  182. static void mlx_poll_cq ( struct net_device *netdev, cq_t cq,
  183. void ( * handler ) ( struct net_device *netdev,
  184. struct ib_cqe_st *ib_cqe ) ) {
  185. struct mlx_nic *mlx = netdev->priv;
  186. struct ib_cqe_st ib_cqe;
  187. uint8_t num_cqes;
  188. while ( 1 ) {
  189. /* Poll for single completion queue entry */
  190. ib_poll_cq ( cq, &ib_cqe, &num_cqes );
  191. /* Return if no entries in the queue */
  192. if ( ! num_cqes )
  193. return;
  194. DBGC ( mlx, "MLX %p cpl in %p: err %x send %x "
  195. "wqe %p count %lx\n", mlx, cq, ib_cqe.is_error,
  196. ib_cqe.is_send, ib_cqe.wqe, ib_cqe.count );
  197. /* Handle TX/RX completion */
  198. handler ( netdev, &ib_cqe );
  199. /* Free associated work queue entry */
  200. free_wqe ( ib_cqe.wqe );
  201. }
  202. }
  203. /**
  204. * Poll for completed and received packets
  205. *
  206. * @v netdev Network device
  207. */
  208. static void mlx_poll ( struct net_device *netdev ) {
  209. struct mlx_nic *mlx = netdev->priv;
  210. int rc;
  211. if ( ( rc = poll_error_buf() ) != 0 ) {
  212. DBG ( "poll_error_buf() failed: %s\n", strerror ( rc ) );
  213. return;
  214. }
  215. /* Drain event queue. We can ignore events, since we're going
  216. * to just poll all completion queues anyway.
  217. */
  218. if ( ( rc = drain_eq() ) != 0 ) {
  219. DBG ( "drain_eq() failed: %s\n", strerror ( rc ) );
  220. return;
  221. }
  222. /* Poll completion queues */
  223. mlx_poll_cq ( netdev, mlx->snd_cqh, mlx_tx_complete );
  224. mlx_poll_cq ( netdev, mlx->rcv_cqh, mlx_rx_complete );
  225. }
  226. /**
  227. * Enable or disable interrupts
  228. *
  229. * @v netdev Network device
  230. * @v enable Interrupts should be enabled
  231. */
  232. static void mlx_irq ( struct net_device *netdev, int enable ) {
  233. ( void ) netdev;
  234. ( void ) enable;
  235. }
  236. static struct net_device_operations mlx_operations = {
  237. .open = mlx_open,
  238. .close = mlx_close,
  239. #if 0
  240. .transmit = mlx_transmit,
  241. #else
  242. .transmit = mlx_transmit_direct,
  243. #endif
  244. .poll = mlx_poll,
  245. .irq = mlx_irq,
  246. };
  247. static struct ib_gid arbel_no_gid = {
  248. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 }
  249. };
  250. static void arbel_ring_doorbell ( struct arbel *arbel, void *db_reg,
  251. unsigned int offset ) {
  252. uint32_t *db_reg_dword = db_reg;
  253. DBG ( "arbel_ring_doorbell %08lx:%08lx to %lx\n",
  254. db_reg_dword[0], db_reg_dword[1],
  255. virt_to_phys ( arbel->uar + offset ) );
  256. barrier();
  257. writel ( db_reg_dword[0], ( arbel->uar + offset + 0 ) );
  258. barrier();
  259. writel ( db_reg_dword[1], ( arbel->uar + offset + 4 ) );
  260. }
  261. static int arbel_post_send ( struct ib_device *ibdev, struct io_buffer *iobuf,
  262. struct ib_address_vector *av,
  263. struct ib_queue_pair *qp ) {
  264. struct arbel *arbel = ibdev->priv;
  265. struct ib_work_queue *wq = &qp->send;
  266. struct arbel_send_work_queue *arbel_wq = wq->priv;
  267. unsigned int wqe_idx_mask = ( wq->num_wqes - 1 );
  268. struct ud_send_wqe_st *prev_wqe;
  269. struct ud_send_wqe_st *wqe;
  270. struct ib_gid *gid;
  271. size_t nds;
  272. union db_record_st *db_rec;
  273. struct send_doorbell_st db_reg;
  274. /* Allocate work queue entry */
  275. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  276. DBGC ( arbel, "ARBEL %p send queue full", arbel );
  277. return -ENOBUFS;
  278. }
  279. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  280. prev_wqe = &arbel_wq->wqe_u[(wq->next_idx - 1) & wqe_idx_mask].wqe_cont.wqe;
  281. wqe = &arbel_wq->wqe_u[wq->next_idx & wqe_idx_mask].wqe_cont.wqe;
  282. /* Construct work queue entry */
  283. MLX_POPULATE_1 ( &wqe->next.next, arbelprm_wqe_segment_next_st, 1,
  284. always1, 1 );
  285. memset ( &wqe->next.control, 0,
  286. sizeof ( wqe->next.control ) );
  287. MLX_POPULATE_1 ( &wqe->next.control,
  288. arbelprm_wqe_segment_ctrl_send_st, 0,
  289. always1, 1 );
  290. memset ( &wqe->udseg, 0, sizeof ( wqe->udseg ) );
  291. MLX_POPULATE_2 ( &wqe->udseg, arbelprm_ud_address_vector_st, 0,
  292. pd, GLOBAL_PD,
  293. port_number, PXE_IB_PORT );
  294. MLX_POPULATE_2 ( &wqe->udseg, arbelprm_ud_address_vector_st, 1,
  295. rlid, av->dlid,
  296. g, av->gid_present );
  297. MLX_POPULATE_2 ( &wqe->udseg, arbelprm_ud_address_vector_st, 2,
  298. max_stat_rate, ( ( av->rate >= 3 ) ? 0 : 1 ),
  299. msg, 3 );
  300. MLX_POPULATE_1 ( &wqe->udseg, arbelprm_ud_address_vector_st, 3,
  301. sl, av->sl );
  302. gid = ( av->gid_present ? &av->gid : &arbel_no_gid );
  303. memcpy ( ( ( ( void * ) &wqe->udseg ) + 16 ),
  304. gid, sizeof ( *gid ) );
  305. MLX_POPULATE_1 ( &wqe->udseg, arbelprm_wqe_segment_ud_st, 8,
  306. destination_qp, av->dest_qp );
  307. MLX_POPULATE_1 ( &wqe->udseg, arbelprm_wqe_segment_ud_st, 9,
  308. q_key, av->qkey );
  309. // wqe->mpointer[0].local_addr_l =
  310. // cpu_to_be32 ( virt_to_bus ( iobuf->data ) );
  311. memcpy ( bus_to_virt ( be32_to_cpu ( wqe->mpointer[0].local_addr_l ) ),
  312. iobuf->data, iob_len ( iobuf ) );
  313. wqe->mpointer[0].byte_count = cpu_to_be32 ( iob_len ( iobuf ) );
  314. DBG ( "Work queue entry:\n" );
  315. DBG_HD ( wqe, sizeof ( *wqe ) );
  316. /* Update previous work queue entry's "next" field */
  317. nds = ( ( offsetof ( typeof ( *wqe ), mpointer ) +
  318. sizeof ( wqe->mpointer[0] ) ) >> 4 );
  319. MLX_MODIFY ( &prev_wqe->next.next, arbelprm_wqe_segment_next_st, 0,
  320. nopcode, XDEV_NOPCODE_SEND );
  321. MLX_POPULATE_3 ( &prev_wqe->next.next, arbelprm_wqe_segment_next_st, 1,
  322. nds, nds,
  323. f, 1,
  324. always1, 1 );
  325. DBG ( "Previous work queue entry's next field:\n" );
  326. DBG_HD ( &prev_wqe->next.next, sizeof ( prev_wqe->next.next ) );
  327. /* Update doorbell record */
  328. db_rec = &arbel->db_rec[arbel_wq->doorbell_idx];
  329. MLX_POPULATE_1 ( db_rec, arbelprm_qp_db_record_st, 0,
  330. counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
  331. barrier();
  332. DBG ( "Doorbell record:\n" );
  333. DBG_HD ( db_rec, 8 );
  334. /* Ring doorbell register */
  335. MLX_POPULATE_4 ( &db_reg, arbelprm_send_doorbell_st, 0,
  336. nopcode, XDEV_NOPCODE_SEND,
  337. f, 1,
  338. wqe_counter, ( wq->next_idx & 0xffff ),
  339. wqe_cnt, 1 );
  340. MLX_POPULATE_2 ( &db_reg, arbelprm_send_doorbell_st, 1,
  341. nds, nds,
  342. qpn, qp->qpn );
  343. arbel_ring_doorbell ( arbel, &db_reg, POST_SND_OFFSET );
  344. /* Update work queue's index */
  345. wq->next_idx++;
  346. return 0;
  347. }
  348. static struct ib_device_operations arbel_ib_operations = {
  349. .post_send = arbel_post_send,
  350. };
  351. /**
  352. * Remove PCI device
  353. *
  354. * @v pci PCI device
  355. */
  356. static void arbel_remove ( struct pci_device *pci ) {
  357. struct net_device *netdev = pci_get_drvdata ( pci );
  358. unregister_netdev ( netdev );
  359. ib_driver_close ( 0 );
  360. netdev_nullify ( netdev );
  361. netdev_put ( netdev );
  362. }
  363. /**
  364. * Probe PCI device
  365. *
  366. * @v pci PCI device
  367. * @v id PCI ID
  368. * @ret rc Return status code
  369. */
  370. static int arbel_probe ( struct pci_device *pci,
  371. const struct pci_device_id *id __unused ) {
  372. struct net_device *netdev;
  373. struct mlx_nic *mlx;
  374. struct ib_mac *mac;
  375. udqp_t qph;
  376. int rc;
  377. /* Allocate net device */
  378. netdev = alloc_ibdev ( sizeof ( *mlx ) );
  379. if ( ! netdev )
  380. return -ENOMEM;
  381. netdev_init ( netdev, &mlx_operations );
  382. mlx = netdev->priv;
  383. pci_set_drvdata ( pci, netdev );
  384. netdev->dev = &pci->dev;
  385. memset ( mlx, 0, sizeof ( *mlx ) );
  386. /* Fix up PCI device */
  387. adjust_pci_device ( pci );
  388. /* Initialise hardware */
  389. if ( ( rc = ib_driver_init ( pci, &qph ) ) != 0 )
  390. goto err_ipoib_init;
  391. mlx->ipoib_qph = qph;
  392. mlx->bcast_av = ib_data.bcast_av;
  393. mlx->snd_cqh = ib_data.ipoib_snd_cq;
  394. mlx->rcv_cqh = ib_data.ipoib_rcv_cq;
  395. mac = ( ( struct ib_mac * ) netdev->ll_addr );
  396. mac->qpn = htonl ( ib_get_qpn ( mlx->ipoib_qph ) );
  397. memcpy ( &mac->gid, ib_data.port_gid.raw, sizeof ( mac->gid ) );
  398. /* Register network device */
  399. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  400. goto err_register_netdev;
  401. return 0;
  402. err_register_netdev:
  403. err_ipoib_init:
  404. ib_driver_close ( 0 );
  405. netdev_nullify ( netdev );
  406. netdev_put ( netdev );
  407. return rc;
  408. }
  409. static struct pci_device_id arbel_nics[] = {
  410. PCI_ROM ( 0x15b3, 0x6282, "MT25218", "MT25218 HCA driver" ),
  411. PCI_ROM ( 0x15b3, 0x6274, "MT25204", "MT25204 HCA driver" ),
  412. };
  413. struct pci_driver arbel_driver __pci_driver = {
  414. .ids = arbel_nics,
  415. .id_count = ( sizeof ( arbel_nics ) / sizeof ( arbel_nics[0] ) ),
  416. .probe = arbel_probe,
  417. .remove = arbel_remove,
  418. };