2 * linux/drivers/net/ehea/ehea_qmr.c
4 * eHEA ethernet device driver for IBM eServer System p
6 * (C) Copyright IBM Corp. 2006
9 * Christoph Raisch <raisch@de.ibm.com>
10 * Jan-Bernd Themann <themann@de.ibm.com>
11 * Thomas Klein <tklein@de.ibm.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "ehea_phyp.h"
34 static void *hw_qpageit_get_inc(struct hw_queue *queue)
36 void *retvalue = hw_qeit_get(queue);
38 queue->current_q_offset += queue->pagesize;
39 if (queue->current_q_offset > queue->queue_length) {
40 queue->current_q_offset -= queue->pagesize;
42 } else if (((u64) retvalue) & (EHEA_PAGESIZE-1)) {
43 ehea_error("not on pageboundary");
49 static int hw_queue_ctor(struct hw_queue *queue, const u32 nr_of_pages,
50 const u32 pagesize, const u32 qe_size)
52 int pages_per_kpage = PAGE_SIZE / pagesize;
55 if ((pagesize > PAGE_SIZE) || (!pages_per_kpage)) {
56 ehea_error("pagesize conflict! kernel pagesize=%d, "
57 "ehea pagesize=%d", (int)PAGE_SIZE, (int)pagesize);
61 queue->queue_length = nr_of_pages * pagesize;
62 queue->queue_pages = kmalloc(nr_of_pages * sizeof(void*), GFP_KERNEL);
63 if (!queue->queue_pages) {
64 ehea_error("no mem for queue_pages");
69 * allocate pages for queue:
70 * outer loop allocates whole kernel pages (page aligned) and
71 * inner loop divides a kernel page into smaller hea queue pages
74 while (i < nr_of_pages) {
75 u8 *kpage = (u8*)get_zeroed_page(GFP_KERNEL);
78 for (k = 0; k < pages_per_kpage && i < nr_of_pages; k++) {
79 (queue->queue_pages)[i] = (struct ehea_page*)kpage;
85 queue->current_q_offset = 0;
86 queue->qe_size = qe_size;
87 queue->pagesize = pagesize;
88 queue->toggle_state = 1;
92 for (i = 0; i < nr_of_pages; i += pages_per_kpage) {
93 if (!(queue->queue_pages)[i])
95 free_page((unsigned long)(queue->queue_pages)[i]);
100 static void hw_queue_dtor(struct hw_queue *queue)
102 int pages_per_kpage = PAGE_SIZE / queue->pagesize;
105 if (!queue || !queue->queue_pages)
108 nr_pages = queue->queue_length / queue->pagesize;
110 for (i = 0; i < nr_pages; i += pages_per_kpage)
111 free_page((unsigned long)(queue->queue_pages)[i]);
113 kfree(queue->queue_pages);
116 struct ehea_cq *ehea_create_cq(struct ehea_adapter *adapter,
117 int nr_of_cqe, u64 eq_handle, u32 cq_token)
121 u64 *cq_handle_ref, hret, rpage;
122 u32 act_nr_of_entries, act_pages, counter;
126 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
128 ehea_error("no mem for cq");
132 cq->attr.max_nr_of_cqes = nr_of_cqe;
133 cq->attr.cq_token = cq_token;
134 cq->attr.eq_handle = eq_handle;
136 cq->adapter = adapter;
138 cq_handle_ref = &cq->fw_handle;
139 act_nr_of_entries = 0;
142 hret = ehea_h_alloc_resource_cq(adapter->handle, &cq->attr,
143 &cq->fw_handle, &cq->epas);
144 if (hret != H_SUCCESS) {
145 ehea_error("alloc_resource_cq failed");
149 ret = hw_queue_ctor(&cq->hw_queue, cq->attr.nr_pages,
150 EHEA_PAGESIZE, sizeof(struct ehea_cqe));
154 for (counter = 0; counter < cq->attr.nr_pages; counter++) {
155 vpage = hw_qpageit_get_inc(&cq->hw_queue);
157 ehea_error("hw_qpageit_get_inc failed");
161 rpage = virt_to_abs(vpage);
162 hret = ehea_h_register_rpage(adapter->handle,
163 0, EHEA_CQ_REGISTER_ORIG,
164 cq->fw_handle, rpage, 1);
165 if (hret < H_SUCCESS) {
166 ehea_error("register_rpage_cq failed ehea_cq=%p "
167 "hret=%lx counter=%i act_pages=%i",
168 cq, hret, counter, cq->attr.nr_pages);
172 if (counter == (cq->attr.nr_pages - 1)) {
173 vpage = hw_qpageit_get_inc(&cq->hw_queue);
175 if ((hret != H_SUCCESS) || (vpage)) {
176 ehea_error("registration of pages not "
177 "complete hret=%lx\n", hret);
181 if ((hret != H_PAGE_REGISTERED) || (!vpage)) {
182 ehea_error("CQ: registration of page failed "
189 hw_qeit_reset(&cq->hw_queue);
190 epa = cq->epas.kernel;
191 ehea_reset_cq_ep(cq);
192 ehea_reset_cq_n1(cq);
197 hw_queue_dtor(&cq->hw_queue);
200 ehea_h_free_resource(adapter->handle, cq->fw_handle, FORCE_FREE);
209 u64 ehea_destroy_cq_res(struct ehea_cq *cq, u64 force)
212 u64 adapter_handle = cq->adapter->handle;
214 /* deregister all previous registered pages */
215 hret = ehea_h_free_resource(adapter_handle, cq->fw_handle, force);
216 if (hret != H_SUCCESS)
219 hw_queue_dtor(&cq->hw_queue);
225 int ehea_destroy_cq(struct ehea_cq *cq)
231 if ((hret = ehea_destroy_cq_res(cq, NORMAL_FREE)) == H_R_STATE) {
232 ehea_error_data(cq->adapter, cq->fw_handle);
233 hret = ehea_destroy_cq_res(cq, FORCE_FREE);
236 if (hret != H_SUCCESS) {
237 ehea_error("destroy CQ failed");
244 struct ehea_eq *ehea_create_eq(struct ehea_adapter *adapter,
245 const enum ehea_eq_type type,
246 const u32 max_nr_of_eqes, const u8 eqe_gen)
253 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
255 ehea_error("no mem for eq");
259 eq->adapter = adapter;
260 eq->attr.type = type;
261 eq->attr.max_nr_of_eqes = max_nr_of_eqes;
262 eq->attr.eqe_gen = eqe_gen;
263 spin_lock_init(&eq->spinlock);
265 hret = ehea_h_alloc_resource_eq(adapter->handle,
266 &eq->attr, &eq->fw_handle);
267 if (hret != H_SUCCESS) {
268 ehea_error("alloc_resource_eq failed");
272 ret = hw_queue_ctor(&eq->hw_queue, eq->attr.nr_pages,
273 EHEA_PAGESIZE, sizeof(struct ehea_eqe));
275 ehea_error("can't allocate eq pages");
279 for (i = 0; i < eq->attr.nr_pages; i++) {
280 vpage = hw_qpageit_get_inc(&eq->hw_queue);
282 ehea_error("hw_qpageit_get_inc failed");
287 rpage = virt_to_abs(vpage);
289 hret = ehea_h_register_rpage(adapter->handle, 0,
290 EHEA_EQ_REGISTER_ORIG,
291 eq->fw_handle, rpage, 1);
293 if (i == (eq->attr.nr_pages - 1)) {
295 vpage = hw_qpageit_get_inc(&eq->hw_queue);
296 if ((hret != H_SUCCESS) || (vpage)) {
300 if ((hret != H_PAGE_REGISTERED) || (!vpage)) {
306 hw_qeit_reset(&eq->hw_queue);
310 hw_queue_dtor(&eq->hw_queue);
313 ehea_h_free_resource(adapter->handle, eq->fw_handle, FORCE_FREE);
320 struct ehea_eqe *ehea_poll_eq(struct ehea_eq *eq)
322 struct ehea_eqe *eqe;
325 spin_lock_irqsave(&eq->spinlock, flags);
326 eqe = (struct ehea_eqe*)hw_eqit_eq_get_inc_valid(&eq->hw_queue);
327 spin_unlock_irqrestore(&eq->spinlock, flags);
332 u64 ehea_destroy_eq_res(struct ehea_eq *eq, u64 force)
337 spin_lock_irqsave(&eq->spinlock, flags);
339 hret = ehea_h_free_resource(eq->adapter->handle, eq->fw_handle, force);
340 spin_unlock_irqrestore(&eq->spinlock, flags);
342 if (hret != H_SUCCESS)
345 hw_queue_dtor(&eq->hw_queue);
351 int ehea_destroy_eq(struct ehea_eq *eq)
357 if ((hret = ehea_destroy_eq_res(eq, NORMAL_FREE)) == H_R_STATE) {
358 ehea_error_data(eq->adapter, eq->fw_handle);
359 hret = ehea_destroy_eq_res(eq, FORCE_FREE);
362 if (hret != H_SUCCESS) {
363 ehea_error("destroy EQ failed");
371 * allocates memory for a queue and registers pages in phyp
373 int ehea_qp_alloc_register(struct ehea_qp *qp, struct hw_queue *hw_queue,
374 int nr_pages, int wqe_size, int act_nr_sges,
375 struct ehea_adapter *adapter, int h_call_q_selector)
381 ret = hw_queue_ctor(hw_queue, nr_pages, EHEA_PAGESIZE, wqe_size);
385 for (cnt = 0; cnt < nr_pages; cnt++) {
386 vpage = hw_qpageit_get_inc(hw_queue);
388 ehea_error("hw_qpageit_get_inc failed");
391 rpage = virt_to_abs(vpage);
392 hret = ehea_h_register_rpage(adapter->handle,
393 0, h_call_q_selector,
394 qp->fw_handle, rpage, 1);
395 if (hret < H_SUCCESS) {
396 ehea_error("register_rpage_qp failed");
400 hw_qeit_reset(hw_queue);
404 hw_queue_dtor(hw_queue);
408 static inline u32 map_wqe_size(u8 wqe_enc_size)
410 return 128 << wqe_enc_size;
413 struct ehea_qp *ehea_create_qp(struct ehea_adapter *adapter,
414 u32 pd, struct ehea_qp_init_attr *init_attr)
419 u32 wqe_size_in_bytes_sq, wqe_size_in_bytes_rq1;
420 u32 wqe_size_in_bytes_rq2, wqe_size_in_bytes_rq3;
423 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
425 ehea_error("no mem for qp");
429 qp->adapter = adapter;
431 hret = ehea_h_alloc_resource_qp(adapter->handle, init_attr, pd,
432 &qp->fw_handle, &qp->epas);
433 if (hret != H_SUCCESS) {
434 ehea_error("ehea_h_alloc_resource_qp failed");
438 wqe_size_in_bytes_sq = map_wqe_size(init_attr->act_wqe_size_enc_sq);
439 wqe_size_in_bytes_rq1 = map_wqe_size(init_attr->act_wqe_size_enc_rq1);
440 wqe_size_in_bytes_rq2 = map_wqe_size(init_attr->act_wqe_size_enc_rq2);
441 wqe_size_in_bytes_rq3 = map_wqe_size(init_attr->act_wqe_size_enc_rq3);
443 ret = ehea_qp_alloc_register(qp, &qp->hw_squeue, init_attr->nr_sq_pages,
444 wqe_size_in_bytes_sq,
445 init_attr->act_wqe_size_enc_sq, adapter,
448 ehea_error("can't register for sq ret=%x", ret);
452 ret = ehea_qp_alloc_register(qp, &qp->hw_rqueue1,
453 init_attr->nr_rq1_pages,
454 wqe_size_in_bytes_rq1,
455 init_attr->act_wqe_size_enc_rq1,
458 ehea_error("can't register for rq1 ret=%x", ret);
462 if (init_attr->rq_count > 1) {
463 ret = ehea_qp_alloc_register(qp, &qp->hw_rqueue2,
464 init_attr->nr_rq2_pages,
465 wqe_size_in_bytes_rq2,
466 init_attr->act_wqe_size_enc_rq2,
469 ehea_error("can't register for rq2 ret=%x", ret);
474 if (init_attr->rq_count > 2) {
475 ret = ehea_qp_alloc_register(qp, &qp->hw_rqueue3,
476 init_attr->nr_rq3_pages,
477 wqe_size_in_bytes_rq3,
478 init_attr->act_wqe_size_enc_rq3,
481 ehea_error("can't register for rq3 ret=%x", ret);
486 qp->init_attr = *init_attr;
491 hw_queue_dtor(&qp->hw_rqueue2);
494 hw_queue_dtor(&qp->hw_rqueue1);
497 hw_queue_dtor(&qp->hw_squeue);
500 ehea_h_disable_and_get_hea(adapter->handle, qp->fw_handle);
501 ehea_h_free_resource(adapter->handle, qp->fw_handle, FORCE_FREE);
508 u64 ehea_destroy_qp_res(struct ehea_qp *qp, u64 force)
511 struct ehea_qp_init_attr *qp_attr = &qp->init_attr;
514 ehea_h_disable_and_get_hea(qp->adapter->handle, qp->fw_handle);
515 hret = ehea_h_free_resource(qp->adapter->handle, qp->fw_handle, force);
516 if (hret != H_SUCCESS)
519 hw_queue_dtor(&qp->hw_squeue);
520 hw_queue_dtor(&qp->hw_rqueue1);
522 if (qp_attr->rq_count > 1)
523 hw_queue_dtor(&qp->hw_rqueue2);
524 if (qp_attr->rq_count > 2)
525 hw_queue_dtor(&qp->hw_rqueue3);
531 int ehea_destroy_qp(struct ehea_qp *qp)
537 if ((hret = ehea_destroy_qp_res(qp, NORMAL_FREE)) == H_R_STATE) {
538 ehea_error_data(qp->adapter, qp->fw_handle);
539 hret = ehea_destroy_qp_res(qp, FORCE_FREE);
542 if (hret != H_SUCCESS) {
543 ehea_error("destroy QP failed");
550 int ehea_reg_kernel_mr(struct ehea_adapter *adapter, struct ehea_mr *mr)
553 u64 hret, pt_abs, start, end, nr_pages;
554 u32 acc_ctrl = EHEA_MR_ACC_CTRL;
558 end = (u64)high_memory;
559 nr_pages = (end - start) / EHEA_PAGESIZE;
561 pt = kzalloc(PAGE_SIZE, GFP_KERNEL);
563 ehea_error("no mem");
567 pt_abs = virt_to_abs(pt);
569 hret = ehea_h_alloc_resource_mr(adapter->handle, start, end - start,
570 acc_ctrl, adapter->pd,
571 &mr->handle, &mr->lkey);
572 if (hret != H_SUCCESS) {
573 ehea_error("alloc_resource_mr failed");
578 mr->vaddr = KERNELBASE;
581 while (nr_pages > 0) {
583 u64 num_pages = min(nr_pages, (u64)512);
584 for (i = 0; i < num_pages; i++)
585 pt[i] = virt_to_abs((void*)(((u64)start) +
589 hret = ehea_h_register_rpage_mr(adapter->handle,
593 nr_pages -= num_pages;
595 u64 abs_adr = virt_to_abs((void*)(((u64)start) +
596 (k * EHEA_PAGESIZE)));
598 hret = ehea_h_register_rpage_mr(adapter->handle,
604 if ((hret != H_SUCCESS) && (hret != H_PAGE_REGISTERED)) {
605 ehea_h_free_resource(adapter->handle,
606 mr->handle, FORCE_FREE);
607 ehea_error("register_rpage_mr failed");
613 if (hret != H_SUCCESS) {
614 ehea_h_free_resource(adapter->handle, mr->handle,
616 ehea_error("register_rpage failed for last page");
621 mr->adapter = adapter;
628 int ehea_rem_mr(struct ehea_mr *mr)
632 if (!mr || !mr->adapter)
635 hret = ehea_h_free_resource(mr->adapter->handle, mr->handle,
637 if (hret != H_SUCCESS) {
638 ehea_error("destroy MR failed");
645 int ehea_gen_smr(struct ehea_adapter *adapter, struct ehea_mr *old_mr,
646 struct ehea_mr *shared_mr)
650 hret = ehea_h_register_smr(adapter->handle, old_mr->handle,
651 old_mr->vaddr, EHEA_MR_ACC_CTRL,
652 adapter->pd, shared_mr);
653 if (hret != H_SUCCESS)
656 shared_mr->adapter = adapter;
661 void print_error_data(u64 *data)
664 u64 type = EHEA_BMASK_GET(ERROR_DATA_TYPE, data[2]);
665 u64 resource = data[1];
667 length = EHEA_BMASK_GET(ERROR_DATA_LENGTH, data[0]);
669 if (length > EHEA_PAGESIZE)
670 length = EHEA_PAGESIZE;
672 if (type == 0x8) /* Queue Pair */
673 ehea_error("QP (resource=%lX) state: AER=0x%lX, AERR=0x%lX, "
674 "port=%lX", resource, data[6], data[12], data[22]);
676 if (type == 0x4) /* Completion Queue */
677 ehea_error("CQ (resource=%lX) state: AER=0x%lX", resource,
680 if (type == 0x3) /* Event Queue */
681 ehea_error("EQ (resource=%lX) state: AER=0x%lX", resource,
684 ehea_dump(data, length, "error data");
687 void ehea_error_data(struct ehea_adapter *adapter, u64 res_handle)
692 rblock = kzalloc(PAGE_SIZE, GFP_KERNEL);
694 ehea_error("Cannot allocate rblock memory.");
698 ret = ehea_h_error_data(adapter->handle,
702 if (ret == H_R_STATE)
703 ehea_error("No error data is available: %lX.", res_handle);
704 else if (ret == H_SUCCESS)
705 print_error_data(rblock);
707 ehea_error("Error data could not be fetched: %lX", res_handle);