fm10k_mbx.c 63 KB

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