ib_isert.c 77 KB

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