1/* uisutils.h
2 *
3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT.  See the GNU General Public License for more
15 * details.
16 */
17
18/*
19 * Unisys Virtual HBA utilities header
20 */
21
22#ifndef __UISUTILS__H__
23#define __UISUTILS__H__
24#include <linux/string.h>
25#include <linux/io.h>
26#include <linux/sched.h>
27#include <linux/gfp.h>
28#include <linux/uuid.h>
29#include <linux/if_ether.h>
30
31#include "vmcallinterface.h"
32#include "channel.h"
33#include "uisthread.h"
34#include "uisqueue.h"
35#include "diagnostics/appos_subsystems.h"
36#include "vbusdeviceinfo.h"
37#include <linux/atomic.h>
38
39/* This is the MAGIC number stuffed by virthba in host->this_id. Used to
40 * identify virtual hbas.
41 */
42#define UIS_MAGIC_VHBA 707
43
44/* global function pointers that act as callback functions into
45 * uisnicmod, uissdmod, and virtpcimod
46 */
47extern int (*uisnic_control_chan_func)(struct io_msgs *);
48extern int (*uissd_control_chan_func)(struct io_msgs *);
49extern int (*virt_control_chan_func)(struct guest_msgs *);
50
51/* Return values of above callback functions: */
52#define CCF_ERROR        0	/* completed and failed */
53#define CCF_OK           1	/* completed successfully */
54#define CCF_PENDING      2	/* operation still pending */
55extern atomic_t uisutils_registered_services;
56
57struct req_handler_info {
58	uuid_le switch_uuid;
59	int (*controlfunc)(struct io_msgs *);
60	unsigned long min_channel_bytes;
61	int (*server_channel_ok)(unsigned long channel_bytes);
62	int (*server_channel_init)(void *x, unsigned char *client_str,
63				   u32 client_str_len, u64 bytes);
64	char switch_type_name[99];
65	struct list_head list_link;	/* links into ReqHandlerInfo_list */
66};
67
68struct req_handler_info *req_handler_find(uuid_le switch_uuid);
69
70#define uislib_ioremap_cache(addr, size) \
71	dbg_ioremap_cache(addr, size, __FILE__, __LINE__)
72
73static inline void __iomem *
74dbg_ioremap_cache(u64 addr, unsigned long size, char *file, int line)
75{
76	void __iomem *new;
77
78	new = ioremap_cache(addr, size);
79	return new;
80}
81
82#define uislib_ioremap(addr, size) dbg_ioremap(addr, size, __FILE__, __LINE__)
83
84static inline void *
85dbg_ioremap(u64 addr, unsigned long size, char *file, int line)
86{
87	void *new;
88
89	new = ioremap(addr, size);
90	return new;
91}
92
93#define uislib_iounmap(addr) dbg_iounmap(addr, __FILE__, __LINE__)
94
95static inline void
96dbg_iounmap(void __iomem *addr, char *file, int line)
97{
98	iounmap(addr);
99}
100
101#define PROC_READ_BUFFER_SIZE 131072	/* size of the buffer to allocate to
102					 * hold all of /proc/XXX/info */
103int uisutil_add_proc_line_ex(int *total, char **buffer, int *buffer_remaining,
104			     char *format, ...);
105
106int uisctrl_register_req_handler(int type, void *fptr,
107			struct ultra_vbus_deviceinfo *chipset_driver_info);
108
109unsigned char *util_map_virt(struct phys_info *sg);
110void util_unmap_virt(struct phys_info *sg);
111unsigned char *util_map_virt_atomic(struct phys_info *sg);
112void util_unmap_virt_atomic(void *buf);
113int uislib_client_inject_add_bus(u32 bus_no, uuid_le inst_uuid,
114				 u64 channel_addr, ulong n_channel_bytes);
115int  uislib_client_inject_del_bus(u32 bus_no);
116
117int uislib_client_inject_add_vhba(u32 bus_no, u32 dev_no,
118				  u64 phys_chan_addr, u32 chan_bytes,
119				  int is_test_addr, uuid_le inst_uuid,
120				  struct irq_info *intr);
121int  uislib_client_inject_pause_vhba(u32 bus_no, u32 dev_no);
122int  uislib_client_inject_resume_vhba(u32 bus_no, u32 dev_no);
123int uislib_client_inject_del_vhba(u32 bus_no, u32 dev_no);
124int uislib_client_inject_add_vnic(u32 bus_no, u32 dev_no,
125				  u64 phys_chan_addr, u32 chan_bytes,
126				  int is_test_addr, uuid_le inst_uuid,
127				  struct irq_info *intr);
128int uislib_client_inject_pause_vnic(u32 bus_no, u32 dev_no);
129int uislib_client_inject_resume_vnic(u32 bus_no, u32 dev_no);
130int uislib_client_inject_del_vnic(u32 bus_no, u32 dev_no);
131#ifdef STORAGE_CHANNEL
132u64 uislib_storage_channel(int client_id);
133#endif
134int uislib_get_owned_pdest(struct uisscsi_dest *pdest);
135
136int uislib_send_event(enum controlvm_id id,
137		      struct controlvm_message_packet *event);
138
139/* structure used by vhba & vnic to keep track of queue & thread info */
140struct chaninfo {
141	struct uisqueue_info *queueinfo;
142	/* this specifies the queue structures for a channel */
143	/* ALLOCATED BY THE OTHER END - WE JUST GET A POINTER TO THE MEMORY */
144	spinlock_t insertlock;
145	/* currently used only in virtnic when sending data to uisnic */
146	/* to synchronize the inserts into the signal queue */
147	struct uisthread_info threadinfo;
148	/* this specifies the thread structures used by the thread that */
149	/* handles this channel */
150};
151
152/* this is the wait code for all the threads - it is used to get
153* something from a queue choices: wait_for_completion_interruptible,
154* _timeout, interruptible_timeout
155*/
156#define UIS_THREAD_WAIT_MSEC(x) { \
157	set_current_state(TASK_INTERRUPTIBLE); \
158	schedule_timeout(msecs_to_jiffies(x)); \
159}
160
161#define UIS_THREAD_WAIT_USEC(x) { \
162	set_current_state(TASK_INTERRUPTIBLE); \
163	schedule_timeout(usecs_to_jiffies(x)); \
164}
165
166#define UIS_THREAD_WAIT UIS_THREAD_WAIT_MSEC(5)
167
168#define UIS_THREAD_WAIT_SEC(x) { \
169	set_current_state(TASK_INTERRUPTIBLE); \
170	schedule_timeout((x)*HZ); \
171}
172
173/* This is a hack until we fix IOVM to initialize the channel header
174 * correctly at DEVICE_CREATE time, INSTEAD OF waiting until
175 * DEVICE_CONFIGURE time.
176 */
177static inline void
178wait_for_valid_guid(uuid_le __iomem *guid)
179{
180	uuid_le tmpguid;
181
182	while (1) {
183		memcpy_fromio((void *)&tmpguid,
184			      (void __iomem *)guid, sizeof(uuid_le));
185		if (uuid_le_cmp(tmpguid, NULL_UUID_LE) != 0)
186			break;
187		UIS_THREAD_WAIT_SEC(5);
188	}
189}
190
191static inline unsigned int
192issue_vmcall_io_controlvm_addr(u64 *control_addr, u32 *control_bytes)
193{
194	struct vmcall_io_controlvm_addr_params params;
195	int result = VMCALL_SUCCESS;
196	u64 physaddr;
197
198	physaddr = virt_to_phys(&params);
199	ISSUE_IO_VMCALL(VMCALL_IO_CONTROLVM_ADDR, physaddr, result);
200	if (VMCALL_SUCCESSFUL(result)) {
201		*control_addr = params.address;
202		*control_bytes = params.channel_bytes;
203	}
204	return result;
205}
206
207static inline unsigned int issue_vmcall_io_diag_addr(u64 *diag_channel_addr)
208{
209	struct vmcall_io_diag_addr_params params;
210	int result = VMCALL_SUCCESS;
211	u64 physaddr;
212
213	physaddr = virt_to_phys(&params);
214	ISSUE_IO_VMCALL(VMCALL_IO_DIAG_ADDR, physaddr, result);
215	if (VMCALL_SUCCESSFUL(result))
216		*diag_channel_addr = params.address;
217	return result;
218}
219
220static inline unsigned int issue_vmcall_io_visorserial_addr(u64 *channel_addr)
221{
222	struct vmcall_io_visorserial_addr_params params;
223	int result = VMCALL_SUCCESS;
224	u64 physaddr;
225
226	physaddr = virt_to_phys(&params);
227	ISSUE_IO_VMCALL(VMCALL_IO_VISORSERIAL_ADDR, physaddr, result);
228	if (VMCALL_SUCCESSFUL(result))
229		*channel_addr = params.address;
230	return result;
231}
232
233static inline s64 issue_vmcall_query_guest_virtual_time_offset(void)
234{
235	u64 result = VMCALL_SUCCESS;
236	u64 physaddr = 0;
237
238	ISSUE_IO_VMCALL(VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET, physaddr,
239			result);
240	return result;
241}
242
243struct log_info_t {
244	unsigned long long last_cycles;
245	unsigned long long delta_sum[64];
246	unsigned long long delta_cnt[64];
247	unsigned long long max_delta[64];
248	unsigned long long min_delta[64];
249};
250
251static inline int issue_vmcall_update_physical_time(u64 adjustment)
252{
253	int result = VMCALL_SUCCESS;
254
255	ISSUE_IO_VMCALL(VMCALL_UPDATE_PHYSICAL_TIME, adjustment, result);
256	return result;
257}
258
259static inline unsigned int issue_vmcall_channel_mismatch(const char *chname,
260			      const char *item_name, u32 line_no,
261			      const char *path_n_fn)
262{
263	struct vmcall_channel_version_mismatch_params params;
264	int result = VMCALL_SUCCESS;
265	u64 physaddr;
266	char *last_slash = NULL;
267
268	strlcpy(params.chname, chname, sizeof(params.chname));
269	strlcpy(params.item_name, item_name, sizeof(params.item_name));
270	params.line_no = line_no;
271
272	last_slash = strrchr(path_n_fn, '/');
273	if (last_slash != NULL) {
274		last_slash++;
275		strlcpy(params.file_name, last_slash, sizeof(params.file_name));
276	} else
277		strlcpy(params.file_name,
278			"Cannot determine source filename",
279			sizeof(params.file_name));
280
281	physaddr = virt_to_phys(&params);
282	ISSUE_IO_VMCALL(VMCALL_CHANNEL_VERSION_MISMATCH, physaddr, result);
283	return result;
284}
285
286#define UIS_DAEMONIZE(nam)
287void *uislib_cache_alloc(struct kmem_cache *cur_pool, char *fn, int ln);
288#define UISCACHEALLOC(cur_pool) uislib_cache_alloc(cur_pool, __FILE__, __LINE__)
289void uislib_cache_free(struct kmem_cache *cur_pool, void *p, char *fn, int ln);
290#define UISCACHEFREE(cur_pool, p) \
291	uislib_cache_free(cur_pool, p, __FILE__, __LINE__)
292
293void uislib_enable_channel_interrupts(u32 bus_no, u32 dev_no,
294				      int (*interrupt)(void *),
295				      void *interrupt_context);
296void uislib_disable_channel_interrupts(u32 bus_no, u32 dev_no);
297void uislib_force_channel_interrupt(u32 bus_no, u32 dev_no);
298
299#endif /* __UISUTILS__H__ */
300