mthca_provider.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, 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. * $Id: mthca_provider.c 1397 2004-12-28 05:09:00Z roland $
  34. */
  35. #include <ib_smi.h>
  36. #include "mthca_dev.h"
  37. #include "mthca_cmd.h"
  38. static int mthca_query_device(struct ib_device *ibdev,
  39. struct ib_device_attr *props)
  40. {
  41. struct ib_smp *in_mad = NULL;
  42. struct ib_smp *out_mad = NULL;
  43. int err = -ENOMEM;
  44. struct mthca_dev* mdev = to_mdev(ibdev);
  45. u8 status;
  46. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  47. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  48. if (!in_mad || !out_mad)
  49. goto out;
  50. memset(props, 0, sizeof *props);
  51. props->fw_ver = mdev->fw_ver;
  52. memset(in_mad, 0, sizeof *in_mad);
  53. in_mad->base_version = 1;
  54. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  55. in_mad->class_version = 1;
  56. in_mad->method = IB_MGMT_METHOD_GET;
  57. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  58. err = mthca_MAD_IFC(mdev, 1, 1,
  59. 1, NULL, NULL, in_mad, out_mad,
  60. &status);
  61. if (err)
  62. goto out;
  63. if (status) {
  64. err = -EINVAL;
  65. goto out;
  66. }
  67. props->device_cap_flags = mdev->device_cap_flags;
  68. props->vendor_id = be32_to_cpup((u32 *) (out_mad->data + 36)) &
  69. 0xffffff;
  70. props->vendor_part_id = be16_to_cpup((u16 *) (out_mad->data + 30));
  71. props->hw_ver = be16_to_cpup((u16 *) (out_mad->data + 32));
  72. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  73. memcpy(&props->node_guid, out_mad->data + 12, 8);
  74. props->max_mr_size = ~0ull;
  75. props->max_qp = mdev->limits.num_qps - mdev->limits.reserved_qps;
  76. props->max_qp_wr = 0xffff;
  77. props->max_sge = mdev->limits.max_sg;
  78. props->max_cq = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
  79. props->max_cqe = 0xffff;
  80. props->max_mr = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
  81. props->max_pd = mdev->limits.num_pds - mdev->limits.reserved_pds;
  82. props->max_qp_rd_atom = 1 << mdev->qp_table.rdb_shift;
  83. props->max_qp_init_rd_atom = 1 << mdev->qp_table.rdb_shift;
  84. props->local_ca_ack_delay = mdev->limits.local_ca_ack_delay;
  85. err = 0;
  86. out:
  87. kfree(in_mad);
  88. kfree(out_mad);
  89. return err;
  90. }
  91. static int mthca_query_port(struct ib_device *ibdev,
  92. u8 port, struct ib_port_attr *props)
  93. {
  94. struct ib_smp *in_mad = NULL;
  95. struct ib_smp *out_mad = NULL;
  96. int err = -ENOMEM;
  97. u8 status;
  98. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  99. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  100. if (!in_mad || !out_mad)
  101. goto out;
  102. memset(in_mad, 0, sizeof *in_mad);
  103. in_mad->base_version = 1;
  104. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  105. in_mad->class_version = 1;
  106. in_mad->method = IB_MGMT_METHOD_GET;
  107. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  108. in_mad->attr_mod = cpu_to_be32(port);
  109. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  110. port, NULL, NULL, in_mad, out_mad,
  111. &status);
  112. if (err)
  113. goto out;
  114. if (status) {
  115. err = -EINVAL;
  116. goto out;
  117. }
  118. props->lid = be16_to_cpup((u16 *) (out_mad->data + 16));
  119. props->lmc = out_mad->data[34] & 0x7;
  120. props->sm_lid = be16_to_cpup((u16 *) (out_mad->data + 18));
  121. props->sm_sl = out_mad->data[36] & 0xf;
  122. props->state = out_mad->data[32] & 0xf;
  123. props->phys_state = out_mad->data[33] >> 4;
  124. props->port_cap_flags = be32_to_cpup((u32 *) (out_mad->data + 20));
  125. props->gid_tbl_len = to_mdev(ibdev)->limits.gid_table_len;
  126. props->pkey_tbl_len = to_mdev(ibdev)->limits.pkey_table_len;
  127. props->qkey_viol_cntr = be16_to_cpup((u16 *) (out_mad->data + 48));
  128. props->active_width = out_mad->data[31] & 0xf;
  129. props->active_speed = out_mad->data[35] >> 4;
  130. out:
  131. kfree(in_mad);
  132. kfree(out_mad);
  133. return err;
  134. }
  135. static int mthca_modify_port(struct ib_device *ibdev,
  136. u8 port, int port_modify_mask,
  137. struct ib_port_modify *props)
  138. {
  139. struct mthca_set_ib_param set_ib;
  140. struct ib_port_attr attr;
  141. int err;
  142. u8 status;
  143. if (down_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
  144. return -ERESTARTSYS;
  145. err = mthca_query_port(ibdev, port, &attr);
  146. if (err)
  147. goto out;
  148. set_ib.set_si_guid = 0;
  149. set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
  150. set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  151. ~props->clr_port_cap_mask;
  152. err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
  153. if (err)
  154. goto out;
  155. if (status) {
  156. err = -EINVAL;
  157. goto out;
  158. }
  159. out:
  160. up(&to_mdev(ibdev)->cap_mask_mutex);
  161. return err;
  162. }
  163. static int mthca_query_pkey(struct ib_device *ibdev,
  164. u8 port, u16 index, u16 *pkey)
  165. {
  166. struct ib_smp *in_mad = NULL;
  167. struct ib_smp *out_mad = NULL;
  168. int err = -ENOMEM;
  169. u8 status;
  170. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  171. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  172. if (!in_mad || !out_mad)
  173. goto out;
  174. memset(in_mad, 0, sizeof *in_mad);
  175. in_mad->base_version = 1;
  176. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  177. in_mad->class_version = 1;
  178. in_mad->method = IB_MGMT_METHOD_GET;
  179. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  180. in_mad->attr_mod = cpu_to_be32(index / 32);
  181. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  182. port, NULL, NULL, in_mad, out_mad,
  183. &status);
  184. if (err)
  185. goto out;
  186. if (status) {
  187. err = -EINVAL;
  188. goto out;
  189. }
  190. *pkey = be16_to_cpu(((u16 *) out_mad->data)[index % 32]);
  191. out:
  192. kfree(in_mad);
  193. kfree(out_mad);
  194. return err;
  195. }
  196. static int mthca_query_gid(struct ib_device *ibdev, u8 port,
  197. int index, union ib_gid *gid)
  198. {
  199. struct ib_smp *in_mad = NULL;
  200. struct ib_smp *out_mad = NULL;
  201. int err = -ENOMEM;
  202. u8 status;
  203. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  204. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  205. if (!in_mad || !out_mad)
  206. goto out;
  207. memset(in_mad, 0, sizeof *in_mad);
  208. in_mad->base_version = 1;
  209. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  210. in_mad->class_version = 1;
  211. in_mad->method = IB_MGMT_METHOD_GET;
  212. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  213. in_mad->attr_mod = cpu_to_be32(port);
  214. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  215. port, NULL, NULL, in_mad, out_mad,
  216. &status);
  217. if (err)
  218. goto out;
  219. if (status) {
  220. err = -EINVAL;
  221. goto out;
  222. }
  223. memcpy(gid->raw, out_mad->data + 8, 8);
  224. memset(in_mad, 0, sizeof *in_mad);
  225. in_mad->base_version = 1;
  226. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  227. in_mad->class_version = 1;
  228. in_mad->method = IB_MGMT_METHOD_GET;
  229. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  230. in_mad->attr_mod = cpu_to_be32(index / 8);
  231. err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
  232. port, NULL, NULL, in_mad, out_mad,
  233. &status);
  234. if (err)
  235. goto out;
  236. if (status) {
  237. err = -EINVAL;
  238. goto out;
  239. }
  240. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
  241. out:
  242. kfree(in_mad);
  243. kfree(out_mad);
  244. return err;
  245. }
  246. static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
  247. struct ib_ucontext *context,
  248. struct ib_udata *udata)
  249. {
  250. struct mthca_pd *pd;
  251. int err;
  252. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  253. if (!pd)
  254. return ERR_PTR(-ENOMEM);
  255. err = mthca_pd_alloc(to_mdev(ibdev), pd);
  256. if (err) {
  257. kfree(pd);
  258. return ERR_PTR(err);
  259. }
  260. return &pd->ibpd;
  261. }
  262. static int mthca_dealloc_pd(struct ib_pd *pd)
  263. {
  264. mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
  265. kfree(pd);
  266. return 0;
  267. }
  268. static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
  269. struct ib_ah_attr *ah_attr)
  270. {
  271. int err;
  272. struct mthca_ah *ah;
  273. ah = kmalloc(sizeof *ah, GFP_ATOMIC);
  274. if (!ah)
  275. return ERR_PTR(-ENOMEM);
  276. err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
  277. if (err) {
  278. kfree(ah);
  279. return ERR_PTR(err);
  280. }
  281. return &ah->ibah;
  282. }
  283. static int mthca_ah_destroy(struct ib_ah *ah)
  284. {
  285. mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
  286. kfree(ah);
  287. return 0;
  288. }
  289. static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
  290. struct ib_qp_init_attr *init_attr,
  291. struct ib_udata *udata)
  292. {
  293. struct mthca_qp *qp;
  294. int err;
  295. switch (init_attr->qp_type) {
  296. case IB_QPT_RC:
  297. case IB_QPT_UC:
  298. case IB_QPT_UD:
  299. {
  300. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  301. if (!qp)
  302. return ERR_PTR(-ENOMEM);
  303. qp->sq.max = init_attr->cap.max_send_wr;
  304. qp->rq.max = init_attr->cap.max_recv_wr;
  305. qp->sq.max_gs = init_attr->cap.max_send_sge;
  306. qp->rq.max_gs = init_attr->cap.max_recv_sge;
  307. err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
  308. to_mcq(init_attr->send_cq),
  309. to_mcq(init_attr->recv_cq),
  310. init_attr->qp_type, init_attr->sq_sig_type,
  311. qp);
  312. qp->ibqp.qp_num = qp->qpn;
  313. break;
  314. }
  315. case IB_QPT_SMI:
  316. case IB_QPT_GSI:
  317. {
  318. qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
  319. if (!qp)
  320. return ERR_PTR(-ENOMEM);
  321. qp->sq.max = init_attr->cap.max_send_wr;
  322. qp->rq.max = init_attr->cap.max_recv_wr;
  323. qp->sq.max_gs = init_attr->cap.max_send_sge;
  324. qp->rq.max_gs = init_attr->cap.max_recv_sge;
  325. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  326. err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
  327. to_mcq(init_attr->send_cq),
  328. to_mcq(init_attr->recv_cq),
  329. init_attr->sq_sig_type,
  330. qp->ibqp.qp_num, init_attr->port_num,
  331. to_msqp(qp));
  332. break;
  333. }
  334. default:
  335. /* Don't support raw QPs */
  336. return ERR_PTR(-ENOSYS);
  337. }
  338. if (err) {
  339. kfree(qp);
  340. return ERR_PTR(err);
  341. }
  342. init_attr->cap.max_inline_data = 0;
  343. return &qp->ibqp;
  344. }
  345. static int mthca_destroy_qp(struct ib_qp *qp)
  346. {
  347. mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
  348. kfree(qp);
  349. return 0;
  350. }
  351. static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
  352. struct ib_ucontext *context,
  353. struct ib_udata *udata)
  354. {
  355. struct mthca_cq *cq;
  356. int nent;
  357. int err;
  358. cq = kmalloc(sizeof *cq, GFP_KERNEL);
  359. if (!cq)
  360. return ERR_PTR(-ENOMEM);
  361. for (nent = 1; nent <= entries; nent <<= 1)
  362. ; /* nothing */
  363. err = mthca_init_cq(to_mdev(ibdev), nent, cq);
  364. if (err) {
  365. kfree(cq);
  366. cq = ERR_PTR(err);
  367. }
  368. return &cq->ibcq;
  369. }
  370. static int mthca_destroy_cq(struct ib_cq *cq)
  371. {
  372. mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
  373. kfree(cq);
  374. return 0;
  375. }
  376. static inline u32 convert_access(int acc)
  377. {
  378. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
  379. (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
  380. (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
  381. (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
  382. MTHCA_MPT_FLAG_LOCAL_READ;
  383. }
  384. static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
  385. {
  386. struct mthca_mr *mr;
  387. int err;
  388. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  389. if (!mr)
  390. return ERR_PTR(-ENOMEM);
  391. err = mthca_mr_alloc_notrans(to_mdev(pd->device),
  392. to_mpd(pd)->pd_num,
  393. convert_access(acc), mr);
  394. if (err) {
  395. kfree(mr);
  396. return ERR_PTR(err);
  397. }
  398. return &mr->ibmr;
  399. }
  400. static struct ib_mr *mthca_reg_phys_mr(struct ib_pd *pd,
  401. struct ib_phys_buf *buffer_list,
  402. int num_phys_buf,
  403. int acc,
  404. u64 *iova_start)
  405. {
  406. struct mthca_mr *mr;
  407. u64 *page_list;
  408. u64 total_size;
  409. u64 mask;
  410. int shift;
  411. int npages;
  412. int err;
  413. int i, j, n;
  414. /* First check that we have enough alignment */
  415. if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
  416. return ERR_PTR(-EINVAL);
  417. if (num_phys_buf > 1 &&
  418. ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK))
  419. return ERR_PTR(-EINVAL);
  420. mask = 0;
  421. total_size = 0;
  422. for (i = 0; i < num_phys_buf; ++i) {
  423. if (i != 0 && buffer_list[i].addr & ~PAGE_MASK)
  424. return ERR_PTR(-EINVAL);
  425. if (i != 0 && i != num_phys_buf - 1 &&
  426. (buffer_list[i].size & ~PAGE_MASK))
  427. return ERR_PTR(-EINVAL);
  428. total_size += buffer_list[i].size;
  429. if (i > 0)
  430. mask |= buffer_list[i].addr;
  431. }
  432. /* Find largest page shift we can use to cover buffers */
  433. for (shift = PAGE_SHIFT; shift < 31; ++shift)
  434. if (num_phys_buf > 1) {
  435. if ((1ULL << shift) & mask)
  436. break;
  437. } else {
  438. if (1ULL << shift >=
  439. buffer_list[0].size +
  440. (buffer_list[0].addr & ((1ULL << shift) - 1)))
  441. break;
  442. }
  443. buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
  444. buffer_list[0].addr &= ~0ull << shift;
  445. mr = kmalloc(sizeof *mr, GFP_KERNEL);
  446. if (!mr)
  447. return ERR_PTR(-ENOMEM);
  448. npages = 0;
  449. for (i = 0; i < num_phys_buf; ++i)
  450. npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  451. if (!npages)
  452. return &mr->ibmr;
  453. page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
  454. if (!page_list) {
  455. kfree(mr);
  456. return ERR_PTR(-ENOMEM);
  457. }
  458. n = 0;
  459. for (i = 0; i < num_phys_buf; ++i)
  460. for (j = 0;
  461. j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
  462. ++j)
  463. page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
  464. mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
  465. "in PD %x; shift %d, npages %d.\n",
  466. (unsigned long long) buffer_list[0].addr,
  467. (unsigned long long) *iova_start,
  468. to_mpd(pd)->pd_num,
  469. shift, npages);
  470. err = mthca_mr_alloc_phys(to_mdev(pd->device),
  471. to_mpd(pd)->pd_num,
  472. page_list, shift, npages,
  473. *iova_start, total_size,
  474. convert_access(acc), mr);
  475. if (err) {
  476. kfree(page_list);
  477. kfree(mr);
  478. return ERR_PTR(err);
  479. }
  480. kfree(page_list);
  481. return &mr->ibmr;
  482. }
  483. static int mthca_dereg_mr(struct ib_mr *mr)
  484. {
  485. struct mthca_mr *mmr = to_mmr(mr);
  486. mthca_free_mr(to_mdev(mr->device), mmr);
  487. kfree(mmr);
  488. return 0;
  489. }
  490. static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  491. struct ib_fmr_attr *fmr_attr)
  492. {
  493. struct mthca_fmr *fmr;
  494. int err;
  495. fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
  496. if (!fmr)
  497. return ERR_PTR(-ENOMEM);
  498. memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
  499. err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
  500. convert_access(mr_access_flags), fmr);
  501. if (err) {
  502. kfree(fmr);
  503. return ERR_PTR(err);
  504. }
  505. return &fmr->ibmr;
  506. }
  507. static int mthca_dealloc_fmr(struct ib_fmr *fmr)
  508. {
  509. struct mthca_fmr *mfmr = to_mfmr(fmr);
  510. int err;
  511. err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
  512. if (err)
  513. return err;
  514. kfree(mfmr);
  515. return 0;
  516. }
  517. static int mthca_unmap_fmr(struct list_head *fmr_list)
  518. {
  519. struct ib_fmr *fmr;
  520. int err;
  521. u8 status;
  522. struct mthca_dev *mdev = NULL;
  523. list_for_each_entry(fmr, fmr_list, list) {
  524. if (mdev && to_mdev(fmr->device) != mdev)
  525. return -EINVAL;
  526. mdev = to_mdev(fmr->device);
  527. }
  528. if (!mdev)
  529. return 0;
  530. if (mthca_is_memfree(mdev)) {
  531. list_for_each_entry(fmr, fmr_list, list)
  532. mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
  533. wmb();
  534. } else
  535. list_for_each_entry(fmr, fmr_list, list)
  536. mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
  537. err = mthca_SYNC_TPT(mdev, &status);
  538. if (err)
  539. return err;
  540. if (status)
  541. return -EINVAL;
  542. return 0;
  543. }
  544. static ssize_t show_rev(struct class_device *cdev, char *buf)
  545. {
  546. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  547. return sprintf(buf, "%x\n", dev->rev_id);
  548. }
  549. static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
  550. {
  551. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  552. return sprintf(buf, "%x.%x.%x\n", (int) (dev->fw_ver >> 32),
  553. (int) (dev->fw_ver >> 16) & 0xffff,
  554. (int) dev->fw_ver & 0xffff);
  555. }
  556. static ssize_t show_hca(struct class_device *cdev, char *buf)
  557. {
  558. struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
  559. switch (dev->pdev->device) {
  560. case PCI_DEVICE_ID_MELLANOX_TAVOR:
  561. return sprintf(buf, "MT23108\n");
  562. case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
  563. return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
  564. case PCI_DEVICE_ID_MELLANOX_ARBEL:
  565. return sprintf(buf, "MT25208\n");
  566. case PCI_DEVICE_ID_MELLANOX_SINAI:
  567. case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
  568. return sprintf(buf, "MT25204\n");
  569. default:
  570. return sprintf(buf, "unknown\n");
  571. }
  572. }
  573. static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  574. static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  575. static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  576. static struct class_device_attribute *mthca_class_attributes[] = {
  577. &class_device_attr_hw_rev,
  578. &class_device_attr_fw_ver,
  579. &class_device_attr_hca_type
  580. };
  581. int mthca_register_device(struct mthca_dev *dev)
  582. {
  583. int ret;
  584. int i;
  585. strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
  586. dev->ib_dev.owner = THIS_MODULE;
  587. dev->ib_dev.node_type = IB_NODE_CA;
  588. dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
  589. dev->ib_dev.dma_device = &dev->pdev->dev;
  590. dev->ib_dev.class_dev.dev = &dev->pdev->dev;
  591. dev->ib_dev.query_device = mthca_query_device;
  592. dev->ib_dev.query_port = mthca_query_port;
  593. dev->ib_dev.modify_port = mthca_modify_port;
  594. dev->ib_dev.query_pkey = mthca_query_pkey;
  595. dev->ib_dev.query_gid = mthca_query_gid;
  596. dev->ib_dev.alloc_pd = mthca_alloc_pd;
  597. dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
  598. dev->ib_dev.create_ah = mthca_ah_create;
  599. dev->ib_dev.destroy_ah = mthca_ah_destroy;
  600. dev->ib_dev.create_qp = mthca_create_qp;
  601. dev->ib_dev.modify_qp = mthca_modify_qp;
  602. dev->ib_dev.destroy_qp = mthca_destroy_qp;
  603. dev->ib_dev.create_cq = mthca_create_cq;
  604. dev->ib_dev.destroy_cq = mthca_destroy_cq;
  605. dev->ib_dev.poll_cq = mthca_poll_cq;
  606. dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
  607. dev->ib_dev.reg_phys_mr = mthca_reg_phys_mr;
  608. dev->ib_dev.dereg_mr = mthca_dereg_mr;
  609. if (dev->mthca_flags & MTHCA_FLAG_FMR) {
  610. dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
  611. dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
  612. dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
  613. if (mthca_is_memfree(dev))
  614. dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
  615. else
  616. dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
  617. }
  618. dev->ib_dev.attach_mcast = mthca_multicast_attach;
  619. dev->ib_dev.detach_mcast = mthca_multicast_detach;
  620. dev->ib_dev.process_mad = mthca_process_mad;
  621. if (mthca_is_memfree(dev)) {
  622. dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
  623. dev->ib_dev.post_send = mthca_arbel_post_send;
  624. dev->ib_dev.post_recv = mthca_arbel_post_receive;
  625. } else {
  626. dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
  627. dev->ib_dev.post_send = mthca_tavor_post_send;
  628. dev->ib_dev.post_recv = mthca_tavor_post_receive;
  629. }
  630. init_MUTEX(&dev->cap_mask_mutex);
  631. ret = ib_register_device(&dev->ib_dev);
  632. if (ret)
  633. return ret;
  634. for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
  635. ret = class_device_create_file(&dev->ib_dev.class_dev,
  636. mthca_class_attributes[i]);
  637. if (ret) {
  638. ib_unregister_device(&dev->ib_dev);
  639. return ret;
  640. }
  641. }
  642. return 0;
  643. }
  644. void mthca_unregister_device(struct mthca_dev *dev)
  645. {
  646. ib_unregister_device(&dev->ib_dev);
  647. }