saa7164-cmd.c 15 KB

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