i40e_adminq.c 28 KB

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