tsi721.c 82 KB

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