i40e_client.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/list.h>
  27. #include <linux/errno.h>
  28. #include "i40e.h"
  29. #include "i40e_prototype.h"
  30. #include "i40e_client.h"
  31. static const char i40e_client_interface_version_str[] = I40E_CLIENT_VERSION_STR;
  32. static LIST_HEAD(i40e_devices);
  33. static DEFINE_MUTEX(i40e_device_mutex);
  34. static LIST_HEAD(i40e_clients);
  35. static DEFINE_MUTEX(i40e_client_mutex);
  36. static LIST_HEAD(i40e_client_instances);
  37. static DEFINE_MUTEX(i40e_client_instance_mutex);
  38. static int i40e_client_virtchnl_send(struct i40e_info *ldev,
  39. struct i40e_client *client,
  40. u32 vf_id, u8 *msg, u16 len);
  41. static int i40e_client_setup_qvlist(struct i40e_info *ldev,
  42. struct i40e_client *client,
  43. struct i40e_qvlist_info *qvlist_info);
  44. static void i40e_client_request_reset(struct i40e_info *ldev,
  45. struct i40e_client *client,
  46. u32 reset_level);
  47. static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
  48. struct i40e_client *client,
  49. bool is_vf, u32 vf_id,
  50. u32 flag, u32 valid_flag);
  51. static struct i40e_ops i40e_lan_ops = {
  52. .virtchnl_send = i40e_client_virtchnl_send,
  53. .setup_qvlist = i40e_client_setup_qvlist,
  54. .request_reset = i40e_client_request_reset,
  55. .update_vsi_ctxt = i40e_client_update_vsi_ctxt,
  56. };
  57. /**
  58. * i40e_client_type_to_vsi_type - convert client type to vsi type
  59. * @client_type: the i40e_client type
  60. *
  61. * returns the related vsi type value
  62. **/
  63. static
  64. enum i40e_vsi_type i40e_client_type_to_vsi_type(enum i40e_client_type type)
  65. {
  66. switch (type) {
  67. case I40E_CLIENT_IWARP:
  68. return I40E_VSI_IWARP;
  69. case I40E_CLIENT_VMDQ2:
  70. return I40E_VSI_VMDQ2;
  71. default:
  72. pr_err("i40e: Client type unknown\n");
  73. return I40E_VSI_TYPE_UNKNOWN;
  74. }
  75. }
  76. /**
  77. * i40e_client_get_params - Get the params that can change at runtime
  78. * @vsi: the VSI with the message
  79. * @param: clinet param struct
  80. *
  81. **/
  82. static
  83. int i40e_client_get_params(struct i40e_vsi *vsi, struct i40e_params *params)
  84. {
  85. struct i40e_dcbx_config *dcb_cfg = &vsi->back->hw.local_dcbx_config;
  86. int i = 0;
  87. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  88. u8 tc = dcb_cfg->etscfg.prioritytable[i];
  89. u16 qs_handle;
  90. /* If TC is not enabled for VSI use TC0 for UP */
  91. if (!(vsi->tc_config.enabled_tc & BIT(tc)))
  92. tc = 0;
  93. qs_handle = le16_to_cpu(vsi->info.qs_handle[tc]);
  94. params->qos.prio_qos[i].tc = tc;
  95. params->qos.prio_qos[i].qs_handle = qs_handle;
  96. if (qs_handle == I40E_AQ_VSI_QS_HANDLE_INVALID) {
  97. dev_err(&vsi->back->pdev->dev, "Invalid queue set handle for TC = %d, vsi id = %d\n",
  98. tc, vsi->id);
  99. return -EINVAL;
  100. }
  101. }
  102. params->mtu = vsi->netdev->mtu;
  103. return 0;
  104. }
  105. /**
  106. * i40e_notify_client_of_vf_msg - call the client vf message callback
  107. * @vsi: the VSI with the message
  108. * @vf_id: the absolute VF id that sent the message
  109. * @msg: message buffer
  110. * @len: length of the message
  111. *
  112. * If there is a client to this VSI, call the client
  113. **/
  114. void
  115. i40e_notify_client_of_vf_msg(struct i40e_vsi *vsi, u32 vf_id, u8 *msg, u16 len)
  116. {
  117. struct i40e_client_instance *cdev;
  118. if (!vsi)
  119. return;
  120. mutex_lock(&i40e_client_instance_mutex);
  121. list_for_each_entry(cdev, &i40e_client_instances, list) {
  122. if (cdev->lan_info.pf == vsi->back) {
  123. if (!cdev->client ||
  124. !cdev->client->ops ||
  125. !cdev->client->ops->virtchnl_receive) {
  126. dev_dbg(&vsi->back->pdev->dev,
  127. "Cannot locate client instance virtual channel receive routine\n");
  128. continue;
  129. }
  130. cdev->client->ops->virtchnl_receive(&cdev->lan_info,
  131. cdev->client,
  132. vf_id, msg, len);
  133. }
  134. }
  135. mutex_unlock(&i40e_client_instance_mutex);
  136. }
  137. /**
  138. * i40e_notify_client_of_l2_param_changes - call the client notify callback
  139. * @vsi: the VSI with l2 param changes
  140. *
  141. * If there is a client to this VSI, call the client
  142. **/
  143. void i40e_notify_client_of_l2_param_changes(struct i40e_vsi *vsi)
  144. {
  145. struct i40e_client_instance *cdev;
  146. struct i40e_params params;
  147. if (!vsi)
  148. return;
  149. memset(&params, 0, sizeof(params));
  150. i40e_client_get_params(vsi, &params);
  151. mutex_lock(&i40e_client_instance_mutex);
  152. list_for_each_entry(cdev, &i40e_client_instances, list) {
  153. if (cdev->lan_info.pf == vsi->back) {
  154. if (!cdev->client ||
  155. !cdev->client->ops ||
  156. !cdev->client->ops->l2_param_change) {
  157. dev_dbg(&vsi->back->pdev->dev,
  158. "Cannot locate client instance l2_param_change routine\n");
  159. continue;
  160. }
  161. cdev->lan_info.params = params;
  162. cdev->client->ops->l2_param_change(&cdev->lan_info,
  163. cdev->client,
  164. &params);
  165. }
  166. }
  167. mutex_unlock(&i40e_client_instance_mutex);
  168. }
  169. /**
  170. * i40e_notify_client_of_netdev_open - call the client open callback
  171. * @vsi: the VSI with netdev opened
  172. *
  173. * If there is a client to this netdev, call the client with open
  174. **/
  175. void i40e_notify_client_of_netdev_open(struct i40e_vsi *vsi)
  176. {
  177. struct i40e_client_instance *cdev;
  178. if (!vsi)
  179. return;
  180. mutex_lock(&i40e_client_instance_mutex);
  181. list_for_each_entry(cdev, &i40e_client_instances, list) {
  182. if (cdev->lan_info.netdev == vsi->netdev) {
  183. if (!cdev->client ||
  184. !cdev->client->ops || !cdev->client->ops->open) {
  185. dev_dbg(&vsi->back->pdev->dev,
  186. "Cannot locate client instance open routine\n");
  187. continue;
  188. }
  189. cdev->client->ops->open(&cdev->lan_info, cdev->client);
  190. }
  191. }
  192. mutex_unlock(&i40e_client_instance_mutex);
  193. }
  194. /**
  195. * i40e_client_release_qvlist
  196. * @ldev: pointer to L2 context.
  197. *
  198. **/
  199. static void i40e_client_release_qvlist(struct i40e_info *ldev)
  200. {
  201. struct i40e_qvlist_info *qvlist_info = ldev->qvlist_info;
  202. u32 i;
  203. if (!ldev->qvlist_info)
  204. return;
  205. for (i = 0; i < qvlist_info->num_vectors; i++) {
  206. struct i40e_pf *pf = ldev->pf;
  207. struct i40e_qv_info *qv_info;
  208. u32 reg_idx;
  209. qv_info = &qvlist_info->qv_info[i];
  210. if (!qv_info)
  211. continue;
  212. reg_idx = I40E_PFINT_LNKLSTN(qv_info->v_idx - 1);
  213. wr32(&pf->hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
  214. }
  215. kfree(ldev->qvlist_info);
  216. ldev->qvlist_info = NULL;
  217. }
  218. /**
  219. * i40e_notify_client_of_netdev_close - call the client close callback
  220. * @vsi: the VSI with netdev closed
  221. * @reset: true when close called due to a reset pending
  222. *
  223. * If there is a client to this netdev, call the client with close
  224. **/
  225. void i40e_notify_client_of_netdev_close(struct i40e_vsi *vsi, bool reset)
  226. {
  227. struct i40e_client_instance *cdev;
  228. if (!vsi)
  229. return;
  230. mutex_lock(&i40e_client_instance_mutex);
  231. list_for_each_entry(cdev, &i40e_client_instances, list) {
  232. if (cdev->lan_info.netdev == vsi->netdev) {
  233. if (!cdev->client ||
  234. !cdev->client->ops || !cdev->client->ops->close) {
  235. dev_dbg(&vsi->back->pdev->dev,
  236. "Cannot locate client instance close routine\n");
  237. continue;
  238. }
  239. cdev->client->ops->close(&cdev->lan_info, cdev->client,
  240. reset);
  241. i40e_client_release_qvlist(&cdev->lan_info);
  242. }
  243. }
  244. mutex_unlock(&i40e_client_instance_mutex);
  245. }
  246. /**
  247. * i40e_notify_client_of_vf_reset - call the client vf reset callback
  248. * @pf: PF device pointer
  249. * @vf_id: asolute id of VF being reset
  250. *
  251. * If there is a client attached to this PF, notify when a VF is reset
  252. **/
  253. void i40e_notify_client_of_vf_reset(struct i40e_pf *pf, u32 vf_id)
  254. {
  255. struct i40e_client_instance *cdev;
  256. if (!pf)
  257. return;
  258. mutex_lock(&i40e_client_instance_mutex);
  259. list_for_each_entry(cdev, &i40e_client_instances, list) {
  260. if (cdev->lan_info.pf == pf) {
  261. if (!cdev->client ||
  262. !cdev->client->ops ||
  263. !cdev->client->ops->vf_reset) {
  264. dev_dbg(&pf->pdev->dev,
  265. "Cannot locate client instance VF reset routine\n");
  266. continue;
  267. }
  268. cdev->client->ops->vf_reset(&cdev->lan_info,
  269. cdev->client, vf_id);
  270. }
  271. }
  272. mutex_unlock(&i40e_client_instance_mutex);
  273. }
  274. /**
  275. * i40e_notify_client_of_vf_enable - call the client vf notification callback
  276. * @pf: PF device pointer
  277. * @num_vfs: the number of VFs currently enabled, 0 for disable
  278. *
  279. * If there is a client attached to this PF, call its VF notification routine
  280. **/
  281. void i40e_notify_client_of_vf_enable(struct i40e_pf *pf, u32 num_vfs)
  282. {
  283. struct i40e_client_instance *cdev;
  284. if (!pf)
  285. return;
  286. mutex_lock(&i40e_client_instance_mutex);
  287. list_for_each_entry(cdev, &i40e_client_instances, list) {
  288. if (cdev->lan_info.pf == pf) {
  289. if (!cdev->client ||
  290. !cdev->client->ops ||
  291. !cdev->client->ops->vf_enable) {
  292. dev_dbg(&pf->pdev->dev,
  293. "Cannot locate client instance VF enable routine\n");
  294. continue;
  295. }
  296. cdev->client->ops->vf_enable(&cdev->lan_info,
  297. cdev->client, num_vfs);
  298. }
  299. }
  300. mutex_unlock(&i40e_client_instance_mutex);
  301. }
  302. /**
  303. * i40e_vf_client_capable - ask the client if it likes the specified VF
  304. * @pf: PF device pointer
  305. * @vf_id: the VF in question
  306. *
  307. * If there is a client of the specified type attached to this PF, call
  308. * its vf_capable routine
  309. **/
  310. int i40e_vf_client_capable(struct i40e_pf *pf, u32 vf_id,
  311. enum i40e_client_type type)
  312. {
  313. struct i40e_client_instance *cdev;
  314. int capable = false;
  315. if (!pf)
  316. return false;
  317. mutex_lock(&i40e_client_instance_mutex);
  318. list_for_each_entry(cdev, &i40e_client_instances, list) {
  319. if (cdev->lan_info.pf == pf) {
  320. if (!cdev->client ||
  321. !cdev->client->ops ||
  322. !cdev->client->ops->vf_capable ||
  323. !(cdev->client->type == type)) {
  324. dev_dbg(&pf->pdev->dev,
  325. "Cannot locate client instance VF capability routine\n");
  326. continue;
  327. }
  328. capable = cdev->client->ops->vf_capable(&cdev->lan_info,
  329. cdev->client,
  330. vf_id);
  331. break;
  332. }
  333. }
  334. mutex_unlock(&i40e_client_instance_mutex);
  335. return capable;
  336. }
  337. /**
  338. * i40e_vsi_lookup - finds a matching VSI from the PF list starting at start_vsi
  339. * @pf: board private structure
  340. * @type: vsi type
  341. * @start_vsi: a VSI pointer from where to start the search
  342. *
  343. * Returns non NULL on success or NULL for failure
  344. **/
  345. struct i40e_vsi *i40e_vsi_lookup(struct i40e_pf *pf,
  346. enum i40e_vsi_type type,
  347. struct i40e_vsi *start_vsi)
  348. {
  349. struct i40e_vsi *vsi;
  350. int i = 0;
  351. if (start_vsi) {
  352. for (i = 0; i < pf->num_alloc_vsi; i++) {
  353. vsi = pf->vsi[i];
  354. if (vsi == start_vsi)
  355. break;
  356. }
  357. }
  358. for (; i < pf->num_alloc_vsi; i++) {
  359. vsi = pf->vsi[i];
  360. if (vsi && vsi->type == type)
  361. return vsi;
  362. }
  363. return NULL;
  364. }
  365. /**
  366. * i40e_client_add_instance - add a client instance struct to the instance list
  367. * @pf: pointer to the board struct
  368. * @client: pointer to a client struct in the client list.
  369. *
  370. * Returns cdev ptr on success, NULL on failure
  371. **/
  372. static
  373. struct i40e_client_instance *i40e_client_add_instance(struct i40e_pf *pf,
  374. struct i40e_client *client)
  375. {
  376. struct i40e_client_instance *cdev;
  377. struct netdev_hw_addr *mac = NULL;
  378. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  379. mutex_lock(&i40e_client_instance_mutex);
  380. list_for_each_entry(cdev, &i40e_client_instances, list) {
  381. if ((cdev->lan_info.pf == pf) && (cdev->client == client)) {
  382. cdev = NULL;
  383. goto out;
  384. }
  385. }
  386. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  387. if (!cdev)
  388. goto out;
  389. cdev->lan_info.pf = (void *)pf;
  390. cdev->lan_info.netdev = vsi->netdev;
  391. cdev->lan_info.pcidev = pf->pdev;
  392. cdev->lan_info.fid = pf->hw.pf_id;
  393. cdev->lan_info.ftype = I40E_CLIENT_FTYPE_PF;
  394. cdev->lan_info.hw_addr = pf->hw.hw_addr;
  395. cdev->lan_info.ops = &i40e_lan_ops;
  396. cdev->lan_info.version.major = I40E_CLIENT_VERSION_MAJOR;
  397. cdev->lan_info.version.minor = I40E_CLIENT_VERSION_MINOR;
  398. cdev->lan_info.version.build = I40E_CLIENT_VERSION_BUILD;
  399. cdev->lan_info.fw_maj_ver = pf->hw.aq.fw_maj_ver;
  400. cdev->lan_info.fw_min_ver = pf->hw.aq.fw_min_ver;
  401. cdev->lan_info.fw_build = pf->hw.aq.fw_build;
  402. set_bit(__I40E_CLIENT_INSTANCE_NONE, &cdev->state);
  403. if (i40e_client_get_params(vsi, &cdev->lan_info.params)) {
  404. kfree(cdev);
  405. cdev = NULL;
  406. goto out;
  407. }
  408. cdev->lan_info.msix_count = pf->num_iwarp_msix;
  409. cdev->lan_info.msix_entries = &pf->msix_entries[pf->iwarp_base_vector];
  410. mac = list_first_entry(&cdev->lan_info.netdev->dev_addrs.list,
  411. struct netdev_hw_addr, list);
  412. if (mac)
  413. ether_addr_copy(cdev->lan_info.lanmac, mac->addr);
  414. else
  415. dev_err(&pf->pdev->dev, "MAC address list is empty!\n");
  416. cdev->client = client;
  417. INIT_LIST_HEAD(&cdev->list);
  418. list_add(&cdev->list, &i40e_client_instances);
  419. out:
  420. mutex_unlock(&i40e_client_instance_mutex);
  421. return cdev;
  422. }
  423. /**
  424. * i40e_client_del_instance - removes a client instance from the list
  425. * @pf: pointer to the board struct
  426. *
  427. * Returns 0 on success or non-0 on error
  428. **/
  429. static
  430. int i40e_client_del_instance(struct i40e_pf *pf, struct i40e_client *client)
  431. {
  432. struct i40e_client_instance *cdev, *tmp;
  433. int ret = -ENODEV;
  434. mutex_lock(&i40e_client_instance_mutex);
  435. list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
  436. if ((cdev->lan_info.pf != pf) || (cdev->client != client))
  437. continue;
  438. dev_info(&pf->pdev->dev, "Deleted instance of Client %s, of dev %d bus=0x%02x func=0x%02x)\n",
  439. client->name, pf->hw.pf_id,
  440. pf->hw.bus.device, pf->hw.bus.func);
  441. list_del(&cdev->list);
  442. kfree(cdev);
  443. ret = 0;
  444. break;
  445. }
  446. mutex_unlock(&i40e_client_instance_mutex);
  447. return ret;
  448. }
  449. /**
  450. * i40e_client_subtask - client maintenance work
  451. * @pf: board private structure
  452. **/
  453. void i40e_client_subtask(struct i40e_pf *pf)
  454. {
  455. struct i40e_client_instance *cdev;
  456. struct i40e_client *client;
  457. int ret = 0;
  458. if (!(pf->flags & I40E_FLAG_SERVICE_CLIENT_REQUESTED))
  459. return;
  460. pf->flags &= ~I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  461. /* If we're down or resetting, just bail */
  462. if (test_bit(__I40E_DOWN, &pf->state) ||
  463. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  464. return;
  465. /* Check client state and instantiate client if client registered */
  466. mutex_lock(&i40e_client_mutex);
  467. list_for_each_entry(client, &i40e_clients, list) {
  468. /* first check client is registered */
  469. if (!test_bit(__I40E_CLIENT_REGISTERED, &client->state))
  470. continue;
  471. /* Do we also need the LAN VSI to be up, to create instance */
  472. if (!(client->flags & I40E_CLIENT_FLAGS_LAUNCH_ON_PROBE)) {
  473. /* check if L2 VSI is up, if not we are not ready */
  474. if (test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
  475. continue;
  476. }
  477. /* Add the client instance to the instance list */
  478. cdev = i40e_client_add_instance(pf, client);
  479. if (!cdev)
  480. continue;
  481. /* Also up the ref_cnt of no. of instances of this client */
  482. atomic_inc(&client->ref_cnt);
  483. dev_info(&pf->pdev->dev, "Added instance of Client %s to PF%d bus=0x%02x func=0x%02x\n",
  484. client->name, pf->hw.pf_id,
  485. pf->hw.bus.device, pf->hw.bus.func);
  486. /* Send an Open request to the client */
  487. atomic_inc(&cdev->ref_cnt);
  488. if (client->ops && client->ops->open)
  489. ret = client->ops->open(&cdev->lan_info, client);
  490. atomic_dec(&cdev->ref_cnt);
  491. if (!ret) {
  492. set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
  493. } else {
  494. /* remove client instance */
  495. i40e_client_del_instance(pf, client);
  496. atomic_dec(&client->ref_cnt);
  497. continue;
  498. }
  499. }
  500. mutex_unlock(&i40e_client_mutex);
  501. }
  502. /**
  503. * i40e_lan_add_device - add a lan device struct to the list of lan devices
  504. * @pf: pointer to the board struct
  505. *
  506. * Returns 0 on success or none 0 on error
  507. **/
  508. int i40e_lan_add_device(struct i40e_pf *pf)
  509. {
  510. struct i40e_device *ldev;
  511. int ret = 0;
  512. mutex_lock(&i40e_device_mutex);
  513. list_for_each_entry(ldev, &i40e_devices, list) {
  514. if (ldev->pf == pf) {
  515. ret = -EEXIST;
  516. goto out;
  517. }
  518. }
  519. ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
  520. if (!ldev) {
  521. ret = -ENOMEM;
  522. goto out;
  523. }
  524. ldev->pf = pf;
  525. INIT_LIST_HEAD(&ldev->list);
  526. list_add(&ldev->list, &i40e_devices);
  527. dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x func=0x%02x\n",
  528. pf->hw.pf_id, pf->hw.bus.device, pf->hw.bus.func);
  529. /* Since in some cases register may have happened before a device gets
  530. * added, we can schedule a subtask to go initiate the clients.
  531. */
  532. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  533. i40e_service_event_schedule(pf);
  534. out:
  535. mutex_unlock(&i40e_device_mutex);
  536. return ret;
  537. }
  538. /**
  539. * i40e_lan_del_device - removes a lan device from the device list
  540. * @pf: pointer to the board struct
  541. *
  542. * Returns 0 on success or non-0 on error
  543. **/
  544. int i40e_lan_del_device(struct i40e_pf *pf)
  545. {
  546. struct i40e_device *ldev, *tmp;
  547. int ret = -ENODEV;
  548. mutex_lock(&i40e_device_mutex);
  549. list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
  550. if (ldev->pf == pf) {
  551. dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x func=0x%02x\n",
  552. pf->hw.pf_id, pf->hw.bus.device,
  553. pf->hw.bus.func);
  554. list_del(&ldev->list);
  555. kfree(ldev);
  556. ret = 0;
  557. break;
  558. }
  559. }
  560. mutex_unlock(&i40e_device_mutex);
  561. return ret;
  562. }
  563. /**
  564. * i40e_client_release - release client specific resources
  565. * @client: pointer to the registered client
  566. *
  567. * Return 0 on success or < 0 on error
  568. **/
  569. static int i40e_client_release(struct i40e_client *client)
  570. {
  571. struct i40e_client_instance *cdev, *tmp;
  572. struct i40e_pf *pf = NULL;
  573. int ret = 0;
  574. LIST_HEAD(cdevs_tmp);
  575. mutex_lock(&i40e_client_instance_mutex);
  576. list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
  577. if (strncmp(cdev->client->name, client->name,
  578. I40E_CLIENT_STR_LENGTH))
  579. continue;
  580. if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
  581. if (atomic_read(&cdev->ref_cnt) > 0) {
  582. ret = I40E_ERR_NOT_READY;
  583. goto out;
  584. }
  585. pf = (struct i40e_pf *)cdev->lan_info.pf;
  586. if (client->ops && client->ops->close)
  587. client->ops->close(&cdev->lan_info, client,
  588. false);
  589. i40e_client_release_qvlist(&cdev->lan_info);
  590. clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
  591. dev_warn(&pf->pdev->dev,
  592. "Client %s instance for PF id %d closed\n",
  593. client->name, pf->hw.pf_id);
  594. }
  595. /* delete the client instance from the list */
  596. list_del(&cdev->list);
  597. list_add(&cdev->list, &cdevs_tmp);
  598. atomic_dec(&client->ref_cnt);
  599. dev_info(&pf->pdev->dev, "Deleted client instance of Client %s\n",
  600. client->name);
  601. }
  602. out:
  603. mutex_unlock(&i40e_client_instance_mutex);
  604. /* free the client device and release its vsi */
  605. list_for_each_entry_safe(cdev, tmp, &cdevs_tmp, list) {
  606. kfree(cdev);
  607. }
  608. return ret;
  609. }
  610. /**
  611. * i40e_client_prepare - prepare client specific resources
  612. * @client: pointer to the registered client
  613. *
  614. * Return 0 on success or < 0 on error
  615. **/
  616. static int i40e_client_prepare(struct i40e_client *client)
  617. {
  618. struct i40e_device *ldev;
  619. struct i40e_pf *pf;
  620. int ret = 0;
  621. mutex_lock(&i40e_device_mutex);
  622. list_for_each_entry(ldev, &i40e_devices, list) {
  623. pf = ldev->pf;
  624. /* Start the client subtask */
  625. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  626. i40e_service_event_schedule(pf);
  627. }
  628. mutex_unlock(&i40e_device_mutex);
  629. return ret;
  630. }
  631. /**
  632. * i40e_client_virtchnl_send - TBD
  633. * @ldev: pointer to L2 context
  634. * @client: Client pointer
  635. * @vf_id: absolute VF identifier
  636. * @msg: message buffer
  637. * @len: length of message buffer
  638. *
  639. * Return 0 on success or < 0 on error
  640. **/
  641. static int i40e_client_virtchnl_send(struct i40e_info *ldev,
  642. struct i40e_client *client,
  643. u32 vf_id, u8 *msg, u16 len)
  644. {
  645. struct i40e_pf *pf = ldev->pf;
  646. struct i40e_hw *hw = &pf->hw;
  647. i40e_status err;
  648. err = i40e_aq_send_msg_to_vf(hw, vf_id, I40E_VIRTCHNL_OP_IWARP,
  649. 0, msg, len, NULL);
  650. if (err)
  651. dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
  652. err, hw->aq.asq_last_status);
  653. return err;
  654. }
  655. /**
  656. * i40e_client_setup_qvlist
  657. * @ldev: pointer to L2 context.
  658. * @client: Client pointer.
  659. * @qv_info: queue and vector list
  660. *
  661. * Return 0 on success or < 0 on error
  662. **/
  663. static int i40e_client_setup_qvlist(struct i40e_info *ldev,
  664. struct i40e_client *client,
  665. struct i40e_qvlist_info *qvlist_info)
  666. {
  667. struct i40e_pf *pf = ldev->pf;
  668. struct i40e_hw *hw = &pf->hw;
  669. struct i40e_qv_info *qv_info;
  670. u32 v_idx, i, reg_idx, reg;
  671. u32 size;
  672. size = sizeof(struct i40e_qvlist_info) +
  673. (sizeof(struct i40e_qv_info) * (qvlist_info->num_vectors - 1));
  674. ldev->qvlist_info = kzalloc(size, GFP_KERNEL);
  675. ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
  676. for (i = 0; i < qvlist_info->num_vectors; i++) {
  677. qv_info = &qvlist_info->qv_info[i];
  678. if (!qv_info)
  679. continue;
  680. v_idx = qv_info->v_idx;
  681. /* Validate vector id belongs to this client */
  682. if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
  683. (v_idx < pf->iwarp_base_vector))
  684. goto err;
  685. ldev->qvlist_info->qv_info[i] = *qv_info;
  686. reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
  687. if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
  688. /* Special case - No CEQ mapped on this vector */
  689. wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
  690. } else {
  691. reg = (qv_info->ceq_idx &
  692. I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  693. (I40E_QUEUE_TYPE_PE_CEQ <<
  694. I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  695. wr32(hw, reg_idx, reg);
  696. reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
  697. (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
  698. (qv_info->itr_idx <<
  699. I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
  700. (I40E_QUEUE_END_OF_LIST <<
  701. I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
  702. wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
  703. }
  704. if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
  705. reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
  706. (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
  707. (qv_info->itr_idx <<
  708. I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
  709. wr32(hw, I40E_PFINT_AEQCTL, reg);
  710. }
  711. }
  712. return 0;
  713. err:
  714. kfree(ldev->qvlist_info);
  715. ldev->qvlist_info = NULL;
  716. return -EINVAL;
  717. }
  718. /**
  719. * i40e_client_request_reset
  720. * @ldev: pointer to L2 context.
  721. * @client: Client pointer.
  722. * @level: reset level
  723. **/
  724. static void i40e_client_request_reset(struct i40e_info *ldev,
  725. struct i40e_client *client,
  726. u32 reset_level)
  727. {
  728. struct i40e_pf *pf = ldev->pf;
  729. switch (reset_level) {
  730. case I40E_CLIENT_RESET_LEVEL_PF:
  731. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  732. break;
  733. case I40E_CLIENT_RESET_LEVEL_CORE:
  734. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  735. break;
  736. default:
  737. dev_warn(&pf->pdev->dev,
  738. "Client %s instance for PF id %d request an unsupported reset: %d.\n",
  739. client->name, pf->hw.pf_id, reset_level);
  740. break;
  741. }
  742. i40e_service_event_schedule(pf);
  743. }
  744. /**
  745. * i40e_client_update_vsi_ctxt
  746. * @ldev: pointer to L2 context.
  747. * @client: Client pointer.
  748. * @is_vf: if this for the VF
  749. * @vf_id: if is_vf true this carries the vf_id
  750. * @flag: Any device level setting that needs to be done for PE
  751. * @valid_flag: Bits in this match up and enable changing of flag bits
  752. *
  753. * Return 0 on success or < 0 on error
  754. **/
  755. static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
  756. struct i40e_client *client,
  757. bool is_vf, u32 vf_id,
  758. u32 flag, u32 valid_flag)
  759. {
  760. struct i40e_pf *pf = ldev->pf;
  761. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  762. struct i40e_vsi_context ctxt;
  763. bool update = true;
  764. i40e_status err;
  765. /* TODO: for now do not allow setting VF's VSI setting */
  766. if (is_vf)
  767. return -EINVAL;
  768. ctxt.seid = pf->main_vsi_seid;
  769. ctxt.pf_num = pf->hw.pf_id;
  770. err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  771. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  772. if (err) {
  773. dev_info(&pf->pdev->dev,
  774. "couldn't get PF vsi config, err %s aq_err %s\n",
  775. i40e_stat_str(&pf->hw, err),
  776. i40e_aq_str(&pf->hw,
  777. pf->hw.aq.asq_last_status));
  778. return -ENOENT;
  779. }
  780. if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
  781. (flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
  782. ctxt.info.valid_sections =
  783. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  784. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  785. } else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
  786. !(flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
  787. ctxt.info.valid_sections =
  788. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  789. ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  790. } else {
  791. update = false;
  792. dev_warn(&pf->pdev->dev,
  793. "Client %s instance for PF id %d request an unsupported Config: %x.\n",
  794. client->name, pf->hw.pf_id, flag);
  795. }
  796. if (update) {
  797. err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  798. if (err) {
  799. dev_info(&pf->pdev->dev,
  800. "update VSI ctxt for PE failed, err %s aq_err %s\n",
  801. i40e_stat_str(&pf->hw, err),
  802. i40e_aq_str(&pf->hw,
  803. pf->hw.aq.asq_last_status));
  804. }
  805. }
  806. return err;
  807. }
  808. /**
  809. * i40e_register_client - Register a i40e client driver with the L2 driver
  810. * @client: pointer to the i40e_client struct
  811. *
  812. * Returns 0 on success or non-0 on error
  813. **/
  814. int i40e_register_client(struct i40e_client *client)
  815. {
  816. int ret = 0;
  817. enum i40e_vsi_type vsi_type;
  818. if (!client) {
  819. ret = -EIO;
  820. goto out;
  821. }
  822. if (strlen(client->name) == 0) {
  823. pr_info("i40e: Failed to register client with no name\n");
  824. ret = -EIO;
  825. goto out;
  826. }
  827. mutex_lock(&i40e_client_mutex);
  828. if (i40e_client_is_registered(client)) {
  829. pr_info("i40e: Client %s has already been registered!\n",
  830. client->name);
  831. mutex_unlock(&i40e_client_mutex);
  832. ret = -EEXIST;
  833. goto out;
  834. }
  835. if ((client->version.major != I40E_CLIENT_VERSION_MAJOR) ||
  836. (client->version.minor != I40E_CLIENT_VERSION_MINOR)) {
  837. pr_info("i40e: Failed to register client %s due to mismatched client interface version\n",
  838. client->name);
  839. pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
  840. client->version.major, client->version.minor,
  841. client->version.build,
  842. i40e_client_interface_version_str);
  843. mutex_unlock(&i40e_client_mutex);
  844. ret = -EIO;
  845. goto out;
  846. }
  847. vsi_type = i40e_client_type_to_vsi_type(client->type);
  848. if (vsi_type == I40E_VSI_TYPE_UNKNOWN) {
  849. pr_info("i40e: Failed to register client %s due to unknown client type %d\n",
  850. client->name, client->type);
  851. mutex_unlock(&i40e_client_mutex);
  852. ret = -EIO;
  853. goto out;
  854. }
  855. list_add(&client->list, &i40e_clients);
  856. set_bit(__I40E_CLIENT_REGISTERED, &client->state);
  857. mutex_unlock(&i40e_client_mutex);
  858. if (i40e_client_prepare(client)) {
  859. ret = -EIO;
  860. goto out;
  861. }
  862. pr_info("i40e: Registered client %s with return code %d\n",
  863. client->name, ret);
  864. out:
  865. return ret;
  866. }
  867. EXPORT_SYMBOL(i40e_register_client);
  868. /**
  869. * i40e_unregister_client - Unregister a i40e client driver with the L2 driver
  870. * @client: pointer to the i40e_client struct
  871. *
  872. * Returns 0 on success or non-0 on error
  873. **/
  874. int i40e_unregister_client(struct i40e_client *client)
  875. {
  876. int ret = 0;
  877. /* When a unregister request comes through we would have to send
  878. * a close for each of the client instances that were opened.
  879. * client_release function is called to handle this.
  880. */
  881. if (!client || i40e_client_release(client)) {
  882. ret = -EIO;
  883. goto out;
  884. }
  885. /* TODO: check if device is in reset, or if that matters? */
  886. mutex_lock(&i40e_client_mutex);
  887. if (!i40e_client_is_registered(client)) {
  888. pr_info("i40e: Client %s has not been registered\n",
  889. client->name);
  890. mutex_unlock(&i40e_client_mutex);
  891. ret = -ENODEV;
  892. goto out;
  893. }
  894. if (atomic_read(&client->ref_cnt) == 0) {
  895. clear_bit(__I40E_CLIENT_REGISTERED, &client->state);
  896. list_del(&client->list);
  897. pr_info("i40e: Unregistered client %s with return code %d\n",
  898. client->name, ret);
  899. } else {
  900. ret = I40E_ERR_NOT_READY;
  901. pr_err("i40e: Client %s failed unregister - client has open instances\n",
  902. client->name);
  903. }
  904. mutex_unlock(&i40e_client_mutex);
  905. out:
  906. return ret;
  907. }
  908. EXPORT_SYMBOL(i40e_unregister_client);