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
6 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
7 * Jan Glauber <jang@linux.vnet.ibm.com>
8 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
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>
26 #include "qdio_debug.h"
28 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
29 "Jan Glauber <jang@linux.vnet.ibm.com>");
30 MODULE_DESCRIPTION("QDIO base support");
31 MODULE_LICENSE("GPL");
33 static inline int do_siga_sync(unsigned long schid,
34 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", "0", "1", "2", "3");
54 static inline int do_siga_input(unsigned long schid, unsigned long mask,
67 : [fc] "d" (fc), [schid] "d" (schid), [mask] "d" (mask)
68 : "cc", "0", "1", "2");
73 * do_siga_output - perform SIGA-w/wt function
74 * @schid: subchannel id or in case of QEBSM the subchannel token
75 * @mask: which output queues to process
76 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
77 * @fc: function code to perform
78 * @aob: asynchronous operation block
80 * Returns condition code.
81 * Note: For IQDC unicast queues only the highest priority queue is processed.
83 static inline int do_siga_output(unsigned long schid, unsigned long mask,
84 unsigned int *bb, unsigned long fc,
98 : [cc] "=&d" (cc), [fc] "+&d" (fc)
99 : [schid] "d" (schid), [mask] "d" (mask), [aob] "d" (aob)
100 : "cc", "0", "1", "2", "3");
106 * qdio_do_eqbs - extract buffer states for QEBSM
107 * @q: queue to manipulate
108 * @state: state of the extracted buffers
109 * @start: buffer number to start at
110 * @count: count of buffers to examine
111 * @auto_ack: automatically acknowledge buffers
113 * Returns the number of successfully extracted equal buffer states.
114 * Stops processing if a state is different from the last buffers state.
116 static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
117 int start, int count, int auto_ack)
119 int tmp_count = count, tmp_start = start, nr = q->nr;
120 unsigned int ccq = 0;
125 nr += q->irq_ptr->nr_input_qs;
127 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
133 /* all done, or next buffer state different */
134 return count - tmp_count;
136 /* not all buffers processed */
137 qperf_inc(q, eqbs_partial);
138 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "EQBS part:%02x",
140 return count - tmp_count;
142 /* no buffer processed */
143 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
146 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
147 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
148 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
149 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE, q->nr,
150 q->first_to_check, count, q->irq_ptr->int_parm);
156 * qdio_do_sqbs - set buffer states for QEBSM
157 * @q: queue to manipulate
158 * @state: new state of the buffers
159 * @start: first buffer number to change
160 * @count: how many buffers to change
162 * Returns the number of successfully changed buffers.
163 * Does retrying until the specified count of buffer states is set or an
166 static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
169 unsigned int ccq = 0;
170 int tmp_count = count, tmp_start = start;
176 nr += q->irq_ptr->nr_input_qs;
178 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
183 /* all done, or active buffer adapter-owned */
184 WARN_ON_ONCE(tmp_count);
185 return count - tmp_count;
187 /* not all buffers processed */
188 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
189 qperf_inc(q, sqbs_partial);
192 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
193 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
194 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
195 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE, q->nr,
196 q->first_to_check, count, q->irq_ptr->int_parm);
202 * Returns number of examined buffers and their common state in *state.
203 * Requested number of buffers-to-examine must be > 0.
205 static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
206 unsigned char *state, unsigned int count,
209 unsigned char __state = 0;
213 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
215 /* get initial state: */
216 __state = q->slsb.val[bufnr];
218 /* Bail out early if there is no work on the queue: */
219 if (__state & SLSB_OWNER_CU)
222 for (; i < count; i++) {
223 bufnr = next_buf(bufnr);
225 /* stop if next state differs from initial state: */
226 if (q->slsb.val[bufnr] != __state)
235 static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
236 unsigned char *state, int auto_ack)
238 return get_buf_states(q, bufnr, state, 1, auto_ack);
241 /* wrap-around safe setting of slsb states, returns number of changed buffers */
242 static inline int set_buf_states(struct qdio_q *q, int bufnr,
243 unsigned char state, int count)
248 return qdio_do_sqbs(q, state, bufnr, count);
250 /* Ensure that all preceding changes to the SBALs are visible: */
253 for (i = 0; i < count; i++) {
254 WRITE_ONCE(q->slsb.val[bufnr], state);
255 bufnr = next_buf(bufnr);
258 /* Make our SLSB changes visible: */
264 static inline int set_buf_state(struct qdio_q *q, int bufnr,
267 return set_buf_states(q, bufnr, state, 1);
270 /* set slsb states to initial state */
271 static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
276 for_each_input_queue(irq_ptr, q, i)
277 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
278 QDIO_MAX_BUFFERS_PER_Q);
279 for_each_output_queue(irq_ptr, q, i)
280 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
281 QDIO_MAX_BUFFERS_PER_Q);
284 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
287 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
288 unsigned int fc = QDIO_SIGA_SYNC;
291 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
292 qperf_inc(q, siga_sync);
295 schid = q->irq_ptr->sch_token;
296 fc |= QDIO_SIGA_QEBSM_FLAG;
299 cc = do_siga_sync(schid, output, input, fc);
301 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
302 return (cc) ? -EIO : 0;
305 static inline int qdio_sync_input_queue(struct qdio_q *q)
307 return qdio_siga_sync(q, 0, q->mask);
310 static inline int qdio_sync_output_queue(struct qdio_q *q)
312 return qdio_siga_sync(q, q->mask, 0);
315 static inline int qdio_siga_sync_q(struct qdio_q *q)
318 return qdio_sync_input_queue(q);
320 return qdio_sync_output_queue(q);
323 static int qdio_siga_output(struct qdio_q *q, unsigned int count,
324 unsigned int *busy_bit, unsigned long aob)
326 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
327 unsigned int fc = QDIO_SIGA_WRITE;
331 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q)) {
333 fc = QDIO_SIGA_WRITEM;
335 fc = QDIO_SIGA_WRITEQ;
339 schid = q->irq_ptr->sch_token;
340 fc |= QDIO_SIGA_QEBSM_FLAG;
343 cc = do_siga_output(schid, q->mask, busy_bit, fc, aob);
345 /* hipersocket busy condition */
346 if (unlikely(*busy_bit)) {
350 start_time = get_tod_clock_fast();
353 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
357 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
358 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
359 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
364 static inline int qdio_siga_input(struct qdio_q *q)
366 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
367 unsigned int fc = QDIO_SIGA_READ;
370 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
371 qperf_inc(q, siga_read);
374 schid = q->irq_ptr->sch_token;
375 fc |= QDIO_SIGA_QEBSM_FLAG;
378 cc = do_siga_input(schid, q->mask, fc);
380 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
381 return (cc) ? -EIO : 0;
384 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
385 unsigned char *state)
387 if (qdio_need_siga_sync(q->irq_ptr))
389 return get_buf_state(q, bufnr, state, 0);
392 static inline void qdio_stop_polling(struct qdio_q *q)
394 if (!q->u.in.batch_count)
397 qperf_inc(q, stop_polling);
399 /* show the card that we are not polling anymore */
400 set_buf_states(q, q->u.in.batch_start, SLSB_P_INPUT_NOT_INIT,
401 q->u.in.batch_count);
402 q->u.in.batch_count = 0;
405 static inline void account_sbals(struct qdio_q *q, unsigned int count)
407 q->q_stats.nr_sbal_total += count;
408 q->q_stats.nr_sbals[ilog2(count)]++;
411 static void process_buffer_error(struct qdio_q *q, unsigned int start,
414 /* special handling for no target buffer empty */
415 if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
416 q->sbal[start]->element[15].sflags == 0x10) {
417 qperf_inc(q, target_full);
418 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
422 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
423 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
424 DBF_ERROR("FTC:%3d C:%3d", start, count);
425 DBF_ERROR("F14:%2x F15:%2x",
426 q->sbal[start]->element[14].sflags,
427 q->sbal[start]->element[15].sflags);
430 static inline void inbound_handle_work(struct qdio_q *q, unsigned int start,
431 int count, bool auto_ack)
433 /* ACK the newest SBAL: */
435 set_buf_state(q, add_buf(start, count - 1), SLSB_P_INPUT_ACK);
437 if (!q->u.in.batch_count)
438 q->u.in.batch_start = start;
439 q->u.in.batch_count += count;
442 static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start,
445 unsigned char state = 0;
448 q->timestamp = get_tod_clock_fast();
450 count = atomic_read(&q->nr_buf_used);
454 if (qdio_need_siga_sync(q->irq_ptr))
455 qdio_sync_input_queue(q);
457 count = get_buf_states(q, start, &state, count, 1);
462 case SLSB_P_INPUT_PRIMED:
463 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr,
466 inbound_handle_work(q, start, count, is_qebsm(q));
467 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
468 qperf_inc(q, inbound_queue_full);
469 if (q->irq_ptr->perf_stat_enabled)
470 account_sbals(q, count);
472 case SLSB_P_INPUT_ERROR:
473 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in err:%1d %02x", q->nr,
476 *error = QDIO_ERROR_SLSB_STATE;
477 process_buffer_error(q, start, count);
478 inbound_handle_work(q, start, count, false);
479 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
480 qperf_inc(q, inbound_queue_full);
481 if (q->irq_ptr->perf_stat_enabled)
482 account_sbals_error(q, count);
484 case SLSB_CU_INPUT_EMPTY:
485 if (q->irq_ptr->perf_stat_enabled)
486 q->q_stats.nr_sbal_nop++;
487 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
490 case SLSB_P_INPUT_NOT_INIT:
491 case SLSB_P_INPUT_ACK:
492 /* We should never see this state, throw a WARN: */
494 dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
495 "found state %#x at index %u on queue %u\n",
496 state, start, q->nr);
501 int qdio_inspect_input_queue(struct ccw_device *cdev, unsigned int nr,
502 unsigned int *bufnr, unsigned int *error)
504 struct qdio_irq *irq = cdev->private->qdio_data;
512 q = irq->input_qs[nr];
513 start = q->first_to_check;
516 count = get_inbound_buffer_frontier(q, start, error);
521 q->first_to_check = add_buf(start, count);
524 EXPORT_SYMBOL_GPL(qdio_inspect_input_queue);
526 static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
528 unsigned char state = 0;
530 if (!atomic_read(&q->nr_buf_used))
533 if (qdio_need_siga_sync(q->irq_ptr))
534 qdio_sync_input_queue(q);
535 get_buf_state(q, start, &state, 0);
537 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
538 /* more work coming */
544 static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start,
547 unsigned char state = 0;
550 q->timestamp = get_tod_clock_fast();
552 count = atomic_read(&q->nr_buf_used);
556 if (qdio_need_siga_sync(q->irq_ptr))
557 qdio_sync_output_queue(q);
559 count = get_buf_states(q, start, &state, count, 0);
564 case SLSB_P_OUTPUT_PENDING:
565 *error = QDIO_ERROR_SLSB_PENDING;
567 case SLSB_P_OUTPUT_EMPTY:
568 /* the adapter got it */
569 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
570 "out empty:%1d %02x", q->nr, count);
572 atomic_sub(count, &q->nr_buf_used);
573 if (q->irq_ptr->perf_stat_enabled)
574 account_sbals(q, count);
576 case SLSB_P_OUTPUT_ERROR:
577 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out error:%1d %02x",
580 *error = QDIO_ERROR_SLSB_STATE;
581 process_buffer_error(q, start, count);
582 atomic_sub(count, &q->nr_buf_used);
583 if (q->irq_ptr->perf_stat_enabled)
584 account_sbals_error(q, count);
586 case SLSB_CU_OUTPUT_PRIMED:
587 /* the adapter has not fetched the output yet */
588 if (q->irq_ptr->perf_stat_enabled)
589 q->q_stats.nr_sbal_nop++;
590 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
593 case SLSB_P_OUTPUT_HALTED:
595 case SLSB_P_OUTPUT_NOT_INIT:
596 /* We should never see this state, throw a WARN: */
598 dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
599 "found state %#x at index %u on queue %u\n",
600 state, start, q->nr);
605 int qdio_inspect_output_queue(struct ccw_device *cdev, unsigned int nr,
606 unsigned int *bufnr, unsigned int *error)
608 struct qdio_irq *irq = cdev->private->qdio_data;
616 q = irq->output_qs[nr];
617 start = q->first_to_check;
620 count = get_outbound_buffer_frontier(q, start, error);
625 q->first_to_check = add_buf(start, count);
628 EXPORT_SYMBOL_GPL(qdio_inspect_output_queue);
630 static int qdio_kick_outbound_q(struct qdio_q *q, unsigned int count,
634 unsigned int busy_bit;
636 if (!qdio_need_siga_out(q->irq_ptr))
639 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
641 qperf_inc(q, siga_write);
643 cc = qdio_siga_output(q, count, &busy_bit, aob);
649 while (++retries < QDIO_BUSY_BIT_RETRIES) {
650 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
653 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
656 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
662 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
667 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
668 DBF_ERROR("count:%u", retries);
673 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
674 enum qdio_irq_states state)
676 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
678 irq_ptr->state = state;
682 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
684 if (irb->esw.esw0.erw.cons) {
685 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
686 DBF_ERROR_HEX(irb, 64);
687 DBF_ERROR_HEX(irb->ecw, 64);
691 /* PCI interrupt handler */
692 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
694 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
697 qdio_deliver_irq(irq_ptr);
698 irq_ptr->last_data_irq_time = S390_lowcore.int_clock;
701 static void qdio_handle_activate_check(struct qdio_irq *irq_ptr,
702 unsigned long intparm, int cstat,
705 unsigned int first_to_check = 0;
707 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
708 DBF_ERROR("intp :%lx", intparm);
709 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
711 /* zfcp wants this: */
712 if (irq_ptr->nr_input_qs)
713 first_to_check = irq_ptr->input_qs[0]->first_to_check;
715 irq_ptr->error_handler(irq_ptr->cdev, QDIO_ERROR_ACTIVATE, 0,
716 first_to_check, 0, irq_ptr->int_parm);
717 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
719 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
720 * Therefore we call the LGR detection function here.
725 static void qdio_establish_handle_irq(struct qdio_irq *irq_ptr, int cstat,
728 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
732 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);
745 /* qdio interrupt handler */
746 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
749 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
750 struct subchannel_id schid;
753 if (!intparm || !irq_ptr) {
754 ccw_device_get_schid(cdev, &schid);
755 DBF_ERROR("qint:%4x", schid.sch_no);
759 if (irq_ptr->perf_stat_enabled)
760 irq_ptr->perf_stat.qdio_int++;
763 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
764 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
765 wake_up(&cdev->private->wait_q);
768 qdio_irq_check_sense(irq_ptr, irb);
769 cstat = irb->scsw.cmd.cstat;
770 dstat = irb->scsw.cmd.dstat;
772 switch (irq_ptr->state) {
773 case QDIO_IRQ_STATE_INACTIVE:
774 qdio_establish_handle_irq(irq_ptr, cstat, dstat);
776 case QDIO_IRQ_STATE_CLEANUP:
777 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
779 case QDIO_IRQ_STATE_ESTABLISHED:
780 case QDIO_IRQ_STATE_ACTIVE:
781 if (cstat & SCHN_STAT_PCI) {
782 qdio_int_handler_pci(irq_ptr);
786 qdio_handle_activate_check(irq_ptr, intparm, cstat,
789 case QDIO_IRQ_STATE_STOPPED:
794 wake_up(&cdev->private->wait_q);
798 * qdio_get_ssqd_desc - get qdio subchannel description
799 * @cdev: ccw device to get description for
800 * @data: where to store the ssqd
802 * Returns 0 or an error code. The results of the chsc are stored in the
803 * specified structure.
805 int qdio_get_ssqd_desc(struct ccw_device *cdev,
806 struct qdio_ssqd_desc *data)
808 struct subchannel_id schid;
810 if (!cdev || !cdev->private)
813 ccw_device_get_schid(cdev, &schid);
814 DBF_EVENT("get ssqd:%4x", schid.sch_no);
815 return qdio_setup_get_ssqd(NULL, &schid, data);
817 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
819 static int qdio_cancel_ccw(struct qdio_irq *irq, int how)
821 struct ccw_device *cdev = irq->cdev;
825 spin_lock_irq(get_ccwdev_lock(cdev));
826 qdio_set_state(irq, QDIO_IRQ_STATE_CLEANUP);
827 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
828 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
830 /* default behaviour is halt */
831 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
832 spin_unlock_irq(get_ccwdev_lock(cdev));
834 DBF_ERROR("%4x SHUTD ERR", irq->schid.sch_no);
835 DBF_ERROR("rc:%4d", rc);
839 timeout = wait_event_interruptible_timeout(cdev->private->wait_q,
840 irq->state == QDIO_IRQ_STATE_INACTIVE ||
841 irq->state == QDIO_IRQ_STATE_ERR,
844 rc = (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
850 * qdio_shutdown - shut down a qdio subchannel
851 * @cdev: associated ccw device
852 * @how: use halt or clear to shutdown
854 int qdio_shutdown(struct ccw_device *cdev, int how)
856 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
857 struct subchannel_id schid;
863 WARN_ON_ONCE(irqs_disabled());
864 ccw_device_get_schid(cdev, &schid);
865 DBF_EVENT("qshutdown:%4x", schid.sch_no);
867 mutex_lock(&irq_ptr->setup_mutex);
869 * Subchannel was already shot down. We cannot prevent being called
870 * twice since cio may trigger a shutdown asynchronously.
872 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
873 mutex_unlock(&irq_ptr->setup_mutex);
878 * Indicate that the device is going down.
880 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
882 qdio_shutdown_debug_entries(irq_ptr);
884 rc = qdio_cancel_ccw(irq_ptr, how);
885 qdio_shutdown_thinint(irq_ptr);
886 qdio_shutdown_irq(irq_ptr);
888 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
889 mutex_unlock(&irq_ptr->setup_mutex);
894 EXPORT_SYMBOL_GPL(qdio_shutdown);
897 * qdio_free - free data structures for a qdio subchannel
898 * @cdev: associated ccw device
900 int qdio_free(struct ccw_device *cdev)
902 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
903 struct subchannel_id schid;
908 ccw_device_get_schid(cdev, &schid);
909 DBF_EVENT("qfree:%4x", schid.sch_no);
910 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
911 mutex_lock(&irq_ptr->setup_mutex);
913 irq_ptr->debug_area = NULL;
914 cdev->private->qdio_data = NULL;
915 mutex_unlock(&irq_ptr->setup_mutex);
917 qdio_free_queues(irq_ptr);
918 free_page((unsigned long) irq_ptr->qdr);
919 free_page(irq_ptr->chsc_page);
921 free_page((unsigned long) irq_ptr);
924 EXPORT_SYMBOL_GPL(qdio_free);
927 * qdio_allocate - allocate qdio queues and associated data
928 * @cdev: associated ccw device
929 * @no_input_qs: allocate this number of Input Queues
930 * @no_output_qs: allocate this number of Output Queues
932 int qdio_allocate(struct ccw_device *cdev, unsigned int no_input_qs,
933 unsigned int no_output_qs)
935 struct subchannel_id schid;
936 struct qdio_irq *irq_ptr;
939 ccw_device_get_schid(cdev, &schid);
940 DBF_EVENT("qallocate:%4x", schid.sch_no);
942 if (no_input_qs > QDIO_MAX_QUEUES_PER_IRQ ||
943 no_output_qs > QDIO_MAX_QUEUES_PER_IRQ)
946 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL);
950 irq_ptr->ccw = kmalloc(sizeof(*irq_ptr->ccw), GFP_KERNEL | GFP_DMA);
954 /* kmemleak doesn't scan the page-allocated irq_ptr: */
955 kmemleak_not_leak(irq_ptr->ccw);
957 irq_ptr->cdev = cdev;
958 mutex_init(&irq_ptr->setup_mutex);
959 if (qdio_allocate_dbf(irq_ptr))
962 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "alloc niq:%1u noq:%1u", no_input_qs,
966 * Allocate a page for the chsc calls in qdio_establish.
967 * Must be pre-allocated since a zfcp recovery will call
968 * qdio_establish. In case of low memory and swap on a zfcp disk
969 * we may not be able to allocate memory otherwise.
971 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
972 if (!irq_ptr->chsc_page)
975 /* qdr is used in ccw1.cda which is u32 */
976 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
980 rc = qdio_allocate_qs(irq_ptr, no_input_qs, no_output_qs);
984 cdev->private->qdio_data = irq_ptr;
985 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
989 free_page((unsigned long) irq_ptr->qdr);
991 free_page(irq_ptr->chsc_page);
996 free_page((unsigned long) irq_ptr);
999 EXPORT_SYMBOL_GPL(qdio_allocate);
1001 static void qdio_trace_init_data(struct qdio_irq *irq,
1002 struct qdio_initialize *data)
1004 DBF_DEV_EVENT(DBF_ERR, irq, "qfmt:%1u", data->q_format);
1005 DBF_DEV_EVENT(DBF_ERR, irq, "qpff%4x", data->qib_param_field_format);
1006 DBF_DEV_HEX(irq, &data->qib_param_field, sizeof(void *), DBF_ERR);
1007 DBF_DEV_EVENT(DBF_ERR, irq, "niq:%1u noq:%1u", data->no_input_qs,
1008 data->no_output_qs);
1009 DBF_DEV_HEX(irq, &data->input_handler, sizeof(void *), DBF_ERR);
1010 DBF_DEV_HEX(irq, &data->output_handler, sizeof(void *), DBF_ERR);
1011 DBF_DEV_HEX(irq, &data->int_parm, sizeof(long), DBF_ERR);
1012 DBF_DEV_HEX(irq, &data->input_sbal_addr_array, sizeof(void *), DBF_ERR);
1013 DBF_DEV_HEX(irq, &data->output_sbal_addr_array, sizeof(void *),
1018 * qdio_establish - establish queues on a qdio subchannel
1019 * @cdev: associated ccw device
1020 * @init_data: initialization data
1022 int qdio_establish(struct ccw_device *cdev,
1023 struct qdio_initialize *init_data)
1025 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1026 struct subchannel_id schid;
1031 ccw_device_get_schid(cdev, &schid);
1032 DBF_EVENT("qestablish:%4x", schid.sch_no);
1037 if (init_data->no_input_qs > irq_ptr->max_input_qs ||
1038 init_data->no_output_qs > irq_ptr->max_output_qs)
1041 /* Needed as error_handler: */
1042 if (!init_data->input_handler)
1045 if (init_data->no_output_qs && !init_data->output_handler)
1048 if (!init_data->input_sbal_addr_array ||
1049 !init_data->output_sbal_addr_array)
1052 if (!init_data->irq_poll)
1055 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_EQUEUE);
1057 DBF_ERROR("%4x NO EQ", schid.sch_no);
1061 mutex_lock(&irq_ptr->setup_mutex);
1062 qdio_trace_init_data(irq_ptr, init_data);
1063 qdio_setup_irq(irq_ptr, init_data);
1065 rc = qdio_establish_thinint(irq_ptr);
1070 irq_ptr->ccw->cmd_code = ciw->cmd;
1071 irq_ptr->ccw->flags = CCW_FLAG_SLI;
1072 irq_ptr->ccw->count = ciw->count;
1073 irq_ptr->ccw->cda = (u32) virt_to_phys(irq_ptr->qdr);
1075 spin_lock_irq(get_ccwdev_lock(cdev));
1076 ccw_device_set_options_mask(cdev, 0);
1078 rc = ccw_device_start(cdev, irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1079 spin_unlock_irq(get_ccwdev_lock(cdev));
1081 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1082 DBF_ERROR("rc:%4x", rc);
1086 timeout = wait_event_interruptible_timeout(cdev->private->wait_q,
1087 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1088 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1090 rc = (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
1091 goto err_ccw_timeout;
1094 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1099 qdio_setup_ssqd_info(irq_ptr);
1101 /* qebsm is now setup if available, initialize buffer states */
1102 qdio_init_buf_states(irq_ptr);
1104 mutex_unlock(&irq_ptr->setup_mutex);
1105 qdio_print_subchannel_info(irq_ptr);
1106 qdio_setup_debug_entries(irq_ptr);
1110 qdio_cancel_ccw(irq_ptr, QDIO_FLAG_CLEANUP_USING_CLEAR);
1113 qdio_shutdown_thinint(irq_ptr);
1115 qdio_shutdown_irq(irq_ptr);
1116 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1117 mutex_unlock(&irq_ptr->setup_mutex);
1120 EXPORT_SYMBOL_GPL(qdio_establish);
1123 * qdio_activate - activate queues on a qdio subchannel
1124 * @cdev: associated cdev
1126 int qdio_activate(struct ccw_device *cdev)
1128 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1129 struct subchannel_id schid;
1133 ccw_device_get_schid(cdev, &schid);
1134 DBF_EVENT("qactivate:%4x", schid.sch_no);
1139 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_AQUEUE);
1141 DBF_ERROR("%4x NO AQ", schid.sch_no);
1145 mutex_lock(&irq_ptr->setup_mutex);
1146 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1151 irq_ptr->ccw->cmd_code = ciw->cmd;
1152 irq_ptr->ccw->flags = CCW_FLAG_SLI;
1153 irq_ptr->ccw->count = ciw->count;
1154 irq_ptr->ccw->cda = 0;
1156 spin_lock_irq(get_ccwdev_lock(cdev));
1157 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1159 rc = ccw_device_start(cdev, irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1160 0, DOIO_DENY_PREFETCH);
1161 spin_unlock_irq(get_ccwdev_lock(cdev));
1163 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1164 DBF_ERROR("rc:%4x", rc);
1168 /* wait for subchannel to become active */
1171 switch (irq_ptr->state) {
1172 case QDIO_IRQ_STATE_STOPPED:
1173 case QDIO_IRQ_STATE_ERR:
1177 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1181 mutex_unlock(&irq_ptr->setup_mutex);
1184 EXPORT_SYMBOL_GPL(qdio_activate);
1187 * handle_inbound - reset processed input buffers
1188 * @q: queue containing the buffers
1189 * @bufnr: first buffer to process
1190 * @count: how many buffers are emptied
1192 static int handle_inbound(struct qdio_q *q, int bufnr, int count)
1196 qperf_inc(q, inbound_call);
1198 /* If any processed SBALs are returned to HW, adjust our tracking: */
1199 overlap = min_t(int, count - sub_buf(q->u.in.batch_start, bufnr),
1200 q->u.in.batch_count);
1202 q->u.in.batch_start = add_buf(q->u.in.batch_start, overlap);
1203 q->u.in.batch_count -= overlap;
1206 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1207 atomic_add(count, &q->nr_buf_used);
1209 if (qdio_need_siga_in(q->irq_ptr))
1210 return qdio_siga_input(q);
1216 * qdio_add_bufs_to_input_queue - process buffers on an Input Queue
1217 * @cdev: associated ccw_device for the qdio subchannel
1218 * @q_nr: queue number
1219 * @bufnr: buffer number
1220 * @count: how many buffers to process
1222 int qdio_add_bufs_to_input_queue(struct ccw_device *cdev, unsigned int q_nr,
1223 unsigned int bufnr, unsigned int count)
1225 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1227 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1233 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "addi b:%02x c:%02x", bufnr, count);
1235 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1240 return handle_inbound(irq_ptr->input_qs[q_nr], bufnr, count);
1242 EXPORT_SYMBOL_GPL(qdio_add_bufs_to_input_queue);
1245 * handle_outbound - process filled outbound buffers
1246 * @q: queue containing the buffers
1247 * @bufnr: first buffer to process
1248 * @count: how many buffers are filled
1249 * @aob: asynchronous operation block
1251 static int handle_outbound(struct qdio_q *q, unsigned int bufnr, unsigned int count,
1254 unsigned char state = 0;
1257 qperf_inc(q, outbound_call);
1259 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1260 used = atomic_add_return(count, &q->nr_buf_used);
1262 if (used == QDIO_MAX_BUFFERS_PER_Q)
1263 qperf_inc(q, outbound_queue_full);
1265 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1266 unsigned long phys_aob = aob ? virt_to_phys(aob) : 0;
1268 WARN_ON_ONCE(!IS_ALIGNED(phys_aob, 256));
1269 rc = qdio_kick_outbound_q(q, count, phys_aob);
1270 } else if (qdio_need_siga_sync(q->irq_ptr)) {
1271 rc = qdio_sync_output_queue(q);
1272 } else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1273 get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1274 state == SLSB_CU_OUTPUT_PRIMED) {
1275 /* The previous buffer is not processed yet, tack on. */
1276 qperf_inc(q, fast_requeue);
1278 rc = qdio_kick_outbound_q(q, count, 0);
1285 * qdio_add_bufs_to_output_queue - process buffers on an Output Queue
1286 * @cdev: associated ccw_device for the qdio subchannel
1287 * @q_nr: queue number
1288 * @bufnr: buffer number
1289 * @count: how many buffers to process
1290 * @aob: asynchronous operation block
1292 int qdio_add_bufs_to_output_queue(struct ccw_device *cdev, unsigned int q_nr,
1293 unsigned int bufnr, unsigned int count,
1296 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1298 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1304 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "addo b:%02x c:%02x", bufnr, count);
1306 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1311 return handle_outbound(irq_ptr->output_qs[q_nr], bufnr, count, aob);
1313 EXPORT_SYMBOL_GPL(qdio_add_bufs_to_output_queue);
1316 * qdio_start_irq - enable interrupt processing for the device
1317 * @cdev: associated ccw_device for the qdio subchannel
1321 * 1 - irqs not started since new data is available
1323 int qdio_start_irq(struct ccw_device *cdev)
1326 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1332 for_each_input_queue(irq_ptr, q, i)
1333 qdio_stop_polling(q);
1335 clear_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
1338 * We need to check again to not lose initiative after
1339 * resetting the ACK state.
1341 if (test_nonshared_ind(irq_ptr))
1344 for_each_input_queue(irq_ptr, q, i) {
1345 if (!qdio_inbound_q_done(q, q->first_to_check))
1352 if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1358 EXPORT_SYMBOL(qdio_start_irq);
1361 * qdio_stop_irq - disable interrupt processing for the device
1362 * @cdev: associated ccw_device for the qdio subchannel
1365 * 0 - interrupts were already disabled
1366 * 1 - interrupts successfully disabled
1368 int qdio_stop_irq(struct ccw_device *cdev)
1370 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1375 if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1380 EXPORT_SYMBOL(qdio_stop_irq);
1382 static int __init init_QDIO(void)
1386 rc = qdio_debug_init();
1389 rc = qdio_setup_init();
1392 rc = qdio_thinint_init();
1404 static void __exit exit_QDIO(void)
1406 qdio_thinint_exit();
1411 module_init(init_QDIO);
1412 module_exit(exit_QDIO);