cxgb3i.c 41 KB

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