fm10k_mbx.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  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 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_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 section 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 section 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 acknowledgment 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 treat 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 recipient 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;
  1035. s32 err;
  1036. /* we will need to pull the header field for verification */
  1037. head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
  1038. /* We should not be receiving disconnect if Rx is incomplete */
  1039. if (mbx->pushed)
  1040. return FM10K_MBX_ERR_TAIL;
  1041. /* we have already verified mbx->head == tail so we know this is 0 */
  1042. mbx->head_len = 0;
  1043. /* verify the checksum on the incoming header is correct */
  1044. err = fm10k_mbx_verify_remote_crc(mbx);
  1045. if (err)
  1046. return err;
  1047. switch (state) {
  1048. case FM10K_STATE_DISCONNECT:
  1049. case FM10K_STATE_OPEN:
  1050. /* state doesn't change if we still have work to do */
  1051. if (!fm10k_mbx_tx_complete(mbx))
  1052. break;
  1053. /* verify the head indicates we completed all transmits */
  1054. if (head != mbx->tail)
  1055. return FM10K_MBX_ERR_HEAD;
  1056. /* reset any in-progress work */
  1057. fm10k_mbx_connect_reset(mbx);
  1058. break;
  1059. default:
  1060. break;
  1061. }
  1062. return fm10k_mbx_create_reply(hw, mbx, head);
  1063. }
  1064. /**
  1065. * fm10k_mbx_process_error - Process error header
  1066. * @mbx: pointer to mailbox
  1067. *
  1068. * This function will read an incoming error header and reply with the
  1069. * appropriate message. It will return a value indicating the number of
  1070. * data DWORDs on success, or will return a negative value on failure.
  1071. **/
  1072. static s32 fm10k_mbx_process_error(struct fm10k_hw *hw,
  1073. struct fm10k_mbx_info *mbx)
  1074. {
  1075. const u32 *hdr = &mbx->mbx_hdr;
  1076. s32 err_no;
  1077. u16 head;
  1078. /* we will need to pull all of the fields for verification */
  1079. head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
  1080. /* we only have lower 10 bits of error number so add upper bits */
  1081. err_no = FM10K_MSG_HDR_FIELD_GET(*hdr, ERR_NO);
  1082. err_no |= ~FM10K_MSG_HDR_MASK(ERR_NO);
  1083. switch (mbx->state) {
  1084. case FM10K_STATE_OPEN:
  1085. case FM10K_STATE_DISCONNECT:
  1086. /* flush any uncompleted work */
  1087. fm10k_mbx_reset_work(mbx);
  1088. /* reset CRC seeds */
  1089. mbx->local = FM10K_MBX_CRC_SEED;
  1090. mbx->remote = FM10K_MBX_CRC_SEED;
  1091. /* reset tail index and size to prepare for reconnect */
  1092. mbx->tail = head;
  1093. /* if open then reset max_size and go back to connect */
  1094. if (mbx->state == FM10K_STATE_OPEN) {
  1095. mbx->state = FM10K_STATE_CONNECT;
  1096. break;
  1097. }
  1098. /* send a connect message to get data flowing again */
  1099. fm10k_mbx_create_connect_hdr(mbx);
  1100. return 0;
  1101. default:
  1102. break;
  1103. }
  1104. return fm10k_mbx_create_reply(hw, mbx, mbx->tail);
  1105. }
  1106. /**
  1107. * fm10k_mbx_process - Process mailbox interrupt
  1108. * @hw: pointer to hardware structure
  1109. * @mbx: pointer to mailbox
  1110. *
  1111. * This function will process incoming mailbox events and generate mailbox
  1112. * replies. It will return a value indicating the number of DWORDs
  1113. * transmitted excluding header on success or a negative value on error.
  1114. **/
  1115. static s32 fm10k_mbx_process(struct fm10k_hw *hw,
  1116. struct fm10k_mbx_info *mbx)
  1117. {
  1118. s32 err;
  1119. /* we do not read mailbox if closed */
  1120. if (mbx->state == FM10K_STATE_CLOSED)
  1121. return 0;
  1122. /* copy data from mailbox */
  1123. err = fm10k_mbx_read(hw, mbx);
  1124. if (err)
  1125. return err;
  1126. /* validate type, source, and destination */
  1127. err = fm10k_mbx_validate_msg_hdr(mbx);
  1128. if (err < 0)
  1129. goto msg_err;
  1130. switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, TYPE)) {
  1131. case FM10K_MSG_CONNECT:
  1132. err = fm10k_mbx_process_connect(hw, mbx);
  1133. break;
  1134. case FM10K_MSG_DATA:
  1135. err = fm10k_mbx_process_data(hw, mbx);
  1136. break;
  1137. case FM10K_MSG_DISCONNECT:
  1138. err = fm10k_mbx_process_disconnect(hw, mbx);
  1139. break;
  1140. case FM10K_MSG_ERROR:
  1141. err = fm10k_mbx_process_error(hw, mbx);
  1142. break;
  1143. default:
  1144. err = FM10K_MBX_ERR_TYPE;
  1145. break;
  1146. }
  1147. msg_err:
  1148. /* notify partner of errors on our end */
  1149. if (err < 0)
  1150. fm10k_mbx_create_error_msg(mbx, err);
  1151. /* copy data from mailbox */
  1152. fm10k_mbx_write(hw, mbx);
  1153. return err;
  1154. }
  1155. /**
  1156. * fm10k_mbx_disconnect - Shutdown mailbox connection
  1157. * @hw: pointer to hardware structure
  1158. * @mbx: pointer to mailbox
  1159. *
  1160. * This function will shut down the mailbox. It places the mailbox first
  1161. * in the disconnect state, it then allows up to a predefined timeout for
  1162. * the mailbox to transition to close on its own. If this does not occur
  1163. * then the mailbox will be forced into the closed state.
  1164. *
  1165. * Any mailbox transactions not completed before calling this function
  1166. * are not guaranteed to complete and may be dropped.
  1167. **/
  1168. static void fm10k_mbx_disconnect(struct fm10k_hw *hw,
  1169. struct fm10k_mbx_info *mbx)
  1170. {
  1171. int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
  1172. /* Place mbx in ready to disconnect state */
  1173. mbx->state = FM10K_STATE_DISCONNECT;
  1174. /* trigger interrupt to start shutdown process */
  1175. fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
  1176. FM10K_MBX_INTERRUPT_DISABLE);
  1177. do {
  1178. udelay(FM10K_MBX_POLL_DELAY);
  1179. mbx->ops.process(hw, mbx);
  1180. timeout -= FM10K_MBX_POLL_DELAY;
  1181. } while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
  1182. /* in case we didn't close just force the mailbox into shutdown */
  1183. fm10k_mbx_connect_reset(mbx);
  1184. fm10k_mbx_update_max_size(mbx, 0);
  1185. fm10k_write_reg(hw, mbx->mbmem_reg, 0);
  1186. }
  1187. /**
  1188. * fm10k_mbx_connect - Start mailbox connection
  1189. * @hw: pointer to hardware structure
  1190. * @mbx: pointer to mailbox
  1191. *
  1192. * This function will initiate a mailbox connection. It will populate the
  1193. * mailbox with a broadcast connect message and then initialize the lock.
  1194. * This is safe since the connect message is a single DWORD so the mailbox
  1195. * transaction is guaranteed to be atomic.
  1196. *
  1197. * This function will return an error if the mailbox has not been initiated
  1198. * or is currently in use.
  1199. **/
  1200. static s32 fm10k_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
  1201. {
  1202. /* we cannot connect an uninitialized mailbox */
  1203. if (!mbx->rx.buffer)
  1204. return FM10K_MBX_ERR_NO_SPACE;
  1205. /* we cannot connect an already connected mailbox */
  1206. if (mbx->state != FM10K_STATE_CLOSED)
  1207. return FM10K_MBX_ERR_BUSY;
  1208. /* mailbox timeout can now become active */
  1209. mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
  1210. /* Place mbx in ready to connect state */
  1211. mbx->state = FM10K_STATE_CONNECT;
  1212. /* initialize header of remote mailbox */
  1213. fm10k_mbx_create_disconnect_hdr(mbx);
  1214. fm10k_write_reg(hw, mbx->mbmem_reg ^ mbx->mbmem_len, mbx->mbx_hdr);
  1215. /* enable interrupt and notify other party of new message */
  1216. mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
  1217. FM10K_MBX_INTERRUPT_ENABLE;
  1218. /* generate and load connect header into mailbox */
  1219. fm10k_mbx_create_connect_hdr(mbx);
  1220. fm10k_mbx_write(hw, mbx);
  1221. return 0;
  1222. }
  1223. /**
  1224. * fm10k_mbx_validate_handlers - Validate layout of message parsing data
  1225. * @msg_data: handlers for mailbox events
  1226. *
  1227. * This function validates the layout of the message parsing data. This
  1228. * should be mostly static, but it is important to catch any errors that
  1229. * are made when constructing the parsers.
  1230. **/
  1231. static s32 fm10k_mbx_validate_handlers(const struct fm10k_msg_data *msg_data)
  1232. {
  1233. const struct fm10k_tlv_attr *attr;
  1234. unsigned int id;
  1235. /* Allow NULL mailboxes that transmit but don't receive */
  1236. if (!msg_data)
  1237. return 0;
  1238. while (msg_data->id != FM10K_TLV_ERROR) {
  1239. /* all messages should have a function handler */
  1240. if (!msg_data->func)
  1241. return FM10K_ERR_PARAM;
  1242. /* parser is optional */
  1243. attr = msg_data->attr;
  1244. if (attr) {
  1245. while (attr->id != FM10K_TLV_ERROR) {
  1246. id = attr->id;
  1247. attr++;
  1248. /* ID should always be increasing */
  1249. if (id >= attr->id)
  1250. return FM10K_ERR_PARAM;
  1251. /* ID should fit in results array */
  1252. if (id >= FM10K_TLV_RESULTS_MAX)
  1253. return FM10K_ERR_PARAM;
  1254. }
  1255. /* verify terminator is in the list */
  1256. if (attr->id != FM10K_TLV_ERROR)
  1257. return FM10K_ERR_PARAM;
  1258. }
  1259. id = msg_data->id;
  1260. msg_data++;
  1261. /* ID should always be increasing */
  1262. if (id >= msg_data->id)
  1263. return FM10K_ERR_PARAM;
  1264. }
  1265. /* verify terminator is in the list */
  1266. if ((msg_data->id != FM10K_TLV_ERROR) || !msg_data->func)
  1267. return FM10K_ERR_PARAM;
  1268. return 0;
  1269. }
  1270. /**
  1271. * fm10k_mbx_register_handlers - Register a set of handler ops for mailbox
  1272. * @mbx: pointer to mailbox
  1273. * @msg_data: handlers for mailbox events
  1274. *
  1275. * This function associates a set of message handling ops with a mailbox.
  1276. **/
  1277. static s32 fm10k_mbx_register_handlers(struct fm10k_mbx_info *mbx,
  1278. const struct fm10k_msg_data *msg_data)
  1279. {
  1280. /* validate layout of handlers before assigning them */
  1281. if (fm10k_mbx_validate_handlers(msg_data))
  1282. return FM10K_ERR_PARAM;
  1283. /* initialize the message handlers */
  1284. mbx->msg_data = msg_data;
  1285. return 0;
  1286. }
  1287. /**
  1288. * fm10k_pfvf_mbx_init - Initialize mailbox memory for PF/VF mailbox
  1289. * @hw: pointer to hardware structure
  1290. * @mbx: pointer to mailbox
  1291. * @msg_data: handlers for mailbox events
  1292. * @id: ID reference for PF as it supports up to 64 PF/VF mailboxes
  1293. *
  1294. * This function initializes the mailbox for use. It will split the
  1295. * buffer provided an use that th populate both the Tx and Rx FIFO by
  1296. * evenly splitting it. In order to allow for easy masking of head/tail
  1297. * the value reported in size must be a power of 2 and is reported in
  1298. * DWORDs, not bytes. Any invalid values will cause the mailbox to return
  1299. * error.
  1300. **/
  1301. s32 fm10k_pfvf_mbx_init(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx,
  1302. const struct fm10k_msg_data *msg_data, u8 id)
  1303. {
  1304. /* initialize registers */
  1305. switch (hw->mac.type) {
  1306. case fm10k_mac_vf:
  1307. mbx->mbx_reg = FM10K_VFMBX;
  1308. mbx->mbmem_reg = FM10K_VFMBMEM(FM10K_VFMBMEM_VF_XOR);
  1309. break;
  1310. case fm10k_mac_pf:
  1311. /* there are only 64 VF <-> PF mailboxes */
  1312. if (id < 64) {
  1313. mbx->mbx_reg = FM10K_MBX(id);
  1314. mbx->mbmem_reg = FM10K_MBMEM_VF(id, 0);
  1315. break;
  1316. }
  1317. /* fallthough */
  1318. default:
  1319. return FM10K_MBX_ERR_NO_MBX;
  1320. }
  1321. /* start out in closed state */
  1322. mbx->state = FM10K_STATE_CLOSED;
  1323. /* validate layout of handlers before assigning them */
  1324. if (fm10k_mbx_validate_handlers(msg_data))
  1325. return FM10K_ERR_PARAM;
  1326. /* initialize the message handlers */
  1327. mbx->msg_data = msg_data;
  1328. /* start mailbox as timed out and let the reset_hw call
  1329. * set the timeout value to begin communications
  1330. */
  1331. mbx->timeout = 0;
  1332. mbx->udelay = FM10K_MBX_INIT_DELAY;
  1333. /* initialize tail and head */
  1334. mbx->tail = 1;
  1335. mbx->head = 1;
  1336. /* initialize CRC seeds */
  1337. mbx->local = FM10K_MBX_CRC_SEED;
  1338. mbx->remote = FM10K_MBX_CRC_SEED;
  1339. /* Split buffer for use by Tx/Rx FIFOs */
  1340. mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
  1341. mbx->mbmem_len = FM10K_VFMBMEM_VF_XOR;
  1342. /* initialize the FIFOs, sizes are in 4 byte increments */
  1343. fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
  1344. fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
  1345. FM10K_MBX_RX_BUFFER_SIZE);
  1346. /* initialize function pointers */
  1347. mbx->ops.connect = fm10k_mbx_connect;
  1348. mbx->ops.disconnect = fm10k_mbx_disconnect;
  1349. mbx->ops.rx_ready = fm10k_mbx_rx_ready;
  1350. mbx->ops.tx_ready = fm10k_mbx_tx_ready;
  1351. mbx->ops.tx_complete = fm10k_mbx_tx_complete;
  1352. mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
  1353. mbx->ops.process = fm10k_mbx_process;
  1354. mbx->ops.register_handlers = fm10k_mbx_register_handlers;
  1355. return 0;
  1356. }
  1357. /**
  1358. * fm10k_sm_mbx_create_data_hdr - Generate a mailbox header for local FIFO
  1359. * @mbx: pointer to mailbox
  1360. *
  1361. * This function returns a connection mailbox header
  1362. **/
  1363. static void fm10k_sm_mbx_create_data_hdr(struct fm10k_mbx_info *mbx)
  1364. {
  1365. if (mbx->tail_len)
  1366. mbx->mbx_lock |= FM10K_MBX_REQ;
  1367. mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
  1368. FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
  1369. FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD);
  1370. }
  1371. /**
  1372. * fm10k_sm_mbx_create_connect_hdr - Generate a mailbox header for local FIFO
  1373. * @mbx: pointer to mailbox
  1374. * @err: error flags to report if any
  1375. *
  1376. * This function returns a connection mailbox header
  1377. **/
  1378. static void fm10k_sm_mbx_create_connect_hdr(struct fm10k_mbx_info *mbx, u8 err)
  1379. {
  1380. if (mbx->local)
  1381. mbx->mbx_lock |= FM10K_MBX_REQ;
  1382. mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
  1383. FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
  1384. FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD) |
  1385. FM10K_MSG_HDR_FIELD_SET(err, SM_ERR);
  1386. }
  1387. /**
  1388. * fm10k_sm_mbx_connect_reset - Reset following request for reset
  1389. * @mbx: pointer to mailbox
  1390. *
  1391. * This function resets the mailbox to a just connected state
  1392. **/
  1393. static void fm10k_sm_mbx_connect_reset(struct fm10k_mbx_info *mbx)
  1394. {
  1395. /* flush any uncompleted work */
  1396. fm10k_mbx_reset_work(mbx);
  1397. /* set local version to max and remote version to 0 */
  1398. mbx->local = FM10K_SM_MBX_VERSION;
  1399. mbx->remote = 0;
  1400. /* initialize tail and head */
  1401. mbx->tail = 1;
  1402. mbx->head = 1;
  1403. /* reset state back to connect */
  1404. mbx->state = FM10K_STATE_CONNECT;
  1405. }
  1406. /**
  1407. * fm10k_sm_mbx_connect - Start switch manager mailbox connection
  1408. * @hw: pointer to hardware structure
  1409. * @mbx: pointer to mailbox
  1410. *
  1411. * This function will initiate a mailbox connection with the switch
  1412. * manager. To do this it will first disconnect the mailbox, and then
  1413. * reconnect it in order to complete a reset of the mailbox.
  1414. *
  1415. * This function will return an error if the mailbox has not been initiated
  1416. * or is currently in use.
  1417. **/
  1418. static s32 fm10k_sm_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
  1419. {
  1420. /* we cannot connect an uninitialized mailbox */
  1421. if (!mbx->rx.buffer)
  1422. return FM10K_MBX_ERR_NO_SPACE;
  1423. /* we cannot connect an already connected mailbox */
  1424. if (mbx->state != FM10K_STATE_CLOSED)
  1425. return FM10K_MBX_ERR_BUSY;
  1426. /* mailbox timeout can now become active */
  1427. mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
  1428. /* Place mbx in ready to connect state */
  1429. mbx->state = FM10K_STATE_CONNECT;
  1430. mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
  1431. /* reset interface back to connect */
  1432. fm10k_sm_mbx_connect_reset(mbx);
  1433. /* enable interrupt and notify other party of new message */
  1434. mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
  1435. FM10K_MBX_INTERRUPT_ENABLE;
  1436. /* generate and load connect header into mailbox */
  1437. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1438. fm10k_mbx_write(hw, mbx);
  1439. /* enable interrupt and notify other party of new message */
  1440. return 0;
  1441. }
  1442. /**
  1443. * fm10k_sm_mbx_disconnect - Shutdown mailbox connection
  1444. * @hw: pointer to hardware structure
  1445. * @mbx: pointer to mailbox
  1446. *
  1447. * This function will shut down the mailbox. It places the mailbox first
  1448. * in the disconnect state, it then allows up to a predefined timeout for
  1449. * the mailbox to transition to close on its own. If this does not occur
  1450. * then the mailbox will be forced into the closed state.
  1451. *
  1452. * Any mailbox transactions not completed before calling this function
  1453. * are not guaranteed to complete and may be dropped.
  1454. **/
  1455. static void fm10k_sm_mbx_disconnect(struct fm10k_hw *hw,
  1456. struct fm10k_mbx_info *mbx)
  1457. {
  1458. int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
  1459. /* Place mbx in ready to disconnect state */
  1460. mbx->state = FM10K_STATE_DISCONNECT;
  1461. /* trigger interrupt to start shutdown process */
  1462. fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
  1463. FM10K_MBX_INTERRUPT_DISABLE);
  1464. do {
  1465. udelay(FM10K_MBX_POLL_DELAY);
  1466. mbx->ops.process(hw, mbx);
  1467. timeout -= FM10K_MBX_POLL_DELAY;
  1468. } while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
  1469. /* in case we didn't close just force the mailbox into shutdown */
  1470. mbx->state = FM10K_STATE_CLOSED;
  1471. mbx->remote = 0;
  1472. fm10k_mbx_reset_work(mbx);
  1473. fm10k_mbx_update_max_size(mbx, 0);
  1474. fm10k_write_reg(hw, mbx->mbmem_reg, 0);
  1475. }
  1476. /**
  1477. * fm10k_mbx_validate_fifo_hdr - Validate fields in the remote FIFO header
  1478. * @mbx: pointer to mailbox
  1479. *
  1480. * This function will parse up the fields in the mailbox header and return
  1481. * an error if the header contains any of a number of invalid configurations
  1482. * including unrecognized offsets or version numbers.
  1483. **/
  1484. static s32 fm10k_sm_mbx_validate_fifo_hdr(struct fm10k_mbx_info *mbx)
  1485. {
  1486. const u32 *hdr = &mbx->mbx_hdr;
  1487. u16 tail, head, ver;
  1488. tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
  1489. ver = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_VER);
  1490. head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
  1491. switch (ver) {
  1492. case 0:
  1493. break;
  1494. case FM10K_SM_MBX_VERSION:
  1495. if (!head || head > FM10K_SM_MBX_FIFO_LEN)
  1496. return FM10K_MBX_ERR_HEAD;
  1497. if (!tail || tail > FM10K_SM_MBX_FIFO_LEN)
  1498. return FM10K_MBX_ERR_TAIL;
  1499. if (mbx->tail < head)
  1500. head += mbx->mbmem_len - 1;
  1501. if (tail < mbx->head)
  1502. tail += mbx->mbmem_len - 1;
  1503. if (fm10k_mbx_index_len(mbx, head, mbx->tail) > mbx->tail_len)
  1504. return FM10K_MBX_ERR_HEAD;
  1505. if (fm10k_mbx_index_len(mbx, mbx->head, tail) < mbx->mbmem_len)
  1506. break;
  1507. return FM10K_MBX_ERR_TAIL;
  1508. default:
  1509. return FM10K_MBX_ERR_SRC;
  1510. }
  1511. return 0;
  1512. }
  1513. /**
  1514. * fm10k_sm_mbx_process_error - Process header with error flag set
  1515. * @mbx: pointer to mailbox
  1516. *
  1517. * This function is meant to respond to a request where the error flag
  1518. * is set. As a result we will terminate a connection if one is present
  1519. * and fall back into the reset state with a connection header of version
  1520. * 0 (RESET).
  1521. **/
  1522. static void fm10k_sm_mbx_process_error(struct fm10k_mbx_info *mbx)
  1523. {
  1524. const enum fm10k_mbx_state state = mbx->state;
  1525. switch (state) {
  1526. case FM10K_STATE_DISCONNECT:
  1527. /* if there is an error just disconnect */
  1528. mbx->remote = 0;
  1529. break;
  1530. case FM10K_STATE_OPEN:
  1531. /* flush any uncompleted work */
  1532. fm10k_sm_mbx_connect_reset(mbx);
  1533. break;
  1534. case FM10K_STATE_CONNECT:
  1535. /* try connnecting at lower version */
  1536. if (mbx->remote) {
  1537. while (mbx->local > 1)
  1538. mbx->local--;
  1539. mbx->remote = 0;
  1540. }
  1541. break;
  1542. default:
  1543. break;
  1544. }
  1545. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1546. }
  1547. /**
  1548. * fm10k_sm_mbx_create_error_message - Process an error in FIFO hdr
  1549. * @mbx: pointer to mailbox
  1550. * @err: local error encountered
  1551. *
  1552. * This function will interpret the error provided by err, and based on
  1553. * that it may set the error bit in the local message header
  1554. **/
  1555. static void fm10k_sm_mbx_create_error_msg(struct fm10k_mbx_info *mbx, s32 err)
  1556. {
  1557. /* only generate an error message for these types */
  1558. switch (err) {
  1559. case FM10K_MBX_ERR_TAIL:
  1560. case FM10K_MBX_ERR_HEAD:
  1561. case FM10K_MBX_ERR_SRC:
  1562. case FM10K_MBX_ERR_SIZE:
  1563. case FM10K_MBX_ERR_RSVD0:
  1564. break;
  1565. default:
  1566. return;
  1567. }
  1568. /* process it as though we received an error, and send error reply */
  1569. fm10k_sm_mbx_process_error(mbx);
  1570. fm10k_sm_mbx_create_connect_hdr(mbx, 1);
  1571. }
  1572. /**
  1573. * fm10k_sm_mbx_receive - Take message from Rx mailbox FIFO and put it in Rx
  1574. * @hw: pointer to hardware structure
  1575. * @mbx: pointer to mailbox
  1576. *
  1577. * This function will dequeue one message from the Rx switch manager mailbox
  1578. * FIFO and place it in the Rx mailbox FIFO for processing by software.
  1579. **/
  1580. static s32 fm10k_sm_mbx_receive(struct fm10k_hw *hw,
  1581. struct fm10k_mbx_info *mbx,
  1582. u16 tail)
  1583. {
  1584. /* reduce length by 1 to convert to a mask */
  1585. u16 mbmem_len = mbx->mbmem_len - 1;
  1586. s32 err;
  1587. /* push tail in front of head */
  1588. if (tail < mbx->head)
  1589. tail += mbmem_len;
  1590. /* copy data to the Rx FIFO */
  1591. err = fm10k_mbx_push_tail(hw, mbx, tail);
  1592. if (err < 0)
  1593. return err;
  1594. /* process messages if we have received any */
  1595. fm10k_mbx_dequeue_rx(hw, mbx);
  1596. /* guarantee head aligns with the end of the last message */
  1597. mbx->head = fm10k_mbx_head_sub(mbx, mbx->pushed);
  1598. mbx->pushed = 0;
  1599. /* clear any extra bits left over since index adds 1 extra bit */
  1600. if (mbx->head > mbmem_len)
  1601. mbx->head -= mbmem_len;
  1602. return err;
  1603. }
  1604. /**
  1605. * fm10k_sm_mbx_transmit - Take message from Tx and put it in Tx mailbox FIFO
  1606. * @hw: pointer to hardware structure
  1607. * @mbx: pointer to mailbox
  1608. *
  1609. * This function will dequeue one message from the Tx mailbox FIFO and place
  1610. * it in the Tx switch manager mailbox FIFO for processing by hardware.
  1611. **/
  1612. static void fm10k_sm_mbx_transmit(struct fm10k_hw *hw,
  1613. struct fm10k_mbx_info *mbx, u16 head)
  1614. {
  1615. struct fm10k_mbx_fifo *fifo = &mbx->tx;
  1616. /* reduce length by 1 to convert to a mask */
  1617. u16 mbmem_len = mbx->mbmem_len - 1;
  1618. u16 tail_len, len = 0;
  1619. u32 *msg;
  1620. /* push head behind tail */
  1621. if (mbx->tail < head)
  1622. head += mbmem_len;
  1623. fm10k_mbx_pull_head(hw, mbx, head);
  1624. /* determine msg aligned offset for end of buffer */
  1625. do {
  1626. msg = fifo->buffer + fm10k_fifo_head_offset(fifo, len);
  1627. tail_len = len;
  1628. len += FM10K_TLV_DWORD_LEN(*msg);
  1629. } while ((len <= mbx->tail_len) && (len < mbmem_len));
  1630. /* guarantee we stop on a message boundary */
  1631. if (mbx->tail_len > tail_len) {
  1632. mbx->tail = fm10k_mbx_tail_sub(mbx, mbx->tail_len - tail_len);
  1633. mbx->tail_len = tail_len;
  1634. }
  1635. /* clear any extra bits left over since index adds 1 extra bit */
  1636. if (mbx->tail > mbmem_len)
  1637. mbx->tail -= mbmem_len;
  1638. }
  1639. /**
  1640. * fm10k_sm_mbx_create_reply - Generate reply based on state and remote head
  1641. * @mbx: pointer to mailbox
  1642. * @head: acknowledgement number
  1643. *
  1644. * This function will generate an outgoing message based on the current
  1645. * mailbox state and the remote fifo head. It will return the length
  1646. * of the outgoing message excluding header on success, and a negative value
  1647. * on error.
  1648. **/
  1649. static void fm10k_sm_mbx_create_reply(struct fm10k_hw *hw,
  1650. struct fm10k_mbx_info *mbx, u16 head)
  1651. {
  1652. switch (mbx->state) {
  1653. case FM10K_STATE_OPEN:
  1654. case FM10K_STATE_DISCONNECT:
  1655. /* flush out Tx data */
  1656. fm10k_sm_mbx_transmit(hw, mbx, head);
  1657. /* generate new header based on data */
  1658. if (mbx->tail_len || (mbx->state == FM10K_STATE_OPEN)) {
  1659. fm10k_sm_mbx_create_data_hdr(mbx);
  1660. } else {
  1661. mbx->remote = 0;
  1662. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1663. }
  1664. break;
  1665. case FM10K_STATE_CONNECT:
  1666. case FM10K_STATE_CLOSED:
  1667. fm10k_sm_mbx_create_connect_hdr(mbx, 0);
  1668. break;
  1669. default:
  1670. break;
  1671. }
  1672. }
  1673. /**
  1674. * fm10k_sm_mbx_process_reset - Process header with version == 0 (RESET)
  1675. * @hw: pointer to hardware structure
  1676. * @mbx: pointer to mailbox
  1677. *
  1678. * This function is meant to respond to a request where the version data
  1679. * is set to 0. As such we will either terminate the connection or go
  1680. * into the connect state in order to re-establish the connection. This
  1681. * function can also be used to respond to an error as the connection
  1682. * resetting would also be a means of dealing with errors.
  1683. **/
  1684. static void fm10k_sm_mbx_process_reset(struct fm10k_hw *hw,
  1685. struct fm10k_mbx_info *mbx)
  1686. {
  1687. const enum fm10k_mbx_state state = mbx->state;
  1688. switch (state) {
  1689. case FM10K_STATE_DISCONNECT:
  1690. /* drop remote connections and disconnect */
  1691. mbx->state = FM10K_STATE_CLOSED;
  1692. mbx->remote = 0;
  1693. mbx->local = 0;
  1694. break;
  1695. case FM10K_STATE_OPEN:
  1696. /* flush any incomplete work */
  1697. fm10k_sm_mbx_connect_reset(mbx);
  1698. break;
  1699. case FM10K_STATE_CONNECT:
  1700. /* Update remote value to match local value */
  1701. mbx->remote = mbx->local;
  1702. default:
  1703. break;
  1704. }
  1705. fm10k_sm_mbx_create_reply(hw, mbx, mbx->tail);
  1706. }
  1707. /**
  1708. * fm10k_sm_mbx_process_version_1 - Process header with version == 1
  1709. * @hw: pointer to hardware structure
  1710. * @mbx: pointer to mailbox
  1711. *
  1712. * This function is meant to process messages received when the remote
  1713. * mailbox is active.
  1714. **/
  1715. static s32 fm10k_sm_mbx_process_version_1(struct fm10k_hw *hw,
  1716. struct fm10k_mbx_info *mbx)
  1717. {
  1718. const u32 *hdr = &mbx->mbx_hdr;
  1719. u16 head, tail;
  1720. s32 len;
  1721. /* pull all fields needed for verification */
  1722. tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
  1723. head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
  1724. /* if we are in connect and wanting version 1 then start up and go */
  1725. if (mbx->state == FM10K_STATE_CONNECT) {
  1726. if (!mbx->remote)
  1727. goto send_reply;
  1728. if (mbx->remote != 1)
  1729. return FM10K_MBX_ERR_SRC;
  1730. mbx->state = FM10K_STATE_OPEN;
  1731. }
  1732. do {
  1733. /* abort on message size errors */
  1734. len = fm10k_sm_mbx_receive(hw, mbx, tail);
  1735. if (len < 0)
  1736. return len;
  1737. /* continue until we have flushed the Rx FIFO */
  1738. } while (len);
  1739. send_reply:
  1740. fm10k_sm_mbx_create_reply(hw, mbx, head);
  1741. return 0;
  1742. }
  1743. /**
  1744. * fm10k_sm_mbx_process - Process mailbox switch mailbox interrupt
  1745. * @hw: pointer to hardware structure
  1746. * @mbx: pointer to mailbox
  1747. *
  1748. * This function will process incoming mailbox events and generate mailbox
  1749. * replies. It will return a value indicating the number of DWORDs
  1750. * transmitted excluding header on success or a negative value on error.
  1751. **/
  1752. static s32 fm10k_sm_mbx_process(struct fm10k_hw *hw,
  1753. struct fm10k_mbx_info *mbx)
  1754. {
  1755. s32 err;
  1756. /* we do not read mailbox if closed */
  1757. if (mbx->state == FM10K_STATE_CLOSED)
  1758. return 0;
  1759. /* retrieve data from switch manager */
  1760. err = fm10k_mbx_read(hw, mbx);
  1761. if (err)
  1762. return err;
  1763. err = fm10k_sm_mbx_validate_fifo_hdr(mbx);
  1764. if (err < 0)
  1765. goto fifo_err;
  1766. if (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_ERR)) {
  1767. fm10k_sm_mbx_process_error(mbx);
  1768. goto fifo_err;
  1769. }
  1770. switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_VER)) {
  1771. case 0:
  1772. fm10k_sm_mbx_process_reset(hw, mbx);
  1773. break;
  1774. case FM10K_SM_MBX_VERSION:
  1775. err = fm10k_sm_mbx_process_version_1(hw, mbx);
  1776. break;
  1777. }
  1778. fifo_err:
  1779. if (err < 0)
  1780. fm10k_sm_mbx_create_error_msg(mbx, err);
  1781. /* report data to switch manager */
  1782. fm10k_mbx_write(hw, mbx);
  1783. return err;
  1784. }
  1785. /**
  1786. * fm10k_sm_mbx_init - Initialize mailbox memory for PF/SM mailbox
  1787. * @hw: pointer to hardware structure
  1788. * @mbx: pointer to mailbox
  1789. * @msg_data: handlers for mailbox events
  1790. *
  1791. * This function for now is used to stub out the PF/SM mailbox
  1792. **/
  1793. s32 fm10k_sm_mbx_init(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx,
  1794. const struct fm10k_msg_data *msg_data)
  1795. {
  1796. mbx->mbx_reg = FM10K_GMBX;
  1797. mbx->mbmem_reg = FM10K_MBMEM_PF(0);
  1798. /* start out in closed state */
  1799. mbx->state = FM10K_STATE_CLOSED;
  1800. /* validate layout of handlers before assigning them */
  1801. if (fm10k_mbx_validate_handlers(msg_data))
  1802. return FM10K_ERR_PARAM;
  1803. /* initialize the message handlers */
  1804. mbx->msg_data = msg_data;
  1805. /* start mailbox as timed out and let the reset_hw call
  1806. * set the timeout value to begin communications
  1807. */
  1808. mbx->timeout = 0;
  1809. mbx->udelay = FM10K_MBX_INIT_DELAY;
  1810. /* Split buffer for use by Tx/Rx FIFOs */
  1811. mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
  1812. mbx->mbmem_len = FM10K_MBMEM_PF_XOR;
  1813. /* initialize the FIFOs, sizes are in 4 byte increments */
  1814. fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
  1815. fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
  1816. FM10K_MBX_RX_BUFFER_SIZE);
  1817. /* initialize function pointers */
  1818. mbx->ops.connect = fm10k_sm_mbx_connect;
  1819. mbx->ops.disconnect = fm10k_sm_mbx_disconnect;
  1820. mbx->ops.rx_ready = fm10k_mbx_rx_ready;
  1821. mbx->ops.tx_ready = fm10k_mbx_tx_ready;
  1822. mbx->ops.tx_complete = fm10k_mbx_tx_complete;
  1823. mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
  1824. mbx->ops.process = fm10k_sm_mbx_process;
  1825. mbx->ops.register_handlers = fm10k_mbx_register_handlers;
  1826. return 0;
  1827. }