ib_srpt.c 101 KB

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