blob: b96f0b97dc3ae2777d16f80ad6692a10715d0bf4 [file] [log] [blame]
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001/*
2 * RapidIO mport driver for Tsi721 PCIExpress-to-SRIO bridge
3 *
4 * Copyright 2011 Integrated Device Technology, Inc.
5 * Alexandre Bounine <alexandre.bounine@idt.com>
6 * Chul Kim <chul.kim@idt.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#include <linux/io.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/ioport.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30#include <linux/rio.h>
31#include <linux/rio_drv.h>
32#include <linux/dma-mapping.h>
33#include <linux/interrupt.h>
34#include <linux/kfifo.h>
35#include <linux/delay.h>
36
37#include "tsi721.h"
38
39#define DEBUG_PW /* Inbound Port-Write debugging */
40
41static void tsi721_omsg_handler(struct tsi721_device *priv, int ch);
42static void tsi721_imsg_handler(struct tsi721_device *priv, int ch);
43
44/**
45 * tsi721_lcread - read from local SREP config space
46 * @mport: RapidIO master port info
47 * @index: ID of RapdiIO interface
48 * @offset: Offset into configuration space
49 * @len: Length (in bytes) of the maintenance transaction
50 * @data: Value to be read into
51 *
52 * Generates a local SREP space read. Returns %0 on
53 * success or %-EINVAL on failure.
54 */
55static int tsi721_lcread(struct rio_mport *mport, int index, u32 offset,
56 int len, u32 *data)
57{
58 struct tsi721_device *priv = mport->priv;
59
60 if (len != sizeof(u32))
61 return -EINVAL; /* only 32-bit access is supported */
62
63 *data = ioread32(priv->regs + offset);
64
65 return 0;
66}
67
68/**
69 * tsi721_lcwrite - write into local SREP config space
70 * @mport: RapidIO master port info
71 * @index: ID of RapdiIO interface
72 * @offset: Offset into configuration space
73 * @len: Length (in bytes) of the maintenance transaction
74 * @data: Value to be written
75 *
76 * Generates a local write into SREP configuration space. Returns %0 on
77 * success or %-EINVAL on failure.
78 */
79static int tsi721_lcwrite(struct rio_mport *mport, int index, u32 offset,
80 int len, u32 data)
81{
82 struct tsi721_device *priv = mport->priv;
83
84 if (len != sizeof(u32))
85 return -EINVAL; /* only 32-bit access is supported */
86
87 iowrite32(data, priv->regs + offset);
88
89 return 0;
90}
91
92/**
93 * tsi721_maint_dma - Helper function to generate RapidIO maintenance
94 * transactions using designated Tsi721 DMA channel.
95 * @priv: pointer to tsi721 private data
96 * @sys_size: RapdiIO transport system size
97 * @destid: Destination ID of transaction
98 * @hopcount: Number of hops to target device
99 * @offset: Offset into configuration space
100 * @len: Length (in bytes) of the maintenance transaction
101 * @data: Location to be read from or write into
102 * @do_wr: Operation flag (1 == MAINT_WR)
103 *
104 * Generates a RapidIO maintenance transaction (Read or Write).
105 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
106 */
107static int tsi721_maint_dma(struct tsi721_device *priv, u32 sys_size,
108 u16 destid, u8 hopcount, u32 offset, int len,
109 u32 *data, int do_wr)
110{
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700111 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(priv->mdma.ch_id);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700112 struct tsi721_dma_desc *bd_ptr;
113 u32 rd_count, swr_ptr, ch_stat;
114 int i, err = 0;
115 u32 op = do_wr ? MAINT_WR : MAINT_RD;
116
117 if (offset > (RIO_MAINT_SPACE_SZ - len) || (len != sizeof(u32)))
118 return -EINVAL;
119
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700120 bd_ptr = priv->mdma.bd_base;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700121
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700122 rd_count = ioread32(regs + TSI721_DMAC_DRDCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700123
124 /* Initialize DMA descriptor */
125 bd_ptr[0].type_id = cpu_to_le32((DTYPE2 << 29) | (op << 19) | destid);
126 bd_ptr[0].bcount = cpu_to_le32((sys_size << 26) | 0x04);
127 bd_ptr[0].raddr_lo = cpu_to_le32((hopcount << 24) | offset);
128 bd_ptr[0].raddr_hi = 0;
129 if (do_wr)
130 bd_ptr[0].data[0] = cpu_to_be32p(data);
131 else
132 bd_ptr[0].data[0] = 0xffffffff;
133
134 mb();
135
136 /* Start DMA operation */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700137 iowrite32(rd_count + 2, regs + TSI721_DMAC_DWRCNT);
138 ioread32(regs + TSI721_DMAC_DWRCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700139 i = 0;
140
141 /* Wait until DMA transfer is finished */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700142 while ((ch_stat = ioread32(regs + TSI721_DMAC_STS))
143 & TSI721_DMAC_STS_RUN) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700144 udelay(1);
145 if (++i >= 5000000) {
146 dev_dbg(&priv->pdev->dev,
147 "%s : DMA[%d] read timeout ch_status=%x\n",
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700148 __func__, priv->mdma.ch_id, ch_stat);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700149 if (!do_wr)
150 *data = 0xffffffff;
151 err = -EIO;
152 goto err_out;
153 }
154 }
155
156 if (ch_stat & TSI721_DMAC_STS_ABORT) {
157 /* If DMA operation aborted due to error,
158 * reinitialize DMA channel
159 */
160 dev_dbg(&priv->pdev->dev, "%s : DMA ABORT ch_stat=%x\n",
161 __func__, ch_stat);
162 dev_dbg(&priv->pdev->dev, "OP=%d : destid=%x hc=%x off=%x\n",
163 do_wr ? MAINT_WR : MAINT_RD, destid, hopcount, offset);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700164 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
165 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700166 udelay(10);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700167 iowrite32(0, regs + TSI721_DMAC_DWRCNT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700168 udelay(1);
169 if (!do_wr)
170 *data = 0xffffffff;
171 err = -EIO;
172 goto err_out;
173 }
174
175 if (!do_wr)
176 *data = be32_to_cpu(bd_ptr[0].data[0]);
177
178 /*
179 * Update descriptor status FIFO RD pointer.
180 * NOTE: Skipping check and clear FIFO entries because we are waiting
181 * for transfer to be completed.
182 */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700183 swr_ptr = ioread32(regs + TSI721_DMAC_DSWP);
184 iowrite32(swr_ptr, regs + TSI721_DMAC_DSRP);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700185err_out:
186
187 return err;
188}
189
190/**
191 * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
192 * using Tsi721 BDMA engine.
193 * @mport: RapidIO master port control structure
194 * @index: ID of RapdiIO interface
195 * @destid: Destination ID of transaction
196 * @hopcount: Number of hops to target device
197 * @offset: Offset into configuration space
198 * @len: Length (in bytes) of the maintenance transaction
199 * @val: Location to be read into
200 *
201 * Generates a RapidIO maintenance read transaction.
202 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
203 */
204static int tsi721_cread_dma(struct rio_mport *mport, int index, u16 destid,
205 u8 hopcount, u32 offset, int len, u32 *data)
206{
207 struct tsi721_device *priv = mport->priv;
208
209 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
210 offset, len, data, 0);
211}
212
213/**
214 * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
215 * using Tsi721 BDMA engine
216 * @mport: RapidIO master port control structure
217 * @index: ID of RapdiIO interface
218 * @destid: Destination ID of transaction
219 * @hopcount: Number of hops to target device
220 * @offset: Offset into configuration space
221 * @len: Length (in bytes) of the maintenance transaction
222 * @val: Value to be written
223 *
224 * Generates a RapidIO maintenance write transaction.
225 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
226 */
227static int tsi721_cwrite_dma(struct rio_mport *mport, int index, u16 destid,
228 u8 hopcount, u32 offset, int len, u32 data)
229{
230 struct tsi721_device *priv = mport->priv;
231 u32 temp = data;
232
233 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
234 offset, len, &temp, 1);
235}
236
237/**
238 * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
239 * @mport: RapidIO master port structure
240 *
241 * Handles inbound port-write interrupts. Copies PW message from an internal
242 * buffer into PW message FIFO and schedules deferred routine to process
243 * queued messages.
244 */
245static int
246tsi721_pw_handler(struct rio_mport *mport)
247{
248 struct tsi721_device *priv = mport->priv;
249 u32 pw_stat;
250 u32 pw_buf[TSI721_RIO_PW_MSG_SIZE/sizeof(u32)];
251
252
253 pw_stat = ioread32(priv->regs + TSI721_RIO_PW_RX_STAT);
254
255 if (pw_stat & TSI721_RIO_PW_RX_STAT_PW_VAL) {
256 pw_buf[0] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(0));
257 pw_buf[1] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(1));
258 pw_buf[2] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(2));
259 pw_buf[3] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(3));
260
261 /* Queue PW message (if there is room in FIFO),
262 * otherwise discard it.
263 */
264 spin_lock(&priv->pw_fifo_lock);
265 if (kfifo_avail(&priv->pw_fifo) >= TSI721_RIO_PW_MSG_SIZE)
266 kfifo_in(&priv->pw_fifo, pw_buf,
267 TSI721_RIO_PW_MSG_SIZE);
268 else
269 priv->pw_discard_count++;
270 spin_unlock(&priv->pw_fifo_lock);
271 }
272
273 /* Clear pending PW interrupts */
274 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
275 priv->regs + TSI721_RIO_PW_RX_STAT);
276
277 schedule_work(&priv->pw_work);
278
279 return 0;
280}
281
282static void tsi721_pw_dpc(struct work_struct *work)
283{
284 struct tsi721_device *priv = container_of(work, struct tsi721_device,
285 pw_work);
286 u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)]; /* Use full size PW message
287 buffer for RIO layer */
288
289 /*
290 * Process port-write messages
291 */
292 while (kfifo_out_spinlocked(&priv->pw_fifo, (unsigned char *)msg_buffer,
293 TSI721_RIO_PW_MSG_SIZE, &priv->pw_fifo_lock)) {
294 /* Process one message */
295#ifdef DEBUG_PW
296 {
297 u32 i;
298 pr_debug("%s : Port-Write Message:", __func__);
299 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); ) {
300 pr_debug("0x%02x: %08x %08x %08x %08x", i*4,
301 msg_buffer[i], msg_buffer[i + 1],
302 msg_buffer[i + 2], msg_buffer[i + 3]);
303 i += 4;
304 }
305 pr_debug("\n");
306 }
307#endif
308 /* Pass the port-write message to RIO core for processing */
309 rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
310 }
311}
312
313/**
314 * tsi721_pw_enable - enable/disable port-write interface init
315 * @mport: Master port implementing the port write unit
316 * @enable: 1=enable; 0=disable port-write message handling
317 */
318static int tsi721_pw_enable(struct rio_mport *mport, int enable)
319{
320 struct tsi721_device *priv = mport->priv;
321 u32 rval;
322
323 rval = ioread32(priv->regs + TSI721_RIO_EM_INT_ENABLE);
324
325 if (enable)
326 rval |= TSI721_RIO_EM_INT_ENABLE_PW_RX;
327 else
328 rval &= ~TSI721_RIO_EM_INT_ENABLE_PW_RX;
329
330 /* Clear pending PW interrupts */
331 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
332 priv->regs + TSI721_RIO_PW_RX_STAT);
333 /* Update enable bits */
334 iowrite32(rval, priv->regs + TSI721_RIO_EM_INT_ENABLE);
335
336 return 0;
337}
338
339/**
340 * tsi721_dsend - Send a RapidIO doorbell
341 * @mport: RapidIO master port info
342 * @index: ID of RapidIO interface
343 * @destid: Destination ID of target device
344 * @data: 16-bit info field of RapidIO doorbell
345 *
346 * Sends a RapidIO doorbell message. Always returns %0.
347 */
348static int tsi721_dsend(struct rio_mport *mport, int index,
349 u16 destid, u16 data)
350{
351 struct tsi721_device *priv = mport->priv;
352 u32 offset;
353
354 offset = (((mport->sys_size) ? RIO_TT_CODE_16 : RIO_TT_CODE_8) << 18) |
355 (destid << 2);
356
357 dev_dbg(&priv->pdev->dev,
358 "Send Doorbell 0x%04x to destID 0x%x\n", data, destid);
359 iowrite16be(data, priv->odb_base + offset);
360
361 return 0;
362}
363
364/**
365 * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
366 * @mport: RapidIO master port structure
367 *
368 * Handles inbound doorbell interrupts. Copies doorbell entry from an internal
369 * buffer into DB message FIFO and schedules deferred routine to process
370 * queued DBs.
371 */
372static int
373tsi721_dbell_handler(struct rio_mport *mport)
374{
375 struct tsi721_device *priv = mport->priv;
376 u32 regval;
377
378 /* Disable IDB interrupts */
379 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
380 regval &= ~TSI721_SR_CHINT_IDBQRCV;
381 iowrite32(regval,
382 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
383
384 schedule_work(&priv->idb_work);
385
386 return 0;
387}
388
389static void tsi721_db_dpc(struct work_struct *work)
390{
391 struct tsi721_device *priv = container_of(work, struct tsi721_device,
392 idb_work);
393 struct rio_mport *mport;
394 struct rio_dbell *dbell;
395 int found = 0;
396 u32 wr_ptr, rd_ptr;
397 u64 *idb_entry;
398 u32 regval;
399 union {
400 u64 msg;
401 u8 bytes[8];
402 } idb;
403
404 /*
405 * Process queued inbound doorbells
406 */
407 mport = priv->mport;
408
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800409 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
410 rd_ptr = ioread32(priv->regs + TSI721_IDQ_RP(IDB_QUEUE)) % IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700411
412 while (wr_ptr != rd_ptr) {
413 idb_entry = (u64 *)(priv->idb_base +
414 (TSI721_IDB_ENTRY_SIZE * rd_ptr));
415 rd_ptr++;
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800416 rd_ptr %= IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700417 idb.msg = *idb_entry;
418 *idb_entry = 0;
419
420 /* Process one doorbell */
421 list_for_each_entry(dbell, &mport->dbells, node) {
422 if ((dbell->res->start <= DBELL_INF(idb.bytes)) &&
423 (dbell->res->end >= DBELL_INF(idb.bytes))) {
424 found = 1;
425 break;
426 }
427 }
428
429 if (found) {
430 dbell->dinb(mport, dbell->dev_id, DBELL_SID(idb.bytes),
431 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
432 } else {
433 dev_dbg(&priv->pdev->dev,
434 "spurious inb doorbell, sid %2.2x tid %2.2x"
435 " info %4.4x\n", DBELL_SID(idb.bytes),
436 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
437 }
Alexandre Bounine3670e7e2012-08-21 16:16:11 -0700438
439 wr_ptr = ioread32(priv->regs +
440 TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700441 }
442
443 iowrite32(rd_ptr & (IDB_QSIZE - 1),
444 priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
445
446 /* Re-enable IDB interrupts */
447 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
448 regval |= TSI721_SR_CHINT_IDBQRCV;
449 iowrite32(regval,
450 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
Alexandre Bounine3670e7e2012-08-21 16:16:11 -0700451
452 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
453 if (wr_ptr != rd_ptr)
454 schedule_work(&priv->idb_work);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700455}
456
457/**
458 * tsi721_irqhandler - Tsi721 interrupt handler
459 * @irq: Linux interrupt number
460 * @ptr: Pointer to interrupt-specific data (mport structure)
461 *
462 * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
463 * interrupt events and calls an event-specific handler(s).
464 */
465static irqreturn_t tsi721_irqhandler(int irq, void *ptr)
466{
467 struct rio_mport *mport = (struct rio_mport *)ptr;
468 struct tsi721_device *priv = mport->priv;
469 u32 dev_int;
470 u32 dev_ch_int;
471 u32 intval;
472 u32 ch_inte;
473
Alexandre Bounine1ccc8192013-05-24 15:55:17 -0700474 /* For MSI mode disable all device-level interrupts */
475 if (priv->flags & TSI721_USING_MSI)
476 iowrite32(0, priv->regs + TSI721_DEV_INTE);
477
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700478 dev_int = ioread32(priv->regs + TSI721_DEV_INT);
479 if (!dev_int)
480 return IRQ_NONE;
481
482 dev_ch_int = ioread32(priv->regs + TSI721_DEV_CHAN_INT);
483
484 if (dev_int & TSI721_DEV_INT_SR2PC_CH) {
485 /* Service SR2PC Channel interrupts */
486 if (dev_ch_int & TSI721_INT_SR2PC_CHAN(IDB_QUEUE)) {
487 /* Service Inbound Doorbell interrupt */
488 intval = ioread32(priv->regs +
489 TSI721_SR_CHINT(IDB_QUEUE));
490 if (intval & TSI721_SR_CHINT_IDBQRCV)
491 tsi721_dbell_handler(mport);
492 else
493 dev_info(&priv->pdev->dev,
494 "Unsupported SR_CH_INT %x\n", intval);
495
496 /* Clear interrupts */
497 iowrite32(intval,
498 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
499 ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
500 }
501 }
502
503 if (dev_int & TSI721_DEV_INT_SMSG_CH) {
504 int ch;
505
506 /*
507 * Service channel interrupts from Messaging Engine
508 */
509
510 if (dev_ch_int & TSI721_INT_IMSG_CHAN_M) { /* Inbound Msg */
511 /* Disable signaled OB MSG Channel interrupts */
512 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
513 ch_inte &= ~(dev_ch_int & TSI721_INT_IMSG_CHAN_M);
514 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
515
516 /*
517 * Process Inbound Message interrupt for each MBOX
518 */
519 for (ch = 4; ch < RIO_MAX_MBOX + 4; ch++) {
520 if (!(dev_ch_int & TSI721_INT_IMSG_CHAN(ch)))
521 continue;
522 tsi721_imsg_handler(priv, ch);
523 }
524 }
525
526 if (dev_ch_int & TSI721_INT_OMSG_CHAN_M) { /* Outbound Msg */
527 /* Disable signaled OB MSG Channel interrupts */
528 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
529 ch_inte &= ~(dev_ch_int & TSI721_INT_OMSG_CHAN_M);
530 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
531
532 /*
533 * Process Outbound Message interrupts for each MBOX
534 */
535
536 for (ch = 0; ch < RIO_MAX_MBOX; ch++) {
537 if (!(dev_ch_int & TSI721_INT_OMSG_CHAN(ch)))
538 continue;
539 tsi721_omsg_handler(priv, ch);
540 }
541 }
542 }
543
544 if (dev_int & TSI721_DEV_INT_SRIO) {
545 /* Service SRIO MAC interrupts */
546 intval = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
547 if (intval & TSI721_RIO_EM_INT_STAT_PW_RX)
548 tsi721_pw_handler(mport);
549 }
550
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700551#ifdef CONFIG_RAPIDIO_DMA_ENGINE
552 if (dev_int & TSI721_DEV_INT_BDMA_CH) {
553 int ch;
554
555 if (dev_ch_int & TSI721_INT_BDMA_CHAN_M) {
556 dev_dbg(&priv->pdev->dev,
557 "IRQ from DMA channel 0x%08x\n", dev_ch_int);
558
559 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++) {
560 if (!(dev_ch_int & TSI721_INT_BDMA_CHAN(ch)))
561 continue;
562 tsi721_bdma_handler(&priv->bdma[ch]);
563 }
564 }
565 }
566#endif
Alexandre Bounine1ccc8192013-05-24 15:55:17 -0700567
568 /* For MSI mode re-enable device-level interrupts */
569 if (priv->flags & TSI721_USING_MSI) {
570 dev_int = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
571 TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
572 iowrite32(dev_int, priv->regs + TSI721_DEV_INTE);
573 }
574
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700575 return IRQ_HANDLED;
576}
577
578static void tsi721_interrupts_init(struct tsi721_device *priv)
579{
580 u32 intr;
581
582 /* Enable IDB interrupts */
583 iowrite32(TSI721_SR_CHINT_ALL,
584 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
585 iowrite32(TSI721_SR_CHINT_IDBQRCV,
586 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700587
588 /* Enable SRIO MAC interrupts */
589 iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT,
590 priv->regs + TSI721_RIO_EM_DEV_INT_EN);
591
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700592 /* Enable interrupts from channels in use */
593#ifdef CONFIG_RAPIDIO_DMA_ENGINE
594 intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE) |
595 (TSI721_INT_BDMA_CHAN_M &
596 ~TSI721_INT_BDMA_CHAN(TSI721_DMACH_MAINT));
597#else
598 intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE);
599#endif
600 iowrite32(intr, priv->regs + TSI721_DEV_CHAN_INTE);
601
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700602 if (priv->flags & TSI721_USING_MSIX)
603 intr = TSI721_DEV_INT_SRIO;
604 else
605 intr = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700606 TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700607
608 iowrite32(intr, priv->regs + TSI721_DEV_INTE);
609 ioread32(priv->regs + TSI721_DEV_INTE);
610}
611
612#ifdef CONFIG_PCI_MSI
613/**
614 * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
615 * @irq: Linux interrupt number
616 * @ptr: Pointer to interrupt-specific data (mport structure)
617 *
618 * Handles outbound messaging interrupts signaled using MSI-X.
619 */
620static irqreturn_t tsi721_omsg_msix(int irq, void *ptr)
621{
622 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
623 int mbox;
624
625 mbox = (irq - priv->msix[TSI721_VECT_OMB0_DONE].vector) % RIO_MAX_MBOX;
626 tsi721_omsg_handler(priv, mbox);
627 return IRQ_HANDLED;
628}
629
630/**
631 * tsi721_imsg_msix - MSI-X interrupt handler for inbound messaging
632 * @irq: Linux interrupt number
633 * @ptr: Pointer to interrupt-specific data (mport structure)
634 *
635 * Handles inbound messaging interrupts signaled using MSI-X.
636 */
637static irqreturn_t tsi721_imsg_msix(int irq, void *ptr)
638{
639 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
640 int mbox;
641
642 mbox = (irq - priv->msix[TSI721_VECT_IMB0_RCV].vector) % RIO_MAX_MBOX;
643 tsi721_imsg_handler(priv, mbox + 4);
644 return IRQ_HANDLED;
645}
646
647/**
648 * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
649 * @irq: Linux interrupt number
650 * @ptr: Pointer to interrupt-specific data (mport structure)
651 *
652 * Handles Tsi721 interrupts from SRIO MAC.
653 */
654static irqreturn_t tsi721_srio_msix(int irq, void *ptr)
655{
656 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
657 u32 srio_int;
658
659 /* Service SRIO MAC interrupts */
660 srio_int = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
661 if (srio_int & TSI721_RIO_EM_INT_STAT_PW_RX)
662 tsi721_pw_handler((struct rio_mport *)ptr);
663
664 return IRQ_HANDLED;
665}
666
667/**
668 * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
669 * @irq: Linux interrupt number
670 * @ptr: Pointer to interrupt-specific data (mport structure)
671 *
672 * Handles Tsi721 interrupts from SR2PC Channel.
673 * NOTE: At this moment services only one SR2PC channel associated with inbound
674 * doorbells.
675 */
676static irqreturn_t tsi721_sr2pc_ch_msix(int irq, void *ptr)
677{
678 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
679 u32 sr_ch_int;
680
681 /* Service Inbound DB interrupt from SR2PC channel */
682 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
683 if (sr_ch_int & TSI721_SR_CHINT_IDBQRCV)
684 tsi721_dbell_handler((struct rio_mport *)ptr);
685
686 /* Clear interrupts */
687 iowrite32(sr_ch_int, priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
688 /* Read back to ensure that interrupt was cleared */
689 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
690
691 return IRQ_HANDLED;
692}
693
694/**
695 * tsi721_request_msix - register interrupt service for MSI-X mode.
696 * @mport: RapidIO master port structure
697 *
698 * Registers MSI-X interrupt service routines for interrupts that are active
699 * immediately after mport initialization. Messaging interrupt service routines
700 * should be registered during corresponding open requests.
701 */
702static int tsi721_request_msix(struct rio_mport *mport)
703{
704 struct tsi721_device *priv = mport->priv;
705 int err = 0;
706
707 err = request_irq(priv->msix[TSI721_VECT_IDB].vector,
708 tsi721_sr2pc_ch_msix, 0,
709 priv->msix[TSI721_VECT_IDB].irq_name, (void *)mport);
710 if (err)
711 goto out;
712
713 err = request_irq(priv->msix[TSI721_VECT_PWRX].vector,
714 tsi721_srio_msix, 0,
715 priv->msix[TSI721_VECT_PWRX].irq_name, (void *)mport);
716 if (err)
717 free_irq(
718 priv->msix[TSI721_VECT_IDB].vector,
719 (void *)mport);
720out:
721 return err;
722}
723
724/**
725 * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
726 * @priv: pointer to tsi721 private data
727 *
728 * Configures MSI-X support for Tsi721. Supports only an exact number
729 * of requested vectors.
730 */
731static int tsi721_enable_msix(struct tsi721_device *priv)
732{
733 struct msix_entry entries[TSI721_VECT_MAX];
734 int err;
735 int i;
736
737 entries[TSI721_VECT_IDB].entry = TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE);
738 entries[TSI721_VECT_PWRX].entry = TSI721_MSIX_SRIO_MAC_INT;
739
740 /*
741 * Initialize MSI-X entries for Messaging Engine:
742 * this driver supports four RIO mailboxes (inbound and outbound)
743 * NOTE: Inbound message MBOX 0...4 use IB channels 4...7. Therefore
744 * offset +4 is added to IB MBOX number.
745 */
746 for (i = 0; i < RIO_MAX_MBOX; i++) {
747 entries[TSI721_VECT_IMB0_RCV + i].entry =
748 TSI721_MSIX_IMSG_DQ_RCV(i + 4);
749 entries[TSI721_VECT_IMB0_INT + i].entry =
750 TSI721_MSIX_IMSG_INT(i + 4);
751 entries[TSI721_VECT_OMB0_DONE + i].entry =
752 TSI721_MSIX_OMSG_DONE(i);
753 entries[TSI721_VECT_OMB0_INT + i].entry =
754 TSI721_MSIX_OMSG_INT(i);
755 }
756
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700757#ifdef CONFIG_RAPIDIO_DMA_ENGINE
758 /*
759 * Initialize MSI-X entries for Block DMA Engine:
760 * this driver supports XXX DMA channels
761 * (one is reserved for SRIO maintenance transactions)
762 */
763 for (i = 0; i < TSI721_DMA_CHNUM; i++) {
764 entries[TSI721_VECT_DMA0_DONE + i].entry =
765 TSI721_MSIX_DMACH_DONE(i);
766 entries[TSI721_VECT_DMA0_INT + i].entry =
767 TSI721_MSIX_DMACH_INT(i);
768 }
769#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
770
Alexander Gordeev1c92ab12014-06-06 14:37:16 -0700771 err = pci_enable_msix_exact(priv->pdev, entries, ARRAY_SIZE(entries));
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700772 if (err) {
Alexander Gordeev1c92ab12014-06-06 14:37:16 -0700773 dev_err(&priv->pdev->dev,
774 "Failed to enable MSI-X (err=%d)\n", err);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700775 return err;
776 }
777
778 /*
779 * Copy MSI-X vector information into tsi721 private structure
780 */
781 priv->msix[TSI721_VECT_IDB].vector = entries[TSI721_VECT_IDB].vector;
782 snprintf(priv->msix[TSI721_VECT_IDB].irq_name, IRQ_DEVICE_NAME_MAX,
783 DRV_NAME "-idb@pci:%s", pci_name(priv->pdev));
784 priv->msix[TSI721_VECT_PWRX].vector = entries[TSI721_VECT_PWRX].vector;
785 snprintf(priv->msix[TSI721_VECT_PWRX].irq_name, IRQ_DEVICE_NAME_MAX,
786 DRV_NAME "-pwrx@pci:%s", pci_name(priv->pdev));
787
788 for (i = 0; i < RIO_MAX_MBOX; i++) {
789 priv->msix[TSI721_VECT_IMB0_RCV + i].vector =
790 entries[TSI721_VECT_IMB0_RCV + i].vector;
791 snprintf(priv->msix[TSI721_VECT_IMB0_RCV + i].irq_name,
792 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbr%d@pci:%s",
793 i, pci_name(priv->pdev));
794
795 priv->msix[TSI721_VECT_IMB0_INT + i].vector =
796 entries[TSI721_VECT_IMB0_INT + i].vector;
797 snprintf(priv->msix[TSI721_VECT_IMB0_INT + i].irq_name,
798 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbi%d@pci:%s",
799 i, pci_name(priv->pdev));
800
801 priv->msix[TSI721_VECT_OMB0_DONE + i].vector =
802 entries[TSI721_VECT_OMB0_DONE + i].vector;
803 snprintf(priv->msix[TSI721_VECT_OMB0_DONE + i].irq_name,
804 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombd%d@pci:%s",
805 i, pci_name(priv->pdev));
806
807 priv->msix[TSI721_VECT_OMB0_INT + i].vector =
808 entries[TSI721_VECT_OMB0_INT + i].vector;
809 snprintf(priv->msix[TSI721_VECT_OMB0_INT + i].irq_name,
810 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombi%d@pci:%s",
811 i, pci_name(priv->pdev));
812 }
813
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700814#ifdef CONFIG_RAPIDIO_DMA_ENGINE
815 for (i = 0; i < TSI721_DMA_CHNUM; i++) {
816 priv->msix[TSI721_VECT_DMA0_DONE + i].vector =
817 entries[TSI721_VECT_DMA0_DONE + i].vector;
818 snprintf(priv->msix[TSI721_VECT_DMA0_DONE + i].irq_name,
819 IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmad%d@pci:%s",
820 i, pci_name(priv->pdev));
821
822 priv->msix[TSI721_VECT_DMA0_INT + i].vector =
823 entries[TSI721_VECT_DMA0_INT + i].vector;
824 snprintf(priv->msix[TSI721_VECT_DMA0_INT + i].irq_name,
825 IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmai%d@pci:%s",
826 i, pci_name(priv->pdev));
827 }
828#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
829
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700830 return 0;
831}
832#endif /* CONFIG_PCI_MSI */
833
834static int tsi721_request_irq(struct rio_mport *mport)
835{
836 struct tsi721_device *priv = mport->priv;
837 int err;
838
839#ifdef CONFIG_PCI_MSI
840 if (priv->flags & TSI721_USING_MSIX)
841 err = tsi721_request_msix(mport);
842 else
843#endif
844 err = request_irq(priv->pdev->irq, tsi721_irqhandler,
845 (priv->flags & TSI721_USING_MSI) ? 0 : IRQF_SHARED,
846 DRV_NAME, (void *)mport);
847
848 if (err)
849 dev_err(&priv->pdev->dev,
850 "Unable to allocate interrupt, Error: %d\n", err);
851
852 return err;
853}
854
855/**
856 * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
857 * translation regions.
858 * @priv: pointer to tsi721 private data
859 *
860 * Disables SREP translation regions.
861 */
862static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
863{
864 int i;
865
866 /* Disable all PC2SR translation windows */
867 for (i = 0; i < TSI721_OBWIN_NUM; i++)
868 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
869}
870
871/**
Alexandre Bounine71afe342012-10-04 17:16:00 -0700872 * tsi721_rio_map_inb_mem -- Mapping inbound memory region.
873 * @mport: RapidIO master port
874 * @lstart: Local memory space start address.
875 * @rstart: RapidIO space start address.
876 * @size: The mapping region size.
877 * @flags: Flags for mapping. 0 for using default flags.
878 *
879 * Return: 0 -- Success.
880 *
881 * This function will create the inbound mapping
882 * from rstart to lstart.
883 */
884static int tsi721_rio_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
885 u64 rstart, u32 size, u32 flags)
886{
887 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700888 int i, avail = -1;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700889 u32 regval;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700890 struct tsi721_ib_win *ib_win;
891 int ret = -EBUSY;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700892
893 if (!is_power_of_2(size) || size < 0x1000 ||
894 ((u64)lstart & (size - 1)) || (rstart & (size - 1)))
895 return -EINVAL;
896
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700897 spin_lock(&priv->win_lock);
898 /*
899 * Scan for overlapping with active regions and mark the first available
900 * IB window at the same time.
901 */
Alexandre Bounine71afe342012-10-04 17:16:00 -0700902 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700903 ib_win = &priv->ib_win[i];
904 if (!ib_win->active) {
905 if (avail == -1) {
906 avail = i;
907 ret = 0;
908 }
909 } else if (rstart < (ib_win->rstart + ib_win->size) &&
910 (rstart + size) > ib_win->rstart) {
911 ret = -EFAULT;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700912 break;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700913 }
Alexandre Bounine71afe342012-10-04 17:16:00 -0700914 }
915
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700916 if (ret)
917 goto err_out;
918 i = avail;
919
920 /* Sanity check: available IB window must be disabled at this point */
921 regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
922 if (WARN_ON(regval & TSI721_IBWIN_LB_WEN)) {
923 ret = -EIO;
924 goto err_out;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700925 }
926
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700927 ib_win = &priv->ib_win[i];
928 ib_win->active = true;
929 ib_win->rstart = rstart;
930 ib_win->lstart = lstart;
931 ib_win->size = size;
932 spin_unlock(&priv->win_lock);
933
Alexandre Bounine71afe342012-10-04 17:16:00 -0700934 iowrite32(TSI721_IBWIN_SIZE(size) << 8,
935 priv->regs + TSI721_IBWIN_SZ(i));
936
937 iowrite32(((u64)lstart >> 32), priv->regs + TSI721_IBWIN_TUA(i));
938 iowrite32(((u64)lstart & TSI721_IBWIN_TLA_ADD),
939 priv->regs + TSI721_IBWIN_TLA(i));
940
941 iowrite32(rstart >> 32, priv->regs + TSI721_IBWIN_UB(i));
942 iowrite32((rstart & TSI721_IBWIN_LB_BA) | TSI721_IBWIN_LB_WEN,
943 priv->regs + TSI721_IBWIN_LB(i));
944 dev_dbg(&priv->pdev->dev,
945 "Configured IBWIN%d mapping (RIO_0x%llx -> PCIe_0x%llx)\n",
946 i, rstart, (unsigned long long)lstart);
947
948 return 0;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700949err_out:
950 spin_unlock(&priv->win_lock);
951 return ret;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700952}
953
954/**
955 * fsl_rio_unmap_inb_mem -- Unmapping inbound memory region.
956 * @mport: RapidIO master port
957 * @lstart: Local memory space start address.
958 */
959static void tsi721_rio_unmap_inb_mem(struct rio_mport *mport,
960 dma_addr_t lstart)
961{
962 struct tsi721_device *priv = mport->priv;
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700963 struct tsi721_ib_win *ib_win;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700964 int i;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700965
966 /* Search for matching active inbound translation window */
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700967 spin_lock(&priv->win_lock);
Alexandre Bounine71afe342012-10-04 17:16:00 -0700968 for (i = 0; i < TSI721_IBWIN_NUM; i++) {
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700969 ib_win = &priv->ib_win[i];
970 if (ib_win->active && ib_win->lstart == lstart) {
971 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
972 ib_win->active = false;
973 break;
Alexandre Bounine71afe342012-10-04 17:16:00 -0700974 }
975 }
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700976 spin_unlock(&priv->win_lock);
977
978 if (i == TSI721_IBWIN_NUM)
979 dev_err(&priv->pdev->dev,
980 "IB window mapped to %llx not found\n",
981 (unsigned long long)lstart);
Alexandre Bounine71afe342012-10-04 17:16:00 -0700982}
983
984/**
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700985 * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
986 * translation regions.
987 * @priv: pointer to tsi721 private data
988 *
989 * Disables inbound windows.
990 */
991static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
992{
993 int i;
994
995 /* Disable all SR2PC inbound windows */
996 for (i = 0; i < TSI721_IBWIN_NUM; i++)
Alexandre Bounine71afe342012-10-04 17:16:00 -0700997 iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
Alexandre Bounineba5d1412016-03-22 14:25:51 -0700998 spin_lock_init(&priv->win_lock);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700999}
1000
1001/**
1002 * tsi721_port_write_init - Inbound port write interface init
1003 * @priv: pointer to tsi721 private data
1004 *
1005 * Initializes inbound port write handler.
1006 * Returns %0 on success or %-ENOMEM on failure.
1007 */
1008static int tsi721_port_write_init(struct tsi721_device *priv)
1009{
1010 priv->pw_discard_count = 0;
1011 INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
1012 spin_lock_init(&priv->pw_fifo_lock);
1013 if (kfifo_alloc(&priv->pw_fifo,
1014 TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
1015 dev_err(&priv->pdev->dev, "PW FIFO allocation failed\n");
1016 return -ENOMEM;
1017 }
1018
1019 /* Use reliable port-write capture mode */
1020 iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
1021 return 0;
1022}
1023
1024static int tsi721_doorbell_init(struct tsi721_device *priv)
1025{
1026 /* Outbound Doorbells do not require any setup.
1027 * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
1028 * That BAR1 was mapped during the probe routine.
1029 */
1030
1031 /* Initialize Inbound Doorbell processing DPC and queue */
1032 priv->db_discard_count = 0;
1033 INIT_WORK(&priv->idb_work, tsi721_db_dpc);
1034
1035 /* Allocate buffer for inbound doorbells queue */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001036 priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001037 IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1038 &priv->idb_dma, GFP_KERNEL);
1039 if (!priv->idb_base)
1040 return -ENOMEM;
1041
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001042 dev_dbg(&priv->pdev->dev, "Allocated IDB buffer @ %p (phys = %llx)\n",
1043 priv->idb_base, (unsigned long long)priv->idb_dma);
1044
1045 iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
1046 priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
1047 iowrite32(((u64)priv->idb_dma >> 32),
1048 priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
1049 iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
1050 priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
1051 /* Enable accepting all inbound doorbells */
1052 iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
1053
1054 iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
1055
1056 iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
1057
1058 return 0;
1059}
1060
1061static void tsi721_doorbell_free(struct tsi721_device *priv)
1062{
1063 if (priv->idb_base == NULL)
1064 return;
1065
1066 /* Free buffer allocated for inbound doorbell queue */
1067 dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
1068 priv->idb_base, priv->idb_dma);
1069 priv->idb_base = NULL;
1070}
1071
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001072/**
1073 * tsi721_bdma_maint_init - Initialize maintenance request BDMA channel.
1074 * @priv: pointer to tsi721 private data
1075 *
1076 * Initialize BDMA channel allocated for RapidIO maintenance read/write
1077 * request generation
1078 * Returns %0 on success or %-ENOMEM on failure.
1079 */
1080static int tsi721_bdma_maint_init(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001081{
1082 struct tsi721_dma_desc *bd_ptr;
1083 u64 *sts_ptr;
1084 dma_addr_t bd_phys, sts_phys;
1085 int sts_size;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001086 int bd_num = 2;
1087 void __iomem *regs;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001088
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001089 dev_dbg(&priv->pdev->dev,
1090 "Init Block DMA Engine for Maintenance requests, CH%d\n",
1091 TSI721_DMACH_MAINT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001092
1093 /*
1094 * Initialize DMA channel for maintenance requests
1095 */
1096
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001097 priv->mdma.ch_id = TSI721_DMACH_MAINT;
1098 regs = priv->regs + TSI721_DMAC_BASE(TSI721_DMACH_MAINT);
1099
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001100 /* Allocate space for DMA descriptors */
Alexandre Bounineceb96392011-12-08 14:34:35 -08001101 bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001102 bd_num * sizeof(struct tsi721_dma_desc),
1103 &bd_phys, GFP_KERNEL);
1104 if (!bd_ptr)
1105 return -ENOMEM;
1106
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001107 priv->mdma.bd_num = bd_num;
1108 priv->mdma.bd_phys = bd_phys;
1109 priv->mdma.bd_base = bd_ptr;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001110
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001111 dev_dbg(&priv->pdev->dev, "DMA descriptors @ %p (phys = %llx)\n",
1112 bd_ptr, (unsigned long long)bd_phys);
1113
1114 /* Allocate space for descriptor status FIFO */
1115 sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
1116 bd_num : TSI721_DMA_MINSTSSZ;
1117 sts_size = roundup_pow_of_two(sts_size);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001118 sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001119 sts_size * sizeof(struct tsi721_dma_sts),
1120 &sts_phys, GFP_KERNEL);
1121 if (!sts_ptr) {
1122 /* Free space allocated for DMA descriptors */
1123 dma_free_coherent(&priv->pdev->dev,
1124 bd_num * sizeof(struct tsi721_dma_desc),
1125 bd_ptr, bd_phys);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001126 priv->mdma.bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001127 return -ENOMEM;
1128 }
1129
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001130 priv->mdma.sts_phys = sts_phys;
1131 priv->mdma.sts_base = sts_ptr;
1132 priv->mdma.sts_size = sts_size;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001133
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001134 dev_dbg(&priv->pdev->dev,
1135 "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
1136 sts_ptr, (unsigned long long)sts_phys, sts_size);
1137
1138 /* Initialize DMA descriptors ring */
1139 bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
1140 bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
1141 TSI721_DMAC_DPTRL_MASK);
1142 bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
1143
1144 /* Setup DMA descriptor pointers */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001145 iowrite32(((u64)bd_phys >> 32), regs + TSI721_DMAC_DPTRH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001146 iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001147 regs + TSI721_DMAC_DPTRL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001148
1149 /* Setup descriptor status FIFO */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001150 iowrite32(((u64)sts_phys >> 32), regs + TSI721_DMAC_DSBH);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001151 iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001152 regs + TSI721_DMAC_DSBL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001153 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001154 regs + TSI721_DMAC_DSSZ);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001155
1156 /* Clear interrupt bits */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001157 iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001158
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001159 ioread32(regs + TSI721_DMAC_INT);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001160
1161 /* Toggle DMA channel initialization */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001162 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
1163 ioread32(regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001164 udelay(10);
1165
1166 return 0;
1167}
1168
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001169static int tsi721_bdma_maint_free(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001170{
1171 u32 ch_stat;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001172 struct tsi721_bdma_maint *mdma = &priv->mdma;
1173 void __iomem *regs = priv->regs + TSI721_DMAC_BASE(mdma->ch_id);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001174
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001175 if (mdma->bd_base == NULL)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001176 return 0;
1177
1178 /* Check if DMA channel still running */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001179 ch_stat = ioread32(regs + TSI721_DMAC_STS);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001180 if (ch_stat & TSI721_DMAC_STS_RUN)
1181 return -EFAULT;
1182
1183 /* Put DMA channel into init state */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001184 iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001185
1186 /* Free space allocated for DMA descriptors */
1187 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001188 mdma->bd_num * sizeof(struct tsi721_dma_desc),
1189 mdma->bd_base, mdma->bd_phys);
1190 mdma->bd_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001191
1192 /* Free space allocated for status FIFO */
1193 dma_free_coherent(&priv->pdev->dev,
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001194 mdma->sts_size * sizeof(struct tsi721_dma_sts),
1195 mdma->sts_base, mdma->sts_phys);
1196 mdma->sts_base = NULL;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001197 return 0;
1198}
1199
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001200/* Enable Inbound Messaging Interrupts */
1201static void
1202tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
1203 u32 inte_mask)
1204{
1205 u32 rval;
1206
1207 if (!inte_mask)
1208 return;
1209
1210 /* Clear pending Inbound Messaging interrupts */
1211 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1212
1213 /* Enable Inbound Messaging interrupts */
1214 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1215 iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
1216
1217 if (priv->flags & TSI721_USING_MSIX)
1218 return; /* Finished if we are in MSI-X mode */
1219
1220 /*
1221 * For MSI and INTA interrupt signalling we need to enable next levels
1222 */
1223
1224 /* Enable Device Channel Interrupt */
1225 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1226 iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
1227 priv->regs + TSI721_DEV_CHAN_INTE);
1228}
1229
1230/* Disable Inbound Messaging Interrupts */
1231static void
1232tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
1233 u32 inte_mask)
1234{
1235 u32 rval;
1236
1237 if (!inte_mask)
1238 return;
1239
1240 /* Clear pending Inbound Messaging interrupts */
1241 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1242
1243 /* Disable Inbound Messaging interrupts */
1244 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1245 rval &= ~inte_mask;
1246 iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
1247
1248 if (priv->flags & TSI721_USING_MSIX)
1249 return; /* Finished if we are in MSI-X mode */
1250
1251 /*
1252 * For MSI and INTA interrupt signalling we need to disable next levels
1253 */
1254
1255 /* Disable Device Channel Interrupt */
1256 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1257 rval &= ~TSI721_INT_IMSG_CHAN(ch);
1258 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1259}
1260
1261/* Enable Outbound Messaging interrupts */
1262static void
1263tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
1264 u32 inte_mask)
1265{
1266 u32 rval;
1267
1268 if (!inte_mask)
1269 return;
1270
1271 /* Clear pending Outbound Messaging interrupts */
1272 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1273
1274 /* Enable Outbound Messaging channel interrupts */
1275 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1276 iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
1277
1278 if (priv->flags & TSI721_USING_MSIX)
1279 return; /* Finished if we are in MSI-X mode */
1280
1281 /*
1282 * For MSI and INTA interrupt signalling we need to enable next levels
1283 */
1284
1285 /* Enable Device Channel Interrupt */
1286 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1287 iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
1288 priv->regs + TSI721_DEV_CHAN_INTE);
1289}
1290
1291/* Disable Outbound Messaging interrupts */
1292static void
1293tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
1294 u32 inte_mask)
1295{
1296 u32 rval;
1297
1298 if (!inte_mask)
1299 return;
1300
1301 /* Clear pending Outbound Messaging interrupts */
1302 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1303
1304 /* Disable Outbound Messaging interrupts */
1305 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1306 rval &= ~inte_mask;
1307 iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
1308
1309 if (priv->flags & TSI721_USING_MSIX)
1310 return; /* Finished if we are in MSI-X mode */
1311
1312 /*
1313 * For MSI and INTA interrupt signalling we need to disable next levels
1314 */
1315
1316 /* Disable Device Channel Interrupt */
1317 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1318 rval &= ~TSI721_INT_OMSG_CHAN(ch);
1319 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1320}
1321
1322/**
1323 * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
1324 * @mport: Master port with outbound message queue
1325 * @rdev: Target of outbound message
1326 * @mbox: Outbound mailbox
1327 * @buffer: Message to add to outbound queue
1328 * @len: Length of message
1329 */
1330static int
1331tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
1332 void *buffer, size_t len)
1333{
1334 struct tsi721_device *priv = mport->priv;
1335 struct tsi721_omsg_desc *desc;
1336 u32 tx_slot;
1337
1338 if (!priv->omsg_init[mbox] ||
1339 len > TSI721_MSG_MAX_SIZE || len < 8)
1340 return -EINVAL;
1341
1342 tx_slot = priv->omsg_ring[mbox].tx_slot;
1343
1344 /* Copy copy message into transfer buffer */
1345 memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
1346
1347 if (len & 0x7)
1348 len += 8;
1349
1350 /* Build descriptor associated with buffer */
1351 desc = priv->omsg_ring[mbox].omd_base;
1352 desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
1353 if (tx_slot % 4 == 0)
1354 desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
1355
1356 desc[tx_slot].msg_info =
1357 cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
1358 (0xe << 12) | (len & 0xff8));
1359 desc[tx_slot].bufptr_lo =
1360 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
1361 0xffffffff);
1362 desc[tx_slot].bufptr_hi =
1363 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
1364
1365 priv->omsg_ring[mbox].wr_count++;
1366
1367 /* Go to next descriptor */
1368 if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
1369 priv->omsg_ring[mbox].tx_slot = 0;
1370 /* Move through the ring link descriptor at the end */
1371 priv->omsg_ring[mbox].wr_count++;
1372 }
1373
1374 mb();
1375
1376 /* Set new write count value */
1377 iowrite32(priv->omsg_ring[mbox].wr_count,
1378 priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1379 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1380
1381 return 0;
1382}
1383
1384/**
1385 * tsi721_omsg_handler - Outbound Message Interrupt Handler
1386 * @priv: pointer to tsi721 private data
1387 * @ch: number of OB MSG channel to service
1388 *
1389 * Services channel interrupts from outbound messaging engine.
1390 */
1391static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
1392{
1393 u32 omsg_int;
1394
1395 spin_lock(&priv->omsg_ring[ch].lock);
1396
1397 omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
1398
1399 if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
1400 dev_info(&priv->pdev->dev,
1401 "OB MBOX%d: Status FIFO is full\n", ch);
1402
1403 if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
1404 u32 srd_ptr;
1405 u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
1406 int i, j;
1407 u32 tx_slot;
1408
1409 /*
1410 * Find last successfully processed descriptor
1411 */
1412
1413 /* Check and clear descriptor status FIFO entries */
1414 srd_ptr = priv->omsg_ring[ch].sts_rdptr;
1415 sts_ptr = priv->omsg_ring[ch].sts_base;
1416 j = srd_ptr * 8;
1417 while (sts_ptr[j]) {
1418 for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
1419 prev_ptr = last_ptr;
1420 last_ptr = le64_to_cpu(sts_ptr[j]);
1421 sts_ptr[j] = 0;
1422 }
1423
1424 ++srd_ptr;
1425 srd_ptr %= priv->omsg_ring[ch].sts_size;
1426 j = srd_ptr * 8;
1427 }
1428
1429 if (last_ptr == 0)
1430 goto no_sts_update;
1431
1432 priv->omsg_ring[ch].sts_rdptr = srd_ptr;
1433 iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
1434
1435 if (!priv->mport->outb_msg[ch].mcback)
1436 goto no_sts_update;
1437
1438 /* Inform upper layer about transfer completion */
1439
1440 tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
1441 sizeof(struct tsi721_omsg_desc);
1442
1443 /*
1444 * Check if this is a Link Descriptor (LD).
1445 * If yes, ignore LD and use descriptor processed
1446 * before LD.
1447 */
1448 if (tx_slot == priv->omsg_ring[ch].size) {
1449 if (prev_ptr)
1450 tx_slot = (prev_ptr -
1451 (u64)priv->omsg_ring[ch].omd_phys)/
1452 sizeof(struct tsi721_omsg_desc);
1453 else
1454 goto no_sts_update;
1455 }
1456
1457 /* Move slot index to the next message to be sent */
1458 ++tx_slot;
1459 if (tx_slot == priv->omsg_ring[ch].size)
1460 tx_slot = 0;
1461 BUG_ON(tx_slot >= priv->omsg_ring[ch].size);
1462 priv->mport->outb_msg[ch].mcback(priv->mport,
1463 priv->omsg_ring[ch].dev_id, ch,
1464 tx_slot);
1465 }
1466
1467no_sts_update:
1468
1469 if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
1470 /*
1471 * Outbound message operation aborted due to error,
1472 * reinitialize OB MSG channel
1473 */
1474
1475 dev_dbg(&priv->pdev->dev, "OB MSG ABORT ch_stat=%x\n",
1476 ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
1477
1478 iowrite32(TSI721_OBDMAC_INT_ERROR,
1479 priv->regs + TSI721_OBDMAC_INT(ch));
1480 iowrite32(TSI721_OBDMAC_CTL_INIT,
1481 priv->regs + TSI721_OBDMAC_CTL(ch));
1482 ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
1483
1484 /* Inform upper level to clear all pending tx slots */
1485 if (priv->mport->outb_msg[ch].mcback)
1486 priv->mport->outb_msg[ch].mcback(priv->mport,
1487 priv->omsg_ring[ch].dev_id, ch,
1488 priv->omsg_ring[ch].tx_slot);
1489 /* Synch tx_slot tracking */
1490 iowrite32(priv->omsg_ring[ch].tx_slot,
1491 priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1492 ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1493 priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
1494 priv->omsg_ring[ch].sts_rdptr = 0;
1495 }
1496
1497 /* Clear channel interrupts */
1498 iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
1499
1500 if (!(priv->flags & TSI721_USING_MSIX)) {
1501 u32 ch_inte;
1502
1503 /* Re-enable channel interrupts */
1504 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1505 ch_inte |= TSI721_INT_OMSG_CHAN(ch);
1506 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1507 }
1508
1509 spin_unlock(&priv->omsg_ring[ch].lock);
1510}
1511
1512/**
1513 * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
1514 * @mport: Master port implementing Outbound Messaging Engine
1515 * @dev_id: Device specific pointer to pass on event
1516 * @mbox: Mailbox to open
1517 * @entries: Number of entries in the outbound mailbox ring
1518 */
1519static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
1520 int mbox, int entries)
1521{
1522 struct tsi721_device *priv = mport->priv;
1523 struct tsi721_omsg_desc *bd_ptr;
1524 int i, rc = 0;
1525
1526 if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
1527 (entries > (TSI721_OMSGD_RING_SIZE)) ||
1528 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1529 rc = -EINVAL;
1530 goto out;
1531 }
1532
1533 priv->omsg_ring[mbox].dev_id = dev_id;
1534 priv->omsg_ring[mbox].size = entries;
1535 priv->omsg_ring[mbox].sts_rdptr = 0;
1536 spin_lock_init(&priv->omsg_ring[mbox].lock);
1537
1538 /* Outbound Msg Buffer allocation based on
1539 the number of maximum descriptor entries */
1540 for (i = 0; i < entries; i++) {
1541 priv->omsg_ring[mbox].omq_base[i] =
1542 dma_alloc_coherent(
1543 &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
1544 &priv->omsg_ring[mbox].omq_phys[i],
1545 GFP_KERNEL);
1546 if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
1547 dev_dbg(&priv->pdev->dev,
1548 "Unable to allocate OB MSG data buffer for"
1549 " MBOX%d\n", mbox);
1550 rc = -ENOMEM;
1551 goto out_buf;
1552 }
1553 }
1554
1555 /* Outbound message descriptor allocation */
1556 priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
1557 &priv->pdev->dev,
1558 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1559 &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
1560 if (priv->omsg_ring[mbox].omd_base == NULL) {
1561 dev_dbg(&priv->pdev->dev,
1562 "Unable to allocate OB MSG descriptor memory "
1563 "for MBOX%d\n", mbox);
1564 rc = -ENOMEM;
1565 goto out_buf;
1566 }
1567
1568 priv->omsg_ring[mbox].tx_slot = 0;
1569
1570 /* Outbound message descriptor status FIFO allocation */
1571 priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001572 priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001573 priv->omsg_ring[mbox].sts_size *
1574 sizeof(struct tsi721_dma_sts),
1575 &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
1576 if (priv->omsg_ring[mbox].sts_base == NULL) {
1577 dev_dbg(&priv->pdev->dev,
1578 "Unable to allocate OB MSG descriptor status FIFO "
1579 "for MBOX%d\n", mbox);
1580 rc = -ENOMEM;
1581 goto out_desc;
1582 }
1583
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001584 /*
1585 * Configure Outbound Messaging Engine
1586 */
1587
1588 /* Setup Outbound Message descriptor pointer */
1589 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
1590 priv->regs + TSI721_OBDMAC_DPTRH(mbox));
1591 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
1592 TSI721_OBDMAC_DPTRL_MASK),
1593 priv->regs + TSI721_OBDMAC_DPTRL(mbox));
1594
1595 /* Setup Outbound Message descriptor status FIFO */
1596 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
1597 priv->regs + TSI721_OBDMAC_DSBH(mbox));
1598 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
1599 TSI721_OBDMAC_DSBL_MASK),
1600 priv->regs + TSI721_OBDMAC_DSBL(mbox));
1601 iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
1602 priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
1603
1604 /* Enable interrupts */
1605
1606#ifdef CONFIG_PCI_MSI
1607 if (priv->flags & TSI721_USING_MSIX) {
1608 /* Request interrupt service if we are in MSI-X mode */
1609 rc = request_irq(
1610 priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
1611 tsi721_omsg_msix, 0,
1612 priv->msix[TSI721_VECT_OMB0_DONE + mbox].irq_name,
1613 (void *)mport);
1614
1615 if (rc) {
1616 dev_dbg(&priv->pdev->dev,
1617 "Unable to allocate MSI-X interrupt for "
1618 "OBOX%d-DONE\n", mbox);
1619 goto out_stat;
1620 }
1621
1622 rc = request_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
1623 tsi721_omsg_msix, 0,
1624 priv->msix[TSI721_VECT_OMB0_INT + mbox].irq_name,
1625 (void *)mport);
1626
1627 if (rc) {
1628 dev_dbg(&priv->pdev->dev,
1629 "Unable to allocate MSI-X interrupt for "
1630 "MBOX%d-INT\n", mbox);
1631 free_irq(
1632 priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
1633 (void *)mport);
1634 goto out_stat;
1635 }
1636 }
1637#endif /* CONFIG_PCI_MSI */
1638
1639 tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1640
1641 /* Initialize Outbound Message descriptors ring */
1642 bd_ptr = priv->omsg_ring[mbox].omd_base;
1643 bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
1644 bd_ptr[entries].msg_info = 0;
1645 bd_ptr[entries].next_lo =
1646 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
1647 TSI721_OBDMAC_DPTRL_MASK);
1648 bd_ptr[entries].next_hi =
1649 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
1650 priv->omsg_ring[mbox].wr_count = 0;
1651 mb();
1652
1653 /* Initialize Outbound Message engine */
1654 iowrite32(TSI721_OBDMAC_CTL_INIT, priv->regs + TSI721_OBDMAC_CTL(mbox));
1655 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1656 udelay(10);
1657
1658 priv->omsg_init[mbox] = 1;
1659
1660 return 0;
1661
1662#ifdef CONFIG_PCI_MSI
1663out_stat:
1664 dma_free_coherent(&priv->pdev->dev,
1665 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1666 priv->omsg_ring[mbox].sts_base,
1667 priv->omsg_ring[mbox].sts_phys);
1668
1669 priv->omsg_ring[mbox].sts_base = NULL;
1670#endif /* CONFIG_PCI_MSI */
1671
1672out_desc:
1673 dma_free_coherent(&priv->pdev->dev,
1674 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1675 priv->omsg_ring[mbox].omd_base,
1676 priv->omsg_ring[mbox].omd_phys);
1677
1678 priv->omsg_ring[mbox].omd_base = NULL;
1679
1680out_buf:
1681 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1682 if (priv->omsg_ring[mbox].omq_base[i]) {
1683 dma_free_coherent(&priv->pdev->dev,
1684 TSI721_MSG_BUFFER_SIZE,
1685 priv->omsg_ring[mbox].omq_base[i],
1686 priv->omsg_ring[mbox].omq_phys[i]);
1687
1688 priv->omsg_ring[mbox].omq_base[i] = NULL;
1689 }
1690 }
1691
1692out:
1693 return rc;
1694}
1695
1696/**
1697 * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
1698 * @mport: Master port implementing the outbound message unit
1699 * @mbox: Mailbox to close
1700 */
1701static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
1702{
1703 struct tsi721_device *priv = mport->priv;
1704 u32 i;
1705
1706 if (!priv->omsg_init[mbox])
1707 return;
1708 priv->omsg_init[mbox] = 0;
1709
1710 /* Disable Interrupts */
1711
1712 tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1713
1714#ifdef CONFIG_PCI_MSI
1715 if (priv->flags & TSI721_USING_MSIX) {
1716 free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
1717 (void *)mport);
1718 free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
1719 (void *)mport);
1720 }
1721#endif /* CONFIG_PCI_MSI */
1722
1723 /* Free OMSG Descriptor Status FIFO */
1724 dma_free_coherent(&priv->pdev->dev,
1725 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1726 priv->omsg_ring[mbox].sts_base,
1727 priv->omsg_ring[mbox].sts_phys);
1728
1729 priv->omsg_ring[mbox].sts_base = NULL;
1730
1731 /* Free OMSG descriptors */
1732 dma_free_coherent(&priv->pdev->dev,
1733 (priv->omsg_ring[mbox].size + 1) *
1734 sizeof(struct tsi721_omsg_desc),
1735 priv->omsg_ring[mbox].omd_base,
1736 priv->omsg_ring[mbox].omd_phys);
1737
1738 priv->omsg_ring[mbox].omd_base = NULL;
1739
1740 /* Free message buffers */
1741 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1742 if (priv->omsg_ring[mbox].omq_base[i]) {
1743 dma_free_coherent(&priv->pdev->dev,
1744 TSI721_MSG_BUFFER_SIZE,
1745 priv->omsg_ring[mbox].omq_base[i],
1746 priv->omsg_ring[mbox].omq_phys[i]);
1747
1748 priv->omsg_ring[mbox].omq_base[i] = NULL;
1749 }
1750 }
1751}
1752
1753/**
1754 * tsi721_imsg_handler - Inbound Message Interrupt Handler
1755 * @priv: pointer to tsi721 private data
1756 * @ch: inbound message channel number to service
1757 *
1758 * Services channel interrupts from inbound messaging engine.
1759 */
1760static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
1761{
1762 u32 mbox = ch - 4;
1763 u32 imsg_int;
1764
1765 spin_lock(&priv->imsg_ring[mbox].lock);
1766
1767 imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
1768
1769 if (imsg_int & TSI721_IBDMAC_INT_SRTO)
1770 dev_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout\n",
1771 mbox);
1772
1773 if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
1774 dev_info(&priv->pdev->dev, "IB MBOX%d PCIe error\n",
1775 mbox);
1776
1777 if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
1778 dev_info(&priv->pdev->dev,
1779 "IB MBOX%d IB free queue low\n", mbox);
1780
1781 /* Clear IB channel interrupts */
1782 iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
1783
1784 /* If an IB Msg is received notify the upper layer */
1785 if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
1786 priv->mport->inb_msg[mbox].mcback)
1787 priv->mport->inb_msg[mbox].mcback(priv->mport,
1788 priv->imsg_ring[mbox].dev_id, mbox, -1);
1789
1790 if (!(priv->flags & TSI721_USING_MSIX)) {
1791 u32 ch_inte;
1792
1793 /* Re-enable channel interrupts */
1794 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1795 ch_inte |= TSI721_INT_IMSG_CHAN(ch);
1796 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1797 }
1798
1799 spin_unlock(&priv->imsg_ring[mbox].lock);
1800}
1801
1802/**
1803 * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
1804 * @mport: Master port implementing the Inbound Messaging Engine
1805 * @dev_id: Device specific pointer to pass on event
1806 * @mbox: Mailbox to open
1807 * @entries: Number of entries in the inbound mailbox ring
1808 */
1809static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
1810 int mbox, int entries)
1811{
1812 struct tsi721_device *priv = mport->priv;
1813 int ch = mbox + 4;
1814 int i;
1815 u64 *free_ptr;
1816 int rc = 0;
1817
1818 if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
1819 (entries > TSI721_IMSGD_RING_SIZE) ||
1820 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1821 rc = -EINVAL;
1822 goto out;
1823 }
1824
1825 /* Initialize IB Messaging Ring */
1826 priv->imsg_ring[mbox].dev_id = dev_id;
1827 priv->imsg_ring[mbox].size = entries;
1828 priv->imsg_ring[mbox].rx_slot = 0;
1829 priv->imsg_ring[mbox].desc_rdptr = 0;
1830 priv->imsg_ring[mbox].fq_wrptr = 0;
1831 for (i = 0; i < priv->imsg_ring[mbox].size; i++)
1832 priv->imsg_ring[mbox].imq_base[i] = NULL;
1833 spin_lock_init(&priv->imsg_ring[mbox].lock);
1834
1835 /* Allocate buffers for incoming messages */
1836 priv->imsg_ring[mbox].buf_base =
1837 dma_alloc_coherent(&priv->pdev->dev,
1838 entries * TSI721_MSG_BUFFER_SIZE,
1839 &priv->imsg_ring[mbox].buf_phys,
1840 GFP_KERNEL);
1841
1842 if (priv->imsg_ring[mbox].buf_base == NULL) {
1843 dev_err(&priv->pdev->dev,
1844 "Failed to allocate buffers for IB MBOX%d\n", mbox);
1845 rc = -ENOMEM;
1846 goto out;
1847 }
1848
1849 /* Allocate memory for circular free list */
1850 priv->imsg_ring[mbox].imfq_base =
1851 dma_alloc_coherent(&priv->pdev->dev,
1852 entries * 8,
1853 &priv->imsg_ring[mbox].imfq_phys,
1854 GFP_KERNEL);
1855
1856 if (priv->imsg_ring[mbox].imfq_base == NULL) {
1857 dev_err(&priv->pdev->dev,
1858 "Failed to allocate free queue for IB MBOX%d\n", mbox);
1859 rc = -ENOMEM;
1860 goto out_buf;
1861 }
1862
1863 /* Allocate memory for Inbound message descriptors */
1864 priv->imsg_ring[mbox].imd_base =
1865 dma_alloc_coherent(&priv->pdev->dev,
1866 entries * sizeof(struct tsi721_imsg_desc),
1867 &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
1868
1869 if (priv->imsg_ring[mbox].imd_base == NULL) {
1870 dev_err(&priv->pdev->dev,
1871 "Failed to allocate descriptor memory for IB MBOX%d\n",
1872 mbox);
1873 rc = -ENOMEM;
1874 goto out_dma;
1875 }
1876
1877 /* Fill free buffer pointer list */
1878 free_ptr = priv->imsg_ring[mbox].imfq_base;
1879 for (i = 0; i < entries; i++)
1880 free_ptr[i] = cpu_to_le64(
1881 (u64)(priv->imsg_ring[mbox].buf_phys) +
1882 i * 0x1000);
1883
1884 mb();
1885
1886 /*
1887 * For mapping of inbound SRIO Messages into appropriate queues we need
1888 * to set Inbound Device ID register in the messaging engine. We do it
1889 * once when first inbound mailbox is requested.
1890 */
1891 if (!(priv->flags & TSI721_IMSGID_SET)) {
1892 iowrite32((u32)priv->mport->host_deviceid,
1893 priv->regs + TSI721_IB_DEVID);
1894 priv->flags |= TSI721_IMSGID_SET;
1895 }
1896
1897 /*
1898 * Configure Inbound Messaging channel (ch = mbox + 4)
1899 */
1900
1901 /* Setup Inbound Message free queue */
1902 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
1903 priv->regs + TSI721_IBDMAC_FQBH(ch));
1904 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
1905 TSI721_IBDMAC_FQBL_MASK),
1906 priv->regs+TSI721_IBDMAC_FQBL(ch));
1907 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
1908 priv->regs + TSI721_IBDMAC_FQSZ(ch));
1909
1910 /* Setup Inbound Message descriptor queue */
1911 iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
1912 priv->regs + TSI721_IBDMAC_DQBH(ch));
1913 iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
1914 (u32)TSI721_IBDMAC_DQBL_MASK),
1915 priv->regs+TSI721_IBDMAC_DQBL(ch));
1916 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
1917 priv->regs + TSI721_IBDMAC_DQSZ(ch));
1918
1919 /* Enable interrupts */
1920
1921#ifdef CONFIG_PCI_MSI
1922 if (priv->flags & TSI721_USING_MSIX) {
1923 /* Request interrupt service if we are in MSI-X mode */
1924 rc = request_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
1925 tsi721_imsg_msix, 0,
1926 priv->msix[TSI721_VECT_IMB0_RCV + mbox].irq_name,
1927 (void *)mport);
1928
1929 if (rc) {
1930 dev_dbg(&priv->pdev->dev,
1931 "Unable to allocate MSI-X interrupt for "
1932 "IBOX%d-DONE\n", mbox);
1933 goto out_desc;
1934 }
1935
1936 rc = request_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
1937 tsi721_imsg_msix, 0,
1938 priv->msix[TSI721_VECT_IMB0_INT + mbox].irq_name,
1939 (void *)mport);
1940
1941 if (rc) {
1942 dev_dbg(&priv->pdev->dev,
1943 "Unable to allocate MSI-X interrupt for "
1944 "IBOX%d-INT\n", mbox);
1945 free_irq(
1946 priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
1947 (void *)mport);
1948 goto out_desc;
1949 }
1950 }
1951#endif /* CONFIG_PCI_MSI */
1952
1953 tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
1954
1955 /* Initialize Inbound Message Engine */
1956 iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
1957 ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
1958 udelay(10);
1959 priv->imsg_ring[mbox].fq_wrptr = entries - 1;
1960 iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
1961
1962 priv->imsg_init[mbox] = 1;
1963 return 0;
1964
1965#ifdef CONFIG_PCI_MSI
1966out_desc:
1967 dma_free_coherent(&priv->pdev->dev,
1968 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
1969 priv->imsg_ring[mbox].imd_base,
1970 priv->imsg_ring[mbox].imd_phys);
1971
1972 priv->imsg_ring[mbox].imd_base = NULL;
1973#endif /* CONFIG_PCI_MSI */
1974
1975out_dma:
1976 dma_free_coherent(&priv->pdev->dev,
1977 priv->imsg_ring[mbox].size * 8,
1978 priv->imsg_ring[mbox].imfq_base,
1979 priv->imsg_ring[mbox].imfq_phys);
1980
1981 priv->imsg_ring[mbox].imfq_base = NULL;
1982
1983out_buf:
1984 dma_free_coherent(&priv->pdev->dev,
1985 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
1986 priv->imsg_ring[mbox].buf_base,
1987 priv->imsg_ring[mbox].buf_phys);
1988
1989 priv->imsg_ring[mbox].buf_base = NULL;
1990
1991out:
1992 return rc;
1993}
1994
1995/**
1996 * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
1997 * @mport: Master port implementing the Inbound Messaging Engine
1998 * @mbox: Mailbox to close
1999 */
2000static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
2001{
2002 struct tsi721_device *priv = mport->priv;
2003 u32 rx_slot;
2004 int ch = mbox + 4;
2005
2006 if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
2007 return;
2008 priv->imsg_init[mbox] = 0;
2009
2010 /* Disable Inbound Messaging Engine */
2011
2012 /* Disable Interrupts */
2013 tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
2014
2015#ifdef CONFIG_PCI_MSI
2016 if (priv->flags & TSI721_USING_MSIX) {
2017 free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
2018 (void *)mport);
2019 free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
2020 (void *)mport);
2021 }
2022#endif /* CONFIG_PCI_MSI */
2023
2024 /* Clear Inbound Buffer Queue */
2025 for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
2026 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2027
2028 /* Free memory allocated for message buffers */
2029 dma_free_coherent(&priv->pdev->dev,
2030 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
2031 priv->imsg_ring[mbox].buf_base,
2032 priv->imsg_ring[mbox].buf_phys);
2033
2034 priv->imsg_ring[mbox].buf_base = NULL;
2035
2036 /* Free memory allocated for free pointr list */
2037 dma_free_coherent(&priv->pdev->dev,
2038 priv->imsg_ring[mbox].size * 8,
2039 priv->imsg_ring[mbox].imfq_base,
2040 priv->imsg_ring[mbox].imfq_phys);
2041
2042 priv->imsg_ring[mbox].imfq_base = NULL;
2043
2044 /* Free memory allocated for RX descriptors */
2045 dma_free_coherent(&priv->pdev->dev,
2046 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
2047 priv->imsg_ring[mbox].imd_base,
2048 priv->imsg_ring[mbox].imd_phys);
2049
2050 priv->imsg_ring[mbox].imd_base = NULL;
2051}
2052
2053/**
2054 * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
2055 * @mport: Master port implementing the Inbound Messaging Engine
2056 * @mbox: Inbound mailbox number
2057 * @buf: Buffer to add to inbound queue
2058 */
2059static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
2060{
2061 struct tsi721_device *priv = mport->priv;
2062 u32 rx_slot;
2063 int rc = 0;
2064
2065 rx_slot = priv->imsg_ring[mbox].rx_slot;
2066 if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
2067 dev_err(&priv->pdev->dev,
2068 "Error adding inbound buffer %d, buffer exists\n",
2069 rx_slot);
2070 rc = -EINVAL;
2071 goto out;
2072 }
2073
2074 priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
2075
2076 if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
2077 priv->imsg_ring[mbox].rx_slot = 0;
2078
2079out:
2080 return rc;
2081}
2082
2083/**
2084 * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
2085 * @mport: Master port implementing the Inbound Messaging Engine
2086 * @mbox: Inbound mailbox number
2087 *
2088 * Returns pointer to the message on success or NULL on failure.
2089 */
2090static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
2091{
2092 struct tsi721_device *priv = mport->priv;
2093 struct tsi721_imsg_desc *desc;
2094 u32 rx_slot;
2095 void *rx_virt = NULL;
2096 u64 rx_phys;
2097 void *buf = NULL;
2098 u64 *free_ptr;
2099 int ch = mbox + 4;
2100 int msg_size;
2101
2102 if (!priv->imsg_init[mbox])
2103 return NULL;
2104
2105 desc = priv->imsg_ring[mbox].imd_base;
2106 desc += priv->imsg_ring[mbox].desc_rdptr;
2107
2108 if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
2109 goto out;
2110
2111 rx_slot = priv->imsg_ring[mbox].rx_slot;
2112 while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
2113 if (++rx_slot == priv->imsg_ring[mbox].size)
2114 rx_slot = 0;
2115 }
2116
2117 rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
2118 le32_to_cpu(desc->bufptr_lo);
2119
2120 rx_virt = priv->imsg_ring[mbox].buf_base +
2121 (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
2122
2123 buf = priv->imsg_ring[mbox].imq_base[rx_slot];
2124 msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
2125 if (msg_size == 0)
2126 msg_size = RIO_MAX_MSG_SIZE;
2127
2128 memcpy(buf, rx_virt, msg_size);
2129 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
2130
2131 desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
2132 if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
2133 priv->imsg_ring[mbox].desc_rdptr = 0;
2134
2135 iowrite32(priv->imsg_ring[mbox].desc_rdptr,
2136 priv->regs + TSI721_IBDMAC_DQRP(ch));
2137
2138 /* Return free buffer into the pointer list */
2139 free_ptr = priv->imsg_ring[mbox].imfq_base;
2140 free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
2141
2142 if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
2143 priv->imsg_ring[mbox].fq_wrptr = 0;
2144
2145 iowrite32(priv->imsg_ring[mbox].fq_wrptr,
2146 priv->regs + TSI721_IBDMAC_FQWP(ch));
2147out:
2148 return buf;
2149}
2150
2151/**
2152 * tsi721_messages_init - Initialization of Messaging Engine
2153 * @priv: pointer to tsi721 private data
2154 *
2155 * Configures Tsi721 messaging engine.
2156 */
2157static int tsi721_messages_init(struct tsi721_device *priv)
2158{
2159 int ch;
2160
2161 iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
2162 iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
2163 iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
2164
2165 /* Set SRIO Message Request/Response Timeout */
2166 iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
2167
2168 /* Initialize Inbound Messaging Engine Registers */
2169 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
2170 /* Clear interrupt bits */
2171 iowrite32(TSI721_IBDMAC_INT_MASK,
2172 priv->regs + TSI721_IBDMAC_INT(ch));
2173 /* Clear Status */
2174 iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
2175
2176 iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
2177 priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
2178 iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
2179 priv->regs + TSI721_SMSG_ECC_NCOR(ch));
2180 }
2181
2182 return 0;
2183}
2184
2185/**
2186 * tsi721_disable_ints - disables all device interrupts
2187 * @priv: pointer to tsi721 private data
2188 */
2189static void tsi721_disable_ints(struct tsi721_device *priv)
2190{
2191 int ch;
2192
2193 /* Disable all device level interrupts */
2194 iowrite32(0, priv->regs + TSI721_DEV_INTE);
2195
2196 /* Disable all Device Channel interrupts */
2197 iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
2198
2199 /* Disable all Inbound Msg Channel interrupts */
2200 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
2201 iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
2202
2203 /* Disable all Outbound Msg Channel interrupts */
2204 for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
2205 iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
2206
2207 /* Disable all general messaging interrupts */
2208 iowrite32(0, priv->regs + TSI721_SMSG_INTE);
2209
2210 /* Disable all BDMA Channel interrupts */
2211 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002212 iowrite32(0,
2213 priv->regs + TSI721_DMAC_BASE(ch) + TSI721_DMAC_INTE);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002214
2215 /* Disable all general BDMA interrupts */
2216 iowrite32(0, priv->regs + TSI721_BDMA_INTE);
2217
2218 /* Disable all SRIO Channel interrupts */
2219 for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
2220 iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
2221
2222 /* Disable all general SR2PC interrupts */
2223 iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
2224
2225 /* Disable all PC2SR interrupts */
2226 iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
2227
2228 /* Disable all I2C interrupts */
2229 iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
2230
2231 /* Disable SRIO MAC interrupts */
2232 iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
2233 iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
2234}
2235
2236/**
2237 * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
2238 * @priv: pointer to tsi721 private data
2239 *
2240 * Configures Tsi721 as RapidIO master port.
2241 */
Bill Pemberton305c8912012-11-19 13:23:25 -05002242static int tsi721_setup_mport(struct tsi721_device *priv)
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002243{
2244 struct pci_dev *pdev = priv->pdev;
2245 int err = 0;
2246 struct rio_ops *ops;
2247
2248 struct rio_mport *mport;
2249
2250 ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
2251 if (!ops) {
2252 dev_dbg(&pdev->dev, "Unable to allocate memory for rio_ops\n");
2253 return -ENOMEM;
2254 }
2255
2256 ops->lcread = tsi721_lcread;
2257 ops->lcwrite = tsi721_lcwrite;
2258 ops->cread = tsi721_cread_dma;
2259 ops->cwrite = tsi721_cwrite_dma;
2260 ops->dsend = tsi721_dsend;
2261 ops->open_inb_mbox = tsi721_open_inb_mbox;
2262 ops->close_inb_mbox = tsi721_close_inb_mbox;
2263 ops->open_outb_mbox = tsi721_open_outb_mbox;
2264 ops->close_outb_mbox = tsi721_close_outb_mbox;
2265 ops->add_outb_message = tsi721_add_outb_message;
2266 ops->add_inb_buffer = tsi721_add_inb_buffer;
2267 ops->get_inb_message = tsi721_get_inb_message;
Alexandre Bounine71afe342012-10-04 17:16:00 -07002268 ops->map_inb = tsi721_rio_map_inb_mem;
2269 ops->unmap_inb = tsi721_rio_unmap_inb_mem;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002270
2271 mport = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
2272 if (!mport) {
2273 kfree(ops);
2274 dev_dbg(&pdev->dev, "Unable to allocate memory for mport\n");
2275 return -ENOMEM;
2276 }
2277
2278 mport->ops = ops;
2279 mport->index = 0;
2280 mport->sys_size = 0; /* small system */
2281 mport->phy_type = RIO_PHY_SERIAL;
2282 mport->priv = (void *)priv;
2283 mport->phys_efptr = 0x100;
Alexandre Bounine2aaf3082014-04-07 15:38:56 -07002284 mport->dev.parent = &pdev->dev;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002285 priv->mport = mport;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002286
2287 INIT_LIST_HEAD(&mport->dbells);
2288
2289 rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
Alexandre Bounineb439e662011-12-08 14:34:36 -08002290 rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
2291 rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
Alexandre Bounineed43f442012-10-04 17:15:51 -07002292 snprintf(mport->name, RIO_MAX_MPORT_NAME, "%s(%s)",
2293 dev_driver_string(&pdev->dev), dev_name(&pdev->dev));
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002294
2295 /* Hook up interrupt handler */
2296
2297#ifdef CONFIG_PCI_MSI
2298 if (!tsi721_enable_msix(priv))
2299 priv->flags |= TSI721_USING_MSIX;
2300 else if (!pci_enable_msi(pdev))
2301 priv->flags |= TSI721_USING_MSI;
2302 else
2303 dev_info(&pdev->dev,
2304 "MSI/MSI-X is not available. Using legacy INTx.\n");
2305#endif /* CONFIG_PCI_MSI */
2306
2307 err = tsi721_request_irq(mport);
2308
2309 if (!err) {
2310 tsi721_interrupts_init(priv);
2311 ops->pwenable = tsi721_pw_enable;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002312 } else {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002313 dev_err(&pdev->dev, "Unable to get assigned PCI IRQ "
2314 "vector %02X err=0x%x\n", pdev->irq, err);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002315 goto err_exit;
2316 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002317
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002318#ifdef CONFIG_RAPIDIO_DMA_ENGINE
2319 tsi721_register_dma(priv);
2320#endif
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002321 /* Enable SRIO link */
2322 iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
2323 TSI721_DEVCTL_SRBOOT_CMPL,
2324 priv->regs + TSI721_DEVCTL);
2325
2326 rio_register_mport(mport);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002327
2328 if (mport->host_deviceid >= 0)
2329 iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
2330 RIO_PORT_GEN_DISCOVERED,
2331 priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2332 else
2333 iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2334
2335 return 0;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002336
2337err_exit:
2338 kfree(mport);
2339 kfree(ops);
2340 return err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002341}
2342
Bill Pemberton305c8912012-11-19 13:23:25 -05002343static int tsi721_probe(struct pci_dev *pdev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002344 const struct pci_device_id *id)
2345{
2346 struct tsi721_device *priv;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002347 int err;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002348
2349 priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
2350 if (priv == NULL) {
2351 dev_err(&pdev->dev, "Failed to allocate memory for device\n");
2352 err = -ENOMEM;
2353 goto err_exit;
2354 }
2355
2356 err = pci_enable_device(pdev);
2357 if (err) {
2358 dev_err(&pdev->dev, "Failed to enable PCI device\n");
2359 goto err_clean;
2360 }
2361
2362 priv->pdev = pdev;
2363
2364#ifdef DEBUG
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002365 {
2366 int i;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002367 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
2368 dev_dbg(&pdev->dev, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
2369 i, (unsigned long long)pci_resource_start(pdev, i),
2370 (unsigned long)pci_resource_len(pdev, i),
2371 pci_resource_flags(pdev, i));
2372 }
Alexandre Bounine9a9a9a72012-08-21 16:16:12 -07002373 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002374#endif
2375 /*
2376 * Verify BAR configuration
2377 */
2378
2379 /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
2380 if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
2381 pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
2382 pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
2383 dev_err(&pdev->dev,
2384 "Missing or misconfigured CSR BAR0, aborting.\n");
2385 err = -ENODEV;
2386 goto err_disable_pdev;
2387 }
2388
2389 /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
2390 if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
2391 pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
2392 pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
2393 dev_err(&pdev->dev,
2394 "Missing or misconfigured Doorbell BAR1, aborting.\n");
2395 err = -ENODEV;
2396 goto err_disable_pdev;
2397 }
2398
2399 /*
2400 * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
2401 * space.
2402 * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
2403 * It may be a good idea to keep them disabled using HW configuration
2404 * to save PCI memory space.
2405 */
2406 if ((pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM) &&
2407 (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64)) {
2408 dev_info(&pdev->dev, "Outbound BAR2 is not used but enabled.\n");
2409 }
2410
2411 if ((pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM) &&
2412 (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64)) {
2413 dev_info(&pdev->dev, "Outbound BAR4 is not used but enabled.\n");
2414 }
2415
2416 err = pci_request_regions(pdev, DRV_NAME);
2417 if (err) {
2418 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
2419 "aborting.\n");
2420 goto err_disable_pdev;
2421 }
2422
2423 pci_set_master(pdev);
2424
2425 priv->regs = pci_ioremap_bar(pdev, BAR_0);
2426 if (!priv->regs) {
2427 dev_err(&pdev->dev,
2428 "Unable to map device registers space, aborting\n");
2429 err = -ENOMEM;
2430 goto err_free_res;
2431 }
2432
2433 priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
2434 if (!priv->odb_base) {
2435 dev_err(&pdev->dev,
2436 "Unable to map outbound doorbells space, aborting\n");
2437 err = -ENOMEM;
2438 goto err_unmap_bars;
2439 }
2440
2441 /* Configure DMA attributes. */
2442 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Peter Senna Tschudin18f62872012-10-04 17:15:55 -07002443 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2444 if (err) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002445 dev_info(&pdev->dev, "Unable to set DMA mask\n");
2446 goto err_unmap_bars;
2447 }
2448
2449 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2450 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2451 } else {
2452 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2453 if (err)
2454 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2455 }
2456
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002457 BUG_ON(!pci_is_pcie(pdev));
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002458
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002459 /* Clear "no snoop" and "relaxed ordering" bits. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002460 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
Alexandre Bounine174f1a72016-03-22 14:25:48 -07002461 PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN, 0);
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002462
2463 /* Adjust PCIe completion timeout. */
Jiang Liu5cdaaf82012-07-24 17:20:31 +08002464 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL2, 0xf, 0x2);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002465
2466 /*
2467 * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
2468 */
2469 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
2470 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
2471 TSI721_MSIXTBL_OFFSET);
2472 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
2473 TSI721_MSIXPBA_OFFSET);
2474 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
2475 /* End of FIXUP */
2476
2477 tsi721_disable_ints(priv);
2478
2479 tsi721_init_pc2sr_mapping(priv);
2480 tsi721_init_sr2pc_mapping(priv);
2481
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002482 if (tsi721_bdma_maint_init(priv)) {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002483 dev_err(&pdev->dev, "BDMA initialization failed, aborting\n");
2484 err = -ENOMEM;
2485 goto err_unmap_bars;
2486 }
2487
2488 err = tsi721_doorbell_init(priv);
2489 if (err)
2490 goto err_free_bdma;
2491
2492 tsi721_port_write_init(priv);
2493
2494 err = tsi721_messages_init(priv);
2495 if (err)
2496 goto err_free_consistent;
2497
2498 err = tsi721_setup_mport(priv);
2499 if (err)
2500 goto err_free_consistent;
2501
2502 return 0;
2503
2504err_free_consistent:
2505 tsi721_doorbell_free(priv);
2506err_free_bdma:
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07002507 tsi721_bdma_maint_free(priv);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002508err_unmap_bars:
2509 if (priv->regs)
2510 iounmap(priv->regs);
2511 if (priv->odb_base)
2512 iounmap(priv->odb_base);
2513err_free_res:
2514 pci_release_regions(pdev);
2515 pci_clear_master(pdev);
2516err_disable_pdev:
2517 pci_disable_device(pdev);
2518err_clean:
2519 kfree(priv);
2520err_exit:
2521 return err;
2522}
2523
Benoit Taine9baa3c32014-08-08 15:56:03 +02002524static const struct pci_device_id tsi721_pci_tbl[] = {
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002525 { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
2526 { 0, } /* terminate list */
2527};
2528
2529MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
2530
2531static struct pci_driver tsi721_driver = {
2532 .name = "tsi721",
2533 .id_table = tsi721_pci_tbl,
2534 .probe = tsi721_probe,
2535};
2536
2537static int __init tsi721_init(void)
2538{
2539 return pci_register_driver(&tsi721_driver);
2540}
2541
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002542device_initcall(tsi721_init);
Alexandre Bounine94d9bd42013-07-03 15:08:55 -07002543
2544MODULE_DESCRIPTION("IDT Tsi721 PCIExpress-to-SRIO bridge driver");
2545MODULE_AUTHOR("Integrated Device Technology, Inc.");
2546MODULE_LICENSE("GPL");