device.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. #include <rdma/ib_verbs.h>
  36. #include "iw_cxgb4.h"
  37. #define DRV_VERSION "0.1"
  38. MODULE_AUTHOR("Steve Wise");
  39. MODULE_DESCRIPTION("Chelsio T4 RDMA Driver");
  40. MODULE_LICENSE("Dual BSD/GPL");
  41. MODULE_VERSION(DRV_VERSION);
  42. static LIST_HEAD(dev_list);
  43. static DEFINE_MUTEX(dev_mutex);
  44. static struct dentry *c4iw_debugfs_root;
  45. struct debugfs_qp_data {
  46. struct c4iw_dev *devp;
  47. char *buf;
  48. int bufsize;
  49. int pos;
  50. };
  51. static int count_qps(int id, void *p, void *data)
  52. {
  53. struct c4iw_qp *qp = p;
  54. int *countp = data;
  55. if (id != qp->wq.sq.qid)
  56. return 0;
  57. *countp = *countp + 1;
  58. return 0;
  59. }
  60. static int dump_qps(int id, void *p, void *data)
  61. {
  62. struct c4iw_qp *qp = p;
  63. struct debugfs_qp_data *qpd = data;
  64. int space;
  65. int cc;
  66. if (id != qp->wq.sq.qid)
  67. return 0;
  68. space = qpd->bufsize - qpd->pos - 1;
  69. if (space == 0)
  70. return 1;
  71. if (qp->ep)
  72. cc = snprintf(qpd->buf + qpd->pos, space, "qp id %u state %u "
  73. "ep tid %u state %u %pI4:%u->%pI4:%u\n",
  74. qp->wq.sq.qid, (int)qp->attr.state,
  75. qp->ep->hwtid, (int)qp->ep->com.state,
  76. &qp->ep->com.local_addr.sin_addr.s_addr,
  77. ntohs(qp->ep->com.local_addr.sin_port),
  78. &qp->ep->com.remote_addr.sin_addr.s_addr,
  79. ntohs(qp->ep->com.remote_addr.sin_port));
  80. else
  81. cc = snprintf(qpd->buf + qpd->pos, space, "qp id %u state %u\n",
  82. qp->wq.sq.qid, (int)qp->attr.state);
  83. if (cc < space)
  84. qpd->pos += cc;
  85. return 0;
  86. }
  87. static int qp_release(struct inode *inode, struct file *file)
  88. {
  89. struct debugfs_qp_data *qpd = file->private_data;
  90. if (!qpd) {
  91. printk(KERN_INFO "%s null qpd?\n", __func__);
  92. return 0;
  93. }
  94. kfree(qpd->buf);
  95. kfree(qpd);
  96. return 0;
  97. }
  98. static int qp_open(struct inode *inode, struct file *file)
  99. {
  100. struct debugfs_qp_data *qpd;
  101. int ret = 0;
  102. int count = 1;
  103. qpd = kmalloc(sizeof *qpd, GFP_KERNEL);
  104. if (!qpd) {
  105. ret = -ENOMEM;
  106. goto out;
  107. }
  108. qpd->devp = inode->i_private;
  109. qpd->pos = 0;
  110. spin_lock_irq(&qpd->devp->lock);
  111. idr_for_each(&qpd->devp->qpidr, count_qps, &count);
  112. spin_unlock_irq(&qpd->devp->lock);
  113. qpd->bufsize = count * 128;
  114. qpd->buf = kmalloc(qpd->bufsize, GFP_KERNEL);
  115. if (!qpd->buf) {
  116. ret = -ENOMEM;
  117. goto err1;
  118. }
  119. spin_lock_irq(&qpd->devp->lock);
  120. idr_for_each(&qpd->devp->qpidr, dump_qps, qpd);
  121. spin_unlock_irq(&qpd->devp->lock);
  122. qpd->buf[qpd->pos++] = 0;
  123. file->private_data = qpd;
  124. goto out;
  125. err1:
  126. kfree(qpd);
  127. out:
  128. return ret;
  129. }
  130. static ssize_t qp_read(struct file *file, char __user *buf, size_t count,
  131. loff_t *ppos)
  132. {
  133. struct debugfs_qp_data *qpd = file->private_data;
  134. loff_t pos = *ppos;
  135. loff_t avail = qpd->pos;
  136. if (pos < 0)
  137. return -EINVAL;
  138. if (pos >= avail)
  139. return 0;
  140. if (count > avail - pos)
  141. count = avail - pos;
  142. while (count) {
  143. size_t len = 0;
  144. len = min((int)count, (int)qpd->pos - (int)pos);
  145. if (copy_to_user(buf, qpd->buf + pos, len))
  146. return -EFAULT;
  147. if (len == 0)
  148. return -EINVAL;
  149. buf += len;
  150. pos += len;
  151. count -= len;
  152. }
  153. count = pos - *ppos;
  154. *ppos = pos;
  155. return count;
  156. }
  157. static const struct file_operations qp_debugfs_fops = {
  158. .owner = THIS_MODULE,
  159. .open = qp_open,
  160. .release = qp_release,
  161. .read = qp_read,
  162. };
  163. static int setup_debugfs(struct c4iw_dev *devp)
  164. {
  165. struct dentry *de;
  166. if (!devp->debugfs_root)
  167. return -1;
  168. de = debugfs_create_file("qps", S_IWUSR, devp->debugfs_root,
  169. (void *)devp, &qp_debugfs_fops);
  170. if (de && de->d_inode)
  171. de->d_inode->i_size = 4096;
  172. return 0;
  173. }
  174. void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
  175. struct c4iw_dev_ucontext *uctx)
  176. {
  177. struct list_head *pos, *nxt;
  178. struct c4iw_qid_list *entry;
  179. mutex_lock(&uctx->lock);
  180. list_for_each_safe(pos, nxt, &uctx->qpids) {
  181. entry = list_entry(pos, struct c4iw_qid_list, entry);
  182. list_del_init(&entry->entry);
  183. if (!(entry->qid & rdev->qpmask))
  184. c4iw_put_resource(&rdev->resource.qid_fifo, entry->qid,
  185. &rdev->resource.qid_fifo_lock);
  186. kfree(entry);
  187. }
  188. list_for_each_safe(pos, nxt, &uctx->qpids) {
  189. entry = list_entry(pos, struct c4iw_qid_list, entry);
  190. list_del_init(&entry->entry);
  191. kfree(entry);
  192. }
  193. mutex_unlock(&uctx->lock);
  194. }
  195. void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev,
  196. struct c4iw_dev_ucontext *uctx)
  197. {
  198. INIT_LIST_HEAD(&uctx->qpids);
  199. INIT_LIST_HEAD(&uctx->cqids);
  200. mutex_init(&uctx->lock);
  201. }
  202. /* Caller takes care of locking if needed */
  203. static int c4iw_rdev_open(struct c4iw_rdev *rdev)
  204. {
  205. int err;
  206. c4iw_init_dev_ucontext(rdev, &rdev->uctx);
  207. /*
  208. * qpshift is the number of bits to shift the qpid left in order
  209. * to get the correct address of the doorbell for that qp.
  210. */
  211. rdev->qpshift = PAGE_SHIFT - ilog2(rdev->lldi.udb_density);
  212. rdev->qpmask = rdev->lldi.udb_density - 1;
  213. rdev->cqshift = PAGE_SHIFT - ilog2(rdev->lldi.ucq_density);
  214. rdev->cqmask = rdev->lldi.ucq_density - 1;
  215. PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "
  216. "pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x\n",
  217. __func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
  218. rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
  219. rdev->lldi.vr->pbl.start,
  220. rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
  221. rdev->lldi.vr->rq.size);
  222. PDBG("udb len 0x%x udb base %p db_reg %p gts_reg %p qpshift %lu "
  223. "qpmask 0x%x cqshift %lu cqmask 0x%x\n",
  224. (unsigned)pci_resource_len(rdev->lldi.pdev, 2),
  225. (void *)pci_resource_start(rdev->lldi.pdev, 2),
  226. rdev->lldi.db_reg,
  227. rdev->lldi.gts_reg,
  228. rdev->qpshift, rdev->qpmask,
  229. rdev->cqshift, rdev->cqmask);
  230. if (c4iw_num_stags(rdev) == 0) {
  231. err = -EINVAL;
  232. goto err1;
  233. }
  234. err = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
  235. if (err) {
  236. printk(KERN_ERR MOD "error %d initializing resources\n", err);
  237. goto err1;
  238. }
  239. err = c4iw_pblpool_create(rdev);
  240. if (err) {
  241. printk(KERN_ERR MOD "error %d initializing pbl pool\n", err);
  242. goto err2;
  243. }
  244. err = c4iw_rqtpool_create(rdev);
  245. if (err) {
  246. printk(KERN_ERR MOD "error %d initializing rqt pool\n", err);
  247. goto err3;
  248. }
  249. return 0;
  250. err3:
  251. c4iw_pblpool_destroy(rdev);
  252. err2:
  253. c4iw_destroy_resource(&rdev->resource);
  254. err1:
  255. return err;
  256. }
  257. static void c4iw_rdev_close(struct c4iw_rdev *rdev)
  258. {
  259. c4iw_pblpool_destroy(rdev);
  260. c4iw_rqtpool_destroy(rdev);
  261. c4iw_destroy_resource(&rdev->resource);
  262. }
  263. static void c4iw_remove(struct c4iw_dev *dev)
  264. {
  265. PDBG("%s c4iw_dev %p\n", __func__, dev);
  266. cancel_delayed_work_sync(&dev->db_drop_task);
  267. list_del(&dev->entry);
  268. if (dev->registered)
  269. c4iw_unregister_device(dev);
  270. c4iw_rdev_close(&dev->rdev);
  271. idr_destroy(&dev->cqidr);
  272. idr_destroy(&dev->qpidr);
  273. idr_destroy(&dev->mmidr);
  274. ib_dealloc_device(&dev->ibdev);
  275. }
  276. static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
  277. {
  278. struct c4iw_dev *devp;
  279. int ret;
  280. devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));
  281. if (!devp) {
  282. printk(KERN_ERR MOD "Cannot allocate ib device\n");
  283. return NULL;
  284. }
  285. devp->rdev.lldi = *infop;
  286. mutex_lock(&dev_mutex);
  287. ret = c4iw_rdev_open(&devp->rdev);
  288. if (ret) {
  289. mutex_unlock(&dev_mutex);
  290. printk(KERN_ERR MOD "Unable to open CXIO rdev err %d\n", ret);
  291. ib_dealloc_device(&devp->ibdev);
  292. return NULL;
  293. }
  294. idr_init(&devp->cqidr);
  295. idr_init(&devp->qpidr);
  296. idr_init(&devp->mmidr);
  297. spin_lock_init(&devp->lock);
  298. list_add_tail(&devp->entry, &dev_list);
  299. mutex_unlock(&dev_mutex);
  300. if (c4iw_debugfs_root) {
  301. devp->debugfs_root = debugfs_create_dir(
  302. pci_name(devp->rdev.lldi.pdev),
  303. c4iw_debugfs_root);
  304. setup_debugfs(devp);
  305. }
  306. return devp;
  307. }
  308. static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)
  309. {
  310. struct c4iw_dev *dev;
  311. static int vers_printed;
  312. int i;
  313. if (!vers_printed++)
  314. printk(KERN_INFO MOD "Chelsio T4 RDMA Driver - version %s\n",
  315. DRV_VERSION);
  316. dev = c4iw_alloc(infop);
  317. if (!dev)
  318. goto out;
  319. PDBG("%s found device %s nchan %u nrxq %u ntxq %u nports %u\n",
  320. __func__, pci_name(dev->rdev.lldi.pdev),
  321. dev->rdev.lldi.nchan, dev->rdev.lldi.nrxq,
  322. dev->rdev.lldi.ntxq, dev->rdev.lldi.nports);
  323. for (i = 0; i < dev->rdev.lldi.nrxq; i++)
  324. PDBG("rxqid[%u] %u\n", i, dev->rdev.lldi.rxq_ids[i]);
  325. out:
  326. return dev;
  327. }
  328. static struct sk_buff *t4_pktgl_to_skb(const struct pkt_gl *gl,
  329. unsigned int skb_len,
  330. unsigned int pull_len)
  331. {
  332. struct sk_buff *skb;
  333. struct skb_shared_info *ssi;
  334. if (gl->tot_len <= 512) {
  335. skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
  336. if (unlikely(!skb))
  337. goto out;
  338. __skb_put(skb, gl->tot_len);
  339. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  340. } else {
  341. skb = alloc_skb(skb_len, GFP_ATOMIC);
  342. if (unlikely(!skb))
  343. goto out;
  344. __skb_put(skb, pull_len);
  345. skb_copy_to_linear_data(skb, gl->va, pull_len);
  346. ssi = skb_shinfo(skb);
  347. ssi->frags[0].page = gl->frags[0].page;
  348. ssi->frags[0].page_offset = gl->frags[0].page_offset + pull_len;
  349. ssi->frags[0].size = gl->frags[0].size - pull_len;
  350. if (gl->nfrags > 1)
  351. memcpy(&ssi->frags[1], &gl->frags[1],
  352. (gl->nfrags - 1) * sizeof(skb_frag_t));
  353. ssi->nr_frags = gl->nfrags;
  354. skb->len = gl->tot_len;
  355. skb->data_len = skb->len - pull_len;
  356. skb->truesize += skb->data_len;
  357. /* Get a reference for the last page, we don't own it */
  358. get_page(gl->frags[gl->nfrags - 1].page);
  359. }
  360. out:
  361. return skb;
  362. }
  363. static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,
  364. const struct pkt_gl *gl)
  365. {
  366. struct c4iw_dev *dev = handle;
  367. struct sk_buff *skb;
  368. const struct cpl_act_establish *rpl;
  369. unsigned int opcode;
  370. if (gl == NULL) {
  371. /* omit RSS and rsp_ctrl at end of descriptor */
  372. unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
  373. skb = alloc_skb(256, GFP_ATOMIC);
  374. if (!skb)
  375. goto nomem;
  376. __skb_put(skb, len);
  377. skb_copy_to_linear_data(skb, &rsp[1], len);
  378. } else if (gl == CXGB4_MSG_AN) {
  379. const struct rsp_ctrl *rc = (void *)rsp;
  380. u32 qid = be32_to_cpu(rc->pldbuflen_qid);
  381. c4iw_ev_handler(dev, qid);
  382. return 0;
  383. } else {
  384. skb = t4_pktgl_to_skb(gl, 128, 128);
  385. if (unlikely(!skb))
  386. goto nomem;
  387. }
  388. rpl = cplhdr(skb);
  389. opcode = rpl->ot.opcode;
  390. if (c4iw_handlers[opcode])
  391. c4iw_handlers[opcode](dev, skb);
  392. else
  393. printk(KERN_INFO "%s no handler opcode 0x%x...\n", __func__,
  394. opcode);
  395. return 0;
  396. nomem:
  397. return -1;
  398. }
  399. static int c4iw_uld_state_change(void *handle, enum cxgb4_state new_state)
  400. {
  401. struct c4iw_dev *dev = handle;
  402. PDBG("%s new_state %u\n", __func__, new_state);
  403. switch (new_state) {
  404. case CXGB4_STATE_UP:
  405. printk(KERN_INFO MOD "%s: Up\n", pci_name(dev->rdev.lldi.pdev));
  406. if (!dev->registered) {
  407. int ret;
  408. ret = c4iw_register_device(dev);
  409. if (ret)
  410. printk(KERN_ERR MOD
  411. "%s: RDMA registration failed: %d\n",
  412. pci_name(dev->rdev.lldi.pdev), ret);
  413. }
  414. break;
  415. case CXGB4_STATE_DOWN:
  416. printk(KERN_INFO MOD "%s: Down\n",
  417. pci_name(dev->rdev.lldi.pdev));
  418. if (dev->registered)
  419. c4iw_unregister_device(dev);
  420. break;
  421. case CXGB4_STATE_START_RECOVERY:
  422. printk(KERN_INFO MOD "%s: Fatal Error\n",
  423. pci_name(dev->rdev.lldi.pdev));
  424. if (dev->registered)
  425. c4iw_unregister_device(dev);
  426. break;
  427. case CXGB4_STATE_DETACH:
  428. printk(KERN_INFO MOD "%s: Detach\n",
  429. pci_name(dev->rdev.lldi.pdev));
  430. mutex_lock(&dev_mutex);
  431. c4iw_remove(dev);
  432. mutex_unlock(&dev_mutex);
  433. break;
  434. }
  435. return 0;
  436. }
  437. static struct cxgb4_uld_info c4iw_uld_info = {
  438. .name = DRV_NAME,
  439. .add = c4iw_uld_add,
  440. .rx_handler = c4iw_uld_rx_handler,
  441. .state_change = c4iw_uld_state_change,
  442. };
  443. static int __init c4iw_init_module(void)
  444. {
  445. int err;
  446. err = c4iw_cm_init();
  447. if (err)
  448. return err;
  449. c4iw_debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
  450. if (!c4iw_debugfs_root)
  451. printk(KERN_WARNING MOD
  452. "could not create debugfs entry, continuing\n");
  453. cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);
  454. return 0;
  455. }
  456. static void __exit c4iw_exit_module(void)
  457. {
  458. struct c4iw_dev *dev, *tmp;
  459. mutex_lock(&dev_mutex);
  460. list_for_each_entry_safe(dev, tmp, &dev_list, entry) {
  461. c4iw_remove(dev);
  462. }
  463. mutex_unlock(&dev_mutex);
  464. cxgb4_unregister_uld(CXGB4_ULD_RDMA);
  465. c4iw_cm_term();
  466. debugfs_remove_recursive(c4iw_debugfs_root);
  467. }
  468. module_init(c4iw_init_module);
  469. module_exit(c4iw_exit_module);