blob: 5e1d52674e174395a20c6d6e8c5692a36f905100 [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
Alexandre Bounine48618fb2011-11-02 13:39:09 -070039static void tsi721_omsg_handler(struct tsi721_device *priv, int ch);
40static void tsi721_imsg_handler(struct tsi721_device *priv, int ch);
41
42/**
43 * tsi721_lcread - read from local SREP config space
44 * @mport: RapidIO master port info
45 * @index: ID of RapdiIO interface
46 * @offset: Offset into configuration space
47 * @len: Length (in bytes) of the maintenance transaction
48 * @data: Value to be read into
49 *
50 * Generates a local SREP space read. Returns %0 on
51 * success or %-EINVAL on failure.
52 */
53static int tsi721_lcread(struct rio_mport *mport, int index, u32 offset,
54 int len, u32 *data)
55{
56 struct tsi721_device *priv = mport->priv;
57
58 if (len != sizeof(u32))
59 return -EINVAL; /* only 32-bit access is supported */
60
61 *data = ioread32(priv->regs + offset);
62
63 return 0;
64}
65
66/**
67 * tsi721_lcwrite - write into local SREP config space
68 * @mport: RapidIO master port info
69 * @index: ID of RapdiIO interface
70 * @offset: Offset into configuration space
71 * @len: Length (in bytes) of the maintenance transaction
72 * @data: Value to be written
73 *
74 * Generates a local write into SREP configuration space. Returns %0 on
75 * success or %-EINVAL on failure.
76 */
77static int tsi721_lcwrite(struct rio_mport *mport, int index, u32 offset,
78 int len, u32 data)
79{
80 struct tsi721_device *priv = mport->priv;
81
82 if (len != sizeof(u32))
83 return -EINVAL; /* only 32-bit access is supported */
84
85 iowrite32(data, priv->regs + offset);
86
87 return 0;
88}
89
90/**
91 * tsi721_maint_dma - Helper function to generate RapidIO maintenance
92 * transactions using designated Tsi721 DMA channel.
93 * @priv: pointer to tsi721 private data
94 * @sys_size: RapdiIO transport system size
95 * @destid: Destination ID of transaction
96 * @hopcount: Number of hops to target device
97 * @offset: Offset into configuration space
98 * @len: Length (in bytes) of the maintenance transaction
99 * @data: Location to be read from or write into
100 * @do_wr: Operation flag (1 == MAINT_WR)
101 *
102 * Generates a RapidIO maintenance transaction (Read or Write).
103 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
104 */
105static int tsi721_maint_dma(struct tsi721_device *priv, u32 sys_size,
106 u16 destid, u8 hopcount, u32 offset, int len,
107 u32 *data, int do_wr)
108{
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700109 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(priv->mdma.ch_id);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700110 struct tsi721_dma_desc *bd_ptr;
111 u32 rd_count, swr_ptr, ch_stat;
112 int i, err = 0;
113 u32 op = do_wr ? MAINT_WR : MAINT_RD;
114
115 if (offset > (RIO_MAINT_SPACE_SZ - len) || (len != sizeof(u32)))
116 return -EINVAL;
117
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700118 bd_ptr = priv->mdma.bd_base;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700119
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700120 rd_count = ioread32(regs + TSI721_DMAC_DRDCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700121
122 /* Initialize DMA descriptor */
123 bd_ptr[0].type_id = cpu_to_le32((DTYPE2 << 29) | (op << 19) | destid);
124 bd_ptr[0].bcount = cpu_to_le32((sys_size << 26) | 0x04);
125 bd_ptr[0].raddr_lo = cpu_to_le32((hopcount << 24) | offset);
126 bd_ptr[0].raddr_hi = 0;
127 if (do_wr)
128 bd_ptr[0].data[0] = cpu_to_be32p(data);
129 else
130 bd_ptr[0].data[0] = 0xffffffff;
131
132 mb();
133
134 /* Start DMA operation */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700135 iowrite32(rd_count + 2, regs + TSI721_DMAC_DWRCNT);
136 ioread32(regs + TSI721_DMAC_DWRCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700137 i = 0;
138
139 /* Wait until DMA transfer is finished */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700140 while ((ch_stat = ioread32(regs + TSI721_DMAC_STS))
141 & TSI721_DMAC_STS_RUN) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700142 udelay(1);
143 if (++i >= 5000000) {
144 dev_dbg(&priv->pdev->dev,
145 "%s : DMA[%d] read timeout ch_status=%x\n",
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700146 __func__, priv->mdma.ch_id, ch_stat);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700147 if (!do_wr)
148 *data = 0xffffffff;
149 err = -EIO;
150 goto err_out;
151 }
152 }
153
154 if (ch_stat & TSI721_DMAC_STS_ABORT) {
155 /* If DMA operation aborted due to error,
156 * reinitialize DMA channel
157 */
158 dev_dbg(&priv->pdev->dev, "%s : DMA ABORT ch_stat=%x\n",
159 __func__, ch_stat);
160 dev_dbg(&priv->pdev->dev, "OP=%d : destid=%x hc=%x off=%x\n",
161 do_wr ? MAINT_WR : MAINT_RD, destid, hopcount, offset);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700162 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
163 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700164 udelay(10);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700165 iowrite32(0, regs + TSI721_DMAC_DWRCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700166 udelay(1);
167 if (!do_wr)
168 *data = 0xffffffff;
169 err = -EIO;
170 goto err_out;
171 }
172
173 if (!do_wr)
174 *data = be32_to_cpu(bd_ptr[0].data[0]);
175
176 /*
177 * Update descriptor status FIFO RD pointer.
178 * NOTE: Skipping check and clear FIFO entries because we are waiting
179 * for transfer to be completed.
180 */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700181 swr_ptr = ioread32(regs + TSI721_DMAC_DSWP);
182 iowrite32(swr_ptr, regs + TSI721_DMAC_DSRP);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700183err_out:
184
185 return err;
186}
187
188/**
189 * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
190 * using Tsi721 BDMA engine.
191 * @mport: RapidIO master port control structure
192 * @index: ID of RapdiIO interface
193 * @destid: Destination ID of transaction
194 * @hopcount: Number of hops to target device
195 * @offset: Offset into configuration space
196 * @len: Length (in bytes) of the maintenance transaction
197 * @val: Location to be read into
198 *
199 * Generates a RapidIO maintenance read transaction.
200 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
201 */
202static int tsi721_cread_dma(struct rio_mport *mport, int index, u16 destid,
203 u8 hopcount, u32 offset, int len, u32 *data)
204{
205 struct tsi721_device *priv = mport->priv;
206
207 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
208 offset, len, data, 0);
209}
210
211/**
212 * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
213 * using Tsi721 BDMA engine
214 * @mport: RapidIO master port control structure
215 * @index: ID of RapdiIO interface
216 * @destid: Destination ID of transaction
217 * @hopcount: Number of hops to target device
218 * @offset: Offset into configuration space
219 * @len: Length (in bytes) of the maintenance transaction
220 * @val: Value to be written
221 *
222 * Generates a RapidIO maintenance write transaction.
223 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
224 */
225static int tsi721_cwrite_dma(struct rio_mport *mport, int index, u16 destid,
226 u8 hopcount, u32 offset, int len, u32 data)
227{
228 struct tsi721_device *priv = mport->priv;
229 u32 temp = data;
230
231 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
232 offset, len, &temp, 1);
233}
234
235/**
236 * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
Alexandre Bounine748353c2016-03-22 14:26:23 -0700237 * @priv: tsi721 device private structure
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700238 *
239 * Handles inbound port-write interrupts. Copies PW message from an internal
240 * buffer into PW message FIFO and schedules deferred routine to process
241 * queued messages.
242 */
243static int
Alexandre Bounine748353c2016-03-22 14:26:23 -0700244tsi721_pw_handler(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700245{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700246 u32 pw_stat;
247 u32 pw_buf[TSI721_RIO_PW_MSG_SIZE/sizeof(u32)];
248
249
250 pw_stat = ioread32(priv->regs + TSI721_RIO_PW_RX_STAT);
251
252 if (pw_stat & TSI721_RIO_PW_RX_STAT_PW_VAL) {
253 pw_buf[0] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(0));
254 pw_buf[1] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(1));
255 pw_buf[2] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(2));
256 pw_buf[3] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(3));
257
258 /* Queue PW message (if there is room in FIFO),
259 * otherwise discard it.
260 */
261 spin_lock(&priv->pw_fifo_lock);
262 if (kfifo_avail(&priv->pw_fifo) >= TSI721_RIO_PW_MSG_SIZE)
263 kfifo_in(&priv->pw_fifo, pw_buf,
264 TSI721_RIO_PW_MSG_SIZE);
265 else
266 priv->pw_discard_count++;
267 spin_unlock(&priv->pw_fifo_lock);
268 }
269
270 /* Clear pending PW interrupts */
271 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
272 priv->regs + TSI721_RIO_PW_RX_STAT);
273
274 schedule_work(&priv->pw_work);
275
276 return 0;
277}
278
279static void tsi721_pw_dpc(struct work_struct *work)
280{
281 struct tsi721_device *priv = container_of(work, struct tsi721_device,
282 pw_work);
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700283 union rio_pw_msg pwmsg;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700284
285 /*
286 * Process port-write messages
287 */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700288 while (kfifo_out_spinlocked(&priv->pw_fifo, (unsigned char *)&pwmsg,
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700289 TSI721_RIO_PW_MSG_SIZE, &priv->pw_fifo_lock)) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700290 /* Pass the port-write message to RIO core for processing */
Alexandre Bounine9a0b0622016-03-22 14:26:44 -0700291 rio_inb_pwrite_handler(&priv->mport, &pwmsg);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700292 }
293}
294
295/**
296 * tsi721_pw_enable - enable/disable port-write interface init
297 * @mport: Master port implementing the port write unit
298 * @enable: 1=enable; 0=disable port-write message handling
299 */
300static int tsi721_pw_enable(struct rio_mport *mport, int enable)
301{
302 struct tsi721_device *priv = mport->priv;
303 u32 rval;
304
305 rval = ioread32(priv->regs + TSI721_RIO_EM_INT_ENABLE);
306
307 if (enable)
308 rval |= TSI721_RIO_EM_INT_ENABLE_PW_RX;
309 else
310 rval &= ~TSI721_RIO_EM_INT_ENABLE_PW_RX;
311
312 /* Clear pending PW interrupts */
313 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
314 priv->regs + TSI721_RIO_PW_RX_STAT);
315 /* Update enable bits */
316 iowrite32(rval, priv->regs + TSI721_RIO_EM_INT_ENABLE);
317
318 return 0;
319}
320
321/**
322 * tsi721_dsend - Send a RapidIO doorbell
323 * @mport: RapidIO master port info
324 * @index: ID of RapidIO interface
325 * @destid: Destination ID of target device
326 * @data: 16-bit info field of RapidIO doorbell
327 *
328 * Sends a RapidIO doorbell message. Always returns %0.
329 */
330static int tsi721_dsend(struct rio_mport *mport, int index,
331 u16 destid, u16 data)
332{
333 struct tsi721_device *priv = mport->priv;
334 u32 offset;
335
336 offset = (((mport->sys_size) ? RIO_TT_CODE_16 : RIO_TT_CODE_8) << 18) |
337 (destid << 2);
338
339 dev_dbg(&priv->pdev->dev,
340 "Send Doorbell 0x%04x to destID 0x%x\n", data, destid);
341 iowrite16be(data, priv->odb_base + offset);
342
343 return 0;
344}
345
346/**
347 * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
Alexandre Bounine748353c2016-03-22 14:26:23 -0700348 * @priv: tsi721 device-specific data structure
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700349 *
350 * Handles inbound doorbell interrupts. Copies doorbell entry from an internal
351 * buffer into DB message FIFO and schedules deferred routine to process
352 * queued DBs.
353 */
354static int
Alexandre Bounine748353c2016-03-22 14:26:23 -0700355tsi721_dbell_handler(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700356{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700357 u32 regval;
358
359 /* Disable IDB interrupts */
360 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
361 regval &= ~TSI721_SR_CHINT_IDBQRCV;
362 iowrite32(regval,
363 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
364
365 schedule_work(&priv->idb_work);
366
367 return 0;
368}
369
370static void tsi721_db_dpc(struct work_struct *work)
371{
372 struct tsi721_device *priv = container_of(work, struct tsi721_device,
373 idb_work);
374 struct rio_mport *mport;
375 struct rio_dbell *dbell;
376 int found = 0;
377 u32 wr_ptr, rd_ptr;
378 u64 *idb_entry;
379 u32 regval;
380 union {
381 u64 msg;
382 u8 bytes[8];
383 } idb;
384
385 /*
386 * Process queued inbound doorbells
387 */
Alexandre Bounine748353c2016-03-22 14:26:23 -0700388 mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700389
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800390 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
391 rd_ptr = ioread32(priv->regs + TSI721_IDQ_RP(IDB_QUEUE)) % IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700392
393 while (wr_ptr != rd_ptr) {
394 idb_entry = (u64 *)(priv->idb_base +
395 (TSI721_IDB_ENTRY_SIZE * rd_ptr));
396 rd_ptr++;
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800397 rd_ptr %= IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700398 idb.msg = *idb_entry;
399 *idb_entry = 0;
400
401 /* Process one doorbell */
402 list_for_each_entry(dbell, &mport->dbells, node) {
403 if ((dbell->res->start <= DBELL_INF(idb.bytes)) &&
404 (dbell->res->end >= DBELL_INF(idb.bytes))) {
405 found = 1;
406 break;
407 }
408 }
409
410 if (found) {
411 dbell->dinb(mport, dbell->dev_id, DBELL_SID(idb.bytes),
412 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
413 } else {
414 dev_dbg(&priv->pdev->dev,
415 "spurious inb doorbell, sid %2.2x tid %2.2x"
416 " info %4.4x\n", DBELL_SID(idb.bytes),
417 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
418 }
Alexandre Bounine3670e7e2012-08-21 16:16:11 -0700419
420 wr_ptr = ioread32(priv->regs +
421 TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700422 }
423
424 iowrite32(rd_ptr & (IDB_QSIZE - 1),
425 priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
426
427 /* Re-enable IDB interrupts */
428 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
429 regval |= TSI721_SR_CHINT_IDBQRCV;
430 iowrite32(regval,
431 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
Alexandre Bounine3670e7e2012-08-21 16:16:11 -0700432
433 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
434 if (wr_ptr != rd_ptr)
435 schedule_work(&priv->idb_work);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700436}
437
438/**
439 * tsi721_irqhandler - Tsi721 interrupt handler
440 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700441 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700442 *
443 * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
444 * interrupt events and calls an event-specific handler(s).
445 */
446static irqreturn_t tsi721_irqhandler(int irq, void *ptr)
447{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700448 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700449 u32 dev_int;
450 u32 dev_ch_int;
451 u32 intval;
452 u32 ch_inte;
453
Alexandre Bounine1ccc8192013-05-24 15:55:17 -0700454 /* For MSI mode disable all device-level interrupts */
455 if (priv->flags & TSI721_USING_MSI)
456 iowrite32(0, priv->regs + TSI721_DEV_INTE);
457
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700458 dev_int = ioread32(priv->regs + TSI721_DEV_INT);
459 if (!dev_int)
460 return IRQ_NONE;
461
462 dev_ch_int = ioread32(priv->regs + TSI721_DEV_CHAN_INT);
463
464 if (dev_int & TSI721_DEV_INT_SR2PC_CH) {
465 /* Service SR2PC Channel interrupts */
466 if (dev_ch_int & TSI721_INT_SR2PC_CHAN(IDB_QUEUE)) {
467 /* Service Inbound Doorbell interrupt */
468 intval = ioread32(priv->regs +
469 TSI721_SR_CHINT(IDB_QUEUE));
470 if (intval & TSI721_SR_CHINT_IDBQRCV)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700471 tsi721_dbell_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700472 else
473 dev_info(&priv->pdev->dev,
474 "Unsupported SR_CH_INT %x\n", intval);
475
476 /* Clear interrupts */
477 iowrite32(intval,
478 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
479 ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
480 }
481 }
482
483 if (dev_int & TSI721_DEV_INT_SMSG_CH) {
484 int ch;
485
486 /*
487 * Service channel interrupts from Messaging Engine
488 */
489
490 if (dev_ch_int & TSI721_INT_IMSG_CHAN_M) { /* Inbound Msg */
491 /* Disable signaled OB MSG Channel interrupts */
492 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
493 ch_inte &= ~(dev_ch_int & TSI721_INT_IMSG_CHAN_M);
494 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
495
496 /*
497 * Process Inbound Message interrupt for each MBOX
498 */
499 for (ch = 4; ch < RIO_MAX_MBOX + 4; ch++) {
500 if (!(dev_ch_int & TSI721_INT_IMSG_CHAN(ch)))
501 continue;
502 tsi721_imsg_handler(priv, ch);
503 }
504 }
505
506 if (dev_ch_int & TSI721_INT_OMSG_CHAN_M) { /* Outbound Msg */
507 /* Disable signaled OB MSG Channel interrupts */
508 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
509 ch_inte &= ~(dev_ch_int & TSI721_INT_OMSG_CHAN_M);
510 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
511
512 /*
513 * Process Outbound Message interrupts for each MBOX
514 */
515
516 for (ch = 0; ch < RIO_MAX_MBOX; ch++) {
517 if (!(dev_ch_int & TSI721_INT_OMSG_CHAN(ch)))
518 continue;
519 tsi721_omsg_handler(priv, ch);
520 }
521 }
522 }
523
524 if (dev_int & TSI721_DEV_INT_SRIO) {
525 /* Service SRIO MAC interrupts */
526 intval = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
527 if (intval & TSI721_RIO_EM_INT_STAT_PW_RX)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700528 tsi721_pw_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700529 }
530
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700531#ifdef CONFIG_RAPIDIO_DMA_ENGINE
532 if (dev_int & TSI721_DEV_INT_BDMA_CH) {
533 int ch;
534
535 if (dev_ch_int & TSI721_INT_BDMA_CHAN_M) {
536 dev_dbg(&priv->pdev->dev,
537 "IRQ from DMA channel 0x%08x\n", dev_ch_int);
538
539 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++) {
540 if (!(dev_ch_int & TSI721_INT_BDMA_CHAN(ch)))
541 continue;
542 tsi721_bdma_handler(&priv->bdma[ch]);
543 }
544 }
545 }
546#endif
Alexandre Bounine1ccc8192013-05-24 15:55:17 -0700547
548 /* For MSI mode re-enable device-level interrupts */
549 if (priv->flags & TSI721_USING_MSI) {
550 dev_int = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
551 TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
552 iowrite32(dev_int, priv->regs + TSI721_DEV_INTE);
553 }
554
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700555 return IRQ_HANDLED;
556}
557
558static void tsi721_interrupts_init(struct tsi721_device *priv)
559{
560 u32 intr;
561
562 /* Enable IDB interrupts */
563 iowrite32(TSI721_SR_CHINT_ALL,
564 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
565 iowrite32(TSI721_SR_CHINT_IDBQRCV,
566 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700567
568 /* Enable SRIO MAC interrupts */
569 iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT,
570 priv->regs + TSI721_RIO_EM_DEV_INT_EN);
571
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700572 /* Enable interrupts from channels in use */
573#ifdef CONFIG_RAPIDIO_DMA_ENGINE
574 intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE) |
575 (TSI721_INT_BDMA_CHAN_M &
576 ~TSI721_INT_BDMA_CHAN(TSI721_DMACH_MAINT));
577#else
578 intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE);
579#endif
580 iowrite32(intr, priv->regs + TSI721_DEV_CHAN_INTE);
581
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700582 if (priv->flags & TSI721_USING_MSIX)
583 intr = TSI721_DEV_INT_SRIO;
584 else
585 intr = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700586 TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700587
588 iowrite32(intr, priv->regs + TSI721_DEV_INTE);
589 ioread32(priv->regs + TSI721_DEV_INTE);
590}
591
592#ifdef CONFIG_PCI_MSI
593/**
594 * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
595 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700596 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700597 *
598 * Handles outbound messaging interrupts signaled using MSI-X.
599 */
600static irqreturn_t tsi721_omsg_msix(int irq, void *ptr)
601{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700602 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700603 int mbox;
604
605 mbox = (irq - priv->msix[TSI721_VECT_OMB0_DONE].vector) % RIO_MAX_MBOX;
606 tsi721_omsg_handler(priv, mbox);
607 return IRQ_HANDLED;
608}
609
610/**
611 * tsi721_imsg_msix - MSI-X interrupt handler for inbound 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 inbound messaging interrupts signaled using MSI-X.
616 */
617static irqreturn_t tsi721_imsg_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_IMB0_RCV].vector) % RIO_MAX_MBOX;
623 tsi721_imsg_handler(priv, mbox + 4);
624 return IRQ_HANDLED;
625}
626
627/**
628 * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
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 Tsi721 interrupts from SRIO MAC.
633 */
634static irqreturn_t tsi721_srio_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 u32 srio_int;
638
639 /* Service SRIO MAC interrupts */
640 srio_int = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
641 if (srio_int & TSI721_RIO_EM_INT_STAT_PW_RX)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700642 tsi721_pw_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700643
644 return IRQ_HANDLED;
645}
646
647/**
648 * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
649 * @irq: Linux interrupt number
Alexandre Bounine748353c2016-03-22 14:26:23 -0700650 * @ptr: Pointer to interrupt-specific data (tsi721_device structure)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700651 *
652 * Handles Tsi721 interrupts from SR2PC Channel.
653 * NOTE: At this moment services only one SR2PC channel associated with inbound
654 * doorbells.
655 */
656static irqreturn_t tsi721_sr2pc_ch_msix(int irq, void *ptr)
657{
Alexandre Bounine748353c2016-03-22 14:26:23 -0700658 struct tsi721_device *priv = (struct tsi721_device *)ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700659 u32 sr_ch_int;
660
661 /* Service Inbound DB interrupt from SR2PC channel */
662 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
663 if (sr_ch_int & TSI721_SR_CHINT_IDBQRCV)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700664 tsi721_dbell_handler(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700665
666 /* Clear interrupts */
667 iowrite32(sr_ch_int, priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
668 /* Read back to ensure that interrupt was cleared */
669 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
670
671 return IRQ_HANDLED;
672}
673
674/**
675 * tsi721_request_msix - register interrupt service for MSI-X mode.
Alexandre Bounine748353c2016-03-22 14:26:23 -0700676 * @priv: tsi721 device-specific data structure
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700677 *
678 * Registers MSI-X interrupt service routines for interrupts that are active
679 * immediately after mport initialization. Messaging interrupt service routines
680 * should be registered during corresponding open requests.
681 */
Alexandre Bounine748353c2016-03-22 14:26:23 -0700682static int tsi721_request_msix(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700683{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700684 int err = 0;
685
686 err = request_irq(priv->msix[TSI721_VECT_IDB].vector,
687 tsi721_sr2pc_ch_msix, 0,
Alexandre Bounine748353c2016-03-22 14:26:23 -0700688 priv->msix[TSI721_VECT_IDB].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700689 if (err)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700690 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700691
692 err = request_irq(priv->msix[TSI721_VECT_PWRX].vector,
693 tsi721_srio_msix, 0,
Alexandre Bounine748353c2016-03-22 14:26:23 -0700694 priv->msix[TSI721_VECT_PWRX].irq_name, (void *)priv);
695 if (err) {
696 free_irq(priv->msix[TSI721_VECT_IDB].vector, (void *)priv);
697 return err;
698 }
699
700 return 0;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700701}
702
703/**
704 * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
705 * @priv: pointer to tsi721 private data
706 *
707 * Configures MSI-X support for Tsi721. Supports only an exact number
708 * of requested vectors.
709 */
710static int tsi721_enable_msix(struct tsi721_device *priv)
711{
712 struct msix_entry entries[TSI721_VECT_MAX];
713 int err;
714 int i;
715
716 entries[TSI721_VECT_IDB].entry = TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE);
717 entries[TSI721_VECT_PWRX].entry = TSI721_MSIX_SRIO_MAC_INT;
718
719 /*
720 * Initialize MSI-X entries for Messaging Engine:
721 * this driver supports four RIO mailboxes (inbound and outbound)
722 * NOTE: Inbound message MBOX 0...4 use IB channels 4...7. Therefore
723 * offset +4 is added to IB MBOX number.
724 */
725 for (i = 0; i < RIO_MAX_MBOX; i++) {
726 entries[TSI721_VECT_IMB0_RCV + i].entry =
727 TSI721_MSIX_IMSG_DQ_RCV(i + 4);
728 entries[TSI721_VECT_IMB0_INT + i].entry =
729 TSI721_MSIX_IMSG_INT(i + 4);
730 entries[TSI721_VECT_OMB0_DONE + i].entry =
731 TSI721_MSIX_OMSG_DONE(i);
732 entries[TSI721_VECT_OMB0_INT + i].entry =
733 TSI721_MSIX_OMSG_INT(i);
734 }
735
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700736#ifdef CONFIG_RAPIDIO_DMA_ENGINE
737 /*
738 * Initialize MSI-X entries for Block DMA Engine:
739 * this driver supports XXX DMA channels
740 * (one is reserved for SRIO maintenance transactions)
741 */
742 for (i = 0; i < TSI721_DMA_CHNUM; i++) {
743 entries[TSI721_VECT_DMA0_DONE + i].entry =
744 TSI721_MSIX_DMACH_DONE(i);
745 entries[TSI721_VECT_DMA0_INT + i].entry =
746 TSI721_MSIX_DMACH_INT(i);
747 }
748#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
749
Alexander Gordeev1c92ab12014-06-06 14:37:16 -0700750 err = pci_enable_msix_exact(priv->pdev, entries, ARRAY_SIZE(entries));
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700751 if (err) {
Alexander Gordeev1c92ab12014-06-06 14:37:16 -0700752 dev_err(&priv->pdev->dev,
753 "Failed to enable MSI-X (err=%d)\n", err);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700754 return err;
755 }
756
757 /*
758 * Copy MSI-X vector information into tsi721 private structure
759 */
760 priv->msix[TSI721_VECT_IDB].vector = entries[TSI721_VECT_IDB].vector;
761 snprintf(priv->msix[TSI721_VECT_IDB].irq_name, IRQ_DEVICE_NAME_MAX,
762 DRV_NAME "-idb@pci:%s", pci_name(priv->pdev));
763 priv->msix[TSI721_VECT_PWRX].vector = entries[TSI721_VECT_PWRX].vector;
764 snprintf(priv->msix[TSI721_VECT_PWRX].irq_name, IRQ_DEVICE_NAME_MAX,
765 DRV_NAME "-pwrx@pci:%s", pci_name(priv->pdev));
766
767 for (i = 0; i < RIO_MAX_MBOX; i++) {
768 priv->msix[TSI721_VECT_IMB0_RCV + i].vector =
769 entries[TSI721_VECT_IMB0_RCV + i].vector;
770 snprintf(priv->msix[TSI721_VECT_IMB0_RCV + i].irq_name,
771 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbr%d@pci:%s",
772 i, pci_name(priv->pdev));
773
774 priv->msix[TSI721_VECT_IMB0_INT + i].vector =
775 entries[TSI721_VECT_IMB0_INT + i].vector;
776 snprintf(priv->msix[TSI721_VECT_IMB0_INT + i].irq_name,
777 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbi%d@pci:%s",
778 i, pci_name(priv->pdev));
779
780 priv->msix[TSI721_VECT_OMB0_DONE + i].vector =
781 entries[TSI721_VECT_OMB0_DONE + i].vector;
782 snprintf(priv->msix[TSI721_VECT_OMB0_DONE + i].irq_name,
783 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombd%d@pci:%s",
784 i, pci_name(priv->pdev));
785
786 priv->msix[TSI721_VECT_OMB0_INT + i].vector =
787 entries[TSI721_VECT_OMB0_INT + i].vector;
788 snprintf(priv->msix[TSI721_VECT_OMB0_INT + i].irq_name,
789 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombi%d@pci:%s",
790 i, pci_name(priv->pdev));
791 }
792
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700793#ifdef CONFIG_RAPIDIO_DMA_ENGINE
794 for (i = 0; i < TSI721_DMA_CHNUM; i++) {
795 priv->msix[TSI721_VECT_DMA0_DONE + i].vector =
796 entries[TSI721_VECT_DMA0_DONE + i].vector;
797 snprintf(priv->msix[TSI721_VECT_DMA0_DONE + i].irq_name,
798 IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmad%d@pci:%s",
799 i, pci_name(priv->pdev));
800
801 priv->msix[TSI721_VECT_DMA0_INT + i].vector =
802 entries[TSI721_VECT_DMA0_INT + i].vector;
803 snprintf(priv->msix[TSI721_VECT_DMA0_INT + i].irq_name,
804 IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmai%d@pci:%s",
805 i, pci_name(priv->pdev));
806 }
807#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
808
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700809 return 0;
810}
811#endif /* CONFIG_PCI_MSI */
812
Alexandre Bounine748353c2016-03-22 14:26:23 -0700813static int tsi721_request_irq(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700814{
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700815 int err;
816
817#ifdef CONFIG_PCI_MSI
818 if (priv->flags & TSI721_USING_MSIX)
Alexandre Bounine748353c2016-03-22 14:26:23 -0700819 err = tsi721_request_msix(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700820 else
821#endif
822 err = request_irq(priv->pdev->irq, tsi721_irqhandler,
823 (priv->flags & TSI721_USING_MSI) ? 0 : IRQF_SHARED,
Alexandre Bounine748353c2016-03-22 14:26:23 -0700824 DRV_NAME, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700825
826 if (err)
827 dev_err(&priv->pdev->dev,
828 "Unable to allocate interrupt, Error: %d\n", err);
829
830 return err;
831}
832
Alexandre Bounine748353c2016-03-22 14:26:23 -0700833static void tsi721_free_irq(struct tsi721_device *priv)
834{
835#ifdef CONFIG_PCI_MSI
836 if (priv->flags & TSI721_USING_MSIX) {
837 free_irq(priv->msix[TSI721_VECT_IDB].vector, (void *)priv);
838 free_irq(priv->msix[TSI721_VECT_PWRX].vector, (void *)priv);
839 } else
840#endif
841 free_irq(priv->pdev->irq, (void *)priv);
842}
843
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700844/**
845 * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
846 * translation regions.
847 * @priv: pointer to tsi721 private data
848 *
849 * Disables SREP translation regions.
850 */
851static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
852{
853 int i;
854
855 /* Disable all PC2SR translation windows */
856 for (i = 0; i < TSI721_OBWIN_NUM; i++)
857 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
858}
859
860/**
Alexandre Bounine71afe342012-10-04 17:16:00 -0700861 * tsi721_rio_map_inb_mem -- Mapping inbound memory region.
862 * @mport: RapidIO master port
863 * @lstart: Local memory space start address.
864 * @rstart: RapidIO space start address.
865 * @size: The mapping region size.
866 * @flags: Flags for mapping. 0 for using default flags.
867 *
868 * Return: 0 -- Success.
869 *
870 * This function will create the inbound mapping
871 * from rstart to lstart.
872 */
873static int tsi721_rio_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
874 u64 rstart, u32 size, u32 flags)
875{
876 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700877 int i, avail = -1;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700878 u32 regval;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700879 struct tsi721_ib_win *ib_win;
Alexandre Bounine9673b882016-03-22 14:25:54 -0700880 bool direct = (lstart == rstart);
881 u64 ibw_size;
882 dma_addr_t loc_start;
883 u64 ibw_start;
884 struct tsi721_ib_win_mapping *map = NULL;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700885 int ret = -EBUSY;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700886
Alexandre Bounine9673b882016-03-22 14:25:54 -0700887 if (direct) {
888 dev_dbg(&priv->pdev->dev,
889 "Direct (RIO_0x%llx -> PCIe_0x%pad), size=0x%x",
890 rstart, &lstart, size);
Alexandre Bounine71afe342012-10-04 17:16:00 -0700891
Alexandre Bounine9673b882016-03-22 14:25:54 -0700892 /* Calculate minimal acceptable window size and base address */
893
894 ibw_size = roundup_pow_of_two(size);
895 ibw_start = lstart & ~(ibw_size - 1);
896
897 while ((lstart + size) > (ibw_start + ibw_size)) {
898 ibw_size *= 2;
899 ibw_start = lstart & ~(ibw_size - 1);
900 if (ibw_size > 0x80000000) { /* Limit max size to 2GB */
901 return -EBUSY;
902 }
903 }
904
905 loc_start = ibw_start;
906
907 map = kzalloc(sizeof(struct tsi721_ib_win_mapping), GFP_ATOMIC);
908 if (map == NULL)
909 return -ENOMEM;
910
911 } else {
912 dev_dbg(&priv->pdev->dev,
913 "Translated (RIO_0x%llx -> PCIe_0x%pad), size=0x%x",
914 rstart, &lstart, size);
915
916 if (!is_power_of_2(size) || size < 0x1000 ||
917 ((u64)lstart & (size - 1)) || (rstart & (size - 1)))
918 return -EINVAL;
919 if (priv->ibwin_cnt == 0)
920 return -EBUSY;
921 ibw_start = rstart;
922 ibw_size = size;
923 loc_start = lstart;
924 }
925
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700926 /*
927 * Scan for overlapping with active regions and mark the first available
928 * IB window at the same time.
929 */
Alexandre Bounine71afe342012-10-04 17:16:00 -0700930 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700931 ib_win = &priv->ib_win[i];
Alexandre Bounine9673b882016-03-22 14:25:54 -0700932
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700933 if (!ib_win->active) {
934 if (avail == -1) {
935 avail = i;
936 ret = 0;
937 }
Alexandre Bounine9673b882016-03-22 14:25:54 -0700938 } else if (ibw_start < (ib_win->rstart + ib_win->size) &&
939 (ibw_start + ibw_size) > ib_win->rstart) {
940 /* Return error if address translation involved */
941 if (direct && ib_win->xlat) {
942 ret = -EFAULT;
943 break;
944 }
945
946 /*
947 * Direct mappings usually are larger than originally
948 * requested fragments - check if this new request fits
949 * into it.
950 */
951 if (rstart >= ib_win->rstart &&
952 (rstart + size) <= (ib_win->rstart +
953 ib_win->size)) {
954 /* We are in - no further mapping required */
955 map->lstart = lstart;
956 list_add_tail(&map->node, &ib_win->mappings);
957 return 0;
958 }
959
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700960 ret = -EFAULT;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700961 break;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700962 }
Alexandre Bounine71afe342012-10-04 17:16:00 -0700963 }
964
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700965 if (ret)
Alexandre Bounine9673b882016-03-22 14:25:54 -0700966 goto out;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700967 i = avail;
968
969 /* Sanity check: available IB window must be disabled at this point */
970 regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
971 if (WARN_ON(regval & TSI721_IBWIN_LB_WEN)) {
972 ret = -EIO;
Alexandre Bounine9673b882016-03-22 14:25:54 -0700973 goto out;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700974 }
975
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700976 ib_win = &priv->ib_win[i];
977 ib_win->active = true;
Alexandre Bounine9673b882016-03-22 14:25:54 -0700978 ib_win->rstart = ibw_start;
979 ib_win->lstart = loc_start;
980 ib_win->size = ibw_size;
981 ib_win->xlat = (lstart != rstart);
982 INIT_LIST_HEAD(&ib_win->mappings);
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700983
Alexandre Bounine9673b882016-03-22 14:25:54 -0700984 /*
985 * When using direct IBW mapping and have larger than requested IBW size
986 * we can have multiple local memory blocks mapped through the same IBW
987 * To handle this situation we maintain list of "clients" for such IBWs.
988 */
989 if (direct) {
990 map->lstart = lstart;
991 list_add_tail(&map->node, &ib_win->mappings);
992 }
993
994 iowrite32(TSI721_IBWIN_SIZE(ibw_size) << 8,
Alexandre Bounine71afe342012-10-04 17:16:00 -0700995 priv->regs + TSI721_IBWIN_SZ(i));
996
Alexandre Bounine9673b882016-03-22 14:25:54 -0700997 iowrite32(((u64)loc_start >> 32), priv->regs + TSI721_IBWIN_TUA(i));
998 iowrite32(((u64)loc_start & TSI721_IBWIN_TLA_ADD),
Alexandre Bounine71afe342012-10-04 17:16:00 -0700999 priv->regs + TSI721_IBWIN_TLA(i));
1000
Alexandre Bounine9673b882016-03-22 14:25:54 -07001001 iowrite32(ibw_start >> 32, priv->regs + TSI721_IBWIN_UB(i));
1002 iowrite32((ibw_start & TSI721_IBWIN_LB_BA) | TSI721_IBWIN_LB_WEN,
Alexandre Bounine71afe342012-10-04 17:16:00 -07001003 priv->regs + TSI721_IBWIN_LB(i));
Alexandre Bounine9673b882016-03-22 14:25:54 -07001004
1005 priv->ibwin_cnt--;
1006
Alexandre Bounine71afe342012-10-04 17:16:00 -07001007 dev_dbg(&priv->pdev->dev,
Alexandre Bounine9673b882016-03-22 14:25:54 -07001008 "Configured IBWIN%d (RIO_0x%llx -> PCIe_0x%llx), size=0x%llx\n",
1009 i, ibw_start, (unsigned long long)loc_start, ibw_size);
Alexandre Bounine71afe342012-10-04 17:16:00 -07001010
1011 return 0;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001012out:
1013 kfree(map);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001014 return ret;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001015}
1016
1017/**
Alexandre Bounine9673b882016-03-22 14:25:54 -07001018 * tsi721_rio_unmap_inb_mem -- Unmapping inbound memory region.
Alexandre Bounine71afe342012-10-04 17:16:00 -07001019 * @mport: RapidIO master port
1020 * @lstart: Local memory space start address.
1021 */
1022static void tsi721_rio_unmap_inb_mem(struct rio_mport *mport,
1023 dma_addr_t lstart)
1024{
1025 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001026 struct tsi721_ib_win *ib_win;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001027 int i;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001028
Alexandre Bounine9673b882016-03-22 14:25:54 -07001029 dev_dbg(&priv->pdev->dev,
1030 "Unmap IBW mapped to PCIe_0x%pad", &lstart);
1031
Alexandre Bounine71afe342012-10-04 17:16:00 -07001032 /* Search for matching active inbound translation window */
1033 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001034 ib_win = &priv->ib_win[i];
Alexandre Bounine9673b882016-03-22 14:25:54 -07001035
1036 /* Address translating IBWs must to be an exact march */
1037 if (!ib_win->active ||
1038 (ib_win->xlat && lstart != ib_win->lstart))
1039 continue;
1040
1041 if (lstart >= ib_win->lstart &&
1042 lstart < (ib_win->lstart + ib_win->size)) {
1043
1044 if (!ib_win->xlat) {
1045 struct tsi721_ib_win_mapping *map;
1046 int found = 0;
1047
1048 list_for_each_entry(map,
1049 &ib_win->mappings, node) {
1050 if (map->lstart == lstart) {
1051 list_del(&map->node);
1052 kfree(map);
1053 found = 1;
1054 break;
1055 }
1056 }
1057
1058 if (!found)
1059 continue;
1060
1061 if (!list_empty(&ib_win->mappings))
1062 break;
1063 }
1064
1065 dev_dbg(&priv->pdev->dev, "Disable IBWIN_%d", i);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001066 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1067 ib_win->active = false;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001068 priv->ibwin_cnt++;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001069 break;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001070 }
1071 }
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001072
1073 if (i == TSI721_IBWIN_NUM)
Alexandre Bounine9673b882016-03-22 14:25:54 -07001074 dev_dbg(&priv->pdev->dev,
1075 "IB window mapped to %pad not found", &lstart);
Alexandre Bounine71afe342012-10-04 17:16:00 -07001076}
1077
1078/**
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001079 * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
1080 * translation regions.
1081 * @priv: pointer to tsi721 private data
1082 *
1083 * Disables inbound windows.
1084 */
1085static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
1086{
1087 int i;
1088
1089 /* Disable all SR2PC inbound windows */
1090 for (i = 0; i < TSI721_IBWIN_NUM; i++)
Alexandre Bounine71afe342012-10-04 17:16:00 -07001091 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
Alexandre Bounine9673b882016-03-22 14:25:54 -07001092 priv->ibwin_cnt = TSI721_IBWIN_NUM;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001093}
1094
Alexandre Bounine748353c2016-03-22 14:26:23 -07001095/*
1096 * tsi721_close_sr2pc_mapping - closes all active inbound (SRIO->PCIe)
1097 * translation regions.
1098 * @priv: pointer to tsi721 device private data
1099 */
1100static void tsi721_close_sr2pc_mapping(struct tsi721_device *priv)
1101{
1102 struct tsi721_ib_win *ib_win;
1103 int i;
1104
1105 /* Disable all active SR2PC inbound windows */
1106 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
1107 ib_win = &priv->ib_win[i];
1108 if (ib_win->active) {
1109 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1110 ib_win->active = false;
1111 }
1112 }
1113}
1114
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001115/**
1116 * tsi721_port_write_init - Inbound port write interface init
1117 * @priv: pointer to tsi721 private data
1118 *
1119 * Initializes inbound port write handler.
1120 * Returns %0 on success or %-ENOMEM on failure.
1121 */
1122static int tsi721_port_write_init(struct tsi721_device *priv)
1123{
1124 priv->pw_discard_count = 0;
1125 INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
1126 spin_lock_init(&priv->pw_fifo_lock);
1127 if (kfifo_alloc(&priv->pw_fifo,
1128 TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
1129 dev_err(&priv->pdev->dev, "PW FIFO allocation failed\n");
1130 return -ENOMEM;
1131 }
1132
1133 /* Use reliable port-write capture mode */
1134 iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
1135 return 0;
1136}
1137
Alexandre Bounine748353c2016-03-22 14:26:23 -07001138static void tsi721_port_write_free(struct tsi721_device *priv)
1139{
1140 kfifo_free(&priv->pw_fifo);
1141}
1142
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001143static int tsi721_doorbell_init(struct tsi721_device *priv)
1144{
1145 /* Outbound Doorbells do not require any setup.
1146 * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
1147 * That BAR1 was mapped during the probe routine.
1148 */
1149
1150 /* Initialize Inbound Doorbell processing DPC and queue */
1151 priv->db_discard_count = 0;
1152 INIT_WORK(&priv->idb_work, tsi721_db_dpc);
1153
1154 /* Allocate buffer for inbound doorbells queue */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001155 priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001156 IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1157 &priv->idb_dma, GFP_KERNEL);
1158 if (!priv->idb_base)
1159 return -ENOMEM;
1160
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001161 dev_dbg(&priv->pdev->dev, "Allocated IDB buffer @ %p (phys = %llx)\n",
1162 priv->idb_base, (unsigned long long)priv->idb_dma);
1163
1164 iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
1165 priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
1166 iowrite32(((u64)priv->idb_dma >> 32),
1167 priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
1168 iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
1169 priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
1170 /* Enable accepting all inbound doorbells */
1171 iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
1172
1173 iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
1174
1175 iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
1176
1177 return 0;
1178}
1179
1180static void tsi721_doorbell_free(struct tsi721_device *priv)
1181{
1182 if (priv->idb_base == NULL)
1183 return;
1184
1185 /* Free buffer allocated for inbound doorbell queue */
1186 dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1187 priv->idb_base, priv->idb_dma);
1188 priv->idb_base = NULL;
1189}
1190
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001191/**
1192 * tsi721_bdma_maint_init - Initialize maintenance request BDMA channel.
1193 * @priv: pointer to tsi721 private data
1194 *
1195 * Initialize BDMA channel allocated for RapidIO maintenance read/write
1196 * request generation
1197 * Returns %0 on success or %-ENOMEM on failure.
1198 */
1199static int tsi721_bdma_maint_init(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001200{
1201 struct tsi721_dma_desc *bd_ptr;
1202 u64 *sts_ptr;
1203 dma_addr_t bd_phys, sts_phys;
1204 int sts_size;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001205 int bd_num = 2;
1206 void __iomem *regs;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001207
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001208 dev_dbg(&priv->pdev->dev,
1209 "Init Block DMA Engine for Maintenance requests, CH%d\n",
1210 TSI721_DMACH_MAINT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001211
1212 /*
1213 * Initialize DMA channel for maintenance requests
1214 */
1215
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001216 priv->mdma.ch_id = TSI721_DMACH_MAINT;
1217 regs = priv->regs + TSI721_DMAC_BASE(TSI721_DMACH_MAINT);
1218
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001219 /* Allocate space for DMA descriptors */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001220 bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001221 bd_num * sizeof(struct tsi721_dma_desc),
1222 &bd_phys, GFP_KERNEL);
1223 if (!bd_ptr)
1224 return -ENOMEM;
1225
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001226 priv->mdma.bd_num = bd_num;
1227 priv->mdma.bd_phys = bd_phys;
1228 priv->mdma.bd_base = bd_ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001229
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001230 dev_dbg(&priv->pdev->dev, "DMA descriptors @ %p (phys = %llx)\n",
1231 bd_ptr, (unsigned long long)bd_phys);
1232
1233 /* Allocate space for descriptor status FIFO */
1234 sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
1235 bd_num : TSI721_DMA_MINSTSSZ;
1236 sts_size = roundup_pow_of_two(sts_size);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001237 sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001238 sts_size * sizeof(struct tsi721_dma_sts),
1239 &sts_phys, GFP_KERNEL);
1240 if (!sts_ptr) {
1241 /* Free space allocated for DMA descriptors */
1242 dma_free_coherent(&priv->pdev->dev,
1243 bd_num * sizeof(struct tsi721_dma_desc),
1244 bd_ptr, bd_phys);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001245 priv->mdma.bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001246 return -ENOMEM;
1247 }
1248
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001249 priv->mdma.sts_phys = sts_phys;
1250 priv->mdma.sts_base = sts_ptr;
1251 priv->mdma.sts_size = sts_size;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001252
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001253 dev_dbg(&priv->pdev->dev,
1254 "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
1255 sts_ptr, (unsigned long long)sts_phys, sts_size);
1256
1257 /* Initialize DMA descriptors ring */
1258 bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
1259 bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
1260 TSI721_DMAC_DPTRL_MASK);
1261 bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
1262
1263 /* Setup DMA descriptor pointers */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001264 iowrite32(((u64)bd_phys >> 32), regs + TSI721_DMAC_DPTRH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001265 iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001266 regs + TSI721_DMAC_DPTRL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001267
1268 /* Setup descriptor status FIFO */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001269 iowrite32(((u64)sts_phys >> 32), regs + TSI721_DMAC_DSBH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001270 iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001271 regs + TSI721_DMAC_DSBL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001272 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001273 regs + TSI721_DMAC_DSSZ);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001274
1275 /* Clear interrupt bits */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001276 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001277
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001278 ioread32(regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001279
1280 /* Toggle DMA channel initialization */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001281 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
1282 ioread32(regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001283 udelay(10);
1284
1285 return 0;
1286}
1287
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001288static int tsi721_bdma_maint_free(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001289{
1290 u32 ch_stat;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001291 struct tsi721_bdma_maint *mdma = &priv->mdma;
1292 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(mdma->ch_id);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001293
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001294 if (mdma->bd_base == NULL)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001295 return 0;
1296
1297 /* Check if DMA channel still running */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001298 ch_stat = ioread32(regs + TSI721_DMAC_STS);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001299 if (ch_stat & TSI721_DMAC_STS_RUN)
1300 return -EFAULT;
1301
1302 /* Put DMA channel into init state */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001303 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001304
1305 /* Free space allocated for DMA descriptors */
1306 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001307 mdma->bd_num * sizeof(struct tsi721_dma_desc),
1308 mdma->bd_base, mdma->bd_phys);
1309 mdma->bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001310
1311 /* Free space allocated for status FIFO */
1312 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001313 mdma->sts_size * sizeof(struct tsi721_dma_sts),
1314 mdma->sts_base, mdma->sts_phys);
1315 mdma->sts_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001316 return 0;
1317}
1318
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001319/* Enable Inbound Messaging Interrupts */
1320static void
1321tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
1322 u32 inte_mask)
1323{
1324 u32 rval;
1325
1326 if (!inte_mask)
1327 return;
1328
1329 /* Clear pending Inbound Messaging interrupts */
1330 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1331
1332 /* Enable Inbound Messaging interrupts */
1333 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1334 iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
1335
1336 if (priv->flags & TSI721_USING_MSIX)
1337 return; /* Finished if we are in MSI-X mode */
1338
1339 /*
1340 * For MSI and INTA interrupt signalling we need to enable next levels
1341 */
1342
1343 /* Enable Device Channel Interrupt */
1344 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1345 iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
1346 priv->regs + TSI721_DEV_CHAN_INTE);
1347}
1348
1349/* Disable Inbound Messaging Interrupts */
1350static void
1351tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
1352 u32 inte_mask)
1353{
1354 u32 rval;
1355
1356 if (!inte_mask)
1357 return;
1358
1359 /* Clear pending Inbound Messaging interrupts */
1360 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1361
1362 /* Disable Inbound Messaging interrupts */
1363 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1364 rval &= ~inte_mask;
1365 iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
1366
1367 if (priv->flags & TSI721_USING_MSIX)
1368 return; /* Finished if we are in MSI-X mode */
1369
1370 /*
1371 * For MSI and INTA interrupt signalling we need to disable next levels
1372 */
1373
1374 /* Disable Device Channel Interrupt */
1375 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1376 rval &= ~TSI721_INT_IMSG_CHAN(ch);
1377 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1378}
1379
1380/* Enable Outbound Messaging interrupts */
1381static void
1382tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
1383 u32 inte_mask)
1384{
1385 u32 rval;
1386
1387 if (!inte_mask)
1388 return;
1389
1390 /* Clear pending Outbound Messaging interrupts */
1391 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1392
1393 /* Enable Outbound Messaging channel interrupts */
1394 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1395 iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
1396
1397 if (priv->flags & TSI721_USING_MSIX)
1398 return; /* Finished if we are in MSI-X mode */
1399
1400 /*
1401 * For MSI and INTA interrupt signalling we need to enable next levels
1402 */
1403
1404 /* Enable Device Channel Interrupt */
1405 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1406 iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
1407 priv->regs + TSI721_DEV_CHAN_INTE);
1408}
1409
1410/* Disable Outbound Messaging interrupts */
1411static void
1412tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
1413 u32 inte_mask)
1414{
1415 u32 rval;
1416
1417 if (!inte_mask)
1418 return;
1419
1420 /* Clear pending Outbound Messaging interrupts */
1421 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1422
1423 /* Disable Outbound Messaging interrupts */
1424 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1425 rval &= ~inte_mask;
1426 iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
1427
1428 if (priv->flags & TSI721_USING_MSIX)
1429 return; /* Finished if we are in MSI-X mode */
1430
1431 /*
1432 * For MSI and INTA interrupt signalling we need to disable next levels
1433 */
1434
1435 /* Disable Device Channel Interrupt */
1436 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1437 rval &= ~TSI721_INT_OMSG_CHAN(ch);
1438 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1439}
1440
1441/**
1442 * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
1443 * @mport: Master port with outbound message queue
1444 * @rdev: Target of outbound message
1445 * @mbox: Outbound mailbox
1446 * @buffer: Message to add to outbound queue
1447 * @len: Length of message
1448 */
1449static int
1450tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
1451 void *buffer, size_t len)
1452{
1453 struct tsi721_device *priv = mport->priv;
1454 struct tsi721_omsg_desc *desc;
1455 u32 tx_slot;
1456
1457 if (!priv->omsg_init[mbox] ||
1458 len > TSI721_MSG_MAX_SIZE || len < 8)
1459 return -EINVAL;
1460
1461 tx_slot = priv->omsg_ring[mbox].tx_slot;
1462
1463 /* Copy copy message into transfer buffer */
1464 memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
1465
1466 if (len & 0x7)
1467 len += 8;
1468
1469 /* Build descriptor associated with buffer */
1470 desc = priv->omsg_ring[mbox].omd_base;
1471 desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
1472 if (tx_slot % 4 == 0)
1473 desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
1474
1475 desc[tx_slot].msg_info =
1476 cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
1477 (0xe << 12) | (len & 0xff8));
1478 desc[tx_slot].bufptr_lo =
1479 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
1480 0xffffffff);
1481 desc[tx_slot].bufptr_hi =
1482 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
1483
1484 priv->omsg_ring[mbox].wr_count++;
1485
1486 /* Go to next descriptor */
1487 if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
1488 priv->omsg_ring[mbox].tx_slot = 0;
1489 /* Move through the ring link descriptor at the end */
1490 priv->omsg_ring[mbox].wr_count++;
1491 }
1492
1493 mb();
1494
1495 /* Set new write count value */
1496 iowrite32(priv->omsg_ring[mbox].wr_count,
1497 priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1498 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1499
1500 return 0;
1501}
1502
1503/**
1504 * tsi721_omsg_handler - Outbound Message Interrupt Handler
1505 * @priv: pointer to tsi721 private data
1506 * @ch: number of OB MSG channel to service
1507 *
1508 * Services channel interrupts from outbound messaging engine.
1509 */
1510static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
1511{
1512 u32 omsg_int;
Alexandre Bounine748353c2016-03-22 14:26:23 -07001513 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001514
1515 spin_lock(&priv->omsg_ring[ch].lock);
1516
1517 omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
1518
1519 if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
1520 dev_info(&priv->pdev->dev,
1521 "OB MBOX%d: Status FIFO is full\n", ch);
1522
1523 if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
1524 u32 srd_ptr;
1525 u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
1526 int i, j;
1527 u32 tx_slot;
1528
1529 /*
1530 * Find last successfully processed descriptor
1531 */
1532
1533 /* Check and clear descriptor status FIFO entries */
1534 srd_ptr = priv->omsg_ring[ch].sts_rdptr;
1535 sts_ptr = priv->omsg_ring[ch].sts_base;
1536 j = srd_ptr * 8;
1537 while (sts_ptr[j]) {
1538 for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
1539 prev_ptr = last_ptr;
1540 last_ptr = le64_to_cpu(sts_ptr[j]);
1541 sts_ptr[j] = 0;
1542 }
1543
1544 ++srd_ptr;
1545 srd_ptr %= priv->omsg_ring[ch].sts_size;
1546 j = srd_ptr * 8;
1547 }
1548
1549 if (last_ptr == 0)
1550 goto no_sts_update;
1551
1552 priv->omsg_ring[ch].sts_rdptr = srd_ptr;
1553 iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
1554
Alexandre Bounine748353c2016-03-22 14:26:23 -07001555 if (!mport->outb_msg[ch].mcback)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001556 goto no_sts_update;
1557
1558 /* Inform upper layer about transfer completion */
1559
1560 tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
1561 sizeof(struct tsi721_omsg_desc);
1562
1563 /*
1564 * Check if this is a Link Descriptor (LD).
1565 * If yes, ignore LD and use descriptor processed
1566 * before LD.
1567 */
1568 if (tx_slot == priv->omsg_ring[ch].size) {
1569 if (prev_ptr)
1570 tx_slot = (prev_ptr -
1571 (u64)priv->omsg_ring[ch].omd_phys)/
1572 sizeof(struct tsi721_omsg_desc);
1573 else
1574 goto no_sts_update;
1575 }
1576
1577 /* Move slot index to the next message to be sent */
1578 ++tx_slot;
1579 if (tx_slot == priv->omsg_ring[ch].size)
1580 tx_slot = 0;
1581 BUG_ON(tx_slot >= priv->omsg_ring[ch].size);
Alexandre Bounine748353c2016-03-22 14:26:23 -07001582 mport->outb_msg[ch].mcback(mport,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001583 priv->omsg_ring[ch].dev_id, ch,
1584 tx_slot);
1585 }
1586
1587no_sts_update:
1588
1589 if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
1590 /*
1591 * Outbound message operation aborted due to error,
1592 * reinitialize OB MSG channel
1593 */
1594
1595 dev_dbg(&priv->pdev->dev, "OB MSG ABORT ch_stat=%x\n",
1596 ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
1597
1598 iowrite32(TSI721_OBDMAC_INT_ERROR,
1599 priv->regs + TSI721_OBDMAC_INT(ch));
1600 iowrite32(TSI721_OBDMAC_CTL_INIT,
1601 priv->regs + TSI721_OBDMAC_CTL(ch));
1602 ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
1603
1604 /* Inform upper level to clear all pending tx slots */
Alexandre Bounine748353c2016-03-22 14:26:23 -07001605 if (mport->outb_msg[ch].mcback)
1606 mport->outb_msg[ch].mcback(mport,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001607 priv->omsg_ring[ch].dev_id, ch,
1608 priv->omsg_ring[ch].tx_slot);
1609 /* Synch tx_slot tracking */
1610 iowrite32(priv->omsg_ring[ch].tx_slot,
1611 priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1612 ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1613 priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
1614 priv->omsg_ring[ch].sts_rdptr = 0;
1615 }
1616
1617 /* Clear channel interrupts */
1618 iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
1619
1620 if (!(priv->flags & TSI721_USING_MSIX)) {
1621 u32 ch_inte;
1622
1623 /* Re-enable channel interrupts */
1624 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1625 ch_inte |= TSI721_INT_OMSG_CHAN(ch);
1626 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1627 }
1628
1629 spin_unlock(&priv->omsg_ring[ch].lock);
1630}
1631
1632/**
1633 * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
1634 * @mport: Master port implementing Outbound Messaging Engine
1635 * @dev_id: Device specific pointer to pass on event
1636 * @mbox: Mailbox to open
1637 * @entries: Number of entries in the outbound mailbox ring
1638 */
1639static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
1640 int mbox, int entries)
1641{
1642 struct tsi721_device *priv = mport->priv;
1643 struct tsi721_omsg_desc *bd_ptr;
1644 int i, rc = 0;
1645
1646 if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
1647 (entries > (TSI721_OMSGD_RING_SIZE)) ||
1648 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1649 rc = -EINVAL;
1650 goto out;
1651 }
1652
1653 priv->omsg_ring[mbox].dev_id = dev_id;
1654 priv->omsg_ring[mbox].size = entries;
1655 priv->omsg_ring[mbox].sts_rdptr = 0;
1656 spin_lock_init(&priv->omsg_ring[mbox].lock);
1657
1658 /* Outbound Msg Buffer allocation based on
1659 the number of maximum descriptor entries */
1660 for (i = 0; i < entries; i++) {
1661 priv->omsg_ring[mbox].omq_base[i] =
1662 dma_alloc_coherent(
1663 &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
1664 &priv->omsg_ring[mbox].omq_phys[i],
1665 GFP_KERNEL);
1666 if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
1667 dev_dbg(&priv->pdev->dev,
1668 "Unable to allocate OB MSG data buffer for"
1669 " MBOX%d\n", mbox);
1670 rc = -ENOMEM;
1671 goto out_buf;
1672 }
1673 }
1674
1675 /* Outbound message descriptor allocation */
1676 priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
1677 &priv->pdev->dev,
1678 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1679 &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
1680 if (priv->omsg_ring[mbox].omd_base == NULL) {
1681 dev_dbg(&priv->pdev->dev,
1682 "Unable to allocate OB MSG descriptor memory "
1683 "for MBOX%d\n", mbox);
1684 rc = -ENOMEM;
1685 goto out_buf;
1686 }
1687
1688 priv->omsg_ring[mbox].tx_slot = 0;
1689
1690 /* Outbound message descriptor status FIFO allocation */
1691 priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001692 priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001693 priv->omsg_ring[mbox].sts_size *
1694 sizeof(struct tsi721_dma_sts),
1695 &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
1696 if (priv->omsg_ring[mbox].sts_base == NULL) {
1697 dev_dbg(&priv->pdev->dev,
1698 "Unable to allocate OB MSG descriptor status FIFO "
1699 "for MBOX%d\n", mbox);
1700 rc = -ENOMEM;
1701 goto out_desc;
1702 }
1703
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001704 /*
1705 * Configure Outbound Messaging Engine
1706 */
1707
1708 /* Setup Outbound Message descriptor pointer */
1709 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
1710 priv->regs + TSI721_OBDMAC_DPTRH(mbox));
1711 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
1712 TSI721_OBDMAC_DPTRL_MASK),
1713 priv->regs + TSI721_OBDMAC_DPTRL(mbox));
1714
1715 /* Setup Outbound Message descriptor status FIFO */
1716 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
1717 priv->regs + TSI721_OBDMAC_DSBH(mbox));
1718 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
1719 TSI721_OBDMAC_DSBL_MASK),
1720 priv->regs + TSI721_OBDMAC_DSBL(mbox));
1721 iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
1722 priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
1723
1724 /* Enable interrupts */
1725
1726#ifdef CONFIG_PCI_MSI
1727 if (priv->flags & TSI721_USING_MSIX) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07001728 int idx = TSI721_VECT_OMB0_DONE + mbox;
1729
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001730 /* Request interrupt service if we are in MSI-X mode */
Alexandre Bounine748353c2016-03-22 14:26:23 -07001731 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1732 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001733
1734 if (rc) {
1735 dev_dbg(&priv->pdev->dev,
1736 "Unable to allocate MSI-X interrupt for "
1737 "OBOX%d-DONE\n", mbox);
1738 goto out_stat;
1739 }
1740
Alexandre Bounine748353c2016-03-22 14:26:23 -07001741 idx = TSI721_VECT_OMB0_INT + mbox;
1742 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1743 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001744
1745 if (rc) {
1746 dev_dbg(&priv->pdev->dev,
1747 "Unable to allocate MSI-X interrupt for "
1748 "MBOX%d-INT\n", mbox);
Alexandre Bounine748353c2016-03-22 14:26:23 -07001749 idx = TSI721_VECT_OMB0_DONE + mbox;
1750 free_irq(priv->msix[idx].vector, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001751 goto out_stat;
1752 }
1753 }
1754#endif /* CONFIG_PCI_MSI */
1755
1756 tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1757
1758 /* Initialize Outbound Message descriptors ring */
1759 bd_ptr = priv->omsg_ring[mbox].omd_base;
1760 bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
1761 bd_ptr[entries].msg_info = 0;
1762 bd_ptr[entries].next_lo =
1763 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
1764 TSI721_OBDMAC_DPTRL_MASK);
1765 bd_ptr[entries].next_hi =
1766 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
1767 priv->omsg_ring[mbox].wr_count = 0;
1768 mb();
1769
1770 /* Initialize Outbound Message engine */
1771 iowrite32(TSI721_OBDMAC_CTL_INIT, priv->regs + TSI721_OBDMAC_CTL(mbox));
1772 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1773 udelay(10);
1774
1775 priv->omsg_init[mbox] = 1;
1776
1777 return 0;
1778
1779#ifdef CONFIG_PCI_MSI
1780out_stat:
1781 dma_free_coherent(&priv->pdev->dev,
1782 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1783 priv->omsg_ring[mbox].sts_base,
1784 priv->omsg_ring[mbox].sts_phys);
1785
1786 priv->omsg_ring[mbox].sts_base = NULL;
1787#endif /* CONFIG_PCI_MSI */
1788
1789out_desc:
1790 dma_free_coherent(&priv->pdev->dev,
1791 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1792 priv->omsg_ring[mbox].omd_base,
1793 priv->omsg_ring[mbox].omd_phys);
1794
1795 priv->omsg_ring[mbox].omd_base = NULL;
1796
1797out_buf:
1798 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1799 if (priv->omsg_ring[mbox].omq_base[i]) {
1800 dma_free_coherent(&priv->pdev->dev,
1801 TSI721_MSG_BUFFER_SIZE,
1802 priv->omsg_ring[mbox].omq_base[i],
1803 priv->omsg_ring[mbox].omq_phys[i]);
1804
1805 priv->omsg_ring[mbox].omq_base[i] = NULL;
1806 }
1807 }
1808
1809out:
1810 return rc;
1811}
1812
1813/**
1814 * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
1815 * @mport: Master port implementing the outbound message unit
1816 * @mbox: Mailbox to close
1817 */
1818static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
1819{
1820 struct tsi721_device *priv = mport->priv;
1821 u32 i;
1822
1823 if (!priv->omsg_init[mbox])
1824 return;
1825 priv->omsg_init[mbox] = 0;
1826
1827 /* Disable Interrupts */
1828
1829 tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1830
1831#ifdef CONFIG_PCI_MSI
1832 if (priv->flags & TSI721_USING_MSIX) {
1833 free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07001834 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001835 free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07001836 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001837 }
1838#endif /* CONFIG_PCI_MSI */
1839
1840 /* Free OMSG Descriptor Status FIFO */
1841 dma_free_coherent(&priv->pdev->dev,
1842 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1843 priv->omsg_ring[mbox].sts_base,
1844 priv->omsg_ring[mbox].sts_phys);
1845
1846 priv->omsg_ring[mbox].sts_base = NULL;
1847
1848 /* Free OMSG descriptors */
1849 dma_free_coherent(&priv->pdev->dev,
1850 (priv->omsg_ring[mbox].size + 1) *
1851 sizeof(struct tsi721_omsg_desc),
1852 priv->omsg_ring[mbox].omd_base,
1853 priv->omsg_ring[mbox].omd_phys);
1854
1855 priv->omsg_ring[mbox].omd_base = NULL;
1856
1857 /* Free message buffers */
1858 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1859 if (priv->omsg_ring[mbox].omq_base[i]) {
1860 dma_free_coherent(&priv->pdev->dev,
1861 TSI721_MSG_BUFFER_SIZE,
1862 priv->omsg_ring[mbox].omq_base[i],
1863 priv->omsg_ring[mbox].omq_phys[i]);
1864
1865 priv->omsg_ring[mbox].omq_base[i] = NULL;
1866 }
1867 }
1868}
1869
1870/**
1871 * tsi721_imsg_handler - Inbound Message Interrupt Handler
1872 * @priv: pointer to tsi721 private data
1873 * @ch: inbound message channel number to service
1874 *
1875 * Services channel interrupts from inbound messaging engine.
1876 */
1877static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
1878{
1879 u32 mbox = ch - 4;
1880 u32 imsg_int;
Alexandre Bounine748353c2016-03-22 14:26:23 -07001881 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001882
1883 spin_lock(&priv->imsg_ring[mbox].lock);
1884
1885 imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
1886
1887 if (imsg_int & TSI721_IBDMAC_INT_SRTO)
1888 dev_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout\n",
1889 mbox);
1890
1891 if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
1892 dev_info(&priv->pdev->dev, "IB MBOX%d PCIe error\n",
1893 mbox);
1894
1895 if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
1896 dev_info(&priv->pdev->dev,
1897 "IB MBOX%d IB free queue low\n", mbox);
1898
1899 /* Clear IB channel interrupts */
1900 iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
1901
1902 /* If an IB Msg is received notify the upper layer */
1903 if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
Alexandre Bounine748353c2016-03-22 14:26:23 -07001904 mport->inb_msg[mbox].mcback)
1905 mport->inb_msg[mbox].mcback(mport,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001906 priv->imsg_ring[mbox].dev_id, mbox, -1);
1907
1908 if (!(priv->flags & TSI721_USING_MSIX)) {
1909 u32 ch_inte;
1910
1911 /* Re-enable channel interrupts */
1912 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1913 ch_inte |= TSI721_INT_IMSG_CHAN(ch);
1914 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1915 }
1916
1917 spin_unlock(&priv->imsg_ring[mbox].lock);
1918}
1919
1920/**
1921 * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
1922 * @mport: Master port implementing the Inbound Messaging Engine
1923 * @dev_id: Device specific pointer to pass on event
1924 * @mbox: Mailbox to open
1925 * @entries: Number of entries in the inbound mailbox ring
1926 */
1927static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
1928 int mbox, int entries)
1929{
1930 struct tsi721_device *priv = mport->priv;
1931 int ch = mbox + 4;
1932 int i;
1933 u64 *free_ptr;
1934 int rc = 0;
1935
1936 if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
1937 (entries > TSI721_IMSGD_RING_SIZE) ||
1938 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1939 rc = -EINVAL;
1940 goto out;
1941 }
1942
1943 /* Initialize IB Messaging Ring */
1944 priv->imsg_ring[mbox].dev_id = dev_id;
1945 priv->imsg_ring[mbox].size = entries;
1946 priv->imsg_ring[mbox].rx_slot = 0;
1947 priv->imsg_ring[mbox].desc_rdptr = 0;
1948 priv->imsg_ring[mbox].fq_wrptr = 0;
1949 for (i = 0; i < priv->imsg_ring[mbox].size; i++)
1950 priv->imsg_ring[mbox].imq_base[i] = NULL;
1951 spin_lock_init(&priv->imsg_ring[mbox].lock);
1952
1953 /* Allocate buffers for incoming messages */
1954 priv->imsg_ring[mbox].buf_base =
1955 dma_alloc_coherent(&priv->pdev->dev,
1956 entries * TSI721_MSG_BUFFER_SIZE,
1957 &priv->imsg_ring[mbox].buf_phys,
1958 GFP_KERNEL);
1959
1960 if (priv->imsg_ring[mbox].buf_base == NULL) {
1961 dev_err(&priv->pdev->dev,
1962 "Failed to allocate buffers for IB MBOX%d\n", mbox);
1963 rc = -ENOMEM;
1964 goto out;
1965 }
1966
1967 /* Allocate memory for circular free list */
1968 priv->imsg_ring[mbox].imfq_base =
1969 dma_alloc_coherent(&priv->pdev->dev,
1970 entries * 8,
1971 &priv->imsg_ring[mbox].imfq_phys,
1972 GFP_KERNEL);
1973
1974 if (priv->imsg_ring[mbox].imfq_base == NULL) {
1975 dev_err(&priv->pdev->dev,
1976 "Failed to allocate free queue for IB MBOX%d\n", mbox);
1977 rc = -ENOMEM;
1978 goto out_buf;
1979 }
1980
1981 /* Allocate memory for Inbound message descriptors */
1982 priv->imsg_ring[mbox].imd_base =
1983 dma_alloc_coherent(&priv->pdev->dev,
1984 entries * sizeof(struct tsi721_imsg_desc),
1985 &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
1986
1987 if (priv->imsg_ring[mbox].imd_base == NULL) {
1988 dev_err(&priv->pdev->dev,
1989 "Failed to allocate descriptor memory for IB MBOX%d\n",
1990 mbox);
1991 rc = -ENOMEM;
1992 goto out_dma;
1993 }
1994
1995 /* Fill free buffer pointer list */
1996 free_ptr = priv->imsg_ring[mbox].imfq_base;
1997 for (i = 0; i < entries; i++)
1998 free_ptr[i] = cpu_to_le64(
1999 (u64)(priv->imsg_ring[mbox].buf_phys) +
2000 i * 0x1000);
2001
2002 mb();
2003
2004 /*
2005 * For mapping of inbound SRIO Messages into appropriate queues we need
2006 * to set Inbound Device ID register in the messaging engine. We do it
2007 * once when first inbound mailbox is requested.
2008 */
2009 if (!(priv->flags & TSI721_IMSGID_SET)) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07002010 iowrite32((u32)priv->mport.host_deviceid,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002011 priv->regs + TSI721_IB_DEVID);
2012 priv->flags |= TSI721_IMSGID_SET;
2013 }
2014
2015 /*
2016 * Configure Inbound Messaging channel (ch = mbox + 4)
2017 */
2018
2019 /* Setup Inbound Message free queue */
2020 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
2021 priv->regs + TSI721_IBDMAC_FQBH(ch));
2022 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
2023 TSI721_IBDMAC_FQBL_MASK),
2024 priv->regs+TSI721_IBDMAC_FQBL(ch));
2025 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2026 priv->regs + TSI721_IBDMAC_FQSZ(ch));
2027
2028 /* Setup Inbound Message descriptor queue */
2029 iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
2030 priv->regs + TSI721_IBDMAC_DQBH(ch));
2031 iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
2032 (u32)TSI721_IBDMAC_DQBL_MASK),
2033 priv->regs+TSI721_IBDMAC_DQBL(ch));
2034 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2035 priv->regs + TSI721_IBDMAC_DQSZ(ch));
2036
2037 /* Enable interrupts */
2038
2039#ifdef CONFIG_PCI_MSI
2040 if (priv->flags & TSI721_USING_MSIX) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07002041 int idx = TSI721_VECT_IMB0_RCV + mbox;
2042
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002043 /* Request interrupt service if we are in MSI-X mode */
Alexandre Bounine748353c2016-03-22 14:26:23 -07002044 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2045 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002046
2047 if (rc) {
2048 dev_dbg(&priv->pdev->dev,
2049 "Unable to allocate MSI-X interrupt for "
2050 "IBOX%d-DONE\n", mbox);
2051 goto out_desc;
2052 }
2053
Alexandre Bounine748353c2016-03-22 14:26:23 -07002054 idx = TSI721_VECT_IMB0_INT + mbox;
2055 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2056 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002057
2058 if (rc) {
2059 dev_dbg(&priv->pdev->dev,
2060 "Unable to allocate MSI-X interrupt for "
2061 "IBOX%d-INT\n", mbox);
2062 free_irq(
2063 priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002064 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002065 goto out_desc;
2066 }
2067 }
2068#endif /* CONFIG_PCI_MSI */
2069
2070 tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
2071
2072 /* Initialize Inbound Message Engine */
2073 iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
2074 ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
2075 udelay(10);
2076 priv->imsg_ring[mbox].fq_wrptr = entries - 1;
2077 iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
2078
2079 priv->imsg_init[mbox] = 1;
2080 return 0;
2081
2082#ifdef CONFIG_PCI_MSI
2083out_desc:
2084 dma_free_coherent(&priv->pdev->dev,
2085 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2086 priv->imsg_ring[mbox].imd_base,
2087 priv->imsg_ring[mbox].imd_phys);
2088
2089 priv->imsg_ring[mbox].imd_base = NULL;
2090#endif /* CONFIG_PCI_MSI */
2091
2092out_dma:
2093 dma_free_coherent(&priv->pdev->dev,
2094 priv->imsg_ring[mbox].size * 8,
2095 priv->imsg_ring[mbox].imfq_base,
2096 priv->imsg_ring[mbox].imfq_phys);
2097
2098 priv->imsg_ring[mbox].imfq_base = NULL;
2099
2100out_buf:
2101 dma_free_coherent(&priv->pdev->dev,
2102 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2103 priv->imsg_ring[mbox].buf_base,
2104 priv->imsg_ring[mbox].buf_phys);
2105
2106 priv->imsg_ring[mbox].buf_base = NULL;
2107
2108out:
2109 return rc;
2110}
2111
2112/**
2113 * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
2114 * @mport: Master port implementing the Inbound Messaging Engine
2115 * @mbox: Mailbox to close
2116 */
2117static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
2118{
2119 struct tsi721_device *priv = mport->priv;
2120 u32 rx_slot;
2121 int ch = mbox + 4;
2122
2123 if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
2124 return;
2125 priv->imsg_init[mbox] = 0;
2126
2127 /* Disable Inbound Messaging Engine */
2128
2129 /* Disable Interrupts */
2130 tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
2131
2132#ifdef CONFIG_PCI_MSI
2133 if (priv->flags & TSI721_USING_MSIX) {
2134 free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002135 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002136 free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002137 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002138 }
2139#endif /* CONFIG_PCI_MSI */
2140
2141 /* Clear Inbound Buffer Queue */
2142 for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
2143 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2144
2145 /* Free memory allocated for message buffers */
2146 dma_free_coherent(&priv->pdev->dev,
2147 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2148 priv->imsg_ring[mbox].buf_base,
2149 priv->imsg_ring[mbox].buf_phys);
2150
2151 priv->imsg_ring[mbox].buf_base = NULL;
2152
2153 /* Free memory allocated for free pointr list */
2154 dma_free_coherent(&priv->pdev->dev,
2155 priv->imsg_ring[mbox].size * 8,
2156 priv->imsg_ring[mbox].imfq_base,
2157 priv->imsg_ring[mbox].imfq_phys);
2158
2159 priv->imsg_ring[mbox].imfq_base = NULL;
2160
2161 /* Free memory allocated for RX descriptors */
2162 dma_free_coherent(&priv->pdev->dev,
2163 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2164 priv->imsg_ring[mbox].imd_base,
2165 priv->imsg_ring[mbox].imd_phys);
2166
2167 priv->imsg_ring[mbox].imd_base = NULL;
2168}
2169
2170/**
2171 * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
2172 * @mport: Master port implementing the Inbound Messaging Engine
2173 * @mbox: Inbound mailbox number
2174 * @buf: Buffer to add to inbound queue
2175 */
2176static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
2177{
2178 struct tsi721_device *priv = mport->priv;
2179 u32 rx_slot;
2180 int rc = 0;
2181
2182 rx_slot = priv->imsg_ring[mbox].rx_slot;
2183 if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
2184 dev_err(&priv->pdev->dev,
2185 "Error adding inbound buffer %d, buffer exists\n",
2186 rx_slot);
2187 rc = -EINVAL;
2188 goto out;
2189 }
2190
2191 priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
2192
2193 if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
2194 priv->imsg_ring[mbox].rx_slot = 0;
2195
2196out:
2197 return rc;
2198}
2199
2200/**
2201 * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
2202 * @mport: Master port implementing the Inbound Messaging Engine
2203 * @mbox: Inbound mailbox number
2204 *
2205 * Returns pointer to the message on success or NULL on failure.
2206 */
2207static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
2208{
2209 struct tsi721_device *priv = mport->priv;
2210 struct tsi721_imsg_desc *desc;
2211 u32 rx_slot;
2212 void *rx_virt = NULL;
2213 u64 rx_phys;
2214 void *buf = NULL;
2215 u64 *free_ptr;
2216 int ch = mbox + 4;
2217 int msg_size;
2218
2219 if (!priv->imsg_init[mbox])
2220 return NULL;
2221
2222 desc = priv->imsg_ring[mbox].imd_base;
2223 desc += priv->imsg_ring[mbox].desc_rdptr;
2224
2225 if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
2226 goto out;
2227
2228 rx_slot = priv->imsg_ring[mbox].rx_slot;
2229 while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
2230 if (++rx_slot == priv->imsg_ring[mbox].size)
2231 rx_slot = 0;
2232 }
2233
2234 rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
2235 le32_to_cpu(desc->bufptr_lo);
2236
2237 rx_virt = priv->imsg_ring[mbox].buf_base +
2238 (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
2239
2240 buf = priv->imsg_ring[mbox].imq_base[rx_slot];
2241 msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
2242 if (msg_size == 0)
2243 msg_size = RIO_MAX_MSG_SIZE;
2244
2245 memcpy(buf, rx_virt, msg_size);
2246 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2247
2248 desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
2249 if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
2250 priv->imsg_ring[mbox].desc_rdptr = 0;
2251
2252 iowrite32(priv->imsg_ring[mbox].desc_rdptr,
2253 priv->regs + TSI721_IBDMAC_DQRP(ch));
2254
2255 /* Return free buffer into the pointer list */
2256 free_ptr = priv->imsg_ring[mbox].imfq_base;
2257 free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
2258
2259 if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
2260 priv->imsg_ring[mbox].fq_wrptr = 0;
2261
2262 iowrite32(priv->imsg_ring[mbox].fq_wrptr,
2263 priv->regs + TSI721_IBDMAC_FQWP(ch));
2264out:
2265 return buf;
2266}
2267
2268/**
2269 * tsi721_messages_init - Initialization of Messaging Engine
2270 * @priv: pointer to tsi721 private data
2271 *
2272 * Configures Tsi721 messaging engine.
2273 */
2274static int tsi721_messages_init(struct tsi721_device *priv)
2275{
2276 int ch;
2277
2278 iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
2279 iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
2280 iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
2281
2282 /* Set SRIO Message Request/Response Timeout */
2283 iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
2284
2285 /* Initialize Inbound Messaging Engine Registers */
2286 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
2287 /* Clear interrupt bits */
2288 iowrite32(TSI721_IBDMAC_INT_MASK,
2289 priv->regs + TSI721_IBDMAC_INT(ch));
2290 /* Clear Status */
2291 iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
2292
2293 iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
2294 priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
2295 iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
2296 priv->regs + TSI721_SMSG_ECC_NCOR(ch));
2297 }
2298
2299 return 0;
2300}
2301
2302/**
Alexandre Bouninedbe74af2016-03-22 14:26:02 -07002303 * tsi721_query_mport - Fetch inbound message from the Tsi721 MSG Queue
2304 * @mport: Master port implementing the Inbound Messaging Engine
2305 * @mbox: Inbound mailbox number
2306 *
2307 * Returns pointer to the message on success or NULL on failure.
2308 */
2309static int tsi721_query_mport(struct rio_mport *mport,
2310 struct rio_mport_attr *attr)
2311{
2312 struct tsi721_device *priv = mport->priv;
2313 u32 rval;
2314
2315 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_ERR_STS_CSR(0)));
2316 if (rval & RIO_PORT_N_ERR_STS_PORT_OK) {
2317 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL2_CSR(0)));
2318 attr->link_speed = (rval & RIO_PORT_N_CTL2_SEL_BAUD) >> 28;
2319 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL_CSR(0)));
2320 attr->link_width = (rval & RIO_PORT_N_CTL_IPW) >> 27;
2321 } else
2322 attr->link_speed = RIO_LINK_DOWN;
2323
2324#ifdef CONFIG_RAPIDIO_DMA_ENGINE
2325 attr->flags = RIO_MPORT_DMA | RIO_MPORT_DMA_SG;
2326 attr->dma_max_sge = 0;
2327 attr->dma_max_size = TSI721_BDMA_MAX_BCOUNT;
2328 attr->dma_align = 0;
2329#else
2330 attr->flags = 0;
2331#endif
2332 return 0;
2333}
2334
2335/**
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002336 * tsi721_disable_ints - disables all device interrupts
2337 * @priv: pointer to tsi721 private data
2338 */
2339static void tsi721_disable_ints(struct tsi721_device *priv)
2340{
2341 int ch;
2342
2343 /* Disable all device level interrupts */
2344 iowrite32(0, priv->regs + TSI721_DEV_INTE);
2345
2346 /* Disable all Device Channel interrupts */
2347 iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
2348
2349 /* Disable all Inbound Msg Channel interrupts */
2350 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
2351 iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
2352
2353 /* Disable all Outbound Msg Channel interrupts */
2354 for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
2355 iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
2356
2357 /* Disable all general messaging interrupts */
2358 iowrite32(0, priv->regs + TSI721_SMSG_INTE);
2359
2360 /* Disable all BDMA Channel interrupts */
2361 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002362 iowrite32(0,
2363 priv->regs + TSI721_DMAC_BASE(ch) + TSI721_DMAC_INTE);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002364
2365 /* Disable all general BDMA interrupts */
2366 iowrite32(0, priv->regs + TSI721_BDMA_INTE);
2367
2368 /* Disable all SRIO Channel interrupts */
2369 for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
2370 iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
2371
2372 /* Disable all general SR2PC interrupts */
2373 iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
2374
2375 /* Disable all PC2SR interrupts */
2376 iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
2377
2378 /* Disable all I2C interrupts */
2379 iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
2380
2381 /* Disable SRIO MAC interrupts */
2382 iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
2383 iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
2384}
2385
Alexandre Bounine748353c2016-03-22 14:26:23 -07002386static struct rio_ops tsi721_rio_ops = {
2387 .lcread = tsi721_lcread,
2388 .lcwrite = tsi721_lcwrite,
2389 .cread = tsi721_cread_dma,
2390 .cwrite = tsi721_cwrite_dma,
2391 .dsend = tsi721_dsend,
2392 .open_inb_mbox = tsi721_open_inb_mbox,
2393 .close_inb_mbox = tsi721_close_inb_mbox,
2394 .open_outb_mbox = tsi721_open_outb_mbox,
2395 .close_outb_mbox = tsi721_close_outb_mbox,
2396 .add_outb_message = tsi721_add_outb_message,
2397 .add_inb_buffer = tsi721_add_inb_buffer,
2398 .get_inb_message = tsi721_get_inb_message,
2399 .map_inb = tsi721_rio_map_inb_mem,
2400 .unmap_inb = tsi721_rio_unmap_inb_mem,
2401 .pwenable = tsi721_pw_enable,
2402 .query_mport = tsi721_query_mport,
2403};
2404
2405static void tsi721_mport_release(struct device *dev)
2406{
2407 struct rio_mport *mport = to_rio_mport(dev);
2408
2409 dev_dbg(dev, "RIO: %s %s id=%d\n", __func__, mport->name, mport->id);
2410}
2411
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002412/**
2413 * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
2414 * @priv: pointer to tsi721 private data
2415 *
2416 * Configures Tsi721 as RapidIO master port.
2417 */
Bill Pemberton305c8912012-11-19 13:23:25 -05002418static int tsi721_setup_mport(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002419{
2420 struct pci_dev *pdev = priv->pdev;
2421 int err = 0;
Alexandre Bounine748353c2016-03-22 14:26:23 -07002422 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002423
Alexandre Bounine748353c2016-03-22 14:26:23 -07002424 err = rio_mport_initialize(mport);
2425 if (err)
2426 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002427
Alexandre Bounine748353c2016-03-22 14:26:23 -07002428 mport->ops = &tsi721_rio_ops;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002429 mport->index = 0;
2430 mport->sys_size = 0; /* small system */
2431 mport->phy_type = RIO_PHY_SERIAL;
2432 mport->priv = (void *)priv;
2433 mport->phys_efptr = 0x100;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002434 mport->dev.parent = &pdev->dev;
Alexandre Bounine748353c2016-03-22 14:26:23 -07002435 mport->dev.release = tsi721_mport_release;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002436
2437 INIT_LIST_HEAD(&mport->dbells);
2438
2439 rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
Alexandre Bounineb439e662011-12-08 14:34:36 -08002440 rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
2441 rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
Alexandre Bounineed43f442012-10-04 17:15:51 -07002442 snprintf(mport->name, RIO_MAX_MPORT_NAME, "%s(%s)",
2443 dev_driver_string(&pdev->dev), dev_name(&pdev->dev));
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002444
2445 /* Hook up interrupt handler */
2446
2447#ifdef CONFIG_PCI_MSI
2448 if (!tsi721_enable_msix(priv))
2449 priv->flags |= TSI721_USING_MSIX;
2450 else if (!pci_enable_msi(pdev))
2451 priv->flags |= TSI721_USING_MSI;
2452 else
2453 dev_info(&pdev->dev,
2454 "MSI/MSI-X is not available. Using legacy INTx.\n");
2455#endif /* CONFIG_PCI_MSI */
2456
Alexandre Bounine748353c2016-03-22 14:26:23 -07002457 err = tsi721_request_irq(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002458
Alexandre Bounine748353c2016-03-22 14:26:23 -07002459 if (err) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002460 dev_err(&pdev->dev, "Unable to get assigned PCI IRQ "
2461 "vector %02X err=0x%x\n", pdev->irq, err);
Alexandre Bounine748353c2016-03-22 14:26:23 -07002462 return err;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002463 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002464
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002465#ifdef CONFIG_RAPIDIO_DMA_ENGINE
Alexandre Bounine748353c2016-03-22 14:26:23 -07002466 err = tsi721_register_dma(priv);
2467 if (err)
2468 goto err_exit;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002469#endif
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002470 /* Enable SRIO link */
2471 iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
2472 TSI721_DEVCTL_SRBOOT_CMPL,
2473 priv->regs + TSI721_DEVCTL);
2474
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002475 if (mport->host_deviceid >= 0)
2476 iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
2477 RIO_PORT_GEN_DISCOVERED,
2478 priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2479 else
2480 iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2481
Alexandre Bounine748353c2016-03-22 14:26:23 -07002482 err = rio_register_mport(mport);
2483 if (err) {
2484 tsi721_unregister_dma(priv);
2485 goto err_exit;
2486 }
2487
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002488 return 0;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002489
2490err_exit:
Alexandre Bounine748353c2016-03-22 14:26:23 -07002491 tsi721_free_irq(priv);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002492 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002493}
2494
Bill Pemberton305c8912012-11-19 13:23:25 -05002495static int tsi721_probe(struct pci_dev *pdev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002496 const struct pci_device_id *id)
2497{
2498 struct tsi721_device *priv;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002499 int err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002500
2501 priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
2502 if (priv == NULL) {
2503 dev_err(&pdev->dev, "Failed to allocate memory for device\n");
2504 err = -ENOMEM;
2505 goto err_exit;
2506 }
2507
2508 err = pci_enable_device(pdev);
2509 if (err) {
2510 dev_err(&pdev->dev, "Failed to enable PCI device\n");
2511 goto err_clean;
2512 }
2513
2514 priv->pdev = pdev;
2515
2516#ifdef DEBUG
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002517 {
2518 int i;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002519 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
2520 dev_dbg(&pdev->dev, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
2521 i, (unsigned long long)pci_resource_start(pdev, i),
2522 (unsigned long)pci_resource_len(pdev, i),
2523 pci_resource_flags(pdev, i));
2524 }
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002525 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002526#endif
2527 /*
2528 * Verify BAR configuration
2529 */
2530
2531 /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
2532 if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
2533 pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
2534 pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
2535 dev_err(&pdev->dev,
2536 "Missing or misconfigured CSR BAR0, aborting.\n");
2537 err = -ENODEV;
2538 goto err_disable_pdev;
2539 }
2540
2541 /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
2542 if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
2543 pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
2544 pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
2545 dev_err(&pdev->dev,
2546 "Missing or misconfigured Doorbell BAR1, aborting.\n");
2547 err = -ENODEV;
2548 goto err_disable_pdev;
2549 }
2550
2551 /*
2552 * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
2553 * space.
2554 * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
2555 * It may be a good idea to keep them disabled using HW configuration
2556 * to save PCI memory space.
2557 */
2558 if ((pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM) &&
2559 (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64)) {
2560 dev_info(&pdev->dev, "Outbound BAR2 is not used but enabled.\n");
2561 }
2562
2563 if ((pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM) &&
2564 (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64)) {
2565 dev_info(&pdev->dev, "Outbound BAR4 is not used but enabled.\n");
2566 }
2567
2568 err = pci_request_regions(pdev, DRV_NAME);
2569 if (err) {
2570 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
2571 "aborting.\n");
2572 goto err_disable_pdev;
2573 }
2574
2575 pci_set_master(pdev);
2576
2577 priv->regs = pci_ioremap_bar(pdev, BAR_0);
2578 if (!priv->regs) {
2579 dev_err(&pdev->dev,
2580 "Unable to map device registers space, aborting\n");
2581 err = -ENOMEM;
2582 goto err_free_res;
2583 }
2584
2585 priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
2586 if (!priv->odb_base) {
2587 dev_err(&pdev->dev,
2588 "Unable to map outbound doorbells space, aborting\n");
2589 err = -ENOMEM;
2590 goto err_unmap_bars;
2591 }
2592
2593 /* Configure DMA attributes. */
2594 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Peter Senna Tschudin18f62872012-10-04 17:15:55 -07002595 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2596 if (err) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002597 dev_info(&pdev->dev, "Unable to set DMA mask\n");
2598 goto err_unmap_bars;
2599 }
2600
2601 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2602 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2603 } else {
2604 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2605 if (err)
2606 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2607 }
2608
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002609 BUG_ON(!pci_is_pcie(pdev));
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002610
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002611 /* Clear "no snoop" and "relaxed ordering" bits. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002612 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002613 PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN, 0);
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002614
2615 /* Adjust PCIe completion timeout. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002616 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL2, 0xf, 0x2);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002617
2618 /*
2619 * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
2620 */
2621 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
2622 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
2623 TSI721_MSIXTBL_OFFSET);
2624 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
2625 TSI721_MSIXPBA_OFFSET);
2626 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
2627 /* End of FIXUP */
2628
2629 tsi721_disable_ints(priv);
2630
2631 tsi721_init_pc2sr_mapping(priv);
2632 tsi721_init_sr2pc_mapping(priv);
2633
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002634 if (tsi721_bdma_maint_init(priv)) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002635 dev_err(&pdev->dev, "BDMA initialization failed, aborting\n");
2636 err = -ENOMEM;
2637 goto err_unmap_bars;
2638 }
2639
2640 err = tsi721_doorbell_init(priv);
2641 if (err)
2642 goto err_free_bdma;
2643
2644 tsi721_port_write_init(priv);
2645
2646 err = tsi721_messages_init(priv);
2647 if (err)
2648 goto err_free_consistent;
2649
2650 err = tsi721_setup_mport(priv);
2651 if (err)
2652 goto err_free_consistent;
2653
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002654 pci_set_drvdata(pdev, priv);
Alexandre Bounine748353c2016-03-22 14:26:23 -07002655 tsi721_interrupts_init(priv);
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002656
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002657 return 0;
2658
2659err_free_consistent:
Alexandre Bounine748353c2016-03-22 14:26:23 -07002660 tsi721_port_write_free(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002661 tsi721_doorbell_free(priv);
2662err_free_bdma:
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002663 tsi721_bdma_maint_free(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002664err_unmap_bars:
2665 if (priv->regs)
2666 iounmap(priv->regs);
2667 if (priv->odb_base)
2668 iounmap(priv->odb_base);
2669err_free_res:
2670 pci_release_regions(pdev);
2671 pci_clear_master(pdev);
2672err_disable_pdev:
2673 pci_disable_device(pdev);
2674err_clean:
2675 kfree(priv);
2676err_exit:
2677 return err;
2678}
2679
Alexandre Bounine748353c2016-03-22 14:26:23 -07002680static void tsi721_remove(struct pci_dev *pdev)
2681{
2682 struct tsi721_device *priv = pci_get_drvdata(pdev);
2683
2684 dev_dbg(&pdev->dev, "%s enter\n", __func__);
2685
2686 tsi721_disable_ints(priv);
2687 tsi721_free_irq(priv);
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07002688 flush_scheduled_work();
Alexandre Bounine748353c2016-03-22 14:26:23 -07002689 rio_unregister_mport(&priv->mport);
2690
2691 tsi721_unregister_dma(priv);
2692 tsi721_bdma_maint_free(priv);
2693 tsi721_doorbell_free(priv);
2694 tsi721_port_write_free(priv);
2695 tsi721_close_sr2pc_mapping(priv);
2696
2697 if (priv->regs)
2698 iounmap(priv->regs);
2699 if (priv->odb_base)
2700 iounmap(priv->odb_base);
2701#ifdef CONFIG_PCI_MSI
2702 if (priv->flags & TSI721_USING_MSIX)
2703 pci_disable_msix(priv->pdev);
2704 else if (priv->flags & TSI721_USING_MSI)
2705 pci_disable_msi(priv->pdev);
2706#endif
2707 pci_release_regions(pdev);
2708 pci_clear_master(pdev);
2709 pci_disable_device(pdev);
2710 pci_set_drvdata(pdev, NULL);
2711 kfree(priv);
2712 dev_dbg(&pdev->dev, "%s exit\n", __func__);
2713}
2714
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002715static void tsi721_shutdown(struct pci_dev *pdev)
2716{
2717 struct tsi721_device *priv = pci_get_drvdata(pdev);
2718
2719 dev_dbg(&pdev->dev, "RIO: %s\n", __func__);
2720
2721 tsi721_disable_ints(priv);
2722 tsi721_dma_stop_all(priv);
2723 pci_clear_master(pdev);
2724 pci_disable_device(pdev);
2725}
2726
Benoit Taine9baa3c32014-08-08 15:56:03 +02002727static const struct pci_device_id tsi721_pci_tbl[] = {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002728 { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
2729 { 0, } /* terminate list */
2730};
2731
2732MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
2733
2734static struct pci_driver tsi721_driver = {
2735 .name = "tsi721",
2736 .id_table = tsi721_pci_tbl,
2737 .probe = tsi721_probe,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002738 .remove = tsi721_remove,
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002739 .shutdown = tsi721_shutdown,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002740};
2741
Alexandre Bounine748353c2016-03-22 14:26:23 -07002742module_pci_driver(tsi721_driver);
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002743
2744MODULE_DESCRIPTION("IDT Tsi721 PCIExpress-to-SRIO bridge driver");
2745MODULE_AUTHOR("Integrated Device Technology, Inc.");
2746MODULE_LICENSE("GPL");