File Index
Files and directories
Sign in
FILE

saa7164-cmd.c

File preview with syntax highlighting.

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