saa7164-cmd.c 14 KB

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