1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * RapidIO interconnect services
4  * (RapidIO Interconnect Specification, http://www.rapidio.org)
5  *
6  * Copyright 2005 MontaVista Software, Inc.
7  * Matt Porter <mporter@kernel.crashing.org>
8  */
9 
10 #ifndef LINUX_RIO_H
11 #define LINUX_RIO_H
12 
13 #include <linux/types.h>
14 #include <linux/ioport.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/device.h>
18 #include <linux/rio_regs.h>
19 #include <linux/mod_devicetable.h>
20 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
21 #include <linux/dmaengine.h>
22 #endif
23 
24 #define RIO_NO_HOPCOUNT		-1
25 #define RIO_INVALID_DESTID	0xffff
26 
27 #define RIO_MAX_MPORTS		8
28 #define RIO_MAX_MPORT_RESOURCES	16
29 #define RIO_MAX_DEV_RESOURCES	16
30 #define RIO_MAX_MPORT_NAME	40
31 
32 #define RIO_GLOBAL_TABLE	0xff	/* Indicates access of a switch's
33 					   global routing table if it
34 					   has multiple (or per port)
35 					   tables */
36 
37 #define RIO_INVALID_ROUTE	0xff	/* Indicates that a route table
38 					   entry is invalid (no route
39 					   exists for the device ID) */
40 
41 #define RIO_MAX_ROUTE_ENTRIES(size)	(size ? (1 << 16) : (1 << 8))
42 #define RIO_ANY_DESTID(size)		(size ? 0xffff : 0xff)
43 
44 #define RIO_MAX_MBOX		4
45 #define RIO_MAX_MSG_SIZE	0x1000
46 
47 /*
48  * Error values that may be returned by RIO functions.
49  */
50 #define RIO_SUCCESSFUL			0x00
51 #define RIO_BAD_SIZE			0x81
52 
53 /*
54  * For RIO devices, the region numbers are assigned this way:
55  *
56  *	0	RapidIO outbound doorbells
57  *      1-15	RapidIO memory regions
58  *
59  * For RIO master ports, the region number are assigned this way:
60  *
61  *	0	RapidIO inbound doorbells
62  *	1	RapidIO inbound mailboxes
63  *	2	RapidIO outbound mailboxes
64  */
65 #define RIO_DOORBELL_RESOURCE	0
66 #define RIO_INB_MBOX_RESOURCE	1
67 #define RIO_OUTB_MBOX_RESOURCE	2
68 
69 #define RIO_PW_MSG_SIZE		64
70 
71 /*
72  * A component tag value (stored in the component tag CSR) is used as device's
73  * unique identifier assigned during enumeration. Besides being used for
74  * identifying switches (which do not have device ID register), it also is used
75  * by error management notification and therefore has to be assigned
76  * to endpoints as well.
77  */
78 #define RIO_CTAG_RESRVD	0xfffe0000 /* Reserved */
79 #define RIO_CTAG_UDEVID	0x0001ffff /* Unique device identifier */
80 
81 extern struct bus_type rio_bus_type;
82 extern struct class rio_mport_class;
83 
84 struct rio_mport;
85 struct rio_dev;
86 union rio_pw_msg;
87 
88 /**
89  * struct rio_switch - RIO switch info
90  * @node: Node in global list of switches
91  * @route_table: Copy of switch routing table
92  * @port_ok: Status of each port (one bit per port) - OK=1 or UNINIT=0
93  * @ops: pointer to switch-specific operations
94  * @lock: lock to serialize operations updates
95  * @nextdev: Array of per-port pointers to the next attached device
96  */
97 struct rio_switch {
98 	struct list_head node;
99 	u8 *route_table;
100 	u32 port_ok;
101 	struct rio_switch_ops *ops;
102 	spinlock_t lock;
103 	struct rio_dev *nextdev[];
104 };
105 
106 /**
107  * struct rio_switch_ops - Per-switch operations
108  * @owner: The module owner of this structure
109  * @add_entry: Callback for switch-specific route add function
110  * @get_entry: Callback for switch-specific route get function
111  * @clr_table: Callback for switch-specific clear route table function
112  * @set_domain: Callback for switch-specific domain setting function
113  * @get_domain: Callback for switch-specific domain get function
114  * @em_init: Callback for switch-specific error management init function
115  * @em_handle: Callback for switch-specific error management handler function
116  *
117  * Defines the operations that are necessary to initialize/control
118  * a particular RIO switch device.
119  */
120 struct rio_switch_ops {
121 	struct module *owner;
122 	int (*add_entry) (struct rio_mport *mport, u16 destid, u8 hopcount,
123 			  u16 table, u16 route_destid, u8 route_port);
124 	int (*get_entry) (struct rio_mport *mport, u16 destid, u8 hopcount,
125 			  u16 table, u16 route_destid, u8 *route_port);
126 	int (*clr_table) (struct rio_mport *mport, u16 destid, u8 hopcount,
127 			  u16 table);
128 	int (*set_domain) (struct rio_mport *mport, u16 destid, u8 hopcount,
129 			   u8 sw_domain);
130 	int (*get_domain) (struct rio_mport *mport, u16 destid, u8 hopcount,
131 			   u8 *sw_domain);
132 	int (*em_init) (struct rio_dev *dev);
133 	int (*em_handle) (struct rio_dev *dev, u8 swport);
134 };
135 
136 enum rio_device_state {
137 	RIO_DEVICE_INITIALIZING,
138 	RIO_DEVICE_RUNNING,
139 	RIO_DEVICE_GONE,
140 	RIO_DEVICE_SHUTDOWN,
141 };
142 
143 /**
144  * struct rio_dev - RIO device info
145  * @global_list: Node in list of all RIO devices
146  * @net_list: Node in list of RIO devices in a network
147  * @net: Network this device is a part of
148  * @do_enum: Enumeration flag
149  * @did: Device ID
150  * @vid: Vendor ID
151  * @device_rev: Device revision
152  * @asm_did: Assembly device ID
153  * @asm_vid: Assembly vendor ID
154  * @asm_rev: Assembly revision
155  * @efptr: Extended feature pointer
156  * @pef: Processing element features
157  * @swpinfo: Switch port info
158  * @src_ops: Source operation capabilities
159  * @dst_ops: Destination operation capabilities
160  * @comp_tag: RIO component tag
161  * @phys_efptr: RIO device extended features pointer
162  * @phys_rmap: LP-Serial Register Map Type (1 or 2)
163  * @em_efptr: RIO Error Management features pointer
164  * @dma_mask: Mask of bits of RIO address this device implements
165  * @driver: Driver claiming this device
166  * @dev: Device model device
167  * @riores: RIO resources this device owns
168  * @pwcback: port-write callback function for this device
169  * @destid: Network destination ID (or associated destid for switch)
170  * @hopcount: Hopcount to this device
171  * @prev: Previous RIO device connected to the current one
172  * @state: device state
173  * @rswitch: struct rio_switch (if valid for this device)
174  */
175 struct rio_dev {
176 	struct list_head global_list;	/* node in list of all RIO devices */
177 	struct list_head net_list;	/* node in per net list */
178 	struct rio_net *net;	/* RIO net this device resides in */
179 	bool do_enum;
180 	u16 did;
181 	u16 vid;
182 	u32 device_rev;
183 	u16 asm_did;
184 	u16 asm_vid;
185 	u16 asm_rev;
186 	u16 efptr;
187 	u32 pef;
188 	u32 swpinfo;
189 	u32 src_ops;
190 	u32 dst_ops;
191 	u32 comp_tag;
192 	u32 phys_efptr;
193 	u32 phys_rmap;
194 	u32 em_efptr;
195 	u64 dma_mask;
196 	struct rio_driver *driver;	/* RIO driver claiming this device */
197 	struct device dev;	/* LDM device structure */
198 	struct resource riores[RIO_MAX_DEV_RESOURCES];
199 	int (*pwcback) (struct rio_dev *rdev, union rio_pw_msg *msg, int step);
200 	u16 destid;
201 	u8 hopcount;
202 	struct rio_dev *prev;
203 	atomic_t state;
204 	struct rio_switch rswitch[];	/* RIO switch info */
205 };
206 
207 #define rio_dev_g(n) list_entry(n, struct rio_dev, global_list)
208 #define rio_dev_f(n) list_entry(n, struct rio_dev, net_list)
209 #define	to_rio_dev(n) container_of(n, struct rio_dev, dev)
210 #define sw_to_rio_dev(n) container_of(n, struct rio_dev, rswitch[0])
211 #define	to_rio_mport(n) container_of(n, struct rio_mport, dev)
212 #define	to_rio_net(n) container_of(n, struct rio_net, dev)
213 
214 /**
215  * struct rio_msg - RIO message event
216  * @res: Mailbox resource
217  * @mcback: Message event callback
218  */
219 struct rio_msg {
220 	struct resource *res;
221 	void (*mcback) (struct rio_mport * mport, void *dev_id, int mbox, int slot);
222 };
223 
224 /**
225  * struct rio_dbell - RIO doorbell event
226  * @node: Node in list of doorbell events
227  * @res: Doorbell resource
228  * @dinb: Doorbell event callback
229  * @dev_id: Device specific pointer to pass on event
230  */
231 struct rio_dbell {
232 	struct list_head node;
233 	struct resource *res;
234 	void (*dinb) (struct rio_mport *mport, void *dev_id, u16 src, u16 dst, u16 info);
235 	void *dev_id;
236 };
237 
238 /**
239  * struct rio_mport - RIO master port info
240  * @dbells: List of doorbell events
241  * @pwrites: List of portwrite events
242  * @node: Node in global list of master ports
243  * @nnode: Node in network list of master ports
244  * @net: RIO net this mport is attached to
245  * @lock: lock to synchronize lists manipulations
246  * @iores: I/O mem resource that this master port interface owns
247  * @riores: RIO resources that this master port interfaces owns
248  * @inb_msg: RIO inbound message event descriptors
249  * @outb_msg: RIO outbound message event descriptors
250  * @host_deviceid: Host device ID associated with this master port
251  * @ops: configuration space functions
252  * @id: Port ID, unique among all ports
253  * @index: Port index, unique among all port interfaces of the same type
254  * @sys_size: RapidIO common transport system size
255  * @phys_efptr: RIO port extended features pointer
256  * @phys_rmap: LP-Serial EFB Register Mapping type (1 or 2).
257  * @name: Port name string
258  * @dev: device structure associated with an mport
259  * @priv: Master port private data
260  * @dma: DMA device associated with mport
261  * @nscan: RapidIO network enumeration/discovery operations
262  * @state: mport device state
263  * @pwe_refcnt: port-write enable ref counter to track enable/disable requests
264  */
265 struct rio_mport {
266 	struct list_head dbells;	/* list of doorbell events */
267 	struct list_head pwrites;	/* list of portwrite events */
268 	struct list_head node;	/* node in global list of ports */
269 	struct list_head nnode;	/* node in net list of ports */
270 	struct rio_net *net;	/* RIO net this mport is attached to */
271 	struct mutex lock;
272 	struct resource iores;
273 	struct resource riores[RIO_MAX_MPORT_RESOURCES];
274 	struct rio_msg inb_msg[RIO_MAX_MBOX];
275 	struct rio_msg outb_msg[RIO_MAX_MBOX];
276 	int host_deviceid;	/* Host device ID */
277 	struct rio_ops *ops;	/* low-level architecture-dependent routines */
278 	unsigned char id;	/* port ID, unique among all ports */
279 	unsigned char index;	/* port index, unique among all port
280 				   interfaces of the same type */
281 	unsigned int sys_size;	/* RapidIO common transport system size.
282 				 * 0 - Small size. 256 devices.
283 				 * 1 - Large size, 65536 devices.
284 				 */
285 	u32 phys_efptr;
286 	u32 phys_rmap;
287 	unsigned char name[RIO_MAX_MPORT_NAME];
288 	struct device dev;
289 	void *priv;		/* Master port private data */
290 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
291 	struct dma_device	dma;
292 #endif
293 	struct rio_scan *nscan;
294 	atomic_t state;
295 	unsigned int pwe_refcnt;
296 };
297 
rio_mport_is_running(struct rio_mport * mport)298 static inline int rio_mport_is_running(struct rio_mport *mport)
299 {
300 	return atomic_read(&mport->state) == RIO_DEVICE_RUNNING;
301 }
302 
303 /*
304  * Enumeration/discovery control flags
305  */
306 #define RIO_SCAN_ENUM_NO_WAIT	0x00000001 /* Do not wait for enum completed */
307 
308 /**
309  * struct rio_net - RIO network info
310  * @node: Node in global list of RIO networks
311  * @devices: List of devices in this network
312  * @switches: List of switches in this network
313  * @mports: List of master ports accessing this network
314  * @hport: Default port for accessing this network
315  * @id: RIO network ID
316  * @dev: Device object
317  * @enum_data: private data specific to a network enumerator
318  * @release: enumerator-specific release callback
319  */
320 struct rio_net {
321 	struct list_head node;	/* node in list of networks */
322 	struct list_head devices;	/* list of devices in this net */
323 	struct list_head switches;	/* list of switches in this net */
324 	struct list_head mports;	/* list of ports accessing net */
325 	struct rio_mport *hport;	/* primary port for accessing net */
326 	unsigned char id;	/* RIO network ID */
327 	struct device dev;
328 	void *enum_data;	/* private data for enumerator of the network */
329 	void (*release)(struct rio_net *net);
330 };
331 
332 enum rio_link_speed {
333 	RIO_LINK_DOWN = 0, /* SRIO Link not initialized */
334 	RIO_LINK_125 = 1, /* 1.25 GBaud  */
335 	RIO_LINK_250 = 2, /* 2.5 GBaud   */
336 	RIO_LINK_312 = 3, /* 3.125 GBaud */
337 	RIO_LINK_500 = 4, /* 5.0 GBaud   */
338 	RIO_LINK_625 = 5  /* 6.25 GBaud  */
339 };
340 
341 enum rio_link_width {
342 	RIO_LINK_1X  = 0,
343 	RIO_LINK_1XR = 1,
344 	RIO_LINK_2X  = 3,
345 	RIO_LINK_4X  = 2,
346 	RIO_LINK_8X  = 4,
347 	RIO_LINK_16X = 5
348 };
349 
350 enum rio_mport_flags {
351 	RIO_MPORT_DMA	 = (1 << 0), /* supports DMA data transfers */
352 	RIO_MPORT_DMA_SG = (1 << 1), /* DMA supports HW SG mode */
353 	RIO_MPORT_IBSG	 = (1 << 2), /* inbound mapping supports SG */
354 };
355 
356 /**
357  * struct rio_mport_attr - RIO mport device attributes
358  * @flags: mport device capability flags
359  * @link_speed: SRIO link speed value (as defined by RapidIO specification)
360  * @link_width:	SRIO link width value (as defined by RapidIO specification)
361  * @dma_max_sge: number of SG list entries that can be handled by DMA channel(s)
362  * @dma_max_size: max number of bytes in single DMA transfer (SG entry)
363  * @dma_align: alignment shift for DMA operations (as for other DMA operations)
364  */
365 struct rio_mport_attr {
366 	int flags;
367 	int link_speed;
368 	int link_width;
369 
370 	/* DMA capability info: valid only if RIO_MPORT_DMA flag is set */
371 	int dma_max_sge;
372 	int dma_max_size;
373 	int dma_align;
374 };
375 
376 /* Low-level architecture-dependent routines */
377 
378 /**
379  * struct rio_ops - Low-level RIO configuration space operations
380  * @lcread: Callback to perform local (master port) read of config space.
381  * @lcwrite: Callback to perform local (master port) write of config space.
382  * @cread: Callback to perform network read of config space.
383  * @cwrite: Callback to perform network write of config space.
384  * @dsend: Callback to send a doorbell message.
385  * @pwenable: Callback to enable/disable port-write message handling.
386  * @open_outb_mbox: Callback to initialize outbound mailbox.
387  * @close_outb_mbox: Callback to shut down outbound mailbox.
388  * @open_inb_mbox: Callback to initialize inbound mailbox.
389  * @close_inb_mbox: Callback to	shut down inbound mailbox.
390  * @add_outb_message: Callback to add a message to an outbound mailbox queue.
391  * @add_inb_buffer: Callback to	add a buffer to an inbound mailbox queue.
392  * @get_inb_message: Callback to get a message from an inbound mailbox queue.
393  * @map_inb: Callback to map RapidIO address region into local memory space.
394  * @unmap_inb: Callback to unmap RapidIO address region mapped with map_inb().
395  * @query_mport: Callback to query mport device attributes.
396  * @map_outb: Callback to map outbound address region into local memory space.
397  * @unmap_outb: Callback to unmap outbound RapidIO address region.
398  */
399 struct rio_ops {
400 	int (*lcread) (struct rio_mport *mport, int index, u32 offset, int len,
401 			u32 *data);
402 	int (*lcwrite) (struct rio_mport *mport, int index, u32 offset, int len,
403 			u32 data);
404 	int (*cread) (struct rio_mport *mport, int index, u16 destid,
405 			u8 hopcount, u32 offset, int len, u32 *data);
406 	int (*cwrite) (struct rio_mport *mport, int index, u16 destid,
407 			u8 hopcount, u32 offset, int len, u32 data);
408 	int (*dsend) (struct rio_mport *mport, int index, u16 destid, u16 data);
409 	int (*pwenable) (struct rio_mport *mport, int enable);
410 	int (*open_outb_mbox)(struct rio_mport *mport, void *dev_id,
411 			      int mbox, int entries);
412 	void (*close_outb_mbox)(struct rio_mport *mport, int mbox);
413 	int  (*open_inb_mbox)(struct rio_mport *mport, void *dev_id,
414 			     int mbox, int entries);
415 	void (*close_inb_mbox)(struct rio_mport *mport, int mbox);
416 	int  (*add_outb_message)(struct rio_mport *mport, struct rio_dev *rdev,
417 				 int mbox, void *buffer, size_t len);
418 	int (*add_inb_buffer)(struct rio_mport *mport, int mbox, void *buf);
419 	void *(*get_inb_message)(struct rio_mport *mport, int mbox);
420 	int (*map_inb)(struct rio_mport *mport, dma_addr_t lstart,
421 			u64 rstart, u64 size, u32 flags);
422 	void (*unmap_inb)(struct rio_mport *mport, dma_addr_t lstart);
423 	int (*query_mport)(struct rio_mport *mport,
424 			   struct rio_mport_attr *attr);
425 	int (*map_outb)(struct rio_mport *mport, u16 destid, u64 rstart,
426 			u32 size, u32 flags, dma_addr_t *laddr);
427 	void (*unmap_outb)(struct rio_mport *mport, u16 destid, u64 rstart);
428 };
429 
430 #define RIO_RESOURCE_MEM	0x00000100
431 #define RIO_RESOURCE_DOORBELL	0x00000200
432 #define RIO_RESOURCE_MAILBOX	0x00000400
433 
434 #define RIO_RESOURCE_CACHEABLE	0x00010000
435 #define RIO_RESOURCE_PCI	0x00020000
436 
437 #define RIO_RESOURCE_BUSY	0x80000000
438 
439 /**
440  * struct rio_driver - RIO driver info
441  * @node: Node in list of drivers
442  * @name: RIO driver name
443  * @id_table: RIO device ids to be associated with this driver
444  * @probe: RIO device inserted
445  * @remove: RIO device removed
446  * @shutdown: shutdown notification callback
447  * @suspend: RIO device suspended
448  * @resume: RIO device awakened
449  * @enable_wake: RIO device enable wake event
450  * @driver: LDM driver struct
451  *
452  * Provides info on a RIO device driver for insertion/removal and
453  * power management purposes.
454  */
455 struct rio_driver {
456 	struct list_head node;
457 	char *name;
458 	const struct rio_device_id *id_table;
459 	int (*probe) (struct rio_dev * dev, const struct rio_device_id * id);
460 	void (*remove) (struct rio_dev * dev);
461 	void (*shutdown)(struct rio_dev *dev);
462 	int (*suspend) (struct rio_dev * dev, u32 state);
463 	int (*resume) (struct rio_dev * dev);
464 	int (*enable_wake) (struct rio_dev * dev, u32 state, int enable);
465 	struct device_driver driver;
466 };
467 
468 #define	to_rio_driver(drv) container_of_const(drv,struct rio_driver, driver)
469 
470 union rio_pw_msg {
471 	struct {
472 		u32 comptag;	/* Component Tag CSR */
473 		u32 errdetect;	/* Port N Error Detect CSR */
474 		u32 is_port;	/* Implementation specific + PortID */
475 		u32 ltlerrdet;	/* LTL Error Detect CSR */
476 		u32 padding[12];
477 	} em;
478 	u32 raw[RIO_PW_MSG_SIZE/sizeof(u32)];
479 };
480 
481 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
482 
483 /*
484  * enum rio_write_type - RIO write transaction types used in DMA transfers
485  *
486  * Note: RapidIO specification defines write (NWRITE) and
487  * write-with-response (NWRITE_R) data transfer operations.
488  * Existing DMA controllers that service RapidIO may use one of these operations
489  * for entire data transfer or their combination with only the last data packet
490  * requires response.
491  */
492 enum rio_write_type {
493 	RDW_DEFAULT,		/* default method used by DMA driver */
494 	RDW_ALL_NWRITE,		/* all packets use NWRITE */
495 	RDW_ALL_NWRITE_R,	/* all packets use NWRITE_R */
496 	RDW_LAST_NWRITE_R,	/* last packet uses NWRITE_R, others - NWRITE */
497 };
498 
499 struct rio_dma_ext {
500 	u16 destid;
501 	u64 rio_addr;	/* low 64-bits of 66-bit RapidIO address */
502 	u8  rio_addr_u;  /* upper 2-bits of 66-bit RapidIO address */
503 	enum rio_write_type wr_type; /* preferred RIO write operation type */
504 };
505 
506 struct rio_dma_data {
507 	/* Local data (as scatterlist) */
508 	struct scatterlist	*sg;	/* I/O scatter list */
509 	unsigned int		sg_len;	/* size of scatter list */
510 	/* Remote device address (flat buffer) */
511 	u64 rio_addr;	/* low 64-bits of 66-bit RapidIO address */
512 	u8  rio_addr_u;  /* upper 2-bits of 66-bit RapidIO address */
513 	enum rio_write_type wr_type; /* preferred RIO write operation type */
514 };
515 
dma_to_mport(struct dma_device * ddev)516 static inline struct rio_mport *dma_to_mport(struct dma_device *ddev)
517 {
518 	return container_of(ddev, struct rio_mport, dma);
519 }
520 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
521 
522 /**
523  * struct rio_scan - RIO enumeration and discovery operations
524  * @owner: The module owner of this structure
525  * @enumerate: Callback to perform RapidIO fabric enumeration.
526  * @discover: Callback to perform RapidIO fabric discovery.
527  */
528 struct rio_scan {
529 	struct module *owner;
530 	int (*enumerate)(struct rio_mport *mport, u32 flags);
531 	int (*discover)(struct rio_mport *mport, u32 flags);
532 };
533 
534 /**
535  * struct rio_scan_node - list node to register RapidIO enumeration and
536  * discovery methods with RapidIO core.
537  * @mport_id: ID of an mport (net) serviced by this enumerator
538  * @node: node in global list of registered enumerators
539  * @ops: RIO enumeration and discovery operations
540  */
541 struct rio_scan_node {
542 	int mport_id;
543 	struct list_head node;
544 	struct rio_scan *ops;
545 };
546 
547 /* Architecture and hardware-specific functions */
548 extern int rio_mport_initialize(struct rio_mport *);
549 extern int rio_register_mport(struct rio_mport *);
550 extern int rio_unregister_mport(struct rio_mport *);
551 extern int rio_open_inb_mbox(struct rio_mport *, void *, int, int);
552 extern void rio_close_inb_mbox(struct rio_mport *, int);
553 extern int rio_open_outb_mbox(struct rio_mport *, void *, int, int);
554 extern void rio_close_outb_mbox(struct rio_mport *, int);
555 extern int rio_query_mport(struct rio_mport *port,
556 			   struct rio_mport_attr *mport_attr);
557 
558 #endif				/* LINUX_RIO_H */
559