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>
22 static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
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;
36 mutex_unlock(&dev->lock);
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)
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);
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;
83 wait_queue_head_t *q = NULL;
85 dprintk(DBGLVL_CMD, "%s()\n", __func__);
87 /* While any outstand message on the bus exists... */
90 /* Peek the msg bus */
91 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
92 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
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);
101 "%s() signalled seqno(%d) (for dequeue)\n",
102 __func__, tRsp.seqno);
103 dev->cmds[tRsp.seqno].signalled = 1;
107 "%s() found timed out command on the bus\n",
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 */
121 /* It's unlikely to have more than 4 or 5 pending messages,
122 * ensure we exit at some point regardless.
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)
135 wait_queue_head_t *q = NULL;
137 dprintk(DBGLVL_CMD, "%s()\n", __func__);
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)
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);
153 printk(KERN_ERR "found timed out command on the bus\n");
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 */
165 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
166 printk(KERN_ERR "split response\n");
168 saa7164_cmd_free_seqno(dev, tRsp.seqno);
170 printk(KERN_ERR " timeout continue\n");
174 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
175 __func__, tRsp.seqno);
176 dev->cmds[tRsp.seqno].signalled = 1;
182 static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
185 struct tmComResBusInfo *bus = &dev->bus;
193 printk(KERN_ERR "%s() !msg\n", __func__);
194 return SAA_ERR_BAD_PARAMETER;
197 mutex_lock(&dev->cmds[msg->id].lock);
201 cmds = size / bus->m_wMaxReqSize;
202 if (size % bus->m_wMaxReqSize == 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);
216 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
222 ret = SAA_ERR_OVERFLOW;
228 /* If not the last command... */
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);
236 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
242 ret = SAA_ERR_OVERFLOW;
248 mutex_unlock(&dev->cmds[msg->id].lock);
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;
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);
275 /* If we haven't been signalled we need to wait */
276 if (dev->cmds[seqno].signalled == 0) {
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
289 wait_event_timeout(*q, dev->cmds[seqno].signalled,
291 r = time_before(jiffies, stamp + (HZ * waitsecs));
295 saa7164_cmd_timeout_seqno(dev, seqno);
297 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
299 dev->cmds[seqno].signalled);
303 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
309 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
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) {
318 "seqno %d inuse, sig = %d, t/out = %d\n",
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",
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;
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;
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);
371 printk(KERN_ERR "%s() No free sequences\n", __func__);
372 ret = SAA_ERR_NO_RESOURCES;
376 command_t.seqno = (u8)ret;
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);
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);
395 /* Flag a timeout, because at least one
396 * command was sent */
397 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
402 /* With split responses we have to collect the msgs piece by piece */
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);
420 printk(KERN_ERR "spurious error\n");
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__);
431 printk(KERN_ERR "peek failed\n");
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) {
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);
453 printk(KERN_ERR "dequeue failed, ret = %d\n",
457 "dequeue exceeded, safety exit\n");
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);
471 printk(KERN_ERR "get error(2)\n");
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],
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",
489 ret = SAA_ERR_INVALID_COMMAND;
491 case PVC_ERRORCODE_INVALID_DATA:
492 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
494 ret = SAA_ERR_BAD_PARAMETER;
496 case PVC_ERRORCODE_TIMEOUT:
497 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
498 ret = SAA_ERR_TIMEOUT;
500 case PVC_ERRORCODE_NAK:
501 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
502 ret = SAA_ERR_NULL_PACKET;
504 case PVC_ERRORCODE_UNKNOWN:
505 case PVC_ERRORCODE_INVALID_CONTROL:
507 "%s() UNKNOWN OR INVALID CONTROL\n",
509 ret = SAA_ERR_NOT_SUPPORTED;
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");
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)) {
533 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
534 ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
536 printk(KERN_ERR "get failed\n");
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");
546 /* OK, now we're actually getting out correct response */
547 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
549 printk(KERN_ERR "get failed\n");
553 data_recd = presponse_t->size + data_recd;
554 if (resp_dsize == data_recd) {
555 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
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");
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");