Merge tag 'riscv/for-v5.5-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / media / pci / saa7164 / saa7164-cmd.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for the NXP SAA7164 PCIe bridge
4  *
5  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
6  */
7
8 #include <linux/wait.h>
9
10 #include "saa7164.h"
11
12 static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
13 {
14         int i, ret = -1;
15
16         mutex_lock(&dev->lock);
17         for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
18                 if (dev->cmds[i].inuse == 0) {
19                         dev->cmds[i].inuse = 1;
20                         dev->cmds[i].signalled = 0;
21                         dev->cmds[i].timeout = 0;
22                         ret = dev->cmds[i].seqno;
23                         break;
24                 }
25         }
26         mutex_unlock(&dev->lock);
27
28         return ret;
29 }
30
31 static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
32 {
33         mutex_lock(&dev->lock);
34         if ((dev->cmds[seqno].inuse == 1) &&
35                 (dev->cmds[seqno].seqno == seqno)) {
36                 dev->cmds[seqno].inuse = 0;
37                 dev->cmds[seqno].signalled = 0;
38                 dev->cmds[seqno].timeout = 0;
39         }
40         mutex_unlock(&dev->lock);
41 }
42
43 static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
44 {
45         mutex_lock(&dev->lock);
46         if ((dev->cmds[seqno].inuse == 1) &&
47                 (dev->cmds[seqno].seqno == seqno)) {
48                 dev->cmds[seqno].timeout = 1;
49         }
50         mutex_unlock(&dev->lock);
51 }
52
53 static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
54 {
55         int ret = 0;
56
57         mutex_lock(&dev->lock);
58         if ((dev->cmds[seqno].inuse == 1) &&
59                 (dev->cmds[seqno].seqno == seqno)) {
60                 ret = dev->cmds[seqno].timeout;
61         }
62         mutex_unlock(&dev->lock);
63
64         return ret;
65 }
66
67 /* Commands to the f/w get marshelled to/from this code then onto the PCI
68  * -bus/c running buffer. */
69 int saa7164_irq_dequeue(struct saa7164_dev *dev)
70 {
71         int ret = SAA_OK, i = 0;
72         u32 timeout;
73         wait_queue_head_t *q = NULL;
74         u8 tmp[512];
75         dprintk(DBGLVL_CMD, "%s()\n", __func__);
76
77         /* While any outstand message on the bus exists... */
78         do {
79
80                 /* Peek the msg bus */
81                 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
82                 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
83                 if (ret != SAA_OK)
84                         break;
85
86                 q = &dev->cmds[tRsp.seqno].wait;
87                 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
88                 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
89                 if (!timeout) {
90                         dprintk(DBGLVL_CMD,
91                                 "%s() signalled seqno(%d) (for dequeue)\n",
92                                 __func__, tRsp.seqno);
93                         dev->cmds[tRsp.seqno].signalled = 1;
94                         wake_up(q);
95                 } else {
96                         printk(KERN_ERR
97                                 "%s() found timed out command on the bus\n",
98                                         __func__);
99
100                         /* Clean the bus */
101                         ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
102                         printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
103                         if (ret == SAA_ERR_EMPTY)
104                                 /* Someone else already fetched the response */
105                                 return SAA_OK;
106
107                         if (ret != SAA_OK)
108                                 return ret;
109                 }
110
111                 /* It's unlikely to have more than 4 or 5 pending messages,
112                  * ensure we exit at some point regardless.
113                  */
114         } while (i++ < 32);
115
116         return ret;
117 }
118
119 /* Commands to the f/w get marshelled to/from this code then onto the PCI
120  * -bus/c running buffer. */
121 static int saa7164_cmd_dequeue(struct saa7164_dev *dev)
122 {
123         int ret;
124         u32 timeout;
125         wait_queue_head_t *q = NULL;
126         u8 tmp[512];
127         dprintk(DBGLVL_CMD, "%s()\n", __func__);
128
129         while (true) {
130
131                 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
132                 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
133                 if (ret == SAA_ERR_EMPTY)
134                         return SAA_OK;
135
136                 if (ret != SAA_OK)
137                         return ret;
138
139                 q = &dev->cmds[tRsp.seqno].wait;
140                 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
141                 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
142                 if (timeout) {
143                         printk(KERN_ERR "found timed out command on the bus\n");
144
145                         /* Clean the bus */
146                         ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
147                         printk(KERN_ERR "ret = %x\n", ret);
148                         if (ret == SAA_ERR_EMPTY)
149                                 /* Someone else already fetched the response */
150                                 return SAA_OK;
151
152                         if (ret != SAA_OK)
153                                 return ret;
154
155                         if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
156                                 printk(KERN_ERR "split response\n");
157                         else
158                                 saa7164_cmd_free_seqno(dev, tRsp.seqno);
159
160                         printk(KERN_ERR " timeout continue\n");
161                         continue;
162                 }
163
164                 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
165                         __func__, tRsp.seqno);
166                 dev->cmds[tRsp.seqno].signalled = 1;
167                 wake_up(q);
168                 return SAA_OK;
169         }
170 }
171
172 static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
173                            void *buf)
174 {
175         struct tmComResBusInfo *bus = &dev->bus;
176         u8 cmd_sent;
177         u16 size, idx;
178         u32 cmds;
179         void *tmp;
180         int ret = -1;
181
182         if (!msg) {
183                 printk(KERN_ERR "%s() !msg\n", __func__);
184                 return SAA_ERR_BAD_PARAMETER;
185         }
186
187         mutex_lock(&dev->cmds[msg->id].lock);
188
189         size = msg->size;
190         idx = 0;
191         cmds = size / bus->m_wMaxReqSize;
192         if (size % bus->m_wMaxReqSize == 0)
193                 cmds -= 1;
194
195         cmd_sent = 0;
196
197         /* Split the request into smaller chunks */
198         for (idx = 0; idx < cmds; idx++) {
199
200                 msg->flags |= SAA_CMDFLAG_CONTINUE;
201                 msg->size = bus->m_wMaxReqSize;
202                 tmp = buf + idx * bus->m_wMaxReqSize;
203
204                 ret = saa7164_bus_set(dev, msg, tmp);
205                 if (ret != SAA_OK) {
206                         printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
207
208                         if (cmd_sent) {
209                                 ret = SAA_ERR_BUSY;
210                                 goto out;
211                         }
212                         ret = SAA_ERR_OVERFLOW;
213                         goto out;
214                 }
215                 cmd_sent = 1;
216         }
217
218         /* If not the last command... */
219         if (idx != 0)
220                 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
221
222         msg->size = size - idx * bus->m_wMaxReqSize;
223
224         ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
225         if (ret != SAA_OK) {
226                 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
227
228                 if (cmd_sent) {
229                         ret = SAA_ERR_BUSY;
230                         goto out;
231                 }
232                 ret = SAA_ERR_OVERFLOW;
233                 goto out;
234         }
235         ret = SAA_OK;
236
237 out:
238         mutex_unlock(&dev->cmds[msg->id].lock);
239         return ret;
240 }
241
242 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
243  * the event never occurred, or SAA_OK if it was signaled during the wait.
244  */
245 static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
246 {
247         wait_queue_head_t *q = NULL;
248         int ret = SAA_BUS_TIMEOUT;
249         unsigned long stamp;
250         int r;
251
252         if (saa_debug >= 4)
253                 saa7164_bus_dump(dev);
254
255         dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
256
257         mutex_lock(&dev->lock);
258         if ((dev->cmds[seqno].inuse == 1) &&
259                 (dev->cmds[seqno].seqno == seqno)) {
260                 q = &dev->cmds[seqno].wait;
261         }
262         mutex_unlock(&dev->lock);
263
264         if (q) {
265                 /* If we haven't been signalled we need to wait */
266                 if (dev->cmds[seqno].signalled == 0) {
267                         stamp = jiffies;
268                         dprintk(DBGLVL_CMD,
269                                 "%s(seqno=%d) Waiting (signalled=%d)\n",
270                                 __func__, seqno, dev->cmds[seqno].signalled);
271
272                         /* Wait for signalled to be flagged or timeout */
273                         /* In a highly stressed system this can easily extend
274                          * into multiple seconds before the deferred worker
275                          * is scheduled, and we're woken up via signal.
276                          * We typically are signalled in < 50ms but it can
277                          * take MUCH longer.
278                          */
279                         wait_event_timeout(*q, dev->cmds[seqno].signalled,
280                                 (HZ * waitsecs));
281                         r = time_before(jiffies, stamp + (HZ * waitsecs));
282                         if (r)
283                                 ret = SAA_OK;
284                         else
285                                 saa7164_cmd_timeout_seqno(dev, seqno);
286
287                         dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
288                                 __func__, seqno, r,
289                                 dev->cmds[seqno].signalled);
290                 } else
291                         ret = SAA_OK;
292         } else
293                 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
294                         __func__, seqno);
295
296         return ret;
297 }
298
299 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
300 {
301         int i;
302         dprintk(DBGLVL_CMD, "%s()\n", __func__);
303
304         mutex_lock(&dev->lock);
305         for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
306                 if (dev->cmds[i].inuse == 1) {
307                         dprintk(DBGLVL_CMD,
308                                 "seqno %d inuse, sig = %d, t/out = %d\n",
309                                 dev->cmds[i].seqno,
310                                 dev->cmds[i].signalled,
311                                 dev->cmds[i].timeout);
312                 }
313         }
314
315         for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
316                 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
317                         (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
318                         dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
319                                 __func__, i);
320                         dev->cmds[i].signalled = 1;
321                         wake_up(&dev->cmds[i].wait);
322                 }
323         }
324         mutex_unlock(&dev->lock);
325 }
326
327 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
328         u16 controlselector, u16 size, void *buf)
329 {
330         struct tmComResInfo command_t, *pcommand_t;
331         struct tmComResInfo response_t, *presponse_t;
332         u8 errdata[256];
333         u16 resp_dsize;
334         u16 data_recd;
335         u32 loop;
336         int ret;
337         int safety = 0;
338
339         dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
340                 __func__, saa7164_unitid_name(dev, id), id,
341                 command, controlselector);
342
343         if ((size == 0) || (buf == NULL)) {
344                 printk(KERN_ERR "%s() Invalid param\n", __func__);
345                 return SAA_ERR_BAD_PARAMETER;
346         }
347
348         /* Prepare some basic command/response structures */
349         memset(&command_t, 0, sizeof(command_t));
350         memset(&response_t, 0, sizeof(response_t));
351         pcommand_t = &command_t;
352         presponse_t = &response_t;
353         command_t.id = id;
354         command_t.command = command;
355         command_t.controlselector = controlselector;
356         command_t.size = size;
357
358         /* Allocate a unique sequence number */
359         ret = saa7164_cmd_alloc_seqno(dev);
360         if (ret < 0) {
361                 printk(KERN_ERR "%s() No free sequences\n", __func__);
362                 ret = SAA_ERR_NO_RESOURCES;
363                 goto out;
364         }
365
366         command_t.seqno = (u8)ret;
367
368         /* Send Command */
369         resp_dsize = size;
370         pcommand_t->size = size;
371
372         dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
373                 __func__, pcommand_t->seqno);
374
375         dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
376                 __func__, pcommand_t->size);
377
378         ret = saa7164_cmd_set(dev, pcommand_t, buf);
379         if (ret != SAA_OK) {
380                 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
381
382                 if (ret != SAA_ERR_BUSY)
383                         saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
384                 else
385                         /* Flag a timeout, because at least one
386                          * command was sent */
387                         saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
388
389                 goto out;
390         }
391
392         /* With split responses we have to collect the msgs piece by piece */
393         data_recd = 0;
394         loop = 1;
395         while (loop) {
396                 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
397
398                 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
399                 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
400
401                 /* if power is down and this is not a power command ... */
402
403                 if (ret == SAA_BUS_TIMEOUT) {
404                         printk(KERN_ERR "Event timed out\n");
405                         saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
406                         return ret;
407                 }
408
409                 if (ret != SAA_OK) {
410                         printk(KERN_ERR "spurious error\n");
411                         return ret;
412                 }
413
414                 /* Peek response */
415                 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
416                 if (ret == SAA_ERR_EMPTY) {
417                         dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
418                         continue;
419                 }
420                 if (ret != SAA_OK) {
421                         printk(KERN_ERR "peek failed\n");
422                         return ret;
423                 }
424
425                 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
426                         __func__, presponse_t->seqno);
427
428                 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
429                         __func__, presponse_t->flags);
430
431                 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
432                         __func__, presponse_t->size);
433
434                 /* Check if the response was for our command */
435                 if (presponse_t->seqno != pcommand_t->seqno) {
436
437                         dprintk(DBGLVL_CMD,
438                                 "wrong event: seqno = %d, expected seqno = %d, will dequeue regardless\n",
439                                 presponse_t->seqno, pcommand_t->seqno);
440
441                         ret = saa7164_cmd_dequeue(dev);
442                         if (ret != SAA_OK) {
443                                 printk(KERN_ERR "dequeue failed, ret = %d\n",
444                                         ret);
445                                 if (safety++ > 16) {
446                                         printk(KERN_ERR
447                                         "dequeue exceeded, safety exit\n");
448                                         return SAA_ERR_BUSY;
449                                 }
450                         }
451
452                         continue;
453                 }
454
455                 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
456
457                         memset(&errdata[0], 0, sizeof(errdata));
458
459                         ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
460                         if (ret != SAA_OK) {
461                                 printk(KERN_ERR "get error(2)\n");
462                                 return ret;
463                         }
464
465                         saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
466
467                         dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
468                                 __func__, errdata[0], errdata[1], errdata[2],
469                                 errdata[3]);
470
471                         /* Map error codes */
472                         dprintk(DBGLVL_CMD, "%s() cmd, error code  = 0x%x\n",
473                                 __func__, errdata[0]);
474
475                         switch (errdata[0]) {
476                         case PVC_ERRORCODE_INVALID_COMMAND:
477                                 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
478                                         __func__);
479                                 ret = SAA_ERR_INVALID_COMMAND;
480                                 break;
481                         case PVC_ERRORCODE_INVALID_DATA:
482                                 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
483                                         __func__);
484                                 ret = SAA_ERR_BAD_PARAMETER;
485                                 break;
486                         case PVC_ERRORCODE_TIMEOUT:
487                                 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
488                                 ret = SAA_ERR_TIMEOUT;
489                                 break;
490                         case PVC_ERRORCODE_NAK:
491                                 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
492                                 ret = SAA_ERR_NULL_PACKET;
493                                 break;
494                         case PVC_ERRORCODE_UNKNOWN:
495                         case PVC_ERRORCODE_INVALID_CONTROL:
496                                 dprintk(DBGLVL_CMD,
497                                         "%s() UNKNOWN OR INVALID CONTROL\n",
498                                         __func__);
499                                 ret = SAA_ERR_NOT_SUPPORTED;
500                                 break;
501                         default:
502                                 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
503                                 ret = SAA_ERR_NOT_SUPPORTED;
504                         }
505
506                         /* See of other commands are on the bus */
507                         if (saa7164_cmd_dequeue(dev) != SAA_OK)
508                                 printk(KERN_ERR "dequeue(2) failed\n");
509
510                         return ret;
511                 }
512
513                 /* If response is invalid */
514                 if ((presponse_t->id != pcommand_t->id) ||
515                         (presponse_t->command != pcommand_t->command) ||
516                         (presponse_t->controlselector !=
517                                 pcommand_t->controlselector) ||
518                         (((resp_dsize - data_recd) != presponse_t->size) &&
519                                 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
520                         ((resp_dsize - data_recd) < presponse_t->size)) {
521
522                         /* Invalid */
523                         dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
524                         ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
525                         if (ret != SAA_OK) {
526                                 printk(KERN_ERR "get failed\n");
527                                 return ret;
528                         }
529
530                         /* See of other commands are on the bus */
531                         if (saa7164_cmd_dequeue(dev) != SAA_OK)
532                                 printk(KERN_ERR "dequeue(3) failed\n");
533                         continue;
534                 }
535
536                 /* OK, now we're actually getting out correct response */
537                 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
538                 if (ret != SAA_OK) {
539                         printk(KERN_ERR "get failed\n");
540                         return ret;
541                 }
542
543                 data_recd = presponse_t->size + data_recd;
544                 if (resp_dsize == data_recd) {
545                         dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
546                         break;
547                 }
548
549                 /* See of other commands are on the bus */
550                 if (saa7164_cmd_dequeue(dev) != SAA_OK)
551                         printk(KERN_ERR "dequeue(3) failed\n");
552
553                 continue;
554
555         } /* (loop) */
556
557         /* Release the sequence number allocation */
558         saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
559
560         /* if powerdown signal all pending commands */
561
562         dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
563
564         /* See of other commands are on the bus */
565         if (saa7164_cmd_dequeue(dev) != SAA_OK)
566                 printk(KERN_ERR "dequeue(4) failed\n");
567
568         ret = SAA_OK;
569 out:
570         return ret;
571 }
572