main.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. 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/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/errno.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/inetdevice.h>
  39. #include <linux/rtnetlink.h>
  40. #include <linux/if_vlan.h>
  41. #include <rdma/ib_smi.h>
  42. #include <rdma/ib_user_verbs.h>
  43. #include <rdma/ib_addr.h>
  44. #include <linux/mlx4/driver.h>
  45. #include <linux/mlx4/cmd.h>
  46. #include "mlx4_ib.h"
  47. #include "user.h"
  48. #define DRV_NAME MLX4_IB_DRV_NAME
  49. #define DRV_VERSION "1.0"
  50. #define DRV_RELDATE "April 4, 2008"
  51. MODULE_AUTHOR("Roland Dreier");
  52. MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
  53. MODULE_LICENSE("Dual BSD/GPL");
  54. MODULE_VERSION(DRV_VERSION);
  55. static const char mlx4_ib_version[] =
  56. DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
  57. DRV_VERSION " (" DRV_RELDATE ")\n";
  58. struct update_gid_work {
  59. struct work_struct work;
  60. union ib_gid gids[128];
  61. struct mlx4_ib_dev *dev;
  62. int port;
  63. };
  64. static struct workqueue_struct *wq;
  65. static void init_query_mad(struct ib_smp *mad)
  66. {
  67. mad->base_version = 1;
  68. mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  69. mad->class_version = 1;
  70. mad->method = IB_MGMT_METHOD_GET;
  71. }
  72. static union ib_gid zgid;
  73. static int mlx4_ib_query_device(struct ib_device *ibdev,
  74. struct ib_device_attr *props)
  75. {
  76. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  77. struct ib_smp *in_mad = NULL;
  78. struct ib_smp *out_mad = NULL;
  79. int err = -ENOMEM;
  80. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  81. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  82. if (!in_mad || !out_mad)
  83. goto out;
  84. init_query_mad(in_mad);
  85. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  86. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);
  87. if (err)
  88. goto out;
  89. memset(props, 0, sizeof *props);
  90. props->fw_ver = dev->dev->caps.fw_ver;
  91. props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  92. IB_DEVICE_PORT_ACTIVE_EVENT |
  93. IB_DEVICE_SYS_IMAGE_GUID |
  94. IB_DEVICE_RC_RNR_NAK_GEN |
  95. IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  96. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
  97. props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  98. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
  99. props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  100. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
  101. props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  102. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
  103. props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
  104. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
  105. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  106. if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
  107. props->device_cap_flags |= IB_DEVICE_UD_TSO;
  108. if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
  109. props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
  110. if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
  111. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
  112. (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
  113. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  114. if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
  115. props->device_cap_flags |= IB_DEVICE_XRC;
  116. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  117. 0xffffff;
  118. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  119. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  120. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  121. props->max_mr_size = ~0ull;
  122. props->page_size_cap = dev->dev->caps.page_size_cap;
  123. props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
  124. props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
  125. props->max_sge = min(dev->dev->caps.max_sq_sg,
  126. dev->dev->caps.max_rq_sg);
  127. props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
  128. props->max_cqe = dev->dev->caps.max_cqes;
  129. props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
  130. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  131. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  132. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  133. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  134. props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
  135. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  136. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  137. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  138. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  139. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  140. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  141. props->masked_atomic_cap = props->atomic_cap;
  142. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  143. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  144. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  145. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  146. props->max_mcast_grp;
  147. props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
  148. out:
  149. kfree(in_mad);
  150. kfree(out_mad);
  151. return err;
  152. }
  153. static enum rdma_link_layer
  154. mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
  155. {
  156. struct mlx4_dev *dev = to_mdev(device)->dev;
  157. return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
  158. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  159. }
  160. static int ib_link_query_port(struct ib_device *ibdev, u8 port,
  161. struct ib_port_attr *props)
  162. {
  163. struct ib_smp *in_mad = NULL;
  164. struct ib_smp *out_mad = NULL;
  165. int ext_active_speed;
  166. int err = -ENOMEM;
  167. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  168. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  169. if (!in_mad || !out_mad)
  170. goto out;
  171. init_query_mad(in_mad);
  172. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  173. in_mad->attr_mod = cpu_to_be32(port);
  174. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL,
  175. in_mad, out_mad);
  176. if (err)
  177. goto out;
  178. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  179. props->lmc = out_mad->data[34] & 0x7;
  180. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  181. props->sm_sl = out_mad->data[36] & 0xf;
  182. props->state = out_mad->data[32] & 0xf;
  183. props->phys_state = out_mad->data[33] >> 4;
  184. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  185. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  186. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  187. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  188. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  189. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  190. props->active_width = out_mad->data[31] & 0xf;
  191. props->active_speed = out_mad->data[35] >> 4;
  192. props->max_mtu = out_mad->data[41] & 0xf;
  193. props->active_mtu = out_mad->data[36] >> 4;
  194. props->subnet_timeout = out_mad->data[51] & 0x1f;
  195. props->max_vl_num = out_mad->data[37] >> 4;
  196. props->init_type_reply = out_mad->data[41] >> 4;
  197. /* Check if extended speeds (EDR/FDR/...) are supported */
  198. if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
  199. ext_active_speed = out_mad->data[62] >> 4;
  200. switch (ext_active_speed) {
  201. case 1:
  202. props->active_speed = IB_SPEED_FDR;
  203. break;
  204. case 2:
  205. props->active_speed = IB_SPEED_EDR;
  206. break;
  207. }
  208. }
  209. /* If reported active speed is QDR, check if is FDR-10 */
  210. if (props->active_speed == IB_SPEED_QDR) {
  211. init_query_mad(in_mad);
  212. in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
  213. in_mad->attr_mod = cpu_to_be32(port);
  214. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port,
  215. NULL, NULL, in_mad, out_mad);
  216. if (err)
  217. goto out;
  218. /* Checking LinkSpeedActive for FDR-10 */
  219. if (out_mad->data[15] & 0x1)
  220. props->active_speed = IB_SPEED_FDR10;
  221. }
  222. /* Avoid wrong speed value returned by FW if the IB link is down. */
  223. if (props->state == IB_PORT_DOWN)
  224. props->active_speed = IB_SPEED_SDR;
  225. out:
  226. kfree(in_mad);
  227. kfree(out_mad);
  228. return err;
  229. }
  230. static u8 state_to_phys_state(enum ib_port_state state)
  231. {
  232. return state == IB_PORT_ACTIVE ? 5 : 3;
  233. }
  234. static int eth_link_query_port(struct ib_device *ibdev, u8 port,
  235. struct ib_port_attr *props)
  236. {
  237. struct mlx4_ib_dev *mdev = to_mdev(ibdev);
  238. struct mlx4_ib_iboe *iboe = &mdev->iboe;
  239. struct net_device *ndev;
  240. enum ib_mtu tmp;
  241. struct mlx4_cmd_mailbox *mailbox;
  242. int err = 0;
  243. mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
  244. if (IS_ERR(mailbox))
  245. return PTR_ERR(mailbox);
  246. err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
  247. MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
  248. MLX4_CMD_WRAPPED);
  249. if (err)
  250. goto out;
  251. props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ?
  252. IB_WIDTH_4X : IB_WIDTH_1X;
  253. props->active_speed = IB_SPEED_QDR;
  254. props->port_cap_flags = IB_PORT_CM_SUP;
  255. props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
  256. props->max_msg_sz = mdev->dev->caps.max_msg_sz;
  257. props->pkey_tbl_len = 1;
  258. props->max_mtu = IB_MTU_4096;
  259. props->max_vl_num = 2;
  260. props->state = IB_PORT_DOWN;
  261. props->phys_state = state_to_phys_state(props->state);
  262. props->active_mtu = IB_MTU_256;
  263. spin_lock(&iboe->lock);
  264. ndev = iboe->netdevs[port - 1];
  265. if (!ndev)
  266. goto out_unlock;
  267. tmp = iboe_get_mtu(ndev->mtu);
  268. props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
  269. props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
  270. IB_PORT_ACTIVE : IB_PORT_DOWN;
  271. props->phys_state = state_to_phys_state(props->state);
  272. out_unlock:
  273. spin_unlock(&iboe->lock);
  274. out:
  275. mlx4_free_cmd_mailbox(mdev->dev, mailbox);
  276. return err;
  277. }
  278. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  279. struct ib_port_attr *props)
  280. {
  281. int err;
  282. memset(props, 0, sizeof *props);
  283. err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
  284. ib_link_query_port(ibdev, port, props) :
  285. eth_link_query_port(ibdev, port, props);
  286. return err;
  287. }
  288. static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  289. union ib_gid *gid)
  290. {
  291. struct ib_smp *in_mad = NULL;
  292. struct ib_smp *out_mad = NULL;
  293. int err = -ENOMEM;
  294. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  295. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  296. if (!in_mad || !out_mad)
  297. goto out;
  298. init_query_mad(in_mad);
  299. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  300. in_mad->attr_mod = cpu_to_be32(port);
  301. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  302. if (err)
  303. goto out;
  304. memcpy(gid->raw, out_mad->data + 8, 8);
  305. init_query_mad(in_mad);
  306. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  307. in_mad->attr_mod = cpu_to_be32(index / 8);
  308. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  309. if (err)
  310. goto out;
  311. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  312. out:
  313. kfree(in_mad);
  314. kfree(out_mad);
  315. return err;
  316. }
  317. static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
  318. union ib_gid *gid)
  319. {
  320. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  321. *gid = dev->iboe.gid_table[port - 1][index];
  322. return 0;
  323. }
  324. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  325. union ib_gid *gid)
  326. {
  327. if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
  328. return __mlx4_ib_query_gid(ibdev, port, index, gid);
  329. else
  330. return iboe_query_gid(ibdev, port, index, gid);
  331. }
  332. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  333. u16 *pkey)
  334. {
  335. struct ib_smp *in_mad = NULL;
  336. struct ib_smp *out_mad = NULL;
  337. int err = -ENOMEM;
  338. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  339. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  340. if (!in_mad || !out_mad)
  341. goto out;
  342. init_query_mad(in_mad);
  343. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  344. in_mad->attr_mod = cpu_to_be32(index / 32);
  345. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  346. if (err)
  347. goto out;
  348. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  349. out:
  350. kfree(in_mad);
  351. kfree(out_mad);
  352. return err;
  353. }
  354. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  355. struct ib_device_modify *props)
  356. {
  357. struct mlx4_cmd_mailbox *mailbox;
  358. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  359. return -EOPNOTSUPP;
  360. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  361. return 0;
  362. spin_lock(&to_mdev(ibdev)->sm_lock);
  363. memcpy(ibdev->node_desc, props->node_desc, 64);
  364. spin_unlock(&to_mdev(ibdev)->sm_lock);
  365. /*
  366. * If possible, pass node desc to FW, so it can generate
  367. * a 144 trap. If cmd fails, just ignore.
  368. */
  369. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  370. if (IS_ERR(mailbox))
  371. return 0;
  372. memset(mailbox->buf, 0, 256);
  373. memcpy(mailbox->buf, props->node_desc, 64);
  374. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  375. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  376. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  377. return 0;
  378. }
  379. static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  380. u32 cap_mask)
  381. {
  382. struct mlx4_cmd_mailbox *mailbox;
  383. int err;
  384. u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  385. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  386. if (IS_ERR(mailbox))
  387. return PTR_ERR(mailbox);
  388. memset(mailbox->buf, 0, 256);
  389. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  390. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  391. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  392. } else {
  393. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  394. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  395. }
  396. err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  397. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  398. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  399. return err;
  400. }
  401. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  402. struct ib_port_modify *props)
  403. {
  404. struct ib_port_attr attr;
  405. u32 cap_mask;
  406. int err;
  407. mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
  408. err = mlx4_ib_query_port(ibdev, port, &attr);
  409. if (err)
  410. goto out;
  411. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  412. ~props->clr_port_cap_mask;
  413. err = mlx4_SET_PORT(to_mdev(ibdev), port,
  414. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  415. cap_mask);
  416. out:
  417. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  418. return err;
  419. }
  420. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  421. struct ib_udata *udata)
  422. {
  423. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  424. struct mlx4_ib_ucontext *context;
  425. struct mlx4_ib_alloc_ucontext_resp resp;
  426. int err;
  427. if (!dev->ib_active)
  428. return ERR_PTR(-EAGAIN);
  429. resp.qp_tab_size = dev->dev->caps.num_qps;
  430. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  431. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  432. context = kmalloc(sizeof *context, GFP_KERNEL);
  433. if (!context)
  434. return ERR_PTR(-ENOMEM);
  435. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  436. if (err) {
  437. kfree(context);
  438. return ERR_PTR(err);
  439. }
  440. INIT_LIST_HEAD(&context->db_page_list);
  441. mutex_init(&context->db_page_mutex);
  442. err = ib_copy_to_udata(udata, &resp, sizeof resp);
  443. if (err) {
  444. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  445. kfree(context);
  446. return ERR_PTR(-EFAULT);
  447. }
  448. return &context->ibucontext;
  449. }
  450. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  451. {
  452. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  453. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  454. kfree(context);
  455. return 0;
  456. }
  457. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  458. {
  459. struct mlx4_ib_dev *dev = to_mdev(context->device);
  460. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  461. return -EINVAL;
  462. if (vma->vm_pgoff == 0) {
  463. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  464. if (io_remap_pfn_range(vma, vma->vm_start,
  465. to_mucontext(context)->uar.pfn,
  466. PAGE_SIZE, vma->vm_page_prot))
  467. return -EAGAIN;
  468. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  469. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  470. if (io_remap_pfn_range(vma, vma->vm_start,
  471. to_mucontext(context)->uar.pfn +
  472. dev->dev->caps.num_uars,
  473. PAGE_SIZE, vma->vm_page_prot))
  474. return -EAGAIN;
  475. } else
  476. return -EINVAL;
  477. return 0;
  478. }
  479. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  480. struct ib_ucontext *context,
  481. struct ib_udata *udata)
  482. {
  483. struct mlx4_ib_pd *pd;
  484. int err;
  485. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  486. if (!pd)
  487. return ERR_PTR(-ENOMEM);
  488. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  489. if (err) {
  490. kfree(pd);
  491. return ERR_PTR(err);
  492. }
  493. if (context)
  494. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  495. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  496. kfree(pd);
  497. return ERR_PTR(-EFAULT);
  498. }
  499. return &pd->ibpd;
  500. }
  501. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  502. {
  503. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  504. kfree(pd);
  505. return 0;
  506. }
  507. static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
  508. struct ib_ucontext *context,
  509. struct ib_udata *udata)
  510. {
  511. struct mlx4_ib_xrcd *xrcd;
  512. int err;
  513. if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  514. return ERR_PTR(-ENOSYS);
  515. xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
  516. if (!xrcd)
  517. return ERR_PTR(-ENOMEM);
  518. err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
  519. if (err)
  520. goto err1;
  521. xrcd->pd = ib_alloc_pd(ibdev);
  522. if (IS_ERR(xrcd->pd)) {
  523. err = PTR_ERR(xrcd->pd);
  524. goto err2;
  525. }
  526. xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
  527. if (IS_ERR(xrcd->cq)) {
  528. err = PTR_ERR(xrcd->cq);
  529. goto err3;
  530. }
  531. return &xrcd->ibxrcd;
  532. err3:
  533. ib_dealloc_pd(xrcd->pd);
  534. err2:
  535. mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
  536. err1:
  537. kfree(xrcd);
  538. return ERR_PTR(err);
  539. }
  540. static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  541. {
  542. ib_destroy_cq(to_mxrcd(xrcd)->cq);
  543. ib_dealloc_pd(to_mxrcd(xrcd)->pd);
  544. mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
  545. kfree(xrcd);
  546. return 0;
  547. }
  548. static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
  549. {
  550. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  551. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  552. struct mlx4_ib_gid_entry *ge;
  553. ge = kzalloc(sizeof *ge, GFP_KERNEL);
  554. if (!ge)
  555. return -ENOMEM;
  556. ge->gid = *gid;
  557. if (mlx4_ib_add_mc(mdev, mqp, gid)) {
  558. ge->port = mqp->port;
  559. ge->added = 1;
  560. }
  561. mutex_lock(&mqp->mutex);
  562. list_add_tail(&ge->list, &mqp->gid_list);
  563. mutex_unlock(&mqp->mutex);
  564. return 0;
  565. }
  566. int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  567. union ib_gid *gid)
  568. {
  569. u8 mac[6];
  570. struct net_device *ndev;
  571. int ret = 0;
  572. if (!mqp->port)
  573. return 0;
  574. spin_lock(&mdev->iboe.lock);
  575. ndev = mdev->iboe.netdevs[mqp->port - 1];
  576. if (ndev)
  577. dev_hold(ndev);
  578. spin_unlock(&mdev->iboe.lock);
  579. if (ndev) {
  580. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  581. rtnl_lock();
  582. dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
  583. ret = 1;
  584. rtnl_unlock();
  585. dev_put(ndev);
  586. }
  587. return ret;
  588. }
  589. struct mlx4_ib_steering {
  590. struct list_head list;
  591. u64 reg_id;
  592. union ib_gid gid;
  593. };
  594. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  595. {
  596. int err;
  597. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  598. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  599. u64 reg_id;
  600. struct mlx4_ib_steering *ib_steering = NULL;
  601. if (mdev->dev->caps.steering_mode ==
  602. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  603. ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
  604. if (!ib_steering)
  605. return -ENOMEM;
  606. }
  607. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
  608. !!(mqp->flags &
  609. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
  610. MLX4_PROT_IB_IPV6, &reg_id);
  611. if (err)
  612. goto err_malloc;
  613. err = add_gid_entry(ibqp, gid);
  614. if (err)
  615. goto err_add;
  616. if (ib_steering) {
  617. memcpy(ib_steering->gid.raw, gid->raw, 16);
  618. ib_steering->reg_id = reg_id;
  619. mutex_lock(&mqp->mutex);
  620. list_add(&ib_steering->list, &mqp->steering_rules);
  621. mutex_unlock(&mqp->mutex);
  622. }
  623. return 0;
  624. err_add:
  625. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  626. MLX4_PROT_IB_IPV6, reg_id);
  627. err_malloc:
  628. kfree(ib_steering);
  629. return err;
  630. }
  631. static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
  632. {
  633. struct mlx4_ib_gid_entry *ge;
  634. struct mlx4_ib_gid_entry *tmp;
  635. struct mlx4_ib_gid_entry *ret = NULL;
  636. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  637. if (!memcmp(raw, ge->gid.raw, 16)) {
  638. ret = ge;
  639. break;
  640. }
  641. }
  642. return ret;
  643. }
  644. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  645. {
  646. int err;
  647. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  648. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  649. u8 mac[6];
  650. struct net_device *ndev;
  651. struct mlx4_ib_gid_entry *ge;
  652. u64 reg_id = 0;
  653. if (mdev->dev->caps.steering_mode ==
  654. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  655. struct mlx4_ib_steering *ib_steering;
  656. mutex_lock(&mqp->mutex);
  657. list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
  658. if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
  659. list_del(&ib_steering->list);
  660. break;
  661. }
  662. }
  663. mutex_unlock(&mqp->mutex);
  664. if (&ib_steering->list == &mqp->steering_rules) {
  665. pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
  666. return -EINVAL;
  667. }
  668. reg_id = ib_steering->reg_id;
  669. kfree(ib_steering);
  670. }
  671. err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
  672. MLX4_PROT_IB_IPV6, reg_id);
  673. if (err)
  674. return err;
  675. mutex_lock(&mqp->mutex);
  676. ge = find_gid_entry(mqp, gid->raw);
  677. if (ge) {
  678. spin_lock(&mdev->iboe.lock);
  679. ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
  680. if (ndev)
  681. dev_hold(ndev);
  682. spin_unlock(&mdev->iboe.lock);
  683. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  684. if (ndev) {
  685. rtnl_lock();
  686. dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
  687. rtnl_unlock();
  688. dev_put(ndev);
  689. }
  690. list_del(&ge->list);
  691. kfree(ge);
  692. } else
  693. pr_warn("could not find mgid entry\n");
  694. mutex_unlock(&mqp->mutex);
  695. return 0;
  696. }
  697. static int init_node_data(struct mlx4_ib_dev *dev)
  698. {
  699. struct ib_smp *in_mad = NULL;
  700. struct ib_smp *out_mad = NULL;
  701. int err = -ENOMEM;
  702. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  703. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  704. if (!in_mad || !out_mad)
  705. goto out;
  706. init_query_mad(in_mad);
  707. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  708. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  709. if (err)
  710. goto out;
  711. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  712. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  713. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  714. if (err)
  715. goto out;
  716. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  717. out:
  718. kfree(in_mad);
  719. kfree(out_mad);
  720. return err;
  721. }
  722. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  723. char *buf)
  724. {
  725. struct mlx4_ib_dev *dev =
  726. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  727. return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
  728. }
  729. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  730. char *buf)
  731. {
  732. struct mlx4_ib_dev *dev =
  733. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  734. return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
  735. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  736. (int) dev->dev->caps.fw_ver & 0xffff);
  737. }
  738. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  739. char *buf)
  740. {
  741. struct mlx4_ib_dev *dev =
  742. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  743. return sprintf(buf, "%x\n", dev->dev->rev_id);
  744. }
  745. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  746. char *buf)
  747. {
  748. struct mlx4_ib_dev *dev =
  749. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  750. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  751. dev->dev->board_id);
  752. }
  753. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  754. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  755. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  756. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  757. static struct device_attribute *mlx4_class_attributes[] = {
  758. &dev_attr_hw_rev,
  759. &dev_attr_fw_ver,
  760. &dev_attr_hca_type,
  761. &dev_attr_board_id
  762. };
  763. static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
  764. {
  765. memcpy(eui, dev->dev_addr, 3);
  766. memcpy(eui + 5, dev->dev_addr + 3, 3);
  767. if (vlan_id < 0x1000) {
  768. eui[3] = vlan_id >> 8;
  769. eui[4] = vlan_id & 0xff;
  770. } else {
  771. eui[3] = 0xff;
  772. eui[4] = 0xfe;
  773. }
  774. eui[0] ^= 2;
  775. }
  776. static void update_gids_task(struct work_struct *work)
  777. {
  778. struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
  779. struct mlx4_cmd_mailbox *mailbox;
  780. union ib_gid *gids;
  781. int err;
  782. struct mlx4_dev *dev = gw->dev->dev;
  783. mailbox = mlx4_alloc_cmd_mailbox(dev);
  784. if (IS_ERR(mailbox)) {
  785. pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
  786. return;
  787. }
  788. gids = mailbox->buf;
  789. memcpy(gids, gw->gids, sizeof gw->gids);
  790. err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
  791. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
  792. MLX4_CMD_NATIVE);
  793. if (err)
  794. pr_warn("set port command failed\n");
  795. else {
  796. memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
  797. mlx4_ib_dispatch_event(gw->dev, gw->port, IB_EVENT_GID_CHANGE);
  798. }
  799. mlx4_free_cmd_mailbox(dev, mailbox);
  800. kfree(gw);
  801. }
  802. static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
  803. {
  804. struct net_device *ndev = dev->iboe.netdevs[port - 1];
  805. struct update_gid_work *work;
  806. struct net_device *tmp;
  807. int i;
  808. u8 *hits;
  809. int ret;
  810. union ib_gid gid;
  811. int free;
  812. int found;
  813. int need_update = 0;
  814. u16 vid;
  815. work = kzalloc(sizeof *work, GFP_ATOMIC);
  816. if (!work)
  817. return -ENOMEM;
  818. hits = kzalloc(128, GFP_ATOMIC);
  819. if (!hits) {
  820. ret = -ENOMEM;
  821. goto out;
  822. }
  823. rcu_read_lock();
  824. for_each_netdev_rcu(&init_net, tmp) {
  825. if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
  826. gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  827. vid = rdma_vlan_dev_vlan_id(tmp);
  828. mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
  829. found = 0;
  830. free = -1;
  831. for (i = 0; i < 128; ++i) {
  832. if (free < 0 &&
  833. !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  834. free = i;
  835. if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
  836. hits[i] = 1;
  837. found = 1;
  838. break;
  839. }
  840. }
  841. if (!found) {
  842. if (tmp == ndev &&
  843. (memcmp(&dev->iboe.gid_table[port - 1][0],
  844. &gid, sizeof gid) ||
  845. !memcmp(&dev->iboe.gid_table[port - 1][0],
  846. &zgid, sizeof gid))) {
  847. dev->iboe.gid_table[port - 1][0] = gid;
  848. ++need_update;
  849. hits[0] = 1;
  850. } else if (free >= 0) {
  851. dev->iboe.gid_table[port - 1][free] = gid;
  852. hits[free] = 1;
  853. ++need_update;
  854. }
  855. }
  856. }
  857. }
  858. rcu_read_unlock();
  859. for (i = 0; i < 128; ++i)
  860. if (!hits[i]) {
  861. if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  862. ++need_update;
  863. dev->iboe.gid_table[port - 1][i] = zgid;
  864. }
  865. if (need_update) {
  866. memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
  867. INIT_WORK(&work->work, update_gids_task);
  868. work->port = port;
  869. work->dev = dev;
  870. queue_work(wq, &work->work);
  871. } else
  872. kfree(work);
  873. kfree(hits);
  874. return 0;
  875. out:
  876. kfree(work);
  877. return ret;
  878. }
  879. static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
  880. {
  881. switch (event) {
  882. case NETDEV_UP:
  883. case NETDEV_CHANGEADDR:
  884. update_ipv6_gids(dev, port, 0);
  885. break;
  886. case NETDEV_DOWN:
  887. update_ipv6_gids(dev, port, 1);
  888. dev->iboe.netdevs[port - 1] = NULL;
  889. }
  890. }
  891. static void netdev_added(struct mlx4_ib_dev *dev, int port)
  892. {
  893. update_ipv6_gids(dev, port, 0);
  894. }
  895. static void netdev_removed(struct mlx4_ib_dev *dev, int port)
  896. {
  897. update_ipv6_gids(dev, port, 1);
  898. }
  899. static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
  900. void *ptr)
  901. {
  902. struct net_device *dev = ptr;
  903. struct mlx4_ib_dev *ibdev;
  904. struct net_device *oldnd;
  905. struct mlx4_ib_iboe *iboe;
  906. int port;
  907. if (!net_eq(dev_net(dev), &init_net))
  908. return NOTIFY_DONE;
  909. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
  910. iboe = &ibdev->iboe;
  911. spin_lock(&iboe->lock);
  912. mlx4_foreach_ib_transport_port(port, ibdev->dev) {
  913. oldnd = iboe->netdevs[port - 1];
  914. iboe->netdevs[port - 1] =
  915. mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
  916. if (oldnd != iboe->netdevs[port - 1]) {
  917. if (iboe->netdevs[port - 1])
  918. netdev_added(ibdev, port);
  919. else
  920. netdev_removed(ibdev, port);
  921. }
  922. }
  923. if (dev == iboe->netdevs[0] ||
  924. (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
  925. handle_en_event(ibdev, 1, event);
  926. else if (dev == iboe->netdevs[1]
  927. || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
  928. handle_en_event(ibdev, 2, event);
  929. spin_unlock(&iboe->lock);
  930. return NOTIFY_DONE;
  931. }
  932. static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  933. {
  934. char name[32];
  935. int eq_per_port = 0;
  936. int added_eqs = 0;
  937. int total_eqs = 0;
  938. int i, j, eq;
  939. /* Legacy mode or comp_pool is not large enough */
  940. if (dev->caps.comp_pool == 0 ||
  941. dev->caps.num_ports > dev->caps.comp_pool)
  942. return;
  943. eq_per_port = rounddown_pow_of_two(dev->caps.comp_pool/
  944. dev->caps.num_ports);
  945. /* Init eq table */
  946. added_eqs = 0;
  947. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
  948. added_eqs += eq_per_port;
  949. total_eqs = dev->caps.num_comp_vectors + added_eqs;
  950. ibdev->eq_table = kzalloc(total_eqs * sizeof(int), GFP_KERNEL);
  951. if (!ibdev->eq_table)
  952. return;
  953. ibdev->eq_added = added_eqs;
  954. eq = 0;
  955. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB) {
  956. for (j = 0; j < eq_per_port; j++) {
  957. sprintf(name, "mlx4-ib-%d-%d@%s",
  958. i, j, dev->pdev->bus->name);
  959. /* Set IRQ for specific name (per ring) */
  960. if (mlx4_assign_eq(dev, name, NULL,
  961. &ibdev->eq_table[eq])) {
  962. /* Use legacy (same as mlx4_en driver) */
  963. pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq);
  964. ibdev->eq_table[eq] =
  965. (eq % dev->caps.num_comp_vectors);
  966. }
  967. eq++;
  968. }
  969. }
  970. /* Fill the reset of the vector with legacy EQ */
  971. for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
  972. ibdev->eq_table[eq++] = i;
  973. /* Advertise the new number of EQs to clients */
  974. ibdev->ib_dev.num_comp_vectors = total_eqs;
  975. }
  976. static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
  977. {
  978. int i;
  979. /* no additional eqs were added */
  980. if (!ibdev->eq_table)
  981. return;
  982. /* Reset the advertised EQ number */
  983. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  984. /* Free only the added eqs */
  985. for (i = 0; i < ibdev->eq_added; i++) {
  986. /* Don't free legacy eqs if used */
  987. if (ibdev->eq_table[i] <= dev->caps.num_comp_vectors)
  988. continue;
  989. mlx4_release_eq(dev, ibdev->eq_table[i]);
  990. }
  991. kfree(ibdev->eq_table);
  992. }
  993. static void *mlx4_ib_add(struct mlx4_dev *dev)
  994. {
  995. struct mlx4_ib_dev *ibdev;
  996. int num_ports = 0;
  997. int i, j;
  998. int err;
  999. struct mlx4_ib_iboe *iboe;
  1000. pr_info_once("%s", mlx4_ib_version);
  1001. if (mlx4_is_mfunc(dev)) {
  1002. pr_warn("IB not yet supported in SRIOV\n");
  1003. return NULL;
  1004. }
  1005. mlx4_foreach_ib_transport_port(i, dev)
  1006. num_ports++;
  1007. /* No point in registering a device with no ports... */
  1008. if (num_ports == 0)
  1009. return NULL;
  1010. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  1011. if (!ibdev) {
  1012. dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
  1013. return NULL;
  1014. }
  1015. iboe = &ibdev->iboe;
  1016. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  1017. goto err_dealloc;
  1018. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  1019. goto err_pd;
  1020. ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
  1021. PAGE_SIZE);
  1022. if (!ibdev->uar_map)
  1023. goto err_uar;
  1024. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  1025. ibdev->dev = dev;
  1026. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  1027. ibdev->ib_dev.owner = THIS_MODULE;
  1028. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  1029. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  1030. ibdev->num_ports = num_ports;
  1031. ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
  1032. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  1033. ibdev->ib_dev.dma_device = &dev->pdev->dev;
  1034. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  1035. ibdev->ib_dev.uverbs_cmd_mask =
  1036. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1037. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1038. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1039. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1040. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1041. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1042. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1043. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1044. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1045. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1046. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1047. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1048. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1049. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1050. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1051. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1052. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  1053. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1054. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1055. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1056. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  1057. (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
  1058. (1ull << IB_USER_VERBS_CMD_OPEN_QP);
  1059. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  1060. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  1061. ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
  1062. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  1063. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  1064. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  1065. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  1066. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  1067. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  1068. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  1069. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  1070. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  1071. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  1072. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  1073. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  1074. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  1075. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  1076. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  1077. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  1078. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  1079. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  1080. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  1081. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  1082. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  1083. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  1084. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  1085. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  1086. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  1087. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  1088. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  1089. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  1090. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  1091. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  1092. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  1093. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  1094. ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
  1095. ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
  1096. ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
  1097. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  1098. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  1099. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  1100. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  1101. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  1102. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  1103. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  1104. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
  1105. ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
  1106. ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
  1107. ibdev->ib_dev.uverbs_cmd_mask |=
  1108. (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
  1109. (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
  1110. }
  1111. mlx4_ib_alloc_eqs(dev, ibdev);
  1112. spin_lock_init(&iboe->lock);
  1113. if (init_node_data(ibdev))
  1114. goto err_map;
  1115. for (i = 0; i < ibdev->num_ports; ++i) {
  1116. if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
  1117. IB_LINK_LAYER_ETHERNET) {
  1118. err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
  1119. if (err)
  1120. ibdev->counters[i] = -1;
  1121. } else
  1122. ibdev->counters[i] = -1;
  1123. }
  1124. spin_lock_init(&ibdev->sm_lock);
  1125. mutex_init(&ibdev->cap_mask_mutex);
  1126. if (ib_register_device(&ibdev->ib_dev, NULL))
  1127. goto err_counter;
  1128. if (mlx4_ib_mad_init(ibdev))
  1129. goto err_reg;
  1130. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
  1131. iboe->nb.notifier_call = mlx4_ib_netdev_event;
  1132. err = register_netdevice_notifier(&iboe->nb);
  1133. if (err)
  1134. goto err_reg;
  1135. }
  1136. for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
  1137. if (device_create_file(&ibdev->ib_dev.dev,
  1138. mlx4_class_attributes[j]))
  1139. goto err_notif;
  1140. }
  1141. ibdev->ib_active = true;
  1142. return ibdev;
  1143. err_notif:
  1144. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  1145. pr_warn("failure unregistering notifier\n");
  1146. flush_workqueue(wq);
  1147. err_reg:
  1148. ib_unregister_device(&ibdev->ib_dev);
  1149. err_counter:
  1150. for (; i; --i)
  1151. if (ibdev->counters[i - 1] != -1)
  1152. mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
  1153. err_map:
  1154. iounmap(ibdev->uar_map);
  1155. err_uar:
  1156. mlx4_uar_free(dev, &ibdev->priv_uar);
  1157. err_pd:
  1158. mlx4_pd_free(dev, ibdev->priv_pdn);
  1159. err_dealloc:
  1160. ib_dealloc_device(&ibdev->ib_dev);
  1161. return NULL;
  1162. }
  1163. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  1164. {
  1165. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  1166. int p;
  1167. mlx4_ib_mad_cleanup(ibdev);
  1168. ib_unregister_device(&ibdev->ib_dev);
  1169. if (ibdev->iboe.nb.notifier_call) {
  1170. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  1171. pr_warn("failure unregistering notifier\n");
  1172. ibdev->iboe.nb.notifier_call = NULL;
  1173. }
  1174. iounmap(ibdev->uar_map);
  1175. for (p = 0; p < ibdev->num_ports; ++p)
  1176. if (ibdev->counters[p] != -1)
  1177. mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
  1178. mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
  1179. mlx4_CLOSE_PORT(dev, p);
  1180. mlx4_ib_free_eqs(dev, ibdev);
  1181. mlx4_uar_free(dev, &ibdev->priv_uar);
  1182. mlx4_pd_free(dev, ibdev->priv_pdn);
  1183. ib_dealloc_device(&ibdev->ib_dev);
  1184. }
  1185. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  1186. enum mlx4_dev_event event, unsigned long param)
  1187. {
  1188. struct ib_event ibev;
  1189. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  1190. struct mlx4_eqe *eqe = NULL;
  1191. struct ib_event_work *ew;
  1192. int port = 0;
  1193. if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
  1194. eqe = (struct mlx4_eqe *)param;
  1195. else
  1196. port = (u8)param;
  1197. if (port > ibdev->num_ports)
  1198. return;
  1199. switch (event) {
  1200. case MLX4_DEV_EVENT_PORT_UP:
  1201. ibev.event = IB_EVENT_PORT_ACTIVE;
  1202. break;
  1203. case MLX4_DEV_EVENT_PORT_DOWN:
  1204. ibev.event = IB_EVENT_PORT_ERR;
  1205. break;
  1206. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  1207. ibdev->ib_active = false;
  1208. ibev.event = IB_EVENT_DEVICE_FATAL;
  1209. break;
  1210. case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
  1211. ew = kmalloc(sizeof *ew, GFP_ATOMIC);
  1212. if (!ew) {
  1213. pr_err("failed to allocate memory for events work\n");
  1214. break;
  1215. }
  1216. INIT_WORK(&ew->work, handle_port_mgmt_change_event);
  1217. memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
  1218. ew->ib_dev = ibdev;
  1219. handle_port_mgmt_change_event(&ew->work);
  1220. return;
  1221. default:
  1222. return;
  1223. }
  1224. ibev.device = ibdev_ptr;
  1225. ibev.element.port_num = port;
  1226. ib_dispatch_event(&ibev);
  1227. }
  1228. static struct mlx4_interface mlx4_ib_interface = {
  1229. .add = mlx4_ib_add,
  1230. .remove = mlx4_ib_remove,
  1231. .event = mlx4_ib_event,
  1232. .protocol = MLX4_PROT_IB_IPV6
  1233. };
  1234. static int __init mlx4_ib_init(void)
  1235. {
  1236. int err;
  1237. wq = create_singlethread_workqueue("mlx4_ib");
  1238. if (!wq)
  1239. return -ENOMEM;
  1240. err = mlx4_register_interface(&mlx4_ib_interface);
  1241. if (err) {
  1242. destroy_workqueue(wq);
  1243. return err;
  1244. }
  1245. return 0;
  1246. }
  1247. static void __exit mlx4_ib_cleanup(void)
  1248. {
  1249. mlx4_unregister_interface(&mlx4_ib_interface);
  1250. destroy_workqueue(wq);
  1251. }
  1252. module_init(mlx4_ib_init);
  1253. module_exit(mlx4_ib_cleanup);