blob: 77bd4165238f48f32f46f830b937c6bcfcd57227 [file] [log] [blame]
Matt Porter394b7012005-11-07 01:00:15 -08001/*
2 * RapidIO interconnect services
3 * (RapidIO Interconnect Specification, http://www.rapidio.org)
4 *
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
7 *
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07008 * Copyright 2009 Integrated Device Technology, Inc.
9 * Alex Bounine <alexandre.bounine@idt.com>
10 * - Added Port-Write/Error Management initialization and handling
11 *
Matt Porter394b7012005-11-07 01:00:15 -080012 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 */
17
Matt Porter394b7012005-11-07 01:00:15 -080018#include <linux/types.h>
19#include <linux/kernel.h>
20
21#include <linux/delay.h>
22#include <linux/init.h>
23#include <linux/rio.h>
24#include <linux/rio_drv.h>
25#include <linux/rio_ids.h>
26#include <linux/rio_regs.h>
27#include <linux/module.h>
28#include <linux/spinlock.h>
Tim Schmielaude259682006-01-08 01:02:05 -080029#include <linux/slab.h>
Kumar Gala5febf1c2008-01-23 05:53:47 -060030#include <linux/interrupt.h>
Matt Porter394b7012005-11-07 01:00:15 -080031
32#include "rio.h"
33
34static LIST_HEAD(rio_mports);
35
36/**
37 * rio_local_get_device_id - Get the base/extended device id for a port
38 * @port: RIO master port from which to get the deviceid
39 *
40 * Reads the base/extended device id from the local device
41 * implementing the master port. Returns the 8/16-bit device
42 * id.
43 */
44u16 rio_local_get_device_id(struct rio_mport *port)
45{
46 u32 result;
47
48 rio_local_read_config_32(port, RIO_DID_CSR, &result);
49
Zhang Weie0423232008-04-18 13:33:42 -070050 return (RIO_GET_DID(port->sys_size, result));
Matt Porter394b7012005-11-07 01:00:15 -080051}
52
53/**
54 * rio_request_inb_mbox - request inbound mailbox service
55 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -080056 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -080057 * @mbox: Mailbox number to claim
58 * @entries: Number of entries in inbound mailbox queue
59 * @minb: Callback to execute when inbound message is received
60 *
61 * Requests ownership of an inbound mailbox resource and binds
62 * a callback function to the resource. Returns %0 on success.
63 */
64int rio_request_inb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -080065 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -080066 int mbox,
67 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -080068 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
Matt Porter394b7012005-11-07 01:00:15 -080069 int slot))
70{
71 int rc = 0;
72
73 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
74
75 if (res) {
76 rio_init_mbox_res(res, mbox, mbox);
77
78 /* Make sure this mailbox isn't in use */
79 if ((rc =
80 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
81 res)) < 0) {
82 kfree(res);
83 goto out;
84 }
85
86 mport->inb_msg[mbox].res = res;
87
88 /* Hook the inbound message callback */
89 mport->inb_msg[mbox].mcback = minb;
90
Matt Porter6978bbc2005-11-07 01:00:20 -080091 rc = rio_open_inb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -080092 } else
93 rc = -ENOMEM;
94
95 out:
96 return rc;
97}
98
99/**
100 * rio_release_inb_mbox - release inbound mailbox message service
101 * @mport: RIO master port from which to release the mailbox resource
102 * @mbox: Mailbox number to release
103 *
104 * Releases ownership of an inbound mailbox resource. Returns 0
105 * if the request has been satisfied.
106 */
107int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
108{
109 rio_close_inb_mbox(mport, mbox);
110
111 /* Release the mailbox resource */
112 return release_resource(mport->inb_msg[mbox].res);
113}
114
115/**
116 * rio_request_outb_mbox - request outbound mailbox service
117 * @mport: RIO master port from which to allocate the mailbox resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800118 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800119 * @mbox: Mailbox number to claim
120 * @entries: Number of entries in outbound mailbox queue
121 * @moutb: Callback to execute when outbound message is sent
122 *
123 * Requests ownership of an outbound mailbox resource and binds
124 * a callback function to the resource. Returns 0 on success.
125 */
126int rio_request_outb_mbox(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800127 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800128 int mbox,
129 int entries,
Matt Porter6978bbc2005-11-07 01:00:20 -0800130 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
Matt Porter394b7012005-11-07 01:00:15 -0800131{
132 int rc = 0;
133
134 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
135
136 if (res) {
137 rio_init_mbox_res(res, mbox, mbox);
138
139 /* Make sure this outbound mailbox isn't in use */
140 if ((rc =
141 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
142 res)) < 0) {
143 kfree(res);
144 goto out;
145 }
146
147 mport->outb_msg[mbox].res = res;
148
149 /* Hook the inbound message callback */
150 mport->outb_msg[mbox].mcback = moutb;
151
Matt Porter6978bbc2005-11-07 01:00:20 -0800152 rc = rio_open_outb_mbox(mport, dev_id, mbox, entries);
Matt Porter394b7012005-11-07 01:00:15 -0800153 } else
154 rc = -ENOMEM;
155
156 out:
157 return rc;
158}
159
160/**
161 * rio_release_outb_mbox - release outbound mailbox message service
162 * @mport: RIO master port from which to release the mailbox resource
163 * @mbox: Mailbox number to release
164 *
165 * Releases ownership of an inbound mailbox resource. Returns 0
166 * if the request has been satisfied.
167 */
168int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
169{
170 rio_close_outb_mbox(mport, mbox);
171
172 /* Release the mailbox resource */
173 return release_resource(mport->outb_msg[mbox].res);
174}
175
176/**
177 * rio_setup_inb_dbell - bind inbound doorbell callback
178 * @mport: RIO master port to bind the doorbell callback
Matt Porter6978bbc2005-11-07 01:00:20 -0800179 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800180 * @res: Doorbell message resource
181 * @dinb: Callback to execute when doorbell is received
182 *
183 * Adds a doorbell resource/callback pair into a port's
184 * doorbell event list. Returns 0 if the request has been
185 * satisfied.
186 */
187static int
Matt Porter6978bbc2005-11-07 01:00:20 -0800188rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
189 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
Matt Porter394b7012005-11-07 01:00:15 -0800190 u16 info))
191{
192 int rc = 0;
193 struct rio_dbell *dbell;
194
195 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
196 rc = -ENOMEM;
197 goto out;
198 }
199
200 dbell->res = res;
201 dbell->dinb = dinb;
Matt Porter6978bbc2005-11-07 01:00:20 -0800202 dbell->dev_id = dev_id;
Matt Porter394b7012005-11-07 01:00:15 -0800203
204 list_add_tail(&dbell->node, &mport->dbells);
205
206 out:
207 return rc;
208}
209
210/**
211 * rio_request_inb_dbell - request inbound doorbell message service
212 * @mport: RIO master port from which to allocate the doorbell resource
Matt Porter6978bbc2005-11-07 01:00:20 -0800213 * @dev_id: Device specific pointer to pass on event
Matt Porter394b7012005-11-07 01:00:15 -0800214 * @start: Doorbell info range start
215 * @end: Doorbell info range end
216 * @dinb: Callback to execute when doorbell is received
217 *
218 * Requests ownership of an inbound doorbell resource and binds
219 * a callback function to the resource. Returns 0 if the request
220 * has been satisfied.
221 */
222int rio_request_inb_dbell(struct rio_mport *mport,
Matt Porter6978bbc2005-11-07 01:00:20 -0800223 void *dev_id,
Matt Porter394b7012005-11-07 01:00:15 -0800224 u16 start,
225 u16 end,
Matt Porter6978bbc2005-11-07 01:00:20 -0800226 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
Matt Porter394b7012005-11-07 01:00:15 -0800227 u16 dst, u16 info))
228{
229 int rc = 0;
230
231 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
232
233 if (res) {
234 rio_init_dbell_res(res, start, end);
235
236 /* Make sure these doorbells aren't in use */
237 if ((rc =
238 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
239 res)) < 0) {
240 kfree(res);
241 goto out;
242 }
243
244 /* Hook the doorbell callback */
Matt Porter6978bbc2005-11-07 01:00:20 -0800245 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
Matt Porter394b7012005-11-07 01:00:15 -0800246 } else
247 rc = -ENOMEM;
248
249 out:
250 return rc;
251}
252
253/**
254 * rio_release_inb_dbell - release inbound doorbell message service
255 * @mport: RIO master port from which to release the doorbell resource
256 * @start: Doorbell info range start
257 * @end: Doorbell info range end
258 *
259 * Releases ownership of an inbound doorbell resource and removes
260 * callback from the doorbell event list. Returns 0 if the request
261 * has been satisfied.
262 */
263int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
264{
265 int rc = 0, found = 0;
266 struct rio_dbell *dbell;
267
268 list_for_each_entry(dbell, &mport->dbells, node) {
269 if ((dbell->res->start == start) && (dbell->res->end == end)) {
270 found = 1;
271 break;
272 }
273 }
274
275 /* If we can't find an exact match, fail */
276 if (!found) {
277 rc = -EINVAL;
278 goto out;
279 }
280
281 /* Delete from list */
282 list_del(&dbell->node);
283
284 /* Release the doorbell resource */
285 rc = release_resource(dbell->res);
286
287 /* Free the doorbell event */
288 kfree(dbell);
289
290 out:
291 return rc;
292}
293
294/**
295 * rio_request_outb_dbell - request outbound doorbell message range
296 * @rdev: RIO device from which to allocate the doorbell resource
297 * @start: Doorbell message range start
298 * @end: Doorbell message range end
299 *
300 * Requests ownership of a doorbell message range. Returns a resource
301 * if the request has been satisfied or %NULL on failure.
302 */
303struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
304 u16 end)
305{
306 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
307
308 if (res) {
309 rio_init_dbell_res(res, start, end);
310
311 /* Make sure these doorbells aren't in use */
312 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
313 < 0) {
314 kfree(res);
315 res = NULL;
316 }
317 }
318
319 return res;
320}
321
322/**
323 * rio_release_outb_dbell - release outbound doorbell message range
324 * @rdev: RIO device from which to release the doorbell resource
325 * @res: Doorbell resource to be freed
326 *
327 * Releases ownership of a doorbell message range. Returns 0 if the
328 * request has been satisfied.
329 */
330int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
331{
332 int rc = release_resource(res);
333
334 kfree(res);
335
336 return rc;
337}
338
339/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700340 * rio_request_inb_pwrite - request inbound port-write message service
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700341 * @rdev: RIO device to which register inbound port-write callback routine
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700342 * @pwcback: Callback routine to execute when port-write is received
343 *
344 * Binds a port-write callback function to the RapidIO device.
345 * Returns 0 if the request has been satisfied.
346 */
347int rio_request_inb_pwrite(struct rio_dev *rdev,
348 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
349{
350 int rc = 0;
351
352 spin_lock(&rio_global_list_lock);
353 if (rdev->pwcback != NULL)
354 rc = -ENOMEM;
355 else
356 rdev->pwcback = pwcback;
357
358 spin_unlock(&rio_global_list_lock);
359 return rc;
360}
361EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
362
363/**
364 * rio_release_inb_pwrite - release inbound port-write message service
365 * @rdev: RIO device which registered for inbound port-write callback
366 *
367 * Removes callback from the rio_dev structure. Returns 0 if the request
368 * has been satisfied.
369 */
370int rio_release_inb_pwrite(struct rio_dev *rdev)
371{
372 int rc = -ENOMEM;
373
374 spin_lock(&rio_global_list_lock);
375 if (rdev->pwcback) {
376 rdev->pwcback = NULL;
377 rc = 0;
378 }
379
380 spin_unlock(&rio_global_list_lock);
381 return rc;
382}
383EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
384
385/**
386 * rio_mport_get_physefb - Helper function that returns register offset
387 * for Physical Layer Extended Features Block.
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700388 * @port: Master port to issue transaction
389 * @local: Indicate a local master port or remote device access
390 * @destid: Destination ID of the device
391 * @hopcount: Number of switch hops to the device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700392 */
393u32
394rio_mport_get_physefb(struct rio_mport *port, int local,
395 u16 destid, u8 hopcount)
396{
397 u32 ext_ftr_ptr;
398 u32 ftr_header;
399
400 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
401
402 while (ext_ftr_ptr) {
403 if (local)
404 rio_local_read_config_32(port, ext_ftr_ptr,
405 &ftr_header);
406 else
407 rio_mport_read_config_32(port, destid, hopcount,
408 ext_ftr_ptr, &ftr_header);
409
410 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
411 switch (ftr_header) {
412
413 case RIO_EFB_SER_EP_ID_V13P:
414 case RIO_EFB_SER_EP_REC_ID_V13P:
415 case RIO_EFB_SER_EP_FREE_ID_V13P:
416 case RIO_EFB_SER_EP_ID:
417 case RIO_EFB_SER_EP_REC_ID:
418 case RIO_EFB_SER_EP_FREE_ID:
419 case RIO_EFB_SER_EP_FREC_ID:
420
421 return ext_ftr_ptr;
422
423 default:
424 break;
425 }
426
427 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
428 hopcount, ext_ftr_ptr);
429 }
430
431 return ext_ftr_ptr;
432}
433
434/**
435 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
Randy Dunlap97ef6f72010-05-28 15:08:08 -0700436 * @comp_tag: RIO component tag to match
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700437 * @from: Previous RIO device found in search, or %NULL for new search
438 *
439 * Iterates through the list of known RIO devices. If a RIO device is
440 * found with a matching @comp_tag, a pointer to its device
441 * structure is returned. Otherwise, %NULL is returned. A new search
442 * is initiated by passing %NULL to the @from argument. Otherwise, if
443 * @from is not %NULL, searches continue from next device on the global
444 * list.
445 */
446static struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
447{
448 struct list_head *n;
449 struct rio_dev *rdev;
450
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700451 spin_lock(&rio_global_list_lock);
452 n = from ? from->global_list.next : rio_devices.next;
453
454 while (n && (n != &rio_devices)) {
455 rdev = rio_dev_g(n);
456 if (rdev->comp_tag == comp_tag)
457 goto exit;
458 n = n->next;
459 }
460 rdev = NULL;
461exit:
462 spin_unlock(&rio_global_list_lock);
463 return rdev;
464}
465
466/**
467 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
468 * @rdev: Pointer to RIO device control structure
469 * @pnum: Switch port number to set LOCKOUT bit
470 * @lock: Operation : set (=1) or clear (=0)
471 */
472int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
473{
474 u8 hopcount = 0xff;
475 u16 destid = rdev->destid;
476 u32 regval;
477
478 if (rdev->rswitch) {
479 destid = rdev->rswitch->destid;
480 hopcount = rdev->rswitch->hopcount;
481 }
482
483 rio_mport_read_config_32(rdev->net->hport, destid, hopcount,
484 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
485 &regval);
486 if (lock)
487 regval |= RIO_PORT_N_CTL_LOCKOUT;
488 else
489 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
490
491 rio_mport_write_config_32(rdev->net->hport, destid, hopcount,
492 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
493 regval);
494 return 0;
495}
496
497/**
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700498 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
499 * returns link-response (if requested).
500 * @rdev: RIO devive to issue Input-status command
501 * @pnum: Device port number to issue the command
502 * @lnkresp: Response from a link partner
503 */
504static int
505rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
506{
507 struct rio_mport *mport = rdev->net->hport;
508 u16 destid = rdev->rswitch->destid;
509 u8 hopcount = rdev->rswitch->hopcount;
510 u32 regval;
511 int checkcount;
512
513 if (lnkresp) {
514 /* Read from link maintenance response register
515 * to clear valid bit */
516 rio_mport_read_config_32(mport, destid, hopcount,
517 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
518 &regval);
519 udelay(50);
520 }
521
522 /* Issue Input-status command */
523 rio_mport_write_config_32(mport, destid, hopcount,
524 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
525 RIO_MNT_REQ_CMD_IS);
526
527 /* Exit if the response is not expected */
528 if (lnkresp == NULL)
529 return 0;
530
531 checkcount = 3;
532 while (checkcount--) {
533 udelay(50);
534 rio_mport_read_config_32(mport, destid, hopcount,
535 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
536 &regval);
537 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
538 *lnkresp = regval;
539 return 0;
540 }
541 }
542
543 return -EIO;
544}
545
546/**
547 * rio_clr_err_stopped - Clears port Error-stopped states.
548 * @rdev: Pointer to RIO device control structure
549 * @pnum: Switch port number to clear errors
550 * @err_status: port error status (if 0 reads register from device)
551 */
552static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
553{
554 struct rio_mport *mport = rdev->net->hport;
555 u16 destid = rdev->rswitch->destid;
556 u8 hopcount = rdev->rswitch->hopcount;
557 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
558 u32 regval;
559 u32 far_ackid, far_linkstat, near_ackid;
560
561 if (err_status == 0)
562 rio_mport_read_config_32(mport, destid, hopcount,
563 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
564 &err_status);
565
566 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
567 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
568 /*
569 * Send a Link-Request/Input-Status control symbol
570 */
571 if (rio_get_input_status(rdev, pnum, &regval)) {
572 pr_debug("RIO_EM: Input-status response timeout\n");
573 goto rd_err;
574 }
575
576 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
577 pnum, regval);
578 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
579 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
580 rio_mport_read_config_32(mport, destid, hopcount,
581 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
582 &regval);
583 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
584 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
585 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
586 " near_ackID=0x%02x\n",
587 pnum, far_ackid, far_linkstat, near_ackid);
588
589 /*
590 * If required, synchronize ackIDs of near and
591 * far sides.
592 */
593 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
594 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
595 /* Align near outstanding/outbound ackIDs with
596 * far inbound.
597 */
598 rio_mport_write_config_32(mport, destid,
599 hopcount, rdev->phys_efptr +
600 RIO_PORT_N_ACK_STS_CSR(pnum),
601 (near_ackid << 24) |
602 (far_ackid << 8) | far_ackid);
603 /* Align far outstanding/outbound ackIDs with
604 * near inbound.
605 */
606 far_ackid++;
607 if (nextdev)
608 rio_write_config_32(nextdev,
609 nextdev->phys_efptr +
610 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
611 (far_ackid << 24) |
612 (near_ackid << 8) | near_ackid);
613 else
614 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
615 }
616rd_err:
617 rio_mport_read_config_32(mport, destid, hopcount,
618 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
619 &err_status);
620 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
621 }
622
623 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
624 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
625 rio_get_input_status(nextdev,
626 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
627 udelay(50);
628
629 rio_mport_read_config_32(mport, destid, hopcount,
630 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
631 &err_status);
632 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
633 }
634
635 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
636 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
637}
638
639/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700640 * rio_inb_pwrite_handler - process inbound port-write message
641 * @pw_msg: pointer to inbound port-write message
642 *
643 * Processes an inbound port-write message. Returns 0 if the request
644 * has been satisfied.
645 */
646int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
647{
648 struct rio_dev *rdev;
649 struct rio_mport *mport;
650 u8 hopcount;
651 u16 destid;
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700652 u32 err_status, em_perrdet, em_ltlerrdet;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700653 int rc, portnum;
654
655 rdev = rio_get_comptag(pw_msg->em.comptag, NULL);
656 if (rdev == NULL) {
657 /* Someting bad here (probably enumeration error) */
658 pr_err("RIO: %s No matching device for CTag 0x%08x\n",
659 __func__, pw_msg->em.comptag);
660 return -EIO;
661 }
662
663 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
664
665#ifdef DEBUG_PW
666 {
667 u32 i;
668 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700669 pr_debug("0x%02x: %08x %08x %08x %08x\n",
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700670 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
671 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
672 i += 4;
673 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700674 }
675#endif
676
677 /* Call an external service function (if such is registered
678 * for this device). This may be the service for endpoints that send
679 * device-specific port-write messages. End-point messages expected
680 * to be handled completely by EP specific device driver.
681 * For switches rc==0 signals that no standard processing required.
682 */
683 if (rdev->pwcback != NULL) {
684 rc = rdev->pwcback(rdev, pw_msg, 0);
685 if (rc == 0)
686 return 0;
687 }
688
689 /* For End-point devices processing stops here */
690 if (!(rdev->pef & RIO_PEF_SWITCH))
691 return 0;
692
693 if (rdev->phys_efptr == 0) {
694 pr_err("RIO_PW: Bad switch initialization for %s\n",
695 rio_name(rdev));
696 return 0;
697 }
698
699 mport = rdev->net->hport;
700 destid = rdev->rswitch->destid;
701 hopcount = rdev->rswitch->hopcount;
702
703 /*
704 * Process the port-write notification from switch
705 */
706
707 portnum = pw_msg->em.is_port & 0xFF;
708
709 if (rdev->rswitch->em_handle)
710 rdev->rswitch->em_handle(rdev, portnum);
711
712 rio_mport_read_config_32(mport, destid, hopcount,
713 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
714 &err_status);
715 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
716
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700717 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700718
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700719 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
720 rdev->rswitch->port_ok |= (1 << portnum);
721 rio_set_port_lockout(rdev, portnum, 0);
722 /* Schedule Insertion Service */
723 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
724 rio_name(rdev), portnum);
725 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700726
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700727 /* Clear error-stopped states (if reported).
728 * Depending on the link partner state, two attempts
729 * may be needed for successful recovery.
730 */
731 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
732 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
733 if (rio_clr_err_stopped(rdev, portnum, err_status))
734 rio_clr_err_stopped(rdev, portnum, 0);
735 }
736 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700737
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700738 if (rdev->rswitch->port_ok & (1 << portnum)) {
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700739 rdev->rswitch->port_ok &= ~(1 << portnum);
740 rio_set_port_lockout(rdev, portnum, 1);
741
742 rio_mport_write_config_32(mport, destid, hopcount,
743 rdev->phys_efptr +
744 RIO_PORT_N_ACK_STS_CSR(portnum),
745 RIO_PORT_N_ACK_CLEAR);
746
747 /* Schedule Extraction Service */
748 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
749 rio_name(rdev), portnum);
750 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700751 }
752
Alexandre Bouninedd5648c2010-10-27 15:34:30 -0700753 rio_mport_read_config_32(mport, destid, hopcount,
754 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
755 if (em_perrdet) {
756 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
757 portnum, em_perrdet);
758 /* Clear EM Port N Error Detect CSR */
759 rio_mport_write_config_32(mport, destid, hopcount,
760 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
761 }
762
763 rio_mport_read_config_32(mport, destid, hopcount,
764 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
765 if (em_ltlerrdet) {
766 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
767 em_ltlerrdet);
768 /* Clear EM L/T Layer Error Detect CSR */
769 rio_mport_write_config_32(mport, destid, hopcount,
770 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
771 }
772
773 /* Clear remaining error bits */
774 rio_mport_write_config_32(mport, destid, hopcount,
775 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
776 err_status & RIO_PORT_N_ERR_STS_CLR_MASK);
777
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700778 /* Clear Port-Write Pending bit */
779 rio_mport_write_config_32(mport, destid, hopcount,
780 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
781 RIO_PORT_N_ERR_STS_PW_PEND);
782
783 return 0;
784}
785EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
786
787/**
788 * rio_mport_get_efb - get pointer to next extended features block
789 * @port: Master port to issue transaction
790 * @local: Indicate a local master port or remote device access
791 * @destid: Destination ID of the device
792 * @hopcount: Number of switch hops to the device
793 * @from: Offset of current Extended Feature block header (if 0 starts
794 * from ExtFeaturePtr)
795 */
796u32
797rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
798 u8 hopcount, u32 from)
799{
800 u32 reg_val;
801
802 if (from == 0) {
803 if (local)
804 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
805 &reg_val);
806 else
807 rio_mport_read_config_32(port, destid, hopcount,
808 RIO_ASM_INFO_CAR, &reg_val);
809 return reg_val & RIO_EXT_FTR_PTR_MASK;
810 } else {
811 if (local)
812 rio_local_read_config_32(port, from, &reg_val);
813 else
814 rio_mport_read_config_32(port, destid, hopcount,
815 from, &reg_val);
816 return RIO_GET_BLOCK_ID(reg_val);
817 }
818}
819
820/**
Matt Porter394b7012005-11-07 01:00:15 -0800821 * rio_mport_get_feature - query for devices' extended features
822 * @port: Master port to issue transaction
823 * @local: Indicate a local master port or remote device access
824 * @destid: Destination ID of the device
825 * @hopcount: Number of switch hops to the device
826 * @ftr: Extended feature code
827 *
828 * Tell if a device supports a given RapidIO capability.
829 * Returns the offset of the requested extended feature
830 * block within the device's RIO configuration space or
831 * 0 in case the device does not support it. Possible
832 * values for @ftr:
833 *
834 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
835 *
836 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
837 *
838 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
839 *
840 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
841 *
842 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
843 *
844 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
845 */
846u32
847rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
848 u8 hopcount, int ftr)
849{
850 u32 asm_info, ext_ftr_ptr, ftr_header;
851
852 if (local)
853 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
854 else
855 rio_mport_read_config_32(port, destid, hopcount,
856 RIO_ASM_INFO_CAR, &asm_info);
857
858 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
859
860 while (ext_ftr_ptr) {
861 if (local)
862 rio_local_read_config_32(port, ext_ftr_ptr,
863 &ftr_header);
864 else
865 rio_mport_read_config_32(port, destid, hopcount,
866 ext_ftr_ptr, &ftr_header);
867 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
868 return ext_ftr_ptr;
869 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
870 break;
871 }
872
873 return 0;
874}
875
876/**
877 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
878 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
879 * @did: RIO did to match or %RIO_ANY_ID to match all dids
880 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
881 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
882 * @from: Previous RIO device found in search, or %NULL for new search
883 *
884 * Iterates through the list of known RIO devices. If a RIO device is
885 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
886 * count to the device is incrememted and a pointer to its device
887 * structure is returned. Otherwise, %NULL is returned. A new search
888 * is initiated by passing %NULL to the @from argument. Otherwise, if
889 * @from is not %NULL, searches continue from next device on the global
890 * list. The reference count for @from is always decremented if it is
891 * not %NULL.
892 */
893struct rio_dev *rio_get_asm(u16 vid, u16 did,
894 u16 asm_vid, u16 asm_did, struct rio_dev *from)
895{
896 struct list_head *n;
897 struct rio_dev *rdev;
898
899 WARN_ON(in_interrupt());
900 spin_lock(&rio_global_list_lock);
901 n = from ? from->global_list.next : rio_devices.next;
902
903 while (n && (n != &rio_devices)) {
904 rdev = rio_dev_g(n);
905 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
906 (did == RIO_ANY_ID || rdev->did == did) &&
907 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
908 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
909 goto exit;
910 n = n->next;
911 }
912 rdev = NULL;
913 exit:
914 rio_dev_put(from);
915 rdev = rio_dev_get(rdev);
916 spin_unlock(&rio_global_list_lock);
917 return rdev;
918}
919
920/**
921 * rio_get_device - Begin or continue searching for a RIO device by vid/did
922 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
923 * @did: RIO did to match or %RIO_ANY_ID to match all dids
924 * @from: Previous RIO device found in search, or %NULL for new search
925 *
926 * Iterates through the list of known RIO devices. If a RIO device is
927 * found with a matching @vid and @did, the reference count to the
928 * device is incrememted and a pointer to its device structure is returned.
929 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
930 * to the @from argument. Otherwise, if @from is not %NULL, searches
931 * continue from next device on the global list. The reference count for
932 * @from is always decremented if it is not %NULL.
933 */
934struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
935{
936 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
937}
938
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700939/**
940 * rio_std_route_add_entry - Add switch route table entry using standard
941 * registers defined in RIO specification rev.1.3
942 * @mport: Master port to issue transaction
943 * @destid: Destination ID of the device
944 * @hopcount: Number of switch hops to the device
945 * @table: routing table ID (global or port-specific)
946 * @route_destid: destID entry in the RT
947 * @route_port: destination port for specified destID
948 */
949int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
950 u16 table, u16 route_destid, u8 route_port)
951{
952 if (table == RIO_GLOBAL_TABLE) {
953 rio_mport_write_config_32(mport, destid, hopcount,
954 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
955 (u32)route_destid);
956 rio_mport_write_config_32(mport, destid, hopcount,
957 RIO_STD_RTE_CONF_PORT_SEL_CSR,
958 (u32)route_port);
959 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700960
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700961 udelay(10);
962 return 0;
963}
964
965/**
966 * rio_std_route_get_entry - Read switch route table entry (port number)
Uwe Kleine-König638c5942010-06-11 12:16:58 +0200967 * associated with specified destID using standard registers defined in RIO
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700968 * specification rev.1.3
969 * @mport: Master port to issue transaction
970 * @destid: Destination ID of the device
971 * @hopcount: Number of switch hops to the device
972 * @table: routing table ID (global or port-specific)
973 * @route_destid: destID entry in the RT
974 * @route_port: returned destination port for specified destID
975 */
976int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
977 u16 table, u16 route_destid, u8 *route_port)
978{
979 u32 result;
980
981 if (table == RIO_GLOBAL_TABLE) {
982 rio_mport_write_config_32(mport, destid, hopcount,
983 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
984 rio_mport_read_config_32(mport, destid, hopcount,
985 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
986
987 *route_port = (u8)result;
988 }
989
990 return 0;
991}
992
993/**
994 * rio_std_route_clr_table - Clear swotch route table using standard registers
995 * defined in RIO specification rev.1.3.
996 * @mport: Master port to issue transaction
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700997 * @destid: Destination ID of the device
998 * @hopcount: Number of switch hops to the device
999 * @table: routing table ID (global or port-specific)
1000 */
1001int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1002 u16 table)
1003{
1004 u32 max_destid = 0xff;
1005 u32 i, pef, id_inc = 1, ext_cfg = 0;
1006 u32 port_sel = RIO_INVALID_ROUTE;
1007
1008 if (table == RIO_GLOBAL_TABLE) {
1009 rio_mport_read_config_32(mport, destid, hopcount,
1010 RIO_PEF_CAR, &pef);
1011
1012 if (mport->sys_size) {
1013 rio_mport_read_config_32(mport, destid, hopcount,
1014 RIO_SWITCH_RT_LIMIT,
1015 &max_destid);
1016 max_destid &= RIO_RT_MAX_DESTID;
1017 }
1018
1019 if (pef & RIO_PEF_EXT_RT) {
1020 ext_cfg = 0x80000000;
1021 id_inc = 4;
1022 port_sel = (RIO_INVALID_ROUTE << 24) |
1023 (RIO_INVALID_ROUTE << 16) |
1024 (RIO_INVALID_ROUTE << 8) |
1025 RIO_INVALID_ROUTE;
1026 }
1027
1028 for (i = 0; i <= max_destid;) {
1029 rio_mport_write_config_32(mport, destid, hopcount,
1030 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1031 ext_cfg | i);
1032 rio_mport_write_config_32(mport, destid, hopcount,
1033 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1034 port_sel);
1035 i += id_inc;
1036 }
1037 }
1038
1039 udelay(10);
1040 return 0;
1041}
1042
Matt Porter394b7012005-11-07 01:00:15 -08001043static void rio_fixup_device(struct rio_dev *dev)
1044{
1045}
1046
1047static int __devinit rio_init(void)
1048{
1049 struct rio_dev *dev = NULL;
1050
1051 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1052 rio_fixup_device(dev);
1053 }
1054 return 0;
1055}
1056
1057device_initcall(rio_init);
1058
Al Viro37d33d12008-11-22 17:36:24 +00001059int __devinit rio_init_mports(void)
Matt Porter394b7012005-11-07 01:00:15 -08001060{
1061 int rc = 0;
1062 struct rio_mport *port;
1063
1064 list_for_each_entry(port, &rio_mports, node) {
1065 if (!request_mem_region(port->iores.start,
1066 port->iores.end - port->iores.start,
1067 port->name)) {
1068 printk(KERN_ERR
Kumar Gala5febf1c2008-01-23 05:53:47 -06001069 "RIO: Error requesting master port region 0x%016llx-0x%016llx\n",
1070 (u64)port->iores.start, (u64)port->iores.end - 1);
Matt Porter394b7012005-11-07 01:00:15 -08001071 rc = -ENOMEM;
1072 goto out;
1073 }
1074
1075 if (port->host_deviceid >= 0)
1076 rio_enum_mport(port);
1077 else
1078 rio_disc_mport(port);
1079 }
1080
1081 out:
1082 return rc;
1083}
1084
1085void rio_register_mport(struct rio_mport *port)
1086{
1087 list_add_tail(&port->node, &rio_mports);
1088}
1089
1090EXPORT_SYMBOL_GPL(rio_local_get_device_id);
1091EXPORT_SYMBOL_GPL(rio_get_device);
1092EXPORT_SYMBOL_GPL(rio_get_asm);
1093EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
1094EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
1095EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
1096EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
1097EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
1098EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
1099EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
1100EXPORT_SYMBOL_GPL(rio_release_outb_mbox);