fm10k_mbx.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2015 Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * The full GNU General Public License is included in this distribution in
  14. * the file called "COPYING".
  15. *
  16. * Contact Information:
  17. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. */
  20. #include "fm10k_common.h"
  21. /**
  22. * fm10k_fifo_init - Initialize a message FIFO
  23. * @fifo: pointer to FIFO
  24. * @buffer: pointer to memory to be used to store FIFO
  25. * @size: maximum message size to store in FIFO, must be 2^n - 1
  26. **/
  27. static void fm10k_fifo_init(struct fm10k_mbx_fifo *fifo, u32 *buffer, u16 size)
  28. {
  29. fifo->buffer = buffer;
  30. fifo->size = size;
  31. fifo->head = 0;
  32. fifo->tail = 0;
  33. }
  34. /**
  35. * fm10k_fifo_used - Retrieve used space in FIFO
  36. * @fifo: pointer to FIFO
  37. *
  38. * This function returns the number of DWORDs used in the FIFO
  39. **/
  40. static u16 fm10k_fifo_used(struct fm10k_mbx_fifo *fifo)
  41. {
  42. return fifo->tail - fifo->head;
  43. }
  44. /**
  45. * fm10k_fifo_unused - Retrieve unused space in FIFO
  46. * @fifo: pointer to FIFO
  47. *
  48. * This function returns the number of unused DWORDs in the FIFO
  49. **/
  50. static u16 fm10k_fifo_unused(struct fm10k_mbx_fifo *fifo)
  51. {
  52. return fifo->size + fifo->head - fifo->tail;
  53. }
  54. /**
  55. * fm10k_fifo_empty - Test to verify if fifo is empty
  56. * @fifo: pointer to FIFO
  57. *
  58. * This function returns true if the FIFO is empty, else false
  59. **/
  60. static bool fm10k_fifo_empty(struct fm10k_mbx_fifo *fifo)
  61. {
  62. return fifo->head == fifo->tail;
  63. }
  64. /**
  65. * fm10k_fifo_head_offset - returns indices of head with given offset
  66. * @fifo: pointer to FIFO
  67. * @offset: offset to add to head
  68. *
  69. * This function returns the indices into the fifo based on head + offset
  70. **/
  71. static u16 fm10k_fifo_head_offset(struct fm10k_mbx_fifo *fifo, u16 offset)
  72. {
  73. return (fifo->head + offset) & (fifo->size - 1);
  74. }
  75. /**
  76. * fm10k_fifo_tail_offset - returns indices of tail with given offset
  77. * @fifo: pointer to FIFO
  78. * @offset: offset to add to tail
  79. *
  80. * This function returns the indices into the fifo based on tail + offset
  81. **/
  82. static u16 fm10k_fifo_tail_offset(struct fm10k_mbx_fifo *fifo, u16 offset)
  83. {
  84. return (fifo->tail + offset) & (fifo->size - 1);
  85. }
  86. /**
  87. * fm10k_fifo_head_len - Retrieve length of first message in FIFO
  88. * @fifo: pointer to FIFO
  89. *
  90. * This function returns the size of the first message in the FIFO
  91. **/
  92. static u16 fm10k_fifo_head_len(struct fm10k_mbx_fifo *fifo)
  93. {
  94. u32 *head = fifo->buffer + fm10k_fifo_head_offset(fifo, 0);
  95. /* verify there is at least 1 DWORD in the fifo so *head is valid */
  96. if (fm10k_fifo_empty(fifo))
  97. return 0;
  98. /* retieve the message length */
  99. return FM10K_TLV_DWORD_LEN(*head);
  100. }
  101. /**
  102. * fm10k_fifo_head_drop - Drop the first message in FIFO
  103. * @fifo: pointer to FIFO
  104. *
  105. * This function returns the size of the message dropped from the FIFO
  106. **/
  107. static u16 fm10k_fifo_head_drop(struct fm10k_mbx_fifo *fifo)
  108. {
  109. u16 len = fm10k_fifo_head_len(fifo);
  110. /* update head so it is at the start of next frame */
  111. fifo->head += len;
  112. return len;
  113. }
  114. /**
  115. * fm10k_fifo_drop_all - Drop all messages in FIFO
  116. * @fifo: pointer to FIFO
  117. *
  118. * This function resets the head pointer to drop all messages in the FIFO and
  119. * ensure the FIFO is empty.
  120. **/
  121. static void fm10k_fifo_drop_all(struct fm10k_mbx_fifo *fifo)
  122. {
  123. fifo->head = fifo->tail;
  124. }
  125. /**
  126. * fm10k_mbx_index_len - Convert a head/tail index into a length value
  127. * @mbx: pointer to mailbox
  128. * @head: head index
  129. * @tail: head index
  130. *
  131. * This function takes the head and tail index and determines the length
  132. * of the data indicated by this pair.
  133. **/
  134. static u16 fm10k_mbx_index_len(struct fm10k_mbx_info *mbx, u16 head, u16 tail)
  135. {
  136. u16 len = tail - head;
  137. /* we wrapped so subtract 2, one for index 0, one for all 1s index */
  138. if (len > tail)
  139. len -= 2;
  140. return len & ((mbx->mbmem_len << 1) - 1);
  141. }
  142. /**
  143. * fm10k_mbx_tail_add - Determine new tail value with added offset
  144. * @mbx: pointer to mailbox
  145. * @offset: length to add to head offset
  146. *
  147. * This function takes the local tail index and recomputes it for
  148. * a given length added as an offset.
  149. **/
  150. static u16 fm10k_mbx_tail_add(struct fm10k_mbx_info *mbx, u16 offset)
  151. {
  152. u16 tail = (mbx->tail + offset + 1) & ((mbx->mbmem_len << 1) - 1);
  153. /* add/sub 1 because we cannot have offset 0 or all 1s */
  154. return (tail > mbx->tail) ? --tail : ++tail;
  155. }
  156. /**
  157. * fm10k_mbx_tail_sub - Determine new tail value with subtracted offset
  158. * @mbx: pointer to mailbox
  159. * @offset: length to add to head offset
  160. *
  161. * This function takes the local tail index and recomputes it for
  162. * a given length added as an offset.
  163. **/
  164. static u16 fm10k_mbx_tail_sub(struct fm10k_mbx_info *mbx, u16 offset)
  165. {
  166. u16 tail = (mbx->tail - offset - 1) & ((mbx->mbmem_len << 1) - 1);
  167. /* sub/add 1 because we cannot have offset 0 or all 1s */
  168. return (tail < mbx->tail) ? ++tail : --tail;
  169. }
  170. /**
  171. * fm10k_mbx_head_add - Determine new head value with added offset
  172. * @mbx: pointer to mailbox
  173. * @offset: length to add to head offset
  174. *
  175. * This function takes the local head index and recomputes it for
  176. * a given length added as an offset.
  177. **/
  178. static u16 fm10k_mbx_head_add(struct fm10k_mbx_info *mbx, u16 offset)
  179. {
  180. u16 head = (mbx->head + offset + 1) & ((mbx->mbmem_len << 1) - 1);
  181. /* add/sub 1 because we cannot have offset 0 or all 1s */
  182. return (head > mbx->head) ? --head : ++head;
  183. }
  184. /**
  185. * fm10k_mbx_head_sub - Determine new head value with subtracted offset
  186. * @mbx: pointer to mailbox
  187. * @offset: length to add to head offset
  188. *
  189. * This function takes the local head index and recomputes it for
  190. * a given length added as an offset.
  191. **/
  192. static u16 fm10k_mbx_head_sub(struct fm10k_mbx_info *mbx, u16 offset)
  193. {
  194. u16 head = (mbx->head - offset - 1) & ((mbx->mbmem_len << 1) - 1);
  195. /* sub/add 1 because we cannot have offset 0 or all 1s */
  196. return (head < mbx->head) ? ++head : --head;
  197. }
  198. /**
  199. * fm10k_mbx_pushed_tail_len - Retrieve the length of message being pushed
  200. * @mbx: pointer to mailbox
  201. *
  202. * This function will return the length of the message currently being
  203. * pushed onto the tail of the Rx queue.
  204. **/
  205. static u16 fm10k_mbx_pushed_tail_len(struct fm10k_mbx_info *mbx)
  206. {
  207. u32 *tail = mbx->rx.buffer + fm10k_fifo_tail_offset(&mbx->rx, 0);
  208. /* pushed tail is only valid if pushed is set */
  209. if (!mbx->pushed)
  210. return 0;
  211. return FM10K_TLV_DWORD_LEN(*tail);
  212. }
  213. /**
  214. * fm10k_fifo_write_copy - pulls data off of msg and places it in fifo
  215. * @fifo: pointer to FIFO
  216. * @msg: message array to populate
  217. * @tail_offset: additional offset to add to tail pointer
  218. * @len: length of FIFO to copy into message header
  219. *
  220. * This function will take a message and copy it into a section of the
  221. * FIFO. In order to get something into a location other than just
  222. * the tail you can use tail_offset to adjust the pointer.
  223. **/
  224. static void fm10k_fifo_write_copy(struct fm10k_mbx_fifo *fifo,
  225. const u32 *msg, u16 tail_offset, u16 len)
  226. {
  227. u16 end = fm10k_fifo_tail_offset(fifo, tail_offset);
  228. u32 *tail = fifo->buffer + end;
  229. /* track when we should cross the end of the FIFO */
  230. end = fifo->size - end;
  231. /* copy end of message before start of message */
  232. if (end < len)
  233. memcpy(fifo->buffer, msg + end, (len - end) << 2);
  234. else
  235. end = len;
  236. /* Copy remaining message into Tx FIFO */
  237. memcpy(tail, msg, end << 2);
  238. }
  239. /**
  240. * fm10k_fifo_enqueue - Enqueues the message to the tail of the FIFO
  241. * @fifo: pointer to FIFO
  242. * @msg: message array to read
  243. *
  244. * This function enqueues a message up to the size specified by the length
  245. * contained in the first DWORD of the message and will place at the tail
  246. * of the FIFO. It will return 0 on success, or a negative value on error.
  247. **/
  248. static s32 fm10k_fifo_enqueue(struct fm10k_mbx_fifo *fifo, const u32 *msg)
  249. {
  250. u16 len = FM10K_TLV_DWORD_LEN(*msg);
  251. /* verify parameters */
  252. if (len > fifo->size)
  253. return FM10K_MBX_ERR_SIZE;
  254. /* verify there is room for the message */
  255. if (len > fm10k_fifo_unused(fifo))
  256. return FM10K_MBX_ERR_NO_SPACE;
  257. /* Copy message into FIFO */
  258. fm10k_fifo_write_copy(fifo, msg, 0, len);
  259. /* memory barrier to guarantee FIFO is written before tail update */
  260. wmb();
  261. /* Update Tx FIFO tail */
  262. fifo->tail += len;
  263. return 0;
  264. }
  265. /**
  266. * fm10k_mbx_validate_msg_size - Validate incoming message based on size
  267. * @mbx: pointer to mailbox
  268. * @len: length of data pushed onto buffer
  269. *
  270. * This function analyzes the frame and will return a non-zero value when
  271. * the start of a message larger than the mailbox is detected.
  272. **/
  273. static u16 fm10k_mbx_validate_msg_size(struct fm10k_mbx_info *mbx, u16 len)
  274. {
  275. struct fm10k_mbx_fifo *fifo = &mbx->rx;
  276. u16 total_len = 0, msg_len;
  277. u32 *msg;
  278. /* length should include previous amounts pushed */
  279. len += mbx->pushed;
  280. /* offset in message is based off of current message size */
  281. do {
  282. msg = fifo->buffer + fm10k_fifo_tail_offset(fifo, total_len);
  283. msg_len = FM10K_TLV_DWORD_LEN(*msg);
  284. total_len += msg_len;
  285. } while (total_len < len);
  286. /* message extends out of pushed section, but fits in FIFO */
  287. if ((len < total_len) && (msg_len <= mbx->max_size))
  288. return 0;
  289. /* return length of invalid section */
  290. return (len < total_len) ? len : (len - total_len);
  291. }
  292. /**
  293. * fm10k_mbx_write_copy - pulls data off of Tx FIFO and places it in mbmem
  294. * @mbx: pointer to mailbox
  295. *
  296. * This function will take a section of the Tx FIFO and copy it into the
  297. * mailbox memory. The offset in mbmem is based on the lower bits of the
  298. * tail and len determines the length to copy.
  299. **/
  300. static void fm10k_mbx_write_copy(struct fm10k_hw *hw,
  301. struct fm10k_mbx_info *mbx)
  302. {
  303. struct fm10k_mbx_fifo *fifo = &mbx->tx;
  304. u32 mbmem = mbx->mbmem_reg;
  305. u32 *head = fifo->buffer;
  306. u16 end, len, tail, mask;
  307. if (!mbx->tail_len)
  308. return;
  309. /* determine data length and mbmem tail index */
  310. mask = mbx->mbmem_len - 1;
  311. len = mbx->tail_len;
  312. tail = fm10k_mbx_tail_sub(mbx, len);
  313. if (tail > mask)
  314. tail++;
  315. /* determine offset in the ring */
  316. end = fm10k_fifo_head_offset(fifo, mbx->pulled);
  317. head += end;
  318. /* memory barrier to guarantee data is ready to be read */
  319. rmb();
  320. /* Copy message from Tx FIFO */
  321. for (end = fifo->size - end; len; head = fifo->buffer) {
  322. do {
  323. /* adjust tail to match offset for FIFO */
  324. tail &= mask;
  325. if (!tail)
  326. tail++;
  327. /* write message to hardware FIFO */
  328. fm10k_write_reg(hw, mbmem + tail++, *(head++));
  329. } while (--len && --end);
  330. }
  331. }
  332. /**
  333. * fm10k_mbx_pull_head - Pulls data off of head of Tx FIFO
  334. * @hw: pointer to hardware structure
  335. * @mbx: pointer to mailbox
  336. * @head: acknowledgement number last received
  337. *
  338. * This function will push the tail index forward based on the remote
  339. * head index. It will then pull up to mbmem_len DWORDs off of the
  340. * head of the FIFO and will place it in the MBMEM registers
  341. * associated with the mailbox.
  342. **/
  343. static void fm10k_mbx_pull_head(struct fm10k_hw *hw,
  344. struct fm10k_mbx_info *mbx, u16 head)
  345. {
  346. u16 mbmem_len, len, ack = fm10k_mbx_index_len(mbx, head, mbx->tail);
  347. struct fm10k_mbx_fifo *fifo = &mbx->tx;
  348. /* update number of bytes pulled and update bytes in transit */
  349. mbx->pulled += mbx->tail_len - ack;
  350. /* determine length of data to pull, reserve space for mbmem header */
  351. mbmem_len = mbx->mbmem_len - 1;
  352. len = fm10k_fifo_used(fifo) - mbx->pulled;
  353. if (len > mbmem_len)
  354. len = mbmem_len;
  355. /* update tail and record number of bytes in transit */
  356. mbx->tail = fm10k_mbx_tail_add(mbx, len - ack);
  357. mbx->tail_len = len;
  358. /* drop pulled messages from the FIFO */
  359. for (len = fm10k_fifo_head_len(fifo);
  360. len && (mbx->pulled >= len);
  361. len = fm10k_fifo_head_len(fifo)) {
  362. mbx->pulled -= fm10k_fifo_head_drop(fifo);
  363. mbx->tx_messages++;
  364. mbx->tx_dwords += len;
  365. }
  366. /* Copy message out from the Tx FIFO */
  367. fm10k_mbx_write_copy(hw, mbx);
  368. }
  369. /**
  370. * fm10k_mbx_read_copy - pulls data off of mbmem and places it in Rx FIFO
  371. * @hw: pointer to hardware structure
  372. * @mbx: pointer to mailbox
  373. *
  374. * This function will take a section of the mailbox memory and copy it
  375. * into the Rx FIFO. The offset is based on the lower bits of the
  376. * head and len determines the length to copy.
  377. **/
  378. static void fm10k_mbx_read_copy(struct fm10k_hw *hw,
  379. struct fm10k_mbx_info *mbx)
  380. {
  381. struct fm10k_mbx_fifo *fifo = &mbx->rx;
  382. u32 mbmem = mbx->mbmem_reg ^ mbx->mbmem_len;
  383. u32 *tail = fifo->buffer;
  384. u16 end, len, head;
  385. /* determine data length and mbmem head index */
  386. len = mbx->head_len;
  387. head = fm10k_mbx_head_sub(mbx, len);
  388. if (head >= mbx->mbmem_len)
  389. head++;
  390. /* determine offset in the ring */
  391. end = fm10k_fifo_tail_offset(fifo, mbx->pushed);
  392. tail += end;
  393. /* Copy message into Rx FIFO */
  394. for (end = fifo->size - end; len; tail = fifo->buffer) {
  395. do {
  396. /* adjust head to match offset for FIFO */
  397. head &= mbx->mbmem_len - 1;
  398. if (!head)
  399. head++;
  400. /* read message from hardware FIFO */
  401. *(tail++) = fm10k_read_reg(hw, mbmem + head++);
  402. } while (--len && --end);
  403. }
  404. /* memory barrier to guarantee FIFO is written before tail update */
  405. wmb();
  406. }
  407. /**
  408. * fm10k_mbx_push_tail - Pushes up to 15 DWORDs on to tail of FIFO
  409. * @hw: pointer to hardware structure
  410. * @mbx: pointer to mailbox
  411. * @tail: tail index of message
  412. *
  413. * This function will first validate the tail index and size for the
  414. * incoming message. It then updates the acknowledgment number and
  415. * copies the data into the FIFO. It will return the number of messages
  416. * dequeued on success and a negative value on error.
  417. **/
  418. static s32 fm10k_mbx_push_tail(struct fm10k_hw *hw,
  419. struct fm10k_mbx_info *mbx,
  420. u16 tail)
  421. {
  422. struct fm10k_mbx_fifo *fifo = &mbx->rx;
  423. u16 len, seq = fm10k_mbx_index_len(mbx, mbx->head, tail);
  424. /* determine length of data to push */
  425. len = fm10k_fifo_unused(fifo) - mbx->pushed;
  426. if (len > seq)
  427. len = seq;
  428. /* update head and record bytes received */
  429. mbx->head = fm10k_mbx_head_add(mbx, len);
  430. mbx->head_len = len;
  431. /* nothing to do if there is no data */
  432. if (!len)
  433. return 0;
  434. /* Copy msg into Rx FIFO */
  435. fm10k_mbx_read_copy(hw, mbx);
  436. /* determine if there are any invalid lengths in message */
  437. if (fm10k_mbx_validate_msg_size(mbx, len))
  438. return FM10K_MBX_ERR_SIZE;
  439. /* Update pushed */
  440. mbx->pushed += len;
  441. /* flush any completed messages */
  442. for (len = fm10k_mbx_pushed_tail_len(mbx);
  443. len && (mbx->pushed >= len);
  444. len = fm10k_mbx_pushed_tail_len(mbx)) {
  445. fifo->tail += len;
  446. mbx->pushed -= len;
  447. mbx->rx_messages++;
  448. mbx->rx_dwords += len;
  449. }
  450. return 0;
  451. }
  452. /* pre-generated data for generating the CRC based on the poly 0xAC9A. */
  453. static const u16 fm10k_crc_16b_table[256] = {
  454. 0x0000, 0x7956, 0xF2AC, 0x8BFA, 0xBC6D, 0xC53B, 0x4EC1, 0x3797,
  455. 0x21EF, 0x58B9, 0xD343, 0xAA15, 0x9D82, 0xE4D4, 0x6F2E, 0x1678,
  456. 0x43DE, 0x3A88, 0xB172, 0xC824, 0xFFB3, 0x86E5, 0x0D1F, 0x7449,
  457. 0x6231, 0x1B67, 0x909D, 0xE9CB, 0xDE5C, 0xA70A, 0x2CF0, 0x55A6,
  458. 0x87BC, 0xFEEA, 0x7510, 0x0C46, 0x3BD1, 0x4287, 0xC97D, 0xB02B,
  459. 0xA653, 0xDF05, 0x54FF, 0x2DA9, 0x1A3E, 0x6368, 0xE892, 0x91C4,
  460. 0xC462, 0xBD34, 0x36CE, 0x4F98, 0x780F, 0x0159, 0x8AA3, 0xF3F5,
  461. 0xE58D, 0x9CDB, 0x1721, 0x6E77, 0x59E0, 0x20B6, 0xAB4C, 0xD21A,
  462. 0x564D, 0x2F1B, 0xA4E1, 0xDDB7, 0xEA20, 0x9376, 0x188C, 0x61DA,
  463. 0x77A2, 0x0EF4, 0x850E, 0xFC58, 0xCBCF, 0xB299, 0x3963, 0x4035,
  464. 0x1593, 0x6CC5, 0xE73F, 0x9E69, 0xA9FE, 0xD0A8, 0x5B52, 0x2204,
  465. 0x347C, 0x4D2A, 0xC6D0, 0xBF86, 0x8811, 0xF147, 0x7ABD, 0x03EB,
  466. 0xD1F1, 0xA8A7, 0x235D, 0x5A0B, 0x6D9C, 0x14CA, 0x9F30, 0xE666,
  467. 0xF01E, 0x8948, 0x02B2, 0x7BE4, 0x4C73, 0x3525, 0xBEDF, 0xC789,
  468. 0x922F, 0xEB79, 0x6083, 0x19D5, 0x2E42, 0x5714, 0xDCEE, 0xA5B8,
  469. 0xB3C0, 0xCA96, 0x416C, 0x383A, 0x0FAD, 0x76FB, 0xFD01, 0x8457,
  470. 0xAC9A, 0xD5CC, 0x5E36, 0x2760, 0x10F7, 0x69A1, 0xE25B, 0x9B0D,
  471. 0x8D75, 0xF423, 0x7FD9, 0x068F, 0x3118, 0x484E, 0xC3B4, 0xBAE2,
  472. 0xEF44, 0x9612, 0x1DE8, 0x64BE, 0x5329, 0x2A7F, 0xA185, 0xD8D3,
  473. 0xCEAB, 0xB7FD, 0x3C07, 0x4551, 0x72C6, 0x0B90, 0x806A, 0xF93C,
  474. 0x2B26, 0x5270, 0xD98A, 0xA0DC, 0x974B, 0xEE1D, 0x65E7, 0x1CB1,
  475. 0x0AC9, 0x739F, 0xF865, 0x8133, 0xB6A4, 0xCFF2, 0x4408, 0x3D5E,
  476. 0x68F8, 0x11AE, 0x9A54, 0xE302, 0xD495, 0xADC3, 0x2639, 0x5F6F,
  477. 0x4917, 0x3041, 0xBBBB, 0xC2ED, 0xF57A, 0x8C2C, 0x07D6, 0x7E80,
  478. 0xFAD7, 0x8381, 0x087B, 0x712D, 0x46BA, 0x3FEC, 0xB416, 0xCD40,
  479. 0xDB38, 0xA26E, 0x2994, 0x50C2, 0x6755, 0x1E03, 0x95F9, 0xECAF,
  480. 0xB909, 0xC05F, 0x4BA5, 0x32F3, 0x0564, 0x7C32, 0xF7C8, 0x8E9E,
  481. 0x98E6, 0xE1B0, 0x6A4A, 0x131C, 0x248B, 0x5DDD, 0xD627, 0xAF71,
  482. 0x7D6B, 0x043D, 0x8FC7, 0xF691, 0xC106, 0xB850, 0x33AA, 0x4AFC,
  483. 0x5C84, 0x25D2, 0xAE28, 0xD77E, 0xE0E9, 0x99BF, 0x1245, 0x6B13,
  484. 0x3EB5, 0x47E3, 0xCC19, 0xB54F, 0x82D8, 0xFB8E, 0x7074, 0x0922,
  485. 0x1F5A, 0x660C, 0xEDF6, 0x94A0, 0xA337, 0xDA61, 0x519B, 0x28CD };
  486. /**
  487. * fm10k_crc_16b - Generate a 16 bit CRC for a region of 16 bit data
  488. * @data: pointer to data to process
  489. * @seed: seed value for CRC
  490. * @len: length measured in 16 bits words
  491. *
  492. * This function will generate a CRC based on the polynomial 0xAC9A and
  493. * whatever value is stored in the seed variable. Note that this
  494. * value inverts the local seed and the result in order to capture all
  495. * leading and trailing zeros.
  496. */
  497. static u16 fm10k_crc_16b(const u32 *data, u16 seed, u16 len)
  498. {
  499. u32 result = seed;
  500. while (len--) {
  501. result ^= *(data++);
  502. result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
  503. result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
  504. if (!(len--))
  505. break;
  506. result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
  507. result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
  508. }
  509. return (u16)result;
  510. }
  511. /**
  512. * fm10k_fifo_crc - generate a CRC based off of FIFO data
  513. * @fifo: pointer to FIFO
  514. * @offset: offset point for start of FIFO
  515. * @len: number of DWORDS words to process
  516. * @seed: seed value for CRC
  517. *
  518. * This function generates a CRC for some region of the FIFO
  519. **/
  520. static u16 fm10k_fifo_crc(struct fm10k_mbx_fifo *fifo, u16 offset,
  521. u16 len, u16 seed)
  522. {
  523. u32 *data = fifo->buffer + offset;
  524. /* track when we should cross the end of the FIFO */
  525. offset = fifo->size - offset;
  526. /* if we are in 2 blocks process the end of the FIFO first */
  527. if (offset < len) {
  528. seed = fm10k_crc_16b(data, seed, offset * 2);
  529. data = fifo->buffer;
  530. len -= offset;
  531. }
  532. /* process any remaining bits */
  533. return fm10k_crc_16b(data, seed, len * 2);
  534. }
  535. /**
  536. * fm10k_mbx_update_local_crc - Update the local CRC for outgoing data
  537. * @mbx: pointer to mailbox
  538. * @head: head index provided by remote mailbox
  539. *
  540. * This function will generate the CRC for all data from the end of the
  541. * last head update to the current one. It uses the result of the
  542. * previous CRC as the seed for this update. The result is stored in
  543. * mbx->local.
  544. **/
  545. static void fm10k_mbx_update_local_crc(struct fm10k_mbx_info *mbx, u16 head)
  546. {
  547. u16 len = mbx->tail_len - fm10k_mbx_index_len(mbx, head, mbx->tail);
  548. /* determine the offset for the start of the region to be pulled */
  549. head = fm10k_fifo_head_offset(&mbx->tx, mbx->pulled);
  550. /* update local CRC to include all of the pulled data */
  551. mbx->local = fm10k_fifo_crc(&mbx->tx, head, len, mbx->local);
  552. }
  553. /**
  554. * fm10k_mbx_verify_remote_crc - Verify the CRC is correct for current data
  555. * @mbx: pointer to mailbox
  556. *
  557. * This function will take all data that has been provided from the remote
  558. * end and generate a CRC for it. This is stored in mbx->remote. The
  559. * CRC for the header is then computed and if the result is non-zero this
  560. * is an error and we signal an error dropping all data and resetting the
  561. * connection.
  562. */
  563. static s32 fm10k_mbx_verify_remote_crc(struct fm10k_mbx_info *mbx)
  564. {
  565. struct fm10k_mbx_fifo *fifo = &mbx->rx;
  566. u16 len = mbx->head_len;
  567. u16 offset = fm10k_fifo_tail_offset(fifo, mbx->pushed) - len;
  568. u16 crc;
  569. /* update the remote CRC if new data has been received */
  570. if (len)
  571. mbx->remote = fm10k_fifo_crc(fifo, offset, len, mbx->remote);
  572. /* process the full header as we have to validate the CRC */
  573. crc = fm10k_crc_16b(&mbx->mbx_hdr, mbx->remote, 1);
  574. /* notify other end if we have a problem */
  575. return crc ? FM10K_MBX_ERR_CRC : 0;
  576. }
  577. /**
  578. * fm10k_mbx_rx_ready - Indicates that a message is ready in the Rx FIFO
  579. * @mbx: pointer to mailbox
  580. *
  581. * This function returns true if there is a message in the Rx FIFO to dequeue.
  582. **/
  583. static bool fm10k_mbx_rx_ready(struct fm10k_mbx_info *mbx)
  584. {
  585. u16 msg_size = fm10k_fifo_head_len(&mbx->rx);
  586. return msg_size && (fm10k_fifo_used(&mbx->rx) >= msg_size);
  587. }
  588. /**
  589. * fm10k_mbx_tx_ready - Indicates that the mailbox is in state ready for Tx
  590. * @mbx: pointer to mailbox
  591. * @len: verify free space is >= this value
  592. *
  593. * This function returns true if the mailbox is in a state ready to transmit.
  594. **/
  595. static bool fm10k_mbx_tx_ready(struct fm10k_mbx_info *mbx, u16 len)
  596. {
  597. u16 fifo_unused = fm10k_fifo_unused(&mbx->tx);
  598. return (mbx->state == FM10K_STATE_OPEN) && (fifo_unused >= len);
  599. }
  600. /**
  601. * fm10k_mbx_tx_complete - Indicates that the Tx FIFO has been emptied
  602. * @mbx: pointer to mailbox
  603. *
  604. * This function returns true if the Tx FIFO is empty.
  605. **/
  606. static bool fm10k_mbx_tx_complete(struct fm10k_mbx_info *mbx)
  607. {
  608. return fm10k_fifo_empty(&mbx->tx);
  609. }
  610. /**
  611. * fm10k_mbx_deqeueue_rx - Dequeues the message from the head in the Rx FIFO
  612. * @hw: pointer to hardware structure
  613. * @mbx: pointer to mailbox
  614. *
  615. * This function dequeues messages and hands them off to the tlv parser.
  616. * It will return the number of messages processed when called.
  617. **/
  618. static u16 fm10k_mbx_dequeue_rx(struct fm10k_hw *hw,
  619. struct fm10k_mbx_info *mbx)
  620. {
  621. struct fm10k_mbx_fifo *fifo = &mbx->rx;
  622. s32 err;
  623. u16 cnt;
  624. /* parse Rx messages out of the Rx FIFO to empty it */
  625. for (cnt = 0; !fm10k_fifo_empty(fifo); cnt++) {
  626. err = fm10k_tlv_msg_parse(hw, fifo->buffer + fifo->head,
  627. mbx, mbx->msg_data);
  628. if (err < 0)
  629. mbx->rx_parse_err++;
  630. fm10k_fifo_head_drop(fifo);
  631. }
  632. /* shift remaining bytes back to start of FIFO */
  633. memmove(fifo->buffer, fifo->buffer + fifo->tail, mbx->pushed << 2);
  634. /* shift head and tail based on the memory we moved */
  635. fifo->tail -= fifo->head;
  636. fifo->head = 0;
  637. return cnt;
  638. }
  639. /**
  640. * fm10k_mbx_enqueue_tx - Enqueues the message to the tail of the Tx FIFO
  641. * @hw: pointer to hardware structure
  642. * @mbx: pointer to mailbox
  643. * @msg: message array to read
  644. *
  645. * This function enqueues a message up to the size specified by the length
  646. * contained in the first DWORD of the message and will place at the tail
  647. * of the FIFO. It will return 0 on success, or a negative value on error.
  648. **/
  649. static s32 fm10k_mbx_enqueue_tx(struct fm10k_hw *hw,
  650. struct fm10k_mbx_info *mbx, const u32 *msg)
  651. {
  652. u32 countdown = mbx->timeout;
  653. s32 err;
  654. switch (mbx->state) {
  655. case FM10K_STATE_CLOSED:
  656. case FM10K_STATE_DISCONNECT:
  657. return FM10K_MBX_ERR_NO_MBX;
  658. default:
  659. break;
  660. }
  661. /* enqueue the message on the Tx FIFO */
  662. err = fm10k_fifo_enqueue(&mbx->tx, msg);
  663. /* if it failed give the FIFO a chance to drain */
  664. while (err && countdown) {
  665. countdown--;
  666. udelay(mbx->udelay);
  667. mbx->ops.process(hw, mbx);
  668. err = fm10k_fifo_enqueue(&mbx->tx, msg);
  669. }
  670. /* if we failed treat the error */
  671. if (err) {
  672. mbx->timeout = 0;
  673. mbx->tx_busy++;
  674. }
  675. /* begin processing message, ignore errors as this is just meant
  676. * to start the mailbox flow so we are not concerned if there
  677. * is a bad error, or the mailbox is already busy with a request
  678. */
  679. if (!mbx->tail_len)
  680. mbx->ops.process(hw, mbx);
  681. return 0;
  682. }
  683. /**
  684. * fm10k_mbx_read - Copies the mbmem to local message buffer
  685. * @hw: pointer to hardware structure
  686. * @mbx: pointer to mailbox
  687. *
  688. * This function copies the message from the mbmem to the message array
  689. **/
  690. static s32 fm10k_mbx_read(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
  691. {
  692. /* only allow one reader in here at a time */
  693. if (mbx->mbx_hdr)
  694. return FM10K_MBX_ERR_BUSY;
  695. /* read to capture initial interrupt bits */
  696. if (fm10k_read_reg(hw, mbx->mbx_reg) & FM10K_MBX_REQ_INTERRUPT)
  697. mbx->mbx_lock = FM10K_MBX_ACK;
  698. /* write back interrupt bits to clear */
  699. fm10k_write_reg(hw, mbx->mbx_reg,
  700. FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT);
  701. /* read remote header */
  702. mbx->mbx_hdr = fm10k_read_reg(hw, mbx->mbmem_reg ^ mbx->mbmem_len);
  703. return 0;
  704. }
  705. /**
  706. * fm10k_mbx_write - Copies the local message buffer to mbmem
  707. * @hw: pointer to hardware structure
  708. * @mbx: pointer to mailbox
  709. *
  710. * This function copies the message from the the message array to mbmem
  711. **/
  712. static void fm10k_mbx_write(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
  713. {
  714. u32 mbmem = mbx->mbmem_reg;
  715. /* write new msg header to notify recipient of change */
  716. fm10k_write_reg(hw, mbmem, mbx->mbx_hdr);
  717. /* write mailbox to send interrupt */
  718. if (mbx->mbx_lock)
  719. fm10k_write_reg(hw, mbx->mbx_reg, mbx->mbx_lock);
  720. /* we no longer are using the header so free it */
  721. mbx->mbx_hdr = 0;
  722. mbx->mbx_lock = 0;
  723. }
  724. /**
  725. * fm10k_mbx_create_connect_hdr - Generate a connect mailbox header
  726. * @mbx: pointer to mailbox
  727. *
  728. * This function returns a connection mailbox header
  729. **/
  730. static void fm10k_mbx_create_connect_hdr(struct fm10k_mbx_info *mbx)
  731. {
  732. mbx->mbx_lock |= FM10K_MBX_REQ;
  733. mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_CONNECT, TYPE) |
  734. FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD) |
  735. FM10K_MSG_HDR_FIELD_SET(mbx->rx.size - 1, CONNECT_SIZE);
  736. }
  737. /**
  738. * fm10k_mbx_create_data_hdr - Generate a data mailbox header
  739. * @mbx: pointer to mailbox
  740. *
  741. * This function returns a data mailbox header
  742. **/
  743. static void fm10k_mbx_create_data_hdr(struct fm10k_mbx_info *mbx)
  744. {
  745. u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DATA, TYPE) |
  746. FM10K_MSG_HDR_FIELD_SET(mbx->tail, TAIL) |
  747. FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
  748. struct fm10k_mbx_fifo *fifo = &mbx->tx;
  749. u16 crc;
  750. if (mbx->tail_len)
  751. mbx->mbx_lock |= FM10K_MBX_REQ;
  752. /* generate CRC for data in flight and header */
  753. crc = fm10k_fifo_crc(fifo, fm10k_fifo_head_offset(fifo, mbx->pulled),
  754. mbx->tail_len, mbx->local);
  755. crc = fm10k_crc_16b(&hdr, crc, 1);
  756. /* load header to memory to be written */
  757. mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
  758. }
  759. /**
  760. * fm10k_mbx_create_disconnect_hdr - Generate a disconnect mailbox header
  761. * @mbx: pointer to mailbox
  762. *
  763. * This function returns a disconnect mailbox header
  764. **/
  765. static void fm10k_mbx_create_disconnect_hdr(struct fm10k_mbx_info *mbx)
  766. {
  767. u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DISCONNECT, TYPE) |
  768. FM10K_MSG_HDR_FIELD_SET(mbx->tail, TAIL) |
  769. FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
  770. u16 crc = fm10k_crc_16b(&hdr, mbx->local, 1);
  771. mbx->mbx_lock |= FM10K_MBX_ACK;
  772. /* load header to memory to be written */
  773. mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
  774. }
  775. /**
  776. * fm10k_mbx_create_fake_disconnect_hdr - Generate a false disconnect mailbox header
  777. * @mbx: pointer to mailbox
  778. *
  779. * This function creates a fake disconnect header for loading into remote
  780. * mailbox header. The primary purpose is to prevent errors on immediate
  781. * start up after mbx->connect.
  782. **/
  783. static void fm10k_mbx_create_fake_disconnect_hdr(struct fm10k_mbx_info *mbx)
  784. {
  785. u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DISCONNECT, TYPE) |
  786. FM10K_MSG_HDR_FIELD_SET(mbx->head, TAIL) |
  787. FM10K_MSG_HDR_FIELD_SET(mbx->tail, HEAD);
  788. u16 crc = fm10k_crc_16b(&hdr, mbx->local, 1);
  789. mbx->mbx_lock |= FM10K_MBX_ACK;
  790. /* load header to memory to be written */
  791. mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
  792. }
  793. /**
  794. * fm10k_mbx_create_error_msg - Generate a error message
  795. * @mbx: pointer to mailbox
  796. * @err: local error encountered
  797. *
  798. * This function will interpret the error provided by err, and based on
  799. * that it may shift the message by 1 DWORD and then place an error header
  800. * at the start of the message.
  801. **/
  802. static void fm10k_mbx_create_error_msg(struct fm10k_mbx_info *mbx, s32 err)
  803. {
  804. /* only generate an error message for these types */
  805. switch (err) {
  806. case FM10K_MBX_ERR_TAIL:
  807. case FM10K_MBX_ERR_HEAD:
  808. case FM10K_MBX_ERR_TYPE:
  809. case FM10K_MBX_ERR_SIZE:
  810. case FM10K_MBX_ERR_RSVD0:
  811. case FM10K_MBX_ERR_CRC:
  812. break;
  813. default:
  814. return;
  815. }
  816. mbx->mbx_lock |= FM10K_MBX_REQ;
  817. mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_ERROR, TYPE) |
  818. FM10K_MSG_HDR_FIELD_SET(err, ERR_NO) |
  819. FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
  820. }
  821. /**
  822. * fm10k_mbx_validate_msg_hdr - Validate common fields in the message header
  823. * @mbx: pointer to mailbox
  824. * @msg: message array to read
  825. *
  826. * This function will parse up the fields in the mailbox header and return
  827. * an error if the header contains any of a number of invalid configurations
  828. * including unrecognized type, invalid route, or a malformed message.
  829. **/
  830. static s32 fm10k_mbx_validate_msg_hdr(struct fm10k_mbx_info *mbx)
  831. {
  832. u16 type, rsvd0, head, tail, size;
  833. const u32 *hdr = &mbx->mbx_hdr;
  834. type = FM10K_MSG_HDR_FIELD_GET(*hdr, TYPE);
  835. rsvd0 = FM10K_MSG_HDR_FIELD_GET(*hdr, RSVD0);
  836. tail = FM10K_MSG_HDR_FIELD_GET(*hdr, TAIL);
  837. head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
  838. size = FM10K_MSG_HDR_FIELD_GET(*hdr, CONNECT_SIZE);
  839. if (rsvd0)
  840. return FM10K_MBX_ERR_RSVD0;
  841. switch (type) {
  842. case FM10K_MSG_DISCONNECT:
  843. /* validate that all data has been received */
  844. if (tail != mbx->head)
  845. return FM10K_MBX_ERR_TAIL;
  846. /* fall through */
  847. case FM10K_MSG_DATA:
  848. /* validate that head is moving correctly */
  849. if (!head || (head == FM10K_MSG_HDR_MASK(HEAD)))
  850. return FM10K_MBX_ERR_HEAD;
  851. if (fm10k_mbx_index_len(mbx, head, mbx->tail) > mbx->tail_len)
  852. return FM10K_MBX_ERR_HEAD;
  853. /* validate that tail is moving correctly */
  854. if (!tail || (tail == FM10K_MSG_HDR_MASK(TAIL)))
  855. return FM10K_MBX_ERR_TAIL;
  856. if (fm10k_mbx_index_len(mbx, mbx->head, tail) < mbx->mbmem_len)
  857. break;
  858. return FM10K_MBX_ERR_TAIL;
  859. case FM10K_MSG_CONNECT:
  860. /* validate size is in range and is power of 2 mask */
  861. if ((size < FM10K_VFMBX_MSG_MTU) || (size & (size + 1)))
  862. return FM10K_MBX_ERR_SIZE;
  863. /* fall through */
  864. case FM10K_MSG_ERROR:
  865. if (!head || (head == FM10K_MSG_HDR_MASK(HEAD)))
  866. return FM10K_MBX_ERR_HEAD;
  867. /* neither create nor error include a tail offset */
  868. if (tail)
  869. return FM10K_MBX_ERR_TAIL;
  870. break;
  871. default:
  872. return FM10K_MBX_ERR_TYPE;
  873. }
  874. return 0;
  875. }
  876. /**
  877. * fm10k_mbx_create_reply - Generate reply based on state and remote head
  878. * @mbx: pointer to mailbox
  879. * @head: acknowledgement number
  880. *
  881. * This function will generate an outgoing message based on the current
  882. * mailbox state and the remote fifo head. It will return the length
  883. * of the outgoing message excluding header on success, and a negative value
  884. * on error.
  885. **/
  886. static s32 fm10k_mbx_create_reply(struct fm10k_hw *hw,
  887. struct fm10k_mbx_info *mbx, u16 head)
  888. {
  889. switch (mbx->state) {
  890. case FM10K_STATE_OPEN:
  891. case FM10K_STATE_DISCONNECT:
  892. /* update our checksum for the outgoing data */
  893. fm10k_mbx_update_local_crc(mbx, head);
  894. /* as long as other end recognizes us keep sending data */
  895. fm10k_mbx_pull_head(hw, mbx, head);
  896. /* generate new header based on data */
  897. if (mbx->tail_len || (mbx->state == FM10K_STATE_OPEN))
  898. fm10k_mbx_create_data_hdr(mbx);
  899. else
  900. fm10k_mbx_create_disconnect_hdr(mbx);
  901. break;
  902. case FM10K_STATE_CONNECT:
  903. /* send disconnect even if we aren't connected */
  904. fm10k_mbx_create_connect_hdr(mbx);
  905. break;
  906. case FM10K_STATE_CLOSED:
  907. /* generate new header based on data */
  908. fm10k_mbx_create_disconnect_hdr(mbx);
  909. default:
  910. break;
  911. }
  912. return 0;
  913. }
  914. /**
  915. * fm10k_mbx_reset_work- Reset internal pointers for any pending work
  916. * @mbx: pointer to mailbox
  917. *
  918. * This function will reset all internal pointers so any work in progress
  919. * is dropped. This call should occur every time we transition from the
  920. * open state to the connect state.
  921. **/
  922. static void fm10k_mbx_reset_work(struct fm10k_mbx_info *mbx)
  923. {
  924. u16 len, head, ack;
  925. /* reset our outgoing max size back to Rx limits */
  926. mbx->max_size = mbx->rx.size - 1;
  927. /* update mbx->pulled to account for tail_len and ack */
  928. head = FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, HEAD);
  929. ack = fm10k_mbx_index_len(mbx, head, mbx->tail);
  930. mbx->pulled += mbx->tail_len - ack;
  931. /* now drop any messages which have started or finished transmitting */
  932. while (fm10k_fifo_head_len(&mbx->tx) && mbx->pulled) {
  933. len = fm10k_fifo_head_drop(&mbx->tx);
  934. mbx->tx_dropped++;
  935. if (mbx->pulled >= len)
  936. mbx->pulled -= len;
  937. else
  938. mbx->pulled = 0;
  939. }
  940. /* just do a quick resysnc to start of message */
  941. mbx->pushed = 0;
  942. mbx->pulled = 0;
  943. mbx->tail_len = 0;
  944. mbx->head_len = 0;
  945. mbx->rx.tail = 0;
  946. mbx->rx.head = 0;
  947. }
  948. /**
  949. * fm10k_mbx_update_max_size - Update the max_size and drop any large messages
  950. * @mbx: pointer to mailbox
  951. * @size: new value for max_size
  952. *
  953. * This function updates the max_size value and drops any outgoing messages
  954. * at the head of the Tx FIFO if they are larger than max_size. It does not
  955. * drop all messages, as this is too difficult to parse and remove them from
  956. * the FIFO. Instead, rely on the checking to ensure that messages larger
  957. * than max_size aren't pushed into the memory buffer.
  958. **/
  959. static void fm10k_mbx_update_max_size(struct fm10k_mbx_info *mbx, u16 size)
  960. {
  961. u16 len;
  962. mbx->max_size = size;
  963. /* flush any oversized messages from the queue */
  964. for (len = fm10k_fifo_head_len(&mbx->tx);
  965. len > size;
  966. len = fm10k_fifo_head_len(&mbx->tx)) {
  967. fm10k_fifo_head_drop(&mbx->tx);
  968. mbx->tx_dropped++;
  969. }
  970. }
  971. /**
  972. * fm10k_mbx_connect_reset - Reset following request for reset
  973. * @mbx: pointer to mailbox
  974. *
  975. * This function resets the mailbox to either a disconnected state
  976. * or a connect state depending on the current mailbox state
  977. **/
  978. static void fm10k_mbx_connect_reset(struct fm10k_mbx_info *mbx)
  979. {
  980. /* just do a quick resysnc to start of frame */
  981. fm10k_mbx_reset_work(mbx);
  982. /* reset CRC seeds */
  983. mbx->local = FM10K_MBX_CRC_SEED;
  984. mbx->remote = FM10K_MBX_CRC_SEED;
  985. /* we cannot exit connect until the size is good */
  986. if (mbx->state == FM10K_STATE_OPEN)
  987. mbx->state = FM10K_STATE_CONNECT;
  988. else
  989. mbx->state = FM10K_STATE_CLOSED;
  990. }
  991. /**
  992. * fm10k_mbx_process_connect - Process connect header
  993. * @mbx: pointer to mailbox
  994. * @msg: message array to process
  995. *
  996. * This function will read an incoming connect header and reply with the
  997. * appropriate message. It will return a value indicating the number of
  998. * data DWORDs on success, or will return a negative value on failure.
  999. **/
  1000. static s32 fm10k_mbx_process_connect(struct fm10k_hw *hw,
  1001. struct fm10k_mbx_info *mbx)
  1002. {
  1003. const enum fm10k_mbx_state state = mbx->state;
  1004. const u32 *hdr = &mbx->mbx_hdr;
  1005. u16 size, head;
  1006. /* we will need to pull all of the fields for verification */
  1007. size = FM10K_MSG_HDR_FIELD_GET(*hdr, CONNECT_SIZE);
  1008. head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
  1009. switch (state) {
  1010. case FM10K_STATE_DISCONNECT:
  1011. case FM10K_STATE_OPEN:
  1012. /* reset any in-progress work */
  1013. fm10k_mbx_connect_reset(mbx);
  1014. break;
  1015. case FM10K_STATE_CONNECT:
  1016. /* we cannot exit connect until the size is good */
  1017. if (size > mbx->rx.size) {
  1018. mbx->max_size = mbx->rx.size - 1;
  1019. } else {
  1020. /* record the remote system requesting connection */
  1021. mbx->state = FM10K_STATE_OPEN;
  1022. fm10k_mbx_update_max_size(mbx, size);
  1023. }
  1024. break;
  1025. default:
  1026. break;
  1027. }
  1028. /* align our tail index to remote head index */
  1029. mbx->tail = head;
  1030. return fm10k_mbx_create_reply(hw, mbx, head);
  1031. }
  1032. /**
  1033. * fm10k_mbx_process_data - Process data header
  1034. * @mbx: pointer to mailbox
  1035. *
  1036. * This function will read an incoming data header and reply with the
  1037. * appropriate message. It will return a value indicating the number of
  1038. * data DWORDs on success, or will return a negative value on failure.
  1039. **/
  1040. static s32 fm10k_mbx_process_data(struct fm10k_hw *hw,
  1041. struct fm10k_mbx_info *mbx)
  1042. {
  1043. const u32 *hdr = &mbx->mbx_hdr;
  1044. u16 head, tail;
  1045. s32 err;
  1046. /* we will need to pull all of the fields for verification */
  1047. head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
  1048. tail = FM10K_MSG_HDR_FIELD_GET(*hdr, TAIL);
  1049. /* if we are in connect just update our data and go */
  1050. if (mbx->state == FM10K_STATE_CONNECT) {
  1051. mbx->tail = head;
  1052. mbx->state = FM10K_STATE_OPEN;
  1053. }
  1054. /* abort on message size errors */
  1055. err = fm10k_mbx_push_tail(hw, mbx, tail);
  1056. if (err < 0)
  1057. return err;
  1058. /* verify the checksum on the incoming data */
  1059. err = fm10k_mbx_verify_remote_crc(mbx);
  1060. if (err)
  1061. return err;
  1062. /* process messages if we have received any */
  1063. fm10k_mbx_dequeue_rx(hw, mbx);
  1064. return fm10k_mbx_create_reply(hw, mbx, head);
  1065. }
  1066. /**
  1067. * fm10k_mbx_process_disconnect - Process disconnect header
  1068. * @mbx: pointer to mailbox
  1069. *
  1070. * This function will read an incoming disconnect header and reply with the
  1071. * appropriate message. It will return a value indicating the number of
  1072. * data DWORDs on success, or will return a negative value on failure.
  1073. **/
  1074. static s32 fm10k_mbx_process_disconnect(struct fm10k_hw *hw,
  1075. struct fm10k_mbx_info *mbx)
  1076. {
  1077. const enum fm10k_mbx_state state = mbx->state;
  1078. const u32 *hdr = &mbx->mbx_hdr;
  1079. u16 head;
  1080. s32 err;
  1081. /* we will need to pull the header field for verification */
  1082. head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
  1083. /* We should not be receiving disconnect if Rx is incomplete */
  1084. if (mbx->pushed)
  1085. return FM10K_MBX_ERR_TAIL;
  1086. /* we have already verified mbx->head == tail so we know this is 0 */
  1087. mbx->head_len = 0;
  1088. /* verify the checksum on the incoming header is correct */
  1089. err = fm10k_mbx_verify_remote_crc(mbx);
  1090. if (err)
  1091. return err;
  1092. switch (state) {
  1093. case FM10K_STATE_DISCONNECT:
  1094. case FM10K_STATE_OPEN:
  1095. /* state doesn't change if we still have work to do */
  1096. if (!fm10k_mbx_tx_complete(mbx))
  1097. break;
  1098. /* verify the head indicates we completed all transmits */
  1099. if (head != mbx->tail)
  1100. return FM10K_MBX_ERR_HEAD;
  1101. /* reset any in-progress work */
  1102. fm10k_mbx_connect_reset(mbx);
  1103. break;
  1104. default:
  1105. break;
  1106. }
  1107. return fm10k_mbx_create_reply(hw, mbx, head);
  1108. }
  1109. /**
  1110. * fm10k_mbx_process_error - Process error header
  1111. * @mbx: pointer to mailbox
  1112. *
  1113. * This function will read an incoming error header and reply with the
  1114. * appropriate message. It will return a value indicating the number of
  1115. * data DWORDs on success, or will return a negative value on failure.
  1116. **/
  1117. static s32 fm10k_mbx_process_error(struct fm10k_hw *hw,
  1118. struct fm10k_mbx_info *mbx)
  1119. {
  1120. const u32 *hdr = &mbx->mbx_hdr;
  1121. u16 head;
  1122. /* we will need to pull all of the fields for verification */
  1123. head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
  1124. switch (mbx->state) {
  1125. case FM10K_STATE_OPEN:
  1126. case FM10K_STATE_DISCONNECT:
  1127. /* flush any uncompleted work */
  1128. fm10k_mbx_reset_work(mbx);
  1129. /* reset CRC seeds */
  1130. mbx->local = FM10K_MBX_CRC_SEED;
  1131. mbx->remote = FM10K_MBX_CRC_SEED;
  1132. /* reset tail index and size to prepare for reconnect */
  1133. mbx->tail = head;
  1134. /* if open then reset max_size and go back to connect */
  1135. if (mbx->state == FM10K_STATE_OPEN) {
  1136. mbx->state = FM10K_STATE_CONNECT;
  1137. break;
  1138. }
  1139. /* send a connect message to get data flowing again */
  1140. fm10k_mbx_create_connect_hdr(mbx);
  1141. return 0;
  1142. default:
  1143. break;
  1144. }
  1145. return fm10k_mbx_create_reply(hw, mbx, mbx->tail);
  1146. }
  1147. /**
  1148. * fm10k_mbx_process - Process mailbox interrupt
  1149. * @hw: pointer to hardware structure
  1150. * @mbx: pointer to mailbox
  1151. *
  1152. * This function will process incoming mailbox events and generate mailbox
  1153. * replies. It will return a value indicating the number of DWORDs
  1154. * transmitted excluding header on success or a negative value on error.
  1155. **/
  1156. static s32 fm10k_mbx_process(struct fm10k_hw *hw,
  1157. struct fm10k_mbx_info *mbx)
  1158. {
  1159. s32 err;
  1160. /* we do not read mailbox if closed */
  1161. if (mbx->state == FM10K_STATE_CLOSED)
  1162. return 0;
  1163. /* copy data from mailbox */
  1164. err = fm10k_mbx_read(hw, mbx);
  1165. if (err)
  1166. return err;
  1167. /* validate type, source, and destination */
  1168. err = fm10k_mbx_validate_msg_hdr(mbx);
  1169. if (err < 0)
  1170. goto msg_err;
  1171. switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, TYPE)) {
  1172. case FM10K_MSG_CONNECT:
  1173. err = fm10k_mbx_process_connect(hw, mbx);
  1174. break;
  1175. case FM10K_MSG_DATA:
  1176. err = fm10k_mbx_process_data(hw, mbx);
  1177. break;
  1178. case FM10K_MSG_DISCONNECT:
  1179. err = fm10k_mbx_process_disconnect(hw, mbx);
  1180. break;
  1181. case FM10K_MSG_ERROR:
  1182. err = fm10k_mbx_process_error(hw, mbx);
  1183. break;
  1184. default:
  1185. err = FM10K_MBX_ERR_TYPE;
  1186. break;
  1187. }
  1188. msg_err:
  1189. /* notify partner of errors on our end */
  1190. if (err < 0)
  1191. fm10k_mbx_create_error_msg(mbx, err);
  1192. /* copy data from mailbox */
  1193. fm10k_mbx_write(hw, mbx);
  1194. return err;
  1195. }
  1196. /**
  1197. * fm10k_mbx_disconnect - Shutdown mailbox connection
  1198. * @hw: pointer to hardware structure
  1199. * @mbx: pointer to mailbox
  1200. *
  1201. * This function will shut down the mailbox. It places the mailbox first
  1202. * in the disconnect state, it then allows up to a predefined timeout for
  1203. * the mailbox to transition to close on its own. If this does not occur
  1204. * then the mailbox will be forced into the closed state.
  1205. *
  1206. * Any mailbox transactions not completed before calling this function
  1207. * are not guaranteed to complete and may be dropped.
  1208. **/
  1209. static void fm10k_mbx_disconnect(struct fm10k_hw *hw,
  1210. struct fm10k_mbx_info *mbx)
  1211. {
  1212. int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
  1213. /* Place mbx in ready to disconnect state */
  1214. mbx->state = FM10K_STATE_DISCONNECT;
  1215. /* trigger interrupt to start shutdown process */
  1216. fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
  1217. FM10K_MBX_INTERRUPT_DISABLE);
  1218. do {
  1219. udelay(FM10K_MBX_POLL_DELAY);
  1220. mbx->ops.process(hw, mbx);
  1221. timeout -= FM10K_MBX_POLL_DELAY;
  1222. } while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
  1223. /* in case we didn't close, just force the mailbox into shutdown and
  1224. * drop all left over messages in the FIFO.
  1225. */
  1226. fm10k_mbx_connect_reset(mbx);
  1227. fm10k_fifo_drop_all(&mbx->tx);
  1228. fm10k_write_reg(hw, mbx->mbmem_reg, 0);
  1229. }
  1230. /**
  1231. * fm10k_mbx_connect - Start mailbox connection
  1232. * @hw: pointer to hardware structure
  1233. * @mbx: pointer to mailbox
  1234. *
  1235. * This function will initiate a mailbox connection. It will populate the
  1236. * mailbox with a broadcast connect message and then initialize the lock.
  1237. * This is safe since the connect message is a single DWORD so the mailbox
  1238. * transaction is guaranteed to be atomic.
  1239. *
  1240. * This function will return an error if the mailbox has not been initiated
  1241. * or is currently in use.
  1242. **/
  1243. static s32 fm10k_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
  1244. {
  1245. /* we cannot connect an uninitialized mailbox */
  1246. if (!mbx->rx.buffer)
  1247. return FM10K_MBX_ERR_NO_SPACE;
  1248. /* we cannot connect an already connected mailbox */
  1249. if (mbx->state != FM10K_STATE_CLOSED)
  1250. return FM10K_MBX_ERR_BUSY;
  1251. /* mailbox timeout can now become active */
  1252. mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
  1253. /* Place mbx in ready to connect state */
  1254. mbx->state = FM10K_STATE_CONNECT;
  1255. fm10k_mbx_reset_work(mbx);
  1256. /* initialize header of remote mailbox */
  1257. fm10k_mbx_create_fake_disconnect_hdr(mbx);
  1258. fm10k_write_reg(hw, mbx->mbmem_reg ^ mbx->mbmem_len, mbx->mbx_hdr);
  1259. /* enable interrupt and notify other party of new message */
  1260. mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
  1261. FM10K_MBX_INTERRUPT_ENABLE;
  1262. /* generate and load connect header into mailbox */
  1263. fm10k_mbx_create_connect_hdr(mbx);
  1264. fm10k_mbx_write(hw, mbx);
  1265. return 0;
  1266. }
  1267. /**
  1268. * fm10k_mbx_validate_handlers - Validate layout of message parsing data
  1269. * @msg_data: handlers for mailbox events
  1270. *
  1271. * This function validates the layout of the message parsing data. This
  1272. * should be mostly static, but it is important to catch any errors that
  1273. * are made when constructing the parsers.
  1274. **/
  1275. static s32 fm10k_mbx_validate_handlers(const struct fm10k_msg_data *msg_data)
  1276. {
  1277. const struct fm10k_tlv_attr *attr;
  1278. unsigned int id;
  1279. /* Allow NULL mailboxes that transmit but don't receive */
  1280. if (!msg_data)
  1281. return 0;
  1282. while (msg_data->id != FM10K_TLV_ERROR) {
  1283. /* all messages should have a function handler */
  1284. if (!msg_data->func)
  1285. return FM10K_ERR_PARAM;
  1286. /* parser is optional */
  1287. attr = msg_data->attr;
  1288. if (attr) {
  1289. while (attr->id != FM10K_TLV_ERROR) {
  1290. id = attr->id;
  1291. attr++;
  1292. /* ID should always be increasing */
  1293. if (id >= attr->id)
  1294. return FM10K_ERR_PARAM;
  1295. /* ID should fit in results array */
  1296. if (id >= FM10K_TLV_RESULTS_MAX)
  1297. return FM10K_ERR_PARAM;
  1298. }
  1299. /* verify terminator is in the list */
  1300. if (attr->id != FM10K_TLV_ERROR)
  1301. return FM10K_ERR_PARAM;
  1302. }
  1303. id = msg_data->id;
  1304. msg_data++;
  1305. /* ID should always be increasing */
  1306. if (id >= msg_data->id)
  1307. return FM10K_ERR_PARAM;
  1308. }
  1309. /* verify terminator is in the list */
  1310. if ((msg_data->id != FM10K_TLV_ERROR) || !msg_data->func)
  1311. return FM10K_ERR_PARAM;
  1312. return 0;
  1313. }
  1314. /**
  1315. * fm10k_mbx_register_handlers - Register a set of handler ops for mailbox
  1316. * @mbx: pointer to mailbox
  1317. * @msg_data: handlers for mailbox events
  1318. *
  1319. * This function associates a set of message handling ops with a mailbox.
  1320. **/
  1321. static s32 fm10k_mbx_register_handlers(struct fm10k_mbx_info *mbx,
  1322. const struct fm10k_msg_data *msg_data)
  1323. {
  1324. /* validate layout of handlers before assigning them */
  1325. if (fm10k_mbx_validate_handlers(msg_data))
  1326. return FM10K_ERR_PARAM;
  1327. /* initialize the message handlers */
  1328. mbx->msg_data = msg_data;
  1329. return 0;
  1330. }
  1331. /**
  1332. * fm10k_pfvf_mbx_init - Initialize mailbox memory for PF/VF mailbox
  1333. * @hw: pointer to hardware structure
  1334. * @mbx: pointer to mailbox
  1335. * @msg_data: handlers for mailbox events
  1336. * @id: ID reference for PF as it supports up to 64 PF/VF mailboxes
  1337. *
  1338. * This function initializes the mailbox for use. It will split the
  1339. * buffer provided an use that th populate both the Tx and Rx FIFO by
  1340. * evenly splitting it. In order to allow for easy masking of head/tail
  1341. * the value reported in size must be a power of 2 and is reported in
  1342. * DWORDs, not bytes. Any invalid values will cause the mailbox to return
  1343. * error.
  1344. **/
  1345. s32 fm10k_pfvf_mbx_init(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx,
  1346. const struct fm10k_msg_data *msg_data, u8 id)
  1347. {
  1348. /* initialize registers */
  1349. switch (hw->mac.type) {
  1350. case fm10k_mac_vf:
  1351. mbx->mbx_reg = FM10K_VFMBX;
  1352. mbx->mbmem_reg = FM10K_VFMBMEM(FM10K_VFMBMEM_VF_XOR);
  1353. break;
  1354. case fm10k_mac_pf:
  1355. /* there are only 64 VF <-> PF mailboxes */
  1356. if (id < 64) {
  1357. mbx->mbx_reg = FM10K_MBX(id);
  1358. mbx->mbmem_reg = FM10K_MBMEM_VF(id, 0);
  1359. break;
  1360. }
  1361. /* fallthough */
  1362. default:
  1363. return FM10K_MBX_ERR_NO_MBX;
  1364. }
  1365. /* start out in closed state */
  1366. mbx->state = FM10K_STATE_CLOSED;
  1367. /* validate layout of handlers before assigning them */
  1368. if (fm10k_mbx_validate_handlers(msg_data))
  1369. return FM10K_ERR_PARAM;
  1370. /* initialize the message handlers */
  1371. mbx->msg_data = msg_data;
  1372. /* start mailbox as timed out and let the reset_hw call
  1373. * set the timeout value to begin communications
  1374. */
  1375. mbx->timeout = 0;
  1376. mbx->udelay = FM10K_MBX_INIT_DELAY;
  1377. /* initialize tail and head */
  1378. mbx->tail = 1;
  1379. mbx->head = 1;
  1380. /* initialize CRC seeds */
  1381. mbx->local = FM10K_MBX_CRC_SEED;
  1382. mbx->remote = FM10K_MBX_CRC_SEED;
  1383. /* Split buffer for use by Tx/Rx FIFOs */
  1384. mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
  1385. mbx->mbmem_len = FM10K_VFMBMEM_VF_XOR;
  1386. /* initialize the FIFOs, sizes are in 4 byte increments */
  1387. fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
  1388. fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
  1389. FM10K_MBX_RX_BUFFER_SIZE);
  1390. /* initialize function pointers */
  1391. mbx->ops.connect = fm10k_mbx_connect;
  1392. mbx->ops.disconnect = fm10k_mbx_disconnect;
  1393. mbx->ops.rx_ready = fm10k_mbx_rx_ready;
  1394. mbx->ops.tx_ready = fm10k_mbx_tx_ready;
  1395. mbx->ops.tx_complete = fm10k_mbx_tx_complete;
  1396. mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
  1397. mbx->ops.process = fm10k_mbx_process;
  1398. mbx->ops.register_handlers = fm10k_mbx_register_handlers;
  1399. return 0;
  1400. }
  1401. /**
  1402. * fm10k_sm_mbx_create_data_hdr - Generate a mailbox header for local FIFO
  1403. * @mbx: pointer to mailbox
  1404. *
  1405. * This function returns a connection mailbox header
  1406. **/
  1407. static void fm10k_sm_mbx_create_data_hdr(struct fm10k_mbx_info *mbx)
  1408. {
  1409. if (mbx->tail_len)
  1410. mbx->mbx_lock |= FM10K_MBX_REQ;
  1411. mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
  1412. FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
  1413. FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD);
  1414. }
  1415. /**
  1416. * fm10k_sm_mbx_create_connect_hdr - Generate a mailbox header for local FIFO
  1417. * @mbx: pointer to mailbox
  1418. * @err: error flags to report if any
  1419. *
  1420. * This function returns a connection mailbox header
  1421. **/
  1422. static void fm10k_sm_mbx_create_connect_hdr(struct fm10k_mbx_info *mbx, u8 err)
  1423. {
  1424. if (mbx->local)
  1425. mbx->mbx_lock |= FM10K_MBX_REQ;
  1426. mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
  1427. FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
  1428. FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD) |
  1429. FM10K_MSG_HDR_FIELD_SET(err, SM_ERR);
  1430. }
  1431. /**
  1432. * fm10k_sm_mbx_connect_reset - Reset following request for reset
  1433. * @mbx: pointer to mailbox
  1434. *
  1435. * This function resets the mailbox to a just connected state
  1436. **/
  1437. static void fm10k_sm_mbx_connect_reset(struct fm10k_mbx_info *mbx)
  1438. {
  1439. /* flush any uncompleted work */
  1440. fm10k_mbx_reset_work(mbx);
  1441. /* set local version to max and remote version to 0 */
  1442. mbx->local = FM10K_SM_MBX_VERSION;
  1443. mbx->remote = 0;
  1444. /* initialize tail and head */
  1445. mbx->tail = 1;
  1446. mbx->head = 1;
  1447. /* reset state back to connect */
  1448. mbx->state = FM10K_STATE_CONNECT;
  1449. }
  1450. /**
  1451. * fm10k_sm_mbx_connect - Start switch manager mailbox connection
  1452. * @hw: pointer to hardware structure
  1453. * @mbx: pointer to mailbox
  1454. *
  1455. * This function will initiate a mailbox connection with the switch
  1456. * manager. To do this it will first disconnect the mailbox, and then
  1457. * reconnect it in order to complete a reset of the mailbox.
  1458. *
  1459. * This function will return an error if the mailbox has not been initiated
  1460. * or is currently in use.
  1461. **/
  1462. static s32 fm10k_sm_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
  1463. {
  1464. /* we cannot connect an uninitialized mailbox */
  1465. if (!mbx->rx.buffer)
  1466. return FM10K_MBX_ERR_NO_SPACE;
  1467. /* we cannot connect an already connected mailbox */
  1468. if (mbx->state != FM10K_STATE_CLOSED)
  1469. return FM10K_MBX_ERR_BUSY;
  1470. /* mailbox timeout can now become active */
  1471. mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
  1472. /* Place mbx in ready to connect state */
  1473. mbx->state = FM10K_STATE_CONNECT;
  1474. mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
  1475. /* reset interface back to connect */
  1476. fm10k_sm_mbx_connect_reset(mbx);
  1477. /* enable interrupt and notify other party of new message */
  1478. mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
  1479. FM10K_MBX_INTERRUPT_ENABLE;
  1480. /* generate and load connect header into mailbox */
  1481. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1482. fm10k_mbx_write(hw, mbx);
  1483. /* enable interrupt and notify other party of new message */
  1484. return 0;
  1485. }
  1486. /**
  1487. * fm10k_sm_mbx_disconnect - Shutdown mailbox connection
  1488. * @hw: pointer to hardware structure
  1489. * @mbx: pointer to mailbox
  1490. *
  1491. * This function will shut down the mailbox. It places the mailbox first
  1492. * in the disconnect state, it then allows up to a predefined timeout for
  1493. * the mailbox to transition to close on its own. If this does not occur
  1494. * then the mailbox will be forced into the closed state.
  1495. *
  1496. * Any mailbox transactions not completed before calling this function
  1497. * are not guaranteed to complete and may be dropped.
  1498. **/
  1499. static void fm10k_sm_mbx_disconnect(struct fm10k_hw *hw,
  1500. struct fm10k_mbx_info *mbx)
  1501. {
  1502. int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
  1503. /* Place mbx in ready to disconnect state */
  1504. mbx->state = FM10K_STATE_DISCONNECT;
  1505. /* trigger interrupt to start shutdown process */
  1506. fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
  1507. FM10K_MBX_INTERRUPT_DISABLE);
  1508. do {
  1509. udelay(FM10K_MBX_POLL_DELAY);
  1510. mbx->ops.process(hw, mbx);
  1511. timeout -= FM10K_MBX_POLL_DELAY;
  1512. } while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
  1513. /* in case we didn't close just force the mailbox into shutdown */
  1514. mbx->state = FM10K_STATE_CLOSED;
  1515. mbx->remote = 0;
  1516. fm10k_mbx_reset_work(mbx);
  1517. fm10k_fifo_drop_all(&mbx->tx);
  1518. fm10k_write_reg(hw, mbx->mbmem_reg, 0);
  1519. }
  1520. /**
  1521. * fm10k_mbx_validate_fifo_hdr - Validate fields in the remote FIFO header
  1522. * @mbx: pointer to mailbox
  1523. *
  1524. * This function will parse up the fields in the mailbox header and return
  1525. * an error if the header contains any of a number of invalid configurations
  1526. * including unrecognized offsets or version numbers.
  1527. **/
  1528. static s32 fm10k_sm_mbx_validate_fifo_hdr(struct fm10k_mbx_info *mbx)
  1529. {
  1530. const u32 *hdr = &mbx->mbx_hdr;
  1531. u16 tail, head, ver;
  1532. tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
  1533. ver = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_VER);
  1534. head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
  1535. switch (ver) {
  1536. case 0:
  1537. break;
  1538. case FM10K_SM_MBX_VERSION:
  1539. if (!head || head > FM10K_SM_MBX_FIFO_LEN)
  1540. return FM10K_MBX_ERR_HEAD;
  1541. if (!tail || tail > FM10K_SM_MBX_FIFO_LEN)
  1542. return FM10K_MBX_ERR_TAIL;
  1543. if (mbx->tail < head)
  1544. head += mbx->mbmem_len - 1;
  1545. if (tail < mbx->head)
  1546. tail += mbx->mbmem_len - 1;
  1547. if (fm10k_mbx_index_len(mbx, head, mbx->tail) > mbx->tail_len)
  1548. return FM10K_MBX_ERR_HEAD;
  1549. if (fm10k_mbx_index_len(mbx, mbx->head, tail) < mbx->mbmem_len)
  1550. break;
  1551. return FM10K_MBX_ERR_TAIL;
  1552. default:
  1553. return FM10K_MBX_ERR_SRC;
  1554. }
  1555. return 0;
  1556. }
  1557. /**
  1558. * fm10k_sm_mbx_process_error - Process header with error flag set
  1559. * @mbx: pointer to mailbox
  1560. *
  1561. * This function is meant to respond to a request where the error flag
  1562. * is set. As a result we will terminate a connection if one is present
  1563. * and fall back into the reset state with a connection header of version
  1564. * 0 (RESET).
  1565. **/
  1566. static void fm10k_sm_mbx_process_error(struct fm10k_mbx_info *mbx)
  1567. {
  1568. const enum fm10k_mbx_state state = mbx->state;
  1569. switch (state) {
  1570. case FM10K_STATE_DISCONNECT:
  1571. /* if there is an error just disconnect */
  1572. mbx->remote = 0;
  1573. break;
  1574. case FM10K_STATE_OPEN:
  1575. /* flush any uncompleted work */
  1576. fm10k_sm_mbx_connect_reset(mbx);
  1577. break;
  1578. case FM10K_STATE_CONNECT:
  1579. /* try connnecting at lower version */
  1580. if (mbx->remote) {
  1581. while (mbx->local > 1)
  1582. mbx->local--;
  1583. mbx->remote = 0;
  1584. }
  1585. break;
  1586. default:
  1587. break;
  1588. }
  1589. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1590. }
  1591. /**
  1592. * fm10k_sm_mbx_create_error_message - Process an error in FIFO hdr
  1593. * @mbx: pointer to mailbox
  1594. * @err: local error encountered
  1595. *
  1596. * This function will interpret the error provided by err, and based on
  1597. * that it may set the error bit in the local message header
  1598. **/
  1599. static void fm10k_sm_mbx_create_error_msg(struct fm10k_mbx_info *mbx, s32 err)
  1600. {
  1601. /* only generate an error message for these types */
  1602. switch (err) {
  1603. case FM10K_MBX_ERR_TAIL:
  1604. case FM10K_MBX_ERR_HEAD:
  1605. case FM10K_MBX_ERR_SRC:
  1606. case FM10K_MBX_ERR_SIZE:
  1607. case FM10K_MBX_ERR_RSVD0:
  1608. break;
  1609. default:
  1610. return;
  1611. }
  1612. /* process it as though we received an error, and send error reply */
  1613. fm10k_sm_mbx_process_error(mbx);
  1614. fm10k_sm_mbx_create_connect_hdr(mbx, 1);
  1615. }
  1616. /**
  1617. * fm10k_sm_mbx_receive - Take message from Rx mailbox FIFO and put it in Rx
  1618. * @hw: pointer to hardware structure
  1619. * @mbx: pointer to mailbox
  1620. *
  1621. * This function will dequeue one message from the Rx switch manager mailbox
  1622. * FIFO and place it in the Rx mailbox FIFO for processing by software.
  1623. **/
  1624. static s32 fm10k_sm_mbx_receive(struct fm10k_hw *hw,
  1625. struct fm10k_mbx_info *mbx,
  1626. u16 tail)
  1627. {
  1628. /* reduce length by 1 to convert to a mask */
  1629. u16 mbmem_len = mbx->mbmem_len - 1;
  1630. s32 err;
  1631. /* push tail in front of head */
  1632. if (tail < mbx->head)
  1633. tail += mbmem_len;
  1634. /* copy data to the Rx FIFO */
  1635. err = fm10k_mbx_push_tail(hw, mbx, tail);
  1636. if (err < 0)
  1637. return err;
  1638. /* process messages if we have received any */
  1639. fm10k_mbx_dequeue_rx(hw, mbx);
  1640. /* guarantee head aligns with the end of the last message */
  1641. mbx->head = fm10k_mbx_head_sub(mbx, mbx->pushed);
  1642. mbx->pushed = 0;
  1643. /* clear any extra bits left over since index adds 1 extra bit */
  1644. if (mbx->head > mbmem_len)
  1645. mbx->head -= mbmem_len;
  1646. return err;
  1647. }
  1648. /**
  1649. * fm10k_sm_mbx_transmit - Take message from Tx and put it in Tx mailbox FIFO
  1650. * @hw: pointer to hardware structure
  1651. * @mbx: pointer to mailbox
  1652. *
  1653. * This function will dequeue one message from the Tx mailbox FIFO and place
  1654. * it in the Tx switch manager mailbox FIFO for processing by hardware.
  1655. **/
  1656. static void fm10k_sm_mbx_transmit(struct fm10k_hw *hw,
  1657. struct fm10k_mbx_info *mbx, u16 head)
  1658. {
  1659. struct fm10k_mbx_fifo *fifo = &mbx->tx;
  1660. /* reduce length by 1 to convert to a mask */
  1661. u16 mbmem_len = mbx->mbmem_len - 1;
  1662. u16 tail_len, len = 0;
  1663. u32 *msg;
  1664. /* push head behind tail */
  1665. if (mbx->tail < head)
  1666. head += mbmem_len;
  1667. fm10k_mbx_pull_head(hw, mbx, head);
  1668. /* determine msg aligned offset for end of buffer */
  1669. do {
  1670. msg = fifo->buffer + fm10k_fifo_head_offset(fifo, len);
  1671. tail_len = len;
  1672. len += FM10K_TLV_DWORD_LEN(*msg);
  1673. } while ((len <= mbx->tail_len) && (len < mbmem_len));
  1674. /* guarantee we stop on a message boundary */
  1675. if (mbx->tail_len > tail_len) {
  1676. mbx->tail = fm10k_mbx_tail_sub(mbx, mbx->tail_len - tail_len);
  1677. mbx->tail_len = tail_len;
  1678. }
  1679. /* clear any extra bits left over since index adds 1 extra bit */
  1680. if (mbx->tail > mbmem_len)
  1681. mbx->tail -= mbmem_len;
  1682. }
  1683. /**
  1684. * fm10k_sm_mbx_create_reply - Generate reply based on state and remote head
  1685. * @mbx: pointer to mailbox
  1686. * @head: acknowledgement number
  1687. *
  1688. * This function will generate an outgoing message based on the current
  1689. * mailbox state and the remote fifo head. It will return the length
  1690. * of the outgoing message excluding header on success, and a negative value
  1691. * on error.
  1692. **/
  1693. static void fm10k_sm_mbx_create_reply(struct fm10k_hw *hw,
  1694. struct fm10k_mbx_info *mbx, u16 head)
  1695. {
  1696. switch (mbx->state) {
  1697. case FM10K_STATE_OPEN:
  1698. case FM10K_STATE_DISCONNECT:
  1699. /* flush out Tx data */
  1700. fm10k_sm_mbx_transmit(hw, mbx, head);
  1701. /* generate new header based on data */
  1702. if (mbx->tail_len || (mbx->state == FM10K_STATE_OPEN)) {
  1703. fm10k_sm_mbx_create_data_hdr(mbx);
  1704. } else {
  1705. mbx->remote = 0;
  1706. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1707. }
  1708. break;
  1709. case FM10K_STATE_CONNECT:
  1710. case FM10K_STATE_CLOSED:
  1711. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1712. break;
  1713. default:
  1714. break;
  1715. }
  1716. }
  1717. /**
  1718. * fm10k_sm_mbx_process_reset - Process header with version == 0 (RESET)
  1719. * @hw: pointer to hardware structure
  1720. * @mbx: pointer to mailbox
  1721. *
  1722. * This function is meant to respond to a request where the version data
  1723. * is set to 0. As such we will either terminate the connection or go
  1724. * into the connect state in order to re-establish the connection. This
  1725. * function can also be used to respond to an error as the connection
  1726. * resetting would also be a means of dealing with errors.
  1727. **/
  1728. static void fm10k_sm_mbx_process_reset(struct fm10k_hw *hw,
  1729. struct fm10k_mbx_info *mbx)
  1730. {
  1731. const enum fm10k_mbx_state state = mbx->state;
  1732. switch (state) {
  1733. case FM10K_STATE_DISCONNECT:
  1734. /* drop remote connections and disconnect */
  1735. mbx->state = FM10K_STATE_CLOSED;
  1736. mbx->remote = 0;
  1737. mbx->local = 0;
  1738. break;
  1739. case FM10K_STATE_OPEN:
  1740. /* flush any incomplete work */
  1741. fm10k_sm_mbx_connect_reset(mbx);
  1742. break;
  1743. case FM10K_STATE_CONNECT:
  1744. /* Update remote value to match local value */
  1745. mbx->remote = mbx->local;
  1746. default:
  1747. break;
  1748. }
  1749. fm10k_sm_mbx_create_reply(hw, mbx, mbx->tail);
  1750. }
  1751. /**
  1752. * fm10k_sm_mbx_process_version_1 - Process header with version == 1
  1753. * @hw: pointer to hardware structure
  1754. * @mbx: pointer to mailbox
  1755. *
  1756. * This function is meant to process messages received when the remote
  1757. * mailbox is active.
  1758. **/
  1759. static s32 fm10k_sm_mbx_process_version_1(struct fm10k_hw *hw,
  1760. struct fm10k_mbx_info *mbx)
  1761. {
  1762. const u32 *hdr = &mbx->mbx_hdr;
  1763. u16 head, tail;
  1764. s32 len;
  1765. /* pull all fields needed for verification */
  1766. tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
  1767. head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
  1768. /* if we are in connect and wanting version 1 then start up and go */
  1769. if (mbx->state == FM10K_STATE_CONNECT) {
  1770. if (!mbx->remote)
  1771. goto send_reply;
  1772. if (mbx->remote != 1)
  1773. return FM10K_MBX_ERR_SRC;
  1774. mbx->state = FM10K_STATE_OPEN;
  1775. }
  1776. do {
  1777. /* abort on message size errors */
  1778. len = fm10k_sm_mbx_receive(hw, mbx, tail);
  1779. if (len < 0)
  1780. return len;
  1781. /* continue until we have flushed the Rx FIFO */
  1782. } while (len);
  1783. send_reply:
  1784. fm10k_sm_mbx_create_reply(hw, mbx, head);
  1785. return 0;
  1786. }
  1787. /**
  1788. * fm10k_sm_mbx_process - Process mailbox switch mailbox interrupt
  1789. * @hw: pointer to hardware structure
  1790. * @mbx: pointer to mailbox
  1791. *
  1792. * This function will process incoming mailbox events and generate mailbox
  1793. * replies. It will return a value indicating the number of DWORDs
  1794. * transmitted excluding header on success or a negative value on error.
  1795. **/
  1796. static s32 fm10k_sm_mbx_process(struct fm10k_hw *hw,
  1797. struct fm10k_mbx_info *mbx)
  1798. {
  1799. s32 err;
  1800. /* we do not read mailbox if closed */
  1801. if (mbx->state == FM10K_STATE_CLOSED)
  1802. return 0;
  1803. /* retrieve data from switch manager */
  1804. err = fm10k_mbx_read(hw, mbx);
  1805. if (err)
  1806. return err;
  1807. err = fm10k_sm_mbx_validate_fifo_hdr(mbx);
  1808. if (err < 0)
  1809. goto fifo_err;
  1810. if (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_ERR)) {
  1811. fm10k_sm_mbx_process_error(mbx);
  1812. goto fifo_err;
  1813. }
  1814. switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_VER)) {
  1815. case 0:
  1816. fm10k_sm_mbx_process_reset(hw, mbx);
  1817. break;
  1818. case FM10K_SM_MBX_VERSION:
  1819. err = fm10k_sm_mbx_process_version_1(hw, mbx);
  1820. break;
  1821. }
  1822. fifo_err:
  1823. if (err < 0)
  1824. fm10k_sm_mbx_create_error_msg(mbx, err);
  1825. /* report data to switch manager */
  1826. fm10k_mbx_write(hw, mbx);
  1827. return err;
  1828. }
  1829. /**
  1830. * fm10k_sm_mbx_init - Initialize mailbox memory for PF/SM mailbox
  1831. * @hw: pointer to hardware structure
  1832. * @mbx: pointer to mailbox
  1833. * @msg_data: handlers for mailbox events
  1834. *
  1835. * This function for now is used to stub out the PF/SM mailbox
  1836. **/
  1837. s32 fm10k_sm_mbx_init(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx,
  1838. const struct fm10k_msg_data *msg_data)
  1839. {
  1840. mbx->mbx_reg = FM10K_GMBX;
  1841. mbx->mbmem_reg = FM10K_MBMEM_PF(0);
  1842. /* start out in closed state */
  1843. mbx->state = FM10K_STATE_CLOSED;
  1844. /* validate layout of handlers before assigning them */
  1845. if (fm10k_mbx_validate_handlers(msg_data))
  1846. return FM10K_ERR_PARAM;
  1847. /* initialize the message handlers */
  1848. mbx->msg_data = msg_data;
  1849. /* start mailbox as timed out and let the reset_hw call
  1850. * set the timeout value to begin communications
  1851. */
  1852. mbx->timeout = 0;
  1853. mbx->udelay = FM10K_MBX_INIT_DELAY;
  1854. /* Split buffer for use by Tx/Rx FIFOs */
  1855. mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
  1856. mbx->mbmem_len = FM10K_MBMEM_PF_XOR;
  1857. /* initialize the FIFOs, sizes are in 4 byte increments */
  1858. fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
  1859. fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
  1860. FM10K_MBX_RX_BUFFER_SIZE);
  1861. /* initialize function pointers */
  1862. mbx->ops.connect = fm10k_sm_mbx_connect;
  1863. mbx->ops.disconnect = fm10k_sm_mbx_disconnect;
  1864. mbx->ops.rx_ready = fm10k_mbx_rx_ready;
  1865. mbx->ops.tx_ready = fm10k_mbx_tx_ready;
  1866. mbx->ops.tx_complete = fm10k_mbx_tx_complete;
  1867. mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
  1868. mbx->ops.process = fm10k_sm_mbx_process;
  1869. mbx->ops.register_handlers = fm10k_mbx_register_handlers;
  1870. return 0;
  1871. }