main.c 39 KB

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