fm10k_mbx.c 61 KB

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