cxgb3i.c 41 KB

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