i40e_client.c 28 KB

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