cxgbit_cm.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. /*
  2. * Copyright (c) 2016 Chelsio Communications, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/list.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/timer.h>
  13. #include <linux/notifier.h>
  14. #include <linux/inetdevice.h>
  15. #include <linux/ip.h>
  16. #include <linux/tcp.h>
  17. #include <linux/if_vlan.h>
  18. #include <net/neighbour.h>
  19. #include <net/netevent.h>
  20. #include <net/route.h>
  21. #include <net/tcp.h>
  22. #include <net/ip6_route.h>
  23. #include <net/addrconf.h>
  24. #include <libcxgb_cm.h>
  25. #include "cxgbit.h"
  26. #include "clip_tbl.h"
  27. static void cxgbit_init_wr_wait(struct cxgbit_wr_wait *wr_waitp)
  28. {
  29. wr_waitp->ret = 0;
  30. reinit_completion(&wr_waitp->completion);
  31. }
  32. static void
  33. cxgbit_wake_up(struct cxgbit_wr_wait *wr_waitp, const char *func, u8 ret)
  34. {
  35. if (ret == CPL_ERR_NONE)
  36. wr_waitp->ret = 0;
  37. else
  38. wr_waitp->ret = -EIO;
  39. if (wr_waitp->ret)
  40. pr_err("%s: err:%u", func, ret);
  41. complete(&wr_waitp->completion);
  42. }
  43. static int
  44. cxgbit_wait_for_reply(struct cxgbit_device *cdev,
  45. struct cxgbit_wr_wait *wr_waitp, u32 tid, u32 timeout,
  46. const char *func)
  47. {
  48. int ret;
  49. if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
  50. wr_waitp->ret = -EIO;
  51. goto out;
  52. }
  53. ret = wait_for_completion_timeout(&wr_waitp->completion, timeout * HZ);
  54. if (!ret) {
  55. pr_info("%s - Device %s not responding tid %u\n",
  56. func, pci_name(cdev->lldi.pdev), tid);
  57. wr_waitp->ret = -ETIMEDOUT;
  58. }
  59. out:
  60. if (wr_waitp->ret)
  61. pr_info("%s: FW reply %d tid %u\n",
  62. pci_name(cdev->lldi.pdev), wr_waitp->ret, tid);
  63. return wr_waitp->ret;
  64. }
  65. static int cxgbit_np_hashfn(const struct cxgbit_np *cnp)
  66. {
  67. return ((unsigned long)cnp >> 10) & (NP_INFO_HASH_SIZE - 1);
  68. }
  69. static struct np_info *
  70. cxgbit_np_hash_add(struct cxgbit_device *cdev, struct cxgbit_np *cnp,
  71. unsigned int stid)
  72. {
  73. struct np_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
  74. if (p) {
  75. int bucket = cxgbit_np_hashfn(cnp);
  76. p->cnp = cnp;
  77. p->stid = stid;
  78. spin_lock(&cdev->np_lock);
  79. p->next = cdev->np_hash_tab[bucket];
  80. cdev->np_hash_tab[bucket] = p;
  81. spin_unlock(&cdev->np_lock);
  82. }
  83. return p;
  84. }
  85. static int
  86. cxgbit_np_hash_find(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
  87. {
  88. int stid = -1, bucket = cxgbit_np_hashfn(cnp);
  89. struct np_info *p;
  90. spin_lock(&cdev->np_lock);
  91. for (p = cdev->np_hash_tab[bucket]; p; p = p->next) {
  92. if (p->cnp == cnp) {
  93. stid = p->stid;
  94. break;
  95. }
  96. }
  97. spin_unlock(&cdev->np_lock);
  98. return stid;
  99. }
  100. static int cxgbit_np_hash_del(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
  101. {
  102. int stid = -1, bucket = cxgbit_np_hashfn(cnp);
  103. struct np_info *p, **prev = &cdev->np_hash_tab[bucket];
  104. spin_lock(&cdev->np_lock);
  105. for (p = *prev; p; prev = &p->next, p = p->next) {
  106. if (p->cnp == cnp) {
  107. stid = p->stid;
  108. *prev = p->next;
  109. kfree(p);
  110. break;
  111. }
  112. }
  113. spin_unlock(&cdev->np_lock);
  114. return stid;
  115. }
  116. void _cxgbit_free_cnp(struct kref *kref)
  117. {
  118. struct cxgbit_np *cnp;
  119. cnp = container_of(kref, struct cxgbit_np, kref);
  120. kfree(cnp);
  121. }
  122. static int
  123. cxgbit_create_server6(struct cxgbit_device *cdev, unsigned int stid,
  124. struct cxgbit_np *cnp)
  125. {
  126. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
  127. &cnp->com.local_addr;
  128. int addr_type;
  129. int ret;
  130. pr_debug("%s: dev = %s; stid = %u; sin6_port = %u\n",
  131. __func__, cdev->lldi.ports[0]->name, stid, sin6->sin6_port);
  132. addr_type = ipv6_addr_type((const struct in6_addr *)
  133. &sin6->sin6_addr);
  134. if (addr_type != IPV6_ADDR_ANY) {
  135. ret = cxgb4_clip_get(cdev->lldi.ports[0],
  136. (const u32 *)&sin6->sin6_addr.s6_addr, 1);
  137. if (ret) {
  138. pr_err("Unable to find clip table entry. laddr %pI6. Error:%d.\n",
  139. sin6->sin6_addr.s6_addr, ret);
  140. return -ENOMEM;
  141. }
  142. }
  143. cxgbit_get_cnp(cnp);
  144. cxgbit_init_wr_wait(&cnp->com.wr_wait);
  145. ret = cxgb4_create_server6(cdev->lldi.ports[0],
  146. stid, &sin6->sin6_addr,
  147. sin6->sin6_port,
  148. cdev->lldi.rxq_ids[0]);
  149. if (!ret)
  150. ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
  151. 0, 10, __func__);
  152. else if (ret > 0)
  153. ret = net_xmit_errno(ret);
  154. else
  155. cxgbit_put_cnp(cnp);
  156. if (ret) {
  157. if (ret != -ETIMEDOUT)
  158. cxgb4_clip_release(cdev->lldi.ports[0],
  159. (const u32 *)&sin6->sin6_addr.s6_addr, 1);
  160. pr_err("create server6 err %d stid %d laddr %pI6 lport %d\n",
  161. ret, stid, sin6->sin6_addr.s6_addr,
  162. ntohs(sin6->sin6_port));
  163. }
  164. return ret;
  165. }
  166. static int
  167. cxgbit_create_server4(struct cxgbit_device *cdev, unsigned int stid,
  168. struct cxgbit_np *cnp)
  169. {
  170. struct sockaddr_in *sin = (struct sockaddr_in *)
  171. &cnp->com.local_addr;
  172. int ret;
  173. pr_debug("%s: dev = %s; stid = %u; sin_port = %u\n",
  174. __func__, cdev->lldi.ports[0]->name, stid, sin->sin_port);
  175. cxgbit_get_cnp(cnp);
  176. cxgbit_init_wr_wait(&cnp->com.wr_wait);
  177. ret = cxgb4_create_server(cdev->lldi.ports[0],
  178. stid, sin->sin_addr.s_addr,
  179. sin->sin_port, 0,
  180. cdev->lldi.rxq_ids[0]);
  181. if (!ret)
  182. ret = cxgbit_wait_for_reply(cdev,
  183. &cnp->com.wr_wait,
  184. 0, 10, __func__);
  185. else if (ret > 0)
  186. ret = net_xmit_errno(ret);
  187. else
  188. cxgbit_put_cnp(cnp);
  189. if (ret)
  190. pr_err("create server failed err %d stid %d laddr %pI4 lport %d\n",
  191. ret, stid, &sin->sin_addr, ntohs(sin->sin_port));
  192. return ret;
  193. }
  194. struct cxgbit_device *cxgbit_find_device(struct net_device *ndev, u8 *port_id)
  195. {
  196. struct cxgbit_device *cdev;
  197. u8 i;
  198. list_for_each_entry(cdev, &cdev_list_head, list) {
  199. struct cxgb4_lld_info *lldi = &cdev->lldi;
  200. for (i = 0; i < lldi->nports; i++) {
  201. if (lldi->ports[i] == ndev) {
  202. if (port_id)
  203. *port_id = i;
  204. return cdev;
  205. }
  206. }
  207. }
  208. return NULL;
  209. }
  210. static struct net_device *cxgbit_get_real_dev(struct net_device *ndev)
  211. {
  212. if (ndev->priv_flags & IFF_BONDING) {
  213. pr_err("Bond devices are not supported. Interface:%s\n",
  214. ndev->name);
  215. return NULL;
  216. }
  217. if (is_vlan_dev(ndev))
  218. return vlan_dev_real_dev(ndev);
  219. return ndev;
  220. }
  221. static struct net_device *cxgbit_ipv4_netdev(__be32 saddr)
  222. {
  223. struct net_device *ndev;
  224. ndev = __ip_dev_find(&init_net, saddr, false);
  225. if (!ndev)
  226. return NULL;
  227. return cxgbit_get_real_dev(ndev);
  228. }
  229. static struct net_device *cxgbit_ipv6_netdev(struct in6_addr *addr6)
  230. {
  231. struct net_device *ndev = NULL;
  232. bool found = false;
  233. if (IS_ENABLED(CONFIG_IPV6)) {
  234. for_each_netdev_rcu(&init_net, ndev)
  235. if (ipv6_chk_addr(&init_net, addr6, ndev, 1)) {
  236. found = true;
  237. break;
  238. }
  239. }
  240. if (!found)
  241. return NULL;
  242. return cxgbit_get_real_dev(ndev);
  243. }
  244. static struct cxgbit_device *cxgbit_find_np_cdev(struct cxgbit_np *cnp)
  245. {
  246. struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
  247. int ss_family = sockaddr->ss_family;
  248. struct net_device *ndev = NULL;
  249. struct cxgbit_device *cdev = NULL;
  250. rcu_read_lock();
  251. if (ss_family == AF_INET) {
  252. struct sockaddr_in *sin;
  253. sin = (struct sockaddr_in *)sockaddr;
  254. ndev = cxgbit_ipv4_netdev(sin->sin_addr.s_addr);
  255. } else if (ss_family == AF_INET6) {
  256. struct sockaddr_in6 *sin6;
  257. sin6 = (struct sockaddr_in6 *)sockaddr;
  258. ndev = cxgbit_ipv6_netdev(&sin6->sin6_addr);
  259. }
  260. if (!ndev)
  261. goto out;
  262. cdev = cxgbit_find_device(ndev, NULL);
  263. out:
  264. rcu_read_unlock();
  265. return cdev;
  266. }
  267. static bool cxgbit_inaddr_any(struct cxgbit_np *cnp)
  268. {
  269. struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
  270. int ss_family = sockaddr->ss_family;
  271. int addr_type;
  272. if (ss_family == AF_INET) {
  273. struct sockaddr_in *sin;
  274. sin = (struct sockaddr_in *)sockaddr;
  275. if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
  276. return true;
  277. } else if (ss_family == AF_INET6) {
  278. struct sockaddr_in6 *sin6;
  279. sin6 = (struct sockaddr_in6 *)sockaddr;
  280. addr_type = ipv6_addr_type((const struct in6_addr *)
  281. &sin6->sin6_addr);
  282. if (addr_type == IPV6_ADDR_ANY)
  283. return true;
  284. }
  285. return false;
  286. }
  287. static int
  288. __cxgbit_setup_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
  289. {
  290. int stid, ret;
  291. int ss_family = cnp->com.local_addr.ss_family;
  292. if (!test_bit(CDEV_STATE_UP, &cdev->flags))
  293. return -EINVAL;
  294. stid = cxgb4_alloc_stid(cdev->lldi.tids, ss_family, cnp);
  295. if (stid < 0)
  296. return -EINVAL;
  297. if (!cxgbit_np_hash_add(cdev, cnp, stid)) {
  298. cxgb4_free_stid(cdev->lldi.tids, stid, ss_family);
  299. return -EINVAL;
  300. }
  301. if (ss_family == AF_INET)
  302. ret = cxgbit_create_server4(cdev, stid, cnp);
  303. else
  304. ret = cxgbit_create_server6(cdev, stid, cnp);
  305. if (ret) {
  306. if (ret != -ETIMEDOUT)
  307. cxgb4_free_stid(cdev->lldi.tids, stid,
  308. ss_family);
  309. cxgbit_np_hash_del(cdev, cnp);
  310. return ret;
  311. }
  312. return ret;
  313. }
  314. static int cxgbit_setup_cdev_np(struct cxgbit_np *cnp)
  315. {
  316. struct cxgbit_device *cdev;
  317. int ret = -1;
  318. mutex_lock(&cdev_list_lock);
  319. cdev = cxgbit_find_np_cdev(cnp);
  320. if (!cdev)
  321. goto out;
  322. if (cxgbit_np_hash_find(cdev, cnp) >= 0)
  323. goto out;
  324. if (__cxgbit_setup_cdev_np(cdev, cnp))
  325. goto out;
  326. cnp->com.cdev = cdev;
  327. ret = 0;
  328. out:
  329. mutex_unlock(&cdev_list_lock);
  330. return ret;
  331. }
  332. static int cxgbit_setup_all_np(struct cxgbit_np *cnp)
  333. {
  334. struct cxgbit_device *cdev;
  335. int ret;
  336. u32 count = 0;
  337. mutex_lock(&cdev_list_lock);
  338. list_for_each_entry(cdev, &cdev_list_head, list) {
  339. if (cxgbit_np_hash_find(cdev, cnp) >= 0) {
  340. mutex_unlock(&cdev_list_lock);
  341. return -1;
  342. }
  343. }
  344. list_for_each_entry(cdev, &cdev_list_head, list) {
  345. ret = __cxgbit_setup_cdev_np(cdev, cnp);
  346. if (ret == -ETIMEDOUT)
  347. break;
  348. if (ret != 0)
  349. continue;
  350. count++;
  351. }
  352. mutex_unlock(&cdev_list_lock);
  353. return count ? 0 : -1;
  354. }
  355. int cxgbit_setup_np(struct iscsi_np *np, struct sockaddr_storage *ksockaddr)
  356. {
  357. struct cxgbit_np *cnp;
  358. int ret;
  359. if ((ksockaddr->ss_family != AF_INET) &&
  360. (ksockaddr->ss_family != AF_INET6))
  361. return -EINVAL;
  362. cnp = kzalloc(sizeof(*cnp), GFP_KERNEL);
  363. if (!cnp)
  364. return -ENOMEM;
  365. init_waitqueue_head(&cnp->accept_wait);
  366. init_completion(&cnp->com.wr_wait.completion);
  367. init_completion(&cnp->accept_comp);
  368. INIT_LIST_HEAD(&cnp->np_accept_list);
  369. spin_lock_init(&cnp->np_accept_lock);
  370. kref_init(&cnp->kref);
  371. memcpy(&np->np_sockaddr, ksockaddr,
  372. sizeof(struct sockaddr_storage));
  373. memcpy(&cnp->com.local_addr, &np->np_sockaddr,
  374. sizeof(cnp->com.local_addr));
  375. cnp->np = np;
  376. cnp->com.cdev = NULL;
  377. if (cxgbit_inaddr_any(cnp))
  378. ret = cxgbit_setup_all_np(cnp);
  379. else
  380. ret = cxgbit_setup_cdev_np(cnp);
  381. if (ret) {
  382. cxgbit_put_cnp(cnp);
  383. return -EINVAL;
  384. }
  385. np->np_context = cnp;
  386. cnp->com.state = CSK_STATE_LISTEN;
  387. return 0;
  388. }
  389. static void
  390. cxgbit_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
  391. struct cxgbit_sock *csk)
  392. {
  393. conn->login_family = np->np_sockaddr.ss_family;
  394. conn->login_sockaddr = csk->com.remote_addr;
  395. conn->local_sockaddr = csk->com.local_addr;
  396. }
  397. int cxgbit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
  398. {
  399. struct cxgbit_np *cnp = np->np_context;
  400. struct cxgbit_sock *csk;
  401. int ret = 0;
  402. accept_wait:
  403. ret = wait_for_completion_interruptible(&cnp->accept_comp);
  404. if (ret)
  405. return -ENODEV;
  406. spin_lock_bh(&np->np_thread_lock);
  407. if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
  408. spin_unlock_bh(&np->np_thread_lock);
  409. /**
  410. * No point in stalling here when np_thread
  411. * is in state RESET/SHUTDOWN/EXIT - bail
  412. **/
  413. return -ENODEV;
  414. }
  415. spin_unlock_bh(&np->np_thread_lock);
  416. spin_lock_bh(&cnp->np_accept_lock);
  417. if (list_empty(&cnp->np_accept_list)) {
  418. spin_unlock_bh(&cnp->np_accept_lock);
  419. goto accept_wait;
  420. }
  421. csk = list_first_entry(&cnp->np_accept_list,
  422. struct cxgbit_sock,
  423. accept_node);
  424. list_del_init(&csk->accept_node);
  425. spin_unlock_bh(&cnp->np_accept_lock);
  426. conn->context = csk;
  427. csk->conn = conn;
  428. cxgbit_set_conn_info(np, conn, csk);
  429. return 0;
  430. }
  431. static int
  432. __cxgbit_free_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
  433. {
  434. int stid, ret;
  435. bool ipv6 = false;
  436. stid = cxgbit_np_hash_del(cdev, cnp);
  437. if (stid < 0)
  438. return -EINVAL;
  439. if (!test_bit(CDEV_STATE_UP, &cdev->flags))
  440. return -EINVAL;
  441. if (cnp->np->np_sockaddr.ss_family == AF_INET6)
  442. ipv6 = true;
  443. cxgbit_get_cnp(cnp);
  444. cxgbit_init_wr_wait(&cnp->com.wr_wait);
  445. ret = cxgb4_remove_server(cdev->lldi.ports[0], stid,
  446. cdev->lldi.rxq_ids[0], ipv6);
  447. if (ret > 0)
  448. ret = net_xmit_errno(ret);
  449. if (ret) {
  450. cxgbit_put_cnp(cnp);
  451. return ret;
  452. }
  453. ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
  454. 0, 10, __func__);
  455. if (ret == -ETIMEDOUT)
  456. return ret;
  457. if (ipv6 && cnp->com.cdev) {
  458. struct sockaddr_in6 *sin6;
  459. sin6 = (struct sockaddr_in6 *)&cnp->com.local_addr;
  460. cxgb4_clip_release(cdev->lldi.ports[0],
  461. (const u32 *)&sin6->sin6_addr.s6_addr,
  462. 1);
  463. }
  464. cxgb4_free_stid(cdev->lldi.tids, stid,
  465. cnp->com.local_addr.ss_family);
  466. return 0;
  467. }
  468. static void cxgbit_free_all_np(struct cxgbit_np *cnp)
  469. {
  470. struct cxgbit_device *cdev;
  471. int ret;
  472. mutex_lock(&cdev_list_lock);
  473. list_for_each_entry(cdev, &cdev_list_head, list) {
  474. ret = __cxgbit_free_cdev_np(cdev, cnp);
  475. if (ret == -ETIMEDOUT)
  476. break;
  477. }
  478. mutex_unlock(&cdev_list_lock);
  479. }
  480. static void cxgbit_free_cdev_np(struct cxgbit_np *cnp)
  481. {
  482. struct cxgbit_device *cdev;
  483. bool found = false;
  484. mutex_lock(&cdev_list_lock);
  485. list_for_each_entry(cdev, &cdev_list_head, list) {
  486. if (cdev == cnp->com.cdev) {
  487. found = true;
  488. break;
  489. }
  490. }
  491. if (!found)
  492. goto out;
  493. __cxgbit_free_cdev_np(cdev, cnp);
  494. out:
  495. mutex_unlock(&cdev_list_lock);
  496. }
  497. void cxgbit_free_np(struct iscsi_np *np)
  498. {
  499. struct cxgbit_np *cnp = np->np_context;
  500. cnp->com.state = CSK_STATE_DEAD;
  501. if (cnp->com.cdev)
  502. cxgbit_free_cdev_np(cnp);
  503. else
  504. cxgbit_free_all_np(cnp);
  505. np->np_context = NULL;
  506. cxgbit_put_cnp(cnp);
  507. }
  508. static void cxgbit_send_halfclose(struct cxgbit_sock *csk)
  509. {
  510. struct sk_buff *skb;
  511. u32 len = roundup(sizeof(struct cpl_close_con_req), 16);
  512. skb = alloc_skb(len, GFP_ATOMIC);
  513. if (!skb)
  514. return;
  515. cxgb_mk_close_con_req(skb, len, csk->tid, csk->txq_idx,
  516. NULL, NULL);
  517. cxgbit_skcb_flags(skb) |= SKCBF_TX_FLAG_COMPL;
  518. __skb_queue_tail(&csk->txq, skb);
  519. cxgbit_push_tx_frames(csk);
  520. }
  521. static void cxgbit_arp_failure_discard(void *handle, struct sk_buff *skb)
  522. {
  523. pr_debug("%s cxgbit_device %p\n", __func__, handle);
  524. kfree_skb(skb);
  525. }
  526. static void cxgbit_abort_arp_failure(void *handle, struct sk_buff *skb)
  527. {
  528. struct cxgbit_device *cdev = handle;
  529. struct cpl_abort_req *req = cplhdr(skb);
  530. pr_debug("%s cdev %p\n", __func__, cdev);
  531. req->cmd = CPL_ABORT_NO_RST;
  532. cxgbit_ofld_send(cdev, skb);
  533. }
  534. static int cxgbit_send_abort_req(struct cxgbit_sock *csk)
  535. {
  536. struct sk_buff *skb;
  537. u32 len = roundup(sizeof(struct cpl_abort_req), 16);
  538. pr_debug("%s: csk %p tid %u; state %d\n",
  539. __func__, csk, csk->tid, csk->com.state);
  540. __skb_queue_purge(&csk->txq);
  541. if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
  542. cxgbit_send_tx_flowc_wr(csk);
  543. skb = __skb_dequeue(&csk->skbq);
  544. cxgb_mk_abort_req(skb, len, csk->tid, csk->txq_idx,
  545. csk->com.cdev, cxgbit_abort_arp_failure);
  546. return cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
  547. }
  548. void cxgbit_free_conn(struct iscsi_conn *conn)
  549. {
  550. struct cxgbit_sock *csk = conn->context;
  551. bool release = false;
  552. pr_debug("%s: state %d\n",
  553. __func__, csk->com.state);
  554. spin_lock_bh(&csk->lock);
  555. switch (csk->com.state) {
  556. case CSK_STATE_ESTABLISHED:
  557. if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
  558. csk->com.state = CSK_STATE_CLOSING;
  559. cxgbit_send_halfclose(csk);
  560. } else {
  561. csk->com.state = CSK_STATE_ABORTING;
  562. cxgbit_send_abort_req(csk);
  563. }
  564. break;
  565. case CSK_STATE_CLOSING:
  566. csk->com.state = CSK_STATE_MORIBUND;
  567. cxgbit_send_halfclose(csk);
  568. break;
  569. case CSK_STATE_DEAD:
  570. release = true;
  571. break;
  572. default:
  573. pr_err("%s: csk %p; state %d\n",
  574. __func__, csk, csk->com.state);
  575. }
  576. spin_unlock_bh(&csk->lock);
  577. if (release)
  578. cxgbit_put_csk(csk);
  579. }
  580. static void cxgbit_set_emss(struct cxgbit_sock *csk, u16 opt)
  581. {
  582. csk->emss = csk->com.cdev->lldi.mtus[TCPOPT_MSS_G(opt)] -
  583. ((csk->com.remote_addr.ss_family == AF_INET) ?
  584. sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
  585. sizeof(struct tcphdr);
  586. csk->mss = csk->emss;
  587. if (TCPOPT_TSTAMP_G(opt))
  588. csk->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
  589. if (csk->emss < 128)
  590. csk->emss = 128;
  591. if (csk->emss & 7)
  592. pr_info("Warning: misaligned mtu idx %u mss %u emss=%u\n",
  593. TCPOPT_MSS_G(opt), csk->mss, csk->emss);
  594. pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
  595. csk->mss, csk->emss);
  596. }
  597. static void cxgbit_free_skb(struct cxgbit_sock *csk)
  598. {
  599. struct sk_buff *skb;
  600. __skb_queue_purge(&csk->txq);
  601. __skb_queue_purge(&csk->rxq);
  602. __skb_queue_purge(&csk->backlogq);
  603. __skb_queue_purge(&csk->ppodq);
  604. __skb_queue_purge(&csk->skbq);
  605. while ((skb = cxgbit_sock_dequeue_wr(csk)))
  606. kfree_skb(skb);
  607. __kfree_skb(csk->lro_hskb);
  608. }
  609. void _cxgbit_free_csk(struct kref *kref)
  610. {
  611. struct cxgbit_sock *csk;
  612. struct cxgbit_device *cdev;
  613. csk = container_of(kref, struct cxgbit_sock, kref);
  614. pr_debug("%s csk %p state %d\n", __func__, csk, csk->com.state);
  615. if (csk->com.local_addr.ss_family == AF_INET6) {
  616. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
  617. &csk->com.local_addr;
  618. cxgb4_clip_release(csk->com.cdev->lldi.ports[0],
  619. (const u32 *)
  620. &sin6->sin6_addr.s6_addr, 1);
  621. }
  622. cxgb4_remove_tid(csk->com.cdev->lldi.tids, 0, csk->tid,
  623. csk->com.local_addr.ss_family);
  624. dst_release(csk->dst);
  625. cxgb4_l2t_release(csk->l2t);
  626. cdev = csk->com.cdev;
  627. spin_lock_bh(&cdev->cskq.lock);
  628. list_del(&csk->list);
  629. spin_unlock_bh(&cdev->cskq.lock);
  630. cxgbit_free_skb(csk);
  631. cxgbit_put_cdev(cdev);
  632. kfree(csk);
  633. }
  634. static void cxgbit_set_tcp_window(struct cxgbit_sock *csk, struct port_info *pi)
  635. {
  636. unsigned int linkspeed;
  637. u8 scale;
  638. linkspeed = pi->link_cfg.speed;
  639. scale = linkspeed / SPEED_10000;
  640. #define CXGBIT_10G_RCV_WIN (256 * 1024)
  641. csk->rcv_win = CXGBIT_10G_RCV_WIN;
  642. if (scale)
  643. csk->rcv_win *= scale;
  644. #define CXGBIT_10G_SND_WIN (256 * 1024)
  645. csk->snd_win = CXGBIT_10G_SND_WIN;
  646. if (scale)
  647. csk->snd_win *= scale;
  648. pr_debug("%s snd_win %d rcv_win %d\n",
  649. __func__, csk->snd_win, csk->rcv_win);
  650. }
  651. #ifdef CONFIG_CHELSIO_T4_DCB
  652. static u8 cxgbit_get_iscsi_dcb_state(struct net_device *ndev)
  653. {
  654. return ndev->dcbnl_ops->getstate(ndev);
  655. }
  656. static int cxgbit_select_priority(int pri_mask)
  657. {
  658. if (!pri_mask)
  659. return 0;
  660. return (ffs(pri_mask) - 1);
  661. }
  662. static u8 cxgbit_get_iscsi_dcb_priority(struct net_device *ndev, u16 local_port)
  663. {
  664. int ret;
  665. u8 caps;
  666. struct dcb_app iscsi_dcb_app = {
  667. .protocol = local_port
  668. };
  669. ret = (int)ndev->dcbnl_ops->getcap(ndev, DCB_CAP_ATTR_DCBX, &caps);
  670. if (ret)
  671. return 0;
  672. if (caps & DCB_CAP_DCBX_VER_IEEE) {
  673. iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_ANY;
  674. ret = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
  675. } else if (caps & DCB_CAP_DCBX_VER_CEE) {
  676. iscsi_dcb_app.selector = DCB_APP_IDTYPE_PORTNUM;
  677. ret = dcb_getapp(ndev, &iscsi_dcb_app);
  678. }
  679. pr_info("iSCSI priority is set to %u\n", cxgbit_select_priority(ret));
  680. return cxgbit_select_priority(ret);
  681. }
  682. #endif
  683. static int
  684. cxgbit_offload_init(struct cxgbit_sock *csk, int iptype, __u8 *peer_ip,
  685. u16 local_port, struct dst_entry *dst,
  686. struct cxgbit_device *cdev)
  687. {
  688. struct neighbour *n;
  689. int ret, step;
  690. struct net_device *ndev;
  691. u16 rxq_idx, port_id;
  692. #ifdef CONFIG_CHELSIO_T4_DCB
  693. u8 priority = 0;
  694. #endif
  695. n = dst_neigh_lookup(dst, peer_ip);
  696. if (!n)
  697. return -ENODEV;
  698. rcu_read_lock();
  699. ret = -ENOMEM;
  700. if (n->dev->flags & IFF_LOOPBACK) {
  701. if (iptype == 4)
  702. ndev = cxgbit_ipv4_netdev(*(__be32 *)peer_ip);
  703. else if (IS_ENABLED(CONFIG_IPV6))
  704. ndev = cxgbit_ipv6_netdev((struct in6_addr *)peer_ip);
  705. else
  706. ndev = NULL;
  707. if (!ndev) {
  708. ret = -ENODEV;
  709. goto out;
  710. }
  711. csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t,
  712. n, ndev, 0);
  713. if (!csk->l2t)
  714. goto out;
  715. csk->mtu = ndev->mtu;
  716. csk->tx_chan = cxgb4_port_chan(ndev);
  717. csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
  718. cxgb4_port_viid(ndev));
  719. step = cdev->lldi.ntxq /
  720. cdev->lldi.nchan;
  721. csk->txq_idx = cxgb4_port_idx(ndev) * step;
  722. step = cdev->lldi.nrxq /
  723. cdev->lldi.nchan;
  724. csk->ctrlq_idx = cxgb4_port_idx(ndev);
  725. csk->rss_qid = cdev->lldi.rxq_ids[
  726. cxgb4_port_idx(ndev) * step];
  727. csk->port_id = cxgb4_port_idx(ndev);
  728. cxgbit_set_tcp_window(csk,
  729. (struct port_info *)netdev_priv(ndev));
  730. } else {
  731. ndev = cxgbit_get_real_dev(n->dev);
  732. if (!ndev) {
  733. ret = -ENODEV;
  734. goto out;
  735. }
  736. #ifdef CONFIG_CHELSIO_T4_DCB
  737. if (cxgbit_get_iscsi_dcb_state(ndev))
  738. priority = cxgbit_get_iscsi_dcb_priority(ndev,
  739. local_port);
  740. csk->dcb_priority = priority;
  741. csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, priority);
  742. #else
  743. csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, 0);
  744. #endif
  745. if (!csk->l2t)
  746. goto out;
  747. port_id = cxgb4_port_idx(ndev);
  748. csk->mtu = dst_mtu(dst);
  749. csk->tx_chan = cxgb4_port_chan(ndev);
  750. csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
  751. cxgb4_port_viid(ndev));
  752. step = cdev->lldi.ntxq /
  753. cdev->lldi.nports;
  754. csk->txq_idx = (port_id * step) +
  755. (cdev->selectq[port_id][0]++ % step);
  756. csk->ctrlq_idx = cxgb4_port_idx(ndev);
  757. step = cdev->lldi.nrxq /
  758. cdev->lldi.nports;
  759. rxq_idx = (port_id * step) +
  760. (cdev->selectq[port_id][1]++ % step);
  761. csk->rss_qid = cdev->lldi.rxq_ids[rxq_idx];
  762. csk->port_id = port_id;
  763. cxgbit_set_tcp_window(csk,
  764. (struct port_info *)netdev_priv(ndev));
  765. }
  766. ret = 0;
  767. out:
  768. rcu_read_unlock();
  769. neigh_release(n);
  770. return ret;
  771. }
  772. int cxgbit_ofld_send(struct cxgbit_device *cdev, struct sk_buff *skb)
  773. {
  774. int ret = 0;
  775. if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
  776. kfree_skb(skb);
  777. pr_err("%s - device not up - dropping\n", __func__);
  778. return -EIO;
  779. }
  780. ret = cxgb4_ofld_send(cdev->lldi.ports[0], skb);
  781. if (ret < 0)
  782. kfree_skb(skb);
  783. return ret < 0 ? ret : 0;
  784. }
  785. static void cxgbit_release_tid(struct cxgbit_device *cdev, u32 tid)
  786. {
  787. u32 len = roundup(sizeof(struct cpl_tid_release), 16);
  788. struct sk_buff *skb;
  789. skb = alloc_skb(len, GFP_ATOMIC);
  790. if (!skb)
  791. return;
  792. cxgb_mk_tid_release(skb, len, tid, 0);
  793. cxgbit_ofld_send(cdev, skb);
  794. }
  795. int
  796. cxgbit_l2t_send(struct cxgbit_device *cdev, struct sk_buff *skb,
  797. struct l2t_entry *l2e)
  798. {
  799. int ret = 0;
  800. if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
  801. kfree_skb(skb);
  802. pr_err("%s - device not up - dropping\n", __func__);
  803. return -EIO;
  804. }
  805. ret = cxgb4_l2t_send(cdev->lldi.ports[0], skb, l2e);
  806. if (ret < 0)
  807. kfree_skb(skb);
  808. return ret < 0 ? ret : 0;
  809. }
  810. static void cxgbit_send_rx_credits(struct cxgbit_sock *csk, struct sk_buff *skb)
  811. {
  812. if (csk->com.state != CSK_STATE_ESTABLISHED) {
  813. __kfree_skb(skb);
  814. return;
  815. }
  816. cxgbit_ofld_send(csk->com.cdev, skb);
  817. }
  818. /*
  819. * CPL connection rx data ack: host ->
  820. * Send RX credits through an RX_DATA_ACK CPL message.
  821. * Returns the number of credits sent.
  822. */
  823. int cxgbit_rx_data_ack(struct cxgbit_sock *csk)
  824. {
  825. struct sk_buff *skb;
  826. u32 len = roundup(sizeof(struct cpl_rx_data_ack), 16);
  827. u32 credit_dack;
  828. skb = alloc_skb(len, GFP_KERNEL);
  829. if (!skb)
  830. return -1;
  831. credit_dack = RX_DACK_CHANGE_F | RX_DACK_MODE_V(1) |
  832. RX_CREDITS_V(csk->rx_credits);
  833. cxgb_mk_rx_data_ack(skb, len, csk->tid, csk->ctrlq_idx,
  834. credit_dack);
  835. csk->rx_credits = 0;
  836. spin_lock_bh(&csk->lock);
  837. if (csk->lock_owner) {
  838. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_send_rx_credits;
  839. __skb_queue_tail(&csk->backlogq, skb);
  840. spin_unlock_bh(&csk->lock);
  841. return 0;
  842. }
  843. cxgbit_send_rx_credits(csk, skb);
  844. spin_unlock_bh(&csk->lock);
  845. return 0;
  846. }
  847. #define FLOWC_WR_NPARAMS_MIN 9
  848. #define FLOWC_WR_NPARAMS_MAX 11
  849. static int cxgbit_alloc_csk_skb(struct cxgbit_sock *csk)
  850. {
  851. struct sk_buff *skb;
  852. u32 len, flowclen;
  853. u8 i;
  854. flowclen = offsetof(struct fw_flowc_wr,
  855. mnemval[FLOWC_WR_NPARAMS_MAX]);
  856. len = max_t(u32, sizeof(struct cpl_abort_req),
  857. sizeof(struct cpl_abort_rpl));
  858. len = max(len, flowclen);
  859. len = roundup(len, 16);
  860. for (i = 0; i < 3; i++) {
  861. skb = alloc_skb(len, GFP_ATOMIC);
  862. if (!skb)
  863. goto out;
  864. __skb_queue_tail(&csk->skbq, skb);
  865. }
  866. skb = alloc_skb(LRO_SKB_MIN_HEADROOM, GFP_ATOMIC);
  867. if (!skb)
  868. goto out;
  869. memset(skb->data, 0, LRO_SKB_MIN_HEADROOM);
  870. csk->lro_hskb = skb;
  871. return 0;
  872. out:
  873. __skb_queue_purge(&csk->skbq);
  874. return -ENOMEM;
  875. }
  876. static void
  877. cxgbit_pass_accept_rpl(struct cxgbit_sock *csk, struct cpl_pass_accept_req *req)
  878. {
  879. struct sk_buff *skb;
  880. const struct tcphdr *tcph;
  881. struct cpl_t5_pass_accept_rpl *rpl5;
  882. struct cxgb4_lld_info *lldi = &csk->com.cdev->lldi;
  883. unsigned int len = roundup(sizeof(*rpl5), 16);
  884. unsigned int mtu_idx;
  885. u64 opt0;
  886. u32 opt2, hlen;
  887. u32 wscale;
  888. u32 win;
  889. pr_debug("%s csk %p tid %u\n", __func__, csk, csk->tid);
  890. skb = alloc_skb(len, GFP_ATOMIC);
  891. if (!skb) {
  892. cxgbit_put_csk(csk);
  893. return;
  894. }
  895. rpl5 = __skb_put(skb, len);
  896. memset(rpl5, 0, len);
  897. INIT_TP_WR(rpl5, csk->tid);
  898. OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
  899. csk->tid));
  900. cxgb_best_mtu(csk->com.cdev->lldi.mtus, csk->mtu, &mtu_idx,
  901. req->tcpopt.tstamp,
  902. (csk->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
  903. wscale = cxgb_compute_wscale(csk->rcv_win);
  904. /*
  905. * Specify the largest window that will fit in opt0. The
  906. * remainder will be specified in the rx_data_ack.
  907. */
  908. win = csk->rcv_win >> 10;
  909. if (win > RCV_BUFSIZ_M)
  910. win = RCV_BUFSIZ_M;
  911. opt0 = TCAM_BYPASS_F |
  912. WND_SCALE_V(wscale) |
  913. MSS_IDX_V(mtu_idx) |
  914. L2T_IDX_V(csk->l2t->idx) |
  915. TX_CHAN_V(csk->tx_chan) |
  916. SMAC_SEL_V(csk->smac_idx) |
  917. DSCP_V(csk->tos >> 2) |
  918. ULP_MODE_V(ULP_MODE_ISCSI) |
  919. RCV_BUFSIZ_V(win);
  920. opt2 = RX_CHANNEL_V(0) |
  921. RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
  922. if (!is_t5(lldi->adapter_type))
  923. opt2 |= RX_FC_DISABLE_F;
  924. if (req->tcpopt.tstamp)
  925. opt2 |= TSTAMPS_EN_F;
  926. if (req->tcpopt.sack)
  927. opt2 |= SACK_EN_F;
  928. if (wscale)
  929. opt2 |= WND_SCALE_EN_F;
  930. hlen = ntohl(req->hdr_len);
  931. if (is_t5(lldi->adapter_type))
  932. tcph = (struct tcphdr *)((u8 *)(req + 1) +
  933. ETH_HDR_LEN_G(hlen) + IP_HDR_LEN_G(hlen));
  934. else
  935. tcph = (struct tcphdr *)((u8 *)(req + 1) +
  936. T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
  937. if (tcph->ece && tcph->cwr)
  938. opt2 |= CCTRL_ECN_V(1);
  939. opt2 |= RX_COALESCE_V(3);
  940. opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
  941. opt2 |= T5_ISS_F;
  942. rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
  943. opt2 |= T5_OPT_2_VALID_F;
  944. rpl5->opt0 = cpu_to_be64(opt0);
  945. rpl5->opt2 = cpu_to_be32(opt2);
  946. set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->ctrlq_idx);
  947. t4_set_arp_err_handler(skb, NULL, cxgbit_arp_failure_discard);
  948. cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
  949. }
  950. static void
  951. cxgbit_pass_accept_req(struct cxgbit_device *cdev, struct sk_buff *skb)
  952. {
  953. struct cxgbit_sock *csk = NULL;
  954. struct cxgbit_np *cnp;
  955. struct cpl_pass_accept_req *req = cplhdr(skb);
  956. unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
  957. struct tid_info *t = cdev->lldi.tids;
  958. unsigned int tid = GET_TID(req);
  959. u16 peer_mss = ntohs(req->tcpopt.mss);
  960. unsigned short hdrs;
  961. struct dst_entry *dst;
  962. __u8 local_ip[16], peer_ip[16];
  963. __be16 local_port, peer_port;
  964. int ret;
  965. int iptype;
  966. pr_debug("%s: cdev = %p; stid = %u; tid = %u\n",
  967. __func__, cdev, stid, tid);
  968. cnp = lookup_stid(t, stid);
  969. if (!cnp) {
  970. pr_err("%s connect request on invalid stid %d\n",
  971. __func__, stid);
  972. goto rel_skb;
  973. }
  974. if (cnp->com.state != CSK_STATE_LISTEN) {
  975. pr_err("%s - listening parent not in CSK_STATE_LISTEN\n",
  976. __func__);
  977. goto reject;
  978. }
  979. csk = lookup_tid(t, tid);
  980. if (csk) {
  981. pr_err("%s csk not null tid %u\n",
  982. __func__, tid);
  983. goto rel_skb;
  984. }
  985. cxgb_get_4tuple(req, cdev->lldi.adapter_type, &iptype, local_ip,
  986. peer_ip, &local_port, &peer_port);
  987. /* Find output route */
  988. if (iptype == 4) {
  989. pr_debug("%s parent sock %p tid %u laddr %pI4 raddr %pI4 "
  990. "lport %d rport %d peer_mss %d\n"
  991. , __func__, cnp, tid,
  992. local_ip, peer_ip, ntohs(local_port),
  993. ntohs(peer_port), peer_mss);
  994. dst = cxgb_find_route(&cdev->lldi, cxgbit_get_real_dev,
  995. *(__be32 *)local_ip,
  996. *(__be32 *)peer_ip,
  997. local_port, peer_port,
  998. PASS_OPEN_TOS_G(ntohl(req->tos_stid)));
  999. } else {
  1000. pr_debug("%s parent sock %p tid %u laddr %pI6 raddr %pI6 "
  1001. "lport %d rport %d peer_mss %d\n"
  1002. , __func__, cnp, tid,
  1003. local_ip, peer_ip, ntohs(local_port),
  1004. ntohs(peer_port), peer_mss);
  1005. dst = cxgb_find_route6(&cdev->lldi, cxgbit_get_real_dev,
  1006. local_ip, peer_ip,
  1007. local_port, peer_port,
  1008. PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
  1009. ((struct sockaddr_in6 *)
  1010. &cnp->com.local_addr)->sin6_scope_id);
  1011. }
  1012. if (!dst) {
  1013. pr_err("%s - failed to find dst entry!\n",
  1014. __func__);
  1015. goto reject;
  1016. }
  1017. csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
  1018. if (!csk) {
  1019. dst_release(dst);
  1020. goto rel_skb;
  1021. }
  1022. ret = cxgbit_offload_init(csk, iptype, peer_ip, ntohs(local_port),
  1023. dst, cdev);
  1024. if (ret) {
  1025. pr_err("%s - failed to allocate l2t entry!\n",
  1026. __func__);
  1027. dst_release(dst);
  1028. kfree(csk);
  1029. goto reject;
  1030. }
  1031. kref_init(&csk->kref);
  1032. init_completion(&csk->com.wr_wait.completion);
  1033. INIT_LIST_HEAD(&csk->accept_node);
  1034. hdrs = (iptype == 4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
  1035. sizeof(struct tcphdr) + (req->tcpopt.tstamp ? 12 : 0);
  1036. if (peer_mss && csk->mtu > (peer_mss + hdrs))
  1037. csk->mtu = peer_mss + hdrs;
  1038. csk->com.state = CSK_STATE_CONNECTING;
  1039. csk->com.cdev = cdev;
  1040. csk->cnp = cnp;
  1041. csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
  1042. csk->dst = dst;
  1043. csk->tid = tid;
  1044. csk->wr_cred = cdev->lldi.wr_cred -
  1045. DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
  1046. csk->wr_max_cred = csk->wr_cred;
  1047. csk->wr_una_cred = 0;
  1048. if (iptype == 4) {
  1049. struct sockaddr_in *sin = (struct sockaddr_in *)
  1050. &csk->com.local_addr;
  1051. sin->sin_family = AF_INET;
  1052. sin->sin_port = local_port;
  1053. sin->sin_addr.s_addr = *(__be32 *)local_ip;
  1054. sin = (struct sockaddr_in *)&csk->com.remote_addr;
  1055. sin->sin_family = AF_INET;
  1056. sin->sin_port = peer_port;
  1057. sin->sin_addr.s_addr = *(__be32 *)peer_ip;
  1058. } else {
  1059. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
  1060. &csk->com.local_addr;
  1061. sin6->sin6_family = PF_INET6;
  1062. sin6->sin6_port = local_port;
  1063. memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
  1064. cxgb4_clip_get(cdev->lldi.ports[0],
  1065. (const u32 *)&sin6->sin6_addr.s6_addr,
  1066. 1);
  1067. sin6 = (struct sockaddr_in6 *)&csk->com.remote_addr;
  1068. sin6->sin6_family = PF_INET6;
  1069. sin6->sin6_port = peer_port;
  1070. memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
  1071. }
  1072. skb_queue_head_init(&csk->rxq);
  1073. skb_queue_head_init(&csk->txq);
  1074. skb_queue_head_init(&csk->ppodq);
  1075. skb_queue_head_init(&csk->backlogq);
  1076. skb_queue_head_init(&csk->skbq);
  1077. cxgbit_sock_reset_wr_list(csk);
  1078. spin_lock_init(&csk->lock);
  1079. init_waitqueue_head(&csk->waitq);
  1080. init_waitqueue_head(&csk->ack_waitq);
  1081. csk->lock_owner = false;
  1082. if (cxgbit_alloc_csk_skb(csk)) {
  1083. dst_release(dst);
  1084. kfree(csk);
  1085. goto rel_skb;
  1086. }
  1087. cxgbit_get_cdev(cdev);
  1088. spin_lock(&cdev->cskq.lock);
  1089. list_add_tail(&csk->list, &cdev->cskq.list);
  1090. spin_unlock(&cdev->cskq.lock);
  1091. cxgb4_insert_tid(t, csk, tid, csk->com.local_addr.ss_family);
  1092. cxgbit_pass_accept_rpl(csk, req);
  1093. goto rel_skb;
  1094. reject:
  1095. cxgbit_release_tid(cdev, tid);
  1096. rel_skb:
  1097. __kfree_skb(skb);
  1098. }
  1099. static u32
  1100. cxgbit_tx_flowc_wr_credits(struct cxgbit_sock *csk, u32 *nparamsp,
  1101. u32 *flowclenp)
  1102. {
  1103. u32 nparams, flowclen16, flowclen;
  1104. nparams = FLOWC_WR_NPARAMS_MIN;
  1105. if (csk->snd_wscale)
  1106. nparams++;
  1107. #ifdef CONFIG_CHELSIO_T4_DCB
  1108. nparams++;
  1109. #endif
  1110. flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
  1111. flowclen16 = DIV_ROUND_UP(flowclen, 16);
  1112. flowclen = flowclen16 * 16;
  1113. /*
  1114. * Return the number of 16-byte credits used by the flowc request.
  1115. * Pass back the nparams and actual flowc length if requested.
  1116. */
  1117. if (nparamsp)
  1118. *nparamsp = nparams;
  1119. if (flowclenp)
  1120. *flowclenp = flowclen;
  1121. return flowclen16;
  1122. }
  1123. u32 cxgbit_send_tx_flowc_wr(struct cxgbit_sock *csk)
  1124. {
  1125. struct cxgbit_device *cdev = csk->com.cdev;
  1126. struct fw_flowc_wr *flowc;
  1127. u32 nparams, flowclen16, flowclen;
  1128. struct sk_buff *skb;
  1129. u8 index;
  1130. #ifdef CONFIG_CHELSIO_T4_DCB
  1131. u16 vlan = ((struct l2t_entry *)csk->l2t)->vlan;
  1132. #endif
  1133. flowclen16 = cxgbit_tx_flowc_wr_credits(csk, &nparams, &flowclen);
  1134. skb = __skb_dequeue(&csk->skbq);
  1135. flowc = __skb_put(skb, flowclen);
  1136. memset(flowc, 0, flowclen);
  1137. flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
  1138. FW_FLOWC_WR_NPARAMS_V(nparams));
  1139. flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
  1140. FW_WR_FLOWID_V(csk->tid));
  1141. flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
  1142. flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
  1143. (csk->com.cdev->lldi.pf));
  1144. flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
  1145. flowc->mnemval[1].val = cpu_to_be32(csk->tx_chan);
  1146. flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
  1147. flowc->mnemval[2].val = cpu_to_be32(csk->tx_chan);
  1148. flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
  1149. flowc->mnemval[3].val = cpu_to_be32(csk->rss_qid);
  1150. flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
  1151. flowc->mnemval[4].val = cpu_to_be32(csk->snd_nxt);
  1152. flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
  1153. flowc->mnemval[5].val = cpu_to_be32(csk->rcv_nxt);
  1154. flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
  1155. flowc->mnemval[6].val = cpu_to_be32(csk->snd_win);
  1156. flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
  1157. flowc->mnemval[7].val = cpu_to_be32(csk->emss);
  1158. flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
  1159. if (test_bit(CDEV_ISO_ENABLE, &cdev->flags))
  1160. flowc->mnemval[8].val = cpu_to_be32(CXGBIT_MAX_ISO_PAYLOAD);
  1161. else
  1162. flowc->mnemval[8].val = cpu_to_be32(16384);
  1163. index = 9;
  1164. if (csk->snd_wscale) {
  1165. flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_RCV_SCALE;
  1166. flowc->mnemval[index].val = cpu_to_be32(csk->snd_wscale);
  1167. index++;
  1168. }
  1169. #ifdef CONFIG_CHELSIO_T4_DCB
  1170. flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_DCBPRIO;
  1171. if (vlan == VLAN_NONE) {
  1172. pr_warn("csk %u without VLAN Tag on DCB Link\n", csk->tid);
  1173. flowc->mnemval[index].val = cpu_to_be32(0);
  1174. } else
  1175. flowc->mnemval[index].val = cpu_to_be32(
  1176. (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT);
  1177. #endif
  1178. pr_debug("%s: csk %p; tx_chan = %u; rss_qid = %u; snd_seq = %u;"
  1179. " rcv_seq = %u; snd_win = %u; emss = %u\n",
  1180. __func__, csk, csk->tx_chan, csk->rss_qid, csk->snd_nxt,
  1181. csk->rcv_nxt, csk->snd_win, csk->emss);
  1182. set_wr_txq(skb, CPL_PRIORITY_DATA, csk->txq_idx);
  1183. cxgbit_ofld_send(csk->com.cdev, skb);
  1184. return flowclen16;
  1185. }
  1186. int cxgbit_setup_conn_digest(struct cxgbit_sock *csk)
  1187. {
  1188. struct sk_buff *skb;
  1189. struct cpl_set_tcb_field *req;
  1190. u8 hcrc = csk->submode & CXGBIT_SUBMODE_HCRC;
  1191. u8 dcrc = csk->submode & CXGBIT_SUBMODE_DCRC;
  1192. unsigned int len = roundup(sizeof(*req), 16);
  1193. int ret;
  1194. skb = alloc_skb(len, GFP_KERNEL);
  1195. if (!skb)
  1196. return -ENOMEM;
  1197. /* set up ulp submode */
  1198. req = __skb_put(skb, len);
  1199. memset(req, 0, len);
  1200. INIT_TP_WR(req, csk->tid);
  1201. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
  1202. req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
  1203. req->word_cookie = htons(0);
  1204. req->mask = cpu_to_be64(0x3 << 4);
  1205. req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
  1206. (dcrc ? ULP_CRC_DATA : 0)) << 4);
  1207. set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
  1208. cxgbit_get_csk(csk);
  1209. cxgbit_init_wr_wait(&csk->com.wr_wait);
  1210. cxgbit_ofld_send(csk->com.cdev, skb);
  1211. ret = cxgbit_wait_for_reply(csk->com.cdev,
  1212. &csk->com.wr_wait,
  1213. csk->tid, 5, __func__);
  1214. if (ret)
  1215. return -1;
  1216. return 0;
  1217. }
  1218. int cxgbit_setup_conn_pgidx(struct cxgbit_sock *csk, u32 pg_idx)
  1219. {
  1220. struct sk_buff *skb;
  1221. struct cpl_set_tcb_field *req;
  1222. unsigned int len = roundup(sizeof(*req), 16);
  1223. int ret;
  1224. skb = alloc_skb(len, GFP_KERNEL);
  1225. if (!skb)
  1226. return -ENOMEM;
  1227. req = __skb_put(skb, len);
  1228. memset(req, 0, len);
  1229. INIT_TP_WR(req, csk->tid);
  1230. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
  1231. req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
  1232. req->word_cookie = htons(0);
  1233. req->mask = cpu_to_be64(0x3 << 8);
  1234. req->val = cpu_to_be64(pg_idx << 8);
  1235. set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
  1236. cxgbit_get_csk(csk);
  1237. cxgbit_init_wr_wait(&csk->com.wr_wait);
  1238. cxgbit_ofld_send(csk->com.cdev, skb);
  1239. ret = cxgbit_wait_for_reply(csk->com.cdev,
  1240. &csk->com.wr_wait,
  1241. csk->tid, 5, __func__);
  1242. if (ret)
  1243. return -1;
  1244. return 0;
  1245. }
  1246. static void
  1247. cxgbit_pass_open_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
  1248. {
  1249. struct cpl_pass_open_rpl *rpl = cplhdr(skb);
  1250. struct tid_info *t = cdev->lldi.tids;
  1251. unsigned int stid = GET_TID(rpl);
  1252. struct cxgbit_np *cnp = lookup_stid(t, stid);
  1253. pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
  1254. __func__, cnp, stid, rpl->status);
  1255. if (!cnp) {
  1256. pr_info("%s stid %d lookup failure\n", __func__, stid);
  1257. return;
  1258. }
  1259. cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
  1260. cxgbit_put_cnp(cnp);
  1261. }
  1262. static void
  1263. cxgbit_close_listsrv_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
  1264. {
  1265. struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
  1266. struct tid_info *t = cdev->lldi.tids;
  1267. unsigned int stid = GET_TID(rpl);
  1268. struct cxgbit_np *cnp = lookup_stid(t, stid);
  1269. pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
  1270. __func__, cnp, stid, rpl->status);
  1271. if (!cnp) {
  1272. pr_info("%s stid %d lookup failure\n", __func__, stid);
  1273. return;
  1274. }
  1275. cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
  1276. cxgbit_put_cnp(cnp);
  1277. }
  1278. static void
  1279. cxgbit_pass_establish(struct cxgbit_device *cdev, struct sk_buff *skb)
  1280. {
  1281. struct cpl_pass_establish *req = cplhdr(skb);
  1282. struct tid_info *t = cdev->lldi.tids;
  1283. unsigned int tid = GET_TID(req);
  1284. struct cxgbit_sock *csk;
  1285. struct cxgbit_np *cnp;
  1286. u16 tcp_opt = be16_to_cpu(req->tcp_opt);
  1287. u32 snd_isn = be32_to_cpu(req->snd_isn);
  1288. u32 rcv_isn = be32_to_cpu(req->rcv_isn);
  1289. csk = lookup_tid(t, tid);
  1290. if (unlikely(!csk)) {
  1291. pr_err("can't find connection for tid %u.\n", tid);
  1292. goto rel_skb;
  1293. }
  1294. cnp = csk->cnp;
  1295. pr_debug("%s: csk %p; tid %u; cnp %p\n",
  1296. __func__, csk, tid, cnp);
  1297. csk->write_seq = snd_isn;
  1298. csk->snd_una = snd_isn;
  1299. csk->snd_nxt = snd_isn;
  1300. csk->rcv_nxt = rcv_isn;
  1301. if (csk->rcv_win > (RCV_BUFSIZ_M << 10))
  1302. csk->rx_credits = (csk->rcv_win - (RCV_BUFSIZ_M << 10));
  1303. csk->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
  1304. cxgbit_set_emss(csk, tcp_opt);
  1305. dst_confirm(csk->dst);
  1306. csk->com.state = CSK_STATE_ESTABLISHED;
  1307. spin_lock_bh(&cnp->np_accept_lock);
  1308. list_add_tail(&csk->accept_node, &cnp->np_accept_list);
  1309. spin_unlock_bh(&cnp->np_accept_lock);
  1310. complete(&cnp->accept_comp);
  1311. rel_skb:
  1312. __kfree_skb(skb);
  1313. }
  1314. static void cxgbit_queue_rx_skb(struct cxgbit_sock *csk, struct sk_buff *skb)
  1315. {
  1316. cxgbit_skcb_flags(skb) = 0;
  1317. spin_lock_bh(&csk->rxq.lock);
  1318. __skb_queue_tail(&csk->rxq, skb);
  1319. spin_unlock_bh(&csk->rxq.lock);
  1320. wake_up(&csk->waitq);
  1321. }
  1322. static void cxgbit_peer_close(struct cxgbit_sock *csk, struct sk_buff *skb)
  1323. {
  1324. pr_debug("%s: csk %p; tid %u; state %d\n",
  1325. __func__, csk, csk->tid, csk->com.state);
  1326. switch (csk->com.state) {
  1327. case CSK_STATE_ESTABLISHED:
  1328. csk->com.state = CSK_STATE_CLOSING;
  1329. cxgbit_queue_rx_skb(csk, skb);
  1330. return;
  1331. case CSK_STATE_CLOSING:
  1332. /* simultaneous close */
  1333. csk->com.state = CSK_STATE_MORIBUND;
  1334. break;
  1335. case CSK_STATE_MORIBUND:
  1336. csk->com.state = CSK_STATE_DEAD;
  1337. cxgbit_put_csk(csk);
  1338. break;
  1339. case CSK_STATE_ABORTING:
  1340. break;
  1341. default:
  1342. pr_info("%s: cpl_peer_close in bad state %d\n",
  1343. __func__, csk->com.state);
  1344. }
  1345. __kfree_skb(skb);
  1346. }
  1347. static void cxgbit_close_con_rpl(struct cxgbit_sock *csk, struct sk_buff *skb)
  1348. {
  1349. pr_debug("%s: csk %p; tid %u; state %d\n",
  1350. __func__, csk, csk->tid, csk->com.state);
  1351. switch (csk->com.state) {
  1352. case CSK_STATE_CLOSING:
  1353. csk->com.state = CSK_STATE_MORIBUND;
  1354. break;
  1355. case CSK_STATE_MORIBUND:
  1356. csk->com.state = CSK_STATE_DEAD;
  1357. cxgbit_put_csk(csk);
  1358. break;
  1359. case CSK_STATE_ABORTING:
  1360. case CSK_STATE_DEAD:
  1361. break;
  1362. default:
  1363. pr_info("%s: cpl_close_con_rpl in bad state %d\n",
  1364. __func__, csk->com.state);
  1365. }
  1366. __kfree_skb(skb);
  1367. }
  1368. static void cxgbit_abort_req_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
  1369. {
  1370. struct cpl_abort_req_rss *hdr = cplhdr(skb);
  1371. unsigned int tid = GET_TID(hdr);
  1372. struct sk_buff *rpl_skb;
  1373. bool release = false;
  1374. bool wakeup_thread = false;
  1375. u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
  1376. pr_debug("%s: csk %p; tid %u; state %d\n",
  1377. __func__, csk, tid, csk->com.state);
  1378. if (cxgb_is_neg_adv(hdr->status)) {
  1379. pr_err("%s: got neg advise %d on tid %u\n",
  1380. __func__, hdr->status, tid);
  1381. goto rel_skb;
  1382. }
  1383. switch (csk->com.state) {
  1384. case CSK_STATE_CONNECTING:
  1385. case CSK_STATE_MORIBUND:
  1386. csk->com.state = CSK_STATE_DEAD;
  1387. release = true;
  1388. break;
  1389. case CSK_STATE_ESTABLISHED:
  1390. csk->com.state = CSK_STATE_DEAD;
  1391. wakeup_thread = true;
  1392. break;
  1393. case CSK_STATE_CLOSING:
  1394. csk->com.state = CSK_STATE_DEAD;
  1395. if (!csk->conn)
  1396. release = true;
  1397. break;
  1398. case CSK_STATE_ABORTING:
  1399. break;
  1400. default:
  1401. pr_info("%s: cpl_abort_req_rss in bad state %d\n",
  1402. __func__, csk->com.state);
  1403. csk->com.state = CSK_STATE_DEAD;
  1404. }
  1405. __skb_queue_purge(&csk->txq);
  1406. if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
  1407. cxgbit_send_tx_flowc_wr(csk);
  1408. rpl_skb = __skb_dequeue(&csk->skbq);
  1409. cxgb_mk_abort_rpl(rpl_skb, len, csk->tid, csk->txq_idx);
  1410. cxgbit_ofld_send(csk->com.cdev, rpl_skb);
  1411. if (wakeup_thread) {
  1412. cxgbit_queue_rx_skb(csk, skb);
  1413. return;
  1414. }
  1415. if (release)
  1416. cxgbit_put_csk(csk);
  1417. rel_skb:
  1418. __kfree_skb(skb);
  1419. }
  1420. static void cxgbit_abort_rpl_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
  1421. {
  1422. pr_debug("%s: csk %p; tid %u; state %d\n",
  1423. __func__, csk, csk->tid, csk->com.state);
  1424. switch (csk->com.state) {
  1425. case CSK_STATE_ABORTING:
  1426. csk->com.state = CSK_STATE_DEAD;
  1427. cxgbit_put_csk(csk);
  1428. break;
  1429. default:
  1430. pr_info("%s: cpl_abort_rpl_rss in state %d\n",
  1431. __func__, csk->com.state);
  1432. }
  1433. __kfree_skb(skb);
  1434. }
  1435. static bool cxgbit_credit_err(const struct cxgbit_sock *csk)
  1436. {
  1437. const struct sk_buff *skb = csk->wr_pending_head;
  1438. u32 credit = 0;
  1439. if (unlikely(csk->wr_cred > csk->wr_max_cred)) {
  1440. pr_err("csk 0x%p, tid %u, credit %u > %u\n",
  1441. csk, csk->tid, csk->wr_cred, csk->wr_max_cred);
  1442. return true;
  1443. }
  1444. while (skb) {
  1445. credit += (__force u32)skb->csum;
  1446. skb = cxgbit_skcb_tx_wr_next(skb);
  1447. }
  1448. if (unlikely((csk->wr_cred + credit) != csk->wr_max_cred)) {
  1449. pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
  1450. csk, csk->tid, csk->wr_cred,
  1451. credit, csk->wr_max_cred);
  1452. return true;
  1453. }
  1454. return false;
  1455. }
  1456. static void cxgbit_fw4_ack(struct cxgbit_sock *csk, struct sk_buff *skb)
  1457. {
  1458. struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)cplhdr(skb);
  1459. u32 credits = rpl->credits;
  1460. u32 snd_una = ntohl(rpl->snd_una);
  1461. csk->wr_cred += credits;
  1462. if (csk->wr_una_cred > (csk->wr_max_cred - csk->wr_cred))
  1463. csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
  1464. while (credits) {
  1465. struct sk_buff *p = cxgbit_sock_peek_wr(csk);
  1466. const u32 csum = (__force u32)p->csum;
  1467. if (unlikely(!p)) {
  1468. pr_err("csk 0x%p,%u, cr %u,%u+%u, empty.\n",
  1469. csk, csk->tid, credits,
  1470. csk->wr_cred, csk->wr_una_cred);
  1471. break;
  1472. }
  1473. if (unlikely(credits < csum)) {
  1474. pr_warn("csk 0x%p,%u, cr %u,%u+%u, < %u.\n",
  1475. csk, csk->tid,
  1476. credits, csk->wr_cred, csk->wr_una_cred,
  1477. csum);
  1478. p->csum = (__force __wsum)(csum - credits);
  1479. break;
  1480. }
  1481. cxgbit_sock_dequeue_wr(csk);
  1482. credits -= csum;
  1483. kfree_skb(p);
  1484. }
  1485. if (unlikely(cxgbit_credit_err(csk))) {
  1486. cxgbit_queue_rx_skb(csk, skb);
  1487. return;
  1488. }
  1489. if (rpl->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
  1490. if (unlikely(before(snd_una, csk->snd_una))) {
  1491. pr_warn("csk 0x%p,%u, snd_una %u/%u.",
  1492. csk, csk->tid, snd_una,
  1493. csk->snd_una);
  1494. goto rel_skb;
  1495. }
  1496. if (csk->snd_una != snd_una) {
  1497. csk->snd_una = snd_una;
  1498. dst_confirm(csk->dst);
  1499. wake_up(&csk->ack_waitq);
  1500. }
  1501. }
  1502. if (skb_queue_len(&csk->txq))
  1503. cxgbit_push_tx_frames(csk);
  1504. rel_skb:
  1505. __kfree_skb(skb);
  1506. }
  1507. static void cxgbit_set_tcb_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
  1508. {
  1509. struct cxgbit_sock *csk;
  1510. struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
  1511. unsigned int tid = GET_TID(rpl);
  1512. struct cxgb4_lld_info *lldi = &cdev->lldi;
  1513. struct tid_info *t = lldi->tids;
  1514. csk = lookup_tid(t, tid);
  1515. if (unlikely(!csk))
  1516. pr_err("can't find connection for tid %u.\n", tid);
  1517. else
  1518. cxgbit_wake_up(&csk->com.wr_wait, __func__, rpl->status);
  1519. cxgbit_put_csk(csk);
  1520. }
  1521. static void cxgbit_rx_data(struct cxgbit_device *cdev, struct sk_buff *skb)
  1522. {
  1523. struct cxgbit_sock *csk;
  1524. struct cpl_rx_data *cpl = cplhdr(skb);
  1525. unsigned int tid = GET_TID(cpl);
  1526. struct cxgb4_lld_info *lldi = &cdev->lldi;
  1527. struct tid_info *t = lldi->tids;
  1528. csk = lookup_tid(t, tid);
  1529. if (unlikely(!csk)) {
  1530. pr_err("can't find conn. for tid %u.\n", tid);
  1531. goto rel_skb;
  1532. }
  1533. cxgbit_queue_rx_skb(csk, skb);
  1534. return;
  1535. rel_skb:
  1536. __kfree_skb(skb);
  1537. }
  1538. static void
  1539. __cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
  1540. {
  1541. spin_lock(&csk->lock);
  1542. if (csk->lock_owner) {
  1543. __skb_queue_tail(&csk->backlogq, skb);
  1544. spin_unlock(&csk->lock);
  1545. return;
  1546. }
  1547. cxgbit_skcb_rx_backlog_fn(skb)(csk, skb);
  1548. spin_unlock(&csk->lock);
  1549. }
  1550. static void cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
  1551. {
  1552. cxgbit_get_csk(csk);
  1553. __cxgbit_process_rx_cpl(csk, skb);
  1554. cxgbit_put_csk(csk);
  1555. }
  1556. static void cxgbit_rx_cpl(struct cxgbit_device *cdev, struct sk_buff *skb)
  1557. {
  1558. struct cxgbit_sock *csk;
  1559. struct cpl_tx_data *cpl = cplhdr(skb);
  1560. struct cxgb4_lld_info *lldi = &cdev->lldi;
  1561. struct tid_info *t = lldi->tids;
  1562. unsigned int tid = GET_TID(cpl);
  1563. u8 opcode = cxgbit_skcb_rx_opcode(skb);
  1564. bool ref = true;
  1565. switch (opcode) {
  1566. case CPL_FW4_ACK:
  1567. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_fw4_ack;
  1568. ref = false;
  1569. break;
  1570. case CPL_PEER_CLOSE:
  1571. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_peer_close;
  1572. break;
  1573. case CPL_CLOSE_CON_RPL:
  1574. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_close_con_rpl;
  1575. break;
  1576. case CPL_ABORT_REQ_RSS:
  1577. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_req_rss;
  1578. break;
  1579. case CPL_ABORT_RPL_RSS:
  1580. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_rpl_rss;
  1581. break;
  1582. default:
  1583. goto rel_skb;
  1584. }
  1585. csk = lookup_tid(t, tid);
  1586. if (unlikely(!csk)) {
  1587. pr_err("can't find conn. for tid %u.\n", tid);
  1588. goto rel_skb;
  1589. }
  1590. if (ref)
  1591. cxgbit_process_rx_cpl(csk, skb);
  1592. else
  1593. __cxgbit_process_rx_cpl(csk, skb);
  1594. return;
  1595. rel_skb:
  1596. __kfree_skb(skb);
  1597. }
  1598. cxgbit_cplhandler_func cxgbit_cplhandlers[NUM_CPL_CMDS] = {
  1599. [CPL_PASS_OPEN_RPL] = cxgbit_pass_open_rpl,
  1600. [CPL_CLOSE_LISTSRV_RPL] = cxgbit_close_listsrv_rpl,
  1601. [CPL_PASS_ACCEPT_REQ] = cxgbit_pass_accept_req,
  1602. [CPL_PASS_ESTABLISH] = cxgbit_pass_establish,
  1603. [CPL_SET_TCB_RPL] = cxgbit_set_tcb_rpl,
  1604. [CPL_RX_DATA] = cxgbit_rx_data,
  1605. [CPL_FW4_ACK] = cxgbit_rx_cpl,
  1606. [CPL_PEER_CLOSE] = cxgbit_rx_cpl,
  1607. [CPL_CLOSE_CON_RPL] = cxgbit_rx_cpl,
  1608. [CPL_ABORT_REQ_RSS] = cxgbit_rx_cpl,
  1609. [CPL_ABORT_RPL_RSS] = cxgbit_rx_cpl,
  1610. };