main.c 46 KB

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