Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / saa7164 / saa7164-cmd.c
blobdfebd77ada59d9666ecb6e5d65b7b29bb5683c86
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/wait.h>
20 #include "saa7164.h"
22 static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
24 int i, ret = -1;
26 mutex_lock(&dev->lock);
27 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
28 if (dev->cmds[i].inuse == 0) {
29 dev->cmds[i].inuse = 1;
30 dev->cmds[i].signalled = 0;
31 dev->cmds[i].timeout = 0;
32 ret = dev->cmds[i].seqno;
33 break;
36 mutex_unlock(&dev->lock);
38 return ret;
41 static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
43 mutex_lock(&dev->lock);
44 if ((dev->cmds[seqno].inuse == 1) &&
45 (dev->cmds[seqno].seqno == seqno)) {
46 dev->cmds[seqno].inuse = 0;
47 dev->cmds[seqno].signalled = 0;
48 dev->cmds[seqno].timeout = 0;
50 mutex_unlock(&dev->lock);
53 static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
55 mutex_lock(&dev->lock);
56 if ((dev->cmds[seqno].inuse == 1) &&
57 (dev->cmds[seqno].seqno == seqno)) {
58 dev->cmds[seqno].timeout = 1;
60 mutex_unlock(&dev->lock);
63 static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
65 int ret = 0;
67 mutex_lock(&dev->lock);
68 if ((dev->cmds[seqno].inuse == 1) &&
69 (dev->cmds[seqno].seqno == seqno)) {
70 ret = dev->cmds[seqno].timeout;
72 mutex_unlock(&dev->lock);
74 return ret;
77 /* Commands to the f/w get marshelled to/from this code then onto the PCI
78 * -bus/c running buffer. */
79 int saa7164_irq_dequeue(struct saa7164_dev *dev)
81 int ret = SAA_OK, i = 0;
82 u32 timeout;
83 wait_queue_head_t *q = NULL;
84 u8 tmp[512];
85 dprintk(DBGLVL_CMD, "%s()\n", __func__);
87 /* While any outstand message on the bus exists... */
88 do {
90 /* Peek the msg bus */
91 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
92 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
93 if (ret != SAA_OK)
94 break;
96 q = &dev->cmds[tRsp.seqno].wait;
97 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
98 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
99 if (!timeout) {
100 dprintk(DBGLVL_CMD,
101 "%s() signalled seqno(%d) (for dequeue)\n",
102 __func__, tRsp.seqno);
103 dev->cmds[tRsp.seqno].signalled = 1;
104 wake_up(q);
105 } else {
106 printk(KERN_ERR
107 "%s() found timed out command on the bus\n",
108 __func__);
110 /* Clean the bus */
111 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
112 printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
113 if (ret == SAA_ERR_EMPTY)
114 /* Someone else already fetched the response */
115 return SAA_OK;
117 if (ret != SAA_OK)
118 return ret;
121 /* It's unlikely to have more than 4 or 5 pending messages,
122 * ensure we exit at some point regardless.
124 } while (i++ < 32);
126 return ret;
129 /* Commands to the f/w get marshelled to/from this code then onto the PCI
130 * -bus/c running buffer. */
131 static int saa7164_cmd_dequeue(struct saa7164_dev *dev)
133 int ret;
134 u32 timeout;
135 wait_queue_head_t *q = NULL;
136 u8 tmp[512];
137 dprintk(DBGLVL_CMD, "%s()\n", __func__);
139 while (true) {
141 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
142 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
143 if (ret == SAA_ERR_EMPTY)
144 return SAA_OK;
146 if (ret != SAA_OK)
147 return ret;
149 q = &dev->cmds[tRsp.seqno].wait;
150 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
151 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
152 if (timeout) {
153 printk(KERN_ERR "found timed out command on the bus\n");
155 /* Clean the bus */
156 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
157 printk(KERN_ERR "ret = %x\n", ret);
158 if (ret == SAA_ERR_EMPTY)
159 /* Someone else already fetched the response */
160 return SAA_OK;
162 if (ret != SAA_OK)
163 return ret;
165 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
166 printk(KERN_ERR "split response\n");
167 else
168 saa7164_cmd_free_seqno(dev, tRsp.seqno);
170 printk(KERN_ERR " timeout continue\n");
171 continue;
174 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
175 __func__, tRsp.seqno);
176 dev->cmds[tRsp.seqno].signalled = 1;
177 wake_up(q);
178 return SAA_OK;
182 static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
183 void *buf)
185 struct tmComResBusInfo *bus = &dev->bus;
186 u8 cmd_sent;
187 u16 size, idx;
188 u32 cmds;
189 void *tmp;
190 int ret = -1;
192 if (!msg) {
193 printk(KERN_ERR "%s() !msg\n", __func__);
194 return SAA_ERR_BAD_PARAMETER;
197 mutex_lock(&dev->cmds[msg->id].lock);
199 size = msg->size;
200 idx = 0;
201 cmds = size / bus->m_wMaxReqSize;
202 if (size % bus->m_wMaxReqSize == 0)
203 cmds -= 1;
205 cmd_sent = 0;
207 /* Split the request into smaller chunks */
208 for (idx = 0; idx < cmds; idx++) {
210 msg->flags |= SAA_CMDFLAG_CONTINUE;
211 msg->size = bus->m_wMaxReqSize;
212 tmp = buf + idx * bus->m_wMaxReqSize;
214 ret = saa7164_bus_set(dev, msg, tmp);
215 if (ret != SAA_OK) {
216 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
218 if (cmd_sent) {
219 ret = SAA_ERR_BUSY;
220 goto out;
222 ret = SAA_ERR_OVERFLOW;
223 goto out;
225 cmd_sent = 1;
228 /* If not the last command... */
229 if (idx != 0)
230 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
232 msg->size = size - idx * bus->m_wMaxReqSize;
234 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
235 if (ret != SAA_OK) {
236 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
238 if (cmd_sent) {
239 ret = SAA_ERR_BUSY;
240 goto out;
242 ret = SAA_ERR_OVERFLOW;
243 goto out;
245 ret = SAA_OK;
247 out:
248 mutex_unlock(&dev->cmds[msg->id].lock);
249 return ret;
252 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
253 * the event never occurred, or SAA_OK if it was signaled during the wait.
255 static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
257 wait_queue_head_t *q = NULL;
258 int ret = SAA_BUS_TIMEOUT;
259 unsigned long stamp;
260 int r;
262 if (saa_debug >= 4)
263 saa7164_bus_dump(dev);
265 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
267 mutex_lock(&dev->lock);
268 if ((dev->cmds[seqno].inuse == 1) &&
269 (dev->cmds[seqno].seqno == seqno)) {
270 q = &dev->cmds[seqno].wait;
272 mutex_unlock(&dev->lock);
274 if (q) {
275 /* If we haven't been signalled we need to wait */
276 if (dev->cmds[seqno].signalled == 0) {
277 stamp = jiffies;
278 dprintk(DBGLVL_CMD,
279 "%s(seqno=%d) Waiting (signalled=%d)\n",
280 __func__, seqno, dev->cmds[seqno].signalled);
282 /* Wait for signalled to be flagged or timeout */
283 /* In a highly stressed system this can easily extend
284 * into multiple seconds before the deferred worker
285 * is scheduled, and we're woken up via signal.
286 * We typically are signalled in < 50ms but it can
287 * take MUCH longer.
289 wait_event_timeout(*q, dev->cmds[seqno].signalled,
290 (HZ * waitsecs));
291 r = time_before(jiffies, stamp + (HZ * waitsecs));
292 if (r)
293 ret = SAA_OK;
294 else
295 saa7164_cmd_timeout_seqno(dev, seqno);
297 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
298 __func__, seqno, r,
299 dev->cmds[seqno].signalled);
300 } else
301 ret = SAA_OK;
302 } else
303 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
304 __func__, seqno);
306 return ret;
309 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
311 int i;
312 dprintk(DBGLVL_CMD, "%s()\n", __func__);
314 mutex_lock(&dev->lock);
315 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
316 if (dev->cmds[i].inuse == 1) {
317 dprintk(DBGLVL_CMD,
318 "seqno %d inuse, sig = %d, t/out = %d\n",
319 dev->cmds[i].seqno,
320 dev->cmds[i].signalled,
321 dev->cmds[i].timeout);
325 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
326 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
327 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
328 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
329 __func__, i);
330 dev->cmds[i].signalled = 1;
331 wake_up(&dev->cmds[i].wait);
334 mutex_unlock(&dev->lock);
337 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
338 u16 controlselector, u16 size, void *buf)
340 struct tmComResInfo command_t, *pcommand_t;
341 struct tmComResInfo response_t, *presponse_t;
342 u8 errdata[256];
343 u16 resp_dsize;
344 u16 data_recd;
345 u32 loop;
346 int ret;
347 int safety = 0;
349 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
350 __func__, saa7164_unitid_name(dev, id), id,
351 command, controlselector);
353 if ((size == 0) || (buf == NULL)) {
354 printk(KERN_ERR "%s() Invalid param\n", __func__);
355 return SAA_ERR_BAD_PARAMETER;
358 /* Prepare some basic command/response structures */
359 memset(&command_t, 0, sizeof(command_t));
360 memset(&response_t, 0, sizeof(response_t));
361 pcommand_t = &command_t;
362 presponse_t = &response_t;
363 command_t.id = id;
364 command_t.command = command;
365 command_t.controlselector = controlselector;
366 command_t.size = size;
368 /* Allocate a unique sequence number */
369 ret = saa7164_cmd_alloc_seqno(dev);
370 if (ret < 0) {
371 printk(KERN_ERR "%s() No free sequences\n", __func__);
372 ret = SAA_ERR_NO_RESOURCES;
373 goto out;
376 command_t.seqno = (u8)ret;
378 /* Send Command */
379 resp_dsize = size;
380 pcommand_t->size = size;
382 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
383 __func__, pcommand_t->seqno);
385 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
386 __func__, pcommand_t->size);
388 ret = saa7164_cmd_set(dev, pcommand_t, buf);
389 if (ret != SAA_OK) {
390 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
392 if (ret != SAA_ERR_BUSY)
393 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
394 else
395 /* Flag a timeout, because at least one
396 * command was sent */
397 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
399 goto out;
402 /* With split responses we have to collect the msgs piece by piece */
403 data_recd = 0;
404 loop = 1;
405 while (loop) {
406 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
408 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
409 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
411 /* if power is down and this is not a power command ... */
413 if (ret == SAA_BUS_TIMEOUT) {
414 printk(KERN_ERR "Event timed out\n");
415 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
416 return ret;
419 if (ret != SAA_OK) {
420 printk(KERN_ERR "spurious error\n");
421 return ret;
424 /* Peek response */
425 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
426 if (ret == SAA_ERR_EMPTY) {
427 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
428 continue;
430 if (ret != SAA_OK) {
431 printk(KERN_ERR "peek failed\n");
432 return ret;
435 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
436 __func__, presponse_t->seqno);
438 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
439 __func__, presponse_t->flags);
441 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
442 __func__, presponse_t->size);
444 /* Check if the response was for our command */
445 if (presponse_t->seqno != pcommand_t->seqno) {
447 dprintk(DBGLVL_CMD,
448 "wrong event: seqno = %d, expected seqno = %d, will dequeue regardless\n",
449 presponse_t->seqno, pcommand_t->seqno);
451 ret = saa7164_cmd_dequeue(dev);
452 if (ret != SAA_OK) {
453 printk(KERN_ERR "dequeue failed, ret = %d\n",
454 ret);
455 if (safety++ > 16) {
456 printk(KERN_ERR
457 "dequeue exceeded, safety exit\n");
458 return SAA_ERR_BUSY;
462 continue;
465 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
467 memset(&errdata[0], 0, sizeof(errdata));
469 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
470 if (ret != SAA_OK) {
471 printk(KERN_ERR "get error(2)\n");
472 return ret;
475 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
477 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
478 __func__, errdata[0], errdata[1], errdata[2],
479 errdata[3]);
481 /* Map error codes */
482 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
483 __func__, errdata[0]);
485 switch (errdata[0]) {
486 case PVC_ERRORCODE_INVALID_COMMAND:
487 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
488 __func__);
489 ret = SAA_ERR_INVALID_COMMAND;
490 break;
491 case PVC_ERRORCODE_INVALID_DATA:
492 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
493 __func__);
494 ret = SAA_ERR_BAD_PARAMETER;
495 break;
496 case PVC_ERRORCODE_TIMEOUT:
497 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
498 ret = SAA_ERR_TIMEOUT;
499 break;
500 case PVC_ERRORCODE_NAK:
501 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
502 ret = SAA_ERR_NULL_PACKET;
503 break;
504 case PVC_ERRORCODE_UNKNOWN:
505 case PVC_ERRORCODE_INVALID_CONTROL:
506 dprintk(DBGLVL_CMD,
507 "%s() UNKNOWN OR INVALID CONTROL\n",
508 __func__);
509 ret = SAA_ERR_NOT_SUPPORTED;
510 break;
511 default:
512 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
513 ret = SAA_ERR_NOT_SUPPORTED;
516 /* See of other commands are on the bus */
517 if (saa7164_cmd_dequeue(dev) != SAA_OK)
518 printk(KERN_ERR "dequeue(2) failed\n");
520 return ret;
523 /* If response is invalid */
524 if ((presponse_t->id != pcommand_t->id) ||
525 (presponse_t->command != pcommand_t->command) ||
526 (presponse_t->controlselector !=
527 pcommand_t->controlselector) ||
528 (((resp_dsize - data_recd) != presponse_t->size) &&
529 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
530 ((resp_dsize - data_recd) < presponse_t->size)) {
532 /* Invalid */
533 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
534 ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
535 if (ret != SAA_OK) {
536 printk(KERN_ERR "get failed\n");
537 return ret;
540 /* See of other commands are on the bus */
541 if (saa7164_cmd_dequeue(dev) != SAA_OK)
542 printk(KERN_ERR "dequeue(3) failed\n");
543 continue;
546 /* OK, now we're actually getting out correct response */
547 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
548 if (ret != SAA_OK) {
549 printk(KERN_ERR "get failed\n");
550 return ret;
553 data_recd = presponse_t->size + data_recd;
554 if (resp_dsize == data_recd) {
555 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
556 break;
559 /* See of other commands are on the bus */
560 if (saa7164_cmd_dequeue(dev) != SAA_OK)
561 printk(KERN_ERR "dequeue(3) failed\n");
563 continue;
565 } /* (loop) */
567 /* Release the sequence number allocation */
568 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
570 /* if powerdown signal all pending commands */
572 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
574 /* See of other commands are on the bus */
575 if (saa7164_cmd_dequeue(dev) != SAA_OK)
576 printk(KERN_ERR "dequeue(4) failed\n");
578 ret = SAA_OK;
579 out:
580 return ret;