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)
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
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.
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>
39 #define DEBUG_PW /* Inbound Port-Write debugging */
41 static void tsi721_omsg_handler(struct tsi721_device
*priv
, int ch
);
42 static void tsi721_imsg_handler(struct tsi721_device
*priv
, int ch
);
45 * tsi721_lcread - read from local SREP config space
46 * @mport: RapidIO master port info
47 * @index: ID of RapdiIO interface
48 * @offset: Offset into configuration space
49 * @len: Length (in bytes) of the maintenance transaction
50 * @data: Value to be read into
52 * Generates a local SREP space read. Returns %0 on
53 * success or %-EINVAL on failure.
55 static int tsi721_lcread(struct rio_mport
*mport
, int index
, u32 offset
,
58 struct tsi721_device
*priv
= mport
->priv
;
60 if (len
!= sizeof(u32
))
61 return -EINVAL
; /* only 32-bit access is supported */
63 *data
= ioread32(priv
->regs
+ offset
);
69 * tsi721_lcwrite - write into local SREP config space
70 * @mport: RapidIO master port info
71 * @index: ID of RapdiIO interface
72 * @offset: Offset into configuration space
73 * @len: Length (in bytes) of the maintenance transaction
74 * @data: Value to be written
76 * Generates a local write into SREP configuration space. Returns %0 on
77 * success or %-EINVAL on failure.
79 static int tsi721_lcwrite(struct rio_mport
*mport
, int index
, u32 offset
,
82 struct tsi721_device
*priv
= mport
->priv
;
84 if (len
!= sizeof(u32
))
85 return -EINVAL
; /* only 32-bit access is supported */
87 iowrite32(data
, priv
->regs
+ offset
);
93 * tsi721_maint_dma - Helper function to generate RapidIO maintenance
94 * transactions using designated Tsi721 DMA channel.
95 * @priv: pointer to tsi721 private data
96 * @sys_size: RapdiIO transport system size
97 * @destid: Destination ID of transaction
98 * @hopcount: Number of hops to target device
99 * @offset: Offset into configuration space
100 * @len: Length (in bytes) of the maintenance transaction
101 * @data: Location to be read from or write into
102 * @do_wr: Operation flag (1 == MAINT_WR)
104 * Generates a RapidIO maintenance transaction (Read or Write).
105 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
107 static int tsi721_maint_dma(struct tsi721_device
*priv
, u32 sys_size
,
108 u16 destid
, u8 hopcount
, u32 offset
, int len
,
109 u32
*data
, int do_wr
)
111 struct tsi721_dma_desc
*bd_ptr
;
112 u32 rd_count
, swr_ptr
, ch_stat
;
114 u32 op
= do_wr
? MAINT_WR
: MAINT_RD
;
116 if (offset
> (RIO_MAINT_SPACE_SZ
- len
) || (len
!= sizeof(u32
)))
119 bd_ptr
= priv
->bdma
[TSI721_DMACH_MAINT
].bd_base
;
122 priv
->regs
+ TSI721_DMAC_DRDCNT(TSI721_DMACH_MAINT
));
124 /* Initialize DMA descriptor */
125 bd_ptr
[0].type_id
= cpu_to_le32((DTYPE2
<< 29) | (op
<< 19) | destid
);
126 bd_ptr
[0].bcount
= cpu_to_le32((sys_size
<< 26) | 0x04);
127 bd_ptr
[0].raddr_lo
= cpu_to_le32((hopcount
<< 24) | offset
);
128 bd_ptr
[0].raddr_hi
= 0;
130 bd_ptr
[0].data
[0] = cpu_to_be32p(data
);
132 bd_ptr
[0].data
[0] = 0xffffffff;
136 /* Start DMA operation */
137 iowrite32(rd_count
+ 2,
138 priv
->regs
+ TSI721_DMAC_DWRCNT(TSI721_DMACH_MAINT
));
139 ioread32(priv
->regs
+ TSI721_DMAC_DWRCNT(TSI721_DMACH_MAINT
));
142 /* Wait until DMA transfer is finished */
143 while ((ch_stat
= ioread32(priv
->regs
+
144 TSI721_DMAC_STS(TSI721_DMACH_MAINT
))) & TSI721_DMAC_STS_RUN
) {
146 if (++i
>= 5000000) {
147 dev_dbg(&priv
->pdev
->dev
,
148 "%s : DMA[%d] read timeout ch_status=%x\n",
149 __func__
, TSI721_DMACH_MAINT
, ch_stat
);
157 if (ch_stat
& TSI721_DMAC_STS_ABORT
) {
158 /* If DMA operation aborted due to error,
159 * reinitialize DMA channel
161 dev_dbg(&priv
->pdev
->dev
, "%s : DMA ABORT ch_stat=%x\n",
163 dev_dbg(&priv
->pdev
->dev
, "OP=%d : destid=%x hc=%x off=%x\n",
164 do_wr
? MAINT_WR
: MAINT_RD
, destid
, hopcount
, offset
);
165 iowrite32(TSI721_DMAC_INT_ALL
,
166 priv
->regs
+ TSI721_DMAC_INT(TSI721_DMACH_MAINT
));
167 iowrite32(TSI721_DMAC_CTL_INIT
,
168 priv
->regs
+ TSI721_DMAC_CTL(TSI721_DMACH_MAINT
));
170 iowrite32(0, priv
->regs
+
171 TSI721_DMAC_DWRCNT(TSI721_DMACH_MAINT
));
180 *data
= be32_to_cpu(bd_ptr
[0].data
[0]);
183 * Update descriptor status FIFO RD pointer.
184 * NOTE: Skipping check and clear FIFO entries because we are waiting
185 * for transfer to be completed.
187 swr_ptr
= ioread32(priv
->regs
+ TSI721_DMAC_DSWP(TSI721_DMACH_MAINT
));
188 iowrite32(swr_ptr
, priv
->regs
+ TSI721_DMAC_DSRP(TSI721_DMACH_MAINT
));
195 * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
196 * using Tsi721 BDMA engine.
197 * @mport: RapidIO master port control structure
198 * @index: ID of RapdiIO interface
199 * @destid: Destination ID of transaction
200 * @hopcount: Number of hops to target device
201 * @offset: Offset into configuration space
202 * @len: Length (in bytes) of the maintenance transaction
203 * @val: Location to be read into
205 * Generates a RapidIO maintenance read transaction.
206 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
208 static int tsi721_cread_dma(struct rio_mport
*mport
, int index
, u16 destid
,
209 u8 hopcount
, u32 offset
, int len
, u32
*data
)
211 struct tsi721_device
*priv
= mport
->priv
;
213 return tsi721_maint_dma(priv
, mport
->sys_size
, destid
, hopcount
,
214 offset
, len
, data
, 0);
218 * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
219 * using Tsi721 BDMA engine
220 * @mport: RapidIO master port control structure
221 * @index: ID of RapdiIO interface
222 * @destid: Destination ID of transaction
223 * @hopcount: Number of hops to target device
224 * @offset: Offset into configuration space
225 * @len: Length (in bytes) of the maintenance transaction
226 * @val: Value to be written
228 * Generates a RapidIO maintenance write transaction.
229 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
231 static int tsi721_cwrite_dma(struct rio_mport
*mport
, int index
, u16 destid
,
232 u8 hopcount
, u32 offset
, int len
, u32 data
)
234 struct tsi721_device
*priv
= mport
->priv
;
237 return tsi721_maint_dma(priv
, mport
->sys_size
, destid
, hopcount
,
238 offset
, len
, &temp
, 1);
242 * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
243 * @mport: RapidIO master port structure
245 * Handles inbound port-write interrupts. Copies PW message from an internal
246 * buffer into PW message FIFO and schedules deferred routine to process
250 tsi721_pw_handler(struct rio_mport
*mport
)
252 struct tsi721_device
*priv
= mport
->priv
;
254 u32 pw_buf
[TSI721_RIO_PW_MSG_SIZE
/sizeof(u32
)];
257 pw_stat
= ioread32(priv
->regs
+ TSI721_RIO_PW_RX_STAT
);
259 if (pw_stat
& TSI721_RIO_PW_RX_STAT_PW_VAL
) {
260 pw_buf
[0] = ioread32(priv
->regs
+ TSI721_RIO_PW_RX_CAPT(0));
261 pw_buf
[1] = ioread32(priv
->regs
+ TSI721_RIO_PW_RX_CAPT(1));
262 pw_buf
[2] = ioread32(priv
->regs
+ TSI721_RIO_PW_RX_CAPT(2));
263 pw_buf
[3] = ioread32(priv
->regs
+ TSI721_RIO_PW_RX_CAPT(3));
265 /* Queue PW message (if there is room in FIFO),
266 * otherwise discard it.
268 spin_lock(&priv
->pw_fifo_lock
);
269 if (kfifo_avail(&priv
->pw_fifo
) >= TSI721_RIO_PW_MSG_SIZE
)
270 kfifo_in(&priv
->pw_fifo
, pw_buf
,
271 TSI721_RIO_PW_MSG_SIZE
);
273 priv
->pw_discard_count
++;
274 spin_unlock(&priv
->pw_fifo_lock
);
277 /* Clear pending PW interrupts */
278 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC
| TSI721_RIO_PW_RX_STAT_PW_VAL
,
279 priv
->regs
+ TSI721_RIO_PW_RX_STAT
);
281 schedule_work(&priv
->pw_work
);
286 static void tsi721_pw_dpc(struct work_struct
*work
)
288 struct tsi721_device
*priv
= container_of(work
, struct tsi721_device
,
290 u32 msg_buffer
[RIO_PW_MSG_SIZE
/sizeof(u32
)]; /* Use full size PW message
291 buffer for RIO layer */
294 * Process port-write messages
296 while (kfifo_out_spinlocked(&priv
->pw_fifo
, (unsigned char *)msg_buffer
,
297 TSI721_RIO_PW_MSG_SIZE
, &priv
->pw_fifo_lock
)) {
298 /* Process one message */
302 pr_debug("%s : Port-Write Message:", __func__
);
303 for (i
= 0; i
< RIO_PW_MSG_SIZE
/sizeof(u32
); ) {
304 pr_debug("0x%02x: %08x %08x %08x %08x", i
*4,
305 msg_buffer
[i
], msg_buffer
[i
+ 1],
306 msg_buffer
[i
+ 2], msg_buffer
[i
+ 3]);
312 /* Pass the port-write message to RIO core for processing */
313 rio_inb_pwrite_handler((union rio_pw_msg
*)msg_buffer
);
318 * tsi721_pw_enable - enable/disable port-write interface init
319 * @mport: Master port implementing the port write unit
320 * @enable: 1=enable; 0=disable port-write message handling
322 static int tsi721_pw_enable(struct rio_mport
*mport
, int enable
)
324 struct tsi721_device
*priv
= mport
->priv
;
327 rval
= ioread32(priv
->regs
+ TSI721_RIO_EM_INT_ENABLE
);
330 rval
|= TSI721_RIO_EM_INT_ENABLE_PW_RX
;
332 rval
&= ~TSI721_RIO_EM_INT_ENABLE_PW_RX
;
334 /* Clear pending PW interrupts */
335 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC
| TSI721_RIO_PW_RX_STAT_PW_VAL
,
336 priv
->regs
+ TSI721_RIO_PW_RX_STAT
);
337 /* Update enable bits */
338 iowrite32(rval
, priv
->regs
+ TSI721_RIO_EM_INT_ENABLE
);
344 * tsi721_dsend - Send a RapidIO doorbell
345 * @mport: RapidIO master port info
346 * @index: ID of RapidIO interface
347 * @destid: Destination ID of target device
348 * @data: 16-bit info field of RapidIO doorbell
350 * Sends a RapidIO doorbell message. Always returns %0.
352 static int tsi721_dsend(struct rio_mport
*mport
, int index
,
353 u16 destid
, u16 data
)
355 struct tsi721_device
*priv
= mport
->priv
;
358 offset
= (((mport
->sys_size
) ? RIO_TT_CODE_16
: RIO_TT_CODE_8
) << 18) |
361 dev_dbg(&priv
->pdev
->dev
,
362 "Send Doorbell 0x%04x to destID 0x%x\n", data
, destid
);
363 iowrite16be(data
, priv
->odb_base
+ offset
);
369 * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
370 * @mport: RapidIO master port structure
372 * Handles inbound doorbell interrupts. Copies doorbell entry from an internal
373 * buffer into DB message FIFO and schedules deferred routine to process
377 tsi721_dbell_handler(struct rio_mport
*mport
)
379 struct tsi721_device
*priv
= mport
->priv
;
382 /* Disable IDB interrupts */
383 regval
= ioread32(priv
->regs
+ TSI721_SR_CHINTE(IDB_QUEUE
));
384 regval
&= ~TSI721_SR_CHINT_IDBQRCV
;
386 priv
->regs
+ TSI721_SR_CHINTE(IDB_QUEUE
));
388 schedule_work(&priv
->idb_work
);
393 static void tsi721_db_dpc(struct work_struct
*work
)
395 struct tsi721_device
*priv
= container_of(work
, struct tsi721_device
,
397 struct rio_mport
*mport
;
398 struct rio_dbell
*dbell
;
409 * Process queued inbound doorbells
413 wr_ptr
= ioread32(priv
->regs
+ TSI721_IDQ_WP(IDB_QUEUE
)) % IDB_QSIZE
;
414 rd_ptr
= ioread32(priv
->regs
+ TSI721_IDQ_RP(IDB_QUEUE
)) % IDB_QSIZE
;
416 while (wr_ptr
!= rd_ptr
) {
417 idb_entry
= (u64
*)(priv
->idb_base
+
418 (TSI721_IDB_ENTRY_SIZE
* rd_ptr
));
421 idb
.msg
= *idb_entry
;
424 /* Process one doorbell */
425 list_for_each_entry(dbell
, &mport
->dbells
, node
) {
426 if ((dbell
->res
->start
<= DBELL_INF(idb
.bytes
)) &&
427 (dbell
->res
->end
>= DBELL_INF(idb
.bytes
))) {
434 dbell
->dinb(mport
, dbell
->dev_id
, DBELL_SID(idb
.bytes
),
435 DBELL_TID(idb
.bytes
), DBELL_INF(idb
.bytes
));
437 dev_dbg(&priv
->pdev
->dev
,
438 "spurious inb doorbell, sid %2.2x tid %2.2x"
439 " info %4.4x\n", DBELL_SID(idb
.bytes
),
440 DBELL_TID(idb
.bytes
), DBELL_INF(idb
.bytes
));
444 iowrite32(rd_ptr
& (IDB_QSIZE
- 1),
445 priv
->regs
+ TSI721_IDQ_RP(IDB_QUEUE
));
447 /* Re-enable IDB interrupts */
448 regval
= ioread32(priv
->regs
+ TSI721_SR_CHINTE(IDB_QUEUE
));
449 regval
|= TSI721_SR_CHINT_IDBQRCV
;
451 priv
->regs
+ TSI721_SR_CHINTE(IDB_QUEUE
));
455 * tsi721_irqhandler - Tsi721 interrupt handler
456 * @irq: Linux interrupt number
457 * @ptr: Pointer to interrupt-specific data (mport structure)
459 * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
460 * interrupt events and calls an event-specific handler(s).
462 static irqreturn_t
tsi721_irqhandler(int irq
, void *ptr
)
464 struct rio_mport
*mport
= (struct rio_mport
*)ptr
;
465 struct tsi721_device
*priv
= mport
->priv
;
471 dev_int
= ioread32(priv
->regs
+ TSI721_DEV_INT
);
475 dev_ch_int
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INT
);
477 if (dev_int
& TSI721_DEV_INT_SR2PC_CH
) {
478 /* Service SR2PC Channel interrupts */
479 if (dev_ch_int
& TSI721_INT_SR2PC_CHAN(IDB_QUEUE
)) {
480 /* Service Inbound Doorbell interrupt */
481 intval
= ioread32(priv
->regs
+
482 TSI721_SR_CHINT(IDB_QUEUE
));
483 if (intval
& TSI721_SR_CHINT_IDBQRCV
)
484 tsi721_dbell_handler(mport
);
486 dev_info(&priv
->pdev
->dev
,
487 "Unsupported SR_CH_INT %x\n", intval
);
489 /* Clear interrupts */
491 priv
->regs
+ TSI721_SR_CHINT(IDB_QUEUE
));
492 ioread32(priv
->regs
+ TSI721_SR_CHINT(IDB_QUEUE
));
496 if (dev_int
& TSI721_DEV_INT_SMSG_CH
) {
500 * Service channel interrupts from Messaging Engine
503 if (dev_ch_int
& TSI721_INT_IMSG_CHAN_M
) { /* Inbound Msg */
504 /* Disable signaled OB MSG Channel interrupts */
505 ch_inte
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
506 ch_inte
&= ~(dev_ch_int
& TSI721_INT_IMSG_CHAN_M
);
507 iowrite32(ch_inte
, priv
->regs
+ TSI721_DEV_CHAN_INTE
);
510 * Process Inbound Message interrupt for each MBOX
512 for (ch
= 4; ch
< RIO_MAX_MBOX
+ 4; ch
++) {
513 if (!(dev_ch_int
& TSI721_INT_IMSG_CHAN(ch
)))
515 tsi721_imsg_handler(priv
, ch
);
519 if (dev_ch_int
& TSI721_INT_OMSG_CHAN_M
) { /* Outbound Msg */
520 /* Disable signaled OB MSG Channel interrupts */
521 ch_inte
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
522 ch_inte
&= ~(dev_ch_int
& TSI721_INT_OMSG_CHAN_M
);
523 iowrite32(ch_inte
, priv
->regs
+ TSI721_DEV_CHAN_INTE
);
526 * Process Outbound Message interrupts for each MBOX
529 for (ch
= 0; ch
< RIO_MAX_MBOX
; ch
++) {
530 if (!(dev_ch_int
& TSI721_INT_OMSG_CHAN(ch
)))
532 tsi721_omsg_handler(priv
, ch
);
537 if (dev_int
& TSI721_DEV_INT_SRIO
) {
538 /* Service SRIO MAC interrupts */
539 intval
= ioread32(priv
->regs
+ TSI721_RIO_EM_INT_STAT
);
540 if (intval
& TSI721_RIO_EM_INT_STAT_PW_RX
)
541 tsi721_pw_handler(mport
);
547 static void tsi721_interrupts_init(struct tsi721_device
*priv
)
551 /* Enable IDB interrupts */
552 iowrite32(TSI721_SR_CHINT_ALL
,
553 priv
->regs
+ TSI721_SR_CHINT(IDB_QUEUE
));
554 iowrite32(TSI721_SR_CHINT_IDBQRCV
,
555 priv
->regs
+ TSI721_SR_CHINTE(IDB_QUEUE
));
556 iowrite32(TSI721_INT_SR2PC_CHAN(IDB_QUEUE
),
557 priv
->regs
+ TSI721_DEV_CHAN_INTE
);
559 /* Enable SRIO MAC interrupts */
560 iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT
,
561 priv
->regs
+ TSI721_RIO_EM_DEV_INT_EN
);
563 if (priv
->flags
& TSI721_USING_MSIX
)
564 intr
= TSI721_DEV_INT_SRIO
;
566 intr
= TSI721_DEV_INT_SR2PC_CH
| TSI721_DEV_INT_SRIO
|
567 TSI721_DEV_INT_SMSG_CH
;
569 iowrite32(intr
, priv
->regs
+ TSI721_DEV_INTE
);
570 ioread32(priv
->regs
+ TSI721_DEV_INTE
);
573 #ifdef CONFIG_PCI_MSI
575 * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
576 * @irq: Linux interrupt number
577 * @ptr: Pointer to interrupt-specific data (mport structure)
579 * Handles outbound messaging interrupts signaled using MSI-X.
581 static irqreturn_t
tsi721_omsg_msix(int irq
, void *ptr
)
583 struct tsi721_device
*priv
= ((struct rio_mport
*)ptr
)->priv
;
586 mbox
= (irq
- priv
->msix
[TSI721_VECT_OMB0_DONE
].vector
) % RIO_MAX_MBOX
;
587 tsi721_omsg_handler(priv
, mbox
);
592 * tsi721_imsg_msix - MSI-X interrupt handler for inbound messaging
593 * @irq: Linux interrupt number
594 * @ptr: Pointer to interrupt-specific data (mport structure)
596 * Handles inbound messaging interrupts signaled using MSI-X.
598 static irqreturn_t
tsi721_imsg_msix(int irq
, void *ptr
)
600 struct tsi721_device
*priv
= ((struct rio_mport
*)ptr
)->priv
;
603 mbox
= (irq
- priv
->msix
[TSI721_VECT_IMB0_RCV
].vector
) % RIO_MAX_MBOX
;
604 tsi721_imsg_handler(priv
, mbox
+ 4);
609 * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
610 * @irq: Linux interrupt number
611 * @ptr: Pointer to interrupt-specific data (mport structure)
613 * Handles Tsi721 interrupts from SRIO MAC.
615 static irqreturn_t
tsi721_srio_msix(int irq
, void *ptr
)
617 struct tsi721_device
*priv
= ((struct rio_mport
*)ptr
)->priv
;
620 /* Service SRIO MAC interrupts */
621 srio_int
= ioread32(priv
->regs
+ TSI721_RIO_EM_INT_STAT
);
622 if (srio_int
& TSI721_RIO_EM_INT_STAT_PW_RX
)
623 tsi721_pw_handler((struct rio_mport
*)ptr
);
629 * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
630 * @irq: Linux interrupt number
631 * @ptr: Pointer to interrupt-specific data (mport structure)
633 * Handles Tsi721 interrupts from SR2PC Channel.
634 * NOTE: At this moment services only one SR2PC channel associated with inbound
637 static irqreturn_t
tsi721_sr2pc_ch_msix(int irq
, void *ptr
)
639 struct tsi721_device
*priv
= ((struct rio_mport
*)ptr
)->priv
;
642 /* Service Inbound DB interrupt from SR2PC channel */
643 sr_ch_int
= ioread32(priv
->regs
+ TSI721_SR_CHINT(IDB_QUEUE
));
644 if (sr_ch_int
& TSI721_SR_CHINT_IDBQRCV
)
645 tsi721_dbell_handler((struct rio_mport
*)ptr
);
647 /* Clear interrupts */
648 iowrite32(sr_ch_int
, priv
->regs
+ TSI721_SR_CHINT(IDB_QUEUE
));
649 /* Read back to ensure that interrupt was cleared */
650 sr_ch_int
= ioread32(priv
->regs
+ TSI721_SR_CHINT(IDB_QUEUE
));
656 * tsi721_request_msix - register interrupt service for MSI-X mode.
657 * @mport: RapidIO master port structure
659 * Registers MSI-X interrupt service routines for interrupts that are active
660 * immediately after mport initialization. Messaging interrupt service routines
661 * should be registered during corresponding open requests.
663 static int tsi721_request_msix(struct rio_mport
*mport
)
665 struct tsi721_device
*priv
= mport
->priv
;
668 err
= request_irq(priv
->msix
[TSI721_VECT_IDB
].vector
,
669 tsi721_sr2pc_ch_msix
, 0,
670 priv
->msix
[TSI721_VECT_IDB
].irq_name
, (void *)mport
);
674 err
= request_irq(priv
->msix
[TSI721_VECT_PWRX
].vector
,
676 priv
->msix
[TSI721_VECT_PWRX
].irq_name
, (void *)mport
);
679 priv
->msix
[TSI721_VECT_IDB
].vector
,
686 * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
687 * @priv: pointer to tsi721 private data
689 * Configures MSI-X support for Tsi721. Supports only an exact number
690 * of requested vectors.
692 static int tsi721_enable_msix(struct tsi721_device
*priv
)
694 struct msix_entry entries
[TSI721_VECT_MAX
];
698 entries
[TSI721_VECT_IDB
].entry
= TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE
);
699 entries
[TSI721_VECT_PWRX
].entry
= TSI721_MSIX_SRIO_MAC_INT
;
702 * Initialize MSI-X entries for Messaging Engine:
703 * this driver supports four RIO mailboxes (inbound and outbound)
704 * NOTE: Inbound message MBOX 0...4 use IB channels 4...7. Therefore
705 * offset +4 is added to IB MBOX number.
707 for (i
= 0; i
< RIO_MAX_MBOX
; i
++) {
708 entries
[TSI721_VECT_IMB0_RCV
+ i
].entry
=
709 TSI721_MSIX_IMSG_DQ_RCV(i
+ 4);
710 entries
[TSI721_VECT_IMB0_INT
+ i
].entry
=
711 TSI721_MSIX_IMSG_INT(i
+ 4);
712 entries
[TSI721_VECT_OMB0_DONE
+ i
].entry
=
713 TSI721_MSIX_OMSG_DONE(i
);
714 entries
[TSI721_VECT_OMB0_INT
+ i
].entry
=
715 TSI721_MSIX_OMSG_INT(i
);
718 err
= pci_enable_msix(priv
->pdev
, entries
, ARRAY_SIZE(entries
));
721 dev_info(&priv
->pdev
->dev
,
722 "Only %d MSI-X vectors available, "
723 "not using MSI-X\n", err
);
728 * Copy MSI-X vector information into tsi721 private structure
730 priv
->msix
[TSI721_VECT_IDB
].vector
= entries
[TSI721_VECT_IDB
].vector
;
731 snprintf(priv
->msix
[TSI721_VECT_IDB
].irq_name
, IRQ_DEVICE_NAME_MAX
,
732 DRV_NAME
"-idb@pci:%s", pci_name(priv
->pdev
));
733 priv
->msix
[TSI721_VECT_PWRX
].vector
= entries
[TSI721_VECT_PWRX
].vector
;
734 snprintf(priv
->msix
[TSI721_VECT_PWRX
].irq_name
, IRQ_DEVICE_NAME_MAX
,
735 DRV_NAME
"-pwrx@pci:%s", pci_name(priv
->pdev
));
737 for (i
= 0; i
< RIO_MAX_MBOX
; i
++) {
738 priv
->msix
[TSI721_VECT_IMB0_RCV
+ i
].vector
=
739 entries
[TSI721_VECT_IMB0_RCV
+ i
].vector
;
740 snprintf(priv
->msix
[TSI721_VECT_IMB0_RCV
+ i
].irq_name
,
741 IRQ_DEVICE_NAME_MAX
, DRV_NAME
"-imbr%d@pci:%s",
742 i
, pci_name(priv
->pdev
));
744 priv
->msix
[TSI721_VECT_IMB0_INT
+ i
].vector
=
745 entries
[TSI721_VECT_IMB0_INT
+ i
].vector
;
746 snprintf(priv
->msix
[TSI721_VECT_IMB0_INT
+ i
].irq_name
,
747 IRQ_DEVICE_NAME_MAX
, DRV_NAME
"-imbi%d@pci:%s",
748 i
, pci_name(priv
->pdev
));
750 priv
->msix
[TSI721_VECT_OMB0_DONE
+ i
].vector
=
751 entries
[TSI721_VECT_OMB0_DONE
+ i
].vector
;
752 snprintf(priv
->msix
[TSI721_VECT_OMB0_DONE
+ i
].irq_name
,
753 IRQ_DEVICE_NAME_MAX
, DRV_NAME
"-ombd%d@pci:%s",
754 i
, pci_name(priv
->pdev
));
756 priv
->msix
[TSI721_VECT_OMB0_INT
+ i
].vector
=
757 entries
[TSI721_VECT_OMB0_INT
+ i
].vector
;
758 snprintf(priv
->msix
[TSI721_VECT_OMB0_INT
+ i
].irq_name
,
759 IRQ_DEVICE_NAME_MAX
, DRV_NAME
"-ombi%d@pci:%s",
760 i
, pci_name(priv
->pdev
));
765 #endif /* CONFIG_PCI_MSI */
767 static int tsi721_request_irq(struct rio_mport
*mport
)
769 struct tsi721_device
*priv
= mport
->priv
;
772 #ifdef CONFIG_PCI_MSI
773 if (priv
->flags
& TSI721_USING_MSIX
)
774 err
= tsi721_request_msix(mport
);
777 err
= request_irq(priv
->pdev
->irq
, tsi721_irqhandler
,
778 (priv
->flags
& TSI721_USING_MSI
) ? 0 : IRQF_SHARED
,
779 DRV_NAME
, (void *)mport
);
782 dev_err(&priv
->pdev
->dev
,
783 "Unable to allocate interrupt, Error: %d\n", err
);
789 * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
790 * translation regions.
791 * @priv: pointer to tsi721 private data
793 * Disables SREP translation regions.
795 static void tsi721_init_pc2sr_mapping(struct tsi721_device
*priv
)
799 /* Disable all PC2SR translation windows */
800 for (i
= 0; i
< TSI721_OBWIN_NUM
; i
++)
801 iowrite32(0, priv
->regs
+ TSI721_OBWINLB(i
));
805 * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
806 * translation regions.
807 * @priv: pointer to tsi721 private data
809 * Disables inbound windows.
811 static void tsi721_init_sr2pc_mapping(struct tsi721_device
*priv
)
815 /* Disable all SR2PC inbound windows */
816 for (i
= 0; i
< TSI721_IBWIN_NUM
; i
++)
817 iowrite32(0, priv
->regs
+ TSI721_IBWINLB(i
));
821 * tsi721_port_write_init - Inbound port write interface init
822 * @priv: pointer to tsi721 private data
824 * Initializes inbound port write handler.
825 * Returns %0 on success or %-ENOMEM on failure.
827 static int tsi721_port_write_init(struct tsi721_device
*priv
)
829 priv
->pw_discard_count
= 0;
830 INIT_WORK(&priv
->pw_work
, tsi721_pw_dpc
);
831 spin_lock_init(&priv
->pw_fifo_lock
);
832 if (kfifo_alloc(&priv
->pw_fifo
,
833 TSI721_RIO_PW_MSG_SIZE
* 32, GFP_KERNEL
)) {
834 dev_err(&priv
->pdev
->dev
, "PW FIFO allocation failed\n");
838 /* Use reliable port-write capture mode */
839 iowrite32(TSI721_RIO_PW_CTL_PWC_REL
, priv
->regs
+ TSI721_RIO_PW_CTL
);
843 static int tsi721_doorbell_init(struct tsi721_device
*priv
)
845 /* Outbound Doorbells do not require any setup.
846 * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
847 * That BAR1 was mapped during the probe routine.
850 /* Initialize Inbound Doorbell processing DPC and queue */
851 priv
->db_discard_count
= 0;
852 INIT_WORK(&priv
->idb_work
, tsi721_db_dpc
);
854 /* Allocate buffer for inbound doorbells queue */
855 priv
->idb_base
= dma_zalloc_coherent(&priv
->pdev
->dev
,
856 IDB_QSIZE
* TSI721_IDB_ENTRY_SIZE
,
857 &priv
->idb_dma
, GFP_KERNEL
);
861 dev_dbg(&priv
->pdev
->dev
, "Allocated IDB buffer @ %p (phys = %llx)\n",
862 priv
->idb_base
, (unsigned long long)priv
->idb_dma
);
864 iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE
),
865 priv
->regs
+ TSI721_IDQ_SIZE(IDB_QUEUE
));
866 iowrite32(((u64
)priv
->idb_dma
>> 32),
867 priv
->regs
+ TSI721_IDQ_BASEU(IDB_QUEUE
));
868 iowrite32(((u64
)priv
->idb_dma
& TSI721_IDQ_BASEL_ADDR
),
869 priv
->regs
+ TSI721_IDQ_BASEL(IDB_QUEUE
));
870 /* Enable accepting all inbound doorbells */
871 iowrite32(0, priv
->regs
+ TSI721_IDQ_MASK(IDB_QUEUE
));
873 iowrite32(TSI721_IDQ_INIT
, priv
->regs
+ TSI721_IDQ_CTL(IDB_QUEUE
));
875 iowrite32(0, priv
->regs
+ TSI721_IDQ_RP(IDB_QUEUE
));
880 static void tsi721_doorbell_free(struct tsi721_device
*priv
)
882 if (priv
->idb_base
== NULL
)
885 /* Free buffer allocated for inbound doorbell queue */
886 dma_free_coherent(&priv
->pdev
->dev
, IDB_QSIZE
* TSI721_IDB_ENTRY_SIZE
,
887 priv
->idb_base
, priv
->idb_dma
);
888 priv
->idb_base
= NULL
;
891 static int tsi721_bdma_ch_init(struct tsi721_device
*priv
, int chnum
)
893 struct tsi721_dma_desc
*bd_ptr
;
895 dma_addr_t bd_phys
, sts_phys
;
897 int bd_num
= priv
->bdma
[chnum
].bd_num
;
899 dev_dbg(&priv
->pdev
->dev
, "Init Block DMA Engine, CH%d\n", chnum
);
902 * Initialize DMA channel for maintenance requests
905 /* Allocate space for DMA descriptors */
906 bd_ptr
= dma_zalloc_coherent(&priv
->pdev
->dev
,
907 bd_num
* sizeof(struct tsi721_dma_desc
),
908 &bd_phys
, GFP_KERNEL
);
912 priv
->bdma
[chnum
].bd_phys
= bd_phys
;
913 priv
->bdma
[chnum
].bd_base
= bd_ptr
;
915 dev_dbg(&priv
->pdev
->dev
, "DMA descriptors @ %p (phys = %llx)\n",
916 bd_ptr
, (unsigned long long)bd_phys
);
918 /* Allocate space for descriptor status FIFO */
919 sts_size
= (bd_num
>= TSI721_DMA_MINSTSSZ
) ?
920 bd_num
: TSI721_DMA_MINSTSSZ
;
921 sts_size
= roundup_pow_of_two(sts_size
);
922 sts_ptr
= dma_zalloc_coherent(&priv
->pdev
->dev
,
923 sts_size
* sizeof(struct tsi721_dma_sts
),
924 &sts_phys
, GFP_KERNEL
);
926 /* Free space allocated for DMA descriptors */
927 dma_free_coherent(&priv
->pdev
->dev
,
928 bd_num
* sizeof(struct tsi721_dma_desc
),
930 priv
->bdma
[chnum
].bd_base
= NULL
;
934 priv
->bdma
[chnum
].sts_phys
= sts_phys
;
935 priv
->bdma
[chnum
].sts_base
= sts_ptr
;
936 priv
->bdma
[chnum
].sts_size
= sts_size
;
938 dev_dbg(&priv
->pdev
->dev
,
939 "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
940 sts_ptr
, (unsigned long long)sts_phys
, sts_size
);
942 /* Initialize DMA descriptors ring */
943 bd_ptr
[bd_num
- 1].type_id
= cpu_to_le32(DTYPE3
<< 29);
944 bd_ptr
[bd_num
- 1].next_lo
= cpu_to_le32((u64
)bd_phys
&
945 TSI721_DMAC_DPTRL_MASK
);
946 bd_ptr
[bd_num
- 1].next_hi
= cpu_to_le32((u64
)bd_phys
>> 32);
948 /* Setup DMA descriptor pointers */
949 iowrite32(((u64
)bd_phys
>> 32),
950 priv
->regs
+ TSI721_DMAC_DPTRH(chnum
));
951 iowrite32(((u64
)bd_phys
& TSI721_DMAC_DPTRL_MASK
),
952 priv
->regs
+ TSI721_DMAC_DPTRL(chnum
));
954 /* Setup descriptor status FIFO */
955 iowrite32(((u64
)sts_phys
>> 32),
956 priv
->regs
+ TSI721_DMAC_DSBH(chnum
));
957 iowrite32(((u64
)sts_phys
& TSI721_DMAC_DSBL_MASK
),
958 priv
->regs
+ TSI721_DMAC_DSBL(chnum
));
959 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size
),
960 priv
->regs
+ TSI721_DMAC_DSSZ(chnum
));
962 /* Clear interrupt bits */
963 iowrite32(TSI721_DMAC_INT_ALL
,
964 priv
->regs
+ TSI721_DMAC_INT(chnum
));
966 ioread32(priv
->regs
+ TSI721_DMAC_INT(chnum
));
968 /* Toggle DMA channel initialization */
969 iowrite32(TSI721_DMAC_CTL_INIT
, priv
->regs
+ TSI721_DMAC_CTL(chnum
));
970 ioread32(priv
->regs
+ TSI721_DMAC_CTL(chnum
));
976 static int tsi721_bdma_ch_free(struct tsi721_device
*priv
, int chnum
)
980 if (priv
->bdma
[chnum
].bd_base
== NULL
)
983 /* Check if DMA channel still running */
984 ch_stat
= ioread32(priv
->regs
+ TSI721_DMAC_STS(chnum
));
985 if (ch_stat
& TSI721_DMAC_STS_RUN
)
988 /* Put DMA channel into init state */
989 iowrite32(TSI721_DMAC_CTL_INIT
,
990 priv
->regs
+ TSI721_DMAC_CTL(chnum
));
992 /* Free space allocated for DMA descriptors */
993 dma_free_coherent(&priv
->pdev
->dev
,
994 priv
->bdma
[chnum
].bd_num
* sizeof(struct tsi721_dma_desc
),
995 priv
->bdma
[chnum
].bd_base
, priv
->bdma
[chnum
].bd_phys
);
996 priv
->bdma
[chnum
].bd_base
= NULL
;
998 /* Free space allocated for status FIFO */
999 dma_free_coherent(&priv
->pdev
->dev
,
1000 priv
->bdma
[chnum
].sts_size
* sizeof(struct tsi721_dma_sts
),
1001 priv
->bdma
[chnum
].sts_base
, priv
->bdma
[chnum
].sts_phys
);
1002 priv
->bdma
[chnum
].sts_base
= NULL
;
1006 static int tsi721_bdma_init(struct tsi721_device
*priv
)
1008 /* Initialize BDMA channel allocated for RapidIO maintenance read/write
1009 * request generation
1011 priv
->bdma
[TSI721_DMACH_MAINT
].bd_num
= 2;
1012 if (tsi721_bdma_ch_init(priv
, TSI721_DMACH_MAINT
)) {
1013 dev_err(&priv
->pdev
->dev
, "Unable to initialize maintenance DMA"
1014 " channel %d, aborting\n", TSI721_DMACH_MAINT
);
1021 static void tsi721_bdma_free(struct tsi721_device
*priv
)
1023 tsi721_bdma_ch_free(priv
, TSI721_DMACH_MAINT
);
1026 /* Enable Inbound Messaging Interrupts */
1028 tsi721_imsg_interrupt_enable(struct tsi721_device
*priv
, int ch
,
1036 /* Clear pending Inbound Messaging interrupts */
1037 iowrite32(inte_mask
, priv
->regs
+ TSI721_IBDMAC_INT(ch
));
1039 /* Enable Inbound Messaging interrupts */
1040 rval
= ioread32(priv
->regs
+ TSI721_IBDMAC_INTE(ch
));
1041 iowrite32(rval
| inte_mask
, priv
->regs
+ TSI721_IBDMAC_INTE(ch
));
1043 if (priv
->flags
& TSI721_USING_MSIX
)
1044 return; /* Finished if we are in MSI-X mode */
1047 * For MSI and INTA interrupt signalling we need to enable next levels
1050 /* Enable Device Channel Interrupt */
1051 rval
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1052 iowrite32(rval
| TSI721_INT_IMSG_CHAN(ch
),
1053 priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1056 /* Disable Inbound Messaging Interrupts */
1058 tsi721_imsg_interrupt_disable(struct tsi721_device
*priv
, int ch
,
1066 /* Clear pending Inbound Messaging interrupts */
1067 iowrite32(inte_mask
, priv
->regs
+ TSI721_IBDMAC_INT(ch
));
1069 /* Disable Inbound Messaging interrupts */
1070 rval
= ioread32(priv
->regs
+ TSI721_IBDMAC_INTE(ch
));
1072 iowrite32(rval
, priv
->regs
+ TSI721_IBDMAC_INTE(ch
));
1074 if (priv
->flags
& TSI721_USING_MSIX
)
1075 return; /* Finished if we are in MSI-X mode */
1078 * For MSI and INTA interrupt signalling we need to disable next levels
1081 /* Disable Device Channel Interrupt */
1082 rval
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1083 rval
&= ~TSI721_INT_IMSG_CHAN(ch
);
1084 iowrite32(rval
, priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1087 /* Enable Outbound Messaging interrupts */
1089 tsi721_omsg_interrupt_enable(struct tsi721_device
*priv
, int ch
,
1097 /* Clear pending Outbound Messaging interrupts */
1098 iowrite32(inte_mask
, priv
->regs
+ TSI721_OBDMAC_INT(ch
));
1100 /* Enable Outbound Messaging channel interrupts */
1101 rval
= ioread32(priv
->regs
+ TSI721_OBDMAC_INTE(ch
));
1102 iowrite32(rval
| inte_mask
, priv
->regs
+ TSI721_OBDMAC_INTE(ch
));
1104 if (priv
->flags
& TSI721_USING_MSIX
)
1105 return; /* Finished if we are in MSI-X mode */
1108 * For MSI and INTA interrupt signalling we need to enable next levels
1111 /* Enable Device Channel Interrupt */
1112 rval
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1113 iowrite32(rval
| TSI721_INT_OMSG_CHAN(ch
),
1114 priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1117 /* Disable Outbound Messaging interrupts */
1119 tsi721_omsg_interrupt_disable(struct tsi721_device
*priv
, int ch
,
1127 /* Clear pending Outbound Messaging interrupts */
1128 iowrite32(inte_mask
, priv
->regs
+ TSI721_OBDMAC_INT(ch
));
1130 /* Disable Outbound Messaging interrupts */
1131 rval
= ioread32(priv
->regs
+ TSI721_OBDMAC_INTE(ch
));
1133 iowrite32(rval
, priv
->regs
+ TSI721_OBDMAC_INTE(ch
));
1135 if (priv
->flags
& TSI721_USING_MSIX
)
1136 return; /* Finished if we are in MSI-X mode */
1139 * For MSI and INTA interrupt signalling we need to disable next levels
1142 /* Disable Device Channel Interrupt */
1143 rval
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1144 rval
&= ~TSI721_INT_OMSG_CHAN(ch
);
1145 iowrite32(rval
, priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1149 * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
1150 * @mport: Master port with outbound message queue
1151 * @rdev: Target of outbound message
1152 * @mbox: Outbound mailbox
1153 * @buffer: Message to add to outbound queue
1154 * @len: Length of message
1157 tsi721_add_outb_message(struct rio_mport
*mport
, struct rio_dev
*rdev
, int mbox
,
1158 void *buffer
, size_t len
)
1160 struct tsi721_device
*priv
= mport
->priv
;
1161 struct tsi721_omsg_desc
*desc
;
1164 if (!priv
->omsg_init
[mbox
] ||
1165 len
> TSI721_MSG_MAX_SIZE
|| len
< 8)
1168 tx_slot
= priv
->omsg_ring
[mbox
].tx_slot
;
1170 /* Copy copy message into transfer buffer */
1171 memcpy(priv
->omsg_ring
[mbox
].omq_base
[tx_slot
], buffer
, len
);
1176 /* Build descriptor associated with buffer */
1177 desc
= priv
->omsg_ring
[mbox
].omd_base
;
1178 desc
[tx_slot
].type_id
= cpu_to_le32((DTYPE4
<< 29) | rdev
->destid
);
1179 if (tx_slot
% 4 == 0)
1180 desc
[tx_slot
].type_id
|= cpu_to_le32(TSI721_OMD_IOF
);
1182 desc
[tx_slot
].msg_info
=
1183 cpu_to_le32((mport
->sys_size
<< 26) | (mbox
<< 22) |
1184 (0xe << 12) | (len
& 0xff8));
1185 desc
[tx_slot
].bufptr_lo
=
1186 cpu_to_le32((u64
)priv
->omsg_ring
[mbox
].omq_phys
[tx_slot
] &
1188 desc
[tx_slot
].bufptr_hi
=
1189 cpu_to_le32((u64
)priv
->omsg_ring
[mbox
].omq_phys
[tx_slot
] >> 32);
1191 priv
->omsg_ring
[mbox
].wr_count
++;
1193 /* Go to next descriptor */
1194 if (++priv
->omsg_ring
[mbox
].tx_slot
== priv
->omsg_ring
[mbox
].size
) {
1195 priv
->omsg_ring
[mbox
].tx_slot
= 0;
1196 /* Move through the ring link descriptor at the end */
1197 priv
->omsg_ring
[mbox
].wr_count
++;
1202 /* Set new write count value */
1203 iowrite32(priv
->omsg_ring
[mbox
].wr_count
,
1204 priv
->regs
+ TSI721_OBDMAC_DWRCNT(mbox
));
1205 ioread32(priv
->regs
+ TSI721_OBDMAC_DWRCNT(mbox
));
1211 * tsi721_omsg_handler - Outbound Message Interrupt Handler
1212 * @priv: pointer to tsi721 private data
1213 * @ch: number of OB MSG channel to service
1215 * Services channel interrupts from outbound messaging engine.
1217 static void tsi721_omsg_handler(struct tsi721_device
*priv
, int ch
)
1221 spin_lock(&priv
->omsg_ring
[ch
].lock
);
1223 omsg_int
= ioread32(priv
->regs
+ TSI721_OBDMAC_INT(ch
));
1225 if (omsg_int
& TSI721_OBDMAC_INT_ST_FULL
)
1226 dev_info(&priv
->pdev
->dev
,
1227 "OB MBOX%d: Status FIFO is full\n", ch
);
1229 if (omsg_int
& (TSI721_OBDMAC_INT_DONE
| TSI721_OBDMAC_INT_IOF_DONE
)) {
1231 u64
*sts_ptr
, last_ptr
= 0, prev_ptr
= 0;
1236 * Find last successfully processed descriptor
1239 /* Check and clear descriptor status FIFO entries */
1240 srd_ptr
= priv
->omsg_ring
[ch
].sts_rdptr
;
1241 sts_ptr
= priv
->omsg_ring
[ch
].sts_base
;
1243 while (sts_ptr
[j
]) {
1244 for (i
= 0; i
< 8 && sts_ptr
[j
]; i
++, j
++) {
1245 prev_ptr
= last_ptr
;
1246 last_ptr
= le64_to_cpu(sts_ptr
[j
]);
1251 srd_ptr
%= priv
->omsg_ring
[ch
].sts_size
;
1258 priv
->omsg_ring
[ch
].sts_rdptr
= srd_ptr
;
1259 iowrite32(srd_ptr
, priv
->regs
+ TSI721_OBDMAC_DSRP(ch
));
1261 if (!priv
->mport
->outb_msg
[ch
].mcback
)
1264 /* Inform upper layer about transfer completion */
1266 tx_slot
= (last_ptr
- (u64
)priv
->omsg_ring
[ch
].omd_phys
)/
1267 sizeof(struct tsi721_omsg_desc
);
1270 * Check if this is a Link Descriptor (LD).
1271 * If yes, ignore LD and use descriptor processed
1274 if (tx_slot
== priv
->omsg_ring
[ch
].size
) {
1276 tx_slot
= (prev_ptr
-
1277 (u64
)priv
->omsg_ring
[ch
].omd_phys
)/
1278 sizeof(struct tsi721_omsg_desc
);
1283 /* Move slot index to the next message to be sent */
1285 if (tx_slot
== priv
->omsg_ring
[ch
].size
)
1287 BUG_ON(tx_slot
>= priv
->omsg_ring
[ch
].size
);
1288 priv
->mport
->outb_msg
[ch
].mcback(priv
->mport
,
1289 priv
->omsg_ring
[ch
].dev_id
, ch
,
1295 if (omsg_int
& TSI721_OBDMAC_INT_ERROR
) {
1297 * Outbound message operation aborted due to error,
1298 * reinitialize OB MSG channel
1301 dev_dbg(&priv
->pdev
->dev
, "OB MSG ABORT ch_stat=%x\n",
1302 ioread32(priv
->regs
+ TSI721_OBDMAC_STS(ch
)));
1304 iowrite32(TSI721_OBDMAC_INT_ERROR
,
1305 priv
->regs
+ TSI721_OBDMAC_INT(ch
));
1306 iowrite32(TSI721_OBDMAC_CTL_INIT
,
1307 priv
->regs
+ TSI721_OBDMAC_CTL(ch
));
1308 ioread32(priv
->regs
+ TSI721_OBDMAC_CTL(ch
));
1310 /* Inform upper level to clear all pending tx slots */
1311 if (priv
->mport
->outb_msg
[ch
].mcback
)
1312 priv
->mport
->outb_msg
[ch
].mcback(priv
->mport
,
1313 priv
->omsg_ring
[ch
].dev_id
, ch
,
1314 priv
->omsg_ring
[ch
].tx_slot
);
1315 /* Synch tx_slot tracking */
1316 iowrite32(priv
->omsg_ring
[ch
].tx_slot
,
1317 priv
->regs
+ TSI721_OBDMAC_DRDCNT(ch
));
1318 ioread32(priv
->regs
+ TSI721_OBDMAC_DRDCNT(ch
));
1319 priv
->omsg_ring
[ch
].wr_count
= priv
->omsg_ring
[ch
].tx_slot
;
1320 priv
->omsg_ring
[ch
].sts_rdptr
= 0;
1323 /* Clear channel interrupts */
1324 iowrite32(omsg_int
, priv
->regs
+ TSI721_OBDMAC_INT(ch
));
1326 if (!(priv
->flags
& TSI721_USING_MSIX
)) {
1329 /* Re-enable channel interrupts */
1330 ch_inte
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1331 ch_inte
|= TSI721_INT_OMSG_CHAN(ch
);
1332 iowrite32(ch_inte
, priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1335 spin_unlock(&priv
->omsg_ring
[ch
].lock
);
1339 * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
1340 * @mport: Master port implementing Outbound Messaging Engine
1341 * @dev_id: Device specific pointer to pass on event
1342 * @mbox: Mailbox to open
1343 * @entries: Number of entries in the outbound mailbox ring
1345 static int tsi721_open_outb_mbox(struct rio_mport
*mport
, void *dev_id
,
1346 int mbox
, int entries
)
1348 struct tsi721_device
*priv
= mport
->priv
;
1349 struct tsi721_omsg_desc
*bd_ptr
;
1352 if ((entries
< TSI721_OMSGD_MIN_RING_SIZE
) ||
1353 (entries
> (TSI721_OMSGD_RING_SIZE
)) ||
1354 (!is_power_of_2(entries
)) || mbox
>= RIO_MAX_MBOX
) {
1359 priv
->omsg_ring
[mbox
].dev_id
= dev_id
;
1360 priv
->omsg_ring
[mbox
].size
= entries
;
1361 priv
->omsg_ring
[mbox
].sts_rdptr
= 0;
1362 spin_lock_init(&priv
->omsg_ring
[mbox
].lock
);
1364 /* Outbound Msg Buffer allocation based on
1365 the number of maximum descriptor entries */
1366 for (i
= 0; i
< entries
; i
++) {
1367 priv
->omsg_ring
[mbox
].omq_base
[i
] =
1369 &priv
->pdev
->dev
, TSI721_MSG_BUFFER_SIZE
,
1370 &priv
->omsg_ring
[mbox
].omq_phys
[i
],
1372 if (priv
->omsg_ring
[mbox
].omq_base
[i
] == NULL
) {
1373 dev_dbg(&priv
->pdev
->dev
,
1374 "Unable to allocate OB MSG data buffer for"
1381 /* Outbound message descriptor allocation */
1382 priv
->omsg_ring
[mbox
].omd_base
= dma_alloc_coherent(
1384 (entries
+ 1) * sizeof(struct tsi721_omsg_desc
),
1385 &priv
->omsg_ring
[mbox
].omd_phys
, GFP_KERNEL
);
1386 if (priv
->omsg_ring
[mbox
].omd_base
== NULL
) {
1387 dev_dbg(&priv
->pdev
->dev
,
1388 "Unable to allocate OB MSG descriptor memory "
1389 "for MBOX%d\n", mbox
);
1394 priv
->omsg_ring
[mbox
].tx_slot
= 0;
1396 /* Outbound message descriptor status FIFO allocation */
1397 priv
->omsg_ring
[mbox
].sts_size
= roundup_pow_of_two(entries
+ 1);
1398 priv
->omsg_ring
[mbox
].sts_base
= dma_zalloc_coherent(&priv
->pdev
->dev
,
1399 priv
->omsg_ring
[mbox
].sts_size
*
1400 sizeof(struct tsi721_dma_sts
),
1401 &priv
->omsg_ring
[mbox
].sts_phys
, GFP_KERNEL
);
1402 if (priv
->omsg_ring
[mbox
].sts_base
== NULL
) {
1403 dev_dbg(&priv
->pdev
->dev
,
1404 "Unable to allocate OB MSG descriptor status FIFO "
1405 "for MBOX%d\n", mbox
);
1411 * Configure Outbound Messaging Engine
1414 /* Setup Outbound Message descriptor pointer */
1415 iowrite32(((u64
)priv
->omsg_ring
[mbox
].omd_phys
>> 32),
1416 priv
->regs
+ TSI721_OBDMAC_DPTRH(mbox
));
1417 iowrite32(((u64
)priv
->omsg_ring
[mbox
].omd_phys
&
1418 TSI721_OBDMAC_DPTRL_MASK
),
1419 priv
->regs
+ TSI721_OBDMAC_DPTRL(mbox
));
1421 /* Setup Outbound Message descriptor status FIFO */
1422 iowrite32(((u64
)priv
->omsg_ring
[mbox
].sts_phys
>> 32),
1423 priv
->regs
+ TSI721_OBDMAC_DSBH(mbox
));
1424 iowrite32(((u64
)priv
->omsg_ring
[mbox
].sts_phys
&
1425 TSI721_OBDMAC_DSBL_MASK
),
1426 priv
->regs
+ TSI721_OBDMAC_DSBL(mbox
));
1427 iowrite32(TSI721_DMAC_DSSZ_SIZE(priv
->omsg_ring
[mbox
].sts_size
),
1428 priv
->regs
+ (u32
)TSI721_OBDMAC_DSSZ(mbox
));
1430 /* Enable interrupts */
1432 #ifdef CONFIG_PCI_MSI
1433 if (priv
->flags
& TSI721_USING_MSIX
) {
1434 /* Request interrupt service if we are in MSI-X mode */
1436 priv
->msix
[TSI721_VECT_OMB0_DONE
+ mbox
].vector
,
1437 tsi721_omsg_msix
, 0,
1438 priv
->msix
[TSI721_VECT_OMB0_DONE
+ mbox
].irq_name
,
1442 dev_dbg(&priv
->pdev
->dev
,
1443 "Unable to allocate MSI-X interrupt for "
1444 "OBOX%d-DONE\n", mbox
);
1448 rc
= request_irq(priv
->msix
[TSI721_VECT_OMB0_INT
+ mbox
].vector
,
1449 tsi721_omsg_msix
, 0,
1450 priv
->msix
[TSI721_VECT_OMB0_INT
+ mbox
].irq_name
,
1454 dev_dbg(&priv
->pdev
->dev
,
1455 "Unable to allocate MSI-X interrupt for "
1456 "MBOX%d-INT\n", mbox
);
1458 priv
->msix
[TSI721_VECT_OMB0_DONE
+ mbox
].vector
,
1463 #endif /* CONFIG_PCI_MSI */
1465 tsi721_omsg_interrupt_enable(priv
, mbox
, TSI721_OBDMAC_INT_ALL
);
1467 /* Initialize Outbound Message descriptors ring */
1468 bd_ptr
= priv
->omsg_ring
[mbox
].omd_base
;
1469 bd_ptr
[entries
].type_id
= cpu_to_le32(DTYPE5
<< 29);
1470 bd_ptr
[entries
].msg_info
= 0;
1471 bd_ptr
[entries
].next_lo
=
1472 cpu_to_le32((u64
)priv
->omsg_ring
[mbox
].omd_phys
&
1473 TSI721_OBDMAC_DPTRL_MASK
);
1474 bd_ptr
[entries
].next_hi
=
1475 cpu_to_le32((u64
)priv
->omsg_ring
[mbox
].omd_phys
>> 32);
1476 priv
->omsg_ring
[mbox
].wr_count
= 0;
1479 /* Initialize Outbound Message engine */
1480 iowrite32(TSI721_OBDMAC_CTL_INIT
, priv
->regs
+ TSI721_OBDMAC_CTL(mbox
));
1481 ioread32(priv
->regs
+ TSI721_OBDMAC_DWRCNT(mbox
));
1484 priv
->omsg_init
[mbox
] = 1;
1488 #ifdef CONFIG_PCI_MSI
1490 dma_free_coherent(&priv
->pdev
->dev
,
1491 priv
->omsg_ring
[mbox
].sts_size
* sizeof(struct tsi721_dma_sts
),
1492 priv
->omsg_ring
[mbox
].sts_base
,
1493 priv
->omsg_ring
[mbox
].sts_phys
);
1495 priv
->omsg_ring
[mbox
].sts_base
= NULL
;
1496 #endif /* CONFIG_PCI_MSI */
1499 dma_free_coherent(&priv
->pdev
->dev
,
1500 (entries
+ 1) * sizeof(struct tsi721_omsg_desc
),
1501 priv
->omsg_ring
[mbox
].omd_base
,
1502 priv
->omsg_ring
[mbox
].omd_phys
);
1504 priv
->omsg_ring
[mbox
].omd_base
= NULL
;
1507 for (i
= 0; i
< priv
->omsg_ring
[mbox
].size
; i
++) {
1508 if (priv
->omsg_ring
[mbox
].omq_base
[i
]) {
1509 dma_free_coherent(&priv
->pdev
->dev
,
1510 TSI721_MSG_BUFFER_SIZE
,
1511 priv
->omsg_ring
[mbox
].omq_base
[i
],
1512 priv
->omsg_ring
[mbox
].omq_phys
[i
]);
1514 priv
->omsg_ring
[mbox
].omq_base
[i
] = NULL
;
1523 * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
1524 * @mport: Master port implementing the outbound message unit
1525 * @mbox: Mailbox to close
1527 static void tsi721_close_outb_mbox(struct rio_mport
*mport
, int mbox
)
1529 struct tsi721_device
*priv
= mport
->priv
;
1532 if (!priv
->omsg_init
[mbox
])
1534 priv
->omsg_init
[mbox
] = 0;
1536 /* Disable Interrupts */
1538 tsi721_omsg_interrupt_disable(priv
, mbox
, TSI721_OBDMAC_INT_ALL
);
1540 #ifdef CONFIG_PCI_MSI
1541 if (priv
->flags
& TSI721_USING_MSIX
) {
1542 free_irq(priv
->msix
[TSI721_VECT_OMB0_DONE
+ mbox
].vector
,
1544 free_irq(priv
->msix
[TSI721_VECT_OMB0_INT
+ mbox
].vector
,
1547 #endif /* CONFIG_PCI_MSI */
1549 /* Free OMSG Descriptor Status FIFO */
1550 dma_free_coherent(&priv
->pdev
->dev
,
1551 priv
->omsg_ring
[mbox
].sts_size
* sizeof(struct tsi721_dma_sts
),
1552 priv
->omsg_ring
[mbox
].sts_base
,
1553 priv
->omsg_ring
[mbox
].sts_phys
);
1555 priv
->omsg_ring
[mbox
].sts_base
= NULL
;
1557 /* Free OMSG descriptors */
1558 dma_free_coherent(&priv
->pdev
->dev
,
1559 (priv
->omsg_ring
[mbox
].size
+ 1) *
1560 sizeof(struct tsi721_omsg_desc
),
1561 priv
->omsg_ring
[mbox
].omd_base
,
1562 priv
->omsg_ring
[mbox
].omd_phys
);
1564 priv
->omsg_ring
[mbox
].omd_base
= NULL
;
1566 /* Free message buffers */
1567 for (i
= 0; i
< priv
->omsg_ring
[mbox
].size
; i
++) {
1568 if (priv
->omsg_ring
[mbox
].omq_base
[i
]) {
1569 dma_free_coherent(&priv
->pdev
->dev
,
1570 TSI721_MSG_BUFFER_SIZE
,
1571 priv
->omsg_ring
[mbox
].omq_base
[i
],
1572 priv
->omsg_ring
[mbox
].omq_phys
[i
]);
1574 priv
->omsg_ring
[mbox
].omq_base
[i
] = NULL
;
1580 * tsi721_imsg_handler - Inbound Message Interrupt Handler
1581 * @priv: pointer to tsi721 private data
1582 * @ch: inbound message channel number to service
1584 * Services channel interrupts from inbound messaging engine.
1586 static void tsi721_imsg_handler(struct tsi721_device
*priv
, int ch
)
1591 spin_lock(&priv
->imsg_ring
[mbox
].lock
);
1593 imsg_int
= ioread32(priv
->regs
+ TSI721_IBDMAC_INT(ch
));
1595 if (imsg_int
& TSI721_IBDMAC_INT_SRTO
)
1596 dev_info(&priv
->pdev
->dev
, "IB MBOX%d SRIO timeout\n",
1599 if (imsg_int
& TSI721_IBDMAC_INT_PC_ERROR
)
1600 dev_info(&priv
->pdev
->dev
, "IB MBOX%d PCIe error\n",
1603 if (imsg_int
& TSI721_IBDMAC_INT_FQ_LOW
)
1604 dev_info(&priv
->pdev
->dev
,
1605 "IB MBOX%d IB free queue low\n", mbox
);
1607 /* Clear IB channel interrupts */
1608 iowrite32(imsg_int
, priv
->regs
+ TSI721_IBDMAC_INT(ch
));
1610 /* If an IB Msg is received notify the upper layer */
1611 if (imsg_int
& TSI721_IBDMAC_INT_DQ_RCV
&&
1612 priv
->mport
->inb_msg
[mbox
].mcback
)
1613 priv
->mport
->inb_msg
[mbox
].mcback(priv
->mport
,
1614 priv
->imsg_ring
[mbox
].dev_id
, mbox
, -1);
1616 if (!(priv
->flags
& TSI721_USING_MSIX
)) {
1619 /* Re-enable channel interrupts */
1620 ch_inte
= ioread32(priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1621 ch_inte
|= TSI721_INT_IMSG_CHAN(ch
);
1622 iowrite32(ch_inte
, priv
->regs
+ TSI721_DEV_CHAN_INTE
);
1625 spin_unlock(&priv
->imsg_ring
[mbox
].lock
);
1629 * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
1630 * @mport: Master port implementing the Inbound Messaging Engine
1631 * @dev_id: Device specific pointer to pass on event
1632 * @mbox: Mailbox to open
1633 * @entries: Number of entries in the inbound mailbox ring
1635 static int tsi721_open_inb_mbox(struct rio_mport
*mport
, void *dev_id
,
1636 int mbox
, int entries
)
1638 struct tsi721_device
*priv
= mport
->priv
;
1644 if ((entries
< TSI721_IMSGD_MIN_RING_SIZE
) ||
1645 (entries
> TSI721_IMSGD_RING_SIZE
) ||
1646 (!is_power_of_2(entries
)) || mbox
>= RIO_MAX_MBOX
) {
1651 /* Initialize IB Messaging Ring */
1652 priv
->imsg_ring
[mbox
].dev_id
= dev_id
;
1653 priv
->imsg_ring
[mbox
].size
= entries
;
1654 priv
->imsg_ring
[mbox
].rx_slot
= 0;
1655 priv
->imsg_ring
[mbox
].desc_rdptr
= 0;
1656 priv
->imsg_ring
[mbox
].fq_wrptr
= 0;
1657 for (i
= 0; i
< priv
->imsg_ring
[mbox
].size
; i
++)
1658 priv
->imsg_ring
[mbox
].imq_base
[i
] = NULL
;
1659 spin_lock_init(&priv
->imsg_ring
[mbox
].lock
);
1661 /* Allocate buffers for incoming messages */
1662 priv
->imsg_ring
[mbox
].buf_base
=
1663 dma_alloc_coherent(&priv
->pdev
->dev
,
1664 entries
* TSI721_MSG_BUFFER_SIZE
,
1665 &priv
->imsg_ring
[mbox
].buf_phys
,
1668 if (priv
->imsg_ring
[mbox
].buf_base
== NULL
) {
1669 dev_err(&priv
->pdev
->dev
,
1670 "Failed to allocate buffers for IB MBOX%d\n", mbox
);
1675 /* Allocate memory for circular free list */
1676 priv
->imsg_ring
[mbox
].imfq_base
=
1677 dma_alloc_coherent(&priv
->pdev
->dev
,
1679 &priv
->imsg_ring
[mbox
].imfq_phys
,
1682 if (priv
->imsg_ring
[mbox
].imfq_base
== NULL
) {
1683 dev_err(&priv
->pdev
->dev
,
1684 "Failed to allocate free queue for IB MBOX%d\n", mbox
);
1689 /* Allocate memory for Inbound message descriptors */
1690 priv
->imsg_ring
[mbox
].imd_base
=
1691 dma_alloc_coherent(&priv
->pdev
->dev
,
1692 entries
* sizeof(struct tsi721_imsg_desc
),
1693 &priv
->imsg_ring
[mbox
].imd_phys
, GFP_KERNEL
);
1695 if (priv
->imsg_ring
[mbox
].imd_base
== NULL
) {
1696 dev_err(&priv
->pdev
->dev
,
1697 "Failed to allocate descriptor memory for IB MBOX%d\n",
1703 /* Fill free buffer pointer list */
1704 free_ptr
= priv
->imsg_ring
[mbox
].imfq_base
;
1705 for (i
= 0; i
< entries
; i
++)
1706 free_ptr
[i
] = cpu_to_le64(
1707 (u64
)(priv
->imsg_ring
[mbox
].buf_phys
) +
1713 * For mapping of inbound SRIO Messages into appropriate queues we need
1714 * to set Inbound Device ID register in the messaging engine. We do it
1715 * once when first inbound mailbox is requested.
1717 if (!(priv
->flags
& TSI721_IMSGID_SET
)) {
1718 iowrite32((u32
)priv
->mport
->host_deviceid
,
1719 priv
->regs
+ TSI721_IB_DEVID
);
1720 priv
->flags
|= TSI721_IMSGID_SET
;
1724 * Configure Inbound Messaging channel (ch = mbox + 4)
1727 /* Setup Inbound Message free queue */
1728 iowrite32(((u64
)priv
->imsg_ring
[mbox
].imfq_phys
>> 32),
1729 priv
->regs
+ TSI721_IBDMAC_FQBH(ch
));
1730 iowrite32(((u64
)priv
->imsg_ring
[mbox
].imfq_phys
&
1731 TSI721_IBDMAC_FQBL_MASK
),
1732 priv
->regs
+TSI721_IBDMAC_FQBL(ch
));
1733 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries
),
1734 priv
->regs
+ TSI721_IBDMAC_FQSZ(ch
));
1736 /* Setup Inbound Message descriptor queue */
1737 iowrite32(((u64
)priv
->imsg_ring
[mbox
].imd_phys
>> 32),
1738 priv
->regs
+ TSI721_IBDMAC_DQBH(ch
));
1739 iowrite32(((u32
)priv
->imsg_ring
[mbox
].imd_phys
&
1740 (u32
)TSI721_IBDMAC_DQBL_MASK
),
1741 priv
->regs
+TSI721_IBDMAC_DQBL(ch
));
1742 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries
),
1743 priv
->regs
+ TSI721_IBDMAC_DQSZ(ch
));
1745 /* Enable interrupts */
1747 #ifdef CONFIG_PCI_MSI
1748 if (priv
->flags
& TSI721_USING_MSIX
) {
1749 /* Request interrupt service if we are in MSI-X mode */
1750 rc
= request_irq(priv
->msix
[TSI721_VECT_IMB0_RCV
+ mbox
].vector
,
1751 tsi721_imsg_msix
, 0,
1752 priv
->msix
[TSI721_VECT_IMB0_RCV
+ mbox
].irq_name
,
1756 dev_dbg(&priv
->pdev
->dev
,
1757 "Unable to allocate MSI-X interrupt for "
1758 "IBOX%d-DONE\n", mbox
);
1762 rc
= request_irq(priv
->msix
[TSI721_VECT_IMB0_INT
+ mbox
].vector
,
1763 tsi721_imsg_msix
, 0,
1764 priv
->msix
[TSI721_VECT_IMB0_INT
+ mbox
].irq_name
,
1768 dev_dbg(&priv
->pdev
->dev
,
1769 "Unable to allocate MSI-X interrupt for "
1770 "IBOX%d-INT\n", mbox
);
1772 priv
->msix
[TSI721_VECT_IMB0_RCV
+ mbox
].vector
,
1777 #endif /* CONFIG_PCI_MSI */
1779 tsi721_imsg_interrupt_enable(priv
, ch
, TSI721_IBDMAC_INT_ALL
);
1781 /* Initialize Inbound Message Engine */
1782 iowrite32(TSI721_IBDMAC_CTL_INIT
, priv
->regs
+ TSI721_IBDMAC_CTL(ch
));
1783 ioread32(priv
->regs
+ TSI721_IBDMAC_CTL(ch
));
1785 priv
->imsg_ring
[mbox
].fq_wrptr
= entries
- 1;
1786 iowrite32(entries
- 1, priv
->regs
+ TSI721_IBDMAC_FQWP(ch
));
1788 priv
->imsg_init
[mbox
] = 1;
1791 #ifdef CONFIG_PCI_MSI
1793 dma_free_coherent(&priv
->pdev
->dev
,
1794 priv
->imsg_ring
[mbox
].size
* sizeof(struct tsi721_imsg_desc
),
1795 priv
->imsg_ring
[mbox
].imd_base
,
1796 priv
->imsg_ring
[mbox
].imd_phys
);
1798 priv
->imsg_ring
[mbox
].imd_base
= NULL
;
1799 #endif /* CONFIG_PCI_MSI */
1802 dma_free_coherent(&priv
->pdev
->dev
,
1803 priv
->imsg_ring
[mbox
].size
* 8,
1804 priv
->imsg_ring
[mbox
].imfq_base
,
1805 priv
->imsg_ring
[mbox
].imfq_phys
);
1807 priv
->imsg_ring
[mbox
].imfq_base
= NULL
;
1810 dma_free_coherent(&priv
->pdev
->dev
,
1811 priv
->imsg_ring
[mbox
].size
* TSI721_MSG_BUFFER_SIZE
,
1812 priv
->imsg_ring
[mbox
].buf_base
,
1813 priv
->imsg_ring
[mbox
].buf_phys
);
1815 priv
->imsg_ring
[mbox
].buf_base
= NULL
;
1822 * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
1823 * @mport: Master port implementing the Inbound Messaging Engine
1824 * @mbox: Mailbox to close
1826 static void tsi721_close_inb_mbox(struct rio_mport
*mport
, int mbox
)
1828 struct tsi721_device
*priv
= mport
->priv
;
1832 if (!priv
->imsg_init
[mbox
]) /* mbox isn't initialized yet */
1834 priv
->imsg_init
[mbox
] = 0;
1836 /* Disable Inbound Messaging Engine */
1838 /* Disable Interrupts */
1839 tsi721_imsg_interrupt_disable(priv
, ch
, TSI721_OBDMAC_INT_MASK
);
1841 #ifdef CONFIG_PCI_MSI
1842 if (priv
->flags
& TSI721_USING_MSIX
) {
1843 free_irq(priv
->msix
[TSI721_VECT_IMB0_RCV
+ mbox
].vector
,
1845 free_irq(priv
->msix
[TSI721_VECT_IMB0_INT
+ mbox
].vector
,
1848 #endif /* CONFIG_PCI_MSI */
1850 /* Clear Inbound Buffer Queue */
1851 for (rx_slot
= 0; rx_slot
< priv
->imsg_ring
[mbox
].size
; rx_slot
++)
1852 priv
->imsg_ring
[mbox
].imq_base
[rx_slot
] = NULL
;
1854 /* Free memory allocated for message buffers */
1855 dma_free_coherent(&priv
->pdev
->dev
,
1856 priv
->imsg_ring
[mbox
].size
* TSI721_MSG_BUFFER_SIZE
,
1857 priv
->imsg_ring
[mbox
].buf_base
,
1858 priv
->imsg_ring
[mbox
].buf_phys
);
1860 priv
->imsg_ring
[mbox
].buf_base
= NULL
;
1862 /* Free memory allocated for free pointr list */
1863 dma_free_coherent(&priv
->pdev
->dev
,
1864 priv
->imsg_ring
[mbox
].size
* 8,
1865 priv
->imsg_ring
[mbox
].imfq_base
,
1866 priv
->imsg_ring
[mbox
].imfq_phys
);
1868 priv
->imsg_ring
[mbox
].imfq_base
= NULL
;
1870 /* Free memory allocated for RX descriptors */
1871 dma_free_coherent(&priv
->pdev
->dev
,
1872 priv
->imsg_ring
[mbox
].size
* sizeof(struct tsi721_imsg_desc
),
1873 priv
->imsg_ring
[mbox
].imd_base
,
1874 priv
->imsg_ring
[mbox
].imd_phys
);
1876 priv
->imsg_ring
[mbox
].imd_base
= NULL
;
1880 * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
1881 * @mport: Master port implementing the Inbound Messaging Engine
1882 * @mbox: Inbound mailbox number
1883 * @buf: Buffer to add to inbound queue
1885 static int tsi721_add_inb_buffer(struct rio_mport
*mport
, int mbox
, void *buf
)
1887 struct tsi721_device
*priv
= mport
->priv
;
1891 rx_slot
= priv
->imsg_ring
[mbox
].rx_slot
;
1892 if (priv
->imsg_ring
[mbox
].imq_base
[rx_slot
]) {
1893 dev_err(&priv
->pdev
->dev
,
1894 "Error adding inbound buffer %d, buffer exists\n",
1900 priv
->imsg_ring
[mbox
].imq_base
[rx_slot
] = buf
;
1902 if (++priv
->imsg_ring
[mbox
].rx_slot
== priv
->imsg_ring
[mbox
].size
)
1903 priv
->imsg_ring
[mbox
].rx_slot
= 0;
1910 * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
1911 * @mport: Master port implementing the Inbound Messaging Engine
1912 * @mbox: Inbound mailbox number
1914 * Returns pointer to the message on success or NULL on failure.
1916 static void *tsi721_get_inb_message(struct rio_mport
*mport
, int mbox
)
1918 struct tsi721_device
*priv
= mport
->priv
;
1919 struct tsi721_imsg_desc
*desc
;
1921 void *rx_virt
= NULL
;
1928 if (!priv
->imsg_init
[mbox
])
1931 desc
= priv
->imsg_ring
[mbox
].imd_base
;
1932 desc
+= priv
->imsg_ring
[mbox
].desc_rdptr
;
1934 if (!(le32_to_cpu(desc
->msg_info
) & TSI721_IMD_HO
))
1937 rx_slot
= priv
->imsg_ring
[mbox
].rx_slot
;
1938 while (priv
->imsg_ring
[mbox
].imq_base
[rx_slot
] == NULL
) {
1939 if (++rx_slot
== priv
->imsg_ring
[mbox
].size
)
1943 rx_phys
= ((u64
)le32_to_cpu(desc
->bufptr_hi
) << 32) |
1944 le32_to_cpu(desc
->bufptr_lo
);
1946 rx_virt
= priv
->imsg_ring
[mbox
].buf_base
+
1947 (rx_phys
- (u64
)priv
->imsg_ring
[mbox
].buf_phys
);
1949 buf
= priv
->imsg_ring
[mbox
].imq_base
[rx_slot
];
1950 msg_size
= le32_to_cpu(desc
->msg_info
) & TSI721_IMD_BCOUNT
;
1952 msg_size
= RIO_MAX_MSG_SIZE
;
1954 memcpy(buf
, rx_virt
, msg_size
);
1955 priv
->imsg_ring
[mbox
].imq_base
[rx_slot
] = NULL
;
1957 desc
->msg_info
&= cpu_to_le32(~TSI721_IMD_HO
);
1958 if (++priv
->imsg_ring
[mbox
].desc_rdptr
== priv
->imsg_ring
[mbox
].size
)
1959 priv
->imsg_ring
[mbox
].desc_rdptr
= 0;
1961 iowrite32(priv
->imsg_ring
[mbox
].desc_rdptr
,
1962 priv
->regs
+ TSI721_IBDMAC_DQRP(ch
));
1964 /* Return free buffer into the pointer list */
1965 free_ptr
= priv
->imsg_ring
[mbox
].imfq_base
;
1966 free_ptr
[priv
->imsg_ring
[mbox
].fq_wrptr
] = cpu_to_le64(rx_phys
);
1968 if (++priv
->imsg_ring
[mbox
].fq_wrptr
== priv
->imsg_ring
[mbox
].size
)
1969 priv
->imsg_ring
[mbox
].fq_wrptr
= 0;
1971 iowrite32(priv
->imsg_ring
[mbox
].fq_wrptr
,
1972 priv
->regs
+ TSI721_IBDMAC_FQWP(ch
));
1978 * tsi721_messages_init - Initialization of Messaging Engine
1979 * @priv: pointer to tsi721 private data
1981 * Configures Tsi721 messaging engine.
1983 static int tsi721_messages_init(struct tsi721_device
*priv
)
1987 iowrite32(0, priv
->regs
+ TSI721_SMSG_ECC_LOG
);
1988 iowrite32(0, priv
->regs
+ TSI721_RETRY_GEN_CNT
);
1989 iowrite32(0, priv
->regs
+ TSI721_RETRY_RX_CNT
);
1991 /* Set SRIO Message Request/Response Timeout */
1992 iowrite32(TSI721_RQRPTO_VAL
, priv
->regs
+ TSI721_RQRPTO
);
1994 /* Initialize Inbound Messaging Engine Registers */
1995 for (ch
= 0; ch
< TSI721_IMSG_CHNUM
; ch
++) {
1996 /* Clear interrupt bits */
1997 iowrite32(TSI721_IBDMAC_INT_MASK
,
1998 priv
->regs
+ TSI721_IBDMAC_INT(ch
));
2000 iowrite32(0, priv
->regs
+ TSI721_IBDMAC_STS(ch
));
2002 iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK
,
2003 priv
->regs
+ TSI721_SMSG_ECC_COR_LOG(ch
));
2004 iowrite32(TSI721_SMSG_ECC_NCOR_MASK
,
2005 priv
->regs
+ TSI721_SMSG_ECC_NCOR(ch
));
2012 * tsi721_disable_ints - disables all device interrupts
2013 * @priv: pointer to tsi721 private data
2015 static void tsi721_disable_ints(struct tsi721_device
*priv
)
2019 /* Disable all device level interrupts */
2020 iowrite32(0, priv
->regs
+ TSI721_DEV_INTE
);
2022 /* Disable all Device Channel interrupts */
2023 iowrite32(0, priv
->regs
+ TSI721_DEV_CHAN_INTE
);
2025 /* Disable all Inbound Msg Channel interrupts */
2026 for (ch
= 0; ch
< TSI721_IMSG_CHNUM
; ch
++)
2027 iowrite32(0, priv
->regs
+ TSI721_IBDMAC_INTE(ch
));
2029 /* Disable all Outbound Msg Channel interrupts */
2030 for (ch
= 0; ch
< TSI721_OMSG_CHNUM
; ch
++)
2031 iowrite32(0, priv
->regs
+ TSI721_OBDMAC_INTE(ch
));
2033 /* Disable all general messaging interrupts */
2034 iowrite32(0, priv
->regs
+ TSI721_SMSG_INTE
);
2036 /* Disable all BDMA Channel interrupts */
2037 for (ch
= 0; ch
< TSI721_DMA_MAXCH
; ch
++)
2038 iowrite32(0, priv
->regs
+ TSI721_DMAC_INTE(ch
));
2040 /* Disable all general BDMA interrupts */
2041 iowrite32(0, priv
->regs
+ TSI721_BDMA_INTE
);
2043 /* Disable all SRIO Channel interrupts */
2044 for (ch
= 0; ch
< TSI721_SRIO_MAXCH
; ch
++)
2045 iowrite32(0, priv
->regs
+ TSI721_SR_CHINTE(ch
));
2047 /* Disable all general SR2PC interrupts */
2048 iowrite32(0, priv
->regs
+ TSI721_SR2PC_GEN_INTE
);
2050 /* Disable all PC2SR interrupts */
2051 iowrite32(0, priv
->regs
+ TSI721_PC2SR_INTE
);
2053 /* Disable all I2C interrupts */
2054 iowrite32(0, priv
->regs
+ TSI721_I2C_INT_ENABLE
);
2056 /* Disable SRIO MAC interrupts */
2057 iowrite32(0, priv
->regs
+ TSI721_RIO_EM_INT_ENABLE
);
2058 iowrite32(0, priv
->regs
+ TSI721_RIO_EM_DEV_INT_EN
);
2062 * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
2063 * @priv: pointer to tsi721 private data
2065 * Configures Tsi721 as RapidIO master port.
2067 static int __devinit
tsi721_setup_mport(struct tsi721_device
*priv
)
2069 struct pci_dev
*pdev
= priv
->pdev
;
2071 struct rio_ops
*ops
;
2073 struct rio_mport
*mport
;
2075 ops
= kzalloc(sizeof(struct rio_ops
), GFP_KERNEL
);
2077 dev_dbg(&pdev
->dev
, "Unable to allocate memory for rio_ops\n");
2081 ops
->lcread
= tsi721_lcread
;
2082 ops
->lcwrite
= tsi721_lcwrite
;
2083 ops
->cread
= tsi721_cread_dma
;
2084 ops
->cwrite
= tsi721_cwrite_dma
;
2085 ops
->dsend
= tsi721_dsend
;
2086 ops
->open_inb_mbox
= tsi721_open_inb_mbox
;
2087 ops
->close_inb_mbox
= tsi721_close_inb_mbox
;
2088 ops
->open_outb_mbox
= tsi721_open_outb_mbox
;
2089 ops
->close_outb_mbox
= tsi721_close_outb_mbox
;
2090 ops
->add_outb_message
= tsi721_add_outb_message
;
2091 ops
->add_inb_buffer
= tsi721_add_inb_buffer
;
2092 ops
->get_inb_message
= tsi721_get_inb_message
;
2094 mport
= kzalloc(sizeof(struct rio_mport
), GFP_KERNEL
);
2097 dev_dbg(&pdev
->dev
, "Unable to allocate memory for mport\n");
2103 mport
->sys_size
= 0; /* small system */
2104 mport
->phy_type
= RIO_PHY_SERIAL
;
2105 mport
->priv
= (void *)priv
;
2106 mport
->phys_efptr
= 0x100;
2108 INIT_LIST_HEAD(&mport
->dbells
);
2110 rio_init_dbell_res(&mport
->riores
[RIO_DOORBELL_RESOURCE
], 0, 0xffff);
2111 rio_init_mbox_res(&mport
->riores
[RIO_INB_MBOX_RESOURCE
], 0, 3);
2112 rio_init_mbox_res(&mport
->riores
[RIO_OUTB_MBOX_RESOURCE
], 0, 3);
2113 strcpy(mport
->name
, "Tsi721 mport");
2115 /* Hook up interrupt handler */
2117 #ifdef CONFIG_PCI_MSI
2118 if (!tsi721_enable_msix(priv
))
2119 priv
->flags
|= TSI721_USING_MSIX
;
2120 else if (!pci_enable_msi(pdev
))
2121 priv
->flags
|= TSI721_USING_MSI
;
2123 dev_info(&pdev
->dev
,
2124 "MSI/MSI-X is not available. Using legacy INTx.\n");
2125 #endif /* CONFIG_PCI_MSI */
2127 err
= tsi721_request_irq(mport
);
2130 tsi721_interrupts_init(priv
);
2131 ops
->pwenable
= tsi721_pw_enable
;
2133 dev_err(&pdev
->dev
, "Unable to get assigned PCI IRQ "
2134 "vector %02X err=0x%x\n", pdev
->irq
, err
);
2136 /* Enable SRIO link */
2137 iowrite32(ioread32(priv
->regs
+ TSI721_DEVCTL
) |
2138 TSI721_DEVCTL_SRBOOT_CMPL
,
2139 priv
->regs
+ TSI721_DEVCTL
);
2141 rio_register_mport(mport
);
2142 priv
->mport
= mport
;
2144 if (mport
->host_deviceid
>= 0)
2145 iowrite32(RIO_PORT_GEN_HOST
| RIO_PORT_GEN_MASTER
|
2146 RIO_PORT_GEN_DISCOVERED
,
2147 priv
->regs
+ (0x100 + RIO_PORT_GEN_CTL_CSR
));
2149 iowrite32(0, priv
->regs
+ (0x100 + RIO_PORT_GEN_CTL_CSR
));
2154 static int __devinit
tsi721_probe(struct pci_dev
*pdev
,
2155 const struct pci_device_id
*id
)
2157 struct tsi721_device
*priv
;
2162 priv
= kzalloc(sizeof(struct tsi721_device
), GFP_KERNEL
);
2164 dev_err(&pdev
->dev
, "Failed to allocate memory for device\n");
2169 err
= pci_enable_device(pdev
);
2171 dev_err(&pdev
->dev
, "Failed to enable PCI device\n");
2178 for (i
= 0; i
<= PCI_STD_RESOURCE_END
; i
++) {
2179 dev_dbg(&pdev
->dev
, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
2180 i
, (unsigned long long)pci_resource_start(pdev
, i
),
2181 (unsigned long)pci_resource_len(pdev
, i
),
2182 pci_resource_flags(pdev
, i
));
2186 * Verify BAR configuration
2189 /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
2190 if (!(pci_resource_flags(pdev
, BAR_0
) & IORESOURCE_MEM
) ||
2191 pci_resource_flags(pdev
, BAR_0
) & IORESOURCE_MEM_64
||
2192 pci_resource_len(pdev
, BAR_0
) < TSI721_REG_SPACE_SIZE
) {
2194 "Missing or misconfigured CSR BAR0, aborting.\n");
2196 goto err_disable_pdev
;
2199 /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
2200 if (!(pci_resource_flags(pdev
, BAR_1
) & IORESOURCE_MEM
) ||
2201 pci_resource_flags(pdev
, BAR_1
) & IORESOURCE_MEM_64
||
2202 pci_resource_len(pdev
, BAR_1
) < TSI721_DB_WIN_SIZE
) {
2204 "Missing or misconfigured Doorbell BAR1, aborting.\n");
2206 goto err_disable_pdev
;
2210 * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
2212 * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
2213 * It may be a good idea to keep them disabled using HW configuration
2214 * to save PCI memory space.
2216 if ((pci_resource_flags(pdev
, BAR_2
) & IORESOURCE_MEM
) &&
2217 (pci_resource_flags(pdev
, BAR_2
) & IORESOURCE_MEM_64
)) {
2218 dev_info(&pdev
->dev
, "Outbound BAR2 is not used but enabled.\n");
2221 if ((pci_resource_flags(pdev
, BAR_4
) & IORESOURCE_MEM
) &&
2222 (pci_resource_flags(pdev
, BAR_4
) & IORESOURCE_MEM_64
)) {
2223 dev_info(&pdev
->dev
, "Outbound BAR4 is not used but enabled.\n");
2226 err
= pci_request_regions(pdev
, DRV_NAME
);
2228 dev_err(&pdev
->dev
, "Cannot obtain PCI resources, "
2230 goto err_disable_pdev
;
2233 pci_set_master(pdev
);
2235 priv
->regs
= pci_ioremap_bar(pdev
, BAR_0
);
2238 "Unable to map device registers space, aborting\n");
2243 priv
->odb_base
= pci_ioremap_bar(pdev
, BAR_1
);
2244 if (!priv
->odb_base
) {
2246 "Unable to map outbound doorbells space, aborting\n");
2248 goto err_unmap_bars
;
2251 /* Configure DMA attributes. */
2252 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
2253 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
2254 dev_info(&pdev
->dev
, "Unable to set DMA mask\n");
2255 goto err_unmap_bars
;
2258 if (pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32)))
2259 dev_info(&pdev
->dev
, "Unable to set consistent DMA mask\n");
2261 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
2263 dev_info(&pdev
->dev
, "Unable to set consistent DMA mask\n");
2266 cap
= pci_pcie_cap(pdev
);
2269 /* Clear "no snoop" and "relaxed ordering" bits, use default MRRS. */
2270 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DEVCTL
, ®val
);
2271 regval
&= ~(PCI_EXP_DEVCTL_READRQ
| PCI_EXP_DEVCTL_RELAX_EN
|
2272 PCI_EXP_DEVCTL_NOSNOOP_EN
);
2273 regval
|= 0x2 << MAX_READ_REQUEST_SZ_SHIFT
;
2274 pci_write_config_dword(pdev
, cap
+ PCI_EXP_DEVCTL
, regval
);
2276 /* Adjust PCIe completion timeout. */
2277 pci_read_config_dword(pdev
, cap
+ PCI_EXP_DEVCTL2
, ®val
);
2279 pci_write_config_dword(pdev
, cap
+ PCI_EXP_DEVCTL2
, regval
| 0x2);
2282 * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
2284 pci_write_config_dword(pdev
, TSI721_PCIECFG_EPCTL
, 0x01);
2285 pci_write_config_dword(pdev
, TSI721_PCIECFG_MSIXTBL
,
2286 TSI721_MSIXTBL_OFFSET
);
2287 pci_write_config_dword(pdev
, TSI721_PCIECFG_MSIXPBA
,
2288 TSI721_MSIXPBA_OFFSET
);
2289 pci_write_config_dword(pdev
, TSI721_PCIECFG_EPCTL
, 0);
2292 tsi721_disable_ints(priv
);
2294 tsi721_init_pc2sr_mapping(priv
);
2295 tsi721_init_sr2pc_mapping(priv
);
2297 if (tsi721_bdma_init(priv
)) {
2298 dev_err(&pdev
->dev
, "BDMA initialization failed, aborting\n");
2300 goto err_unmap_bars
;
2303 err
= tsi721_doorbell_init(priv
);
2307 tsi721_port_write_init(priv
);
2309 err
= tsi721_messages_init(priv
);
2311 goto err_free_consistent
;
2313 err
= tsi721_setup_mport(priv
);
2315 goto err_free_consistent
;
2319 err_free_consistent
:
2320 tsi721_doorbell_free(priv
);
2322 tsi721_bdma_free(priv
);
2325 iounmap(priv
->regs
);
2327 iounmap(priv
->odb_base
);
2329 pci_release_regions(pdev
);
2330 pci_clear_master(pdev
);
2332 pci_disable_device(pdev
);
2339 static DEFINE_PCI_DEVICE_TABLE(tsi721_pci_tbl
) = {
2340 { PCI_DEVICE(PCI_VENDOR_ID_IDT
, PCI_DEVICE_ID_TSI721
) },
2341 { 0, } /* terminate list */
2344 MODULE_DEVICE_TABLE(pci
, tsi721_pci_tbl
);
2346 static struct pci_driver tsi721_driver
= {
2348 .id_table
= tsi721_pci_tbl
,
2349 .probe
= tsi721_probe
,
2352 static int __init
tsi721_init(void)
2354 return pci_register_driver(&tsi721_driver
);
2357 static void __exit
tsi721_exit(void)
2359 pci_unregister_driver(&tsi721_driver
);
2362 device_initcall(tsi721_init
);