xref: /openbmc/linux/arch/sparc/include/asm/vio.h (revision 711aab1d)
1 #ifndef _SPARC64_VIO_H
2 #define _SPARC64_VIO_H
3 
4 #include <linux/kernel.h>
5 #include <linux/device.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/timer.h>
8 #include <linux/spinlock.h>
9 #include <linux/completion.h>
10 #include <linux/list.h>
11 #include <linux/log2.h>
12 
13 #include <asm/ldc.h>
14 #include <asm/mdesc.h>
15 
16 struct vio_msg_tag {
17 	u8			type;
18 #define VIO_TYPE_CTRL		0x01
19 #define VIO_TYPE_DATA		0x02
20 #define VIO_TYPE_ERR		0x04
21 
22 	u8			stype;
23 #define VIO_SUBTYPE_INFO	0x01
24 #define VIO_SUBTYPE_ACK		0x02
25 #define VIO_SUBTYPE_NACK	0x04
26 
27 	u16			stype_env;
28 #define VIO_VER_INFO		0x0001
29 #define VIO_ATTR_INFO		0x0002
30 #define VIO_DRING_REG		0x0003
31 #define VIO_DRING_UNREG		0x0004
32 #define VIO_RDX			0x0005
33 #define VIO_PKT_DATA		0x0040
34 #define VIO_DESC_DATA		0x0041
35 #define VIO_DRING_DATA		0x0042
36 #define VNET_MCAST_INFO		0x0101
37 
38 	u32		sid;
39 };
40 
41 struct vio_rdx {
42 	struct vio_msg_tag	tag;
43 	u64			resv[6];
44 };
45 
46 struct vio_ver_info {
47 	struct vio_msg_tag	tag;
48 	u16			major;
49 	u16			minor;
50 	u8			dev_class;
51 #define VDEV_NETWORK		0x01
52 #define VDEV_NETWORK_SWITCH	0x02
53 #define VDEV_DISK		0x03
54 #define VDEV_DISK_SERVER	0x04
55 #define VDEV_CONSOLE_CON	0x05
56 
57 	u8			resv1[3];
58 	u64			resv2[5];
59 };
60 
61 struct vio_dring_register {
62 	struct vio_msg_tag	tag;
63 	u64			dring_ident;
64 	u32			num_descr;
65 	u32			descr_size;
66 	u16			options;
67 #define VIO_TX_DRING		0x0001
68 #define VIO_RX_DRING		0x0002
69 #define VIO_RX_DRING_DATA	0x0004
70 	u16			resv;
71 	u32			num_cookies;
72 	struct ldc_trans_cookie	cookies[0];
73 };
74 
75 struct vio_dring_unregister {
76 	struct vio_msg_tag	tag;
77 	u64			dring_ident;
78 	u64			resv[5];
79 };
80 
81 /* Data transfer modes */
82 #define VIO_PKT_MODE		0x01 /* Packet based transfer	*/
83 #define VIO_DESC_MODE		0x02 /* In-band descriptors	*/
84 #define VIO_DRING_MODE		0x03 /* Descriptor rings	*/
85 /* in vers >= 1.2, VIO_DRING_MODE is 0x04 and transfer mode is a bitmask */
86 #define VIO_NEW_DRING_MODE	0x04
87 
88 struct vio_dring_data {
89 	struct vio_msg_tag	tag;
90 	u64			seq;
91 	u64			dring_ident;
92 	u32			start_idx;
93 	u32			end_idx;
94 	u8			state;
95 #define VIO_DRING_ACTIVE	0x01
96 #define VIO_DRING_STOPPED	0x02
97 
98 	u8			__pad1;
99 	u16			__pad2;
100 	u32			__pad3;
101 	u64			__par4[2];
102 };
103 
104 struct vio_dring_hdr {
105 	u8			state;
106 #define VIO_DESC_FREE		0x01
107 #define VIO_DESC_READY		0x02
108 #define VIO_DESC_ACCEPTED	0x03
109 #define VIO_DESC_DONE		0x04
110 	u8			ack;
111 #define VIO_ACK_ENABLE		0x01
112 #define VIO_ACK_DISABLE		0x00
113 
114 	u16			__pad1;
115 	u32			__pad2;
116 };
117 
118 /* VIO disk specific structures and defines */
119 struct vio_disk_attr_info {
120 	struct vio_msg_tag	tag;
121 	u8			xfer_mode;
122 	u8			vdisk_type;
123 #define VD_DISK_TYPE_SLICE	0x01 /* Slice in block device	*/
124 #define VD_DISK_TYPE_DISK	0x02 /* Entire block device	*/
125 	u8			vdisk_mtype;		/* v1.1 */
126 #define VD_MEDIA_TYPE_FIXED	0x01 /* Fixed device */
127 #define VD_MEDIA_TYPE_CD	0x02 /* CD Device    */
128 #define VD_MEDIA_TYPE_DVD	0x03 /* DVD Device   */
129 	u8			resv1;
130 	u32			vdisk_block_size;
131 	u64			operations;
132 	u64			vdisk_size;		/* v1.1 */
133 	u64			max_xfer_size;
134 	u32			phys_block_size;	/* v1.2 */
135 	u32			resv2;
136 	u64			resv3[1];
137 };
138 
139 struct vio_disk_desc {
140 	struct vio_dring_hdr	hdr;
141 	u64			req_id;
142 	u8			operation;
143 #define VD_OP_BREAD		0x01 /* Block read			*/
144 #define VD_OP_BWRITE		0x02 /* Block write			*/
145 #define VD_OP_FLUSH		0x03 /* Flush disk contents		*/
146 #define VD_OP_GET_WCE		0x04 /* Get write-cache status		*/
147 #define VD_OP_SET_WCE		0x05 /* Enable/disable write-cache	*/
148 #define VD_OP_GET_VTOC		0x06 /* Get VTOC			*/
149 #define VD_OP_SET_VTOC		0x07 /* Set VTOC			*/
150 #define VD_OP_GET_DISKGEOM	0x08 /* Get disk geometry		*/
151 #define VD_OP_SET_DISKGEOM	0x09 /* Set disk geometry		*/
152 #define VD_OP_SCSICMD		0x0a /* SCSI control command		*/
153 #define VD_OP_GET_DEVID		0x0b /* Get device ID			*/
154 #define VD_OP_GET_EFI		0x0c /* Get EFI				*/
155 #define VD_OP_SET_EFI		0x0d /* Set EFI				*/
156 	u8			slice;
157 	u16			resv1;
158 	u32			status;
159 	u64			offset;
160 	u64			size;
161 	u32			ncookies;
162 	u32			resv2;
163 	struct ldc_trans_cookie	cookies[0];
164 };
165 
166 #define VIO_DISK_VNAME_LEN	8
167 #define VIO_DISK_ALABEL_LEN	128
168 #define VIO_DISK_NUM_PART	8
169 
170 struct vio_disk_vtoc {
171 	u8			volume_name[VIO_DISK_VNAME_LEN];
172 	u16			sector_size;
173 	u16			num_partitions;
174 	u8			ascii_label[VIO_DISK_ALABEL_LEN];
175 	struct {
176 		u16		id;
177 		u16		perm_flags;
178 		u32		resv;
179 		u64		start_block;
180 		u64		num_blocks;
181 	} partitions[VIO_DISK_NUM_PART];
182 };
183 
184 struct vio_disk_geom {
185 	u16			num_cyl; /* Num data cylinders		*/
186 	u16			alt_cyl; /* Num alternate cylinders	*/
187 	u16			beg_cyl; /* Cyl off of fixed head area	*/
188 	u16			num_hd;  /* Num heads			*/
189 	u16			num_sec; /* Num sectors			*/
190 	u16			ifact;   /* Interleave factor		*/
191 	u16			apc;     /* Alts per cylinder (SCSI)	*/
192 	u16			rpm;	 /* Revolutions per minute	*/
193 	u16			phy_cyl; /* Num physical cylinders	*/
194 	u16			wr_skip; /* Num sects to skip, writes	*/
195 	u16			rd_skip; /* Num sects to skip, writes	*/
196 };
197 
198 struct vio_disk_devid {
199 	u16			resv;
200 	u16			type;
201 	u32			len;
202 	char			id[0];
203 };
204 
205 struct vio_disk_efi {
206 	u64			lba;
207 	u64			len;
208 	char			data[0];
209 };
210 
211 /* VIO net specific structures and defines */
212 struct vio_net_attr_info {
213 	struct vio_msg_tag	tag;
214 	u8			xfer_mode;
215 	u8			addr_type;
216 #define VNET_ADDR_ETHERMAC	0x01
217 	u16			ack_freq;
218 	u8			plnk_updt;
219 #define PHYSLINK_UPDATE_NONE		0x00
220 #define PHYSLINK_UPDATE_STATE		0x01
221 #define PHYSLINK_UPDATE_STATE_ACK	0x02
222 #define PHYSLINK_UPDATE_STATE_NACK	0x03
223 	u8			options;
224 	u16			resv1;
225 	u64			addr;
226 	u64			mtu;
227 	u16			cflags;
228 #define VNET_LSO_IPV4_CAPAB		0x0001
229 	u16			ipv4_lso_maxlen;
230 	u32			resv2;
231 	u64			resv3[2];
232 };
233 
234 #define VNET_NUM_MCAST		7
235 
236 struct vio_net_mcast_info {
237 	struct vio_msg_tag	tag;
238 	u8			set;
239 	u8			count;
240 	u8			mcast_addr[VNET_NUM_MCAST * 6];
241 	u32			resv;
242 };
243 
244 struct vio_net_desc {
245 	struct vio_dring_hdr	hdr;
246 	u32			size;
247 	u32			ncookies;
248 	struct ldc_trans_cookie	cookies[0];
249 };
250 
251 struct vio_net_dext {
252 	u8		flags;
253 #define VNET_PKT_HASH			0x01
254 #define	VNET_PKT_HCK_IPV4_HDRCKSUM	0x02
255 #define	VNET_PKT_HCK_FULLCKSUM		0x04
256 #define	VNET_PKT_IPV4_LSO		0x08
257 #define	VNET_PKT_HCK_IPV4_HDRCKSUM_OK	0x10
258 #define	VNET_PKT_HCK_FULLCKSUM_OK	0x20
259 
260 	u8		vnet_hashval;
261 	u16		ipv4_lso_mss;
262 	u32		resv3;
263 };
264 
265 static inline struct vio_net_dext *vio_net_ext(struct vio_net_desc *desc)
266 {
267 	return (struct vio_net_dext *)&desc->cookies[2];
268 }
269 
270 #define VIO_MAX_RING_COOKIES	24
271 
272 struct vio_dring_state {
273 	u64			ident;
274 	void			*base;
275 	u64			snd_nxt;
276 	u64			rcv_nxt;
277 	u32			entry_size;
278 	u32			num_entries;
279 	u32			prod;
280 	u32			cons;
281 	u32			pending;
282 	int			ncookies;
283 	struct ldc_trans_cookie	cookies[VIO_MAX_RING_COOKIES];
284 };
285 
286 #define VIO_TAG_SIZE		((int)sizeof(struct vio_msg_tag))
287 #define VIO_VCC_MTU_SIZE	(LDC_PACKET_SIZE - VIO_TAG_SIZE)
288 
289 struct vio_vcc {
290 	struct vio_msg_tag	tag;
291 	char			data[VIO_VCC_MTU_SIZE];
292 };
293 
294 static inline void *vio_dring_cur(struct vio_dring_state *dr)
295 {
296 	return dr->base + (dr->entry_size * dr->prod);
297 }
298 
299 static inline void *vio_dring_entry(struct vio_dring_state *dr,
300 				    unsigned int index)
301 {
302 	return dr->base + (dr->entry_size * index);
303 }
304 
305 static inline u32 vio_dring_avail(struct vio_dring_state *dr,
306 				  unsigned int ring_size)
307 {
308 	return (dr->pending -
309 		((dr->prod - dr->cons) & (ring_size - 1)) - 1);
310 }
311 
312 static inline u32 vio_dring_next(struct vio_dring_state *dr, u32 index)
313 {
314 	if (++index == dr->num_entries)
315 		index = 0;
316 	return index;
317 }
318 
319 static inline u32 vio_dring_prev(struct vio_dring_state *dr, u32 index)
320 {
321 	if (index == 0)
322 		return dr->num_entries - 1;
323 	else
324 		return index - 1;
325 }
326 
327 #define VIO_MAX_TYPE_LEN	32
328 #define VIO_MAX_NAME_LEN	32
329 #define VIO_MAX_COMPAT_LEN	64
330 
331 struct vio_dev {
332 	u64			mp;
333 	struct device_node	*dp;
334 
335 	char			node_name[VIO_MAX_NAME_LEN];
336 	char			type[VIO_MAX_TYPE_LEN];
337 	char			compat[VIO_MAX_COMPAT_LEN];
338 	int			compat_len;
339 
340 	u64			dev_no;
341 
342 	unsigned long		port_id;
343 	unsigned long		channel_id;
344 
345 	unsigned int		tx_irq;
346 	unsigned int		rx_irq;
347 	u64			rx_ino;
348 	u64			tx_ino;
349 
350 	/* Handle to the root of "channel-devices" sub-tree in MDESC */
351 	u64			cdev_handle;
352 
353 	/* MD specific data used to identify the vdev in MD */
354 	union md_node_info	md_node_info;
355 
356 	struct device		dev;
357 };
358 
359 struct vio_driver {
360 	const char			*name;
361 	struct list_head		node;
362 	const struct vio_device_id	*id_table;
363 	int (*probe)(struct vio_dev *dev, const struct vio_device_id *id);
364 	int (*remove)(struct vio_dev *dev);
365 	void (*shutdown)(struct vio_dev *dev);
366 	unsigned long			driver_data;
367 	struct device_driver		driver;
368 	bool				no_irq;
369 };
370 
371 struct vio_version {
372 	u16		major;
373 	u16		minor;
374 };
375 
376 struct vio_driver_state;
377 struct vio_driver_ops {
378 	int	(*send_attr)(struct vio_driver_state *vio);
379 	int	(*handle_attr)(struct vio_driver_state *vio, void *pkt);
380 	void	(*handshake_complete)(struct vio_driver_state *vio);
381 };
382 
383 struct vio_completion {
384 	struct completion	com;
385 	int			err;
386 	int			waiting_for;
387 };
388 
389 struct vio_driver_state {
390 	/* Protects VIO handshake and, optionally, driver private state.  */
391 	spinlock_t		lock;
392 
393 	struct ldc_channel	*lp;
394 
395 	u32			_peer_sid;
396 	u32			_local_sid;
397 	struct vio_dring_state	drings[2];
398 #define VIO_DRIVER_TX_RING	0
399 #define VIO_DRIVER_RX_RING	1
400 
401 	u8			hs_state;
402 #define VIO_HS_INVALID		0x00
403 #define VIO_HS_GOTVERS		0x01
404 #define VIO_HS_GOT_ATTR		0x04
405 #define VIO_HS_SENT_DREG	0x08
406 #define VIO_HS_SENT_RDX		0x10
407 #define VIO_HS_GOT_RDX_ACK	0x20
408 #define VIO_HS_GOT_RDX		0x40
409 #define VIO_HS_SENT_RDX_ACK	0x80
410 #define VIO_HS_COMPLETE		(VIO_HS_GOT_RDX_ACK | VIO_HS_SENT_RDX_ACK)
411 
412 	u8			dev_class;
413 
414 	u8			dr_state;
415 #define VIO_DR_STATE_TXREG	0x01
416 #define VIO_DR_STATE_RXREG	0x02
417 #define VIO_DR_STATE_TXREQ	0x10
418 #define VIO_DR_STATE_RXREQ	0x20
419 
420 	u8			debug;
421 #define VIO_DEBUG_HS		0x01
422 #define VIO_DEBUG_DATA		0x02
423 
424 	void			*desc_buf;
425 	unsigned int		desc_buf_len;
426 
427 	struct vio_completion	*cmp;
428 
429 	struct vio_dev		*vdev;
430 
431 	struct timer_list	timer;
432 
433 	struct vio_version	ver;
434 
435 	struct vio_version	*ver_table;
436 	int			ver_table_entries;
437 
438 	char			*name;
439 
440 	struct vio_driver_ops	*ops;
441 };
442 
443 static inline bool vio_version_before(struct vio_driver_state *vio,
444 				      u16 major, u16 minor)
445 {
446 	u32 have = (u32)vio->ver.major << 16 | vio->ver.minor;
447 	u32 want = (u32)major << 16 | minor;
448 
449 	return have < want;
450 }
451 
452 static inline bool vio_version_after(struct vio_driver_state *vio,
453 				      u16 major, u16 minor)
454 {
455 	u32 have = (u32)vio->ver.major << 16 | vio->ver.minor;
456 	u32 want = (u32)major << 16 | minor;
457 
458 	return have > want;
459 }
460 
461 static inline bool vio_version_after_eq(struct vio_driver_state *vio,
462 					u16 major, u16 minor)
463 {
464 	u32 have = (u32)vio->ver.major << 16 | vio->ver.minor;
465 	u32 want = (u32)major << 16 | minor;
466 
467 	return have >= want;
468 }
469 
470 #define viodbg(TYPE, f, a...) \
471 do {	if (vio->debug & VIO_DEBUG_##TYPE) \
472 		printk(KERN_INFO "vio: ID[%lu] " f, \
473 		       vio->vdev->channel_id, ## a); \
474 } while (0)
475 
476 int __vio_register_driver(struct vio_driver *drv, struct module *owner,
477 				 const char *mod_name);
478 /*
479  * vio_register_driver must be a macro so that KBUILD_MODNAME can be expanded
480  */
481 #define vio_register_driver(driver)		\
482 	__vio_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
483 void vio_unregister_driver(struct vio_driver *drv);
484 
485 static inline struct vio_driver *to_vio_driver(struct device_driver *drv)
486 {
487 	return container_of(drv, struct vio_driver, driver);
488 }
489 
490 static inline struct vio_dev *to_vio_dev(struct device *dev)
491 {
492 	return container_of(dev, struct vio_dev, dev);
493 }
494 
495 int vio_ldc_send(struct vio_driver_state *vio, void *data, int len);
496 void vio_link_state_change(struct vio_driver_state *vio, int event);
497 void vio_conn_reset(struct vio_driver_state *vio);
498 int vio_control_pkt_engine(struct vio_driver_state *vio, void *pkt);
499 int vio_validate_sid(struct vio_driver_state *vio,
500 		     struct vio_msg_tag *tp);
501 u32 vio_send_sid(struct vio_driver_state *vio);
502 int vio_ldc_alloc(struct vio_driver_state *vio,
503 		  struct ldc_channel_config *base_cfg, void *event_arg);
504 void vio_ldc_free(struct vio_driver_state *vio);
505 int vio_driver_init(struct vio_driver_state *vio, struct vio_dev *vdev,
506 		    u8 dev_class, struct vio_version *ver_table,
507 		    int ver_table_size, struct vio_driver_ops *ops,
508 		    char *name);
509 
510 void vio_port_up(struct vio_driver_state *vio);
511 int vio_set_intr(unsigned long dev_ino, int state);
512 u64 vio_vdev_node(struct mdesc_handle *hp, struct vio_dev *vdev);
513 
514 #endif /* _SPARC64_VIO_H */
515