blob: 1d24652b2c390f1d26ba0240a05500ac89934668 [file] [log] [blame]
Ralph Campbellf9315512010-05-23 21:44:54 -07001/*
2 * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
3 * All rights reserved.
4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#include <rdma/ib_mad.h>
36#include <rdma/ib_user_verbs.h>
37#include <linux/io.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040038#include <linux/module.h>
Ralph Campbellf9315512010-05-23 21:44:54 -070039#include <linux/utsname.h>
40#include <linux/rculist.h>
41#include <linux/mm.h>
42
43#include "qib.h"
44#include "qib_common.h"
45
46static unsigned int ib_qib_qp_table_size = 251;
47module_param_named(qp_table_size, ib_qib_qp_table_size, uint, S_IRUGO);
48MODULE_PARM_DESC(qp_table_size, "QP table size");
49
50unsigned int ib_qib_lkey_table_size = 16;
51module_param_named(lkey_table_size, ib_qib_lkey_table_size, uint,
52 S_IRUGO);
53MODULE_PARM_DESC(lkey_table_size,
54 "LKEY table size in bits (2^n, 1 <= n <= 23)");
55
56static unsigned int ib_qib_max_pds = 0xFFFF;
57module_param_named(max_pds, ib_qib_max_pds, uint, S_IRUGO);
58MODULE_PARM_DESC(max_pds,
59 "Maximum number of protection domains to support");
60
61static unsigned int ib_qib_max_ahs = 0xFFFF;
62module_param_named(max_ahs, ib_qib_max_ahs, uint, S_IRUGO);
63MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
64
65unsigned int ib_qib_max_cqes = 0x2FFFF;
66module_param_named(max_cqes, ib_qib_max_cqes, uint, S_IRUGO);
67MODULE_PARM_DESC(max_cqes,
68 "Maximum number of completion queue entries to support");
69
70unsigned int ib_qib_max_cqs = 0x1FFFF;
71module_param_named(max_cqs, ib_qib_max_cqs, uint, S_IRUGO);
72MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
73
74unsigned int ib_qib_max_qp_wrs = 0x3FFF;
75module_param_named(max_qp_wrs, ib_qib_max_qp_wrs, uint, S_IRUGO);
76MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
77
78unsigned int ib_qib_max_qps = 16384;
79module_param_named(max_qps, ib_qib_max_qps, uint, S_IRUGO);
80MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
81
82unsigned int ib_qib_max_sges = 0x60;
83module_param_named(max_sges, ib_qib_max_sges, uint, S_IRUGO);
84MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
85
86unsigned int ib_qib_max_mcast_grps = 16384;
87module_param_named(max_mcast_grps, ib_qib_max_mcast_grps, uint, S_IRUGO);
88MODULE_PARM_DESC(max_mcast_grps,
89 "Maximum number of multicast groups to support");
90
91unsigned int ib_qib_max_mcast_qp_attached = 16;
92module_param_named(max_mcast_qp_attached, ib_qib_max_mcast_qp_attached,
93 uint, S_IRUGO);
94MODULE_PARM_DESC(max_mcast_qp_attached,
95 "Maximum number of attached QPs to support");
96
97unsigned int ib_qib_max_srqs = 1024;
98module_param_named(max_srqs, ib_qib_max_srqs, uint, S_IRUGO);
99MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
100
101unsigned int ib_qib_max_srq_sges = 128;
102module_param_named(max_srq_sges, ib_qib_max_srq_sges, uint, S_IRUGO);
103MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
104
105unsigned int ib_qib_max_srq_wrs = 0x1FFFF;
106module_param_named(max_srq_wrs, ib_qib_max_srq_wrs, uint, S_IRUGO);
107MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
108
109static unsigned int ib_qib_disable_sma;
110module_param_named(disable_sma, ib_qib_disable_sma, uint, S_IWUSR | S_IRUGO);
111MODULE_PARM_DESC(disable_sma, "Disable the SMA");
112
113/*
114 * Note that it is OK to post send work requests in the SQE and ERR
115 * states; qib_do_send() will process them and generate error
116 * completions as per IB 1.2 C10-96.
117 */
118const int ib_qib_state_ops[IB_QPS_ERR + 1] = {
119 [IB_QPS_RESET] = 0,
120 [IB_QPS_INIT] = QIB_POST_RECV_OK,
121 [IB_QPS_RTR] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK,
122 [IB_QPS_RTS] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
123 QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK |
124 QIB_PROCESS_NEXT_SEND_OK,
125 [IB_QPS_SQD] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
126 QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK,
127 [IB_QPS_SQE] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
128 QIB_POST_SEND_OK | QIB_FLUSH_SEND,
129 [IB_QPS_ERR] = QIB_POST_RECV_OK | QIB_FLUSH_RECV |
130 QIB_POST_SEND_OK | QIB_FLUSH_SEND,
131};
132
133struct qib_ucontext {
134 struct ib_ucontext ibucontext;
135};
136
137static inline struct qib_ucontext *to_iucontext(struct ib_ucontext
138 *ibucontext)
139{
140 return container_of(ibucontext, struct qib_ucontext, ibucontext);
141}
142
143/*
144 * Translate ib_wr_opcode into ib_wc_opcode.
145 */
146const enum ib_wc_opcode ib_qib_wc_opcode[] = {
147 [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
148 [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
149 [IB_WR_SEND] = IB_WC_SEND,
150 [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
151 [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
152 [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
153 [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
154};
155
156/*
157 * System image GUID.
158 */
159__be64 ib_qib_sys_image_guid;
160
161/**
162 * qib_copy_sge - copy data to SGE memory
163 * @ss: the SGE state
164 * @data: the data to copy
165 * @length: the length of the data
166 */
167void qib_copy_sge(struct qib_sge_state *ss, void *data, u32 length, int release)
168{
169 struct qib_sge *sge = &ss->sge;
170
171 while (length) {
172 u32 len = sge->length;
173
174 if (len > length)
175 len = length;
176 if (len > sge->sge_length)
177 len = sge->sge_length;
178 BUG_ON(len == 0);
179 memcpy(sge->vaddr, data, len);
180 sge->vaddr += len;
181 sge->length -= len;
182 sge->sge_length -= len;
183 if (sge->sge_length == 0) {
184 if (release)
185 atomic_dec(&sge->mr->refcount);
186 if (--ss->num_sge)
187 *sge = *ss->sg_list++;
188 } else if (sge->length == 0 && sge->mr->lkey) {
189 if (++sge->n >= QIB_SEGSZ) {
190 if (++sge->m >= sge->mr->mapsz)
191 break;
192 sge->n = 0;
193 }
194 sge->vaddr =
195 sge->mr->map[sge->m]->segs[sge->n].vaddr;
196 sge->length =
197 sge->mr->map[sge->m]->segs[sge->n].length;
198 }
199 data += len;
200 length -= len;
201 }
202}
203
204/**
205 * qib_skip_sge - skip over SGE memory - XXX almost dup of prev func
206 * @ss: the SGE state
207 * @length: the number of bytes to skip
208 */
209void qib_skip_sge(struct qib_sge_state *ss, u32 length, int release)
210{
211 struct qib_sge *sge = &ss->sge;
212
213 while (length) {
214 u32 len = sge->length;
215
216 if (len > length)
217 len = length;
218 if (len > sge->sge_length)
219 len = sge->sge_length;
220 BUG_ON(len == 0);
221 sge->vaddr += len;
222 sge->length -= len;
223 sge->sge_length -= len;
224 if (sge->sge_length == 0) {
225 if (release)
226 atomic_dec(&sge->mr->refcount);
227 if (--ss->num_sge)
228 *sge = *ss->sg_list++;
229 } else if (sge->length == 0 && sge->mr->lkey) {
230 if (++sge->n >= QIB_SEGSZ) {
231 if (++sge->m >= sge->mr->mapsz)
232 break;
233 sge->n = 0;
234 }
235 sge->vaddr =
236 sge->mr->map[sge->m]->segs[sge->n].vaddr;
237 sge->length =
238 sge->mr->map[sge->m]->segs[sge->n].length;
239 }
240 length -= len;
241 }
242}
243
244/*
245 * Count the number of DMA descriptors needed to send length bytes of data.
246 * Don't modify the qib_sge_state to get the count.
247 * Return zero if any of the segments is not aligned.
248 */
249static u32 qib_count_sge(struct qib_sge_state *ss, u32 length)
250{
251 struct qib_sge *sg_list = ss->sg_list;
252 struct qib_sge sge = ss->sge;
253 u8 num_sge = ss->num_sge;
254 u32 ndesc = 1; /* count the header */
255
256 while (length) {
257 u32 len = sge.length;
258
259 if (len > length)
260 len = length;
261 if (len > sge.sge_length)
262 len = sge.sge_length;
263 BUG_ON(len == 0);
264 if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
265 (len != length && (len & (sizeof(u32) - 1)))) {
266 ndesc = 0;
267 break;
268 }
269 ndesc++;
270 sge.vaddr += len;
271 sge.length -= len;
272 sge.sge_length -= len;
273 if (sge.sge_length == 0) {
274 if (--num_sge)
275 sge = *sg_list++;
276 } else if (sge.length == 0 && sge.mr->lkey) {
277 if (++sge.n >= QIB_SEGSZ) {
278 if (++sge.m >= sge.mr->mapsz)
279 break;
280 sge.n = 0;
281 }
282 sge.vaddr =
283 sge.mr->map[sge.m]->segs[sge.n].vaddr;
284 sge.length =
285 sge.mr->map[sge.m]->segs[sge.n].length;
286 }
287 length -= len;
288 }
289 return ndesc;
290}
291
292/*
293 * Copy from the SGEs to the data buffer.
294 */
295static void qib_copy_from_sge(void *data, struct qib_sge_state *ss, u32 length)
296{
297 struct qib_sge *sge = &ss->sge;
298
299 while (length) {
300 u32 len = sge->length;
301
302 if (len > length)
303 len = length;
304 if (len > sge->sge_length)
305 len = sge->sge_length;
306 BUG_ON(len == 0);
307 memcpy(data, sge->vaddr, len);
308 sge->vaddr += len;
309 sge->length -= len;
310 sge->sge_length -= len;
311 if (sge->sge_length == 0) {
312 if (--ss->num_sge)
313 *sge = *ss->sg_list++;
314 } else if (sge->length == 0 && sge->mr->lkey) {
315 if (++sge->n >= QIB_SEGSZ) {
316 if (++sge->m >= sge->mr->mapsz)
317 break;
318 sge->n = 0;
319 }
320 sge->vaddr =
321 sge->mr->map[sge->m]->segs[sge->n].vaddr;
322 sge->length =
323 sge->mr->map[sge->m]->segs[sge->n].length;
324 }
325 data += len;
326 length -= len;
327 }
328}
329
330/**
331 * qib_post_one_send - post one RC, UC, or UD send work request
332 * @qp: the QP to post on
333 * @wr: the work request to send
334 */
335static int qib_post_one_send(struct qib_qp *qp, struct ib_send_wr *wr)
336{
337 struct qib_swqe *wqe;
338 u32 next;
339 int i;
340 int j;
341 int acc;
342 int ret;
343 unsigned long flags;
344 struct qib_lkey_table *rkt;
345 struct qib_pd *pd;
346
347 spin_lock_irqsave(&qp->s_lock, flags);
348
349 /* Check that state is OK to post send. */
350 if (unlikely(!(ib_qib_state_ops[qp->state] & QIB_POST_SEND_OK)))
351 goto bail_inval;
352
353 /* IB spec says that num_sge == 0 is OK. */
354 if (wr->num_sge > qp->s_max_sge)
355 goto bail_inval;
356
357 /*
358 * Don't allow RDMA reads or atomic operations on UC or
359 * undefined operations.
360 * Make sure buffer is large enough to hold the result for atomics.
361 */
362 if (wr->opcode == IB_WR_FAST_REG_MR) {
363 if (qib_fast_reg_mr(qp, wr))
364 goto bail_inval;
365 } else if (qp->ibqp.qp_type == IB_QPT_UC) {
366 if ((unsigned) wr->opcode >= IB_WR_RDMA_READ)
367 goto bail_inval;
368 } else if (qp->ibqp.qp_type != IB_QPT_RC) {
369 /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
370 if (wr->opcode != IB_WR_SEND &&
371 wr->opcode != IB_WR_SEND_WITH_IMM)
372 goto bail_inval;
373 /* Check UD destination address PD */
374 if (qp->ibqp.pd != wr->wr.ud.ah->pd)
375 goto bail_inval;
376 } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD)
377 goto bail_inval;
378 else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
379 (wr->num_sge == 0 ||
380 wr->sg_list[0].length < sizeof(u64) ||
381 wr->sg_list[0].addr & (sizeof(u64) - 1)))
382 goto bail_inval;
383 else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic)
384 goto bail_inval;
385
386 next = qp->s_head + 1;
387 if (next >= qp->s_size)
388 next = 0;
389 if (next == qp->s_last) {
390 ret = -ENOMEM;
391 goto bail;
392 }
393
394 rkt = &to_idev(qp->ibqp.device)->lk_table;
395 pd = to_ipd(qp->ibqp.pd);
396 wqe = get_swqe_ptr(qp, qp->s_head);
397 wqe->wr = *wr;
398 wqe->length = 0;
399 j = 0;
400 if (wr->num_sge) {
401 acc = wr->opcode >= IB_WR_RDMA_READ ?
402 IB_ACCESS_LOCAL_WRITE : 0;
403 for (i = 0; i < wr->num_sge; i++) {
404 u32 length = wr->sg_list[i].length;
405 int ok;
406
407 if (length == 0)
408 continue;
409 ok = qib_lkey_ok(rkt, pd, &wqe->sg_list[j],
410 &wr->sg_list[i], acc);
411 if (!ok)
412 goto bail_inval_free;
413 wqe->length += length;
414 j++;
415 }
416 wqe->wr.num_sge = j;
417 }
418 if (qp->ibqp.qp_type == IB_QPT_UC ||
419 qp->ibqp.qp_type == IB_QPT_RC) {
420 if (wqe->length > 0x80000000U)
421 goto bail_inval_free;
422 } else if (wqe->length > (dd_from_ibdev(qp->ibqp.device)->pport +
423 qp->port_num - 1)->ibmtu)
424 goto bail_inval_free;
425 else
426 atomic_inc(&to_iah(wr->wr.ud.ah)->refcount);
427 wqe->ssn = qp->s_ssn++;
428 qp->s_head = next;
429
430 ret = 0;
431 goto bail;
432
433bail_inval_free:
434 while (j) {
435 struct qib_sge *sge = &wqe->sg_list[--j];
436
437 atomic_dec(&sge->mr->refcount);
438 }
439bail_inval:
440 ret = -EINVAL;
441bail:
442 spin_unlock_irqrestore(&qp->s_lock, flags);
443 return ret;
444}
445
446/**
447 * qib_post_send - post a send on a QP
448 * @ibqp: the QP to post the send on
449 * @wr: the list of work requests to post
450 * @bad_wr: the first bad WR is put here
451 *
452 * This may be called from interrupt context.
453 */
454static int qib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
455 struct ib_send_wr **bad_wr)
456{
457 struct qib_qp *qp = to_iqp(ibqp);
458 int err = 0;
459
460 for (; wr; wr = wr->next) {
461 err = qib_post_one_send(qp, wr);
462 if (err) {
463 *bad_wr = wr;
464 goto bail;
465 }
466 }
467
468 /* Try to do the send work in the caller's context. */
469 qib_do_send(&qp->s_work);
470
471bail:
472 return err;
473}
474
475/**
476 * qib_post_receive - post a receive on a QP
477 * @ibqp: the QP to post the receive on
478 * @wr: the WR to post
479 * @bad_wr: the first bad WR is put here
480 *
481 * This may be called from interrupt context.
482 */
483static int qib_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
484 struct ib_recv_wr **bad_wr)
485{
486 struct qib_qp *qp = to_iqp(ibqp);
487 struct qib_rwq *wq = qp->r_rq.wq;
488 unsigned long flags;
489 int ret;
490
491 /* Check that state is OK to post receive. */
492 if (!(ib_qib_state_ops[qp->state] & QIB_POST_RECV_OK) || !wq) {
493 *bad_wr = wr;
494 ret = -EINVAL;
495 goto bail;
496 }
497
498 for (; wr; wr = wr->next) {
499 struct qib_rwqe *wqe;
500 u32 next;
501 int i;
502
503 if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
504 *bad_wr = wr;
505 ret = -EINVAL;
506 goto bail;
507 }
508
509 spin_lock_irqsave(&qp->r_rq.lock, flags);
510 next = wq->head + 1;
511 if (next >= qp->r_rq.size)
512 next = 0;
513 if (next == wq->tail) {
514 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
515 *bad_wr = wr;
516 ret = -ENOMEM;
517 goto bail;
518 }
519
520 wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
521 wqe->wr_id = wr->wr_id;
522 wqe->num_sge = wr->num_sge;
523 for (i = 0; i < wr->num_sge; i++)
524 wqe->sg_list[i] = wr->sg_list[i];
525 /* Make sure queue entry is written before the head index. */
526 smp_wmb();
527 wq->head = next;
528 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
529 }
530 ret = 0;
531
532bail:
533 return ret;
534}
535
536/**
537 * qib_qp_rcv - processing an incoming packet on a QP
538 * @rcd: the context pointer
539 * @hdr: the packet header
540 * @has_grh: true if the packet has a GRH
541 * @data: the packet data
542 * @tlen: the packet length
543 * @qp: the QP the packet came on
544 *
545 * This is called from qib_ib_rcv() to process an incoming packet
546 * for the given QP.
547 * Called at interrupt level.
548 */
549static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct qib_ib_header *hdr,
550 int has_grh, void *data, u32 tlen, struct qib_qp *qp)
551{
552 struct qib_ibport *ibp = &rcd->ppd->ibport_data;
553
Ralph Campbella5210c12010-08-02 22:39:30 +0000554 spin_lock(&qp->r_lock);
555
Ralph Campbellf9315512010-05-23 21:44:54 -0700556 /* Check for valid receive state. */
557 if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {
558 ibp->n_pkt_drops++;
Ralph Campbella5210c12010-08-02 22:39:30 +0000559 goto unlock;
Ralph Campbellf9315512010-05-23 21:44:54 -0700560 }
561
562 switch (qp->ibqp.qp_type) {
563 case IB_QPT_SMI:
564 case IB_QPT_GSI:
565 if (ib_qib_disable_sma)
566 break;
567 /* FALLTHROUGH */
568 case IB_QPT_UD:
569 qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
570 break;
571
572 case IB_QPT_RC:
573 qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
574 break;
575
576 case IB_QPT_UC:
577 qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
578 break;
579
580 default:
581 break;
582 }
Ralph Campbella5210c12010-08-02 22:39:30 +0000583
584unlock:
585 spin_unlock(&qp->r_lock);
Ralph Campbellf9315512010-05-23 21:44:54 -0700586}
587
588/**
589 * qib_ib_rcv - process an incoming packet
590 * @rcd: the context pointer
591 * @rhdr: the header of the packet
592 * @data: the packet payload
593 * @tlen: the packet length
594 *
595 * This is called from qib_kreceive() to process an incoming packet at
596 * interrupt level. Tlen is the length of the header + data + CRC in bytes.
597 */
598void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
599{
600 struct qib_pportdata *ppd = rcd->ppd;
601 struct qib_ibport *ibp = &ppd->ibport_data;
602 struct qib_ib_header *hdr = rhdr;
603 struct qib_other_headers *ohdr;
604 struct qib_qp *qp;
605 u32 qp_num;
606 int lnh;
607 u8 opcode;
608 u16 lid;
609
610 /* 24 == LRH+BTH+CRC */
611 if (unlikely(tlen < 24))
612 goto drop;
613
614 /* Check for a valid destination LID (see ch. 7.11.1). */
615 lid = be16_to_cpu(hdr->lrh[1]);
616 if (lid < QIB_MULTICAST_LID_BASE) {
617 lid &= ~((1 << ppd->lmc) - 1);
618 if (unlikely(lid != ppd->lid))
619 goto drop;
620 }
621
622 /* Check for GRH */
623 lnh = be16_to_cpu(hdr->lrh[0]) & 3;
624 if (lnh == QIB_LRH_BTH)
625 ohdr = &hdr->u.oth;
626 else if (lnh == QIB_LRH_GRH) {
627 u32 vtf;
628
629 ohdr = &hdr->u.l.oth;
630 if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
631 goto drop;
632 vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
633 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
634 goto drop;
635 } else
636 goto drop;
637
638 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
639 ibp->opstats[opcode & 0x7f].n_bytes += tlen;
640 ibp->opstats[opcode & 0x7f].n_packets++;
641
642 /* Get the destination QP number. */
643 qp_num = be32_to_cpu(ohdr->bth[1]) & QIB_QPN_MASK;
644 if (qp_num == QIB_MULTICAST_QPN) {
645 struct qib_mcast *mcast;
646 struct qib_mcast_qp *p;
647
648 if (lnh != QIB_LRH_GRH)
649 goto drop;
650 mcast = qib_mcast_find(ibp, &hdr->u.l.grh.dgid);
651 if (mcast == NULL)
652 goto drop;
653 ibp->n_multicast_rcv++;
654 list_for_each_entry_rcu(p, &mcast->qp_list, list)
655 qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
656 /*
657 * Notify qib_multicast_detach() if it is waiting for us
658 * to finish.
659 */
660 if (atomic_dec_return(&mcast->refcount) <= 1)
661 wake_up(&mcast->wait);
662 } else {
663 qp = qib_lookup_qpn(ibp, qp_num);
664 if (!qp)
665 goto drop;
666 ibp->n_unicast_rcv++;
667 qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
668 /*
669 * Notify qib_destroy_qp() if it is waiting
670 * for us to finish.
671 */
672 if (atomic_dec_and_test(&qp->refcount))
673 wake_up(&qp->wait);
674 }
675 return;
676
677drop:
678 ibp->n_pkt_drops++;
679}
680
681/*
682 * This is called from a timer to check for QPs
683 * which need kernel memory in order to send a packet.
684 */
685static void mem_timer(unsigned long data)
686{
687 struct qib_ibdev *dev = (struct qib_ibdev *) data;
688 struct list_head *list = &dev->memwait;
689 struct qib_qp *qp = NULL;
690 unsigned long flags;
691
692 spin_lock_irqsave(&dev->pending_lock, flags);
693 if (!list_empty(list)) {
694 qp = list_entry(list->next, struct qib_qp, iowait);
695 list_del_init(&qp->iowait);
696 atomic_inc(&qp->refcount);
697 if (!list_empty(list))
698 mod_timer(&dev->mem_timer, jiffies + 1);
699 }
700 spin_unlock_irqrestore(&dev->pending_lock, flags);
701
702 if (qp) {
703 spin_lock_irqsave(&qp->s_lock, flags);
704 if (qp->s_flags & QIB_S_WAIT_KMEM) {
705 qp->s_flags &= ~QIB_S_WAIT_KMEM;
706 qib_schedule_send(qp);
707 }
708 spin_unlock_irqrestore(&qp->s_lock, flags);
709 if (atomic_dec_and_test(&qp->refcount))
710 wake_up(&qp->wait);
711 }
712}
713
714static void update_sge(struct qib_sge_state *ss, u32 length)
715{
716 struct qib_sge *sge = &ss->sge;
717
718 sge->vaddr += length;
719 sge->length -= length;
720 sge->sge_length -= length;
721 if (sge->sge_length == 0) {
722 if (--ss->num_sge)
723 *sge = *ss->sg_list++;
724 } else if (sge->length == 0 && sge->mr->lkey) {
725 if (++sge->n >= QIB_SEGSZ) {
726 if (++sge->m >= sge->mr->mapsz)
727 return;
728 sge->n = 0;
729 }
730 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
731 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
732 }
733}
734
735#ifdef __LITTLE_ENDIAN
736static inline u32 get_upper_bits(u32 data, u32 shift)
737{
738 return data >> shift;
739}
740
741static inline u32 set_upper_bits(u32 data, u32 shift)
742{
743 return data << shift;
744}
745
746static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
747{
748 data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
749 data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
750 return data;
751}
752#else
753static inline u32 get_upper_bits(u32 data, u32 shift)
754{
755 return data << shift;
756}
757
758static inline u32 set_upper_bits(u32 data, u32 shift)
759{
760 return data >> shift;
761}
762
763static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
764{
765 data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
766 data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
767 return data;
768}
769#endif
770
771static void copy_io(u32 __iomem *piobuf, struct qib_sge_state *ss,
772 u32 length, unsigned flush_wc)
773{
774 u32 extra = 0;
775 u32 data = 0;
776 u32 last;
777
778 while (1) {
779 u32 len = ss->sge.length;
780 u32 off;
781
782 if (len > length)
783 len = length;
784 if (len > ss->sge.sge_length)
785 len = ss->sge.sge_length;
786 BUG_ON(len == 0);
787 /* If the source address is not aligned, try to align it. */
788 off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
789 if (off) {
790 u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
791 ~(sizeof(u32) - 1));
792 u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
793 u32 y;
794
795 y = sizeof(u32) - off;
796 if (len > y)
797 len = y;
798 if (len + extra >= sizeof(u32)) {
799 data |= set_upper_bits(v, extra *
800 BITS_PER_BYTE);
801 len = sizeof(u32) - extra;
802 if (len == length) {
803 last = data;
804 break;
805 }
806 __raw_writel(data, piobuf);
807 piobuf++;
808 extra = 0;
809 data = 0;
810 } else {
811 /* Clear unused upper bytes */
812 data |= clear_upper_bytes(v, len, extra);
813 if (len == length) {
814 last = data;
815 break;
816 }
817 extra += len;
818 }
819 } else if (extra) {
820 /* Source address is aligned. */
821 u32 *addr = (u32 *) ss->sge.vaddr;
822 int shift = extra * BITS_PER_BYTE;
823 int ushift = 32 - shift;
824 u32 l = len;
825
826 while (l >= sizeof(u32)) {
827 u32 v = *addr;
828
829 data |= set_upper_bits(v, shift);
830 __raw_writel(data, piobuf);
831 data = get_upper_bits(v, ushift);
832 piobuf++;
833 addr++;
834 l -= sizeof(u32);
835 }
836 /*
837 * We still have 'extra' number of bytes leftover.
838 */
839 if (l) {
840 u32 v = *addr;
841
842 if (l + extra >= sizeof(u32)) {
843 data |= set_upper_bits(v, shift);
844 len -= l + extra - sizeof(u32);
845 if (len == length) {
846 last = data;
847 break;
848 }
849 __raw_writel(data, piobuf);
850 piobuf++;
851 extra = 0;
852 data = 0;
853 } else {
854 /* Clear unused upper bytes */
855 data |= clear_upper_bytes(v, l, extra);
856 if (len == length) {
857 last = data;
858 break;
859 }
860 extra += l;
861 }
862 } else if (len == length) {
863 last = data;
864 break;
865 }
866 } else if (len == length) {
867 u32 w;
868
869 /*
870 * Need to round up for the last dword in the
871 * packet.
872 */
873 w = (len + 3) >> 2;
874 qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
875 piobuf += w - 1;
876 last = ((u32 *) ss->sge.vaddr)[w - 1];
877 break;
878 } else {
879 u32 w = len >> 2;
880
881 qib_pio_copy(piobuf, ss->sge.vaddr, w);
882 piobuf += w;
883
884 extra = len & (sizeof(u32) - 1);
885 if (extra) {
886 u32 v = ((u32 *) ss->sge.vaddr)[w];
887
888 /* Clear unused upper bytes */
889 data = clear_upper_bytes(v, extra, 0);
890 }
891 }
892 update_sge(ss, len);
893 length -= len;
894 }
895 /* Update address before sending packet. */
896 update_sge(ss, length);
897 if (flush_wc) {
898 /* must flush early everything before trigger word */
899 qib_flush_wc();
900 __raw_writel(last, piobuf);
901 /* be sure trigger word is written */
902 qib_flush_wc();
903 } else
904 __raw_writel(last, piobuf);
905}
906
907static struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
908 struct qib_qp *qp, int *retp)
909{
910 struct qib_verbs_txreq *tx;
911 unsigned long flags;
912
913 spin_lock_irqsave(&qp->s_lock, flags);
914 spin_lock(&dev->pending_lock);
915
916 if (!list_empty(&dev->txreq_free)) {
917 struct list_head *l = dev->txreq_free.next;
918
919 list_del(l);
920 tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
921 *retp = 0;
922 } else {
923 if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK &&
924 list_empty(&qp->iowait)) {
925 dev->n_txwait++;
926 qp->s_flags |= QIB_S_WAIT_TX;
927 list_add_tail(&qp->iowait, &dev->txwait);
928 }
929 tx = NULL;
930 qp->s_flags &= ~QIB_S_BUSY;
931 *retp = -EBUSY;
932 }
933
934 spin_unlock(&dev->pending_lock);
935 spin_unlock_irqrestore(&qp->s_lock, flags);
936
937 return tx;
938}
939
940void qib_put_txreq(struct qib_verbs_txreq *tx)
941{
942 struct qib_ibdev *dev;
943 struct qib_qp *qp;
944 unsigned long flags;
945
946 qp = tx->qp;
947 dev = to_idev(qp->ibqp.device);
948
949 if (atomic_dec_and_test(&qp->refcount))
950 wake_up(&qp->wait);
951 if (tx->mr) {
952 atomic_dec(&tx->mr->refcount);
953 tx->mr = NULL;
954 }
955 if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
956 tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
957 dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
958 tx->txreq.addr, tx->hdr_dwords << 2,
959 DMA_TO_DEVICE);
960 kfree(tx->align_buf);
961 }
962
963 spin_lock_irqsave(&dev->pending_lock, flags);
964
965 /* Put struct back on free list */
966 list_add(&tx->txreq.list, &dev->txreq_free);
967
968 if (!list_empty(&dev->txwait)) {
969 /* Wake up first QP wanting a free struct */
970 qp = list_entry(dev->txwait.next, struct qib_qp, iowait);
971 list_del_init(&qp->iowait);
972 atomic_inc(&qp->refcount);
973 spin_unlock_irqrestore(&dev->pending_lock, flags);
974
975 spin_lock_irqsave(&qp->s_lock, flags);
976 if (qp->s_flags & QIB_S_WAIT_TX) {
977 qp->s_flags &= ~QIB_S_WAIT_TX;
978 qib_schedule_send(qp);
979 }
980 spin_unlock_irqrestore(&qp->s_lock, flags);
981
982 if (atomic_dec_and_test(&qp->refcount))
983 wake_up(&qp->wait);
984 } else
985 spin_unlock_irqrestore(&dev->pending_lock, flags);
986}
987
988/*
989 * This is called when there are send DMA descriptors that might be
990 * available.
991 *
992 * This is called with ppd->sdma_lock held.
993 */
994void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
995{
996 struct qib_qp *qp, *nqp;
997 struct qib_qp *qps[20];
998 struct qib_ibdev *dev;
999 unsigned i, n;
1000
1001 n = 0;
1002 dev = &ppd->dd->verbs_dev;
1003 spin_lock(&dev->pending_lock);
1004
1005 /* Search wait list for first QP wanting DMA descriptors. */
1006 list_for_each_entry_safe(qp, nqp, &dev->dmawait, iowait) {
1007 if (qp->port_num != ppd->port)
1008 continue;
1009 if (n == ARRAY_SIZE(qps))
1010 break;
1011 if (qp->s_tx->txreq.sg_count > avail)
1012 break;
1013 avail -= qp->s_tx->txreq.sg_count;
1014 list_del_init(&qp->iowait);
1015 atomic_inc(&qp->refcount);
1016 qps[n++] = qp;
1017 }
1018
1019 spin_unlock(&dev->pending_lock);
1020
1021 for (i = 0; i < n; i++) {
1022 qp = qps[i];
1023 spin_lock(&qp->s_lock);
1024 if (qp->s_flags & QIB_S_WAIT_DMA_DESC) {
1025 qp->s_flags &= ~QIB_S_WAIT_DMA_DESC;
1026 qib_schedule_send(qp);
1027 }
1028 spin_unlock(&qp->s_lock);
1029 if (atomic_dec_and_test(&qp->refcount))
1030 wake_up(&qp->wait);
1031 }
1032}
1033
1034/*
1035 * This is called with ppd->sdma_lock held.
1036 */
1037static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
1038{
1039 struct qib_verbs_txreq *tx =
1040 container_of(cookie, struct qib_verbs_txreq, txreq);
1041 struct qib_qp *qp = tx->qp;
1042
1043 spin_lock(&qp->s_lock);
1044 if (tx->wqe)
1045 qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
1046 else if (qp->ibqp.qp_type == IB_QPT_RC) {
1047 struct qib_ib_header *hdr;
1048
1049 if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
1050 hdr = &tx->align_buf->hdr;
1051 else {
1052 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1053
1054 hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
1055 }
1056 qib_rc_send_complete(qp, hdr);
1057 }
1058 if (atomic_dec_and_test(&qp->s_dma_busy)) {
1059 if (qp->state == IB_QPS_RESET)
1060 wake_up(&qp->wait_dma);
1061 else if (qp->s_flags & QIB_S_WAIT_DMA) {
1062 qp->s_flags &= ~QIB_S_WAIT_DMA;
1063 qib_schedule_send(qp);
1064 }
1065 }
1066 spin_unlock(&qp->s_lock);
1067
1068 qib_put_txreq(tx);
1069}
1070
1071static int wait_kmem(struct qib_ibdev *dev, struct qib_qp *qp)
1072{
1073 unsigned long flags;
1074 int ret = 0;
1075
1076 spin_lock_irqsave(&qp->s_lock, flags);
1077 if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
1078 spin_lock(&dev->pending_lock);
1079 if (list_empty(&qp->iowait)) {
1080 if (list_empty(&dev->memwait))
1081 mod_timer(&dev->mem_timer, jiffies + 1);
1082 qp->s_flags |= QIB_S_WAIT_KMEM;
1083 list_add_tail(&qp->iowait, &dev->memwait);
1084 }
1085 spin_unlock(&dev->pending_lock);
1086 qp->s_flags &= ~QIB_S_BUSY;
1087 ret = -EBUSY;
1088 }
1089 spin_unlock_irqrestore(&qp->s_lock, flags);
1090
1091 return ret;
1092}
1093
1094static int qib_verbs_send_dma(struct qib_qp *qp, struct qib_ib_header *hdr,
1095 u32 hdrwords, struct qib_sge_state *ss, u32 len,
1096 u32 plen, u32 dwords)
1097{
1098 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1099 struct qib_devdata *dd = dd_from_dev(dev);
1100 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
1101 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1102 struct qib_verbs_txreq *tx;
1103 struct qib_pio_header *phdr;
1104 u32 control;
1105 u32 ndesc;
1106 int ret;
1107
1108 tx = qp->s_tx;
1109 if (tx) {
1110 qp->s_tx = NULL;
1111 /* resend previously constructed packet */
1112 ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
1113 goto bail;
1114 }
1115
1116 tx = get_txreq(dev, qp, &ret);
1117 if (!tx)
1118 goto bail;
1119
1120 control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
1121 be16_to_cpu(hdr->lrh[0]) >> 12);
1122 tx->qp = qp;
1123 atomic_inc(&qp->refcount);
1124 tx->wqe = qp->s_wqe;
1125 tx->mr = qp->s_rdma_mr;
1126 if (qp->s_rdma_mr)
1127 qp->s_rdma_mr = NULL;
1128 tx->txreq.callback = sdma_complete;
1129 if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
1130 tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
1131 else
1132 tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
1133 if (plen + 1 > dd->piosize2kmax_dwords)
1134 tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
1135
1136 if (len) {
1137 /*
1138 * Don't try to DMA if it takes more descriptors than
1139 * the queue holds.
1140 */
1141 ndesc = qib_count_sge(ss, len);
1142 if (ndesc >= ppd->sdma_descq_cnt)
1143 ndesc = 0;
1144 } else
1145 ndesc = 1;
1146 if (ndesc) {
1147 phdr = &dev->pio_hdrs[tx->hdr_inx];
1148 phdr->pbc[0] = cpu_to_le32(plen);
1149 phdr->pbc[1] = cpu_to_le32(control);
1150 memcpy(&phdr->hdr, hdr, hdrwords << 2);
1151 tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
1152 tx->txreq.sg_count = ndesc;
1153 tx->txreq.addr = dev->pio_hdrs_phys +
1154 tx->hdr_inx * sizeof(struct qib_pio_header);
1155 tx->hdr_dwords = hdrwords + 2; /* add PBC length */
1156 ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
1157 goto bail;
1158 }
1159
1160 /* Allocate a buffer and copy the header and payload to it. */
1161 tx->hdr_dwords = plen + 1;
1162 phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
1163 if (!phdr)
1164 goto err_tx;
1165 phdr->pbc[0] = cpu_to_le32(plen);
1166 phdr->pbc[1] = cpu_to_le32(control);
1167 memcpy(&phdr->hdr, hdr, hdrwords << 2);
1168 qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
1169
1170 tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
1171 tx->hdr_dwords << 2, DMA_TO_DEVICE);
1172 if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
1173 goto map_err;
1174 tx->align_buf = phdr;
1175 tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
1176 tx->txreq.sg_count = 1;
1177 ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
1178 goto unaligned;
1179
1180map_err:
1181 kfree(phdr);
1182err_tx:
1183 qib_put_txreq(tx);
1184 ret = wait_kmem(dev, qp);
1185unaligned:
1186 ibp->n_unaligned++;
1187bail:
1188 return ret;
1189}
1190
1191/*
1192 * If we are now in the error state, return zero to flush the
1193 * send work request.
1194 */
1195static int no_bufs_available(struct qib_qp *qp)
1196{
1197 struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1198 struct qib_devdata *dd;
1199 unsigned long flags;
1200 int ret = 0;
1201
1202 /*
1203 * Note that as soon as want_buffer() is called and
1204 * possibly before it returns, qib_ib_piobufavail()
1205 * could be called. Therefore, put QP on the I/O wait list before
1206 * enabling the PIO avail interrupt.
1207 */
1208 spin_lock_irqsave(&qp->s_lock, flags);
1209 if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
1210 spin_lock(&dev->pending_lock);
1211 if (list_empty(&qp->iowait)) {
1212 dev->n_piowait++;
1213 qp->s_flags |= QIB_S_WAIT_PIO;
1214 list_add_tail(&qp->iowait, &dev->piowait);
1215 dd = dd_from_dev(dev);
1216 dd->f_wantpiobuf_intr(dd, 1);
1217 }
1218 spin_unlock(&dev->pending_lock);
1219 qp->s_flags &= ~QIB_S_BUSY;
1220 ret = -EBUSY;
1221 }
1222 spin_unlock_irqrestore(&qp->s_lock, flags);
1223 return ret;
1224}
1225
1226static int qib_verbs_send_pio(struct qib_qp *qp, struct qib_ib_header *ibhdr,
1227 u32 hdrwords, struct qib_sge_state *ss, u32 len,
1228 u32 plen, u32 dwords)
1229{
1230 struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1231 struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
1232 u32 *hdr = (u32 *) ibhdr;
1233 u32 __iomem *piobuf_orig;
1234 u32 __iomem *piobuf;
1235 u64 pbc;
1236 unsigned long flags;
1237 unsigned flush_wc;
1238 u32 control;
1239 u32 pbufn;
1240
1241 control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
1242 be16_to_cpu(ibhdr->lrh[0]) >> 12);
1243 pbc = ((u64) control << 32) | plen;
1244 piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
1245 if (unlikely(piobuf == NULL))
1246 return no_bufs_available(qp);
1247
1248 /*
1249 * Write the pbc.
1250 * We have to flush after the PBC for correctness on some cpus
1251 * or WC buffer can be written out of order.
1252 */
1253 writeq(pbc, piobuf);
1254 piobuf_orig = piobuf;
1255 piobuf += 2;
1256
1257 flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
1258 if (len == 0) {
1259 /*
1260 * If there is just the header portion, must flush before
1261 * writing last word of header for correctness, and after
1262 * the last header word (trigger word).
1263 */
1264 if (flush_wc) {
1265 qib_flush_wc();
1266 qib_pio_copy(piobuf, hdr, hdrwords - 1);
1267 qib_flush_wc();
1268 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
1269 qib_flush_wc();
1270 } else
1271 qib_pio_copy(piobuf, hdr, hdrwords);
1272 goto done;
1273 }
1274
1275 if (flush_wc)
1276 qib_flush_wc();
1277 qib_pio_copy(piobuf, hdr, hdrwords);
1278 piobuf += hdrwords;
1279
1280 /* The common case is aligned and contained in one segment. */
1281 if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
1282 !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
1283 u32 *addr = (u32 *) ss->sge.vaddr;
1284
1285 /* Update address before sending packet. */
1286 update_sge(ss, len);
1287 if (flush_wc) {
1288 qib_pio_copy(piobuf, addr, dwords - 1);
1289 /* must flush early everything before trigger word */
1290 qib_flush_wc();
1291 __raw_writel(addr[dwords - 1], piobuf + dwords - 1);
1292 /* be sure trigger word is written */
1293 qib_flush_wc();
1294 } else
1295 qib_pio_copy(piobuf, addr, dwords);
1296 goto done;
1297 }
1298 copy_io(piobuf, ss, len, flush_wc);
1299done:
1300 if (dd->flags & QIB_USE_SPCL_TRIG) {
1301 u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
1302 qib_flush_wc();
1303 __raw_writel(0xaebecede, piobuf_orig + spcl_off);
1304 }
1305 qib_sendbuf_done(dd, pbufn);
1306 if (qp->s_rdma_mr) {
1307 atomic_dec(&qp->s_rdma_mr->refcount);
1308 qp->s_rdma_mr = NULL;
1309 }
1310 if (qp->s_wqe) {
1311 spin_lock_irqsave(&qp->s_lock, flags);
1312 qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
1313 spin_unlock_irqrestore(&qp->s_lock, flags);
1314 } else if (qp->ibqp.qp_type == IB_QPT_RC) {
1315 spin_lock_irqsave(&qp->s_lock, flags);
1316 qib_rc_send_complete(qp, ibhdr);
1317 spin_unlock_irqrestore(&qp->s_lock, flags);
1318 }
1319 return 0;
1320}
1321
1322/**
1323 * qib_verbs_send - send a packet
1324 * @qp: the QP to send on
1325 * @hdr: the packet header
1326 * @hdrwords: the number of 32-bit words in the header
1327 * @ss: the SGE to send
1328 * @len: the length of the packet in bytes
1329 *
1330 * Return zero if packet is sent or queued OK.
1331 * Return non-zero and clear qp->s_flags QIB_S_BUSY otherwise.
1332 */
1333int qib_verbs_send(struct qib_qp *qp, struct qib_ib_header *hdr,
1334 u32 hdrwords, struct qib_sge_state *ss, u32 len)
1335{
1336 struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1337 u32 plen;
1338 int ret;
1339 u32 dwords = (len + 3) >> 2;
1340
1341 /*
1342 * Calculate the send buffer trigger address.
1343 * The +1 counts for the pbc control dword following the pbc length.
1344 */
1345 plen = hdrwords + dwords + 1;
1346
1347 /*
1348 * VL15 packets (IB_QPT_SMI) will always use PIO, so we
1349 * can defer SDMA restart until link goes ACTIVE without
1350 * worrying about just how we got there.
1351 */
1352 if (qp->ibqp.qp_type == IB_QPT_SMI ||
1353 !(dd->flags & QIB_HAS_SEND_DMA))
1354 ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
1355 plen, dwords);
1356 else
1357 ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
1358 plen, dwords);
1359
1360 return ret;
1361}
1362
1363int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
1364 u64 *rwords, u64 *spkts, u64 *rpkts,
1365 u64 *xmit_wait)
1366{
1367 int ret;
1368 struct qib_devdata *dd = ppd->dd;
1369
1370 if (!(dd->flags & QIB_PRESENT)) {
1371 /* no hardware, freeze, etc. */
1372 ret = -EINVAL;
1373 goto bail;
1374 }
1375 *swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
1376 *rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
1377 *spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
1378 *rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
1379 *xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
1380
1381 ret = 0;
1382
1383bail:
1384 return ret;
1385}
1386
1387/**
1388 * qib_get_counters - get various chip counters
1389 * @dd: the qlogic_ib device
1390 * @cntrs: counters are placed here
1391 *
1392 * Return the counters needed by recv_pma_get_portcounters().
1393 */
1394int qib_get_counters(struct qib_pportdata *ppd,
1395 struct qib_verbs_counters *cntrs)
1396{
1397 int ret;
1398
1399 if (!(ppd->dd->flags & QIB_PRESENT)) {
1400 /* no hardware, freeze, etc. */
1401 ret = -EINVAL;
1402 goto bail;
1403 }
1404 cntrs->symbol_error_counter =
1405 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
1406 cntrs->link_error_recovery_counter =
1407 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
1408 /*
1409 * The link downed counter counts when the other side downs the
1410 * connection. We add in the number of times we downed the link
1411 * due to local link integrity errors to compensate.
1412 */
1413 cntrs->link_downed_counter =
1414 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
1415 cntrs->port_rcv_errors =
1416 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
1417 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
1418 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
1419 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
1420 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
1421 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
1422 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
1423 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
1424 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
1425 cntrs->port_rcv_errors +=
1426 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
1427 cntrs->port_rcv_errors +=
1428 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
1429 cntrs->port_rcv_remphys_errors =
1430 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
1431 cntrs->port_xmit_discards =
1432 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
1433 cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
1434 QIBPORTCNTR_WORDSEND);
1435 cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
1436 QIBPORTCNTR_WORDRCV);
1437 cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
1438 QIBPORTCNTR_PKTSEND);
1439 cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
1440 QIBPORTCNTR_PKTRCV);
1441 cntrs->local_link_integrity_errors =
1442 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
1443 cntrs->excessive_buffer_overrun_errors =
1444 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
1445 cntrs->vl15_dropped =
1446 ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
1447
1448 ret = 0;
1449
1450bail:
1451 return ret;
1452}
1453
1454/**
1455 * qib_ib_piobufavail - callback when a PIO buffer is available
1456 * @dd: the device pointer
1457 *
1458 * This is called from qib_intr() at interrupt level when a PIO buffer is
1459 * available after qib_verbs_send() returned an error that no buffers were
1460 * available. Disable the interrupt if there are no more QPs waiting.
1461 */
1462void qib_ib_piobufavail(struct qib_devdata *dd)
1463{
1464 struct qib_ibdev *dev = &dd->verbs_dev;
1465 struct list_head *list;
1466 struct qib_qp *qps[5];
1467 struct qib_qp *qp;
1468 unsigned long flags;
1469 unsigned i, n;
1470
1471 list = &dev->piowait;
1472 n = 0;
1473
1474 /*
1475 * Note: checking that the piowait list is empty and clearing
1476 * the buffer available interrupt needs to be atomic or we
1477 * could end up with QPs on the wait list with the interrupt
1478 * disabled.
1479 */
1480 spin_lock_irqsave(&dev->pending_lock, flags);
1481 while (!list_empty(list)) {
1482 if (n == ARRAY_SIZE(qps))
1483 goto full;
1484 qp = list_entry(list->next, struct qib_qp, iowait);
1485 list_del_init(&qp->iowait);
1486 atomic_inc(&qp->refcount);
1487 qps[n++] = qp;
1488 }
1489 dd->f_wantpiobuf_intr(dd, 0);
1490full:
1491 spin_unlock_irqrestore(&dev->pending_lock, flags);
1492
1493 for (i = 0; i < n; i++) {
1494 qp = qps[i];
1495
1496 spin_lock_irqsave(&qp->s_lock, flags);
1497 if (qp->s_flags & QIB_S_WAIT_PIO) {
1498 qp->s_flags &= ~QIB_S_WAIT_PIO;
1499 qib_schedule_send(qp);
1500 }
1501 spin_unlock_irqrestore(&qp->s_lock, flags);
1502
1503 /* Notify qib_destroy_qp() if it is waiting. */
1504 if (atomic_dec_and_test(&qp->refcount))
1505 wake_up(&qp->wait);
1506 }
1507}
1508
1509static int qib_query_device(struct ib_device *ibdev,
1510 struct ib_device_attr *props)
1511{
1512 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1513 struct qib_ibdev *dev = to_idev(ibdev);
1514
1515 memset(props, 0, sizeof(*props));
1516
1517 props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
1518 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
1519 IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
1520 IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
1521 props->page_size_cap = PAGE_SIZE;
1522 props->vendor_id =
1523 QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
1524 props->vendor_part_id = dd->deviceid;
1525 props->hw_ver = dd->minrev;
1526 props->sys_image_guid = ib_qib_sys_image_guid;
1527 props->max_mr_size = ~0ULL;
1528 props->max_qp = ib_qib_max_qps;
1529 props->max_qp_wr = ib_qib_max_qp_wrs;
1530 props->max_sge = ib_qib_max_sges;
1531 props->max_cq = ib_qib_max_cqs;
1532 props->max_ah = ib_qib_max_ahs;
1533 props->max_cqe = ib_qib_max_cqes;
1534 props->max_mr = dev->lk_table.max;
1535 props->max_fmr = dev->lk_table.max;
1536 props->max_map_per_fmr = 32767;
1537 props->max_pd = ib_qib_max_pds;
1538 props->max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
1539 props->max_qp_init_rd_atom = 255;
1540 /* props->max_res_rd_atom */
1541 props->max_srq = ib_qib_max_srqs;
1542 props->max_srq_wr = ib_qib_max_srq_wrs;
1543 props->max_srq_sge = ib_qib_max_srq_sges;
1544 /* props->local_ca_ack_delay */
1545 props->atomic_cap = IB_ATOMIC_GLOB;
1546 props->max_pkeys = qib_get_npkeys(dd);
1547 props->max_mcast_grp = ib_qib_max_mcast_grps;
1548 props->max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
1549 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1550 props->max_mcast_grp;
1551
1552 return 0;
1553}
1554
1555static int qib_query_port(struct ib_device *ibdev, u8 port,
1556 struct ib_port_attr *props)
1557{
1558 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1559 struct qib_ibport *ibp = to_iport(ibdev, port);
1560 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1561 enum ib_mtu mtu;
1562 u16 lid = ppd->lid;
1563
1564 memset(props, 0, sizeof(*props));
1565 props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
1566 props->lmc = ppd->lmc;
1567 props->sm_lid = ibp->sm_lid;
1568 props->sm_sl = ibp->sm_sl;
1569 props->state = dd->f_iblink_state(ppd->lastibcstat);
1570 props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
1571 props->port_cap_flags = ibp->port_cap_flags;
1572 props->gid_tbl_len = QIB_GUIDS_PER_PORT;
1573 props->max_msg_sz = 0x80000000;
1574 props->pkey_tbl_len = qib_get_npkeys(dd);
1575 props->bad_pkey_cntr = ibp->pkey_violations;
1576 props->qkey_viol_cntr = ibp->qkey_violations;
1577 props->active_width = ppd->link_width_active;
1578 /* See rate_show() */
1579 props->active_speed = ppd->link_speed_active;
1580 props->max_vl_num = qib_num_vls(ppd->vls_supported);
1581 props->init_type_reply = 0;
1582
1583 props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
1584 switch (ppd->ibmtu) {
1585 case 4096:
1586 mtu = IB_MTU_4096;
1587 break;
1588 case 2048:
1589 mtu = IB_MTU_2048;
1590 break;
1591 case 1024:
1592 mtu = IB_MTU_1024;
1593 break;
1594 case 512:
1595 mtu = IB_MTU_512;
1596 break;
1597 case 256:
1598 mtu = IB_MTU_256;
1599 break;
1600 default:
1601 mtu = IB_MTU_2048;
1602 }
1603 props->active_mtu = mtu;
1604 props->subnet_timeout = ibp->subnet_timeout;
1605
1606 return 0;
1607}
1608
1609static int qib_modify_device(struct ib_device *device,
1610 int device_modify_mask,
1611 struct ib_device_modify *device_modify)
1612{
1613 struct qib_devdata *dd = dd_from_ibdev(device);
1614 unsigned i;
1615 int ret;
1616
1617 if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1618 IB_DEVICE_MODIFY_NODE_DESC)) {
1619 ret = -EOPNOTSUPP;
1620 goto bail;
1621 }
1622
1623 if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
1624 memcpy(device->node_desc, device_modify->node_desc, 64);
1625 for (i = 0; i < dd->num_pports; i++) {
1626 struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1627
1628 qib_node_desc_chg(ibp);
1629 }
1630 }
1631
1632 if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
1633 ib_qib_sys_image_guid =
1634 cpu_to_be64(device_modify->sys_image_guid);
1635 for (i = 0; i < dd->num_pports; i++) {
1636 struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1637
1638 qib_sys_guid_chg(ibp);
1639 }
1640 }
1641
1642 ret = 0;
1643
1644bail:
1645 return ret;
1646}
1647
1648static int qib_modify_port(struct ib_device *ibdev, u8 port,
1649 int port_modify_mask, struct ib_port_modify *props)
1650{
1651 struct qib_ibport *ibp = to_iport(ibdev, port);
1652 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1653
1654 ibp->port_cap_flags |= props->set_port_cap_mask;
1655 ibp->port_cap_flags &= ~props->clr_port_cap_mask;
1656 if (props->set_port_cap_mask || props->clr_port_cap_mask)
1657 qib_cap_mask_chg(ibp);
1658 if (port_modify_mask & IB_PORT_SHUTDOWN)
1659 qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
1660 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1661 ibp->qkey_violations = 0;
1662 return 0;
1663}
1664
1665static int qib_query_gid(struct ib_device *ibdev, u8 port,
1666 int index, union ib_gid *gid)
1667{
1668 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1669 int ret = 0;
1670
1671 if (!port || port > dd->num_pports)
1672 ret = -EINVAL;
1673 else {
1674 struct qib_ibport *ibp = to_iport(ibdev, port);
1675 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1676
1677 gid->global.subnet_prefix = ibp->gid_prefix;
1678 if (index == 0)
1679 gid->global.interface_id = ppd->guid;
1680 else if (index < QIB_GUIDS_PER_PORT)
1681 gid->global.interface_id = ibp->guids[index - 1];
1682 else
1683 ret = -EINVAL;
1684 }
1685
1686 return ret;
1687}
1688
1689static struct ib_pd *qib_alloc_pd(struct ib_device *ibdev,
1690 struct ib_ucontext *context,
1691 struct ib_udata *udata)
1692{
1693 struct qib_ibdev *dev = to_idev(ibdev);
1694 struct qib_pd *pd;
1695 struct ib_pd *ret;
1696
1697 /*
1698 * This is actually totally arbitrary. Some correctness tests
1699 * assume there's a maximum number of PDs that can be allocated.
1700 * We don't actually have this limit, but we fail the test if
1701 * we allow allocations of more than we report for this value.
1702 */
1703
1704 pd = kmalloc(sizeof *pd, GFP_KERNEL);
1705 if (!pd) {
1706 ret = ERR_PTR(-ENOMEM);
1707 goto bail;
1708 }
1709
1710 spin_lock(&dev->n_pds_lock);
1711 if (dev->n_pds_allocated == ib_qib_max_pds) {
1712 spin_unlock(&dev->n_pds_lock);
1713 kfree(pd);
1714 ret = ERR_PTR(-ENOMEM);
1715 goto bail;
1716 }
1717
1718 dev->n_pds_allocated++;
1719 spin_unlock(&dev->n_pds_lock);
1720
1721 /* ib_alloc_pd() will initialize pd->ibpd. */
1722 pd->user = udata != NULL;
1723
1724 ret = &pd->ibpd;
1725
1726bail:
1727 return ret;
1728}
1729
1730static int qib_dealloc_pd(struct ib_pd *ibpd)
1731{
1732 struct qib_pd *pd = to_ipd(ibpd);
1733 struct qib_ibdev *dev = to_idev(ibpd->device);
1734
1735 spin_lock(&dev->n_pds_lock);
1736 dev->n_pds_allocated--;
1737 spin_unlock(&dev->n_pds_lock);
1738
1739 kfree(pd);
1740
1741 return 0;
1742}
1743
1744int qib_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr)
1745{
1746 /* A multicast address requires a GRH (see ch. 8.4.1). */
1747 if (ah_attr->dlid >= QIB_MULTICAST_LID_BASE &&
1748 ah_attr->dlid != QIB_PERMISSIVE_LID &&
1749 !(ah_attr->ah_flags & IB_AH_GRH))
1750 goto bail;
1751 if ((ah_attr->ah_flags & IB_AH_GRH) &&
1752 ah_attr->grh.sgid_index >= QIB_GUIDS_PER_PORT)
1753 goto bail;
1754 if (ah_attr->dlid == 0)
1755 goto bail;
1756 if (ah_attr->port_num < 1 ||
1757 ah_attr->port_num > ibdev->phys_port_cnt)
1758 goto bail;
1759 if (ah_attr->static_rate != IB_RATE_PORT_CURRENT &&
1760 ib_rate_to_mult(ah_attr->static_rate) < 0)
1761 goto bail;
1762 if (ah_attr->sl > 15)
1763 goto bail;
1764 return 0;
1765bail:
1766 return -EINVAL;
1767}
1768
1769/**
1770 * qib_create_ah - create an address handle
1771 * @pd: the protection domain
1772 * @ah_attr: the attributes of the AH
1773 *
1774 * This may be called from interrupt context.
1775 */
1776static struct ib_ah *qib_create_ah(struct ib_pd *pd,
1777 struct ib_ah_attr *ah_attr)
1778{
1779 struct qib_ah *ah;
1780 struct ib_ah *ret;
1781 struct qib_ibdev *dev = to_idev(pd->device);
1782 unsigned long flags;
1783
1784 if (qib_check_ah(pd->device, ah_attr)) {
1785 ret = ERR_PTR(-EINVAL);
1786 goto bail;
1787 }
1788
1789 ah = kmalloc(sizeof *ah, GFP_ATOMIC);
1790 if (!ah) {
1791 ret = ERR_PTR(-ENOMEM);
1792 goto bail;
1793 }
1794
1795 spin_lock_irqsave(&dev->n_ahs_lock, flags);
1796 if (dev->n_ahs_allocated == ib_qib_max_ahs) {
1797 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1798 kfree(ah);
1799 ret = ERR_PTR(-ENOMEM);
1800 goto bail;
1801 }
1802
1803 dev->n_ahs_allocated++;
1804 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1805
1806 /* ib_create_ah() will initialize ah->ibah. */
1807 ah->attr = *ah_attr;
1808 atomic_set(&ah->refcount, 0);
1809
1810 ret = &ah->ibah;
1811
1812bail:
1813 return ret;
1814}
1815
1816/**
1817 * qib_destroy_ah - destroy an address handle
1818 * @ibah: the AH to destroy
1819 *
1820 * This may be called from interrupt context.
1821 */
1822static int qib_destroy_ah(struct ib_ah *ibah)
1823{
1824 struct qib_ibdev *dev = to_idev(ibah->device);
1825 struct qib_ah *ah = to_iah(ibah);
1826 unsigned long flags;
1827
1828 if (atomic_read(&ah->refcount) != 0)
1829 return -EBUSY;
1830
1831 spin_lock_irqsave(&dev->n_ahs_lock, flags);
1832 dev->n_ahs_allocated--;
1833 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1834
1835 kfree(ah);
1836
1837 return 0;
1838}
1839
1840static int qib_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1841{
1842 struct qib_ah *ah = to_iah(ibah);
1843
1844 if (qib_check_ah(ibah->device, ah_attr))
1845 return -EINVAL;
1846
1847 ah->attr = *ah_attr;
1848
1849 return 0;
1850}
1851
1852static int qib_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1853{
1854 struct qib_ah *ah = to_iah(ibah);
1855
1856 *ah_attr = ah->attr;
1857
1858 return 0;
1859}
1860
1861/**
1862 * qib_get_npkeys - return the size of the PKEY table for context 0
1863 * @dd: the qlogic_ib device
1864 */
1865unsigned qib_get_npkeys(struct qib_devdata *dd)
1866{
1867 return ARRAY_SIZE(dd->rcd[0]->pkeys);
1868}
1869
1870/*
1871 * Return the indexed PKEY from the port PKEY table.
1872 * No need to validate rcd[ctxt]; the port is setup if we are here.
1873 */
1874unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
1875{
1876 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1877 struct qib_devdata *dd = ppd->dd;
1878 unsigned ctxt = ppd->hw_pidx;
1879 unsigned ret;
1880
1881 /* dd->rcd null if mini_init or some init failures */
1882 if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
1883 ret = 0;
1884 else
1885 ret = dd->rcd[ctxt]->pkeys[index];
1886
1887 return ret;
1888}
1889
1890static int qib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1891 u16 *pkey)
1892{
1893 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1894 int ret;
1895
1896 if (index >= qib_get_npkeys(dd)) {
1897 ret = -EINVAL;
1898 goto bail;
1899 }
1900
1901 *pkey = qib_get_pkey(to_iport(ibdev, port), index);
1902 ret = 0;
1903
1904bail:
1905 return ret;
1906}
1907
1908/**
1909 * qib_alloc_ucontext - allocate a ucontest
1910 * @ibdev: the infiniband device
1911 * @udata: not used by the QLogic_IB driver
1912 */
1913
1914static struct ib_ucontext *qib_alloc_ucontext(struct ib_device *ibdev,
1915 struct ib_udata *udata)
1916{
1917 struct qib_ucontext *context;
1918 struct ib_ucontext *ret;
1919
1920 context = kmalloc(sizeof *context, GFP_KERNEL);
1921 if (!context) {
1922 ret = ERR_PTR(-ENOMEM);
1923 goto bail;
1924 }
1925
1926 ret = &context->ibucontext;
1927
1928bail:
1929 return ret;
1930}
1931
1932static int qib_dealloc_ucontext(struct ib_ucontext *context)
1933{
1934 kfree(to_iucontext(context));
1935 return 0;
1936}
1937
1938static void init_ibport(struct qib_pportdata *ppd)
1939{
1940 struct qib_verbs_counters cntrs;
1941 struct qib_ibport *ibp = &ppd->ibport_data;
1942
1943 spin_lock_init(&ibp->lock);
1944 /* Set the prefix to the default value (see ch. 4.1.1) */
1945 ibp->gid_prefix = IB_DEFAULT_GID_PREFIX;
1946 ibp->sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
1947 ibp->port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
1948 IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
1949 IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
1950 IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
1951 IB_PORT_OTHER_LOCAL_CHANGES_SUP;
1952 if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
1953 ibp->port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
1954 ibp->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1955 ibp->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1956 ibp->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1957 ibp->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1958 ibp->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
1959
1960 /* Snapshot current HW counters to "clear" them. */
1961 qib_get_counters(ppd, &cntrs);
1962 ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1963 ibp->z_link_error_recovery_counter =
1964 cntrs.link_error_recovery_counter;
1965 ibp->z_link_downed_counter = cntrs.link_downed_counter;
1966 ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1967 ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
1968 ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1969 ibp->z_port_xmit_data = cntrs.port_xmit_data;
1970 ibp->z_port_rcv_data = cntrs.port_rcv_data;
1971 ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1972 ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1973 ibp->z_local_link_integrity_errors =
1974 cntrs.local_link_integrity_errors;
1975 ibp->z_excessive_buffer_overrun_errors =
1976 cntrs.excessive_buffer_overrun_errors;
1977 ibp->z_vl15_dropped = cntrs.vl15_dropped;
1978}
1979
1980/**
1981 * qib_register_ib_device - register our device with the infiniband core
1982 * @dd: the device data structure
1983 * Return the allocated qib_ibdev pointer or NULL on error.
1984 */
1985int qib_register_ib_device(struct qib_devdata *dd)
1986{
1987 struct qib_ibdev *dev = &dd->verbs_dev;
1988 struct ib_device *ibdev = &dev->ibdev;
1989 struct qib_pportdata *ppd = dd->pport;
1990 unsigned i, lk_tab_size;
1991 int ret;
1992
1993 dev->qp_table_size = ib_qib_qp_table_size;
1994 dev->qp_table = kzalloc(dev->qp_table_size * sizeof *dev->qp_table,
1995 GFP_KERNEL);
1996 if (!dev->qp_table) {
1997 ret = -ENOMEM;
1998 goto err_qpt;
1999 }
2000
2001 for (i = 0; i < dd->num_pports; i++)
2002 init_ibport(ppd + i);
2003
2004 /* Only need to initialize non-zero fields. */
2005 spin_lock_init(&dev->qpt_lock);
2006 spin_lock_init(&dev->n_pds_lock);
2007 spin_lock_init(&dev->n_ahs_lock);
2008 spin_lock_init(&dev->n_cqs_lock);
2009 spin_lock_init(&dev->n_qps_lock);
2010 spin_lock_init(&dev->n_srqs_lock);
2011 spin_lock_init(&dev->n_mcast_grps_lock);
2012 init_timer(&dev->mem_timer);
2013 dev->mem_timer.function = mem_timer;
2014 dev->mem_timer.data = (unsigned long) dev;
2015
2016 qib_init_qpn_table(dd, &dev->qpn_table);
2017
2018 /*
2019 * The top ib_qib_lkey_table_size bits are used to index the
2020 * table. The lower 8 bits can be owned by the user (copied from
2021 * the LKEY). The remaining bits act as a generation number or tag.
2022 */
2023 spin_lock_init(&dev->lk_table.lock);
2024 dev->lk_table.max = 1 << ib_qib_lkey_table_size;
2025 lk_tab_size = dev->lk_table.max * sizeof(*dev->lk_table.table);
2026 dev->lk_table.table = (struct qib_mregion **)
2027 __get_free_pages(GFP_KERNEL, get_order(lk_tab_size));
2028 if (dev->lk_table.table == NULL) {
2029 ret = -ENOMEM;
2030 goto err_lk;
2031 }
2032 memset(dev->lk_table.table, 0, lk_tab_size);
2033 INIT_LIST_HEAD(&dev->pending_mmaps);
2034 spin_lock_init(&dev->pending_lock);
2035 dev->mmap_offset = PAGE_SIZE;
2036 spin_lock_init(&dev->mmap_offset_lock);
2037 INIT_LIST_HEAD(&dev->piowait);
2038 INIT_LIST_HEAD(&dev->dmawait);
2039 INIT_LIST_HEAD(&dev->txwait);
2040 INIT_LIST_HEAD(&dev->memwait);
2041 INIT_LIST_HEAD(&dev->txreq_free);
2042
2043 if (ppd->sdma_descq_cnt) {
2044 dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
2045 ppd->sdma_descq_cnt *
2046 sizeof(struct qib_pio_header),
2047 &dev->pio_hdrs_phys,
2048 GFP_KERNEL);
2049 if (!dev->pio_hdrs) {
2050 ret = -ENOMEM;
2051 goto err_hdrs;
2052 }
2053 }
2054
2055 for (i = 0; i < ppd->sdma_descq_cnt; i++) {
2056 struct qib_verbs_txreq *tx;
2057
2058 tx = kzalloc(sizeof *tx, GFP_KERNEL);
2059 if (!tx) {
2060 ret = -ENOMEM;
2061 goto err_tx;
2062 }
2063 tx->hdr_inx = i;
2064 list_add(&tx->txreq.list, &dev->txreq_free);
2065 }
2066
2067 /*
2068 * The system image GUID is supposed to be the same for all
2069 * IB HCAs in a single system but since there can be other
2070 * device types in the system, we can't be sure this is unique.
2071 */
2072 if (!ib_qib_sys_image_guid)
2073 ib_qib_sys_image_guid = ppd->guid;
2074
2075 strlcpy(ibdev->name, "qib%d", IB_DEVICE_NAME_MAX);
2076 ibdev->owner = THIS_MODULE;
2077 ibdev->node_guid = ppd->guid;
2078 ibdev->uverbs_abi_ver = QIB_UVERBS_ABI_VERSION;
2079 ibdev->uverbs_cmd_mask =
2080 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
2081 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
2082 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
2083 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
2084 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
2085 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
2086 (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
2087 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
2088 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
2089 (1ull << IB_USER_VERBS_CMD_REG_MR) |
2090 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
2091 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
2092 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
2093 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
2094 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
2095 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
2096 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
2097 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
2098 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
2099 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
2100 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
2101 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
2102 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
2103 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
2104 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
2105 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
2106 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
2107 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
2108 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
2109 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
2110 ibdev->node_type = RDMA_NODE_IB_CA;
2111 ibdev->phys_port_cnt = dd->num_pports;
2112 ibdev->num_comp_vectors = 1;
2113 ibdev->dma_device = &dd->pcidev->dev;
2114 ibdev->query_device = qib_query_device;
2115 ibdev->modify_device = qib_modify_device;
2116 ibdev->query_port = qib_query_port;
2117 ibdev->modify_port = qib_modify_port;
2118 ibdev->query_pkey = qib_query_pkey;
2119 ibdev->query_gid = qib_query_gid;
2120 ibdev->alloc_ucontext = qib_alloc_ucontext;
2121 ibdev->dealloc_ucontext = qib_dealloc_ucontext;
2122 ibdev->alloc_pd = qib_alloc_pd;
2123 ibdev->dealloc_pd = qib_dealloc_pd;
2124 ibdev->create_ah = qib_create_ah;
2125 ibdev->destroy_ah = qib_destroy_ah;
2126 ibdev->modify_ah = qib_modify_ah;
2127 ibdev->query_ah = qib_query_ah;
2128 ibdev->create_srq = qib_create_srq;
2129 ibdev->modify_srq = qib_modify_srq;
2130 ibdev->query_srq = qib_query_srq;
2131 ibdev->destroy_srq = qib_destroy_srq;
2132 ibdev->create_qp = qib_create_qp;
2133 ibdev->modify_qp = qib_modify_qp;
2134 ibdev->query_qp = qib_query_qp;
2135 ibdev->destroy_qp = qib_destroy_qp;
2136 ibdev->post_send = qib_post_send;
2137 ibdev->post_recv = qib_post_receive;
2138 ibdev->post_srq_recv = qib_post_srq_receive;
2139 ibdev->create_cq = qib_create_cq;
2140 ibdev->destroy_cq = qib_destroy_cq;
2141 ibdev->resize_cq = qib_resize_cq;
2142 ibdev->poll_cq = qib_poll_cq;
2143 ibdev->req_notify_cq = qib_req_notify_cq;
2144 ibdev->get_dma_mr = qib_get_dma_mr;
2145 ibdev->reg_phys_mr = qib_reg_phys_mr;
2146 ibdev->reg_user_mr = qib_reg_user_mr;
2147 ibdev->dereg_mr = qib_dereg_mr;
2148 ibdev->alloc_fast_reg_mr = qib_alloc_fast_reg_mr;
2149 ibdev->alloc_fast_reg_page_list = qib_alloc_fast_reg_page_list;
2150 ibdev->free_fast_reg_page_list = qib_free_fast_reg_page_list;
2151 ibdev->alloc_fmr = qib_alloc_fmr;
2152 ibdev->map_phys_fmr = qib_map_phys_fmr;
2153 ibdev->unmap_fmr = qib_unmap_fmr;
2154 ibdev->dealloc_fmr = qib_dealloc_fmr;
2155 ibdev->attach_mcast = qib_multicast_attach;
2156 ibdev->detach_mcast = qib_multicast_detach;
2157 ibdev->process_mad = qib_process_mad;
2158 ibdev->mmap = qib_mmap;
2159 ibdev->dma_ops = &qib_dma_mapping_ops;
2160
2161 snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
2162 QIB_IDSTR " %s", init_utsname()->nodename);
2163
2164 ret = ib_register_device(ibdev, qib_create_port_files);
2165 if (ret)
2166 goto err_reg;
2167
2168 ret = qib_create_agents(dev);
2169 if (ret)
2170 goto err_agents;
2171
2172 if (qib_verbs_register_sysfs(dd))
2173 goto err_class;
2174
2175 goto bail;
2176
2177err_class:
2178 qib_free_agents(dev);
2179err_agents:
2180 ib_unregister_device(ibdev);
2181err_reg:
2182err_tx:
2183 while (!list_empty(&dev->txreq_free)) {
2184 struct list_head *l = dev->txreq_free.next;
2185 struct qib_verbs_txreq *tx;
2186
2187 list_del(l);
2188 tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
2189 kfree(tx);
2190 }
2191 if (ppd->sdma_descq_cnt)
2192 dma_free_coherent(&dd->pcidev->dev,
2193 ppd->sdma_descq_cnt *
2194 sizeof(struct qib_pio_header),
2195 dev->pio_hdrs, dev->pio_hdrs_phys);
2196err_hdrs:
2197 free_pages((unsigned long) dev->lk_table.table, get_order(lk_tab_size));
2198err_lk:
2199 kfree(dev->qp_table);
2200err_qpt:
2201 qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
2202bail:
2203 return ret;
2204}
2205
2206void qib_unregister_ib_device(struct qib_devdata *dd)
2207{
2208 struct qib_ibdev *dev = &dd->verbs_dev;
2209 struct ib_device *ibdev = &dev->ibdev;
2210 u32 qps_inuse;
2211 unsigned lk_tab_size;
2212
2213 qib_verbs_unregister_sysfs(dd);
2214
2215 qib_free_agents(dev);
2216
2217 ib_unregister_device(ibdev);
2218
2219 if (!list_empty(&dev->piowait))
2220 qib_dev_err(dd, "piowait list not empty!\n");
2221 if (!list_empty(&dev->dmawait))
2222 qib_dev_err(dd, "dmawait list not empty!\n");
2223 if (!list_empty(&dev->txwait))
2224 qib_dev_err(dd, "txwait list not empty!\n");
2225 if (!list_empty(&dev->memwait))
2226 qib_dev_err(dd, "memwait list not empty!\n");
2227 if (dev->dma_mr)
2228 qib_dev_err(dd, "DMA MR not NULL!\n");
2229
2230 qps_inuse = qib_free_all_qps(dd);
2231 if (qps_inuse)
2232 qib_dev_err(dd, "QP memory leak! %u still in use\n",
2233 qps_inuse);
2234
2235 del_timer_sync(&dev->mem_timer);
2236 qib_free_qpn_table(&dev->qpn_table);
2237 while (!list_empty(&dev->txreq_free)) {
2238 struct list_head *l = dev->txreq_free.next;
2239 struct qib_verbs_txreq *tx;
2240
2241 list_del(l);
2242 tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
2243 kfree(tx);
2244 }
2245 if (dd->pport->sdma_descq_cnt)
2246 dma_free_coherent(&dd->pcidev->dev,
2247 dd->pport->sdma_descq_cnt *
2248 sizeof(struct qib_pio_header),
2249 dev->pio_hdrs, dev->pio_hdrs_phys);
2250 lk_tab_size = dev->lk_table.max * sizeof(*dev->lk_table.table);
2251 free_pages((unsigned long) dev->lk_table.table,
2252 get_order(lk_tab_size));
2253 kfree(dev->qp_table);
2254}