fm10k_mbx.c 62 KB

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