fm10k_mbx.c 61 KB

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