tsi721.c 82 KB

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