saa7164-cmd.c 14 KB

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