i40e_client.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
  131. &cdev->state)) {
  132. dev_dbg(&vsi->back->pdev->dev, "Client is not open, abort virtchnl_receive\n");
  133. continue;
  134. }
  135. cdev->client->ops->virtchnl_receive(&cdev->lan_info,
  136. cdev->client,
  137. vf_id, msg, len);
  138. }
  139. }
  140. mutex_unlock(&i40e_client_instance_mutex);
  141. }
  142. /**
  143. * i40e_notify_client_of_l2_param_changes - call the client notify callback
  144. * @vsi: the VSI with l2 param changes
  145. *
  146. * If there is a client to this VSI, call the client
  147. **/
  148. void i40e_notify_client_of_l2_param_changes(struct i40e_vsi *vsi)
  149. {
  150. struct i40e_client_instance *cdev;
  151. struct i40e_params params;
  152. if (!vsi)
  153. return;
  154. memset(&params, 0, sizeof(params));
  155. i40e_client_get_params(vsi, &params);
  156. mutex_lock(&i40e_client_instance_mutex);
  157. list_for_each_entry(cdev, &i40e_client_instances, list) {
  158. if (cdev->lan_info.pf == vsi->back) {
  159. if (!cdev->client ||
  160. !cdev->client->ops ||
  161. !cdev->client->ops->l2_param_change) {
  162. dev_dbg(&vsi->back->pdev->dev,
  163. "Cannot locate client instance l2_param_change routine\n");
  164. continue;
  165. }
  166. if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
  167. &cdev->state)) {
  168. dev_dbg(&vsi->back->pdev->dev, "Client is not open, abort l2 param change\n");
  169. continue;
  170. }
  171. cdev->lan_info.params = params;
  172. cdev->client->ops->l2_param_change(&cdev->lan_info,
  173. cdev->client,
  174. &params);
  175. }
  176. }
  177. mutex_unlock(&i40e_client_instance_mutex);
  178. }
  179. /**
  180. * i40e_notify_client_of_netdev_open - call the client open callback
  181. * @vsi: the VSI with netdev opened
  182. *
  183. * If there is a client to this netdev, call the client with open
  184. **/
  185. void i40e_notify_client_of_netdev_open(struct i40e_vsi *vsi)
  186. {
  187. struct i40e_client_instance *cdev;
  188. int ret = 0;
  189. if (!vsi)
  190. return;
  191. mutex_lock(&i40e_client_instance_mutex);
  192. list_for_each_entry(cdev, &i40e_client_instances, list) {
  193. if (cdev->lan_info.netdev == vsi->netdev) {
  194. if (!cdev->client ||
  195. !cdev->client->ops || !cdev->client->ops->open) {
  196. dev_dbg(&vsi->back->pdev->dev,
  197. "Cannot locate client instance open routine\n");
  198. continue;
  199. }
  200. if (!(test_bit(__I40E_CLIENT_INSTANCE_OPENED,
  201. &cdev->state))) {
  202. ret = cdev->client->ops->open(&cdev->lan_info,
  203. cdev->client);
  204. if (!ret)
  205. set_bit(__I40E_CLIENT_INSTANCE_OPENED,
  206. &cdev->state);
  207. }
  208. }
  209. }
  210. mutex_unlock(&i40e_client_instance_mutex);
  211. }
  212. /**
  213. * i40e_client_release_qvlist
  214. * @ldev: pointer to L2 context.
  215. *
  216. **/
  217. static void i40e_client_release_qvlist(struct i40e_info *ldev)
  218. {
  219. struct i40e_qvlist_info *qvlist_info = ldev->qvlist_info;
  220. u32 i;
  221. if (!ldev->qvlist_info)
  222. return;
  223. for (i = 0; i < qvlist_info->num_vectors; i++) {
  224. struct i40e_pf *pf = ldev->pf;
  225. struct i40e_qv_info *qv_info;
  226. u32 reg_idx;
  227. qv_info = &qvlist_info->qv_info[i];
  228. if (!qv_info)
  229. continue;
  230. reg_idx = I40E_PFINT_LNKLSTN(qv_info->v_idx - 1);
  231. wr32(&pf->hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
  232. }
  233. kfree(ldev->qvlist_info);
  234. ldev->qvlist_info = NULL;
  235. }
  236. /**
  237. * i40e_notify_client_of_netdev_close - call the client close callback
  238. * @vsi: the VSI with netdev closed
  239. * @reset: true when close called due to a reset pending
  240. *
  241. * If there is a client to this netdev, call the client with close
  242. **/
  243. void i40e_notify_client_of_netdev_close(struct i40e_vsi *vsi, bool reset)
  244. {
  245. struct i40e_client_instance *cdev;
  246. if (!vsi)
  247. return;
  248. mutex_lock(&i40e_client_instance_mutex);
  249. list_for_each_entry(cdev, &i40e_client_instances, list) {
  250. if (cdev->lan_info.netdev == vsi->netdev) {
  251. if (!cdev->client ||
  252. !cdev->client->ops || !cdev->client->ops->close) {
  253. dev_dbg(&vsi->back->pdev->dev,
  254. "Cannot locate client instance close routine\n");
  255. continue;
  256. }
  257. cdev->client->ops->close(&cdev->lan_info, cdev->client,
  258. reset);
  259. i40e_client_release_qvlist(&cdev->lan_info);
  260. }
  261. }
  262. mutex_unlock(&i40e_client_instance_mutex);
  263. }
  264. /**
  265. * i40e_notify_client_of_vf_reset - call the client vf reset callback
  266. * @pf: PF device pointer
  267. * @vf_id: asolute id of VF being reset
  268. *
  269. * If there is a client attached to this PF, notify when a VF is reset
  270. **/
  271. void i40e_notify_client_of_vf_reset(struct i40e_pf *pf, u32 vf_id)
  272. {
  273. struct i40e_client_instance *cdev;
  274. if (!pf)
  275. return;
  276. mutex_lock(&i40e_client_instance_mutex);
  277. list_for_each_entry(cdev, &i40e_client_instances, list) {
  278. if (cdev->lan_info.pf == pf) {
  279. if (!cdev->client ||
  280. !cdev->client->ops ||
  281. !cdev->client->ops->vf_reset) {
  282. dev_dbg(&pf->pdev->dev,
  283. "Cannot locate client instance VF reset routine\n");
  284. continue;
  285. }
  286. if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
  287. &cdev->state)) {
  288. dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-reset\n");
  289. continue;
  290. }
  291. cdev->client->ops->vf_reset(&cdev->lan_info,
  292. cdev->client, vf_id);
  293. }
  294. }
  295. mutex_unlock(&i40e_client_instance_mutex);
  296. }
  297. /**
  298. * i40e_notify_client_of_vf_enable - call the client vf notification callback
  299. * @pf: PF device pointer
  300. * @num_vfs: the number of VFs currently enabled, 0 for disable
  301. *
  302. * If there is a client attached to this PF, call its VF notification routine
  303. **/
  304. void i40e_notify_client_of_vf_enable(struct i40e_pf *pf, u32 num_vfs)
  305. {
  306. struct i40e_client_instance *cdev;
  307. if (!pf)
  308. return;
  309. mutex_lock(&i40e_client_instance_mutex);
  310. list_for_each_entry(cdev, &i40e_client_instances, list) {
  311. if (cdev->lan_info.pf == pf) {
  312. if (!cdev->client ||
  313. !cdev->client->ops ||
  314. !cdev->client->ops->vf_enable) {
  315. dev_dbg(&pf->pdev->dev,
  316. "Cannot locate client instance VF enable routine\n");
  317. continue;
  318. }
  319. if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
  320. &cdev->state)) {
  321. dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-enable\n");
  322. continue;
  323. }
  324. cdev->client->ops->vf_enable(&cdev->lan_info,
  325. cdev->client, num_vfs);
  326. }
  327. }
  328. mutex_unlock(&i40e_client_instance_mutex);
  329. }
  330. /**
  331. * i40e_vf_client_capable - ask the client if it likes the specified VF
  332. * @pf: PF device pointer
  333. * @vf_id: the VF in question
  334. *
  335. * If there is a client of the specified type attached to this PF, call
  336. * its vf_capable routine
  337. **/
  338. int i40e_vf_client_capable(struct i40e_pf *pf, u32 vf_id,
  339. enum i40e_client_type type)
  340. {
  341. struct i40e_client_instance *cdev;
  342. int capable = false;
  343. if (!pf)
  344. return false;
  345. mutex_lock(&i40e_client_instance_mutex);
  346. list_for_each_entry(cdev, &i40e_client_instances, list) {
  347. if (cdev->lan_info.pf == pf) {
  348. if (!cdev->client ||
  349. !cdev->client->ops ||
  350. !cdev->client->ops->vf_capable ||
  351. !(cdev->client->type == type)) {
  352. dev_dbg(&pf->pdev->dev,
  353. "Cannot locate client instance VF capability routine\n");
  354. continue;
  355. }
  356. if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
  357. &cdev->state)) {
  358. dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-capable\n");
  359. continue;
  360. }
  361. capable = cdev->client->ops->vf_capable(&cdev->lan_info,
  362. cdev->client,
  363. vf_id);
  364. break;
  365. }
  366. }
  367. mutex_unlock(&i40e_client_instance_mutex);
  368. return capable;
  369. }
  370. /**
  371. * i40e_vsi_lookup - finds a matching VSI from the PF list starting at start_vsi
  372. * @pf: board private structure
  373. * @type: vsi type
  374. * @start_vsi: a VSI pointer from where to start the search
  375. *
  376. * Returns non NULL on success or NULL for failure
  377. **/
  378. struct i40e_vsi *i40e_vsi_lookup(struct i40e_pf *pf,
  379. enum i40e_vsi_type type,
  380. struct i40e_vsi *start_vsi)
  381. {
  382. struct i40e_vsi *vsi;
  383. int i = 0;
  384. if (start_vsi) {
  385. for (i = 0; i < pf->num_alloc_vsi; i++) {
  386. vsi = pf->vsi[i];
  387. if (vsi == start_vsi)
  388. break;
  389. }
  390. }
  391. for (; i < pf->num_alloc_vsi; i++) {
  392. vsi = pf->vsi[i];
  393. if (vsi && vsi->type == type)
  394. return vsi;
  395. }
  396. return NULL;
  397. }
  398. /**
  399. * i40e_client_add_instance - add a client instance struct to the instance list
  400. * @pf: pointer to the board struct
  401. * @client: pointer to a client struct in the client list.
  402. * @existing: if there was already an existing instance
  403. *
  404. * Returns cdev ptr on success or if already exists, NULL on failure
  405. **/
  406. static
  407. struct i40e_client_instance *i40e_client_add_instance(struct i40e_pf *pf,
  408. struct i40e_client *client,
  409. bool *existing)
  410. {
  411. struct i40e_client_instance *cdev;
  412. struct netdev_hw_addr *mac = NULL;
  413. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  414. mutex_lock(&i40e_client_instance_mutex);
  415. list_for_each_entry(cdev, &i40e_client_instances, list) {
  416. if ((cdev->lan_info.pf == pf) && (cdev->client == client)) {
  417. *existing = true;
  418. goto out;
  419. }
  420. }
  421. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  422. if (!cdev)
  423. goto out;
  424. cdev->lan_info.pf = (void *)pf;
  425. cdev->lan_info.netdev = vsi->netdev;
  426. cdev->lan_info.pcidev = pf->pdev;
  427. cdev->lan_info.fid = pf->hw.pf_id;
  428. cdev->lan_info.ftype = I40E_CLIENT_FTYPE_PF;
  429. cdev->lan_info.hw_addr = pf->hw.hw_addr;
  430. cdev->lan_info.ops = &i40e_lan_ops;
  431. cdev->lan_info.version.major = I40E_CLIENT_VERSION_MAJOR;
  432. cdev->lan_info.version.minor = I40E_CLIENT_VERSION_MINOR;
  433. cdev->lan_info.version.build = I40E_CLIENT_VERSION_BUILD;
  434. cdev->lan_info.fw_maj_ver = pf->hw.aq.fw_maj_ver;
  435. cdev->lan_info.fw_min_ver = pf->hw.aq.fw_min_ver;
  436. cdev->lan_info.fw_build = pf->hw.aq.fw_build;
  437. set_bit(__I40E_CLIENT_INSTANCE_NONE, &cdev->state);
  438. if (i40e_client_get_params(vsi, &cdev->lan_info.params)) {
  439. kfree(cdev);
  440. cdev = NULL;
  441. goto out;
  442. }
  443. cdev->lan_info.msix_count = pf->num_iwarp_msix;
  444. cdev->lan_info.msix_entries = &pf->msix_entries[pf->iwarp_base_vector];
  445. mac = list_first_entry(&cdev->lan_info.netdev->dev_addrs.list,
  446. struct netdev_hw_addr, list);
  447. if (mac)
  448. ether_addr_copy(cdev->lan_info.lanmac, mac->addr);
  449. else
  450. dev_err(&pf->pdev->dev, "MAC address list is empty!\n");
  451. cdev->client = client;
  452. INIT_LIST_HEAD(&cdev->list);
  453. list_add(&cdev->list, &i40e_client_instances);
  454. out:
  455. mutex_unlock(&i40e_client_instance_mutex);
  456. return cdev;
  457. }
  458. /**
  459. * i40e_client_del_instance - removes a client instance from the list
  460. * @pf: pointer to the board struct
  461. *
  462. * Returns 0 on success or non-0 on error
  463. **/
  464. static
  465. int i40e_client_del_instance(struct i40e_pf *pf, struct i40e_client *client)
  466. {
  467. struct i40e_client_instance *cdev, *tmp;
  468. int ret = -ENODEV;
  469. mutex_lock(&i40e_client_instance_mutex);
  470. list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
  471. if ((cdev->lan_info.pf != pf) || (cdev->client != client))
  472. continue;
  473. dev_info(&pf->pdev->dev, "Deleted instance of Client %s, of dev %d bus=0x%02x func=0x%02x)\n",
  474. client->name, pf->hw.pf_id,
  475. pf->hw.bus.device, pf->hw.bus.func);
  476. list_del(&cdev->list);
  477. kfree(cdev);
  478. ret = 0;
  479. break;
  480. }
  481. mutex_unlock(&i40e_client_instance_mutex);
  482. return ret;
  483. }
  484. /**
  485. * i40e_client_subtask - client maintenance work
  486. * @pf: board private structure
  487. **/
  488. void i40e_client_subtask(struct i40e_pf *pf)
  489. {
  490. struct i40e_client_instance *cdev;
  491. struct i40e_client *client;
  492. bool existing = false;
  493. int ret = 0;
  494. if (!(pf->flags & I40E_FLAG_SERVICE_CLIENT_REQUESTED))
  495. return;
  496. pf->flags &= ~I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  497. /* If we're down or resetting, just bail */
  498. if (test_bit(__I40E_DOWN, &pf->state) ||
  499. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  500. return;
  501. /* Check client state and instantiate client if client registered */
  502. mutex_lock(&i40e_client_mutex);
  503. list_for_each_entry(client, &i40e_clients, list) {
  504. /* first check client is registered */
  505. if (!test_bit(__I40E_CLIENT_REGISTERED, &client->state))
  506. continue;
  507. /* Do we also need the LAN VSI to be up, to create instance */
  508. if (!(client->flags & I40E_CLIENT_FLAGS_LAUNCH_ON_PROBE)) {
  509. /* check if L2 VSI is up, if not we are not ready */
  510. if (test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
  511. continue;
  512. } else {
  513. dev_warn(&pf->pdev->dev, "This client %s is being instanciated at probe\n",
  514. client->name);
  515. }
  516. /* Add the client instance to the instance list */
  517. cdev = i40e_client_add_instance(pf, client, &existing);
  518. if (!cdev)
  519. continue;
  520. if (!existing) {
  521. /* Also up the ref_cnt for no. of instances of this
  522. * client.
  523. */
  524. atomic_inc(&client->ref_cnt);
  525. dev_info(&pf->pdev->dev, "Added instance of Client %s to PF%d bus=0x%02x func=0x%02x\n",
  526. client->name, pf->hw.pf_id,
  527. pf->hw.bus.device, pf->hw.bus.func);
  528. }
  529. mutex_lock(&i40e_client_instance_mutex);
  530. /* Send an Open request to the client */
  531. atomic_inc(&cdev->ref_cnt);
  532. if (client->ops && client->ops->open)
  533. ret = client->ops->open(&cdev->lan_info, client);
  534. atomic_dec(&cdev->ref_cnt);
  535. if (!ret) {
  536. set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
  537. } else {
  538. /* remove client instance */
  539. mutex_unlock(&i40e_client_instance_mutex);
  540. i40e_client_del_instance(pf, client);
  541. atomic_dec(&client->ref_cnt);
  542. continue;
  543. }
  544. mutex_unlock(&i40e_client_instance_mutex);
  545. }
  546. mutex_unlock(&i40e_client_mutex);
  547. }
  548. /**
  549. * i40e_lan_add_device - add a lan device struct to the list of lan devices
  550. * @pf: pointer to the board struct
  551. *
  552. * Returns 0 on success or none 0 on error
  553. **/
  554. int i40e_lan_add_device(struct i40e_pf *pf)
  555. {
  556. struct i40e_device *ldev;
  557. int ret = 0;
  558. mutex_lock(&i40e_device_mutex);
  559. list_for_each_entry(ldev, &i40e_devices, list) {
  560. if (ldev->pf == pf) {
  561. ret = -EEXIST;
  562. goto out;
  563. }
  564. }
  565. ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
  566. if (!ldev) {
  567. ret = -ENOMEM;
  568. goto out;
  569. }
  570. ldev->pf = pf;
  571. INIT_LIST_HEAD(&ldev->list);
  572. list_add(&ldev->list, &i40e_devices);
  573. dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x func=0x%02x\n",
  574. pf->hw.pf_id, pf->hw.bus.device, pf->hw.bus.func);
  575. /* Since in some cases register may have happened before a device gets
  576. * added, we can schedule a subtask to go initiate the clients if
  577. * they can be launched at probe time.
  578. */
  579. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  580. i40e_service_event_schedule(pf);
  581. out:
  582. mutex_unlock(&i40e_device_mutex);
  583. return ret;
  584. }
  585. /**
  586. * i40e_lan_del_device - removes a lan device from the device list
  587. * @pf: pointer to the board struct
  588. *
  589. * Returns 0 on success or non-0 on error
  590. **/
  591. int i40e_lan_del_device(struct i40e_pf *pf)
  592. {
  593. struct i40e_device *ldev, *tmp;
  594. int ret = -ENODEV;
  595. mutex_lock(&i40e_device_mutex);
  596. list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
  597. if (ldev->pf == pf) {
  598. dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x func=0x%02x\n",
  599. pf->hw.pf_id, pf->hw.bus.device,
  600. pf->hw.bus.func);
  601. list_del(&ldev->list);
  602. kfree(ldev);
  603. ret = 0;
  604. break;
  605. }
  606. }
  607. mutex_unlock(&i40e_device_mutex);
  608. return ret;
  609. }
  610. /**
  611. * i40e_client_release - release 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_release(struct i40e_client *client)
  617. {
  618. struct i40e_client_instance *cdev, *tmp;
  619. struct i40e_pf *pf;
  620. int ret = 0;
  621. LIST_HEAD(cdevs_tmp);
  622. mutex_lock(&i40e_client_instance_mutex);
  623. list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
  624. if (strncmp(cdev->client->name, client->name,
  625. I40E_CLIENT_STR_LENGTH))
  626. continue;
  627. pf = (struct i40e_pf *)cdev->lan_info.pf;
  628. if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
  629. if (atomic_read(&cdev->ref_cnt) > 0) {
  630. ret = I40E_ERR_NOT_READY;
  631. goto out;
  632. }
  633. if (client->ops && client->ops->close)
  634. client->ops->close(&cdev->lan_info, client,
  635. false);
  636. i40e_client_release_qvlist(&cdev->lan_info);
  637. clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
  638. dev_warn(&pf->pdev->dev,
  639. "Client %s instance for PF id %d closed\n",
  640. client->name, pf->hw.pf_id);
  641. }
  642. /* delete the client instance from the list */
  643. list_move(&cdev->list, &cdevs_tmp);
  644. atomic_dec(&client->ref_cnt);
  645. dev_info(&pf->pdev->dev, "Deleted client instance of Client %s\n",
  646. client->name);
  647. }
  648. out:
  649. mutex_unlock(&i40e_client_instance_mutex);
  650. /* free the client device and release its vsi */
  651. list_for_each_entry_safe(cdev, tmp, &cdevs_tmp, list) {
  652. kfree(cdev);
  653. }
  654. return ret;
  655. }
  656. /**
  657. * i40e_client_prepare - prepare client specific resources
  658. * @client: pointer to the registered client
  659. *
  660. * Return 0 on success or < 0 on error
  661. **/
  662. static int i40e_client_prepare(struct i40e_client *client)
  663. {
  664. struct i40e_device *ldev;
  665. struct i40e_pf *pf;
  666. int ret = 0;
  667. mutex_lock(&i40e_device_mutex);
  668. list_for_each_entry(ldev, &i40e_devices, list) {
  669. pf = ldev->pf;
  670. /* Start the client subtask */
  671. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  672. i40e_service_event_schedule(pf);
  673. }
  674. mutex_unlock(&i40e_device_mutex);
  675. return ret;
  676. }
  677. /**
  678. * i40e_client_virtchnl_send - TBD
  679. * @ldev: pointer to L2 context
  680. * @client: Client pointer
  681. * @vf_id: absolute VF identifier
  682. * @msg: message buffer
  683. * @len: length of message buffer
  684. *
  685. * Return 0 on success or < 0 on error
  686. **/
  687. static int i40e_client_virtchnl_send(struct i40e_info *ldev,
  688. struct i40e_client *client,
  689. u32 vf_id, u8 *msg, u16 len)
  690. {
  691. struct i40e_pf *pf = ldev->pf;
  692. struct i40e_hw *hw = &pf->hw;
  693. i40e_status err;
  694. err = i40e_aq_send_msg_to_vf(hw, vf_id, I40E_VIRTCHNL_OP_IWARP,
  695. 0, msg, len, NULL);
  696. if (err)
  697. dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
  698. err, hw->aq.asq_last_status);
  699. return err;
  700. }
  701. /**
  702. * i40e_client_setup_qvlist
  703. * @ldev: pointer to L2 context.
  704. * @client: Client pointer.
  705. * @qv_info: queue and vector list
  706. *
  707. * Return 0 on success or < 0 on error
  708. **/
  709. static int i40e_client_setup_qvlist(struct i40e_info *ldev,
  710. struct i40e_client *client,
  711. struct i40e_qvlist_info *qvlist_info)
  712. {
  713. struct i40e_pf *pf = ldev->pf;
  714. struct i40e_hw *hw = &pf->hw;
  715. struct i40e_qv_info *qv_info;
  716. u32 v_idx, i, reg_idx, reg;
  717. u32 size;
  718. size = sizeof(struct i40e_qvlist_info) +
  719. (sizeof(struct i40e_qv_info) * (qvlist_info->num_vectors - 1));
  720. ldev->qvlist_info = kzalloc(size, GFP_KERNEL);
  721. ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
  722. for (i = 0; i < qvlist_info->num_vectors; i++) {
  723. qv_info = &qvlist_info->qv_info[i];
  724. if (!qv_info)
  725. continue;
  726. v_idx = qv_info->v_idx;
  727. /* Validate vector id belongs to this client */
  728. if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
  729. (v_idx < pf->iwarp_base_vector))
  730. goto err;
  731. ldev->qvlist_info->qv_info[i] = *qv_info;
  732. reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
  733. if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
  734. /* Special case - No CEQ mapped on this vector */
  735. wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
  736. } else {
  737. reg = (qv_info->ceq_idx &
  738. I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  739. (I40E_QUEUE_TYPE_PE_CEQ <<
  740. I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  741. wr32(hw, reg_idx, reg);
  742. reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
  743. (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
  744. (qv_info->itr_idx <<
  745. I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
  746. (I40E_QUEUE_END_OF_LIST <<
  747. I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
  748. wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
  749. }
  750. if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
  751. reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
  752. (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
  753. (qv_info->itr_idx <<
  754. I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
  755. wr32(hw, I40E_PFINT_AEQCTL, reg);
  756. }
  757. }
  758. /* Mitigate sync problems with iwarp VF driver */
  759. i40e_flush(hw);
  760. return 0;
  761. err:
  762. kfree(ldev->qvlist_info);
  763. ldev->qvlist_info = NULL;
  764. return -EINVAL;
  765. }
  766. /**
  767. * i40e_client_request_reset
  768. * @ldev: pointer to L2 context.
  769. * @client: Client pointer.
  770. * @level: reset level
  771. **/
  772. static void i40e_client_request_reset(struct i40e_info *ldev,
  773. struct i40e_client *client,
  774. u32 reset_level)
  775. {
  776. struct i40e_pf *pf = ldev->pf;
  777. switch (reset_level) {
  778. case I40E_CLIENT_RESET_LEVEL_PF:
  779. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  780. break;
  781. case I40E_CLIENT_RESET_LEVEL_CORE:
  782. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  783. break;
  784. default:
  785. dev_warn(&pf->pdev->dev,
  786. "Client %s instance for PF id %d request an unsupported reset: %d.\n",
  787. client->name, pf->hw.pf_id, reset_level);
  788. break;
  789. }
  790. i40e_service_event_schedule(pf);
  791. }
  792. /**
  793. * i40e_client_update_vsi_ctxt
  794. * @ldev: pointer to L2 context.
  795. * @client: Client pointer.
  796. * @is_vf: if this for the VF
  797. * @vf_id: if is_vf true this carries the vf_id
  798. * @flag: Any device level setting that needs to be done for PE
  799. * @valid_flag: Bits in this match up and enable changing of flag bits
  800. *
  801. * Return 0 on success or < 0 on error
  802. **/
  803. static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
  804. struct i40e_client *client,
  805. bool is_vf, u32 vf_id,
  806. u32 flag, u32 valid_flag)
  807. {
  808. struct i40e_pf *pf = ldev->pf;
  809. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  810. struct i40e_vsi_context ctxt;
  811. bool update = true;
  812. i40e_status err;
  813. /* TODO: for now do not allow setting VF's VSI setting */
  814. if (is_vf)
  815. return -EINVAL;
  816. ctxt.seid = pf->main_vsi_seid;
  817. ctxt.pf_num = pf->hw.pf_id;
  818. err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  819. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  820. if (err) {
  821. dev_info(&pf->pdev->dev,
  822. "couldn't get PF vsi config, err %s aq_err %s\n",
  823. i40e_stat_str(&pf->hw, err),
  824. i40e_aq_str(&pf->hw,
  825. pf->hw.aq.asq_last_status));
  826. return -ENOENT;
  827. }
  828. if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
  829. (flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
  830. ctxt.info.valid_sections =
  831. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  832. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  833. } else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
  834. !(flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
  835. ctxt.info.valid_sections =
  836. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  837. ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  838. } else {
  839. update = false;
  840. dev_warn(&pf->pdev->dev,
  841. "Client %s instance for PF id %d request an unsupported Config: %x.\n",
  842. client->name, pf->hw.pf_id, flag);
  843. }
  844. if (update) {
  845. err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  846. if (err) {
  847. dev_info(&pf->pdev->dev,
  848. "update VSI ctxt for PE failed, err %s aq_err %s\n",
  849. i40e_stat_str(&pf->hw, err),
  850. i40e_aq_str(&pf->hw,
  851. pf->hw.aq.asq_last_status));
  852. }
  853. }
  854. return err;
  855. }
  856. /**
  857. * i40e_register_client - Register a i40e client driver with the L2 driver
  858. * @client: pointer to the i40e_client struct
  859. *
  860. * Returns 0 on success or non-0 on error
  861. **/
  862. int i40e_register_client(struct i40e_client *client)
  863. {
  864. int ret = 0;
  865. enum i40e_vsi_type vsi_type;
  866. if (!client) {
  867. ret = -EIO;
  868. goto out;
  869. }
  870. if (strlen(client->name) == 0) {
  871. pr_info("i40e: Failed to register client with no name\n");
  872. ret = -EIO;
  873. goto out;
  874. }
  875. mutex_lock(&i40e_client_mutex);
  876. if (i40e_client_is_registered(client)) {
  877. pr_info("i40e: Client %s has already been registered!\n",
  878. client->name);
  879. mutex_unlock(&i40e_client_mutex);
  880. ret = -EEXIST;
  881. goto out;
  882. }
  883. if ((client->version.major != I40E_CLIENT_VERSION_MAJOR) ||
  884. (client->version.minor != I40E_CLIENT_VERSION_MINOR)) {
  885. pr_info("i40e: Failed to register client %s due to mismatched client interface version\n",
  886. client->name);
  887. pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
  888. client->version.major, client->version.minor,
  889. client->version.build,
  890. i40e_client_interface_version_str);
  891. mutex_unlock(&i40e_client_mutex);
  892. ret = -EIO;
  893. goto out;
  894. }
  895. vsi_type = i40e_client_type_to_vsi_type(client->type);
  896. if (vsi_type == I40E_VSI_TYPE_UNKNOWN) {
  897. pr_info("i40e: Failed to register client %s due to unknown client type %d\n",
  898. client->name, client->type);
  899. mutex_unlock(&i40e_client_mutex);
  900. ret = -EIO;
  901. goto out;
  902. }
  903. list_add(&client->list, &i40e_clients);
  904. set_bit(__I40E_CLIENT_REGISTERED, &client->state);
  905. mutex_unlock(&i40e_client_mutex);
  906. if (i40e_client_prepare(client)) {
  907. ret = -EIO;
  908. goto out;
  909. }
  910. pr_info("i40e: Registered client %s with return code %d\n",
  911. client->name, ret);
  912. out:
  913. return ret;
  914. }
  915. EXPORT_SYMBOL(i40e_register_client);
  916. /**
  917. * i40e_unregister_client - Unregister a i40e client driver with the L2 driver
  918. * @client: pointer to the i40e_client struct
  919. *
  920. * Returns 0 on success or non-0 on error
  921. **/
  922. int i40e_unregister_client(struct i40e_client *client)
  923. {
  924. int ret = 0;
  925. /* When a unregister request comes through we would have to send
  926. * a close for each of the client instances that were opened.
  927. * client_release function is called to handle this.
  928. */
  929. mutex_lock(&i40e_client_mutex);
  930. if (!client || i40e_client_release(client)) {
  931. ret = -EIO;
  932. goto out;
  933. }
  934. /* TODO: check if device is in reset, or if that matters? */
  935. if (!i40e_client_is_registered(client)) {
  936. pr_info("i40e: Client %s has not been registered\n",
  937. client->name);
  938. ret = -ENODEV;
  939. goto out;
  940. }
  941. if (atomic_read(&client->ref_cnt) == 0) {
  942. clear_bit(__I40E_CLIENT_REGISTERED, &client->state);
  943. list_del(&client->list);
  944. pr_info("i40e: Unregistered client %s with return code %d\n",
  945. client->name, ret);
  946. } else {
  947. ret = I40E_ERR_NOT_READY;
  948. pr_err("i40e: Client %s failed unregister - client has open instances\n",
  949. client->name);
  950. }
  951. out:
  952. mutex_unlock(&i40e_client_mutex);
  953. return ret;
  954. }
  955. EXPORT_SYMBOL(i40e_unregister_client);