cxgb3i.c 42 KB

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