cxgbit_cm.c 45 KB

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