main.c 52 KB

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