tsi721.c 82 KB

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