xref: /linux/drivers/media/pci/saa7164/saa7164-cmd.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
5  *
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.
10  *
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
14  *
15  *  GNU General Public License for more details.
16  *
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.
20  */
21 
22 #include <linux/wait.h>
23 
24 #include "saa7164.h"
25 
26 static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
27 {
28 	int i, ret = -1;
29 
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;
37 			break;
38 		}
39 	}
40 	mutex_unlock(&dev->lock);
41 
42 	return ret;
43 }
44 
45 static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
46 {
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;
53 	}
54 	mutex_unlock(&dev->lock);
55 }
56 
57 static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
58 {
59 	mutex_lock(&dev->lock);
60 	if ((dev->cmds[seqno].inuse == 1) &&
61 		(dev->cmds[seqno].seqno == seqno)) {
62 		dev->cmds[seqno].timeout = 1;
63 	}
64 	mutex_unlock(&dev->lock);
65 }
66 
67 static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
68 {
69 	int ret = 0;
70 
71 	mutex_lock(&dev->lock);
72 	if ((dev->cmds[seqno].inuse == 1) &&
73 		(dev->cmds[seqno].seqno == seqno)) {
74 		ret = dev->cmds[seqno].timeout;
75 	}
76 	mutex_unlock(&dev->lock);
77 
78 	return ret;
79 }
80 
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)
84 {
85 	int ret = SAA_OK, i = 0;
86 	u32 timeout;
87 	wait_queue_head_t *q = NULL;
88 	u8 tmp[512];
89 	dprintk(DBGLVL_CMD, "%s()\n", __func__);
90 
91 	/* While any outstand message on the bus exists... */
92 	do {
93 
94 		/* Peek the msg bus */
95 		struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
96 		ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
97 		if (ret != SAA_OK)
98 			break;
99 
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);
103 		if (!timeout) {
104 			dprintk(DBGLVL_CMD,
105 				"%s() signalled seqno(%d) (for dequeue)\n",
106 				__func__, tRsp.seqno);
107 			dev->cmds[tRsp.seqno].signalled = 1;
108 			wake_up(q);
109 		} else {
110 			printk(KERN_ERR
111 				"%s() found timed out command on the bus\n",
112 					__func__);
113 
114 			/* Clean the bus */
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 */
119 				return SAA_OK;
120 
121 			if (ret != SAA_OK)
122 				return ret;
123 		}
124 
125 		/* It's unlikely to have more than 4 or 5 pending messages,
126 		 * ensure we exit at some point regardless.
127 		 */
128 	} while (i++ < 32);
129 
130 	return ret;
131 }
132 
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)
136 {
137 	int loop = 1;
138 	int ret;
139 	u32 timeout;
140 	wait_queue_head_t *q = NULL;
141 	u8 tmp[512];
142 	dprintk(DBGLVL_CMD, "%s()\n", __func__);
143 
144 	while (loop) {
145 
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)
149 			return SAA_OK;
150 
151 		if (ret != SAA_OK)
152 			return ret;
153 
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);
157 		if (timeout) {
158 			printk(KERN_ERR "found timed out command on the bus\n");
159 
160 			/* Clean the bus */
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 */
165 				return SAA_OK;
166 
167 			if (ret != SAA_OK)
168 				return ret;
169 
170 			if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
171 				printk(KERN_ERR "split response\n");
172 			else
173 				saa7164_cmd_free_seqno(dev, tRsp.seqno);
174 
175 			printk(KERN_ERR " timeout continue\n");
176 			continue;
177 		}
178 
179 		dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
180 			__func__, tRsp.seqno);
181 		dev->cmds[tRsp.seqno].signalled = 1;
182 		wake_up(q);
183 		return SAA_OK;
184 	}
185 
186 	return SAA_OK;
187 }
188 
189 static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
190 			   void *buf)
191 {
192 	struct tmComResBusInfo *bus = &dev->bus;
193 	u8 cmd_sent;
194 	u16 size, idx;
195 	u32 cmds;
196 	void *tmp;
197 	int ret = -1;
198 
199 	if (!msg) {
200 		printk(KERN_ERR "%s() !msg\n", __func__);
201 		return SAA_ERR_BAD_PARAMETER;
202 	}
203 
204 	mutex_lock(&dev->cmds[msg->id].lock);
205 
206 	size = msg->size;
207 	idx = 0;
208 	cmds = size / bus->m_wMaxReqSize;
209 	if (size % bus->m_wMaxReqSize == 0)
210 		cmds -= 1;
211 
212 	cmd_sent = 0;
213 
214 	/* Split the request into smaller chunks */
215 	for (idx = 0; idx < cmds; idx++) {
216 
217 		msg->flags |= SAA_CMDFLAG_CONTINUE;
218 		msg->size = bus->m_wMaxReqSize;
219 		tmp = buf + idx * bus->m_wMaxReqSize;
220 
221 		ret = saa7164_bus_set(dev, msg, tmp);
222 		if (ret != SAA_OK) {
223 			printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
224 
225 			if (cmd_sent) {
226 				ret = SAA_ERR_BUSY;
227 				goto out;
228 			}
229 			ret = SAA_ERR_OVERFLOW;
230 			goto out;
231 		}
232 		cmd_sent = 1;
233 	}
234 
235 	/* If not the last command... */
236 	if (idx != 0)
237 		msg->flags &= ~SAA_CMDFLAG_CONTINUE;
238 
239 	msg->size = size - idx * bus->m_wMaxReqSize;
240 
241 	ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
242 	if (ret != SAA_OK) {
243 		printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
244 
245 		if (cmd_sent) {
246 			ret = SAA_ERR_BUSY;
247 			goto out;
248 		}
249 		ret = SAA_ERR_OVERFLOW;
250 		goto out;
251 	}
252 	ret = SAA_OK;
253 
254 out:
255 	mutex_unlock(&dev->cmds[msg->id].lock);
256 	return ret;
257 }
258 
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.
261  */
262 static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
263 {
264 	wait_queue_head_t *q = NULL;
265 	int ret = SAA_BUS_TIMEOUT;
266 	unsigned long stamp;
267 	int r;
268 
269 	if (saa_debug >= 4)
270 		saa7164_bus_dump(dev);
271 
272 	dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
273 
274 	mutex_lock(&dev->lock);
275 	if ((dev->cmds[seqno].inuse == 1) &&
276 		(dev->cmds[seqno].seqno == seqno)) {
277 		q = &dev->cmds[seqno].wait;
278 	}
279 	mutex_unlock(&dev->lock);
280 
281 	if (q) {
282 		/* If we haven't been signalled we need to wait */
283 		if (dev->cmds[seqno].signalled == 0) {
284 			stamp = jiffies;
285 			dprintk(DBGLVL_CMD,
286 				"%s(seqno=%d) Waiting (signalled=%d)\n",
287 				__func__, seqno, dev->cmds[seqno].signalled);
288 
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
294 			 * take MUCH longer.
295 			 */
296 			wait_event_timeout(*q, dev->cmds[seqno].signalled,
297 				(HZ * waitsecs));
298 			r = time_before(jiffies, stamp + (HZ * waitsecs));
299 			if (r)
300 				ret = SAA_OK;
301 			else
302 				saa7164_cmd_timeout_seqno(dev, seqno);
303 
304 			dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d "
305 				"(signalled=%d)\n", __func__, seqno, r,
306 				dev->cmds[seqno].signalled);
307 		} else
308 			ret = SAA_OK;
309 	} else
310 		printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
311 			__func__, seqno);
312 
313 	return ret;
314 }
315 
316 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
317 {
318 	int i;
319 	dprintk(DBGLVL_CMD, "%s()\n", __func__);
320 
321 	mutex_lock(&dev->lock);
322 	for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
323 		if (dev->cmds[i].inuse == 1) {
324 			dprintk(DBGLVL_CMD,
325 				"seqno %d inuse, sig = %d, t/out = %d\n",
326 				dev->cmds[i].seqno,
327 				dev->cmds[i].signalled,
328 				dev->cmds[i].timeout);
329 		}
330 	}
331 
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",
336 				__func__, i);
337 			dev->cmds[i].signalled = 1;
338 			wake_up(&dev->cmds[i].wait);
339 		}
340 	}
341 	mutex_unlock(&dev->lock);
342 }
343 
344 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
345 	u16 controlselector, u16 size, void *buf)
346 {
347 	struct tmComResInfo command_t, *pcommand_t;
348 	struct tmComResInfo response_t, *presponse_t;
349 	u8 errdata[256];
350 	u16 resp_dsize;
351 	u16 data_recd;
352 	u32 loop;
353 	int ret;
354 	int safety = 0;
355 
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);
359 
360 	if ((size == 0) || (buf == NULL)) {
361 		printk(KERN_ERR "%s() Invalid param\n", __func__);
362 		return SAA_ERR_BAD_PARAMETER;
363 	}
364 
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;
370 	command_t.id = id;
371 	command_t.command = command;
372 	command_t.controlselector = controlselector;
373 	command_t.size = size;
374 
375 	/* Allocate a unique sequence number */
376 	ret = saa7164_cmd_alloc_seqno(dev);
377 	if (ret < 0) {
378 		printk(KERN_ERR "%s() No free sequences\n", __func__);
379 		ret = SAA_ERR_NO_RESOURCES;
380 		goto out;
381 	}
382 
383 	command_t.seqno = (u8)ret;
384 
385 	/* Send Command */
386 	resp_dsize = size;
387 	pcommand_t->size = size;
388 
389 	dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
390 		__func__, pcommand_t->seqno);
391 
392 	dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
393 		__func__, pcommand_t->size);
394 
395 	ret = saa7164_cmd_set(dev, pcommand_t, buf);
396 	if (ret != SAA_OK) {
397 		printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
398 
399 		if (ret != SAA_ERR_BUSY)
400 			saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
401 		else
402 			/* Flag a timeout, because at least one
403 			 * command was sent */
404 			saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
405 
406 		goto out;
407 	}
408 
409 	/* With split responses we have to collect the msgs piece by piece */
410 	data_recd = 0;
411 	loop = 1;
412 	while (loop) {
413 		dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
414 
415 		ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
416 		dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
417 
418 		/* if power is down and this is not a power command ... */
419 
420 		if (ret == SAA_BUS_TIMEOUT) {
421 			printk(KERN_ERR "Event timed out\n");
422 			saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
423 			return ret;
424 		}
425 
426 		if (ret != SAA_OK) {
427 			printk(KERN_ERR "spurious error\n");
428 			return ret;
429 		}
430 
431 		/* Peek response */
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__);
435 			continue;
436 		}
437 		if (ret != SAA_OK) {
438 			printk(KERN_ERR "peek failed\n");
439 			return ret;
440 		}
441 
442 		dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
443 			__func__, presponse_t->seqno);
444 
445 		dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
446 			__func__, presponse_t->flags);
447 
448 		dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
449 			__func__, presponse_t->size);
450 
451 		/* Check if the response was for our command */
452 		if (presponse_t->seqno != pcommand_t->seqno) {
453 
454 			dprintk(DBGLVL_CMD,
455 				"wrong event: seqno = %d, "
456 				"expected seqno = %d, "
457 				"will dequeue regardless\n",
458 				presponse_t->seqno, pcommand_t->seqno);
459 
460 			ret = saa7164_cmd_dequeue(dev);
461 			if (ret != SAA_OK) {
462 				printk(KERN_ERR "dequeue failed, ret = %d\n",
463 					ret);
464 				if (safety++ > 16) {
465 					printk(KERN_ERR
466 					"dequeue exceeded, safety exit\n");
467 					return SAA_ERR_BUSY;
468 				}
469 			}
470 
471 			continue;
472 		}
473 
474 		if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
475 
476 			memset(&errdata[0], 0, sizeof(errdata));
477 
478 			ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
479 			if (ret != SAA_OK) {
480 				printk(KERN_ERR "get error(2)\n");
481 				return ret;
482 			}
483 
484 			saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
485 
486 			dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
487 				__func__, errdata[0], errdata[1], errdata[2],
488 				errdata[3]);
489 
490 			/* Map error codes */
491 			dprintk(DBGLVL_CMD, "%s() cmd, error code  = 0x%x\n",
492 				__func__, errdata[0]);
493 
494 			switch (errdata[0]) {
495 			case PVC_ERRORCODE_INVALID_COMMAND:
496 				dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
497 					__func__);
498 				ret = SAA_ERR_INVALID_COMMAND;
499 				break;
500 			case PVC_ERRORCODE_INVALID_DATA:
501 				dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
502 					__func__);
503 				ret = SAA_ERR_BAD_PARAMETER;
504 				break;
505 			case PVC_ERRORCODE_TIMEOUT:
506 				dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
507 				ret = SAA_ERR_TIMEOUT;
508 				break;
509 			case PVC_ERRORCODE_NAK:
510 				dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
511 				ret = SAA_ERR_NULL_PACKET;
512 				break;
513 			case PVC_ERRORCODE_UNKNOWN:
514 			case PVC_ERRORCODE_INVALID_CONTROL:
515 				dprintk(DBGLVL_CMD,
516 					"%s() UNKNOWN OR INVALID CONTROL\n",
517 					__func__);
518 			default:
519 				dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
520 				ret = SAA_ERR_NOT_SUPPORTED;
521 			}
522 
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");
526 
527 			return ret;
528 		}
529 
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)) {
538 
539 			/* Invalid */
540 			dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
541 			ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
542 			if (ret != SAA_OK) {
543 				printk(KERN_ERR "get failed\n");
544 				return ret;
545 			}
546 
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");
550 			continue;
551 		}
552 
553 		/* OK, now we're actually getting out correct response */
554 		ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
555 		if (ret != SAA_OK) {
556 			printk(KERN_ERR "get failed\n");
557 			return ret;
558 		}
559 
560 		data_recd = presponse_t->size + data_recd;
561 		if (resp_dsize == data_recd) {
562 			dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
563 			break;
564 		}
565 
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");
569 
570 		continue;
571 
572 	} /* (loop) */
573 
574 	/* Release the sequence number allocation */
575 	saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
576 
577 	/* if powerdown signal all pending commands */
578 
579 	dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
580 
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");
584 
585 	ret = SAA_OK;
586 out:
587 	return ret;
588 }
589 
590