Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 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 | |
| 56 | u8 resv1[3]; |
| 57 | u64 resv2[5]; |
| 58 | }; |
| 59 | |
| 60 | struct vio_dring_register { |
| 61 | struct vio_msg_tag tag; |
| 62 | u64 dring_ident; |
| 63 | u32 num_descr; |
| 64 | u32 descr_size; |
| 65 | u16 options; |
| 66 | #define VIO_TX_DRING 0x0001 |
| 67 | #define VIO_RX_DRING 0x0002 |
David L Stevens | 163a4e7 | 2014-09-29 19:47:59 -0400 | [diff] [blame] | 68 | #define VIO_RX_DRING_DATA 0x0004 |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 69 | u16 resv; |
| 70 | u32 num_cookies; |
| 71 | struct ldc_trans_cookie cookies[0]; |
| 72 | }; |
| 73 | |
| 74 | struct vio_dring_unregister { |
| 75 | struct vio_msg_tag tag; |
| 76 | u64 dring_ident; |
| 77 | u64 resv[5]; |
| 78 | }; |
| 79 | |
| 80 | /* Data transfer modes */ |
| 81 | #define VIO_PKT_MODE 0x01 /* Packet based transfer */ |
| 82 | #define VIO_DESC_MODE 0x02 /* In-band descriptors */ |
| 83 | #define VIO_DRING_MODE 0x03 /* Descriptor rings */ |
David L Stevens | 163a4e7 | 2014-09-29 19:47:59 -0400 | [diff] [blame] | 84 | /* in vers >= 1.2, VIO_DRING_MODE is 0x04 and transfer mode is a bitmask */ |
| 85 | #define VIO_NEW_DRING_MODE 0x04 |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 86 | |
| 87 | struct vio_dring_data { |
| 88 | struct vio_msg_tag tag; |
| 89 | u64 seq; |
| 90 | u64 dring_ident; |
| 91 | u32 start_idx; |
| 92 | u32 end_idx; |
| 93 | u8 state; |
| 94 | #define VIO_DRING_ACTIVE 0x01 |
| 95 | #define VIO_DRING_STOPPED 0x02 |
| 96 | |
| 97 | u8 __pad1; |
| 98 | u16 __pad2; |
| 99 | u32 __pad3; |
| 100 | u64 __par4[2]; |
| 101 | }; |
| 102 | |
| 103 | struct vio_dring_hdr { |
| 104 | u8 state; |
| 105 | #define VIO_DESC_FREE 0x01 |
| 106 | #define VIO_DESC_READY 0x02 |
| 107 | #define VIO_DESC_ACCEPTED 0x03 |
| 108 | #define VIO_DESC_DONE 0x04 |
| 109 | u8 ack; |
| 110 | #define VIO_ACK_ENABLE 0x01 |
| 111 | #define VIO_ACK_DISABLE 0x00 |
| 112 | |
| 113 | u16 __pad1; |
| 114 | u32 __pad2; |
| 115 | }; |
| 116 | |
| 117 | /* VIO disk specific structures and defines */ |
| 118 | struct vio_disk_attr_info { |
| 119 | struct vio_msg_tag tag; |
| 120 | u8 xfer_mode; |
| 121 | u8 vdisk_type; |
| 122 | #define VD_DISK_TYPE_SLICE 0x01 /* Slice in block device */ |
| 123 | #define VD_DISK_TYPE_DISK 0x02 /* Entire block device */ |
Allen Pais | 9bce218 | 2014-09-19 09:42:14 -0400 | [diff] [blame^] | 124 | u8 vdisk_mtype; /* v1.1 */ |
| 125 | #define VD_MEDIA_TYPE_FIXED 0x01 /* Fixed device */ |
| 126 | #define VD_MEDIA_TYPE_CD 0x02 /* CD Device */ |
| 127 | #define VD_MEDIA_TYPE_DVD 0x03 /* DVD Device */ |
| 128 | u8 resv1; |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 129 | u32 vdisk_block_size; |
| 130 | u64 operations; |
Allen Pais | 9bce218 | 2014-09-19 09:42:14 -0400 | [diff] [blame^] | 131 | u64 vdisk_size; /* v1.1 */ |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 132 | u64 max_xfer_size; |
Allen Pais | 9bce218 | 2014-09-19 09:42:14 -0400 | [diff] [blame^] | 133 | u32 phys_block_size; /* v1.2 */ |
| 134 | u32 resv2; |
| 135 | u64 resv3[1]; |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | struct vio_disk_desc { |
| 139 | struct vio_dring_hdr hdr; |
| 140 | u64 req_id; |
| 141 | u8 operation; |
| 142 | #define VD_OP_BREAD 0x01 /* Block read */ |
| 143 | #define VD_OP_BWRITE 0x02 /* Block write */ |
| 144 | #define VD_OP_FLUSH 0x03 /* Flush disk contents */ |
| 145 | #define VD_OP_GET_WCE 0x04 /* Get write-cache status */ |
| 146 | #define VD_OP_SET_WCE 0x05 /* Enable/disable write-cache */ |
| 147 | #define VD_OP_GET_VTOC 0x06 /* Get VTOC */ |
| 148 | #define VD_OP_SET_VTOC 0x07 /* Set VTOC */ |
| 149 | #define VD_OP_GET_DISKGEOM 0x08 /* Get disk geometry */ |
| 150 | #define VD_OP_SET_DISKGEOM 0x09 /* Set disk geometry */ |
| 151 | #define VD_OP_SCSICMD 0x0a /* SCSI control command */ |
| 152 | #define VD_OP_GET_DEVID 0x0b /* Get device ID */ |
| 153 | #define VD_OP_GET_EFI 0x0c /* Get EFI */ |
| 154 | #define VD_OP_SET_EFI 0x0d /* Set EFI */ |
| 155 | u8 slice; |
| 156 | u16 resv1; |
| 157 | u32 status; |
| 158 | u64 offset; |
| 159 | u64 size; |
| 160 | u32 ncookies; |
| 161 | u32 resv2; |
| 162 | struct ldc_trans_cookie cookies[0]; |
| 163 | }; |
| 164 | |
| 165 | #define VIO_DISK_VNAME_LEN 8 |
| 166 | #define VIO_DISK_ALABEL_LEN 128 |
| 167 | #define VIO_DISK_NUM_PART 8 |
| 168 | |
| 169 | struct vio_disk_vtoc { |
| 170 | u8 volume_name[VIO_DISK_VNAME_LEN]; |
| 171 | u16 sector_size; |
| 172 | u16 num_partitions; |
| 173 | u8 ascii_label[VIO_DISK_ALABEL_LEN]; |
| 174 | struct { |
| 175 | u16 id; |
| 176 | u16 perm_flags; |
| 177 | u32 resv; |
| 178 | u64 start_block; |
| 179 | u64 num_blocks; |
| 180 | } partitions[VIO_DISK_NUM_PART]; |
| 181 | }; |
| 182 | |
| 183 | struct vio_disk_geom { |
| 184 | u16 num_cyl; /* Num data cylinders */ |
| 185 | u16 alt_cyl; /* Num alternate cylinders */ |
| 186 | u16 beg_cyl; /* Cyl off of fixed head area */ |
| 187 | u16 num_hd; /* Num heads */ |
| 188 | u16 num_sec; /* Num sectors */ |
| 189 | u16 ifact; /* Interleave factor */ |
| 190 | u16 apc; /* Alts per cylinder (SCSI) */ |
| 191 | u16 rpm; /* Revolutions per minute */ |
| 192 | u16 phy_cyl; /* Num physical cylinders */ |
| 193 | u16 wr_skip; /* Num sects to skip, writes */ |
| 194 | u16 rd_skip; /* Num sects to skip, writes */ |
| 195 | }; |
| 196 | |
| 197 | struct vio_disk_devid { |
| 198 | u16 resv; |
| 199 | u16 type; |
| 200 | u32 len; |
| 201 | char id[0]; |
| 202 | }; |
| 203 | |
| 204 | struct vio_disk_efi { |
| 205 | u64 lba; |
| 206 | u64 len; |
| 207 | char data[0]; |
| 208 | }; |
| 209 | |
| 210 | /* VIO net specific structures and defines */ |
| 211 | struct vio_net_attr_info { |
| 212 | struct vio_msg_tag tag; |
| 213 | u8 xfer_mode; |
| 214 | u8 addr_type; |
| 215 | #define VNET_ADDR_ETHERMAC 0x01 |
| 216 | u16 ack_freq; |
David L Stevens | 163a4e7 | 2014-09-29 19:47:59 -0400 | [diff] [blame] | 217 | u8 plnk_updt; |
| 218 | #define PHYSLINK_UPDATE_NONE 0x00 |
| 219 | #define PHYSLINK_UPDATE_STATE 0x01 |
| 220 | #define PHYSLINK_UPDATE_STATE_ACK 0x02 |
| 221 | #define PHYSLINK_UPDATE_STATE_NACK 0x03 |
| 222 | u8 options; |
| 223 | u16 resv1; |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 224 | u64 addr; |
| 225 | u64 mtu; |
David L Stevens | 163a4e7 | 2014-09-29 19:47:59 -0400 | [diff] [blame] | 226 | u16 cflags; |
| 227 | #define VNET_LSO_IPV4_CAPAB 0x0001 |
| 228 | u16 ipv4_lso_maxlen; |
| 229 | u32 resv2; |
| 230 | u64 resv3[2]; |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | #define VNET_NUM_MCAST 7 |
| 234 | |
| 235 | struct vio_net_mcast_info { |
| 236 | struct vio_msg_tag tag; |
| 237 | u8 set; |
| 238 | u8 count; |
| 239 | u8 mcast_addr[VNET_NUM_MCAST * 6]; |
| 240 | u32 resv; |
| 241 | }; |
| 242 | |
| 243 | struct vio_net_desc { |
| 244 | struct vio_dring_hdr hdr; |
| 245 | u32 size; |
| 246 | u32 ncookies; |
| 247 | struct ldc_trans_cookie cookies[0]; |
| 248 | }; |
| 249 | |
| 250 | #define VIO_MAX_RING_COOKIES 24 |
| 251 | |
| 252 | struct vio_dring_state { |
| 253 | u64 ident; |
| 254 | void *base; |
| 255 | u64 snd_nxt; |
| 256 | u64 rcv_nxt; |
| 257 | u32 entry_size; |
| 258 | u32 num_entries; |
| 259 | u32 prod; |
| 260 | u32 cons; |
| 261 | u32 pending; |
| 262 | int ncookies; |
| 263 | struct ldc_trans_cookie cookies[VIO_MAX_RING_COOKIES]; |
| 264 | }; |
| 265 | |
| 266 | static inline void *vio_dring_cur(struct vio_dring_state *dr) |
| 267 | { |
| 268 | return dr->base + (dr->entry_size * dr->prod); |
| 269 | } |
| 270 | |
| 271 | static inline void *vio_dring_entry(struct vio_dring_state *dr, |
| 272 | unsigned int index) |
| 273 | { |
| 274 | return dr->base + (dr->entry_size * index); |
| 275 | } |
| 276 | |
| 277 | static inline u32 vio_dring_avail(struct vio_dring_state *dr, |
| 278 | unsigned int ring_size) |
| 279 | { |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 280 | return (dr->pending - |
| 281 | ((dr->prod - dr->cons) & (ring_size - 1))); |
| 282 | } |
| 283 | |
| 284 | #define VIO_MAX_TYPE_LEN 32 |
| 285 | #define VIO_MAX_COMPAT_LEN 64 |
| 286 | |
| 287 | struct vio_dev { |
| 288 | u64 mp; |
| 289 | struct device_node *dp; |
| 290 | |
| 291 | char type[VIO_MAX_TYPE_LEN]; |
| 292 | char compat[VIO_MAX_COMPAT_LEN]; |
| 293 | int compat_len; |
| 294 | |
| 295 | u64 dev_no; |
| 296 | |
| 297 | unsigned long channel_id; |
| 298 | |
| 299 | unsigned int tx_irq; |
| 300 | unsigned int rx_irq; |
| 301 | |
| 302 | struct device dev; |
| 303 | }; |
| 304 | |
| 305 | struct vio_driver { |
Benjamin Herrenschmidt | cb52d89 | 2012-03-26 19:06:30 +0000 | [diff] [blame] | 306 | const char *name; |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 307 | struct list_head node; |
| 308 | const struct vio_device_id *id_table; |
| 309 | int (*probe)(struct vio_dev *dev, const struct vio_device_id *id); |
| 310 | int (*remove)(struct vio_dev *dev); |
| 311 | void (*shutdown)(struct vio_dev *dev); |
| 312 | unsigned long driver_data; |
| 313 | struct device_driver driver; |
| 314 | }; |
| 315 | |
| 316 | struct vio_version { |
| 317 | u16 major; |
| 318 | u16 minor; |
| 319 | }; |
| 320 | |
| 321 | struct vio_driver_state; |
| 322 | struct vio_driver_ops { |
| 323 | int (*send_attr)(struct vio_driver_state *vio); |
| 324 | int (*handle_attr)(struct vio_driver_state *vio, void *pkt); |
| 325 | void (*handshake_complete)(struct vio_driver_state *vio); |
| 326 | }; |
| 327 | |
| 328 | struct vio_completion { |
| 329 | struct completion com; |
| 330 | int err; |
| 331 | int waiting_for; |
| 332 | }; |
| 333 | |
| 334 | struct vio_driver_state { |
| 335 | /* Protects VIO handshake and, optionally, driver private state. */ |
| 336 | spinlock_t lock; |
| 337 | |
| 338 | struct ldc_channel *lp; |
| 339 | |
| 340 | u32 _peer_sid; |
| 341 | u32 _local_sid; |
| 342 | struct vio_dring_state drings[2]; |
| 343 | #define VIO_DRIVER_TX_RING 0 |
| 344 | #define VIO_DRIVER_RX_RING 1 |
| 345 | |
| 346 | u8 hs_state; |
| 347 | #define VIO_HS_INVALID 0x00 |
| 348 | #define VIO_HS_GOTVERS 0x01 |
| 349 | #define VIO_HS_GOT_ATTR 0x04 |
| 350 | #define VIO_HS_SENT_DREG 0x08 |
| 351 | #define VIO_HS_SENT_RDX 0x10 |
| 352 | #define VIO_HS_GOT_RDX_ACK 0x20 |
| 353 | #define VIO_HS_GOT_RDX 0x40 |
| 354 | #define VIO_HS_SENT_RDX_ACK 0x80 |
| 355 | #define VIO_HS_COMPLETE (VIO_HS_GOT_RDX_ACK | VIO_HS_SENT_RDX_ACK) |
| 356 | |
| 357 | u8 dev_class; |
| 358 | |
| 359 | u8 dr_state; |
| 360 | #define VIO_DR_STATE_TXREG 0x01 |
| 361 | #define VIO_DR_STATE_RXREG 0x02 |
| 362 | #define VIO_DR_STATE_TXREQ 0x10 |
| 363 | #define VIO_DR_STATE_RXREQ 0x20 |
| 364 | |
| 365 | u8 debug; |
| 366 | #define VIO_DEBUG_HS 0x01 |
| 367 | #define VIO_DEBUG_DATA 0x02 |
| 368 | |
| 369 | void *desc_buf; |
| 370 | unsigned int desc_buf_len; |
| 371 | |
| 372 | struct vio_completion *cmp; |
| 373 | |
| 374 | struct vio_dev *vdev; |
| 375 | |
| 376 | struct timer_list timer; |
| 377 | |
| 378 | struct vio_version ver; |
| 379 | |
| 380 | struct vio_version *ver_table; |
| 381 | int ver_table_entries; |
| 382 | |
| 383 | char *name; |
| 384 | |
| 385 | struct vio_driver_ops *ops; |
| 386 | }; |
| 387 | |
David L Stevens | 163a4e7 | 2014-09-29 19:47:59 -0400 | [diff] [blame] | 388 | static inline bool vio_version_before(struct vio_driver_state *vio, |
| 389 | u16 major, u16 minor) |
| 390 | { |
| 391 | u32 have = (u32)vio->ver.major << 16 | vio->ver.minor; |
| 392 | u32 want = (u32)major << 16 | minor; |
| 393 | |
| 394 | return have < want; |
| 395 | } |
| 396 | |
| 397 | static inline bool vio_version_after(struct vio_driver_state *vio, |
| 398 | u16 major, u16 minor) |
| 399 | { |
| 400 | u32 have = (u32)vio->ver.major << 16 | vio->ver.minor; |
| 401 | u32 want = (u32)major << 16 | minor; |
| 402 | |
| 403 | return have > want; |
| 404 | } |
| 405 | |
| 406 | static inline bool vio_version_after_eq(struct vio_driver_state *vio, |
| 407 | u16 major, u16 minor) |
| 408 | { |
| 409 | u32 have = (u32)vio->ver.major << 16 | vio->ver.minor; |
| 410 | u32 want = (u32)major << 16 | minor; |
| 411 | |
| 412 | return have >= want; |
| 413 | } |
| 414 | |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 415 | #define viodbg(TYPE, f, a...) \ |
| 416 | do { if (vio->debug & VIO_DEBUG_##TYPE) \ |
| 417 | printk(KERN_INFO "vio: ID[%lu] " f, \ |
| 418 | vio->vdev->channel_id, ## a); \ |
| 419 | } while (0) |
| 420 | |
Sam Ravnborg | f05a686 | 2014-05-16 23:25:50 +0200 | [diff] [blame] | 421 | int __vio_register_driver(struct vio_driver *drv, struct module *owner, |
Benjamin Herrenschmidt | cb52d89 | 2012-03-26 19:06:30 +0000 | [diff] [blame] | 422 | const char *mod_name); |
| 423 | /* |
| 424 | * vio_register_driver must be a macro so that KBUILD_MODNAME can be expanded |
| 425 | */ |
| 426 | #define vio_register_driver(driver) \ |
| 427 | __vio_register_driver(driver, THIS_MODULE, KBUILD_MODNAME) |
Sam Ravnborg | f05a686 | 2014-05-16 23:25:50 +0200 | [diff] [blame] | 428 | void vio_unregister_driver(struct vio_driver *drv); |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 429 | |
| 430 | static inline struct vio_driver *to_vio_driver(struct device_driver *drv) |
| 431 | { |
| 432 | return container_of(drv, struct vio_driver, driver); |
| 433 | } |
| 434 | |
| 435 | static inline struct vio_dev *to_vio_dev(struct device *dev) |
| 436 | { |
| 437 | return container_of(dev, struct vio_dev, dev); |
| 438 | } |
| 439 | |
Sam Ravnborg | f05a686 | 2014-05-16 23:25:50 +0200 | [diff] [blame] | 440 | int vio_ldc_send(struct vio_driver_state *vio, void *data, int len); |
| 441 | void vio_link_state_change(struct vio_driver_state *vio, int event); |
| 442 | void vio_conn_reset(struct vio_driver_state *vio); |
| 443 | int vio_control_pkt_engine(struct vio_driver_state *vio, void *pkt); |
| 444 | int vio_validate_sid(struct vio_driver_state *vio, |
| 445 | struct vio_msg_tag *tp); |
| 446 | u32 vio_send_sid(struct vio_driver_state *vio); |
| 447 | int vio_ldc_alloc(struct vio_driver_state *vio, |
| 448 | struct ldc_channel_config *base_cfg, void *event_arg); |
| 449 | void vio_ldc_free(struct vio_driver_state *vio); |
| 450 | int vio_driver_init(struct vio_driver_state *vio, struct vio_dev *vdev, |
| 451 | u8 dev_class, struct vio_version *ver_table, |
| 452 | int ver_table_size, struct vio_driver_ops *ops, |
| 453 | char *name); |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 454 | |
Sam Ravnborg | f05a686 | 2014-05-16 23:25:50 +0200 | [diff] [blame] | 455 | void vio_port_up(struct vio_driver_state *vio); |
Sam Ravnborg | a00736e | 2008-06-19 20:26:19 +0200 | [diff] [blame] | 456 | |
| 457 | #endif /* _SPARC64_VIO_H */ |