Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

igbvf_main.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*******************************************************************************
  2. Intel(R) 82576 Virtual Function Linux driver
  3. Copyright(c) 2009 Intel Corporation.
  4. Copyright(c) 2010 Eric Keller <ekeller@princeton.edu>
  5. Copyright(c) 2010 Red Hat Inc.
  6. Alex Williamson <alex.williamson@redhat.com>
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms and conditions of the GNU General Public License,
  9. version 2, as published by the Free Software Foundation.
  10. This program is distributed in the hope it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. You should have received a copy of the GNU General Public License along with
  15. this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. The full GNU General Public License is included in this distribution in
  18. the file called "COPYING".
  19. Contact Information:
  20. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  21. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  22. *******************************************************************************/
  23. FILE_LICENCE ( GPL2_ONLY );
  24. #include "igbvf.h"
  25. /**
  26. * igbvf_setup_tx_resources - allocate Tx resources (Descriptors)
  27. *
  28. * @v adapter e1000 private structure
  29. *
  30. * @ret rc Returns 0 on success, negative on failure
  31. **/
  32. int igbvf_setup_tx_resources ( struct igbvf_adapter *adapter )
  33. {
  34. DBG ( "igbvf_setup_tx_resources\n" );
  35. /* Allocate transmit descriptor ring memory.
  36. It must not cross a 64K boundary because of hardware errata #23
  37. so we use malloc_dma() requesting a 128 byte block that is
  38. 128 byte aligned. This should guarantee that the memory
  39. allocated will not cross a 64K boundary, because 128 is an
  40. even multiple of 65536 ( 65536 / 128 == 512 ), so all possible
  41. allocations of 128 bytes on a 128 byte boundary will not
  42. cross 64K bytes.
  43. */
  44. adapter->tx_base =
  45. malloc_dma ( adapter->tx_ring_size, adapter->tx_ring_size );
  46. if ( ! adapter->tx_base ) {
  47. return -ENOMEM;
  48. }
  49. memset ( adapter->tx_base, 0, adapter->tx_ring_size );
  50. DBG ( "adapter->tx_base = %#08lx\n", virt_to_bus ( adapter->tx_base ) );
  51. return 0;
  52. }
  53. /**
  54. * igbvf_free_tx_resources - Free Tx Resources per Queue
  55. * @adapter: board private structure
  56. *
  57. * Free all transmit software resources
  58. **/
  59. void igbvf_free_tx_resources ( struct igbvf_adapter *adapter )
  60. {
  61. DBG ( "igbvf_free_tx_resources\n" );
  62. free_dma ( adapter->tx_base, adapter->tx_ring_size );
  63. }
  64. /**
  65. * igbvf_free_rx_resources - Free Rx Resources
  66. * @adapter: board private structure
  67. *
  68. * Free all receive software resources
  69. **/
  70. void igbvf_free_rx_resources ( struct igbvf_adapter *adapter )
  71. {
  72. int i;
  73. DBG ( "igbvf_free_rx_resources\n" );
  74. free_dma ( adapter->rx_base, adapter->rx_ring_size );
  75. for ( i = 0; i < NUM_RX_DESC; i++ ) {
  76. free_iob ( adapter->rx_iobuf[i] );
  77. }
  78. }
  79. /**
  80. * igbvf_refill_rx_ring - allocate Rx io_buffers
  81. *
  82. * @v adapter e1000 private structure
  83. *
  84. * @ret rc Returns 0 on success, negative on failure
  85. **/
  86. static int igbvf_refill_rx_ring ( struct igbvf_adapter *adapter )
  87. {
  88. int i, rx_curr;
  89. int rc = 0;
  90. union e1000_adv_rx_desc *rx_curr_desc;
  91. struct e1000_hw *hw = &adapter->hw;
  92. struct io_buffer *iob;
  93. DBGP ("igbvf_refill_rx_ring\n");
  94. for ( i = 0; i < NUM_RX_DESC; i++ ) {
  95. rx_curr = ( ( adapter->rx_curr + i ) % NUM_RX_DESC );
  96. rx_curr_desc = adapter->rx_base + rx_curr;
  97. if ( rx_curr_desc->wb.upper.status_error & E1000_RXD_STAT_DD )
  98. continue;
  99. if ( adapter->rx_iobuf[rx_curr] != NULL )
  100. continue;
  101. DBG2 ( "Refilling rx desc %d\n", rx_curr );
  102. iob = alloc_iob ( MAXIMUM_ETHERNET_VLAN_SIZE );
  103. adapter->rx_iobuf[rx_curr] = iob;
  104. rx_curr_desc->wb.upper.status_error = 0;
  105. if ( ! iob ) {
  106. DBG ( "alloc_iob failed\n" );
  107. rc = -ENOMEM;
  108. break;
  109. } else {
  110. rx_curr_desc->read.pkt_addr = virt_to_bus ( iob->data );
  111. rx_curr_desc->read.hdr_addr = 0;
  112. ew32 ( RDT(0), rx_curr );
  113. }
  114. }
  115. return rc;
  116. }
  117. /**
  118. * igbvf_irq_disable - Mask off interrupt generation on the NIC
  119. * @adapter: board private structure
  120. **/
  121. static void igbvf_irq_disable ( struct igbvf_adapter *adapter )
  122. {
  123. struct e1000_hw *hw = &adapter->hw;
  124. ew32 ( EIMC, ~0 );
  125. }
  126. /**
  127. * igbvf_irq_enable - Enable default interrupt generation settings
  128. * @adapter: board private structure
  129. **/
  130. static void igbvf_irq_enable ( struct igbvf_adapter *adapter )
  131. {
  132. struct e1000_hw *hw = &adapter->hw;
  133. ew32 ( EIAC, IMS_ENABLE_MASK );
  134. ew32 ( EIAM, IMS_ENABLE_MASK );
  135. ew32 ( EIMS, IMS_ENABLE_MASK );
  136. }
  137. /**
  138. * igbvf_irq - enable or Disable interrupts
  139. *
  140. * @v adapter e1000 adapter
  141. * @v action requested interrupt action
  142. **/
  143. static void igbvf_irq ( struct net_device *netdev, int enable )
  144. {
  145. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  146. DBG ( "igbvf_irq\n" );
  147. if ( enable ) {
  148. igbvf_irq_enable ( adapter );
  149. } else {
  150. igbvf_irq_disable ( adapter );
  151. }
  152. }
  153. /**
  154. * igbvf_process_tx_packets - process transmitted packets
  155. *
  156. * @v netdev network interface device structure
  157. **/
  158. static void igbvf_process_tx_packets ( struct net_device *netdev )
  159. {
  160. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  161. uint32_t i;
  162. uint32_t tx_status;
  163. union e1000_adv_tx_desc *tx_curr_desc;
  164. /* Check status of transmitted packets
  165. */
  166. DBGP ( "process_tx_packets: tx_head = %d, tx_tail = %d\n", adapter->tx_head,
  167. adapter->tx_tail );
  168. while ( ( i = adapter->tx_head ) != adapter->tx_tail ) {
  169. tx_curr_desc = ( void * ) ( adapter->tx_base ) +
  170. ( i * sizeof ( *adapter->tx_base ) );
  171. tx_status = tx_curr_desc->wb.status;
  172. DBG ( " tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
  173. DBG ( " tx_status = %#08x\n", tx_status );
  174. /* if the packet at tx_head is not owned by hardware it is for us */
  175. if ( ! ( tx_status & E1000_TXD_STAT_DD ) )
  176. break;
  177. DBG ( "Sent packet. tx_head: %d tx_tail: %d tx_status: %#08x\n",
  178. adapter->tx_head, adapter->tx_tail, tx_status );
  179. netdev_tx_complete ( netdev, adapter->tx_iobuf[i] );
  180. DBG ( "Success transmitting packet, tx_status: %#08x\n",
  181. tx_status );
  182. /* Decrement count of used descriptors, clear this descriptor
  183. */
  184. adapter->tx_fill_ctr--;
  185. memset ( tx_curr_desc, 0, sizeof ( *tx_curr_desc ) );
  186. adapter->tx_head = ( adapter->tx_head + 1 ) % NUM_TX_DESC;
  187. }
  188. }
  189. /**
  190. * igbvf_process_rx_packets - process received packets
  191. *
  192. * @v netdev network interface device structure
  193. **/
  194. static void igbvf_process_rx_packets ( struct net_device *netdev )
  195. {
  196. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  197. struct e1000_hw *hw = &adapter->hw;
  198. uint32_t i;
  199. uint32_t rx_status;
  200. uint32_t rx_len;
  201. uint32_t rx_err;
  202. union e1000_adv_rx_desc *rx_curr_desc;
  203. DBGP ( "igbvf_process_rx_packets\n" );
  204. /* Process received packets
  205. */
  206. while ( 1 ) {
  207. i = adapter->rx_curr;
  208. rx_curr_desc = ( void * ) ( adapter->rx_base ) +
  209. ( i * sizeof ( *adapter->rx_base ) );
  210. rx_status = rx_curr_desc->wb.upper.status_error;
  211. DBG2 ( "Before DD Check RX_status: %#08x, rx_curr: %d\n",
  212. rx_status, i );
  213. if ( ! ( rx_status & E1000_RXD_STAT_DD ) )
  214. break;
  215. if ( adapter->rx_iobuf[i] == NULL )
  216. break;
  217. DBG ( "E1000_RCTL = %#08x\n", er32 (RCTL) );
  218. rx_len = rx_curr_desc->wb.upper.length;
  219. DBG ( "Received packet, rx_curr: %d rx_status: %#08x rx_len: %d\n",
  220. i, rx_status, rx_len );
  221. rx_err = rx_status;
  222. iob_put ( adapter->rx_iobuf[i], rx_len );
  223. if ( rx_err & E1000_RXDEXT_ERR_FRAME_ERR_MASK ) {
  224. netdev_rx_err ( netdev, adapter->rx_iobuf[i], -EINVAL );
  225. DBG ( "igbvf_process_rx_packets: Corrupted packet received!"
  226. " rx_err: %#08x\n", rx_err );
  227. } else {
  228. /* Add this packet to the receive queue. */
  229. netdev_rx ( netdev, adapter->rx_iobuf[i] );
  230. }
  231. adapter->rx_iobuf[i] = NULL;
  232. memset ( rx_curr_desc, 0, sizeof ( *rx_curr_desc ) );
  233. adapter->rx_curr = ( adapter->rx_curr + 1 ) % NUM_RX_DESC;
  234. }
  235. }
  236. /**
  237. * igbvf_poll - Poll for received packets
  238. *
  239. * @v netdev Network device
  240. */
  241. static void igbvf_poll ( struct net_device *netdev )
  242. {
  243. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  244. uint32_t rx_status;
  245. union e1000_adv_rx_desc *rx_curr_desc;
  246. DBGP ( "igbvf_poll\n" );
  247. rx_curr_desc = ( void * ) ( adapter->rx_base ) +
  248. ( adapter->rx_curr * sizeof ( *adapter->rx_base ) );
  249. rx_status = rx_curr_desc->wb.upper.status_error;
  250. if ( ! ( rx_status & E1000_RXD_STAT_DD ) )
  251. return;
  252. igbvf_process_tx_packets ( netdev );
  253. igbvf_process_rx_packets ( netdev );
  254. igbvf_refill_rx_ring ( adapter );
  255. }
  256. /**
  257. * igbvf_config_collision_dist_generic - Configure collision distance
  258. * @hw: pointer to the HW structure
  259. *
  260. * Configures the collision distance to the default value and is used
  261. * during link setup. Currently no func pointer exists and all
  262. * implementations are handled in the generic version of this function.
  263. **/
  264. void igbvf_config_collision_dist ( struct e1000_hw *hw )
  265. {
  266. u32 tctl;
  267. DBG ("igbvf_config_collision_dist");
  268. tctl = er32 (TCTL);
  269. tctl &= ~E1000_TCTL_COLD;
  270. tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
  271. ew32 (TCTL, tctl);
  272. e1e_flush();
  273. }
  274. /**
  275. * igbvf_configure_tx - Configure Transmit Unit after Reset
  276. * @adapter: board private structure
  277. *
  278. * Configure the Tx unit of the MAC after a reset.
  279. **/
  280. static void igbvf_configure_tx ( struct igbvf_adapter *adapter )
  281. {
  282. struct e1000_hw *hw = &adapter->hw;
  283. u32 tctl, txdctl;
  284. DBG ( "igbvf_configure_tx\n" );
  285. /* disable transmits while setting up the descriptors */
  286. tctl = er32 ( TCTL );
  287. ew32 ( TCTL, tctl & ~E1000_TCTL_EN );
  288. e1e_flush();
  289. mdelay (10);
  290. ew32 ( TDBAH(0), 0 );
  291. ew32 ( TDBAL(0), virt_to_bus ( adapter->tx_base ) );
  292. ew32 ( TDLEN(0), adapter->tx_ring_size );
  293. DBG ( "E1000_TDBAL(0): %#08x\n", er32 ( TDBAL(0) ) );
  294. DBG ( "E1000_TDLEN(0): %d\n", er32 ( TDLEN(0) ) );
  295. /* Setup the HW Tx Head and Tail descriptor pointers */
  296. ew32 ( TDH(0), 0 );
  297. ew32 ( TDT(0), 0 );
  298. adapter->tx_head = 0;
  299. adapter->tx_tail = 0;
  300. adapter->tx_fill_ctr = 0;
  301. txdctl = er32(TXDCTL(0));
  302. txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
  303. ew32 ( TXDCTL(0), txdctl );
  304. txdctl = er32 ( TXDCTL(0) );
  305. txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
  306. ew32 ( TXDCTL(0), txdctl );
  307. /* Setup Transmit Descriptor Settings for eop descriptor */
  308. adapter->txd_cmd = E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_IFCS;
  309. /* Advanced descriptor */
  310. adapter->txd_cmd |= E1000_ADVTXD_DCMD_DEXT;
  311. /* (not part of cmd, but in same 32 bit word...) */
  312. adapter->txd_cmd |= E1000_ADVTXD_DTYP_DATA;
  313. /* enable Report Status bit */
  314. adapter->txd_cmd |= E1000_ADVTXD_DCMD_RS;
  315. /* Program the Transmit Control Register */
  316. tctl &= ~E1000_TCTL_CT;
  317. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  318. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  319. igbvf_config_collision_dist ( hw );
  320. /* Enable transmits */
  321. tctl |= E1000_TCTL_EN;
  322. ew32(TCTL, tctl);
  323. e1e_flush();
  324. }
  325. /* igbvf_reset - bring the hardware into a known good state
  326. *
  327. * This function boots the hardware and enables some settings that
  328. * require a configuration cycle of the hardware - those cannot be
  329. * set/changed during runtime. After reset the device needs to be
  330. * properly configured for Rx, Tx etc.
  331. */
  332. void igbvf_reset ( struct igbvf_adapter *adapter )
  333. {
  334. struct e1000_mac_info *mac = &adapter->hw.mac;
  335. struct net_device *netdev = adapter->netdev;
  336. struct e1000_hw *hw = &adapter->hw;
  337. /* Allow time for pending master requests to run */
  338. if ( mac->ops.reset_hw(hw) )
  339. DBG ("PF still resetting\n");
  340. mac->ops.init_hw ( hw );
  341. if ( is_valid_ether_addr(adapter->hw.mac.addr) ) {
  342. memcpy ( netdev->hw_addr, adapter->hw.mac.addr, ETH_ALEN );
  343. }
  344. }
  345. extern void igbvf_init_function_pointers_vf(struct e1000_hw *hw);
  346. /**
  347. * igbvf_sw_init - Initialize general software structures (struct igbvf_adapter)
  348. * @adapter: board private structure to initialize
  349. *
  350. * igbvf_sw_init initializes the Adapter private data structure.
  351. * Fields are initialized based on PCI device information and
  352. * OS network device settings (MTU size).
  353. **/
  354. static int __devinit igbvf_sw_init ( struct igbvf_adapter *adapter )
  355. {
  356. struct e1000_hw *hw = &adapter->hw;
  357. struct pci_device *pdev = adapter->pdev;
  358. int rc;
  359. /* PCI config space info */
  360. hw->vendor_id = pdev->vendor;
  361. hw->device_id = pdev->device;
  362. pci_read_config_byte ( pdev, PCI_REVISION, &hw->revision_id );
  363. pci_read_config_word ( pdev, PCI_COMMAND, &hw->bus.pci_cmd_word );
  364. adapter->max_frame_size = MAXIMUM_ETHERNET_VLAN_SIZE + ETH_HLEN + ETH_FCS_LEN;
  365. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  366. /* Set various function pointers */
  367. igbvf_init_function_pointers_vf ( &adapter->hw );
  368. rc = adapter->hw.mac.ops.init_params ( &adapter->hw );
  369. if (rc) {
  370. DBG ("hw.mac.ops.init_params(&adapter->hw) Failure\n");
  371. return rc;
  372. }
  373. rc = adapter->hw.mbx.ops.init_params ( &adapter->hw );
  374. if (rc) {
  375. DBG ("hw.mbx.ops.init_params(&adapter->hw) Failure\n");
  376. return rc;
  377. }
  378. /* Explicitly disable IRQ since the NIC can be in any state. */
  379. igbvf_irq_disable ( adapter );
  380. return 0;
  381. }
  382. /**
  383. * igbvf_setup_srrctl - configure the receive control registers
  384. * @adapter: Board private structure
  385. **/
  386. static void igbvf_setup_srrctl ( struct igbvf_adapter *adapter )
  387. {
  388. struct e1000_hw *hw = &adapter->hw;
  389. u32 srrctl = 0;
  390. DBG ( "igbvf_setup_srrctl\n" );
  391. srrctl &= ~(E1000_SRRCTL_DESCTYPE_MASK |
  392. E1000_SRRCTL_BSIZEHDR_MASK |
  393. E1000_SRRCTL_BSIZEPKT_MASK);
  394. /* Enable queue drop to avoid head of line blocking */
  395. srrctl |= E1000_SRRCTL_DROP_EN;
  396. /* Setup buffer sizes */
  397. srrctl |= 2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
  398. srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
  399. ew32 ( SRRCTL(0), srrctl );
  400. }
  401. /**
  402. * igbvf_configure_rx - Configure 8254x Receive Unit after Reset
  403. * @adapter: board private structure
  404. *
  405. * Configure the Rx unit of the MAC after a reset.
  406. **/
  407. static void igbvf_configure_rx ( struct igbvf_adapter *adapter )
  408. {
  409. struct e1000_hw *hw = &adapter->hw;
  410. u32 rxdctl;
  411. DBG ( "igbvf_configure_rx\n" );
  412. /* disable receives */
  413. rxdctl = er32 ( RXDCTL(0) );
  414. ew32 ( RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE );
  415. msleep ( 10 );
  416. /*
  417. * Setup the HW Rx Head and Tail Descriptor Pointers and
  418. * the Base and Length of the Rx Descriptor Ring
  419. */
  420. ew32 ( RDBAL(0), virt_to_bus (adapter->rx_base) );
  421. ew32 ( RDBAH(0), 0 );
  422. ew32 ( RDLEN(0), adapter->rx_ring_size );
  423. adapter->rx_curr = 0;
  424. ew32 ( RDH(0), 0 );
  425. ew32 ( RDT(0), 0 );
  426. rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
  427. rxdctl &= 0xFFF00000;
  428. rxdctl |= IGBVF_RX_PTHRESH;
  429. rxdctl |= IGBVF_RX_HTHRESH << 8;
  430. rxdctl |= IGBVF_RX_WTHRESH << 16;
  431. igbvf_rlpml_set_vf ( hw, adapter->max_frame_size );
  432. /* enable receives */
  433. ew32 ( RXDCTL(0), rxdctl );
  434. ew32 ( RDT(0), NUM_RX_DESC );
  435. }
  436. /**
  437. * igbvf_setup_rx_resources - allocate Rx resources (Descriptors)
  438. *
  439. * @v adapter e1000 private structure
  440. **/
  441. int igbvf_setup_rx_resources ( struct igbvf_adapter *adapter )
  442. {
  443. int i;
  444. union e1000_adv_rx_desc *rx_curr_desc;
  445. struct io_buffer *iob;
  446. DBG ( "igbvf_setup_rx_resources\n" );
  447. /* Allocate receive descriptor ring memory.
  448. It must not cross a 64K boundary because of hardware errata
  449. */
  450. adapter->rx_base =
  451. malloc_dma ( adapter->rx_ring_size, adapter->rx_ring_size );
  452. if ( ! adapter->rx_base ) {
  453. return -ENOMEM;
  454. }
  455. memset ( adapter->rx_base, 0, adapter->rx_ring_size );
  456. for ( i = 0; i < NUM_RX_DESC; i++ ) {
  457. rx_curr_desc = adapter->rx_base + i;
  458. iob = alloc_iob ( MAXIMUM_ETHERNET_VLAN_SIZE );
  459. adapter->rx_iobuf[i] = iob;
  460. rx_curr_desc->wb.upper.status_error = 0;
  461. if ( ! iob ) {
  462. DBG ( "alloc_iob failed\n" );
  463. return -ENOMEM;
  464. } else {
  465. rx_curr_desc->read.pkt_addr = virt_to_bus ( iob->data );
  466. rx_curr_desc->read.hdr_addr = 0;
  467. }
  468. }
  469. return 0;
  470. }
  471. /**
  472. * igbvf_open - Called when a network interface is made active
  473. * @netdev: network interface device structure
  474. *
  475. * Returns 0 on success, negative value on failure
  476. *
  477. * The open entry point is called when a network interface is made
  478. * active by the system (IFF_UP). At this point all resources needed
  479. * for transmit and receive operations are allocated, the interrupt
  480. * handler is registered with the OS, the watchdog timer is started,
  481. * and the stack is notified that the interface is ready.
  482. **/
  483. static int igbvf_open ( struct net_device *netdev )
  484. {
  485. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  486. int err;
  487. DBG ("igbvf_open\n");
  488. /* Update MAC address */
  489. memcpy ( adapter->hw.mac.addr, netdev->ll_addr, ETH_ALEN );
  490. igbvf_reset( adapter );
  491. /* allocate transmit descriptors */
  492. err = igbvf_setup_tx_resources ( adapter );
  493. if (err) {
  494. DBG ( "Error setting up TX resources!\n" );
  495. goto err_setup_tx;
  496. }
  497. igbvf_configure_tx ( adapter );
  498. igbvf_setup_srrctl( adapter );
  499. err = igbvf_setup_rx_resources( adapter );
  500. if (err) {
  501. DBG ( "Error setting up RX resources!\n" );
  502. goto err_setup_rx;
  503. }
  504. igbvf_configure_rx ( adapter );
  505. return 0;
  506. err_setup_rx:
  507. DBG ( "err_setup_rx\n" );
  508. igbvf_free_tx_resources ( adapter );
  509. return err;
  510. err_setup_tx:
  511. DBG ( "err_setup_tx\n" );
  512. igbvf_reset ( adapter );
  513. return err;
  514. }
  515. /**
  516. * igbvf_close - Disables a network interface
  517. * @netdev: network interface device structure
  518. *
  519. * Returns 0, this is not allowed to fail
  520. *
  521. * The close entry point is called when an interface is de-activated
  522. * by the OS. The hardware is still under the drivers control, but
  523. * needs to be disabled. A global MAC reset is issued to stop the
  524. * hardware, and all transmit and receive resources are freed.
  525. **/
  526. static void igbvf_close ( struct net_device *netdev )
  527. {
  528. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  529. struct e1000_hw *hw = &adapter->hw;
  530. uint32_t rxdctl;
  531. DBG ( "igbvf_close\n" );
  532. /* Disable and acknowledge interrupts */
  533. igbvf_irq_disable ( adapter );
  534. er32(EICR);
  535. /* disable receives */
  536. rxdctl = er32 ( RXDCTL(0) );
  537. ew32 ( RXDCTL(0), rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE );
  538. mdelay ( 10 );
  539. igbvf_reset ( adapter );
  540. igbvf_free_tx_resources( adapter );
  541. igbvf_free_rx_resources( adapter );
  542. }
  543. /**
  544. * igbvf_transmit - Transmit a packet
  545. *
  546. * @v netdev Network device
  547. * @v iobuf I/O buffer
  548. *
  549. * @ret rc Returns 0 on success, negative on failure
  550. */
  551. static int igbvf_transmit ( struct net_device *netdev, struct io_buffer *iobuf )
  552. {
  553. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  554. struct e1000_hw *hw = &adapter->hw;
  555. uint32_t tx_curr = adapter->tx_tail;
  556. union e1000_adv_tx_desc *tx_curr_desc;
  557. DBGP ("igbvf_transmit\n");
  558. if ( adapter->tx_fill_ctr == NUM_TX_DESC ) {
  559. DBG ("TX overflow\n");
  560. return -ENOBUFS;
  561. }
  562. /* Save pointer to iobuf we have been given to transmit,
  563. netdev_tx_complete() will need it later
  564. */
  565. adapter->tx_iobuf[tx_curr] = iobuf;
  566. tx_curr_desc = ( void * ) ( adapter->tx_base ) +
  567. ( tx_curr * sizeof ( *adapter->tx_base ) );
  568. DBG ( "tx_curr_desc = %#08lx\n", virt_to_bus ( tx_curr_desc ) );
  569. DBG ( "tx_curr_desc + 16 = %#08lx\n", virt_to_bus ( tx_curr_desc ) + 16 );
  570. DBG ( "iobuf->data = %#08lx\n", virt_to_bus ( iobuf->data ) );
  571. /* Add the packet to TX ring
  572. */
  573. tx_curr_desc->read.buffer_addr = virt_to_bus ( iobuf->data );
  574. tx_curr_desc->read.cmd_type_len = adapter->txd_cmd |(iob_len ( iobuf )) ;
  575. // minus hdr_len ????
  576. tx_curr_desc->read.olinfo_status = ((iob_len ( iobuf )) << E1000_ADVTXD_PAYLEN_SHIFT);
  577. DBG ( "TX fill: %d tx_curr: %d addr: %#08lx len: %zd\n", adapter->tx_fill_ctr,
  578. tx_curr, virt_to_bus ( iobuf->data ), iob_len ( iobuf ) );
  579. /* Point to next free descriptor */
  580. adapter->tx_tail = ( adapter->tx_tail + 1 ) % NUM_TX_DESC;
  581. adapter->tx_fill_ctr++;
  582. /* Write new tail to NIC, making packet available for transmit
  583. */
  584. ew32 ( TDT(0), adapter->tx_tail );
  585. e1e_flush ();
  586. return 0;
  587. }
  588. /** igbvf net device operations */
  589. static struct net_device_operations igbvf_operations = {
  590. .open = igbvf_open,
  591. .close = igbvf_close,
  592. .transmit = igbvf_transmit,
  593. .poll = igbvf_poll,
  594. .irq = igbvf_irq,
  595. };
  596. /**
  597. * igbvf_get_hw_control - get control of the h/w from f/w
  598. * @adapter: address of board private structure
  599. *
  600. * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
  601. * For ASF and Pass Through versions of f/w this means that
  602. * the driver is loaded.
  603. *
  604. **/
  605. void igbvf_get_hw_control ( struct igbvf_adapter *adapter )
  606. {
  607. struct e1000_hw *hw = &adapter->hw;
  608. u32 ctrl_ext;
  609. /* Let firmware know the driver has taken over */
  610. ctrl_ext = er32 ( CTRL_EXT );
  611. ew32 ( CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD );
  612. }
  613. /**
  614. * igbvf_probe - Device Initialization Routine
  615. * @pdev: PCI device information struct
  616. * @ent: entry in igbvf_pci_tbl
  617. *
  618. * Returns 0 on success, negative on failure
  619. *
  620. * igbvf_probe initializes an adapter identified by a pci_dev structure.
  621. * The OS initialization, configuring of the adapter private structure,
  622. * and a hardware reset occur.
  623. **/
  624. int igbvf_probe ( struct pci_device *pdev )
  625. {
  626. int err;
  627. struct net_device *netdev;
  628. struct igbvf_adapter *adapter;
  629. unsigned long mmio_start, mmio_len;
  630. struct e1000_hw *hw;
  631. DBG ( "igbvf_probe\n" );
  632. err = -ENOMEM;
  633. /* Allocate net device ( also allocates memory for netdev->priv
  634. and makes netdev-priv point to it ) */
  635. netdev = alloc_etherdev ( sizeof ( struct igbvf_adapter ) );
  636. if ( ! netdev )
  637. goto err_alloc_etherdev;
  638. /* Associate igbvf-specific network operations operations with
  639. * generic network device layer */
  640. netdev_init ( netdev, &igbvf_operations );
  641. /* Associate this network device with given PCI device */
  642. pci_set_drvdata ( pdev, netdev );
  643. netdev->dev = &pdev->dev;
  644. /* Initialize driver private storage */
  645. adapter = netdev_priv ( netdev );
  646. memset ( adapter, 0, ( sizeof ( *adapter ) ) );
  647. adapter->pdev = pdev;
  648. adapter->ioaddr = pdev->ioaddr;
  649. adapter->hw.io_base = pdev->ioaddr;
  650. hw = &adapter->hw;
  651. hw->vendor_id = pdev->vendor;
  652. hw->device_id = pdev->device;
  653. adapter->irqno = pdev->irq;
  654. adapter->netdev = netdev;
  655. adapter->hw.back = adapter;
  656. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  657. adapter->max_hw_frame_size = ETH_FRAME_LEN + ETH_FCS_LEN;
  658. adapter->tx_ring_size = sizeof ( *adapter->tx_base ) * NUM_TX_DESC;
  659. adapter->rx_ring_size = sizeof ( *adapter->rx_base ) * NUM_RX_DESC;
  660. /* Fix up PCI device */
  661. adjust_pci_device ( pdev );
  662. err = -EIO;
  663. mmio_start = pci_bar_start ( pdev, PCI_BASE_ADDRESS_0 );
  664. mmio_len = pci_bar_size ( pdev, PCI_BASE_ADDRESS_0 );
  665. DBG ( "mmio_start: %#08lx\n", mmio_start );
  666. DBG ( "mmio_len: %#08lx\n", mmio_len );
  667. adapter->hw.hw_addr = ioremap ( mmio_start, mmio_len );
  668. DBG ( "adapter->hw.hw_addr: %p\n", adapter->hw.hw_addr );
  669. if ( ! adapter->hw.hw_addr ) {
  670. DBG ( "err_ioremap\n" );
  671. goto err_ioremap;
  672. }
  673. /* setup adapter struct */
  674. err = igbvf_sw_init ( adapter );
  675. if (err) {
  676. DBG ( "err_sw_init\n" );
  677. goto err_sw_init;
  678. }
  679. /* reset the controller to put the device in a known good state */
  680. err = hw->mac.ops.reset_hw ( hw );
  681. if ( err ) {
  682. DBG ("PF still in reset state, assigning new address\n");
  683. netdev->hw_addr[0] = 0x21;
  684. netdev->hw_addr[1] = 0x21;
  685. netdev->hw_addr[2] = 0x21;
  686. netdev->hw_addr[3] = 0x21;
  687. netdev->hw_addr[4] = 0x21;
  688. netdev->hw_addr[5] = 0x21;
  689. netdev->hw_addr[6] = 0x21;
  690. } else {
  691. err = hw->mac.ops.read_mac_addr(hw);
  692. if (err) {
  693. DBG ("Error reading MAC address\n");
  694. goto err_hw_init;
  695. }
  696. if ( ! is_valid_ether_addr(adapter->hw.mac.addr) ) {
  697. /* Assign random MAC address */
  698. eth_random_addr(adapter->hw.mac.addr);
  699. }
  700. }
  701. memcpy ( netdev->hw_addr, adapter->hw.mac.addr, ETH_ALEN );
  702. /* reset the hardware with the new settings */
  703. igbvf_reset ( adapter );
  704. /* let the f/w know that the h/w is now under the control of the
  705. * driver. */
  706. igbvf_get_hw_control ( adapter );
  707. /* Mark as link up; we don't yet handle link state */
  708. netdev_link_up ( netdev );
  709. if ( ( err = register_netdev ( netdev ) ) != 0) {
  710. DBG ( "err_register\n" );
  711. goto err_register;
  712. }
  713. DBG ("igbvf_probe_succeeded\n");
  714. return 0;
  715. err_register:
  716. err_hw_init:
  717. err_sw_init:
  718. iounmap ( adapter->hw.hw_addr );
  719. err_ioremap:
  720. netdev_put ( netdev );
  721. err_alloc_etherdev:
  722. return err;
  723. }
  724. /**
  725. * igbvf_remove - Device Removal Routine
  726. * @pdev: PCI device information struct
  727. *
  728. * igbvf_remove is called by the PCI subsystem to alert the driver
  729. * that it should release a PCI device. The could be caused by a
  730. * Hot-Plug event, or because the driver is going to be removed from
  731. * memory.
  732. **/
  733. void igbvf_remove ( struct pci_device *pdev )
  734. {
  735. struct net_device *netdev = pci_get_drvdata ( pdev );
  736. struct igbvf_adapter *adapter = netdev_priv ( netdev );
  737. DBG ( "igbvf_remove\n" );
  738. if ( adapter->hw.flash_address )
  739. iounmap ( adapter->hw.flash_address );
  740. if ( adapter->hw.hw_addr )
  741. iounmap ( adapter->hw.hw_addr );
  742. unregister_netdev ( netdev );
  743. igbvf_reset ( adapter );
  744. netdev_nullify ( netdev );
  745. netdev_put ( netdev );
  746. }
  747. static struct pci_device_id igbvf_pci_tbl[] = {
  748. PCI_ROM(0x8086, 0x10CA, "igbvf", "E1000_DEV_ID_82576_VF", 0),
  749. PCI_ROM(0x8086, 0x1520, "i350vf", "E1000_DEV_ID_I350_VF", 0),
  750. };
  751. struct pci_driver igbvf_driver __pci_driver = {
  752. .ids = igbvf_pci_tbl,
  753. .id_count = (sizeof(igbvf_pci_tbl) / sizeof(igbvf_pci_tbl[0])),
  754. .probe = igbvf_probe,
  755. .remove = igbvf_remove,
  756. };