2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 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 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 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 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 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 = 0;
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, ensure we exit
126 * at some point regardles.
133 /* Commands to the f/w get marshelled to/from this code then onto the PCI
134 * -bus/c running buffer. */
135 int saa7164_cmd_dequeue(struct saa7164_dev *dev)
140 wait_queue_head_t *q = 0;
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 int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo* msg, void *buf)
191 struct tmComResBusInfo *bus = &dev->bus;
199 printk(KERN_ERR "%s() !msg\n", __func__);
200 return SAA_ERR_BAD_PARAMETER;
203 mutex_lock(&dev->cmds[msg->id].lock);
207 cmds = size / bus->m_wMaxReqSize;
208 if (size % bus->m_wMaxReqSize == 0)
213 /* Split the request into smaller chunks */
214 for (idx = 0; idx < cmds; idx++) {
216 msg->flags |= SAA_CMDFLAG_CONTINUE;
217 msg->size = bus->m_wMaxReqSize;
218 tmp = buf + idx * bus->m_wMaxReqSize;
220 ret = saa7164_bus_set(dev, msg, tmp);
222 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
228 ret = SAA_ERR_OVERFLOW;
234 /* If not the last command... */
236 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
238 msg->size = size - idx * bus->m_wMaxReqSize;
240 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
242 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
248 ret = SAA_ERR_OVERFLOW;
254 mutex_unlock(&dev->cmds[msg->id].lock);
258 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
259 * the event never occured, or SAA_OK if it was signaled during the wait.
261 int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
263 wait_queue_head_t *q = 0;
264 int ret = SAA_BUS_TIMEOUT;
269 saa7164_bus_dump(dev);
271 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
273 mutex_lock(&dev->lock);
274 if ((dev->cmds[seqno].inuse == 1) &&
275 (dev->cmds[seqno].seqno == seqno)) {
276 q = &dev->cmds[seqno].wait;
278 mutex_unlock(&dev->lock);
281 /* If we haven't been signalled we need to wait */
282 if (dev->cmds[seqno].signalled == 0) {
285 "%s(seqno=%d) Waiting (signalled=%d)\n",
286 __func__, seqno, dev->cmds[seqno].signalled);
288 /* Wait for signalled to be flagged or timeout */
289 /* In a highly stressed system this can easily extend
290 * into multiple seconds before the deferred worker
291 * is scheduled, and we're woken up via signal.
292 * We typically are signalled in < 50ms but it can
295 wait_event_timeout(*q, dev->cmds[seqno].signalled, (HZ * waitsecs));
296 r = time_before(jiffies, stamp + (HZ * waitsecs));
300 saa7164_cmd_timeout_seqno(dev, seqno);
302 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d "
303 "(signalled=%d)\n", __func__, seqno, r,
304 dev->cmds[seqno].signalled);
308 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
314 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
317 dprintk(DBGLVL_CMD, "%s()\n", __func__);
319 mutex_lock(&dev->lock);
320 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
321 if (dev->cmds[i].inuse == 1) {
323 "seqno %d inuse, sig = %d, t/out = %d\n",
325 dev->cmds[i].signalled,
326 dev->cmds[i].timeout);
330 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
331 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
332 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
333 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
335 dev->cmds[i].signalled = 1;
336 wake_up(&dev->cmds[i].wait);
339 mutex_unlock(&dev->lock);
342 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
343 u16 controlselector, u16 size, void *buf)
345 struct tmComResInfo command_t, *pcommand_t;
346 struct tmComResInfo response_t, *presponse_t;
354 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, "
355 "sel = 0x%x)\n", __func__, saa7164_unitid_name(dev, id), id,
356 command, controlselector);
358 if ((size == 0) || (buf == 0)) {
359 printk(KERN_ERR "%s() Invalid param\n", __func__);
360 return SAA_ERR_BAD_PARAMETER;
363 /* Prepare some basic command/response structures */
364 memset(&command_t, 0, sizeof(command_t));
365 memset(&response_t, 0, sizeof(response_t));
366 pcommand_t = &command_t;
367 presponse_t = &response_t;
369 command_t.command = command;
370 command_t.controlselector = controlselector;
371 command_t.size = size;
373 /* Allocate a unique sequence number */
374 ret = saa7164_cmd_alloc_seqno(dev);
376 printk(KERN_ERR "%s() No free sequences\n", __func__);
377 ret = SAA_ERR_NO_RESOURCES;
381 command_t.seqno = (u8)ret;
385 pcommand_t->size = size;
387 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
388 __func__, pcommand_t->seqno);
390 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
391 __func__, pcommand_t->size);
393 ret = saa7164_cmd_set(dev, pcommand_t, buf);
395 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
397 if (ret != SAA_ERR_BUSY)
398 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
400 /* Flag a timeout, because at least one
401 * command was sent */
402 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
407 /* With split responses we have to collect the msgs piece by piece */
411 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
413 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
414 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
416 /* if power is down and this is not a power command ... */
418 if (ret == SAA_BUS_TIMEOUT) {
419 printk(KERN_ERR "Event timed out\n");
420 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
425 printk(KERN_ERR "spurious error\n");
430 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
431 if (ret == SAA_ERR_EMPTY) {
432 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
436 printk(KERN_ERR "peek failed\n");
440 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
441 __func__, presponse_t->seqno);
443 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
444 __func__, presponse_t->flags);
446 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
447 __func__, presponse_t->size);
449 /* Check if the response was for our command */
450 if (presponse_t->seqno != pcommand_t->seqno) {
453 "wrong event: seqno = %d, "
454 "expected seqno = %d, "
455 "will dequeue regardless\n",
456 presponse_t->seqno, pcommand_t->seqno);
458 ret = saa7164_cmd_dequeue(dev);
460 printk(KERN_ERR "dequeue failed, ret = %d\n",
464 "dequeue exceeded, safety exit\n");
472 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
474 memset(&errdata[0], 0, sizeof(errdata));
476 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
478 printk(KERN_ERR "get error(2)\n");
482 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
484 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
485 __func__, errdata[0], errdata[1], errdata[2],
488 /* Map error codes */
489 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
490 __func__, errdata[0]);
492 switch (errdata[0]) {
493 case PVC_ERRORCODE_INVALID_COMMAND:
494 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
496 ret = SAA_ERR_INVALID_COMMAND;
498 case PVC_ERRORCODE_INVALID_DATA:
499 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
501 ret = SAA_ERR_BAD_PARAMETER;
503 case PVC_ERRORCODE_TIMEOUT:
504 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
505 ret = SAA_ERR_TIMEOUT;
507 case PVC_ERRORCODE_NAK:
508 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
509 ret = SAA_ERR_NULL_PACKET;
511 case PVC_ERRORCODE_UNKNOWN:
512 case PVC_ERRORCODE_INVALID_CONTROL:
514 "%s() UNKNOWN OR INVALID CONTROL\n",
517 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
518 ret = SAA_ERR_NOT_SUPPORTED;
521 /* See of other commands are on the bus */
522 if (saa7164_cmd_dequeue(dev) != SAA_OK)
523 printk(KERN_ERR "dequeue(2) failed\n");
528 /* If response is invalid */
529 if ((presponse_t->id != pcommand_t->id) ||
530 (presponse_t->command != pcommand_t->command) ||
531 (presponse_t->controlselector !=
532 pcommand_t->controlselector) ||
533 (((resp_dsize - data_recd) != presponse_t->size) &&
534 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
535 ((resp_dsize - data_recd) < presponse_t->size)) {
538 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
539 ret = saa7164_bus_get(dev, presponse_t, 0, 0);
541 printk(KERN_ERR "get failed\n");
545 /* See of other commands are on the bus */
546 if (saa7164_cmd_dequeue(dev) != SAA_OK)
547 printk(KERN_ERR "dequeue(3) failed\n");
551 /* OK, now we're actually getting out correct response */
552 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
554 printk(KERN_ERR "get failed\n");
558 data_recd = presponse_t->size + data_recd;
559 if (resp_dsize == data_recd) {
560 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
564 /* See of other commands are on the bus */
565 if (saa7164_cmd_dequeue(dev) != SAA_OK)
566 printk(KERN_ERR "dequeue(3) failed\n");
572 /* Release the sequence number allocation */
573 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
575 /* if powerdown signal all pending commands */
577 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
579 /* See of other commands are on the bus */
580 if (saa7164_cmd_dequeue(dev) != SAA_OK)
581 printk(KERN_ERR "dequeue(4) failed\n");