opa_vnic_vema.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. * Copyright(c) 2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. /*
  48. * This file contains OPA Virtual Network Interface Controller (VNIC)
  49. * Ethernet Management Agent (EMA) driver
  50. */
  51. #include <linux/module.h>
  52. #include <rdma/ib_addr.h>
  53. #include <rdma/ib_smi.h>
  54. #include "opa_vnic_internal.h"
  55. #define DRV_VERSION "1.0"
  56. char opa_vnic_driver_name[] = "opa_vnic";
  57. const char opa_vnic_driver_version[] = DRV_VERSION;
  58. /*
  59. * The trap service level is kept in bits 3 to 7 in the trap_sl_rsvd
  60. * field in the class port info MAD.
  61. */
  62. #define GET_TRAP_SL_FROM_CLASS_PORT_INFO(x) (((x) >> 3) & 0x1f)
  63. /* Cap trap bursts to a reasonable limit good for normal cases */
  64. #define OPA_VNIC_TRAP_BURST_LIMIT 4
  65. /*
  66. * VNIC trap limit timeout.
  67. * Inverse of cap2_mask response time out (1.0737 secs) = 0.9
  68. * secs approx IB spec 13.4.6.2.1 PortInfoSubnetTimeout and
  69. * 13.4.9 Traps.
  70. */
  71. #define OPA_VNIC_TRAP_TIMEOUT ((4096 * (1UL << 18)) / 1000)
  72. #define OPA_VNIC_UNSUP_ATTR \
  73. cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB)
  74. #define OPA_VNIC_INVAL_ATTR \
  75. cpu_to_be16(IB_MGMT_MAD_STATUS_INVALID_ATTRIB_VALUE)
  76. #define OPA_VNIC_CLASS_CAP_TRAP 0x1
  77. /* Maximum number of VNIC ports supported */
  78. #define OPA_VNIC_MAX_NUM_VPORT 255
  79. /**
  80. * struct opa_vnic_vema_port -- VNIC VEMA port details
  81. * @cport: pointer to port
  82. * @mad_agent: pointer to mad agent for port
  83. * @class_port_info: Class port info information.
  84. * @tid: Transaction id
  85. * @port_num: OPA port number
  86. * @vport_idr: vnic ports idr
  87. * @event_handler: ib event handler
  88. * @lock: adapter interface lock
  89. */
  90. struct opa_vnic_vema_port {
  91. struct opa_vnic_ctrl_port *cport;
  92. struct ib_mad_agent *mad_agent;
  93. struct opa_class_port_info class_port_info;
  94. u64 tid;
  95. u8 port_num;
  96. struct idr vport_idr;
  97. struct ib_event_handler event_handler;
  98. /* Lock to query/update network adapter */
  99. struct mutex lock;
  100. };
  101. static void opa_vnic_vema_add_one(struct ib_device *device);
  102. static void opa_vnic_vema_rem_one(struct ib_device *device,
  103. void *client_data);
  104. static struct ib_client opa_vnic_client = {
  105. .name = opa_vnic_driver_name,
  106. .add = opa_vnic_vema_add_one,
  107. .remove = opa_vnic_vema_rem_one,
  108. };
  109. /**
  110. * vema_get_vport_num -- Get the vnic from the mad
  111. * @recvd_mad: Received mad
  112. *
  113. * Return: returns value of the vnic port number
  114. */
  115. static inline u8 vema_get_vport_num(struct opa_vnic_vema_mad *recvd_mad)
  116. {
  117. return be32_to_cpu(recvd_mad->mad_hdr.attr_mod) & 0xff;
  118. }
  119. /**
  120. * vema_get_vport_adapter -- Get vnic port adapter from recvd mad
  121. * @recvd_mad: received mad
  122. * @port: ptr to port struct on which MAD was recvd
  123. *
  124. * Return: vnic adapter
  125. */
  126. static inline struct opa_vnic_adapter *
  127. vema_get_vport_adapter(struct opa_vnic_vema_mad *recvd_mad,
  128. struct opa_vnic_vema_port *port)
  129. {
  130. u8 vport_num = vema_get_vport_num(recvd_mad);
  131. return idr_find(&port->vport_idr, vport_num);
  132. }
  133. /**
  134. * vema_mac_tbl_req_ok -- Check if mac request has correct values
  135. * @mac_tbl: mac table
  136. *
  137. * This function checks for the validity of the offset and number of
  138. * entries required.
  139. *
  140. * Return: true if offset and num_entries are valid
  141. */
  142. static inline bool vema_mac_tbl_req_ok(struct opa_veswport_mactable *mac_tbl)
  143. {
  144. u16 offset, num_entries;
  145. u16 req_entries = ((OPA_VNIC_EMA_DATA - sizeof(*mac_tbl)) /
  146. sizeof(mac_tbl->tbl_entries[0]));
  147. offset = be16_to_cpu(mac_tbl->offset);
  148. num_entries = be16_to_cpu(mac_tbl->num_entries);
  149. return ((num_entries <= req_entries) &&
  150. (offset + num_entries <= OPA_VNIC_MAC_TBL_MAX_ENTRIES));
  151. }
  152. /*
  153. * Return the power on default values in the port info structure
  154. * in big endian format as required by MAD.
  155. */
  156. static inline void vema_get_pod_values(struct opa_veswport_info *port_info)
  157. {
  158. memset(port_info, 0, sizeof(*port_info));
  159. port_info->vport.max_mac_tbl_ent =
  160. cpu_to_be16(OPA_VNIC_MAC_TBL_MAX_ENTRIES);
  161. port_info->vport.max_smac_ent =
  162. cpu_to_be16(OPA_VNIC_MAX_SMAC_LIMIT);
  163. port_info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL;
  164. port_info->vport.config_state = OPA_VNIC_STATE_DROP_ALL;
  165. }
  166. /**
  167. * vema_add_vport -- Add a new vnic port
  168. * @port: ptr to opa_vnic_vema_port struct
  169. * @vport_num: vnic port number (to be added)
  170. *
  171. * Return a pointer to the vnic adapter structure
  172. */
  173. static struct opa_vnic_adapter *vema_add_vport(struct opa_vnic_vema_port *port,
  174. u8 vport_num)
  175. {
  176. struct opa_vnic_ctrl_port *cport = port->cport;
  177. struct opa_vnic_adapter *adapter;
  178. adapter = opa_vnic_add_netdev(cport->ibdev, port->port_num, vport_num);
  179. if (!IS_ERR(adapter)) {
  180. int rc;
  181. adapter->cport = cport;
  182. rc = idr_alloc(&port->vport_idr, adapter, vport_num,
  183. vport_num + 1, GFP_NOWAIT);
  184. if (rc < 0) {
  185. opa_vnic_rem_netdev(adapter);
  186. adapter = ERR_PTR(rc);
  187. }
  188. }
  189. return adapter;
  190. }
  191. /**
  192. * vema_get_class_port_info -- Get class info for port
  193. * @port: Port on whic MAD was received
  194. * @recvd_mad: pointer to the received mad
  195. * @rsp_mad: pointer to respose mad
  196. *
  197. * This function copies the latest class port info value set for the
  198. * port and stores it for generating traps
  199. */
  200. static void vema_get_class_port_info(struct opa_vnic_vema_port *port,
  201. struct opa_vnic_vema_mad *recvd_mad,
  202. struct opa_vnic_vema_mad *rsp_mad)
  203. {
  204. struct opa_class_port_info *port_info;
  205. port_info = (struct opa_class_port_info *)rsp_mad->data;
  206. memcpy(port_info, &port->class_port_info, sizeof(*port_info));
  207. port_info->base_version = OPA_MGMT_BASE_VERSION,
  208. port_info->class_version = OPA_EMA_CLASS_VERSION;
  209. /*
  210. * Set capability mask bit indicating agent generates traps,
  211. * and set the maximum number of VNIC ports supported.
  212. */
  213. port_info->cap_mask = cpu_to_be16((OPA_VNIC_CLASS_CAP_TRAP |
  214. (OPA_VNIC_MAX_NUM_VPORT << 8)));
  215. /*
  216. * Since a get routine is always sent by the EM first we
  217. * set the expected response time to
  218. * 4.096 usec * 2^18 == 1.0737 sec here.
  219. */
  220. port_info->cap_mask2_resp_time = cpu_to_be32(18);
  221. }
  222. /**
  223. * vema_set_class_port_info -- Get class info for port
  224. * @port: Port on whic MAD was received
  225. * @recvd_mad: pointer to the received mad
  226. * @rsp_mad: pointer to respose mad
  227. *
  228. * This function updates the port class info for the specific vnic
  229. * and sets up the response mad data
  230. */
  231. static void vema_set_class_port_info(struct opa_vnic_vema_port *port,
  232. struct opa_vnic_vema_mad *recvd_mad,
  233. struct opa_vnic_vema_mad *rsp_mad)
  234. {
  235. memcpy(&port->class_port_info, recvd_mad->data,
  236. sizeof(port->class_port_info));
  237. vema_get_class_port_info(port, recvd_mad, rsp_mad);
  238. }
  239. /**
  240. * vema_get_veswport_info -- Get veswport info
  241. * @port: source port on which MAD was received
  242. * @recvd_mad: pointer to the received mad
  243. * @rsp_mad: pointer to respose mad
  244. */
  245. static void vema_get_veswport_info(struct opa_vnic_vema_port *port,
  246. struct opa_vnic_vema_mad *recvd_mad,
  247. struct opa_vnic_vema_mad *rsp_mad)
  248. {
  249. struct opa_veswport_info *port_info =
  250. (struct opa_veswport_info *)rsp_mad->data;
  251. struct opa_vnic_adapter *adapter;
  252. adapter = vema_get_vport_adapter(recvd_mad, port);
  253. if (adapter) {
  254. memset(port_info, 0, sizeof(*port_info));
  255. opa_vnic_get_vesw_info(adapter, &port_info->vesw);
  256. opa_vnic_get_per_veswport_info(adapter,
  257. &port_info->vport);
  258. } else {
  259. vema_get_pod_values(port_info);
  260. }
  261. }
  262. /**
  263. * vema_set_veswport_info -- Set veswport info
  264. * @port: source port on which MAD was received
  265. * @recvd_mad: pointer to the received mad
  266. * @rsp_mad: pointer to respose mad
  267. *
  268. * This function gets the port class infor for vnic
  269. */
  270. static void vema_set_veswport_info(struct opa_vnic_vema_port *port,
  271. struct opa_vnic_vema_mad *recvd_mad,
  272. struct opa_vnic_vema_mad *rsp_mad)
  273. {
  274. struct opa_vnic_ctrl_port *cport = port->cport;
  275. struct opa_veswport_info *port_info;
  276. struct opa_vnic_adapter *adapter;
  277. u8 vport_num;
  278. vport_num = vema_get_vport_num(recvd_mad);
  279. adapter = vema_get_vport_adapter(recvd_mad, port);
  280. if (!adapter) {
  281. adapter = vema_add_vport(port, vport_num);
  282. if (IS_ERR(adapter)) {
  283. c_err("failed to add vport %d: %ld\n",
  284. vport_num, PTR_ERR(adapter));
  285. goto err_exit;
  286. }
  287. }
  288. port_info = (struct opa_veswport_info *)recvd_mad->data;
  289. opa_vnic_set_vesw_info(adapter, &port_info->vesw);
  290. opa_vnic_set_per_veswport_info(adapter, &port_info->vport);
  291. /* Process the new config settings */
  292. opa_vnic_process_vema_config(adapter);
  293. vema_get_veswport_info(port, recvd_mad, rsp_mad);
  294. return;
  295. err_exit:
  296. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  297. }
  298. /**
  299. * vema_get_mac_entries -- Get MAC entries in VNIC MAC table
  300. * @port: source port on which MAD was received
  301. * @recvd_mad: pointer to the received mad
  302. * @rsp_mad: pointer to respose mad
  303. *
  304. * This function gets the MAC entries that are programmed into
  305. * the VNIC MAC forwarding table. It checks for the validity of
  306. * the index into the MAC table and the number of entries that
  307. * are to be retrieved.
  308. */
  309. static void vema_get_mac_entries(struct opa_vnic_vema_port *port,
  310. struct opa_vnic_vema_mad *recvd_mad,
  311. struct opa_vnic_vema_mad *rsp_mad)
  312. {
  313. struct opa_veswport_mactable *mac_tbl_in, *mac_tbl_out;
  314. struct opa_vnic_adapter *adapter;
  315. adapter = vema_get_vport_adapter(recvd_mad, port);
  316. if (!adapter) {
  317. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  318. return;
  319. }
  320. mac_tbl_in = (struct opa_veswport_mactable *)recvd_mad->data;
  321. mac_tbl_out = (struct opa_veswport_mactable *)rsp_mad->data;
  322. if (vema_mac_tbl_req_ok(mac_tbl_in)) {
  323. mac_tbl_out->offset = mac_tbl_in->offset;
  324. mac_tbl_out->num_entries = mac_tbl_in->num_entries;
  325. opa_vnic_query_mac_tbl(adapter, mac_tbl_out);
  326. } else {
  327. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  328. }
  329. }
  330. /**
  331. * vema_set_mac_entries -- Set MAC entries in VNIC MAC table
  332. * @port: source port on which MAD was received
  333. * @recvd_mad: pointer to the received mad
  334. * @rsp_mad: pointer to respose mad
  335. *
  336. * This function sets the MAC entries in the VNIC forwarding table
  337. * It checks for the validity of the index and the number of forwarding
  338. * table entries to be programmed.
  339. */
  340. static void vema_set_mac_entries(struct opa_vnic_vema_port *port,
  341. struct opa_vnic_vema_mad *recvd_mad,
  342. struct opa_vnic_vema_mad *rsp_mad)
  343. {
  344. struct opa_veswport_mactable *mac_tbl;
  345. struct opa_vnic_adapter *adapter;
  346. adapter = vema_get_vport_adapter(recvd_mad, port);
  347. if (!adapter) {
  348. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  349. return;
  350. }
  351. mac_tbl = (struct opa_veswport_mactable *)recvd_mad->data;
  352. if (vema_mac_tbl_req_ok(mac_tbl)) {
  353. if (opa_vnic_update_mac_tbl(adapter, mac_tbl))
  354. rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
  355. } else {
  356. rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
  357. }
  358. vema_get_mac_entries(port, recvd_mad, rsp_mad);
  359. }
  360. /**
  361. * vema_set_delete_vesw -- Reset VESW info to POD values
  362. * @port: source port on which MAD was received
  363. * @recvd_mad: pointer to the received mad
  364. * @rsp_mad: pointer to respose mad
  365. *
  366. * This function clears all the fields of veswport info for the requested vesw
  367. * and sets them back to the power-on default values. It does not delete the
  368. * vesw.
  369. */
  370. static void vema_set_delete_vesw(struct opa_vnic_vema_port *port,
  371. struct opa_vnic_vema_mad *recvd_mad,
  372. struct opa_vnic_vema_mad *rsp_mad)
  373. {
  374. struct opa_veswport_info *port_info =
  375. (struct opa_veswport_info *)rsp_mad->data;
  376. struct opa_vnic_adapter *adapter;
  377. adapter = vema_get_vport_adapter(recvd_mad, port);
  378. if (!adapter) {
  379. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  380. return;
  381. }
  382. vema_get_pod_values(port_info);
  383. opa_vnic_set_vesw_info(adapter, &port_info->vesw);
  384. opa_vnic_set_per_veswport_info(adapter, &port_info->vport);
  385. /* Process the new config settings */
  386. opa_vnic_process_vema_config(adapter);
  387. opa_vnic_release_mac_tbl(adapter);
  388. vema_get_veswport_info(port, recvd_mad, rsp_mad);
  389. }
  390. /**
  391. * vema_get_mac_list -- Get the unicast/multicast macs.
  392. * @port: source port on which MAD was received
  393. * @recvd_mad: Received mad contains fields to set vnic parameters
  394. * @rsp_mad: Response mad to be built
  395. * @attr_id: Attribute ID indicating multicast or unicast mac list
  396. */
  397. static void vema_get_mac_list(struct opa_vnic_vema_port *port,
  398. struct opa_vnic_vema_mad *recvd_mad,
  399. struct opa_vnic_vema_mad *rsp_mad,
  400. u16 attr_id)
  401. {
  402. struct opa_veswport_iface_macs *macs_in, *macs_out;
  403. int max_entries = (OPA_VNIC_EMA_DATA - sizeof(*macs_out)) / ETH_ALEN;
  404. struct opa_vnic_adapter *adapter;
  405. adapter = vema_get_vport_adapter(recvd_mad, port);
  406. if (!adapter) {
  407. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  408. return;
  409. }
  410. macs_in = (struct opa_veswport_iface_macs *)recvd_mad->data;
  411. macs_out = (struct opa_veswport_iface_macs *)rsp_mad->data;
  412. macs_out->start_idx = macs_in->start_idx;
  413. if (macs_in->num_macs_in_msg)
  414. macs_out->num_macs_in_msg = macs_in->num_macs_in_msg;
  415. else
  416. macs_out->num_macs_in_msg = cpu_to_be16(max_entries);
  417. if (attr_id == OPA_EM_ATTR_IFACE_MCAST_MACS)
  418. opa_vnic_query_mcast_macs(adapter, macs_out);
  419. else
  420. opa_vnic_query_ucast_macs(adapter, macs_out);
  421. }
  422. /**
  423. * vema_get_summary_counters -- Gets summary counters.
  424. * @port: source port on which MAD was received
  425. * @recvd_mad: Received mad contains fields to set vnic parameters
  426. * @rsp_mad: Response mad to be built
  427. */
  428. static void vema_get_summary_counters(struct opa_vnic_vema_port *port,
  429. struct opa_vnic_vema_mad *recvd_mad,
  430. struct opa_vnic_vema_mad *rsp_mad)
  431. {
  432. struct opa_veswport_summary_counters *cntrs;
  433. struct opa_vnic_adapter *adapter;
  434. adapter = vema_get_vport_adapter(recvd_mad, port);
  435. if (adapter) {
  436. cntrs = (struct opa_veswport_summary_counters *)rsp_mad->data;
  437. opa_vnic_get_summary_counters(adapter, cntrs);
  438. } else {
  439. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  440. }
  441. }
  442. /**
  443. * vema_get_error_counters -- Gets summary counters.
  444. * @port: source port on which MAD was received
  445. * @recvd_mad: Received mad contains fields to set vnic parameters
  446. * @rsp_mad: Response mad to be built
  447. */
  448. static void vema_get_error_counters(struct opa_vnic_vema_port *port,
  449. struct opa_vnic_vema_mad *recvd_mad,
  450. struct opa_vnic_vema_mad *rsp_mad)
  451. {
  452. struct opa_veswport_error_counters *cntrs;
  453. struct opa_vnic_adapter *adapter;
  454. adapter = vema_get_vport_adapter(recvd_mad, port);
  455. if (adapter) {
  456. cntrs = (struct opa_veswport_error_counters *)rsp_mad->data;
  457. opa_vnic_get_error_counters(adapter, cntrs);
  458. } else {
  459. rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
  460. }
  461. }
  462. /**
  463. * vema_get -- Process received get MAD
  464. * @port: source port on which MAD was received
  465. * @recvd_mad: Received mad
  466. * @rsp_mad: Response mad to be built
  467. */
  468. static void vema_get(struct opa_vnic_vema_port *port,
  469. struct opa_vnic_vema_mad *recvd_mad,
  470. struct opa_vnic_vema_mad *rsp_mad)
  471. {
  472. u16 attr_id = be16_to_cpu(recvd_mad->mad_hdr.attr_id);
  473. switch (attr_id) {
  474. case OPA_EM_ATTR_CLASS_PORT_INFO:
  475. vema_get_class_port_info(port, recvd_mad, rsp_mad);
  476. break;
  477. case OPA_EM_ATTR_VESWPORT_INFO:
  478. vema_get_veswport_info(port, recvd_mad, rsp_mad);
  479. break;
  480. case OPA_EM_ATTR_VESWPORT_MAC_ENTRIES:
  481. vema_get_mac_entries(port, recvd_mad, rsp_mad);
  482. break;
  483. case OPA_EM_ATTR_IFACE_UCAST_MACS:
  484. /* fall through */
  485. case OPA_EM_ATTR_IFACE_MCAST_MACS:
  486. vema_get_mac_list(port, recvd_mad, rsp_mad, attr_id);
  487. break;
  488. case OPA_EM_ATTR_VESWPORT_SUMMARY_COUNTERS:
  489. vema_get_summary_counters(port, recvd_mad, rsp_mad);
  490. break;
  491. case OPA_EM_ATTR_VESWPORT_ERROR_COUNTERS:
  492. vema_get_error_counters(port, recvd_mad, rsp_mad);
  493. break;
  494. default:
  495. rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
  496. break;
  497. }
  498. }
  499. /**
  500. * vema_set -- Process received set MAD
  501. * @port: source port on which MAD was received
  502. * @recvd_mad: Received mad contains fields to set vnic parameters
  503. * @rsp_mad: Response mad to be built
  504. */
  505. static void vema_set(struct opa_vnic_vema_port *port,
  506. struct opa_vnic_vema_mad *recvd_mad,
  507. struct opa_vnic_vema_mad *rsp_mad)
  508. {
  509. u16 attr_id = be16_to_cpu(recvd_mad->mad_hdr.attr_id);
  510. switch (attr_id) {
  511. case OPA_EM_ATTR_CLASS_PORT_INFO:
  512. vema_set_class_port_info(port, recvd_mad, rsp_mad);
  513. break;
  514. case OPA_EM_ATTR_VESWPORT_INFO:
  515. vema_set_veswport_info(port, recvd_mad, rsp_mad);
  516. break;
  517. case OPA_EM_ATTR_VESWPORT_MAC_ENTRIES:
  518. vema_set_mac_entries(port, recvd_mad, rsp_mad);
  519. break;
  520. case OPA_EM_ATTR_DELETE_VESW:
  521. vema_set_delete_vesw(port, recvd_mad, rsp_mad);
  522. break;
  523. default:
  524. rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
  525. break;
  526. }
  527. }
  528. /**
  529. * vema_send -- Send handler for VEMA MAD agent
  530. * @mad_agent: pointer to the mad agent
  531. * @mad_wc: pointer to mad send work completion information
  532. *
  533. * Free all the data structures associated with the sent MAD
  534. */
  535. static void vema_send(struct ib_mad_agent *mad_agent,
  536. struct ib_mad_send_wc *mad_wc)
  537. {
  538. rdma_destroy_ah(mad_wc->send_buf->ah);
  539. ib_free_send_mad(mad_wc->send_buf);
  540. }
  541. /**
  542. * vema_recv -- Recv handler for VEMA MAD agent
  543. * @mad_agent: pointer to the mad agent
  544. * @send_buf: Send buffer if found, else NULL
  545. * @mad_wc: pointer to mad send work completion information
  546. *
  547. * Handle only set and get methods and respond to other methods
  548. * as unsupported. Allocate response buffer and address handle
  549. * for the response MAD.
  550. */
  551. static void vema_recv(struct ib_mad_agent *mad_agent,
  552. struct ib_mad_send_buf *send_buf,
  553. struct ib_mad_recv_wc *mad_wc)
  554. {
  555. struct opa_vnic_vema_port *port;
  556. struct ib_ah *ah;
  557. struct ib_mad_send_buf *rsp;
  558. struct opa_vnic_vema_mad *vema_mad;
  559. if (!mad_wc || !mad_wc->recv_buf.mad)
  560. return;
  561. port = mad_agent->context;
  562. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  563. mad_wc->recv_buf.grh, mad_agent->port_num);
  564. if (IS_ERR(ah))
  565. goto free_recv_mad;
  566. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  567. mad_wc->wc->pkey_index, 0,
  568. IB_MGMT_VENDOR_HDR, OPA_VNIC_EMA_DATA,
  569. GFP_KERNEL, OPA_MGMT_BASE_VERSION);
  570. if (IS_ERR(rsp))
  571. goto err_rsp;
  572. rsp->ah = ah;
  573. vema_mad = rsp->mad;
  574. memcpy(vema_mad, mad_wc->recv_buf.mad, IB_MGMT_VENDOR_HDR);
  575. vema_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  576. vema_mad->mad_hdr.status = 0;
  577. /* Lock ensures network adapter is not removed */
  578. mutex_lock(&port->lock);
  579. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  580. case IB_MGMT_METHOD_GET:
  581. vema_get(port, (struct opa_vnic_vema_mad *)mad_wc->recv_buf.mad,
  582. vema_mad);
  583. break;
  584. case IB_MGMT_METHOD_SET:
  585. vema_set(port, (struct opa_vnic_vema_mad *)mad_wc->recv_buf.mad,
  586. vema_mad);
  587. break;
  588. default:
  589. vema_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
  590. break;
  591. }
  592. mutex_unlock(&port->lock);
  593. if (!ib_post_send_mad(rsp, NULL)) {
  594. /*
  595. * with post send successful ah and send mad
  596. * will be destroyed in send handler
  597. */
  598. goto free_recv_mad;
  599. }
  600. ib_free_send_mad(rsp);
  601. err_rsp:
  602. rdma_destroy_ah(ah);
  603. free_recv_mad:
  604. ib_free_recv_mad(mad_wc);
  605. }
  606. /**
  607. * vema_get_port -- Gets the opa_vnic_vema_port
  608. * @cport: pointer to control dev
  609. * @port_num: Port number
  610. *
  611. * This function loops through the ports and returns
  612. * the opa_vnic_vema port structure that is associated
  613. * with the OPA port number
  614. *
  615. * Return: ptr to requested opa_vnic_vema_port strucure
  616. * if success, NULL if not
  617. */
  618. static struct opa_vnic_vema_port *
  619. vema_get_port(struct opa_vnic_ctrl_port *cport, u8 port_num)
  620. {
  621. struct opa_vnic_vema_port *port = (void *)cport + sizeof(*cport);
  622. if (port_num > cport->num_ports)
  623. return NULL;
  624. return port + (port_num - 1);
  625. }
  626. /**
  627. * opa_vnic_vema_send_trap -- This function sends a trap to the EM
  628. * @cport: pointer to vnic control port
  629. * @data: pointer to trap data filled by calling function
  630. * @lid: issuers lid (encap_slid from vesw_port_info)
  631. *
  632. * This function is called from the VNIC driver to send a trap if there
  633. * is somethng the EM should be notified about. These events currently
  634. * are
  635. * 1) UNICAST INTERFACE MACADDRESS changes
  636. * 2) MULTICAST INTERFACE MACADDRESS changes
  637. * 3) ETHERNET LINK STATUS changes
  638. * While allocating the send mad the remote site qpn used is 1
  639. * as this is the well known QP.
  640. *
  641. */
  642. void opa_vnic_vema_send_trap(struct opa_vnic_adapter *adapter,
  643. struct __opa_veswport_trap *data, u32 lid)
  644. {
  645. struct opa_vnic_ctrl_port *cport = adapter->cport;
  646. struct ib_mad_send_buf *send_buf;
  647. struct opa_vnic_vema_port *port;
  648. struct ib_device *ibp;
  649. struct opa_vnic_vema_mad_trap *trap_mad;
  650. struct opa_class_port_info *class;
  651. struct rdma_ah_attr ah_attr;
  652. struct ib_ah *ah;
  653. struct opa_veswport_trap *trap;
  654. u32 trap_lid;
  655. u16 pkey_idx;
  656. if (!cport)
  657. goto err_exit;
  658. ibp = cport->ibdev;
  659. port = vema_get_port(cport, data->opaportnum);
  660. if (!port || !port->mad_agent)
  661. goto err_exit;
  662. if (time_before(jiffies, adapter->trap_timeout)) {
  663. if (adapter->trap_count == OPA_VNIC_TRAP_BURST_LIMIT) {
  664. v_warn("Trap rate exceeded\n");
  665. goto err_exit;
  666. } else {
  667. adapter->trap_count++;
  668. }
  669. } else {
  670. adapter->trap_count = 0;
  671. }
  672. class = &port->class_port_info;
  673. /* Set up address handle */
  674. memset(&ah_attr, 0, sizeof(ah_attr));
  675. ah_attr.type = rdma_ah_find_type(ibp, port->port_num);
  676. rdma_ah_set_sl(&ah_attr,
  677. GET_TRAP_SL_FROM_CLASS_PORT_INFO(class->trap_sl_rsvd));
  678. rdma_ah_set_port_num(&ah_attr, port->port_num);
  679. trap_lid = be32_to_cpu(class->trap_lid);
  680. /*
  681. * check for trap lid validity, must not be zero
  682. * The trap sink could change after we fashion the MAD but since traps
  683. * are not guaranteed we won't use a lock as anyway the change will take
  684. * place even with locking.
  685. */
  686. if (!trap_lid) {
  687. c_err("%s: Invalid dlid\n", __func__);
  688. goto err_exit;
  689. }
  690. rdma_ah_set_dlid(&ah_attr, trap_lid);
  691. ah = rdma_create_ah(port->mad_agent->qp->pd, &ah_attr);
  692. if (IS_ERR(ah)) {
  693. c_err("%s:Couldn't create new AH = %p\n", __func__, ah);
  694. c_err("%s:dlid = %d, sl = %d, port = %d\n", __func__,
  695. rdma_ah_get_dlid(&ah_attr), rdma_ah_get_sl(&ah_attr),
  696. rdma_ah_get_port_num(&ah_attr));
  697. goto err_exit;
  698. }
  699. if (ib_find_pkey(ibp, data->opaportnum, IB_DEFAULT_PKEY_FULL,
  700. &pkey_idx) < 0) {
  701. c_err("%s:full key not found, defaulting to partial\n",
  702. __func__);
  703. if (ib_find_pkey(ibp, data->opaportnum, IB_DEFAULT_PKEY_PARTIAL,
  704. &pkey_idx) < 0)
  705. pkey_idx = 1;
  706. }
  707. send_buf = ib_create_send_mad(port->mad_agent, 1, pkey_idx, 0,
  708. IB_MGMT_VENDOR_HDR, IB_MGMT_MAD_DATA,
  709. GFP_KERNEL, OPA_MGMT_BASE_VERSION);
  710. if (IS_ERR(send_buf)) {
  711. c_err("%s:Couldn't allocate send buf\n", __func__);
  712. goto err_sndbuf;
  713. }
  714. send_buf->ah = ah;
  715. /* Set up common MAD hdr */
  716. trap_mad = send_buf->mad;
  717. trap_mad->mad_hdr.base_version = OPA_MGMT_BASE_VERSION;
  718. trap_mad->mad_hdr.mgmt_class = OPA_MGMT_CLASS_INTEL_EMA;
  719. trap_mad->mad_hdr.class_version = OPA_EMA_CLASS_VERSION;
  720. trap_mad->mad_hdr.method = IB_MGMT_METHOD_TRAP;
  721. port->tid++;
  722. trap_mad->mad_hdr.tid = cpu_to_be64(port->tid);
  723. trap_mad->mad_hdr.attr_id = IB_SMP_ATTR_NOTICE;
  724. /* Set up vendor OUI */
  725. trap_mad->oui[0] = INTEL_OUI_1;
  726. trap_mad->oui[1] = INTEL_OUI_2;
  727. trap_mad->oui[2] = INTEL_OUI_3;
  728. /* Setup notice attribute portion */
  729. trap_mad->notice.gen_type = OPA_INTEL_EMA_NOTICE_TYPE_INFO << 1;
  730. trap_mad->notice.oui_1 = INTEL_OUI_1;
  731. trap_mad->notice.oui_2 = INTEL_OUI_2;
  732. trap_mad->notice.oui_3 = INTEL_OUI_3;
  733. trap_mad->notice.issuer_lid = cpu_to_be32(lid);
  734. /* copy the actual trap data */
  735. trap = (struct opa_veswport_trap *)trap_mad->notice.raw_data;
  736. trap->fabric_id = cpu_to_be16(data->fabric_id);
  737. trap->veswid = cpu_to_be16(data->veswid);
  738. trap->veswportnum = cpu_to_be32(data->veswportnum);
  739. trap->opaportnum = cpu_to_be16(data->opaportnum);
  740. trap->veswportindex = data->veswportindex;
  741. trap->opcode = data->opcode;
  742. /* If successful send set up rate limit timeout else bail */
  743. if (ib_post_send_mad(send_buf, NULL)) {
  744. ib_free_send_mad(send_buf);
  745. } else {
  746. if (adapter->trap_count)
  747. return;
  748. adapter->trap_timeout = jiffies +
  749. usecs_to_jiffies(OPA_VNIC_TRAP_TIMEOUT);
  750. return;
  751. }
  752. err_sndbuf:
  753. rdma_destroy_ah(ah);
  754. err_exit:
  755. v_err("Aborting trap\n");
  756. }
  757. static int vema_rem_vport(int id, void *p, void *data)
  758. {
  759. struct opa_vnic_adapter *adapter = p;
  760. opa_vnic_rem_netdev(adapter);
  761. return 0;
  762. }
  763. static int vema_enable_vport(int id, void *p, void *data)
  764. {
  765. struct opa_vnic_adapter *adapter = p;
  766. netif_carrier_on(adapter->netdev);
  767. return 0;
  768. }
  769. static int vema_disable_vport(int id, void *p, void *data)
  770. {
  771. struct opa_vnic_adapter *adapter = p;
  772. netif_carrier_off(adapter->netdev);
  773. return 0;
  774. }
  775. static void opa_vnic_event(struct ib_event_handler *handler,
  776. struct ib_event *record)
  777. {
  778. struct opa_vnic_vema_port *port =
  779. container_of(handler, struct opa_vnic_vema_port, event_handler);
  780. struct opa_vnic_ctrl_port *cport = port->cport;
  781. if (record->element.port_num != port->port_num)
  782. return;
  783. c_dbg("OPA_VNIC received event %d on device %s port %d\n",
  784. record->event, record->device->name, record->element.port_num);
  785. if (record->event == IB_EVENT_PORT_ERR)
  786. idr_for_each(&port->vport_idr, vema_disable_vport, NULL);
  787. if (record->event == IB_EVENT_PORT_ACTIVE)
  788. idr_for_each(&port->vport_idr, vema_enable_vport, NULL);
  789. }
  790. /**
  791. * vema_unregister -- Unregisters agent
  792. * @cport: pointer to control port
  793. *
  794. * This deletes the registration by VEMA for MADs
  795. */
  796. static void vema_unregister(struct opa_vnic_ctrl_port *cport)
  797. {
  798. int i;
  799. for (i = 1; i <= cport->num_ports; i++) {
  800. struct opa_vnic_vema_port *port = vema_get_port(cport, i);
  801. if (!port->mad_agent)
  802. continue;
  803. /* Lock ensures no MAD is being processed */
  804. mutex_lock(&port->lock);
  805. idr_for_each(&port->vport_idr, vema_rem_vport, NULL);
  806. mutex_unlock(&port->lock);
  807. ib_unregister_mad_agent(port->mad_agent);
  808. port->mad_agent = NULL;
  809. mutex_destroy(&port->lock);
  810. idr_destroy(&port->vport_idr);
  811. ib_unregister_event_handler(&port->event_handler);
  812. }
  813. }
  814. /**
  815. * vema_register -- Registers agent
  816. * @cport: pointer to control port
  817. *
  818. * This function registers the handlers for the VEMA MADs
  819. *
  820. * Return: returns 0 on success. non zero otherwise
  821. */
  822. static int vema_register(struct opa_vnic_ctrl_port *cport)
  823. {
  824. struct ib_mad_reg_req reg_req = {
  825. .mgmt_class = OPA_MGMT_CLASS_INTEL_EMA,
  826. .mgmt_class_version = OPA_MGMT_BASE_VERSION,
  827. .oui = { INTEL_OUI_1, INTEL_OUI_2, INTEL_OUI_3 }
  828. };
  829. int i;
  830. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  831. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  832. /* register ib event handler and mad agent for each port on dev */
  833. for (i = 1; i <= cport->num_ports; i++) {
  834. struct opa_vnic_vema_port *port = vema_get_port(cport, i);
  835. int ret;
  836. port->cport = cport;
  837. port->port_num = i;
  838. INIT_IB_EVENT_HANDLER(&port->event_handler,
  839. cport->ibdev, opa_vnic_event);
  840. ret = ib_register_event_handler(&port->event_handler);
  841. if (ret) {
  842. c_err("port %d: event handler register failed\n", i);
  843. vema_unregister(cport);
  844. return ret;
  845. }
  846. idr_init(&port->vport_idr);
  847. mutex_init(&port->lock);
  848. port->mad_agent = ib_register_mad_agent(cport->ibdev, i,
  849. IB_QPT_GSI, &reg_req,
  850. IB_MGMT_RMPP_VERSION,
  851. vema_send, vema_recv,
  852. port, 0);
  853. if (IS_ERR(port->mad_agent)) {
  854. ret = PTR_ERR(port->mad_agent);
  855. port->mad_agent = NULL;
  856. mutex_destroy(&port->lock);
  857. idr_destroy(&port->vport_idr);
  858. vema_unregister(cport);
  859. return ret;
  860. }
  861. }
  862. return 0;
  863. }
  864. /**
  865. * opa_vnic_vema_add_one -- Handle new ib device
  866. * @device: ib device pointer
  867. *
  868. * Allocate the vnic control port and initialize it.
  869. */
  870. static void opa_vnic_vema_add_one(struct ib_device *device)
  871. {
  872. struct opa_vnic_ctrl_port *cport;
  873. int rc, size = sizeof(*cport);
  874. if (!rdma_cap_opa_vnic(device))
  875. return;
  876. size += device->phys_port_cnt * sizeof(struct opa_vnic_vema_port);
  877. cport = kzalloc(size, GFP_KERNEL);
  878. if (!cport)
  879. return;
  880. cport->num_ports = device->phys_port_cnt;
  881. cport->ibdev = device;
  882. /* Initialize opa vnic management agent (vema) */
  883. rc = vema_register(cport);
  884. if (!rc)
  885. c_info("VNIC client initialized\n");
  886. ib_set_client_data(device, &opa_vnic_client, cport);
  887. }
  888. /**
  889. * opa_vnic_vema_rem_one -- Handle ib device removal
  890. * @device: ib device pointer
  891. * @client_data: ib client data
  892. *
  893. * Uninitialize and free the vnic control port.
  894. */
  895. static void opa_vnic_vema_rem_one(struct ib_device *device,
  896. void *client_data)
  897. {
  898. struct opa_vnic_ctrl_port *cport = client_data;
  899. if (!cport)
  900. return;
  901. c_info("removing VNIC client\n");
  902. vema_unregister(cport);
  903. kfree(cport);
  904. }
  905. static int __init opa_vnic_init(void)
  906. {
  907. int rc;
  908. pr_info("OPA Virtual Network Driver - v%s\n",
  909. opa_vnic_driver_version);
  910. rc = ib_register_client(&opa_vnic_client);
  911. if (rc)
  912. pr_err("VNIC driver register failed %d\n", rc);
  913. return rc;
  914. }
  915. module_init(opa_vnic_init);
  916. static void opa_vnic_deinit(void)
  917. {
  918. ib_unregister_client(&opa_vnic_client);
  919. }
  920. module_exit(opa_vnic_deinit);
  921. MODULE_LICENSE("Dual BSD/GPL");
  922. MODULE_AUTHOR("Intel Corporation");
  923. MODULE_DESCRIPTION("Intel OPA Virtual Network driver");
  924. MODULE_VERSION(DRV_VERSION);