i40iw_utils.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2015-2016 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenFabrics.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. *******************************************************************************/
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/etherdevice.h>
  38. #include <linux/ethtool.h>
  39. #include <linux/mii.h>
  40. #include <linux/if_vlan.h>
  41. #include <linux/crc32.h>
  42. #include <linux/in.h>
  43. #include <linux/ip.h>
  44. #include <linux/tcp.h>
  45. #include <linux/init.h>
  46. #include <linux/io.h>
  47. #include <asm/irq.h>
  48. #include <asm/byteorder.h>
  49. #include <net/netevent.h>
  50. #include <net/neighbour.h>
  51. #include "i40iw.h"
  52. /**
  53. * i40iw_arp_table - manage arp table
  54. * @iwdev: iwarp device
  55. * @ip_addr: ip address for device
  56. * @mac_addr: mac address ptr
  57. * @action: modify, delete or add
  58. */
  59. int i40iw_arp_table(struct i40iw_device *iwdev,
  60. u32 *ip_addr,
  61. bool ipv4,
  62. u8 *mac_addr,
  63. u32 action)
  64. {
  65. int arp_index;
  66. int err;
  67. u32 ip[4];
  68. if (ipv4) {
  69. memset(ip, 0, sizeof(ip));
  70. ip[0] = *ip_addr;
  71. } else {
  72. memcpy(ip, ip_addr, sizeof(ip));
  73. }
  74. for (arp_index = 0; (u32)arp_index < iwdev->arp_table_size; arp_index++)
  75. if (memcmp(iwdev->arp_table[arp_index].ip_addr, ip, sizeof(ip)) == 0)
  76. break;
  77. switch (action) {
  78. case I40IW_ARP_ADD:
  79. if (arp_index != iwdev->arp_table_size)
  80. return -1;
  81. arp_index = 0;
  82. err = i40iw_alloc_resource(iwdev, iwdev->allocated_arps,
  83. iwdev->arp_table_size,
  84. (u32 *)&arp_index,
  85. &iwdev->next_arp_index);
  86. if (err)
  87. return err;
  88. memcpy(iwdev->arp_table[arp_index].ip_addr, ip, sizeof(ip));
  89. ether_addr_copy(iwdev->arp_table[arp_index].mac_addr, mac_addr);
  90. break;
  91. case I40IW_ARP_RESOLVE:
  92. if (arp_index == iwdev->arp_table_size)
  93. return -1;
  94. break;
  95. case I40IW_ARP_DELETE:
  96. if (arp_index == iwdev->arp_table_size)
  97. return -1;
  98. memset(iwdev->arp_table[arp_index].ip_addr, 0,
  99. sizeof(iwdev->arp_table[arp_index].ip_addr));
  100. eth_zero_addr(iwdev->arp_table[arp_index].mac_addr);
  101. i40iw_free_resource(iwdev, iwdev->allocated_arps, arp_index);
  102. break;
  103. default:
  104. return -1;
  105. }
  106. return arp_index;
  107. }
  108. /**
  109. * i40iw_wr32 - write 32 bits to hw register
  110. * @hw: hardware information including registers
  111. * @reg: register offset
  112. * @value: vvalue to write to register
  113. */
  114. inline void i40iw_wr32(struct i40iw_hw *hw, u32 reg, u32 value)
  115. {
  116. writel(value, hw->hw_addr + reg);
  117. }
  118. /**
  119. * i40iw_rd32 - read a 32 bit hw register
  120. * @hw: hardware information including registers
  121. * @reg: register offset
  122. *
  123. * Return value of register content
  124. */
  125. inline u32 i40iw_rd32(struct i40iw_hw *hw, u32 reg)
  126. {
  127. return readl(hw->hw_addr + reg);
  128. }
  129. /**
  130. * i40iw_inetaddr_event - system notifier for netdev events
  131. * @notfier: not used
  132. * @event: event for notifier
  133. * @ptr: if address
  134. */
  135. int i40iw_inetaddr_event(struct notifier_block *notifier,
  136. unsigned long event,
  137. void *ptr)
  138. {
  139. struct in_ifaddr *ifa = ptr;
  140. struct net_device *event_netdev = ifa->ifa_dev->dev;
  141. struct net_device *netdev;
  142. struct net_device *upper_dev;
  143. struct i40iw_device *iwdev;
  144. struct i40iw_handler *hdl;
  145. u32 local_ipaddr;
  146. hdl = i40iw_find_netdev(event_netdev);
  147. if (!hdl)
  148. return NOTIFY_DONE;
  149. iwdev = &hdl->device;
  150. netdev = iwdev->ldev->netdev;
  151. upper_dev = netdev_master_upper_dev_get(netdev);
  152. if (netdev != event_netdev)
  153. return NOTIFY_DONE;
  154. switch (event) {
  155. case NETDEV_DOWN:
  156. if (upper_dev)
  157. local_ipaddr = ntohl(
  158. ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address);
  159. else
  160. local_ipaddr = ntohl(ifa->ifa_address);
  161. i40iw_manage_arp_cache(iwdev,
  162. netdev->dev_addr,
  163. &local_ipaddr,
  164. true,
  165. I40IW_ARP_DELETE);
  166. return NOTIFY_OK;
  167. case NETDEV_UP:
  168. if (upper_dev)
  169. local_ipaddr = ntohl(
  170. ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address);
  171. else
  172. local_ipaddr = ntohl(ifa->ifa_address);
  173. i40iw_manage_arp_cache(iwdev,
  174. netdev->dev_addr,
  175. &local_ipaddr,
  176. true,
  177. I40IW_ARP_ADD);
  178. break;
  179. case NETDEV_CHANGEADDR:
  180. /* Add the address to the IP table */
  181. if (upper_dev)
  182. local_ipaddr = ntohl(
  183. ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address);
  184. else
  185. local_ipaddr = ntohl(ifa->ifa_address);
  186. i40iw_manage_arp_cache(iwdev,
  187. netdev->dev_addr,
  188. &local_ipaddr,
  189. true,
  190. I40IW_ARP_ADD);
  191. break;
  192. default:
  193. break;
  194. }
  195. return NOTIFY_DONE;
  196. }
  197. /**
  198. * i40iw_inet6addr_event - system notifier for ipv6 netdev events
  199. * @notfier: not used
  200. * @event: event for notifier
  201. * @ptr: if address
  202. */
  203. int i40iw_inet6addr_event(struct notifier_block *notifier,
  204. unsigned long event,
  205. void *ptr)
  206. {
  207. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  208. struct net_device *event_netdev = ifa->idev->dev;
  209. struct net_device *netdev;
  210. struct i40iw_device *iwdev;
  211. struct i40iw_handler *hdl;
  212. u32 local_ipaddr6[4];
  213. hdl = i40iw_find_netdev(event_netdev);
  214. if (!hdl)
  215. return NOTIFY_DONE;
  216. iwdev = &hdl->device;
  217. netdev = iwdev->ldev->netdev;
  218. if (netdev != event_netdev)
  219. return NOTIFY_DONE;
  220. switch (event) {
  221. case NETDEV_DOWN:
  222. i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
  223. i40iw_manage_arp_cache(iwdev,
  224. netdev->dev_addr,
  225. local_ipaddr6,
  226. false,
  227. I40IW_ARP_DELETE);
  228. return NOTIFY_OK;
  229. case NETDEV_UP:
  230. /* Fall through */
  231. case NETDEV_CHANGEADDR:
  232. i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
  233. i40iw_manage_arp_cache(iwdev,
  234. netdev->dev_addr,
  235. local_ipaddr6,
  236. false,
  237. I40IW_ARP_ADD);
  238. break;
  239. default:
  240. break;
  241. }
  242. return NOTIFY_DONE;
  243. }
  244. /**
  245. * i40iw_net_event - system notifier for net events
  246. * @notfier: not used
  247. * @event: event for notifier
  248. * @ptr: neighbor
  249. */
  250. int i40iw_net_event(struct notifier_block *notifier, unsigned long event, void *ptr)
  251. {
  252. struct neighbour *neigh = ptr;
  253. struct i40iw_device *iwdev;
  254. struct i40iw_handler *iwhdl;
  255. __be32 *p;
  256. u32 local_ipaddr[4];
  257. switch (event) {
  258. case NETEVENT_NEIGH_UPDATE:
  259. iwhdl = i40iw_find_netdev((struct net_device *)neigh->dev);
  260. if (!iwhdl)
  261. return NOTIFY_DONE;
  262. iwdev = &iwhdl->device;
  263. p = (__be32 *)neigh->primary_key;
  264. i40iw_copy_ip_ntohl(local_ipaddr, p);
  265. if (neigh->nud_state & NUD_VALID) {
  266. i40iw_manage_arp_cache(iwdev,
  267. neigh->ha,
  268. local_ipaddr,
  269. false,
  270. I40IW_ARP_ADD);
  271. } else {
  272. i40iw_manage_arp_cache(iwdev,
  273. neigh->ha,
  274. local_ipaddr,
  275. false,
  276. I40IW_ARP_DELETE);
  277. }
  278. break;
  279. default:
  280. break;
  281. }
  282. return NOTIFY_DONE;
  283. }
  284. /**
  285. * i40iw_get_cqp_request - get cqp struct
  286. * @cqp: device cqp ptr
  287. * @wait: cqp to be used in wait mode
  288. */
  289. struct i40iw_cqp_request *i40iw_get_cqp_request(struct i40iw_cqp *cqp, bool wait)
  290. {
  291. struct i40iw_cqp_request *cqp_request = NULL;
  292. unsigned long flags;
  293. spin_lock_irqsave(&cqp->req_lock, flags);
  294. if (!list_empty(&cqp->cqp_avail_reqs)) {
  295. cqp_request = list_entry(cqp->cqp_avail_reqs.next,
  296. struct i40iw_cqp_request, list);
  297. list_del_init(&cqp_request->list);
  298. }
  299. spin_unlock_irqrestore(&cqp->req_lock, flags);
  300. if (!cqp_request) {
  301. cqp_request = kzalloc(sizeof(*cqp_request), GFP_ATOMIC);
  302. if (cqp_request) {
  303. cqp_request->dynamic = true;
  304. INIT_LIST_HEAD(&cqp_request->list);
  305. init_waitqueue_head(&cqp_request->waitq);
  306. }
  307. }
  308. if (!cqp_request) {
  309. i40iw_pr_err("CQP Request Fail: No Memory");
  310. return NULL;
  311. }
  312. if (wait) {
  313. atomic_set(&cqp_request->refcount, 2);
  314. cqp_request->waiting = true;
  315. } else {
  316. atomic_set(&cqp_request->refcount, 1);
  317. }
  318. return cqp_request;
  319. }
  320. /**
  321. * i40iw_free_cqp_request - free cqp request
  322. * @cqp: cqp ptr
  323. * @cqp_request: to be put back in cqp list
  324. */
  325. void i40iw_free_cqp_request(struct i40iw_cqp *cqp, struct i40iw_cqp_request *cqp_request)
  326. {
  327. unsigned long flags;
  328. if (cqp_request->dynamic) {
  329. kfree(cqp_request);
  330. } else {
  331. cqp_request->request_done = false;
  332. cqp_request->callback_fcn = NULL;
  333. cqp_request->waiting = false;
  334. spin_lock_irqsave(&cqp->req_lock, flags);
  335. list_add_tail(&cqp_request->list, &cqp->cqp_avail_reqs);
  336. spin_unlock_irqrestore(&cqp->req_lock, flags);
  337. }
  338. }
  339. /**
  340. * i40iw_put_cqp_request - dec ref count and free if 0
  341. * @cqp: cqp ptr
  342. * @cqp_request: to be put back in cqp list
  343. */
  344. void i40iw_put_cqp_request(struct i40iw_cqp *cqp,
  345. struct i40iw_cqp_request *cqp_request)
  346. {
  347. if (atomic_dec_and_test(&cqp_request->refcount))
  348. i40iw_free_cqp_request(cqp, cqp_request);
  349. }
  350. /**
  351. * i40iw_free_qp - callback after destroy cqp completes
  352. * @cqp_request: cqp request for destroy qp
  353. * @num: not used
  354. */
  355. static void i40iw_free_qp(struct i40iw_cqp_request *cqp_request, u32 num)
  356. {
  357. struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)cqp_request->param;
  358. struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
  359. struct i40iw_device *iwdev;
  360. u32 qp_num = iwqp->ibqp.qp_num;
  361. iwdev = iwqp->iwdev;
  362. i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
  363. i40iw_free_qp_resources(iwdev, iwqp, qp_num);
  364. }
  365. /**
  366. * i40iw_wait_event - wait for completion
  367. * @iwdev: iwarp device
  368. * @cqp_request: cqp request to wait
  369. */
  370. static int i40iw_wait_event(struct i40iw_device *iwdev,
  371. struct i40iw_cqp_request *cqp_request)
  372. {
  373. struct cqp_commands_info *info = &cqp_request->info;
  374. struct i40iw_cqp *iwcqp = &iwdev->cqp;
  375. bool cqp_error = false;
  376. int err_code = 0;
  377. int timeout_ret = 0;
  378. timeout_ret = wait_event_timeout(cqp_request->waitq,
  379. cqp_request->request_done,
  380. I40IW_EVENT_TIMEOUT);
  381. if (!timeout_ret) {
  382. i40iw_pr_err("error cqp command 0x%x timed out ret = %d\n",
  383. info->cqp_cmd, timeout_ret);
  384. err_code = -ETIME;
  385. i40iw_request_reset(iwdev);
  386. goto done;
  387. }
  388. cqp_error = cqp_request->compl_info.error;
  389. if (cqp_error) {
  390. i40iw_pr_err("error cqp command 0x%x completion maj = 0x%x min=0x%x\n",
  391. info->cqp_cmd, cqp_request->compl_info.maj_err_code,
  392. cqp_request->compl_info.min_err_code);
  393. err_code = -EPROTO;
  394. goto done;
  395. }
  396. done:
  397. i40iw_put_cqp_request(iwcqp, cqp_request);
  398. return err_code;
  399. }
  400. /**
  401. * i40iw_handle_cqp_op - process cqp command
  402. * @iwdev: iwarp device
  403. * @cqp_request: cqp request to process
  404. */
  405. enum i40iw_status_code i40iw_handle_cqp_op(struct i40iw_device *iwdev,
  406. struct i40iw_cqp_request
  407. *cqp_request)
  408. {
  409. struct i40iw_sc_dev *dev = &iwdev->sc_dev;
  410. enum i40iw_status_code status;
  411. struct cqp_commands_info *info = &cqp_request->info;
  412. int err_code = 0;
  413. status = i40iw_process_cqp_cmd(dev, info);
  414. if (status) {
  415. i40iw_pr_err("error cqp command 0x%x failed\n", info->cqp_cmd);
  416. i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
  417. return status;
  418. }
  419. if (cqp_request->waiting)
  420. err_code = i40iw_wait_event(iwdev, cqp_request);
  421. if (err_code)
  422. status = I40IW_ERR_CQP_COMPL_ERROR;
  423. return status;
  424. }
  425. /**
  426. * i40iw_add_pdusecount - add pd refcount
  427. * @iwpd: pd for refcount
  428. */
  429. void i40iw_add_pdusecount(struct i40iw_pd *iwpd)
  430. {
  431. atomic_inc(&iwpd->usecount);
  432. }
  433. /**
  434. * i40iw_rem_pdusecount - decrement refcount for pd and free if 0
  435. * @iwpd: pd for refcount
  436. * @iwdev: iwarp device
  437. */
  438. void i40iw_rem_pdusecount(struct i40iw_pd *iwpd, struct i40iw_device *iwdev)
  439. {
  440. if (!atomic_dec_and_test(&iwpd->usecount))
  441. return;
  442. i40iw_free_resource(iwdev, iwdev->allocated_pds, iwpd->sc_pd.pd_id);
  443. kfree(iwpd);
  444. }
  445. /**
  446. * i40iw_add_ref - add refcount for qp
  447. * @ibqp: iqarp qp
  448. */
  449. void i40iw_add_ref(struct ib_qp *ibqp)
  450. {
  451. struct i40iw_qp *iwqp = (struct i40iw_qp *)ibqp;
  452. atomic_inc(&iwqp->refcount);
  453. }
  454. /**
  455. * i40iw_rem_ref - rem refcount for qp and free if 0
  456. * @ibqp: iqarp qp
  457. */
  458. void i40iw_rem_ref(struct ib_qp *ibqp)
  459. {
  460. struct i40iw_qp *iwqp;
  461. enum i40iw_status_code status;
  462. struct i40iw_cqp_request *cqp_request;
  463. struct cqp_commands_info *cqp_info;
  464. struct i40iw_device *iwdev;
  465. u32 qp_num;
  466. unsigned long flags;
  467. iwqp = to_iwqp(ibqp);
  468. iwdev = iwqp->iwdev;
  469. spin_lock_irqsave(&iwdev->qptable_lock, flags);
  470. if (!atomic_dec_and_test(&iwqp->refcount)) {
  471. spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
  472. return;
  473. }
  474. qp_num = iwqp->ibqp.qp_num;
  475. iwdev->qp_table[qp_num] = NULL;
  476. spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
  477. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
  478. if (!cqp_request)
  479. return;
  480. cqp_request->callback_fcn = i40iw_free_qp;
  481. cqp_request->param = (void *)&iwqp->sc_qp;
  482. cqp_info = &cqp_request->info;
  483. cqp_info->cqp_cmd = OP_QP_DESTROY;
  484. cqp_info->post_sq = 1;
  485. cqp_info->in.u.qp_destroy.qp = &iwqp->sc_qp;
  486. cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
  487. cqp_info->in.u.qp_destroy.remove_hash_idx = true;
  488. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  489. if (status)
  490. i40iw_pr_err("CQP-OP Destroy QP fail");
  491. }
  492. /**
  493. * i40iw_get_qp - get qp address
  494. * @device: iwarp device
  495. * @qpn: qp number
  496. */
  497. struct ib_qp *i40iw_get_qp(struct ib_device *device, int qpn)
  498. {
  499. struct i40iw_device *iwdev = to_iwdev(device);
  500. if ((qpn < IW_FIRST_QPN) || (qpn >= iwdev->max_qp))
  501. return NULL;
  502. return &iwdev->qp_table[qpn]->ibqp;
  503. }
  504. /**
  505. * i40iw_debug_buf - print debug msg and buffer is mask set
  506. * @dev: hardware control device structure
  507. * @mask: mask to compare if to print debug buffer
  508. * @buf: points buffer addr
  509. * @size: saize of buffer to print
  510. */
  511. void i40iw_debug_buf(struct i40iw_sc_dev *dev,
  512. enum i40iw_debug_flag mask,
  513. char *desc,
  514. u64 *buf,
  515. u32 size)
  516. {
  517. u32 i;
  518. if (!(dev->debug_mask & mask))
  519. return;
  520. i40iw_debug(dev, mask, "%s\n", desc);
  521. i40iw_debug(dev, mask, "starting address virt=%p phy=%llxh\n", buf,
  522. (unsigned long long)virt_to_phys(buf));
  523. for (i = 0; i < size; i += 8)
  524. i40iw_debug(dev, mask, "index %03d val: %016llx\n", i, buf[i / 8]);
  525. }
  526. /**
  527. * i40iw_get_hw_addr - return hw addr
  528. * @par: points to shared dev
  529. */
  530. u8 __iomem *i40iw_get_hw_addr(void *par)
  531. {
  532. struct i40iw_sc_dev *dev = (struct i40iw_sc_dev *)par;
  533. return dev->hw->hw_addr;
  534. }
  535. /**
  536. * i40iw_remove_head - return head entry and remove from list
  537. * @list: list for entry
  538. */
  539. void *i40iw_remove_head(struct list_head *list)
  540. {
  541. struct list_head *entry;
  542. if (list_empty(list))
  543. return NULL;
  544. entry = (void *)list->next;
  545. list_del(entry);
  546. return (void *)entry;
  547. }
  548. /**
  549. * i40iw_allocate_dma_mem - Memory alloc helper fn
  550. * @hw: pointer to the HW structure
  551. * @mem: ptr to mem struct to fill out
  552. * @size: size of memory requested
  553. * @alignment: what to align the allocation to
  554. */
  555. enum i40iw_status_code i40iw_allocate_dma_mem(struct i40iw_hw *hw,
  556. struct i40iw_dma_mem *mem,
  557. u64 size,
  558. u32 alignment)
  559. {
  560. struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
  561. if (!mem)
  562. return I40IW_ERR_PARAM;
  563. mem->size = ALIGN(size, alignment);
  564. mem->va = dma_zalloc_coherent(&pcidev->dev, mem->size,
  565. (dma_addr_t *)&mem->pa, GFP_KERNEL);
  566. if (!mem->va)
  567. return I40IW_ERR_NO_MEMORY;
  568. return 0;
  569. }
  570. /**
  571. * i40iw_free_dma_mem - Memory free helper fn
  572. * @hw: pointer to the HW structure
  573. * @mem: ptr to mem struct to free
  574. */
  575. void i40iw_free_dma_mem(struct i40iw_hw *hw, struct i40iw_dma_mem *mem)
  576. {
  577. struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
  578. if (!mem || !mem->va)
  579. return;
  580. dma_free_coherent(&pcidev->dev, mem->size,
  581. mem->va, (dma_addr_t)mem->pa);
  582. mem->va = NULL;
  583. }
  584. /**
  585. * i40iw_allocate_virt_mem - virtual memory alloc helper fn
  586. * @hw: pointer to the HW structure
  587. * @mem: ptr to mem struct to fill out
  588. * @size: size of memory requested
  589. */
  590. enum i40iw_status_code i40iw_allocate_virt_mem(struct i40iw_hw *hw,
  591. struct i40iw_virt_mem *mem,
  592. u32 size)
  593. {
  594. if (!mem)
  595. return I40IW_ERR_PARAM;
  596. mem->size = size;
  597. mem->va = kzalloc(size, GFP_KERNEL);
  598. if (mem->va)
  599. return 0;
  600. else
  601. return I40IW_ERR_NO_MEMORY;
  602. }
  603. /**
  604. * i40iw_free_virt_mem - virtual memory free helper fn
  605. * @hw: pointer to the HW structure
  606. * @mem: ptr to mem struct to free
  607. */
  608. enum i40iw_status_code i40iw_free_virt_mem(struct i40iw_hw *hw,
  609. struct i40iw_virt_mem *mem)
  610. {
  611. if (!mem)
  612. return I40IW_ERR_PARAM;
  613. /*
  614. * mem->va points to the parent of mem, so both mem and mem->va
  615. * can not be touched once mem->va is freed
  616. */
  617. kfree(mem->va);
  618. return 0;
  619. }
  620. /**
  621. * i40iw_cqp_sds_cmd - create cqp command for sd
  622. * @dev: hardware control device structure
  623. * @sd_info: information for sd cqp
  624. *
  625. */
  626. enum i40iw_status_code i40iw_cqp_sds_cmd(struct i40iw_sc_dev *dev,
  627. struct i40iw_update_sds_info *sdinfo)
  628. {
  629. enum i40iw_status_code status;
  630. struct i40iw_cqp_request *cqp_request;
  631. struct cqp_commands_info *cqp_info;
  632. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  633. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  634. if (!cqp_request)
  635. return I40IW_ERR_NO_MEMORY;
  636. cqp_info = &cqp_request->info;
  637. memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
  638. sizeof(cqp_info->in.u.update_pe_sds.info));
  639. cqp_info->cqp_cmd = OP_UPDATE_PE_SDS;
  640. cqp_info->post_sq = 1;
  641. cqp_info->in.u.update_pe_sds.dev = dev;
  642. cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
  643. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  644. if (status)
  645. i40iw_pr_err("CQP-OP Update SD's fail");
  646. return status;
  647. }
  648. /**
  649. * i40iw_term_modify_qp - modify qp for term message
  650. * @qp: hardware control qp
  651. * @next_state: qp's next state
  652. * @term: terminate code
  653. * @term_len: length
  654. */
  655. void i40iw_term_modify_qp(struct i40iw_sc_qp *qp, u8 next_state, u8 term, u8 term_len)
  656. {
  657. struct i40iw_qp *iwqp;
  658. iwqp = (struct i40iw_qp *)qp->back_qp;
  659. i40iw_next_iw_state(iwqp, next_state, 0, term, term_len);
  660. };
  661. /**
  662. * i40iw_terminate_done - after terminate is completed
  663. * @qp: hardware control qp
  664. * @timeout_occurred: indicates if terminate timer expired
  665. */
  666. void i40iw_terminate_done(struct i40iw_sc_qp *qp, int timeout_occurred)
  667. {
  668. struct i40iw_qp *iwqp;
  669. u32 next_iwarp_state = I40IW_QP_STATE_ERROR;
  670. u8 hte = 0;
  671. bool first_time;
  672. unsigned long flags;
  673. iwqp = (struct i40iw_qp *)qp->back_qp;
  674. spin_lock_irqsave(&iwqp->lock, flags);
  675. if (iwqp->hte_added) {
  676. iwqp->hte_added = 0;
  677. hte = 1;
  678. }
  679. first_time = !(qp->term_flags & I40IW_TERM_DONE);
  680. qp->term_flags |= I40IW_TERM_DONE;
  681. spin_unlock_irqrestore(&iwqp->lock, flags);
  682. if (first_time) {
  683. if (!timeout_occurred)
  684. i40iw_terminate_del_timer(qp);
  685. else
  686. next_iwarp_state = I40IW_QP_STATE_CLOSING;
  687. i40iw_next_iw_state(iwqp, next_iwarp_state, hte, 0, 0);
  688. i40iw_cm_disconn(iwqp);
  689. }
  690. }
  691. /**
  692. * i40iw_terminate_imeout - timeout happened
  693. * @context: points to iwarp qp
  694. */
  695. static void i40iw_terminate_timeout(unsigned long context)
  696. {
  697. struct i40iw_qp *iwqp = (struct i40iw_qp *)context;
  698. struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)&iwqp->sc_qp;
  699. i40iw_terminate_done(qp, 1);
  700. }
  701. /**
  702. * i40iw_terminate_start_timer - start terminate timeout
  703. * @qp: hardware control qp
  704. */
  705. void i40iw_terminate_start_timer(struct i40iw_sc_qp *qp)
  706. {
  707. struct i40iw_qp *iwqp;
  708. iwqp = (struct i40iw_qp *)qp->back_qp;
  709. init_timer(&iwqp->terminate_timer);
  710. iwqp->terminate_timer.function = i40iw_terminate_timeout;
  711. iwqp->terminate_timer.expires = jiffies + HZ;
  712. iwqp->terminate_timer.data = (unsigned long)iwqp;
  713. add_timer(&iwqp->terminate_timer);
  714. }
  715. /**
  716. * i40iw_terminate_del_timer - delete terminate timeout
  717. * @qp: hardware control qp
  718. */
  719. void i40iw_terminate_del_timer(struct i40iw_sc_qp *qp)
  720. {
  721. struct i40iw_qp *iwqp;
  722. iwqp = (struct i40iw_qp *)qp->back_qp;
  723. del_timer(&iwqp->terminate_timer);
  724. }
  725. /**
  726. * i40iw_cqp_generic_worker - generic worker for cqp
  727. * @work: work pointer
  728. */
  729. static void i40iw_cqp_generic_worker(struct work_struct *work)
  730. {
  731. struct i40iw_virtchnl_work_info *work_info =
  732. &((struct virtchnl_work *)work)->work_info;
  733. if (work_info->worker_vf_dev)
  734. work_info->callback_fcn(work_info->worker_vf_dev);
  735. }
  736. /**
  737. * i40iw_cqp_spawn_worker - spawn worket thread
  738. * @iwdev: device struct pointer
  739. * @work_info: work request info
  740. * @iw_vf_idx: virtual function index
  741. */
  742. void i40iw_cqp_spawn_worker(struct i40iw_sc_dev *dev,
  743. struct i40iw_virtchnl_work_info *work_info,
  744. u32 iw_vf_idx)
  745. {
  746. struct virtchnl_work *work;
  747. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  748. work = &iwdev->virtchnl_w[iw_vf_idx];
  749. memcpy(&work->work_info, work_info, sizeof(*work_info));
  750. INIT_WORK(&work->work, i40iw_cqp_generic_worker);
  751. queue_work(iwdev->virtchnl_wq, &work->work);
  752. }
  753. /**
  754. * i40iw_cqp_manage_hmc_fcn_worker -
  755. * @work: work pointer for hmc info
  756. */
  757. static void i40iw_cqp_manage_hmc_fcn_worker(struct work_struct *work)
  758. {
  759. struct i40iw_cqp_request *cqp_request =
  760. ((struct virtchnl_work *)work)->cqp_request;
  761. struct i40iw_ccq_cqe_info ccq_cqe_info;
  762. struct i40iw_hmc_fcn_info *hmcfcninfo =
  763. &cqp_request->info.in.u.manage_hmc_pm.info;
  764. struct i40iw_device *iwdev =
  765. (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->back_dev;
  766. ccq_cqe_info.cqp = NULL;
  767. ccq_cqe_info.maj_err_code = cqp_request->compl_info.maj_err_code;
  768. ccq_cqe_info.min_err_code = cqp_request->compl_info.min_err_code;
  769. ccq_cqe_info.op_code = cqp_request->compl_info.op_code;
  770. ccq_cqe_info.op_ret_val = cqp_request->compl_info.op_ret_val;
  771. ccq_cqe_info.scratch = 0;
  772. ccq_cqe_info.error = cqp_request->compl_info.error;
  773. hmcfcninfo->callback_fcn(cqp_request->info.in.u.manage_hmc_pm.dev,
  774. hmcfcninfo->cqp_callback_param, &ccq_cqe_info);
  775. i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
  776. }
  777. /**
  778. * i40iw_cqp_manage_hmc_fcn_callback - called function after cqp completion
  779. * @cqp_request: cqp request info struct for hmc fun
  780. * @unused: unused param of callback
  781. */
  782. static void i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request *cqp_request,
  783. u32 unused)
  784. {
  785. struct virtchnl_work *work;
  786. struct i40iw_hmc_fcn_info *hmcfcninfo =
  787. &cqp_request->info.in.u.manage_hmc_pm.info;
  788. struct i40iw_device *iwdev =
  789. (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->
  790. back_dev;
  791. if (hmcfcninfo && hmcfcninfo->callback_fcn) {
  792. i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s1\n", __func__);
  793. atomic_inc(&cqp_request->refcount);
  794. work = &iwdev->virtchnl_w[hmcfcninfo->iw_vf_idx];
  795. work->cqp_request = cqp_request;
  796. INIT_WORK(&work->work, i40iw_cqp_manage_hmc_fcn_worker);
  797. queue_work(iwdev->virtchnl_wq, &work->work);
  798. i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s2\n", __func__);
  799. } else {
  800. i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s: Something wrong\n", __func__);
  801. }
  802. }
  803. /**
  804. * i40iw_cqp_manage_hmc_fcn_cmd - issue cqp command to manage hmc
  805. * @dev: hardware control device structure
  806. * @hmcfcninfo: info for hmc
  807. */
  808. enum i40iw_status_code i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev *dev,
  809. struct i40iw_hmc_fcn_info *hmcfcninfo)
  810. {
  811. enum i40iw_status_code status;
  812. struct i40iw_cqp_request *cqp_request;
  813. struct cqp_commands_info *cqp_info;
  814. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  815. i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s\n", __func__);
  816. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
  817. if (!cqp_request)
  818. return I40IW_ERR_NO_MEMORY;
  819. cqp_info = &cqp_request->info;
  820. cqp_request->callback_fcn = i40iw_cqp_manage_hmc_fcn_callback;
  821. cqp_request->param = hmcfcninfo;
  822. memcpy(&cqp_info->in.u.manage_hmc_pm.info, hmcfcninfo,
  823. sizeof(*hmcfcninfo));
  824. cqp_info->in.u.manage_hmc_pm.dev = dev;
  825. cqp_info->cqp_cmd = OP_MANAGE_HMC_PM_FUNC_TABLE;
  826. cqp_info->post_sq = 1;
  827. cqp_info->in.u.manage_hmc_pm.scratch = (uintptr_t)cqp_request;
  828. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  829. if (status)
  830. i40iw_pr_err("CQP-OP Manage HMC fail");
  831. return status;
  832. }
  833. /**
  834. * i40iw_cqp_query_fpm_values_cmd - send cqp command for fpm
  835. * @iwdev: function device struct
  836. * @values_mem: buffer for fpm
  837. * @hmc_fn_id: function id for fpm
  838. */
  839. enum i40iw_status_code i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev *dev,
  840. struct i40iw_dma_mem *values_mem,
  841. u8 hmc_fn_id)
  842. {
  843. enum i40iw_status_code status;
  844. struct i40iw_cqp_request *cqp_request;
  845. struct cqp_commands_info *cqp_info;
  846. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  847. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  848. if (!cqp_request)
  849. return I40IW_ERR_NO_MEMORY;
  850. cqp_info = &cqp_request->info;
  851. cqp_request->param = NULL;
  852. cqp_info->in.u.query_fpm_values.cqp = dev->cqp;
  853. cqp_info->in.u.query_fpm_values.fpm_values_pa = values_mem->pa;
  854. cqp_info->in.u.query_fpm_values.fpm_values_va = values_mem->va;
  855. cqp_info->in.u.query_fpm_values.hmc_fn_id = hmc_fn_id;
  856. cqp_info->cqp_cmd = OP_QUERY_FPM_VALUES;
  857. cqp_info->post_sq = 1;
  858. cqp_info->in.u.query_fpm_values.scratch = (uintptr_t)cqp_request;
  859. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  860. if (status)
  861. i40iw_pr_err("CQP-OP Query FPM fail");
  862. return status;
  863. }
  864. /**
  865. * i40iw_cqp_commit_fpm_values_cmd - commit fpm values in hw
  866. * @dev: hardware control device structure
  867. * @values_mem: buffer with fpm values
  868. * @hmc_fn_id: function id for fpm
  869. */
  870. enum i40iw_status_code i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev *dev,
  871. struct i40iw_dma_mem *values_mem,
  872. u8 hmc_fn_id)
  873. {
  874. enum i40iw_status_code status;
  875. struct i40iw_cqp_request *cqp_request;
  876. struct cqp_commands_info *cqp_info;
  877. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  878. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  879. if (!cqp_request)
  880. return I40IW_ERR_NO_MEMORY;
  881. cqp_info = &cqp_request->info;
  882. cqp_request->param = NULL;
  883. cqp_info->in.u.commit_fpm_values.cqp = dev->cqp;
  884. cqp_info->in.u.commit_fpm_values.fpm_values_pa = values_mem->pa;
  885. cqp_info->in.u.commit_fpm_values.fpm_values_va = values_mem->va;
  886. cqp_info->in.u.commit_fpm_values.hmc_fn_id = hmc_fn_id;
  887. cqp_info->cqp_cmd = OP_COMMIT_FPM_VALUES;
  888. cqp_info->post_sq = 1;
  889. cqp_info->in.u.commit_fpm_values.scratch = (uintptr_t)cqp_request;
  890. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  891. if (status)
  892. i40iw_pr_err("CQP-OP Commit FPM fail");
  893. return status;
  894. }
  895. /**
  896. * i40iw_vf_wait_vchnl_resp - wait for channel msg
  897. * @iwdev: function's device struct
  898. */
  899. enum i40iw_status_code i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev *dev)
  900. {
  901. struct i40iw_device *iwdev = dev->back_dev;
  902. int timeout_ret;
  903. i40iw_debug(dev, I40IW_DEBUG_VIRT, "%s[%u] dev %p, iwdev %p\n",
  904. __func__, __LINE__, dev, iwdev);
  905. atomic_set(&iwdev->vchnl_msgs, 2);
  906. timeout_ret = wait_event_timeout(iwdev->vchnl_waitq,
  907. (atomic_read(&iwdev->vchnl_msgs) == 1),
  908. I40IW_VCHNL_EVENT_TIMEOUT);
  909. atomic_dec(&iwdev->vchnl_msgs);
  910. if (!timeout_ret) {
  911. i40iw_pr_err("virt channel completion timeout = 0x%x\n", timeout_ret);
  912. atomic_set(&iwdev->vchnl_msgs, 0);
  913. dev->vchnl_up = false;
  914. return I40IW_ERR_TIMEOUT;
  915. }
  916. wake_up(&dev->vf_reqs);
  917. return 0;
  918. }
  919. /**
  920. * i40iw_ieq_mpa_crc_ae - generate AE for crc error
  921. * @dev: hardware control device structure
  922. * @qp: hardware control qp
  923. */
  924. void i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
  925. {
  926. struct i40iw_qp_flush_info info;
  927. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  928. i40iw_debug(dev, I40IW_DEBUG_AEQ, "%s entered\n", __func__);
  929. memset(&info, 0, sizeof(info));
  930. info.ae_code = I40IW_AE_LLP_RECEIVED_MPA_CRC_ERROR;
  931. info.generate_ae = true;
  932. info.ae_source = 0x3;
  933. (void)i40iw_hw_flush_wqes(iwdev, qp, &info, false);
  934. }
  935. /**
  936. * i40iw_init_hash_desc - initialize hash for crc calculation
  937. * @desc: cryption type
  938. */
  939. enum i40iw_status_code i40iw_init_hash_desc(struct shash_desc **desc)
  940. {
  941. struct crypto_shash *tfm;
  942. struct shash_desc *tdesc;
  943. tfm = crypto_alloc_shash("crc32c", 0, 0);
  944. if (IS_ERR(tfm))
  945. return I40IW_ERR_MPA_CRC;
  946. tdesc = kzalloc(sizeof(*tdesc) + crypto_shash_descsize(tfm),
  947. GFP_KERNEL);
  948. if (!tdesc) {
  949. crypto_free_shash(tfm);
  950. return I40IW_ERR_MPA_CRC;
  951. }
  952. tdesc->tfm = tfm;
  953. *desc = tdesc;
  954. return 0;
  955. }
  956. /**
  957. * i40iw_free_hash_desc - free hash desc
  958. * @desc: to be freed
  959. */
  960. void i40iw_free_hash_desc(struct shash_desc *desc)
  961. {
  962. if (desc) {
  963. crypto_free_shash(desc->tfm);
  964. kfree(desc);
  965. }
  966. }
  967. /**
  968. * i40iw_alloc_query_fpm_buf - allocate buffer for fpm
  969. * @dev: hardware control device structure
  970. * @mem: buffer ptr for fpm to be allocated
  971. * @return: memory allocation status
  972. */
  973. enum i40iw_status_code i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev *dev,
  974. struct i40iw_dma_mem *mem)
  975. {
  976. enum i40iw_status_code status;
  977. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  978. status = i40iw_obj_aligned_mem(iwdev, mem, I40IW_QUERY_FPM_BUF_SIZE,
  979. I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
  980. return status;
  981. }
  982. /**
  983. * i40iw_ieq_check_mpacrc - check if mpa crc is OK
  984. * @desc: desc for hash
  985. * @addr: address of buffer for crc
  986. * @length: length of buffer
  987. * @value: value to be compared
  988. */
  989. enum i40iw_status_code i40iw_ieq_check_mpacrc(struct shash_desc *desc,
  990. void *addr,
  991. u32 length,
  992. u32 value)
  993. {
  994. u32 crc = 0;
  995. int ret;
  996. enum i40iw_status_code ret_code = 0;
  997. crypto_shash_init(desc);
  998. ret = crypto_shash_update(desc, addr, length);
  999. if (!ret)
  1000. crypto_shash_final(desc, (u8 *)&crc);
  1001. if (crc != value) {
  1002. i40iw_pr_err("mpa crc check fail\n");
  1003. ret_code = I40IW_ERR_MPA_CRC;
  1004. }
  1005. return ret_code;
  1006. }
  1007. /**
  1008. * i40iw_ieq_get_qp - get qp based on quad in puda buffer
  1009. * @dev: hardware control device structure
  1010. * @buf: receive puda buffer on exception q
  1011. */
  1012. struct i40iw_sc_qp *i40iw_ieq_get_qp(struct i40iw_sc_dev *dev,
  1013. struct i40iw_puda_buf *buf)
  1014. {
  1015. struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
  1016. struct i40iw_qp *iwqp;
  1017. struct i40iw_cm_node *cm_node;
  1018. u32 loc_addr[4], rem_addr[4];
  1019. u16 loc_port, rem_port;
  1020. struct ipv6hdr *ip6h;
  1021. struct iphdr *iph = (struct iphdr *)buf->iph;
  1022. struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
  1023. if (iph->version == 4) {
  1024. memset(loc_addr, 0, sizeof(loc_addr));
  1025. loc_addr[0] = ntohl(iph->daddr);
  1026. memset(rem_addr, 0, sizeof(rem_addr));
  1027. rem_addr[0] = ntohl(iph->saddr);
  1028. } else {
  1029. ip6h = (struct ipv6hdr *)buf->iph;
  1030. i40iw_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
  1031. i40iw_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
  1032. }
  1033. loc_port = ntohs(tcph->dest);
  1034. rem_port = ntohs(tcph->source);
  1035. cm_node = i40iw_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
  1036. loc_addr, false);
  1037. if (!cm_node)
  1038. return NULL;
  1039. iwqp = cm_node->iwqp;
  1040. return &iwqp->sc_qp;
  1041. }
  1042. /**
  1043. * i40iw_ieq_update_tcpip_info - update tcpip in the buffer
  1044. * @buf: puda to update
  1045. * @length: length of buffer
  1046. * @seqnum: seq number for tcp
  1047. */
  1048. void i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf *buf, u16 length, u32 seqnum)
  1049. {
  1050. struct tcphdr *tcph;
  1051. struct iphdr *iph;
  1052. u16 iphlen;
  1053. u16 packetsize;
  1054. u8 *addr = (u8 *)buf->mem.va;
  1055. iphlen = (buf->ipv4) ? 20 : 40;
  1056. iph = (struct iphdr *)(addr + buf->maclen);
  1057. tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
  1058. packetsize = length + buf->tcphlen + iphlen;
  1059. iph->tot_len = htons(packetsize);
  1060. tcph->seq = htonl(seqnum);
  1061. }
  1062. /**
  1063. * i40iw_puda_get_tcpip_info - get tcpip info from puda buffer
  1064. * @info: to get information
  1065. * @buf: puda buffer
  1066. */
  1067. enum i40iw_status_code i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info *info,
  1068. struct i40iw_puda_buf *buf)
  1069. {
  1070. struct iphdr *iph;
  1071. struct ipv6hdr *ip6h;
  1072. struct tcphdr *tcph;
  1073. u16 iphlen;
  1074. u16 pkt_len;
  1075. u8 *mem = (u8 *)buf->mem.va;
  1076. struct ethhdr *ethh = (struct ethhdr *)buf->mem.va;
  1077. if (ethh->h_proto == htons(0x8100)) {
  1078. info->vlan_valid = true;
  1079. buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) & VLAN_VID_MASK;
  1080. }
  1081. buf->maclen = (info->vlan_valid) ? 18 : 14;
  1082. iphlen = (info->l3proto) ? 40 : 20;
  1083. buf->ipv4 = (info->l3proto) ? false : true;
  1084. buf->iph = mem + buf->maclen;
  1085. iph = (struct iphdr *)buf->iph;
  1086. buf->tcph = buf->iph + iphlen;
  1087. tcph = (struct tcphdr *)buf->tcph;
  1088. if (buf->ipv4) {
  1089. pkt_len = ntohs(iph->tot_len);
  1090. } else {
  1091. ip6h = (struct ipv6hdr *)buf->iph;
  1092. pkt_len = ntohs(ip6h->payload_len) + iphlen;
  1093. }
  1094. buf->totallen = pkt_len + buf->maclen;
  1095. if (info->payload_len < buf->totallen - 4) {
  1096. i40iw_pr_err("payload_len = 0x%x totallen expected0x%x\n",
  1097. info->payload_len, buf->totallen);
  1098. return I40IW_ERR_INVALID_SIZE;
  1099. }
  1100. buf->tcphlen = (tcph->doff) << 2;
  1101. buf->datalen = pkt_len - iphlen - buf->tcphlen;
  1102. buf->data = (buf->datalen) ? buf->tcph + buf->tcphlen : NULL;
  1103. buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
  1104. buf->seqnum = ntohl(tcph->seq);
  1105. return 0;
  1106. }
  1107. /**
  1108. * i40iw_hw_stats_timeout - Stats timer-handler which updates all HW stats
  1109. * @dev: hardware control device structure
  1110. */
  1111. static void i40iw_hw_stats_timeout(unsigned long dev)
  1112. {
  1113. struct i40iw_sc_dev *pf_dev = (struct i40iw_sc_dev *)dev;
  1114. struct i40iw_dev_pestat *pf_devstat = &pf_dev->dev_pestat;
  1115. struct i40iw_dev_pestat *vf_devstat = NULL;
  1116. u16 iw_vf_idx;
  1117. unsigned long flags;
  1118. /*PF*/
  1119. pf_devstat->ops.iw_hw_stat_read_all(pf_devstat, &pf_devstat->hw_stats);
  1120. for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
  1121. spin_lock_irqsave(&pf_devstat->stats_lock, flags);
  1122. if (pf_dev->vf_dev[iw_vf_idx]) {
  1123. if (pf_dev->vf_dev[iw_vf_idx]->stats_initialized) {
  1124. vf_devstat = &pf_dev->vf_dev[iw_vf_idx]->dev_pestat;
  1125. vf_devstat->ops.iw_hw_stat_read_all(vf_devstat, &vf_devstat->hw_stats);
  1126. }
  1127. }
  1128. spin_unlock_irqrestore(&pf_devstat->stats_lock, flags);
  1129. }
  1130. mod_timer(&pf_devstat->stats_timer,
  1131. jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
  1132. }
  1133. /**
  1134. * i40iw_hw_stats_start_timer - Start periodic stats timer
  1135. * @dev: hardware control device structure
  1136. */
  1137. void i40iw_hw_stats_start_timer(struct i40iw_sc_dev *dev)
  1138. {
  1139. struct i40iw_dev_pestat *devstat = &dev->dev_pestat;
  1140. init_timer(&devstat->stats_timer);
  1141. devstat->stats_timer.function = i40iw_hw_stats_timeout;
  1142. devstat->stats_timer.data = (unsigned long)dev;
  1143. mod_timer(&devstat->stats_timer,
  1144. jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
  1145. }
  1146. /**
  1147. * i40iw_hw_stats_del_timer - Delete periodic stats timer
  1148. * @dev: hardware control device structure
  1149. */
  1150. void i40iw_hw_stats_del_timer(struct i40iw_sc_dev *dev)
  1151. {
  1152. struct i40iw_dev_pestat *devstat = &dev->dev_pestat;
  1153. del_timer_sync(&devstat->stats_timer);
  1154. }