ap_queue.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2016
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  5. *
  6. * Adjunct processor bus, queue related code.
  7. */
  8. #define KMSG_COMPONENT "ap"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <asm/facility.h>
  13. #include "ap_bus.h"
  14. #include "ap_debug.h"
  15. static void __ap_flush_queue(struct ap_queue *aq);
  16. /**
  17. * ap_queue_enable_irq(): Enable interrupt support on this AP queue.
  18. * @qid: The AP queue number
  19. * @ind: the notification indicator byte
  20. *
  21. * Enables interruption on AP queue via ap_aqic(). Based on the return
  22. * value it waits a while and tests the AP queue if interrupts
  23. * have been switched on using ap_test_queue().
  24. */
  25. static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
  26. {
  27. struct ap_queue_status status;
  28. struct ap_qirq_ctrl qirqctrl = { 0 };
  29. qirqctrl.ir = 1;
  30. qirqctrl.isc = AP_ISC;
  31. status = ap_aqic(aq->qid, qirqctrl, ind);
  32. switch (status.response_code) {
  33. case AP_RESPONSE_NORMAL:
  34. case AP_RESPONSE_OTHERWISE_CHANGED:
  35. return 0;
  36. case AP_RESPONSE_Q_NOT_AVAIL:
  37. case AP_RESPONSE_DECONFIGURED:
  38. case AP_RESPONSE_CHECKSTOPPED:
  39. case AP_RESPONSE_INVALID_ADDRESS:
  40. pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
  41. AP_QID_CARD(aq->qid),
  42. AP_QID_QUEUE(aq->qid));
  43. return -EOPNOTSUPP;
  44. case AP_RESPONSE_RESET_IN_PROGRESS:
  45. case AP_RESPONSE_BUSY:
  46. default:
  47. return -EBUSY;
  48. }
  49. }
  50. /**
  51. * __ap_send(): Send message to adjunct processor queue.
  52. * @qid: The AP queue number
  53. * @psmid: The program supplied message identifier
  54. * @msg: The message text
  55. * @length: The message length
  56. * @special: Special Bit
  57. *
  58. * Returns AP queue status structure.
  59. * Condition code 1 on NQAP can't happen because the L bit is 1.
  60. * Condition code 2 on NQAP also means the send is incomplete,
  61. * because a segment boundary was reached. The NQAP is repeated.
  62. */
  63. static inline struct ap_queue_status
  64. __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
  65. int special)
  66. {
  67. if (special)
  68. qid |= 0x400000UL;
  69. return ap_nqap(qid, psmid, msg, length);
  70. }
  71. int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
  72. {
  73. struct ap_queue_status status;
  74. status = __ap_send(qid, psmid, msg, length, 0);
  75. switch (status.response_code) {
  76. case AP_RESPONSE_NORMAL:
  77. return 0;
  78. case AP_RESPONSE_Q_FULL:
  79. case AP_RESPONSE_RESET_IN_PROGRESS:
  80. return -EBUSY;
  81. case AP_RESPONSE_REQ_FAC_NOT_INST:
  82. return -EINVAL;
  83. default: /* Device is gone. */
  84. return -ENODEV;
  85. }
  86. }
  87. EXPORT_SYMBOL(ap_send);
  88. int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
  89. {
  90. struct ap_queue_status status;
  91. if (msg == NULL)
  92. return -EINVAL;
  93. status = ap_dqap(qid, psmid, msg, length);
  94. switch (status.response_code) {
  95. case AP_RESPONSE_NORMAL:
  96. return 0;
  97. case AP_RESPONSE_NO_PENDING_REPLY:
  98. if (status.queue_empty)
  99. return -ENOENT;
  100. return -EBUSY;
  101. case AP_RESPONSE_RESET_IN_PROGRESS:
  102. return -EBUSY;
  103. default:
  104. return -ENODEV;
  105. }
  106. }
  107. EXPORT_SYMBOL(ap_recv);
  108. /* State machine definitions and helpers */
  109. static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq)
  110. {
  111. return AP_SM_WAIT_NONE;
  112. }
  113. /**
  114. * ap_sm_recv(): Receive pending reply messages from an AP queue but do
  115. * not change the state of the device.
  116. * @aq: pointer to the AP queue
  117. *
  118. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  119. */
  120. static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
  121. {
  122. struct ap_queue_status status;
  123. struct ap_message *ap_msg;
  124. bool found = false;
  125. status = ap_dqap(aq->qid, &aq->reply->psmid,
  126. aq->reply->msg, aq->reply->len);
  127. switch (status.response_code) {
  128. case AP_RESPONSE_NORMAL:
  129. aq->queue_count = max_t(int, 0, aq->queue_count - 1);
  130. if (!status.queue_empty && !aq->queue_count)
  131. aq->queue_count++;
  132. if (aq->queue_count > 0)
  133. mod_timer(&aq->timeout,
  134. jiffies + aq->request_timeout);
  135. list_for_each_entry(ap_msg, &aq->pendingq, list) {
  136. if (ap_msg->psmid != aq->reply->psmid)
  137. continue;
  138. list_del_init(&ap_msg->list);
  139. aq->pendingq_count--;
  140. ap_msg->receive(aq, ap_msg, aq->reply);
  141. found = true;
  142. break;
  143. }
  144. if (!found) {
  145. AP_DBF_WARN("%s unassociated reply psmid=0x%016llx on 0x%02x.%04x\n",
  146. __func__, aq->reply->psmid,
  147. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  148. }
  149. fallthrough;
  150. case AP_RESPONSE_NO_PENDING_REPLY:
  151. if (!status.queue_empty || aq->queue_count <= 0)
  152. break;
  153. /* The card shouldn't forget requests but who knows. */
  154. aq->queue_count = 0;
  155. list_splice_init(&aq->pendingq, &aq->requestq);
  156. aq->requestq_count += aq->pendingq_count;
  157. aq->pendingq_count = 0;
  158. break;
  159. default:
  160. break;
  161. }
  162. return status;
  163. }
  164. /**
  165. * ap_sm_read(): Receive pending reply messages from an AP queue.
  166. * @aq: pointer to the AP queue
  167. *
  168. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  169. */
  170. static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
  171. {
  172. struct ap_queue_status status;
  173. if (!aq->reply)
  174. return AP_SM_WAIT_NONE;
  175. status = ap_sm_recv(aq);
  176. switch (status.response_code) {
  177. case AP_RESPONSE_NORMAL:
  178. if (aq->queue_count > 0) {
  179. aq->sm_state = AP_SM_STATE_WORKING;
  180. return AP_SM_WAIT_AGAIN;
  181. }
  182. aq->sm_state = AP_SM_STATE_IDLE;
  183. return AP_SM_WAIT_NONE;
  184. case AP_RESPONSE_NO_PENDING_REPLY:
  185. if (aq->queue_count > 0)
  186. return aq->interrupt ?
  187. AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
  188. aq->sm_state = AP_SM_STATE_IDLE;
  189. return AP_SM_WAIT_NONE;
  190. default:
  191. aq->dev_state = AP_DEV_STATE_ERROR;
  192. aq->last_err_rc = status.response_code;
  193. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  194. __func__, status.response_code,
  195. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  196. return AP_SM_WAIT_NONE;
  197. }
  198. }
  199. /**
  200. * ap_sm_write(): Send messages from the request queue to an AP queue.
  201. * @aq: pointer to the AP queue
  202. *
  203. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  204. */
  205. static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
  206. {
  207. struct ap_queue_status status;
  208. struct ap_message *ap_msg;
  209. ap_qid_t qid = aq->qid;
  210. if (aq->requestq_count <= 0)
  211. return AP_SM_WAIT_NONE;
  212. /* Start the next request on the queue. */
  213. ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
  214. #ifdef CONFIG_ZCRYPT_DEBUG
  215. if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
  216. AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
  217. __func__, ap_msg->fi.cmd);
  218. qid = 0xFF00;
  219. }
  220. #endif
  221. status = __ap_send(qid, ap_msg->psmid,
  222. ap_msg->msg, ap_msg->len,
  223. ap_msg->flags & AP_MSG_FLAG_SPECIAL);
  224. switch (status.response_code) {
  225. case AP_RESPONSE_NORMAL:
  226. aq->queue_count = max_t(int, 1, aq->queue_count + 1);
  227. if (aq->queue_count == 1)
  228. mod_timer(&aq->timeout, jiffies + aq->request_timeout);
  229. list_move_tail(&ap_msg->list, &aq->pendingq);
  230. aq->requestq_count--;
  231. aq->pendingq_count++;
  232. if (aq->queue_count < aq->card->queue_depth) {
  233. aq->sm_state = AP_SM_STATE_WORKING;
  234. return AP_SM_WAIT_AGAIN;
  235. }
  236. fallthrough;
  237. case AP_RESPONSE_Q_FULL:
  238. aq->sm_state = AP_SM_STATE_QUEUE_FULL;
  239. return aq->interrupt ?
  240. AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
  241. case AP_RESPONSE_RESET_IN_PROGRESS:
  242. aq->sm_state = AP_SM_STATE_RESET_WAIT;
  243. return AP_SM_WAIT_TIMEOUT;
  244. case AP_RESPONSE_INVALID_DOMAIN:
  245. AP_DBF(DBF_WARN, "AP_RESPONSE_INVALID_DOMAIN on NQAP\n");
  246. fallthrough;
  247. case AP_RESPONSE_MESSAGE_TOO_BIG:
  248. case AP_RESPONSE_REQ_FAC_NOT_INST:
  249. list_del_init(&ap_msg->list);
  250. aq->requestq_count--;
  251. ap_msg->rc = -EINVAL;
  252. ap_msg->receive(aq, ap_msg, NULL);
  253. return AP_SM_WAIT_AGAIN;
  254. default:
  255. aq->dev_state = AP_DEV_STATE_ERROR;
  256. aq->last_err_rc = status.response_code;
  257. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  258. __func__, status.response_code,
  259. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  260. return AP_SM_WAIT_NONE;
  261. }
  262. }
  263. /**
  264. * ap_sm_read_write(): Send and receive messages to/from an AP queue.
  265. * @aq: pointer to the AP queue
  266. *
  267. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  268. */
  269. static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
  270. {
  271. return min(ap_sm_read(aq), ap_sm_write(aq));
  272. }
  273. /**
  274. * ap_sm_reset(): Reset an AP queue.
  275. * @qid: The AP queue number
  276. *
  277. * Submit the Reset command to an AP queue.
  278. */
  279. static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
  280. {
  281. struct ap_queue_status status;
  282. status = ap_rapq(aq->qid);
  283. switch (status.response_code) {
  284. case AP_RESPONSE_NORMAL:
  285. case AP_RESPONSE_RESET_IN_PROGRESS:
  286. aq->sm_state = AP_SM_STATE_RESET_WAIT;
  287. aq->interrupt = false;
  288. return AP_SM_WAIT_TIMEOUT;
  289. default:
  290. aq->dev_state = AP_DEV_STATE_ERROR;
  291. aq->last_err_rc = status.response_code;
  292. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  293. __func__, status.response_code,
  294. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  295. return AP_SM_WAIT_NONE;
  296. }
  297. }
  298. /**
  299. * ap_sm_reset_wait(): Test queue for completion of the reset operation
  300. * @aq: pointer to the AP queue
  301. *
  302. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  303. */
  304. static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
  305. {
  306. struct ap_queue_status status;
  307. void *lsi_ptr;
  308. if (aq->queue_count > 0 && aq->reply)
  309. /* Try to read a completed message and get the status */
  310. status = ap_sm_recv(aq);
  311. else
  312. /* Get the status with TAPQ */
  313. status = ap_tapq(aq->qid, NULL);
  314. switch (status.response_code) {
  315. case AP_RESPONSE_NORMAL:
  316. lsi_ptr = ap_airq_ptr();
  317. if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
  318. aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
  319. else
  320. aq->sm_state = (aq->queue_count > 0) ?
  321. AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
  322. return AP_SM_WAIT_AGAIN;
  323. case AP_RESPONSE_BUSY:
  324. case AP_RESPONSE_RESET_IN_PROGRESS:
  325. return AP_SM_WAIT_TIMEOUT;
  326. case AP_RESPONSE_Q_NOT_AVAIL:
  327. case AP_RESPONSE_DECONFIGURED:
  328. case AP_RESPONSE_CHECKSTOPPED:
  329. default:
  330. aq->dev_state = AP_DEV_STATE_ERROR;
  331. aq->last_err_rc = status.response_code;
  332. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  333. __func__, status.response_code,
  334. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  335. return AP_SM_WAIT_NONE;
  336. }
  337. }
  338. /**
  339. * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
  340. * @aq: pointer to the AP queue
  341. *
  342. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  343. */
  344. static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
  345. {
  346. struct ap_queue_status status;
  347. if (aq->queue_count > 0 && aq->reply)
  348. /* Try to read a completed message and get the status */
  349. status = ap_sm_recv(aq);
  350. else
  351. /* Get the status with TAPQ */
  352. status = ap_tapq(aq->qid, NULL);
  353. if (status.irq_enabled == 1) {
  354. /* Irqs are now enabled */
  355. aq->interrupt = true;
  356. aq->sm_state = (aq->queue_count > 0) ?
  357. AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
  358. }
  359. switch (status.response_code) {
  360. case AP_RESPONSE_NORMAL:
  361. if (aq->queue_count > 0)
  362. return AP_SM_WAIT_AGAIN;
  363. fallthrough;
  364. case AP_RESPONSE_NO_PENDING_REPLY:
  365. return AP_SM_WAIT_TIMEOUT;
  366. default:
  367. aq->dev_state = AP_DEV_STATE_ERROR;
  368. aq->last_err_rc = status.response_code;
  369. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  370. __func__, status.response_code,
  371. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  372. return AP_SM_WAIT_NONE;
  373. }
  374. }
  375. /*
  376. * AP state machine jump table
  377. */
  378. static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
  379. [AP_SM_STATE_RESET_START] = {
  380. [AP_SM_EVENT_POLL] = ap_sm_reset,
  381. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  382. },
  383. [AP_SM_STATE_RESET_WAIT] = {
  384. [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
  385. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  386. },
  387. [AP_SM_STATE_SETIRQ_WAIT] = {
  388. [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
  389. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  390. },
  391. [AP_SM_STATE_IDLE] = {
  392. [AP_SM_EVENT_POLL] = ap_sm_write,
  393. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  394. },
  395. [AP_SM_STATE_WORKING] = {
  396. [AP_SM_EVENT_POLL] = ap_sm_read_write,
  397. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  398. },
  399. [AP_SM_STATE_QUEUE_FULL] = {
  400. [AP_SM_EVENT_POLL] = ap_sm_read,
  401. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  402. },
  403. };
  404. enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
  405. {
  406. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  407. return ap_jumptable[aq->sm_state][event](aq);
  408. else
  409. return AP_SM_WAIT_NONE;
  410. }
  411. enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
  412. {
  413. enum ap_sm_wait wait;
  414. while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
  415. ;
  416. return wait;
  417. }
  418. /*
  419. * AP queue related attributes.
  420. */
  421. static ssize_t request_count_show(struct device *dev,
  422. struct device_attribute *attr,
  423. char *buf)
  424. {
  425. struct ap_queue *aq = to_ap_queue(dev);
  426. bool valid = false;
  427. u64 req_cnt;
  428. spin_lock_bh(&aq->lock);
  429. if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
  430. req_cnt = aq->total_request_count;
  431. valid = true;
  432. }
  433. spin_unlock_bh(&aq->lock);
  434. if (valid)
  435. return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
  436. else
  437. return scnprintf(buf, PAGE_SIZE, "-\n");
  438. }
  439. static ssize_t request_count_store(struct device *dev,
  440. struct device_attribute *attr,
  441. const char *buf, size_t count)
  442. {
  443. struct ap_queue *aq = to_ap_queue(dev);
  444. spin_lock_bh(&aq->lock);
  445. aq->total_request_count = 0;
  446. spin_unlock_bh(&aq->lock);
  447. return count;
  448. }
  449. static DEVICE_ATTR_RW(request_count);
  450. static ssize_t requestq_count_show(struct device *dev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. struct ap_queue *aq = to_ap_queue(dev);
  454. unsigned int reqq_cnt = 0;
  455. spin_lock_bh(&aq->lock);
  456. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  457. reqq_cnt = aq->requestq_count;
  458. spin_unlock_bh(&aq->lock);
  459. return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
  460. }
  461. static DEVICE_ATTR_RO(requestq_count);
  462. static ssize_t pendingq_count_show(struct device *dev,
  463. struct device_attribute *attr, char *buf)
  464. {
  465. struct ap_queue *aq = to_ap_queue(dev);
  466. unsigned int penq_cnt = 0;
  467. spin_lock_bh(&aq->lock);
  468. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  469. penq_cnt = aq->pendingq_count;
  470. spin_unlock_bh(&aq->lock);
  471. return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
  472. }
  473. static DEVICE_ATTR_RO(pendingq_count);
  474. static ssize_t reset_show(struct device *dev,
  475. struct device_attribute *attr, char *buf)
  476. {
  477. struct ap_queue *aq = to_ap_queue(dev);
  478. int rc = 0;
  479. spin_lock_bh(&aq->lock);
  480. switch (aq->sm_state) {
  481. case AP_SM_STATE_RESET_START:
  482. case AP_SM_STATE_RESET_WAIT:
  483. rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
  484. break;
  485. case AP_SM_STATE_WORKING:
  486. case AP_SM_STATE_QUEUE_FULL:
  487. rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
  488. break;
  489. default:
  490. rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
  491. }
  492. spin_unlock_bh(&aq->lock);
  493. return rc;
  494. }
  495. static ssize_t reset_store(struct device *dev,
  496. struct device_attribute *attr,
  497. const char *buf, size_t count)
  498. {
  499. struct ap_queue *aq = to_ap_queue(dev);
  500. spin_lock_bh(&aq->lock);
  501. __ap_flush_queue(aq);
  502. aq->sm_state = AP_SM_STATE_RESET_START;
  503. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  504. spin_unlock_bh(&aq->lock);
  505. AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
  506. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  507. return count;
  508. }
  509. static DEVICE_ATTR_RW(reset);
  510. static ssize_t interrupt_show(struct device *dev,
  511. struct device_attribute *attr, char *buf)
  512. {
  513. struct ap_queue *aq = to_ap_queue(dev);
  514. int rc = 0;
  515. spin_lock_bh(&aq->lock);
  516. if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
  517. rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
  518. else if (aq->interrupt)
  519. rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
  520. else
  521. rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
  522. spin_unlock_bh(&aq->lock);
  523. return rc;
  524. }
  525. static DEVICE_ATTR_RO(interrupt);
  526. static ssize_t config_show(struct device *dev,
  527. struct device_attribute *attr, char *buf)
  528. {
  529. struct ap_queue *aq = to_ap_queue(dev);
  530. int rc;
  531. spin_lock_bh(&aq->lock);
  532. rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
  533. spin_unlock_bh(&aq->lock);
  534. return rc;
  535. }
  536. static DEVICE_ATTR_RO(config);
  537. #ifdef CONFIG_ZCRYPT_DEBUG
  538. static ssize_t states_show(struct device *dev,
  539. struct device_attribute *attr, char *buf)
  540. {
  541. struct ap_queue *aq = to_ap_queue(dev);
  542. int rc = 0;
  543. spin_lock_bh(&aq->lock);
  544. /* queue device state */
  545. switch (aq->dev_state) {
  546. case AP_DEV_STATE_UNINITIATED:
  547. rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
  548. break;
  549. case AP_DEV_STATE_OPERATING:
  550. rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
  551. break;
  552. case AP_DEV_STATE_SHUTDOWN:
  553. rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
  554. break;
  555. case AP_DEV_STATE_ERROR:
  556. rc = scnprintf(buf, PAGE_SIZE, "ERROR");
  557. break;
  558. default:
  559. rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
  560. }
  561. /* state machine state */
  562. if (aq->dev_state) {
  563. switch (aq->sm_state) {
  564. case AP_SM_STATE_RESET_START:
  565. rc += scnprintf(buf + rc, PAGE_SIZE - rc,
  566. " [RESET_START]\n");
  567. break;
  568. case AP_SM_STATE_RESET_WAIT:
  569. rc += scnprintf(buf + rc, PAGE_SIZE - rc,
  570. " [RESET_WAIT]\n");
  571. break;
  572. case AP_SM_STATE_SETIRQ_WAIT:
  573. rc += scnprintf(buf + rc, PAGE_SIZE - rc,
  574. " [SETIRQ_WAIT]\n");
  575. break;
  576. case AP_SM_STATE_IDLE:
  577. rc += scnprintf(buf + rc, PAGE_SIZE - rc,
  578. " [IDLE]\n");
  579. break;
  580. case AP_SM_STATE_WORKING:
  581. rc += scnprintf(buf + rc, PAGE_SIZE - rc,
  582. " [WORKING]\n");
  583. break;
  584. case AP_SM_STATE_QUEUE_FULL:
  585. rc += scnprintf(buf + rc, PAGE_SIZE - rc,
  586. " [FULL]\n");
  587. break;
  588. default:
  589. rc += scnprintf(buf + rc, PAGE_SIZE - rc,
  590. " [UNKNOWN]\n");
  591. }
  592. }
  593. spin_unlock_bh(&aq->lock);
  594. return rc;
  595. }
  596. static DEVICE_ATTR_RO(states);
  597. static ssize_t last_err_rc_show(struct device *dev,
  598. struct device_attribute *attr, char *buf)
  599. {
  600. struct ap_queue *aq = to_ap_queue(dev);
  601. int rc;
  602. spin_lock_bh(&aq->lock);
  603. rc = aq->last_err_rc;
  604. spin_unlock_bh(&aq->lock);
  605. switch (rc) {
  606. case AP_RESPONSE_NORMAL:
  607. return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
  608. case AP_RESPONSE_Q_NOT_AVAIL:
  609. return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
  610. case AP_RESPONSE_RESET_IN_PROGRESS:
  611. return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
  612. case AP_RESPONSE_DECONFIGURED:
  613. return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
  614. case AP_RESPONSE_CHECKSTOPPED:
  615. return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
  616. case AP_RESPONSE_BUSY:
  617. return scnprintf(buf, PAGE_SIZE, "BUSY\n");
  618. case AP_RESPONSE_INVALID_ADDRESS:
  619. return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
  620. case AP_RESPONSE_OTHERWISE_CHANGED:
  621. return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
  622. case AP_RESPONSE_Q_FULL:
  623. return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
  624. case AP_RESPONSE_INDEX_TOO_BIG:
  625. return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
  626. case AP_RESPONSE_NO_FIRST_PART:
  627. return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
  628. case AP_RESPONSE_MESSAGE_TOO_BIG:
  629. return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
  630. case AP_RESPONSE_REQ_FAC_NOT_INST:
  631. return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
  632. default:
  633. return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
  634. }
  635. }
  636. static DEVICE_ATTR_RO(last_err_rc);
  637. #endif
  638. static struct attribute *ap_queue_dev_attrs[] = {
  639. &dev_attr_request_count.attr,
  640. &dev_attr_requestq_count.attr,
  641. &dev_attr_pendingq_count.attr,
  642. &dev_attr_reset.attr,
  643. &dev_attr_interrupt.attr,
  644. &dev_attr_config.attr,
  645. #ifdef CONFIG_ZCRYPT_DEBUG
  646. &dev_attr_states.attr,
  647. &dev_attr_last_err_rc.attr,
  648. #endif
  649. NULL
  650. };
  651. static struct attribute_group ap_queue_dev_attr_group = {
  652. .attrs = ap_queue_dev_attrs
  653. };
  654. static const struct attribute_group *ap_queue_dev_attr_groups[] = {
  655. &ap_queue_dev_attr_group,
  656. NULL
  657. };
  658. static struct device_type ap_queue_type = {
  659. .name = "ap_queue",
  660. .groups = ap_queue_dev_attr_groups,
  661. };
  662. static void ap_queue_device_release(struct device *dev)
  663. {
  664. struct ap_queue *aq = to_ap_queue(dev);
  665. spin_lock_bh(&ap_queues_lock);
  666. hash_del(&aq->hnode);
  667. spin_unlock_bh(&ap_queues_lock);
  668. kfree(aq);
  669. }
  670. struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
  671. {
  672. struct ap_queue *aq;
  673. aq = kzalloc(sizeof(*aq), GFP_KERNEL);
  674. if (!aq)
  675. return NULL;
  676. aq->ap_dev.device.release = ap_queue_device_release;
  677. aq->ap_dev.device.type = &ap_queue_type;
  678. aq->ap_dev.device_type = device_type;
  679. aq->qid = qid;
  680. aq->interrupt = false;
  681. spin_lock_init(&aq->lock);
  682. INIT_LIST_HEAD(&aq->pendingq);
  683. INIT_LIST_HEAD(&aq->requestq);
  684. timer_setup(&aq->timeout, ap_request_timeout, 0);
  685. return aq;
  686. }
  687. void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
  688. {
  689. aq->reply = reply;
  690. spin_lock_bh(&aq->lock);
  691. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  692. spin_unlock_bh(&aq->lock);
  693. }
  694. EXPORT_SYMBOL(ap_queue_init_reply);
  695. /**
  696. * ap_queue_message(): Queue a request to an AP device.
  697. * @aq: The AP device to queue the message to
  698. * @ap_msg: The message that is to be added
  699. */
  700. int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
  701. {
  702. int rc = 0;
  703. /* msg needs to have a valid receive-callback */
  704. BUG_ON(!ap_msg->receive);
  705. spin_lock_bh(&aq->lock);
  706. /* only allow to queue new messages if device state is ok */
  707. if (aq->dev_state == AP_DEV_STATE_OPERATING) {
  708. list_add_tail(&ap_msg->list, &aq->requestq);
  709. aq->requestq_count++;
  710. aq->total_request_count++;
  711. atomic64_inc(&aq->card->total_request_count);
  712. } else
  713. rc = -ENODEV;
  714. /* Send/receive as many request from the queue as possible. */
  715. ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
  716. spin_unlock_bh(&aq->lock);
  717. return rc;
  718. }
  719. EXPORT_SYMBOL(ap_queue_message);
  720. /**
  721. * ap_cancel_message(): Cancel a crypto request.
  722. * @aq: The AP device that has the message queued
  723. * @ap_msg: The message that is to be removed
  724. *
  725. * Cancel a crypto request. This is done by removing the request
  726. * from the device pending or request queue. Note that the
  727. * request stays on the AP queue. When it finishes the message
  728. * reply will be discarded because the psmid can't be found.
  729. */
  730. void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
  731. {
  732. struct ap_message *tmp;
  733. spin_lock_bh(&aq->lock);
  734. if (!list_empty(&ap_msg->list)) {
  735. list_for_each_entry(tmp, &aq->pendingq, list)
  736. if (tmp->psmid == ap_msg->psmid) {
  737. aq->pendingq_count--;
  738. goto found;
  739. }
  740. aq->requestq_count--;
  741. found:
  742. list_del_init(&ap_msg->list);
  743. }
  744. spin_unlock_bh(&aq->lock);
  745. }
  746. EXPORT_SYMBOL(ap_cancel_message);
  747. /**
  748. * __ap_flush_queue(): Flush requests.
  749. * @aq: Pointer to the AP queue
  750. *
  751. * Flush all requests from the request/pending queue of an AP device.
  752. */
  753. static void __ap_flush_queue(struct ap_queue *aq)
  754. {
  755. struct ap_message *ap_msg, *next;
  756. list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
  757. list_del_init(&ap_msg->list);
  758. aq->pendingq_count--;
  759. ap_msg->rc = -EAGAIN;
  760. ap_msg->receive(aq, ap_msg, NULL);
  761. }
  762. list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
  763. list_del_init(&ap_msg->list);
  764. aq->requestq_count--;
  765. ap_msg->rc = -EAGAIN;
  766. ap_msg->receive(aq, ap_msg, NULL);
  767. }
  768. aq->queue_count = 0;
  769. }
  770. void ap_flush_queue(struct ap_queue *aq)
  771. {
  772. spin_lock_bh(&aq->lock);
  773. __ap_flush_queue(aq);
  774. spin_unlock_bh(&aq->lock);
  775. }
  776. EXPORT_SYMBOL(ap_flush_queue);
  777. void ap_queue_prepare_remove(struct ap_queue *aq)
  778. {
  779. spin_lock_bh(&aq->lock);
  780. /* flush queue */
  781. __ap_flush_queue(aq);
  782. /* move queue device state to SHUTDOWN in progress */
  783. aq->dev_state = AP_DEV_STATE_SHUTDOWN;
  784. spin_unlock_bh(&aq->lock);
  785. del_timer_sync(&aq->timeout);
  786. }
  787. void ap_queue_remove(struct ap_queue *aq)
  788. {
  789. /*
  790. * all messages have been flushed and the device state
  791. * is SHUTDOWN. Now reset with zero which also clears
  792. * the irq registration and move the device state
  793. * to the initial value AP_DEV_STATE_UNINITIATED.
  794. */
  795. spin_lock_bh(&aq->lock);
  796. ap_zapq(aq->qid);
  797. aq->dev_state = AP_DEV_STATE_UNINITIATED;
  798. spin_unlock_bh(&aq->lock);
  799. }
  800. void ap_queue_init_state(struct ap_queue *aq)
  801. {
  802. spin_lock_bh(&aq->lock);
  803. aq->dev_state = AP_DEV_STATE_OPERATING;
  804. aq->sm_state = AP_SM_STATE_RESET_START;
  805. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  806. spin_unlock_bh(&aq->lock);
  807. }
  808. EXPORT_SYMBOL(ap_queue_init_state);