blob: bb70845279b8148aba4f2dcf4a769b9429efda48 [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
167 BUG_ON(len == 0);
168 if (len > length)
169 len = length;
170 memcpy(sge->vaddr, data, len);
171 sge->vaddr += len;
172 sge->length -= len;
173 sge->sge_length -= len;
174 if (sge->sge_length == 0) {
175 if (--ss->num_sge)
176 *sge = *ss->sg_list++;
177 } else if (sge->length == 0 && sge->mr != NULL) {
178 if (++sge->n >= IPATH_SEGSZ) {
179 if (++sge->m >= sge->mr->mapsz)
180 break;
181 sge->n = 0;
182 }
183 sge->vaddr =
184 sge->mr->map[sge->m]->segs[sge->n].vaddr;
185 sge->length =
186 sge->mr->map[sge->m]->segs[sge->n].length;
187 }
188 data += len;
189 length -= len;
190 }
191}
192
193/**
194 * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
195 * @ss: the SGE state
196 * @length: the number of bytes to skip
197 */
198void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
199{
200 struct ipath_sge *sge = &ss->sge;
201
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800202 while (length) {
203 u32 len = sge->length;
204
205 BUG_ON(len == 0);
206 if (len > length)
207 len = length;
208 sge->vaddr += len;
209 sge->length -= len;
210 sge->sge_length -= len;
211 if (sge->sge_length == 0) {
212 if (--ss->num_sge)
213 *sge = *ss->sg_list++;
214 } else if (sge->length == 0 && sge->mr != NULL) {
215 if (++sge->n >= IPATH_SEGSZ) {
216 if (++sge->m >= sge->mr->mapsz)
217 break;
218 sge->n = 0;
219 }
220 sge->vaddr =
221 sge->mr->map[sge->m]->segs[sge->n].vaddr;
222 sge->length =
223 sge->mr->map[sge->m]->segs[sge->n].length;
224 }
225 length -= len;
226 }
227}
228
229/**
230 * ipath_post_send - post a send on a QP
231 * @ibqp: the QP to post the send on
232 * @wr: the list of work requests to post
233 * @bad_wr: the first bad WR is put here
234 *
235 * This may be called from interrupt context.
236 */
237static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
238 struct ib_send_wr **bad_wr)
239{
240 struct ipath_qp *qp = to_iqp(ibqp);
241 int err = 0;
242
243 /* Check that state is OK to post send. */
244 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK)) {
245 *bad_wr = wr;
246 err = -EINVAL;
247 goto bail;
248 }
249
250 for (; wr; wr = wr->next) {
251 switch (qp->ibqp.qp_type) {
252 case IB_QPT_UC:
253 case IB_QPT_RC:
Bryan O'Sullivanddd4bb22006-07-01 04:35:50 -0700254 err = ipath_post_ruc_send(qp, wr);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800255 break;
256
257 case IB_QPT_SMI:
258 case IB_QPT_GSI:
259 case IB_QPT_UD:
260 err = ipath_post_ud_send(qp, wr);
261 break;
262
263 default:
264 err = -EINVAL;
265 }
266 if (err) {
267 *bad_wr = wr;
268 break;
269 }
270 }
271
272bail:
273 return err;
274}
275
276/**
277 * ipath_post_receive - post a receive on a QP
278 * @ibqp: the QP to post the receive on
279 * @wr: the WR to post
280 * @bad_wr: the first bad WR is put here
281 *
282 * This may be called from interrupt context.
283 */
284static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
285 struct ib_recv_wr **bad_wr)
286{
287 struct ipath_qp *qp = to_iqp(ibqp);
Ralph Campbell373d9912006-09-22 15:22:26 -0700288 struct ipath_rwq *wq = qp->r_rq.wq;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800289 unsigned long flags;
290 int ret;
291
292 /* Check that state is OK to post receive. */
Ralph Campbell373d9912006-09-22 15:22:26 -0700293 if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK) || !wq) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800294 *bad_wr = wr;
295 ret = -EINVAL;
296 goto bail;
297 }
298
299 for (; wr; wr = wr->next) {
300 struct ipath_rwqe *wqe;
301 u32 next;
Ralph Campbell373d9912006-09-22 15:22:26 -0700302 int i;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800303
Ralph Campbell373d9912006-09-22 15:22:26 -0700304 if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800305 *bad_wr = wr;
306 ret = -ENOMEM;
307 goto bail;
308 }
309
310 spin_lock_irqsave(&qp->r_rq.lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700311 next = wq->head + 1;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800312 if (next >= qp->r_rq.size)
313 next = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700314 if (next == wq->tail) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800315 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
316 *bad_wr = wr;
317 ret = -ENOMEM;
318 goto bail;
319 }
320
Ralph Campbell373d9912006-09-22 15:22:26 -0700321 wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800322 wqe->wr_id = wr->wr_id;
Ralph Campbell373d9912006-09-22 15:22:26 -0700323 wqe->num_sge = wr->num_sge;
324 for (i = 0; i < wr->num_sge; i++)
325 wqe->sg_list[i] = wr->sg_list[i];
326 wq->head = next;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800327 spin_unlock_irqrestore(&qp->r_rq.lock, flags);
328 }
329 ret = 0;
330
331bail:
332 return ret;
333}
334
335/**
336 * ipath_qp_rcv - processing an incoming packet on a QP
337 * @dev: the device the packet came on
338 * @hdr: the packet header
339 * @has_grh: true if the packet has a GRH
340 * @data: the packet data
341 * @tlen: the packet length
342 * @qp: the QP the packet came on
343 *
344 * This is called from ipath_ib_rcv() to process an incoming packet
345 * for the given QP.
346 * Called at interrupt level.
347 */
348static void ipath_qp_rcv(struct ipath_ibdev *dev,
349 struct ipath_ib_header *hdr, int has_grh,
350 void *data, u32 tlen, struct ipath_qp *qp)
351{
352 /* Check for valid receive state. */
353 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
354 dev->n_pkt_drops++;
355 return;
356 }
357
358 switch (qp->ibqp.qp_type) {
359 case IB_QPT_SMI:
360 case IB_QPT_GSI:
Bryan O'Sullivand821f022006-08-25 11:24:45 -0700361 if (ib_ipath_disable_sma)
362 break;
363 /* FALLTHROUGH */
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800364 case IB_QPT_UD:
365 ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
366 break;
367
368 case IB_QPT_RC:
369 ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
370 break;
371
372 case IB_QPT_UC:
373 ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
374 break;
375
376 default:
377 break;
378 }
379}
380
381/**
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700382 * ipath_ib_rcv - process an incoming packet
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800383 * @arg: the device pointer
384 * @rhdr: the header of the packet
385 * @data: the packet data
386 * @tlen: the packet length
387 *
388 * This is called from ipath_kreceive() to process an incoming packet at
389 * interrupt level. Tlen is the length of the header + data + CRC in bytes.
390 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700391void ipath_ib_rcv(struct ipath_ibdev *dev, void *rhdr, void *data,
392 u32 tlen)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800393{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800394 struct ipath_ib_header *hdr = rhdr;
395 struct ipath_other_headers *ohdr;
396 struct ipath_qp *qp;
397 u32 qp_num;
398 int lnh;
399 u8 opcode;
400 u16 lid;
401
402 if (unlikely(dev == NULL))
403 goto bail;
404
405 if (unlikely(tlen < 24)) { /* LRH+BTH+CRC */
406 dev->rcv_errors++;
407 goto bail;
408 }
409
410 /* Check for a valid destination LID (see ch. 7.11.1). */
411 lid = be16_to_cpu(hdr->lrh[1]);
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700412 if (lid < IPATH_MULTICAST_LID_BASE) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800413 lid &= ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700414 if (unlikely(lid != dev->dd->ipath_lid)) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800415 dev->rcv_errors++;
416 goto bail;
417 }
418 }
419
420 /* Check for GRH */
421 lnh = be16_to_cpu(hdr->lrh[0]) & 3;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700422 if (lnh == IPATH_LRH_BTH)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800423 ohdr = &hdr->u.oth;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700424 else if (lnh == IPATH_LRH_GRH)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800425 ohdr = &hdr->u.l.oth;
426 else {
427 dev->rcv_errors++;
428 goto bail;
429 }
430
431 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
432 dev->opstats[opcode].n_bytes += tlen;
433 dev->opstats[opcode].n_packets++;
434
435 /* Get the destination QP number. */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700436 qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
437 if (qp_num == IPATH_MULTICAST_QPN) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800438 struct ipath_mcast *mcast;
439 struct ipath_mcast_qp *p;
440
Bryan O'Sullivan5a7d4ee2007-03-15 14:45:03 -0700441 if (lnh != IPATH_LRH_GRH) {
442 dev->n_pkt_drops++;
443 goto bail;
444 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800445 mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
446 if (mcast == NULL) {
447 dev->n_pkt_drops++;
448 goto bail;
449 }
450 dev->n_multicast_rcv++;
451 list_for_each_entry_rcu(p, &mcast->qp_list, list)
Bryan O'Sullivan5a7d4ee2007-03-15 14:45:03 -0700452 ipath_qp_rcv(dev, hdr, 1, data, tlen, p->qp);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800453 /*
454 * Notify ipath_multicast_detach() if it is waiting for us
455 * to finish.
456 */
457 if (atomic_dec_return(&mcast->refcount) <= 1)
458 wake_up(&mcast->wait);
459 } else {
460 qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
461 if (qp) {
462 dev->n_unicast_rcv++;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700463 ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800464 tlen, qp);
465 /*
466 * Notify ipath_destroy_qp() if it is waiting
467 * for us to finish.
468 */
469 if (atomic_dec_and_test(&qp->refcount))
470 wake_up(&qp->wait);
471 } else
472 dev->n_pkt_drops++;
473 }
474
475bail:;
476}
477
478/**
479 * ipath_ib_timer - verbs timer
480 * @arg: the device pointer
481 *
482 * This is called from ipath_do_rcv_timer() at interrupt level to check for
483 * QPs which need retransmits and to collect performance numbers.
484 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700485void ipath_ib_timer(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800486{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800487 struct ipath_qp *resend = NULL;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800488 struct list_head *last;
489 struct ipath_qp *qp;
490 unsigned long flags;
491
492 if (dev == NULL)
493 return;
494
495 spin_lock_irqsave(&dev->pending_lock, flags);
496 /* Start filling the next pending queue. */
497 if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
498 dev->pending_index = 0;
499 /* Save any requests still in the new queue, they have timed out. */
500 last = &dev->pending[dev->pending_index];
501 while (!list_empty(last)) {
502 qp = list_entry(last->next, struct ipath_qp, timerwait);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700503 list_del_init(&qp->timerwait);
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700504 qp->timer_next = resend;
505 resend = qp;
506 atomic_inc(&qp->refcount);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800507 }
508 last = &dev->rnrwait;
509 if (!list_empty(last)) {
510 qp = list_entry(last->next, struct ipath_qp, timerwait);
511 if (--qp->s_rnr_timeout == 0) {
512 do {
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700513 list_del_init(&qp->timerwait);
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700514 tasklet_hi_schedule(&qp->s_task);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800515 if (list_empty(last))
516 break;
517 qp = list_entry(last->next, struct ipath_qp,
518 timerwait);
519 } while (qp->s_rnr_timeout == 0);
520 }
521 }
522 /*
523 * We should only be in the started state if pma_sample_start != 0
524 */
525 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
526 --dev->pma_sample_start == 0) {
527 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700528 ipath_snapshot_counters(dev->dd, &dev->ipath_sword,
529 &dev->ipath_rword,
530 &dev->ipath_spkts,
531 &dev->ipath_rpkts,
532 &dev->ipath_xmit_wait);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800533 }
534 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
535 if (dev->pma_sample_interval == 0) {
536 u64 ta, tb, tc, td, te;
537
538 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700539 ipath_snapshot_counters(dev->dd, &ta, &tb,
540 &tc, &td, &te);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800541
542 dev->ipath_sword = ta - dev->ipath_sword;
543 dev->ipath_rword = tb - dev->ipath_rword;
544 dev->ipath_spkts = tc - dev->ipath_spkts;
545 dev->ipath_rpkts = td - dev->ipath_rpkts;
546 dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
547 }
548 else
549 dev->pma_sample_interval--;
550 }
551 spin_unlock_irqrestore(&dev->pending_lock, flags);
552
553 /* XXX What if timer fires again while this is running? */
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700554 for (qp = resend; qp != NULL; qp = qp->timer_next) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800555 struct ib_wc wc;
556
557 spin_lock_irqsave(&qp->s_lock, flags);
558 if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
559 dev->n_timeouts++;
560 ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
561 }
562 spin_unlock_irqrestore(&qp->s_lock, flags);
563
564 /* Notify ipath_destroy_qp() if it is waiting. */
565 if (atomic_dec_and_test(&qp->refcount))
566 wake_up(&qp->wait);
567 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800568}
569
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700570static void update_sge(struct ipath_sge_state *ss, u32 length)
571{
572 struct ipath_sge *sge = &ss->sge;
573
574 sge->vaddr += length;
575 sge->length -= length;
576 sge->sge_length -= length;
577 if (sge->sge_length == 0) {
578 if (--ss->num_sge)
579 *sge = *ss->sg_list++;
580 } else if (sge->length == 0 && sge->mr != NULL) {
581 if (++sge->n >= IPATH_SEGSZ) {
582 if (++sge->m >= sge->mr->mapsz)
583 return;
584 sge->n = 0;
585 }
586 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
587 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
588 }
589}
590
591#ifdef __LITTLE_ENDIAN
592static inline u32 get_upper_bits(u32 data, u32 shift)
593{
594 return data >> shift;
595}
596
597static inline u32 set_upper_bits(u32 data, u32 shift)
598{
599 return data << shift;
600}
601
602static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
603{
604 data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
605 data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
606 return data;
607}
608#else
609static inline u32 get_upper_bits(u32 data, u32 shift)
610{
611 return data << shift;
612}
613
614static inline u32 set_upper_bits(u32 data, u32 shift)
615{
616 return data >> shift;
617}
618
619static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
620{
621 data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
622 data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
623 return data;
624}
625#endif
626
627static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
628 u32 length)
629{
630 u32 extra = 0;
631 u32 data = 0;
632 u32 last;
633
634 while (1) {
635 u32 len = ss->sge.length;
636 u32 off;
637
638 BUG_ON(len == 0);
639 if (len > length)
640 len = length;
641 if (len > ss->sge.sge_length)
642 len = ss->sge.sge_length;
643 /* If the source address is not aligned, try to align it. */
644 off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
645 if (off) {
646 u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
647 ~(sizeof(u32) - 1));
648 u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
649 u32 y;
650
651 y = sizeof(u32) - off;
652 if (len > y)
653 len = y;
654 if (len + extra >= sizeof(u32)) {
655 data |= set_upper_bits(v, extra *
656 BITS_PER_BYTE);
657 len = sizeof(u32) - extra;
658 if (len == length) {
659 last = data;
660 break;
661 }
662 __raw_writel(data, piobuf);
663 piobuf++;
664 extra = 0;
665 data = 0;
666 } else {
667 /* Clear unused upper bytes */
668 data |= clear_upper_bytes(v, len, extra);
669 if (len == length) {
670 last = data;
671 break;
672 }
673 extra += len;
674 }
675 } else if (extra) {
676 /* Source address is aligned. */
677 u32 *addr = (u32 *) ss->sge.vaddr;
678 int shift = extra * BITS_PER_BYTE;
679 int ushift = 32 - shift;
680 u32 l = len;
681
682 while (l >= sizeof(u32)) {
683 u32 v = *addr;
684
685 data |= set_upper_bits(v, shift);
686 __raw_writel(data, piobuf);
687 data = get_upper_bits(v, ushift);
688 piobuf++;
689 addr++;
690 l -= sizeof(u32);
691 }
692 /*
693 * We still have 'extra' number of bytes leftover.
694 */
695 if (l) {
696 u32 v = *addr;
697
698 if (l + extra >= sizeof(u32)) {
699 data |= set_upper_bits(v, shift);
700 len -= l + extra - sizeof(u32);
701 if (len == length) {
702 last = data;
703 break;
704 }
705 __raw_writel(data, piobuf);
706 piobuf++;
707 extra = 0;
708 data = 0;
709 } else {
710 /* Clear unused upper bytes */
711 data |= clear_upper_bytes(v, l,
712 extra);
713 if (len == length) {
714 last = data;
715 break;
716 }
717 extra += l;
718 }
719 } else if (len == length) {
720 last = data;
721 break;
722 }
723 } else if (len == length) {
724 u32 w;
725
726 /*
727 * Need to round up for the last dword in the
728 * packet.
729 */
730 w = (len + 3) >> 2;
731 __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
732 piobuf += w - 1;
733 last = ((u32 *) ss->sge.vaddr)[w - 1];
734 break;
735 } else {
736 u32 w = len >> 2;
737
738 __iowrite32_copy(piobuf, ss->sge.vaddr, w);
739 piobuf += w;
740
741 extra = len & (sizeof(u32) - 1);
742 if (extra) {
743 u32 v = ((u32 *) ss->sge.vaddr)[w];
744
745 /* Clear unused upper bytes */
746 data = clear_upper_bytes(v, extra, 0);
747 }
748 }
749 update_sge(ss, len);
750 length -= len;
751 }
752 /* Update address before sending packet. */
753 update_sge(ss, length);
754 /* must flush early everything before trigger word */
755 ipath_flush_wc();
756 __raw_writel(last, piobuf);
757 /* be sure trigger word is written */
758 ipath_flush_wc();
759}
760
761/**
762 * ipath_verbs_send - send a packet
763 * @dd: the infinipath device
764 * @hdrwords: the number of words in the header
765 * @hdr: the packet header
766 * @len: the length of the packet in bytes
767 * @ss: the SGE to send
768 */
769int ipath_verbs_send(struct ipath_devdata *dd, u32 hdrwords,
770 u32 *hdr, u32 len, struct ipath_sge_state *ss)
771{
772 u32 __iomem *piobuf;
773 u32 plen;
774 int ret;
775
776 /* +1 is for the qword padding of pbc */
777 plen = hdrwords + ((len + 3) >> 2) + 1;
778 if (unlikely((plen << 2) > dd->ipath_ibmaxlen)) {
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700779 ret = -EINVAL;
780 goto bail;
781 }
782
783 /* Get a PIO buffer to use. */
784 piobuf = ipath_getpiobuf(dd, NULL);
785 if (unlikely(piobuf == NULL)) {
786 ret = -EBUSY;
787 goto bail;
788 }
789
790 /*
791 * Write len to control qword, no flags.
792 * We have to flush after the PBC for correctness on some cpus
793 * or WC buffer can be written out of order.
794 */
795 writeq(plen, piobuf);
796 ipath_flush_wc();
797 piobuf += 2;
798 if (len == 0) {
799 /*
800 * If there is just the header portion, must flush before
801 * writing last word of header for correctness, and after
802 * the last header word (trigger word).
803 */
804 __iowrite32_copy(piobuf, hdr, hdrwords - 1);
805 ipath_flush_wc();
806 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
807 ipath_flush_wc();
808 ret = 0;
809 goto bail;
810 }
811
812 __iowrite32_copy(piobuf, hdr, hdrwords);
813 piobuf += hdrwords;
814
815 /* The common case is aligned and contained in one segment. */
816 if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
817 !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
818 u32 w;
819 u32 *addr = (u32 *) ss->sge.vaddr;
820
821 /* Update address before sending packet. */
822 update_sge(ss, len);
823 /* Need to round up for the last dword in the packet. */
824 w = (len + 3) >> 2;
825 __iowrite32_copy(piobuf, addr, w - 1);
826 /* must flush early everything before trigger word */
827 ipath_flush_wc();
828 __raw_writel(addr[w - 1], piobuf + w - 1);
829 /* be sure trigger word is written */
830 ipath_flush_wc();
831 ret = 0;
832 goto bail;
833 }
834 copy_io(piobuf, ss, len);
835 ret = 0;
836
837bail:
838 return ret;
839}
840
841int ipath_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
842 u64 *rwords, u64 *spkts, u64 *rpkts,
843 u64 *xmit_wait)
844{
845 int ret;
846
847 if (!(dd->ipath_flags & IPATH_INITTED)) {
848 /* no hardware, freeze, etc. */
849 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
850 ret = -EINVAL;
851 goto bail;
852 }
853 *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
854 *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
855 *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
856 *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
857 *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
858
859 ret = 0;
860
861bail:
862 return ret;
863}
864
865/**
866 * ipath_get_counters - get various chip counters
867 * @dd: the infinipath device
868 * @cntrs: counters are placed here
869 *
870 * Return the counters needed by recv_pma_get_portcounters().
871 */
872int ipath_get_counters(struct ipath_devdata *dd,
873 struct ipath_verbs_counters *cntrs)
874{
875 int ret;
876
877 if (!(dd->ipath_flags & IPATH_INITTED)) {
878 /* no hardware, freeze, etc. */
879 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
880 ret = -EINVAL;
881 goto bail;
882 }
883 cntrs->symbol_error_counter =
884 ipath_snap_cntr(dd, dd->ipath_cregs->cr_ibsymbolerrcnt);
885 cntrs->link_error_recovery_counter =
886 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkerrrecovcnt);
887 /*
888 * The link downed counter counts when the other side downs the
889 * connection. We add in the number of times we downed the link
890 * due to local link integrity errors to compensate.
891 */
892 cntrs->link_downed_counter =
893 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkdowncnt);
894 cntrs->port_rcv_errors =
895 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rxdroppktcnt) +
896 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvovflcnt) +
897 ipath_snap_cntr(dd, dd->ipath_cregs->cr_portovflcnt) +
898 ipath_snap_cntr(dd, dd->ipath_cregs->cr_err_rlencnt) +
899 ipath_snap_cntr(dd, dd->ipath_cregs->cr_invalidrlencnt) +
900 ipath_snap_cntr(dd, dd->ipath_cregs->cr_erricrccnt) +
901 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errvcrccnt) +
902 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlpcrccnt) +
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -0700903 ipath_snap_cntr(dd, dd->ipath_cregs->cr_badformatcnt) +
904 dd->ipath_rxfc_unsupvl_errs;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700905 cntrs->port_rcv_remphys_errors =
906 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvebpcnt);
907 cntrs->port_xmit_discards =
908 ipath_snap_cntr(dd, dd->ipath_cregs->cr_unsupvlcnt);
909 cntrs->port_xmit_data =
910 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
911 cntrs->port_rcv_data =
912 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
913 cntrs->port_xmit_packets =
914 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
915 cntrs->port_rcv_packets =
916 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -0700917 cntrs->local_link_integrity_errors =
918 (dd->ipath_flags & IPATH_GPIO_ERRINTRS) ?
919 dd->ipath_lli_errs : dd->ipath_lli_errors;
920 cntrs->excessive_buffer_overrun_errors = dd->ipath_overrun_thresh_errs;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700921
922 ret = 0;
923
924bail:
925 return ret;
926}
927
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800928/**
929 * ipath_ib_piobufavail - callback when a PIO buffer is available
930 * @arg: the device pointer
931 *
932 * This is called from ipath_intr() at interrupt level when a PIO buffer is
933 * available after ipath_verbs_send() returned an error that no buffers were
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700934 * available. Return 1 if we consumed all the PIO buffers and we still have
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800935 * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700936 * return zero).
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800937 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700938int ipath_ib_piobufavail(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800939{
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800940 struct ipath_qp *qp;
941 unsigned long flags;
942
943 if (dev == NULL)
944 goto bail;
945
946 spin_lock_irqsave(&dev->pending_lock, flags);
947 while (!list_empty(&dev->piowait)) {
948 qp = list_entry(dev->piowait.next, struct ipath_qp,
949 piowait);
Bryan O'Sullivan94b8d9f2006-05-23 11:32:32 -0700950 list_del_init(&qp->piowait);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800951 tasklet_hi_schedule(&qp->s_task);
952 }
953 spin_unlock_irqrestore(&dev->pending_lock, flags);
954
955bail:
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -0700956 return 0;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800957}
958
959static int ipath_query_device(struct ib_device *ibdev,
960 struct ib_device_attr *props)
961{
962 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800963
964 memset(props, 0, sizeof(*props));
965
966 props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
967 IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
968 IB_DEVICE_SYS_IMAGE_GUID;
Ralph Campbellc9f79bd2006-07-17 18:19:54 -0700969 props->page_size_cap = PAGE_SIZE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700970 props->vendor_id = dev->dd->ipath_vendorid;
971 props->vendor_part_id = dev->dd->ipath_deviceid;
972 props->hw_ver = dev->dd->ipath_pcirev;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800973
974 props->sys_image_guid = dev->sys_image_guid;
975
976 props->max_mr_size = ~0ull;
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -0700977 props->max_qp = ib_ipath_max_qps;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700978 props->max_qp_wr = ib_ipath_max_qp_wrs;
979 props->max_sge = ib_ipath_max_sges;
980 props->max_cq = ib_ipath_max_cqs;
981 props->max_ah = ib_ipath_max_ahs;
982 props->max_cqe = ib_ipath_max_cqes;
983 props->max_mr = dev->lk_table.max;
984 props->max_pd = ib_ipath_max_pds;
Ralph Campbell3859e392007-03-15 14:44:51 -0700985 props->max_qp_rd_atom = IPATH_MAX_RDMA_ATOMIC;
986 props->max_qp_init_rd_atom = 255;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800987 /* props->max_res_rd_atom */
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700988 props->max_srq = ib_ipath_max_srqs;
989 props->max_srq_wr = ib_ipath_max_srq_wrs;
990 props->max_srq_sge = ib_ipath_max_srq_sges;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800991 /* props->local_ca_ack_delay */
Ralph Campbell3859e392007-03-15 14:44:51 -0700992 props->atomic_cap = IB_ATOMIC_GLOB;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700993 props->max_pkeys = ipath_get_npkeys(dev->dd);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700994 props->max_mcast_grp = ib_ipath_max_mcast_grps;
995 props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
Bryan O'Sullivan65221082006-03-29 15:23:38 -0800996 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
997 props->max_mcast_grp;
998
999 return 0;
1000}
1001
1002const u8 ipath_cvt_physportstate[16] = {
1003 [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3,
1004 [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5,
1005 [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2,
1006 [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2,
1007 [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1,
1008 [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1,
1009 [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4,
1010 [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4,
1011 [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4,
1012 [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4,
1013 [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6,
1014 [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6,
1015 [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6,
1016};
1017
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001018u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
1019{
1020 return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
1021}
1022
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001023static int ipath_query_port(struct ib_device *ibdev,
1024 u8 port, struct ib_port_attr *props)
1025{
1026 struct ipath_ibdev *dev = to_idev(ibdev);
1027 enum ib_mtu mtu;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001028 u16 lid = dev->dd->ipath_lid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001029 u64 ibcstat;
1030
1031 memset(props, 0, sizeof(*props));
1032 props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1033 props->lmc = dev->mkeyprot_resv_lmc & 7;
1034 props->sm_lid = dev->sm_lid;
1035 props->sm_sl = dev->sm_sl;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001036 ibcstat = dev->dd->ipath_lastibcstat;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001037 props->state = ((ibcstat >> 4) & 0x3) + 1;
1038 /* See phys_state_show() */
1039 props->phys_state = ipath_cvt_physportstate[
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001040 dev->dd->ipath_lastibcstat & 0xf];
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001041 props->port_cap_flags = dev->port_cap_flags;
1042 props->gid_tbl_len = 1;
Bryan O'Sullivanc100f622006-07-01 04:36:07 -07001043 props->max_msg_sz = 0x80000000;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001044 props->pkey_tbl_len = ipath_get_npkeys(dev->dd);
1045 props->bad_pkey_cntr = ipath_get_cr_errpkey(dev->dd) -
Bryan O'Sullivan443a64a2006-07-01 04:35:48 -07001046 dev->z_pkey_violations;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001047 props->qkey_viol_cntr = dev->qkey_violations;
1048 props->active_width = IB_WIDTH_4X;
1049 /* See rate_show() */
1050 props->active_speed = 1; /* Regular 10Mbs speed. */
1051 props->max_vl_num = 1; /* VLCap = VL0 */
1052 props->init_type_reply = 0;
1053
1054 props->max_mtu = IB_MTU_4096;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001055 switch (dev->dd->ipath_ibmtu) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001056 case 4096:
1057 mtu = IB_MTU_4096;
1058 break;
1059 case 2048:
1060 mtu = IB_MTU_2048;
1061 break;
1062 case 1024:
1063 mtu = IB_MTU_1024;
1064 break;
1065 case 512:
1066 mtu = IB_MTU_512;
1067 break;
1068 case 256:
1069 mtu = IB_MTU_256;
1070 break;
1071 default:
1072 mtu = IB_MTU_2048;
1073 }
1074 props->active_mtu = mtu;
1075 props->subnet_timeout = dev->subnet_timeout;
1076
1077 return 0;
1078}
1079
1080static int ipath_modify_device(struct ib_device *device,
1081 int device_modify_mask,
1082 struct ib_device_modify *device_modify)
1083{
1084 int ret;
1085
1086 if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1087 IB_DEVICE_MODIFY_NODE_DESC)) {
1088 ret = -EOPNOTSUPP;
1089 goto bail;
1090 }
1091
1092 if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
1093 memcpy(device->node_desc, device_modify->node_desc, 64);
1094
1095 if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
1096 to_idev(device)->sys_image_guid =
1097 cpu_to_be64(device_modify->sys_image_guid);
1098
1099 ret = 0;
1100
1101bail:
1102 return ret;
1103}
1104
1105static int ipath_modify_port(struct ib_device *ibdev,
1106 u8 port, int port_modify_mask,
1107 struct ib_port_modify *props)
1108{
1109 struct ipath_ibdev *dev = to_idev(ibdev);
1110
1111 dev->port_cap_flags |= props->set_port_cap_mask;
1112 dev->port_cap_flags &= ~props->clr_port_cap_mask;
1113 if (port_modify_mask & IB_PORT_SHUTDOWN)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001114 ipath_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001115 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1116 dev->qkey_violations = 0;
1117 return 0;
1118}
1119
1120static int ipath_query_gid(struct ib_device *ibdev, u8 port,
1121 int index, union ib_gid *gid)
1122{
1123 struct ipath_ibdev *dev = to_idev(ibdev);
1124 int ret;
1125
1126 if (index >= 1) {
1127 ret = -EINVAL;
1128 goto bail;
1129 }
1130 gid->global.subnet_prefix = dev->gid_prefix;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001131 gid->global.interface_id = dev->dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001132
1133 ret = 0;
1134
1135bail:
1136 return ret;
1137}
1138
1139static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
1140 struct ib_ucontext *context,
1141 struct ib_udata *udata)
1142{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001143 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001144 struct ipath_pd *pd;
1145 struct ib_pd *ret;
1146
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001147 /*
1148 * This is actually totally arbitrary. Some correctness tests
1149 * assume there's a maximum number of PDs that can be allocated.
1150 * We don't actually have this limit, but we fail the test if
1151 * we allow allocations of more than we report for this value.
1152 */
1153
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001154 pd = kmalloc(sizeof *pd, GFP_KERNEL);
1155 if (!pd) {
1156 ret = ERR_PTR(-ENOMEM);
1157 goto bail;
1158 }
1159
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001160 spin_lock(&dev->n_pds_lock);
1161 if (dev->n_pds_allocated == ib_ipath_max_pds) {
1162 spin_unlock(&dev->n_pds_lock);
1163 kfree(pd);
1164 ret = ERR_PTR(-ENOMEM);
1165 goto bail;
1166 }
1167
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001168 dev->n_pds_allocated++;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001169 spin_unlock(&dev->n_pds_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001170
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001171 /* ib_alloc_pd() will initialize pd->ibpd. */
1172 pd->user = udata != NULL;
1173
1174 ret = &pd->ibpd;
1175
1176bail:
1177 return ret;
1178}
1179
1180static int ipath_dealloc_pd(struct ib_pd *ibpd)
1181{
1182 struct ipath_pd *pd = to_ipd(ibpd);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001183 struct ipath_ibdev *dev = to_idev(ibpd->device);
1184
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001185 spin_lock(&dev->n_pds_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001186 dev->n_pds_allocated--;
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001187 spin_unlock(&dev->n_pds_lock);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001188
1189 kfree(pd);
1190
1191 return 0;
1192}
1193
1194/**
1195 * ipath_create_ah - create an address handle
1196 * @pd: the protection domain
1197 * @ah_attr: the attributes of the AH
1198 *
1199 * This may be called from interrupt context.
1200 */
1201static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
1202 struct ib_ah_attr *ah_attr)
1203{
1204 struct ipath_ah *ah;
1205 struct ib_ah *ret;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001206 struct ipath_ibdev *dev = to_idev(pd->device);
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001207 unsigned long flags;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001208
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001209 /* A multicast address requires a GRH (see ch. 8.4.1). */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -07001210 if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
1211 ah_attr->dlid != IPATH_PERMISSIVE_LID &&
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001212 !(ah_attr->ah_flags & IB_AH_GRH)) {
1213 ret = ERR_PTR(-EINVAL);
1214 goto bail;
1215 }
1216
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -07001217 if (ah_attr->dlid == 0) {
1218 ret = ERR_PTR(-EINVAL);
1219 goto bail;
1220 }
1221
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001222 if (ah_attr->port_num < 1 ||
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -07001223 ah_attr->port_num > pd->device->phys_port_cnt) {
1224 ret = ERR_PTR(-EINVAL);
1225 goto bail;
1226 }
1227
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001228 ah = kmalloc(sizeof *ah, GFP_ATOMIC);
1229 if (!ah) {
1230 ret = ERR_PTR(-ENOMEM);
1231 goto bail;
1232 }
1233
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001234 spin_lock_irqsave(&dev->n_ahs_lock, flags);
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001235 if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001236 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001237 kfree(ah);
1238 ret = ERR_PTR(-ENOMEM);
1239 goto bail;
1240 }
1241
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001242 dev->n_ahs_allocated++;
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001243 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001244
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001245 /* ib_create_ah() will initialize ah->ibah. */
1246 ah->attr = *ah_attr;
1247
1248 ret = &ah->ibah;
1249
1250bail:
1251 return ret;
1252}
1253
1254/**
1255 * ipath_destroy_ah - destroy an address handle
1256 * @ibah: the AH to destroy
1257 *
1258 * This may be called from interrupt context.
1259 */
1260static int ipath_destroy_ah(struct ib_ah *ibah)
1261{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001262 struct ipath_ibdev *dev = to_idev(ibah->device);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001263 struct ipath_ah *ah = to_iah(ibah);
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001264 unsigned long flags;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001265
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001266 spin_lock_irqsave(&dev->n_ahs_lock, flags);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001267 dev->n_ahs_allocated--;
Bryan O'Sullivan3d27b002006-09-28 09:00:24 -07001268 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -07001269
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001270 kfree(ah);
1271
1272 return 0;
1273}
1274
1275static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1276{
1277 struct ipath_ah *ah = to_iah(ibah);
1278
1279 *ah_attr = ah->attr;
1280
1281 return 0;
1282}
1283
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001284/**
1285 * ipath_get_npkeys - return the size of the PKEY table for port 0
1286 * @dd: the infinipath device
1287 */
1288unsigned ipath_get_npkeys(struct ipath_devdata *dd)
1289{
1290 return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
1291}
1292
1293/**
1294 * ipath_get_pkey - return the indexed PKEY from the port 0 PKEY table
1295 * @dd: the infinipath device
1296 * @index: the PKEY index
1297 */
1298unsigned ipath_get_pkey(struct ipath_devdata *dd, unsigned index)
1299{
1300 unsigned ret;
1301
1302 if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
1303 ret = 0;
1304 else
1305 ret = dd->ipath_pd[0]->port_pkeys[index];
1306
1307 return ret;
1308}
1309
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001310static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1311 u16 *pkey)
1312{
1313 struct ipath_ibdev *dev = to_idev(ibdev);
1314 int ret;
1315
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001316 if (index >= ipath_get_npkeys(dev->dd)) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001317 ret = -EINVAL;
1318 goto bail;
1319 }
1320
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001321 *pkey = ipath_get_pkey(dev->dd, index);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001322 ret = 0;
1323
1324bail:
1325 return ret;
1326}
1327
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001328/**
1329 * ipath_alloc_ucontext - allocate a ucontest
1330 * @ibdev: the infiniband device
1331 * @udata: not used by the InfiniPath driver
1332 */
1333
1334static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
1335 struct ib_udata *udata)
1336{
1337 struct ipath_ucontext *context;
1338 struct ib_ucontext *ret;
1339
1340 context = kmalloc(sizeof *context, GFP_KERNEL);
1341 if (!context) {
1342 ret = ERR_PTR(-ENOMEM);
1343 goto bail;
1344 }
1345
1346 ret = &context->ibucontext;
1347
1348bail:
1349 return ret;
1350}
1351
1352static int ipath_dealloc_ucontext(struct ib_ucontext *context)
1353{
1354 kfree(to_iucontext(context));
1355 return 0;
1356}
1357
1358static int ipath_verbs_register_sysfs(struct ib_device *dev);
1359
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001360static void __verbs_timer(unsigned long arg)
1361{
1362 struct ipath_devdata *dd = (struct ipath_devdata *) arg;
1363
1364 /*
1365 * If port 0 receive packet interrupts are not available, or
1366 * can be missed, poll the receive queue
1367 */
1368 if (dd->ipath_flags & IPATH_POLL_RX_INTR)
1369 ipath_kreceive(dd);
1370
1371 /* Handle verbs layer timeouts. */
1372 ipath_ib_timer(dd->verbs_dev);
1373
1374 mod_timer(&dd->verbs_timer, jiffies + 1);
1375}
1376
1377static int enable_timer(struct ipath_devdata *dd)
1378{
1379 /*
1380 * Early chips had a design flaw where the chip and kernel idea
1381 * of the tail register don't always agree, and therefore we won't
1382 * get an interrupt on the next packet received.
1383 * If the board supports per packet receive interrupts, use it.
1384 * Otherwise, the timer function periodically checks for packets
1385 * to cover this case.
1386 * Either way, the timer is needed for verbs layer related
1387 * processing.
1388 */
1389 if (dd->ipath_flags & IPATH_GPIO_INTR) {
1390 ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
1391 0x2074076542310ULL);
1392 /* Enable GPIO bit 2 interrupt */
Arthur Jones8f140b42007-05-10 12:10:49 -07001393 dd->ipath_gpio_mask |= (u64) (1 << IPATH_GPIO_PORT0_BIT);
1394 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1395 dd->ipath_gpio_mask);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001396 }
1397
1398 init_timer(&dd->verbs_timer);
1399 dd->verbs_timer.function = __verbs_timer;
1400 dd->verbs_timer.data = (unsigned long)dd;
1401 dd->verbs_timer.expires = jiffies + 1;
1402 add_timer(&dd->verbs_timer);
1403
1404 return 0;
1405}
1406
1407static int disable_timer(struct ipath_devdata *dd)
1408{
1409 /* Disable GPIO bit 2 interrupt */
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -07001410 if (dd->ipath_flags & IPATH_GPIO_INTR) {
1411 u64 val;
1412 /* Disable GPIO bit 2 interrupt */
1413 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_mask);
Arthur Jones8f140b42007-05-10 12:10:49 -07001414 dd->ipath_gpio_mask &= ~((u64) (1 << IPATH_GPIO_PORT0_BIT));
1415 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1416 dd->ipath_gpio_mask);
Bryan O'Sullivan2c9446a2006-09-28 09:00:00 -07001417 /*
1418 * We might want to undo changes to debugportselect,
1419 * but how?
1420 */
1421 }
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001422
1423 del_timer_sync(&dd->verbs_timer);
1424
1425 return 0;
1426}
1427
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001428/**
1429 * ipath_register_ib_device - register our device with the infiniband core
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001430 * @dd: the device data structure
1431 * Return the allocated ipath_ibdev pointer or NULL on error.
1432 */
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001433int ipath_register_ib_device(struct ipath_devdata *dd)
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001434{
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001435 struct ipath_verbs_counters cntrs;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001436 struct ipath_ibdev *idev;
1437 struct ib_device *dev;
1438 int ret;
1439
1440 idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001441 if (idev == NULL) {
1442 ret = -ENOMEM;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001443 goto bail;
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001444 }
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001445
1446 dev = &idev->ibdev;
1447
1448 /* Only need to initialize non-zero fields. */
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001449 spin_lock_init(&idev->n_pds_lock);
1450 spin_lock_init(&idev->n_ahs_lock);
1451 spin_lock_init(&idev->n_cqs_lock);
Bryan O'Sullivan0b81e4f2006-08-25 11:24:43 -07001452 spin_lock_init(&idev->n_qps_lock);
Bryan O'Sullivanc27fef22006-08-25 11:24:27 -07001453 spin_lock_init(&idev->n_srqs_lock);
1454 spin_lock_init(&idev->n_mcast_grps_lock);
1455
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001456 spin_lock_init(&idev->qp_table.lock);
1457 spin_lock_init(&idev->lk_table.lock);
1458 idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
1459 /* Set the prefix to the default value (see ch. 4.1.1) */
1460 idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
1461
1462 ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
1463 if (ret)
1464 goto err_qp;
1465
1466 /*
1467 * The top ib_ipath_lkey_table_size bits are used to index the
1468 * table. The lower 8 bits can be owned by the user (copied from
1469 * the LKEY). The remaining bits act as a generation number or tag.
1470 */
1471 idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
1472 idev->lk_table.table = kzalloc(idev->lk_table.max *
1473 sizeof(*idev->lk_table.table),
1474 GFP_KERNEL);
1475 if (idev->lk_table.table == NULL) {
1476 ret = -ENOMEM;
1477 goto err_lk;
1478 }
Robert Walsh6b66b2da2007-04-27 21:07:23 -07001479 INIT_LIST_HEAD(&idev->pending_mmaps);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001480 spin_lock_init(&idev->pending_lock);
Robert Walsh6b66b2da2007-04-27 21:07:23 -07001481 idev->mmap_offset = PAGE_SIZE;
1482 spin_lock_init(&idev->mmap_offset_lock);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001483 INIT_LIST_HEAD(&idev->pending[0]);
1484 INIT_LIST_HEAD(&idev->pending[1]);
1485 INIT_LIST_HEAD(&idev->pending[2]);
1486 INIT_LIST_HEAD(&idev->piowait);
1487 INIT_LIST_HEAD(&idev->rnrwait);
1488 idev->pending_index = 0;
1489 idev->port_cap_flags =
1490 IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
1491 idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1492 idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1493 idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1494 idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
Roland Dreier53533e12006-11-29 15:33:07 -08001495 idev->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001496 idev->link_width_enabled = 3; /* 1x or 4x */
1497
Bryan O'Sullivanfba75202006-07-01 04:36:09 -07001498 /* Snapshot current HW counters to "clear" them. */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001499 ipath_get_counters(dd, &cntrs);
Bryan O'Sullivanfba75202006-07-01 04:36:09 -07001500 idev->z_symbol_error_counter = cntrs.symbol_error_counter;
1501 idev->z_link_error_recovery_counter =
1502 cntrs.link_error_recovery_counter;
1503 idev->z_link_downed_counter = cntrs.link_downed_counter;
1504 idev->z_port_rcv_errors = cntrs.port_rcv_errors;
1505 idev->z_port_rcv_remphys_errors =
1506 cntrs.port_rcv_remphys_errors;
1507 idev->z_port_xmit_discards = cntrs.port_xmit_discards;
1508 idev->z_port_xmit_data = cntrs.port_xmit_data;
1509 idev->z_port_rcv_data = cntrs.port_rcv_data;
1510 idev->z_port_xmit_packets = cntrs.port_xmit_packets;
1511 idev->z_port_rcv_packets = cntrs.port_rcv_packets;
1512 idev->z_local_link_integrity_errors =
1513 cntrs.local_link_integrity_errors;
1514 idev->z_excessive_buffer_overrun_errors =
1515 cntrs.excessive_buffer_overrun_errors;
1516
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001517 /*
1518 * The system image GUID is supposed to be the same for all
1519 * IB HCAs in a single system but since there can be other
1520 * device types in the system, we can't be sure this is unique.
1521 */
1522 if (!sys_image_guid)
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001523 sys_image_guid = dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001524 idev->sys_image_guid = sys_image_guid;
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001525 idev->ib_unit = dd->ipath_unit;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001526 idev->dd = dd;
1527
1528 strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
Bryan O'Sullivan41c75a12006-05-23 11:32:36 -07001529 dev->owner = THIS_MODULE;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001530 dev->node_guid = dd->ipath_guid;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001531 dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
1532 dev->uverbs_cmd_mask =
1533 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1534 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1535 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1536 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1537 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1538 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
1539 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
1540 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
1541 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1542 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1543 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1544 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
1545 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
1546 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1547 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
1548 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
1549 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1550 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
1551 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
1552 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1553 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
1554 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
1555 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1556 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1557 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1558 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
1559 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
1560 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
1561 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001562 dev->node_type = RDMA_NODE_IB_CA;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001563 dev->phys_port_cnt = 1;
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +03001564 dev->num_comp_vectors = 1;
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001565 dev->dma_device = &dd->pcidev->dev;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001566 dev->query_device = ipath_query_device;
1567 dev->modify_device = ipath_modify_device;
1568 dev->query_port = ipath_query_port;
1569 dev->modify_port = ipath_modify_port;
1570 dev->query_pkey = ipath_query_pkey;
1571 dev->query_gid = ipath_query_gid;
1572 dev->alloc_ucontext = ipath_alloc_ucontext;
1573 dev->dealloc_ucontext = ipath_dealloc_ucontext;
1574 dev->alloc_pd = ipath_alloc_pd;
1575 dev->dealloc_pd = ipath_dealloc_pd;
1576 dev->create_ah = ipath_create_ah;
1577 dev->destroy_ah = ipath_destroy_ah;
1578 dev->query_ah = ipath_query_ah;
1579 dev->create_srq = ipath_create_srq;
1580 dev->modify_srq = ipath_modify_srq;
1581 dev->query_srq = ipath_query_srq;
1582 dev->destroy_srq = ipath_destroy_srq;
1583 dev->create_qp = ipath_create_qp;
1584 dev->modify_qp = ipath_modify_qp;
1585 dev->query_qp = ipath_query_qp;
1586 dev->destroy_qp = ipath_destroy_qp;
1587 dev->post_send = ipath_post_send;
1588 dev->post_recv = ipath_post_receive;
1589 dev->post_srq_recv = ipath_post_srq_receive;
1590 dev->create_cq = ipath_create_cq;
1591 dev->destroy_cq = ipath_destroy_cq;
1592 dev->resize_cq = ipath_resize_cq;
1593 dev->poll_cq = ipath_poll_cq;
1594 dev->req_notify_cq = ipath_req_notify_cq;
1595 dev->get_dma_mr = ipath_get_dma_mr;
1596 dev->reg_phys_mr = ipath_reg_phys_mr;
1597 dev->reg_user_mr = ipath_reg_user_mr;
1598 dev->dereg_mr = ipath_dereg_mr;
1599 dev->alloc_fmr = ipath_alloc_fmr;
1600 dev->map_phys_fmr = ipath_map_phys_fmr;
1601 dev->unmap_fmr = ipath_unmap_fmr;
1602 dev->dealloc_fmr = ipath_dealloc_fmr;
1603 dev->attach_mcast = ipath_multicast_attach;
1604 dev->detach_mcast = ipath_multicast_detach;
1605 dev->process_mad = ipath_process_mad;
Ralph Campbell373d9912006-09-22 15:22:26 -07001606 dev->mmap = ipath_mmap;
Ralph Campbellf2cbb662006-12-12 14:28:28 -08001607 dev->dma_ops = &ipath_dma_mapping_ops;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001608
1609 snprintf(dev->node_desc, sizeof(dev->node_desc),
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001610 IPATH_IDSTR " %s", init_utsname()->nodename);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001611
1612 ret = ib_register_device(dev);
1613 if (ret)
1614 goto err_reg;
1615
1616 if (ipath_verbs_register_sysfs(dev))
1617 goto err_class;
1618
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001619 enable_timer(dd);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001620
1621 goto bail;
1622
1623err_class:
1624 ib_unregister_device(dev);
1625err_reg:
1626 kfree(idev->lk_table.table);
1627err_lk:
1628 kfree(idev->qp_table.table);
1629err_qp:
1630 ib_dealloc_device(dev);
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001631 ipath_dev_err(dd, "cannot register verbs: %d!\n", -ret);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001632 idev = NULL;
1633
1634bail:
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001635 dd->verbs_dev = idev;
1636 return ret;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001637}
1638
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -07001639void ipath_unregister_ib_device(struct ipath_ibdev *dev)
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001640{
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001641 struct ib_device *ibdev = &dev->ibdev;
1642
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001643 disable_timer(dev->dd);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001644
1645 ib_unregister_device(ibdev);
1646
1647 if (!list_empty(&dev->pending[0]) ||
1648 !list_empty(&dev->pending[1]) ||
1649 !list_empty(&dev->pending[2]))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001650 ipath_dev_err(dev->dd, "pending list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001651 if (!list_empty(&dev->piowait))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001652 ipath_dev_err(dev->dd, "piowait list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001653 if (!list_empty(&dev->rnrwait))
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001654 ipath_dev_err(dev->dd, "rnrwait list not empty!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001655 if (!ipath_mcast_tree_empty())
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -07001656 ipath_dev_err(dev->dd, "multicast table memory leak!\n");
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001657 /*
1658 * Note that ipath_unregister_ib_device() can be called before all
1659 * the QPs are destroyed!
1660 */
1661 ipath_free_all_qps(&dev->qp_table);
1662 kfree(dev->qp_table.table);
1663 kfree(dev->lk_table.table);
1664 ib_dealloc_device(ibdev);
1665}
1666
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001667static ssize_t show_rev(struct class_device *cdev, char *buf)
1668{
Roland Dreier5494c222006-04-19 11:40:12 -07001669 struct ipath_ibdev *dev =
1670 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001671
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001672 return sprintf(buf, "%x\n", dev->dd->ipath_pcirev);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001673}
1674
1675static ssize_t show_hca(struct class_device *cdev, char *buf)
1676{
Roland Dreier5494c222006-04-19 11:40:12 -07001677 struct ipath_ibdev *dev =
1678 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1679 int ret;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001680
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -07001681 ret = dev->dd->ipath_f_get_boardname(dev->dd, buf, 128);
Roland Dreier5494c222006-04-19 11:40:12 -07001682 if (ret < 0)
1683 goto bail;
1684 strcat(buf, "\n");
1685 ret = strlen(buf);
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001686
1687bail:
1688 return ret;
1689}
1690
1691static ssize_t show_stats(struct class_device *cdev, char *buf)
1692{
Roland Dreier5494c222006-04-19 11:40:12 -07001693 struct ipath_ibdev *dev =
1694 container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
1695 int i;
1696 int len;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001697
Roland Dreier5494c222006-04-19 11:40:12 -07001698 len = sprintf(buf,
1699 "RC resends %d\n"
Bryan O'Sullivan9b2017f2006-04-24 14:23:06 -07001700 "RC no QACK %d\n"
Roland Dreier5494c222006-04-19 11:40:12 -07001701 "RC ACKs %d\n"
1702 "RC SEQ NAKs %d\n"
1703 "RC RDMA seq %d\n"
1704 "RC RNR NAKs %d\n"
1705 "RC OTH NAKs %d\n"
1706 "RC timeouts %d\n"
1707 "RC RDMA dup %d\n"
Bryan O'Sullivan60229432006-09-28 08:59:57 -07001708 "RC stalls %d\n"
Roland Dreier5494c222006-04-19 11:40:12 -07001709 "piobuf wait %d\n"
1710 "no piobuf %d\n"
1711 "PKT drops %d\n"
1712 "WQE errs %d\n",
1713 dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
1714 dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
1715 dev->n_other_naks, dev->n_timeouts,
Bryan O'Sullivan60229432006-09-28 08:59:57 -07001716 dev->n_rdma_dup_busy, dev->n_rc_stalls, dev->n_piowait,
Roland Dreier5494c222006-04-19 11:40:12 -07001717 dev->n_no_piobuf, dev->n_pkt_drops, dev->n_wqe_errs);
1718 for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001719 const struct ipath_opcode_stats *si = &dev->opstats[i];
1720
Roland Dreier5494c222006-04-19 11:40:12 -07001721 if (!si->n_packets && !si->n_bytes)
1722 continue;
1723 len += sprintf(buf + len, "%02x %llu/%llu\n", i,
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001724 (unsigned long long) si->n_packets,
Roland Dreier5494c222006-04-19 11:40:12 -07001725 (unsigned long long) si->n_bytes);
1726 }
1727 return len;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001728}
1729
1730static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1731static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1732static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
1733static CLASS_DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
1734
1735static struct class_device_attribute *ipath_class_attributes[] = {
Roland Dreier5494c222006-04-19 11:40:12 -07001736 &class_device_attr_hw_rev,
1737 &class_device_attr_hca_type,
1738 &class_device_attr_board_id,
1739 &class_device_attr_stats
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001740};
1741
1742static int ipath_verbs_register_sysfs(struct ib_device *dev)
1743{
Roland Dreier5494c222006-04-19 11:40:12 -07001744 int i;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001745 int ret;
1746
Roland Dreier5494c222006-04-19 11:40:12 -07001747 for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
1748 if (class_device_create_file(&dev->class_dev,
1749 ipath_class_attributes[i])) {
1750 ret = 1;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001751 goto bail;
1752 }
1753
Roland Dreier5494c222006-04-19 11:40:12 -07001754 ret = 0;
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001755
1756bail:
1757 return ret;
1758}