2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 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
)
87 wait_queue_head_t
*q
= 0;
88 dprintk(DBGLVL_CMD
, "%s()\n", __func__
);
90 /* While any outstand message on the bus exists... */
93 /* Peek the msg bus */
94 tmComResInfo_t tRsp
= { 0, 0, 0, 0, 0, 0 };
95 ret
= saa7164_bus_get(dev
, &tRsp
, NULL
, 1);
99 q
= &dev
->cmds
[tRsp
.seqno
].wait
;
100 timeout
= saa7164_cmd_timeout_get(dev
, tRsp
.seqno
);
101 dprintk(DBGLVL_CMD
, "%s() timeout = %d\n", __func__
, timeout
);
104 "%s() signalled seqno(%d) (for dequeue)\n",
105 __func__
, tRsp
.seqno
);
106 dev
->cmds
[tRsp
.seqno
].signalled
= 1;
110 "%s() found timed out command on the bus\n",
118 /* Commands to the f/w get marshelled to/from this code then onto the PCI
119 * -bus/c running buffer. */
120 int saa7164_cmd_dequeue(struct saa7164_dev
*dev
)
125 wait_queue_head_t
*q
= 0;
127 dprintk(DBGLVL_CMD
, "%s()\n", __func__
);
131 tmComResInfo_t 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;
174 int saa7164_cmd_set(struct saa7164_dev
*dev
, tmComResInfo_t
* msg
, void *buf
)
176 tmComResBusInfo_t
*bus
= &dev
->bus
;
184 printk(KERN_ERR
"%s() !msg\n", __func__
);
185 return SAA_ERR_BAD_PARAMETER
;
188 mutex_lock(&dev
->cmds
[msg
->id
].lock
);
192 cmds
= size
/ bus
->m_wMaxReqSize
;
193 if (size
% bus
->m_wMaxReqSize
== 0)
198 /* Split the request into smaller chunks */
199 for (idx
= 0; idx
< cmds
; idx
++) {
201 msg
->flags
|= SAA_CMDFLAG_CONTINUE
;
202 msg
->size
= bus
->m_wMaxReqSize
;
203 tmp
= buf
+ idx
* bus
->m_wMaxReqSize
;
205 ret
= saa7164_bus_set(dev
, msg
, tmp
);
207 printk(KERN_ERR
"%s() set failed %d\n", __func__
, ret
);
213 ret
= SAA_ERR_OVERFLOW
;
219 /* If not the last command... */
221 msg
->flags
&= ~SAA_CMDFLAG_CONTINUE
;
223 msg
->size
= size
- idx
* bus
->m_wMaxReqSize
;
225 ret
= saa7164_bus_set(dev
, msg
, buf
+ idx
* bus
->m_wMaxReqSize
);
227 printk(KERN_ERR
"%s() set last failed %d\n", __func__
, ret
);
233 ret
= SAA_ERR_OVERFLOW
;
239 mutex_unlock(&dev
->cmds
[msg
->id
].lock
);
243 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
244 * the event never occured, or SAA_OK if it was signaled during the wait.
246 int saa7164_cmd_wait(struct saa7164_dev
*dev
, u8 seqno
)
248 wait_queue_head_t
*q
= 0;
249 int ret
= SAA_BUS_TIMEOUT
;
254 saa7164_bus_dump(dev
);
256 dprintk(DBGLVL_CMD
, "%s(seqno=%d)\n", __func__
, seqno
);
258 mutex_lock(&dev
->lock
);
259 if ((dev
->cmds
[seqno
].inuse
== 1) &&
260 (dev
->cmds
[seqno
].seqno
== seqno
)) {
261 q
= &dev
->cmds
[seqno
].wait
;
263 mutex_unlock(&dev
->lock
);
266 /* If we haven't been signalled we need to wait */
267 if (dev
->cmds
[seqno
].signalled
== 0) {
270 "%s(seqno=%d) Waiting (signalled=%d)\n",
271 __func__
, seqno
, dev
->cmds
[seqno
].signalled
);
273 /* Wait for signalled to be flagged or timeout */
274 /* In a highly stressed system this can easily extend
275 * into multiple seconds before the deferred worker
276 * is scheduled, and we're woken up via signal.
277 * We typically are signalled in < 50ms but it can
280 wait_event_timeout(*q
, dev
->cmds
[seqno
].signalled
, (HZ
* waitsecs
));
281 r
= time_before(jiffies
, stamp
+ (HZ
* waitsecs
));
285 saa7164_cmd_timeout_seqno(dev
, seqno
);
287 dprintk(DBGLVL_CMD
, "%s(seqno=%d) Waiting res = %d "
288 "(signalled=%d)\n", __func__
, seqno
, r
,
289 dev
->cmds
[seqno
].signalled
);
293 printk(KERN_ERR
"%s(seqno=%d) seqno is invalid\n",
299 void saa7164_cmd_signal(struct saa7164_dev
*dev
, u8 seqno
)
302 dprintk(DBGLVL_CMD
, "%s()\n", __func__
);
304 mutex_lock(&dev
->lock
);
305 for (i
= 0; i
< SAA_CMD_MAX_MSG_UNITS
; i
++) {
306 if (dev
->cmds
[i
].inuse
== 1) {
308 "seqno %d inuse, sig = %d, t/out = %d\n",
310 dev
->cmds
[i
].signalled
,
311 dev
->cmds
[i
].timeout
);
315 for (i
= 0; i
< SAA_CMD_MAX_MSG_UNITS
; i
++) {
316 if ((dev
->cmds
[i
].inuse
== 1) && ((i
== 0) ||
317 (dev
->cmds
[i
].signalled
) || (dev
->cmds
[i
].timeout
))) {
318 dprintk(DBGLVL_CMD
, "%s(seqno=%d) calling wake_up\n",
320 dev
->cmds
[i
].signalled
= 1;
321 wake_up(&dev
->cmds
[i
].wait
);
324 mutex_unlock(&dev
->lock
);
327 int saa7164_cmd_send(struct saa7164_dev
*dev
, u8 id
, tmComResCmd_t command
,
328 u16 controlselector
, u16 size
, void *buf
)
330 tmComResInfo_t command_t
, *pcommand_t
;
331 tmComResInfo_t response_t
, *presponse_t
;
339 dprintk(DBGLVL_CMD
, "%s(unitid = %s (%d) , command = 0x%x, "
340 "sel = 0x%x)\n", __func__
, saa7164_unitid_name(dev
, id
), id
,
341 command
, controlselector
);
343 if ((size
== 0) || (buf
== 0)) {
344 printk(KERN_ERR
"%s() Invalid param\n", __func__
);
345 return SAA_ERR_BAD_PARAMETER
;
348 /* Prepare some basic command/response structures */
349 memset(&command_t
, 0, sizeof(command_t
));
350 memset(&response_t
, 0, sizeof(response_t
));
351 pcommand_t
= &command_t
;
352 presponse_t
= &response_t
;
354 command_t
.command
= command
;
355 command_t
.controlselector
= controlselector
;
356 command_t
.size
= size
;
358 /* Allocate a unique sequence number */
359 ret
= saa7164_cmd_alloc_seqno(dev
);
361 printk(KERN_ERR
"%s() No free sequences\n", __func__
);
362 ret
= SAA_ERR_NO_RESOURCES
;
366 command_t
.seqno
= (u8
)ret
;
370 pcommand_t
->size
= size
;
372 dprintk(DBGLVL_CMD
, "%s() pcommand_t.seqno = %d\n",
373 __func__
, pcommand_t
->seqno
);
375 dprintk(DBGLVL_CMD
, "%s() pcommand_t.size = %d\n",
376 __func__
, pcommand_t
->size
);
378 ret
= saa7164_cmd_set(dev
, pcommand_t
, buf
);
380 printk(KERN_ERR
"%s() set command failed %d\n", __func__
, ret
);
382 if (ret
!= SAA_ERR_BUSY
)
383 saa7164_cmd_free_seqno(dev
, pcommand_t
->seqno
);
385 /* Flag a timeout, because at least one
386 * command was sent */
387 saa7164_cmd_timeout_seqno(dev
, pcommand_t
->seqno
);
392 /* With split responses we have to collect the msgs piece by piece */
396 dprintk(DBGLVL_CMD
, "%s() loop\n", __func__
);
398 ret
= saa7164_cmd_wait(dev
, pcommand_t
->seqno
);
399 dprintk(DBGLVL_CMD
, "%s() loop ret = %d\n", __func__
, ret
);
401 /* if power is down and this is not a power command ... */
403 if (ret
== SAA_BUS_TIMEOUT
) {
404 printk(KERN_ERR
"Event timed out\n");
405 saa7164_cmd_timeout_seqno(dev
, pcommand_t
->seqno
);
410 printk(KERN_ERR
"spurious error\n");
415 ret
= saa7164_bus_get(dev
, presponse_t
, NULL
, 1);
416 if (ret
== SAA_ERR_EMPTY
) {
417 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__
);
421 printk(KERN_ERR
"peek failed\n");
425 dprintk(DBGLVL_CMD
, "%s() presponse_t->seqno = %d\n",
426 __func__
, presponse_t
->seqno
);
428 dprintk(DBGLVL_CMD
, "%s() presponse_t->flags = 0x%x\n",
429 __func__
, presponse_t
->flags
);
431 dprintk(DBGLVL_CMD
, "%s() presponse_t->size = %d\n",
432 __func__
, presponse_t
->size
);
434 /* Check if the response was for our command */
435 if (presponse_t
->seqno
!= pcommand_t
->seqno
) {
438 "wrong event: seqno = %d, "
439 "expected seqno = %d, "
440 "will dequeue regardless\n",
441 presponse_t
->seqno
, pcommand_t
->seqno
);
443 ret
= saa7164_cmd_dequeue(dev
);
445 printk(KERN_ERR
"dequeue failed, ret = %d\n",
449 "dequeue exceeded, safety exit\n");
457 if ((presponse_t
->flags
& PVC_RESPONSEFLAG_ERROR
) != 0) {
459 memset(&errdata
[0], 0, sizeof(errdata
));
461 ret
= saa7164_bus_get(dev
, presponse_t
, &errdata
[0], 0);
463 printk(KERN_ERR
"get error(2)\n");
467 saa7164_cmd_free_seqno(dev
, pcommand_t
->seqno
);
469 dprintk(DBGLVL_CMD
, "%s() errdata %02x%02x%02x%02x\n",
470 __func__
, errdata
[0], errdata
[1], errdata
[2],
473 /* Map error codes */
474 dprintk(DBGLVL_CMD
, "%s() cmd, error code = 0x%x\n",
475 __func__
, errdata
[0]);
477 switch (errdata
[0]) {
478 case PVC_ERRORCODE_INVALID_COMMAND
:
479 dprintk(DBGLVL_CMD
, "%s() INVALID_COMMAND\n",
481 ret
= SAA_ERR_INVALID_COMMAND
;
483 case PVC_ERRORCODE_INVALID_DATA
:
484 dprintk(DBGLVL_CMD
, "%s() INVALID_DATA\n",
486 ret
= SAA_ERR_BAD_PARAMETER
;
488 case PVC_ERRORCODE_TIMEOUT
:
489 dprintk(DBGLVL_CMD
, "%s() TIMEOUT\n", __func__
);
490 ret
= SAA_ERR_TIMEOUT
;
492 case PVC_ERRORCODE_NAK
:
493 dprintk(DBGLVL_CMD
, "%s() NAK\n", __func__
);
494 ret
= SAA_ERR_NULL_PACKET
;
496 case PVC_ERRORCODE_UNKNOWN
:
497 case PVC_ERRORCODE_INVALID_CONTROL
:
499 "%s() UNKNOWN OR INVALID CONTROL\n",
502 dprintk(DBGLVL_CMD
, "%s() UNKNOWN\n", __func__
);
503 ret
= SAA_ERR_NOT_SUPPORTED
;
506 /* See of other commands are on the bus */
507 if (saa7164_cmd_dequeue(dev
) != SAA_OK
)
508 printk(KERN_ERR
"dequeue(2) failed\n");
513 /* If response is invalid */
514 if ((presponse_t
->id
!= pcommand_t
->id
) ||
515 (presponse_t
->command
!= pcommand_t
->command
) ||
516 (presponse_t
->controlselector
!=
517 pcommand_t
->controlselector
) ||
518 (((resp_dsize
- data_recd
) != presponse_t
->size
) &&
519 !(presponse_t
->flags
& PVC_CMDFLAG_CONTINUE
)) ||
520 ((resp_dsize
- data_recd
) < presponse_t
->size
)) {
523 dprintk(DBGLVL_CMD
, "%s() Invalid\n", __func__
);
524 ret
= saa7164_bus_get(dev
, presponse_t
, 0, 0);
526 printk(KERN_ERR
"get failed\n");
530 /* See of other commands are on the bus */
531 if (saa7164_cmd_dequeue(dev
) != SAA_OK
)
532 printk(KERN_ERR
"dequeue(3) failed\n");
536 /* OK, now we're actually getting out correct response */
537 ret
= saa7164_bus_get(dev
, presponse_t
, buf
+ data_recd
, 0);
539 printk(KERN_ERR
"get failed\n");
543 data_recd
= presponse_t
->size
+ data_recd
;
544 if (resp_dsize
== data_recd
) {
545 dprintk(DBGLVL_CMD
, "%s() Resp recd\n", __func__
);
549 /* See of other commands are on the bus */
550 if (saa7164_cmd_dequeue(dev
) != SAA_OK
)
551 printk(KERN_ERR
"dequeue(3) failed\n");
557 /* Release the sequence number allocation */
558 saa7164_cmd_free_seqno(dev
, pcommand_t
->seqno
);
560 /* if powerdown signal all pending commands */
562 dprintk(DBGLVL_CMD
, "%s() Calling dequeue then exit\n", __func__
);
564 /* See of other commands are on the bus */
565 if (saa7164_cmd_dequeue(dev
) != SAA_OK
)
566 printk(KERN_ERR
"dequeue(4) failed\n");