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 ret;
  115. u32 timeout;
  116. wait_queue_head_t *q = NULL;
  117. u8 tmp[512];
  118. dprintk(DBGLVL_CMD, "%s()\n", __func__);
  119. while (true) {
  120. struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
  121. ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
  122. if (ret == SAA_ERR_EMPTY)
  123. return SAA_OK;
  124. if (ret != SAA_OK)
  125. return ret;
  126. q = &dev->cmds[tRsp.seqno].wait;
  127. timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
  128. dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
  129. if (timeout) {
  130. printk(KERN_ERR "found timed out command on the bus\n");
  131. /* Clean the bus */
  132. ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
  133. printk(KERN_ERR "ret = %x\n", ret);
  134. if (ret == SAA_ERR_EMPTY)
  135. /* Someone else already fetched the response */
  136. return SAA_OK;
  137. if (ret != SAA_OK)
  138. return ret;
  139. if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
  140. printk(KERN_ERR "split response\n");
  141. else
  142. saa7164_cmd_free_seqno(dev, tRsp.seqno);
  143. printk(KERN_ERR " timeout continue\n");
  144. continue;
  145. }
  146. dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
  147. __func__, tRsp.seqno);
  148. dev->cmds[tRsp.seqno].signalled = 1;
  149. wake_up(q);
  150. return SAA_OK;
  151. }
  152. }
  153. static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
  154. void *buf)
  155. {
  156. struct tmComResBusInfo *bus = &dev->bus;
  157. u8 cmd_sent;
  158. u16 size, idx;
  159. u32 cmds;
  160. void *tmp;
  161. int ret = -1;
  162. if (!msg) {
  163. printk(KERN_ERR "%s() !msg\n", __func__);
  164. return SAA_ERR_BAD_PARAMETER;
  165. }
  166. mutex_lock(&dev->cmds[msg->id].lock);
  167. size = msg->size;
  168. idx = 0;
  169. cmds = size / bus->m_wMaxReqSize;
  170. if (size % bus->m_wMaxReqSize == 0)
  171. cmds -= 1;
  172. cmd_sent = 0;
  173. /* Split the request into smaller chunks */
  174. for (idx = 0; idx < cmds; idx++) {
  175. msg->flags |= SAA_CMDFLAG_CONTINUE;
  176. msg->size = bus->m_wMaxReqSize;
  177. tmp = buf + idx * bus->m_wMaxReqSize;
  178. ret = saa7164_bus_set(dev, msg, tmp);
  179. if (ret != SAA_OK) {
  180. printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
  181. if (cmd_sent) {
  182. ret = SAA_ERR_BUSY;
  183. goto out;
  184. }
  185. ret = SAA_ERR_OVERFLOW;
  186. goto out;
  187. }
  188. cmd_sent = 1;
  189. }
  190. /* If not the last command... */
  191. if (idx != 0)
  192. msg->flags &= ~SAA_CMDFLAG_CONTINUE;
  193. msg->size = size - idx * bus->m_wMaxReqSize;
  194. ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
  195. if (ret != SAA_OK) {
  196. printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
  197. if (cmd_sent) {
  198. ret = SAA_ERR_BUSY;
  199. goto out;
  200. }
  201. ret = SAA_ERR_OVERFLOW;
  202. goto out;
  203. }
  204. ret = SAA_OK;
  205. out:
  206. mutex_unlock(&dev->cmds[msg->id].lock);
  207. return ret;
  208. }
  209. /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
  210. * the event never occurred, or SAA_OK if it was signaled during the wait.
  211. */
  212. static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
  213. {
  214. wait_queue_head_t *q = NULL;
  215. int ret = SAA_BUS_TIMEOUT;
  216. unsigned long stamp;
  217. int r;
  218. if (saa_debug >= 4)
  219. saa7164_bus_dump(dev);
  220. dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
  221. mutex_lock(&dev->lock);
  222. if ((dev->cmds[seqno].inuse == 1) &&
  223. (dev->cmds[seqno].seqno == seqno)) {
  224. q = &dev->cmds[seqno].wait;
  225. }
  226. mutex_unlock(&dev->lock);
  227. if (q) {
  228. /* If we haven't been signalled we need to wait */
  229. if (dev->cmds[seqno].signalled == 0) {
  230. stamp = jiffies;
  231. dprintk(DBGLVL_CMD,
  232. "%s(seqno=%d) Waiting (signalled=%d)\n",
  233. __func__, seqno, dev->cmds[seqno].signalled);
  234. /* Wait for signalled to be flagged or timeout */
  235. /* In a highly stressed system this can easily extend
  236. * into multiple seconds before the deferred worker
  237. * is scheduled, and we're woken up via signal.
  238. * We typically are signalled in < 50ms but it can
  239. * take MUCH longer.
  240. */
  241. wait_event_timeout(*q, dev->cmds[seqno].signalled,
  242. (HZ * waitsecs));
  243. r = time_before(jiffies, stamp + (HZ * waitsecs));
  244. if (r)
  245. ret = SAA_OK;
  246. else
  247. saa7164_cmd_timeout_seqno(dev, seqno);
  248. dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
  249. __func__, seqno, r,
  250. dev->cmds[seqno].signalled);
  251. } else
  252. ret = SAA_OK;
  253. } else
  254. printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
  255. __func__, seqno);
  256. return ret;
  257. }
  258. void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
  259. {
  260. int i;
  261. dprintk(DBGLVL_CMD, "%s()\n", __func__);
  262. mutex_lock(&dev->lock);
  263. for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
  264. if (dev->cmds[i].inuse == 1) {
  265. dprintk(DBGLVL_CMD,
  266. "seqno %d inuse, sig = %d, t/out = %d\n",
  267. dev->cmds[i].seqno,
  268. dev->cmds[i].signalled,
  269. dev->cmds[i].timeout);
  270. }
  271. }
  272. for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
  273. if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
  274. (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
  275. dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
  276. __func__, i);
  277. dev->cmds[i].signalled = 1;
  278. wake_up(&dev->cmds[i].wait);
  279. }
  280. }
  281. mutex_unlock(&dev->lock);
  282. }
  283. int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
  284. u16 controlselector, u16 size, void *buf)
  285. {
  286. struct tmComResInfo command_t, *pcommand_t;
  287. struct tmComResInfo response_t, *presponse_t;
  288. u8 errdata[256];
  289. u16 resp_dsize;
  290. u16 data_recd;
  291. u32 loop;
  292. int ret;
  293. int safety = 0;
  294. dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
  295. __func__, saa7164_unitid_name(dev, id), id,
  296. command, controlselector);
  297. if ((size == 0) || (buf == NULL)) {
  298. printk(KERN_ERR "%s() Invalid param\n", __func__);
  299. return SAA_ERR_BAD_PARAMETER;
  300. }
  301. /* Prepare some basic command/response structures */
  302. memset(&command_t, 0, sizeof(command_t));
  303. memset(&response_t, 0, sizeof(response_t));
  304. pcommand_t = &command_t;
  305. presponse_t = &response_t;
  306. command_t.id = id;
  307. command_t.command = command;
  308. command_t.controlselector = controlselector;
  309. command_t.size = size;
  310. /* Allocate a unique sequence number */
  311. ret = saa7164_cmd_alloc_seqno(dev);
  312. if (ret < 0) {
  313. printk(KERN_ERR "%s() No free sequences\n", __func__);
  314. ret = SAA_ERR_NO_RESOURCES;
  315. goto out;
  316. }
  317. command_t.seqno = (u8)ret;
  318. /* Send Command */
  319. resp_dsize = size;
  320. pcommand_t->size = size;
  321. dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
  322. __func__, pcommand_t->seqno);
  323. dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
  324. __func__, pcommand_t->size);
  325. ret = saa7164_cmd_set(dev, pcommand_t, buf);
  326. if (ret != SAA_OK) {
  327. printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
  328. if (ret != SAA_ERR_BUSY)
  329. saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
  330. else
  331. /* Flag a timeout, because at least one
  332. * command was sent */
  333. saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
  334. goto out;
  335. }
  336. /* With split responses we have to collect the msgs piece by piece */
  337. data_recd = 0;
  338. loop = 1;
  339. while (loop) {
  340. dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
  341. ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
  342. dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
  343. /* if power is down and this is not a power command ... */
  344. if (ret == SAA_BUS_TIMEOUT) {
  345. printk(KERN_ERR "Event timed out\n");
  346. saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
  347. return ret;
  348. }
  349. if (ret != SAA_OK) {
  350. printk(KERN_ERR "spurious error\n");
  351. return ret;
  352. }
  353. /* Peek response */
  354. ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
  355. if (ret == SAA_ERR_EMPTY) {
  356. dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
  357. continue;
  358. }
  359. if (ret != SAA_OK) {
  360. printk(KERN_ERR "peek failed\n");
  361. return ret;
  362. }
  363. dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
  364. __func__, presponse_t->seqno);
  365. dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
  366. __func__, presponse_t->flags);
  367. dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
  368. __func__, presponse_t->size);
  369. /* Check if the response was for our command */
  370. if (presponse_t->seqno != pcommand_t->seqno) {
  371. dprintk(DBGLVL_CMD,
  372. "wrong event: seqno = %d, expected seqno = %d, will dequeue regardless\n",
  373. presponse_t->seqno, pcommand_t->seqno);
  374. ret = saa7164_cmd_dequeue(dev);
  375. if (ret != SAA_OK) {
  376. printk(KERN_ERR "dequeue failed, ret = %d\n",
  377. ret);
  378. if (safety++ > 16) {
  379. printk(KERN_ERR
  380. "dequeue exceeded, safety exit\n");
  381. return SAA_ERR_BUSY;
  382. }
  383. }
  384. continue;
  385. }
  386. if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
  387. memset(&errdata[0], 0, sizeof(errdata));
  388. ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
  389. if (ret != SAA_OK) {
  390. printk(KERN_ERR "get error(2)\n");
  391. return ret;
  392. }
  393. saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
  394. dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
  395. __func__, errdata[0], errdata[1], errdata[2],
  396. errdata[3]);
  397. /* Map error codes */
  398. dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
  399. __func__, errdata[0]);
  400. switch (errdata[0]) {
  401. case PVC_ERRORCODE_INVALID_COMMAND:
  402. dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
  403. __func__);
  404. ret = SAA_ERR_INVALID_COMMAND;
  405. break;
  406. case PVC_ERRORCODE_INVALID_DATA:
  407. dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
  408. __func__);
  409. ret = SAA_ERR_BAD_PARAMETER;
  410. break;
  411. case PVC_ERRORCODE_TIMEOUT:
  412. dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
  413. ret = SAA_ERR_TIMEOUT;
  414. break;
  415. case PVC_ERRORCODE_NAK:
  416. dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
  417. ret = SAA_ERR_NULL_PACKET;
  418. break;
  419. case PVC_ERRORCODE_UNKNOWN:
  420. case PVC_ERRORCODE_INVALID_CONTROL:
  421. dprintk(DBGLVL_CMD,
  422. "%s() UNKNOWN OR INVALID CONTROL\n",
  423. __func__);
  424. ret = SAA_ERR_NOT_SUPPORTED;
  425. break;
  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. }