blob: c6937a044e8a72e724abb65f01bd5ebe17e706ea [file] [log] [blame]
Heiko J Schickfab97222006-09-22 15:22:22 -07001/*
2 * IBM eServer eHCA Infiniband device driver for Linux on POWER
3 *
4 * internal queue handling
5 *
6 * Authors: Waleri Fomin <fomin@de.ibm.com>
7 * Reinhard Ernst <rernst@de.ibm.com>
8 * Christoph Raisch <raisch@de.ibm.com>
9 *
10 * Copyright (c) 2005 IBM Corporation
11 *
12 * All rights reserved.
13 *
14 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
15 * BSD.
16 *
17 * OpenIB BSD License
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
21 *
22 * Redistributions of source code must retain the above copyright notice, this
23 * list of conditions and the following disclaimer.
24 *
25 * Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials
28 * provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
38 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43#ifndef __IPZ_PT_FN_H__
44#define __IPZ_PT_FN_H__
45
46#define EHCA_PAGESHIFT 12
47#define EHCA_PAGESIZE 4096UL
48#define EHCA_PAGEMASK (~(EHCA_PAGESIZE-1))
49#define EHCA_PT_ENTRIES 512UL
50
51#include "ehca_tools.h"
52#include "ehca_qes.h"
53
Stefan Roschere2f81da2007-07-20 16:04:17 +020054struct ehca_pd;
55struct ipz_small_queue_page;
56
Heiko J Schickfab97222006-09-22 15:22:22 -070057/* struct generic ehca page */
58struct ipz_page {
59 u8 entries[EHCA_PAGESIZE];
60};
61
Stefan Roschere2f81da2007-07-20 16:04:17 +020062#define IPZ_SPAGE_PER_KPAGE (PAGE_SIZE / 512)
63
64struct ipz_small_queue_page {
65 unsigned long page;
66 unsigned long bitmap[IPZ_SPAGE_PER_KPAGE / BITS_PER_LONG];
67 int fill;
68 void *mapped_addr;
69 u32 mmap_count;
70 struct list_head list;
71};
72
Heiko J Schickfab97222006-09-22 15:22:22 -070073/* struct generic queue in linux kernel virtual memory (kv) */
74struct ipz_queue {
75 u64 current_q_offset; /* current queue entry */
76
77 struct ipz_page **queue_pages; /* array of pages belonging to queue */
78 u32 qe_size; /* queue entry size */
79 u32 act_nr_of_sg;
80 u32 queue_length; /* queue length allocated in bytes */
81 u32 pagesize;
82 u32 toggle_state; /* toggle flag - per page */
Stefan Roschere2f81da2007-07-20 16:04:17 +020083 u32 offset; /* save offset within page for small_qp */
84 struct ipz_small_queue_page *small_page;
Heiko J Schickfab97222006-09-22 15:22:22 -070085};
86
87/*
88 * return current Queue Entry for a certain q_offset
89 * returns address (kv) of Queue Entry
90 */
91static inline void *ipz_qeit_calc(struct ipz_queue *queue, u64 q_offset)
92{
93 struct ipz_page *current_page;
94 if (q_offset >= queue->queue_length)
95 return NULL;
96 current_page = (queue->queue_pages)[q_offset >> EHCA_PAGESHIFT];
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +010097 return &current_page->entries[q_offset & (EHCA_PAGESIZE - 1)];
Heiko J Schickfab97222006-09-22 15:22:22 -070098}
99
100/*
101 * return current Queue Entry
102 * returns address (kv) of Queue Entry
103 */
104static inline void *ipz_qeit_get(struct ipz_queue *queue)
105{
106 return ipz_qeit_calc(queue, queue->current_q_offset);
107}
108
109/*
110 * return current Queue Page , increment Queue Page iterator from
111 * page to page in struct ipz_queue, last increment will return 0! and
112 * NOT wrap
113 * returns address (kv) of Queue Page
114 * warning don't use in parallel with ipz_QE_get_inc()
115 */
116void *ipz_qpageit_get_inc(struct ipz_queue *queue);
117
118/*
119 * return current Queue Entry, increment Queue Entry iterator by one
120 * step in struct ipz_queue, will wrap in ringbuffer
121 * returns address (kv) of Queue Entry BEFORE increment
122 * warning don't use in parallel with ipz_qpageit_get_inc()
Heiko J Schickfab97222006-09-22 15:22:22 -0700123 */
124static inline void *ipz_qeit_get_inc(struct ipz_queue *queue)
125{
126 void *ret = ipz_qeit_get(queue);
127 queue->current_q_offset += queue->qe_size;
128 if (queue->current_q_offset >= queue->queue_length) {
129 queue->current_q_offset = 0;
130 /* toggle the valid flag */
131 queue->toggle_state = (~queue->toggle_state) & 1;
132 }
133
134 return ret;
135}
136
137/*
Joachim Fenkesfffba372007-05-24 16:51:05 +0200138 * return a bool indicating whether current Queue Entry is valid
139 */
140static inline int ipz_qeit_is_valid(struct ipz_queue *queue)
141{
142 struct ehca_cqe *cqe = ipz_qeit_get(queue);
143 return ((cqe->cqe_flags >> 7) == (queue->toggle_state & 1));
144}
145
146/*
Heiko J Schickfab97222006-09-22 15:22:22 -0700147 * return current Queue Entry, increment Queue Entry iterator by one
148 * step in struct ipz_queue, will wrap in ringbuffer
149 * returns address (kv) of Queue Entry BEFORE increment
150 * returns 0 and does not increment, if wrong valid state
151 * warning don't use in parallel with ipz_qpageit_get_inc()
Heiko J Schickfab97222006-09-22 15:22:22 -0700152 */
153static inline void *ipz_qeit_get_inc_valid(struct ipz_queue *queue)
154{
Joachim Fenkesfffba372007-05-24 16:51:05 +0200155 return ipz_qeit_is_valid(queue) ? ipz_qeit_get_inc(queue) : NULL;
Roland Dreiered23a722007-05-06 21:02:48 -0700156}
157
Heiko J Schickfab97222006-09-22 15:22:22 -0700158/*
159 * returns and resets Queue Entry iterator
160 * returns address (kv) of first Queue Entry
161 */
162static inline void *ipz_qeit_reset(struct ipz_queue *queue)
163{
164 queue->current_q_offset = 0;
165 return ipz_qeit_get(queue);
166}
167
Hoang-Nam Nguyen2771e9e2006-11-20 23:54:12 +0100168/*
169 * return the q_offset corresponding to an absolute address
170 */
171int ipz_queue_abs_to_offset(struct ipz_queue *queue, u64 addr, u64 *q_offset);
172
173/*
174 * return the next queue offset. don't modify the queue.
175 */
176static inline u64 ipz_queue_advance_offset(struct ipz_queue *queue, u64 offset)
177{
178 offset += queue->qe_size;
179 if (offset >= queue->queue_length) offset = 0;
180 return offset;
181}
182
Heiko J Schickfab97222006-09-22 15:22:22 -0700183/* struct generic page table */
184struct ipz_pt {
185 u64 entries[EHCA_PT_ENTRIES];
186};
187
188/* struct page table for a queue, only to be used in pf */
189struct ipz_qpt {
190 /* queue page tables (kv), use u64 because we know the element length */
191 u64 *qpts;
192 u32 n_qpts;
193 u32 n_ptes; /* number of page table entries */
194 u64 *current_pte_addr;
195};
196
197/*
198 * constructor for a ipz_queue_t, placement new for ipz_queue_t,
199 * new for all dependent datastructors
200 * all QP Tables are the same
201 * flow:
202 * allocate+pin queue
203 * see ipz_qpt_ctor()
204 * returns true if ok, false if out of memory
205 */
Stefan Roschere2f81da2007-07-20 16:04:17 +0200206int ipz_queue_ctor(struct ehca_pd *pd, struct ipz_queue *queue,
207 const u32 nr_of_pages, const u32 pagesize,
208 const u32 qe_size, const u32 nr_of_sg,
209 int is_small);
Heiko J Schickfab97222006-09-22 15:22:22 -0700210
211/*
212 * destructor for a ipz_queue_t
213 * -# free queue
214 * see ipz_queue_ctor()
215 * returns true if ok, false if queue was NULL-ptr of free failed
216 */
Stefan Roschere2f81da2007-07-20 16:04:17 +0200217int ipz_queue_dtor(struct ehca_pd *pd, struct ipz_queue *queue);
Heiko J Schickfab97222006-09-22 15:22:22 -0700218
219/*
220 * constructor for a ipz_qpt_t,
221 * placement new for struct ipz_queue, new for all dependent datastructors
222 * all QP Tables are the same,
223 * flow:
224 * -# allocate+pin queue
225 * -# initialise ptcb
226 * -# allocate+pin PTs
227 * -# link PTs to a ring, according to HCA Arch, set bit62 id needed
228 * -# the ring must have room for exactly nr_of_PTEs
229 * see ipz_qpt_ctor()
230 */
231void ipz_qpt_ctor(struct ipz_qpt *qpt,
232 const u32 nr_of_qes,
233 const u32 pagesize,
234 const u32 qe_size,
235 const u8 lowbyte, const u8 toggle,
236 u32 * act_nr_of_QEs, u32 * act_nr_of_pages);
237
238/*
239 * return current Queue Entry, increment Queue Entry iterator by one
240 * step in struct ipz_queue, will wrap in ringbuffer
241 * returns address (kv) of Queue Entry BEFORE increment
242 * warning don't use in parallel with ipz_qpageit_get_inc()
243 * warning unpredictable results may occur if steps>act_nr_of_queue_entries
244 * fix EQ page problems
245 */
246void *ipz_qeit_eq_get_inc(struct ipz_queue *queue);
247
248/*
249 * return current Event Queue Entry, increment Queue Entry iterator
250 * by one step in struct ipz_queue if valid, will wrap in ringbuffer
251 * returns address (kv) of Queue Entry BEFORE increment
252 * returns 0 and does not increment, if wrong valid state
253 * warning don't use in parallel with ipz_queue_QPageit_get_inc()
254 * warning unpredictable results may occur if steps>act_nr_of_queue_entries
255 */
256static inline void *ipz_eqit_eq_get_inc_valid(struct ipz_queue *queue)
257{
258 void *ret = ipz_qeit_get(queue);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200259 u32 qe = *(u8 *)ret;
Heiko J Schickfab97222006-09-22 15:22:22 -0700260 if ((qe >> 7) != (queue->toggle_state & 1))
261 return NULL;
262 ipz_qeit_eq_get_inc(queue); /* this is a good one */
263 return ret;
264}
265
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100266static inline void *ipz_eqit_eq_peek_valid(struct ipz_queue *queue)
267{
268 void *ret = ipz_qeit_get(queue);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200269 u32 qe = *(u8 *)ret;
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100270 if ((qe >> 7) != (queue->toggle_state & 1))
271 return NULL;
272 return ret;
273}
274
Heiko J Schickfab97222006-09-22 15:22:22 -0700275/* returns address (GX) of first queue entry */
276static inline u64 ipz_qpt_get_firstpage(struct ipz_qpt *qpt)
277{
278 return be64_to_cpu(qpt->qpts[0]);
279}
280
281/* returns address (kv) of first page of queue page table */
282static inline void *ipz_qpt_get_qpt(struct ipz_qpt *qpt)
283{
284 return qpt->qpts;
285}
286
287#endif /* __IPZ_PT_FN_H__ */