cxgb4i.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. /*
  2. * cxgb4i.c: Chelsio T4 iSCSI driver.
  3. *
  4. * Copyright (c) 2010 Chelsio Communications, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation.
  9. *
  10. * Written by: Karen Xie (kxie@chelsio.com)
  11. * Rakesh Ranjan (rranjan@chelsio.com)
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <scsi/scsi_host.h>
  17. #include <net/tcp.h>
  18. #include <net/dst.h>
  19. #include <linux/netdevice.h>
  20. #include <net/addrconf.h>
  21. #include "t4_regs.h"
  22. #include "t4_msg.h"
  23. #include "cxgb4.h"
  24. #include "cxgb4_uld.h"
  25. #include "t4fw_api.h"
  26. #include "l2t.h"
  27. #include "cxgb4i.h"
  28. static unsigned int dbg_level;
  29. #include "../libcxgbi.h"
  30. #define DRV_MODULE_NAME "cxgb4i"
  31. #define DRV_MODULE_DESC "Chelsio T4/T5 iSCSI Driver"
  32. #define DRV_MODULE_VERSION "0.9.4"
  33. static char version[] =
  34. DRV_MODULE_DESC " " DRV_MODULE_NAME
  35. " v" DRV_MODULE_VERSION "\n";
  36. MODULE_AUTHOR("Chelsio Communications, Inc.");
  37. MODULE_DESCRIPTION(DRV_MODULE_DESC);
  38. MODULE_VERSION(DRV_MODULE_VERSION);
  39. MODULE_LICENSE("GPL");
  40. module_param(dbg_level, uint, 0644);
  41. MODULE_PARM_DESC(dbg_level, "Debug flag (default=0)");
  42. static int cxgb4i_rcv_win = 256 * 1024;
  43. module_param(cxgb4i_rcv_win, int, 0644);
  44. MODULE_PARM_DESC(cxgb4i_rcv_win, "TCP reveive window in bytes");
  45. static int cxgb4i_snd_win = 128 * 1024;
  46. module_param(cxgb4i_snd_win, int, 0644);
  47. MODULE_PARM_DESC(cxgb4i_snd_win, "TCP send window in bytes");
  48. static int cxgb4i_rx_credit_thres = 10 * 1024;
  49. module_param(cxgb4i_rx_credit_thres, int, 0644);
  50. MODULE_PARM_DESC(cxgb4i_rx_credit_thres,
  51. "RX credits return threshold in bytes (default=10KB)");
  52. static unsigned int cxgb4i_max_connect = (8 * 1024);
  53. module_param(cxgb4i_max_connect, uint, 0644);
  54. MODULE_PARM_DESC(cxgb4i_max_connect, "Maximum number of connections");
  55. static unsigned short cxgb4i_sport_base = 20000;
  56. module_param(cxgb4i_sport_base, ushort, 0644);
  57. MODULE_PARM_DESC(cxgb4i_sport_base, "Starting port number (default 20000)");
  58. typedef void (*cxgb4i_cplhandler_func)(struct cxgbi_device *, struct sk_buff *);
  59. static void *t4_uld_add(const struct cxgb4_lld_info *);
  60. static int t4_uld_rx_handler(void *, const __be64 *, const struct pkt_gl *);
  61. static int t4_uld_state_change(void *, enum cxgb4_state state);
  62. static const struct cxgb4_uld_info cxgb4i_uld_info = {
  63. .name = DRV_MODULE_NAME,
  64. .add = t4_uld_add,
  65. .rx_handler = t4_uld_rx_handler,
  66. .state_change = t4_uld_state_change,
  67. };
  68. static struct scsi_host_template cxgb4i_host_template = {
  69. .module = THIS_MODULE,
  70. .name = DRV_MODULE_NAME,
  71. .proc_name = DRV_MODULE_NAME,
  72. .can_queue = CXGB4I_SCSI_HOST_QDEPTH,
  73. .queuecommand = iscsi_queuecommand,
  74. .change_queue_depth = iscsi_change_queue_depth,
  75. .sg_tablesize = SG_ALL,
  76. .max_sectors = 0xFFFF,
  77. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  78. .eh_abort_handler = iscsi_eh_abort,
  79. .eh_device_reset_handler = iscsi_eh_device_reset,
  80. .eh_target_reset_handler = iscsi_eh_recover_target,
  81. .target_alloc = iscsi_target_alloc,
  82. .use_clustering = DISABLE_CLUSTERING,
  83. .this_id = -1,
  84. };
  85. static struct iscsi_transport cxgb4i_iscsi_transport = {
  86. .owner = THIS_MODULE,
  87. .name = DRV_MODULE_NAME,
  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 *cxgb4i_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. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  134. #define RCV_BUFSIZ_MASK 0x3FFU
  135. #define MAX_IMM_TX_PKT_LEN 128
  136. static inline void set_queue(struct sk_buff *skb, unsigned int queue,
  137. const struct cxgbi_sock *csk)
  138. {
  139. skb->queue_mapping = queue;
  140. }
  141. static int push_tx_frames(struct cxgbi_sock *, int);
  142. /*
  143. * is_ofld_imm - check whether a packet can be sent as immediate data
  144. * @skb: the packet
  145. *
  146. * Returns true if a packet can be sent as an offload WR with immediate
  147. * data. We currently use the same limit as for Ethernet packets.
  148. */
  149. static inline int is_ofld_imm(const struct sk_buff *skb)
  150. {
  151. return skb->len <= (MAX_IMM_TX_PKT_LEN -
  152. sizeof(struct fw_ofld_tx_data_wr));
  153. }
  154. static void send_act_open_req(struct cxgbi_sock *csk, struct sk_buff *skb,
  155. struct l2t_entry *e)
  156. {
  157. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
  158. int t4 = is_t4(lldi->adapter_type);
  159. int wscale = cxgbi_sock_compute_wscale(csk->mss_idx);
  160. unsigned long long opt0;
  161. unsigned int opt2;
  162. unsigned int qid_atid = ((unsigned int)csk->atid) |
  163. (((unsigned int)csk->rss_qid) << 14);
  164. opt0 = KEEP_ALIVE(1) |
  165. WND_SCALE(wscale) |
  166. MSS_IDX(csk->mss_idx) |
  167. L2T_IDX(((struct l2t_entry *)csk->l2t)->idx) |
  168. TX_CHAN(csk->tx_chan) |
  169. SMAC_SEL(csk->smac_idx) |
  170. ULP_MODE(ULP_MODE_ISCSI) |
  171. RCV_BUFSIZ(cxgb4i_rcv_win >> 10);
  172. opt2 = RX_CHANNEL(0) |
  173. RSS_QUEUE_VALID |
  174. (1 << 20) |
  175. RSS_QUEUE(csk->rss_qid);
  176. if (is_t4(lldi->adapter_type)) {
  177. struct cpl_act_open_req *req =
  178. (struct cpl_act_open_req *)skb->head;
  179. INIT_TP_WR(req, 0);
  180. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
  181. qid_atid));
  182. req->local_port = csk->saddr.sin_port;
  183. req->peer_port = csk->daddr.sin_port;
  184. req->local_ip = csk->saddr.sin_addr.s_addr;
  185. req->peer_ip = csk->daddr.sin_addr.s_addr;
  186. req->opt0 = cpu_to_be64(opt0);
  187. req->params = cpu_to_be32(cxgb4_select_ntuple(
  188. csk->cdev->ports[csk->port_id],
  189. csk->l2t));
  190. opt2 |= 1 << 22;
  191. req->opt2 = cpu_to_be32(opt2);
  192. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  193. "csk t4 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
  194. csk, &req->local_ip, ntohs(req->local_port),
  195. &req->peer_ip, ntohs(req->peer_port),
  196. csk->atid, csk->rss_qid);
  197. } else {
  198. struct cpl_t5_act_open_req *req =
  199. (struct cpl_t5_act_open_req *)skb->head;
  200. INIT_TP_WR(req, 0);
  201. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
  202. qid_atid));
  203. req->local_port = csk->saddr.sin_port;
  204. req->peer_port = csk->daddr.sin_port;
  205. req->local_ip = csk->saddr.sin_addr.s_addr;
  206. req->peer_ip = csk->daddr.sin_addr.s_addr;
  207. req->opt0 = cpu_to_be64(opt0);
  208. req->params = cpu_to_be64(V_FILTER_TUPLE(
  209. cxgb4_select_ntuple(
  210. csk->cdev->ports[csk->port_id],
  211. csk->l2t)));
  212. opt2 |= 1 << 31;
  213. req->opt2 = cpu_to_be32(opt2);
  214. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  215. "csk t5 0x%p, %pI4:%u-%pI4:%u, atid %d, qid %u.\n",
  216. csk, &req->local_ip, ntohs(req->local_port),
  217. &req->peer_ip, ntohs(req->peer_port),
  218. csk->atid, csk->rss_qid);
  219. }
  220. set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
  221. pr_info_ipaddr("t%d csk 0x%p,%u,0x%lx,%u, rss_qid %u.\n",
  222. (&csk->saddr), (&csk->daddr), t4 ? 4 : 5, csk,
  223. csk->state, csk->flags, csk->atid, csk->rss_qid);
  224. cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
  225. }
  226. #if IS_ENABLED(CONFIG_IPV6)
  227. static void send_act_open_req6(struct cxgbi_sock *csk, struct sk_buff *skb,
  228. struct l2t_entry *e)
  229. {
  230. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
  231. int t4 = is_t4(lldi->adapter_type);
  232. int wscale = cxgbi_sock_compute_wscale(csk->mss_idx);
  233. unsigned long long opt0;
  234. unsigned int opt2;
  235. unsigned int qid_atid = ((unsigned int)csk->atid) |
  236. (((unsigned int)csk->rss_qid) << 14);
  237. opt0 = KEEP_ALIVE(1) |
  238. WND_SCALE(wscale) |
  239. MSS_IDX(csk->mss_idx) |
  240. L2T_IDX(((struct l2t_entry *)csk->l2t)->idx) |
  241. TX_CHAN(csk->tx_chan) |
  242. SMAC_SEL(csk->smac_idx) |
  243. ULP_MODE(ULP_MODE_ISCSI) |
  244. RCV_BUFSIZ(cxgb4i_rcv_win >> 10);
  245. opt2 = RX_CHANNEL(0) |
  246. RSS_QUEUE_VALID |
  247. RX_FC_DISABLE |
  248. RSS_QUEUE(csk->rss_qid);
  249. if (t4) {
  250. struct cpl_act_open_req6 *req =
  251. (struct cpl_act_open_req6 *)skb->head;
  252. INIT_TP_WR(req, 0);
  253. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
  254. qid_atid));
  255. req->local_port = csk->saddr6.sin6_port;
  256. req->peer_port = csk->daddr6.sin6_port;
  257. req->local_ip_hi = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr);
  258. req->local_ip_lo = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr +
  259. 8);
  260. req->peer_ip_hi = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr);
  261. req->peer_ip_lo = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr +
  262. 8);
  263. req->opt0 = cpu_to_be64(opt0);
  264. opt2 |= RX_FC_VALID;
  265. req->opt2 = cpu_to_be32(opt2);
  266. req->params = cpu_to_be32(cxgb4_select_ntuple(
  267. csk->cdev->ports[csk->port_id],
  268. csk->l2t));
  269. } else {
  270. struct cpl_t5_act_open_req6 *req =
  271. (struct cpl_t5_act_open_req6 *)skb->head;
  272. INIT_TP_WR(req, 0);
  273. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
  274. qid_atid));
  275. req->local_port = csk->saddr6.sin6_port;
  276. req->peer_port = csk->daddr6.sin6_port;
  277. req->local_ip_hi = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr);
  278. req->local_ip_lo = *(__be64 *)(csk->saddr6.sin6_addr.s6_addr +
  279. 8);
  280. req->peer_ip_hi = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr);
  281. req->peer_ip_lo = *(__be64 *)(csk->daddr6.sin6_addr.s6_addr +
  282. 8);
  283. req->opt0 = cpu_to_be64(opt0);
  284. opt2 |= T5_OPT_2_VALID;
  285. req->opt2 = cpu_to_be32(opt2);
  286. req->params = cpu_to_be64(V_FILTER_TUPLE(cxgb4_select_ntuple(
  287. csk->cdev->ports[csk->port_id],
  288. csk->l2t)));
  289. }
  290. set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
  291. pr_info("t%d csk 0x%p,%u,0x%lx,%u, [%pI6]:%u-[%pI6]:%u, rss_qid %u.\n",
  292. t4 ? 4 : 5, csk, csk->state, csk->flags, csk->atid,
  293. &csk->saddr6.sin6_addr, ntohs(csk->saddr.sin_port),
  294. &csk->daddr6.sin6_addr, ntohs(csk->daddr.sin_port),
  295. csk->rss_qid);
  296. cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
  297. }
  298. #endif
  299. static void send_close_req(struct cxgbi_sock *csk)
  300. {
  301. struct sk_buff *skb = csk->cpl_close;
  302. struct cpl_close_con_req *req = (struct cpl_close_con_req *)skb->head;
  303. unsigned int tid = csk->tid;
  304. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  305. "csk 0x%p,%u,0x%lx, tid %u.\n",
  306. csk, csk->state, csk->flags, csk->tid);
  307. csk->cpl_close = NULL;
  308. set_wr_txq(skb, CPL_PRIORITY_DATA, csk->port_id);
  309. INIT_TP_WR(req, tid);
  310. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
  311. req->rsvd = 0;
  312. cxgbi_sock_skb_entail(csk, skb);
  313. if (csk->state >= CTP_ESTABLISHED)
  314. push_tx_frames(csk, 1);
  315. }
  316. static void abort_arp_failure(void *handle, struct sk_buff *skb)
  317. {
  318. struct cxgbi_sock *csk = (struct cxgbi_sock *)handle;
  319. struct cpl_abort_req *req;
  320. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  321. "csk 0x%p,%u,0x%lx, tid %u, abort.\n",
  322. csk, csk->state, csk->flags, csk->tid);
  323. req = (struct cpl_abort_req *)skb->data;
  324. req->cmd = CPL_ABORT_NO_RST;
  325. cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
  326. }
  327. static void send_abort_req(struct cxgbi_sock *csk)
  328. {
  329. struct cpl_abort_req *req;
  330. struct sk_buff *skb = csk->cpl_abort_req;
  331. if (unlikely(csk->state == CTP_ABORTING) || !skb || !csk->cdev)
  332. return;
  333. cxgbi_sock_set_state(csk, CTP_ABORTING);
  334. cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_PENDING);
  335. cxgbi_sock_purge_write_queue(csk);
  336. csk->cpl_abort_req = NULL;
  337. req = (struct cpl_abort_req *)skb->head;
  338. set_queue(skb, CPL_PRIORITY_DATA, csk);
  339. req->cmd = CPL_ABORT_SEND_RST;
  340. t4_set_arp_err_handler(skb, csk, abort_arp_failure);
  341. INIT_TP_WR(req, csk->tid);
  342. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, csk->tid));
  343. req->rsvd0 = htonl(csk->snd_nxt);
  344. req->rsvd1 = !cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT);
  345. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  346. "csk 0x%p,%u,0x%lx,%u, snd_nxt %u, 0x%x.\n",
  347. csk, csk->state, csk->flags, csk->tid, csk->snd_nxt,
  348. req->rsvd1);
  349. cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
  350. }
  351. static void send_abort_rpl(struct cxgbi_sock *csk, int rst_status)
  352. {
  353. struct sk_buff *skb = csk->cpl_abort_rpl;
  354. struct cpl_abort_rpl *rpl = (struct cpl_abort_rpl *)skb->head;
  355. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  356. "csk 0x%p,%u,0x%lx,%u, status %d.\n",
  357. csk, csk->state, csk->flags, csk->tid, rst_status);
  358. csk->cpl_abort_rpl = NULL;
  359. set_queue(skb, CPL_PRIORITY_DATA, csk);
  360. INIT_TP_WR(rpl, csk->tid);
  361. OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, csk->tid));
  362. rpl->cmd = rst_status;
  363. cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
  364. }
  365. /*
  366. * CPL connection rx data ack: host ->
  367. * Send RX credits through an RX_DATA_ACK CPL message. Returns the number of
  368. * credits sent.
  369. */
  370. static u32 send_rx_credits(struct cxgbi_sock *csk, u32 credits)
  371. {
  372. struct sk_buff *skb;
  373. struct cpl_rx_data_ack *req;
  374. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  375. "csk 0x%p,%u,0x%lx,%u, credit %u.\n",
  376. csk, csk->state, csk->flags, csk->tid, credits);
  377. skb = alloc_wr(sizeof(*req), 0, GFP_ATOMIC);
  378. if (!skb) {
  379. pr_info("csk 0x%p, credit %u, OOM.\n", csk, credits);
  380. return 0;
  381. }
  382. req = (struct cpl_rx_data_ack *)skb->head;
  383. set_wr_txq(skb, CPL_PRIORITY_ACK, csk->port_id);
  384. INIT_TP_WR(req, csk->tid);
  385. OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
  386. csk->tid));
  387. req->credit_dack = cpu_to_be32(RX_CREDITS(credits) | RX_FORCE_ACK(1));
  388. cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
  389. return credits;
  390. }
  391. /*
  392. * sgl_len - calculates the size of an SGL of the given capacity
  393. * @n: the number of SGL entries
  394. * Calculates the number of flits needed for a scatter/gather list that
  395. * can hold the given number of entries.
  396. */
  397. static inline unsigned int sgl_len(unsigned int n)
  398. {
  399. n--;
  400. return (3 * n) / 2 + (n & 1) + 2;
  401. }
  402. /*
  403. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  404. * @skb: the packet
  405. *
  406. * Returns the number of flits needed for the given offload packet.
  407. * These packets are already fully constructed and no additional headers
  408. * will be added.
  409. */
  410. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  411. {
  412. unsigned int flits, cnt;
  413. if (is_ofld_imm(skb))
  414. return DIV_ROUND_UP(skb->len, 8);
  415. flits = skb_transport_offset(skb) / 8;
  416. cnt = skb_shinfo(skb)->nr_frags;
  417. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  418. cnt++;
  419. return flits + sgl_len(cnt);
  420. }
  421. static inline void send_tx_flowc_wr(struct cxgbi_sock *csk)
  422. {
  423. struct sk_buff *skb;
  424. struct fw_flowc_wr *flowc;
  425. int flowclen, i;
  426. flowclen = 80;
  427. skb = alloc_wr(flowclen, 0, GFP_ATOMIC);
  428. flowc = (struct fw_flowc_wr *)skb->head;
  429. flowc->op_to_nparams =
  430. htonl(FW_WR_OP(FW_FLOWC_WR) | FW_FLOWC_WR_NPARAMS(8));
  431. flowc->flowid_len16 =
  432. htonl(FW_WR_LEN16(DIV_ROUND_UP(72, 16)) |
  433. FW_WR_FLOWID(csk->tid));
  434. flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
  435. flowc->mnemval[0].val = htonl(csk->cdev->pfvf);
  436. flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
  437. flowc->mnemval[1].val = htonl(csk->tx_chan);
  438. flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
  439. flowc->mnemval[2].val = htonl(csk->tx_chan);
  440. flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
  441. flowc->mnemval[3].val = htonl(csk->rss_qid);
  442. flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
  443. flowc->mnemval[4].val = htonl(csk->snd_nxt);
  444. flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
  445. flowc->mnemval[5].val = htonl(csk->rcv_nxt);
  446. flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
  447. flowc->mnemval[6].val = htonl(cxgb4i_snd_win);
  448. flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
  449. flowc->mnemval[7].val = htonl(csk->advmss);
  450. flowc->mnemval[8].mnemonic = 0;
  451. flowc->mnemval[8].val = 0;
  452. for (i = 0; i < 9; i++) {
  453. flowc->mnemval[i].r4[0] = 0;
  454. flowc->mnemval[i].r4[1] = 0;
  455. flowc->mnemval[i].r4[2] = 0;
  456. }
  457. set_queue(skb, CPL_PRIORITY_DATA, csk);
  458. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  459. "csk 0x%p, tid 0x%x, %u,%u,%u,%u,%u,%u,%u.\n",
  460. csk, csk->tid, 0, csk->tx_chan, csk->rss_qid,
  461. csk->snd_nxt, csk->rcv_nxt, cxgb4i_snd_win,
  462. csk->advmss);
  463. cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
  464. }
  465. static inline void make_tx_data_wr(struct cxgbi_sock *csk, struct sk_buff *skb,
  466. int dlen, int len, u32 credits, int compl)
  467. {
  468. struct fw_ofld_tx_data_wr *req;
  469. unsigned int submode = cxgbi_skcb_ulp_mode(skb) & 3;
  470. unsigned int wr_ulp_mode = 0;
  471. req = (struct fw_ofld_tx_data_wr *)__skb_push(skb, sizeof(*req));
  472. if (is_ofld_imm(skb)) {
  473. req->op_to_immdlen = htonl(FW_WR_OP(FW_OFLD_TX_DATA_WR) |
  474. FW_WR_COMPL(1) |
  475. FW_WR_IMMDLEN(dlen));
  476. req->flowid_len16 = htonl(FW_WR_FLOWID(csk->tid) |
  477. FW_WR_LEN16(credits));
  478. } else {
  479. req->op_to_immdlen =
  480. cpu_to_be32(FW_WR_OP(FW_OFLD_TX_DATA_WR) |
  481. FW_WR_COMPL(1) |
  482. FW_WR_IMMDLEN(0));
  483. req->flowid_len16 =
  484. cpu_to_be32(FW_WR_FLOWID(csk->tid) |
  485. FW_WR_LEN16(credits));
  486. }
  487. if (submode)
  488. wr_ulp_mode = FW_OFLD_TX_DATA_WR_ULPMODE(ULP2_MODE_ISCSI) |
  489. FW_OFLD_TX_DATA_WR_ULPSUBMODE(submode);
  490. req->tunnel_to_proxy = htonl(wr_ulp_mode |
  491. FW_OFLD_TX_DATA_WR_SHOVE(skb_peek(&csk->write_queue) ? 0 : 1));
  492. req->plen = htonl(len);
  493. if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT))
  494. cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
  495. }
  496. static void arp_failure_skb_discard(void *handle, struct sk_buff *skb)
  497. {
  498. kfree_skb(skb);
  499. }
  500. static int push_tx_frames(struct cxgbi_sock *csk, int req_completion)
  501. {
  502. int total_size = 0;
  503. struct sk_buff *skb;
  504. if (unlikely(csk->state < CTP_ESTABLISHED ||
  505. csk->state == CTP_CLOSE_WAIT_1 || csk->state >= CTP_ABORTING)) {
  506. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK |
  507. 1 << CXGBI_DBG_PDU_TX,
  508. "csk 0x%p,%u,0x%lx,%u, in closing state.\n",
  509. csk, csk->state, csk->flags, csk->tid);
  510. return 0;
  511. }
  512. while (csk->wr_cred && (skb = skb_peek(&csk->write_queue)) != NULL) {
  513. int dlen = skb->len;
  514. int len = skb->len;
  515. unsigned int credits_needed;
  516. skb_reset_transport_header(skb);
  517. if (is_ofld_imm(skb))
  518. credits_needed = DIV_ROUND_UP(dlen +
  519. sizeof(struct fw_ofld_tx_data_wr), 16);
  520. else
  521. credits_needed = DIV_ROUND_UP(8*calc_tx_flits_ofld(skb)
  522. + sizeof(struct fw_ofld_tx_data_wr),
  523. 16);
  524. if (csk->wr_cred < credits_needed) {
  525. log_debug(1 << CXGBI_DBG_PDU_TX,
  526. "csk 0x%p, skb %u/%u, wr %d < %u.\n",
  527. csk, skb->len, skb->data_len,
  528. credits_needed, csk->wr_cred);
  529. break;
  530. }
  531. __skb_unlink(skb, &csk->write_queue);
  532. set_queue(skb, CPL_PRIORITY_DATA, csk);
  533. skb->csum = credits_needed;
  534. csk->wr_cred -= credits_needed;
  535. csk->wr_una_cred += credits_needed;
  536. cxgbi_sock_enqueue_wr(csk, skb);
  537. log_debug(1 << CXGBI_DBG_PDU_TX,
  538. "csk 0x%p, skb %u/%u, wr %d, left %u, unack %u.\n",
  539. csk, skb->len, skb->data_len, credits_needed,
  540. csk->wr_cred, csk->wr_una_cred);
  541. if (likely(cxgbi_skcb_test_flag(skb, SKCBF_TX_NEED_HDR))) {
  542. if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
  543. send_tx_flowc_wr(csk);
  544. skb->csum += 5;
  545. csk->wr_cred -= 5;
  546. csk->wr_una_cred += 5;
  547. }
  548. len += cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
  549. make_tx_data_wr(csk, skb, dlen, len, credits_needed,
  550. req_completion);
  551. csk->snd_nxt += len;
  552. cxgbi_skcb_clear_flag(skb, SKCBF_TX_NEED_HDR);
  553. }
  554. total_size += skb->truesize;
  555. t4_set_arp_err_handler(skb, csk, arp_failure_skb_discard);
  556. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_TX,
  557. "csk 0x%p,%u,0x%lx,%u, skb 0x%p, %u.\n",
  558. csk, csk->state, csk->flags, csk->tid, skb, len);
  559. cxgb4_l2t_send(csk->cdev->ports[csk->port_id], skb, csk->l2t);
  560. }
  561. return total_size;
  562. }
  563. static inline void free_atid(struct cxgbi_sock *csk)
  564. {
  565. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
  566. if (cxgbi_sock_flag(csk, CTPF_HAS_ATID)) {
  567. cxgb4_free_atid(lldi->tids, csk->atid);
  568. cxgbi_sock_clear_flag(csk, CTPF_HAS_ATID);
  569. cxgbi_sock_put(csk);
  570. }
  571. }
  572. static void do_act_establish(struct cxgbi_device *cdev, struct sk_buff *skb)
  573. {
  574. struct cxgbi_sock *csk;
  575. struct cpl_act_establish *req = (struct cpl_act_establish *)skb->data;
  576. unsigned short tcp_opt = ntohs(req->tcp_opt);
  577. unsigned int tid = GET_TID(req);
  578. unsigned int atid = GET_TID_TID(ntohl(req->tos_atid));
  579. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  580. struct tid_info *t = lldi->tids;
  581. u32 rcv_isn = be32_to_cpu(req->rcv_isn);
  582. csk = lookup_atid(t, atid);
  583. if (unlikely(!csk)) {
  584. pr_err("NO conn. for atid %u, cdev 0x%p.\n", atid, cdev);
  585. goto rel_skb;
  586. }
  587. if (csk->atid != atid) {
  588. pr_err("bad conn atid %u, csk 0x%p,%u,0x%lx,tid %u, atid %u.\n",
  589. atid, csk, csk->state, csk->flags, csk->tid, csk->atid);
  590. goto rel_skb;
  591. }
  592. pr_info_ipaddr("atid 0x%x, tid 0x%x, csk 0x%p,%u,0x%lx, isn %u.\n",
  593. (&csk->saddr), (&csk->daddr),
  594. atid, tid, csk, csk->state, csk->flags, rcv_isn);
  595. module_put(THIS_MODULE);
  596. cxgbi_sock_get(csk);
  597. csk->tid = tid;
  598. cxgb4_insert_tid(lldi->tids, csk, tid);
  599. cxgbi_sock_set_flag(csk, CTPF_HAS_TID);
  600. free_atid(csk);
  601. spin_lock_bh(&csk->lock);
  602. if (unlikely(csk->state != CTP_ACTIVE_OPEN))
  603. pr_info("csk 0x%p,%u,0x%lx,%u, got EST.\n",
  604. csk, csk->state, csk->flags, csk->tid);
  605. if (csk->retry_timer.function) {
  606. del_timer(&csk->retry_timer);
  607. csk->retry_timer.function = NULL;
  608. }
  609. csk->copied_seq = csk->rcv_wup = csk->rcv_nxt = rcv_isn;
  610. /*
  611. * Causes the first RX_DATA_ACK to supply any Rx credits we couldn't
  612. * pass through opt0.
  613. */
  614. if (cxgb4i_rcv_win > (RCV_BUFSIZ_MASK << 10))
  615. csk->rcv_wup -= cxgb4i_rcv_win - (RCV_BUFSIZ_MASK << 10);
  616. csk->advmss = lldi->mtus[GET_TCPOPT_MSS(tcp_opt)] - 40;
  617. if (GET_TCPOPT_TSTAMP(tcp_opt))
  618. csk->advmss -= 12;
  619. if (csk->advmss < 128)
  620. csk->advmss = 128;
  621. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  622. "csk 0x%p, mss_idx %u, advmss %u.\n",
  623. csk, GET_TCPOPT_MSS(tcp_opt), csk->advmss);
  624. cxgbi_sock_established(csk, ntohl(req->snd_isn), ntohs(req->tcp_opt));
  625. if (unlikely(cxgbi_sock_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED)))
  626. send_abort_req(csk);
  627. else {
  628. if (skb_queue_len(&csk->write_queue))
  629. push_tx_frames(csk, 0);
  630. cxgbi_conn_tx_open(csk);
  631. }
  632. spin_unlock_bh(&csk->lock);
  633. rel_skb:
  634. __kfree_skb(skb);
  635. }
  636. static int act_open_rpl_status_to_errno(int status)
  637. {
  638. switch (status) {
  639. case CPL_ERR_CONN_RESET:
  640. return -ECONNREFUSED;
  641. case CPL_ERR_ARP_MISS:
  642. return -EHOSTUNREACH;
  643. case CPL_ERR_CONN_TIMEDOUT:
  644. return -ETIMEDOUT;
  645. case CPL_ERR_TCAM_FULL:
  646. return -ENOMEM;
  647. case CPL_ERR_CONN_EXIST:
  648. return -EADDRINUSE;
  649. default:
  650. return -EIO;
  651. }
  652. }
  653. static void csk_act_open_retry_timer(unsigned long data)
  654. {
  655. struct sk_buff *skb = NULL;
  656. struct cxgbi_sock *csk = (struct cxgbi_sock *)data;
  657. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(csk->cdev);
  658. void (*send_act_open_func)(struct cxgbi_sock *, struct sk_buff *,
  659. struct l2t_entry *);
  660. int t4 = is_t4(lldi->adapter_type), size, size6;
  661. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  662. "csk 0x%p,%u,0x%lx,%u.\n",
  663. csk, csk->state, csk->flags, csk->tid);
  664. cxgbi_sock_get(csk);
  665. spin_lock_bh(&csk->lock);
  666. if (t4) {
  667. size = sizeof(struct cpl_act_open_req);
  668. size6 = sizeof(struct cpl_act_open_req6);
  669. } else {
  670. size = sizeof(struct cpl_t5_act_open_req);
  671. size6 = sizeof(struct cpl_t5_act_open_req6);
  672. }
  673. if (csk->csk_family == AF_INET) {
  674. send_act_open_func = send_act_open_req;
  675. skb = alloc_wr(size, 0, GFP_ATOMIC);
  676. #if IS_ENABLED(CONFIG_IPV6)
  677. } else {
  678. send_act_open_func = send_act_open_req6;
  679. skb = alloc_wr(size6, 0, GFP_ATOMIC);
  680. #endif
  681. }
  682. if (!skb)
  683. cxgbi_sock_fail_act_open(csk, -ENOMEM);
  684. else {
  685. skb->sk = (struct sock *)csk;
  686. t4_set_arp_err_handler(skb, csk,
  687. cxgbi_sock_act_open_req_arp_failure);
  688. send_act_open_func(csk, skb, csk->l2t);
  689. }
  690. spin_unlock_bh(&csk->lock);
  691. cxgbi_sock_put(csk);
  692. }
  693. static void do_act_open_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
  694. {
  695. struct cxgbi_sock *csk;
  696. struct cpl_act_open_rpl *rpl = (struct cpl_act_open_rpl *)skb->data;
  697. unsigned int tid = GET_TID(rpl);
  698. unsigned int atid =
  699. GET_TID_TID(GET_AOPEN_ATID(be32_to_cpu(rpl->atid_status)));
  700. unsigned int status = GET_AOPEN_STATUS(be32_to_cpu(rpl->atid_status));
  701. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  702. struct tid_info *t = lldi->tids;
  703. csk = lookup_atid(t, atid);
  704. if (unlikely(!csk)) {
  705. pr_err("NO matching conn. atid %u, tid %u.\n", atid, tid);
  706. goto rel_skb;
  707. }
  708. pr_info_ipaddr("tid %u/%u, status %u.\n"
  709. "csk 0x%p,%u,0x%lx. ", (&csk->saddr), (&csk->daddr),
  710. atid, tid, status, csk, csk->state, csk->flags);
  711. if (status == CPL_ERR_RTX_NEG_ADVICE)
  712. goto rel_skb;
  713. module_put(THIS_MODULE);
  714. if (status && status != CPL_ERR_TCAM_FULL &&
  715. status != CPL_ERR_CONN_EXIST &&
  716. status != CPL_ERR_ARP_MISS)
  717. cxgb4_remove_tid(lldi->tids, csk->port_id, GET_TID(rpl));
  718. cxgbi_sock_get(csk);
  719. spin_lock_bh(&csk->lock);
  720. if (status == CPL_ERR_CONN_EXIST &&
  721. csk->retry_timer.function != csk_act_open_retry_timer) {
  722. csk->retry_timer.function = csk_act_open_retry_timer;
  723. mod_timer(&csk->retry_timer, jiffies + HZ / 2);
  724. } else
  725. cxgbi_sock_fail_act_open(csk,
  726. act_open_rpl_status_to_errno(status));
  727. spin_unlock_bh(&csk->lock);
  728. cxgbi_sock_put(csk);
  729. rel_skb:
  730. __kfree_skb(skb);
  731. }
  732. static void do_peer_close(struct cxgbi_device *cdev, struct sk_buff *skb)
  733. {
  734. struct cxgbi_sock *csk;
  735. struct cpl_peer_close *req = (struct cpl_peer_close *)skb->data;
  736. unsigned int tid = GET_TID(req);
  737. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  738. struct tid_info *t = lldi->tids;
  739. csk = lookup_tid(t, tid);
  740. if (unlikely(!csk)) {
  741. pr_err("can't find connection for tid %u.\n", tid);
  742. goto rel_skb;
  743. }
  744. pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u.\n",
  745. (&csk->saddr), (&csk->daddr),
  746. csk, csk->state, csk->flags, csk->tid);
  747. cxgbi_sock_rcv_peer_close(csk);
  748. rel_skb:
  749. __kfree_skb(skb);
  750. }
  751. static void do_close_con_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
  752. {
  753. struct cxgbi_sock *csk;
  754. struct cpl_close_con_rpl *rpl = (struct cpl_close_con_rpl *)skb->data;
  755. unsigned int tid = GET_TID(rpl);
  756. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  757. struct tid_info *t = lldi->tids;
  758. csk = lookup_tid(t, tid);
  759. if (unlikely(!csk)) {
  760. pr_err("can't find connection for tid %u.\n", tid);
  761. goto rel_skb;
  762. }
  763. pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u.\n",
  764. (&csk->saddr), (&csk->daddr),
  765. csk, csk->state, csk->flags, csk->tid);
  766. cxgbi_sock_rcv_close_conn_rpl(csk, ntohl(rpl->snd_nxt));
  767. rel_skb:
  768. __kfree_skb(skb);
  769. }
  770. static int abort_status_to_errno(struct cxgbi_sock *csk, int abort_reason,
  771. int *need_rst)
  772. {
  773. switch (abort_reason) {
  774. case CPL_ERR_BAD_SYN: /* fall through */
  775. case CPL_ERR_CONN_RESET:
  776. return csk->state > CTP_ESTABLISHED ?
  777. -EPIPE : -ECONNRESET;
  778. case CPL_ERR_XMIT_TIMEDOUT:
  779. case CPL_ERR_PERSIST_TIMEDOUT:
  780. case CPL_ERR_FINWAIT2_TIMEDOUT:
  781. case CPL_ERR_KEEPALIVE_TIMEDOUT:
  782. return -ETIMEDOUT;
  783. default:
  784. return -EIO;
  785. }
  786. }
  787. static void do_abort_req_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
  788. {
  789. struct cxgbi_sock *csk;
  790. struct cpl_abort_req_rss *req = (struct cpl_abort_req_rss *)skb->data;
  791. unsigned int tid = GET_TID(req);
  792. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  793. struct tid_info *t = lldi->tids;
  794. int rst_status = CPL_ABORT_NO_RST;
  795. csk = lookup_tid(t, tid);
  796. if (unlikely(!csk)) {
  797. pr_err("can't find connection for tid %u.\n", tid);
  798. goto rel_skb;
  799. }
  800. pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u, status %u.\n",
  801. (&csk->saddr), (&csk->daddr),
  802. csk, csk->state, csk->flags, csk->tid, req->status);
  803. if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
  804. req->status == CPL_ERR_PERSIST_NEG_ADVICE)
  805. goto rel_skb;
  806. cxgbi_sock_get(csk);
  807. spin_lock_bh(&csk->lock);
  808. cxgbi_sock_clear_flag(csk, CTPF_ABORT_REQ_RCVD);
  809. if (!cxgbi_sock_flag(csk, CTPF_TX_DATA_SENT)) {
  810. send_tx_flowc_wr(csk);
  811. cxgbi_sock_set_flag(csk, CTPF_TX_DATA_SENT);
  812. }
  813. cxgbi_sock_set_flag(csk, CTPF_ABORT_REQ_RCVD);
  814. cxgbi_sock_set_state(csk, CTP_ABORTING);
  815. send_abort_rpl(csk, rst_status);
  816. if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
  817. csk->err = abort_status_to_errno(csk, req->status, &rst_status);
  818. cxgbi_sock_closed(csk);
  819. }
  820. spin_unlock_bh(&csk->lock);
  821. cxgbi_sock_put(csk);
  822. rel_skb:
  823. __kfree_skb(skb);
  824. }
  825. static void do_abort_rpl_rss(struct cxgbi_device *cdev, struct sk_buff *skb)
  826. {
  827. struct cxgbi_sock *csk;
  828. struct cpl_abort_rpl_rss *rpl = (struct cpl_abort_rpl_rss *)skb->data;
  829. unsigned int tid = GET_TID(rpl);
  830. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  831. struct tid_info *t = lldi->tids;
  832. csk = lookup_tid(t, tid);
  833. if (!csk)
  834. goto rel_skb;
  835. if (csk)
  836. pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u, status %u.\n",
  837. (&csk->saddr), (&csk->daddr), csk,
  838. csk->state, csk->flags, csk->tid, rpl->status);
  839. if (rpl->status == CPL_ERR_ABORT_FAILED)
  840. goto rel_skb;
  841. cxgbi_sock_rcv_abort_rpl(csk);
  842. rel_skb:
  843. __kfree_skb(skb);
  844. }
  845. static void do_rx_iscsi_hdr(struct cxgbi_device *cdev, struct sk_buff *skb)
  846. {
  847. struct cxgbi_sock *csk;
  848. struct cpl_iscsi_hdr *cpl = (struct cpl_iscsi_hdr *)skb->data;
  849. unsigned short pdu_len_ddp = be16_to_cpu(cpl->pdu_len_ddp);
  850. unsigned int tid = GET_TID(cpl);
  851. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  852. struct tid_info *t = lldi->tids;
  853. csk = lookup_tid(t, tid);
  854. if (unlikely(!csk)) {
  855. pr_err("can't find conn. for tid %u.\n", tid);
  856. goto rel_skb;
  857. }
  858. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  859. "csk 0x%p,%u,0x%lx, tid %u, skb 0x%p,%u, 0x%x.\n",
  860. csk, csk->state, csk->flags, csk->tid, skb, skb->len,
  861. pdu_len_ddp);
  862. spin_lock_bh(&csk->lock);
  863. if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
  864. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  865. "csk 0x%p,%u,0x%lx,%u, bad state.\n",
  866. csk, csk->state, csk->flags, csk->tid);
  867. if (csk->state != CTP_ABORTING)
  868. goto abort_conn;
  869. else
  870. goto discard;
  871. }
  872. cxgbi_skcb_tcp_seq(skb) = ntohl(cpl->seq);
  873. cxgbi_skcb_flags(skb) = 0;
  874. skb_reset_transport_header(skb);
  875. __skb_pull(skb, sizeof(*cpl));
  876. __pskb_trim(skb, ntohs(cpl->len));
  877. if (!csk->skb_ulp_lhdr) {
  878. unsigned char *bhs;
  879. unsigned int hlen, dlen, plen;
  880. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  881. "csk 0x%p,%u,0x%lx, tid %u, skb 0x%p header.\n",
  882. csk, csk->state, csk->flags, csk->tid, skb);
  883. csk->skb_ulp_lhdr = skb;
  884. cxgbi_skcb_set_flag(skb, SKCBF_RX_HDR);
  885. if (cxgbi_skcb_tcp_seq(skb) != csk->rcv_nxt) {
  886. pr_info("tid %u, CPL_ISCSI_HDR, bad seq, 0x%x/0x%x.\n",
  887. csk->tid, cxgbi_skcb_tcp_seq(skb),
  888. csk->rcv_nxt);
  889. goto abort_conn;
  890. }
  891. bhs = skb->data;
  892. hlen = ntohs(cpl->len);
  893. dlen = ntohl(*(unsigned int *)(bhs + 4)) & 0xFFFFFF;
  894. plen = ISCSI_PDU_LEN(pdu_len_ddp);
  895. if (is_t4(lldi->adapter_type))
  896. plen -= 40;
  897. if ((hlen + dlen) != plen) {
  898. pr_info("tid 0x%x, CPL_ISCSI_HDR, pdu len "
  899. "mismatch %u != %u + %u, seq 0x%x.\n",
  900. csk->tid, plen, hlen, dlen,
  901. cxgbi_skcb_tcp_seq(skb));
  902. goto abort_conn;
  903. }
  904. cxgbi_skcb_rx_pdulen(skb) = (hlen + dlen + 3) & (~0x3);
  905. if (dlen)
  906. cxgbi_skcb_rx_pdulen(skb) += csk->dcrc_len;
  907. csk->rcv_nxt += cxgbi_skcb_rx_pdulen(skb);
  908. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  909. "csk 0x%p, skb 0x%p, 0x%x,%u+%u,0x%x,0x%x.\n",
  910. csk, skb, *bhs, hlen, dlen,
  911. ntohl(*((unsigned int *)(bhs + 16))),
  912. ntohl(*((unsigned int *)(bhs + 24))));
  913. } else {
  914. struct sk_buff *lskb = csk->skb_ulp_lhdr;
  915. cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA);
  916. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  917. "csk 0x%p,%u,0x%lx, skb 0x%p data, 0x%p.\n",
  918. csk, csk->state, csk->flags, skb, lskb);
  919. }
  920. __skb_queue_tail(&csk->receive_queue, skb);
  921. spin_unlock_bh(&csk->lock);
  922. return;
  923. abort_conn:
  924. send_abort_req(csk);
  925. discard:
  926. spin_unlock_bh(&csk->lock);
  927. rel_skb:
  928. __kfree_skb(skb);
  929. }
  930. static void do_rx_data_ddp(struct cxgbi_device *cdev,
  931. struct sk_buff *skb)
  932. {
  933. struct cxgbi_sock *csk;
  934. struct sk_buff *lskb;
  935. struct cpl_rx_data_ddp *rpl = (struct cpl_rx_data_ddp *)skb->data;
  936. unsigned int tid = GET_TID(rpl);
  937. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  938. struct tid_info *t = lldi->tids;
  939. unsigned int status = ntohl(rpl->ddpvld);
  940. csk = lookup_tid(t, tid);
  941. if (unlikely(!csk)) {
  942. pr_err("can't find connection for tid %u.\n", tid);
  943. goto rel_skb;
  944. }
  945. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_PDU_RX,
  946. "csk 0x%p,%u,0x%lx, skb 0x%p,0x%x, lhdr 0x%p.\n",
  947. csk, csk->state, csk->flags, skb, status, csk->skb_ulp_lhdr);
  948. spin_lock_bh(&csk->lock);
  949. if (unlikely(csk->state >= CTP_PASSIVE_CLOSE)) {
  950. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  951. "csk 0x%p,%u,0x%lx,%u, bad state.\n",
  952. csk, csk->state, csk->flags, csk->tid);
  953. if (csk->state != CTP_ABORTING)
  954. goto abort_conn;
  955. else
  956. goto discard;
  957. }
  958. if (!csk->skb_ulp_lhdr) {
  959. pr_err("tid 0x%x, rcv RX_DATA_DDP w/o pdu bhs.\n", csk->tid);
  960. goto abort_conn;
  961. }
  962. lskb = csk->skb_ulp_lhdr;
  963. csk->skb_ulp_lhdr = NULL;
  964. cxgbi_skcb_rx_ddigest(lskb) = ntohl(rpl->ulp_crc);
  965. if (ntohs(rpl->len) != cxgbi_skcb_rx_pdulen(lskb))
  966. pr_info("tid 0x%x, RX_DATA_DDP pdulen %u != %u.\n",
  967. csk->tid, ntohs(rpl->len), cxgbi_skcb_rx_pdulen(lskb));
  968. if (status & (1 << CPL_RX_DDP_STATUS_HCRC_SHIFT)) {
  969. pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, hcrc bad 0x%lx.\n",
  970. csk, lskb, status, cxgbi_skcb_flags(lskb));
  971. cxgbi_skcb_set_flag(lskb, SKCBF_RX_HCRC_ERR);
  972. }
  973. if (status & (1 << CPL_RX_DDP_STATUS_DCRC_SHIFT)) {
  974. pr_info("csk 0x%p, lhdr 0x%p, status 0x%x, dcrc bad 0x%lx.\n",
  975. csk, lskb, status, cxgbi_skcb_flags(lskb));
  976. cxgbi_skcb_set_flag(lskb, SKCBF_RX_DCRC_ERR);
  977. }
  978. if (status & (1 << CPL_RX_DDP_STATUS_PAD_SHIFT)) {
  979. log_debug(1 << CXGBI_DBG_PDU_RX,
  980. "csk 0x%p, lhdr 0x%p, status 0x%x, pad bad.\n",
  981. csk, lskb, status);
  982. cxgbi_skcb_set_flag(lskb, SKCBF_RX_PAD_ERR);
  983. }
  984. if ((status & (1 << CPL_RX_DDP_STATUS_DDP_SHIFT)) &&
  985. !cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA)) {
  986. log_debug(1 << CXGBI_DBG_PDU_RX,
  987. "csk 0x%p, lhdr 0x%p, 0x%x, data ddp'ed.\n",
  988. csk, lskb, status);
  989. cxgbi_skcb_set_flag(lskb, SKCBF_RX_DATA_DDPD);
  990. }
  991. log_debug(1 << CXGBI_DBG_PDU_RX,
  992. "csk 0x%p, lskb 0x%p, f 0x%lx.\n",
  993. csk, lskb, cxgbi_skcb_flags(lskb));
  994. cxgbi_skcb_set_flag(lskb, SKCBF_RX_STATUS);
  995. cxgbi_conn_pdu_ready(csk);
  996. spin_unlock_bh(&csk->lock);
  997. goto rel_skb;
  998. abort_conn:
  999. send_abort_req(csk);
  1000. discard:
  1001. spin_unlock_bh(&csk->lock);
  1002. rel_skb:
  1003. __kfree_skb(skb);
  1004. }
  1005. static void do_fw4_ack(struct cxgbi_device *cdev, struct sk_buff *skb)
  1006. {
  1007. struct cxgbi_sock *csk;
  1008. struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)skb->data;
  1009. unsigned int tid = GET_TID(rpl);
  1010. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  1011. struct tid_info *t = lldi->tids;
  1012. csk = lookup_tid(t, tid);
  1013. if (unlikely(!csk))
  1014. pr_err("can't find connection for tid %u.\n", tid);
  1015. else {
  1016. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  1017. "csk 0x%p,%u,0x%lx,%u.\n",
  1018. csk, csk->state, csk->flags, csk->tid);
  1019. cxgbi_sock_rcv_wr_ack(csk, rpl->credits, ntohl(rpl->snd_una),
  1020. rpl->seq_vld);
  1021. }
  1022. __kfree_skb(skb);
  1023. }
  1024. static void do_set_tcb_rpl(struct cxgbi_device *cdev, struct sk_buff *skb)
  1025. {
  1026. struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
  1027. unsigned int tid = GET_TID(rpl);
  1028. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  1029. struct tid_info *t = lldi->tids;
  1030. struct cxgbi_sock *csk;
  1031. csk = lookup_tid(t, tid);
  1032. if (!csk)
  1033. pr_err("can't find conn. for tid %u.\n", tid);
  1034. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  1035. "csk 0x%p,%u,%lx,%u, status 0x%x.\n",
  1036. csk, csk->state, csk->flags, csk->tid, rpl->status);
  1037. if (rpl->status != CPL_ERR_NONE)
  1038. pr_err("csk 0x%p,%u, SET_TCB_RPL status %u.\n",
  1039. csk, tid, rpl->status);
  1040. __kfree_skb(skb);
  1041. }
  1042. static int alloc_cpls(struct cxgbi_sock *csk)
  1043. {
  1044. csk->cpl_close = alloc_wr(sizeof(struct cpl_close_con_req),
  1045. 0, GFP_KERNEL);
  1046. if (!csk->cpl_close)
  1047. return -ENOMEM;
  1048. csk->cpl_abort_req = alloc_wr(sizeof(struct cpl_abort_req),
  1049. 0, GFP_KERNEL);
  1050. if (!csk->cpl_abort_req)
  1051. goto free_cpls;
  1052. csk->cpl_abort_rpl = alloc_wr(sizeof(struct cpl_abort_rpl),
  1053. 0, GFP_KERNEL);
  1054. if (!csk->cpl_abort_rpl)
  1055. goto free_cpls;
  1056. return 0;
  1057. free_cpls:
  1058. cxgbi_sock_free_cpl_skbs(csk);
  1059. return -ENOMEM;
  1060. }
  1061. static inline void l2t_put(struct cxgbi_sock *csk)
  1062. {
  1063. if (csk->l2t) {
  1064. cxgb4_l2t_release(csk->l2t);
  1065. csk->l2t = NULL;
  1066. cxgbi_sock_put(csk);
  1067. }
  1068. }
  1069. static void release_offload_resources(struct cxgbi_sock *csk)
  1070. {
  1071. struct cxgb4_lld_info *lldi;
  1072. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  1073. "csk 0x%p,%u,0x%lx,%u.\n",
  1074. csk, csk->state, csk->flags, csk->tid);
  1075. cxgbi_sock_free_cpl_skbs(csk);
  1076. if (csk->wr_cred != csk->wr_max_cred) {
  1077. cxgbi_sock_purge_wr_queue(csk);
  1078. cxgbi_sock_reset_wr_list(csk);
  1079. }
  1080. l2t_put(csk);
  1081. if (cxgbi_sock_flag(csk, CTPF_HAS_ATID))
  1082. free_atid(csk);
  1083. else if (cxgbi_sock_flag(csk, CTPF_HAS_TID)) {
  1084. lldi = cxgbi_cdev_priv(csk->cdev);
  1085. cxgb4_remove_tid(lldi->tids, 0, csk->tid);
  1086. cxgbi_sock_clear_flag(csk, CTPF_HAS_TID);
  1087. cxgbi_sock_put(csk);
  1088. }
  1089. csk->dst = NULL;
  1090. csk->cdev = NULL;
  1091. }
  1092. static int init_act_open(struct cxgbi_sock *csk)
  1093. {
  1094. struct cxgbi_device *cdev = csk->cdev;
  1095. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  1096. struct net_device *ndev = cdev->ports[csk->port_id];
  1097. struct sk_buff *skb = NULL;
  1098. struct neighbour *n = NULL;
  1099. void *daddr;
  1100. unsigned int step;
  1101. unsigned int size, size6;
  1102. int t4 = is_t4(lldi->adapter_type);
  1103. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  1104. "csk 0x%p,%u,0x%lx,%u.\n",
  1105. csk, csk->state, csk->flags, csk->tid);
  1106. if (csk->csk_family == AF_INET)
  1107. daddr = &csk->daddr.sin_addr.s_addr;
  1108. #if IS_ENABLED(CONFIG_IPV6)
  1109. else if (csk->csk_family == AF_INET6)
  1110. daddr = &csk->daddr6.sin6_addr;
  1111. #endif
  1112. else {
  1113. pr_err("address family 0x%x not supported\n", csk->csk_family);
  1114. goto rel_resource;
  1115. }
  1116. n = dst_neigh_lookup(csk->dst, daddr);
  1117. if (!n) {
  1118. pr_err("%s, can't get neighbour of csk->dst.\n", ndev->name);
  1119. goto rel_resource;
  1120. }
  1121. csk->atid = cxgb4_alloc_atid(lldi->tids, csk);
  1122. if (csk->atid < 0) {
  1123. pr_err("%s, NO atid available.\n", ndev->name);
  1124. return -EINVAL;
  1125. }
  1126. cxgbi_sock_set_flag(csk, CTPF_HAS_ATID);
  1127. cxgbi_sock_get(csk);
  1128. csk->l2t = cxgb4_l2t_get(lldi->l2t, n, ndev, 0);
  1129. if (!csk->l2t) {
  1130. pr_err("%s, cannot alloc l2t.\n", ndev->name);
  1131. goto rel_resource;
  1132. }
  1133. cxgbi_sock_get(csk);
  1134. if (t4) {
  1135. size = sizeof(struct cpl_act_open_req);
  1136. size6 = sizeof(struct cpl_act_open_req6);
  1137. } else {
  1138. size = sizeof(struct cpl_t5_act_open_req);
  1139. size6 = sizeof(struct cpl_t5_act_open_req6);
  1140. }
  1141. if (csk->csk_family == AF_INET)
  1142. skb = alloc_wr(size, 0, GFP_NOIO);
  1143. #if IS_ENABLED(CONFIG_IPV6)
  1144. else
  1145. skb = alloc_wr(size6, 0, GFP_NOIO);
  1146. #endif
  1147. if (!skb)
  1148. goto rel_resource;
  1149. skb->sk = (struct sock *)csk;
  1150. t4_set_arp_err_handler(skb, csk, cxgbi_sock_act_open_req_arp_failure);
  1151. if (!csk->mtu)
  1152. csk->mtu = dst_mtu(csk->dst);
  1153. cxgb4_best_mtu(lldi->mtus, csk->mtu, &csk->mss_idx);
  1154. csk->tx_chan = cxgb4_port_chan(ndev);
  1155. /* SMT two entries per row */
  1156. csk->smac_idx = ((cxgb4_port_viid(ndev) & 0x7F)) << 1;
  1157. step = lldi->ntxq / lldi->nchan;
  1158. csk->txq_idx = cxgb4_port_idx(ndev) * step;
  1159. step = lldi->nrxq / lldi->nchan;
  1160. csk->rss_qid = lldi->rxq_ids[cxgb4_port_idx(ndev) * step];
  1161. csk->wr_cred = lldi->wr_cred -
  1162. DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
  1163. csk->wr_max_cred = csk->wr_cred;
  1164. csk->wr_una_cred = 0;
  1165. cxgbi_sock_reset_wr_list(csk);
  1166. csk->err = 0;
  1167. pr_info_ipaddr("csk 0x%p,%u,0x%lx,%u,%u,%u, mtu %u,%u, smac %u.\n",
  1168. (&csk->saddr), (&csk->daddr), csk, csk->state,
  1169. csk->flags, csk->tx_chan, csk->txq_idx, csk->rss_qid,
  1170. csk->mtu, csk->mss_idx, csk->smac_idx);
  1171. /* must wait for either a act_open_rpl or act_open_establish */
  1172. try_module_get(THIS_MODULE);
  1173. cxgbi_sock_set_state(csk, CTP_ACTIVE_OPEN);
  1174. if (csk->csk_family == AF_INET)
  1175. send_act_open_req(csk, skb, csk->l2t);
  1176. #if IS_ENABLED(CONFIG_IPV6)
  1177. else
  1178. send_act_open_req6(csk, skb, csk->l2t);
  1179. #endif
  1180. neigh_release(n);
  1181. return 0;
  1182. rel_resource:
  1183. if (n)
  1184. neigh_release(n);
  1185. if (skb)
  1186. __kfree_skb(skb);
  1187. return -EINVAL;
  1188. }
  1189. cxgb4i_cplhandler_func cxgb4i_cplhandlers[NUM_CPL_CMDS] = {
  1190. [CPL_ACT_ESTABLISH] = do_act_establish,
  1191. [CPL_ACT_OPEN_RPL] = do_act_open_rpl,
  1192. [CPL_PEER_CLOSE] = do_peer_close,
  1193. [CPL_ABORT_REQ_RSS] = do_abort_req_rss,
  1194. [CPL_ABORT_RPL_RSS] = do_abort_rpl_rss,
  1195. [CPL_CLOSE_CON_RPL] = do_close_con_rpl,
  1196. [CPL_FW4_ACK] = do_fw4_ack,
  1197. [CPL_ISCSI_HDR] = do_rx_iscsi_hdr,
  1198. [CPL_ISCSI_DATA] = do_rx_iscsi_hdr,
  1199. [CPL_SET_TCB_RPL] = do_set_tcb_rpl,
  1200. [CPL_RX_DATA_DDP] = do_rx_data_ddp,
  1201. [CPL_RX_ISCSI_DDP] = do_rx_data_ddp,
  1202. };
  1203. int cxgb4i_ofld_init(struct cxgbi_device *cdev)
  1204. {
  1205. int rc;
  1206. if (cxgb4i_max_connect > CXGB4I_MAX_CONN)
  1207. cxgb4i_max_connect = CXGB4I_MAX_CONN;
  1208. rc = cxgbi_device_portmap_create(cdev, cxgb4i_sport_base,
  1209. cxgb4i_max_connect);
  1210. if (rc < 0)
  1211. return rc;
  1212. cdev->csk_release_offload_resources = release_offload_resources;
  1213. cdev->csk_push_tx_frames = push_tx_frames;
  1214. cdev->csk_send_abort_req = send_abort_req;
  1215. cdev->csk_send_close_req = send_close_req;
  1216. cdev->csk_send_rx_credits = send_rx_credits;
  1217. cdev->csk_alloc_cpls = alloc_cpls;
  1218. cdev->csk_init_act_open = init_act_open;
  1219. pr_info("cdev 0x%p, offload up, added.\n", cdev);
  1220. return 0;
  1221. }
  1222. /*
  1223. * functions to program the pagepod in h/w
  1224. */
  1225. #define ULPMEM_IDATA_MAX_NPPODS 4 /* 256/PPOD_SIZE */
  1226. static inline void ulp_mem_io_set_hdr(struct cxgb4_lld_info *lldi,
  1227. struct ulp_mem_io *req,
  1228. unsigned int wr_len, unsigned int dlen,
  1229. unsigned int pm_addr)
  1230. {
  1231. struct ulptx_idata *idata = (struct ulptx_idata *)(req + 1);
  1232. INIT_ULPTX_WR(req, wr_len, 0, 0);
  1233. if (is_t4(lldi->adapter_type))
  1234. req->cmd = htonl(ULPTX_CMD(ULP_TX_MEM_WRITE) |
  1235. (ULP_MEMIO_ORDER(1)));
  1236. else
  1237. req->cmd = htonl(ULPTX_CMD(ULP_TX_MEM_WRITE) |
  1238. (V_T5_ULP_MEMIO_IMM(1)));
  1239. req->dlen = htonl(ULP_MEMIO_DATA_LEN(dlen >> 5));
  1240. req->lock_addr = htonl(ULP_MEMIO_ADDR(pm_addr >> 5));
  1241. req->len16 = htonl(DIV_ROUND_UP(wr_len - sizeof(req->wr), 16));
  1242. idata->cmd_more = htonl(ULPTX_CMD(ULP_TX_SC_IMM));
  1243. idata->len = htonl(dlen);
  1244. }
  1245. static int ddp_ppod_write_idata(struct cxgbi_device *cdev, unsigned int port_id,
  1246. struct cxgbi_pagepod_hdr *hdr, unsigned int idx,
  1247. unsigned int npods,
  1248. struct cxgbi_gather_list *gl,
  1249. unsigned int gl_pidx)
  1250. {
  1251. struct cxgbi_ddp_info *ddp = cdev->ddp;
  1252. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  1253. struct sk_buff *skb;
  1254. struct ulp_mem_io *req;
  1255. struct ulptx_idata *idata;
  1256. struct cxgbi_pagepod *ppod;
  1257. unsigned int pm_addr = idx * PPOD_SIZE + ddp->llimit;
  1258. unsigned int dlen = PPOD_SIZE * npods;
  1259. unsigned int wr_len = roundup(sizeof(struct ulp_mem_io) +
  1260. sizeof(struct ulptx_idata) + dlen, 16);
  1261. unsigned int i;
  1262. skb = alloc_wr(wr_len, 0, GFP_ATOMIC);
  1263. if (!skb) {
  1264. pr_err("cdev 0x%p, idx %u, npods %u, OOM.\n",
  1265. cdev, idx, npods);
  1266. return -ENOMEM;
  1267. }
  1268. req = (struct ulp_mem_io *)skb->head;
  1269. set_queue(skb, CPL_PRIORITY_CONTROL, NULL);
  1270. ulp_mem_io_set_hdr(lldi, req, wr_len, dlen, pm_addr);
  1271. idata = (struct ulptx_idata *)(req + 1);
  1272. ppod = (struct cxgbi_pagepod *)(idata + 1);
  1273. for (i = 0; i < npods; i++, ppod++, gl_pidx += PPOD_PAGES_MAX) {
  1274. if (!hdr && !gl)
  1275. cxgbi_ddp_ppod_clear(ppod);
  1276. else
  1277. cxgbi_ddp_ppod_set(ppod, hdr, gl, gl_pidx);
  1278. }
  1279. cxgb4_ofld_send(cdev->ports[port_id], skb);
  1280. return 0;
  1281. }
  1282. static int ddp_set_map(struct cxgbi_sock *csk, struct cxgbi_pagepod_hdr *hdr,
  1283. unsigned int idx, unsigned int npods,
  1284. struct cxgbi_gather_list *gl)
  1285. {
  1286. unsigned int i, cnt;
  1287. int err = 0;
  1288. for (i = 0; i < npods; i += cnt, idx += cnt) {
  1289. cnt = npods - i;
  1290. if (cnt > ULPMEM_IDATA_MAX_NPPODS)
  1291. cnt = ULPMEM_IDATA_MAX_NPPODS;
  1292. err = ddp_ppod_write_idata(csk->cdev, csk->port_id, hdr,
  1293. idx, cnt, gl, 4 * i);
  1294. if (err < 0)
  1295. break;
  1296. }
  1297. return err;
  1298. }
  1299. static void ddp_clear_map(struct cxgbi_hba *chba, unsigned int tag,
  1300. unsigned int idx, unsigned int npods)
  1301. {
  1302. unsigned int i, cnt;
  1303. int err;
  1304. for (i = 0; i < npods; i += cnt, idx += cnt) {
  1305. cnt = npods - i;
  1306. if (cnt > ULPMEM_IDATA_MAX_NPPODS)
  1307. cnt = ULPMEM_IDATA_MAX_NPPODS;
  1308. err = ddp_ppod_write_idata(chba->cdev, chba->port_id, NULL,
  1309. idx, cnt, NULL, 0);
  1310. if (err < 0)
  1311. break;
  1312. }
  1313. }
  1314. static int ddp_setup_conn_pgidx(struct cxgbi_sock *csk, unsigned int tid,
  1315. int pg_idx, bool reply)
  1316. {
  1317. struct sk_buff *skb;
  1318. struct cpl_set_tcb_field *req;
  1319. if (!pg_idx || pg_idx >= DDP_PGIDX_MAX)
  1320. return 0;
  1321. skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
  1322. if (!skb)
  1323. return -ENOMEM;
  1324. /* set up ulp page size */
  1325. req = (struct cpl_set_tcb_field *)skb->head;
  1326. INIT_TP_WR(req, csk->tid);
  1327. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
  1328. req->reply_ctrl = htons(NO_REPLY(reply) | QUEUENO(csk->rss_qid));
  1329. req->word_cookie = htons(0);
  1330. req->mask = cpu_to_be64(0x3 << 8);
  1331. req->val = cpu_to_be64(pg_idx << 8);
  1332. set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);
  1333. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  1334. "csk 0x%p, tid 0x%x, pg_idx %u.\n", csk, csk->tid, pg_idx);
  1335. cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
  1336. return 0;
  1337. }
  1338. static int ddp_setup_conn_digest(struct cxgbi_sock *csk, unsigned int tid,
  1339. int hcrc, int dcrc, int reply)
  1340. {
  1341. struct sk_buff *skb;
  1342. struct cpl_set_tcb_field *req;
  1343. if (!hcrc && !dcrc)
  1344. return 0;
  1345. skb = alloc_wr(sizeof(*req), 0, GFP_KERNEL);
  1346. if (!skb)
  1347. return -ENOMEM;
  1348. csk->hcrc_len = (hcrc ? 4 : 0);
  1349. csk->dcrc_len = (dcrc ? 4 : 0);
  1350. /* set up ulp submode */
  1351. req = (struct cpl_set_tcb_field *)skb->head;
  1352. INIT_TP_WR(req, tid);
  1353. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
  1354. req->reply_ctrl = htons(NO_REPLY(reply) | QUEUENO(csk->rss_qid));
  1355. req->word_cookie = htons(0);
  1356. req->mask = cpu_to_be64(0x3 << 4);
  1357. req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
  1358. (dcrc ? ULP_CRC_DATA : 0)) << 4);
  1359. set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->port_id);
  1360. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  1361. "csk 0x%p, tid 0x%x, crc %d,%d.\n", csk, csk->tid, hcrc, dcrc);
  1362. cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
  1363. return 0;
  1364. }
  1365. static int cxgb4i_ddp_init(struct cxgbi_device *cdev)
  1366. {
  1367. struct cxgb4_lld_info *lldi = cxgbi_cdev_priv(cdev);
  1368. struct cxgbi_ddp_info *ddp = cdev->ddp;
  1369. unsigned int tagmask, pgsz_factor[4];
  1370. int err;
  1371. if (ddp) {
  1372. kref_get(&ddp->refcnt);
  1373. pr_warn("cdev 0x%p, ddp 0x%p already set up.\n",
  1374. cdev, cdev->ddp);
  1375. return -EALREADY;
  1376. }
  1377. err = cxgbi_ddp_init(cdev, lldi->vr->iscsi.start,
  1378. lldi->vr->iscsi.start + lldi->vr->iscsi.size - 1,
  1379. lldi->iscsi_iolen, lldi->iscsi_iolen);
  1380. if (err < 0)
  1381. return err;
  1382. ddp = cdev->ddp;
  1383. tagmask = ddp->idx_mask << PPOD_IDX_SHIFT;
  1384. cxgbi_ddp_page_size_factor(pgsz_factor);
  1385. cxgb4_iscsi_init(lldi->ports[0], tagmask, pgsz_factor);
  1386. cdev->csk_ddp_setup_digest = ddp_setup_conn_digest;
  1387. cdev->csk_ddp_setup_pgidx = ddp_setup_conn_pgidx;
  1388. cdev->csk_ddp_set = ddp_set_map;
  1389. cdev->csk_ddp_clear = ddp_clear_map;
  1390. pr_info("cxgb4i 0x%p tag: sw %u, rsvd %u,%u, mask 0x%x.\n",
  1391. cdev, cdev->tag_format.sw_bits, cdev->tag_format.rsvd_bits,
  1392. cdev->tag_format.rsvd_shift, cdev->tag_format.rsvd_mask);
  1393. pr_info("cxgb4i 0x%p, nppods %u, bits %u, mask 0x%x,0x%x pkt %u/%u, "
  1394. " %u/%u.\n",
  1395. cdev, ddp->nppods, ddp->idx_bits, ddp->idx_mask,
  1396. ddp->rsvd_tag_mask, ddp->max_txsz, lldi->iscsi_iolen,
  1397. ddp->max_rxsz, lldi->iscsi_iolen);
  1398. pr_info("cxgb4i 0x%p max payload size: %u/%u, %u/%u.\n",
  1399. cdev, cdev->tx_max_size, ddp->max_txsz, cdev->rx_max_size,
  1400. ddp->max_rxsz);
  1401. return 0;
  1402. }
  1403. static void *t4_uld_add(const struct cxgb4_lld_info *lldi)
  1404. {
  1405. struct cxgbi_device *cdev;
  1406. struct port_info *pi;
  1407. int i, rc;
  1408. cdev = cxgbi_device_register(sizeof(*lldi), lldi->nports);
  1409. if (!cdev) {
  1410. pr_info("t4 device 0x%p, register failed.\n", lldi);
  1411. return NULL;
  1412. }
  1413. pr_info("0x%p,0x%x, ports %u,%s, chan %u, q %u,%u, wr %u.\n",
  1414. cdev, lldi->adapter_type, lldi->nports,
  1415. lldi->ports[0]->name, lldi->nchan, lldi->ntxq,
  1416. lldi->nrxq, lldi->wr_cred);
  1417. for (i = 0; i < lldi->nrxq; i++)
  1418. log_debug(1 << CXGBI_DBG_DEV,
  1419. "t4 0x%p, rxq id #%d: %u.\n",
  1420. cdev, i, lldi->rxq_ids[i]);
  1421. memcpy(cxgbi_cdev_priv(cdev), lldi, sizeof(*lldi));
  1422. cdev->flags = CXGBI_FLAG_DEV_T4;
  1423. cdev->pdev = lldi->pdev;
  1424. cdev->ports = lldi->ports;
  1425. cdev->nports = lldi->nports;
  1426. cdev->mtus = lldi->mtus;
  1427. cdev->nmtus = NMTUS;
  1428. cdev->snd_win = cxgb4i_snd_win;
  1429. cdev->rcv_win = cxgb4i_rcv_win;
  1430. cdev->rx_credit_thres = cxgb4i_rx_credit_thres;
  1431. cdev->skb_tx_rsvd = CXGB4I_TX_HEADER_LEN;
  1432. cdev->skb_rx_extra = sizeof(struct cpl_iscsi_hdr);
  1433. cdev->itp = &cxgb4i_iscsi_transport;
  1434. cdev->pfvf = FW_VIID_PFN_GET(cxgb4_port_viid(lldi->ports[0])) << 8;
  1435. pr_info("cdev 0x%p,%s, pfvf %u.\n",
  1436. cdev, lldi->ports[0]->name, cdev->pfvf);
  1437. rc = cxgb4i_ddp_init(cdev);
  1438. if (rc) {
  1439. pr_info("t4 0x%p ddp init failed.\n", cdev);
  1440. goto err_out;
  1441. }
  1442. rc = cxgb4i_ofld_init(cdev);
  1443. if (rc) {
  1444. pr_info("t4 0x%p ofld init failed.\n", cdev);
  1445. goto err_out;
  1446. }
  1447. rc = cxgbi_hbas_add(cdev, CXGB4I_MAX_LUN, CXGBI_MAX_CONN,
  1448. &cxgb4i_host_template, cxgb4i_stt);
  1449. if (rc)
  1450. goto err_out;
  1451. for (i = 0; i < cdev->nports; i++) {
  1452. pi = netdev_priv(lldi->ports[i]);
  1453. cdev->hbas[i]->port_id = pi->port_id;
  1454. }
  1455. return cdev;
  1456. err_out:
  1457. cxgbi_device_unregister(cdev);
  1458. return ERR_PTR(-ENOMEM);
  1459. }
  1460. #define RX_PULL_LEN 128
  1461. static int t4_uld_rx_handler(void *handle, const __be64 *rsp,
  1462. const struct pkt_gl *pgl)
  1463. {
  1464. const struct cpl_act_establish *rpl;
  1465. struct sk_buff *skb;
  1466. unsigned int opc;
  1467. struct cxgbi_device *cdev = handle;
  1468. if (pgl == NULL) {
  1469. unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
  1470. skb = alloc_wr(len, 0, GFP_ATOMIC);
  1471. if (!skb)
  1472. goto nomem;
  1473. skb_copy_to_linear_data(skb, &rsp[1], len);
  1474. } else {
  1475. if (unlikely(*(u8 *)rsp != *(u8 *)pgl->va)) {
  1476. pr_info("? FL 0x%p,RSS%#llx,FL %#llx,len %u.\n",
  1477. pgl->va, be64_to_cpu(*rsp),
  1478. be64_to_cpu(*(u64 *)pgl->va),
  1479. pgl->tot_len);
  1480. return 0;
  1481. }
  1482. skb = cxgb4_pktgl_to_skb(pgl, RX_PULL_LEN, RX_PULL_LEN);
  1483. if (unlikely(!skb))
  1484. goto nomem;
  1485. }
  1486. rpl = (struct cpl_act_establish *)skb->data;
  1487. opc = rpl->ot.opcode;
  1488. log_debug(1 << CXGBI_DBG_TOE,
  1489. "cdev %p, opcode 0x%x(0x%x,0x%x), skb %p.\n",
  1490. cdev, opc, rpl->ot.opcode_tid, ntohl(rpl->ot.opcode_tid), skb);
  1491. if (cxgb4i_cplhandlers[opc])
  1492. cxgb4i_cplhandlers[opc](cdev, skb);
  1493. else {
  1494. pr_err("No handler for opcode 0x%x.\n", opc);
  1495. __kfree_skb(skb);
  1496. }
  1497. return 0;
  1498. nomem:
  1499. log_debug(1 << CXGBI_DBG_TOE, "OOM bailing out.\n");
  1500. return 1;
  1501. }
  1502. static int t4_uld_state_change(void *handle, enum cxgb4_state state)
  1503. {
  1504. struct cxgbi_device *cdev = handle;
  1505. switch (state) {
  1506. case CXGB4_STATE_UP:
  1507. pr_info("cdev 0x%p, UP.\n", cdev);
  1508. break;
  1509. case CXGB4_STATE_START_RECOVERY:
  1510. pr_info("cdev 0x%p, RECOVERY.\n", cdev);
  1511. /* close all connections */
  1512. break;
  1513. case CXGB4_STATE_DOWN:
  1514. pr_info("cdev 0x%p, DOWN.\n", cdev);
  1515. break;
  1516. case CXGB4_STATE_DETACH:
  1517. pr_info("cdev 0x%p, DETACH.\n", cdev);
  1518. cxgbi_device_unregister(cdev);
  1519. break;
  1520. default:
  1521. pr_info("cdev 0x%p, unknown state %d.\n", cdev, state);
  1522. break;
  1523. }
  1524. return 0;
  1525. }
  1526. static int __init cxgb4i_init_module(void)
  1527. {
  1528. int rc;
  1529. printk(KERN_INFO "%s", version);
  1530. rc = cxgbi_iscsi_init(&cxgb4i_iscsi_transport, &cxgb4i_stt);
  1531. if (rc < 0)
  1532. return rc;
  1533. cxgb4_register_uld(CXGB4_ULD_ISCSI, &cxgb4i_uld_info);
  1534. return 0;
  1535. }
  1536. static void __exit cxgb4i_exit_module(void)
  1537. {
  1538. cxgb4_unregister_uld(CXGB4_ULD_ISCSI);
  1539. cxgbi_device_unregister_all(CXGBI_FLAG_DEV_T4);
  1540. cxgbi_iscsi_cleanup(&cxgb4i_iscsi_transport, &cxgb4i_stt);
  1541. }
  1542. module_init(cxgb4i_init_module);
  1543. module_exit(cxgb4i_exit_module);