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