ib_isert.c 68 KB

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