i40e_adminq.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  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. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * Contact Information:
  23. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. ******************************************************************************/
  27. #include "i40e_status.h"
  28. #include "i40e_type.h"
  29. #include "i40e_register.h"
  30. #include "i40e_adminq.h"
  31. #include "i40e_prototype.h"
  32. /**
  33. * i40e_adminq_init_regs - Initialize AdminQ registers
  34. * @hw: pointer to the hardware structure
  35. *
  36. * This assumes the alloc_asq and alloc_arq functions have already been called
  37. **/
  38. static void i40e_adminq_init_regs(struct i40e_hw *hw)
  39. {
  40. /* set head and tail registers in our local struct */
  41. if (hw->mac.type == I40E_MAC_VF) {
  42. hw->aq.asq.tail = I40E_VF_ATQT1;
  43. hw->aq.asq.head = I40E_VF_ATQH1;
  44. hw->aq.asq.len = I40E_VF_ATQLEN1;
  45. hw->aq.arq.tail = I40E_VF_ARQT1;
  46. hw->aq.arq.head = I40E_VF_ARQH1;
  47. hw->aq.arq.len = I40E_VF_ARQLEN1;
  48. } else {
  49. hw->aq.asq.tail = I40E_PF_ATQT;
  50. hw->aq.asq.head = I40E_PF_ATQH;
  51. hw->aq.asq.len = I40E_PF_ATQLEN;
  52. hw->aq.arq.tail = I40E_PF_ARQT;
  53. hw->aq.arq.head = I40E_PF_ARQH;
  54. hw->aq.arq.len = I40E_PF_ARQLEN;
  55. }
  56. }
  57. /**
  58. * i40e_alloc_adminq_asq_ring - Allocate Admin Queue send rings
  59. * @hw: pointer to the hardware structure
  60. **/
  61. static i40e_status i40e_alloc_adminq_asq_ring(struct i40e_hw *hw)
  62. {
  63. i40e_status ret_code;
  64. struct i40e_virt_mem mem;
  65. ret_code = i40e_allocate_dma_mem(hw, &hw->aq.asq_mem,
  66. i40e_mem_atq_ring,
  67. (hw->aq.num_asq_entries *
  68. sizeof(struct i40e_aq_desc)),
  69. I40E_ADMINQ_DESC_ALIGNMENT);
  70. if (ret_code)
  71. return ret_code;
  72. hw->aq.asq.desc = hw->aq.asq_mem.va;
  73. hw->aq.asq.dma_addr = hw->aq.asq_mem.pa;
  74. ret_code = i40e_allocate_virt_mem(hw, &mem,
  75. (hw->aq.num_asq_entries *
  76. sizeof(struct i40e_asq_cmd_details)));
  77. if (ret_code) {
  78. i40e_free_dma_mem(hw, &hw->aq.asq_mem);
  79. hw->aq.asq_mem.va = NULL;
  80. hw->aq.asq_mem.pa = 0;
  81. return ret_code;
  82. }
  83. hw->aq.asq.details = mem.va;
  84. return ret_code;
  85. }
  86. /**
  87. * i40e_alloc_adminq_arq_ring - Allocate Admin Queue receive rings
  88. * @hw: pointer to the hardware structure
  89. **/
  90. static i40e_status i40e_alloc_adminq_arq_ring(struct i40e_hw *hw)
  91. {
  92. i40e_status ret_code;
  93. ret_code = i40e_allocate_dma_mem(hw, &hw->aq.arq_mem,
  94. i40e_mem_arq_ring,
  95. (hw->aq.num_arq_entries *
  96. sizeof(struct i40e_aq_desc)),
  97. I40E_ADMINQ_DESC_ALIGNMENT);
  98. if (ret_code)
  99. return ret_code;
  100. hw->aq.arq.desc = hw->aq.arq_mem.va;
  101. hw->aq.arq.dma_addr = hw->aq.arq_mem.pa;
  102. return ret_code;
  103. }
  104. /**
  105. * i40e_free_adminq_asq - Free Admin Queue send rings
  106. * @hw: pointer to the hardware structure
  107. *
  108. * This assumes the posted send buffers have already been cleaned
  109. * and de-allocated
  110. **/
  111. static void i40e_free_adminq_asq(struct i40e_hw *hw)
  112. {
  113. struct i40e_virt_mem mem;
  114. i40e_free_dma_mem(hw, &hw->aq.asq_mem);
  115. hw->aq.asq_mem.va = NULL;
  116. hw->aq.asq_mem.pa = 0;
  117. mem.va = hw->aq.asq.details;
  118. i40e_free_virt_mem(hw, &mem);
  119. hw->aq.asq.details = NULL;
  120. }
  121. /**
  122. * i40e_free_adminq_arq - Free Admin Queue receive rings
  123. * @hw: pointer to the hardware structure
  124. *
  125. * This assumes the posted receive buffers have already been cleaned
  126. * and de-allocated
  127. **/
  128. static void i40e_free_adminq_arq(struct i40e_hw *hw)
  129. {
  130. i40e_free_dma_mem(hw, &hw->aq.arq_mem);
  131. hw->aq.arq_mem.va = NULL;
  132. hw->aq.arq_mem.pa = 0;
  133. }
  134. /**
  135. * i40e_alloc_arq_bufs - Allocate pre-posted buffers for the receive queue
  136. * @hw: pointer to the hardware structure
  137. **/
  138. static i40e_status i40e_alloc_arq_bufs(struct i40e_hw *hw)
  139. {
  140. i40e_status ret_code;
  141. struct i40e_aq_desc *desc;
  142. struct i40e_virt_mem mem;
  143. struct i40e_dma_mem *bi;
  144. int i;
  145. /* We'll be allocating the buffer info memory first, then we can
  146. * allocate the mapped buffers for the event processing
  147. */
  148. /* buffer_info structures do not need alignment */
  149. ret_code = i40e_allocate_virt_mem(hw, &mem, (hw->aq.num_arq_entries *
  150. sizeof(struct i40e_dma_mem)));
  151. if (ret_code)
  152. goto alloc_arq_bufs;
  153. hw->aq.arq.r.arq_bi = (struct i40e_dma_mem *)mem.va;
  154. /* allocate the mapped buffers */
  155. for (i = 0; i < hw->aq.num_arq_entries; i++) {
  156. bi = &hw->aq.arq.r.arq_bi[i];
  157. ret_code = i40e_allocate_dma_mem(hw, bi,
  158. i40e_mem_arq_buf,
  159. hw->aq.arq_buf_size,
  160. I40E_ADMINQ_DESC_ALIGNMENT);
  161. if (ret_code)
  162. goto unwind_alloc_arq_bufs;
  163. /* now configure the descriptors for use */
  164. desc = I40E_ADMINQ_DESC(hw->aq.arq, i);
  165. desc->flags = cpu_to_le16(I40E_AQ_FLAG_BUF);
  166. if (hw->aq.arq_buf_size > I40E_AQ_LARGE_BUF)
  167. desc->flags |= cpu_to_le16(I40E_AQ_FLAG_LB);
  168. desc->opcode = 0;
  169. /* This is in accordance with Admin queue design, there is no
  170. * register for buffer size configuration
  171. */
  172. desc->datalen = cpu_to_le16((u16)bi->size);
  173. desc->retval = 0;
  174. desc->cookie_high = 0;
  175. desc->cookie_low = 0;
  176. desc->params.external.addr_high =
  177. cpu_to_le32(upper_32_bits(bi->pa));
  178. desc->params.external.addr_low =
  179. cpu_to_le32(lower_32_bits(bi->pa));
  180. desc->params.external.param0 = 0;
  181. desc->params.external.param1 = 0;
  182. }
  183. alloc_arq_bufs:
  184. return ret_code;
  185. unwind_alloc_arq_bufs:
  186. /* don't try to free the one that failed... */
  187. i--;
  188. for (; i >= 0; i--)
  189. i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
  190. mem.va = hw->aq.arq.r.arq_bi;
  191. i40e_free_virt_mem(hw, &mem);
  192. return ret_code;
  193. }
  194. /**
  195. * i40e_alloc_asq_bufs - Allocate empty buffer structs for the send queue
  196. * @hw: pointer to the hardware structure
  197. **/
  198. static i40e_status i40e_alloc_asq_bufs(struct i40e_hw *hw)
  199. {
  200. i40e_status ret_code;
  201. struct i40e_virt_mem mem;
  202. struct i40e_dma_mem *bi;
  203. int i;
  204. /* No mapped memory needed yet, just the buffer info structures */
  205. ret_code = i40e_allocate_virt_mem(hw, &mem, (hw->aq.num_asq_entries *
  206. sizeof(struct i40e_dma_mem)));
  207. if (ret_code)
  208. goto alloc_asq_bufs;
  209. hw->aq.asq.r.asq_bi = (struct i40e_dma_mem *)mem.va;
  210. /* allocate the mapped buffers */
  211. for (i = 0; i < hw->aq.num_asq_entries; i++) {
  212. bi = &hw->aq.asq.r.asq_bi[i];
  213. ret_code = i40e_allocate_dma_mem(hw, bi,
  214. i40e_mem_asq_buf,
  215. hw->aq.asq_buf_size,
  216. I40E_ADMINQ_DESC_ALIGNMENT);
  217. if (ret_code)
  218. goto unwind_alloc_asq_bufs;
  219. }
  220. alloc_asq_bufs:
  221. return ret_code;
  222. unwind_alloc_asq_bufs:
  223. /* don't try to free the one that failed... */
  224. i--;
  225. for (; i >= 0; i--)
  226. i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
  227. mem.va = hw->aq.asq.r.asq_bi;
  228. i40e_free_virt_mem(hw, &mem);
  229. return ret_code;
  230. }
  231. /**
  232. * i40e_free_arq_bufs - Free receive queue buffer info elements
  233. * @hw: pointer to the hardware structure
  234. **/
  235. static void i40e_free_arq_bufs(struct i40e_hw *hw)
  236. {
  237. struct i40e_virt_mem mem;
  238. int i;
  239. for (i = 0; i < hw->aq.num_arq_entries; i++)
  240. i40e_free_dma_mem(hw, &hw->aq.arq.r.arq_bi[i]);
  241. mem.va = hw->aq.arq.r.arq_bi;
  242. i40e_free_virt_mem(hw, &mem);
  243. }
  244. /**
  245. * i40e_free_asq_bufs - Free send queue buffer info elements
  246. * @hw: pointer to the hardware structure
  247. **/
  248. static void i40e_free_asq_bufs(struct i40e_hw *hw)
  249. {
  250. struct i40e_virt_mem mem;
  251. int i;
  252. /* only unmap if the address is non-NULL */
  253. for (i = 0; i < hw->aq.num_asq_entries; i++)
  254. if (hw->aq.asq.r.asq_bi[i].pa)
  255. i40e_free_dma_mem(hw, &hw->aq.asq.r.asq_bi[i]);
  256. /* now free the buffer info list */
  257. mem.va = hw->aq.asq.r.asq_bi;
  258. i40e_free_virt_mem(hw, &mem);
  259. }
  260. /**
  261. * i40e_config_asq_regs - configure ASQ registers
  262. * @hw: pointer to the hardware structure
  263. *
  264. * Configure base address and length registers for the transmit queue
  265. **/
  266. static void i40e_config_asq_regs(struct i40e_hw *hw)
  267. {
  268. if (hw->mac.type == I40E_MAC_VF) {
  269. /* configure the transmit queue */
  270. wr32(hw, I40E_VF_ATQBAH1, upper_32_bits(hw->aq.asq.dma_addr));
  271. wr32(hw, I40E_VF_ATQBAL1, lower_32_bits(hw->aq.asq.dma_addr));
  272. wr32(hw, I40E_VF_ATQLEN1, (hw->aq.num_asq_entries |
  273. I40E_VF_ATQLEN1_ATQENABLE_MASK));
  274. } else {
  275. /* configure the transmit queue */
  276. wr32(hw, I40E_PF_ATQBAH, upper_32_bits(hw->aq.asq.dma_addr));
  277. wr32(hw, I40E_PF_ATQBAL, lower_32_bits(hw->aq.asq.dma_addr));
  278. wr32(hw, I40E_PF_ATQLEN, (hw->aq.num_asq_entries |
  279. I40E_PF_ATQLEN_ATQENABLE_MASK));
  280. }
  281. }
  282. /**
  283. * i40e_config_arq_regs - ARQ register configuration
  284. * @hw: pointer to the hardware structure
  285. *
  286. * Configure base address and length registers for the receive (event queue)
  287. **/
  288. static void i40e_config_arq_regs(struct i40e_hw *hw)
  289. {
  290. if (hw->mac.type == I40E_MAC_VF) {
  291. /* configure the receive queue */
  292. wr32(hw, I40E_VF_ARQBAH1, upper_32_bits(hw->aq.arq.dma_addr));
  293. wr32(hw, I40E_VF_ARQBAL1, lower_32_bits(hw->aq.arq.dma_addr));
  294. wr32(hw, I40E_VF_ARQLEN1, (hw->aq.num_arq_entries |
  295. I40E_VF_ARQLEN1_ARQENABLE_MASK));
  296. } else {
  297. /* configure the receive queue */
  298. wr32(hw, I40E_PF_ARQBAH, upper_32_bits(hw->aq.arq.dma_addr));
  299. wr32(hw, I40E_PF_ARQBAL, lower_32_bits(hw->aq.arq.dma_addr));
  300. wr32(hw, I40E_PF_ARQLEN, (hw->aq.num_arq_entries |
  301. I40E_PF_ARQLEN_ARQENABLE_MASK));
  302. }
  303. /* Update tail in the HW to post pre-allocated buffers */
  304. wr32(hw, hw->aq.arq.tail, hw->aq.num_arq_entries - 1);
  305. }
  306. /**
  307. * i40e_init_asq - main initialization routine for ASQ
  308. * @hw: pointer to the hardware structure
  309. *
  310. * This is the main initialization routine for the Admin Send Queue
  311. * Prior to calling this function, drivers *MUST* set the following fields
  312. * in the hw->aq structure:
  313. * - hw->aq.num_asq_entries
  314. * - hw->aq.arq_buf_size
  315. *
  316. * Do *NOT* hold the lock when calling this as the memory allocation routines
  317. * called are not going to be atomic context safe
  318. **/
  319. static i40e_status i40e_init_asq(struct i40e_hw *hw)
  320. {
  321. i40e_status ret_code = 0;
  322. if (hw->aq.asq.count > 0) {
  323. /* queue already initialized */
  324. ret_code = I40E_ERR_NOT_READY;
  325. goto init_adminq_exit;
  326. }
  327. /* verify input for valid configuration */
  328. if ((hw->aq.num_asq_entries == 0) ||
  329. (hw->aq.asq_buf_size == 0)) {
  330. ret_code = I40E_ERR_CONFIG;
  331. goto init_adminq_exit;
  332. }
  333. hw->aq.asq.next_to_use = 0;
  334. hw->aq.asq.next_to_clean = 0;
  335. hw->aq.asq.count = hw->aq.num_asq_entries;
  336. /* allocate the ring memory */
  337. ret_code = i40e_alloc_adminq_asq_ring(hw);
  338. if (ret_code)
  339. goto init_adminq_exit;
  340. /* allocate buffers in the rings */
  341. ret_code = i40e_alloc_asq_bufs(hw);
  342. if (ret_code)
  343. goto init_adminq_free_rings;
  344. /* initialize base registers */
  345. i40e_config_asq_regs(hw);
  346. /* success! */
  347. goto init_adminq_exit;
  348. init_adminq_free_rings:
  349. i40e_free_adminq_asq(hw);
  350. init_adminq_exit:
  351. return ret_code;
  352. }
  353. /**
  354. * i40e_init_arq - initialize ARQ
  355. * @hw: pointer to the hardware structure
  356. *
  357. * The main initialization routine for the Admin Receive (Event) Queue.
  358. * Prior to calling this function, drivers *MUST* set the following fields
  359. * in the hw->aq structure:
  360. * - hw->aq.num_asq_entries
  361. * - hw->aq.arq_buf_size
  362. *
  363. * Do *NOT* hold the lock when calling this as the memory allocation routines
  364. * called are not going to be atomic context safe
  365. **/
  366. static i40e_status i40e_init_arq(struct i40e_hw *hw)
  367. {
  368. i40e_status ret_code = 0;
  369. if (hw->aq.arq.count > 0) {
  370. /* queue already initialized */
  371. ret_code = I40E_ERR_NOT_READY;
  372. goto init_adminq_exit;
  373. }
  374. /* verify input for valid configuration */
  375. if ((hw->aq.num_arq_entries == 0) ||
  376. (hw->aq.arq_buf_size == 0)) {
  377. ret_code = I40E_ERR_CONFIG;
  378. goto init_adminq_exit;
  379. }
  380. hw->aq.arq.next_to_use = 0;
  381. hw->aq.arq.next_to_clean = 0;
  382. hw->aq.arq.count = hw->aq.num_arq_entries;
  383. /* allocate the ring memory */
  384. ret_code = i40e_alloc_adminq_arq_ring(hw);
  385. if (ret_code)
  386. goto init_adminq_exit;
  387. /* allocate buffers in the rings */
  388. ret_code = i40e_alloc_arq_bufs(hw);
  389. if (ret_code)
  390. goto init_adminq_free_rings;
  391. /* initialize base registers */
  392. i40e_config_arq_regs(hw);
  393. /* success! */
  394. goto init_adminq_exit;
  395. init_adminq_free_rings:
  396. i40e_free_adminq_arq(hw);
  397. init_adminq_exit:
  398. return ret_code;
  399. }
  400. /**
  401. * i40e_shutdown_asq - shutdown the ASQ
  402. * @hw: pointer to the hardware structure
  403. *
  404. * The main shutdown routine for the Admin Send Queue
  405. **/
  406. static i40e_status i40e_shutdown_asq(struct i40e_hw *hw)
  407. {
  408. i40e_status ret_code = 0;
  409. if (hw->aq.asq.count == 0)
  410. return I40E_ERR_NOT_READY;
  411. /* Stop firmware AdminQ processing */
  412. wr32(hw, hw->aq.asq.head, 0);
  413. wr32(hw, hw->aq.asq.tail, 0);
  414. wr32(hw, hw->aq.asq.len, 0);
  415. /* make sure lock is available */
  416. mutex_lock(&hw->aq.asq_mutex);
  417. hw->aq.asq.count = 0; /* to indicate uninitialized queue */
  418. /* free ring buffers */
  419. i40e_free_asq_bufs(hw);
  420. /* free the ring descriptors */
  421. i40e_free_adminq_asq(hw);
  422. mutex_unlock(&hw->aq.asq_mutex);
  423. return ret_code;
  424. }
  425. /**
  426. * i40e_shutdown_arq - shutdown ARQ
  427. * @hw: pointer to the hardware structure
  428. *
  429. * The main shutdown routine for the Admin Receive Queue
  430. **/
  431. static i40e_status i40e_shutdown_arq(struct i40e_hw *hw)
  432. {
  433. i40e_status ret_code = 0;
  434. if (hw->aq.arq.count == 0)
  435. return I40E_ERR_NOT_READY;
  436. /* Stop firmware AdminQ processing */
  437. wr32(hw, hw->aq.arq.head, 0);
  438. wr32(hw, hw->aq.arq.tail, 0);
  439. wr32(hw, hw->aq.arq.len, 0);
  440. /* make sure lock is available */
  441. mutex_lock(&hw->aq.arq_mutex);
  442. hw->aq.arq.count = 0; /* to indicate uninitialized queue */
  443. /* free ring buffers */
  444. i40e_free_arq_bufs(hw);
  445. /* free the ring descriptors */
  446. i40e_free_adminq_arq(hw);
  447. mutex_unlock(&hw->aq.arq_mutex);
  448. return ret_code;
  449. }
  450. /**
  451. * i40e_init_adminq - main initialization routine for Admin Queue
  452. * @hw: pointer to the hardware structure
  453. *
  454. * Prior to calling this function, drivers *MUST* set the following fields
  455. * in the hw->aq structure:
  456. * - hw->aq.num_asq_entries
  457. * - hw->aq.num_arq_entries
  458. * - hw->aq.arq_buf_size
  459. * - hw->aq.asq_buf_size
  460. **/
  461. i40e_status i40e_init_adminq(struct i40e_hw *hw)
  462. {
  463. i40e_status ret_code;
  464. u16 eetrack_lo, eetrack_hi;
  465. int retry = 0;
  466. /* verify input for valid configuration */
  467. if ((hw->aq.num_arq_entries == 0) ||
  468. (hw->aq.num_asq_entries == 0) ||
  469. (hw->aq.arq_buf_size == 0) ||
  470. (hw->aq.asq_buf_size == 0)) {
  471. ret_code = I40E_ERR_CONFIG;
  472. goto init_adminq_exit;
  473. }
  474. /* initialize locks */
  475. mutex_init(&hw->aq.asq_mutex);
  476. mutex_init(&hw->aq.arq_mutex);
  477. /* Set up register offsets */
  478. i40e_adminq_init_regs(hw);
  479. /* allocate the ASQ */
  480. ret_code = i40e_init_asq(hw);
  481. if (ret_code)
  482. goto init_adminq_destroy_locks;
  483. /* allocate the ARQ */
  484. ret_code = i40e_init_arq(hw);
  485. if (ret_code)
  486. goto init_adminq_free_asq;
  487. /* There are some cases where the firmware may not be quite ready
  488. * for AdminQ operations, so we retry the AdminQ setup a few times
  489. * if we see timeouts in this first AQ call.
  490. */
  491. do {
  492. ret_code = i40e_aq_get_firmware_version(hw,
  493. &hw->aq.fw_maj_ver,
  494. &hw->aq.fw_min_ver,
  495. &hw->aq.api_maj_ver,
  496. &hw->aq.api_min_ver,
  497. NULL);
  498. if (ret_code != I40E_ERR_ADMIN_QUEUE_TIMEOUT)
  499. break;
  500. retry++;
  501. msleep(100);
  502. i40e_resume_aq(hw);
  503. } while (retry < 10);
  504. if (ret_code != I40E_SUCCESS)
  505. goto init_adminq_free_arq;
  506. if (hw->aq.api_maj_ver != I40E_FW_API_VERSION_MAJOR ||
  507. hw->aq.api_min_ver != I40E_FW_API_VERSION_MINOR) {
  508. ret_code = I40E_ERR_FIRMWARE_API_VERSION;
  509. goto init_adminq_free_arq;
  510. }
  511. i40e_read_nvm_word(hw, I40E_SR_NVM_IMAGE_VERSION, &hw->nvm.version);
  512. i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_LO, &eetrack_lo);
  513. i40e_read_nvm_word(hw, I40E_SR_NVM_EETRACK_HI, &eetrack_hi);
  514. hw->nvm.eetrack = (eetrack_hi << 16) | eetrack_lo;
  515. ret_code = i40e_aq_set_hmc_resource_profile(hw,
  516. I40E_HMC_PROFILE_DEFAULT,
  517. 0,
  518. NULL);
  519. ret_code = 0;
  520. /* success! */
  521. goto init_adminq_exit;
  522. init_adminq_free_arq:
  523. i40e_shutdown_arq(hw);
  524. init_adminq_free_asq:
  525. i40e_shutdown_asq(hw);
  526. init_adminq_destroy_locks:
  527. init_adminq_exit:
  528. return ret_code;
  529. }
  530. /**
  531. * i40e_shutdown_adminq - shutdown routine for the Admin Queue
  532. * @hw: pointer to the hardware structure
  533. **/
  534. i40e_status i40e_shutdown_adminq(struct i40e_hw *hw)
  535. {
  536. i40e_status ret_code = 0;
  537. i40e_shutdown_asq(hw);
  538. i40e_shutdown_arq(hw);
  539. /* destroy the locks */
  540. return ret_code;
  541. }
  542. /**
  543. * i40e_clean_asq - cleans Admin send queue
  544. * @asq: pointer to the adminq send ring
  545. *
  546. * returns the number of free desc
  547. **/
  548. static u16 i40e_clean_asq(struct i40e_hw *hw)
  549. {
  550. struct i40e_adminq_ring *asq = &(hw->aq.asq);
  551. struct i40e_asq_cmd_details *details;
  552. u16 ntc = asq->next_to_clean;
  553. struct i40e_aq_desc desc_cb;
  554. struct i40e_aq_desc *desc;
  555. desc = I40E_ADMINQ_DESC(*asq, ntc);
  556. details = I40E_ADMINQ_DETAILS(*asq, ntc);
  557. while (rd32(hw, hw->aq.asq.head) != ntc) {
  558. if (details->callback) {
  559. I40E_ADMINQ_CALLBACK cb_func =
  560. (I40E_ADMINQ_CALLBACK)details->callback;
  561. desc_cb = *desc;
  562. cb_func(hw, &desc_cb);
  563. }
  564. memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
  565. memset((void *)details, 0,
  566. sizeof(struct i40e_asq_cmd_details));
  567. ntc++;
  568. if (ntc == asq->count)
  569. ntc = 0;
  570. desc = I40E_ADMINQ_DESC(*asq, ntc);
  571. details = I40E_ADMINQ_DETAILS(*asq, ntc);
  572. }
  573. asq->next_to_clean = ntc;
  574. return I40E_DESC_UNUSED(asq);
  575. }
  576. /**
  577. * i40e_asq_done - check if FW has processed the Admin Send Queue
  578. * @hw: pointer to the hw struct
  579. *
  580. * Returns true if the firmware has processed all descriptors on the
  581. * admin send queue. Returns false if there are still requests pending.
  582. **/
  583. bool i40e_asq_done(struct i40e_hw *hw)
  584. {
  585. /* AQ designers suggest use of head for better
  586. * timing reliability than DD bit
  587. */
  588. return (rd32(hw, hw->aq.asq.head) == hw->aq.asq.next_to_use);
  589. }
  590. /**
  591. * i40e_asq_send_command - send command to Admin Queue
  592. * @hw: pointer to the hw struct
  593. * @desc: prefilled descriptor describing the command (non DMA mem)
  594. * @buff: buffer to use for indirect commands
  595. * @buff_size: size of buffer for indirect commands
  596. * @opaque: pointer to info to be used in async cleanup
  597. *
  598. * This is the main send command driver routine for the Admin Queue send
  599. * queue. It runs the queue, cleans the queue, etc
  600. **/
  601. i40e_status i40e_asq_send_command(struct i40e_hw *hw,
  602. struct i40e_aq_desc *desc,
  603. void *buff, /* can be NULL */
  604. u16 buff_size,
  605. struct i40e_asq_cmd_details *cmd_details)
  606. {
  607. i40e_status status = 0;
  608. struct i40e_dma_mem *dma_buff = NULL;
  609. struct i40e_asq_cmd_details *details;
  610. struct i40e_aq_desc *desc_on_ring;
  611. bool cmd_completed = false;
  612. u16 retval = 0;
  613. if (hw->aq.asq.count == 0) {
  614. i40e_debug(hw, I40E_DEBUG_AQ_MESSAGE,
  615. "AQTX: Admin queue not initialized.\n");
  616. status = I40E_ERR_QUEUE_EMPTY;
  617. goto asq_send_command_exit;
  618. }
  619. details = I40E_ADMINQ_DETAILS(hw->aq.asq, hw->aq.asq.next_to_use);
  620. if (cmd_details) {
  621. *details = *cmd_details;
  622. /* If the cmd_details are defined copy the cookie. The
  623. * cpu_to_le32 is not needed here because the data is ignored
  624. * by the FW, only used by the driver
  625. */
  626. if (details->cookie) {
  627. desc->cookie_high =
  628. cpu_to_le32(upper_32_bits(details->cookie));
  629. desc->cookie_low =
  630. cpu_to_le32(lower_32_bits(details->cookie));
  631. }
  632. } else {
  633. memset(details, 0, sizeof(struct i40e_asq_cmd_details));
  634. }
  635. /* clear requested flags and then set additional flags if defined */
  636. desc->flags &= ~cpu_to_le16(details->flags_dis);
  637. desc->flags |= cpu_to_le16(details->flags_ena);
  638. mutex_lock(&hw->aq.asq_mutex);
  639. if (buff_size > hw->aq.asq_buf_size) {
  640. i40e_debug(hw,
  641. I40E_DEBUG_AQ_MESSAGE,
  642. "AQTX: Invalid buffer size: %d.\n",
  643. buff_size);
  644. status = I40E_ERR_INVALID_SIZE;
  645. goto asq_send_command_error;
  646. }
  647. if (details->postpone && !details->async) {
  648. i40e_debug(hw,
  649. I40E_DEBUG_AQ_MESSAGE,
  650. "AQTX: Async flag not set along with postpone flag");
  651. status = I40E_ERR_PARAM;
  652. goto asq_send_command_error;
  653. }
  654. /* call clean and check queue available function to reclaim the
  655. * descriptors that were processed by FW, the function returns the
  656. * number of desc available
  657. */
  658. /* the clean function called here could be called in a separate thread
  659. * in case of asynchronous completions
  660. */
  661. if (i40e_clean_asq(hw) == 0) {
  662. i40e_debug(hw,
  663. I40E_DEBUG_AQ_MESSAGE,
  664. "AQTX: Error queue is full.\n");
  665. status = I40E_ERR_ADMIN_QUEUE_FULL;
  666. goto asq_send_command_error;
  667. }
  668. /* initialize the temp desc pointer with the right desc */
  669. desc_on_ring = I40E_ADMINQ_DESC(hw->aq.asq, hw->aq.asq.next_to_use);
  670. /* if the desc is available copy the temp desc to the right place */
  671. *desc_on_ring = *desc;
  672. /* if buff is not NULL assume indirect command */
  673. if (buff != NULL) {
  674. dma_buff = &(hw->aq.asq.r.asq_bi[hw->aq.asq.next_to_use]);
  675. /* copy the user buff into the respective DMA buff */
  676. memcpy(dma_buff->va, buff, buff_size);
  677. desc_on_ring->datalen = cpu_to_le16(buff_size);
  678. /* Update the address values in the desc with the pa value
  679. * for respective buffer
  680. */
  681. desc_on_ring->params.external.addr_high =
  682. cpu_to_le32(upper_32_bits(dma_buff->pa));
  683. desc_on_ring->params.external.addr_low =
  684. cpu_to_le32(lower_32_bits(dma_buff->pa));
  685. }
  686. /* bump the tail */
  687. i40e_debug_aq(hw, I40E_DEBUG_AQ_COMMAND, (void *)desc_on_ring, buff);
  688. (hw->aq.asq.next_to_use)++;
  689. if (hw->aq.asq.next_to_use == hw->aq.asq.count)
  690. hw->aq.asq.next_to_use = 0;
  691. if (!details->postpone)
  692. wr32(hw, hw->aq.asq.tail, hw->aq.asq.next_to_use);
  693. /* if cmd_details are not defined or async flag is not set,
  694. * we need to wait for desc write back
  695. */
  696. if (!details->async && !details->postpone) {
  697. u32 total_delay = 0;
  698. u32 delay_len = 10;
  699. do {
  700. /* AQ designers suggest use of head for better
  701. * timing reliability than DD bit
  702. */
  703. if (i40e_asq_done(hw))
  704. break;
  705. /* ugh! delay while spin_lock */
  706. udelay(delay_len);
  707. total_delay += delay_len;
  708. } while (total_delay < I40E_ASQ_CMD_TIMEOUT);
  709. }
  710. /* if ready, copy the desc back to temp */
  711. if (i40e_asq_done(hw)) {
  712. *desc = *desc_on_ring;
  713. if (buff != NULL)
  714. memcpy(buff, dma_buff->va, buff_size);
  715. retval = le16_to_cpu(desc->retval);
  716. if (retval != 0) {
  717. i40e_debug(hw,
  718. I40E_DEBUG_AQ_MESSAGE,
  719. "AQTX: Command completed with error 0x%X.\n",
  720. retval);
  721. /* strip off FW internal code */
  722. retval &= 0xff;
  723. }
  724. cmd_completed = true;
  725. if ((enum i40e_admin_queue_err)retval == I40E_AQ_RC_OK)
  726. status = 0;
  727. else
  728. status = I40E_ERR_ADMIN_QUEUE_ERROR;
  729. hw->aq.asq_last_status = (enum i40e_admin_queue_err)retval;
  730. }
  731. /* update the error if time out occurred */
  732. if ((!cmd_completed) &&
  733. (!details->async && !details->postpone)) {
  734. i40e_debug(hw,
  735. I40E_DEBUG_AQ_MESSAGE,
  736. "AQTX: Writeback timeout.\n");
  737. status = I40E_ERR_ADMIN_QUEUE_TIMEOUT;
  738. }
  739. asq_send_command_error:
  740. mutex_unlock(&hw->aq.asq_mutex);
  741. asq_send_command_exit:
  742. return status;
  743. }
  744. /**
  745. * i40e_fill_default_direct_cmd_desc - AQ descriptor helper function
  746. * @desc: pointer to the temp descriptor (non DMA mem)
  747. * @opcode: the opcode can be used to decide which flags to turn off or on
  748. *
  749. * Fill the desc with default values
  750. **/
  751. void i40e_fill_default_direct_cmd_desc(struct i40e_aq_desc *desc,
  752. u16 opcode)
  753. {
  754. /* zero out the desc */
  755. memset((void *)desc, 0, sizeof(struct i40e_aq_desc));
  756. desc->opcode = cpu_to_le16(opcode);
  757. desc->flags = cpu_to_le16(I40E_AQ_FLAG_EI | I40E_AQ_FLAG_SI);
  758. }
  759. /**
  760. * i40e_clean_arq_element
  761. * @hw: pointer to the hw struct
  762. * @e: event info from the receive descriptor, includes any buffers
  763. * @pending: number of events that could be left to process
  764. *
  765. * This function cleans one Admin Receive Queue element and returns
  766. * the contents through e. It can also return how many events are
  767. * left to process through 'pending'
  768. **/
  769. i40e_status i40e_clean_arq_element(struct i40e_hw *hw,
  770. struct i40e_arq_event_info *e,
  771. u16 *pending)
  772. {
  773. i40e_status ret_code = 0;
  774. u16 ntc = hw->aq.arq.next_to_clean;
  775. struct i40e_aq_desc *desc;
  776. struct i40e_dma_mem *bi;
  777. u16 desc_idx;
  778. u16 datalen;
  779. u16 flags;
  780. u16 ntu;
  781. /* take the lock before we start messing with the ring */
  782. mutex_lock(&hw->aq.arq_mutex);
  783. /* set next_to_use to head */
  784. ntu = (rd32(hw, hw->aq.arq.head) & I40E_PF_ARQH_ARQH_MASK);
  785. if (ntu == ntc) {
  786. /* nothing to do - shouldn't need to update ring's values */
  787. i40e_debug(hw,
  788. I40E_DEBUG_AQ_MESSAGE,
  789. "AQRX: Queue is empty.\n");
  790. ret_code = I40E_ERR_ADMIN_QUEUE_NO_WORK;
  791. goto clean_arq_element_out;
  792. }
  793. /* now clean the next descriptor */
  794. desc = I40E_ADMINQ_DESC(hw->aq.arq, ntc);
  795. desc_idx = ntc;
  796. i40e_debug_aq(hw,
  797. I40E_DEBUG_AQ_COMMAND,
  798. (void *)desc,
  799. hw->aq.arq.r.arq_bi[desc_idx].va);
  800. flags = le16_to_cpu(desc->flags);
  801. if (flags & I40E_AQ_FLAG_ERR) {
  802. ret_code = I40E_ERR_ADMIN_QUEUE_ERROR;
  803. hw->aq.arq_last_status =
  804. (enum i40e_admin_queue_err)le16_to_cpu(desc->retval);
  805. i40e_debug(hw,
  806. I40E_DEBUG_AQ_MESSAGE,
  807. "AQRX: Event received with error 0x%X.\n",
  808. hw->aq.arq_last_status);
  809. } else {
  810. memcpy(&e->desc, desc, sizeof(struct i40e_aq_desc));
  811. datalen = le16_to_cpu(desc->datalen);
  812. e->msg_size = min(datalen, e->msg_size);
  813. if (e->msg_buf != NULL && (e->msg_size != 0))
  814. memcpy(e->msg_buf, hw->aq.arq.r.arq_bi[desc_idx].va,
  815. e->msg_size);
  816. }
  817. /* Restore the original datalen and buffer address in the desc,
  818. * FW updates datalen to indicate the event message
  819. * size
  820. */
  821. bi = &hw->aq.arq.r.arq_bi[ntc];
  822. desc->datalen = cpu_to_le16((u16)bi->size);
  823. desc->params.external.addr_high = cpu_to_le32(upper_32_bits(bi->pa));
  824. desc->params.external.addr_low = cpu_to_le32(lower_32_bits(bi->pa));
  825. /* set tail = the last cleaned desc index. */
  826. wr32(hw, hw->aq.arq.tail, ntc);
  827. /* ntc is updated to tail + 1 */
  828. ntc++;
  829. if (ntc == hw->aq.num_arq_entries)
  830. ntc = 0;
  831. hw->aq.arq.next_to_clean = ntc;
  832. hw->aq.arq.next_to_use = ntu;
  833. clean_arq_element_out:
  834. /* Set pending if needed, unlock and return */
  835. if (pending != NULL)
  836. *pending = (ntc > ntu ? hw->aq.arq.count : 0) + (ntu - ntc);
  837. mutex_unlock(&hw->aq.arq_mutex);
  838. return ret_code;
  839. }
  840. void i40e_resume_aq(struct i40e_hw *hw)
  841. {
  842. u32 reg = 0;
  843. /* Registers are reset after PF reset */
  844. hw->aq.asq.next_to_use = 0;
  845. hw->aq.asq.next_to_clean = 0;
  846. i40e_config_asq_regs(hw);
  847. reg = hw->aq.num_asq_entries | I40E_PF_ATQLEN_ATQENABLE_MASK;
  848. wr32(hw, hw->aq.asq.len, reg);
  849. hw->aq.arq.next_to_use = 0;
  850. hw->aq.arq.next_to_clean = 0;
  851. i40e_config_arq_regs(hw);
  852. reg = hw->aq.num_arq_entries | I40E_PF_ATQLEN_ATQENABLE_MASK;
  853. wr32(hw, hw->aq.arq.len, reg);
  854. }