cxgb3i.c 41 KB

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