Merge branch 'for-4.19/sony' into for-linus
[sfrench/cifs-2.6.git] / drivers / hid / intel-ish-hid / ishtp / hbm.c
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>
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                 cl->ts_out_fc = ktime_get();
325                 if (cl->ts_rx) {
326                         ktime_t ts_diff = ktime_sub(cl->ts_out_fc, cl->ts_rx);
327                         if (ktime_after(ts_diff, cl->ts_max_fc_delay))
328                                 cl->ts_max_fc_delay = ts_diff;
329                 }
330         } else {
331                 ++cl->err_send_fc;
332         }
333
334         spin_unlock_irqrestore(&cl->fc_spinlock, flags);
335         return  rv;
336 }
337
338 /**
339  * ishtp_hbm_cl_disconnect_req() - Send disconnect request
340  * @dev: ISHTP device instance
341  * @cl: ISHTP client instance
342  *
343  * Send disconnect message to fw
344  *
345  * Return: 0 if success else error code
346  */
347 int ishtp_hbm_cl_disconnect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
348 {
349         struct ishtp_msg_hdr hdr;
350         unsigned char data[128];
351         struct ishtp_msg_hdr *ishtp_hdr = &hdr;
352         const size_t len = sizeof(struct hbm_client_connect_request);
353
354         ishtp_hbm_hdr(ishtp_hdr, len);
355         ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, data, len);
356
357         return ishtp_write_message(dev, ishtp_hdr, data);
358 }
359
360 /**
361  * ishtp_hbm_cl_disconnect_res() - Get disconnect response
362  * @dev: ISHTP device instance
363  * @rs: Response message
364  *
365  * Received disconnect response from fw
366  */
367 static void ishtp_hbm_cl_disconnect_res(struct ishtp_device *dev,
368         struct hbm_client_connect_response *rs)
369 {
370         struct ishtp_cl *cl = NULL;
371         unsigned long   flags;
372
373         spin_lock_irqsave(&dev->cl_list_lock, flags);
374         list_for_each_entry(cl, &dev->cl_list, link) {
375                 if (!rs->status && ishtp_hbm_cl_addr_equal(cl, rs)) {
376                         cl->state = ISHTP_CL_DISCONNECTED;
377                         wake_up_interruptible(&cl->wait_ctrl_res);
378                         break;
379                 }
380         }
381         spin_unlock_irqrestore(&dev->cl_list_lock, flags);
382 }
383
384 /**
385  * ishtp_hbm_cl_connect_req() - Send connect request
386  * @dev: ISHTP device instance
387  * @cl: client device instance
388  *
389  * Send connection request to specific fw client
390  *
391  * Return: 0 if success else error code
392  */
393 int ishtp_hbm_cl_connect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
394 {
395         struct ishtp_msg_hdr hdr;
396         unsigned char data[128];
397         struct ishtp_msg_hdr *ishtp_hdr = &hdr;
398         const size_t len = sizeof(struct hbm_client_connect_request);
399
400         ishtp_hbm_hdr(ishtp_hdr, len);
401         ishtp_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, data, len);
402
403         return ishtp_write_message(dev, ishtp_hdr, data);
404 }
405
406 /**
407  * ishtp_hbm_cl_connect_res() - Get connect response
408  * @dev: ISHTP device instance
409  * @rs: Response message
410  *
411  * Received connect response from fw
412  */
413 static void ishtp_hbm_cl_connect_res(struct ishtp_device *dev,
414         struct hbm_client_connect_response *rs)
415 {
416         struct ishtp_cl *cl = NULL;
417         unsigned long   flags;
418
419         spin_lock_irqsave(&dev->cl_list_lock, flags);
420         list_for_each_entry(cl, &dev->cl_list, link) {
421                 if (ishtp_hbm_cl_addr_equal(cl, rs)) {
422                         if (!rs->status) {
423                                 cl->state = ISHTP_CL_CONNECTED;
424                                 cl->status = 0;
425                         } else {
426                                 cl->state = ISHTP_CL_DISCONNECTED;
427                                 cl->status = -ENODEV;
428                         }
429                         wake_up_interruptible(&cl->wait_ctrl_res);
430                         break;
431                 }
432         }
433         spin_unlock_irqrestore(&dev->cl_list_lock, flags);
434 }
435
436 /**
437  * ishtp_client_disconnect_request() - Receive disconnect request
438  * @dev: ISHTP device instance
439  * @disconnect_req: disconnect request structure
440  *
441  * Disconnect request bus message from the fw. Send diconnect response.
442  */
443 static void ishtp_hbm_fw_disconnect_req(struct ishtp_device *dev,
444         struct hbm_client_connect_request *disconnect_req)
445 {
446         struct ishtp_cl *cl;
447         const size_t len = sizeof(struct hbm_client_connect_response);
448         unsigned long   flags;
449         struct ishtp_msg_hdr hdr;
450         unsigned char data[4];  /* All HBM messages are 4 bytes */
451
452         spin_lock_irqsave(&dev->cl_list_lock, flags);
453         list_for_each_entry(cl, &dev->cl_list, link) {
454                 if (ishtp_hbm_cl_addr_equal(cl, disconnect_req)) {
455                         cl->state = ISHTP_CL_DISCONNECTED;
456
457                         /* send disconnect response */
458                         ishtp_hbm_hdr(&hdr, len);
459                         ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, data,
460                                 len);
461                         ishtp_write_message(dev, &hdr, data);
462                         break;
463                 }
464         }
465         spin_unlock_irqrestore(&dev->cl_list_lock, flags);
466 }
467
468 /**
469  * ishtp_hbm_dma_xfer_ack(() - Receive transfer ACK
470  * @dev: ISHTP device instance
471  * @dma_xfer: HBM transfer message
472  *
473  * Receive ack for ISHTP-over-DMA client message
474  */
475 static void ishtp_hbm_dma_xfer_ack(struct ishtp_device *dev,
476                                    struct dma_xfer_hbm *dma_xfer)
477 {
478         void    *msg;
479         uint64_t        offs;
480         struct ishtp_msg_hdr    *ishtp_hdr =
481                 (struct ishtp_msg_hdr *)&dev->ishtp_msg_hdr;
482         unsigned int    msg_offs;
483         struct ishtp_cl *cl;
484
485         for (msg_offs = 0; msg_offs < ishtp_hdr->length;
486                 msg_offs += sizeof(struct dma_xfer_hbm)) {
487                 offs = dma_xfer->msg_addr - dev->ishtp_host_dma_tx_buf_phys;
488                 if (offs > dev->ishtp_host_dma_tx_buf_size) {
489                         dev_err(dev->devc, "Bad DMA Tx ack message address\n");
490                         return;
491                 }
492                 if (dma_xfer->msg_length >
493                                 dev->ishtp_host_dma_tx_buf_size - offs) {
494                         dev_err(dev->devc, "Bad DMA Tx ack message size\n");
495                         return;
496                 }
497
498                 /* logical address of the acked mem */
499                 msg = (unsigned char *)dev->ishtp_host_dma_tx_buf + offs;
500                 ishtp_cl_release_dma_acked_mem(dev, msg, dma_xfer->msg_length);
501
502                 list_for_each_entry(cl, &dev->cl_list, link) {
503                         if (cl->fw_client_id == dma_xfer->fw_client_id &&
504                             cl->host_client_id == dma_xfer->host_client_id)
505                                 /*
506                                  * in case that a single ack may be sent
507                                  * over several dma transfers, and the last msg
508                                  * addr was inside the acked memory, but not in
509                                  * its start
510                                  */
511                                 if (cl->last_dma_addr >=
512                                                         (unsigned char *)msg &&
513                                                 cl->last_dma_addr <
514                                                 (unsigned char *)msg +
515                                                 dma_xfer->msg_length) {
516                                         cl->last_dma_acked = 1;
517
518                                         if (!list_empty(&cl->tx_list.list) &&
519                                                 cl->ishtp_flow_ctrl_creds) {
520                                                 /*
521                                                  * start sending the first msg
522                                                  */
523                                                 ishtp_cl_send_msg(dev, cl);
524                                         }
525                                 }
526                 }
527                 ++dma_xfer;
528         }
529 }
530
531 /**
532  * ishtp_hbm_dma_xfer() - Receive DMA transfer message
533  * @dev: ISHTP device instance
534  * @dma_xfer: HBM transfer message
535  *
536  * Receive ISHTP-over-DMA client message
537  */
538 static void ishtp_hbm_dma_xfer(struct ishtp_device *dev,
539                                struct dma_xfer_hbm *dma_xfer)
540 {
541         void    *msg;
542         uint64_t        offs;
543         struct ishtp_msg_hdr    hdr;
544         struct ishtp_msg_hdr    *ishtp_hdr =
545                 (struct ishtp_msg_hdr *) &dev->ishtp_msg_hdr;
546         struct dma_xfer_hbm     *prm = dma_xfer;
547         unsigned int    msg_offs;
548
549         for (msg_offs = 0; msg_offs < ishtp_hdr->length;
550                 msg_offs += sizeof(struct dma_xfer_hbm)) {
551
552                 offs = dma_xfer->msg_addr - dev->ishtp_host_dma_rx_buf_phys;
553                 if (offs > dev->ishtp_host_dma_rx_buf_size) {
554                         dev_err(dev->devc, "Bad DMA Rx message address\n");
555                         return;
556                 }
557                 if (dma_xfer->msg_length >
558                                 dev->ishtp_host_dma_rx_buf_size - offs) {
559                         dev_err(dev->devc, "Bad DMA Rx message size\n");
560                         return;
561                 }
562                 msg = dev->ishtp_host_dma_rx_buf + offs;
563                 recv_ishtp_cl_msg_dma(dev, msg, dma_xfer);
564                 dma_xfer->hbm = DMA_XFER_ACK;   /* Prepare for response */
565                 ++dma_xfer;
566         }
567
568         /* Send DMA_XFER_ACK [...] */
569         ishtp_hbm_hdr(&hdr, ishtp_hdr->length);
570         ishtp_write_message(dev, &hdr, (unsigned char *)prm);
571 }
572
573 /**
574  * ishtp_hbm_dispatch() - HBM dispatch function
575  * @dev: ISHTP device instance
576  * @hdr: bus message
577  *
578  * Bottom half read routine after ISR to handle the read bus message cmd
579  * processing
580  */
581 void ishtp_hbm_dispatch(struct ishtp_device *dev,
582                         struct ishtp_bus_message *hdr)
583 {
584         struct ishtp_bus_message *ishtp_msg;
585         struct ishtp_fw_client *fw_client;
586         struct hbm_host_version_response *version_res;
587         struct hbm_client_connect_response *connect_res;
588         struct hbm_client_connect_response *disconnect_res;
589         struct hbm_client_connect_request *disconnect_req;
590         struct hbm_props_response *props_res;
591         struct hbm_host_enum_response *enum_res;
592         struct ishtp_msg_hdr ishtp_hdr;
593         struct dma_alloc_notify dma_alloc_notify;
594         struct dma_xfer_hbm     *dma_xfer;
595
596         ishtp_msg = hdr;
597
598         switch (ishtp_msg->hbm_cmd) {
599         case HOST_START_RES_CMD:
600                 version_res = (struct hbm_host_version_response *)ishtp_msg;
601                 if (!version_res->host_version_supported) {
602                         dev->version = version_res->fw_max_version;
603
604                         dev->hbm_state = ISHTP_HBM_STOPPED;
605                         ishtp_hbm_stop_req(dev);
606                         return;
607                 }
608
609                 dev->version.major_version = HBM_MAJOR_VERSION;
610                 dev->version.minor_version = HBM_MINOR_VERSION;
611                 if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
612                                 dev->hbm_state == ISHTP_HBM_START) {
613                         dev->hbm_state = ISHTP_HBM_STARTED;
614                         ishtp_hbm_enum_clients_req(dev);
615                 } else {
616                         dev_err(dev->devc,
617                                 "reset: wrong host start response\n");
618                         /* BUG: why do we arrive here? */
619                         ish_hw_reset(dev);
620                         return;
621                 }
622
623                 wake_up_interruptible(&dev->wait_hbm_recvd_msg);
624                 break;
625
626         case CLIENT_CONNECT_RES_CMD:
627                 connect_res = (struct hbm_client_connect_response *)ishtp_msg;
628                 ishtp_hbm_cl_connect_res(dev, connect_res);
629                 break;
630
631         case CLIENT_DISCONNECT_RES_CMD:
632                 disconnect_res =
633                         (struct hbm_client_connect_response *)ishtp_msg;
634                 ishtp_hbm_cl_disconnect_res(dev, disconnect_res);
635                 break;
636
637         case HOST_CLIENT_PROPERTIES_RES_CMD:
638                 props_res = (struct hbm_props_response *)ishtp_msg;
639                 fw_client = &dev->fw_clients[dev->fw_client_presentation_num];
640
641                 if (props_res->status || !dev->fw_clients) {
642                         dev_err(dev->devc,
643                         "reset: properties response hbm wrong status\n");
644                         ish_hw_reset(dev);
645                         return;
646                 }
647
648                 if (fw_client->client_id != props_res->address) {
649                         dev_err(dev->devc,
650                                 "reset: host properties response address mismatch [%02X %02X]\n",
651                                 fw_client->client_id, props_res->address);
652                         ish_hw_reset(dev);
653                         return;
654                 }
655
656                 if (dev->dev_state != ISHTP_DEV_INIT_CLIENTS ||
657                         dev->hbm_state != ISHTP_HBM_CLIENT_PROPERTIES) {
658                         dev_err(dev->devc,
659                                 "reset: unexpected properties response\n");
660                         ish_hw_reset(dev);
661                         return;
662                 }
663
664                 fw_client->props = props_res->client_properties;
665                 dev->fw_client_index++;
666                 dev->fw_client_presentation_num++;
667
668                 /* request property for the next client */
669                 ishtp_hbm_prop_req(dev);
670
671                 if (dev->dev_state != ISHTP_DEV_ENABLED)
672                         break;
673
674                 if (!ishtp_use_dma_transfer())
675                         break;
676
677                 dev_dbg(dev->devc, "Requesting to use DMA\n");
678                 ishtp_cl_alloc_dma_buf(dev);
679                 if (dev->ishtp_host_dma_rx_buf) {
680                         const size_t len = sizeof(dma_alloc_notify);
681
682                         memset(&dma_alloc_notify, 0, sizeof(dma_alloc_notify));
683                         dma_alloc_notify.hbm = DMA_BUFFER_ALLOC_NOTIFY;
684                         dma_alloc_notify.buf_size =
685                                         dev->ishtp_host_dma_rx_buf_size;
686                         dma_alloc_notify.buf_address =
687                                         dev->ishtp_host_dma_rx_buf_phys;
688                         ishtp_hbm_hdr(&ishtp_hdr, len);
689                         ishtp_write_message(dev, &ishtp_hdr,
690                                 (unsigned char *)&dma_alloc_notify);
691                 }
692
693                 break;
694
695         case HOST_ENUM_RES_CMD:
696                 enum_res = (struct hbm_host_enum_response *) ishtp_msg;
697                 memcpy(dev->fw_clients_map, enum_res->valid_addresses, 32);
698                 if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
699                         dev->hbm_state == ISHTP_HBM_ENUM_CLIENTS) {
700                         dev->fw_client_presentation_num = 0;
701                         dev->fw_client_index = 0;
702
703                         ishtp_hbm_fw_cl_allocate(dev);
704                         dev->hbm_state = ISHTP_HBM_CLIENT_PROPERTIES;
705
706                         /* first property request */
707                         ishtp_hbm_prop_req(dev);
708                 } else {
709                         dev_err(dev->devc,
710                               "reset: unexpected enumeration response hbm\n");
711                         ish_hw_reset(dev);
712                         return;
713                 }
714                 break;
715
716         case HOST_STOP_RES_CMD:
717                 if (dev->hbm_state != ISHTP_HBM_STOPPED)
718                         dev_err(dev->devc, "unexpected stop response\n");
719
720                 dev->dev_state = ISHTP_DEV_DISABLED;
721                 dev_info(dev->devc, "reset: FW stop response\n");
722                 ish_hw_reset(dev);
723                 break;
724
725         case CLIENT_DISCONNECT_REQ_CMD:
726                 /* search for client */
727                 disconnect_req =
728                         (struct hbm_client_connect_request *)ishtp_msg;
729                 ishtp_hbm_fw_disconnect_req(dev, disconnect_req);
730                 break;
731
732         case FW_STOP_REQ_CMD:
733                 dev->hbm_state = ISHTP_HBM_STOPPED;
734                 break;
735
736         case DMA_BUFFER_ALLOC_RESPONSE:
737                 dev->ishtp_host_dma_enabled = 1;
738                 break;
739
740         case DMA_XFER:
741                 dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
742                 if (!dev->ishtp_host_dma_enabled) {
743                         dev_err(dev->devc,
744                                 "DMA XFER requested but DMA is not enabled\n");
745                         break;
746                 }
747                 ishtp_hbm_dma_xfer(dev, dma_xfer);
748                 break;
749
750         case DMA_XFER_ACK:
751                 dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
752                 if (!dev->ishtp_host_dma_enabled ||
753                     !dev->ishtp_host_dma_tx_buf) {
754                         dev_err(dev->devc,
755                                 "DMA XFER acked but DMA Tx is not enabled\n");
756                         break;
757                 }
758                 ishtp_hbm_dma_xfer_ack(dev, dma_xfer);
759                 break;
760
761         default:
762                 dev_err(dev->devc, "unknown HBM: %u\n",
763                         (unsigned int)ishtp_msg->hbm_cmd);
764
765                 break;
766         }
767 }
768
769 /**
770  * bh_hbm_work_fn() - HBM work function
771  * @work: work struct
772  *
773  * Bottom half processing work function (instead of thread handler)
774  * for processing hbm messages
775  */
776 void    bh_hbm_work_fn(struct work_struct *work)
777 {
778         unsigned long   flags;
779         struct ishtp_device     *dev;
780         unsigned char   hbm[IPC_PAYLOAD_SIZE];
781
782         dev = container_of(work, struct ishtp_device, bh_hbm_work);
783         spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
784         if (dev->rd_msg_fifo_head != dev->rd_msg_fifo_tail) {
785                 memcpy(hbm, dev->rd_msg_fifo + dev->rd_msg_fifo_head,
786                         IPC_PAYLOAD_SIZE);
787                 dev->rd_msg_fifo_head =
788                         (dev->rd_msg_fifo_head + IPC_PAYLOAD_SIZE) %
789                         (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
790                 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
791                 ishtp_hbm_dispatch(dev, (struct ishtp_bus_message *)hbm);
792         } else {
793                 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
794         }
795 }
796
797 /**
798  * recv_hbm() - Receive HBM message
799  * @dev: ISHTP device instance
800  * @ishtp_hdr: received bus message
801  *
802  * Receive and process ISHTP bus messages in ISR context. This will schedule
803  * work function to process message
804  */
805 void    recv_hbm(struct ishtp_device *dev, struct ishtp_msg_hdr *ishtp_hdr)
806 {
807         uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
808         struct ishtp_bus_message        *ishtp_msg =
809                 (struct ishtp_bus_message *)rd_msg_buf;
810         unsigned long   flags;
811
812         dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
813
814         /* Flow control - handle in place */
815         if (ishtp_msg->hbm_cmd == ISHTP_FLOW_CONTROL_CMD) {
816                 struct hbm_flow_control *flow_control =
817                         (struct hbm_flow_control *)ishtp_msg;
818                 struct ishtp_cl *cl = NULL;
819                 unsigned long   flags, tx_flags;
820
821                 spin_lock_irqsave(&dev->cl_list_lock, flags);
822                 list_for_each_entry(cl, &dev->cl_list, link) {
823                         if (cl->host_client_id == flow_control->host_addr &&
824                                         cl->fw_client_id ==
825                                         flow_control->fw_addr) {
826                                 /*
827                                  * NOTE: It's valid only for counting
828                                  * flow-control implementation to receive a
829                                  * FC in the middle of sending. Meanwhile not
830                                  * supported
831                                  */
832                                 if (cl->ishtp_flow_ctrl_creds)
833                                         dev_err(dev->devc,
834                                          "recv extra FC from FW client %u (host client %u) (FC count was %d)\n",
835                                          (unsigned int)cl->fw_client_id,
836                                          (unsigned int)cl->host_client_id,
837                                          cl->ishtp_flow_ctrl_creds);
838                                 else {
839                                         ++cl->ishtp_flow_ctrl_creds;
840                                         ++cl->ishtp_flow_ctrl_cnt;
841                                         cl->last_ipc_acked = 1;
842                                         spin_lock_irqsave(
843                                                         &cl->tx_list_spinlock,
844                                                         tx_flags);
845                                         if (!list_empty(&cl->tx_list.list)) {
846                                                 /*
847                                                  * start sending the first msg
848                                                  *      = the callback function
849                                                  */
850                                                 spin_unlock_irqrestore(
851                                                         &cl->tx_list_spinlock,
852                                                         tx_flags);
853                                                 ishtp_cl_send_msg(dev, cl);
854                                         } else {
855                                                 spin_unlock_irqrestore(
856                                                         &cl->tx_list_spinlock,
857                                                         tx_flags);
858                                         }
859                                 }
860                                 break;
861                         }
862                 }
863                 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
864                 goto    eoi;
865         }
866
867         /*
868          * Some messages that are safe for ISR processing and important
869          * to be done "quickly" and in-order, go here
870          */
871         if (ishtp_msg->hbm_cmd == CLIENT_CONNECT_RES_CMD ||
872                         ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_RES_CMD ||
873                         ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_REQ_CMD ||
874                         ishtp_msg->hbm_cmd == DMA_XFER) {
875                 ishtp_hbm_dispatch(dev, ishtp_msg);
876                 goto    eoi;
877         }
878
879         /*
880          * All other HBMs go here.
881          * We schedule HBMs for processing serially by using system wq,
882          * possibly there will be multiple HBMs scheduled at the same time.
883          */
884         spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
885         if ((dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
886                         (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE) ==
887                         dev->rd_msg_fifo_head) {
888                 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
889                 dev_err(dev->devc, "BH buffer overflow, dropping HBM %u\n",
890                         (unsigned int)ishtp_msg->hbm_cmd);
891                 goto    eoi;
892         }
893         memcpy(dev->rd_msg_fifo + dev->rd_msg_fifo_tail, ishtp_msg,
894                 ishtp_hdr->length);
895         dev->rd_msg_fifo_tail = (dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
896                 (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
897         spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
898         schedule_work(&dev->bh_hbm_work);
899 eoi:
900         return;
901 }
902
903 /**
904  * recv_fixed_cl_msg() - Receive fixed client message
905  * @dev: ISHTP device instance
906  * @ishtp_hdr: received bus message
907  *
908  * Receive and process ISHTP fixed client messages (address == 0)
909  * in ISR context
910  */
911 void recv_fixed_cl_msg(struct ishtp_device *dev,
912         struct ishtp_msg_hdr *ishtp_hdr)
913 {
914         uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
915
916         dev->print_log(dev,
917                 "%s() got fixed client msg from client #%d\n",
918                 __func__, ishtp_hdr->fw_addr);
919         dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
920         if (ishtp_hdr->fw_addr == ISHTP_SYSTEM_STATE_CLIENT_ADDR) {
921                 struct ish_system_states_header *msg_hdr =
922                         (struct ish_system_states_header *)rd_msg_buf;
923                 if (msg_hdr->cmd == SYSTEM_STATE_SUBSCRIBE)
924                         ishtp_send_resume(dev);
925                 /* if FW request arrived here, the system is not suspended */
926                 else
927                         dev_err(dev->devc, "unknown fixed client msg [%02X]\n",
928                                 msg_hdr->cmd);
929         }
930 }
931
932 /**
933  * fix_cl_hdr() - Initialize fixed client header
934  * @hdr: message header
935  * @length: length of message
936  * @cl_addr: Client address
937  *
938  * Initialize message header for fixed client
939  */
940 static inline void fix_cl_hdr(struct ishtp_msg_hdr *hdr, size_t length,
941         uint8_t cl_addr)
942 {
943         hdr->host_addr = 0;
944         hdr->fw_addr = cl_addr;
945         hdr->length = length;
946         hdr->msg_complete = 1;
947         hdr->reserved = 0;
948 }
949
950 /*** Suspend and resume notification ***/
951
952 static uint32_t current_state;
953 static uint32_t supported_states = 0 | SUSPEND_STATE_BIT;
954
955 /**
956  * ishtp_send_suspend() - Send suspend message to FW
957  * @dev: ISHTP device instance
958  *
959  * Send suspend message to FW. This is useful for system freeze (non S3) case
960  */
961 void ishtp_send_suspend(struct ishtp_device *dev)
962 {
963         struct ishtp_msg_hdr    ishtp_hdr;
964         struct ish_system_states_status state_status_msg;
965         const size_t len = sizeof(struct ish_system_states_status);
966
967         fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
968
969         memset(&state_status_msg, 0, len);
970         state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
971         state_status_msg.supported_states = supported_states;
972         current_state |= SUSPEND_STATE_BIT;
973         dev->print_log(dev, "%s() sends SUSPEND notification\n", __func__);
974         state_status_msg.states_status = current_state;
975
976         ishtp_write_message(dev, &ishtp_hdr,
977                 (unsigned char *)&state_status_msg);
978 }
979 EXPORT_SYMBOL(ishtp_send_suspend);
980
981 /**
982  * ishtp_send_resume() - Send resume message to FW
983  * @dev: ISHTP device instance
984  *
985  * Send resume message to FW. This is useful for system freeze (non S3) case
986  */
987 void ishtp_send_resume(struct ishtp_device *dev)
988 {
989         struct ishtp_msg_hdr    ishtp_hdr;
990         struct ish_system_states_status state_status_msg;
991         const size_t len = sizeof(struct ish_system_states_status);
992
993         fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
994
995         memset(&state_status_msg, 0, len);
996         state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
997         state_status_msg.supported_states = supported_states;
998         current_state &= ~SUSPEND_STATE_BIT;
999         dev->print_log(dev, "%s() sends RESUME notification\n", __func__);
1000         state_status_msg.states_status = current_state;
1001
1002         ishtp_write_message(dev, &ishtp_hdr,
1003                 (unsigned char *)&state_status_msg);
1004 }
1005 EXPORT_SYMBOL(ishtp_send_resume);
1006
1007 /**
1008  * ishtp_query_subscribers() - Send query subscribers message
1009  * @dev: ISHTP device instance
1010  *
1011  * Send message to query subscribers
1012  */
1013 void ishtp_query_subscribers(struct ishtp_device *dev)
1014 {
1015         struct ishtp_msg_hdr    ishtp_hdr;
1016         struct ish_system_states_query_subscribers query_subscribers_msg;
1017         const size_t len = sizeof(struct ish_system_states_query_subscribers);
1018
1019         fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
1020
1021         memset(&query_subscribers_msg, 0, len);
1022         query_subscribers_msg.hdr.cmd = SYSTEM_STATE_QUERY_SUBSCRIBERS;
1023
1024         ishtp_write_message(dev, &ishtp_hdr,
1025                 (unsigned char *)&query_subscribers_msg);
1026 }