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