cregs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Filename: cregs.c
  3. *
  4. *
  5. * Authors: Joshua Morris <josh.h.morris@us.ibm.com>
  6. * Philip Kelleher <pjk1939@linux.vnet.ibm.com>
  7. *
  8. * (C) Copyright 2013 IBM Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/completion.h>
  25. #include <linux/slab.h>
  26. #include "rsxx_priv.h"
  27. #define CREG_TIMEOUT_MSEC 10000
  28. typedef void (*creg_cmd_cb)(struct rsxx_cardinfo *card,
  29. struct creg_cmd *cmd,
  30. int st);
  31. struct creg_cmd {
  32. struct list_head list;
  33. creg_cmd_cb cb;
  34. void *cb_private;
  35. unsigned int op;
  36. unsigned int addr;
  37. int cnt8;
  38. void *buf;
  39. unsigned int stream;
  40. unsigned int status;
  41. };
  42. static struct kmem_cache *creg_cmd_pool;
  43. /*------------ Private Functions --------------*/
  44. #if defined(__LITTLE_ENDIAN)
  45. #define LITTLE_ENDIAN 1
  46. #elif defined(__BIG_ENDIAN)
  47. #define LITTLE_ENDIAN 0
  48. #else
  49. #error Unknown endianess!!! Aborting...
  50. #endif
  51. static int copy_to_creg_data(struct rsxx_cardinfo *card,
  52. int cnt8,
  53. void *buf,
  54. unsigned int stream)
  55. {
  56. int i = 0;
  57. u32 *data = buf;
  58. if (unlikely(card->eeh_state))
  59. return -EIO;
  60. for (i = 0; cnt8 > 0; i++, cnt8 -= 4) {
  61. /*
  62. * Firmware implementation makes it necessary to byte swap on
  63. * little endian processors.
  64. */
  65. if (LITTLE_ENDIAN && stream)
  66. iowrite32be(data[i], card->regmap + CREG_DATA(i));
  67. else
  68. iowrite32(data[i], card->regmap + CREG_DATA(i));
  69. }
  70. return 0;
  71. }
  72. static int copy_from_creg_data(struct rsxx_cardinfo *card,
  73. int cnt8,
  74. void *buf,
  75. unsigned int stream)
  76. {
  77. int i = 0;
  78. u32 *data = buf;
  79. if (unlikely(card->eeh_state))
  80. return -EIO;
  81. for (i = 0; cnt8 > 0; i++, cnt8 -= 4) {
  82. /*
  83. * Firmware implementation makes it necessary to byte swap on
  84. * little endian processors.
  85. */
  86. if (LITTLE_ENDIAN && stream)
  87. data[i] = ioread32be(card->regmap + CREG_DATA(i));
  88. else
  89. data[i] = ioread32(card->regmap + CREG_DATA(i));
  90. }
  91. return 0;
  92. }
  93. static void creg_issue_cmd(struct rsxx_cardinfo *card, struct creg_cmd *cmd)
  94. {
  95. int st;
  96. if (unlikely(card->eeh_state))
  97. return;
  98. iowrite32(cmd->addr, card->regmap + CREG_ADD);
  99. iowrite32(cmd->cnt8, card->regmap + CREG_CNT);
  100. if (cmd->op == CREG_OP_WRITE) {
  101. if (cmd->buf) {
  102. st = copy_to_creg_data(card, cmd->cnt8,
  103. cmd->buf, cmd->stream);
  104. if (st)
  105. return;
  106. }
  107. }
  108. if (unlikely(card->eeh_state))
  109. return;
  110. /* Setting the valid bit will kick off the command. */
  111. iowrite32(cmd->op, card->regmap + CREG_CMD);
  112. }
  113. static void creg_kick_queue(struct rsxx_cardinfo *card)
  114. {
  115. if (card->creg_ctrl.active || list_empty(&card->creg_ctrl.queue))
  116. return;
  117. card->creg_ctrl.active = 1;
  118. card->creg_ctrl.active_cmd = list_first_entry(&card->creg_ctrl.queue,
  119. struct creg_cmd, list);
  120. list_del(&card->creg_ctrl.active_cmd->list);
  121. card->creg_ctrl.q_depth--;
  122. /*
  123. * We have to set the timer before we push the new command. Otherwise,
  124. * we could create a race condition that would occur if the timer
  125. * was not canceled, and expired after the new command was pushed,
  126. * but before the command was issued to hardware.
  127. */
  128. mod_timer(&card->creg_ctrl.cmd_timer,
  129. jiffies + msecs_to_jiffies(CREG_TIMEOUT_MSEC));
  130. creg_issue_cmd(card, card->creg_ctrl.active_cmd);
  131. }
  132. static int creg_queue_cmd(struct rsxx_cardinfo *card,
  133. unsigned int op,
  134. unsigned int addr,
  135. unsigned int cnt8,
  136. void *buf,
  137. int stream,
  138. creg_cmd_cb callback,
  139. void *cb_private)
  140. {
  141. struct creg_cmd *cmd;
  142. /* Don't queue stuff up if we're halted. */
  143. if (unlikely(card->halt))
  144. return -EINVAL;
  145. if (card->creg_ctrl.reset)
  146. return -EAGAIN;
  147. if (cnt8 > MAX_CREG_DATA8)
  148. return -EINVAL;
  149. cmd = kmem_cache_alloc(creg_cmd_pool, GFP_KERNEL);
  150. if (!cmd)
  151. return -ENOMEM;
  152. INIT_LIST_HEAD(&cmd->list);
  153. cmd->op = op;
  154. cmd->addr = addr;
  155. cmd->cnt8 = cnt8;
  156. cmd->buf = buf;
  157. cmd->stream = stream;
  158. cmd->cb = callback;
  159. cmd->cb_private = cb_private;
  160. cmd->status = 0;
  161. spin_lock_bh(&card->creg_ctrl.lock);
  162. list_add_tail(&cmd->list, &card->creg_ctrl.queue);
  163. card->creg_ctrl.q_depth++;
  164. creg_kick_queue(card);
  165. spin_unlock_bh(&card->creg_ctrl.lock);
  166. return 0;
  167. }
  168. static void creg_cmd_timed_out(unsigned long data)
  169. {
  170. struct rsxx_cardinfo *card = (struct rsxx_cardinfo *) data;
  171. struct creg_cmd *cmd;
  172. spin_lock(&card->creg_ctrl.lock);
  173. cmd = card->creg_ctrl.active_cmd;
  174. card->creg_ctrl.active_cmd = NULL;
  175. spin_unlock(&card->creg_ctrl.lock);
  176. if (cmd == NULL) {
  177. card->creg_ctrl.creg_stats.creg_timeout++;
  178. dev_warn(CARD_TO_DEV(card),
  179. "No active command associated with timeout!\n");
  180. return;
  181. }
  182. if (cmd->cb)
  183. cmd->cb(card, cmd, -ETIMEDOUT);
  184. kmem_cache_free(creg_cmd_pool, cmd);
  185. spin_lock(&card->creg_ctrl.lock);
  186. card->creg_ctrl.active = 0;
  187. creg_kick_queue(card);
  188. spin_unlock(&card->creg_ctrl.lock);
  189. }
  190. static void creg_cmd_done(struct work_struct *work)
  191. {
  192. struct rsxx_cardinfo *card;
  193. struct creg_cmd *cmd;
  194. int st = 0;
  195. card = container_of(work, struct rsxx_cardinfo,
  196. creg_ctrl.done_work);
  197. /*
  198. * The timer could not be cancelled for some reason,
  199. * race to pop the active command.
  200. */
  201. if (del_timer_sync(&card->creg_ctrl.cmd_timer) == 0)
  202. card->creg_ctrl.creg_stats.failed_cancel_timer++;
  203. spin_lock_bh(&card->creg_ctrl.lock);
  204. cmd = card->creg_ctrl.active_cmd;
  205. card->creg_ctrl.active_cmd = NULL;
  206. spin_unlock_bh(&card->creg_ctrl.lock);
  207. if (cmd == NULL) {
  208. dev_err(CARD_TO_DEV(card),
  209. "Spurious creg interrupt!\n");
  210. return;
  211. }
  212. card->creg_ctrl.creg_stats.stat = ioread32(card->regmap + CREG_STAT);
  213. cmd->status = card->creg_ctrl.creg_stats.stat;
  214. if ((cmd->status & CREG_STAT_STATUS_MASK) == 0) {
  215. dev_err(CARD_TO_DEV(card),
  216. "Invalid status on creg command\n");
  217. /*
  218. * At this point we're probably reading garbage from HW. Don't
  219. * do anything else that could mess up the system and let
  220. * the sync function return an error.
  221. */
  222. st = -EIO;
  223. goto creg_done;
  224. } else if (cmd->status & CREG_STAT_ERROR) {
  225. st = -EIO;
  226. }
  227. if ((cmd->op == CREG_OP_READ)) {
  228. unsigned int cnt8 = ioread32(card->regmap + CREG_CNT);
  229. /* Paranoid Sanity Checks */
  230. if (!cmd->buf) {
  231. dev_err(CARD_TO_DEV(card),
  232. "Buffer not given for read.\n");
  233. st = -EIO;
  234. goto creg_done;
  235. }
  236. if (cnt8 != cmd->cnt8) {
  237. dev_err(CARD_TO_DEV(card),
  238. "count mismatch\n");
  239. st = -EIO;
  240. goto creg_done;
  241. }
  242. st = copy_from_creg_data(card, cnt8, cmd->buf, cmd->stream);
  243. }
  244. creg_done:
  245. if (cmd->cb)
  246. cmd->cb(card, cmd, st);
  247. kmem_cache_free(creg_cmd_pool, cmd);
  248. spin_lock_bh(&card->creg_ctrl.lock);
  249. card->creg_ctrl.active = 0;
  250. creg_kick_queue(card);
  251. spin_unlock_bh(&card->creg_ctrl.lock);
  252. }
  253. static void creg_reset(struct rsxx_cardinfo *card)
  254. {
  255. struct creg_cmd *cmd = NULL;
  256. struct creg_cmd *tmp;
  257. unsigned long flags;
  258. /*
  259. * mutex_trylock is used here because if reset_lock is taken then a
  260. * reset is already happening. So, we can just go ahead and return.
  261. */
  262. if (!mutex_trylock(&card->creg_ctrl.reset_lock))
  263. return;
  264. card->creg_ctrl.reset = 1;
  265. spin_lock_irqsave(&card->irq_lock, flags);
  266. rsxx_disable_ier_and_isr(card, CR_INTR_CREG | CR_INTR_EVENT);
  267. spin_unlock_irqrestore(&card->irq_lock, flags);
  268. dev_warn(CARD_TO_DEV(card),
  269. "Resetting creg interface for recovery\n");
  270. /* Cancel outstanding commands */
  271. spin_lock_bh(&card->creg_ctrl.lock);
  272. list_for_each_entry_safe(cmd, tmp, &card->creg_ctrl.queue, list) {
  273. list_del(&cmd->list);
  274. card->creg_ctrl.q_depth--;
  275. if (cmd->cb)
  276. cmd->cb(card, cmd, -ECANCELED);
  277. kmem_cache_free(creg_cmd_pool, cmd);
  278. }
  279. cmd = card->creg_ctrl.active_cmd;
  280. card->creg_ctrl.active_cmd = NULL;
  281. if (cmd) {
  282. if (timer_pending(&card->creg_ctrl.cmd_timer))
  283. del_timer_sync(&card->creg_ctrl.cmd_timer);
  284. if (cmd->cb)
  285. cmd->cb(card, cmd, -ECANCELED);
  286. kmem_cache_free(creg_cmd_pool, cmd);
  287. card->creg_ctrl.active = 0;
  288. }
  289. spin_unlock_bh(&card->creg_ctrl.lock);
  290. card->creg_ctrl.reset = 0;
  291. spin_lock_irqsave(&card->irq_lock, flags);
  292. rsxx_enable_ier_and_isr(card, CR_INTR_CREG | CR_INTR_EVENT);
  293. spin_unlock_irqrestore(&card->irq_lock, flags);
  294. mutex_unlock(&card->creg_ctrl.reset_lock);
  295. }
  296. /* Used for synchronous accesses */
  297. struct creg_completion {
  298. struct completion *cmd_done;
  299. int st;
  300. u32 creg_status;
  301. };
  302. static void creg_cmd_done_cb(struct rsxx_cardinfo *card,
  303. struct creg_cmd *cmd,
  304. int st)
  305. {
  306. struct creg_completion *cmd_completion;
  307. cmd_completion = cmd->cb_private;
  308. BUG_ON(!cmd_completion);
  309. cmd_completion->st = st;
  310. cmd_completion->creg_status = cmd->status;
  311. complete(cmd_completion->cmd_done);
  312. }
  313. static int __issue_creg_rw(struct rsxx_cardinfo *card,
  314. unsigned int op,
  315. unsigned int addr,
  316. unsigned int cnt8,
  317. void *buf,
  318. int stream,
  319. unsigned int *hw_stat)
  320. {
  321. DECLARE_COMPLETION_ONSTACK(cmd_done);
  322. struct creg_completion completion;
  323. unsigned long timeout;
  324. int st;
  325. completion.cmd_done = &cmd_done;
  326. completion.st = 0;
  327. completion.creg_status = 0;
  328. st = creg_queue_cmd(card, op, addr, cnt8, buf, stream, creg_cmd_done_cb,
  329. &completion);
  330. if (st)
  331. return st;
  332. /*
  333. * This timeout is necessary for unresponsive hardware. The additional
  334. * 20 seconds to used to guarantee that each cregs requests has time to
  335. * complete.
  336. */
  337. timeout = msecs_to_jiffies(CREG_TIMEOUT_MSEC *
  338. card->creg_ctrl.q_depth + 20000);
  339. /*
  340. * The creg interface is guaranteed to complete. It has a timeout
  341. * mechanism that will kick in if hardware does not respond.
  342. */
  343. st = wait_for_completion_timeout(completion.cmd_done, timeout);
  344. if (st == 0) {
  345. /*
  346. * This is really bad, because the kernel timer did not
  347. * expire and notify us of a timeout!
  348. */
  349. dev_crit(CARD_TO_DEV(card),
  350. "cregs timer failed\n");
  351. creg_reset(card);
  352. return -EIO;
  353. }
  354. *hw_stat = completion.creg_status;
  355. if (completion.st) {
  356. /*
  357. * This read is needed to verify that there has not been any
  358. * extreme errors that might have occurred, i.e. EEH. The
  359. * function iowrite32 will not detect EEH errors, so it is
  360. * necessary that we recover if such an error is the reason
  361. * for the timeout. This is a dummy read.
  362. */
  363. ioread32(card->regmap + SCRATCH);
  364. dev_warn(CARD_TO_DEV(card),
  365. "creg command failed(%d x%08x)\n",
  366. completion.st, addr);
  367. return completion.st;
  368. }
  369. return 0;
  370. }
  371. static int issue_creg_rw(struct rsxx_cardinfo *card,
  372. u32 addr,
  373. unsigned int size8,
  374. void *data,
  375. int stream,
  376. int read)
  377. {
  378. unsigned int hw_stat;
  379. unsigned int xfer;
  380. unsigned int op;
  381. int st;
  382. op = read ? CREG_OP_READ : CREG_OP_WRITE;
  383. do {
  384. xfer = min_t(unsigned int, size8, MAX_CREG_DATA8);
  385. st = __issue_creg_rw(card, op, addr, xfer,
  386. data, stream, &hw_stat);
  387. if (st)
  388. return st;
  389. data = (char *)data + xfer;
  390. addr += xfer;
  391. size8 -= xfer;
  392. } while (size8);
  393. return 0;
  394. }
  395. /* ---------------------------- Public API ---------------------------------- */
  396. int rsxx_creg_write(struct rsxx_cardinfo *card,
  397. u32 addr,
  398. unsigned int size8,
  399. void *data,
  400. int byte_stream)
  401. {
  402. return issue_creg_rw(card, addr, size8, data, byte_stream, 0);
  403. }
  404. int rsxx_creg_read(struct rsxx_cardinfo *card,
  405. u32 addr,
  406. unsigned int size8,
  407. void *data,
  408. int byte_stream)
  409. {
  410. return issue_creg_rw(card, addr, size8, data, byte_stream, 1);
  411. }
  412. int rsxx_get_card_state(struct rsxx_cardinfo *card, unsigned int *state)
  413. {
  414. return rsxx_creg_read(card, CREG_ADD_CARD_STATE,
  415. sizeof(*state), state, 0);
  416. }
  417. int rsxx_get_card_size8(struct rsxx_cardinfo *card, u64 *size8)
  418. {
  419. unsigned int size;
  420. int st;
  421. st = rsxx_creg_read(card, CREG_ADD_CARD_SIZE,
  422. sizeof(size), &size, 0);
  423. if (st)
  424. return st;
  425. *size8 = (u64)size * RSXX_HW_BLK_SIZE;
  426. return 0;
  427. }
  428. int rsxx_get_num_targets(struct rsxx_cardinfo *card,
  429. unsigned int *n_targets)
  430. {
  431. return rsxx_creg_read(card, CREG_ADD_NUM_TARGETS,
  432. sizeof(*n_targets), n_targets, 0);
  433. }
  434. int rsxx_get_card_capabilities(struct rsxx_cardinfo *card,
  435. u32 *capabilities)
  436. {
  437. return rsxx_creg_read(card, CREG_ADD_CAPABILITIES,
  438. sizeof(*capabilities), capabilities, 0);
  439. }
  440. int rsxx_issue_card_cmd(struct rsxx_cardinfo *card, u32 cmd)
  441. {
  442. return rsxx_creg_write(card, CREG_ADD_CARD_CMD,
  443. sizeof(cmd), &cmd, 0);
  444. }
  445. /*----------------- HW Log Functions -------------------*/
  446. static void hw_log_msg(struct rsxx_cardinfo *card, const char *str, int len)
  447. {
  448. static char level;
  449. /*
  450. * New messages start with "<#>", where # is the log level. Messages
  451. * that extend past the log buffer will use the previous level
  452. */
  453. if ((len > 3) && (str[0] == '<') && (str[2] == '>')) {
  454. level = str[1];
  455. str += 3; /* Skip past the log level. */
  456. len -= 3;
  457. }
  458. switch (level) {
  459. case '0':
  460. dev_emerg(CARD_TO_DEV(card), "HW: %.*s", len, str);
  461. break;
  462. case '1':
  463. dev_alert(CARD_TO_DEV(card), "HW: %.*s", len, str);
  464. break;
  465. case '2':
  466. dev_crit(CARD_TO_DEV(card), "HW: %.*s", len, str);
  467. break;
  468. case '3':
  469. dev_err(CARD_TO_DEV(card), "HW: %.*s", len, str);
  470. break;
  471. case '4':
  472. dev_warn(CARD_TO_DEV(card), "HW: %.*s", len, str);
  473. break;
  474. case '5':
  475. dev_notice(CARD_TO_DEV(card), "HW: %.*s", len, str);
  476. break;
  477. case '6':
  478. dev_info(CARD_TO_DEV(card), "HW: %.*s", len, str);
  479. break;
  480. case '7':
  481. dev_dbg(CARD_TO_DEV(card), "HW: %.*s", len, str);
  482. break;
  483. default:
  484. dev_info(CARD_TO_DEV(card), "HW: %.*s", len, str);
  485. break;
  486. }
  487. }
  488. /*
  489. * The substrncpy function copies the src string (which includes the
  490. * terminating '\0' character), up to the count into the dest pointer.
  491. * Returns the number of bytes copied to dest.
  492. */
  493. static int substrncpy(char *dest, const char *src, int count)
  494. {
  495. int max_cnt = count;
  496. while (count) {
  497. count--;
  498. *dest = *src;
  499. if (*dest == '\0')
  500. break;
  501. src++;
  502. dest++;
  503. }
  504. return max_cnt - count;
  505. }
  506. static void read_hw_log_done(struct rsxx_cardinfo *card,
  507. struct creg_cmd *cmd,
  508. int st)
  509. {
  510. char *buf;
  511. char *log_str;
  512. int cnt;
  513. int len;
  514. int off;
  515. buf = cmd->buf;
  516. off = 0;
  517. /* Failed getting the log message */
  518. if (st)
  519. return;
  520. while (off < cmd->cnt8) {
  521. log_str = &card->log.buf[card->log.buf_len];
  522. cnt = min(cmd->cnt8 - off, LOG_BUF_SIZE8 - card->log.buf_len);
  523. len = substrncpy(log_str, &buf[off], cnt);
  524. off += len;
  525. card->log.buf_len += len;
  526. /*
  527. * Flush the log if we've hit the end of a message or if we've
  528. * run out of buffer space.
  529. */
  530. if ((log_str[len - 1] == '\0') ||
  531. (card->log.buf_len == LOG_BUF_SIZE8)) {
  532. if (card->log.buf_len != 1) /* Don't log blank lines. */
  533. hw_log_msg(card, card->log.buf,
  534. card->log.buf_len);
  535. card->log.buf_len = 0;
  536. }
  537. }
  538. if (cmd->status & CREG_STAT_LOG_PENDING)
  539. rsxx_read_hw_log(card);
  540. }
  541. int rsxx_read_hw_log(struct rsxx_cardinfo *card)
  542. {
  543. int st;
  544. st = creg_queue_cmd(card, CREG_OP_READ, CREG_ADD_LOG,
  545. sizeof(card->log.tmp), card->log.tmp,
  546. 1, read_hw_log_done, NULL);
  547. if (st)
  548. dev_err(CARD_TO_DEV(card),
  549. "Failed getting log text\n");
  550. return st;
  551. }
  552. /*-------------- IOCTL REG Access ------------------*/
  553. static int issue_reg_cmd(struct rsxx_cardinfo *card,
  554. struct rsxx_reg_access *cmd,
  555. int read)
  556. {
  557. unsigned int op = read ? CREG_OP_READ : CREG_OP_WRITE;
  558. return __issue_creg_rw(card, op, cmd->addr, cmd->cnt, cmd->data,
  559. cmd->stream, &cmd->stat);
  560. }
  561. int rsxx_reg_access(struct rsxx_cardinfo *card,
  562. struct rsxx_reg_access __user *ucmd,
  563. int read)
  564. {
  565. struct rsxx_reg_access cmd;
  566. int st;
  567. st = copy_from_user(&cmd, ucmd, sizeof(cmd));
  568. if (st)
  569. return -EFAULT;
  570. if (cmd.cnt > RSXX_MAX_REG_CNT)
  571. return -EFAULT;
  572. st = issue_reg_cmd(card, &cmd, read);
  573. if (st)
  574. return st;
  575. st = put_user(cmd.stat, &ucmd->stat);
  576. if (st)
  577. return -EFAULT;
  578. if (read) {
  579. st = copy_to_user(ucmd->data, cmd.data, cmd.cnt);
  580. if (st)
  581. return -EFAULT;
  582. }
  583. return 0;
  584. }
  585. void rsxx_eeh_save_issued_creg(struct rsxx_cardinfo *card)
  586. {
  587. struct creg_cmd *cmd = NULL;
  588. cmd = card->creg_ctrl.active_cmd;
  589. card->creg_ctrl.active_cmd = NULL;
  590. if (cmd) {
  591. del_timer_sync(&card->creg_ctrl.cmd_timer);
  592. spin_lock_bh(&card->creg_ctrl.lock);
  593. list_add(&cmd->list, &card->creg_ctrl.queue);
  594. card->creg_ctrl.q_depth++;
  595. card->creg_ctrl.active = 0;
  596. spin_unlock_bh(&card->creg_ctrl.lock);
  597. }
  598. }
  599. void rsxx_kick_creg_queue(struct rsxx_cardinfo *card)
  600. {
  601. spin_lock_bh(&card->creg_ctrl.lock);
  602. if (!list_empty(&card->creg_ctrl.queue))
  603. creg_kick_queue(card);
  604. spin_unlock_bh(&card->creg_ctrl.lock);
  605. }
  606. /*------------ Initialization & Setup --------------*/
  607. int rsxx_creg_setup(struct rsxx_cardinfo *card)
  608. {
  609. card->creg_ctrl.active_cmd = NULL;
  610. card->creg_ctrl.creg_wq =
  611. create_singlethread_workqueue(DRIVER_NAME"_creg");
  612. if (!card->creg_ctrl.creg_wq)
  613. return -ENOMEM;
  614. INIT_WORK(&card->creg_ctrl.done_work, creg_cmd_done);
  615. mutex_init(&card->creg_ctrl.reset_lock);
  616. INIT_LIST_HEAD(&card->creg_ctrl.queue);
  617. spin_lock_init(&card->creg_ctrl.lock);
  618. setup_timer(&card->creg_ctrl.cmd_timer, creg_cmd_timed_out,
  619. (unsigned long) card);
  620. return 0;
  621. }
  622. void rsxx_creg_destroy(struct rsxx_cardinfo *card)
  623. {
  624. struct creg_cmd *cmd;
  625. struct creg_cmd *tmp;
  626. int cnt = 0;
  627. /* Cancel outstanding commands */
  628. spin_lock_bh(&card->creg_ctrl.lock);
  629. list_for_each_entry_safe(cmd, tmp, &card->creg_ctrl.queue, list) {
  630. list_del(&cmd->list);
  631. if (cmd->cb)
  632. cmd->cb(card, cmd, -ECANCELED);
  633. kmem_cache_free(creg_cmd_pool, cmd);
  634. cnt++;
  635. }
  636. if (cnt)
  637. dev_info(CARD_TO_DEV(card),
  638. "Canceled %d queue creg commands\n", cnt);
  639. cmd = card->creg_ctrl.active_cmd;
  640. card->creg_ctrl.active_cmd = NULL;
  641. if (cmd) {
  642. if (timer_pending(&card->creg_ctrl.cmd_timer))
  643. del_timer_sync(&card->creg_ctrl.cmd_timer);
  644. if (cmd->cb)
  645. cmd->cb(card, cmd, -ECANCELED);
  646. dev_info(CARD_TO_DEV(card),
  647. "Canceled active creg command\n");
  648. kmem_cache_free(creg_cmd_pool, cmd);
  649. }
  650. spin_unlock_bh(&card->creg_ctrl.lock);
  651. cancel_work_sync(&card->creg_ctrl.done_work);
  652. }
  653. int rsxx_creg_init(void)
  654. {
  655. creg_cmd_pool = KMEM_CACHE(creg_cmd, SLAB_HWCACHE_ALIGN);
  656. if (!creg_cmd_pool)
  657. return -ENOMEM;
  658. return 0;
  659. }
  660. void rsxx_creg_cleanup(void)
  661. {
  662. kmem_cache_destroy(creg_cmd_pool);
  663. }