cxgbit_cm.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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. struct cxgbit_sock *csk = handle;
  524. pr_debug("%s cxgbit_device %p\n", __func__, handle);
  525. kfree_skb(skb);
  526. cxgbit_put_csk(csk);
  527. }
  528. static void cxgbit_abort_arp_failure(void *handle, struct sk_buff *skb)
  529. {
  530. struct cxgbit_device *cdev = handle;
  531. struct cpl_abort_req *req = cplhdr(skb);
  532. pr_debug("%s cdev %p\n", __func__, cdev);
  533. req->cmd = CPL_ABORT_NO_RST;
  534. cxgbit_ofld_send(cdev, skb);
  535. }
  536. static int cxgbit_send_abort_req(struct cxgbit_sock *csk)
  537. {
  538. struct sk_buff *skb;
  539. u32 len = roundup(sizeof(struct cpl_abort_req), 16);
  540. pr_debug("%s: csk %p tid %u; state %d\n",
  541. __func__, csk, csk->tid, csk->com.state);
  542. __skb_queue_purge(&csk->txq);
  543. if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
  544. cxgbit_send_tx_flowc_wr(csk);
  545. skb = __skb_dequeue(&csk->skbq);
  546. cxgb_mk_abort_req(skb, len, csk->tid, csk->txq_idx,
  547. csk->com.cdev, cxgbit_abort_arp_failure);
  548. return cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
  549. }
  550. void cxgbit_free_conn(struct iscsi_conn *conn)
  551. {
  552. struct cxgbit_sock *csk = conn->context;
  553. bool release = false;
  554. pr_debug("%s: state %d\n",
  555. __func__, csk->com.state);
  556. spin_lock_bh(&csk->lock);
  557. switch (csk->com.state) {
  558. case CSK_STATE_ESTABLISHED:
  559. if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
  560. csk->com.state = CSK_STATE_CLOSING;
  561. cxgbit_send_halfclose(csk);
  562. } else {
  563. csk->com.state = CSK_STATE_ABORTING;
  564. cxgbit_send_abort_req(csk);
  565. }
  566. break;
  567. case CSK_STATE_CLOSING:
  568. csk->com.state = CSK_STATE_MORIBUND;
  569. cxgbit_send_halfclose(csk);
  570. break;
  571. case CSK_STATE_DEAD:
  572. release = true;
  573. break;
  574. default:
  575. pr_err("%s: csk %p; state %d\n",
  576. __func__, csk, csk->com.state);
  577. }
  578. spin_unlock_bh(&csk->lock);
  579. if (release)
  580. cxgbit_put_csk(csk);
  581. }
  582. static void cxgbit_set_emss(struct cxgbit_sock *csk, u16 opt)
  583. {
  584. csk->emss = csk->com.cdev->lldi.mtus[TCPOPT_MSS_G(opt)] -
  585. ((csk->com.remote_addr.ss_family == AF_INET) ?
  586. sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
  587. sizeof(struct tcphdr);
  588. csk->mss = csk->emss;
  589. if (TCPOPT_TSTAMP_G(opt))
  590. csk->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
  591. if (csk->emss < 128)
  592. csk->emss = 128;
  593. if (csk->emss & 7)
  594. pr_info("Warning: misaligned mtu idx %u mss %u emss=%u\n",
  595. TCPOPT_MSS_G(opt), csk->mss, csk->emss);
  596. pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
  597. csk->mss, csk->emss);
  598. }
  599. static void cxgbit_free_skb(struct cxgbit_sock *csk)
  600. {
  601. struct sk_buff *skb;
  602. __skb_queue_purge(&csk->txq);
  603. __skb_queue_purge(&csk->rxq);
  604. __skb_queue_purge(&csk->backlogq);
  605. __skb_queue_purge(&csk->ppodq);
  606. __skb_queue_purge(&csk->skbq);
  607. while ((skb = cxgbit_sock_dequeue_wr(csk)))
  608. kfree_skb(skb);
  609. __kfree_skb(csk->lro_hskb);
  610. }
  611. void _cxgbit_free_csk(struct kref *kref)
  612. {
  613. struct cxgbit_sock *csk;
  614. struct cxgbit_device *cdev;
  615. csk = container_of(kref, struct cxgbit_sock, kref);
  616. pr_debug("%s csk %p state %d\n", __func__, csk, csk->com.state);
  617. if (csk->com.local_addr.ss_family == AF_INET6) {
  618. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
  619. &csk->com.local_addr;
  620. cxgb4_clip_release(csk->com.cdev->lldi.ports[0],
  621. (const u32 *)
  622. &sin6->sin6_addr.s6_addr, 1);
  623. }
  624. cxgb4_remove_tid(csk->com.cdev->lldi.tids, 0, csk->tid,
  625. csk->com.local_addr.ss_family);
  626. dst_release(csk->dst);
  627. cxgb4_l2t_release(csk->l2t);
  628. cdev = csk->com.cdev;
  629. spin_lock_bh(&cdev->cskq.lock);
  630. list_del(&csk->list);
  631. spin_unlock_bh(&cdev->cskq.lock);
  632. cxgbit_free_skb(csk);
  633. cxgbit_put_cdev(cdev);
  634. kfree(csk);
  635. }
  636. static void cxgbit_set_tcp_window(struct cxgbit_sock *csk, struct port_info *pi)
  637. {
  638. unsigned int linkspeed;
  639. u8 scale;
  640. linkspeed = pi->link_cfg.speed;
  641. scale = linkspeed / SPEED_10000;
  642. #define CXGBIT_10G_RCV_WIN (256 * 1024)
  643. csk->rcv_win = CXGBIT_10G_RCV_WIN;
  644. if (scale)
  645. csk->rcv_win *= scale;
  646. #define CXGBIT_10G_SND_WIN (256 * 1024)
  647. csk->snd_win = CXGBIT_10G_SND_WIN;
  648. if (scale)
  649. csk->snd_win *= scale;
  650. pr_debug("%s snd_win %d rcv_win %d\n",
  651. __func__, csk->snd_win, csk->rcv_win);
  652. }
  653. #ifdef CONFIG_CHELSIO_T4_DCB
  654. static u8 cxgbit_get_iscsi_dcb_state(struct net_device *ndev)
  655. {
  656. return ndev->dcbnl_ops->getstate(ndev);
  657. }
  658. static int cxgbit_select_priority(int pri_mask)
  659. {
  660. if (!pri_mask)
  661. return 0;
  662. return (ffs(pri_mask) - 1);
  663. }
  664. static u8 cxgbit_get_iscsi_dcb_priority(struct net_device *ndev, u16 local_port)
  665. {
  666. int ret;
  667. u8 caps;
  668. struct dcb_app iscsi_dcb_app = {
  669. .protocol = local_port
  670. };
  671. ret = (int)ndev->dcbnl_ops->getcap(ndev, DCB_CAP_ATTR_DCBX, &caps);
  672. if (ret)
  673. return 0;
  674. if (caps & DCB_CAP_DCBX_VER_IEEE) {
  675. iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_ANY;
  676. ret = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
  677. } else if (caps & DCB_CAP_DCBX_VER_CEE) {
  678. iscsi_dcb_app.selector = DCB_APP_IDTYPE_PORTNUM;
  679. ret = dcb_getapp(ndev, &iscsi_dcb_app);
  680. }
  681. pr_info("iSCSI priority is set to %u\n", cxgbit_select_priority(ret));
  682. return cxgbit_select_priority(ret);
  683. }
  684. #endif
  685. static int
  686. cxgbit_offload_init(struct cxgbit_sock *csk, int iptype, __u8 *peer_ip,
  687. u16 local_port, struct dst_entry *dst,
  688. struct cxgbit_device *cdev)
  689. {
  690. struct neighbour *n;
  691. int ret, step;
  692. struct net_device *ndev;
  693. u16 rxq_idx, port_id;
  694. #ifdef CONFIG_CHELSIO_T4_DCB
  695. u8 priority = 0;
  696. #endif
  697. n = dst_neigh_lookup(dst, peer_ip);
  698. if (!n)
  699. return -ENODEV;
  700. rcu_read_lock();
  701. ret = -ENOMEM;
  702. if (n->dev->flags & IFF_LOOPBACK) {
  703. if (iptype == 4)
  704. ndev = cxgbit_ipv4_netdev(*(__be32 *)peer_ip);
  705. else if (IS_ENABLED(CONFIG_IPV6))
  706. ndev = cxgbit_ipv6_netdev((struct in6_addr *)peer_ip);
  707. else
  708. ndev = NULL;
  709. if (!ndev) {
  710. ret = -ENODEV;
  711. goto out;
  712. }
  713. csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t,
  714. n, ndev, 0);
  715. if (!csk->l2t)
  716. goto out;
  717. csk->mtu = ndev->mtu;
  718. csk->tx_chan = cxgb4_port_chan(ndev);
  719. csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
  720. cxgb4_port_viid(ndev));
  721. step = cdev->lldi.ntxq /
  722. cdev->lldi.nchan;
  723. csk->txq_idx = cxgb4_port_idx(ndev) * step;
  724. step = cdev->lldi.nrxq /
  725. cdev->lldi.nchan;
  726. csk->ctrlq_idx = cxgb4_port_idx(ndev);
  727. csk->rss_qid = cdev->lldi.rxq_ids[
  728. cxgb4_port_idx(ndev) * step];
  729. csk->port_id = cxgb4_port_idx(ndev);
  730. cxgbit_set_tcp_window(csk,
  731. (struct port_info *)netdev_priv(ndev));
  732. } else {
  733. ndev = cxgbit_get_real_dev(n->dev);
  734. if (!ndev) {
  735. ret = -ENODEV;
  736. goto out;
  737. }
  738. #ifdef CONFIG_CHELSIO_T4_DCB
  739. if (cxgbit_get_iscsi_dcb_state(ndev))
  740. priority = cxgbit_get_iscsi_dcb_priority(ndev,
  741. local_port);
  742. csk->dcb_priority = priority;
  743. csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, priority);
  744. #else
  745. csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, 0);
  746. #endif
  747. if (!csk->l2t)
  748. goto out;
  749. port_id = cxgb4_port_idx(ndev);
  750. csk->mtu = dst_mtu(dst);
  751. csk->tx_chan = cxgb4_port_chan(ndev);
  752. csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
  753. cxgb4_port_viid(ndev));
  754. step = cdev->lldi.ntxq /
  755. cdev->lldi.nports;
  756. csk->txq_idx = (port_id * step) +
  757. (cdev->selectq[port_id][0]++ % step);
  758. csk->ctrlq_idx = cxgb4_port_idx(ndev);
  759. step = cdev->lldi.nrxq /
  760. cdev->lldi.nports;
  761. rxq_idx = (port_id * step) +
  762. (cdev->selectq[port_id][1]++ % step);
  763. csk->rss_qid = cdev->lldi.rxq_ids[rxq_idx];
  764. csk->port_id = port_id;
  765. cxgbit_set_tcp_window(csk,
  766. (struct port_info *)netdev_priv(ndev));
  767. }
  768. ret = 0;
  769. out:
  770. rcu_read_unlock();
  771. neigh_release(n);
  772. return ret;
  773. }
  774. int cxgbit_ofld_send(struct cxgbit_device *cdev, struct sk_buff *skb)
  775. {
  776. int ret = 0;
  777. if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
  778. kfree_skb(skb);
  779. pr_err("%s - device not up - dropping\n", __func__);
  780. return -EIO;
  781. }
  782. ret = cxgb4_ofld_send(cdev->lldi.ports[0], skb);
  783. if (ret < 0)
  784. kfree_skb(skb);
  785. return ret < 0 ? ret : 0;
  786. }
  787. static void cxgbit_release_tid(struct cxgbit_device *cdev, u32 tid)
  788. {
  789. u32 len = roundup(sizeof(struct cpl_tid_release), 16);
  790. struct sk_buff *skb;
  791. skb = alloc_skb(len, GFP_ATOMIC);
  792. if (!skb)
  793. return;
  794. cxgb_mk_tid_release(skb, len, tid, 0);
  795. cxgbit_ofld_send(cdev, skb);
  796. }
  797. int
  798. cxgbit_l2t_send(struct cxgbit_device *cdev, struct sk_buff *skb,
  799. struct l2t_entry *l2e)
  800. {
  801. int ret = 0;
  802. if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
  803. kfree_skb(skb);
  804. pr_err("%s - device not up - dropping\n", __func__);
  805. return -EIO;
  806. }
  807. ret = cxgb4_l2t_send(cdev->lldi.ports[0], skb, l2e);
  808. if (ret < 0)
  809. kfree_skb(skb);
  810. return ret < 0 ? ret : 0;
  811. }
  812. static void cxgbit_send_rx_credits(struct cxgbit_sock *csk, struct sk_buff *skb)
  813. {
  814. if (csk->com.state != CSK_STATE_ESTABLISHED) {
  815. __kfree_skb(skb);
  816. return;
  817. }
  818. cxgbit_ofld_send(csk->com.cdev, skb);
  819. }
  820. /*
  821. * CPL connection rx data ack: host ->
  822. * Send RX credits through an RX_DATA_ACK CPL message.
  823. * Returns the number of credits sent.
  824. */
  825. int cxgbit_rx_data_ack(struct cxgbit_sock *csk)
  826. {
  827. struct sk_buff *skb;
  828. u32 len = roundup(sizeof(struct cpl_rx_data_ack), 16);
  829. u32 credit_dack;
  830. skb = alloc_skb(len, GFP_KERNEL);
  831. if (!skb)
  832. return -1;
  833. credit_dack = RX_DACK_CHANGE_F | RX_DACK_MODE_V(1) |
  834. RX_CREDITS_V(csk->rx_credits);
  835. cxgb_mk_rx_data_ack(skb, len, csk->tid, csk->ctrlq_idx,
  836. credit_dack);
  837. csk->rx_credits = 0;
  838. spin_lock_bh(&csk->lock);
  839. if (csk->lock_owner) {
  840. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_send_rx_credits;
  841. __skb_queue_tail(&csk->backlogq, skb);
  842. spin_unlock_bh(&csk->lock);
  843. return 0;
  844. }
  845. cxgbit_send_rx_credits(csk, skb);
  846. spin_unlock_bh(&csk->lock);
  847. return 0;
  848. }
  849. #define FLOWC_WR_NPARAMS_MIN 9
  850. #define FLOWC_WR_NPARAMS_MAX 11
  851. static int cxgbit_alloc_csk_skb(struct cxgbit_sock *csk)
  852. {
  853. struct sk_buff *skb;
  854. u32 len, flowclen;
  855. u8 i;
  856. flowclen = offsetof(struct fw_flowc_wr,
  857. mnemval[FLOWC_WR_NPARAMS_MAX]);
  858. len = max_t(u32, sizeof(struct cpl_abort_req),
  859. sizeof(struct cpl_abort_rpl));
  860. len = max(len, flowclen);
  861. len = roundup(len, 16);
  862. for (i = 0; i < 3; i++) {
  863. skb = alloc_skb(len, GFP_ATOMIC);
  864. if (!skb)
  865. goto out;
  866. __skb_queue_tail(&csk->skbq, skb);
  867. }
  868. skb = alloc_skb(LRO_SKB_MIN_HEADROOM, GFP_ATOMIC);
  869. if (!skb)
  870. goto out;
  871. memset(skb->data, 0, LRO_SKB_MIN_HEADROOM);
  872. csk->lro_hskb = skb;
  873. return 0;
  874. out:
  875. __skb_queue_purge(&csk->skbq);
  876. return -ENOMEM;
  877. }
  878. static void
  879. cxgbit_pass_accept_rpl(struct cxgbit_sock *csk, struct cpl_pass_accept_req *req)
  880. {
  881. struct sk_buff *skb;
  882. const struct tcphdr *tcph;
  883. struct cpl_t5_pass_accept_rpl *rpl5;
  884. struct cxgb4_lld_info *lldi = &csk->com.cdev->lldi;
  885. unsigned int len = roundup(sizeof(*rpl5), 16);
  886. unsigned int mtu_idx;
  887. u64 opt0;
  888. u32 opt2, hlen;
  889. u32 wscale;
  890. u32 win;
  891. pr_debug("%s csk %p tid %u\n", __func__, csk, csk->tid);
  892. skb = alloc_skb(len, GFP_ATOMIC);
  893. if (!skb) {
  894. cxgbit_put_csk(csk);
  895. return;
  896. }
  897. rpl5 = __skb_put_zero(skb, len);
  898. INIT_TP_WR(rpl5, csk->tid);
  899. OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
  900. csk->tid));
  901. cxgb_best_mtu(csk->com.cdev->lldi.mtus, csk->mtu, &mtu_idx,
  902. req->tcpopt.tstamp,
  903. (csk->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
  904. wscale = cxgb_compute_wscale(csk->rcv_win);
  905. /*
  906. * Specify the largest window that will fit in opt0. The
  907. * remainder will be specified in the rx_data_ack.
  908. */
  909. win = csk->rcv_win >> 10;
  910. if (win > RCV_BUFSIZ_M)
  911. win = RCV_BUFSIZ_M;
  912. opt0 = TCAM_BYPASS_F |
  913. WND_SCALE_V(wscale) |
  914. MSS_IDX_V(mtu_idx) |
  915. L2T_IDX_V(csk->l2t->idx) |
  916. TX_CHAN_V(csk->tx_chan) |
  917. SMAC_SEL_V(csk->smac_idx) |
  918. DSCP_V(csk->tos >> 2) |
  919. ULP_MODE_V(ULP_MODE_ISCSI) |
  920. RCV_BUFSIZ_V(win);
  921. opt2 = RX_CHANNEL_V(0) |
  922. RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
  923. if (!is_t5(lldi->adapter_type))
  924. opt2 |= RX_FC_DISABLE_F;
  925. if (req->tcpopt.tstamp)
  926. opt2 |= TSTAMPS_EN_F;
  927. if (req->tcpopt.sack)
  928. opt2 |= SACK_EN_F;
  929. if (wscale)
  930. opt2 |= WND_SCALE_EN_F;
  931. hlen = ntohl(req->hdr_len);
  932. if (is_t5(lldi->adapter_type))
  933. tcph = (struct tcphdr *)((u8 *)(req + 1) +
  934. ETH_HDR_LEN_G(hlen) + IP_HDR_LEN_G(hlen));
  935. else
  936. tcph = (struct tcphdr *)((u8 *)(req + 1) +
  937. T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
  938. if (tcph->ece && tcph->cwr)
  939. opt2 |= CCTRL_ECN_V(1);
  940. opt2 |= RX_COALESCE_V(3);
  941. opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
  942. opt2 |= T5_ISS_F;
  943. rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
  944. opt2 |= T5_OPT_2_VALID_F;
  945. rpl5->opt0 = cpu_to_be64(opt0);
  946. rpl5->opt2 = cpu_to_be32(opt2);
  947. set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->ctrlq_idx);
  948. t4_set_arp_err_handler(skb, csk, cxgbit_arp_failure_discard);
  949. cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
  950. }
  951. static void
  952. cxgbit_pass_accept_req(struct cxgbit_device *cdev, struct sk_buff *skb)
  953. {
  954. struct cxgbit_sock *csk = NULL;
  955. struct cxgbit_np *cnp;
  956. struct cpl_pass_accept_req *req = cplhdr(skb);
  957. unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
  958. struct tid_info *t = cdev->lldi.tids;
  959. unsigned int tid = GET_TID(req);
  960. u16 peer_mss = ntohs(req->tcpopt.mss);
  961. unsigned short hdrs;
  962. struct dst_entry *dst;
  963. __u8 local_ip[16], peer_ip[16];
  964. __be16 local_port, peer_port;
  965. int ret;
  966. int iptype;
  967. pr_debug("%s: cdev = %p; stid = %u; tid = %u\n",
  968. __func__, cdev, stid, tid);
  969. cnp = lookup_stid(t, stid);
  970. if (!cnp) {
  971. pr_err("%s connect request on invalid stid %d\n",
  972. __func__, stid);
  973. goto rel_skb;
  974. }
  975. if (cnp->com.state != CSK_STATE_LISTEN) {
  976. pr_err("%s - listening parent not in CSK_STATE_LISTEN\n",
  977. __func__);
  978. goto reject;
  979. }
  980. csk = lookup_tid(t, tid);
  981. if (csk) {
  982. pr_err("%s csk not null tid %u\n",
  983. __func__, tid);
  984. goto rel_skb;
  985. }
  986. cxgb_get_4tuple(req, cdev->lldi.adapter_type, &iptype, local_ip,
  987. peer_ip, &local_port, &peer_port);
  988. /* Find output route */
  989. if (iptype == 4) {
  990. pr_debug("%s parent sock %p tid %u laddr %pI4 raddr %pI4 "
  991. "lport %d rport %d peer_mss %d\n"
  992. , __func__, cnp, tid,
  993. local_ip, peer_ip, ntohs(local_port),
  994. ntohs(peer_port), peer_mss);
  995. dst = cxgb_find_route(&cdev->lldi, cxgbit_get_real_dev,
  996. *(__be32 *)local_ip,
  997. *(__be32 *)peer_ip,
  998. local_port, peer_port,
  999. PASS_OPEN_TOS_G(ntohl(req->tos_stid)));
  1000. } else {
  1001. pr_debug("%s parent sock %p tid %u laddr %pI6 raddr %pI6 "
  1002. "lport %d rport %d peer_mss %d\n"
  1003. , __func__, cnp, tid,
  1004. local_ip, peer_ip, ntohs(local_port),
  1005. ntohs(peer_port), peer_mss);
  1006. dst = cxgb_find_route6(&cdev->lldi, cxgbit_get_real_dev,
  1007. local_ip, peer_ip,
  1008. local_port, peer_port,
  1009. PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
  1010. ((struct sockaddr_in6 *)
  1011. &cnp->com.local_addr)->sin6_scope_id);
  1012. }
  1013. if (!dst) {
  1014. pr_err("%s - failed to find dst entry!\n",
  1015. __func__);
  1016. goto reject;
  1017. }
  1018. csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
  1019. if (!csk) {
  1020. dst_release(dst);
  1021. goto rel_skb;
  1022. }
  1023. ret = cxgbit_offload_init(csk, iptype, peer_ip, ntohs(local_port),
  1024. dst, cdev);
  1025. if (ret) {
  1026. pr_err("%s - failed to allocate l2t entry!\n",
  1027. __func__);
  1028. dst_release(dst);
  1029. kfree(csk);
  1030. goto reject;
  1031. }
  1032. kref_init(&csk->kref);
  1033. init_completion(&csk->com.wr_wait.completion);
  1034. INIT_LIST_HEAD(&csk->accept_node);
  1035. hdrs = (iptype == 4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
  1036. sizeof(struct tcphdr) + (req->tcpopt.tstamp ? 12 : 0);
  1037. if (peer_mss && csk->mtu > (peer_mss + hdrs))
  1038. csk->mtu = peer_mss + hdrs;
  1039. csk->com.state = CSK_STATE_CONNECTING;
  1040. csk->com.cdev = cdev;
  1041. csk->cnp = cnp;
  1042. csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
  1043. csk->dst = dst;
  1044. csk->tid = tid;
  1045. csk->wr_cred = cdev->lldi.wr_cred -
  1046. DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
  1047. csk->wr_max_cred = csk->wr_cred;
  1048. csk->wr_una_cred = 0;
  1049. if (iptype == 4) {
  1050. struct sockaddr_in *sin = (struct sockaddr_in *)
  1051. &csk->com.local_addr;
  1052. sin->sin_family = AF_INET;
  1053. sin->sin_port = local_port;
  1054. sin->sin_addr.s_addr = *(__be32 *)local_ip;
  1055. sin = (struct sockaddr_in *)&csk->com.remote_addr;
  1056. sin->sin_family = AF_INET;
  1057. sin->sin_port = peer_port;
  1058. sin->sin_addr.s_addr = *(__be32 *)peer_ip;
  1059. } else {
  1060. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
  1061. &csk->com.local_addr;
  1062. sin6->sin6_family = PF_INET6;
  1063. sin6->sin6_port = local_port;
  1064. memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
  1065. cxgb4_clip_get(cdev->lldi.ports[0],
  1066. (const u32 *)&sin6->sin6_addr.s6_addr,
  1067. 1);
  1068. sin6 = (struct sockaddr_in6 *)&csk->com.remote_addr;
  1069. sin6->sin6_family = PF_INET6;
  1070. sin6->sin6_port = peer_port;
  1071. memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
  1072. }
  1073. skb_queue_head_init(&csk->rxq);
  1074. skb_queue_head_init(&csk->txq);
  1075. skb_queue_head_init(&csk->ppodq);
  1076. skb_queue_head_init(&csk->backlogq);
  1077. skb_queue_head_init(&csk->skbq);
  1078. cxgbit_sock_reset_wr_list(csk);
  1079. spin_lock_init(&csk->lock);
  1080. init_waitqueue_head(&csk->waitq);
  1081. init_waitqueue_head(&csk->ack_waitq);
  1082. csk->lock_owner = false;
  1083. if (cxgbit_alloc_csk_skb(csk)) {
  1084. dst_release(dst);
  1085. kfree(csk);
  1086. goto rel_skb;
  1087. }
  1088. cxgbit_get_cdev(cdev);
  1089. spin_lock(&cdev->cskq.lock);
  1090. list_add_tail(&csk->list, &cdev->cskq.list);
  1091. spin_unlock(&cdev->cskq.lock);
  1092. cxgb4_insert_tid(t, csk, tid, csk->com.local_addr.ss_family);
  1093. cxgbit_pass_accept_rpl(csk, req);
  1094. goto rel_skb;
  1095. reject:
  1096. cxgbit_release_tid(cdev, tid);
  1097. rel_skb:
  1098. __kfree_skb(skb);
  1099. }
  1100. static u32
  1101. cxgbit_tx_flowc_wr_credits(struct cxgbit_sock *csk, u32 *nparamsp,
  1102. u32 *flowclenp)
  1103. {
  1104. u32 nparams, flowclen16, flowclen;
  1105. nparams = FLOWC_WR_NPARAMS_MIN;
  1106. if (csk->snd_wscale)
  1107. nparams++;
  1108. #ifdef CONFIG_CHELSIO_T4_DCB
  1109. nparams++;
  1110. #endif
  1111. flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
  1112. flowclen16 = DIV_ROUND_UP(flowclen, 16);
  1113. flowclen = flowclen16 * 16;
  1114. /*
  1115. * Return the number of 16-byte credits used by the flowc request.
  1116. * Pass back the nparams and actual flowc length if requested.
  1117. */
  1118. if (nparamsp)
  1119. *nparamsp = nparams;
  1120. if (flowclenp)
  1121. *flowclenp = flowclen;
  1122. return flowclen16;
  1123. }
  1124. u32 cxgbit_send_tx_flowc_wr(struct cxgbit_sock *csk)
  1125. {
  1126. struct cxgbit_device *cdev = csk->com.cdev;
  1127. struct fw_flowc_wr *flowc;
  1128. u32 nparams, flowclen16, flowclen;
  1129. struct sk_buff *skb;
  1130. u8 index;
  1131. #ifdef CONFIG_CHELSIO_T4_DCB
  1132. u16 vlan = ((struct l2t_entry *)csk->l2t)->vlan;
  1133. #endif
  1134. flowclen16 = cxgbit_tx_flowc_wr_credits(csk, &nparams, &flowclen);
  1135. skb = __skb_dequeue(&csk->skbq);
  1136. flowc = __skb_put_zero(skb, 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_zero(skb, len);
  1199. INIT_TP_WR(req, csk->tid);
  1200. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
  1201. req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
  1202. req->word_cookie = htons(0);
  1203. req->mask = cpu_to_be64(0x3 << 4);
  1204. req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
  1205. (dcrc ? ULP_CRC_DATA : 0)) << 4);
  1206. set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
  1207. cxgbit_get_csk(csk);
  1208. cxgbit_init_wr_wait(&csk->com.wr_wait);
  1209. cxgbit_ofld_send(csk->com.cdev, skb);
  1210. ret = cxgbit_wait_for_reply(csk->com.cdev,
  1211. &csk->com.wr_wait,
  1212. csk->tid, 5, __func__);
  1213. if (ret)
  1214. return -1;
  1215. return 0;
  1216. }
  1217. int cxgbit_setup_conn_pgidx(struct cxgbit_sock *csk, u32 pg_idx)
  1218. {
  1219. struct sk_buff *skb;
  1220. struct cpl_set_tcb_field *req;
  1221. unsigned int len = roundup(sizeof(*req), 16);
  1222. int ret;
  1223. skb = alloc_skb(len, GFP_KERNEL);
  1224. if (!skb)
  1225. return -ENOMEM;
  1226. req = __skb_put_zero(skb, len);
  1227. INIT_TP_WR(req, csk->tid);
  1228. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
  1229. req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
  1230. req->word_cookie = htons(0);
  1231. req->mask = cpu_to_be64(0x3 << 8);
  1232. req->val = cpu_to_be64(pg_idx << 8);
  1233. set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
  1234. cxgbit_get_csk(csk);
  1235. cxgbit_init_wr_wait(&csk->com.wr_wait);
  1236. cxgbit_ofld_send(csk->com.cdev, skb);
  1237. ret = cxgbit_wait_for_reply(csk->com.cdev,
  1238. &csk->com.wr_wait,
  1239. csk->tid, 5, __func__);
  1240. if (ret)
  1241. return -1;
  1242. return 0;
  1243. }
  1244. static void
  1245. cxgbit_pass_open_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
  1246. {
  1247. struct cpl_pass_open_rpl *rpl = cplhdr(skb);
  1248. struct tid_info *t = cdev->lldi.tids;
  1249. unsigned int stid = GET_TID(rpl);
  1250. struct cxgbit_np *cnp = lookup_stid(t, stid);
  1251. pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
  1252. __func__, cnp, stid, rpl->status);
  1253. if (!cnp) {
  1254. pr_info("%s stid %d lookup failure\n", __func__, stid);
  1255. goto rel_skb;
  1256. }
  1257. cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
  1258. cxgbit_put_cnp(cnp);
  1259. rel_skb:
  1260. __kfree_skb(skb);
  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. goto rel_skb;
  1274. }
  1275. cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
  1276. cxgbit_put_cnp(cnp);
  1277. rel_skb:
  1278. __kfree_skb(skb);
  1279. }
  1280. static void
  1281. cxgbit_pass_establish(struct cxgbit_device *cdev, struct sk_buff *skb)
  1282. {
  1283. struct cpl_pass_establish *req = cplhdr(skb);
  1284. struct tid_info *t = cdev->lldi.tids;
  1285. unsigned int tid = GET_TID(req);
  1286. struct cxgbit_sock *csk;
  1287. struct cxgbit_np *cnp;
  1288. u16 tcp_opt = be16_to_cpu(req->tcp_opt);
  1289. u32 snd_isn = be32_to_cpu(req->snd_isn);
  1290. u32 rcv_isn = be32_to_cpu(req->rcv_isn);
  1291. csk = lookup_tid(t, tid);
  1292. if (unlikely(!csk)) {
  1293. pr_err("can't find connection for tid %u.\n", tid);
  1294. goto rel_skb;
  1295. }
  1296. cnp = csk->cnp;
  1297. pr_debug("%s: csk %p; tid %u; cnp %p\n",
  1298. __func__, csk, tid, cnp);
  1299. csk->write_seq = snd_isn;
  1300. csk->snd_una = snd_isn;
  1301. csk->snd_nxt = snd_isn;
  1302. csk->rcv_nxt = rcv_isn;
  1303. if (csk->rcv_win > (RCV_BUFSIZ_M << 10))
  1304. csk->rx_credits = (csk->rcv_win - (RCV_BUFSIZ_M << 10));
  1305. csk->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
  1306. cxgbit_set_emss(csk, tcp_opt);
  1307. dst_confirm(csk->dst);
  1308. csk->com.state = CSK_STATE_ESTABLISHED;
  1309. spin_lock_bh(&cnp->np_accept_lock);
  1310. list_add_tail(&csk->accept_node, &cnp->np_accept_list);
  1311. spin_unlock_bh(&cnp->np_accept_lock);
  1312. complete(&cnp->accept_comp);
  1313. rel_skb:
  1314. __kfree_skb(skb);
  1315. }
  1316. static void cxgbit_queue_rx_skb(struct cxgbit_sock *csk, struct sk_buff *skb)
  1317. {
  1318. cxgbit_skcb_flags(skb) = 0;
  1319. spin_lock_bh(&csk->rxq.lock);
  1320. __skb_queue_tail(&csk->rxq, skb);
  1321. spin_unlock_bh(&csk->rxq.lock);
  1322. wake_up(&csk->waitq);
  1323. }
  1324. static void cxgbit_peer_close(struct cxgbit_sock *csk, struct sk_buff *skb)
  1325. {
  1326. pr_debug("%s: csk %p; tid %u; state %d\n",
  1327. __func__, csk, csk->tid, csk->com.state);
  1328. switch (csk->com.state) {
  1329. case CSK_STATE_ESTABLISHED:
  1330. csk->com.state = CSK_STATE_CLOSING;
  1331. cxgbit_queue_rx_skb(csk, skb);
  1332. return;
  1333. case CSK_STATE_CLOSING:
  1334. /* simultaneous close */
  1335. csk->com.state = CSK_STATE_MORIBUND;
  1336. break;
  1337. case CSK_STATE_MORIBUND:
  1338. csk->com.state = CSK_STATE_DEAD;
  1339. cxgbit_put_csk(csk);
  1340. break;
  1341. case CSK_STATE_ABORTING:
  1342. break;
  1343. default:
  1344. pr_info("%s: cpl_peer_close in bad state %d\n",
  1345. __func__, csk->com.state);
  1346. }
  1347. __kfree_skb(skb);
  1348. }
  1349. static void cxgbit_close_con_rpl(struct cxgbit_sock *csk, struct sk_buff *skb)
  1350. {
  1351. pr_debug("%s: csk %p; tid %u; state %d\n",
  1352. __func__, csk, csk->tid, csk->com.state);
  1353. switch (csk->com.state) {
  1354. case CSK_STATE_CLOSING:
  1355. csk->com.state = CSK_STATE_MORIBUND;
  1356. break;
  1357. case CSK_STATE_MORIBUND:
  1358. csk->com.state = CSK_STATE_DEAD;
  1359. cxgbit_put_csk(csk);
  1360. break;
  1361. case CSK_STATE_ABORTING:
  1362. case CSK_STATE_DEAD:
  1363. break;
  1364. default:
  1365. pr_info("%s: cpl_close_con_rpl in bad state %d\n",
  1366. __func__, csk->com.state);
  1367. }
  1368. __kfree_skb(skb);
  1369. }
  1370. static void cxgbit_abort_req_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
  1371. {
  1372. struct cpl_abort_req_rss *hdr = cplhdr(skb);
  1373. unsigned int tid = GET_TID(hdr);
  1374. struct sk_buff *rpl_skb;
  1375. bool release = false;
  1376. bool wakeup_thread = false;
  1377. u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
  1378. pr_debug("%s: csk %p; tid %u; state %d\n",
  1379. __func__, csk, tid, csk->com.state);
  1380. if (cxgb_is_neg_adv(hdr->status)) {
  1381. pr_err("%s: got neg advise %d on tid %u\n",
  1382. __func__, hdr->status, tid);
  1383. goto rel_skb;
  1384. }
  1385. switch (csk->com.state) {
  1386. case CSK_STATE_CONNECTING:
  1387. case CSK_STATE_MORIBUND:
  1388. csk->com.state = CSK_STATE_DEAD;
  1389. release = true;
  1390. break;
  1391. case CSK_STATE_ESTABLISHED:
  1392. csk->com.state = CSK_STATE_DEAD;
  1393. wakeup_thread = true;
  1394. break;
  1395. case CSK_STATE_CLOSING:
  1396. csk->com.state = CSK_STATE_DEAD;
  1397. if (!csk->conn)
  1398. release = true;
  1399. break;
  1400. case CSK_STATE_ABORTING:
  1401. break;
  1402. default:
  1403. pr_info("%s: cpl_abort_req_rss in bad state %d\n",
  1404. __func__, csk->com.state);
  1405. csk->com.state = CSK_STATE_DEAD;
  1406. }
  1407. __skb_queue_purge(&csk->txq);
  1408. if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
  1409. cxgbit_send_tx_flowc_wr(csk);
  1410. rpl_skb = __skb_dequeue(&csk->skbq);
  1411. cxgb_mk_abort_rpl(rpl_skb, len, csk->tid, csk->txq_idx);
  1412. cxgbit_ofld_send(csk->com.cdev, rpl_skb);
  1413. if (wakeup_thread) {
  1414. cxgbit_queue_rx_skb(csk, skb);
  1415. return;
  1416. }
  1417. if (release)
  1418. cxgbit_put_csk(csk);
  1419. rel_skb:
  1420. __kfree_skb(skb);
  1421. }
  1422. static void cxgbit_abort_rpl_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
  1423. {
  1424. pr_debug("%s: csk %p; tid %u; state %d\n",
  1425. __func__, csk, csk->tid, csk->com.state);
  1426. switch (csk->com.state) {
  1427. case CSK_STATE_ABORTING:
  1428. csk->com.state = CSK_STATE_DEAD;
  1429. cxgbit_put_csk(csk);
  1430. break;
  1431. default:
  1432. pr_info("%s: cpl_abort_rpl_rss in state %d\n",
  1433. __func__, csk->com.state);
  1434. }
  1435. __kfree_skb(skb);
  1436. }
  1437. static bool cxgbit_credit_err(const struct cxgbit_sock *csk)
  1438. {
  1439. const struct sk_buff *skb = csk->wr_pending_head;
  1440. u32 credit = 0;
  1441. if (unlikely(csk->wr_cred > csk->wr_max_cred)) {
  1442. pr_err("csk 0x%p, tid %u, credit %u > %u\n",
  1443. csk, csk->tid, csk->wr_cred, csk->wr_max_cred);
  1444. return true;
  1445. }
  1446. while (skb) {
  1447. credit += (__force u32)skb->csum;
  1448. skb = cxgbit_skcb_tx_wr_next(skb);
  1449. }
  1450. if (unlikely((csk->wr_cred + credit) != csk->wr_max_cred)) {
  1451. pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
  1452. csk, csk->tid, csk->wr_cred,
  1453. credit, csk->wr_max_cred);
  1454. return true;
  1455. }
  1456. return false;
  1457. }
  1458. static void cxgbit_fw4_ack(struct cxgbit_sock *csk, struct sk_buff *skb)
  1459. {
  1460. struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)cplhdr(skb);
  1461. u32 credits = rpl->credits;
  1462. u32 snd_una = ntohl(rpl->snd_una);
  1463. csk->wr_cred += credits;
  1464. if (csk->wr_una_cred > (csk->wr_max_cred - csk->wr_cred))
  1465. csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
  1466. while (credits) {
  1467. struct sk_buff *p = cxgbit_sock_peek_wr(csk);
  1468. u32 csum;
  1469. if (unlikely(!p)) {
  1470. pr_err("csk 0x%p,%u, cr %u,%u+%u, empty.\n",
  1471. csk, csk->tid, credits,
  1472. csk->wr_cred, csk->wr_una_cred);
  1473. break;
  1474. }
  1475. csum = (__force u32)p->csum;
  1476. if (unlikely(credits < csum)) {
  1477. pr_warn("csk 0x%p,%u, cr %u,%u+%u, < %u.\n",
  1478. csk, csk->tid,
  1479. credits, csk->wr_cred, csk->wr_una_cred,
  1480. csum);
  1481. p->csum = (__force __wsum)(csum - credits);
  1482. break;
  1483. }
  1484. cxgbit_sock_dequeue_wr(csk);
  1485. credits -= csum;
  1486. kfree_skb(p);
  1487. }
  1488. if (unlikely(cxgbit_credit_err(csk))) {
  1489. cxgbit_queue_rx_skb(csk, skb);
  1490. return;
  1491. }
  1492. if (rpl->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
  1493. if (unlikely(before(snd_una, csk->snd_una))) {
  1494. pr_warn("csk 0x%p,%u, snd_una %u/%u.",
  1495. csk, csk->tid, snd_una,
  1496. csk->snd_una);
  1497. goto rel_skb;
  1498. }
  1499. if (csk->snd_una != snd_una) {
  1500. csk->snd_una = snd_una;
  1501. dst_confirm(csk->dst);
  1502. wake_up(&csk->ack_waitq);
  1503. }
  1504. }
  1505. if (skb_queue_len(&csk->txq))
  1506. cxgbit_push_tx_frames(csk);
  1507. rel_skb:
  1508. __kfree_skb(skb);
  1509. }
  1510. static void cxgbit_set_tcb_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
  1511. {
  1512. struct cxgbit_sock *csk;
  1513. struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
  1514. unsigned int tid = GET_TID(rpl);
  1515. struct cxgb4_lld_info *lldi = &cdev->lldi;
  1516. struct tid_info *t = lldi->tids;
  1517. csk = lookup_tid(t, tid);
  1518. if (unlikely(!csk)) {
  1519. pr_err("can't find connection for tid %u.\n", tid);
  1520. goto rel_skb;
  1521. } else {
  1522. cxgbit_wake_up(&csk->com.wr_wait, __func__, rpl->status);
  1523. }
  1524. cxgbit_put_csk(csk);
  1525. rel_skb:
  1526. __kfree_skb(skb);
  1527. }
  1528. static void cxgbit_rx_data(struct cxgbit_device *cdev, struct sk_buff *skb)
  1529. {
  1530. struct cxgbit_sock *csk;
  1531. struct cpl_rx_data *cpl = cplhdr(skb);
  1532. unsigned int tid = GET_TID(cpl);
  1533. struct cxgb4_lld_info *lldi = &cdev->lldi;
  1534. struct tid_info *t = lldi->tids;
  1535. csk = lookup_tid(t, tid);
  1536. if (unlikely(!csk)) {
  1537. pr_err("can't find conn. for tid %u.\n", tid);
  1538. goto rel_skb;
  1539. }
  1540. cxgbit_queue_rx_skb(csk, skb);
  1541. return;
  1542. rel_skb:
  1543. __kfree_skb(skb);
  1544. }
  1545. static void
  1546. __cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
  1547. {
  1548. spin_lock(&csk->lock);
  1549. if (csk->lock_owner) {
  1550. __skb_queue_tail(&csk->backlogq, skb);
  1551. spin_unlock(&csk->lock);
  1552. return;
  1553. }
  1554. cxgbit_skcb_rx_backlog_fn(skb)(csk, skb);
  1555. spin_unlock(&csk->lock);
  1556. }
  1557. static void cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
  1558. {
  1559. cxgbit_get_csk(csk);
  1560. __cxgbit_process_rx_cpl(csk, skb);
  1561. cxgbit_put_csk(csk);
  1562. }
  1563. static void cxgbit_rx_cpl(struct cxgbit_device *cdev, struct sk_buff *skb)
  1564. {
  1565. struct cxgbit_sock *csk;
  1566. struct cpl_tx_data *cpl = cplhdr(skb);
  1567. struct cxgb4_lld_info *lldi = &cdev->lldi;
  1568. struct tid_info *t = lldi->tids;
  1569. unsigned int tid = GET_TID(cpl);
  1570. u8 opcode = cxgbit_skcb_rx_opcode(skb);
  1571. bool ref = true;
  1572. switch (opcode) {
  1573. case CPL_FW4_ACK:
  1574. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_fw4_ack;
  1575. ref = false;
  1576. break;
  1577. case CPL_PEER_CLOSE:
  1578. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_peer_close;
  1579. break;
  1580. case CPL_CLOSE_CON_RPL:
  1581. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_close_con_rpl;
  1582. break;
  1583. case CPL_ABORT_REQ_RSS:
  1584. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_req_rss;
  1585. break;
  1586. case CPL_ABORT_RPL_RSS:
  1587. cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_rpl_rss;
  1588. break;
  1589. default:
  1590. goto rel_skb;
  1591. }
  1592. csk = lookup_tid(t, tid);
  1593. if (unlikely(!csk)) {
  1594. pr_err("can't find conn. for tid %u.\n", tid);
  1595. goto rel_skb;
  1596. }
  1597. if (ref)
  1598. cxgbit_process_rx_cpl(csk, skb);
  1599. else
  1600. __cxgbit_process_rx_cpl(csk, skb);
  1601. return;
  1602. rel_skb:
  1603. __kfree_skb(skb);
  1604. }
  1605. cxgbit_cplhandler_func cxgbit_cplhandlers[NUM_CPL_CMDS] = {
  1606. [CPL_PASS_OPEN_RPL] = cxgbit_pass_open_rpl,
  1607. [CPL_CLOSE_LISTSRV_RPL] = cxgbit_close_listsrv_rpl,
  1608. [CPL_PASS_ACCEPT_REQ] = cxgbit_pass_accept_req,
  1609. [CPL_PASS_ESTABLISH] = cxgbit_pass_establish,
  1610. [CPL_SET_TCB_RPL] = cxgbit_set_tcb_rpl,
  1611. [CPL_RX_DATA] = cxgbit_rx_data,
  1612. [CPL_FW4_ACK] = cxgbit_rx_cpl,
  1613. [CPL_PEER_CLOSE] = cxgbit_rx_cpl,
  1614. [CPL_CLOSE_CON_RPL] = cxgbit_rx_cpl,
  1615. [CPL_ABORT_REQ_RSS] = cxgbit_rx_cpl,
  1616. [CPL_ABORT_RPL_RSS] = cxgbit_rx_cpl,
  1617. };