cxgb4i.c 48 KB

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