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