ib_isert.c 67 KB

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