i40e_adminq.c 28 KB

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