i40evf_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include <linux/list.h>
  4. #include <linux/errno.h>
  5. #include "i40evf.h"
  6. #include "i40e_prototype.h"
  7. #include "i40evf_client.h"
  8. static
  9. const char i40evf_client_interface_version_str[] = I40EVF_CLIENT_VERSION_STR;
  10. static struct i40e_client *vf_registered_client;
  11. static LIST_HEAD(i40evf_devices);
  12. static DEFINE_MUTEX(i40evf_device_mutex);
  13. static u32 i40evf_client_virtchnl_send(struct i40e_info *ldev,
  14. struct i40e_client *client,
  15. u8 *msg, u16 len);
  16. static int i40evf_client_setup_qvlist(struct i40e_info *ldev,
  17. struct i40e_client *client,
  18. struct i40e_qvlist_info *qvlist_info);
  19. static struct i40e_ops i40evf_lan_ops = {
  20. .virtchnl_send = i40evf_client_virtchnl_send,
  21. .setup_qvlist = i40evf_client_setup_qvlist,
  22. };
  23. /**
  24. * i40evf_client_get_params - retrieve relevant client parameters
  25. * @vsi: VSI with parameters
  26. * @params: client param struct
  27. **/
  28. static
  29. void i40evf_client_get_params(struct i40e_vsi *vsi, struct i40e_params *params)
  30. {
  31. int i;
  32. memset(params, 0, sizeof(struct i40e_params));
  33. params->mtu = vsi->netdev->mtu;
  34. params->link_up = vsi->back->link_up;
  35. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  36. params->qos.prio_qos[i].tc = 0;
  37. params->qos.prio_qos[i].qs_handle = vsi->qs_handle;
  38. }
  39. }
  40. /**
  41. * i40evf_notify_client_message - call the client message receive callback
  42. * @vsi: the VSI associated with this client
  43. * @msg: message buffer
  44. * @len: length of message
  45. *
  46. * If there is a client to this VSI, call the client
  47. **/
  48. void i40evf_notify_client_message(struct i40e_vsi *vsi, u8 *msg, u16 len)
  49. {
  50. struct i40e_client_instance *cinst;
  51. if (!vsi)
  52. return;
  53. cinst = vsi->back->cinst;
  54. if (!cinst || !cinst->client || !cinst->client->ops ||
  55. !cinst->client->ops->virtchnl_receive) {
  56. dev_dbg(&vsi->back->pdev->dev,
  57. "Cannot locate client instance virtchnl_receive function\n");
  58. return;
  59. }
  60. cinst->client->ops->virtchnl_receive(&cinst->lan_info, cinst->client,
  61. msg, len);
  62. }
  63. /**
  64. * i40evf_notify_client_l2_params - call the client notify callback
  65. * @vsi: the VSI with l2 param changes
  66. *
  67. * If there is a client to this VSI, call the client
  68. **/
  69. void i40evf_notify_client_l2_params(struct i40e_vsi *vsi)
  70. {
  71. struct i40e_client_instance *cinst;
  72. struct i40e_params params;
  73. if (!vsi)
  74. return;
  75. cinst = vsi->back->cinst;
  76. if (!cinst || !cinst->client || !cinst->client->ops ||
  77. !cinst->client->ops->l2_param_change) {
  78. dev_dbg(&vsi->back->pdev->dev,
  79. "Cannot locate client instance l2_param_change function\n");
  80. return;
  81. }
  82. i40evf_client_get_params(vsi, &params);
  83. cinst->lan_info.params = params;
  84. cinst->client->ops->l2_param_change(&cinst->lan_info, cinst->client,
  85. &params);
  86. }
  87. /**
  88. * i40evf_notify_client_open - call the client open callback
  89. * @vsi: the VSI with netdev opened
  90. *
  91. * If there is a client to this netdev, call the client with open
  92. **/
  93. void i40evf_notify_client_open(struct i40e_vsi *vsi)
  94. {
  95. struct i40evf_adapter *adapter = vsi->back;
  96. struct i40e_client_instance *cinst = adapter->cinst;
  97. int ret;
  98. if (!cinst || !cinst->client || !cinst->client->ops ||
  99. !cinst->client->ops->open) {
  100. dev_dbg(&vsi->back->pdev->dev,
  101. "Cannot locate client instance open function\n");
  102. return;
  103. }
  104. if (!(test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state))) {
  105. ret = cinst->client->ops->open(&cinst->lan_info, cinst->client);
  106. if (!ret)
  107. set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state);
  108. }
  109. }
  110. /**
  111. * i40evf_client_release_qvlist - send a message to the PF to release iwarp qv map
  112. * @ldev: pointer to L2 context.
  113. *
  114. * Return 0 on success or < 0 on error
  115. **/
  116. static int i40evf_client_release_qvlist(struct i40e_info *ldev)
  117. {
  118. struct i40evf_adapter *adapter = ldev->vf;
  119. i40e_status err;
  120. if (adapter->aq_required)
  121. return -EAGAIN;
  122. err = i40e_aq_send_msg_to_pf(&adapter->hw,
  123. VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
  124. I40E_SUCCESS, NULL, 0, NULL);
  125. if (err)
  126. dev_err(&adapter->pdev->dev,
  127. "Unable to send iWarp vector release message to PF, error %d, aq status %d\n",
  128. err, adapter->hw.aq.asq_last_status);
  129. return err;
  130. }
  131. /**
  132. * i40evf_notify_client_close - call the client close callback
  133. * @vsi: the VSI with netdev closed
  134. * @reset: true when close called due to reset pending
  135. *
  136. * If there is a client to this netdev, call the client with close
  137. **/
  138. void i40evf_notify_client_close(struct i40e_vsi *vsi, bool reset)
  139. {
  140. struct i40evf_adapter *adapter = vsi->back;
  141. struct i40e_client_instance *cinst = adapter->cinst;
  142. if (!cinst || !cinst->client || !cinst->client->ops ||
  143. !cinst->client->ops->close) {
  144. dev_dbg(&vsi->back->pdev->dev,
  145. "Cannot locate client instance close function\n");
  146. return;
  147. }
  148. cinst->client->ops->close(&cinst->lan_info, cinst->client, reset);
  149. i40evf_client_release_qvlist(&cinst->lan_info);
  150. clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state);
  151. }
  152. /**
  153. * i40evf_client_add_instance - add a client instance to the instance list
  154. * @adapter: pointer to the board struct
  155. *
  156. * Returns cinst ptr on success, NULL on failure
  157. **/
  158. static struct i40e_client_instance *
  159. i40evf_client_add_instance(struct i40evf_adapter *adapter)
  160. {
  161. struct i40e_client_instance *cinst = NULL;
  162. struct i40e_vsi *vsi = &adapter->vsi;
  163. struct netdev_hw_addr *mac = NULL;
  164. struct i40e_params params;
  165. if (!vf_registered_client)
  166. goto out;
  167. if (adapter->cinst) {
  168. cinst = adapter->cinst;
  169. goto out;
  170. }
  171. cinst = kzalloc(sizeof(*cinst), GFP_KERNEL);
  172. if (!cinst)
  173. goto out;
  174. cinst->lan_info.vf = (void *)adapter;
  175. cinst->lan_info.netdev = vsi->netdev;
  176. cinst->lan_info.pcidev = adapter->pdev;
  177. cinst->lan_info.fid = 0;
  178. cinst->lan_info.ftype = I40E_CLIENT_FTYPE_VF;
  179. cinst->lan_info.hw_addr = adapter->hw.hw_addr;
  180. cinst->lan_info.ops = &i40evf_lan_ops;
  181. cinst->lan_info.version.major = I40EVF_CLIENT_VERSION_MAJOR;
  182. cinst->lan_info.version.minor = I40EVF_CLIENT_VERSION_MINOR;
  183. cinst->lan_info.version.build = I40EVF_CLIENT_VERSION_BUILD;
  184. i40evf_client_get_params(vsi, &params);
  185. cinst->lan_info.params = params;
  186. set_bit(__I40E_CLIENT_INSTANCE_NONE, &cinst->state);
  187. cinst->lan_info.msix_count = adapter->num_iwarp_msix;
  188. cinst->lan_info.msix_entries =
  189. &adapter->msix_entries[adapter->iwarp_base_vector];
  190. mac = list_first_entry(&cinst->lan_info.netdev->dev_addrs.list,
  191. struct netdev_hw_addr, list);
  192. if (mac)
  193. ether_addr_copy(cinst->lan_info.lanmac, mac->addr);
  194. else
  195. dev_err(&adapter->pdev->dev, "MAC address list is empty!\n");
  196. cinst->client = vf_registered_client;
  197. adapter->cinst = cinst;
  198. out:
  199. return cinst;
  200. }
  201. /**
  202. * i40evf_client_del_instance - removes a client instance from the list
  203. * @adapter: pointer to the board struct
  204. *
  205. **/
  206. static
  207. void i40evf_client_del_instance(struct i40evf_adapter *adapter)
  208. {
  209. kfree(adapter->cinst);
  210. adapter->cinst = NULL;
  211. }
  212. /**
  213. * i40evf_client_subtask - client maintenance work
  214. * @adapter: board private structure
  215. **/
  216. void i40evf_client_subtask(struct i40evf_adapter *adapter)
  217. {
  218. struct i40e_client *client = vf_registered_client;
  219. struct i40e_client_instance *cinst;
  220. int ret = 0;
  221. if (adapter->state < __I40EVF_DOWN)
  222. return;
  223. /* first check client is registered */
  224. if (!client)
  225. return;
  226. /* Add the client instance to the instance list */
  227. cinst = i40evf_client_add_instance(adapter);
  228. if (!cinst)
  229. return;
  230. dev_info(&adapter->pdev->dev, "Added instance of Client %s\n",
  231. client->name);
  232. if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state)) {
  233. /* Send an Open request to the client */
  234. if (client->ops && client->ops->open)
  235. ret = client->ops->open(&cinst->lan_info, client);
  236. if (!ret)
  237. set_bit(__I40E_CLIENT_INSTANCE_OPENED,
  238. &cinst->state);
  239. else
  240. /* remove client instance */
  241. i40evf_client_del_instance(adapter);
  242. }
  243. }
  244. /**
  245. * i40evf_lan_add_device - add a lan device struct to the list of lan devices
  246. * @adapter: pointer to the board struct
  247. *
  248. * Returns 0 on success or none 0 on error
  249. **/
  250. int i40evf_lan_add_device(struct i40evf_adapter *adapter)
  251. {
  252. struct i40e_device *ldev;
  253. int ret = 0;
  254. mutex_lock(&i40evf_device_mutex);
  255. list_for_each_entry(ldev, &i40evf_devices, list) {
  256. if (ldev->vf == adapter) {
  257. ret = -EEXIST;
  258. goto out;
  259. }
  260. }
  261. ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
  262. if (!ldev) {
  263. ret = -ENOMEM;
  264. goto out;
  265. }
  266. ldev->vf = adapter;
  267. INIT_LIST_HEAD(&ldev->list);
  268. list_add(&ldev->list, &i40evf_devices);
  269. dev_info(&adapter->pdev->dev, "Added LAN device bus=0x%02x dev=0x%02x func=0x%02x\n",
  270. adapter->hw.bus.bus_id, adapter->hw.bus.device,
  271. adapter->hw.bus.func);
  272. /* Since in some cases register may have happened before a device gets
  273. * added, we can schedule a subtask to go initiate the clients.
  274. */
  275. adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  276. out:
  277. mutex_unlock(&i40evf_device_mutex);
  278. return ret;
  279. }
  280. /**
  281. * i40evf_lan_del_device - removes a lan device from the device list
  282. * @adapter: pointer to the board struct
  283. *
  284. * Returns 0 on success or non-0 on error
  285. **/
  286. int i40evf_lan_del_device(struct i40evf_adapter *adapter)
  287. {
  288. struct i40e_device *ldev, *tmp;
  289. int ret = -ENODEV;
  290. mutex_lock(&i40evf_device_mutex);
  291. list_for_each_entry_safe(ldev, tmp, &i40evf_devices, list) {
  292. if (ldev->vf == adapter) {
  293. dev_info(&adapter->pdev->dev,
  294. "Deleted LAN device bus=0x%02x dev=0x%02x func=0x%02x\n",
  295. adapter->hw.bus.bus_id, adapter->hw.bus.device,
  296. adapter->hw.bus.func);
  297. list_del(&ldev->list);
  298. kfree(ldev);
  299. ret = 0;
  300. break;
  301. }
  302. }
  303. mutex_unlock(&i40evf_device_mutex);
  304. return ret;
  305. }
  306. /**
  307. * i40evf_client_release - release client specific resources
  308. * @client: pointer to the registered client
  309. *
  310. **/
  311. static void i40evf_client_release(struct i40e_client *client)
  312. {
  313. struct i40e_client_instance *cinst;
  314. struct i40e_device *ldev;
  315. struct i40evf_adapter *adapter;
  316. mutex_lock(&i40evf_device_mutex);
  317. list_for_each_entry(ldev, &i40evf_devices, list) {
  318. adapter = ldev->vf;
  319. cinst = adapter->cinst;
  320. if (!cinst)
  321. continue;
  322. if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state)) {
  323. if (client->ops && client->ops->close)
  324. client->ops->close(&cinst->lan_info, client,
  325. false);
  326. i40evf_client_release_qvlist(&cinst->lan_info);
  327. clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state);
  328. dev_warn(&adapter->pdev->dev,
  329. "Client %s instance closed\n", client->name);
  330. }
  331. /* delete the client instance */
  332. i40evf_client_del_instance(adapter);
  333. dev_info(&adapter->pdev->dev, "Deleted client instance of Client %s\n",
  334. client->name);
  335. }
  336. mutex_unlock(&i40evf_device_mutex);
  337. }
  338. /**
  339. * i40evf_client_prepare - prepare client specific resources
  340. * @client: pointer to the registered client
  341. *
  342. **/
  343. static void i40evf_client_prepare(struct i40e_client *client)
  344. {
  345. struct i40e_device *ldev;
  346. struct i40evf_adapter *adapter;
  347. mutex_lock(&i40evf_device_mutex);
  348. list_for_each_entry(ldev, &i40evf_devices, list) {
  349. adapter = ldev->vf;
  350. /* Signal the watchdog to service the client */
  351. adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  352. }
  353. mutex_unlock(&i40evf_device_mutex);
  354. }
  355. /**
  356. * i40evf_client_virtchnl_send - send a message to the PF instance
  357. * @ldev: pointer to L2 context.
  358. * @client: Client pointer.
  359. * @msg: pointer to message buffer
  360. * @len: message length
  361. *
  362. * Return 0 on success or < 0 on error
  363. **/
  364. static u32 i40evf_client_virtchnl_send(struct i40e_info *ldev,
  365. struct i40e_client *client,
  366. u8 *msg, u16 len)
  367. {
  368. struct i40evf_adapter *adapter = ldev->vf;
  369. i40e_status err;
  370. if (adapter->aq_required)
  371. return -EAGAIN;
  372. err = i40e_aq_send_msg_to_pf(&adapter->hw, VIRTCHNL_OP_IWARP,
  373. I40E_SUCCESS, msg, len, NULL);
  374. if (err)
  375. dev_err(&adapter->pdev->dev, "Unable to send iWarp message to PF, error %d, aq status %d\n",
  376. err, adapter->hw.aq.asq_last_status);
  377. return err;
  378. }
  379. /**
  380. * i40evf_client_setup_qvlist - send a message to the PF to setup iwarp qv map
  381. * @ldev: pointer to L2 context.
  382. * @client: Client pointer.
  383. * @qvlist_info: queue and vector list
  384. *
  385. * Return 0 on success or < 0 on error
  386. **/
  387. static int i40evf_client_setup_qvlist(struct i40e_info *ldev,
  388. struct i40e_client *client,
  389. struct i40e_qvlist_info *qvlist_info)
  390. {
  391. struct virtchnl_iwarp_qvlist_info *v_qvlist_info;
  392. struct i40evf_adapter *adapter = ldev->vf;
  393. struct i40e_qv_info *qv_info;
  394. i40e_status err;
  395. u32 v_idx, i;
  396. u32 msg_size;
  397. if (adapter->aq_required)
  398. return -EAGAIN;
  399. /* A quick check on whether the vectors belong to the client */
  400. for (i = 0; i < qvlist_info->num_vectors; i++) {
  401. qv_info = &qvlist_info->qv_info[i];
  402. if (!qv_info)
  403. continue;
  404. v_idx = qv_info->v_idx;
  405. if ((v_idx >=
  406. (adapter->iwarp_base_vector + adapter->num_iwarp_msix)) ||
  407. (v_idx < adapter->iwarp_base_vector))
  408. return -EINVAL;
  409. }
  410. v_qvlist_info = (struct virtchnl_iwarp_qvlist_info *)qvlist_info;
  411. msg_size = sizeof(struct virtchnl_iwarp_qvlist_info) +
  412. (sizeof(struct virtchnl_iwarp_qv_info) *
  413. (v_qvlist_info->num_vectors - 1));
  414. adapter->client_pending |= BIT(VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP);
  415. err = i40e_aq_send_msg_to_pf(&adapter->hw,
  416. VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP,
  417. I40E_SUCCESS, (u8 *)v_qvlist_info, msg_size, NULL);
  418. if (err) {
  419. dev_err(&adapter->pdev->dev,
  420. "Unable to send iWarp vector config message to PF, error %d, aq status %d\n",
  421. err, adapter->hw.aq.asq_last_status);
  422. goto out;
  423. }
  424. err = -EBUSY;
  425. for (i = 0; i < 5; i++) {
  426. msleep(100);
  427. if (!(adapter->client_pending &
  428. BIT(VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP))) {
  429. err = 0;
  430. break;
  431. }
  432. }
  433. out:
  434. return err;
  435. }
  436. /**
  437. * i40evf_register_client - Register a i40e client driver with the L2 driver
  438. * @client: pointer to the i40e_client struct
  439. *
  440. * Returns 0 on success or non-0 on error
  441. **/
  442. int i40evf_register_client(struct i40e_client *client)
  443. {
  444. int ret = 0;
  445. if (!client) {
  446. ret = -EIO;
  447. goto out;
  448. }
  449. if (strlen(client->name) == 0) {
  450. pr_info("i40evf: Failed to register client with no name\n");
  451. ret = -EIO;
  452. goto out;
  453. }
  454. if (vf_registered_client) {
  455. pr_info("i40evf: Client %s has already been registered!\n",
  456. client->name);
  457. ret = -EEXIST;
  458. goto out;
  459. }
  460. if ((client->version.major != I40EVF_CLIENT_VERSION_MAJOR) ||
  461. (client->version.minor != I40EVF_CLIENT_VERSION_MINOR)) {
  462. pr_info("i40evf: Failed to register client %s due to mismatched client interface version\n",
  463. client->name);
  464. pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
  465. client->version.major, client->version.minor,
  466. client->version.build,
  467. i40evf_client_interface_version_str);
  468. ret = -EIO;
  469. goto out;
  470. }
  471. vf_registered_client = client;
  472. i40evf_client_prepare(client);
  473. pr_info("i40evf: Registered client %s with return code %d\n",
  474. client->name, ret);
  475. out:
  476. return ret;
  477. }
  478. EXPORT_SYMBOL(i40evf_register_client);
  479. /**
  480. * i40evf_unregister_client - Unregister a i40e client driver with the L2 driver
  481. * @client: pointer to the i40e_client struct
  482. *
  483. * Returns 0 on success or non-0 on error
  484. **/
  485. int i40evf_unregister_client(struct i40e_client *client)
  486. {
  487. int ret = 0;
  488. /* When a unregister request comes through we would have to send
  489. * a close for each of the client instances that were opened.
  490. * client_release function is called to handle this.
  491. */
  492. i40evf_client_release(client);
  493. if (vf_registered_client != client) {
  494. pr_info("i40evf: Client %s has not been registered\n",
  495. client->name);
  496. ret = -ENODEV;
  497. goto out;
  498. }
  499. vf_registered_client = NULL;
  500. pr_info("i40evf: Unregistered client %s\n", client->name);
  501. out:
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(i40evf_unregister_client);