ap_queue.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2016, 2023
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  5. *
  6. * Adjunct processor bus, queue related code.
  7. */
  8. #define pr_fmt(fmt) "ap: " fmt
  9. #include <linux/export.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <asm/facility.h>
  13. #define CREATE_TRACE_POINTS
  14. #include <asm/trace/ap.h>
  15. #include "ap_bus.h"
  16. #include "ap_debug.h"
  17. EXPORT_TRACEPOINT_SYMBOL(s390_ap_nqap);
  18. EXPORT_TRACEPOINT_SYMBOL(s390_ap_dqap);
  19. static void __ap_flush_queue(struct ap_queue *aq);
  20. /*
  21. * some AP queue helper functions
  22. */
  23. static inline bool ap_q_supported_in_se(struct ap_queue *aq)
  24. {
  25. return aq->card->hwinfo.ep11 || aq->card->hwinfo.accel;
  26. }
  27. static inline bool ap_q_supports_bind(struct ap_queue *aq)
  28. {
  29. return aq->card->hwinfo.ep11 || aq->card->hwinfo.accel;
  30. }
  31. static inline bool ap_q_supports_assoc(struct ap_queue *aq)
  32. {
  33. return aq->card->hwinfo.ep11;
  34. }
  35. static inline bool ap_q_needs_bind(struct ap_queue *aq)
  36. {
  37. return ap_q_supports_bind(aq) && ap_sb_available();
  38. }
  39. /**
  40. * ap_queue_enable_irq(): Enable interrupt support on this AP queue.
  41. * @aq: The AP queue
  42. * @ind: the notification indicator byte
  43. *
  44. * Enables interruption on AP queue via ap_aqic(). Based on the return
  45. * value it waits a while and tests the AP queue if interrupts
  46. * have been switched on using ap_test_queue().
  47. */
  48. static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
  49. {
  50. union ap_qirq_ctrl qirqctrl = { .value = 0 };
  51. struct ap_queue_status status;
  52. qirqctrl.ir = 1;
  53. qirqctrl.isc = AP_ISC;
  54. status = ap_aqic(aq->qid, qirqctrl, virt_to_phys(ind));
  55. if (status.async)
  56. return -EPERM;
  57. switch (status.response_code) {
  58. case AP_RESPONSE_NORMAL:
  59. case AP_RESPONSE_OTHERWISE_CHANGED:
  60. return 0;
  61. case AP_RESPONSE_Q_NOT_AVAIL:
  62. case AP_RESPONSE_DECONFIGURED:
  63. case AP_RESPONSE_CHECKSTOPPED:
  64. case AP_RESPONSE_INVALID_ADDRESS:
  65. pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
  66. AP_QID_CARD(aq->qid),
  67. AP_QID_QUEUE(aq->qid));
  68. return -EOPNOTSUPP;
  69. case AP_RESPONSE_RESET_IN_PROGRESS:
  70. case AP_RESPONSE_BUSY:
  71. default:
  72. return -EBUSY;
  73. }
  74. }
  75. /**
  76. * __ap_send(): Send message to adjunct processor queue.
  77. * @qid: The AP queue number
  78. * @psmid: The program supplied message identifier
  79. * @msg: The message text
  80. * @msglen: The message length
  81. * @special: Special Bit
  82. *
  83. * Returns AP queue status structure.
  84. * Condition code 1 on NQAP can't happen because the L bit is 1.
  85. * Condition code 2 on NQAP also means the send is incomplete,
  86. * because a segment boundary was reached. The NQAP is repeated.
  87. */
  88. static inline struct ap_queue_status
  89. __ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen,
  90. int special)
  91. {
  92. struct ap_queue_status status;
  93. if (special)
  94. qid |= 0x400000UL;
  95. status = ap_nqap(qid, psmid, msg, msglen);
  96. trace_s390_ap_nqap(AP_QID_CARD(qid), AP_QID_QUEUE(qid),
  97. status.value, psmid);
  98. return status;
  99. }
  100. /* State machine definitions and helpers */
  101. static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq)
  102. {
  103. return AP_SM_WAIT_NONE;
  104. }
  105. /**
  106. * ap_sm_recv(): Receive pending reply messages from an AP queue but do
  107. * not change the state of the device.
  108. * @aq: pointer to the AP queue
  109. *
  110. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  111. */
  112. static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
  113. {
  114. struct ap_queue_status status;
  115. struct ap_message *ap_msg;
  116. bool found = false;
  117. size_t reslen;
  118. unsigned long resgr0 = 0;
  119. int parts = 0;
  120. /*
  121. * DQAP loop until response code and resgr0 indicate that
  122. * the msg is totally received. As we use the very same buffer
  123. * the msg is overwritten with each invocation. That's intended
  124. * and the receiver of the msg is informed with a msg rc code
  125. * of EMSGSIZE in such a case.
  126. */
  127. do {
  128. status = ap_dqap(aq->qid, &aq->reply->psmid,
  129. aq->reply->msg, aq->reply->bufsize,
  130. &aq->reply->len, &reslen, &resgr0);
  131. parts++;
  132. } while (status.response_code == 0xFF && resgr0 != 0);
  133. trace_s390_ap_dqap(AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid),
  134. status.value, aq->reply->psmid);
  135. switch (status.response_code) {
  136. case AP_RESPONSE_NORMAL:
  137. print_hex_dump_debug("aprpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
  138. aq->reply->msg, aq->reply->len, false);
  139. aq->queue_count = max_t(int, 0, aq->queue_count - 1);
  140. if (!status.queue_empty && !aq->queue_count)
  141. aq->queue_count++;
  142. if (aq->queue_count > 0)
  143. mod_timer(&aq->timeout,
  144. jiffies + aq->request_timeout);
  145. list_for_each_entry(ap_msg, &aq->pendingq, list) {
  146. if (ap_msg->psmid != aq->reply->psmid)
  147. continue;
  148. list_del_init(&ap_msg->list);
  149. aq->pendingq_count--;
  150. if (parts > 1) {
  151. ap_msg->rc = -EMSGSIZE;
  152. ap_msg->receive(aq, ap_msg, NULL);
  153. } else {
  154. ap_msg->receive(aq, ap_msg, aq->reply);
  155. }
  156. found = true;
  157. break;
  158. }
  159. if (!found) {
  160. AP_DBF_WARN("%s unassociated reply psmid=0x%016lx on 0x%02x.%04x\n",
  161. __func__, aq->reply->psmid,
  162. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  163. }
  164. fallthrough;
  165. case AP_RESPONSE_NO_PENDING_REPLY:
  166. if (!status.queue_empty || aq->queue_count <= 0)
  167. break;
  168. /* The card shouldn't forget requests but who knows. */
  169. aq->queue_count = 0;
  170. list_splice_init(&aq->pendingq, &aq->requestq);
  171. aq->requestq_count += aq->pendingq_count;
  172. pr_debug("queue 0x%02x.%04x rescheduled %d reqs (new req %d)\n",
  173. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid),
  174. aq->pendingq_count, aq->requestq_count);
  175. aq->pendingq_count = 0;
  176. break;
  177. default:
  178. break;
  179. }
  180. return status;
  181. }
  182. /**
  183. * ap_sm_read(): Receive pending reply messages from an AP queue.
  184. * @aq: pointer to the AP queue
  185. *
  186. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  187. */
  188. static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
  189. {
  190. struct ap_queue_status status;
  191. if (!aq->reply)
  192. return AP_SM_WAIT_NONE;
  193. status = ap_sm_recv(aq);
  194. if (status.async)
  195. return AP_SM_WAIT_NONE;
  196. switch (status.response_code) {
  197. case AP_RESPONSE_NORMAL:
  198. if (aq->queue_count > 0) {
  199. aq->sm_state = AP_SM_STATE_WORKING;
  200. return AP_SM_WAIT_AGAIN;
  201. }
  202. aq->sm_state = AP_SM_STATE_IDLE;
  203. break;
  204. case AP_RESPONSE_NO_PENDING_REPLY:
  205. if (aq->queue_count > 0)
  206. return status.irq_enabled ?
  207. AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_HIGH_TIMEOUT;
  208. aq->sm_state = AP_SM_STATE_IDLE;
  209. break;
  210. default:
  211. aq->dev_state = AP_DEV_STATE_ERROR;
  212. aq->last_err_rc = status.response_code;
  213. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  214. __func__, status.response_code,
  215. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  216. return AP_SM_WAIT_NONE;
  217. }
  218. /* Check and maybe enable irq support (again) on this queue */
  219. if (!status.irq_enabled && status.queue_empty) {
  220. void *lsi_ptr = ap_airq_ptr();
  221. if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0) {
  222. aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
  223. return AP_SM_WAIT_AGAIN;
  224. }
  225. }
  226. return AP_SM_WAIT_NONE;
  227. }
  228. /**
  229. * ap_sm_write(): Send messages from the request queue to an AP queue.
  230. * @aq: pointer to the AP queue
  231. *
  232. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  233. */
  234. static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
  235. {
  236. struct ap_queue_status status;
  237. struct ap_message *ap_msg;
  238. ap_qid_t qid = aq->qid;
  239. if (aq->requestq_count <= 0)
  240. return AP_SM_WAIT_NONE;
  241. /* Start the next request on the queue. */
  242. ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
  243. print_hex_dump_debug("apreq: ", DUMP_PREFIX_ADDRESS, 16, 1,
  244. ap_msg->msg, ap_msg->len, false);
  245. status = __ap_send(qid, ap_msg->psmid,
  246. ap_msg->msg, ap_msg->len,
  247. ap_msg->flags & AP_MSG_FLAG_SPECIAL);
  248. if (status.async)
  249. return AP_SM_WAIT_NONE;
  250. switch (status.response_code) {
  251. case AP_RESPONSE_NORMAL:
  252. aq->queue_count = max_t(int, 1, aq->queue_count + 1);
  253. if (aq->queue_count == 1)
  254. mod_timer(&aq->timeout, jiffies + aq->request_timeout);
  255. list_move_tail(&ap_msg->list, &aq->pendingq);
  256. aq->requestq_count--;
  257. aq->pendingq_count++;
  258. if (aq->queue_count < aq->card->hwinfo.qd + 1) {
  259. aq->sm_state = AP_SM_STATE_WORKING;
  260. return AP_SM_WAIT_AGAIN;
  261. }
  262. fallthrough;
  263. case AP_RESPONSE_Q_FULL:
  264. aq->sm_state = AP_SM_STATE_QUEUE_FULL;
  265. return status.irq_enabled ?
  266. AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_HIGH_TIMEOUT;
  267. case AP_RESPONSE_RESET_IN_PROGRESS:
  268. aq->sm_state = AP_SM_STATE_RESET_WAIT;
  269. return AP_SM_WAIT_LOW_TIMEOUT;
  270. case AP_RESPONSE_INVALID_DOMAIN:
  271. AP_DBF_WARN("%s RESPONSE_INVALID_DOMAIN on NQAP\n", __func__);
  272. fallthrough;
  273. case AP_RESPONSE_MESSAGE_TOO_BIG:
  274. case AP_RESPONSE_REQ_FAC_NOT_INST:
  275. list_del_init(&ap_msg->list);
  276. aq->requestq_count--;
  277. ap_msg->rc = -EINVAL;
  278. ap_msg->receive(aq, ap_msg, NULL);
  279. return AP_SM_WAIT_AGAIN;
  280. default:
  281. aq->dev_state = AP_DEV_STATE_ERROR;
  282. aq->last_err_rc = status.response_code;
  283. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  284. __func__, status.response_code,
  285. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  286. return AP_SM_WAIT_NONE;
  287. }
  288. }
  289. /**
  290. * ap_sm_read_write(): Send and receive messages to/from an AP queue.
  291. * @aq: pointer to the AP queue
  292. *
  293. * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
  294. */
  295. static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
  296. {
  297. return min(ap_sm_read(aq), ap_sm_write(aq));
  298. }
  299. /**
  300. * ap_sm_reset(): Reset an AP queue.
  301. * @aq: The AP queue
  302. *
  303. * Submit the Reset command to an AP queue.
  304. */
  305. static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
  306. {
  307. struct ap_queue_status status;
  308. status = ap_rapq(aq->qid, aq->rapq_fbit);
  309. if (status.async)
  310. return AP_SM_WAIT_NONE;
  311. switch (status.response_code) {
  312. case AP_RESPONSE_NORMAL:
  313. case AP_RESPONSE_RESET_IN_PROGRESS:
  314. aq->sm_state = AP_SM_STATE_RESET_WAIT;
  315. aq->rapq_fbit = 0;
  316. return AP_SM_WAIT_LOW_TIMEOUT;
  317. default:
  318. aq->dev_state = AP_DEV_STATE_ERROR;
  319. aq->last_err_rc = status.response_code;
  320. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  321. __func__, status.response_code,
  322. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  323. return AP_SM_WAIT_NONE;
  324. }
  325. }
  326. /**
  327. * ap_sm_reset_wait(): Test queue for completion of the reset operation
  328. * @aq: pointer to the AP queue
  329. *
  330. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  331. */
  332. static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
  333. {
  334. struct ap_queue_status status;
  335. struct ap_tapq_hwinfo hwinfo;
  336. void *lsi_ptr;
  337. /* Get the status with TAPQ */
  338. status = ap_test_queue(aq->qid, 1, &hwinfo);
  339. switch (status.response_code) {
  340. case AP_RESPONSE_NORMAL:
  341. aq->se_bstate = hwinfo.bs;
  342. lsi_ptr = ap_airq_ptr();
  343. if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
  344. aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
  345. else
  346. aq->sm_state = (aq->queue_count > 0) ?
  347. AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
  348. return AP_SM_WAIT_AGAIN;
  349. case AP_RESPONSE_BUSY:
  350. case AP_RESPONSE_RESET_IN_PROGRESS:
  351. return AP_SM_WAIT_LOW_TIMEOUT;
  352. case AP_RESPONSE_Q_NOT_AVAIL:
  353. case AP_RESPONSE_DECONFIGURED:
  354. case AP_RESPONSE_CHECKSTOPPED:
  355. default:
  356. aq->dev_state = AP_DEV_STATE_ERROR;
  357. aq->last_err_rc = status.response_code;
  358. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  359. __func__, status.response_code,
  360. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  361. return AP_SM_WAIT_NONE;
  362. }
  363. }
  364. /**
  365. * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
  366. * @aq: pointer to the AP queue
  367. *
  368. * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
  369. */
  370. static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
  371. {
  372. struct ap_queue_status status;
  373. if (aq->queue_count > 0 && aq->reply)
  374. /* Try to read a completed message and get the status */
  375. status = ap_sm_recv(aq);
  376. else
  377. /* Get the status with TAPQ */
  378. status = ap_tapq(aq->qid, NULL);
  379. if (status.irq_enabled == 1) {
  380. /* Irqs are now enabled */
  381. aq->sm_state = (aq->queue_count > 0) ?
  382. AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
  383. }
  384. switch (status.response_code) {
  385. case AP_RESPONSE_NORMAL:
  386. if (aq->queue_count > 0)
  387. return AP_SM_WAIT_AGAIN;
  388. fallthrough;
  389. case AP_RESPONSE_NO_PENDING_REPLY:
  390. return AP_SM_WAIT_LOW_TIMEOUT;
  391. default:
  392. aq->dev_state = AP_DEV_STATE_ERROR;
  393. aq->last_err_rc = status.response_code;
  394. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  395. __func__, status.response_code,
  396. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  397. return AP_SM_WAIT_NONE;
  398. }
  399. }
  400. /**
  401. * ap_sm_assoc_wait(): Test queue for completion of a pending
  402. * association request.
  403. * @aq: pointer to the AP queue
  404. */
  405. static enum ap_sm_wait ap_sm_assoc_wait(struct ap_queue *aq)
  406. {
  407. struct ap_queue_status status;
  408. struct ap_tapq_hwinfo hwinfo;
  409. status = ap_test_queue(aq->qid, 1, &hwinfo);
  410. /* handle asynchronous error on this queue */
  411. if (status.async && status.response_code) {
  412. aq->dev_state = AP_DEV_STATE_ERROR;
  413. aq->last_err_rc = status.response_code;
  414. AP_DBF_WARN("%s asynch RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  415. __func__, status.response_code,
  416. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  417. return AP_SM_WAIT_NONE;
  418. }
  419. if (status.response_code > AP_RESPONSE_BUSY) {
  420. aq->dev_state = AP_DEV_STATE_ERROR;
  421. aq->last_err_rc = status.response_code;
  422. AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  423. __func__, status.response_code,
  424. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  425. return AP_SM_WAIT_NONE;
  426. }
  427. /* update queue's SE bind state */
  428. aq->se_bstate = hwinfo.bs;
  429. /* check bs bits */
  430. switch (hwinfo.bs) {
  431. case AP_BS_Q_USABLE:
  432. /* association is through */
  433. aq->sm_state = AP_SM_STATE_IDLE;
  434. pr_debug("queue 0x%02x.%04x associated with %u\n",
  435. AP_QID_CARD(aq->qid),
  436. AP_QID_QUEUE(aq->qid), aq->assoc_idx);
  437. return AP_SM_WAIT_NONE;
  438. case AP_BS_Q_USABLE_NO_SECURE_KEY:
  439. /* association still pending */
  440. return AP_SM_WAIT_LOW_TIMEOUT;
  441. default:
  442. /* reset from 'outside' happened or no idea at all */
  443. aq->assoc_idx = ASSOC_IDX_INVALID;
  444. aq->dev_state = AP_DEV_STATE_ERROR;
  445. aq->last_err_rc = status.response_code;
  446. AP_DBF_WARN("%s bs 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
  447. __func__, hwinfo.bs,
  448. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  449. return AP_SM_WAIT_NONE;
  450. }
  451. }
  452. /*
  453. * AP state machine jump table
  454. */
  455. static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
  456. [AP_SM_STATE_RESET_START] = {
  457. [AP_SM_EVENT_POLL] = ap_sm_reset,
  458. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  459. },
  460. [AP_SM_STATE_RESET_WAIT] = {
  461. [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
  462. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  463. },
  464. [AP_SM_STATE_SETIRQ_WAIT] = {
  465. [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
  466. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  467. },
  468. [AP_SM_STATE_IDLE] = {
  469. [AP_SM_EVENT_POLL] = ap_sm_write,
  470. [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
  471. },
  472. [AP_SM_STATE_WORKING] = {
  473. [AP_SM_EVENT_POLL] = ap_sm_read_write,
  474. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  475. },
  476. [AP_SM_STATE_QUEUE_FULL] = {
  477. [AP_SM_EVENT_POLL] = ap_sm_read,
  478. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  479. },
  480. [AP_SM_STATE_ASSOC_WAIT] = {
  481. [AP_SM_EVENT_POLL] = ap_sm_assoc_wait,
  482. [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
  483. },
  484. };
  485. enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
  486. {
  487. if (aq->config && !aq->chkstop &&
  488. aq->dev_state > AP_DEV_STATE_UNINITIATED)
  489. return ap_jumptable[aq->sm_state][event](aq);
  490. else
  491. return AP_SM_WAIT_NONE;
  492. }
  493. enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
  494. {
  495. enum ap_sm_wait wait;
  496. while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
  497. ;
  498. return wait;
  499. }
  500. /*
  501. * AP queue related attributes.
  502. */
  503. static ssize_t request_count_show(struct device *dev,
  504. struct device_attribute *attr,
  505. char *buf)
  506. {
  507. struct ap_queue *aq = to_ap_queue(dev);
  508. bool valid = false;
  509. u64 req_cnt;
  510. spin_lock_bh(&aq->lock);
  511. if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
  512. req_cnt = aq->total_request_count;
  513. valid = true;
  514. }
  515. spin_unlock_bh(&aq->lock);
  516. if (valid)
  517. return sysfs_emit(buf, "%llu\n", req_cnt);
  518. else
  519. return sysfs_emit(buf, "-\n");
  520. }
  521. static ssize_t request_count_store(struct device *dev,
  522. struct device_attribute *attr,
  523. const char *buf, size_t count)
  524. {
  525. struct ap_queue *aq = to_ap_queue(dev);
  526. spin_lock_bh(&aq->lock);
  527. aq->total_request_count = 0;
  528. spin_unlock_bh(&aq->lock);
  529. return count;
  530. }
  531. static DEVICE_ATTR_RW(request_count);
  532. static ssize_t requestq_count_show(struct device *dev,
  533. struct device_attribute *attr, char *buf)
  534. {
  535. struct ap_queue *aq = to_ap_queue(dev);
  536. unsigned int reqq_cnt = 0;
  537. spin_lock_bh(&aq->lock);
  538. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  539. reqq_cnt = aq->requestq_count;
  540. spin_unlock_bh(&aq->lock);
  541. return sysfs_emit(buf, "%d\n", reqq_cnt);
  542. }
  543. static DEVICE_ATTR_RO(requestq_count);
  544. static ssize_t pendingq_count_show(struct device *dev,
  545. struct device_attribute *attr, char *buf)
  546. {
  547. struct ap_queue *aq = to_ap_queue(dev);
  548. unsigned int penq_cnt = 0;
  549. spin_lock_bh(&aq->lock);
  550. if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
  551. penq_cnt = aq->pendingq_count;
  552. spin_unlock_bh(&aq->lock);
  553. return sysfs_emit(buf, "%d\n", penq_cnt);
  554. }
  555. static DEVICE_ATTR_RO(pendingq_count);
  556. static ssize_t reset_show(struct device *dev,
  557. struct device_attribute *attr, char *buf)
  558. {
  559. struct ap_queue *aq = to_ap_queue(dev);
  560. int rc = 0;
  561. spin_lock_bh(&aq->lock);
  562. switch (aq->sm_state) {
  563. case AP_SM_STATE_RESET_START:
  564. case AP_SM_STATE_RESET_WAIT:
  565. rc = sysfs_emit(buf, "Reset in progress.\n");
  566. break;
  567. case AP_SM_STATE_WORKING:
  568. case AP_SM_STATE_QUEUE_FULL:
  569. rc = sysfs_emit(buf, "Reset Timer armed.\n");
  570. break;
  571. default:
  572. rc = sysfs_emit(buf, "No Reset Timer set.\n");
  573. }
  574. spin_unlock_bh(&aq->lock);
  575. return rc;
  576. }
  577. static ssize_t reset_store(struct device *dev,
  578. struct device_attribute *attr,
  579. const char *buf, size_t count)
  580. {
  581. struct ap_queue *aq = to_ap_queue(dev);
  582. spin_lock_bh(&aq->lock);
  583. __ap_flush_queue(aq);
  584. aq->sm_state = AP_SM_STATE_RESET_START;
  585. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  586. spin_unlock_bh(&aq->lock);
  587. AP_DBF_INFO("%s reset queue=%02x.%04x triggered by user\n",
  588. __func__, AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  589. return count;
  590. }
  591. static DEVICE_ATTR_RW(reset);
  592. static ssize_t interrupt_show(struct device *dev,
  593. struct device_attribute *attr, char *buf)
  594. {
  595. struct ap_queue *aq = to_ap_queue(dev);
  596. struct ap_queue_status status;
  597. int rc = 0;
  598. spin_lock_bh(&aq->lock);
  599. if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT) {
  600. rc = sysfs_emit(buf, "Enable Interrupt pending.\n");
  601. } else {
  602. status = ap_tapq(aq->qid, NULL);
  603. if (status.irq_enabled)
  604. rc = sysfs_emit(buf, "Interrupts enabled.\n");
  605. else
  606. rc = sysfs_emit(buf, "Interrupts disabled.\n");
  607. }
  608. spin_unlock_bh(&aq->lock);
  609. return rc;
  610. }
  611. static DEVICE_ATTR_RO(interrupt);
  612. static ssize_t config_show(struct device *dev,
  613. struct device_attribute *attr, char *buf)
  614. {
  615. struct ap_queue *aq = to_ap_queue(dev);
  616. int rc;
  617. spin_lock_bh(&aq->lock);
  618. rc = sysfs_emit(buf, "%d\n", aq->config ? 1 : 0);
  619. spin_unlock_bh(&aq->lock);
  620. return rc;
  621. }
  622. static DEVICE_ATTR_RO(config);
  623. static ssize_t chkstop_show(struct device *dev,
  624. struct device_attribute *attr, char *buf)
  625. {
  626. struct ap_queue *aq = to_ap_queue(dev);
  627. int rc;
  628. spin_lock_bh(&aq->lock);
  629. rc = sysfs_emit(buf, "%d\n", aq->chkstop ? 1 : 0);
  630. spin_unlock_bh(&aq->lock);
  631. return rc;
  632. }
  633. static DEVICE_ATTR_RO(chkstop);
  634. static ssize_t ap_functions_show(struct device *dev,
  635. struct device_attribute *attr, char *buf)
  636. {
  637. struct ap_queue *aq = to_ap_queue(dev);
  638. struct ap_queue_status status;
  639. struct ap_tapq_hwinfo hwinfo;
  640. status = ap_test_queue(aq->qid, 1, &hwinfo);
  641. if (status.response_code > AP_RESPONSE_BUSY) {
  642. pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n",
  643. status.response_code,
  644. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  645. return -EIO;
  646. }
  647. return sysfs_emit(buf, "0x%08X\n", hwinfo.fac);
  648. }
  649. static DEVICE_ATTR_RO(ap_functions);
  650. static ssize_t driver_override_show(struct device *dev,
  651. struct device_attribute *attr,
  652. char *buf)
  653. {
  654. struct ap_queue *aq = to_ap_queue(dev);
  655. struct ap_device *ap_dev = &aq->ap_dev;
  656. int rc;
  657. device_lock(dev);
  658. if (ap_dev->driver_override)
  659. rc = sysfs_emit(buf, "%s\n", ap_dev->driver_override);
  660. else
  661. rc = sysfs_emit(buf, "\n");
  662. device_unlock(dev);
  663. return rc;
  664. }
  665. static ssize_t driver_override_store(struct device *dev,
  666. struct device_attribute *attr,
  667. const char *buf, size_t count)
  668. {
  669. struct ap_queue *aq = to_ap_queue(dev);
  670. struct ap_device *ap_dev = &aq->ap_dev;
  671. int rc = -EINVAL;
  672. bool old_value;
  673. if (mutex_lock_interruptible(&ap_attr_mutex))
  674. return -ERESTARTSYS;
  675. /* Do not allow driver override if apmask/aqmask is in use */
  676. if (ap_apmask_aqmask_in_use)
  677. goto out;
  678. old_value = ap_dev->driver_override ? true : false;
  679. rc = driver_set_override(dev, &ap_dev->driver_override, buf, count);
  680. if (rc)
  681. goto out;
  682. if (old_value && !ap_dev->driver_override)
  683. --ap_driver_override_ctr;
  684. else if (!old_value && ap_dev->driver_override)
  685. ++ap_driver_override_ctr;
  686. rc = count;
  687. out:
  688. mutex_unlock(&ap_attr_mutex);
  689. return rc;
  690. }
  691. static DEVICE_ATTR_RW(driver_override);
  692. #ifdef CONFIG_AP_DEBUG
  693. static ssize_t states_show(struct device *dev,
  694. struct device_attribute *attr, char *buf)
  695. {
  696. struct ap_queue *aq = to_ap_queue(dev);
  697. int rc = 0;
  698. spin_lock_bh(&aq->lock);
  699. /* queue device state */
  700. switch (aq->dev_state) {
  701. case AP_DEV_STATE_UNINITIATED:
  702. rc = sysfs_emit(buf, "UNINITIATED\n");
  703. break;
  704. case AP_DEV_STATE_OPERATING:
  705. rc = sysfs_emit(buf, "OPERATING");
  706. break;
  707. case AP_DEV_STATE_SHUTDOWN:
  708. rc = sysfs_emit(buf, "SHUTDOWN");
  709. break;
  710. case AP_DEV_STATE_ERROR:
  711. rc = sysfs_emit(buf, "ERROR");
  712. break;
  713. default:
  714. rc = sysfs_emit(buf, "UNKNOWN");
  715. }
  716. /* state machine state */
  717. if (aq->dev_state) {
  718. switch (aq->sm_state) {
  719. case AP_SM_STATE_RESET_START:
  720. rc += sysfs_emit_at(buf, rc, " [RESET_START]\n");
  721. break;
  722. case AP_SM_STATE_RESET_WAIT:
  723. rc += sysfs_emit_at(buf, rc, " [RESET_WAIT]\n");
  724. break;
  725. case AP_SM_STATE_SETIRQ_WAIT:
  726. rc += sysfs_emit_at(buf, rc, " [SETIRQ_WAIT]\n");
  727. break;
  728. case AP_SM_STATE_IDLE:
  729. rc += sysfs_emit_at(buf, rc, " [IDLE]\n");
  730. break;
  731. case AP_SM_STATE_WORKING:
  732. rc += sysfs_emit_at(buf, rc, " [WORKING]\n");
  733. break;
  734. case AP_SM_STATE_QUEUE_FULL:
  735. rc += sysfs_emit_at(buf, rc, " [FULL]\n");
  736. break;
  737. case AP_SM_STATE_ASSOC_WAIT:
  738. rc += sysfs_emit_at(buf, rc, " [ASSOC_WAIT]\n");
  739. break;
  740. default:
  741. rc += sysfs_emit_at(buf, rc, " [UNKNOWN]\n");
  742. }
  743. }
  744. spin_unlock_bh(&aq->lock);
  745. return rc;
  746. }
  747. static DEVICE_ATTR_RO(states);
  748. static ssize_t last_err_rc_show(struct device *dev,
  749. struct device_attribute *attr, char *buf)
  750. {
  751. struct ap_queue *aq = to_ap_queue(dev);
  752. int rc;
  753. spin_lock_bh(&aq->lock);
  754. rc = aq->last_err_rc;
  755. spin_unlock_bh(&aq->lock);
  756. switch (rc) {
  757. case AP_RESPONSE_NORMAL:
  758. return sysfs_emit(buf, "NORMAL\n");
  759. case AP_RESPONSE_Q_NOT_AVAIL:
  760. return sysfs_emit(buf, "Q_NOT_AVAIL\n");
  761. case AP_RESPONSE_RESET_IN_PROGRESS:
  762. return sysfs_emit(buf, "RESET_IN_PROGRESS\n");
  763. case AP_RESPONSE_DECONFIGURED:
  764. return sysfs_emit(buf, "DECONFIGURED\n");
  765. case AP_RESPONSE_CHECKSTOPPED:
  766. return sysfs_emit(buf, "CHECKSTOPPED\n");
  767. case AP_RESPONSE_BUSY:
  768. return sysfs_emit(buf, "BUSY\n");
  769. case AP_RESPONSE_INVALID_ADDRESS:
  770. return sysfs_emit(buf, "INVALID_ADDRESS\n");
  771. case AP_RESPONSE_OTHERWISE_CHANGED:
  772. return sysfs_emit(buf, "OTHERWISE_CHANGED\n");
  773. case AP_RESPONSE_Q_FULL:
  774. return sysfs_emit(buf, "Q_FULL/NO_PENDING_REPLY\n");
  775. case AP_RESPONSE_INDEX_TOO_BIG:
  776. return sysfs_emit(buf, "INDEX_TOO_BIG\n");
  777. case AP_RESPONSE_NO_FIRST_PART:
  778. return sysfs_emit(buf, "NO_FIRST_PART\n");
  779. case AP_RESPONSE_MESSAGE_TOO_BIG:
  780. return sysfs_emit(buf, "MESSAGE_TOO_BIG\n");
  781. case AP_RESPONSE_REQ_FAC_NOT_INST:
  782. return sysfs_emit(buf, "REQ_FAC_NOT_INST\n");
  783. default:
  784. return sysfs_emit(buf, "response code %d\n", rc);
  785. }
  786. }
  787. static DEVICE_ATTR_RO(last_err_rc);
  788. #endif
  789. static struct attribute *ap_queue_dev_attrs[] = {
  790. &dev_attr_request_count.attr,
  791. &dev_attr_requestq_count.attr,
  792. &dev_attr_pendingq_count.attr,
  793. &dev_attr_reset.attr,
  794. &dev_attr_interrupt.attr,
  795. &dev_attr_config.attr,
  796. &dev_attr_chkstop.attr,
  797. &dev_attr_ap_functions.attr,
  798. &dev_attr_driver_override.attr,
  799. #ifdef CONFIG_AP_DEBUG
  800. &dev_attr_states.attr,
  801. &dev_attr_last_err_rc.attr,
  802. #endif
  803. NULL
  804. };
  805. static struct attribute_group ap_queue_dev_attr_group = {
  806. .attrs = ap_queue_dev_attrs
  807. };
  808. static const struct attribute_group *ap_queue_dev_attr_groups[] = {
  809. &ap_queue_dev_attr_group,
  810. NULL
  811. };
  812. static struct device_type ap_queue_type = {
  813. .name = "ap_queue",
  814. .groups = ap_queue_dev_attr_groups,
  815. };
  816. static ssize_t se_bind_show(struct device *dev,
  817. struct device_attribute *attr, char *buf)
  818. {
  819. struct ap_queue *aq = to_ap_queue(dev);
  820. struct ap_queue_status status;
  821. struct ap_tapq_hwinfo hwinfo;
  822. if (!ap_q_supports_bind(aq))
  823. return sysfs_emit(buf, "-\n");
  824. status = ap_test_queue(aq->qid, 1, &hwinfo);
  825. if (status.response_code > AP_RESPONSE_BUSY) {
  826. pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n",
  827. status.response_code,
  828. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  829. return -EIO;
  830. }
  831. /* update queue's SE bind state */
  832. spin_lock_bh(&aq->lock);
  833. aq->se_bstate = hwinfo.bs;
  834. spin_unlock_bh(&aq->lock);
  835. switch (hwinfo.bs) {
  836. case AP_BS_Q_USABLE:
  837. case AP_BS_Q_USABLE_NO_SECURE_KEY:
  838. return sysfs_emit(buf, "bound\n");
  839. default:
  840. return sysfs_emit(buf, "unbound\n");
  841. }
  842. }
  843. static ssize_t se_bind_store(struct device *dev,
  844. struct device_attribute *attr,
  845. const char *buf, size_t count)
  846. {
  847. struct ap_queue *aq = to_ap_queue(dev);
  848. struct ap_queue_status status;
  849. struct ap_tapq_hwinfo hwinfo;
  850. bool value;
  851. int rc;
  852. if (!ap_q_supports_bind(aq))
  853. return -EINVAL;
  854. /* only 0 (unbind) and 1 (bind) allowed */
  855. rc = kstrtobool(buf, &value);
  856. if (rc)
  857. return rc;
  858. if (!value) {
  859. /* Unbind. Set F bit arg and trigger RAPQ */
  860. spin_lock_bh(&aq->lock);
  861. __ap_flush_queue(aq);
  862. aq->rapq_fbit = 1;
  863. _ap_queue_init_state(aq);
  864. rc = count;
  865. goto out;
  866. }
  867. /* Bind. Check current SE bind state */
  868. status = ap_test_queue(aq->qid, 1, &hwinfo);
  869. if (status.response_code) {
  870. AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n",
  871. __func__, status.response_code,
  872. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  873. return -EIO;
  874. }
  875. /* Update BS state */
  876. spin_lock_bh(&aq->lock);
  877. aq->se_bstate = hwinfo.bs;
  878. if (hwinfo.bs != AP_BS_Q_AVAIL_FOR_BINDING) {
  879. AP_DBF_WARN("%s bind attempt with bs %d on queue 0x%02x.%04x\n",
  880. __func__, hwinfo.bs,
  881. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  882. rc = -EINVAL;
  883. goto out;
  884. }
  885. /* Check SM state */
  886. if (aq->sm_state < AP_SM_STATE_IDLE) {
  887. rc = -EBUSY;
  888. goto out;
  889. }
  890. /* invoke BAPQ */
  891. status = ap_bapq(aq->qid);
  892. if (status.response_code) {
  893. AP_DBF_WARN("%s RC 0x%02x on bapq(0x%02x.%04x)\n",
  894. __func__, status.response_code,
  895. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  896. rc = -EIO;
  897. goto out;
  898. }
  899. aq->assoc_idx = ASSOC_IDX_INVALID;
  900. /* verify SE bind state */
  901. status = ap_test_queue(aq->qid, 1, &hwinfo);
  902. if (status.response_code) {
  903. AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n",
  904. __func__, status.response_code,
  905. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  906. rc = -EIO;
  907. goto out;
  908. }
  909. aq->se_bstate = hwinfo.bs;
  910. if (!(hwinfo.bs == AP_BS_Q_USABLE ||
  911. hwinfo.bs == AP_BS_Q_USABLE_NO_SECURE_KEY)) {
  912. AP_DBF_WARN("%s BAPQ success, but bs shows %d on queue 0x%02x.%04x\n",
  913. __func__, hwinfo.bs,
  914. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  915. rc = -EIO;
  916. goto out;
  917. }
  918. /* SE bind was successful */
  919. AP_DBF_INFO("%s bapq(0x%02x.%04x) success\n", __func__,
  920. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  921. rc = count;
  922. out:
  923. spin_unlock_bh(&aq->lock);
  924. return rc;
  925. }
  926. static DEVICE_ATTR_RW(se_bind);
  927. static ssize_t se_associate_show(struct device *dev,
  928. struct device_attribute *attr, char *buf)
  929. {
  930. struct ap_queue *aq = to_ap_queue(dev);
  931. struct ap_queue_status status;
  932. struct ap_tapq_hwinfo hwinfo;
  933. if (!ap_q_supports_assoc(aq))
  934. return sysfs_emit(buf, "-\n");
  935. status = ap_test_queue(aq->qid, 1, &hwinfo);
  936. if (status.response_code > AP_RESPONSE_BUSY) {
  937. pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n",
  938. status.response_code,
  939. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  940. return -EIO;
  941. }
  942. /* update queue's SE bind state */
  943. spin_lock_bh(&aq->lock);
  944. aq->se_bstate = hwinfo.bs;
  945. spin_unlock_bh(&aq->lock);
  946. switch (hwinfo.bs) {
  947. case AP_BS_Q_USABLE:
  948. if (aq->assoc_idx == ASSOC_IDX_INVALID) {
  949. AP_DBF_WARN("%s AP_BS_Q_USABLE but invalid assoc_idx\n", __func__);
  950. return -EIO;
  951. }
  952. return sysfs_emit(buf, "associated %u\n", aq->assoc_idx);
  953. case AP_BS_Q_USABLE_NO_SECURE_KEY:
  954. if (aq->assoc_idx != ASSOC_IDX_INVALID)
  955. return sysfs_emit(buf, "association pending\n");
  956. fallthrough;
  957. default:
  958. return sysfs_emit(buf, "unassociated\n");
  959. }
  960. }
  961. static ssize_t se_associate_store(struct device *dev,
  962. struct device_attribute *attr,
  963. const char *buf, size_t count)
  964. {
  965. struct ap_queue *aq = to_ap_queue(dev);
  966. struct ap_queue_status status;
  967. struct ap_tapq_hwinfo hwinfo;
  968. unsigned int value;
  969. int rc;
  970. if (!ap_q_supports_assoc(aq))
  971. return -EINVAL;
  972. /* association index needs to be >= 0 */
  973. rc = kstrtouint(buf, 0, &value);
  974. if (rc)
  975. return rc;
  976. if (value >= ASSOC_IDX_INVALID)
  977. return -EINVAL;
  978. /* check current SE bind state */
  979. status = ap_test_queue(aq->qid, 1, &hwinfo);
  980. if (status.response_code) {
  981. AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n",
  982. __func__, status.response_code,
  983. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  984. return -EIO;
  985. }
  986. spin_lock_bh(&aq->lock);
  987. aq->se_bstate = hwinfo.bs;
  988. if (hwinfo.bs != AP_BS_Q_USABLE_NO_SECURE_KEY) {
  989. AP_DBF_WARN("%s association attempt with bs %d on queue 0x%02x.%04x\n",
  990. __func__, hwinfo.bs,
  991. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  992. rc = -EINVAL;
  993. goto out;
  994. }
  995. /* check SM state */
  996. if (aq->sm_state != AP_SM_STATE_IDLE) {
  997. rc = -EBUSY;
  998. goto out;
  999. }
  1000. /* trigger the asynchronous association request */
  1001. status = ap_aapq(aq->qid, value);
  1002. switch (status.response_code) {
  1003. case AP_RESPONSE_NORMAL:
  1004. case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
  1005. aq->sm_state = AP_SM_STATE_ASSOC_WAIT;
  1006. aq->assoc_idx = value;
  1007. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  1008. break;
  1009. default:
  1010. AP_DBF_WARN("%s RC 0x%02x on aapq(0x%02x.%04x)\n",
  1011. __func__, status.response_code,
  1012. AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
  1013. rc = -EIO;
  1014. goto out;
  1015. }
  1016. rc = count;
  1017. out:
  1018. spin_unlock_bh(&aq->lock);
  1019. return rc;
  1020. }
  1021. static DEVICE_ATTR_RW(se_associate);
  1022. static struct attribute *ap_queue_dev_sb_attrs[] = {
  1023. &dev_attr_se_bind.attr,
  1024. &dev_attr_se_associate.attr,
  1025. NULL
  1026. };
  1027. static struct attribute_group ap_queue_dev_sb_attr_group = {
  1028. .attrs = ap_queue_dev_sb_attrs
  1029. };
  1030. static const struct attribute_group *ap_queue_dev_sb_attr_groups[] = {
  1031. &ap_queue_dev_sb_attr_group,
  1032. NULL
  1033. };
  1034. static void ap_queue_device_release(struct device *dev)
  1035. {
  1036. struct ap_queue *aq = to_ap_queue(dev);
  1037. spin_lock_bh(&ap_queues_lock);
  1038. hash_del(&aq->hnode);
  1039. spin_unlock_bh(&ap_queues_lock);
  1040. kfree(aq);
  1041. }
  1042. struct ap_queue *ap_queue_create(ap_qid_t qid, struct ap_card *ac)
  1043. {
  1044. struct ap_queue *aq;
  1045. aq = kzalloc_obj(*aq);
  1046. if (!aq)
  1047. return NULL;
  1048. aq->card = ac;
  1049. aq->ap_dev.device.release = ap_queue_device_release;
  1050. aq->ap_dev.device.type = &ap_queue_type;
  1051. aq->ap_dev.device_type = ac->ap_dev.device_type;
  1052. /* in SE environment add bind/associate attributes group */
  1053. if (ap_is_se_guest() && ap_q_supported_in_se(aq))
  1054. aq->ap_dev.device.groups = ap_queue_dev_sb_attr_groups;
  1055. aq->qid = qid;
  1056. spin_lock_init(&aq->lock);
  1057. INIT_LIST_HEAD(&aq->pendingq);
  1058. INIT_LIST_HEAD(&aq->requestq);
  1059. timer_setup(&aq->timeout, ap_request_timeout, 0);
  1060. return aq;
  1061. }
  1062. void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
  1063. {
  1064. aq->reply = reply;
  1065. spin_lock_bh(&aq->lock);
  1066. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  1067. spin_unlock_bh(&aq->lock);
  1068. }
  1069. EXPORT_SYMBOL(ap_queue_init_reply);
  1070. /**
  1071. * ap_queue_message(): Queue a request to an AP device.
  1072. * @aq: The AP device to queue the message to
  1073. * @ap_msg: The message that is to be added
  1074. */
  1075. int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
  1076. {
  1077. int rc = 0;
  1078. /* msg needs to have a valid receive-callback */
  1079. BUG_ON(!ap_msg->receive);
  1080. spin_lock_bh(&aq->lock);
  1081. /* only allow to queue new messages if device state is ok */
  1082. if (aq->dev_state == AP_DEV_STATE_OPERATING) {
  1083. list_add_tail(&ap_msg->list, &aq->requestq);
  1084. aq->requestq_count++;
  1085. aq->total_request_count++;
  1086. atomic64_inc(&aq->card->total_request_count);
  1087. } else {
  1088. rc = -ENODEV;
  1089. }
  1090. /* Send/receive as many request from the queue as possible. */
  1091. ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
  1092. spin_unlock_bh(&aq->lock);
  1093. return rc;
  1094. }
  1095. EXPORT_SYMBOL(ap_queue_message);
  1096. /**
  1097. * ap_queue_usable(): Check if queue is usable just now.
  1098. * @aq: The AP queue device to test for usability.
  1099. * This function is intended for the scheduler to query if it makes
  1100. * sense to enqueue a message into this AP queue device by calling
  1101. * ap_queue_message(). The perspective is very short-term as the
  1102. * state machine and device state(s) may change at any time.
  1103. */
  1104. bool ap_queue_usable(struct ap_queue *aq)
  1105. {
  1106. bool rc = true;
  1107. spin_lock_bh(&aq->lock);
  1108. /* check for not configured or checkstopped */
  1109. if (!aq->config || aq->chkstop) {
  1110. rc = false;
  1111. goto unlock_and_out;
  1112. }
  1113. /* device state needs to be ok */
  1114. if (aq->dev_state != AP_DEV_STATE_OPERATING) {
  1115. rc = false;
  1116. goto unlock_and_out;
  1117. }
  1118. /* SE guest's queues additionally need to be bound */
  1119. if (ap_is_se_guest()) {
  1120. if (!ap_q_supported_in_se(aq)) {
  1121. rc = false;
  1122. goto unlock_and_out;
  1123. }
  1124. if (ap_q_needs_bind(aq) &&
  1125. !(aq->se_bstate == AP_BS_Q_USABLE ||
  1126. aq->se_bstate == AP_BS_Q_USABLE_NO_SECURE_KEY))
  1127. rc = false;
  1128. }
  1129. unlock_and_out:
  1130. spin_unlock_bh(&aq->lock);
  1131. return rc;
  1132. }
  1133. EXPORT_SYMBOL(ap_queue_usable);
  1134. /**
  1135. * ap_cancel_message(): Cancel a crypto request.
  1136. * @aq: The AP device that has the message queued
  1137. * @ap_msg: The message that is to be removed
  1138. *
  1139. * Cancel a crypto request. This is done by removing the request
  1140. * from the device pending or request queue. Note that the
  1141. * request stays on the AP queue. When it finishes the message
  1142. * reply will be discarded because the psmid can't be found.
  1143. */
  1144. void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
  1145. {
  1146. struct ap_message *tmp;
  1147. spin_lock_bh(&aq->lock);
  1148. if (!list_empty(&ap_msg->list)) {
  1149. list_for_each_entry(tmp, &aq->pendingq, list)
  1150. if (tmp->psmid == ap_msg->psmid) {
  1151. aq->pendingq_count--;
  1152. goto found;
  1153. }
  1154. aq->requestq_count--;
  1155. found:
  1156. list_del_init(&ap_msg->list);
  1157. }
  1158. spin_unlock_bh(&aq->lock);
  1159. }
  1160. EXPORT_SYMBOL(ap_cancel_message);
  1161. /**
  1162. * __ap_flush_queue(): Flush requests.
  1163. * @aq: Pointer to the AP queue
  1164. *
  1165. * Flush all requests from the request/pending queue of an AP device.
  1166. */
  1167. static void __ap_flush_queue(struct ap_queue *aq)
  1168. {
  1169. struct ap_message *ap_msg, *next;
  1170. list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
  1171. list_del_init(&ap_msg->list);
  1172. aq->pendingq_count--;
  1173. ap_msg->rc = -EAGAIN;
  1174. ap_msg->receive(aq, ap_msg, NULL);
  1175. }
  1176. list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
  1177. list_del_init(&ap_msg->list);
  1178. aq->requestq_count--;
  1179. ap_msg->rc = -EAGAIN;
  1180. ap_msg->receive(aq, ap_msg, NULL);
  1181. }
  1182. aq->queue_count = 0;
  1183. }
  1184. void ap_flush_queue(struct ap_queue *aq)
  1185. {
  1186. spin_lock_bh(&aq->lock);
  1187. __ap_flush_queue(aq);
  1188. spin_unlock_bh(&aq->lock);
  1189. }
  1190. EXPORT_SYMBOL(ap_flush_queue);
  1191. void ap_queue_prepare_remove(struct ap_queue *aq)
  1192. {
  1193. spin_lock_bh(&aq->lock);
  1194. /* flush queue */
  1195. __ap_flush_queue(aq);
  1196. /* move queue device state to SHUTDOWN in progress */
  1197. aq->dev_state = AP_DEV_STATE_SHUTDOWN;
  1198. spin_unlock_bh(&aq->lock);
  1199. timer_delete_sync(&aq->timeout);
  1200. }
  1201. void ap_queue_remove(struct ap_queue *aq)
  1202. {
  1203. /*
  1204. * all messages have been flushed and the device state
  1205. * is SHUTDOWN. Now reset with zero which also clears
  1206. * the irq registration and move the device state
  1207. * to the initial value AP_DEV_STATE_UNINITIATED.
  1208. */
  1209. spin_lock_bh(&aq->lock);
  1210. ap_zapq(aq->qid, 0);
  1211. aq->dev_state = AP_DEV_STATE_UNINITIATED;
  1212. spin_unlock_bh(&aq->lock);
  1213. }
  1214. void _ap_queue_init_state(struct ap_queue *aq)
  1215. {
  1216. aq->dev_state = AP_DEV_STATE_OPERATING;
  1217. aq->sm_state = AP_SM_STATE_RESET_START;
  1218. aq->last_err_rc = 0;
  1219. aq->assoc_idx = ASSOC_IDX_INVALID;
  1220. ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
  1221. }
  1222. void ap_queue_init_state(struct ap_queue *aq)
  1223. {
  1224. spin_lock_bh(&aq->lock);
  1225. _ap_queue_init_state(aq);
  1226. spin_unlock_bh(&aq->lock);
  1227. }
  1228. EXPORT_SYMBOL(ap_queue_init_state);