main.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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"
  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. props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
  115. 0xffffff;
  116. props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
  117. props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
  118. memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
  119. props->max_mr_size = ~0ull;
  120. props->page_size_cap = dev->dev->caps.page_size_cap;
  121. props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
  122. props->max_qp_wr = dev->dev->caps.max_wqes;
  123. props->max_sge = min(dev->dev->caps.max_sq_sg,
  124. dev->dev->caps.max_rq_sg);
  125. props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
  126. props->max_cqe = dev->dev->caps.max_cqes;
  127. props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
  128. props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
  129. props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
  130. props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
  131. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  132. props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
  133. props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
  134. props->max_srq_sge = dev->dev->caps.max_srq_sge;
  135. props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
  136. props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
  137. props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
  138. IB_ATOMIC_HCA : IB_ATOMIC_NONE;
  139. props->masked_atomic_cap = IB_ATOMIC_HCA;
  140. props->max_pkeys = dev->dev->caps.pkey_table_len[1];
  141. props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
  142. props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
  143. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  144. props->max_mcast_grp;
  145. props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1;
  146. out:
  147. kfree(in_mad);
  148. kfree(out_mad);
  149. return err;
  150. }
  151. static enum rdma_link_layer
  152. mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
  153. {
  154. struct mlx4_dev *dev = to_mdev(device)->dev;
  155. return dev->caps.port_mask & (1 << (port_num - 1)) ?
  156. IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
  157. }
  158. static int ib_link_query_port(struct ib_device *ibdev, u8 port,
  159. struct ib_port_attr *props,
  160. struct ib_smp *out_mad)
  161. {
  162. props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
  163. props->lmc = out_mad->data[34] & 0x7;
  164. props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
  165. props->sm_sl = out_mad->data[36] & 0xf;
  166. props->state = out_mad->data[32] & 0xf;
  167. props->phys_state = out_mad->data[33] >> 4;
  168. props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
  169. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  170. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  171. props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
  172. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  173. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  174. props->active_width = out_mad->data[31] & 0xf;
  175. props->active_speed = out_mad->data[35] >> 4;
  176. props->max_mtu = out_mad->data[41] & 0xf;
  177. props->active_mtu = out_mad->data[36] >> 4;
  178. props->subnet_timeout = out_mad->data[51] & 0x1f;
  179. props->max_vl_num = out_mad->data[37] >> 4;
  180. props->init_type_reply = out_mad->data[41] >> 4;
  181. return 0;
  182. }
  183. static u8 state_to_phys_state(enum ib_port_state state)
  184. {
  185. return state == IB_PORT_ACTIVE ? 5 : 3;
  186. }
  187. static int eth_link_query_port(struct ib_device *ibdev, u8 port,
  188. struct ib_port_attr *props,
  189. struct ib_smp *out_mad)
  190. {
  191. struct mlx4_ib_iboe *iboe = &to_mdev(ibdev)->iboe;
  192. struct net_device *ndev;
  193. enum ib_mtu tmp;
  194. props->active_width = IB_WIDTH_1X;
  195. props->active_speed = 4;
  196. props->port_cap_flags = IB_PORT_CM_SUP;
  197. props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
  198. props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
  199. props->pkey_tbl_len = 1;
  200. props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
  201. props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
  202. props->max_mtu = IB_MTU_2048;
  203. props->subnet_timeout = 0;
  204. props->max_vl_num = out_mad->data[37] >> 4;
  205. props->init_type_reply = 0;
  206. props->state = IB_PORT_DOWN;
  207. props->phys_state = state_to_phys_state(props->state);
  208. props->active_mtu = IB_MTU_256;
  209. spin_lock(&iboe->lock);
  210. ndev = iboe->netdevs[port - 1];
  211. if (!ndev)
  212. goto out;
  213. tmp = iboe_get_mtu(ndev->mtu);
  214. props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
  215. props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
  216. IB_PORT_ACTIVE : IB_PORT_DOWN;
  217. props->phys_state = state_to_phys_state(props->state);
  218. out:
  219. spin_unlock(&iboe->lock);
  220. return 0;
  221. }
  222. static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
  223. struct ib_port_attr *props)
  224. {
  225. struct ib_smp *in_mad = NULL;
  226. struct ib_smp *out_mad = NULL;
  227. int err = -ENOMEM;
  228. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  229. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  230. if (!in_mad || !out_mad)
  231. goto out;
  232. memset(props, 0, sizeof *props);
  233. init_query_mad(in_mad);
  234. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  235. in_mad->attr_mod = cpu_to_be32(port);
  236. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  237. if (err)
  238. goto out;
  239. err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
  240. ib_link_query_port(ibdev, port, props, out_mad) :
  241. eth_link_query_port(ibdev, port, props, out_mad);
  242. out:
  243. kfree(in_mad);
  244. kfree(out_mad);
  245. return err;
  246. }
  247. static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  248. union ib_gid *gid)
  249. {
  250. struct ib_smp *in_mad = NULL;
  251. struct ib_smp *out_mad = NULL;
  252. int err = -ENOMEM;
  253. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  254. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  255. if (!in_mad || !out_mad)
  256. goto out;
  257. init_query_mad(in_mad);
  258. in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
  259. in_mad->attr_mod = cpu_to_be32(port);
  260. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  261. if (err)
  262. goto out;
  263. memcpy(gid->raw, out_mad->data + 8, 8);
  264. init_query_mad(in_mad);
  265. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  266. in_mad->attr_mod = cpu_to_be32(index / 8);
  267. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  268. if (err)
  269. goto out;
  270. memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
  271. out:
  272. kfree(in_mad);
  273. kfree(out_mad);
  274. return err;
  275. }
  276. static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
  277. union ib_gid *gid)
  278. {
  279. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  280. *gid = dev->iboe.gid_table[port - 1][index];
  281. return 0;
  282. }
  283. static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  284. union ib_gid *gid)
  285. {
  286. if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
  287. return __mlx4_ib_query_gid(ibdev, port, index, gid);
  288. else
  289. return iboe_query_gid(ibdev, port, index, gid);
  290. }
  291. static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  292. u16 *pkey)
  293. {
  294. struct ib_smp *in_mad = NULL;
  295. struct ib_smp *out_mad = NULL;
  296. int err = -ENOMEM;
  297. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  298. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  299. if (!in_mad || !out_mad)
  300. goto out;
  301. init_query_mad(in_mad);
  302. in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
  303. in_mad->attr_mod = cpu_to_be32(index / 32);
  304. err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
  305. if (err)
  306. goto out;
  307. *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
  308. out:
  309. kfree(in_mad);
  310. kfree(out_mad);
  311. return err;
  312. }
  313. static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
  314. struct ib_device_modify *props)
  315. {
  316. struct mlx4_cmd_mailbox *mailbox;
  317. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  318. return -EOPNOTSUPP;
  319. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  320. return 0;
  321. spin_lock(&to_mdev(ibdev)->sm_lock);
  322. memcpy(ibdev->node_desc, props->node_desc, 64);
  323. spin_unlock(&to_mdev(ibdev)->sm_lock);
  324. /*
  325. * If possible, pass node desc to FW, so it can generate
  326. * a 144 trap. If cmd fails, just ignore.
  327. */
  328. mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
  329. if (IS_ERR(mailbox))
  330. return 0;
  331. memset(mailbox->buf, 0, 256);
  332. memcpy(mailbox->buf, props->node_desc, 64);
  333. mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
  334. MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A);
  335. mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
  336. return 0;
  337. }
  338. static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
  339. u32 cap_mask)
  340. {
  341. struct mlx4_cmd_mailbox *mailbox;
  342. int err;
  343. u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  344. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  345. if (IS_ERR(mailbox))
  346. return PTR_ERR(mailbox);
  347. memset(mailbox->buf, 0, 256);
  348. if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
  349. *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
  350. ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
  351. } else {
  352. ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
  353. ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
  354. }
  355. err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
  356. MLX4_CMD_TIME_CLASS_B);
  357. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  358. return err;
  359. }
  360. static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  361. struct ib_port_modify *props)
  362. {
  363. struct ib_port_attr attr;
  364. u32 cap_mask;
  365. int err;
  366. mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
  367. err = mlx4_ib_query_port(ibdev, port, &attr);
  368. if (err)
  369. goto out;
  370. cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
  371. ~props->clr_port_cap_mask;
  372. err = mlx4_SET_PORT(to_mdev(ibdev), port,
  373. !!(mask & IB_PORT_RESET_QKEY_CNTR),
  374. cap_mask);
  375. out:
  376. mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
  377. return err;
  378. }
  379. static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
  380. struct ib_udata *udata)
  381. {
  382. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  383. struct mlx4_ib_ucontext *context;
  384. struct mlx4_ib_alloc_ucontext_resp resp;
  385. int err;
  386. if (!dev->ib_active)
  387. return ERR_PTR(-EAGAIN);
  388. resp.qp_tab_size = dev->dev->caps.num_qps;
  389. resp.bf_reg_size = dev->dev->caps.bf_reg_size;
  390. resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
  391. context = kmalloc(sizeof *context, GFP_KERNEL);
  392. if (!context)
  393. return ERR_PTR(-ENOMEM);
  394. err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
  395. if (err) {
  396. kfree(context);
  397. return ERR_PTR(err);
  398. }
  399. INIT_LIST_HEAD(&context->db_page_list);
  400. mutex_init(&context->db_page_mutex);
  401. err = ib_copy_to_udata(udata, &resp, sizeof resp);
  402. if (err) {
  403. mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
  404. kfree(context);
  405. return ERR_PTR(-EFAULT);
  406. }
  407. return &context->ibucontext;
  408. }
  409. static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  410. {
  411. struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
  412. mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
  413. kfree(context);
  414. return 0;
  415. }
  416. static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  417. {
  418. struct mlx4_ib_dev *dev = to_mdev(context->device);
  419. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  420. return -EINVAL;
  421. if (vma->vm_pgoff == 0) {
  422. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  423. if (io_remap_pfn_range(vma, vma->vm_start,
  424. to_mucontext(context)->uar.pfn,
  425. PAGE_SIZE, vma->vm_page_prot))
  426. return -EAGAIN;
  427. } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
  428. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  429. if (io_remap_pfn_range(vma, vma->vm_start,
  430. to_mucontext(context)->uar.pfn +
  431. dev->dev->caps.num_uars,
  432. PAGE_SIZE, vma->vm_page_prot))
  433. return -EAGAIN;
  434. } else
  435. return -EINVAL;
  436. return 0;
  437. }
  438. static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
  439. struct ib_ucontext *context,
  440. struct ib_udata *udata)
  441. {
  442. struct mlx4_ib_pd *pd;
  443. int err;
  444. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  445. if (!pd)
  446. return ERR_PTR(-ENOMEM);
  447. err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
  448. if (err) {
  449. kfree(pd);
  450. return ERR_PTR(err);
  451. }
  452. if (context)
  453. if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
  454. mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
  455. kfree(pd);
  456. return ERR_PTR(-EFAULT);
  457. }
  458. return &pd->ibpd;
  459. }
  460. static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
  461. {
  462. mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
  463. kfree(pd);
  464. return 0;
  465. }
  466. static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
  467. {
  468. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  469. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  470. struct mlx4_ib_gid_entry *ge;
  471. ge = kzalloc(sizeof *ge, GFP_KERNEL);
  472. if (!ge)
  473. return -ENOMEM;
  474. ge->gid = *gid;
  475. if (mlx4_ib_add_mc(mdev, mqp, gid)) {
  476. ge->port = mqp->port;
  477. ge->added = 1;
  478. }
  479. mutex_lock(&mqp->mutex);
  480. list_add_tail(&ge->list, &mqp->gid_list);
  481. mutex_unlock(&mqp->mutex);
  482. return 0;
  483. }
  484. int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
  485. union ib_gid *gid)
  486. {
  487. u8 mac[6];
  488. struct net_device *ndev;
  489. int ret = 0;
  490. if (!mqp->port)
  491. return 0;
  492. spin_lock(&mdev->iboe.lock);
  493. ndev = mdev->iboe.netdevs[mqp->port - 1];
  494. if (ndev)
  495. dev_hold(ndev);
  496. spin_unlock(&mdev->iboe.lock);
  497. if (ndev) {
  498. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  499. rtnl_lock();
  500. dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
  501. ret = 1;
  502. rtnl_unlock();
  503. dev_put(ndev);
  504. }
  505. return ret;
  506. }
  507. static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  508. {
  509. int err;
  510. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  511. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  512. err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, !!(mqp->flags &
  513. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK));
  514. if (err)
  515. return err;
  516. err = add_gid_entry(ibqp, gid);
  517. if (err)
  518. goto err_add;
  519. return 0;
  520. err_add:
  521. mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw);
  522. return err;
  523. }
  524. static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
  525. {
  526. struct mlx4_ib_gid_entry *ge;
  527. struct mlx4_ib_gid_entry *tmp;
  528. struct mlx4_ib_gid_entry *ret = NULL;
  529. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  530. if (!memcmp(raw, ge->gid.raw, 16)) {
  531. ret = ge;
  532. break;
  533. }
  534. }
  535. return ret;
  536. }
  537. static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  538. {
  539. int err;
  540. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  541. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  542. u8 mac[6];
  543. struct net_device *ndev;
  544. struct mlx4_ib_gid_entry *ge;
  545. err = mlx4_multicast_detach(mdev->dev,
  546. &mqp->mqp, gid->raw);
  547. if (err)
  548. return err;
  549. mutex_lock(&mqp->mutex);
  550. ge = find_gid_entry(mqp, gid->raw);
  551. if (ge) {
  552. spin_lock(&mdev->iboe.lock);
  553. ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
  554. if (ndev)
  555. dev_hold(ndev);
  556. spin_unlock(&mdev->iboe.lock);
  557. rdma_get_mcast_mac((struct in6_addr *)gid, mac);
  558. if (ndev) {
  559. rtnl_lock();
  560. dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
  561. rtnl_unlock();
  562. dev_put(ndev);
  563. }
  564. list_del(&ge->list);
  565. kfree(ge);
  566. } else
  567. printk(KERN_WARNING "could not find mgid entry\n");
  568. mutex_unlock(&mqp->mutex);
  569. return 0;
  570. }
  571. static int init_node_data(struct mlx4_ib_dev *dev)
  572. {
  573. struct ib_smp *in_mad = NULL;
  574. struct ib_smp *out_mad = NULL;
  575. int err = -ENOMEM;
  576. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  577. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  578. if (!in_mad || !out_mad)
  579. goto out;
  580. init_query_mad(in_mad);
  581. in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
  582. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  583. if (err)
  584. goto out;
  585. memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
  586. in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
  587. err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
  588. if (err)
  589. goto out;
  590. dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
  591. memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
  592. out:
  593. kfree(in_mad);
  594. kfree(out_mad);
  595. return err;
  596. }
  597. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  598. char *buf)
  599. {
  600. struct mlx4_ib_dev *dev =
  601. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  602. return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
  603. }
  604. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  605. char *buf)
  606. {
  607. struct mlx4_ib_dev *dev =
  608. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  609. return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
  610. (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
  611. (int) dev->dev->caps.fw_ver & 0xffff);
  612. }
  613. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  614. char *buf)
  615. {
  616. struct mlx4_ib_dev *dev =
  617. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  618. return sprintf(buf, "%x\n", dev->dev->rev_id);
  619. }
  620. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  621. char *buf)
  622. {
  623. struct mlx4_ib_dev *dev =
  624. container_of(device, struct mlx4_ib_dev, ib_dev.dev);
  625. return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
  626. dev->dev->board_id);
  627. }
  628. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  629. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  630. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  631. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  632. static struct device_attribute *mlx4_class_attributes[] = {
  633. &dev_attr_hw_rev,
  634. &dev_attr_fw_ver,
  635. &dev_attr_hca_type,
  636. &dev_attr_board_id
  637. };
  638. static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
  639. {
  640. memcpy(eui, dev->dev_addr, 3);
  641. memcpy(eui + 5, dev->dev_addr + 3, 3);
  642. if (vlan_id < 0x1000) {
  643. eui[3] = vlan_id >> 8;
  644. eui[4] = vlan_id & 0xff;
  645. } else {
  646. eui[3] = 0xff;
  647. eui[4] = 0xfe;
  648. }
  649. eui[0] ^= 2;
  650. }
  651. static void update_gids_task(struct work_struct *work)
  652. {
  653. struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
  654. struct mlx4_cmd_mailbox *mailbox;
  655. union ib_gid *gids;
  656. int err;
  657. struct mlx4_dev *dev = gw->dev->dev;
  658. struct ib_event event;
  659. mailbox = mlx4_alloc_cmd_mailbox(dev);
  660. if (IS_ERR(mailbox)) {
  661. printk(KERN_WARNING "update gid table failed %ld\n", PTR_ERR(mailbox));
  662. return;
  663. }
  664. gids = mailbox->buf;
  665. memcpy(gids, gw->gids, sizeof gw->gids);
  666. err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
  667. 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B);
  668. if (err)
  669. printk(KERN_WARNING "set port command failed\n");
  670. else {
  671. memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
  672. event.device = &gw->dev->ib_dev;
  673. event.element.port_num = gw->port;
  674. event.event = IB_EVENT_LID_CHANGE;
  675. ib_dispatch_event(&event);
  676. }
  677. mlx4_free_cmd_mailbox(dev, mailbox);
  678. kfree(gw);
  679. }
  680. static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
  681. {
  682. struct net_device *ndev = dev->iboe.netdevs[port - 1];
  683. struct update_gid_work *work;
  684. struct net_device *tmp;
  685. int i;
  686. u8 *hits;
  687. int ret;
  688. union ib_gid gid;
  689. int free;
  690. int found;
  691. int need_update = 0;
  692. u16 vid;
  693. work = kzalloc(sizeof *work, GFP_ATOMIC);
  694. if (!work)
  695. return -ENOMEM;
  696. hits = kzalloc(128, GFP_ATOMIC);
  697. if (!hits) {
  698. ret = -ENOMEM;
  699. goto out;
  700. }
  701. rcu_read_lock();
  702. for_each_netdev_rcu(&init_net, tmp) {
  703. if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
  704. gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  705. vid = rdma_vlan_dev_vlan_id(tmp);
  706. mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
  707. found = 0;
  708. free = -1;
  709. for (i = 0; i < 128; ++i) {
  710. if (free < 0 &&
  711. !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  712. free = i;
  713. if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
  714. hits[i] = 1;
  715. found = 1;
  716. break;
  717. }
  718. }
  719. if (!found) {
  720. if (tmp == ndev &&
  721. (memcmp(&dev->iboe.gid_table[port - 1][0],
  722. &gid, sizeof gid) ||
  723. !memcmp(&dev->iboe.gid_table[port - 1][0],
  724. &zgid, sizeof gid))) {
  725. dev->iboe.gid_table[port - 1][0] = gid;
  726. ++need_update;
  727. hits[0] = 1;
  728. } else if (free >= 0) {
  729. dev->iboe.gid_table[port - 1][free] = gid;
  730. hits[free] = 1;
  731. ++need_update;
  732. }
  733. }
  734. }
  735. }
  736. rcu_read_unlock();
  737. for (i = 0; i < 128; ++i)
  738. if (!hits[i]) {
  739. if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
  740. ++need_update;
  741. dev->iboe.gid_table[port - 1][i] = zgid;
  742. }
  743. if (need_update) {
  744. memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
  745. INIT_WORK(&work->work, update_gids_task);
  746. work->port = port;
  747. work->dev = dev;
  748. queue_work(wq, &work->work);
  749. } else
  750. kfree(work);
  751. kfree(hits);
  752. return 0;
  753. out:
  754. kfree(work);
  755. return ret;
  756. }
  757. static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
  758. {
  759. switch (event) {
  760. case NETDEV_UP:
  761. case NETDEV_CHANGEADDR:
  762. update_ipv6_gids(dev, port, 0);
  763. break;
  764. case NETDEV_DOWN:
  765. update_ipv6_gids(dev, port, 1);
  766. dev->iboe.netdevs[port - 1] = NULL;
  767. }
  768. }
  769. static void netdev_added(struct mlx4_ib_dev *dev, int port)
  770. {
  771. update_ipv6_gids(dev, port, 0);
  772. }
  773. static void netdev_removed(struct mlx4_ib_dev *dev, int port)
  774. {
  775. update_ipv6_gids(dev, port, 1);
  776. }
  777. static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
  778. void *ptr)
  779. {
  780. struct net_device *dev = ptr;
  781. struct mlx4_ib_dev *ibdev;
  782. struct net_device *oldnd;
  783. struct mlx4_ib_iboe *iboe;
  784. int port;
  785. if (!net_eq(dev_net(dev), &init_net))
  786. return NOTIFY_DONE;
  787. ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
  788. iboe = &ibdev->iboe;
  789. spin_lock(&iboe->lock);
  790. mlx4_foreach_ib_transport_port(port, ibdev->dev) {
  791. oldnd = iboe->netdevs[port - 1];
  792. iboe->netdevs[port - 1] =
  793. mlx4_get_protocol_dev(ibdev->dev, MLX4_PROTOCOL_EN, port);
  794. if (oldnd != iboe->netdevs[port - 1]) {
  795. if (iboe->netdevs[port - 1])
  796. netdev_added(ibdev, port);
  797. else
  798. netdev_removed(ibdev, port);
  799. }
  800. }
  801. if (dev == iboe->netdevs[0] ||
  802. (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
  803. handle_en_event(ibdev, 1, event);
  804. else if (dev == iboe->netdevs[1]
  805. || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
  806. handle_en_event(ibdev, 2, event);
  807. spin_unlock(&iboe->lock);
  808. return NOTIFY_DONE;
  809. }
  810. static void *mlx4_ib_add(struct mlx4_dev *dev)
  811. {
  812. struct mlx4_ib_dev *ibdev;
  813. int num_ports = 0;
  814. int i;
  815. int err;
  816. struct mlx4_ib_iboe *iboe;
  817. printk_once(KERN_INFO "%s", mlx4_ib_version);
  818. mlx4_foreach_ib_transport_port(i, dev)
  819. num_ports++;
  820. /* No point in registering a device with no ports... */
  821. if (num_ports == 0)
  822. return NULL;
  823. ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
  824. if (!ibdev) {
  825. dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
  826. return NULL;
  827. }
  828. iboe = &ibdev->iboe;
  829. if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
  830. goto err_dealloc;
  831. if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
  832. goto err_pd;
  833. ibdev->uar_map = ioremap(ibdev->priv_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
  834. if (!ibdev->uar_map)
  835. goto err_uar;
  836. MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
  837. ibdev->dev = dev;
  838. strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
  839. ibdev->ib_dev.owner = THIS_MODULE;
  840. ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
  841. ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
  842. ibdev->num_ports = num_ports;
  843. ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
  844. ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
  845. ibdev->ib_dev.dma_device = &dev->pdev->dev;
  846. ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
  847. ibdev->ib_dev.uverbs_cmd_mask =
  848. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  849. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  850. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  851. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  852. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  853. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  854. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  855. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  856. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  857. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  858. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  859. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  860. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  861. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  862. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  863. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  864. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  865. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  866. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  867. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  868. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
  869. ibdev->ib_dev.query_device = mlx4_ib_query_device;
  870. ibdev->ib_dev.query_port = mlx4_ib_query_port;
  871. ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
  872. ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
  873. ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
  874. ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
  875. ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
  876. ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
  877. ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
  878. ibdev->ib_dev.mmap = mlx4_ib_mmap;
  879. ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
  880. ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
  881. ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
  882. ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
  883. ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
  884. ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
  885. ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
  886. ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
  887. ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
  888. ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
  889. ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
  890. ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
  891. ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
  892. ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
  893. ibdev->ib_dev.post_send = mlx4_ib_post_send;
  894. ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
  895. ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
  896. ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
  897. ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
  898. ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
  899. ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
  900. ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
  901. ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
  902. ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
  903. ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
  904. ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
  905. ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
  906. ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
  907. ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
  908. ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
  909. ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
  910. ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
  911. ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
  912. ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
  913. ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
  914. spin_lock_init(&iboe->lock);
  915. if (init_node_data(ibdev))
  916. goto err_map;
  917. spin_lock_init(&ibdev->sm_lock);
  918. mutex_init(&ibdev->cap_mask_mutex);
  919. if (ib_register_device(&ibdev->ib_dev, NULL))
  920. goto err_map;
  921. if (mlx4_ib_mad_init(ibdev))
  922. goto err_reg;
  923. if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
  924. iboe->nb.notifier_call = mlx4_ib_netdev_event;
  925. err = register_netdevice_notifier(&iboe->nb);
  926. if (err)
  927. goto err_reg;
  928. }
  929. for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {
  930. if (device_create_file(&ibdev->ib_dev.dev,
  931. mlx4_class_attributes[i]))
  932. goto err_notif;
  933. }
  934. ibdev->ib_active = true;
  935. return ibdev;
  936. err_notif:
  937. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  938. printk(KERN_WARNING "failure unregistering notifier\n");
  939. flush_workqueue(wq);
  940. err_reg:
  941. ib_unregister_device(&ibdev->ib_dev);
  942. err_map:
  943. iounmap(ibdev->uar_map);
  944. err_uar:
  945. mlx4_uar_free(dev, &ibdev->priv_uar);
  946. err_pd:
  947. mlx4_pd_free(dev, ibdev->priv_pdn);
  948. err_dealloc:
  949. ib_dealloc_device(&ibdev->ib_dev);
  950. return NULL;
  951. }
  952. static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
  953. {
  954. struct mlx4_ib_dev *ibdev = ibdev_ptr;
  955. int p;
  956. mlx4_ib_mad_cleanup(ibdev);
  957. ib_unregister_device(&ibdev->ib_dev);
  958. if (ibdev->iboe.nb.notifier_call) {
  959. if (unregister_netdevice_notifier(&ibdev->iboe.nb))
  960. printk(KERN_WARNING "failure unregistering notifier\n");
  961. ibdev->iboe.nb.notifier_call = NULL;
  962. }
  963. iounmap(ibdev->uar_map);
  964. mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
  965. mlx4_CLOSE_PORT(dev, p);
  966. mlx4_uar_free(dev, &ibdev->priv_uar);
  967. mlx4_pd_free(dev, ibdev->priv_pdn);
  968. ib_dealloc_device(&ibdev->ib_dev);
  969. }
  970. static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
  971. enum mlx4_dev_event event, int port)
  972. {
  973. struct ib_event ibev;
  974. struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
  975. if (port > ibdev->num_ports)
  976. return;
  977. switch (event) {
  978. case MLX4_DEV_EVENT_PORT_UP:
  979. ibev.event = IB_EVENT_PORT_ACTIVE;
  980. break;
  981. case MLX4_DEV_EVENT_PORT_DOWN:
  982. ibev.event = IB_EVENT_PORT_ERR;
  983. break;
  984. case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
  985. ibdev->ib_active = false;
  986. ibev.event = IB_EVENT_DEVICE_FATAL;
  987. break;
  988. default:
  989. return;
  990. }
  991. ibev.device = ibdev_ptr;
  992. ibev.element.port_num = port;
  993. ib_dispatch_event(&ibev);
  994. }
  995. static struct mlx4_interface mlx4_ib_interface = {
  996. .add = mlx4_ib_add,
  997. .remove = mlx4_ib_remove,
  998. .event = mlx4_ib_event,
  999. .protocol = MLX4_PROTOCOL_IB
  1000. };
  1001. static int __init mlx4_ib_init(void)
  1002. {
  1003. int err;
  1004. wq = create_singlethread_workqueue("mlx4_ib");
  1005. if (!wq)
  1006. return -ENOMEM;
  1007. err = mlx4_register_interface(&mlx4_ib_interface);
  1008. if (err) {
  1009. destroy_workqueue(wq);
  1010. return err;
  1011. }
  1012. return 0;
  1013. }
  1014. static void __exit mlx4_ib_cleanup(void)
  1015. {
  1016. mlx4_unregister_interface(&mlx4_ib_interface);
  1017. destroy_workqueue(wq);
  1018. }
  1019. module_init(mlx4_ib_init);
  1020. module_exit(mlx4_ib_cleanup);