mcdi.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2008-2013 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/atomic.h>
  12. #include "net_driver.h"
  13. #include "nic.h"
  14. #include "io.h"
  15. #include "farch_regs.h"
  16. #include "mcdi_pcol.h"
  17. #include "phy.h"
  18. /**************************************************************************
  19. *
  20. * Management-Controller-to-Driver Interface
  21. *
  22. **************************************************************************
  23. */
  24. #define MCDI_RPC_TIMEOUT (10 * HZ)
  25. /* A reboot/assertion causes the MCDI status word to be set after the
  26. * command word is set or a REBOOT event is sent. If we notice a reboot
  27. * via these mechanisms then wait 250ms for the status word to be set.
  28. */
  29. #define MCDI_STATUS_DELAY_US 100
  30. #define MCDI_STATUS_DELAY_COUNT 2500
  31. #define MCDI_STATUS_SLEEP_MS \
  32. (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
  33. #define SEQ_MASK \
  34. EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
  35. struct efx_mcdi_async_param {
  36. struct list_head list;
  37. unsigned int cmd;
  38. size_t inlen;
  39. size_t outlen;
  40. bool quiet;
  41. efx_mcdi_async_completer *complete;
  42. unsigned long cookie;
  43. /* followed by request/response buffer */
  44. };
  45. static void efx_mcdi_timeout_async(unsigned long context);
  46. static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
  47. bool *was_attached_out);
  48. static bool efx_mcdi_poll_once(struct efx_nic *efx);
  49. static void efx_mcdi_abandon(struct efx_nic *efx);
  50. #ifdef CONFIG_SFC_MCDI_LOGGING
  51. static bool mcdi_logging_default;
  52. module_param(mcdi_logging_default, bool, 0644);
  53. MODULE_PARM_DESC(mcdi_logging_default,
  54. "Enable MCDI logging on newly-probed functions");
  55. #endif
  56. int efx_mcdi_init(struct efx_nic *efx)
  57. {
  58. struct efx_mcdi_iface *mcdi;
  59. bool already_attached;
  60. int rc = -ENOMEM;
  61. efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
  62. if (!efx->mcdi)
  63. goto fail;
  64. mcdi = efx_mcdi(efx);
  65. mcdi->efx = efx;
  66. #ifdef CONFIG_SFC_MCDI_LOGGING
  67. /* consuming code assumes buffer is page-sized */
  68. mcdi->logging_buffer = (char *)__get_free_page(GFP_KERNEL);
  69. if (!mcdi->logging_buffer)
  70. goto fail1;
  71. mcdi->logging_enabled = mcdi_logging_default;
  72. #endif
  73. init_waitqueue_head(&mcdi->wq);
  74. spin_lock_init(&mcdi->iface_lock);
  75. mcdi->state = MCDI_STATE_QUIESCENT;
  76. mcdi->mode = MCDI_MODE_POLL;
  77. spin_lock_init(&mcdi->async_lock);
  78. INIT_LIST_HEAD(&mcdi->async_list);
  79. setup_timer(&mcdi->async_timer, efx_mcdi_timeout_async,
  80. (unsigned long)mcdi);
  81. (void) efx_mcdi_poll_reboot(efx);
  82. mcdi->new_epoch = true;
  83. /* Recover from a failed assertion before probing */
  84. rc = efx_mcdi_handle_assertion(efx);
  85. if (rc)
  86. goto fail2;
  87. /* Let the MC (and BMC, if this is a LOM) know that the driver
  88. * is loaded. We should do this before we reset the NIC.
  89. */
  90. rc = efx_mcdi_drv_attach(efx, true, &already_attached);
  91. if (rc) {
  92. netif_err(efx, probe, efx->net_dev,
  93. "Unable to register driver with MCPU\n");
  94. goto fail2;
  95. }
  96. if (already_attached)
  97. /* Not a fatal error */
  98. netif_err(efx, probe, efx->net_dev,
  99. "Host already registered with MCPU\n");
  100. if (efx->mcdi->fn_flags &
  101. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY))
  102. efx->primary = efx;
  103. return 0;
  104. fail2:
  105. #ifdef CONFIG_SFC_MCDI_LOGGING
  106. free_page((unsigned long)mcdi->logging_buffer);
  107. fail1:
  108. #endif
  109. kfree(efx->mcdi);
  110. efx->mcdi = NULL;
  111. fail:
  112. return rc;
  113. }
  114. void efx_mcdi_fini(struct efx_nic *efx)
  115. {
  116. if (!efx->mcdi)
  117. return;
  118. BUG_ON(efx->mcdi->iface.state != MCDI_STATE_QUIESCENT);
  119. /* Relinquish the device (back to the BMC, if this is a LOM) */
  120. efx_mcdi_drv_attach(efx, false, NULL);
  121. #ifdef CONFIG_SFC_MCDI_LOGGING
  122. free_page((unsigned long)efx->mcdi->iface.logging_buffer);
  123. #endif
  124. kfree(efx->mcdi);
  125. }
  126. static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd,
  127. const efx_dword_t *inbuf, size_t inlen)
  128. {
  129. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  130. #ifdef CONFIG_SFC_MCDI_LOGGING
  131. char *buf = mcdi->logging_buffer; /* page-sized */
  132. #endif
  133. efx_dword_t hdr[2];
  134. size_t hdr_len;
  135. u32 xflags, seqno;
  136. BUG_ON(mcdi->state == MCDI_STATE_QUIESCENT);
  137. /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
  138. spin_lock_bh(&mcdi->iface_lock);
  139. ++mcdi->seqno;
  140. spin_unlock_bh(&mcdi->iface_lock);
  141. seqno = mcdi->seqno & SEQ_MASK;
  142. xflags = 0;
  143. if (mcdi->mode == MCDI_MODE_EVENTS)
  144. xflags |= MCDI_HEADER_XFLAGS_EVREQ;
  145. if (efx->type->mcdi_max_ver == 1) {
  146. /* MCDI v1 */
  147. EFX_POPULATE_DWORD_7(hdr[0],
  148. MCDI_HEADER_RESPONSE, 0,
  149. MCDI_HEADER_RESYNC, 1,
  150. MCDI_HEADER_CODE, cmd,
  151. MCDI_HEADER_DATALEN, inlen,
  152. MCDI_HEADER_SEQ, seqno,
  153. MCDI_HEADER_XFLAGS, xflags,
  154. MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
  155. hdr_len = 4;
  156. } else {
  157. /* MCDI v2 */
  158. BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
  159. EFX_POPULATE_DWORD_7(hdr[0],
  160. MCDI_HEADER_RESPONSE, 0,
  161. MCDI_HEADER_RESYNC, 1,
  162. MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
  163. MCDI_HEADER_DATALEN, 0,
  164. MCDI_HEADER_SEQ, seqno,
  165. MCDI_HEADER_XFLAGS, xflags,
  166. MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
  167. EFX_POPULATE_DWORD_2(hdr[1],
  168. MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd,
  169. MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen);
  170. hdr_len = 8;
  171. }
  172. #ifdef CONFIG_SFC_MCDI_LOGGING
  173. if (mcdi->logging_enabled && !WARN_ON_ONCE(!buf)) {
  174. int bytes = 0;
  175. int i;
  176. /* Lengths should always be a whole number of dwords, so scream
  177. * if they're not.
  178. */
  179. WARN_ON_ONCE(hdr_len % 4);
  180. WARN_ON_ONCE(inlen % 4);
  181. /* We own the logging buffer, as only one MCDI can be in
  182. * progress on a NIC at any one time. So no need for locking.
  183. */
  184. for (i = 0; i < hdr_len / 4 && bytes < PAGE_SIZE; i++)
  185. bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
  186. " %08x", le32_to_cpu(hdr[i].u32[0]));
  187. for (i = 0; i < inlen / 4 && bytes < PAGE_SIZE; i++)
  188. bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
  189. " %08x", le32_to_cpu(inbuf[i].u32[0]));
  190. netif_info(efx, hw, efx->net_dev, "MCDI RPC REQ:%s\n", buf);
  191. }
  192. #endif
  193. efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen);
  194. mcdi->new_epoch = false;
  195. }
  196. static int efx_mcdi_errno(unsigned int mcdi_err)
  197. {
  198. switch (mcdi_err) {
  199. case 0:
  200. return 0;
  201. #define TRANSLATE_ERROR(name) \
  202. case MC_CMD_ERR_ ## name: \
  203. return -name;
  204. TRANSLATE_ERROR(EPERM);
  205. TRANSLATE_ERROR(ENOENT);
  206. TRANSLATE_ERROR(EINTR);
  207. TRANSLATE_ERROR(EAGAIN);
  208. TRANSLATE_ERROR(EACCES);
  209. TRANSLATE_ERROR(EBUSY);
  210. TRANSLATE_ERROR(EINVAL);
  211. TRANSLATE_ERROR(EDEADLK);
  212. TRANSLATE_ERROR(ENOSYS);
  213. TRANSLATE_ERROR(ETIME);
  214. TRANSLATE_ERROR(EALREADY);
  215. TRANSLATE_ERROR(ENOSPC);
  216. #undef TRANSLATE_ERROR
  217. case MC_CMD_ERR_ENOTSUP:
  218. return -EOPNOTSUPP;
  219. case MC_CMD_ERR_ALLOC_FAIL:
  220. return -ENOBUFS;
  221. case MC_CMD_ERR_MAC_EXIST:
  222. return -EADDRINUSE;
  223. default:
  224. return -EPROTO;
  225. }
  226. }
  227. static void efx_mcdi_read_response_header(struct efx_nic *efx)
  228. {
  229. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  230. unsigned int respseq, respcmd, error;
  231. #ifdef CONFIG_SFC_MCDI_LOGGING
  232. char *buf = mcdi->logging_buffer; /* page-sized */
  233. #endif
  234. efx_dword_t hdr;
  235. efx->type->mcdi_read_response(efx, &hdr, 0, 4);
  236. respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ);
  237. respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE);
  238. error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR);
  239. if (respcmd != MC_CMD_V2_EXTN) {
  240. mcdi->resp_hdr_len = 4;
  241. mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
  242. } else {
  243. efx->type->mcdi_read_response(efx, &hdr, 4, 4);
  244. mcdi->resp_hdr_len = 8;
  245. mcdi->resp_data_len =
  246. EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
  247. }
  248. #ifdef CONFIG_SFC_MCDI_LOGGING
  249. if (mcdi->logging_enabled && !WARN_ON_ONCE(!buf)) {
  250. size_t hdr_len, data_len;
  251. int bytes = 0;
  252. int i;
  253. WARN_ON_ONCE(mcdi->resp_hdr_len % 4);
  254. hdr_len = mcdi->resp_hdr_len / 4;
  255. /* MCDI_DECLARE_BUF ensures that underlying buffer is padded
  256. * to dword size, and the MCDI buffer is always dword size
  257. */
  258. data_len = DIV_ROUND_UP(mcdi->resp_data_len, 4);
  259. /* We own the logging buffer, as only one MCDI can be in
  260. * progress on a NIC at any one time. So no need for locking.
  261. */
  262. for (i = 0; i < hdr_len && bytes < PAGE_SIZE; i++) {
  263. efx->type->mcdi_read_response(efx, &hdr, (i * 4), 4);
  264. bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
  265. " %08x", le32_to_cpu(hdr.u32[0]));
  266. }
  267. for (i = 0; i < data_len && bytes < PAGE_SIZE; i++) {
  268. efx->type->mcdi_read_response(efx, &hdr,
  269. mcdi->resp_hdr_len + (i * 4), 4);
  270. bytes += snprintf(buf + bytes, PAGE_SIZE - bytes,
  271. " %08x", le32_to_cpu(hdr.u32[0]));
  272. }
  273. netif_info(efx, hw, efx->net_dev, "MCDI RPC RESP:%s\n", buf);
  274. }
  275. #endif
  276. if (error && mcdi->resp_data_len == 0) {
  277. netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
  278. mcdi->resprc = -EIO;
  279. } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
  280. netif_err(efx, hw, efx->net_dev,
  281. "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
  282. respseq, mcdi->seqno);
  283. mcdi->resprc = -EIO;
  284. } else if (error) {
  285. efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4);
  286. mcdi->resprc =
  287. efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0));
  288. } else {
  289. mcdi->resprc = 0;
  290. }
  291. }
  292. static bool efx_mcdi_poll_once(struct efx_nic *efx)
  293. {
  294. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  295. rmb();
  296. if (!efx->type->mcdi_poll_response(efx))
  297. return false;
  298. spin_lock_bh(&mcdi->iface_lock);
  299. efx_mcdi_read_response_header(efx);
  300. spin_unlock_bh(&mcdi->iface_lock);
  301. return true;
  302. }
  303. static int efx_mcdi_poll(struct efx_nic *efx)
  304. {
  305. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  306. unsigned long time, finish;
  307. unsigned int spins;
  308. int rc;
  309. /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
  310. rc = efx_mcdi_poll_reboot(efx);
  311. if (rc) {
  312. spin_lock_bh(&mcdi->iface_lock);
  313. mcdi->resprc = rc;
  314. mcdi->resp_hdr_len = 0;
  315. mcdi->resp_data_len = 0;
  316. spin_unlock_bh(&mcdi->iface_lock);
  317. return 0;
  318. }
  319. /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
  320. * because generally mcdi responses are fast. After that, back off
  321. * and poll once a jiffy (approximately)
  322. */
  323. spins = TICK_USEC;
  324. finish = jiffies + MCDI_RPC_TIMEOUT;
  325. while (1) {
  326. if (spins != 0) {
  327. --spins;
  328. udelay(1);
  329. } else {
  330. schedule_timeout_uninterruptible(1);
  331. }
  332. time = jiffies;
  333. if (efx_mcdi_poll_once(efx))
  334. break;
  335. if (time_after(time, finish))
  336. return -ETIMEDOUT;
  337. }
  338. /* Return rc=0 like wait_event_timeout() */
  339. return 0;
  340. }
  341. /* Test and clear MC-rebooted flag for this port/function; reset
  342. * software state as necessary.
  343. */
  344. int efx_mcdi_poll_reboot(struct efx_nic *efx)
  345. {
  346. if (!efx->mcdi)
  347. return 0;
  348. return efx->type->mcdi_poll_reboot(efx);
  349. }
  350. static bool efx_mcdi_acquire_async(struct efx_mcdi_iface *mcdi)
  351. {
  352. return cmpxchg(&mcdi->state,
  353. MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_ASYNC) ==
  354. MCDI_STATE_QUIESCENT;
  355. }
  356. static void efx_mcdi_acquire_sync(struct efx_mcdi_iface *mcdi)
  357. {
  358. /* Wait until the interface becomes QUIESCENT and we win the race
  359. * to mark it RUNNING_SYNC.
  360. */
  361. wait_event(mcdi->wq,
  362. cmpxchg(&mcdi->state,
  363. MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_SYNC) ==
  364. MCDI_STATE_QUIESCENT);
  365. }
  366. static int efx_mcdi_await_completion(struct efx_nic *efx)
  367. {
  368. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  369. if (wait_event_timeout(mcdi->wq, mcdi->state == MCDI_STATE_COMPLETED,
  370. MCDI_RPC_TIMEOUT) == 0)
  371. return -ETIMEDOUT;
  372. /* Check if efx_mcdi_set_mode() switched us back to polled completions.
  373. * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
  374. * completed the request first, then we'll just end up completing the
  375. * request again, which is safe.
  376. *
  377. * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
  378. * wait_event_timeout() implicitly provides.
  379. */
  380. if (mcdi->mode == MCDI_MODE_POLL)
  381. return efx_mcdi_poll(efx);
  382. return 0;
  383. }
  384. /* If the interface is RUNNING_SYNC, switch to COMPLETED and wake the
  385. * requester. Return whether this was done. Does not take any locks.
  386. */
  387. static bool efx_mcdi_complete_sync(struct efx_mcdi_iface *mcdi)
  388. {
  389. if (cmpxchg(&mcdi->state,
  390. MCDI_STATE_RUNNING_SYNC, MCDI_STATE_COMPLETED) ==
  391. MCDI_STATE_RUNNING_SYNC) {
  392. wake_up(&mcdi->wq);
  393. return true;
  394. }
  395. return false;
  396. }
  397. static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
  398. {
  399. if (mcdi->mode == MCDI_MODE_EVENTS) {
  400. struct efx_mcdi_async_param *async;
  401. struct efx_nic *efx = mcdi->efx;
  402. /* Process the asynchronous request queue */
  403. spin_lock_bh(&mcdi->async_lock);
  404. async = list_first_entry_or_null(
  405. &mcdi->async_list, struct efx_mcdi_async_param, list);
  406. if (async) {
  407. mcdi->state = MCDI_STATE_RUNNING_ASYNC;
  408. efx_mcdi_send_request(efx, async->cmd,
  409. (const efx_dword_t *)(async + 1),
  410. async->inlen);
  411. mod_timer(&mcdi->async_timer,
  412. jiffies + MCDI_RPC_TIMEOUT);
  413. }
  414. spin_unlock_bh(&mcdi->async_lock);
  415. if (async)
  416. return;
  417. }
  418. mcdi->state = MCDI_STATE_QUIESCENT;
  419. wake_up(&mcdi->wq);
  420. }
  421. /* If the interface is RUNNING_ASYNC, switch to COMPLETED, call the
  422. * asynchronous completion function, and release the interface.
  423. * Return whether this was done. Must be called in bh-disabled
  424. * context. Will take iface_lock and async_lock.
  425. */
  426. static bool efx_mcdi_complete_async(struct efx_mcdi_iface *mcdi, bool timeout)
  427. {
  428. struct efx_nic *efx = mcdi->efx;
  429. struct efx_mcdi_async_param *async;
  430. size_t hdr_len, data_len, err_len;
  431. efx_dword_t *outbuf;
  432. MCDI_DECLARE_BUF_ERR(errbuf);
  433. int rc;
  434. if (cmpxchg(&mcdi->state,
  435. MCDI_STATE_RUNNING_ASYNC, MCDI_STATE_COMPLETED) !=
  436. MCDI_STATE_RUNNING_ASYNC)
  437. return false;
  438. spin_lock(&mcdi->iface_lock);
  439. if (timeout) {
  440. /* Ensure that if the completion event arrives later,
  441. * the seqno check in efx_mcdi_ev_cpl() will fail
  442. */
  443. ++mcdi->seqno;
  444. ++mcdi->credits;
  445. rc = -ETIMEDOUT;
  446. hdr_len = 0;
  447. data_len = 0;
  448. } else {
  449. rc = mcdi->resprc;
  450. hdr_len = mcdi->resp_hdr_len;
  451. data_len = mcdi->resp_data_len;
  452. }
  453. spin_unlock(&mcdi->iface_lock);
  454. /* Stop the timer. In case the timer function is running, we
  455. * must wait for it to return so that there is no possibility
  456. * of it aborting the next request.
  457. */
  458. if (!timeout)
  459. del_timer_sync(&mcdi->async_timer);
  460. spin_lock(&mcdi->async_lock);
  461. async = list_first_entry(&mcdi->async_list,
  462. struct efx_mcdi_async_param, list);
  463. list_del(&async->list);
  464. spin_unlock(&mcdi->async_lock);
  465. outbuf = (efx_dword_t *)(async + 1);
  466. efx->type->mcdi_read_response(efx, outbuf, hdr_len,
  467. min(async->outlen, data_len));
  468. if (!timeout && rc && !async->quiet) {
  469. err_len = min(sizeof(errbuf), data_len);
  470. efx->type->mcdi_read_response(efx, errbuf, hdr_len,
  471. sizeof(errbuf));
  472. efx_mcdi_display_error(efx, async->cmd, async->inlen, errbuf,
  473. err_len, rc);
  474. }
  475. async->complete(efx, async->cookie, rc, outbuf, data_len);
  476. kfree(async);
  477. efx_mcdi_release(mcdi);
  478. return true;
  479. }
  480. static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
  481. unsigned int datalen, unsigned int mcdi_err)
  482. {
  483. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  484. bool wake = false;
  485. spin_lock(&mcdi->iface_lock);
  486. if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
  487. if (mcdi->credits)
  488. /* The request has been cancelled */
  489. --mcdi->credits;
  490. else
  491. netif_err(efx, hw, efx->net_dev,
  492. "MC response mismatch tx seq 0x%x rx "
  493. "seq 0x%x\n", seqno, mcdi->seqno);
  494. } else {
  495. if (efx->type->mcdi_max_ver >= 2) {
  496. /* MCDI v2 responses don't fit in an event */
  497. efx_mcdi_read_response_header(efx);
  498. } else {
  499. mcdi->resprc = efx_mcdi_errno(mcdi_err);
  500. mcdi->resp_hdr_len = 4;
  501. mcdi->resp_data_len = datalen;
  502. }
  503. wake = true;
  504. }
  505. spin_unlock(&mcdi->iface_lock);
  506. if (wake) {
  507. if (!efx_mcdi_complete_async(mcdi, false))
  508. (void) efx_mcdi_complete_sync(mcdi);
  509. /* If the interface isn't RUNNING_ASYNC or
  510. * RUNNING_SYNC then we've received a duplicate
  511. * completion after we've already transitioned back to
  512. * QUIESCENT. [A subsequent invocation would increment
  513. * seqno, so would have failed the seqno check].
  514. */
  515. }
  516. }
  517. static void efx_mcdi_timeout_async(unsigned long context)
  518. {
  519. struct efx_mcdi_iface *mcdi = (struct efx_mcdi_iface *)context;
  520. efx_mcdi_complete_async(mcdi, true);
  521. }
  522. static int
  523. efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen)
  524. {
  525. if (efx->type->mcdi_max_ver < 0 ||
  526. (efx->type->mcdi_max_ver < 2 &&
  527. cmd > MC_CMD_CMD_SPACE_ESCAPE_7))
  528. return -EINVAL;
  529. if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 ||
  530. (efx->type->mcdi_max_ver < 2 &&
  531. inlen > MCDI_CTL_SDU_LEN_MAX_V1))
  532. return -EMSGSIZE;
  533. return 0;
  534. }
  535. static int _efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
  536. efx_dword_t *outbuf, size_t outlen,
  537. size_t *outlen_actual, bool quiet)
  538. {
  539. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  540. MCDI_DECLARE_BUF_ERR(errbuf);
  541. int rc;
  542. if (mcdi->mode == MCDI_MODE_POLL)
  543. rc = efx_mcdi_poll(efx);
  544. else
  545. rc = efx_mcdi_await_completion(efx);
  546. if (rc != 0) {
  547. netif_err(efx, hw, efx->net_dev,
  548. "MC command 0x%x inlen %d mode %d timed out\n",
  549. cmd, (int)inlen, mcdi->mode);
  550. if (mcdi->mode == MCDI_MODE_EVENTS && efx_mcdi_poll_once(efx)) {
  551. netif_err(efx, hw, efx->net_dev,
  552. "MCDI request was completed without an event\n");
  553. rc = 0;
  554. }
  555. efx_mcdi_abandon(efx);
  556. /* Close the race with efx_mcdi_ev_cpl() executing just too late
  557. * and completing a request we've just cancelled, by ensuring
  558. * that the seqno check therein fails.
  559. */
  560. spin_lock_bh(&mcdi->iface_lock);
  561. ++mcdi->seqno;
  562. ++mcdi->credits;
  563. spin_unlock_bh(&mcdi->iface_lock);
  564. }
  565. if (rc != 0) {
  566. if (outlen_actual)
  567. *outlen_actual = 0;
  568. } else {
  569. size_t hdr_len, data_len, err_len;
  570. /* At the very least we need a memory barrier here to ensure
  571. * we pick up changes from efx_mcdi_ev_cpl(). Protect against
  572. * a spurious efx_mcdi_ev_cpl() running concurrently by
  573. * acquiring the iface_lock. */
  574. spin_lock_bh(&mcdi->iface_lock);
  575. rc = mcdi->resprc;
  576. hdr_len = mcdi->resp_hdr_len;
  577. data_len = mcdi->resp_data_len;
  578. err_len = min(sizeof(errbuf), data_len);
  579. spin_unlock_bh(&mcdi->iface_lock);
  580. BUG_ON(rc > 0);
  581. efx->type->mcdi_read_response(efx, outbuf, hdr_len,
  582. min(outlen, data_len));
  583. if (outlen_actual)
  584. *outlen_actual = data_len;
  585. efx->type->mcdi_read_response(efx, errbuf, hdr_len, err_len);
  586. if (cmd == MC_CMD_REBOOT && rc == -EIO) {
  587. /* Don't reset if MC_CMD_REBOOT returns EIO */
  588. } else if (rc == -EIO || rc == -EINTR) {
  589. netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
  590. -rc);
  591. efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
  592. } else if (rc && !quiet) {
  593. efx_mcdi_display_error(efx, cmd, inlen, errbuf, err_len,
  594. rc);
  595. }
  596. if (rc == -EIO || rc == -EINTR) {
  597. msleep(MCDI_STATUS_SLEEP_MS);
  598. efx_mcdi_poll_reboot(efx);
  599. mcdi->new_epoch = true;
  600. }
  601. }
  602. efx_mcdi_release(mcdi);
  603. return rc;
  604. }
  605. static int _efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
  606. const efx_dword_t *inbuf, size_t inlen,
  607. efx_dword_t *outbuf, size_t outlen,
  608. size_t *outlen_actual, bool quiet)
  609. {
  610. int rc;
  611. rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
  612. if (rc) {
  613. if (outlen_actual)
  614. *outlen_actual = 0;
  615. return rc;
  616. }
  617. return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
  618. outlen_actual, quiet);
  619. }
  620. int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
  621. const efx_dword_t *inbuf, size_t inlen,
  622. efx_dword_t *outbuf, size_t outlen,
  623. size_t *outlen_actual)
  624. {
  625. return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
  626. outlen_actual, false);
  627. }
  628. /* Normally, on receiving an error code in the MCDI response,
  629. * efx_mcdi_rpc will log an error message containing (among other
  630. * things) the raw error code, by means of efx_mcdi_display_error.
  631. * This _quiet version suppresses that; if the caller wishes to log
  632. * the error conditionally on the return code, it should call this
  633. * function and is then responsible for calling efx_mcdi_display_error
  634. * as needed.
  635. */
  636. int efx_mcdi_rpc_quiet(struct efx_nic *efx, unsigned cmd,
  637. const efx_dword_t *inbuf, size_t inlen,
  638. efx_dword_t *outbuf, size_t outlen,
  639. size_t *outlen_actual)
  640. {
  641. return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
  642. outlen_actual, true);
  643. }
  644. int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
  645. const efx_dword_t *inbuf, size_t inlen)
  646. {
  647. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  648. int rc;
  649. rc = efx_mcdi_check_supported(efx, cmd, inlen);
  650. if (rc)
  651. return rc;
  652. if (efx->mc_bist_for_other_fn)
  653. return -ENETDOWN;
  654. if (mcdi->mode == MCDI_MODE_FAIL)
  655. return -ENETDOWN;
  656. efx_mcdi_acquire_sync(mcdi);
  657. efx_mcdi_send_request(efx, cmd, inbuf, inlen);
  658. return 0;
  659. }
  660. static int _efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
  661. const efx_dword_t *inbuf, size_t inlen,
  662. size_t outlen,
  663. efx_mcdi_async_completer *complete,
  664. unsigned long cookie, bool quiet)
  665. {
  666. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  667. struct efx_mcdi_async_param *async;
  668. int rc;
  669. rc = efx_mcdi_check_supported(efx, cmd, inlen);
  670. if (rc)
  671. return rc;
  672. if (efx->mc_bist_for_other_fn)
  673. return -ENETDOWN;
  674. async = kmalloc(sizeof(*async) + ALIGN(max(inlen, outlen), 4),
  675. GFP_ATOMIC);
  676. if (!async)
  677. return -ENOMEM;
  678. async->cmd = cmd;
  679. async->inlen = inlen;
  680. async->outlen = outlen;
  681. async->quiet = quiet;
  682. async->complete = complete;
  683. async->cookie = cookie;
  684. memcpy(async + 1, inbuf, inlen);
  685. spin_lock_bh(&mcdi->async_lock);
  686. if (mcdi->mode == MCDI_MODE_EVENTS) {
  687. list_add_tail(&async->list, &mcdi->async_list);
  688. /* If this is at the front of the queue, try to start it
  689. * immediately
  690. */
  691. if (mcdi->async_list.next == &async->list &&
  692. efx_mcdi_acquire_async(mcdi)) {
  693. efx_mcdi_send_request(efx, cmd, inbuf, inlen);
  694. mod_timer(&mcdi->async_timer,
  695. jiffies + MCDI_RPC_TIMEOUT);
  696. }
  697. } else {
  698. kfree(async);
  699. rc = -ENETDOWN;
  700. }
  701. spin_unlock_bh(&mcdi->async_lock);
  702. return rc;
  703. }
  704. /**
  705. * efx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
  706. * @efx: NIC through which to issue the command
  707. * @cmd: Command type number
  708. * @inbuf: Command parameters
  709. * @inlen: Length of command parameters, in bytes
  710. * @outlen: Length to allocate for response buffer, in bytes
  711. * @complete: Function to be called on completion or cancellation.
  712. * @cookie: Arbitrary value to be passed to @complete.
  713. *
  714. * This function does not sleep and therefore may be called in atomic
  715. * context. It will fail if event queues are disabled or if MCDI
  716. * event completions have been disabled due to an error.
  717. *
  718. * If it succeeds, the @complete function will be called exactly once
  719. * in atomic context, when one of the following occurs:
  720. * (a) the completion event is received (in NAPI context)
  721. * (b) event queues are disabled (in the process that disables them)
  722. * (c) the request times-out (in timer context)
  723. */
  724. int
  725. efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
  726. const efx_dword_t *inbuf, size_t inlen, size_t outlen,
  727. efx_mcdi_async_completer *complete, unsigned long cookie)
  728. {
  729. return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
  730. cookie, false);
  731. }
  732. int efx_mcdi_rpc_async_quiet(struct efx_nic *efx, unsigned int cmd,
  733. const efx_dword_t *inbuf, size_t inlen,
  734. size_t outlen, efx_mcdi_async_completer *complete,
  735. unsigned long cookie)
  736. {
  737. return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
  738. cookie, true);
  739. }
  740. int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
  741. efx_dword_t *outbuf, size_t outlen,
  742. size_t *outlen_actual)
  743. {
  744. return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
  745. outlen_actual, false);
  746. }
  747. int efx_mcdi_rpc_finish_quiet(struct efx_nic *efx, unsigned cmd, size_t inlen,
  748. efx_dword_t *outbuf, size_t outlen,
  749. size_t *outlen_actual)
  750. {
  751. return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
  752. outlen_actual, true);
  753. }
  754. void efx_mcdi_display_error(struct efx_nic *efx, unsigned cmd,
  755. size_t inlen, efx_dword_t *outbuf,
  756. size_t outlen, int rc)
  757. {
  758. int code = 0, err_arg = 0;
  759. if (outlen >= MC_CMD_ERR_CODE_OFST + 4)
  760. code = MCDI_DWORD(outbuf, ERR_CODE);
  761. if (outlen >= MC_CMD_ERR_ARG_OFST + 4)
  762. err_arg = MCDI_DWORD(outbuf, ERR_ARG);
  763. netif_err(efx, hw, efx->net_dev,
  764. "MC command 0x%x inlen %d failed rc=%d (raw=%d) arg=%d\n",
  765. cmd, (int)inlen, rc, code, err_arg);
  766. }
  767. /* Switch to polled MCDI completions. This can be called in various
  768. * error conditions with various locks held, so it must be lockless.
  769. * Caller is responsible for flushing asynchronous requests later.
  770. */
  771. void efx_mcdi_mode_poll(struct efx_nic *efx)
  772. {
  773. struct efx_mcdi_iface *mcdi;
  774. if (!efx->mcdi)
  775. return;
  776. mcdi = efx_mcdi(efx);
  777. /* If already in polling mode, nothing to do.
  778. * If in fail-fast state, don't switch to polled completion.
  779. * FLR recovery will do that later.
  780. */
  781. if (mcdi->mode == MCDI_MODE_POLL || mcdi->mode == MCDI_MODE_FAIL)
  782. return;
  783. /* We can switch from event completion to polled completion, because
  784. * mcdi requests are always completed in shared memory. We do this by
  785. * switching the mode to POLL'd then completing the request.
  786. * efx_mcdi_await_completion() will then call efx_mcdi_poll().
  787. *
  788. * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
  789. * which efx_mcdi_complete_sync() provides for us.
  790. */
  791. mcdi->mode = MCDI_MODE_POLL;
  792. efx_mcdi_complete_sync(mcdi);
  793. }
  794. /* Flush any running or queued asynchronous requests, after event processing
  795. * is stopped
  796. */
  797. void efx_mcdi_flush_async(struct efx_nic *efx)
  798. {
  799. struct efx_mcdi_async_param *async, *next;
  800. struct efx_mcdi_iface *mcdi;
  801. if (!efx->mcdi)
  802. return;
  803. mcdi = efx_mcdi(efx);
  804. /* We must be in poll or fail mode so no more requests can be queued */
  805. BUG_ON(mcdi->mode == MCDI_MODE_EVENTS);
  806. del_timer_sync(&mcdi->async_timer);
  807. /* If a request is still running, make sure we give the MC
  808. * time to complete it so that the response won't overwrite our
  809. * next request.
  810. */
  811. if (mcdi->state == MCDI_STATE_RUNNING_ASYNC) {
  812. efx_mcdi_poll(efx);
  813. mcdi->state = MCDI_STATE_QUIESCENT;
  814. }
  815. /* Nothing else will access the async list now, so it is safe
  816. * to walk it without holding async_lock. If we hold it while
  817. * calling a completer then lockdep may warn that we have
  818. * acquired locks in the wrong order.
  819. */
  820. list_for_each_entry_safe(async, next, &mcdi->async_list, list) {
  821. async->complete(efx, async->cookie, -ENETDOWN, NULL, 0);
  822. list_del(&async->list);
  823. kfree(async);
  824. }
  825. }
  826. void efx_mcdi_mode_event(struct efx_nic *efx)
  827. {
  828. struct efx_mcdi_iface *mcdi;
  829. if (!efx->mcdi)
  830. return;
  831. mcdi = efx_mcdi(efx);
  832. /* If already in event completion mode, nothing to do.
  833. * If in fail-fast state, don't switch to event completion. FLR
  834. * recovery will do that later.
  835. */
  836. if (mcdi->mode == MCDI_MODE_EVENTS || mcdi->mode == MCDI_MODE_FAIL)
  837. return;
  838. /* We can't switch from polled to event completion in the middle of a
  839. * request, because the completion method is specified in the request.
  840. * So acquire the interface to serialise the requestors. We don't need
  841. * to acquire the iface_lock to change the mode here, but we do need a
  842. * write memory barrier ensure that efx_mcdi_rpc() sees it, which
  843. * efx_mcdi_acquire() provides.
  844. */
  845. efx_mcdi_acquire_sync(mcdi);
  846. mcdi->mode = MCDI_MODE_EVENTS;
  847. efx_mcdi_release(mcdi);
  848. }
  849. static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
  850. {
  851. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  852. /* If there is an outstanding MCDI request, it has been terminated
  853. * either by a BADASSERT or REBOOT event. If the mcdi interface is
  854. * in polled mode, then do nothing because the MC reboot handler will
  855. * set the header correctly. However, if the mcdi interface is waiting
  856. * for a CMDDONE event it won't receive it [and since all MCDI events
  857. * are sent to the same queue, we can't be racing with
  858. * efx_mcdi_ev_cpl()]
  859. *
  860. * If there is an outstanding asynchronous request, we can't
  861. * complete it now (efx_mcdi_complete() would deadlock). The
  862. * reset process will take care of this.
  863. *
  864. * There's a race here with efx_mcdi_send_request(), because
  865. * we might receive a REBOOT event *before* the request has
  866. * been copied out. In polled mode (during startup) this is
  867. * irrelevant, because efx_mcdi_complete_sync() is ignored. In
  868. * event mode, this condition is just an edge-case of
  869. * receiving a REBOOT event after posting the MCDI
  870. * request. Did the mc reboot before or after the copyout? The
  871. * best we can do always is just return failure.
  872. */
  873. spin_lock(&mcdi->iface_lock);
  874. if (efx_mcdi_complete_sync(mcdi)) {
  875. if (mcdi->mode == MCDI_MODE_EVENTS) {
  876. mcdi->resprc = rc;
  877. mcdi->resp_hdr_len = 0;
  878. mcdi->resp_data_len = 0;
  879. ++mcdi->credits;
  880. }
  881. } else {
  882. int count;
  883. /* Consume the status word since efx_mcdi_rpc_finish() won't */
  884. for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
  885. rc = efx_mcdi_poll_reboot(efx);
  886. if (rc)
  887. break;
  888. udelay(MCDI_STATUS_DELAY_US);
  889. }
  890. /* On EF10, a CODE_MC_REBOOT event can be received without the
  891. * reboot detection in efx_mcdi_poll_reboot() being triggered.
  892. * If zero was returned from the final call to
  893. * efx_mcdi_poll_reboot(), the MC reboot wasn't noticed but the
  894. * MC has definitely rebooted so prepare for the reset.
  895. */
  896. if (!rc && efx->type->mcdi_reboot_detected)
  897. efx->type->mcdi_reboot_detected(efx);
  898. mcdi->new_epoch = true;
  899. /* Nobody was waiting for an MCDI request, so trigger a reset */
  900. efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
  901. }
  902. spin_unlock(&mcdi->iface_lock);
  903. }
  904. /* The MC is going down in to BIST mode. set the BIST flag to block
  905. * new MCDI, cancel any outstanding MCDI and and schedule a BIST-type reset
  906. * (which doesn't actually execute a reset, it waits for the controlling
  907. * function to reset it).
  908. */
  909. static void efx_mcdi_ev_bist(struct efx_nic *efx)
  910. {
  911. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  912. spin_lock(&mcdi->iface_lock);
  913. efx->mc_bist_for_other_fn = true;
  914. if (efx_mcdi_complete_sync(mcdi)) {
  915. if (mcdi->mode == MCDI_MODE_EVENTS) {
  916. mcdi->resprc = -EIO;
  917. mcdi->resp_hdr_len = 0;
  918. mcdi->resp_data_len = 0;
  919. ++mcdi->credits;
  920. }
  921. }
  922. mcdi->new_epoch = true;
  923. efx_schedule_reset(efx, RESET_TYPE_MC_BIST);
  924. spin_unlock(&mcdi->iface_lock);
  925. }
  926. /* MCDI timeouts seen, so make all MCDI calls fail-fast and issue an FLR to try
  927. * to recover.
  928. */
  929. static void efx_mcdi_abandon(struct efx_nic *efx)
  930. {
  931. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  932. if (xchg(&mcdi->mode, MCDI_MODE_FAIL) == MCDI_MODE_FAIL)
  933. return; /* it had already been done */
  934. netif_dbg(efx, hw, efx->net_dev, "MCDI is timing out; trying to recover\n");
  935. efx_schedule_reset(efx, RESET_TYPE_MCDI_TIMEOUT);
  936. }
  937. /* Called from falcon_process_eventq for MCDI events */
  938. void efx_mcdi_process_event(struct efx_channel *channel,
  939. efx_qword_t *event)
  940. {
  941. struct efx_nic *efx = channel->efx;
  942. int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
  943. u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
  944. switch (code) {
  945. case MCDI_EVENT_CODE_BADSSERT:
  946. netif_err(efx, hw, efx->net_dev,
  947. "MC watchdog or assertion failure at 0x%x\n", data);
  948. efx_mcdi_ev_death(efx, -EINTR);
  949. break;
  950. case MCDI_EVENT_CODE_PMNOTICE:
  951. netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
  952. break;
  953. case MCDI_EVENT_CODE_CMDDONE:
  954. efx_mcdi_ev_cpl(efx,
  955. MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
  956. MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
  957. MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
  958. break;
  959. case MCDI_EVENT_CODE_LINKCHANGE:
  960. efx_mcdi_process_link_change(efx, event);
  961. break;
  962. case MCDI_EVENT_CODE_SENSOREVT:
  963. efx_mcdi_sensor_event(efx, event);
  964. break;
  965. case MCDI_EVENT_CODE_SCHEDERR:
  966. netif_dbg(efx, hw, efx->net_dev,
  967. "MC Scheduler alert (0x%x)\n", data);
  968. break;
  969. case MCDI_EVENT_CODE_REBOOT:
  970. case MCDI_EVENT_CODE_MC_REBOOT:
  971. netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
  972. efx_mcdi_ev_death(efx, -EIO);
  973. break;
  974. case MCDI_EVENT_CODE_MC_BIST:
  975. netif_info(efx, hw, efx->net_dev, "MC entered BIST mode\n");
  976. efx_mcdi_ev_bist(efx);
  977. break;
  978. case MCDI_EVENT_CODE_MAC_STATS_DMA:
  979. /* MAC stats are gather lazily. We can ignore this. */
  980. break;
  981. case MCDI_EVENT_CODE_FLR:
  982. if (efx->type->sriov_flr)
  983. efx->type->sriov_flr(efx,
  984. MCDI_EVENT_FIELD(*event, FLR_VF));
  985. break;
  986. case MCDI_EVENT_CODE_PTP_RX:
  987. case MCDI_EVENT_CODE_PTP_FAULT:
  988. case MCDI_EVENT_CODE_PTP_PPS:
  989. efx_ptp_event(efx, event);
  990. break;
  991. case MCDI_EVENT_CODE_PTP_TIME:
  992. efx_time_sync_event(channel, event);
  993. break;
  994. case MCDI_EVENT_CODE_TX_FLUSH:
  995. case MCDI_EVENT_CODE_RX_FLUSH:
  996. /* Two flush events will be sent: one to the same event
  997. * queue as completions, and one to event queue 0.
  998. * In the latter case the {RX,TX}_FLUSH_TO_DRIVER
  999. * flag will be set, and we should ignore the event
  1000. * because we want to wait for all completions.
  1001. */
  1002. BUILD_BUG_ON(MCDI_EVENT_TX_FLUSH_TO_DRIVER_LBN !=
  1003. MCDI_EVENT_RX_FLUSH_TO_DRIVER_LBN);
  1004. if (!MCDI_EVENT_FIELD(*event, TX_FLUSH_TO_DRIVER))
  1005. efx_ef10_handle_drain_event(efx);
  1006. break;
  1007. case MCDI_EVENT_CODE_TX_ERR:
  1008. case MCDI_EVENT_CODE_RX_ERR:
  1009. netif_err(efx, hw, efx->net_dev,
  1010. "%s DMA error (event: "EFX_QWORD_FMT")\n",
  1011. code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX",
  1012. EFX_QWORD_VAL(*event));
  1013. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  1014. break;
  1015. default:
  1016. netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
  1017. code);
  1018. }
  1019. }
  1020. /**************************************************************************
  1021. *
  1022. * Specific request functions
  1023. *
  1024. **************************************************************************
  1025. */
  1026. void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
  1027. {
  1028. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_VERSION_OUT_LEN);
  1029. size_t outlength;
  1030. const __le16 *ver_words;
  1031. size_t offset;
  1032. int rc;
  1033. BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
  1034. rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
  1035. outbuf, sizeof(outbuf), &outlength);
  1036. if (rc)
  1037. goto fail;
  1038. if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
  1039. rc = -EIO;
  1040. goto fail;
  1041. }
  1042. ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
  1043. offset = snprintf(buf, len, "%u.%u.%u.%u",
  1044. le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
  1045. le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
  1046. /* EF10 may have multiple datapath firmware variants within a
  1047. * single version. Report which variants are running.
  1048. */
  1049. if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
  1050. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1051. offset += snprintf(buf + offset, len - offset, " rx%x tx%x",
  1052. nic_data->rx_dpcpu_fw_id,
  1053. nic_data->tx_dpcpu_fw_id);
  1054. /* It's theoretically possible for the string to exceed 31
  1055. * characters, though in practice the first three version
  1056. * components are short enough that this doesn't happen.
  1057. */
  1058. if (WARN_ON(offset >= len))
  1059. buf[0] = 0;
  1060. }
  1061. return;
  1062. fail:
  1063. netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1064. buf[0] = 0;
  1065. }
  1066. static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
  1067. bool *was_attached)
  1068. {
  1069. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
  1070. MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_EXT_OUT_LEN);
  1071. size_t outlen;
  1072. int rc;
  1073. MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
  1074. driver_operating ? 1 : 0);
  1075. MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
  1076. MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY);
  1077. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
  1078. outbuf, sizeof(outbuf), &outlen);
  1079. /* If we're not the primary PF, trying to ATTACH with a FIRMWARE_ID
  1080. * specified will fail with EPERM, and we have to tell the MC we don't
  1081. * care what firmware we get.
  1082. */
  1083. if (rc == -EPERM) {
  1084. netif_dbg(efx, probe, efx->net_dev,
  1085. "efx_mcdi_drv_attach with fw-variant setting failed EPERM, trying without it\n");
  1086. MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID,
  1087. MC_CMD_FW_DONT_CARE);
  1088. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_DRV_ATTACH, inbuf,
  1089. sizeof(inbuf), outbuf, sizeof(outbuf),
  1090. &outlen);
  1091. }
  1092. if (rc) {
  1093. efx_mcdi_display_error(efx, MC_CMD_DRV_ATTACH, sizeof(inbuf),
  1094. outbuf, outlen, rc);
  1095. goto fail;
  1096. }
  1097. if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
  1098. rc = -EIO;
  1099. goto fail;
  1100. }
  1101. if (driver_operating) {
  1102. if (outlen >= MC_CMD_DRV_ATTACH_EXT_OUT_LEN) {
  1103. efx->mcdi->fn_flags =
  1104. MCDI_DWORD(outbuf,
  1105. DRV_ATTACH_EXT_OUT_FUNC_FLAGS);
  1106. } else {
  1107. /* Synthesise flags for Siena */
  1108. efx->mcdi->fn_flags =
  1109. 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
  1110. 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED |
  1111. (efx_port_num(efx) == 0) <<
  1112. MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY;
  1113. }
  1114. }
  1115. /* We currently assume we have control of the external link
  1116. * and are completely trusted by firmware. Abort probing
  1117. * if that's not true for this function.
  1118. */
  1119. if (was_attached != NULL)
  1120. *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
  1121. return 0;
  1122. fail:
  1123. netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1124. return rc;
  1125. }
  1126. int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
  1127. u16 *fw_subtype_list, u32 *capabilities)
  1128. {
  1129. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
  1130. size_t outlen, i;
  1131. int port_num = efx_port_num(efx);
  1132. int rc;
  1133. BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
  1134. /* we need __aligned(2) for ether_addr_copy */
  1135. BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST & 1);
  1136. BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST & 1);
  1137. rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
  1138. outbuf, sizeof(outbuf), &outlen);
  1139. if (rc)
  1140. goto fail;
  1141. if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
  1142. rc = -EIO;
  1143. goto fail;
  1144. }
  1145. if (mac_address)
  1146. ether_addr_copy(mac_address,
  1147. port_num ?
  1148. MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
  1149. MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0));
  1150. if (fw_subtype_list) {
  1151. for (i = 0;
  1152. i < MCDI_VAR_ARRAY_LEN(outlen,
  1153. GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
  1154. i++)
  1155. fw_subtype_list[i] = MCDI_ARRAY_WORD(
  1156. outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
  1157. for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
  1158. fw_subtype_list[i] = 0;
  1159. }
  1160. if (capabilities) {
  1161. if (port_num)
  1162. *capabilities = MCDI_DWORD(outbuf,
  1163. GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
  1164. else
  1165. *capabilities = MCDI_DWORD(outbuf,
  1166. GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
  1167. }
  1168. return 0;
  1169. fail:
  1170. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
  1171. __func__, rc, (int)outlen);
  1172. return rc;
  1173. }
  1174. int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
  1175. {
  1176. MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
  1177. u32 dest = 0;
  1178. int rc;
  1179. if (uart)
  1180. dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
  1181. if (evq)
  1182. dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
  1183. MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
  1184. MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
  1185. BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
  1186. rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
  1187. NULL, 0, NULL);
  1188. return rc;
  1189. }
  1190. int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
  1191. {
  1192. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
  1193. size_t outlen;
  1194. int rc;
  1195. BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
  1196. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
  1197. outbuf, sizeof(outbuf), &outlen);
  1198. if (rc)
  1199. goto fail;
  1200. if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
  1201. rc = -EIO;
  1202. goto fail;
  1203. }
  1204. *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
  1205. return 0;
  1206. fail:
  1207. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
  1208. __func__, rc);
  1209. return rc;
  1210. }
  1211. int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
  1212. size_t *size_out, size_t *erase_size_out,
  1213. bool *protected_out)
  1214. {
  1215. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
  1216. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
  1217. size_t outlen;
  1218. int rc;
  1219. MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
  1220. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
  1221. outbuf, sizeof(outbuf), &outlen);
  1222. if (rc)
  1223. goto fail;
  1224. if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
  1225. rc = -EIO;
  1226. goto fail;
  1227. }
  1228. *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
  1229. *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
  1230. *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
  1231. (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
  1232. return 0;
  1233. fail:
  1234. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1235. return rc;
  1236. }
  1237. static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
  1238. {
  1239. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
  1240. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
  1241. int rc;
  1242. MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
  1243. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
  1244. outbuf, sizeof(outbuf), NULL);
  1245. if (rc)
  1246. return rc;
  1247. switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
  1248. case MC_CMD_NVRAM_TEST_PASS:
  1249. case MC_CMD_NVRAM_TEST_NOTSUPP:
  1250. return 0;
  1251. default:
  1252. return -EIO;
  1253. }
  1254. }
  1255. int efx_mcdi_nvram_test_all(struct efx_nic *efx)
  1256. {
  1257. u32 nvram_types;
  1258. unsigned int type;
  1259. int rc;
  1260. rc = efx_mcdi_nvram_types(efx, &nvram_types);
  1261. if (rc)
  1262. goto fail1;
  1263. type = 0;
  1264. while (nvram_types != 0) {
  1265. if (nvram_types & 1) {
  1266. rc = efx_mcdi_nvram_test(efx, type);
  1267. if (rc)
  1268. goto fail2;
  1269. }
  1270. type++;
  1271. nvram_types >>= 1;
  1272. }
  1273. return 0;
  1274. fail2:
  1275. netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
  1276. __func__, type);
  1277. fail1:
  1278. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1279. return rc;
  1280. }
  1281. /* Returns 1 if an assertion was read, 0 if no assertion had fired,
  1282. * negative on error.
  1283. */
  1284. static int efx_mcdi_read_assertion(struct efx_nic *efx)
  1285. {
  1286. MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
  1287. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
  1288. unsigned int flags, index;
  1289. const char *reason;
  1290. size_t outlen;
  1291. int retry;
  1292. int rc;
  1293. /* Attempt to read any stored assertion state before we reboot
  1294. * the mcfw out of the assertion handler. Retry twice, once
  1295. * because a boot-time assertion might cause this command to fail
  1296. * with EINTR. And once again because GET_ASSERTS can race with
  1297. * MC_CMD_REBOOT running on the other port. */
  1298. retry = 2;
  1299. do {
  1300. MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
  1301. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_ASSERTS,
  1302. inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
  1303. outbuf, sizeof(outbuf), &outlen);
  1304. if (rc == -EPERM)
  1305. return 0;
  1306. } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
  1307. if (rc) {
  1308. efx_mcdi_display_error(efx, MC_CMD_GET_ASSERTS,
  1309. MC_CMD_GET_ASSERTS_IN_LEN, outbuf,
  1310. outlen, rc);
  1311. return rc;
  1312. }
  1313. if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
  1314. return -EIO;
  1315. /* Print out any recorded assertion state */
  1316. flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
  1317. if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
  1318. return 0;
  1319. reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
  1320. ? "system-level assertion"
  1321. : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
  1322. ? "thread-level assertion"
  1323. : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
  1324. ? "watchdog reset"
  1325. : "unknown assertion";
  1326. netif_err(efx, hw, efx->net_dev,
  1327. "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
  1328. MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
  1329. MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
  1330. /* Print out the registers */
  1331. for (index = 0;
  1332. index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
  1333. index++)
  1334. netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
  1335. 1 + index,
  1336. MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
  1337. index));
  1338. return 1;
  1339. }
  1340. static int efx_mcdi_exit_assertion(struct efx_nic *efx)
  1341. {
  1342. MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
  1343. int rc;
  1344. /* If the MC is running debug firmware, it might now be
  1345. * waiting for a debugger to attach, but we just want it to
  1346. * reboot. We set a flag that makes the command a no-op if it
  1347. * has already done so.
  1348. * The MCDI will thus return either 0 or -EIO.
  1349. */
  1350. BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
  1351. MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
  1352. MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
  1353. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
  1354. NULL, 0, NULL);
  1355. if (rc == -EIO)
  1356. rc = 0;
  1357. if (rc)
  1358. efx_mcdi_display_error(efx, MC_CMD_REBOOT, MC_CMD_REBOOT_IN_LEN,
  1359. NULL, 0, rc);
  1360. return rc;
  1361. }
  1362. int efx_mcdi_handle_assertion(struct efx_nic *efx)
  1363. {
  1364. int rc;
  1365. rc = efx_mcdi_read_assertion(efx);
  1366. if (rc <= 0)
  1367. return rc;
  1368. return efx_mcdi_exit_assertion(efx);
  1369. }
  1370. void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
  1371. {
  1372. MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
  1373. int rc;
  1374. BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
  1375. BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
  1376. BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
  1377. BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
  1378. MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
  1379. rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
  1380. NULL, 0, NULL);
  1381. }
  1382. static int efx_mcdi_reset_func(struct efx_nic *efx)
  1383. {
  1384. MCDI_DECLARE_BUF(inbuf, MC_CMD_ENTITY_RESET_IN_LEN);
  1385. int rc;
  1386. BUILD_BUG_ON(MC_CMD_ENTITY_RESET_OUT_LEN != 0);
  1387. MCDI_POPULATE_DWORD_1(inbuf, ENTITY_RESET_IN_FLAG,
  1388. ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
  1389. rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, inbuf, sizeof(inbuf),
  1390. NULL, 0, NULL);
  1391. return rc;
  1392. }
  1393. static int efx_mcdi_reset_mc(struct efx_nic *efx)
  1394. {
  1395. MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
  1396. int rc;
  1397. BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
  1398. MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
  1399. rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
  1400. NULL, 0, NULL);
  1401. /* White is black, and up is down */
  1402. if (rc == -EIO)
  1403. return 0;
  1404. if (rc == 0)
  1405. rc = -EIO;
  1406. return rc;
  1407. }
  1408. enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
  1409. {
  1410. return RESET_TYPE_RECOVER_OR_ALL;
  1411. }
  1412. int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
  1413. {
  1414. int rc;
  1415. /* If MCDI is down, we can't handle_assertion */
  1416. if (method == RESET_TYPE_MCDI_TIMEOUT) {
  1417. rc = pci_reset_function(efx->pci_dev);
  1418. if (rc)
  1419. return rc;
  1420. /* Re-enable polled MCDI completion */
  1421. if (efx->mcdi) {
  1422. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  1423. mcdi->mode = MCDI_MODE_POLL;
  1424. }
  1425. return 0;
  1426. }
  1427. /* Recover from a failed assertion pre-reset */
  1428. rc = efx_mcdi_handle_assertion(efx);
  1429. if (rc)
  1430. return rc;
  1431. if (method == RESET_TYPE_DATAPATH)
  1432. return 0;
  1433. else if (method == RESET_TYPE_WORLD)
  1434. return efx_mcdi_reset_mc(efx);
  1435. else
  1436. return efx_mcdi_reset_func(efx);
  1437. }
  1438. static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
  1439. const u8 *mac, int *id_out)
  1440. {
  1441. MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
  1442. MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
  1443. size_t outlen;
  1444. int rc;
  1445. MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
  1446. MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
  1447. MC_CMD_FILTER_MODE_SIMPLE);
  1448. ether_addr_copy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac);
  1449. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
  1450. outbuf, sizeof(outbuf), &outlen);
  1451. if (rc)
  1452. goto fail;
  1453. if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
  1454. rc = -EIO;
  1455. goto fail;
  1456. }
  1457. *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
  1458. return 0;
  1459. fail:
  1460. *id_out = -1;
  1461. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1462. return rc;
  1463. }
  1464. int
  1465. efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
  1466. {
  1467. return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
  1468. }
  1469. int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
  1470. {
  1471. MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
  1472. size_t outlen;
  1473. int rc;
  1474. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
  1475. outbuf, sizeof(outbuf), &outlen);
  1476. if (rc)
  1477. goto fail;
  1478. if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
  1479. rc = -EIO;
  1480. goto fail;
  1481. }
  1482. *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
  1483. return 0;
  1484. fail:
  1485. *id_out = -1;
  1486. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1487. return rc;
  1488. }
  1489. int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
  1490. {
  1491. MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
  1492. int rc;
  1493. MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
  1494. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
  1495. NULL, 0, NULL);
  1496. return rc;
  1497. }
  1498. int efx_mcdi_flush_rxqs(struct efx_nic *efx)
  1499. {
  1500. struct efx_channel *channel;
  1501. struct efx_rx_queue *rx_queue;
  1502. MCDI_DECLARE_BUF(inbuf,
  1503. MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
  1504. int rc, count;
  1505. BUILD_BUG_ON(EFX_MAX_CHANNELS >
  1506. MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
  1507. count = 0;
  1508. efx_for_each_channel(channel, efx) {
  1509. efx_for_each_channel_rx_queue(rx_queue, channel) {
  1510. if (rx_queue->flush_pending) {
  1511. rx_queue->flush_pending = false;
  1512. atomic_dec(&efx->rxq_flush_pending);
  1513. MCDI_SET_ARRAY_DWORD(
  1514. inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
  1515. count, efx_rx_queue_index(rx_queue));
  1516. count++;
  1517. }
  1518. }
  1519. }
  1520. rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
  1521. MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
  1522. WARN_ON(rc < 0);
  1523. return rc;
  1524. }
  1525. int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
  1526. {
  1527. int rc;
  1528. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
  1529. return rc;
  1530. }
  1531. int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled,
  1532. unsigned int *flags)
  1533. {
  1534. MCDI_DECLARE_BUF(inbuf, MC_CMD_WORKAROUND_IN_LEN);
  1535. MCDI_DECLARE_BUF(outbuf, MC_CMD_WORKAROUND_EXT_OUT_LEN);
  1536. size_t outlen;
  1537. int rc;
  1538. BUILD_BUG_ON(MC_CMD_WORKAROUND_OUT_LEN != 0);
  1539. MCDI_SET_DWORD(inbuf, WORKAROUND_IN_TYPE, type);
  1540. MCDI_SET_DWORD(inbuf, WORKAROUND_IN_ENABLED, enabled);
  1541. rc = efx_mcdi_rpc(efx, MC_CMD_WORKAROUND, inbuf, sizeof(inbuf),
  1542. outbuf, sizeof(outbuf), &outlen);
  1543. if (rc)
  1544. return rc;
  1545. if (!flags)
  1546. return 0;
  1547. if (outlen >= MC_CMD_WORKAROUND_EXT_OUT_LEN)
  1548. *flags = MCDI_DWORD(outbuf, WORKAROUND_EXT_OUT_FLAGS);
  1549. else
  1550. *flags = 0;
  1551. return 0;
  1552. }
  1553. int efx_mcdi_get_workarounds(struct efx_nic *efx, unsigned int *impl_out,
  1554. unsigned int *enabled_out)
  1555. {
  1556. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_WORKAROUNDS_OUT_LEN);
  1557. size_t outlen;
  1558. int rc;
  1559. rc = efx_mcdi_rpc(efx, MC_CMD_GET_WORKAROUNDS, NULL, 0,
  1560. outbuf, sizeof(outbuf), &outlen);
  1561. if (rc)
  1562. goto fail;
  1563. if (outlen < MC_CMD_GET_WORKAROUNDS_OUT_LEN) {
  1564. rc = -EIO;
  1565. goto fail;
  1566. }
  1567. if (impl_out)
  1568. *impl_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_IMPLEMENTED);
  1569. if (enabled_out)
  1570. *enabled_out = MCDI_DWORD(outbuf, GET_WORKAROUNDS_OUT_ENABLED);
  1571. return 0;
  1572. fail:
  1573. /* Older firmware lacks GET_WORKAROUNDS and this isn't especially
  1574. * terrifying. The call site will have to deal with it though.
  1575. */
  1576. netif_printk(efx, hw, rc == -ENOSYS ? KERN_DEBUG : KERN_ERR,
  1577. efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1578. return rc;
  1579. }
  1580. #ifdef CONFIG_SFC_MTD
  1581. #define EFX_MCDI_NVRAM_LEN_MAX 128
  1582. static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
  1583. {
  1584. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
  1585. int rc;
  1586. MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
  1587. BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
  1588. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
  1589. NULL, 0, NULL);
  1590. return rc;
  1591. }
  1592. static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
  1593. loff_t offset, u8 *buffer, size_t length)
  1594. {
  1595. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
  1596. MCDI_DECLARE_BUF(outbuf,
  1597. MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
  1598. size_t outlen;
  1599. int rc;
  1600. MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
  1601. MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
  1602. MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
  1603. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
  1604. outbuf, sizeof(outbuf), &outlen);
  1605. if (rc)
  1606. return rc;
  1607. memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
  1608. return 0;
  1609. }
  1610. static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
  1611. loff_t offset, const u8 *buffer, size_t length)
  1612. {
  1613. MCDI_DECLARE_BUF(inbuf,
  1614. MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
  1615. int rc;
  1616. MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
  1617. MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
  1618. MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
  1619. memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
  1620. BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
  1621. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
  1622. ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
  1623. NULL, 0, NULL);
  1624. return rc;
  1625. }
  1626. static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
  1627. loff_t offset, size_t length)
  1628. {
  1629. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
  1630. int rc;
  1631. MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
  1632. MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
  1633. MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
  1634. BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
  1635. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
  1636. NULL, 0, NULL);
  1637. return rc;
  1638. }
  1639. static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
  1640. {
  1641. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
  1642. int rc;
  1643. MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
  1644. BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
  1645. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
  1646. NULL, 0, NULL);
  1647. return rc;
  1648. }
  1649. int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
  1650. size_t len, size_t *retlen, u8 *buffer)
  1651. {
  1652. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1653. struct efx_nic *efx = mtd->priv;
  1654. loff_t offset = start;
  1655. loff_t end = min_t(loff_t, start + len, mtd->size);
  1656. size_t chunk;
  1657. int rc = 0;
  1658. while (offset < end) {
  1659. chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
  1660. rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
  1661. buffer, chunk);
  1662. if (rc)
  1663. goto out;
  1664. offset += chunk;
  1665. buffer += chunk;
  1666. }
  1667. out:
  1668. *retlen = offset - start;
  1669. return rc;
  1670. }
  1671. int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
  1672. {
  1673. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1674. struct efx_nic *efx = mtd->priv;
  1675. loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
  1676. loff_t end = min_t(loff_t, start + len, mtd->size);
  1677. size_t chunk = part->common.mtd.erasesize;
  1678. int rc = 0;
  1679. if (!part->updating) {
  1680. rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
  1681. if (rc)
  1682. goto out;
  1683. part->updating = true;
  1684. }
  1685. /* The MCDI interface can in fact do multiple erase blocks at once;
  1686. * but erasing may be slow, so we make multiple calls here to avoid
  1687. * tripping the MCDI RPC timeout. */
  1688. while (offset < end) {
  1689. rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
  1690. chunk);
  1691. if (rc)
  1692. goto out;
  1693. offset += chunk;
  1694. }
  1695. out:
  1696. return rc;
  1697. }
  1698. int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
  1699. size_t len, size_t *retlen, const u8 *buffer)
  1700. {
  1701. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1702. struct efx_nic *efx = mtd->priv;
  1703. loff_t offset = start;
  1704. loff_t end = min_t(loff_t, start + len, mtd->size);
  1705. size_t chunk;
  1706. int rc = 0;
  1707. if (!part->updating) {
  1708. rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
  1709. if (rc)
  1710. goto out;
  1711. part->updating = true;
  1712. }
  1713. while (offset < end) {
  1714. chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
  1715. rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
  1716. buffer, chunk);
  1717. if (rc)
  1718. goto out;
  1719. offset += chunk;
  1720. buffer += chunk;
  1721. }
  1722. out:
  1723. *retlen = offset - start;
  1724. return rc;
  1725. }
  1726. int efx_mcdi_mtd_sync(struct mtd_info *mtd)
  1727. {
  1728. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1729. struct efx_nic *efx = mtd->priv;
  1730. int rc = 0;
  1731. if (part->updating) {
  1732. part->updating = false;
  1733. rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
  1734. }
  1735. return rc;
  1736. }
  1737. void efx_mcdi_mtd_rename(struct efx_mtd_partition *part)
  1738. {
  1739. struct efx_mcdi_mtd_partition *mcdi_part =
  1740. container_of(part, struct efx_mcdi_mtd_partition, common);
  1741. struct efx_nic *efx = part->mtd.priv;
  1742. snprintf(part->name, sizeof(part->name), "%s %s:%02x",
  1743. efx->name, part->type_name, mcdi_part->fw_subtype);
  1744. }
  1745. #endif /* CONFIG_SFC_MTD */