ib_srpt.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958
  1. /*
  2. * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
  3. * Copyright (C) 2008 - 2011 Bart Van Assche <[email protected]>.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/err.h>
  38. #include <linux/ctype.h>
  39. #include <linux/kthread.h>
  40. #include <linux/string.h>
  41. #include <linux/delay.h>
  42. #include <linux/atomic.h>
  43. #include <linux/inet.h>
  44. #include <rdma/ib_cache.h>
  45. #include <scsi/scsi_proto.h>
  46. #include <scsi/scsi_tcq.h>
  47. #include <target/target_core_base.h>
  48. #include <target/target_core_fabric.h>
  49. #include "ib_srpt.h"
  50. /* Name of this kernel module. */
  51. #define DRV_NAME "ib_srpt"
  52. #define SRPT_ID_STRING "Linux SRP target"
  53. #undef pr_fmt
  54. #define pr_fmt(fmt) DRV_NAME " " fmt
  55. MODULE_AUTHOR("Vu Pham and Bart Van Assche");
  56. MODULE_DESCRIPTION("SCSI RDMA Protocol target driver");
  57. MODULE_LICENSE("Dual BSD/GPL");
  58. /*
  59. * Global Variables
  60. */
  61. static u64 srpt_service_guid;
  62. static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
  63. static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
  64. static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
  65. module_param(srp_max_req_size, int, 0444);
  66. MODULE_PARM_DESC(srp_max_req_size,
  67. "Maximum size of SRP request messages in bytes.");
  68. static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
  69. module_param(srpt_srq_size, int, 0444);
  70. MODULE_PARM_DESC(srpt_srq_size,
  71. "Shared receive queue (SRQ) size.");
  72. static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp)
  73. {
  74. return sprintf(buffer, "0x%016llx\n", *(u64 *)kp->arg);
  75. }
  76. module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
  77. 0444);
  78. MODULE_PARM_DESC(srpt_service_guid,
  79. "Using this value for ioc_guid, id_ext, and cm_listen_id instead of using the node_guid of the first HCA.");
  80. static struct ib_client srpt_client;
  81. /* Protects both rdma_cm_port and rdma_cm_id. */
  82. static DEFINE_MUTEX(rdma_cm_mutex);
  83. /* Port number RDMA/CM will bind to. */
  84. static u16 rdma_cm_port;
  85. static struct rdma_cm_id *rdma_cm_id;
  86. static void srpt_release_cmd(struct se_cmd *se_cmd);
  87. static void srpt_free_ch(struct kref *kref);
  88. static int srpt_queue_status(struct se_cmd *cmd);
  89. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  90. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
  91. static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
  92. /*
  93. * The only allowed channel state changes are those that change the channel
  94. * state into a state with a higher numerical value. Hence the new > prev test.
  95. */
  96. static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
  97. {
  98. unsigned long flags;
  99. enum rdma_ch_state prev;
  100. bool changed = false;
  101. spin_lock_irqsave(&ch->spinlock, flags);
  102. prev = ch->state;
  103. if (new > prev) {
  104. ch->state = new;
  105. changed = true;
  106. }
  107. spin_unlock_irqrestore(&ch->spinlock, flags);
  108. return changed;
  109. }
  110. /**
  111. * srpt_event_handler - asynchronous IB event callback function
  112. * @handler: IB event handler registered by ib_register_event_handler().
  113. * @event: Description of the event that occurred.
  114. *
  115. * Callback function called by the InfiniBand core when an asynchronous IB
  116. * event occurs. This callback may occur in interrupt context. See also
  117. * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
  118. * Architecture Specification.
  119. */
  120. static void srpt_event_handler(struct ib_event_handler *handler,
  121. struct ib_event *event)
  122. {
  123. struct srpt_device *sdev =
  124. container_of(handler, struct srpt_device, event_handler);
  125. struct srpt_port *sport;
  126. u8 port_num;
  127. pr_debug("ASYNC event= %d on device= %s\n", event->event,
  128. dev_name(&sdev->device->dev));
  129. switch (event->event) {
  130. case IB_EVENT_PORT_ERR:
  131. port_num = event->element.port_num - 1;
  132. if (port_num < sdev->device->phys_port_cnt) {
  133. sport = &sdev->port[port_num];
  134. sport->lid = 0;
  135. sport->sm_lid = 0;
  136. } else {
  137. WARN(true, "event %d: port_num %d out of range 1..%d\n",
  138. event->event, port_num + 1,
  139. sdev->device->phys_port_cnt);
  140. }
  141. break;
  142. case IB_EVENT_PORT_ACTIVE:
  143. case IB_EVENT_LID_CHANGE:
  144. case IB_EVENT_PKEY_CHANGE:
  145. case IB_EVENT_SM_CHANGE:
  146. case IB_EVENT_CLIENT_REREGISTER:
  147. case IB_EVENT_GID_CHANGE:
  148. /* Refresh port data asynchronously. */
  149. port_num = event->element.port_num - 1;
  150. if (port_num < sdev->device->phys_port_cnt) {
  151. sport = &sdev->port[port_num];
  152. if (!sport->lid && !sport->sm_lid)
  153. schedule_work(&sport->work);
  154. } else {
  155. WARN(true, "event %d: port_num %d out of range 1..%d\n",
  156. event->event, port_num + 1,
  157. sdev->device->phys_port_cnt);
  158. }
  159. break;
  160. default:
  161. pr_err("received unrecognized IB event %d\n", event->event);
  162. break;
  163. }
  164. }
  165. /**
  166. * srpt_srq_event - SRQ event callback function
  167. * @event: Description of the event that occurred.
  168. * @ctx: Context pointer specified at SRQ creation time.
  169. */
  170. static void srpt_srq_event(struct ib_event *event, void *ctx)
  171. {
  172. pr_debug("SRQ event %d\n", event->event);
  173. }
  174. static const char *get_ch_state_name(enum rdma_ch_state s)
  175. {
  176. switch (s) {
  177. case CH_CONNECTING:
  178. return "connecting";
  179. case CH_LIVE:
  180. return "live";
  181. case CH_DISCONNECTING:
  182. return "disconnecting";
  183. case CH_DRAINING:
  184. return "draining";
  185. case CH_DISCONNECTED:
  186. return "disconnected";
  187. }
  188. return "???";
  189. }
  190. /**
  191. * srpt_qp_event - QP event callback function
  192. * @event: Description of the event that occurred.
  193. * @ch: SRPT RDMA channel.
  194. */
  195. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  196. {
  197. pr_debug("QP event %d on ch=%p sess_name=%s-%d state=%s\n",
  198. event->event, ch, ch->sess_name, ch->qp->qp_num,
  199. get_ch_state_name(ch->state));
  200. switch (event->event) {
  201. case IB_EVENT_COMM_EST:
  202. if (ch->using_rdma_cm)
  203. rdma_notify(ch->rdma_cm.cm_id, event->event);
  204. else
  205. ib_cm_notify(ch->ib_cm.cm_id, event->event);
  206. break;
  207. case IB_EVENT_QP_LAST_WQE_REACHED:
  208. pr_debug("%s-%d, state %s: received Last WQE event.\n",
  209. ch->sess_name, ch->qp->qp_num,
  210. get_ch_state_name(ch->state));
  211. break;
  212. default:
  213. pr_err("received unrecognized IB QP event %d\n", event->event);
  214. break;
  215. }
  216. }
  217. /**
  218. * srpt_set_ioc - initialize a IOUnitInfo structure
  219. * @c_list: controller list.
  220. * @slot: one-based slot number.
  221. * @value: four-bit value.
  222. *
  223. * Copies the lowest four bits of value in element slot of the array of four
  224. * bit elements called c_list (controller list). The index slot is one-based.
  225. */
  226. static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
  227. {
  228. u16 id;
  229. u8 tmp;
  230. id = (slot - 1) / 2;
  231. if (slot & 0x1) {
  232. tmp = c_list[id] & 0xf;
  233. c_list[id] = (value << 4) | tmp;
  234. } else {
  235. tmp = c_list[id] & 0xf0;
  236. c_list[id] = (value & 0xf) | tmp;
  237. }
  238. }
  239. /**
  240. * srpt_get_class_port_info - copy ClassPortInfo to a management datagram
  241. * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO.
  242. *
  243. * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
  244. * Specification.
  245. */
  246. static void srpt_get_class_port_info(struct ib_dm_mad *mad)
  247. {
  248. struct ib_class_port_info *cif;
  249. cif = (struct ib_class_port_info *)mad->data;
  250. memset(cif, 0, sizeof(*cif));
  251. cif->base_version = 1;
  252. cif->class_version = 1;
  253. ib_set_cpi_resp_time(cif, 20);
  254. mad->mad_hdr.status = 0;
  255. }
  256. /**
  257. * srpt_get_iou - write IOUnitInfo to a management datagram
  258. * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO.
  259. *
  260. * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
  261. * Specification. See also section B.7, table B.6 in the SRP r16a document.
  262. */
  263. static void srpt_get_iou(struct ib_dm_mad *mad)
  264. {
  265. struct ib_dm_iou_info *ioui;
  266. u8 slot;
  267. int i;
  268. ioui = (struct ib_dm_iou_info *)mad->data;
  269. ioui->change_id = cpu_to_be16(1);
  270. ioui->max_controllers = 16;
  271. /* set present for slot 1 and empty for the rest */
  272. srpt_set_ioc(ioui->controller_list, 1, 1);
  273. for (i = 1, slot = 2; i < 16; i++, slot++)
  274. srpt_set_ioc(ioui->controller_list, slot, 0);
  275. mad->mad_hdr.status = 0;
  276. }
  277. /**
  278. * srpt_get_ioc - write IOControllerprofile to a management datagram
  279. * @sport: HCA port through which the MAD has been received.
  280. * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query.
  281. * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE.
  282. *
  283. * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
  284. * Architecture Specification. See also section B.7, table B.7 in the SRP
  285. * r16a document.
  286. */
  287. static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
  288. struct ib_dm_mad *mad)
  289. {
  290. struct srpt_device *sdev = sport->sdev;
  291. struct ib_dm_ioc_profile *iocp;
  292. int send_queue_depth;
  293. iocp = (struct ib_dm_ioc_profile *)mad->data;
  294. if (!slot || slot > 16) {
  295. mad->mad_hdr.status
  296. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  297. return;
  298. }
  299. if (slot > 2) {
  300. mad->mad_hdr.status
  301. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  302. return;
  303. }
  304. if (sdev->use_srq)
  305. send_queue_depth = sdev->srq_size;
  306. else
  307. send_queue_depth = min(MAX_SRPT_RQ_SIZE,
  308. sdev->device->attrs.max_qp_wr);
  309. memset(iocp, 0, sizeof(*iocp));
  310. strcpy(iocp->id_string, SRPT_ID_STRING);
  311. iocp->guid = cpu_to_be64(srpt_service_guid);
  312. iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  313. iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
  314. iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
  315. iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  316. iocp->subsys_device_id = 0x0;
  317. iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
  318. iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
  319. iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
  320. iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
  321. iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
  322. iocp->rdma_read_depth = 4;
  323. iocp->send_size = cpu_to_be32(srp_max_req_size);
  324. iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
  325. 1U << 24));
  326. iocp->num_svc_entries = 1;
  327. iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
  328. SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
  329. mad->mad_hdr.status = 0;
  330. }
  331. /**
  332. * srpt_get_svc_entries - write ServiceEntries to a management datagram
  333. * @ioc_guid: I/O controller GUID to use in reply.
  334. * @slot: I/O controller number.
  335. * @hi: End of the range of service entries to be specified in the reply.
  336. * @lo: Start of the range of service entries to be specified in the reply..
  337. * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES.
  338. *
  339. * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
  340. * Specification. See also section B.7, table B.8 in the SRP r16a document.
  341. */
  342. static void srpt_get_svc_entries(u64 ioc_guid,
  343. u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
  344. {
  345. struct ib_dm_svc_entries *svc_entries;
  346. WARN_ON(!ioc_guid);
  347. if (!slot || slot > 16) {
  348. mad->mad_hdr.status
  349. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  350. return;
  351. }
  352. if (slot > 2 || lo > hi || hi > 1) {
  353. mad->mad_hdr.status
  354. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  355. return;
  356. }
  357. svc_entries = (struct ib_dm_svc_entries *)mad->data;
  358. memset(svc_entries, 0, sizeof(*svc_entries));
  359. svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
  360. snprintf(svc_entries->service_entries[0].name,
  361. sizeof(svc_entries->service_entries[0].name),
  362. "%s%016llx",
  363. SRP_SERVICE_NAME_PREFIX,
  364. ioc_guid);
  365. mad->mad_hdr.status = 0;
  366. }
  367. /**
  368. * srpt_mgmt_method_get - process a received management datagram
  369. * @sp: HCA port through which the MAD has been received.
  370. * @rq_mad: received MAD.
  371. * @rsp_mad: response MAD.
  372. */
  373. static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
  374. struct ib_dm_mad *rsp_mad)
  375. {
  376. u16 attr_id;
  377. u32 slot;
  378. u8 hi, lo;
  379. attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
  380. switch (attr_id) {
  381. case DM_ATTR_CLASS_PORT_INFO:
  382. srpt_get_class_port_info(rsp_mad);
  383. break;
  384. case DM_ATTR_IOU_INFO:
  385. srpt_get_iou(rsp_mad);
  386. break;
  387. case DM_ATTR_IOC_PROFILE:
  388. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  389. srpt_get_ioc(sp, slot, rsp_mad);
  390. break;
  391. case DM_ATTR_SVC_ENTRIES:
  392. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  393. hi = (u8) ((slot >> 8) & 0xff);
  394. lo = (u8) (slot & 0xff);
  395. slot = (u16) ((slot >> 16) & 0xffff);
  396. srpt_get_svc_entries(srpt_service_guid,
  397. slot, hi, lo, rsp_mad);
  398. break;
  399. default:
  400. rsp_mad->mad_hdr.status =
  401. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  402. break;
  403. }
  404. }
  405. /**
  406. * srpt_mad_send_handler - MAD send completion callback
  407. * @mad_agent: Return value of ib_register_mad_agent().
  408. * @mad_wc: Work completion reporting that the MAD has been sent.
  409. */
  410. static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
  411. struct ib_mad_send_wc *mad_wc)
  412. {
  413. rdma_destroy_ah(mad_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE);
  414. ib_free_send_mad(mad_wc->send_buf);
  415. }
  416. /**
  417. * srpt_mad_recv_handler - MAD reception callback function
  418. * @mad_agent: Return value of ib_register_mad_agent().
  419. * @send_buf: Not used.
  420. * @mad_wc: Work completion reporting that a MAD has been received.
  421. */
  422. static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
  423. struct ib_mad_send_buf *send_buf,
  424. struct ib_mad_recv_wc *mad_wc)
  425. {
  426. struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
  427. struct ib_ah *ah;
  428. struct ib_mad_send_buf *rsp;
  429. struct ib_dm_mad *dm_mad;
  430. if (!mad_wc || !mad_wc->recv_buf.mad)
  431. return;
  432. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  433. mad_wc->recv_buf.grh, mad_agent->port_num);
  434. if (IS_ERR(ah))
  435. goto err;
  436. BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
  437. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  438. mad_wc->wc->pkey_index, 0,
  439. IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
  440. GFP_KERNEL,
  441. IB_MGMT_BASE_VERSION);
  442. if (IS_ERR(rsp))
  443. goto err_rsp;
  444. rsp->ah = ah;
  445. dm_mad = rsp->mad;
  446. memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
  447. dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  448. dm_mad->mad_hdr.status = 0;
  449. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  450. case IB_MGMT_METHOD_GET:
  451. srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
  452. break;
  453. case IB_MGMT_METHOD_SET:
  454. dm_mad->mad_hdr.status =
  455. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  456. break;
  457. default:
  458. dm_mad->mad_hdr.status =
  459. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
  460. break;
  461. }
  462. if (!ib_post_send_mad(rsp, NULL)) {
  463. ib_free_recv_mad(mad_wc);
  464. /* will destroy_ah & free_send_mad in send completion */
  465. return;
  466. }
  467. ib_free_send_mad(rsp);
  468. err_rsp:
  469. rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
  470. err:
  471. ib_free_recv_mad(mad_wc);
  472. }
  473. static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid)
  474. {
  475. const __be16 *g = (const __be16 *)guid;
  476. return snprintf(buf, size, "%04x:%04x:%04x:%04x",
  477. be16_to_cpu(g[0]), be16_to_cpu(g[1]),
  478. be16_to_cpu(g[2]), be16_to_cpu(g[3]));
  479. }
  480. /**
  481. * srpt_refresh_port - configure a HCA port
  482. * @sport: SRPT HCA port.
  483. *
  484. * Enable InfiniBand management datagram processing, update the cached sm_lid,
  485. * lid and gid values, and register a callback function for processing MADs
  486. * on the specified port.
  487. *
  488. * Note: It is safe to call this function more than once for the same port.
  489. */
  490. static int srpt_refresh_port(struct srpt_port *sport)
  491. {
  492. struct ib_mad_agent *mad_agent;
  493. struct ib_mad_reg_req reg_req;
  494. struct ib_port_modify port_modify;
  495. struct ib_port_attr port_attr;
  496. int ret;
  497. ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
  498. if (ret)
  499. return ret;
  500. sport->sm_lid = port_attr.sm_lid;
  501. sport->lid = port_attr.lid;
  502. ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
  503. if (ret)
  504. return ret;
  505. srpt_format_guid(sport->guid_name, ARRAY_SIZE(sport->guid_name),
  506. &sport->gid.global.interface_id);
  507. snprintf(sport->gid_name, ARRAY_SIZE(sport->gid_name),
  508. "0x%016llx%016llx",
  509. be64_to_cpu(sport->gid.global.subnet_prefix),
  510. be64_to_cpu(sport->gid.global.interface_id));
  511. if (rdma_protocol_iwarp(sport->sdev->device, sport->port))
  512. return 0;
  513. memset(&port_modify, 0, sizeof(port_modify));
  514. port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  515. port_modify.clr_port_cap_mask = 0;
  516. ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  517. if (ret) {
  518. pr_warn("%s-%d: enabling device management failed (%d). Note: this is expected if SR-IOV is enabled.\n",
  519. dev_name(&sport->sdev->device->dev), sport->port, ret);
  520. return 0;
  521. }
  522. if (!sport->mad_agent) {
  523. memset(&reg_req, 0, sizeof(reg_req));
  524. reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
  525. reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
  526. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  527. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  528. mad_agent = ib_register_mad_agent(sport->sdev->device,
  529. sport->port,
  530. IB_QPT_GSI,
  531. &reg_req, 0,
  532. srpt_mad_send_handler,
  533. srpt_mad_recv_handler,
  534. sport, 0);
  535. if (IS_ERR(mad_agent)) {
  536. pr_err("%s-%d: MAD agent registration failed (%ld). Note: this is expected if SR-IOV is enabled.\n",
  537. dev_name(&sport->sdev->device->dev), sport->port,
  538. PTR_ERR(mad_agent));
  539. sport->mad_agent = NULL;
  540. memset(&port_modify, 0, sizeof(port_modify));
  541. port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  542. ib_modify_port(sport->sdev->device, sport->port, 0,
  543. &port_modify);
  544. return 0;
  545. }
  546. sport->mad_agent = mad_agent;
  547. }
  548. return 0;
  549. }
  550. /**
  551. * srpt_unregister_mad_agent - unregister MAD callback functions
  552. * @sdev: SRPT HCA pointer.
  553. * @port_cnt: number of ports with registered MAD
  554. *
  555. * Note: It is safe to call this function more than once for the same device.
  556. */
  557. static void srpt_unregister_mad_agent(struct srpt_device *sdev, int port_cnt)
  558. {
  559. struct ib_port_modify port_modify = {
  560. .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
  561. };
  562. struct srpt_port *sport;
  563. int i;
  564. for (i = 1; i <= port_cnt; i++) {
  565. sport = &sdev->port[i - 1];
  566. WARN_ON(sport->port != i);
  567. if (sport->mad_agent) {
  568. ib_modify_port(sdev->device, i, 0, &port_modify);
  569. ib_unregister_mad_agent(sport->mad_agent);
  570. sport->mad_agent = NULL;
  571. }
  572. }
  573. }
  574. /**
  575. * srpt_alloc_ioctx - allocate a SRPT I/O context structure
  576. * @sdev: SRPT HCA pointer.
  577. * @ioctx_size: I/O context size.
  578. * @buf_cache: I/O buffer cache.
  579. * @dir: DMA data direction.
  580. */
  581. static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
  582. int ioctx_size,
  583. struct kmem_cache *buf_cache,
  584. enum dma_data_direction dir)
  585. {
  586. struct srpt_ioctx *ioctx;
  587. ioctx = kzalloc(ioctx_size, GFP_KERNEL);
  588. if (!ioctx)
  589. goto err;
  590. ioctx->buf = kmem_cache_alloc(buf_cache, GFP_KERNEL);
  591. if (!ioctx->buf)
  592. goto err_free_ioctx;
  593. ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf,
  594. kmem_cache_size(buf_cache), dir);
  595. if (ib_dma_mapping_error(sdev->device, ioctx->dma))
  596. goto err_free_buf;
  597. return ioctx;
  598. err_free_buf:
  599. kmem_cache_free(buf_cache, ioctx->buf);
  600. err_free_ioctx:
  601. kfree(ioctx);
  602. err:
  603. return NULL;
  604. }
  605. /**
  606. * srpt_free_ioctx - free a SRPT I/O context structure
  607. * @sdev: SRPT HCA pointer.
  608. * @ioctx: I/O context pointer.
  609. * @buf_cache: I/O buffer cache.
  610. * @dir: DMA data direction.
  611. */
  612. static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
  613. struct kmem_cache *buf_cache,
  614. enum dma_data_direction dir)
  615. {
  616. if (!ioctx)
  617. return;
  618. ib_dma_unmap_single(sdev->device, ioctx->dma,
  619. kmem_cache_size(buf_cache), dir);
  620. kmem_cache_free(buf_cache, ioctx->buf);
  621. kfree(ioctx);
  622. }
  623. /**
  624. * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures
  625. * @sdev: Device to allocate the I/O context ring for.
  626. * @ring_size: Number of elements in the I/O context ring.
  627. * @ioctx_size: I/O context size.
  628. * @buf_cache: I/O buffer cache.
  629. * @alignment_offset: Offset in each ring buffer at which the SRP information
  630. * unit starts.
  631. * @dir: DMA data direction.
  632. */
  633. static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
  634. int ring_size, int ioctx_size,
  635. struct kmem_cache *buf_cache,
  636. int alignment_offset,
  637. enum dma_data_direction dir)
  638. {
  639. struct srpt_ioctx **ring;
  640. int i;
  641. WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx) &&
  642. ioctx_size != sizeof(struct srpt_send_ioctx));
  643. ring = kvmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL);
  644. if (!ring)
  645. goto out;
  646. for (i = 0; i < ring_size; ++i) {
  647. ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, buf_cache, dir);
  648. if (!ring[i])
  649. goto err;
  650. ring[i]->index = i;
  651. ring[i]->offset = alignment_offset;
  652. }
  653. goto out;
  654. err:
  655. while (--i >= 0)
  656. srpt_free_ioctx(sdev, ring[i], buf_cache, dir);
  657. kvfree(ring);
  658. ring = NULL;
  659. out:
  660. return ring;
  661. }
  662. /**
  663. * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures
  664. * @ioctx_ring: I/O context ring to be freed.
  665. * @sdev: SRPT HCA pointer.
  666. * @ring_size: Number of ring elements.
  667. * @buf_cache: I/O buffer cache.
  668. * @dir: DMA data direction.
  669. */
  670. static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
  671. struct srpt_device *sdev, int ring_size,
  672. struct kmem_cache *buf_cache,
  673. enum dma_data_direction dir)
  674. {
  675. int i;
  676. if (!ioctx_ring)
  677. return;
  678. for (i = 0; i < ring_size; ++i)
  679. srpt_free_ioctx(sdev, ioctx_ring[i], buf_cache, dir);
  680. kvfree(ioctx_ring);
  681. }
  682. /**
  683. * srpt_set_cmd_state - set the state of a SCSI command
  684. * @ioctx: Send I/O context.
  685. * @new: New I/O context state.
  686. *
  687. * Does not modify the state of aborted commands. Returns the previous command
  688. * state.
  689. */
  690. static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
  691. enum srpt_command_state new)
  692. {
  693. enum srpt_command_state previous;
  694. previous = ioctx->state;
  695. if (previous != SRPT_STATE_DONE)
  696. ioctx->state = new;
  697. return previous;
  698. }
  699. /**
  700. * srpt_test_and_set_cmd_state - test and set the state of a command
  701. * @ioctx: Send I/O context.
  702. * @old: Current I/O context state.
  703. * @new: New I/O context state.
  704. *
  705. * Returns true if and only if the previous command state was equal to 'old'.
  706. */
  707. static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
  708. enum srpt_command_state old,
  709. enum srpt_command_state new)
  710. {
  711. enum srpt_command_state previous;
  712. WARN_ON(!ioctx);
  713. WARN_ON(old == SRPT_STATE_DONE);
  714. WARN_ON(new == SRPT_STATE_NEW);
  715. previous = ioctx->state;
  716. if (previous == old)
  717. ioctx->state = new;
  718. return previous == old;
  719. }
  720. /**
  721. * srpt_post_recv - post an IB receive request
  722. * @sdev: SRPT HCA pointer.
  723. * @ch: SRPT RDMA channel.
  724. * @ioctx: Receive I/O context pointer.
  725. */
  726. static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
  727. struct srpt_recv_ioctx *ioctx)
  728. {
  729. struct ib_sge list;
  730. struct ib_recv_wr wr;
  731. BUG_ON(!sdev);
  732. list.addr = ioctx->ioctx.dma + ioctx->ioctx.offset;
  733. list.length = srp_max_req_size;
  734. list.lkey = sdev->lkey;
  735. ioctx->ioctx.cqe.done = srpt_recv_done;
  736. wr.wr_cqe = &ioctx->ioctx.cqe;
  737. wr.next = NULL;
  738. wr.sg_list = &list;
  739. wr.num_sge = 1;
  740. if (sdev->use_srq)
  741. return ib_post_srq_recv(sdev->srq, &wr, NULL);
  742. else
  743. return ib_post_recv(ch->qp, &wr, NULL);
  744. }
  745. /**
  746. * srpt_zerolength_write - perform a zero-length RDMA write
  747. * @ch: SRPT RDMA channel.
  748. *
  749. * A quote from the InfiniBand specification: C9-88: For an HCA responder
  750. * using Reliable Connection service, for each zero-length RDMA READ or WRITE
  751. * request, the R_Key shall not be validated, even if the request includes
  752. * Immediate data.
  753. */
  754. static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
  755. {
  756. struct ib_rdma_wr wr = {
  757. .wr = {
  758. .next = NULL,
  759. { .wr_cqe = &ch->zw_cqe, },
  760. .opcode = IB_WR_RDMA_WRITE,
  761. .send_flags = IB_SEND_SIGNALED,
  762. }
  763. };
  764. pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
  765. ch->qp->qp_num);
  766. return ib_post_send(ch->qp, &wr.wr, NULL);
  767. }
  768. static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
  769. {
  770. struct srpt_rdma_ch *ch = wc->qp->qp_context;
  771. pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num,
  772. wc->status);
  773. if (wc->status == IB_WC_SUCCESS) {
  774. srpt_process_wait_list(ch);
  775. } else {
  776. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  777. schedule_work(&ch->release_work);
  778. else
  779. pr_debug("%s-%d: already disconnected.\n",
  780. ch->sess_name, ch->qp->qp_num);
  781. }
  782. }
  783. static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
  784. struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
  785. unsigned *sg_cnt)
  786. {
  787. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  788. struct srpt_rdma_ch *ch = ioctx->ch;
  789. struct scatterlist *prev = NULL;
  790. unsigned prev_nents;
  791. int ret, i;
  792. if (nbufs == 1) {
  793. ioctx->rw_ctxs = &ioctx->s_rw_ctx;
  794. } else {
  795. ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
  796. GFP_KERNEL);
  797. if (!ioctx->rw_ctxs)
  798. return -ENOMEM;
  799. }
  800. for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
  801. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  802. u64 remote_addr = be64_to_cpu(db->va);
  803. u32 size = be32_to_cpu(db->len);
  804. u32 rkey = be32_to_cpu(db->key);
  805. ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
  806. i < nbufs - 1);
  807. if (ret)
  808. goto unwind;
  809. ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
  810. ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
  811. if (ret < 0) {
  812. target_free_sgl(ctx->sg, ctx->nents);
  813. goto unwind;
  814. }
  815. ioctx->n_rdma += ret;
  816. ioctx->n_rw_ctx++;
  817. if (prev) {
  818. sg_unmark_end(&prev[prev_nents - 1]);
  819. sg_chain(prev, prev_nents + 1, ctx->sg);
  820. } else {
  821. *sg = ctx->sg;
  822. }
  823. prev = ctx->sg;
  824. prev_nents = ctx->nents;
  825. *sg_cnt += ctx->nents;
  826. }
  827. return 0;
  828. unwind:
  829. while (--i >= 0) {
  830. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  831. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  832. ctx->sg, ctx->nents, dir);
  833. target_free_sgl(ctx->sg, ctx->nents);
  834. }
  835. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  836. kfree(ioctx->rw_ctxs);
  837. return ret;
  838. }
  839. static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
  840. struct srpt_send_ioctx *ioctx)
  841. {
  842. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  843. int i;
  844. for (i = 0; i < ioctx->n_rw_ctx; i++) {
  845. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  846. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  847. ctx->sg, ctx->nents, dir);
  848. target_free_sgl(ctx->sg, ctx->nents);
  849. }
  850. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  851. kfree(ioctx->rw_ctxs);
  852. }
  853. static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
  854. {
  855. /*
  856. * The pointer computations below will only be compiled correctly
  857. * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
  858. * whether srp_cmd::add_data has been declared as a byte pointer.
  859. */
  860. BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
  861. !__same_type(srp_cmd->add_data[0], (u8)0));
  862. /*
  863. * According to the SRP spec, the lower two bits of the 'ADDITIONAL
  864. * CDB LENGTH' field are reserved and the size in bytes of this field
  865. * is four times the value specified in bits 3..7. Hence the "& ~3".
  866. */
  867. return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
  868. }
  869. /**
  870. * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request
  871. * @recv_ioctx: I/O context associated with the received command @srp_cmd.
  872. * @ioctx: I/O context that will be used for responding to the initiator.
  873. * @srp_cmd: Pointer to the SRP_CMD request data.
  874. * @dir: Pointer to the variable to which the transfer direction will be
  875. * written.
  876. * @sg: [out] scatterlist for the parsed SRP_CMD.
  877. * @sg_cnt: [out] length of @sg.
  878. * @data_len: Pointer to the variable to which the total data length of all
  879. * descriptors in the SRP_CMD request will be written.
  880. * @imm_data_offset: [in] Offset in SRP_CMD requests at which immediate data
  881. * starts.
  882. *
  883. * This function initializes ioctx->nrbuf and ioctx->r_bufs.
  884. *
  885. * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
  886. * -ENOMEM when memory allocation fails and zero upon success.
  887. */
  888. static int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx,
  889. struct srpt_send_ioctx *ioctx,
  890. struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
  891. struct scatterlist **sg, unsigned int *sg_cnt, u64 *data_len,
  892. u16 imm_data_offset)
  893. {
  894. BUG_ON(!dir);
  895. BUG_ON(!data_len);
  896. /*
  897. * The lower four bits of the buffer format field contain the DATA-IN
  898. * buffer descriptor format, and the highest four bits contain the
  899. * DATA-OUT buffer descriptor format.
  900. */
  901. if (srp_cmd->buf_fmt & 0xf)
  902. /* DATA-IN: transfer data from target to initiator (read). */
  903. *dir = DMA_FROM_DEVICE;
  904. else if (srp_cmd->buf_fmt >> 4)
  905. /* DATA-OUT: transfer data from initiator to target (write). */
  906. *dir = DMA_TO_DEVICE;
  907. else
  908. *dir = DMA_NONE;
  909. /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
  910. ioctx->cmd.data_direction = *dir;
  911. if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
  912. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
  913. struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
  914. *data_len = be32_to_cpu(db->len);
  915. return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
  916. } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
  917. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
  918. struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
  919. int nbufs = be32_to_cpu(idb->table_desc.len) /
  920. sizeof(struct srp_direct_buf);
  921. if (nbufs >
  922. (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
  923. pr_err("received unsupported SRP_CMD request type (%u out + %u in != %u / %zu)\n",
  924. srp_cmd->data_out_desc_cnt,
  925. srp_cmd->data_in_desc_cnt,
  926. be32_to_cpu(idb->table_desc.len),
  927. sizeof(struct srp_direct_buf));
  928. return -EINVAL;
  929. }
  930. *data_len = be32_to_cpu(idb->len);
  931. return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
  932. sg, sg_cnt);
  933. } else if ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_IMM) {
  934. struct srp_imm_buf *imm_buf = srpt_get_desc_buf(srp_cmd);
  935. void *data = (void *)srp_cmd + imm_data_offset;
  936. uint32_t len = be32_to_cpu(imm_buf->len);
  937. uint32_t req_size = imm_data_offset + len;
  938. if (req_size > srp_max_req_size) {
  939. pr_err("Immediate data (length %d + %d) exceeds request size %d\n",
  940. imm_data_offset, len, srp_max_req_size);
  941. return -EINVAL;
  942. }
  943. if (recv_ioctx->byte_len < req_size) {
  944. pr_err("Received too few data - %d < %d\n",
  945. recv_ioctx->byte_len, req_size);
  946. return -EIO;
  947. }
  948. /*
  949. * The immediate data buffer descriptor must occur before the
  950. * immediate data itself.
  951. */
  952. if ((void *)(imm_buf + 1) > (void *)data) {
  953. pr_err("Received invalid write request\n");
  954. return -EINVAL;
  955. }
  956. *data_len = len;
  957. ioctx->recv_ioctx = recv_ioctx;
  958. if ((uintptr_t)data & 511) {
  959. pr_warn_once("Internal error - the receive buffers are not aligned properly.\n");
  960. return -EINVAL;
  961. }
  962. sg_init_one(&ioctx->imm_sg, data, len);
  963. *sg = &ioctx->imm_sg;
  964. *sg_cnt = 1;
  965. return 0;
  966. } else {
  967. *data_len = 0;
  968. return 0;
  969. }
  970. }
  971. /**
  972. * srpt_init_ch_qp - initialize queue pair attributes
  973. * @ch: SRPT RDMA channel.
  974. * @qp: Queue pair pointer.
  975. *
  976. * Initialized the attributes of queue pair 'qp' by allowing local write,
  977. * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
  978. */
  979. static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  980. {
  981. struct ib_qp_attr *attr;
  982. int ret;
  983. WARN_ON_ONCE(ch->using_rdma_cm);
  984. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  985. if (!attr)
  986. return -ENOMEM;
  987. attr->qp_state = IB_QPS_INIT;
  988. attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
  989. attr->port_num = ch->sport->port;
  990. ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port,
  991. ch->pkey, &attr->pkey_index);
  992. if (ret < 0)
  993. pr_err("Translating pkey %#x failed (%d) - using index 0\n",
  994. ch->pkey, ret);
  995. ret = ib_modify_qp(qp, attr,
  996. IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
  997. IB_QP_PKEY_INDEX);
  998. kfree(attr);
  999. return ret;
  1000. }
  1001. /**
  1002. * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR)
  1003. * @ch: channel of the queue pair.
  1004. * @qp: queue pair to change the state of.
  1005. *
  1006. * Returns zero upon success and a negative value upon failure.
  1007. *
  1008. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  1009. * If this structure ever becomes larger, it might be necessary to allocate
  1010. * it dynamically instead of on the stack.
  1011. */
  1012. static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  1013. {
  1014. struct ib_qp_attr qp_attr;
  1015. int attr_mask;
  1016. int ret;
  1017. WARN_ON_ONCE(ch->using_rdma_cm);
  1018. qp_attr.qp_state = IB_QPS_RTR;
  1019. ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
  1020. if (ret)
  1021. goto out;
  1022. qp_attr.max_dest_rd_atomic = 4;
  1023. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  1024. out:
  1025. return ret;
  1026. }
  1027. /**
  1028. * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS)
  1029. * @ch: channel of the queue pair.
  1030. * @qp: queue pair to change the state of.
  1031. *
  1032. * Returns zero upon success and a negative value upon failure.
  1033. *
  1034. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  1035. * If this structure ever becomes larger, it might be necessary to allocate
  1036. * it dynamically instead of on the stack.
  1037. */
  1038. static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  1039. {
  1040. struct ib_qp_attr qp_attr;
  1041. int attr_mask;
  1042. int ret;
  1043. qp_attr.qp_state = IB_QPS_RTS;
  1044. ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
  1045. if (ret)
  1046. goto out;
  1047. qp_attr.max_rd_atomic = 4;
  1048. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  1049. out:
  1050. return ret;
  1051. }
  1052. /**
  1053. * srpt_ch_qp_err - set the channel queue pair state to 'error'
  1054. * @ch: SRPT RDMA channel.
  1055. */
  1056. static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
  1057. {
  1058. struct ib_qp_attr qp_attr;
  1059. qp_attr.qp_state = IB_QPS_ERR;
  1060. return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
  1061. }
  1062. /**
  1063. * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator
  1064. * @ch: SRPT RDMA channel.
  1065. */
  1066. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  1067. {
  1068. struct srpt_send_ioctx *ioctx;
  1069. int tag, cpu;
  1070. BUG_ON(!ch);
  1071. tag = sbitmap_queue_get(&ch->sess->sess_tag_pool, &cpu);
  1072. if (tag < 0)
  1073. return NULL;
  1074. ioctx = ch->ioctx_ring[tag];
  1075. BUG_ON(ioctx->ch != ch);
  1076. ioctx->state = SRPT_STATE_NEW;
  1077. WARN_ON_ONCE(ioctx->recv_ioctx);
  1078. ioctx->n_rdma = 0;
  1079. ioctx->n_rw_ctx = 0;
  1080. ioctx->queue_status_only = false;
  1081. /*
  1082. * transport_init_se_cmd() does not initialize all fields, so do it
  1083. * here.
  1084. */
  1085. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  1086. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  1087. ioctx->cmd.map_tag = tag;
  1088. ioctx->cmd.map_cpu = cpu;
  1089. return ioctx;
  1090. }
  1091. /**
  1092. * srpt_abort_cmd - abort a SCSI command
  1093. * @ioctx: I/O context associated with the SCSI command.
  1094. */
  1095. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  1096. {
  1097. enum srpt_command_state state;
  1098. BUG_ON(!ioctx);
  1099. /*
  1100. * If the command is in a state where the target core is waiting for
  1101. * the ib_srpt driver, change the state to the next state.
  1102. */
  1103. state = ioctx->state;
  1104. switch (state) {
  1105. case SRPT_STATE_NEED_DATA:
  1106. ioctx->state = SRPT_STATE_DATA_IN;
  1107. break;
  1108. case SRPT_STATE_CMD_RSP_SENT:
  1109. case SRPT_STATE_MGMT_RSP_SENT:
  1110. ioctx->state = SRPT_STATE_DONE;
  1111. break;
  1112. default:
  1113. WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
  1114. __func__, state);
  1115. break;
  1116. }
  1117. pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
  1118. ioctx->state, ioctx->cmd.tag);
  1119. switch (state) {
  1120. case SRPT_STATE_NEW:
  1121. case SRPT_STATE_DATA_IN:
  1122. case SRPT_STATE_MGMT:
  1123. case SRPT_STATE_DONE:
  1124. /*
  1125. * Do nothing - defer abort processing until
  1126. * srpt_queue_response() is invoked.
  1127. */
  1128. break;
  1129. case SRPT_STATE_NEED_DATA:
  1130. pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
  1131. transport_generic_request_failure(&ioctx->cmd,
  1132. TCM_CHECK_CONDITION_ABORT_CMD);
  1133. break;
  1134. case SRPT_STATE_CMD_RSP_SENT:
  1135. /*
  1136. * SRP_RSP sending failed or the SRP_RSP send completion has
  1137. * not been received in time.
  1138. */
  1139. transport_generic_free_cmd(&ioctx->cmd, 0);
  1140. break;
  1141. case SRPT_STATE_MGMT_RSP_SENT:
  1142. transport_generic_free_cmd(&ioctx->cmd, 0);
  1143. break;
  1144. default:
  1145. WARN(1, "Unexpected command state (%d)", state);
  1146. break;
  1147. }
  1148. return state;
  1149. }
  1150. /**
  1151. * srpt_rdma_read_done - RDMA read completion callback
  1152. * @cq: Completion queue.
  1153. * @wc: Work completion.
  1154. *
  1155. * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
  1156. * the data that has been transferred via IB RDMA had to be postponed until the
  1157. * check_stop_free() callback. None of this is necessary anymore and needs to
  1158. * be cleaned up.
  1159. */
  1160. static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
  1161. {
  1162. struct srpt_rdma_ch *ch = wc->qp->qp_context;
  1163. struct srpt_send_ioctx *ioctx =
  1164. container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
  1165. WARN_ON(ioctx->n_rdma <= 0);
  1166. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1167. ioctx->n_rdma = 0;
  1168. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1169. pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
  1170. ioctx, wc->status);
  1171. srpt_abort_cmd(ioctx);
  1172. return;
  1173. }
  1174. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1175. SRPT_STATE_DATA_IN))
  1176. target_execute_cmd(&ioctx->cmd);
  1177. else
  1178. pr_err("%s[%d]: wrong state = %d\n", __func__,
  1179. __LINE__, ioctx->state);
  1180. }
  1181. /**
  1182. * srpt_build_cmd_rsp - build a SRP_RSP response
  1183. * @ch: RDMA channel through which the request has been received.
  1184. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1185. * be built in the buffer ioctx->buf points at and hence this function will
  1186. * overwrite the request data.
  1187. * @tag: tag of the request for which this response is being generated.
  1188. * @status: value for the STATUS field of the SRP_RSP information unit.
  1189. *
  1190. * Returns the size in bytes of the SRP_RSP response.
  1191. *
  1192. * An SRP_RSP response contains a SCSI status or service response. See also
  1193. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1194. * response. See also SPC-2 for more information about sense data.
  1195. */
  1196. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1197. struct srpt_send_ioctx *ioctx, u64 tag,
  1198. int status)
  1199. {
  1200. struct se_cmd *cmd = &ioctx->cmd;
  1201. struct srp_rsp *srp_rsp;
  1202. const u8 *sense_data;
  1203. int sense_data_len, max_sense_len;
  1204. u32 resid = cmd->residual_count;
  1205. /*
  1206. * The lowest bit of all SAM-3 status codes is zero (see also
  1207. * paragraph 5.3 in SAM-3).
  1208. */
  1209. WARN_ON(status & 1);
  1210. srp_rsp = ioctx->ioctx.buf;
  1211. BUG_ON(!srp_rsp);
  1212. sense_data = ioctx->sense_data;
  1213. sense_data_len = ioctx->cmd.scsi_sense_length;
  1214. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1215. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1216. srp_rsp->opcode = SRP_RSP;
  1217. srp_rsp->req_lim_delta =
  1218. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1219. srp_rsp->tag = tag;
  1220. srp_rsp->status = status;
  1221. if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  1222. if (cmd->data_direction == DMA_TO_DEVICE) {
  1223. /* residual data from an underflow write */
  1224. srp_rsp->flags = SRP_RSP_FLAG_DOUNDER;
  1225. srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
  1226. } else if (cmd->data_direction == DMA_FROM_DEVICE) {
  1227. /* residual data from an underflow read */
  1228. srp_rsp->flags = SRP_RSP_FLAG_DIUNDER;
  1229. srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
  1230. }
  1231. } else if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  1232. if (cmd->data_direction == DMA_TO_DEVICE) {
  1233. /* residual data from an overflow write */
  1234. srp_rsp->flags = SRP_RSP_FLAG_DOOVER;
  1235. srp_rsp->data_out_res_cnt = cpu_to_be32(resid);
  1236. } else if (cmd->data_direction == DMA_FROM_DEVICE) {
  1237. /* residual data from an overflow read */
  1238. srp_rsp->flags = SRP_RSP_FLAG_DIOVER;
  1239. srp_rsp->data_in_res_cnt = cpu_to_be32(resid);
  1240. }
  1241. }
  1242. if (sense_data_len) {
  1243. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1244. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1245. if (sense_data_len > max_sense_len) {
  1246. pr_warn("truncated sense data from %d to %d bytes\n",
  1247. sense_data_len, max_sense_len);
  1248. sense_data_len = max_sense_len;
  1249. }
  1250. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1251. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1252. memcpy(srp_rsp->data, sense_data, sense_data_len);
  1253. }
  1254. return sizeof(*srp_rsp) + sense_data_len;
  1255. }
  1256. /**
  1257. * srpt_build_tskmgmt_rsp - build a task management response
  1258. * @ch: RDMA channel through which the request has been received.
  1259. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1260. * @rsp_code: RSP_CODE that will be stored in the response.
  1261. * @tag: Tag of the request for which this response is being generated.
  1262. *
  1263. * Returns the size in bytes of the SRP_RSP response.
  1264. *
  1265. * An SRP_RSP response contains a SCSI status or service response. See also
  1266. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1267. * response.
  1268. */
  1269. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1270. struct srpt_send_ioctx *ioctx,
  1271. u8 rsp_code, u64 tag)
  1272. {
  1273. struct srp_rsp *srp_rsp;
  1274. int resp_data_len;
  1275. int resp_len;
  1276. resp_data_len = 4;
  1277. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1278. srp_rsp = ioctx->ioctx.buf;
  1279. BUG_ON(!srp_rsp);
  1280. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1281. srp_rsp->opcode = SRP_RSP;
  1282. srp_rsp->req_lim_delta =
  1283. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1284. srp_rsp->tag = tag;
  1285. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1286. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1287. srp_rsp->data[3] = rsp_code;
  1288. return resp_len;
  1289. }
  1290. static int srpt_check_stop_free(struct se_cmd *cmd)
  1291. {
  1292. struct srpt_send_ioctx *ioctx = container_of(cmd,
  1293. struct srpt_send_ioctx, cmd);
  1294. return target_put_sess_cmd(&ioctx->cmd);
  1295. }
  1296. /**
  1297. * srpt_handle_cmd - process a SRP_CMD information unit
  1298. * @ch: SRPT RDMA channel.
  1299. * @recv_ioctx: Receive I/O context.
  1300. * @send_ioctx: Send I/O context.
  1301. */
  1302. static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1303. struct srpt_recv_ioctx *recv_ioctx,
  1304. struct srpt_send_ioctx *send_ioctx)
  1305. {
  1306. struct se_cmd *cmd;
  1307. struct srp_cmd *srp_cmd;
  1308. struct scatterlist *sg = NULL;
  1309. unsigned sg_cnt = 0;
  1310. u64 data_len;
  1311. enum dma_data_direction dir;
  1312. int rc;
  1313. BUG_ON(!send_ioctx);
  1314. srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
  1315. cmd = &send_ioctx->cmd;
  1316. cmd->tag = srp_cmd->tag;
  1317. switch (srp_cmd->task_attr) {
  1318. case SRP_CMD_SIMPLE_Q:
  1319. cmd->sam_task_attr = TCM_SIMPLE_TAG;
  1320. break;
  1321. case SRP_CMD_ORDERED_Q:
  1322. default:
  1323. cmd->sam_task_attr = TCM_ORDERED_TAG;
  1324. break;
  1325. case SRP_CMD_HEAD_OF_Q:
  1326. cmd->sam_task_attr = TCM_HEAD_TAG;
  1327. break;
  1328. case SRP_CMD_ACA:
  1329. cmd->sam_task_attr = TCM_ACA_TAG;
  1330. break;
  1331. }
  1332. rc = srpt_get_desc_tbl(recv_ioctx, send_ioctx, srp_cmd, &dir,
  1333. &sg, &sg_cnt, &data_len, ch->imm_data_offset);
  1334. if (rc) {
  1335. if (rc != -EAGAIN) {
  1336. pr_err("0x%llx: parsing SRP descriptor table failed.\n",
  1337. srp_cmd->tag);
  1338. }
  1339. goto busy;
  1340. }
  1341. rc = target_init_cmd(cmd, ch->sess, &send_ioctx->sense_data[0],
  1342. scsilun_to_int(&srp_cmd->lun), data_len,
  1343. TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
  1344. if (rc != 0) {
  1345. pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
  1346. srp_cmd->tag);
  1347. goto busy;
  1348. }
  1349. if (target_submit_prep(cmd, srp_cmd->cdb, sg, sg_cnt, NULL, 0, NULL, 0,
  1350. GFP_KERNEL))
  1351. return;
  1352. target_submit(cmd);
  1353. return;
  1354. busy:
  1355. target_send_busy(cmd);
  1356. }
  1357. static int srp_tmr_to_tcm(int fn)
  1358. {
  1359. switch (fn) {
  1360. case SRP_TSK_ABORT_TASK:
  1361. return TMR_ABORT_TASK;
  1362. case SRP_TSK_ABORT_TASK_SET:
  1363. return TMR_ABORT_TASK_SET;
  1364. case SRP_TSK_CLEAR_TASK_SET:
  1365. return TMR_CLEAR_TASK_SET;
  1366. case SRP_TSK_LUN_RESET:
  1367. return TMR_LUN_RESET;
  1368. case SRP_TSK_CLEAR_ACA:
  1369. return TMR_CLEAR_ACA;
  1370. default:
  1371. return -1;
  1372. }
  1373. }
  1374. /**
  1375. * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit
  1376. * @ch: SRPT RDMA channel.
  1377. * @recv_ioctx: Receive I/O context.
  1378. * @send_ioctx: Send I/O context.
  1379. *
  1380. * Returns 0 if and only if the request will be processed by the target core.
  1381. *
  1382. * For more information about SRP_TSK_MGMT information units, see also section
  1383. * 6.7 in the SRP r16a document.
  1384. */
  1385. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1386. struct srpt_recv_ioctx *recv_ioctx,
  1387. struct srpt_send_ioctx *send_ioctx)
  1388. {
  1389. struct srp_tsk_mgmt *srp_tsk;
  1390. struct se_cmd *cmd;
  1391. struct se_session *sess = ch->sess;
  1392. int tcm_tmr;
  1393. int rc;
  1394. BUG_ON(!send_ioctx);
  1395. srp_tsk = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
  1396. cmd = &send_ioctx->cmd;
  1397. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n",
  1398. srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch,
  1399. ch->sess);
  1400. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1401. send_ioctx->cmd.tag = srp_tsk->tag;
  1402. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1403. rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
  1404. scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
  1405. GFP_KERNEL, srp_tsk->task_tag,
  1406. TARGET_SCF_ACK_KREF);
  1407. if (rc != 0) {
  1408. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1409. cmd->se_tfo->queue_tm_rsp(cmd);
  1410. }
  1411. return;
  1412. }
  1413. /**
  1414. * srpt_handle_new_iu - process a newly received information unit
  1415. * @ch: RDMA channel through which the information unit has been received.
  1416. * @recv_ioctx: Receive I/O context associated with the information unit.
  1417. */
  1418. static bool
  1419. srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx)
  1420. {
  1421. struct srpt_send_ioctx *send_ioctx = NULL;
  1422. struct srp_cmd *srp_cmd;
  1423. bool res = false;
  1424. u8 opcode;
  1425. BUG_ON(!ch);
  1426. BUG_ON(!recv_ioctx);
  1427. if (unlikely(ch->state == CH_CONNECTING))
  1428. goto push;
  1429. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1430. recv_ioctx->ioctx.dma,
  1431. recv_ioctx->ioctx.offset + srp_max_req_size,
  1432. DMA_FROM_DEVICE);
  1433. srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset;
  1434. opcode = srp_cmd->opcode;
  1435. if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) {
  1436. send_ioctx = srpt_get_send_ioctx(ch);
  1437. if (unlikely(!send_ioctx))
  1438. goto push;
  1439. }
  1440. if (!list_empty(&recv_ioctx->wait_list)) {
  1441. WARN_ON_ONCE(!ch->processing_wait_list);
  1442. list_del_init(&recv_ioctx->wait_list);
  1443. }
  1444. switch (opcode) {
  1445. case SRP_CMD:
  1446. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1447. break;
  1448. case SRP_TSK_MGMT:
  1449. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1450. break;
  1451. case SRP_I_LOGOUT:
  1452. pr_err("Not yet implemented: SRP_I_LOGOUT\n");
  1453. break;
  1454. case SRP_CRED_RSP:
  1455. pr_debug("received SRP_CRED_RSP\n");
  1456. break;
  1457. case SRP_AER_RSP:
  1458. pr_debug("received SRP_AER_RSP\n");
  1459. break;
  1460. case SRP_RSP:
  1461. pr_err("Received SRP_RSP\n");
  1462. break;
  1463. default:
  1464. pr_err("received IU with unknown opcode 0x%x\n", opcode);
  1465. break;
  1466. }
  1467. if (!send_ioctx || !send_ioctx->recv_ioctx)
  1468. srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
  1469. res = true;
  1470. out:
  1471. return res;
  1472. push:
  1473. if (list_empty(&recv_ioctx->wait_list)) {
  1474. WARN_ON_ONCE(ch->processing_wait_list);
  1475. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1476. }
  1477. goto out;
  1478. }
  1479. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  1480. {
  1481. struct srpt_rdma_ch *ch = wc->qp->qp_context;
  1482. struct srpt_recv_ioctx *ioctx =
  1483. container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
  1484. if (wc->status == IB_WC_SUCCESS) {
  1485. int req_lim;
  1486. req_lim = atomic_dec_return(&ch->req_lim);
  1487. if (unlikely(req_lim < 0))
  1488. pr_err("req_lim = %d < 0\n", req_lim);
  1489. ioctx->byte_len = wc->byte_len;
  1490. srpt_handle_new_iu(ch, ioctx);
  1491. } else {
  1492. pr_info_ratelimited("receiving failed for ioctx %p with status %d\n",
  1493. ioctx, wc->status);
  1494. }
  1495. }
  1496. /*
  1497. * This function must be called from the context in which RDMA completions are
  1498. * processed because it accesses the wait list without protection against
  1499. * access from other threads.
  1500. */
  1501. static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
  1502. {
  1503. struct srpt_recv_ioctx *recv_ioctx, *tmp;
  1504. WARN_ON_ONCE(ch->state == CH_CONNECTING);
  1505. if (list_empty(&ch->cmd_wait_list))
  1506. return;
  1507. WARN_ON_ONCE(ch->processing_wait_list);
  1508. ch->processing_wait_list = true;
  1509. list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list,
  1510. wait_list) {
  1511. if (!srpt_handle_new_iu(ch, recv_ioctx))
  1512. break;
  1513. }
  1514. ch->processing_wait_list = false;
  1515. }
  1516. /**
  1517. * srpt_send_done - send completion callback
  1518. * @cq: Completion queue.
  1519. * @wc: Work completion.
  1520. *
  1521. * Note: Although this has not yet been observed during tests, at least in
  1522. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1523. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1524. * value in each response is set to one, and it is possible that this response
  1525. * makes the initiator send a new request before the send completion for that
  1526. * response has been processed. This could e.g. happen if the call to
  1527. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1528. * if IB retransmission causes generation of the send completion to be
  1529. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1530. * are queued on cmd_wait_list. The code below processes these delayed
  1531. * requests one at a time.
  1532. */
  1533. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
  1534. {
  1535. struct srpt_rdma_ch *ch = wc->qp->qp_context;
  1536. struct srpt_send_ioctx *ioctx =
  1537. container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
  1538. enum srpt_command_state state;
  1539. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1540. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
  1541. state != SRPT_STATE_MGMT_RSP_SENT);
  1542. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  1543. if (wc->status != IB_WC_SUCCESS)
  1544. pr_info("sending response for ioctx 0x%p failed with status %d\n",
  1545. ioctx, wc->status);
  1546. if (state != SRPT_STATE_DONE) {
  1547. transport_generic_free_cmd(&ioctx->cmd, 0);
  1548. } else {
  1549. pr_err("IB completion has been received too late for wr_id = %u.\n",
  1550. ioctx->ioctx.index);
  1551. }
  1552. srpt_process_wait_list(ch);
  1553. }
  1554. /**
  1555. * srpt_create_ch_ib - create receive and send completion queues
  1556. * @ch: SRPT RDMA channel.
  1557. */
  1558. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1559. {
  1560. struct ib_qp_init_attr *qp_init;
  1561. struct srpt_port *sport = ch->sport;
  1562. struct srpt_device *sdev = sport->sdev;
  1563. const struct ib_device_attr *attrs = &sdev->device->attrs;
  1564. int sq_size = sport->port_attrib.srp_sq_size;
  1565. int i, ret;
  1566. WARN_ON(ch->rq_size < 1);
  1567. ret = -ENOMEM;
  1568. qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
  1569. if (!qp_init)
  1570. goto out;
  1571. retry:
  1572. ch->cq = ib_cq_pool_get(sdev->device, ch->rq_size + sq_size, -1,
  1573. IB_POLL_WORKQUEUE);
  1574. if (IS_ERR(ch->cq)) {
  1575. ret = PTR_ERR(ch->cq);
  1576. pr_err("failed to create CQ cqe= %d ret= %d\n",
  1577. ch->rq_size + sq_size, ret);
  1578. goto out;
  1579. }
  1580. ch->cq_size = ch->rq_size + sq_size;
  1581. qp_init->qp_context = (void *)ch;
  1582. qp_init->event_handler
  1583. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1584. qp_init->send_cq = ch->cq;
  1585. qp_init->recv_cq = ch->cq;
  1586. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1587. qp_init->qp_type = IB_QPT_RC;
  1588. /*
  1589. * We divide up our send queue size into half SEND WRs to send the
  1590. * completions, and half R/W contexts to actually do the RDMA
  1591. * READ/WRITE transfers. Note that we need to allocate CQ slots for
  1592. * both both, as RDMA contexts will also post completions for the
  1593. * RDMA READ case.
  1594. */
  1595. qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr);
  1596. qp_init->cap.max_rdma_ctxs = sq_size / 2;
  1597. qp_init->cap.max_send_sge = attrs->max_send_sge;
  1598. qp_init->cap.max_recv_sge = 1;
  1599. qp_init->port_num = ch->sport->port;
  1600. if (sdev->use_srq)
  1601. qp_init->srq = sdev->srq;
  1602. else
  1603. qp_init->cap.max_recv_wr = ch->rq_size;
  1604. if (ch->using_rdma_cm) {
  1605. ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init);
  1606. ch->qp = ch->rdma_cm.cm_id->qp;
  1607. } else {
  1608. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1609. if (!IS_ERR(ch->qp)) {
  1610. ret = srpt_init_ch_qp(ch, ch->qp);
  1611. if (ret)
  1612. ib_destroy_qp(ch->qp);
  1613. } else {
  1614. ret = PTR_ERR(ch->qp);
  1615. }
  1616. }
  1617. if (ret) {
  1618. bool retry = sq_size > MIN_SRPT_SQ_SIZE;
  1619. if (retry) {
  1620. pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n",
  1621. sq_size, ret);
  1622. ib_cq_pool_put(ch->cq, ch->cq_size);
  1623. sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE);
  1624. goto retry;
  1625. } else {
  1626. pr_err("failed to create queue pair with sq_size = %d (%d)\n",
  1627. sq_size, ret);
  1628. goto err_destroy_cq;
  1629. }
  1630. }
  1631. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1632. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n",
  1633. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1634. qp_init->cap.max_send_wr, ch);
  1635. if (!sdev->use_srq)
  1636. for (i = 0; i < ch->rq_size; i++)
  1637. srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
  1638. out:
  1639. kfree(qp_init);
  1640. return ret;
  1641. err_destroy_cq:
  1642. ch->qp = NULL;
  1643. ib_cq_pool_put(ch->cq, ch->cq_size);
  1644. goto out;
  1645. }
  1646. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1647. {
  1648. ib_destroy_qp(ch->qp);
  1649. ib_cq_pool_put(ch->cq, ch->cq_size);
  1650. }
  1651. /**
  1652. * srpt_close_ch - close a RDMA channel
  1653. * @ch: SRPT RDMA channel.
  1654. *
  1655. * Make sure all resources associated with the channel will be deallocated at
  1656. * an appropriate time.
  1657. *
  1658. * Returns true if and only if the channel state has been modified into
  1659. * CH_DRAINING.
  1660. */
  1661. static bool srpt_close_ch(struct srpt_rdma_ch *ch)
  1662. {
  1663. int ret;
  1664. if (!srpt_set_ch_state(ch, CH_DRAINING)) {
  1665. pr_debug("%s: already closed\n", ch->sess_name);
  1666. return false;
  1667. }
  1668. kref_get(&ch->kref);
  1669. ret = srpt_ch_qp_err(ch);
  1670. if (ret < 0)
  1671. pr_err("%s-%d: changing queue pair into error state failed: %d\n",
  1672. ch->sess_name, ch->qp->qp_num, ret);
  1673. ret = srpt_zerolength_write(ch);
  1674. if (ret < 0) {
  1675. pr_err("%s-%d: queuing zero-length write failed: %d\n",
  1676. ch->sess_name, ch->qp->qp_num, ret);
  1677. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  1678. schedule_work(&ch->release_work);
  1679. else
  1680. WARN_ON_ONCE(true);
  1681. }
  1682. kref_put(&ch->kref, srpt_free_ch);
  1683. return true;
  1684. }
  1685. /*
  1686. * Change the channel state into CH_DISCONNECTING. If a channel has not yet
  1687. * reached the connected state, close it. If a channel is in the connected
  1688. * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
  1689. * the responsibility of the caller to ensure that this function is not
  1690. * invoked concurrently with the code that accepts a connection. This means
  1691. * that this function must either be invoked from inside a CM callback
  1692. * function or that it must be invoked with the srpt_port.mutex held.
  1693. */
  1694. static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
  1695. {
  1696. int ret;
  1697. if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
  1698. return -ENOTCONN;
  1699. if (ch->using_rdma_cm) {
  1700. ret = rdma_disconnect(ch->rdma_cm.cm_id);
  1701. } else {
  1702. ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0);
  1703. if (ret < 0)
  1704. ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0);
  1705. }
  1706. if (ret < 0 && srpt_close_ch(ch))
  1707. ret = 0;
  1708. return ret;
  1709. }
  1710. /* Send DREQ and wait for DREP. */
  1711. static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
  1712. {
  1713. DECLARE_COMPLETION_ONSTACK(closed);
  1714. struct srpt_port *sport = ch->sport;
  1715. pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
  1716. ch->state);
  1717. ch->closed = &closed;
  1718. mutex_lock(&sport->mutex);
  1719. srpt_disconnect_ch(ch);
  1720. mutex_unlock(&sport->mutex);
  1721. while (wait_for_completion_timeout(&closed, 5 * HZ) == 0)
  1722. pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
  1723. ch->sess_name, ch->qp->qp_num, ch->state);
  1724. }
  1725. static void __srpt_close_all_ch(struct srpt_port *sport)
  1726. {
  1727. struct srpt_nexus *nexus;
  1728. struct srpt_rdma_ch *ch;
  1729. lockdep_assert_held(&sport->mutex);
  1730. list_for_each_entry(nexus, &sport->nexus_list, entry) {
  1731. list_for_each_entry(ch, &nexus->ch_list, list) {
  1732. if (srpt_disconnect_ch(ch) >= 0)
  1733. pr_info("Closing channel %s-%d because target %s_%d has been disabled\n",
  1734. ch->sess_name, ch->qp->qp_num,
  1735. dev_name(&sport->sdev->device->dev),
  1736. sport->port);
  1737. srpt_close_ch(ch);
  1738. }
  1739. }
  1740. }
  1741. /*
  1742. * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if
  1743. * it does not yet exist.
  1744. */
  1745. static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport,
  1746. const u8 i_port_id[16],
  1747. const u8 t_port_id[16])
  1748. {
  1749. struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n;
  1750. for (;;) {
  1751. mutex_lock(&sport->mutex);
  1752. list_for_each_entry(n, &sport->nexus_list, entry) {
  1753. if (memcmp(n->i_port_id, i_port_id, 16) == 0 &&
  1754. memcmp(n->t_port_id, t_port_id, 16) == 0) {
  1755. nexus = n;
  1756. break;
  1757. }
  1758. }
  1759. if (!nexus && tmp_nexus) {
  1760. list_add_tail_rcu(&tmp_nexus->entry,
  1761. &sport->nexus_list);
  1762. swap(nexus, tmp_nexus);
  1763. }
  1764. mutex_unlock(&sport->mutex);
  1765. if (nexus)
  1766. break;
  1767. tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
  1768. if (!tmp_nexus) {
  1769. nexus = ERR_PTR(-ENOMEM);
  1770. break;
  1771. }
  1772. INIT_LIST_HEAD(&tmp_nexus->ch_list);
  1773. memcpy(tmp_nexus->i_port_id, i_port_id, 16);
  1774. memcpy(tmp_nexus->t_port_id, t_port_id, 16);
  1775. }
  1776. kfree(tmp_nexus);
  1777. return nexus;
  1778. }
  1779. static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
  1780. __must_hold(&sport->mutex)
  1781. {
  1782. lockdep_assert_held(&sport->mutex);
  1783. if (sport->enabled == enabled)
  1784. return;
  1785. sport->enabled = enabled;
  1786. if (!enabled)
  1787. __srpt_close_all_ch(sport);
  1788. }
  1789. static void srpt_drop_sport_ref(struct srpt_port *sport)
  1790. {
  1791. if (atomic_dec_return(&sport->refcount) == 0 && sport->freed_channels)
  1792. complete(sport->freed_channels);
  1793. }
  1794. static void srpt_free_ch(struct kref *kref)
  1795. {
  1796. struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
  1797. srpt_drop_sport_ref(ch->sport);
  1798. kfree_rcu(ch, rcu);
  1799. }
  1800. /*
  1801. * Shut down the SCSI target session, tell the connection manager to
  1802. * disconnect the associated RDMA channel, transition the QP to the error
  1803. * state and remove the channel from the channel list. This function is
  1804. * typically called from inside srpt_zerolength_write_done(). Concurrent
  1805. * srpt_zerolength_write() calls from inside srpt_close_ch() are possible
  1806. * as long as the channel is on sport->nexus_list.
  1807. */
  1808. static void srpt_release_channel_work(struct work_struct *w)
  1809. {
  1810. struct srpt_rdma_ch *ch;
  1811. struct srpt_device *sdev;
  1812. struct srpt_port *sport;
  1813. struct se_session *se_sess;
  1814. ch = container_of(w, struct srpt_rdma_ch, release_work);
  1815. pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num);
  1816. sdev = ch->sport->sdev;
  1817. BUG_ON(!sdev);
  1818. se_sess = ch->sess;
  1819. BUG_ON(!se_sess);
  1820. target_stop_session(se_sess);
  1821. target_wait_for_sess_cmds(se_sess);
  1822. target_remove_session(se_sess);
  1823. ch->sess = NULL;
  1824. if (ch->using_rdma_cm)
  1825. rdma_destroy_id(ch->rdma_cm.cm_id);
  1826. else
  1827. ib_destroy_cm_id(ch->ib_cm.cm_id);
  1828. sport = ch->sport;
  1829. mutex_lock(&sport->mutex);
  1830. list_del_rcu(&ch->list);
  1831. mutex_unlock(&sport->mutex);
  1832. if (ch->closed)
  1833. complete(ch->closed);
  1834. srpt_destroy_ch_ib(ch);
  1835. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1836. ch->sport->sdev, ch->rq_size,
  1837. ch->rsp_buf_cache, DMA_TO_DEVICE);
  1838. kmem_cache_destroy(ch->rsp_buf_cache);
  1839. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
  1840. sdev, ch->rq_size,
  1841. ch->req_buf_cache, DMA_FROM_DEVICE);
  1842. kmem_cache_destroy(ch->req_buf_cache);
  1843. kref_put(&ch->kref, srpt_free_ch);
  1844. }
  1845. /**
  1846. * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED
  1847. * @sdev: HCA through which the login request was received.
  1848. * @ib_cm_id: IB/CM connection identifier in case of IB/CM.
  1849. * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM.
  1850. * @port_num: Port through which the REQ message was received.
  1851. * @pkey: P_Key of the incoming connection.
  1852. * @req: SRP login request.
  1853. * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted
  1854. * the login request.
  1855. *
  1856. * Ownership of the cm_id is transferred to the target session if this
  1857. * function returns zero. Otherwise the caller remains the owner of cm_id.
  1858. */
  1859. static int srpt_cm_req_recv(struct srpt_device *const sdev,
  1860. struct ib_cm_id *ib_cm_id,
  1861. struct rdma_cm_id *rdma_cm_id,
  1862. u8 port_num, __be16 pkey,
  1863. const struct srp_login_req *req,
  1864. const char *src_addr)
  1865. {
  1866. struct srpt_port *sport = &sdev->port[port_num - 1];
  1867. struct srpt_nexus *nexus;
  1868. struct srp_login_rsp *rsp = NULL;
  1869. struct srp_login_rej *rej = NULL;
  1870. union {
  1871. struct rdma_conn_param rdma_cm;
  1872. struct ib_cm_rep_param ib_cm;
  1873. } *rep_param = NULL;
  1874. struct srpt_rdma_ch *ch = NULL;
  1875. char i_port_id[36];
  1876. u32 it_iu_len;
  1877. int i, tag_num, tag_size, ret;
  1878. struct srpt_tpg *stpg;
  1879. WARN_ON_ONCE(irqs_disabled());
  1880. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  1881. pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n",
  1882. req->initiator_port_id, req->target_port_id, it_iu_len,
  1883. port_num, &sport->gid, be16_to_cpu(pkey));
  1884. nexus = srpt_get_nexus(sport, req->initiator_port_id,
  1885. req->target_port_id);
  1886. if (IS_ERR(nexus)) {
  1887. ret = PTR_ERR(nexus);
  1888. goto out;
  1889. }
  1890. ret = -ENOMEM;
  1891. rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
  1892. rej = kzalloc(sizeof(*rej), GFP_KERNEL);
  1893. rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
  1894. if (!rsp || !rej || !rep_param)
  1895. goto out;
  1896. ret = -EINVAL;
  1897. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  1898. rej->reason = cpu_to_be32(
  1899. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  1900. pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n",
  1901. it_iu_len, 64, srp_max_req_size);
  1902. goto reject;
  1903. }
  1904. if (!sport->enabled) {
  1905. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1906. pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n",
  1907. dev_name(&sport->sdev->device->dev), port_num);
  1908. goto reject;
  1909. }
  1910. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  1911. || *(__be64 *)(req->target_port_id + 8) !=
  1912. cpu_to_be64(srpt_service_guid)) {
  1913. rej->reason = cpu_to_be32(
  1914. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  1915. pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n");
  1916. goto reject;
  1917. }
  1918. ret = -ENOMEM;
  1919. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1920. if (!ch) {
  1921. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1922. pr_err("rejected SRP_LOGIN_REQ because out of memory.\n");
  1923. goto reject;
  1924. }
  1925. kref_init(&ch->kref);
  1926. ch->pkey = be16_to_cpu(pkey);
  1927. ch->nexus = nexus;
  1928. ch->zw_cqe.done = srpt_zerolength_write_done;
  1929. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  1930. ch->sport = sport;
  1931. if (rdma_cm_id) {
  1932. ch->using_rdma_cm = true;
  1933. ch->rdma_cm.cm_id = rdma_cm_id;
  1934. rdma_cm_id->context = ch;
  1935. } else {
  1936. ch->ib_cm.cm_id = ib_cm_id;
  1937. ib_cm_id->context = ch;
  1938. }
  1939. /*
  1940. * ch->rq_size should be at least as large as the initiator queue
  1941. * depth to avoid that the initiator driver has to report QUEUE_FULL
  1942. * to the SCSI mid-layer.
  1943. */
  1944. ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
  1945. spin_lock_init(&ch->spinlock);
  1946. ch->state = CH_CONNECTING;
  1947. INIT_LIST_HEAD(&ch->cmd_wait_list);
  1948. ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  1949. ch->rsp_buf_cache = kmem_cache_create("srpt-rsp-buf", ch->max_rsp_size,
  1950. 512, 0, NULL);
  1951. if (!ch->rsp_buf_cache)
  1952. goto free_ch;
  1953. ch->ioctx_ring = (struct srpt_send_ioctx **)
  1954. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  1955. sizeof(*ch->ioctx_ring[0]),
  1956. ch->rsp_buf_cache, 0, DMA_TO_DEVICE);
  1957. if (!ch->ioctx_ring) {
  1958. pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n");
  1959. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1960. goto free_rsp_cache;
  1961. }
  1962. for (i = 0; i < ch->rq_size; i++)
  1963. ch->ioctx_ring[i]->ch = ch;
  1964. if (!sdev->use_srq) {
  1965. u16 imm_data_offset = req->req_flags & SRP_IMMED_REQUESTED ?
  1966. be16_to_cpu(req->imm_data_offset) : 0;
  1967. u16 alignment_offset;
  1968. u32 req_sz;
  1969. if (req->req_flags & SRP_IMMED_REQUESTED)
  1970. pr_debug("imm_data_offset = %d\n",
  1971. be16_to_cpu(req->imm_data_offset));
  1972. if (imm_data_offset >= sizeof(struct srp_cmd)) {
  1973. ch->imm_data_offset = imm_data_offset;
  1974. rsp->rsp_flags |= SRP_LOGIN_RSP_IMMED_SUPP;
  1975. } else {
  1976. ch->imm_data_offset = 0;
  1977. }
  1978. alignment_offset = round_up(imm_data_offset, 512) -
  1979. imm_data_offset;
  1980. req_sz = alignment_offset + imm_data_offset + srp_max_req_size;
  1981. ch->req_buf_cache = kmem_cache_create("srpt-req-buf", req_sz,
  1982. 512, 0, NULL);
  1983. if (!ch->req_buf_cache)
  1984. goto free_rsp_ring;
  1985. ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
  1986. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  1987. sizeof(*ch->ioctx_recv_ring[0]),
  1988. ch->req_buf_cache,
  1989. alignment_offset,
  1990. DMA_FROM_DEVICE);
  1991. if (!ch->ioctx_recv_ring) {
  1992. pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
  1993. rej->reason =
  1994. cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1995. goto free_recv_cache;
  1996. }
  1997. for (i = 0; i < ch->rq_size; i++)
  1998. INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list);
  1999. }
  2000. ret = srpt_create_ch_ib(ch);
  2001. if (ret) {
  2002. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2003. pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n");
  2004. goto free_recv_ring;
  2005. }
  2006. strscpy(ch->sess_name, src_addr, sizeof(ch->sess_name));
  2007. snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx",
  2008. be64_to_cpu(*(__be64 *)nexus->i_port_id),
  2009. be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8)));
  2010. pr_debug("registering src addr %s or i_port_id %s\n", ch->sess_name,
  2011. i_port_id);
  2012. tag_num = ch->rq_size;
  2013. tag_size = 1; /* ib_srpt does not use se_sess->sess_cmd_map */
  2014. if (sport->guid_id) {
  2015. mutex_lock(&sport->guid_id->mutex);
  2016. list_for_each_entry(stpg, &sport->guid_id->tpg_list, entry) {
  2017. if (!IS_ERR_OR_NULL(ch->sess))
  2018. break;
  2019. ch->sess = target_setup_session(&stpg->tpg, tag_num,
  2020. tag_size, TARGET_PROT_NORMAL,
  2021. ch->sess_name, ch, NULL);
  2022. }
  2023. mutex_unlock(&sport->guid_id->mutex);
  2024. }
  2025. if (sport->gid_id) {
  2026. mutex_lock(&sport->gid_id->mutex);
  2027. list_for_each_entry(stpg, &sport->gid_id->tpg_list, entry) {
  2028. if (!IS_ERR_OR_NULL(ch->sess))
  2029. break;
  2030. ch->sess = target_setup_session(&stpg->tpg, tag_num,
  2031. tag_size, TARGET_PROT_NORMAL, i_port_id,
  2032. ch, NULL);
  2033. if (!IS_ERR_OR_NULL(ch->sess))
  2034. break;
  2035. /* Retry without leading "0x" */
  2036. ch->sess = target_setup_session(&stpg->tpg, tag_num,
  2037. tag_size, TARGET_PROT_NORMAL,
  2038. i_port_id + 2, ch, NULL);
  2039. }
  2040. mutex_unlock(&sport->gid_id->mutex);
  2041. }
  2042. if (IS_ERR_OR_NULL(ch->sess)) {
  2043. WARN_ON_ONCE(ch->sess == NULL);
  2044. ret = PTR_ERR(ch->sess);
  2045. ch->sess = NULL;
  2046. pr_info("Rejected login for initiator %s: ret = %d.\n",
  2047. ch->sess_name, ret);
  2048. rej->reason = cpu_to_be32(ret == -ENOMEM ?
  2049. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
  2050. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  2051. goto destroy_ib;
  2052. }
  2053. /*
  2054. * Once a session has been created destruction of srpt_rdma_ch objects
  2055. * will decrement sport->refcount. Hence increment sport->refcount now.
  2056. */
  2057. atomic_inc(&sport->refcount);
  2058. mutex_lock(&sport->mutex);
  2059. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  2060. struct srpt_rdma_ch *ch2;
  2061. list_for_each_entry(ch2, &nexus->ch_list, list) {
  2062. if (srpt_disconnect_ch(ch2) < 0)
  2063. continue;
  2064. pr_info("Relogin - closed existing channel %s\n",
  2065. ch2->sess_name);
  2066. rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  2067. }
  2068. } else {
  2069. rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  2070. }
  2071. list_add_tail_rcu(&ch->list, &nexus->ch_list);
  2072. if (!sport->enabled) {
  2073. rej->reason = cpu_to_be32(
  2074. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2075. pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n",
  2076. dev_name(&sdev->device->dev), port_num);
  2077. mutex_unlock(&sport->mutex);
  2078. ret = -EINVAL;
  2079. goto reject;
  2080. }
  2081. mutex_unlock(&sport->mutex);
  2082. ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp);
  2083. if (ret) {
  2084. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2085. pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n",
  2086. ret);
  2087. goto reject;
  2088. }
  2089. pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess,
  2090. ch->sess_name, ch);
  2091. /* create srp_login_response */
  2092. rsp->opcode = SRP_LOGIN_RSP;
  2093. rsp->tag = req->tag;
  2094. rsp->max_it_iu_len = cpu_to_be32(srp_max_req_size);
  2095. rsp->max_ti_iu_len = req->req_it_iu_len;
  2096. ch->max_ti_iu_len = it_iu_len;
  2097. rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  2098. SRP_BUF_FORMAT_INDIRECT);
  2099. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  2100. atomic_set(&ch->req_lim, ch->rq_size);
  2101. atomic_set(&ch->req_lim_delta, 0);
  2102. /* create cm reply */
  2103. if (ch->using_rdma_cm) {
  2104. rep_param->rdma_cm.private_data = (void *)rsp;
  2105. rep_param->rdma_cm.private_data_len = sizeof(*rsp);
  2106. rep_param->rdma_cm.rnr_retry_count = 7;
  2107. rep_param->rdma_cm.flow_control = 1;
  2108. rep_param->rdma_cm.responder_resources = 4;
  2109. rep_param->rdma_cm.initiator_depth = 4;
  2110. } else {
  2111. rep_param->ib_cm.qp_num = ch->qp->qp_num;
  2112. rep_param->ib_cm.private_data = (void *)rsp;
  2113. rep_param->ib_cm.private_data_len = sizeof(*rsp);
  2114. rep_param->ib_cm.rnr_retry_count = 7;
  2115. rep_param->ib_cm.flow_control = 1;
  2116. rep_param->ib_cm.failover_accepted = 0;
  2117. rep_param->ib_cm.srq = 1;
  2118. rep_param->ib_cm.responder_resources = 4;
  2119. rep_param->ib_cm.initiator_depth = 4;
  2120. }
  2121. /*
  2122. * Hold the sport mutex while accepting a connection to avoid that
  2123. * srpt_disconnect_ch() is invoked concurrently with this code.
  2124. */
  2125. mutex_lock(&sport->mutex);
  2126. if (sport->enabled && ch->state == CH_CONNECTING) {
  2127. if (ch->using_rdma_cm)
  2128. ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm);
  2129. else
  2130. ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm);
  2131. } else {
  2132. ret = -EINVAL;
  2133. }
  2134. mutex_unlock(&sport->mutex);
  2135. switch (ret) {
  2136. case 0:
  2137. break;
  2138. case -EINVAL:
  2139. goto reject;
  2140. default:
  2141. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2142. pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n",
  2143. ret);
  2144. goto reject;
  2145. }
  2146. goto out;
  2147. destroy_ib:
  2148. srpt_destroy_ch_ib(ch);
  2149. free_recv_ring:
  2150. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
  2151. ch->sport->sdev, ch->rq_size,
  2152. ch->req_buf_cache, DMA_FROM_DEVICE);
  2153. free_recv_cache:
  2154. kmem_cache_destroy(ch->req_buf_cache);
  2155. free_rsp_ring:
  2156. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2157. ch->sport->sdev, ch->rq_size,
  2158. ch->rsp_buf_cache, DMA_TO_DEVICE);
  2159. free_rsp_cache:
  2160. kmem_cache_destroy(ch->rsp_buf_cache);
  2161. free_ch:
  2162. if (rdma_cm_id)
  2163. rdma_cm_id->context = NULL;
  2164. else
  2165. ib_cm_id->context = NULL;
  2166. kfree(ch);
  2167. ch = NULL;
  2168. WARN_ON_ONCE(ret == 0);
  2169. reject:
  2170. pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason));
  2171. rej->opcode = SRP_LOGIN_REJ;
  2172. rej->tag = req->tag;
  2173. rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  2174. SRP_BUF_FORMAT_INDIRECT);
  2175. if (rdma_cm_id)
  2176. rdma_reject(rdma_cm_id, rej, sizeof(*rej),
  2177. IB_CM_REJ_CONSUMER_DEFINED);
  2178. else
  2179. ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  2180. rej, sizeof(*rej));
  2181. if (ch && ch->sess) {
  2182. srpt_close_ch(ch);
  2183. /*
  2184. * Tell the caller not to free cm_id since
  2185. * srpt_release_channel_work() will do that.
  2186. */
  2187. ret = 0;
  2188. }
  2189. out:
  2190. kfree(rep_param);
  2191. kfree(rsp);
  2192. kfree(rej);
  2193. return ret;
  2194. }
  2195. static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id,
  2196. const struct ib_cm_req_event_param *param,
  2197. void *private_data)
  2198. {
  2199. char sguid[40];
  2200. srpt_format_guid(sguid, sizeof(sguid),
  2201. &param->primary_path->dgid.global.interface_id);
  2202. return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port,
  2203. param->primary_path->pkey,
  2204. private_data, sguid);
  2205. }
  2206. static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id,
  2207. struct rdma_cm_event *event)
  2208. {
  2209. struct srpt_device *sdev;
  2210. struct srp_login_req req;
  2211. const struct srp_login_req_rdma *req_rdma;
  2212. struct sa_path_rec *path_rec = cm_id->route.path_rec;
  2213. char src_addr[40];
  2214. sdev = ib_get_client_data(cm_id->device, &srpt_client);
  2215. if (!sdev)
  2216. return -ECONNREFUSED;
  2217. if (event->param.conn.private_data_len < sizeof(*req_rdma))
  2218. return -EINVAL;
  2219. /* Transform srp_login_req_rdma into srp_login_req. */
  2220. req_rdma = event->param.conn.private_data;
  2221. memset(&req, 0, sizeof(req));
  2222. req.opcode = req_rdma->opcode;
  2223. req.tag = req_rdma->tag;
  2224. req.req_it_iu_len = req_rdma->req_it_iu_len;
  2225. req.req_buf_fmt = req_rdma->req_buf_fmt;
  2226. req.req_flags = req_rdma->req_flags;
  2227. memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16);
  2228. memcpy(req.target_port_id, req_rdma->target_port_id, 16);
  2229. req.imm_data_offset = req_rdma->imm_data_offset;
  2230. snprintf(src_addr, sizeof(src_addr), "%pIS",
  2231. &cm_id->route.addr.src_addr);
  2232. return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num,
  2233. path_rec ? path_rec->pkey : 0, &req, src_addr);
  2234. }
  2235. static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
  2236. enum ib_cm_rej_reason reason,
  2237. const u8 *private_data,
  2238. u8 private_data_len)
  2239. {
  2240. char *priv = NULL;
  2241. int i;
  2242. if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
  2243. GFP_KERNEL))) {
  2244. for (i = 0; i < private_data_len; i++)
  2245. sprintf(priv + 3 * i, " %02x", private_data[i]);
  2246. }
  2247. pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
  2248. ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
  2249. "; private data" : "", priv ? priv : " (?)");
  2250. kfree(priv);
  2251. }
  2252. /**
  2253. * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event
  2254. * @ch: SRPT RDMA channel.
  2255. *
  2256. * An RTU (ready to use) message indicates that the connection has been
  2257. * established and that the recipient may begin transmitting.
  2258. */
  2259. static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
  2260. {
  2261. int ret;
  2262. ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp);
  2263. if (ret < 0) {
  2264. pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name,
  2265. ch->qp->qp_num);
  2266. srpt_close_ch(ch);
  2267. return;
  2268. }
  2269. /*
  2270. * Note: calling srpt_close_ch() if the transition to the LIVE state
  2271. * fails is not necessary since that means that that function has
  2272. * already been invoked from another thread.
  2273. */
  2274. if (!srpt_set_ch_state(ch, CH_LIVE)) {
  2275. pr_err("%s-%d: channel transition to LIVE state failed\n",
  2276. ch->sess_name, ch->qp->qp_num);
  2277. return;
  2278. }
  2279. /* Trigger wait list processing. */
  2280. ret = srpt_zerolength_write(ch);
  2281. WARN_ONCE(ret < 0, "%d\n", ret);
  2282. }
  2283. /**
  2284. * srpt_cm_handler - IB connection manager callback function
  2285. * @cm_id: IB/CM connection identifier.
  2286. * @event: IB/CM event.
  2287. *
  2288. * A non-zero return value will cause the caller destroy the CM ID.
  2289. *
  2290. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  2291. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  2292. * a non-zero value in any other case will trigger a race with the
  2293. * ib_destroy_cm_id() call in srpt_release_channel().
  2294. */
  2295. static int srpt_cm_handler(struct ib_cm_id *cm_id,
  2296. const struct ib_cm_event *event)
  2297. {
  2298. struct srpt_rdma_ch *ch = cm_id->context;
  2299. int ret;
  2300. ret = 0;
  2301. switch (event->event) {
  2302. case IB_CM_REQ_RECEIVED:
  2303. ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd,
  2304. event->private_data);
  2305. break;
  2306. case IB_CM_REJ_RECEIVED:
  2307. srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
  2308. event->private_data,
  2309. IB_CM_REJ_PRIVATE_DATA_SIZE);
  2310. break;
  2311. case IB_CM_RTU_RECEIVED:
  2312. case IB_CM_USER_ESTABLISHED:
  2313. srpt_cm_rtu_recv(ch);
  2314. break;
  2315. case IB_CM_DREQ_RECEIVED:
  2316. srpt_disconnect_ch(ch);
  2317. break;
  2318. case IB_CM_DREP_RECEIVED:
  2319. pr_info("Received CM DREP message for ch %s-%d.\n",
  2320. ch->sess_name, ch->qp->qp_num);
  2321. srpt_close_ch(ch);
  2322. break;
  2323. case IB_CM_TIMEWAIT_EXIT:
  2324. pr_info("Received CM TimeWait exit for ch %s-%d.\n",
  2325. ch->sess_name, ch->qp->qp_num);
  2326. srpt_close_ch(ch);
  2327. break;
  2328. case IB_CM_REP_ERROR:
  2329. pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
  2330. ch->qp->qp_num);
  2331. break;
  2332. case IB_CM_DREQ_ERROR:
  2333. pr_info("Received CM DREQ ERROR event.\n");
  2334. break;
  2335. case IB_CM_MRA_RECEIVED:
  2336. pr_info("Received CM MRA event\n");
  2337. break;
  2338. default:
  2339. pr_err("received unrecognized CM event %d\n", event->event);
  2340. break;
  2341. }
  2342. return ret;
  2343. }
  2344. static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id,
  2345. struct rdma_cm_event *event)
  2346. {
  2347. struct srpt_rdma_ch *ch = cm_id->context;
  2348. int ret = 0;
  2349. switch (event->event) {
  2350. case RDMA_CM_EVENT_CONNECT_REQUEST:
  2351. ret = srpt_rdma_cm_req_recv(cm_id, event);
  2352. break;
  2353. case RDMA_CM_EVENT_REJECTED:
  2354. srpt_cm_rej_recv(ch, event->status,
  2355. event->param.conn.private_data,
  2356. event->param.conn.private_data_len);
  2357. break;
  2358. case RDMA_CM_EVENT_ESTABLISHED:
  2359. srpt_cm_rtu_recv(ch);
  2360. break;
  2361. case RDMA_CM_EVENT_DISCONNECTED:
  2362. if (ch->state < CH_DISCONNECTING)
  2363. srpt_disconnect_ch(ch);
  2364. else
  2365. srpt_close_ch(ch);
  2366. break;
  2367. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  2368. srpt_close_ch(ch);
  2369. break;
  2370. case RDMA_CM_EVENT_UNREACHABLE:
  2371. pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
  2372. ch->qp->qp_num);
  2373. break;
  2374. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  2375. case RDMA_CM_EVENT_ADDR_CHANGE:
  2376. break;
  2377. default:
  2378. pr_err("received unrecognized RDMA CM event %d\n",
  2379. event->event);
  2380. break;
  2381. }
  2382. return ret;
  2383. }
  2384. /*
  2385. * srpt_write_pending - Start data transfer from initiator to target (write).
  2386. */
  2387. static int srpt_write_pending(struct se_cmd *se_cmd)
  2388. {
  2389. struct srpt_send_ioctx *ioctx =
  2390. container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2391. struct srpt_rdma_ch *ch = ioctx->ch;
  2392. struct ib_send_wr *first_wr = NULL;
  2393. struct ib_cqe *cqe = &ioctx->rdma_cqe;
  2394. enum srpt_command_state new_state;
  2395. int ret, i;
  2396. if (ioctx->recv_ioctx) {
  2397. srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
  2398. target_execute_cmd(&ioctx->cmd);
  2399. return 0;
  2400. }
  2401. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  2402. WARN_ON(new_state == SRPT_STATE_DONE);
  2403. if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
  2404. pr_warn("%s: IB send queue full (needed %d)\n",
  2405. __func__, ioctx->n_rdma);
  2406. ret = -ENOMEM;
  2407. goto out_undo;
  2408. }
  2409. cqe->done = srpt_rdma_read_done;
  2410. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  2411. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  2412. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
  2413. cqe, first_wr);
  2414. cqe = NULL;
  2415. }
  2416. ret = ib_post_send(ch->qp, first_wr, NULL);
  2417. if (ret) {
  2418. pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
  2419. __func__, ret, ioctx->n_rdma,
  2420. atomic_read(&ch->sq_wr_avail));
  2421. goto out_undo;
  2422. }
  2423. return 0;
  2424. out_undo:
  2425. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  2426. return ret;
  2427. }
  2428. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  2429. {
  2430. switch (tcm_mgmt_status) {
  2431. case TMR_FUNCTION_COMPLETE:
  2432. return SRP_TSK_MGMT_SUCCESS;
  2433. case TMR_FUNCTION_REJECTED:
  2434. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  2435. }
  2436. return SRP_TSK_MGMT_FAILED;
  2437. }
  2438. /**
  2439. * srpt_queue_response - transmit the response to a SCSI command
  2440. * @cmd: SCSI target command.
  2441. *
  2442. * Callback function called by the TCM core. Must not block since it can be
  2443. * invoked on the context of the IB completion handler.
  2444. */
  2445. static void srpt_queue_response(struct se_cmd *cmd)
  2446. {
  2447. struct srpt_send_ioctx *ioctx =
  2448. container_of(cmd, struct srpt_send_ioctx, cmd);
  2449. struct srpt_rdma_ch *ch = ioctx->ch;
  2450. struct srpt_device *sdev = ch->sport->sdev;
  2451. struct ib_send_wr send_wr, *first_wr = &send_wr;
  2452. struct ib_sge sge;
  2453. enum srpt_command_state state;
  2454. int resp_len, ret, i;
  2455. u8 srp_tm_status;
  2456. state = ioctx->state;
  2457. switch (state) {
  2458. case SRPT_STATE_NEW:
  2459. case SRPT_STATE_DATA_IN:
  2460. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  2461. break;
  2462. case SRPT_STATE_MGMT:
  2463. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  2464. break;
  2465. default:
  2466. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  2467. ch, ioctx->ioctx.index, ioctx->state);
  2468. break;
  2469. }
  2470. if (WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))
  2471. return;
  2472. /* For read commands, transfer the data to the initiator. */
  2473. if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
  2474. ioctx->cmd.data_length &&
  2475. !ioctx->queue_status_only) {
  2476. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  2477. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  2478. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
  2479. ch->sport->port, NULL, first_wr);
  2480. }
  2481. }
  2482. if (state != SRPT_STATE_MGMT)
  2483. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
  2484. cmd->scsi_status);
  2485. else {
  2486. srp_tm_status
  2487. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2488. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2489. ioctx->cmd.tag);
  2490. }
  2491. atomic_inc(&ch->req_lim);
  2492. if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
  2493. &ch->sq_wr_avail) < 0)) {
  2494. pr_warn("%s: IB send queue full (needed %d)\n",
  2495. __func__, ioctx->n_rdma);
  2496. goto out;
  2497. }
  2498. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
  2499. DMA_TO_DEVICE);
  2500. sge.addr = ioctx->ioctx.dma;
  2501. sge.length = resp_len;
  2502. sge.lkey = sdev->lkey;
  2503. ioctx->ioctx.cqe.done = srpt_send_done;
  2504. send_wr.next = NULL;
  2505. send_wr.wr_cqe = &ioctx->ioctx.cqe;
  2506. send_wr.sg_list = &sge;
  2507. send_wr.num_sge = 1;
  2508. send_wr.opcode = IB_WR_SEND;
  2509. send_wr.send_flags = IB_SEND_SIGNALED;
  2510. ret = ib_post_send(ch->qp, first_wr, NULL);
  2511. if (ret < 0) {
  2512. pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
  2513. __func__, ioctx->cmd.tag, ret);
  2514. goto out;
  2515. }
  2516. return;
  2517. out:
  2518. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  2519. atomic_dec(&ch->req_lim);
  2520. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2521. target_put_sess_cmd(&ioctx->cmd);
  2522. }
  2523. static int srpt_queue_data_in(struct se_cmd *cmd)
  2524. {
  2525. srpt_queue_response(cmd);
  2526. return 0;
  2527. }
  2528. static void srpt_queue_tm_rsp(struct se_cmd *cmd)
  2529. {
  2530. srpt_queue_response(cmd);
  2531. }
  2532. /*
  2533. * This function is called for aborted commands if no response is sent to the
  2534. * initiator. Make sure that the credits freed by aborting a command are
  2535. * returned to the initiator the next time a response is sent by incrementing
  2536. * ch->req_lim_delta.
  2537. */
  2538. static void srpt_aborted_task(struct se_cmd *cmd)
  2539. {
  2540. struct srpt_send_ioctx *ioctx = container_of(cmd,
  2541. struct srpt_send_ioctx, cmd);
  2542. struct srpt_rdma_ch *ch = ioctx->ch;
  2543. atomic_inc(&ch->req_lim_delta);
  2544. }
  2545. static int srpt_queue_status(struct se_cmd *cmd)
  2546. {
  2547. struct srpt_send_ioctx *ioctx;
  2548. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2549. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2550. if (cmd->se_cmd_flags &
  2551. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2552. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2553. ioctx->queue_status_only = true;
  2554. srpt_queue_response(cmd);
  2555. return 0;
  2556. }
  2557. static void srpt_refresh_port_work(struct work_struct *work)
  2558. {
  2559. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2560. srpt_refresh_port(sport);
  2561. }
  2562. /**
  2563. * srpt_release_sport - disable login and wait for associated channels
  2564. * @sport: SRPT HCA port.
  2565. */
  2566. static int srpt_release_sport(struct srpt_port *sport)
  2567. {
  2568. DECLARE_COMPLETION_ONSTACK(c);
  2569. struct srpt_nexus *nexus, *next_n;
  2570. struct srpt_rdma_ch *ch;
  2571. WARN_ON_ONCE(irqs_disabled());
  2572. sport->freed_channels = &c;
  2573. mutex_lock(&sport->mutex);
  2574. srpt_set_enabled(sport, false);
  2575. mutex_unlock(&sport->mutex);
  2576. while (atomic_read(&sport->refcount) > 0 &&
  2577. wait_for_completion_timeout(&c, 5 * HZ) <= 0) {
  2578. pr_info("%s_%d: waiting for unregistration of %d sessions ...\n",
  2579. dev_name(&sport->sdev->device->dev), sport->port,
  2580. atomic_read(&sport->refcount));
  2581. rcu_read_lock();
  2582. list_for_each_entry(nexus, &sport->nexus_list, entry) {
  2583. list_for_each_entry(ch, &nexus->ch_list, list) {
  2584. pr_info("%s-%d: state %s\n",
  2585. ch->sess_name, ch->qp->qp_num,
  2586. get_ch_state_name(ch->state));
  2587. }
  2588. }
  2589. rcu_read_unlock();
  2590. }
  2591. mutex_lock(&sport->mutex);
  2592. list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) {
  2593. list_del(&nexus->entry);
  2594. kfree_rcu(nexus, rcu);
  2595. }
  2596. mutex_unlock(&sport->mutex);
  2597. return 0;
  2598. }
  2599. struct port_and_port_id {
  2600. struct srpt_port *sport;
  2601. struct srpt_port_id **port_id;
  2602. };
  2603. static struct port_and_port_id __srpt_lookup_port(const char *name)
  2604. {
  2605. struct ib_device *dev;
  2606. struct srpt_device *sdev;
  2607. struct srpt_port *sport;
  2608. int i;
  2609. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2610. dev = sdev->device;
  2611. if (!dev)
  2612. continue;
  2613. for (i = 0; i < dev->phys_port_cnt; i++) {
  2614. sport = &sdev->port[i];
  2615. if (strcmp(sport->guid_name, name) == 0) {
  2616. kref_get(&sdev->refcnt);
  2617. return (struct port_and_port_id){
  2618. sport, &sport->guid_id};
  2619. }
  2620. if (strcmp(sport->gid_name, name) == 0) {
  2621. kref_get(&sdev->refcnt);
  2622. return (struct port_and_port_id){
  2623. sport, &sport->gid_id};
  2624. }
  2625. }
  2626. }
  2627. return (struct port_and_port_id){};
  2628. }
  2629. /**
  2630. * srpt_lookup_port() - Look up an RDMA port by name
  2631. * @name: ASCII port name
  2632. *
  2633. * Increments the RDMA port reference count if an RDMA port pointer is returned.
  2634. * The caller must drop that reference count by calling srpt_port_put_ref().
  2635. */
  2636. static struct port_and_port_id srpt_lookup_port(const char *name)
  2637. {
  2638. struct port_and_port_id papi;
  2639. spin_lock(&srpt_dev_lock);
  2640. papi = __srpt_lookup_port(name);
  2641. spin_unlock(&srpt_dev_lock);
  2642. return papi;
  2643. }
  2644. static void srpt_free_srq(struct srpt_device *sdev)
  2645. {
  2646. if (!sdev->srq)
  2647. return;
  2648. ib_destroy_srq(sdev->srq);
  2649. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2650. sdev->srq_size, sdev->req_buf_cache,
  2651. DMA_FROM_DEVICE);
  2652. kmem_cache_destroy(sdev->req_buf_cache);
  2653. sdev->srq = NULL;
  2654. }
  2655. static int srpt_alloc_srq(struct srpt_device *sdev)
  2656. {
  2657. struct ib_srq_init_attr srq_attr = {
  2658. .event_handler = srpt_srq_event,
  2659. .srq_context = (void *)sdev,
  2660. .attr.max_wr = sdev->srq_size,
  2661. .attr.max_sge = 1,
  2662. .srq_type = IB_SRQT_BASIC,
  2663. };
  2664. struct ib_device *device = sdev->device;
  2665. struct ib_srq *srq;
  2666. int i;
  2667. WARN_ON_ONCE(sdev->srq);
  2668. srq = ib_create_srq(sdev->pd, &srq_attr);
  2669. if (IS_ERR(srq)) {
  2670. pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
  2671. return PTR_ERR(srq);
  2672. }
  2673. pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
  2674. sdev->device->attrs.max_srq_wr, dev_name(&device->dev));
  2675. sdev->req_buf_cache = kmem_cache_create("srpt-srq-req-buf",
  2676. srp_max_req_size, 0, 0, NULL);
  2677. if (!sdev->req_buf_cache)
  2678. goto free_srq;
  2679. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2680. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2681. sizeof(*sdev->ioctx_ring[0]),
  2682. sdev->req_buf_cache, 0, DMA_FROM_DEVICE);
  2683. if (!sdev->ioctx_ring)
  2684. goto free_cache;
  2685. sdev->use_srq = true;
  2686. sdev->srq = srq;
  2687. for (i = 0; i < sdev->srq_size; ++i) {
  2688. INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list);
  2689. srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
  2690. }
  2691. return 0;
  2692. free_cache:
  2693. kmem_cache_destroy(sdev->req_buf_cache);
  2694. free_srq:
  2695. ib_destroy_srq(srq);
  2696. return -ENOMEM;
  2697. }
  2698. static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
  2699. {
  2700. struct ib_device *device = sdev->device;
  2701. int ret = 0;
  2702. if (!use_srq) {
  2703. srpt_free_srq(sdev);
  2704. sdev->use_srq = false;
  2705. } else if (use_srq && !sdev->srq) {
  2706. ret = srpt_alloc_srq(sdev);
  2707. }
  2708. pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__,
  2709. dev_name(&device->dev), sdev->use_srq, ret);
  2710. return ret;
  2711. }
  2712. static void srpt_free_sdev(struct kref *refcnt)
  2713. {
  2714. struct srpt_device *sdev = container_of(refcnt, typeof(*sdev), refcnt);
  2715. kfree(sdev);
  2716. }
  2717. static void srpt_sdev_put(struct srpt_device *sdev)
  2718. {
  2719. kref_put(&sdev->refcnt, srpt_free_sdev);
  2720. }
  2721. /**
  2722. * srpt_add_one - InfiniBand device addition callback function
  2723. * @device: Describes a HCA.
  2724. */
  2725. static int srpt_add_one(struct ib_device *device)
  2726. {
  2727. struct srpt_device *sdev;
  2728. struct srpt_port *sport;
  2729. int ret;
  2730. u32 i;
  2731. pr_debug("device = %p\n", device);
  2732. sdev = kzalloc(struct_size(sdev, port, device->phys_port_cnt),
  2733. GFP_KERNEL);
  2734. if (!sdev)
  2735. return -ENOMEM;
  2736. kref_init(&sdev->refcnt);
  2737. sdev->device = device;
  2738. mutex_init(&sdev->sdev_mutex);
  2739. sdev->pd = ib_alloc_pd(device, 0);
  2740. if (IS_ERR(sdev->pd)) {
  2741. ret = PTR_ERR(sdev->pd);
  2742. goto free_dev;
  2743. }
  2744. sdev->lkey = sdev->pd->local_dma_lkey;
  2745. sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
  2746. srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
  2747. if (!srpt_service_guid)
  2748. srpt_service_guid = be64_to_cpu(device->node_guid);
  2749. if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND)
  2750. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2751. if (IS_ERR(sdev->cm_id)) {
  2752. pr_info("ib_create_cm_id() failed: %ld\n",
  2753. PTR_ERR(sdev->cm_id));
  2754. ret = PTR_ERR(sdev->cm_id);
  2755. sdev->cm_id = NULL;
  2756. if (!rdma_cm_id)
  2757. goto err_ring;
  2758. }
  2759. /* print out target login information */
  2760. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n",
  2761. srpt_service_guid, srpt_service_guid, srpt_service_guid);
  2762. /*
  2763. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2764. * to identify this target. We currently use the guid of the first HCA
  2765. * in the system as service_id; therefore, the target_id will change
  2766. * if this HCA is gone bad and replaced by different HCA
  2767. */
  2768. ret = sdev->cm_id ?
  2769. ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid)) :
  2770. 0;
  2771. if (ret < 0) {
  2772. pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret,
  2773. sdev->cm_id->state);
  2774. goto err_cm;
  2775. }
  2776. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2777. srpt_event_handler);
  2778. ib_register_event_handler(&sdev->event_handler);
  2779. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2780. sport = &sdev->port[i - 1];
  2781. INIT_LIST_HEAD(&sport->nexus_list);
  2782. mutex_init(&sport->mutex);
  2783. sport->sdev = sdev;
  2784. sport->port = i;
  2785. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2786. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2787. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2788. sport->port_attrib.use_srq = false;
  2789. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2790. ret = srpt_refresh_port(sport);
  2791. if (ret) {
  2792. pr_err("MAD registration failed for %s-%d.\n",
  2793. dev_name(&sdev->device->dev), i);
  2794. i--;
  2795. goto err_port;
  2796. }
  2797. }
  2798. spin_lock(&srpt_dev_lock);
  2799. list_add_tail(&sdev->list, &srpt_dev_list);
  2800. spin_unlock(&srpt_dev_lock);
  2801. ib_set_client_data(device, &srpt_client, sdev);
  2802. pr_debug("added %s.\n", dev_name(&device->dev));
  2803. return 0;
  2804. err_port:
  2805. srpt_unregister_mad_agent(sdev, i);
  2806. ib_unregister_event_handler(&sdev->event_handler);
  2807. err_cm:
  2808. if (sdev->cm_id)
  2809. ib_destroy_cm_id(sdev->cm_id);
  2810. err_ring:
  2811. srpt_free_srq(sdev);
  2812. ib_dealloc_pd(sdev->pd);
  2813. free_dev:
  2814. srpt_sdev_put(sdev);
  2815. pr_info("%s(%s) failed.\n", __func__, dev_name(&device->dev));
  2816. return ret;
  2817. }
  2818. /**
  2819. * srpt_remove_one - InfiniBand device removal callback function
  2820. * @device: Describes a HCA.
  2821. * @client_data: The value passed as the third argument to ib_set_client_data().
  2822. */
  2823. static void srpt_remove_one(struct ib_device *device, void *client_data)
  2824. {
  2825. struct srpt_device *sdev = client_data;
  2826. int i;
  2827. srpt_unregister_mad_agent(sdev, sdev->device->phys_port_cnt);
  2828. ib_unregister_event_handler(&sdev->event_handler);
  2829. /* Cancel any work queued by the just unregistered IB event handler. */
  2830. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2831. cancel_work_sync(&sdev->port[i].work);
  2832. if (sdev->cm_id)
  2833. ib_destroy_cm_id(sdev->cm_id);
  2834. ib_set_client_data(device, &srpt_client, NULL);
  2835. /*
  2836. * Unregistering a target must happen after destroying sdev->cm_id
  2837. * such that no new SRP_LOGIN_REQ information units can arrive while
  2838. * destroying the target.
  2839. */
  2840. spin_lock(&srpt_dev_lock);
  2841. list_del(&sdev->list);
  2842. spin_unlock(&srpt_dev_lock);
  2843. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2844. srpt_release_sport(&sdev->port[i]);
  2845. srpt_free_srq(sdev);
  2846. ib_dealloc_pd(sdev->pd);
  2847. srpt_sdev_put(sdev);
  2848. }
  2849. static struct ib_client srpt_client = {
  2850. .name = DRV_NAME,
  2851. .add = srpt_add_one,
  2852. .remove = srpt_remove_one
  2853. };
  2854. static int srpt_check_true(struct se_portal_group *se_tpg)
  2855. {
  2856. return 1;
  2857. }
  2858. static int srpt_check_false(struct se_portal_group *se_tpg)
  2859. {
  2860. return 0;
  2861. }
  2862. static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
  2863. {
  2864. return tpg->se_tpg_wwn->priv;
  2865. }
  2866. static struct srpt_port_id *srpt_wwn_to_sport_id(struct se_wwn *wwn)
  2867. {
  2868. struct srpt_port *sport = wwn->priv;
  2869. if (sport->guid_id && &sport->guid_id->wwn == wwn)
  2870. return sport->guid_id;
  2871. if (sport->gid_id && &sport->gid_id->wwn == wwn)
  2872. return sport->gid_id;
  2873. WARN_ON_ONCE(true);
  2874. return NULL;
  2875. }
  2876. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2877. {
  2878. struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg);
  2879. return stpg->sport_id->name;
  2880. }
  2881. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2882. {
  2883. return 1;
  2884. }
  2885. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2886. {
  2887. return 1;
  2888. }
  2889. static void srpt_release_cmd(struct se_cmd *se_cmd)
  2890. {
  2891. struct srpt_send_ioctx *ioctx = container_of(se_cmd,
  2892. struct srpt_send_ioctx, cmd);
  2893. struct srpt_rdma_ch *ch = ioctx->ch;
  2894. struct srpt_recv_ioctx *recv_ioctx = ioctx->recv_ioctx;
  2895. WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
  2896. !(ioctx->cmd.transport_state & CMD_T_ABORTED));
  2897. if (recv_ioctx) {
  2898. WARN_ON_ONCE(!list_empty(&recv_ioctx->wait_list));
  2899. ioctx->recv_ioctx = NULL;
  2900. srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
  2901. }
  2902. if (ioctx->n_rw_ctx) {
  2903. srpt_free_rw_ctxs(ch, ioctx);
  2904. ioctx->n_rw_ctx = 0;
  2905. }
  2906. target_free_tag(se_cmd->se_sess, se_cmd);
  2907. }
  2908. /**
  2909. * srpt_close_session - forcibly close a session
  2910. * @se_sess: SCSI target session.
  2911. *
  2912. * Callback function invoked by the TCM core to clean up sessions associated
  2913. * with a node ACL when the user invokes
  2914. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2915. */
  2916. static void srpt_close_session(struct se_session *se_sess)
  2917. {
  2918. struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
  2919. srpt_disconnect_ch_sync(ch);
  2920. }
  2921. /**
  2922. * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB)
  2923. * @se_sess: SCSI target session.
  2924. *
  2925. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  2926. * This object represents an arbitrary integer used to uniquely identify a
  2927. * particular attached remote initiator port to a particular SCSI target port
  2928. * within a particular SCSI target device within a particular SCSI instance.
  2929. */
  2930. static u32 srpt_sess_get_index(struct se_session *se_sess)
  2931. {
  2932. return 0;
  2933. }
  2934. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  2935. {
  2936. }
  2937. /* Note: only used from inside debug printk's by the TCM core. */
  2938. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  2939. {
  2940. struct srpt_send_ioctx *ioctx;
  2941. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2942. return ioctx->state;
  2943. }
  2944. static int srpt_parse_guid(u64 *guid, const char *name)
  2945. {
  2946. u16 w[4];
  2947. int ret = -EINVAL;
  2948. if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
  2949. goto out;
  2950. *guid = get_unaligned_be64(w);
  2951. ret = 0;
  2952. out:
  2953. return ret;
  2954. }
  2955. /**
  2956. * srpt_parse_i_port_id - parse an initiator port ID
  2957. * @name: ASCII representation of a 128-bit initiator port ID.
  2958. * @i_port_id: Binary 128-bit port ID.
  2959. */
  2960. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  2961. {
  2962. const char *p;
  2963. unsigned len, count, leading_zero_bytes;
  2964. int ret;
  2965. p = name;
  2966. if (strncasecmp(p, "0x", 2) == 0)
  2967. p += 2;
  2968. ret = -EINVAL;
  2969. len = strlen(p);
  2970. if (len % 2)
  2971. goto out;
  2972. count = min(len / 2, 16U);
  2973. leading_zero_bytes = 16 - count;
  2974. memset(i_port_id, 0, leading_zero_bytes);
  2975. ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
  2976. out:
  2977. return ret;
  2978. }
  2979. /*
  2980. * configfs callback function invoked for mkdir
  2981. * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2982. *
  2983. * i_port_id must be an initiator port GUID, GID or IP address. See also the
  2984. * target_alloc_session() calls in this driver. Examples of valid initiator
  2985. * port IDs:
  2986. * 0x0000000000000000505400fffe4a0b7b
  2987. * 0000000000000000505400fffe4a0b7b
  2988. * 5054:00ff:fe4a:0b7b
  2989. * 192.168.122.76
  2990. */
  2991. static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
  2992. {
  2993. struct sockaddr_storage sa;
  2994. u64 guid;
  2995. u8 i_port_id[16];
  2996. int ret;
  2997. ret = srpt_parse_guid(&guid, name);
  2998. if (ret < 0)
  2999. ret = srpt_parse_i_port_id(i_port_id, name);
  3000. if (ret < 0)
  3001. ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL,
  3002. &sa);
  3003. if (ret < 0)
  3004. pr_err("invalid initiator port ID %s\n", name);
  3005. return ret;
  3006. }
  3007. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
  3008. char *page)
  3009. {
  3010. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3011. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3012. return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  3013. }
  3014. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
  3015. const char *page, size_t count)
  3016. {
  3017. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3018. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3019. unsigned long val;
  3020. int ret;
  3021. ret = kstrtoul(page, 0, &val);
  3022. if (ret < 0) {
  3023. pr_err("kstrtoul() failed with ret: %d\n", ret);
  3024. return -EINVAL;
  3025. }
  3026. if (val > MAX_SRPT_RDMA_SIZE) {
  3027. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  3028. MAX_SRPT_RDMA_SIZE);
  3029. return -EINVAL;
  3030. }
  3031. if (val < DEFAULT_MAX_RDMA_SIZE) {
  3032. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  3033. val, DEFAULT_MAX_RDMA_SIZE);
  3034. return -EINVAL;
  3035. }
  3036. sport->port_attrib.srp_max_rdma_size = val;
  3037. return count;
  3038. }
  3039. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
  3040. char *page)
  3041. {
  3042. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3043. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3044. return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  3045. }
  3046. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
  3047. const char *page, size_t count)
  3048. {
  3049. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3050. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3051. unsigned long val;
  3052. int ret;
  3053. ret = kstrtoul(page, 0, &val);
  3054. if (ret < 0) {
  3055. pr_err("kstrtoul() failed with ret: %d\n", ret);
  3056. return -EINVAL;
  3057. }
  3058. if (val > MAX_SRPT_RSP_SIZE) {
  3059. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  3060. MAX_SRPT_RSP_SIZE);
  3061. return -EINVAL;
  3062. }
  3063. if (val < MIN_MAX_RSP_SIZE) {
  3064. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  3065. MIN_MAX_RSP_SIZE);
  3066. return -EINVAL;
  3067. }
  3068. sport->port_attrib.srp_max_rsp_size = val;
  3069. return count;
  3070. }
  3071. static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
  3072. char *page)
  3073. {
  3074. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3075. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3076. return sysfs_emit(page, "%u\n", sport->port_attrib.srp_sq_size);
  3077. }
  3078. static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
  3079. const char *page, size_t count)
  3080. {
  3081. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3082. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3083. unsigned long val;
  3084. int ret;
  3085. ret = kstrtoul(page, 0, &val);
  3086. if (ret < 0) {
  3087. pr_err("kstrtoul() failed with ret: %d\n", ret);
  3088. return -EINVAL;
  3089. }
  3090. if (val > MAX_SRPT_SRQ_SIZE) {
  3091. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  3092. MAX_SRPT_SRQ_SIZE);
  3093. return -EINVAL;
  3094. }
  3095. if (val < MIN_SRPT_SRQ_SIZE) {
  3096. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  3097. MIN_SRPT_SRQ_SIZE);
  3098. return -EINVAL;
  3099. }
  3100. sport->port_attrib.srp_sq_size = val;
  3101. return count;
  3102. }
  3103. static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
  3104. char *page)
  3105. {
  3106. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3107. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3108. return sysfs_emit(page, "%d\n", sport->port_attrib.use_srq);
  3109. }
  3110. static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
  3111. const char *page, size_t count)
  3112. {
  3113. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  3114. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3115. struct srpt_device *sdev = sport->sdev;
  3116. unsigned long val;
  3117. bool enabled;
  3118. int ret;
  3119. ret = kstrtoul(page, 0, &val);
  3120. if (ret < 0)
  3121. return ret;
  3122. if (val != !!val)
  3123. return -EINVAL;
  3124. ret = mutex_lock_interruptible(&sdev->sdev_mutex);
  3125. if (ret < 0)
  3126. return ret;
  3127. ret = mutex_lock_interruptible(&sport->mutex);
  3128. if (ret < 0)
  3129. goto unlock_sdev;
  3130. enabled = sport->enabled;
  3131. /* Log out all initiator systems before changing 'use_srq'. */
  3132. srpt_set_enabled(sport, false);
  3133. sport->port_attrib.use_srq = val;
  3134. srpt_use_srq(sdev, sport->port_attrib.use_srq);
  3135. srpt_set_enabled(sport, enabled);
  3136. ret = count;
  3137. mutex_unlock(&sport->mutex);
  3138. unlock_sdev:
  3139. mutex_unlock(&sdev->sdev_mutex);
  3140. return ret;
  3141. }
  3142. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
  3143. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
  3144. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
  3145. CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq);
  3146. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  3147. &srpt_tpg_attrib_attr_srp_max_rdma_size,
  3148. &srpt_tpg_attrib_attr_srp_max_rsp_size,
  3149. &srpt_tpg_attrib_attr_srp_sq_size,
  3150. &srpt_tpg_attrib_attr_use_srq,
  3151. NULL,
  3152. };
  3153. static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
  3154. {
  3155. struct rdma_cm_id *rdma_cm_id;
  3156. int ret;
  3157. rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler,
  3158. NULL, RDMA_PS_TCP, IB_QPT_RC);
  3159. if (IS_ERR(rdma_cm_id)) {
  3160. pr_err("RDMA/CM ID creation failed: %ld\n",
  3161. PTR_ERR(rdma_cm_id));
  3162. goto out;
  3163. }
  3164. ret = rdma_bind_addr(rdma_cm_id, listen_addr);
  3165. if (ret) {
  3166. char addr_str[64];
  3167. snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr);
  3168. pr_err("Binding RDMA/CM ID to address %s failed: %d\n",
  3169. addr_str, ret);
  3170. rdma_destroy_id(rdma_cm_id);
  3171. rdma_cm_id = ERR_PTR(ret);
  3172. goto out;
  3173. }
  3174. ret = rdma_listen(rdma_cm_id, 128);
  3175. if (ret) {
  3176. pr_err("rdma_listen() failed: %d\n", ret);
  3177. rdma_destroy_id(rdma_cm_id);
  3178. rdma_cm_id = ERR_PTR(ret);
  3179. }
  3180. out:
  3181. return rdma_cm_id;
  3182. }
  3183. static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
  3184. {
  3185. return sysfs_emit(page, "%d\n", rdma_cm_port);
  3186. }
  3187. static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
  3188. const char *page, size_t count)
  3189. {
  3190. struct sockaddr_in addr4 = { .sin_family = AF_INET };
  3191. struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 };
  3192. struct rdma_cm_id *new_id = NULL;
  3193. u16 val;
  3194. int ret;
  3195. ret = kstrtou16(page, 0, &val);
  3196. if (ret < 0)
  3197. return ret;
  3198. ret = count;
  3199. if (rdma_cm_port == val)
  3200. goto out;
  3201. if (val) {
  3202. addr6.sin6_port = cpu_to_be16(val);
  3203. new_id = srpt_create_rdma_id((struct sockaddr *)&addr6);
  3204. if (IS_ERR(new_id)) {
  3205. addr4.sin_port = cpu_to_be16(val);
  3206. new_id = srpt_create_rdma_id((struct sockaddr *)&addr4);
  3207. if (IS_ERR(new_id)) {
  3208. ret = PTR_ERR(new_id);
  3209. goto out;
  3210. }
  3211. }
  3212. }
  3213. mutex_lock(&rdma_cm_mutex);
  3214. rdma_cm_port = val;
  3215. swap(rdma_cm_id, new_id);
  3216. mutex_unlock(&rdma_cm_mutex);
  3217. if (new_id)
  3218. rdma_destroy_id(new_id);
  3219. ret = count;
  3220. out:
  3221. return ret;
  3222. }
  3223. CONFIGFS_ATTR(srpt_, rdma_cm_port);
  3224. static struct configfs_attribute *srpt_da_attrs[] = {
  3225. &srpt_attr_rdma_cm_port,
  3226. NULL,
  3227. };
  3228. static int srpt_enable_tpg(struct se_portal_group *se_tpg, bool enable)
  3229. {
  3230. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3231. mutex_lock(&sport->mutex);
  3232. srpt_set_enabled(sport, enable);
  3233. mutex_unlock(&sport->mutex);
  3234. return 0;
  3235. }
  3236. /**
  3237. * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg
  3238. * @wwn: Corresponds to $driver/$port.
  3239. * @name: $tpg.
  3240. */
  3241. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  3242. const char *name)
  3243. {
  3244. struct srpt_port_id *sport_id = srpt_wwn_to_sport_id(wwn);
  3245. struct srpt_tpg *stpg;
  3246. int res = -ENOMEM;
  3247. stpg = kzalloc(sizeof(*stpg), GFP_KERNEL);
  3248. if (!stpg)
  3249. return ERR_PTR(res);
  3250. stpg->sport_id = sport_id;
  3251. res = core_tpg_register(wwn, &stpg->tpg, SCSI_PROTOCOL_SRP);
  3252. if (res) {
  3253. kfree(stpg);
  3254. return ERR_PTR(res);
  3255. }
  3256. mutex_lock(&sport_id->mutex);
  3257. list_add_tail(&stpg->entry, &sport_id->tpg_list);
  3258. mutex_unlock(&sport_id->mutex);
  3259. return &stpg->tpg;
  3260. }
  3261. /**
  3262. * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg
  3263. * @tpg: Target portal group to deregister.
  3264. */
  3265. static void srpt_drop_tpg(struct se_portal_group *tpg)
  3266. {
  3267. struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg);
  3268. struct srpt_port_id *sport_id = stpg->sport_id;
  3269. struct srpt_port *sport = srpt_tpg_to_sport(tpg);
  3270. mutex_lock(&sport_id->mutex);
  3271. list_del(&stpg->entry);
  3272. mutex_unlock(&sport_id->mutex);
  3273. sport->enabled = false;
  3274. core_tpg_deregister(tpg);
  3275. kfree(stpg);
  3276. }
  3277. /**
  3278. * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port
  3279. * @tf: Not used.
  3280. * @group: Not used.
  3281. * @name: $port.
  3282. */
  3283. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  3284. struct config_group *group,
  3285. const char *name)
  3286. {
  3287. struct port_and_port_id papi = srpt_lookup_port(name);
  3288. struct srpt_port *sport = papi.sport;
  3289. struct srpt_port_id *port_id;
  3290. if (!papi.port_id)
  3291. return ERR_PTR(-EINVAL);
  3292. if (*papi.port_id) {
  3293. /* Attempt to create a directory that already exists. */
  3294. WARN_ON_ONCE(true);
  3295. return &(*papi.port_id)->wwn;
  3296. }
  3297. port_id = kzalloc(sizeof(*port_id), GFP_KERNEL);
  3298. if (!port_id) {
  3299. srpt_sdev_put(sport->sdev);
  3300. return ERR_PTR(-ENOMEM);
  3301. }
  3302. mutex_init(&port_id->mutex);
  3303. INIT_LIST_HEAD(&port_id->tpg_list);
  3304. port_id->wwn.priv = sport;
  3305. memcpy(port_id->name, port_id == sport->guid_id ? sport->guid_name :
  3306. sport->gid_name, ARRAY_SIZE(port_id->name));
  3307. *papi.port_id = port_id;
  3308. return &port_id->wwn;
  3309. }
  3310. /**
  3311. * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port
  3312. * @wwn: $port.
  3313. */
  3314. static void srpt_drop_tport(struct se_wwn *wwn)
  3315. {
  3316. struct srpt_port_id *port_id = container_of(wwn, typeof(*port_id), wwn);
  3317. struct srpt_port *sport = wwn->priv;
  3318. if (sport->guid_id == port_id)
  3319. sport->guid_id = NULL;
  3320. else if (sport->gid_id == port_id)
  3321. sport->gid_id = NULL;
  3322. else
  3323. WARN_ON_ONCE(true);
  3324. srpt_sdev_put(sport->sdev);
  3325. kfree(port_id);
  3326. }
  3327. static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
  3328. {
  3329. return sysfs_emit(buf, "\n");
  3330. }
  3331. CONFIGFS_ATTR_RO(srpt_wwn_, version);
  3332. static struct configfs_attribute *srpt_wwn_attrs[] = {
  3333. &srpt_wwn_attr_version,
  3334. NULL,
  3335. };
  3336. static const struct target_core_fabric_ops srpt_template = {
  3337. .module = THIS_MODULE,
  3338. .fabric_name = "srpt",
  3339. .tpg_get_wwn = srpt_get_fabric_wwn,
  3340. .tpg_get_tag = srpt_get_tag,
  3341. .tpg_check_demo_mode = srpt_check_false,
  3342. .tpg_check_demo_mode_cache = srpt_check_true,
  3343. .tpg_check_demo_mode_write_protect = srpt_check_true,
  3344. .tpg_check_prod_mode_write_protect = srpt_check_false,
  3345. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  3346. .release_cmd = srpt_release_cmd,
  3347. .check_stop_free = srpt_check_stop_free,
  3348. .close_session = srpt_close_session,
  3349. .sess_get_index = srpt_sess_get_index,
  3350. .sess_get_initiator_sid = NULL,
  3351. .write_pending = srpt_write_pending,
  3352. .set_default_node_attributes = srpt_set_default_node_attrs,
  3353. .get_cmd_state = srpt_get_tcm_cmd_state,
  3354. .queue_data_in = srpt_queue_data_in,
  3355. .queue_status = srpt_queue_status,
  3356. .queue_tm_rsp = srpt_queue_tm_rsp,
  3357. .aborted_task = srpt_aborted_task,
  3358. /*
  3359. * Setup function pointers for generic logic in
  3360. * target_core_fabric_configfs.c
  3361. */
  3362. .fabric_make_wwn = srpt_make_tport,
  3363. .fabric_drop_wwn = srpt_drop_tport,
  3364. .fabric_make_tpg = srpt_make_tpg,
  3365. .fabric_enable_tpg = srpt_enable_tpg,
  3366. .fabric_drop_tpg = srpt_drop_tpg,
  3367. .fabric_init_nodeacl = srpt_init_nodeacl,
  3368. .tfc_discovery_attrs = srpt_da_attrs,
  3369. .tfc_wwn_attrs = srpt_wwn_attrs,
  3370. .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
  3371. };
  3372. /**
  3373. * srpt_init_module - kernel module initialization
  3374. *
  3375. * Note: Since ib_register_client() registers callback functions, and since at
  3376. * least one of these callback functions (srpt_add_one()) calls target core
  3377. * functions, this driver must be registered with the target core before
  3378. * ib_register_client() is called.
  3379. */
  3380. static int __init srpt_init_module(void)
  3381. {
  3382. int ret;
  3383. ret = -EINVAL;
  3384. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  3385. pr_err("invalid value %d for kernel module parameter srp_max_req_size -- must be at least %d.\n",
  3386. srp_max_req_size, MIN_MAX_REQ_SIZE);
  3387. goto out;
  3388. }
  3389. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  3390. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  3391. pr_err("invalid value %d for kernel module parameter srpt_srq_size -- must be in the range [%d..%d].\n",
  3392. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  3393. goto out;
  3394. }
  3395. ret = target_register_template(&srpt_template);
  3396. if (ret)
  3397. goto out;
  3398. ret = ib_register_client(&srpt_client);
  3399. if (ret) {
  3400. pr_err("couldn't register IB client\n");
  3401. goto out_unregister_target;
  3402. }
  3403. return 0;
  3404. out_unregister_target:
  3405. target_unregister_template(&srpt_template);
  3406. out:
  3407. return ret;
  3408. }
  3409. static void __exit srpt_cleanup_module(void)
  3410. {
  3411. if (rdma_cm_id)
  3412. rdma_destroy_id(rdma_cm_id);
  3413. ib_unregister_client(&srpt_client);
  3414. target_unregister_template(&srpt_template);
  3415. }
  3416. module_init(srpt_init_module);
  3417. module_exit(srpt_cleanup_module);