81d21b1bae5722db4d02fc9a155c170db5774f20
[firefly-linux-kernel-4.4.55.git] / drivers / staging / unisys / include / uisutils.h
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
30 #include "vmcallinterface.h"
31 #include "channel.h"
32 #include "uisthread.h"
33 #include "uisqueue.h"
34 #include "diagnostics/appos_subsystems.h"
35 #include "vbusdeviceinfo.h"
36 #include <linux/atomic.h>
37
38 /* This is the MAGIC number stuffed by virthba in host->this_id. Used to
39  * identify virtual hbas.
40  */
41 #define UIS_MAGIC_VHBA 707
42
43 /* global function pointers that act as callback functions into
44  * uisnicmod, uissdmod, and virtpcimod
45  */
46 extern int (*UisnicControlChanFunc)(struct io_msgs *);
47 extern int (*UissdControlChanFunc)(struct io_msgs *);
48 extern int (*VirtControlChanFunc)(struct guest_msgs *);
49
50 /* Return values of above callback functions: */
51 #define CCF_ERROR        0      /* completed and failed */
52 #define CCF_OK           1      /* completed successfully */
53 #define CCF_PENDING      2      /* operation still pending */
54 extern atomic_t UisUtils_Registered_Services;
55
56 typedef unsigned int MACARRAY[MAX_MACADDR_LEN];
57 typedef struct ReqHandlerInfo_struct {
58         uuid_le switchTypeGuid;
59         int (*controlfunc)(struct io_msgs *);
60         unsigned long min_channel_bytes;
61         int (*Server_Channel_Ok)(unsigned long channelBytes);
62         int (*Server_Channel_Init)
63          (void *x, unsigned char *clientStr, u32 clientStrLen, u64 bytes);
64         char switch_type_name[99];
65         struct list_head list_link;     /* links into ReqHandlerInfo_list */
66 } ReqHandlerInfo_t;
67
68 ReqHandlerInfo_t *ReqHandlerAdd(uuid_le switchTypeGuid,
69                                 const char *switch_type_name,
70                                 int (*controlfunc)(struct io_msgs *),
71                                 unsigned long min_channel_bytes,
72                                 int (*Server_Channel_Ok)(unsigned long
73                                                          channelBytes),
74                                 int (*Server_Channel_Init)
75                                  (void *x, unsigned char *clientStr,
76                                   u32 clientStrLen, u64 bytes));
77 ReqHandlerInfo_t *ReqHandlerFind(uuid_le switchTypeGuid);
78 int ReqHandlerDel(uuid_le switchTypeGuid);
79
80 #define uislib_ioremap_cache(addr, size) \
81         dbg_ioremap_cache(addr, size, __FILE__, __LINE__)
82
83 static inline void __iomem *
84 dbg_ioremap_cache(u64 addr, unsigned long size, char *file, int line)
85 {
86         void __iomem *new;
87
88         new = ioremap_cache(addr, size);
89         return new;
90 }
91
92 #define uislib_ioremap(addr, size) dbg_ioremap(addr, size, __FILE__, __LINE__)
93
94 static inline void *
95 dbg_ioremap(u64 addr, unsigned long size, char *file, int line)
96 {
97         void *new;
98
99         new = ioremap(addr, size);
100         return new;
101 }
102
103 #define uislib_iounmap(addr) dbg_iounmap(addr, __FILE__, __LINE__)
104
105 static inline void
106 dbg_iounmap(void __iomem *addr, char *file, int line)
107 {
108         iounmap(addr);
109 }
110
111 #define PROC_READ_BUFFER_SIZE 131072    /* size of the buffer to allocate to
112                                          * hold all of /proc/XXX/info */
113 int uisutil_add_proc_line_ex(int *total, char **buffer, int *buffer_remaining,
114                              char *format, ...);
115
116 int uisctrl_register_req_handler(int type, void *fptr,
117                                  ULTRA_VBUS_DEVICEINFO *chipset_driver_info);
118 int uisctrl_register_req_handler_ex(uuid_le switchTypeGuid,
119                                     const char *switch_type_name,
120                                     int (*fptr)(struct io_msgs *),
121                                     unsigned long min_channel_bytes,
122                                     int (*Server_Channel_Ok)(unsigned long
123                                                              channelBytes),
124                                     int (*Server_Channel_Init)
125                                     (void *x, unsigned char *clientStr,
126                                      u32 clientStrLen, u64 bytes),
127                                     ULTRA_VBUS_DEVICEINFO *chipset_DriverInfo);
128
129 int uisctrl_unregister_req_handler_ex(uuid_le switchTypeGuid);
130 unsigned char *util_map_virt(struct phys_info *sg);
131 void util_unmap_virt(struct phys_info *sg);
132 unsigned char *util_map_virt_atomic(struct phys_info *sg);
133 void util_unmap_virt_atomic(void *buf);
134 int uislib_server_inject_add_vnic(u32 switchNo, u32 BusNo, u32 numIntPorts,
135                                   u32 numExtPorts, MACARRAY pmac[],
136                                   pCHANNEL_HEADER **chan);
137 void uislib_server_inject_del_vnic(u32 switchNo, u32 busNo, u32 numIntPorts,
138                                    u32 numExtPorts);
139 int uislib_client_inject_add_bus(u32 busNo, uuid_le instGuid,
140                                  u64 channelAddr, ulong nChannelBytes);
141 int  uislib_client_inject_del_bus(u32 busNo);
142
143 int uislib_client_inject_add_vhba(u32 busNo, u32 devNo,
144                                   u64 phys_chan_addr, u32 chan_bytes,
145                                   int is_test_addr, uuid_le instGuid,
146                                   struct InterruptInfo *intr);
147 int  uislib_client_inject_pause_vhba(u32 busNo, u32 devNo);
148 int  uislib_client_inject_resume_vhba(u32 busNo, u32 devNo);
149 int uislib_client_inject_del_vhba(u32 busNo, u32 devNo);
150 int uislib_client_inject_add_vnic(u32 busNo, u32 devNo,
151                                   u64 phys_chan_addr, u32 chan_bytes,
152                                   int is_test_addr, uuid_le instGuid,
153                                   struct InterruptInfo *intr);
154 int uislib_client_inject_pause_vnic(u32 busNo, u32 devNo);
155 int uislib_client_inject_resume_vnic(u32 busNo, u32 devNo);
156 int uislib_client_inject_del_vnic(u32 busNo, u32 devNo);
157 #ifdef STORAGE_CHANNEL
158 u64 uislib_storage_channel(int client_id);
159 #endif
160 int uislib_get_owned_pdest(struct uisscsi_dest *pdest);
161
162 int uislib_send_event(CONTROLVM_ID id, CONTROLVM_MESSAGE_PACKET *event);
163
164 /* structure used by vhba & vnic to keep track of queue & thread info */
165 struct chaninfo {
166         struct uisqueue_info *queueinfo;
167         /* this specifies the queue structures for a channel */
168         /* ALLOCATED BY THE OTHER END - WE JUST GET A POINTER TO THE MEMORY */
169         spinlock_t insertlock;
170         /* currently used only in virtnic when sending data to uisnic */
171         /* to synchronize the inserts into the signal queue */
172         struct uisthread_info threadinfo;
173         /* this specifies the thread structures used by the thread that */
174         /* handles this channel */
175 };
176
177 /* this is the wait code for all the threads - it is used to get
178 * something from a queue choices: wait_for_completion_interruptible,
179 * _timeout, interruptible_timeout
180 */
181 #define UIS_THREAD_WAIT_MSEC(x) { \
182         set_current_state(TASK_INTERRUPTIBLE); \
183         schedule_timeout(msecs_to_jiffies(x)); \
184 }
185 #define UIS_THREAD_WAIT_USEC(x) { \
186         set_current_state(TASK_INTERRUPTIBLE); \
187         schedule_timeout(usecs_to_jiffies(x)); \
188 }
189 #define UIS_THREAD_WAIT UIS_THREAD_WAIT_MSEC(5)
190 #define UIS_THREAD_WAIT_SEC(x) { \
191         set_current_state(TASK_INTERRUPTIBLE); \
192         schedule_timeout((x)*HZ); \
193 }
194
195 /* This is a hack until we fix IOVM to initialize the channel header
196  * correctly at DEVICE_CREATE time, INSTEAD OF waiting until
197  * DEVICE_CONFIGURE time.
198  */
199 static inline void
200 wait_for_valid_guid(uuid_le __iomem *guid)
201 {
202         uuid_le tmpguid;
203
204         while (1) {
205                 memcpy_fromio((void *)&tmpguid,
206                               (void __iomem *)guid, sizeof(uuid_le));
207                 if (uuid_le_cmp(tmpguid, NULL_UUID_LE) != 0)
208                         break;
209                 LOGERR("Waiting for non-0 GUID (why???)...\n");
210                 UIS_THREAD_WAIT_SEC(5);
211         }
212         LOGERR("OK... GUID is non-0 now\n");
213 }
214
215 /* CopyFragsInfoFromSkb returns the number of entries added to frags array
216  * Returns -1 on failure.
217  */
218 unsigned int uisutil_copy_fragsinfo_from_skb(unsigned char *calling_ctx,
219                                              void *skb_in,
220                                              unsigned int firstfraglen,
221                                              unsigned int frags_max,
222                                              struct phys_info frags[]);
223
224 static inline unsigned int
225 issue_vmcall_io_controlvm_addr(u64 *control_addr, u32 *control_bytes)
226 {
227         VMCALL_IO_CONTROLVM_ADDR_PARAMS params;
228         int result = VMCALL_SUCCESS;
229         u64 physaddr;
230
231         physaddr = virt_to_phys(&params);
232         ISSUE_IO_VMCALL(VMCALL_IO_CONTROLVM_ADDR, physaddr, result);
233         if (VMCALL_SUCCESSFUL(result)) {
234                 *control_addr = params.ChannelAddress;
235                 *control_bytes = params.ChannelBytes;
236         }
237         return result;
238 }
239
240 static inline unsigned int issue_vmcall_io_diag_addr(u64 *diag_channel_addr)
241 {
242         VMCALL_IO_DIAG_ADDR_PARAMS params;
243         int result = VMCALL_SUCCESS;
244         u64 physaddr;
245
246         physaddr = virt_to_phys(&params);
247         ISSUE_IO_VMCALL(VMCALL_IO_DIAG_ADDR, physaddr, result);
248         if (VMCALL_SUCCESSFUL(result))
249                 *diag_channel_addr = params.ChannelAddress;
250         return result;
251 }
252
253 static inline unsigned int issue_vmcall_io_visorserial_addr(u64 *channel_addr)
254 {
255         VMCALL_IO_VISORSERIAL_ADDR_PARAMS params;
256         int result = VMCALL_SUCCESS;
257         u64 physaddr;
258
259         physaddr = virt_to_phys(&params);
260         ISSUE_IO_VMCALL(VMCALL_IO_VISORSERIAL_ADDR, physaddr, result);
261         if (VMCALL_SUCCESSFUL(result))
262                 *channel_addr = params.ChannelAddress;
263         return result;
264 }
265
266 static inline s64 issue_vmcall_query_guest_virtual_time_offset(void)
267 {
268         u64 result = VMCALL_SUCCESS;
269         u64 physaddr = 0;
270
271         ISSUE_IO_VMCALL(VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET, physaddr,
272                         result);
273         return result;
274 }
275
276 static inline s64 issue_vmcall_measurement_do_nothing(void)
277 {
278         u64 result = VMCALL_SUCCESS;
279         u64 physaddr = 0;
280
281         ISSUE_IO_VMCALL(VMCALL_MEASUREMENT_DO_NOTHING, physaddr, result);
282         return result;
283 }
284
285 struct log_info_t {
286         volatile unsigned long long last_cycles;
287         unsigned long long delta_sum[64];
288         unsigned long long delta_cnt[64];
289         unsigned long long max_delta[64];
290         unsigned long long min_delta[64];
291 };
292
293 static inline int issue_vmcall_update_physical_time(u64 adjustment)
294 {
295         int result = VMCALL_SUCCESS;
296
297         ISSUE_IO_VMCALL(VMCALL_UPDATE_PHYSICAL_TIME, adjustment, result);
298         return result;
299 }
300
301 static inline unsigned int
302 Issue_VMCALL_CHANNEL_MISMATCH(const char *ChannelName,
303                               const char *ItemName,
304                               u32 SourceLineNumber, const char *path_n_fn)
305 {
306         VMCALL_CHANNEL_VERSION_MISMATCH_PARAMS params;
307         int result = VMCALL_SUCCESS;
308         u64 physaddr;
309         char *last_slash = NULL;
310
311         strlcpy(params.ChannelName, ChannelName,
312                 lengthof(VMCALL_CHANNEL_VERSION_MISMATCH_PARAMS, ChannelName));
313         strlcpy(params.ItemName, ItemName,
314                 lengthof(VMCALL_CHANNEL_VERSION_MISMATCH_PARAMS, ItemName));
315         params.SourceLineNumber = SourceLineNumber;
316
317         last_slash = strrchr(path_n_fn, '/');
318         if (last_slash != NULL) {
319                 last_slash++;
320                 strlcpy(params.SourceFileName, last_slash,
321                         lengthof(VMCALL_CHANNEL_VERSION_MISMATCH_PARAMS,
322                                  SourceFileName));
323         } else
324                 strlcpy(params.SourceFileName,
325                         "Cannot determine source filename",
326                         lengthof(VMCALL_CHANNEL_VERSION_MISMATCH_PARAMS,
327                                  SourceFileName));
328
329         physaddr = virt_to_phys(&params);
330         ISSUE_IO_VMCALL(VMCALL_CHANNEL_VERSION_MISMATCH, physaddr, result);
331         return result;
332 }
333
334 static inline unsigned int Issue_VMCALL_FATAL_BYE_BYE(void)
335 {
336         int result = VMCALL_SUCCESS;
337         u64 physaddr = 0;
338
339         ISSUE_IO_VMCALL(VMCALL_GENERIC_SURRENDER_QUANTUM_FOREVER, physaddr,
340                         result);
341         return result;
342 }
343
344 #define UIS_DAEMONIZE(nam)
345 void *uislib_cache_alloc(struct kmem_cache *cur_pool, char *fn, int ln);
346 #define UISCACHEALLOC(cur_pool) uislib_cache_alloc(cur_pool, __FILE__, __LINE__)
347 void uislib_cache_free(struct kmem_cache *cur_pool, void *p, char *fn, int ln);
348 #define UISCACHEFREE(cur_pool, p) \
349         uislib_cache_free(cur_pool, p, __FILE__, __LINE__)
350
351 void uislib_enable_channel_interrupts(u32 busNo, u32 devNo,
352                                       int (*interrupt)(void *),
353                                       void *interrupt_context);
354 void uislib_disable_channel_interrupts(u32 busNo, u32 devNo);
355 void uislib_force_channel_interrupt(u32 busNo, u32 devNo);
356
357 #endif /* __UISUTILS__H__ */