ib_isert.c 66 KB

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