1 // SPDX-License-Identifier: GPL-2.0
3 * Linux for s390 qdio support, buffer handling, qdio API and module support.
5 * Copyright IBM Corp. 2000, 2008
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/kmemleak.h>
14 #include <linux/delay.h>
15 #include <linux/gfp.h>
17 #include <linux/atomic.h>
18 #include <asm/debug.h>
27 #include "qdio_debug.h"
31 MODULE_DESCRIPTION("QDIO base support");
32 MODULE_LICENSE("GPL");
34 static inline int do_siga_sync(unsigned long schid,
35 unsigned long out_mask, unsigned long in_mask,
48 : [fc] "d" (fc), [schid] "d" (schid),
49 [out] "d" (out_mask), [in] "d" (in_mask)
50 : CC_CLOBBER_LIST("0", "1", "2", "3"));
51 return CC_TRANSFORM(cc);
54 static inline int do_siga_input(unsigned long schid, unsigned long mask,
66 : [fc] "d" (fc), [schid] "d" (schid), [mask] "d" (mask)
67 : CC_CLOBBER_LIST("0", "1", "2"));
68 return CC_TRANSFORM(cc);
72 * do_siga_output - perform SIGA-w/wt function
73 * @schid: subchannel id or in case of QEBSM the subchannel token
74 * @mask: which output queues to process
75 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
76 * @fc: function code to perform
77 * @aob: asynchronous operation block
79 * Returns condition code.
80 * Note: For IQDC unicast queues only the highest priority queue is processed.
82 static inline int do_siga_output(unsigned long schid, unsigned long mask,
83 unsigned int *bb, unsigned long fc,
96 : CC_OUT(cc, cc), [fc] "+&d" (fc)
97 : [schid] "d" (schid), [mask] "d" (mask), [aob] "d" (aob)
98 : CC_CLOBBER_LIST("0", "1", "2", "3"));
100 return CC_TRANSFORM(cc);
104 * qdio_do_eqbs - extract buffer states for QEBSM
105 * @q: queue to manipulate
106 * @state: state of the extracted buffers
107 * @start: buffer number to start at
108 * @count: count of buffers to examine
109 * @auto_ack: automatically acknowledge buffers
111 * Returns the number of successfully extracted equal buffer states.
112 * Stops processing if a state is different from the last buffers state.
114 static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
115 int start, int count, int auto_ack)
117 int tmp_count = count, tmp_start = start, nr = q->nr;
118 unsigned int ccq = 0;
123 nr += q->irq_ptr->nr_input_qs;
125 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
131 /* all done, or next buffer state different */
132 return count - tmp_count;
134 /* not all buffers processed */
135 qperf_inc(q, eqbs_partial);
136 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "EQBS part:%02x",
138 return count - tmp_count;
140 /* no buffer processed */
141 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
144 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
145 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
146 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
147 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE, q->nr,
148 q->first_to_check, count, q->irq_ptr->int_parm);
154 * qdio_do_sqbs - set buffer states for QEBSM
155 * @q: queue to manipulate
156 * @state: new state of the buffers
157 * @start: first buffer number to change
158 * @count: how many buffers to change
160 * Returns the number of successfully changed buffers.
161 * Does retrying until the specified count of buffer states is set or an
164 static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
167 unsigned int ccq = 0;
168 int tmp_count = count, tmp_start = start;
174 nr += q->irq_ptr->nr_input_qs;
176 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
181 /* all done, or active buffer adapter-owned */
182 WARN_ON_ONCE(tmp_count);
183 return count - tmp_count;
185 /* not all buffers processed */
186 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
187 qperf_inc(q, sqbs_partial);
190 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
191 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
192 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
193 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE, q->nr,
194 q->first_to_check, count, q->irq_ptr->int_parm);
200 * Returns number of examined buffers and their common state in *state.
201 * Requested number of buffers-to-examine must be > 0.
203 static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
204 unsigned char *state, unsigned int count,
207 unsigned char __state = 0;
211 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
213 /* get initial state: */
214 __state = q->slsb.val[bufnr];
216 /* Bail out early if there is no work on the queue: */
217 if (__state & SLSB_OWNER_CU)
220 for (; i < count; i++) {
221 bufnr = next_buf(bufnr);
223 /* stop if next state differs from initial state: */
224 if (q->slsb.val[bufnr] != __state)
233 static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
234 unsigned char *state, int auto_ack)
236 return get_buf_states(q, bufnr, state, 1, auto_ack);
239 /* wrap-around safe setting of slsb states, returns number of changed buffers */
240 static inline int set_buf_states(struct qdio_q *q, int bufnr,
241 unsigned char state, int count)
246 return qdio_do_sqbs(q, state, bufnr, count);
248 /* Ensure that all preceding changes to the SBALs are visible: */
251 for (i = 0; i < count; i++) {
252 WRITE_ONCE(q->slsb.val[bufnr], state);
253 bufnr = next_buf(bufnr);
256 /* Make our SLSB changes visible: */
262 static inline int set_buf_state(struct qdio_q *q, int bufnr,
265 return set_buf_states(q, bufnr, state, 1);
268 /* set slsb states to initial state */
269 static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
274 for_each_input_queue(irq_ptr, q, i)
275 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
276 QDIO_MAX_BUFFERS_PER_Q);
277 for_each_output_queue(irq_ptr, q, i)
278 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
279 QDIO_MAX_BUFFERS_PER_Q);
282 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
285 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
286 unsigned int fc = QDIO_SIGA_SYNC;
289 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
290 qperf_inc(q, siga_sync);
293 schid = q->irq_ptr->sch_token;
294 fc |= QDIO_SIGA_QEBSM_FLAG;
297 cc = do_siga_sync(schid, output, input, fc);
299 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
300 return (cc) ? -EIO : 0;
303 static inline int qdio_sync_input_queue(struct qdio_q *q)
305 return qdio_siga_sync(q, 0, q->mask);
308 static inline int qdio_sync_output_queue(struct qdio_q *q)
310 return qdio_siga_sync(q, q->mask, 0);
313 static inline int qdio_siga_sync_q(struct qdio_q *q)
316 return qdio_sync_input_queue(q);
318 return qdio_sync_output_queue(q);
321 static int qdio_siga_output(struct qdio_q *q, unsigned int count,
322 unsigned int *busy_bit, dma64_t aob)
324 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
325 unsigned int fc = QDIO_SIGA_WRITE;
329 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q)) {
331 fc = QDIO_SIGA_WRITEM;
333 fc = QDIO_SIGA_WRITEQ;
337 schid = q->irq_ptr->sch_token;
338 fc |= QDIO_SIGA_QEBSM_FLAG;
341 cc = do_siga_output(schid, q->mask, busy_bit, fc, aob);
343 /* hipersocket busy condition */
344 if (unlikely(*busy_bit)) {
348 start_time = get_tod_clock_fast();
351 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
355 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
356 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
357 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
362 static inline int qdio_siga_input(struct qdio_q *q)
364 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
365 unsigned int fc = QDIO_SIGA_READ;
368 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
369 qperf_inc(q, siga_read);
372 schid = q->irq_ptr->sch_token;
373 fc |= QDIO_SIGA_QEBSM_FLAG;
376 cc = do_siga_input(schid, q->mask, fc);
378 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
379 return (cc) ? -EIO : 0;
382 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
383 unsigned char *state)
385 if (qdio_need_siga_sync(q->irq_ptr))
387 return get_buf_state(q, bufnr, state, 0);
390 static inline void qdio_stop_polling(struct qdio_q *q)
392 if (!q->u.in.batch_count)
395 qperf_inc(q, stop_polling);
397 /* show the card that we are not polling anymore */
398 set_buf_states(q, q->u.in.batch_start, SLSB_P_INPUT_NOT_INIT,
399 q->u.in.batch_count);
400 q->u.in.batch_count = 0;
403 static inline void account_sbals(struct qdio_q *q, unsigned int count)
405 q->q_stats.nr_sbal_total += count;
406 q->q_stats.nr_sbals[ilog2(count)]++;
409 static void process_buffer_error(struct qdio_q *q, unsigned int start,
412 /* special handling for no target buffer empty */
413 if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
414 q->sbal[start]->element[15].sflags == 0x10) {
415 qperf_inc(q, target_full);
416 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
420 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
421 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
422 DBF_ERROR("FTC:%3d C:%3d", start, count);
423 DBF_ERROR("F14:%2x F15:%2x",
424 q->sbal[start]->element[14].sflags,
425 q->sbal[start]->element[15].sflags);
428 static inline void inbound_handle_work(struct qdio_q *q, unsigned int start,
429 int count, bool auto_ack)
431 /* ACK the newest SBAL: */
433 set_buf_state(q, add_buf(start, count - 1), SLSB_P_INPUT_ACK);
435 if (!q->u.in.batch_count)
436 q->u.in.batch_start = start;
437 q->u.in.batch_count += count;
440 static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start,
443 unsigned char state = 0;
446 q->timestamp = get_tod_clock_fast();
448 count = atomic_read(&q->nr_buf_used);
452 if (qdio_need_siga_sync(q->irq_ptr))
453 qdio_sync_input_queue(q);
455 count = get_buf_states(q, start, &state, count, 1);
460 case SLSB_P_INPUT_PRIMED:
461 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr,
464 inbound_handle_work(q, start, count, is_qebsm(q));
465 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
466 qperf_inc(q, inbound_queue_full);
467 if (q->irq_ptr->perf_stat_enabled)
468 account_sbals(q, count);
470 case SLSB_P_INPUT_ERROR:
471 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in err:%1d %02x", q->nr,
474 *error = QDIO_ERROR_SLSB_STATE;
475 process_buffer_error(q, start, count);
476 inbound_handle_work(q, start, count, false);
477 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
478 qperf_inc(q, inbound_queue_full);
479 if (q->irq_ptr->perf_stat_enabled)
480 account_sbals_error(q, count);
482 case SLSB_CU_INPUT_EMPTY:
483 if (q->irq_ptr->perf_stat_enabled)
484 q->q_stats.nr_sbal_nop++;
485 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
488 case SLSB_P_INPUT_NOT_INIT:
489 case SLSB_P_INPUT_ACK:
490 /* We should never see this state, throw a WARN: */
492 dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
493 "found state %#x at index %u on queue %u\n",
494 state, start, q->nr);
499 int qdio_inspect_input_queue(struct ccw_device *cdev, unsigned int nr,
500 unsigned int *bufnr, unsigned int *error)
502 struct qdio_irq *irq = cdev->private->qdio_data;
510 q = irq->input_qs[nr];
511 start = q->first_to_check;
514 count = get_inbound_buffer_frontier(q, start, error);
519 q->first_to_check = add_buf(start, count);
522 EXPORT_SYMBOL_GPL(qdio_inspect_input_queue);
524 static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
526 unsigned char state = 0;
528 if (!atomic_read(&q->nr_buf_used))
531 if (qdio_need_siga_sync(q->irq_ptr))
532 qdio_sync_input_queue(q);
533 get_buf_state(q, start, &state, 0);
535 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
536 /* more work coming */
542 static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start,
545 unsigned char state = 0;
548 q->timestamp = get_tod_clock_fast();
550 count = atomic_read(&q->nr_buf_used);
554 if (qdio_need_siga_sync(q->irq_ptr))
555 qdio_sync_output_queue(q);
557 count = get_buf_states(q, start, &state, count, 0);
562 case SLSB_P_OUTPUT_PENDING:
563 *error = QDIO_ERROR_SLSB_PENDING;
565 case SLSB_P_OUTPUT_EMPTY:
566 /* the adapter got it */
567 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
568 "out empty:%1d %02x", q->nr, count);
570 atomic_sub(count, &q->nr_buf_used);
571 if (q->irq_ptr->perf_stat_enabled)
572 account_sbals(q, count);
574 case SLSB_P_OUTPUT_ERROR:
575 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out error:%1d %02x",
578 *error = QDIO_ERROR_SLSB_STATE;
579 process_buffer_error(q, start, count);
580 atomic_sub(count, &q->nr_buf_used);
581 if (q->irq_ptr->perf_stat_enabled)
582 account_sbals_error(q, count);
584 case SLSB_CU_OUTPUT_PRIMED:
585 /* the adapter has not fetched the output yet */
586 if (q->irq_ptr->perf_stat_enabled)
587 q->q_stats.nr_sbal_nop++;
588 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
591 case SLSB_P_OUTPUT_HALTED:
593 case SLSB_P_OUTPUT_NOT_INIT:
594 /* We should never see this state, throw a WARN: */
596 dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
597 "found state %#x at index %u on queue %u\n",
598 state, start, q->nr);
603 int qdio_inspect_output_queue(struct ccw_device *cdev, unsigned int nr,
604 unsigned int *bufnr, unsigned int *error)
606 struct qdio_irq *irq = cdev->private->qdio_data;
614 q = irq->output_qs[nr];
615 start = q->first_to_check;
618 count = get_outbound_buffer_frontier(q, start, error);
623 q->first_to_check = add_buf(start, count);
626 EXPORT_SYMBOL_GPL(qdio_inspect_output_queue);
628 static int qdio_kick_outbound_q(struct qdio_q *q, unsigned int count,
632 unsigned int busy_bit;
634 if (!qdio_need_siga_out(q->irq_ptr))
637 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
639 qperf_inc(q, siga_write);
641 cc = qdio_siga_output(q, count, &busy_bit, aob);
647 while (++retries < QDIO_BUSY_BIT_RETRIES) {
648 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
651 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
654 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
660 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
665 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
666 DBF_ERROR("count:%u", retries);
671 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
672 enum qdio_irq_states state)
674 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
676 irq_ptr->state = state;
680 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
682 if (irb->esw.esw0.erw.cons) {
683 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
684 DBF_ERROR_HEX(irb, 64);
685 DBF_ERROR_HEX(irb->ecw, 64);
689 /* PCI interrupt handler */
690 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
692 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
695 qdio_deliver_irq(irq_ptr);
696 irq_ptr->last_data_irq_time = get_lowcore()->int_clock;
699 static void qdio_handle_activate_check(struct qdio_irq *irq_ptr,
700 unsigned long intparm, int cstat,
703 unsigned int first_to_check = 0;
705 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
706 DBF_ERROR("intp :%lx", intparm);
707 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
709 /* zfcp wants this: */
710 if (irq_ptr->nr_input_qs)
711 first_to_check = irq_ptr->input_qs[0]->first_to_check;
713 irq_ptr->error_handler(irq_ptr->cdev, QDIO_ERROR_ACTIVATE, 0,
714 first_to_check, 0, irq_ptr->int_parm);
715 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
717 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
718 * Therefore we call the LGR detection function here.
723 static int qdio_establish_handle_irq(struct qdio_irq *irq_ptr, int cstat,
726 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
730 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
734 if (!(dstat & DEV_STAT_DEV_END))
736 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
740 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
741 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
742 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
746 /* qdio interrupt handler */
747 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
750 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
751 struct subchannel_id schid;
752 int cstat, dstat, rc, dcc;
754 if (!intparm || !irq_ptr) {
755 ccw_device_get_schid(cdev, &schid);
756 DBF_ERROR("qint:%4x", schid.sch_no);
760 if (irq_ptr->perf_stat_enabled)
761 irq_ptr->perf_stat.qdio_int++;
764 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
765 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
766 wake_up(&cdev->private->wait_q);
769 qdio_irq_check_sense(irq_ptr, irb);
770 cstat = irb->scsw.cmd.cstat;
771 dstat = irb->scsw.cmd.dstat;
772 dcc = scsw_cmd_is_valid_cc(&irb->scsw) ? irb->scsw.cmd.cc : 0;
775 switch (irq_ptr->state) {
776 case QDIO_IRQ_STATE_INACTIVE:
777 rc = qdio_establish_handle_irq(irq_ptr, cstat, dstat, dcc);
779 case QDIO_IRQ_STATE_CLEANUP:
780 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
782 case QDIO_IRQ_STATE_ESTABLISHED:
783 case QDIO_IRQ_STATE_ACTIVE:
784 if (cstat & SCHN_STAT_PCI) {
785 qdio_int_handler_pci(irq_ptr);
789 qdio_handle_activate_check(irq_ptr, intparm, cstat,
794 case QDIO_IRQ_STATE_STOPPED:
801 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qint retry");
802 rc = ccw_device_start(cdev, irq_ptr->ccw, intparm, 0, 0);
805 DBF_ERROR("%4x RETRY ERR", irq_ptr->schid.sch_no);
806 DBF_ERROR("rc:%4x", rc);
807 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
810 wake_up(&cdev->private->wait_q);
814 * qdio_get_ssqd_desc - get qdio subchannel description
815 * @cdev: ccw device to get description for
816 * @data: where to store the ssqd
818 * Returns 0 or an error code. The results of the chsc are stored in the
819 * specified structure.
821 int qdio_get_ssqd_desc(struct ccw_device *cdev,
822 struct qdio_ssqd_desc *data)
824 struct subchannel_id schid;
826 if (!cdev || !cdev->private)
829 ccw_device_get_schid(cdev, &schid);
830 DBF_EVENT("get ssqd:%4x", schid.sch_no);
831 return qdio_setup_get_ssqd(NULL, &schid, data);
833 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
835 static int qdio_cancel_ccw(struct qdio_irq *irq, int how)
837 struct ccw_device *cdev = irq->cdev;
841 spin_lock_irq(get_ccwdev_lock(cdev));
842 qdio_set_state(irq, QDIO_IRQ_STATE_CLEANUP);
843 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
844 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
846 /* default behaviour is halt */
847 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
848 spin_unlock_irq(get_ccwdev_lock(cdev));
850 DBF_ERROR("%4x SHUTD ERR", irq->schid.sch_no);
851 DBF_ERROR("rc:%4d", rc);
855 timeout = wait_event_interruptible_timeout(cdev->private->wait_q,
856 irq->state == QDIO_IRQ_STATE_INACTIVE ||
857 irq->state == QDIO_IRQ_STATE_ERR,
860 rc = (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
866 * qdio_shutdown - shut down a qdio subchannel
867 * @cdev: associated ccw device
868 * @how: use halt or clear to shutdown
870 int qdio_shutdown(struct ccw_device *cdev, int how)
872 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
873 struct subchannel_id schid;
879 WARN_ON_ONCE(irqs_disabled());
880 ccw_device_get_schid(cdev, &schid);
881 DBF_EVENT("qshutdown:%4x", schid.sch_no);
883 mutex_lock(&irq_ptr->setup_mutex);
885 * Subchannel was already shot down. We cannot prevent being called
886 * twice since cio may trigger a shutdown asynchronously.
888 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
889 mutex_unlock(&irq_ptr->setup_mutex);
894 * Indicate that the device is going down.
896 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
898 qdio_shutdown_debug_entries(irq_ptr);
900 rc = qdio_cancel_ccw(irq_ptr, how);
901 qdio_shutdown_thinint(irq_ptr);
902 qdio_shutdown_irq(irq_ptr);
904 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
905 mutex_unlock(&irq_ptr->setup_mutex);
910 EXPORT_SYMBOL_GPL(qdio_shutdown);
913 * qdio_free - free data structures for a qdio subchannel
914 * @cdev: associated ccw device
916 int qdio_free(struct ccw_device *cdev)
918 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
919 struct subchannel_id schid;
924 ccw_device_get_schid(cdev, &schid);
925 DBF_EVENT("qfree:%4x", schid.sch_no);
926 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
927 mutex_lock(&irq_ptr->setup_mutex);
929 irq_ptr->debug_area = NULL;
930 cdev->private->qdio_data = NULL;
931 mutex_unlock(&irq_ptr->setup_mutex);
933 qdio_free_queues(irq_ptr);
934 free_page((unsigned long) irq_ptr->qdr);
935 free_page(irq_ptr->chsc_page);
937 free_page((unsigned long) irq_ptr);
940 EXPORT_SYMBOL_GPL(qdio_free);
943 * qdio_allocate - allocate qdio queues and associated data
944 * @cdev: associated ccw device
945 * @no_input_qs: allocate this number of Input Queues
946 * @no_output_qs: allocate this number of Output Queues
948 int qdio_allocate(struct ccw_device *cdev, unsigned int no_input_qs,
949 unsigned int no_output_qs)
951 struct subchannel_id schid;
952 struct qdio_irq *irq_ptr;
955 ccw_device_get_schid(cdev, &schid);
956 DBF_EVENT("qallocate:%4x", schid.sch_no);
958 if (no_input_qs > QDIO_MAX_QUEUES_PER_IRQ ||
959 no_output_qs > QDIO_MAX_QUEUES_PER_IRQ)
962 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL);
966 irq_ptr->ccw = kmalloc(sizeof(*irq_ptr->ccw), GFP_KERNEL | GFP_DMA);
970 /* kmemleak doesn't scan the page-allocated irq_ptr: */
971 kmemleak_not_leak(irq_ptr->ccw);
973 irq_ptr->cdev = cdev;
974 mutex_init(&irq_ptr->setup_mutex);
975 if (qdio_allocate_dbf(irq_ptr))
978 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "alloc niq:%1u noq:%1u", no_input_qs,
982 * Allocate a page for the chsc calls in qdio_establish.
983 * Must be pre-allocated since a zfcp recovery will call
984 * qdio_establish. In case of low memory and swap on a zfcp disk
985 * we may not be able to allocate memory otherwise.
987 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
988 if (!irq_ptr->chsc_page)
991 /* qdr is used in ccw1.cda which is u32 */
992 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
996 rc = qdio_allocate_qs(irq_ptr, no_input_qs, no_output_qs);
1000 cdev->private->qdio_data = irq_ptr;
1001 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1005 free_page((unsigned long) irq_ptr->qdr);
1007 free_page(irq_ptr->chsc_page);
1010 kfree(irq_ptr->ccw);
1012 free_page((unsigned long) irq_ptr);
1015 EXPORT_SYMBOL_GPL(qdio_allocate);
1017 static void qdio_trace_init_data(struct qdio_irq *irq,
1018 struct qdio_initialize *data)
1020 DBF_DEV_EVENT(DBF_ERR, irq, "qfmt:%1u", data->q_format);
1021 DBF_DEV_EVENT(DBF_ERR, irq, "qpff%4x", data->qib_param_field_format);
1022 DBF_DEV_HEX(irq, &data->qib_param_field, sizeof(void *), DBF_ERR);
1023 DBF_DEV_EVENT(DBF_ERR, irq, "niq:%1u noq:%1u", data->no_input_qs,
1024 data->no_output_qs);
1025 DBF_DEV_HEX(irq, &data->input_handler, sizeof(void *), DBF_ERR);
1026 DBF_DEV_HEX(irq, &data->output_handler, sizeof(void *), DBF_ERR);
1027 DBF_DEV_HEX(irq, &data->int_parm, sizeof(long), DBF_ERR);
1028 DBF_DEV_HEX(irq, &data->input_sbal_addr_array, sizeof(void *), DBF_ERR);
1029 DBF_DEV_HEX(irq, &data->output_sbal_addr_array, sizeof(void *),
1034 * qdio_establish - establish queues on a qdio subchannel
1035 * @cdev: associated ccw device
1036 * @init_data: initialization data
1038 int qdio_establish(struct ccw_device *cdev,
1039 struct qdio_initialize *init_data)
1041 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1042 struct subchannel_id schid;
1047 ccw_device_get_schid(cdev, &schid);
1048 DBF_EVENT("qestablish:%4x", schid.sch_no);
1053 if (init_data->no_input_qs > irq_ptr->max_input_qs ||
1054 init_data->no_output_qs > irq_ptr->max_output_qs)
1057 /* Needed as error_handler: */
1058 if (!init_data->input_handler)
1061 if (init_data->no_output_qs && !init_data->output_handler)
1064 if (!init_data->input_sbal_addr_array ||
1065 !init_data->output_sbal_addr_array)
1068 if (!init_data->irq_poll)
1071 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_EQUEUE);
1073 DBF_ERROR("%4x NO EQ", schid.sch_no);
1077 mutex_lock(&irq_ptr->setup_mutex);
1078 qdio_trace_init_data(irq_ptr, init_data);
1079 qdio_setup_irq(irq_ptr, init_data);
1081 rc = qdio_establish_thinint(irq_ptr);
1086 irq_ptr->ccw->cmd_code = ciw->cmd;
1087 irq_ptr->ccw->flags = CCW_FLAG_SLI;
1088 irq_ptr->ccw->count = ciw->count;
1089 irq_ptr->ccw->cda = virt_to_dma32(irq_ptr->qdr);
1091 spin_lock_irq(get_ccwdev_lock(cdev));
1092 ccw_device_set_options_mask(cdev, 0);
1094 rc = ccw_device_start(cdev, irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1095 spin_unlock_irq(get_ccwdev_lock(cdev));
1097 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1098 DBF_ERROR("rc:%4x", rc);
1102 timeout = wait_event_interruptible_timeout(cdev->private->wait_q,
1103 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1104 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1106 rc = (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
1107 goto err_ccw_timeout;
1110 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1115 qdio_setup_ssqd_info(irq_ptr);
1117 /* qebsm is now setup if available, initialize buffer states */
1118 qdio_init_buf_states(irq_ptr);
1120 mutex_unlock(&irq_ptr->setup_mutex);
1121 qdio_print_subchannel_info(irq_ptr);
1122 qdio_setup_debug_entries(irq_ptr);
1126 qdio_cancel_ccw(irq_ptr, QDIO_FLAG_CLEANUP_USING_CLEAR);
1129 qdio_shutdown_thinint(irq_ptr);
1131 qdio_shutdown_irq(irq_ptr);
1132 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1133 mutex_unlock(&irq_ptr->setup_mutex);
1136 EXPORT_SYMBOL_GPL(qdio_establish);
1139 * qdio_activate - activate queues on a qdio subchannel
1140 * @cdev: associated cdev
1142 int qdio_activate(struct ccw_device *cdev)
1144 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1145 struct subchannel_id schid;
1149 ccw_device_get_schid(cdev, &schid);
1150 DBF_EVENT("qactivate:%4x", schid.sch_no);
1155 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_AQUEUE);
1157 DBF_ERROR("%4x NO AQ", schid.sch_no);
1161 mutex_lock(&irq_ptr->setup_mutex);
1162 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1167 irq_ptr->ccw->cmd_code = ciw->cmd;
1168 irq_ptr->ccw->flags = CCW_FLAG_SLI;
1169 irq_ptr->ccw->count = ciw->count;
1170 irq_ptr->ccw->cda = 0;
1172 spin_lock_irq(get_ccwdev_lock(cdev));
1173 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1175 rc = ccw_device_start(cdev, irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1176 0, DOIO_DENY_PREFETCH);
1177 spin_unlock_irq(get_ccwdev_lock(cdev));
1179 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1180 DBF_ERROR("rc:%4x", rc);
1184 /* wait for subchannel to become active */
1187 switch (irq_ptr->state) {
1188 case QDIO_IRQ_STATE_STOPPED:
1189 case QDIO_IRQ_STATE_ERR:
1193 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1197 mutex_unlock(&irq_ptr->setup_mutex);
1200 EXPORT_SYMBOL_GPL(qdio_activate);
1203 * handle_inbound - reset processed input buffers
1204 * @q: queue containing the buffers
1205 * @bufnr: first buffer to process
1206 * @count: how many buffers are emptied
1208 static int handle_inbound(struct qdio_q *q, int bufnr, int count)
1212 qperf_inc(q, inbound_call);
1214 /* If any processed SBALs are returned to HW, adjust our tracking: */
1215 overlap = min_t(int, count - sub_buf(q->u.in.batch_start, bufnr),
1216 q->u.in.batch_count);
1218 q->u.in.batch_start = add_buf(q->u.in.batch_start, overlap);
1219 q->u.in.batch_count -= overlap;
1222 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1223 atomic_add(count, &q->nr_buf_used);
1225 if (qdio_need_siga_in(q->irq_ptr))
1226 return qdio_siga_input(q);
1232 * qdio_add_bufs_to_input_queue - process buffers on an Input Queue
1233 * @cdev: associated ccw_device for the qdio subchannel
1234 * @q_nr: queue number
1235 * @bufnr: buffer number
1236 * @count: how many buffers to process
1238 int qdio_add_bufs_to_input_queue(struct ccw_device *cdev, unsigned int q_nr,
1239 unsigned int bufnr, unsigned int count)
1241 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1243 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1249 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "addi b:%02x c:%02x", bufnr, count);
1251 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1256 return handle_inbound(irq_ptr->input_qs[q_nr], bufnr, count);
1258 EXPORT_SYMBOL_GPL(qdio_add_bufs_to_input_queue);
1261 * handle_outbound - process filled outbound buffers
1262 * @q: queue containing the buffers
1263 * @bufnr: first buffer to process
1264 * @count: how many buffers are filled
1265 * @aob: asynchronous operation block
1267 static int handle_outbound(struct qdio_q *q, unsigned int bufnr, unsigned int count,
1270 unsigned char state = 0;
1273 qperf_inc(q, outbound_call);
1275 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1276 used = atomic_add_return(count, &q->nr_buf_used);
1278 if (used == QDIO_MAX_BUFFERS_PER_Q)
1279 qperf_inc(q, outbound_queue_full);
1281 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1282 dma64_t phys_aob = aob ? virt_to_dma64(aob) : 0;
1284 WARN_ON_ONCE(!IS_ALIGNED(dma64_to_u64(phys_aob), 256));
1285 rc = qdio_kick_outbound_q(q, count, phys_aob);
1286 } else if (qdio_need_siga_sync(q->irq_ptr)) {
1287 rc = qdio_sync_output_queue(q);
1288 } else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1289 get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1290 state == SLSB_CU_OUTPUT_PRIMED) {
1291 /* The previous buffer is not processed yet, tack on. */
1292 qperf_inc(q, fast_requeue);
1294 rc = qdio_kick_outbound_q(q, count, 0);
1301 * qdio_add_bufs_to_output_queue - process buffers on an Output Queue
1302 * @cdev: associated ccw_device for the qdio subchannel
1303 * @q_nr: queue number
1304 * @bufnr: buffer number
1305 * @count: how many buffers to process
1306 * @aob: asynchronous operation block
1308 int qdio_add_bufs_to_output_queue(struct ccw_device *cdev, unsigned int q_nr,
1309 unsigned int bufnr, unsigned int count,
1312 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1314 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1320 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "addo b:%02x c:%02x", bufnr, count);
1322 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1327 return handle_outbound(irq_ptr->output_qs[q_nr], bufnr, count, aob);
1329 EXPORT_SYMBOL_GPL(qdio_add_bufs_to_output_queue);
1332 * qdio_start_irq - enable interrupt processing for the device
1333 * @cdev: associated ccw_device for the qdio subchannel
1337 * 1 - irqs not started since new data is available
1339 int qdio_start_irq(struct ccw_device *cdev)
1342 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1348 for_each_input_queue(irq_ptr, q, i)
1349 qdio_stop_polling(q);
1351 clear_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
1354 * We need to check again to not lose initiative after
1355 * resetting the ACK state.
1357 if (test_nonshared_ind(irq_ptr))
1360 for_each_input_queue(irq_ptr, q, i) {
1361 if (!qdio_inbound_q_done(q, q->first_to_check))
1368 if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1374 EXPORT_SYMBOL(qdio_start_irq);
1377 * qdio_stop_irq - disable interrupt processing for the device
1378 * @cdev: associated ccw_device for the qdio subchannel
1381 * 0 - interrupts were already disabled
1382 * 1 - interrupts successfully disabled
1384 int qdio_stop_irq(struct ccw_device *cdev)
1386 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1391 if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1396 EXPORT_SYMBOL(qdio_stop_irq);
1398 static int __init init_QDIO(void)
1402 rc = qdio_debug_init();
1405 rc = qdio_setup_init();
1408 rc = qdio_thinint_init();
1420 static void __exit exit_QDIO(void)
1422 qdio_thinint_exit();
1427 module_init(init_QDIO);
1428 module_exit(exit_QDIO);