blob: 33471e11a73834b2d87a00fcce7437ff5489dc58 [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{
111 struct tsi721_dma_desc *bd_ptr;
112 u32 rd_count, swr_ptr, ch_stat;
113 int i, err = 0;
114 u32 op = do_wr ? MAINT_WR : MAINT_RD;
115
116 if (offset > (RIO_MAINT_SPACE_SZ - len) || (len != sizeof(u32)))
117 return -EINVAL;
118
119 bd_ptr = priv->bdma[TSI721_DMACH_MAINT].bd_base;
120
121 rd_count = ioread32(
122 priv->regs + TSI721_DMAC_DRDCNT(TSI721_DMACH_MAINT));
123
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 */
137 iowrite32(rd_count + 2,
138 priv->regs + TSI721_DMAC_DWRCNT(TSI721_DMACH_MAINT));
139 ioread32(priv->regs + TSI721_DMAC_DWRCNT(TSI721_DMACH_MAINT));
140 i = 0;
141
142 /* Wait until DMA transfer is finished */
143 while ((ch_stat = ioread32(priv->regs +
144 TSI721_DMAC_STS(TSI721_DMACH_MAINT))) & TSI721_DMAC_STS_RUN) {
145 udelay(1);
146 if (++i >= 5000000) {
147 dev_dbg(&priv->pdev->dev,
148 "%s : DMA[%d] read timeout ch_status=%x\n",
149 __func__, TSI721_DMACH_MAINT, ch_stat);
150 if (!do_wr)
151 *data = 0xffffffff;
152 err = -EIO;
153 goto err_out;
154 }
155 }
156
157 if (ch_stat & TSI721_DMAC_STS_ABORT) {
158 /* If DMA operation aborted due to error,
159 * reinitialize DMA channel
160 */
161 dev_dbg(&priv->pdev->dev, "%s : DMA ABORT ch_stat=%x\n",
162 __func__, ch_stat);
163 dev_dbg(&priv->pdev->dev, "OP=%d : destid=%x hc=%x off=%x\n",
164 do_wr ? MAINT_WR : MAINT_RD, destid, hopcount, offset);
165 iowrite32(TSI721_DMAC_INT_ALL,
166 priv->regs + TSI721_DMAC_INT(TSI721_DMACH_MAINT));
167 iowrite32(TSI721_DMAC_CTL_INIT,
168 priv->regs + TSI721_DMAC_CTL(TSI721_DMACH_MAINT));
169 udelay(10);
170 iowrite32(0, priv->regs +
171 TSI721_DMAC_DWRCNT(TSI721_DMACH_MAINT));
172 udelay(1);
173 if (!do_wr)
174 *data = 0xffffffff;
175 err = -EIO;
176 goto err_out;
177 }
178
179 if (!do_wr)
180 *data = be32_to_cpu(bd_ptr[0].data[0]);
181
182 /*
183 * Update descriptor status FIFO RD pointer.
184 * NOTE: Skipping check and clear FIFO entries because we are waiting
185 * for transfer to be completed.
186 */
187 swr_ptr = ioread32(priv->regs + TSI721_DMAC_DSWP(TSI721_DMACH_MAINT));
188 iowrite32(swr_ptr, priv->regs + TSI721_DMAC_DSRP(TSI721_DMACH_MAINT));
189err_out:
190
191 return err;
192}
193
194/**
195 * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
196 * using Tsi721 BDMA engine.
197 * @mport: RapidIO master port control structure
198 * @index: ID of RapdiIO interface
199 * @destid: Destination ID of transaction
200 * @hopcount: Number of hops to target device
201 * @offset: Offset into configuration space
202 * @len: Length (in bytes) of the maintenance transaction
203 * @val: Location to be read into
204 *
205 * Generates a RapidIO maintenance read transaction.
206 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
207 */
208static int tsi721_cread_dma(struct rio_mport *mport, int index, u16 destid,
209 u8 hopcount, u32 offset, int len, u32 *data)
210{
211 struct tsi721_device *priv = mport->priv;
212
213 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
214 offset, len, data, 0);
215}
216
217/**
218 * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
219 * using Tsi721 BDMA engine
220 * @mport: RapidIO master port control structure
221 * @index: ID of RapdiIO interface
222 * @destid: Destination ID of transaction
223 * @hopcount: Number of hops to target device
224 * @offset: Offset into configuration space
225 * @len: Length (in bytes) of the maintenance transaction
226 * @val: Value to be written
227 *
228 * Generates a RapidIO maintenance write transaction.
229 * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
230 */
231static int tsi721_cwrite_dma(struct rio_mport *mport, int index, u16 destid,
232 u8 hopcount, u32 offset, int len, u32 data)
233{
234 struct tsi721_device *priv = mport->priv;
235 u32 temp = data;
236
237 return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
238 offset, len, &temp, 1);
239}
240
241/**
242 * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
243 * @mport: RapidIO master port structure
244 *
245 * Handles inbound port-write interrupts. Copies PW message from an internal
246 * buffer into PW message FIFO and schedules deferred routine to process
247 * queued messages.
248 */
249static int
250tsi721_pw_handler(struct rio_mport *mport)
251{
252 struct tsi721_device *priv = mport->priv;
253 u32 pw_stat;
254 u32 pw_buf[TSI721_RIO_PW_MSG_SIZE/sizeof(u32)];
255
256
257 pw_stat = ioread32(priv->regs + TSI721_RIO_PW_RX_STAT);
258
259 if (pw_stat & TSI721_RIO_PW_RX_STAT_PW_VAL) {
260 pw_buf[0] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(0));
261 pw_buf[1] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(1));
262 pw_buf[2] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(2));
263 pw_buf[3] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(3));
264
265 /* Queue PW message (if there is room in FIFO),
266 * otherwise discard it.
267 */
268 spin_lock(&priv->pw_fifo_lock);
269 if (kfifo_avail(&priv->pw_fifo) >= TSI721_RIO_PW_MSG_SIZE)
270 kfifo_in(&priv->pw_fifo, pw_buf,
271 TSI721_RIO_PW_MSG_SIZE);
272 else
273 priv->pw_discard_count++;
274 spin_unlock(&priv->pw_fifo_lock);
275 }
276
277 /* Clear pending PW interrupts */
278 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
279 priv->regs + TSI721_RIO_PW_RX_STAT);
280
281 schedule_work(&priv->pw_work);
282
283 return 0;
284}
285
286static void tsi721_pw_dpc(struct work_struct *work)
287{
288 struct tsi721_device *priv = container_of(work, struct tsi721_device,
289 pw_work);
290 u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)]; /* Use full size PW message
291 buffer for RIO layer */
292
293 /*
294 * Process port-write messages
295 */
296 while (kfifo_out_spinlocked(&priv->pw_fifo, (unsigned char *)msg_buffer,
297 TSI721_RIO_PW_MSG_SIZE, &priv->pw_fifo_lock)) {
298 /* Process one message */
299#ifdef DEBUG_PW
300 {
301 u32 i;
302 pr_debug("%s : Port-Write Message:", __func__);
303 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); ) {
304 pr_debug("0x%02x: %08x %08x %08x %08x", i*4,
305 msg_buffer[i], msg_buffer[i + 1],
306 msg_buffer[i + 2], msg_buffer[i + 3]);
307 i += 4;
308 }
309 pr_debug("\n");
310 }
311#endif
312 /* Pass the port-write message to RIO core for processing */
313 rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
314 }
315}
316
317/**
318 * tsi721_pw_enable - enable/disable port-write interface init
319 * @mport: Master port implementing the port write unit
320 * @enable: 1=enable; 0=disable port-write message handling
321 */
322static int tsi721_pw_enable(struct rio_mport *mport, int enable)
323{
324 struct tsi721_device *priv = mport->priv;
325 u32 rval;
326
327 rval = ioread32(priv->regs + TSI721_RIO_EM_INT_ENABLE);
328
329 if (enable)
330 rval |= TSI721_RIO_EM_INT_ENABLE_PW_RX;
331 else
332 rval &= ~TSI721_RIO_EM_INT_ENABLE_PW_RX;
333
334 /* Clear pending PW interrupts */
335 iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
336 priv->regs + TSI721_RIO_PW_RX_STAT);
337 /* Update enable bits */
338 iowrite32(rval, priv->regs + TSI721_RIO_EM_INT_ENABLE);
339
340 return 0;
341}
342
343/**
344 * tsi721_dsend - Send a RapidIO doorbell
345 * @mport: RapidIO master port info
346 * @index: ID of RapidIO interface
347 * @destid: Destination ID of target device
348 * @data: 16-bit info field of RapidIO doorbell
349 *
350 * Sends a RapidIO doorbell message. Always returns %0.
351 */
352static int tsi721_dsend(struct rio_mport *mport, int index,
353 u16 destid, u16 data)
354{
355 struct tsi721_device *priv = mport->priv;
356 u32 offset;
357
358 offset = (((mport->sys_size) ? RIO_TT_CODE_16 : RIO_TT_CODE_8) << 18) |
359 (destid << 2);
360
361 dev_dbg(&priv->pdev->dev,
362 "Send Doorbell 0x%04x to destID 0x%x\n", data, destid);
363 iowrite16be(data, priv->odb_base + offset);
364
365 return 0;
366}
367
368/**
369 * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
370 * @mport: RapidIO master port structure
371 *
372 * Handles inbound doorbell interrupts. Copies doorbell entry from an internal
373 * buffer into DB message FIFO and schedules deferred routine to process
374 * queued DBs.
375 */
376static int
377tsi721_dbell_handler(struct rio_mport *mport)
378{
379 struct tsi721_device *priv = mport->priv;
380 u32 regval;
381
382 /* Disable IDB interrupts */
383 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
384 regval &= ~TSI721_SR_CHINT_IDBQRCV;
385 iowrite32(regval,
386 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
387
388 schedule_work(&priv->idb_work);
389
390 return 0;
391}
392
393static void tsi721_db_dpc(struct work_struct *work)
394{
395 struct tsi721_device *priv = container_of(work, struct tsi721_device,
396 idb_work);
397 struct rio_mport *mport;
398 struct rio_dbell *dbell;
399 int found = 0;
400 u32 wr_ptr, rd_ptr;
401 u64 *idb_entry;
402 u32 regval;
403 union {
404 u64 msg;
405 u8 bytes[8];
406 } idb;
407
408 /*
409 * Process queued inbound doorbells
410 */
411 mport = priv->mport;
412
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800413 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
414 rd_ptr = ioread32(priv->regs + TSI721_IDQ_RP(IDB_QUEUE)) % IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700415
416 while (wr_ptr != rd_ptr) {
417 idb_entry = (u64 *)(priv->idb_base +
418 (TSI721_IDB_ENTRY_SIZE * rd_ptr));
419 rd_ptr++;
Alexandre Bounineb24823e2012-03-05 14:59:21 -0800420 rd_ptr %= IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700421 idb.msg = *idb_entry;
422 *idb_entry = 0;
423
424 /* Process one doorbell */
425 list_for_each_entry(dbell, &mport->dbells, node) {
426 if ((dbell->res->start <= DBELL_INF(idb.bytes)) &&
427 (dbell->res->end >= DBELL_INF(idb.bytes))) {
428 found = 1;
429 break;
430 }
431 }
432
433 if (found) {
434 dbell->dinb(mport, dbell->dev_id, DBELL_SID(idb.bytes),
435 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
436 } else {
437 dev_dbg(&priv->pdev->dev,
438 "spurious inb doorbell, sid %2.2x tid %2.2x"
439 " info %4.4x\n", DBELL_SID(idb.bytes),
440 DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
441 }
Alexandre Bounine8d30b9932012-08-21 16:16:11 -0700442
443 wr_ptr = ioread32(priv->regs +
444 TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700445 }
446
447 iowrite32(rd_ptr & (IDB_QSIZE - 1),
448 priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
449
450 /* Re-enable IDB interrupts */
451 regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
452 regval |= TSI721_SR_CHINT_IDBQRCV;
453 iowrite32(regval,
454 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
Alexandre Bounine8d30b9932012-08-21 16:16:11 -0700455
456 wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
457 if (wr_ptr != rd_ptr)
458 schedule_work(&priv->idb_work);
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700459}
460
461/**
462 * tsi721_irqhandler - Tsi721 interrupt handler
463 * @irq: Linux interrupt number
464 * @ptr: Pointer to interrupt-specific data (mport structure)
465 *
466 * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
467 * interrupt events and calls an event-specific handler(s).
468 */
469static irqreturn_t tsi721_irqhandler(int irq, void *ptr)
470{
471 struct rio_mport *mport = (struct rio_mport *)ptr;
472 struct tsi721_device *priv = mport->priv;
473 u32 dev_int;
474 u32 dev_ch_int;
475 u32 intval;
476 u32 ch_inte;
477
478 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
551 return IRQ_HANDLED;
552}
553
554static void tsi721_interrupts_init(struct tsi721_device *priv)
555{
556 u32 intr;
557
558 /* Enable IDB interrupts */
559 iowrite32(TSI721_SR_CHINT_ALL,
560 priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
561 iowrite32(TSI721_SR_CHINT_IDBQRCV,
562 priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
563 iowrite32(TSI721_INT_SR2PC_CHAN(IDB_QUEUE),
564 priv->regs + TSI721_DEV_CHAN_INTE);
565
566 /* Enable SRIO MAC interrupts */
567 iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT,
568 priv->regs + TSI721_RIO_EM_DEV_INT_EN);
569
570 if (priv->flags & TSI721_USING_MSIX)
571 intr = TSI721_DEV_INT_SRIO;
572 else
573 intr = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
574 TSI721_DEV_INT_SMSG_CH;
575
576 iowrite32(intr, priv->regs + TSI721_DEV_INTE);
577 ioread32(priv->regs + TSI721_DEV_INTE);
578}
579
580#ifdef CONFIG_PCI_MSI
581/**
582 * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
583 * @irq: Linux interrupt number
584 * @ptr: Pointer to interrupt-specific data (mport structure)
585 *
586 * Handles outbound messaging interrupts signaled using MSI-X.
587 */
588static irqreturn_t tsi721_omsg_msix(int irq, void *ptr)
589{
590 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
591 int mbox;
592
593 mbox = (irq - priv->msix[TSI721_VECT_OMB0_DONE].vector) % RIO_MAX_MBOX;
594 tsi721_omsg_handler(priv, mbox);
595 return IRQ_HANDLED;
596}
597
598/**
599 * tsi721_imsg_msix - MSI-X interrupt handler for inbound messaging
600 * @irq: Linux interrupt number
601 * @ptr: Pointer to interrupt-specific data (mport structure)
602 *
603 * Handles inbound messaging interrupts signaled using MSI-X.
604 */
605static irqreturn_t tsi721_imsg_msix(int irq, void *ptr)
606{
607 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
608 int mbox;
609
610 mbox = (irq - priv->msix[TSI721_VECT_IMB0_RCV].vector) % RIO_MAX_MBOX;
611 tsi721_imsg_handler(priv, mbox + 4);
612 return IRQ_HANDLED;
613}
614
615/**
616 * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
617 * @irq: Linux interrupt number
618 * @ptr: Pointer to interrupt-specific data (mport structure)
619 *
620 * Handles Tsi721 interrupts from SRIO MAC.
621 */
622static irqreturn_t tsi721_srio_msix(int irq, void *ptr)
623{
624 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
625 u32 srio_int;
626
627 /* Service SRIO MAC interrupts */
628 srio_int = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
629 if (srio_int & TSI721_RIO_EM_INT_STAT_PW_RX)
630 tsi721_pw_handler((struct rio_mport *)ptr);
631
632 return IRQ_HANDLED;
633}
634
635/**
636 * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
637 * @irq: Linux interrupt number
638 * @ptr: Pointer to interrupt-specific data (mport structure)
639 *
640 * Handles Tsi721 interrupts from SR2PC Channel.
641 * NOTE: At this moment services only one SR2PC channel associated with inbound
642 * doorbells.
643 */
644static irqreturn_t tsi721_sr2pc_ch_msix(int irq, void *ptr)
645{
646 struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
647 u32 sr_ch_int;
648
649 /* Service Inbound DB interrupt from SR2PC channel */
650 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
651 if (sr_ch_int & TSI721_SR_CHINT_IDBQRCV)
652 tsi721_dbell_handler((struct rio_mport *)ptr);
653
654 /* Clear interrupts */
655 iowrite32(sr_ch_int, priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
656 /* Read back to ensure that interrupt was cleared */
657 sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
658
659 return IRQ_HANDLED;
660}
661
662/**
663 * tsi721_request_msix - register interrupt service for MSI-X mode.
664 * @mport: RapidIO master port structure
665 *
666 * Registers MSI-X interrupt service routines for interrupts that are active
667 * immediately after mport initialization. Messaging interrupt service routines
668 * should be registered during corresponding open requests.
669 */
670static int tsi721_request_msix(struct rio_mport *mport)
671{
672 struct tsi721_device *priv = mport->priv;
673 int err = 0;
674
675 err = request_irq(priv->msix[TSI721_VECT_IDB].vector,
676 tsi721_sr2pc_ch_msix, 0,
677 priv->msix[TSI721_VECT_IDB].irq_name, (void *)mport);
678 if (err)
679 goto out;
680
681 err = request_irq(priv->msix[TSI721_VECT_PWRX].vector,
682 tsi721_srio_msix, 0,
683 priv->msix[TSI721_VECT_PWRX].irq_name, (void *)mport);
684 if (err)
685 free_irq(
686 priv->msix[TSI721_VECT_IDB].vector,
687 (void *)mport);
688out:
689 return err;
690}
691
692/**
693 * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
694 * @priv: pointer to tsi721 private data
695 *
696 * Configures MSI-X support for Tsi721. Supports only an exact number
697 * of requested vectors.
698 */
699static int tsi721_enable_msix(struct tsi721_device *priv)
700{
701 struct msix_entry entries[TSI721_VECT_MAX];
702 int err;
703 int i;
704
705 entries[TSI721_VECT_IDB].entry = TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE);
706 entries[TSI721_VECT_PWRX].entry = TSI721_MSIX_SRIO_MAC_INT;
707
708 /*
709 * Initialize MSI-X entries for Messaging Engine:
710 * this driver supports four RIO mailboxes (inbound and outbound)
711 * NOTE: Inbound message MBOX 0...4 use IB channels 4...7. Therefore
712 * offset +4 is added to IB MBOX number.
713 */
714 for (i = 0; i < RIO_MAX_MBOX; i++) {
715 entries[TSI721_VECT_IMB0_RCV + i].entry =
716 TSI721_MSIX_IMSG_DQ_RCV(i + 4);
717 entries[TSI721_VECT_IMB0_INT + i].entry =
718 TSI721_MSIX_IMSG_INT(i + 4);
719 entries[TSI721_VECT_OMB0_DONE + i].entry =
720 TSI721_MSIX_OMSG_DONE(i);
721 entries[TSI721_VECT_OMB0_INT + i].entry =
722 TSI721_MSIX_OMSG_INT(i);
723 }
724
725 err = pci_enable_msix(priv->pdev, entries, ARRAY_SIZE(entries));
726 if (err) {
727 if (err > 0)
728 dev_info(&priv->pdev->dev,
729 "Only %d MSI-X vectors available, "
730 "not using MSI-X\n", err);
731 return err;
732 }
733
734 /*
735 * Copy MSI-X vector information into tsi721 private structure
736 */
737 priv->msix[TSI721_VECT_IDB].vector = entries[TSI721_VECT_IDB].vector;
738 snprintf(priv->msix[TSI721_VECT_IDB].irq_name, IRQ_DEVICE_NAME_MAX,
739 DRV_NAME "-idb@pci:%s", pci_name(priv->pdev));
740 priv->msix[TSI721_VECT_PWRX].vector = entries[TSI721_VECT_PWRX].vector;
741 snprintf(priv->msix[TSI721_VECT_PWRX].irq_name, IRQ_DEVICE_NAME_MAX,
742 DRV_NAME "-pwrx@pci:%s", pci_name(priv->pdev));
743
744 for (i = 0; i < RIO_MAX_MBOX; i++) {
745 priv->msix[TSI721_VECT_IMB0_RCV + i].vector =
746 entries[TSI721_VECT_IMB0_RCV + i].vector;
747 snprintf(priv->msix[TSI721_VECT_IMB0_RCV + i].irq_name,
748 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbr%d@pci:%s",
749 i, pci_name(priv->pdev));
750
751 priv->msix[TSI721_VECT_IMB0_INT + i].vector =
752 entries[TSI721_VECT_IMB0_INT + i].vector;
753 snprintf(priv->msix[TSI721_VECT_IMB0_INT + i].irq_name,
754 IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbi%d@pci:%s",
755 i, pci_name(priv->pdev));
756
757 priv->msix[TSI721_VECT_OMB0_DONE + i].vector =
758 entries[TSI721_VECT_OMB0_DONE + i].vector;
759 snprintf(priv->msix[TSI721_VECT_OMB0_DONE + i].irq_name,
760 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombd%d@pci:%s",
761 i, pci_name(priv->pdev));
762
763 priv->msix[TSI721_VECT_OMB0_INT + i].vector =
764 entries[TSI721_VECT_OMB0_INT + i].vector;
765 snprintf(priv->msix[TSI721_VECT_OMB0_INT + i].irq_name,
766 IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombi%d@pci:%s",
767 i, pci_name(priv->pdev));
768 }
769
770 return 0;
771}
772#endif /* CONFIG_PCI_MSI */
773
774static int tsi721_request_irq(struct rio_mport *mport)
775{
776 struct tsi721_device *priv = mport->priv;
777 int err;
778
779#ifdef CONFIG_PCI_MSI
780 if (priv->flags & TSI721_USING_MSIX)
781 err = tsi721_request_msix(mport);
782 else
783#endif
784 err = request_irq(priv->pdev->irq, tsi721_irqhandler,
785 (priv->flags & TSI721_USING_MSI) ? 0 : IRQF_SHARED,
786 DRV_NAME, (void *)mport);
787
788 if (err)
789 dev_err(&priv->pdev->dev,
790 "Unable to allocate interrupt, Error: %d\n", err);
791
792 return err;
793}
794
795/**
796 * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
797 * translation regions.
798 * @priv: pointer to tsi721 private data
799 *
800 * Disables SREP translation regions.
801 */
802static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
803{
804 int i;
805
806 /* Disable all PC2SR translation windows */
807 for (i = 0; i < TSI721_OBWIN_NUM; i++)
808 iowrite32(0, priv->regs + TSI721_OBWINLB(i));
809}
810
811/**
812 * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
813 * translation regions.
814 * @priv: pointer to tsi721 private data
815 *
816 * Disables inbound windows.
817 */
818static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
819{
820 int i;
821
822 /* Disable all SR2PC inbound windows */
823 for (i = 0; i < TSI721_IBWIN_NUM; i++)
824 iowrite32(0, priv->regs + TSI721_IBWINLB(i));
825}
826
827/**
828 * tsi721_port_write_init - Inbound port write interface init
829 * @priv: pointer to tsi721 private data
830 *
831 * Initializes inbound port write handler.
832 * Returns %0 on success or %-ENOMEM on failure.
833 */
834static int tsi721_port_write_init(struct tsi721_device *priv)
835{
836 priv->pw_discard_count = 0;
837 INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
838 spin_lock_init(&priv->pw_fifo_lock);
839 if (kfifo_alloc(&priv->pw_fifo,
840 TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
841 dev_err(&priv->pdev->dev, "PW FIFO allocation failed\n");
842 return -ENOMEM;
843 }
844
845 /* Use reliable port-write capture mode */
846 iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
847 return 0;
848}
849
850static int tsi721_doorbell_init(struct tsi721_device *priv)
851{
852 /* Outbound Doorbells do not require any setup.
853 * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
854 * That BAR1 was mapped during the probe routine.
855 */
856
857 /* Initialize Inbound Doorbell processing DPC and queue */
858 priv->db_discard_count = 0;
859 INIT_WORK(&priv->idb_work, tsi721_db_dpc);
860
861 /* Allocate buffer for inbound doorbells queue */
Alexandre Bounineceb96392011-12-08 14:34:35 -0800862 priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700863 IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
864 &priv->idb_dma, GFP_KERNEL);
865 if (!priv->idb_base)
866 return -ENOMEM;
867
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700868 dev_dbg(&priv->pdev->dev, "Allocated IDB buffer @ %p (phys = %llx)\n",
869 priv->idb_base, (unsigned long long)priv->idb_dma);
870
871 iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
872 priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
873 iowrite32(((u64)priv->idb_dma >> 32),
874 priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
875 iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
876 priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
877 /* Enable accepting all inbound doorbells */
878 iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
879
880 iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
881
882 iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
883
884 return 0;
885}
886
887static void tsi721_doorbell_free(struct tsi721_device *priv)
888{
889 if (priv->idb_base == NULL)
890 return;
891
892 /* Free buffer allocated for inbound doorbell queue */
893 dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
894 priv->idb_base, priv->idb_dma);
895 priv->idb_base = NULL;
896}
897
898static int tsi721_bdma_ch_init(struct tsi721_device *priv, int chnum)
899{
900 struct tsi721_dma_desc *bd_ptr;
901 u64 *sts_ptr;
902 dma_addr_t bd_phys, sts_phys;
903 int sts_size;
904 int bd_num = priv->bdma[chnum].bd_num;
905
906 dev_dbg(&priv->pdev->dev, "Init Block DMA Engine, CH%d\n", chnum);
907
908 /*
909 * Initialize DMA channel for maintenance requests
910 */
911
912 /* Allocate space for DMA descriptors */
Alexandre Bounineceb96392011-12-08 14:34:35 -0800913 bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700914 bd_num * sizeof(struct tsi721_dma_desc),
915 &bd_phys, GFP_KERNEL);
916 if (!bd_ptr)
917 return -ENOMEM;
918
919 priv->bdma[chnum].bd_phys = bd_phys;
920 priv->bdma[chnum].bd_base = bd_ptr;
921
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700922 dev_dbg(&priv->pdev->dev, "DMA descriptors @ %p (phys = %llx)\n",
923 bd_ptr, (unsigned long long)bd_phys);
924
925 /* Allocate space for descriptor status FIFO */
926 sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
927 bd_num : TSI721_DMA_MINSTSSZ;
928 sts_size = roundup_pow_of_two(sts_size);
Alexandre Bounineceb96392011-12-08 14:34:35 -0800929 sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700930 sts_size * sizeof(struct tsi721_dma_sts),
931 &sts_phys, GFP_KERNEL);
932 if (!sts_ptr) {
933 /* Free space allocated for DMA descriptors */
934 dma_free_coherent(&priv->pdev->dev,
935 bd_num * sizeof(struct tsi721_dma_desc),
936 bd_ptr, bd_phys);
937 priv->bdma[chnum].bd_base = NULL;
938 return -ENOMEM;
939 }
940
941 priv->bdma[chnum].sts_phys = sts_phys;
942 priv->bdma[chnum].sts_base = sts_ptr;
943 priv->bdma[chnum].sts_size = sts_size;
944
Alexandre Bounine48618fb2011-11-02 13:39:09 -0700945 dev_dbg(&priv->pdev->dev,
946 "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
947 sts_ptr, (unsigned long long)sts_phys, sts_size);
948
949 /* Initialize DMA descriptors ring */
950 bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
951 bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
952 TSI721_DMAC_DPTRL_MASK);
953 bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
954
955 /* Setup DMA descriptor pointers */
956 iowrite32(((u64)bd_phys >> 32),
957 priv->regs + TSI721_DMAC_DPTRH(chnum));
958 iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
959 priv->regs + TSI721_DMAC_DPTRL(chnum));
960
961 /* Setup descriptor status FIFO */
962 iowrite32(((u64)sts_phys >> 32),
963 priv->regs + TSI721_DMAC_DSBH(chnum));
964 iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
965 priv->regs + TSI721_DMAC_DSBL(chnum));
966 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
967 priv->regs + TSI721_DMAC_DSSZ(chnum));
968
969 /* Clear interrupt bits */
970 iowrite32(TSI721_DMAC_INT_ALL,
971 priv->regs + TSI721_DMAC_INT(chnum));
972
973 ioread32(priv->regs + TSI721_DMAC_INT(chnum));
974
975 /* Toggle DMA channel initialization */
976 iowrite32(TSI721_DMAC_CTL_INIT, priv->regs + TSI721_DMAC_CTL(chnum));
977 ioread32(priv->regs + TSI721_DMAC_CTL(chnum));
978 udelay(10);
979
980 return 0;
981}
982
983static int tsi721_bdma_ch_free(struct tsi721_device *priv, int chnum)
984{
985 u32 ch_stat;
986
987 if (priv->bdma[chnum].bd_base == NULL)
988 return 0;
989
990 /* Check if DMA channel still running */
991 ch_stat = ioread32(priv->regs + TSI721_DMAC_STS(chnum));
992 if (ch_stat & TSI721_DMAC_STS_RUN)
993 return -EFAULT;
994
995 /* Put DMA channel into init state */
996 iowrite32(TSI721_DMAC_CTL_INIT,
997 priv->regs + TSI721_DMAC_CTL(chnum));
998
999 /* Free space allocated for DMA descriptors */
1000 dma_free_coherent(&priv->pdev->dev,
1001 priv->bdma[chnum].bd_num * sizeof(struct tsi721_dma_desc),
1002 priv->bdma[chnum].bd_base, priv->bdma[chnum].bd_phys);
1003 priv->bdma[chnum].bd_base = NULL;
1004
1005 /* Free space allocated for status FIFO */
1006 dma_free_coherent(&priv->pdev->dev,
1007 priv->bdma[chnum].sts_size * sizeof(struct tsi721_dma_sts),
1008 priv->bdma[chnum].sts_base, priv->bdma[chnum].sts_phys);
1009 priv->bdma[chnum].sts_base = NULL;
1010 return 0;
1011}
1012
1013static int tsi721_bdma_init(struct tsi721_device *priv)
1014{
1015 /* Initialize BDMA channel allocated for RapidIO maintenance read/write
1016 * request generation
1017 */
1018 priv->bdma[TSI721_DMACH_MAINT].bd_num = 2;
1019 if (tsi721_bdma_ch_init(priv, TSI721_DMACH_MAINT)) {
1020 dev_err(&priv->pdev->dev, "Unable to initialize maintenance DMA"
1021 " channel %d, aborting\n", TSI721_DMACH_MAINT);
1022 return -ENOMEM;
1023 }
1024
1025 return 0;
1026}
1027
1028static void tsi721_bdma_free(struct tsi721_device *priv)
1029{
1030 tsi721_bdma_ch_free(priv, TSI721_DMACH_MAINT);
1031}
1032
1033/* Enable Inbound Messaging Interrupts */
1034static void
1035tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
1036 u32 inte_mask)
1037{
1038 u32 rval;
1039
1040 if (!inte_mask)
1041 return;
1042
1043 /* Clear pending Inbound Messaging interrupts */
1044 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1045
1046 /* Enable Inbound Messaging interrupts */
1047 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1048 iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
1049
1050 if (priv->flags & TSI721_USING_MSIX)
1051 return; /* Finished if we are in MSI-X mode */
1052
1053 /*
1054 * For MSI and INTA interrupt signalling we need to enable next levels
1055 */
1056
1057 /* Enable Device Channel Interrupt */
1058 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1059 iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
1060 priv->regs + TSI721_DEV_CHAN_INTE);
1061}
1062
1063/* Disable Inbound Messaging Interrupts */
1064static void
1065tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
1066 u32 inte_mask)
1067{
1068 u32 rval;
1069
1070 if (!inte_mask)
1071 return;
1072
1073 /* Clear pending Inbound Messaging interrupts */
1074 iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
1075
1076 /* Disable Inbound Messaging interrupts */
1077 rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
1078 rval &= ~inte_mask;
1079 iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
1080
1081 if (priv->flags & TSI721_USING_MSIX)
1082 return; /* Finished if we are in MSI-X mode */
1083
1084 /*
1085 * For MSI and INTA interrupt signalling we need to disable next levels
1086 */
1087
1088 /* Disable Device Channel Interrupt */
1089 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1090 rval &= ~TSI721_INT_IMSG_CHAN(ch);
1091 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1092}
1093
1094/* Enable Outbound Messaging interrupts */
1095static void
1096tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
1097 u32 inte_mask)
1098{
1099 u32 rval;
1100
1101 if (!inte_mask)
1102 return;
1103
1104 /* Clear pending Outbound Messaging interrupts */
1105 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1106
1107 /* Enable Outbound Messaging channel interrupts */
1108 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1109 iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
1110
1111 if (priv->flags & TSI721_USING_MSIX)
1112 return; /* Finished if we are in MSI-X mode */
1113
1114 /*
1115 * For MSI and INTA interrupt signalling we need to enable next levels
1116 */
1117
1118 /* Enable Device Channel Interrupt */
1119 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1120 iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
1121 priv->regs + TSI721_DEV_CHAN_INTE);
1122}
1123
1124/* Disable Outbound Messaging interrupts */
1125static void
1126tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
1127 u32 inte_mask)
1128{
1129 u32 rval;
1130
1131 if (!inte_mask)
1132 return;
1133
1134 /* Clear pending Outbound Messaging interrupts */
1135 iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
1136
1137 /* Disable Outbound Messaging interrupts */
1138 rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
1139 rval &= ~inte_mask;
1140 iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
1141
1142 if (priv->flags & TSI721_USING_MSIX)
1143 return; /* Finished if we are in MSI-X mode */
1144
1145 /*
1146 * For MSI and INTA interrupt signalling we need to disable next levels
1147 */
1148
1149 /* Disable Device Channel Interrupt */
1150 rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1151 rval &= ~TSI721_INT_OMSG_CHAN(ch);
1152 iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
1153}
1154
1155/**
1156 * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
1157 * @mport: Master port with outbound message queue
1158 * @rdev: Target of outbound message
1159 * @mbox: Outbound mailbox
1160 * @buffer: Message to add to outbound queue
1161 * @len: Length of message
1162 */
1163static int
1164tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
1165 void *buffer, size_t len)
1166{
1167 struct tsi721_device *priv = mport->priv;
1168 struct tsi721_omsg_desc *desc;
1169 u32 tx_slot;
1170
1171 if (!priv->omsg_init[mbox] ||
1172 len > TSI721_MSG_MAX_SIZE || len < 8)
1173 return -EINVAL;
1174
1175 tx_slot = priv->omsg_ring[mbox].tx_slot;
1176
1177 /* Copy copy message into transfer buffer */
1178 memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
1179
1180 if (len & 0x7)
1181 len += 8;
1182
1183 /* Build descriptor associated with buffer */
1184 desc = priv->omsg_ring[mbox].omd_base;
1185 desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
1186 if (tx_slot % 4 == 0)
1187 desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
1188
1189 desc[tx_slot].msg_info =
1190 cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
1191 (0xe << 12) | (len & 0xff8));
1192 desc[tx_slot].bufptr_lo =
1193 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
1194 0xffffffff);
1195 desc[tx_slot].bufptr_hi =
1196 cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
1197
1198 priv->omsg_ring[mbox].wr_count++;
1199
1200 /* Go to next descriptor */
1201 if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
1202 priv->omsg_ring[mbox].tx_slot = 0;
1203 /* Move through the ring link descriptor at the end */
1204 priv->omsg_ring[mbox].wr_count++;
1205 }
1206
1207 mb();
1208
1209 /* Set new write count value */
1210 iowrite32(priv->omsg_ring[mbox].wr_count,
1211 priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1212 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1213
1214 return 0;
1215}
1216
1217/**
1218 * tsi721_omsg_handler - Outbound Message Interrupt Handler
1219 * @priv: pointer to tsi721 private data
1220 * @ch: number of OB MSG channel to service
1221 *
1222 * Services channel interrupts from outbound messaging engine.
1223 */
1224static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
1225{
1226 u32 omsg_int;
1227
1228 spin_lock(&priv->omsg_ring[ch].lock);
1229
1230 omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
1231
1232 if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
1233 dev_info(&priv->pdev->dev,
1234 "OB MBOX%d: Status FIFO is full\n", ch);
1235
1236 if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
1237 u32 srd_ptr;
1238 u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
1239 int i, j;
1240 u32 tx_slot;
1241
1242 /*
1243 * Find last successfully processed descriptor
1244 */
1245
1246 /* Check and clear descriptor status FIFO entries */
1247 srd_ptr = priv->omsg_ring[ch].sts_rdptr;
1248 sts_ptr = priv->omsg_ring[ch].sts_base;
1249 j = srd_ptr * 8;
1250 while (sts_ptr[j]) {
1251 for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
1252 prev_ptr = last_ptr;
1253 last_ptr = le64_to_cpu(sts_ptr[j]);
1254 sts_ptr[j] = 0;
1255 }
1256
1257 ++srd_ptr;
1258 srd_ptr %= priv->omsg_ring[ch].sts_size;
1259 j = srd_ptr * 8;
1260 }
1261
1262 if (last_ptr == 0)
1263 goto no_sts_update;
1264
1265 priv->omsg_ring[ch].sts_rdptr = srd_ptr;
1266 iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
1267
1268 if (!priv->mport->outb_msg[ch].mcback)
1269 goto no_sts_update;
1270
1271 /* Inform upper layer about transfer completion */
1272
1273 tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
1274 sizeof(struct tsi721_omsg_desc);
1275
1276 /*
1277 * Check if this is a Link Descriptor (LD).
1278 * If yes, ignore LD and use descriptor processed
1279 * before LD.
1280 */
1281 if (tx_slot == priv->omsg_ring[ch].size) {
1282 if (prev_ptr)
1283 tx_slot = (prev_ptr -
1284 (u64)priv->omsg_ring[ch].omd_phys)/
1285 sizeof(struct tsi721_omsg_desc);
1286 else
1287 goto no_sts_update;
1288 }
1289
1290 /* Move slot index to the next message to be sent */
1291 ++tx_slot;
1292 if (tx_slot == priv->omsg_ring[ch].size)
1293 tx_slot = 0;
1294 BUG_ON(tx_slot >= priv->omsg_ring[ch].size);
1295 priv->mport->outb_msg[ch].mcback(priv->mport,
1296 priv->omsg_ring[ch].dev_id, ch,
1297 tx_slot);
1298 }
1299
1300no_sts_update:
1301
1302 if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
1303 /*
1304 * Outbound message operation aborted due to error,
1305 * reinitialize OB MSG channel
1306 */
1307
1308 dev_dbg(&priv->pdev->dev, "OB MSG ABORT ch_stat=%x\n",
1309 ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
1310
1311 iowrite32(TSI721_OBDMAC_INT_ERROR,
1312 priv->regs + TSI721_OBDMAC_INT(ch));
1313 iowrite32(TSI721_OBDMAC_CTL_INIT,
1314 priv->regs + TSI721_OBDMAC_CTL(ch));
1315 ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
1316
1317 /* Inform upper level to clear all pending tx slots */
1318 if (priv->mport->outb_msg[ch].mcback)
1319 priv->mport->outb_msg[ch].mcback(priv->mport,
1320 priv->omsg_ring[ch].dev_id, ch,
1321 priv->omsg_ring[ch].tx_slot);
1322 /* Synch tx_slot tracking */
1323 iowrite32(priv->omsg_ring[ch].tx_slot,
1324 priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1325 ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
1326 priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
1327 priv->omsg_ring[ch].sts_rdptr = 0;
1328 }
1329
1330 /* Clear channel interrupts */
1331 iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
1332
1333 if (!(priv->flags & TSI721_USING_MSIX)) {
1334 u32 ch_inte;
1335
1336 /* Re-enable channel interrupts */
1337 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1338 ch_inte |= TSI721_INT_OMSG_CHAN(ch);
1339 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1340 }
1341
1342 spin_unlock(&priv->omsg_ring[ch].lock);
1343}
1344
1345/**
1346 * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
1347 * @mport: Master port implementing Outbound Messaging Engine
1348 * @dev_id: Device specific pointer to pass on event
1349 * @mbox: Mailbox to open
1350 * @entries: Number of entries in the outbound mailbox ring
1351 */
1352static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
1353 int mbox, int entries)
1354{
1355 struct tsi721_device *priv = mport->priv;
1356 struct tsi721_omsg_desc *bd_ptr;
1357 int i, rc = 0;
1358
1359 if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
1360 (entries > (TSI721_OMSGD_RING_SIZE)) ||
1361 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1362 rc = -EINVAL;
1363 goto out;
1364 }
1365
1366 priv->omsg_ring[mbox].dev_id = dev_id;
1367 priv->omsg_ring[mbox].size = entries;
1368 priv->omsg_ring[mbox].sts_rdptr = 0;
1369 spin_lock_init(&priv->omsg_ring[mbox].lock);
1370
1371 /* Outbound Msg Buffer allocation based on
1372 the number of maximum descriptor entries */
1373 for (i = 0; i < entries; i++) {
1374 priv->omsg_ring[mbox].omq_base[i] =
1375 dma_alloc_coherent(
1376 &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
1377 &priv->omsg_ring[mbox].omq_phys[i],
1378 GFP_KERNEL);
1379 if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
1380 dev_dbg(&priv->pdev->dev,
1381 "Unable to allocate OB MSG data buffer for"
1382 " MBOX%d\n", mbox);
1383 rc = -ENOMEM;
1384 goto out_buf;
1385 }
1386 }
1387
1388 /* Outbound message descriptor allocation */
1389 priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
1390 &priv->pdev->dev,
1391 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1392 &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
1393 if (priv->omsg_ring[mbox].omd_base == NULL) {
1394 dev_dbg(&priv->pdev->dev,
1395 "Unable to allocate OB MSG descriptor memory "
1396 "for MBOX%d\n", mbox);
1397 rc = -ENOMEM;
1398 goto out_buf;
1399 }
1400
1401 priv->omsg_ring[mbox].tx_slot = 0;
1402
1403 /* Outbound message descriptor status FIFO allocation */
1404 priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
Alexandre Bounineceb96392011-12-08 14:34:35 -08001405 priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001406 priv->omsg_ring[mbox].sts_size *
1407 sizeof(struct tsi721_dma_sts),
1408 &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
1409 if (priv->omsg_ring[mbox].sts_base == NULL) {
1410 dev_dbg(&priv->pdev->dev,
1411 "Unable to allocate OB MSG descriptor status FIFO "
1412 "for MBOX%d\n", mbox);
1413 rc = -ENOMEM;
1414 goto out_desc;
1415 }
1416
Alexandre Bounine48618fb2011-11-02 13:39:09 -07001417 /*
1418 * Configure Outbound Messaging Engine
1419 */
1420
1421 /* Setup Outbound Message descriptor pointer */
1422 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
1423 priv->regs + TSI721_OBDMAC_DPTRH(mbox));
1424 iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
1425 TSI721_OBDMAC_DPTRL_MASK),
1426 priv->regs + TSI721_OBDMAC_DPTRL(mbox));
1427
1428 /* Setup Outbound Message descriptor status FIFO */
1429 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
1430 priv->regs + TSI721_OBDMAC_DSBH(mbox));
1431 iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
1432 TSI721_OBDMAC_DSBL_MASK),
1433 priv->regs + TSI721_OBDMAC_DSBL(mbox));
1434 iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
1435 priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
1436
1437 /* Enable interrupts */
1438
1439#ifdef CONFIG_PCI_MSI
1440 if (priv->flags & TSI721_USING_MSIX) {
1441 /* Request interrupt service if we are in MSI-X mode */
1442 rc = request_irq(
1443 priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
1444 tsi721_omsg_msix, 0,
1445 priv->msix[TSI721_VECT_OMB0_DONE + mbox].irq_name,
1446 (void *)mport);
1447
1448 if (rc) {
1449 dev_dbg(&priv->pdev->dev,
1450 "Unable to allocate MSI-X interrupt for "
1451 "OBOX%d-DONE\n", mbox);
1452 goto out_stat;
1453 }
1454
1455 rc = request_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
1456 tsi721_omsg_msix, 0,
1457 priv->msix[TSI721_VECT_OMB0_INT + mbox].irq_name,
1458 (void *)mport);
1459
1460 if (rc) {
1461 dev_dbg(&priv->pdev->dev,
1462 "Unable to allocate MSI-X interrupt for "
1463 "MBOX%d-INT\n", mbox);
1464 free_irq(
1465 priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
1466 (void *)mport);
1467 goto out_stat;
1468 }
1469 }
1470#endif /* CONFIG_PCI_MSI */
1471
1472 tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1473
1474 /* Initialize Outbound Message descriptors ring */
1475 bd_ptr = priv->omsg_ring[mbox].omd_base;
1476 bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
1477 bd_ptr[entries].msg_info = 0;
1478 bd_ptr[entries].next_lo =
1479 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
1480 TSI721_OBDMAC_DPTRL_MASK);
1481 bd_ptr[entries].next_hi =
1482 cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
1483 priv->omsg_ring[mbox].wr_count = 0;
1484 mb();
1485
1486 /* Initialize Outbound Message engine */
1487 iowrite32(TSI721_OBDMAC_CTL_INIT, priv->regs + TSI721_OBDMAC_CTL(mbox));
1488 ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
1489 udelay(10);
1490
1491 priv->omsg_init[mbox] = 1;
1492
1493 return 0;
1494
1495#ifdef CONFIG_PCI_MSI
1496out_stat:
1497 dma_free_coherent(&priv->pdev->dev,
1498 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1499 priv->omsg_ring[mbox].sts_base,
1500 priv->omsg_ring[mbox].sts_phys);
1501
1502 priv->omsg_ring[mbox].sts_base = NULL;
1503#endif /* CONFIG_PCI_MSI */
1504
1505out_desc:
1506 dma_free_coherent(&priv->pdev->dev,
1507 (entries + 1) * sizeof(struct tsi721_omsg_desc),
1508 priv->omsg_ring[mbox].omd_base,
1509 priv->omsg_ring[mbox].omd_phys);
1510
1511 priv->omsg_ring[mbox].omd_base = NULL;
1512
1513out_buf:
1514 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1515 if (priv->omsg_ring[mbox].omq_base[i]) {
1516 dma_free_coherent(&priv->pdev->dev,
1517 TSI721_MSG_BUFFER_SIZE,
1518 priv->omsg_ring[mbox].omq_base[i],
1519 priv->omsg_ring[mbox].omq_phys[i]);
1520
1521 priv->omsg_ring[mbox].omq_base[i] = NULL;
1522 }
1523 }
1524
1525out:
1526 return rc;
1527}
1528
1529/**
1530 * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
1531 * @mport: Master port implementing the outbound message unit
1532 * @mbox: Mailbox to close
1533 */
1534static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
1535{
1536 struct tsi721_device *priv = mport->priv;
1537 u32 i;
1538
1539 if (!priv->omsg_init[mbox])
1540 return;
1541 priv->omsg_init[mbox] = 0;
1542
1543 /* Disable Interrupts */
1544
1545 tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
1546
1547#ifdef CONFIG_PCI_MSI
1548 if (priv->flags & TSI721_USING_MSIX) {
1549 free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
1550 (void *)mport);
1551 free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
1552 (void *)mport);
1553 }
1554#endif /* CONFIG_PCI_MSI */
1555
1556 /* Free OMSG Descriptor Status FIFO */
1557 dma_free_coherent(&priv->pdev->dev,
1558 priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
1559 priv->omsg_ring[mbox].sts_base,
1560 priv->omsg_ring[mbox].sts_phys);
1561
1562 priv->omsg_ring[mbox].sts_base = NULL;
1563
1564 /* Free OMSG descriptors */
1565 dma_free_coherent(&priv->pdev->dev,
1566 (priv->omsg_ring[mbox].size + 1) *
1567 sizeof(struct tsi721_omsg_desc),
1568 priv->omsg_ring[mbox].omd_base,
1569 priv->omsg_ring[mbox].omd_phys);
1570
1571 priv->omsg_ring[mbox].omd_base = NULL;
1572
1573 /* Free message buffers */
1574 for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
1575 if (priv->omsg_ring[mbox].omq_base[i]) {
1576 dma_free_coherent(&priv->pdev->dev,
1577 TSI721_MSG_BUFFER_SIZE,
1578 priv->omsg_ring[mbox].omq_base[i],
1579 priv->omsg_ring[mbox].omq_phys[i]);
1580
1581 priv->omsg_ring[mbox].omq_base[i] = NULL;
1582 }
1583 }
1584}
1585
1586/**
1587 * tsi721_imsg_handler - Inbound Message Interrupt Handler
1588 * @priv: pointer to tsi721 private data
1589 * @ch: inbound message channel number to service
1590 *
1591 * Services channel interrupts from inbound messaging engine.
1592 */
1593static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
1594{
1595 u32 mbox = ch - 4;
1596 u32 imsg_int;
1597
1598 spin_lock(&priv->imsg_ring[mbox].lock);
1599
1600 imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
1601
1602 if (imsg_int & TSI721_IBDMAC_INT_SRTO)
1603 dev_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout\n",
1604 mbox);
1605
1606 if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
1607 dev_info(&priv->pdev->dev, "IB MBOX%d PCIe error\n",
1608 mbox);
1609
1610 if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
1611 dev_info(&priv->pdev->dev,
1612 "IB MBOX%d IB free queue low\n", mbox);
1613
1614 /* Clear IB channel interrupts */
1615 iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
1616
1617 /* If an IB Msg is received notify the upper layer */
1618 if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
1619 priv->mport->inb_msg[mbox].mcback)
1620 priv->mport->inb_msg[mbox].mcback(priv->mport,
1621 priv->imsg_ring[mbox].dev_id, mbox, -1);
1622
1623 if (!(priv->flags & TSI721_USING_MSIX)) {
1624 u32 ch_inte;
1625
1626 /* Re-enable channel interrupts */
1627 ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
1628 ch_inte |= TSI721_INT_IMSG_CHAN(ch);
1629 iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
1630 }
1631
1632 spin_unlock(&priv->imsg_ring[mbox].lock);
1633}
1634
1635/**
1636 * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
1637 * @mport: Master port implementing the Inbound Messaging Engine
1638 * @dev_id: Device specific pointer to pass on event
1639 * @mbox: Mailbox to open
1640 * @entries: Number of entries in the inbound mailbox ring
1641 */
1642static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
1643 int mbox, int entries)
1644{
1645 struct tsi721_device *priv = mport->priv;
1646 int ch = mbox + 4;
1647 int i;
1648 u64 *free_ptr;
1649 int rc = 0;
1650
1651 if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
1652 (entries > TSI721_IMSGD_RING_SIZE) ||
1653 (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
1654 rc = -EINVAL;
1655 goto out;
1656 }
1657
1658 /* Initialize IB Messaging Ring */
1659 priv->imsg_ring[mbox].dev_id = dev_id;
1660 priv->imsg_ring[mbox].size = entries;
1661 priv->imsg_ring[mbox].rx_slot = 0;
1662 priv->imsg_ring[mbox].desc_rdptr = 0;
1663 priv->imsg_ring[mbox].fq_wrptr = 0;
1664 for (i = 0; i < priv->imsg_ring[mbox].size; i++)
1665 priv->imsg_ring[mbox].imq_base[i] = NULL;
1666 spin_lock_init(&priv->imsg_ring[mbox].lock);
1667
1668 /* Allocate buffers for incoming messages */
1669 priv->imsg_ring[mbox].buf_base =
1670 dma_alloc_coherent(&priv->pdev->dev,
1671 entries * TSI721_MSG_BUFFER_SIZE,
1672 &priv->imsg_ring[mbox].buf_phys,
1673 GFP_KERNEL);
1674
1675 if (priv->imsg_ring[mbox].buf_base == NULL) {
1676 dev_err(&priv->pdev->dev,
1677 "Failed to allocate buffers for IB MBOX%d\n", mbox);
1678 rc = -ENOMEM;
1679 goto out;
1680 }
1681
1682 /* Allocate memory for circular free list */
1683 priv->imsg_ring[mbox].imfq_base =
1684 dma_alloc_coherent(&priv->pdev->dev,
1685 entries * 8,
1686 &priv->imsg_ring[mbox].imfq_phys,
1687 GFP_KERNEL);
1688
1689 if (priv->imsg_ring[mbox].imfq_base == NULL) {
1690 dev_err(&priv->pdev->dev,
1691 "Failed to allocate free queue for IB MBOX%d\n", mbox);
1692 rc = -ENOMEM;
1693 goto out_buf;
1694 }
1695
1696 /* Allocate memory for Inbound message descriptors */
1697 priv->imsg_ring[mbox].imd_base =
1698 dma_alloc_coherent(&priv->pdev->dev,
1699 entries * sizeof(struct tsi721_imsg_desc),
1700 &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
1701
1702 if (priv->imsg_ring[mbox].imd_base == NULL) {
1703 dev_err(&priv->pdev->dev,
1704 "Failed to allocate descriptor memory for IB MBOX%d\n",
1705 mbox);
1706 rc = -ENOMEM;
1707 goto out_dma;
1708 }
1709
1710 /* Fill free buffer pointer list */
1711 free_ptr = priv->imsg_ring[mbox].imfq_base;
1712 for (i = 0; i < entries; i++)
1713 free_ptr[i] = cpu_to_le64(
1714 (u64)(priv->imsg_ring[mbox].buf_phys) +
1715 i * 0x1000);
1716
1717 mb();
1718
1719 /*
1720 * For mapping of inbound SRIO Messages into appropriate queues we need
1721 * to set Inbound Device ID register in the messaging engine. We do it
1722 * once when first inbound mailbox is requested.
1723 */
1724 if (!(priv->flags & TSI721_IMSGID_SET)) {
1725 iowrite32((u32)priv->mport->host_deviceid,
1726 priv->regs + TSI721_IB_DEVID);
1727 priv->flags |= TSI721_IMSGID_SET;
1728 }
1729
1730 /*
1731 * Configure Inbound Messaging channel (ch = mbox + 4)
1732 */
1733
1734 /* Setup Inbound Message free queue */
1735 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
1736 priv->regs + TSI721_IBDMAC_FQBH(ch));
1737 iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
1738 TSI721_IBDMAC_FQBL_MASK),
1739 priv->regs+TSI721_IBDMAC_FQBL(ch));
1740 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
1741 priv->regs + TSI721_IBDMAC_FQSZ(ch));
1742
1743 /* Setup Inbound Message descriptor queue */
1744 iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
1745 priv->regs + TSI721_IBDMAC_DQBH(ch));
1746 iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
1747 (u32)TSI721_IBDMAC_DQBL_MASK),
1748 priv->regs+TSI721_IBDMAC_DQBL(ch));
1749 iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
1750 priv->regs + TSI721_IBDMAC_DQSZ(ch));
1751
1752 /* Enable interrupts */
1753
1754#ifdef CONFIG_PCI_MSI
1755 if (priv->flags & TSI721_USING_MSIX) {
1756 /* Request interrupt service if we are in MSI-X mode */
1757 rc = request_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
1758 tsi721_imsg_msix, 0,
1759 priv->msix[TSI721_VECT_IMB0_RCV + mbox].irq_name,
1760 (void *)mport);
1761
1762 if (rc) {
1763 dev_dbg(&priv->pdev->dev,
1764 "Unable to allocate MSI-X interrupt for "
1765 "IBOX%d-DONE\n", mbox);
1766 goto out_desc;
1767 }
1768
1769 rc = request_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
1770 tsi721_imsg_msix, 0,
1771 priv->msix[TSI721_VECT_IMB0_INT + mbox].irq_name,
1772 (void *)mport);
1773
1774 if (rc) {
1775 dev_dbg(&priv->pdev->dev,
1776 "Unable to allocate MSI-X interrupt for "
1777 "IBOX%d-INT\n", mbox);
1778 free_irq(
1779 priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
1780 (void *)mport);
1781 goto out_desc;
1782 }
1783 }
1784#endif /* CONFIG_PCI_MSI */
1785
1786 tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
1787
1788 /* Initialize Inbound Message Engine */
1789 iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
1790 ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
1791 udelay(10);
1792 priv->imsg_ring[mbox].fq_wrptr = entries - 1;
1793 iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
1794
1795 priv->imsg_init[mbox] = 1;
1796 return 0;
1797
1798#ifdef CONFIG_PCI_MSI
1799out_desc:
1800 dma_free_coherent(&priv->pdev->dev,
1801 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
1802 priv->imsg_ring[mbox].imd_base,
1803 priv->imsg_ring[mbox].imd_phys);
1804
1805 priv->imsg_ring[mbox].imd_base = NULL;
1806#endif /* CONFIG_PCI_MSI */
1807
1808out_dma:
1809 dma_free_coherent(&priv->pdev->dev,
1810 priv->imsg_ring[mbox].size * 8,
1811 priv->imsg_ring[mbox].imfq_base,
1812 priv->imsg_ring[mbox].imfq_phys);
1813
1814 priv->imsg_ring[mbox].imfq_base = NULL;
1815
1816out_buf:
1817 dma_free_coherent(&priv->pdev->dev,
1818 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
1819 priv->imsg_ring[mbox].buf_base,
1820 priv->imsg_ring[mbox].buf_phys);
1821
1822 priv->imsg_ring[mbox].buf_base = NULL;
1823
1824out:
1825 return rc;
1826}
1827
1828/**
1829 * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
1830 * @mport: Master port implementing the Inbound Messaging Engine
1831 * @mbox: Mailbox to close
1832 */
1833static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
1834{
1835 struct tsi721_device *priv = mport->priv;
1836 u32 rx_slot;
1837 int ch = mbox + 4;
1838
1839 if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
1840 return;
1841 priv->imsg_init[mbox] = 0;
1842
1843 /* Disable Inbound Messaging Engine */
1844
1845 /* Disable Interrupts */
1846 tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
1847
1848#ifdef CONFIG_PCI_MSI
1849 if (priv->flags & TSI721_USING_MSIX) {
1850 free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
1851 (void *)mport);
1852 free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
1853 (void *)mport);
1854 }
1855#endif /* CONFIG_PCI_MSI */
1856
1857 /* Clear Inbound Buffer Queue */
1858 for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
1859 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
1860
1861 /* Free memory allocated for message buffers */
1862 dma_free_coherent(&priv->pdev->dev,
1863 priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
1864 priv->imsg_ring[mbox].buf_base,
1865 priv->imsg_ring[mbox].buf_phys);
1866
1867 priv->imsg_ring[mbox].buf_base = NULL;
1868
1869 /* Free memory allocated for free pointr list */
1870 dma_free_coherent(&priv->pdev->dev,
1871 priv->imsg_ring[mbox].size * 8,
1872 priv->imsg_ring[mbox].imfq_base,
1873 priv->imsg_ring[mbox].imfq_phys);
1874
1875 priv->imsg_ring[mbox].imfq_base = NULL;
1876
1877 /* Free memory allocated for RX descriptors */
1878 dma_free_coherent(&priv->pdev->dev,
1879 priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
1880 priv->imsg_ring[mbox].imd_base,
1881 priv->imsg_ring[mbox].imd_phys);
1882
1883 priv->imsg_ring[mbox].imd_base = NULL;
1884}
1885
1886/**
1887 * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
1888 * @mport: Master port implementing the Inbound Messaging Engine
1889 * @mbox: Inbound mailbox number
1890 * @buf: Buffer to add to inbound queue
1891 */
1892static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
1893{
1894 struct tsi721_device *priv = mport->priv;
1895 u32 rx_slot;
1896 int rc = 0;
1897
1898 rx_slot = priv->imsg_ring[mbox].rx_slot;
1899 if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
1900 dev_err(&priv->pdev->dev,
1901 "Error adding inbound buffer %d, buffer exists\n",
1902 rx_slot);
1903 rc = -EINVAL;
1904 goto out;
1905 }
1906
1907 priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
1908
1909 if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
1910 priv->imsg_ring[mbox].rx_slot = 0;
1911
1912out:
1913 return rc;
1914}
1915
1916/**
1917 * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
1918 * @mport: Master port implementing the Inbound Messaging Engine
1919 * @mbox: Inbound mailbox number
1920 *
1921 * Returns pointer to the message on success or NULL on failure.
1922 */
1923static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
1924{
1925 struct tsi721_device *priv = mport->priv;
1926 struct tsi721_imsg_desc *desc;
1927 u32 rx_slot;
1928 void *rx_virt = NULL;
1929 u64 rx_phys;
1930 void *buf = NULL;
1931 u64 *free_ptr;
1932 int ch = mbox + 4;
1933 int msg_size;
1934
1935 if (!priv->imsg_init[mbox])
1936 return NULL;
1937
1938 desc = priv->imsg_ring[mbox].imd_base;
1939 desc += priv->imsg_ring[mbox].desc_rdptr;
1940
1941 if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
1942 goto out;
1943
1944 rx_slot = priv->imsg_ring[mbox].rx_slot;
1945 while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
1946 if (++rx_slot == priv->imsg_ring[mbox].size)
1947 rx_slot = 0;
1948 }
1949
1950 rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
1951 le32_to_cpu(desc->bufptr_lo);
1952
1953 rx_virt = priv->imsg_ring[mbox].buf_base +
1954 (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
1955
1956 buf = priv->imsg_ring[mbox].imq_base[rx_slot];
1957 msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
1958 if (msg_size == 0)
1959 msg_size = RIO_MAX_MSG_SIZE;
1960
1961 memcpy(buf, rx_virt, msg_size);
1962 priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
1963
1964 desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
1965 if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
1966 priv->imsg_ring[mbox].desc_rdptr = 0;
1967
1968 iowrite32(priv->imsg_ring[mbox].desc_rdptr,
1969 priv->regs + TSI721_IBDMAC_DQRP(ch));
1970
1971 /* Return free buffer into the pointer list */
1972 free_ptr = priv->imsg_ring[mbox].imfq_base;
1973 free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
1974
1975 if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
1976 priv->imsg_ring[mbox].fq_wrptr = 0;
1977
1978 iowrite32(priv->imsg_ring[mbox].fq_wrptr,
1979 priv->regs + TSI721_IBDMAC_FQWP(ch));
1980out:
1981 return buf;
1982}
1983
1984/**
1985 * tsi721_messages_init - Initialization of Messaging Engine
1986 * @priv: pointer to tsi721 private data
1987 *
1988 * Configures Tsi721 messaging engine.
1989 */
1990static int tsi721_messages_init(struct tsi721_device *priv)
1991{
1992 int ch;
1993
1994 iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
1995 iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
1996 iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
1997
1998 /* Set SRIO Message Request/Response Timeout */
1999 iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
2000
2001 /* Initialize Inbound Messaging Engine Registers */
2002 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
2003 /* Clear interrupt bits */
2004 iowrite32(TSI721_IBDMAC_INT_MASK,
2005 priv->regs + TSI721_IBDMAC_INT(ch));
2006 /* Clear Status */
2007 iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
2008
2009 iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
2010 priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
2011 iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
2012 priv->regs + TSI721_SMSG_ECC_NCOR(ch));
2013 }
2014
2015 return 0;
2016}
2017
2018/**
2019 * tsi721_disable_ints - disables all device interrupts
2020 * @priv: pointer to tsi721 private data
2021 */
2022static void tsi721_disable_ints(struct tsi721_device *priv)
2023{
2024 int ch;
2025
2026 /* Disable all device level interrupts */
2027 iowrite32(0, priv->regs + TSI721_DEV_INTE);
2028
2029 /* Disable all Device Channel interrupts */
2030 iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
2031
2032 /* Disable all Inbound Msg Channel interrupts */
2033 for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
2034 iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
2035
2036 /* Disable all Outbound Msg Channel interrupts */
2037 for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
2038 iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
2039
2040 /* Disable all general messaging interrupts */
2041 iowrite32(0, priv->regs + TSI721_SMSG_INTE);
2042
2043 /* Disable all BDMA Channel interrupts */
2044 for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
2045 iowrite32(0, priv->regs + TSI721_DMAC_INTE(ch));
2046
2047 /* Disable all general BDMA interrupts */
2048 iowrite32(0, priv->regs + TSI721_BDMA_INTE);
2049
2050 /* Disable all SRIO Channel interrupts */
2051 for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
2052 iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
2053
2054 /* Disable all general SR2PC interrupts */
2055 iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
2056
2057 /* Disable all PC2SR interrupts */
2058 iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
2059
2060 /* Disable all I2C interrupts */
2061 iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
2062
2063 /* Disable SRIO MAC interrupts */
2064 iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
2065 iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
2066}
2067
2068/**
2069 * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
2070 * @priv: pointer to tsi721 private data
2071 *
2072 * Configures Tsi721 as RapidIO master port.
2073 */
2074static int __devinit tsi721_setup_mport(struct tsi721_device *priv)
2075{
2076 struct pci_dev *pdev = priv->pdev;
2077 int err = 0;
2078 struct rio_ops *ops;
2079
2080 struct rio_mport *mport;
2081
2082 ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
2083 if (!ops) {
2084 dev_dbg(&pdev->dev, "Unable to allocate memory for rio_ops\n");
2085 return -ENOMEM;
2086 }
2087
2088 ops->lcread = tsi721_lcread;
2089 ops->lcwrite = tsi721_lcwrite;
2090 ops->cread = tsi721_cread_dma;
2091 ops->cwrite = tsi721_cwrite_dma;
2092 ops->dsend = tsi721_dsend;
2093 ops->open_inb_mbox = tsi721_open_inb_mbox;
2094 ops->close_inb_mbox = tsi721_close_inb_mbox;
2095 ops->open_outb_mbox = tsi721_open_outb_mbox;
2096 ops->close_outb_mbox = tsi721_close_outb_mbox;
2097 ops->add_outb_message = tsi721_add_outb_message;
2098 ops->add_inb_buffer = tsi721_add_inb_buffer;
2099 ops->get_inb_message = tsi721_get_inb_message;
2100
2101 mport = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
2102 if (!mport) {
2103 kfree(ops);
2104 dev_dbg(&pdev->dev, "Unable to allocate memory for mport\n");
2105 return -ENOMEM;
2106 }
2107
2108 mport->ops = ops;
2109 mport->index = 0;
2110 mport->sys_size = 0; /* small system */
2111 mport->phy_type = RIO_PHY_SERIAL;
2112 mport->priv = (void *)priv;
2113 mport->phys_efptr = 0x100;
2114
2115 INIT_LIST_HEAD(&mport->dbells);
2116
2117 rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
Alexandre Bounineb439e662011-12-08 14:34:36 -08002118 rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
2119 rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002120 strcpy(mport->name, "Tsi721 mport");
2121
2122 /* Hook up interrupt handler */
2123
2124#ifdef CONFIG_PCI_MSI
2125 if (!tsi721_enable_msix(priv))
2126 priv->flags |= TSI721_USING_MSIX;
2127 else if (!pci_enable_msi(pdev))
2128 priv->flags |= TSI721_USING_MSI;
2129 else
2130 dev_info(&pdev->dev,
2131 "MSI/MSI-X is not available. Using legacy INTx.\n");
2132#endif /* CONFIG_PCI_MSI */
2133
2134 err = tsi721_request_irq(mport);
2135
2136 if (!err) {
2137 tsi721_interrupts_init(priv);
2138 ops->pwenable = tsi721_pw_enable;
2139 } else
2140 dev_err(&pdev->dev, "Unable to get assigned PCI IRQ "
2141 "vector %02X err=0x%x\n", pdev->irq, err);
2142
2143 /* Enable SRIO link */
2144 iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
2145 TSI721_DEVCTL_SRBOOT_CMPL,
2146 priv->regs + TSI721_DEVCTL);
2147
2148 rio_register_mport(mport);
2149 priv->mport = mport;
2150
2151 if (mport->host_deviceid >= 0)
2152 iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
2153 RIO_PORT_GEN_DISCOVERED,
2154 priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2155 else
2156 iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
2157
2158 return 0;
2159}
2160
2161static int __devinit tsi721_probe(struct pci_dev *pdev,
2162 const struct pci_device_id *id)
2163{
2164 struct tsi721_device *priv;
Alexandre Bounine3ad3bc52012-08-21 16:16:12 -07002165 int cap;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002166 int err;
2167 u32 regval;
2168
2169 priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
2170 if (priv == NULL) {
2171 dev_err(&pdev->dev, "Failed to allocate memory for device\n");
2172 err = -ENOMEM;
2173 goto err_exit;
2174 }
2175
2176 err = pci_enable_device(pdev);
2177 if (err) {
2178 dev_err(&pdev->dev, "Failed to enable PCI device\n");
2179 goto err_clean;
2180 }
2181
2182 priv->pdev = pdev;
2183
2184#ifdef DEBUG
Alexandre Bounine3ad3bc52012-08-21 16:16:12 -07002185 {
2186 int i;
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002187 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
2188 dev_dbg(&pdev->dev, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
2189 i, (unsigned long long)pci_resource_start(pdev, i),
2190 (unsigned long)pci_resource_len(pdev, i),
2191 pci_resource_flags(pdev, i));
2192 }
Alexandre Bounine3ad3bc52012-08-21 16:16:12 -07002193 }
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002194#endif
2195 /*
2196 * Verify BAR configuration
2197 */
2198
2199 /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
2200 if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
2201 pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
2202 pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
2203 dev_err(&pdev->dev,
2204 "Missing or misconfigured CSR BAR0, aborting.\n");
2205 err = -ENODEV;
2206 goto err_disable_pdev;
2207 }
2208
2209 /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
2210 if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
2211 pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
2212 pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
2213 dev_err(&pdev->dev,
2214 "Missing or misconfigured Doorbell BAR1, aborting.\n");
2215 err = -ENODEV;
2216 goto err_disable_pdev;
2217 }
2218
2219 /*
2220 * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
2221 * space.
2222 * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
2223 * It may be a good idea to keep them disabled using HW configuration
2224 * to save PCI memory space.
2225 */
2226 if ((pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM) &&
2227 (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64)) {
2228 dev_info(&pdev->dev, "Outbound BAR2 is not used but enabled.\n");
2229 }
2230
2231 if ((pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM) &&
2232 (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64)) {
2233 dev_info(&pdev->dev, "Outbound BAR4 is not used but enabled.\n");
2234 }
2235
2236 err = pci_request_regions(pdev, DRV_NAME);
2237 if (err) {
2238 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
2239 "aborting.\n");
2240 goto err_disable_pdev;
2241 }
2242
2243 pci_set_master(pdev);
2244
2245 priv->regs = pci_ioremap_bar(pdev, BAR_0);
2246 if (!priv->regs) {
2247 dev_err(&pdev->dev,
2248 "Unable to map device registers space, aborting\n");
2249 err = -ENOMEM;
2250 goto err_free_res;
2251 }
2252
2253 priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
2254 if (!priv->odb_base) {
2255 dev_err(&pdev->dev,
2256 "Unable to map outbound doorbells space, aborting\n");
2257 err = -ENOMEM;
2258 goto err_unmap_bars;
2259 }
2260
2261 /* Configure DMA attributes. */
2262 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
2263 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
2264 dev_info(&pdev->dev, "Unable to set DMA mask\n");
2265 goto err_unmap_bars;
2266 }
2267
2268 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2269 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2270 } else {
2271 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2272 if (err)
2273 dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
2274 }
2275
Alexandre Bounine1cee22b2011-12-08 14:34:42 -08002276 cap = pci_pcie_cap(pdev);
2277 BUG_ON(cap == 0);
2278
2279 /* Clear "no snoop" and "relaxed ordering" bits, use default MRRS. */
2280 pci_read_config_dword(pdev, cap + PCI_EXP_DEVCTL, &regval);
2281 regval &= ~(PCI_EXP_DEVCTL_READRQ | PCI_EXP_DEVCTL_RELAX_EN |
2282 PCI_EXP_DEVCTL_NOSNOOP_EN);
2283 regval |= 0x2 << MAX_READ_REQUEST_SZ_SHIFT;
2284 pci_write_config_dword(pdev, cap + PCI_EXP_DEVCTL, regval);
2285
2286 /* Adjust PCIe completion timeout. */
2287 pci_read_config_dword(pdev, cap + PCI_EXP_DEVCTL2, &regval);
2288 regval &= ~(0x0f);
2289 pci_write_config_dword(pdev, cap + PCI_EXP_DEVCTL2, regval | 0x2);
Alexandre Bounine48618fb2011-11-02 13:39:09 -07002290
2291 /*
2292 * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
2293 */
2294 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
2295 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
2296 TSI721_MSIXTBL_OFFSET);
2297 pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
2298 TSI721_MSIXPBA_OFFSET);
2299 pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
2300 /* End of FIXUP */
2301
2302 tsi721_disable_ints(priv);
2303
2304 tsi721_init_pc2sr_mapping(priv);
2305 tsi721_init_sr2pc_mapping(priv);
2306
2307 if (tsi721_bdma_init(priv)) {
2308 dev_err(&pdev->dev, "BDMA initialization failed, aborting\n");
2309 err = -ENOMEM;
2310 goto err_unmap_bars;
2311 }
2312
2313 err = tsi721_doorbell_init(priv);
2314 if (err)
2315 goto err_free_bdma;
2316
2317 tsi721_port_write_init(priv);
2318
2319 err = tsi721_messages_init(priv);
2320 if (err)
2321 goto err_free_consistent;
2322
2323 err = tsi721_setup_mport(priv);
2324 if (err)
2325 goto err_free_consistent;
2326
2327 return 0;
2328
2329err_free_consistent:
2330 tsi721_doorbell_free(priv);
2331err_free_bdma:
2332 tsi721_bdma_free(priv);
2333err_unmap_bars:
2334 if (priv->regs)
2335 iounmap(priv->regs);
2336 if (priv->odb_base)
2337 iounmap(priv->odb_base);
2338err_free_res:
2339 pci_release_regions(pdev);
2340 pci_clear_master(pdev);
2341err_disable_pdev:
2342 pci_disable_device(pdev);
2343err_clean:
2344 kfree(priv);
2345err_exit:
2346 return err;
2347}
2348
2349static DEFINE_PCI_DEVICE_TABLE(tsi721_pci_tbl) = {
2350 { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
2351 { 0, } /* terminate list */
2352};
2353
2354MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
2355
2356static struct pci_driver tsi721_driver = {
2357 .name = "tsi721",
2358 .id_table = tsi721_pci_tbl,
2359 .probe = tsi721_probe,
2360};
2361
2362static int __init tsi721_init(void)
2363{
2364 return pci_register_driver(&tsi721_driver);
2365}
2366
2367static void __exit tsi721_exit(void)
2368{
2369 pci_unregister_driver(&tsi721_driver);
2370}
2371
2372device_initcall(tsi721_init);