cxgb3i.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * cxgb3i_offload.c: Chelsio S3xx iscsi offloaded tcp connection management
  3. *
  4. * Copyright (C) 2003-2015 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. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <scsi/scsi_host.h>
  18. #include "common.h"
  19. #include "t3_cpl.h"
  20. #include "t3cdev.h"
  21. #include "cxgb3_defs.h"
  22. #include "cxgb3_ctl_defs.h"
  23. #include "cxgb3_offload.h"
  24. #include "firmware_exports.h"
  25. #include "cxgb3i.h"
  26. static unsigned int dbg_level;
  27. #include "../libcxgbi.h"
  28. #define DRV_MODULE_NAME "cxgb3i"
  29. #define DRV_MODULE_DESC "Chelsio T3 iSCSI Driver"
  30. #define DRV_MODULE_VERSION "2.0.1-ko"
  31. #define DRV_MODULE_RELDATE "Apr. 2015"
  32. static char version[] =
  33. DRV_MODULE_DESC " " DRV_MODULE_NAME
  34. " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  35. MODULE_AUTHOR("Chelsio Communications, Inc.");
  36. MODULE_DESCRIPTION(DRV_MODULE_DESC);
  37. MODULE_VERSION(DRV_MODULE_VERSION);
  38. MODULE_LICENSE("GPL");
  39. module_param(dbg_level, uint, 0644);
  40. MODULE_PARM_DESC(dbg_level, "debug flag (default=0)");
  41. static int cxgb3i_rcv_win = 256 * 1024;
  42. module_param(cxgb3i_rcv_win, int, 0644);
  43. MODULE_PARM_DESC(cxgb3i_rcv_win, "TCP receive window in bytes (default=256KB)");
  44. static int cxgb3i_snd_win = 128 * 1024;
  45. module_param(cxgb3i_snd_win, int, 0644);
  46. MODULE_PARM_DESC(cxgb3i_snd_win, "TCP send window in bytes (default=128KB)");
  47. static int cxgb3i_rx_credit_thres = 10 * 1024;
  48. module_param(cxgb3i_rx_credit_thres, int, 0644);
  49. MODULE_PARM_DESC(rx_credit_thres,
  50. "RX credits return threshold in bytes (default=10KB)");
  51. static unsigned int cxgb3i_max_connect = 8 * 1024;
  52. module_param(cxgb3i_max_connect, uint, 0644);
  53. MODULE_PARM_DESC(cxgb3i_max_connect, "Max. # of connections (default=8092)");
  54. static unsigned int cxgb3i_sport_base = 20000;
  55. module_param(cxgb3i_sport_base, uint, 0644);
  56. MODULE_PARM_DESC(cxgb3i_sport_base, "starting port number (default=20000)");
  57. static void cxgb3i_dev_open(struct t3cdev *);
  58. static void cxgb3i_dev_close(struct t3cdev *);
  59. static void cxgb3i_dev_event_handler(struct t3cdev *, u32, u32);
  60. static struct cxgb3_client t3_client = {
  61. .name = DRV_MODULE_NAME,
  62. .handlers = cxgb3i_cpl_handlers,
  63. .add = cxgb3i_dev_open,
  64. .remove = cxgb3i_dev_close,
  65. .event_handler = cxgb3i_dev_event_handler,
  66. };
  67. static struct scsi_host_template cxgb3i_host_template = {
  68. .module = THIS_MODULE,
  69. .name = DRV_MODULE_NAME,
  70. .proc_name = DRV_MODULE_NAME,
  71. .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
  72. .queuecommand = iscsi_queuecommand,
  73. .change_queue_depth = scsi_change_queue_depth,
  74. .sg_tablesize = SG_ALL,
  75. .max_sectors = 0xFFFF,
  76. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  77. .eh_abort_handler = iscsi_eh_abort,
  78. .eh_device_reset_handler = iscsi_eh_device_reset,
  79. .eh_target_reset_handler = iscsi_eh_recover_target,
  80. .target_alloc = iscsi_target_alloc,
  81. .use_clustering = DISABLE_CLUSTERING,
  82. .this_id = -1,
  83. .track_queue_depth = 1,
  84. };
  85. static struct iscsi_transport cxgb3i_iscsi_transport = {
  86. .owner = THIS_MODULE,
  87. .name = DRV_MODULE_NAME,
  88. /* owner and name should be set already */
  89. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  90. | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
  91. CAP_PADDING_OFFLOAD | CAP_TEXT_NEGO,
  92. .attr_is_visible = cxgbi_attr_is_visible,
  93. .get_host_param = cxgbi_get_host_param,
  94. .set_host_param = cxgbi_set_host_param,
  95. /* session management */
  96. .create_session = cxgbi_create_session,
  97. .destroy_session = cxgbi_destroy_session,
  98. .get_session_param = iscsi_session_get_param,
  99. /* connection management */
  100. .create_conn = cxgbi_create_conn,
  101. .bind_conn = cxgbi_bind_conn,
  102. .destroy_conn = iscsi_tcp_conn_teardown,
  103. .start_conn = iscsi_conn_start,
  104. .stop_conn = iscsi_conn_stop,
  105. .get_conn_param = iscsi_conn_get_param,
  106. .set_param = cxgbi_set_conn_param,
  107. .get_stats = cxgbi_get_conn_stats,
  108. /* pdu xmit req from user space */
  109. .send_pdu = iscsi_conn_send_pdu,
  110. /* task */
  111. .init_task = iscsi_tcp_task_init,
  112. .xmit_task = iscsi_tcp_task_xmit,
  113. .cleanup_task = cxgbi_cleanup_task,
  114. /* pdu */
  115. .alloc_pdu = cxgbi_conn_alloc_pdu,
  116. .init_pdu = cxgbi_conn_init_pdu,
  117. .xmit_pdu = cxgbi_conn_xmit_pdu,
  118. .parse_pdu_itt = cxgbi_parse_pdu_itt,
  119. /* TCP connect/disconnect */
  120. .get_ep_param = cxgbi_get_ep_param,
  121. .ep_connect = cxgbi_ep_connect,
  122. .ep_poll = cxgbi_ep_poll,
  123. .ep_disconnect = cxgbi_ep_disconnect,
  124. /* Error recovery timeout call */
  125. .session_recovery_timedout = iscsi_session_recovery_timedout,
  126. };
  127. static struct scsi_transport_template *cxgb3i_stt;
  128. /*
  129. * CPL (Chelsio Protocol Language) defines a message passing interface between
  130. * the host driver and Chelsio asic.
  131. * The section below implments CPLs that related to iscsi tcp connection
  132. * open/close/abort and data send/receive.
  133. */
  134. static int push_tx_frames(struct cxgbi_sock *csk, int req_completion);
  135. static void send_act_open_req(struct cxgbi_sock *csk, struct sk_buff *skb,
  136. const struct l2t_entry *e)
  137. {
  138. unsigned int wscale = cxgbi_sock_compute_wscale(csk->rcv_win);
  139. struct cpl_act_open_req *req = (struct cpl_act_open_req *)skb->head;
  140. skb->priority = CPL_PRIORITY_SETUP;
  141. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  142. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, csk->atid));
  143. req->local_port = csk->saddr.sin_port;
  144. req->peer_port = csk->daddr.sin_port;
  145. req->local_ip = csk->saddr.sin_addr.s_addr;
  146. req->peer_ip = csk->daddr.sin_addr.s_addr;
  147. req->opt0h = htonl(V_KEEP_ALIVE(1) | F_TCAM_BYPASS |
  148. V_WND_SCALE(wscale) | V_MSS_IDX(csk->mss_idx) |
  149. V_L2T_IDX(e->idx) | V_TX_CHANNEL(e->smt_idx));
  150. req->opt0l = htonl(V_ULP_MODE(ULP2_MODE_ISCSI) |
  151. V_RCV_BUFSIZ(csk->rcv_win >> 10));
  152. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  153. "csk 0x%p,%u,0x%lx,%u, %pI4:%u-%pI4:%u, %u,%u,%u.\n",
  154. csk, csk->state, csk->flags, csk->atid,
  155. &req->local_ip, ntohs(req->local_port),
  156. &req->peer_ip, ntohs(req->peer_port),
  157. csk->mss_idx, e->idx, e->smt_idx);
  158. l2t_send(csk->cdev->lldev, skb, csk->l2t);
  159. }
  160. static inline void act_open_arp_failure(struct t3cdev *dev, struct sk_buff *skb)
  161. {
  162. cxgbi_sock_act_open_req_arp_failure(NULL, skb);
  163. }
  164. /*
  165. * CPL connection close request: host ->
  166. *
  167. * Close a connection by sending a CPL_CLOSE_CON_REQ message and queue it to
  168. * the write queue (i.e., after any unsent txt data).
  169. */
  170. static void send_close_req(struct cxgbi_sock *csk)
  171. {
  172. struct sk_buff *skb = csk->cpl_close;
  173. struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
  174. unsigned int tid = csk->tid;
  175. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  176. "csk 0x%p,%u,0x%lx,%u.\n",
  177. csk, csk->state, csk->flags, csk->tid);
  178. csk->cpl_close = NULL;
  179. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_CLOSE_CON));
  180. req->wr.wr_lo = htonl(V_WR_TID(tid));
  181. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
  182. req->rsvd = htonl(csk->write_seq);
  183. cxgbi_sock_skb_entail(csk, skb);
  184. if (csk->state >= CTP_ESTABLISHED)
  185. push_tx_frames(csk, 1);
  186. }
  187. /*
  188. * CPL connection abort request: host ->
  189. *
  190. * Send an ABORT_REQ message. Makes sure we do not send multiple ABORT_REQs
  191. * for the same connection and also that we do not try to send a message
  192. * after the connection has closed.
  193. */
  194. static void abort_arp_failure(struct t3cdev *tdev, struct sk_buff *skb)
  195. {
  196. struct cpl_abort_req *req = cplhdr(skb);
  197. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  198. "t3dev 0x%p, tid %u, skb 0x%p.\n",
  199. tdev, GET_TID(req), skb);
  200. req->cmd = CPL_ABORT_NO_RST;
  201. cxgb3_ofld_send(tdev, skb);
  202. }
  203. static void send_abort_req(struct cxgbi_sock *csk)
  204. {
  205. struct sk_buff *skb = csk->cpl_abort_req;
  206. struct cpl_abort_req *req;
  207. if (unlikely(csk->state == CTP_ABORTING || !skb))
  208. return;
  209. cxgbi_sock_set_state(csk, CTP_ABORTING);
  210. cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_PENDING);
  211. /* Purge the send queue so we don't send anything after an abort. */
  212. cxgbi_sock_purge_write_queue(csk);
  213. csk->cpl_abort_req = NULL;
  214. req = (struct cpl_abort_req *)skb->head;
  215. skb->priority = CPL_PRIORITY_DATA;
  216. set_arp_failure_handler(skb, abort_arp_failure);
  217. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_REQ));
  218. req->wr.wr_lo = htonl(V_WR_TID(csk->tid));
  219. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, csk->tid));
  220. req->rsvd0 = htonl(csk->snd_nxt);
  221. req->rsvd1 = !cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT);
  222. req->cmd = CPL_ABORT_SEND_RST;
  223. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  224. "csk 0x%p,%u,0x%lx,%u, snd_nxt %u, 0x%x.\n",
  225. csk, csk->state, csk->flags, csk->tid, csk->snd_nxt,
  226. req->rsvd1);
  227. l2t_send(csk->cdev->lldev, skb, csk->l2t);
  228. }
  229. /*
  230. * CPL connection abort reply: host ->
  231. *
  232. * Send an ABORT_RPL message in response of the ABORT_REQ received.
  233. */
  234. static void send_abort_rpl(struct cxgbi_sock *csk, int rst_status)
  235. {
  236. struct sk_buff *skb = csk->cpl_abort_rpl;
  237. struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;
  238. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  239. "csk 0x%p,%u,0x%lx,%u, status %d.\n",
  240. csk, csk->state, csk->flags, csk->tid, rst_status);
  241. csk->cpl_abort_rpl = NULL;
  242. skb->priority = CPL_PRIORITY_DATA;
  243. rpl->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
  244. rpl->wr.wr_lo = htonl(V_WR_TID(csk->tid));
  245. OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, csk->tid));
  246. rpl->cmd = rst_status;
  247. cxgb3_ofld_send(csk->cdev->lldev, skb);
  248. }
  249. /*
  250. * CPL connection rx data ack: host ->
  251. * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
  252. * credits sent.
  253. */
  254. static u32 send_rx_credits(struct cxgbi_sock *csk, u32 credits)
  255. {
  256. struct sk_buff *skb;
  257. struct cpl_rx_data_ack *req;
  258. u32 dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
  259. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  260. "csk 0x%p,%u,0x%lx,%u, credit %u, dack %u.\n",
  261. csk, csk->state, csk->flags, csk->tid, credits, dack);
  262. skb = alloc_wr(sizeof(*req), 0, GFP_ATOMIC);
  263. if (!skb) {
  264. pr_info("csk 0x%p, credit %u, OOM.\n", csk, credits);
  265. return 0;
  266. }
  267. req = (struct cpl_rx_data_ack *)skb->head;
  268. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  269. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RX_DATA_ACK, csk->tid));
  270. req->credit_dack = htonl(F_RX_DACK_CHANGE | V_RX_DACK_MODE(1) |
  271. V_RX_CREDITS(credits));
  272. skb->priority = CPL_PRIORITY_ACK;
  273. cxgb3_ofld_send(csk->cdev->lldev, skb);
  274. return credits;
  275. }
  276. /*
  277. * CPL connection tx data: host ->
  278. *
  279. * Send iscsi PDU via TX_DATA CPL message. Returns the number of
  280. * credits sent.
  281. * Each TX_DATA consumes work request credit (wrs), so we need to keep track of
  282. * how many we've used so far and how many are pending (i.e., yet ack'ed by T3).
  283. */
  284. static unsigned int wrlen __read_mostly;
  285. static unsigned int skb_wrs[SKB_WR_LIST_SIZE] __read_mostly;
  286. static void init_wr_tab(unsigned int wr_len)
  287. {
  288. int i;
  289. if (skb_wrs[1]) /* already initialized */
  290. return;
  291. for (i = 1; i < SKB_WR_LIST_SIZE; i++) {
  292. int sgl_len = (3 * i) / 2 + (i & 1);
  293. sgl_len += 3;
  294. skb_wrs[i] = (sgl_len <= wr_len
  295. ? 1 : 1 + (sgl_len - 2) / (wr_len - 1));
  296. }
  297. wrlen = wr_len * 8;
  298. }
  299. static inline void make_tx_data_wr(struct cxgbi_sock *csk, struct sk_buff *skb,
  300. int len, int req_completion)
  301. {
  302. struct tx_data_wr *req;
  303. struct l2t_entry *l2t = csk->l2t;
  304. skb_reset_transport_header(skb);
  305. req = (struct tx_data_wr *)__skb_push(skb, sizeof(*req));
  306. req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_OFLD_TX_DATA) |
  307. (req_completion ? F_WR_COMPL : 0));
  308. req->wr_lo = htonl(V_WR_TID(csk->tid));
  309. /* len includes the length of any HW ULP additions */
  310. req->len = htonl(len);
  311. /* V_TX_ULP_SUBMODE sets both the mode and submode */
  312. req->flags = htonl(V_TX_ULP_SUBMODE(cxgbi_skcb_ulp_mode(skb)) |
  313. V_TX_SHOVE((skb_peek(&csk->write_queue) ? 0 : 1)));
  314. req->sndseq = htonl(csk->snd_nxt);
  315. req->param = htonl(V_TX_PORT(l2t->smt_idx));
  316. if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
  317. req->flags |= htonl(V_TX_ACK_PAGES(2) | F_TX_INIT |
  318. V_TX_CPU_IDX(csk->rss_qid));
  319. /* sendbuffer is in units of 32KB. */
  320. req->param |= htonl(V_TX_SNDBUF(csk->snd_win >> 15));
  321. cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
  322. }
  323. }
  324. /**
  325. * push_tx_frames -- start transmit
  326. * @c3cn: the offloaded connection
  327. * @req_completion: request wr_ack or not
  328. *
  329. * Prepends TX_DATA_WR or CPL_CLOSE_CON_REQ headers to buffers waiting in a
  330. * connection's send queue and sends them on to T3. Must be called with the
  331. * connection's lock held. Returns the amount of send buffer space that was
  332. * freed as a result of sending queued data to T3.
  333. */
  334. static void arp_failure_skb_discard(struct t3cdev *dev, struct sk_buff *skb)
  335. {
  336. kfree_skb(skb);
  337. }
  338. static int push_tx_frames(struct cxgbi_sock *csk, int req_completion)
  339. {
  340. int total_size = 0;
  341. struct sk_buff *skb;
  342. if (unlikely(csk->state < CTP_ESTABLISHED ||
  343. csk->state == CTP_CLOSE_WAIT_1 || csk->state >= CTP_ABORTING)) {
  344. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_TX,
  345. "csk 0x%p,%u,0x%lx,%u, in closing state.\n",
  346. csk, csk->state, csk->flags, csk->tid);
  347. return 0;
  348. }
  349. while (csk->wr_cred && (skb = skb_peek(&csk->write_queue)) != NULL) {
  350. int len = skb->len; /* length before skb_push */
  351. int frags = skb_shinfo(skb)->nr_frags + (len != skb->data_len);
  352. int wrs_needed = skb_wrs[frags];
  353. if (wrs_needed > 1 && len + sizeof(struct tx_data_wr) <= wrlen)
  354. wrs_needed = 1;
  355. WARN_ON(frags >= SKB_WR_LIST_SIZE || wrs_needed < 1);
  356. if (csk->wr_cred < wrs_needed) {
  357. log_debug(1 << CXGBI_DBG_PDU_TX,
  358. "csk 0x%p, skb len %u/%u, frag %u, wr %d<%u.\n",
  359. csk, skb->len, skb->data_len, frags,
  360. wrs_needed, csk->wr_cred);
  361. break;
  362. }
  363. __skb_unlink(skb, &csk->write_queue);
  364. skb->priority = CPL_PRIORITY_DATA;
  365. skb->csum = wrs_needed; /* remember this until the WR_ACK */
  366. csk->wr_cred -= wrs_needed;
  367. csk->wr_una_cred += wrs_needed;
  368. cxgbi_sock_enqueue_wr(csk, skb);
  369. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_TX,
  370. "csk 0x%p, enqueue, skb len %u/%u, frag %u, wr %d, "
  371. "left %u, unack %u.\n",
  372. csk, skb->len, skb->data_len, frags, skb->csum,
  373. csk->wr_cred, csk->wr_una_cred);
  374. if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR))) {
  375. if ((req_completion &&
  376. csk->wr_una_cred == wrs_needed) ||
  377. csk->wr_una_cred >= csk->wr_max_cred / 2) {
  378. req_completion = 1;
  379. csk->wr_una_cred = 0;
  380. }
  381. len += cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
  382. make_tx_data_wr(csk, skb, len, req_completion);
  383. csk->snd_nxt += len;
  384. cxgbi_skcb_clear_flag(skb, SKCBF_TX_NEED_HDR);
  385. }
  386. total_size += skb->truesize;
  387. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_TX,
  388. "csk 0x%p, tid 0x%x, send skb 0x%p.\n",
  389. csk, csk->tid, skb);
  390. set_arp_failure_handler(skb, arp_failure_skb_discard);
  391. l2t_send(csk->cdev->lldev, skb, csk->l2t);
  392. }
  393. return total_size;
  394. }
  395. /*
  396. * Process a CPL_ACT_ESTABLISH message: -> host
  397. * Updates connection state from an active establish CPL message. Runs with
  398. * the connection lock held.
  399. */
  400. static inline void free_atid(struct cxgbi_sock *csk)
  401. {
  402. if (cxgbi_sock_flag(csk, CTPF_HAS_ATID)) {
  403. cxgb3_free_atid(csk->cdev->lldev, csk->atid);
  404. cxgbi_sock_clear_flag(csk, CTPF_HAS_ATID);
  405. cxgbi_sock_put(csk);
  406. }
  407. }
  408. static int do_act_establish(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  409. {
  410. struct cxgbi_sock *csk = ctx;
  411. struct cpl_act_establish *req = cplhdr(skb);
  412. unsigned int tid = GET_TID(req);
  413. unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
  414. u32 rcv_isn = ntohl(req->rcv_isn); /* real RCV_ISN + 1 */
  415. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  416. "atid 0x%x,tid 0x%x, csk 0x%p,%u,0x%lx, isn %u.\n",
  417. atid, atid, csk, csk->state, csk->flags, rcv_isn);
  418. cxgbi_sock_get(csk);
  419. cxgbi_sock_set_flag(csk, CTPF_HAS_TID);
  420. csk->tid = tid;
  421. cxgb3_insert_tid(csk->cdev->lldev, &t3_client, csk, tid);
  422. free_atid(csk);
  423. csk->rss_qid = G_QNUM(ntohs(skb->csum));
  424. spin_lock_bh(&csk->lock);
  425. if (csk->retry_timer.function) {
  426. del_timer(&csk->retry_timer);
  427. csk->retry_timer.function = NULL;
  428. }
  429. if (unlikely(csk->state != CTP_ACTIVE_OPEN))
  430. pr_info("csk 0x%p,%u,0x%lx,%u, got EST.\n",
  431. csk, csk->state, csk->flags, csk->tid);
  432. csk->copied_seq = csk->rcv_wup = csk->rcv_nxt = rcv_isn;
  433. if (csk->rcv_win > (M_RCV_BUFSIZ << 10))
  434. csk->rcv_wup -= csk->rcv_win - (M_RCV_BUFSIZ << 10);
  435. cxgbi_sock_established(csk, ntohl(req->snd_isn), ntohs(req->tcp_opt));
  436. if (unlikely(cxgbi_sock_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED)))
  437. /* upper layer has requested closing */
  438. send_abort_req(csk);
  439. else {
  440. if (skb_queue_len(&csk->write_queue))
  441. push_tx_frames(csk, 1);
  442. cxgbi_conn_tx_open(csk);
  443. }
  444. spin_unlock_bh(&csk->lock);
  445. __kfree_skb(skb);
  446. return 0;
  447. }
  448. /*
  449. * Process a CPL_ACT_OPEN_RPL message: -> host
  450. * Handle active open failures.
  451. */
  452. static int act_open_rpl_status_to_errno(int status)
  453. {
  454. switch (status) {
  455. case CPL_ERR_CONN_RESET:
  456. return -ECONNREFUSED;
  457. case CPL_ERR_ARP_MISS:
  458. return -EHOSTUNREACH;
  459. case CPL_ERR_CONN_TIMEDOUT:
  460. return -ETIMEDOUT;
  461. case CPL_ERR_TCAM_FULL:
  462. return -ENOMEM;
  463. case CPL_ERR_CONN_EXIST:
  464. return -EADDRINUSE;
  465. default:
  466. return -EIO;
  467. }
  468. }
  469. static void act_open_retry_timer(unsigned long data)
  470. {
  471. struct sk_buff *skb;
  472. struct cxgbi_sock *csk = (struct cxgbi_sock *)data;
  473. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  474. "csk 0x%p,%u,0x%lx,%u.\n",
  475. csk, csk->state, csk->flags, csk->tid);
  476. cxgbi_sock_get(csk);
  477. spin_lock_bh(&csk->lock);
  478. skb = alloc_wr(sizeof(struct cpl_act_open_req), 0, GFP_ATOMIC);
  479. if (!skb)
  480. cxgbi_sock_fail_act_open(csk, -ENOMEM);
  481. else {
  482. skb->sk = (struct sock *)csk;
  483. set_arp_failure_handler(skb, act_open_arp_failure);
  484. send_act_open_req(csk, skb, csk->l2t);
  485. }
  486. spin_unlock_bh(&csk->lock);
  487. cxgbi_sock_put(csk);
  488. }
  489. static int do_act_open_rpl(struct t3cdev *tdev, struct sk_buff *skb, void *ctx)
  490. {
  491. struct cxgbi_sock *csk = ctx;
  492. struct cpl_act_open_rpl *rpl = cplhdr(skb);
  493. pr_info("csk 0x%p,%u,0x%lx,%u, status %u, %pI4:%u-%pI4:%u.\n",
  494. csk, csk->state, csk->flags, csk->atid, rpl->status,
  495. &csk->saddr.sin_addr.s_addr, ntohs(csk->saddr.sin_port),
  496. &csk->daddr.sin_addr.s_addr, ntohs(csk->daddr.sin_port));
  497. if (rpl->status != CPL_ERR_TCAM_FULL &&
  498. rpl->status != CPL_ERR_CONN_EXIST &&
  499. rpl->status != CPL_ERR_ARP_MISS)
  500. cxgb3_queue_tid_release(tdev, GET_TID(rpl));
  501. cxgbi_sock_get(csk);
  502. spin_lock_bh(&csk->lock);
  503. if (rpl->status == CPL_ERR_CONN_EXIST &&
  504. csk->retry_timer.function != act_open_retry_timer) {
  505. csk->retry_timer.function = act_open_retry_timer;
  506. mod_timer(&csk->retry_timer, jiffies + HZ / 2);
  507. } else
  508. cxgbi_sock_fail_act_open(csk,
  509. act_open_rpl_status_to_errno(rpl->status));
  510. spin_unlock_bh(&csk->lock);
  511. cxgbi_sock_put(csk);
  512. __kfree_skb(skb);
  513. return 0;
  514. }
  515. /*
  516. * Process PEER_CLOSE CPL messages: -> host
  517. * Handle peer FIN.
  518. */
  519. static int do_peer_close(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  520. {
  521. struct cxgbi_sock *csk = ctx;
  522. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  523. "csk 0x%p,%u,0x%lx,%u.\n",
  524. csk, csk->state, csk->flags, csk->tid);
  525. cxgbi_sock_rcv_peer_close(csk);
  526. __kfree_skb(skb);
  527. return 0;
  528. }
  529. /*
  530. * Process CLOSE_CONN_RPL CPL message: -> host
  531. * Process a peer ACK to our FIN.
  532. */
  533. static int do_close_con_rpl(struct t3cdev *cdev, struct sk_buff *skb,
  534. void *ctx)
  535. {
  536. struct cxgbi_sock *csk = ctx;
  537. struct cpl_close_con_rpl *rpl = cplhdr(skb);
  538. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  539. "csk 0x%p,%u,0x%lx,%u, snxt %u.\n",
  540. csk, csk->state, csk->flags, csk->tid, ntohl(rpl->snd_nxt));
  541. cxgbi_sock_rcv_close_conn_rpl(csk, ntohl(rpl->snd_nxt));
  542. __kfree_skb(skb);
  543. return 0;
  544. }
  545. /*
  546. * Process ABORT_REQ_RSS CPL message: -> host
  547. * Process abort requests. If we are waiting for an ABORT_RPL we ignore this
  548. * request except that we need to reply to it.
  549. */
  550. static int abort_status_to_errno(struct cxgbi_sock *csk, int abort_reason,
  551. int *need_rst)
  552. {
  553. switch (abort_reason) {
  554. case CPL_ERR_BAD_SYN: /* fall through */
  555. case CPL_ERR_CONN_RESET:
  556. return csk->state > CTP_ESTABLISHED ? -EPIPE : -ECONNRESET;
  557. case CPL_ERR_XMIT_TIMEDOUT:
  558. case CPL_ERR_PERSIST_TIMEDOUT:
  559. case CPL_ERR_FINWAIT2_TIMEDOUT:
  560. case CPL_ERR_KEEPALIVE_TIMEDOUT:
  561. return -ETIMEDOUT;
  562. default:
  563. return -EIO;
  564. }
  565. }
  566. static int do_abort_req(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  567. {
  568. const struct cpl_abort_req_rss *req = cplhdr(skb);
  569. struct cxgbi_sock *csk = ctx;
  570. int rst_status = CPL_ABORT_NO_RST;
  571. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  572. "csk 0x%p,%u,0x%lx,%u.\n",
  573. csk, csk->state, csk->flags, csk->tid);
  574. if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
  575. req->status == CPL_ERR_PERSIST_NEG_ADVICE) {
  576. goto done;
  577. }
  578. cxgbi_sock_get(csk);
  579. spin_lock_bh(&csk->lock);
  580. if (!cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD)) {
  581. cxgbi_sock_set_flag(csk, CTPF_ABORT_REQ_RCVD);
  582. cxgbi_sock_set_state(csk, CTP_ABORTING);
  583. goto out;
  584. }
  585. cxgbi_sock_clear_flag(csk, CTPF_ABORT_REQ_RCVD);
  586. send_abort_rpl(csk, rst_status);
  587. if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
  588. csk->err = abort_status_to_errno(csk, req->status, &rst_status);
  589. cxgbi_sock_closed(csk);
  590. }
  591. out:
  592. spin_unlock_bh(&csk->lock);
  593. cxgbi_sock_put(csk);
  594. done:
  595. __kfree_skb(skb);
  596. return 0;
  597. }
  598. /*
  599. * Process ABORT_RPL_RSS CPL message: -> host
  600. * Process abort replies. We only process these messages if we anticipate
  601. * them as the coordination between SW and HW in this area is somewhat lacking
  602. * and sometimes we get ABORT_RPLs after we are done with the connection that
  603. * originated the ABORT_REQ.
  604. */
  605. static int do_abort_rpl(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  606. {
  607. struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
  608. struct cxgbi_sock *csk = ctx;
  609. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  610. "status 0x%x, csk 0x%p, s %u, 0x%lx.\n",
  611. rpl->status, csk, csk ? csk->state : 0,
  612. csk ? csk->flags : 0UL);
  613. /*
  614. * Ignore replies to post-close aborts indicating that the abort was
  615. * requested too late. These connections are terminated when we get
  616. * PEER_CLOSE or CLOSE_CON_RPL and by the time the abort_rpl_rss
  617. * arrives the TID is either no longer used or it has been recycled.
  618. */
  619. if (rpl->status == CPL_ERR_ABORT_FAILED)
  620. goto rel_skb;
  621. /*
  622. * Sometimes we've already closed the connection, e.g., a post-close
  623. * abort races with ABORT_REQ_RSS, the latter frees the connection
  624. * expecting the ABORT_REQ will fail with CPL_ERR_ABORT_FAILED,
  625. * but FW turns the ABORT_REQ into a regular one and so we get
  626. * ABORT_RPL_RSS with status 0 and no connection.
  627. */
  628. if (csk)
  629. cxgbi_sock_rcv_abort_rpl(csk);
  630. rel_skb:
  631. __kfree_skb(skb);
  632. return 0;
  633. }
  634. /*
  635. * Process RX_ISCSI_HDR CPL message: -> host
  636. * Handle received PDUs, the payload could be DDP'ed. If not, the payload
  637. * follow after the bhs.
  638. */
  639. static int do_iscsi_hdr(struct t3cdev *t3dev, struct sk_buff *skb, void *ctx)
  640. {
  641. struct cxgbi_sock *csk = ctx;
  642. struct cpl_iscsi_hdr *hdr_cpl = cplhdr(skb);
  643. struct cpl_iscsi_hdr_norss data_cpl;
  644. struct cpl_rx_data_ddp_norss ddp_cpl;
  645. unsigned int hdr_len, data_len, status;
  646. unsigned int len;
  647. int err;
  648. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  649. "csk 0x%p,%u,0x%lx,%u, skb 0x%p,%u.\n",
  650. csk, csk->state, csk->flags, csk->tid, skb, skb->len);
  651. spin_lock_bh(&csk->lock);
  652. if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
  653. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  654. "csk 0x%p,%u,0x%lx,%u, bad state.\n",
  655. csk, csk->state, csk->flags, csk->tid);
  656. if (csk->state != CTP_ABORTING)
  657. goto abort_conn;
  658. else
  659. goto discard;
  660. }
  661. cxgbi_skcb_tcp_seq(skb) = ntohl(hdr_cpl->seq);
  662. cxgbi_skcb_flags(skb) = 0;
  663. skb_reset_transport_header(skb);
  664. __skb_pull(skb, sizeof(struct cpl_iscsi_hdr));
  665. len = hdr_len = ntohs(hdr_cpl->len);
  666. /* msg coalesce is off or not enough data received */
  667. if (skb->len <= hdr_len) {
  668. pr_err("%s: tid %u, CPL_ISCSI_HDR, skb len %u < %u.\n",
  669. csk->cdev->ports[csk->port_id]->name, csk->tid,
  670. skb->len, hdr_len);
  671. goto abort_conn;
  672. }
  673. cxgbi_skcb_set_flag(skb, SKCBF_RX_COALESCED);
  674. err = skb_copy_bits(skb, skb->len - sizeof(ddp_cpl), &ddp_cpl,
  675. sizeof(ddp_cpl));
  676. if (err < 0) {
  677. pr_err("%s: tid %u, copy cpl_ddp %u-%zu failed %d.\n",
  678. csk->cdev->ports[csk->port_id]->name, csk->tid,
  679. skb->len, sizeof(ddp_cpl), err);
  680. goto abort_conn;
  681. }
  682. cxgbi_skcb_set_flag(skb, SKCBF_RX_STATUS);
  683. cxgbi_skcb_rx_pdulen(skb) = ntohs(ddp_cpl.len);
  684. cxgbi_skcb_rx_ddigest(skb) = ntohl(ddp_cpl.ulp_crc);
  685. status = ntohl(ddp_cpl.ddp_status);
  686. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  687. "csk 0x%p, skb 0x%p,%u, pdulen %u, status 0x%x.\n",
  688. csk, skb, skb->len, cxgbi_skcb_rx_pdulen(skb), status);
  689. if (status & (1 << CPL_RX_DDP_STATUS_HCRC_SHIFT))
  690. cxgbi_skcb_set_flag(skb, SKCBF_RX_HCRC_ERR);
  691. if (status & (1 << CPL_RX_DDP_STATUS_DCRC_SHIFT))
  692. cxgbi_skcb_set_flag(skb, SKCBF_RX_DCRC_ERR);
  693. if (status & (1 << CPL_RX_DDP_STATUS_PAD_SHIFT))
  694. cxgbi_skcb_set_flag(skb, SKCBF_RX_PAD_ERR);
  695. if (skb->len > (hdr_len + sizeof(ddp_cpl))) {
  696. err = skb_copy_bits(skb, hdr_len, &data_cpl, sizeof(data_cpl));
  697. if (err < 0) {
  698. pr_err("%s: tid %u, cp %zu/%u failed %d.\n",
  699. csk->cdev->ports[csk->port_id]->name,
  700. csk->tid, sizeof(data_cpl), skb->len, err);
  701. goto abort_conn;
  702. }
  703. data_len = ntohs(data_cpl.len);
  704. log_debug(1 << CXGBI_DBG_DDP | 1 << CXGBI_DBG_PDU_RX,
  705. "skb 0x%p, pdu not ddp'ed %u/%u, status 0x%x.\n",
  706. skb, data_len, cxgbi_skcb_rx_pdulen(skb), status);
  707. len += sizeof(data_cpl) + data_len;
  708. } else if (status & (1 << CPL_RX_DDP_STATUS_DDP_SHIFT))
  709. cxgbi_skcb_set_flag(skb, SKCBF_RX_DATA_DDPD);
  710. csk->rcv_nxt = ntohl(ddp_cpl.seq) + cxgbi_skcb_rx_pdulen(skb);
  711. __pskb_trim(skb, len);
  712. __skb_queue_tail(&csk->receive_queue, skb);
  713. cxgbi_conn_pdu_ready(csk);
  714. spin_unlock_bh(&csk->lock);
  715. return 0;
  716. abort_conn:
  717. send_abort_req(csk);
  718. discard:
  719. spin_unlock_bh(&csk->lock);
  720. __kfree_skb(skb);
  721. return 0;
  722. }
  723. /*
  724. * Process TX_DATA_ACK CPL messages: -> host
  725. * Process an acknowledgment of WR completion. Advance snd_una and send the
  726. * next batch of work requests from the write queue.
  727. */
  728. static int do_wr_ack(struct t3cdev *cdev, struct sk_buff *skb, void *ctx)
  729. {
  730. struct cxgbi_sock *csk = ctx;
  731. struct cpl_wr_ack *hdr = cplhdr(skb);
  732. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  733. "csk 0x%p,%u,0x%lx,%u, cr %u.\n",
  734. csk, csk->state, csk->flags, csk->tid, ntohs(hdr->credits));
  735. cxgbi_sock_rcv_wr_ack(csk, ntohs(hdr->credits), ntohl(hdr->snd_una), 1);
  736. __kfree_skb(skb);
  737. return 0;
  738. }
  739. /*
  740. * for each connection, pre-allocate skbs needed for close/abort requests. So
  741. * that we can service the request right away.
  742. */
  743. static int alloc_cpls(struct cxgbi_sock *csk)
  744. {
  745. csk->cpl_close = alloc_wr(sizeof(struct cpl_close_con_req), 0,
  746. GFP_KERNEL);
  747. if (!csk->cpl_close)
  748. return -ENOMEM;
  749. csk->cpl_abort_req = alloc_wr(sizeof(struct cpl_abort_req), 0,
  750. GFP_KERNEL);
  751. if (!csk->cpl_abort_req)
  752. goto free_cpl_skbs;
  753. csk->cpl_abort_rpl = alloc_wr(sizeof(struct cpl_abort_rpl), 0,
  754. GFP_KERNEL);
  755. if (!csk->cpl_abort_rpl)
  756. goto free_cpl_skbs;
  757. return 0;
  758. free_cpl_skbs:
  759. cxgbi_sock_free_cpl_skbs(csk);
  760. return -ENOMEM;
  761. }
  762. /**
  763. * release_offload_resources - release offload resource
  764. * @c3cn: the offloaded iscsi tcp connection.
  765. * Release resources held by an offload connection (TID, L2T entry, etc.)
  766. */
  767. static void l2t_put(struct cxgbi_sock *csk)
  768. {
  769. struct t3cdev *t3dev = (struct t3cdev *)csk->cdev->lldev;
  770. if (csk->l2t) {
  771. l2t_release(t3dev, csk->l2t);
  772. csk->l2t = NULL;
  773. cxgbi_sock_put(csk);
  774. }
  775. }
  776. static void release_offload_resources(struct cxgbi_sock *csk)
  777. {
  778. struct t3cdev *t3dev = (struct t3cdev *)csk->cdev->lldev;
  779. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  780. "csk 0x%p,%u,0x%lx,%u.\n",
  781. csk, csk->state, csk->flags, csk->tid);
  782. csk->rss_qid = 0;
  783. cxgbi_sock_free_cpl_skbs(csk);
  784. if (csk->wr_cred != csk->wr_max_cred) {
  785. cxgbi_sock_purge_wr_queue(csk);
  786. cxgbi_sock_reset_wr_list(csk);
  787. }
  788. l2t_put(csk);
  789. if (cxgbi_sock_flag(csk, CTPF_HAS_ATID))
  790. free_atid(csk);
  791. else if (cxgbi_sock_flag(csk, CTPF_HAS_TID)) {
  792. cxgb3_remove_tid(t3dev, (void *)csk, csk->tid);
  793. cxgbi_sock_clear_flag(csk, CTPF_HAS_TID);
  794. cxgbi_sock_put(csk);
  795. }
  796. csk->dst = NULL;
  797. csk->cdev = NULL;
  798. }
  799. static void update_address(struct cxgbi_hba *chba)
  800. {
  801. if (chba->ipv4addr) {
  802. if (chba->vdev &&
  803. chba->ipv4addr != cxgb3i_get_private_ipv4addr(chba->vdev)) {
  804. cxgb3i_set_private_ipv4addr(chba->vdev, chba->ipv4addr);
  805. cxgb3i_set_private_ipv4addr(chba->ndev, 0);
  806. pr_info("%s set %pI4.\n",
  807. chba->vdev->name, &chba->ipv4addr);
  808. } else if (chba->ipv4addr !=
  809. cxgb3i_get_private_ipv4addr(chba->ndev)) {
  810. cxgb3i_set_private_ipv4addr(chba->ndev, chba->ipv4addr);
  811. pr_info("%s set %pI4.\n",
  812. chba->ndev->name, &chba->ipv4addr);
  813. }
  814. } else if (cxgb3i_get_private_ipv4addr(chba->ndev)) {
  815. if (chba->vdev)
  816. cxgb3i_set_private_ipv4addr(chba->vdev, 0);
  817. cxgb3i_set_private_ipv4addr(chba->ndev, 0);
  818. }
  819. }
  820. static int init_act_open(struct cxgbi_sock *csk)
  821. {
  822. struct dst_entry *dst = csk->dst;
  823. struct cxgbi_device *cdev = csk->cdev;
  824. struct t3cdev *t3dev = (struct t3cdev *)cdev->lldev;
  825. struct net_device *ndev = cdev->ports[csk->port_id];
  826. struct cxgbi_hba *chba = cdev->hbas[csk->port_id];
  827. struct sk_buff *skb = NULL;
  828. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  829. "csk 0x%p,%u,0x%lx.\n", csk, csk->state, csk->flags);
  830. update_address(chba);
  831. if (chba->ipv4addr)
  832. csk->saddr.sin_addr.s_addr = chba->ipv4addr;
  833. csk->rss_qid = 0;
  834. csk->l2t = t3_l2t_get(t3dev, dst, ndev,
  835. &csk->daddr.sin_addr.s_addr);
  836. if (!csk->l2t) {
  837. pr_err("NO l2t available.\n");
  838. return -EINVAL;
  839. }
  840. cxgbi_sock_get(csk);
  841. csk->atid = cxgb3_alloc_atid(t3dev, &t3_client, csk);
  842. if (csk->atid < 0) {
  843. pr_err("NO atid available.\n");
  844. goto rel_resource;
  845. }
  846. cxgbi_sock_set_flag(csk, CTPF_HAS_ATID);
  847. cxgbi_sock_get(csk);
  848. skb = alloc_wr(sizeof(struct cpl_act_open_req), 0, GFP_KERNEL);
  849. if (!skb)
  850. goto rel_resource;
  851. skb->sk = (struct sock *)csk;
  852. set_arp_failure_handler(skb, act_open_arp_failure);
  853. csk->snd_win = cxgb3i_snd_win;
  854. csk->rcv_win = cxgb3i_rcv_win;
  855. csk->wr_max_cred = csk->wr_cred = T3C_DATA(t3dev)->max_wrs - 1;
  856. csk->wr_una_cred = 0;
  857. csk->mss_idx = cxgbi_sock_select_mss(csk, dst_mtu(dst));
  858. cxgbi_sock_reset_wr_list(csk);
  859. csk->err = 0;
  860. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  861. "csk 0x%p,%u,0x%lx, %pI4:%u-%pI4:%u.\n",
  862. csk, csk->state, csk->flags,
  863. &csk->saddr.sin_addr.s_addr, ntohs(csk->saddr.sin_port),
  864. &csk->daddr.sin_addr.s_addr, ntohs(csk->daddr.sin_port));
  865. cxgbi_sock_set_state(csk, CTP_ACTIVE_OPEN);
  866. send_act_open_req(csk, skb, csk->l2t);
  867. return 0;
  868. rel_resource:
  869. if (skb)
  870. __kfree_skb(skb);
  871. return -EINVAL;
  872. }
  873. cxgb3_cpl_handler_func cxgb3i_cpl_handlers[NUM_CPL_CMDS] = {
  874. [CPL_ACT_ESTABLISH] = do_act_establish,
  875. [CPL_ACT_OPEN_RPL] = do_act_open_rpl,
  876. [CPL_PEER_CLOSE] = do_peer_close,
  877. [CPL_ABORT_REQ_RSS] = do_abort_req,
  878. [CPL_ABORT_RPL_RSS] = do_abort_rpl,
  879. [CPL_CLOSE_CON_RPL] = do_close_con_rpl,
  880. [CPL_TX_DMA_ACK] = do_wr_ack,
  881. [CPL_ISCSI_HDR] = do_iscsi_hdr,
  882. };
  883. /**
  884. * cxgb3i_ofld_init - allocate and initialize resources for each adapter found
  885. * @cdev: cxgbi adapter
  886. */
  887. int cxgb3i_ofld_init(struct cxgbi_device *cdev)
  888. {
  889. struct t3cdev *t3dev = (struct t3cdev *)cdev->lldev;
  890. struct adap_ports port;
  891. struct ofld_page_info rx_page_info;
  892. unsigned int wr_len;
  893. int rc;
  894. if (t3dev->ctl(t3dev, GET_WR_LEN, &wr_len) < 0 ||
  895. t3dev->ctl(t3dev, GET_PORTS, &port) < 0 ||
  896. t3dev->ctl(t3dev, GET_RX_PAGE_INFO, &rx_page_info) < 0) {
  897. pr_warn("t3 0x%p, offload up, ioctl failed.\n", t3dev);
  898. return -EINVAL;
  899. }
  900. if (cxgb3i_max_connect > CXGBI_MAX_CONN)
  901. cxgb3i_max_connect = CXGBI_MAX_CONN;
  902. rc = cxgbi_device_portmap_create(cdev, cxgb3i_sport_base,
  903. cxgb3i_max_connect);
  904. if (rc < 0)
  905. return rc;
  906. init_wr_tab(wr_len);
  907. cdev->csk_release_offload_resources = release_offload_resources;
  908. cdev->csk_push_tx_frames = push_tx_frames;
  909. cdev->csk_send_abort_req = send_abort_req;
  910. cdev->csk_send_close_req = send_close_req;
  911. cdev->csk_send_rx_credits = send_rx_credits;
  912. cdev->csk_alloc_cpls = alloc_cpls;
  913. cdev->csk_init_act_open = init_act_open;
  914. pr_info("cdev 0x%p, offload up, added.\n", cdev);
  915. return 0;
  916. }
  917. /*
  918. * functions to program the pagepod in h/w
  919. */
  920. static inline void ulp_mem_io_set_hdr(struct sk_buff *skb, unsigned int addr)
  921. {
  922. struct ulp_mem_io *req = (struct ulp_mem_io *)skb->head;
  923. memset(req, 0, sizeof(*req));
  924. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_BYPASS));
  925. req->cmd_lock_addr = htonl(V_ULP_MEMIO_ADDR(addr >> 5) |
  926. V_ULPTX_CMD(ULP_MEM_WRITE));
  927. req->len = htonl(V_ULP_MEMIO_DATA_LEN(PPOD_SIZE >> 5) |
  928. V_ULPTX_NFLITS((PPOD_SIZE >> 3) + 1));
  929. }
  930. static int ddp_set_map(struct cxgbi_sock *csk, struct cxgbi_pagepod_hdr *hdr,
  931. unsigned int idx, unsigned int npods,
  932. struct cxgbi_gather_list *gl)
  933. {
  934. struct cxgbi_device *cdev = csk->cdev;
  935. struct cxgbi_ddp_info *ddp = cdev->ddp;
  936. unsigned int pm_addr = (idx << PPOD_SIZE_SHIFT) + ddp->llimit;
  937. int i;
  938. log_debug(1 << CXGBI_DBG_DDP,
  939. "csk 0x%p, idx %u, npods %u, gl 0x%p.\n",
  940. csk, idx, npods, gl);
  941. for (i = 0; i < npods; i++, idx++, pm_addr += PPOD_SIZE) {
  942. struct sk_buff *skb = alloc_wr(sizeof(struct ulp_mem_io) +
  943. PPOD_SIZE, 0, GFP_ATOMIC);
  944. if (!skb)
  945. return -ENOMEM;
  946. ulp_mem_io_set_hdr(skb, pm_addr);
  947. cxgbi_ddp_ppod_set((struct cxgbi_pagepod *)(skb->head +
  948. sizeof(struct ulp_mem_io)),
  949. hdr, gl, i * PPOD_PAGES_MAX);
  950. skb->priority = CPL_PRIORITY_CONTROL;
  951. cxgb3_ofld_send(cdev->lldev, skb);
  952. }
  953. return 0;
  954. }
  955. static void ddp_clear_map(struct cxgbi_hba *chba, unsigned int tag,
  956. unsigned int idx, unsigned int npods)
  957. {
  958. struct cxgbi_device *cdev = chba->cdev;
  959. struct cxgbi_ddp_info *ddp = cdev->ddp;
  960. unsigned int pm_addr = (idx << PPOD_SIZE_SHIFT) + ddp->llimit;
  961. int i;
  962. log_debug(1 << CXGBI_DBG_DDP,
  963. "cdev 0x%p, idx %u, npods %u, tag 0x%x.\n",
  964. cdev, idx, npods, tag);
  965. for (i = 0; i < npods; i++, idx++, pm_addr += PPOD_SIZE) {
  966. struct sk_buff *skb = alloc_wr(sizeof(struct ulp_mem_io) +
  967. PPOD_SIZE, 0, GFP_ATOMIC);
  968. if (!skb) {
  969. pr_err("tag 0x%x, 0x%x, %d/%u, skb OOM.\n",
  970. tag, idx, i, npods);
  971. continue;
  972. }
  973. ulp_mem_io_set_hdr(skb, pm_addr);
  974. skb->priority = CPL_PRIORITY_CONTROL;
  975. cxgb3_ofld_send(cdev->lldev, skb);
  976. }
  977. }
  978. static int ddp_setup_conn_pgidx(struct cxgbi_sock *csk,
  979. unsigned int tid, int pg_idx, bool reply)
  980. {
  981. struct sk_buff *skb = alloc_wr(sizeof(struct cpl_set_tcb_field), 0,
  982. GFP_KERNEL);
  983. struct cpl_set_tcb_field *req;
  984. u64 val = pg_idx < DDP_PGIDX_MAX ? pg_idx : 0;
  985. log_debug(1 << CXGBI_DBG_DDP,
  986. "csk 0x%p, tid %u, pg_idx %d.\n", csk, tid, pg_idx);
  987. if (!skb)
  988. return -ENOMEM;
  989. /* set up ulp submode and page size */
  990. req = (struct cpl_set_tcb_field *)skb->head;
  991. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  992. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
  993. req->reply = V_NO_REPLY(reply ? 0 : 1);
  994. req->cpu_idx = 0;
  995. req->word = htons(31);
  996. req->mask = cpu_to_be64(0xF0000000);
  997. req->val = cpu_to_be64(val << 28);
  998. skb->priority = CPL_PRIORITY_CONTROL;
  999. cxgb3_ofld_send(csk->cdev->lldev, skb);
  1000. return 0;
  1001. }
  1002. /**
  1003. * cxgb3i_setup_conn_digest - setup conn. digest setting
  1004. * @csk: cxgb tcp socket
  1005. * @tid: connection id
  1006. * @hcrc: header digest enabled
  1007. * @dcrc: data digest enabled
  1008. * @reply: request reply from h/w
  1009. * set up the iscsi digest settings for a connection identified by tid
  1010. */
  1011. static int ddp_setup_conn_digest(struct cxgbi_sock *csk, unsigned int tid,
  1012. int hcrc, int dcrc, int reply)
  1013. {
  1014. struct sk_buff *skb = alloc_wr(sizeof(struct cpl_set_tcb_field), 0,
  1015. GFP_KERNEL);
  1016. struct cpl_set_tcb_field *req;
  1017. u64 val = (hcrc ? 1 : 0) | (dcrc ? 2 : 0);
  1018. log_debug(1 << CXGBI_DBG_DDP,
  1019. "csk 0x%p, tid %u, crc %d,%d.\n", csk, tid, hcrc, dcrc);
  1020. if (!skb)
  1021. return -ENOMEM;
  1022. /* set up ulp submode and page size */
  1023. req = (struct cpl_set_tcb_field *)skb->head;
  1024. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  1025. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
  1026. req->reply = V_NO_REPLY(reply ? 0 : 1);
  1027. req->cpu_idx = 0;
  1028. req->word = htons(31);
  1029. req->mask = cpu_to_be64(0x0F000000);
  1030. req->val = cpu_to_be64(val << 24);
  1031. skb->priority = CPL_PRIORITY_CONTROL;
  1032. cxgb3_ofld_send(csk->cdev->lldev, skb);
  1033. return 0;
  1034. }
  1035. /**
  1036. * t3_ddp_cleanup - release the cxgb3 adapter's ddp resource
  1037. * @cdev: cxgb3i adapter
  1038. * release all the resource held by the ddp pagepod manager for a given
  1039. * adapter if needed
  1040. */
  1041. static void t3_ddp_cleanup(struct cxgbi_device *cdev)
  1042. {
  1043. struct t3cdev *tdev = (struct t3cdev *)cdev->lldev;
  1044. if (cxgbi_ddp_cleanup(cdev)) {
  1045. pr_info("t3dev 0x%p, ulp_iscsi no more user.\n", tdev);
  1046. tdev->ulp_iscsi = NULL;
  1047. }
  1048. }
  1049. /**
  1050. * ddp_init - initialize the cxgb3 adapter's ddp resource
  1051. * @cdev: cxgb3i adapter
  1052. * initialize the ddp pagepod manager for a given adapter
  1053. */
  1054. static int cxgb3i_ddp_init(struct cxgbi_device *cdev)
  1055. {
  1056. struct t3cdev *tdev = (struct t3cdev *)cdev->lldev;
  1057. struct cxgbi_ddp_info *ddp = tdev->ulp_iscsi;
  1058. struct ulp_iscsi_info uinfo;
  1059. unsigned int pgsz_factor[4];
  1060. int i, err;
  1061. if (ddp) {
  1062. kref_get(&ddp->refcnt);
  1063. pr_warn("t3dev 0x%p, ddp 0x%p already set up.\n",
  1064. tdev, tdev->ulp_iscsi);
  1065. cdev->ddp = ddp;
  1066. return -EALREADY;
  1067. }
  1068. err = tdev->ctl(tdev, ULP_ISCSI_GET_PARAMS, &uinfo);
  1069. if (err < 0) {
  1070. pr_err("%s, failed to get iscsi param err=%d.\n",
  1071. tdev->name, err);
  1072. return err;
  1073. }
  1074. err = cxgbi_ddp_init(cdev, uinfo.llimit, uinfo.ulimit,
  1075. uinfo.max_txsz, uinfo.max_rxsz);
  1076. if (err < 0)
  1077. return err;
  1078. ddp = cdev->ddp;
  1079. uinfo.tagmask = ddp->idx_mask << PPOD_IDX_SHIFT;
  1080. cxgbi_ddp_page_size_factor(pgsz_factor);
  1081. for (i = 0; i < 4; i++)
  1082. uinfo.pgsz_factor[i] = pgsz_factor[i];
  1083. uinfo.ulimit = uinfo.llimit + (ddp->nppods << PPOD_SIZE_SHIFT);
  1084. err = tdev->ctl(tdev, ULP_ISCSI_SET_PARAMS, &uinfo);
  1085. if (err < 0) {
  1086. pr_warn("%s unable to set iscsi param err=%d, ddp disabled.\n",
  1087. tdev->name, err);
  1088. cxgbi_ddp_cleanup(cdev);
  1089. return err;
  1090. }
  1091. tdev->ulp_iscsi = ddp;
  1092. cdev->csk_ddp_setup_digest = ddp_setup_conn_digest;
  1093. cdev->csk_ddp_setup_pgidx = ddp_setup_conn_pgidx;
  1094. cdev->csk_ddp_set = ddp_set_map;
  1095. cdev->csk_ddp_clear = ddp_clear_map;
  1096. pr_info("tdev 0x%p, nppods %u, bits %u, mask 0x%x,0x%x pkt %u/%u, "
  1097. "%u/%u.\n",
  1098. tdev, ddp->nppods, ddp->idx_bits, ddp->idx_mask,
  1099. ddp->rsvd_tag_mask, ddp->max_txsz, uinfo.max_txsz,
  1100. ddp->max_rxsz, uinfo.max_rxsz);
  1101. return 0;
  1102. }
  1103. static void cxgb3i_dev_close(struct t3cdev *t3dev)
  1104. {
  1105. struct cxgbi_device *cdev = cxgbi_device_find_by_lldev(t3dev);
  1106. if (!cdev || cdev->flags & CXGBI_FLAG_ADAPTER_RESET) {
  1107. pr_info("0x%p close, f 0x%x.\n", cdev, cdev ? cdev->flags : 0);
  1108. return;
  1109. }
  1110. cxgbi_device_unregister(cdev);
  1111. }
  1112. /**
  1113. * cxgb3i_dev_open - init a t3 adapter structure and any h/w settings
  1114. * @t3dev: t3cdev adapter
  1115. */
  1116. static void cxgb3i_dev_open(struct t3cdev *t3dev)
  1117. {
  1118. struct cxgbi_device *cdev = cxgbi_device_find_by_lldev(t3dev);
  1119. struct adapter *adapter = tdev2adap(t3dev);
  1120. int i, err;
  1121. if (cdev) {
  1122. pr_info("0x%p, updating.\n", cdev);
  1123. return;
  1124. }
  1125. cdev = cxgbi_device_register(0, adapter->params.nports);
  1126. if (!cdev) {
  1127. pr_warn("device 0x%p register failed.\n", t3dev);
  1128. return;
  1129. }
  1130. cdev->flags = CXGBI_FLAG_DEV_T3 | CXGBI_FLAG_IPV4_SET;
  1131. cdev->lldev = t3dev;
  1132. cdev->pdev = adapter->pdev;
  1133. cdev->ports = adapter->port;
  1134. cdev->nports = adapter->params.nports;
  1135. cdev->mtus = adapter->params.mtus;
  1136. cdev->nmtus = NMTUS;
  1137. cdev->rx_credit_thres = cxgb3i_rx_credit_thres;
  1138. cdev->skb_tx_rsvd = CXGB3I_TX_HEADER_LEN;
  1139. cdev->skb_rx_extra = sizeof(struct cpl_iscsi_hdr_norss);
  1140. cdev->dev_ddp_cleanup = t3_ddp_cleanup;
  1141. cdev->itp = &cxgb3i_iscsi_transport;
  1142. err = cxgb3i_ddp_init(cdev);
  1143. if (err) {
  1144. pr_info("0x%p ddp init failed\n", cdev);
  1145. goto err_out;
  1146. }
  1147. err = cxgb3i_ofld_init(cdev);
  1148. if (err) {
  1149. pr_info("0x%p offload init failed\n", cdev);
  1150. goto err_out;
  1151. }
  1152. err = cxgbi_hbas_add(cdev, CXGB3I_MAX_LUN, CXGBI_MAX_CONN,
  1153. &cxgb3i_host_template, cxgb3i_stt);
  1154. if (err)
  1155. goto err_out;
  1156. for (i = 0; i < cdev->nports; i++)
  1157. cdev->hbas[i]->ipv4addr =
  1158. cxgb3i_get_private_ipv4addr(cdev->ports[i]);
  1159. pr_info("cdev 0x%p, f 0x%x, t3dev 0x%p open, err %d.\n",
  1160. cdev, cdev ? cdev->flags : 0, t3dev, err);
  1161. return;
  1162. err_out:
  1163. cxgbi_device_unregister(cdev);
  1164. }
  1165. static void cxgb3i_dev_event_handler(struct t3cdev *t3dev, u32 event, u32 port)
  1166. {
  1167. struct cxgbi_device *cdev = cxgbi_device_find_by_lldev(t3dev);
  1168. log_debug(1 << CXGBI_DBG_TOE,
  1169. "0x%p, cdev 0x%p, event 0x%x, port 0x%x.\n",
  1170. t3dev, cdev, event, port);
  1171. if (!cdev)
  1172. return;
  1173. switch (event) {
  1174. case OFFLOAD_STATUS_DOWN:
  1175. cdev->flags |= CXGBI_FLAG_ADAPTER_RESET;
  1176. break;
  1177. case OFFLOAD_STATUS_UP:
  1178. cdev->flags &= ~CXGBI_FLAG_ADAPTER_RESET;
  1179. break;
  1180. }
  1181. }
  1182. /**
  1183. * cxgb3i_init_module - module init entry point
  1184. *
  1185. * initialize any driver wide global data structures and register itself
  1186. * with the cxgb3 module
  1187. */
  1188. static int __init cxgb3i_init_module(void)
  1189. {
  1190. int rc;
  1191. printk(KERN_INFO "%s", version);
  1192. rc = cxgbi_iscsi_init(&cxgb3i_iscsi_transport, &cxgb3i_stt);
  1193. if (rc < 0)
  1194. return rc;
  1195. cxgb3_register_client(&t3_client);
  1196. return 0;
  1197. }
  1198. /**
  1199. * cxgb3i_exit_module - module cleanup/exit entry point
  1200. *
  1201. * go through the driver hba list and for each hba, release any resource held.
  1202. * and unregisters iscsi transport and the cxgb3 module
  1203. */
  1204. static void __exit cxgb3i_exit_module(void)
  1205. {
  1206. cxgb3_unregister_client(&t3_client);
  1207. cxgbi_device_unregister_all(CXGBI_FLAG_DEV_T3);
  1208. cxgbi_iscsi_cleanup(&cxgb3i_iscsi_transport, &cxgb3i_stt);
  1209. }
  1210. module_init(cxgb3i_init_module);
  1211. module_exit(cxgb3i_exit_module);