1
2
3
4
5
6
7
8#define KMSG_COMPONENT "ap"
9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <asm/facility.h>
14
15#include "ap_bus.h"
16#include "ap_asm.h"
17
18
19
20
21
22
23
24
25
26
27
28
29struct ap_queue_status ap_queue_irq_ctrl(ap_qid_t qid,
30 struct ap_qirq_ctrl qirqctrl,
31 void *ind)
32{
33 return ap_aqic(qid, qirqctrl, ind);
34}
35EXPORT_SYMBOL(ap_queue_irq_ctrl);
36
37
38
39
40
41
42
43
44
45
46static int ap_queue_enable_interruption(struct ap_queue *aq, void *ind)
47{
48 struct ap_queue_status status;
49 struct ap_qirq_ctrl qirqctrl = { 0 };
50
51 qirqctrl.ir = 1;
52 qirqctrl.isc = AP_ISC;
53 status = ap_aqic(aq->qid, qirqctrl, ind);
54 switch (status.response_code) {
55 case AP_RESPONSE_NORMAL:
56 case AP_RESPONSE_OTHERWISE_CHANGED:
57 return 0;
58 case AP_RESPONSE_Q_NOT_AVAIL:
59 case AP_RESPONSE_DECONFIGURED:
60 case AP_RESPONSE_CHECKSTOPPED:
61 case AP_RESPONSE_INVALID_ADDRESS:
62 pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
63 AP_QID_CARD(aq->qid),
64 AP_QID_QUEUE(aq->qid));
65 return -EOPNOTSUPP;
66 case AP_RESPONSE_RESET_IN_PROGRESS:
67 case AP_RESPONSE_BUSY:
68 default:
69 return -EBUSY;
70 }
71}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86static inline struct ap_queue_status
87__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
88 unsigned int special)
89{
90 if (special == 1)
91 qid |= 0x400000UL;
92 return ap_nqap(qid, psmid, msg, length);
93}
94
95int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
96{
97 struct ap_queue_status status;
98
99 status = __ap_send(qid, psmid, msg, length, 0);
100 switch (status.response_code) {
101 case AP_RESPONSE_NORMAL:
102 return 0;
103 case AP_RESPONSE_Q_FULL:
104 case AP_RESPONSE_RESET_IN_PROGRESS:
105 return -EBUSY;
106 case AP_RESPONSE_REQ_FAC_NOT_INST:
107 return -EINVAL;
108 default:
109 return -ENODEV;
110 }
111}
112EXPORT_SYMBOL(ap_send);
113
114int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
115{
116 struct ap_queue_status status;
117
118 if (msg == NULL)
119 return -EINVAL;
120 status = ap_dqap(qid, psmid, msg, length);
121 switch (status.response_code) {
122 case AP_RESPONSE_NORMAL:
123 return 0;
124 case AP_RESPONSE_NO_PENDING_REPLY:
125 if (status.queue_empty)
126 return -ENOENT;
127 return -EBUSY;
128 case AP_RESPONSE_RESET_IN_PROGRESS:
129 return -EBUSY;
130 default:
131 return -ENODEV;
132 }
133}
134EXPORT_SYMBOL(ap_recv);
135
136
137
138static enum ap_wait ap_sm_nop(struct ap_queue *aq)
139{
140 return AP_WAIT_NONE;
141}
142
143
144
145
146
147
148
149
150static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
151{
152 struct ap_queue_status status;
153 struct ap_message *ap_msg;
154
155 status = ap_dqap(aq->qid, &aq->reply->psmid,
156 aq->reply->message, aq->reply->length);
157 switch (status.response_code) {
158 case AP_RESPONSE_NORMAL:
159 aq->queue_count--;
160 if (aq->queue_count > 0)
161 mod_timer(&aq->timeout,
162 jiffies + aq->request_timeout);
163 list_for_each_entry(ap_msg, &aq->pendingq, list) {
164 if (ap_msg->psmid != aq->reply->psmid)
165 continue;
166 list_del_init(&ap_msg->list);
167 aq->pendingq_count--;
168 ap_msg->receive(aq, ap_msg, aq->reply);
169 break;
170 }
171 case AP_RESPONSE_NO_PENDING_REPLY:
172 if (!status.queue_empty || aq->queue_count <= 0)
173 break;
174
175 aq->queue_count = 0;
176 list_splice_init(&aq->pendingq, &aq->requestq);
177 aq->requestq_count += aq->pendingq_count;
178 aq->pendingq_count = 0;
179 break;
180 default:
181 break;
182 }
183 return status;
184}
185
186
187
188
189
190
191
192static enum ap_wait ap_sm_read(struct ap_queue *aq)
193{
194 struct ap_queue_status status;
195
196 if (!aq->reply)
197 return AP_WAIT_NONE;
198 status = ap_sm_recv(aq);
199 switch (status.response_code) {
200 case AP_RESPONSE_NORMAL:
201 if (aq->queue_count > 0) {
202 aq->state = AP_STATE_WORKING;
203 return AP_WAIT_AGAIN;
204 }
205 aq->state = AP_STATE_IDLE;
206 return AP_WAIT_NONE;
207 case AP_RESPONSE_NO_PENDING_REPLY:
208 if (aq->queue_count > 0)
209 return AP_WAIT_INTERRUPT;
210 aq->state = AP_STATE_IDLE;
211 return AP_WAIT_NONE;
212 default:
213 aq->state = AP_STATE_BORKED;
214 return AP_WAIT_NONE;
215 }
216}
217
218
219
220
221
222
223
224
225
226static enum ap_wait ap_sm_suspend_read(struct ap_queue *aq)
227{
228 struct ap_queue_status status;
229
230 if (!aq->reply)
231 return AP_WAIT_NONE;
232 status = ap_sm_recv(aq);
233 switch (status.response_code) {
234 case AP_RESPONSE_NORMAL:
235 if (aq->queue_count > 0)
236 return AP_WAIT_AGAIN;
237
238 default:
239 return AP_WAIT_NONE;
240 }
241}
242
243
244
245
246
247
248
249static enum ap_wait ap_sm_write(struct ap_queue *aq)
250{
251 struct ap_queue_status status;
252 struct ap_message *ap_msg;
253
254 if (aq->requestq_count <= 0)
255 return AP_WAIT_NONE;
256
257 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
258 status = __ap_send(aq->qid, ap_msg->psmid,
259 ap_msg->message, ap_msg->length, ap_msg->special);
260 switch (status.response_code) {
261 case AP_RESPONSE_NORMAL:
262 aq->queue_count++;
263 if (aq->queue_count == 1)
264 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
265 list_move_tail(&ap_msg->list, &aq->pendingq);
266 aq->requestq_count--;
267 aq->pendingq_count++;
268 if (aq->queue_count < aq->card->queue_depth) {
269 aq->state = AP_STATE_WORKING;
270 return AP_WAIT_AGAIN;
271 }
272
273 case AP_RESPONSE_Q_FULL:
274 aq->state = AP_STATE_QUEUE_FULL;
275 return AP_WAIT_INTERRUPT;
276 case AP_RESPONSE_RESET_IN_PROGRESS:
277 aq->state = AP_STATE_RESET_WAIT;
278 return AP_WAIT_TIMEOUT;
279 case AP_RESPONSE_MESSAGE_TOO_BIG:
280 case AP_RESPONSE_REQ_FAC_NOT_INST:
281 list_del_init(&ap_msg->list);
282 aq->requestq_count--;
283 ap_msg->rc = -EINVAL;
284 ap_msg->receive(aq, ap_msg, NULL);
285 return AP_WAIT_AGAIN;
286 default:
287 aq->state = AP_STATE_BORKED;
288 return AP_WAIT_NONE;
289 }
290}
291
292
293
294
295
296
297
298static enum ap_wait ap_sm_read_write(struct ap_queue *aq)
299{
300 return min(ap_sm_read(aq), ap_sm_write(aq));
301}
302
303
304
305
306
307
308
309static enum ap_wait ap_sm_reset(struct ap_queue *aq)
310{
311 struct ap_queue_status status;
312
313 status = ap_rapq(aq->qid);
314 switch (status.response_code) {
315 case AP_RESPONSE_NORMAL:
316 case AP_RESPONSE_RESET_IN_PROGRESS:
317 aq->state = AP_STATE_RESET_WAIT;
318 aq->interrupt = AP_INTR_DISABLED;
319 return AP_WAIT_TIMEOUT;
320 case AP_RESPONSE_BUSY:
321 return AP_WAIT_TIMEOUT;
322 case AP_RESPONSE_Q_NOT_AVAIL:
323 case AP_RESPONSE_DECONFIGURED:
324 case AP_RESPONSE_CHECKSTOPPED:
325 default:
326 aq->state = AP_STATE_BORKED;
327 return AP_WAIT_NONE;
328 }
329}
330
331
332
333
334
335
336
337static enum ap_wait ap_sm_reset_wait(struct ap_queue *aq)
338{
339 struct ap_queue_status status;
340 void *lsi_ptr;
341
342 if (aq->queue_count > 0 && aq->reply)
343
344 status = ap_sm_recv(aq);
345 else
346
347 status = ap_tapq(aq->qid, NULL);
348
349 switch (status.response_code) {
350 case AP_RESPONSE_NORMAL:
351 lsi_ptr = ap_airq_ptr();
352 if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
353 aq->state = AP_STATE_SETIRQ_WAIT;
354 else
355 aq->state = (aq->queue_count > 0) ?
356 AP_STATE_WORKING : AP_STATE_IDLE;
357 return AP_WAIT_AGAIN;
358 case AP_RESPONSE_BUSY:
359 case AP_RESPONSE_RESET_IN_PROGRESS:
360 return AP_WAIT_TIMEOUT;
361 case AP_RESPONSE_Q_NOT_AVAIL:
362 case AP_RESPONSE_DECONFIGURED:
363 case AP_RESPONSE_CHECKSTOPPED:
364 default:
365 aq->state = AP_STATE_BORKED;
366 return AP_WAIT_NONE;
367 }
368}
369
370
371
372
373
374
375
376static enum ap_wait ap_sm_setirq_wait(struct ap_queue *aq)
377{
378 struct ap_queue_status status;
379
380 if (aq->queue_count > 0 && aq->reply)
381
382 status = ap_sm_recv(aq);
383 else
384
385 status = ap_tapq(aq->qid, NULL);
386
387 if (status.irq_enabled == 1) {
388
389 aq->interrupt = AP_INTR_ENABLED;
390 aq->state = (aq->queue_count > 0) ?
391 AP_STATE_WORKING : AP_STATE_IDLE;
392 }
393
394 switch (status.response_code) {
395 case AP_RESPONSE_NORMAL:
396 if (aq->queue_count > 0)
397 return AP_WAIT_AGAIN;
398
399 case AP_RESPONSE_NO_PENDING_REPLY:
400 return AP_WAIT_TIMEOUT;
401 default:
402 aq->state = AP_STATE_BORKED;
403 return AP_WAIT_NONE;
404 }
405}
406
407
408
409
410static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
411 [AP_STATE_RESET_START] = {
412 [AP_EVENT_POLL] = ap_sm_reset,
413 [AP_EVENT_TIMEOUT] = ap_sm_nop,
414 },
415 [AP_STATE_RESET_WAIT] = {
416 [AP_EVENT_POLL] = ap_sm_reset_wait,
417 [AP_EVENT_TIMEOUT] = ap_sm_nop,
418 },
419 [AP_STATE_SETIRQ_WAIT] = {
420 [AP_EVENT_POLL] = ap_sm_setirq_wait,
421 [AP_EVENT_TIMEOUT] = ap_sm_nop,
422 },
423 [AP_STATE_IDLE] = {
424 [AP_EVENT_POLL] = ap_sm_write,
425 [AP_EVENT_TIMEOUT] = ap_sm_nop,
426 },
427 [AP_STATE_WORKING] = {
428 [AP_EVENT_POLL] = ap_sm_read_write,
429 [AP_EVENT_TIMEOUT] = ap_sm_reset,
430 },
431 [AP_STATE_QUEUE_FULL] = {
432 [AP_EVENT_POLL] = ap_sm_read,
433 [AP_EVENT_TIMEOUT] = ap_sm_reset,
434 },
435 [AP_STATE_SUSPEND_WAIT] = {
436 [AP_EVENT_POLL] = ap_sm_suspend_read,
437 [AP_EVENT_TIMEOUT] = ap_sm_nop,
438 },
439 [AP_STATE_BORKED] = {
440 [AP_EVENT_POLL] = ap_sm_nop,
441 [AP_EVENT_TIMEOUT] = ap_sm_nop,
442 },
443};
444
445enum ap_wait ap_sm_event(struct ap_queue *aq, enum ap_event event)
446{
447 return ap_jumptable[aq->state][event](aq);
448}
449
450enum ap_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_event event)
451{
452 enum ap_wait wait;
453
454 while ((wait = ap_sm_event(aq, event)) == AP_WAIT_AGAIN)
455 ;
456 return wait;
457}
458
459
460
461
462void ap_queue_suspend(struct ap_device *ap_dev)
463{
464 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
465
466
467 spin_lock_bh(&aq->lock);
468 aq->state = AP_STATE_SUSPEND_WAIT;
469 while (ap_sm_event(aq, AP_EVENT_POLL) != AP_WAIT_NONE)
470 ;
471 aq->state = AP_STATE_BORKED;
472 spin_unlock_bh(&aq->lock);
473}
474EXPORT_SYMBOL(ap_queue_suspend);
475
476void ap_queue_resume(struct ap_device *ap_dev)
477{
478}
479EXPORT_SYMBOL(ap_queue_resume);
480
481
482
483
484static ssize_t ap_req_count_show(struct device *dev,
485 struct device_attribute *attr,
486 char *buf)
487{
488 struct ap_queue *aq = to_ap_queue(dev);
489 unsigned int req_cnt;
490
491 spin_lock_bh(&aq->lock);
492 req_cnt = aq->total_request_count;
493 spin_unlock_bh(&aq->lock);
494 return snprintf(buf, PAGE_SIZE, "%d\n", req_cnt);
495}
496
497static ssize_t ap_req_count_store(struct device *dev,
498 struct device_attribute *attr,
499 const char *buf, size_t count)
500{
501 struct ap_queue *aq = to_ap_queue(dev);
502
503 spin_lock_bh(&aq->lock);
504 aq->total_request_count = 0;
505 spin_unlock_bh(&aq->lock);
506
507 return count;
508}
509
510static DEVICE_ATTR(request_count, 0644, ap_req_count_show, ap_req_count_store);
511
512static ssize_t ap_requestq_count_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
514{
515 struct ap_queue *aq = to_ap_queue(dev);
516 unsigned int reqq_cnt = 0;
517
518 spin_lock_bh(&aq->lock);
519 reqq_cnt = aq->requestq_count;
520 spin_unlock_bh(&aq->lock);
521 return snprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
522}
523
524static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
525
526static ssize_t ap_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 penq_cnt = aq->pendingq_count;
534 spin_unlock_bh(&aq->lock);
535 return snprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
536}
537
538static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
539
540static ssize_t ap_reset_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
543 struct ap_queue *aq = to_ap_queue(dev);
544 int rc = 0;
545
546 spin_lock_bh(&aq->lock);
547 switch (aq->state) {
548 case AP_STATE_RESET_START:
549 case AP_STATE_RESET_WAIT:
550 rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
551 break;
552 case AP_STATE_WORKING:
553 case AP_STATE_QUEUE_FULL:
554 rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
555 break;
556 default:
557 rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
558 }
559 spin_unlock_bh(&aq->lock);
560 return rc;
561}
562
563static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
564
565static ssize_t ap_interrupt_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
568 struct ap_queue *aq = to_ap_queue(dev);
569 int rc = 0;
570
571 spin_lock_bh(&aq->lock);
572 if (aq->state == AP_STATE_SETIRQ_WAIT)
573 rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
574 else if (aq->interrupt == AP_INTR_ENABLED)
575 rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
576 else
577 rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
578 spin_unlock_bh(&aq->lock);
579 return rc;
580}
581
582static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
583
584static struct attribute *ap_queue_dev_attrs[] = {
585 &dev_attr_request_count.attr,
586 &dev_attr_requestq_count.attr,
587 &dev_attr_pendingq_count.attr,
588 &dev_attr_reset.attr,
589 &dev_attr_interrupt.attr,
590 NULL
591};
592
593static struct attribute_group ap_queue_dev_attr_group = {
594 .attrs = ap_queue_dev_attrs
595};
596
597static const struct attribute_group *ap_queue_dev_attr_groups[] = {
598 &ap_queue_dev_attr_group,
599 NULL
600};
601
602struct device_type ap_queue_type = {
603 .name = "ap_queue",
604 .groups = ap_queue_dev_attr_groups,
605};
606
607static void ap_queue_device_release(struct device *dev)
608{
609 struct ap_queue *aq = to_ap_queue(dev);
610
611 if (!list_empty(&aq->list)) {
612 spin_lock_bh(&ap_list_lock);
613 list_del_init(&aq->list);
614 spin_unlock_bh(&ap_list_lock);
615 }
616 kfree(aq);
617}
618
619struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
620{
621 struct ap_queue *aq;
622
623 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
624 if (!aq)
625 return NULL;
626 aq->ap_dev.device.release = ap_queue_device_release;
627 aq->ap_dev.device.type = &ap_queue_type;
628 aq->ap_dev.device_type = device_type;
629 aq->qid = qid;
630 aq->state = AP_STATE_RESET_START;
631 aq->interrupt = AP_INTR_DISABLED;
632 spin_lock_init(&aq->lock);
633 INIT_LIST_HEAD(&aq->list);
634 INIT_LIST_HEAD(&aq->pendingq);
635 INIT_LIST_HEAD(&aq->requestq);
636 setup_timer(&aq->timeout, ap_request_timeout, (unsigned long) aq);
637
638 return aq;
639}
640
641void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
642{
643 aq->reply = reply;
644
645 spin_lock_bh(&aq->lock);
646 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
647 spin_unlock_bh(&aq->lock);
648}
649EXPORT_SYMBOL(ap_queue_init_reply);
650
651
652
653
654
655
656void ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
657{
658
659
660
661 BUG_ON(!ap_msg->receive);
662
663 spin_lock_bh(&aq->lock);
664
665 list_add_tail(&ap_msg->list, &aq->requestq);
666 aq->requestq_count++;
667 aq->total_request_count++;
668 atomic_inc(&aq->card->total_request_count);
669
670 ap_wait(ap_sm_event_loop(aq, AP_EVENT_POLL));
671 spin_unlock_bh(&aq->lock);
672}
673EXPORT_SYMBOL(ap_queue_message);
674
675
676
677
678
679
680
681
682
683
684
685void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
686{
687 struct ap_message *tmp;
688
689 spin_lock_bh(&aq->lock);
690 if (!list_empty(&ap_msg->list)) {
691 list_for_each_entry(tmp, &aq->pendingq, list)
692 if (tmp->psmid == ap_msg->psmid) {
693 aq->pendingq_count--;
694 goto found;
695 }
696 aq->requestq_count--;
697found:
698 list_del_init(&ap_msg->list);
699 }
700 spin_unlock_bh(&aq->lock);
701}
702EXPORT_SYMBOL(ap_cancel_message);
703
704
705
706
707
708
709
710static void __ap_flush_queue(struct ap_queue *aq)
711{
712 struct ap_message *ap_msg, *next;
713
714 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
715 list_del_init(&ap_msg->list);
716 aq->pendingq_count--;
717 ap_msg->rc = -EAGAIN;
718 ap_msg->receive(aq, ap_msg, NULL);
719 }
720 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
721 list_del_init(&ap_msg->list);
722 aq->requestq_count--;
723 ap_msg->rc = -EAGAIN;
724 ap_msg->receive(aq, ap_msg, NULL);
725 }
726}
727
728void ap_flush_queue(struct ap_queue *aq)
729{
730 spin_lock_bh(&aq->lock);
731 __ap_flush_queue(aq);
732 spin_unlock_bh(&aq->lock);
733}
734EXPORT_SYMBOL(ap_flush_queue);
735
736void ap_queue_remove(struct ap_queue *aq)
737{
738 ap_flush_queue(aq);
739 del_timer_sync(&aq->timeout);
740}
741EXPORT_SYMBOL(ap_queue_remove);
742