blob: 784a60626fa1c71375d5c8e94edc0f809dacfc0f [file] [log] [blame]
Oren Weil3ce72722011-05-15 13:43:43 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weil3ce72722011-05-15 13:43:43 +03005 *
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"
Tomas Winkler4f3afe12012-05-09 16:38:59 +030019#include <linux/mei.h>
Oren Weil3ce72722011-05-15 13:43:43 +030020#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 */
30void 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 */
43void 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 */
54void 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 */
67static 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 */
84int 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 */
104int mei_count_empty_write_slots(struct mei_device *dev)
105{
Tomas Winkler24aadc82012-06-25 23:46:27 +0300106 unsigned char filled_slots, empty_slots;
Oren Weil3ce72722011-05-15 13:43:43 +0300107
108 dev->host_hw_state = mei_hcsr_read(dev);
Oren Weil3ce72722011-05-15 13:43:43 +0300109 filled_slots = _host_get_filled_slots(dev);
Tomas Winkler24aadc82012-06-25 23:46:27 +0300110 empty_slots = dev->hbuf_depth - filled_slots;
Oren Weil3ce72722011-05-15 13:43:43 +0300111
112 /* check for overflow */
Tomas Winkler24aadc82012-06-25 23:46:27 +0300113 if (filled_slots > dev->hbuf_depth)
Oren Weil3ce72722011-05-15 13:43:43 +0300114 return -EOVERFLOW;
115
116 return empty_slots;
117}
118
119/**
120 * mei_write_message - writes a message to mei device.
121 *
122 * @dev: the device structure
123 * @header: header of message
124 * @write_buffer: message buffer will be written
125 * @write_length: message size will be written
126 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200127 * This function returns -EIO if write has failed
Oren Weil3ce72722011-05-15 13:43:43 +0300128 */
Tomas Winkler169d1332012-06-19 09:13:35 +0300129int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
130 unsigned char *buf, unsigned long length)
Oren Weil3ce72722011-05-15 13:43:43 +0300131{
Tomas Winkler169d1332012-06-19 09:13:35 +0300132 unsigned long rem, dw_cnt;
133 u32 *reg_buf = (u32 *)buf;
134 int i;
135 int empty_slots;
Oren Weil3ce72722011-05-15 13:43:43 +0300136
Oren Weil3ce72722011-05-15 13:43:43 +0300137
138 dev_dbg(&dev->pdev->dev,
139 "mei_write_message header=%08x.\n",
140 *((u32 *) header));
141
Tomas Winkler169d1332012-06-19 09:13:35 +0300142 empty_slots = mei_count_empty_write_slots(dev);
143 dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
Oren Weil3ce72722011-05-15 13:43:43 +0300144
Tomas Winkler169d1332012-06-19 09:13:35 +0300145 dw_cnt = (length + sizeof(*header) + 3) / 4;
146 if (empty_slots < 0 || dw_cnt > empty_slots)
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200147 return -EIO;
Oren Weil3ce72722011-05-15 13:43:43 +0300148
149 mei_reg_write(dev, H_CB_WW, *((u32 *) header));
150
Tomas Winkler169d1332012-06-19 09:13:35 +0300151 for (i = 0; i < length / 4; i++)
152 mei_reg_write(dev, H_CB_WW, reg_buf[i]);
153
154 rem = length & 0x3;
155 if (rem > 0) {
156 u32 reg = 0;
157 memcpy(&reg, &buf[length - rem], rem);
158 mei_reg_write(dev, H_CB_WW, reg);
Oren Weil3ce72722011-05-15 13:43:43 +0300159 }
160
Tomas Winkler169d1332012-06-19 09:13:35 +0300161 dev->host_hw_state = mei_hcsr_read(dev);
Oren Weil3ce72722011-05-15 13:43:43 +0300162 dev->host_hw_state |= H_IG;
163 mei_hcsr_set(dev);
164 dev->me_hw_state = mei_mecsr_read(dev);
165 if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200166 return -EIO;
Oren Weil3ce72722011-05-15 13:43:43 +0300167
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200168 return 0;
Oren Weil3ce72722011-05-15 13:43:43 +0300169}
170
171/**
172 * mei_count_full_read_slots - counts read full slots.
173 *
174 * @dev: the device structure
175 *
176 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
177 */
178int mei_count_full_read_slots(struct mei_device *dev)
179{
180 char read_ptr, write_ptr;
181 unsigned char buffer_depth, filled_slots;
182
183 dev->me_hw_state = mei_mecsr_read(dev);
184 buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
185 read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
186 write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
187 filled_slots = (unsigned char) (write_ptr - read_ptr);
188
189 /* check for overflow */
190 if (filled_slots > buffer_depth)
191 return -EOVERFLOW;
192
193 dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
194 return (int)filled_slots;
195}
196
197/**
198 * mei_read_slots - reads a message from mei device.
199 *
200 * @dev: the device structure
201 * @buffer: message buffer will be written
202 * @buffer_length: message size will be read
203 */
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200204void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
205 unsigned long buffer_length)
Oren Weil3ce72722011-05-15 13:43:43 +0300206{
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200207 u32 *reg_buf = (u32 *)buffer;
Oren Weil3ce72722011-05-15 13:43:43 +0300208
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200209 for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
210 *reg_buf++ = mei_mecbrw_read(dev);
Oren Weil3ce72722011-05-15 13:43:43 +0300211
212 if (buffer_length > 0) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200213 u32 reg = mei_mecbrw_read(dev);
214 memcpy(reg_buf, &reg, buffer_length);
Oren Weil3ce72722011-05-15 13:43:43 +0300215 }
216
217 dev->host_hw_state |= H_IG;
218 mei_hcsr_set(dev);
219}
220
221/**
222 * mei_flow_ctrl_creds - checks flow_control credentials.
223 *
224 * @dev: the device structure
225 * @cl: private data of the file object
226 *
227 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
228 * -ENOENT if mei_cl is not present
229 * -EINVAL if single_recv_buf == 0
230 */
231int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
232{
233 int i;
234
Tomas Winklercf9673d2011-06-06 10:44:33 +0300235 if (!dev->me_clients_num)
Oren Weil3ce72722011-05-15 13:43:43 +0300236 return 0;
237
238 if (cl->mei_flow_ctrl_creds > 0)
239 return 1;
240
Tomas Winklercf9673d2011-06-06 10:44:33 +0300241 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weil3ce72722011-05-15 13:43:43 +0300242 struct mei_me_client *me_cl = &dev->me_clients[i];
243 if (me_cl->client_id == cl->me_client_id) {
244 if (me_cl->mei_flow_ctrl_creds) {
245 if (WARN_ON(me_cl->props.single_recv_buf == 0))
246 return -EINVAL;
247 return 1;
248 } else {
249 return 0;
250 }
251 }
252 }
253 return -ENOENT;
254}
255
256/**
257 * mei_flow_ctrl_reduce - reduces flow_control.
258 *
259 * @dev: the device structure
260 * @cl: private data of the file object
261 * @returns
262 * 0 on success
263 * -ENOENT when me client is not found
Justin P. Mattock5f9092f32012-03-12 07:18:09 -0700264 * -EINVAL when ctrl credits are <= 0
Oren Weil3ce72722011-05-15 13:43:43 +0300265 */
266int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
267{
268 int i;
269
Tomas Winklercf9673d2011-06-06 10:44:33 +0300270 if (!dev->me_clients_num)
Oren Weil3ce72722011-05-15 13:43:43 +0300271 return -ENOENT;
272
Tomas Winklercf9673d2011-06-06 10:44:33 +0300273 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weil3ce72722011-05-15 13:43:43 +0300274 struct mei_me_client *me_cl = &dev->me_clients[i];
275 if (me_cl->client_id == cl->me_client_id) {
276 if (me_cl->props.single_recv_buf != 0) {
277 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
278 return -EINVAL;
279 dev->me_clients[i].mei_flow_ctrl_creds--;
280 } else {
281 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
282 return -EINVAL;
283 cl->mei_flow_ctrl_creds--;
284 }
285 return 0;
286 }
287 }
288 return -ENOENT;
289}
290
291/**
292 * mei_send_flow_control - sends flow control to fw.
293 *
294 * @dev: the device structure
295 * @cl: private data of the file object
296 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200297 * This function returns -EIO on write failure
Oren Weil3ce72722011-05-15 13:43:43 +0300298 */
299int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl)
300{
301 struct mei_msg_hdr *mei_hdr;
302 struct hbm_flow_control *mei_flow_control;
303
304 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
305 mei_hdr->host_addr = 0;
306 mei_hdr->me_addr = 0;
307 mei_hdr->length = sizeof(struct hbm_flow_control);
308 mei_hdr->msg_complete = 1;
309 mei_hdr->reserved = 0;
310
311 mei_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
Julia Lawallc0569982011-09-16 08:57:33 +0200312 memset(mei_flow_control, 0, sizeof(*mei_flow_control));
Oren Weil3ce72722011-05-15 13:43:43 +0300313 mei_flow_control->host_addr = cl->host_client_id;
314 mei_flow_control->me_addr = cl->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200315 mei_flow_control->hbm_cmd = MEI_FLOW_CONTROL_CMD;
Oren Weil3ce72722011-05-15 13:43:43 +0300316 memset(mei_flow_control->reserved, 0,
317 sizeof(mei_flow_control->reserved));
318 dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200319 cl->host_client_id, cl->me_client_id);
320
321 return mei_write_message(dev, mei_hdr,
Oren Weil3ce72722011-05-15 13:43:43 +0300322 (unsigned char *) mei_flow_control,
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200323 sizeof(struct hbm_flow_control));
Oren Weil3ce72722011-05-15 13:43:43 +0300324}
325
326/**
327 * mei_other_client_is_connecting - checks if other
328 * client with the same client id is connected.
329 *
330 * @dev: the device structure
331 * @cl: private data of the file object
332 *
333 * returns 1 if other client is connected, 0 - otherwise.
334 */
335int mei_other_client_is_connecting(struct mei_device *dev,
336 struct mei_cl *cl)
337{
338 struct mei_cl *cl_pos = NULL;
339 struct mei_cl *cl_next = NULL;
340
341 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
342 if ((cl_pos->state == MEI_FILE_CONNECTING) &&
343 (cl_pos != cl) &&
344 cl->me_client_id == cl_pos->me_client_id)
345 return 1;
346
347 }
348 return 0;
349}
350
351/**
352 * mei_disconnect - sends disconnect message to fw.
353 *
354 * @dev: the device structure
355 * @cl: private data of the file object
356 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200357 * This function returns -EIO on write failure
Oren Weil3ce72722011-05-15 13:43:43 +0300358 */
359int mei_disconnect(struct mei_device *dev, struct mei_cl *cl)
360{
361 struct mei_msg_hdr *mei_hdr;
362 struct hbm_client_disconnect_request *mei_cli_disconnect;
363
364 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
365 mei_hdr->host_addr = 0;
366 mei_hdr->me_addr = 0;
367 mei_hdr->length = sizeof(struct hbm_client_disconnect_request);
368 mei_hdr->msg_complete = 1;
369 mei_hdr->reserved = 0;
370
371 mei_cli_disconnect =
372 (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
Julia Lawallc0569982011-09-16 08:57:33 +0200373 memset(mei_cli_disconnect, 0, sizeof(*mei_cli_disconnect));
Oren Weil3ce72722011-05-15 13:43:43 +0300374 mei_cli_disconnect->host_addr = cl->host_client_id;
375 mei_cli_disconnect->me_addr = cl->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200376 mei_cli_disconnect->hbm_cmd = CLIENT_DISCONNECT_REQ_CMD;
Oren Weil3ce72722011-05-15 13:43:43 +0300377 mei_cli_disconnect->reserved[0] = 0;
378
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200379 return mei_write_message(dev, mei_hdr,
Oren Weil3ce72722011-05-15 13:43:43 +0300380 (unsigned char *) mei_cli_disconnect,
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200381 sizeof(struct hbm_client_disconnect_request));
Oren Weil3ce72722011-05-15 13:43:43 +0300382}
383
384/**
385 * mei_connect - sends connect message to fw.
386 *
387 * @dev: the device structure
388 * @cl: private data of the file object
389 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200390 * This function returns -EIO on write failure
Oren Weil3ce72722011-05-15 13:43:43 +0300391 */
392int mei_connect(struct mei_device *dev, struct mei_cl *cl)
393{
394 struct mei_msg_hdr *mei_hdr;
395 struct hbm_client_connect_request *mei_cli_connect;
396
397 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
398 mei_hdr->host_addr = 0;
399 mei_hdr->me_addr = 0;
400 mei_hdr->length = sizeof(struct hbm_client_connect_request);
401 mei_hdr->msg_complete = 1;
402 mei_hdr->reserved = 0;
403
404 mei_cli_connect =
405 (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
406 mei_cli_connect->host_addr = cl->host_client_id;
407 mei_cli_connect->me_addr = cl->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200408 mei_cli_connect->hbm_cmd = CLIENT_CONNECT_REQ_CMD;
Oren Weil3ce72722011-05-15 13:43:43 +0300409 mei_cli_connect->reserved = 0;
410
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200411 return mei_write_message(dev, mei_hdr,
Oren Weil3ce72722011-05-15 13:43:43 +0300412 (unsigned char *) mei_cli_connect,
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200413 sizeof(struct hbm_client_connect_request));
Oren Weil3ce72722011-05-15 13:43:43 +0300414}