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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/wait.h>
26 static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
30 mutex_lock(&dev->lock);
31 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
32 if (dev->cmds[i].inuse == 0) {
33 dev->cmds[i].inuse = 1;
34 dev->cmds[i].signalled = 0;
35 dev->cmds[i].timeout = 0;
36 ret = dev->cmds[i].seqno;
40 mutex_unlock(&dev->lock);
45 static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
47 mutex_lock(&dev->lock);
48 if ((dev->cmds[seqno].inuse == 1) &&
49 (dev->cmds[seqno].seqno == seqno)) {
50 dev->cmds[seqno].inuse = 0;
51 dev->cmds[seqno].signalled = 0;
52 dev->cmds[seqno].timeout = 0;
54 mutex_unlock(&dev->lock);
57 static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
59 mutex_lock(&dev->lock);
60 if ((dev->cmds[seqno].inuse == 1) &&
61 (dev->cmds[seqno].seqno == seqno)) {
62 dev->cmds[seqno].timeout = 1;
64 mutex_unlock(&dev->lock);
67 static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
71 mutex_lock(&dev->lock);
72 if ((dev->cmds[seqno].inuse == 1) &&
73 (dev->cmds[seqno].seqno == seqno)) {
74 ret = dev->cmds[seqno].timeout;
76 mutex_unlock(&dev->lock);
81 /* Commands to the f/w get marshelled to/from this code then onto the PCI
82 * -bus/c running buffer. */
83 int saa7164_irq_dequeue(struct saa7164_dev *dev)
85 int ret = SAA_OK, i = 0;
87 wait_queue_head_t *q = NULL;
89 dprintk(DBGLVL_CMD, "%s()\n", __func__);
91 /* While any outstand message on the bus exists... */
94 /* Peek the msg bus */
95 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
96 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
100 q = &dev->cmds[tRsp.seqno].wait;
101 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
102 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
105 "%s() signalled seqno(%d) (for dequeue)\n",
106 __func__, tRsp.seqno);
107 dev->cmds[tRsp.seqno].signalled = 1;
111 "%s() found timed out command on the bus\n",
115 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
116 printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
117 if (ret == SAA_ERR_EMPTY)
118 /* Someone else already fetched the response */
125 /* It's unlikely to have more than 4 or 5 pending messages,
126 * ensure we exit at some point regardless.
133 /* Commands to the f/w get marshelled to/from this code then onto the PCI
134 * -bus/c running buffer. */
135 static int saa7164_cmd_dequeue(struct saa7164_dev *dev)
140 wait_queue_head_t *q = NULL;
142 dprintk(DBGLVL_CMD, "%s()\n", __func__);
146 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
147 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
148 if (ret == SAA_ERR_EMPTY)
154 q = &dev->cmds[tRsp.seqno].wait;
155 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
156 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
158 printk(KERN_ERR "found timed out command on the bus\n");
161 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
162 printk(KERN_ERR "ret = %x\n", ret);
163 if (ret == SAA_ERR_EMPTY)
164 /* Someone else already fetched the response */
170 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
171 printk(KERN_ERR "split response\n");
173 saa7164_cmd_free_seqno(dev, tRsp.seqno);
175 printk(KERN_ERR " timeout continue\n");
179 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
180 __func__, tRsp.seqno);
181 dev->cmds[tRsp.seqno].signalled = 1;
189 static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
192 struct tmComResBusInfo *bus = &dev->bus;
200 printk(KERN_ERR "%s() !msg\n", __func__);
201 return SAA_ERR_BAD_PARAMETER;
204 mutex_lock(&dev->cmds[msg->id].lock);
208 cmds = size / bus->m_wMaxReqSize;
209 if (size % bus->m_wMaxReqSize == 0)
214 /* Split the request into smaller chunks */
215 for (idx = 0; idx < cmds; idx++) {
217 msg->flags |= SAA_CMDFLAG_CONTINUE;
218 msg->size = bus->m_wMaxReqSize;
219 tmp = buf + idx * bus->m_wMaxReqSize;
221 ret = saa7164_bus_set(dev, msg, tmp);
223 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
229 ret = SAA_ERR_OVERFLOW;
235 /* If not the last command... */
237 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
239 msg->size = size - idx * bus->m_wMaxReqSize;
241 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
243 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
249 ret = SAA_ERR_OVERFLOW;
255 mutex_unlock(&dev->cmds[msg->id].lock);
259 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
260 * the event never occurred, or SAA_OK if it was signaled during the wait.
262 static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
264 wait_queue_head_t *q = NULL;
265 int ret = SAA_BUS_TIMEOUT;
270 saa7164_bus_dump(dev);
272 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
274 mutex_lock(&dev->lock);
275 if ((dev->cmds[seqno].inuse == 1) &&
276 (dev->cmds[seqno].seqno == seqno)) {
277 q = &dev->cmds[seqno].wait;
279 mutex_unlock(&dev->lock);
282 /* If we haven't been signalled we need to wait */
283 if (dev->cmds[seqno].signalled == 0) {
286 "%s(seqno=%d) Waiting (signalled=%d)\n",
287 __func__, seqno, dev->cmds[seqno].signalled);
289 /* Wait for signalled to be flagged or timeout */
290 /* In a highly stressed system this can easily extend
291 * into multiple seconds before the deferred worker
292 * is scheduled, and we're woken up via signal.
293 * We typically are signalled in < 50ms but it can
296 wait_event_timeout(*q, dev->cmds[seqno].signalled,
298 r = time_before(jiffies, stamp + (HZ * waitsecs));
302 saa7164_cmd_timeout_seqno(dev, seqno);
304 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d "
305 "(signalled=%d)\n", __func__, seqno, r,
306 dev->cmds[seqno].signalled);
310 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
316 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
319 dprintk(DBGLVL_CMD, "%s()\n", __func__);
321 mutex_lock(&dev->lock);
322 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
323 if (dev->cmds[i].inuse == 1) {
325 "seqno %d inuse, sig = %d, t/out = %d\n",
327 dev->cmds[i].signalled,
328 dev->cmds[i].timeout);
332 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
333 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
334 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
335 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
337 dev->cmds[i].signalled = 1;
338 wake_up(&dev->cmds[i].wait);
341 mutex_unlock(&dev->lock);
344 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
345 u16 controlselector, u16 size, void *buf)
347 struct tmComResInfo command_t, *pcommand_t;
348 struct tmComResInfo response_t, *presponse_t;
356 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, "
357 "sel = 0x%x)\n", __func__, saa7164_unitid_name(dev, id), id,
358 command, controlselector);
360 if ((size == 0) || (buf == NULL)) {
361 printk(KERN_ERR "%s() Invalid param\n", __func__);
362 return SAA_ERR_BAD_PARAMETER;
365 /* Prepare some basic command/response structures */
366 memset(&command_t, 0, sizeof(command_t));
367 memset(&response_t, 0, sizeof(response_t));
368 pcommand_t = &command_t;
369 presponse_t = &response_t;
371 command_t.command = command;
372 command_t.controlselector = controlselector;
373 command_t.size = size;
375 /* Allocate a unique sequence number */
376 ret = saa7164_cmd_alloc_seqno(dev);
378 printk(KERN_ERR "%s() No free sequences\n", __func__);
379 ret = SAA_ERR_NO_RESOURCES;
383 command_t.seqno = (u8)ret;
387 pcommand_t->size = size;
389 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
390 __func__, pcommand_t->seqno);
392 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
393 __func__, pcommand_t->size);
395 ret = saa7164_cmd_set(dev, pcommand_t, buf);
397 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
399 if (ret != SAA_ERR_BUSY)
400 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
402 /* Flag a timeout, because at least one
403 * command was sent */
404 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
409 /* With split responses we have to collect the msgs piece by piece */
413 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
415 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
416 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
418 /* if power is down and this is not a power command ... */
420 if (ret == SAA_BUS_TIMEOUT) {
421 printk(KERN_ERR "Event timed out\n");
422 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
427 printk(KERN_ERR "spurious error\n");
432 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
433 if (ret == SAA_ERR_EMPTY) {
434 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
438 printk(KERN_ERR "peek failed\n");
442 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
443 __func__, presponse_t->seqno);
445 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
446 __func__, presponse_t->flags);
448 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
449 __func__, presponse_t->size);
451 /* Check if the response was for our command */
452 if (presponse_t->seqno != pcommand_t->seqno) {
455 "wrong event: seqno = %d, "
456 "expected seqno = %d, "
457 "will dequeue regardless\n",
458 presponse_t->seqno, pcommand_t->seqno);
460 ret = saa7164_cmd_dequeue(dev);
462 printk(KERN_ERR "dequeue failed, ret = %d\n",
466 "dequeue exceeded, safety exit\n");
474 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
476 memset(&errdata[0], 0, sizeof(errdata));
478 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
480 printk(KERN_ERR "get error(2)\n");
484 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
486 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
487 __func__, errdata[0], errdata[1], errdata[2],
490 /* Map error codes */
491 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
492 __func__, errdata[0]);
494 switch (errdata[0]) {
495 case PVC_ERRORCODE_INVALID_COMMAND:
496 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
498 ret = SAA_ERR_INVALID_COMMAND;
500 case PVC_ERRORCODE_INVALID_DATA:
501 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
503 ret = SAA_ERR_BAD_PARAMETER;
505 case PVC_ERRORCODE_TIMEOUT:
506 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
507 ret = SAA_ERR_TIMEOUT;
509 case PVC_ERRORCODE_NAK:
510 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
511 ret = SAA_ERR_NULL_PACKET;
513 case PVC_ERRORCODE_UNKNOWN:
514 case PVC_ERRORCODE_INVALID_CONTROL:
516 "%s() UNKNOWN OR INVALID CONTROL\n",
519 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
520 ret = SAA_ERR_NOT_SUPPORTED;
523 /* See of other commands are on the bus */
524 if (saa7164_cmd_dequeue(dev) != SAA_OK)
525 printk(KERN_ERR "dequeue(2) failed\n");
530 /* If response is invalid */
531 if ((presponse_t->id != pcommand_t->id) ||
532 (presponse_t->command != pcommand_t->command) ||
533 (presponse_t->controlselector !=
534 pcommand_t->controlselector) ||
535 (((resp_dsize - data_recd) != presponse_t->size) &&
536 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
537 ((resp_dsize - data_recd) < presponse_t->size)) {
540 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
541 ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
543 printk(KERN_ERR "get failed\n");
547 /* See of other commands are on the bus */
548 if (saa7164_cmd_dequeue(dev) != SAA_OK)
549 printk(KERN_ERR "dequeue(3) failed\n");
553 /* OK, now we're actually getting out correct response */
554 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
556 printk(KERN_ERR "get failed\n");
560 data_recd = presponse_t->size + data_recd;
561 if (resp_dsize == data_recd) {
562 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
566 /* See of other commands are on the bus */
567 if (saa7164_cmd_dequeue(dev) != SAA_OK)
568 printk(KERN_ERR "dequeue(3) failed\n");
574 /* Release the sequence number allocation */
575 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
577 /* if powerdown signal all pending commands */
579 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
581 /* See of other commands are on the bus */
582 if (saa7164_cmd_dequeue(dev) != SAA_OK)
583 printk(KERN_ERR "dequeue(4) failed\n");