Commit | Line | Data |
---|---|---|
bb1b0133 TW |
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 | ||
4fcbc99b | 17 | #include <linux/export.h> |
bb1b0133 TW |
18 | #include <linux/pci.h> |
19 | #include <linux/sched.h> | |
20 | #include <linux/wait.h> | |
21 | #include <linux/mei.h> | |
180ea05b | 22 | #include <linux/pm_runtime.h> |
bb1b0133 TW |
23 | |
24 | #include "mei_dev.h" | |
0edb23fc | 25 | #include "hbm.h" |
12d00665 | 26 | #include "client.h" |
bb1b0133 | 27 | |
285e2996 AU |
28 | static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status) |
29 | { | |
30 | #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status | |
31 | switch (status) { | |
32 | MEI_CL_CS(SUCCESS); | |
33 | MEI_CL_CS(NOT_FOUND); | |
34 | MEI_CL_CS(ALREADY_STARTED); | |
35 | MEI_CL_CS(OUT_OF_RESOURCES); | |
36 | MEI_CL_CS(MESSAGE_SMALL); | |
37 | default: return "unknown"; | |
38 | } | |
39 | #undef MEI_CL_CCS | |
40 | } | |
41 | ||
42 | /** | |
43 | * mei_cl_conn_status_to_errno - convert client connect response | |
44 | * status to error code | |
45 | * | |
46 | * @status: client connect response status | |
47 | * | |
48 | * returns corresponding error code | |
49 | */ | |
50 | static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status) | |
51 | { | |
52 | switch (status) { | |
53 | case MEI_CL_CONN_SUCCESS: return 0; | |
54 | case MEI_CL_CONN_NOT_FOUND: return -ENOTTY; | |
55 | case MEI_CL_CONN_ALREADY_STARTED: return -EBUSY; | |
56 | case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY; | |
57 | case MEI_CL_CONN_MESSAGE_SMALL: return -EINVAL; | |
58 | default: return -EINVAL; | |
59 | } | |
60 | } | |
61 | ||
84b3294a TW |
62 | /** |
63 | * mei_hbm_idle - set hbm to idle state | |
64 | * | |
65 | * @dev: the device structure | |
66 | */ | |
67 | void mei_hbm_idle(struct mei_device *dev) | |
68 | { | |
69 | dev->init_clients_timer = 0; | |
70 | dev->hbm_state = MEI_HBM_IDLE; | |
71 | } | |
72 | ||
73 | /** | |
74 | * mei_hbm_reset - reset hbm counters and book keeping data structurs | |
75 | * | |
76 | * @dev: the device structure | |
77 | */ | |
78 | void mei_hbm_reset(struct mei_device *dev) | |
79 | { | |
80 | dev->me_clients_num = 0; | |
81 | dev->me_client_presentation_num = 0; | |
82 | dev->me_client_index = 0; | |
83 | ||
84 | kfree(dev->me_clients); | |
85 | dev->me_clients = NULL; | |
86 | ||
87 | mei_hbm_idle(dev); | |
88 | } | |
89 | ||
a40b260d TW |
90 | /** |
91 | * mei_hbm_me_cl_allocate - allocates storage for me clients | |
92 | * | |
93 | * @dev: the device structure | |
94 | * | |
544f9460 | 95 | * returns 0 on success -ENOMEM on allocation failure |
a40b260d | 96 | */ |
544f9460 | 97 | static int mei_hbm_me_cl_allocate(struct mei_device *dev) |
a40b260d TW |
98 | { |
99 | struct mei_me_client *clients; | |
100 | int b; | |
101 | ||
84b3294a | 102 | mei_hbm_reset(dev); |
1aee351a | 103 | |
a40b260d TW |
104 | /* count how many ME clients we have */ |
105 | for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX) | |
106 | dev->me_clients_num++; | |
107 | ||
1aee351a | 108 | if (dev->me_clients_num == 0) |
544f9460 | 109 | return 0; |
a40b260d | 110 | |
e19555ce | 111 | dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%ld.\n", |
a40b260d TW |
112 | dev->me_clients_num * sizeof(struct mei_me_client)); |
113 | /* allocate storage for ME clients representation */ | |
114 | clients = kcalloc(dev->me_clients_num, | |
115 | sizeof(struct mei_me_client), GFP_KERNEL); | |
116 | if (!clients) { | |
117 | dev_err(&dev->pdev->dev, "memory allocation for ME clients failed.\n"); | |
544f9460 | 118 | return -ENOMEM; |
a40b260d TW |
119 | } |
120 | dev->me_clients = clients; | |
544f9460 | 121 | return 0; |
a40b260d TW |
122 | } |
123 | ||
cd51ed64 TW |
124 | /** |
125 | * mei_hbm_cl_hdr - construct client hbm header | |
393b148f | 126 | * |
cd51ed64 TW |
127 | * @cl: - client |
128 | * @hbm_cmd: host bus message command | |
129 | * @buf: buffer for cl header | |
130 | * @len: buffer length | |
131 | */ | |
132 | static inline | |
133 | void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len) | |
134 | { | |
135 | struct mei_hbm_cl_cmd *cmd = buf; | |
136 | ||
137 | memset(cmd, 0, len); | |
138 | ||
139 | cmd->hbm_cmd = hbm_cmd; | |
140 | cmd->host_addr = cl->host_client_id; | |
141 | cmd->me_addr = cl->me_client_id; | |
142 | } | |
143 | ||
144 | /** | |
83ce0741 | 145 | * mei_hbm_cl_addr_equal - tells if they have the same address |
cd51ed64 | 146 | * |
83ce0741 AU |
147 | * @cl: - client |
148 | * @buf: buffer with cl header | |
cd51ed64 | 149 | * |
83ce0741 | 150 | * returns true if addresses are the same |
cd51ed64 TW |
151 | */ |
152 | static inline | |
153 | bool mei_hbm_cl_addr_equal(struct mei_cl *cl, void *buf) | |
154 | { | |
155 | struct mei_hbm_cl_cmd *cmd = buf; | |
156 | return cl->host_client_id == cmd->host_addr && | |
157 | cl->me_client_id == cmd->me_addr; | |
158 | } | |
159 | ||
160 | ||
9b0d5efc TW |
161 | int mei_hbm_start_wait(struct mei_device *dev) |
162 | { | |
163 | int ret; | |
164 | if (dev->hbm_state > MEI_HBM_START) | |
165 | return 0; | |
166 | ||
167 | mutex_unlock(&dev->device_lock); | |
168 | ret = wait_event_interruptible_timeout(dev->wait_recvd_msg, | |
169 | dev->hbm_state == MEI_HBM_IDLE || | |
544f9460 | 170 | dev->hbm_state >= MEI_HBM_STARTED, |
7d93e58d | 171 | mei_secs_to_jiffies(MEI_HBM_TIMEOUT)); |
9b0d5efc TW |
172 | mutex_lock(&dev->device_lock); |
173 | ||
174 | if (ret <= 0 && (dev->hbm_state <= MEI_HBM_START)) { | |
175 | dev->hbm_state = MEI_HBM_IDLE; | |
8b513d0c | 176 | dev_err(&dev->pdev->dev, "waiting for mei start failed\n"); |
7ca96aa2 | 177 | return -ETIME; |
9b0d5efc TW |
178 | } |
179 | return 0; | |
180 | } | |
181 | ||
bb1b0133 | 182 | /** |
8120e720 | 183 | * mei_hbm_start_req - sends start request message. |
bb1b0133 TW |
184 | * |
185 | * @dev: the device structure | |
544f9460 TW |
186 | * |
187 | * returns 0 on success and < 0 on failure | |
bb1b0133 | 188 | */ |
9b0d5efc | 189 | int mei_hbm_start_req(struct mei_device *dev) |
bb1b0133 | 190 | { |
e46f1874 | 191 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; |
bb1b0133 TW |
192 | struct hbm_host_version_request *start_req; |
193 | const size_t len = sizeof(struct hbm_host_version_request); | |
544f9460 | 194 | int ret; |
bb1b0133 | 195 | |
e46f1874 | 196 | mei_hbm_hdr(mei_hdr, len); |
bb1b0133 TW |
197 | |
198 | /* host start message */ | |
e46f1874 | 199 | start_req = (struct hbm_host_version_request *)dev->wr_msg.data; |
bb1b0133 TW |
200 | memset(start_req, 0, len); |
201 | start_req->hbm_cmd = HOST_START_REQ_CMD; | |
202 | start_req->host_version.major_version = HBM_MAJOR_VERSION; | |
203 | start_req->host_version.minor_version = HBM_MINOR_VERSION; | |
204 | ||
9b0d5efc | 205 | dev->hbm_state = MEI_HBM_IDLE; |
544f9460 TW |
206 | ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data); |
207 | if (ret) { | |
208 | dev_err(&dev->pdev->dev, "version message write failed: ret = %d\n", | |
209 | ret); | |
210 | return ret; | |
bb1b0133 | 211 | } |
544f9460 | 212 | |
9b0d5efc | 213 | dev->hbm_state = MEI_HBM_START; |
bb1b0133 | 214 | dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; |
9b0d5efc | 215 | return 0; |
bb1b0133 TW |
216 | } |
217 | ||
9b0d5efc | 218 | /* |
8120e720 | 219 | * mei_hbm_enum_clients_req - sends enumeration client request message. |
bb1b0133 TW |
220 | * |
221 | * @dev: the device structure | |
222 | * | |
544f9460 | 223 | * returns 0 on success and < 0 on failure |
bb1b0133 | 224 | */ |
544f9460 | 225 | static int mei_hbm_enum_clients_req(struct mei_device *dev) |
bb1b0133 | 226 | { |
e46f1874 | 227 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; |
bb1b0133 TW |
228 | struct hbm_host_enum_request *enum_req; |
229 | const size_t len = sizeof(struct hbm_host_enum_request); | |
544f9460 TW |
230 | int ret; |
231 | ||
bb1b0133 | 232 | /* enumerate clients */ |
e46f1874 | 233 | mei_hbm_hdr(mei_hdr, len); |
bb1b0133 | 234 | |
e46f1874 TW |
235 | enum_req = (struct hbm_host_enum_request *)dev->wr_msg.data; |
236 | memset(enum_req, 0, len); | |
bb1b0133 TW |
237 | enum_req->hbm_cmd = HOST_ENUM_REQ_CMD; |
238 | ||
544f9460 TW |
239 | ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data); |
240 | if (ret) { | |
241 | dev_err(&dev->pdev->dev, "enumeration request write failed: ret = %d.\n", | |
242 | ret); | |
243 | return ret; | |
bb1b0133 | 244 | } |
9b0d5efc | 245 | dev->hbm_state = MEI_HBM_ENUM_CLIENTS; |
bb1b0133 | 246 | dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; |
544f9460 | 247 | return 0; |
bb1b0133 TW |
248 | } |
249 | ||
8120e720 | 250 | /** |
393b148f | 251 | * mei_hbm_prop_req - request property for a single client |
8120e720 TW |
252 | * |
253 | * @dev: the device structure | |
254 | * | |
544f9460 | 255 | * returns 0 on success and < 0 on failure |
8120e720 | 256 | */ |
bb1b0133 | 257 | |
8120e720 | 258 | static int mei_hbm_prop_req(struct mei_device *dev) |
bb1b0133 TW |
259 | { |
260 | ||
e46f1874 | 261 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; |
bb1b0133 TW |
262 | struct hbm_props_request *prop_req; |
263 | const size_t len = sizeof(struct hbm_props_request); | |
264 | unsigned long next_client_index; | |
1aee351a | 265 | unsigned long client_num; |
544f9460 | 266 | int ret; |
bb1b0133 TW |
267 | |
268 | client_num = dev->me_client_presentation_num; | |
269 | ||
270 | next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, | |
271 | dev->me_client_index); | |
272 | ||
273 | /* We got all client properties */ | |
274 | if (next_client_index == MEI_CLIENTS_MAX) { | |
9b0d5efc | 275 | dev->hbm_state = MEI_HBM_STARTED; |
bb1b0133 TW |
276 | schedule_work(&dev->init_work); |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
281 | dev->me_clients[client_num].client_id = next_client_index; | |
282 | dev->me_clients[client_num].mei_flow_ctrl_creds = 0; | |
283 | ||
e46f1874 TW |
284 | mei_hbm_hdr(mei_hdr, len); |
285 | prop_req = (struct hbm_props_request *)dev->wr_msg.data; | |
bb1b0133 TW |
286 | |
287 | memset(prop_req, 0, sizeof(struct hbm_props_request)); | |
288 | ||
289 | ||
290 | prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD; | |
291 | prop_req->address = next_client_index; | |
292 | ||
544f9460 TW |
293 | ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data); |
294 | if (ret) { | |
295 | dev_err(&dev->pdev->dev, "properties request write failed: ret = %d\n", | |
296 | ret); | |
297 | return ret; | |
bb1b0133 TW |
298 | } |
299 | ||
300 | dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT; | |
301 | dev->me_client_index = next_client_index; | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
4fcbc99b TW |
306 | /* |
307 | * mei_hbm_pg - sends pg command | |
308 | * | |
309 | * @dev: the device structure | |
310 | * @pg_cmd: the pg command code | |
311 | * | |
312 | * This function returns -EIO on write failure | |
313 | */ | |
314 | int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd) | |
315 | { | |
316 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; | |
317 | struct hbm_power_gate *req; | |
318 | const size_t len = sizeof(struct hbm_power_gate); | |
319 | int ret; | |
320 | ||
321 | mei_hbm_hdr(mei_hdr, len); | |
322 | ||
323 | req = (struct hbm_power_gate *)dev->wr_msg.data; | |
324 | memset(req, 0, len); | |
325 | req->hbm_cmd = pg_cmd; | |
326 | ||
327 | ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data); | |
328 | if (ret) | |
329 | dev_err(&dev->pdev->dev, "power gate command write failed.\n"); | |
330 | return ret; | |
331 | } | |
332 | EXPORT_SYMBOL_GPL(mei_hbm_pg); | |
333 | ||
e46f1874 | 334 | /** |
6bb948c9 | 335 | * mei_hbm_stop_req - send stop request message |
e46f1874 TW |
336 | * |
337 | * @dev - mei device | |
6bb948c9 TW |
338 | * @cl: client info |
339 | * | |
340 | * This function returns -EIO on write failure | |
e46f1874 | 341 | */ |
6bb948c9 | 342 | static int mei_hbm_stop_req(struct mei_device *dev) |
e46f1874 | 343 | { |
6bb948c9 | 344 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; |
e46f1874 | 345 | struct hbm_host_stop_request *req = |
6bb948c9 | 346 | (struct hbm_host_stop_request *)dev->wr_msg.data; |
e46f1874 TW |
347 | const size_t len = sizeof(struct hbm_host_stop_request); |
348 | ||
349 | mei_hbm_hdr(mei_hdr, len); | |
350 | ||
351 | memset(req, 0, len); | |
352 | req->hbm_cmd = HOST_STOP_REQ_CMD; | |
353 | req->reason = DRIVER_STOP_REQUEST; | |
6bb948c9 TW |
354 | |
355 | return mei_write_message(dev, mei_hdr, dev->wr_msg.data); | |
e46f1874 TW |
356 | } |
357 | ||
bb1b0133 | 358 | /** |
83ce0741 | 359 | * mei_hbm_cl_flow_control_req - sends flow control request. |
bb1b0133 TW |
360 | * |
361 | * @dev: the device structure | |
8120e720 | 362 | * @cl: client info |
bb1b0133 TW |
363 | * |
364 | * This function returns -EIO on write failure | |
365 | */ | |
8120e720 | 366 | int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl) |
bb1b0133 | 367 | { |
e46f1874 | 368 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; |
bb1b0133 TW |
369 | const size_t len = sizeof(struct hbm_flow_control); |
370 | ||
e46f1874 TW |
371 | mei_hbm_hdr(mei_hdr, len); |
372 | mei_hbm_cl_hdr(cl, MEI_FLOW_CONTROL_CMD, dev->wr_msg.data, len); | |
bb1b0133 | 373 | |
46922186 | 374 | cl_dbg(dev, cl, "sending flow control\n"); |
bb1b0133 | 375 | |
e46f1874 | 376 | return mei_write_message(dev, mei_hdr, dev->wr_msg.data); |
bb1b0133 TW |
377 | } |
378 | ||
6bbda15f | 379 | /** |
393b148f | 380 | * mei_hbm_add_single_flow_creds - adds single buffer credentials. |
6bbda15f | 381 | * |
393b148f | 382 | * @dev: the device structure |
6bbda15f | 383 | * @flow: flow control. |
12d00665 AU |
384 | * |
385 | * return 0 on success, < 0 otherwise | |
6bbda15f | 386 | */ |
12d00665 | 387 | static int mei_hbm_add_single_flow_creds(struct mei_device *dev, |
6bbda15f TW |
388 | struct hbm_flow_control *flow) |
389 | { | |
12d00665 AU |
390 | struct mei_me_client *me_cl; |
391 | int id; | |
392 | ||
393 | id = mei_me_cl_by_id(dev, flow->me_addr); | |
394 | if (id < 0) { | |
395 | dev_err(&dev->pdev->dev, "no such me client %d\n", | |
396 | flow->me_addr); | |
397 | return id; | |
398 | } | |
399 | ||
400 | me_cl = &dev->me_clients[id]; | |
401 | if (me_cl->props.single_recv_buf) { | |
402 | me_cl->mei_flow_ctrl_creds++; | |
403 | dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n", | |
404 | flow->me_addr); | |
405 | dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n", | |
406 | me_cl->mei_flow_ctrl_creds); | |
407 | } else { | |
408 | BUG(); /* error in flow control */ | |
6bbda15f | 409 | } |
12d00665 AU |
410 | |
411 | return 0; | |
6bbda15f TW |
412 | } |
413 | ||
414 | /** | |
415 | * mei_hbm_cl_flow_control_res - flow control response from me | |
416 | * | |
417 | * @dev: the device structure | |
418 | * @flow_control: flow control response bus message | |
419 | */ | |
420 | static void mei_hbm_cl_flow_control_res(struct mei_device *dev, | |
421 | struct hbm_flow_control *flow_control) | |
422 | { | |
31f88f57 | 423 | struct mei_cl *cl; |
6bbda15f TW |
424 | |
425 | if (!flow_control->host_addr) { | |
426 | /* single receive buffer */ | |
427 | mei_hbm_add_single_flow_creds(dev, flow_control); | |
428 | return; | |
429 | } | |
430 | ||
431 | /* normal connection */ | |
31f88f57 | 432 | list_for_each_entry(cl, &dev->file_list, link) { |
6bbda15f TW |
433 | if (mei_hbm_cl_addr_equal(cl, flow_control)) { |
434 | cl->mei_flow_ctrl_creds++; | |
435 | dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n", | |
436 | flow_control->host_addr, flow_control->me_addr); | |
437 | dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n", | |
438 | cl->mei_flow_ctrl_creds); | |
439 | break; | |
440 | } | |
441 | } | |
442 | } | |
443 | ||
444 | ||
bb1b0133 | 445 | /** |
8120e720 | 446 | * mei_hbm_cl_disconnect_req - sends disconnect message to fw. |
bb1b0133 TW |
447 | * |
448 | * @dev: the device structure | |
8120e720 | 449 | * @cl: a client to disconnect from |
bb1b0133 TW |
450 | * |
451 | * This function returns -EIO on write failure | |
452 | */ | |
8120e720 | 453 | int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl) |
bb1b0133 | 454 | { |
e46f1874 | 455 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; |
bb1b0133 TW |
456 | const size_t len = sizeof(struct hbm_client_connect_request); |
457 | ||
e46f1874 TW |
458 | mei_hbm_hdr(mei_hdr, len); |
459 | mei_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, dev->wr_msg.data, len); | |
bb1b0133 | 460 | |
e46f1874 | 461 | return mei_write_message(dev, mei_hdr, dev->wr_msg.data); |
bb1b0133 TW |
462 | } |
463 | ||
6bb948c9 TW |
464 | /** |
465 | * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW | |
466 | * | |
467 | * @dev: the device structure | |
468 | * @cl: a client to disconnect from | |
469 | * | |
470 | * This function returns -EIO on write failure | |
471 | */ | |
472 | int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl) | |
473 | { | |
474 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; | |
475 | const size_t len = sizeof(struct hbm_client_connect_response); | |
476 | ||
477 | mei_hbm_hdr(mei_hdr, len); | |
478 | mei_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, dev->wr_msg.data, len); | |
479 | ||
480 | return mei_write_message(dev, mei_hdr, dev->wr_msg.data); | |
481 | } | |
482 | ||
6bbda15f TW |
483 | /** |
484 | * mei_hbm_cl_disconnect_res - disconnect response from ME | |
485 | * | |
486 | * @dev: the device structure | |
487 | * @rs: disconnect response bus message | |
488 | */ | |
489 | static void mei_hbm_cl_disconnect_res(struct mei_device *dev, | |
490 | struct hbm_client_connect_response *rs) | |
491 | { | |
492 | struct mei_cl *cl; | |
64092858 | 493 | struct mei_cl_cb *cb, *next; |
6bbda15f | 494 | |
285e2996 AU |
495 | dev_dbg(&dev->pdev->dev, "hbm: disconnect response cl:host=%02d me=%02d status=%d\n", |
496 | rs->me_addr, rs->host_addr, rs->status); | |
6bbda15f | 497 | |
64092858 TW |
498 | list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) { |
499 | cl = cb->cl; | |
6bbda15f | 500 | |
64092858 TW |
501 | /* this should not happen */ |
502 | if (WARN_ON(!cl)) { | |
503 | list_del(&cb->list); | |
6bbda15f TW |
504 | return; |
505 | } | |
506 | ||
6bbda15f | 507 | if (mei_hbm_cl_addr_equal(cl, rs)) { |
64092858 | 508 | list_del(&cb->list); |
285e2996 | 509 | if (rs->status == MEI_CL_DISCONN_SUCCESS) |
6bbda15f TW |
510 | cl->state = MEI_FILE_DISCONNECTED; |
511 | ||
512 | cl->status = 0; | |
513 | cl->timer_count = 0; | |
514 | break; | |
515 | } | |
516 | } | |
517 | } | |
518 | ||
bb1b0133 | 519 | /** |
8120e720 | 520 | * mei_hbm_cl_connect_req - send connection request to specific me client |
bb1b0133 TW |
521 | * |
522 | * @dev: the device structure | |
8120e720 | 523 | * @cl: a client to connect to |
bb1b0133 | 524 | * |
8120e720 | 525 | * returns -EIO on write failure |
bb1b0133 | 526 | */ |
8120e720 | 527 | int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl) |
bb1b0133 | 528 | { |
e46f1874 | 529 | struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr; |
bb1b0133 TW |
530 | const size_t len = sizeof(struct hbm_client_connect_request); |
531 | ||
e46f1874 TW |
532 | mei_hbm_hdr(mei_hdr, len); |
533 | mei_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, dev->wr_msg.data, len); | |
bb1b0133 | 534 | |
e46f1874 | 535 | return mei_write_message(dev, mei_hdr, dev->wr_msg.data); |
bb1b0133 TW |
536 | } |
537 | ||
6bbda15f | 538 | /** |
83ce0741 | 539 | * mei_hbm_cl_connect_res - connect response from the ME |
6bbda15f TW |
540 | * |
541 | * @dev: the device structure | |
542 | * @rs: connect response bus message | |
543 | */ | |
544 | static void mei_hbm_cl_connect_res(struct mei_device *dev, | |
545 | struct hbm_client_connect_response *rs) | |
546 | { | |
547 | ||
548 | struct mei_cl *cl; | |
64092858 | 549 | struct mei_cl_cb *cb, *next; |
6bbda15f | 550 | |
285e2996 AU |
551 | dev_dbg(&dev->pdev->dev, "hbm: connect response cl:host=%02d me=%02d status=%s\n", |
552 | rs->me_addr, rs->host_addr, | |
553 | mei_cl_conn_status_str(rs->status)); | |
6bbda15f | 554 | |
64092858 | 555 | cl = NULL; |
6bbda15f | 556 | |
64092858 | 557 | list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) { |
6bbda15f | 558 | |
64092858 TW |
559 | cl = cb->cl; |
560 | /* this should not happen */ | |
561 | if (WARN_ON(!cl)) { | |
562 | list_del_init(&cb->list); | |
563 | continue; | |
564 | } | |
6bbda15f | 565 | |
64092858 TW |
566 | if (cb->fop_type != MEI_FOP_CONNECT) |
567 | continue; | |
6bbda15f | 568 | |
64092858 TW |
569 | if (mei_hbm_cl_addr_equal(cl, rs)) { |
570 | list_del(&cb->list); | |
571 | break; | |
6bbda15f TW |
572 | } |
573 | } | |
64092858 TW |
574 | |
575 | if (!cl) | |
576 | return; | |
577 | ||
578 | cl->timer_count = 0; | |
579 | if (rs->status == MEI_CL_CONN_SUCCESS) | |
580 | cl->state = MEI_FILE_CONNECTED; | |
581 | else | |
582 | cl->state = MEI_FILE_DISCONNECTED; | |
583 | cl->status = mei_cl_conn_status_to_errno(rs->status); | |
6bbda15f TW |
584 | } |
585 | ||
586 | ||
bb1b0133 | 587 | /** |
83ce0741 AU |
588 | * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware |
589 | * host sends disconnect response | |
bb1b0133 TW |
590 | * |
591 | * @dev: the device structure. | |
8120e720 | 592 | * @disconnect_req: disconnect request bus message from the me |
6bb948c9 TW |
593 | * |
594 | * returns -ENOMEM on allocation failure | |
bb1b0133 | 595 | */ |
6bb948c9 | 596 | static int mei_hbm_fw_disconnect_req(struct mei_device *dev, |
bb1b0133 TW |
597 | struct hbm_client_connect_request *disconnect_req) |
598 | { | |
31f88f57 | 599 | struct mei_cl *cl; |
6bb948c9 | 600 | struct mei_cl_cb *cb; |
bb1b0133 | 601 | |
31f88f57 | 602 | list_for_each_entry(cl, &dev->file_list, link) { |
cd51ed64 | 603 | if (mei_hbm_cl_addr_equal(cl, disconnect_req)) { |
bb1b0133 TW |
604 | dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n", |
605 | disconnect_req->host_addr, | |
606 | disconnect_req->me_addr); | |
cd51ed64 TW |
607 | cl->state = MEI_FILE_DISCONNECTED; |
608 | cl->timer_count = 0; | |
bb1b0133 | 609 | |
6bb948c9 TW |
610 | cb = mei_io_cb_init(cl, NULL); |
611 | if (!cb) | |
612 | return -ENOMEM; | |
613 | cb->fop_type = MEI_FOP_DISCONNECT_RSP; | |
614 | cl_dbg(dev, cl, "add disconnect response as first\n"); | |
615 | list_add(&cb->list, &dev->ctrl_wr_list.list); | |
616 | ||
bb1b0133 TW |
617 | break; |
618 | } | |
619 | } | |
6bb948c9 | 620 | return 0; |
bb1b0133 TW |
621 | } |
622 | ||
623 | ||
2c9b48ac TW |
624 | /** |
625 | * mei_hbm_version_is_supported - checks whether the driver can | |
626 | * support the hbm version of the device | |
627 | * | |
628 | * @dev: the device structure | |
629 | * returns true if driver can support hbm version of the device | |
630 | */ | |
631 | bool mei_hbm_version_is_supported(struct mei_device *dev) | |
632 | { | |
633 | return (dev->version.major_version < HBM_MAJOR_VERSION) || | |
634 | (dev->version.major_version == HBM_MAJOR_VERSION && | |
635 | dev->version.minor_version <= HBM_MINOR_VERSION); | |
636 | } | |
637 | ||
bb1b0133 TW |
638 | /** |
639 | * mei_hbm_dispatch - bottom half read routine after ISR to | |
640 | * handle the read bus message cmd processing. | |
641 | * | |
642 | * @dev: the device structure | |
643 | * @mei_hdr: header of bus message | |
544f9460 TW |
644 | * |
645 | * returns 0 on success and < 0 on failure | |
bb1b0133 | 646 | */ |
544f9460 | 647 | int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr) |
bb1b0133 TW |
648 | { |
649 | struct mei_bus_message *mei_msg; | |
650 | struct mei_me_client *me_client; | |
651 | struct hbm_host_version_response *version_res; | |
652 | struct hbm_client_connect_response *connect_res; | |
653 | struct hbm_client_connect_response *disconnect_res; | |
654 | struct hbm_client_connect_request *disconnect_req; | |
655 | struct hbm_flow_control *flow_control; | |
656 | struct hbm_props_response *props_res; | |
657 | struct hbm_host_enum_response *enum_res; | |
bb1b0133 TW |
658 | |
659 | /* read the message to our buffer */ | |
660 | BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf)); | |
661 | mei_read_slots(dev, dev->rd_msg_buf, hdr->length); | |
662 | mei_msg = (struct mei_bus_message *)dev->rd_msg_buf; | |
663 | ||
66ae460b TW |
664 | /* ignore spurious message and prevent reset nesting |
665 | * hbm is put to idle during system reset | |
666 | */ | |
667 | if (dev->hbm_state == MEI_HBM_IDLE) { | |
668 | dev_dbg(&dev->pdev->dev, "hbm: state is idle ignore spurious messages\n"); | |
669 | return 0; | |
670 | } | |
671 | ||
bb1b0133 TW |
672 | switch (mei_msg->hbm_cmd) { |
673 | case HOST_START_RES_CMD: | |
544f9460 TW |
674 | dev_dbg(&dev->pdev->dev, "hbm: start: response message received.\n"); |
675 | ||
676 | dev->init_clients_timer = 0; | |
677 | ||
bb1b0133 | 678 | version_res = (struct hbm_host_version_response *)mei_msg; |
2c9b48ac TW |
679 | |
680 | dev_dbg(&dev->pdev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n", | |
681 | HBM_MAJOR_VERSION, HBM_MINOR_VERSION, | |
682 | version_res->me_max_version.major_version, | |
683 | version_res->me_max_version.minor_version); | |
684 | ||
685 | if (version_res->host_version_supported) { | |
686 | dev->version.major_version = HBM_MAJOR_VERSION; | |
687 | dev->version.minor_version = HBM_MINOR_VERSION; | |
688 | } else { | |
689 | dev->version.major_version = | |
690 | version_res->me_max_version.major_version; | |
691 | dev->version.minor_version = | |
692 | version_res->me_max_version.minor_version; | |
693 | } | |
694 | ||
695 | if (!mei_hbm_version_is_supported(dev)) { | |
544f9460 | 696 | dev_warn(&dev->pdev->dev, "hbm: start: version mismatch - stopping the driver.\n"); |
bb1b0133 | 697 | |
544f9460 | 698 | dev->hbm_state = MEI_HBM_STOPPED; |
6bb948c9 | 699 | if (mei_hbm_stop_req(dev)) { |
544f9460 TW |
700 | dev_err(&dev->pdev->dev, "hbm: start: failed to send stop request\n"); |
701 | return -EIO; | |
702 | } | |
703 | break; | |
704 | } | |
9b0d5efc | 705 | |
544f9460 TW |
706 | if (dev->dev_state != MEI_DEV_INIT_CLIENTS || |
707 | dev->hbm_state != MEI_HBM_START) { | |
708 | dev_err(&dev->pdev->dev, "hbm: start: state mismatch, [%d, %d]\n", | |
709 | dev->dev_state, dev->hbm_state); | |
710 | return -EPROTO; | |
e46f1874 | 711 | } |
bb1b0133 | 712 | |
544f9460 TW |
713 | dev->hbm_state = MEI_HBM_STARTED; |
714 | ||
715 | if (mei_hbm_enum_clients_req(dev)) { | |
716 | dev_err(&dev->pdev->dev, "hbm: start: failed to send enumeration request\n"); | |
717 | return -EIO; | |
bb1b0133 TW |
718 | } |
719 | ||
9b0d5efc | 720 | wake_up_interruptible(&dev->wait_recvd_msg); |
bb1b0133 TW |
721 | break; |
722 | ||
723 | case CLIENT_CONNECT_RES_CMD: | |
544f9460 TW |
724 | dev_dbg(&dev->pdev->dev, "hbm: client connect response: message received.\n"); |
725 | ||
bb1b0133 | 726 | connect_res = (struct hbm_client_connect_response *) mei_msg; |
6bbda15f | 727 | mei_hbm_cl_connect_res(dev, connect_res); |
bb1b0133 TW |
728 | wake_up(&dev->wait_recvd_msg); |
729 | break; | |
730 | ||
731 | case CLIENT_DISCONNECT_RES_CMD: | |
544f9460 TW |
732 | dev_dbg(&dev->pdev->dev, "hbm: client disconnect response: message received.\n"); |
733 | ||
bb1b0133 | 734 | disconnect_res = (struct hbm_client_connect_response *) mei_msg; |
6bbda15f | 735 | mei_hbm_cl_disconnect_res(dev, disconnect_res); |
bb1b0133 TW |
736 | wake_up(&dev->wait_recvd_msg); |
737 | break; | |
738 | ||
739 | case MEI_FLOW_CONTROL_CMD: | |
544f9460 TW |
740 | dev_dbg(&dev->pdev->dev, "hbm: client flow control response: message received.\n"); |
741 | ||
bb1b0133 | 742 | flow_control = (struct hbm_flow_control *) mei_msg; |
6bbda15f | 743 | mei_hbm_cl_flow_control_res(dev, flow_control); |
bb1b0133 TW |
744 | break; |
745 | ||
4fcbc99b TW |
746 | case MEI_PG_ISOLATION_ENTRY_RES_CMD: |
747 | dev_dbg(&dev->pdev->dev, "power gate isolation entry response received\n"); | |
ba9cdd0e | 748 | dev->pg_event = MEI_PG_EVENT_RECEIVED; |
4fcbc99b TW |
749 | if (waitqueue_active(&dev->wait_pg)) |
750 | wake_up(&dev->wait_pg); | |
751 | break; | |
752 | ||
753 | case MEI_PG_ISOLATION_EXIT_REQ_CMD: | |
754 | dev_dbg(&dev->pdev->dev, "power gate isolation exit request received\n"); | |
ba9cdd0e | 755 | dev->pg_event = MEI_PG_EVENT_RECEIVED; |
4fcbc99b TW |
756 | if (waitqueue_active(&dev->wait_pg)) |
757 | wake_up(&dev->wait_pg); | |
180ea05b TW |
758 | else |
759 | /* | |
760 | * If the driver is not waiting on this then | |
761 | * this is HW initiated exit from PG. | |
762 | * Start runtime pm resume sequence to exit from PG. | |
763 | */ | |
764 | pm_request_resume(&dev->pdev->dev); | |
4fcbc99b TW |
765 | break; |
766 | ||
bb1b0133 | 767 | case HOST_CLIENT_PROPERTIES_RES_CMD: |
544f9460 TW |
768 | dev_dbg(&dev->pdev->dev, "hbm: properties response: message received.\n"); |
769 | ||
770 | dev->init_clients_timer = 0; | |
771 | ||
772 | if (dev->me_clients == NULL) { | |
773 | dev_err(&dev->pdev->dev, "hbm: properties response: mei_clients not allocated\n"); | |
774 | return -EPROTO; | |
775 | } | |
776 | ||
bb1b0133 TW |
777 | props_res = (struct hbm_props_response *)mei_msg; |
778 | me_client = &dev->me_clients[dev->me_client_presentation_num]; | |
779 | ||
544f9460 TW |
780 | if (props_res->status) { |
781 | dev_err(&dev->pdev->dev, "hbm: properties response: wrong status = %d\n", | |
782 | props_res->status); | |
783 | return -EPROTO; | |
bb1b0133 TW |
784 | } |
785 | ||
786 | if (me_client->client_id != props_res->address) { | |
544f9460 TW |
787 | dev_err(&dev->pdev->dev, "hbm: properties response: address mismatch %d ?= %d\n", |
788 | me_client->client_id, props_res->address); | |
789 | return -EPROTO; | |
bb1b0133 TW |
790 | } |
791 | ||
792 | if (dev->dev_state != MEI_DEV_INIT_CLIENTS || | |
9b0d5efc | 793 | dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) { |
544f9460 TW |
794 | dev_err(&dev->pdev->dev, "hbm: properties response: state mismatch, [%d, %d]\n", |
795 | dev->dev_state, dev->hbm_state); | |
796 | return -EPROTO; | |
bb1b0133 TW |
797 | } |
798 | ||
799 | me_client->props = props_res->client_properties; | |
800 | dev->me_client_index++; | |
801 | dev->me_client_presentation_num++; | |
802 | ||
8120e720 | 803 | /* request property for the next client */ |
544f9460 TW |
804 | if (mei_hbm_prop_req(dev)) |
805 | return -EIO; | |
bb1b0133 TW |
806 | |
807 | break; | |
808 | ||
809 | case HOST_ENUM_RES_CMD: | |
544f9460 TW |
810 | dev_dbg(&dev->pdev->dev, "hbm: enumeration response: message received\n"); |
811 | ||
812 | dev->init_clients_timer = 0; | |
813 | ||
bb1b0133 | 814 | enum_res = (struct hbm_host_enum_response *) mei_msg; |
23f5a322 TW |
815 | BUILD_BUG_ON(sizeof(dev->me_clients_map) |
816 | < sizeof(enum_res->valid_addresses)); | |
817 | memcpy(dev->me_clients_map, enum_res->valid_addresses, | |
818 | sizeof(enum_res->valid_addresses)); | |
544f9460 TW |
819 | |
820 | if (dev->dev_state != MEI_DEV_INIT_CLIENTS || | |
821 | dev->hbm_state != MEI_HBM_ENUM_CLIENTS) { | |
822 | dev_err(&dev->pdev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n", | |
823 | dev->dev_state, dev->hbm_state); | |
824 | return -EPROTO; | |
bb1b0133 | 825 | } |
544f9460 TW |
826 | |
827 | if (mei_hbm_me_cl_allocate(dev)) { | |
828 | dev_err(&dev->pdev->dev, "hbm: enumeration response: cannot allocate clients array\n"); | |
829 | return -ENOMEM; | |
830 | } | |
831 | ||
832 | dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES; | |
833 | ||
834 | /* first property request */ | |
835 | if (mei_hbm_prop_req(dev)) | |
836 | return -EIO; | |
837 | ||
bb1b0133 TW |
838 | break; |
839 | ||
840 | case HOST_STOP_RES_CMD: | |
544f9460 TW |
841 | dev_dbg(&dev->pdev->dev, "hbm: stop response: message received\n"); |
842 | ||
843 | dev->init_clients_timer = 0; | |
844 | ||
845 | if (dev->hbm_state != MEI_HBM_STOPPED) { | |
846 | dev_err(&dev->pdev->dev, "hbm: stop response: state mismatch, [%d, %d]\n", | |
847 | dev->dev_state, dev->hbm_state); | |
848 | return -EPROTO; | |
849 | } | |
9b0d5efc | 850 | |
33ec0826 | 851 | dev->dev_state = MEI_DEV_POWER_DOWN; |
544f9460 TW |
852 | dev_info(&dev->pdev->dev, "hbm: stop response: resetting.\n"); |
853 | /* force the reset */ | |
854 | return -EPROTO; | |
bb1b0133 TW |
855 | break; |
856 | ||
857 | case CLIENT_DISCONNECT_REQ_CMD: | |
544f9460 TW |
858 | dev_dbg(&dev->pdev->dev, "hbm: disconnect request: message received\n"); |
859 | ||
bb1b0133 | 860 | disconnect_req = (struct hbm_client_connect_request *)mei_msg; |
8120e720 | 861 | mei_hbm_fw_disconnect_req(dev, disconnect_req); |
bb1b0133 TW |
862 | break; |
863 | ||
864 | case ME_STOP_REQ_CMD: | |
544f9460 | 865 | dev_dbg(&dev->pdev->dev, "hbm: stop request: message received\n"); |
544f9460 | 866 | dev->hbm_state = MEI_HBM_STOPPED; |
6bb948c9 TW |
867 | if (mei_hbm_stop_req(dev)) { |
868 | dev_err(&dev->pdev->dev, "hbm: start: failed to send stop request\n"); | |
869 | return -EIO; | |
870 | } | |
bb1b0133 | 871 | break; |
bb1b0133 TW |
872 | default: |
873 | BUG(); | |
874 | break; | |
875 | ||
876 | } | |
544f9460 | 877 | return 0; |
bb1b0133 TW |
878 | } |
879 |