ib_isert.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  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. complete(&isert_conn->conn_login_comp);
  730. }
  731. static void
  732. isert_release_cmd(struct iscsi_cmd *cmd)
  733. {
  734. struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
  735. iscsi_cmd);
  736. pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
  737. kfree(cmd->buf_ptr);
  738. kfree(cmd->tmr_req);
  739. kmem_cache_free(isert_cmd_cache, isert_cmd);
  740. }
  741. static struct iscsi_cmd
  742. *isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
  743. {
  744. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  745. struct isert_cmd *isert_cmd;
  746. isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
  747. if (!isert_cmd) {
  748. pr_err("Unable to allocate isert_cmd\n");
  749. return NULL;
  750. }
  751. isert_cmd->conn = isert_conn;
  752. isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
  753. return &isert_cmd->iscsi_cmd;
  754. }
  755. static int
  756. isert_handle_scsi_cmd(struct isert_conn *isert_conn,
  757. struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
  758. unsigned char *buf)
  759. {
  760. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  761. struct iscsi_conn *conn = isert_conn->conn;
  762. struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
  763. struct scatterlist *sg;
  764. int imm_data, imm_data_len, unsol_data, sg_nents, rc;
  765. bool dump_payload = false;
  766. rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
  767. if (rc < 0)
  768. return rc;
  769. imm_data = cmd->immediate_data;
  770. imm_data_len = cmd->first_burst_len;
  771. unsol_data = cmd->unsolicited_data;
  772. rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
  773. if (rc < 0) {
  774. return 0;
  775. } else if (rc > 0) {
  776. dump_payload = true;
  777. goto sequence_cmd;
  778. }
  779. if (!imm_data)
  780. return 0;
  781. sg = &cmd->se_cmd.t_data_sg[0];
  782. sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
  783. pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
  784. sg, sg_nents, &rx_desc->data[0], imm_data_len);
  785. sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
  786. cmd->write_data_done += imm_data_len;
  787. if (cmd->write_data_done == cmd->se_cmd.data_length) {
  788. spin_lock_bh(&cmd->istate_lock);
  789. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  790. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  791. spin_unlock_bh(&cmd->istate_lock);
  792. }
  793. sequence_cmd:
  794. rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
  795. if (!rc && dump_payload == false && unsol_data)
  796. iscsit_set_unsoliticed_dataout(cmd);
  797. return 0;
  798. }
  799. static int
  800. isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
  801. struct iser_rx_desc *rx_desc, unsigned char *buf)
  802. {
  803. struct scatterlist *sg_start;
  804. struct iscsi_conn *conn = isert_conn->conn;
  805. struct iscsi_cmd *cmd = NULL;
  806. struct iscsi_data *hdr = (struct iscsi_data *)buf;
  807. u32 unsol_data_len = ntoh24(hdr->dlength);
  808. int rc, sg_nents, sg_off, page_off;
  809. rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
  810. if (rc < 0)
  811. return rc;
  812. else if (!cmd)
  813. return 0;
  814. /*
  815. * FIXME: Unexpected unsolicited_data out
  816. */
  817. if (!cmd->unsolicited_data) {
  818. pr_err("Received unexpected solicited data payload\n");
  819. dump_stack();
  820. return -1;
  821. }
  822. pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
  823. unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
  824. sg_off = cmd->write_data_done / PAGE_SIZE;
  825. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  826. sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
  827. page_off = cmd->write_data_done % PAGE_SIZE;
  828. /*
  829. * FIXME: Non page-aligned unsolicited_data out
  830. */
  831. if (page_off) {
  832. pr_err("Received unexpected non-page aligned data payload\n");
  833. dump_stack();
  834. return -1;
  835. }
  836. pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
  837. sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
  838. sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
  839. unsol_data_len);
  840. rc = iscsit_check_dataout_payload(cmd, hdr, false);
  841. if (rc < 0)
  842. return rc;
  843. return 0;
  844. }
  845. static int
  846. isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  847. struct iser_rx_desc *rx_desc, unsigned char *buf)
  848. {
  849. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  850. struct iscsi_conn *conn = isert_conn->conn;
  851. struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
  852. int rc;
  853. rc = iscsit_setup_nop_out(conn, cmd, hdr);
  854. if (rc < 0)
  855. return rc;
  856. /*
  857. * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
  858. */
  859. return iscsit_process_nop_out(conn, cmd, hdr);
  860. }
  861. static int
  862. isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  863. struct iser_rx_desc *rx_desc, struct iscsi_text *hdr)
  864. {
  865. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  866. struct iscsi_conn *conn = isert_conn->conn;
  867. u32 payload_length = ntoh24(hdr->dlength);
  868. int rc;
  869. unsigned char *text_in;
  870. rc = iscsit_setup_text_cmd(conn, cmd, hdr);
  871. if (rc < 0)
  872. return rc;
  873. text_in = kzalloc(payload_length, GFP_KERNEL);
  874. if (!text_in) {
  875. pr_err("Unable to allocate text_in of payload_length: %u\n",
  876. payload_length);
  877. return -ENOMEM;
  878. }
  879. cmd->text_in_ptr = text_in;
  880. memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
  881. return iscsit_process_text_cmd(conn, cmd, hdr);
  882. }
  883. static int
  884. isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
  885. uint32_t read_stag, uint64_t read_va,
  886. uint32_t write_stag, uint64_t write_va)
  887. {
  888. struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
  889. struct iscsi_conn *conn = isert_conn->conn;
  890. struct iscsi_session *sess = conn->sess;
  891. struct iscsi_cmd *cmd;
  892. struct isert_cmd *isert_cmd;
  893. int ret = -EINVAL;
  894. u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
  895. if (sess->sess_ops->SessionType &&
  896. (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
  897. pr_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
  898. " ignoring\n", opcode);
  899. return 0;
  900. }
  901. switch (opcode) {
  902. case ISCSI_OP_SCSI_CMD:
  903. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  904. if (!cmd)
  905. break;
  906. isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
  907. isert_cmd->read_stag = read_stag;
  908. isert_cmd->read_va = read_va;
  909. isert_cmd->write_stag = write_stag;
  910. isert_cmd->write_va = write_va;
  911. ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
  912. rx_desc, (unsigned char *)hdr);
  913. break;
  914. case ISCSI_OP_NOOP_OUT:
  915. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  916. if (!cmd)
  917. break;
  918. isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
  919. ret = isert_handle_nop_out(isert_conn, isert_cmd,
  920. rx_desc, (unsigned char *)hdr);
  921. break;
  922. case ISCSI_OP_SCSI_DATA_OUT:
  923. ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
  924. (unsigned char *)hdr);
  925. break;
  926. case ISCSI_OP_SCSI_TMFUNC:
  927. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  928. if (!cmd)
  929. break;
  930. ret = iscsit_handle_task_mgt_cmd(conn, cmd,
  931. (unsigned char *)hdr);
  932. break;
  933. case ISCSI_OP_LOGOUT:
  934. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  935. if (!cmd)
  936. break;
  937. ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
  938. if (ret > 0)
  939. wait_for_completion_timeout(&conn->conn_logout_comp,
  940. SECONDS_FOR_LOGOUT_COMP *
  941. HZ);
  942. break;
  943. case ISCSI_OP_TEXT:
  944. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  945. if (!cmd)
  946. break;
  947. isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
  948. ret = isert_handle_text_cmd(isert_conn, isert_cmd,
  949. rx_desc, (struct iscsi_text *)hdr);
  950. break;
  951. default:
  952. pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
  953. dump_stack();
  954. break;
  955. }
  956. return ret;
  957. }
  958. static void
  959. isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
  960. {
  961. struct iser_hdr *iser_hdr = &rx_desc->iser_header;
  962. uint64_t read_va = 0, write_va = 0;
  963. uint32_t read_stag = 0, write_stag = 0;
  964. int rc;
  965. switch (iser_hdr->flags & 0xF0) {
  966. case ISCSI_CTRL:
  967. if (iser_hdr->flags & ISER_RSV) {
  968. read_stag = be32_to_cpu(iser_hdr->read_stag);
  969. read_va = be64_to_cpu(iser_hdr->read_va);
  970. pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
  971. read_stag, (unsigned long long)read_va);
  972. }
  973. if (iser_hdr->flags & ISER_WSV) {
  974. write_stag = be32_to_cpu(iser_hdr->write_stag);
  975. write_va = be64_to_cpu(iser_hdr->write_va);
  976. pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
  977. write_stag, (unsigned long long)write_va);
  978. }
  979. pr_debug("ISER ISCSI_CTRL PDU\n");
  980. break;
  981. case ISER_HELLO:
  982. pr_err("iSER Hello message\n");
  983. break;
  984. default:
  985. pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
  986. break;
  987. }
  988. rc = isert_rx_opcode(isert_conn, rx_desc,
  989. read_stag, read_va, write_stag, write_va);
  990. }
  991. static void
  992. isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
  993. unsigned long xfer_len)
  994. {
  995. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  996. struct iscsi_hdr *hdr;
  997. u64 rx_dma;
  998. int rx_buflen, outstanding;
  999. if ((char *)desc == isert_conn->login_req_buf) {
  1000. rx_dma = isert_conn->login_req_dma;
  1001. rx_buflen = ISER_RX_LOGIN_SIZE;
  1002. pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  1003. rx_dma, rx_buflen);
  1004. } else {
  1005. rx_dma = desc->dma_addr;
  1006. rx_buflen = ISER_RX_PAYLOAD_SIZE;
  1007. pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  1008. rx_dma, rx_buflen);
  1009. }
  1010. ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
  1011. hdr = &desc->iscsi_header;
  1012. pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
  1013. hdr->opcode, hdr->itt, hdr->flags,
  1014. (int)(xfer_len - ISER_HEADERS_LEN));
  1015. if ((char *)desc == isert_conn->login_req_buf)
  1016. isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
  1017. isert_conn);
  1018. else
  1019. isert_rx_do_work(desc, isert_conn);
  1020. ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
  1021. DMA_FROM_DEVICE);
  1022. isert_conn->post_recv_buf_count--;
  1023. pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
  1024. isert_conn->post_recv_buf_count);
  1025. if ((char *)desc == isert_conn->login_req_buf)
  1026. return;
  1027. outstanding = isert_conn->post_recv_buf_count;
  1028. if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
  1029. int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
  1030. ISERT_MIN_POSTED_RX);
  1031. err = isert_post_recv(isert_conn, count);
  1032. if (err) {
  1033. pr_err("isert_post_recv() count: %d failed, %d\n",
  1034. count, err);
  1035. }
  1036. }
  1037. }
  1038. static void
  1039. isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
  1040. {
  1041. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1042. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1043. pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
  1044. if (wr->sge) {
  1045. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
  1046. wr->sge = NULL;
  1047. }
  1048. kfree(wr->send_wr);
  1049. wr->send_wr = NULL;
  1050. kfree(isert_cmd->ib_sge);
  1051. isert_cmd->ib_sge = NULL;
  1052. }
  1053. static void
  1054. isert_put_cmd(struct isert_cmd *isert_cmd)
  1055. {
  1056. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1057. struct isert_conn *isert_conn = isert_cmd->conn;
  1058. struct iscsi_conn *conn = isert_conn->conn;
  1059. pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
  1060. switch (cmd->iscsi_opcode) {
  1061. case ISCSI_OP_SCSI_CMD:
  1062. spin_lock_bh(&conn->cmd_lock);
  1063. if (!list_empty(&cmd->i_conn_node))
  1064. list_del(&cmd->i_conn_node);
  1065. spin_unlock_bh(&conn->cmd_lock);
  1066. if (cmd->data_direction == DMA_TO_DEVICE)
  1067. iscsit_stop_dataout_timer(cmd);
  1068. isert_unmap_cmd(isert_cmd, isert_conn);
  1069. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1070. break;
  1071. case ISCSI_OP_SCSI_TMFUNC:
  1072. spin_lock_bh(&conn->cmd_lock);
  1073. if (!list_empty(&cmd->i_conn_node))
  1074. list_del(&cmd->i_conn_node);
  1075. spin_unlock_bh(&conn->cmd_lock);
  1076. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1077. break;
  1078. case ISCSI_OP_REJECT:
  1079. case ISCSI_OP_NOOP_OUT:
  1080. case ISCSI_OP_TEXT:
  1081. spin_lock_bh(&conn->cmd_lock);
  1082. if (!list_empty(&cmd->i_conn_node))
  1083. list_del(&cmd->i_conn_node);
  1084. spin_unlock_bh(&conn->cmd_lock);
  1085. /*
  1086. * Handle special case for REJECT when iscsi_add_reject*() has
  1087. * overwritten the original iscsi_opcode assignment, and the
  1088. * associated cmd->se_cmd needs to be released.
  1089. */
  1090. if (cmd->se_cmd.se_tfo != NULL) {
  1091. pr_debug("Calling transport_generic_free_cmd from"
  1092. " isert_put_cmd for 0x%02x\n",
  1093. cmd->iscsi_opcode);
  1094. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1095. break;
  1096. }
  1097. /*
  1098. * Fall-through
  1099. */
  1100. default:
  1101. isert_release_cmd(cmd);
  1102. break;
  1103. }
  1104. }
  1105. static void
  1106. isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
  1107. {
  1108. if (tx_desc->dma_addr != 0) {
  1109. pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
  1110. ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
  1111. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  1112. tx_desc->dma_addr = 0;
  1113. }
  1114. }
  1115. static void
  1116. isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
  1117. struct ib_device *ib_dev)
  1118. {
  1119. if (isert_cmd->pdu_buf_dma != 0) {
  1120. pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
  1121. ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
  1122. isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
  1123. isert_cmd->pdu_buf_dma = 0;
  1124. }
  1125. isert_unmap_tx_desc(tx_desc, ib_dev);
  1126. isert_put_cmd(isert_cmd);
  1127. }
  1128. static void
  1129. isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
  1130. struct isert_cmd *isert_cmd)
  1131. {
  1132. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1133. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1134. struct se_cmd *se_cmd = &cmd->se_cmd;
  1135. struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
  1136. iscsit_stop_dataout_timer(cmd);
  1137. if (wr->sge) {
  1138. pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
  1139. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
  1140. wr->sge = NULL;
  1141. }
  1142. if (isert_cmd->ib_sge) {
  1143. pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
  1144. kfree(isert_cmd->ib_sge);
  1145. isert_cmd->ib_sge = NULL;
  1146. }
  1147. cmd->write_data_done = se_cmd->data_length;
  1148. pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
  1149. spin_lock_bh(&cmd->istate_lock);
  1150. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  1151. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  1152. spin_unlock_bh(&cmd->istate_lock);
  1153. target_execute_cmd(se_cmd);
  1154. }
  1155. static void
  1156. isert_do_control_comp(struct work_struct *work)
  1157. {
  1158. struct isert_cmd *isert_cmd = container_of(work,
  1159. struct isert_cmd, comp_work);
  1160. struct isert_conn *isert_conn = isert_cmd->conn;
  1161. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1162. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1163. switch (cmd->i_state) {
  1164. case ISTATE_SEND_TASKMGTRSP:
  1165. pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
  1166. atomic_dec(&isert_conn->post_send_buf_count);
  1167. iscsit_tmr_post_handler(cmd, cmd->conn);
  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_REJECT:
  1172. pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
  1173. atomic_dec(&isert_conn->post_send_buf_count);
  1174. cmd->i_state = ISTATE_SENT_STATUS;
  1175. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1176. break;
  1177. case ISTATE_SEND_LOGOUTRSP:
  1178. pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
  1179. /*
  1180. * Call atomic_dec(&isert_conn->post_send_buf_count)
  1181. * from isert_free_conn()
  1182. */
  1183. isert_conn->logout_posted = true;
  1184. iscsit_logout_post_handler(cmd, cmd->conn);
  1185. break;
  1186. case ISTATE_SEND_TEXTRSP:
  1187. atomic_dec(&isert_conn->post_send_buf_count);
  1188. cmd->i_state = ISTATE_SENT_STATUS;
  1189. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1190. break;
  1191. default:
  1192. pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
  1193. dump_stack();
  1194. break;
  1195. }
  1196. }
  1197. static void
  1198. isert_response_completion(struct iser_tx_desc *tx_desc,
  1199. struct isert_cmd *isert_cmd,
  1200. struct isert_conn *isert_conn,
  1201. struct ib_device *ib_dev)
  1202. {
  1203. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1204. if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
  1205. cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
  1206. cmd->i_state == ISTATE_SEND_REJECT ||
  1207. cmd->i_state == ISTATE_SEND_TEXTRSP) {
  1208. isert_unmap_tx_desc(tx_desc, ib_dev);
  1209. INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
  1210. queue_work(isert_comp_wq, &isert_cmd->comp_work);
  1211. return;
  1212. }
  1213. atomic_dec(&isert_conn->post_send_buf_count);
  1214. cmd->i_state = ISTATE_SENT_STATUS;
  1215. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1216. }
  1217. static void
  1218. isert_send_completion(struct iser_tx_desc *tx_desc,
  1219. struct isert_conn *isert_conn)
  1220. {
  1221. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1222. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1223. struct isert_rdma_wr *wr;
  1224. if (!isert_cmd) {
  1225. atomic_dec(&isert_conn->post_send_buf_count);
  1226. isert_unmap_tx_desc(tx_desc, ib_dev);
  1227. return;
  1228. }
  1229. wr = &isert_cmd->rdma_wr;
  1230. switch (wr->iser_ib_op) {
  1231. case ISER_IB_RECV:
  1232. pr_err("isert_send_completion: Got ISER_IB_RECV\n");
  1233. dump_stack();
  1234. break;
  1235. case ISER_IB_SEND:
  1236. pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
  1237. isert_response_completion(tx_desc, isert_cmd,
  1238. isert_conn, ib_dev);
  1239. break;
  1240. case ISER_IB_RDMA_WRITE:
  1241. pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
  1242. dump_stack();
  1243. break;
  1244. case ISER_IB_RDMA_READ:
  1245. pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
  1246. atomic_dec(&isert_conn->post_send_buf_count);
  1247. isert_completion_rdma_read(tx_desc, isert_cmd);
  1248. break;
  1249. default:
  1250. pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
  1251. dump_stack();
  1252. break;
  1253. }
  1254. }
  1255. static void
  1256. isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
  1257. {
  1258. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1259. if (tx_desc) {
  1260. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1261. if (!isert_cmd)
  1262. isert_unmap_tx_desc(tx_desc, ib_dev);
  1263. else
  1264. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1265. }
  1266. if (isert_conn->post_recv_buf_count == 0 &&
  1267. atomic_read(&isert_conn->post_send_buf_count) == 0) {
  1268. pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  1269. pr_debug("Calling wake_up from isert_cq_comp_err\n");
  1270. mutex_lock(&isert_conn->conn_mutex);
  1271. if (isert_conn->state != ISER_CONN_DOWN)
  1272. isert_conn->state = ISER_CONN_TERMINATING;
  1273. mutex_unlock(&isert_conn->conn_mutex);
  1274. wake_up(&isert_conn->conn_wait_comp_err);
  1275. }
  1276. }
  1277. static void
  1278. isert_cq_tx_work(struct work_struct *work)
  1279. {
  1280. struct isert_cq_desc *cq_desc = container_of(work,
  1281. struct isert_cq_desc, cq_tx_work);
  1282. struct isert_device *device = cq_desc->device;
  1283. int cq_index = cq_desc->cq_index;
  1284. struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
  1285. struct isert_conn *isert_conn;
  1286. struct iser_tx_desc *tx_desc;
  1287. struct ib_wc wc;
  1288. while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
  1289. tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
  1290. isert_conn = wc.qp->qp_context;
  1291. if (wc.status == IB_WC_SUCCESS) {
  1292. isert_send_completion(tx_desc, isert_conn);
  1293. } else {
  1294. pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1295. pr_debug("TX wc.status: 0x%08x\n", wc.status);
  1296. pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
  1297. atomic_dec(&isert_conn->post_send_buf_count);
  1298. isert_cq_comp_err(tx_desc, isert_conn);
  1299. }
  1300. }
  1301. ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
  1302. }
  1303. static void
  1304. isert_cq_tx_callback(struct ib_cq *cq, void *context)
  1305. {
  1306. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1307. INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
  1308. queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
  1309. }
  1310. static void
  1311. isert_cq_rx_work(struct work_struct *work)
  1312. {
  1313. struct isert_cq_desc *cq_desc = container_of(work,
  1314. struct isert_cq_desc, cq_rx_work);
  1315. struct isert_device *device = cq_desc->device;
  1316. int cq_index = cq_desc->cq_index;
  1317. struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
  1318. struct isert_conn *isert_conn;
  1319. struct iser_rx_desc *rx_desc;
  1320. struct ib_wc wc;
  1321. unsigned long xfer_len;
  1322. while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
  1323. rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
  1324. isert_conn = wc.qp->qp_context;
  1325. if (wc.status == IB_WC_SUCCESS) {
  1326. xfer_len = (unsigned long)wc.byte_len;
  1327. isert_rx_completion(rx_desc, isert_conn, xfer_len);
  1328. } else {
  1329. pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1330. if (wc.status != IB_WC_WR_FLUSH_ERR) {
  1331. pr_debug("RX wc.status: 0x%08x\n", wc.status);
  1332. pr_debug("RX wc.vendor_err: 0x%08x\n",
  1333. wc.vendor_err);
  1334. }
  1335. isert_conn->post_recv_buf_count--;
  1336. isert_cq_comp_err(NULL, isert_conn);
  1337. }
  1338. }
  1339. ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
  1340. }
  1341. static void
  1342. isert_cq_rx_callback(struct ib_cq *cq, void *context)
  1343. {
  1344. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1345. INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
  1346. queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
  1347. }
  1348. static int
  1349. isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
  1350. {
  1351. struct ib_send_wr *wr_failed;
  1352. int ret;
  1353. atomic_inc(&isert_conn->post_send_buf_count);
  1354. ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
  1355. &wr_failed);
  1356. if (ret) {
  1357. pr_err("ib_post_send failed with %d\n", ret);
  1358. atomic_dec(&isert_conn->post_send_buf_count);
  1359. return ret;
  1360. }
  1361. return ret;
  1362. }
  1363. static int
  1364. isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1365. {
  1366. struct isert_cmd *isert_cmd = container_of(cmd,
  1367. struct isert_cmd, iscsi_cmd);
  1368. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1369. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1370. struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
  1371. &isert_cmd->tx_desc.iscsi_header;
  1372. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1373. iscsit_build_rsp_pdu(cmd, conn, true, hdr);
  1374. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1375. /*
  1376. * Attach SENSE DATA payload to iSCSI Response PDU
  1377. */
  1378. if (cmd->se_cmd.sense_buffer &&
  1379. ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  1380. (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  1381. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1382. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1383. u32 padding, pdu_len;
  1384. put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
  1385. cmd->sense_buffer);
  1386. cmd->se_cmd.scsi_sense_length += sizeof(__be16);
  1387. padding = -(cmd->se_cmd.scsi_sense_length) & 3;
  1388. hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
  1389. pdu_len = cmd->se_cmd.scsi_sense_length + padding;
  1390. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1391. (void *)cmd->sense_buffer, pdu_len,
  1392. DMA_TO_DEVICE);
  1393. isert_cmd->pdu_buf_len = pdu_len;
  1394. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1395. tx_dsg->length = pdu_len;
  1396. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1397. isert_cmd->tx_desc.num_sge = 2;
  1398. }
  1399. isert_init_send_wr(isert_cmd, send_wr);
  1400. pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1401. return isert_post_response(isert_conn, isert_cmd);
  1402. }
  1403. static int
  1404. isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
  1405. bool nopout_response)
  1406. {
  1407. struct isert_cmd *isert_cmd = container_of(cmd,
  1408. struct isert_cmd, iscsi_cmd);
  1409. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1410. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1411. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1412. iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
  1413. &isert_cmd->tx_desc.iscsi_header,
  1414. nopout_response);
  1415. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1416. isert_init_send_wr(isert_cmd, send_wr);
  1417. pr_debug("Posting NOPIN Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1418. return isert_post_response(isert_conn, isert_cmd);
  1419. }
  1420. static int
  1421. isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1422. {
  1423. struct isert_cmd *isert_cmd = container_of(cmd,
  1424. struct isert_cmd, iscsi_cmd);
  1425. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1426. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1427. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1428. iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
  1429. &isert_cmd->tx_desc.iscsi_header);
  1430. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1431. isert_init_send_wr(isert_cmd, send_wr);
  1432. pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1433. return isert_post_response(isert_conn, isert_cmd);
  1434. }
  1435. static int
  1436. isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1437. {
  1438. struct isert_cmd *isert_cmd = container_of(cmd,
  1439. struct isert_cmd, iscsi_cmd);
  1440. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1441. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1442. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1443. iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
  1444. &isert_cmd->tx_desc.iscsi_header);
  1445. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1446. isert_init_send_wr(isert_cmd, send_wr);
  1447. pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1448. return isert_post_response(isert_conn, isert_cmd);
  1449. }
  1450. static int
  1451. isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1452. {
  1453. struct isert_cmd *isert_cmd = container_of(cmd,
  1454. struct isert_cmd, iscsi_cmd);
  1455. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1456. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1457. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1458. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1459. struct iscsi_reject *hdr =
  1460. (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
  1461. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1462. iscsit_build_reject(cmd, conn, hdr);
  1463. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1464. hton24(hdr->dlength, ISCSI_HDR_LEN);
  1465. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1466. (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
  1467. DMA_TO_DEVICE);
  1468. isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
  1469. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1470. tx_dsg->length = ISCSI_HDR_LEN;
  1471. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1472. isert_cmd->tx_desc.num_sge = 2;
  1473. isert_init_send_wr(isert_cmd, send_wr);
  1474. pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1475. return isert_post_response(isert_conn, isert_cmd);
  1476. }
  1477. static int
  1478. isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1479. {
  1480. struct isert_cmd *isert_cmd = container_of(cmd,
  1481. struct isert_cmd, iscsi_cmd);
  1482. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1483. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1484. struct iscsi_text_rsp *hdr =
  1485. (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
  1486. u32 txt_rsp_len;
  1487. int rc;
  1488. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1489. rc = iscsit_build_text_rsp(cmd, conn, hdr);
  1490. if (rc < 0)
  1491. return rc;
  1492. txt_rsp_len = rc;
  1493. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1494. if (txt_rsp_len) {
  1495. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1496. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1497. void *txt_rsp_buf = cmd->buf_ptr;
  1498. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1499. txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
  1500. isert_cmd->pdu_buf_len = txt_rsp_len;
  1501. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1502. tx_dsg->length = txt_rsp_len;
  1503. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1504. isert_cmd->tx_desc.num_sge = 2;
  1505. }
  1506. isert_init_send_wr(isert_cmd, send_wr);
  1507. pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1508. return isert_post_response(isert_conn, isert_cmd);
  1509. }
  1510. static int
  1511. isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1512. struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
  1513. u32 data_left, u32 offset)
  1514. {
  1515. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1516. struct scatterlist *sg_start, *tmp_sg;
  1517. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1518. u32 sg_off, page_off;
  1519. int i = 0, sg_nents;
  1520. sg_off = offset / PAGE_SIZE;
  1521. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1522. sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
  1523. page_off = offset % PAGE_SIZE;
  1524. send_wr->sg_list = ib_sge;
  1525. send_wr->num_sge = sg_nents;
  1526. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  1527. /*
  1528. * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
  1529. */
  1530. for_each_sg(sg_start, tmp_sg, sg_nents, i) {
  1531. pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
  1532. (unsigned long long)tmp_sg->dma_address,
  1533. tmp_sg->length, page_off);
  1534. ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
  1535. ib_sge->length = min_t(u32, data_left,
  1536. ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
  1537. ib_sge->lkey = isert_conn->conn_mr->lkey;
  1538. pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u\n",
  1539. ib_sge->addr, ib_sge->length);
  1540. page_off = 0;
  1541. data_left -= ib_sge->length;
  1542. ib_sge++;
  1543. pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
  1544. }
  1545. pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
  1546. send_wr->sg_list, send_wr->num_sge);
  1547. return sg_nents;
  1548. }
  1549. static int
  1550. isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1551. {
  1552. struct se_cmd *se_cmd = &cmd->se_cmd;
  1553. struct isert_cmd *isert_cmd = container_of(cmd,
  1554. struct isert_cmd, iscsi_cmd);
  1555. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1556. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1557. struct ib_send_wr *wr_failed, *send_wr;
  1558. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1559. struct ib_sge *ib_sge;
  1560. struct scatterlist *sg;
  1561. u32 offset = 0, data_len, data_left, rdma_write_max;
  1562. int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
  1563. pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
  1564. sg = &se_cmd->t_data_sg[0];
  1565. sg_nents = se_cmd->t_data_nents;
  1566. count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
  1567. if (unlikely(!count)) {
  1568. pr_err("Unable to map put_datain SGs\n");
  1569. return -EINVAL;
  1570. }
  1571. wr->sge = sg;
  1572. wr->num_sge = sg_nents;
  1573. pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
  1574. count, sg, sg_nents);
  1575. ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
  1576. if (!ib_sge) {
  1577. pr_warn("Unable to allocate datain ib_sge\n");
  1578. ret = -ENOMEM;
  1579. goto unmap_sg;
  1580. }
  1581. isert_cmd->ib_sge = ib_sge;
  1582. pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
  1583. ib_sge, se_cmd->t_data_nents);
  1584. wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
  1585. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  1586. GFP_KERNEL);
  1587. if (!wr->send_wr) {
  1588. pr_err("Unable to allocate wr->send_wr\n");
  1589. ret = -ENOMEM;
  1590. goto unmap_sg;
  1591. }
  1592. pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
  1593. wr->send_wr, wr->send_wr_num);
  1594. iscsit_increment_maxcmdsn(cmd, conn->sess);
  1595. cmd->stat_sn = conn->stat_sn++;
  1596. wr->isert_cmd = isert_cmd;
  1597. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  1598. data_left = se_cmd->data_length;
  1599. for (i = 0; i < wr->send_wr_num; i++) {
  1600. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  1601. data_len = min(data_left, rdma_write_max);
  1602. send_wr->opcode = IB_WR_RDMA_WRITE;
  1603. send_wr->send_flags = 0;
  1604. send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
  1605. send_wr->wr.rdma.rkey = isert_cmd->read_stag;
  1606. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  1607. send_wr, data_len, offset);
  1608. ib_sge += ib_sge_cnt;
  1609. if (i + 1 == wr->send_wr_num)
  1610. send_wr->next = &isert_cmd->tx_desc.send_wr;
  1611. else
  1612. send_wr->next = &wr->send_wr[i + 1];
  1613. offset += data_len;
  1614. data_left -= data_len;
  1615. }
  1616. /*
  1617. * Build isert_conn->tx_desc for iSCSI response PDU and attach
  1618. */
  1619. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1620. iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
  1621. &isert_cmd->tx_desc.iscsi_header);
  1622. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1623. isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
  1624. atomic_inc(&isert_conn->post_send_buf_count);
  1625. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  1626. if (rc) {
  1627. pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
  1628. atomic_dec(&isert_conn->post_send_buf_count);
  1629. }
  1630. pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
  1631. return 1;
  1632. unmap_sg:
  1633. ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
  1634. return ret;
  1635. }
  1636. static int
  1637. isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
  1638. {
  1639. struct se_cmd *se_cmd = &cmd->se_cmd;
  1640. struct isert_cmd *isert_cmd = container_of(cmd,
  1641. struct isert_cmd, iscsi_cmd);
  1642. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1643. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1644. struct ib_send_wr *wr_failed, *send_wr;
  1645. struct ib_sge *ib_sge;
  1646. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1647. struct scatterlist *sg_start;
  1648. u32 sg_off, sg_nents, page_off, va_offset = 0;
  1649. u32 offset = 0, data_len, data_left, rdma_write_max;
  1650. int rc, ret = 0, count, i, ib_sge_cnt;
  1651. pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
  1652. se_cmd->data_length, cmd->write_data_done);
  1653. sg_off = cmd->write_data_done / PAGE_SIZE;
  1654. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1655. page_off = cmd->write_data_done % PAGE_SIZE;
  1656. pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
  1657. sg_off, sg_start, page_off);
  1658. data_left = se_cmd->data_length - cmd->write_data_done;
  1659. sg_nents = se_cmd->t_data_nents - sg_off;
  1660. pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
  1661. data_left, sg_nents);
  1662. count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
  1663. if (unlikely(!count)) {
  1664. pr_err("Unable to map get_dataout SGs\n");
  1665. return -EINVAL;
  1666. }
  1667. wr->sge = sg_start;
  1668. wr->num_sge = sg_nents;
  1669. pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
  1670. count, sg_start, sg_nents);
  1671. ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
  1672. if (!ib_sge) {
  1673. pr_warn("Unable to allocate dataout ib_sge\n");
  1674. ret = -ENOMEM;
  1675. goto unmap_sg;
  1676. }
  1677. isert_cmd->ib_sge = ib_sge;
  1678. pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
  1679. ib_sge, sg_nents);
  1680. wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
  1681. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  1682. GFP_KERNEL);
  1683. if (!wr->send_wr) {
  1684. pr_debug("Unable to allocate wr->send_wr\n");
  1685. ret = -ENOMEM;
  1686. goto unmap_sg;
  1687. }
  1688. pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
  1689. wr->send_wr, wr->send_wr_num);
  1690. isert_cmd->tx_desc.isert_cmd = isert_cmd;
  1691. wr->iser_ib_op = ISER_IB_RDMA_READ;
  1692. wr->isert_cmd = isert_cmd;
  1693. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  1694. offset = cmd->write_data_done;
  1695. for (i = 0; i < wr->send_wr_num; i++) {
  1696. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  1697. data_len = min(data_left, rdma_write_max);
  1698. send_wr->opcode = IB_WR_RDMA_READ;
  1699. send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
  1700. send_wr->wr.rdma.rkey = isert_cmd->write_stag;
  1701. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  1702. send_wr, data_len, offset);
  1703. ib_sge += ib_sge_cnt;
  1704. if (i + 1 == wr->send_wr_num)
  1705. send_wr->send_flags = IB_SEND_SIGNALED;
  1706. else
  1707. send_wr->next = &wr->send_wr[i + 1];
  1708. offset += data_len;
  1709. va_offset += data_len;
  1710. data_left -= data_len;
  1711. }
  1712. atomic_inc(&isert_conn->post_send_buf_count);
  1713. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  1714. if (rc) {
  1715. pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
  1716. atomic_dec(&isert_conn->post_send_buf_count);
  1717. }
  1718. pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
  1719. return 0;
  1720. unmap_sg:
  1721. ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
  1722. return ret;
  1723. }
  1724. static int
  1725. isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  1726. {
  1727. int ret;
  1728. switch (state) {
  1729. case ISTATE_SEND_NOPIN_WANT_RESPONSE:
  1730. ret = isert_put_nopin(cmd, conn, false);
  1731. break;
  1732. default:
  1733. pr_err("Unknown immediate state: 0x%02x\n", state);
  1734. ret = -EINVAL;
  1735. break;
  1736. }
  1737. return ret;
  1738. }
  1739. static int
  1740. isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  1741. {
  1742. int ret;
  1743. switch (state) {
  1744. case ISTATE_SEND_LOGOUTRSP:
  1745. ret = isert_put_logout_rsp(cmd, conn);
  1746. if (!ret) {
  1747. pr_debug("Returning iSER Logout -EAGAIN\n");
  1748. ret = -EAGAIN;
  1749. }
  1750. break;
  1751. case ISTATE_SEND_NOPIN:
  1752. ret = isert_put_nopin(cmd, conn, true);
  1753. break;
  1754. case ISTATE_SEND_TASKMGTRSP:
  1755. ret = isert_put_tm_rsp(cmd, conn);
  1756. break;
  1757. case ISTATE_SEND_REJECT:
  1758. ret = isert_put_reject(cmd, conn);
  1759. break;
  1760. case ISTATE_SEND_TEXTRSP:
  1761. ret = isert_put_text_rsp(cmd, conn);
  1762. break;
  1763. case ISTATE_SEND_STATUS:
  1764. /*
  1765. * Special case for sending non GOOD SCSI status from TX thread
  1766. * context during pre se_cmd excecution failure.
  1767. */
  1768. ret = isert_put_response(conn, cmd);
  1769. break;
  1770. default:
  1771. pr_err("Unknown response state: 0x%02x\n", state);
  1772. ret = -EINVAL;
  1773. break;
  1774. }
  1775. return ret;
  1776. }
  1777. static int
  1778. isert_setup_np(struct iscsi_np *np,
  1779. struct __kernel_sockaddr_storage *ksockaddr)
  1780. {
  1781. struct isert_np *isert_np;
  1782. struct rdma_cm_id *isert_lid;
  1783. struct sockaddr *sa;
  1784. int ret;
  1785. isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
  1786. if (!isert_np) {
  1787. pr_err("Unable to allocate struct isert_np\n");
  1788. return -ENOMEM;
  1789. }
  1790. init_waitqueue_head(&isert_np->np_accept_wq);
  1791. mutex_init(&isert_np->np_accept_mutex);
  1792. INIT_LIST_HEAD(&isert_np->np_accept_list);
  1793. init_completion(&isert_np->np_login_comp);
  1794. sa = (struct sockaddr *)ksockaddr;
  1795. pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
  1796. /*
  1797. * Setup the np->np_sockaddr from the passed sockaddr setup
  1798. * in iscsi_target_configfs.c code..
  1799. */
  1800. memcpy(&np->np_sockaddr, ksockaddr,
  1801. sizeof(struct __kernel_sockaddr_storage));
  1802. isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
  1803. IB_QPT_RC);
  1804. if (IS_ERR(isert_lid)) {
  1805. pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
  1806. PTR_ERR(isert_lid));
  1807. ret = PTR_ERR(isert_lid);
  1808. goto out;
  1809. }
  1810. ret = rdma_bind_addr(isert_lid, sa);
  1811. if (ret) {
  1812. pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
  1813. goto out_lid;
  1814. }
  1815. ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
  1816. if (ret) {
  1817. pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
  1818. goto out_lid;
  1819. }
  1820. isert_np->np_cm_id = isert_lid;
  1821. np->np_context = isert_np;
  1822. pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
  1823. return 0;
  1824. out_lid:
  1825. rdma_destroy_id(isert_lid);
  1826. out:
  1827. kfree(isert_np);
  1828. return ret;
  1829. }
  1830. static int
  1831. isert_check_accept_queue(struct isert_np *isert_np)
  1832. {
  1833. int empty;
  1834. mutex_lock(&isert_np->np_accept_mutex);
  1835. empty = list_empty(&isert_np->np_accept_list);
  1836. mutex_unlock(&isert_np->np_accept_mutex);
  1837. return empty;
  1838. }
  1839. static int
  1840. isert_rdma_accept(struct isert_conn *isert_conn)
  1841. {
  1842. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  1843. struct rdma_conn_param cp;
  1844. int ret;
  1845. memset(&cp, 0, sizeof(struct rdma_conn_param));
  1846. cp.responder_resources = isert_conn->responder_resources;
  1847. cp.initiator_depth = isert_conn->initiator_depth;
  1848. cp.retry_count = 7;
  1849. cp.rnr_retry_count = 7;
  1850. pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
  1851. ret = rdma_accept(cm_id, &cp);
  1852. if (ret) {
  1853. pr_err("rdma_accept() failed with: %d\n", ret);
  1854. return ret;
  1855. }
  1856. pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
  1857. return 0;
  1858. }
  1859. static int
  1860. isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
  1861. {
  1862. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1863. int ret;
  1864. pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
  1865. ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
  1866. if (ret)
  1867. return ret;
  1868. pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
  1869. return 0;
  1870. }
  1871. static void
  1872. isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
  1873. struct isert_conn *isert_conn)
  1874. {
  1875. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  1876. struct rdma_route *cm_route = &cm_id->route;
  1877. struct sockaddr_in *sock_in;
  1878. struct sockaddr_in6 *sock_in6;
  1879. conn->login_family = np->np_sockaddr.ss_family;
  1880. if (np->np_sockaddr.ss_family == AF_INET6) {
  1881. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
  1882. snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
  1883. &sock_in6->sin6_addr.in6_u);
  1884. conn->login_port = ntohs(sock_in6->sin6_port);
  1885. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
  1886. snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
  1887. &sock_in6->sin6_addr.in6_u);
  1888. conn->local_port = ntohs(sock_in6->sin6_port);
  1889. } else {
  1890. sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
  1891. sprintf(conn->login_ip, "%pI4",
  1892. &sock_in->sin_addr.s_addr);
  1893. conn->login_port = ntohs(sock_in->sin_port);
  1894. sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
  1895. sprintf(conn->local_ip, "%pI4",
  1896. &sock_in->sin_addr.s_addr);
  1897. conn->local_port = ntohs(sock_in->sin_port);
  1898. }
  1899. }
  1900. static int
  1901. isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
  1902. {
  1903. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  1904. struct isert_conn *isert_conn;
  1905. int max_accept = 0, ret;
  1906. accept_wait:
  1907. ret = wait_event_interruptible(isert_np->np_accept_wq,
  1908. !isert_check_accept_queue(isert_np) ||
  1909. np->np_thread_state == ISCSI_NP_THREAD_RESET);
  1910. if (max_accept > 5)
  1911. return -ENODEV;
  1912. spin_lock_bh(&np->np_thread_lock);
  1913. if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
  1914. spin_unlock_bh(&np->np_thread_lock);
  1915. pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
  1916. return -ENODEV;
  1917. }
  1918. spin_unlock_bh(&np->np_thread_lock);
  1919. mutex_lock(&isert_np->np_accept_mutex);
  1920. if (list_empty(&isert_np->np_accept_list)) {
  1921. mutex_unlock(&isert_np->np_accept_mutex);
  1922. max_accept++;
  1923. goto accept_wait;
  1924. }
  1925. isert_conn = list_first_entry(&isert_np->np_accept_list,
  1926. struct isert_conn, conn_accept_node);
  1927. list_del_init(&isert_conn->conn_accept_node);
  1928. mutex_unlock(&isert_np->np_accept_mutex);
  1929. conn->context = isert_conn;
  1930. isert_conn->conn = conn;
  1931. max_accept = 0;
  1932. ret = isert_rdma_post_recvl(isert_conn);
  1933. if (ret)
  1934. return ret;
  1935. ret = isert_rdma_accept(isert_conn);
  1936. if (ret)
  1937. return ret;
  1938. isert_set_conn_info(np, conn, isert_conn);
  1939. pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
  1940. return 0;
  1941. }
  1942. static void
  1943. isert_free_np(struct iscsi_np *np)
  1944. {
  1945. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  1946. rdma_destroy_id(isert_np->np_cm_id);
  1947. np->np_context = NULL;
  1948. kfree(isert_np);
  1949. }
  1950. static int isert_check_state(struct isert_conn *isert_conn, int state)
  1951. {
  1952. int ret;
  1953. mutex_lock(&isert_conn->conn_mutex);
  1954. ret = (isert_conn->state == state);
  1955. mutex_unlock(&isert_conn->conn_mutex);
  1956. return ret;
  1957. }
  1958. static void isert_free_conn(struct iscsi_conn *conn)
  1959. {
  1960. struct isert_conn *isert_conn = conn->context;
  1961. pr_debug("isert_free_conn: Starting \n");
  1962. /*
  1963. * Decrement post_send_buf_count for special case when called
  1964. * from isert_do_control_comp() -> iscsit_logout_post_handler()
  1965. */
  1966. mutex_lock(&isert_conn->conn_mutex);
  1967. if (isert_conn->logout_posted)
  1968. atomic_dec(&isert_conn->post_send_buf_count);
  1969. if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
  1970. pr_debug("Calling rdma_disconnect from isert_free_conn\n");
  1971. rdma_disconnect(isert_conn->conn_cm_id);
  1972. }
  1973. /*
  1974. * Only wait for conn_wait_comp_err if the isert_conn made it
  1975. * into full feature phase..
  1976. */
  1977. if (isert_conn->state == ISER_CONN_UP) {
  1978. pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
  1979. isert_conn->state);
  1980. mutex_unlock(&isert_conn->conn_mutex);
  1981. wait_event(isert_conn->conn_wait_comp_err,
  1982. (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
  1983. wait_event(isert_conn->conn_wait,
  1984. (isert_check_state(isert_conn, ISER_CONN_DOWN)));
  1985. isert_put_conn(isert_conn);
  1986. return;
  1987. }
  1988. if (isert_conn->state == ISER_CONN_INIT) {
  1989. mutex_unlock(&isert_conn->conn_mutex);
  1990. isert_put_conn(isert_conn);
  1991. return;
  1992. }
  1993. pr_debug("isert_free_conn: wait_event conn_wait %d\n",
  1994. isert_conn->state);
  1995. mutex_unlock(&isert_conn->conn_mutex);
  1996. wait_event(isert_conn->conn_wait,
  1997. (isert_check_state(isert_conn, ISER_CONN_DOWN)));
  1998. isert_put_conn(isert_conn);
  1999. }
  2000. static struct iscsit_transport iser_target_transport = {
  2001. .name = "IB/iSER",
  2002. .transport_type = ISCSI_INFINIBAND,
  2003. .owner = THIS_MODULE,
  2004. .iscsit_setup_np = isert_setup_np,
  2005. .iscsit_accept_np = isert_accept_np,
  2006. .iscsit_free_np = isert_free_np,
  2007. .iscsit_free_conn = isert_free_conn,
  2008. .iscsit_alloc_cmd = isert_alloc_cmd,
  2009. .iscsit_get_login_rx = isert_get_login_rx,
  2010. .iscsit_put_login_tx = isert_put_login_tx,
  2011. .iscsit_immediate_queue = isert_immediate_queue,
  2012. .iscsit_response_queue = isert_response_queue,
  2013. .iscsit_get_dataout = isert_get_dataout,
  2014. .iscsit_queue_data_in = isert_put_datain,
  2015. .iscsit_queue_status = isert_put_response,
  2016. };
  2017. static int __init isert_init(void)
  2018. {
  2019. int ret;
  2020. isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
  2021. if (!isert_rx_wq) {
  2022. pr_err("Unable to allocate isert_rx_wq\n");
  2023. return -ENOMEM;
  2024. }
  2025. isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
  2026. if (!isert_comp_wq) {
  2027. pr_err("Unable to allocate isert_comp_wq\n");
  2028. ret = -ENOMEM;
  2029. goto destroy_rx_wq;
  2030. }
  2031. isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
  2032. sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
  2033. 0, NULL);
  2034. if (!isert_cmd_cache) {
  2035. pr_err("Unable to create isert_cmd_cache\n");
  2036. ret = -ENOMEM;
  2037. goto destroy_tx_cq;
  2038. }
  2039. iscsit_register_transport(&iser_target_transport);
  2040. pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
  2041. return 0;
  2042. destroy_tx_cq:
  2043. destroy_workqueue(isert_comp_wq);
  2044. destroy_rx_wq:
  2045. destroy_workqueue(isert_rx_wq);
  2046. return ret;
  2047. }
  2048. static void __exit isert_exit(void)
  2049. {
  2050. kmem_cache_destroy(isert_cmd_cache);
  2051. destroy_workqueue(isert_comp_wq);
  2052. destroy_workqueue(isert_rx_wq);
  2053. iscsit_unregister_transport(&iser_target_transport);
  2054. pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
  2055. }
  2056. MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
  2057. MODULE_VERSION("0.1");
  2058. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  2059. MODULE_LICENSE("GPL");
  2060. module_init(isert_init);
  2061. module_exit(isert_exit);