fm10k_mbx.c 63 KB

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