ib_isert.c 78 KB

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