ixgbe_common.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2008 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include <linux/pci.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include "ixgbe_common.h"
  24. #include "ixgbe_phy.h"
  25. static s32 ixgbe_poll_eeprom_eerd_done(struct ixgbe_hw *hw);
  26. static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw);
  27. static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw);
  28. static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw);
  29. static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw);
  30. static void ixgbe_standby_eeprom(struct ixgbe_hw *hw);
  31. static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
  32. u16 count);
  33. static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count);
  34. static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
  35. static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
  36. static void ixgbe_release_eeprom(struct ixgbe_hw *hw);
  37. static u16 ixgbe_calc_eeprom_checksum(struct ixgbe_hw *hw);
  38. static void ixgbe_enable_rar(struct ixgbe_hw *hw, u32 index);
  39. static void ixgbe_disable_rar(struct ixgbe_hw *hw, u32 index);
  40. static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr);
  41. static void ixgbe_add_mc_addr(struct ixgbe_hw *hw, u8 *mc_addr);
  42. static void ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq);
  43. /**
  44. * ixgbe_start_hw_generic - Prepare hardware for Tx/Rx
  45. * @hw: pointer to hardware structure
  46. *
  47. * Starts the hardware by filling the bus info structure and media type, clears
  48. * all on chip counters, initializes receive address registers, multicast
  49. * table, VLAN filter table, calls routine to set up link and flow control
  50. * settings, and leaves transmit and receive units disabled and uninitialized
  51. **/
  52. s32 ixgbe_start_hw_generic(struct ixgbe_hw *hw)
  53. {
  54. u32 ctrl_ext;
  55. /* Set the media type */
  56. hw->phy.media_type = hw->mac.ops.get_media_type(hw);
  57. /* Identify the PHY */
  58. hw->phy.ops.identify(hw);
  59. /*
  60. * Store MAC address from RAR0, clear receive address registers, and
  61. * clear the multicast table
  62. */
  63. hw->mac.ops.init_rx_addrs(hw);
  64. /* Clear the VLAN filter table */
  65. hw->mac.ops.clear_vfta(hw);
  66. /* Set up link */
  67. hw->mac.ops.setup_link(hw);
  68. /* Clear statistics registers */
  69. hw->mac.ops.clear_hw_cntrs(hw);
  70. /* Set No Snoop Disable */
  71. ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
  72. ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
  73. IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
  74. IXGBE_WRITE_FLUSH(hw);
  75. /* Clear adapter stopped flag */
  76. hw->adapter_stopped = false;
  77. return 0;
  78. }
  79. /**
  80. * ixgbe_init_hw_generic - Generic hardware initialization
  81. * @hw: pointer to hardware structure
  82. *
  83. * Initialize the hardware by resetting the hardware, filling the bus info
  84. * structure and media type, clears all on chip counters, initializes receive
  85. * address registers, multicast table, VLAN filter table, calls routine to set
  86. * up link and flow control settings, and leaves transmit and receive units
  87. * disabled and uninitialized
  88. **/
  89. s32 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
  90. {
  91. /* Reset the hardware */
  92. hw->mac.ops.reset_hw(hw);
  93. /* Start the HW */
  94. hw->mac.ops.start_hw(hw);
  95. return 0;
  96. }
  97. /**
  98. * ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
  99. * @hw: pointer to hardware structure
  100. *
  101. * Clears all hardware statistics counters by reading them from the hardware
  102. * Statistics counters are clear on read.
  103. **/
  104. s32 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
  105. {
  106. u16 i = 0;
  107. IXGBE_READ_REG(hw, IXGBE_CRCERRS);
  108. IXGBE_READ_REG(hw, IXGBE_ILLERRC);
  109. IXGBE_READ_REG(hw, IXGBE_ERRBC);
  110. IXGBE_READ_REG(hw, IXGBE_MSPDC);
  111. for (i = 0; i < 8; i++)
  112. IXGBE_READ_REG(hw, IXGBE_MPC(i));
  113. IXGBE_READ_REG(hw, IXGBE_MLFC);
  114. IXGBE_READ_REG(hw, IXGBE_MRFC);
  115. IXGBE_READ_REG(hw, IXGBE_RLEC);
  116. IXGBE_READ_REG(hw, IXGBE_LXONTXC);
  117. IXGBE_READ_REG(hw, IXGBE_LXONRXC);
  118. IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
  119. IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
  120. for (i = 0; i < 8; i++) {
  121. IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
  122. IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
  123. IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
  124. IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
  125. }
  126. IXGBE_READ_REG(hw, IXGBE_PRC64);
  127. IXGBE_READ_REG(hw, IXGBE_PRC127);
  128. IXGBE_READ_REG(hw, IXGBE_PRC255);
  129. IXGBE_READ_REG(hw, IXGBE_PRC511);
  130. IXGBE_READ_REG(hw, IXGBE_PRC1023);
  131. IXGBE_READ_REG(hw, IXGBE_PRC1522);
  132. IXGBE_READ_REG(hw, IXGBE_GPRC);
  133. IXGBE_READ_REG(hw, IXGBE_BPRC);
  134. IXGBE_READ_REG(hw, IXGBE_MPRC);
  135. IXGBE_READ_REG(hw, IXGBE_GPTC);
  136. IXGBE_READ_REG(hw, IXGBE_GORCL);
  137. IXGBE_READ_REG(hw, IXGBE_GORCH);
  138. IXGBE_READ_REG(hw, IXGBE_GOTCL);
  139. IXGBE_READ_REG(hw, IXGBE_GOTCH);
  140. for (i = 0; i < 8; i++)
  141. IXGBE_READ_REG(hw, IXGBE_RNBC(i));
  142. IXGBE_READ_REG(hw, IXGBE_RUC);
  143. IXGBE_READ_REG(hw, IXGBE_RFC);
  144. IXGBE_READ_REG(hw, IXGBE_ROC);
  145. IXGBE_READ_REG(hw, IXGBE_RJC);
  146. IXGBE_READ_REG(hw, IXGBE_MNGPRC);
  147. IXGBE_READ_REG(hw, IXGBE_MNGPDC);
  148. IXGBE_READ_REG(hw, IXGBE_MNGPTC);
  149. IXGBE_READ_REG(hw, IXGBE_TORL);
  150. IXGBE_READ_REG(hw, IXGBE_TORH);
  151. IXGBE_READ_REG(hw, IXGBE_TPR);
  152. IXGBE_READ_REG(hw, IXGBE_TPT);
  153. IXGBE_READ_REG(hw, IXGBE_PTC64);
  154. IXGBE_READ_REG(hw, IXGBE_PTC127);
  155. IXGBE_READ_REG(hw, IXGBE_PTC255);
  156. IXGBE_READ_REG(hw, IXGBE_PTC511);
  157. IXGBE_READ_REG(hw, IXGBE_PTC1023);
  158. IXGBE_READ_REG(hw, IXGBE_PTC1522);
  159. IXGBE_READ_REG(hw, IXGBE_MPTC);
  160. IXGBE_READ_REG(hw, IXGBE_BPTC);
  161. for (i = 0; i < 16; i++) {
  162. IXGBE_READ_REG(hw, IXGBE_QPRC(i));
  163. IXGBE_READ_REG(hw, IXGBE_QBRC(i));
  164. IXGBE_READ_REG(hw, IXGBE_QPTC(i));
  165. IXGBE_READ_REG(hw, IXGBE_QBTC(i));
  166. }
  167. return 0;
  168. }
  169. /**
  170. * ixgbe_read_pba_num_generic - Reads part number from EEPROM
  171. * @hw: pointer to hardware structure
  172. * @pba_num: stores the part number from the EEPROM
  173. *
  174. * Reads the part number from the EEPROM.
  175. **/
  176. s32 ixgbe_read_pba_num_generic(struct ixgbe_hw *hw, u32 *pba_num)
  177. {
  178. s32 ret_val;
  179. u16 data;
  180. ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
  181. if (ret_val) {
  182. hw_dbg(hw, "NVM Read Error\n");
  183. return ret_val;
  184. }
  185. *pba_num = (u32)(data << 16);
  186. ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &data);
  187. if (ret_val) {
  188. hw_dbg(hw, "NVM Read Error\n");
  189. return ret_val;
  190. }
  191. *pba_num |= data;
  192. return 0;
  193. }
  194. /**
  195. * ixgbe_get_mac_addr_generic - Generic get MAC address
  196. * @hw: pointer to hardware structure
  197. * @mac_addr: Adapter MAC address
  198. *
  199. * Reads the adapter's MAC address from first Receive Address Register (RAR0)
  200. * A reset of the adapter must be performed prior to calling this function
  201. * in order for the MAC address to have been loaded from the EEPROM into RAR0
  202. **/
  203. s32 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
  204. {
  205. u32 rar_high;
  206. u32 rar_low;
  207. u16 i;
  208. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
  209. rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
  210. for (i = 0; i < 4; i++)
  211. mac_addr[i] = (u8)(rar_low >> (i*8));
  212. for (i = 0; i < 2; i++)
  213. mac_addr[i+4] = (u8)(rar_high >> (i*8));
  214. return 0;
  215. }
  216. /**
  217. * ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
  218. * @hw: pointer to hardware structure
  219. *
  220. * Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
  221. * disables transmit and receive units. The adapter_stopped flag is used by
  222. * the shared code and drivers to determine if the adapter is in a stopped
  223. * state and should not touch the hardware.
  224. **/
  225. s32 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
  226. {
  227. u32 number_of_queues;
  228. u32 reg_val;
  229. u16 i;
  230. /*
  231. * Set the adapter_stopped flag so other driver functions stop touching
  232. * the hardware
  233. */
  234. hw->adapter_stopped = true;
  235. /* Disable the receive unit */
  236. reg_val = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
  237. reg_val &= ~(IXGBE_RXCTRL_RXEN);
  238. IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, reg_val);
  239. IXGBE_WRITE_FLUSH(hw);
  240. msleep(2);
  241. /* Clear interrupt mask to stop from interrupts being generated */
  242. IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
  243. /* Clear any pending interrupts */
  244. IXGBE_READ_REG(hw, IXGBE_EICR);
  245. /* Disable the transmit unit. Each queue must be disabled. */
  246. number_of_queues = hw->mac.max_tx_queues;
  247. for (i = 0; i < number_of_queues; i++) {
  248. reg_val = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
  249. if (reg_val & IXGBE_TXDCTL_ENABLE) {
  250. reg_val &= ~IXGBE_TXDCTL_ENABLE;
  251. IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), reg_val);
  252. }
  253. }
  254. /*
  255. * Prevent the PCI-E bus from from hanging by disabling PCI-E master
  256. * access and verify no pending requests
  257. */
  258. if (ixgbe_disable_pcie_master(hw) != 0)
  259. hw_dbg(hw, "PCI-E Master disable polling has failed.\n");
  260. return 0;
  261. }
  262. /**
  263. * ixgbe_led_on_generic - Turns on the software controllable LEDs.
  264. * @hw: pointer to hardware structure
  265. * @index: led number to turn on
  266. **/
  267. s32 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
  268. {
  269. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  270. /* To turn on the LED, set mode to ON. */
  271. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  272. led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
  273. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  274. IXGBE_WRITE_FLUSH(hw);
  275. return 0;
  276. }
  277. /**
  278. * ixgbe_led_off_generic - Turns off the software controllable LEDs.
  279. * @hw: pointer to hardware structure
  280. * @index: led number to turn off
  281. **/
  282. s32 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
  283. {
  284. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  285. /* To turn off the LED, set mode to OFF. */
  286. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  287. led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
  288. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  289. IXGBE_WRITE_FLUSH(hw);
  290. return 0;
  291. }
  292. /**
  293. * ixgbe_init_eeprom_params_generic - Initialize EEPROM params
  294. * @hw: pointer to hardware structure
  295. *
  296. * Initializes the EEPROM parameters ixgbe_eeprom_info within the
  297. * ixgbe_hw struct in order to set up EEPROM access.
  298. **/
  299. s32 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
  300. {
  301. struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
  302. u32 eec;
  303. u16 eeprom_size;
  304. if (eeprom->type == ixgbe_eeprom_uninitialized) {
  305. eeprom->type = ixgbe_eeprom_none;
  306. /* Set default semaphore delay to 10ms which is a well
  307. * tested value */
  308. eeprom->semaphore_delay = 10;
  309. /*
  310. * Check for EEPROM present first.
  311. * If not present leave as none
  312. */
  313. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  314. if (eec & IXGBE_EEC_PRES) {
  315. eeprom->type = ixgbe_eeprom_spi;
  316. /*
  317. * SPI EEPROM is assumed here. This code would need to
  318. * change if a future EEPROM is not SPI.
  319. */
  320. eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
  321. IXGBE_EEC_SIZE_SHIFT);
  322. eeprom->word_size = 1 << (eeprom_size +
  323. IXGBE_EEPROM_WORD_SIZE_SHIFT);
  324. }
  325. if (eec & IXGBE_EEC_ADDR_SIZE)
  326. eeprom->address_bits = 16;
  327. else
  328. eeprom->address_bits = 8;
  329. hw_dbg(hw, "Eeprom params: type = %d, size = %d, address bits: "
  330. "%d\n", eeprom->type, eeprom->word_size,
  331. eeprom->address_bits);
  332. }
  333. return 0;
  334. }
  335. /**
  336. * ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
  337. * @hw: pointer to hardware structure
  338. * @offset: offset within the EEPROM to be read
  339. * @data: read 16 bit value from EEPROM
  340. *
  341. * Reads 16 bit value from EEPROM through bit-bang method
  342. **/
  343. s32 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
  344. u16 *data)
  345. {
  346. s32 status;
  347. u16 word_in;
  348. u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
  349. hw->eeprom.ops.init_params(hw);
  350. if (offset >= hw->eeprom.word_size) {
  351. status = IXGBE_ERR_EEPROM;
  352. goto out;
  353. }
  354. /* Prepare the EEPROM for reading */
  355. status = ixgbe_acquire_eeprom(hw);
  356. if (status == 0) {
  357. if (ixgbe_ready_eeprom(hw) != 0) {
  358. ixgbe_release_eeprom(hw);
  359. status = IXGBE_ERR_EEPROM;
  360. }
  361. }
  362. if (status == 0) {
  363. ixgbe_standby_eeprom(hw);
  364. /*
  365. * Some SPI eeproms use the 8th address bit embedded in the
  366. * opcode
  367. */
  368. if ((hw->eeprom.address_bits == 8) && (offset >= 128))
  369. read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
  370. /* Send the READ command (opcode + addr) */
  371. ixgbe_shift_out_eeprom_bits(hw, read_opcode,
  372. IXGBE_EEPROM_OPCODE_BITS);
  373. ixgbe_shift_out_eeprom_bits(hw, (u16)(offset*2),
  374. hw->eeprom.address_bits);
  375. /* Read the data. */
  376. word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
  377. *data = (word_in >> 8) | (word_in << 8);
  378. /* End this read operation */
  379. ixgbe_release_eeprom(hw);
  380. }
  381. out:
  382. return status;
  383. }
  384. /**
  385. * ixgbe_read_eeprom_generic - Read EEPROM word using EERD
  386. * @hw: pointer to hardware structure
  387. * @offset: offset of word in the EEPROM to read
  388. * @data: word read from the EEPROM
  389. *
  390. * Reads a 16 bit word from the EEPROM using the EERD register.
  391. **/
  392. s32 ixgbe_read_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
  393. {
  394. u32 eerd;
  395. s32 status;
  396. hw->eeprom.ops.init_params(hw);
  397. if (offset >= hw->eeprom.word_size) {
  398. status = IXGBE_ERR_EEPROM;
  399. goto out;
  400. }
  401. eerd = (offset << IXGBE_EEPROM_READ_ADDR_SHIFT) +
  402. IXGBE_EEPROM_READ_REG_START;
  403. IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
  404. status = ixgbe_poll_eeprom_eerd_done(hw);
  405. if (status == 0)
  406. *data = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
  407. IXGBE_EEPROM_READ_REG_DATA);
  408. else
  409. hw_dbg(hw, "Eeprom read timed out\n");
  410. out:
  411. return status;
  412. }
  413. /**
  414. * ixgbe_poll_eeprom_eerd_done - Poll EERD status
  415. * @hw: pointer to hardware structure
  416. *
  417. * Polls the status bit (bit 1) of the EERD to determine when the read is done.
  418. **/
  419. static s32 ixgbe_poll_eeprom_eerd_done(struct ixgbe_hw *hw)
  420. {
  421. u32 i;
  422. u32 reg;
  423. s32 status = IXGBE_ERR_EEPROM;
  424. for (i = 0; i < IXGBE_EERD_ATTEMPTS; i++) {
  425. reg = IXGBE_READ_REG(hw, IXGBE_EERD);
  426. if (reg & IXGBE_EEPROM_READ_REG_DONE) {
  427. status = 0;
  428. break;
  429. }
  430. udelay(5);
  431. }
  432. return status;
  433. }
  434. /**
  435. * ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
  436. * @hw: pointer to hardware structure
  437. *
  438. * Prepares EEPROM for access using bit-bang method. This function should
  439. * be called before issuing a command to the EEPROM.
  440. **/
  441. static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
  442. {
  443. s32 status = 0;
  444. u32 eec;
  445. u32 i;
  446. if (ixgbe_acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) != 0)
  447. status = IXGBE_ERR_SWFW_SYNC;
  448. if (status == 0) {
  449. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  450. /* Request EEPROM Access */
  451. eec |= IXGBE_EEC_REQ;
  452. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  453. for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
  454. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  455. if (eec & IXGBE_EEC_GNT)
  456. break;
  457. udelay(5);
  458. }
  459. /* Release if grant not acquired */
  460. if (!(eec & IXGBE_EEC_GNT)) {
  461. eec &= ~IXGBE_EEC_REQ;
  462. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  463. hw_dbg(hw, "Could not acquire EEPROM grant\n");
  464. ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
  465. status = IXGBE_ERR_EEPROM;
  466. }
  467. }
  468. /* Setup EEPROM for Read/Write */
  469. if (status == 0) {
  470. /* Clear CS and SK */
  471. eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
  472. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  473. IXGBE_WRITE_FLUSH(hw);
  474. udelay(1);
  475. }
  476. return status;
  477. }
  478. /**
  479. * ixgbe_get_eeprom_semaphore - Get hardware semaphore
  480. * @hw: pointer to hardware structure
  481. *
  482. * Sets the hardware semaphores so EEPROM access can occur for bit-bang method
  483. **/
  484. static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
  485. {
  486. s32 status = IXGBE_ERR_EEPROM;
  487. u32 timeout;
  488. u32 i;
  489. u32 swsm;
  490. /* Set timeout value based on size of EEPROM */
  491. timeout = hw->eeprom.word_size + 1;
  492. /* Get SMBI software semaphore between device drivers first */
  493. for (i = 0; i < timeout; i++) {
  494. /*
  495. * If the SMBI bit is 0 when we read it, then the bit will be
  496. * set and we have the semaphore
  497. */
  498. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  499. if (!(swsm & IXGBE_SWSM_SMBI)) {
  500. status = 0;
  501. break;
  502. }
  503. msleep(1);
  504. }
  505. /* Now get the semaphore between SW/FW through the SWESMBI bit */
  506. if (status == 0) {
  507. for (i = 0; i < timeout; i++) {
  508. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  509. /* Set the SW EEPROM semaphore bit to request access */
  510. swsm |= IXGBE_SWSM_SWESMBI;
  511. IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
  512. /*
  513. * If we set the bit successfully then we got the
  514. * semaphore.
  515. */
  516. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  517. if (swsm & IXGBE_SWSM_SWESMBI)
  518. break;
  519. udelay(50);
  520. }
  521. /*
  522. * Release semaphores and return error if SW EEPROM semaphore
  523. * was not granted because we don't have access to the EEPROM
  524. */
  525. if (i >= timeout) {
  526. hw_dbg(hw, "Driver can't access the Eeprom - Semaphore "
  527. "not granted.\n");
  528. ixgbe_release_eeprom_semaphore(hw);
  529. status = IXGBE_ERR_EEPROM;
  530. }
  531. }
  532. return status;
  533. }
  534. /**
  535. * ixgbe_release_eeprom_semaphore - Release hardware semaphore
  536. * @hw: pointer to hardware structure
  537. *
  538. * This function clears hardware semaphore bits.
  539. **/
  540. static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
  541. {
  542. u32 swsm;
  543. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  544. /* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
  545. swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
  546. IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
  547. IXGBE_WRITE_FLUSH(hw);
  548. }
  549. /**
  550. * ixgbe_ready_eeprom - Polls for EEPROM ready
  551. * @hw: pointer to hardware structure
  552. **/
  553. static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
  554. {
  555. s32 status = 0;
  556. u16 i;
  557. u8 spi_stat_reg;
  558. /*
  559. * Read "Status Register" repeatedly until the LSB is cleared. The
  560. * EEPROM will signal that the command has been completed by clearing
  561. * bit 0 of the internal status register. If it's not cleared within
  562. * 5 milliseconds, then error out.
  563. */
  564. for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
  565. ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
  566. IXGBE_EEPROM_OPCODE_BITS);
  567. spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
  568. if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
  569. break;
  570. udelay(5);
  571. ixgbe_standby_eeprom(hw);
  572. };
  573. /*
  574. * On some parts, SPI write time could vary from 0-20mSec on 3.3V
  575. * devices (and only 0-5mSec on 5V devices)
  576. */
  577. if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
  578. hw_dbg(hw, "SPI EEPROM Status error\n");
  579. status = IXGBE_ERR_EEPROM;
  580. }
  581. return status;
  582. }
  583. /**
  584. * ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
  585. * @hw: pointer to hardware structure
  586. **/
  587. static void ixgbe_standby_eeprom(struct ixgbe_hw *hw)
  588. {
  589. u32 eec;
  590. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  591. /* Toggle CS to flush commands */
  592. eec |= IXGBE_EEC_CS;
  593. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  594. IXGBE_WRITE_FLUSH(hw);
  595. udelay(1);
  596. eec &= ~IXGBE_EEC_CS;
  597. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  598. IXGBE_WRITE_FLUSH(hw);
  599. udelay(1);
  600. }
  601. /**
  602. * ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
  603. * @hw: pointer to hardware structure
  604. * @data: data to send to the EEPROM
  605. * @count: number of bits to shift out
  606. **/
  607. static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
  608. u16 count)
  609. {
  610. u32 eec;
  611. u32 mask;
  612. u32 i;
  613. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  614. /*
  615. * Mask is used to shift "count" bits of "data" out to the EEPROM
  616. * one bit at a time. Determine the starting bit based on count
  617. */
  618. mask = 0x01 << (count - 1);
  619. for (i = 0; i < count; i++) {
  620. /*
  621. * A "1" is shifted out to the EEPROM by setting bit "DI" to a
  622. * "1", and then raising and then lowering the clock (the SK
  623. * bit controls the clock input to the EEPROM). A "0" is
  624. * shifted out to the EEPROM by setting "DI" to "0" and then
  625. * raising and then lowering the clock.
  626. */
  627. if (data & mask)
  628. eec |= IXGBE_EEC_DI;
  629. else
  630. eec &= ~IXGBE_EEC_DI;
  631. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  632. IXGBE_WRITE_FLUSH(hw);
  633. udelay(1);
  634. ixgbe_raise_eeprom_clk(hw, &eec);
  635. ixgbe_lower_eeprom_clk(hw, &eec);
  636. /*
  637. * Shift mask to signify next bit of data to shift in to the
  638. * EEPROM
  639. */
  640. mask = mask >> 1;
  641. };
  642. /* We leave the "DI" bit set to "0" when we leave this routine. */
  643. eec &= ~IXGBE_EEC_DI;
  644. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  645. IXGBE_WRITE_FLUSH(hw);
  646. }
  647. /**
  648. * ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
  649. * @hw: pointer to hardware structure
  650. **/
  651. static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
  652. {
  653. u32 eec;
  654. u32 i;
  655. u16 data = 0;
  656. /*
  657. * In order to read a register from the EEPROM, we need to shift
  658. * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
  659. * the clock input to the EEPROM (setting the SK bit), and then reading
  660. * the value of the "DO" bit. During this "shifting in" process the
  661. * "DI" bit should always be clear.
  662. */
  663. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  664. eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
  665. for (i = 0; i < count; i++) {
  666. data = data << 1;
  667. ixgbe_raise_eeprom_clk(hw, &eec);
  668. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  669. eec &= ~(IXGBE_EEC_DI);
  670. if (eec & IXGBE_EEC_DO)
  671. data |= 1;
  672. ixgbe_lower_eeprom_clk(hw, &eec);
  673. }
  674. return data;
  675. }
  676. /**
  677. * ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
  678. * @hw: pointer to hardware structure
  679. * @eec: EEC register's current value
  680. **/
  681. static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
  682. {
  683. /*
  684. * Raise the clock input to the EEPROM
  685. * (setting the SK bit), then delay
  686. */
  687. *eec = *eec | IXGBE_EEC_SK;
  688. IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
  689. IXGBE_WRITE_FLUSH(hw);
  690. udelay(1);
  691. }
  692. /**
  693. * ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
  694. * @hw: pointer to hardware structure
  695. * @eecd: EECD's current value
  696. **/
  697. static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
  698. {
  699. /*
  700. * Lower the clock input to the EEPROM (clearing the SK bit), then
  701. * delay
  702. */
  703. *eec = *eec & ~IXGBE_EEC_SK;
  704. IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
  705. IXGBE_WRITE_FLUSH(hw);
  706. udelay(1);
  707. }
  708. /**
  709. * ixgbe_release_eeprom - Release EEPROM, release semaphores
  710. * @hw: pointer to hardware structure
  711. **/
  712. static void ixgbe_release_eeprom(struct ixgbe_hw *hw)
  713. {
  714. u32 eec;
  715. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  716. eec |= IXGBE_EEC_CS; /* Pull CS high */
  717. eec &= ~IXGBE_EEC_SK; /* Lower SCK */
  718. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  719. IXGBE_WRITE_FLUSH(hw);
  720. udelay(1);
  721. /* Stop requesting EEPROM access */
  722. eec &= ~IXGBE_EEC_REQ;
  723. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  724. ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
  725. }
  726. /**
  727. * ixgbe_calc_eeprom_checksum - Calculates and returns the checksum
  728. * @hw: pointer to hardware structure
  729. **/
  730. static u16 ixgbe_calc_eeprom_checksum(struct ixgbe_hw *hw)
  731. {
  732. u16 i;
  733. u16 j;
  734. u16 checksum = 0;
  735. u16 length = 0;
  736. u16 pointer = 0;
  737. u16 word = 0;
  738. /* Include 0x0-0x3F in the checksum */
  739. for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
  740. if (hw->eeprom.ops.read(hw, i, &word) != 0) {
  741. hw_dbg(hw, "EEPROM read failed\n");
  742. break;
  743. }
  744. checksum += word;
  745. }
  746. /* Include all data from pointers except for the fw pointer */
  747. for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
  748. hw->eeprom.ops.read(hw, i, &pointer);
  749. /* Make sure the pointer seems valid */
  750. if (pointer != 0xFFFF && pointer != 0) {
  751. hw->eeprom.ops.read(hw, pointer, &length);
  752. if (length != 0xFFFF && length != 0) {
  753. for (j = pointer+1; j <= pointer+length; j++) {
  754. hw->eeprom.ops.read(hw, j, &word);
  755. checksum += word;
  756. }
  757. }
  758. }
  759. }
  760. checksum = (u16)IXGBE_EEPROM_SUM - checksum;
  761. return checksum;
  762. }
  763. /**
  764. * ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
  765. * @hw: pointer to hardware structure
  766. * @checksum_val: calculated checksum
  767. *
  768. * Performs checksum calculation and validates the EEPROM checksum. If the
  769. * caller does not need checksum_val, the value can be NULL.
  770. **/
  771. s32 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
  772. u16 *checksum_val)
  773. {
  774. s32 status;
  775. u16 checksum;
  776. u16 read_checksum = 0;
  777. /*
  778. * Read the first word from the EEPROM. If this times out or fails, do
  779. * not continue or we could be in for a very long wait while every
  780. * EEPROM read fails
  781. */
  782. status = hw->eeprom.ops.read(hw, 0, &checksum);
  783. if (status == 0) {
  784. checksum = ixgbe_calc_eeprom_checksum(hw);
  785. hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
  786. /*
  787. * Verify read checksum from EEPROM is the same as
  788. * calculated checksum
  789. */
  790. if (read_checksum != checksum)
  791. status = IXGBE_ERR_EEPROM_CHECKSUM;
  792. /* If the user cares, return the calculated checksum */
  793. if (checksum_val)
  794. *checksum_val = checksum;
  795. } else {
  796. hw_dbg(hw, "EEPROM read failed\n");
  797. }
  798. return status;
  799. }
  800. /**
  801. * ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
  802. * @hw: pointer to hardware structure
  803. **/
  804. s32 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
  805. {
  806. s32 status;
  807. u16 checksum;
  808. /*
  809. * Read the first word from the EEPROM. If this times out or fails, do
  810. * not continue or we could be in for a very long wait while every
  811. * EEPROM read fails
  812. */
  813. status = hw->eeprom.ops.read(hw, 0, &checksum);
  814. if (status == 0) {
  815. checksum = ixgbe_calc_eeprom_checksum(hw);
  816. status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM,
  817. checksum);
  818. } else {
  819. hw_dbg(hw, "EEPROM read failed\n");
  820. }
  821. return status;
  822. }
  823. /**
  824. * ixgbe_validate_mac_addr - Validate MAC address
  825. * @mac_addr: pointer to MAC address.
  826. *
  827. * Tests a MAC address to ensure it is a valid Individual Address
  828. **/
  829. s32 ixgbe_validate_mac_addr(u8 *mac_addr)
  830. {
  831. s32 status = 0;
  832. /* Make sure it is not a multicast address */
  833. if (IXGBE_IS_MULTICAST(mac_addr))
  834. status = IXGBE_ERR_INVALID_MAC_ADDR;
  835. /* Not a broadcast address */
  836. else if (IXGBE_IS_BROADCAST(mac_addr))
  837. status = IXGBE_ERR_INVALID_MAC_ADDR;
  838. /* Reject the zero address */
  839. else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
  840. mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0)
  841. status = IXGBE_ERR_INVALID_MAC_ADDR;
  842. return status;
  843. }
  844. /**
  845. * ixgbe_set_rar_generic - Set Rx address register
  846. * @hw: pointer to hardware structure
  847. * @index: Receive address register to write
  848. * @addr: Address to put into receive address register
  849. * @vmdq: VMDq "set" or "pool" index
  850. * @enable_addr: set flag that address is active
  851. *
  852. * Puts an ethernet address into a receive address register.
  853. **/
  854. s32 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
  855. u32 enable_addr)
  856. {
  857. u32 rar_low, rar_high;
  858. u32 rar_entries = hw->mac.num_rar_entries;
  859. /* setup VMDq pool selection before this RAR gets enabled */
  860. hw->mac.ops.set_vmdq(hw, index, vmdq);
  861. /* Make sure we are using a valid rar index range */
  862. if (index < rar_entries) {
  863. /*
  864. * HW expects these in little endian so we reverse the byte
  865. * order from network order (big endian) to little endian
  866. */
  867. rar_low = ((u32)addr[0] |
  868. ((u32)addr[1] << 8) |
  869. ((u32)addr[2] << 16) |
  870. ((u32)addr[3] << 24));
  871. /*
  872. * Some parts put the VMDq setting in the extra RAH bits,
  873. * so save everything except the lower 16 bits that hold part
  874. * of the address and the address valid bit.
  875. */
  876. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  877. rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
  878. rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
  879. if (enable_addr != 0)
  880. rar_high |= IXGBE_RAH_AV;
  881. IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
  882. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  883. } else {
  884. hw_dbg(hw, "RAR index %d is out of range.\n", index);
  885. }
  886. return 0;
  887. }
  888. /**
  889. * ixgbe_clear_rar_generic - Remove Rx address register
  890. * @hw: pointer to hardware structure
  891. * @index: Receive address register to write
  892. *
  893. * Clears an ethernet address from a receive address register.
  894. **/
  895. s32 ixgbe_clear_rar_generic(struct ixgbe_hw *hw, u32 index)
  896. {
  897. u32 rar_high;
  898. u32 rar_entries = hw->mac.num_rar_entries;
  899. /* Make sure we are using a valid rar index range */
  900. if (index < rar_entries) {
  901. /*
  902. * Some parts put the VMDq setting in the extra RAH bits,
  903. * so save everything except the lower 16 bits that hold part
  904. * of the address and the address valid bit.
  905. */
  906. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  907. rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
  908. IXGBE_WRITE_REG(hw, IXGBE_RAL(index), 0);
  909. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  910. } else {
  911. hw_dbg(hw, "RAR index %d is out of range.\n", index);
  912. }
  913. /* clear VMDq pool/queue selection for this RAR */
  914. hw->mac.ops.clear_vmdq(hw, index, IXGBE_CLEAR_VMDQ_ALL);
  915. return 0;
  916. }
  917. /**
  918. * ixgbe_enable_rar - Enable Rx address register
  919. * @hw: pointer to hardware structure
  920. * @index: index into the RAR table
  921. *
  922. * Enables the select receive address register.
  923. **/
  924. static void ixgbe_enable_rar(struct ixgbe_hw *hw, u32 index)
  925. {
  926. u32 rar_high;
  927. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  928. rar_high |= IXGBE_RAH_AV;
  929. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  930. }
  931. /**
  932. * ixgbe_disable_rar - Disable Rx address register
  933. * @hw: pointer to hardware structure
  934. * @index: index into the RAR table
  935. *
  936. * Disables the select receive address register.
  937. **/
  938. static void ixgbe_disable_rar(struct ixgbe_hw *hw, u32 index)
  939. {
  940. u32 rar_high;
  941. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  942. rar_high &= (~IXGBE_RAH_AV);
  943. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  944. }
  945. /**
  946. * ixgbe_init_rx_addrs_generic - Initializes receive address filters.
  947. * @hw: pointer to hardware structure
  948. *
  949. * Places the MAC address in receive address register 0 and clears the rest
  950. * of the receive address registers. Clears the multicast table. Assumes
  951. * the receiver is in reset when the routine is called.
  952. **/
  953. s32 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
  954. {
  955. u32 i;
  956. u32 rar_entries = hw->mac.num_rar_entries;
  957. /*
  958. * If the current mac address is valid, assume it is a software override
  959. * to the permanent address.
  960. * Otherwise, use the permanent address from the eeprom.
  961. */
  962. if (ixgbe_validate_mac_addr(hw->mac.addr) ==
  963. IXGBE_ERR_INVALID_MAC_ADDR) {
  964. /* Get the MAC address from the RAR0 for later reference */
  965. hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
  966. hw_dbg(hw, " Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
  967. hw->mac.addr[0], hw->mac.addr[1],
  968. hw->mac.addr[2]);
  969. hw_dbg(hw, "%.2X %.2X %.2X\n", hw->mac.addr[3],
  970. hw->mac.addr[4], hw->mac.addr[5]);
  971. } else {
  972. /* Setup the receive address. */
  973. hw_dbg(hw, "Overriding MAC Address in RAR[0]\n");
  974. hw_dbg(hw, " New MAC Addr =%.2X %.2X %.2X ",
  975. hw->mac.addr[0], hw->mac.addr[1],
  976. hw->mac.addr[2]);
  977. hw_dbg(hw, "%.2X %.2X %.2X\n", hw->mac.addr[3],
  978. hw->mac.addr[4], hw->mac.addr[5]);
  979. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
  980. }
  981. hw->addr_ctrl.overflow_promisc = 0;
  982. hw->addr_ctrl.rar_used_count = 1;
  983. /* Zero out the other receive addresses. */
  984. hw_dbg(hw, "Clearing RAR[1-%d]\n", rar_entries - 1);
  985. for (i = 1; i < rar_entries; i++) {
  986. IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
  987. IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
  988. }
  989. /* Clear the MTA */
  990. hw->addr_ctrl.mc_addr_in_rar_count = 0;
  991. hw->addr_ctrl.mta_in_use = 0;
  992. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
  993. hw_dbg(hw, " Clearing MTA\n");
  994. for (i = 0; i < hw->mac.mcft_size; i++)
  995. IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
  996. if (hw->mac.ops.init_uta_tables)
  997. hw->mac.ops.init_uta_tables(hw);
  998. return 0;
  999. }
  1000. /**
  1001. * ixgbe_add_uc_addr - Adds a secondary unicast address.
  1002. * @hw: pointer to hardware structure
  1003. * @addr: new address
  1004. *
  1005. * Adds it to unused receive address register or goes into promiscuous mode.
  1006. **/
  1007. static void ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq)
  1008. {
  1009. u32 rar_entries = hw->mac.num_rar_entries;
  1010. u32 rar;
  1011. hw_dbg(hw, " UC Addr = %.2X %.2X %.2X %.2X %.2X %.2X\n",
  1012. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  1013. /*
  1014. * Place this address in the RAR if there is room,
  1015. * else put the controller into promiscuous mode
  1016. */
  1017. if (hw->addr_ctrl.rar_used_count < rar_entries) {
  1018. rar = hw->addr_ctrl.rar_used_count -
  1019. hw->addr_ctrl.mc_addr_in_rar_count;
  1020. hw->mac.ops.set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
  1021. hw_dbg(hw, "Added a secondary address to RAR[%d]\n", rar);
  1022. hw->addr_ctrl.rar_used_count++;
  1023. } else {
  1024. hw->addr_ctrl.overflow_promisc++;
  1025. }
  1026. hw_dbg(hw, "ixgbe_add_uc_addr Complete\n");
  1027. }
  1028. /**
  1029. * ixgbe_update_uc_addr_list_generic - Updates MAC list of secondary addresses
  1030. * @hw: pointer to hardware structure
  1031. * @addr_list: the list of new addresses
  1032. * @addr_count: number of addresses
  1033. * @next: iterator function to walk the address list
  1034. *
  1035. * The given list replaces any existing list. Clears the secondary addrs from
  1036. * receive address registers. Uses unused receive address registers for the
  1037. * first secondary addresses, and falls back to promiscuous mode as needed.
  1038. *
  1039. * Drivers using secondary unicast addresses must set user_set_promisc when
  1040. * manually putting the device into promiscuous mode.
  1041. **/
  1042. s32 ixgbe_update_uc_addr_list_generic(struct ixgbe_hw *hw, u8 *addr_list,
  1043. u32 addr_count, ixgbe_mc_addr_itr next)
  1044. {
  1045. u8 *addr;
  1046. u32 i;
  1047. u32 old_promisc_setting = hw->addr_ctrl.overflow_promisc;
  1048. u32 uc_addr_in_use;
  1049. u32 fctrl;
  1050. u32 vmdq;
  1051. /*
  1052. * Clear accounting of old secondary address list,
  1053. * don't count RAR[0]
  1054. */
  1055. uc_addr_in_use = hw->addr_ctrl.rar_used_count -
  1056. hw->addr_ctrl.mc_addr_in_rar_count - 1;
  1057. hw->addr_ctrl.rar_used_count -= uc_addr_in_use;
  1058. hw->addr_ctrl.overflow_promisc = 0;
  1059. /* Zero out the other receive addresses */
  1060. hw_dbg(hw, "Clearing RAR[1-%d]\n", uc_addr_in_use);
  1061. for (i = 1; i <= uc_addr_in_use; i++) {
  1062. IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
  1063. IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
  1064. }
  1065. /* Add the new addresses */
  1066. for (i = 0; i < addr_count; i++) {
  1067. hw_dbg(hw, " Adding the secondary addresses:\n");
  1068. addr = next(hw, &addr_list, &vmdq);
  1069. ixgbe_add_uc_addr(hw, addr, vmdq);
  1070. }
  1071. if (hw->addr_ctrl.overflow_promisc) {
  1072. /* enable promisc if not already in overflow or set by user */
  1073. if (!old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
  1074. hw_dbg(hw, " Entering address overflow promisc mode\n");
  1075. fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
  1076. fctrl |= IXGBE_FCTRL_UPE;
  1077. IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
  1078. }
  1079. } else {
  1080. /* only disable if set by overflow, not by user */
  1081. if (old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
  1082. hw_dbg(hw, " Leaving address overflow promisc mode\n");
  1083. fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
  1084. fctrl &= ~IXGBE_FCTRL_UPE;
  1085. IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
  1086. }
  1087. }
  1088. hw_dbg(hw, "ixgbe_update_uc_addr_list_generic Complete\n");
  1089. return 0;
  1090. }
  1091. /**
  1092. * ixgbe_mta_vector - Determines bit-vector in multicast table to set
  1093. * @hw: pointer to hardware structure
  1094. * @mc_addr: the multicast address
  1095. *
  1096. * Extracts the 12 bits, from a multicast address, to determine which
  1097. * bit-vector to set in the multicast table. The hardware uses 12 bits, from
  1098. * incoming rx multicast addresses, to determine the bit-vector to check in
  1099. * the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
  1100. * by the MO field of the MCSTCTRL. The MO field is set during initialization
  1101. * to mc_filter_type.
  1102. **/
  1103. static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
  1104. {
  1105. u32 vector = 0;
  1106. switch (hw->mac.mc_filter_type) {
  1107. case 0: /* use bits [47:36] of the address */
  1108. vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
  1109. break;
  1110. case 1: /* use bits [46:35] of the address */
  1111. vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
  1112. break;
  1113. case 2: /* use bits [45:34] of the address */
  1114. vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
  1115. break;
  1116. case 3: /* use bits [43:32] of the address */
  1117. vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
  1118. break;
  1119. default: /* Invalid mc_filter_type */
  1120. hw_dbg(hw, "MC filter type param set incorrectly\n");
  1121. break;
  1122. }
  1123. /* vector can only be 12-bits or boundary will be exceeded */
  1124. vector &= 0xFFF;
  1125. return vector;
  1126. }
  1127. /**
  1128. * ixgbe_set_mta - Set bit-vector in multicast table
  1129. * @hw: pointer to hardware structure
  1130. * @hash_value: Multicast address hash value
  1131. *
  1132. * Sets the bit-vector in the multicast table.
  1133. **/
  1134. static void ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
  1135. {
  1136. u32 vector;
  1137. u32 vector_bit;
  1138. u32 vector_reg;
  1139. u32 mta_reg;
  1140. hw->addr_ctrl.mta_in_use++;
  1141. vector = ixgbe_mta_vector(hw, mc_addr);
  1142. hw_dbg(hw, " bit-vector = 0x%03X\n", vector);
  1143. /*
  1144. * The MTA is a register array of 128 32-bit registers. It is treated
  1145. * like an array of 4096 bits. We want to set bit
  1146. * BitArray[vector_value]. So we figure out what register the bit is
  1147. * in, read it, OR in the new bit, then write back the new value. The
  1148. * register is determined by the upper 7 bits of the vector value and
  1149. * the bit within that register are determined by the lower 5 bits of
  1150. * the value.
  1151. */
  1152. vector_reg = (vector >> 5) & 0x7F;
  1153. vector_bit = vector & 0x1F;
  1154. mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
  1155. mta_reg |= (1 << vector_bit);
  1156. IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
  1157. }
  1158. /**
  1159. * ixgbe_add_mc_addr - Adds a multicast address.
  1160. * @hw: pointer to hardware structure
  1161. * @mc_addr: new multicast address
  1162. *
  1163. * Adds it to unused receive address register or to the multicast table.
  1164. **/
  1165. static void ixgbe_add_mc_addr(struct ixgbe_hw *hw, u8 *mc_addr)
  1166. {
  1167. u32 rar_entries = hw->mac.num_rar_entries;
  1168. u32 rar;
  1169. hw_dbg(hw, " MC Addr =%.2X %.2X %.2X %.2X %.2X %.2X\n",
  1170. mc_addr[0], mc_addr[1], mc_addr[2],
  1171. mc_addr[3], mc_addr[4], mc_addr[5]);
  1172. /*
  1173. * Place this multicast address in the RAR if there is room,
  1174. * else put it in the MTA
  1175. */
  1176. if (hw->addr_ctrl.rar_used_count < rar_entries) {
  1177. /* use RAR from the end up for multicast */
  1178. rar = rar_entries - hw->addr_ctrl.mc_addr_in_rar_count - 1;
  1179. hw->mac.ops.set_rar(hw, rar, mc_addr, 0, IXGBE_RAH_AV);
  1180. hw_dbg(hw, "Added a multicast address to RAR[%d]\n", rar);
  1181. hw->addr_ctrl.rar_used_count++;
  1182. hw->addr_ctrl.mc_addr_in_rar_count++;
  1183. } else {
  1184. ixgbe_set_mta(hw, mc_addr);
  1185. }
  1186. hw_dbg(hw, "ixgbe_add_mc_addr Complete\n");
  1187. }
  1188. /**
  1189. * ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
  1190. * @hw: pointer to hardware structure
  1191. * @mc_addr_list: the list of new multicast addresses
  1192. * @mc_addr_count: number of addresses
  1193. * @next: iterator function to walk the multicast address list
  1194. *
  1195. * The given list replaces any existing list. Clears the MC addrs from receive
  1196. * address registers and the multicast table. Uses unused receive address
  1197. * registers for the first multicast addresses, and hashes the rest into the
  1198. * multicast table.
  1199. **/
  1200. s32 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw, u8 *mc_addr_list,
  1201. u32 mc_addr_count, ixgbe_mc_addr_itr next)
  1202. {
  1203. u32 i;
  1204. u32 rar_entries = hw->mac.num_rar_entries;
  1205. u32 vmdq;
  1206. /*
  1207. * Set the new number of MC addresses that we are being requested to
  1208. * use.
  1209. */
  1210. hw->addr_ctrl.num_mc_addrs = mc_addr_count;
  1211. hw->addr_ctrl.rar_used_count -= hw->addr_ctrl.mc_addr_in_rar_count;
  1212. hw->addr_ctrl.mc_addr_in_rar_count = 0;
  1213. hw->addr_ctrl.mta_in_use = 0;
  1214. /* Zero out the other receive addresses. */
  1215. hw_dbg(hw, "Clearing RAR[%d-%d]\n", hw->addr_ctrl.rar_used_count,
  1216. rar_entries - 1);
  1217. for (i = hw->addr_ctrl.rar_used_count; i < rar_entries; i++) {
  1218. IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
  1219. IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
  1220. }
  1221. /* Clear the MTA */
  1222. hw_dbg(hw, " Clearing MTA\n");
  1223. for (i = 0; i < hw->mac.mcft_size; i++)
  1224. IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
  1225. /* Add the new addresses */
  1226. for (i = 0; i < mc_addr_count; i++) {
  1227. hw_dbg(hw, " Adding the multicast addresses:\n");
  1228. ixgbe_add_mc_addr(hw, next(hw, &mc_addr_list, &vmdq));
  1229. }
  1230. /* Enable mta */
  1231. if (hw->addr_ctrl.mta_in_use > 0)
  1232. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
  1233. IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
  1234. hw_dbg(hw, "ixgbe_update_mc_addr_list_generic Complete\n");
  1235. return 0;
  1236. }
  1237. /**
  1238. * ixgbe_enable_mc_generic - Enable multicast address in RAR
  1239. * @hw: pointer to hardware structure
  1240. *
  1241. * Enables multicast address in RAR and the use of the multicast hash table.
  1242. **/
  1243. s32 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
  1244. {
  1245. u32 i;
  1246. u32 rar_entries = hw->mac.num_rar_entries;
  1247. struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
  1248. if (a->mc_addr_in_rar_count > 0)
  1249. for (i = (rar_entries - a->mc_addr_in_rar_count);
  1250. i < rar_entries; i++)
  1251. ixgbe_enable_rar(hw, i);
  1252. if (a->mta_in_use > 0)
  1253. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
  1254. hw->mac.mc_filter_type);
  1255. return 0;
  1256. }
  1257. /**
  1258. * ixgbe_disable_mc_generic - Disable multicast address in RAR
  1259. * @hw: pointer to hardware structure
  1260. *
  1261. * Disables multicast address in RAR and the use of the multicast hash table.
  1262. **/
  1263. s32 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
  1264. {
  1265. u32 i;
  1266. u32 rar_entries = hw->mac.num_rar_entries;
  1267. struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
  1268. if (a->mc_addr_in_rar_count > 0)
  1269. for (i = (rar_entries - a->mc_addr_in_rar_count);
  1270. i < rar_entries; i++)
  1271. ixgbe_disable_rar(hw, i);
  1272. if (a->mta_in_use > 0)
  1273. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
  1274. return 0;
  1275. }
  1276. /**
  1277. * ixgbe_disable_pcie_master - Disable PCI-express master access
  1278. * @hw: pointer to hardware structure
  1279. *
  1280. * Disables PCI-Express master access and verifies there are no pending
  1281. * requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
  1282. * bit hasn't caused the master requests to be disabled, else 0
  1283. * is returned signifying master requests disabled.
  1284. **/
  1285. s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
  1286. {
  1287. u32 i;
  1288. u32 reg_val;
  1289. u32 number_of_queues;
  1290. s32 status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
  1291. /* Disable the receive unit by stopping each queue */
  1292. number_of_queues = hw->mac.max_rx_queues;
  1293. for (i = 0; i < number_of_queues; i++) {
  1294. reg_val = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
  1295. if (reg_val & IXGBE_RXDCTL_ENABLE) {
  1296. reg_val &= ~IXGBE_RXDCTL_ENABLE;
  1297. IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), reg_val);
  1298. }
  1299. }
  1300. reg_val = IXGBE_READ_REG(hw, IXGBE_CTRL);
  1301. reg_val |= IXGBE_CTRL_GIO_DIS;
  1302. IXGBE_WRITE_REG(hw, IXGBE_CTRL, reg_val);
  1303. for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
  1304. if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO)) {
  1305. status = 0;
  1306. break;
  1307. }
  1308. udelay(100);
  1309. }
  1310. return status;
  1311. }
  1312. /**
  1313. * ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
  1314. * @hw: pointer to hardware structure
  1315. * @mask: Mask to specify which semaphore to acquire
  1316. *
  1317. * Acquires the SWFW semaphore thought the GSSR register for the specified
  1318. * function (CSR, PHY0, PHY1, EEPROM, Flash)
  1319. **/
  1320. s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u16 mask)
  1321. {
  1322. u32 gssr;
  1323. u32 swmask = mask;
  1324. u32 fwmask = mask << 5;
  1325. s32 timeout = 200;
  1326. while (timeout) {
  1327. if (ixgbe_get_eeprom_semaphore(hw))
  1328. return -IXGBE_ERR_SWFW_SYNC;
  1329. gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
  1330. if (!(gssr & (fwmask | swmask)))
  1331. break;
  1332. /*
  1333. * Firmware currently using resource (fwmask) or other software
  1334. * thread currently using resource (swmask)
  1335. */
  1336. ixgbe_release_eeprom_semaphore(hw);
  1337. msleep(5);
  1338. timeout--;
  1339. }
  1340. if (!timeout) {
  1341. hw_dbg(hw, "Driver can't access resource, GSSR timeout.\n");
  1342. return -IXGBE_ERR_SWFW_SYNC;
  1343. }
  1344. gssr |= swmask;
  1345. IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
  1346. ixgbe_release_eeprom_semaphore(hw);
  1347. return 0;
  1348. }
  1349. /**
  1350. * ixgbe_release_swfw_sync - Release SWFW semaphore
  1351. * @hw: pointer to hardware structure
  1352. * @mask: Mask to specify which semaphore to release
  1353. *
  1354. * Releases the SWFW semaphore thought the GSSR register for the specified
  1355. * function (CSR, PHY0, PHY1, EEPROM, Flash)
  1356. **/
  1357. void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u16 mask)
  1358. {
  1359. u32 gssr;
  1360. u32 swmask = mask;
  1361. ixgbe_get_eeprom_semaphore(hw);
  1362. gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
  1363. gssr &= ~swmask;
  1364. IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
  1365. ixgbe_release_eeprom_semaphore(hw);
  1366. }