rcu: Maintain special bits at bottom of ->dynticks counter
[linux/fpc-iii.git] / drivers / rapidio / devices / tsi721.c
blob9d19b9a62011b376be541b247336d455952bb42b
1 /*
2 * RapidIO mport driver for Tsi721 PCIExpress-to-SRIO bridge
4 * Copyright 2011 Integrated Device Technology, Inc.
5 * Alexandre Bounine <alexandre.bounine@idt.com>
6 * Chul Kim <chul.kim@idt.com>
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.
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.
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.
23 #include <linux/io.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/ioport.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/rio.h>
31 #include <linux/rio_drv.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/interrupt.h>
34 #include <linux/kfifo.h>
35 #include <linux/delay.h>
37 #include "tsi721.h"
39 #ifdef DEBUG
40 u32 dbg_level;
41 module_param(dbg_level, uint, S_IWUSR | S_IRUGO);
42 MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
43 #endif
45 static int pcie_mrrs = -1;
46 module_param(pcie_mrrs, int, S_IRUGO);
47 MODULE_PARM_DESC(pcie_mrrs, "PCIe MRRS override value (0...5)");
49 static u8 mbox_sel = 0x0f;
50 module_param(mbox_sel, byte, S_IRUGO);
51 MODULE_PARM_DESC(mbox_sel,
52 "RIO Messaging MBOX Selection Mask (default: 0x0f = all)");
54 static void tsi721_omsg_handler(struct tsi721_device *priv, int ch);
55 static void tsi721_imsg_handler(struct tsi721_device *priv, int ch);
57 /**
58 * tsi721_lcread - read from local SREP config space
59 * @mport: RapidIO master port info
60 * @index: ID of RapdiIO interface
61 * @offset: Offset into configuration space
62 * @len: Length (in bytes) of the maintenance transaction
63 * @data: Value to be read into
65 * Generates a local SREP space read. Returns %0 on
66 * success or %-EINVAL on failure.
68 static int tsi721_lcread(struct rio_mport *mport, int index, u32 offset,
69 int len, u32 *data)
71 struct tsi721_device *priv = mport->priv;
73 if (len != sizeof(u32))
74 return -EINVAL; /* only 32-bit access is supported */
76 *data = ioread32(priv->regs + offset);
78 return 0;
81 /**
82 * tsi721_lcwrite - write into local SREP config space
83 * @mport: RapidIO master port info
84 * @index: ID of RapdiIO interface
85 * @offset: Offset into configuration space
86 * @len: Length (in bytes) of the maintenance transaction
87 * @data: Value to be written
89 * Generates a local write into SREP configuration space. Returns %0 on
90 * success or %-EINVAL on failure.
92 static int tsi721_lcwrite(struct rio_mport *mport, int index, u32 offset,
93 int len, u32 data)
95 struct tsi721_device *priv = mport->priv;
97 if (len != sizeof(u32))
98 return -EINVAL; /* only 32-bit access is supported */
100 iowrite32(data, priv->regs + offset);
102 return 0;
106 * tsi721_maint_dma - Helper function to generate RapidIO maintenance
107 * transactions using designated Tsi721 DMA channel.
108 * @priv: pointer to tsi721 private data
109 * @sys_size: RapdiIO transport system size
110 * @destid: Destination ID of transaction
111 * @hopcount: Number of hops to target device
112 * @offset: Offset into configuration space
113 * @len: Length (in bytes) of the maintenance transaction
114 * @data: Location to be read from or write into
115 * @do_wr: Operation flag (1 == MAINT_WR)
117 * Generates a RapidIO maintenance transaction (Read or Write).
118 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
120 static int tsi721_maint_dma(struct tsi721_device *priv, u32 sys_size,
121 u16 destid, u8 hopcount, u32 offset, int len,
122 u32 *data, int do_wr)
124 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(priv->mdma.ch_id);
125 struct tsi721_dma_desc *bd_ptr;
126 u32 rd_count, swr_ptr, ch_stat;
127 int i, err = 0;
128 u32 op = do_wr ? MAINT_WR : MAINT_RD;
130 if (offset > (RIO_MAINT_SPACE_SZ - len) || (len != sizeof(u32)))
131 return -EINVAL;
133 bd_ptr = priv->mdma.bd_base;
135 rd_count = ioread32(regs + TSI721_DMAC_DRDCNT);
137 /* Initialize DMA descriptor */
138 bd_ptr[0].type_id = cpu_to_le32((DTYPE2 << 29) | (op << 19) | destid);
139 bd_ptr[0].bcount = cpu_to_le32((sys_size << 26) | 0x04);
140 bd_ptr[0].raddr_lo = cpu_to_le32((hopcount << 24) | offset);
141 bd_ptr[0].raddr_hi = 0;
142 if (do_wr)
143 bd_ptr[0].data[0] = cpu_to_be32p(data);
144 else
145 bd_ptr[0].data[0] = 0xffffffff;
147 mb();
149 /* Start DMA operation */
150 iowrite32(rd_count + 2, regs + TSI721_DMAC_DWRCNT);
151 ioread32(regs + TSI721_DMAC_DWRCNT);
152 i = 0;
154 /* Wait until DMA transfer is finished */
155 while ((ch_stat = ioread32(regs + TSI721_DMAC_STS))
156 & TSI721_DMAC_STS_RUN) {
157 udelay(1);
158 if (++i >= 5000000) {
159 tsi_debug(MAINT, &priv->pdev->dev,
160 "DMA[%d] read timeout ch_status=%x",
161 priv->mdma.ch_id, ch_stat);
162 if (!do_wr)
163 *data = 0xffffffff;
164 err = -EIO;
165 goto err_out;
169 if (ch_stat & TSI721_DMAC_STS_ABORT) {
170 /* If DMA operation aborted due to error,
171 * reinitialize DMA channel
173 tsi_debug(MAINT, &priv->pdev->dev, "DMA ABORT ch_stat=%x",
174 ch_stat);
175 tsi_debug(MAINT, &priv->pdev->dev,
176 "OP=%d : destid=%x hc=%x off=%x",
177 do_wr ? MAINT_WR : MAINT_RD,
178 destid, hopcount, offset);
179 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
180 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
181 udelay(10);
182 iowrite32(0, regs + TSI721_DMAC_DWRCNT);
183 udelay(1);
184 if (!do_wr)
185 *data = 0xffffffff;
186 err = -EIO;
187 goto err_out;
190 if (!do_wr)
191 *data = be32_to_cpu(bd_ptr[0].data[0]);
194 * Update descriptor status FIFO RD pointer.
195 * NOTE: Skipping check and clear FIFO entries because we are waiting
196 * for transfer to be completed.
198 swr_ptr = ioread32(regs + TSI721_DMAC_DSWP);
199 iowrite32(swr_ptr, regs + TSI721_DMAC_DSRP);
200 err_out:
202 return err;
206 * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
207 * using Tsi721 BDMA engine.
208 * @mport: RapidIO master port control structure
209 * @index: ID of RapdiIO interface
210 * @destid: Destination ID of transaction
211 * @hopcount: Number of hops to target device
212 * @offset: Offset into configuration space
213 * @len: Length (in bytes) of the maintenance transaction
214 * @val: Location to be read into
216 * Generates a RapidIO maintenance read transaction.
217 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
219 static int tsi721_cread_dma(struct rio_mport *mport, int index, u16 destid,
220 u8 hopcount, u32 offset, int len, u32 *data)
222 struct tsi721_device *priv = mport->priv;
224 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
225 offset, len, data, 0);
229 * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
230 * using Tsi721 BDMA engine
231 * @mport: RapidIO master port control structure
232 * @index: ID of RapdiIO interface
233 * @destid: Destination ID of transaction
234 * @hopcount: Number of hops to target device
235 * @offset: Offset into configuration space
236 * @len: Length (in bytes) of the maintenance transaction
237 * @val: Value to be written
239 * Generates a RapidIO maintenance write transaction.
240 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
242 static int tsi721_cwrite_dma(struct rio_mport *mport, int index, u16 destid,
243 u8 hopcount, u32 offset, int len, u32 data)
245 struct tsi721_device *priv = mport->priv;
246 u32 temp = data;
248 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
249 offset, len, &temp, 1);
253 * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
254 * @priv: tsi721 device private structure
256 * Handles inbound port-write interrupts. Copies PW message from an internal
257 * buffer into PW message FIFO and schedules deferred routine to process
258 * queued messages.
260 static int
261 tsi721_pw_handler(struct tsi721_device *priv)
263 u32 pw_stat;
264 u32 pw_buf[TSI721_RIO_PW_MSG_SIZE/sizeof(u32)];
267 pw_stat = ioread32(priv->regs + TSI721_RIO_PW_RX_STAT);
269 if (pw_stat & TSI721_RIO_PW_RX_STAT_PW_VAL) {
270 pw_buf[0] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(0));
271 pw_buf[1] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(1));
272 pw_buf[2] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(2));
273 pw_buf[3] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(3));
275 /* Queue PW message (if there is room in FIFO),
276 * otherwise discard it.
278 spin_lock(&priv->pw_fifo_lock);
279 if (kfifo_avail(&priv->pw_fifo) >= TSI721_RIO_PW_MSG_SIZE)
280 kfifo_in(&priv->pw_fifo, pw_buf,
281 TSI721_RIO_PW_MSG_SIZE);
282 else
283 priv->pw_discard_count++;
284 spin_unlock(&priv->pw_fifo_lock);
287 /* Clear pending PW interrupts */
288 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
289 priv->regs + TSI721_RIO_PW_RX_STAT);
291 schedule_work(&priv->pw_work);
293 return 0;
296 static void tsi721_pw_dpc(struct work_struct *work)
298 struct tsi721_device *priv = container_of(work, struct tsi721_device,
299 pw_work);
300 union rio_pw_msg pwmsg;
303 * Process port-write messages
305 while (kfifo_out_spinlocked(&priv->pw_fifo, (unsigned char *)&pwmsg,
306 TSI721_RIO_PW_MSG_SIZE, &priv->pw_fifo_lock)) {
307 /* Pass the port-write message to RIO core for processing */
308 rio_inb_pwrite_handler(&priv->mport, &pwmsg);
313 * tsi721_pw_enable - enable/disable port-write interface init
314 * @mport: Master port implementing the port write unit
315 * @enable: 1=enable; 0=disable port-write message handling
317 static int tsi721_pw_enable(struct rio_mport *mport, int enable)
319 struct tsi721_device *priv = mport->priv;
320 u32 rval;
322 rval = ioread32(priv->regs + TSI721_RIO_EM_INT_ENABLE);
324 if (enable)
325 rval |= TSI721_RIO_EM_INT_ENABLE_PW_RX;
326 else
327 rval &= ~TSI721_RIO_EM_INT_ENABLE_PW_RX;
329 /* Clear pending PW interrupts */
330 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
331 priv->regs + TSI721_RIO_PW_RX_STAT);
332 /* Update enable bits */
333 iowrite32(rval, priv->regs + TSI721_RIO_EM_INT_ENABLE);
335 return 0;
339 * tsi721_dsend - Send a RapidIO doorbell
340 * @mport: RapidIO master port info
341 * @index: ID of RapidIO interface
342 * @destid: Destination ID of target device
343 * @data: 16-bit info field of RapidIO doorbell
345 * Sends a RapidIO doorbell message. Always returns %0.
347 static int tsi721_dsend(struct rio_mport *mport, int index,
348 u16 destid, u16 data)
350 struct tsi721_device *priv = mport->priv;
351 u32 offset;
353 offset = (((mport->sys_size) ? RIO_TT_CODE_16 : RIO_TT_CODE_8) << 18) |
354 (destid << 2);
356 tsi_debug(DBELL, &priv->pdev->dev,
357 "Send Doorbell 0x%04x to destID 0x%x", data, destid);
358 iowrite16be(data, priv->odb_base + offset);
360 return 0;
364 * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
365 * @priv: tsi721 device-specific data structure
367 * Handles inbound doorbell interrupts. Copies doorbell entry from an internal
368 * buffer into DB message FIFO and schedules deferred routine to process
369 * queued DBs.
371 static int
372 tsi721_dbell_handler(struct tsi721_device *priv)
374 u32 regval;
376 /* Disable IDB interrupts */
377 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
378 regval &= ~TSI721_SR_CHINT_IDBQRCV;
379 iowrite32(regval,
380 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
382 schedule_work(&priv->idb_work);
384 return 0;
387 static void tsi721_db_dpc(struct work_struct *work)
389 struct tsi721_device *priv = container_of(work, struct tsi721_device,
390 idb_work);
391 struct rio_mport *mport;
392 struct rio_dbell *dbell;
393 int found = 0;
394 u32 wr_ptr, rd_ptr;
395 u64 *idb_entry;
396 u32 regval;
397 union {
398 u64 msg;
399 u8 bytes[8];
400 } idb;
403 * Process queued inbound doorbells
405 mport = &priv->mport;
407 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
408 rd_ptr = ioread32(priv->regs + TSI721_IDQ_RP(IDB_QUEUE)) % IDB_QSIZE;
410 while (wr_ptr != rd_ptr) {
411 idb_entry = (u64 *)(priv->idb_base +
412 (TSI721_IDB_ENTRY_SIZE * rd_ptr));
413 rd_ptr++;
414 rd_ptr %= IDB_QSIZE;
415 idb.msg = *idb_entry;
416 *idb_entry = 0;
418 /* Process one doorbell */
419 list_for_each_entry(dbell, &mport->dbells, node) {
420 if ((dbell->res->start <= DBELL_INF(idb.bytes)) &&
421 (dbell->res->end >= DBELL_INF(idb.bytes))) {
422 found = 1;
423 break;
427 if (found) {
428 dbell->dinb(mport, dbell->dev_id, DBELL_SID(idb.bytes),
429 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
430 } else {
431 tsi_debug(DBELL, &priv->pdev->dev,
432 "spurious IDB sid %2.2x tid %2.2x info %4.4x",
433 DBELL_SID(idb.bytes), DBELL_TID(idb.bytes),
434 DBELL_INF(idb.bytes));
437 wr_ptr = ioread32(priv->regs +
438 TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
441 iowrite32(rd_ptr & (IDB_QSIZE - 1),
442 priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
444 /* Re-enable IDB interrupts */
445 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
446 regval |= TSI721_SR_CHINT_IDBQRCV;
447 iowrite32(regval,
448 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
450 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
451 if (wr_ptr != rd_ptr)
452 schedule_work(&priv->idb_work);
456 * tsi721_irqhandler - Tsi721 interrupt handler
457 * @irq: Linux interrupt number
458 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
460 * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
461 * interrupt events and calls an event-specific handler(s).
463 static irqreturn_t tsi721_irqhandler(int irq, void *ptr)
465 struct tsi721_device *priv = (struct tsi721_device *)ptr;
466 u32 dev_int;
467 u32 dev_ch_int;
468 u32 intval;
469 u32 ch_inte;
471 /* For MSI mode disable all device-level interrupts */
472 if (priv->flags & TSI721_USING_MSI)
473 iowrite32(0, priv->regs + TSI721_DEV_INTE);
475 dev_int = ioread32(priv->regs + TSI721_DEV_INT);
476 if (!dev_int)
477 return IRQ_NONE;
479 dev_ch_int = ioread32(priv->regs + TSI721_DEV_CHAN_INT);
481 if (dev_int & TSI721_DEV_INT_SR2PC_CH) {
482 /* Service SR2PC Channel interrupts */
483 if (dev_ch_int & TSI721_INT_SR2PC_CHAN(IDB_QUEUE)) {
484 /* Service Inbound Doorbell interrupt */
485 intval = ioread32(priv->regs +
486 TSI721_SR_CHINT(IDB_QUEUE));
487 if (intval & TSI721_SR_CHINT_IDBQRCV)
488 tsi721_dbell_handler(priv);
489 else
490 tsi_info(&priv->pdev->dev,
491 "Unsupported SR_CH_INT %x", intval);
493 /* Clear interrupts */
494 iowrite32(intval,
495 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
496 ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
500 if (dev_int & TSI721_DEV_INT_SMSG_CH) {
501 int ch;
504 * Service channel interrupts from Messaging Engine
507 if (dev_ch_int & TSI721_INT_IMSG_CHAN_M) { /* Inbound Msg */
508 /* Disable signaled OB MSG Channel interrupts */
509 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
510 ch_inte &= ~(dev_ch_int & TSI721_INT_IMSG_CHAN_M);
511 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
514 * Process Inbound Message interrupt for each MBOX
516 for (ch = 4; ch < RIO_MAX_MBOX + 4; ch++) {
517 if (!(dev_ch_int & TSI721_INT_IMSG_CHAN(ch)))
518 continue;
519 tsi721_imsg_handler(priv, ch);
523 if (dev_ch_int & TSI721_INT_OMSG_CHAN_M) { /* Outbound Msg */
524 /* Disable signaled OB MSG Channel interrupts */
525 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
526 ch_inte &= ~(dev_ch_int & TSI721_INT_OMSG_CHAN_M);
527 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
530 * Process Outbound Message interrupts for each MBOX
533 for (ch = 0; ch < RIO_MAX_MBOX; ch++) {
534 if (!(dev_ch_int & TSI721_INT_OMSG_CHAN(ch)))
535 continue;
536 tsi721_omsg_handler(priv, ch);
541 if (dev_int & TSI721_DEV_INT_SRIO) {
542 /* Service SRIO MAC interrupts */
543 intval = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
544 if (intval & TSI721_RIO_EM_INT_STAT_PW_RX)
545 tsi721_pw_handler(priv);
548 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
549 if (dev_int & TSI721_DEV_INT_BDMA_CH) {
550 int ch;
552 if (dev_ch_int & TSI721_INT_BDMA_CHAN_M) {
553 tsi_debug(DMA, &priv->pdev->dev,
554 "IRQ from DMA channel 0x%08x", dev_ch_int);
556 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++) {
557 if (!(dev_ch_int & TSI721_INT_BDMA_CHAN(ch)))
558 continue;
559 tsi721_bdma_handler(&priv->bdma[ch]);
563 #endif
565 /* For MSI mode re-enable device-level interrupts */
566 if (priv->flags & TSI721_USING_MSI) {
567 dev_int = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
568 TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
569 iowrite32(dev_int, priv->regs + TSI721_DEV_INTE);
572 return IRQ_HANDLED;
575 static void tsi721_interrupts_init(struct tsi721_device *priv)
577 u32 intr;
579 /* Enable IDB interrupts */
580 iowrite32(TSI721_SR_CHINT_ALL,
581 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
582 iowrite32(TSI721_SR_CHINT_IDBQRCV,
583 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
585 /* Enable SRIO MAC interrupts */
586 iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT,
587 priv->regs + TSI721_RIO_EM_DEV_INT_EN);
589 /* Enable interrupts from channels in use */
590 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
591 intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE) |
592 (TSI721_INT_BDMA_CHAN_M &
593 ~TSI721_INT_BDMA_CHAN(TSI721_DMACH_MAINT));
594 #else
595 intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE);
596 #endif
597 iowrite32(intr, priv->regs + TSI721_DEV_CHAN_INTE);
599 if (priv->flags & TSI721_USING_MSIX)
600 intr = TSI721_DEV_INT_SRIO;
601 else
602 intr = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
603 TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
605 iowrite32(intr, priv->regs + TSI721_DEV_INTE);
606 ioread32(priv->regs + TSI721_DEV_INTE);
609 #ifdef CONFIG_PCI_MSI
611 * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
612 * @irq: Linux interrupt number
613 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
615 * Handles outbound messaging interrupts signaled using MSI-X.
617 static irqreturn_t tsi721_omsg_msix(int irq, void *ptr)
619 struct tsi721_device *priv = (struct tsi721_device *)ptr;
620 int mbox;
622 mbox = (irq - priv->msix[TSI721_VECT_OMB0_DONE].vector) % RIO_MAX_MBOX;
623 tsi721_omsg_handler(priv, mbox);
624 return IRQ_HANDLED;
628 * tsi721_imsg_msix - MSI-X interrupt handler for inbound messaging
629 * @irq: Linux interrupt number
630 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
632 * Handles inbound messaging interrupts signaled using MSI-X.
634 static irqreturn_t tsi721_imsg_msix(int irq, void *ptr)
636 struct tsi721_device *priv = (struct tsi721_device *)ptr;
637 int mbox;
639 mbox = (irq - priv->msix[TSI721_VECT_IMB0_RCV].vector) % RIO_MAX_MBOX;
640 tsi721_imsg_handler(priv, mbox + 4);
641 return IRQ_HANDLED;
645 * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
646 * @irq: Linux interrupt number
647 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
649 * Handles Tsi721 interrupts from SRIO MAC.
651 static irqreturn_t tsi721_srio_msix(int irq, void *ptr)
653 struct tsi721_device *priv = (struct tsi721_device *)ptr;
654 u32 srio_int;
656 /* Service SRIO MAC interrupts */
657 srio_int = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
658 if (srio_int & TSI721_RIO_EM_INT_STAT_PW_RX)
659 tsi721_pw_handler(priv);
661 return IRQ_HANDLED;
665 * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
666 * @irq: Linux interrupt number
667 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
669 * Handles Tsi721 interrupts from SR2PC Channel.
670 * NOTE: At this moment services only one SR2PC channel associated with inbound
671 * doorbells.
673 static irqreturn_t tsi721_sr2pc_ch_msix(int irq, void *ptr)
675 struct tsi721_device *priv = (struct tsi721_device *)ptr;
676 u32 sr_ch_int;
678 /* Service Inbound DB interrupt from SR2PC channel */
679 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
680 if (sr_ch_int & TSI721_SR_CHINT_IDBQRCV)
681 tsi721_dbell_handler(priv);
683 /* Clear interrupts */
684 iowrite32(sr_ch_int, priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
685 /* Read back to ensure that interrupt was cleared */
686 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
688 return IRQ_HANDLED;
692 * tsi721_request_msix - register interrupt service for MSI-X mode.
693 * @priv: tsi721 device-specific data structure
695 * Registers MSI-X interrupt service routines for interrupts that are active
696 * immediately after mport initialization. Messaging interrupt service routines
697 * should be registered during corresponding open requests.
699 static int tsi721_request_msix(struct tsi721_device *priv)
701 int err = 0;
703 err = request_irq(priv->msix[TSI721_VECT_IDB].vector,
704 tsi721_sr2pc_ch_msix, 0,
705 priv->msix[TSI721_VECT_IDB].irq_name, (void *)priv);
706 if (err)
707 return err;
709 err = request_irq(priv->msix[TSI721_VECT_PWRX].vector,
710 tsi721_srio_msix, 0,
711 priv->msix[TSI721_VECT_PWRX].irq_name, (void *)priv);
712 if (err) {
713 free_irq(priv->msix[TSI721_VECT_IDB].vector, (void *)priv);
714 return err;
717 return 0;
721 * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
722 * @priv: pointer to tsi721 private data
724 * Configures MSI-X support for Tsi721. Supports only an exact number
725 * of requested vectors.
727 static int tsi721_enable_msix(struct tsi721_device *priv)
729 struct msix_entry entries[TSI721_VECT_MAX];
730 int err;
731 int i;
733 entries[TSI721_VECT_IDB].entry = TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE);
734 entries[TSI721_VECT_PWRX].entry = TSI721_MSIX_SRIO_MAC_INT;
737 * Initialize MSI-X entries for Messaging Engine:
738 * this driver supports four RIO mailboxes (inbound and outbound)
739 * NOTE: Inbound message MBOX 0...4 use IB channels 4...7. Therefore
740 * offset +4 is added to IB MBOX number.
742 for (i = 0; i < RIO_MAX_MBOX; i++) {
743 entries[TSI721_VECT_IMB0_RCV + i].entry =
744 TSI721_MSIX_IMSG_DQ_RCV(i + 4);
745 entries[TSI721_VECT_IMB0_INT + i].entry =
746 TSI721_MSIX_IMSG_INT(i + 4);
747 entries[TSI721_VECT_OMB0_DONE + i].entry =
748 TSI721_MSIX_OMSG_DONE(i);
749 entries[TSI721_VECT_OMB0_INT + i].entry =
750 TSI721_MSIX_OMSG_INT(i);
753 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
755 * Initialize MSI-X entries for Block DMA Engine:
756 * this driver supports XXX DMA channels
757 * (one is reserved for SRIO maintenance transactions)
759 for (i = 0; i < TSI721_DMA_CHNUM; i++) {
760 entries[TSI721_VECT_DMA0_DONE + i].entry =
761 TSI721_MSIX_DMACH_DONE(i);
762 entries[TSI721_VECT_DMA0_INT + i].entry =
763 TSI721_MSIX_DMACH_INT(i);
765 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
767 err = pci_enable_msix_exact(priv->pdev, entries, ARRAY_SIZE(entries));
768 if (err) {
769 tsi_err(&priv->pdev->dev,
770 "Failed to enable MSI-X (err=%d)", err);
771 return err;
775 * Copy MSI-X vector information into tsi721 private structure
777 priv->msix[TSI721_VECT_IDB].vector = entries[TSI721_VECT_IDB].vector;
778 snprintf(priv->msix[TSI721_VECT_IDB].irq_name, IRQ_DEVICE_NAME_MAX,
779 DRV_NAME "-idb@pci:%s", pci_name(priv->pdev));
780 priv->msix[TSI721_VECT_PWRX].vector = entries[TSI721_VECT_PWRX].vector;
781 snprintf(priv->msix[TSI721_VECT_PWRX].irq_name, IRQ_DEVICE_NAME_MAX,
782 DRV_NAME "-pwrx@pci:%s", pci_name(priv->pdev));
784 for (i = 0; i < RIO_MAX_MBOX; i++) {
785 priv->msix[TSI721_VECT_IMB0_RCV + i].vector =
786 entries[TSI721_VECT_IMB0_RCV + i].vector;
787 snprintf(priv->msix[TSI721_VECT_IMB0_RCV + i].irq_name,
788 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbr%d@pci:%s",
789 i, pci_name(priv->pdev));
791 priv->msix[TSI721_VECT_IMB0_INT + i].vector =
792 entries[TSI721_VECT_IMB0_INT + i].vector;
793 snprintf(priv->msix[TSI721_VECT_IMB0_INT + i].irq_name,
794 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbi%d@pci:%s",
795 i, pci_name(priv->pdev));
797 priv->msix[TSI721_VECT_OMB0_DONE + i].vector =
798 entries[TSI721_VECT_OMB0_DONE + i].vector;
799 snprintf(priv->msix[TSI721_VECT_OMB0_DONE + i].irq_name,
800 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombd%d@pci:%s",
801 i, pci_name(priv->pdev));
803 priv->msix[TSI721_VECT_OMB0_INT + i].vector =
804 entries[TSI721_VECT_OMB0_INT + i].vector;
805 snprintf(priv->msix[TSI721_VECT_OMB0_INT + i].irq_name,
806 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombi%d@pci:%s",
807 i, pci_name(priv->pdev));
810 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
811 for (i = 0; i < TSI721_DMA_CHNUM; i++) {
812 priv->msix[TSI721_VECT_DMA0_DONE + i].vector =
813 entries[TSI721_VECT_DMA0_DONE + i].vector;
814 snprintf(priv->msix[TSI721_VECT_DMA0_DONE + i].irq_name,
815 IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmad%d@pci:%s",
816 i, pci_name(priv->pdev));
818 priv->msix[TSI721_VECT_DMA0_INT + i].vector =
819 entries[TSI721_VECT_DMA0_INT + i].vector;
820 snprintf(priv->msix[TSI721_VECT_DMA0_INT + i].irq_name,
821 IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmai%d@pci:%s",
822 i, pci_name(priv->pdev));
824 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
826 return 0;
828 #endif /* CONFIG_PCI_MSI */
830 static int tsi721_request_irq(struct tsi721_device *priv)
832 int err;
834 #ifdef CONFIG_PCI_MSI
835 if (priv->flags & TSI721_USING_MSIX)
836 err = tsi721_request_msix(priv);
837 else
838 #endif
839 err = request_irq(priv->pdev->irq, tsi721_irqhandler,
840 (priv->flags & TSI721_USING_MSI) ? 0 : IRQF_SHARED,
841 DRV_NAME, (void *)priv);
843 if (err)
844 tsi_err(&priv->pdev->dev,
845 "Unable to allocate interrupt, err=%d", err);
847 return err;
850 static void tsi721_free_irq(struct tsi721_device *priv)
852 #ifdef CONFIG_PCI_MSI
853 if (priv->flags & TSI721_USING_MSIX) {
854 free_irq(priv->msix[TSI721_VECT_IDB].vector, (void *)priv);
855 free_irq(priv->msix[TSI721_VECT_PWRX].vector, (void *)priv);
856 } else
857 #endif
858 free_irq(priv->pdev->irq, (void *)priv);
861 static int
862 tsi721_obw_alloc(struct tsi721_device *priv, struct tsi721_obw_bar *pbar,
863 u32 size, int *win_id)
865 u64 win_base;
866 u64 bar_base;
867 u64 bar_end;
868 u32 align;
869 struct tsi721_ob_win *win;
870 struct tsi721_ob_win *new_win = NULL;
871 int new_win_idx = -1;
872 int i = 0;
874 bar_base = pbar->base;
875 bar_end = bar_base + pbar->size;
876 win_base = bar_base;
877 align = size/TSI721_PC2SR_ZONES;
879 while (i < TSI721_IBWIN_NUM) {
880 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
881 if (!priv->ob_win[i].active) {
882 if (new_win == NULL) {
883 new_win = &priv->ob_win[i];
884 new_win_idx = i;
886 continue;
890 * If this window belongs to the current BAR check it
891 * for overlap
893 win = &priv->ob_win[i];
895 if (win->base >= bar_base && win->base < bar_end) {
896 if (win_base < (win->base + win->size) &&
897 (win_base + size) > win->base) {
898 /* Overlap detected */
899 win_base = win->base + win->size;
900 win_base = ALIGN(win_base, align);
901 break;
907 if (win_base + size > bar_end)
908 return -ENOMEM;
910 if (!new_win) {
911 tsi_err(&priv->pdev->dev, "OBW count tracking failed");
912 return -EIO;
915 new_win->active = true;
916 new_win->base = win_base;
917 new_win->size = size;
918 new_win->pbar = pbar;
919 priv->obwin_cnt--;
920 pbar->free -= size;
921 *win_id = new_win_idx;
922 return 0;
925 static int tsi721_map_outb_win(struct rio_mport *mport, u16 destid, u64 rstart,
926 u32 size, u32 flags, dma_addr_t *laddr)
928 struct tsi721_device *priv = mport->priv;
929 int i;
930 struct tsi721_obw_bar *pbar;
931 struct tsi721_ob_win *ob_win;
932 int obw = -1;
933 u32 rval;
934 u64 rio_addr;
935 u32 zsize;
936 int ret = -ENOMEM;
938 tsi_debug(OBW, &priv->pdev->dev,
939 "did=%d ra=0x%llx sz=0x%x", destid, rstart, size);
941 if (!is_power_of_2(size) || (size < 0x8000) || (rstart & (size - 1)))
942 return -EINVAL;
944 if (priv->obwin_cnt == 0)
945 return -EBUSY;
947 for (i = 0; i < 2; i++) {
948 if (priv->p2r_bar[i].free >= size) {
949 pbar = &priv->p2r_bar[i];
950 ret = tsi721_obw_alloc(priv, pbar, size, &obw);
951 if (!ret)
952 break;
956 if (ret)
957 return ret;
959 WARN_ON(obw == -1);
960 ob_win = &priv->ob_win[obw];
961 ob_win->destid = destid;
962 ob_win->rstart = rstart;
963 tsi_debug(OBW, &priv->pdev->dev,
964 "allocated OBW%d @%llx", obw, ob_win->base);
967 * Configure Outbound Window
970 zsize = size/TSI721_PC2SR_ZONES;
971 rio_addr = rstart;
974 * Program Address Translation Zones:
975 * This implementation uses all 8 zones associated wit window.
977 for (i = 0; i < TSI721_PC2SR_ZONES; i++) {
979 while (ioread32(priv->regs + TSI721_ZONE_SEL) &
980 TSI721_ZONE_SEL_GO) {
981 udelay(1);
984 rval = (u32)(rio_addr & TSI721_LUT_DATA0_ADD) |
985 TSI721_LUT_DATA0_NREAD | TSI721_LUT_DATA0_NWR;
986 iowrite32(rval, priv->regs + TSI721_LUT_DATA0);
987 rval = (u32)(rio_addr >> 32);
988 iowrite32(rval, priv->regs + TSI721_LUT_DATA1);
989 rval = destid;
990 iowrite32(rval, priv->regs + TSI721_LUT_DATA2);
992 rval = TSI721_ZONE_SEL_GO | (obw << 3) | i;
993 iowrite32(rval, priv->regs + TSI721_ZONE_SEL);
995 rio_addr += zsize;
998 iowrite32(TSI721_OBWIN_SIZE(size) << 8,
999 priv->regs + TSI721_OBWINSZ(obw));
1000 iowrite32((u32)(ob_win->base >> 32), priv->regs + TSI721_OBWINUB(obw));
1001 iowrite32((u32)(ob_win->base & TSI721_OBWINLB_BA) | TSI721_OBWINLB_WEN,
1002 priv->regs + TSI721_OBWINLB(obw));
1004 *laddr = ob_win->base;
1005 return 0;
1008 static void tsi721_unmap_outb_win(struct rio_mport *mport,
1009 u16 destid, u64 rstart)
1011 struct tsi721_device *priv = mport->priv;
1012 struct tsi721_ob_win *ob_win;
1013 int i;
1015 tsi_debug(OBW, &priv->pdev->dev, "did=%d ra=0x%llx", destid, rstart);
1017 for (i = 0; i < TSI721_OBWIN_NUM; i++) {
1018 ob_win = &priv->ob_win[i];
1020 if (ob_win->active &&
1021 ob_win->destid == destid && ob_win->rstart == rstart) {
1022 tsi_debug(OBW, &priv->pdev->dev,
1023 "free OBW%d @%llx", i, ob_win->base);
1024 ob_win->active = false;
1025 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
1026 ob_win->pbar->free += ob_win->size;
1027 priv->obwin_cnt++;
1028 break;
1034 * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
1035 * translation regions.
1036 * @priv: pointer to tsi721 private data
1038 * Disables SREP translation regions.
1040 static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
1042 int i, z;
1043 u32 rval;
1045 /* Disable all PC2SR translation windows */
1046 for (i = 0; i < TSI721_OBWIN_NUM; i++)
1047 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
1049 /* Initialize zone lookup tables to avoid ECC errors on reads */
1050 iowrite32(0, priv->regs + TSI721_LUT_DATA0);
1051 iowrite32(0, priv->regs + TSI721_LUT_DATA1);
1052 iowrite32(0, priv->regs + TSI721_LUT_DATA2);
1054 for (i = 0; i < TSI721_OBWIN_NUM; i++) {
1055 for (z = 0; z < TSI721_PC2SR_ZONES; z++) {
1056 while (ioread32(priv->regs + TSI721_ZONE_SEL) &
1057 TSI721_ZONE_SEL_GO) {
1058 udelay(1);
1060 rval = TSI721_ZONE_SEL_GO | (i << 3) | z;
1061 iowrite32(rval, priv->regs + TSI721_ZONE_SEL);
1065 if (priv->p2r_bar[0].size == 0 && priv->p2r_bar[1].size == 0) {
1066 priv->obwin_cnt = 0;
1067 return;
1070 priv->p2r_bar[0].free = priv->p2r_bar[0].size;
1071 priv->p2r_bar[1].free = priv->p2r_bar[1].size;
1073 for (i = 0; i < TSI721_OBWIN_NUM; i++)
1074 priv->ob_win[i].active = false;
1076 priv->obwin_cnt = TSI721_OBWIN_NUM;
1080 * tsi721_rio_map_inb_mem -- Mapping inbound memory region.
1081 * @mport: RapidIO master port
1082 * @lstart: Local memory space start address.
1083 * @rstart: RapidIO space start address.
1084 * @size: The mapping region size.
1085 * @flags: Flags for mapping. 0 for using default flags.
1087 * Return: 0 -- Success.
1089 * This function will create the inbound mapping
1090 * from rstart to lstart.
1092 static int tsi721_rio_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
1093 u64 rstart, u64 size, u32 flags)
1095 struct tsi721_device *priv = mport->priv;
1096 int i, avail = -1;
1097 u32 regval;
1098 struct tsi721_ib_win *ib_win;
1099 bool direct = (lstart == rstart);
1100 u64 ibw_size;
1101 dma_addr_t loc_start;
1102 u64 ibw_start;
1103 struct tsi721_ib_win_mapping *map = NULL;
1104 int ret = -EBUSY;
1106 /* Max IBW size supported by HW is 16GB */
1107 if (size > 0x400000000UL)
1108 return -EINVAL;
1110 if (direct) {
1111 /* Calculate minimal acceptable window size and base address */
1113 ibw_size = roundup_pow_of_two(size);
1114 ibw_start = lstart & ~(ibw_size - 1);
1116 tsi_debug(IBW, &priv->pdev->dev,
1117 "Direct (RIO_0x%llx -> PCIe_%pad), size=0x%llx, ibw_start = 0x%llx",
1118 rstart, &lstart, size, ibw_start);
1120 while ((lstart + size) > (ibw_start + ibw_size)) {
1121 ibw_size *= 2;
1122 ibw_start = lstart & ~(ibw_size - 1);
1123 /* Check for crossing IBW max size 16GB */
1124 if (ibw_size > 0x400000000UL)
1125 return -EBUSY;
1128 loc_start = ibw_start;
1130 map = kzalloc(sizeof(struct tsi721_ib_win_mapping), GFP_ATOMIC);
1131 if (map == NULL)
1132 return -ENOMEM;
1134 } else {
1135 tsi_debug(IBW, &priv->pdev->dev,
1136 "Translated (RIO_0x%llx -> PCIe_%pad), size=0x%llx",
1137 rstart, &lstart, size);
1139 if (!is_power_of_2(size) || size < 0x1000 ||
1140 ((u64)lstart & (size - 1)) || (rstart & (size - 1)))
1141 return -EINVAL;
1142 if (priv->ibwin_cnt == 0)
1143 return -EBUSY;
1144 ibw_start = rstart;
1145 ibw_size = size;
1146 loc_start = lstart;
1150 * Scan for overlapping with active regions and mark the first available
1151 * IB window at the same time.
1153 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
1154 ib_win = &priv->ib_win[i];
1156 if (!ib_win->active) {
1157 if (avail == -1) {
1158 avail = i;
1159 ret = 0;
1161 } else if (ibw_start < (ib_win->rstart + ib_win->size) &&
1162 (ibw_start + ibw_size) > ib_win->rstart) {
1163 /* Return error if address translation involved */
1164 if (!direct || ib_win->xlat) {
1165 ret = -EFAULT;
1166 break;
1170 * Direct mappings usually are larger than originally
1171 * requested fragments - check if this new request fits
1172 * into it.
1174 if (rstart >= ib_win->rstart &&
1175 (rstart + size) <= (ib_win->rstart +
1176 ib_win->size)) {
1177 /* We are in - no further mapping required */
1178 map->lstart = lstart;
1179 list_add_tail(&map->node, &ib_win->mappings);
1180 return 0;
1183 ret = -EFAULT;
1184 break;
1188 if (ret)
1189 goto out;
1190 i = avail;
1192 /* Sanity check: available IB window must be disabled at this point */
1193 regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
1194 if (WARN_ON(regval & TSI721_IBWIN_LB_WEN)) {
1195 ret = -EIO;
1196 goto out;
1199 ib_win = &priv->ib_win[i];
1200 ib_win->active = true;
1201 ib_win->rstart = ibw_start;
1202 ib_win->lstart = loc_start;
1203 ib_win->size = ibw_size;
1204 ib_win->xlat = (lstart != rstart);
1205 INIT_LIST_HEAD(&ib_win->mappings);
1208 * When using direct IBW mapping and have larger than requested IBW size
1209 * we can have multiple local memory blocks mapped through the same IBW
1210 * To handle this situation we maintain list of "clients" for such IBWs.
1212 if (direct) {
1213 map->lstart = lstart;
1214 list_add_tail(&map->node, &ib_win->mappings);
1217 iowrite32(TSI721_IBWIN_SIZE(ibw_size) << 8,
1218 priv->regs + TSI721_IBWIN_SZ(i));
1220 iowrite32(((u64)loc_start >> 32), priv->regs + TSI721_IBWIN_TUA(i));
1221 iowrite32(((u64)loc_start & TSI721_IBWIN_TLA_ADD),
1222 priv->regs + TSI721_IBWIN_TLA(i));
1224 iowrite32(ibw_start >> 32, priv->regs + TSI721_IBWIN_UB(i));
1225 iowrite32((ibw_start & TSI721_IBWIN_LB_BA) | TSI721_IBWIN_LB_WEN,
1226 priv->regs + TSI721_IBWIN_LB(i));
1228 priv->ibwin_cnt--;
1230 tsi_debug(IBW, &priv->pdev->dev,
1231 "Configured IBWIN%d (RIO_0x%llx -> PCIe_%pad), size=0x%llx",
1232 i, ibw_start, &loc_start, ibw_size);
1234 return 0;
1235 out:
1236 kfree(map);
1237 return ret;
1241 * tsi721_rio_unmap_inb_mem -- Unmapping inbound memory region.
1242 * @mport: RapidIO master port
1243 * @lstart: Local memory space start address.
1245 static void tsi721_rio_unmap_inb_mem(struct rio_mport *mport,
1246 dma_addr_t lstart)
1248 struct tsi721_device *priv = mport->priv;
1249 struct tsi721_ib_win *ib_win;
1250 int i;
1252 tsi_debug(IBW, &priv->pdev->dev,
1253 "Unmap IBW mapped to PCIe_%pad", &lstart);
1255 /* Search for matching active inbound translation window */
1256 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
1257 ib_win = &priv->ib_win[i];
1259 /* Address translating IBWs must to be an exact march */
1260 if (!ib_win->active ||
1261 (ib_win->xlat && lstart != ib_win->lstart))
1262 continue;
1264 if (lstart >= ib_win->lstart &&
1265 lstart < (ib_win->lstart + ib_win->size)) {
1267 if (!ib_win->xlat) {
1268 struct tsi721_ib_win_mapping *map;
1269 int found = 0;
1271 list_for_each_entry(map,
1272 &ib_win->mappings, node) {
1273 if (map->lstart == lstart) {
1274 list_del(&map->node);
1275 kfree(map);
1276 found = 1;
1277 break;
1281 if (!found)
1282 continue;
1284 if (!list_empty(&ib_win->mappings))
1285 break;
1288 tsi_debug(IBW, &priv->pdev->dev, "Disable IBWIN_%d", i);
1289 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1290 ib_win->active = false;
1291 priv->ibwin_cnt++;
1292 break;
1296 if (i == TSI721_IBWIN_NUM)
1297 tsi_debug(IBW, &priv->pdev->dev,
1298 "IB window mapped to %pad not found", &lstart);
1302 * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
1303 * translation regions.
1304 * @priv: pointer to tsi721 private data
1306 * Disables inbound windows.
1308 static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
1310 int i;
1312 /* Disable all SR2PC inbound windows */
1313 for (i = 0; i < TSI721_IBWIN_NUM; i++)
1314 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1315 priv->ibwin_cnt = TSI721_IBWIN_NUM;
1319 * tsi721_close_sr2pc_mapping - closes all active inbound (SRIO->PCIe)
1320 * translation regions.
1321 * @priv: pointer to tsi721 device private data
1323 static void tsi721_close_sr2pc_mapping(struct tsi721_device *priv)
1325 struct tsi721_ib_win *ib_win;
1326 int i;
1328 /* Disable all active SR2PC inbound windows */
1329 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
1330 ib_win = &priv->ib_win[i];
1331 if (ib_win->active) {
1332 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1333 ib_win->active = false;
1339 * tsi721_port_write_init - Inbound port write interface init
1340 * @priv: pointer to tsi721 private data
1342 * Initializes inbound port write handler.
1343 * Returns %0 on success or %-ENOMEM on failure.
1345 static int tsi721_port_write_init(struct tsi721_device *priv)
1347 priv->pw_discard_count = 0;
1348 INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
1349 spin_lock_init(&priv->pw_fifo_lock);
1350 if (kfifo_alloc(&priv->pw_fifo,
1351 TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
1352 tsi_err(&priv->pdev->dev, "PW FIFO allocation failed");
1353 return -ENOMEM;
1356 /* Use reliable port-write capture mode */
1357 iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
1358 return 0;
1361 static void tsi721_port_write_free(struct tsi721_device *priv)
1363 kfifo_free(&priv->pw_fifo);
1366 static int tsi721_doorbell_init(struct tsi721_device *priv)
1368 /* Outbound Doorbells do not require any setup.
1369 * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
1370 * That BAR1 was mapped during the probe routine.
1373 /* Initialize Inbound Doorbell processing DPC and queue */
1374 priv->db_discard_count = 0;
1375 INIT_WORK(&priv->idb_work, tsi721_db_dpc);
1377 /* Allocate buffer for inbound doorbells queue */
1378 priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
1379 IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1380 &priv->idb_dma, GFP_KERNEL);
1381 if (!priv->idb_base)
1382 return -ENOMEM;
1384 tsi_debug(DBELL, &priv->pdev->dev,
1385 "Allocated IDB buffer @ %p (phys = %pad)",
1386 priv->idb_base, &priv->idb_dma);
1388 iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
1389 priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
1390 iowrite32(((u64)priv->idb_dma >> 32),
1391 priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
1392 iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
1393 priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
1394 /* Enable accepting all inbound doorbells */
1395 iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
1397 iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
1399 iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
1401 return 0;
1404 static void tsi721_doorbell_free(struct tsi721_device *priv)
1406 if (priv->idb_base == NULL)
1407 return;
1409 /* Free buffer allocated for inbound doorbell queue */
1410 dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1411 priv->idb_base, priv->idb_dma);
1412 priv->idb_base = NULL;
1416 * tsi721_bdma_maint_init - Initialize maintenance request BDMA channel.
1417 * @priv: pointer to tsi721 private data
1419 * Initialize BDMA channel allocated for RapidIO maintenance read/write
1420 * request generation
1421 * Returns %0 on success or %-ENOMEM on failure.
1423 static int tsi721_bdma_maint_init(struct tsi721_device *priv)
1425 struct tsi721_dma_desc *bd_ptr;
1426 u64 *sts_ptr;
1427 dma_addr_t bd_phys, sts_phys;
1428 int sts_size;
1429 int bd_num = 2;
1430 void __iomem *regs;
1432 tsi_debug(MAINT, &priv->pdev->dev,
1433 "Init BDMA_%d Maintenance requests", TSI721_DMACH_MAINT);
1436 * Initialize DMA channel for maintenance requests
1439 priv->mdma.ch_id = TSI721_DMACH_MAINT;
1440 regs = priv->regs + TSI721_DMAC_BASE(TSI721_DMACH_MAINT);
1442 /* Allocate space for DMA descriptors */
1443 bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
1444 bd_num * sizeof(struct tsi721_dma_desc),
1445 &bd_phys, GFP_KERNEL);
1446 if (!bd_ptr)
1447 return -ENOMEM;
1449 priv->mdma.bd_num = bd_num;
1450 priv->mdma.bd_phys = bd_phys;
1451 priv->mdma.bd_base = bd_ptr;
1453 tsi_debug(MAINT, &priv->pdev->dev, "DMA descriptors @ %p (phys = %pad)",
1454 bd_ptr, &bd_phys);
1456 /* Allocate space for descriptor status FIFO */
1457 sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
1458 bd_num : TSI721_DMA_MINSTSSZ;
1459 sts_size = roundup_pow_of_two(sts_size);
1460 sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
1461 sts_size * sizeof(struct tsi721_dma_sts),
1462 &sts_phys, GFP_KERNEL);
1463 if (!sts_ptr) {
1464 /* Free space allocated for DMA descriptors */
1465 dma_free_coherent(&priv->pdev->dev,
1466 bd_num * sizeof(struct tsi721_dma_desc),
1467 bd_ptr, bd_phys);
1468 priv->mdma.bd_base = NULL;
1469 return -ENOMEM;
1472 priv->mdma.sts_phys = sts_phys;
1473 priv->mdma.sts_base = sts_ptr;
1474 priv->mdma.sts_size = sts_size;
1476 tsi_debug(MAINT, &priv->pdev->dev,
1477 "desc status FIFO @ %p (phys = %pad) size=0x%x",
1478 sts_ptr, &sts_phys, sts_size);
1480 /* Initialize DMA descriptors ring */
1481 bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
1482 bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
1483 TSI721_DMAC_DPTRL_MASK);
1484 bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
1486 /* Setup DMA descriptor pointers */
1487 iowrite32(((u64)bd_phys >> 32), regs + TSI721_DMAC_DPTRH);
1488 iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
1489 regs + TSI721_DMAC_DPTRL);
1491 /* Setup descriptor status FIFO */
1492 iowrite32(((u64)sts_phys >> 32), regs + TSI721_DMAC_DSBH);
1493 iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
1494 regs + TSI721_DMAC_DSBL);
1495 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
1496 regs + TSI721_DMAC_DSSZ);
1498 /* Clear interrupt bits */
1499 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
1501 ioread32(regs + TSI721_DMAC_INT);
1503 /* Toggle DMA channel initialization */
1504 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
1505 ioread32(regs + TSI721_DMAC_CTL);
1506 udelay(10);
1508 return 0;
1511 static int tsi721_bdma_maint_free(struct tsi721_device *priv)
1513 u32 ch_stat;
1514 struct tsi721_bdma_maint *mdma = &priv->mdma;
1515 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(mdma->ch_id);
1517 if (mdma->bd_base == NULL)
1518 return 0;
1520 /* Check if DMA channel still running */
1521 ch_stat = ioread32(regs + TSI721_DMAC_STS);
1522 if (ch_stat & TSI721_DMAC_STS_RUN)
1523 return -EFAULT;
1525 /* Put DMA channel into init state */
1526 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
1528 /* Free space allocated for DMA descriptors */
1529 dma_free_coherent(&priv->pdev->dev,
1530 mdma->bd_num * sizeof(struct tsi721_dma_desc),
1531 mdma->bd_base, mdma->bd_phys);
1532 mdma->bd_base = NULL;
1534 /* Free space allocated for status FIFO */
1535 dma_free_coherent(&priv->pdev->dev,
1536 mdma->sts_size * sizeof(struct tsi721_dma_sts),
1537 mdma->sts_base, mdma->sts_phys);
1538 mdma->sts_base = NULL;
1539 return 0;
1542 /* Enable Inbound Messaging Interrupts */
1543 static void
1544 tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
1545 u32 inte_mask)
1547 u32 rval;
1549 if (!inte_mask)
1550 return;
1552 /* Clear pending Inbound Messaging interrupts */
1553 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1555 /* Enable Inbound Messaging interrupts */
1556 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1557 iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
1559 if (priv->flags & TSI721_USING_MSIX)
1560 return; /* Finished if we are in MSI-X mode */
1563 * For MSI and INTA interrupt signalling we need to enable next levels
1566 /* Enable Device Channel Interrupt */
1567 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1568 iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
1569 priv->regs + TSI721_DEV_CHAN_INTE);
1572 /* Disable Inbound Messaging Interrupts */
1573 static void
1574 tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
1575 u32 inte_mask)
1577 u32 rval;
1579 if (!inte_mask)
1580 return;
1582 /* Clear pending Inbound Messaging interrupts */
1583 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1585 /* Disable Inbound Messaging interrupts */
1586 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1587 rval &= ~inte_mask;
1588 iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
1590 if (priv->flags & TSI721_USING_MSIX)
1591 return; /* Finished if we are in MSI-X mode */
1594 * For MSI and INTA interrupt signalling we need to disable next levels
1597 /* Disable Device Channel Interrupt */
1598 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1599 rval &= ~TSI721_INT_IMSG_CHAN(ch);
1600 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1603 /* Enable Outbound Messaging interrupts */
1604 static void
1605 tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
1606 u32 inte_mask)
1608 u32 rval;
1610 if (!inte_mask)
1611 return;
1613 /* Clear pending Outbound Messaging interrupts */
1614 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1616 /* Enable Outbound Messaging channel interrupts */
1617 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1618 iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
1620 if (priv->flags & TSI721_USING_MSIX)
1621 return; /* Finished if we are in MSI-X mode */
1624 * For MSI and INTA interrupt signalling we need to enable next levels
1627 /* Enable Device Channel Interrupt */
1628 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1629 iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
1630 priv->regs + TSI721_DEV_CHAN_INTE);
1633 /* Disable Outbound Messaging interrupts */
1634 static void
1635 tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
1636 u32 inte_mask)
1638 u32 rval;
1640 if (!inte_mask)
1641 return;
1643 /* Clear pending Outbound Messaging interrupts */
1644 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1646 /* Disable Outbound Messaging interrupts */
1647 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1648 rval &= ~inte_mask;
1649 iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
1651 if (priv->flags & TSI721_USING_MSIX)
1652 return; /* Finished if we are in MSI-X mode */
1655 * For MSI and INTA interrupt signalling we need to disable next levels
1658 /* Disable Device Channel Interrupt */
1659 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1660 rval &= ~TSI721_INT_OMSG_CHAN(ch);
1661 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1665 * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
1666 * @mport: Master port with outbound message queue
1667 * @rdev: Target of outbound message
1668 * @mbox: Outbound mailbox
1669 * @buffer: Message to add to outbound queue
1670 * @len: Length of message
1672 static int
1673 tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
1674 void *buffer, size_t len)
1676 struct tsi721_device *priv = mport->priv;
1677 struct tsi721_omsg_desc *desc;
1678 u32 tx_slot;
1679 unsigned long flags;
1681 if (!priv->omsg_init[mbox] ||
1682 len > TSI721_MSG_MAX_SIZE || len < 8)
1683 return -EINVAL;
1685 spin_lock_irqsave(&priv->omsg_ring[mbox].lock, flags);
1687 tx_slot = priv->omsg_ring[mbox].tx_slot;
1689 /* Copy copy message into transfer buffer */
1690 memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
1692 if (len & 0x7)
1693 len += 8;
1695 /* Build descriptor associated with buffer */
1696 desc = priv->omsg_ring[mbox].omd_base;
1697 desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
1698 #ifdef TSI721_OMSG_DESC_INT
1699 /* Request IOF_DONE interrupt generation for each N-th frame in queue */
1700 if (tx_slot % 4 == 0)
1701 desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
1702 #endif
1703 desc[tx_slot].msg_info =
1704 cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
1705 (0xe << 12) | (len & 0xff8));
1706 desc[tx_slot].bufptr_lo =
1707 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
1708 0xffffffff);
1709 desc[tx_slot].bufptr_hi =
1710 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
1712 priv->omsg_ring[mbox].wr_count++;
1714 /* Go to next descriptor */
1715 if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
1716 priv->omsg_ring[mbox].tx_slot = 0;
1717 /* Move through the ring link descriptor at the end */
1718 priv->omsg_ring[mbox].wr_count++;
1721 mb();
1723 /* Set new write count value */
1724 iowrite32(priv->omsg_ring[mbox].wr_count,
1725 priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1726 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1728 spin_unlock_irqrestore(&priv->omsg_ring[mbox].lock, flags);
1730 return 0;
1734 * tsi721_omsg_handler - Outbound Message Interrupt Handler
1735 * @priv: pointer to tsi721 private data
1736 * @ch: number of OB MSG channel to service
1738 * Services channel interrupts from outbound messaging engine.
1740 static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
1742 u32 omsg_int;
1743 struct rio_mport *mport = &priv->mport;
1744 void *dev_id = NULL;
1745 u32 tx_slot = 0xffffffff;
1746 int do_callback = 0;
1748 spin_lock(&priv->omsg_ring[ch].lock);
1750 omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
1752 if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
1753 tsi_info(&priv->pdev->dev,
1754 "OB MBOX%d: Status FIFO is full", ch);
1756 if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
1757 u32 srd_ptr;
1758 u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
1759 int i, j;
1762 * Find last successfully processed descriptor
1765 /* Check and clear descriptor status FIFO entries */
1766 srd_ptr = priv->omsg_ring[ch].sts_rdptr;
1767 sts_ptr = priv->omsg_ring[ch].sts_base;
1768 j = srd_ptr * 8;
1769 while (sts_ptr[j]) {
1770 for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
1771 prev_ptr = last_ptr;
1772 last_ptr = le64_to_cpu(sts_ptr[j]);
1773 sts_ptr[j] = 0;
1776 ++srd_ptr;
1777 srd_ptr %= priv->omsg_ring[ch].sts_size;
1778 j = srd_ptr * 8;
1781 if (last_ptr == 0)
1782 goto no_sts_update;
1784 priv->omsg_ring[ch].sts_rdptr = srd_ptr;
1785 iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
1787 if (!mport->outb_msg[ch].mcback)
1788 goto no_sts_update;
1790 /* Inform upper layer about transfer completion */
1792 tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
1793 sizeof(struct tsi721_omsg_desc);
1796 * Check if this is a Link Descriptor (LD).
1797 * If yes, ignore LD and use descriptor processed
1798 * before LD.
1800 if (tx_slot == priv->omsg_ring[ch].size) {
1801 if (prev_ptr)
1802 tx_slot = (prev_ptr -
1803 (u64)priv->omsg_ring[ch].omd_phys)/
1804 sizeof(struct tsi721_omsg_desc);
1805 else
1806 goto no_sts_update;
1809 if (tx_slot >= priv->omsg_ring[ch].size)
1810 tsi_debug(OMSG, &priv->pdev->dev,
1811 "OB_MSG tx_slot=%x > size=%x",
1812 tx_slot, priv->omsg_ring[ch].size);
1813 WARN_ON(tx_slot >= priv->omsg_ring[ch].size);
1815 /* Move slot index to the next message to be sent */
1816 ++tx_slot;
1817 if (tx_slot == priv->omsg_ring[ch].size)
1818 tx_slot = 0;
1820 dev_id = priv->omsg_ring[ch].dev_id;
1821 do_callback = 1;
1824 no_sts_update:
1826 if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
1828 * Outbound message operation aborted due to error,
1829 * reinitialize OB MSG channel
1832 tsi_debug(OMSG, &priv->pdev->dev, "OB MSG ABORT ch_stat=%x",
1833 ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
1835 iowrite32(TSI721_OBDMAC_INT_ERROR,
1836 priv->regs + TSI721_OBDMAC_INT(ch));
1837 iowrite32(TSI721_OBDMAC_CTL_RETRY_THR | TSI721_OBDMAC_CTL_INIT,
1838 priv->regs + TSI721_OBDMAC_CTL(ch));
1839 ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
1841 /* Inform upper level to clear all pending tx slots */
1842 dev_id = priv->omsg_ring[ch].dev_id;
1843 tx_slot = priv->omsg_ring[ch].tx_slot;
1844 do_callback = 1;
1846 /* Synch tx_slot tracking */
1847 iowrite32(priv->omsg_ring[ch].tx_slot,
1848 priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1849 ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1850 priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
1851 priv->omsg_ring[ch].sts_rdptr = 0;
1854 /* Clear channel interrupts */
1855 iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
1857 if (!(priv->flags & TSI721_USING_MSIX)) {
1858 u32 ch_inte;
1860 /* Re-enable channel interrupts */
1861 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1862 ch_inte |= TSI721_INT_OMSG_CHAN(ch);
1863 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1866 spin_unlock(&priv->omsg_ring[ch].lock);
1868 if (mport->outb_msg[ch].mcback && do_callback)
1869 mport->outb_msg[ch].mcback(mport, dev_id, ch, tx_slot);
1873 * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
1874 * @mport: Master port implementing Outbound Messaging Engine
1875 * @dev_id: Device specific pointer to pass on event
1876 * @mbox: Mailbox to open
1877 * @entries: Number of entries in the outbound mailbox ring
1879 static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
1880 int mbox, int entries)
1882 struct tsi721_device *priv = mport->priv;
1883 struct tsi721_omsg_desc *bd_ptr;
1884 int i, rc = 0;
1886 if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
1887 (entries > (TSI721_OMSGD_RING_SIZE)) ||
1888 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1889 rc = -EINVAL;
1890 goto out;
1893 if ((mbox_sel & (1 << mbox)) == 0) {
1894 rc = -ENODEV;
1895 goto out;
1898 priv->omsg_ring[mbox].dev_id = dev_id;
1899 priv->omsg_ring[mbox].size = entries;
1900 priv->omsg_ring[mbox].sts_rdptr = 0;
1901 spin_lock_init(&priv->omsg_ring[mbox].lock);
1903 /* Outbound Msg Buffer allocation based on
1904 the number of maximum descriptor entries */
1905 for (i = 0; i < entries; i++) {
1906 priv->omsg_ring[mbox].omq_base[i] =
1907 dma_alloc_coherent(
1908 &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
1909 &priv->omsg_ring[mbox].omq_phys[i],
1910 GFP_KERNEL);
1911 if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
1912 tsi_debug(OMSG, &priv->pdev->dev,
1913 "ENOMEM for OB_MSG_%d data buffer", mbox);
1914 rc = -ENOMEM;
1915 goto out_buf;
1919 /* Outbound message descriptor allocation */
1920 priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
1921 &priv->pdev->dev,
1922 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1923 &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
1924 if (priv->omsg_ring[mbox].omd_base == NULL) {
1925 tsi_debug(OMSG, &priv->pdev->dev,
1926 "ENOMEM for OB_MSG_%d descriptor memory", mbox);
1927 rc = -ENOMEM;
1928 goto out_buf;
1931 priv->omsg_ring[mbox].tx_slot = 0;
1933 /* Outbound message descriptor status FIFO allocation */
1934 priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
1935 priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
1936 priv->omsg_ring[mbox].sts_size *
1937 sizeof(struct tsi721_dma_sts),
1938 &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
1939 if (priv->omsg_ring[mbox].sts_base == NULL) {
1940 tsi_debug(OMSG, &priv->pdev->dev,
1941 "ENOMEM for OB_MSG_%d status FIFO", mbox);
1942 rc = -ENOMEM;
1943 goto out_desc;
1947 * Configure Outbound Messaging Engine
1950 /* Setup Outbound Message descriptor pointer */
1951 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
1952 priv->regs + TSI721_OBDMAC_DPTRH(mbox));
1953 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
1954 TSI721_OBDMAC_DPTRL_MASK),
1955 priv->regs + TSI721_OBDMAC_DPTRL(mbox));
1957 /* Setup Outbound Message descriptor status FIFO */
1958 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
1959 priv->regs + TSI721_OBDMAC_DSBH(mbox));
1960 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
1961 TSI721_OBDMAC_DSBL_MASK),
1962 priv->regs + TSI721_OBDMAC_DSBL(mbox));
1963 iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
1964 priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
1966 /* Enable interrupts */
1968 #ifdef CONFIG_PCI_MSI
1969 if (priv->flags & TSI721_USING_MSIX) {
1970 int idx = TSI721_VECT_OMB0_DONE + mbox;
1972 /* Request interrupt service if we are in MSI-X mode */
1973 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1974 priv->msix[idx].irq_name, (void *)priv);
1976 if (rc) {
1977 tsi_debug(OMSG, &priv->pdev->dev,
1978 "Unable to get MSI-X IRQ for OBOX%d-DONE",
1979 mbox);
1980 goto out_stat;
1983 idx = TSI721_VECT_OMB0_INT + mbox;
1984 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1985 priv->msix[idx].irq_name, (void *)priv);
1987 if (rc) {
1988 tsi_debug(OMSG, &priv->pdev->dev,
1989 "Unable to get MSI-X IRQ for MBOX%d-INT", mbox);
1990 idx = TSI721_VECT_OMB0_DONE + mbox;
1991 free_irq(priv->msix[idx].vector, (void *)priv);
1992 goto out_stat;
1995 #endif /* CONFIG_PCI_MSI */
1997 tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1999 /* Initialize Outbound Message descriptors ring */
2000 bd_ptr = priv->omsg_ring[mbox].omd_base;
2001 bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
2002 bd_ptr[entries].msg_info = 0;
2003 bd_ptr[entries].next_lo =
2004 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
2005 TSI721_OBDMAC_DPTRL_MASK);
2006 bd_ptr[entries].next_hi =
2007 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
2008 priv->omsg_ring[mbox].wr_count = 0;
2009 mb();
2011 /* Initialize Outbound Message engine */
2012 iowrite32(TSI721_OBDMAC_CTL_RETRY_THR | TSI721_OBDMAC_CTL_INIT,
2013 priv->regs + TSI721_OBDMAC_CTL(mbox));
2014 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
2015 udelay(10);
2017 priv->omsg_init[mbox] = 1;
2019 return 0;
2021 #ifdef CONFIG_PCI_MSI
2022 out_stat:
2023 dma_free_coherent(&priv->pdev->dev,
2024 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
2025 priv->omsg_ring[mbox].sts_base,
2026 priv->omsg_ring[mbox].sts_phys);
2028 priv->omsg_ring[mbox].sts_base = NULL;
2029 #endif /* CONFIG_PCI_MSI */
2031 out_desc:
2032 dma_free_coherent(&priv->pdev->dev,
2033 (entries + 1) * sizeof(struct tsi721_omsg_desc),
2034 priv->omsg_ring[mbox].omd_base,
2035 priv->omsg_ring[mbox].omd_phys);
2037 priv->omsg_ring[mbox].omd_base = NULL;
2039 out_buf:
2040 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
2041 if (priv->omsg_ring[mbox].omq_base[i]) {
2042 dma_free_coherent(&priv->pdev->dev,
2043 TSI721_MSG_BUFFER_SIZE,
2044 priv->omsg_ring[mbox].omq_base[i],
2045 priv->omsg_ring[mbox].omq_phys[i]);
2047 priv->omsg_ring[mbox].omq_base[i] = NULL;
2051 out:
2052 return rc;
2056 * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
2057 * @mport: Master port implementing the outbound message unit
2058 * @mbox: Mailbox to close
2060 static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
2062 struct tsi721_device *priv = mport->priv;
2063 u32 i;
2065 if (!priv->omsg_init[mbox])
2066 return;
2067 priv->omsg_init[mbox] = 0;
2069 /* Disable Interrupts */
2071 tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
2073 #ifdef CONFIG_PCI_MSI
2074 if (priv->flags & TSI721_USING_MSIX) {
2075 free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
2076 (void *)priv);
2077 free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
2078 (void *)priv);
2080 #endif /* CONFIG_PCI_MSI */
2082 /* Free OMSG Descriptor Status FIFO */
2083 dma_free_coherent(&priv->pdev->dev,
2084 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
2085 priv->omsg_ring[mbox].sts_base,
2086 priv->omsg_ring[mbox].sts_phys);
2088 priv->omsg_ring[mbox].sts_base = NULL;
2090 /* Free OMSG descriptors */
2091 dma_free_coherent(&priv->pdev->dev,
2092 (priv->omsg_ring[mbox].size + 1) *
2093 sizeof(struct tsi721_omsg_desc),
2094 priv->omsg_ring[mbox].omd_base,
2095 priv->omsg_ring[mbox].omd_phys);
2097 priv->omsg_ring[mbox].omd_base = NULL;
2099 /* Free message buffers */
2100 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
2101 if (priv->omsg_ring[mbox].omq_base[i]) {
2102 dma_free_coherent(&priv->pdev->dev,
2103 TSI721_MSG_BUFFER_SIZE,
2104 priv->omsg_ring[mbox].omq_base[i],
2105 priv->omsg_ring[mbox].omq_phys[i]);
2107 priv->omsg_ring[mbox].omq_base[i] = NULL;
2113 * tsi721_imsg_handler - Inbound Message Interrupt Handler
2114 * @priv: pointer to tsi721 private data
2115 * @ch: inbound message channel number to service
2117 * Services channel interrupts from inbound messaging engine.
2119 static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
2121 u32 mbox = ch - 4;
2122 u32 imsg_int;
2123 struct rio_mport *mport = &priv->mport;
2125 spin_lock(&priv->imsg_ring[mbox].lock);
2127 imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
2129 if (imsg_int & TSI721_IBDMAC_INT_SRTO)
2130 tsi_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout", mbox);
2132 if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
2133 tsi_info(&priv->pdev->dev, "IB MBOX%d PCIe error", mbox);
2135 if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
2136 tsi_info(&priv->pdev->dev, "IB MBOX%d IB free queue low", mbox);
2138 /* Clear IB channel interrupts */
2139 iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
2141 /* If an IB Msg is received notify the upper layer */
2142 if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
2143 mport->inb_msg[mbox].mcback)
2144 mport->inb_msg[mbox].mcback(mport,
2145 priv->imsg_ring[mbox].dev_id, mbox, -1);
2147 if (!(priv->flags & TSI721_USING_MSIX)) {
2148 u32 ch_inte;
2150 /* Re-enable channel interrupts */
2151 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
2152 ch_inte |= TSI721_INT_IMSG_CHAN(ch);
2153 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
2156 spin_unlock(&priv->imsg_ring[mbox].lock);
2160 * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
2161 * @mport: Master port implementing the Inbound Messaging Engine
2162 * @dev_id: Device specific pointer to pass on event
2163 * @mbox: Mailbox to open
2164 * @entries: Number of entries in the inbound mailbox ring
2166 static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
2167 int mbox, int entries)
2169 struct tsi721_device *priv = mport->priv;
2170 int ch = mbox + 4;
2171 int i;
2172 u64 *free_ptr;
2173 int rc = 0;
2175 if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
2176 (entries > TSI721_IMSGD_RING_SIZE) ||
2177 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
2178 rc = -EINVAL;
2179 goto out;
2182 if ((mbox_sel & (1 << mbox)) == 0) {
2183 rc = -ENODEV;
2184 goto out;
2187 /* Initialize IB Messaging Ring */
2188 priv->imsg_ring[mbox].dev_id = dev_id;
2189 priv->imsg_ring[mbox].size = entries;
2190 priv->imsg_ring[mbox].rx_slot = 0;
2191 priv->imsg_ring[mbox].desc_rdptr = 0;
2192 priv->imsg_ring[mbox].fq_wrptr = 0;
2193 for (i = 0; i < priv->imsg_ring[mbox].size; i++)
2194 priv->imsg_ring[mbox].imq_base[i] = NULL;
2195 spin_lock_init(&priv->imsg_ring[mbox].lock);
2197 /* Allocate buffers for incoming messages */
2198 priv->imsg_ring[mbox].buf_base =
2199 dma_alloc_coherent(&priv->pdev->dev,
2200 entries * TSI721_MSG_BUFFER_SIZE,
2201 &priv->imsg_ring[mbox].buf_phys,
2202 GFP_KERNEL);
2204 if (priv->imsg_ring[mbox].buf_base == NULL) {
2205 tsi_err(&priv->pdev->dev,
2206 "Failed to allocate buffers for IB MBOX%d", mbox);
2207 rc = -ENOMEM;
2208 goto out;
2211 /* Allocate memory for circular free list */
2212 priv->imsg_ring[mbox].imfq_base =
2213 dma_alloc_coherent(&priv->pdev->dev,
2214 entries * 8,
2215 &priv->imsg_ring[mbox].imfq_phys,
2216 GFP_KERNEL);
2218 if (priv->imsg_ring[mbox].imfq_base == NULL) {
2219 tsi_err(&priv->pdev->dev,
2220 "Failed to allocate free queue for IB MBOX%d", mbox);
2221 rc = -ENOMEM;
2222 goto out_buf;
2225 /* Allocate memory for Inbound message descriptors */
2226 priv->imsg_ring[mbox].imd_base =
2227 dma_alloc_coherent(&priv->pdev->dev,
2228 entries * sizeof(struct tsi721_imsg_desc),
2229 &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
2231 if (priv->imsg_ring[mbox].imd_base == NULL) {
2232 tsi_err(&priv->pdev->dev,
2233 "Failed to allocate descriptor memory for IB MBOX%d",
2234 mbox);
2235 rc = -ENOMEM;
2236 goto out_dma;
2239 /* Fill free buffer pointer list */
2240 free_ptr = priv->imsg_ring[mbox].imfq_base;
2241 for (i = 0; i < entries; i++)
2242 free_ptr[i] = cpu_to_le64(
2243 (u64)(priv->imsg_ring[mbox].buf_phys) +
2244 i * 0x1000);
2246 mb();
2249 * For mapping of inbound SRIO Messages into appropriate queues we need
2250 * to set Inbound Device ID register in the messaging engine. We do it
2251 * once when first inbound mailbox is requested.
2253 if (!(priv->flags & TSI721_IMSGID_SET)) {
2254 iowrite32((u32)priv->mport.host_deviceid,
2255 priv->regs + TSI721_IB_DEVID);
2256 priv->flags |= TSI721_IMSGID_SET;
2260 * Configure Inbound Messaging channel (ch = mbox + 4)
2263 /* Setup Inbound Message free queue */
2264 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
2265 priv->regs + TSI721_IBDMAC_FQBH(ch));
2266 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
2267 TSI721_IBDMAC_FQBL_MASK),
2268 priv->regs+TSI721_IBDMAC_FQBL(ch));
2269 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2270 priv->regs + TSI721_IBDMAC_FQSZ(ch));
2272 /* Setup Inbound Message descriptor queue */
2273 iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
2274 priv->regs + TSI721_IBDMAC_DQBH(ch));
2275 iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
2276 (u32)TSI721_IBDMAC_DQBL_MASK),
2277 priv->regs+TSI721_IBDMAC_DQBL(ch));
2278 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2279 priv->regs + TSI721_IBDMAC_DQSZ(ch));
2281 /* Enable interrupts */
2283 #ifdef CONFIG_PCI_MSI
2284 if (priv->flags & TSI721_USING_MSIX) {
2285 int idx = TSI721_VECT_IMB0_RCV + mbox;
2287 /* Request interrupt service if we are in MSI-X mode */
2288 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2289 priv->msix[idx].irq_name, (void *)priv);
2291 if (rc) {
2292 tsi_debug(IMSG, &priv->pdev->dev,
2293 "Unable to get MSI-X IRQ for IBOX%d-DONE",
2294 mbox);
2295 goto out_desc;
2298 idx = TSI721_VECT_IMB0_INT + mbox;
2299 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2300 priv->msix[idx].irq_name, (void *)priv);
2302 if (rc) {
2303 tsi_debug(IMSG, &priv->pdev->dev,
2304 "Unable to get MSI-X IRQ for IBOX%d-INT", mbox);
2305 free_irq(
2306 priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
2307 (void *)priv);
2308 goto out_desc;
2311 #endif /* CONFIG_PCI_MSI */
2313 tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
2315 /* Initialize Inbound Message Engine */
2316 iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
2317 ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
2318 udelay(10);
2319 priv->imsg_ring[mbox].fq_wrptr = entries - 1;
2320 iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
2322 priv->imsg_init[mbox] = 1;
2323 return 0;
2325 #ifdef CONFIG_PCI_MSI
2326 out_desc:
2327 dma_free_coherent(&priv->pdev->dev,
2328 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2329 priv->imsg_ring[mbox].imd_base,
2330 priv->imsg_ring[mbox].imd_phys);
2332 priv->imsg_ring[mbox].imd_base = NULL;
2333 #endif /* CONFIG_PCI_MSI */
2335 out_dma:
2336 dma_free_coherent(&priv->pdev->dev,
2337 priv->imsg_ring[mbox].size * 8,
2338 priv->imsg_ring[mbox].imfq_base,
2339 priv->imsg_ring[mbox].imfq_phys);
2341 priv->imsg_ring[mbox].imfq_base = NULL;
2343 out_buf:
2344 dma_free_coherent(&priv->pdev->dev,
2345 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2346 priv->imsg_ring[mbox].buf_base,
2347 priv->imsg_ring[mbox].buf_phys);
2349 priv->imsg_ring[mbox].buf_base = NULL;
2351 out:
2352 return rc;
2356 * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
2357 * @mport: Master port implementing the Inbound Messaging Engine
2358 * @mbox: Mailbox to close
2360 static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
2362 struct tsi721_device *priv = mport->priv;
2363 u32 rx_slot;
2364 int ch = mbox + 4;
2366 if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
2367 return;
2368 priv->imsg_init[mbox] = 0;
2370 /* Disable Inbound Messaging Engine */
2372 /* Disable Interrupts */
2373 tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
2375 #ifdef CONFIG_PCI_MSI
2376 if (priv->flags & TSI721_USING_MSIX) {
2377 free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
2378 (void *)priv);
2379 free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
2380 (void *)priv);
2382 #endif /* CONFIG_PCI_MSI */
2384 /* Clear Inbound Buffer Queue */
2385 for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
2386 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2388 /* Free memory allocated for message buffers */
2389 dma_free_coherent(&priv->pdev->dev,
2390 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2391 priv->imsg_ring[mbox].buf_base,
2392 priv->imsg_ring[mbox].buf_phys);
2394 priv->imsg_ring[mbox].buf_base = NULL;
2396 /* Free memory allocated for free pointr list */
2397 dma_free_coherent(&priv->pdev->dev,
2398 priv->imsg_ring[mbox].size * 8,
2399 priv->imsg_ring[mbox].imfq_base,
2400 priv->imsg_ring[mbox].imfq_phys);
2402 priv->imsg_ring[mbox].imfq_base = NULL;
2404 /* Free memory allocated for RX descriptors */
2405 dma_free_coherent(&priv->pdev->dev,
2406 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2407 priv->imsg_ring[mbox].imd_base,
2408 priv->imsg_ring[mbox].imd_phys);
2410 priv->imsg_ring[mbox].imd_base = NULL;
2414 * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
2415 * @mport: Master port implementing the Inbound Messaging Engine
2416 * @mbox: Inbound mailbox number
2417 * @buf: Buffer to add to inbound queue
2419 static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
2421 struct tsi721_device *priv = mport->priv;
2422 u32 rx_slot;
2423 int rc = 0;
2425 rx_slot = priv->imsg_ring[mbox].rx_slot;
2426 if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
2427 tsi_err(&priv->pdev->dev,
2428 "Error adding inbound buffer %d, buffer exists",
2429 rx_slot);
2430 rc = -EINVAL;
2431 goto out;
2434 priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
2436 if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
2437 priv->imsg_ring[mbox].rx_slot = 0;
2439 out:
2440 return rc;
2444 * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
2445 * @mport: Master port implementing the Inbound Messaging Engine
2446 * @mbox: Inbound mailbox number
2448 * Returns pointer to the message on success or NULL on failure.
2450 static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
2452 struct tsi721_device *priv = mport->priv;
2453 struct tsi721_imsg_desc *desc;
2454 u32 rx_slot;
2455 void *rx_virt = NULL;
2456 u64 rx_phys;
2457 void *buf = NULL;
2458 u64 *free_ptr;
2459 int ch = mbox + 4;
2460 int msg_size;
2462 if (!priv->imsg_init[mbox])
2463 return NULL;
2465 desc = priv->imsg_ring[mbox].imd_base;
2466 desc += priv->imsg_ring[mbox].desc_rdptr;
2468 if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
2469 goto out;
2471 rx_slot = priv->imsg_ring[mbox].rx_slot;
2472 while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
2473 if (++rx_slot == priv->imsg_ring[mbox].size)
2474 rx_slot = 0;
2477 rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
2478 le32_to_cpu(desc->bufptr_lo);
2480 rx_virt = priv->imsg_ring[mbox].buf_base +
2481 (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
2483 buf = priv->imsg_ring[mbox].imq_base[rx_slot];
2484 msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
2485 if (msg_size == 0)
2486 msg_size = RIO_MAX_MSG_SIZE;
2488 memcpy(buf, rx_virt, msg_size);
2489 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2491 desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
2492 if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
2493 priv->imsg_ring[mbox].desc_rdptr = 0;
2495 iowrite32(priv->imsg_ring[mbox].desc_rdptr,
2496 priv->regs + TSI721_IBDMAC_DQRP(ch));
2498 /* Return free buffer into the pointer list */
2499 free_ptr = priv->imsg_ring[mbox].imfq_base;
2500 free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
2502 if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
2503 priv->imsg_ring[mbox].fq_wrptr = 0;
2505 iowrite32(priv->imsg_ring[mbox].fq_wrptr,
2506 priv->regs + TSI721_IBDMAC_FQWP(ch));
2507 out:
2508 return buf;
2512 * tsi721_messages_init - Initialization of Messaging Engine
2513 * @priv: pointer to tsi721 private data
2515 * Configures Tsi721 messaging engine.
2517 static int tsi721_messages_init(struct tsi721_device *priv)
2519 int ch;
2521 iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
2522 iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
2523 iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
2525 /* Set SRIO Message Request/Response Timeout */
2526 iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
2528 /* Initialize Inbound Messaging Engine Registers */
2529 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
2530 /* Clear interrupt bits */
2531 iowrite32(TSI721_IBDMAC_INT_MASK,
2532 priv->regs + TSI721_IBDMAC_INT(ch));
2533 /* Clear Status */
2534 iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
2536 iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
2537 priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
2538 iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
2539 priv->regs + TSI721_SMSG_ECC_NCOR(ch));
2542 return 0;
2546 * tsi721_query_mport - Fetch inbound message from the Tsi721 MSG Queue
2547 * @mport: Master port implementing the Inbound Messaging Engine
2548 * @mbox: Inbound mailbox number
2550 * Returns pointer to the message on success or NULL on failure.
2552 static int tsi721_query_mport(struct rio_mport *mport,
2553 struct rio_mport_attr *attr)
2555 struct tsi721_device *priv = mport->priv;
2556 u32 rval;
2558 rval = ioread32(priv->regs + 0x100 + RIO_PORT_N_ERR_STS_CSR(0, 0));
2559 if (rval & RIO_PORT_N_ERR_STS_PORT_OK) {
2560 rval = ioread32(priv->regs + 0x100 + RIO_PORT_N_CTL2_CSR(0, 0));
2561 attr->link_speed = (rval & RIO_PORT_N_CTL2_SEL_BAUD) >> 28;
2562 rval = ioread32(priv->regs + 0x100 + RIO_PORT_N_CTL_CSR(0, 0));
2563 attr->link_width = (rval & RIO_PORT_N_CTL_IPW) >> 27;
2564 } else
2565 attr->link_speed = RIO_LINK_DOWN;
2567 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
2568 attr->flags = RIO_MPORT_DMA | RIO_MPORT_DMA_SG;
2569 attr->dma_max_sge = 0;
2570 attr->dma_max_size = TSI721_BDMA_MAX_BCOUNT;
2571 attr->dma_align = 0;
2572 #else
2573 attr->flags = 0;
2574 #endif
2575 return 0;
2579 * tsi721_disable_ints - disables all device interrupts
2580 * @priv: pointer to tsi721 private data
2582 static void tsi721_disable_ints(struct tsi721_device *priv)
2584 int ch;
2586 /* Disable all device level interrupts */
2587 iowrite32(0, priv->regs + TSI721_DEV_INTE);
2589 /* Disable all Device Channel interrupts */
2590 iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
2592 /* Disable all Inbound Msg Channel interrupts */
2593 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
2594 iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
2596 /* Disable all Outbound Msg Channel interrupts */
2597 for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
2598 iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
2600 /* Disable all general messaging interrupts */
2601 iowrite32(0, priv->regs + TSI721_SMSG_INTE);
2603 /* Disable all BDMA Channel interrupts */
2604 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
2605 iowrite32(0,
2606 priv->regs + TSI721_DMAC_BASE(ch) + TSI721_DMAC_INTE);
2608 /* Disable all general BDMA interrupts */
2609 iowrite32(0, priv->regs + TSI721_BDMA_INTE);
2611 /* Disable all SRIO Channel interrupts */
2612 for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
2613 iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
2615 /* Disable all general SR2PC interrupts */
2616 iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
2618 /* Disable all PC2SR interrupts */
2619 iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
2621 /* Disable all I2C interrupts */
2622 iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
2624 /* Disable SRIO MAC interrupts */
2625 iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
2626 iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
2629 static struct rio_ops tsi721_rio_ops = {
2630 .lcread = tsi721_lcread,
2631 .lcwrite = tsi721_lcwrite,
2632 .cread = tsi721_cread_dma,
2633 .cwrite = tsi721_cwrite_dma,
2634 .dsend = tsi721_dsend,
2635 .open_inb_mbox = tsi721_open_inb_mbox,
2636 .close_inb_mbox = tsi721_close_inb_mbox,
2637 .open_outb_mbox = tsi721_open_outb_mbox,
2638 .close_outb_mbox = tsi721_close_outb_mbox,
2639 .add_outb_message = tsi721_add_outb_message,
2640 .add_inb_buffer = tsi721_add_inb_buffer,
2641 .get_inb_message = tsi721_get_inb_message,
2642 .map_inb = tsi721_rio_map_inb_mem,
2643 .unmap_inb = tsi721_rio_unmap_inb_mem,
2644 .pwenable = tsi721_pw_enable,
2645 .query_mport = tsi721_query_mport,
2646 .map_outb = tsi721_map_outb_win,
2647 .unmap_outb = tsi721_unmap_outb_win,
2650 static void tsi721_mport_release(struct device *dev)
2652 struct rio_mport *mport = to_rio_mport(dev);
2654 tsi_debug(EXIT, dev, "%s id=%d", mport->name, mport->id);
2658 * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
2659 * @priv: pointer to tsi721 private data
2661 * Configures Tsi721 as RapidIO master port.
2663 static int tsi721_setup_mport(struct tsi721_device *priv)
2665 struct pci_dev *pdev = priv->pdev;
2666 int err = 0;
2667 struct rio_mport *mport = &priv->mport;
2669 err = rio_mport_initialize(mport);
2670 if (err)
2671 return err;
2673 mport->ops = &tsi721_rio_ops;
2674 mport->index = 0;
2675 mport->sys_size = 0; /* small system */
2676 mport->priv = (void *)priv;
2677 mport->phys_efptr = 0x100;
2678 mport->phys_rmap = 1;
2679 mport->dev.parent = &pdev->dev;
2680 mport->dev.release = tsi721_mport_release;
2682 INIT_LIST_HEAD(&mport->dbells);
2684 rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
2685 rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
2686 rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
2687 snprintf(mport->name, RIO_MAX_MPORT_NAME, "%s(%s)",
2688 dev_driver_string(&pdev->dev), dev_name(&pdev->dev));
2690 /* Hook up interrupt handler */
2692 #ifdef CONFIG_PCI_MSI
2693 if (!tsi721_enable_msix(priv))
2694 priv->flags |= TSI721_USING_MSIX;
2695 else if (!pci_enable_msi(pdev))
2696 priv->flags |= TSI721_USING_MSI;
2697 else
2698 tsi_debug(MPORT, &pdev->dev,
2699 "MSI/MSI-X is not available. Using legacy INTx.");
2700 #endif /* CONFIG_PCI_MSI */
2702 err = tsi721_request_irq(priv);
2704 if (err) {
2705 tsi_err(&pdev->dev, "Unable to get PCI IRQ %02X (err=0x%x)",
2706 pdev->irq, err);
2707 return err;
2710 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
2711 err = tsi721_register_dma(priv);
2712 if (err)
2713 goto err_exit;
2714 #endif
2715 /* Enable SRIO link */
2716 iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
2717 TSI721_DEVCTL_SRBOOT_CMPL,
2718 priv->regs + TSI721_DEVCTL);
2720 if (mport->host_deviceid >= 0)
2721 iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
2722 RIO_PORT_GEN_DISCOVERED,
2723 priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2724 else
2725 iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2727 err = rio_register_mport(mport);
2728 if (err) {
2729 tsi721_unregister_dma(priv);
2730 goto err_exit;
2733 return 0;
2735 err_exit:
2736 tsi721_free_irq(priv);
2737 return err;
2740 static int tsi721_probe(struct pci_dev *pdev,
2741 const struct pci_device_id *id)
2743 struct tsi721_device *priv;
2744 int err;
2746 priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
2747 if (!priv) {
2748 err = -ENOMEM;
2749 goto err_exit;
2752 err = pci_enable_device(pdev);
2753 if (err) {
2754 tsi_err(&pdev->dev, "Failed to enable PCI device");
2755 goto err_clean;
2758 priv->pdev = pdev;
2760 #ifdef DEBUG
2762 int i;
2764 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
2765 tsi_debug(INIT, &pdev->dev, "res%d %pR",
2766 i, &pdev->resource[i]);
2769 #endif
2771 * Verify BAR configuration
2774 /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
2775 if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
2776 pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
2777 pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
2778 tsi_err(&pdev->dev, "Missing or misconfigured CSR BAR0");
2779 err = -ENODEV;
2780 goto err_disable_pdev;
2783 /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
2784 if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
2785 pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
2786 pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
2787 tsi_err(&pdev->dev, "Missing or misconfigured Doorbell BAR1");
2788 err = -ENODEV;
2789 goto err_disable_pdev;
2793 * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
2794 * space.
2795 * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
2796 * It may be a good idea to keep them disabled using HW configuration
2797 * to save PCI memory space.
2800 priv->p2r_bar[0].size = priv->p2r_bar[1].size = 0;
2802 if (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64) {
2803 if (pci_resource_flags(pdev, BAR_2) & IORESOURCE_PREFETCH)
2804 tsi_debug(INIT, &pdev->dev,
2805 "Prefetchable OBW BAR2 will not be used");
2806 else {
2807 priv->p2r_bar[0].base = pci_resource_start(pdev, BAR_2);
2808 priv->p2r_bar[0].size = pci_resource_len(pdev, BAR_2);
2812 if (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64) {
2813 if (pci_resource_flags(pdev, BAR_4) & IORESOURCE_PREFETCH)
2814 tsi_debug(INIT, &pdev->dev,
2815 "Prefetchable OBW BAR4 will not be used");
2816 else {
2817 priv->p2r_bar[1].base = pci_resource_start(pdev, BAR_4);
2818 priv->p2r_bar[1].size = pci_resource_len(pdev, BAR_4);
2822 err = pci_request_regions(pdev, DRV_NAME);
2823 if (err) {
2824 tsi_err(&pdev->dev, "Unable to obtain PCI resources");
2825 goto err_disable_pdev;
2828 pci_set_master(pdev);
2830 priv->regs = pci_ioremap_bar(pdev, BAR_0);
2831 if (!priv->regs) {
2832 tsi_err(&pdev->dev, "Unable to map device registers space");
2833 err = -ENOMEM;
2834 goto err_free_res;
2837 priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
2838 if (!priv->odb_base) {
2839 tsi_err(&pdev->dev, "Unable to map outbound doorbells space");
2840 err = -ENOMEM;
2841 goto err_unmap_bars;
2844 /* Configure DMA attributes. */
2845 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
2846 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2847 if (err) {
2848 tsi_err(&pdev->dev, "Unable to set DMA mask");
2849 goto err_unmap_bars;
2852 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2853 tsi_info(&pdev->dev, "Unable to set consistent DMA mask");
2854 } else {
2855 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2856 if (err)
2857 tsi_info(&pdev->dev, "Unable to set consistent DMA mask");
2860 BUG_ON(!pci_is_pcie(pdev));
2862 /* Clear "no snoop" and "relaxed ordering" bits. */
2863 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
2864 PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN, 0);
2866 /* Override PCIe Maximum Read Request Size setting if requested */
2867 if (pcie_mrrs >= 0) {
2868 if (pcie_mrrs <= 5)
2869 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
2870 PCI_EXP_DEVCTL_READRQ, pcie_mrrs << 12);
2871 else
2872 tsi_info(&pdev->dev,
2873 "Invalid MRRS override value %d", pcie_mrrs);
2876 /* Adjust PCIe completion timeout. */
2877 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL2, 0xf, 0x2);
2880 * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
2882 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
2883 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
2884 TSI721_MSIXTBL_OFFSET);
2885 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
2886 TSI721_MSIXPBA_OFFSET);
2887 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
2888 /* End of FIXUP */
2890 tsi721_disable_ints(priv);
2892 tsi721_init_pc2sr_mapping(priv);
2893 tsi721_init_sr2pc_mapping(priv);
2895 if (tsi721_bdma_maint_init(priv)) {
2896 tsi_err(&pdev->dev, "BDMA initialization failed");
2897 err = -ENOMEM;
2898 goto err_unmap_bars;
2901 err = tsi721_doorbell_init(priv);
2902 if (err)
2903 goto err_free_bdma;
2905 tsi721_port_write_init(priv);
2907 err = tsi721_messages_init(priv);
2908 if (err)
2909 goto err_free_consistent;
2911 err = tsi721_setup_mport(priv);
2912 if (err)
2913 goto err_free_consistent;
2915 pci_set_drvdata(pdev, priv);
2916 tsi721_interrupts_init(priv);
2918 return 0;
2920 err_free_consistent:
2921 tsi721_port_write_free(priv);
2922 tsi721_doorbell_free(priv);
2923 err_free_bdma:
2924 tsi721_bdma_maint_free(priv);
2925 err_unmap_bars:
2926 if (priv->regs)
2927 iounmap(priv->regs);
2928 if (priv->odb_base)
2929 iounmap(priv->odb_base);
2930 err_free_res:
2931 pci_release_regions(pdev);
2932 pci_clear_master(pdev);
2933 err_disable_pdev:
2934 pci_disable_device(pdev);
2935 err_clean:
2936 kfree(priv);
2937 err_exit:
2938 return err;
2941 static void tsi721_remove(struct pci_dev *pdev)
2943 struct tsi721_device *priv = pci_get_drvdata(pdev);
2945 tsi_debug(EXIT, &pdev->dev, "enter");
2947 tsi721_disable_ints(priv);
2948 tsi721_free_irq(priv);
2949 flush_scheduled_work();
2950 rio_unregister_mport(&priv->mport);
2952 tsi721_unregister_dma(priv);
2953 tsi721_bdma_maint_free(priv);
2954 tsi721_doorbell_free(priv);
2955 tsi721_port_write_free(priv);
2956 tsi721_close_sr2pc_mapping(priv);
2958 if (priv->regs)
2959 iounmap(priv->regs);
2960 if (priv->odb_base)
2961 iounmap(priv->odb_base);
2962 #ifdef CONFIG_PCI_MSI
2963 if (priv->flags & TSI721_USING_MSIX)
2964 pci_disable_msix(priv->pdev);
2965 else if (priv->flags & TSI721_USING_MSI)
2966 pci_disable_msi(priv->pdev);
2967 #endif
2968 pci_release_regions(pdev);
2969 pci_clear_master(pdev);
2970 pci_disable_device(pdev);
2971 pci_set_drvdata(pdev, NULL);
2972 kfree(priv);
2973 tsi_debug(EXIT, &pdev->dev, "exit");
2976 static void tsi721_shutdown(struct pci_dev *pdev)
2978 struct tsi721_device *priv = pci_get_drvdata(pdev);
2980 tsi_debug(EXIT, &pdev->dev, "enter");
2982 tsi721_disable_ints(priv);
2983 tsi721_dma_stop_all(priv);
2984 pci_clear_master(pdev);
2985 pci_disable_device(pdev);
2988 static const struct pci_device_id tsi721_pci_tbl[] = {
2989 { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
2990 { 0, } /* terminate list */
2993 MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
2995 static struct pci_driver tsi721_driver = {
2996 .name = "tsi721",
2997 .id_table = tsi721_pci_tbl,
2998 .probe = tsi721_probe,
2999 .remove = tsi721_remove,
3000 .shutdown = tsi721_shutdown,
3003 module_pci_driver(tsi721_driver);
3005 MODULE_DESCRIPTION("IDT Tsi721 PCIExpress-to-SRIO bridge driver");
3006 MODULE_AUTHOR("Integrated Device Technology, Inc.");
3007 MODULE_LICENSE("GPL");