Commit | Line | Data |
---|---|---|
3703f53b SP |
1 | /* |
2 | * ISHTP bus layer messages handling | |
3 | * | |
4 | * Copyright (c) 2003-2016, 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/export.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/sched.h> | |
20 | #include <linux/wait.h> | |
21 | #include <linux/spinlock.h> | |
3703f53b SP |
22 | #include "ishtp-dev.h" |
23 | #include "hbm.h" | |
24 | #include "client.h" | |
25 | ||
26 | /** | |
27 | * ishtp_hbm_fw_cl_allocate() - Allocate FW clients | |
28 | * @dev: ISHTP device instance | |
29 | * | |
30 | * Allocates storage for fw clients | |
31 | */ | |
32 | static void ishtp_hbm_fw_cl_allocate(struct ishtp_device *dev) | |
33 | { | |
34 | struct ishtp_fw_client *clients; | |
35 | int b; | |
36 | ||
37 | /* count how many ISH clients we have */ | |
38 | for_each_set_bit(b, dev->fw_clients_map, ISHTP_CLIENTS_MAX) | |
39 | dev->fw_clients_num++; | |
40 | ||
41 | if (dev->fw_clients_num <= 0) | |
42 | return; | |
43 | ||
44 | /* allocate storage for fw clients representation */ | |
45 | clients = kcalloc(dev->fw_clients_num, sizeof(struct ishtp_fw_client), | |
46 | GFP_KERNEL); | |
47 | if (!clients) { | |
48 | dev->dev_state = ISHTP_DEV_RESETTING; | |
49 | ish_hw_reset(dev); | |
50 | return; | |
51 | } | |
52 | dev->fw_clients = clients; | |
53 | } | |
54 | ||
55 | /** | |
56 | * ishtp_hbm_cl_hdr() - construct client hbm header | |
57 | * @cl: client | |
58 | * @hbm_cmd: host bus message command | |
59 | * @buf: buffer for cl header | |
60 | * @len: buffer length | |
61 | * | |
62 | * Initialize HBM buffer | |
63 | */ | |
64 | static inline void ishtp_hbm_cl_hdr(struct ishtp_cl *cl, uint8_t hbm_cmd, | |
65 | void *buf, size_t len) | |
66 | { | |
67 | struct ishtp_hbm_cl_cmd *cmd = buf; | |
68 | ||
69 | memset(cmd, 0, len); | |
70 | ||
71 | cmd->hbm_cmd = hbm_cmd; | |
72 | cmd->host_addr = cl->host_client_id; | |
73 | cmd->fw_addr = cl->fw_client_id; | |
74 | } | |
75 | ||
76 | /** | |
77 | * ishtp_hbm_cl_addr_equal() - Compare client address | |
78 | * @cl: client | |
79 | * @buf: Client command buffer | |
80 | * | |
81 | * Compare client address with the address in command buffer | |
82 | * | |
83 | * Return: True if they have the same address | |
84 | */ | |
85 | static inline bool ishtp_hbm_cl_addr_equal(struct ishtp_cl *cl, void *buf) | |
86 | { | |
87 | struct ishtp_hbm_cl_cmd *cmd = buf; | |
88 | ||
89 | return cl->host_client_id == cmd->host_addr && | |
90 | cl->fw_client_id == cmd->fw_addr; | |
91 | } | |
92 | ||
93 | /** | |
94 | * ishtp_hbm_start_wait() - Wait for HBM start message | |
95 | * @dev: ISHTP device instance | |
96 | * | |
97 | * Wait for HBM start message from firmware | |
98 | * | |
99 | * Return: 0 if HBM start is/was received else timeout error | |
100 | */ | |
101 | int ishtp_hbm_start_wait(struct ishtp_device *dev) | |
102 | { | |
103 | int ret; | |
104 | ||
105 | if (dev->hbm_state > ISHTP_HBM_START) | |
106 | return 0; | |
107 | ||
108 | dev_dbg(dev->devc, "Going to wait for ishtp start. hbm_state=%08X\n", | |
109 | dev->hbm_state); | |
110 | ret = wait_event_interruptible_timeout(dev->wait_hbm_recvd_msg, | |
111 | dev->hbm_state >= ISHTP_HBM_STARTED, | |
112 | (ISHTP_INTEROP_TIMEOUT * HZ)); | |
113 | ||
114 | dev_dbg(dev->devc, | |
115 | "Woke up from waiting for ishtp start. hbm_state=%08X\n", | |
116 | dev->hbm_state); | |
117 | ||
118 | if (ret <= 0 && (dev->hbm_state <= ISHTP_HBM_START)) { | |
119 | dev->hbm_state = ISHTP_HBM_IDLE; | |
120 | dev_err(dev->devc, | |
121 | "waiting for ishtp start failed. ret=%d hbm_state=%08X\n", | |
122 | ret, dev->hbm_state); | |
123 | return -ETIMEDOUT; | |
124 | } | |
125 | return 0; | |
126 | } | |
127 | ||
128 | /** | |
129 | * ishtp_hbm_start_req() - Send HBM start message | |
130 | * @dev: ISHTP device instance | |
131 | * | |
132 | * Send HBM start message to firmware | |
133 | * | |
134 | * Return: 0 if success else error code | |
135 | */ | |
136 | int ishtp_hbm_start_req(struct ishtp_device *dev) | |
137 | { | |
138 | struct ishtp_msg_hdr hdr; | |
139 | unsigned char data[128]; | |
140 | struct ishtp_msg_hdr *ishtp_hdr = &hdr; | |
141 | struct hbm_host_version_request *start_req; | |
142 | const size_t len = sizeof(struct hbm_host_version_request); | |
143 | ||
144 | ishtp_hbm_hdr(ishtp_hdr, len); | |
145 | ||
146 | /* host start message */ | |
147 | start_req = (struct hbm_host_version_request *)data; | |
148 | memset(start_req, 0, len); | |
149 | start_req->hbm_cmd = HOST_START_REQ_CMD; | |
150 | start_req->host_version.major_version = HBM_MAJOR_VERSION; | |
151 | start_req->host_version.minor_version = HBM_MINOR_VERSION; | |
152 | ||
153 | /* | |
154 | * (!) Response to HBM start may be so quick that this thread would get | |
155 | * preempted BEFORE managing to set hbm_state = ISHTP_HBM_START. | |
156 | * So set it at first, change back to ISHTP_HBM_IDLE upon failure | |
157 | */ | |
158 | dev->hbm_state = ISHTP_HBM_START; | |
159 | if (ishtp_write_message(dev, ishtp_hdr, data)) { | |
160 | dev_err(dev->devc, "version message send failed\n"); | |
161 | dev->dev_state = ISHTP_DEV_RESETTING; | |
162 | dev->hbm_state = ISHTP_HBM_IDLE; | |
163 | ish_hw_reset(dev); | |
164 | return -ENODEV; | |
165 | } | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | /** | |
171 | * ishtp_hbm_enum_clients_req() - Send client enum req | |
172 | * @dev: ISHTP device instance | |
173 | * | |
174 | * Send enumeration client request message | |
175 | * | |
176 | * Return: 0 if success else error code | |
177 | */ | |
178 | void ishtp_hbm_enum_clients_req(struct ishtp_device *dev) | |
179 | { | |
180 | struct ishtp_msg_hdr hdr; | |
181 | unsigned char data[128]; | |
182 | struct ishtp_msg_hdr *ishtp_hdr = &hdr; | |
183 | struct hbm_host_enum_request *enum_req; | |
184 | const size_t len = sizeof(struct hbm_host_enum_request); | |
185 | ||
186 | /* enumerate clients */ | |
187 | ishtp_hbm_hdr(ishtp_hdr, len); | |
188 | ||
189 | enum_req = (struct hbm_host_enum_request *)data; | |
190 | memset(enum_req, 0, len); | |
191 | enum_req->hbm_cmd = HOST_ENUM_REQ_CMD; | |
192 | ||
193 | if (ishtp_write_message(dev, ishtp_hdr, data)) { | |
194 | dev->dev_state = ISHTP_DEV_RESETTING; | |
195 | dev_err(dev->devc, "enumeration request send failed\n"); | |
196 | ish_hw_reset(dev); | |
197 | } | |
198 | dev->hbm_state = ISHTP_HBM_ENUM_CLIENTS; | |
199 | } | |
200 | ||
201 | /** | |
202 | * ishtp_hbm_prop_req() - Request property | |
203 | * @dev: ISHTP device instance | |
204 | * | |
205 | * Request property for a single client | |
206 | * | |
207 | * Return: 0 if success else error code | |
208 | */ | |
209 | static int ishtp_hbm_prop_req(struct ishtp_device *dev) | |
210 | { | |
211 | ||
212 | struct ishtp_msg_hdr hdr; | |
213 | unsigned char data[128]; | |
214 | struct ishtp_msg_hdr *ishtp_hdr = &hdr; | |
215 | struct hbm_props_request *prop_req; | |
216 | const size_t len = sizeof(struct hbm_props_request); | |
217 | unsigned long next_client_index; | |
218 | uint8_t client_num; | |
219 | ||
220 | client_num = dev->fw_client_presentation_num; | |
221 | ||
222 | next_client_index = find_next_bit(dev->fw_clients_map, | |
223 | ISHTP_CLIENTS_MAX, dev->fw_client_index); | |
224 | ||
225 | /* We got all client properties */ | |
226 | if (next_client_index == ISHTP_CLIENTS_MAX) { | |
227 | dev->hbm_state = ISHTP_HBM_WORKING; | |
228 | dev->dev_state = ISHTP_DEV_ENABLED; | |
229 | ||
230 | for (dev->fw_client_presentation_num = 1; | |
231 | dev->fw_client_presentation_num < client_num + 1; | |
232 | ++dev->fw_client_presentation_num) | |
233 | /* Add new client device */ | |
234 | ishtp_bus_new_client(dev); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | dev->fw_clients[client_num].client_id = next_client_index; | |
239 | ||
240 | ishtp_hbm_hdr(ishtp_hdr, len); | |
241 | prop_req = (struct hbm_props_request *)data; | |
242 | ||
243 | memset(prop_req, 0, sizeof(struct hbm_props_request)); | |
244 | ||
245 | prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD; | |
246 | prop_req->address = next_client_index; | |
247 | ||
248 | if (ishtp_write_message(dev, ishtp_hdr, data)) { | |
249 | dev->dev_state = ISHTP_DEV_RESETTING; | |
250 | dev_err(dev->devc, "properties request send failed\n"); | |
251 | ish_hw_reset(dev); | |
252 | return -EIO; | |
253 | } | |
254 | ||
255 | dev->fw_client_index = next_client_index; | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | /** | |
261 | * ishtp_hbm_stop_req() - Send HBM stop | |
262 | * @dev: ISHTP device instance | |
263 | * | |
264 | * Send stop request message | |
265 | */ | |
266 | static void ishtp_hbm_stop_req(struct ishtp_device *dev) | |
267 | { | |
268 | struct ishtp_msg_hdr hdr; | |
269 | unsigned char data[128]; | |
270 | struct ishtp_msg_hdr *ishtp_hdr = &hdr; | |
271 | struct hbm_host_stop_request *req; | |
272 | const size_t len = sizeof(struct hbm_host_stop_request); | |
273 | ||
274 | ishtp_hbm_hdr(ishtp_hdr, len); | |
275 | req = (struct hbm_host_stop_request *)data; | |
276 | ||
277 | memset(req, 0, sizeof(struct hbm_host_stop_request)); | |
278 | req->hbm_cmd = HOST_STOP_REQ_CMD; | |
279 | req->reason = DRIVER_STOP_REQUEST; | |
280 | ||
281 | ishtp_write_message(dev, ishtp_hdr, data); | |
282 | } | |
283 | ||
284 | /** | |
285 | * ishtp_hbm_cl_flow_control_req() - Send flow control request | |
286 | * @dev: ISHTP device instance | |
287 | * @cl: ISHTP client instance | |
288 | * | |
289 | * Send flow control request | |
290 | * | |
291 | * Return: 0 if success else error code | |
292 | */ | |
293 | int ishtp_hbm_cl_flow_control_req(struct ishtp_device *dev, | |
294 | struct ishtp_cl *cl) | |
295 | { | |
296 | struct ishtp_msg_hdr hdr; | |
297 | unsigned char data[128]; | |
298 | struct ishtp_msg_hdr *ishtp_hdr = &hdr; | |
299 | const size_t len = sizeof(struct hbm_flow_control); | |
300 | int rv; | |
301 | unsigned int num_frags; | |
302 | unsigned long flags; | |
303 | ||
304 | spin_lock_irqsave(&cl->fc_spinlock, flags); | |
305 | ishtp_hbm_hdr(ishtp_hdr, len); | |
306 | ishtp_hbm_cl_hdr(cl, ISHTP_FLOW_CONTROL_CMD, data, len); | |
307 | ||
308 | /* | |
309 | * Sync possible race when RB recycle and packet receive paths | |
310 | * both try to send an out FC | |
311 | */ | |
312 | if (cl->out_flow_ctrl_creds) { | |
313 | spin_unlock_irqrestore(&cl->fc_spinlock, flags); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | num_frags = cl->recv_msg_num_frags; | |
318 | cl->recv_msg_num_frags = 0; | |
319 | ||
320 | rv = ishtp_write_message(dev, ishtp_hdr, data); | |
321 | if (!rv) { | |
322 | ++cl->out_flow_ctrl_creds; | |
323 | ++cl->out_flow_ctrl_cnt; | |
324 | getnstimeofday(&cl->ts_out_fc); | |
325 | if (cl->ts_rx.tv_sec && cl->ts_rx.tv_nsec) { | |
326 | struct timespec ts_diff; | |
327 | ||
328 | ts_diff = timespec_sub(cl->ts_out_fc, cl->ts_rx); | |
329 | if (timespec_compare(&ts_diff, &cl->ts_max_fc_delay) | |
330 | > 0) | |
331 | cl->ts_max_fc_delay = ts_diff; | |
332 | } | |
333 | } else { | |
334 | ++cl->err_send_fc; | |
335 | } | |
336 | ||
337 | spin_unlock_irqrestore(&cl->fc_spinlock, flags); | |
338 | return rv; | |
339 | } | |
340 | ||
341 | /** | |
342 | * ishtp_hbm_cl_disconnect_req() - Send disconnect request | |
343 | * @dev: ISHTP device instance | |
344 | * @cl: ISHTP client instance | |
345 | * | |
346 | * Send disconnect message to fw | |
347 | * | |
348 | * Return: 0 if success else error code | |
349 | */ | |
350 | int ishtp_hbm_cl_disconnect_req(struct ishtp_device *dev, struct ishtp_cl *cl) | |
351 | { | |
352 | struct ishtp_msg_hdr hdr; | |
353 | unsigned char data[128]; | |
354 | struct ishtp_msg_hdr *ishtp_hdr = &hdr; | |
355 | const size_t len = sizeof(struct hbm_client_connect_request); | |
356 | ||
357 | ishtp_hbm_hdr(ishtp_hdr, len); | |
358 | ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, data, len); | |
359 | ||
360 | return ishtp_write_message(dev, ishtp_hdr, data); | |
361 | } | |
362 | ||
363 | /** | |
364 | * ishtp_hbm_cl_disconnect_res() - Get disconnect response | |
365 | * @dev: ISHTP device instance | |
366 | * @rs: Response message | |
367 | * | |
368 | * Received disconnect response from fw | |
369 | */ | |
370 | static void ishtp_hbm_cl_disconnect_res(struct ishtp_device *dev, | |
371 | struct hbm_client_connect_response *rs) | |
372 | { | |
373 | struct ishtp_cl *cl = NULL; | |
374 | unsigned long flags; | |
375 | ||
376 | spin_lock_irqsave(&dev->cl_list_lock, flags); | |
377 | list_for_each_entry(cl, &dev->cl_list, link) { | |
378 | if (!rs->status && ishtp_hbm_cl_addr_equal(cl, rs)) { | |
379 | cl->state = ISHTP_CL_DISCONNECTED; | |
6d290391 | 380 | wake_up_interruptible(&cl->wait_ctrl_res); |
3703f53b SP |
381 | break; |
382 | } | |
383 | } | |
3703f53b SP |
384 | spin_unlock_irqrestore(&dev->cl_list_lock, flags); |
385 | } | |
386 | ||
387 | /** | |
388 | * ishtp_hbm_cl_connect_req() - Send connect request | |
389 | * @dev: ISHTP device instance | |
390 | * @cl: client device instance | |
391 | * | |
392 | * Send connection request to specific fw client | |
393 | * | |
394 | * Return: 0 if success else error code | |
395 | */ | |
396 | int ishtp_hbm_cl_connect_req(struct ishtp_device *dev, struct ishtp_cl *cl) | |
397 | { | |
398 | struct ishtp_msg_hdr hdr; | |
399 | unsigned char data[128]; | |
400 | struct ishtp_msg_hdr *ishtp_hdr = &hdr; | |
401 | const size_t len = sizeof(struct hbm_client_connect_request); | |
402 | ||
403 | ishtp_hbm_hdr(ishtp_hdr, len); | |
404 | ishtp_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, data, len); | |
405 | ||
406 | return ishtp_write_message(dev, ishtp_hdr, data); | |
407 | } | |
408 | ||
409 | /** | |
410 | * ishtp_hbm_cl_connect_res() - Get connect response | |
411 | * @dev: ISHTP device instance | |
412 | * @rs: Response message | |
413 | * | |
414 | * Received connect response from fw | |
415 | */ | |
416 | static void ishtp_hbm_cl_connect_res(struct ishtp_device *dev, | |
417 | struct hbm_client_connect_response *rs) | |
418 | { | |
419 | struct ishtp_cl *cl = NULL; | |
420 | unsigned long flags; | |
421 | ||
422 | spin_lock_irqsave(&dev->cl_list_lock, flags); | |
423 | list_for_each_entry(cl, &dev->cl_list, link) { | |
424 | if (ishtp_hbm_cl_addr_equal(cl, rs)) { | |
425 | if (!rs->status) { | |
426 | cl->state = ISHTP_CL_CONNECTED; | |
427 | cl->status = 0; | |
428 | } else { | |
429 | cl->state = ISHTP_CL_DISCONNECTED; | |
430 | cl->status = -ENODEV; | |
431 | } | |
6d290391 | 432 | wake_up_interruptible(&cl->wait_ctrl_res); |
3703f53b SP |
433 | break; |
434 | } | |
435 | } | |
3703f53b SP |
436 | spin_unlock_irqrestore(&dev->cl_list_lock, flags); |
437 | } | |
438 | ||
439 | /** | |
440 | * ishtp_client_disconnect_request() - Receive disconnect request | |
441 | * @dev: ISHTP device instance | |
442 | * @disconnect_req: disconnect request structure | |
443 | * | |
444 | * Disconnect request bus message from the fw. Send diconnect response. | |
445 | */ | |
446 | static void ishtp_hbm_fw_disconnect_req(struct ishtp_device *dev, | |
447 | struct hbm_client_connect_request *disconnect_req) | |
448 | { | |
449 | struct ishtp_cl *cl; | |
450 | const size_t len = sizeof(struct hbm_client_connect_response); | |
451 | unsigned long flags; | |
452 | struct ishtp_msg_hdr hdr; | |
453 | unsigned char data[4]; /* All HBM messages are 4 bytes */ | |
454 | ||
455 | spin_lock_irqsave(&dev->cl_list_lock, flags); | |
456 | list_for_each_entry(cl, &dev->cl_list, link) { | |
457 | if (ishtp_hbm_cl_addr_equal(cl, disconnect_req)) { | |
458 | cl->state = ISHTP_CL_DISCONNECTED; | |
459 | ||
460 | /* send disconnect response */ | |
461 | ishtp_hbm_hdr(&hdr, len); | |
462 | ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, data, | |
463 | len); | |
464 | ishtp_write_message(dev, &hdr, data); | |
465 | break; | |
466 | } | |
467 | } | |
468 | spin_unlock_irqrestore(&dev->cl_list_lock, flags); | |
469 | } | |
470 | ||
471 | /** | |
472 | * ishtp_hbm_dma_xfer_ack(() - Receive transfer ACK | |
473 | * @dev: ISHTP device instance | |
474 | * @dma_xfer: HBM transfer message | |
475 | * | |
476 | * Receive ack for ISHTP-over-DMA client message | |
477 | */ | |
478 | static void ishtp_hbm_dma_xfer_ack(struct ishtp_device *dev, | |
479 | struct dma_xfer_hbm *dma_xfer) | |
480 | { | |
481 | void *msg; | |
482 | uint64_t offs; | |
483 | struct ishtp_msg_hdr *ishtp_hdr = | |
484 | (struct ishtp_msg_hdr *)&dev->ishtp_msg_hdr; | |
485 | unsigned int msg_offs; | |
486 | struct ishtp_cl *cl; | |
487 | ||
488 | for (msg_offs = 0; msg_offs < ishtp_hdr->length; | |
489 | msg_offs += sizeof(struct dma_xfer_hbm)) { | |
490 | offs = dma_xfer->msg_addr - dev->ishtp_host_dma_tx_buf_phys; | |
491 | if (offs > dev->ishtp_host_dma_tx_buf_size) { | |
492 | dev_err(dev->devc, "Bad DMA Tx ack message address\n"); | |
493 | return; | |
494 | } | |
495 | if (dma_xfer->msg_length > | |
496 | dev->ishtp_host_dma_tx_buf_size - offs) { | |
497 | dev_err(dev->devc, "Bad DMA Tx ack message size\n"); | |
498 | return; | |
499 | } | |
500 | ||
501 | /* logical address of the acked mem */ | |
502 | msg = (unsigned char *)dev->ishtp_host_dma_tx_buf + offs; | |
503 | ishtp_cl_release_dma_acked_mem(dev, msg, dma_xfer->msg_length); | |
504 | ||
505 | list_for_each_entry(cl, &dev->cl_list, link) { | |
506 | if (cl->fw_client_id == dma_xfer->fw_client_id && | |
507 | cl->host_client_id == dma_xfer->host_client_id) | |
508 | /* | |
509 | * in case that a single ack may be sent | |
510 | * over several dma transfers, and the last msg | |
511 | * addr was inside the acked memory, but not in | |
512 | * its start | |
513 | */ | |
514 | if (cl->last_dma_addr >= | |
515 | (unsigned char *)msg && | |
516 | cl->last_dma_addr < | |
517 | (unsigned char *)msg + | |
518 | dma_xfer->msg_length) { | |
519 | cl->last_dma_acked = 1; | |
520 | ||
521 | if (!list_empty(&cl->tx_list.list) && | |
522 | cl->ishtp_flow_ctrl_creds) { | |
523 | /* | |
524 | * start sending the first msg | |
525 | */ | |
526 | ishtp_cl_send_msg(dev, cl); | |
527 | } | |
528 | } | |
529 | } | |
530 | ++dma_xfer; | |
531 | } | |
532 | } | |
533 | ||
534 | /** | |
535 | * ishtp_hbm_dma_xfer() - Receive DMA transfer message | |
536 | * @dev: ISHTP device instance | |
537 | * @dma_xfer: HBM transfer message | |
538 | * | |
539 | * Receive ISHTP-over-DMA client message | |
540 | */ | |
541 | static void ishtp_hbm_dma_xfer(struct ishtp_device *dev, | |
542 | struct dma_xfer_hbm *dma_xfer) | |
543 | { | |
544 | void *msg; | |
545 | uint64_t offs; | |
546 | struct ishtp_msg_hdr hdr; | |
547 | struct ishtp_msg_hdr *ishtp_hdr = | |
548 | (struct ishtp_msg_hdr *) &dev->ishtp_msg_hdr; | |
549 | struct dma_xfer_hbm *prm = dma_xfer; | |
550 | unsigned int msg_offs; | |
551 | ||
552 | for (msg_offs = 0; msg_offs < ishtp_hdr->length; | |
553 | msg_offs += sizeof(struct dma_xfer_hbm)) { | |
554 | ||
555 | offs = dma_xfer->msg_addr - dev->ishtp_host_dma_rx_buf_phys; | |
556 | if (offs > dev->ishtp_host_dma_rx_buf_size) { | |
557 | dev_err(dev->devc, "Bad DMA Rx message address\n"); | |
558 | return; | |
559 | } | |
560 | if (dma_xfer->msg_length > | |
561 | dev->ishtp_host_dma_rx_buf_size - offs) { | |
562 | dev_err(dev->devc, "Bad DMA Rx message size\n"); | |
563 | return; | |
564 | } | |
565 | msg = dev->ishtp_host_dma_rx_buf + offs; | |
566 | recv_ishtp_cl_msg_dma(dev, msg, dma_xfer); | |
567 | dma_xfer->hbm = DMA_XFER_ACK; /* Prepare for response */ | |
568 | ++dma_xfer; | |
569 | } | |
570 | ||
571 | /* Send DMA_XFER_ACK [...] */ | |
572 | ishtp_hbm_hdr(&hdr, ishtp_hdr->length); | |
573 | ishtp_write_message(dev, &hdr, (unsigned char *)prm); | |
574 | } | |
575 | ||
576 | /** | |
577 | * ishtp_hbm_dispatch() - HBM dispatch function | |
578 | * @dev: ISHTP device instance | |
579 | * @hdr: bus message | |
580 | * | |
581 | * Bottom half read routine after ISR to handle the read bus message cmd | |
582 | * processing | |
583 | */ | |
584 | void ishtp_hbm_dispatch(struct ishtp_device *dev, | |
585 | struct ishtp_bus_message *hdr) | |
586 | { | |
587 | struct ishtp_bus_message *ishtp_msg; | |
588 | struct ishtp_fw_client *fw_client; | |
589 | struct hbm_host_version_response *version_res; | |
590 | struct hbm_client_connect_response *connect_res; | |
591 | struct hbm_client_connect_response *disconnect_res; | |
592 | struct hbm_client_connect_request *disconnect_req; | |
593 | struct hbm_props_response *props_res; | |
594 | struct hbm_host_enum_response *enum_res; | |
595 | struct ishtp_msg_hdr ishtp_hdr; | |
596 | struct dma_alloc_notify dma_alloc_notify; | |
597 | struct dma_xfer_hbm *dma_xfer; | |
598 | ||
599 | ishtp_msg = hdr; | |
600 | ||
601 | switch (ishtp_msg->hbm_cmd) { | |
602 | case HOST_START_RES_CMD: | |
603 | version_res = (struct hbm_host_version_response *)ishtp_msg; | |
604 | if (!version_res->host_version_supported) { | |
605 | dev->version = version_res->fw_max_version; | |
606 | ||
607 | dev->hbm_state = ISHTP_HBM_STOPPED; | |
608 | ishtp_hbm_stop_req(dev); | |
609 | return; | |
610 | } | |
611 | ||
612 | dev->version.major_version = HBM_MAJOR_VERSION; | |
613 | dev->version.minor_version = HBM_MINOR_VERSION; | |
614 | if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS && | |
615 | dev->hbm_state == ISHTP_HBM_START) { | |
616 | dev->hbm_state = ISHTP_HBM_STARTED; | |
617 | ishtp_hbm_enum_clients_req(dev); | |
618 | } else { | |
619 | dev_err(dev->devc, | |
620 | "reset: wrong host start response\n"); | |
621 | /* BUG: why do we arrive here? */ | |
622 | ish_hw_reset(dev); | |
623 | return; | |
624 | } | |
625 | ||
626 | wake_up_interruptible(&dev->wait_hbm_recvd_msg); | |
627 | break; | |
628 | ||
629 | case CLIENT_CONNECT_RES_CMD: | |
630 | connect_res = (struct hbm_client_connect_response *)ishtp_msg; | |
631 | ishtp_hbm_cl_connect_res(dev, connect_res); | |
632 | break; | |
633 | ||
634 | case CLIENT_DISCONNECT_RES_CMD: | |
635 | disconnect_res = | |
636 | (struct hbm_client_connect_response *)ishtp_msg; | |
637 | ishtp_hbm_cl_disconnect_res(dev, disconnect_res); | |
638 | break; | |
639 | ||
640 | case HOST_CLIENT_PROPERTIES_RES_CMD: | |
641 | props_res = (struct hbm_props_response *)ishtp_msg; | |
642 | fw_client = &dev->fw_clients[dev->fw_client_presentation_num]; | |
643 | ||
644 | if (props_res->status || !dev->fw_clients) { | |
645 | dev_err(dev->devc, | |
646 | "reset: properties response hbm wrong status\n"); | |
647 | ish_hw_reset(dev); | |
648 | return; | |
649 | } | |
650 | ||
651 | if (fw_client->client_id != props_res->address) { | |
652 | dev_err(dev->devc, | |
653 | "reset: host properties response address mismatch [%02X %02X]\n", | |
654 | fw_client->client_id, props_res->address); | |
655 | ish_hw_reset(dev); | |
656 | return; | |
657 | } | |
658 | ||
659 | if (dev->dev_state != ISHTP_DEV_INIT_CLIENTS || | |
660 | dev->hbm_state != ISHTP_HBM_CLIENT_PROPERTIES) { | |
661 | dev_err(dev->devc, | |
662 | "reset: unexpected properties response\n"); | |
663 | ish_hw_reset(dev); | |
664 | return; | |
665 | } | |
666 | ||
667 | fw_client->props = props_res->client_properties; | |
668 | dev->fw_client_index++; | |
669 | dev->fw_client_presentation_num++; | |
670 | ||
671 | /* request property for the next client */ | |
672 | ishtp_hbm_prop_req(dev); | |
673 | ||
674 | if (dev->dev_state != ISHTP_DEV_ENABLED) | |
675 | break; | |
676 | ||
677 | if (!ishtp_use_dma_transfer()) | |
678 | break; | |
679 | ||
680 | dev_dbg(dev->devc, "Requesting to use DMA\n"); | |
681 | ishtp_cl_alloc_dma_buf(dev); | |
682 | if (dev->ishtp_host_dma_rx_buf) { | |
683 | const size_t len = sizeof(dma_alloc_notify); | |
684 | ||
685 | memset(&dma_alloc_notify, 0, sizeof(dma_alloc_notify)); | |
686 | dma_alloc_notify.hbm = DMA_BUFFER_ALLOC_NOTIFY; | |
687 | dma_alloc_notify.buf_size = | |
688 | dev->ishtp_host_dma_rx_buf_size; | |
689 | dma_alloc_notify.buf_address = | |
690 | dev->ishtp_host_dma_rx_buf_phys; | |
691 | ishtp_hbm_hdr(&ishtp_hdr, len); | |
692 | ishtp_write_message(dev, &ishtp_hdr, | |
693 | (unsigned char *)&dma_alloc_notify); | |
694 | } | |
695 | ||
696 | break; | |
697 | ||
698 | case HOST_ENUM_RES_CMD: | |
699 | enum_res = (struct hbm_host_enum_response *) ishtp_msg; | |
700 | memcpy(dev->fw_clients_map, enum_res->valid_addresses, 32); | |
701 | if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS && | |
702 | dev->hbm_state == ISHTP_HBM_ENUM_CLIENTS) { | |
703 | dev->fw_client_presentation_num = 0; | |
704 | dev->fw_client_index = 0; | |
705 | ||
706 | ishtp_hbm_fw_cl_allocate(dev); | |
707 | dev->hbm_state = ISHTP_HBM_CLIENT_PROPERTIES; | |
708 | ||
709 | /* first property request */ | |
710 | ishtp_hbm_prop_req(dev); | |
711 | } else { | |
712 | dev_err(dev->devc, | |
713 | "reset: unexpected enumeration response hbm\n"); | |
714 | ish_hw_reset(dev); | |
715 | return; | |
716 | } | |
717 | break; | |
718 | ||
719 | case HOST_STOP_RES_CMD: | |
720 | if (dev->hbm_state != ISHTP_HBM_STOPPED) | |
721 | dev_err(dev->devc, "unexpected stop response\n"); | |
722 | ||
723 | dev->dev_state = ISHTP_DEV_DISABLED; | |
724 | dev_info(dev->devc, "reset: FW stop response\n"); | |
725 | ish_hw_reset(dev); | |
726 | break; | |
727 | ||
728 | case CLIENT_DISCONNECT_REQ_CMD: | |
729 | /* search for client */ | |
730 | disconnect_req = | |
731 | (struct hbm_client_connect_request *)ishtp_msg; | |
732 | ishtp_hbm_fw_disconnect_req(dev, disconnect_req); | |
733 | break; | |
734 | ||
735 | case FW_STOP_REQ_CMD: | |
736 | dev->hbm_state = ISHTP_HBM_STOPPED; | |
737 | break; | |
738 | ||
739 | case DMA_BUFFER_ALLOC_RESPONSE: | |
740 | dev->ishtp_host_dma_enabled = 1; | |
741 | break; | |
742 | ||
743 | case DMA_XFER: | |
744 | dma_xfer = (struct dma_xfer_hbm *)ishtp_msg; | |
745 | if (!dev->ishtp_host_dma_enabled) { | |
746 | dev_err(dev->devc, | |
747 | "DMA XFER requested but DMA is not enabled\n"); | |
748 | break; | |
749 | } | |
750 | ishtp_hbm_dma_xfer(dev, dma_xfer); | |
751 | break; | |
752 | ||
753 | case DMA_XFER_ACK: | |
754 | dma_xfer = (struct dma_xfer_hbm *)ishtp_msg; | |
755 | if (!dev->ishtp_host_dma_enabled || | |
756 | !dev->ishtp_host_dma_tx_buf) { | |
757 | dev_err(dev->devc, | |
758 | "DMA XFER acked but DMA Tx is not enabled\n"); | |
759 | break; | |
760 | } | |
761 | ishtp_hbm_dma_xfer_ack(dev, dma_xfer); | |
762 | break; | |
763 | ||
764 | default: | |
765 | dev_err(dev->devc, "unknown HBM: %u\n", | |
766 | (unsigned int)ishtp_msg->hbm_cmd); | |
767 | ||
768 | break; | |
769 | } | |
770 | } | |
771 | ||
772 | /** | |
773 | * bh_hbm_work_fn() - HBM work function | |
774 | * @work: work struct | |
775 | * | |
776 | * Bottom half processing work function (instead of thread handler) | |
777 | * for processing hbm messages | |
778 | */ | |
779 | void bh_hbm_work_fn(struct work_struct *work) | |
780 | { | |
781 | unsigned long flags; | |
782 | struct ishtp_device *dev; | |
783 | unsigned char hbm[IPC_PAYLOAD_SIZE]; | |
784 | ||
785 | dev = container_of(work, struct ishtp_device, bh_hbm_work); | |
786 | spin_lock_irqsave(&dev->rd_msg_spinlock, flags); | |
787 | if (dev->rd_msg_fifo_head != dev->rd_msg_fifo_tail) { | |
788 | memcpy(hbm, dev->rd_msg_fifo + dev->rd_msg_fifo_head, | |
789 | IPC_PAYLOAD_SIZE); | |
790 | dev->rd_msg_fifo_head = | |
791 | (dev->rd_msg_fifo_head + IPC_PAYLOAD_SIZE) % | |
792 | (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE); | |
793 | spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags); | |
794 | ishtp_hbm_dispatch(dev, (struct ishtp_bus_message *)hbm); | |
795 | } else { | |
796 | spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags); | |
797 | } | |
798 | } | |
799 | ||
800 | /** | |
801 | * recv_hbm() - Receive HBM message | |
802 | * @dev: ISHTP device instance | |
803 | * @ishtp_hdr: received bus message | |
804 | * | |
805 | * Receive and process ISHTP bus messages in ISR context. This will schedule | |
806 | * work function to process message | |
807 | */ | |
808 | void recv_hbm(struct ishtp_device *dev, struct ishtp_msg_hdr *ishtp_hdr) | |
809 | { | |
810 | uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE]; | |
811 | struct ishtp_bus_message *ishtp_msg = | |
812 | (struct ishtp_bus_message *)rd_msg_buf; | |
813 | unsigned long flags; | |
814 | ||
815 | dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length); | |
816 | ||
817 | /* Flow control - handle in place */ | |
818 | if (ishtp_msg->hbm_cmd == ISHTP_FLOW_CONTROL_CMD) { | |
819 | struct hbm_flow_control *flow_control = | |
820 | (struct hbm_flow_control *)ishtp_msg; | |
821 | struct ishtp_cl *cl = NULL; | |
822 | unsigned long flags, tx_flags; | |
823 | ||
824 | spin_lock_irqsave(&dev->cl_list_lock, flags); | |
825 | list_for_each_entry(cl, &dev->cl_list, link) { | |
826 | if (cl->host_client_id == flow_control->host_addr && | |
827 | cl->fw_client_id == | |
828 | flow_control->fw_addr) { | |
829 | /* | |
830 | * NOTE: It's valid only for counting | |
831 | * flow-control implementation to receive a | |
832 | * FC in the middle of sending. Meanwhile not | |
833 | * supported | |
834 | */ | |
835 | if (cl->ishtp_flow_ctrl_creds) | |
836 | dev_err(dev->devc, | |
837 | "recv extra FC from FW client %u (host client %u) (FC count was %d)\n", | |
838 | (unsigned int)cl->fw_client_id, | |
839 | (unsigned int)cl->host_client_id, | |
840 | cl->ishtp_flow_ctrl_creds); | |
841 | else { | |
842 | ++cl->ishtp_flow_ctrl_creds; | |
843 | ++cl->ishtp_flow_ctrl_cnt; | |
844 | cl->last_ipc_acked = 1; | |
845 | spin_lock_irqsave( | |
846 | &cl->tx_list_spinlock, | |
847 | tx_flags); | |
848 | if (!list_empty(&cl->tx_list.list)) { | |
849 | /* | |
850 | * start sending the first msg | |
851 | * = the callback function | |
852 | */ | |
853 | spin_unlock_irqrestore( | |
854 | &cl->tx_list_spinlock, | |
855 | tx_flags); | |
856 | ishtp_cl_send_msg(dev, cl); | |
857 | } else { | |
858 | spin_unlock_irqrestore( | |
859 | &cl->tx_list_spinlock, | |
860 | tx_flags); | |
861 | } | |
862 | } | |
863 | break; | |
864 | } | |
865 | } | |
866 | spin_unlock_irqrestore(&dev->cl_list_lock, flags); | |
867 | goto eoi; | |
868 | } | |
869 | ||
870 | /* | |
871 | * Some messages that are safe for ISR processing and important | |
872 | * to be done "quickly" and in-order, go here | |
873 | */ | |
874 | if (ishtp_msg->hbm_cmd == CLIENT_CONNECT_RES_CMD || | |
875 | ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_RES_CMD || | |
876 | ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_REQ_CMD || | |
877 | ishtp_msg->hbm_cmd == DMA_XFER) { | |
878 | ishtp_hbm_dispatch(dev, ishtp_msg); | |
879 | goto eoi; | |
880 | } | |
881 | ||
882 | /* | |
883 | * All other HBMs go here. | |
884 | * We schedule HBMs for processing serially by using system wq, | |
885 | * possibly there will be multiple HBMs scheduled at the same time. | |
886 | */ | |
887 | spin_lock_irqsave(&dev->rd_msg_spinlock, flags); | |
888 | if ((dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) % | |
889 | (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE) == | |
890 | dev->rd_msg_fifo_head) { | |
891 | spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags); | |
892 | dev_err(dev->devc, "BH buffer overflow, dropping HBM %u\n", | |
893 | (unsigned int)ishtp_msg->hbm_cmd); | |
894 | goto eoi; | |
895 | } | |
896 | memcpy(dev->rd_msg_fifo + dev->rd_msg_fifo_tail, ishtp_msg, | |
897 | ishtp_hdr->length); | |
898 | dev->rd_msg_fifo_tail = (dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) % | |
899 | (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE); | |
900 | spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags); | |
901 | schedule_work(&dev->bh_hbm_work); | |
902 | eoi: | |
903 | return; | |
904 | } | |
905 | ||
906 | /** | |
907 | * recv_fixed_cl_msg() - Receive fixed client message | |
908 | * @dev: ISHTP device instance | |
909 | * @ishtp_hdr: received bus message | |
910 | * | |
911 | * Receive and process ISHTP fixed client messages (address == 0) | |
912 | * in ISR context | |
913 | */ | |
914 | void recv_fixed_cl_msg(struct ishtp_device *dev, | |
915 | struct ishtp_msg_hdr *ishtp_hdr) | |
916 | { | |
917 | uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE]; | |
918 | ||
919 | dev->print_log(dev, | |
920 | "%s() got fixed client msg from client #%d\n", | |
921 | __func__, ishtp_hdr->fw_addr); | |
922 | dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length); | |
923 | if (ishtp_hdr->fw_addr == ISHTP_SYSTEM_STATE_CLIENT_ADDR) { | |
924 | struct ish_system_states_header *msg_hdr = | |
925 | (struct ish_system_states_header *)rd_msg_buf; | |
926 | if (msg_hdr->cmd == SYSTEM_STATE_SUBSCRIBE) | |
927 | ishtp_send_resume(dev); | |
928 | /* if FW request arrived here, the system is not suspended */ | |
929 | else | |
930 | dev_err(dev->devc, "unknown fixed client msg [%02X]\n", | |
931 | msg_hdr->cmd); | |
932 | } | |
933 | } | |
934 | ||
935 | /** | |
936 | * fix_cl_hdr() - Initialize fixed client header | |
937 | * @hdr: message header | |
938 | * @length: length of message | |
939 | * @cl_addr: Client address | |
940 | * | |
941 | * Initialize message header for fixed client | |
942 | */ | |
943 | static inline void fix_cl_hdr(struct ishtp_msg_hdr *hdr, size_t length, | |
944 | uint8_t cl_addr) | |
945 | { | |
946 | hdr->host_addr = 0; | |
947 | hdr->fw_addr = cl_addr; | |
948 | hdr->length = length; | |
949 | hdr->msg_complete = 1; | |
950 | hdr->reserved = 0; | |
951 | } | |
952 | ||
953 | /*** Suspend and resume notification ***/ | |
954 | ||
955 | static uint32_t current_state; | |
956 | static uint32_t supported_states = 0 | SUSPEND_STATE_BIT; | |
957 | ||
958 | /** | |
959 | * ishtp_send_suspend() - Send suspend message to FW | |
960 | * @dev: ISHTP device instance | |
961 | * | |
962 | * Send suspend message to FW. This is useful for system freeze (non S3) case | |
963 | */ | |
964 | void ishtp_send_suspend(struct ishtp_device *dev) | |
965 | { | |
966 | struct ishtp_msg_hdr ishtp_hdr; | |
967 | struct ish_system_states_status state_status_msg; | |
968 | const size_t len = sizeof(struct ish_system_states_status); | |
969 | ||
970 | fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR); | |
971 | ||
972 | memset(&state_status_msg, 0, len); | |
973 | state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS; | |
974 | state_status_msg.supported_states = supported_states; | |
975 | current_state |= SUSPEND_STATE_BIT; | |
976 | dev->print_log(dev, "%s() sends SUSPEND notification\n", __func__); | |
977 | state_status_msg.states_status = current_state; | |
978 | ||
979 | ishtp_write_message(dev, &ishtp_hdr, | |
980 | (unsigned char *)&state_status_msg); | |
981 | } | |
982 | EXPORT_SYMBOL(ishtp_send_suspend); | |
983 | ||
984 | /** | |
985 | * ishtp_send_resume() - Send resume message to FW | |
986 | * @dev: ISHTP device instance | |
987 | * | |
988 | * Send resume message to FW. This is useful for system freeze (non S3) case | |
989 | */ | |
990 | void ishtp_send_resume(struct ishtp_device *dev) | |
991 | { | |
992 | struct ishtp_msg_hdr ishtp_hdr; | |
993 | struct ish_system_states_status state_status_msg; | |
994 | const size_t len = sizeof(struct ish_system_states_status); | |
995 | ||
996 | fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR); | |
997 | ||
998 | memset(&state_status_msg, 0, len); | |
999 | state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS; | |
1000 | state_status_msg.supported_states = supported_states; | |
1001 | current_state &= ~SUSPEND_STATE_BIT; | |
1002 | dev->print_log(dev, "%s() sends RESUME notification\n", __func__); | |
1003 | state_status_msg.states_status = current_state; | |
1004 | ||
1005 | ishtp_write_message(dev, &ishtp_hdr, | |
1006 | (unsigned char *)&state_status_msg); | |
1007 | } | |
1008 | EXPORT_SYMBOL(ishtp_send_resume); | |
1009 | ||
1010 | /** | |
1011 | * ishtp_query_subscribers() - Send query subscribers message | |
1012 | * @dev: ISHTP device instance | |
1013 | * | |
1014 | * Send message to query subscribers | |
1015 | */ | |
1016 | void ishtp_query_subscribers(struct ishtp_device *dev) | |
1017 | { | |
1018 | struct ishtp_msg_hdr ishtp_hdr; | |
1019 | struct ish_system_states_query_subscribers query_subscribers_msg; | |
1020 | const size_t len = sizeof(struct ish_system_states_query_subscribers); | |
1021 | ||
1022 | fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR); | |
1023 | ||
1024 | memset(&query_subscribers_msg, 0, len); | |
1025 | query_subscribers_msg.hdr.cmd = SYSTEM_STATE_QUERY_SUBSCRIBERS; | |
1026 | ||
1027 | ishtp_write_message(dev, &ishtp_hdr, | |
1028 | (unsigned char *)&query_subscribers_msg); | |
1029 | } |