mthca_av.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2004 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. #include <linux/string.h>
  34. #include <linux/slab.h>
  35. #include <rdma/ib_verbs.h>
  36. #include <rdma/ib_cache.h>
  37. #include "mthca_dev.h"
  38. enum {
  39. MTHCA_RATE_TAVOR_FULL = 0,
  40. MTHCA_RATE_TAVOR_1X = 1,
  41. MTHCA_RATE_TAVOR_4X = 2,
  42. MTHCA_RATE_TAVOR_1X_DDR = 3
  43. };
  44. enum {
  45. MTHCA_RATE_MEMFREE_FULL = 0,
  46. MTHCA_RATE_MEMFREE_QUARTER = 1,
  47. MTHCA_RATE_MEMFREE_EIGHTH = 2,
  48. MTHCA_RATE_MEMFREE_HALF = 3
  49. };
  50. struct mthca_av {
  51. __be32 port_pd;
  52. u8 reserved1;
  53. u8 g_slid;
  54. __be16 dlid;
  55. u8 reserved2;
  56. u8 gid_index;
  57. u8 msg_sr;
  58. u8 hop_limit;
  59. __be32 sl_tclass_flowlabel;
  60. __be32 dgid[4];
  61. };
  62. static enum ib_rate memfree_rate_to_ib(u8 mthca_rate, u8 port_rate)
  63. {
  64. switch (mthca_rate) {
  65. case MTHCA_RATE_MEMFREE_EIGHTH:
  66. return mult_to_ib_rate(port_rate >> 3);
  67. case MTHCA_RATE_MEMFREE_QUARTER:
  68. return mult_to_ib_rate(port_rate >> 2);
  69. case MTHCA_RATE_MEMFREE_HALF:
  70. return mult_to_ib_rate(port_rate >> 1);
  71. case MTHCA_RATE_MEMFREE_FULL:
  72. default:
  73. return mult_to_ib_rate(port_rate);
  74. }
  75. }
  76. static enum ib_rate tavor_rate_to_ib(u8 mthca_rate, u8 port_rate)
  77. {
  78. switch (mthca_rate) {
  79. case MTHCA_RATE_TAVOR_1X: return IB_RATE_2_5_GBPS;
  80. case MTHCA_RATE_TAVOR_1X_DDR: return IB_RATE_5_GBPS;
  81. case MTHCA_RATE_TAVOR_4X: return IB_RATE_10_GBPS;
  82. default: return mult_to_ib_rate(port_rate);
  83. }
  84. }
  85. enum ib_rate mthca_rate_to_ib(struct mthca_dev *dev, u8 mthca_rate, u8 port)
  86. {
  87. if (mthca_is_memfree(dev)) {
  88. /* Handle old Arbel FW */
  89. if (dev->limits.stat_rate_support == 0x3 && mthca_rate)
  90. return IB_RATE_2_5_GBPS;
  91. return memfree_rate_to_ib(mthca_rate, dev->rate[port - 1]);
  92. } else
  93. return tavor_rate_to_ib(mthca_rate, dev->rate[port - 1]);
  94. }
  95. static u8 ib_rate_to_memfree(u8 req_rate, u8 cur_rate)
  96. {
  97. if (cur_rate <= req_rate)
  98. return 0;
  99. /*
  100. * Inter-packet delay (IPD) to get from rate X down to a rate
  101. * no more than Y is (X - 1) / Y.
  102. */
  103. switch ((cur_rate - 1) / req_rate) {
  104. case 0: return MTHCA_RATE_MEMFREE_FULL;
  105. case 1: return MTHCA_RATE_MEMFREE_HALF;
  106. case 2: /* fall through */
  107. case 3: return MTHCA_RATE_MEMFREE_QUARTER;
  108. default: return MTHCA_RATE_MEMFREE_EIGHTH;
  109. }
  110. }
  111. static u8 ib_rate_to_tavor(u8 static_rate)
  112. {
  113. switch (static_rate) {
  114. case IB_RATE_2_5_GBPS: return MTHCA_RATE_TAVOR_1X;
  115. case IB_RATE_5_GBPS: return MTHCA_RATE_TAVOR_1X_DDR;
  116. case IB_RATE_10_GBPS: return MTHCA_RATE_TAVOR_4X;
  117. default: return MTHCA_RATE_TAVOR_FULL;
  118. }
  119. }
  120. u8 mthca_get_rate(struct mthca_dev *dev, int static_rate, u8 port)
  121. {
  122. u8 rate;
  123. if (!static_rate || ib_rate_to_mult(static_rate) >= dev->rate[port - 1])
  124. return 0;
  125. if (mthca_is_memfree(dev))
  126. rate = ib_rate_to_memfree(ib_rate_to_mult(static_rate),
  127. dev->rate[port - 1]);
  128. else
  129. rate = ib_rate_to_tavor(static_rate);
  130. if (!(dev->limits.stat_rate_support & (1 << rate)))
  131. rate = 1;
  132. return rate;
  133. }
  134. int mthca_create_ah(struct mthca_dev *dev,
  135. struct mthca_pd *pd,
  136. struct rdma_ah_attr *ah_attr,
  137. struct mthca_ah *ah)
  138. {
  139. u32 index = -1;
  140. struct mthca_av *av = NULL;
  141. ah->type = MTHCA_AH_PCI_POOL;
  142. if (mthca_is_memfree(dev)) {
  143. ah->av = kmalloc(sizeof *ah->av, GFP_ATOMIC);
  144. if (!ah->av)
  145. return -ENOMEM;
  146. ah->type = MTHCA_AH_KMALLOC;
  147. av = ah->av;
  148. } else if (!atomic_read(&pd->sqp_count) &&
  149. !(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
  150. index = mthca_alloc(&dev->av_table.alloc);
  151. /* fall back to allocate in host memory */
  152. if (index == -1)
  153. goto on_hca_fail;
  154. av = kmalloc(sizeof *av, GFP_ATOMIC);
  155. if (!av)
  156. goto on_hca_fail;
  157. ah->type = MTHCA_AH_ON_HCA;
  158. ah->avdma = dev->av_table.ddr_av_base +
  159. index * MTHCA_AV_SIZE;
  160. }
  161. on_hca_fail:
  162. if (ah->type == MTHCA_AH_PCI_POOL) {
  163. ah->av = dma_pool_zalloc(dev->av_table.pool,
  164. GFP_ATOMIC, &ah->avdma);
  165. if (!ah->av)
  166. return -ENOMEM;
  167. av = ah->av;
  168. }
  169. ah->key = pd->ntmr.ibmr.lkey;
  170. av->port_pd = cpu_to_be32(pd->pd_num |
  171. (rdma_ah_get_port_num(ah_attr) << 24));
  172. av->g_slid = rdma_ah_get_path_bits(ah_attr);
  173. av->dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr));
  174. av->msg_sr = (3 << 4) | /* 2K message */
  175. mthca_get_rate(dev, rdma_ah_get_static_rate(ah_attr),
  176. rdma_ah_get_port_num(ah_attr));
  177. av->sl_tclass_flowlabel = cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28);
  178. if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
  179. const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
  180. av->g_slid |= 0x80;
  181. av->gid_index = (rdma_ah_get_port_num(ah_attr) - 1) *
  182. dev->limits.gid_table_len +
  183. grh->sgid_index;
  184. av->hop_limit = grh->hop_limit;
  185. av->sl_tclass_flowlabel |=
  186. cpu_to_be32((grh->traffic_class << 20) |
  187. grh->flow_label);
  188. memcpy(av->dgid, grh->dgid.raw, 16);
  189. } else {
  190. /* Arbel workaround -- low byte of GID must be 2 */
  191. av->dgid[3] = cpu_to_be32(2);
  192. }
  193. if (0) {
  194. int j;
  195. mthca_dbg(dev, "Created UDAV at %p/%08lx:\n",
  196. av, (unsigned long) ah->avdma);
  197. for (j = 0; j < 8; ++j)
  198. printk(KERN_DEBUG " [%2x] %08x\n",
  199. j * 4, be32_to_cpu(((__be32 *) av)[j]));
  200. }
  201. if (ah->type == MTHCA_AH_ON_HCA) {
  202. memcpy_toio(dev->av_table.av_map + index * MTHCA_AV_SIZE,
  203. av, MTHCA_AV_SIZE);
  204. kfree(av);
  205. }
  206. return 0;
  207. }
  208. int mthca_destroy_ah(struct mthca_dev *dev, struct mthca_ah *ah)
  209. {
  210. switch (ah->type) {
  211. case MTHCA_AH_ON_HCA:
  212. mthca_free(&dev->av_table.alloc,
  213. (ah->avdma - dev->av_table.ddr_av_base) /
  214. MTHCA_AV_SIZE);
  215. break;
  216. case MTHCA_AH_PCI_POOL:
  217. dma_pool_free(dev->av_table.pool, ah->av, ah->avdma);
  218. break;
  219. case MTHCA_AH_KMALLOC:
  220. kfree(ah->av);
  221. break;
  222. }
  223. return 0;
  224. }
  225. int mthca_ah_grh_present(struct mthca_ah *ah)
  226. {
  227. return !!(ah->av->g_slid & 0x80);
  228. }
  229. int mthca_read_ah(struct mthca_dev *dev, struct mthca_ah *ah,
  230. struct ib_ud_header *header)
  231. {
  232. if (ah->type == MTHCA_AH_ON_HCA)
  233. return -EINVAL;
  234. header->lrh.service_level = be32_to_cpu(ah->av->sl_tclass_flowlabel) >> 28;
  235. header->lrh.destination_lid = ah->av->dlid;
  236. header->lrh.source_lid = cpu_to_be16(ah->av->g_slid & 0x7f);
  237. if (mthca_ah_grh_present(ah)) {
  238. header->grh.traffic_class =
  239. (be32_to_cpu(ah->av->sl_tclass_flowlabel) >> 20) & 0xff;
  240. header->grh.flow_label =
  241. ah->av->sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  242. header->grh.hop_limit = ah->av->hop_limit;
  243. ib_get_cached_gid(&dev->ib_dev,
  244. be32_to_cpu(ah->av->port_pd) >> 24,
  245. ah->av->gid_index % dev->limits.gid_table_len,
  246. &header->grh.source_gid, NULL);
  247. memcpy(header->grh.destination_gid.raw,
  248. ah->av->dgid, 16);
  249. }
  250. return 0;
  251. }
  252. int mthca_ah_query(struct ib_ah *ibah, struct rdma_ah_attr *attr)
  253. {
  254. struct mthca_ah *ah = to_mah(ibah);
  255. struct mthca_dev *dev = to_mdev(ibah->device);
  256. u8 port_num = be32_to_cpu(ah->av->port_pd) >> 24;
  257. /* Only implement for MAD and memfree ah for now. */
  258. if (ah->type == MTHCA_AH_ON_HCA)
  259. return -ENOSYS;
  260. memset(attr, 0, sizeof *attr);
  261. attr->type = ibah->type;
  262. rdma_ah_set_dlid(attr, be16_to_cpu(ah->av->dlid));
  263. rdma_ah_set_sl(attr, be32_to_cpu(ah->av->sl_tclass_flowlabel) >> 28);
  264. rdma_ah_set_port_num(attr, port_num);
  265. rdma_ah_set_static_rate(attr,
  266. mthca_rate_to_ib(dev, ah->av->msg_sr & 0x7,
  267. port_num));
  268. rdma_ah_set_path_bits(attr, ah->av->g_slid & 0x7F);
  269. if (mthca_ah_grh_present(ah)) {
  270. u32 tc_fl = be32_to_cpu(ah->av->sl_tclass_flowlabel);
  271. rdma_ah_set_grh(attr, NULL,
  272. tc_fl & 0xfffff,
  273. ah->av->gid_index &
  274. (dev->limits.gid_table_len - 1),
  275. ah->av->hop_limit,
  276. (tc_fl >> 20) & 0xff);
  277. rdma_ah_set_dgid_raw(attr, ah->av->dgid);
  278. }
  279. return 0;
  280. }
  281. int mthca_init_av_table(struct mthca_dev *dev)
  282. {
  283. int err;
  284. if (mthca_is_memfree(dev))
  285. return 0;
  286. err = mthca_alloc_init(&dev->av_table.alloc,
  287. dev->av_table.num_ddr_avs,
  288. dev->av_table.num_ddr_avs - 1,
  289. 0);
  290. if (err)
  291. return err;
  292. dev->av_table.pool = dma_pool_create("mthca_av", &dev->pdev->dev,
  293. MTHCA_AV_SIZE,
  294. MTHCA_AV_SIZE, 0);
  295. if (!dev->av_table.pool)
  296. goto out_free_alloc;
  297. if (!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
  298. dev->av_table.av_map = ioremap(pci_resource_start(dev->pdev, 4) +
  299. dev->av_table.ddr_av_base -
  300. dev->ddr_start,
  301. dev->av_table.num_ddr_avs *
  302. MTHCA_AV_SIZE);
  303. if (!dev->av_table.av_map)
  304. goto out_free_pool;
  305. } else
  306. dev->av_table.av_map = NULL;
  307. return 0;
  308. out_free_pool:
  309. dma_pool_destroy(dev->av_table.pool);
  310. out_free_alloc:
  311. mthca_alloc_cleanup(&dev->av_table.alloc);
  312. return -ENOMEM;
  313. }
  314. void mthca_cleanup_av_table(struct mthca_dev *dev)
  315. {
  316. if (mthca_is_memfree(dev))
  317. return;
  318. if (dev->av_table.av_map)
  319. iounmap(dev->av_table.av_map);
  320. dma_pool_destroy(dev->av_table.pool);
  321. mthca_alloc_cleanup(&dev->av_table.alloc);
  322. }