cxgb3i_offload.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. /*
  2. * cxgb3i_offload.c: Chelsio S3xx iscsi offloaded tcp connection management
  3. *
  4. * Copyright (C) 2003-2008 Chelsio Communications. All rights reserved.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. * FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file included in this
  9. * release for licensing terms and conditions.
  10. *
  11. * Written by: Dimitris Michailidis (dm@chelsio.com)
  12. * Karen Xie (kxie@chelsio.com)
  13. */
  14. #include <linux/if_vlan.h>
  15. #include <linux/version.h>
  16. #include "cxgb3_defs.h"
  17. #include "cxgb3_ctl_defs.h"
  18. #include "firmware_exports.h"
  19. #include "cxgb3i_offload.h"
  20. #include "cxgb3i_pdu.h"
  21. #include "cxgb3i_ddp.h"
  22. #ifdef __DEBUG_C3CN_CONN__
  23. #define c3cn_conn_debug cxgb3i_log_info
  24. #else
  25. #define c3cn_conn_debug(fmt...)
  26. #endif
  27. #ifdef __DEBUG_C3CN_TX__
  28. #define c3cn_tx_debug cxgb3i_log_debug
  29. #else
  30. #define c3cn_tx_debug(fmt...)
  31. #endif
  32. #ifdef __DEBUG_C3CN_RX__
  33. #define c3cn_rx_debug cxgb3i_log_debug
  34. #else
  35. #define c3cn_rx_debug(fmt...)
  36. #endif
  37. /*
  38. * module parameters releated to offloaded iscsi connection
  39. */
  40. static int cxgb3_rcv_win = 256 * 1024;
  41. module_param(cxgb3_rcv_win, int, 0644);
  42. MODULE_PARM_DESC(cxgb3_rcv_win, "TCP receive window in bytes (default=256KB)");
  43. static int cxgb3_snd_win = 64 * 1024;
  44. module_param(cxgb3_snd_win, int, 0644);
  45. MODULE_PARM_DESC(cxgb3_snd_win, "TCP send window in bytes (default=64KB)");
  46. static int cxgb3_rx_credit_thres = 10 * 1024;
  47. module_param(cxgb3_rx_credit_thres, int, 0644);
  48. MODULE_PARM_DESC(rx_credit_thres,
  49. "RX credits return threshold in bytes (default=10KB)");
  50. static unsigned int cxgb3_max_connect = 8 * 1024;
  51. module_param(cxgb3_max_connect, uint, 0644);
  52. MODULE_PARM_DESC(cxgb3_max_connect, "Max. # of connections (default=8092)");
  53. static unsigned int cxgb3_sport_base = 20000;
  54. module_param(cxgb3_sport_base, uint, 0644);
  55. MODULE_PARM_DESC(cxgb3_sport_base, "starting port number (default=20000)");
  56. /*
  57. * cxgb3i tcp connection data(per adapter) list
  58. */
  59. static LIST_HEAD(cdata_list);
  60. static DEFINE_RWLOCK(cdata_rwlock);
  61. static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion);
  62. static void c3cn_release_offload_resources(struct s3_conn *c3cn);
  63. /*
  64. * iscsi source port management
  65. *
  66. * Find a free source port in the port allocation map. We use a very simple
  67. * rotor scheme to look for the next free port.
  68. *
  69. * If a source port has been specified make sure that it doesn't collide with
  70. * our normal source port allocation map. If it's outside the range of our
  71. * allocation/deallocation scheme just let them use it.
  72. *
  73. * If the source port is outside our allocation range, the caller is
  74. * responsible for keeping track of their port usage.
  75. */
  76. static int c3cn_get_port(struct s3_conn *c3cn, struct cxgb3i_sdev_data *cdata)
  77. {
  78. unsigned int start;
  79. int idx;
  80. if (!cdata)
  81. goto error_out;
  82. if (c3cn->saddr.sin_port != 0) {
  83. idx = ntohs(c3cn->saddr.sin_port) - cxgb3_sport_base;
  84. if (idx < 0 || idx >= cxgb3_max_connect)
  85. return 0;
  86. if (!test_and_set_bit(idx, cdata->sport_map))
  87. return -EADDRINUSE;
  88. }
  89. /* the sport_map_next may not be accurate but that is okay, sport_map
  90. should be */
  91. start = idx = cdata->sport_map_next;
  92. do {
  93. if (++idx >= cxgb3_max_connect)
  94. idx = 0;
  95. if (!(test_and_set_bit(idx, cdata->sport_map))) {
  96. c3cn->saddr.sin_port = htons(cxgb3_sport_base + idx);
  97. cdata->sport_map_next = idx;
  98. c3cn_conn_debug("%s reserve port %u.\n",
  99. cdata->cdev->name,
  100. cxgb3_sport_base + idx);
  101. return 0;
  102. }
  103. } while (idx != start);
  104. error_out:
  105. return -EADDRNOTAVAIL;
  106. }
  107. static void c3cn_put_port(struct s3_conn *c3cn)
  108. {
  109. struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(c3cn->cdev);
  110. if (c3cn->saddr.sin_port) {
  111. int idx = ntohs(c3cn->saddr.sin_port) - cxgb3_sport_base;
  112. c3cn->saddr.sin_port = 0;
  113. if (idx < 0 || idx >= cxgb3_max_connect)
  114. return;
  115. clear_bit(idx, cdata->sport_map);
  116. c3cn_conn_debug("%s, release port %u.\n",
  117. cdata->cdev->name, cxgb3_sport_base + idx);
  118. }
  119. }
  120. static inline void c3cn_set_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
  121. {
  122. __set_bit(flag, &c3cn->flags);
  123. c3cn_conn_debug("c3cn 0x%p, set %d, s %u, f 0x%lx.\n",
  124. c3cn, flag, c3cn->state, c3cn->flags);
  125. }
  126. static inline void c3cn_clear_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
  127. {
  128. __clear_bit(flag, &c3cn->flags);
  129. c3cn_conn_debug("c3cn 0x%p, clear %d, s %u, f 0x%lx.\n",
  130. c3cn, flag, c3cn->state, c3cn->flags);
  131. }
  132. static inline int c3cn_flag(struct s3_conn *c3cn, enum c3cn_flags flag)
  133. {
  134. if (c3cn == NULL)
  135. return 0;
  136. return test_bit(flag, &c3cn->flags);
  137. }
  138. static void c3cn_set_state(struct s3_conn *c3cn, int state)
  139. {
  140. c3cn_conn_debug("c3cn 0x%p state -> %u.\n", c3cn, state);
  141. c3cn->state = state;
  142. }
  143. static inline void c3cn_hold(struct s3_conn *c3cn)
  144. {
  145. atomic_inc(&c3cn->refcnt);
  146. }
  147. static inline void c3cn_put(struct s3_conn *c3cn)
  148. {
  149. if (atomic_dec_and_test(&c3cn->refcnt)) {
  150. c3cn_conn_debug("free c3cn 0x%p, s %u, f 0x%lx.\n",
  151. c3cn, c3cn->state, c3cn->flags);
  152. kfree(c3cn);
  153. }
  154. }
  155. static void c3cn_closed(struct s3_conn *c3cn)
  156. {
  157. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  158. c3cn, c3cn->state, c3cn->flags);
  159. c3cn_put_port(c3cn);
  160. c3cn_release_offload_resources(c3cn);
  161. c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
  162. cxgb3i_conn_closing(c3cn);
  163. }
  164. /*
  165. * CPL (Chelsio Protocol Language) defines a message passing interface between
  166. * the host driver and T3 asic.
  167. * The section below implments CPLs that related to iscsi tcp connection
  168. * open/close/abort and data send/receive.
  169. */
  170. /*
  171. * CPL connection active open request: host ->
  172. */
  173. static unsigned int find_best_mtu(const struct t3c_data *d, unsigned short mtu)
  174. {
  175. int i = 0;
  176. while (i < d->nmtus - 1 && d->mtus[i + 1] <= mtu)
  177. ++i;
  178. return i;
  179. }
  180. static unsigned int select_mss(struct s3_conn *c3cn, unsigned int pmtu)
  181. {
  182. unsigned int idx;
  183. struct dst_entry *dst = c3cn->dst_cache;
  184. struct t3cdev *cdev = c3cn->cdev;
  185. const struct t3c_data *td = T3C_DATA(cdev);
  186. u16 advmss = dst_metric(dst, RTAX_ADVMSS);
  187. if (advmss > pmtu - 40)
  188. advmss = pmtu - 40;
  189. if (advmss < td->mtus[0] - 40)
  190. advmss = td->mtus[0] - 40;
  191. idx = find_best_mtu(td, advmss + 40);
  192. return idx;
  193. }
  194. static inline int compute_wscale(int win)
  195. {
  196. int wscale = 0;
  197. while (wscale < 14 && (65535<<wscale) < win)
  198. wscale++;
  199. return wscale;
  200. }
  201. static inline unsigned int calc_opt0h(struct s3_conn *c3cn)
  202. {
  203. int wscale = compute_wscale(cxgb3_rcv_win);
  204. return V_KEEP_ALIVE(1) |
  205. F_TCAM_BYPASS |
  206. V_WND_SCALE(wscale) |
  207. V_MSS_IDX(c3cn->mss_idx);
  208. }
  209. static inline unsigned int calc_opt0l(struct s3_conn *c3cn)
  210. {
  211. return V_ULP_MODE(ULP_MODE_ISCSI) |
  212. V_RCV_BUFSIZ(cxgb3_rcv_win>>10);
  213. }
  214. static void make_act_open_req(struct s3_conn *c3cn, struct sk_buff *skb,
  215. unsigned int atid, const struct l2t_entry *e)
  216. {
  217. struct cpl_act_open_req *req;
  218. c3cn_conn_debug("c3cn 0x%p, atid 0x%x.\n", c3cn, atid);
  219. skb->priority = CPL_PRIORITY_SETUP;
  220. req = (struct cpl_act_open_req *)__skb_put(skb, sizeof(*req));
  221. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  222. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, atid));
  223. req->local_port = c3cn->saddr.sin_port;
  224. req->peer_port = c3cn->daddr.sin_port;
  225. req->local_ip = c3cn->saddr.sin_addr.s_addr;
  226. req->peer_ip = c3cn->daddr.sin_addr.s_addr;
  227. req->opt0h = htonl(calc_opt0h(c3cn) | V_L2T_IDX(e->idx) |
  228. V_TX_CHANNEL(e->smt_idx));
  229. req->opt0l = htonl(calc_opt0l(c3cn));
  230. req->params = 0;
  231. }
  232. static void fail_act_open(struct s3_conn *c3cn, int errno)
  233. {
  234. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  235. c3cn, c3cn->state, c3cn->flags);
  236. c3cn->err = errno;
  237. c3cn_closed(c3cn);
  238. }
  239. static void act_open_req_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
  240. {
  241. struct s3_conn *c3cn = (struct s3_conn *)skb->sk;
  242. c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
  243. c3cn_hold(c3cn);
  244. spin_lock_bh(&c3cn->lock);
  245. if (c3cn->state == C3CN_STATE_CONNECTING)
  246. fail_act_open(c3cn, EHOSTUNREACH);
  247. spin_unlock_bh(&c3cn->lock);
  248. c3cn_put(c3cn);
  249. __kfree_skb(skb);
  250. }
  251. /*
  252. * CPL connection close request: host ->
  253. *
  254. * Close a connection by sending a CPL_CLOSE_CON_REQ message and queue it to
  255. * the write queue (i.e., after any unsent txt data).
  256. */
  257. static void skb_entail(struct s3_conn *c3cn, struct sk_buff *skb,
  258. int flags)
  259. {
  260. CXGB3_SKB_CB(skb)->seq = c3cn->write_seq;
  261. CXGB3_SKB_CB(skb)->flags = flags;
  262. __skb_queue_tail(&c3cn->write_queue, skb);
  263. }
  264. static void send_close_req(struct s3_conn *c3cn)
  265. {
  266. struct sk_buff *skb = c3cn->cpl_close;
  267. struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
  268. unsigned int tid = c3cn->tid;
  269. c3cn_conn_debug("c3cn 0x%p, state 0x%x, flag 0x%lx.\n",
  270. c3cn, c3cn->state, c3cn->flags);
  271. c3cn->cpl_close = NULL;
  272. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
  273. req->wr.wr_lo = htonl(V_WR_TID(tid));
  274. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
  275. req->rsvd = htonl(c3cn->write_seq);
  276. skb_entail(c3cn, skb, C3CB_FLAG_NO_APPEND);
  277. if (c3cn->state != C3CN_STATE_CONNECTING)
  278. c3cn_push_tx_frames(c3cn, 1);
  279. }
  280. /*
  281. * CPL connection abort request: host ->
  282. *
  283. * Send an ABORT_REQ message. Makes sure we do not send multiple ABORT_REQs
  284. * for the same connection and also that we do not try to send a message
  285. * after the connection has closed.
  286. */
  287. static void abort_arp_failure(struct t3cdev *cdev, struct sk_buff *skb)
  288. {
  289. struct cpl_abort_req *req = cplhdr(skb);
  290. c3cn_conn_debug("tdev 0x%p.\n", cdev);
  291. req->cmd = CPL_ABORT_NO_RST;
  292. cxgb3_ofld_send(cdev, skb);
  293. }
  294. static inline void c3cn_purge_write_queue(struct s3_conn *c3cn)
  295. {
  296. struct sk_buff *skb;
  297. while ((skb = __skb_dequeue(&c3cn->write_queue)))
  298. __kfree_skb(skb);
  299. }
  300. static void send_abort_req(struct s3_conn *c3cn)
  301. {
  302. struct sk_buff *skb = c3cn->cpl_abort_req;
  303. struct cpl_abort_req *req;
  304. unsigned int tid = c3cn->tid;
  305. if (unlikely(c3cn->state == C3CN_STATE_ABORTING) || !skb ||
  306. !c3cn->cdev)
  307. return;
  308. c3cn_set_state(c3cn, C3CN_STATE_ABORTING);
  309. c3cn_conn_debug("c3cn 0x%p, flag ABORT_RPL + ABORT_SHUT.\n", c3cn);
  310. c3cn_set_flag(c3cn, C3CN_ABORT_RPL_PENDING);
  311. /* Purge the send queue so we don't send anything after an abort. */
  312. c3cn_purge_write_queue(c3cn);
  313. c3cn->cpl_abort_req = NULL;
  314. req = (struct cpl_abort_req *)skb->head;
  315. skb->priority = CPL_PRIORITY_DATA;
  316. set_arp_failure_handler(skb, abort_arp_failure);
  317. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
  318. req->wr.wr_lo = htonl(V_WR_TID(tid));
  319. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, tid));
  320. req->rsvd0 = htonl(c3cn->snd_nxt);
  321. req->rsvd1 = !c3cn_flag(c3cn, C3CN_TX_DATA_SENT);
  322. req->cmd = CPL_ABORT_SEND_RST;
  323. l2t_send(c3cn->cdev, skb, c3cn->l2t);
  324. }
  325. /*
  326. * CPL connection abort reply: host ->
  327. *
  328. * Send an ABORT_RPL message in response of the ABORT_REQ received.
  329. */
  330. static void send_abort_rpl(struct s3_conn *c3cn, int rst_status)
  331. {
  332. struct sk_buff *skb = c3cn->cpl_abort_rpl;
  333. struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;
  334. c3cn->cpl_abort_rpl = NULL;
  335. skb->priority = CPL_PRIORITY_DATA;
  336. rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
  337. rpl->wr.wr_lo = htonl(V_WR_TID(c3cn->tid));
  338. OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, c3cn->tid));
  339. rpl->cmd = rst_status;
  340. cxgb3_ofld_send(c3cn->cdev, skb);
  341. }
  342. /*
  343. * CPL connection rx data ack: host ->
  344. * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
  345. * credits sent.
  346. */
  347. static u32 send_rx_credits(struct s3_conn *c3cn, u32 credits, u32 dack)
  348. {
  349. struct sk_buff *skb;
  350. struct cpl_rx_data_ack *req;
  351. skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
  352. if (!skb)
  353. return 0;
  354. req = (struct cpl_rx_data_ack *)__skb_put(skb, sizeof(*req));
  355. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  356. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, c3cn->tid));
  357. req->credit_dack = htonl(dack | V_RX_CREDITS(credits));
  358. skb->priority = CPL_PRIORITY_ACK;
  359. cxgb3_ofld_send(c3cn->cdev, skb);
  360. return credits;
  361. }
  362. /*
  363. * CPL connection tx data: host ->
  364. *
  365. * Send iscsi PDU via TX_DATA CPL message. Returns the number of
  366. * credits sent.
  367. * Each TX_DATA consumes work request credit (wrs), so we need to keep track of
  368. * how many we've used so far and how many are pending (i.e., yet ack'ed by T3).
  369. */
  370. /*
  371. * For ULP connections HW may inserts digest bytes into the pdu. Those digest
  372. * bytes are not sent by the host but are part of the TCP payload and therefore
  373. * consume TCP sequence space.
  374. */
  375. static const unsigned int cxgb3_ulp_extra_len[] = { 0, 4, 4, 8 };
  376. static inline unsigned int ulp_extra_len(const struct sk_buff *skb)
  377. {
  378. return cxgb3_ulp_extra_len[skb_ulp_mode(skb) & 3];
  379. }
  380. static unsigned int wrlen __read_mostly;
  381. /*
  382. * The number of WRs needed for an skb depends on the number of fragments
  383. * in the skb and whether it has any payload in its main body. This maps the
  384. * length of the gather list represented by an skb into the # of necessary WRs.
  385. *
  386. * The max. length of an skb is controlled by the max pdu size which is ~16K.
  387. * Also, assume the min. fragment length is the sector size (512), then add
  388. * extra fragment counts for iscsi bhs and payload padding.
  389. */
  390. #define SKB_WR_LIST_SIZE (16384/512 + 3)
  391. static unsigned int skb_wrs[SKB_WR_LIST_SIZE] __read_mostly;
  392. static void s3_init_wr_tab(unsigned int wr_len)
  393. {
  394. int i;
  395. if (skb_wrs[1]) /* already initialized */
  396. return;
  397. for (i = 1; i < SKB_WR_LIST_SIZE; i++) {
  398. int sgl_len = (3 * i) / 2 + (i & 1);
  399. sgl_len += 3;
  400. skb_wrs[i] = (sgl_len <= wr_len
  401. ? 1 : 1 + (sgl_len - 2) / (wr_len - 1));
  402. }
  403. wrlen = wr_len * 8;
  404. }
  405. static inline void reset_wr_list(struct s3_conn *c3cn)
  406. {
  407. c3cn->wr_pending_head = NULL;
  408. }
  409. /*
  410. * Add a WR to a connections's list of pending WRs. This is a singly-linked
  411. * list of sk_buffs operating as a FIFO. The head is kept in wr_pending_head
  412. * and the tail in wr_pending_tail.
  413. */
  414. static inline void enqueue_wr(struct s3_conn *c3cn,
  415. struct sk_buff *skb)
  416. {
  417. skb_wr_data(skb) = NULL;
  418. /*
  419. * We want to take an extra reference since both us and the driver
  420. * need to free the packet before it's really freed. We know there's
  421. * just one user currently so we use atomic_set rather than skb_get
  422. * to avoid the atomic op.
  423. */
  424. atomic_set(&skb->users, 2);
  425. if (!c3cn->wr_pending_head)
  426. c3cn->wr_pending_head = skb;
  427. else
  428. skb_wr_data(skb) = skb;
  429. c3cn->wr_pending_tail = skb;
  430. }
  431. static inline struct sk_buff *peek_wr(const struct s3_conn *c3cn)
  432. {
  433. return c3cn->wr_pending_head;
  434. }
  435. static inline void free_wr_skb(struct sk_buff *skb)
  436. {
  437. kfree_skb(skb);
  438. }
  439. static inline struct sk_buff *dequeue_wr(struct s3_conn *c3cn)
  440. {
  441. struct sk_buff *skb = c3cn->wr_pending_head;
  442. if (likely(skb)) {
  443. /* Don't bother clearing the tail */
  444. c3cn->wr_pending_head = skb_wr_data(skb);
  445. skb_wr_data(skb) = NULL;
  446. }
  447. return skb;
  448. }
  449. static void purge_wr_queue(struct s3_conn *c3cn)
  450. {
  451. struct sk_buff *skb;
  452. while ((skb = dequeue_wr(c3cn)) != NULL)
  453. free_wr_skb(skb);
  454. }
  455. static inline void make_tx_data_wr(struct s3_conn *c3cn, struct sk_buff *skb,
  456. int len)
  457. {
  458. struct tx_data_wr *req;
  459. skb_reset_transport_header(skb);
  460. req = (struct tx_data_wr *)__skb_push(skb, sizeof(*req));
  461. req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA));
  462. req->wr_lo = htonl(V_WR_TID(c3cn->tid));
  463. req->sndseq = htonl(c3cn->snd_nxt);
  464. /* len includes the length of any HW ULP additions */
  465. req->len = htonl(len);
  466. req->param = htonl(V_TX_PORT(c3cn->l2t->smt_idx));
  467. /* V_TX_ULP_SUBMODE sets both the mode and submode */
  468. req->flags = htonl(V_TX_ULP_SUBMODE(skb_ulp_mode(skb)) |
  469. V_TX_SHOVE((skb_peek(&c3cn->write_queue) ? 0 : 1)));
  470. if (!c3cn_flag(c3cn, C3CN_TX_DATA_SENT)) {
  471. req->flags |= htonl(V_TX_ACK_PAGES(2) | F_TX_INIT |
  472. V_TX_CPU_IDX(c3cn->qset));
  473. /* Sendbuffer is in units of 32KB. */
  474. req->param |= htonl(V_TX_SNDBUF(cxgb3_snd_win >> 15));
  475. c3cn_set_flag(c3cn, C3CN_TX_DATA_SENT);
  476. }
  477. }
  478. /**
  479. * c3cn_push_tx_frames -- start transmit
  480. * @c3cn: the offloaded connection
  481. * @req_completion: request wr_ack or not
  482. *
  483. * Prepends TX_DATA_WR or CPL_CLOSE_CON_REQ headers to buffers waiting in a
  484. * connection's send queue and sends them on to T3. Must be called with the
  485. * connection's lock held. Returns the amount of send buffer space that was
  486. * freed as a result of sending queued data to T3.
  487. */
  488. static void arp_failure_discard(struct t3cdev *cdev, struct sk_buff *skb)
  489. {
  490. kfree_skb(skb);
  491. }
  492. static int c3cn_push_tx_frames(struct s3_conn *c3cn, int req_completion)
  493. {
  494. int total_size = 0;
  495. struct sk_buff *skb;
  496. struct t3cdev *cdev;
  497. struct cxgb3i_sdev_data *cdata;
  498. if (unlikely(c3cn->state == C3CN_STATE_CONNECTING ||
  499. c3cn->state == C3CN_STATE_CLOSE_WAIT_1 ||
  500. c3cn->state == C3CN_STATE_ABORTING)) {
  501. c3cn_tx_debug("c3cn 0x%p, in closing state %u.\n",
  502. c3cn, c3cn->state);
  503. return 0;
  504. }
  505. cdev = c3cn->cdev;
  506. cdata = CXGB3_SDEV_DATA(cdev);
  507. while (c3cn->wr_avail
  508. && (skb = skb_peek(&c3cn->write_queue)) != NULL) {
  509. int len = skb->len; /* length before skb_push */
  510. int frags = skb_shinfo(skb)->nr_frags + (len != skb->data_len);
  511. int wrs_needed = skb_wrs[frags];
  512. if (wrs_needed > 1 && len + sizeof(struct tx_data_wr) <= wrlen)
  513. wrs_needed = 1;
  514. WARN_ON(frags >= SKB_WR_LIST_SIZE || wrs_needed < 1);
  515. if (c3cn->wr_avail < wrs_needed) {
  516. c3cn_tx_debug("c3cn 0x%p, skb len %u/%u, frag %u, "
  517. "wr %d < %u.\n",
  518. c3cn, skb->len, skb->datalen, frags,
  519. wrs_needed, c3cn->wr_avail);
  520. break;
  521. }
  522. __skb_unlink(skb, &c3cn->write_queue);
  523. skb->priority = CPL_PRIORITY_DATA;
  524. skb->csum = wrs_needed; /* remember this until the WR_ACK */
  525. c3cn->wr_avail -= wrs_needed;
  526. c3cn->wr_unacked += wrs_needed;
  527. enqueue_wr(c3cn, skb);
  528. if (likely(CXGB3_SKB_CB(skb)->flags & C3CB_FLAG_NEED_HDR)) {
  529. len += ulp_extra_len(skb);
  530. make_tx_data_wr(c3cn, skb, len);
  531. c3cn->snd_nxt += len;
  532. if ((req_completion
  533. && c3cn->wr_unacked == wrs_needed)
  534. || (CXGB3_SKB_CB(skb)->flags & C3CB_FLAG_COMPL)
  535. || c3cn->wr_unacked >= c3cn->wr_max / 2) {
  536. struct work_request_hdr *wr = cplhdr(skb);
  537. wr->wr_hi |= htonl(F_WR_COMPL);
  538. c3cn->wr_unacked = 0;
  539. }
  540. CXGB3_SKB_CB(skb)->flags &= ~C3CB_FLAG_NEED_HDR;
  541. }
  542. total_size += skb->truesize;
  543. set_arp_failure_handler(skb, arp_failure_discard);
  544. l2t_send(cdev, skb, c3cn->l2t);
  545. }
  546. return total_size;
  547. }
  548. /*
  549. * process_cpl_msg: -> host
  550. * Top-level CPL message processing used by most CPL messages that
  551. * pertain to connections.
  552. */
  553. static inline void process_cpl_msg(void (*fn)(struct s3_conn *,
  554. struct sk_buff *),
  555. struct s3_conn *c3cn,
  556. struct sk_buff *skb)
  557. {
  558. spin_lock_bh(&c3cn->lock);
  559. fn(c3cn, skb);
  560. spin_unlock_bh(&c3cn->lock);
  561. }
  562. /*
  563. * process_cpl_msg_ref: -> host
  564. * Similar to process_cpl_msg() but takes an extra connection reference around
  565. * the call to the handler. Should be used if the handler may drop a
  566. * connection reference.
  567. */
  568. static inline void process_cpl_msg_ref(void (*fn) (struct s3_conn *,
  569. struct sk_buff *),
  570. struct s3_conn *c3cn,
  571. struct sk_buff *skb)
  572. {
  573. c3cn_hold(c3cn);
  574. process_cpl_msg(fn, c3cn, skb);
  575. c3cn_put(c3cn);
  576. }
  577. /*
  578. * Process a CPL_ACT_ESTABLISH message: -> host
  579. * Updates connection state from an active establish CPL message. Runs with
  580. * the connection lock held.
  581. */
  582. static inline void s3_free_atid(struct t3cdev *cdev, unsigned int tid)
  583. {
  584. struct s3_conn *c3cn = cxgb3_free_atid(cdev, tid);
  585. if (c3cn)
  586. c3cn_put(c3cn);
  587. }
  588. static void c3cn_established(struct s3_conn *c3cn, u32 snd_isn,
  589. unsigned int opt)
  590. {
  591. c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
  592. c3cn->write_seq = c3cn->snd_nxt = c3cn->snd_una = snd_isn;
  593. /*
  594. * Causes the first RX_DATA_ACK to supply any Rx credits we couldn't
  595. * pass through opt0.
  596. */
  597. if (cxgb3_rcv_win > (M_RCV_BUFSIZ << 10))
  598. c3cn->rcv_wup -= cxgb3_rcv_win - (M_RCV_BUFSIZ << 10);
  599. dst_confirm(c3cn->dst_cache);
  600. smp_mb();
  601. c3cn_set_state(c3cn, C3CN_STATE_ESTABLISHED);
  602. }
  603. static void process_act_establish(struct s3_conn *c3cn, struct sk_buff *skb)
  604. {
  605. struct cpl_act_establish *req = cplhdr(skb);
  606. u32 rcv_isn = ntohl(req->rcv_isn); /* real RCV_ISN + 1 */
  607. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  608. c3cn, c3cn->state, c3cn->flags);
  609. if (unlikely(c3cn->state != C3CN_STATE_CONNECTING))
  610. cxgb3i_log_error("TID %u expected SYN_SENT, got EST., s %u\n",
  611. c3cn->tid, c3cn->state);
  612. c3cn->copied_seq = c3cn->rcv_wup = c3cn->rcv_nxt = rcv_isn;
  613. c3cn_established(c3cn, ntohl(req->snd_isn), ntohs(req->tcp_opt));
  614. __kfree_skb(skb);
  615. if (unlikely(c3cn_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED)))
  616. /* upper layer has requested closing */
  617. send_abort_req(c3cn);
  618. else if (c3cn_push_tx_frames(c3cn, 1))
  619. cxgb3i_conn_tx_open(c3cn);
  620. }
  621. static int do_act_establish(struct t3cdev *cdev, struct sk_buff *skb,
  622. void *ctx)
  623. {
  624. struct cpl_act_establish *req = cplhdr(skb);
  625. unsigned int tid = GET_TID(req);
  626. unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
  627. struct s3_conn *c3cn = ctx;
  628. struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
  629. c3cn_conn_debug("rcv, tid 0x%x, c3cn 0x%p, s %u, f 0x%lx.\n",
  630. tid, c3cn, c3cn->state, c3cn->flags);
  631. c3cn->tid = tid;
  632. c3cn_hold(c3cn);
  633. cxgb3_insert_tid(cdata->cdev, cdata->client, c3cn, tid);
  634. s3_free_atid(cdev, atid);
  635. c3cn->qset = G_QNUM(ntohl(skb->csum));
  636. process_cpl_msg(process_act_establish, c3cn, skb);
  637. return 0;
  638. }
  639. /*
  640. * Process a CPL_ACT_OPEN_RPL message: -> host
  641. * Handle active open failures.
  642. */
  643. static int act_open_rpl_status_to_errno(int status)
  644. {
  645. switch (status) {
  646. case CPL_ERR_CONN_RESET:
  647. return ECONNREFUSED;
  648. case CPL_ERR_ARP_MISS:
  649. return EHOSTUNREACH;
  650. case CPL_ERR_CONN_TIMEDOUT:
  651. return ETIMEDOUT;
  652. case CPL_ERR_TCAM_FULL:
  653. return ENOMEM;
  654. case CPL_ERR_CONN_EXIST:
  655. cxgb3i_log_error("ACTIVE_OPEN_RPL: 4-tuple in use\n");
  656. return EADDRINUSE;
  657. default:
  658. return EIO;
  659. }
  660. }
  661. static void act_open_retry_timer(unsigned long data)
  662. {
  663. struct sk_buff *skb;
  664. struct s3_conn *c3cn = (struct s3_conn *)data;
  665. c3cn_conn_debug("c3cn 0x%p, state %u.\n", c3cn, c3cn->state);
  666. spin_lock_bh(&c3cn->lock);
  667. skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_ATOMIC);
  668. if (!skb)
  669. fail_act_open(c3cn, ENOMEM);
  670. else {
  671. skb->sk = (struct sock *)c3cn;
  672. set_arp_failure_handler(skb, act_open_req_arp_failure);
  673. make_act_open_req(c3cn, skb, c3cn->tid, c3cn->l2t);
  674. l2t_send(c3cn->cdev, skb, c3cn->l2t);
  675. }
  676. spin_unlock_bh(&c3cn->lock);
  677. c3cn_put(c3cn);
  678. }
  679. static void process_act_open_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
  680. {
  681. struct cpl_act_open_rpl *rpl = cplhdr(skb);
  682. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  683. c3cn, c3cn->state, c3cn->flags);
  684. if (rpl->status == CPL_ERR_CONN_EXIST &&
  685. c3cn->retry_timer.function != act_open_retry_timer) {
  686. c3cn->retry_timer.function = act_open_retry_timer;
  687. if (!mod_timer(&c3cn->retry_timer, jiffies + HZ / 2))
  688. c3cn_hold(c3cn);
  689. } else
  690. fail_act_open(c3cn, act_open_rpl_status_to_errno(rpl->status));
  691. __kfree_skb(skb);
  692. }
  693. static int do_act_open_rpl(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  694. {
  695. struct s3_conn *c3cn = ctx;
  696. struct cpl_act_open_rpl *rpl = cplhdr(skb);
  697. c3cn_conn_debug("rcv, status 0x%x, c3cn 0x%p, s %u, f 0x%lx.\n",
  698. rpl->status, c3cn, c3cn->state, c3cn->flags);
  699. if (rpl->status != CPL_ERR_TCAM_FULL &&
  700. rpl->status != CPL_ERR_CONN_EXIST &&
  701. rpl->status != CPL_ERR_ARP_MISS)
  702. cxgb3_queue_tid_release(cdev, GET_TID(rpl));
  703. process_cpl_msg_ref(process_act_open_rpl, c3cn, skb);
  704. return 0;
  705. }
  706. /*
  707. * Process PEER_CLOSE CPL messages: -> host
  708. * Handle peer FIN.
  709. */
  710. static void process_peer_close(struct s3_conn *c3cn, struct sk_buff *skb)
  711. {
  712. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  713. c3cn, c3cn->state, c3cn->flags);
  714. if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING))
  715. goto out;
  716. switch (c3cn->state) {
  717. case C3CN_STATE_ESTABLISHED:
  718. c3cn_set_state(c3cn, C3CN_STATE_PASSIVE_CLOSE);
  719. break;
  720. case C3CN_STATE_ACTIVE_CLOSE:
  721. c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_2);
  722. break;
  723. case C3CN_STATE_CLOSE_WAIT_1:
  724. c3cn_closed(c3cn);
  725. break;
  726. case C3CN_STATE_ABORTING:
  727. break;
  728. default:
  729. cxgb3i_log_error("%s: peer close, TID %u in bad state %u\n",
  730. c3cn->cdev->name, c3cn->tid, c3cn->state);
  731. }
  732. cxgb3i_conn_closing(c3cn);
  733. out:
  734. __kfree_skb(skb);
  735. }
  736. static int do_peer_close(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  737. {
  738. struct s3_conn *c3cn = ctx;
  739. c3cn_conn_debug("rcv, c3cn 0x%p, s %u, f 0x%lx.\n",
  740. c3cn, c3cn->state, c3cn->flags);
  741. process_cpl_msg_ref(process_peer_close, c3cn, skb);
  742. return 0;
  743. }
  744. /*
  745. * Process CLOSE_CONN_RPL CPL message: -> host
  746. * Process a peer ACK to our FIN.
  747. */
  748. static void process_close_con_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
  749. {
  750. struct cpl_close_con_rpl *rpl = cplhdr(skb);
  751. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  752. c3cn, c3cn->state, c3cn->flags);
  753. c3cn->snd_una = ntohl(rpl->snd_nxt) - 1; /* exclude FIN */
  754. if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING))
  755. goto out;
  756. switch (c3cn->state) {
  757. case C3CN_STATE_ACTIVE_CLOSE:
  758. c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_1);
  759. break;
  760. case C3CN_STATE_CLOSE_WAIT_1:
  761. case C3CN_STATE_CLOSE_WAIT_2:
  762. c3cn_closed(c3cn);
  763. break;
  764. case C3CN_STATE_ABORTING:
  765. break;
  766. default:
  767. cxgb3i_log_error("%s: close_rpl, TID %u in bad state %u\n",
  768. c3cn->cdev->name, c3cn->tid, c3cn->state);
  769. }
  770. out:
  771. kfree_skb(skb);
  772. }
  773. static int do_close_con_rpl(struct t3cdev *cdev, struct sk_buff *skb,
  774. void *ctx)
  775. {
  776. struct s3_conn *c3cn = ctx;
  777. c3cn_conn_debug("rcv, c3cn 0x%p, s %u, f 0x%lx.\n",
  778. c3cn, c3cn->state, c3cn->flags);
  779. process_cpl_msg_ref(process_close_con_rpl, c3cn, skb);
  780. return 0;
  781. }
  782. /*
  783. * Process ABORT_REQ_RSS CPL message: -> host
  784. * Process abort requests. If we are waiting for an ABORT_RPL we ignore this
  785. * request except that we need to reply to it.
  786. */
  787. static int abort_status_to_errno(struct s3_conn *c3cn, int abort_reason,
  788. int *need_rst)
  789. {
  790. switch (abort_reason) {
  791. case CPL_ERR_BAD_SYN: /* fall through */
  792. case CPL_ERR_CONN_RESET:
  793. return c3cn->state > C3CN_STATE_ESTABLISHED ?
  794. EPIPE : ECONNRESET;
  795. case CPL_ERR_XMIT_TIMEDOUT:
  796. case CPL_ERR_PERSIST_TIMEDOUT:
  797. case CPL_ERR_FINWAIT2_TIMEDOUT:
  798. case CPL_ERR_KEEPALIVE_TIMEDOUT:
  799. return ETIMEDOUT;
  800. default:
  801. return EIO;
  802. }
  803. }
  804. static void process_abort_req(struct s3_conn *c3cn, struct sk_buff *skb)
  805. {
  806. int rst_status = CPL_ABORT_NO_RST;
  807. const struct cpl_abort_req_rss *req = cplhdr(skb);
  808. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  809. c3cn, c3cn->state, c3cn->flags);
  810. if (!c3cn_flag(c3cn, C3CN_ABORT_REQ_RCVD)) {
  811. c3cn_set_flag(c3cn, C3CN_ABORT_REQ_RCVD);
  812. c3cn_set_state(c3cn, C3CN_STATE_ABORTING);
  813. __kfree_skb(skb);
  814. return;
  815. }
  816. c3cn_clear_flag(c3cn, C3CN_ABORT_REQ_RCVD);
  817. send_abort_rpl(c3cn, rst_status);
  818. if (!c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING)) {
  819. c3cn->err =
  820. abort_status_to_errno(c3cn, req->status, &rst_status);
  821. c3cn_closed(c3cn);
  822. }
  823. }
  824. static int do_abort_req(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  825. {
  826. const struct cpl_abort_req_rss *req = cplhdr(skb);
  827. struct s3_conn *c3cn = ctx;
  828. c3cn_conn_debug("rcv, c3cn 0x%p, s 0x%x, f 0x%lx.\n",
  829. c3cn, c3cn->state, c3cn->flags);
  830. if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
  831. req->status == CPL_ERR_PERSIST_NEG_ADVICE) {
  832. __kfree_skb(skb);
  833. return 0;
  834. }
  835. process_cpl_msg_ref(process_abort_req, c3cn, skb);
  836. return 0;
  837. }
  838. /*
  839. * Process ABORT_RPL_RSS CPL message: -> host
  840. * Process abort replies. We only process these messages if we anticipate
  841. * them as the coordination between SW and HW in this area is somewhat lacking
  842. * and sometimes we get ABORT_RPLs after we are done with the connection that
  843. * originated the ABORT_REQ.
  844. */
  845. static void process_abort_rpl(struct s3_conn *c3cn, struct sk_buff *skb)
  846. {
  847. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  848. c3cn, c3cn->state, c3cn->flags);
  849. if (c3cn_flag(c3cn, C3CN_ABORT_RPL_PENDING)) {
  850. if (!c3cn_flag(c3cn, C3CN_ABORT_RPL_RCVD))
  851. c3cn_set_flag(c3cn, C3CN_ABORT_RPL_RCVD);
  852. else {
  853. c3cn_clear_flag(c3cn, C3CN_ABORT_RPL_RCVD);
  854. c3cn_clear_flag(c3cn, C3CN_ABORT_RPL_PENDING);
  855. if (c3cn_flag(c3cn, C3CN_ABORT_REQ_RCVD))
  856. cxgb3i_log_error("%s tid %u, ABORT_RPL_RSS\n",
  857. c3cn->cdev->name, c3cn->tid);
  858. c3cn_closed(c3cn);
  859. }
  860. }
  861. __kfree_skb(skb);
  862. }
  863. static int do_abort_rpl(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  864. {
  865. struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
  866. struct s3_conn *c3cn = ctx;
  867. c3cn_conn_debug("rcv, status 0x%x, c3cn 0x%p, s %u, 0x%lx.\n",
  868. rpl->status, c3cn, c3cn ? c3cn->state : 0,
  869. c3cn ? c3cn->flags : 0UL);
  870. /*
  871. * Ignore replies to post-close aborts indicating that the abort was
  872. * requested too late. These connections are terminated when we get
  873. * PEER_CLOSE or CLOSE_CON_RPL and by the time the abort_rpl_rss
  874. * arrives the TID is either no longer used or it has been recycled.
  875. */
  876. if (rpl->status == CPL_ERR_ABORT_FAILED)
  877. goto discard;
  878. /*
  879. * Sometimes we've already closed the connection, e.g., a post-close
  880. * abort races with ABORT_REQ_RSS, the latter frees the connection
  881. * expecting the ABORT_REQ will fail with CPL_ERR_ABORT_FAILED,
  882. * but FW turns the ABORT_REQ into a regular one and so we get
  883. * ABORT_RPL_RSS with status 0 and no connection.
  884. */
  885. if (!c3cn)
  886. goto discard;
  887. process_cpl_msg_ref(process_abort_rpl, c3cn, skb);
  888. return 0;
  889. discard:
  890. __kfree_skb(skb);
  891. return 0;
  892. }
  893. /*
  894. * Process RX_ISCSI_HDR CPL message: -> host
  895. * Handle received PDUs, the payload could be DDP'ed. If not, the payload
  896. * follow after the bhs.
  897. */
  898. static void process_rx_iscsi_hdr(struct s3_conn *c3cn, struct sk_buff *skb)
  899. {
  900. struct cpl_iscsi_hdr *hdr_cpl = cplhdr(skb);
  901. struct cpl_iscsi_hdr_norss data_cpl;
  902. struct cpl_rx_data_ddp_norss ddp_cpl;
  903. unsigned int hdr_len, data_len, status;
  904. unsigned int len;
  905. int err;
  906. if (unlikely(c3cn->state >= C3CN_STATE_PASSIVE_CLOSE)) {
  907. if (c3cn->state != C3CN_STATE_ABORTING)
  908. send_abort_req(c3cn);
  909. __kfree_skb(skb);
  910. return;
  911. }
  912. CXGB3_SKB_CB(skb)->seq = ntohl(hdr_cpl->seq);
  913. CXGB3_SKB_CB(skb)->flags = 0;
  914. skb_reset_transport_header(skb);
  915. __skb_pull(skb, sizeof(struct cpl_iscsi_hdr));
  916. len = hdr_len = ntohs(hdr_cpl->len);
  917. /* msg coalesce is off or not enough data received */
  918. if (skb->len <= hdr_len) {
  919. cxgb3i_log_error("%s: TID %u, ISCSI_HDR, skb len %u < %u.\n",
  920. c3cn->cdev->name, c3cn->tid,
  921. skb->len, hdr_len);
  922. goto abort_conn;
  923. }
  924. err = skb_copy_bits(skb, skb->len - sizeof(ddp_cpl), &ddp_cpl,
  925. sizeof(ddp_cpl));
  926. if (err < 0)
  927. goto abort_conn;
  928. skb_ulp_mode(skb) = ULP2_FLAG_DATA_READY;
  929. skb_ulp_pdulen(skb) = ntohs(ddp_cpl.len);
  930. skb_ulp_ddigest(skb) = ntohl(ddp_cpl.ulp_crc);
  931. status = ntohl(ddp_cpl.ddp_status);
  932. c3cn_rx_debug("rx skb 0x%p, len %u, pdulen %u, ddp status 0x%x.\n",
  933. skb, skb->len, skb_ulp_pdulen(skb), status);
  934. if (status & (1 << RX_DDP_STATUS_HCRC_SHIFT))
  935. skb_ulp_mode(skb) |= ULP2_FLAG_HCRC_ERROR;
  936. if (status & (1 << RX_DDP_STATUS_DCRC_SHIFT))
  937. skb_ulp_mode(skb) |= ULP2_FLAG_DCRC_ERROR;
  938. if (status & (1 << RX_DDP_STATUS_PAD_SHIFT))
  939. skb_ulp_mode(skb) |= ULP2_FLAG_PAD_ERROR;
  940. if (skb->len > (hdr_len + sizeof(ddp_cpl))) {
  941. err = skb_copy_bits(skb, hdr_len, &data_cpl, sizeof(data_cpl));
  942. if (err < 0)
  943. goto abort_conn;
  944. data_len = ntohs(data_cpl.len);
  945. len += sizeof(data_cpl) + data_len;
  946. } else if (status & (1 << RX_DDP_STATUS_DDP_SHIFT))
  947. skb_ulp_mode(skb) |= ULP2_FLAG_DATA_DDPED;
  948. c3cn->rcv_nxt = ntohl(ddp_cpl.seq) + skb_ulp_pdulen(skb);
  949. __pskb_trim(skb, len);
  950. __skb_queue_tail(&c3cn->receive_queue, skb);
  951. cxgb3i_conn_pdu_ready(c3cn);
  952. return;
  953. abort_conn:
  954. send_abort_req(c3cn);
  955. __kfree_skb(skb);
  956. }
  957. static int do_iscsi_hdr(struct t3cdev *t3dev, struct sk_buff *skb, void *ctx)
  958. {
  959. struct s3_conn *c3cn = ctx;
  960. process_cpl_msg(process_rx_iscsi_hdr, c3cn, skb);
  961. return 0;
  962. }
  963. /*
  964. * Process TX_DATA_ACK CPL messages: -> host
  965. * Process an acknowledgment of WR completion. Advance snd_una and send the
  966. * next batch of work requests from the write queue.
  967. */
  968. static void process_wr_ack(struct s3_conn *c3cn, struct sk_buff *skb)
  969. {
  970. struct cpl_wr_ack *hdr = cplhdr(skb);
  971. unsigned int credits = ntohs(hdr->credits);
  972. u32 snd_una = ntohl(hdr->snd_una);
  973. c3cn->wr_avail += credits;
  974. if (c3cn->wr_unacked > c3cn->wr_max - c3cn->wr_avail)
  975. c3cn->wr_unacked = c3cn->wr_max - c3cn->wr_avail;
  976. while (credits) {
  977. struct sk_buff *p = peek_wr(c3cn);
  978. if (unlikely(!p)) {
  979. cxgb3i_log_error("%u WR_ACK credits for TID %u with "
  980. "nothing pending, state %u\n",
  981. credits, c3cn->tid, c3cn->state);
  982. break;
  983. }
  984. if (unlikely(credits < p->csum)) {
  985. p->csum -= credits;
  986. break;
  987. } else {
  988. dequeue_wr(c3cn);
  989. credits -= p->csum;
  990. free_wr_skb(p);
  991. }
  992. }
  993. if (unlikely(before(snd_una, c3cn->snd_una)))
  994. goto out_free;
  995. if (c3cn->snd_una != snd_una) {
  996. c3cn->snd_una = snd_una;
  997. dst_confirm(c3cn->dst_cache);
  998. }
  999. if (skb_queue_len(&c3cn->write_queue) && c3cn_push_tx_frames(c3cn, 0))
  1000. cxgb3i_conn_tx_open(c3cn);
  1001. out_free:
  1002. __kfree_skb(skb);
  1003. }
  1004. static int do_wr_ack(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  1005. {
  1006. struct s3_conn *c3cn = ctx;
  1007. process_cpl_msg(process_wr_ack, c3cn, skb);
  1008. return 0;
  1009. }
  1010. /*
  1011. * for each connection, pre-allocate skbs needed for close/abort requests. So
  1012. * that we can service the request right away.
  1013. */
  1014. static void c3cn_free_cpl_skbs(struct s3_conn *c3cn)
  1015. {
  1016. if (c3cn->cpl_close)
  1017. kfree_skb(c3cn->cpl_close);
  1018. if (c3cn->cpl_abort_req)
  1019. kfree_skb(c3cn->cpl_abort_req);
  1020. if (c3cn->cpl_abort_rpl)
  1021. kfree_skb(c3cn->cpl_abort_rpl);
  1022. }
  1023. static int c3cn_alloc_cpl_skbs(struct s3_conn *c3cn)
  1024. {
  1025. c3cn->cpl_close = alloc_skb(sizeof(struct cpl_close_con_req),
  1026. GFP_KERNEL);
  1027. if (!c3cn->cpl_close)
  1028. return -ENOMEM;
  1029. skb_put(c3cn->cpl_close, sizeof(struct cpl_close_con_req));
  1030. c3cn->cpl_abort_req = alloc_skb(sizeof(struct cpl_abort_req),
  1031. GFP_KERNEL);
  1032. if (!c3cn->cpl_abort_req)
  1033. goto free_cpl_skbs;
  1034. skb_put(c3cn->cpl_abort_req, sizeof(struct cpl_abort_req));
  1035. c3cn->cpl_abort_rpl = alloc_skb(sizeof(struct cpl_abort_rpl),
  1036. GFP_KERNEL);
  1037. if (!c3cn->cpl_abort_rpl)
  1038. goto free_cpl_skbs;
  1039. skb_put(c3cn->cpl_abort_rpl, sizeof(struct cpl_abort_rpl));
  1040. return 0;
  1041. free_cpl_skbs:
  1042. c3cn_free_cpl_skbs(c3cn);
  1043. return -ENOMEM;
  1044. }
  1045. /**
  1046. * c3cn_release_offload_resources - release offload resource
  1047. * @c3cn: the offloaded iscsi tcp connection.
  1048. * Release resources held by an offload connection (TID, L2T entry, etc.)
  1049. */
  1050. static void c3cn_release_offload_resources(struct s3_conn *c3cn)
  1051. {
  1052. struct t3cdev *cdev = c3cn->cdev;
  1053. unsigned int tid = c3cn->tid;
  1054. if (!cdev)
  1055. return;
  1056. c3cn->qset = 0;
  1057. c3cn_free_cpl_skbs(c3cn);
  1058. if (c3cn->wr_avail != c3cn->wr_max) {
  1059. purge_wr_queue(c3cn);
  1060. reset_wr_list(c3cn);
  1061. }
  1062. if (c3cn->l2t) {
  1063. l2t_release(L2DATA(cdev), c3cn->l2t);
  1064. c3cn->l2t = NULL;
  1065. }
  1066. if (c3cn->state == C3CN_STATE_CONNECTING) /* we have ATID */
  1067. s3_free_atid(cdev, tid);
  1068. else { /* we have TID */
  1069. cxgb3_remove_tid(cdev, (void *)c3cn, tid);
  1070. c3cn_put(c3cn);
  1071. }
  1072. c3cn->cdev = NULL;
  1073. }
  1074. /**
  1075. * cxgb3i_c3cn_create - allocate and initialize an s3_conn structure
  1076. * returns the s3_conn structure allocated.
  1077. */
  1078. struct s3_conn *cxgb3i_c3cn_create(void)
  1079. {
  1080. struct s3_conn *c3cn;
  1081. c3cn = kzalloc(sizeof(*c3cn), GFP_KERNEL);
  1082. if (!c3cn)
  1083. return NULL;
  1084. /* pre-allocate close/abort cpl, so we don't need to wait for memory
  1085. when close/abort is requested. */
  1086. if (c3cn_alloc_cpl_skbs(c3cn) < 0)
  1087. goto free_c3cn;
  1088. c3cn_conn_debug("alloc c3cn 0x%p.\n", c3cn);
  1089. c3cn->flags = 0;
  1090. spin_lock_init(&c3cn->lock);
  1091. atomic_set(&c3cn->refcnt, 1);
  1092. skb_queue_head_init(&c3cn->receive_queue);
  1093. skb_queue_head_init(&c3cn->write_queue);
  1094. setup_timer(&c3cn->retry_timer, NULL, (unsigned long)c3cn);
  1095. rwlock_init(&c3cn->callback_lock);
  1096. return c3cn;
  1097. free_c3cn:
  1098. kfree(c3cn);
  1099. return NULL;
  1100. }
  1101. static void c3cn_active_close(struct s3_conn *c3cn)
  1102. {
  1103. int data_lost;
  1104. int close_req = 0;
  1105. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  1106. c3cn, c3cn->state, c3cn->flags);
  1107. dst_confirm(c3cn->dst_cache);
  1108. c3cn_hold(c3cn);
  1109. spin_lock_bh(&c3cn->lock);
  1110. data_lost = skb_queue_len(&c3cn->receive_queue);
  1111. __skb_queue_purge(&c3cn->receive_queue);
  1112. switch (c3cn->state) {
  1113. case C3CN_STATE_CLOSED:
  1114. case C3CN_STATE_ACTIVE_CLOSE:
  1115. case C3CN_STATE_CLOSE_WAIT_1:
  1116. case C3CN_STATE_CLOSE_WAIT_2:
  1117. case C3CN_STATE_ABORTING:
  1118. /* nothing need to be done */
  1119. break;
  1120. case C3CN_STATE_CONNECTING:
  1121. /* defer until cpl_act_open_rpl or cpl_act_establish */
  1122. c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
  1123. break;
  1124. case C3CN_STATE_ESTABLISHED:
  1125. close_req = 1;
  1126. c3cn_set_state(c3cn, C3CN_STATE_ACTIVE_CLOSE);
  1127. break;
  1128. case C3CN_STATE_PASSIVE_CLOSE:
  1129. close_req = 1;
  1130. c3cn_set_state(c3cn, C3CN_STATE_CLOSE_WAIT_2);
  1131. break;
  1132. }
  1133. if (close_req) {
  1134. if (data_lost)
  1135. /* Unread data was tossed, zap the connection. */
  1136. send_abort_req(c3cn);
  1137. else
  1138. send_close_req(c3cn);
  1139. }
  1140. spin_unlock_bh(&c3cn->lock);
  1141. c3cn_put(c3cn);
  1142. }
  1143. /**
  1144. * cxgb3i_c3cn_release - close and release an iscsi tcp connection and any
  1145. * resource held
  1146. * @c3cn: the iscsi tcp connection
  1147. */
  1148. void cxgb3i_c3cn_release(struct s3_conn *c3cn)
  1149. {
  1150. c3cn_conn_debug("c3cn 0x%p, s %u, f 0x%lx.\n",
  1151. c3cn, c3cn->state, c3cn->flags);
  1152. if (likely(c3cn->state != C3CN_STATE_CONNECTING))
  1153. c3cn_active_close(c3cn);
  1154. else
  1155. c3cn_set_flag(c3cn, C3CN_ACTIVE_CLOSE_NEEDED);
  1156. c3cn_put(c3cn);
  1157. }
  1158. static int is_cxgb3_dev(struct net_device *dev)
  1159. {
  1160. struct cxgb3i_sdev_data *cdata;
  1161. write_lock(&cdata_rwlock);
  1162. list_for_each_entry(cdata, &cdata_list, list) {
  1163. struct adap_ports *ports = &cdata->ports;
  1164. int i;
  1165. for (i = 0; i < ports->nports; i++)
  1166. if (dev == ports->lldevs[i]) {
  1167. write_unlock(&cdata_rwlock);
  1168. return 1;
  1169. }
  1170. }
  1171. write_unlock(&cdata_rwlock);
  1172. return 0;
  1173. }
  1174. /**
  1175. * cxgb3_egress_dev - return the cxgb3 egress device
  1176. * @root_dev: the root device anchoring the search
  1177. * @c3cn: the connection used to determine egress port in bonding mode
  1178. * @context: in bonding mode, indicates a connection set up or failover
  1179. *
  1180. * Return egress device or NULL if the egress device isn't one of our ports.
  1181. */
  1182. static struct net_device *cxgb3_egress_dev(struct net_device *root_dev,
  1183. struct s3_conn *c3cn,
  1184. int context)
  1185. {
  1186. while (root_dev) {
  1187. if (root_dev->priv_flags & IFF_802_1Q_VLAN)
  1188. root_dev = vlan_dev_real_dev(root_dev);
  1189. else if (is_cxgb3_dev(root_dev))
  1190. return root_dev;
  1191. else
  1192. return NULL;
  1193. }
  1194. return NULL;
  1195. }
  1196. static struct rtable *find_route(__be32 saddr, __be32 daddr,
  1197. __be16 sport, __be16 dport)
  1198. {
  1199. struct rtable *rt;
  1200. struct flowi fl = {
  1201. .oif = 0,
  1202. .nl_u = {
  1203. .ip4_u = {
  1204. .daddr = daddr,
  1205. .saddr = saddr,
  1206. .tos = 0 } },
  1207. .proto = IPPROTO_TCP,
  1208. .uli_u = {
  1209. .ports = {
  1210. .sport = sport,
  1211. .dport = dport } } };
  1212. if (ip_route_output_flow(&init_net, &rt, &fl, NULL, 0))
  1213. return NULL;
  1214. return rt;
  1215. }
  1216. /*
  1217. * Assign offload parameters to some connection fields.
  1218. */
  1219. static void init_offload_conn(struct s3_conn *c3cn,
  1220. struct t3cdev *cdev,
  1221. struct dst_entry *dst)
  1222. {
  1223. BUG_ON(c3cn->cdev != cdev);
  1224. c3cn->wr_max = c3cn->wr_avail = T3C_DATA(cdev)->max_wrs;
  1225. c3cn->wr_unacked = 0;
  1226. c3cn->mss_idx = select_mss(c3cn, dst_mtu(dst));
  1227. reset_wr_list(c3cn);
  1228. }
  1229. static int initiate_act_open(struct s3_conn *c3cn, struct net_device *dev)
  1230. {
  1231. struct cxgb3i_sdev_data *cdata = NDEV2CDATA(dev);
  1232. struct t3cdev *cdev = cdata->cdev;
  1233. struct dst_entry *dst = c3cn->dst_cache;
  1234. struct sk_buff *skb;
  1235. c3cn_conn_debug("c3cn 0x%p, state %u, flag 0x%lx.\n",
  1236. c3cn, c3cn->state, c3cn->flags);
  1237. /*
  1238. * Initialize connection data. Note that the flags and ULP mode are
  1239. * initialized higher up ...
  1240. */
  1241. c3cn->dev = dev;
  1242. c3cn->cdev = cdev;
  1243. c3cn->tid = cxgb3_alloc_atid(cdev, cdata->client, c3cn);
  1244. if (c3cn->tid < 0)
  1245. goto out_err;
  1246. c3cn->qset = 0;
  1247. c3cn->l2t = t3_l2t_get(cdev, dst->neighbour, dev);
  1248. if (!c3cn->l2t)
  1249. goto free_tid;
  1250. skb = alloc_skb(sizeof(struct cpl_act_open_req), GFP_KERNEL);
  1251. if (!skb)
  1252. goto free_l2t;
  1253. skb->sk = (struct sock *)c3cn;
  1254. set_arp_failure_handler(skb, act_open_req_arp_failure);
  1255. c3cn_hold(c3cn);
  1256. init_offload_conn(c3cn, cdev, dst);
  1257. c3cn->err = 0;
  1258. make_act_open_req(c3cn, skb, c3cn->tid, c3cn->l2t);
  1259. l2t_send(cdev, skb, c3cn->l2t);
  1260. return 0;
  1261. free_l2t:
  1262. l2t_release(L2DATA(cdev), c3cn->l2t);
  1263. free_tid:
  1264. s3_free_atid(cdev, c3cn->tid);
  1265. c3cn->tid = 0;
  1266. out_err:
  1267. return -1;
  1268. }
  1269. /**
  1270. * cxgb3i_c3cn_connect - initiates an iscsi tcp connection to a given address
  1271. * @c3cn: the iscsi tcp connection
  1272. * @usin: destination address
  1273. *
  1274. * return 0 if active open request is sent, < 0 otherwise.
  1275. */
  1276. int cxgb3i_c3cn_connect(struct s3_conn *c3cn, struct sockaddr_in *usin)
  1277. {
  1278. struct rtable *rt;
  1279. struct net_device *dev;
  1280. struct cxgb3i_sdev_data *cdata;
  1281. struct t3cdev *cdev;
  1282. __be32 sipv4;
  1283. int err;
  1284. if (usin->sin_family != AF_INET)
  1285. return -EAFNOSUPPORT;
  1286. c3cn->daddr.sin_port = usin->sin_port;
  1287. c3cn->daddr.sin_addr.s_addr = usin->sin_addr.s_addr;
  1288. rt = find_route(c3cn->saddr.sin_addr.s_addr,
  1289. c3cn->daddr.sin_addr.s_addr,
  1290. c3cn->saddr.sin_port,
  1291. c3cn->daddr.sin_port);
  1292. if (rt == NULL) {
  1293. c3cn_conn_debug("NO route to 0x%x, port %u.\n",
  1294. c3cn->daddr.sin_addr.s_addr,
  1295. ntohs(c3cn->daddr.sin_port));
  1296. return -ENETUNREACH;
  1297. }
  1298. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  1299. c3cn_conn_debug("multi-cast route to 0x%x, port %u.\n",
  1300. c3cn->daddr.sin_addr.s_addr,
  1301. ntohs(c3cn->daddr.sin_port));
  1302. ip_rt_put(rt);
  1303. return -ENETUNREACH;
  1304. }
  1305. if (!c3cn->saddr.sin_addr.s_addr)
  1306. c3cn->saddr.sin_addr.s_addr = rt->rt_src;
  1307. /* now commit destination to connection */
  1308. c3cn->dst_cache = &rt->u.dst;
  1309. /* try to establish an offloaded connection */
  1310. dev = cxgb3_egress_dev(c3cn->dst_cache->dev, c3cn, 0);
  1311. if (dev == NULL) {
  1312. c3cn_conn_debug("c3cn 0x%p, egress dev NULL.\n", c3cn);
  1313. return -ENETUNREACH;
  1314. }
  1315. cdata = NDEV2CDATA(dev);
  1316. cdev = cdata->cdev;
  1317. /* get a source port if one hasn't been provided */
  1318. err = c3cn_get_port(c3cn, cdata);
  1319. if (err)
  1320. return err;
  1321. c3cn_conn_debug("c3cn 0x%p get port %u.\n",
  1322. c3cn, ntohs(c3cn->saddr.sin_port));
  1323. sipv4 = cxgb3i_get_private_ipv4addr(dev);
  1324. if (!sipv4) {
  1325. c3cn_conn_debug("c3cn 0x%p, iscsi ip not configured.\n", c3cn);
  1326. sipv4 = c3cn->saddr.sin_addr.s_addr;
  1327. cxgb3i_set_private_ipv4addr(dev, sipv4);
  1328. } else
  1329. c3cn->saddr.sin_addr.s_addr = sipv4;
  1330. c3cn_conn_debug("c3cn 0x%p, %u.%u.%u.%u,%u-%u.%u.%u.%u,%u SYN_SENT.\n",
  1331. c3cn, NIPQUAD(c3cn->saddr.sin_addr.s_addr),
  1332. ntohs(c3cn->saddr.sin_port),
  1333. NIPQUAD(c3cn->daddr.sin_addr.s_addr),
  1334. ntohs(c3cn->daddr.sin_port));
  1335. c3cn_set_state(c3cn, C3CN_STATE_CONNECTING);
  1336. if (!initiate_act_open(c3cn, dev))
  1337. return 0;
  1338. /*
  1339. * If we get here, we don't have an offload connection so simply
  1340. * return a failure.
  1341. */
  1342. err = -ENOTSUPP;
  1343. /*
  1344. * This trashes the connection and releases the local port,
  1345. * if necessary.
  1346. */
  1347. c3cn_conn_debug("c3cn 0x%p -> CLOSED.\n", c3cn);
  1348. c3cn_set_state(c3cn, C3CN_STATE_CLOSED);
  1349. ip_rt_put(rt);
  1350. c3cn_put_port(c3cn);
  1351. c3cn->daddr.sin_port = 0;
  1352. return err;
  1353. }
  1354. /**
  1355. * cxgb3i_c3cn_rx_credits - ack received tcp data.
  1356. * @c3cn: iscsi tcp connection
  1357. * @copied: # of bytes processed
  1358. *
  1359. * Called after some received data has been read. It returns RX credits
  1360. * to the HW for the amount of data processed.
  1361. */
  1362. void cxgb3i_c3cn_rx_credits(struct s3_conn *c3cn, int copied)
  1363. {
  1364. struct t3cdev *cdev;
  1365. int must_send;
  1366. u32 credits, dack = 0;
  1367. if (c3cn->state != C3CN_STATE_ESTABLISHED)
  1368. return;
  1369. credits = c3cn->copied_seq - c3cn->rcv_wup;
  1370. if (unlikely(!credits))
  1371. return;
  1372. cdev = c3cn->cdev;
  1373. if (unlikely(cxgb3_rx_credit_thres == 0))
  1374. return;
  1375. dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
  1376. /*
  1377. * For coalescing to work effectively ensure the receive window has
  1378. * at least 16KB left.
  1379. */
  1380. must_send = credits + 16384 >= cxgb3_rcv_win;
  1381. if (must_send || credits >= cxgb3_rx_credit_thres)
  1382. c3cn->rcv_wup += send_rx_credits(c3cn, credits, dack);
  1383. }
  1384. /**
  1385. * cxgb3i_c3cn_send_pdus - send the skbs containing iscsi pdus
  1386. * @c3cn: iscsi tcp connection
  1387. * @skb: skb contains the iscsi pdu
  1388. *
  1389. * Add a list of skbs to a connection send queue. The skbs must comply with
  1390. * the max size limit of the device and have a headroom of at least
  1391. * TX_HEADER_LEN bytes.
  1392. * Return # of bytes queued.
  1393. */
  1394. int cxgb3i_c3cn_send_pdus(struct s3_conn *c3cn, struct sk_buff *skb)
  1395. {
  1396. struct sk_buff *next;
  1397. int err, copied = 0;
  1398. spin_lock_bh(&c3cn->lock);
  1399. if (c3cn->state != C3CN_STATE_ESTABLISHED) {
  1400. c3cn_tx_debug("c3cn 0x%p, not in est. state %u.\n",
  1401. c3cn, c3cn->state);
  1402. err = -EAGAIN;
  1403. goto out_err;
  1404. }
  1405. err = -EPIPE;
  1406. if (c3cn->err) {
  1407. c3cn_tx_debug("c3cn 0x%p, err %d.\n", c3cn, c3cn->err);
  1408. goto out_err;
  1409. }
  1410. while (skb) {
  1411. int frags = skb_shinfo(skb)->nr_frags +
  1412. (skb->len != skb->data_len);
  1413. if (unlikely(skb_headroom(skb) < TX_HEADER_LEN)) {
  1414. c3cn_tx_debug("c3cn 0x%p, skb head.\n", c3cn);
  1415. err = -EINVAL;
  1416. goto out_err;
  1417. }
  1418. if (frags >= SKB_WR_LIST_SIZE) {
  1419. cxgb3i_log_error("c3cn 0x%p, tx frags %d, len %u,%u.\n",
  1420. c3cn, skb_shinfo(skb)->nr_frags,
  1421. skb->len, skb->data_len);
  1422. err = -EINVAL;
  1423. goto out_err;
  1424. }
  1425. next = skb->next;
  1426. skb->next = NULL;
  1427. skb_entail(c3cn, skb, C3CB_FLAG_NO_APPEND | C3CB_FLAG_NEED_HDR);
  1428. copied += skb->len;
  1429. c3cn->write_seq += skb->len + ulp_extra_len(skb);
  1430. skb = next;
  1431. }
  1432. done:
  1433. if (likely(skb_queue_len(&c3cn->write_queue)))
  1434. c3cn_push_tx_frames(c3cn, 1);
  1435. spin_unlock_bh(&c3cn->lock);
  1436. return copied;
  1437. out_err:
  1438. if (copied == 0 && err == -EPIPE)
  1439. copied = c3cn->err ? c3cn->err : -EPIPE;
  1440. goto done;
  1441. }
  1442. static void sdev_data_cleanup(struct cxgb3i_sdev_data *cdata)
  1443. {
  1444. struct adap_ports *ports = &cdata->ports;
  1445. int i;
  1446. for (i = 0; i < ports->nports; i++)
  1447. NDEV2CDATA(ports->lldevs[i]) = NULL;
  1448. cxgb3i_free_big_mem(cdata);
  1449. }
  1450. void cxgb3i_sdev_cleanup(void)
  1451. {
  1452. struct cxgb3i_sdev_data *cdata;
  1453. write_lock(&cdata_rwlock);
  1454. list_for_each_entry(cdata, &cdata_list, list) {
  1455. list_del(&cdata->list);
  1456. sdev_data_cleanup(cdata);
  1457. }
  1458. write_unlock(&cdata_rwlock);
  1459. }
  1460. int cxgb3i_sdev_init(cxgb3_cpl_handler_func *cpl_handlers)
  1461. {
  1462. cpl_handlers[CPL_ACT_ESTABLISH] = do_act_establish;
  1463. cpl_handlers[CPL_ACT_OPEN_RPL] = do_act_open_rpl;
  1464. cpl_handlers[CPL_PEER_CLOSE] = do_peer_close;
  1465. cpl_handlers[CPL_ABORT_REQ_RSS] = do_abort_req;
  1466. cpl_handlers[CPL_ABORT_RPL_RSS] = do_abort_rpl;
  1467. cpl_handlers[CPL_CLOSE_CON_RPL] = do_close_con_rpl;
  1468. cpl_handlers[CPL_TX_DMA_ACK] = do_wr_ack;
  1469. cpl_handlers[CPL_ISCSI_HDR] = do_iscsi_hdr;
  1470. if (cxgb3_max_connect > CXGB3I_MAX_CONN)
  1471. cxgb3_max_connect = CXGB3I_MAX_CONN;
  1472. return 0;
  1473. }
  1474. /**
  1475. * cxgb3i_sdev_add - allocate and initialize resources for each adapter found
  1476. * @cdev: t3cdev adapter
  1477. * @client: cxgb3 driver client
  1478. */
  1479. void cxgb3i_sdev_add(struct t3cdev *cdev, struct cxgb3_client *client)
  1480. {
  1481. struct cxgb3i_sdev_data *cdata;
  1482. struct ofld_page_info rx_page_info;
  1483. unsigned int wr_len;
  1484. int mapsize = DIV_ROUND_UP(cxgb3_max_connect,
  1485. 8 * sizeof(unsigned long));
  1486. int i;
  1487. cdata = cxgb3i_alloc_big_mem(sizeof(*cdata) + mapsize, GFP_KERNEL);
  1488. if (!cdata)
  1489. return;
  1490. if (cdev->ctl(cdev, GET_WR_LEN, &wr_len) < 0 ||
  1491. cdev->ctl(cdev, GET_PORTS, &cdata->ports) < 0 ||
  1492. cdev->ctl(cdev, GET_RX_PAGE_INFO, &rx_page_info) < 0)
  1493. goto free_cdata;
  1494. s3_init_wr_tab(wr_len);
  1495. INIT_LIST_HEAD(&cdata->list);
  1496. cdata->cdev = cdev;
  1497. cdata->client = client;
  1498. for (i = 0; i < cdata->ports.nports; i++)
  1499. NDEV2CDATA(cdata->ports.lldevs[i]) = cdata;
  1500. write_lock(&cdata_rwlock);
  1501. list_add_tail(&cdata->list, &cdata_list);
  1502. write_unlock(&cdata_rwlock);
  1503. return;
  1504. free_cdata:
  1505. cxgb3i_free_big_mem(cdata);
  1506. }
  1507. /**
  1508. * cxgb3i_sdev_remove - free the allocated resources for the adapter
  1509. * @cdev: t3cdev adapter
  1510. */
  1511. void cxgb3i_sdev_remove(struct t3cdev *cdev)
  1512. {
  1513. struct cxgb3i_sdev_data *cdata = CXGB3_SDEV_DATA(cdev);
  1514. write_lock(&cdata_rwlock);
  1515. list_del(&cdata->list);
  1516. write_unlock(&cdata_rwlock);
  1517. sdev_data_cleanup(cdata);
  1518. }