blob: b9be0fd2ed361f4c9ddb5867a1af19a9d003474e [file] [log] [blame]
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivan65221082006-03-29 15:23:38 -08003 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <rdma/ib_mad.h>
35#include <rdma/ib_user_verbs.h>
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -070036#include <linux/io.h>
Bryan O'Sullivan65221082006-03-29 15:23:38 -080037#include <linux/utsname.h>
38
39#include "ipath_kernel.h"
40#include "ipath_verbs.h"
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -070041#include "ipath_common.h"
Bryan O'Sullivan65221082006-03-29 15:23:38 -080042
Roland Dreierac2ae4c2006-04-19 11:40:12 -070043static unsigned int ib_ipath_qp_table_size = 251;
Bryan O'Sullivan65221082006-03-29 15:23:38 -080044module_param_named(qp_table_size, ib_ipath_qp_table_size, uint, S_IRUGO);
45MODULE_PARM_DESC(qp_table_size, "QP table size");
46
47unsigned int ib_ipath_lkey_table_size = 12;
48module_param_named(lkey_table_size, ib_ipath_lkey_table_size, uint,
49 S_IRUGO);
50MODULE_PARM_DESC(lkey_table_size,
51 "LKEY table size in bits (2^n, 1 <= n <= 23)");
52
Bryan O'Sullivanfe625462006-07-01 04:35:58 -070053static unsigned int ib_ipath_max_pds = 0xFFFF;
54module_param_named(max_pds, ib_ipath_max_pds, uint, S_IWUSR | S_IRUGO);
55MODULE_PARM_DESC(max_pds,
56 "Maximum number of protection domains to support");
57
58static unsigned int ib_ipath_max_ahs = 0xFFFF;
59module_param_named(max_ahs, ib_ipath_max_ahs, uint, S_IWUSR | S_IRUGO);
60MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
61
62unsigned int ib_ipath_max_cqes = 0x2FFFF;
63module_param_named(max_cqes, ib_ipath_max_cqes, uint, S_IWUSR | S_IRUGO);
64MODULE_PARM_DESC(max_cqes,
65 "Maximum number of completion queue entries to support");
66
67unsigned int ib_ipath_max_cqs = 0x1FFFF;
68module_param_named(max_cqs, ib_ipath_max_cqs, uint, S_IWUSR | S_IRUGO);
69MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
70
71unsigned int ib_ipath_max_qp_wrs = 0x3FFF;
72module_param_named(max_qp_wrs, ib_ipath_max_qp_wrs, uint,
73 S_IWUSR | S_IRUGO);
74MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
75
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -070076unsigned int ib_ipath_max_qps = 16384;
77module_param_named(max_qps, ib_ipath_max_qps, uint, S_IWUSR | S_IRUGO);
78MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
79
Bryan O'Sullivanfe625462006-07-01 04:35:58 -070080unsigned int ib_ipath_max_sges = 0x60;
81module_param_named(max_sges, ib_ipath_max_sges, uint, S_IWUSR | S_IRUGO);
82MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
83
84unsigned int ib_ipath_max_mcast_grps = 16384;
85module_param_named(max_mcast_grps, ib_ipath_max_mcast_grps, uint,
86 S_IWUSR | S_IRUGO);
87MODULE_PARM_DESC(max_mcast_grps,
88 "Maximum number of multicast groups to support");
89
90unsigned int ib_ipath_max_mcast_qp_attached = 16;
91module_param_named(max_mcast_qp_attached, ib_ipath_max_mcast_qp_attached,
92 uint, S_IWUSR | S_IRUGO);
93MODULE_PARM_DESC(max_mcast_qp_attached,
94 "Maximum number of attached QPs to support");
95
96unsigned int ib_ipath_max_srqs = 1024;
97module_param_named(max_srqs, ib_ipath_max_srqs, uint, S_IWUSR | S_IRUGO);
98MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
99
100unsigned int ib_ipath_max_srq_sges = 128;
101module_param_named(max_srq_sges, ib_ipath_max_srq_sges,
102 uint, S_IWUSR | S_IRUGO);
103MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
104
105unsigned int ib_ipath_max_srq_wrs = 0x1FFFF;
106module_param_named(max_srq_wrs, ib_ipath_max_srq_wrs,
107 uint, S_IWUSR | S_IRUGO);
108MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
109
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800110const int ib_ipath_state_ops[IB_QPS_ERR + 1] = {
111 [IB_QPS_RESET] = 0,
112 [IB_QPS_INIT] = IPATH_POST_RECV_OK,
113 [IB_QPS_RTR] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
114 [IB_QPS_RTS] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
115 IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK,
116 [IB_QPS_SQD] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
117 IPATH_POST_SEND_OK,
118 [IB_QPS_SQE] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
119 [IB_QPS_ERR] = 0,
120};
121
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700122struct ipath_ucontext {
123 struct ib_ucontext ibucontext;
124};
125
126static inline struct ipath_ucontext *to_iucontext(struct ib_ucontext
127 *ibucontext)
128{
129 return container_of(ibucontext, struct ipath_ucontext, ibucontext);
130}
131
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800132/*
133 * Translate ib_wr_opcode into ib_wc_opcode.
134 */
135const enum ib_wc_opcode ib_ipath_wc_opcode[] = {
136 [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
137 [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
138 [IB_WR_SEND] = IB_WC_SEND,
139 [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
140 [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
141 [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
142 [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
143};
144
145/*
146 * System image GUID.
147 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700148static __be64 sys_image_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800149
150/**
151 * ipath_copy_sge - copy data to SGE memory
152 * @ss: the SGE state
153 * @data: the data to copy
154 * @length: the length of the data
155 */
156void ipath_copy_sge(struct ipath_sge_state *ss, void *data, u32 length)
157{
158 struct ipath_sge *sge = &ss->sge;
159
160 while (length) {
161 u32 len = sge->length;
162
163 BUG_ON(len == 0);
164 if (len > length)
165 len = length;
166 memcpy(sge->vaddr, data, len);
167 sge->vaddr += len;
168 sge->length -= len;
169 sge->sge_length -= len;
170 if (sge->sge_length == 0) {
171 if (--ss->num_sge)
172 *sge = *ss->sg_list++;
173 } else if (sge->length == 0 && sge->mr != NULL) {
174 if (++sge->n >= IPATH_SEGSZ) {
175 if (++sge->m >= sge->mr->mapsz)
176 break;
177 sge->n = 0;
178 }
179 sge->vaddr =
180 sge->mr->map[sge->m]->segs[sge->n].vaddr;
181 sge->length =
182 sge->mr->map[sge->m]->segs[sge->n].length;
183 }
184 data += len;
185 length -= len;
186 }
187}
188
189/**
190 * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
191 * @ss: the SGE state
192 * @length: the number of bytes to skip
193 */
194void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
195{
196 struct ipath_sge *sge = &ss->sge;
197
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800198 while (length) {
199 u32 len = sge->length;
200
201 BUG_ON(len == 0);
202 if (len > length)
203 len = length;
204 sge->vaddr += len;
205 sge->length -= len;
206 sge->sge_length -= len;
207 if (sge->sge_length == 0) {
208 if (--ss->num_sge)
209 *sge = *ss->sg_list++;
210 } else if (sge->length == 0 && sge->mr != NULL) {
211 if (++sge->n >= IPATH_SEGSZ) {
212 if (++sge->m >= sge->mr->mapsz)
213 break;
214 sge->n = 0;
215 }
216 sge->vaddr =
217 sge->mr->map[sge->m]->segs[sge->n].vaddr;
218 sge->length =
219 sge->mr->map[sge->m]->segs[sge->n].length;
220 }
221 length -= len;
222 }
223}
224
225/**
226 * ipath_post_send - post a send on a QP
227 * @ibqp: the QP to post the send on
228 * @wr: the list of work requests to post
229 * @bad_wr: the first bad WR is put here
230 *
231 * This may be called from interrupt context.
232 */
233static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
234 struct ib_send_wr **bad_wr)
235{
236 struct ipath_qp *qp = to_iqp(ibqp);
237 int err = 0;
238
239 /* Check that state is OK to post send. */
240 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK)) {
241 *bad_wr = wr;
242 err = -EINVAL;
243 goto bail;
244 }
245
246 for (; wr; wr = wr->next) {
247 switch (qp->ibqp.qp_type) {
248 case IB_QPT_UC:
249 case IB_QPT_RC:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700250 err = ipath_post_ruc_send(qp, wr);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800251 break;
252
253 case IB_QPT_SMI:
254 case IB_QPT_GSI:
255 case IB_QPT_UD:
256 err = ipath_post_ud_send(qp, wr);
257 break;
258
259 default:
260 err = -EINVAL;
261 }
262 if (err) {
263 *bad_wr = wr;
264 break;
265 }
266 }
267
268bail:
269 return err;
270}
271
272/**
273 * ipath_post_receive - post a receive on a QP
274 * @ibqp: the QP to post the receive on
275 * @wr: the WR to post
276 * @bad_wr: the first bad WR is put here
277 *
278 * This may be called from interrupt context.
279 */
280static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
281 struct ib_recv_wr **bad_wr)
282{
283 struct ipath_qp *qp = to_iqp(ibqp);
Ralph Campbell373d9912006-09-22 15:22:26 -0700284 struct ipath_rwq *wq = qp->r_rq.wq;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800285 unsigned long flags;
286 int ret;
287
288 /* Check that state is OK to post receive. */
Ralph Campbell373d9912006-09-22 15:22:26 -0700289 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK) || !wq) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800290 *bad_wr = wr;
291 ret = -EINVAL;
292 goto bail;
293 }
294
295 for (; wr; wr = wr->next) {
296 struct ipath_rwqe *wqe;
297 u32 next;
Ralph Campbell373d9912006-09-22 15:22:26 -0700298 int i;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800299
Ralph Campbell373d9912006-09-22 15:22:26 -0700300 if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800301 *bad_wr = wr;
302 ret = -ENOMEM;
303 goto bail;
304 }
305
306 spin_lock_irqsave(&qp->r_rq.lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700307 next = wq->head + 1;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800308 if (next >= qp->r_rq.size)
309 next = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700310 if (next == wq->tail) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800311 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
312 *bad_wr = wr;
313 ret = -ENOMEM;
314 goto bail;
315 }
316
Ralph Campbell373d9912006-09-22 15:22:26 -0700317 wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800318 wqe->wr_id = wr->wr_id;
Ralph Campbell373d9912006-09-22 15:22:26 -0700319 wqe->num_sge = wr->num_sge;
320 for (i = 0; i < wr->num_sge; i++)
321 wqe->sg_list[i] = wr->sg_list[i];
322 wq->head = next;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800323 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
324 }
325 ret = 0;
326
327bail:
328 return ret;
329}
330
331/**
332 * ipath_qp_rcv - processing an incoming packet on a QP
333 * @dev: the device the packet came on
334 * @hdr: the packet header
335 * @has_grh: true if the packet has a GRH
336 * @data: the packet data
337 * @tlen: the packet length
338 * @qp: the QP the packet came on
339 *
340 * This is called from ipath_ib_rcv() to process an incoming packet
341 * for the given QP.
342 * Called at interrupt level.
343 */
344static void ipath_qp_rcv(struct ipath_ibdev *dev,
345 struct ipath_ib_header *hdr, int has_grh,
346 void *data, u32 tlen, struct ipath_qp *qp)
347{
348 /* Check for valid receive state. */
349 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
350 dev->n_pkt_drops++;
351 return;
352 }
353
354 switch (qp->ibqp.qp_type) {
355 case IB_QPT_SMI:
356 case IB_QPT_GSI:
357 case IB_QPT_UD:
358 ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
359 break;
360
361 case IB_QPT_RC:
362 ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
363 break;
364
365 case IB_QPT_UC:
366 ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
367 break;
368
369 default:
370 break;
371 }
372}
373
374/**
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700375 * ipath_ib_rcv - process an incoming packet
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800376 * @arg: the device pointer
377 * @rhdr: the header of the packet
378 * @data: the packet data
379 * @tlen: the packet length
380 *
381 * This is called from ipath_kreceive() to process an incoming packet at
382 * interrupt level. Tlen is the length of the header + data + CRC in bytes.
383 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700384void ipath_ib_rcv(struct ipath_ibdev *dev, void *rhdr, void *data,
385 u32 tlen)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800386{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800387 struct ipath_ib_header *hdr = rhdr;
388 struct ipath_other_headers *ohdr;
389 struct ipath_qp *qp;
390 u32 qp_num;
391 int lnh;
392 u8 opcode;
393 u16 lid;
394
395 if (unlikely(dev == NULL))
396 goto bail;
397
398 if (unlikely(tlen < 24)) { /* LRH+BTH+CRC */
399 dev->rcv_errors++;
400 goto bail;
401 }
402
403 /* Check for a valid destination LID (see ch. 7.11.1). */
404 lid = be16_to_cpu(hdr->lrh[1]);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700405 if (lid < IPATH_MULTICAST_LID_BASE) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800406 lid &= ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700407 if (unlikely(lid != dev->dd->ipath_lid)) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800408 dev->rcv_errors++;
409 goto bail;
410 }
411 }
412
413 /* Check for GRH */
414 lnh = be16_to_cpu(hdr->lrh[0]) & 3;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700415 if (lnh == IPATH_LRH_BTH)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800416 ohdr = &hdr->u.oth;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700417 else if (lnh == IPATH_LRH_GRH)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800418 ohdr = &hdr->u.l.oth;
419 else {
420 dev->rcv_errors++;
421 goto bail;
422 }
423
424 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
425 dev->opstats[opcode].n_bytes += tlen;
426 dev->opstats[opcode].n_packets++;
427
428 /* Get the destination QP number. */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700429 qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
430 if (qp_num == IPATH_MULTICAST_QPN) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800431 struct ipath_mcast *mcast;
432 struct ipath_mcast_qp *p;
433
434 mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
435 if (mcast == NULL) {
436 dev->n_pkt_drops++;
437 goto bail;
438 }
439 dev->n_multicast_rcv++;
440 list_for_each_entry_rcu(p, &mcast->qp_list, list)
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700441 ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800442 tlen, p->qp);
443 /*
444 * Notify ipath_multicast_detach() if it is waiting for us
445 * to finish.
446 */
447 if (atomic_dec_return(&mcast->refcount) <= 1)
448 wake_up(&mcast->wait);
449 } else {
450 qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
451 if (qp) {
452 dev->n_unicast_rcv++;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700453 ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800454 tlen, qp);
455 /*
456 * Notify ipath_destroy_qp() if it is waiting
457 * for us to finish.
458 */
459 if (atomic_dec_and_test(&qp->refcount))
460 wake_up(&qp->wait);
461 } else
462 dev->n_pkt_drops++;
463 }
464
465bail:;
466}
467
468/**
469 * ipath_ib_timer - verbs timer
470 * @arg: the device pointer
471 *
472 * This is called from ipath_do_rcv_timer() at interrupt level to check for
473 * QPs which need retransmits and to collect performance numbers.
474 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700475void ipath_ib_timer(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800476{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800477 struct ipath_qp *resend = NULL;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800478 struct list_head *last;
479 struct ipath_qp *qp;
480 unsigned long flags;
481
482 if (dev == NULL)
483 return;
484
485 spin_lock_irqsave(&dev->pending_lock, flags);
486 /* Start filling the next pending queue. */
487 if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
488 dev->pending_index = 0;
489 /* Save any requests still in the new queue, they have timed out. */
490 last = &dev->pending[dev->pending_index];
491 while (!list_empty(last)) {
492 qp = list_entry(last->next, struct ipath_qp, timerwait);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700493 list_del_init(&qp->timerwait);
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700494 qp->timer_next = resend;
495 resend = qp;
496 atomic_inc(&qp->refcount);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800497 }
498 last = &dev->rnrwait;
499 if (!list_empty(last)) {
500 qp = list_entry(last->next, struct ipath_qp, timerwait);
501 if (--qp->s_rnr_timeout == 0) {
502 do {
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700503 list_del_init(&qp->timerwait);
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700504 tasklet_hi_schedule(&qp->s_task);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800505 if (list_empty(last))
506 break;
507 qp = list_entry(last->next, struct ipath_qp,
508 timerwait);
509 } while (qp->s_rnr_timeout == 0);
510 }
511 }
512 /*
513 * We should only be in the started state if pma_sample_start != 0
514 */
515 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
516 --dev->pma_sample_start == 0) {
517 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700518 ipath_snapshot_counters(dev->dd, &dev->ipath_sword,
519 &dev->ipath_rword,
520 &dev->ipath_spkts,
521 &dev->ipath_rpkts,
522 &dev->ipath_xmit_wait);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800523 }
524 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
525 if (dev->pma_sample_interval == 0) {
526 u64 ta, tb, tc, td, te;
527
528 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700529 ipath_snapshot_counters(dev->dd, &ta, &tb,
530 &tc, &td, &te);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800531
532 dev->ipath_sword = ta - dev->ipath_sword;
533 dev->ipath_rword = tb - dev->ipath_rword;
534 dev->ipath_spkts = tc - dev->ipath_spkts;
535 dev->ipath_rpkts = td - dev->ipath_rpkts;
536 dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
537 }
538 else
539 dev->pma_sample_interval--;
540 }
541 spin_unlock_irqrestore(&dev->pending_lock, flags);
542
543 /* XXX What if timer fires again while this is running? */
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700544 for (qp = resend; qp != NULL; qp = qp->timer_next) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800545 struct ib_wc wc;
546
547 spin_lock_irqsave(&qp->s_lock, flags);
548 if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
549 dev->n_timeouts++;
550 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
551 }
552 spin_unlock_irqrestore(&qp->s_lock, flags);
553
554 /* Notify ipath_destroy_qp() if it is waiting. */
555 if (atomic_dec_and_test(&qp->refcount))
556 wake_up(&qp->wait);
557 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800558}
559
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700560static void update_sge(struct ipath_sge_state *ss, u32 length)
561{
562 struct ipath_sge *sge = &ss->sge;
563
564 sge->vaddr += length;
565 sge->length -= length;
566 sge->sge_length -= length;
567 if (sge->sge_length == 0) {
568 if (--ss->num_sge)
569 *sge = *ss->sg_list++;
570 } else if (sge->length == 0 && sge->mr != NULL) {
571 if (++sge->n >= IPATH_SEGSZ) {
572 if (++sge->m >= sge->mr->mapsz)
573 return;
574 sge->n = 0;
575 }
576 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
577 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
578 }
579}
580
581#ifdef __LITTLE_ENDIAN
582static inline u32 get_upper_bits(u32 data, u32 shift)
583{
584 return data >> shift;
585}
586
587static inline u32 set_upper_bits(u32 data, u32 shift)
588{
589 return data << shift;
590}
591
592static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
593{
594 data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
595 data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
596 return data;
597}
598#else
599static inline u32 get_upper_bits(u32 data, u32 shift)
600{
601 return data << shift;
602}
603
604static inline u32 set_upper_bits(u32 data, u32 shift)
605{
606 return data >> shift;
607}
608
609static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
610{
611 data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
612 data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
613 return data;
614}
615#endif
616
617static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
618 u32 length)
619{
620 u32 extra = 0;
621 u32 data = 0;
622 u32 last;
623
624 while (1) {
625 u32 len = ss->sge.length;
626 u32 off;
627
628 BUG_ON(len == 0);
629 if (len > length)
630 len = length;
631 if (len > ss->sge.sge_length)
632 len = ss->sge.sge_length;
633 /* If the source address is not aligned, try to align it. */
634 off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
635 if (off) {
636 u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
637 ~(sizeof(u32) - 1));
638 u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
639 u32 y;
640
641 y = sizeof(u32) - off;
642 if (len > y)
643 len = y;
644 if (len + extra >= sizeof(u32)) {
645 data |= set_upper_bits(v, extra *
646 BITS_PER_BYTE);
647 len = sizeof(u32) - extra;
648 if (len == length) {
649 last = data;
650 break;
651 }
652 __raw_writel(data, piobuf);
653 piobuf++;
654 extra = 0;
655 data = 0;
656 } else {
657 /* Clear unused upper bytes */
658 data |= clear_upper_bytes(v, len, extra);
659 if (len == length) {
660 last = data;
661 break;
662 }
663 extra += len;
664 }
665 } else if (extra) {
666 /* Source address is aligned. */
667 u32 *addr = (u32 *) ss->sge.vaddr;
668 int shift = extra * BITS_PER_BYTE;
669 int ushift = 32 - shift;
670 u32 l = len;
671
672 while (l >= sizeof(u32)) {
673 u32 v = *addr;
674
675 data |= set_upper_bits(v, shift);
676 __raw_writel(data, piobuf);
677 data = get_upper_bits(v, ushift);
678 piobuf++;
679 addr++;
680 l -= sizeof(u32);
681 }
682 /*
683 * We still have 'extra' number of bytes leftover.
684 */
685 if (l) {
686 u32 v = *addr;
687
688 if (l + extra >= sizeof(u32)) {
689 data |= set_upper_bits(v, shift);
690 len -= l + extra - sizeof(u32);
691 if (len == length) {
692 last = data;
693 break;
694 }
695 __raw_writel(data, piobuf);
696 piobuf++;
697 extra = 0;
698 data = 0;
699 } else {
700 /* Clear unused upper bytes */
701 data |= clear_upper_bytes(v, l,
702 extra);
703 if (len == length) {
704 last = data;
705 break;
706 }
707 extra += l;
708 }
709 } else if (len == length) {
710 last = data;
711 break;
712 }
713 } else if (len == length) {
714 u32 w;
715
716 /*
717 * Need to round up for the last dword in the
718 * packet.
719 */
720 w = (len + 3) >> 2;
721 __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
722 piobuf += w - 1;
723 last = ((u32 *) ss->sge.vaddr)[w - 1];
724 break;
725 } else {
726 u32 w = len >> 2;
727
728 __iowrite32_copy(piobuf, ss->sge.vaddr, w);
729 piobuf += w;
730
731 extra = len & (sizeof(u32) - 1);
732 if (extra) {
733 u32 v = ((u32 *) ss->sge.vaddr)[w];
734
735 /* Clear unused upper bytes */
736 data = clear_upper_bytes(v, extra, 0);
737 }
738 }
739 update_sge(ss, len);
740 length -= len;
741 }
742 /* Update address before sending packet. */
743 update_sge(ss, length);
744 /* must flush early everything before trigger word */
745 ipath_flush_wc();
746 __raw_writel(last, piobuf);
747 /* be sure trigger word is written */
748 ipath_flush_wc();
749}
750
751/**
752 * ipath_verbs_send - send a packet
753 * @dd: the infinipath device
754 * @hdrwords: the number of words in the header
755 * @hdr: the packet header
756 * @len: the length of the packet in bytes
757 * @ss: the SGE to send
758 */
759int ipath_verbs_send(struct ipath_devdata *dd, u32 hdrwords,
760 u32 *hdr, u32 len, struct ipath_sge_state *ss)
761{
762 u32 __iomem *piobuf;
763 u32 plen;
764 int ret;
765
766 /* +1 is for the qword padding of pbc */
767 plen = hdrwords + ((len + 3) >> 2) + 1;
768 if (unlikely((plen << 2) > dd->ipath_ibmaxlen)) {
769 ipath_dbg("packet len 0x%x too long, failing\n", plen);
770 ret = -EINVAL;
771 goto bail;
772 }
773
774 /* Get a PIO buffer to use. */
775 piobuf = ipath_getpiobuf(dd, NULL);
776 if (unlikely(piobuf == NULL)) {
777 ret = -EBUSY;
778 goto bail;
779 }
780
781 /*
782 * Write len to control qword, no flags.
783 * We have to flush after the PBC for correctness on some cpus
784 * or WC buffer can be written out of order.
785 */
786 writeq(plen, piobuf);
787 ipath_flush_wc();
788 piobuf += 2;
789 if (len == 0) {
790 /*
791 * If there is just the header portion, must flush before
792 * writing last word of header for correctness, and after
793 * the last header word (trigger word).
794 */
795 __iowrite32_copy(piobuf, hdr, hdrwords - 1);
796 ipath_flush_wc();
797 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
798 ipath_flush_wc();
799 ret = 0;
800 goto bail;
801 }
802
803 __iowrite32_copy(piobuf, hdr, hdrwords);
804 piobuf += hdrwords;
805
806 /* The common case is aligned and contained in one segment. */
807 if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
808 !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
809 u32 w;
810 u32 *addr = (u32 *) ss->sge.vaddr;
811
812 /* Update address before sending packet. */
813 update_sge(ss, len);
814 /* Need to round up for the last dword in the packet. */
815 w = (len + 3) >> 2;
816 __iowrite32_copy(piobuf, addr, w - 1);
817 /* must flush early everything before trigger word */
818 ipath_flush_wc();
819 __raw_writel(addr[w - 1], piobuf + w - 1);
820 /* be sure trigger word is written */
821 ipath_flush_wc();
822 ret = 0;
823 goto bail;
824 }
825 copy_io(piobuf, ss, len);
826 ret = 0;
827
828bail:
829 return ret;
830}
831
832int ipath_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
833 u64 *rwords, u64 *spkts, u64 *rpkts,
834 u64 *xmit_wait)
835{
836 int ret;
837
838 if (!(dd->ipath_flags & IPATH_INITTED)) {
839 /* no hardware, freeze, etc. */
840 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
841 ret = -EINVAL;
842 goto bail;
843 }
844 *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
845 *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
846 *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
847 *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
848 *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
849
850 ret = 0;
851
852bail:
853 return ret;
854}
855
856/**
857 * ipath_get_counters - get various chip counters
858 * @dd: the infinipath device
859 * @cntrs: counters are placed here
860 *
861 * Return the counters needed by recv_pma_get_portcounters().
862 */
863int ipath_get_counters(struct ipath_devdata *dd,
864 struct ipath_verbs_counters *cntrs)
865{
866 int ret;
867
868 if (!(dd->ipath_flags & IPATH_INITTED)) {
869 /* no hardware, freeze, etc. */
870 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
871 ret = -EINVAL;
872 goto bail;
873 }
874 cntrs->symbol_error_counter =
875 ipath_snap_cntr(dd, dd->ipath_cregs->cr_ibsymbolerrcnt);
876 cntrs->link_error_recovery_counter =
877 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkerrrecovcnt);
878 /*
879 * The link downed counter counts when the other side downs the
880 * connection. We add in the number of times we downed the link
881 * due to local link integrity errors to compensate.
882 */
883 cntrs->link_downed_counter =
884 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkdowncnt);
885 cntrs->port_rcv_errors =
886 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rxdroppktcnt) +
887 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvovflcnt) +
888 ipath_snap_cntr(dd, dd->ipath_cregs->cr_portovflcnt) +
889 ipath_snap_cntr(dd, dd->ipath_cregs->cr_err_rlencnt) +
890 ipath_snap_cntr(dd, dd->ipath_cregs->cr_invalidrlencnt) +
891 ipath_snap_cntr(dd, dd->ipath_cregs->cr_erricrccnt) +
892 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errvcrccnt) +
893 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlpcrccnt) +
894 ipath_snap_cntr(dd, dd->ipath_cregs->cr_badformatcnt);
895 cntrs->port_rcv_remphys_errors =
896 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvebpcnt);
897 cntrs->port_xmit_discards =
898 ipath_snap_cntr(dd, dd->ipath_cregs->cr_unsupvlcnt);
899 cntrs->port_xmit_data =
900 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
901 cntrs->port_rcv_data =
902 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
903 cntrs->port_xmit_packets =
904 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
905 cntrs->port_rcv_packets =
906 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
907 cntrs->local_link_integrity_errors = dd->ipath_lli_errors;
908 cntrs->excessive_buffer_overrun_errors = 0; /* XXX */
909
910 ret = 0;
911
912bail:
913 return ret;
914}
915
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800916/**
917 * ipath_ib_piobufavail - callback when a PIO buffer is available
918 * @arg: the device pointer
919 *
920 * This is called from ipath_intr() at interrupt level when a PIO buffer is
921 * available after ipath_verbs_send() returned an error that no buffers were
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700922 * available. Return 1 if we consumed all the PIO buffers and we still have
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800923 * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700924 * return zero).
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800925 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700926int ipath_ib_piobufavail(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800927{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800928 struct ipath_qp *qp;
929 unsigned long flags;
930
931 if (dev == NULL)
932 goto bail;
933
934 spin_lock_irqsave(&dev->pending_lock, flags);
935 while (!list_empty(&dev->piowait)) {
936 qp = list_entry(dev->piowait.next, struct ipath_qp,
937 piowait);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700938 list_del_init(&qp->piowait);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800939 tasklet_hi_schedule(&qp->s_task);
940 }
941 spin_unlock_irqrestore(&dev->pending_lock, flags);
942
943bail:
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700944 return 0;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800945}
946
947static int ipath_query_device(struct ib_device *ibdev,
948 struct ib_device_attr *props)
949{
950 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800951
952 memset(props, 0, sizeof(*props));
953
954 props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
955 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
956 IB_DEVICE_SYS_IMAGE_GUID;
Ralph Campbellc9f79bd2006-07-17 18:19:54 -0700957 props->page_size_cap = PAGE_SIZE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700958 props->vendor_id = dev->dd->ipath_vendorid;
959 props->vendor_part_id = dev->dd->ipath_deviceid;
960 props->hw_ver = dev->dd->ipath_pcirev;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800961
962 props->sys_image_guid = dev->sys_image_guid;
963
964 props->max_mr_size = ~0ull;
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700965 props->max_qp = ib_ipath_max_qps;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700966 props->max_qp_wr = ib_ipath_max_qp_wrs;
967 props->max_sge = ib_ipath_max_sges;
968 props->max_cq = ib_ipath_max_cqs;
969 props->max_ah = ib_ipath_max_ahs;
970 props->max_cqe = ib_ipath_max_cqes;
971 props->max_mr = dev->lk_table.max;
972 props->max_pd = ib_ipath_max_pds;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800973 props->max_qp_rd_atom = 1;
974 props->max_qp_init_rd_atom = 1;
975 /* props->max_res_rd_atom */
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700976 props->max_srq = ib_ipath_max_srqs;
977 props->max_srq_wr = ib_ipath_max_srq_wrs;
978 props->max_srq_sge = ib_ipath_max_srq_sges;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800979 /* props->local_ca_ack_delay */
980 props->atomic_cap = IB_ATOMIC_HCA;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700981 props->max_pkeys = ipath_get_npkeys(dev->dd);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700982 props->max_mcast_grp = ib_ipath_max_mcast_grps;
983 props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800984 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
985 props->max_mcast_grp;
986
987 return 0;
988}
989
990const u8 ipath_cvt_physportstate[16] = {
991 [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3,
992 [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5,
993 [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2,
994 [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2,
995 [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1,
996 [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1,
997 [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4,
998 [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4,
999 [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4,
1000 [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4,
1001 [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6,
1002 [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6,
1003 [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6,
1004};
1005
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001006u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
1007{
1008 return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
1009}
1010
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001011static int ipath_query_port(struct ib_device *ibdev,
1012 u8 port, struct ib_port_attr *props)
1013{
1014 struct ipath_ibdev *dev = to_idev(ibdev);
1015 enum ib_mtu mtu;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001016 u16 lid = dev->dd->ipath_lid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001017 u64 ibcstat;
1018
1019 memset(props, 0, sizeof(*props));
1020 props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1021 props->lmc = dev->mkeyprot_resv_lmc & 7;
1022 props->sm_lid = dev->sm_lid;
1023 props->sm_sl = dev->sm_sl;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001024 ibcstat = dev->dd->ipath_lastibcstat;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001025 props->state = ((ibcstat >> 4) & 0x3) + 1;
1026 /* See phys_state_show() */
1027 props->phys_state = ipath_cvt_physportstate[
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001028 dev->dd->ipath_lastibcstat & 0xf];
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001029 props->port_cap_flags = dev->port_cap_flags;
1030 props->gid_tbl_len = 1;
Bryan O'Sullivanc100f622006-07-01 04:36:07 -07001031 props->max_msg_sz = 0x80000000;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001032 props->pkey_tbl_len = ipath_get_npkeys(dev->dd);
1033 props->bad_pkey_cntr = ipath_get_cr_errpkey(dev->dd) -
Bryan O'Sullivan443a64a2006-07-01 04:35:48 -07001034 dev->z_pkey_violations;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001035 props->qkey_viol_cntr = dev->qkey_violations;
1036 props->active_width = IB_WIDTH_4X;
1037 /* See rate_show() */
1038 props->active_speed = 1; /* Regular 10Mbs speed. */
1039 props->max_vl_num = 1; /* VLCap = VL0 */
1040 props->init_type_reply = 0;
1041
1042 props->max_mtu = IB_MTU_4096;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001043 switch (dev->dd->ipath_ibmtu) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001044 case 4096:
1045 mtu = IB_MTU_4096;
1046 break;
1047 case 2048:
1048 mtu = IB_MTU_2048;
1049 break;
1050 case 1024:
1051 mtu = IB_MTU_1024;
1052 break;
1053 case 512:
1054 mtu = IB_MTU_512;
1055 break;
1056 case 256:
1057 mtu = IB_MTU_256;
1058 break;
1059 default:
1060 mtu = IB_MTU_2048;
1061 }
1062 props->active_mtu = mtu;
1063 props->subnet_timeout = dev->subnet_timeout;
1064
1065 return 0;
1066}
1067
1068static int ipath_modify_device(struct ib_device *device,
1069 int device_modify_mask,
1070 struct ib_device_modify *device_modify)
1071{
1072 int ret;
1073
1074 if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1075 IB_DEVICE_MODIFY_NODE_DESC)) {
1076 ret = -EOPNOTSUPP;
1077 goto bail;
1078 }
1079
1080 if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
1081 memcpy(device->node_desc, device_modify->node_desc, 64);
1082
1083 if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
1084 to_idev(device)->sys_image_guid =
1085 cpu_to_be64(device_modify->sys_image_guid);
1086
1087 ret = 0;
1088
1089bail:
1090 return ret;
1091}
1092
1093static int ipath_modify_port(struct ib_device *ibdev,
1094 u8 port, int port_modify_mask,
1095 struct ib_port_modify *props)
1096{
1097 struct ipath_ibdev *dev = to_idev(ibdev);
1098
1099 dev->port_cap_flags |= props->set_port_cap_mask;
1100 dev->port_cap_flags &= ~props->clr_port_cap_mask;
1101 if (port_modify_mask & IB_PORT_SHUTDOWN)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001102 ipath_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001103 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1104 dev->qkey_violations = 0;
1105 return 0;
1106}
1107
1108static int ipath_query_gid(struct ib_device *ibdev, u8 port,
1109 int index, union ib_gid *gid)
1110{
1111 struct ipath_ibdev *dev = to_idev(ibdev);
1112 int ret;
1113
1114 if (index >= 1) {
1115 ret = -EINVAL;
1116 goto bail;
1117 }
1118 gid->global.subnet_prefix = dev->gid_prefix;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001119 gid->global.interface_id = dev->dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001120
1121 ret = 0;
1122
1123bail:
1124 return ret;
1125}
1126
1127static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
1128 struct ib_ucontext *context,
1129 struct ib_udata *udata)
1130{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001131 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001132 struct ipath_pd *pd;
1133 struct ib_pd *ret;
1134
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001135 /*
1136 * This is actually totally arbitrary. Some correctness tests
1137 * assume there's a maximum number of PDs that can be allocated.
1138 * We don't actually have this limit, but we fail the test if
1139 * we allow allocations of more than we report for this value.
1140 */
1141
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001142 pd = kmalloc(sizeof *pd, GFP_KERNEL);
1143 if (!pd) {
1144 ret = ERR_PTR(-ENOMEM);
1145 goto bail;
1146 }
1147
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001148 spin_lock(&dev->n_pds_lock);
1149 if (dev->n_pds_allocated == ib_ipath_max_pds) {
1150 spin_unlock(&dev->n_pds_lock);
1151 kfree(pd);
1152 ret = ERR_PTR(-ENOMEM);
1153 goto bail;
1154 }
1155
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001156 dev->n_pds_allocated++;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001157 spin_unlock(&dev->n_pds_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001158
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001159 /* ib_alloc_pd() will initialize pd->ibpd. */
1160 pd->user = udata != NULL;
1161
1162 ret = &pd->ibpd;
1163
1164bail:
1165 return ret;
1166}
1167
1168static int ipath_dealloc_pd(struct ib_pd *ibpd)
1169{
1170 struct ipath_pd *pd = to_ipd(ibpd);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001171 struct ipath_ibdev *dev = to_idev(ibpd->device);
1172
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001173 spin_lock(&dev->n_pds_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001174 dev->n_pds_allocated--;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001175 spin_unlock(&dev->n_pds_lock);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001176
1177 kfree(pd);
1178
1179 return 0;
1180}
1181
1182/**
1183 * ipath_create_ah - create an address handle
1184 * @pd: the protection domain
1185 * @ah_attr: the attributes of the AH
1186 *
1187 * This may be called from interrupt context.
1188 */
1189static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
1190 struct ib_ah_attr *ah_attr)
1191{
1192 struct ipath_ah *ah;
1193 struct ib_ah *ret;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001194 struct ipath_ibdev *dev = to_idev(pd->device);
1195
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001196 /* A multicast address requires a GRH (see ch. 8.4.1). */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001197 if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
1198 ah_attr->dlid != IPATH_PERMISSIVE_LID &&
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001199 !(ah_attr->ah_flags & IB_AH_GRH)) {
1200 ret = ERR_PTR(-EINVAL);
1201 goto bail;
1202 }
1203
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -07001204 if (ah_attr->dlid == 0) {
1205 ret = ERR_PTR(-EINVAL);
1206 goto bail;
1207 }
1208
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001209 if (ah_attr->port_num < 1 ||
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -07001210 ah_attr->port_num > pd->device->phys_port_cnt) {
1211 ret = ERR_PTR(-EINVAL);
1212 goto bail;
1213 }
1214
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001215 ah = kmalloc(sizeof *ah, GFP_ATOMIC);
1216 if (!ah) {
1217 ret = ERR_PTR(-ENOMEM);
1218 goto bail;
1219 }
1220
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001221 spin_lock(&dev->n_ahs_lock);
1222 if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
1223 spin_unlock(&dev->n_ahs_lock);
1224 kfree(ah);
1225 ret = ERR_PTR(-ENOMEM);
1226 goto bail;
1227 }
1228
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001229 dev->n_ahs_allocated++;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001230 spin_unlock(&dev->n_ahs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001231
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001232 /* ib_create_ah() will initialize ah->ibah. */
1233 ah->attr = *ah_attr;
1234
1235 ret = &ah->ibah;
1236
1237bail:
1238 return ret;
1239}
1240
1241/**
1242 * ipath_destroy_ah - destroy an address handle
1243 * @ibah: the AH to destroy
1244 *
1245 * This may be called from interrupt context.
1246 */
1247static int ipath_destroy_ah(struct ib_ah *ibah)
1248{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001249 struct ipath_ibdev *dev = to_idev(ibah->device);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001250 struct ipath_ah *ah = to_iah(ibah);
1251
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001252 spin_lock(&dev->n_ahs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001253 dev->n_ahs_allocated--;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001254 spin_unlock(&dev->n_ahs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001255
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001256 kfree(ah);
1257
1258 return 0;
1259}
1260
1261static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1262{
1263 struct ipath_ah *ah = to_iah(ibah);
1264
1265 *ah_attr = ah->attr;
1266
1267 return 0;
1268}
1269
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001270/**
1271 * ipath_get_npkeys - return the size of the PKEY table for port 0
1272 * @dd: the infinipath device
1273 */
1274unsigned ipath_get_npkeys(struct ipath_devdata *dd)
1275{
1276 return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
1277}
1278
1279/**
1280 * ipath_get_pkey - return the indexed PKEY from the port 0 PKEY table
1281 * @dd: the infinipath device
1282 * @index: the PKEY index
1283 */
1284unsigned ipath_get_pkey(struct ipath_devdata *dd, unsigned index)
1285{
1286 unsigned ret;
1287
1288 if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
1289 ret = 0;
1290 else
1291 ret = dd->ipath_pd[0]->port_pkeys[index];
1292
1293 return ret;
1294}
1295
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001296static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1297 u16 *pkey)
1298{
1299 struct ipath_ibdev *dev = to_idev(ibdev);
1300 int ret;
1301
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001302 if (index >= ipath_get_npkeys(dev->dd)) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001303 ret = -EINVAL;
1304 goto bail;
1305 }
1306
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001307 *pkey = ipath_get_pkey(dev->dd, index);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001308 ret = 0;
1309
1310bail:
1311 return ret;
1312}
1313
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001314/**
1315 * ipath_alloc_ucontext - allocate a ucontest
1316 * @ibdev: the infiniband device
1317 * @udata: not used by the InfiniPath driver
1318 */
1319
1320static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
1321 struct ib_udata *udata)
1322{
1323 struct ipath_ucontext *context;
1324 struct ib_ucontext *ret;
1325
1326 context = kmalloc(sizeof *context, GFP_KERNEL);
1327 if (!context) {
1328 ret = ERR_PTR(-ENOMEM);
1329 goto bail;
1330 }
1331
1332 ret = &context->ibucontext;
1333
1334bail:
1335 return ret;
1336}
1337
1338static int ipath_dealloc_ucontext(struct ib_ucontext *context)
1339{
1340 kfree(to_iucontext(context));
1341 return 0;
1342}
1343
1344static int ipath_verbs_register_sysfs(struct ib_device *dev);
1345
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001346static void __verbs_timer(unsigned long arg)
1347{
1348 struct ipath_devdata *dd = (struct ipath_devdata *) arg;
1349
1350 /*
1351 * If port 0 receive packet interrupts are not available, or
1352 * can be missed, poll the receive queue
1353 */
1354 if (dd->ipath_flags & IPATH_POLL_RX_INTR)
1355 ipath_kreceive(dd);
1356
1357 /* Handle verbs layer timeouts. */
1358 ipath_ib_timer(dd->verbs_dev);
1359
1360 mod_timer(&dd->verbs_timer, jiffies + 1);
1361}
1362
1363static int enable_timer(struct ipath_devdata *dd)
1364{
1365 /*
1366 * Early chips had a design flaw where the chip and kernel idea
1367 * of the tail register don't always agree, and therefore we won't
1368 * get an interrupt on the next packet received.
1369 * If the board supports per packet receive interrupts, use it.
1370 * Otherwise, the timer function periodically checks for packets
1371 * to cover this case.
1372 * Either way, the timer is needed for verbs layer related
1373 * processing.
1374 */
1375 if (dd->ipath_flags & IPATH_GPIO_INTR) {
1376 ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
1377 0x2074076542310ULL);
1378 /* Enable GPIO bit 2 interrupt */
1379 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1380 (u64) (1 << 2));
1381 }
1382
1383 init_timer(&dd->verbs_timer);
1384 dd->verbs_timer.function = __verbs_timer;
1385 dd->verbs_timer.data = (unsigned long)dd;
1386 dd->verbs_timer.expires = jiffies + 1;
1387 add_timer(&dd->verbs_timer);
1388
1389 return 0;
1390}
1391
1392static int disable_timer(struct ipath_devdata *dd)
1393{
1394 /* Disable GPIO bit 2 interrupt */
1395 if (dd->ipath_flags & IPATH_GPIO_INTR)
1396 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask, 0);
1397
1398 del_timer_sync(&dd->verbs_timer);
1399
1400 return 0;
1401}
1402
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001403/**
1404 * ipath_register_ib_device - register our device with the infiniband core
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001405 * @dd: the device data structure
1406 * Return the allocated ipath_ibdev pointer or NULL on error.
1407 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001408int ipath_register_ib_device(struct ipath_devdata *dd)
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001409{
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001410 struct ipath_verbs_counters cntrs;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001411 struct ipath_ibdev *idev;
1412 struct ib_device *dev;
1413 int ret;
1414
1415 idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001416 if (idev == NULL) {
1417 ret = -ENOMEM;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001418 goto bail;
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001419 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001420
1421 dev = &idev->ibdev;
1422
1423 /* Only need to initialize non-zero fields. */
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001424 spin_lock_init(&idev->n_pds_lock);
1425 spin_lock_init(&idev->n_ahs_lock);
1426 spin_lock_init(&idev->n_cqs_lock);
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -07001427 spin_lock_init(&idev->n_qps_lock);
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001428 spin_lock_init(&idev->n_srqs_lock);
1429 spin_lock_init(&idev->n_mcast_grps_lock);
1430
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001431 spin_lock_init(&idev->qp_table.lock);
1432 spin_lock_init(&idev->lk_table.lock);
1433 idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1434 /* Set the prefix to the default value (see ch. 4.1.1) */
1435 idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
1436
1437 ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
1438 if (ret)
1439 goto err_qp;
1440
1441 /*
1442 * The top ib_ipath_lkey_table_size bits are used to index the
1443 * table. The lower 8 bits can be owned by the user (copied from
1444 * the LKEY). The remaining bits act as a generation number or tag.
1445 */
1446 idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
1447 idev->lk_table.table = kzalloc(idev->lk_table.max *
1448 sizeof(*idev->lk_table.table),
1449 GFP_KERNEL);
1450 if (idev->lk_table.table == NULL) {
1451 ret = -ENOMEM;
1452 goto err_lk;
1453 }
1454 spin_lock_init(&idev->pending_lock);
1455 INIT_LIST_HEAD(&idev->pending[0]);
1456 INIT_LIST_HEAD(&idev->pending[1]);
1457 INIT_LIST_HEAD(&idev->pending[2]);
1458 INIT_LIST_HEAD(&idev->piowait);
1459 INIT_LIST_HEAD(&idev->rnrwait);
1460 idev->pending_index = 0;
1461 idev->port_cap_flags =
1462 IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
1463 idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1464 idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1465 idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1466 idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1467 idev->pma_counter_select[5] = IB_PMA_PORT_XMIT_WAIT;
1468 idev->link_width_enabled = 3; /* 1x or 4x */
1469
Bryan O'Sullivanfba75202006-07-01 04:36:09 -07001470 /* Snapshot current HW counters to "clear" them. */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001471 ipath_get_counters(dd, &cntrs);
Bryan O'Sullivanfba75202006-07-01 04:36:09 -07001472 idev->z_symbol_error_counter = cntrs.symbol_error_counter;
1473 idev->z_link_error_recovery_counter =
1474 cntrs.link_error_recovery_counter;
1475 idev->z_link_downed_counter = cntrs.link_downed_counter;
1476 idev->z_port_rcv_errors = cntrs.port_rcv_errors;
1477 idev->z_port_rcv_remphys_errors =
1478 cntrs.port_rcv_remphys_errors;
1479 idev->z_port_xmit_discards = cntrs.port_xmit_discards;
1480 idev->z_port_xmit_data = cntrs.port_xmit_data;
1481 idev->z_port_rcv_data = cntrs.port_rcv_data;
1482 idev->z_port_xmit_packets = cntrs.port_xmit_packets;
1483 idev->z_port_rcv_packets = cntrs.port_rcv_packets;
1484 idev->z_local_link_integrity_errors =
1485 cntrs.local_link_integrity_errors;
1486 idev->z_excessive_buffer_overrun_errors =
1487 cntrs.excessive_buffer_overrun_errors;
1488
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001489 /*
1490 * The system image GUID is supposed to be the same for all
1491 * IB HCAs in a single system but since there can be other
1492 * device types in the system, we can't be sure this is unique.
1493 */
1494 if (!sys_image_guid)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001495 sys_image_guid = dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001496 idev->sys_image_guid = sys_image_guid;
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001497 idev->ib_unit = dd->ipath_unit;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001498 idev->dd = dd;
1499
1500 strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
Bryan O'Sullivan41c75a12006-05-23 11:32:36 -07001501 dev->owner = THIS_MODULE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001502 dev->node_guid = dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001503 dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
1504 dev->uverbs_cmd_mask =
1505 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1506 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1507 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1508 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1509 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1510 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
1511 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
1512 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
1513 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1514 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1515 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1516 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
1517 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
1518 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1519 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
1520 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
1521 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1522 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
1523 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
1524 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1525 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
1526 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
1527 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1528 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1529 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1530 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
1531 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
1532 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
1533 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
1534 dev->node_type = IB_NODE_CA;
1535 dev->phys_port_cnt = 1;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001536 dev->dma_device = &dd->pcidev->dev;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001537 dev->class_dev.dev = dev->dma_device;
1538 dev->query_device = ipath_query_device;
1539 dev->modify_device = ipath_modify_device;
1540 dev->query_port = ipath_query_port;
1541 dev->modify_port = ipath_modify_port;
1542 dev->query_pkey = ipath_query_pkey;
1543 dev->query_gid = ipath_query_gid;
1544 dev->alloc_ucontext = ipath_alloc_ucontext;
1545 dev->dealloc_ucontext = ipath_dealloc_ucontext;
1546 dev->alloc_pd = ipath_alloc_pd;
1547 dev->dealloc_pd = ipath_dealloc_pd;
1548 dev->create_ah = ipath_create_ah;
1549 dev->destroy_ah = ipath_destroy_ah;
1550 dev->query_ah = ipath_query_ah;
1551 dev->create_srq = ipath_create_srq;
1552 dev->modify_srq = ipath_modify_srq;
1553 dev->query_srq = ipath_query_srq;
1554 dev->destroy_srq = ipath_destroy_srq;
1555 dev->create_qp = ipath_create_qp;
1556 dev->modify_qp = ipath_modify_qp;
1557 dev->query_qp = ipath_query_qp;
1558 dev->destroy_qp = ipath_destroy_qp;
1559 dev->post_send = ipath_post_send;
1560 dev->post_recv = ipath_post_receive;
1561 dev->post_srq_recv = ipath_post_srq_receive;
1562 dev->create_cq = ipath_create_cq;
1563 dev->destroy_cq = ipath_destroy_cq;
1564 dev->resize_cq = ipath_resize_cq;
1565 dev->poll_cq = ipath_poll_cq;
1566 dev->req_notify_cq = ipath_req_notify_cq;
1567 dev->get_dma_mr = ipath_get_dma_mr;
1568 dev->reg_phys_mr = ipath_reg_phys_mr;
1569 dev->reg_user_mr = ipath_reg_user_mr;
1570 dev->dereg_mr = ipath_dereg_mr;
1571 dev->alloc_fmr = ipath_alloc_fmr;
1572 dev->map_phys_fmr = ipath_map_phys_fmr;
1573 dev->unmap_fmr = ipath_unmap_fmr;
1574 dev->dealloc_fmr = ipath_dealloc_fmr;
1575 dev->attach_mcast = ipath_multicast_attach;
1576 dev->detach_mcast = ipath_multicast_detach;
1577 dev->process_mad = ipath_process_mad;
Ralph Campbell373d9912006-09-22 15:22:26 -07001578 dev->mmap = ipath_mmap;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001579
1580 snprintf(dev->node_desc, sizeof(dev->node_desc),
Bryan O'Sullivan0fd41362006-08-25 11:24:34 -07001581 IPATH_IDSTR " %s", system_utsname.nodename);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001582
1583 ret = ib_register_device(dev);
1584 if (ret)
1585 goto err_reg;
1586
1587 if (ipath_verbs_register_sysfs(dev))
1588 goto err_class;
1589
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001590 enable_timer(dd);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001591
1592 goto bail;
1593
1594err_class:
1595 ib_unregister_device(dev);
1596err_reg:
1597 kfree(idev->lk_table.table);
1598err_lk:
1599 kfree(idev->qp_table.table);
1600err_qp:
1601 ib_dealloc_device(dev);
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001602 ipath_dev_err(dd, "cannot register verbs: %d!\n", -ret);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001603 idev = NULL;
1604
1605bail:
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001606 dd->verbs_dev = idev;
1607 return ret;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001608}
1609
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001610void ipath_unregister_ib_device(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001611{
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001612 struct ib_device *ibdev = &dev->ibdev;
1613
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001614 disable_timer(dev->dd);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001615
1616 ib_unregister_device(ibdev);
1617
1618 if (!list_empty(&dev->pending[0]) ||
1619 !list_empty(&dev->pending[1]) ||
1620 !list_empty(&dev->pending[2]))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001621 ipath_dev_err(dev->dd, "pending list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001622 if (!list_empty(&dev->piowait))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001623 ipath_dev_err(dev->dd, "piowait list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001624 if (!list_empty(&dev->rnrwait))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001625 ipath_dev_err(dev->dd, "rnrwait list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001626 if (!ipath_mcast_tree_empty())
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001627 ipath_dev_err(dev->dd, "multicast table memory leak!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001628 /*
1629 * Note that ipath_unregister_ib_device() can be called before all
1630 * the QPs are destroyed!
1631 */
1632 ipath_free_all_qps(&dev->qp_table);
1633 kfree(dev->qp_table.table);
1634 kfree(dev->lk_table.table);
1635 ib_dealloc_device(ibdev);
1636}
1637
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001638static ssize_t show_rev(struct class_device *cdev, char *buf)
1639{
Roland Dreier5494c222006-04-19 11:40:12 -07001640 struct ipath_ibdev *dev =
1641 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001642
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001643 return sprintf(buf, "%x\n", dev->dd->ipath_pcirev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001644}
1645
1646static ssize_t show_hca(struct class_device *cdev, char *buf)
1647{
Roland Dreier5494c222006-04-19 11:40:12 -07001648 struct ipath_ibdev *dev =
1649 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1650 int ret;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001651
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001652 ret = dev->dd->ipath_f_get_boardname(dev->dd, buf, 128);
Roland Dreier5494c222006-04-19 11:40:12 -07001653 if (ret < 0)
1654 goto bail;
1655 strcat(buf, "\n");
1656 ret = strlen(buf);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001657
1658bail:
1659 return ret;
1660}
1661
1662static ssize_t show_stats(struct class_device *cdev, char *buf)
1663{
Roland Dreier5494c222006-04-19 11:40:12 -07001664 struct ipath_ibdev *dev =
1665 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1666 int i;
1667 int len;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001668
Roland Dreier5494c222006-04-19 11:40:12 -07001669 len = sprintf(buf,
1670 "RC resends %d\n"
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -07001671 "RC no QACK %d\n"
Roland Dreier5494c222006-04-19 11:40:12 -07001672 "RC ACKs %d\n"
1673 "RC SEQ NAKs %d\n"
1674 "RC RDMA seq %d\n"
1675 "RC RNR NAKs %d\n"
1676 "RC OTH NAKs %d\n"
1677 "RC timeouts %d\n"
1678 "RC RDMA dup %d\n"
1679 "piobuf wait %d\n"
1680 "no piobuf %d\n"
1681 "PKT drops %d\n"
1682 "WQE errs %d\n",
1683 dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
1684 dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
1685 dev->n_other_naks, dev->n_timeouts,
1686 dev->n_rdma_dup_busy, dev->n_piowait,
1687 dev->n_no_piobuf, dev->n_pkt_drops, dev->n_wqe_errs);
1688 for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001689 const struct ipath_opcode_stats *si = &dev->opstats[i];
1690
Roland Dreier5494c222006-04-19 11:40:12 -07001691 if (!si->n_packets && !si->n_bytes)
1692 continue;
1693 len += sprintf(buf + len, "%02x %llu/%llu\n", i,
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001694 (unsigned long long) si->n_packets,
Roland Dreier5494c222006-04-19 11:40:12 -07001695 (unsigned long long) si->n_bytes);
1696 }
1697 return len;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001698}
1699
1700static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1701static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1702static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
1703static CLASS_DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
1704
1705static struct class_device_attribute *ipath_class_attributes[] = {
Roland Dreier5494c222006-04-19 11:40:12 -07001706 &class_device_attr_hw_rev,
1707 &class_device_attr_hca_type,
1708 &class_device_attr_board_id,
1709 &class_device_attr_stats
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001710};
1711
1712static int ipath_verbs_register_sysfs(struct ib_device *dev)
1713{
Roland Dreier5494c222006-04-19 11:40:12 -07001714 int i;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001715 int ret;
1716
Roland Dreier5494c222006-04-19 11:40:12 -07001717 for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
1718 if (class_device_create_file(&dev->class_dev,
1719 ipath_class_attributes[i])) {
1720 ret = 1;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001721 goto bail;
1722 }
1723
Roland Dreier5494c222006-04-19 11:40:12 -07001724 ret = 0;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001725
1726bail:
1727 return ret;
1728}