fm10k_mbx.c 62 KB

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