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