mcdi.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  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 falcon_process_eventq 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, "Unknown MCDI event 0x%x\n",
  1201. code);
  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 */