ib_isert.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801
  1. /*******************************************************************************
  2. * This file contains iSCSI extentions for RDMA (iSER) Verbs
  3. *
  4. * (c) Copyright 2013 Datera, Inc.
  5. *
  6. * Nicholas A. Bellinger <nab@linux-iscsi.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. ****************************************************************************/
  18. #include <linux/string.h>
  19. #include <linux/module.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/socket.h>
  22. #include <linux/in.h>
  23. #include <linux/in6.h>
  24. #include <linux/llist.h>
  25. #include <rdma/ib_verbs.h>
  26. #include <rdma/rdma_cm.h>
  27. #include <target/target_core_base.h>
  28. #include <target/target_core_fabric.h>
  29. #include <target/iscsi/iscsi_transport.h>
  30. #include "isert_proto.h"
  31. #include "ib_isert.h"
  32. #define ISERT_MAX_CONN 8
  33. #define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
  34. #define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
  35. static DEFINE_MUTEX(device_list_mutex);
  36. static LIST_HEAD(device_list);
  37. static struct workqueue_struct *isert_rx_wq;
  38. static struct workqueue_struct *isert_comp_wq;
  39. static void
  40. isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
  41. static int
  42. isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  43. struct isert_rdma_wr *wr);
  44. static void
  45. isert_unreg_rdma_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
  46. static int
  47. isert_reg_rdma_frwr(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  48. struct isert_rdma_wr *wr);
  49. static void
  50. isert_qp_event_callback(struct ib_event *e, void *context)
  51. {
  52. struct isert_conn *isert_conn = (struct isert_conn *)context;
  53. pr_err("isert_qp_event_callback event: %d\n", e->event);
  54. switch (e->event) {
  55. case IB_EVENT_COMM_EST:
  56. rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
  57. break;
  58. case IB_EVENT_QP_LAST_WQE_REACHED:
  59. pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. static int
  66. isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
  67. {
  68. int ret;
  69. ret = ib_query_device(ib_dev, devattr);
  70. if (ret) {
  71. pr_err("ib_query_device() failed: %d\n", ret);
  72. return ret;
  73. }
  74. pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
  75. pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
  76. return 0;
  77. }
  78. static int
  79. isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
  80. {
  81. struct isert_device *device = isert_conn->conn_device;
  82. struct ib_qp_init_attr attr;
  83. int ret, index, min_index = 0;
  84. mutex_lock(&device_list_mutex);
  85. for (index = 0; index < device->cqs_used; index++)
  86. if (device->cq_active_qps[index] <
  87. device->cq_active_qps[min_index])
  88. min_index = index;
  89. device->cq_active_qps[min_index]++;
  90. pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
  91. mutex_unlock(&device_list_mutex);
  92. memset(&attr, 0, sizeof(struct ib_qp_init_attr));
  93. attr.event_handler = isert_qp_event_callback;
  94. attr.qp_context = isert_conn;
  95. attr.send_cq = device->dev_tx_cq[min_index];
  96. attr.recv_cq = device->dev_rx_cq[min_index];
  97. attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
  98. attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
  99. /*
  100. * FIXME: Use devattr.max_sge - 2 for max_send_sge as
  101. * work-around for RDMA_READ..
  102. */
  103. attr.cap.max_send_sge = device->dev_attr.max_sge - 2;
  104. isert_conn->max_sge = attr.cap.max_send_sge;
  105. attr.cap.max_recv_sge = 1;
  106. attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  107. attr.qp_type = IB_QPT_RC;
  108. pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
  109. cma_id->device);
  110. pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
  111. isert_conn->conn_pd->device);
  112. ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
  113. if (ret) {
  114. pr_err("rdma_create_qp failed for cma_id %d\n", ret);
  115. return ret;
  116. }
  117. isert_conn->conn_qp = cma_id->qp;
  118. pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
  119. return 0;
  120. }
  121. static void
  122. isert_cq_event_callback(struct ib_event *e, void *context)
  123. {
  124. pr_debug("isert_cq_event_callback event: %d\n", e->event);
  125. }
  126. static int
  127. isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
  128. {
  129. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  130. struct iser_rx_desc *rx_desc;
  131. struct ib_sge *rx_sg;
  132. u64 dma_addr;
  133. int i, j;
  134. isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
  135. sizeof(struct iser_rx_desc), GFP_KERNEL);
  136. if (!isert_conn->conn_rx_descs)
  137. goto fail;
  138. rx_desc = isert_conn->conn_rx_descs;
  139. for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
  140. dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
  141. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  142. if (ib_dma_mapping_error(ib_dev, dma_addr))
  143. goto dma_map_fail;
  144. rx_desc->dma_addr = dma_addr;
  145. rx_sg = &rx_desc->rx_sg;
  146. rx_sg->addr = rx_desc->dma_addr;
  147. rx_sg->length = ISER_RX_PAYLOAD_SIZE;
  148. rx_sg->lkey = isert_conn->conn_mr->lkey;
  149. }
  150. isert_conn->conn_rx_desc_head = 0;
  151. return 0;
  152. dma_map_fail:
  153. rx_desc = isert_conn->conn_rx_descs;
  154. for (j = 0; j < i; j++, rx_desc++) {
  155. ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
  156. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  157. }
  158. kfree(isert_conn->conn_rx_descs);
  159. isert_conn->conn_rx_descs = NULL;
  160. fail:
  161. return -ENOMEM;
  162. }
  163. static void
  164. isert_free_rx_descriptors(struct isert_conn *isert_conn)
  165. {
  166. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  167. struct iser_rx_desc *rx_desc;
  168. int i;
  169. if (!isert_conn->conn_rx_descs)
  170. return;
  171. rx_desc = isert_conn->conn_rx_descs;
  172. for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
  173. ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
  174. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  175. }
  176. kfree(isert_conn->conn_rx_descs);
  177. isert_conn->conn_rx_descs = NULL;
  178. }
  179. static void isert_cq_tx_callback(struct ib_cq *, void *);
  180. static void isert_cq_rx_callback(struct ib_cq *, void *);
  181. static int
  182. isert_create_device_ib_res(struct isert_device *device)
  183. {
  184. struct ib_device *ib_dev = device->ib_device;
  185. struct isert_cq_desc *cq_desc;
  186. struct ib_device_attr *dev_attr;
  187. int ret = 0, i, j;
  188. dev_attr = &device->dev_attr;
  189. ret = isert_query_device(ib_dev, dev_attr);
  190. if (ret)
  191. return ret;
  192. /* asign function handlers */
  193. if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
  194. device->use_frwr = 1;
  195. device->reg_rdma_mem = isert_reg_rdma_frwr;
  196. device->unreg_rdma_mem = isert_unreg_rdma_frwr;
  197. } else {
  198. device->use_frwr = 0;
  199. device->reg_rdma_mem = isert_map_rdma;
  200. device->unreg_rdma_mem = isert_unmap_cmd;
  201. }
  202. device->cqs_used = min_t(int, num_online_cpus(),
  203. device->ib_device->num_comp_vectors);
  204. device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
  205. pr_debug("Using %d CQs, device %s supports %d vectors support FRWR %d\n",
  206. device->cqs_used, device->ib_device->name,
  207. device->ib_device->num_comp_vectors, device->use_frwr);
  208. device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
  209. device->cqs_used, GFP_KERNEL);
  210. if (!device->cq_desc) {
  211. pr_err("Unable to allocate device->cq_desc\n");
  212. return -ENOMEM;
  213. }
  214. cq_desc = device->cq_desc;
  215. device->dev_pd = ib_alloc_pd(ib_dev);
  216. if (IS_ERR(device->dev_pd)) {
  217. ret = PTR_ERR(device->dev_pd);
  218. pr_err("ib_alloc_pd failed for dev_pd: %d\n", ret);
  219. goto out_cq_desc;
  220. }
  221. for (i = 0; i < device->cqs_used; i++) {
  222. cq_desc[i].device = device;
  223. cq_desc[i].cq_index = i;
  224. device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
  225. isert_cq_rx_callback,
  226. isert_cq_event_callback,
  227. (void *)&cq_desc[i],
  228. ISER_MAX_RX_CQ_LEN, i);
  229. if (IS_ERR(device->dev_rx_cq[i]))
  230. goto out_cq;
  231. device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
  232. isert_cq_tx_callback,
  233. isert_cq_event_callback,
  234. (void *)&cq_desc[i],
  235. ISER_MAX_TX_CQ_LEN, i);
  236. if (IS_ERR(device->dev_tx_cq[i]))
  237. goto out_cq;
  238. if (ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP))
  239. goto out_cq;
  240. if (ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP))
  241. goto out_cq;
  242. }
  243. device->dev_mr = ib_get_dma_mr(device->dev_pd, IB_ACCESS_LOCAL_WRITE);
  244. if (IS_ERR(device->dev_mr)) {
  245. ret = PTR_ERR(device->dev_mr);
  246. pr_err("ib_get_dma_mr failed for dev_mr: %d\n", ret);
  247. goto out_cq;
  248. }
  249. return 0;
  250. out_cq:
  251. for (j = 0; j < i; j++) {
  252. cq_desc = &device->cq_desc[j];
  253. if (device->dev_rx_cq[j]) {
  254. cancel_work_sync(&cq_desc->cq_rx_work);
  255. ib_destroy_cq(device->dev_rx_cq[j]);
  256. }
  257. if (device->dev_tx_cq[j]) {
  258. cancel_work_sync(&cq_desc->cq_tx_work);
  259. ib_destroy_cq(device->dev_tx_cq[j]);
  260. }
  261. }
  262. ib_dealloc_pd(device->dev_pd);
  263. out_cq_desc:
  264. kfree(device->cq_desc);
  265. return ret;
  266. }
  267. static void
  268. isert_free_device_ib_res(struct isert_device *device)
  269. {
  270. struct isert_cq_desc *cq_desc;
  271. int i;
  272. for (i = 0; i < device->cqs_used; i++) {
  273. cq_desc = &device->cq_desc[i];
  274. cancel_work_sync(&cq_desc->cq_rx_work);
  275. cancel_work_sync(&cq_desc->cq_tx_work);
  276. ib_destroy_cq(device->dev_rx_cq[i]);
  277. ib_destroy_cq(device->dev_tx_cq[i]);
  278. device->dev_rx_cq[i] = NULL;
  279. device->dev_tx_cq[i] = NULL;
  280. }
  281. ib_dereg_mr(device->dev_mr);
  282. ib_dealloc_pd(device->dev_pd);
  283. kfree(device->cq_desc);
  284. }
  285. static void
  286. isert_device_try_release(struct isert_device *device)
  287. {
  288. mutex_lock(&device_list_mutex);
  289. device->refcount--;
  290. if (!device->refcount) {
  291. isert_free_device_ib_res(device);
  292. list_del(&device->dev_node);
  293. kfree(device);
  294. }
  295. mutex_unlock(&device_list_mutex);
  296. }
  297. static struct isert_device *
  298. isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
  299. {
  300. struct isert_device *device;
  301. int ret;
  302. mutex_lock(&device_list_mutex);
  303. list_for_each_entry(device, &device_list, dev_node) {
  304. if (device->ib_device->node_guid == cma_id->device->node_guid) {
  305. device->refcount++;
  306. mutex_unlock(&device_list_mutex);
  307. return device;
  308. }
  309. }
  310. device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
  311. if (!device) {
  312. mutex_unlock(&device_list_mutex);
  313. return ERR_PTR(-ENOMEM);
  314. }
  315. INIT_LIST_HEAD(&device->dev_node);
  316. device->ib_device = cma_id->device;
  317. ret = isert_create_device_ib_res(device);
  318. if (ret) {
  319. kfree(device);
  320. mutex_unlock(&device_list_mutex);
  321. return ERR_PTR(ret);
  322. }
  323. device->refcount++;
  324. list_add_tail(&device->dev_node, &device_list);
  325. mutex_unlock(&device_list_mutex);
  326. return device;
  327. }
  328. static void
  329. isert_conn_free_frwr_pool(struct isert_conn *isert_conn)
  330. {
  331. struct fast_reg_descriptor *fr_desc, *tmp;
  332. int i = 0;
  333. if (list_empty(&isert_conn->conn_frwr_pool))
  334. return;
  335. pr_debug("Freeing conn %p frwr pool", isert_conn);
  336. list_for_each_entry_safe(fr_desc, tmp,
  337. &isert_conn->conn_frwr_pool, list) {
  338. list_del(&fr_desc->list);
  339. ib_free_fast_reg_page_list(fr_desc->data_frpl);
  340. ib_dereg_mr(fr_desc->data_mr);
  341. kfree(fr_desc);
  342. ++i;
  343. }
  344. if (i < isert_conn->conn_frwr_pool_size)
  345. pr_warn("Pool still has %d regions registered\n",
  346. isert_conn->conn_frwr_pool_size - i);
  347. }
  348. static int
  349. isert_conn_create_frwr_pool(struct isert_conn *isert_conn)
  350. {
  351. struct fast_reg_descriptor *fr_desc;
  352. struct isert_device *device = isert_conn->conn_device;
  353. int i, ret;
  354. INIT_LIST_HEAD(&isert_conn->conn_frwr_pool);
  355. isert_conn->conn_frwr_pool_size = 0;
  356. for (i = 0; i < ISCSI_DEF_XMIT_CMDS_MAX; i++) {
  357. fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
  358. if (!fr_desc) {
  359. pr_err("Failed to allocate fast_reg descriptor\n");
  360. ret = -ENOMEM;
  361. goto err;
  362. }
  363. fr_desc->data_frpl =
  364. ib_alloc_fast_reg_page_list(device->ib_device,
  365. ISCSI_ISER_SG_TABLESIZE);
  366. if (IS_ERR(fr_desc->data_frpl)) {
  367. pr_err("Failed to allocate fr_pg_list err=%ld\n",
  368. PTR_ERR(fr_desc->data_frpl));
  369. ret = PTR_ERR(fr_desc->data_frpl);
  370. goto err;
  371. }
  372. fr_desc->data_mr = ib_alloc_fast_reg_mr(device->dev_pd,
  373. ISCSI_ISER_SG_TABLESIZE);
  374. if (IS_ERR(fr_desc->data_mr)) {
  375. pr_err("Failed to allocate frmr err=%ld\n",
  376. PTR_ERR(fr_desc->data_mr));
  377. ret = PTR_ERR(fr_desc->data_mr);
  378. ib_free_fast_reg_page_list(fr_desc->data_frpl);
  379. goto err;
  380. }
  381. pr_debug("Create fr_desc %p page_list %p\n",
  382. fr_desc, fr_desc->data_frpl->page_list);
  383. fr_desc->valid = true;
  384. list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
  385. isert_conn->conn_frwr_pool_size++;
  386. }
  387. pr_debug("Creating conn %p frwr pool size=%d",
  388. isert_conn, isert_conn->conn_frwr_pool_size);
  389. return 0;
  390. err:
  391. isert_conn_free_frwr_pool(isert_conn);
  392. return ret;
  393. }
  394. static int
  395. isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  396. {
  397. struct iscsi_np *np = cma_id->context;
  398. struct isert_np *isert_np = np->np_context;
  399. struct isert_conn *isert_conn;
  400. struct isert_device *device;
  401. struct ib_device *ib_dev = cma_id->device;
  402. int ret = 0;
  403. pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
  404. cma_id, cma_id->context);
  405. isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
  406. if (!isert_conn) {
  407. pr_err("Unable to allocate isert_conn\n");
  408. return -ENOMEM;
  409. }
  410. isert_conn->state = ISER_CONN_INIT;
  411. INIT_LIST_HEAD(&isert_conn->conn_accept_node);
  412. init_completion(&isert_conn->conn_login_comp);
  413. init_waitqueue_head(&isert_conn->conn_wait);
  414. init_waitqueue_head(&isert_conn->conn_wait_comp_err);
  415. kref_init(&isert_conn->conn_kref);
  416. kref_get(&isert_conn->conn_kref);
  417. mutex_init(&isert_conn->conn_mutex);
  418. mutex_init(&isert_conn->conn_comp_mutex);
  419. spin_lock_init(&isert_conn->conn_lock);
  420. cma_id->context = isert_conn;
  421. isert_conn->conn_cm_id = cma_id;
  422. isert_conn->responder_resources = event->param.conn.responder_resources;
  423. isert_conn->initiator_depth = event->param.conn.initiator_depth;
  424. pr_debug("Using responder_resources: %u initiator_depth: %u\n",
  425. isert_conn->responder_resources, isert_conn->initiator_depth);
  426. isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
  427. ISER_RX_LOGIN_SIZE, GFP_KERNEL);
  428. if (!isert_conn->login_buf) {
  429. pr_err("Unable to allocate isert_conn->login_buf\n");
  430. ret = -ENOMEM;
  431. goto out;
  432. }
  433. isert_conn->login_req_buf = isert_conn->login_buf;
  434. isert_conn->login_rsp_buf = isert_conn->login_buf +
  435. ISCSI_DEF_MAX_RECV_SEG_LEN;
  436. pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
  437. isert_conn->login_buf, isert_conn->login_req_buf,
  438. isert_conn->login_rsp_buf);
  439. isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
  440. (void *)isert_conn->login_req_buf,
  441. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  442. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
  443. if (ret) {
  444. pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
  445. ret);
  446. isert_conn->login_req_dma = 0;
  447. goto out_login_buf;
  448. }
  449. isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
  450. (void *)isert_conn->login_rsp_buf,
  451. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  452. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
  453. if (ret) {
  454. pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
  455. ret);
  456. isert_conn->login_rsp_dma = 0;
  457. goto out_req_dma_map;
  458. }
  459. device = isert_device_find_by_ib_dev(cma_id);
  460. if (IS_ERR(device)) {
  461. ret = PTR_ERR(device);
  462. goto out_rsp_dma_map;
  463. }
  464. isert_conn->conn_device = device;
  465. isert_conn->conn_pd = device->dev_pd;
  466. isert_conn->conn_mr = device->dev_mr;
  467. if (device->use_frwr) {
  468. ret = isert_conn_create_frwr_pool(isert_conn);
  469. if (ret) {
  470. pr_err("Conn: %p failed to create frwr_pool\n", isert_conn);
  471. goto out_frwr;
  472. }
  473. }
  474. ret = isert_conn_setup_qp(isert_conn, cma_id);
  475. if (ret)
  476. goto out_conn_dev;
  477. mutex_lock(&isert_np->np_accept_mutex);
  478. list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
  479. mutex_unlock(&isert_np->np_accept_mutex);
  480. pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
  481. wake_up(&isert_np->np_accept_wq);
  482. return 0;
  483. out_conn_dev:
  484. if (device->use_frwr)
  485. isert_conn_free_frwr_pool(isert_conn);
  486. out_frwr:
  487. isert_device_try_release(device);
  488. out_rsp_dma_map:
  489. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  490. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  491. out_req_dma_map:
  492. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  493. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  494. out_login_buf:
  495. kfree(isert_conn->login_buf);
  496. out:
  497. kfree(isert_conn);
  498. return ret;
  499. }
  500. static void
  501. isert_connect_release(struct isert_conn *isert_conn)
  502. {
  503. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  504. struct isert_device *device = isert_conn->conn_device;
  505. int cq_index;
  506. pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  507. if (device->use_frwr)
  508. isert_conn_free_frwr_pool(isert_conn);
  509. if (isert_conn->conn_qp) {
  510. cq_index = ((struct isert_cq_desc *)
  511. isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
  512. pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
  513. isert_conn->conn_device->cq_active_qps[cq_index]--;
  514. rdma_destroy_qp(isert_conn->conn_cm_id);
  515. }
  516. isert_free_rx_descriptors(isert_conn);
  517. rdma_destroy_id(isert_conn->conn_cm_id);
  518. if (isert_conn->login_buf) {
  519. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  520. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  521. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  522. ISCSI_DEF_MAX_RECV_SEG_LEN,
  523. DMA_FROM_DEVICE);
  524. kfree(isert_conn->login_buf);
  525. }
  526. kfree(isert_conn);
  527. if (device)
  528. isert_device_try_release(device);
  529. pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
  530. }
  531. static void
  532. isert_connected_handler(struct rdma_cm_id *cma_id)
  533. {
  534. return;
  535. }
  536. static void
  537. isert_release_conn_kref(struct kref *kref)
  538. {
  539. struct isert_conn *isert_conn = container_of(kref,
  540. struct isert_conn, conn_kref);
  541. pr_debug("Calling isert_connect_release for final kref %s/%d\n",
  542. current->comm, current->pid);
  543. isert_connect_release(isert_conn);
  544. }
  545. static void
  546. isert_put_conn(struct isert_conn *isert_conn)
  547. {
  548. kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
  549. }
  550. static void
  551. isert_disconnect_work(struct work_struct *work)
  552. {
  553. struct isert_conn *isert_conn = container_of(work,
  554. struct isert_conn, conn_logout_work);
  555. pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  556. mutex_lock(&isert_conn->conn_mutex);
  557. isert_conn->state = ISER_CONN_DOWN;
  558. if (isert_conn->post_recv_buf_count == 0 &&
  559. atomic_read(&isert_conn->post_send_buf_count) == 0) {
  560. pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
  561. mutex_unlock(&isert_conn->conn_mutex);
  562. goto wake_up;
  563. }
  564. if (!isert_conn->conn_cm_id) {
  565. mutex_unlock(&isert_conn->conn_mutex);
  566. isert_put_conn(isert_conn);
  567. return;
  568. }
  569. if (!isert_conn->logout_posted) {
  570. pr_debug("Calling rdma_disconnect for !logout_posted from"
  571. " isert_disconnect_work\n");
  572. rdma_disconnect(isert_conn->conn_cm_id);
  573. mutex_unlock(&isert_conn->conn_mutex);
  574. iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
  575. goto wake_up;
  576. }
  577. mutex_unlock(&isert_conn->conn_mutex);
  578. wake_up:
  579. wake_up(&isert_conn->conn_wait);
  580. isert_put_conn(isert_conn);
  581. }
  582. static void
  583. isert_disconnected_handler(struct rdma_cm_id *cma_id)
  584. {
  585. struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
  586. INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
  587. schedule_work(&isert_conn->conn_logout_work);
  588. }
  589. static int
  590. isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  591. {
  592. int ret = 0;
  593. pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
  594. event->event, event->status, cma_id->context, cma_id);
  595. switch (event->event) {
  596. case RDMA_CM_EVENT_CONNECT_REQUEST:
  597. pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
  598. ret = isert_connect_request(cma_id, event);
  599. break;
  600. case RDMA_CM_EVENT_ESTABLISHED:
  601. pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
  602. isert_connected_handler(cma_id);
  603. break;
  604. case RDMA_CM_EVENT_DISCONNECTED:
  605. pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
  606. isert_disconnected_handler(cma_id);
  607. break;
  608. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  609. case RDMA_CM_EVENT_ADDR_CHANGE:
  610. break;
  611. case RDMA_CM_EVENT_CONNECT_ERROR:
  612. default:
  613. pr_err("Unknown RDMA CMA event: %d\n", event->event);
  614. break;
  615. }
  616. if (ret != 0) {
  617. pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
  618. event->event, ret);
  619. dump_stack();
  620. }
  621. return ret;
  622. }
  623. static int
  624. isert_post_recv(struct isert_conn *isert_conn, u32 count)
  625. {
  626. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  627. int i, ret;
  628. unsigned int rx_head = isert_conn->conn_rx_desc_head;
  629. struct iser_rx_desc *rx_desc;
  630. for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
  631. rx_desc = &isert_conn->conn_rx_descs[rx_head];
  632. rx_wr->wr_id = (unsigned long)rx_desc;
  633. rx_wr->sg_list = &rx_desc->rx_sg;
  634. rx_wr->num_sge = 1;
  635. rx_wr->next = rx_wr + 1;
  636. rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
  637. }
  638. rx_wr--;
  639. rx_wr->next = NULL; /* mark end of work requests list */
  640. isert_conn->post_recv_buf_count += count;
  641. ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
  642. &rx_wr_failed);
  643. if (ret) {
  644. pr_err("ib_post_recv() failed with ret: %d\n", ret);
  645. isert_conn->post_recv_buf_count -= count;
  646. } else {
  647. pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
  648. isert_conn->conn_rx_desc_head = rx_head;
  649. }
  650. return ret;
  651. }
  652. static int
  653. isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
  654. {
  655. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  656. struct ib_send_wr send_wr, *send_wr_failed;
  657. int ret;
  658. ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
  659. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  660. send_wr.next = NULL;
  661. send_wr.wr_id = (unsigned long)tx_desc;
  662. send_wr.sg_list = tx_desc->tx_sg;
  663. send_wr.num_sge = tx_desc->num_sge;
  664. send_wr.opcode = IB_WR_SEND;
  665. send_wr.send_flags = IB_SEND_SIGNALED;
  666. atomic_inc(&isert_conn->post_send_buf_count);
  667. ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
  668. if (ret) {
  669. pr_err("ib_post_send() failed, ret: %d\n", ret);
  670. atomic_dec(&isert_conn->post_send_buf_count);
  671. }
  672. return ret;
  673. }
  674. static void
  675. isert_create_send_desc(struct isert_conn *isert_conn,
  676. struct isert_cmd *isert_cmd,
  677. struct iser_tx_desc *tx_desc)
  678. {
  679. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  680. ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
  681. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  682. memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
  683. tx_desc->iser_header.flags = ISER_VER;
  684. tx_desc->num_sge = 1;
  685. tx_desc->isert_cmd = isert_cmd;
  686. if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
  687. tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
  688. pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
  689. }
  690. }
  691. static int
  692. isert_init_tx_hdrs(struct isert_conn *isert_conn,
  693. struct iser_tx_desc *tx_desc)
  694. {
  695. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  696. u64 dma_addr;
  697. dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
  698. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  699. if (ib_dma_mapping_error(ib_dev, dma_addr)) {
  700. pr_err("ib_dma_mapping_error() failed\n");
  701. return -ENOMEM;
  702. }
  703. tx_desc->dma_addr = dma_addr;
  704. tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
  705. tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
  706. tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
  707. pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
  708. " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
  709. tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
  710. return 0;
  711. }
  712. static void
  713. isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  714. struct ib_send_wr *send_wr, bool coalesce)
  715. {
  716. struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
  717. isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
  718. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  719. send_wr->opcode = IB_WR_SEND;
  720. send_wr->sg_list = &tx_desc->tx_sg[0];
  721. send_wr->num_sge = isert_cmd->tx_desc.num_sge;
  722. /*
  723. * Coalesce send completion interrupts by only setting IB_SEND_SIGNALED
  724. * bit for every ISERT_COMP_BATCH_COUNT number of ib_post_send() calls.
  725. */
  726. mutex_lock(&isert_conn->conn_comp_mutex);
  727. if (coalesce &&
  728. ++isert_conn->conn_comp_batch < ISERT_COMP_BATCH_COUNT) {
  729. llist_add(&tx_desc->comp_llnode, &isert_conn->conn_comp_llist);
  730. mutex_unlock(&isert_conn->conn_comp_mutex);
  731. return;
  732. }
  733. isert_conn->conn_comp_batch = 0;
  734. tx_desc->comp_llnode_batch = llist_del_all(&isert_conn->conn_comp_llist);
  735. mutex_unlock(&isert_conn->conn_comp_mutex);
  736. send_wr->send_flags = IB_SEND_SIGNALED;
  737. }
  738. static int
  739. isert_rdma_post_recvl(struct isert_conn *isert_conn)
  740. {
  741. struct ib_recv_wr rx_wr, *rx_wr_fail;
  742. struct ib_sge sge;
  743. int ret;
  744. memset(&sge, 0, sizeof(struct ib_sge));
  745. sge.addr = isert_conn->login_req_dma;
  746. sge.length = ISER_RX_LOGIN_SIZE;
  747. sge.lkey = isert_conn->conn_mr->lkey;
  748. pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
  749. sge.addr, sge.length, sge.lkey);
  750. memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
  751. rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
  752. rx_wr.sg_list = &sge;
  753. rx_wr.num_sge = 1;
  754. isert_conn->post_recv_buf_count++;
  755. ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
  756. if (ret) {
  757. pr_err("ib_post_recv() failed: %d\n", ret);
  758. isert_conn->post_recv_buf_count--;
  759. }
  760. pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
  761. return ret;
  762. }
  763. static int
  764. isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
  765. u32 length)
  766. {
  767. struct isert_conn *isert_conn = conn->context;
  768. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  769. struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
  770. int ret;
  771. isert_create_send_desc(isert_conn, NULL, tx_desc);
  772. memcpy(&tx_desc->iscsi_header, &login->rsp[0],
  773. sizeof(struct iscsi_hdr));
  774. isert_init_tx_hdrs(isert_conn, tx_desc);
  775. if (length > 0) {
  776. struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
  777. ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
  778. length, DMA_TO_DEVICE);
  779. memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
  780. ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
  781. length, DMA_TO_DEVICE);
  782. tx_dsg->addr = isert_conn->login_rsp_dma;
  783. tx_dsg->length = length;
  784. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  785. tx_desc->num_sge = 2;
  786. }
  787. if (!login->login_failed) {
  788. if (login->login_complete) {
  789. ret = isert_alloc_rx_descriptors(isert_conn);
  790. if (ret)
  791. return ret;
  792. ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
  793. if (ret)
  794. return ret;
  795. isert_conn->state = ISER_CONN_UP;
  796. goto post_send;
  797. }
  798. ret = isert_rdma_post_recvl(isert_conn);
  799. if (ret)
  800. return ret;
  801. }
  802. post_send:
  803. ret = isert_post_send(isert_conn, tx_desc);
  804. if (ret)
  805. return ret;
  806. return 0;
  807. }
  808. static void
  809. isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
  810. struct isert_conn *isert_conn)
  811. {
  812. struct iscsi_conn *conn = isert_conn->conn;
  813. struct iscsi_login *login = conn->conn_login;
  814. int size;
  815. if (!login) {
  816. pr_err("conn->conn_login is NULL\n");
  817. dump_stack();
  818. return;
  819. }
  820. if (login->first_request) {
  821. struct iscsi_login_req *login_req =
  822. (struct iscsi_login_req *)&rx_desc->iscsi_header;
  823. /*
  824. * Setup the initial iscsi_login values from the leading
  825. * login request PDU.
  826. */
  827. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  828. login->current_stage =
  829. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
  830. >> 2;
  831. login->version_min = login_req->min_version;
  832. login->version_max = login_req->max_version;
  833. memcpy(login->isid, login_req->isid, 6);
  834. login->cmd_sn = be32_to_cpu(login_req->cmdsn);
  835. login->init_task_tag = login_req->itt;
  836. login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
  837. login->cid = be16_to_cpu(login_req->cid);
  838. login->tsih = be16_to_cpu(login_req->tsih);
  839. }
  840. memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
  841. size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
  842. pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
  843. size, rx_buflen, MAX_KEY_VALUE_PAIRS);
  844. memcpy(login->req_buf, &rx_desc->data[0], size);
  845. if (login->first_request) {
  846. complete(&isert_conn->conn_login_comp);
  847. return;
  848. }
  849. schedule_delayed_work(&conn->login_work, 0);
  850. }
  851. static struct iscsi_cmd
  852. *isert_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp)
  853. {
  854. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  855. struct isert_cmd *isert_cmd;
  856. struct iscsi_cmd *cmd;
  857. cmd = iscsit_allocate_cmd(conn, gfp);
  858. if (!cmd) {
  859. pr_err("Unable to allocate iscsi_cmd + isert_cmd\n");
  860. return NULL;
  861. }
  862. isert_cmd = iscsit_priv_cmd(cmd);
  863. isert_cmd->conn = isert_conn;
  864. isert_cmd->iscsi_cmd = cmd;
  865. return cmd;
  866. }
  867. static int
  868. isert_handle_scsi_cmd(struct isert_conn *isert_conn,
  869. struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
  870. struct iser_rx_desc *rx_desc, unsigned char *buf)
  871. {
  872. struct iscsi_conn *conn = isert_conn->conn;
  873. struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
  874. struct scatterlist *sg;
  875. int imm_data, imm_data_len, unsol_data, sg_nents, rc;
  876. bool dump_payload = false;
  877. rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
  878. if (rc < 0)
  879. return rc;
  880. imm_data = cmd->immediate_data;
  881. imm_data_len = cmd->first_burst_len;
  882. unsol_data = cmd->unsolicited_data;
  883. rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
  884. if (rc < 0) {
  885. return 0;
  886. } else if (rc > 0) {
  887. dump_payload = true;
  888. goto sequence_cmd;
  889. }
  890. if (!imm_data)
  891. return 0;
  892. sg = &cmd->se_cmd.t_data_sg[0];
  893. sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
  894. pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
  895. sg, sg_nents, &rx_desc->data[0], imm_data_len);
  896. sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
  897. cmd->write_data_done += imm_data_len;
  898. if (cmd->write_data_done == cmd->se_cmd.data_length) {
  899. spin_lock_bh(&cmd->istate_lock);
  900. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  901. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  902. spin_unlock_bh(&cmd->istate_lock);
  903. }
  904. sequence_cmd:
  905. rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
  906. if (!rc && dump_payload == false && unsol_data)
  907. iscsit_set_unsoliticed_dataout(cmd);
  908. return 0;
  909. }
  910. static int
  911. isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
  912. struct iser_rx_desc *rx_desc, unsigned char *buf)
  913. {
  914. struct scatterlist *sg_start;
  915. struct iscsi_conn *conn = isert_conn->conn;
  916. struct iscsi_cmd *cmd = NULL;
  917. struct iscsi_data *hdr = (struct iscsi_data *)buf;
  918. u32 unsol_data_len = ntoh24(hdr->dlength);
  919. int rc, sg_nents, sg_off, page_off;
  920. rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
  921. if (rc < 0)
  922. return rc;
  923. else if (!cmd)
  924. return 0;
  925. /*
  926. * FIXME: Unexpected unsolicited_data out
  927. */
  928. if (!cmd->unsolicited_data) {
  929. pr_err("Received unexpected solicited data payload\n");
  930. dump_stack();
  931. return -1;
  932. }
  933. pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
  934. unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
  935. sg_off = cmd->write_data_done / PAGE_SIZE;
  936. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  937. sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
  938. page_off = cmd->write_data_done % PAGE_SIZE;
  939. /*
  940. * FIXME: Non page-aligned unsolicited_data out
  941. */
  942. if (page_off) {
  943. pr_err("Received unexpected non-page aligned data payload\n");
  944. dump_stack();
  945. return -1;
  946. }
  947. pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
  948. sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
  949. sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
  950. unsol_data_len);
  951. rc = iscsit_check_dataout_payload(cmd, hdr, false);
  952. if (rc < 0)
  953. return rc;
  954. return 0;
  955. }
  956. static int
  957. isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  958. struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
  959. unsigned char *buf)
  960. {
  961. struct iscsi_conn *conn = isert_conn->conn;
  962. struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
  963. int rc;
  964. rc = iscsit_setup_nop_out(conn, cmd, hdr);
  965. if (rc < 0)
  966. return rc;
  967. /*
  968. * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
  969. */
  970. return iscsit_process_nop_out(conn, cmd, hdr);
  971. }
  972. static int
  973. isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  974. struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
  975. struct iscsi_text *hdr)
  976. {
  977. struct iscsi_conn *conn = isert_conn->conn;
  978. u32 payload_length = ntoh24(hdr->dlength);
  979. int rc;
  980. unsigned char *text_in;
  981. rc = iscsit_setup_text_cmd(conn, cmd, hdr);
  982. if (rc < 0)
  983. return rc;
  984. text_in = kzalloc(payload_length, GFP_KERNEL);
  985. if (!text_in) {
  986. pr_err("Unable to allocate text_in of payload_length: %u\n",
  987. payload_length);
  988. return -ENOMEM;
  989. }
  990. cmd->text_in_ptr = text_in;
  991. memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
  992. return iscsit_process_text_cmd(conn, cmd, hdr);
  993. }
  994. static int
  995. isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
  996. uint32_t read_stag, uint64_t read_va,
  997. uint32_t write_stag, uint64_t write_va)
  998. {
  999. struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
  1000. struct iscsi_conn *conn = isert_conn->conn;
  1001. struct iscsi_session *sess = conn->sess;
  1002. struct iscsi_cmd *cmd;
  1003. struct isert_cmd *isert_cmd;
  1004. int ret = -EINVAL;
  1005. u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
  1006. if (sess->sess_ops->SessionType &&
  1007. (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
  1008. pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
  1009. " ignoring\n", opcode);
  1010. return 0;
  1011. }
  1012. switch (opcode) {
  1013. case ISCSI_OP_SCSI_CMD:
  1014. cmd = isert_allocate_cmd(conn, GFP_KERNEL);
  1015. if (!cmd)
  1016. break;
  1017. isert_cmd = iscsit_priv_cmd(cmd);
  1018. isert_cmd->read_stag = read_stag;
  1019. isert_cmd->read_va = read_va;
  1020. isert_cmd->write_stag = write_stag;
  1021. isert_cmd->write_va = write_va;
  1022. ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
  1023. rx_desc, (unsigned char *)hdr);
  1024. break;
  1025. case ISCSI_OP_NOOP_OUT:
  1026. cmd = isert_allocate_cmd(conn, GFP_KERNEL);
  1027. if (!cmd)
  1028. break;
  1029. isert_cmd = iscsit_priv_cmd(cmd);
  1030. ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
  1031. rx_desc, (unsigned char *)hdr);
  1032. break;
  1033. case ISCSI_OP_SCSI_DATA_OUT:
  1034. ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
  1035. (unsigned char *)hdr);
  1036. break;
  1037. case ISCSI_OP_SCSI_TMFUNC:
  1038. cmd = isert_allocate_cmd(conn, GFP_KERNEL);
  1039. if (!cmd)
  1040. break;
  1041. ret = iscsit_handle_task_mgt_cmd(conn, cmd,
  1042. (unsigned char *)hdr);
  1043. break;
  1044. case ISCSI_OP_LOGOUT:
  1045. cmd = isert_allocate_cmd(conn, GFP_KERNEL);
  1046. if (!cmd)
  1047. break;
  1048. ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
  1049. if (ret > 0)
  1050. wait_for_completion_timeout(&conn->conn_logout_comp,
  1051. SECONDS_FOR_LOGOUT_COMP *
  1052. HZ);
  1053. break;
  1054. case ISCSI_OP_TEXT:
  1055. cmd = isert_allocate_cmd(conn, GFP_KERNEL);
  1056. if (!cmd)
  1057. break;
  1058. isert_cmd = iscsit_priv_cmd(cmd);
  1059. ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
  1060. rx_desc, (struct iscsi_text *)hdr);
  1061. break;
  1062. default:
  1063. pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
  1064. dump_stack();
  1065. break;
  1066. }
  1067. return ret;
  1068. }
  1069. static void
  1070. isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
  1071. {
  1072. struct iser_hdr *iser_hdr = &rx_desc->iser_header;
  1073. uint64_t read_va = 0, write_va = 0;
  1074. uint32_t read_stag = 0, write_stag = 0;
  1075. int rc;
  1076. switch (iser_hdr->flags & 0xF0) {
  1077. case ISCSI_CTRL:
  1078. if (iser_hdr->flags & ISER_RSV) {
  1079. read_stag = be32_to_cpu(iser_hdr->read_stag);
  1080. read_va = be64_to_cpu(iser_hdr->read_va);
  1081. pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
  1082. read_stag, (unsigned long long)read_va);
  1083. }
  1084. if (iser_hdr->flags & ISER_WSV) {
  1085. write_stag = be32_to_cpu(iser_hdr->write_stag);
  1086. write_va = be64_to_cpu(iser_hdr->write_va);
  1087. pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
  1088. write_stag, (unsigned long long)write_va);
  1089. }
  1090. pr_debug("ISER ISCSI_CTRL PDU\n");
  1091. break;
  1092. case ISER_HELLO:
  1093. pr_err("iSER Hello message\n");
  1094. break;
  1095. default:
  1096. pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
  1097. break;
  1098. }
  1099. rc = isert_rx_opcode(isert_conn, rx_desc,
  1100. read_stag, read_va, write_stag, write_va);
  1101. }
  1102. static void
  1103. isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
  1104. unsigned long xfer_len)
  1105. {
  1106. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1107. struct iscsi_hdr *hdr;
  1108. u64 rx_dma;
  1109. int rx_buflen, outstanding;
  1110. if ((char *)desc == isert_conn->login_req_buf) {
  1111. rx_dma = isert_conn->login_req_dma;
  1112. rx_buflen = ISER_RX_LOGIN_SIZE;
  1113. pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  1114. rx_dma, rx_buflen);
  1115. } else {
  1116. rx_dma = desc->dma_addr;
  1117. rx_buflen = ISER_RX_PAYLOAD_SIZE;
  1118. pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  1119. rx_dma, rx_buflen);
  1120. }
  1121. ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
  1122. hdr = &desc->iscsi_header;
  1123. pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
  1124. hdr->opcode, hdr->itt, hdr->flags,
  1125. (int)(xfer_len - ISER_HEADERS_LEN));
  1126. if ((char *)desc == isert_conn->login_req_buf)
  1127. isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
  1128. isert_conn);
  1129. else
  1130. isert_rx_do_work(desc, isert_conn);
  1131. ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
  1132. DMA_FROM_DEVICE);
  1133. isert_conn->post_recv_buf_count--;
  1134. pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
  1135. isert_conn->post_recv_buf_count);
  1136. if ((char *)desc == isert_conn->login_req_buf)
  1137. return;
  1138. outstanding = isert_conn->post_recv_buf_count;
  1139. if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
  1140. int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
  1141. ISERT_MIN_POSTED_RX);
  1142. err = isert_post_recv(isert_conn, count);
  1143. if (err) {
  1144. pr_err("isert_post_recv() count: %d failed, %d\n",
  1145. count, err);
  1146. }
  1147. }
  1148. }
  1149. static void
  1150. isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
  1151. {
  1152. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1153. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1154. pr_debug("isert_unmap_cmd: %p\n", isert_cmd);
  1155. if (wr->sge) {
  1156. pr_debug("isert_unmap_cmd: %p unmap_sg op\n", isert_cmd);
  1157. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
  1158. (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
  1159. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1160. wr->sge = NULL;
  1161. }
  1162. if (wr->send_wr) {
  1163. pr_debug("isert_unmap_cmd: %p free send_wr\n", isert_cmd);
  1164. kfree(wr->send_wr);
  1165. wr->send_wr = NULL;
  1166. }
  1167. if (wr->ib_sge) {
  1168. pr_debug("isert_unmap_cmd: %p free ib_sge\n", isert_cmd);
  1169. kfree(wr->ib_sge);
  1170. wr->ib_sge = NULL;
  1171. }
  1172. }
  1173. static void
  1174. isert_unreg_rdma_frwr(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
  1175. {
  1176. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1177. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1178. LIST_HEAD(unmap_list);
  1179. pr_debug("unreg_frwr_cmd: %p\n", isert_cmd);
  1180. if (wr->fr_desc) {
  1181. pr_debug("unreg_frwr_cmd: %p free fr_desc %p\n",
  1182. isert_cmd, wr->fr_desc);
  1183. spin_lock_bh(&isert_conn->conn_lock);
  1184. list_add_tail(&wr->fr_desc->list, &isert_conn->conn_frwr_pool);
  1185. spin_unlock_bh(&isert_conn->conn_lock);
  1186. wr->fr_desc = NULL;
  1187. }
  1188. if (wr->sge) {
  1189. pr_debug("unreg_frwr_cmd: %p unmap_sg op\n", isert_cmd);
  1190. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge,
  1191. (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
  1192. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1193. wr->sge = NULL;
  1194. }
  1195. wr->ib_sge = NULL;
  1196. wr->send_wr = NULL;
  1197. }
  1198. static void
  1199. isert_put_cmd(struct isert_cmd *isert_cmd)
  1200. {
  1201. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1202. struct isert_conn *isert_conn = isert_cmd->conn;
  1203. struct iscsi_conn *conn = isert_conn->conn;
  1204. struct isert_device *device = isert_conn->conn_device;
  1205. pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
  1206. switch (cmd->iscsi_opcode) {
  1207. case ISCSI_OP_SCSI_CMD:
  1208. spin_lock_bh(&conn->cmd_lock);
  1209. if (!list_empty(&cmd->i_conn_node))
  1210. list_del(&cmd->i_conn_node);
  1211. spin_unlock_bh(&conn->cmd_lock);
  1212. if (cmd->data_direction == DMA_TO_DEVICE)
  1213. iscsit_stop_dataout_timer(cmd);
  1214. device->unreg_rdma_mem(isert_cmd, isert_conn);
  1215. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1216. break;
  1217. case ISCSI_OP_SCSI_TMFUNC:
  1218. spin_lock_bh(&conn->cmd_lock);
  1219. if (!list_empty(&cmd->i_conn_node))
  1220. list_del(&cmd->i_conn_node);
  1221. spin_unlock_bh(&conn->cmd_lock);
  1222. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1223. break;
  1224. case ISCSI_OP_REJECT:
  1225. case ISCSI_OP_NOOP_OUT:
  1226. case ISCSI_OP_TEXT:
  1227. spin_lock_bh(&conn->cmd_lock);
  1228. if (!list_empty(&cmd->i_conn_node))
  1229. list_del(&cmd->i_conn_node);
  1230. spin_unlock_bh(&conn->cmd_lock);
  1231. /*
  1232. * Handle special case for REJECT when iscsi_add_reject*() has
  1233. * overwritten the original iscsi_opcode assignment, and the
  1234. * associated cmd->se_cmd needs to be released.
  1235. */
  1236. if (cmd->se_cmd.se_tfo != NULL) {
  1237. pr_debug("Calling transport_generic_free_cmd from"
  1238. " isert_put_cmd for 0x%02x\n",
  1239. cmd->iscsi_opcode);
  1240. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1241. break;
  1242. }
  1243. /*
  1244. * Fall-through
  1245. */
  1246. default:
  1247. iscsit_release_cmd(cmd);
  1248. break;
  1249. }
  1250. }
  1251. static void
  1252. isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
  1253. {
  1254. if (tx_desc->dma_addr != 0) {
  1255. pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
  1256. ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
  1257. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  1258. tx_desc->dma_addr = 0;
  1259. }
  1260. }
  1261. static void
  1262. isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
  1263. struct ib_device *ib_dev)
  1264. {
  1265. if (isert_cmd->pdu_buf_dma != 0) {
  1266. pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
  1267. ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
  1268. isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
  1269. isert_cmd->pdu_buf_dma = 0;
  1270. }
  1271. isert_unmap_tx_desc(tx_desc, ib_dev);
  1272. isert_put_cmd(isert_cmd);
  1273. }
  1274. static void
  1275. isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
  1276. struct isert_cmd *isert_cmd)
  1277. {
  1278. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1279. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1280. struct se_cmd *se_cmd = &cmd->se_cmd;
  1281. struct isert_conn *isert_conn = isert_cmd->conn;
  1282. struct isert_device *device = isert_conn->conn_device;
  1283. iscsit_stop_dataout_timer(cmd);
  1284. device->unreg_rdma_mem(isert_cmd, isert_conn);
  1285. cmd->write_data_done = wr->cur_rdma_length;
  1286. pr_debug("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
  1287. spin_lock_bh(&cmd->istate_lock);
  1288. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  1289. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  1290. spin_unlock_bh(&cmd->istate_lock);
  1291. target_execute_cmd(se_cmd);
  1292. }
  1293. static void
  1294. isert_do_control_comp(struct work_struct *work)
  1295. {
  1296. struct isert_cmd *isert_cmd = container_of(work,
  1297. struct isert_cmd, comp_work);
  1298. struct isert_conn *isert_conn = isert_cmd->conn;
  1299. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1300. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1301. switch (cmd->i_state) {
  1302. case ISTATE_SEND_TASKMGTRSP:
  1303. pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
  1304. atomic_dec(&isert_conn->post_send_buf_count);
  1305. iscsit_tmr_post_handler(cmd, cmd->conn);
  1306. cmd->i_state = ISTATE_SENT_STATUS;
  1307. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1308. break;
  1309. case ISTATE_SEND_REJECT:
  1310. pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
  1311. atomic_dec(&isert_conn->post_send_buf_count);
  1312. cmd->i_state = ISTATE_SENT_STATUS;
  1313. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1314. break;
  1315. case ISTATE_SEND_LOGOUTRSP:
  1316. pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
  1317. /*
  1318. * Call atomic_dec(&isert_conn->post_send_buf_count)
  1319. * from isert_free_conn()
  1320. */
  1321. isert_conn->logout_posted = true;
  1322. iscsit_logout_post_handler(cmd, cmd->conn);
  1323. break;
  1324. case ISTATE_SEND_TEXTRSP:
  1325. atomic_dec(&isert_conn->post_send_buf_count);
  1326. cmd->i_state = ISTATE_SENT_STATUS;
  1327. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1328. break;
  1329. default:
  1330. pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
  1331. dump_stack();
  1332. break;
  1333. }
  1334. }
  1335. static void
  1336. isert_response_completion(struct iser_tx_desc *tx_desc,
  1337. struct isert_cmd *isert_cmd,
  1338. struct isert_conn *isert_conn,
  1339. struct ib_device *ib_dev)
  1340. {
  1341. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1342. if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
  1343. cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
  1344. cmd->i_state == ISTATE_SEND_REJECT ||
  1345. cmd->i_state == ISTATE_SEND_TEXTRSP) {
  1346. isert_unmap_tx_desc(tx_desc, ib_dev);
  1347. INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
  1348. queue_work(isert_comp_wq, &isert_cmd->comp_work);
  1349. return;
  1350. }
  1351. atomic_dec(&isert_conn->post_send_buf_count);
  1352. cmd->i_state = ISTATE_SENT_STATUS;
  1353. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1354. }
  1355. static void
  1356. __isert_send_completion(struct iser_tx_desc *tx_desc,
  1357. struct isert_conn *isert_conn)
  1358. {
  1359. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1360. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1361. struct isert_rdma_wr *wr;
  1362. if (!isert_cmd) {
  1363. atomic_dec(&isert_conn->post_send_buf_count);
  1364. isert_unmap_tx_desc(tx_desc, ib_dev);
  1365. return;
  1366. }
  1367. wr = &isert_cmd->rdma_wr;
  1368. switch (wr->iser_ib_op) {
  1369. case ISER_IB_RECV:
  1370. pr_err("isert_send_completion: Got ISER_IB_RECV\n");
  1371. dump_stack();
  1372. break;
  1373. case ISER_IB_SEND:
  1374. pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
  1375. isert_response_completion(tx_desc, isert_cmd,
  1376. isert_conn, ib_dev);
  1377. break;
  1378. case ISER_IB_RDMA_WRITE:
  1379. pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
  1380. dump_stack();
  1381. break;
  1382. case ISER_IB_RDMA_READ:
  1383. pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
  1384. atomic_dec(&isert_conn->post_send_buf_count);
  1385. isert_completion_rdma_read(tx_desc, isert_cmd);
  1386. break;
  1387. default:
  1388. pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
  1389. dump_stack();
  1390. break;
  1391. }
  1392. }
  1393. static void
  1394. isert_send_completion(struct iser_tx_desc *tx_desc,
  1395. struct isert_conn *isert_conn)
  1396. {
  1397. struct llist_node *llnode = tx_desc->comp_llnode_batch;
  1398. struct iser_tx_desc *t;
  1399. /*
  1400. * Drain coalesced completion llist starting from comp_llnode_batch
  1401. * setup in isert_init_send_wr(), and then complete trailing tx_desc.
  1402. */
  1403. while (llnode) {
  1404. t = llist_entry(llnode, struct iser_tx_desc, comp_llnode);
  1405. llnode = llist_next(llnode);
  1406. __isert_send_completion(t, isert_conn);
  1407. }
  1408. __isert_send_completion(tx_desc, isert_conn);
  1409. }
  1410. static void
  1411. isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
  1412. {
  1413. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1414. if (tx_desc) {
  1415. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1416. if (!isert_cmd)
  1417. isert_unmap_tx_desc(tx_desc, ib_dev);
  1418. else
  1419. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1420. }
  1421. if (isert_conn->post_recv_buf_count == 0 &&
  1422. atomic_read(&isert_conn->post_send_buf_count) == 0) {
  1423. pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  1424. pr_debug("Calling wake_up from isert_cq_comp_err\n");
  1425. mutex_lock(&isert_conn->conn_mutex);
  1426. if (isert_conn->state != ISER_CONN_DOWN)
  1427. isert_conn->state = ISER_CONN_TERMINATING;
  1428. mutex_unlock(&isert_conn->conn_mutex);
  1429. wake_up(&isert_conn->conn_wait_comp_err);
  1430. }
  1431. }
  1432. static void
  1433. isert_cq_tx_work(struct work_struct *work)
  1434. {
  1435. struct isert_cq_desc *cq_desc = container_of(work,
  1436. struct isert_cq_desc, cq_tx_work);
  1437. struct isert_device *device = cq_desc->device;
  1438. int cq_index = cq_desc->cq_index;
  1439. struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
  1440. struct isert_conn *isert_conn;
  1441. struct iser_tx_desc *tx_desc;
  1442. struct ib_wc wc;
  1443. while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
  1444. tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
  1445. isert_conn = wc.qp->qp_context;
  1446. if (wc.status == IB_WC_SUCCESS) {
  1447. isert_send_completion(tx_desc, isert_conn);
  1448. } else {
  1449. pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1450. pr_debug("TX wc.status: 0x%08x\n", wc.status);
  1451. pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
  1452. atomic_dec(&isert_conn->post_send_buf_count);
  1453. isert_cq_comp_err(tx_desc, isert_conn);
  1454. }
  1455. }
  1456. ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
  1457. }
  1458. static void
  1459. isert_cq_tx_callback(struct ib_cq *cq, void *context)
  1460. {
  1461. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1462. INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
  1463. queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
  1464. }
  1465. static void
  1466. isert_cq_rx_work(struct work_struct *work)
  1467. {
  1468. struct isert_cq_desc *cq_desc = container_of(work,
  1469. struct isert_cq_desc, cq_rx_work);
  1470. struct isert_device *device = cq_desc->device;
  1471. int cq_index = cq_desc->cq_index;
  1472. struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
  1473. struct isert_conn *isert_conn;
  1474. struct iser_rx_desc *rx_desc;
  1475. struct ib_wc wc;
  1476. unsigned long xfer_len;
  1477. while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
  1478. rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
  1479. isert_conn = wc.qp->qp_context;
  1480. if (wc.status == IB_WC_SUCCESS) {
  1481. xfer_len = (unsigned long)wc.byte_len;
  1482. isert_rx_completion(rx_desc, isert_conn, xfer_len);
  1483. } else {
  1484. pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1485. if (wc.status != IB_WC_WR_FLUSH_ERR) {
  1486. pr_debug("RX wc.status: 0x%08x\n", wc.status);
  1487. pr_debug("RX wc.vendor_err: 0x%08x\n",
  1488. wc.vendor_err);
  1489. }
  1490. isert_conn->post_recv_buf_count--;
  1491. isert_cq_comp_err(NULL, isert_conn);
  1492. }
  1493. }
  1494. ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
  1495. }
  1496. static void
  1497. isert_cq_rx_callback(struct ib_cq *cq, void *context)
  1498. {
  1499. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1500. INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
  1501. queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
  1502. }
  1503. static int
  1504. isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
  1505. {
  1506. struct ib_send_wr *wr_failed;
  1507. int ret;
  1508. atomic_inc(&isert_conn->post_send_buf_count);
  1509. ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
  1510. &wr_failed);
  1511. if (ret) {
  1512. pr_err("ib_post_send failed with %d\n", ret);
  1513. atomic_dec(&isert_conn->post_send_buf_count);
  1514. return ret;
  1515. }
  1516. return ret;
  1517. }
  1518. static int
  1519. isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1520. {
  1521. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1522. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1523. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1524. struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
  1525. &isert_cmd->tx_desc.iscsi_header;
  1526. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1527. iscsit_build_rsp_pdu(cmd, conn, true, hdr);
  1528. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1529. /*
  1530. * Attach SENSE DATA payload to iSCSI Response PDU
  1531. */
  1532. if (cmd->se_cmd.sense_buffer &&
  1533. ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  1534. (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  1535. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1536. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1537. u32 padding, pdu_len;
  1538. put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
  1539. cmd->sense_buffer);
  1540. cmd->se_cmd.scsi_sense_length += sizeof(__be16);
  1541. padding = -(cmd->se_cmd.scsi_sense_length) & 3;
  1542. hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
  1543. pdu_len = cmd->se_cmd.scsi_sense_length + padding;
  1544. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1545. (void *)cmd->sense_buffer, pdu_len,
  1546. DMA_TO_DEVICE);
  1547. isert_cmd->pdu_buf_len = pdu_len;
  1548. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1549. tx_dsg->length = pdu_len;
  1550. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1551. isert_cmd->tx_desc.num_sge = 2;
  1552. }
  1553. isert_init_send_wr(isert_conn, isert_cmd, send_wr, true);
  1554. pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1555. return isert_post_response(isert_conn, isert_cmd);
  1556. }
  1557. static int
  1558. isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
  1559. bool nopout_response)
  1560. {
  1561. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1562. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1563. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1564. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1565. iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
  1566. &isert_cmd->tx_desc.iscsi_header,
  1567. nopout_response);
  1568. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1569. isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
  1570. pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1571. return isert_post_response(isert_conn, isert_cmd);
  1572. }
  1573. static int
  1574. isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1575. {
  1576. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1577. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1578. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1579. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1580. iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
  1581. &isert_cmd->tx_desc.iscsi_header);
  1582. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1583. isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
  1584. pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1585. return isert_post_response(isert_conn, isert_cmd);
  1586. }
  1587. static int
  1588. isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1589. {
  1590. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1591. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1592. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1593. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1594. iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
  1595. &isert_cmd->tx_desc.iscsi_header);
  1596. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1597. isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
  1598. pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1599. return isert_post_response(isert_conn, isert_cmd);
  1600. }
  1601. static int
  1602. isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1603. {
  1604. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1605. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1606. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1607. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1608. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1609. struct iscsi_reject *hdr =
  1610. (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
  1611. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1612. iscsit_build_reject(cmd, conn, hdr);
  1613. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1614. hton24(hdr->dlength, ISCSI_HDR_LEN);
  1615. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1616. (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
  1617. DMA_TO_DEVICE);
  1618. isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
  1619. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1620. tx_dsg->length = ISCSI_HDR_LEN;
  1621. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1622. isert_cmd->tx_desc.num_sge = 2;
  1623. isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
  1624. pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1625. return isert_post_response(isert_conn, isert_cmd);
  1626. }
  1627. static int
  1628. isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1629. {
  1630. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1631. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1632. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1633. struct iscsi_text_rsp *hdr =
  1634. (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
  1635. u32 txt_rsp_len;
  1636. int rc;
  1637. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1638. rc = iscsit_build_text_rsp(cmd, conn, hdr);
  1639. if (rc < 0)
  1640. return rc;
  1641. txt_rsp_len = rc;
  1642. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1643. if (txt_rsp_len) {
  1644. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1645. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1646. void *txt_rsp_buf = cmd->buf_ptr;
  1647. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1648. txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
  1649. isert_cmd->pdu_buf_len = txt_rsp_len;
  1650. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1651. tx_dsg->length = txt_rsp_len;
  1652. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1653. isert_cmd->tx_desc.num_sge = 2;
  1654. }
  1655. isert_init_send_wr(isert_conn, isert_cmd, send_wr, false);
  1656. pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1657. return isert_post_response(isert_conn, isert_cmd);
  1658. }
  1659. static int
  1660. isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1661. struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
  1662. u32 data_left, u32 offset)
  1663. {
  1664. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1665. struct scatterlist *sg_start, *tmp_sg;
  1666. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1667. u32 sg_off, page_off;
  1668. int i = 0, sg_nents;
  1669. sg_off = offset / PAGE_SIZE;
  1670. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1671. sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
  1672. page_off = offset % PAGE_SIZE;
  1673. send_wr->sg_list = ib_sge;
  1674. send_wr->num_sge = sg_nents;
  1675. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  1676. /*
  1677. * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
  1678. */
  1679. for_each_sg(sg_start, tmp_sg, sg_nents, i) {
  1680. pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
  1681. (unsigned long long)tmp_sg->dma_address,
  1682. tmp_sg->length, page_off);
  1683. ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
  1684. ib_sge->length = min_t(u32, data_left,
  1685. ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
  1686. ib_sge->lkey = isert_conn->conn_mr->lkey;
  1687. pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
  1688. ib_sge->addr, ib_sge->length, ib_sge->lkey);
  1689. page_off = 0;
  1690. data_left -= ib_sge->length;
  1691. ib_sge++;
  1692. pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
  1693. }
  1694. pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
  1695. send_wr->sg_list, send_wr->num_sge);
  1696. return sg_nents;
  1697. }
  1698. static int
  1699. isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  1700. struct isert_rdma_wr *wr)
  1701. {
  1702. struct se_cmd *se_cmd = &cmd->se_cmd;
  1703. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1704. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1705. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1706. struct ib_send_wr *send_wr;
  1707. struct ib_sge *ib_sge;
  1708. struct scatterlist *sg_start;
  1709. u32 sg_off = 0, sg_nents;
  1710. u32 offset = 0, data_len, data_left, rdma_write_max, va_offset = 0;
  1711. int ret = 0, count, i, ib_sge_cnt;
  1712. if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
  1713. data_left = se_cmd->data_length;
  1714. } else {
  1715. sg_off = cmd->write_data_done / PAGE_SIZE;
  1716. data_left = se_cmd->data_length - cmd->write_data_done;
  1717. offset = cmd->write_data_done;
  1718. isert_cmd->tx_desc.isert_cmd = isert_cmd;
  1719. }
  1720. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1721. sg_nents = se_cmd->t_data_nents - sg_off;
  1722. count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
  1723. (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
  1724. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1725. if (unlikely(!count)) {
  1726. pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
  1727. return -EINVAL;
  1728. }
  1729. wr->sge = sg_start;
  1730. wr->num_sge = sg_nents;
  1731. wr->cur_rdma_length = data_left;
  1732. pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
  1733. isert_cmd, count, sg_start, sg_nents, data_left);
  1734. ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
  1735. if (!ib_sge) {
  1736. pr_warn("Unable to allocate ib_sge\n");
  1737. ret = -ENOMEM;
  1738. goto unmap_sg;
  1739. }
  1740. wr->ib_sge = ib_sge;
  1741. wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
  1742. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  1743. GFP_KERNEL);
  1744. if (!wr->send_wr) {
  1745. pr_debug("Unable to allocate wr->send_wr\n");
  1746. ret = -ENOMEM;
  1747. goto unmap_sg;
  1748. }
  1749. wr->isert_cmd = isert_cmd;
  1750. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  1751. for (i = 0; i < wr->send_wr_num; i++) {
  1752. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  1753. data_len = min(data_left, rdma_write_max);
  1754. send_wr->send_flags = 0;
  1755. if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
  1756. send_wr->opcode = IB_WR_RDMA_WRITE;
  1757. send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
  1758. send_wr->wr.rdma.rkey = isert_cmd->read_stag;
  1759. if (i + 1 == wr->send_wr_num)
  1760. send_wr->next = &isert_cmd->tx_desc.send_wr;
  1761. else
  1762. send_wr->next = &wr->send_wr[i + 1];
  1763. } else {
  1764. send_wr->opcode = IB_WR_RDMA_READ;
  1765. send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
  1766. send_wr->wr.rdma.rkey = isert_cmd->write_stag;
  1767. if (i + 1 == wr->send_wr_num)
  1768. send_wr->send_flags = IB_SEND_SIGNALED;
  1769. else
  1770. send_wr->next = &wr->send_wr[i + 1];
  1771. }
  1772. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  1773. send_wr, data_len, offset);
  1774. ib_sge += ib_sge_cnt;
  1775. offset += data_len;
  1776. va_offset += data_len;
  1777. data_left -= data_len;
  1778. }
  1779. return 0;
  1780. unmap_sg:
  1781. ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
  1782. (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
  1783. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1784. return ret;
  1785. }
  1786. static int
  1787. isert_map_fr_pagelist(struct ib_device *ib_dev,
  1788. struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
  1789. {
  1790. u64 start_addr, end_addr, page, chunk_start = 0;
  1791. struct scatterlist *tmp_sg;
  1792. int i = 0, new_chunk, last_ent, n_pages;
  1793. n_pages = 0;
  1794. new_chunk = 1;
  1795. last_ent = sg_nents - 1;
  1796. for_each_sg(sg_start, tmp_sg, sg_nents, i) {
  1797. start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
  1798. if (new_chunk)
  1799. chunk_start = start_addr;
  1800. end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
  1801. pr_debug("SGL[%d] dma_addr: 0x%16llx len: %u\n",
  1802. i, (unsigned long long)tmp_sg->dma_address,
  1803. tmp_sg->length);
  1804. if ((end_addr & ~PAGE_MASK) && i < last_ent) {
  1805. new_chunk = 0;
  1806. continue;
  1807. }
  1808. new_chunk = 1;
  1809. page = chunk_start & PAGE_MASK;
  1810. do {
  1811. fr_pl[n_pages++] = page;
  1812. pr_debug("Mapped page_list[%d] page_addr: 0x%16llx\n",
  1813. n_pages - 1, page);
  1814. page += PAGE_SIZE;
  1815. } while (page < end_addr);
  1816. }
  1817. return n_pages;
  1818. }
  1819. static int
  1820. isert_fast_reg_mr(struct fast_reg_descriptor *fr_desc,
  1821. struct isert_cmd *isert_cmd, struct isert_conn *isert_conn,
  1822. struct ib_sge *ib_sge, u32 offset, unsigned int data_len)
  1823. {
  1824. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1825. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1826. struct scatterlist *sg_start;
  1827. u32 sg_off, page_off;
  1828. struct ib_send_wr fr_wr, inv_wr;
  1829. struct ib_send_wr *bad_wr, *wr = NULL;
  1830. u8 key;
  1831. int ret, sg_nents, pagelist_len;
  1832. sg_off = offset / PAGE_SIZE;
  1833. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1834. sg_nents = min_t(unsigned int, cmd->se_cmd.t_data_nents - sg_off,
  1835. ISCSI_ISER_SG_TABLESIZE);
  1836. page_off = offset % PAGE_SIZE;
  1837. pr_debug("Cmd: %p use fr_desc %p sg_nents %d sg_off %d offset %u\n",
  1838. isert_cmd, fr_desc, sg_nents, sg_off, offset);
  1839. pagelist_len = isert_map_fr_pagelist(ib_dev, sg_start, sg_nents,
  1840. &fr_desc->data_frpl->page_list[0]);
  1841. if (!fr_desc->valid) {
  1842. memset(&inv_wr, 0, sizeof(inv_wr));
  1843. inv_wr.opcode = IB_WR_LOCAL_INV;
  1844. inv_wr.ex.invalidate_rkey = fr_desc->data_mr->rkey;
  1845. wr = &inv_wr;
  1846. /* Bump the key */
  1847. key = (u8)(fr_desc->data_mr->rkey & 0x000000FF);
  1848. ib_update_fast_reg_key(fr_desc->data_mr, ++key);
  1849. }
  1850. /* Prepare FASTREG WR */
  1851. memset(&fr_wr, 0, sizeof(fr_wr));
  1852. fr_wr.opcode = IB_WR_FAST_REG_MR;
  1853. fr_wr.wr.fast_reg.iova_start =
  1854. fr_desc->data_frpl->page_list[0] + page_off;
  1855. fr_wr.wr.fast_reg.page_list = fr_desc->data_frpl;
  1856. fr_wr.wr.fast_reg.page_list_len = pagelist_len;
  1857. fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  1858. fr_wr.wr.fast_reg.length = data_len;
  1859. fr_wr.wr.fast_reg.rkey = fr_desc->data_mr->rkey;
  1860. fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
  1861. if (!wr)
  1862. wr = &fr_wr;
  1863. else
  1864. wr->next = &fr_wr;
  1865. ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
  1866. if (ret) {
  1867. pr_err("fast registration failed, ret:%d\n", ret);
  1868. return ret;
  1869. }
  1870. fr_desc->valid = false;
  1871. ib_sge->lkey = fr_desc->data_mr->lkey;
  1872. ib_sge->addr = fr_desc->data_frpl->page_list[0] + page_off;
  1873. ib_sge->length = data_len;
  1874. pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u lkey: %08x\n",
  1875. ib_sge->addr, ib_sge->length, ib_sge->lkey);
  1876. return ret;
  1877. }
  1878. static int
  1879. isert_reg_rdma_frwr(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  1880. struct isert_rdma_wr *wr)
  1881. {
  1882. struct se_cmd *se_cmd = &cmd->se_cmd;
  1883. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1884. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1885. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1886. struct ib_send_wr *send_wr;
  1887. struct ib_sge *ib_sge;
  1888. struct scatterlist *sg_start;
  1889. struct fast_reg_descriptor *fr_desc;
  1890. u32 sg_off = 0, sg_nents;
  1891. u32 offset = 0, data_len, data_left, rdma_write_max;
  1892. int ret = 0, count;
  1893. unsigned long flags;
  1894. if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
  1895. data_left = se_cmd->data_length;
  1896. } else {
  1897. sg_off = cmd->write_data_done / PAGE_SIZE;
  1898. data_left = se_cmd->data_length - cmd->write_data_done;
  1899. offset = cmd->write_data_done;
  1900. isert_cmd->tx_desc.isert_cmd = isert_cmd;
  1901. }
  1902. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1903. sg_nents = se_cmd->t_data_nents - sg_off;
  1904. count = ib_dma_map_sg(ib_dev, sg_start, sg_nents,
  1905. (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
  1906. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1907. if (unlikely(!count)) {
  1908. pr_err("Cmd: %p unrable to map SGs\n", isert_cmd);
  1909. return -EINVAL;
  1910. }
  1911. wr->sge = sg_start;
  1912. wr->num_sge = sg_nents;
  1913. pr_debug("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
  1914. isert_cmd, count, sg_start, sg_nents, data_left);
  1915. memset(&wr->s_ib_sge, 0, sizeof(*ib_sge));
  1916. ib_sge = &wr->s_ib_sge;
  1917. wr->ib_sge = ib_sge;
  1918. wr->send_wr_num = 1;
  1919. memset(&wr->s_send_wr, 0, sizeof(*send_wr));
  1920. wr->send_wr = &wr->s_send_wr;
  1921. wr->isert_cmd = isert_cmd;
  1922. rdma_write_max = ISCSI_ISER_SG_TABLESIZE * PAGE_SIZE;
  1923. send_wr = &isert_cmd->rdma_wr.s_send_wr;
  1924. send_wr->sg_list = ib_sge;
  1925. send_wr->num_sge = 1;
  1926. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  1927. if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
  1928. send_wr->opcode = IB_WR_RDMA_WRITE;
  1929. send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
  1930. send_wr->wr.rdma.rkey = isert_cmd->read_stag;
  1931. send_wr->send_flags = 0;
  1932. send_wr->next = &isert_cmd->tx_desc.send_wr;
  1933. } else {
  1934. send_wr->opcode = IB_WR_RDMA_READ;
  1935. send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
  1936. send_wr->wr.rdma.rkey = isert_cmd->write_stag;
  1937. send_wr->send_flags = IB_SEND_SIGNALED;
  1938. }
  1939. data_len = min(data_left, rdma_write_max);
  1940. wr->cur_rdma_length = data_len;
  1941. /* if there is a single dma entry, dma mr is sufficient */
  1942. if (count == 1) {
  1943. ib_sge->addr = ib_sg_dma_address(ib_dev, &sg_start[0]);
  1944. ib_sge->length = ib_sg_dma_len(ib_dev, &sg_start[0]);
  1945. ib_sge->lkey = isert_conn->conn_mr->lkey;
  1946. wr->fr_desc = NULL;
  1947. } else {
  1948. spin_lock_irqsave(&isert_conn->conn_lock, flags);
  1949. fr_desc = list_first_entry(&isert_conn->conn_frwr_pool,
  1950. struct fast_reg_descriptor, list);
  1951. list_del(&fr_desc->list);
  1952. spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
  1953. wr->fr_desc = fr_desc;
  1954. ret = isert_fast_reg_mr(fr_desc, isert_cmd, isert_conn,
  1955. ib_sge, offset, data_len);
  1956. if (ret) {
  1957. list_add_tail(&fr_desc->list, &isert_conn->conn_frwr_pool);
  1958. goto unmap_sg;
  1959. }
  1960. }
  1961. return 0;
  1962. unmap_sg:
  1963. ib_dma_unmap_sg(ib_dev, sg_start, sg_nents,
  1964. (wr->iser_ib_op == ISER_IB_RDMA_WRITE) ?
  1965. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1966. return ret;
  1967. }
  1968. static int
  1969. isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1970. {
  1971. struct se_cmd *se_cmd = &cmd->se_cmd;
  1972. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1973. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1974. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1975. struct isert_device *device = isert_conn->conn_device;
  1976. struct ib_send_wr *wr_failed;
  1977. int rc;
  1978. pr_debug("Cmd: %p RDMA_WRITE data_length: %u\n",
  1979. isert_cmd, se_cmd->data_length);
  1980. wr->iser_ib_op = ISER_IB_RDMA_WRITE;
  1981. rc = device->reg_rdma_mem(conn, cmd, wr);
  1982. if (rc) {
  1983. pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
  1984. return rc;
  1985. }
  1986. /*
  1987. * Build isert_conn->tx_desc for iSCSI response PDU and attach
  1988. */
  1989. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1990. iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
  1991. &isert_cmd->tx_desc.iscsi_header);
  1992. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1993. isert_init_send_wr(isert_conn, isert_cmd,
  1994. &isert_cmd->tx_desc.send_wr, true);
  1995. atomic_inc(&isert_conn->post_send_buf_count);
  1996. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  1997. if (rc) {
  1998. pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
  1999. atomic_dec(&isert_conn->post_send_buf_count);
  2000. }
  2001. pr_debug("Cmd: %p posted RDMA_WRITE + Response for iSER Data READ\n",
  2002. isert_cmd);
  2003. return 1;
  2004. }
  2005. static int
  2006. isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
  2007. {
  2008. struct se_cmd *se_cmd = &cmd->se_cmd;
  2009. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  2010. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  2011. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  2012. struct isert_device *device = isert_conn->conn_device;
  2013. struct ib_send_wr *wr_failed;
  2014. int rc;
  2015. pr_debug("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
  2016. isert_cmd, se_cmd->data_length, cmd->write_data_done);
  2017. wr->iser_ib_op = ISER_IB_RDMA_READ;
  2018. rc = device->reg_rdma_mem(conn, cmd, wr);
  2019. if (rc) {
  2020. pr_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
  2021. return rc;
  2022. }
  2023. atomic_inc(&isert_conn->post_send_buf_count);
  2024. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  2025. if (rc) {
  2026. pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
  2027. atomic_dec(&isert_conn->post_send_buf_count);
  2028. }
  2029. pr_debug("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
  2030. isert_cmd);
  2031. return 0;
  2032. }
  2033. static int
  2034. isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  2035. {
  2036. int ret;
  2037. switch (state) {
  2038. case ISTATE_SEND_NOPIN_WANT_RESPONSE:
  2039. ret = isert_put_nopin(cmd, conn, false);
  2040. break;
  2041. default:
  2042. pr_err("Unknown immediate state: 0x%02x\n", state);
  2043. ret = -EINVAL;
  2044. break;
  2045. }
  2046. return ret;
  2047. }
  2048. static int
  2049. isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  2050. {
  2051. int ret;
  2052. switch (state) {
  2053. case ISTATE_SEND_LOGOUTRSP:
  2054. ret = isert_put_logout_rsp(cmd, conn);
  2055. if (!ret) {
  2056. pr_debug("Returning iSER Logout -EAGAIN\n");
  2057. ret = -EAGAIN;
  2058. }
  2059. break;
  2060. case ISTATE_SEND_NOPIN:
  2061. ret = isert_put_nopin(cmd, conn, true);
  2062. break;
  2063. case ISTATE_SEND_TASKMGTRSP:
  2064. ret = isert_put_tm_rsp(cmd, conn);
  2065. break;
  2066. case ISTATE_SEND_REJECT:
  2067. ret = isert_put_reject(cmd, conn);
  2068. break;
  2069. case ISTATE_SEND_TEXTRSP:
  2070. ret = isert_put_text_rsp(cmd, conn);
  2071. break;
  2072. case ISTATE_SEND_STATUS:
  2073. /*
  2074. * Special case for sending non GOOD SCSI status from TX thread
  2075. * context during pre se_cmd excecution failure.
  2076. */
  2077. ret = isert_put_response(conn, cmd);
  2078. break;
  2079. default:
  2080. pr_err("Unknown response state: 0x%02x\n", state);
  2081. ret = -EINVAL;
  2082. break;
  2083. }
  2084. return ret;
  2085. }
  2086. static int
  2087. isert_setup_np(struct iscsi_np *np,
  2088. struct __kernel_sockaddr_storage *ksockaddr)
  2089. {
  2090. struct isert_np *isert_np;
  2091. struct rdma_cm_id *isert_lid;
  2092. struct sockaddr *sa;
  2093. int ret;
  2094. isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
  2095. if (!isert_np) {
  2096. pr_err("Unable to allocate struct isert_np\n");
  2097. return -ENOMEM;
  2098. }
  2099. init_waitqueue_head(&isert_np->np_accept_wq);
  2100. mutex_init(&isert_np->np_accept_mutex);
  2101. INIT_LIST_HEAD(&isert_np->np_accept_list);
  2102. init_completion(&isert_np->np_login_comp);
  2103. sa = (struct sockaddr *)ksockaddr;
  2104. pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
  2105. /*
  2106. * Setup the np->np_sockaddr from the passed sockaddr setup
  2107. * in iscsi_target_configfs.c code..
  2108. */
  2109. memcpy(&np->np_sockaddr, ksockaddr,
  2110. sizeof(struct __kernel_sockaddr_storage));
  2111. isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
  2112. IB_QPT_RC);
  2113. if (IS_ERR(isert_lid)) {
  2114. pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
  2115. PTR_ERR(isert_lid));
  2116. ret = PTR_ERR(isert_lid);
  2117. goto out;
  2118. }
  2119. ret = rdma_bind_addr(isert_lid, sa);
  2120. if (ret) {
  2121. pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
  2122. goto out_lid;
  2123. }
  2124. ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
  2125. if (ret) {
  2126. pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
  2127. goto out_lid;
  2128. }
  2129. isert_np->np_cm_id = isert_lid;
  2130. np->np_context = isert_np;
  2131. pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
  2132. return 0;
  2133. out_lid:
  2134. rdma_destroy_id(isert_lid);
  2135. out:
  2136. kfree(isert_np);
  2137. return ret;
  2138. }
  2139. static int
  2140. isert_check_accept_queue(struct isert_np *isert_np)
  2141. {
  2142. int empty;
  2143. mutex_lock(&isert_np->np_accept_mutex);
  2144. empty = list_empty(&isert_np->np_accept_list);
  2145. mutex_unlock(&isert_np->np_accept_mutex);
  2146. return empty;
  2147. }
  2148. static int
  2149. isert_rdma_accept(struct isert_conn *isert_conn)
  2150. {
  2151. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  2152. struct rdma_conn_param cp;
  2153. int ret;
  2154. memset(&cp, 0, sizeof(struct rdma_conn_param));
  2155. cp.responder_resources = isert_conn->responder_resources;
  2156. cp.initiator_depth = isert_conn->initiator_depth;
  2157. cp.retry_count = 7;
  2158. cp.rnr_retry_count = 7;
  2159. pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
  2160. ret = rdma_accept(cm_id, &cp);
  2161. if (ret) {
  2162. pr_err("rdma_accept() failed with: %d\n", ret);
  2163. return ret;
  2164. }
  2165. pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
  2166. return 0;
  2167. }
  2168. static int
  2169. isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
  2170. {
  2171. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  2172. int ret;
  2173. pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
  2174. /*
  2175. * For login requests after the first PDU, isert_rx_login_req() will
  2176. * kick schedule_delayed_work(&conn->login_work) as the packet is
  2177. * received, which turns this callback from iscsi_target_do_login_rx()
  2178. * into a NOP.
  2179. */
  2180. if (!login->first_request)
  2181. return 0;
  2182. ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
  2183. if (ret)
  2184. return ret;
  2185. pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
  2186. return 0;
  2187. }
  2188. static void
  2189. isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
  2190. struct isert_conn *isert_conn)
  2191. {
  2192. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  2193. struct rdma_route *cm_route = &cm_id->route;
  2194. struct sockaddr_in *sock_in;
  2195. struct sockaddr_in6 *sock_in6;
  2196. conn->login_family = np->np_sockaddr.ss_family;
  2197. if (np->np_sockaddr.ss_family == AF_INET6) {
  2198. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
  2199. snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
  2200. &sock_in6->sin6_addr.in6_u);
  2201. conn->login_port = ntohs(sock_in6->sin6_port);
  2202. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
  2203. snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
  2204. &sock_in6->sin6_addr.in6_u);
  2205. conn->local_port = ntohs(sock_in6->sin6_port);
  2206. } else {
  2207. sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
  2208. sprintf(conn->login_ip, "%pI4",
  2209. &sock_in->sin_addr.s_addr);
  2210. conn->login_port = ntohs(sock_in->sin_port);
  2211. sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
  2212. sprintf(conn->local_ip, "%pI4",
  2213. &sock_in->sin_addr.s_addr);
  2214. conn->local_port = ntohs(sock_in->sin_port);
  2215. }
  2216. }
  2217. static int
  2218. isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
  2219. {
  2220. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  2221. struct isert_conn *isert_conn;
  2222. int max_accept = 0, ret;
  2223. accept_wait:
  2224. ret = wait_event_interruptible(isert_np->np_accept_wq,
  2225. !isert_check_accept_queue(isert_np) ||
  2226. np->np_thread_state == ISCSI_NP_THREAD_RESET);
  2227. if (max_accept > 5)
  2228. return -ENODEV;
  2229. spin_lock_bh(&np->np_thread_lock);
  2230. if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
  2231. spin_unlock_bh(&np->np_thread_lock);
  2232. pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
  2233. return -ENODEV;
  2234. }
  2235. spin_unlock_bh(&np->np_thread_lock);
  2236. mutex_lock(&isert_np->np_accept_mutex);
  2237. if (list_empty(&isert_np->np_accept_list)) {
  2238. mutex_unlock(&isert_np->np_accept_mutex);
  2239. max_accept++;
  2240. goto accept_wait;
  2241. }
  2242. isert_conn = list_first_entry(&isert_np->np_accept_list,
  2243. struct isert_conn, conn_accept_node);
  2244. list_del_init(&isert_conn->conn_accept_node);
  2245. mutex_unlock(&isert_np->np_accept_mutex);
  2246. conn->context = isert_conn;
  2247. isert_conn->conn = conn;
  2248. max_accept = 0;
  2249. ret = isert_rdma_post_recvl(isert_conn);
  2250. if (ret)
  2251. return ret;
  2252. ret = isert_rdma_accept(isert_conn);
  2253. if (ret)
  2254. return ret;
  2255. isert_set_conn_info(np, conn, isert_conn);
  2256. pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
  2257. return 0;
  2258. }
  2259. static void
  2260. isert_free_np(struct iscsi_np *np)
  2261. {
  2262. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  2263. rdma_destroy_id(isert_np->np_cm_id);
  2264. np->np_context = NULL;
  2265. kfree(isert_np);
  2266. }
  2267. static int isert_check_state(struct isert_conn *isert_conn, int state)
  2268. {
  2269. int ret;
  2270. mutex_lock(&isert_conn->conn_mutex);
  2271. ret = (isert_conn->state == state);
  2272. mutex_unlock(&isert_conn->conn_mutex);
  2273. return ret;
  2274. }
  2275. static void isert_free_conn(struct iscsi_conn *conn)
  2276. {
  2277. struct isert_conn *isert_conn = conn->context;
  2278. pr_debug("isert_free_conn: Starting \n");
  2279. /*
  2280. * Decrement post_send_buf_count for special case when called
  2281. * from isert_do_control_comp() -> iscsit_logout_post_handler()
  2282. */
  2283. mutex_lock(&isert_conn->conn_mutex);
  2284. if (isert_conn->logout_posted)
  2285. atomic_dec(&isert_conn->post_send_buf_count);
  2286. if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
  2287. pr_debug("Calling rdma_disconnect from isert_free_conn\n");
  2288. rdma_disconnect(isert_conn->conn_cm_id);
  2289. }
  2290. /*
  2291. * Only wait for conn_wait_comp_err if the isert_conn made it
  2292. * into full feature phase..
  2293. */
  2294. if (isert_conn->state == ISER_CONN_UP) {
  2295. pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
  2296. isert_conn->state);
  2297. mutex_unlock(&isert_conn->conn_mutex);
  2298. wait_event(isert_conn->conn_wait_comp_err,
  2299. (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
  2300. wait_event(isert_conn->conn_wait,
  2301. (isert_check_state(isert_conn, ISER_CONN_DOWN)));
  2302. isert_put_conn(isert_conn);
  2303. return;
  2304. }
  2305. if (isert_conn->state == ISER_CONN_INIT) {
  2306. mutex_unlock(&isert_conn->conn_mutex);
  2307. isert_put_conn(isert_conn);
  2308. return;
  2309. }
  2310. pr_debug("isert_free_conn: wait_event conn_wait %d\n",
  2311. isert_conn->state);
  2312. mutex_unlock(&isert_conn->conn_mutex);
  2313. wait_event(isert_conn->conn_wait,
  2314. (isert_check_state(isert_conn, ISER_CONN_DOWN)));
  2315. isert_put_conn(isert_conn);
  2316. }
  2317. static struct iscsit_transport iser_target_transport = {
  2318. .name = "IB/iSER",
  2319. .transport_type = ISCSI_INFINIBAND,
  2320. .priv_size = sizeof(struct isert_cmd),
  2321. .owner = THIS_MODULE,
  2322. .iscsit_setup_np = isert_setup_np,
  2323. .iscsit_accept_np = isert_accept_np,
  2324. .iscsit_free_np = isert_free_np,
  2325. .iscsit_free_conn = isert_free_conn,
  2326. .iscsit_get_login_rx = isert_get_login_rx,
  2327. .iscsit_put_login_tx = isert_put_login_tx,
  2328. .iscsit_immediate_queue = isert_immediate_queue,
  2329. .iscsit_response_queue = isert_response_queue,
  2330. .iscsit_get_dataout = isert_get_dataout,
  2331. .iscsit_queue_data_in = isert_put_datain,
  2332. .iscsit_queue_status = isert_put_response,
  2333. };
  2334. static int __init isert_init(void)
  2335. {
  2336. int ret;
  2337. isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
  2338. if (!isert_rx_wq) {
  2339. pr_err("Unable to allocate isert_rx_wq\n");
  2340. return -ENOMEM;
  2341. }
  2342. isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
  2343. if (!isert_comp_wq) {
  2344. pr_err("Unable to allocate isert_comp_wq\n");
  2345. ret = -ENOMEM;
  2346. goto destroy_rx_wq;
  2347. }
  2348. iscsit_register_transport(&iser_target_transport);
  2349. pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
  2350. return 0;
  2351. destroy_rx_wq:
  2352. destroy_workqueue(isert_rx_wq);
  2353. return ret;
  2354. }
  2355. static void __exit isert_exit(void)
  2356. {
  2357. destroy_workqueue(isert_comp_wq);
  2358. destroy_workqueue(isert_rx_wq);
  2359. iscsit_unregister_transport(&iser_target_transport);
  2360. pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
  2361. }
  2362. MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
  2363. MODULE_VERSION("0.1");
  2364. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  2365. MODULE_LICENSE("GPL");
  2366. module_init(isert_init);
  2367. module_exit(isert_exit);