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