mcdi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2008-2011 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 "net_driver.h"
  11. #include "nic.h"
  12. #include "io.h"
  13. #include "farch_regs.h"
  14. #include "mcdi_pcol.h"
  15. #include "phy.h"
  16. /**************************************************************************
  17. *
  18. * Management-Controller-to-Driver Interface
  19. *
  20. **************************************************************************
  21. */
  22. #define MCDI_RPC_TIMEOUT (10 * HZ)
  23. /* A reboot/assertion causes the MCDI status word to be set after the
  24. * command word is set or a REBOOT event is sent. If we notice a reboot
  25. * via these mechanisms then wait 10ms for the status word to be set. */
  26. #define MCDI_STATUS_DELAY_US 100
  27. #define MCDI_STATUS_DELAY_COUNT 100
  28. #define MCDI_STATUS_SLEEP_MS \
  29. (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
  30. #define SEQ_MASK \
  31. EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
  32. static inline struct efx_mcdi_iface *efx_mcdi(struct efx_nic *efx)
  33. {
  34. EFX_BUG_ON_PARANOID(!efx->mcdi);
  35. return &efx->mcdi->iface;
  36. }
  37. int efx_mcdi_init(struct efx_nic *efx)
  38. {
  39. struct efx_mcdi_iface *mcdi;
  40. efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
  41. if (!efx->mcdi)
  42. return -ENOMEM;
  43. mcdi = efx_mcdi(efx);
  44. init_waitqueue_head(&mcdi->wq);
  45. spin_lock_init(&mcdi->iface_lock);
  46. atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
  47. mcdi->mode = MCDI_MODE_POLL;
  48. (void) efx_mcdi_poll_reboot(efx);
  49. /* Recover from a failed assertion before probing */
  50. return efx_mcdi_handle_assertion(efx);
  51. }
  52. void efx_mcdi_fini(struct efx_nic *efx)
  53. {
  54. BUG_ON(efx->mcdi &&
  55. atomic_read(&efx->mcdi->iface.state) != MCDI_STATE_QUIESCENT);
  56. kfree(efx->mcdi);
  57. }
  58. static void efx_mcdi_copyin(struct efx_nic *efx, unsigned cmd,
  59. const efx_dword_t *inbuf, size_t inlen)
  60. {
  61. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  62. efx_dword_t hdr[2];
  63. size_t hdr_len;
  64. u32 xflags, seqno;
  65. BUG_ON(atomic_read(&mcdi->state) == MCDI_STATE_QUIESCENT);
  66. seqno = mcdi->seqno & SEQ_MASK;
  67. xflags = 0;
  68. if (mcdi->mode == MCDI_MODE_EVENTS)
  69. xflags |= MCDI_HEADER_XFLAGS_EVREQ;
  70. if (efx->type->mcdi_max_ver == 1) {
  71. /* MCDI v1 */
  72. EFX_POPULATE_DWORD_6(hdr[0],
  73. MCDI_HEADER_RESPONSE, 0,
  74. MCDI_HEADER_RESYNC, 1,
  75. MCDI_HEADER_CODE, cmd,
  76. MCDI_HEADER_DATALEN, inlen,
  77. MCDI_HEADER_SEQ, seqno,
  78. MCDI_HEADER_XFLAGS, xflags);
  79. hdr_len = 4;
  80. } else {
  81. /* MCDI v2 */
  82. BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
  83. EFX_POPULATE_DWORD_6(hdr[0],
  84. MCDI_HEADER_RESPONSE, 0,
  85. MCDI_HEADER_RESYNC, 1,
  86. MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
  87. MCDI_HEADER_DATALEN, 0,
  88. MCDI_HEADER_SEQ, seqno,
  89. MCDI_HEADER_XFLAGS, xflags);
  90. EFX_POPULATE_DWORD_2(hdr[1],
  91. MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd,
  92. MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen);
  93. hdr_len = 8;
  94. }
  95. efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen);
  96. }
  97. static int efx_mcdi_errno(unsigned int mcdi_err)
  98. {
  99. switch (mcdi_err) {
  100. case 0:
  101. return 0;
  102. #define TRANSLATE_ERROR(name) \
  103. case MC_CMD_ERR_ ## name: \
  104. return -name;
  105. TRANSLATE_ERROR(EPERM);
  106. TRANSLATE_ERROR(ENOENT);
  107. TRANSLATE_ERROR(EINTR);
  108. TRANSLATE_ERROR(EAGAIN);
  109. TRANSLATE_ERROR(EACCES);
  110. TRANSLATE_ERROR(EBUSY);
  111. TRANSLATE_ERROR(EINVAL);
  112. TRANSLATE_ERROR(EDEADLK);
  113. TRANSLATE_ERROR(ENOSYS);
  114. TRANSLATE_ERROR(ETIME);
  115. TRANSLATE_ERROR(EALREADY);
  116. TRANSLATE_ERROR(ENOSPC);
  117. #undef TRANSLATE_ERROR
  118. case MC_CMD_ERR_ALLOC_FAIL:
  119. return -ENOBUFS;
  120. case MC_CMD_ERR_MAC_EXIST:
  121. return -EADDRINUSE;
  122. default:
  123. return -EPROTO;
  124. }
  125. }
  126. static void efx_mcdi_read_response_header(struct efx_nic *efx)
  127. {
  128. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  129. unsigned int respseq, respcmd, error;
  130. efx_dword_t hdr;
  131. efx->type->mcdi_read_response(efx, &hdr, 0, 4);
  132. respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ);
  133. respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE);
  134. error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR);
  135. if (respcmd != MC_CMD_V2_EXTN) {
  136. mcdi->resp_hdr_len = 4;
  137. mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
  138. } else {
  139. efx->type->mcdi_read_response(efx, &hdr, 4, 4);
  140. mcdi->resp_hdr_len = 8;
  141. mcdi->resp_data_len =
  142. EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
  143. }
  144. if (error && mcdi->resp_data_len == 0) {
  145. netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
  146. mcdi->resprc = -EIO;
  147. } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
  148. netif_err(efx, hw, efx->net_dev,
  149. "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
  150. respseq, mcdi->seqno);
  151. mcdi->resprc = -EIO;
  152. } else if (error) {
  153. efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4);
  154. mcdi->resprc =
  155. efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0));
  156. } else {
  157. mcdi->resprc = 0;
  158. }
  159. }
  160. static int efx_mcdi_poll(struct efx_nic *efx)
  161. {
  162. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  163. unsigned long time, finish;
  164. unsigned int spins;
  165. int rc;
  166. /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
  167. rc = efx_mcdi_poll_reboot(efx);
  168. if (rc) {
  169. spin_lock_bh(&mcdi->iface_lock);
  170. mcdi->resprc = rc;
  171. mcdi->resp_hdr_len = 0;
  172. mcdi->resp_data_len = 0;
  173. spin_unlock_bh(&mcdi->iface_lock);
  174. return 0;
  175. }
  176. /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
  177. * because generally mcdi responses are fast. After that, back off
  178. * and poll once a jiffy (approximately)
  179. */
  180. spins = TICK_USEC;
  181. finish = jiffies + MCDI_RPC_TIMEOUT;
  182. while (1) {
  183. if (spins != 0) {
  184. --spins;
  185. udelay(1);
  186. } else {
  187. schedule_timeout_uninterruptible(1);
  188. }
  189. time = jiffies;
  190. rmb();
  191. if (efx->type->mcdi_poll_response(efx))
  192. break;
  193. if (time_after(time, finish))
  194. return -ETIMEDOUT;
  195. }
  196. spin_lock_bh(&mcdi->iface_lock);
  197. efx_mcdi_read_response_header(efx);
  198. spin_unlock_bh(&mcdi->iface_lock);
  199. /* Return rc=0 like wait_event_timeout() */
  200. return 0;
  201. }
  202. /* Test and clear MC-rebooted flag for this port/function; reset
  203. * software state as necessary.
  204. */
  205. int efx_mcdi_poll_reboot(struct efx_nic *efx)
  206. {
  207. int rc;
  208. if (!efx->mcdi)
  209. return 0;
  210. rc = efx->type->mcdi_poll_reboot(efx);
  211. if (!rc)
  212. return 0;
  213. /* MAC statistics have been cleared on the NIC; clear our copy
  214. * so that efx_update_diff_stat() can continue to work.
  215. */
  216. memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
  217. return rc;
  218. }
  219. static void efx_mcdi_acquire(struct efx_mcdi_iface *mcdi)
  220. {
  221. /* Wait until the interface becomes QUIESCENT and we win the race
  222. * to mark it RUNNING. */
  223. wait_event(mcdi->wq,
  224. atomic_cmpxchg(&mcdi->state,
  225. MCDI_STATE_QUIESCENT,
  226. MCDI_STATE_RUNNING)
  227. == MCDI_STATE_QUIESCENT);
  228. }
  229. static int efx_mcdi_await_completion(struct efx_nic *efx)
  230. {
  231. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  232. if (wait_event_timeout(
  233. mcdi->wq,
  234. atomic_read(&mcdi->state) == MCDI_STATE_COMPLETED,
  235. MCDI_RPC_TIMEOUT) == 0)
  236. return -ETIMEDOUT;
  237. /* Check if efx_mcdi_set_mode() switched us back to polled completions.
  238. * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
  239. * completed the request first, then we'll just end up completing the
  240. * request again, which is safe.
  241. *
  242. * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
  243. * wait_event_timeout() implicitly provides.
  244. */
  245. if (mcdi->mode == MCDI_MODE_POLL)
  246. return efx_mcdi_poll(efx);
  247. return 0;
  248. }
  249. static bool efx_mcdi_complete(struct efx_mcdi_iface *mcdi)
  250. {
  251. /* If the interface is RUNNING, then move to COMPLETED and wake any
  252. * waiters. If the interface isn't in RUNNING then we've received a
  253. * duplicate completion after we've already transitioned back to
  254. * QUIESCENT. [A subsequent invocation would increment seqno, so would
  255. * have failed the seqno check].
  256. */
  257. if (atomic_cmpxchg(&mcdi->state,
  258. MCDI_STATE_RUNNING,
  259. MCDI_STATE_COMPLETED) == MCDI_STATE_RUNNING) {
  260. wake_up(&mcdi->wq);
  261. return true;
  262. }
  263. return false;
  264. }
  265. static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
  266. {
  267. atomic_set(&mcdi->state, MCDI_STATE_QUIESCENT);
  268. wake_up(&mcdi->wq);
  269. }
  270. static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
  271. unsigned int datalen, unsigned int mcdi_err)
  272. {
  273. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  274. bool wake = false;
  275. spin_lock(&mcdi->iface_lock);
  276. if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
  277. if (mcdi->credits)
  278. /* The request has been cancelled */
  279. --mcdi->credits;
  280. else
  281. netif_err(efx, hw, efx->net_dev,
  282. "MC response mismatch tx seq 0x%x rx "
  283. "seq 0x%x\n", seqno, mcdi->seqno);
  284. } else {
  285. if (efx->type->mcdi_max_ver >= 2) {
  286. /* MCDI v2 responses don't fit in an event */
  287. efx_mcdi_read_response_header(efx);
  288. } else {
  289. mcdi->resprc = efx_mcdi_errno(mcdi_err);
  290. mcdi->resp_hdr_len = 4;
  291. mcdi->resp_data_len = datalen;
  292. }
  293. wake = true;
  294. }
  295. spin_unlock(&mcdi->iface_lock);
  296. if (wake)
  297. efx_mcdi_complete(mcdi);
  298. }
  299. int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
  300. const efx_dword_t *inbuf, size_t inlen,
  301. efx_dword_t *outbuf, size_t outlen,
  302. size_t *outlen_actual)
  303. {
  304. int rc;
  305. rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
  306. if (rc)
  307. return rc;
  308. return efx_mcdi_rpc_finish(efx, cmd, inlen,
  309. outbuf, outlen, outlen_actual);
  310. }
  311. int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
  312. const efx_dword_t *inbuf, size_t inlen)
  313. {
  314. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  315. if (efx->type->mcdi_max_ver < 0 ||
  316. (efx->type->mcdi_max_ver < 2 &&
  317. cmd > MC_CMD_CMD_SPACE_ESCAPE_7))
  318. return -EINVAL;
  319. if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 ||
  320. (efx->type->mcdi_max_ver < 2 &&
  321. inlen > MCDI_CTL_SDU_LEN_MAX_V1))
  322. return -EMSGSIZE;
  323. efx_mcdi_acquire(mcdi);
  324. /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
  325. spin_lock_bh(&mcdi->iface_lock);
  326. ++mcdi->seqno;
  327. spin_unlock_bh(&mcdi->iface_lock);
  328. efx_mcdi_copyin(efx, cmd, inbuf, inlen);
  329. return 0;
  330. }
  331. int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
  332. efx_dword_t *outbuf, size_t outlen,
  333. size_t *outlen_actual)
  334. {
  335. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  336. int rc;
  337. if (mcdi->mode == MCDI_MODE_POLL)
  338. rc = efx_mcdi_poll(efx);
  339. else
  340. rc = efx_mcdi_await_completion(efx);
  341. if (rc != 0) {
  342. /* Close the race with efx_mcdi_ev_cpl() executing just too late
  343. * and completing a request we've just cancelled, by ensuring
  344. * that the seqno check therein fails.
  345. */
  346. spin_lock_bh(&mcdi->iface_lock);
  347. ++mcdi->seqno;
  348. ++mcdi->credits;
  349. spin_unlock_bh(&mcdi->iface_lock);
  350. netif_err(efx, hw, efx->net_dev,
  351. "MC command 0x%x inlen %d mode %d timed out\n",
  352. cmd, (int)inlen, mcdi->mode);
  353. } else {
  354. size_t hdr_len, data_len;
  355. /* At the very least we need a memory barrier here to ensure
  356. * we pick up changes from efx_mcdi_ev_cpl(). Protect against
  357. * a spurious efx_mcdi_ev_cpl() running concurrently by
  358. * acquiring the iface_lock. */
  359. spin_lock_bh(&mcdi->iface_lock);
  360. rc = mcdi->resprc;
  361. hdr_len = mcdi->resp_hdr_len;
  362. data_len = mcdi->resp_data_len;
  363. spin_unlock_bh(&mcdi->iface_lock);
  364. BUG_ON(rc > 0);
  365. if (rc == 0) {
  366. efx->type->mcdi_read_response(efx, outbuf, hdr_len,
  367. min(outlen, data_len));
  368. if (outlen_actual != NULL)
  369. *outlen_actual = data_len;
  370. } else if (cmd == MC_CMD_REBOOT && rc == -EIO)
  371. ; /* Don't reset if MC_CMD_REBOOT returns EIO */
  372. else if (rc == -EIO || rc == -EINTR) {
  373. netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
  374. -rc);
  375. efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
  376. } else
  377. netif_dbg(efx, hw, efx->net_dev,
  378. "MC command 0x%x inlen %d failed rc=%d\n",
  379. cmd, (int)inlen, -rc);
  380. if (rc == -EIO || rc == -EINTR) {
  381. msleep(MCDI_STATUS_SLEEP_MS);
  382. efx_mcdi_poll_reboot(efx);
  383. }
  384. }
  385. efx_mcdi_release(mcdi);
  386. return rc;
  387. }
  388. void efx_mcdi_mode_poll(struct efx_nic *efx)
  389. {
  390. struct efx_mcdi_iface *mcdi;
  391. if (!efx->mcdi)
  392. return;
  393. mcdi = efx_mcdi(efx);
  394. if (mcdi->mode == MCDI_MODE_POLL)
  395. return;
  396. /* We can switch from event completion to polled completion, because
  397. * mcdi requests are always completed in shared memory. We do this by
  398. * switching the mode to POLL'd then completing the request.
  399. * efx_mcdi_await_completion() will then call efx_mcdi_poll().
  400. *
  401. * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
  402. * which efx_mcdi_complete() provides for us.
  403. */
  404. mcdi->mode = MCDI_MODE_POLL;
  405. efx_mcdi_complete(mcdi);
  406. }
  407. void efx_mcdi_mode_event(struct efx_nic *efx)
  408. {
  409. struct efx_mcdi_iface *mcdi;
  410. if (!efx->mcdi)
  411. return;
  412. mcdi = efx_mcdi(efx);
  413. if (mcdi->mode == MCDI_MODE_EVENTS)
  414. return;
  415. /* We can't switch from polled to event completion in the middle of a
  416. * request, because the completion method is specified in the request.
  417. * So acquire the interface to serialise the requestors. We don't need
  418. * to acquire the iface_lock to change the mode here, but we do need a
  419. * write memory barrier ensure that efx_mcdi_rpc() sees it, which
  420. * efx_mcdi_acquire() provides.
  421. */
  422. efx_mcdi_acquire(mcdi);
  423. mcdi->mode = MCDI_MODE_EVENTS;
  424. efx_mcdi_release(mcdi);
  425. }
  426. static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
  427. {
  428. struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
  429. /* If there is an outstanding MCDI request, it has been terminated
  430. * either by a BADASSERT or REBOOT event. If the mcdi interface is
  431. * in polled mode, then do nothing because the MC reboot handler will
  432. * set the header correctly. However, if the mcdi interface is waiting
  433. * for a CMDDONE event it won't receive it [and since all MCDI events
  434. * are sent to the same queue, we can't be racing with
  435. * efx_mcdi_ev_cpl()]
  436. *
  437. * There's a race here with efx_mcdi_rpc(), because we might receive
  438. * a REBOOT event *before* the request has been copied out. In polled
  439. * mode (during startup) this is irrelevant, because efx_mcdi_complete()
  440. * is ignored. In event mode, this condition is just an edge-case of
  441. * receiving a REBOOT event after posting the MCDI request. Did the mc
  442. * reboot before or after the copyout? The best we can do always is
  443. * just return failure.
  444. */
  445. spin_lock(&mcdi->iface_lock);
  446. if (efx_mcdi_complete(mcdi)) {
  447. if (mcdi->mode == MCDI_MODE_EVENTS) {
  448. mcdi->resprc = rc;
  449. mcdi->resp_hdr_len = 0;
  450. mcdi->resp_data_len = 0;
  451. ++mcdi->credits;
  452. }
  453. } else {
  454. int count;
  455. /* Nobody was waiting for an MCDI request, so trigger a reset */
  456. efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
  457. /* Consume the status word since efx_mcdi_rpc_finish() won't */
  458. for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
  459. if (efx_mcdi_poll_reboot(efx))
  460. break;
  461. udelay(MCDI_STATUS_DELAY_US);
  462. }
  463. }
  464. spin_unlock(&mcdi->iface_lock);
  465. }
  466. /* Called from falcon_process_eventq for MCDI events */
  467. void efx_mcdi_process_event(struct efx_channel *channel,
  468. efx_qword_t *event)
  469. {
  470. struct efx_nic *efx = channel->efx;
  471. int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
  472. u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
  473. switch (code) {
  474. case MCDI_EVENT_CODE_BADSSERT:
  475. netif_err(efx, hw, efx->net_dev,
  476. "MC watchdog or assertion failure at 0x%x\n", data);
  477. efx_mcdi_ev_death(efx, -EINTR);
  478. break;
  479. case MCDI_EVENT_CODE_PMNOTICE:
  480. netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
  481. break;
  482. case MCDI_EVENT_CODE_CMDDONE:
  483. efx_mcdi_ev_cpl(efx,
  484. MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
  485. MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
  486. MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
  487. break;
  488. case MCDI_EVENT_CODE_LINKCHANGE:
  489. efx_mcdi_process_link_change(efx, event);
  490. break;
  491. case MCDI_EVENT_CODE_SENSOREVT:
  492. efx_mcdi_sensor_event(efx, event);
  493. break;
  494. case MCDI_EVENT_CODE_SCHEDERR:
  495. netif_info(efx, hw, efx->net_dev,
  496. "MC Scheduler error address=0x%x\n", data);
  497. break;
  498. case MCDI_EVENT_CODE_REBOOT:
  499. netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
  500. efx_mcdi_ev_death(efx, -EIO);
  501. break;
  502. case MCDI_EVENT_CODE_MAC_STATS_DMA:
  503. /* MAC stats are gather lazily. We can ignore this. */
  504. break;
  505. case MCDI_EVENT_CODE_FLR:
  506. efx_sriov_flr(efx, MCDI_EVENT_FIELD(*event, FLR_VF));
  507. break;
  508. case MCDI_EVENT_CODE_PTP_RX:
  509. case MCDI_EVENT_CODE_PTP_FAULT:
  510. case MCDI_EVENT_CODE_PTP_PPS:
  511. efx_ptp_event(efx, event);
  512. break;
  513. default:
  514. netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
  515. code);
  516. }
  517. }
  518. /**************************************************************************
  519. *
  520. * Specific request functions
  521. *
  522. **************************************************************************
  523. */
  524. void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
  525. {
  526. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_VERSION_OUT_LEN);
  527. size_t outlength;
  528. const __le16 *ver_words;
  529. int rc;
  530. BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
  531. rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
  532. outbuf, sizeof(outbuf), &outlength);
  533. if (rc)
  534. goto fail;
  535. if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
  536. rc = -EIO;
  537. goto fail;
  538. }
  539. ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
  540. snprintf(buf, len, "%u.%u.%u.%u",
  541. le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
  542. le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
  543. return;
  544. fail:
  545. netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  546. buf[0] = 0;
  547. }
  548. int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
  549. bool *was_attached)
  550. {
  551. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
  552. MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_OUT_LEN);
  553. size_t outlen;
  554. int rc;
  555. MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
  556. driver_operating ? 1 : 0);
  557. MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
  558. MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY);
  559. rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
  560. outbuf, sizeof(outbuf), &outlen);
  561. if (rc)
  562. goto fail;
  563. if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
  564. rc = -EIO;
  565. goto fail;
  566. }
  567. if (was_attached != NULL)
  568. *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
  569. return 0;
  570. fail:
  571. netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  572. return rc;
  573. }
  574. int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
  575. u16 *fw_subtype_list, u32 *capabilities)
  576. {
  577. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
  578. size_t outlen, i;
  579. int port_num = efx_port_num(efx);
  580. int rc;
  581. BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
  582. rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
  583. outbuf, sizeof(outbuf), &outlen);
  584. if (rc)
  585. goto fail;
  586. if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
  587. rc = -EIO;
  588. goto fail;
  589. }
  590. if (mac_address)
  591. memcpy(mac_address,
  592. port_num ?
  593. MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
  594. MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0),
  595. ETH_ALEN);
  596. if (fw_subtype_list) {
  597. for (i = 0;
  598. i < MCDI_VAR_ARRAY_LEN(outlen,
  599. GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
  600. i++)
  601. fw_subtype_list[i] = MCDI_ARRAY_WORD(
  602. outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
  603. for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
  604. fw_subtype_list[i] = 0;
  605. }
  606. if (capabilities) {
  607. if (port_num)
  608. *capabilities = MCDI_DWORD(outbuf,
  609. GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
  610. else
  611. *capabilities = MCDI_DWORD(outbuf,
  612. GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
  613. }
  614. return 0;
  615. fail:
  616. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
  617. __func__, rc, (int)outlen);
  618. return rc;
  619. }
  620. int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
  621. {
  622. MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
  623. u32 dest = 0;
  624. int rc;
  625. if (uart)
  626. dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
  627. if (evq)
  628. dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
  629. MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
  630. MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
  631. BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
  632. rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
  633. NULL, 0, NULL);
  634. if (rc)
  635. goto fail;
  636. return 0;
  637. fail:
  638. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  639. return rc;
  640. }
  641. int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
  642. {
  643. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
  644. size_t outlen;
  645. int rc;
  646. BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
  647. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
  648. outbuf, sizeof(outbuf), &outlen);
  649. if (rc)
  650. goto fail;
  651. if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
  652. rc = -EIO;
  653. goto fail;
  654. }
  655. *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
  656. return 0;
  657. fail:
  658. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
  659. __func__, rc);
  660. return rc;
  661. }
  662. int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
  663. size_t *size_out, size_t *erase_size_out,
  664. bool *protected_out)
  665. {
  666. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
  667. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
  668. size_t outlen;
  669. int rc;
  670. MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
  671. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
  672. outbuf, sizeof(outbuf), &outlen);
  673. if (rc)
  674. goto fail;
  675. if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
  676. rc = -EIO;
  677. goto fail;
  678. }
  679. *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
  680. *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
  681. *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
  682. (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
  683. return 0;
  684. fail:
  685. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  686. return rc;
  687. }
  688. static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
  689. {
  690. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
  691. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
  692. int rc;
  693. MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
  694. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
  695. outbuf, sizeof(outbuf), NULL);
  696. if (rc)
  697. return rc;
  698. switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
  699. case MC_CMD_NVRAM_TEST_PASS:
  700. case MC_CMD_NVRAM_TEST_NOTSUPP:
  701. return 0;
  702. default:
  703. return -EIO;
  704. }
  705. }
  706. int efx_mcdi_nvram_test_all(struct efx_nic *efx)
  707. {
  708. u32 nvram_types;
  709. unsigned int type;
  710. int rc;
  711. rc = efx_mcdi_nvram_types(efx, &nvram_types);
  712. if (rc)
  713. goto fail1;
  714. type = 0;
  715. while (nvram_types != 0) {
  716. if (nvram_types & 1) {
  717. rc = efx_mcdi_nvram_test(efx, type);
  718. if (rc)
  719. goto fail2;
  720. }
  721. type++;
  722. nvram_types >>= 1;
  723. }
  724. return 0;
  725. fail2:
  726. netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
  727. __func__, type);
  728. fail1:
  729. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  730. return rc;
  731. }
  732. static int efx_mcdi_read_assertion(struct efx_nic *efx)
  733. {
  734. MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
  735. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
  736. unsigned int flags, index;
  737. const char *reason;
  738. size_t outlen;
  739. int retry;
  740. int rc;
  741. /* Attempt to read any stored assertion state before we reboot
  742. * the mcfw out of the assertion handler. Retry twice, once
  743. * because a boot-time assertion might cause this command to fail
  744. * with EINTR. And once again because GET_ASSERTS can race with
  745. * MC_CMD_REBOOT running on the other port. */
  746. retry = 2;
  747. do {
  748. MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
  749. rc = efx_mcdi_rpc(efx, MC_CMD_GET_ASSERTS,
  750. inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
  751. outbuf, sizeof(outbuf), &outlen);
  752. } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
  753. if (rc)
  754. return rc;
  755. if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
  756. return -EIO;
  757. /* Print out any recorded assertion state */
  758. flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
  759. if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
  760. return 0;
  761. reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
  762. ? "system-level assertion"
  763. : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
  764. ? "thread-level assertion"
  765. : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
  766. ? "watchdog reset"
  767. : "unknown assertion";
  768. netif_err(efx, hw, efx->net_dev,
  769. "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
  770. MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
  771. MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
  772. /* Print out the registers */
  773. for (index = 0;
  774. index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
  775. index++)
  776. netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
  777. 1 + index,
  778. MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
  779. index));
  780. return 0;
  781. }
  782. static void efx_mcdi_exit_assertion(struct efx_nic *efx)
  783. {
  784. MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
  785. /* If the MC is running debug firmware, it might now be
  786. * waiting for a debugger to attach, but we just want it to
  787. * reboot. We set a flag that makes the command a no-op if it
  788. * has already done so. We don't know what return code to
  789. * expect (0 or -EIO), so ignore it.
  790. */
  791. BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
  792. MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
  793. MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
  794. (void) efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
  795. NULL, 0, NULL);
  796. }
  797. int efx_mcdi_handle_assertion(struct efx_nic *efx)
  798. {
  799. int rc;
  800. rc = efx_mcdi_read_assertion(efx);
  801. if (rc)
  802. return rc;
  803. efx_mcdi_exit_assertion(efx);
  804. return 0;
  805. }
  806. void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
  807. {
  808. MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
  809. int rc;
  810. BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
  811. BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
  812. BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
  813. BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
  814. MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
  815. rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
  816. NULL, 0, NULL);
  817. if (rc)
  818. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
  819. __func__, rc);
  820. }
  821. static int efx_mcdi_reset_port(struct efx_nic *efx)
  822. {
  823. int rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, NULL, 0, NULL, 0, NULL);
  824. if (rc)
  825. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
  826. __func__, rc);
  827. return rc;
  828. }
  829. static int efx_mcdi_reset_mc(struct efx_nic *efx)
  830. {
  831. MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
  832. int rc;
  833. BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
  834. MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
  835. rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
  836. NULL, 0, NULL);
  837. /* White is black, and up is down */
  838. if (rc == -EIO)
  839. return 0;
  840. if (rc == 0)
  841. rc = -EIO;
  842. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  843. return rc;
  844. }
  845. enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
  846. {
  847. return RESET_TYPE_RECOVER_OR_ALL;
  848. }
  849. int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
  850. {
  851. int rc;
  852. /* Recover from a failed assertion pre-reset */
  853. rc = efx_mcdi_handle_assertion(efx);
  854. if (rc)
  855. return rc;
  856. if (method == RESET_TYPE_WORLD)
  857. return efx_mcdi_reset_mc(efx);
  858. else
  859. return efx_mcdi_reset_port(efx);
  860. }
  861. static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
  862. const u8 *mac, int *id_out)
  863. {
  864. MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
  865. MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
  866. size_t outlen;
  867. int rc;
  868. MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
  869. MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
  870. MC_CMD_FILTER_MODE_SIMPLE);
  871. memcpy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac, ETH_ALEN);
  872. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
  873. outbuf, sizeof(outbuf), &outlen);
  874. if (rc)
  875. goto fail;
  876. if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
  877. rc = -EIO;
  878. goto fail;
  879. }
  880. *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
  881. return 0;
  882. fail:
  883. *id_out = -1;
  884. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  885. return rc;
  886. }
  887. int
  888. efx_mcdi_wol_filter_set_magic(struct efx_nic *efx, const u8 *mac, int *id_out)
  889. {
  890. return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
  891. }
  892. int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
  893. {
  894. MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
  895. size_t outlen;
  896. int rc;
  897. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
  898. outbuf, sizeof(outbuf), &outlen);
  899. if (rc)
  900. goto fail;
  901. if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
  902. rc = -EIO;
  903. goto fail;
  904. }
  905. *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
  906. return 0;
  907. fail:
  908. *id_out = -1;
  909. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  910. return rc;
  911. }
  912. int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
  913. {
  914. MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
  915. int rc;
  916. MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
  917. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
  918. NULL, 0, NULL);
  919. if (rc)
  920. goto fail;
  921. return 0;
  922. fail:
  923. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  924. return rc;
  925. }
  926. int efx_mcdi_flush_rxqs(struct efx_nic *efx)
  927. {
  928. struct efx_channel *channel;
  929. struct efx_rx_queue *rx_queue;
  930. MCDI_DECLARE_BUF(inbuf,
  931. MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
  932. int rc, count;
  933. BUILD_BUG_ON(EFX_MAX_CHANNELS >
  934. MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
  935. count = 0;
  936. efx_for_each_channel(channel, efx) {
  937. efx_for_each_channel_rx_queue(rx_queue, channel) {
  938. if (rx_queue->flush_pending) {
  939. rx_queue->flush_pending = false;
  940. atomic_dec(&efx->rxq_flush_pending);
  941. MCDI_SET_ARRAY_DWORD(
  942. inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
  943. count, efx_rx_queue_index(rx_queue));
  944. count++;
  945. }
  946. }
  947. }
  948. rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
  949. MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
  950. WARN_ON(rc < 0);
  951. return rc;
  952. }
  953. int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
  954. {
  955. int rc;
  956. rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
  957. if (rc)
  958. goto fail;
  959. return 0;
  960. fail:
  961. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  962. return rc;
  963. }
  964. #ifdef CONFIG_SFC_MTD
  965. #define EFX_MCDI_NVRAM_LEN_MAX 128
  966. static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
  967. {
  968. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
  969. int rc;
  970. MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
  971. BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
  972. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
  973. NULL, 0, NULL);
  974. if (rc)
  975. goto fail;
  976. return 0;
  977. fail:
  978. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  979. return rc;
  980. }
  981. static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
  982. loff_t offset, u8 *buffer, size_t length)
  983. {
  984. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
  985. MCDI_DECLARE_BUF(outbuf,
  986. MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
  987. size_t outlen;
  988. int rc;
  989. MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
  990. MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
  991. MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
  992. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
  993. outbuf, sizeof(outbuf), &outlen);
  994. if (rc)
  995. goto fail;
  996. memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
  997. return 0;
  998. fail:
  999. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1000. return rc;
  1001. }
  1002. static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
  1003. loff_t offset, const u8 *buffer, size_t length)
  1004. {
  1005. MCDI_DECLARE_BUF(inbuf,
  1006. MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
  1007. int rc;
  1008. MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
  1009. MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
  1010. MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
  1011. memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
  1012. BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
  1013. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
  1014. ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
  1015. NULL, 0, NULL);
  1016. if (rc)
  1017. goto fail;
  1018. return 0;
  1019. fail:
  1020. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1021. return rc;
  1022. }
  1023. static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
  1024. loff_t offset, size_t length)
  1025. {
  1026. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
  1027. int rc;
  1028. MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
  1029. MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
  1030. MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
  1031. BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
  1032. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
  1033. NULL, 0, NULL);
  1034. if (rc)
  1035. goto fail;
  1036. return 0;
  1037. fail:
  1038. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1039. return rc;
  1040. }
  1041. static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
  1042. {
  1043. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
  1044. int rc;
  1045. MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
  1046. BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
  1047. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
  1048. NULL, 0, NULL);
  1049. if (rc)
  1050. goto fail;
  1051. return 0;
  1052. fail:
  1053. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1054. return rc;
  1055. }
  1056. int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
  1057. size_t len, size_t *retlen, u8 *buffer)
  1058. {
  1059. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1060. struct efx_nic *efx = mtd->priv;
  1061. loff_t offset = start;
  1062. loff_t end = min_t(loff_t, start + len, mtd->size);
  1063. size_t chunk;
  1064. int rc = 0;
  1065. while (offset < end) {
  1066. chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
  1067. rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
  1068. buffer, chunk);
  1069. if (rc)
  1070. goto out;
  1071. offset += chunk;
  1072. buffer += chunk;
  1073. }
  1074. out:
  1075. *retlen = offset - start;
  1076. return rc;
  1077. }
  1078. int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
  1079. {
  1080. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1081. struct efx_nic *efx = mtd->priv;
  1082. loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
  1083. loff_t end = min_t(loff_t, start + len, mtd->size);
  1084. size_t chunk = part->common.mtd.erasesize;
  1085. int rc = 0;
  1086. if (!part->updating) {
  1087. rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
  1088. if (rc)
  1089. goto out;
  1090. part->updating = true;
  1091. }
  1092. /* The MCDI interface can in fact do multiple erase blocks at once;
  1093. * but erasing may be slow, so we make multiple calls here to avoid
  1094. * tripping the MCDI RPC timeout. */
  1095. while (offset < end) {
  1096. rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
  1097. chunk);
  1098. if (rc)
  1099. goto out;
  1100. offset += chunk;
  1101. }
  1102. out:
  1103. return rc;
  1104. }
  1105. int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
  1106. size_t len, size_t *retlen, const u8 *buffer)
  1107. {
  1108. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1109. struct efx_nic *efx = mtd->priv;
  1110. loff_t offset = start;
  1111. loff_t end = min_t(loff_t, start + len, mtd->size);
  1112. size_t chunk;
  1113. int rc = 0;
  1114. if (!part->updating) {
  1115. rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
  1116. if (rc)
  1117. goto out;
  1118. part->updating = true;
  1119. }
  1120. while (offset < end) {
  1121. chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
  1122. rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
  1123. buffer, chunk);
  1124. if (rc)
  1125. goto out;
  1126. offset += chunk;
  1127. buffer += chunk;
  1128. }
  1129. out:
  1130. *retlen = offset - start;
  1131. return rc;
  1132. }
  1133. int efx_mcdi_mtd_sync(struct mtd_info *mtd)
  1134. {
  1135. struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
  1136. struct efx_nic *efx = mtd->priv;
  1137. int rc = 0;
  1138. if (part->updating) {
  1139. part->updating = false;
  1140. rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
  1141. }
  1142. return rc;
  1143. }
  1144. void efx_mcdi_mtd_rename(struct efx_mtd_partition *part)
  1145. {
  1146. struct efx_mcdi_mtd_partition *mcdi_part =
  1147. container_of(part, struct efx_mcdi_mtd_partition, common);
  1148. struct efx_nic *efx = part->mtd.priv;
  1149. snprintf(part->name, sizeof(part->name), "%s %s:%02x",
  1150. efx->name, part->type_name, mcdi_part->fw_subtype);
  1151. }
  1152. #endif /* CONFIG_SFC_MTD */