Merge branch 'v4l_for_2.6.35' of git://git.kernel.org/pub/scm/linux/kernel/git/mcheha...
[pandora-kernel.git] / drivers / infiniband / hw / cxgb4 / device.c
1 /*
2  * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/debugfs.h>
35
36 #include <rdma/ib_verbs.h>
37
38 #include "iw_cxgb4.h"
39
40 #define DRV_VERSION "0.1"
41
42 MODULE_AUTHOR("Steve Wise");
43 MODULE_DESCRIPTION("Chelsio T4 RDMA Driver");
44 MODULE_LICENSE("Dual BSD/GPL");
45 MODULE_VERSION(DRV_VERSION);
46
47 static LIST_HEAD(dev_list);
48 static DEFINE_MUTEX(dev_mutex);
49
50 static struct dentry *c4iw_debugfs_root;
51
52 struct debugfs_qp_data {
53         struct c4iw_dev *devp;
54         char *buf;
55         int bufsize;
56         int pos;
57 };
58
59 static int count_qps(int id, void *p, void *data)
60 {
61         struct c4iw_qp *qp = p;
62         int *countp = data;
63
64         if (id != qp->wq.sq.qid)
65                 return 0;
66
67         *countp = *countp + 1;
68         return 0;
69 }
70
71 static int dump_qps(int id, void *p, void *data)
72 {
73         struct c4iw_qp *qp = p;
74         struct debugfs_qp_data *qpd = data;
75         int space;
76         int cc;
77
78         if (id != qp->wq.sq.qid)
79                 return 0;
80
81         space = qpd->bufsize - qpd->pos - 1;
82         if (space == 0)
83                 return 1;
84
85         if (qp->ep)
86                 cc = snprintf(qpd->buf + qpd->pos, space, "qp id %u state %u "
87                              "ep tid %u state %u %pI4:%u->%pI4:%u\n",
88                              qp->wq.sq.qid, (int)qp->attr.state,
89                              qp->ep->hwtid, (int)qp->ep->com.state,
90                              &qp->ep->com.local_addr.sin_addr.s_addr,
91                              ntohs(qp->ep->com.local_addr.sin_port),
92                              &qp->ep->com.remote_addr.sin_addr.s_addr,
93                              ntohs(qp->ep->com.remote_addr.sin_port));
94         else
95                 cc = snprintf(qpd->buf + qpd->pos, space, "qp id %u state %u\n",
96                               qp->wq.sq.qid, (int)qp->attr.state);
97         if (cc < space)
98                 qpd->pos += cc;
99         return 0;
100 }
101
102 static int qp_release(struct inode *inode, struct file *file)
103 {
104         struct debugfs_qp_data *qpd = file->private_data;
105         if (!qpd) {
106                 printk(KERN_INFO "%s null qpd?\n", __func__);
107                 return 0;
108         }
109         kfree(qpd->buf);
110         kfree(qpd);
111         return 0;
112 }
113
114 static int qp_open(struct inode *inode, struct file *file)
115 {
116         struct debugfs_qp_data *qpd;
117         int ret = 0;
118         int count = 1;
119
120         qpd = kmalloc(sizeof *qpd, GFP_KERNEL);
121         if (!qpd) {
122                 ret = -ENOMEM;
123                 goto out;
124         }
125         qpd->devp = inode->i_private;
126         qpd->pos = 0;
127
128         spin_lock_irq(&qpd->devp->lock);
129         idr_for_each(&qpd->devp->qpidr, count_qps, &count);
130         spin_unlock_irq(&qpd->devp->lock);
131
132         qpd->bufsize = count * 128;
133         qpd->buf = kmalloc(qpd->bufsize, GFP_KERNEL);
134         if (!qpd->buf) {
135                 ret = -ENOMEM;
136                 goto err1;
137         }
138
139         spin_lock_irq(&qpd->devp->lock);
140         idr_for_each(&qpd->devp->qpidr, dump_qps, qpd);
141         spin_unlock_irq(&qpd->devp->lock);
142
143         qpd->buf[qpd->pos++] = 0;
144         file->private_data = qpd;
145         goto out;
146 err1:
147         kfree(qpd);
148 out:
149         return ret;
150 }
151
152 static ssize_t qp_read(struct file *file, char __user *buf, size_t count,
153                         loff_t *ppos)
154 {
155         struct debugfs_qp_data *qpd = file->private_data;
156         loff_t pos = *ppos;
157         loff_t avail = qpd->pos;
158
159         if (pos < 0)
160                 return -EINVAL;
161         if (pos >= avail)
162                 return 0;
163         if (count > avail - pos)
164                 count = avail - pos;
165
166         while (count) {
167                 size_t len = 0;
168
169                 len = min((int)count, (int)qpd->pos - (int)pos);
170                 if (copy_to_user(buf, qpd->buf + pos, len))
171                         return -EFAULT;
172                 if (len == 0)
173                         return -EINVAL;
174
175                 buf += len;
176                 pos += len;
177                 count -= len;
178         }
179         count = pos - *ppos;
180         *ppos = pos;
181         return count;
182 }
183
184 static const struct file_operations qp_debugfs_fops = {
185         .owner   = THIS_MODULE,
186         .open    = qp_open,
187         .release = qp_release,
188         .read    = qp_read,
189 };
190
191 static int setup_debugfs(struct c4iw_dev *devp)
192 {
193         struct dentry *de;
194
195         if (!devp->debugfs_root)
196                 return -1;
197
198         de = debugfs_create_file("qps", S_IWUSR, devp->debugfs_root,
199                                  (void *)devp, &qp_debugfs_fops);
200         if (de && de->d_inode)
201                 de->d_inode->i_size = 4096;
202         return 0;
203 }
204
205 void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
206                                struct c4iw_dev_ucontext *uctx)
207 {
208         struct list_head *pos, *nxt;
209         struct c4iw_qid_list *entry;
210
211         mutex_lock(&uctx->lock);
212         list_for_each_safe(pos, nxt, &uctx->qpids) {
213                 entry = list_entry(pos, struct c4iw_qid_list, entry);
214                 list_del_init(&entry->entry);
215                 if (!(entry->qid & rdev->qpmask))
216                         c4iw_put_resource(&rdev->resource.qid_fifo, entry->qid,
217                                           &rdev->resource.qid_fifo_lock);
218                 kfree(entry);
219         }
220
221         list_for_each_safe(pos, nxt, &uctx->qpids) {
222                 entry = list_entry(pos, struct c4iw_qid_list, entry);
223                 list_del_init(&entry->entry);
224                 kfree(entry);
225         }
226         mutex_unlock(&uctx->lock);
227 }
228
229 void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev,
230                             struct c4iw_dev_ucontext *uctx)
231 {
232         INIT_LIST_HEAD(&uctx->qpids);
233         INIT_LIST_HEAD(&uctx->cqids);
234         mutex_init(&uctx->lock);
235 }
236
237 /* Caller takes care of locking if needed */
238 static int c4iw_rdev_open(struct c4iw_rdev *rdev)
239 {
240         int err;
241
242         c4iw_init_dev_ucontext(rdev, &rdev->uctx);
243
244         /*
245          * qpshift is the number of bits to shift the qpid left in order
246          * to get the correct address of the doorbell for that qp.
247          */
248         rdev->qpshift = PAGE_SHIFT - ilog2(rdev->lldi.udb_density);
249         rdev->qpmask = rdev->lldi.udb_density - 1;
250         rdev->cqshift = PAGE_SHIFT - ilog2(rdev->lldi.ucq_density);
251         rdev->cqmask = rdev->lldi.ucq_density - 1;
252         PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "
253              "pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x\n",
254              __func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
255              rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
256              rdev->lldi.vr->pbl.start,
257              rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
258              rdev->lldi.vr->rq.size);
259         PDBG("udb len 0x%x udb base %p db_reg %p gts_reg %p qpshift %lu "
260              "qpmask 0x%x cqshift %lu cqmask 0x%x\n",
261              (unsigned)pci_resource_len(rdev->lldi.pdev, 2),
262              (void *)pci_resource_start(rdev->lldi.pdev, 2),
263              rdev->lldi.db_reg,
264              rdev->lldi.gts_reg,
265              rdev->qpshift, rdev->qpmask,
266              rdev->cqshift, rdev->cqmask);
267
268         if (c4iw_num_stags(rdev) == 0) {
269                 err = -EINVAL;
270                 goto err1;
271         }
272
273         err = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
274         if (err) {
275                 printk(KERN_ERR MOD "error %d initializing resources\n", err);
276                 goto err1;
277         }
278         err = c4iw_pblpool_create(rdev);
279         if (err) {
280                 printk(KERN_ERR MOD "error %d initializing pbl pool\n", err);
281                 goto err2;
282         }
283         err = c4iw_rqtpool_create(rdev);
284         if (err) {
285                 printk(KERN_ERR MOD "error %d initializing rqt pool\n", err);
286                 goto err3;
287         }
288         return 0;
289 err3:
290         c4iw_pblpool_destroy(rdev);
291 err2:
292         c4iw_destroy_resource(&rdev->resource);
293 err1:
294         return err;
295 }
296
297 static void c4iw_rdev_close(struct c4iw_rdev *rdev)
298 {
299         c4iw_pblpool_destroy(rdev);
300         c4iw_rqtpool_destroy(rdev);
301         c4iw_destroy_resource(&rdev->resource);
302 }
303
304 static void c4iw_remove(struct c4iw_dev *dev)
305 {
306         PDBG("%s c4iw_dev %p\n", __func__,  dev);
307         cancel_delayed_work_sync(&dev->db_drop_task);
308         list_del(&dev->entry);
309         c4iw_unregister_device(dev);
310         c4iw_rdev_close(&dev->rdev);
311         idr_destroy(&dev->cqidr);
312         idr_destroy(&dev->qpidr);
313         idr_destroy(&dev->mmidr);
314         ib_dealloc_device(&dev->ibdev);
315 }
316
317 static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
318 {
319         struct c4iw_dev *devp;
320         int ret;
321
322         devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));
323         if (!devp) {
324                 printk(KERN_ERR MOD "Cannot allocate ib device\n");
325                 return NULL;
326         }
327         devp->rdev.lldi = *infop;
328
329         mutex_lock(&dev_mutex);
330
331         ret = c4iw_rdev_open(&devp->rdev);
332         if (ret) {
333                 mutex_unlock(&dev_mutex);
334                 printk(KERN_ERR MOD "Unable to open CXIO rdev err %d\n", ret);
335                 ib_dealloc_device(&devp->ibdev);
336                 return NULL;
337         }
338
339         idr_init(&devp->cqidr);
340         idr_init(&devp->qpidr);
341         idr_init(&devp->mmidr);
342         spin_lock_init(&devp->lock);
343         list_add_tail(&devp->entry, &dev_list);
344         mutex_unlock(&dev_mutex);
345
346         if (c4iw_register_device(devp)) {
347                 printk(KERN_ERR MOD "Unable to register device\n");
348                 mutex_lock(&dev_mutex);
349                 c4iw_remove(devp);
350                 mutex_unlock(&dev_mutex);
351         }
352         if (c4iw_debugfs_root) {
353                 devp->debugfs_root = debugfs_create_dir(
354                                         pci_name(devp->rdev.lldi.pdev),
355                                         c4iw_debugfs_root);
356                 setup_debugfs(devp);
357         }
358         return devp;
359 }
360
361 static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)
362 {
363         struct c4iw_dev *dev;
364         static int vers_printed;
365         int i;
366
367         if (!vers_printed++)
368                 printk(KERN_INFO MOD "Chelsio T4 RDMA Driver - version %s\n",
369                        DRV_VERSION);
370
371         dev = c4iw_alloc(infop);
372         if (!dev)
373                 goto out;
374
375         PDBG("%s found device %s nchan %u nrxq %u ntxq %u nports %u\n",
376              __func__, pci_name(dev->rdev.lldi.pdev),
377              dev->rdev.lldi.nchan, dev->rdev.lldi.nrxq,
378              dev->rdev.lldi.ntxq, dev->rdev.lldi.nports);
379
380         for (i = 0; i < dev->rdev.lldi.nrxq; i++)
381                 PDBG("rxqid[%u] %u\n", i, dev->rdev.lldi.rxq_ids[i]);
382
383         printk(KERN_INFO MOD "Initialized device %s\n",
384                pci_name(dev->rdev.lldi.pdev));
385 out:
386         return dev;
387 }
388
389 static struct sk_buff *t4_pktgl_to_skb(const struct pkt_gl *gl,
390                                        unsigned int skb_len,
391                                        unsigned int pull_len)
392 {
393         struct sk_buff *skb;
394         struct skb_shared_info *ssi;
395
396         if (gl->tot_len <= 512) {
397                 skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
398                 if (unlikely(!skb))
399                         goto out;
400                 __skb_put(skb, gl->tot_len);
401                 skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
402         } else {
403                 skb = alloc_skb(skb_len, GFP_ATOMIC);
404                 if (unlikely(!skb))
405                         goto out;
406                 __skb_put(skb, pull_len);
407                 skb_copy_to_linear_data(skb, gl->va, pull_len);
408
409                 ssi = skb_shinfo(skb);
410                 ssi->frags[0].page = gl->frags[0].page;
411                 ssi->frags[0].page_offset = gl->frags[0].page_offset + pull_len;
412                 ssi->frags[0].size = gl->frags[0].size - pull_len;
413                 if (gl->nfrags > 1)
414                         memcpy(&ssi->frags[1], &gl->frags[1],
415                                (gl->nfrags - 1) * sizeof(skb_frag_t));
416                 ssi->nr_frags = gl->nfrags;
417
418                 skb->len = gl->tot_len;
419                 skb->data_len = skb->len - pull_len;
420                 skb->truesize += skb->data_len;
421
422                 /* Get a reference for the last page, we don't own it */
423                 get_page(gl->frags[gl->nfrags - 1].page);
424         }
425 out:
426         return skb;
427 }
428
429 static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,
430                         const struct pkt_gl *gl)
431 {
432         struct c4iw_dev *dev = handle;
433         struct sk_buff *skb;
434         const struct cpl_act_establish *rpl;
435         unsigned int opcode;
436
437         if (gl == NULL) {
438                 /* omit RSS and rsp_ctrl at end of descriptor */
439                 unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
440
441                 skb = alloc_skb(256, GFP_ATOMIC);
442                 if (!skb)
443                         goto nomem;
444                 __skb_put(skb, len);
445                 skb_copy_to_linear_data(skb, &rsp[1], len);
446         } else if (gl == CXGB4_MSG_AN) {
447                 const struct rsp_ctrl *rc = (void *)rsp;
448
449                 u32 qid = be32_to_cpu(rc->pldbuflen_qid);
450                 c4iw_ev_handler(dev, qid);
451                 return 0;
452         } else {
453                 skb = t4_pktgl_to_skb(gl, 128, 128);
454                 if (unlikely(!skb))
455                         goto nomem;
456         }
457
458         rpl = cplhdr(skb);
459         opcode = rpl->ot.opcode;
460
461         if (c4iw_handlers[opcode])
462                 c4iw_handlers[opcode](dev, skb);
463         else
464                 printk(KERN_INFO "%s no handler opcode 0x%x...\n", __func__,
465                        opcode);
466
467         return 0;
468 nomem:
469         return -1;
470 }
471
472 static int c4iw_uld_state_change(void *handle, enum cxgb4_state new_state)
473 {
474         PDBG("%s new_state %u\n", __func__, new_state);
475         return 0;
476 }
477
478 static struct cxgb4_uld_info c4iw_uld_info = {
479         .name = DRV_NAME,
480         .add = c4iw_uld_add,
481         .rx_handler = c4iw_uld_rx_handler,
482         .state_change = c4iw_uld_state_change,
483 };
484
485 static int __init c4iw_init_module(void)
486 {
487         int err;
488
489         err = c4iw_cm_init();
490         if (err)
491                 return err;
492
493         c4iw_debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
494         if (!c4iw_debugfs_root)
495                 printk(KERN_WARNING MOD
496                        "could not create debugfs entry, continuing\n");
497
498         cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);
499
500         return 0;
501 }
502
503 static void __exit c4iw_exit_module(void)
504 {
505         struct c4iw_dev *dev, *tmp;
506
507         cxgb4_unregister_uld(CXGB4_ULD_RDMA);
508
509         mutex_lock(&dev_mutex);
510         list_for_each_entry_safe(dev, tmp, &dev_list, entry) {
511                 c4iw_remove(dev);
512         }
513         mutex_unlock(&dev_mutex);
514
515         c4iw_cm_term();
516         debugfs_remove_recursive(c4iw_debugfs_root);
517 }
518
519 module_init(c4iw_init_module);
520 module_exit(c4iw_exit_module);