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