mcdi.c 61 KB

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