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
);
191 cmds
= size
/ bus
->m_wMaxReqSize
;
192 if (size
% bus
->m_wMaxReqSize
== 0)
197 /* Split the request into smaller chunks */
198 for (idx
= 0; idx
< cmds
; idx
++) {
200 msg
->flags
|= SAA_CMDFLAG_CONTINUE
;
201 msg
->size
= bus
->m_wMaxReqSize
;
202 tmp
= buf
+ idx
* bus
->m_wMaxReqSize
;
204 ret
= saa7164_bus_set(dev
, msg
, tmp
);
206 printk(KERN_ERR
"%s() set failed %d\n", __func__
, ret
);
212 ret
= SAA_ERR_OVERFLOW
;
218 /* If not the last command... */
220 msg
->flags
&= ~SAA_CMDFLAG_CONTINUE
;
222 msg
->size
= size
- idx
* bus
->m_wMaxReqSize
;
224 ret
= saa7164_bus_set(dev
, msg
, buf
+ idx
* bus
->m_wMaxReqSize
);
226 printk(KERN_ERR
"%s() set last failed %d\n", __func__
, ret
);
232 ret
= SAA_ERR_OVERFLOW
;
238 mutex_unlock(&dev
->cmds
[msg
->id
].lock
);
242 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
243 * the event never occurred, or SAA_OK if it was signaled during the wait.
245 static int saa7164_cmd_wait(struct saa7164_dev
*dev
, u8 seqno
)
247 wait_queue_head_t
*q
= NULL
;
248 int ret
= SAA_BUS_TIMEOUT
;
253 saa7164_bus_dump(dev
);
255 dprintk(DBGLVL_CMD
, "%s(seqno=%d)\n", __func__
, seqno
);
257 mutex_lock(&dev
->lock
);
258 if ((dev
->cmds
[seqno
].inuse
== 1) &&
259 (dev
->cmds
[seqno
].seqno
== seqno
)) {
260 q
= &dev
->cmds
[seqno
].wait
;
262 mutex_unlock(&dev
->lock
);
265 /* If we haven't been signalled we need to wait */
266 if (dev
->cmds
[seqno
].signalled
== 0) {
269 "%s(seqno=%d) Waiting (signalled=%d)\n",
270 __func__
, seqno
, dev
->cmds
[seqno
].signalled
);
272 /* Wait for signalled to be flagged or timeout */
273 /* In a highly stressed system this can easily extend
274 * into multiple seconds before the deferred worker
275 * is scheduled, and we're woken up via signal.
276 * We typically are signalled in < 50ms but it can
279 wait_event_timeout(*q
, dev
->cmds
[seqno
].signalled
,
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 (signalled=%d)\n",
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
, enum tmComResCmd command
,
328 u16 controlselector
, u16 size
, void *buf
)
330 struct tmComResInfo command_t
, *pcommand_t
;
331 struct tmComResInfo response_t
, *presponse_t
;
339 dprintk(DBGLVL_CMD
, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
340 __func__
, saa7164_unitid_name(dev
, id
), id
,
341 command
, controlselector
);
343 if ((size
== 0) || (buf
== NULL
)) {
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, expected seqno = %d, will dequeue regardless\n",
439 presponse_t
->seqno
, pcommand_t
->seqno
);
441 ret
= saa7164_cmd_dequeue(dev
);
443 printk(KERN_ERR
"dequeue failed, ret = %d\n",
447 "dequeue exceeded, safety exit\n");
455 if ((presponse_t
->flags
& PVC_RESPONSEFLAG_ERROR
) != 0) {
457 memset(&errdata
[0], 0, sizeof(errdata
));
459 ret
= saa7164_bus_get(dev
, presponse_t
, &errdata
[0], 0);
461 printk(KERN_ERR
"get error(2)\n");
465 saa7164_cmd_free_seqno(dev
, pcommand_t
->seqno
);
467 dprintk(DBGLVL_CMD
, "%s() errdata %02x%02x%02x%02x\n",
468 __func__
, errdata
[0], errdata
[1], errdata
[2],
471 /* Map error codes */
472 dprintk(DBGLVL_CMD
, "%s() cmd, error code = 0x%x\n",
473 __func__
, errdata
[0]);
475 switch (errdata
[0]) {
476 case PVC_ERRORCODE_INVALID_COMMAND
:
477 dprintk(DBGLVL_CMD
, "%s() INVALID_COMMAND\n",
479 ret
= SAA_ERR_INVALID_COMMAND
;
481 case PVC_ERRORCODE_INVALID_DATA
:
482 dprintk(DBGLVL_CMD
, "%s() INVALID_DATA\n",
484 ret
= SAA_ERR_BAD_PARAMETER
;
486 case PVC_ERRORCODE_TIMEOUT
:
487 dprintk(DBGLVL_CMD
, "%s() TIMEOUT\n", __func__
);
488 ret
= SAA_ERR_TIMEOUT
;
490 case PVC_ERRORCODE_NAK
:
491 dprintk(DBGLVL_CMD
, "%s() NAK\n", __func__
);
492 ret
= SAA_ERR_NULL_PACKET
;
494 case PVC_ERRORCODE_UNKNOWN
:
495 case PVC_ERRORCODE_INVALID_CONTROL
:
497 "%s() UNKNOWN OR INVALID CONTROL\n",
499 ret
= SAA_ERR_NOT_SUPPORTED
;
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
, NULL
, 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");