cxgb3i.c 41 KB

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