ib_isert.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391
  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 <rdma/ib_verbs.h>
  25. #include <rdma/rdma_cm.h>
  26. #include <target/target_core_base.h>
  27. #include <target/target_core_fabric.h>
  28. #include <target/iscsi/iscsi_transport.h>
  29. #include <linux/semaphore.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. #define ISER_MAX_CQ_LEN (ISER_MAX_RX_CQ_LEN + ISER_MAX_TX_CQ_LEN + \
  36. ISERT_MAX_CONN)
  37. static int isert_debug_level;
  38. module_param_named(debug_level, isert_debug_level, int, 0644);
  39. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:0)");
  40. static DEFINE_MUTEX(device_list_mutex);
  41. static LIST_HEAD(device_list);
  42. static struct workqueue_struct *isert_comp_wq;
  43. static struct workqueue_struct *isert_release_wq;
  44. static void
  45. isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
  46. static int
  47. isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  48. struct isert_rdma_wr *wr);
  49. static void
  50. isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn);
  51. static int
  52. isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  53. struct isert_rdma_wr *wr);
  54. static int
  55. isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd);
  56. static int
  57. isert_rdma_post_recvl(struct isert_conn *isert_conn);
  58. static int
  59. isert_rdma_accept(struct isert_conn *isert_conn);
  60. struct rdma_cm_id *isert_setup_id(struct isert_np *isert_np);
  61. static inline bool
  62. isert_prot_cmd(struct isert_conn *conn, struct se_cmd *cmd)
  63. {
  64. return (conn->pi_support &&
  65. cmd->prot_op != TARGET_PROT_NORMAL);
  66. }
  67. static void
  68. isert_qp_event_callback(struct ib_event *e, void *context)
  69. {
  70. struct isert_conn *isert_conn = (struct isert_conn *)context;
  71. isert_err("conn %p event: %d\n", isert_conn, e->event);
  72. switch (e->event) {
  73. case IB_EVENT_COMM_EST:
  74. rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
  75. break;
  76. case IB_EVENT_QP_LAST_WQE_REACHED:
  77. isert_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED\n");
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. static int
  84. isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
  85. {
  86. int ret;
  87. ret = ib_query_device(ib_dev, devattr);
  88. if (ret) {
  89. isert_err("ib_query_device() failed: %d\n", ret);
  90. return ret;
  91. }
  92. isert_dbg("devattr->max_sge: %d\n", devattr->max_sge);
  93. isert_dbg("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
  94. return 0;
  95. }
  96. static int
  97. isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
  98. {
  99. struct isert_device *device = isert_conn->conn_device;
  100. struct ib_qp_init_attr attr;
  101. struct isert_comp *comp;
  102. int ret, i, min = 0;
  103. mutex_lock(&device_list_mutex);
  104. for (i = 0; i < device->comps_used; i++)
  105. if (device->comps[i].active_qps <
  106. device->comps[min].active_qps)
  107. min = i;
  108. comp = &device->comps[min];
  109. comp->active_qps++;
  110. isert_info("conn %p, using comp %p min_index: %d\n",
  111. isert_conn, comp, min);
  112. mutex_unlock(&device_list_mutex);
  113. memset(&attr, 0, sizeof(struct ib_qp_init_attr));
  114. attr.event_handler = isert_qp_event_callback;
  115. attr.qp_context = isert_conn;
  116. attr.send_cq = comp->cq;
  117. attr.recv_cq = comp->cq;
  118. attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
  119. attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
  120. /*
  121. * FIXME: Use devattr.max_sge - 2 for max_send_sge as
  122. * work-around for RDMA_READs with ConnectX-2.
  123. *
  124. * Also, still make sure to have at least two SGEs for
  125. * outgoing control PDU responses.
  126. */
  127. attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
  128. isert_conn->max_sge = attr.cap.max_send_sge;
  129. attr.cap.max_recv_sge = 1;
  130. attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  131. attr.qp_type = IB_QPT_RC;
  132. if (device->pi_capable)
  133. attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
  134. ret = rdma_create_qp(cma_id, device->pd, &attr);
  135. if (ret) {
  136. isert_err("rdma_create_qp failed for cma_id %d\n", ret);
  137. goto err;
  138. }
  139. isert_conn->conn_qp = cma_id->qp;
  140. return 0;
  141. err:
  142. mutex_lock(&device_list_mutex);
  143. comp->active_qps--;
  144. mutex_unlock(&device_list_mutex);
  145. return ret;
  146. }
  147. static void
  148. isert_cq_event_callback(struct ib_event *e, void *context)
  149. {
  150. isert_dbg("event: %d\n", e->event);
  151. }
  152. static int
  153. isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
  154. {
  155. struct isert_device *device = isert_conn->conn_device;
  156. struct ib_device *ib_dev = device->ib_device;
  157. struct iser_rx_desc *rx_desc;
  158. struct ib_sge *rx_sg;
  159. u64 dma_addr;
  160. int i, j;
  161. isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
  162. sizeof(struct iser_rx_desc), GFP_KERNEL);
  163. if (!isert_conn->conn_rx_descs)
  164. goto fail;
  165. rx_desc = isert_conn->conn_rx_descs;
  166. for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
  167. dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
  168. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  169. if (ib_dma_mapping_error(ib_dev, dma_addr))
  170. goto dma_map_fail;
  171. rx_desc->dma_addr = dma_addr;
  172. rx_sg = &rx_desc->rx_sg;
  173. rx_sg->addr = rx_desc->dma_addr;
  174. rx_sg->length = ISER_RX_PAYLOAD_SIZE;
  175. rx_sg->lkey = device->mr->lkey;
  176. }
  177. isert_conn->conn_rx_desc_head = 0;
  178. return 0;
  179. dma_map_fail:
  180. rx_desc = isert_conn->conn_rx_descs;
  181. for (j = 0; j < i; j++, rx_desc++) {
  182. ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
  183. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  184. }
  185. kfree(isert_conn->conn_rx_descs);
  186. isert_conn->conn_rx_descs = NULL;
  187. fail:
  188. isert_err("conn %p failed to allocate rx descriptors\n", isert_conn);
  189. return -ENOMEM;
  190. }
  191. static void
  192. isert_free_rx_descriptors(struct isert_conn *isert_conn)
  193. {
  194. struct ib_device *ib_dev = isert_conn->conn_device->ib_device;
  195. struct iser_rx_desc *rx_desc;
  196. int i;
  197. if (!isert_conn->conn_rx_descs)
  198. return;
  199. rx_desc = isert_conn->conn_rx_descs;
  200. for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
  201. ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
  202. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  203. }
  204. kfree(isert_conn->conn_rx_descs);
  205. isert_conn->conn_rx_descs = NULL;
  206. }
  207. static void isert_cq_work(struct work_struct *);
  208. static void isert_cq_callback(struct ib_cq *, void *);
  209. static int
  210. isert_create_device_ib_res(struct isert_device *device)
  211. {
  212. struct ib_device *ib_dev = device->ib_device;
  213. struct ib_device_attr *dev_attr;
  214. int ret = 0, i;
  215. int max_cqe;
  216. dev_attr = &device->dev_attr;
  217. ret = isert_query_device(ib_dev, dev_attr);
  218. if (ret)
  219. return ret;
  220. max_cqe = min(ISER_MAX_CQ_LEN, dev_attr->max_cqe);
  221. /* asign function handlers */
  222. if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
  223. dev_attr->device_cap_flags & IB_DEVICE_SIGNATURE_HANDOVER) {
  224. device->use_fastreg = 1;
  225. device->reg_rdma_mem = isert_reg_rdma;
  226. device->unreg_rdma_mem = isert_unreg_rdma;
  227. } else {
  228. device->use_fastreg = 0;
  229. device->reg_rdma_mem = isert_map_rdma;
  230. device->unreg_rdma_mem = isert_unmap_cmd;
  231. }
  232. /* Check signature cap */
  233. device->pi_capable = dev_attr->device_cap_flags &
  234. IB_DEVICE_SIGNATURE_HANDOVER ? true : false;
  235. device->comps_used = min(ISERT_MAX_CQ, min_t(int, num_online_cpus(),
  236. device->ib_device->num_comp_vectors));
  237. isert_info("Using %d CQs, %s supports %d vectors support "
  238. "Fast registration %d pi_capable %d\n",
  239. device->comps_used, device->ib_device->name,
  240. device->ib_device->num_comp_vectors, device->use_fastreg,
  241. device->pi_capable);
  242. device->comps = kcalloc(device->comps_used, sizeof(struct isert_comp),
  243. GFP_KERNEL);
  244. if (!device->comps) {
  245. isert_err("Unable to allocate completion contexts\n");
  246. return -ENOMEM;
  247. }
  248. for (i = 0; i < device->comps_used; i++) {
  249. struct isert_comp *comp = &device->comps[i];
  250. comp->device = device;
  251. INIT_WORK(&comp->work, isert_cq_work);
  252. comp->cq = ib_create_cq(device->ib_device,
  253. isert_cq_callback,
  254. isert_cq_event_callback,
  255. (void *)comp,
  256. max_cqe, i);
  257. if (IS_ERR(comp->cq)) {
  258. ret = PTR_ERR(comp->cq);
  259. comp->cq = NULL;
  260. goto out_cq;
  261. }
  262. ret = ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
  263. if (ret)
  264. goto out_cq;
  265. }
  266. device->pd = ib_alloc_pd(device->ib_device);
  267. if (IS_ERR(device->pd)) {
  268. ret = PTR_ERR(device->pd);
  269. isert_err("failed to allocate pd, device %p, ret=%d\n",
  270. device, ret);
  271. goto out_cq;
  272. }
  273. device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE);
  274. if (IS_ERR(device->mr)) {
  275. ret = PTR_ERR(device->mr);
  276. isert_err("failed to create dma mr, device %p, ret=%d\n",
  277. device, ret);
  278. goto out_mr;
  279. }
  280. return 0;
  281. out_mr:
  282. ib_dealloc_pd(device->pd);
  283. out_cq:
  284. for (i = 0; i < device->comps_used; i++) {
  285. struct isert_comp *comp = &device->comps[i];
  286. if (comp->cq) {
  287. cancel_work_sync(&comp->work);
  288. ib_destroy_cq(comp->cq);
  289. }
  290. }
  291. kfree(device->comps);
  292. return ret;
  293. }
  294. static void
  295. isert_free_device_ib_res(struct isert_device *device)
  296. {
  297. int i;
  298. isert_info("device %p\n", device);
  299. ib_dereg_mr(device->mr);
  300. ib_dealloc_pd(device->pd);
  301. for (i = 0; i < device->comps_used; i++) {
  302. struct isert_comp *comp = &device->comps[i];
  303. cancel_work_sync(&comp->work);
  304. ib_destroy_cq(comp->cq);
  305. comp->cq = NULL;
  306. }
  307. kfree(device->comps);
  308. }
  309. static void
  310. isert_device_try_release(struct isert_device *device)
  311. {
  312. mutex_lock(&device_list_mutex);
  313. device->refcount--;
  314. isert_info("device %p refcount %d\n", device, device->refcount);
  315. if (!device->refcount) {
  316. isert_free_device_ib_res(device);
  317. list_del(&device->dev_node);
  318. kfree(device);
  319. }
  320. mutex_unlock(&device_list_mutex);
  321. }
  322. static struct isert_device *
  323. isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
  324. {
  325. struct isert_device *device;
  326. int ret;
  327. mutex_lock(&device_list_mutex);
  328. list_for_each_entry(device, &device_list, dev_node) {
  329. if (device->ib_device->node_guid == cma_id->device->node_guid) {
  330. device->refcount++;
  331. isert_info("Found iser device %p refcount %d\n",
  332. device, device->refcount);
  333. mutex_unlock(&device_list_mutex);
  334. return device;
  335. }
  336. }
  337. device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
  338. if (!device) {
  339. mutex_unlock(&device_list_mutex);
  340. return ERR_PTR(-ENOMEM);
  341. }
  342. INIT_LIST_HEAD(&device->dev_node);
  343. device->ib_device = cma_id->device;
  344. ret = isert_create_device_ib_res(device);
  345. if (ret) {
  346. kfree(device);
  347. mutex_unlock(&device_list_mutex);
  348. return ERR_PTR(ret);
  349. }
  350. device->refcount++;
  351. list_add_tail(&device->dev_node, &device_list);
  352. isert_info("Created a new iser device %p refcount %d\n",
  353. device, device->refcount);
  354. mutex_unlock(&device_list_mutex);
  355. return device;
  356. }
  357. static void
  358. isert_conn_free_fastreg_pool(struct isert_conn *isert_conn)
  359. {
  360. struct fast_reg_descriptor *fr_desc, *tmp;
  361. int i = 0;
  362. if (list_empty(&isert_conn->conn_fr_pool))
  363. return;
  364. isert_info("Freeing conn %p fastreg pool", isert_conn);
  365. list_for_each_entry_safe(fr_desc, tmp,
  366. &isert_conn->conn_fr_pool, list) {
  367. list_del(&fr_desc->list);
  368. ib_free_fast_reg_page_list(fr_desc->data_frpl);
  369. ib_dereg_mr(fr_desc->data_mr);
  370. if (fr_desc->pi_ctx) {
  371. ib_free_fast_reg_page_list(fr_desc->pi_ctx->prot_frpl);
  372. ib_dereg_mr(fr_desc->pi_ctx->prot_mr);
  373. ib_destroy_mr(fr_desc->pi_ctx->sig_mr);
  374. kfree(fr_desc->pi_ctx);
  375. }
  376. kfree(fr_desc);
  377. ++i;
  378. }
  379. if (i < isert_conn->conn_fr_pool_size)
  380. isert_warn("Pool still has %d regions registered\n",
  381. isert_conn->conn_fr_pool_size - i);
  382. }
  383. static int
  384. isert_create_pi_ctx(struct fast_reg_descriptor *desc,
  385. struct ib_device *device,
  386. struct ib_pd *pd)
  387. {
  388. struct ib_mr_init_attr mr_init_attr;
  389. struct pi_context *pi_ctx;
  390. int ret;
  391. pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
  392. if (!pi_ctx) {
  393. isert_err("Failed to allocate pi context\n");
  394. return -ENOMEM;
  395. }
  396. pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(device,
  397. ISCSI_ISER_SG_TABLESIZE);
  398. if (IS_ERR(pi_ctx->prot_frpl)) {
  399. isert_err("Failed to allocate prot frpl err=%ld\n",
  400. PTR_ERR(pi_ctx->prot_frpl));
  401. ret = PTR_ERR(pi_ctx->prot_frpl);
  402. goto err_pi_ctx;
  403. }
  404. pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
  405. if (IS_ERR(pi_ctx->prot_mr)) {
  406. isert_err("Failed to allocate prot frmr err=%ld\n",
  407. PTR_ERR(pi_ctx->prot_mr));
  408. ret = PTR_ERR(pi_ctx->prot_mr);
  409. goto err_prot_frpl;
  410. }
  411. desc->ind |= ISERT_PROT_KEY_VALID;
  412. memset(&mr_init_attr, 0, sizeof(mr_init_attr));
  413. mr_init_attr.max_reg_descriptors = 2;
  414. mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
  415. pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
  416. if (IS_ERR(pi_ctx->sig_mr)) {
  417. isert_err("Failed to allocate signature enabled mr err=%ld\n",
  418. PTR_ERR(pi_ctx->sig_mr));
  419. ret = PTR_ERR(pi_ctx->sig_mr);
  420. goto err_prot_mr;
  421. }
  422. desc->pi_ctx = pi_ctx;
  423. desc->ind |= ISERT_SIG_KEY_VALID;
  424. desc->ind &= ~ISERT_PROTECTED;
  425. return 0;
  426. err_prot_mr:
  427. ib_dereg_mr(desc->pi_ctx->prot_mr);
  428. err_prot_frpl:
  429. ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
  430. err_pi_ctx:
  431. kfree(desc->pi_ctx);
  432. return ret;
  433. }
  434. static int
  435. isert_create_fr_desc(struct ib_device *ib_device, struct ib_pd *pd,
  436. struct fast_reg_descriptor *fr_desc)
  437. {
  438. int ret;
  439. fr_desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
  440. ISCSI_ISER_SG_TABLESIZE);
  441. if (IS_ERR(fr_desc->data_frpl)) {
  442. isert_err("Failed to allocate data frpl err=%ld\n",
  443. PTR_ERR(fr_desc->data_frpl));
  444. return PTR_ERR(fr_desc->data_frpl);
  445. }
  446. fr_desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE);
  447. if (IS_ERR(fr_desc->data_mr)) {
  448. isert_err("Failed to allocate data frmr err=%ld\n",
  449. PTR_ERR(fr_desc->data_mr));
  450. ret = PTR_ERR(fr_desc->data_mr);
  451. goto err_data_frpl;
  452. }
  453. fr_desc->ind |= ISERT_DATA_KEY_VALID;
  454. isert_dbg("Created fr_desc %p\n", fr_desc);
  455. return 0;
  456. err_data_frpl:
  457. ib_free_fast_reg_page_list(fr_desc->data_frpl);
  458. return ret;
  459. }
  460. static int
  461. isert_conn_create_fastreg_pool(struct isert_conn *isert_conn)
  462. {
  463. struct fast_reg_descriptor *fr_desc;
  464. struct isert_device *device = isert_conn->conn_device;
  465. struct se_session *se_sess = isert_conn->conn->sess->se_sess;
  466. struct se_node_acl *se_nacl = se_sess->se_node_acl;
  467. int i, ret, tag_num;
  468. /*
  469. * Setup the number of FRMRs based upon the number of tags
  470. * available to session in iscsi_target_locate_portal().
  471. */
  472. tag_num = max_t(u32, ISCSIT_MIN_TAGS, se_nacl->queue_depth);
  473. tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
  474. isert_conn->conn_fr_pool_size = 0;
  475. for (i = 0; i < tag_num; i++) {
  476. fr_desc = kzalloc(sizeof(*fr_desc), GFP_KERNEL);
  477. if (!fr_desc) {
  478. isert_err("Failed to allocate fast_reg descriptor\n");
  479. ret = -ENOMEM;
  480. goto err;
  481. }
  482. ret = isert_create_fr_desc(device->ib_device,
  483. device->pd, fr_desc);
  484. if (ret) {
  485. isert_err("Failed to create fastreg descriptor err=%d\n",
  486. ret);
  487. kfree(fr_desc);
  488. goto err;
  489. }
  490. list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
  491. isert_conn->conn_fr_pool_size++;
  492. }
  493. isert_dbg("Creating conn %p fastreg pool size=%d",
  494. isert_conn, isert_conn->conn_fr_pool_size);
  495. return 0;
  496. err:
  497. isert_conn_free_fastreg_pool(isert_conn);
  498. return ret;
  499. }
  500. static int
  501. isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  502. {
  503. struct isert_np *isert_np = cma_id->context;
  504. struct iscsi_np *np = isert_np->np;
  505. struct isert_conn *isert_conn;
  506. struct isert_device *device;
  507. struct ib_device *ib_dev = cma_id->device;
  508. int ret = 0;
  509. spin_lock_bh(&np->np_thread_lock);
  510. if (!np->enabled) {
  511. spin_unlock_bh(&np->np_thread_lock);
  512. isert_dbg("iscsi_np is not enabled, reject connect request\n");
  513. return rdma_reject(cma_id, NULL, 0);
  514. }
  515. spin_unlock_bh(&np->np_thread_lock);
  516. isert_dbg("cma_id: %p, portal: %p\n",
  517. cma_id, cma_id->context);
  518. isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
  519. if (!isert_conn) {
  520. isert_err("Unable to allocate isert_conn\n");
  521. return -ENOMEM;
  522. }
  523. isert_conn->state = ISER_CONN_INIT;
  524. INIT_LIST_HEAD(&isert_conn->conn_accept_node);
  525. init_completion(&isert_conn->conn_login_comp);
  526. init_completion(&isert_conn->login_req_comp);
  527. init_completion(&isert_conn->conn_wait);
  528. kref_init(&isert_conn->conn_kref);
  529. mutex_init(&isert_conn->conn_mutex);
  530. spin_lock_init(&isert_conn->conn_lock);
  531. INIT_LIST_HEAD(&isert_conn->conn_fr_pool);
  532. isert_conn->conn_cm_id = cma_id;
  533. isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
  534. ISER_RX_LOGIN_SIZE, GFP_KERNEL);
  535. if (!isert_conn->login_buf) {
  536. isert_err("Unable to allocate isert_conn->login_buf\n");
  537. ret = -ENOMEM;
  538. goto out;
  539. }
  540. isert_conn->login_req_buf = isert_conn->login_buf;
  541. isert_conn->login_rsp_buf = isert_conn->login_buf +
  542. ISCSI_DEF_MAX_RECV_SEG_LEN;
  543. isert_dbg("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
  544. isert_conn->login_buf, isert_conn->login_req_buf,
  545. isert_conn->login_rsp_buf);
  546. isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
  547. (void *)isert_conn->login_req_buf,
  548. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  549. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
  550. if (ret) {
  551. isert_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
  552. ret);
  553. isert_conn->login_req_dma = 0;
  554. goto out_login_buf;
  555. }
  556. isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
  557. (void *)isert_conn->login_rsp_buf,
  558. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  559. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
  560. if (ret) {
  561. isert_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
  562. ret);
  563. isert_conn->login_rsp_dma = 0;
  564. goto out_req_dma_map;
  565. }
  566. device = isert_device_find_by_ib_dev(cma_id);
  567. if (IS_ERR(device)) {
  568. ret = PTR_ERR(device);
  569. goto out_rsp_dma_map;
  570. }
  571. /* Set max inflight RDMA READ requests */
  572. isert_conn->initiator_depth = min_t(u8,
  573. event->param.conn.initiator_depth,
  574. device->dev_attr.max_qp_init_rd_atom);
  575. isert_dbg("Using initiator_depth: %u\n", isert_conn->initiator_depth);
  576. isert_conn->conn_device = device;
  577. ret = isert_conn_setup_qp(isert_conn, cma_id);
  578. if (ret)
  579. goto out_conn_dev;
  580. ret = isert_rdma_post_recvl(isert_conn);
  581. if (ret)
  582. goto out_conn_dev;
  583. ret = isert_rdma_accept(isert_conn);
  584. if (ret)
  585. goto out_conn_dev;
  586. mutex_lock(&isert_np->np_accept_mutex);
  587. list_add_tail(&isert_conn->conn_accept_node, &isert_np->np_accept_list);
  588. mutex_unlock(&isert_np->np_accept_mutex);
  589. isert_info("np %p: Allow accept_np to continue\n", np);
  590. up(&isert_np->np_sem);
  591. return 0;
  592. out_conn_dev:
  593. isert_device_try_release(device);
  594. out_rsp_dma_map:
  595. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  596. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  597. out_req_dma_map:
  598. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  599. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  600. out_login_buf:
  601. kfree(isert_conn->login_buf);
  602. out:
  603. kfree(isert_conn);
  604. rdma_reject(cma_id, NULL, 0);
  605. return ret;
  606. }
  607. static void
  608. isert_connect_release(struct isert_conn *isert_conn)
  609. {
  610. struct isert_device *device = isert_conn->conn_device;
  611. struct ib_device *ib_dev = device->ib_device;
  612. isert_dbg("conn %p\n", isert_conn);
  613. if (device && device->use_fastreg)
  614. isert_conn_free_fastreg_pool(isert_conn);
  615. isert_free_rx_descriptors(isert_conn);
  616. if (isert_conn->conn_cm_id)
  617. rdma_destroy_id(isert_conn->conn_cm_id);
  618. if (isert_conn->conn_qp) {
  619. struct isert_comp *comp = isert_conn->conn_qp->recv_cq->cq_context;
  620. isert_dbg("dec completion context %p active_qps\n", comp);
  621. mutex_lock(&device_list_mutex);
  622. comp->active_qps--;
  623. mutex_unlock(&device_list_mutex);
  624. ib_destroy_qp(isert_conn->conn_qp);
  625. }
  626. if (isert_conn->login_buf) {
  627. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  628. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  629. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  630. ISCSI_DEF_MAX_RECV_SEG_LEN,
  631. DMA_FROM_DEVICE);
  632. kfree(isert_conn->login_buf);
  633. }
  634. kfree(isert_conn);
  635. if (device)
  636. isert_device_try_release(device);
  637. }
  638. static void
  639. isert_connected_handler(struct rdma_cm_id *cma_id)
  640. {
  641. struct isert_conn *isert_conn = cma_id->qp->qp_context;
  642. isert_info("conn %p\n", isert_conn);
  643. if (!kref_get_unless_zero(&isert_conn->conn_kref)) {
  644. isert_warn("conn %p connect_release is running\n", isert_conn);
  645. return;
  646. }
  647. mutex_lock(&isert_conn->conn_mutex);
  648. if (isert_conn->state != ISER_CONN_FULL_FEATURE)
  649. isert_conn->state = ISER_CONN_UP;
  650. mutex_unlock(&isert_conn->conn_mutex);
  651. }
  652. static void
  653. isert_release_conn_kref(struct kref *kref)
  654. {
  655. struct isert_conn *isert_conn = container_of(kref,
  656. struct isert_conn, conn_kref);
  657. isert_info("conn %p final kref %s/%d\n", isert_conn, current->comm,
  658. current->pid);
  659. isert_connect_release(isert_conn);
  660. }
  661. static void
  662. isert_put_conn(struct isert_conn *isert_conn)
  663. {
  664. kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
  665. }
  666. /**
  667. * isert_conn_terminate() - Initiate connection termination
  668. * @isert_conn: isert connection struct
  669. *
  670. * Notes:
  671. * In case the connection state is FULL_FEATURE, move state
  672. * to TEMINATING and start teardown sequence (rdma_disconnect).
  673. * In case the connection state is UP, complete flush as well.
  674. *
  675. * This routine must be called with conn_mutex held. Thus it is
  676. * safe to call multiple times.
  677. */
  678. static void
  679. isert_conn_terminate(struct isert_conn *isert_conn)
  680. {
  681. int err;
  682. switch (isert_conn->state) {
  683. case ISER_CONN_TERMINATING:
  684. break;
  685. case ISER_CONN_UP:
  686. case ISER_CONN_FULL_FEATURE: /* FALLTHRU */
  687. isert_info("Terminating conn %p state %d\n",
  688. isert_conn, isert_conn->state);
  689. isert_conn->state = ISER_CONN_TERMINATING;
  690. err = rdma_disconnect(isert_conn->conn_cm_id);
  691. if (err)
  692. isert_warn("Failed rdma_disconnect isert_conn %p\n",
  693. isert_conn);
  694. break;
  695. default:
  696. isert_warn("conn %p teminating in state %d\n",
  697. isert_conn, isert_conn->state);
  698. }
  699. }
  700. static int
  701. isert_np_cma_handler(struct isert_np *isert_np,
  702. enum rdma_cm_event_type event)
  703. {
  704. isert_dbg("isert np %p, handling event %d\n", isert_np, event);
  705. switch (event) {
  706. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  707. isert_np->np_cm_id = NULL;
  708. break;
  709. case RDMA_CM_EVENT_ADDR_CHANGE:
  710. isert_np->np_cm_id = isert_setup_id(isert_np);
  711. if (IS_ERR(isert_np->np_cm_id)) {
  712. isert_err("isert np %p setup id failed: %ld\n",
  713. isert_np, PTR_ERR(isert_np->np_cm_id));
  714. isert_np->np_cm_id = NULL;
  715. }
  716. break;
  717. default:
  718. isert_err("isert np %p Unexpected event %d\n",
  719. isert_np, event);
  720. }
  721. return -1;
  722. }
  723. static int
  724. isert_disconnected_handler(struct rdma_cm_id *cma_id,
  725. enum rdma_cm_event_type event)
  726. {
  727. struct isert_np *isert_np = cma_id->context;
  728. struct isert_conn *isert_conn;
  729. if (isert_np->np_cm_id == cma_id)
  730. return isert_np_cma_handler(cma_id->context, event);
  731. isert_conn = cma_id->qp->qp_context;
  732. mutex_lock(&isert_conn->conn_mutex);
  733. isert_conn_terminate(isert_conn);
  734. mutex_unlock(&isert_conn->conn_mutex);
  735. isert_info("conn %p completing conn_wait\n", isert_conn);
  736. complete(&isert_conn->conn_wait);
  737. return 0;
  738. }
  739. static int
  740. isert_connect_error(struct rdma_cm_id *cma_id)
  741. {
  742. struct isert_conn *isert_conn = cma_id->qp->qp_context;
  743. isert_conn->conn_cm_id = NULL;
  744. isert_put_conn(isert_conn);
  745. return -1;
  746. }
  747. static int
  748. isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  749. {
  750. int ret = 0;
  751. isert_info("event %d status %d id %p np %p\n", event->event,
  752. event->status, cma_id, cma_id->context);
  753. switch (event->event) {
  754. case RDMA_CM_EVENT_CONNECT_REQUEST:
  755. ret = isert_connect_request(cma_id, event);
  756. if (ret)
  757. isert_err("failed handle connect request %d\n", ret);
  758. break;
  759. case RDMA_CM_EVENT_ESTABLISHED:
  760. isert_connected_handler(cma_id);
  761. break;
  762. case RDMA_CM_EVENT_ADDR_CHANGE: /* FALLTHRU */
  763. case RDMA_CM_EVENT_DISCONNECTED: /* FALLTHRU */
  764. case RDMA_CM_EVENT_DEVICE_REMOVAL: /* FALLTHRU */
  765. case RDMA_CM_EVENT_TIMEWAIT_EXIT: /* FALLTHRU */
  766. ret = isert_disconnected_handler(cma_id, event->event);
  767. break;
  768. case RDMA_CM_EVENT_REJECTED: /* FALLTHRU */
  769. case RDMA_CM_EVENT_UNREACHABLE: /* FALLTHRU */
  770. case RDMA_CM_EVENT_CONNECT_ERROR:
  771. ret = isert_connect_error(cma_id);
  772. break;
  773. default:
  774. isert_err("Unhandled RDMA CMA event: %d\n", event->event);
  775. break;
  776. }
  777. return ret;
  778. }
  779. static int
  780. isert_post_recv(struct isert_conn *isert_conn, u32 count)
  781. {
  782. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  783. int i, ret;
  784. unsigned int rx_head = isert_conn->conn_rx_desc_head;
  785. struct iser_rx_desc *rx_desc;
  786. for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
  787. rx_desc = &isert_conn->conn_rx_descs[rx_head];
  788. rx_wr->wr_id = (uintptr_t)rx_desc;
  789. rx_wr->sg_list = &rx_desc->rx_sg;
  790. rx_wr->num_sge = 1;
  791. rx_wr->next = rx_wr + 1;
  792. rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
  793. }
  794. rx_wr--;
  795. rx_wr->next = NULL; /* mark end of work requests list */
  796. isert_conn->post_recv_buf_count += count;
  797. ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
  798. &rx_wr_failed);
  799. if (ret) {
  800. isert_err("ib_post_recv() failed with ret: %d\n", ret);
  801. isert_conn->post_recv_buf_count -= count;
  802. } else {
  803. isert_dbg("Posted %d RX buffers\n", count);
  804. isert_conn->conn_rx_desc_head = rx_head;
  805. }
  806. return ret;
  807. }
  808. static int
  809. isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
  810. {
  811. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  812. struct ib_send_wr send_wr, *send_wr_failed;
  813. int ret;
  814. ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
  815. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  816. send_wr.next = NULL;
  817. send_wr.wr_id = (uintptr_t)tx_desc;
  818. send_wr.sg_list = tx_desc->tx_sg;
  819. send_wr.num_sge = tx_desc->num_sge;
  820. send_wr.opcode = IB_WR_SEND;
  821. send_wr.send_flags = IB_SEND_SIGNALED;
  822. ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
  823. if (ret)
  824. isert_err("ib_post_send() failed, ret: %d\n", ret);
  825. return ret;
  826. }
  827. static void
  828. isert_create_send_desc(struct isert_conn *isert_conn,
  829. struct isert_cmd *isert_cmd,
  830. struct iser_tx_desc *tx_desc)
  831. {
  832. struct isert_device *device = isert_conn->conn_device;
  833. struct ib_device *ib_dev = device->ib_device;
  834. ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
  835. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  836. memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
  837. tx_desc->iser_header.flags = ISER_VER;
  838. tx_desc->num_sge = 1;
  839. tx_desc->isert_cmd = isert_cmd;
  840. if (tx_desc->tx_sg[0].lkey != device->mr->lkey) {
  841. tx_desc->tx_sg[0].lkey = device->mr->lkey;
  842. isert_dbg("tx_desc %p lkey mismatch, fixing\n", tx_desc);
  843. }
  844. }
  845. static int
  846. isert_init_tx_hdrs(struct isert_conn *isert_conn,
  847. struct iser_tx_desc *tx_desc)
  848. {
  849. struct isert_device *device = isert_conn->conn_device;
  850. struct ib_device *ib_dev = device->ib_device;
  851. u64 dma_addr;
  852. dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
  853. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  854. if (ib_dma_mapping_error(ib_dev, dma_addr)) {
  855. isert_err("ib_dma_mapping_error() failed\n");
  856. return -ENOMEM;
  857. }
  858. tx_desc->dma_addr = dma_addr;
  859. tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
  860. tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
  861. tx_desc->tx_sg[0].lkey = device->mr->lkey;
  862. isert_dbg("Setup tx_sg[0].addr: 0x%llx length: %u lkey: 0x%x\n",
  863. tx_desc->tx_sg[0].addr, tx_desc->tx_sg[0].length,
  864. tx_desc->tx_sg[0].lkey);
  865. return 0;
  866. }
  867. static void
  868. isert_init_send_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  869. struct ib_send_wr *send_wr)
  870. {
  871. struct iser_tx_desc *tx_desc = &isert_cmd->tx_desc;
  872. isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
  873. send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
  874. send_wr->opcode = IB_WR_SEND;
  875. send_wr->sg_list = &tx_desc->tx_sg[0];
  876. send_wr->num_sge = isert_cmd->tx_desc.num_sge;
  877. send_wr->send_flags = IB_SEND_SIGNALED;
  878. }
  879. static int
  880. isert_rdma_post_recvl(struct isert_conn *isert_conn)
  881. {
  882. struct ib_recv_wr rx_wr, *rx_wr_fail;
  883. struct ib_sge sge;
  884. int ret;
  885. memset(&sge, 0, sizeof(struct ib_sge));
  886. sge.addr = isert_conn->login_req_dma;
  887. sge.length = ISER_RX_LOGIN_SIZE;
  888. sge.lkey = isert_conn->conn_device->mr->lkey;
  889. isert_dbg("Setup sge: addr: %llx length: %d 0x%08x\n",
  890. sge.addr, sge.length, sge.lkey);
  891. memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
  892. rx_wr.wr_id = (uintptr_t)isert_conn->login_req_buf;
  893. rx_wr.sg_list = &sge;
  894. rx_wr.num_sge = 1;
  895. isert_conn->post_recv_buf_count++;
  896. ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
  897. if (ret) {
  898. isert_err("ib_post_recv() failed: %d\n", ret);
  899. isert_conn->post_recv_buf_count--;
  900. }
  901. return ret;
  902. }
  903. static int
  904. isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
  905. u32 length)
  906. {
  907. struct isert_conn *isert_conn = conn->context;
  908. struct isert_device *device = isert_conn->conn_device;
  909. struct ib_device *ib_dev = device->ib_device;
  910. struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
  911. int ret;
  912. isert_create_send_desc(isert_conn, NULL, tx_desc);
  913. memcpy(&tx_desc->iscsi_header, &login->rsp[0],
  914. sizeof(struct iscsi_hdr));
  915. isert_init_tx_hdrs(isert_conn, tx_desc);
  916. if (length > 0) {
  917. struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
  918. ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
  919. length, DMA_TO_DEVICE);
  920. memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
  921. ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
  922. length, DMA_TO_DEVICE);
  923. tx_dsg->addr = isert_conn->login_rsp_dma;
  924. tx_dsg->length = length;
  925. tx_dsg->lkey = isert_conn->conn_device->mr->lkey;
  926. tx_desc->num_sge = 2;
  927. }
  928. if (!login->login_failed) {
  929. if (login->login_complete) {
  930. if (!conn->sess->sess_ops->SessionType &&
  931. isert_conn->conn_device->use_fastreg) {
  932. ret = isert_conn_create_fastreg_pool(isert_conn);
  933. if (ret) {
  934. isert_err("Conn: %p failed to create"
  935. " fastreg pool\n", isert_conn);
  936. return ret;
  937. }
  938. }
  939. ret = isert_alloc_rx_descriptors(isert_conn);
  940. if (ret)
  941. return ret;
  942. ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
  943. if (ret)
  944. return ret;
  945. /* Now we are in FULL_FEATURE phase */
  946. mutex_lock(&isert_conn->conn_mutex);
  947. isert_conn->state = ISER_CONN_FULL_FEATURE;
  948. mutex_unlock(&isert_conn->conn_mutex);
  949. goto post_send;
  950. }
  951. ret = isert_rdma_post_recvl(isert_conn);
  952. if (ret)
  953. return ret;
  954. }
  955. post_send:
  956. ret = isert_post_send(isert_conn, tx_desc);
  957. if (ret)
  958. return ret;
  959. return 0;
  960. }
  961. static void
  962. isert_rx_login_req(struct isert_conn *isert_conn)
  963. {
  964. struct iser_rx_desc *rx_desc = (void *)isert_conn->login_req_buf;
  965. int rx_buflen = isert_conn->login_req_len;
  966. struct iscsi_conn *conn = isert_conn->conn;
  967. struct iscsi_login *login = conn->conn_login;
  968. int size;
  969. isert_info("conn %p\n", isert_conn);
  970. WARN_ON_ONCE(!login);
  971. if (login->first_request) {
  972. struct iscsi_login_req *login_req =
  973. (struct iscsi_login_req *)&rx_desc->iscsi_header;
  974. /*
  975. * Setup the initial iscsi_login values from the leading
  976. * login request PDU.
  977. */
  978. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  979. login->current_stage =
  980. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
  981. >> 2;
  982. login->version_min = login_req->min_version;
  983. login->version_max = login_req->max_version;
  984. memcpy(login->isid, login_req->isid, 6);
  985. login->cmd_sn = be32_to_cpu(login_req->cmdsn);
  986. login->init_task_tag = login_req->itt;
  987. login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
  988. login->cid = be16_to_cpu(login_req->cid);
  989. login->tsih = be16_to_cpu(login_req->tsih);
  990. }
  991. memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
  992. size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
  993. isert_dbg("Using login payload size: %d, rx_buflen: %d "
  994. "MAX_KEY_VALUE_PAIRS: %d\n", size, rx_buflen,
  995. MAX_KEY_VALUE_PAIRS);
  996. memcpy(login->req_buf, &rx_desc->data[0], size);
  997. if (login->first_request) {
  998. complete(&isert_conn->conn_login_comp);
  999. return;
  1000. }
  1001. schedule_delayed_work(&conn->login_work, 0);
  1002. }
  1003. static struct iscsi_cmd
  1004. *isert_allocate_cmd(struct iscsi_conn *conn)
  1005. {
  1006. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1007. struct isert_cmd *isert_cmd;
  1008. struct iscsi_cmd *cmd;
  1009. cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
  1010. if (!cmd) {
  1011. isert_err("Unable to allocate iscsi_cmd + isert_cmd\n");
  1012. return NULL;
  1013. }
  1014. isert_cmd = iscsit_priv_cmd(cmd);
  1015. isert_cmd->conn = isert_conn;
  1016. isert_cmd->iscsi_cmd = cmd;
  1017. return cmd;
  1018. }
  1019. static int
  1020. isert_handle_scsi_cmd(struct isert_conn *isert_conn,
  1021. struct isert_cmd *isert_cmd, struct iscsi_cmd *cmd,
  1022. struct iser_rx_desc *rx_desc, unsigned char *buf)
  1023. {
  1024. struct iscsi_conn *conn = isert_conn->conn;
  1025. struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
  1026. struct scatterlist *sg;
  1027. int imm_data, imm_data_len, unsol_data, sg_nents, rc;
  1028. bool dump_payload = false;
  1029. rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
  1030. if (rc < 0)
  1031. return rc;
  1032. imm_data = cmd->immediate_data;
  1033. imm_data_len = cmd->first_burst_len;
  1034. unsol_data = cmd->unsolicited_data;
  1035. rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
  1036. if (rc < 0) {
  1037. return 0;
  1038. } else if (rc > 0) {
  1039. dump_payload = true;
  1040. goto sequence_cmd;
  1041. }
  1042. if (!imm_data)
  1043. return 0;
  1044. sg = &cmd->se_cmd.t_data_sg[0];
  1045. sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
  1046. isert_dbg("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
  1047. sg, sg_nents, &rx_desc->data[0], imm_data_len);
  1048. sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
  1049. cmd->write_data_done += imm_data_len;
  1050. if (cmd->write_data_done == cmd->se_cmd.data_length) {
  1051. spin_lock_bh(&cmd->istate_lock);
  1052. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  1053. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  1054. spin_unlock_bh(&cmd->istate_lock);
  1055. }
  1056. sequence_cmd:
  1057. rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
  1058. if (!rc && dump_payload == false && unsol_data)
  1059. iscsit_set_unsoliticed_dataout(cmd);
  1060. else if (dump_payload && imm_data)
  1061. target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
  1062. return 0;
  1063. }
  1064. static int
  1065. isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
  1066. struct iser_rx_desc *rx_desc, unsigned char *buf)
  1067. {
  1068. struct scatterlist *sg_start;
  1069. struct iscsi_conn *conn = isert_conn->conn;
  1070. struct iscsi_cmd *cmd = NULL;
  1071. struct iscsi_data *hdr = (struct iscsi_data *)buf;
  1072. u32 unsol_data_len = ntoh24(hdr->dlength);
  1073. int rc, sg_nents, sg_off, page_off;
  1074. rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
  1075. if (rc < 0)
  1076. return rc;
  1077. else if (!cmd)
  1078. return 0;
  1079. /*
  1080. * FIXME: Unexpected unsolicited_data out
  1081. */
  1082. if (!cmd->unsolicited_data) {
  1083. isert_err("Received unexpected solicited data payload\n");
  1084. dump_stack();
  1085. return -1;
  1086. }
  1087. isert_dbg("Unsolicited DataOut unsol_data_len: %u, "
  1088. "write_data_done: %u, data_length: %u\n",
  1089. unsol_data_len, cmd->write_data_done,
  1090. cmd->se_cmd.data_length);
  1091. sg_off = cmd->write_data_done / PAGE_SIZE;
  1092. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1093. sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
  1094. page_off = cmd->write_data_done % PAGE_SIZE;
  1095. /*
  1096. * FIXME: Non page-aligned unsolicited_data out
  1097. */
  1098. if (page_off) {
  1099. isert_err("unexpected non-page aligned data payload\n");
  1100. dump_stack();
  1101. return -1;
  1102. }
  1103. isert_dbg("Copying DataOut: sg_start: %p, sg_off: %u "
  1104. "sg_nents: %u from %p %u\n", sg_start, sg_off,
  1105. sg_nents, &rx_desc->data[0], unsol_data_len);
  1106. sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
  1107. unsol_data_len);
  1108. rc = iscsit_check_dataout_payload(cmd, hdr, false);
  1109. if (rc < 0)
  1110. return rc;
  1111. return 0;
  1112. }
  1113. static int
  1114. isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1115. struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
  1116. unsigned char *buf)
  1117. {
  1118. struct iscsi_conn *conn = isert_conn->conn;
  1119. struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
  1120. int rc;
  1121. rc = iscsit_setup_nop_out(conn, cmd, hdr);
  1122. if (rc < 0)
  1123. return rc;
  1124. /*
  1125. * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
  1126. */
  1127. return iscsit_process_nop_out(conn, cmd, hdr);
  1128. }
  1129. static int
  1130. isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1131. struct iscsi_cmd *cmd, struct iser_rx_desc *rx_desc,
  1132. struct iscsi_text *hdr)
  1133. {
  1134. struct iscsi_conn *conn = isert_conn->conn;
  1135. u32 payload_length = ntoh24(hdr->dlength);
  1136. int rc;
  1137. unsigned char *text_in = NULL;
  1138. rc = iscsit_setup_text_cmd(conn, cmd, hdr);
  1139. if (rc < 0)
  1140. return rc;
  1141. if (payload_length) {
  1142. text_in = kzalloc(payload_length, GFP_KERNEL);
  1143. if (!text_in) {
  1144. isert_err("Unable to allocate text_in of payload_length: %u\n",
  1145. payload_length);
  1146. return -ENOMEM;
  1147. }
  1148. }
  1149. cmd->text_in_ptr = text_in;
  1150. memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
  1151. return iscsit_process_text_cmd(conn, cmd, hdr);
  1152. }
  1153. static int
  1154. isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
  1155. uint32_t read_stag, uint64_t read_va,
  1156. uint32_t write_stag, uint64_t write_va)
  1157. {
  1158. struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
  1159. struct iscsi_conn *conn = isert_conn->conn;
  1160. struct iscsi_session *sess = conn->sess;
  1161. struct iscsi_cmd *cmd;
  1162. struct isert_cmd *isert_cmd;
  1163. int ret = -EINVAL;
  1164. u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
  1165. if (sess->sess_ops->SessionType &&
  1166. (!(opcode & ISCSI_OP_TEXT) || !(opcode & ISCSI_OP_LOGOUT))) {
  1167. isert_err("Got illegal opcode: 0x%02x in SessionType=Discovery,"
  1168. " ignoring\n", opcode);
  1169. return 0;
  1170. }
  1171. switch (opcode) {
  1172. case ISCSI_OP_SCSI_CMD:
  1173. cmd = isert_allocate_cmd(conn);
  1174. if (!cmd)
  1175. break;
  1176. isert_cmd = iscsit_priv_cmd(cmd);
  1177. isert_cmd->read_stag = read_stag;
  1178. isert_cmd->read_va = read_va;
  1179. isert_cmd->write_stag = write_stag;
  1180. isert_cmd->write_va = write_va;
  1181. ret = isert_handle_scsi_cmd(isert_conn, isert_cmd, cmd,
  1182. rx_desc, (unsigned char *)hdr);
  1183. break;
  1184. case ISCSI_OP_NOOP_OUT:
  1185. cmd = isert_allocate_cmd(conn);
  1186. if (!cmd)
  1187. break;
  1188. isert_cmd = iscsit_priv_cmd(cmd);
  1189. ret = isert_handle_nop_out(isert_conn, isert_cmd, cmd,
  1190. rx_desc, (unsigned char *)hdr);
  1191. break;
  1192. case ISCSI_OP_SCSI_DATA_OUT:
  1193. ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
  1194. (unsigned char *)hdr);
  1195. break;
  1196. case ISCSI_OP_SCSI_TMFUNC:
  1197. cmd = isert_allocate_cmd(conn);
  1198. if (!cmd)
  1199. break;
  1200. ret = iscsit_handle_task_mgt_cmd(conn, cmd,
  1201. (unsigned char *)hdr);
  1202. break;
  1203. case ISCSI_OP_LOGOUT:
  1204. cmd = isert_allocate_cmd(conn);
  1205. if (!cmd)
  1206. break;
  1207. ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
  1208. break;
  1209. case ISCSI_OP_TEXT:
  1210. if (be32_to_cpu(hdr->ttt) != 0xFFFFFFFF) {
  1211. cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
  1212. if (!cmd)
  1213. break;
  1214. } else {
  1215. cmd = isert_allocate_cmd(conn);
  1216. if (!cmd)
  1217. break;
  1218. }
  1219. isert_cmd = iscsit_priv_cmd(cmd);
  1220. ret = isert_handle_text_cmd(isert_conn, isert_cmd, cmd,
  1221. rx_desc, (struct iscsi_text *)hdr);
  1222. break;
  1223. default:
  1224. isert_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
  1225. dump_stack();
  1226. break;
  1227. }
  1228. return ret;
  1229. }
  1230. static void
  1231. isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
  1232. {
  1233. struct iser_hdr *iser_hdr = &rx_desc->iser_header;
  1234. uint64_t read_va = 0, write_va = 0;
  1235. uint32_t read_stag = 0, write_stag = 0;
  1236. int rc;
  1237. switch (iser_hdr->flags & 0xF0) {
  1238. case ISCSI_CTRL:
  1239. if (iser_hdr->flags & ISER_RSV) {
  1240. read_stag = be32_to_cpu(iser_hdr->read_stag);
  1241. read_va = be64_to_cpu(iser_hdr->read_va);
  1242. isert_dbg("ISER_RSV: read_stag: 0x%x read_va: 0x%llx\n",
  1243. read_stag, (unsigned long long)read_va);
  1244. }
  1245. if (iser_hdr->flags & ISER_WSV) {
  1246. write_stag = be32_to_cpu(iser_hdr->write_stag);
  1247. write_va = be64_to_cpu(iser_hdr->write_va);
  1248. isert_dbg("ISER_WSV: write_stag: 0x%x write_va: 0x%llx\n",
  1249. write_stag, (unsigned long long)write_va);
  1250. }
  1251. isert_dbg("ISER ISCSI_CTRL PDU\n");
  1252. break;
  1253. case ISER_HELLO:
  1254. isert_err("iSER Hello message\n");
  1255. break;
  1256. default:
  1257. isert_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
  1258. break;
  1259. }
  1260. rc = isert_rx_opcode(isert_conn, rx_desc,
  1261. read_stag, read_va, write_stag, write_va);
  1262. }
  1263. static void
  1264. isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
  1265. u32 xfer_len)
  1266. {
  1267. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1268. struct iscsi_hdr *hdr;
  1269. u64 rx_dma;
  1270. int rx_buflen, outstanding;
  1271. if ((char *)desc == isert_conn->login_req_buf) {
  1272. rx_dma = isert_conn->login_req_dma;
  1273. rx_buflen = ISER_RX_LOGIN_SIZE;
  1274. isert_dbg("login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  1275. rx_dma, rx_buflen);
  1276. } else {
  1277. rx_dma = desc->dma_addr;
  1278. rx_buflen = ISER_RX_PAYLOAD_SIZE;
  1279. isert_dbg("req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  1280. rx_dma, rx_buflen);
  1281. }
  1282. ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
  1283. hdr = &desc->iscsi_header;
  1284. isert_dbg("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
  1285. hdr->opcode, hdr->itt, hdr->flags,
  1286. (int)(xfer_len - ISER_HEADERS_LEN));
  1287. if ((char *)desc == isert_conn->login_req_buf) {
  1288. isert_conn->login_req_len = xfer_len - ISER_HEADERS_LEN;
  1289. if (isert_conn->conn) {
  1290. struct iscsi_login *login = isert_conn->conn->conn_login;
  1291. if (login && !login->first_request)
  1292. isert_rx_login_req(isert_conn);
  1293. }
  1294. mutex_lock(&isert_conn->conn_mutex);
  1295. complete(&isert_conn->login_req_comp);
  1296. mutex_unlock(&isert_conn->conn_mutex);
  1297. } else {
  1298. isert_rx_do_work(desc, isert_conn);
  1299. }
  1300. ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
  1301. DMA_FROM_DEVICE);
  1302. isert_conn->post_recv_buf_count--;
  1303. isert_dbg("Decremented post_recv_buf_count: %d\n",
  1304. isert_conn->post_recv_buf_count);
  1305. if ((char *)desc == isert_conn->login_req_buf)
  1306. return;
  1307. outstanding = isert_conn->post_recv_buf_count;
  1308. if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
  1309. int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
  1310. ISERT_MIN_POSTED_RX);
  1311. err = isert_post_recv(isert_conn, count);
  1312. if (err) {
  1313. isert_err("isert_post_recv() count: %d failed, %d\n",
  1314. count, err);
  1315. }
  1316. }
  1317. }
  1318. static int
  1319. isert_map_data_buf(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1320. struct scatterlist *sg, u32 nents, u32 length, u32 offset,
  1321. enum iser_ib_op_code op, struct isert_data_buf *data)
  1322. {
  1323. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1324. data->dma_dir = op == ISER_IB_RDMA_WRITE ?
  1325. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  1326. data->len = length - offset;
  1327. data->offset = offset;
  1328. data->sg_off = data->offset / PAGE_SIZE;
  1329. data->sg = &sg[data->sg_off];
  1330. data->nents = min_t(unsigned int, nents - data->sg_off,
  1331. ISCSI_ISER_SG_TABLESIZE);
  1332. data->len = min_t(unsigned int, data->len, ISCSI_ISER_SG_TABLESIZE *
  1333. PAGE_SIZE);
  1334. data->dma_nents = ib_dma_map_sg(ib_dev, data->sg, data->nents,
  1335. data->dma_dir);
  1336. if (unlikely(!data->dma_nents)) {
  1337. isert_err("Cmd: unable to dma map SGs %p\n", sg);
  1338. return -EINVAL;
  1339. }
  1340. isert_dbg("Mapped cmd: %p count: %u sg: %p sg_nents: %u rdma_len %d\n",
  1341. isert_cmd, data->dma_nents, data->sg, data->nents, data->len);
  1342. return 0;
  1343. }
  1344. static void
  1345. isert_unmap_data_buf(struct isert_conn *isert_conn, struct isert_data_buf *data)
  1346. {
  1347. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1348. ib_dma_unmap_sg(ib_dev, data->sg, data->nents, data->dma_dir);
  1349. memset(data, 0, sizeof(*data));
  1350. }
  1351. static void
  1352. isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
  1353. {
  1354. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1355. isert_dbg("Cmd %p\n", isert_cmd);
  1356. if (wr->data.sg) {
  1357. isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
  1358. isert_unmap_data_buf(isert_conn, &wr->data);
  1359. }
  1360. if (wr->send_wr) {
  1361. isert_dbg("Cmd %p free send_wr\n", isert_cmd);
  1362. kfree(wr->send_wr);
  1363. wr->send_wr = NULL;
  1364. }
  1365. if (wr->ib_sge) {
  1366. isert_dbg("Cmd %p free ib_sge\n", isert_cmd);
  1367. kfree(wr->ib_sge);
  1368. wr->ib_sge = NULL;
  1369. }
  1370. }
  1371. static void
  1372. isert_unreg_rdma(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
  1373. {
  1374. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1375. LIST_HEAD(unmap_list);
  1376. isert_dbg("Cmd %p\n", isert_cmd);
  1377. if (wr->fr_desc) {
  1378. isert_dbg("Cmd %p free fr_desc %p\n", isert_cmd, wr->fr_desc);
  1379. if (wr->fr_desc->ind & ISERT_PROTECTED) {
  1380. isert_unmap_data_buf(isert_conn, &wr->prot);
  1381. wr->fr_desc->ind &= ~ISERT_PROTECTED;
  1382. }
  1383. spin_lock_bh(&isert_conn->conn_lock);
  1384. list_add_tail(&wr->fr_desc->list, &isert_conn->conn_fr_pool);
  1385. spin_unlock_bh(&isert_conn->conn_lock);
  1386. wr->fr_desc = NULL;
  1387. }
  1388. if (wr->data.sg) {
  1389. isert_dbg("Cmd %p unmap_sg op\n", isert_cmd);
  1390. isert_unmap_data_buf(isert_conn, &wr->data);
  1391. }
  1392. wr->ib_sge = NULL;
  1393. wr->send_wr = NULL;
  1394. }
  1395. static void
  1396. isert_put_cmd(struct isert_cmd *isert_cmd, bool comp_err)
  1397. {
  1398. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1399. struct isert_conn *isert_conn = isert_cmd->conn;
  1400. struct iscsi_conn *conn = isert_conn->conn;
  1401. struct isert_device *device = isert_conn->conn_device;
  1402. struct iscsi_text_rsp *hdr;
  1403. isert_dbg("Cmd %p\n", isert_cmd);
  1404. switch (cmd->iscsi_opcode) {
  1405. case ISCSI_OP_SCSI_CMD:
  1406. spin_lock_bh(&conn->cmd_lock);
  1407. if (!list_empty(&cmd->i_conn_node))
  1408. list_del_init(&cmd->i_conn_node);
  1409. spin_unlock_bh(&conn->cmd_lock);
  1410. if (cmd->data_direction == DMA_TO_DEVICE) {
  1411. iscsit_stop_dataout_timer(cmd);
  1412. /*
  1413. * Check for special case during comp_err where
  1414. * WRITE_PENDING has been handed off from core,
  1415. * but requires an extra target_put_sess_cmd()
  1416. * before transport_generic_free_cmd() below.
  1417. */
  1418. if (comp_err &&
  1419. cmd->se_cmd.t_state == TRANSPORT_WRITE_PENDING) {
  1420. struct se_cmd *se_cmd = &cmd->se_cmd;
  1421. target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  1422. }
  1423. }
  1424. device->unreg_rdma_mem(isert_cmd, isert_conn);
  1425. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1426. break;
  1427. case ISCSI_OP_SCSI_TMFUNC:
  1428. spin_lock_bh(&conn->cmd_lock);
  1429. if (!list_empty(&cmd->i_conn_node))
  1430. list_del_init(&cmd->i_conn_node);
  1431. spin_unlock_bh(&conn->cmd_lock);
  1432. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1433. break;
  1434. case ISCSI_OP_REJECT:
  1435. case ISCSI_OP_NOOP_OUT:
  1436. case ISCSI_OP_TEXT:
  1437. hdr = (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
  1438. /* If the continue bit is on, keep the command alive */
  1439. if (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)
  1440. break;
  1441. spin_lock_bh(&conn->cmd_lock);
  1442. if (!list_empty(&cmd->i_conn_node))
  1443. list_del_init(&cmd->i_conn_node);
  1444. spin_unlock_bh(&conn->cmd_lock);
  1445. /*
  1446. * Handle special case for REJECT when iscsi_add_reject*() has
  1447. * overwritten the original iscsi_opcode assignment, and the
  1448. * associated cmd->se_cmd needs to be released.
  1449. */
  1450. if (cmd->se_cmd.se_tfo != NULL) {
  1451. isert_dbg("Calling transport_generic_free_cmd for 0x%02x\n",
  1452. cmd->iscsi_opcode);
  1453. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1454. break;
  1455. }
  1456. /*
  1457. * Fall-through
  1458. */
  1459. default:
  1460. iscsit_release_cmd(cmd);
  1461. break;
  1462. }
  1463. }
  1464. static void
  1465. isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
  1466. {
  1467. if (tx_desc->dma_addr != 0) {
  1468. isert_dbg("unmap single for tx_desc->dma_addr\n");
  1469. ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
  1470. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  1471. tx_desc->dma_addr = 0;
  1472. }
  1473. }
  1474. static void
  1475. isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
  1476. struct ib_device *ib_dev, bool comp_err)
  1477. {
  1478. if (isert_cmd->pdu_buf_dma != 0) {
  1479. isert_dbg("unmap single for isert_cmd->pdu_buf_dma\n");
  1480. ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
  1481. isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
  1482. isert_cmd->pdu_buf_dma = 0;
  1483. }
  1484. isert_unmap_tx_desc(tx_desc, ib_dev);
  1485. isert_put_cmd(isert_cmd, comp_err);
  1486. }
  1487. static int
  1488. isert_check_pi_status(struct se_cmd *se_cmd, struct ib_mr *sig_mr)
  1489. {
  1490. struct ib_mr_status mr_status;
  1491. int ret;
  1492. ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
  1493. if (ret) {
  1494. isert_err("ib_check_mr_status failed, ret %d\n", ret);
  1495. goto fail_mr_status;
  1496. }
  1497. if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
  1498. u64 sec_offset_err;
  1499. u32 block_size = se_cmd->se_dev->dev_attrib.block_size + 8;
  1500. switch (mr_status.sig_err.err_type) {
  1501. case IB_SIG_BAD_GUARD:
  1502. se_cmd->pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
  1503. break;
  1504. case IB_SIG_BAD_REFTAG:
  1505. se_cmd->pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
  1506. break;
  1507. case IB_SIG_BAD_APPTAG:
  1508. se_cmd->pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
  1509. break;
  1510. }
  1511. sec_offset_err = mr_status.sig_err.sig_err_offset;
  1512. do_div(sec_offset_err, block_size);
  1513. se_cmd->bad_sector = sec_offset_err + se_cmd->t_task_lba;
  1514. isert_err("PI error found type %d at sector 0x%llx "
  1515. "expected 0x%x vs actual 0x%x\n",
  1516. mr_status.sig_err.err_type,
  1517. (unsigned long long)se_cmd->bad_sector,
  1518. mr_status.sig_err.expected,
  1519. mr_status.sig_err.actual);
  1520. ret = 1;
  1521. }
  1522. fail_mr_status:
  1523. return ret;
  1524. }
  1525. static void
  1526. isert_completion_rdma_write(struct iser_tx_desc *tx_desc,
  1527. struct isert_cmd *isert_cmd)
  1528. {
  1529. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1530. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1531. struct se_cmd *se_cmd = &cmd->se_cmd;
  1532. struct isert_conn *isert_conn = isert_cmd->conn;
  1533. struct isert_device *device = isert_conn->conn_device;
  1534. int ret = 0;
  1535. if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
  1536. ret = isert_check_pi_status(se_cmd,
  1537. wr->fr_desc->pi_ctx->sig_mr);
  1538. wr->fr_desc->ind &= ~ISERT_PROTECTED;
  1539. }
  1540. device->unreg_rdma_mem(isert_cmd, isert_conn);
  1541. wr->send_wr_num = 0;
  1542. if (ret)
  1543. transport_send_check_condition_and_sense(se_cmd,
  1544. se_cmd->pi_err, 0);
  1545. else
  1546. isert_put_response(isert_conn->conn, cmd);
  1547. }
  1548. static void
  1549. isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
  1550. struct isert_cmd *isert_cmd)
  1551. {
  1552. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1553. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1554. struct se_cmd *se_cmd = &cmd->se_cmd;
  1555. struct isert_conn *isert_conn = isert_cmd->conn;
  1556. struct isert_device *device = isert_conn->conn_device;
  1557. int ret = 0;
  1558. if (wr->fr_desc && wr->fr_desc->ind & ISERT_PROTECTED) {
  1559. ret = isert_check_pi_status(se_cmd,
  1560. wr->fr_desc->pi_ctx->sig_mr);
  1561. wr->fr_desc->ind &= ~ISERT_PROTECTED;
  1562. }
  1563. iscsit_stop_dataout_timer(cmd);
  1564. device->unreg_rdma_mem(isert_cmd, isert_conn);
  1565. cmd->write_data_done = wr->data.len;
  1566. wr->send_wr_num = 0;
  1567. isert_dbg("Cmd: %p RDMA_READ comp calling execute_cmd\n", isert_cmd);
  1568. spin_lock_bh(&cmd->istate_lock);
  1569. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  1570. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  1571. spin_unlock_bh(&cmd->istate_lock);
  1572. if (ret) {
  1573. target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  1574. transport_send_check_condition_and_sense(se_cmd,
  1575. se_cmd->pi_err, 0);
  1576. } else {
  1577. target_execute_cmd(se_cmd);
  1578. }
  1579. }
  1580. static void
  1581. isert_do_control_comp(struct work_struct *work)
  1582. {
  1583. struct isert_cmd *isert_cmd = container_of(work,
  1584. struct isert_cmd, comp_work);
  1585. struct isert_conn *isert_conn = isert_cmd->conn;
  1586. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1587. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1588. isert_dbg("Cmd %p i_state %d\n", isert_cmd, cmd->i_state);
  1589. switch (cmd->i_state) {
  1590. case ISTATE_SEND_TASKMGTRSP:
  1591. iscsit_tmr_post_handler(cmd, cmd->conn);
  1592. case ISTATE_SEND_REJECT: /* FALLTHRU */
  1593. case ISTATE_SEND_TEXTRSP: /* FALLTHRU */
  1594. cmd->i_state = ISTATE_SENT_STATUS;
  1595. isert_completion_put(&isert_cmd->tx_desc, isert_cmd,
  1596. ib_dev, false);
  1597. break;
  1598. case ISTATE_SEND_LOGOUTRSP:
  1599. iscsit_logout_post_handler(cmd, cmd->conn);
  1600. break;
  1601. default:
  1602. isert_err("Unknown i_state %d\n", cmd->i_state);
  1603. dump_stack();
  1604. break;
  1605. }
  1606. }
  1607. static void
  1608. isert_response_completion(struct iser_tx_desc *tx_desc,
  1609. struct isert_cmd *isert_cmd,
  1610. struct isert_conn *isert_conn,
  1611. struct ib_device *ib_dev)
  1612. {
  1613. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1614. if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
  1615. cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
  1616. cmd->i_state == ISTATE_SEND_REJECT ||
  1617. cmd->i_state == ISTATE_SEND_TEXTRSP) {
  1618. isert_unmap_tx_desc(tx_desc, ib_dev);
  1619. INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
  1620. queue_work(isert_comp_wq, &isert_cmd->comp_work);
  1621. return;
  1622. }
  1623. cmd->i_state = ISTATE_SENT_STATUS;
  1624. isert_completion_put(tx_desc, isert_cmd, ib_dev, false);
  1625. }
  1626. static void
  1627. isert_send_completion(struct iser_tx_desc *tx_desc,
  1628. struct isert_conn *isert_conn)
  1629. {
  1630. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1631. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1632. struct isert_rdma_wr *wr;
  1633. if (!isert_cmd) {
  1634. isert_unmap_tx_desc(tx_desc, ib_dev);
  1635. return;
  1636. }
  1637. wr = &isert_cmd->rdma_wr;
  1638. isert_dbg("Cmd %p iser_ib_op %d\n", isert_cmd, wr->iser_ib_op);
  1639. switch (wr->iser_ib_op) {
  1640. case ISER_IB_RECV:
  1641. isert_err("Got ISER_IB_RECV\n");
  1642. dump_stack();
  1643. break;
  1644. case ISER_IB_SEND:
  1645. isert_response_completion(tx_desc, isert_cmd,
  1646. isert_conn, ib_dev);
  1647. break;
  1648. case ISER_IB_RDMA_WRITE:
  1649. isert_completion_rdma_write(tx_desc, isert_cmd);
  1650. break;
  1651. case ISER_IB_RDMA_READ:
  1652. isert_completion_rdma_read(tx_desc, isert_cmd);
  1653. break;
  1654. default:
  1655. isert_err("Unknown wr->iser_ib_op: 0x%x\n", wr->iser_ib_op);
  1656. dump_stack();
  1657. break;
  1658. }
  1659. }
  1660. /**
  1661. * is_isert_tx_desc() - Indicate if the completion wr_id
  1662. * is a TX descriptor or not.
  1663. * @isert_conn: iser connection
  1664. * @wr_id: completion WR identifier
  1665. *
  1666. * Since we cannot rely on wc opcode in FLUSH errors
  1667. * we must work around it by checking if the wr_id address
  1668. * falls in the iser connection rx_descs buffer. If so
  1669. * it is an RX descriptor, otherwize it is a TX.
  1670. */
  1671. static inline bool
  1672. is_isert_tx_desc(struct isert_conn *isert_conn, void *wr_id)
  1673. {
  1674. void *start = isert_conn->conn_rx_descs;
  1675. int len = ISERT_QP_MAX_RECV_DTOS * sizeof(*isert_conn->conn_rx_descs);
  1676. if (wr_id >= start && wr_id < start + len)
  1677. return false;
  1678. return true;
  1679. }
  1680. static void
  1681. isert_cq_comp_err(struct isert_conn *isert_conn, struct ib_wc *wc)
  1682. {
  1683. if (wc->wr_id == ISER_BEACON_WRID) {
  1684. isert_info("conn %p completing conn_wait_comp_err\n",
  1685. isert_conn);
  1686. complete(&isert_conn->conn_wait_comp_err);
  1687. } else if (is_isert_tx_desc(isert_conn, (void *)(uintptr_t)wc->wr_id)) {
  1688. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1689. struct isert_cmd *isert_cmd;
  1690. struct iser_tx_desc *desc;
  1691. desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
  1692. isert_cmd = desc->isert_cmd;
  1693. if (!isert_cmd)
  1694. isert_unmap_tx_desc(desc, ib_dev);
  1695. else
  1696. isert_completion_put(desc, isert_cmd, ib_dev, true);
  1697. } else {
  1698. isert_conn->post_recv_buf_count--;
  1699. if (!isert_conn->post_recv_buf_count)
  1700. iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
  1701. }
  1702. }
  1703. static void
  1704. isert_handle_wc(struct ib_wc *wc)
  1705. {
  1706. struct isert_conn *isert_conn;
  1707. struct iser_tx_desc *tx_desc;
  1708. struct iser_rx_desc *rx_desc;
  1709. isert_conn = wc->qp->qp_context;
  1710. if (likely(wc->status == IB_WC_SUCCESS)) {
  1711. if (wc->opcode == IB_WC_RECV) {
  1712. rx_desc = (struct iser_rx_desc *)(uintptr_t)wc->wr_id;
  1713. isert_rx_completion(rx_desc, isert_conn, wc->byte_len);
  1714. } else {
  1715. tx_desc = (struct iser_tx_desc *)(uintptr_t)wc->wr_id;
  1716. isert_send_completion(tx_desc, isert_conn);
  1717. }
  1718. } else {
  1719. if (wc->status != IB_WC_WR_FLUSH_ERR)
  1720. isert_err("wr id %llx status %d vend_err %x\n",
  1721. wc->wr_id, wc->status, wc->vendor_err);
  1722. else
  1723. isert_dbg("flush error: wr id %llx\n", wc->wr_id);
  1724. if (wc->wr_id != ISER_FASTREG_LI_WRID)
  1725. isert_cq_comp_err(isert_conn, wc);
  1726. }
  1727. }
  1728. static void
  1729. isert_cq_work(struct work_struct *work)
  1730. {
  1731. enum { isert_poll_budget = 65536 };
  1732. struct isert_comp *comp = container_of(work, struct isert_comp,
  1733. work);
  1734. struct ib_wc *const wcs = comp->wcs;
  1735. int i, n, completed = 0;
  1736. while ((n = ib_poll_cq(comp->cq, ARRAY_SIZE(comp->wcs), wcs)) > 0) {
  1737. for (i = 0; i < n; i++)
  1738. isert_handle_wc(&wcs[i]);
  1739. completed += n;
  1740. if (completed >= isert_poll_budget)
  1741. break;
  1742. }
  1743. ib_req_notify_cq(comp->cq, IB_CQ_NEXT_COMP);
  1744. }
  1745. static void
  1746. isert_cq_callback(struct ib_cq *cq, void *context)
  1747. {
  1748. struct isert_comp *comp = context;
  1749. queue_work(isert_comp_wq, &comp->work);
  1750. }
  1751. static int
  1752. isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
  1753. {
  1754. struct ib_send_wr *wr_failed;
  1755. int ret;
  1756. ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
  1757. &wr_failed);
  1758. if (ret) {
  1759. isert_err("ib_post_send failed with %d\n", ret);
  1760. return ret;
  1761. }
  1762. return ret;
  1763. }
  1764. static int
  1765. isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1766. {
  1767. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1768. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1769. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1770. struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
  1771. &isert_cmd->tx_desc.iscsi_header;
  1772. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1773. iscsit_build_rsp_pdu(cmd, conn, true, hdr);
  1774. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1775. /*
  1776. * Attach SENSE DATA payload to iSCSI Response PDU
  1777. */
  1778. if (cmd->se_cmd.sense_buffer &&
  1779. ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  1780. (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  1781. struct isert_device *device = isert_conn->conn_device;
  1782. struct ib_device *ib_dev = device->ib_device;
  1783. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1784. u32 padding, pdu_len;
  1785. put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
  1786. cmd->sense_buffer);
  1787. cmd->se_cmd.scsi_sense_length += sizeof(__be16);
  1788. padding = -(cmd->se_cmd.scsi_sense_length) & 3;
  1789. hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
  1790. pdu_len = cmd->se_cmd.scsi_sense_length + padding;
  1791. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1792. (void *)cmd->sense_buffer, pdu_len,
  1793. DMA_TO_DEVICE);
  1794. isert_cmd->pdu_buf_len = pdu_len;
  1795. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1796. tx_dsg->length = pdu_len;
  1797. tx_dsg->lkey = device->mr->lkey;
  1798. isert_cmd->tx_desc.num_sge = 2;
  1799. }
  1800. isert_init_send_wr(isert_conn, isert_cmd, send_wr);
  1801. isert_dbg("Posting SCSI Response\n");
  1802. return isert_post_response(isert_conn, isert_cmd);
  1803. }
  1804. static void
  1805. isert_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1806. {
  1807. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1808. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1809. struct isert_device *device = isert_conn->conn_device;
  1810. spin_lock_bh(&conn->cmd_lock);
  1811. if (!list_empty(&cmd->i_conn_node))
  1812. list_del_init(&cmd->i_conn_node);
  1813. spin_unlock_bh(&conn->cmd_lock);
  1814. if (cmd->data_direction == DMA_TO_DEVICE)
  1815. iscsit_stop_dataout_timer(cmd);
  1816. device->unreg_rdma_mem(isert_cmd, isert_conn);
  1817. }
  1818. static enum target_prot_op
  1819. isert_get_sup_prot_ops(struct iscsi_conn *conn)
  1820. {
  1821. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1822. struct isert_device *device = isert_conn->conn_device;
  1823. if (conn->tpg->tpg_attrib.t10_pi) {
  1824. if (device->pi_capable) {
  1825. isert_info("conn %p PI offload enabled\n", isert_conn);
  1826. isert_conn->pi_support = true;
  1827. return TARGET_PROT_ALL;
  1828. }
  1829. }
  1830. isert_info("conn %p PI offload disabled\n", isert_conn);
  1831. isert_conn->pi_support = false;
  1832. return TARGET_PROT_NORMAL;
  1833. }
  1834. static int
  1835. isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
  1836. bool nopout_response)
  1837. {
  1838. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1839. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1840. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1841. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1842. iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
  1843. &isert_cmd->tx_desc.iscsi_header,
  1844. nopout_response);
  1845. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1846. isert_init_send_wr(isert_conn, isert_cmd, send_wr);
  1847. isert_dbg("conn %p Posting NOPIN Response\n", isert_conn);
  1848. return isert_post_response(isert_conn, isert_cmd);
  1849. }
  1850. static int
  1851. isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1852. {
  1853. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1854. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1855. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1856. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1857. iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
  1858. &isert_cmd->tx_desc.iscsi_header);
  1859. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1860. isert_init_send_wr(isert_conn, isert_cmd, send_wr);
  1861. isert_dbg("conn %p Posting Logout Response\n", isert_conn);
  1862. return isert_post_response(isert_conn, isert_cmd);
  1863. }
  1864. static int
  1865. isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1866. {
  1867. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1868. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1869. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1870. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1871. iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
  1872. &isert_cmd->tx_desc.iscsi_header);
  1873. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1874. isert_init_send_wr(isert_conn, isert_cmd, send_wr);
  1875. isert_dbg("conn %p Posting Task Management Response\n", isert_conn);
  1876. return isert_post_response(isert_conn, isert_cmd);
  1877. }
  1878. static int
  1879. isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1880. {
  1881. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1882. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1883. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1884. struct isert_device *device = isert_conn->conn_device;
  1885. struct ib_device *ib_dev = device->ib_device;
  1886. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1887. struct iscsi_reject *hdr =
  1888. (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
  1889. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1890. iscsit_build_reject(cmd, conn, hdr);
  1891. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1892. hton24(hdr->dlength, ISCSI_HDR_LEN);
  1893. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1894. (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
  1895. DMA_TO_DEVICE);
  1896. isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
  1897. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1898. tx_dsg->length = ISCSI_HDR_LEN;
  1899. tx_dsg->lkey = device->mr->lkey;
  1900. isert_cmd->tx_desc.num_sge = 2;
  1901. isert_init_send_wr(isert_conn, isert_cmd, send_wr);
  1902. isert_dbg("conn %p Posting Reject\n", isert_conn);
  1903. return isert_post_response(isert_conn, isert_cmd);
  1904. }
  1905. static int
  1906. isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1907. {
  1908. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1909. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1910. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1911. struct iscsi_text_rsp *hdr =
  1912. (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
  1913. u32 txt_rsp_len;
  1914. int rc;
  1915. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1916. rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_INFINIBAND);
  1917. if (rc < 0)
  1918. return rc;
  1919. txt_rsp_len = rc;
  1920. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1921. if (txt_rsp_len) {
  1922. struct isert_device *device = isert_conn->conn_device;
  1923. struct ib_device *ib_dev = device->ib_device;
  1924. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1925. void *txt_rsp_buf = cmd->buf_ptr;
  1926. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1927. txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
  1928. isert_cmd->pdu_buf_len = txt_rsp_len;
  1929. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1930. tx_dsg->length = txt_rsp_len;
  1931. tx_dsg->lkey = device->mr->lkey;
  1932. isert_cmd->tx_desc.num_sge = 2;
  1933. }
  1934. isert_init_send_wr(isert_conn, isert_cmd, send_wr);
  1935. isert_dbg("conn %p Text Response\n", isert_conn);
  1936. return isert_post_response(isert_conn, isert_cmd);
  1937. }
  1938. static int
  1939. isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1940. struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
  1941. u32 data_left, u32 offset)
  1942. {
  1943. struct iscsi_cmd *cmd = isert_cmd->iscsi_cmd;
  1944. struct scatterlist *sg_start, *tmp_sg;
  1945. struct isert_device *device = isert_conn->conn_device;
  1946. struct ib_device *ib_dev = device->ib_device;
  1947. u32 sg_off, page_off;
  1948. int i = 0, sg_nents;
  1949. sg_off = offset / PAGE_SIZE;
  1950. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1951. sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
  1952. page_off = offset % PAGE_SIZE;
  1953. send_wr->sg_list = ib_sge;
  1954. send_wr->num_sge = sg_nents;
  1955. send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
  1956. /*
  1957. * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
  1958. */
  1959. for_each_sg(sg_start, tmp_sg, sg_nents, i) {
  1960. isert_dbg("RDMA from SGL dma_addr: 0x%llx dma_len: %u, "
  1961. "page_off: %u\n",
  1962. (unsigned long long)tmp_sg->dma_address,
  1963. tmp_sg->length, page_off);
  1964. ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
  1965. ib_sge->length = min_t(u32, data_left,
  1966. ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
  1967. ib_sge->lkey = device->mr->lkey;
  1968. isert_dbg("RDMA ib_sge: addr: 0x%llx length: %u lkey: %x\n",
  1969. ib_sge->addr, ib_sge->length, ib_sge->lkey);
  1970. page_off = 0;
  1971. data_left -= ib_sge->length;
  1972. ib_sge++;
  1973. isert_dbg("Incrementing ib_sge pointer to %p\n", ib_sge);
  1974. }
  1975. isert_dbg("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
  1976. send_wr->sg_list, send_wr->num_sge);
  1977. return sg_nents;
  1978. }
  1979. static int
  1980. isert_map_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  1981. struct isert_rdma_wr *wr)
  1982. {
  1983. struct se_cmd *se_cmd = &cmd->se_cmd;
  1984. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  1985. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1986. struct isert_data_buf *data = &wr->data;
  1987. struct ib_send_wr *send_wr;
  1988. struct ib_sge *ib_sge;
  1989. u32 offset, data_len, data_left, rdma_write_max, va_offset = 0;
  1990. int ret = 0, i, ib_sge_cnt;
  1991. isert_cmd->tx_desc.isert_cmd = isert_cmd;
  1992. offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
  1993. ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
  1994. se_cmd->t_data_nents, se_cmd->data_length,
  1995. offset, wr->iser_ib_op, &wr->data);
  1996. if (ret)
  1997. return ret;
  1998. data_left = data->len;
  1999. offset = data->offset;
  2000. ib_sge = kzalloc(sizeof(struct ib_sge) * data->nents, GFP_KERNEL);
  2001. if (!ib_sge) {
  2002. isert_warn("Unable to allocate ib_sge\n");
  2003. ret = -ENOMEM;
  2004. goto unmap_cmd;
  2005. }
  2006. wr->ib_sge = ib_sge;
  2007. wr->send_wr_num = DIV_ROUND_UP(data->nents, isert_conn->max_sge);
  2008. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  2009. GFP_KERNEL);
  2010. if (!wr->send_wr) {
  2011. isert_dbg("Unable to allocate wr->send_wr\n");
  2012. ret = -ENOMEM;
  2013. goto unmap_cmd;
  2014. }
  2015. wr->isert_cmd = isert_cmd;
  2016. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  2017. for (i = 0; i < wr->send_wr_num; i++) {
  2018. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  2019. data_len = min(data_left, rdma_write_max);
  2020. send_wr->send_flags = 0;
  2021. if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
  2022. send_wr->opcode = IB_WR_RDMA_WRITE;
  2023. send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
  2024. send_wr->wr.rdma.rkey = isert_cmd->read_stag;
  2025. if (i + 1 == wr->send_wr_num)
  2026. send_wr->next = &isert_cmd->tx_desc.send_wr;
  2027. else
  2028. send_wr->next = &wr->send_wr[i + 1];
  2029. } else {
  2030. send_wr->opcode = IB_WR_RDMA_READ;
  2031. send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
  2032. send_wr->wr.rdma.rkey = isert_cmd->write_stag;
  2033. if (i + 1 == wr->send_wr_num)
  2034. send_wr->send_flags = IB_SEND_SIGNALED;
  2035. else
  2036. send_wr->next = &wr->send_wr[i + 1];
  2037. }
  2038. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  2039. send_wr, data_len, offset);
  2040. ib_sge += ib_sge_cnt;
  2041. offset += data_len;
  2042. va_offset += data_len;
  2043. data_left -= data_len;
  2044. }
  2045. return 0;
  2046. unmap_cmd:
  2047. isert_unmap_data_buf(isert_conn, data);
  2048. return ret;
  2049. }
  2050. static int
  2051. isert_map_fr_pagelist(struct ib_device *ib_dev,
  2052. struct scatterlist *sg_start, int sg_nents, u64 *fr_pl)
  2053. {
  2054. u64 start_addr, end_addr, page, chunk_start = 0;
  2055. struct scatterlist *tmp_sg;
  2056. int i = 0, new_chunk, last_ent, n_pages;
  2057. n_pages = 0;
  2058. new_chunk = 1;
  2059. last_ent = sg_nents - 1;
  2060. for_each_sg(sg_start, tmp_sg, sg_nents, i) {
  2061. start_addr = ib_sg_dma_address(ib_dev, tmp_sg);
  2062. if (new_chunk)
  2063. chunk_start = start_addr;
  2064. end_addr = start_addr + ib_sg_dma_len(ib_dev, tmp_sg);
  2065. isert_dbg("SGL[%d] dma_addr: 0x%llx len: %u\n",
  2066. i, (unsigned long long)tmp_sg->dma_address,
  2067. tmp_sg->length);
  2068. if ((end_addr & ~PAGE_MASK) && i < last_ent) {
  2069. new_chunk = 0;
  2070. continue;
  2071. }
  2072. new_chunk = 1;
  2073. page = chunk_start & PAGE_MASK;
  2074. do {
  2075. fr_pl[n_pages++] = page;
  2076. isert_dbg("Mapped page_list[%d] page_addr: 0x%llx\n",
  2077. n_pages - 1, page);
  2078. page += PAGE_SIZE;
  2079. } while (page < end_addr);
  2080. }
  2081. return n_pages;
  2082. }
  2083. static inline void
  2084. isert_inv_rkey(struct ib_send_wr *inv_wr, struct ib_mr *mr)
  2085. {
  2086. u32 rkey;
  2087. memset(inv_wr, 0, sizeof(*inv_wr));
  2088. inv_wr->wr_id = ISER_FASTREG_LI_WRID;
  2089. inv_wr->opcode = IB_WR_LOCAL_INV;
  2090. inv_wr->ex.invalidate_rkey = mr->rkey;
  2091. /* Bump the key */
  2092. rkey = ib_inc_rkey(mr->rkey);
  2093. ib_update_fast_reg_key(mr, rkey);
  2094. }
  2095. static int
  2096. isert_fast_reg_mr(struct isert_conn *isert_conn,
  2097. struct fast_reg_descriptor *fr_desc,
  2098. struct isert_data_buf *mem,
  2099. enum isert_indicator ind,
  2100. struct ib_sge *sge)
  2101. {
  2102. struct isert_device *device = isert_conn->conn_device;
  2103. struct ib_device *ib_dev = device->ib_device;
  2104. struct ib_mr *mr;
  2105. struct ib_fast_reg_page_list *frpl;
  2106. struct ib_send_wr fr_wr, inv_wr;
  2107. struct ib_send_wr *bad_wr, *wr = NULL;
  2108. int ret, pagelist_len;
  2109. u32 page_off;
  2110. if (mem->dma_nents == 1) {
  2111. sge->lkey = device->mr->lkey;
  2112. sge->addr = ib_sg_dma_address(ib_dev, &mem->sg[0]);
  2113. sge->length = ib_sg_dma_len(ib_dev, &mem->sg[0]);
  2114. isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
  2115. sge->addr, sge->length, sge->lkey);
  2116. return 0;
  2117. }
  2118. if (ind == ISERT_DATA_KEY_VALID) {
  2119. /* Registering data buffer */
  2120. mr = fr_desc->data_mr;
  2121. frpl = fr_desc->data_frpl;
  2122. } else {
  2123. /* Registering protection buffer */
  2124. mr = fr_desc->pi_ctx->prot_mr;
  2125. frpl = fr_desc->pi_ctx->prot_frpl;
  2126. }
  2127. page_off = mem->offset % PAGE_SIZE;
  2128. isert_dbg("Use fr_desc %p sg_nents %d offset %u\n",
  2129. fr_desc, mem->nents, mem->offset);
  2130. pagelist_len = isert_map_fr_pagelist(ib_dev, mem->sg, mem->nents,
  2131. &frpl->page_list[0]);
  2132. if (!(fr_desc->ind & ind)) {
  2133. isert_inv_rkey(&inv_wr, mr);
  2134. wr = &inv_wr;
  2135. }
  2136. /* Prepare FASTREG WR */
  2137. memset(&fr_wr, 0, sizeof(fr_wr));
  2138. fr_wr.wr_id = ISER_FASTREG_LI_WRID;
  2139. fr_wr.opcode = IB_WR_FAST_REG_MR;
  2140. fr_wr.wr.fast_reg.iova_start = frpl->page_list[0] + page_off;
  2141. fr_wr.wr.fast_reg.page_list = frpl;
  2142. fr_wr.wr.fast_reg.page_list_len = pagelist_len;
  2143. fr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
  2144. fr_wr.wr.fast_reg.length = mem->len;
  2145. fr_wr.wr.fast_reg.rkey = mr->rkey;
  2146. fr_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE;
  2147. if (!wr)
  2148. wr = &fr_wr;
  2149. else
  2150. wr->next = &fr_wr;
  2151. ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
  2152. if (ret) {
  2153. isert_err("fast registration failed, ret:%d\n", ret);
  2154. return ret;
  2155. }
  2156. fr_desc->ind &= ~ind;
  2157. sge->lkey = mr->lkey;
  2158. sge->addr = frpl->page_list[0] + page_off;
  2159. sge->length = mem->len;
  2160. isert_dbg("sge: addr: 0x%llx length: %u lkey: %x\n",
  2161. sge->addr, sge->length, sge->lkey);
  2162. return ret;
  2163. }
  2164. static inline void
  2165. isert_set_dif_domain(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs,
  2166. struct ib_sig_domain *domain)
  2167. {
  2168. domain->sig_type = IB_SIG_TYPE_T10_DIF;
  2169. domain->sig.dif.bg_type = IB_T10DIF_CRC;
  2170. domain->sig.dif.pi_interval = se_cmd->se_dev->dev_attrib.block_size;
  2171. domain->sig.dif.ref_tag = se_cmd->reftag_seed;
  2172. /*
  2173. * At the moment we hard code those, but if in the future
  2174. * the target core would like to use it, we will take it
  2175. * from se_cmd.
  2176. */
  2177. domain->sig.dif.apptag_check_mask = 0xffff;
  2178. domain->sig.dif.app_escape = true;
  2179. domain->sig.dif.ref_escape = true;
  2180. if (se_cmd->prot_type == TARGET_DIF_TYPE1_PROT ||
  2181. se_cmd->prot_type == TARGET_DIF_TYPE2_PROT)
  2182. domain->sig.dif.ref_remap = true;
  2183. };
  2184. static int
  2185. isert_set_sig_attrs(struct se_cmd *se_cmd, struct ib_sig_attrs *sig_attrs)
  2186. {
  2187. switch (se_cmd->prot_op) {
  2188. case TARGET_PROT_DIN_INSERT:
  2189. case TARGET_PROT_DOUT_STRIP:
  2190. sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
  2191. isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
  2192. break;
  2193. case TARGET_PROT_DOUT_INSERT:
  2194. case TARGET_PROT_DIN_STRIP:
  2195. sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
  2196. isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
  2197. break;
  2198. case TARGET_PROT_DIN_PASS:
  2199. case TARGET_PROT_DOUT_PASS:
  2200. isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->wire);
  2201. isert_set_dif_domain(se_cmd, sig_attrs, &sig_attrs->mem);
  2202. break;
  2203. default:
  2204. isert_err("Unsupported PI operation %d\n", se_cmd->prot_op);
  2205. return -EINVAL;
  2206. }
  2207. return 0;
  2208. }
  2209. static inline u8
  2210. isert_set_prot_checks(u8 prot_checks)
  2211. {
  2212. return (prot_checks & TARGET_DIF_CHECK_GUARD ? 0xc0 : 0) |
  2213. (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x30 : 0) |
  2214. (prot_checks & TARGET_DIF_CHECK_REFTAG ? 0x0f : 0);
  2215. }
  2216. static int
  2217. isert_reg_sig_mr(struct isert_conn *isert_conn,
  2218. struct se_cmd *se_cmd,
  2219. struct isert_rdma_wr *rdma_wr,
  2220. struct fast_reg_descriptor *fr_desc)
  2221. {
  2222. struct ib_send_wr sig_wr, inv_wr;
  2223. struct ib_send_wr *bad_wr, *wr = NULL;
  2224. struct pi_context *pi_ctx = fr_desc->pi_ctx;
  2225. struct ib_sig_attrs sig_attrs;
  2226. int ret;
  2227. memset(&sig_attrs, 0, sizeof(sig_attrs));
  2228. ret = isert_set_sig_attrs(se_cmd, &sig_attrs);
  2229. if (ret)
  2230. goto err;
  2231. sig_attrs.check_mask = isert_set_prot_checks(se_cmd->prot_checks);
  2232. if (!(fr_desc->ind & ISERT_SIG_KEY_VALID)) {
  2233. isert_inv_rkey(&inv_wr, pi_ctx->sig_mr);
  2234. wr = &inv_wr;
  2235. }
  2236. memset(&sig_wr, 0, sizeof(sig_wr));
  2237. sig_wr.opcode = IB_WR_REG_SIG_MR;
  2238. sig_wr.wr_id = ISER_FASTREG_LI_WRID;
  2239. sig_wr.sg_list = &rdma_wr->ib_sg[DATA];
  2240. sig_wr.num_sge = 1;
  2241. sig_wr.wr.sig_handover.access_flags = IB_ACCESS_LOCAL_WRITE;
  2242. sig_wr.wr.sig_handover.sig_attrs = &sig_attrs;
  2243. sig_wr.wr.sig_handover.sig_mr = pi_ctx->sig_mr;
  2244. if (se_cmd->t_prot_sg)
  2245. sig_wr.wr.sig_handover.prot = &rdma_wr->ib_sg[PROT];
  2246. if (!wr)
  2247. wr = &sig_wr;
  2248. else
  2249. wr->next = &sig_wr;
  2250. ret = ib_post_send(isert_conn->conn_qp, wr, &bad_wr);
  2251. if (ret) {
  2252. isert_err("fast registration failed, ret:%d\n", ret);
  2253. goto err;
  2254. }
  2255. fr_desc->ind &= ~ISERT_SIG_KEY_VALID;
  2256. rdma_wr->ib_sg[SIG].lkey = pi_ctx->sig_mr->lkey;
  2257. rdma_wr->ib_sg[SIG].addr = 0;
  2258. rdma_wr->ib_sg[SIG].length = se_cmd->data_length;
  2259. if (se_cmd->prot_op != TARGET_PROT_DIN_STRIP &&
  2260. se_cmd->prot_op != TARGET_PROT_DOUT_INSERT)
  2261. /*
  2262. * We have protection guards on the wire
  2263. * so we need to set a larget transfer
  2264. */
  2265. rdma_wr->ib_sg[SIG].length += se_cmd->prot_length;
  2266. isert_dbg("sig_sge: addr: 0x%llx length: %u lkey: %x\n",
  2267. rdma_wr->ib_sg[SIG].addr, rdma_wr->ib_sg[SIG].length,
  2268. rdma_wr->ib_sg[SIG].lkey);
  2269. err:
  2270. return ret;
  2271. }
  2272. static int
  2273. isert_handle_prot_cmd(struct isert_conn *isert_conn,
  2274. struct isert_cmd *isert_cmd,
  2275. struct isert_rdma_wr *wr)
  2276. {
  2277. struct isert_device *device = isert_conn->conn_device;
  2278. struct se_cmd *se_cmd = &isert_cmd->iscsi_cmd->se_cmd;
  2279. int ret;
  2280. if (!wr->fr_desc->pi_ctx) {
  2281. ret = isert_create_pi_ctx(wr->fr_desc,
  2282. device->ib_device,
  2283. device->pd);
  2284. if (ret) {
  2285. isert_err("conn %p failed to allocate pi_ctx\n",
  2286. isert_conn);
  2287. return ret;
  2288. }
  2289. }
  2290. if (se_cmd->t_prot_sg) {
  2291. ret = isert_map_data_buf(isert_conn, isert_cmd,
  2292. se_cmd->t_prot_sg,
  2293. se_cmd->t_prot_nents,
  2294. se_cmd->prot_length,
  2295. 0, wr->iser_ib_op, &wr->prot);
  2296. if (ret) {
  2297. isert_err("conn %p failed to map protection buffer\n",
  2298. isert_conn);
  2299. return ret;
  2300. }
  2301. memset(&wr->ib_sg[PROT], 0, sizeof(wr->ib_sg[PROT]));
  2302. ret = isert_fast_reg_mr(isert_conn, wr->fr_desc, &wr->prot,
  2303. ISERT_PROT_KEY_VALID, &wr->ib_sg[PROT]);
  2304. if (ret) {
  2305. isert_err("conn %p failed to fast reg mr\n",
  2306. isert_conn);
  2307. goto unmap_prot_cmd;
  2308. }
  2309. }
  2310. ret = isert_reg_sig_mr(isert_conn, se_cmd, wr, wr->fr_desc);
  2311. if (ret) {
  2312. isert_err("conn %p failed to fast reg mr\n",
  2313. isert_conn);
  2314. goto unmap_prot_cmd;
  2315. }
  2316. wr->fr_desc->ind |= ISERT_PROTECTED;
  2317. return 0;
  2318. unmap_prot_cmd:
  2319. if (se_cmd->t_prot_sg)
  2320. isert_unmap_data_buf(isert_conn, &wr->prot);
  2321. return ret;
  2322. }
  2323. static int
  2324. isert_reg_rdma(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  2325. struct isert_rdma_wr *wr)
  2326. {
  2327. struct se_cmd *se_cmd = &cmd->se_cmd;
  2328. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  2329. struct isert_conn *isert_conn = conn->context;
  2330. struct fast_reg_descriptor *fr_desc = NULL;
  2331. struct ib_send_wr *send_wr;
  2332. struct ib_sge *ib_sg;
  2333. u32 offset;
  2334. int ret = 0;
  2335. unsigned long flags;
  2336. isert_cmd->tx_desc.isert_cmd = isert_cmd;
  2337. offset = wr->iser_ib_op == ISER_IB_RDMA_READ ? cmd->write_data_done : 0;
  2338. ret = isert_map_data_buf(isert_conn, isert_cmd, se_cmd->t_data_sg,
  2339. se_cmd->t_data_nents, se_cmd->data_length,
  2340. offset, wr->iser_ib_op, &wr->data);
  2341. if (ret)
  2342. return ret;
  2343. if (wr->data.dma_nents != 1 || isert_prot_cmd(isert_conn, se_cmd)) {
  2344. spin_lock_irqsave(&isert_conn->conn_lock, flags);
  2345. fr_desc = list_first_entry(&isert_conn->conn_fr_pool,
  2346. struct fast_reg_descriptor, list);
  2347. list_del(&fr_desc->list);
  2348. spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
  2349. wr->fr_desc = fr_desc;
  2350. }
  2351. ret = isert_fast_reg_mr(isert_conn, fr_desc, &wr->data,
  2352. ISERT_DATA_KEY_VALID, &wr->ib_sg[DATA]);
  2353. if (ret)
  2354. goto unmap_cmd;
  2355. if (isert_prot_cmd(isert_conn, se_cmd)) {
  2356. ret = isert_handle_prot_cmd(isert_conn, isert_cmd, wr);
  2357. if (ret)
  2358. goto unmap_cmd;
  2359. ib_sg = &wr->ib_sg[SIG];
  2360. } else {
  2361. ib_sg = &wr->ib_sg[DATA];
  2362. }
  2363. memcpy(&wr->s_ib_sge, ib_sg, sizeof(*ib_sg));
  2364. wr->ib_sge = &wr->s_ib_sge;
  2365. wr->send_wr_num = 1;
  2366. memset(&wr->s_send_wr, 0, sizeof(*send_wr));
  2367. wr->send_wr = &wr->s_send_wr;
  2368. wr->isert_cmd = isert_cmd;
  2369. send_wr = &isert_cmd->rdma_wr.s_send_wr;
  2370. send_wr->sg_list = &wr->s_ib_sge;
  2371. send_wr->num_sge = 1;
  2372. send_wr->wr_id = (uintptr_t)&isert_cmd->tx_desc;
  2373. if (wr->iser_ib_op == ISER_IB_RDMA_WRITE) {
  2374. send_wr->opcode = IB_WR_RDMA_WRITE;
  2375. send_wr->wr.rdma.remote_addr = isert_cmd->read_va;
  2376. send_wr->wr.rdma.rkey = isert_cmd->read_stag;
  2377. send_wr->send_flags = !isert_prot_cmd(isert_conn, se_cmd) ?
  2378. 0 : IB_SEND_SIGNALED;
  2379. } else {
  2380. send_wr->opcode = IB_WR_RDMA_READ;
  2381. send_wr->wr.rdma.remote_addr = isert_cmd->write_va;
  2382. send_wr->wr.rdma.rkey = isert_cmd->write_stag;
  2383. send_wr->send_flags = IB_SEND_SIGNALED;
  2384. }
  2385. return 0;
  2386. unmap_cmd:
  2387. if (fr_desc) {
  2388. spin_lock_irqsave(&isert_conn->conn_lock, flags);
  2389. list_add_tail(&fr_desc->list, &isert_conn->conn_fr_pool);
  2390. spin_unlock_irqrestore(&isert_conn->conn_lock, flags);
  2391. }
  2392. isert_unmap_data_buf(isert_conn, &wr->data);
  2393. return ret;
  2394. }
  2395. static int
  2396. isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  2397. {
  2398. struct se_cmd *se_cmd = &cmd->se_cmd;
  2399. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  2400. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  2401. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  2402. struct isert_device *device = isert_conn->conn_device;
  2403. struct ib_send_wr *wr_failed;
  2404. int rc;
  2405. isert_dbg("Cmd: %p RDMA_WRITE data_length: %u\n",
  2406. isert_cmd, se_cmd->data_length);
  2407. wr->iser_ib_op = ISER_IB_RDMA_WRITE;
  2408. rc = device->reg_rdma_mem(conn, cmd, wr);
  2409. if (rc) {
  2410. isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
  2411. return rc;
  2412. }
  2413. if (!isert_prot_cmd(isert_conn, se_cmd)) {
  2414. /*
  2415. * Build isert_conn->tx_desc for iSCSI response PDU and attach
  2416. */
  2417. isert_create_send_desc(isert_conn, isert_cmd,
  2418. &isert_cmd->tx_desc);
  2419. iscsit_build_rsp_pdu(cmd, conn, true, (struct iscsi_scsi_rsp *)
  2420. &isert_cmd->tx_desc.iscsi_header);
  2421. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  2422. isert_init_send_wr(isert_conn, isert_cmd,
  2423. &isert_cmd->tx_desc.send_wr);
  2424. isert_cmd->rdma_wr.s_send_wr.next = &isert_cmd->tx_desc.send_wr;
  2425. wr->send_wr_num += 1;
  2426. }
  2427. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  2428. if (rc)
  2429. isert_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
  2430. if (!isert_prot_cmd(isert_conn, se_cmd))
  2431. isert_dbg("Cmd: %p posted RDMA_WRITE + Response for iSER Data "
  2432. "READ\n", isert_cmd);
  2433. else
  2434. isert_dbg("Cmd: %p posted RDMA_WRITE for iSER Data READ\n",
  2435. isert_cmd);
  2436. return 1;
  2437. }
  2438. static int
  2439. isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
  2440. {
  2441. struct se_cmd *se_cmd = &cmd->se_cmd;
  2442. struct isert_cmd *isert_cmd = iscsit_priv_cmd(cmd);
  2443. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  2444. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  2445. struct isert_device *device = isert_conn->conn_device;
  2446. struct ib_send_wr *wr_failed;
  2447. int rc;
  2448. isert_dbg("Cmd: %p RDMA_READ data_length: %u write_data_done: %u\n",
  2449. isert_cmd, se_cmd->data_length, cmd->write_data_done);
  2450. wr->iser_ib_op = ISER_IB_RDMA_READ;
  2451. rc = device->reg_rdma_mem(conn, cmd, wr);
  2452. if (rc) {
  2453. isert_err("Cmd: %p failed to prepare RDMA res\n", isert_cmd);
  2454. return rc;
  2455. }
  2456. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  2457. if (rc)
  2458. isert_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
  2459. isert_dbg("Cmd: %p posted RDMA_READ memory for ISER Data WRITE\n",
  2460. isert_cmd);
  2461. return 0;
  2462. }
  2463. static int
  2464. isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  2465. {
  2466. int ret;
  2467. switch (state) {
  2468. case ISTATE_SEND_NOPIN_WANT_RESPONSE:
  2469. ret = isert_put_nopin(cmd, conn, false);
  2470. break;
  2471. default:
  2472. isert_err("Unknown immediate state: 0x%02x\n", state);
  2473. ret = -EINVAL;
  2474. break;
  2475. }
  2476. return ret;
  2477. }
  2478. static int
  2479. isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  2480. {
  2481. struct isert_conn *isert_conn = conn->context;
  2482. int ret;
  2483. switch (state) {
  2484. case ISTATE_SEND_LOGOUTRSP:
  2485. ret = isert_put_logout_rsp(cmd, conn);
  2486. if (!ret)
  2487. isert_conn->logout_posted = true;
  2488. break;
  2489. case ISTATE_SEND_NOPIN:
  2490. ret = isert_put_nopin(cmd, conn, true);
  2491. break;
  2492. case ISTATE_SEND_TASKMGTRSP:
  2493. ret = isert_put_tm_rsp(cmd, conn);
  2494. break;
  2495. case ISTATE_SEND_REJECT:
  2496. ret = isert_put_reject(cmd, conn);
  2497. break;
  2498. case ISTATE_SEND_TEXTRSP:
  2499. ret = isert_put_text_rsp(cmd, conn);
  2500. break;
  2501. case ISTATE_SEND_STATUS:
  2502. /*
  2503. * Special case for sending non GOOD SCSI status from TX thread
  2504. * context during pre se_cmd excecution failure.
  2505. */
  2506. ret = isert_put_response(conn, cmd);
  2507. break;
  2508. default:
  2509. isert_err("Unknown response state: 0x%02x\n", state);
  2510. ret = -EINVAL;
  2511. break;
  2512. }
  2513. return ret;
  2514. }
  2515. struct rdma_cm_id *
  2516. isert_setup_id(struct isert_np *isert_np)
  2517. {
  2518. struct iscsi_np *np = isert_np->np;
  2519. struct rdma_cm_id *id;
  2520. struct sockaddr *sa;
  2521. int ret;
  2522. sa = (struct sockaddr *)&np->np_sockaddr;
  2523. isert_dbg("ksockaddr: %p, sa: %p\n", &np->np_sockaddr, sa);
  2524. id = rdma_create_id(isert_cma_handler, isert_np,
  2525. RDMA_PS_TCP, IB_QPT_RC);
  2526. if (IS_ERR(id)) {
  2527. isert_err("rdma_create_id() failed: %ld\n", PTR_ERR(id));
  2528. ret = PTR_ERR(id);
  2529. goto out;
  2530. }
  2531. isert_dbg("id %p context %p\n", id, id->context);
  2532. ret = rdma_bind_addr(id, sa);
  2533. if (ret) {
  2534. isert_err("rdma_bind_addr() failed: %d\n", ret);
  2535. goto out_id;
  2536. }
  2537. ret = rdma_listen(id, ISERT_RDMA_LISTEN_BACKLOG);
  2538. if (ret) {
  2539. isert_err("rdma_listen() failed: %d\n", ret);
  2540. goto out_id;
  2541. }
  2542. return id;
  2543. out_id:
  2544. rdma_destroy_id(id);
  2545. out:
  2546. return ERR_PTR(ret);
  2547. }
  2548. static int
  2549. isert_setup_np(struct iscsi_np *np,
  2550. struct __kernel_sockaddr_storage *ksockaddr)
  2551. {
  2552. struct isert_np *isert_np;
  2553. struct rdma_cm_id *isert_lid;
  2554. int ret;
  2555. isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
  2556. if (!isert_np) {
  2557. isert_err("Unable to allocate struct isert_np\n");
  2558. return -ENOMEM;
  2559. }
  2560. sema_init(&isert_np->np_sem, 0);
  2561. mutex_init(&isert_np->np_accept_mutex);
  2562. INIT_LIST_HEAD(&isert_np->np_accept_list);
  2563. init_completion(&isert_np->np_login_comp);
  2564. isert_np->np = np;
  2565. /*
  2566. * Setup the np->np_sockaddr from the passed sockaddr setup
  2567. * in iscsi_target_configfs.c code..
  2568. */
  2569. memcpy(&np->np_sockaddr, ksockaddr,
  2570. sizeof(struct __kernel_sockaddr_storage));
  2571. isert_lid = isert_setup_id(isert_np);
  2572. if (IS_ERR(isert_lid)) {
  2573. ret = PTR_ERR(isert_lid);
  2574. goto out;
  2575. }
  2576. isert_np->np_cm_id = isert_lid;
  2577. np->np_context = isert_np;
  2578. return 0;
  2579. out:
  2580. kfree(isert_np);
  2581. return ret;
  2582. }
  2583. static int
  2584. isert_rdma_accept(struct isert_conn *isert_conn)
  2585. {
  2586. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  2587. struct rdma_conn_param cp;
  2588. int ret;
  2589. memset(&cp, 0, sizeof(struct rdma_conn_param));
  2590. cp.initiator_depth = isert_conn->initiator_depth;
  2591. cp.retry_count = 7;
  2592. cp.rnr_retry_count = 7;
  2593. ret = rdma_accept(cm_id, &cp);
  2594. if (ret) {
  2595. isert_err("rdma_accept() failed with: %d\n", ret);
  2596. return ret;
  2597. }
  2598. return 0;
  2599. }
  2600. static int
  2601. isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
  2602. {
  2603. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  2604. int ret;
  2605. isert_info("before login_req comp conn: %p\n", isert_conn);
  2606. ret = wait_for_completion_interruptible(&isert_conn->login_req_comp);
  2607. if (ret) {
  2608. isert_err("isert_conn %p interrupted before got login req\n",
  2609. isert_conn);
  2610. return ret;
  2611. }
  2612. reinit_completion(&isert_conn->login_req_comp);
  2613. /*
  2614. * For login requests after the first PDU, isert_rx_login_req() will
  2615. * kick schedule_delayed_work(&conn->login_work) as the packet is
  2616. * received, which turns this callback from iscsi_target_do_login_rx()
  2617. * into a NOP.
  2618. */
  2619. if (!login->first_request)
  2620. return 0;
  2621. isert_rx_login_req(isert_conn);
  2622. isert_info("before conn_login_comp conn: %p\n", conn);
  2623. ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
  2624. if (ret)
  2625. return ret;
  2626. isert_info("processing login->req: %p\n", login->req);
  2627. return 0;
  2628. }
  2629. static void
  2630. isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
  2631. struct isert_conn *isert_conn)
  2632. {
  2633. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  2634. struct rdma_route *cm_route = &cm_id->route;
  2635. struct sockaddr_in *sock_in;
  2636. struct sockaddr_in6 *sock_in6;
  2637. conn->login_family = np->np_sockaddr.ss_family;
  2638. if (np->np_sockaddr.ss_family == AF_INET6) {
  2639. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
  2640. snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
  2641. &sock_in6->sin6_addr.in6_u);
  2642. conn->login_port = ntohs(sock_in6->sin6_port);
  2643. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
  2644. snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
  2645. &sock_in6->sin6_addr.in6_u);
  2646. conn->local_port = ntohs(sock_in6->sin6_port);
  2647. } else {
  2648. sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
  2649. sprintf(conn->login_ip, "%pI4",
  2650. &sock_in->sin_addr.s_addr);
  2651. conn->login_port = ntohs(sock_in->sin_port);
  2652. sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
  2653. sprintf(conn->local_ip, "%pI4",
  2654. &sock_in->sin_addr.s_addr);
  2655. conn->local_port = ntohs(sock_in->sin_port);
  2656. }
  2657. }
  2658. static int
  2659. isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
  2660. {
  2661. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  2662. struct isert_conn *isert_conn;
  2663. int max_accept = 0, ret;
  2664. accept_wait:
  2665. ret = down_interruptible(&isert_np->np_sem);
  2666. if (ret || max_accept > 5)
  2667. return -ENODEV;
  2668. spin_lock_bh(&np->np_thread_lock);
  2669. if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
  2670. spin_unlock_bh(&np->np_thread_lock);
  2671. isert_dbg("np_thread_state %d\n",
  2672. np->np_thread_state);
  2673. /**
  2674. * No point in stalling here when np_thread
  2675. * is in state RESET/SHUTDOWN/EXIT - bail
  2676. **/
  2677. return -ENODEV;
  2678. }
  2679. spin_unlock_bh(&np->np_thread_lock);
  2680. mutex_lock(&isert_np->np_accept_mutex);
  2681. if (list_empty(&isert_np->np_accept_list)) {
  2682. mutex_unlock(&isert_np->np_accept_mutex);
  2683. max_accept++;
  2684. goto accept_wait;
  2685. }
  2686. isert_conn = list_first_entry(&isert_np->np_accept_list,
  2687. struct isert_conn, conn_accept_node);
  2688. list_del_init(&isert_conn->conn_accept_node);
  2689. mutex_unlock(&isert_np->np_accept_mutex);
  2690. conn->context = isert_conn;
  2691. isert_conn->conn = conn;
  2692. max_accept = 0;
  2693. isert_set_conn_info(np, conn, isert_conn);
  2694. isert_dbg("Processing isert_conn: %p\n", isert_conn);
  2695. return 0;
  2696. }
  2697. static void
  2698. isert_free_np(struct iscsi_np *np)
  2699. {
  2700. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  2701. struct isert_conn *isert_conn, *n;
  2702. if (isert_np->np_cm_id)
  2703. rdma_destroy_id(isert_np->np_cm_id);
  2704. /*
  2705. * FIXME: At this point we don't have a good way to insure
  2706. * that at this point we don't have hanging connections that
  2707. * completed RDMA establishment but didn't start iscsi login
  2708. * process. So work-around this by cleaning up what ever piled
  2709. * up in np_accept_list.
  2710. */
  2711. mutex_lock(&isert_np->np_accept_mutex);
  2712. if (!list_empty(&isert_np->np_accept_list)) {
  2713. isert_info("Still have isert connections, cleaning up...\n");
  2714. list_for_each_entry_safe(isert_conn, n,
  2715. &isert_np->np_accept_list,
  2716. conn_accept_node) {
  2717. isert_info("cleaning isert_conn %p state (%d)\n",
  2718. isert_conn, isert_conn->state);
  2719. isert_connect_release(isert_conn);
  2720. }
  2721. }
  2722. mutex_unlock(&isert_np->np_accept_mutex);
  2723. np->np_context = NULL;
  2724. kfree(isert_np);
  2725. }
  2726. static void isert_release_work(struct work_struct *work)
  2727. {
  2728. struct isert_conn *isert_conn = container_of(work,
  2729. struct isert_conn,
  2730. release_work);
  2731. isert_info("Starting release conn %p\n", isert_conn);
  2732. wait_for_completion(&isert_conn->conn_wait);
  2733. mutex_lock(&isert_conn->conn_mutex);
  2734. isert_conn->state = ISER_CONN_DOWN;
  2735. mutex_unlock(&isert_conn->conn_mutex);
  2736. isert_info("Destroying conn %p\n", isert_conn);
  2737. isert_put_conn(isert_conn);
  2738. }
  2739. static void
  2740. isert_wait4logout(struct isert_conn *isert_conn)
  2741. {
  2742. struct iscsi_conn *conn = isert_conn->conn;
  2743. isert_info("conn %p\n", isert_conn);
  2744. if (isert_conn->logout_posted) {
  2745. isert_info("conn %p wait for conn_logout_comp\n", isert_conn);
  2746. wait_for_completion_timeout(&conn->conn_logout_comp,
  2747. SECONDS_FOR_LOGOUT_COMP * HZ);
  2748. }
  2749. }
  2750. static void
  2751. isert_wait4cmds(struct iscsi_conn *conn)
  2752. {
  2753. isert_info("iscsi_conn %p\n", conn);
  2754. if (conn->sess) {
  2755. target_sess_cmd_list_set_waiting(conn->sess->se_sess);
  2756. target_wait_for_sess_cmds(conn->sess->se_sess);
  2757. }
  2758. }
  2759. static void
  2760. isert_wait4flush(struct isert_conn *isert_conn)
  2761. {
  2762. struct ib_recv_wr *bad_wr;
  2763. isert_info("conn %p\n", isert_conn);
  2764. init_completion(&isert_conn->conn_wait_comp_err);
  2765. isert_conn->beacon.wr_id = ISER_BEACON_WRID;
  2766. /* post an indication that all flush errors were consumed */
  2767. if (ib_post_recv(isert_conn->conn_qp, &isert_conn->beacon, &bad_wr)) {
  2768. isert_err("conn %p failed to post beacon", isert_conn);
  2769. return;
  2770. }
  2771. wait_for_completion(&isert_conn->conn_wait_comp_err);
  2772. }
  2773. static void isert_wait_conn(struct iscsi_conn *conn)
  2774. {
  2775. struct isert_conn *isert_conn = conn->context;
  2776. isert_info("Starting conn %p\n", isert_conn);
  2777. mutex_lock(&isert_conn->conn_mutex);
  2778. /*
  2779. * Only wait for conn_wait_comp_err if the isert_conn made it
  2780. * into full feature phase..
  2781. */
  2782. if (isert_conn->state == ISER_CONN_INIT) {
  2783. mutex_unlock(&isert_conn->conn_mutex);
  2784. return;
  2785. }
  2786. isert_conn_terminate(isert_conn);
  2787. mutex_unlock(&isert_conn->conn_mutex);
  2788. isert_wait4cmds(conn);
  2789. isert_wait4flush(isert_conn);
  2790. isert_wait4logout(isert_conn);
  2791. INIT_WORK(&isert_conn->release_work, isert_release_work);
  2792. queue_work(isert_release_wq, &isert_conn->release_work);
  2793. }
  2794. static void isert_free_conn(struct iscsi_conn *conn)
  2795. {
  2796. struct isert_conn *isert_conn = conn->context;
  2797. isert_put_conn(isert_conn);
  2798. }
  2799. static struct iscsit_transport iser_target_transport = {
  2800. .name = "IB/iSER",
  2801. .transport_type = ISCSI_INFINIBAND,
  2802. .priv_size = sizeof(struct isert_cmd),
  2803. .owner = THIS_MODULE,
  2804. .iscsit_setup_np = isert_setup_np,
  2805. .iscsit_accept_np = isert_accept_np,
  2806. .iscsit_free_np = isert_free_np,
  2807. .iscsit_wait_conn = isert_wait_conn,
  2808. .iscsit_free_conn = isert_free_conn,
  2809. .iscsit_get_login_rx = isert_get_login_rx,
  2810. .iscsit_put_login_tx = isert_put_login_tx,
  2811. .iscsit_immediate_queue = isert_immediate_queue,
  2812. .iscsit_response_queue = isert_response_queue,
  2813. .iscsit_get_dataout = isert_get_dataout,
  2814. .iscsit_queue_data_in = isert_put_datain,
  2815. .iscsit_queue_status = isert_put_response,
  2816. .iscsit_aborted_task = isert_aborted_task,
  2817. .iscsit_get_sup_prot_ops = isert_get_sup_prot_ops,
  2818. };
  2819. static int __init isert_init(void)
  2820. {
  2821. int ret;
  2822. isert_comp_wq = alloc_workqueue("isert_comp_wq",
  2823. WQ_UNBOUND | WQ_HIGHPRI, 0);
  2824. if (!isert_comp_wq) {
  2825. isert_err("Unable to allocate isert_comp_wq\n");
  2826. ret = -ENOMEM;
  2827. return -ENOMEM;
  2828. }
  2829. isert_release_wq = alloc_workqueue("isert_release_wq", WQ_UNBOUND,
  2830. WQ_UNBOUND_MAX_ACTIVE);
  2831. if (!isert_release_wq) {
  2832. isert_err("Unable to allocate isert_release_wq\n");
  2833. ret = -ENOMEM;
  2834. goto destroy_comp_wq;
  2835. }
  2836. iscsit_register_transport(&iser_target_transport);
  2837. isert_info("iSER_TARGET[0] - Loaded iser_target_transport\n");
  2838. return 0;
  2839. destroy_comp_wq:
  2840. destroy_workqueue(isert_comp_wq);
  2841. return ret;
  2842. }
  2843. static void __exit isert_exit(void)
  2844. {
  2845. flush_scheduled_work();
  2846. destroy_workqueue(isert_release_wq);
  2847. destroy_workqueue(isert_comp_wq);
  2848. iscsit_unregister_transport(&iser_target_transport);
  2849. isert_info("iSER_TARGET[0] - Released iser_target_transport\n");
  2850. }
  2851. MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
  2852. MODULE_VERSION("0.1");
  2853. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  2854. MODULE_LICENSE("GPL");
  2855. module_init(isert_init);
  2856. module_exit(isert_exit);