ib_srpt.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066
  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 struct target_fabric_configfs *srpt_target;
  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. /* Refresh port data asynchronously. */
  179. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  180. sport = &sdev->port[event->element.port_num - 1];
  181. if (!sport->lid && !sport->sm_lid)
  182. schedule_work(&sport->work);
  183. }
  184. break;
  185. default:
  186. printk(KERN_ERR "received unrecognized IB event %d\n",
  187. event->event);
  188. break;
  189. }
  190. }
  191. /**
  192. * srpt_srq_event() - SRQ event callback function.
  193. */
  194. static void srpt_srq_event(struct ib_event *event, void *ctx)
  195. {
  196. printk(KERN_INFO "SRQ event %d\n", event->event);
  197. }
  198. /**
  199. * srpt_qp_event() - QP event callback function.
  200. */
  201. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  202. {
  203. pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
  204. event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
  205. switch (event->event) {
  206. case IB_EVENT_COMM_EST:
  207. ib_cm_notify(ch->cm_id, event->event);
  208. break;
  209. case IB_EVENT_QP_LAST_WQE_REACHED:
  210. if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
  211. CH_RELEASING))
  212. srpt_release_channel(ch);
  213. else
  214. pr_debug("%s: state %d - ignored LAST_WQE.\n",
  215. ch->sess_name, srpt_get_ch_state(ch));
  216. break;
  217. default:
  218. printk(KERN_ERR "received unrecognized IB QP event %d\n",
  219. 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);
  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. printk(KERN_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. printk(KERN_WARNING "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. printk(KERN_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. transport_do_task_sg_chain(cmd);
  955. ioctx->sg = sg = sg_orig = cmd->t_tasks_sg_chained;
  956. ioctx->sg_cnt = sg_cnt = cmd->t_tasks_sg_chained_no;
  957. count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
  958. opposite_dma_dir(dir));
  959. if (unlikely(!count))
  960. return -EAGAIN;
  961. ioctx->mapped_sg_count = count;
  962. if (ioctx->rdma_ius && ioctx->n_rdma_ius)
  963. nrdma = ioctx->n_rdma_ius;
  964. else {
  965. nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
  966. + ioctx->n_rbuf;
  967. ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
  968. if (!ioctx->rdma_ius)
  969. goto free_mem;
  970. ioctx->n_rdma_ius = nrdma;
  971. }
  972. db = ioctx->rbufs;
  973. tsize = cmd->data_length;
  974. dma_len = ib_sg_dma_len(dev, &sg[0]);
  975. riu = ioctx->rdma_ius;
  976. /*
  977. * For each remote desc - calculate the #ib_sge.
  978. * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
  979. * each remote desc rdma_iu is required a rdma wr;
  980. * else
  981. * we need to allocate extra rdma_iu to carry extra #ib_sge in
  982. * another rdma wr
  983. */
  984. for (i = 0, j = 0;
  985. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  986. rsize = be32_to_cpu(db->len);
  987. raddr = be64_to_cpu(db->va);
  988. riu->raddr = raddr;
  989. riu->rkey = be32_to_cpu(db->key);
  990. riu->sge_cnt = 0;
  991. /* calculate how many sge required for this remote_buf */
  992. while (rsize > 0 && tsize > 0) {
  993. if (rsize >= dma_len) {
  994. tsize -= dma_len;
  995. rsize -= dma_len;
  996. raddr += dma_len;
  997. if (tsize > 0) {
  998. ++j;
  999. if (j < count) {
  1000. sg = sg_next(sg);
  1001. dma_len = ib_sg_dma_len(
  1002. dev, sg);
  1003. }
  1004. }
  1005. } else {
  1006. tsize -= rsize;
  1007. dma_len -= rsize;
  1008. rsize = 0;
  1009. }
  1010. ++riu->sge_cnt;
  1011. if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
  1012. ++ioctx->n_rdma;
  1013. riu->sge =
  1014. kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1015. GFP_KERNEL);
  1016. if (!riu->sge)
  1017. goto free_mem;
  1018. ++riu;
  1019. riu->sge_cnt = 0;
  1020. riu->raddr = raddr;
  1021. riu->rkey = be32_to_cpu(db->key);
  1022. }
  1023. }
  1024. ++ioctx->n_rdma;
  1025. riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1026. GFP_KERNEL);
  1027. if (!riu->sge)
  1028. goto free_mem;
  1029. }
  1030. db = ioctx->rbufs;
  1031. tsize = cmd->data_length;
  1032. riu = ioctx->rdma_ius;
  1033. sg = sg_orig;
  1034. dma_len = ib_sg_dma_len(dev, &sg[0]);
  1035. dma_addr = ib_sg_dma_address(dev, &sg[0]);
  1036. /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
  1037. for (i = 0, j = 0;
  1038. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  1039. rsize = be32_to_cpu(db->len);
  1040. sge = riu->sge;
  1041. k = 0;
  1042. while (rsize > 0 && tsize > 0) {
  1043. sge->addr = dma_addr;
  1044. sge->lkey = ch->sport->sdev->mr->lkey;
  1045. if (rsize >= dma_len) {
  1046. sge->length =
  1047. (tsize < dma_len) ? tsize : dma_len;
  1048. tsize -= dma_len;
  1049. rsize -= dma_len;
  1050. if (tsize > 0) {
  1051. ++j;
  1052. if (j < count) {
  1053. sg = sg_next(sg);
  1054. dma_len = ib_sg_dma_len(
  1055. dev, sg);
  1056. dma_addr = ib_sg_dma_address(
  1057. dev, sg);
  1058. }
  1059. }
  1060. } else {
  1061. sge->length = (tsize < rsize) ? tsize : rsize;
  1062. tsize -= rsize;
  1063. dma_len -= rsize;
  1064. dma_addr += rsize;
  1065. rsize = 0;
  1066. }
  1067. ++k;
  1068. if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
  1069. ++riu;
  1070. sge = riu->sge;
  1071. k = 0;
  1072. } else if (rsize > 0 && tsize > 0)
  1073. ++sge;
  1074. }
  1075. }
  1076. return 0;
  1077. free_mem:
  1078. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  1079. return -ENOMEM;
  1080. }
  1081. /**
  1082. * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
  1083. */
  1084. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  1085. {
  1086. struct srpt_send_ioctx *ioctx;
  1087. unsigned long flags;
  1088. BUG_ON(!ch);
  1089. ioctx = NULL;
  1090. spin_lock_irqsave(&ch->spinlock, flags);
  1091. if (!list_empty(&ch->free_list)) {
  1092. ioctx = list_first_entry(&ch->free_list,
  1093. struct srpt_send_ioctx, free_list);
  1094. list_del(&ioctx->free_list);
  1095. }
  1096. spin_unlock_irqrestore(&ch->spinlock, flags);
  1097. if (!ioctx)
  1098. return ioctx;
  1099. BUG_ON(ioctx->ch != ch);
  1100. kref_init(&ioctx->kref);
  1101. spin_lock_init(&ioctx->spinlock);
  1102. ioctx->state = SRPT_STATE_NEW;
  1103. ioctx->n_rbuf = 0;
  1104. ioctx->rbufs = NULL;
  1105. ioctx->n_rdma = 0;
  1106. ioctx->n_rdma_ius = 0;
  1107. ioctx->rdma_ius = NULL;
  1108. ioctx->mapped_sg_count = 0;
  1109. init_completion(&ioctx->tx_done);
  1110. ioctx->queue_status_only = false;
  1111. /*
  1112. * transport_init_se_cmd() does not initialize all fields, so do it
  1113. * here.
  1114. */
  1115. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  1116. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  1117. return ioctx;
  1118. }
  1119. /**
  1120. * srpt_put_send_ioctx() - Free up resources.
  1121. */
  1122. static void srpt_put_send_ioctx(struct srpt_send_ioctx *ioctx)
  1123. {
  1124. struct srpt_rdma_ch *ch;
  1125. unsigned long flags;
  1126. BUG_ON(!ioctx);
  1127. ch = ioctx->ch;
  1128. BUG_ON(!ch);
  1129. WARN_ON(srpt_get_cmd_state(ioctx) != SRPT_STATE_DONE);
  1130. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1131. transport_generic_free_cmd(&ioctx->cmd, 0);
  1132. if (ioctx->n_rbuf > 1) {
  1133. kfree(ioctx->rbufs);
  1134. ioctx->rbufs = NULL;
  1135. ioctx->n_rbuf = 0;
  1136. }
  1137. spin_lock_irqsave(&ch->spinlock, flags);
  1138. list_add(&ioctx->free_list, &ch->free_list);
  1139. spin_unlock_irqrestore(&ch->spinlock, flags);
  1140. }
  1141. static void srpt_put_send_ioctx_kref(struct kref *kref)
  1142. {
  1143. srpt_put_send_ioctx(container_of(kref, struct srpt_send_ioctx, kref));
  1144. }
  1145. /**
  1146. * srpt_abort_cmd() - Abort a SCSI command.
  1147. * @ioctx: I/O context associated with the SCSI command.
  1148. * @context: Preferred execution context.
  1149. */
  1150. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  1151. {
  1152. enum srpt_command_state state;
  1153. unsigned long flags;
  1154. BUG_ON(!ioctx);
  1155. /*
  1156. * If the command is in a state where the target core is waiting for
  1157. * the ib_srpt driver, change the state to the next state. Changing
  1158. * the state of the command from SRPT_STATE_NEED_DATA to
  1159. * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
  1160. * function a second time.
  1161. */
  1162. spin_lock_irqsave(&ioctx->spinlock, flags);
  1163. state = ioctx->state;
  1164. switch (state) {
  1165. case SRPT_STATE_NEED_DATA:
  1166. ioctx->state = SRPT_STATE_DATA_IN;
  1167. break;
  1168. case SRPT_STATE_DATA_IN:
  1169. case SRPT_STATE_CMD_RSP_SENT:
  1170. case SRPT_STATE_MGMT_RSP_SENT:
  1171. ioctx->state = SRPT_STATE_DONE;
  1172. break;
  1173. default:
  1174. break;
  1175. }
  1176. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  1177. if (state == SRPT_STATE_DONE)
  1178. goto out;
  1179. pr_debug("Aborting cmd with state %d and tag %lld\n", state,
  1180. ioctx->tag);
  1181. switch (state) {
  1182. case SRPT_STATE_NEW:
  1183. case SRPT_STATE_DATA_IN:
  1184. case SRPT_STATE_MGMT:
  1185. /*
  1186. * Do nothing - defer abort processing until
  1187. * srpt_queue_response() is invoked.
  1188. */
  1189. WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
  1190. break;
  1191. case SRPT_STATE_NEED_DATA:
  1192. /* DMA_TO_DEVICE (write) - RDMA read error. */
  1193. spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
  1194. ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
  1195. spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
  1196. transport_generic_handle_data(&ioctx->cmd);
  1197. break;
  1198. case SRPT_STATE_CMD_RSP_SENT:
  1199. /*
  1200. * SRP_RSP sending failed or the SRP_RSP send completion has
  1201. * not been received in time.
  1202. */
  1203. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1204. spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
  1205. ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
  1206. spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
  1207. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1208. break;
  1209. case SRPT_STATE_MGMT_RSP_SENT:
  1210. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1211. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1212. break;
  1213. default:
  1214. WARN_ON("ERROR: unexpected command state");
  1215. break;
  1216. }
  1217. out:
  1218. return state;
  1219. }
  1220. /**
  1221. * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
  1222. */
  1223. static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
  1224. {
  1225. struct srpt_send_ioctx *ioctx;
  1226. enum srpt_command_state state;
  1227. struct se_cmd *cmd;
  1228. u32 index;
  1229. atomic_inc(&ch->sq_wr_avail);
  1230. index = idx_from_wr_id(wr_id);
  1231. ioctx = ch->ioctx_ring[index];
  1232. state = srpt_get_cmd_state(ioctx);
  1233. cmd = &ioctx->cmd;
  1234. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1235. && state != SRPT_STATE_MGMT_RSP_SENT
  1236. && state != SRPT_STATE_NEED_DATA
  1237. && state != SRPT_STATE_DONE);
  1238. /* If SRP_RSP sending failed, undo the ch->req_lim change. */
  1239. if (state == SRPT_STATE_CMD_RSP_SENT
  1240. || state == SRPT_STATE_MGMT_RSP_SENT)
  1241. atomic_dec(&ch->req_lim);
  1242. srpt_abort_cmd(ioctx);
  1243. }
  1244. /**
  1245. * srpt_handle_send_comp() - Process an IB send completion notification.
  1246. */
  1247. static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
  1248. struct srpt_send_ioctx *ioctx)
  1249. {
  1250. enum srpt_command_state state;
  1251. atomic_inc(&ch->sq_wr_avail);
  1252. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1253. if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1254. && state != SRPT_STATE_MGMT_RSP_SENT
  1255. && state != SRPT_STATE_DONE))
  1256. pr_debug("state = %d\n", state);
  1257. if (state != SRPT_STATE_DONE)
  1258. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1259. else
  1260. printk(KERN_ERR "IB completion has been received too late for"
  1261. " wr_id = %u.\n", ioctx->ioctx.index);
  1262. }
  1263. /**
  1264. * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
  1265. *
  1266. * Note: transport_generic_handle_data() is asynchronous so unmapping the
  1267. * data that has been transferred via IB RDMA must be postponed until the
  1268. * check_stop_free() callback.
  1269. */
  1270. static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
  1271. struct srpt_send_ioctx *ioctx,
  1272. enum srpt_opcode opcode)
  1273. {
  1274. WARN_ON(ioctx->n_rdma <= 0);
  1275. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1276. if (opcode == SRPT_RDMA_READ_LAST) {
  1277. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1278. SRPT_STATE_DATA_IN))
  1279. transport_generic_handle_data(&ioctx->cmd);
  1280. else
  1281. printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
  1282. __LINE__, srpt_get_cmd_state(ioctx));
  1283. } else if (opcode == SRPT_RDMA_ABORT) {
  1284. ioctx->rdma_aborted = true;
  1285. } else {
  1286. WARN(true, "unexpected opcode %d\n", opcode);
  1287. }
  1288. }
  1289. /**
  1290. * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
  1291. */
  1292. static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
  1293. struct srpt_send_ioctx *ioctx,
  1294. enum srpt_opcode opcode)
  1295. {
  1296. struct se_cmd *cmd;
  1297. enum srpt_command_state state;
  1298. unsigned long flags;
  1299. cmd = &ioctx->cmd;
  1300. state = srpt_get_cmd_state(ioctx);
  1301. switch (opcode) {
  1302. case SRPT_RDMA_READ_LAST:
  1303. if (ioctx->n_rdma <= 0) {
  1304. printk(KERN_ERR "Received invalid RDMA read"
  1305. " error completion with idx %d\n",
  1306. ioctx->ioctx.index);
  1307. break;
  1308. }
  1309. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1310. if (state == SRPT_STATE_NEED_DATA)
  1311. srpt_abort_cmd(ioctx);
  1312. else
  1313. printk(KERN_ERR "%s[%d]: wrong state = %d\n",
  1314. __func__, __LINE__, state);
  1315. break;
  1316. case SRPT_RDMA_WRITE_LAST:
  1317. spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
  1318. ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
  1319. spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
  1320. break;
  1321. default:
  1322. printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
  1323. __LINE__, opcode);
  1324. break;
  1325. }
  1326. }
  1327. /**
  1328. * srpt_build_cmd_rsp() - Build an SRP_RSP response.
  1329. * @ch: RDMA channel through which the request has been received.
  1330. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1331. * be built in the buffer ioctx->buf points at and hence this function will
  1332. * overwrite the request data.
  1333. * @tag: tag of the request for which this response is being generated.
  1334. * @status: value for the STATUS field of the SRP_RSP information unit.
  1335. *
  1336. * Returns the size in bytes of the SRP_RSP response.
  1337. *
  1338. * An SRP_RSP response contains a SCSI status or service response. See also
  1339. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1340. * response. See also SPC-2 for more information about sense data.
  1341. */
  1342. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1343. struct srpt_send_ioctx *ioctx, u64 tag,
  1344. int status)
  1345. {
  1346. struct srp_rsp *srp_rsp;
  1347. const u8 *sense_data;
  1348. int sense_data_len, max_sense_len;
  1349. /*
  1350. * The lowest bit of all SAM-3 status codes is zero (see also
  1351. * paragraph 5.3 in SAM-3).
  1352. */
  1353. WARN_ON(status & 1);
  1354. srp_rsp = ioctx->ioctx.buf;
  1355. BUG_ON(!srp_rsp);
  1356. sense_data = ioctx->sense_data;
  1357. sense_data_len = ioctx->cmd.scsi_sense_length;
  1358. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1359. memset(srp_rsp, 0, sizeof *srp_rsp);
  1360. srp_rsp->opcode = SRP_RSP;
  1361. srp_rsp->req_lim_delta =
  1362. __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1363. srp_rsp->tag = tag;
  1364. srp_rsp->status = status;
  1365. if (sense_data_len) {
  1366. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1367. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1368. if (sense_data_len > max_sense_len) {
  1369. printk(KERN_WARNING "truncated sense data from %d to %d"
  1370. " bytes\n", sense_data_len, max_sense_len);
  1371. sense_data_len = max_sense_len;
  1372. }
  1373. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1374. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1375. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1376. }
  1377. return sizeof(*srp_rsp) + sense_data_len;
  1378. }
  1379. /**
  1380. * srpt_build_tskmgmt_rsp() - Build a task management response.
  1381. * @ch: RDMA channel through which the request has been received.
  1382. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1383. * @rsp_code: RSP_CODE that will be stored in the response.
  1384. * @tag: Tag of the request for which this response is being generated.
  1385. *
  1386. * Returns the size in bytes of the SRP_RSP response.
  1387. *
  1388. * An SRP_RSP response contains a SCSI status or service response. See also
  1389. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1390. * response.
  1391. */
  1392. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1393. struct srpt_send_ioctx *ioctx,
  1394. u8 rsp_code, u64 tag)
  1395. {
  1396. struct srp_rsp *srp_rsp;
  1397. int resp_data_len;
  1398. int resp_len;
  1399. resp_data_len = 4;
  1400. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1401. srp_rsp = ioctx->ioctx.buf;
  1402. BUG_ON(!srp_rsp);
  1403. memset(srp_rsp, 0, sizeof *srp_rsp);
  1404. srp_rsp->opcode = SRP_RSP;
  1405. srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
  1406. + atomic_xchg(&ch->req_lim_delta, 0));
  1407. srp_rsp->tag = tag;
  1408. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1409. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1410. srp_rsp->data[3] = rsp_code;
  1411. return resp_len;
  1412. }
  1413. #define NO_SUCH_LUN ((uint64_t)-1LL)
  1414. /*
  1415. * SCSI LUN addressing method. See also SAM-2 and the section about
  1416. * eight byte LUNs.
  1417. */
  1418. enum scsi_lun_addr_method {
  1419. SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
  1420. SCSI_LUN_ADDR_METHOD_FLAT = 1,
  1421. SCSI_LUN_ADDR_METHOD_LUN = 2,
  1422. SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
  1423. };
  1424. /*
  1425. * srpt_unpack_lun() - Convert from network LUN to linear LUN.
  1426. *
  1427. * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
  1428. * order (big endian) to a linear LUN. Supports three LUN addressing methods:
  1429. * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
  1430. */
  1431. static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
  1432. {
  1433. uint64_t res = NO_SUCH_LUN;
  1434. int addressing_method;
  1435. if (unlikely(len < 2)) {
  1436. printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
  1437. "more", len);
  1438. goto out;
  1439. }
  1440. switch (len) {
  1441. case 8:
  1442. if ((*((__be64 *)lun) &
  1443. __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
  1444. goto out_err;
  1445. break;
  1446. case 4:
  1447. if (*((__be16 *)&lun[2]) != 0)
  1448. goto out_err;
  1449. break;
  1450. case 6:
  1451. if (*((__be32 *)&lun[2]) != 0)
  1452. goto out_err;
  1453. break;
  1454. case 2:
  1455. break;
  1456. default:
  1457. goto out_err;
  1458. }
  1459. addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
  1460. switch (addressing_method) {
  1461. case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
  1462. case SCSI_LUN_ADDR_METHOD_FLAT:
  1463. case SCSI_LUN_ADDR_METHOD_LUN:
  1464. res = *(lun + 1) | (((*lun) & 0x3f) << 8);
  1465. break;
  1466. case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
  1467. default:
  1468. printk(KERN_ERR "Unimplemented LUN addressing method %u",
  1469. addressing_method);
  1470. break;
  1471. }
  1472. out:
  1473. return res;
  1474. out_err:
  1475. printk(KERN_ERR "Support for multi-level LUNs has not yet been"
  1476. " implemented");
  1477. goto out;
  1478. }
  1479. static int srpt_check_stop_free(struct se_cmd *cmd)
  1480. {
  1481. struct srpt_send_ioctx *ioctx;
  1482. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  1483. return kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1484. }
  1485. /**
  1486. * srpt_handle_cmd() - Process SRP_CMD.
  1487. */
  1488. static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1489. struct srpt_recv_ioctx *recv_ioctx,
  1490. struct srpt_send_ioctx *send_ioctx)
  1491. {
  1492. struct se_cmd *cmd;
  1493. struct srp_cmd *srp_cmd;
  1494. uint64_t unpacked_lun;
  1495. u64 data_len;
  1496. enum dma_data_direction dir;
  1497. int ret;
  1498. BUG_ON(!send_ioctx);
  1499. srp_cmd = recv_ioctx->ioctx.buf;
  1500. kref_get(&send_ioctx->kref);
  1501. cmd = &send_ioctx->cmd;
  1502. send_ioctx->tag = srp_cmd->tag;
  1503. switch (srp_cmd->task_attr) {
  1504. case SRP_CMD_SIMPLE_Q:
  1505. cmd->sam_task_attr = MSG_SIMPLE_TAG;
  1506. break;
  1507. case SRP_CMD_ORDERED_Q:
  1508. default:
  1509. cmd->sam_task_attr = MSG_ORDERED_TAG;
  1510. break;
  1511. case SRP_CMD_HEAD_OF_Q:
  1512. cmd->sam_task_attr = MSG_HEAD_TAG;
  1513. break;
  1514. case SRP_CMD_ACA:
  1515. cmd->sam_task_attr = MSG_ACA_TAG;
  1516. break;
  1517. }
  1518. ret = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len);
  1519. if (ret) {
  1520. printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
  1521. srp_cmd->tag);
  1522. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1523. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1524. kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
  1525. goto send_sense;
  1526. }
  1527. cmd->data_length = data_len;
  1528. cmd->data_direction = dir;
  1529. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
  1530. sizeof(srp_cmd->lun));
  1531. if (transport_lookup_cmd_lun(cmd, unpacked_lun) < 0) {
  1532. kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
  1533. goto send_sense;
  1534. }
  1535. ret = target_setup_cmd_from_cdb(cmd, srp_cmd->cdb);
  1536. if (ret < 0) {
  1537. kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
  1538. if (cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT) {
  1539. srpt_queue_status(cmd);
  1540. return 0;
  1541. } else
  1542. goto send_sense;
  1543. }
  1544. transport_handle_cdb_direct(cmd);
  1545. return 0;
  1546. send_sense:
  1547. transport_send_check_condition_and_sense(cmd, cmd->scsi_sense_reason,
  1548. 0);
  1549. return -1;
  1550. }
  1551. /**
  1552. * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
  1553. * @ch: RDMA channel of the task management request.
  1554. * @fn: Task management function to perform.
  1555. * @req_tag: Tag of the SRP task management request.
  1556. * @mgmt_ioctx: I/O context of the task management request.
  1557. *
  1558. * Returns zero if the target core will process the task management
  1559. * request asynchronously.
  1560. *
  1561. * Note: It is assumed that the initiator serializes tag-based task management
  1562. * requests.
  1563. */
  1564. static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
  1565. {
  1566. struct srpt_device *sdev;
  1567. struct srpt_rdma_ch *ch;
  1568. struct srpt_send_ioctx *target;
  1569. int ret, i;
  1570. ret = -EINVAL;
  1571. ch = ioctx->ch;
  1572. BUG_ON(!ch);
  1573. BUG_ON(!ch->sport);
  1574. sdev = ch->sport->sdev;
  1575. BUG_ON(!sdev);
  1576. spin_lock_irq(&sdev->spinlock);
  1577. for (i = 0; i < ch->rq_size; ++i) {
  1578. target = ch->ioctx_ring[i];
  1579. if (target->cmd.se_lun == ioctx->cmd.se_lun &&
  1580. target->tag == tag &&
  1581. srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
  1582. ret = 0;
  1583. /* now let the target core abort &target->cmd; */
  1584. break;
  1585. }
  1586. }
  1587. spin_unlock_irq(&sdev->spinlock);
  1588. return ret;
  1589. }
  1590. static int srp_tmr_to_tcm(int fn)
  1591. {
  1592. switch (fn) {
  1593. case SRP_TSK_ABORT_TASK:
  1594. return TMR_ABORT_TASK;
  1595. case SRP_TSK_ABORT_TASK_SET:
  1596. return TMR_ABORT_TASK_SET;
  1597. case SRP_TSK_CLEAR_TASK_SET:
  1598. return TMR_CLEAR_TASK_SET;
  1599. case SRP_TSK_LUN_RESET:
  1600. return TMR_LUN_RESET;
  1601. case SRP_TSK_CLEAR_ACA:
  1602. return TMR_CLEAR_ACA;
  1603. default:
  1604. return -1;
  1605. }
  1606. }
  1607. /**
  1608. * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
  1609. *
  1610. * Returns 0 if and only if the request will be processed by the target core.
  1611. *
  1612. * For more information about SRP_TSK_MGMT information units, see also section
  1613. * 6.7 in the SRP r16a document.
  1614. */
  1615. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1616. struct srpt_recv_ioctx *recv_ioctx,
  1617. struct srpt_send_ioctx *send_ioctx)
  1618. {
  1619. struct srp_tsk_mgmt *srp_tsk;
  1620. struct se_cmd *cmd;
  1621. uint64_t unpacked_lun;
  1622. int tcm_tmr;
  1623. int res;
  1624. BUG_ON(!send_ioctx);
  1625. srp_tsk = recv_ioctx->ioctx.buf;
  1626. cmd = &send_ioctx->cmd;
  1627. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
  1628. " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
  1629. srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
  1630. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1631. send_ioctx->tag = srp_tsk->tag;
  1632. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1633. if (tcm_tmr < 0) {
  1634. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1635. send_ioctx->cmd.se_tmr_req->response =
  1636. TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  1637. goto process_tmr;
  1638. }
  1639. res = core_tmr_alloc_req(cmd, NULL, tcm_tmr, GFP_KERNEL);
  1640. if (res < 0) {
  1641. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1642. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1643. goto process_tmr;
  1644. }
  1645. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
  1646. sizeof(srp_tsk->lun));
  1647. res = transport_lookup_tmr_lun(&send_ioctx->cmd, unpacked_lun);
  1648. if (res) {
  1649. pr_debug("rejecting TMR for LUN %lld\n", unpacked_lun);
  1650. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1651. send_ioctx->cmd.se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1652. goto process_tmr;
  1653. }
  1654. if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK)
  1655. srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
  1656. process_tmr:
  1657. kref_get(&send_ioctx->kref);
  1658. if (!(send_ioctx->cmd.se_cmd_flags & SCF_SCSI_CDB_EXCEPTION))
  1659. transport_generic_handle_tmr(&send_ioctx->cmd);
  1660. else
  1661. transport_send_check_condition_and_sense(cmd,
  1662. cmd->scsi_sense_reason, 0);
  1663. }
  1664. /**
  1665. * srpt_handle_new_iu() - Process a newly received information unit.
  1666. * @ch: RDMA channel through which the information unit has been received.
  1667. * @ioctx: SRPT I/O context associated with the information unit.
  1668. */
  1669. static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
  1670. struct srpt_recv_ioctx *recv_ioctx,
  1671. struct srpt_send_ioctx *send_ioctx)
  1672. {
  1673. struct srp_cmd *srp_cmd;
  1674. enum rdma_ch_state ch_state;
  1675. BUG_ON(!ch);
  1676. BUG_ON(!recv_ioctx);
  1677. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1678. recv_ioctx->ioctx.dma, srp_max_req_size,
  1679. DMA_FROM_DEVICE);
  1680. ch_state = srpt_get_ch_state(ch);
  1681. if (unlikely(ch_state == CH_CONNECTING)) {
  1682. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1683. goto out;
  1684. }
  1685. if (unlikely(ch_state != CH_LIVE))
  1686. goto out;
  1687. srp_cmd = recv_ioctx->ioctx.buf;
  1688. if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
  1689. if (!send_ioctx)
  1690. send_ioctx = srpt_get_send_ioctx(ch);
  1691. if (unlikely(!send_ioctx)) {
  1692. list_add_tail(&recv_ioctx->wait_list,
  1693. &ch->cmd_wait_list);
  1694. goto out;
  1695. }
  1696. }
  1697. transport_init_se_cmd(&send_ioctx->cmd, &srpt_target->tf_ops, ch->sess,
  1698. 0, DMA_NONE, MSG_SIMPLE_TAG,
  1699. send_ioctx->sense_data);
  1700. switch (srp_cmd->opcode) {
  1701. case SRP_CMD:
  1702. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1703. break;
  1704. case SRP_TSK_MGMT:
  1705. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1706. break;
  1707. case SRP_I_LOGOUT:
  1708. printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
  1709. break;
  1710. case SRP_CRED_RSP:
  1711. pr_debug("received SRP_CRED_RSP\n");
  1712. break;
  1713. case SRP_AER_RSP:
  1714. pr_debug("received SRP_AER_RSP\n");
  1715. break;
  1716. case SRP_RSP:
  1717. printk(KERN_ERR "Received SRP_RSP\n");
  1718. break;
  1719. default:
  1720. printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
  1721. srp_cmd->opcode);
  1722. break;
  1723. }
  1724. srpt_post_recv(ch->sport->sdev, recv_ioctx);
  1725. out:
  1726. return;
  1727. }
  1728. static void srpt_process_rcv_completion(struct ib_cq *cq,
  1729. struct srpt_rdma_ch *ch,
  1730. struct ib_wc *wc)
  1731. {
  1732. struct srpt_device *sdev = ch->sport->sdev;
  1733. struct srpt_recv_ioctx *ioctx;
  1734. u32 index;
  1735. index = idx_from_wr_id(wc->wr_id);
  1736. if (wc->status == IB_WC_SUCCESS) {
  1737. int req_lim;
  1738. req_lim = atomic_dec_return(&ch->req_lim);
  1739. if (unlikely(req_lim < 0))
  1740. printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
  1741. ioctx = sdev->ioctx_ring[index];
  1742. srpt_handle_new_iu(ch, ioctx, NULL);
  1743. } else {
  1744. printk(KERN_INFO "receiving failed for idx %u with status %d\n",
  1745. index, wc->status);
  1746. }
  1747. }
  1748. /**
  1749. * srpt_process_send_completion() - Process an IB send completion.
  1750. *
  1751. * Note: Although this has not yet been observed during tests, at least in
  1752. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1753. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1754. * value in each response is set to one, and it is possible that this response
  1755. * makes the initiator send a new request before the send completion for that
  1756. * response has been processed. This could e.g. happen if the call to
  1757. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1758. * if IB retransmission causes generation of the send completion to be
  1759. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1760. * are queued on cmd_wait_list. The code below processes these delayed
  1761. * requests one at a time.
  1762. */
  1763. static void srpt_process_send_completion(struct ib_cq *cq,
  1764. struct srpt_rdma_ch *ch,
  1765. struct ib_wc *wc)
  1766. {
  1767. struct srpt_send_ioctx *send_ioctx;
  1768. uint32_t index;
  1769. enum srpt_opcode opcode;
  1770. index = idx_from_wr_id(wc->wr_id);
  1771. opcode = opcode_from_wr_id(wc->wr_id);
  1772. send_ioctx = ch->ioctx_ring[index];
  1773. if (wc->status == IB_WC_SUCCESS) {
  1774. if (opcode == SRPT_SEND)
  1775. srpt_handle_send_comp(ch, send_ioctx);
  1776. else {
  1777. WARN_ON(opcode != SRPT_RDMA_ABORT &&
  1778. wc->opcode != IB_WC_RDMA_READ);
  1779. srpt_handle_rdma_comp(ch, send_ioctx, opcode);
  1780. }
  1781. } else {
  1782. if (opcode == SRPT_SEND) {
  1783. printk(KERN_INFO "sending response for idx %u failed"
  1784. " with status %d\n", index, wc->status);
  1785. srpt_handle_send_err_comp(ch, wc->wr_id);
  1786. } else if (opcode != SRPT_RDMA_MID) {
  1787. printk(KERN_INFO "RDMA t %d for idx %u failed with"
  1788. " status %d", opcode, index, wc->status);
  1789. srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
  1790. }
  1791. }
  1792. while (unlikely(opcode == SRPT_SEND
  1793. && !list_empty(&ch->cmd_wait_list)
  1794. && srpt_get_ch_state(ch) == CH_LIVE
  1795. && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
  1796. struct srpt_recv_ioctx *recv_ioctx;
  1797. recv_ioctx = list_first_entry(&ch->cmd_wait_list,
  1798. struct srpt_recv_ioctx,
  1799. wait_list);
  1800. list_del(&recv_ioctx->wait_list);
  1801. srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
  1802. }
  1803. }
  1804. static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
  1805. {
  1806. struct ib_wc *const wc = ch->wc;
  1807. int i, n;
  1808. WARN_ON(cq != ch->cq);
  1809. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  1810. while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
  1811. for (i = 0; i < n; i++) {
  1812. if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
  1813. srpt_process_rcv_completion(cq, ch, &wc[i]);
  1814. else
  1815. srpt_process_send_completion(cq, ch, &wc[i]);
  1816. }
  1817. }
  1818. }
  1819. /**
  1820. * srpt_completion() - IB completion queue callback function.
  1821. *
  1822. * Notes:
  1823. * - It is guaranteed that a completion handler will never be invoked
  1824. * concurrently on two different CPUs for the same completion queue. See also
  1825. * Documentation/infiniband/core_locking.txt and the implementation of
  1826. * handle_edge_irq() in kernel/irq/chip.c.
  1827. * - When threaded IRQs are enabled, completion handlers are invoked in thread
  1828. * context instead of interrupt context.
  1829. */
  1830. static void srpt_completion(struct ib_cq *cq, void *ctx)
  1831. {
  1832. struct srpt_rdma_ch *ch = ctx;
  1833. wake_up_interruptible(&ch->wait_queue);
  1834. }
  1835. static int srpt_compl_thread(void *arg)
  1836. {
  1837. struct srpt_rdma_ch *ch;
  1838. /* Hibernation / freezing of the SRPT kernel thread is not supported. */
  1839. current->flags |= PF_NOFREEZE;
  1840. ch = arg;
  1841. BUG_ON(!ch);
  1842. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
  1843. ch->sess_name, ch->thread->comm, current->pid);
  1844. while (!kthread_should_stop()) {
  1845. wait_event_interruptible(ch->wait_queue,
  1846. (srpt_process_completion(ch->cq, ch),
  1847. kthread_should_stop()));
  1848. }
  1849. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
  1850. ch->sess_name, ch->thread->comm, current->pid);
  1851. return 0;
  1852. }
  1853. /**
  1854. * srpt_create_ch_ib() - Create receive and send completion queues.
  1855. */
  1856. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1857. {
  1858. struct ib_qp_init_attr *qp_init;
  1859. struct srpt_port *sport = ch->sport;
  1860. struct srpt_device *sdev = sport->sdev;
  1861. u32 srp_sq_size = sport->port_attrib.srp_sq_size;
  1862. int ret;
  1863. WARN_ON(ch->rq_size < 1);
  1864. ret = -ENOMEM;
  1865. qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
  1866. if (!qp_init)
  1867. goto out;
  1868. retry:
  1869. ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
  1870. ch->rq_size + srp_sq_size, 0);
  1871. if (IS_ERR(ch->cq)) {
  1872. ret = PTR_ERR(ch->cq);
  1873. printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
  1874. ch->rq_size + srp_sq_size, ret);
  1875. goto out;
  1876. }
  1877. qp_init->qp_context = (void *)ch;
  1878. qp_init->event_handler
  1879. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1880. qp_init->send_cq = ch->cq;
  1881. qp_init->recv_cq = ch->cq;
  1882. qp_init->srq = sdev->srq;
  1883. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1884. qp_init->qp_type = IB_QPT_RC;
  1885. qp_init->cap.max_send_wr = srp_sq_size;
  1886. qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
  1887. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1888. if (IS_ERR(ch->qp)) {
  1889. ret = PTR_ERR(ch->qp);
  1890. if (ret == -ENOMEM) {
  1891. srp_sq_size /= 2;
  1892. if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
  1893. ib_destroy_cq(ch->cq);
  1894. goto retry;
  1895. }
  1896. }
  1897. printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
  1898. goto err_destroy_cq;
  1899. }
  1900. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1901. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1902. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1903. qp_init->cap.max_send_wr, ch->cm_id);
  1904. ret = srpt_init_ch_qp(ch, ch->qp);
  1905. if (ret)
  1906. goto err_destroy_qp;
  1907. init_waitqueue_head(&ch->wait_queue);
  1908. pr_debug("creating thread for session %s\n", ch->sess_name);
  1909. ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
  1910. if (IS_ERR(ch->thread)) {
  1911. printk(KERN_ERR "failed to create kernel thread %ld\n",
  1912. PTR_ERR(ch->thread));
  1913. ch->thread = NULL;
  1914. goto err_destroy_qp;
  1915. }
  1916. out:
  1917. kfree(qp_init);
  1918. return ret;
  1919. err_destroy_qp:
  1920. ib_destroy_qp(ch->qp);
  1921. err_destroy_cq:
  1922. ib_destroy_cq(ch->cq);
  1923. goto out;
  1924. }
  1925. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1926. {
  1927. if (ch->thread)
  1928. kthread_stop(ch->thread);
  1929. ib_destroy_qp(ch->qp);
  1930. ib_destroy_cq(ch->cq);
  1931. }
  1932. /**
  1933. * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
  1934. *
  1935. * Reset the QP and make sure all resources associated with the channel will
  1936. * be deallocated at an appropriate time.
  1937. *
  1938. * Note: The caller must hold ch->sport->sdev->spinlock.
  1939. */
  1940. static void __srpt_close_ch(struct srpt_rdma_ch *ch)
  1941. {
  1942. struct srpt_device *sdev;
  1943. enum rdma_ch_state prev_state;
  1944. unsigned long flags;
  1945. sdev = ch->sport->sdev;
  1946. spin_lock_irqsave(&ch->spinlock, flags);
  1947. prev_state = ch->state;
  1948. switch (prev_state) {
  1949. case CH_CONNECTING:
  1950. case CH_LIVE:
  1951. ch->state = CH_DISCONNECTING;
  1952. break;
  1953. default:
  1954. break;
  1955. }
  1956. spin_unlock_irqrestore(&ch->spinlock, flags);
  1957. switch (prev_state) {
  1958. case CH_CONNECTING:
  1959. ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
  1960. NULL, 0);
  1961. /* fall through */
  1962. case CH_LIVE:
  1963. if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
  1964. printk(KERN_ERR "sending CM DREQ failed.\n");
  1965. break;
  1966. case CH_DISCONNECTING:
  1967. break;
  1968. case CH_DRAINING:
  1969. case CH_RELEASING:
  1970. break;
  1971. }
  1972. }
  1973. /**
  1974. * srpt_close_ch() - Close an RDMA channel.
  1975. */
  1976. static void srpt_close_ch(struct srpt_rdma_ch *ch)
  1977. {
  1978. struct srpt_device *sdev;
  1979. sdev = ch->sport->sdev;
  1980. spin_lock_irq(&sdev->spinlock);
  1981. __srpt_close_ch(ch);
  1982. spin_unlock_irq(&sdev->spinlock);
  1983. }
  1984. /**
  1985. * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
  1986. * @cm_id: Pointer to the CM ID of the channel to be drained.
  1987. *
  1988. * Note: Must be called from inside srpt_cm_handler to avoid a race between
  1989. * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
  1990. * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
  1991. * waits until all target sessions for the associated IB device have been
  1992. * unregistered and target session registration involves a call to
  1993. * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
  1994. * this function has finished).
  1995. */
  1996. static void srpt_drain_channel(struct ib_cm_id *cm_id)
  1997. {
  1998. struct srpt_device *sdev;
  1999. struct srpt_rdma_ch *ch;
  2000. int ret;
  2001. bool do_reset = false;
  2002. WARN_ON_ONCE(irqs_disabled());
  2003. sdev = cm_id->context;
  2004. BUG_ON(!sdev);
  2005. spin_lock_irq(&sdev->spinlock);
  2006. list_for_each_entry(ch, &sdev->rch_list, list) {
  2007. if (ch->cm_id == cm_id) {
  2008. do_reset = srpt_test_and_set_ch_state(ch,
  2009. CH_CONNECTING, CH_DRAINING) ||
  2010. srpt_test_and_set_ch_state(ch,
  2011. CH_LIVE, CH_DRAINING) ||
  2012. srpt_test_and_set_ch_state(ch,
  2013. CH_DISCONNECTING, CH_DRAINING);
  2014. break;
  2015. }
  2016. }
  2017. spin_unlock_irq(&sdev->spinlock);
  2018. if (do_reset) {
  2019. ret = srpt_ch_qp_err(ch);
  2020. if (ret < 0)
  2021. printk(KERN_ERR "Setting queue pair in error state"
  2022. " failed: %d\n", ret);
  2023. }
  2024. }
  2025. /**
  2026. * srpt_find_channel() - Look up an RDMA channel.
  2027. * @cm_id: Pointer to the CM ID of the channel to be looked up.
  2028. *
  2029. * Return NULL if no matching RDMA channel has been found.
  2030. */
  2031. static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
  2032. struct ib_cm_id *cm_id)
  2033. {
  2034. struct srpt_rdma_ch *ch;
  2035. bool found;
  2036. WARN_ON_ONCE(irqs_disabled());
  2037. BUG_ON(!sdev);
  2038. found = false;
  2039. spin_lock_irq(&sdev->spinlock);
  2040. list_for_each_entry(ch, &sdev->rch_list, list) {
  2041. if (ch->cm_id == cm_id) {
  2042. found = true;
  2043. break;
  2044. }
  2045. }
  2046. spin_unlock_irq(&sdev->spinlock);
  2047. return found ? ch : NULL;
  2048. }
  2049. /**
  2050. * srpt_release_channel() - Release channel resources.
  2051. *
  2052. * Schedules the actual release because:
  2053. * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
  2054. * trigger a deadlock.
  2055. * - It is not safe to call TCM transport_* functions from interrupt context.
  2056. */
  2057. static void srpt_release_channel(struct srpt_rdma_ch *ch)
  2058. {
  2059. schedule_work(&ch->release_work);
  2060. }
  2061. static void srpt_release_channel_work(struct work_struct *w)
  2062. {
  2063. struct srpt_rdma_ch *ch;
  2064. struct srpt_device *sdev;
  2065. ch = container_of(w, struct srpt_rdma_ch, release_work);
  2066. pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
  2067. ch->release_done);
  2068. sdev = ch->sport->sdev;
  2069. BUG_ON(!sdev);
  2070. transport_deregister_session_configfs(ch->sess);
  2071. transport_deregister_session(ch->sess);
  2072. ch->sess = NULL;
  2073. ib_destroy_cm_id(ch->cm_id);
  2074. srpt_destroy_ch_ib(ch);
  2075. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2076. ch->sport->sdev, ch->rq_size,
  2077. ch->rsp_size, DMA_TO_DEVICE);
  2078. spin_lock_irq(&sdev->spinlock);
  2079. list_del(&ch->list);
  2080. spin_unlock_irq(&sdev->spinlock);
  2081. if (ch->release_done)
  2082. complete(ch->release_done);
  2083. wake_up(&sdev->ch_releaseQ);
  2084. kfree(ch);
  2085. }
  2086. static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
  2087. u8 i_port_id[16])
  2088. {
  2089. struct srpt_node_acl *nacl;
  2090. list_for_each_entry(nacl, &sport->port_acl_list, list)
  2091. if (memcmp(nacl->i_port_id, i_port_id,
  2092. sizeof(nacl->i_port_id)) == 0)
  2093. return nacl;
  2094. return NULL;
  2095. }
  2096. static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
  2097. u8 i_port_id[16])
  2098. {
  2099. struct srpt_node_acl *nacl;
  2100. spin_lock_irq(&sport->port_acl_lock);
  2101. nacl = __srpt_lookup_acl(sport, i_port_id);
  2102. spin_unlock_irq(&sport->port_acl_lock);
  2103. return nacl;
  2104. }
  2105. /**
  2106. * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
  2107. *
  2108. * Ownership of the cm_id is transferred to the target session if this
  2109. * functions returns zero. Otherwise the caller remains the owner of cm_id.
  2110. */
  2111. static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
  2112. struct ib_cm_req_event_param *param,
  2113. void *private_data)
  2114. {
  2115. struct srpt_device *sdev = cm_id->context;
  2116. struct srpt_port *sport = &sdev->port[param->port - 1];
  2117. struct srp_login_req *req;
  2118. struct srp_login_rsp *rsp;
  2119. struct srp_login_rej *rej;
  2120. struct ib_cm_rep_param *rep_param;
  2121. struct srpt_rdma_ch *ch, *tmp_ch;
  2122. struct srpt_node_acl *nacl;
  2123. u32 it_iu_len;
  2124. int i;
  2125. int ret = 0;
  2126. WARN_ON_ONCE(irqs_disabled());
  2127. if (WARN_ON(!sdev || !private_data))
  2128. return -EINVAL;
  2129. req = (struct srp_login_req *)private_data;
  2130. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  2131. printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
  2132. " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
  2133. " (guid=0x%llx:0x%llx)\n",
  2134. be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
  2135. be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
  2136. be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
  2137. be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
  2138. it_iu_len,
  2139. param->port,
  2140. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
  2141. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
  2142. rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
  2143. rej = kzalloc(sizeof *rej, GFP_KERNEL);
  2144. rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
  2145. if (!rsp || !rej || !rep_param) {
  2146. ret = -ENOMEM;
  2147. goto out;
  2148. }
  2149. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  2150. rej->reason = __constant_cpu_to_be32(
  2151. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  2152. ret = -EINVAL;
  2153. printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
  2154. " length (%d bytes) is out of range (%d .. %d)\n",
  2155. it_iu_len, 64, srp_max_req_size);
  2156. goto reject;
  2157. }
  2158. if (!sport->enabled) {
  2159. rej->reason = __constant_cpu_to_be32(
  2160. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2161. ret = -EINVAL;
  2162. printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
  2163. " has not yet been enabled\n");
  2164. goto reject;
  2165. }
  2166. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  2167. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  2168. spin_lock_irq(&sdev->spinlock);
  2169. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
  2170. if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
  2171. && !memcmp(ch->t_port_id, req->target_port_id, 16)
  2172. && param->port == ch->sport->port
  2173. && param->listen_id == ch->sport->sdev->cm_id
  2174. && ch->cm_id) {
  2175. enum rdma_ch_state ch_state;
  2176. ch_state = srpt_get_ch_state(ch);
  2177. if (ch_state != CH_CONNECTING
  2178. && ch_state != CH_LIVE)
  2179. continue;
  2180. /* found an existing channel */
  2181. pr_debug("Found existing channel %s"
  2182. " cm_id= %p state= %d\n",
  2183. ch->sess_name, ch->cm_id, ch_state);
  2184. __srpt_close_ch(ch);
  2185. rsp->rsp_flags =
  2186. SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  2187. }
  2188. }
  2189. spin_unlock_irq(&sdev->spinlock);
  2190. } else
  2191. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  2192. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  2193. || *(__be64 *)(req->target_port_id + 8) !=
  2194. cpu_to_be64(srpt_service_guid)) {
  2195. rej->reason = __constant_cpu_to_be32(
  2196. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  2197. ret = -ENOMEM;
  2198. printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
  2199. " has an invalid target port identifier.\n");
  2200. goto reject;
  2201. }
  2202. ch = kzalloc(sizeof *ch, GFP_KERNEL);
  2203. if (!ch) {
  2204. rej->reason = __constant_cpu_to_be32(
  2205. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2206. printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
  2207. ret = -ENOMEM;
  2208. goto reject;
  2209. }
  2210. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  2211. memcpy(ch->i_port_id, req->initiator_port_id, 16);
  2212. memcpy(ch->t_port_id, req->target_port_id, 16);
  2213. ch->sport = &sdev->port[param->port - 1];
  2214. ch->cm_id = cm_id;
  2215. /*
  2216. * Avoid QUEUE_FULL conditions by limiting the number of buffers used
  2217. * for the SRP protocol to the command queue size.
  2218. */
  2219. ch->rq_size = SRPT_RQ_SIZE;
  2220. spin_lock_init(&ch->spinlock);
  2221. ch->state = CH_CONNECTING;
  2222. INIT_LIST_HEAD(&ch->cmd_wait_list);
  2223. ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  2224. ch->ioctx_ring = (struct srpt_send_ioctx **)
  2225. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  2226. sizeof(*ch->ioctx_ring[0]),
  2227. ch->rsp_size, DMA_TO_DEVICE);
  2228. if (!ch->ioctx_ring)
  2229. goto free_ch;
  2230. INIT_LIST_HEAD(&ch->free_list);
  2231. for (i = 0; i < ch->rq_size; i++) {
  2232. ch->ioctx_ring[i]->ch = ch;
  2233. list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
  2234. }
  2235. ret = srpt_create_ch_ib(ch);
  2236. if (ret) {
  2237. rej->reason = __constant_cpu_to_be32(
  2238. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2239. printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
  2240. " a new RDMA channel failed.\n");
  2241. goto free_ring;
  2242. }
  2243. ret = srpt_ch_qp_rtr(ch, ch->qp);
  2244. if (ret) {
  2245. rej->reason = __constant_cpu_to_be32(
  2246. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2247. printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
  2248. " RTR failed (error code = %d)\n", ret);
  2249. goto destroy_ib;
  2250. }
  2251. /*
  2252. * Use the initator port identifier as the session name.
  2253. */
  2254. snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
  2255. be64_to_cpu(*(__be64 *)ch->i_port_id),
  2256. be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
  2257. pr_debug("registering session %s\n", ch->sess_name);
  2258. nacl = srpt_lookup_acl(sport, ch->i_port_id);
  2259. if (!nacl) {
  2260. printk(KERN_INFO "Rejected login because no ACL has been"
  2261. " configured yet for initiator %s.\n", ch->sess_name);
  2262. rej->reason = __constant_cpu_to_be32(
  2263. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  2264. goto destroy_ib;
  2265. }
  2266. ch->sess = transport_init_session();
  2267. if (IS_ERR(ch->sess)) {
  2268. rej->reason = __constant_cpu_to_be32(
  2269. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2270. pr_debug("Failed to create session\n");
  2271. goto deregister_session;
  2272. }
  2273. ch->sess->se_node_acl = &nacl->nacl;
  2274. transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
  2275. pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
  2276. ch->sess_name, ch->cm_id);
  2277. /* create srp_login_response */
  2278. rsp->opcode = SRP_LOGIN_RSP;
  2279. rsp->tag = req->tag;
  2280. rsp->max_it_iu_len = req->req_it_iu_len;
  2281. rsp->max_ti_iu_len = req->req_it_iu_len;
  2282. ch->max_ti_iu_len = it_iu_len;
  2283. rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2284. | SRP_BUF_FORMAT_INDIRECT);
  2285. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  2286. atomic_set(&ch->req_lim, ch->rq_size);
  2287. atomic_set(&ch->req_lim_delta, 0);
  2288. /* create cm reply */
  2289. rep_param->qp_num = ch->qp->qp_num;
  2290. rep_param->private_data = (void *)rsp;
  2291. rep_param->private_data_len = sizeof *rsp;
  2292. rep_param->rnr_retry_count = 7;
  2293. rep_param->flow_control = 1;
  2294. rep_param->failover_accepted = 0;
  2295. rep_param->srq = 1;
  2296. rep_param->responder_resources = 4;
  2297. rep_param->initiator_depth = 4;
  2298. ret = ib_send_cm_rep(cm_id, rep_param);
  2299. if (ret) {
  2300. printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
  2301. " (error code = %d)\n", ret);
  2302. goto release_channel;
  2303. }
  2304. spin_lock_irq(&sdev->spinlock);
  2305. list_add_tail(&ch->list, &sdev->rch_list);
  2306. spin_unlock_irq(&sdev->spinlock);
  2307. goto out;
  2308. release_channel:
  2309. srpt_set_ch_state(ch, CH_RELEASING);
  2310. transport_deregister_session_configfs(ch->sess);
  2311. deregister_session:
  2312. transport_deregister_session(ch->sess);
  2313. ch->sess = NULL;
  2314. destroy_ib:
  2315. srpt_destroy_ch_ib(ch);
  2316. free_ring:
  2317. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2318. ch->sport->sdev, ch->rq_size,
  2319. ch->rsp_size, DMA_TO_DEVICE);
  2320. free_ch:
  2321. kfree(ch);
  2322. reject:
  2323. rej->opcode = SRP_LOGIN_REJ;
  2324. rej->tag = req->tag;
  2325. rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2326. | SRP_BUF_FORMAT_INDIRECT);
  2327. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  2328. (void *)rej, sizeof *rej);
  2329. out:
  2330. kfree(rep_param);
  2331. kfree(rsp);
  2332. kfree(rej);
  2333. return ret;
  2334. }
  2335. static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
  2336. {
  2337. printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
  2338. srpt_drain_channel(cm_id);
  2339. }
  2340. /**
  2341. * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
  2342. *
  2343. * An IB_CM_RTU_RECEIVED message indicates that the connection is established
  2344. * and that the recipient may begin transmitting (RTU = ready to use).
  2345. */
  2346. static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
  2347. {
  2348. struct srpt_rdma_ch *ch;
  2349. int ret;
  2350. ch = srpt_find_channel(cm_id->context, cm_id);
  2351. BUG_ON(!ch);
  2352. if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
  2353. struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
  2354. ret = srpt_ch_qp_rts(ch, ch->qp);
  2355. list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
  2356. wait_list) {
  2357. list_del(&ioctx->wait_list);
  2358. srpt_handle_new_iu(ch, ioctx, NULL);
  2359. }
  2360. if (ret)
  2361. srpt_close_ch(ch);
  2362. }
  2363. }
  2364. static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
  2365. {
  2366. printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
  2367. srpt_drain_channel(cm_id);
  2368. }
  2369. static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
  2370. {
  2371. printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
  2372. srpt_drain_channel(cm_id);
  2373. }
  2374. /**
  2375. * srpt_cm_dreq_recv() - Process reception of a DREQ message.
  2376. */
  2377. static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
  2378. {
  2379. struct srpt_rdma_ch *ch;
  2380. unsigned long flags;
  2381. bool send_drep = false;
  2382. ch = srpt_find_channel(cm_id->context, cm_id);
  2383. BUG_ON(!ch);
  2384. pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
  2385. spin_lock_irqsave(&ch->spinlock, flags);
  2386. switch (ch->state) {
  2387. case CH_CONNECTING:
  2388. case CH_LIVE:
  2389. send_drep = true;
  2390. ch->state = CH_DISCONNECTING;
  2391. break;
  2392. case CH_DISCONNECTING:
  2393. case CH_DRAINING:
  2394. case CH_RELEASING:
  2395. WARN(true, "unexpected channel state %d\n", ch->state);
  2396. break;
  2397. }
  2398. spin_unlock_irqrestore(&ch->spinlock, flags);
  2399. if (send_drep) {
  2400. if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
  2401. printk(KERN_ERR "Sending IB DREP failed.\n");
  2402. printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
  2403. ch->sess_name);
  2404. }
  2405. }
  2406. /**
  2407. * srpt_cm_drep_recv() - Process reception of a DREP message.
  2408. */
  2409. static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
  2410. {
  2411. printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
  2412. cm_id);
  2413. srpt_drain_channel(cm_id);
  2414. }
  2415. /**
  2416. * srpt_cm_handler() - IB connection manager callback function.
  2417. *
  2418. * A non-zero return value will cause the caller destroy the CM ID.
  2419. *
  2420. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  2421. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  2422. * a non-zero value in any other case will trigger a race with the
  2423. * ib_destroy_cm_id() call in srpt_release_channel().
  2424. */
  2425. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  2426. {
  2427. int ret;
  2428. ret = 0;
  2429. switch (event->event) {
  2430. case IB_CM_REQ_RECEIVED:
  2431. ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
  2432. event->private_data);
  2433. break;
  2434. case IB_CM_REJ_RECEIVED:
  2435. srpt_cm_rej_recv(cm_id);
  2436. break;
  2437. case IB_CM_RTU_RECEIVED:
  2438. case IB_CM_USER_ESTABLISHED:
  2439. srpt_cm_rtu_recv(cm_id);
  2440. break;
  2441. case IB_CM_DREQ_RECEIVED:
  2442. srpt_cm_dreq_recv(cm_id);
  2443. break;
  2444. case IB_CM_DREP_RECEIVED:
  2445. srpt_cm_drep_recv(cm_id);
  2446. break;
  2447. case IB_CM_TIMEWAIT_EXIT:
  2448. srpt_cm_timewait_exit(cm_id);
  2449. break;
  2450. case IB_CM_REP_ERROR:
  2451. srpt_cm_rep_error(cm_id);
  2452. break;
  2453. case IB_CM_DREQ_ERROR:
  2454. printk(KERN_INFO "Received IB DREQ ERROR event.\n");
  2455. break;
  2456. case IB_CM_MRA_RECEIVED:
  2457. printk(KERN_INFO "Received IB MRA event\n");
  2458. break;
  2459. default:
  2460. printk(KERN_ERR "received unrecognized IB CM event %d\n",
  2461. event->event);
  2462. break;
  2463. }
  2464. return ret;
  2465. }
  2466. /**
  2467. * srpt_perform_rdmas() - Perform IB RDMA.
  2468. *
  2469. * Returns zero upon success or a negative number upon failure.
  2470. */
  2471. static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
  2472. struct srpt_send_ioctx *ioctx)
  2473. {
  2474. struct ib_send_wr wr;
  2475. struct ib_send_wr *bad_wr;
  2476. struct rdma_iu *riu;
  2477. int i;
  2478. int ret;
  2479. int sq_wr_avail;
  2480. enum dma_data_direction dir;
  2481. const int n_rdma = ioctx->n_rdma;
  2482. dir = ioctx->cmd.data_direction;
  2483. if (dir == DMA_TO_DEVICE) {
  2484. /* write */
  2485. ret = -ENOMEM;
  2486. sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
  2487. if (sq_wr_avail < 0) {
  2488. printk(KERN_WARNING "IB send queue full (needed %d)\n",
  2489. n_rdma);
  2490. goto out;
  2491. }
  2492. }
  2493. ioctx->rdma_aborted = false;
  2494. ret = 0;
  2495. riu = ioctx->rdma_ius;
  2496. memset(&wr, 0, sizeof wr);
  2497. for (i = 0; i < n_rdma; ++i, ++riu) {
  2498. if (dir == DMA_FROM_DEVICE) {
  2499. wr.opcode = IB_WR_RDMA_WRITE;
  2500. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2501. SRPT_RDMA_WRITE_LAST :
  2502. SRPT_RDMA_MID,
  2503. ioctx->ioctx.index);
  2504. } else {
  2505. wr.opcode = IB_WR_RDMA_READ;
  2506. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2507. SRPT_RDMA_READ_LAST :
  2508. SRPT_RDMA_MID,
  2509. ioctx->ioctx.index);
  2510. }
  2511. wr.next = NULL;
  2512. wr.wr.rdma.remote_addr = riu->raddr;
  2513. wr.wr.rdma.rkey = riu->rkey;
  2514. wr.num_sge = riu->sge_cnt;
  2515. wr.sg_list = riu->sge;
  2516. /* only get completion event for the last rdma write */
  2517. if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
  2518. wr.send_flags = IB_SEND_SIGNALED;
  2519. ret = ib_post_send(ch->qp, &wr, &bad_wr);
  2520. if (ret)
  2521. break;
  2522. }
  2523. if (ret)
  2524. printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
  2525. __func__, __LINE__, ret, i, n_rdma);
  2526. if (ret && i > 0) {
  2527. wr.num_sge = 0;
  2528. wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
  2529. wr.send_flags = IB_SEND_SIGNALED;
  2530. while (ch->state == CH_LIVE &&
  2531. ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
  2532. printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
  2533. ioctx->ioctx.index);
  2534. msleep(1000);
  2535. }
  2536. while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
  2537. printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
  2538. ioctx->ioctx.index);
  2539. msleep(1000);
  2540. }
  2541. }
  2542. out:
  2543. if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
  2544. atomic_add(n_rdma, &ch->sq_wr_avail);
  2545. return ret;
  2546. }
  2547. /**
  2548. * srpt_xfer_data() - Start data transfer from initiator to target.
  2549. */
  2550. static int srpt_xfer_data(struct srpt_rdma_ch *ch,
  2551. struct srpt_send_ioctx *ioctx)
  2552. {
  2553. int ret;
  2554. ret = srpt_map_sg_to_ib_sge(ch, ioctx);
  2555. if (ret) {
  2556. printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
  2557. goto out;
  2558. }
  2559. ret = srpt_perform_rdmas(ch, ioctx);
  2560. if (ret) {
  2561. if (ret == -EAGAIN || ret == -ENOMEM)
  2562. printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
  2563. __func__, __LINE__, ret);
  2564. else
  2565. printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
  2566. __func__, __LINE__, ret);
  2567. goto out_unmap;
  2568. }
  2569. out:
  2570. return ret;
  2571. out_unmap:
  2572. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2573. goto out;
  2574. }
  2575. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  2576. {
  2577. struct srpt_send_ioctx *ioctx;
  2578. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2579. return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
  2580. }
  2581. /*
  2582. * srpt_write_pending() - Start data transfer from initiator to target (write).
  2583. */
  2584. static int srpt_write_pending(struct se_cmd *se_cmd)
  2585. {
  2586. struct srpt_rdma_ch *ch;
  2587. struct srpt_send_ioctx *ioctx;
  2588. enum srpt_command_state new_state;
  2589. enum rdma_ch_state ch_state;
  2590. int ret;
  2591. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2592. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  2593. WARN_ON(new_state == SRPT_STATE_DONE);
  2594. ch = ioctx->ch;
  2595. BUG_ON(!ch);
  2596. ch_state = srpt_get_ch_state(ch);
  2597. switch (ch_state) {
  2598. case CH_CONNECTING:
  2599. WARN(true, "unexpected channel state %d\n", ch_state);
  2600. ret = -EINVAL;
  2601. goto out;
  2602. case CH_LIVE:
  2603. break;
  2604. case CH_DISCONNECTING:
  2605. case CH_DRAINING:
  2606. case CH_RELEASING:
  2607. pr_debug("cmd with tag %lld: channel disconnecting\n",
  2608. ioctx->tag);
  2609. srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
  2610. ret = -EINVAL;
  2611. goto out;
  2612. }
  2613. ret = srpt_xfer_data(ch, ioctx);
  2614. out:
  2615. return ret;
  2616. }
  2617. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  2618. {
  2619. switch (tcm_mgmt_status) {
  2620. case TMR_FUNCTION_COMPLETE:
  2621. return SRP_TSK_MGMT_SUCCESS;
  2622. case TMR_FUNCTION_REJECTED:
  2623. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  2624. }
  2625. return SRP_TSK_MGMT_FAILED;
  2626. }
  2627. /**
  2628. * srpt_queue_response() - Transmits the response to a SCSI command.
  2629. *
  2630. * Callback function called by the TCM core. Must not block since it can be
  2631. * invoked on the context of the IB completion handler.
  2632. */
  2633. static int srpt_queue_response(struct se_cmd *cmd)
  2634. {
  2635. struct srpt_rdma_ch *ch;
  2636. struct srpt_send_ioctx *ioctx;
  2637. enum srpt_command_state state;
  2638. unsigned long flags;
  2639. int ret;
  2640. enum dma_data_direction dir;
  2641. int resp_len;
  2642. u8 srp_tm_status;
  2643. ret = 0;
  2644. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2645. ch = ioctx->ch;
  2646. BUG_ON(!ch);
  2647. spin_lock_irqsave(&ioctx->spinlock, flags);
  2648. state = ioctx->state;
  2649. switch (state) {
  2650. case SRPT_STATE_NEW:
  2651. case SRPT_STATE_DATA_IN:
  2652. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  2653. break;
  2654. case SRPT_STATE_MGMT:
  2655. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  2656. break;
  2657. default:
  2658. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  2659. ch, ioctx->ioctx.index, ioctx->state);
  2660. break;
  2661. }
  2662. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  2663. if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
  2664. || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
  2665. atomic_inc(&ch->req_lim_delta);
  2666. srpt_abort_cmd(ioctx);
  2667. goto out;
  2668. }
  2669. dir = ioctx->cmd.data_direction;
  2670. /* For read commands, transfer the data to the initiator. */
  2671. if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
  2672. !ioctx->queue_status_only) {
  2673. ret = srpt_xfer_data(ch, ioctx);
  2674. if (ret) {
  2675. printk(KERN_ERR "xfer_data failed for tag %llu\n",
  2676. ioctx->tag);
  2677. goto out;
  2678. }
  2679. }
  2680. if (state != SRPT_STATE_MGMT)
  2681. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
  2682. cmd->scsi_status);
  2683. else {
  2684. srp_tm_status
  2685. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2686. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2687. ioctx->tag);
  2688. }
  2689. ret = srpt_post_send(ch, ioctx, resp_len);
  2690. if (ret) {
  2691. printk(KERN_ERR "sending cmd response failed for tag %llu\n",
  2692. ioctx->tag);
  2693. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2694. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2695. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  2696. }
  2697. out:
  2698. return ret;
  2699. }
  2700. static int srpt_queue_status(struct se_cmd *cmd)
  2701. {
  2702. struct srpt_send_ioctx *ioctx;
  2703. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2704. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2705. if (cmd->se_cmd_flags &
  2706. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2707. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2708. ioctx->queue_status_only = true;
  2709. return srpt_queue_response(cmd);
  2710. }
  2711. static void srpt_refresh_port_work(struct work_struct *work)
  2712. {
  2713. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2714. srpt_refresh_port(sport);
  2715. }
  2716. static int srpt_ch_list_empty(struct srpt_device *sdev)
  2717. {
  2718. int res;
  2719. spin_lock_irq(&sdev->spinlock);
  2720. res = list_empty(&sdev->rch_list);
  2721. spin_unlock_irq(&sdev->spinlock);
  2722. return res;
  2723. }
  2724. /**
  2725. * srpt_release_sdev() - Free the channel resources associated with a target.
  2726. */
  2727. static int srpt_release_sdev(struct srpt_device *sdev)
  2728. {
  2729. struct srpt_rdma_ch *ch, *tmp_ch;
  2730. int res;
  2731. WARN_ON_ONCE(irqs_disabled());
  2732. BUG_ON(!sdev);
  2733. spin_lock_irq(&sdev->spinlock);
  2734. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
  2735. __srpt_close_ch(ch);
  2736. spin_unlock_irq(&sdev->spinlock);
  2737. res = wait_event_interruptible(sdev->ch_releaseQ,
  2738. srpt_ch_list_empty(sdev));
  2739. if (res)
  2740. printk(KERN_ERR "%s: interrupted.\n", __func__);
  2741. return 0;
  2742. }
  2743. static struct srpt_port *__srpt_lookup_port(const char *name)
  2744. {
  2745. struct ib_device *dev;
  2746. struct srpt_device *sdev;
  2747. struct srpt_port *sport;
  2748. int i;
  2749. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2750. dev = sdev->device;
  2751. if (!dev)
  2752. continue;
  2753. for (i = 0; i < dev->phys_port_cnt; i++) {
  2754. sport = &sdev->port[i];
  2755. if (!strcmp(sport->port_guid, name))
  2756. return sport;
  2757. }
  2758. }
  2759. return NULL;
  2760. }
  2761. static struct srpt_port *srpt_lookup_port(const char *name)
  2762. {
  2763. struct srpt_port *sport;
  2764. spin_lock(&srpt_dev_lock);
  2765. sport = __srpt_lookup_port(name);
  2766. spin_unlock(&srpt_dev_lock);
  2767. return sport;
  2768. }
  2769. /**
  2770. * srpt_add_one() - Infiniband device addition callback function.
  2771. */
  2772. static void srpt_add_one(struct ib_device *device)
  2773. {
  2774. struct srpt_device *sdev;
  2775. struct srpt_port *sport;
  2776. struct ib_srq_init_attr srq_attr;
  2777. int i;
  2778. pr_debug("device = %p, device->dma_ops = %p\n", device,
  2779. device->dma_ops);
  2780. sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
  2781. if (!sdev)
  2782. goto err;
  2783. sdev->device = device;
  2784. INIT_LIST_HEAD(&sdev->rch_list);
  2785. init_waitqueue_head(&sdev->ch_releaseQ);
  2786. spin_lock_init(&sdev->spinlock);
  2787. if (ib_query_device(device, &sdev->dev_attr))
  2788. goto free_dev;
  2789. sdev->pd = ib_alloc_pd(device);
  2790. if (IS_ERR(sdev->pd))
  2791. goto free_dev;
  2792. sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
  2793. if (IS_ERR(sdev->mr))
  2794. goto err_pd;
  2795. sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
  2796. srq_attr.event_handler = srpt_srq_event;
  2797. srq_attr.srq_context = (void *)sdev;
  2798. srq_attr.attr.max_wr = sdev->srq_size;
  2799. srq_attr.attr.max_sge = 1;
  2800. srq_attr.attr.srq_limit = 0;
  2801. srq_attr.srq_type = IB_SRQT_BASIC;
  2802. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2803. if (IS_ERR(sdev->srq))
  2804. goto err_mr;
  2805. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2806. __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
  2807. device->name);
  2808. if (!srpt_service_guid)
  2809. srpt_service_guid = be64_to_cpu(device->node_guid);
  2810. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2811. if (IS_ERR(sdev->cm_id))
  2812. goto err_srq;
  2813. /* print out target login information */
  2814. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2815. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2816. srpt_service_guid, srpt_service_guid);
  2817. /*
  2818. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2819. * to identify this target. We currently use the guid of the first HCA
  2820. * in the system as service_id; therefore, the target_id will change
  2821. * if this HCA is gone bad and replaced by different HCA
  2822. */
  2823. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
  2824. goto err_cm;
  2825. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2826. srpt_event_handler);
  2827. if (ib_register_event_handler(&sdev->event_handler))
  2828. goto err_cm;
  2829. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2830. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2831. sizeof(*sdev->ioctx_ring[0]),
  2832. srp_max_req_size, DMA_FROM_DEVICE);
  2833. if (!sdev->ioctx_ring)
  2834. goto err_event;
  2835. for (i = 0; i < sdev->srq_size; ++i)
  2836. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2837. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2838. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2839. sport = &sdev->port[i - 1];
  2840. sport->sdev = sdev;
  2841. sport->port = i;
  2842. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2843. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2844. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2845. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2846. INIT_LIST_HEAD(&sport->port_acl_list);
  2847. spin_lock_init(&sport->port_acl_lock);
  2848. if (srpt_refresh_port(sport)) {
  2849. printk(KERN_ERR "MAD registration failed for %s-%d.\n",
  2850. srpt_sdev_name(sdev), i);
  2851. goto err_ring;
  2852. }
  2853. snprintf(sport->port_guid, sizeof(sport->port_guid),
  2854. "0x%016llx%016llx",
  2855. be64_to_cpu(sport->gid.global.subnet_prefix),
  2856. be64_to_cpu(sport->gid.global.interface_id));
  2857. }
  2858. spin_lock(&srpt_dev_lock);
  2859. list_add_tail(&sdev->list, &srpt_dev_list);
  2860. spin_unlock(&srpt_dev_lock);
  2861. out:
  2862. ib_set_client_data(device, &srpt_client, sdev);
  2863. pr_debug("added %s.\n", device->name);
  2864. return;
  2865. err_ring:
  2866. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2867. sdev->srq_size, srp_max_req_size,
  2868. DMA_FROM_DEVICE);
  2869. err_event:
  2870. ib_unregister_event_handler(&sdev->event_handler);
  2871. err_cm:
  2872. ib_destroy_cm_id(sdev->cm_id);
  2873. err_srq:
  2874. ib_destroy_srq(sdev->srq);
  2875. err_mr:
  2876. ib_dereg_mr(sdev->mr);
  2877. err_pd:
  2878. ib_dealloc_pd(sdev->pd);
  2879. free_dev:
  2880. kfree(sdev);
  2881. err:
  2882. sdev = NULL;
  2883. printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
  2884. goto out;
  2885. }
  2886. /**
  2887. * srpt_remove_one() - InfiniBand device removal callback function.
  2888. */
  2889. static void srpt_remove_one(struct ib_device *device)
  2890. {
  2891. struct srpt_device *sdev;
  2892. int i;
  2893. sdev = ib_get_client_data(device, &srpt_client);
  2894. if (!sdev) {
  2895. printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
  2896. device->name);
  2897. return;
  2898. }
  2899. srpt_unregister_mad_agent(sdev);
  2900. ib_unregister_event_handler(&sdev->event_handler);
  2901. /* Cancel any work queued by the just unregistered IB event handler. */
  2902. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2903. cancel_work_sync(&sdev->port[i].work);
  2904. ib_destroy_cm_id(sdev->cm_id);
  2905. /*
  2906. * Unregistering a target must happen after destroying sdev->cm_id
  2907. * such that no new SRP_LOGIN_REQ information units can arrive while
  2908. * destroying the target.
  2909. */
  2910. spin_lock(&srpt_dev_lock);
  2911. list_del(&sdev->list);
  2912. spin_unlock(&srpt_dev_lock);
  2913. srpt_release_sdev(sdev);
  2914. ib_destroy_srq(sdev->srq);
  2915. ib_dereg_mr(sdev->mr);
  2916. ib_dealloc_pd(sdev->pd);
  2917. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2918. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2919. sdev->ioctx_ring = NULL;
  2920. kfree(sdev);
  2921. }
  2922. static struct ib_client srpt_client = {
  2923. .name = DRV_NAME,
  2924. .add = srpt_add_one,
  2925. .remove = srpt_remove_one
  2926. };
  2927. static int srpt_check_true(struct se_portal_group *se_tpg)
  2928. {
  2929. return 1;
  2930. }
  2931. static int srpt_check_false(struct se_portal_group *se_tpg)
  2932. {
  2933. return 0;
  2934. }
  2935. static char *srpt_get_fabric_name(void)
  2936. {
  2937. return "srpt";
  2938. }
  2939. static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  2940. {
  2941. return SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2942. }
  2943. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2944. {
  2945. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  2946. return sport->port_guid;
  2947. }
  2948. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2949. {
  2950. return 1;
  2951. }
  2952. static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
  2953. {
  2954. return 1;
  2955. }
  2956. static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
  2957. struct se_node_acl *se_nacl,
  2958. struct t10_pr_registration *pr_reg,
  2959. int *format_code, unsigned char *buf)
  2960. {
  2961. struct srpt_node_acl *nacl;
  2962. struct spc_rdma_transport_id *tr_id;
  2963. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  2964. tr_id = (void *)buf;
  2965. tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2966. memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
  2967. return sizeof(*tr_id);
  2968. }
  2969. static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  2970. struct se_node_acl *se_nacl,
  2971. struct t10_pr_registration *pr_reg,
  2972. int *format_code)
  2973. {
  2974. *format_code = 0;
  2975. return sizeof(struct spc_rdma_transport_id);
  2976. }
  2977. static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  2978. const char *buf, u32 *out_tid_len,
  2979. char **port_nexus_ptr)
  2980. {
  2981. struct spc_rdma_transport_id *tr_id;
  2982. *port_nexus_ptr = NULL;
  2983. *out_tid_len = sizeof(struct spc_rdma_transport_id);
  2984. tr_id = (void *)buf;
  2985. return (char *)tr_id->i_port_id;
  2986. }
  2987. static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
  2988. {
  2989. struct srpt_node_acl *nacl;
  2990. nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
  2991. if (!nacl) {
  2992. printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
  2993. return NULL;
  2994. }
  2995. return &nacl->nacl;
  2996. }
  2997. static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
  2998. struct se_node_acl *se_nacl)
  2999. {
  3000. struct srpt_node_acl *nacl;
  3001. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3002. kfree(nacl);
  3003. }
  3004. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  3005. {
  3006. return 1;
  3007. }
  3008. static void srpt_release_cmd(struct se_cmd *se_cmd)
  3009. {
  3010. }
  3011. /**
  3012. * srpt_shutdown_session() - Whether or not a session may be shut down.
  3013. */
  3014. static int srpt_shutdown_session(struct se_session *se_sess)
  3015. {
  3016. return true;
  3017. }
  3018. /**
  3019. * srpt_close_session() - Forcibly close a session.
  3020. *
  3021. * Callback function invoked by the TCM core to clean up sessions associated
  3022. * with a node ACL when the user invokes
  3023. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3024. */
  3025. static void srpt_close_session(struct se_session *se_sess)
  3026. {
  3027. DECLARE_COMPLETION_ONSTACK(release_done);
  3028. struct srpt_rdma_ch *ch;
  3029. struct srpt_device *sdev;
  3030. int res;
  3031. ch = se_sess->fabric_sess_ptr;
  3032. WARN_ON(ch->sess != se_sess);
  3033. pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
  3034. sdev = ch->sport->sdev;
  3035. spin_lock_irq(&sdev->spinlock);
  3036. BUG_ON(ch->release_done);
  3037. ch->release_done = &release_done;
  3038. __srpt_close_ch(ch);
  3039. spin_unlock_irq(&sdev->spinlock);
  3040. res = wait_for_completion_timeout(&release_done, 60 * HZ);
  3041. WARN_ON(res <= 0);
  3042. }
  3043. /**
  3044. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  3045. *
  3046. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  3047. * This object represents an arbitrary integer used to uniquely identify a
  3048. * particular attached remote initiator port to a particular SCSI target port
  3049. * within a particular SCSI target device within a particular SCSI instance.
  3050. */
  3051. static u32 srpt_sess_get_index(struct se_session *se_sess)
  3052. {
  3053. return 0;
  3054. }
  3055. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  3056. {
  3057. }
  3058. static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
  3059. {
  3060. struct srpt_send_ioctx *ioctx;
  3061. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3062. return ioctx->tag;
  3063. }
  3064. /* Note: only used from inside debug printk's by the TCM core. */
  3065. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  3066. {
  3067. struct srpt_send_ioctx *ioctx;
  3068. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3069. return srpt_get_cmd_state(ioctx);
  3070. }
  3071. static u16 srpt_set_fabric_sense_len(struct se_cmd *cmd, u32 sense_length)
  3072. {
  3073. return 0;
  3074. }
  3075. static u16 srpt_get_fabric_sense_len(void)
  3076. {
  3077. return 0;
  3078. }
  3079. /**
  3080. * srpt_parse_i_port_id() - Parse an initiator port ID.
  3081. * @name: ASCII representation of a 128-bit initiator port ID.
  3082. * @i_port_id: Binary 128-bit port ID.
  3083. */
  3084. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  3085. {
  3086. const char *p;
  3087. unsigned len, count, leading_zero_bytes;
  3088. int ret, rc;
  3089. p = name;
  3090. if (strnicmp(p, "0x", 2) == 0)
  3091. p += 2;
  3092. ret = -EINVAL;
  3093. len = strlen(p);
  3094. if (len % 2)
  3095. goto out;
  3096. count = min(len / 2, 16U);
  3097. leading_zero_bytes = 16 - count;
  3098. memset(i_port_id, 0, leading_zero_bytes);
  3099. rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
  3100. if (rc < 0)
  3101. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
  3102. ret = 0;
  3103. out:
  3104. return ret;
  3105. }
  3106. /*
  3107. * configfs callback function invoked for
  3108. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3109. */
  3110. static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
  3111. struct config_group *group,
  3112. const char *name)
  3113. {
  3114. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  3115. struct se_node_acl *se_nacl, *se_nacl_new;
  3116. struct srpt_node_acl *nacl;
  3117. int ret = 0;
  3118. u32 nexus_depth = 1;
  3119. u8 i_port_id[16];
  3120. if (srpt_parse_i_port_id(i_port_id, name) < 0) {
  3121. printk(KERN_ERR "invalid initiator port ID %s\n", name);
  3122. ret = -EINVAL;
  3123. goto err;
  3124. }
  3125. se_nacl_new = srpt_alloc_fabric_acl(tpg);
  3126. if (!se_nacl_new) {
  3127. ret = -ENOMEM;
  3128. goto err;
  3129. }
  3130. /*
  3131. * nacl_new may be released by core_tpg_add_initiator_node_acl()
  3132. * when converting a node ACL from demo mode to explict
  3133. */
  3134. se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
  3135. nexus_depth);
  3136. if (IS_ERR(se_nacl)) {
  3137. ret = PTR_ERR(se_nacl);
  3138. goto err;
  3139. }
  3140. /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
  3141. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3142. memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
  3143. nacl->sport = sport;
  3144. spin_lock_irq(&sport->port_acl_lock);
  3145. list_add_tail(&nacl->list, &sport->port_acl_list);
  3146. spin_unlock_irq(&sport->port_acl_lock);
  3147. return se_nacl;
  3148. err:
  3149. return ERR_PTR(ret);
  3150. }
  3151. /*
  3152. * configfs callback function invoked for
  3153. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3154. */
  3155. static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
  3156. {
  3157. struct srpt_node_acl *nacl;
  3158. struct srpt_device *sdev;
  3159. struct srpt_port *sport;
  3160. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3161. sport = nacl->sport;
  3162. sdev = sport->sdev;
  3163. spin_lock_irq(&sport->port_acl_lock);
  3164. list_del(&nacl->list);
  3165. spin_unlock_irq(&sport->port_acl_lock);
  3166. core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
  3167. srpt_release_fabric_acl(NULL, se_nacl);
  3168. }
  3169. static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
  3170. struct se_portal_group *se_tpg,
  3171. char *page)
  3172. {
  3173. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3174. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  3175. }
  3176. static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
  3177. struct se_portal_group *se_tpg,
  3178. const char *page,
  3179. size_t count)
  3180. {
  3181. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3182. unsigned long val;
  3183. int ret;
  3184. ret = strict_strtoul(page, 0, &val);
  3185. if (ret < 0) {
  3186. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3187. return -EINVAL;
  3188. }
  3189. if (val > MAX_SRPT_RDMA_SIZE) {
  3190. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  3191. MAX_SRPT_RDMA_SIZE);
  3192. return -EINVAL;
  3193. }
  3194. if (val < DEFAULT_MAX_RDMA_SIZE) {
  3195. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  3196. val, DEFAULT_MAX_RDMA_SIZE);
  3197. return -EINVAL;
  3198. }
  3199. sport->port_attrib.srp_max_rdma_size = val;
  3200. return count;
  3201. }
  3202. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
  3203. static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
  3204. struct se_portal_group *se_tpg,
  3205. char *page)
  3206. {
  3207. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3208. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  3209. }
  3210. static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
  3211. struct se_portal_group *se_tpg,
  3212. const char *page,
  3213. size_t count)
  3214. {
  3215. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3216. unsigned long val;
  3217. int ret;
  3218. ret = strict_strtoul(page, 0, &val);
  3219. if (ret < 0) {
  3220. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3221. return -EINVAL;
  3222. }
  3223. if (val > MAX_SRPT_RSP_SIZE) {
  3224. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  3225. MAX_SRPT_RSP_SIZE);
  3226. return -EINVAL;
  3227. }
  3228. if (val < MIN_MAX_RSP_SIZE) {
  3229. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  3230. MIN_MAX_RSP_SIZE);
  3231. return -EINVAL;
  3232. }
  3233. sport->port_attrib.srp_max_rsp_size = val;
  3234. return count;
  3235. }
  3236. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
  3237. static ssize_t srpt_tpg_attrib_show_srp_sq_size(
  3238. struct se_portal_group *se_tpg,
  3239. char *page)
  3240. {
  3241. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3242. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  3243. }
  3244. static ssize_t srpt_tpg_attrib_store_srp_sq_size(
  3245. struct se_portal_group *se_tpg,
  3246. const char *page,
  3247. size_t count)
  3248. {
  3249. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3250. unsigned long val;
  3251. int ret;
  3252. ret = strict_strtoul(page, 0, &val);
  3253. if (ret < 0) {
  3254. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3255. return -EINVAL;
  3256. }
  3257. if (val > MAX_SRPT_SRQ_SIZE) {
  3258. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  3259. MAX_SRPT_SRQ_SIZE);
  3260. return -EINVAL;
  3261. }
  3262. if (val < MIN_SRPT_SRQ_SIZE) {
  3263. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  3264. MIN_SRPT_SRQ_SIZE);
  3265. return -EINVAL;
  3266. }
  3267. sport->port_attrib.srp_sq_size = val;
  3268. return count;
  3269. }
  3270. TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
  3271. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  3272. &srpt_tpg_attrib_srp_max_rdma_size.attr,
  3273. &srpt_tpg_attrib_srp_max_rsp_size.attr,
  3274. &srpt_tpg_attrib_srp_sq_size.attr,
  3275. NULL,
  3276. };
  3277. static ssize_t srpt_tpg_show_enable(
  3278. struct se_portal_group *se_tpg,
  3279. char *page)
  3280. {
  3281. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3282. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  3283. }
  3284. static ssize_t srpt_tpg_store_enable(
  3285. struct se_portal_group *se_tpg,
  3286. const char *page,
  3287. size_t count)
  3288. {
  3289. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3290. unsigned long tmp;
  3291. int ret;
  3292. ret = strict_strtoul(page, 0, &tmp);
  3293. if (ret < 0) {
  3294. printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
  3295. return -EINVAL;
  3296. }
  3297. if ((tmp != 0) && (tmp != 1)) {
  3298. printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  3299. return -EINVAL;
  3300. }
  3301. if (tmp == 1)
  3302. sport->enabled = true;
  3303. else
  3304. sport->enabled = false;
  3305. return count;
  3306. }
  3307. TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
  3308. static struct configfs_attribute *srpt_tpg_attrs[] = {
  3309. &srpt_tpg_enable.attr,
  3310. NULL,
  3311. };
  3312. /**
  3313. * configfs callback invoked for
  3314. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  3315. */
  3316. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  3317. struct config_group *group,
  3318. const char *name)
  3319. {
  3320. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3321. int res;
  3322. /* Initialize sport->port_wwn and sport->port_tpg_1 */
  3323. res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
  3324. &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
  3325. if (res)
  3326. return ERR_PTR(res);
  3327. return &sport->port_tpg_1;
  3328. }
  3329. /**
  3330. * configfs callback invoked for
  3331. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  3332. */
  3333. static void srpt_drop_tpg(struct se_portal_group *tpg)
  3334. {
  3335. struct srpt_port *sport = container_of(tpg,
  3336. struct srpt_port, port_tpg_1);
  3337. sport->enabled = false;
  3338. core_tpg_deregister(&sport->port_tpg_1);
  3339. }
  3340. /**
  3341. * configfs callback invoked for
  3342. * mkdir /sys/kernel/config/target/$driver/$port
  3343. */
  3344. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  3345. struct config_group *group,
  3346. const char *name)
  3347. {
  3348. struct srpt_port *sport;
  3349. int ret;
  3350. sport = srpt_lookup_port(name);
  3351. pr_debug("make_tport(%s)\n", name);
  3352. ret = -EINVAL;
  3353. if (!sport)
  3354. goto err;
  3355. return &sport->port_wwn;
  3356. err:
  3357. return ERR_PTR(ret);
  3358. }
  3359. /**
  3360. * configfs callback invoked for
  3361. * rmdir /sys/kernel/config/target/$driver/$port
  3362. */
  3363. static void srpt_drop_tport(struct se_wwn *wwn)
  3364. {
  3365. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3366. pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
  3367. }
  3368. static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
  3369. char *buf)
  3370. {
  3371. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  3372. }
  3373. TF_WWN_ATTR_RO(srpt, version);
  3374. static struct configfs_attribute *srpt_wwn_attrs[] = {
  3375. &srpt_wwn_version.attr,
  3376. NULL,
  3377. };
  3378. static struct target_core_fabric_ops srpt_template = {
  3379. .get_fabric_name = srpt_get_fabric_name,
  3380. .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
  3381. .tpg_get_wwn = srpt_get_fabric_wwn,
  3382. .tpg_get_tag = srpt_get_tag,
  3383. .tpg_get_default_depth = srpt_get_default_depth,
  3384. .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
  3385. .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
  3386. .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
  3387. .tpg_check_demo_mode = srpt_check_false,
  3388. .tpg_check_demo_mode_cache = srpt_check_true,
  3389. .tpg_check_demo_mode_write_protect = srpt_check_true,
  3390. .tpg_check_prod_mode_write_protect = srpt_check_false,
  3391. .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
  3392. .tpg_release_fabric_acl = srpt_release_fabric_acl,
  3393. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  3394. .release_cmd = srpt_release_cmd,
  3395. .check_stop_free = srpt_check_stop_free,
  3396. .shutdown_session = srpt_shutdown_session,
  3397. .close_session = srpt_close_session,
  3398. .sess_get_index = srpt_sess_get_index,
  3399. .sess_get_initiator_sid = NULL,
  3400. .write_pending = srpt_write_pending,
  3401. .write_pending_status = srpt_write_pending_status,
  3402. .set_default_node_attributes = srpt_set_default_node_attrs,
  3403. .get_task_tag = srpt_get_task_tag,
  3404. .get_cmd_state = srpt_get_tcm_cmd_state,
  3405. .queue_data_in = srpt_queue_response,
  3406. .queue_status = srpt_queue_status,
  3407. .queue_tm_rsp = srpt_queue_response,
  3408. .get_fabric_sense_len = srpt_get_fabric_sense_len,
  3409. .set_fabric_sense_len = srpt_set_fabric_sense_len,
  3410. /*
  3411. * Setup function pointers for generic logic in
  3412. * target_core_fabric_configfs.c
  3413. */
  3414. .fabric_make_wwn = srpt_make_tport,
  3415. .fabric_drop_wwn = srpt_drop_tport,
  3416. .fabric_make_tpg = srpt_make_tpg,
  3417. .fabric_drop_tpg = srpt_drop_tpg,
  3418. .fabric_post_link = NULL,
  3419. .fabric_pre_unlink = NULL,
  3420. .fabric_make_np = NULL,
  3421. .fabric_drop_np = NULL,
  3422. .fabric_make_nodeacl = srpt_make_nodeacl,
  3423. .fabric_drop_nodeacl = srpt_drop_nodeacl,
  3424. };
  3425. /**
  3426. * srpt_init_module() - Kernel module initialization.
  3427. *
  3428. * Note: Since ib_register_client() registers callback functions, and since at
  3429. * least one of these callback functions (srpt_add_one()) calls target core
  3430. * functions, this driver must be registered with the target core before
  3431. * ib_register_client() is called.
  3432. */
  3433. static int __init srpt_init_module(void)
  3434. {
  3435. int ret;
  3436. ret = -EINVAL;
  3437. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  3438. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3439. " srp_max_req_size -- must be at least %d.\n",
  3440. srp_max_req_size, MIN_MAX_REQ_SIZE);
  3441. goto out;
  3442. }
  3443. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  3444. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  3445. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3446. " srpt_srq_size -- must be in the range [%d..%d].\n",
  3447. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  3448. goto out;
  3449. }
  3450. srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
  3451. if (IS_ERR(srpt_target)) {
  3452. printk(KERN_ERR "couldn't register\n");
  3453. ret = PTR_ERR(srpt_target);
  3454. goto out;
  3455. }
  3456. srpt_target->tf_ops = srpt_template;
  3457. /* Enable SG chaining */
  3458. srpt_target->tf_ops.task_sg_chaining = true;
  3459. /*
  3460. * Set up default attribute lists.
  3461. */
  3462. srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
  3463. srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
  3464. srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
  3465. srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
  3466. srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
  3467. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  3468. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  3469. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  3470. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  3471. ret = target_fabric_configfs_register(srpt_target);
  3472. if (ret < 0) {
  3473. printk(KERN_ERR "couldn't register\n");
  3474. goto out_free_target;
  3475. }
  3476. ret = ib_register_client(&srpt_client);
  3477. if (ret) {
  3478. printk(KERN_ERR "couldn't register IB client\n");
  3479. goto out_unregister_target;
  3480. }
  3481. return 0;
  3482. out_unregister_target:
  3483. target_fabric_configfs_deregister(srpt_target);
  3484. srpt_target = NULL;
  3485. out_free_target:
  3486. if (srpt_target)
  3487. target_fabric_configfs_free(srpt_target);
  3488. out:
  3489. return ret;
  3490. }
  3491. static void __exit srpt_cleanup_module(void)
  3492. {
  3493. ib_unregister_client(&srpt_client);
  3494. target_fabric_configfs_deregister(srpt_target);
  3495. srpt_target = NULL;
  3496. }
  3497. module_init(srpt_init_module);
  3498. module_exit(srpt_cleanup_module);