main.c 31 KB

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