blob: 74bc1fbb784e41638bdab02ea283c586ff969fe4 [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 Bounine1679e8d2016-03-22 14:26:53 -0700844static int
845tsi721_obw_alloc(struct tsi721_device *priv, struct tsi721_obw_bar *pbar,
846 u32 size, int *win_id)
847{
848 u64 win_base;
849 u64 bar_base;
850 u64 bar_end;
851 u32 align;
852 struct tsi721_ob_win *win;
853 struct tsi721_ob_win *new_win = NULL;
854 int new_win_idx = -1;
855 int i = 0;
856
857 bar_base = pbar->base;
858 bar_end = bar_base + pbar->size;
859 win_base = bar_base;
860 align = size/TSI721_PC2SR_ZONES;
861
862 while (i < TSI721_IBWIN_NUM) {
863 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
864 if (!priv->ob_win[i].active) {
865 if (new_win == NULL) {
866 new_win = &priv->ob_win[i];
867 new_win_idx = i;
868 }
869 continue;
870 }
871
872 /*
873 * If this window belongs to the current BAR check it
874 * for overlap
875 */
876 win = &priv->ob_win[i];
877
878 if (win->base >= bar_base && win->base < bar_end) {
879 if (win_base < (win->base + win->size) &&
880 (win_base + size) > win->base) {
881 /* Overlap detected */
882 win_base = win->base + win->size;
883 win_base = ALIGN(win_base, align);
884 break;
885 }
886 }
887 }
888 }
889
890 if (win_base + size > bar_end)
891 return -ENOMEM;
892
893 if (!new_win) {
894 dev_err(&priv->pdev->dev, "ERR: OBW count tracking failed\n");
895 return -EIO;
896 }
897
898 new_win->active = true;
899 new_win->base = win_base;
900 new_win->size = size;
901 new_win->pbar = pbar;
902 priv->obwin_cnt--;
903 pbar->free -= size;
904 *win_id = new_win_idx;
905 return 0;
906}
907
908static int tsi721_map_outb_win(struct rio_mport *mport, u16 destid, u64 rstart,
909 u32 size, u32 flags, dma_addr_t *laddr)
910{
911 struct tsi721_device *priv = mport->priv;
912 int i;
913 struct tsi721_obw_bar *pbar;
914 struct tsi721_ob_win *ob_win;
915 int obw = -1;
916 u32 rval;
917 u64 rio_addr;
918 u32 zsize;
919 int ret = -ENOMEM;
920
921 if (!is_power_of_2(size) || (size < 0x8000) || (rstart & (size - 1)))
922 return -EINVAL;
923
924 if (priv->obwin_cnt == 0)
925 return -EBUSY;
926
927 for (i = 0; i < 2; i++) {
928 if (priv->p2r_bar[i].free >= size) {
929 pbar = &priv->p2r_bar[i];
930 ret = tsi721_obw_alloc(priv, pbar, size, &obw);
931 if (!ret)
932 break;
933 }
934 }
935
936 if (ret)
937 return ret;
938
939 WARN_ON(obw == -1);
940 ob_win = &priv->ob_win[obw];
941 ob_win->destid = destid;
942 ob_win->rstart = rstart;
943
944 /*
945 * Configure Outbound Window
946 */
947
948 zsize = size/TSI721_PC2SR_ZONES;
949 rio_addr = rstart;
950
951 /*
952 * Program Address Translation Zones:
953 * This implementation uses all 8 zones associated wit window.
954 */
955 for (i = 0; i < TSI721_PC2SR_ZONES; i++) {
956
957 while (ioread32(priv->regs + TSI721_ZONE_SEL) &
958 TSI721_ZONE_SEL_GO) {
959 udelay(1);
960 }
961
962 rval = (u32)(rio_addr & TSI721_LUT_DATA0_ADD) |
963 TSI721_LUT_DATA0_NREAD | TSI721_LUT_DATA0_NWR;
964 iowrite32(rval, priv->regs + TSI721_LUT_DATA0);
965 rval = (u32)(rio_addr >> 32);
966 iowrite32(rval, priv->regs + TSI721_LUT_DATA1);
967 rval = destid;
968 iowrite32(rval, priv->regs + TSI721_LUT_DATA2);
969
970 rval = TSI721_ZONE_SEL_GO | (obw << 3) | i;
971 iowrite32(rval, priv->regs + TSI721_ZONE_SEL);
972
973 rio_addr += zsize;
974 }
975
976 iowrite32(TSI721_OBWIN_SIZE(size) << 8,
977 priv->regs + TSI721_OBWINSZ(obw));
978 iowrite32((u32)(ob_win->base >> 32), priv->regs + TSI721_OBWINUB(obw));
979 iowrite32((u32)(ob_win->base & TSI721_OBWINLB_BA) | TSI721_OBWINLB_WEN,
980 priv->regs + TSI721_OBWINLB(obw));
981
982 *laddr = ob_win->base;
983 return 0;
984}
985
986static void tsi721_unmap_outb_win(struct rio_mport *mport,
987 u16 destid, u64 rstart)
988{
989 struct tsi721_device *priv = mport->priv;
990 struct tsi721_ob_win *ob_win;
991 int i;
992
993 for (i = 0; i < TSI721_OBWIN_NUM; i++) {
994 ob_win = &priv->ob_win[i];
995
996 if (ob_win->active &&
997 ob_win->destid == destid && ob_win->rstart == rstart) {
998 ob_win->active = false;
999 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
1000 ob_win->pbar->free += ob_win->size;
1001 priv->obwin_cnt++;
1002 break;
1003 }
1004 }
1005}
1006
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001007/**
1008 * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
1009 * translation regions.
1010 * @priv: pointer to tsi721 private data
1011 *
1012 * Disables SREP translation regions.
1013 */
1014static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
1015{
Alexandre Bounine1679e8d2016-03-22 14:26:53 -07001016 int i, z;
1017 u32 rval;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001018
1019 /* Disable all PC2SR translation windows */
1020 for (i = 0; i < TSI721_OBWIN_NUM; i++)
1021 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
Alexandre Bounine1679e8d2016-03-22 14:26:53 -07001022
1023 /* Initialize zone lookup tables to avoid ECC errors on reads */
1024 iowrite32(0, priv->regs + TSI721_LUT_DATA0);
1025 iowrite32(0, priv->regs + TSI721_LUT_DATA1);
1026 iowrite32(0, priv->regs + TSI721_LUT_DATA2);
1027
1028 for (i = 0; i < TSI721_OBWIN_NUM; i++) {
1029 for (z = 0; z < TSI721_PC2SR_ZONES; z++) {
1030 while (ioread32(priv->regs + TSI721_ZONE_SEL) &
1031 TSI721_ZONE_SEL_GO) {
1032 udelay(1);
1033 }
1034 rval = TSI721_ZONE_SEL_GO | (i << 3) | z;
1035 iowrite32(rval, priv->regs + TSI721_ZONE_SEL);
1036 }
1037 }
1038
1039 if (priv->p2r_bar[0].size == 0 && priv->p2r_bar[1].size == 0) {
1040 priv->obwin_cnt = 0;
1041 return;
1042 }
1043
1044 priv->p2r_bar[0].free = priv->p2r_bar[0].size;
1045 priv->p2r_bar[1].free = priv->p2r_bar[1].size;
1046
1047 for (i = 0; i < TSI721_OBWIN_NUM; i++)
1048 priv->ob_win[i].active = false;
1049
1050 priv->obwin_cnt = TSI721_OBWIN_NUM;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001051}
1052
1053/**
Alexandre Bounine71afe342012-10-04 17:16:00 -07001054 * tsi721_rio_map_inb_mem -- Mapping inbound memory region.
1055 * @mport: RapidIO master port
1056 * @lstart: Local memory space start address.
1057 * @rstart: RapidIO space start address.
1058 * @size: The mapping region size.
1059 * @flags: Flags for mapping. 0 for using default flags.
1060 *
1061 * Return: 0 -- Success.
1062 *
1063 * This function will create the inbound mapping
1064 * from rstart to lstart.
1065 */
1066static int tsi721_rio_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
1067 u64 rstart, u32 size, u32 flags)
1068{
1069 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001070 int i, avail = -1;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001071 u32 regval;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001072 struct tsi721_ib_win *ib_win;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001073 bool direct = (lstart == rstart);
1074 u64 ibw_size;
1075 dma_addr_t loc_start;
1076 u64 ibw_start;
1077 struct tsi721_ib_win_mapping *map = NULL;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001078 int ret = -EBUSY;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001079
Alexandre Bounine9673b882016-03-22 14:25:54 -07001080 if (direct) {
1081 dev_dbg(&priv->pdev->dev,
1082 "Direct (RIO_0x%llx -> PCIe_0x%pad), size=0x%x",
1083 rstart, &lstart, size);
Alexandre Bounine71afe342012-10-04 17:16:00 -07001084
Alexandre Bounine9673b882016-03-22 14:25:54 -07001085 /* Calculate minimal acceptable window size and base address */
1086
1087 ibw_size = roundup_pow_of_two(size);
1088 ibw_start = lstart & ~(ibw_size - 1);
1089
1090 while ((lstart + size) > (ibw_start + ibw_size)) {
1091 ibw_size *= 2;
1092 ibw_start = lstart & ~(ibw_size - 1);
1093 if (ibw_size > 0x80000000) { /* Limit max size to 2GB */
1094 return -EBUSY;
1095 }
1096 }
1097
1098 loc_start = ibw_start;
1099
1100 map = kzalloc(sizeof(struct tsi721_ib_win_mapping), GFP_ATOMIC);
1101 if (map == NULL)
1102 return -ENOMEM;
1103
1104 } else {
1105 dev_dbg(&priv->pdev->dev,
1106 "Translated (RIO_0x%llx -> PCIe_0x%pad), size=0x%x",
1107 rstart, &lstart, size);
1108
1109 if (!is_power_of_2(size) || size < 0x1000 ||
1110 ((u64)lstart & (size - 1)) || (rstart & (size - 1)))
1111 return -EINVAL;
1112 if (priv->ibwin_cnt == 0)
1113 return -EBUSY;
1114 ibw_start = rstart;
1115 ibw_size = size;
1116 loc_start = lstart;
1117 }
1118
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001119 /*
1120 * Scan for overlapping with active regions and mark the first available
1121 * IB window at the same time.
1122 */
Alexandre Bounine71afe342012-10-04 17:16:00 -07001123 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001124 ib_win = &priv->ib_win[i];
Alexandre Bounine9673b882016-03-22 14:25:54 -07001125
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001126 if (!ib_win->active) {
1127 if (avail == -1) {
1128 avail = i;
1129 ret = 0;
1130 }
Alexandre Bounine9673b882016-03-22 14:25:54 -07001131 } else if (ibw_start < (ib_win->rstart + ib_win->size) &&
1132 (ibw_start + ibw_size) > ib_win->rstart) {
1133 /* Return error if address translation involved */
1134 if (direct && ib_win->xlat) {
1135 ret = -EFAULT;
1136 break;
1137 }
1138
1139 /*
1140 * Direct mappings usually are larger than originally
1141 * requested fragments - check if this new request fits
1142 * into it.
1143 */
1144 if (rstart >= ib_win->rstart &&
1145 (rstart + size) <= (ib_win->rstart +
1146 ib_win->size)) {
1147 /* We are in - no further mapping required */
1148 map->lstart = lstart;
1149 list_add_tail(&map->node, &ib_win->mappings);
1150 return 0;
1151 }
1152
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001153 ret = -EFAULT;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001154 break;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001155 }
Alexandre Bounine71afe342012-10-04 17:16:00 -07001156 }
1157
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001158 if (ret)
Alexandre Bounine9673b882016-03-22 14:25:54 -07001159 goto out;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001160 i = avail;
1161
1162 /* Sanity check: available IB window must be disabled at this point */
1163 regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
1164 if (WARN_ON(regval & TSI721_IBWIN_LB_WEN)) {
1165 ret = -EIO;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001166 goto out;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001167 }
1168
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001169 ib_win = &priv->ib_win[i];
1170 ib_win->active = true;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001171 ib_win->rstart = ibw_start;
1172 ib_win->lstart = loc_start;
1173 ib_win->size = ibw_size;
1174 ib_win->xlat = (lstart != rstart);
1175 INIT_LIST_HEAD(&ib_win->mappings);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001176
Alexandre Bounine9673b882016-03-22 14:25:54 -07001177 /*
1178 * When using direct IBW mapping and have larger than requested IBW size
1179 * we can have multiple local memory blocks mapped through the same IBW
1180 * To handle this situation we maintain list of "clients" for such IBWs.
1181 */
1182 if (direct) {
1183 map->lstart = lstart;
1184 list_add_tail(&map->node, &ib_win->mappings);
1185 }
1186
1187 iowrite32(TSI721_IBWIN_SIZE(ibw_size) << 8,
Alexandre Bounine71afe342012-10-04 17:16:00 -07001188 priv->regs + TSI721_IBWIN_SZ(i));
1189
Alexandre Bounine9673b882016-03-22 14:25:54 -07001190 iowrite32(((u64)loc_start >> 32), priv->regs + TSI721_IBWIN_TUA(i));
1191 iowrite32(((u64)loc_start & TSI721_IBWIN_TLA_ADD),
Alexandre Bounine71afe342012-10-04 17:16:00 -07001192 priv->regs + TSI721_IBWIN_TLA(i));
1193
Alexandre Bounine9673b882016-03-22 14:25:54 -07001194 iowrite32(ibw_start >> 32, priv->regs + TSI721_IBWIN_UB(i));
1195 iowrite32((ibw_start & TSI721_IBWIN_LB_BA) | TSI721_IBWIN_LB_WEN,
Alexandre Bounine71afe342012-10-04 17:16:00 -07001196 priv->regs + TSI721_IBWIN_LB(i));
Alexandre Bounine9673b882016-03-22 14:25:54 -07001197
1198 priv->ibwin_cnt--;
1199
Alexandre Bounine71afe342012-10-04 17:16:00 -07001200 dev_dbg(&priv->pdev->dev,
Alexandre Bounine9673b882016-03-22 14:25:54 -07001201 "Configured IBWIN%d (RIO_0x%llx -> PCIe_0x%llx), size=0x%llx\n",
1202 i, ibw_start, (unsigned long long)loc_start, ibw_size);
Alexandre Bounine71afe342012-10-04 17:16:00 -07001203
1204 return 0;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001205out:
1206 kfree(map);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001207 return ret;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001208}
1209
1210/**
Alexandre Bounine9673b882016-03-22 14:25:54 -07001211 * tsi721_rio_unmap_inb_mem -- Unmapping inbound memory region.
Alexandre Bounine71afe342012-10-04 17:16:00 -07001212 * @mport: RapidIO master port
1213 * @lstart: Local memory space start address.
1214 */
1215static void tsi721_rio_unmap_inb_mem(struct rio_mport *mport,
1216 dma_addr_t lstart)
1217{
1218 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001219 struct tsi721_ib_win *ib_win;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001220 int i;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001221
Alexandre Bounine9673b882016-03-22 14:25:54 -07001222 dev_dbg(&priv->pdev->dev,
1223 "Unmap IBW mapped to PCIe_0x%pad", &lstart);
1224
Alexandre Bounine71afe342012-10-04 17:16:00 -07001225 /* Search for matching active inbound translation window */
1226 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001227 ib_win = &priv->ib_win[i];
Alexandre Bounine9673b882016-03-22 14:25:54 -07001228
1229 /* Address translating IBWs must to be an exact march */
1230 if (!ib_win->active ||
1231 (ib_win->xlat && lstart != ib_win->lstart))
1232 continue;
1233
1234 if (lstart >= ib_win->lstart &&
1235 lstart < (ib_win->lstart + ib_win->size)) {
1236
1237 if (!ib_win->xlat) {
1238 struct tsi721_ib_win_mapping *map;
1239 int found = 0;
1240
1241 list_for_each_entry(map,
1242 &ib_win->mappings, node) {
1243 if (map->lstart == lstart) {
1244 list_del(&map->node);
1245 kfree(map);
1246 found = 1;
1247 break;
1248 }
1249 }
1250
1251 if (!found)
1252 continue;
1253
1254 if (!list_empty(&ib_win->mappings))
1255 break;
1256 }
1257
1258 dev_dbg(&priv->pdev->dev, "Disable IBWIN_%d", i);
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001259 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1260 ib_win->active = false;
Alexandre Bounine9673b882016-03-22 14:25:54 -07001261 priv->ibwin_cnt++;
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001262 break;
Alexandre Bounine71afe342012-10-04 17:16:00 -07001263 }
1264 }
Alexandre Bounineba5d1412016-03-22 14:25:51 -07001265
1266 if (i == TSI721_IBWIN_NUM)
Alexandre Bounine9673b882016-03-22 14:25:54 -07001267 dev_dbg(&priv->pdev->dev,
1268 "IB window mapped to %pad not found", &lstart);
Alexandre Bounine71afe342012-10-04 17:16:00 -07001269}
1270
1271/**
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001272 * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
1273 * translation regions.
1274 * @priv: pointer to tsi721 private data
1275 *
1276 * Disables inbound windows.
1277 */
1278static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
1279{
1280 int i;
1281
1282 /* Disable all SR2PC inbound windows */
1283 for (i = 0; i < TSI721_IBWIN_NUM; i++)
Alexandre Bounine71afe342012-10-04 17:16:00 -07001284 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
Alexandre Bounine9673b882016-03-22 14:25:54 -07001285 priv->ibwin_cnt = TSI721_IBWIN_NUM;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001286}
1287
Alexandre Bounine748353c2016-03-22 14:26:23 -07001288/*
1289 * tsi721_close_sr2pc_mapping - closes all active inbound (SRIO->PCIe)
1290 * translation regions.
1291 * @priv: pointer to tsi721 device private data
1292 */
1293static void tsi721_close_sr2pc_mapping(struct tsi721_device *priv)
1294{
1295 struct tsi721_ib_win *ib_win;
1296 int i;
1297
1298 /* Disable all active SR2PC inbound windows */
1299 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
1300 ib_win = &priv->ib_win[i];
1301 if (ib_win->active) {
1302 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
1303 ib_win->active = false;
1304 }
1305 }
1306}
1307
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001308/**
1309 * tsi721_port_write_init - Inbound port write interface init
1310 * @priv: pointer to tsi721 private data
1311 *
1312 * Initializes inbound port write handler.
1313 * Returns %0 on success or %-ENOMEM on failure.
1314 */
1315static int tsi721_port_write_init(struct tsi721_device *priv)
1316{
1317 priv->pw_discard_count = 0;
1318 INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
1319 spin_lock_init(&priv->pw_fifo_lock);
1320 if (kfifo_alloc(&priv->pw_fifo,
1321 TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
1322 dev_err(&priv->pdev->dev, "PW FIFO allocation failed\n");
1323 return -ENOMEM;
1324 }
1325
1326 /* Use reliable port-write capture mode */
1327 iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
1328 return 0;
1329}
1330
Alexandre Bounine748353c2016-03-22 14:26:23 -07001331static void tsi721_port_write_free(struct tsi721_device *priv)
1332{
1333 kfifo_free(&priv->pw_fifo);
1334}
1335
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001336static int tsi721_doorbell_init(struct tsi721_device *priv)
1337{
1338 /* Outbound Doorbells do not require any setup.
1339 * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
1340 * That BAR1 was mapped during the probe routine.
1341 */
1342
1343 /* Initialize Inbound Doorbell processing DPC and queue */
1344 priv->db_discard_count = 0;
1345 INIT_WORK(&priv->idb_work, tsi721_db_dpc);
1346
1347 /* Allocate buffer for inbound doorbells queue */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001348 priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001349 IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1350 &priv->idb_dma, GFP_KERNEL);
1351 if (!priv->idb_base)
1352 return -ENOMEM;
1353
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001354 dev_dbg(&priv->pdev->dev, "Allocated IDB buffer @ %p (phys = %llx)\n",
1355 priv->idb_base, (unsigned long long)priv->idb_dma);
1356
1357 iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
1358 priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
1359 iowrite32(((u64)priv->idb_dma >> 32),
1360 priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
1361 iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
1362 priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
1363 /* Enable accepting all inbound doorbells */
1364 iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
1365
1366 iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
1367
1368 iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
1369
1370 return 0;
1371}
1372
1373static void tsi721_doorbell_free(struct tsi721_device *priv)
1374{
1375 if (priv->idb_base == NULL)
1376 return;
1377
1378 /* Free buffer allocated for inbound doorbell queue */
1379 dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1380 priv->idb_base, priv->idb_dma);
1381 priv->idb_base = NULL;
1382}
1383
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001384/**
1385 * tsi721_bdma_maint_init - Initialize maintenance request BDMA channel.
1386 * @priv: pointer to tsi721 private data
1387 *
1388 * Initialize BDMA channel allocated for RapidIO maintenance read/write
1389 * request generation
1390 * Returns %0 on success or %-ENOMEM on failure.
1391 */
1392static int tsi721_bdma_maint_init(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001393{
1394 struct tsi721_dma_desc *bd_ptr;
1395 u64 *sts_ptr;
1396 dma_addr_t bd_phys, sts_phys;
1397 int sts_size;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001398 int bd_num = 2;
1399 void __iomem *regs;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001400
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001401 dev_dbg(&priv->pdev->dev,
1402 "Init Block DMA Engine for Maintenance requests, CH%d\n",
1403 TSI721_DMACH_MAINT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001404
1405 /*
1406 * Initialize DMA channel for maintenance requests
1407 */
1408
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001409 priv->mdma.ch_id = TSI721_DMACH_MAINT;
1410 regs = priv->regs + TSI721_DMAC_BASE(TSI721_DMACH_MAINT);
1411
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001412 /* Allocate space for DMA descriptors */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001413 bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001414 bd_num * sizeof(struct tsi721_dma_desc),
1415 &bd_phys, GFP_KERNEL);
1416 if (!bd_ptr)
1417 return -ENOMEM;
1418
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001419 priv->mdma.bd_num = bd_num;
1420 priv->mdma.bd_phys = bd_phys;
1421 priv->mdma.bd_base = bd_ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001422
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001423 dev_dbg(&priv->pdev->dev, "DMA descriptors @ %p (phys = %llx)\n",
1424 bd_ptr, (unsigned long long)bd_phys);
1425
1426 /* Allocate space for descriptor status FIFO */
1427 sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
1428 bd_num : TSI721_DMA_MINSTSSZ;
1429 sts_size = roundup_pow_of_two(sts_size);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001430 sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001431 sts_size * sizeof(struct tsi721_dma_sts),
1432 &sts_phys, GFP_KERNEL);
1433 if (!sts_ptr) {
1434 /* Free space allocated for DMA descriptors */
1435 dma_free_coherent(&priv->pdev->dev,
1436 bd_num * sizeof(struct tsi721_dma_desc),
1437 bd_ptr, bd_phys);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001438 priv->mdma.bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001439 return -ENOMEM;
1440 }
1441
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001442 priv->mdma.sts_phys = sts_phys;
1443 priv->mdma.sts_base = sts_ptr;
1444 priv->mdma.sts_size = sts_size;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001445
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001446 dev_dbg(&priv->pdev->dev,
1447 "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
1448 sts_ptr, (unsigned long long)sts_phys, sts_size);
1449
1450 /* Initialize DMA descriptors ring */
1451 bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
1452 bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
1453 TSI721_DMAC_DPTRL_MASK);
1454 bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
1455
1456 /* Setup DMA descriptor pointers */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001457 iowrite32(((u64)bd_phys >> 32), regs + TSI721_DMAC_DPTRH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001458 iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001459 regs + TSI721_DMAC_DPTRL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001460
1461 /* Setup descriptor status FIFO */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001462 iowrite32(((u64)sts_phys >> 32), regs + TSI721_DMAC_DSBH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001463 iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001464 regs + TSI721_DMAC_DSBL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001465 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001466 regs + TSI721_DMAC_DSSZ);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001467
1468 /* Clear interrupt bits */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001469 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001470
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001471 ioread32(regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001472
1473 /* Toggle DMA channel initialization */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001474 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
1475 ioread32(regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001476 udelay(10);
1477
1478 return 0;
1479}
1480
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001481static int tsi721_bdma_maint_free(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001482{
1483 u32 ch_stat;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001484 struct tsi721_bdma_maint *mdma = &priv->mdma;
1485 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(mdma->ch_id);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001486
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001487 if (mdma->bd_base == NULL)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001488 return 0;
1489
1490 /* Check if DMA channel still running */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001491 ch_stat = ioread32(regs + TSI721_DMAC_STS);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001492 if (ch_stat & TSI721_DMAC_STS_RUN)
1493 return -EFAULT;
1494
1495 /* Put DMA channel into init state */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001496 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001497
1498 /* Free space allocated for DMA descriptors */
1499 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001500 mdma->bd_num * sizeof(struct tsi721_dma_desc),
1501 mdma->bd_base, mdma->bd_phys);
1502 mdma->bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001503
1504 /* Free space allocated for status FIFO */
1505 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001506 mdma->sts_size * sizeof(struct tsi721_dma_sts),
1507 mdma->sts_base, mdma->sts_phys);
1508 mdma->sts_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001509 return 0;
1510}
1511
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001512/* Enable Inbound Messaging Interrupts */
1513static void
1514tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
1515 u32 inte_mask)
1516{
1517 u32 rval;
1518
1519 if (!inte_mask)
1520 return;
1521
1522 /* Clear pending Inbound Messaging interrupts */
1523 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1524
1525 /* Enable Inbound Messaging interrupts */
1526 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1527 iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
1528
1529 if (priv->flags & TSI721_USING_MSIX)
1530 return; /* Finished if we are in MSI-X mode */
1531
1532 /*
1533 * For MSI and INTA interrupt signalling we need to enable next levels
1534 */
1535
1536 /* Enable Device Channel Interrupt */
1537 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1538 iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
1539 priv->regs + TSI721_DEV_CHAN_INTE);
1540}
1541
1542/* Disable Inbound Messaging Interrupts */
1543static void
1544tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
1545 u32 inte_mask)
1546{
1547 u32 rval;
1548
1549 if (!inte_mask)
1550 return;
1551
1552 /* Clear pending Inbound Messaging interrupts */
1553 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1554
1555 /* Disable Inbound Messaging interrupts */
1556 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1557 rval &= ~inte_mask;
1558 iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
1559
1560 if (priv->flags & TSI721_USING_MSIX)
1561 return; /* Finished if we are in MSI-X mode */
1562
1563 /*
1564 * For MSI and INTA interrupt signalling we need to disable next levels
1565 */
1566
1567 /* Disable Device Channel Interrupt */
1568 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1569 rval &= ~TSI721_INT_IMSG_CHAN(ch);
1570 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1571}
1572
1573/* Enable Outbound Messaging interrupts */
1574static void
1575tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
1576 u32 inte_mask)
1577{
1578 u32 rval;
1579
1580 if (!inte_mask)
1581 return;
1582
1583 /* Clear pending Outbound Messaging interrupts */
1584 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1585
1586 /* Enable Outbound Messaging channel interrupts */
1587 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1588 iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
1589
1590 if (priv->flags & TSI721_USING_MSIX)
1591 return; /* Finished if we are in MSI-X mode */
1592
1593 /*
1594 * For MSI and INTA interrupt signalling we need to enable next levels
1595 */
1596
1597 /* Enable Device Channel Interrupt */
1598 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1599 iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
1600 priv->regs + TSI721_DEV_CHAN_INTE);
1601}
1602
1603/* Disable Outbound Messaging interrupts */
1604static void
1605tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
1606 u32 inte_mask)
1607{
1608 u32 rval;
1609
1610 if (!inte_mask)
1611 return;
1612
1613 /* Clear pending Outbound Messaging interrupts */
1614 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1615
1616 /* Disable Outbound Messaging interrupts */
1617 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1618 rval &= ~inte_mask;
1619 iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
1620
1621 if (priv->flags & TSI721_USING_MSIX)
1622 return; /* Finished if we are in MSI-X mode */
1623
1624 /*
1625 * For MSI and INTA interrupt signalling we need to disable next levels
1626 */
1627
1628 /* Disable Device Channel Interrupt */
1629 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1630 rval &= ~TSI721_INT_OMSG_CHAN(ch);
1631 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1632}
1633
1634/**
1635 * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
1636 * @mport: Master port with outbound message queue
1637 * @rdev: Target of outbound message
1638 * @mbox: Outbound mailbox
1639 * @buffer: Message to add to outbound queue
1640 * @len: Length of message
1641 */
1642static int
1643tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
1644 void *buffer, size_t len)
1645{
1646 struct tsi721_device *priv = mport->priv;
1647 struct tsi721_omsg_desc *desc;
1648 u32 tx_slot;
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001649 unsigned long flags;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001650
1651 if (!priv->omsg_init[mbox] ||
1652 len > TSI721_MSG_MAX_SIZE || len < 8)
1653 return -EINVAL;
1654
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001655 spin_lock_irqsave(&priv->omsg_ring[mbox].lock, flags);
1656
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001657 tx_slot = priv->omsg_ring[mbox].tx_slot;
1658
1659 /* Copy copy message into transfer buffer */
1660 memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
1661
1662 if (len & 0x7)
1663 len += 8;
1664
1665 /* Build descriptor associated with buffer */
1666 desc = priv->omsg_ring[mbox].omd_base;
1667 desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001668#ifdef TSI721_OMSG_DESC_INT
1669 /* Request IOF_DONE interrupt generation for each N-th frame in queue */
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001670 if (tx_slot % 4 == 0)
1671 desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001672#endif
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001673 desc[tx_slot].msg_info =
1674 cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
1675 (0xe << 12) | (len & 0xff8));
1676 desc[tx_slot].bufptr_lo =
1677 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
1678 0xffffffff);
1679 desc[tx_slot].bufptr_hi =
1680 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
1681
1682 priv->omsg_ring[mbox].wr_count++;
1683
1684 /* Go to next descriptor */
1685 if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
1686 priv->omsg_ring[mbox].tx_slot = 0;
1687 /* Move through the ring link descriptor at the end */
1688 priv->omsg_ring[mbox].wr_count++;
1689 }
1690
1691 mb();
1692
1693 /* Set new write count value */
1694 iowrite32(priv->omsg_ring[mbox].wr_count,
1695 priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1696 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1697
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001698 spin_unlock_irqrestore(&priv->omsg_ring[mbox].lock, flags);
1699
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001700 return 0;
1701}
1702
1703/**
1704 * tsi721_omsg_handler - Outbound Message Interrupt Handler
1705 * @priv: pointer to tsi721 private data
1706 * @ch: number of OB MSG channel to service
1707 *
1708 * Services channel interrupts from outbound messaging engine.
1709 */
1710static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
1711{
1712 u32 omsg_int;
Alexandre Bounine748353c2016-03-22 14:26:23 -07001713 struct rio_mport *mport = &priv->mport;
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001714 void *dev_id = NULL;
1715 u32 tx_slot = 0xffffffff;
1716 int do_callback = 0;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001717
1718 spin_lock(&priv->omsg_ring[ch].lock);
1719
1720 omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
1721
1722 if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
1723 dev_info(&priv->pdev->dev,
1724 "OB MBOX%d: Status FIFO is full\n", ch);
1725
1726 if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
1727 u32 srd_ptr;
1728 u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
1729 int i, j;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001730
1731 /*
1732 * Find last successfully processed descriptor
1733 */
1734
1735 /* Check and clear descriptor status FIFO entries */
1736 srd_ptr = priv->omsg_ring[ch].sts_rdptr;
1737 sts_ptr = priv->omsg_ring[ch].sts_base;
1738 j = srd_ptr * 8;
1739 while (sts_ptr[j]) {
1740 for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
1741 prev_ptr = last_ptr;
1742 last_ptr = le64_to_cpu(sts_ptr[j]);
1743 sts_ptr[j] = 0;
1744 }
1745
1746 ++srd_ptr;
1747 srd_ptr %= priv->omsg_ring[ch].sts_size;
1748 j = srd_ptr * 8;
1749 }
1750
1751 if (last_ptr == 0)
1752 goto no_sts_update;
1753
1754 priv->omsg_ring[ch].sts_rdptr = srd_ptr;
1755 iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
1756
Alexandre Bounine748353c2016-03-22 14:26:23 -07001757 if (!mport->outb_msg[ch].mcback)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001758 goto no_sts_update;
1759
1760 /* Inform upper layer about transfer completion */
1761
1762 tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
1763 sizeof(struct tsi721_omsg_desc);
1764
1765 /*
1766 * Check if this is a Link Descriptor (LD).
1767 * If yes, ignore LD and use descriptor processed
1768 * before LD.
1769 */
1770 if (tx_slot == priv->omsg_ring[ch].size) {
1771 if (prev_ptr)
1772 tx_slot = (prev_ptr -
1773 (u64)priv->omsg_ring[ch].omd_phys)/
1774 sizeof(struct tsi721_omsg_desc);
1775 else
1776 goto no_sts_update;
1777 }
1778
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001779 if (tx_slot >= priv->omsg_ring[ch].size)
1780 dev_dbg(&priv->pdev->dev,
1781 "OB_MSG tx_slot=%x > size=%x",
1782 tx_slot, priv->omsg_ring[ch].size);
1783 WARN_ON(tx_slot >= priv->omsg_ring[ch].size);
1784
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001785 /* Move slot index to the next message to be sent */
1786 ++tx_slot;
1787 if (tx_slot == priv->omsg_ring[ch].size)
1788 tx_slot = 0;
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001789
1790 dev_id = priv->omsg_ring[ch].dev_id;
1791 do_callback = 1;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001792 }
1793
1794no_sts_update:
1795
1796 if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
1797 /*
1798 * Outbound message operation aborted due to error,
1799 * reinitialize OB MSG channel
1800 */
1801
1802 dev_dbg(&priv->pdev->dev, "OB MSG ABORT ch_stat=%x\n",
1803 ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
1804
1805 iowrite32(TSI721_OBDMAC_INT_ERROR,
1806 priv->regs + TSI721_OBDMAC_INT(ch));
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001807 iowrite32(TSI721_OBDMAC_CTL_RETRY_THR | TSI721_OBDMAC_CTL_INIT,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001808 priv->regs + TSI721_OBDMAC_CTL(ch));
1809 ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
1810
1811 /* Inform upper level to clear all pending tx slots */
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001812 dev_id = priv->omsg_ring[ch].dev_id;
1813 tx_slot = priv->omsg_ring[ch].tx_slot;
1814 do_callback = 1;
1815
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001816 /* Synch tx_slot tracking */
1817 iowrite32(priv->omsg_ring[ch].tx_slot,
1818 priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1819 ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1820 priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
1821 priv->omsg_ring[ch].sts_rdptr = 0;
1822 }
1823
1824 /* Clear channel interrupts */
1825 iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
1826
1827 if (!(priv->flags & TSI721_USING_MSIX)) {
1828 u32 ch_inte;
1829
1830 /* Re-enable channel interrupts */
1831 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1832 ch_inte |= TSI721_INT_OMSG_CHAN(ch);
1833 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1834 }
1835
1836 spin_unlock(&priv->omsg_ring[ch].lock);
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001837
1838 if (mport->outb_msg[ch].mcback && do_callback)
1839 mport->outb_msg[ch].mcback(mport, dev_id, ch, tx_slot);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001840}
1841
1842/**
1843 * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
1844 * @mport: Master port implementing Outbound Messaging Engine
1845 * @dev_id: Device specific pointer to pass on event
1846 * @mbox: Mailbox to open
1847 * @entries: Number of entries in the outbound mailbox ring
1848 */
1849static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
1850 int mbox, int entries)
1851{
1852 struct tsi721_device *priv = mport->priv;
1853 struct tsi721_omsg_desc *bd_ptr;
1854 int i, rc = 0;
1855
1856 if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
1857 (entries > (TSI721_OMSGD_RING_SIZE)) ||
1858 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1859 rc = -EINVAL;
1860 goto out;
1861 }
1862
1863 priv->omsg_ring[mbox].dev_id = dev_id;
1864 priv->omsg_ring[mbox].size = entries;
1865 priv->omsg_ring[mbox].sts_rdptr = 0;
1866 spin_lock_init(&priv->omsg_ring[mbox].lock);
1867
1868 /* Outbound Msg Buffer allocation based on
1869 the number of maximum descriptor entries */
1870 for (i = 0; i < entries; i++) {
1871 priv->omsg_ring[mbox].omq_base[i] =
1872 dma_alloc_coherent(
1873 &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
1874 &priv->omsg_ring[mbox].omq_phys[i],
1875 GFP_KERNEL);
1876 if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
1877 dev_dbg(&priv->pdev->dev,
1878 "Unable to allocate OB MSG data buffer for"
1879 " MBOX%d\n", mbox);
1880 rc = -ENOMEM;
1881 goto out_buf;
1882 }
1883 }
1884
1885 /* Outbound message descriptor allocation */
1886 priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
1887 &priv->pdev->dev,
1888 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1889 &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
1890 if (priv->omsg_ring[mbox].omd_base == NULL) {
1891 dev_dbg(&priv->pdev->dev,
1892 "Unable to allocate OB MSG descriptor memory "
1893 "for MBOX%d\n", mbox);
1894 rc = -ENOMEM;
1895 goto out_buf;
1896 }
1897
1898 priv->omsg_ring[mbox].tx_slot = 0;
1899
1900 /* Outbound message descriptor status FIFO allocation */
1901 priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001902 priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001903 priv->omsg_ring[mbox].sts_size *
1904 sizeof(struct tsi721_dma_sts),
1905 &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
1906 if (priv->omsg_ring[mbox].sts_base == NULL) {
1907 dev_dbg(&priv->pdev->dev,
1908 "Unable to allocate OB MSG descriptor status FIFO "
1909 "for MBOX%d\n", mbox);
1910 rc = -ENOMEM;
1911 goto out_desc;
1912 }
1913
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001914 /*
1915 * Configure Outbound Messaging Engine
1916 */
1917
1918 /* Setup Outbound Message descriptor pointer */
1919 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
1920 priv->regs + TSI721_OBDMAC_DPTRH(mbox));
1921 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
1922 TSI721_OBDMAC_DPTRL_MASK),
1923 priv->regs + TSI721_OBDMAC_DPTRL(mbox));
1924
1925 /* Setup Outbound Message descriptor status FIFO */
1926 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
1927 priv->regs + TSI721_OBDMAC_DSBH(mbox));
1928 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
1929 TSI721_OBDMAC_DSBL_MASK),
1930 priv->regs + TSI721_OBDMAC_DSBL(mbox));
1931 iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
1932 priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
1933
1934 /* Enable interrupts */
1935
1936#ifdef CONFIG_PCI_MSI
1937 if (priv->flags & TSI721_USING_MSIX) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07001938 int idx = TSI721_VECT_OMB0_DONE + mbox;
1939
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001940 /* Request interrupt service if we are in MSI-X mode */
Alexandre Bounine748353c2016-03-22 14:26:23 -07001941 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1942 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001943
1944 if (rc) {
1945 dev_dbg(&priv->pdev->dev,
1946 "Unable to allocate MSI-X interrupt for "
1947 "OBOX%d-DONE\n", mbox);
1948 goto out_stat;
1949 }
1950
Alexandre Bounine748353c2016-03-22 14:26:23 -07001951 idx = TSI721_VECT_OMB0_INT + mbox;
1952 rc = request_irq(priv->msix[idx].vector, tsi721_omsg_msix, 0,
1953 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001954
1955 if (rc) {
1956 dev_dbg(&priv->pdev->dev,
1957 "Unable to allocate MSI-X interrupt for "
1958 "MBOX%d-INT\n", mbox);
Alexandre Bounine748353c2016-03-22 14:26:23 -07001959 idx = TSI721_VECT_OMB0_DONE + mbox;
1960 free_irq(priv->msix[idx].vector, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001961 goto out_stat;
1962 }
1963 }
1964#endif /* CONFIG_PCI_MSI */
1965
1966 tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1967
1968 /* Initialize Outbound Message descriptors ring */
1969 bd_ptr = priv->omsg_ring[mbox].omd_base;
1970 bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
1971 bd_ptr[entries].msg_info = 0;
1972 bd_ptr[entries].next_lo =
1973 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
1974 TSI721_OBDMAC_DPTRL_MASK);
1975 bd_ptr[entries].next_hi =
1976 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
1977 priv->omsg_ring[mbox].wr_count = 0;
1978 mb();
1979
1980 /* Initialize Outbound Message engine */
Alexandre Bounine2ece1ca2016-03-22 14:26:47 -07001981 iowrite32(TSI721_OBDMAC_CTL_RETRY_THR | TSI721_OBDMAC_CTL_INIT,
1982 priv->regs + TSI721_OBDMAC_CTL(mbox));
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001983 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1984 udelay(10);
1985
1986 priv->omsg_init[mbox] = 1;
1987
1988 return 0;
1989
1990#ifdef CONFIG_PCI_MSI
1991out_stat:
1992 dma_free_coherent(&priv->pdev->dev,
1993 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1994 priv->omsg_ring[mbox].sts_base,
1995 priv->omsg_ring[mbox].sts_phys);
1996
1997 priv->omsg_ring[mbox].sts_base = NULL;
1998#endif /* CONFIG_PCI_MSI */
1999
2000out_desc:
2001 dma_free_coherent(&priv->pdev->dev,
2002 (entries + 1) * sizeof(struct tsi721_omsg_desc),
2003 priv->omsg_ring[mbox].omd_base,
2004 priv->omsg_ring[mbox].omd_phys);
2005
2006 priv->omsg_ring[mbox].omd_base = NULL;
2007
2008out_buf:
2009 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
2010 if (priv->omsg_ring[mbox].omq_base[i]) {
2011 dma_free_coherent(&priv->pdev->dev,
2012 TSI721_MSG_BUFFER_SIZE,
2013 priv->omsg_ring[mbox].omq_base[i],
2014 priv->omsg_ring[mbox].omq_phys[i]);
2015
2016 priv->omsg_ring[mbox].omq_base[i] = NULL;
2017 }
2018 }
2019
2020out:
2021 return rc;
2022}
2023
2024/**
2025 * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
2026 * @mport: Master port implementing the outbound message unit
2027 * @mbox: Mailbox to close
2028 */
2029static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
2030{
2031 struct tsi721_device *priv = mport->priv;
2032 u32 i;
2033
2034 if (!priv->omsg_init[mbox])
2035 return;
2036 priv->omsg_init[mbox] = 0;
2037
2038 /* Disable Interrupts */
2039
2040 tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
2041
2042#ifdef CONFIG_PCI_MSI
2043 if (priv->flags & TSI721_USING_MSIX) {
2044 free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002045 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002046 free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002047 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002048 }
2049#endif /* CONFIG_PCI_MSI */
2050
2051 /* Free OMSG Descriptor Status FIFO */
2052 dma_free_coherent(&priv->pdev->dev,
2053 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
2054 priv->omsg_ring[mbox].sts_base,
2055 priv->omsg_ring[mbox].sts_phys);
2056
2057 priv->omsg_ring[mbox].sts_base = NULL;
2058
2059 /* Free OMSG descriptors */
2060 dma_free_coherent(&priv->pdev->dev,
2061 (priv->omsg_ring[mbox].size + 1) *
2062 sizeof(struct tsi721_omsg_desc),
2063 priv->omsg_ring[mbox].omd_base,
2064 priv->omsg_ring[mbox].omd_phys);
2065
2066 priv->omsg_ring[mbox].omd_base = NULL;
2067
2068 /* Free message buffers */
2069 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
2070 if (priv->omsg_ring[mbox].omq_base[i]) {
2071 dma_free_coherent(&priv->pdev->dev,
2072 TSI721_MSG_BUFFER_SIZE,
2073 priv->omsg_ring[mbox].omq_base[i],
2074 priv->omsg_ring[mbox].omq_phys[i]);
2075
2076 priv->omsg_ring[mbox].omq_base[i] = NULL;
2077 }
2078 }
2079}
2080
2081/**
2082 * tsi721_imsg_handler - Inbound Message Interrupt Handler
2083 * @priv: pointer to tsi721 private data
2084 * @ch: inbound message channel number to service
2085 *
2086 * Services channel interrupts from inbound messaging engine.
2087 */
2088static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
2089{
2090 u32 mbox = ch - 4;
2091 u32 imsg_int;
Alexandre Bounine748353c2016-03-22 14:26:23 -07002092 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002093
2094 spin_lock(&priv->imsg_ring[mbox].lock);
2095
2096 imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
2097
2098 if (imsg_int & TSI721_IBDMAC_INT_SRTO)
2099 dev_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout\n",
2100 mbox);
2101
2102 if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
2103 dev_info(&priv->pdev->dev, "IB MBOX%d PCIe error\n",
2104 mbox);
2105
2106 if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
2107 dev_info(&priv->pdev->dev,
2108 "IB MBOX%d IB free queue low\n", mbox);
2109
2110 /* Clear IB channel interrupts */
2111 iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
2112
2113 /* If an IB Msg is received notify the upper layer */
2114 if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
Alexandre Bounine748353c2016-03-22 14:26:23 -07002115 mport->inb_msg[mbox].mcback)
2116 mport->inb_msg[mbox].mcback(mport,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002117 priv->imsg_ring[mbox].dev_id, mbox, -1);
2118
2119 if (!(priv->flags & TSI721_USING_MSIX)) {
2120 u32 ch_inte;
2121
2122 /* Re-enable channel interrupts */
2123 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
2124 ch_inte |= TSI721_INT_IMSG_CHAN(ch);
2125 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
2126 }
2127
2128 spin_unlock(&priv->imsg_ring[mbox].lock);
2129}
2130
2131/**
2132 * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
2133 * @mport: Master port implementing the Inbound Messaging Engine
2134 * @dev_id: Device specific pointer to pass on event
2135 * @mbox: Mailbox to open
2136 * @entries: Number of entries in the inbound mailbox ring
2137 */
2138static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
2139 int mbox, int entries)
2140{
2141 struct tsi721_device *priv = mport->priv;
2142 int ch = mbox + 4;
2143 int i;
2144 u64 *free_ptr;
2145 int rc = 0;
2146
2147 if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
2148 (entries > TSI721_IMSGD_RING_SIZE) ||
2149 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
2150 rc = -EINVAL;
2151 goto out;
2152 }
2153
2154 /* Initialize IB Messaging Ring */
2155 priv->imsg_ring[mbox].dev_id = dev_id;
2156 priv->imsg_ring[mbox].size = entries;
2157 priv->imsg_ring[mbox].rx_slot = 0;
2158 priv->imsg_ring[mbox].desc_rdptr = 0;
2159 priv->imsg_ring[mbox].fq_wrptr = 0;
2160 for (i = 0; i < priv->imsg_ring[mbox].size; i++)
2161 priv->imsg_ring[mbox].imq_base[i] = NULL;
2162 spin_lock_init(&priv->imsg_ring[mbox].lock);
2163
2164 /* Allocate buffers for incoming messages */
2165 priv->imsg_ring[mbox].buf_base =
2166 dma_alloc_coherent(&priv->pdev->dev,
2167 entries * TSI721_MSG_BUFFER_SIZE,
2168 &priv->imsg_ring[mbox].buf_phys,
2169 GFP_KERNEL);
2170
2171 if (priv->imsg_ring[mbox].buf_base == NULL) {
2172 dev_err(&priv->pdev->dev,
2173 "Failed to allocate buffers for IB MBOX%d\n", mbox);
2174 rc = -ENOMEM;
2175 goto out;
2176 }
2177
2178 /* Allocate memory for circular free list */
2179 priv->imsg_ring[mbox].imfq_base =
2180 dma_alloc_coherent(&priv->pdev->dev,
2181 entries * 8,
2182 &priv->imsg_ring[mbox].imfq_phys,
2183 GFP_KERNEL);
2184
2185 if (priv->imsg_ring[mbox].imfq_base == NULL) {
2186 dev_err(&priv->pdev->dev,
2187 "Failed to allocate free queue for IB MBOX%d\n", mbox);
2188 rc = -ENOMEM;
2189 goto out_buf;
2190 }
2191
2192 /* Allocate memory for Inbound message descriptors */
2193 priv->imsg_ring[mbox].imd_base =
2194 dma_alloc_coherent(&priv->pdev->dev,
2195 entries * sizeof(struct tsi721_imsg_desc),
2196 &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
2197
2198 if (priv->imsg_ring[mbox].imd_base == NULL) {
2199 dev_err(&priv->pdev->dev,
2200 "Failed to allocate descriptor memory for IB MBOX%d\n",
2201 mbox);
2202 rc = -ENOMEM;
2203 goto out_dma;
2204 }
2205
2206 /* Fill free buffer pointer list */
2207 free_ptr = priv->imsg_ring[mbox].imfq_base;
2208 for (i = 0; i < entries; i++)
2209 free_ptr[i] = cpu_to_le64(
2210 (u64)(priv->imsg_ring[mbox].buf_phys) +
2211 i * 0x1000);
2212
2213 mb();
2214
2215 /*
2216 * For mapping of inbound SRIO Messages into appropriate queues we need
2217 * to set Inbound Device ID register in the messaging engine. We do it
2218 * once when first inbound mailbox is requested.
2219 */
2220 if (!(priv->flags & TSI721_IMSGID_SET)) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07002221 iowrite32((u32)priv->mport.host_deviceid,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002222 priv->regs + TSI721_IB_DEVID);
2223 priv->flags |= TSI721_IMSGID_SET;
2224 }
2225
2226 /*
2227 * Configure Inbound Messaging channel (ch = mbox + 4)
2228 */
2229
2230 /* Setup Inbound Message free queue */
2231 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
2232 priv->regs + TSI721_IBDMAC_FQBH(ch));
2233 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
2234 TSI721_IBDMAC_FQBL_MASK),
2235 priv->regs+TSI721_IBDMAC_FQBL(ch));
2236 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2237 priv->regs + TSI721_IBDMAC_FQSZ(ch));
2238
2239 /* Setup Inbound Message descriptor queue */
2240 iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
2241 priv->regs + TSI721_IBDMAC_DQBH(ch));
2242 iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
2243 (u32)TSI721_IBDMAC_DQBL_MASK),
2244 priv->regs+TSI721_IBDMAC_DQBL(ch));
2245 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
2246 priv->regs + TSI721_IBDMAC_DQSZ(ch));
2247
2248 /* Enable interrupts */
2249
2250#ifdef CONFIG_PCI_MSI
2251 if (priv->flags & TSI721_USING_MSIX) {
Alexandre Bounine748353c2016-03-22 14:26:23 -07002252 int idx = TSI721_VECT_IMB0_RCV + mbox;
2253
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002254 /* Request interrupt service if we are in MSI-X mode */
Alexandre Bounine748353c2016-03-22 14:26:23 -07002255 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2256 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002257
2258 if (rc) {
2259 dev_dbg(&priv->pdev->dev,
2260 "Unable to allocate MSI-X interrupt for "
2261 "IBOX%d-DONE\n", mbox);
2262 goto out_desc;
2263 }
2264
Alexandre Bounine748353c2016-03-22 14:26:23 -07002265 idx = TSI721_VECT_IMB0_INT + mbox;
2266 rc = request_irq(priv->msix[idx].vector, tsi721_imsg_msix, 0,
2267 priv->msix[idx].irq_name, (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002268
2269 if (rc) {
2270 dev_dbg(&priv->pdev->dev,
2271 "Unable to allocate MSI-X interrupt for "
2272 "IBOX%d-INT\n", mbox);
2273 free_irq(
2274 priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002275 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002276 goto out_desc;
2277 }
2278 }
2279#endif /* CONFIG_PCI_MSI */
2280
2281 tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
2282
2283 /* Initialize Inbound Message Engine */
2284 iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
2285 ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
2286 udelay(10);
2287 priv->imsg_ring[mbox].fq_wrptr = entries - 1;
2288 iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
2289
2290 priv->imsg_init[mbox] = 1;
2291 return 0;
2292
2293#ifdef CONFIG_PCI_MSI
2294out_desc:
2295 dma_free_coherent(&priv->pdev->dev,
2296 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2297 priv->imsg_ring[mbox].imd_base,
2298 priv->imsg_ring[mbox].imd_phys);
2299
2300 priv->imsg_ring[mbox].imd_base = NULL;
2301#endif /* CONFIG_PCI_MSI */
2302
2303out_dma:
2304 dma_free_coherent(&priv->pdev->dev,
2305 priv->imsg_ring[mbox].size * 8,
2306 priv->imsg_ring[mbox].imfq_base,
2307 priv->imsg_ring[mbox].imfq_phys);
2308
2309 priv->imsg_ring[mbox].imfq_base = NULL;
2310
2311out_buf:
2312 dma_free_coherent(&priv->pdev->dev,
2313 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2314 priv->imsg_ring[mbox].buf_base,
2315 priv->imsg_ring[mbox].buf_phys);
2316
2317 priv->imsg_ring[mbox].buf_base = NULL;
2318
2319out:
2320 return rc;
2321}
2322
2323/**
2324 * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
2325 * @mport: Master port implementing the Inbound Messaging Engine
2326 * @mbox: Mailbox to close
2327 */
2328static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
2329{
2330 struct tsi721_device *priv = mport->priv;
2331 u32 rx_slot;
2332 int ch = mbox + 4;
2333
2334 if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
2335 return;
2336 priv->imsg_init[mbox] = 0;
2337
2338 /* Disable Inbound Messaging Engine */
2339
2340 /* Disable Interrupts */
2341 tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
2342
2343#ifdef CONFIG_PCI_MSI
2344 if (priv->flags & TSI721_USING_MSIX) {
2345 free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002346 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002347 free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002348 (void *)priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002349 }
2350#endif /* CONFIG_PCI_MSI */
2351
2352 /* Clear Inbound Buffer Queue */
2353 for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
2354 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2355
2356 /* Free memory allocated for message buffers */
2357 dma_free_coherent(&priv->pdev->dev,
2358 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2359 priv->imsg_ring[mbox].buf_base,
2360 priv->imsg_ring[mbox].buf_phys);
2361
2362 priv->imsg_ring[mbox].buf_base = NULL;
2363
2364 /* Free memory allocated for free pointr list */
2365 dma_free_coherent(&priv->pdev->dev,
2366 priv->imsg_ring[mbox].size * 8,
2367 priv->imsg_ring[mbox].imfq_base,
2368 priv->imsg_ring[mbox].imfq_phys);
2369
2370 priv->imsg_ring[mbox].imfq_base = NULL;
2371
2372 /* Free memory allocated for RX descriptors */
2373 dma_free_coherent(&priv->pdev->dev,
2374 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2375 priv->imsg_ring[mbox].imd_base,
2376 priv->imsg_ring[mbox].imd_phys);
2377
2378 priv->imsg_ring[mbox].imd_base = NULL;
2379}
2380
2381/**
2382 * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
2383 * @mport: Master port implementing the Inbound Messaging Engine
2384 * @mbox: Inbound mailbox number
2385 * @buf: Buffer to add to inbound queue
2386 */
2387static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
2388{
2389 struct tsi721_device *priv = mport->priv;
2390 u32 rx_slot;
2391 int rc = 0;
2392
2393 rx_slot = priv->imsg_ring[mbox].rx_slot;
2394 if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
2395 dev_err(&priv->pdev->dev,
2396 "Error adding inbound buffer %d, buffer exists\n",
2397 rx_slot);
2398 rc = -EINVAL;
2399 goto out;
2400 }
2401
2402 priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
2403
2404 if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
2405 priv->imsg_ring[mbox].rx_slot = 0;
2406
2407out:
2408 return rc;
2409}
2410
2411/**
2412 * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
2413 * @mport: Master port implementing the Inbound Messaging Engine
2414 * @mbox: Inbound mailbox number
2415 *
2416 * Returns pointer to the message on success or NULL on failure.
2417 */
2418static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
2419{
2420 struct tsi721_device *priv = mport->priv;
2421 struct tsi721_imsg_desc *desc;
2422 u32 rx_slot;
2423 void *rx_virt = NULL;
2424 u64 rx_phys;
2425 void *buf = NULL;
2426 u64 *free_ptr;
2427 int ch = mbox + 4;
2428 int msg_size;
2429
2430 if (!priv->imsg_init[mbox])
2431 return NULL;
2432
2433 desc = priv->imsg_ring[mbox].imd_base;
2434 desc += priv->imsg_ring[mbox].desc_rdptr;
2435
2436 if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
2437 goto out;
2438
2439 rx_slot = priv->imsg_ring[mbox].rx_slot;
2440 while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
2441 if (++rx_slot == priv->imsg_ring[mbox].size)
2442 rx_slot = 0;
2443 }
2444
2445 rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
2446 le32_to_cpu(desc->bufptr_lo);
2447
2448 rx_virt = priv->imsg_ring[mbox].buf_base +
2449 (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
2450
2451 buf = priv->imsg_ring[mbox].imq_base[rx_slot];
2452 msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
2453 if (msg_size == 0)
2454 msg_size = RIO_MAX_MSG_SIZE;
2455
2456 memcpy(buf, rx_virt, msg_size);
2457 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2458
2459 desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
2460 if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
2461 priv->imsg_ring[mbox].desc_rdptr = 0;
2462
2463 iowrite32(priv->imsg_ring[mbox].desc_rdptr,
2464 priv->regs + TSI721_IBDMAC_DQRP(ch));
2465
2466 /* Return free buffer into the pointer list */
2467 free_ptr = priv->imsg_ring[mbox].imfq_base;
2468 free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
2469
2470 if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
2471 priv->imsg_ring[mbox].fq_wrptr = 0;
2472
2473 iowrite32(priv->imsg_ring[mbox].fq_wrptr,
2474 priv->regs + TSI721_IBDMAC_FQWP(ch));
2475out:
2476 return buf;
2477}
2478
2479/**
2480 * tsi721_messages_init - Initialization of Messaging Engine
2481 * @priv: pointer to tsi721 private data
2482 *
2483 * Configures Tsi721 messaging engine.
2484 */
2485static int tsi721_messages_init(struct tsi721_device *priv)
2486{
2487 int ch;
2488
2489 iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
2490 iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
2491 iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
2492
2493 /* Set SRIO Message Request/Response Timeout */
2494 iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
2495
2496 /* Initialize Inbound Messaging Engine Registers */
2497 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
2498 /* Clear interrupt bits */
2499 iowrite32(TSI721_IBDMAC_INT_MASK,
2500 priv->regs + TSI721_IBDMAC_INT(ch));
2501 /* Clear Status */
2502 iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
2503
2504 iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
2505 priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
2506 iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
2507 priv->regs + TSI721_SMSG_ECC_NCOR(ch));
2508 }
2509
2510 return 0;
2511}
2512
2513/**
Alexandre Bouninedbe74af2016-03-22 14:26:02 -07002514 * tsi721_query_mport - Fetch inbound message from the Tsi721 MSG Queue
2515 * @mport: Master port implementing the Inbound Messaging Engine
2516 * @mbox: Inbound mailbox number
2517 *
2518 * Returns pointer to the message on success or NULL on failure.
2519 */
2520static int tsi721_query_mport(struct rio_mport *mport,
2521 struct rio_mport_attr *attr)
2522{
2523 struct tsi721_device *priv = mport->priv;
2524 u32 rval;
2525
2526 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_ERR_STS_CSR(0)));
2527 if (rval & RIO_PORT_N_ERR_STS_PORT_OK) {
2528 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL2_CSR(0)));
2529 attr->link_speed = (rval & RIO_PORT_N_CTL2_SEL_BAUD) >> 28;
2530 rval = ioread32(priv->regs + (0x100 + RIO_PORT_N_CTL_CSR(0)));
2531 attr->link_width = (rval & RIO_PORT_N_CTL_IPW) >> 27;
2532 } else
2533 attr->link_speed = RIO_LINK_DOWN;
2534
2535#ifdef CONFIG_RAPIDIO_DMA_ENGINE
2536 attr->flags = RIO_MPORT_DMA | RIO_MPORT_DMA_SG;
2537 attr->dma_max_sge = 0;
2538 attr->dma_max_size = TSI721_BDMA_MAX_BCOUNT;
2539 attr->dma_align = 0;
2540#else
2541 attr->flags = 0;
2542#endif
2543 return 0;
2544}
2545
2546/**
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002547 * tsi721_disable_ints - disables all device interrupts
2548 * @priv: pointer to tsi721 private data
2549 */
2550static void tsi721_disable_ints(struct tsi721_device *priv)
2551{
2552 int ch;
2553
2554 /* Disable all device level interrupts */
2555 iowrite32(0, priv->regs + TSI721_DEV_INTE);
2556
2557 /* Disable all Device Channel interrupts */
2558 iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
2559
2560 /* Disable all Inbound Msg Channel interrupts */
2561 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
2562 iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
2563
2564 /* Disable all Outbound Msg Channel interrupts */
2565 for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
2566 iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
2567
2568 /* Disable all general messaging interrupts */
2569 iowrite32(0, priv->regs + TSI721_SMSG_INTE);
2570
2571 /* Disable all BDMA Channel interrupts */
2572 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002573 iowrite32(0,
2574 priv->regs + TSI721_DMAC_BASE(ch) + TSI721_DMAC_INTE);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002575
2576 /* Disable all general BDMA interrupts */
2577 iowrite32(0, priv->regs + TSI721_BDMA_INTE);
2578
2579 /* Disable all SRIO Channel interrupts */
2580 for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
2581 iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
2582
2583 /* Disable all general SR2PC interrupts */
2584 iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
2585
2586 /* Disable all PC2SR interrupts */
2587 iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
2588
2589 /* Disable all I2C interrupts */
2590 iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
2591
2592 /* Disable SRIO MAC interrupts */
2593 iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
2594 iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
2595}
2596
Alexandre Bounine748353c2016-03-22 14:26:23 -07002597static struct rio_ops tsi721_rio_ops = {
2598 .lcread = tsi721_lcread,
2599 .lcwrite = tsi721_lcwrite,
2600 .cread = tsi721_cread_dma,
2601 .cwrite = tsi721_cwrite_dma,
2602 .dsend = tsi721_dsend,
2603 .open_inb_mbox = tsi721_open_inb_mbox,
2604 .close_inb_mbox = tsi721_close_inb_mbox,
2605 .open_outb_mbox = tsi721_open_outb_mbox,
2606 .close_outb_mbox = tsi721_close_outb_mbox,
2607 .add_outb_message = tsi721_add_outb_message,
2608 .add_inb_buffer = tsi721_add_inb_buffer,
2609 .get_inb_message = tsi721_get_inb_message,
2610 .map_inb = tsi721_rio_map_inb_mem,
2611 .unmap_inb = tsi721_rio_unmap_inb_mem,
2612 .pwenable = tsi721_pw_enable,
2613 .query_mport = tsi721_query_mport,
Alexandre Bounine1679e8d2016-03-22 14:26:53 -07002614 .map_outb = tsi721_map_outb_win,
2615 .unmap_outb = tsi721_unmap_outb_win,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002616};
2617
2618static void tsi721_mport_release(struct device *dev)
2619{
2620 struct rio_mport *mport = to_rio_mport(dev);
2621
2622 dev_dbg(dev, "RIO: %s %s id=%d\n", __func__, mport->name, mport->id);
2623}
2624
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002625/**
2626 * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
2627 * @priv: pointer to tsi721 private data
2628 *
2629 * Configures Tsi721 as RapidIO master port.
2630 */
Bill Pemberton305c8912012-11-19 13:23:25 -05002631static int tsi721_setup_mport(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002632{
2633 struct pci_dev *pdev = priv->pdev;
2634 int err = 0;
Alexandre Bounine748353c2016-03-22 14:26:23 -07002635 struct rio_mport *mport = &priv->mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002636
Alexandre Bounine748353c2016-03-22 14:26:23 -07002637 err = rio_mport_initialize(mport);
2638 if (err)
2639 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002640
Alexandre Bounine748353c2016-03-22 14:26:23 -07002641 mport->ops = &tsi721_rio_ops;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002642 mport->index = 0;
2643 mport->sys_size = 0; /* small system */
2644 mport->phy_type = RIO_PHY_SERIAL;
2645 mport->priv = (void *)priv;
2646 mport->phys_efptr = 0x100;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002647 mport->dev.parent = &pdev->dev;
Alexandre Bounine748353c2016-03-22 14:26:23 -07002648 mport->dev.release = tsi721_mport_release;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002649
2650 INIT_LIST_HEAD(&mport->dbells);
2651
2652 rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
Alexandre Bounineb439e662011-12-08 14:34:36 -08002653 rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
2654 rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
Alexandre Bounineed43f442012-10-04 17:15:51 -07002655 snprintf(mport->name, RIO_MAX_MPORT_NAME, "%s(%s)",
2656 dev_driver_string(&pdev->dev), dev_name(&pdev->dev));
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002657
2658 /* Hook up interrupt handler */
2659
2660#ifdef CONFIG_PCI_MSI
2661 if (!tsi721_enable_msix(priv))
2662 priv->flags |= TSI721_USING_MSIX;
2663 else if (!pci_enable_msi(pdev))
2664 priv->flags |= TSI721_USING_MSI;
2665 else
2666 dev_info(&pdev->dev,
2667 "MSI/MSI-X is not available. Using legacy INTx.\n");
2668#endif /* CONFIG_PCI_MSI */
2669
Alexandre Bounine748353c2016-03-22 14:26:23 -07002670 err = tsi721_request_irq(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002671
Alexandre Bounine748353c2016-03-22 14:26:23 -07002672 if (err) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002673 dev_err(&pdev->dev, "Unable to get assigned PCI IRQ "
2674 "vector %02X err=0x%x\n", pdev->irq, err);
Alexandre Bounine748353c2016-03-22 14:26:23 -07002675 return err;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002676 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002677
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002678#ifdef CONFIG_RAPIDIO_DMA_ENGINE
Alexandre Bounine748353c2016-03-22 14:26:23 -07002679 err = tsi721_register_dma(priv);
2680 if (err)
2681 goto err_exit;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002682#endif
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002683 /* Enable SRIO link */
2684 iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
2685 TSI721_DEVCTL_SRBOOT_CMPL,
2686 priv->regs + TSI721_DEVCTL);
2687
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002688 if (mport->host_deviceid >= 0)
2689 iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
2690 RIO_PORT_GEN_DISCOVERED,
2691 priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2692 else
2693 iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2694
Alexandre Bounine748353c2016-03-22 14:26:23 -07002695 err = rio_register_mport(mport);
2696 if (err) {
2697 tsi721_unregister_dma(priv);
2698 goto err_exit;
2699 }
2700
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002701 return 0;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002702
2703err_exit:
Alexandre Bounine748353c2016-03-22 14:26:23 -07002704 tsi721_free_irq(priv);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002705 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002706}
2707
Bill Pemberton305c8912012-11-19 13:23:25 -05002708static int tsi721_probe(struct pci_dev *pdev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002709 const struct pci_device_id *id)
2710{
2711 struct tsi721_device *priv;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002712 int err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002713
2714 priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
2715 if (priv == NULL) {
2716 dev_err(&pdev->dev, "Failed to allocate memory for device\n");
2717 err = -ENOMEM;
2718 goto err_exit;
2719 }
2720
2721 err = pci_enable_device(pdev);
2722 if (err) {
2723 dev_err(&pdev->dev, "Failed to enable PCI device\n");
2724 goto err_clean;
2725 }
2726
2727 priv->pdev = pdev;
2728
2729#ifdef DEBUG
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002730 {
2731 int i;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002732 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
2733 dev_dbg(&pdev->dev, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
2734 i, (unsigned long long)pci_resource_start(pdev, i),
2735 (unsigned long)pci_resource_len(pdev, i),
2736 pci_resource_flags(pdev, i));
2737 }
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002738 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002739#endif
2740 /*
2741 * Verify BAR configuration
2742 */
2743
2744 /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
2745 if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
2746 pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
2747 pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
2748 dev_err(&pdev->dev,
2749 "Missing or misconfigured CSR BAR0, aborting.\n");
2750 err = -ENODEV;
2751 goto err_disable_pdev;
2752 }
2753
2754 /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
2755 if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
2756 pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
2757 pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
2758 dev_err(&pdev->dev,
2759 "Missing or misconfigured Doorbell BAR1, aborting.\n");
2760 err = -ENODEV;
2761 goto err_disable_pdev;
2762 }
2763
2764 /*
2765 * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
2766 * space.
2767 * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
2768 * It may be a good idea to keep them disabled using HW configuration
2769 * to save PCI memory space.
2770 */
Alexandre Bounine1679e8d2016-03-22 14:26:53 -07002771
2772 priv->p2r_bar[0].size = priv->p2r_bar[1].size = 0;
2773
2774 if (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64) {
2775 if (pci_resource_flags(pdev, BAR_2) & IORESOURCE_PREFETCH)
2776 dev_info(&pdev->dev,
2777 "Prefetchable OBW BAR2 will not be used\n");
2778 else {
2779 priv->p2r_bar[0].base = pci_resource_start(pdev, BAR_2);
2780 priv->p2r_bar[0].size = pci_resource_len(pdev, BAR_2);
2781 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002782 }
2783
Alexandre Bounine1679e8d2016-03-22 14:26:53 -07002784 if (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64) {
2785 if (pci_resource_flags(pdev, BAR_4) & IORESOURCE_PREFETCH)
2786 dev_info(&pdev->dev,
2787 "Prefetchable OBW BAR4 will not be used\n");
2788 else {
2789 priv->p2r_bar[1].base = pci_resource_start(pdev, BAR_4);
2790 priv->p2r_bar[1].size = pci_resource_len(pdev, BAR_4);
2791 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002792 }
2793
2794 err = pci_request_regions(pdev, DRV_NAME);
2795 if (err) {
2796 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
2797 "aborting.\n");
2798 goto err_disable_pdev;
2799 }
2800
2801 pci_set_master(pdev);
2802
2803 priv->regs = pci_ioremap_bar(pdev, BAR_0);
2804 if (!priv->regs) {
2805 dev_err(&pdev->dev,
2806 "Unable to map device registers space, aborting\n");
2807 err = -ENOMEM;
2808 goto err_free_res;
2809 }
2810
2811 priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
2812 if (!priv->odb_base) {
2813 dev_err(&pdev->dev,
2814 "Unable to map outbound doorbells space, aborting\n");
2815 err = -ENOMEM;
2816 goto err_unmap_bars;
2817 }
2818
2819 /* Configure DMA attributes. */
2820 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Peter Senna Tschudin18f62872012-10-04 17:15:55 -07002821 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2822 if (err) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002823 dev_info(&pdev->dev, "Unable to set DMA mask\n");
2824 goto err_unmap_bars;
2825 }
2826
2827 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2828 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2829 } else {
2830 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2831 if (err)
2832 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2833 }
2834
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002835 BUG_ON(!pci_is_pcie(pdev));
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002836
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002837 /* Clear "no snoop" and "relaxed ordering" bits. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002838 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002839 PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN, 0);
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002840
2841 /* Adjust PCIe completion timeout. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002842 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL2, 0xf, 0x2);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002843
2844 /*
2845 * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
2846 */
2847 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
2848 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
2849 TSI721_MSIXTBL_OFFSET);
2850 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
2851 TSI721_MSIXPBA_OFFSET);
2852 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
2853 /* End of FIXUP */
2854
2855 tsi721_disable_ints(priv);
2856
2857 tsi721_init_pc2sr_mapping(priv);
2858 tsi721_init_sr2pc_mapping(priv);
2859
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002860 if (tsi721_bdma_maint_init(priv)) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002861 dev_err(&pdev->dev, "BDMA initialization failed, aborting\n");
2862 err = -ENOMEM;
2863 goto err_unmap_bars;
2864 }
2865
2866 err = tsi721_doorbell_init(priv);
2867 if (err)
2868 goto err_free_bdma;
2869
2870 tsi721_port_write_init(priv);
2871
2872 err = tsi721_messages_init(priv);
2873 if (err)
2874 goto err_free_consistent;
2875
2876 err = tsi721_setup_mport(priv);
2877 if (err)
2878 goto err_free_consistent;
2879
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002880 pci_set_drvdata(pdev, priv);
Alexandre Bounine748353c2016-03-22 14:26:23 -07002881 tsi721_interrupts_init(priv);
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002882
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002883 return 0;
2884
2885err_free_consistent:
Alexandre Bounine748353c2016-03-22 14:26:23 -07002886 tsi721_port_write_free(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002887 tsi721_doorbell_free(priv);
2888err_free_bdma:
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002889 tsi721_bdma_maint_free(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002890err_unmap_bars:
2891 if (priv->regs)
2892 iounmap(priv->regs);
2893 if (priv->odb_base)
2894 iounmap(priv->odb_base);
2895err_free_res:
2896 pci_release_regions(pdev);
2897 pci_clear_master(pdev);
2898err_disable_pdev:
2899 pci_disable_device(pdev);
2900err_clean:
2901 kfree(priv);
2902err_exit:
2903 return err;
2904}
2905
Alexandre Bounine748353c2016-03-22 14:26:23 -07002906static void tsi721_remove(struct pci_dev *pdev)
2907{
2908 struct tsi721_device *priv = pci_get_drvdata(pdev);
2909
2910 dev_dbg(&pdev->dev, "%s enter\n", __func__);
2911
2912 tsi721_disable_ints(priv);
2913 tsi721_free_irq(priv);
Alexandre Bounine9a0b0622016-03-22 14:26:44 -07002914 flush_scheduled_work();
Alexandre Bounine748353c2016-03-22 14:26:23 -07002915 rio_unregister_mport(&priv->mport);
2916
2917 tsi721_unregister_dma(priv);
2918 tsi721_bdma_maint_free(priv);
2919 tsi721_doorbell_free(priv);
2920 tsi721_port_write_free(priv);
2921 tsi721_close_sr2pc_mapping(priv);
2922
2923 if (priv->regs)
2924 iounmap(priv->regs);
2925 if (priv->odb_base)
2926 iounmap(priv->odb_base);
2927#ifdef CONFIG_PCI_MSI
2928 if (priv->flags & TSI721_USING_MSIX)
2929 pci_disable_msix(priv->pdev);
2930 else if (priv->flags & TSI721_USING_MSI)
2931 pci_disable_msi(priv->pdev);
2932#endif
2933 pci_release_regions(pdev);
2934 pci_clear_master(pdev);
2935 pci_disable_device(pdev);
2936 pci_set_drvdata(pdev, NULL);
2937 kfree(priv);
2938 dev_dbg(&pdev->dev, "%s exit\n", __func__);
2939}
2940
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002941static void tsi721_shutdown(struct pci_dev *pdev)
2942{
2943 struct tsi721_device *priv = pci_get_drvdata(pdev);
2944
2945 dev_dbg(&pdev->dev, "RIO: %s\n", __func__);
2946
2947 tsi721_disable_ints(priv);
2948 tsi721_dma_stop_all(priv);
2949 pci_clear_master(pdev);
2950 pci_disable_device(pdev);
2951}
2952
Benoit Taine9baa3c32014-08-08 15:56:03 +02002953static const struct pci_device_id tsi721_pci_tbl[] = {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002954 { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
2955 { 0, } /* terminate list */
2956};
2957
2958MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
2959
2960static struct pci_driver tsi721_driver = {
2961 .name = "tsi721",
2962 .id_table = tsi721_pci_tbl,
2963 .probe = tsi721_probe,
Alexandre Bounine748353c2016-03-22 14:26:23 -07002964 .remove = tsi721_remove,
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -07002965 .shutdown = tsi721_shutdown,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002966};
2967
Alexandre Bounine748353c2016-03-22 14:26:23 -07002968module_pci_driver(tsi721_driver);
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002969
2970MODULE_DESCRIPTION("IDT Tsi721 PCIExpress-to-SRIO bridge driver");
2971MODULE_AUTHOR("Integrated Device Technology, Inc.");
2972MODULE_LICENSE("GPL");