mcdi.c 51 KB

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