saa7164-cmd.c 14 KB

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