]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/mei/interface.c
drivers:staging:mei Fix some typos in staging/mei
[mv-sheeva.git] / drivers / staging / mei / interface.c
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2012, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  */
16
17 #include <linux/pci.h>
18 #include "mei_dev.h"
19 #include "mei.h"
20 #include "interface.h"
21
22
23
24 /**
25  * mei_set_csr_register - writes H_CSR register to the mei device,
26  * and ignores the H_IS bit for it is write-one-to-zero.
27  *
28  * @dev: the device structure
29  */
30 void mei_hcsr_set(struct mei_device *dev)
31 {
32         if ((dev->host_hw_state & H_IS) == H_IS)
33                 dev->host_hw_state &= ~H_IS;
34         mei_reg_write(dev, H_CSR, dev->host_hw_state);
35         dev->host_hw_state = mei_hcsr_read(dev);
36 }
37
38 /**
39  * mei_csr_enable_interrupts - enables mei device interrupts
40  *
41  * @dev: the device structure
42  */
43 void mei_enable_interrupts(struct mei_device *dev)
44 {
45         dev->host_hw_state |= H_IE;
46         mei_hcsr_set(dev);
47 }
48
49 /**
50  * mei_csr_disable_interrupts - disables mei device interrupts
51  *
52  * @dev: the device structure
53  */
54 void mei_disable_interrupts(struct mei_device *dev)
55 {
56         dev->host_hw_state &= ~H_IE;
57         mei_hcsr_set(dev);
58 }
59
60 /**
61  * _host_get_filled_slots - gets number of device filled buffer slots
62  *
63  * @device: the device structure
64  *
65  * returns number of filled slots
66  */
67 static unsigned char _host_get_filled_slots(const struct mei_device *dev)
68 {
69         char read_ptr, write_ptr;
70
71         read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
72         write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
73
74         return (unsigned char) (write_ptr - read_ptr);
75 }
76
77 /**
78  * mei_host_buffer_is_empty - checks if host buffer is empty.
79  *
80  * @dev: the device structure
81  *
82  * returns 1 if empty, 0 - otherwise.
83  */
84 int mei_host_buffer_is_empty(struct mei_device *dev)
85 {
86         unsigned char filled_slots;
87
88         dev->host_hw_state = mei_hcsr_read(dev);
89         filled_slots = _host_get_filled_slots(dev);
90
91         if (filled_slots == 0)
92                 return 1;
93
94         return 0;
95 }
96
97 /**
98  * mei_count_empty_write_slots - counts write empty slots.
99  *
100  * @dev: the device structure
101  *
102  * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
103  */
104 int mei_count_empty_write_slots(struct mei_device *dev)
105 {
106         unsigned char buffer_depth, filled_slots, empty_slots;
107
108         dev->host_hw_state = mei_hcsr_read(dev);
109         buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
110         filled_slots = _host_get_filled_slots(dev);
111         empty_slots = buffer_depth - filled_slots;
112
113         /* check for overflow */
114         if (filled_slots > buffer_depth)
115                 return -EOVERFLOW;
116
117         return empty_slots;
118 }
119
120 /**
121  * mei_write_message - writes a message to mei device.
122  *
123  * @dev: the device structure
124  * @header: header of message
125  * @write_buffer: message buffer will be written
126  * @write_length: message size will be written
127  *
128  * returns 1 if success, 0 - otherwise.
129  */
130 int mei_write_message(struct mei_device *dev,
131                       struct mei_msg_hdr *header,
132                       unsigned char *write_buffer,
133                       unsigned long write_length)
134 {
135         u32 temp_msg = 0;
136         unsigned long bytes_written = 0;
137         unsigned char buffer_depth, filled_slots, empty_slots;
138         unsigned long dw_to_write;
139
140         dev->host_hw_state = mei_hcsr_read(dev);
141
142         dev_dbg(&dev->pdev->dev,
143                         "host_hw_state = 0x%08x.\n",
144                         dev->host_hw_state);
145
146         dev_dbg(&dev->pdev->dev,
147                         "mei_write_message header=%08x.\n",
148                         *((u32 *) header));
149
150         buffer_depth = (unsigned char) ((dev->host_hw_state & H_CBD) >> 24);
151         filled_slots = _host_get_filled_slots(dev);
152         empty_slots = buffer_depth - filled_slots;
153         dev_dbg(&dev->pdev->dev,
154                         "filled = %hu, empty = %hu.\n",
155                         filled_slots, empty_slots);
156
157         dw_to_write = ((write_length + 3) / 4);
158
159         if (dw_to_write > empty_slots)
160                 return 0;
161
162         mei_reg_write(dev, H_CB_WW, *((u32 *) header));
163
164         while (write_length >= 4) {
165                 mei_reg_write(dev, H_CB_WW,
166                                 *(u32 *) (write_buffer + bytes_written));
167                 bytes_written += 4;
168                 write_length -= 4;
169         }
170
171         if (write_length > 0) {
172                 memcpy(&temp_msg, &write_buffer[bytes_written], write_length);
173                 mei_reg_write(dev, H_CB_WW, temp_msg);
174         }
175
176         dev->host_hw_state |= H_IG;
177         mei_hcsr_set(dev);
178         dev->me_hw_state = mei_mecsr_read(dev);
179         if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
180                 return 0;
181
182         return 1;
183 }
184
185 /**
186  * mei_count_full_read_slots - counts read full slots.
187  *
188  * @dev: the device structure
189  *
190  * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
191  */
192 int mei_count_full_read_slots(struct mei_device *dev)
193 {
194         char read_ptr, write_ptr;
195         unsigned char buffer_depth, filled_slots;
196
197         dev->me_hw_state = mei_mecsr_read(dev);
198         buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
199         read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
200         write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
201         filled_slots = (unsigned char) (write_ptr - read_ptr);
202
203         /* check for overflow */
204         if (filled_slots > buffer_depth)
205                 return -EOVERFLOW;
206
207         dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
208         return (int)filled_slots;
209 }
210
211 /**
212  * mei_read_slots - reads a message from mei device.
213  *
214  * @dev: the device structure
215  * @buffer: message buffer will be written
216  * @buffer_length: message size will be read
217  */
218 void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
219                     unsigned long buffer_length)
220 {
221         u32 *reg_buf = (u32 *)buffer;
222
223         for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
224                 *reg_buf++ = mei_mecbrw_read(dev);
225
226         if (buffer_length > 0) {
227                 u32 reg = mei_mecbrw_read(dev);
228                 memcpy(reg_buf, &reg, buffer_length);
229         }
230
231         dev->host_hw_state |= H_IG;
232         mei_hcsr_set(dev);
233 }
234
235 /**
236  * mei_flow_ctrl_creds - checks flow_control credentials.
237  *
238  * @dev: the device structure
239  * @cl: private data of the file object
240  *
241  * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
242  *      -ENOENT if mei_cl is not present
243  *      -EINVAL if single_recv_buf == 0
244  */
245 int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
246 {
247         int i;
248
249         if (!dev->me_clients_num)
250                 return 0;
251
252         if (cl->mei_flow_ctrl_creds > 0)
253                 return 1;
254
255         for (i = 0; i < dev->me_clients_num; i++) {
256                 struct mei_me_client  *me_cl = &dev->me_clients[i];
257                 if (me_cl->client_id == cl->me_client_id) {
258                         if (me_cl->mei_flow_ctrl_creds) {
259                                 if (WARN_ON(me_cl->props.single_recv_buf == 0))
260                                         return -EINVAL;
261                                 return 1;
262                         } else {
263                                 return 0;
264                         }
265                 }
266         }
267         return -ENOENT;
268 }
269
270 /**
271  * mei_flow_ctrl_reduce - reduces flow_control.
272  *
273  * @dev: the device structure
274  * @cl: private data of the file object
275  * @returns
276  *      0 on success
277  *      -ENOENT when me client is not found
278  *      -EINVAL when ctrl credits are <= 0
279  */
280 int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
281 {
282         int i;
283
284         if (!dev->me_clients_num)
285                 return -ENOENT;
286
287         for (i = 0; i < dev->me_clients_num; i++) {
288                 struct mei_me_client  *me_cl = &dev->me_clients[i];
289                 if (me_cl->client_id == cl->me_client_id) {
290                         if (me_cl->props.single_recv_buf != 0) {
291                                 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
292                                         return -EINVAL;
293                                 dev->me_clients[i].mei_flow_ctrl_creds--;
294                         } else {
295                                 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
296                                         return -EINVAL;
297                                 cl->mei_flow_ctrl_creds--;
298                         }
299                         return 0;
300                 }
301         }
302         return -ENOENT;
303 }
304
305 /**
306  * mei_send_flow_control - sends flow control to fw.
307  *
308  * @dev: the device structure
309  * @cl: private data of the file object
310  *
311  * returns 1 if success, 0 - otherwise.
312  */
313 int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl)
314 {
315         struct mei_msg_hdr *mei_hdr;
316         struct hbm_flow_control *mei_flow_control;
317
318         mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
319         mei_hdr->host_addr = 0;
320         mei_hdr->me_addr = 0;
321         mei_hdr->length = sizeof(struct hbm_flow_control);
322         mei_hdr->msg_complete = 1;
323         mei_hdr->reserved = 0;
324
325         mei_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
326         memset(mei_flow_control, 0, sizeof(*mei_flow_control));
327         mei_flow_control->host_addr = cl->host_client_id;
328         mei_flow_control->me_addr = cl->me_client_id;
329         mei_flow_control->hbm_cmd = MEI_FLOW_CONTROL_CMD;
330         memset(mei_flow_control->reserved, 0,
331                         sizeof(mei_flow_control->reserved));
332         dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
333             cl->host_client_id, cl->me_client_id);
334         if (!mei_write_message(dev, mei_hdr,
335                                 (unsigned char *) mei_flow_control,
336                                 sizeof(struct hbm_flow_control)))
337                 return 0;
338
339         return 1;
340
341 }
342
343 /**
344  * mei_other_client_is_connecting - checks if other
345  *    client with the same client id is connected.
346  *
347  * @dev: the device structure
348  * @cl: private data of the file object
349  *
350  * returns 1 if other client is connected, 0 - otherwise.
351  */
352 int mei_other_client_is_connecting(struct mei_device *dev,
353                                 struct mei_cl *cl)
354 {
355         struct mei_cl *cl_pos = NULL;
356         struct mei_cl *cl_next = NULL;
357
358         list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
359                 if ((cl_pos->state == MEI_FILE_CONNECTING) &&
360                         (cl_pos != cl) &&
361                         cl->me_client_id == cl_pos->me_client_id)
362                         return 1;
363
364         }
365         return 0;
366 }
367
368 /**
369  * mei_disconnect - sends disconnect message to fw.
370  *
371  * @dev: the device structure
372  * @cl: private data of the file object
373  *
374  * returns 1 if success, 0 - otherwise.
375  */
376 int mei_disconnect(struct mei_device *dev, struct mei_cl *cl)
377 {
378         struct mei_msg_hdr *mei_hdr;
379         struct hbm_client_disconnect_request *mei_cli_disconnect;
380
381         mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
382         mei_hdr->host_addr = 0;
383         mei_hdr->me_addr = 0;
384         mei_hdr->length = sizeof(struct hbm_client_disconnect_request);
385         mei_hdr->msg_complete = 1;
386         mei_hdr->reserved = 0;
387
388         mei_cli_disconnect =
389             (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
390         memset(mei_cli_disconnect, 0, sizeof(*mei_cli_disconnect));
391         mei_cli_disconnect->host_addr = cl->host_client_id;
392         mei_cli_disconnect->me_addr = cl->me_client_id;
393         mei_cli_disconnect->hbm_cmd = CLIENT_DISCONNECT_REQ_CMD;
394         mei_cli_disconnect->reserved[0] = 0;
395
396         if (!mei_write_message(dev, mei_hdr,
397                                 (unsigned char *) mei_cli_disconnect,
398                                 sizeof(struct hbm_client_disconnect_request)))
399                 return 0;
400
401         return 1;
402 }
403
404 /**
405  * mei_connect - sends connect message to fw.
406  *
407  * @dev: the device structure
408  * @cl: private data of the file object
409  *
410  * returns 1 if success, 0 - otherwise.
411  */
412 int mei_connect(struct mei_device *dev, struct mei_cl *cl)
413 {
414         struct mei_msg_hdr *mei_hdr;
415         struct hbm_client_connect_request *mei_cli_connect;
416
417         mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
418         mei_hdr->host_addr = 0;
419         mei_hdr->me_addr = 0;
420         mei_hdr->length = sizeof(struct hbm_client_connect_request);
421         mei_hdr->msg_complete = 1;
422         mei_hdr->reserved = 0;
423
424         mei_cli_connect =
425             (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
426         mei_cli_connect->host_addr = cl->host_client_id;
427         mei_cli_connect->me_addr = cl->me_client_id;
428         mei_cli_connect->hbm_cmd = CLIENT_CONNECT_REQ_CMD;
429         mei_cli_connect->reserved = 0;
430
431         if (!mei_write_message(dev, mei_hdr,
432                                 (unsigned char *) mei_cli_connect,
433                                 sizeof(struct hbm_client_connect_request)))
434                 return 0;
435
436         return 1;
437 }