tsi721.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974
  1. /*
  2. * RapidIO mport driver for Tsi721 PCIExpress-to-SRIO bridge
  3. *
  4. * Copyright 2011 Integrated Device Technology, Inc.
  5. * Alexandre Bounine <alexandre.bounine@idt.com>
  6. * Chul Kim <chul.kim@idt.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59
  20. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <linux/io.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/ioport.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/rio.h>
  30. #include <linux/rio_drv.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kfifo.h>
  34. #include <linux/delay.h>
  35. #include "tsi721.h"
  36. #ifdef DEBUG
  37. u32 dbg_level = DBG_INIT | DBG_EXIT;
  38. module_param(dbg_level, uint, S_IWUSR | S_IRUGO);
  39. MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
  40. #endif
  41. static void tsi721_omsg_handler(struct tsi721_device *priv, int ch);
  42. static void tsi721_imsg_handler(struct tsi721_device *priv, int ch);
  43. /**
  44. * tsi721_lcread - read from local SREP config space
  45. * @mport: RapidIO master port info
  46. * @index: ID of RapdiIO interface
  47. * @offset: Offset into configuration space
  48. * @len: Length (in bytes) of the maintenance transaction
  49. * @data: Value to be read into
  50. *
  51. * Generates a local SREP space read. Returns %0 on
  52. * success or %-EINVAL on failure.
  53. */
  54. static int tsi721_lcread(struct rio_mport *mport, int index, u32 offset,
  55. int len, u32 *data)
  56. {
  57. struct tsi721_device *priv = mport->priv;
  58. if (len != sizeof(u32))
  59. return -EINVAL; /* only 32-bit access is supported */
  60. *data = ioread32(priv->regs + offset);
  61. return 0;
  62. }
  63. /**
  64. * tsi721_lcwrite - write into local SREP config space
  65. * @mport: RapidIO master port info
  66. * @index: ID of RapdiIO interface
  67. * @offset: Offset into configuration space
  68. * @len: Length (in bytes) of the maintenance transaction
  69. * @data: Value to be written
  70. *
  71. * Generates a local write into SREP configuration space. Returns %0 on
  72. * success or %-EINVAL on failure.
  73. */
  74. static int tsi721_lcwrite(struct rio_mport *mport, int index, u32 offset,
  75. int len, u32 data)
  76. {
  77. struct tsi721_device *priv = mport->priv;
  78. if (len != sizeof(u32))
  79. return -EINVAL; /* only 32-bit access is supported */
  80. iowrite32(data, priv->regs + offset);
  81. return 0;
  82. }
  83. /**
  84. * tsi721_maint_dma - Helper function to generate RapidIO maintenance
  85. * transactions using designated Tsi721 DMA channel.
  86. * @priv: pointer to tsi721 private data
  87. * @sys_size: RapdiIO transport system size
  88. * @destid: Destination ID of transaction
  89. * @hopcount: Number of hops to target device
  90. * @offset: Offset into configuration space
  91. * @len: Length (in bytes) of the maintenance transaction
  92. * @data: Location to be read from or write into
  93. * @do_wr: Operation flag (1 == MAINT_WR)
  94. *
  95. * Generates a RapidIO maintenance transaction (Read or Write).
  96. * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
  97. */
  98. static int tsi721_maint_dma(struct tsi721_device *priv, u32 sys_size,
  99. u16 destid, u8 hopcount, u32 offset, int len,
  100. u32 *data, int do_wr)
  101. {
  102. void __iomem *regs = priv->regs + TSI721_DMAC_BASE(priv->mdma.ch_id);
  103. struct tsi721_dma_desc *bd_ptr;
  104. u32 rd_count, swr_ptr, ch_stat;
  105. int i, err = 0;
  106. u32 op = do_wr ? MAINT_WR : MAINT_RD;
  107. if (offset > (RIO_MAINT_SPACE_SZ - len) || (len != sizeof(u32)))
  108. return -EINVAL;
  109. bd_ptr = priv->mdma.bd_base;
  110. rd_count = ioread32(regs + TSI721_DMAC_DRDCNT);
  111. /* Initialize DMA descriptor */
  112. bd_ptr[0].type_id = cpu_to_le32((DTYPE2 << 29) | (op << 19) | destid);
  113. bd_ptr[0].bcount = cpu_to_le32((sys_size << 26) | 0x04);
  114. bd_ptr[0].raddr_lo = cpu_to_le32((hopcount << 24) | offset);
  115. bd_ptr[0].raddr_hi = 0;
  116. if (do_wr)
  117. bd_ptr[0].data[0] = cpu_to_be32p(data);
  118. else
  119. bd_ptr[0].data[0] = 0xffffffff;
  120. mb();
  121. /* Start DMA operation */
  122. iowrite32(rd_count + 2, regs + TSI721_DMAC_DWRCNT);
  123. ioread32(regs + TSI721_DMAC_DWRCNT);
  124. i = 0;
  125. /* Wait until DMA transfer is finished */
  126. while ((ch_stat = ioread32(regs + TSI721_DMAC_STS))
  127. & TSI721_DMAC_STS_RUN) {
  128. udelay(1);
  129. if (++i >= 5000000) {
  130. tsi_debug(MAINT, &priv->pdev->dev,
  131. "DMA[%d] read timeout ch_status=%x",
  132. priv->mdma.ch_id, ch_stat);
  133. if (!do_wr)
  134. *data = 0xffffffff;
  135. err = -EIO;
  136. goto err_out;
  137. }
  138. }
  139. if (ch_stat & TSI721_DMAC_STS_ABORT) {
  140. /* If DMA operation aborted due to error,
  141. * reinitialize DMA channel
  142. */
  143. tsi_debug(MAINT, &priv->pdev->dev, "DMA ABORT ch_stat=%x",
  144. ch_stat);
  145. tsi_debug(MAINT, &priv->pdev->dev,
  146. "OP=%d : destid=%x hc=%x off=%x",
  147. do_wr ? MAINT_WR : MAINT_RD,
  148. destid, hopcount, offset);
  149. iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
  150. iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
  151. udelay(10);
  152. iowrite32(0, regs + TSI721_DMAC_DWRCNT);
  153. udelay(1);
  154. if (!do_wr)
  155. *data = 0xffffffff;
  156. err = -EIO;
  157. goto err_out;
  158. }
  159. if (!do_wr)
  160. *data = be32_to_cpu(bd_ptr[0].data[0]);
  161. /*
  162. * Update descriptor status FIFO RD pointer.
  163. * NOTE: Skipping check and clear FIFO entries because we are waiting
  164. * for transfer to be completed.
  165. */
  166. swr_ptr = ioread32(regs + TSI721_DMAC_DSWP);
  167. iowrite32(swr_ptr, regs + TSI721_DMAC_DSRP);
  168. err_out:
  169. return err;
  170. }
  171. /**
  172. * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
  173. * using Tsi721 BDMA engine.
  174. * @mport: RapidIO master port control structure
  175. * @index: ID of RapdiIO interface
  176. * @destid: Destination ID of transaction
  177. * @hopcount: Number of hops to target device
  178. * @offset: Offset into configuration space
  179. * @len: Length (in bytes) of the maintenance transaction
  180. * @val: Location to be read into
  181. *
  182. * Generates a RapidIO maintenance read transaction.
  183. * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
  184. */
  185. static int tsi721_cread_dma(struct rio_mport *mport, int index, u16 destid,
  186. u8 hopcount, u32 offset, int len, u32 *data)
  187. {
  188. struct tsi721_device *priv = mport->priv;
  189. return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
  190. offset, len, data, 0);
  191. }
  192. /**
  193. * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
  194. * using Tsi721 BDMA engine
  195. * @mport: RapidIO master port control structure
  196. * @index: ID of RapdiIO interface
  197. * @destid: Destination ID of transaction
  198. * @hopcount: Number of hops to target device
  199. * @offset: Offset into configuration space
  200. * @len: Length (in bytes) of the maintenance transaction
  201. * @val: Value to be written
  202. *
  203. * Generates a RapidIO maintenance write transaction.
  204. * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
  205. */
  206. static int tsi721_cwrite_dma(struct rio_mport *mport, int index, u16 destid,
  207. u8 hopcount, u32 offset, int len, u32 data)
  208. {
  209. struct tsi721_device *priv = mport->priv;
  210. u32 temp = data;
  211. return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
  212. offset, len, &temp, 1);
  213. }
  214. /**
  215. * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
  216. * @priv: tsi721 device private structure
  217. *
  218. * Handles inbound port-write interrupts. Copies PW message from an internal
  219. * buffer into PW message FIFO and schedules deferred routine to process
  220. * queued messages.
  221. */
  222. static int
  223. tsi721_pw_handler(struct tsi721_device *priv)
  224. {
  225. u32 pw_stat;
  226. u32 pw_buf[TSI721_RIO_PW_MSG_SIZE/sizeof(u32)];
  227. pw_stat = ioread32(priv->regs + TSI721_RIO_PW_RX_STAT);
  228. if (pw_stat & TSI721_RIO_PW_RX_STAT_PW_VAL) {
  229. pw_buf[0] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(0));
  230. pw_buf[1] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(1));
  231. pw_buf[2] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(2));
  232. pw_buf[3] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(3));
  233. /* Queue PW message (if there is room in FIFO),
  234. * otherwise discard it.
  235. */
  236. spin_lock(&priv->pw_fifo_lock);
  237. if (kfifo_avail(&priv->pw_fifo) >= TSI721_RIO_PW_MSG_SIZE)
  238. kfifo_in(&priv->pw_fifo, pw_buf,
  239. TSI721_RIO_PW_MSG_SIZE);
  240. else
  241. priv->pw_discard_count++;
  242. spin_unlock(&priv->pw_fifo_lock);
  243. }
  244. /* Clear pending PW interrupts */
  245. iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
  246. priv->regs + TSI721_RIO_PW_RX_STAT);
  247. schedule_work(&priv->pw_work);
  248. return 0;
  249. }
  250. static void tsi721_pw_dpc(struct work_struct *work)
  251. {
  252. struct tsi721_device *priv = container_of(work, struct tsi721_device,
  253. pw_work);
  254. union rio_pw_msg pwmsg;
  255. /*
  256. * Process port-write messages
  257. */
  258. while (kfifo_out_spinlocked(&priv->pw_fifo, (unsigned char *)&pwmsg,
  259. TSI721_RIO_PW_MSG_SIZE, &priv->pw_fifo_lock)) {
  260. /* Pass the port-write message to RIO core for processing */
  261. rio_inb_pwrite_handler(&priv->mport, &pwmsg);
  262. }
  263. }
  264. /**
  265. * tsi721_pw_enable - enable/disable port-write interface init
  266. * @mport: Master port implementing the port write unit
  267. * @enable: 1=enable; 0=disable port-write message handling
  268. */
  269. static int tsi721_pw_enable(struct rio_mport *mport, int enable)
  270. {
  271. struct tsi721_device *priv = mport->priv;
  272. u32 rval;
  273. rval = ioread32(priv->regs + TSI721_RIO_EM_INT_ENABLE);
  274. if (enable)
  275. rval |= TSI721_RIO_EM_INT_ENABLE_PW_RX;
  276. else
  277. rval &= ~TSI721_RIO_EM_INT_ENABLE_PW_RX;
  278. /* Clear pending PW interrupts */
  279. iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
  280. priv->regs + TSI721_RIO_PW_RX_STAT);
  281. /* Update enable bits */
  282. iowrite32(rval, priv->regs + TSI721_RIO_EM_INT_ENABLE);
  283. return 0;
  284. }
  285. /**
  286. * tsi721_dsend - Send a RapidIO doorbell
  287. * @mport: RapidIO master port info
  288. * @index: ID of RapidIO interface
  289. * @destid: Destination ID of target device
  290. * @data: 16-bit info field of RapidIO doorbell
  291. *
  292. * Sends a RapidIO doorbell message. Always returns %0.
  293. */
  294. static int tsi721_dsend(struct rio_mport *mport, int index,
  295. u16 destid, u16 data)
  296. {
  297. struct tsi721_device *priv = mport->priv;
  298. u32 offset;
  299. offset = (((mport->sys_size) ? RIO_TT_CODE_16 : RIO_TT_CODE_8) << 18) |
  300. (destid << 2);
  301. tsi_debug(DBELL, &priv->pdev->dev,
  302. "Send Doorbell 0x%04x to destID 0x%x", data, destid);
  303. iowrite16be(data, priv->odb_base + offset);
  304. return 0;
  305. }
  306. /**
  307. * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
  308. * @priv: tsi721 device-specific data structure
  309. *
  310. * Handles inbound doorbell interrupts. Copies doorbell entry from an internal
  311. * buffer into DB message FIFO and schedules deferred routine to process
  312. * queued DBs.
  313. */
  314. static int
  315. tsi721_dbell_handler(struct tsi721_device *priv)
  316. {
  317. u32 regval;
  318. /* Disable IDB interrupts */
  319. regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  320. regval &= ~TSI721_SR_CHINT_IDBQRCV;
  321. iowrite32(regval,
  322. priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  323. schedule_work(&priv->idb_work);
  324. return 0;
  325. }
  326. static void tsi721_db_dpc(struct work_struct *work)
  327. {
  328. struct tsi721_device *priv = container_of(work, struct tsi721_device,
  329. idb_work);
  330. struct rio_mport *mport;
  331. struct rio_dbell *dbell;
  332. int found = 0;
  333. u32 wr_ptr, rd_ptr;
  334. u64 *idb_entry;
  335. u32 regval;
  336. union {
  337. u64 msg;
  338. u8 bytes[8];
  339. } idb;
  340. /*
  341. * Process queued inbound doorbells
  342. */
  343. mport = &priv->mport;
  344. wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
  345. rd_ptr = ioread32(priv->regs + TSI721_IDQ_RP(IDB_QUEUE)) % IDB_QSIZE;
  346. while (wr_ptr != rd_ptr) {
  347. idb_entry = (u64 *)(priv->idb_base +
  348. (TSI721_IDB_ENTRY_SIZE * rd_ptr));
  349. rd_ptr++;
  350. rd_ptr %= IDB_QSIZE;
  351. idb.msg = *idb_entry;
  352. *idb_entry = 0;
  353. /* Process one doorbell */
  354. list_for_each_entry(dbell, &mport->dbells, node) {
  355. if ((dbell->res->start <= DBELL_INF(idb.bytes)) &&
  356. (dbell->res->end >= DBELL_INF(idb.bytes))) {
  357. found = 1;
  358. break;
  359. }
  360. }
  361. if (found) {
  362. dbell->dinb(mport, dbell->dev_id, DBELL_SID(idb.bytes),
  363. DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
  364. } else {
  365. tsi_debug(DBELL, &priv->pdev->dev,
  366. "spurious IDB sid %2.2x tid %2.2x info %4.4x",
  367. DBELL_SID(idb.bytes), DBELL_TID(idb.bytes),
  368. DBELL_INF(idb.bytes));
  369. }
  370. wr_ptr = ioread32(priv->regs +
  371. TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
  372. }
  373. iowrite32(rd_ptr & (IDB_QSIZE - 1),
  374. priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
  375. /* Re-enable IDB interrupts */
  376. regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  377. regval |= TSI721_SR_CHINT_IDBQRCV;
  378. iowrite32(regval,
  379. priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  380. wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
  381. if (wr_ptr != rd_ptr)
  382. schedule_work(&priv->idb_work);
  383. }
  384. /**
  385. * tsi721_irqhandler - Tsi721 interrupt handler
  386. * @irq: Linux interrupt number
  387. * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
  388. *
  389. * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
  390. * interrupt events and calls an event-specific handler(s).
  391. */
  392. static irqreturn_t tsi721_irqhandler(int irq, void *ptr)
  393. {
  394. struct tsi721_device *priv = (struct tsi721_device *)ptr;
  395. u32 dev_int;
  396. u32 dev_ch_int;
  397. u32 intval;
  398. u32 ch_inte;
  399. /* For MSI mode disable all device-level interrupts */
  400. if (priv->flags & TSI721_USING_MSI)
  401. iowrite32(0, priv->regs + TSI721_DEV_INTE);
  402. dev_int = ioread32(priv->regs + TSI721_DEV_INT);
  403. if (!dev_int)
  404. return IRQ_NONE;
  405. dev_ch_int = ioread32(priv->regs + TSI721_DEV_CHAN_INT);
  406. if (dev_int & TSI721_DEV_INT_SR2PC_CH) {
  407. /* Service SR2PC Channel interrupts */
  408. if (dev_ch_int & TSI721_INT_SR2PC_CHAN(IDB_QUEUE)) {
  409. /* Service Inbound Doorbell interrupt */
  410. intval = ioread32(priv->regs +
  411. TSI721_SR_CHINT(IDB_QUEUE));
  412. if (intval & TSI721_SR_CHINT_IDBQRCV)
  413. tsi721_dbell_handler(priv);
  414. else
  415. tsi_info(&priv->pdev->dev,
  416. "Unsupported SR_CH_INT %x", intval);
  417. /* Clear interrupts */
  418. iowrite32(intval,
  419. priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  420. ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  421. }
  422. }
  423. if (dev_int & TSI721_DEV_INT_SMSG_CH) {
  424. int ch;
  425. /*
  426. * Service channel interrupts from Messaging Engine
  427. */
  428. if (dev_ch_int & TSI721_INT_IMSG_CHAN_M) { /* Inbound Msg */
  429. /* Disable signaled OB MSG Channel interrupts */
  430. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  431. ch_inte &= ~(dev_ch_int & TSI721_INT_IMSG_CHAN_M);
  432. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  433. /*
  434. * Process Inbound Message interrupt for each MBOX
  435. */
  436. for (ch = 4; ch < RIO_MAX_MBOX + 4; ch++) {
  437. if (!(dev_ch_int & TSI721_INT_IMSG_CHAN(ch)))
  438. continue;
  439. tsi721_imsg_handler(priv, ch);
  440. }
  441. }
  442. if (dev_ch_int & TSI721_INT_OMSG_CHAN_M) { /* Outbound Msg */
  443. /* Disable signaled OB MSG Channel interrupts */
  444. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  445. ch_inte &= ~(dev_ch_int & TSI721_INT_OMSG_CHAN_M);
  446. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  447. /*
  448. * Process Outbound Message interrupts for each MBOX
  449. */
  450. for (ch = 0; ch < RIO_MAX_MBOX; ch++) {
  451. if (!(dev_ch_int & TSI721_INT_OMSG_CHAN(ch)))
  452. continue;
  453. tsi721_omsg_handler(priv, ch);
  454. }
  455. }
  456. }
  457. if (dev_int & TSI721_DEV_INT_SRIO) {
  458. /* Service SRIO MAC interrupts */
  459. intval = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
  460. if (intval & TSI721_RIO_EM_INT_STAT_PW_RX)
  461. tsi721_pw_handler(priv);
  462. }
  463. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  464. if (dev_int & TSI721_DEV_INT_BDMA_CH) {
  465. int ch;
  466. if (dev_ch_int & TSI721_INT_BDMA_CHAN_M) {
  467. tsi_debug(DMA, &priv->pdev->dev,
  468. "IRQ from DMA channel 0x%08x", dev_ch_int);
  469. for (ch = 0; ch < TSI721_DMA_MAXCH; ch++) {
  470. if (!(dev_ch_int & TSI721_INT_BDMA_CHAN(ch)))
  471. continue;
  472. tsi721_bdma_handler(&priv->bdma[ch]);
  473. }
  474. }
  475. }
  476. #endif
  477. /* For MSI mode re-enable device-level interrupts */
  478. if (priv->flags & TSI721_USING_MSI) {
  479. dev_int = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
  480. TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
  481. iowrite32(dev_int, priv->regs + TSI721_DEV_INTE);
  482. }
  483. return IRQ_HANDLED;
  484. }
  485. static void tsi721_interrupts_init(struct tsi721_device *priv)
  486. {
  487. u32 intr;
  488. /* Enable IDB interrupts */
  489. iowrite32(TSI721_SR_CHINT_ALL,
  490. priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  491. iowrite32(TSI721_SR_CHINT_IDBQRCV,
  492. priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  493. /* Enable SRIO MAC interrupts */
  494. iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT,
  495. priv->regs + TSI721_RIO_EM_DEV_INT_EN);
  496. /* Enable interrupts from channels in use */
  497. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  498. intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE) |
  499. (TSI721_INT_BDMA_CHAN_M &
  500. ~TSI721_INT_BDMA_CHAN(TSI721_DMACH_MAINT));
  501. #else
  502. intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE);
  503. #endif
  504. iowrite32(intr, priv->regs + TSI721_DEV_CHAN_INTE);
  505. if (priv->flags & TSI721_USING_MSIX)
  506. intr = TSI721_DEV_INT_SRIO;
  507. else
  508. intr = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
  509. TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
  510. iowrite32(intr, priv->regs + TSI721_DEV_INTE);
  511. ioread32(priv->regs + TSI721_DEV_INTE);
  512. }
  513. #ifdef CONFIG_PCI_MSI
  514. /**
  515. * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
  516. * @irq: Linux interrupt number
  517. * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
  518. *
  519. * Handles outbound messaging interrupts signaled using MSI-X.
  520. */
  521. static irqreturn_t tsi721_omsg_msix(int irq, void *ptr)
  522. {
  523. struct tsi721_device *priv = (struct tsi721_device *)ptr;
  524. int mbox;
  525. mbox = (irq - priv->msix[TSI721_VECT_OMB0_DONE].vector) % RIO_MAX_MBOX;
  526. tsi721_omsg_handler(priv, mbox);
  527. return IRQ_HANDLED;
  528. }
  529. /**
  530. * tsi721_imsg_msix - MSI-X interrupt handler for inbound messaging
  531. * @irq: Linux interrupt number
  532. * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
  533. *
  534. * Handles inbound messaging interrupts signaled using MSI-X.
  535. */
  536. static irqreturn_t tsi721_imsg_msix(int irq, void *ptr)
  537. {
  538. struct tsi721_device *priv = (struct tsi721_device *)ptr;
  539. int mbox;
  540. mbox = (irq - priv->msix[TSI721_VECT_IMB0_RCV].vector) % RIO_MAX_MBOX;
  541. tsi721_imsg_handler(priv, mbox + 4);
  542. return IRQ_HANDLED;
  543. }
  544. /**
  545. * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
  546. * @irq: Linux interrupt number
  547. * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
  548. *
  549. * Handles Tsi721 interrupts from SRIO MAC.
  550. */
  551. static irqreturn_t tsi721_srio_msix(int irq, void *ptr)
  552. {
  553. struct tsi721_device *priv = (struct tsi721_device *)ptr;
  554. u32 srio_int;
  555. /* Service SRIO MAC interrupts */
  556. srio_int = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
  557. if (srio_int & TSI721_RIO_EM_INT_STAT_PW_RX)
  558. tsi721_pw_handler(priv);
  559. return IRQ_HANDLED;
  560. }
  561. /**
  562. * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
  563. * @irq: Linux interrupt number
  564. * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
  565. *
  566. * Handles Tsi721 interrupts from SR2PC Channel.
  567. * NOTE: At this moment services only one SR2PC channel associated with inbound
  568. * doorbells.
  569. */
  570. static irqreturn_t tsi721_sr2pc_ch_msix(int irq, void *ptr)
  571. {
  572. struct tsi721_device *priv = (struct tsi721_device *)ptr;
  573. u32 sr_ch_int;
  574. /* Service Inbound DB interrupt from SR2PC channel */
  575. sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  576. if (sr_ch_int & TSI721_SR_CHINT_IDBQRCV)
  577. tsi721_dbell_handler(priv);
  578. /* Clear interrupts */
  579. iowrite32(sr_ch_int, priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  580. /* Read back to ensure that interrupt was cleared */
  581. sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  582. return IRQ_HANDLED;
  583. }
  584. /**
  585. * tsi721_request_msix - register interrupt service for MSI-X mode.
  586. * @priv: tsi721 device-specific data structure
  587. *
  588. * Registers MSI-X interrupt service routines for interrupts that are active
  589. * immediately after mport initialization. Messaging interrupt service routines
  590. * should be registered during corresponding open requests.
  591. */
  592. static int tsi721_request_msix(struct tsi721_device *priv)
  593. {
  594. int err = 0;
  595. err = request_irq(priv->msix[TSI721_VECT_IDB].vector,
  596. tsi721_sr2pc_ch_msix, 0,
  597. priv->msix[TSI721_VECT_IDB].irq_name, (void *)priv);
  598. if (err)
  599. return err;
  600. err = request_irq(priv->msix[TSI721_VECT_PWRX].vector,
  601. tsi721_srio_msix, 0,
  602. priv->msix[TSI721_VECT_PWRX].irq_name, (void *)priv);
  603. if (err) {
  604. free_irq(priv->msix[TSI721_VECT_IDB].vector, (void *)priv);
  605. return err;
  606. }
  607. return 0;
  608. }
  609. /**
  610. * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
  611. * @priv: pointer to tsi721 private data
  612. *
  613. * Configures MSI-X support for Tsi721. Supports only an exact number
  614. * of requested vectors.
  615. */
  616. static int tsi721_enable_msix(struct tsi721_device *priv)
  617. {
  618. struct msix_entry entries[TSI721_VECT_MAX];
  619. int err;
  620. int i;
  621. entries[TSI721_VECT_IDB].entry = TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE);
  622. entries[TSI721_VECT_PWRX].entry = TSI721_MSIX_SRIO_MAC_INT;
  623. /*
  624. * Initialize MSI-X entries for Messaging Engine:
  625. * this driver supports four RIO mailboxes (inbound and outbound)
  626. * NOTE: Inbound message MBOX 0...4 use IB channels 4...7. Therefore
  627. * offset +4 is added to IB MBOX number.
  628. */
  629. for (i = 0; i < RIO_MAX_MBOX; i++) {
  630. entries[TSI721_VECT_IMB0_RCV + i].entry =
  631. TSI721_MSIX_IMSG_DQ_RCV(i + 4);
  632. entries[TSI721_VECT_IMB0_INT + i].entry =
  633. TSI721_MSIX_IMSG_INT(i + 4);
  634. entries[TSI721_VECT_OMB0_DONE + i].entry =
  635. TSI721_MSIX_OMSG_DONE(i);
  636. entries[TSI721_VECT_OMB0_INT + i].entry =
  637. TSI721_MSIX_OMSG_INT(i);
  638. }
  639. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  640. /*
  641. * Initialize MSI-X entries for Block DMA Engine:
  642. * this driver supports XXX DMA channels
  643. * (one is reserved for SRIO maintenance transactions)
  644. */
  645. for (i = 0; i < TSI721_DMA_CHNUM; i++) {
  646. entries[TSI721_VECT_DMA0_DONE + i].entry =
  647. TSI721_MSIX_DMACH_DONE(i);
  648. entries[TSI721_VECT_DMA0_INT + i].entry =
  649. TSI721_MSIX_DMACH_INT(i);
  650. }
  651. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  652. err = pci_enable_msix_exact(priv->pdev, entries, ARRAY_SIZE(entries));
  653. if (err) {
  654. tsi_err(&priv->pdev->dev,
  655. "Failed to enable MSI-X (err=%d)", err);
  656. return err;
  657. }
  658. /*
  659. * Copy MSI-X vector information into tsi721 private structure
  660. */
  661. priv->msix[TSI721_VECT_IDB].vector = entries[TSI721_VECT_IDB].vector;
  662. snprintf(priv->msix[TSI721_VECT_IDB].irq_name, IRQ_DEVICE_NAME_MAX,
  663. DRV_NAME "-idb@pci:%s", pci_name(priv->pdev));
  664. priv->msix[TSI721_VECT_PWRX].vector = entries[TSI721_VECT_PWRX].vector;
  665. snprintf(priv->msix[TSI721_VECT_PWRX].irq_name, IRQ_DEVICE_NAME_MAX,
  666. DRV_NAME "-pwrx@pci:%s", pci_name(priv->pdev));
  667. for (i = 0; i < RIO_MAX_MBOX; i++) {
  668. priv->msix[TSI721_VECT_IMB0_RCV + i].vector =
  669. entries[TSI721_VECT_IMB0_RCV + i].vector;
  670. snprintf(priv->msix[TSI721_VECT_IMB0_RCV + i].irq_name,
  671. IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbr%d@pci:%s",
  672. i, pci_name(priv->pdev));
  673. priv->msix[TSI721_VECT_IMB0_INT + i].vector =
  674. entries[TSI721_VECT_IMB0_INT + i].vector;
  675. snprintf(priv->msix[TSI721_VECT_IMB0_INT + i].irq_name,
  676. IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbi%d@pci:%s",
  677. i, pci_name(priv->pdev));
  678. priv->msix[TSI721_VECT_OMB0_DONE + i].vector =
  679. entries[TSI721_VECT_OMB0_DONE + i].vector;
  680. snprintf(priv->msix[TSI721_VECT_OMB0_DONE + i].irq_name,
  681. IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombd%d@pci:%s",
  682. i, pci_name(priv->pdev));
  683. priv->msix[TSI721_VECT_OMB0_INT + i].vector =
  684. entries[TSI721_VECT_OMB0_INT + i].vector;
  685. snprintf(priv->msix[TSI721_VECT_OMB0_INT + i].irq_name,
  686. IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombi%d@pci:%s",
  687. i, pci_name(priv->pdev));
  688. }
  689. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  690. for (i = 0; i < TSI721_DMA_CHNUM; i++) {
  691. priv->msix[TSI721_VECT_DMA0_DONE + i].vector =
  692. entries[TSI721_VECT_DMA0_DONE + i].vector;
  693. snprintf(priv->msix[TSI721_VECT_DMA0_DONE + i].irq_name,
  694. IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmad%d@pci:%s",
  695. i, pci_name(priv->pdev));
  696. priv->msix[TSI721_VECT_DMA0_INT + i].vector =
  697. entries[TSI721_VECT_DMA0_INT + i].vector;
  698. snprintf(priv->msix[TSI721_VECT_DMA0_INT + i].irq_name,
  699. IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmai%d@pci:%s",
  700. i, pci_name(priv->pdev));
  701. }
  702. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  703. return 0;
  704. }
  705. #endif /* CONFIG_PCI_MSI */
  706. static int tsi721_request_irq(struct tsi721_device *priv)
  707. {
  708. int err;
  709. #ifdef CONFIG_PCI_MSI
  710. if (priv->flags & TSI721_USING_MSIX)
  711. err = tsi721_request_msix(priv);
  712. else
  713. #endif
  714. err = request_irq(priv->pdev->irq, tsi721_irqhandler,
  715. (priv->flags & TSI721_USING_MSI) ? 0 : IRQF_SHARED,
  716. DRV_NAME, (void *)priv);
  717. if (err)
  718. tsi_err(&priv->pdev->dev,
  719. "Unable to allocate interrupt, err=%d", err);
  720. return err;
  721. }
  722. static void tsi721_free_irq(struct tsi721_device *priv)
  723. {
  724. #ifdef CONFIG_PCI_MSI
  725. if (priv->flags & TSI721_USING_MSIX) {
  726. free_irq(priv->msix[TSI721_VECT_IDB].vector, (void *)priv);
  727. free_irq(priv->msix[TSI721_VECT_PWRX].vector, (void *)priv);
  728. } else
  729. #endif
  730. free_irq(priv->pdev->irq, (void *)priv);
  731. }
  732. static int
  733. tsi721_obw_alloc(struct tsi721_device *priv, struct tsi721_obw_bar *pbar,
  734. u32 size, int *win_id)
  735. {
  736. u64 win_base;
  737. u64 bar_base;
  738. u64 bar_end;
  739. u32 align;
  740. struct tsi721_ob_win *win;
  741. struct tsi721_ob_win *new_win = NULL;
  742. int new_win_idx = -1;
  743. int i = 0;
  744. bar_base = pbar->base;
  745. bar_end = bar_base + pbar->size;
  746. win_base = bar_base;
  747. align = size/TSI721_PC2SR_ZONES;
  748. while (i < TSI721_IBWIN_NUM) {
  749. for (i = 0; i < TSI721_IBWIN_NUM; i++) {
  750. if (!priv->ob_win[i].active) {
  751. if (new_win == NULL) {
  752. new_win = &priv->ob_win[i];
  753. new_win_idx = i;
  754. }
  755. continue;
  756. }
  757. /*
  758. * If this window belongs to the current BAR check it
  759. * for overlap
  760. */
  761. win = &priv->ob_win[i];
  762. if (win->base >= bar_base && win->base < bar_end) {
  763. if (win_base < (win->base + win->size) &&
  764. (win_base + size) > win->base) {
  765. /* Overlap detected */
  766. win_base = win->base + win->size;
  767. win_base = ALIGN(win_base, align);
  768. break;
  769. }
  770. }
  771. }
  772. }
  773. if (win_base + size > bar_end)
  774. return -ENOMEM;
  775. if (!new_win) {
  776. tsi_err(&priv->pdev->dev, "OBW count tracking failed");
  777. return -EIO;
  778. }
  779. new_win->active = true;
  780. new_win->base = win_base;
  781. new_win->size = size;
  782. new_win->pbar = pbar;
  783. priv->obwin_cnt--;
  784. pbar->free -= size;
  785. *win_id = new_win_idx;
  786. return 0;
  787. }
  788. static int tsi721_map_outb_win(struct rio_mport *mport, u16 destid, u64 rstart,
  789. u32 size, u32 flags, dma_addr_t *laddr)
  790. {
  791. struct tsi721_device *priv = mport->priv;
  792. int i;
  793. struct tsi721_obw_bar *pbar;
  794. struct tsi721_ob_win *ob_win;
  795. int obw = -1;
  796. u32 rval;
  797. u64 rio_addr;
  798. u32 zsize;
  799. int ret = -ENOMEM;
  800. tsi_debug(OBW, &priv->pdev->dev,
  801. "did=%d ra=0x%llx sz=0x%x", destid, rstart, size);
  802. if (!is_power_of_2(size) || (size < 0x8000) || (rstart & (size - 1)))
  803. return -EINVAL;
  804. if (priv->obwin_cnt == 0)
  805. return -EBUSY;
  806. for (i = 0; i < 2; i++) {
  807. if (priv->p2r_bar[i].free >= size) {
  808. pbar = &priv->p2r_bar[i];
  809. ret = tsi721_obw_alloc(priv, pbar, size, &obw);
  810. if (!ret)
  811. break;
  812. }
  813. }
  814. if (ret)
  815. return ret;
  816. WARN_ON(obw == -1);
  817. ob_win = &priv->ob_win[obw];
  818. ob_win->destid = destid;
  819. ob_win->rstart = rstart;
  820. tsi_debug(OBW, &priv->pdev->dev,
  821. "allocated OBW%d @%llx", obw, ob_win->base);
  822. /*
  823. * Configure Outbound Window
  824. */
  825. zsize = size/TSI721_PC2SR_ZONES;
  826. rio_addr = rstart;
  827. /*
  828. * Program Address Translation Zones:
  829. * This implementation uses all 8 zones associated wit window.
  830. */
  831. for (i = 0; i < TSI721_PC2SR_ZONES; i++) {
  832. while (ioread32(priv->regs + TSI721_ZONE_SEL) &
  833. TSI721_ZONE_SEL_GO) {
  834. udelay(1);
  835. }
  836. rval = (u32)(rio_addr & TSI721_LUT_DATA0_ADD) |
  837. TSI721_LUT_DATA0_NREAD | TSI721_LUT_DATA0_NWR;
  838. iowrite32(rval, priv->regs + TSI721_LUT_DATA0);
  839. rval = (u32)(rio_addr >> 32);
  840. iowrite32(rval, priv->regs + TSI721_LUT_DATA1);
  841. rval = destid;
  842. iowrite32(rval, priv->regs + TSI721_LUT_DATA2);
  843. rval = TSI721_ZONE_SEL_GO | (obw << 3) | i;
  844. iowrite32(rval, priv->regs + TSI721_ZONE_SEL);
  845. rio_addr += zsize;
  846. }
  847. iowrite32(TSI721_OBWIN_SIZE(size) << 8,
  848. priv->regs + TSI721_OBWINSZ(obw));
  849. iowrite32((u32)(ob_win->base >> 32), priv->regs + TSI721_OBWINUB(obw));
  850. iowrite32((u32)(ob_win->base & TSI721_OBWINLB_BA) | TSI721_OBWINLB_WEN,
  851. priv->regs + TSI721_OBWINLB(obw));
  852. *laddr = ob_win->base;
  853. return 0;
  854. }
  855. static void tsi721_unmap_outb_win(struct rio_mport *mport,
  856. u16 destid, u64 rstart)
  857. {
  858. struct tsi721_device *priv = mport->priv;
  859. struct tsi721_ob_win *ob_win;
  860. int i;
  861. tsi_debug(OBW, &priv->pdev->dev, "did=%d ra=0x%llx", destid, rstart);
  862. for (i = 0; i < TSI721_OBWIN_NUM; i++) {
  863. ob_win = &priv->ob_win[i];
  864. if (ob_win->active &&
  865. ob_win->destid == destid && ob_win->rstart == rstart) {
  866. tsi_debug(OBW, &priv->pdev->dev,
  867. "free OBW%d @%llx", i, ob_win->base);
  868. ob_win->active = false;
  869. iowrite32(0, priv->regs + TSI721_OBWINLB(i));
  870. ob_win->pbar->free += ob_win->size;
  871. priv->obwin_cnt++;
  872. break;
  873. }
  874. }
  875. }
  876. /**
  877. * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
  878. * translation regions.
  879. * @priv: pointer to tsi721 private data
  880. *
  881. * Disables SREP translation regions.
  882. */
  883. static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
  884. {
  885. int i, z;
  886. u32 rval;
  887. /* Disable all PC2SR translation windows */
  888. for (i = 0; i < TSI721_OBWIN_NUM; i++)
  889. iowrite32(0, priv->regs + TSI721_OBWINLB(i));
  890. /* Initialize zone lookup tables to avoid ECC errors on reads */
  891. iowrite32(0, priv->regs + TSI721_LUT_DATA0);
  892. iowrite32(0, priv->regs + TSI721_LUT_DATA1);
  893. iowrite32(0, priv->regs + TSI721_LUT_DATA2);
  894. for (i = 0; i < TSI721_OBWIN_NUM; i++) {
  895. for (z = 0; z < TSI721_PC2SR_ZONES; z++) {
  896. while (ioread32(priv->regs + TSI721_ZONE_SEL) &
  897. TSI721_ZONE_SEL_GO) {
  898. udelay(1);
  899. }
  900. rval = TSI721_ZONE_SEL_GO | (i << 3) | z;
  901. iowrite32(rval, priv->regs + TSI721_ZONE_SEL);
  902. }
  903. }
  904. if (priv->p2r_bar[0].size == 0 && priv->p2r_bar[1].size == 0) {
  905. priv->obwin_cnt = 0;
  906. return;
  907. }
  908. priv->p2r_bar[0].free = priv->p2r_bar[0].size;
  909. priv->p2r_bar[1].free = priv->p2r_bar[1].size;
  910. for (i = 0; i < TSI721_OBWIN_NUM; i++)
  911. priv->ob_win[i].active = false;
  912. priv->obwin_cnt = TSI721_OBWIN_NUM;
  913. }
  914. /**
  915. * tsi721_rio_map_inb_mem -- Mapping inbound memory region.
  916. * @mport: RapidIO master port
  917. * @lstart: Local memory space start address.
  918. * @rstart: RapidIO space start address.
  919. * @size: The mapping region size.
  920. * @flags: Flags for mapping. 0 for using default flags.
  921. *
  922. * Return: 0 -- Success.
  923. *
  924. * This function will create the inbound mapping
  925. * from rstart to lstart.
  926. */
  927. static int tsi721_rio_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
  928. u64 rstart, u32 size, u32 flags)
  929. {
  930. struct tsi721_device *priv = mport->priv;
  931. int i, avail = -1;
  932. u32 regval;
  933. struct tsi721_ib_win *ib_win;
  934. bool direct = (lstart == rstart);
  935. u64 ibw_size;
  936. dma_addr_t loc_start;
  937. u64 ibw_start;
  938. struct tsi721_ib_win_mapping *map = NULL;
  939. int ret = -EBUSY;
  940. if (direct) {
  941. /* Calculate minimal acceptable window size and base address */
  942. ibw_size = roundup_pow_of_two(size);
  943. ibw_start = lstart & ~(ibw_size - 1);
  944. tsi_debug(IBW, &priv->pdev->dev,
  945. "Direct (RIO_0x%llx -> PCIe_0x%pad), size=0x%x, ibw_start = 0x%llx",
  946. rstart, &lstart, size, ibw_start);
  947. while ((lstart + size) > (ibw_start + ibw_size)) {
  948. ibw_size *= 2;
  949. ibw_start = lstart & ~(ibw_size - 1);
  950. if (ibw_size > 0x80000000) { /* Limit max size to 2GB */
  951. return -EBUSY;
  952. }
  953. }
  954. loc_start = ibw_start;
  955. map = kzalloc(sizeof(struct tsi721_ib_win_mapping), GFP_ATOMIC);
  956. if (map == NULL)
  957. return -ENOMEM;
  958. } else {
  959. tsi_debug(IBW, &priv->pdev->dev,
  960. "Translated (RIO_0x%llx -> PCIe_0x%pad), size=0x%x",
  961. rstart, &lstart, size);
  962. if (!is_power_of_2(size) || size < 0x1000 ||
  963. ((u64)lstart & (size - 1)) || (rstart & (size - 1)))
  964. return -EINVAL;
  965. if (priv->ibwin_cnt == 0)
  966. return -EBUSY;
  967. ibw_start = rstart;
  968. ibw_size = size;
  969. loc_start = lstart;
  970. }
  971. /*
  972. * Scan for overlapping with active regions and mark the first available
  973. * IB window at the same time.
  974. */
  975. for (i = 0; i < TSI721_IBWIN_NUM; i++) {
  976. ib_win = &priv->ib_win[i];
  977. if (!ib_win->active) {
  978. if (avail == -1) {
  979. avail = i;
  980. ret = 0;
  981. }
  982. } else if (ibw_start < (ib_win->rstart + ib_win->size) &&
  983. (ibw_start + ibw_size) > ib_win->rstart) {
  984. /* Return error if address translation involved */
  985. if (direct && ib_win->xlat) {
  986. ret = -EFAULT;
  987. break;
  988. }
  989. /*
  990. * Direct mappings usually are larger than originally
  991. * requested fragments - check if this new request fits
  992. * into it.
  993. */
  994. if (rstart >= ib_win->rstart &&
  995. (rstart + size) <= (ib_win->rstart +
  996. ib_win->size)) {
  997. /* We are in - no further mapping required */
  998. map->lstart = lstart;
  999. list_add_tail(&map->node, &ib_win->mappings);
  1000. return 0;
  1001. }
  1002. ret = -EFAULT;
  1003. break;
  1004. }
  1005. }
  1006. if (ret)
  1007. goto out;
  1008. i = avail;
  1009. /* Sanity check: available IB window must be disabled at this point */
  1010. regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
  1011. if (WARN_ON(regval & TSI721_IBWIN_LB_WEN)) {
  1012. ret = -EIO;
  1013. goto out;
  1014. }
  1015. ib_win = &priv->ib_win[i];
  1016. ib_win->active = true;
  1017. ib_win->rstart = ibw_start;
  1018. ib_win->lstart = loc_start;
  1019. ib_win->size = ibw_size;
  1020. ib_win->xlat = (lstart != rstart);
  1021. INIT_LIST_HEAD(&ib_win->mappings);
  1022. /*
  1023. * When using direct IBW mapping and have larger than requested IBW size
  1024. * we can have multiple local memory blocks mapped through the same IBW
  1025. * To handle this situation we maintain list of "clients" for such IBWs.
  1026. */
  1027. if (direct) {
  1028. map->lstart = lstart;
  1029. list_add_tail(&map->node, &ib_win->mappings);
  1030. }
  1031. iowrite32(TSI721_IBWIN_SIZE(ibw_size) << 8,
  1032. priv->regs + TSI721_IBWIN_SZ(i));
  1033. iowrite32(((u64)loc_start >> 32), priv->regs + TSI721_IBWIN_TUA(i));
  1034. iowrite32(((u64)loc_start & TSI721_IBWIN_TLA_ADD),
  1035. priv->regs + TSI721_IBWIN_TLA(i));
  1036. iowrite32(ibw_start >> 32, priv->regs + TSI721_IBWIN_UB(i));
  1037. iowrite32((ibw_start & TSI721_IBWIN_LB_BA) | TSI721_IBWIN_LB_WEN,
  1038. priv->regs + TSI721_IBWIN_LB(i));
  1039. priv->ibwin_cnt--;
  1040. tsi_debug(IBW, &priv->pdev->dev,
  1041. "Configured IBWIN%d (RIO_0x%llx -> PCIe_0x%pad), size=0x%llx",
  1042. i, ibw_start, &loc_start, ibw_size);
  1043. return 0;
  1044. out:
  1045. kfree(map);
  1046. return ret;
  1047. }
  1048. /**
  1049. * tsi721_rio_unmap_inb_mem -- Unmapping inbound memory region.
  1050. * @mport: RapidIO master port
  1051. * @lstart: Local memory space start address.
  1052. */
  1053. static void tsi721_rio_unmap_inb_mem(struct rio_mport *mport,
  1054. dma_addr_t lstart)
  1055. {
  1056. struct tsi721_device *priv = mport->priv;
  1057. struct tsi721_ib_win *ib_win;
  1058. int i;
  1059. tsi_debug(IBW, &priv->pdev->dev,
  1060. "Unmap IBW mapped to PCIe_0x%pad", &lstart);
  1061. /* Search for matching active inbound translation window */
  1062. for (i = 0; i < TSI721_IBWIN_NUM; i++) {
  1063. ib_win = &priv->ib_win[i];
  1064. /* Address translating IBWs must to be an exact march */
  1065. if (!ib_win->active ||
  1066. (ib_win->xlat && lstart != ib_win->lstart))
  1067. continue;
  1068. if (lstart >= ib_win->lstart &&
  1069. lstart < (ib_win->lstart + ib_win->size)) {
  1070. if (!ib_win->xlat) {
  1071. struct tsi721_ib_win_mapping *map;
  1072. int found = 0;
  1073. list_for_each_entry(map,
  1074. &ib_win->mappings, node) {
  1075. if (map->lstart == lstart) {
  1076. list_del(&map->node);
  1077. kfree(map);
  1078. found = 1;
  1079. break;
  1080. }
  1081. }
  1082. if (!found)
  1083. continue;
  1084. if (!list_empty(&ib_win->mappings))
  1085. break;
  1086. }
  1087. tsi_debug(IBW, &priv->pdev->dev, "Disable IBWIN_%d", i);
  1088. iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
  1089. ib_win->active = false;
  1090. priv->ibwin_cnt++;
  1091. break;
  1092. }
  1093. }
  1094. if (i == TSI721_IBWIN_NUM)
  1095. tsi_debug(IBW, &priv->pdev->dev,
  1096. "IB window mapped to %pad not found", &lstart);
  1097. }
  1098. /**
  1099. * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
  1100. * translation regions.
  1101. * @priv: pointer to tsi721 private data
  1102. *
  1103. * Disables inbound windows.
  1104. */
  1105. static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
  1106. {
  1107. int i;
  1108. /* Disable all SR2PC inbound windows */
  1109. for (i = 0; i < TSI721_IBWIN_NUM; i++)
  1110. iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
  1111. priv->ibwin_cnt = TSI721_IBWIN_NUM;
  1112. }
  1113. /*
  1114. * tsi721_close_sr2pc_mapping - closes all active inbound (SRIO->PCIe)
  1115. * translation regions.
  1116. * @priv: pointer to tsi721 device private data
  1117. */
  1118. static void tsi721_close_sr2pc_mapping(struct tsi721_device *priv)
  1119. {
  1120. struct tsi721_ib_win *ib_win;
  1121. int i;
  1122. /* Disable all active SR2PC inbound windows */
  1123. for (i = 0; i < TSI721_IBWIN_NUM; i++) {
  1124. ib_win = &priv->ib_win[i];
  1125. if (ib_win->active) {
  1126. iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
  1127. ib_win->active = false;
  1128. }
  1129. }
  1130. }
  1131. /**
  1132. * tsi721_port_write_init - Inbound port write interface init
  1133. * @priv: pointer to tsi721 private data
  1134. *
  1135. * Initializes inbound port write handler.
  1136. * Returns %0 on success or %-ENOMEM on failure.
  1137. */
  1138. static int tsi721_port_write_init(struct tsi721_device *priv)
  1139. {
  1140. priv->pw_discard_count = 0;
  1141. INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
  1142. spin_lock_init(&priv->pw_fifo_lock);
  1143. if (kfifo_alloc(&priv->pw_fifo,
  1144. TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
  1145. tsi_err(&priv->pdev->dev, "PW FIFO allocation failed");
  1146. return -ENOMEM;
  1147. }
  1148. /* Use reliable port-write capture mode */
  1149. iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
  1150. return 0;
  1151. }
  1152. static void tsi721_port_write_free(struct tsi721_device *priv)
  1153. {
  1154. kfifo_free(&priv->pw_fifo);
  1155. }
  1156. static int tsi721_doorbell_init(struct tsi721_device *priv)
  1157. {
  1158. /* Outbound Doorbells do not require any setup.
  1159. * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
  1160. * That BAR1 was mapped during the probe routine.
  1161. */
  1162. /* Initialize Inbound Doorbell processing DPC and queue */
  1163. priv->db_discard_count = 0;
  1164. INIT_WORK(&priv->idb_work, tsi721_db_dpc);
  1165. /* Allocate buffer for inbound doorbells queue */
  1166. priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
  1167. IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
  1168. &priv->idb_dma, GFP_KERNEL);
  1169. if (!priv->idb_base)
  1170. return -ENOMEM;
  1171. tsi_debug(DBELL, &priv->pdev->dev,
  1172. "Allocated IDB buffer @ %p (phys = %pad)",
  1173. priv->idb_base, &priv->idb_dma);
  1174. iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
  1175. priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
  1176. iowrite32(((u64)priv->idb_dma >> 32),
  1177. priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
  1178. iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
  1179. priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
  1180. /* Enable accepting all inbound doorbells */
  1181. iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
  1182. iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
  1183. iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
  1184. return 0;
  1185. }
  1186. static void tsi721_doorbell_free(struct tsi721_device *priv)
  1187. {
  1188. if (priv->idb_base == NULL)
  1189. return;
  1190. /* Free buffer allocated for inbound doorbell queue */
  1191. dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
  1192. priv->idb_base, priv->idb_dma);
  1193. priv->idb_base = NULL;
  1194. }
  1195. /**
  1196. * tsi721_bdma_maint_init - Initialize maintenance request BDMA channel.
  1197. * @priv: pointer to tsi721 private data
  1198. *
  1199. * Initialize BDMA channel allocated for RapidIO maintenance read/write
  1200. * request generation
  1201. * Returns %0 on success or %-ENOMEM on failure.
  1202. */
  1203. static int tsi721_bdma_maint_init(struct tsi721_device *priv)
  1204. {
  1205. struct tsi721_dma_desc *bd_ptr;
  1206. u64 *sts_ptr;
  1207. dma_addr_t bd_phys, sts_phys;
  1208. int sts_size;
  1209. int bd_num = 2;
  1210. void __iomem *regs;
  1211. tsi_debug(MAINT, &priv->pdev->dev,
  1212. "Init BDMA_%d Maintenance requests", TSI721_DMACH_MAINT);
  1213. /*
  1214. * Initialize DMA channel for maintenance requests
  1215. */
  1216. priv->mdma.ch_id = TSI721_DMACH_MAINT;
  1217. regs = priv->regs + TSI721_DMAC_BASE(TSI721_DMACH_MAINT);
  1218. /* Allocate space for DMA descriptors */
  1219. bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
  1220. bd_num * sizeof(struct tsi721_dma_desc),
  1221. &bd_phys, GFP_KERNEL);
  1222. if (!bd_ptr)
  1223. return -ENOMEM;
  1224. priv->mdma.bd_num = bd_num;
  1225. priv->mdma.bd_phys = bd_phys;
  1226. priv->mdma.bd_base = bd_ptr;
  1227. tsi_debug(MAINT, &priv->pdev->dev, "DMA descriptors @ %p (phys = %pad)",
  1228. bd_ptr, &bd_phys);
  1229. /* Allocate space for descriptor status FIFO */
  1230. sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
  1231. bd_num : TSI721_DMA_MINSTSSZ;
  1232. sts_size = roundup_pow_of_two(sts_size);
  1233. sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
  1234. sts_size * sizeof(struct tsi721_dma_sts),
  1235. &sts_phys, GFP_KERNEL);
  1236. if (!sts_ptr) {
  1237. /* Free space allocated for DMA descriptors */
  1238. dma_free_coherent(&priv->pdev->dev,
  1239. bd_num * sizeof(struct tsi721_dma_desc),
  1240. bd_ptr, bd_phys);
  1241. priv->mdma.bd_base = NULL;
  1242. return -ENOMEM;
  1243. }
  1244. priv->mdma.sts_phys = sts_phys;
  1245. priv->mdma.sts_base = sts_ptr;
  1246. priv->mdma.sts_size = sts_size;
  1247. tsi_debug(MAINT, &priv->pdev->dev,
  1248. "desc status FIFO @ %p (phys = %pad) size=0x%x",
  1249. sts_ptr, &sts_phys, sts_size);
  1250. /* Initialize DMA descriptors ring */
  1251. bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
  1252. bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
  1253. TSI721_DMAC_DPTRL_MASK);
  1254. bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
  1255. /* Setup DMA descriptor pointers */
  1256. iowrite32(((u64)bd_phys >> 32), regs + TSI721_DMAC_DPTRH);
  1257. iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
  1258. regs + TSI721_DMAC_DPTRL);
  1259. /* Setup descriptor status FIFO */
  1260. iowrite32(((u64)sts_phys >> 32), regs + TSI721_DMAC_DSBH);
  1261. iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
  1262. regs + TSI721_DMAC_DSBL);
  1263. iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
  1264. regs + TSI721_DMAC_DSSZ);
  1265. /* Clear interrupt bits */
  1266. iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
  1267. ioread32(regs + TSI721_DMAC_INT);
  1268. /* Toggle DMA channel initialization */
  1269. iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
  1270. ioread32(regs + TSI721_DMAC_CTL);
  1271. udelay(10);
  1272. return 0;
  1273. }
  1274. static int tsi721_bdma_maint_free(struct tsi721_device *priv)
  1275. {
  1276. u32 ch_stat;
  1277. struct tsi721_bdma_maint *mdma = &priv->mdma;
  1278. void __iomem *regs = priv->regs + TSI721_DMAC_BASE(mdma->ch_id);
  1279. if (mdma->bd_base == NULL)
  1280. return 0;
  1281. /* Check if DMA channel still running */
  1282. ch_stat = ioread32(regs + TSI721_DMAC_STS);
  1283. if (ch_stat & TSI721_DMAC_STS_RUN)
  1284. return -EFAULT;
  1285. /* Put DMA channel into init state */
  1286. iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
  1287. /* Free space allocated for DMA descriptors */
  1288. dma_free_coherent(&priv->pdev->dev,
  1289. mdma->bd_num * sizeof(struct tsi721_dma_desc),
  1290. mdma->bd_base, mdma->bd_phys);
  1291. mdma->bd_base = NULL;
  1292. /* Free space allocated for status FIFO */
  1293. dma_free_coherent(&priv->pdev->dev,
  1294. mdma->sts_size * sizeof(struct tsi721_dma_sts),
  1295. mdma->sts_base, mdma->sts_phys);
  1296. mdma->sts_base = NULL;
  1297. return 0;
  1298. }
  1299. /* Enable Inbound Messaging Interrupts */
  1300. static void
  1301. tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
  1302. u32 inte_mask)
  1303. {
  1304. u32 rval;
  1305. if (!inte_mask)
  1306. return;
  1307. /* Clear pending Inbound Messaging interrupts */
  1308. iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
  1309. /* Enable Inbound Messaging interrupts */
  1310. rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
  1311. iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
  1312. if (priv->flags & TSI721_USING_MSIX)
  1313. return; /* Finished if we are in MSI-X mode */
  1314. /*
  1315. * For MSI and INTA interrupt signalling we need to enable next levels
  1316. */
  1317. /* Enable Device Channel Interrupt */
  1318. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1319. iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
  1320. priv->regs + TSI721_DEV_CHAN_INTE);
  1321. }
  1322. /* Disable Inbound Messaging Interrupts */
  1323. static void
  1324. tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
  1325. u32 inte_mask)
  1326. {
  1327. u32 rval;
  1328. if (!inte_mask)
  1329. return;
  1330. /* Clear pending Inbound Messaging interrupts */
  1331. iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
  1332. /* Disable Inbound Messaging interrupts */
  1333. rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
  1334. rval &= ~inte_mask;
  1335. iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
  1336. if (priv->flags & TSI721_USING_MSIX)
  1337. return; /* Finished if we are in MSI-X mode */
  1338. /*
  1339. * For MSI and INTA interrupt signalling we need to disable next levels
  1340. */
  1341. /* Disable Device Channel Interrupt */
  1342. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1343. rval &= ~TSI721_INT_IMSG_CHAN(ch);
  1344. iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
  1345. }
  1346. /* Enable Outbound Messaging interrupts */
  1347. static void
  1348. tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
  1349. u32 inte_mask)
  1350. {
  1351. u32 rval;
  1352. if (!inte_mask)
  1353. return;
  1354. /* Clear pending Outbound Messaging interrupts */
  1355. iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
  1356. /* Enable Outbound Messaging channel interrupts */
  1357. rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
  1358. iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
  1359. if (priv->flags & TSI721_USING_MSIX)
  1360. return; /* Finished if we are in MSI-X mode */
  1361. /*
  1362. * For MSI and INTA interrupt signalling we need to enable next levels
  1363. */
  1364. /* Enable Device Channel Interrupt */
  1365. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1366. iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
  1367. priv->regs + TSI721_DEV_CHAN_INTE);
  1368. }
  1369. /* Disable Outbound Messaging interrupts */
  1370. static void
  1371. tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
  1372. u32 inte_mask)
  1373. {
  1374. u32 rval;
  1375. if (!inte_mask)
  1376. return;
  1377. /* Clear pending Outbound Messaging interrupts */
  1378. iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
  1379. /* Disable Outbound Messaging interrupts */
  1380. rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
  1381. rval &= ~inte_mask;
  1382. iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
  1383. if (priv->flags & TSI721_USING_MSIX)
  1384. return; /* Finished if we are in MSI-X mode */
  1385. /*
  1386. * For MSI and INTA interrupt signalling we need to disable next levels
  1387. */
  1388. /* Disable Device Channel Interrupt */
  1389. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1390. rval &= ~TSI721_INT_OMSG_CHAN(ch);
  1391. iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
  1392. }
  1393. /**
  1394. * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
  1395. * @mport: Master port with outbound message queue
  1396. * @rdev: Target of outbound message
  1397. * @mbox: Outbound mailbox
  1398. * @buffer: Message to add to outbound queue
  1399. * @len: Length of message
  1400. */
  1401. static int
  1402. tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
  1403. void *buffer, size_t len)
  1404. {
  1405. struct tsi721_device *priv = mport->priv;
  1406. struct tsi721_omsg_desc *desc;
  1407. u32 tx_slot;
  1408. unsigned long flags;
  1409. if (!priv->omsg_init[mbox] ||
  1410. len > TSI721_MSG_MAX_SIZE || len < 8)
  1411. return -EINVAL;
  1412. spin_lock_irqsave(&priv->omsg_ring[mbox].lock, flags);
  1413. tx_slot = priv->omsg_ring[mbox].tx_slot;
  1414. /* Copy copy message into transfer buffer */
  1415. memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
  1416. if (len & 0x7)
  1417. len += 8;
  1418. /* Build descriptor associated with buffer */
  1419. desc = priv->omsg_ring[mbox].omd_base;
  1420. desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
  1421. #ifdef TSI721_OMSG_DESC_INT
  1422. /* Request IOF_DONE interrupt generation for each N-th frame in queue */
  1423. if (tx_slot % 4 == 0)
  1424. desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
  1425. #endif
  1426. desc[tx_slot].msg_info =
  1427. cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
  1428. (0xe << 12) | (len & 0xff8));
  1429. desc[tx_slot].bufptr_lo =
  1430. cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
  1431. 0xffffffff);
  1432. desc[tx_slot].bufptr_hi =
  1433. cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
  1434. priv->omsg_ring[mbox].wr_count++;
  1435. /* Go to next descriptor */
  1436. if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
  1437. priv->omsg_ring[mbox].tx_slot = 0;
  1438. /* Move through the ring link descriptor at the end */
  1439. priv->omsg_ring[mbox].wr_count++;
  1440. }
  1441. mb();
  1442. /* Set new write count value */
  1443. iowrite32(priv->omsg_ring[mbox].wr_count,
  1444. priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1445. ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1446. spin_unlock_irqrestore(&priv->omsg_ring[mbox].lock, flags);
  1447. return 0;
  1448. }
  1449. /**
  1450. * tsi721_omsg_handler - Outbound Message Interrupt Handler
  1451. * @priv: pointer to tsi721 private data
  1452. * @ch: number of OB MSG channel to service
  1453. *
  1454. * Services channel interrupts from outbound messaging engine.
  1455. */
  1456. static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
  1457. {
  1458. u32 omsg_int;
  1459. struct rio_mport *mport = &priv->mport;
  1460. void *dev_id = NULL;
  1461. u32 tx_slot = 0xffffffff;
  1462. int do_callback = 0;
  1463. spin_lock(&priv->omsg_ring[ch].lock);
  1464. omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
  1465. if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
  1466. tsi_info(&priv->pdev->dev,
  1467. "OB MBOX%d: Status FIFO is full", ch);
  1468. if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
  1469. u32 srd_ptr;
  1470. u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
  1471. int i, j;
  1472. /*
  1473. * Find last successfully processed descriptor
  1474. */
  1475. /* Check and clear descriptor status FIFO entries */
  1476. srd_ptr = priv->omsg_ring[ch].sts_rdptr;
  1477. sts_ptr = priv->omsg_ring[ch].sts_base;
  1478. j = srd_ptr * 8;
  1479. while (sts_ptr[j]) {
  1480. for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
  1481. prev_ptr = last_ptr;
  1482. last_ptr = le64_to_cpu(sts_ptr[j]);
  1483. sts_ptr[j] = 0;
  1484. }
  1485. ++srd_ptr;
  1486. srd_ptr %= priv->omsg_ring[ch].sts_size;
  1487. j = srd_ptr * 8;
  1488. }
  1489. if (last_ptr == 0)
  1490. goto no_sts_update;
  1491. priv->omsg_ring[ch].sts_rdptr = srd_ptr;
  1492. iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
  1493. if (!mport->outb_msg[ch].mcback)
  1494. goto no_sts_update;
  1495. /* Inform upper layer about transfer completion */
  1496. tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
  1497. sizeof(struct tsi721_omsg_desc);
  1498. /*
  1499. * Check if this is a Link Descriptor (LD).
  1500. * If yes, ignore LD and use descriptor processed
  1501. * before LD.
  1502. */
  1503. if (tx_slot == priv->omsg_ring[ch].size) {
  1504. if (prev_ptr)
  1505. tx_slot = (prev_ptr -
  1506. (u64)priv->omsg_ring[ch].omd_phys)/
  1507. sizeof(struct tsi721_omsg_desc);
  1508. else
  1509. goto no_sts_update;
  1510. }
  1511. if (tx_slot >= priv->omsg_ring[ch].size)
  1512. tsi_debug(OMSG, &priv->pdev->dev,
  1513. "OB_MSG tx_slot=%x > size=%x",
  1514. tx_slot, priv->omsg_ring[ch].size);
  1515. WARN_ON(tx_slot >= priv->omsg_ring[ch].size);
  1516. /* Move slot index to the next message to be sent */
  1517. ++tx_slot;
  1518. if (tx_slot == priv->omsg_ring[ch].size)
  1519. tx_slot = 0;
  1520. dev_id = priv->omsg_ring[ch].dev_id;
  1521. do_callback = 1;
  1522. }
  1523. no_sts_update:
  1524. if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
  1525. /*
  1526. * Outbound message operation aborted due to error,
  1527. * reinitialize OB MSG channel
  1528. */
  1529. tsi_debug(OMSG, &priv->pdev->dev, "OB MSG ABORT ch_stat=%x",
  1530. ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
  1531. iowrite32(TSI721_OBDMAC_INT_ERROR,
  1532. priv->regs + TSI721_OBDMAC_INT(ch));
  1533. iowrite32(TSI721_OBDMAC_CTL_RETRY_THR | TSI721_OBDMAC_CTL_INIT,
  1534. priv->regs + TSI721_OBDMAC_CTL(ch));
  1535. ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
  1536. /* Inform upper level to clear all pending tx slots */
  1537. dev_id = priv->omsg_ring[ch].dev_id;
  1538. tx_slot = priv->omsg_ring[ch].tx_slot;
  1539. do_callback = 1;
  1540. /* Synch tx_slot tracking */
  1541. iowrite32(priv->omsg_ring[ch].tx_slot,
  1542. priv->regs + TSI721_OBDMAC_DRDCNT(ch));
  1543. ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
  1544. priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
  1545. priv->omsg_ring[ch].sts_rdptr = 0;
  1546. }
  1547. /* Clear channel interrupts */
  1548. iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
  1549. if (!(priv->flags & TSI721_USING_MSIX)) {
  1550. u32 ch_inte;
  1551. /* Re-enable channel interrupts */
  1552. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1553. ch_inte |= TSI721_INT_OMSG_CHAN(ch);
  1554. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  1555. }
  1556. spin_unlock(&priv->omsg_ring[ch].lock);
  1557. if (mport->outb_msg[ch].mcback && do_callback)
  1558. mport->outb_msg[ch].mcback(mport, dev_id, ch, tx_slot);
  1559. }
  1560. /**
  1561. * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
  1562. * @mport: Master port implementing Outbound Messaging Engine
  1563. * @dev_id: Device specific pointer to pass on event
  1564. * @mbox: Mailbox to open
  1565. * @entries: Number of entries in the outbound mailbox ring
  1566. */
  1567. static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
  1568. int mbox, int entries)
  1569. {
  1570. struct tsi721_device *priv = mport->priv;
  1571. struct tsi721_omsg_desc *bd_ptr;
  1572. int i, rc = 0;
  1573. if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
  1574. (entries > (TSI721_OMSGD_RING_SIZE)) ||
  1575. (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
  1576. rc = -EINVAL;
  1577. goto out;
  1578. }
  1579. priv->omsg_ring[mbox].dev_id = dev_id;
  1580. priv->omsg_ring[mbox].size = entries;
  1581. priv->omsg_ring[mbox].sts_rdptr = 0;
  1582. spin_lock_init(&priv->omsg_ring[mbox].lock);
  1583. /* Outbound Msg Buffer allocation based on
  1584. the number of maximum descriptor entries */
  1585. for (i = 0; i < entries; i++) {
  1586. priv->omsg_ring[mbox].omq_base[i] =
  1587. dma_alloc_coherent(
  1588. &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
  1589. &priv->omsg_ring[mbox].omq_phys[i],
  1590. GFP_KERNEL);
  1591. if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
  1592. tsi_debug(OMSG, &priv->pdev->dev,
  1593. "ENOMEM for OB_MSG_%d data buffer", mbox);
  1594. rc = -ENOMEM;
  1595. goto out_buf;
  1596. }
  1597. }
  1598. /* Outbound message descriptor allocation */
  1599. priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
  1600. &priv->pdev->dev,
  1601. (entries + 1) * sizeof(struct tsi721_omsg_desc),
  1602. &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
  1603. if (priv->omsg_ring[mbox].omd_base == NULL) {
  1604. tsi_debug(OMSG, &priv->pdev->dev,
  1605. "ENOMEM for OB_MSG_%d descriptor memory", mbox);
  1606. rc = -ENOMEM;
  1607. goto out_buf;
  1608. }
  1609. priv->omsg_ring[mbox].tx_slot = 0;
  1610. /* Outbound message descriptor status FIFO allocation */
  1611. priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
  1612. priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
  1613. priv->omsg_ring[mbox].sts_size *
  1614. sizeof(struct tsi721_dma_sts),
  1615. &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
  1616. if (priv->omsg_ring[mbox].sts_base == NULL) {
  1617. tsi_debug(OMSG, &priv->pdev->dev,
  1618. "ENOMEM for OB_MSG_%d status FIFO", mbox);
  1619. rc = -ENOMEM;
  1620. goto out_desc;
  1621. }
  1622. /*
  1623. * Configure Outbound Messaging Engine
  1624. */
  1625. /* Setup Outbound Message descriptor pointer */
  1626. iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
  1627. priv->regs + TSI721_OBDMAC_DPTRH(mbox));
  1628. iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
  1629. TSI721_OBDMAC_DPTRL_MASK),
  1630. priv->regs + TSI721_OBDMAC_DPTRL(mbox));
  1631. /* Setup Outbound Message descriptor status FIFO */
  1632. iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
  1633. priv->regs + TSI721_OBDMAC_DSBH(mbox));
  1634. iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
  1635. TSI721_OBDMAC_DSBL_MASK),
  1636. priv->regs + TSI721_OBDMAC_DSBL(mbox));
  1637. iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
  1638. priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
  1639. /* Enable interrupts */
  1640. #ifdef CONFIG_PCI_MSI
  1641. if (priv->flags & TSI721_USING_MSIX) {
  1642. int idx = TSI721_VECT_OMB0_DONE + mbox;
  1643. /* Request interrupt service if we are in MSI-X mode */
  1644. rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
  1645. priv->msix[idx].irq_name, (void *)priv);
  1646. if (rc) {
  1647. tsi_debug(OMSG, &priv->pdev->dev,
  1648. "Unable to get MSI-X IRQ for OBOX%d-DONE",
  1649. mbox);
  1650. goto out_stat;
  1651. }
  1652. idx = TSI721_VECT_OMB0_INT + mbox;
  1653. rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
  1654. priv->msix[idx].irq_name, (void *)priv);
  1655. if (rc) {
  1656. tsi_debug(OMSG, &priv->pdev->dev,
  1657. "Unable to get MSI-X IRQ for MBOX%d-INT", mbox);
  1658. idx = TSI721_VECT_OMB0_DONE + mbox;
  1659. free_irq(priv->msix[idx].vector, (void *)priv);
  1660. goto out_stat;
  1661. }
  1662. }
  1663. #endif /* CONFIG_PCI_MSI */
  1664. tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
  1665. /* Initialize Outbound Message descriptors ring */
  1666. bd_ptr = priv->omsg_ring[mbox].omd_base;
  1667. bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
  1668. bd_ptr[entries].msg_info = 0;
  1669. bd_ptr[entries].next_lo =
  1670. cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
  1671. TSI721_OBDMAC_DPTRL_MASK);
  1672. bd_ptr[entries].next_hi =
  1673. cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
  1674. priv->omsg_ring[mbox].wr_count = 0;
  1675. mb();
  1676. /* Initialize Outbound Message engine */
  1677. iowrite32(TSI721_OBDMAC_CTL_RETRY_THR | TSI721_OBDMAC_CTL_INIT,
  1678. priv->regs + TSI721_OBDMAC_CTL(mbox));
  1679. ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1680. udelay(10);
  1681. priv->omsg_init[mbox] = 1;
  1682. return 0;
  1683. #ifdef CONFIG_PCI_MSI
  1684. out_stat:
  1685. dma_free_coherent(&priv->pdev->dev,
  1686. priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
  1687. priv->omsg_ring[mbox].sts_base,
  1688. priv->omsg_ring[mbox].sts_phys);
  1689. priv->omsg_ring[mbox].sts_base = NULL;
  1690. #endif /* CONFIG_PCI_MSI */
  1691. out_desc:
  1692. dma_free_coherent(&priv->pdev->dev,
  1693. (entries + 1) * sizeof(struct tsi721_omsg_desc),
  1694. priv->omsg_ring[mbox].omd_base,
  1695. priv->omsg_ring[mbox].omd_phys);
  1696. priv->omsg_ring[mbox].omd_base = NULL;
  1697. out_buf:
  1698. for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
  1699. if (priv->omsg_ring[mbox].omq_base[i]) {
  1700. dma_free_coherent(&priv->pdev->dev,
  1701. TSI721_MSG_BUFFER_SIZE,
  1702. priv->omsg_ring[mbox].omq_base[i],
  1703. priv->omsg_ring[mbox].omq_phys[i]);
  1704. priv->omsg_ring[mbox].omq_base[i] = NULL;
  1705. }
  1706. }
  1707. out:
  1708. return rc;
  1709. }
  1710. /**
  1711. * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
  1712. * @mport: Master port implementing the outbound message unit
  1713. * @mbox: Mailbox to close
  1714. */
  1715. static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
  1716. {
  1717. struct tsi721_device *priv = mport->priv;
  1718. u32 i;
  1719. if (!priv->omsg_init[mbox])
  1720. return;
  1721. priv->omsg_init[mbox] = 0;
  1722. /* Disable Interrupts */
  1723. tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
  1724. #ifdef CONFIG_PCI_MSI
  1725. if (priv->flags & TSI721_USING_MSIX) {
  1726. free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
  1727. (void *)priv);
  1728. free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
  1729. (void *)priv);
  1730. }
  1731. #endif /* CONFIG_PCI_MSI */
  1732. /* Free OMSG Descriptor Status FIFO */
  1733. dma_free_coherent(&priv->pdev->dev,
  1734. priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
  1735. priv->omsg_ring[mbox].sts_base,
  1736. priv->omsg_ring[mbox].sts_phys);
  1737. priv->omsg_ring[mbox].sts_base = NULL;
  1738. /* Free OMSG descriptors */
  1739. dma_free_coherent(&priv->pdev->dev,
  1740. (priv->omsg_ring[mbox].size + 1) *
  1741. sizeof(struct tsi721_omsg_desc),
  1742. priv->omsg_ring[mbox].omd_base,
  1743. priv->omsg_ring[mbox].omd_phys);
  1744. priv->omsg_ring[mbox].omd_base = NULL;
  1745. /* Free message buffers */
  1746. for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
  1747. if (priv->omsg_ring[mbox].omq_base[i]) {
  1748. dma_free_coherent(&priv->pdev->dev,
  1749. TSI721_MSG_BUFFER_SIZE,
  1750. priv->omsg_ring[mbox].omq_base[i],
  1751. priv->omsg_ring[mbox].omq_phys[i]);
  1752. priv->omsg_ring[mbox].omq_base[i] = NULL;
  1753. }
  1754. }
  1755. }
  1756. /**
  1757. * tsi721_imsg_handler - Inbound Message Interrupt Handler
  1758. * @priv: pointer to tsi721 private data
  1759. * @ch: inbound message channel number to service
  1760. *
  1761. * Services channel interrupts from inbound messaging engine.
  1762. */
  1763. static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
  1764. {
  1765. u32 mbox = ch - 4;
  1766. u32 imsg_int;
  1767. struct rio_mport *mport = &priv->mport;
  1768. spin_lock(&priv->imsg_ring[mbox].lock);
  1769. imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
  1770. if (imsg_int & TSI721_IBDMAC_INT_SRTO)
  1771. tsi_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout", mbox);
  1772. if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
  1773. tsi_info(&priv->pdev->dev, "IB MBOX%d PCIe error", mbox);
  1774. if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
  1775. tsi_info(&priv->pdev->dev, "IB MBOX%d IB free queue low", mbox);
  1776. /* Clear IB channel interrupts */
  1777. iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
  1778. /* If an IB Msg is received notify the upper layer */
  1779. if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
  1780. mport->inb_msg[mbox].mcback)
  1781. mport->inb_msg[mbox].mcback(mport,
  1782. priv->imsg_ring[mbox].dev_id, mbox, -1);
  1783. if (!(priv->flags & TSI721_USING_MSIX)) {
  1784. u32 ch_inte;
  1785. /* Re-enable channel interrupts */
  1786. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1787. ch_inte |= TSI721_INT_IMSG_CHAN(ch);
  1788. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  1789. }
  1790. spin_unlock(&priv->imsg_ring[mbox].lock);
  1791. }
  1792. /**
  1793. * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
  1794. * @mport: Master port implementing the Inbound Messaging Engine
  1795. * @dev_id: Device specific pointer to pass on event
  1796. * @mbox: Mailbox to open
  1797. * @entries: Number of entries in the inbound mailbox ring
  1798. */
  1799. static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
  1800. int mbox, int entries)
  1801. {
  1802. struct tsi721_device *priv = mport->priv;
  1803. int ch = mbox + 4;
  1804. int i;
  1805. u64 *free_ptr;
  1806. int rc = 0;
  1807. if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
  1808. (entries > TSI721_IMSGD_RING_SIZE) ||
  1809. (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
  1810. rc = -EINVAL;
  1811. goto out;
  1812. }
  1813. /* Initialize IB Messaging Ring */
  1814. priv->imsg_ring[mbox].dev_id = dev_id;
  1815. priv->imsg_ring[mbox].size = entries;
  1816. priv->imsg_ring[mbox].rx_slot = 0;
  1817. priv->imsg_ring[mbox].desc_rdptr = 0;
  1818. priv->imsg_ring[mbox].fq_wrptr = 0;
  1819. for (i = 0; i < priv->imsg_ring[mbox].size; i++)
  1820. priv->imsg_ring[mbox].imq_base[i] = NULL;
  1821. spin_lock_init(&priv->imsg_ring[mbox].lock);
  1822. /* Allocate buffers for incoming messages */
  1823. priv->imsg_ring[mbox].buf_base =
  1824. dma_alloc_coherent(&priv->pdev->dev,
  1825. entries * TSI721_MSG_BUFFER_SIZE,
  1826. &priv->imsg_ring[mbox].buf_phys,
  1827. GFP_KERNEL);
  1828. if (priv->imsg_ring[mbox].buf_base == NULL) {
  1829. tsi_err(&priv->pdev->dev,
  1830. "Failed to allocate buffers for IB MBOX%d", mbox);
  1831. rc = -ENOMEM;
  1832. goto out;
  1833. }
  1834. /* Allocate memory for circular free list */
  1835. priv->imsg_ring[mbox].imfq_base =
  1836. dma_alloc_coherent(&priv->pdev->dev,
  1837. entries * 8,
  1838. &priv->imsg_ring[mbox].imfq_phys,
  1839. GFP_KERNEL);
  1840. if (priv->imsg_ring[mbox].imfq_base == NULL) {
  1841. tsi_err(&priv->pdev->dev,
  1842. "Failed to allocate free queue for IB MBOX%d", mbox);
  1843. rc = -ENOMEM;
  1844. goto out_buf;
  1845. }
  1846. /* Allocate memory for Inbound message descriptors */
  1847. priv->imsg_ring[mbox].imd_base =
  1848. dma_alloc_coherent(&priv->pdev->dev,
  1849. entries * sizeof(struct tsi721_imsg_desc),
  1850. &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
  1851. if (priv->imsg_ring[mbox].imd_base == NULL) {
  1852. tsi_err(&priv->pdev->dev,
  1853. "Failed to allocate descriptor memory for IB MBOX%d",
  1854. mbox);
  1855. rc = -ENOMEM;
  1856. goto out_dma;
  1857. }
  1858. /* Fill free buffer pointer list */
  1859. free_ptr = priv->imsg_ring[mbox].imfq_base;
  1860. for (i = 0; i < entries; i++)
  1861. free_ptr[i] = cpu_to_le64(
  1862. (u64)(priv->imsg_ring[mbox].buf_phys) +
  1863. i * 0x1000);
  1864. mb();
  1865. /*
  1866. * For mapping of inbound SRIO Messages into appropriate queues we need
  1867. * to set Inbound Device ID register in the messaging engine. We do it
  1868. * once when first inbound mailbox is requested.
  1869. */
  1870. if (!(priv->flags & TSI721_IMSGID_SET)) {
  1871. iowrite32((u32)priv->mport.host_deviceid,
  1872. priv->regs + TSI721_IB_DEVID);
  1873. priv->flags |= TSI721_IMSGID_SET;
  1874. }
  1875. /*
  1876. * Configure Inbound Messaging channel (ch = mbox + 4)
  1877. */
  1878. /* Setup Inbound Message free queue */
  1879. iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
  1880. priv->regs + TSI721_IBDMAC_FQBH(ch));
  1881. iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
  1882. TSI721_IBDMAC_FQBL_MASK),
  1883. priv->regs+TSI721_IBDMAC_FQBL(ch));
  1884. iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
  1885. priv->regs + TSI721_IBDMAC_FQSZ(ch));
  1886. /* Setup Inbound Message descriptor queue */
  1887. iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
  1888. priv->regs + TSI721_IBDMAC_DQBH(ch));
  1889. iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
  1890. (u32)TSI721_IBDMAC_DQBL_MASK),
  1891. priv->regs+TSI721_IBDMAC_DQBL(ch));
  1892. iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
  1893. priv->regs + TSI721_IBDMAC_DQSZ(ch));
  1894. /* Enable interrupts */
  1895. #ifdef CONFIG_PCI_MSI
  1896. if (priv->flags & TSI721_USING_MSIX) {
  1897. int idx = TSI721_VECT_IMB0_RCV + mbox;
  1898. /* Request interrupt service if we are in MSI-X mode */
  1899. rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
  1900. priv->msix[idx].irq_name, (void *)priv);
  1901. if (rc) {
  1902. tsi_debug(IMSG, &priv->pdev->dev,
  1903. "Unable to get MSI-X IRQ for IBOX%d-DONE",
  1904. mbox);
  1905. goto out_desc;
  1906. }
  1907. idx = TSI721_VECT_IMB0_INT + mbox;
  1908. rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
  1909. priv->msix[idx].irq_name, (void *)priv);
  1910. if (rc) {
  1911. tsi_debug(IMSG, &priv->pdev->dev,
  1912. "Unable to get MSI-X IRQ for IBOX%d-INT", mbox);
  1913. free_irq(
  1914. priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1915. (void *)priv);
  1916. goto out_desc;
  1917. }
  1918. }
  1919. #endif /* CONFIG_PCI_MSI */
  1920. tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
  1921. /* Initialize Inbound Message Engine */
  1922. iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
  1923. ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
  1924. udelay(10);
  1925. priv->imsg_ring[mbox].fq_wrptr = entries - 1;
  1926. iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
  1927. priv->imsg_init[mbox] = 1;
  1928. return 0;
  1929. #ifdef CONFIG_PCI_MSI
  1930. out_desc:
  1931. dma_free_coherent(&priv->pdev->dev,
  1932. priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
  1933. priv->imsg_ring[mbox].imd_base,
  1934. priv->imsg_ring[mbox].imd_phys);
  1935. priv->imsg_ring[mbox].imd_base = NULL;
  1936. #endif /* CONFIG_PCI_MSI */
  1937. out_dma:
  1938. dma_free_coherent(&priv->pdev->dev,
  1939. priv->imsg_ring[mbox].size * 8,
  1940. priv->imsg_ring[mbox].imfq_base,
  1941. priv->imsg_ring[mbox].imfq_phys);
  1942. priv->imsg_ring[mbox].imfq_base = NULL;
  1943. out_buf:
  1944. dma_free_coherent(&priv->pdev->dev,
  1945. priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
  1946. priv->imsg_ring[mbox].buf_base,
  1947. priv->imsg_ring[mbox].buf_phys);
  1948. priv->imsg_ring[mbox].buf_base = NULL;
  1949. out:
  1950. return rc;
  1951. }
  1952. /**
  1953. * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
  1954. * @mport: Master port implementing the Inbound Messaging Engine
  1955. * @mbox: Mailbox to close
  1956. */
  1957. static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
  1958. {
  1959. struct tsi721_device *priv = mport->priv;
  1960. u32 rx_slot;
  1961. int ch = mbox + 4;
  1962. if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
  1963. return;
  1964. priv->imsg_init[mbox] = 0;
  1965. /* Disable Inbound Messaging Engine */
  1966. /* Disable Interrupts */
  1967. tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
  1968. #ifdef CONFIG_PCI_MSI
  1969. if (priv->flags & TSI721_USING_MSIX) {
  1970. free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1971. (void *)priv);
  1972. free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
  1973. (void *)priv);
  1974. }
  1975. #endif /* CONFIG_PCI_MSI */
  1976. /* Clear Inbound Buffer Queue */
  1977. for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
  1978. priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
  1979. /* Free memory allocated for message buffers */
  1980. dma_free_coherent(&priv->pdev->dev,
  1981. priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
  1982. priv->imsg_ring[mbox].buf_base,
  1983. priv->imsg_ring[mbox].buf_phys);
  1984. priv->imsg_ring[mbox].buf_base = NULL;
  1985. /* Free memory allocated for free pointr list */
  1986. dma_free_coherent(&priv->pdev->dev,
  1987. priv->imsg_ring[mbox].size * 8,
  1988. priv->imsg_ring[mbox].imfq_base,
  1989. priv->imsg_ring[mbox].imfq_phys);
  1990. priv->imsg_ring[mbox].imfq_base = NULL;
  1991. /* Free memory allocated for RX descriptors */
  1992. dma_free_coherent(&priv->pdev->dev,
  1993. priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
  1994. priv->imsg_ring[mbox].imd_base,
  1995. priv->imsg_ring[mbox].imd_phys);
  1996. priv->imsg_ring[mbox].imd_base = NULL;
  1997. }
  1998. /**
  1999. * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
  2000. * @mport: Master port implementing the Inbound Messaging Engine
  2001. * @mbox: Inbound mailbox number
  2002. * @buf: Buffer to add to inbound queue
  2003. */
  2004. static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
  2005. {
  2006. struct tsi721_device *priv = mport->priv;
  2007. u32 rx_slot;
  2008. int rc = 0;
  2009. rx_slot = priv->imsg_ring[mbox].rx_slot;
  2010. if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
  2011. tsi_err(&priv->pdev->dev,
  2012. "Error adding inbound buffer %d, buffer exists",
  2013. rx_slot);
  2014. rc = -EINVAL;
  2015. goto out;
  2016. }
  2017. priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
  2018. if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
  2019. priv->imsg_ring[mbox].rx_slot = 0;
  2020. out:
  2021. return rc;
  2022. }
  2023. /**
  2024. * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
  2025. * @mport: Master port implementing the Inbound Messaging Engine
  2026. * @mbox: Inbound mailbox number
  2027. *
  2028. * Returns pointer to the message on success or NULL on failure.
  2029. */
  2030. static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
  2031. {
  2032. struct tsi721_device *priv = mport->priv;
  2033. struct tsi721_imsg_desc *desc;
  2034. u32 rx_slot;
  2035. void *rx_virt = NULL;
  2036. u64 rx_phys;
  2037. void *buf = NULL;
  2038. u64 *free_ptr;
  2039. int ch = mbox + 4;
  2040. int msg_size;
  2041. if (!priv->imsg_init[mbox])
  2042. return NULL;
  2043. desc = priv->imsg_ring[mbox].imd_base;
  2044. desc += priv->imsg_ring[mbox].desc_rdptr;
  2045. if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
  2046. goto out;
  2047. rx_slot = priv->imsg_ring[mbox].rx_slot;
  2048. while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
  2049. if (++rx_slot == priv->imsg_ring[mbox].size)
  2050. rx_slot = 0;
  2051. }
  2052. rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
  2053. le32_to_cpu(desc->bufptr_lo);
  2054. rx_virt = priv->imsg_ring[mbox].buf_base +
  2055. (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
  2056. buf = priv->imsg_ring[mbox].imq_base[rx_slot];
  2057. msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
  2058. if (msg_size == 0)
  2059. msg_size = RIO_MAX_MSG_SIZE;
  2060. memcpy(buf, rx_virt, msg_size);
  2061. priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
  2062. desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
  2063. if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
  2064. priv->imsg_ring[mbox].desc_rdptr = 0;
  2065. iowrite32(priv->imsg_ring[mbox].desc_rdptr,
  2066. priv->regs + TSI721_IBDMAC_DQRP(ch));
  2067. /* Return free buffer into the pointer list */
  2068. free_ptr = priv->imsg_ring[mbox].imfq_base;
  2069. free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
  2070. if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
  2071. priv->imsg_ring[mbox].fq_wrptr = 0;
  2072. iowrite32(priv->imsg_ring[mbox].fq_wrptr,
  2073. priv->regs + TSI721_IBDMAC_FQWP(ch));
  2074. out:
  2075. return buf;
  2076. }
  2077. /**
  2078. * tsi721_messages_init - Initialization of Messaging Engine
  2079. * @priv: pointer to tsi721 private data
  2080. *
  2081. * Configures Tsi721 messaging engine.
  2082. */
  2083. static int tsi721_messages_init(struct tsi721_device *priv)
  2084. {
  2085. int ch;
  2086. iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
  2087. iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
  2088. iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
  2089. /* Set SRIO Message Request/Response Timeout */
  2090. iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
  2091. /* Initialize Inbound Messaging Engine Registers */
  2092. for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
  2093. /* Clear interrupt bits */
  2094. iowrite32(TSI721_IBDMAC_INT_MASK,
  2095. priv->regs + TSI721_IBDMAC_INT(ch));
  2096. /* Clear Status */
  2097. iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
  2098. iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
  2099. priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
  2100. iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
  2101. priv->regs + TSI721_SMSG_ECC_NCOR(ch));
  2102. }
  2103. return 0;
  2104. }
  2105. /**
  2106. * tsi721_query_mport - Fetch inbound message from the Tsi721 MSG Queue
  2107. * @mport: Master port implementing the Inbound Messaging Engine
  2108. * @mbox: Inbound mailbox number
  2109. *
  2110. * Returns pointer to the message on success or NULL on failure.
  2111. */
  2112. static int tsi721_query_mport(struct rio_mport *mport,
  2113. struct rio_mport_attr *attr)
  2114. {
  2115. struct tsi721_device *priv = mport->priv;
  2116. u32 rval;
  2117. rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_ERR_STS_CSR(0)));
  2118. if (rval & RIO_PORT_N_ERR_STS_PORT_OK) {
  2119. rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL2_CSR(0)));
  2120. attr->link_speed = (rval & RIO_PORT_N_CTL2_SEL_BAUD) >> 28;
  2121. rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL_CSR(0)));
  2122. attr->link_width = (rval & RIO_PORT_N_CTL_IPW) >> 27;
  2123. } else
  2124. attr->link_speed = RIO_LINK_DOWN;
  2125. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  2126. attr->flags = RIO_MPORT_DMA | RIO_MPORT_DMA_SG;
  2127. attr->dma_max_sge = 0;
  2128. attr->dma_max_size = TSI721_BDMA_MAX_BCOUNT;
  2129. attr->dma_align = 0;
  2130. #else
  2131. attr->flags = 0;
  2132. #endif
  2133. return 0;
  2134. }
  2135. /**
  2136. * tsi721_disable_ints - disables all device interrupts
  2137. * @priv: pointer to tsi721 private data
  2138. */
  2139. static void tsi721_disable_ints(struct tsi721_device *priv)
  2140. {
  2141. int ch;
  2142. /* Disable all device level interrupts */
  2143. iowrite32(0, priv->regs + TSI721_DEV_INTE);
  2144. /* Disable all Device Channel interrupts */
  2145. iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
  2146. /* Disable all Inbound Msg Channel interrupts */
  2147. for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
  2148. iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
  2149. /* Disable all Outbound Msg Channel interrupts */
  2150. for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
  2151. iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
  2152. /* Disable all general messaging interrupts */
  2153. iowrite32(0, priv->regs + TSI721_SMSG_INTE);
  2154. /* Disable all BDMA Channel interrupts */
  2155. for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
  2156. iowrite32(0,
  2157. priv->regs + TSI721_DMAC_BASE(ch) + TSI721_DMAC_INTE);
  2158. /* Disable all general BDMA interrupts */
  2159. iowrite32(0, priv->regs + TSI721_BDMA_INTE);
  2160. /* Disable all SRIO Channel interrupts */
  2161. for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
  2162. iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
  2163. /* Disable all general SR2PC interrupts */
  2164. iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
  2165. /* Disable all PC2SR interrupts */
  2166. iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
  2167. /* Disable all I2C interrupts */
  2168. iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
  2169. /* Disable SRIO MAC interrupts */
  2170. iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
  2171. iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
  2172. }
  2173. static struct rio_ops tsi721_rio_ops = {
  2174. .lcread = tsi721_lcread,
  2175. .lcwrite = tsi721_lcwrite,
  2176. .cread = tsi721_cread_dma,
  2177. .cwrite = tsi721_cwrite_dma,
  2178. .dsend = tsi721_dsend,
  2179. .open_inb_mbox = tsi721_open_inb_mbox,
  2180. .close_inb_mbox = tsi721_close_inb_mbox,
  2181. .open_outb_mbox = tsi721_open_outb_mbox,
  2182. .close_outb_mbox = tsi721_close_outb_mbox,
  2183. .add_outb_message = tsi721_add_outb_message,
  2184. .add_inb_buffer = tsi721_add_inb_buffer,
  2185. .get_inb_message = tsi721_get_inb_message,
  2186. .map_inb = tsi721_rio_map_inb_mem,
  2187. .unmap_inb = tsi721_rio_unmap_inb_mem,
  2188. .pwenable = tsi721_pw_enable,
  2189. .query_mport = tsi721_query_mport,
  2190. .map_outb = tsi721_map_outb_win,
  2191. .unmap_outb = tsi721_unmap_outb_win,
  2192. };
  2193. static void tsi721_mport_release(struct device *dev)
  2194. {
  2195. struct rio_mport *mport = to_rio_mport(dev);
  2196. tsi_debug(EXIT, dev, "%s id=%d", mport->name, mport->id);
  2197. }
  2198. /**
  2199. * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
  2200. * @priv: pointer to tsi721 private data
  2201. *
  2202. * Configures Tsi721 as RapidIO master port.
  2203. */
  2204. static int tsi721_setup_mport(struct tsi721_device *priv)
  2205. {
  2206. struct pci_dev *pdev = priv->pdev;
  2207. int err = 0;
  2208. struct rio_mport *mport = &priv->mport;
  2209. err = rio_mport_initialize(mport);
  2210. if (err)
  2211. return err;
  2212. mport->ops = &tsi721_rio_ops;
  2213. mport->index = 0;
  2214. mport->sys_size = 0; /* small system */
  2215. mport->phy_type = RIO_PHY_SERIAL;
  2216. mport->priv = (void *)priv;
  2217. mport->phys_efptr = 0x100;
  2218. mport->dev.parent = &pdev->dev;
  2219. mport->dev.release = tsi721_mport_release;
  2220. INIT_LIST_HEAD(&mport->dbells);
  2221. rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
  2222. rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
  2223. rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
  2224. snprintf(mport->name, RIO_MAX_MPORT_NAME, "%s(%s)",
  2225. dev_driver_string(&pdev->dev), dev_name(&pdev->dev));
  2226. /* Hook up interrupt handler */
  2227. #ifdef CONFIG_PCI_MSI
  2228. if (!tsi721_enable_msix(priv))
  2229. priv->flags |= TSI721_USING_MSIX;
  2230. else if (!pci_enable_msi(pdev))
  2231. priv->flags |= TSI721_USING_MSI;
  2232. else
  2233. tsi_debug(MPORT, &pdev->dev,
  2234. "MSI/MSI-X is not available. Using legacy INTx.");
  2235. #endif /* CONFIG_PCI_MSI */
  2236. err = tsi721_request_irq(priv);
  2237. if (err) {
  2238. tsi_err(&pdev->dev, "Unable to get PCI IRQ %02X (err=0x%x)",
  2239. pdev->irq, err);
  2240. return err;
  2241. }
  2242. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  2243. err = tsi721_register_dma(priv);
  2244. if (err)
  2245. goto err_exit;
  2246. #endif
  2247. /* Enable SRIO link */
  2248. iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
  2249. TSI721_DEVCTL_SRBOOT_CMPL,
  2250. priv->regs + TSI721_DEVCTL);
  2251. if (mport->host_deviceid >= 0)
  2252. iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
  2253. RIO_PORT_GEN_DISCOVERED,
  2254. priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
  2255. else
  2256. iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
  2257. err = rio_register_mport(mport);
  2258. if (err) {
  2259. tsi721_unregister_dma(priv);
  2260. goto err_exit;
  2261. }
  2262. return 0;
  2263. err_exit:
  2264. tsi721_free_irq(priv);
  2265. return err;
  2266. }
  2267. static int tsi721_probe(struct pci_dev *pdev,
  2268. const struct pci_device_id *id)
  2269. {
  2270. struct tsi721_device *priv;
  2271. int err;
  2272. priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
  2273. if (!priv) {
  2274. err = -ENOMEM;
  2275. goto err_exit;
  2276. }
  2277. err = pci_enable_device(pdev);
  2278. if (err) {
  2279. tsi_err(&pdev->dev, "Failed to enable PCI device");
  2280. goto err_clean;
  2281. }
  2282. priv->pdev = pdev;
  2283. #ifdef DEBUG
  2284. {
  2285. int i;
  2286. for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
  2287. tsi_debug(INIT, &pdev->dev, "res%d %pR",
  2288. i, &pdev->resource[i]);
  2289. }
  2290. }
  2291. #endif
  2292. /*
  2293. * Verify BAR configuration
  2294. */
  2295. /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
  2296. if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
  2297. pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
  2298. pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
  2299. tsi_err(&pdev->dev, "Missing or misconfigured CSR BAR0");
  2300. err = -ENODEV;
  2301. goto err_disable_pdev;
  2302. }
  2303. /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
  2304. if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
  2305. pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
  2306. pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
  2307. tsi_err(&pdev->dev, "Missing or misconfigured Doorbell BAR1");
  2308. err = -ENODEV;
  2309. goto err_disable_pdev;
  2310. }
  2311. /*
  2312. * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
  2313. * space.
  2314. * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
  2315. * It may be a good idea to keep them disabled using HW configuration
  2316. * to save PCI memory space.
  2317. */
  2318. priv->p2r_bar[0].size = priv->p2r_bar[1].size = 0;
  2319. if (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64) {
  2320. if (pci_resource_flags(pdev, BAR_2) & IORESOURCE_PREFETCH)
  2321. tsi_debug(INIT, &pdev->dev,
  2322. "Prefetchable OBW BAR2 will not be used");
  2323. else {
  2324. priv->p2r_bar[0].base = pci_resource_start(pdev, BAR_2);
  2325. priv->p2r_bar[0].size = pci_resource_len(pdev, BAR_2);
  2326. }
  2327. }
  2328. if (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64) {
  2329. if (pci_resource_flags(pdev, BAR_4) & IORESOURCE_PREFETCH)
  2330. tsi_debug(INIT, &pdev->dev,
  2331. "Prefetchable OBW BAR4 will not be used");
  2332. else {
  2333. priv->p2r_bar[1].base = pci_resource_start(pdev, BAR_4);
  2334. priv->p2r_bar[1].size = pci_resource_len(pdev, BAR_4);
  2335. }
  2336. }
  2337. err = pci_request_regions(pdev, DRV_NAME);
  2338. if (err) {
  2339. tsi_err(&pdev->dev, "Unable to obtain PCI resources");
  2340. goto err_disable_pdev;
  2341. }
  2342. pci_set_master(pdev);
  2343. priv->regs = pci_ioremap_bar(pdev, BAR_0);
  2344. if (!priv->regs) {
  2345. tsi_err(&pdev->dev, "Unable to map device registers space");
  2346. err = -ENOMEM;
  2347. goto err_free_res;
  2348. }
  2349. priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
  2350. if (!priv->odb_base) {
  2351. tsi_err(&pdev->dev, "Unable to map outbound doorbells space");
  2352. err = -ENOMEM;
  2353. goto err_unmap_bars;
  2354. }
  2355. /* Configure DMA attributes. */
  2356. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  2357. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2358. if (err) {
  2359. tsi_err(&pdev->dev, "Unable to set DMA mask");
  2360. goto err_unmap_bars;
  2361. }
  2362. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  2363. tsi_info(&pdev->dev, "Unable to set consistent DMA mask");
  2364. } else {
  2365. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2366. if (err)
  2367. tsi_info(&pdev->dev, "Unable to set consistent DMA mask");
  2368. }
  2369. BUG_ON(!pci_is_pcie(pdev));
  2370. /* Clear "no snoop" and "relaxed ordering" bits. */
  2371. pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
  2372. PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN, 0);
  2373. /* Adjust PCIe completion timeout. */
  2374. pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL2, 0xf, 0x2);
  2375. /*
  2376. * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
  2377. */
  2378. pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
  2379. pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
  2380. TSI721_MSIXTBL_OFFSET);
  2381. pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
  2382. TSI721_MSIXPBA_OFFSET);
  2383. pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
  2384. /* End of FIXUP */
  2385. tsi721_disable_ints(priv);
  2386. tsi721_init_pc2sr_mapping(priv);
  2387. tsi721_init_sr2pc_mapping(priv);
  2388. if (tsi721_bdma_maint_init(priv)) {
  2389. tsi_err(&pdev->dev, "BDMA initialization failed");
  2390. err = -ENOMEM;
  2391. goto err_unmap_bars;
  2392. }
  2393. err = tsi721_doorbell_init(priv);
  2394. if (err)
  2395. goto err_free_bdma;
  2396. tsi721_port_write_init(priv);
  2397. err = tsi721_messages_init(priv);
  2398. if (err)
  2399. goto err_free_consistent;
  2400. err = tsi721_setup_mport(priv);
  2401. if (err)
  2402. goto err_free_consistent;
  2403. pci_set_drvdata(pdev, priv);
  2404. tsi721_interrupts_init(priv);
  2405. return 0;
  2406. err_free_consistent:
  2407. tsi721_port_write_free(priv);
  2408. tsi721_doorbell_free(priv);
  2409. err_free_bdma:
  2410. tsi721_bdma_maint_free(priv);
  2411. err_unmap_bars:
  2412. if (priv->regs)
  2413. iounmap(priv->regs);
  2414. if (priv->odb_base)
  2415. iounmap(priv->odb_base);
  2416. err_free_res:
  2417. pci_release_regions(pdev);
  2418. pci_clear_master(pdev);
  2419. err_disable_pdev:
  2420. pci_disable_device(pdev);
  2421. err_clean:
  2422. kfree(priv);
  2423. err_exit:
  2424. return err;
  2425. }
  2426. static void tsi721_remove(struct pci_dev *pdev)
  2427. {
  2428. struct tsi721_device *priv = pci_get_drvdata(pdev);
  2429. tsi_debug(EXIT, &pdev->dev, "enter");
  2430. tsi721_disable_ints(priv);
  2431. tsi721_free_irq(priv);
  2432. flush_scheduled_work();
  2433. rio_unregister_mport(&priv->mport);
  2434. tsi721_unregister_dma(priv);
  2435. tsi721_bdma_maint_free(priv);
  2436. tsi721_doorbell_free(priv);
  2437. tsi721_port_write_free(priv);
  2438. tsi721_close_sr2pc_mapping(priv);
  2439. if (priv->regs)
  2440. iounmap(priv->regs);
  2441. if (priv->odb_base)
  2442. iounmap(priv->odb_base);
  2443. #ifdef CONFIG_PCI_MSI
  2444. if (priv->flags & TSI721_USING_MSIX)
  2445. pci_disable_msix(priv->pdev);
  2446. else if (priv->flags & TSI721_USING_MSI)
  2447. pci_disable_msi(priv->pdev);
  2448. #endif
  2449. pci_release_regions(pdev);
  2450. pci_clear_master(pdev);
  2451. pci_disable_device(pdev);
  2452. pci_set_drvdata(pdev, NULL);
  2453. kfree(priv);
  2454. tsi_debug(EXIT, &pdev->dev, "exit");
  2455. }
  2456. static void tsi721_shutdown(struct pci_dev *pdev)
  2457. {
  2458. struct tsi721_device *priv = pci_get_drvdata(pdev);
  2459. tsi_debug(EXIT, &pdev->dev, "enter");
  2460. tsi721_disable_ints(priv);
  2461. tsi721_dma_stop_all(priv);
  2462. pci_clear_master(pdev);
  2463. pci_disable_device(pdev);
  2464. }
  2465. static const struct pci_device_id tsi721_pci_tbl[] = {
  2466. { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
  2467. { 0, } /* terminate list */
  2468. };
  2469. MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
  2470. static struct pci_driver tsi721_driver = {
  2471. .name = "tsi721",
  2472. .id_table = tsi721_pci_tbl,
  2473. .probe = tsi721_probe,
  2474. .remove = tsi721_remove,
  2475. .shutdown = tsi721_shutdown,
  2476. };
  2477. module_pci_driver(tsi721_driver);
  2478. MODULE_DESCRIPTION("IDT Tsi721 PCIExpress-to-SRIO bridge driver");
  2479. MODULE_AUTHOR("Integrated Device Technology, Inc.");
  2480. MODULE_LICENSE("GPL");