mcdi.c 62 KB

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