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