blob: 6c0569177646e3e243735d5ea41165c6b91871d8 [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{
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 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200128 * This function returns -EIO if write has failed
Oren Weil3ce72722011-05-15 13:43:43 +0300129 */
Tomas Winkler169d1332012-06-19 09:13:35 +0300130int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
131 unsigned char *buf, unsigned long length)
Oren Weil3ce72722011-05-15 13:43:43 +0300132{
Tomas Winkler169d1332012-06-19 09:13:35 +0300133 unsigned long rem, dw_cnt;
134 u32 *reg_buf = (u32 *)buf;
135 int i;
136 int empty_slots;
Oren Weil3ce72722011-05-15 13:43:43 +0300137
Oren Weil3ce72722011-05-15 13:43:43 +0300138
139 dev_dbg(&dev->pdev->dev,
140 "mei_write_message header=%08x.\n",
141 *((u32 *) header));
142
Tomas Winkler169d1332012-06-19 09:13:35 +0300143 empty_slots = mei_count_empty_write_slots(dev);
144 dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
Oren Weil3ce72722011-05-15 13:43:43 +0300145
Tomas Winkler169d1332012-06-19 09:13:35 +0300146 dw_cnt = (length + sizeof(*header) + 3) / 4;
147 if (empty_slots < 0 || dw_cnt > empty_slots)
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200148 return -EIO;
Oren Weil3ce72722011-05-15 13:43:43 +0300149
150 mei_reg_write(dev, H_CB_WW, *((u32 *) header));
151
Tomas Winkler169d1332012-06-19 09:13:35 +0300152 for (i = 0; i < length / 4; i++)
153 mei_reg_write(dev, H_CB_WW, reg_buf[i]);
154
155 rem = length & 0x3;
156 if (rem > 0) {
157 u32 reg = 0;
158 memcpy(&reg, &buf[length - rem], rem);
159 mei_reg_write(dev, H_CB_WW, reg);
Oren Weil3ce72722011-05-15 13:43:43 +0300160 }
161
Tomas Winkler169d1332012-06-19 09:13:35 +0300162 dev->host_hw_state = mei_hcsr_read(dev);
Oren Weil3ce72722011-05-15 13:43:43 +0300163 dev->host_hw_state |= H_IG;
164 mei_hcsr_set(dev);
165 dev->me_hw_state = mei_mecsr_read(dev);
166 if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200167 return -EIO;
Oren Weil3ce72722011-05-15 13:43:43 +0300168
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200169 return 0;
Oren Weil3ce72722011-05-15 13:43:43 +0300170}
171
172/**
173 * mei_count_full_read_slots - counts read full slots.
174 *
175 * @dev: the device structure
176 *
177 * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
178 */
179int mei_count_full_read_slots(struct mei_device *dev)
180{
181 char read_ptr, write_ptr;
182 unsigned char buffer_depth, filled_slots;
183
184 dev->me_hw_state = mei_mecsr_read(dev);
185 buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
186 read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
187 write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
188 filled_slots = (unsigned char) (write_ptr - read_ptr);
189
190 /* check for overflow */
191 if (filled_slots > buffer_depth)
192 return -EOVERFLOW;
193
194 dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
195 return (int)filled_slots;
196}
197
198/**
199 * mei_read_slots - reads a message from mei device.
200 *
201 * @dev: the device structure
202 * @buffer: message buffer will be written
203 * @buffer_length: message size will be read
204 */
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200205void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
206 unsigned long buffer_length)
Oren Weil3ce72722011-05-15 13:43:43 +0300207{
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200208 u32 *reg_buf = (u32 *)buffer;
Oren Weil3ce72722011-05-15 13:43:43 +0300209
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200210 for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
211 *reg_buf++ = mei_mecbrw_read(dev);
Oren Weil3ce72722011-05-15 13:43:43 +0300212
213 if (buffer_length > 0) {
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200214 u32 reg = mei_mecbrw_read(dev);
215 memcpy(reg_buf, &reg, buffer_length);
Oren Weil3ce72722011-05-15 13:43:43 +0300216 }
217
218 dev->host_hw_state |= H_IG;
219 mei_hcsr_set(dev);
220}
221
222/**
223 * mei_flow_ctrl_creds - checks flow_control credentials.
224 *
225 * @dev: the device structure
226 * @cl: private data of the file object
227 *
228 * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
229 * -ENOENT if mei_cl is not present
230 * -EINVAL if single_recv_buf == 0
231 */
232int mei_flow_ctrl_creds(struct mei_device *dev, struct mei_cl *cl)
233{
234 int i;
235
Tomas Winklercf9673d2011-06-06 10:44:33 +0300236 if (!dev->me_clients_num)
Oren Weil3ce72722011-05-15 13:43:43 +0300237 return 0;
238
239 if (cl->mei_flow_ctrl_creds > 0)
240 return 1;
241
Tomas Winklercf9673d2011-06-06 10:44:33 +0300242 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weil3ce72722011-05-15 13:43:43 +0300243 struct mei_me_client *me_cl = &dev->me_clients[i];
244 if (me_cl->client_id == cl->me_client_id) {
245 if (me_cl->mei_flow_ctrl_creds) {
246 if (WARN_ON(me_cl->props.single_recv_buf == 0))
247 return -EINVAL;
248 return 1;
249 } else {
250 return 0;
251 }
252 }
253 }
254 return -ENOENT;
255}
256
257/**
258 * mei_flow_ctrl_reduce - reduces flow_control.
259 *
260 * @dev: the device structure
261 * @cl: private data of the file object
262 * @returns
263 * 0 on success
264 * -ENOENT when me client is not found
Justin P. Mattock5f9092f2012-03-12 07:18:09 -0700265 * -EINVAL when ctrl credits are <= 0
Oren Weil3ce72722011-05-15 13:43:43 +0300266 */
267int mei_flow_ctrl_reduce(struct mei_device *dev, struct mei_cl *cl)
268{
269 int i;
270
Tomas Winklercf9673d2011-06-06 10:44:33 +0300271 if (!dev->me_clients_num)
Oren Weil3ce72722011-05-15 13:43:43 +0300272 return -ENOENT;
273
Tomas Winklercf9673d2011-06-06 10:44:33 +0300274 for (i = 0; i < dev->me_clients_num; i++) {
Oren Weil3ce72722011-05-15 13:43:43 +0300275 struct mei_me_client *me_cl = &dev->me_clients[i];
276 if (me_cl->client_id == cl->me_client_id) {
277 if (me_cl->props.single_recv_buf != 0) {
278 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
279 return -EINVAL;
280 dev->me_clients[i].mei_flow_ctrl_creds--;
281 } else {
282 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
283 return -EINVAL;
284 cl->mei_flow_ctrl_creds--;
285 }
286 return 0;
287 }
288 }
289 return -ENOENT;
290}
291
292/**
293 * mei_send_flow_control - sends flow control to fw.
294 *
295 * @dev: the device structure
296 * @cl: private data of the file object
297 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200298 * This function returns -EIO on write failure
Oren Weil3ce72722011-05-15 13:43:43 +0300299 */
300int mei_send_flow_control(struct mei_device *dev, struct mei_cl *cl)
301{
302 struct mei_msg_hdr *mei_hdr;
303 struct hbm_flow_control *mei_flow_control;
304
305 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
306 mei_hdr->host_addr = 0;
307 mei_hdr->me_addr = 0;
308 mei_hdr->length = sizeof(struct hbm_flow_control);
309 mei_hdr->msg_complete = 1;
310 mei_hdr->reserved = 0;
311
312 mei_flow_control = (struct hbm_flow_control *) &dev->wr_msg_buf[1];
Julia Lawallc0569982011-09-16 08:57:33 +0200313 memset(mei_flow_control, 0, sizeof(*mei_flow_control));
Oren Weil3ce72722011-05-15 13:43:43 +0300314 mei_flow_control->host_addr = cl->host_client_id;
315 mei_flow_control->me_addr = cl->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200316 mei_flow_control->hbm_cmd = MEI_FLOW_CONTROL_CMD;
Oren Weil3ce72722011-05-15 13:43:43 +0300317 memset(mei_flow_control->reserved, 0,
318 sizeof(mei_flow_control->reserved));
319 dev_dbg(&dev->pdev->dev, "sending flow control host client = %d, ME client = %d\n",
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200320 cl->host_client_id, cl->me_client_id);
321
322 return mei_write_message(dev, mei_hdr,
Oren Weil3ce72722011-05-15 13:43:43 +0300323 (unsigned char *) mei_flow_control,
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200324 sizeof(struct hbm_flow_control));
Oren Weil3ce72722011-05-15 13:43:43 +0300325}
326
327/**
328 * mei_other_client_is_connecting - checks if other
329 * client with the same client id is connected.
330 *
331 * @dev: the device structure
332 * @cl: private data of the file object
333 *
334 * returns 1 if other client is connected, 0 - otherwise.
335 */
336int mei_other_client_is_connecting(struct mei_device *dev,
337 struct mei_cl *cl)
338{
339 struct mei_cl *cl_pos = NULL;
340 struct mei_cl *cl_next = NULL;
341
342 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
343 if ((cl_pos->state == MEI_FILE_CONNECTING) &&
344 (cl_pos != cl) &&
345 cl->me_client_id == cl_pos->me_client_id)
346 return 1;
347
348 }
349 return 0;
350}
351
352/**
353 * mei_disconnect - sends disconnect message to fw.
354 *
355 * @dev: the device structure
356 * @cl: private data of the file object
357 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200358 * This function returns -EIO on write failure
Oren Weil3ce72722011-05-15 13:43:43 +0300359 */
360int mei_disconnect(struct mei_device *dev, struct mei_cl *cl)
361{
362 struct mei_msg_hdr *mei_hdr;
363 struct hbm_client_disconnect_request *mei_cli_disconnect;
364
365 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
366 mei_hdr->host_addr = 0;
367 mei_hdr->me_addr = 0;
368 mei_hdr->length = sizeof(struct hbm_client_disconnect_request);
369 mei_hdr->msg_complete = 1;
370 mei_hdr->reserved = 0;
371
372 mei_cli_disconnect =
373 (struct hbm_client_disconnect_request *) &dev->wr_msg_buf[1];
Julia Lawallc0569982011-09-16 08:57:33 +0200374 memset(mei_cli_disconnect, 0, sizeof(*mei_cli_disconnect));
Oren Weil3ce72722011-05-15 13:43:43 +0300375 mei_cli_disconnect->host_addr = cl->host_client_id;
376 mei_cli_disconnect->me_addr = cl->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200377 mei_cli_disconnect->hbm_cmd = CLIENT_DISCONNECT_REQ_CMD;
Oren Weil3ce72722011-05-15 13:43:43 +0300378 mei_cli_disconnect->reserved[0] = 0;
379
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200380 return mei_write_message(dev, mei_hdr,
Oren Weil3ce72722011-05-15 13:43:43 +0300381 (unsigned char *) mei_cli_disconnect,
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200382 sizeof(struct hbm_client_disconnect_request));
Oren Weil3ce72722011-05-15 13:43:43 +0300383}
384
385/**
386 * mei_connect - sends connect message to fw.
387 *
388 * @dev: the device structure
389 * @cl: private data of the file object
390 *
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200391 * This function returns -EIO on write failure
Oren Weil3ce72722011-05-15 13:43:43 +0300392 */
393int mei_connect(struct mei_device *dev, struct mei_cl *cl)
394{
395 struct mei_msg_hdr *mei_hdr;
396 struct hbm_client_connect_request *mei_cli_connect;
397
398 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
399 mei_hdr->host_addr = 0;
400 mei_hdr->me_addr = 0;
401 mei_hdr->length = sizeof(struct hbm_client_connect_request);
402 mei_hdr->msg_complete = 1;
403 mei_hdr->reserved = 0;
404
405 mei_cli_connect =
406 (struct hbm_client_connect_request *) &dev->wr_msg_buf[1];
407 mei_cli_connect->host_addr = cl->host_client_id;
408 mei_cli_connect->me_addr = cl->me_client_id;
Tomas Winkler1ca7e782012-02-26 23:18:57 +0200409 mei_cli_connect->hbm_cmd = CLIENT_CONNECT_REQ_CMD;
Oren Weil3ce72722011-05-15 13:43:43 +0300410 mei_cli_connect->reserved = 0;
411
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200412 return mei_write_message(dev, mei_hdr,
Oren Weil3ce72722011-05-15 13:43:43 +0300413 (unsigned char *) mei_cli_connect,
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200414 sizeof(struct hbm_client_connect_request));
Oren Weil3ce72722011-05-15 13:43:43 +0300415}