1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the NXP SAA7164 PCIe bridge
5 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
8 #include <linux/wait.h>
12 static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
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;
26 mutex_unlock(&dev->lock);
31 static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
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;
40 mutex_unlock(&dev->lock);
43 static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
45 mutex_lock(&dev->lock);
46 if ((dev->cmds[seqno].inuse == 1) &&
47 (dev->cmds[seqno].seqno == seqno)) {
48 dev->cmds[seqno].timeout = 1;
50 mutex_unlock(&dev->lock);
53 static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
57 mutex_lock(&dev->lock);
58 if ((dev->cmds[seqno].inuse == 1) &&
59 (dev->cmds[seqno].seqno == seqno)) {
60 ret = dev->cmds[seqno].timeout;
62 mutex_unlock(&dev->lock);
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)
71 int ret = SAA_OK, i = 0;
73 wait_queue_head_t *q = NULL;
75 dprintk(DBGLVL_CMD, "%s()\n", __func__);
77 /* While any outstand message on the bus exists... */
80 /* Peek the msg bus */
81 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
82 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
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);
91 "%s() signalled seqno(%d) (for dequeue)\n",
92 __func__, tRsp.seqno);
93 dev->cmds[tRsp.seqno].signalled = 1;
97 "%s() found timed out command on the bus\n",
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 */
111 /* It's unlikely to have more than 4 or 5 pending messages,
112 * ensure we exit at some point regardless.
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)
125 wait_queue_head_t *q = NULL;
127 dprintk(DBGLVL_CMD, "%s()\n", __func__);
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)
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);
143 printk(KERN_ERR "found timed out command on the bus\n");
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 */
155 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
156 printk(KERN_ERR "split response\n");
158 saa7164_cmd_free_seqno(dev, tRsp.seqno);
160 printk(KERN_ERR " timeout continue\n");
164 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
165 __func__, tRsp.seqno);
166 dev->cmds[tRsp.seqno].signalled = 1;
172 static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
175 struct tmComResBusInfo *bus = &dev->bus;
183 printk(KERN_ERR "%s() !msg\n", __func__);
184 return SAA_ERR_BAD_PARAMETER;
187 mutex_lock(&dev->cmds[msg->id].lock);
190 cmds = size / bus->m_wMaxReqSize;
191 if (size % bus->m_wMaxReqSize == 0)
196 /* Split the request into smaller chunks */
197 for (idx = 0; idx < cmds; idx++) {
199 msg->flags |= SAA_CMDFLAG_CONTINUE;
200 msg->size = bus->m_wMaxReqSize;
201 tmp = buf + idx * bus->m_wMaxReqSize;
203 ret = saa7164_bus_set(dev, msg, tmp);
205 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
211 ret = SAA_ERR_OVERFLOW;
217 /* If not the last command... */
219 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
221 msg->size = size - idx * bus->m_wMaxReqSize;
223 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
225 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
231 ret = SAA_ERR_OVERFLOW;
237 mutex_unlock(&dev->cmds[msg->id].lock);
241 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
242 * the event never occurred, or SAA_OK if it was signaled during the wait.
244 static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
246 wait_queue_head_t *q = NULL;
247 int ret = SAA_BUS_TIMEOUT;
252 saa7164_bus_dump(dev);
254 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
256 mutex_lock(&dev->lock);
257 if ((dev->cmds[seqno].inuse == 1) &&
258 (dev->cmds[seqno].seqno == seqno)) {
259 q = &dev->cmds[seqno].wait;
261 mutex_unlock(&dev->lock);
264 /* If we haven't been signalled we need to wait */
265 if (dev->cmds[seqno].signalled == 0) {
268 "%s(seqno=%d) Waiting (signalled=%d)\n",
269 __func__, seqno, dev->cmds[seqno].signalled);
271 /* Wait for signalled to be flagged or timeout */
272 /* In a highly stressed system this can easily extend
273 * into multiple seconds before the deferred worker
274 * is scheduled, and we're woken up via signal.
275 * We typically are signalled in < 50ms but it can
278 wait_event_timeout(*q, dev->cmds[seqno].signalled,
280 r = time_before(jiffies, stamp + (HZ * waitsecs));
284 saa7164_cmd_timeout_seqno(dev, seqno);
286 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
288 dev->cmds[seqno].signalled);
292 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
298 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
301 dprintk(DBGLVL_CMD, "%s()\n", __func__);
303 mutex_lock(&dev->lock);
304 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
305 if (dev->cmds[i].inuse == 1) {
307 "seqno %d inuse, sig = %d, t/out = %d\n",
309 dev->cmds[i].signalled,
310 dev->cmds[i].timeout);
314 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
315 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
316 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
317 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
319 dev->cmds[i].signalled = 1;
320 wake_up(&dev->cmds[i].wait);
323 mutex_unlock(&dev->lock);
326 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
327 u16 controlselector, u16 size, void *buf)
329 struct tmComResInfo command_t, *pcommand_t;
330 struct tmComResInfo response_t, *presponse_t;
338 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
339 __func__, saa7164_unitid_name(dev, id), id,
340 command, controlselector);
342 if ((size == 0) || (buf == NULL)) {
343 printk(KERN_ERR "%s() Invalid param\n", __func__);
344 return SAA_ERR_BAD_PARAMETER;
347 /* Prepare some basic command/response structures */
348 memset(&command_t, 0, sizeof(command_t));
349 memset(&response_t, 0, sizeof(response_t));
350 pcommand_t = &command_t;
351 presponse_t = &response_t;
353 command_t.command = command;
354 command_t.controlselector = controlselector;
355 command_t.size = size;
357 /* Allocate a unique sequence number */
358 ret = saa7164_cmd_alloc_seqno(dev);
360 printk(KERN_ERR "%s() No free sequences\n", __func__);
361 ret = SAA_ERR_NO_RESOURCES;
365 command_t.seqno = (u8)ret;
369 pcommand_t->size = size;
371 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
372 __func__, pcommand_t->seqno);
374 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
375 __func__, pcommand_t->size);
377 ret = saa7164_cmd_set(dev, pcommand_t, buf);
379 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
381 if (ret != SAA_ERR_BUSY)
382 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
384 /* Flag a timeout, because at least one
385 * command was sent */
386 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
391 /* With split responses we have to collect the msgs piece by piece */
395 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
397 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
398 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
400 /* if power is down and this is not a power command ... */
402 if (ret == SAA_BUS_TIMEOUT) {
403 printk(KERN_ERR "Event timed out\n");
404 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
409 printk(KERN_ERR "spurious error\n");
414 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
415 if (ret == SAA_ERR_EMPTY) {
416 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
420 printk(KERN_ERR "peek failed\n");
424 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
425 __func__, presponse_t->seqno);
427 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
428 __func__, presponse_t->flags);
430 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
431 __func__, presponse_t->size);
433 /* Check if the response was for our command */
434 if (presponse_t->seqno != pcommand_t->seqno) {
437 "wrong event: seqno = %d, expected seqno = %d, will dequeue regardless\n",
438 presponse_t->seqno, pcommand_t->seqno);
440 ret = saa7164_cmd_dequeue(dev);
442 printk(KERN_ERR "dequeue failed, ret = %d\n",
446 "dequeue exceeded, safety exit\n");
454 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
456 memset(&errdata[0], 0, sizeof(errdata));
458 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
460 printk(KERN_ERR "get error(2)\n");
464 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
466 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
467 __func__, errdata[0], errdata[1], errdata[2],
470 /* Map error codes */
471 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
472 __func__, errdata[0]);
474 switch (errdata[0]) {
475 case PVC_ERRORCODE_INVALID_COMMAND:
476 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
478 ret = SAA_ERR_INVALID_COMMAND;
480 case PVC_ERRORCODE_INVALID_DATA:
481 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
483 ret = SAA_ERR_BAD_PARAMETER;
485 case PVC_ERRORCODE_TIMEOUT:
486 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
487 ret = SAA_ERR_TIMEOUT;
489 case PVC_ERRORCODE_NAK:
490 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
491 ret = SAA_ERR_NULL_PACKET;
493 case PVC_ERRORCODE_UNKNOWN:
494 case PVC_ERRORCODE_INVALID_CONTROL:
496 "%s() UNKNOWN OR INVALID CONTROL\n",
498 ret = SAA_ERR_NOT_SUPPORTED;
501 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
502 ret = SAA_ERR_NOT_SUPPORTED;
505 /* See of other commands are on the bus */
506 if (saa7164_cmd_dequeue(dev) != SAA_OK)
507 printk(KERN_ERR "dequeue(2) failed\n");
512 /* If response is invalid */
513 if ((presponse_t->id != pcommand_t->id) ||
514 (presponse_t->command != pcommand_t->command) ||
515 (presponse_t->controlselector !=
516 pcommand_t->controlselector) ||
517 (((resp_dsize - data_recd) != presponse_t->size) &&
518 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
519 ((resp_dsize - data_recd) < presponse_t->size)) {
522 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
523 ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
525 printk(KERN_ERR "get failed\n");
529 /* See of other commands are on the bus */
530 if (saa7164_cmd_dequeue(dev) != SAA_OK)
531 printk(KERN_ERR "dequeue(3) failed\n");
535 /* OK, now we're actually getting out correct response */
536 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
538 printk(KERN_ERR "get failed\n");
542 data_recd = presponse_t->size + data_recd;
543 if (resp_dsize == data_recd) {
544 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
548 /* See of other commands are on the bus */
549 if (saa7164_cmd_dequeue(dev) != SAA_OK)
550 printk(KERN_ERR "dequeue(3) failed\n");
553 /* Release the sequence number allocation */
554 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
556 /* if powerdown signal all pending commands */
558 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
560 /* See of other commands are on the bus */
561 if (saa7164_cmd_dequeue(dev) != SAA_OK)
562 printk(KERN_ERR "dequeue(4) failed\n");