blob: 35241b76d766a7a946a2d3e0fb466e83eece32a2 [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'Sullivand821f022006-08-25 11:24:45 -0700110static unsigned int ib_ipath_disable_sma;
111module_param_named(disable_sma, ib_ipath_disable_sma, uint, S_IWUSR | S_IRUGO);
112MODULE_PARM_DESC(ib_ipath_disable_sma, "Disable the SMA");
113
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800114const int ib_ipath_state_ops[IB_QPS_ERR + 1] = {
115 [IB_QPS_RESET] = 0,
116 [IB_QPS_INIT] = IPATH_POST_RECV_OK,
117 [IB_QPS_RTR] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
118 [IB_QPS_RTS] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
119 IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK,
120 [IB_QPS_SQD] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
121 IPATH_POST_SEND_OK,
122 [IB_QPS_SQE] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
123 [IB_QPS_ERR] = 0,
124};
125
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700126struct ipath_ucontext {
127 struct ib_ucontext ibucontext;
128};
129
130static inline struct ipath_ucontext *to_iucontext(struct ib_ucontext
131 *ibucontext)
132{
133 return container_of(ibucontext, struct ipath_ucontext, ibucontext);
134}
135
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800136/*
137 * Translate ib_wr_opcode into ib_wc_opcode.
138 */
139const enum ib_wc_opcode ib_ipath_wc_opcode[] = {
140 [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
141 [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
142 [IB_WR_SEND] = IB_WC_SEND,
143 [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
144 [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
145 [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
146 [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
147};
148
149/*
150 * System image GUID.
151 */
Roland Dreierac2ae4c2006-04-19 11:40:12 -0700152static __be64 sys_image_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800153
154/**
155 * ipath_copy_sge - copy data to SGE memory
156 * @ss: the SGE state
157 * @data: the data to copy
158 * @length: the length of the data
159 */
160void ipath_copy_sge(struct ipath_sge_state *ss, void *data, u32 length)
161{
162 struct ipath_sge *sge = &ss->sge;
163
164 while (length) {
165 u32 len = sge->length;
166
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800167 if (len > length)
168 len = length;
Ralph Campbell30d149a2007-06-18 14:24:44 -0700169 if (len > sge->sge_length)
170 len = sge->sge_length;
171 BUG_ON(len == 0);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800172 memcpy(sge->vaddr, data, len);
173 sge->vaddr += len;
174 sge->length -= len;
175 sge->sge_length -= len;
176 if (sge->sge_length == 0) {
177 if (--ss->num_sge)
178 *sge = *ss->sg_list++;
179 } else if (sge->length == 0 && sge->mr != NULL) {
180 if (++sge->n >= IPATH_SEGSZ) {
181 if (++sge->m >= sge->mr->mapsz)
182 break;
183 sge->n = 0;
184 }
185 sge->vaddr =
186 sge->mr->map[sge->m]->segs[sge->n].vaddr;
187 sge->length =
188 sge->mr->map[sge->m]->segs[sge->n].length;
189 }
190 data += len;
191 length -= len;
192 }
193}
194
195/**
196 * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
197 * @ss: the SGE state
198 * @length: the number of bytes to skip
199 */
200void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
201{
202 struct ipath_sge *sge = &ss->sge;
203
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800204 while (length) {
205 u32 len = sge->length;
206
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800207 if (len > length)
208 len = length;
Ralph Campbell30d149a2007-06-18 14:24:44 -0700209 if (len > sge->sge_length)
210 len = sge->sge_length;
211 BUG_ON(len == 0);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800212 sge->vaddr += len;
213 sge->length -= len;
214 sge->sge_length -= len;
215 if (sge->sge_length == 0) {
216 if (--ss->num_sge)
217 *sge = *ss->sg_list++;
218 } else if (sge->length == 0 && sge->mr != NULL) {
219 if (++sge->n >= IPATH_SEGSZ) {
220 if (++sge->m >= sge->mr->mapsz)
221 break;
222 sge->n = 0;
223 }
224 sge->vaddr =
225 sge->mr->map[sge->m]->segs[sge->n].vaddr;
226 sge->length =
227 sge->mr->map[sge->m]->segs[sge->n].length;
228 }
229 length -= len;
230 }
231}
232
233/**
234 * ipath_post_send - post a send on a QP
235 * @ibqp: the QP to post the send on
236 * @wr: the list of work requests to post
237 * @bad_wr: the first bad WR is put here
238 *
239 * This may be called from interrupt context.
240 */
241static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
242 struct ib_send_wr **bad_wr)
243{
244 struct ipath_qp *qp = to_iqp(ibqp);
245 int err = 0;
246
247 /* Check that state is OK to post send. */
248 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK)) {
249 *bad_wr = wr;
250 err = -EINVAL;
251 goto bail;
252 }
253
254 for (; wr; wr = wr->next) {
255 switch (qp->ibqp.qp_type) {
256 case IB_QPT_UC:
257 case IB_QPT_RC:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700258 err = ipath_post_ruc_send(qp, wr);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800259 break;
260
261 case IB_QPT_SMI:
262 case IB_QPT_GSI:
263 case IB_QPT_UD:
264 err = ipath_post_ud_send(qp, wr);
265 break;
266
267 default:
268 err = -EINVAL;
269 }
270 if (err) {
271 *bad_wr = wr;
272 break;
273 }
274 }
275
276bail:
277 return err;
278}
279
280/**
281 * ipath_post_receive - post a receive on a QP
282 * @ibqp: the QP to post the receive on
283 * @wr: the WR to post
284 * @bad_wr: the first bad WR is put here
285 *
286 * This may be called from interrupt context.
287 */
288static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
289 struct ib_recv_wr **bad_wr)
290{
291 struct ipath_qp *qp = to_iqp(ibqp);
Ralph Campbell373d9912006-09-22 15:22:26 -0700292 struct ipath_rwq *wq = qp->r_rq.wq;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800293 unsigned long flags;
294 int ret;
295
296 /* Check that state is OK to post receive. */
Ralph Campbell373d9912006-09-22 15:22:26 -0700297 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK) || !wq) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800298 *bad_wr = wr;
299 ret = -EINVAL;
300 goto bail;
301 }
302
303 for (; wr; wr = wr->next) {
304 struct ipath_rwqe *wqe;
305 u32 next;
Ralph Campbell373d9912006-09-22 15:22:26 -0700306 int i;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800307
Ralph Campbell373d9912006-09-22 15:22:26 -0700308 if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800309 *bad_wr = wr;
310 ret = -ENOMEM;
311 goto bail;
312 }
313
314 spin_lock_irqsave(&qp->r_rq.lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700315 next = wq->head + 1;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800316 if (next >= qp->r_rq.size)
317 next = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700318 if (next == wq->tail) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800319 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
320 *bad_wr = wr;
321 ret = -ENOMEM;
322 goto bail;
323 }
324
Ralph Campbell373d9912006-09-22 15:22:26 -0700325 wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800326 wqe->wr_id = wr->wr_id;
Ralph Campbell373d9912006-09-22 15:22:26 -0700327 wqe->num_sge = wr->num_sge;
328 for (i = 0; i < wr->num_sge; i++)
329 wqe->sg_list[i] = wr->sg_list[i];
330 wq->head = next;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800331 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
332 }
333 ret = 0;
334
335bail:
336 return ret;
337}
338
339/**
340 * ipath_qp_rcv - processing an incoming packet on a QP
341 * @dev: the device the packet came on
342 * @hdr: the packet header
343 * @has_grh: true if the packet has a GRH
344 * @data: the packet data
345 * @tlen: the packet length
346 * @qp: the QP the packet came on
347 *
348 * This is called from ipath_ib_rcv() to process an incoming packet
349 * for the given QP.
350 * Called at interrupt level.
351 */
352static void ipath_qp_rcv(struct ipath_ibdev *dev,
353 struct ipath_ib_header *hdr, int has_grh,
354 void *data, u32 tlen, struct ipath_qp *qp)
355{
356 /* Check for valid receive state. */
357 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
358 dev->n_pkt_drops++;
359 return;
360 }
361
362 switch (qp->ibqp.qp_type) {
363 case IB_QPT_SMI:
364 case IB_QPT_GSI:
Bryan O'Sullivand821f022006-08-25 11:24:45 -0700365 if (ib_ipath_disable_sma)
366 break;
367 /* FALLTHROUGH */
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800368 case IB_QPT_UD:
369 ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
370 break;
371
372 case IB_QPT_RC:
373 ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
374 break;
375
376 case IB_QPT_UC:
377 ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
378 break;
379
380 default:
381 break;
382 }
383}
384
385/**
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700386 * ipath_ib_rcv - process an incoming packet
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800387 * @arg: the device pointer
388 * @rhdr: the header of the packet
389 * @data: the packet data
390 * @tlen: the packet length
391 *
392 * This is called from ipath_kreceive() to process an incoming packet at
393 * interrupt level. Tlen is the length of the header + data + CRC in bytes.
394 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700395void ipath_ib_rcv(struct ipath_ibdev *dev, void *rhdr, void *data,
396 u32 tlen)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800397{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800398 struct ipath_ib_header *hdr = rhdr;
399 struct ipath_other_headers *ohdr;
400 struct ipath_qp *qp;
401 u32 qp_num;
402 int lnh;
403 u8 opcode;
404 u16 lid;
405
406 if (unlikely(dev == NULL))
407 goto bail;
408
409 if (unlikely(tlen < 24)) { /* LRH+BTH+CRC */
410 dev->rcv_errors++;
411 goto bail;
412 }
413
414 /* Check for a valid destination LID (see ch. 7.11.1). */
415 lid = be16_to_cpu(hdr->lrh[1]);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700416 if (lid < IPATH_MULTICAST_LID_BASE) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800417 lid &= ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700418 if (unlikely(lid != dev->dd->ipath_lid)) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800419 dev->rcv_errors++;
420 goto bail;
421 }
422 }
423
424 /* Check for GRH */
425 lnh = be16_to_cpu(hdr->lrh[0]) & 3;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700426 if (lnh == IPATH_LRH_BTH)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800427 ohdr = &hdr->u.oth;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700428 else if (lnh == IPATH_LRH_GRH)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800429 ohdr = &hdr->u.l.oth;
430 else {
431 dev->rcv_errors++;
432 goto bail;
433 }
434
435 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
436 dev->opstats[opcode].n_bytes += tlen;
437 dev->opstats[opcode].n_packets++;
438
439 /* Get the destination QP number. */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700440 qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
441 if (qp_num == IPATH_MULTICAST_QPN) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800442 struct ipath_mcast *mcast;
443 struct ipath_mcast_qp *p;
444
Bryan O'Sullivan5a7d4ee2007-03-15 14:45:03 -0700445 if (lnh != IPATH_LRH_GRH) {
446 dev->n_pkt_drops++;
447 goto bail;
448 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800449 mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
450 if (mcast == NULL) {
451 dev->n_pkt_drops++;
452 goto bail;
453 }
454 dev->n_multicast_rcv++;
455 list_for_each_entry_rcu(p, &mcast->qp_list, list)
Bryan O'Sullivan5a7d4ee2007-03-15 14:45:03 -0700456 ipath_qp_rcv(dev, hdr, 1, data, tlen, p->qp);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800457 /*
458 * Notify ipath_multicast_detach() if it is waiting for us
459 * to finish.
460 */
461 if (atomic_dec_return(&mcast->refcount) <= 1)
462 wake_up(&mcast->wait);
463 } else {
464 qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
465 if (qp) {
466 dev->n_unicast_rcv++;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700467 ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800468 tlen, qp);
469 /*
470 * Notify ipath_destroy_qp() if it is waiting
471 * for us to finish.
472 */
473 if (atomic_dec_and_test(&qp->refcount))
474 wake_up(&qp->wait);
475 } else
476 dev->n_pkt_drops++;
477 }
478
479bail:;
480}
481
482/**
483 * ipath_ib_timer - verbs timer
484 * @arg: the device pointer
485 *
486 * This is called from ipath_do_rcv_timer() at interrupt level to check for
487 * QPs which need retransmits and to collect performance numbers.
488 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700489void ipath_ib_timer(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800490{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800491 struct ipath_qp *resend = NULL;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800492 struct list_head *last;
493 struct ipath_qp *qp;
494 unsigned long flags;
495
496 if (dev == NULL)
497 return;
498
499 spin_lock_irqsave(&dev->pending_lock, flags);
500 /* Start filling the next pending queue. */
501 if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
502 dev->pending_index = 0;
503 /* Save any requests still in the new queue, they have timed out. */
504 last = &dev->pending[dev->pending_index];
505 while (!list_empty(last)) {
506 qp = list_entry(last->next, struct ipath_qp, timerwait);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700507 list_del_init(&qp->timerwait);
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700508 qp->timer_next = resend;
509 resend = qp;
510 atomic_inc(&qp->refcount);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800511 }
512 last = &dev->rnrwait;
513 if (!list_empty(last)) {
514 qp = list_entry(last->next, struct ipath_qp, timerwait);
515 if (--qp->s_rnr_timeout == 0) {
516 do {
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700517 list_del_init(&qp->timerwait);
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700518 tasklet_hi_schedule(&qp->s_task);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800519 if (list_empty(last))
520 break;
521 qp = list_entry(last->next, struct ipath_qp,
522 timerwait);
523 } while (qp->s_rnr_timeout == 0);
524 }
525 }
526 /*
527 * We should only be in the started state if pma_sample_start != 0
528 */
529 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
530 --dev->pma_sample_start == 0) {
531 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700532 ipath_snapshot_counters(dev->dd, &dev->ipath_sword,
533 &dev->ipath_rword,
534 &dev->ipath_spkts,
535 &dev->ipath_rpkts,
536 &dev->ipath_xmit_wait);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800537 }
538 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
539 if (dev->pma_sample_interval == 0) {
540 u64 ta, tb, tc, td, te;
541
542 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700543 ipath_snapshot_counters(dev->dd, &ta, &tb,
544 &tc, &td, &te);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800545
546 dev->ipath_sword = ta - dev->ipath_sword;
547 dev->ipath_rword = tb - dev->ipath_rword;
548 dev->ipath_spkts = tc - dev->ipath_spkts;
549 dev->ipath_rpkts = td - dev->ipath_rpkts;
550 dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
551 }
552 else
553 dev->pma_sample_interval--;
554 }
555 spin_unlock_irqrestore(&dev->pending_lock, flags);
556
557 /* XXX What if timer fires again while this is running? */
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700558 for (qp = resend; qp != NULL; qp = qp->timer_next) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800559 struct ib_wc wc;
560
561 spin_lock_irqsave(&qp->s_lock, flags);
562 if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
563 dev->n_timeouts++;
564 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
565 }
566 spin_unlock_irqrestore(&qp->s_lock, flags);
567
568 /* Notify ipath_destroy_qp() if it is waiting. */
569 if (atomic_dec_and_test(&qp->refcount))
570 wake_up(&qp->wait);
571 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800572}
573
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700574static void update_sge(struct ipath_sge_state *ss, u32 length)
575{
576 struct ipath_sge *sge = &ss->sge;
577
578 sge->vaddr += length;
579 sge->length -= length;
580 sge->sge_length -= length;
581 if (sge->sge_length == 0) {
582 if (--ss->num_sge)
583 *sge = *ss->sg_list++;
584 } else if (sge->length == 0 && sge->mr != NULL) {
585 if (++sge->n >= IPATH_SEGSZ) {
586 if (++sge->m >= sge->mr->mapsz)
587 return;
588 sge->n = 0;
589 }
590 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
591 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
592 }
593}
594
595#ifdef __LITTLE_ENDIAN
596static inline u32 get_upper_bits(u32 data, u32 shift)
597{
598 return data >> shift;
599}
600
601static inline u32 set_upper_bits(u32 data, u32 shift)
602{
603 return data << shift;
604}
605
606static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
607{
608 data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
609 data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
610 return data;
611}
612#else
613static inline u32 get_upper_bits(u32 data, u32 shift)
614{
615 return data << shift;
616}
617
618static inline u32 set_upper_bits(u32 data, u32 shift)
619{
620 return data >> shift;
621}
622
623static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
624{
625 data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
626 data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
627 return data;
628}
629#endif
630
631static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
632 u32 length)
633{
634 u32 extra = 0;
635 u32 data = 0;
636 u32 last;
637
638 while (1) {
639 u32 len = ss->sge.length;
640 u32 off;
641
642 BUG_ON(len == 0);
643 if (len > length)
644 len = length;
645 if (len > ss->sge.sge_length)
646 len = ss->sge.sge_length;
647 /* If the source address is not aligned, try to align it. */
648 off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
649 if (off) {
650 u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
651 ~(sizeof(u32) - 1));
652 u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
653 u32 y;
654
655 y = sizeof(u32) - off;
656 if (len > y)
657 len = y;
658 if (len + extra >= sizeof(u32)) {
659 data |= set_upper_bits(v, extra *
660 BITS_PER_BYTE);
661 len = sizeof(u32) - extra;
662 if (len == length) {
663 last = data;
664 break;
665 }
666 __raw_writel(data, piobuf);
667 piobuf++;
668 extra = 0;
669 data = 0;
670 } else {
671 /* Clear unused upper bytes */
672 data |= clear_upper_bytes(v, len, extra);
673 if (len == length) {
674 last = data;
675 break;
676 }
677 extra += len;
678 }
679 } else if (extra) {
680 /* Source address is aligned. */
681 u32 *addr = (u32 *) ss->sge.vaddr;
682 int shift = extra * BITS_PER_BYTE;
683 int ushift = 32 - shift;
684 u32 l = len;
685
686 while (l >= sizeof(u32)) {
687 u32 v = *addr;
688
689 data |= set_upper_bits(v, shift);
690 __raw_writel(data, piobuf);
691 data = get_upper_bits(v, ushift);
692 piobuf++;
693 addr++;
694 l -= sizeof(u32);
695 }
696 /*
697 * We still have 'extra' number of bytes leftover.
698 */
699 if (l) {
700 u32 v = *addr;
701
702 if (l + extra >= sizeof(u32)) {
703 data |= set_upper_bits(v, shift);
704 len -= l + extra - sizeof(u32);
705 if (len == length) {
706 last = data;
707 break;
708 }
709 __raw_writel(data, piobuf);
710 piobuf++;
711 extra = 0;
712 data = 0;
713 } else {
714 /* Clear unused upper bytes */
715 data |= clear_upper_bytes(v, l,
716 extra);
717 if (len == length) {
718 last = data;
719 break;
720 }
721 extra += l;
722 }
723 } else if (len == length) {
724 last = data;
725 break;
726 }
727 } else if (len == length) {
728 u32 w;
729
730 /*
731 * Need to round up for the last dword in the
732 * packet.
733 */
734 w = (len + 3) >> 2;
735 __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
736 piobuf += w - 1;
737 last = ((u32 *) ss->sge.vaddr)[w - 1];
738 break;
739 } else {
740 u32 w = len >> 2;
741
742 __iowrite32_copy(piobuf, ss->sge.vaddr, w);
743 piobuf += w;
744
745 extra = len & (sizeof(u32) - 1);
746 if (extra) {
747 u32 v = ((u32 *) ss->sge.vaddr)[w];
748
749 /* Clear unused upper bytes */
750 data = clear_upper_bytes(v, extra, 0);
751 }
752 }
753 update_sge(ss, len);
754 length -= len;
755 }
756 /* Update address before sending packet. */
757 update_sge(ss, length);
758 /* must flush early everything before trigger word */
759 ipath_flush_wc();
760 __raw_writel(last, piobuf);
761 /* be sure trigger word is written */
762 ipath_flush_wc();
763}
764
765/**
766 * ipath_verbs_send - send a packet
767 * @dd: the infinipath device
768 * @hdrwords: the number of words in the header
769 * @hdr: the packet header
770 * @len: the length of the packet in bytes
771 * @ss: the SGE to send
772 */
773int ipath_verbs_send(struct ipath_devdata *dd, u32 hdrwords,
774 u32 *hdr, u32 len, struct ipath_sge_state *ss)
775{
776 u32 __iomem *piobuf;
777 u32 plen;
778 int ret;
779
780 /* +1 is for the qword padding of pbc */
781 plen = hdrwords + ((len + 3) >> 2) + 1;
782 if (unlikely((plen << 2) > dd->ipath_ibmaxlen)) {
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700783 ret = -EINVAL;
784 goto bail;
785 }
786
787 /* Get a PIO buffer to use. */
788 piobuf = ipath_getpiobuf(dd, NULL);
789 if (unlikely(piobuf == NULL)) {
790 ret = -EBUSY;
791 goto bail;
792 }
793
794 /*
795 * Write len to control qword, no flags.
796 * We have to flush after the PBC for correctness on some cpus
797 * or WC buffer can be written out of order.
798 */
799 writeq(plen, piobuf);
800 ipath_flush_wc();
801 piobuf += 2;
802 if (len == 0) {
803 /*
804 * If there is just the header portion, must flush before
805 * writing last word of header for correctness, and after
806 * the last header word (trigger word).
807 */
808 __iowrite32_copy(piobuf, hdr, hdrwords - 1);
809 ipath_flush_wc();
810 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
811 ipath_flush_wc();
812 ret = 0;
813 goto bail;
814 }
815
816 __iowrite32_copy(piobuf, hdr, hdrwords);
817 piobuf += hdrwords;
818
819 /* The common case is aligned and contained in one segment. */
820 if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
821 !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
822 u32 w;
823 u32 *addr = (u32 *) ss->sge.vaddr;
824
825 /* Update address before sending packet. */
826 update_sge(ss, len);
827 /* Need to round up for the last dword in the packet. */
828 w = (len + 3) >> 2;
829 __iowrite32_copy(piobuf, addr, w - 1);
830 /* must flush early everything before trigger word */
831 ipath_flush_wc();
832 __raw_writel(addr[w - 1], piobuf + w - 1);
833 /* be sure trigger word is written */
834 ipath_flush_wc();
835 ret = 0;
836 goto bail;
837 }
838 copy_io(piobuf, ss, len);
839 ret = 0;
840
841bail:
842 return ret;
843}
844
845int ipath_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
846 u64 *rwords, u64 *spkts, u64 *rpkts,
847 u64 *xmit_wait)
848{
849 int ret;
850
851 if (!(dd->ipath_flags & IPATH_INITTED)) {
852 /* no hardware, freeze, etc. */
853 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
854 ret = -EINVAL;
855 goto bail;
856 }
857 *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
858 *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
859 *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
860 *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
861 *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
862
863 ret = 0;
864
865bail:
866 return ret;
867}
868
869/**
870 * ipath_get_counters - get various chip counters
871 * @dd: the infinipath device
872 * @cntrs: counters are placed here
873 *
874 * Return the counters needed by recv_pma_get_portcounters().
875 */
876int ipath_get_counters(struct ipath_devdata *dd,
877 struct ipath_verbs_counters *cntrs)
878{
879 int ret;
880
881 if (!(dd->ipath_flags & IPATH_INITTED)) {
882 /* no hardware, freeze, etc. */
883 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
884 ret = -EINVAL;
885 goto bail;
886 }
887 cntrs->symbol_error_counter =
888 ipath_snap_cntr(dd, dd->ipath_cregs->cr_ibsymbolerrcnt);
889 cntrs->link_error_recovery_counter =
890 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkerrrecovcnt);
891 /*
892 * The link downed counter counts when the other side downs the
893 * connection. We add in the number of times we downed the link
894 * due to local link integrity errors to compensate.
895 */
896 cntrs->link_downed_counter =
897 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkdowncnt);
898 cntrs->port_rcv_errors =
899 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rxdroppktcnt) +
900 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvovflcnt) +
901 ipath_snap_cntr(dd, dd->ipath_cregs->cr_portovflcnt) +
902 ipath_snap_cntr(dd, dd->ipath_cregs->cr_err_rlencnt) +
903 ipath_snap_cntr(dd, dd->ipath_cregs->cr_invalidrlencnt) +
904 ipath_snap_cntr(dd, dd->ipath_cregs->cr_erricrccnt) +
905 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errvcrccnt) +
906 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlpcrccnt) +
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -0700907 ipath_snap_cntr(dd, dd->ipath_cregs->cr_badformatcnt) +
908 dd->ipath_rxfc_unsupvl_errs;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700909 cntrs->port_rcv_remphys_errors =
910 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvebpcnt);
911 cntrs->port_xmit_discards =
912 ipath_snap_cntr(dd, dd->ipath_cregs->cr_unsupvlcnt);
913 cntrs->port_xmit_data =
914 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
915 cntrs->port_rcv_data =
916 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
917 cntrs->port_xmit_packets =
918 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
919 cntrs->port_rcv_packets =
920 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -0700921 cntrs->local_link_integrity_errors =
922 (dd->ipath_flags & IPATH_GPIO_ERRINTRS) ?
923 dd->ipath_lli_errs : dd->ipath_lli_errors;
924 cntrs->excessive_buffer_overrun_errors = dd->ipath_overrun_thresh_errs;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700925
926 ret = 0;
927
928bail:
929 return ret;
930}
931
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800932/**
933 * ipath_ib_piobufavail - callback when a PIO buffer is available
934 * @arg: the device pointer
935 *
936 * This is called from ipath_intr() at interrupt level when a PIO buffer is
937 * available after ipath_verbs_send() returned an error that no buffers were
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700938 * available. Return 1 if we consumed all the PIO buffers and we still have
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800939 * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700940 * return zero).
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800941 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700942int ipath_ib_piobufavail(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800943{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800944 struct ipath_qp *qp;
945 unsigned long flags;
946
947 if (dev == NULL)
948 goto bail;
949
950 spin_lock_irqsave(&dev->pending_lock, flags);
951 while (!list_empty(&dev->piowait)) {
952 qp = list_entry(dev->piowait.next, struct ipath_qp,
953 piowait);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700954 list_del_init(&qp->piowait);
Ralph Campbelldb5518c2007-06-18 14:24:43 -0700955 clear_bit(IPATH_S_BUSY, &qp->s_busy);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800956 tasklet_hi_schedule(&qp->s_task);
957 }
958 spin_unlock_irqrestore(&dev->pending_lock, flags);
959
960bail:
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700961 return 0;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800962}
963
964static int ipath_query_device(struct ib_device *ibdev,
965 struct ib_device_attr *props)
966{
967 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800968
969 memset(props, 0, sizeof(*props));
970
971 props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
972 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
973 IB_DEVICE_SYS_IMAGE_GUID;
Ralph Campbellc9f79bd2006-07-17 18:19:54 -0700974 props->page_size_cap = PAGE_SIZE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700975 props->vendor_id = dev->dd->ipath_vendorid;
976 props->vendor_part_id = dev->dd->ipath_deviceid;
977 props->hw_ver = dev->dd->ipath_pcirev;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800978
979 props->sys_image_guid = dev->sys_image_guid;
980
981 props->max_mr_size = ~0ull;
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700982 props->max_qp = ib_ipath_max_qps;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700983 props->max_qp_wr = ib_ipath_max_qp_wrs;
984 props->max_sge = ib_ipath_max_sges;
985 props->max_cq = ib_ipath_max_cqs;
986 props->max_ah = ib_ipath_max_ahs;
987 props->max_cqe = ib_ipath_max_cqes;
988 props->max_mr = dev->lk_table.max;
Robert Walshfdc72152007-06-18 14:24:36 -0700989 props->max_fmr = dev->lk_table.max;
990 props->max_map_per_fmr = 32767;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700991 props->max_pd = ib_ipath_max_pds;
Ralph Campbell3859e392007-03-15 14:44:51 -0700992 props->max_qp_rd_atom = IPATH_MAX_RDMA_ATOMIC;
993 props->max_qp_init_rd_atom = 255;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800994 /* props->max_res_rd_atom */
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700995 props->max_srq = ib_ipath_max_srqs;
996 props->max_srq_wr = ib_ipath_max_srq_wrs;
997 props->max_srq_sge = ib_ipath_max_srq_sges;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800998 /* props->local_ca_ack_delay */
Ralph Campbell3859e392007-03-15 14:44:51 -0700999 props->atomic_cap = IB_ATOMIC_GLOB;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001000 props->max_pkeys = ipath_get_npkeys(dev->dd);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001001 props->max_mcast_grp = ib_ipath_max_mcast_grps;
1002 props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001003 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1004 props->max_mcast_grp;
1005
1006 return 0;
1007}
1008
1009const u8 ipath_cvt_physportstate[16] = {
1010 [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3,
1011 [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5,
1012 [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2,
1013 [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2,
1014 [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1,
1015 [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1,
1016 [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4,
1017 [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4,
1018 [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4,
1019 [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4,
1020 [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6,
1021 [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6,
1022 [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6,
1023};
1024
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001025u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
1026{
1027 return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
1028}
1029
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001030static int ipath_query_port(struct ib_device *ibdev,
1031 u8 port, struct ib_port_attr *props)
1032{
1033 struct ipath_ibdev *dev = to_idev(ibdev);
1034 enum ib_mtu mtu;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001035 u16 lid = dev->dd->ipath_lid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001036 u64 ibcstat;
1037
1038 memset(props, 0, sizeof(*props));
1039 props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1040 props->lmc = dev->mkeyprot_resv_lmc & 7;
1041 props->sm_lid = dev->sm_lid;
1042 props->sm_sl = dev->sm_sl;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001043 ibcstat = dev->dd->ipath_lastibcstat;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001044 props->state = ((ibcstat >> 4) & 0x3) + 1;
1045 /* See phys_state_show() */
1046 props->phys_state = ipath_cvt_physportstate[
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001047 dev->dd->ipath_lastibcstat & 0xf];
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001048 props->port_cap_flags = dev->port_cap_flags;
1049 props->gid_tbl_len = 1;
Bryan O'Sullivanc100f622006-07-01 04:36:07 -07001050 props->max_msg_sz = 0x80000000;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001051 props->pkey_tbl_len = ipath_get_npkeys(dev->dd);
1052 props->bad_pkey_cntr = ipath_get_cr_errpkey(dev->dd) -
Bryan O'Sullivan443a64a2006-07-01 04:35:48 -07001053 dev->z_pkey_violations;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001054 props->qkey_viol_cntr = dev->qkey_violations;
1055 props->active_width = IB_WIDTH_4X;
1056 /* See rate_show() */
1057 props->active_speed = 1; /* Regular 10Mbs speed. */
1058 props->max_vl_num = 1; /* VLCap = VL0 */
1059 props->init_type_reply = 0;
1060
Robert Walshe7340f02007-06-18 14:24:35 -07001061 /*
1062 * Note: the chips support a maximum MTU of 4096, but the driver
1063 * hasn't implemented this feature yet, so set the maximum value
1064 * to 2048.
1065 */
1066 props->max_mtu = IB_MTU_2048;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001067 switch (dev->dd->ipath_ibmtu) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001068 case 4096:
1069 mtu = IB_MTU_4096;
1070 break;
1071 case 2048:
1072 mtu = IB_MTU_2048;
1073 break;
1074 case 1024:
1075 mtu = IB_MTU_1024;
1076 break;
1077 case 512:
1078 mtu = IB_MTU_512;
1079 break;
1080 case 256:
1081 mtu = IB_MTU_256;
1082 break;
1083 default:
1084 mtu = IB_MTU_2048;
1085 }
1086 props->active_mtu = mtu;
1087 props->subnet_timeout = dev->subnet_timeout;
1088
1089 return 0;
1090}
1091
1092static int ipath_modify_device(struct ib_device *device,
1093 int device_modify_mask,
1094 struct ib_device_modify *device_modify)
1095{
1096 int ret;
1097
1098 if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1099 IB_DEVICE_MODIFY_NODE_DESC)) {
1100 ret = -EOPNOTSUPP;
1101 goto bail;
1102 }
1103
1104 if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
1105 memcpy(device->node_desc, device_modify->node_desc, 64);
1106
1107 if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
1108 to_idev(device)->sys_image_guid =
1109 cpu_to_be64(device_modify->sys_image_guid);
1110
1111 ret = 0;
1112
1113bail:
1114 return ret;
1115}
1116
1117static int ipath_modify_port(struct ib_device *ibdev,
1118 u8 port, int port_modify_mask,
1119 struct ib_port_modify *props)
1120{
1121 struct ipath_ibdev *dev = to_idev(ibdev);
1122
1123 dev->port_cap_flags |= props->set_port_cap_mask;
1124 dev->port_cap_flags &= ~props->clr_port_cap_mask;
1125 if (port_modify_mask & IB_PORT_SHUTDOWN)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001126 ipath_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001127 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1128 dev->qkey_violations = 0;
1129 return 0;
1130}
1131
1132static int ipath_query_gid(struct ib_device *ibdev, u8 port,
1133 int index, union ib_gid *gid)
1134{
1135 struct ipath_ibdev *dev = to_idev(ibdev);
1136 int ret;
1137
1138 if (index >= 1) {
1139 ret = -EINVAL;
1140 goto bail;
1141 }
1142 gid->global.subnet_prefix = dev->gid_prefix;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001143 gid->global.interface_id = dev->dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001144
1145 ret = 0;
1146
1147bail:
1148 return ret;
1149}
1150
1151static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
1152 struct ib_ucontext *context,
1153 struct ib_udata *udata)
1154{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001155 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001156 struct ipath_pd *pd;
1157 struct ib_pd *ret;
1158
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001159 /*
1160 * This is actually totally arbitrary. Some correctness tests
1161 * assume there's a maximum number of PDs that can be allocated.
1162 * We don't actually have this limit, but we fail the test if
1163 * we allow allocations of more than we report for this value.
1164 */
1165
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001166 pd = kmalloc(sizeof *pd, GFP_KERNEL);
1167 if (!pd) {
1168 ret = ERR_PTR(-ENOMEM);
1169 goto bail;
1170 }
1171
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001172 spin_lock(&dev->n_pds_lock);
1173 if (dev->n_pds_allocated == ib_ipath_max_pds) {
1174 spin_unlock(&dev->n_pds_lock);
1175 kfree(pd);
1176 ret = ERR_PTR(-ENOMEM);
1177 goto bail;
1178 }
1179
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001180 dev->n_pds_allocated++;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001181 spin_unlock(&dev->n_pds_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001182
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001183 /* ib_alloc_pd() will initialize pd->ibpd. */
1184 pd->user = udata != NULL;
1185
1186 ret = &pd->ibpd;
1187
1188bail:
1189 return ret;
1190}
1191
1192static int ipath_dealloc_pd(struct ib_pd *ibpd)
1193{
1194 struct ipath_pd *pd = to_ipd(ibpd);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001195 struct ipath_ibdev *dev = to_idev(ibpd->device);
1196
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001197 spin_lock(&dev->n_pds_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001198 dev->n_pds_allocated--;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001199 spin_unlock(&dev->n_pds_lock);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001200
1201 kfree(pd);
1202
1203 return 0;
1204}
1205
1206/**
1207 * ipath_create_ah - create an address handle
1208 * @pd: the protection domain
1209 * @ah_attr: the attributes of the AH
1210 *
1211 * This may be called from interrupt context.
1212 */
1213static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
1214 struct ib_ah_attr *ah_attr)
1215{
1216 struct ipath_ah *ah;
1217 struct ib_ah *ret;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001218 struct ipath_ibdev *dev = to_idev(pd->device);
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001219 unsigned long flags;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001220
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001221 /* A multicast address requires a GRH (see ch. 8.4.1). */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001222 if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
1223 ah_attr->dlid != IPATH_PERMISSIVE_LID &&
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001224 !(ah_attr->ah_flags & IB_AH_GRH)) {
1225 ret = ERR_PTR(-EINVAL);
1226 goto bail;
1227 }
1228
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -07001229 if (ah_attr->dlid == 0) {
1230 ret = ERR_PTR(-EINVAL);
1231 goto bail;
1232 }
1233
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001234 if (ah_attr->port_num < 1 ||
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -07001235 ah_attr->port_num > pd->device->phys_port_cnt) {
1236 ret = ERR_PTR(-EINVAL);
1237 goto bail;
1238 }
1239
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001240 ah = kmalloc(sizeof *ah, GFP_ATOMIC);
1241 if (!ah) {
1242 ret = ERR_PTR(-ENOMEM);
1243 goto bail;
1244 }
1245
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001246 spin_lock_irqsave(&dev->n_ahs_lock, flags);
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001247 if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001248 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001249 kfree(ah);
1250 ret = ERR_PTR(-ENOMEM);
1251 goto bail;
1252 }
1253
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001254 dev->n_ahs_allocated++;
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001255 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001256
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001257 /* ib_create_ah() will initialize ah->ibah. */
1258 ah->attr = *ah_attr;
1259
1260 ret = &ah->ibah;
1261
1262bail:
1263 return ret;
1264}
1265
1266/**
1267 * ipath_destroy_ah - destroy an address handle
1268 * @ibah: the AH to destroy
1269 *
1270 * This may be called from interrupt context.
1271 */
1272static int ipath_destroy_ah(struct ib_ah *ibah)
1273{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001274 struct ipath_ibdev *dev = to_idev(ibah->device);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001275 struct ipath_ah *ah = to_iah(ibah);
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001276 unsigned long flags;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001277
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001278 spin_lock_irqsave(&dev->n_ahs_lock, flags);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001279 dev->n_ahs_allocated--;
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001280 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001281
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001282 kfree(ah);
1283
1284 return 0;
1285}
1286
1287static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1288{
1289 struct ipath_ah *ah = to_iah(ibah);
1290
1291 *ah_attr = ah->attr;
1292
1293 return 0;
1294}
1295
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001296/**
1297 * ipath_get_npkeys - return the size of the PKEY table for port 0
1298 * @dd: the infinipath device
1299 */
1300unsigned ipath_get_npkeys(struct ipath_devdata *dd)
1301{
1302 return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
1303}
1304
1305/**
1306 * ipath_get_pkey - return the indexed PKEY from the port 0 PKEY table
1307 * @dd: the infinipath device
1308 * @index: the PKEY index
1309 */
1310unsigned ipath_get_pkey(struct ipath_devdata *dd, unsigned index)
1311{
1312 unsigned ret;
1313
1314 if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
1315 ret = 0;
1316 else
1317 ret = dd->ipath_pd[0]->port_pkeys[index];
1318
1319 return ret;
1320}
1321
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001322static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1323 u16 *pkey)
1324{
1325 struct ipath_ibdev *dev = to_idev(ibdev);
1326 int ret;
1327
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001328 if (index >= ipath_get_npkeys(dev->dd)) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001329 ret = -EINVAL;
1330 goto bail;
1331 }
1332
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001333 *pkey = ipath_get_pkey(dev->dd, index);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001334 ret = 0;
1335
1336bail:
1337 return ret;
1338}
1339
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001340/**
1341 * ipath_alloc_ucontext - allocate a ucontest
1342 * @ibdev: the infiniband device
1343 * @udata: not used by the InfiniPath driver
1344 */
1345
1346static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
1347 struct ib_udata *udata)
1348{
1349 struct ipath_ucontext *context;
1350 struct ib_ucontext *ret;
1351
1352 context = kmalloc(sizeof *context, GFP_KERNEL);
1353 if (!context) {
1354 ret = ERR_PTR(-ENOMEM);
1355 goto bail;
1356 }
1357
1358 ret = &context->ibucontext;
1359
1360bail:
1361 return ret;
1362}
1363
1364static int ipath_dealloc_ucontext(struct ib_ucontext *context)
1365{
1366 kfree(to_iucontext(context));
1367 return 0;
1368}
1369
1370static int ipath_verbs_register_sysfs(struct ib_device *dev);
1371
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001372static void __verbs_timer(unsigned long arg)
1373{
1374 struct ipath_devdata *dd = (struct ipath_devdata *) arg;
1375
1376 /*
1377 * If port 0 receive packet interrupts are not available, or
1378 * can be missed, poll the receive queue
1379 */
1380 if (dd->ipath_flags & IPATH_POLL_RX_INTR)
1381 ipath_kreceive(dd);
1382
1383 /* Handle verbs layer timeouts. */
1384 ipath_ib_timer(dd->verbs_dev);
1385
1386 mod_timer(&dd->verbs_timer, jiffies + 1);
1387}
1388
1389static int enable_timer(struct ipath_devdata *dd)
1390{
1391 /*
1392 * Early chips had a design flaw where the chip and kernel idea
1393 * of the tail register don't always agree, and therefore we won't
1394 * get an interrupt on the next packet received.
1395 * If the board supports per packet receive interrupts, use it.
1396 * Otherwise, the timer function periodically checks for packets
1397 * to cover this case.
1398 * Either way, the timer is needed for verbs layer related
1399 * processing.
1400 */
1401 if (dd->ipath_flags & IPATH_GPIO_INTR) {
1402 ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
1403 0x2074076542310ULL);
1404 /* Enable GPIO bit 2 interrupt */
Arthur Jones8f140b42007-05-10 12:10:49 -07001405 dd->ipath_gpio_mask |= (u64) (1 << IPATH_GPIO_PORT0_BIT);
1406 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1407 dd->ipath_gpio_mask);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001408 }
1409
1410 init_timer(&dd->verbs_timer);
1411 dd->verbs_timer.function = __verbs_timer;
1412 dd->verbs_timer.data = (unsigned long)dd;
1413 dd->verbs_timer.expires = jiffies + 1;
1414 add_timer(&dd->verbs_timer);
1415
1416 return 0;
1417}
1418
1419static int disable_timer(struct ipath_devdata *dd)
1420{
1421 /* Disable GPIO bit 2 interrupt */
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -07001422 if (dd->ipath_flags & IPATH_GPIO_INTR) {
1423 u64 val;
1424 /* Disable GPIO bit 2 interrupt */
1425 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_mask);
Arthur Jones8f140b42007-05-10 12:10:49 -07001426 dd->ipath_gpio_mask &= ~((u64) (1 << IPATH_GPIO_PORT0_BIT));
1427 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1428 dd->ipath_gpio_mask);
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -07001429 /*
1430 * We might want to undo changes to debugportselect,
1431 * but how?
1432 */
1433 }
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001434
1435 del_timer_sync(&dd->verbs_timer);
1436
1437 return 0;
1438}
1439
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001440/**
1441 * ipath_register_ib_device - register our device with the infiniband core
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001442 * @dd: the device data structure
1443 * Return the allocated ipath_ibdev pointer or NULL on error.
1444 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001445int ipath_register_ib_device(struct ipath_devdata *dd)
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001446{
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001447 struct ipath_verbs_counters cntrs;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001448 struct ipath_ibdev *idev;
1449 struct ib_device *dev;
1450 int ret;
1451
1452 idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001453 if (idev == NULL) {
1454 ret = -ENOMEM;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001455 goto bail;
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001456 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001457
1458 dev = &idev->ibdev;
1459
1460 /* Only need to initialize non-zero fields. */
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001461 spin_lock_init(&idev->n_pds_lock);
1462 spin_lock_init(&idev->n_ahs_lock);
1463 spin_lock_init(&idev->n_cqs_lock);
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -07001464 spin_lock_init(&idev->n_qps_lock);
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001465 spin_lock_init(&idev->n_srqs_lock);
1466 spin_lock_init(&idev->n_mcast_grps_lock);
1467
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001468 spin_lock_init(&idev->qp_table.lock);
1469 spin_lock_init(&idev->lk_table.lock);
1470 idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1471 /* Set the prefix to the default value (see ch. 4.1.1) */
1472 idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
1473
1474 ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
1475 if (ret)
1476 goto err_qp;
1477
1478 /*
1479 * The top ib_ipath_lkey_table_size bits are used to index the
1480 * table. The lower 8 bits can be owned by the user (copied from
1481 * the LKEY). The remaining bits act as a generation number or tag.
1482 */
1483 idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
1484 idev->lk_table.table = kzalloc(idev->lk_table.max *
1485 sizeof(*idev->lk_table.table),
1486 GFP_KERNEL);
1487 if (idev->lk_table.table == NULL) {
1488 ret = -ENOMEM;
1489 goto err_lk;
1490 }
Robert Walsh6b66b2d2007-04-27 21:07:23 -07001491 INIT_LIST_HEAD(&idev->pending_mmaps);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001492 spin_lock_init(&idev->pending_lock);
Robert Walsh6b66b2d2007-04-27 21:07:23 -07001493 idev->mmap_offset = PAGE_SIZE;
1494 spin_lock_init(&idev->mmap_offset_lock);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001495 INIT_LIST_HEAD(&idev->pending[0]);
1496 INIT_LIST_HEAD(&idev->pending[1]);
1497 INIT_LIST_HEAD(&idev->pending[2]);
1498 INIT_LIST_HEAD(&idev->piowait);
1499 INIT_LIST_HEAD(&idev->rnrwait);
1500 idev->pending_index = 0;
1501 idev->port_cap_flags =
1502 IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
1503 idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1504 idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1505 idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1506 idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
Roland Dreier53533e12006-11-29 15:33:07 -08001507 idev->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001508 idev->link_width_enabled = 3; /* 1x or 4x */
1509
Bryan O'Sullivanfba75202006-07-01 04:36:09 -07001510 /* Snapshot current HW counters to "clear" them. */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001511 ipath_get_counters(dd, &cntrs);
Bryan O'Sullivanfba75202006-07-01 04:36:09 -07001512 idev->z_symbol_error_counter = cntrs.symbol_error_counter;
1513 idev->z_link_error_recovery_counter =
1514 cntrs.link_error_recovery_counter;
1515 idev->z_link_downed_counter = cntrs.link_downed_counter;
1516 idev->z_port_rcv_errors = cntrs.port_rcv_errors;
1517 idev->z_port_rcv_remphys_errors =
1518 cntrs.port_rcv_remphys_errors;
1519 idev->z_port_xmit_discards = cntrs.port_xmit_discards;
1520 idev->z_port_xmit_data = cntrs.port_xmit_data;
1521 idev->z_port_rcv_data = cntrs.port_rcv_data;
1522 idev->z_port_xmit_packets = cntrs.port_xmit_packets;
1523 idev->z_port_rcv_packets = cntrs.port_rcv_packets;
1524 idev->z_local_link_integrity_errors =
1525 cntrs.local_link_integrity_errors;
1526 idev->z_excessive_buffer_overrun_errors =
1527 cntrs.excessive_buffer_overrun_errors;
1528
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001529 /*
1530 * The system image GUID is supposed to be the same for all
1531 * IB HCAs in a single system but since there can be other
1532 * device types in the system, we can't be sure this is unique.
1533 */
1534 if (!sys_image_guid)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001535 sys_image_guid = dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001536 idev->sys_image_guid = sys_image_guid;
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001537 idev->ib_unit = dd->ipath_unit;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001538 idev->dd = dd;
1539
1540 strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
Bryan O'Sullivan41c75a12006-05-23 11:32:36 -07001541 dev->owner = THIS_MODULE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001542 dev->node_guid = dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001543 dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
1544 dev->uverbs_cmd_mask =
1545 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1546 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1547 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1548 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1549 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1550 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
1551 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
1552 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
1553 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1554 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1555 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1556 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
1557 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
1558 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1559 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
1560 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
1561 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1562 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
1563 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
1564 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1565 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
1566 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
1567 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1568 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1569 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1570 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
1571 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
1572 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
1573 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001574 dev->node_type = RDMA_NODE_IB_CA;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001575 dev->phys_port_cnt = 1;
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +03001576 dev->num_comp_vectors = 1;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001577 dev->dma_device = &dd->pcidev->dev;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001578 dev->query_device = ipath_query_device;
1579 dev->modify_device = ipath_modify_device;
1580 dev->query_port = ipath_query_port;
1581 dev->modify_port = ipath_modify_port;
1582 dev->query_pkey = ipath_query_pkey;
1583 dev->query_gid = ipath_query_gid;
1584 dev->alloc_ucontext = ipath_alloc_ucontext;
1585 dev->dealloc_ucontext = ipath_dealloc_ucontext;
1586 dev->alloc_pd = ipath_alloc_pd;
1587 dev->dealloc_pd = ipath_dealloc_pd;
1588 dev->create_ah = ipath_create_ah;
1589 dev->destroy_ah = ipath_destroy_ah;
1590 dev->query_ah = ipath_query_ah;
1591 dev->create_srq = ipath_create_srq;
1592 dev->modify_srq = ipath_modify_srq;
1593 dev->query_srq = ipath_query_srq;
1594 dev->destroy_srq = ipath_destroy_srq;
1595 dev->create_qp = ipath_create_qp;
1596 dev->modify_qp = ipath_modify_qp;
1597 dev->query_qp = ipath_query_qp;
1598 dev->destroy_qp = ipath_destroy_qp;
1599 dev->post_send = ipath_post_send;
1600 dev->post_recv = ipath_post_receive;
1601 dev->post_srq_recv = ipath_post_srq_receive;
1602 dev->create_cq = ipath_create_cq;
1603 dev->destroy_cq = ipath_destroy_cq;
1604 dev->resize_cq = ipath_resize_cq;
1605 dev->poll_cq = ipath_poll_cq;
1606 dev->req_notify_cq = ipath_req_notify_cq;
1607 dev->get_dma_mr = ipath_get_dma_mr;
1608 dev->reg_phys_mr = ipath_reg_phys_mr;
1609 dev->reg_user_mr = ipath_reg_user_mr;
1610 dev->dereg_mr = ipath_dereg_mr;
1611 dev->alloc_fmr = ipath_alloc_fmr;
1612 dev->map_phys_fmr = ipath_map_phys_fmr;
1613 dev->unmap_fmr = ipath_unmap_fmr;
1614 dev->dealloc_fmr = ipath_dealloc_fmr;
1615 dev->attach_mcast = ipath_multicast_attach;
1616 dev->detach_mcast = ipath_multicast_detach;
1617 dev->process_mad = ipath_process_mad;
Ralph Campbell373d9912006-09-22 15:22:26 -07001618 dev->mmap = ipath_mmap;
Ralph Campbellf2cbb662006-12-12 14:28:28 -08001619 dev->dma_ops = &ipath_dma_mapping_ops;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001620
1621 snprintf(dev->node_desc, sizeof(dev->node_desc),
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001622 IPATH_IDSTR " %s", init_utsname()->nodename);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001623
1624 ret = ib_register_device(dev);
1625 if (ret)
1626 goto err_reg;
1627
1628 if (ipath_verbs_register_sysfs(dev))
1629 goto err_class;
1630
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001631 enable_timer(dd);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001632
1633 goto bail;
1634
1635err_class:
1636 ib_unregister_device(dev);
1637err_reg:
1638 kfree(idev->lk_table.table);
1639err_lk:
1640 kfree(idev->qp_table.table);
1641err_qp:
1642 ib_dealloc_device(dev);
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001643 ipath_dev_err(dd, "cannot register verbs: %d!\n", -ret);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001644 idev = NULL;
1645
1646bail:
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001647 dd->verbs_dev = idev;
1648 return ret;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001649}
1650
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001651void ipath_unregister_ib_device(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001652{
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001653 struct ib_device *ibdev = &dev->ibdev;
1654
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001655 disable_timer(dev->dd);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001656
1657 ib_unregister_device(ibdev);
1658
1659 if (!list_empty(&dev->pending[0]) ||
1660 !list_empty(&dev->pending[1]) ||
1661 !list_empty(&dev->pending[2]))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001662 ipath_dev_err(dev->dd, "pending list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001663 if (!list_empty(&dev->piowait))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001664 ipath_dev_err(dev->dd, "piowait list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001665 if (!list_empty(&dev->rnrwait))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001666 ipath_dev_err(dev->dd, "rnrwait list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001667 if (!ipath_mcast_tree_empty())
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001668 ipath_dev_err(dev->dd, "multicast table memory leak!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001669 /*
1670 * Note that ipath_unregister_ib_device() can be called before all
1671 * the QPs are destroyed!
1672 */
1673 ipath_free_all_qps(&dev->qp_table);
1674 kfree(dev->qp_table.table);
1675 kfree(dev->lk_table.table);
1676 ib_dealloc_device(ibdev);
1677}
1678
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001679static ssize_t show_rev(struct class_device *cdev, char *buf)
1680{
Roland Dreier5494c222006-04-19 11:40:12 -07001681 struct ipath_ibdev *dev =
1682 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001683
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001684 return sprintf(buf, "%x\n", dev->dd->ipath_pcirev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001685}
1686
1687static ssize_t show_hca(struct class_device *cdev, char *buf)
1688{
Roland Dreier5494c222006-04-19 11:40:12 -07001689 struct ipath_ibdev *dev =
1690 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1691 int ret;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001692
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001693 ret = dev->dd->ipath_f_get_boardname(dev->dd, buf, 128);
Roland Dreier5494c222006-04-19 11:40:12 -07001694 if (ret < 0)
1695 goto bail;
1696 strcat(buf, "\n");
1697 ret = strlen(buf);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001698
1699bail:
1700 return ret;
1701}
1702
1703static ssize_t show_stats(struct class_device *cdev, char *buf)
1704{
Roland Dreier5494c222006-04-19 11:40:12 -07001705 struct ipath_ibdev *dev =
1706 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1707 int i;
1708 int len;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001709
Roland Dreier5494c222006-04-19 11:40:12 -07001710 len = sprintf(buf,
1711 "RC resends %d\n"
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -07001712 "RC no QACK %d\n"
Roland Dreier5494c222006-04-19 11:40:12 -07001713 "RC ACKs %d\n"
1714 "RC SEQ NAKs %d\n"
1715 "RC RDMA seq %d\n"
1716 "RC RNR NAKs %d\n"
1717 "RC OTH NAKs %d\n"
1718 "RC timeouts %d\n"
1719 "RC RDMA dup %d\n"
Bryan O'Sullivan60229432006-09-28 08:59:57 -07001720 "RC stalls %d\n"
Roland Dreier5494c222006-04-19 11:40:12 -07001721 "piobuf wait %d\n"
1722 "no piobuf %d\n"
1723 "PKT drops %d\n"
1724 "WQE errs %d\n",
1725 dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
1726 dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
1727 dev->n_other_naks, dev->n_timeouts,
Bryan O'Sullivan60229432006-09-28 08:59:57 -07001728 dev->n_rdma_dup_busy, dev->n_rc_stalls, dev->n_piowait,
Roland Dreier5494c222006-04-19 11:40:12 -07001729 dev->n_no_piobuf, dev->n_pkt_drops, dev->n_wqe_errs);
1730 for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001731 const struct ipath_opcode_stats *si = &dev->opstats[i];
1732
Roland Dreier5494c222006-04-19 11:40:12 -07001733 if (!si->n_packets && !si->n_bytes)
1734 continue;
1735 len += sprintf(buf + len, "%02x %llu/%llu\n", i,
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001736 (unsigned long long) si->n_packets,
Roland Dreier5494c222006-04-19 11:40:12 -07001737 (unsigned long long) si->n_bytes);
1738 }
1739 return len;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001740}
1741
1742static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1743static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1744static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
1745static CLASS_DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
1746
1747static struct class_device_attribute *ipath_class_attributes[] = {
Roland Dreier5494c222006-04-19 11:40:12 -07001748 &class_device_attr_hw_rev,
1749 &class_device_attr_hca_type,
1750 &class_device_attr_board_id,
1751 &class_device_attr_stats
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001752};
1753
1754static int ipath_verbs_register_sysfs(struct ib_device *dev)
1755{
Roland Dreier5494c222006-04-19 11:40:12 -07001756 int i;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001757 int ret;
1758
Roland Dreier5494c222006-04-19 11:40:12 -07001759 for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
1760 if (class_device_create_file(&dev->class_dev,
1761 ipath_class_attributes[i])) {
1762 ret = 1;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001763 goto bail;
1764 }
1765
Roland Dreier5494c222006-04-19 11:40:12 -07001766 ret = 0;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001767
1768bail:
1769 return ret;
1770}