blob: db95d71ba4e9afd7d164c4bcb683c5a250c1673d [file] [log] [blame]
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001/*
2 * RapidIO mport driver for Tsi721 PCIExpress-to-SRIO bridge
3 *
4 * Copyright 2011 Integrated Device Technology, Inc.
5 * Alexandre Bounine <alexandre.bounine@idt.com>
6 * Chul Kim <chul.kim@idt.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
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>
36
37#include "tsi721.h"
38
39#define DEBUG_PW /* Inbound Port-Write debugging */
40
41static void tsi721_omsg_handler(struct tsi721_device *priv, int ch);
42static void tsi721_imsg_handler(struct tsi721_device *priv, int ch);
43
44/**
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
51 *
52 * Generates a local SREP space read. Returns %0 on
53 * success or %-EINVAL on failure.
54 */
55static int tsi721_lcread(struct rio_mport *mport, int index, u32 offset,
56 int len, u32 *data)
57{
58 struct tsi721_device *priv = mport->priv;
59
60 if (len != sizeof(u32))
61 return -EINVAL; /* only 32-bit access is supported */
62
63 *data = ioread32(priv->regs + offset);
64
65 return 0;
66}
67
68/**
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
75 *
76 * Generates a local write into SREP configuration space. Returns %0 on
77 * success or %-EINVAL on failure.
78 */
79static int tsi721_lcwrite(struct rio_mport *mport, int index, u32 offset,
80 int len, u32 data)
81{
82 struct tsi721_device *priv = mport->priv;
83
84 if (len != sizeof(u32))
85 return -EINVAL; /* only 32-bit access is supported */
86
87 iowrite32(data, priv->regs + offset);
88
89 return 0;
90}
91
92/**
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)
103 *
104 * Generates a RapidIO maintenance transaction (Read or Write).
105 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
106 */
107static 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)
110{
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700111 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(priv->mdma.ch_id);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700112 struct tsi721_dma_desc *bd_ptr;
113 u32 rd_count, swr_ptr, ch_stat;
114 int i, err = 0;
115 u32 op = do_wr ? MAINT_WR : MAINT_RD;
116
117 if (offset > (RIO_MAINT_SPACE_SZ - len) || (len != sizeof(u32)))
118 return -EINVAL;
119
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700120 bd_ptr = priv->mdma.bd_base;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700121
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700122 rd_count = ioread32(regs + TSI721_DMAC_DRDCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700123
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;
129 if (do_wr)
130 bd_ptr[0].data[0] = cpu_to_be32p(data);
131 else
132 bd_ptr[0].data[0] = 0xffffffff;
133
134 mb();
135
136 /* Start DMA operation */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700137 iowrite32(rd_count + 2, regs + TSI721_DMAC_DWRCNT);
138 ioread32(regs + TSI721_DMAC_DWRCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700139 i = 0;
140
141 /* Wait until DMA transfer is finished */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700142 while ((ch_stat = ioread32(regs + TSI721_DMAC_STS))
143 & TSI721_DMAC_STS_RUN) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700144 udelay(1);
145 if (++i >= 5000000) {
146 dev_dbg(&priv->pdev->dev,
147 "%s : DMA[%d] read timeout ch_status=%x\n",
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700148 __func__, priv->mdma.ch_id, ch_stat);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700149 if (!do_wr)
150 *data = 0xffffffff;
151 err = -EIO;
152 goto err_out;
153 }
154 }
155
156 if (ch_stat & TSI721_DMAC_STS_ABORT) {
157 /* If DMA operation aborted due to error,
158 * reinitialize DMA channel
159 */
160 dev_dbg(&priv->pdev->dev, "%s : DMA ABORT ch_stat=%x\n",
161 __func__, ch_stat);
162 dev_dbg(&priv->pdev->dev, "OP=%d : destid=%x hc=%x off=%x\n",
163 do_wr ? MAINT_WR : MAINT_RD, destid, hopcount, offset);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700164 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
165 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700166 udelay(10);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700167 iowrite32(0, regs + TSI721_DMAC_DWRCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700168 udelay(1);
169 if (!do_wr)
170 *data = 0xffffffff;
171 err = -EIO;
172 goto err_out;
173 }
174
175 if (!do_wr)
176 *data = be32_to_cpu(bd_ptr[0].data[0]);
177
178 /*
179 * Update descriptor status FIFO RD pointer.
180 * NOTE: Skipping check and clear FIFO entries because we are waiting
181 * for transfer to be completed.
182 */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700183 swr_ptr = ioread32(regs + TSI721_DMAC_DSWP);
184 iowrite32(swr_ptr, regs + TSI721_DMAC_DSRP);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700185err_out:
186
187 return err;
188}
189
190/**
191 * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
192 * using Tsi721 BDMA engine.
193 * @mport: RapidIO master port control structure
194 * @index: ID of RapdiIO interface
195 * @destid: Destination ID of transaction
196 * @hopcount: Number of hops to target device
197 * @offset: Offset into configuration space
198 * @len: Length (in bytes) of the maintenance transaction
199 * @val: Location to be read into
200 *
201 * Generates a RapidIO maintenance read transaction.
202 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
203 */
204static int tsi721_cread_dma(struct rio_mport *mport, int index, u16 destid,
205 u8 hopcount, u32 offset, int len, u32 *data)
206{
207 struct tsi721_device *priv = mport->priv;
208
209 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
210 offset, len, data, 0);
211}
212
213/**
214 * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
215 * using Tsi721 BDMA engine
216 * @mport: RapidIO master port control structure
217 * @index: ID of RapdiIO interface
218 * @destid: Destination ID of transaction
219 * @hopcount: Number of hops to target device
220 * @offset: Offset into configuration space
221 * @len: Length (in bytes) of the maintenance transaction
222 * @val: Value to be written
223 *
224 * Generates a RapidIO maintenance write transaction.
225 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
226 */
227static int tsi721_cwrite_dma(struct rio_mport *mport, int index, u16 destid,
228 u8 hopcount, u32 offset, int len, u32 data)
229{
230 struct tsi721_device *priv = mport->priv;
231 u32 temp = data;
232
233 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
234 offset, len, &temp, 1);
235}
236
237/**
238 * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
Alexandre Bounine748353c2016-03-22 14:26:23 -0700239 * @priv: tsi721 device private structure
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700240 *
241 * Handles inbound port-write interrupts. Copies PW message from an internal
242 * buffer into PW message FIFO and schedules deferred routine to process
243 * queued messages.
244 */
245static int
Alexandre Bounine748353c2016-03-22 14:26:23 -0700246tsi721_pw_handler(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700247{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700248 u32 pw_stat;
249 u32 pw_buf[TSI721_RIO_PW_MSG_SIZE/sizeof(u32)];
250
251
252 pw_stat = ioread32(priv->regs + TSI721_RIO_PW_RX_STAT);
253
254 if (pw_stat & TSI721_RIO_PW_RX_STAT_PW_VAL) {
255 pw_buf[0] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(0));
256 pw_buf[1] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(1));
257 pw_buf[2] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(2));
258 pw_buf[3] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(3));
259
260 /* Queue PW message (if there is room in FIFO),
261 * otherwise discard it.
262 */
263 spin_lock(&priv->pw_fifo_lock);
264 if (kfifo_avail(&priv->pw_fifo) >= TSI721_RIO_PW_MSG_SIZE)
265 kfifo_in(&priv->pw_fifo, pw_buf,
266 TSI721_RIO_PW_MSG_SIZE);
267 else
268 priv->pw_discard_count++;
269 spin_unlock(&priv->pw_fifo_lock);
270 }
271
272 /* Clear pending PW interrupts */
273 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
274 priv->regs + TSI721_RIO_PW_RX_STAT);
275
276 schedule_work(&priv->pw_work);
277
278 return 0;
279}
280
281static void tsi721_pw_dpc(struct work_struct *work)
282{
283 struct tsi721_device *priv = container_of(work, struct tsi721_device,
284 pw_work);
285 u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)]; /* Use full size PW message
286 buffer for RIO layer */
287
288 /*
289 * Process port-write messages
290 */
291 while (kfifo_out_spinlocked(&priv->pw_fifo, (unsigned char *)msg_buffer,
292 TSI721_RIO_PW_MSG_SIZE, &priv->pw_fifo_lock)) {
293 /* Process one message */
294#ifdef DEBUG_PW
295 {
296 u32 i;
297 pr_debug("%s : Port-Write Message:", __func__);
298 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); ) {
299 pr_debug("0x%02x: %08x %08x %08x %08x", i*4,
300 msg_buffer[i], msg_buffer[i + 1],
301 msg_buffer[i + 2], msg_buffer[i + 3]);
302 i += 4;
303 }
304 pr_debug("\n");
305 }
306#endif
307 /* Pass the port-write message to RIO core for processing */
308 rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
309 }
310}
311
312/**
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
316 */
317static int tsi721_pw_enable(struct rio_mport *mport, int enable)
318{
319 struct tsi721_device *priv = mport->priv;
320 u32 rval;
321
322 rval = ioread32(priv->regs + TSI721_RIO_EM_INT_ENABLE);
323
324 if (enable)
325 rval |= TSI721_RIO_EM_INT_ENABLE_PW_RX;
326 else
327 rval &= ~TSI721_RIO_EM_INT_ENABLE_PW_RX;
328
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);
334
335 return 0;
336}
337
338/**
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
344 *
345 * Sends a RapidIO doorbell message. Always returns %0.
346 */
347static int tsi721_dsend(struct rio_mport *mport, int index,
348 u16 destid, u16 data)
349{
350 struct tsi721_device *priv = mport->priv;
351 u32 offset;
352
353 offset = (((mport->sys_size) ? RIO_TT_CODE_16 : RIO_TT_CODE_8) << 18) |
354 (destid << 2);
355
356 dev_dbg(&priv->pdev->dev,
357 "Send Doorbell 0x%04x to destID 0x%x\n", data, destid);
358 iowrite16be(data, priv->odb_base + offset);
359
360 return 0;
361}
362
363/**
364 * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
Alexandre Bounine748353c2016-03-22 14:26:23 -0700365 * @priv: tsi721 device-specific data structure
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700366 *
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.
370 */
371static int
Alexandre Bounine748353c2016-03-22 14:26:23 -0700372tsi721_dbell_handler(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700373{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700374 u32 regval;
375
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));
381
382 schedule_work(&priv->idb_work);
383
384 return 0;
385}
386
387static void tsi721_db_dpc(struct work_struct *work)
388{
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;
401
402 /*
403 * Process queued inbound doorbells
404 */
Alexandre Bounine748353c2016-03-22 14:26:23 -0700405 mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700406
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800407 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;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700409
410 while (wr_ptr != rd_ptr) {
411 idb_entry = (u64 *)(priv->idb_base +
412 (TSI721_IDB_ENTRY_SIZE * rd_ptr));
413 rd_ptr++;
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800414 rd_ptr %= IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700415 idb.msg = *idb_entry;
416 *idb_entry = 0;
417
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;
424 }
425 }
426
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 dev_dbg(&priv->pdev->dev,
432 "spurious inb doorbell, sid %2.2x tid %2.2x"
433 " info %4.4x\n", DBELL_SID(idb.bytes),
434 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
435 }
Alexandre Bounine3670e7e2012-08-21 16:16:11 -0700436
437 wr_ptr = ioread32(priv->regs +
438 TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700439 }
440
441 iowrite32(rd_ptr & (IDB_QSIZE - 1),
442 priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
443
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));
Alexandre Bounine3670e7e2012-08-21 16:16:11 -0700449
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);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700453}
454
455/**
456 * tsi721_irqhandler - Tsi721 interrupt handler
457 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700458 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700459 *
460 * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
461 * interrupt events and calls an event-specific handler(s).
462 */
463static irqreturn_t tsi721_irqhandler(int irq, void *ptr)
464{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700465 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700466 u32 dev_int;
467 u32 dev_ch_int;
468 u32 intval;
469 u32 ch_inte;
470
Alexandre Bounine1ccc8192013-05-24 15:55:17 -0700471 /* For MSI mode disable all device-level interrupts */
472 if (priv->flags & TSI721_USING_MSI)
473 iowrite32(0, priv->regs + TSI721_DEV_INTE);
474
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700475 dev_int = ioread32(priv->regs + TSI721_DEV_INT);
476 if (!dev_int)
477 return IRQ_NONE;
478
479 dev_ch_int = ioread32(priv->regs + TSI721_DEV_CHAN_INT);
480
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)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700488 tsi721_dbell_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700489 else
490 dev_info(&priv->pdev->dev,
491 "Unsupported SR_CH_INT %x\n", intval);
492
493 /* Clear interrupts */
494 iowrite32(intval,
495 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
496 ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
497 }
498 }
499
500 if (dev_int & TSI721_DEV_INT_SMSG_CH) {
501 int ch;
502
503 /*
504 * Service channel interrupts from Messaging Engine
505 */
506
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);
512
513 /*
514 * Process Inbound Message interrupt for each MBOX
515 */
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);
520 }
521 }
522
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);
528
529 /*
530 * Process Outbound Message interrupts for each MBOX
531 */
532
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);
537 }
538 }
539 }
540
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)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700545 tsi721_pw_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700546 }
547
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700548#ifdef CONFIG_RAPIDIO_DMA_ENGINE
549 if (dev_int & TSI721_DEV_INT_BDMA_CH) {
550 int ch;
551
552 if (dev_ch_int & TSI721_INT_BDMA_CHAN_M) {
553 dev_dbg(&priv->pdev->dev,
554 "IRQ from DMA channel 0x%08x\n", dev_ch_int);
555
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]);
560 }
561 }
562 }
563#endif
Alexandre Bounine1ccc8192013-05-24 15:55:17 -0700564
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);
570 }
571
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700572 return IRQ_HANDLED;
573}
574
575static void tsi721_interrupts_init(struct tsi721_device *priv)
576{
577 u32 intr;
578
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));
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700584
585 /* Enable SRIO MAC interrupts */
586 iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT,
587 priv->regs + TSI721_RIO_EM_DEV_INT_EN);
588
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700589 /* 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);
598
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700599 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 |
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700603 TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700604
605 iowrite32(intr, priv->regs + TSI721_DEV_INTE);
606 ioread32(priv->regs + TSI721_DEV_INTE);
607}
608
609#ifdef CONFIG_PCI_MSI
610/**
611 * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
612 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700613 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700614 *
615 * Handles outbound messaging interrupts signaled using MSI-X.
616 */
617static irqreturn_t tsi721_omsg_msix(int irq, void *ptr)
618{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700619 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700620 int mbox;
621
622 mbox = (irq - priv->msix[TSI721_VECT_OMB0_DONE].vector) % RIO_MAX_MBOX;
623 tsi721_omsg_handler(priv, mbox);
624 return IRQ_HANDLED;
625}
626
627/**
628 * tsi721_imsg_msix - MSI-X interrupt handler for inbound messaging
629 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700630 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700631 *
632 * Handles inbound messaging interrupts signaled using MSI-X.
633 */
634static irqreturn_t tsi721_imsg_msix(int irq, void *ptr)
635{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700636 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700637 int mbox;
638
639 mbox = (irq - priv->msix[TSI721_VECT_IMB0_RCV].vector) % RIO_MAX_MBOX;
640 tsi721_imsg_handler(priv, mbox + 4);
641 return IRQ_HANDLED;
642}
643
644/**
645 * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
646 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700647 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700648 *
649 * Handles Tsi721 interrupts from SRIO MAC.
650 */
651static irqreturn_t tsi721_srio_msix(int irq, void *ptr)
652{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700653 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700654 u32 srio_int;
655
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)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700659 tsi721_pw_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700660
661 return IRQ_HANDLED;
662}
663
664/**
665 * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
666 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700667 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700668 *
669 * Handles Tsi721 interrupts from SR2PC Channel.
670 * NOTE: At this moment services only one SR2PC channel associated with inbound
671 * doorbells.
672 */
673static irqreturn_t tsi721_sr2pc_ch_msix(int irq, void *ptr)
674{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700675 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700676 u32 sr_ch_int;
677
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)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700681 tsi721_dbell_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700682
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));
687
688 return IRQ_HANDLED;
689}
690
691/**
692 * tsi721_request_msix - register interrupt service for MSI-X mode.
Alexandre Bounine748353c2016-03-22 14:26:23 -0700693 * @priv: tsi721 device-specific data structure
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700694 *
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.
698 */
Alexandre Bounine748353c2016-03-22 14:26:23 -0700699static int tsi721_request_msix(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700700{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700701 int err = 0;
702
703 err = request_irq(priv->msix[TSI721_VECT_IDB].vector,
704 tsi721_sr2pc_ch_msix, 0,
Alexandre Bounine748353c2016-03-22 14:26:23 -0700705 priv->msix[TSI721_VECT_IDB].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700706 if (err)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700707 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700708
709 err = request_irq(priv->msix[TSI721_VECT_PWRX].vector,
710 tsi721_srio_msix, 0,
Alexandre Bounine748353c2016-03-22 14:26:23 -0700711 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;
715 }
716
717 return 0;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700718}
719
720/**
721 * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
722 * @priv: pointer to tsi721 private data
723 *
724 * Configures MSI-X support for Tsi721. Supports only an exact number
725 * of requested vectors.
726 */
727static int tsi721_enable_msix(struct tsi721_device *priv)
728{
729 struct msix_entry entries[TSI721_VECT_MAX];
730 int err;
731 int i;
732
733 entries[TSI721_VECT_IDB].entry = TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE);
734 entries[TSI721_VECT_PWRX].entry = TSI721_MSIX_SRIO_MAC_INT;
735
736 /*
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.
741 */
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);
751 }
752
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700753#ifdef CONFIG_RAPIDIO_DMA_ENGINE
754 /*
755 * Initialize MSI-X entries for Block DMA Engine:
756 * this driver supports XXX DMA channels
757 * (one is reserved for SRIO maintenance transactions)
758 */
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);
764 }
765#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
766
Alexander Gordeev1c92ab12014-06-06 14:37:16 -0700767 err = pci_enable_msix_exact(priv->pdev, entries, ARRAY_SIZE(entries));
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700768 if (err) {
Alexander Gordeev1c92ab12014-06-06 14:37:16 -0700769 dev_err(&priv->pdev->dev,
770 "Failed to enable MSI-X (err=%d)\n", err);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700771 return err;
772 }
773
774 /*
775 * Copy MSI-X vector information into tsi721 private structure
776 */
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));
783
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));
790
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));
796
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));
802
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));
808 }
809
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700810#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));
817
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));
823 }
824#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
825
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700826 return 0;
827}
828#endif /* CONFIG_PCI_MSI */
829
Alexandre Bounine748353c2016-03-22 14:26:23 -0700830static int tsi721_request_irq(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700831{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700832 int err;
833
834#ifdef CONFIG_PCI_MSI
835 if (priv->flags & TSI721_USING_MSIX)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700836 err = tsi721_request_msix(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700837 else
838#endif
839 err = request_irq(priv->pdev->irq, tsi721_irqhandler,
840 (priv->flags & TSI721_USING_MSI) ? 0 : IRQF_SHARED,
Alexandre Bounine748353c2016-03-22 14:26:23 -0700841 DRV_NAME, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700842
843 if (err)
844 dev_err(&priv->pdev->dev,
845 "Unable to allocate interrupt, Error: %d\n", err);
846
847 return err;
848}
849
Alexandre Bounine748353c2016-03-22 14:26:23 -0700850static void tsi721_free_irq(struct tsi721_device *priv)
851{
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);
859}
860
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700861/**
862 * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
863 * translation regions.
864 * @priv: pointer to tsi721 private data
865 *
866 * Disables SREP translation regions.
867 */
868static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
869{
870 int i;
871
872 /* Disable all PC2SR translation windows */
873 for (i = 0; i < TSI721_OBWIN_NUM; i++)
874 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
875}
876
877/**
Alexandre Bounine71afe342012-10-04 17:16:00 -0700878 * tsi721_rio_map_inb_mem -- Mapping inbound memory region.
879 * @mport: RapidIO master port
880 * @lstart: Local memory space start address.
881 * @rstart: RapidIO space start address.
882 * @size: The mapping region size.
883 * @flags: Flags for mapping. 0 for using default flags.
884 *
885 * Return: 0 -- Success.
886 *
887 * This function will create the inbound mapping
888 * from rstart to lstart.
889 */
890static int tsi721_rio_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
891 u64 rstart, u32 size, u32 flags)
892{
893 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700894 int i, avail = -1;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700895 u32 regval;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700896 struct tsi721_ib_win *ib_win;
Alexandre Bounine9673b882016-03-22 14:25:54 -0700897 bool direct = (lstart == rstart);
898 u64 ibw_size;
899 dma_addr_t loc_start;
900 u64 ibw_start;
901 struct tsi721_ib_win_mapping *map = NULL;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700902 int ret = -EBUSY;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700903
Alexandre Bounine9673b882016-03-22 14:25:54 -0700904 if (direct) {
905 dev_dbg(&priv->pdev->dev,
906 "Direct (RIO_0x%llx -> PCIe_0x%pad), size=0x%x",
907 rstart, &lstart, size);
Alexandre Bounine71afe342012-10-04 17:16:00 -0700908
Alexandre Bounine9673b882016-03-22 14:25:54 -0700909 /* Calculate minimal acceptable window size and base address */
910
911 ibw_size = roundup_pow_of_two(size);
912 ibw_start = lstart & ~(ibw_size - 1);
913
914 while ((lstart + size) > (ibw_start + ibw_size)) {
915 ibw_size *= 2;
916 ibw_start = lstart & ~(ibw_size - 1);
917 if (ibw_size > 0x80000000) { /* Limit max size to 2GB */
918 return -EBUSY;
919 }
920 }
921
922 loc_start = ibw_start;
923
924 map = kzalloc(sizeof(struct tsi721_ib_win_mapping), GFP_ATOMIC);
925 if (map == NULL)
926 return -ENOMEM;
927
928 } else {
929 dev_dbg(&priv->pdev->dev,
930 "Translated (RIO_0x%llx -> PCIe_0x%pad), size=0x%x",
931 rstart, &lstart, size);
932
933 if (!is_power_of_2(size) || size < 0x1000 ||
934 ((u64)lstart & (size - 1)) || (rstart & (size - 1)))
935 return -EINVAL;
936 if (priv->ibwin_cnt == 0)
937 return -EBUSY;
938 ibw_start = rstart;
939 ibw_size = size;
940 loc_start = lstart;
941 }
942
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700943 /*
944 * Scan for overlapping with active regions and mark the first available
945 * IB window at the same time.
946 */
Alexandre Bounine71afe342012-10-04 17:16:00 -0700947 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700948 ib_win = &priv->ib_win[i];
Alexandre Bounine9673b882016-03-22 14:25:54 -0700949
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700950 if (!ib_win->active) {
951 if (avail == -1) {
952 avail = i;
953 ret = 0;
954 }
Alexandre Bounine9673b882016-03-22 14:25:54 -0700955 } else if (ibw_start < (ib_win->rstart + ib_win->size) &&
956 (ibw_start + ibw_size) > ib_win->rstart) {
957 /* Return error if address translation involved */
958 if (direct && ib_win->xlat) {
959 ret = -EFAULT;
960 break;
961 }
962
963 /*
964 * Direct mappings usually are larger than originally
965 * requested fragments - check if this new request fits
966 * into it.
967 */
968 if (rstart >= ib_win->rstart &&
969 (rstart + size) <= (ib_win->rstart +
970 ib_win->size)) {
971 /* We are in - no further mapping required */
972 map->lstart = lstart;
973 list_add_tail(&map->node, &ib_win->mappings);
974 return 0;
975 }
976
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700977 ret = -EFAULT;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700978 break;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700979 }
Alexandre Bounine71afe342012-10-04 17:16:00 -0700980 }
981
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700982 if (ret)
Alexandre Bounine9673b882016-03-22 14:25:54 -0700983 goto out;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700984 i = avail;
985
986 /* Sanity check: available IB window must be disabled at this point */
987 regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
988 if (WARN_ON(regval & TSI721_IBWIN_LB_WEN)) {
989 ret = -EIO;
Alexandre Bounine9673b882016-03-22 14:25:54 -0700990 goto out;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700991 }
992
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700993 ib_win = &priv->ib_win[i];
994 ib_win->active = true;
Alexandre Bounine9673b882016-03-22 14:25:54 -0700995 ib_win->rstart = ibw_start;
996 ib_win->lstart = loc_start;
997 ib_win->size = ibw_size;
998 ib_win->xlat = (lstart != rstart);
999 INIT_LIST_HEAD(&ib_win->mappings);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001000
Alexandre Bounine9673b882016-03-22 14:25:54 -07001001 /*
1002 * When using direct IBW mapping and have larger than requested IBW size
1003 * we can have multiple local memory blocks mapped through the same IBW
1004 * To handle this situation we maintain list of "clients" for such IBWs.
1005 */
1006 if (direct) {
1007 map->lstart = lstart;
1008 list_add_tail(&map->node, &ib_win->mappings);
1009 }
1010
1011 iowrite32(TSI721_IBWIN_SIZE(ibw_size) << 8,
Alexandre Bounine71afe342012-10-04 17:16:00 -07001012 priv->regs + TSI721_IBWIN_SZ(i));
1013
Alexandre Bounine9673b882016-03-22 14:25:54 -07001014 iowrite32(((u64)loc_start >> 32), priv->regs + TSI721_IBWIN_TUA(i));
1015 iowrite32(((u64)loc_start & TSI721_IBWIN_TLA_ADD),
Alexandre Bounine71afe342012-10-04 17:16:00 -07001016 priv->regs + TSI721_IBWIN_TLA(i));
1017
Alexandre Bounine9673b882016-03-22 14:25:54 -07001018 iowrite32(ibw_start >> 32, priv->regs + TSI721_IBWIN_UB(i));
1019 iowrite32((ibw_start & TSI721_IBWIN_LB_BA) | TSI721_IBWIN_LB_WEN,
Alexandre Bounine71afe342012-10-04 17:16:00 -07001020 priv->regs + TSI721_IBWIN_LB(i));
Alexandre Bounine9673b882016-03-22 14:25:54 -07001021
1022 priv->ibwin_cnt--;
1023
Alexandre Bounine71afe342012-10-04 17:16:00 -07001024 dev_dbg(&priv->pdev->dev,
Alexandre Bounine9673b882016-03-22 14:25:54 -07001025 "Configured IBWIN%d (RIO_0x%llx -> PCIe_0x%llx), size=0x%llx\n",
1026 i, ibw_start, (unsigned long long)loc_start, ibw_size);
Alexandre Bounine71afe342012-10-04 17:16:00 -07001027
1028 return 0;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001029out:
1030 kfree(map);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001031 return ret;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001032}
1033
1034/**
Alexandre Bounine9673b882016-03-22 14:25:54 -07001035 * tsi721_rio_unmap_inb_mem -- Unmapping inbound memory region.
Alexandre Bounine71afe342012-10-04 17:16:00 -07001036 * @mport: RapidIO master port
1037 * @lstart: Local memory space start address.
1038 */
1039static void tsi721_rio_unmap_inb_mem(struct rio_mport *mport,
1040 dma_addr_t lstart)
1041{
1042 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001043 struct tsi721_ib_win *ib_win;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001044 int i;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001045
Alexandre Bounine9673b882016-03-22 14:25:54 -07001046 dev_dbg(&priv->pdev->dev,
1047 "Unmap IBW mapped to PCIe_0x%pad", &lstart);
1048
Alexandre Bounine71afe342012-10-04 17:16:00 -07001049 /* Search for matching active inbound translation window */
1050 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001051 ib_win = &priv->ib_win[i];
Alexandre Bounine9673b882016-03-22 14:25:54 -07001052
1053 /* Address translating IBWs must to be an exact march */
1054 if (!ib_win->active ||
1055 (ib_win->xlat && lstart != ib_win->lstart))
1056 continue;
1057
1058 if (lstart >= ib_win->lstart &&
1059 lstart < (ib_win->lstart + ib_win->size)) {
1060
1061 if (!ib_win->xlat) {
1062 struct tsi721_ib_win_mapping *map;
1063 int found = 0;
1064
1065 list_for_each_entry(map,
1066 &ib_win->mappings, node) {
1067 if (map->lstart == lstart) {
1068 list_del(&map->node);
1069 kfree(map);
1070 found = 1;
1071 break;
1072 }
1073 }
1074
1075 if (!found)
1076 continue;
1077
1078 if (!list_empty(&ib_win->mappings))
1079 break;
1080 }
1081
1082 dev_dbg(&priv->pdev->dev, "Disable IBWIN_%d", i);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001083 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1084 ib_win->active = false;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001085 priv->ibwin_cnt++;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001086 break;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001087 }
1088 }
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001089
1090 if (i == TSI721_IBWIN_NUM)
Alexandre Bounine9673b882016-03-22 14:25:54 -07001091 dev_dbg(&priv->pdev->dev,
1092 "IB window mapped to %pad not found", &lstart);
Alexandre Bounine71afe342012-10-04 17:16:00 -07001093}
1094
1095/**
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001096 * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
1097 * translation regions.
1098 * @priv: pointer to tsi721 private data
1099 *
1100 * Disables inbound windows.
1101 */
1102static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
1103{
1104 int i;
1105
1106 /* Disable all SR2PC inbound windows */
1107 for (i = 0; i < TSI721_IBWIN_NUM; i++)
Alexandre Bounine71afe342012-10-04 17:16:00 -07001108 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
Alexandre Bounine9673b882016-03-22 14:25:54 -07001109 priv->ibwin_cnt = TSI721_IBWIN_NUM;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001110}
1111
Alexandre Bounine748353c2016-03-22 14:26:23 -07001112/*
1113 * tsi721_close_sr2pc_mapping - closes all active inbound (SRIO->PCIe)
1114 * translation regions.
1115 * @priv: pointer to tsi721 device private data
1116 */
1117static void tsi721_close_sr2pc_mapping(struct tsi721_device *priv)
1118{
1119 struct tsi721_ib_win *ib_win;
1120 int i;
1121
1122 /* Disable all active SR2PC inbound windows */
1123 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
1124 ib_win = &priv->ib_win[i];
1125 if (ib_win->active) {
1126 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1127 ib_win->active = false;
1128 }
1129 }
1130}
1131
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001132/**
1133 * tsi721_port_write_init - Inbound port write interface init
1134 * @priv: pointer to tsi721 private data
1135 *
1136 * Initializes inbound port write handler.
1137 * Returns %0 on success or %-ENOMEM on failure.
1138 */
1139static int tsi721_port_write_init(struct tsi721_device *priv)
1140{
1141 priv->pw_discard_count = 0;
1142 INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
1143 spin_lock_init(&priv->pw_fifo_lock);
1144 if (kfifo_alloc(&priv->pw_fifo,
1145 TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
1146 dev_err(&priv->pdev->dev, "PW FIFO allocation failed\n");
1147 return -ENOMEM;
1148 }
1149
1150 /* Use reliable port-write capture mode */
1151 iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
1152 return 0;
1153}
1154
Alexandre Bounine748353c2016-03-22 14:26:23 -07001155static void tsi721_port_write_free(struct tsi721_device *priv)
1156{
1157 kfifo_free(&priv->pw_fifo);
1158}
1159
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001160static int tsi721_doorbell_init(struct tsi721_device *priv)
1161{
1162 /* Outbound Doorbells do not require any setup.
1163 * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
1164 * That BAR1 was mapped during the probe routine.
1165 */
1166
1167 /* Initialize Inbound Doorbell processing DPC and queue */
1168 priv->db_discard_count = 0;
1169 INIT_WORK(&priv->idb_work, tsi721_db_dpc);
1170
1171 /* Allocate buffer for inbound doorbells queue */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001172 priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001173 IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1174 &priv->idb_dma, GFP_KERNEL);
1175 if (!priv->idb_base)
1176 return -ENOMEM;
1177
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001178 dev_dbg(&priv->pdev->dev, "Allocated IDB buffer @ %p (phys = %llx)\n",
1179 priv->idb_base, (unsigned long long)priv->idb_dma);
1180
1181 iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
1182 priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
1183 iowrite32(((u64)priv->idb_dma >> 32),
1184 priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
1185 iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
1186 priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
1187 /* Enable accepting all inbound doorbells */
1188 iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
1189
1190 iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
1191
1192 iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
1193
1194 return 0;
1195}
1196
1197static void tsi721_doorbell_free(struct tsi721_device *priv)
1198{
1199 if (priv->idb_base == NULL)
1200 return;
1201
1202 /* Free buffer allocated for inbound doorbell queue */
1203 dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1204 priv->idb_base, priv->idb_dma);
1205 priv->idb_base = NULL;
1206}
1207
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001208/**
1209 * tsi721_bdma_maint_init - Initialize maintenance request BDMA channel.
1210 * @priv: pointer to tsi721 private data
1211 *
1212 * Initialize BDMA channel allocated for RapidIO maintenance read/write
1213 * request generation
1214 * Returns %0 on success or %-ENOMEM on failure.
1215 */
1216static int tsi721_bdma_maint_init(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001217{
1218 struct tsi721_dma_desc *bd_ptr;
1219 u64 *sts_ptr;
1220 dma_addr_t bd_phys, sts_phys;
1221 int sts_size;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001222 int bd_num = 2;
1223 void __iomem *regs;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001224
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001225 dev_dbg(&priv->pdev->dev,
1226 "Init Block DMA Engine for Maintenance requests, CH%d\n",
1227 TSI721_DMACH_MAINT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001228
1229 /*
1230 * Initialize DMA channel for maintenance requests
1231 */
1232
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001233 priv->mdma.ch_id = TSI721_DMACH_MAINT;
1234 regs = priv->regs + TSI721_DMAC_BASE(TSI721_DMACH_MAINT);
1235
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001236 /* Allocate space for DMA descriptors */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001237 bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001238 bd_num * sizeof(struct tsi721_dma_desc),
1239 &bd_phys, GFP_KERNEL);
1240 if (!bd_ptr)
1241 return -ENOMEM;
1242
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001243 priv->mdma.bd_num = bd_num;
1244 priv->mdma.bd_phys = bd_phys;
1245 priv->mdma.bd_base = bd_ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001246
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001247 dev_dbg(&priv->pdev->dev, "DMA descriptors @ %p (phys = %llx)\n",
1248 bd_ptr, (unsigned long long)bd_phys);
1249
1250 /* Allocate space for descriptor status FIFO */
1251 sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
1252 bd_num : TSI721_DMA_MINSTSSZ;
1253 sts_size = roundup_pow_of_two(sts_size);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001254 sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001255 sts_size * sizeof(struct tsi721_dma_sts),
1256 &sts_phys, GFP_KERNEL);
1257 if (!sts_ptr) {
1258 /* Free space allocated for DMA descriptors */
1259 dma_free_coherent(&priv->pdev->dev,
1260 bd_num * sizeof(struct tsi721_dma_desc),
1261 bd_ptr, bd_phys);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001262 priv->mdma.bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001263 return -ENOMEM;
1264 }
1265
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001266 priv->mdma.sts_phys = sts_phys;
1267 priv->mdma.sts_base = sts_ptr;
1268 priv->mdma.sts_size = sts_size;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001269
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001270 dev_dbg(&priv->pdev->dev,
1271 "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
1272 sts_ptr, (unsigned long long)sts_phys, sts_size);
1273
1274 /* Initialize DMA descriptors ring */
1275 bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
1276 bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
1277 TSI721_DMAC_DPTRL_MASK);
1278 bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
1279
1280 /* Setup DMA descriptor pointers */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001281 iowrite32(((u64)bd_phys >> 32), regs + TSI721_DMAC_DPTRH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001282 iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001283 regs + TSI721_DMAC_DPTRL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001284
1285 /* Setup descriptor status FIFO */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001286 iowrite32(((u64)sts_phys >> 32), regs + TSI721_DMAC_DSBH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001287 iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001288 regs + TSI721_DMAC_DSBL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001289 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001290 regs + TSI721_DMAC_DSSZ);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001291
1292 /* Clear interrupt bits */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001293 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001294
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001295 ioread32(regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001296
1297 /* Toggle DMA channel initialization */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001298 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
1299 ioread32(regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001300 udelay(10);
1301
1302 return 0;
1303}
1304
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001305static int tsi721_bdma_maint_free(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001306{
1307 u32 ch_stat;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001308 struct tsi721_bdma_maint *mdma = &priv->mdma;
1309 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(mdma->ch_id);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001310
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001311 if (mdma->bd_base == NULL)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001312 return 0;
1313
1314 /* Check if DMA channel still running */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001315 ch_stat = ioread32(regs + TSI721_DMAC_STS);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001316 if (ch_stat & TSI721_DMAC_STS_RUN)
1317 return -EFAULT;
1318
1319 /* Put DMA channel into init state */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001320 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001321
1322 /* Free space allocated for DMA descriptors */
1323 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001324 mdma->bd_num * sizeof(struct tsi721_dma_desc),
1325 mdma->bd_base, mdma->bd_phys);
1326 mdma->bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001327
1328 /* Free space allocated for status FIFO */
1329 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001330 mdma->sts_size * sizeof(struct tsi721_dma_sts),
1331 mdma->sts_base, mdma->sts_phys);
1332 mdma->sts_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001333 return 0;
1334}
1335
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001336/* Enable Inbound Messaging Interrupts */
1337static void
1338tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
1339 u32 inte_mask)
1340{
1341 u32 rval;
1342
1343 if (!inte_mask)
1344 return;
1345
1346 /* Clear pending Inbound Messaging interrupts */
1347 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1348
1349 /* Enable Inbound Messaging interrupts */
1350 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1351 iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
1352
1353 if (priv->flags & TSI721_USING_MSIX)
1354 return; /* Finished if we are in MSI-X mode */
1355
1356 /*
1357 * For MSI and INTA interrupt signalling we need to enable next levels
1358 */
1359
1360 /* Enable Device Channel Interrupt */
1361 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1362 iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
1363 priv->regs + TSI721_DEV_CHAN_INTE);
1364}
1365
1366/* Disable Inbound Messaging Interrupts */
1367static void
1368tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
1369 u32 inte_mask)
1370{
1371 u32 rval;
1372
1373 if (!inte_mask)
1374 return;
1375
1376 /* Clear pending Inbound Messaging interrupts */
1377 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1378
1379 /* Disable Inbound Messaging interrupts */
1380 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1381 rval &= ~inte_mask;
1382 iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
1383
1384 if (priv->flags & TSI721_USING_MSIX)
1385 return; /* Finished if we are in MSI-X mode */
1386
1387 /*
1388 * For MSI and INTA interrupt signalling we need to disable next levels
1389 */
1390
1391 /* Disable Device Channel Interrupt */
1392 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1393 rval &= ~TSI721_INT_IMSG_CHAN(ch);
1394 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1395}
1396
1397/* Enable Outbound Messaging interrupts */
1398static void
1399tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
1400 u32 inte_mask)
1401{
1402 u32 rval;
1403
1404 if (!inte_mask)
1405 return;
1406
1407 /* Clear pending Outbound Messaging interrupts */
1408 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1409
1410 /* Enable Outbound Messaging channel interrupts */
1411 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1412 iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
1413
1414 if (priv->flags & TSI721_USING_MSIX)
1415 return; /* Finished if we are in MSI-X mode */
1416
1417 /*
1418 * For MSI and INTA interrupt signalling we need to enable next levels
1419 */
1420
1421 /* Enable Device Channel Interrupt */
1422 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1423 iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
1424 priv->regs + TSI721_DEV_CHAN_INTE);
1425}
1426
1427/* Disable Outbound Messaging interrupts */
1428static void
1429tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
1430 u32 inte_mask)
1431{
1432 u32 rval;
1433
1434 if (!inte_mask)
1435 return;
1436
1437 /* Clear pending Outbound Messaging interrupts */
1438 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1439
1440 /* Disable Outbound Messaging interrupts */
1441 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1442 rval &= ~inte_mask;
1443 iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
1444
1445 if (priv->flags & TSI721_USING_MSIX)
1446 return; /* Finished if we are in MSI-X mode */
1447
1448 /*
1449 * For MSI and INTA interrupt signalling we need to disable next levels
1450 */
1451
1452 /* Disable Device Channel Interrupt */
1453 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1454 rval &= ~TSI721_INT_OMSG_CHAN(ch);
1455 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1456}
1457
1458/**
1459 * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
1460 * @mport: Master port with outbound message queue
1461 * @rdev: Target of outbound message
1462 * @mbox: Outbound mailbox
1463 * @buffer: Message to add to outbound queue
1464 * @len: Length of message
1465 */
1466static int
1467tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
1468 void *buffer, size_t len)
1469{
1470 struct tsi721_device *priv = mport->priv;
1471 struct tsi721_omsg_desc *desc;
1472 u32 tx_slot;
1473
1474 if (!priv->omsg_init[mbox] ||
1475 len > TSI721_MSG_MAX_SIZE || len < 8)
1476 return -EINVAL;
1477
1478 tx_slot = priv->omsg_ring[mbox].tx_slot;
1479
1480 /* Copy copy message into transfer buffer */
1481 memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
1482
1483 if (len & 0x7)
1484 len += 8;
1485
1486 /* Build descriptor associated with buffer */
1487 desc = priv->omsg_ring[mbox].omd_base;
1488 desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
1489 if (tx_slot % 4 == 0)
1490 desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
1491
1492 desc[tx_slot].msg_info =
1493 cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
1494 (0xe << 12) | (len & 0xff8));
1495 desc[tx_slot].bufptr_lo =
1496 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
1497 0xffffffff);
1498 desc[tx_slot].bufptr_hi =
1499 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
1500
1501 priv->omsg_ring[mbox].wr_count++;
1502
1503 /* Go to next descriptor */
1504 if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
1505 priv->omsg_ring[mbox].tx_slot = 0;
1506 /* Move through the ring link descriptor at the end */
1507 priv->omsg_ring[mbox].wr_count++;
1508 }
1509
1510 mb();
1511
1512 /* Set new write count value */
1513 iowrite32(priv->omsg_ring[mbox].wr_count,
1514 priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1515 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1516
1517 return 0;
1518}
1519
1520/**
1521 * tsi721_omsg_handler - Outbound Message Interrupt Handler
1522 * @priv: pointer to tsi721 private data
1523 * @ch: number of OB MSG channel to service
1524 *
1525 * Services channel interrupts from outbound messaging engine.
1526 */
1527static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
1528{
1529 u32 omsg_int;
Alexandre Bounine748353c2016-03-22 14:26:23 -07001530 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001531
1532 spin_lock(&priv->omsg_ring[ch].lock);
1533
1534 omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
1535
1536 if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
1537 dev_info(&priv->pdev->dev,
1538 "OB MBOX%d: Status FIFO is full\n", ch);
1539
1540 if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
1541 u32 srd_ptr;
1542 u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
1543 int i, j;
1544 u32 tx_slot;
1545
1546 /*
1547 * Find last successfully processed descriptor
1548 */
1549
1550 /* Check and clear descriptor status FIFO entries */
1551 srd_ptr = priv->omsg_ring[ch].sts_rdptr;
1552 sts_ptr = priv->omsg_ring[ch].sts_base;
1553 j = srd_ptr * 8;
1554 while (sts_ptr[j]) {
1555 for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
1556 prev_ptr = last_ptr;
1557 last_ptr = le64_to_cpu(sts_ptr[j]);
1558 sts_ptr[j] = 0;
1559 }
1560
1561 ++srd_ptr;
1562 srd_ptr %= priv->omsg_ring[ch].sts_size;
1563 j = srd_ptr * 8;
1564 }
1565
1566 if (last_ptr == 0)
1567 goto no_sts_update;
1568
1569 priv->omsg_ring[ch].sts_rdptr = srd_ptr;
1570 iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
1571
Alexandre Bounine748353c2016-03-22 14:26:23 -07001572 if (!mport->outb_msg[ch].mcback)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001573 goto no_sts_update;
1574
1575 /* Inform upper layer about transfer completion */
1576
1577 tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
1578 sizeof(struct tsi721_omsg_desc);
1579
1580 /*
1581 * Check if this is a Link Descriptor (LD).
1582 * If yes, ignore LD and use descriptor processed
1583 * before LD.
1584 */
1585 if (tx_slot == priv->omsg_ring[ch].size) {
1586 if (prev_ptr)
1587 tx_slot = (prev_ptr -
1588 (u64)priv->omsg_ring[ch].omd_phys)/
1589 sizeof(struct tsi721_omsg_desc);
1590 else
1591 goto no_sts_update;
1592 }
1593
1594 /* Move slot index to the next message to be sent */
1595 ++tx_slot;
1596 if (tx_slot == priv->omsg_ring[ch].size)
1597 tx_slot = 0;
1598 BUG_ON(tx_slot >= priv->omsg_ring[ch].size);
Alexandre Bounine748353c2016-03-22 14:26:23 -07001599 mport->outb_msg[ch].mcback(mport,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001600 priv->omsg_ring[ch].dev_id, ch,
1601 tx_slot);
1602 }
1603
1604no_sts_update:
1605
1606 if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
1607 /*
1608 * Outbound message operation aborted due to error,
1609 * reinitialize OB MSG channel
1610 */
1611
1612 dev_dbg(&priv->pdev->dev, "OB MSG ABORT ch_stat=%x\n",
1613 ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
1614
1615 iowrite32(TSI721_OBDMAC_INT_ERROR,
1616 priv->regs + TSI721_OBDMAC_INT(ch));
1617 iowrite32(TSI721_OBDMAC_CTL_INIT,
1618 priv->regs + TSI721_OBDMAC_CTL(ch));
1619 ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
1620
1621 /* Inform upper level to clear all pending tx slots */
Alexandre Bounine748353c2016-03-22 14:26:23 -07001622 if (mport->outb_msg[ch].mcback)
1623 mport->outb_msg[ch].mcback(mport,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001624 priv->omsg_ring[ch].dev_id, ch,
1625 priv->omsg_ring[ch].tx_slot);
1626 /* Synch tx_slot tracking */
1627 iowrite32(priv->omsg_ring[ch].tx_slot,
1628 priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1629 ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1630 priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
1631 priv->omsg_ring[ch].sts_rdptr = 0;
1632 }
1633
1634 /* Clear channel interrupts */
1635 iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
1636
1637 if (!(priv->flags & TSI721_USING_MSIX)) {
1638 u32 ch_inte;
1639
1640 /* Re-enable channel interrupts */
1641 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1642 ch_inte |= TSI721_INT_OMSG_CHAN(ch);
1643 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1644 }
1645
1646 spin_unlock(&priv->omsg_ring[ch].lock);
1647}
1648
1649/**
1650 * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
1651 * @mport: Master port implementing Outbound Messaging Engine
1652 * @dev_id: Device specific pointer to pass on event
1653 * @mbox: Mailbox to open
1654 * @entries: Number of entries in the outbound mailbox ring
1655 */
1656static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
1657 int mbox, int entries)
1658{
1659 struct tsi721_device *priv = mport->priv;
1660 struct tsi721_omsg_desc *bd_ptr;
1661 int i, rc = 0;
1662
1663 if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
1664 (entries > (TSI721_OMSGD_RING_SIZE)) ||
1665 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1666 rc = -EINVAL;
1667 goto out;
1668 }
1669
1670 priv->omsg_ring[mbox].dev_id = dev_id;
1671 priv->omsg_ring[mbox].size = entries;
1672 priv->omsg_ring[mbox].sts_rdptr = 0;
1673 spin_lock_init(&priv->omsg_ring[mbox].lock);
1674
1675 /* Outbound Msg Buffer allocation based on
1676 the number of maximum descriptor entries */
1677 for (i = 0; i < entries; i++) {
1678 priv->omsg_ring[mbox].omq_base[i] =
1679 dma_alloc_coherent(
1680 &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
1681 &priv->omsg_ring[mbox].omq_phys[i],
1682 GFP_KERNEL);
1683 if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
1684 dev_dbg(&priv->pdev->dev,
1685 "Unable to allocate OB MSG data buffer for"
1686 " MBOX%d\n", mbox);
1687 rc = -ENOMEM;
1688 goto out_buf;
1689 }
1690 }
1691
1692 /* Outbound message descriptor allocation */
1693 priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
1694 &priv->pdev->dev,
1695 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1696 &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
1697 if (priv->omsg_ring[mbox].omd_base == NULL) {
1698 dev_dbg(&priv->pdev->dev,
1699 "Unable to allocate OB MSG descriptor memory "
1700 "for MBOX%d\n", mbox);
1701 rc = -ENOMEM;
1702 goto out_buf;
1703 }
1704
1705 priv->omsg_ring[mbox].tx_slot = 0;
1706
1707 /* Outbound message descriptor status FIFO allocation */
1708 priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001709 priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001710 priv->omsg_ring[mbox].sts_size *
1711 sizeof(struct tsi721_dma_sts),
1712 &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
1713 if (priv->omsg_ring[mbox].sts_base == NULL) {
1714 dev_dbg(&priv->pdev->dev,
1715 "Unable to allocate OB MSG descriptor status FIFO "
1716 "for MBOX%d\n", mbox);
1717 rc = -ENOMEM;
1718 goto out_desc;
1719 }
1720
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001721 /*
1722 * Configure Outbound Messaging Engine
1723 */
1724
1725 /* Setup Outbound Message descriptor pointer */
1726 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
1727 priv->regs + TSI721_OBDMAC_DPTRH(mbox));
1728 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
1729 TSI721_OBDMAC_DPTRL_MASK),
1730 priv->regs + TSI721_OBDMAC_DPTRL(mbox));
1731
1732 /* Setup Outbound Message descriptor status FIFO */
1733 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
1734 priv->regs + TSI721_OBDMAC_DSBH(mbox));
1735 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
1736 TSI721_OBDMAC_DSBL_MASK),
1737 priv->regs + TSI721_OBDMAC_DSBL(mbox));
1738 iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
1739 priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
1740
1741 /* Enable interrupts */
1742
1743#ifdef CONFIG_PCI_MSI
1744 if (priv->flags & TSI721_USING_MSIX) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07001745 int idx = TSI721_VECT_OMB0_DONE + mbox;
1746
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001747 /* Request interrupt service if we are in MSI-X mode */
Alexandre Bounine748353c2016-03-22 14:26:23 -07001748 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1749 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001750
1751 if (rc) {
1752 dev_dbg(&priv->pdev->dev,
1753 "Unable to allocate MSI-X interrupt for "
1754 "OBOX%d-DONE\n", mbox);
1755 goto out_stat;
1756 }
1757
Alexandre Bounine748353c2016-03-22 14:26:23 -07001758 idx = TSI721_VECT_OMB0_INT + mbox;
1759 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1760 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001761
1762 if (rc) {
1763 dev_dbg(&priv->pdev->dev,
1764 "Unable to allocate MSI-X interrupt for "
1765 "MBOX%d-INT\n", mbox);
Alexandre Bounine748353c2016-03-22 14:26:23 -07001766 idx = TSI721_VECT_OMB0_DONE + mbox;
1767 free_irq(priv->msix[idx].vector, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001768 goto out_stat;
1769 }
1770 }
1771#endif /* CONFIG_PCI_MSI */
1772
1773 tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1774
1775 /* Initialize Outbound Message descriptors ring */
1776 bd_ptr = priv->omsg_ring[mbox].omd_base;
1777 bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
1778 bd_ptr[entries].msg_info = 0;
1779 bd_ptr[entries].next_lo =
1780 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
1781 TSI721_OBDMAC_DPTRL_MASK);
1782 bd_ptr[entries].next_hi =
1783 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
1784 priv->omsg_ring[mbox].wr_count = 0;
1785 mb();
1786
1787 /* Initialize Outbound Message engine */
1788 iowrite32(TSI721_OBDMAC_CTL_INIT, priv->regs + TSI721_OBDMAC_CTL(mbox));
1789 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1790 udelay(10);
1791
1792 priv->omsg_init[mbox] = 1;
1793
1794 return 0;
1795
1796#ifdef CONFIG_PCI_MSI
1797out_stat:
1798 dma_free_coherent(&priv->pdev->dev,
1799 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1800 priv->omsg_ring[mbox].sts_base,
1801 priv->omsg_ring[mbox].sts_phys);
1802
1803 priv->omsg_ring[mbox].sts_base = NULL;
1804#endif /* CONFIG_PCI_MSI */
1805
1806out_desc:
1807 dma_free_coherent(&priv->pdev->dev,
1808 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1809 priv->omsg_ring[mbox].omd_base,
1810 priv->omsg_ring[mbox].omd_phys);
1811
1812 priv->omsg_ring[mbox].omd_base = NULL;
1813
1814out_buf:
1815 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1816 if (priv->omsg_ring[mbox].omq_base[i]) {
1817 dma_free_coherent(&priv->pdev->dev,
1818 TSI721_MSG_BUFFER_SIZE,
1819 priv->omsg_ring[mbox].omq_base[i],
1820 priv->omsg_ring[mbox].omq_phys[i]);
1821
1822 priv->omsg_ring[mbox].omq_base[i] = NULL;
1823 }
1824 }
1825
1826out:
1827 return rc;
1828}
1829
1830/**
1831 * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
1832 * @mport: Master port implementing the outbound message unit
1833 * @mbox: Mailbox to close
1834 */
1835static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
1836{
1837 struct tsi721_device *priv = mport->priv;
1838 u32 i;
1839
1840 if (!priv->omsg_init[mbox])
1841 return;
1842 priv->omsg_init[mbox] = 0;
1843
1844 /* Disable Interrupts */
1845
1846 tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1847
1848#ifdef CONFIG_PCI_MSI
1849 if (priv->flags & TSI721_USING_MSIX) {
1850 free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07001851 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001852 free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07001853 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001854 }
1855#endif /* CONFIG_PCI_MSI */
1856
1857 /* Free OMSG Descriptor Status FIFO */
1858 dma_free_coherent(&priv->pdev->dev,
1859 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1860 priv->omsg_ring[mbox].sts_base,
1861 priv->omsg_ring[mbox].sts_phys);
1862
1863 priv->omsg_ring[mbox].sts_base = NULL;
1864
1865 /* Free OMSG descriptors */
1866 dma_free_coherent(&priv->pdev->dev,
1867 (priv->omsg_ring[mbox].size + 1) *
1868 sizeof(struct tsi721_omsg_desc),
1869 priv->omsg_ring[mbox].omd_base,
1870 priv->omsg_ring[mbox].omd_phys);
1871
1872 priv->omsg_ring[mbox].omd_base = NULL;
1873
1874 /* Free message buffers */
1875 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1876 if (priv->omsg_ring[mbox].omq_base[i]) {
1877 dma_free_coherent(&priv->pdev->dev,
1878 TSI721_MSG_BUFFER_SIZE,
1879 priv->omsg_ring[mbox].omq_base[i],
1880 priv->omsg_ring[mbox].omq_phys[i]);
1881
1882 priv->omsg_ring[mbox].omq_base[i] = NULL;
1883 }
1884 }
1885}
1886
1887/**
1888 * tsi721_imsg_handler - Inbound Message Interrupt Handler
1889 * @priv: pointer to tsi721 private data
1890 * @ch: inbound message channel number to service
1891 *
1892 * Services channel interrupts from inbound messaging engine.
1893 */
1894static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
1895{
1896 u32 mbox = ch - 4;
1897 u32 imsg_int;
Alexandre Bounine748353c2016-03-22 14:26:23 -07001898 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001899
1900 spin_lock(&priv->imsg_ring[mbox].lock);
1901
1902 imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
1903
1904 if (imsg_int & TSI721_IBDMAC_INT_SRTO)
1905 dev_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout\n",
1906 mbox);
1907
1908 if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
1909 dev_info(&priv->pdev->dev, "IB MBOX%d PCIe error\n",
1910 mbox);
1911
1912 if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
1913 dev_info(&priv->pdev->dev,
1914 "IB MBOX%d IB free queue low\n", mbox);
1915
1916 /* Clear IB channel interrupts */
1917 iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
1918
1919 /* If an IB Msg is received notify the upper layer */
1920 if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
Alexandre Bounine748353c2016-03-22 14:26:23 -07001921 mport->inb_msg[mbox].mcback)
1922 mport->inb_msg[mbox].mcback(mport,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001923 priv->imsg_ring[mbox].dev_id, mbox, -1);
1924
1925 if (!(priv->flags & TSI721_USING_MSIX)) {
1926 u32 ch_inte;
1927
1928 /* Re-enable channel interrupts */
1929 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1930 ch_inte |= TSI721_INT_IMSG_CHAN(ch);
1931 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1932 }
1933
1934 spin_unlock(&priv->imsg_ring[mbox].lock);
1935}
1936
1937/**
1938 * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
1939 * @mport: Master port implementing the Inbound Messaging Engine
1940 * @dev_id: Device specific pointer to pass on event
1941 * @mbox: Mailbox to open
1942 * @entries: Number of entries in the inbound mailbox ring
1943 */
1944static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
1945 int mbox, int entries)
1946{
1947 struct tsi721_device *priv = mport->priv;
1948 int ch = mbox + 4;
1949 int i;
1950 u64 *free_ptr;
1951 int rc = 0;
1952
1953 if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
1954 (entries > TSI721_IMSGD_RING_SIZE) ||
1955 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1956 rc = -EINVAL;
1957 goto out;
1958 }
1959
1960 /* Initialize IB Messaging Ring */
1961 priv->imsg_ring[mbox].dev_id = dev_id;
1962 priv->imsg_ring[mbox].size = entries;
1963 priv->imsg_ring[mbox].rx_slot = 0;
1964 priv->imsg_ring[mbox].desc_rdptr = 0;
1965 priv->imsg_ring[mbox].fq_wrptr = 0;
1966 for (i = 0; i < priv->imsg_ring[mbox].size; i++)
1967 priv->imsg_ring[mbox].imq_base[i] = NULL;
1968 spin_lock_init(&priv->imsg_ring[mbox].lock);
1969
1970 /* Allocate buffers for incoming messages */
1971 priv->imsg_ring[mbox].buf_base =
1972 dma_alloc_coherent(&priv->pdev->dev,
1973 entries * TSI721_MSG_BUFFER_SIZE,
1974 &priv->imsg_ring[mbox].buf_phys,
1975 GFP_KERNEL);
1976
1977 if (priv->imsg_ring[mbox].buf_base == NULL) {
1978 dev_err(&priv->pdev->dev,
1979 "Failed to allocate buffers for IB MBOX%d\n", mbox);
1980 rc = -ENOMEM;
1981 goto out;
1982 }
1983
1984 /* Allocate memory for circular free list */
1985 priv->imsg_ring[mbox].imfq_base =
1986 dma_alloc_coherent(&priv->pdev->dev,
1987 entries * 8,
1988 &priv->imsg_ring[mbox].imfq_phys,
1989 GFP_KERNEL);
1990
1991 if (priv->imsg_ring[mbox].imfq_base == NULL) {
1992 dev_err(&priv->pdev->dev,
1993 "Failed to allocate free queue for IB MBOX%d\n", mbox);
1994 rc = -ENOMEM;
1995 goto out_buf;
1996 }
1997
1998 /* Allocate memory for Inbound message descriptors */
1999 priv->imsg_ring[mbox].imd_base =
2000 dma_alloc_coherent(&priv->pdev->dev,
2001 entries * sizeof(struct tsi721_imsg_desc),
2002 &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
2003
2004 if (priv->imsg_ring[mbox].imd_base == NULL) {
2005 dev_err(&priv->pdev->dev,
2006 "Failed to allocate descriptor memory for IB MBOX%d\n",
2007 mbox);
2008 rc = -ENOMEM;
2009 goto out_dma;
2010 }
2011
2012 /* Fill free buffer pointer list */
2013 free_ptr = priv->imsg_ring[mbox].imfq_base;
2014 for (i = 0; i < entries; i++)
2015 free_ptr[i] = cpu_to_le64(
2016 (u64)(priv->imsg_ring[mbox].buf_phys) +
2017 i * 0x1000);
2018
2019 mb();
2020
2021 /*
2022 * For mapping of inbound SRIO Messages into appropriate queues we need
2023 * to set Inbound Device ID register in the messaging engine. We do it
2024 * once when first inbound mailbox is requested.
2025 */
2026 if (!(priv->flags & TSI721_IMSGID_SET)) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07002027 iowrite32((u32)priv->mport.host_deviceid,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002028 priv->regs + TSI721_IB_DEVID);
2029 priv->flags |= TSI721_IMSGID_SET;
2030 }
2031
2032 /*
2033 * Configure Inbound Messaging channel (ch = mbox + 4)
2034 */
2035
2036 /* Setup Inbound Message free queue */
2037 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
2038 priv->regs + TSI721_IBDMAC_FQBH(ch));
2039 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
2040 TSI721_IBDMAC_FQBL_MASK),
2041 priv->regs+TSI721_IBDMAC_FQBL(ch));
2042 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2043 priv->regs + TSI721_IBDMAC_FQSZ(ch));
2044
2045 /* Setup Inbound Message descriptor queue */
2046 iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
2047 priv->regs + TSI721_IBDMAC_DQBH(ch));
2048 iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
2049 (u32)TSI721_IBDMAC_DQBL_MASK),
2050 priv->regs+TSI721_IBDMAC_DQBL(ch));
2051 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2052 priv->regs + TSI721_IBDMAC_DQSZ(ch));
2053
2054 /* Enable interrupts */
2055
2056#ifdef CONFIG_PCI_MSI
2057 if (priv->flags & TSI721_USING_MSIX) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07002058 int idx = TSI721_VECT_IMB0_RCV + mbox;
2059
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002060 /* Request interrupt service if we are in MSI-X mode */
Alexandre Bounine748353c2016-03-22 14:26:23 -07002061 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2062 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002063
2064 if (rc) {
2065 dev_dbg(&priv->pdev->dev,
2066 "Unable to allocate MSI-X interrupt for "
2067 "IBOX%d-DONE\n", mbox);
2068 goto out_desc;
2069 }
2070
Alexandre Bounine748353c2016-03-22 14:26:23 -07002071 idx = TSI721_VECT_IMB0_INT + mbox;
2072 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2073 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002074
2075 if (rc) {
2076 dev_dbg(&priv->pdev->dev,
2077 "Unable to allocate MSI-X interrupt for "
2078 "IBOX%d-INT\n", mbox);
2079 free_irq(
2080 priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002081 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002082 goto out_desc;
2083 }
2084 }
2085#endif /* CONFIG_PCI_MSI */
2086
2087 tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
2088
2089 /* Initialize Inbound Message Engine */
2090 iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
2091 ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
2092 udelay(10);
2093 priv->imsg_ring[mbox].fq_wrptr = entries - 1;
2094 iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
2095
2096 priv->imsg_init[mbox] = 1;
2097 return 0;
2098
2099#ifdef CONFIG_PCI_MSI
2100out_desc:
2101 dma_free_coherent(&priv->pdev->dev,
2102 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2103 priv->imsg_ring[mbox].imd_base,
2104 priv->imsg_ring[mbox].imd_phys);
2105
2106 priv->imsg_ring[mbox].imd_base = NULL;
2107#endif /* CONFIG_PCI_MSI */
2108
2109out_dma:
2110 dma_free_coherent(&priv->pdev->dev,
2111 priv->imsg_ring[mbox].size * 8,
2112 priv->imsg_ring[mbox].imfq_base,
2113 priv->imsg_ring[mbox].imfq_phys);
2114
2115 priv->imsg_ring[mbox].imfq_base = NULL;
2116
2117out_buf:
2118 dma_free_coherent(&priv->pdev->dev,
2119 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2120 priv->imsg_ring[mbox].buf_base,
2121 priv->imsg_ring[mbox].buf_phys);
2122
2123 priv->imsg_ring[mbox].buf_base = NULL;
2124
2125out:
2126 return rc;
2127}
2128
2129/**
2130 * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
2131 * @mport: Master port implementing the Inbound Messaging Engine
2132 * @mbox: Mailbox to close
2133 */
2134static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
2135{
2136 struct tsi721_device *priv = mport->priv;
2137 u32 rx_slot;
2138 int ch = mbox + 4;
2139
2140 if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
2141 return;
2142 priv->imsg_init[mbox] = 0;
2143
2144 /* Disable Inbound Messaging Engine */
2145
2146 /* Disable Interrupts */
2147 tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
2148
2149#ifdef CONFIG_PCI_MSI
2150 if (priv->flags & TSI721_USING_MSIX) {
2151 free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002152 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002153 free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002154 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002155 }
2156#endif /* CONFIG_PCI_MSI */
2157
2158 /* Clear Inbound Buffer Queue */
2159 for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
2160 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2161
2162 /* Free memory allocated for message buffers */
2163 dma_free_coherent(&priv->pdev->dev,
2164 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2165 priv->imsg_ring[mbox].buf_base,
2166 priv->imsg_ring[mbox].buf_phys);
2167
2168 priv->imsg_ring[mbox].buf_base = NULL;
2169
2170 /* Free memory allocated for free pointr list */
2171 dma_free_coherent(&priv->pdev->dev,
2172 priv->imsg_ring[mbox].size * 8,
2173 priv->imsg_ring[mbox].imfq_base,
2174 priv->imsg_ring[mbox].imfq_phys);
2175
2176 priv->imsg_ring[mbox].imfq_base = NULL;
2177
2178 /* Free memory allocated for RX descriptors */
2179 dma_free_coherent(&priv->pdev->dev,
2180 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2181 priv->imsg_ring[mbox].imd_base,
2182 priv->imsg_ring[mbox].imd_phys);
2183
2184 priv->imsg_ring[mbox].imd_base = NULL;
2185}
2186
2187/**
2188 * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
2189 * @mport: Master port implementing the Inbound Messaging Engine
2190 * @mbox: Inbound mailbox number
2191 * @buf: Buffer to add to inbound queue
2192 */
2193static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
2194{
2195 struct tsi721_device *priv = mport->priv;
2196 u32 rx_slot;
2197 int rc = 0;
2198
2199 rx_slot = priv->imsg_ring[mbox].rx_slot;
2200 if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
2201 dev_err(&priv->pdev->dev,
2202 "Error adding inbound buffer %d, buffer exists\n",
2203 rx_slot);
2204 rc = -EINVAL;
2205 goto out;
2206 }
2207
2208 priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
2209
2210 if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
2211 priv->imsg_ring[mbox].rx_slot = 0;
2212
2213out:
2214 return rc;
2215}
2216
2217/**
2218 * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
2219 * @mport: Master port implementing the Inbound Messaging Engine
2220 * @mbox: Inbound mailbox number
2221 *
2222 * Returns pointer to the message on success or NULL on failure.
2223 */
2224static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
2225{
2226 struct tsi721_device *priv = mport->priv;
2227 struct tsi721_imsg_desc *desc;
2228 u32 rx_slot;
2229 void *rx_virt = NULL;
2230 u64 rx_phys;
2231 void *buf = NULL;
2232 u64 *free_ptr;
2233 int ch = mbox + 4;
2234 int msg_size;
2235
2236 if (!priv->imsg_init[mbox])
2237 return NULL;
2238
2239 desc = priv->imsg_ring[mbox].imd_base;
2240 desc += priv->imsg_ring[mbox].desc_rdptr;
2241
2242 if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
2243 goto out;
2244
2245 rx_slot = priv->imsg_ring[mbox].rx_slot;
2246 while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
2247 if (++rx_slot == priv->imsg_ring[mbox].size)
2248 rx_slot = 0;
2249 }
2250
2251 rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
2252 le32_to_cpu(desc->bufptr_lo);
2253
2254 rx_virt = priv->imsg_ring[mbox].buf_base +
2255 (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
2256
2257 buf = priv->imsg_ring[mbox].imq_base[rx_slot];
2258 msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
2259 if (msg_size == 0)
2260 msg_size = RIO_MAX_MSG_SIZE;
2261
2262 memcpy(buf, rx_virt, msg_size);
2263 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2264
2265 desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
2266 if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
2267 priv->imsg_ring[mbox].desc_rdptr = 0;
2268
2269 iowrite32(priv->imsg_ring[mbox].desc_rdptr,
2270 priv->regs + TSI721_IBDMAC_DQRP(ch));
2271
2272 /* Return free buffer into the pointer list */
2273 free_ptr = priv->imsg_ring[mbox].imfq_base;
2274 free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
2275
2276 if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
2277 priv->imsg_ring[mbox].fq_wrptr = 0;
2278
2279 iowrite32(priv->imsg_ring[mbox].fq_wrptr,
2280 priv->regs + TSI721_IBDMAC_FQWP(ch));
2281out:
2282 return buf;
2283}
2284
2285/**
2286 * tsi721_messages_init - Initialization of Messaging Engine
2287 * @priv: pointer to tsi721 private data
2288 *
2289 * Configures Tsi721 messaging engine.
2290 */
2291static int tsi721_messages_init(struct tsi721_device *priv)
2292{
2293 int ch;
2294
2295 iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
2296 iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
2297 iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
2298
2299 /* Set SRIO Message Request/Response Timeout */
2300 iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
2301
2302 /* Initialize Inbound Messaging Engine Registers */
2303 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
2304 /* Clear interrupt bits */
2305 iowrite32(TSI721_IBDMAC_INT_MASK,
2306 priv->regs + TSI721_IBDMAC_INT(ch));
2307 /* Clear Status */
2308 iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
2309
2310 iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
2311 priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
2312 iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
2313 priv->regs + TSI721_SMSG_ECC_NCOR(ch));
2314 }
2315
2316 return 0;
2317}
2318
2319/**
Alexandre Bouninedbe74af2016-03-22 14:26:02 -07002320 * tsi721_query_mport - Fetch inbound message from the Tsi721 MSG Queue
2321 * @mport: Master port implementing the Inbound Messaging Engine
2322 * @mbox: Inbound mailbox number
2323 *
2324 * Returns pointer to the message on success or NULL on failure.
2325 */
2326static int tsi721_query_mport(struct rio_mport *mport,
2327 struct rio_mport_attr *attr)
2328{
2329 struct tsi721_device *priv = mport->priv;
2330 u32 rval;
2331
2332 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_ERR_STS_CSR(0)));
2333 if (rval & RIO_PORT_N_ERR_STS_PORT_OK) {
2334 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL2_CSR(0)));
2335 attr->link_speed = (rval & RIO_PORT_N_CTL2_SEL_BAUD) >> 28;
2336 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL_CSR(0)));
2337 attr->link_width = (rval & RIO_PORT_N_CTL_IPW) >> 27;
2338 } else
2339 attr->link_speed = RIO_LINK_DOWN;
2340
2341#ifdef CONFIG_RAPIDIO_DMA_ENGINE
2342 attr->flags = RIO_MPORT_DMA | RIO_MPORT_DMA_SG;
2343 attr->dma_max_sge = 0;
2344 attr->dma_max_size = TSI721_BDMA_MAX_BCOUNT;
2345 attr->dma_align = 0;
2346#else
2347 attr->flags = 0;
2348#endif
2349 return 0;
2350}
2351
2352/**
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002353 * tsi721_disable_ints - disables all device interrupts
2354 * @priv: pointer to tsi721 private data
2355 */
2356static void tsi721_disable_ints(struct tsi721_device *priv)
2357{
2358 int ch;
2359
2360 /* Disable all device level interrupts */
2361 iowrite32(0, priv->regs + TSI721_DEV_INTE);
2362
2363 /* Disable all Device Channel interrupts */
2364 iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
2365
2366 /* Disable all Inbound Msg Channel interrupts */
2367 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
2368 iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
2369
2370 /* Disable all Outbound Msg Channel interrupts */
2371 for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
2372 iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
2373
2374 /* Disable all general messaging interrupts */
2375 iowrite32(0, priv->regs + TSI721_SMSG_INTE);
2376
2377 /* Disable all BDMA Channel interrupts */
2378 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002379 iowrite32(0,
2380 priv->regs + TSI721_DMAC_BASE(ch) + TSI721_DMAC_INTE);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002381
2382 /* Disable all general BDMA interrupts */
2383 iowrite32(0, priv->regs + TSI721_BDMA_INTE);
2384
2385 /* Disable all SRIO Channel interrupts */
2386 for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
2387 iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
2388
2389 /* Disable all general SR2PC interrupts */
2390 iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
2391
2392 /* Disable all PC2SR interrupts */
2393 iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
2394
2395 /* Disable all I2C interrupts */
2396 iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
2397
2398 /* Disable SRIO MAC interrupts */
2399 iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
2400 iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
2401}
2402
Alexandre Bounine748353c2016-03-22 14:26:23 -07002403static struct rio_ops tsi721_rio_ops = {
2404 .lcread = tsi721_lcread,
2405 .lcwrite = tsi721_lcwrite,
2406 .cread = tsi721_cread_dma,
2407 .cwrite = tsi721_cwrite_dma,
2408 .dsend = tsi721_dsend,
2409 .open_inb_mbox = tsi721_open_inb_mbox,
2410 .close_inb_mbox = tsi721_close_inb_mbox,
2411 .open_outb_mbox = tsi721_open_outb_mbox,
2412 .close_outb_mbox = tsi721_close_outb_mbox,
2413 .add_outb_message = tsi721_add_outb_message,
2414 .add_inb_buffer = tsi721_add_inb_buffer,
2415 .get_inb_message = tsi721_get_inb_message,
2416 .map_inb = tsi721_rio_map_inb_mem,
2417 .unmap_inb = tsi721_rio_unmap_inb_mem,
2418 .pwenable = tsi721_pw_enable,
2419 .query_mport = tsi721_query_mport,
2420};
2421
2422static void tsi721_mport_release(struct device *dev)
2423{
2424 struct rio_mport *mport = to_rio_mport(dev);
2425
2426 dev_dbg(dev, "RIO: %s %s id=%d\n", __func__, mport->name, mport->id);
2427}
2428
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002429/**
2430 * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
2431 * @priv: pointer to tsi721 private data
2432 *
2433 * Configures Tsi721 as RapidIO master port.
2434 */
Bill Pemberton305c8912012-11-19 13:23:25 -05002435static int tsi721_setup_mport(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002436{
2437 struct pci_dev *pdev = priv->pdev;
2438 int err = 0;
Alexandre Bounine748353c2016-03-22 14:26:23 -07002439 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002440
Alexandre Bounine748353c2016-03-22 14:26:23 -07002441 err = rio_mport_initialize(mport);
2442 if (err)
2443 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002444
Alexandre Bounine748353c2016-03-22 14:26:23 -07002445 mport->ops = &tsi721_rio_ops;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002446 mport->index = 0;
2447 mport->sys_size = 0; /* small system */
2448 mport->phy_type = RIO_PHY_SERIAL;
2449 mport->priv = (void *)priv;
2450 mport->phys_efptr = 0x100;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002451 mport->dev.parent = &pdev->dev;
Alexandre Bounine748353c2016-03-22 14:26:23 -07002452 mport->dev.release = tsi721_mport_release;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002453
2454 INIT_LIST_HEAD(&mport->dbells);
2455
2456 rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
Alexandre Bounineb439e662011-12-08 14:34:36 -08002457 rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
2458 rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
Alexandre Bounineed43f442012-10-04 17:15:51 -07002459 snprintf(mport->name, RIO_MAX_MPORT_NAME, "%s(%s)",
2460 dev_driver_string(&pdev->dev), dev_name(&pdev->dev));
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002461
2462 /* Hook up interrupt handler */
2463
2464#ifdef CONFIG_PCI_MSI
2465 if (!tsi721_enable_msix(priv))
2466 priv->flags |= TSI721_USING_MSIX;
2467 else if (!pci_enable_msi(pdev))
2468 priv->flags |= TSI721_USING_MSI;
2469 else
2470 dev_info(&pdev->dev,
2471 "MSI/MSI-X is not available. Using legacy INTx.\n");
2472#endif /* CONFIG_PCI_MSI */
2473
Alexandre Bounine748353c2016-03-22 14:26:23 -07002474 err = tsi721_request_irq(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002475
Alexandre Bounine748353c2016-03-22 14:26:23 -07002476 if (err) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002477 dev_err(&pdev->dev, "Unable to get assigned PCI IRQ "
2478 "vector %02X err=0x%x\n", pdev->irq, err);
Alexandre Bounine748353c2016-03-22 14:26:23 -07002479 return err;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002480 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002481
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002482#ifdef CONFIG_RAPIDIO_DMA_ENGINE
Alexandre Bounine748353c2016-03-22 14:26:23 -07002483 err = tsi721_register_dma(priv);
2484 if (err)
2485 goto err_exit;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002486#endif
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002487 /* Enable SRIO link */
2488 iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
2489 TSI721_DEVCTL_SRBOOT_CMPL,
2490 priv->regs + TSI721_DEVCTL);
2491
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002492 if (mport->host_deviceid >= 0)
2493 iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
2494 RIO_PORT_GEN_DISCOVERED,
2495 priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2496 else
2497 iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2498
Alexandre Bounine748353c2016-03-22 14:26:23 -07002499 err = rio_register_mport(mport);
2500 if (err) {
2501 tsi721_unregister_dma(priv);
2502 goto err_exit;
2503 }
2504
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002505 return 0;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002506
2507err_exit:
Alexandre Bounine748353c2016-03-22 14:26:23 -07002508 tsi721_free_irq(priv);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002509 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002510}
2511
Bill Pemberton305c8912012-11-19 13:23:25 -05002512static int tsi721_probe(struct pci_dev *pdev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002513 const struct pci_device_id *id)
2514{
2515 struct tsi721_device *priv;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002516 int err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002517
2518 priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
2519 if (priv == NULL) {
2520 dev_err(&pdev->dev, "Failed to allocate memory for device\n");
2521 err = -ENOMEM;
2522 goto err_exit;
2523 }
2524
2525 err = pci_enable_device(pdev);
2526 if (err) {
2527 dev_err(&pdev->dev, "Failed to enable PCI device\n");
2528 goto err_clean;
2529 }
2530
2531 priv->pdev = pdev;
2532
2533#ifdef DEBUG
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002534 {
2535 int i;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002536 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
2537 dev_dbg(&pdev->dev, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
2538 i, (unsigned long long)pci_resource_start(pdev, i),
2539 (unsigned long)pci_resource_len(pdev, i),
2540 pci_resource_flags(pdev, i));
2541 }
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002542 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002543#endif
2544 /*
2545 * Verify BAR configuration
2546 */
2547
2548 /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
2549 if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
2550 pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
2551 pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
2552 dev_err(&pdev->dev,
2553 "Missing or misconfigured CSR BAR0, aborting.\n");
2554 err = -ENODEV;
2555 goto err_disable_pdev;
2556 }
2557
2558 /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
2559 if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
2560 pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
2561 pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
2562 dev_err(&pdev->dev,
2563 "Missing or misconfigured Doorbell BAR1, aborting.\n");
2564 err = -ENODEV;
2565 goto err_disable_pdev;
2566 }
2567
2568 /*
2569 * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
2570 * space.
2571 * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
2572 * It may be a good idea to keep them disabled using HW configuration
2573 * to save PCI memory space.
2574 */
2575 if ((pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM) &&
2576 (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64)) {
2577 dev_info(&pdev->dev, "Outbound BAR2 is not used but enabled.\n");
2578 }
2579
2580 if ((pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM) &&
2581 (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64)) {
2582 dev_info(&pdev->dev, "Outbound BAR4 is not used but enabled.\n");
2583 }
2584
2585 err = pci_request_regions(pdev, DRV_NAME);
2586 if (err) {
2587 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
2588 "aborting.\n");
2589 goto err_disable_pdev;
2590 }
2591
2592 pci_set_master(pdev);
2593
2594 priv->regs = pci_ioremap_bar(pdev, BAR_0);
2595 if (!priv->regs) {
2596 dev_err(&pdev->dev,
2597 "Unable to map device registers space, aborting\n");
2598 err = -ENOMEM;
2599 goto err_free_res;
2600 }
2601
2602 priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
2603 if (!priv->odb_base) {
2604 dev_err(&pdev->dev,
2605 "Unable to map outbound doorbells space, aborting\n");
2606 err = -ENOMEM;
2607 goto err_unmap_bars;
2608 }
2609
2610 /* Configure DMA attributes. */
2611 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Peter Senna Tschudin18f62872012-10-04 17:15:55 -07002612 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2613 if (err) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002614 dev_info(&pdev->dev, "Unable to set DMA mask\n");
2615 goto err_unmap_bars;
2616 }
2617
2618 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2619 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2620 } else {
2621 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2622 if (err)
2623 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2624 }
2625
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002626 BUG_ON(!pci_is_pcie(pdev));
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002627
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002628 /* Clear "no snoop" and "relaxed ordering" bits. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002629 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002630 PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN, 0);
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002631
2632 /* Adjust PCIe completion timeout. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002633 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL2, 0xf, 0x2);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002634
2635 /*
2636 * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
2637 */
2638 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
2639 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
2640 TSI721_MSIXTBL_OFFSET);
2641 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
2642 TSI721_MSIXPBA_OFFSET);
2643 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
2644 /* End of FIXUP */
2645
2646 tsi721_disable_ints(priv);
2647
2648 tsi721_init_pc2sr_mapping(priv);
2649 tsi721_init_sr2pc_mapping(priv);
2650
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002651 if (tsi721_bdma_maint_init(priv)) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002652 dev_err(&pdev->dev, "BDMA initialization failed, aborting\n");
2653 err = -ENOMEM;
2654 goto err_unmap_bars;
2655 }
2656
2657 err = tsi721_doorbell_init(priv);
2658 if (err)
2659 goto err_free_bdma;
2660
2661 tsi721_port_write_init(priv);
2662
2663 err = tsi721_messages_init(priv);
2664 if (err)
2665 goto err_free_consistent;
2666
2667 err = tsi721_setup_mport(priv);
2668 if (err)
2669 goto err_free_consistent;
2670
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002671 pci_set_drvdata(pdev, priv);
Alexandre Bounine748353c2016-03-22 14:26:23 -07002672 tsi721_interrupts_init(priv);
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002673
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002674 return 0;
2675
2676err_free_consistent:
Alexandre Bounine748353c2016-03-22 14:26:23 -07002677 tsi721_port_write_free(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002678 tsi721_doorbell_free(priv);
2679err_free_bdma:
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002680 tsi721_bdma_maint_free(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002681err_unmap_bars:
2682 if (priv->regs)
2683 iounmap(priv->regs);
2684 if (priv->odb_base)
2685 iounmap(priv->odb_base);
2686err_free_res:
2687 pci_release_regions(pdev);
2688 pci_clear_master(pdev);
2689err_disable_pdev:
2690 pci_disable_device(pdev);
2691err_clean:
2692 kfree(priv);
2693err_exit:
2694 return err;
2695}
2696
Alexandre Bounine748353c2016-03-22 14:26:23 -07002697static void tsi721_remove(struct pci_dev *pdev)
2698{
2699 struct tsi721_device *priv = pci_get_drvdata(pdev);
2700
2701 dev_dbg(&pdev->dev, "%s enter\n", __func__);
2702
2703 tsi721_disable_ints(priv);
2704 tsi721_free_irq(priv);
2705 rio_unregister_mport(&priv->mport);
2706
2707 tsi721_unregister_dma(priv);
2708 tsi721_bdma_maint_free(priv);
2709 tsi721_doorbell_free(priv);
2710 tsi721_port_write_free(priv);
2711 tsi721_close_sr2pc_mapping(priv);
2712
2713 if (priv->regs)
2714 iounmap(priv->regs);
2715 if (priv->odb_base)
2716 iounmap(priv->odb_base);
2717#ifdef CONFIG_PCI_MSI
2718 if (priv->flags & TSI721_USING_MSIX)
2719 pci_disable_msix(priv->pdev);
2720 else if (priv->flags & TSI721_USING_MSI)
2721 pci_disable_msi(priv->pdev);
2722#endif
2723 pci_release_regions(pdev);
2724 pci_clear_master(pdev);
2725 pci_disable_device(pdev);
2726 pci_set_drvdata(pdev, NULL);
2727 kfree(priv);
2728 dev_dbg(&pdev->dev, "%s exit\n", __func__);
2729}
2730
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002731static void tsi721_shutdown(struct pci_dev *pdev)
2732{
2733 struct tsi721_device *priv = pci_get_drvdata(pdev);
2734
2735 dev_dbg(&pdev->dev, "RIO: %s\n", __func__);
2736
2737 tsi721_disable_ints(priv);
2738 tsi721_dma_stop_all(priv);
2739 pci_clear_master(pdev);
2740 pci_disable_device(pdev);
2741}
2742
Benoit Taine9baa3c32014-08-08 15:56:03 +02002743static const struct pci_device_id tsi721_pci_tbl[] = {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002744 { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
2745 { 0, } /* terminate list */
2746};
2747
2748MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
2749
2750static struct pci_driver tsi721_driver = {
2751 .name = "tsi721",
2752 .id_table = tsi721_pci_tbl,
2753 .probe = tsi721_probe,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002754 .remove = tsi721_remove,
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002755 .shutdown = tsi721_shutdown,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002756};
2757
Alexandre Bounine748353c2016-03-22 14:26:23 -07002758module_pci_driver(tsi721_driver);
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002759
2760MODULE_DESCRIPTION("IDT Tsi721 PCIExpress-to-SRIO bridge driver");
2761MODULE_AUTHOR("Integrated Device Technology, Inc.");
2762MODULE_LICENSE("GPL");