scsi_transport_iscsi.c 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606
  1. /*
  2. * iSCSI transport class definitions
  3. *
  4. * Copyright (C) IBM Corporation, 2004
  5. * Copyright (C) Mike Christie, 2004 - 2005
  6. * Copyright (C) Dmitry Yusupov, 2004 - 2005
  7. * Copyright (C) Alex Aizman, 2004 - 2005
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/slab.h>
  26. #include <linux/bsg-lib.h>
  27. #include <linux/idr.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_transport.h>
  33. #include <scsi/scsi_transport_iscsi.h>
  34. #include <scsi/iscsi_if.h>
  35. #include <scsi/scsi_cmnd.h>
  36. #include <scsi/scsi_bsg_iscsi.h>
  37. #define ISCSI_TRANSPORT_VERSION "2.0-870"
  38. static int dbg_session;
  39. module_param_named(debug_session, dbg_session, int,
  40. S_IRUGO | S_IWUSR);
  41. MODULE_PARM_DESC(debug_session,
  42. "Turn on debugging for sessions in scsi_transport_iscsi "
  43. "module. Set to 1 to turn on, and zero to turn off. Default "
  44. "is off.");
  45. static int dbg_conn;
  46. module_param_named(debug_conn, dbg_conn, int,
  47. S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(debug_conn,
  49. "Turn on debugging for connections in scsi_transport_iscsi "
  50. "module. Set to 1 to turn on, and zero to turn off. Default "
  51. "is off.");
  52. #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
  53. do { \
  54. if (dbg_session) \
  55. iscsi_cls_session_printk(KERN_INFO, _session, \
  56. "%s: " dbg_fmt, \
  57. __func__, ##arg); \
  58. } while (0);
  59. #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
  60. do { \
  61. if (dbg_conn) \
  62. iscsi_cls_conn_printk(KERN_INFO, _conn, \
  63. "%s: " dbg_fmt, \
  64. __func__, ##arg); \
  65. } while (0);
  66. struct iscsi_internal {
  67. struct scsi_transport_template t;
  68. struct iscsi_transport *iscsi_transport;
  69. struct list_head list;
  70. struct device dev;
  71. struct transport_container conn_cont;
  72. struct transport_container session_cont;
  73. };
  74. static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
  75. static struct workqueue_struct *iscsi_eh_timer_workq;
  76. static DEFINE_IDA(iscsi_sess_ida);
  77. /*
  78. * list of registered transports and lock that must
  79. * be held while accessing list. The iscsi_transport_lock must
  80. * be acquired after the rx_queue_mutex.
  81. */
  82. static LIST_HEAD(iscsi_transports);
  83. static DEFINE_SPINLOCK(iscsi_transport_lock);
  84. #define to_iscsi_internal(tmpl) \
  85. container_of(tmpl, struct iscsi_internal, t)
  86. #define dev_to_iscsi_internal(_dev) \
  87. container_of(_dev, struct iscsi_internal, dev)
  88. static void iscsi_transport_release(struct device *dev)
  89. {
  90. struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
  91. kfree(priv);
  92. }
  93. /*
  94. * iscsi_transport_class represents the iscsi_transports that are
  95. * registered.
  96. */
  97. static struct class iscsi_transport_class = {
  98. .name = "iscsi_transport",
  99. .dev_release = iscsi_transport_release,
  100. };
  101. static ssize_t
  102. show_transport_handle(struct device *dev, struct device_attribute *attr,
  103. char *buf)
  104. {
  105. struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
  106. return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
  107. }
  108. static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
  109. #define show_transport_attr(name, format) \
  110. static ssize_t \
  111. show_transport_##name(struct device *dev, \
  112. struct device_attribute *attr,char *buf) \
  113. { \
  114. struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
  115. return sprintf(buf, format"\n", priv->iscsi_transport->name); \
  116. } \
  117. static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
  118. show_transport_attr(caps, "0x%x");
  119. static struct attribute *iscsi_transport_attrs[] = {
  120. &dev_attr_handle.attr,
  121. &dev_attr_caps.attr,
  122. NULL,
  123. };
  124. static struct attribute_group iscsi_transport_group = {
  125. .attrs = iscsi_transport_attrs,
  126. };
  127. /*
  128. * iSCSI endpoint attrs
  129. */
  130. #define iscsi_dev_to_endpoint(_dev) \
  131. container_of(_dev, struct iscsi_endpoint, dev)
  132. #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
  133. struct device_attribute dev_attr_##_prefix##_##_name = \
  134. __ATTR(_name,_mode,_show,_store)
  135. static void iscsi_endpoint_release(struct device *dev)
  136. {
  137. struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
  138. kfree(ep);
  139. }
  140. static struct class iscsi_endpoint_class = {
  141. .name = "iscsi_endpoint",
  142. .dev_release = iscsi_endpoint_release,
  143. };
  144. static ssize_t
  145. show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
  146. {
  147. struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
  148. return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
  149. }
  150. static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
  151. static struct attribute *iscsi_endpoint_attrs[] = {
  152. &dev_attr_ep_handle.attr,
  153. NULL,
  154. };
  155. static struct attribute_group iscsi_endpoint_group = {
  156. .attrs = iscsi_endpoint_attrs,
  157. };
  158. #define ISCSI_MAX_EPID -1
  159. static int iscsi_match_epid(struct device *dev, const void *data)
  160. {
  161. struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
  162. const uint64_t *epid = data;
  163. return *epid == ep->id;
  164. }
  165. struct iscsi_endpoint *
  166. iscsi_create_endpoint(int dd_size)
  167. {
  168. struct device *dev;
  169. struct iscsi_endpoint *ep;
  170. uint64_t id;
  171. int err;
  172. for (id = 1; id < ISCSI_MAX_EPID; id++) {
  173. dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
  174. iscsi_match_epid);
  175. if (!dev)
  176. break;
  177. else
  178. put_device(dev);
  179. }
  180. if (id == ISCSI_MAX_EPID) {
  181. printk(KERN_ERR "Too many connections. Max supported %u\n",
  182. ISCSI_MAX_EPID - 1);
  183. return NULL;
  184. }
  185. ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
  186. if (!ep)
  187. return NULL;
  188. ep->id = id;
  189. ep->dev.class = &iscsi_endpoint_class;
  190. dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
  191. err = device_register(&ep->dev);
  192. if (err)
  193. goto free_ep;
  194. err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
  195. if (err)
  196. goto unregister_dev;
  197. if (dd_size)
  198. ep->dd_data = &ep[1];
  199. return ep;
  200. unregister_dev:
  201. device_unregister(&ep->dev);
  202. return NULL;
  203. free_ep:
  204. kfree(ep);
  205. return NULL;
  206. }
  207. EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
  208. void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
  209. {
  210. sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
  211. device_unregister(&ep->dev);
  212. }
  213. EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
  214. struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
  215. {
  216. struct iscsi_endpoint *ep;
  217. struct device *dev;
  218. dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
  219. iscsi_match_epid);
  220. if (!dev)
  221. return NULL;
  222. ep = iscsi_dev_to_endpoint(dev);
  223. /*
  224. * we can drop this now because the interface will prevent
  225. * removals and lookups from racing.
  226. */
  227. put_device(dev);
  228. return ep;
  229. }
  230. EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
  231. /*
  232. * Interface to display network param to sysfs
  233. */
  234. static void iscsi_iface_release(struct device *dev)
  235. {
  236. struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
  237. struct device *parent = iface->dev.parent;
  238. kfree(iface);
  239. put_device(parent);
  240. }
  241. static struct class iscsi_iface_class = {
  242. .name = "iscsi_iface",
  243. .dev_release = iscsi_iface_release,
  244. };
  245. #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
  246. struct device_attribute dev_attr_##_prefix##_##_name = \
  247. __ATTR(_name, _mode, _show, _store)
  248. /* iface attrs show */
  249. #define iscsi_iface_attr_show(type, name, param_type, param) \
  250. static ssize_t \
  251. show_##type##_##name(struct device *dev, struct device_attribute *attr, \
  252. char *buf) \
  253. { \
  254. struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
  255. struct iscsi_transport *t = iface->transport; \
  256. return t->get_iface_param(iface, param_type, param, buf); \
  257. } \
  258. #define iscsi_iface_net_attr(type, name, param) \
  259. iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
  260. static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
  261. #define iscsi_iface_attr(type, name, param) \
  262. iscsi_iface_attr_show(type, name, ISCSI_IFACE_PARAM, param) \
  263. static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
  264. /* generic read only ipv4 attribute */
  265. iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
  266. iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
  267. iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
  268. iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
  269. iscsi_iface_net_attr(ipv4_iface, dhcp_dns_address_en,
  270. ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN);
  271. iscsi_iface_net_attr(ipv4_iface, dhcp_slp_da_info_en,
  272. ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN);
  273. iscsi_iface_net_attr(ipv4_iface, tos_en, ISCSI_NET_PARAM_IPV4_TOS_EN);
  274. iscsi_iface_net_attr(ipv4_iface, tos, ISCSI_NET_PARAM_IPV4_TOS);
  275. iscsi_iface_net_attr(ipv4_iface, grat_arp_en,
  276. ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN);
  277. iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id_en,
  278. ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN);
  279. iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id,
  280. ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID);
  281. iscsi_iface_net_attr(ipv4_iface, dhcp_req_vendor_id_en,
  282. ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN);
  283. iscsi_iface_net_attr(ipv4_iface, dhcp_use_vendor_id_en,
  284. ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN);
  285. iscsi_iface_net_attr(ipv4_iface, dhcp_vendor_id,
  286. ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID);
  287. iscsi_iface_net_attr(ipv4_iface, dhcp_learn_iqn_en,
  288. ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN);
  289. iscsi_iface_net_attr(ipv4_iface, fragment_disable,
  290. ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE);
  291. iscsi_iface_net_attr(ipv4_iface, incoming_forwarding_en,
  292. ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN);
  293. iscsi_iface_net_attr(ipv4_iface, ttl, ISCSI_NET_PARAM_IPV4_TTL);
  294. /* generic read only ipv6 attribute */
  295. iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
  296. iscsi_iface_net_attr(ipv6_iface, link_local_addr,
  297. ISCSI_NET_PARAM_IPV6_LINKLOCAL);
  298. iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
  299. iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
  300. ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
  301. iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
  302. ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
  303. iscsi_iface_net_attr(ipv6_iface, link_local_state,
  304. ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE);
  305. iscsi_iface_net_attr(ipv6_iface, router_state,
  306. ISCSI_NET_PARAM_IPV6_ROUTER_STATE);
  307. iscsi_iface_net_attr(ipv6_iface, grat_neighbor_adv_en,
  308. ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN);
  309. iscsi_iface_net_attr(ipv6_iface, mld_en, ISCSI_NET_PARAM_IPV6_MLD_EN);
  310. iscsi_iface_net_attr(ipv6_iface, flow_label, ISCSI_NET_PARAM_IPV6_FLOW_LABEL);
  311. iscsi_iface_net_attr(ipv6_iface, traffic_class,
  312. ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS);
  313. iscsi_iface_net_attr(ipv6_iface, hop_limit, ISCSI_NET_PARAM_IPV6_HOP_LIMIT);
  314. iscsi_iface_net_attr(ipv6_iface, nd_reachable_tmo,
  315. ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO);
  316. iscsi_iface_net_attr(ipv6_iface, nd_rexmit_time,
  317. ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME);
  318. iscsi_iface_net_attr(ipv6_iface, nd_stale_tmo,
  319. ISCSI_NET_PARAM_IPV6_ND_STALE_TMO);
  320. iscsi_iface_net_attr(ipv6_iface, dup_addr_detect_cnt,
  321. ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT);
  322. iscsi_iface_net_attr(ipv6_iface, router_adv_link_mtu,
  323. ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU);
  324. /* common read only iface attribute */
  325. iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
  326. iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
  327. iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
  328. iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
  329. iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
  330. iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
  331. iscsi_iface_net_attr(iface, ipaddress_state, ISCSI_NET_PARAM_IPADDR_STATE);
  332. iscsi_iface_net_attr(iface, delayed_ack_en, ISCSI_NET_PARAM_DELAYED_ACK_EN);
  333. iscsi_iface_net_attr(iface, tcp_nagle_disable,
  334. ISCSI_NET_PARAM_TCP_NAGLE_DISABLE);
  335. iscsi_iface_net_attr(iface, tcp_wsf_disable, ISCSI_NET_PARAM_TCP_WSF_DISABLE);
  336. iscsi_iface_net_attr(iface, tcp_wsf, ISCSI_NET_PARAM_TCP_WSF);
  337. iscsi_iface_net_attr(iface, tcp_timer_scale, ISCSI_NET_PARAM_TCP_TIMER_SCALE);
  338. iscsi_iface_net_attr(iface, tcp_timestamp_en, ISCSI_NET_PARAM_TCP_TIMESTAMP_EN);
  339. iscsi_iface_net_attr(iface, cache_id, ISCSI_NET_PARAM_CACHE_ID);
  340. iscsi_iface_net_attr(iface, redirect_en, ISCSI_NET_PARAM_REDIRECT_EN);
  341. /* common iscsi specific settings attributes */
  342. iscsi_iface_attr(iface, def_taskmgmt_tmo, ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO);
  343. iscsi_iface_attr(iface, header_digest, ISCSI_IFACE_PARAM_HDRDGST_EN);
  344. iscsi_iface_attr(iface, data_digest, ISCSI_IFACE_PARAM_DATADGST_EN);
  345. iscsi_iface_attr(iface, immediate_data, ISCSI_IFACE_PARAM_IMM_DATA_EN);
  346. iscsi_iface_attr(iface, initial_r2t, ISCSI_IFACE_PARAM_INITIAL_R2T_EN);
  347. iscsi_iface_attr(iface, data_seq_in_order,
  348. ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN);
  349. iscsi_iface_attr(iface, data_pdu_in_order, ISCSI_IFACE_PARAM_PDU_INORDER_EN);
  350. iscsi_iface_attr(iface, erl, ISCSI_IFACE_PARAM_ERL);
  351. iscsi_iface_attr(iface, max_recv_dlength, ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH);
  352. iscsi_iface_attr(iface, first_burst_len, ISCSI_IFACE_PARAM_FIRST_BURST);
  353. iscsi_iface_attr(iface, max_outstanding_r2t, ISCSI_IFACE_PARAM_MAX_R2T);
  354. iscsi_iface_attr(iface, max_burst_len, ISCSI_IFACE_PARAM_MAX_BURST);
  355. iscsi_iface_attr(iface, chap_auth, ISCSI_IFACE_PARAM_CHAP_AUTH_EN);
  356. iscsi_iface_attr(iface, bidi_chap, ISCSI_IFACE_PARAM_BIDI_CHAP_EN);
  357. iscsi_iface_attr(iface, discovery_auth_optional,
  358. ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL);
  359. iscsi_iface_attr(iface, discovery_logout,
  360. ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN);
  361. iscsi_iface_attr(iface, strict_login_comp_en,
  362. ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN);
  363. iscsi_iface_attr(iface, initiator_name, ISCSI_IFACE_PARAM_INITIATOR_NAME);
  364. static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
  365. struct attribute *attr, int i)
  366. {
  367. struct device *dev = container_of(kobj, struct device, kobj);
  368. struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
  369. struct iscsi_transport *t = iface->transport;
  370. int param;
  371. int param_type;
  372. if (attr == &dev_attr_iface_enabled.attr)
  373. param = ISCSI_NET_PARAM_IFACE_ENABLE;
  374. else if (attr == &dev_attr_iface_vlan_id.attr)
  375. param = ISCSI_NET_PARAM_VLAN_ID;
  376. else if (attr == &dev_attr_iface_vlan_priority.attr)
  377. param = ISCSI_NET_PARAM_VLAN_PRIORITY;
  378. else if (attr == &dev_attr_iface_vlan_enabled.attr)
  379. param = ISCSI_NET_PARAM_VLAN_ENABLED;
  380. else if (attr == &dev_attr_iface_mtu.attr)
  381. param = ISCSI_NET_PARAM_MTU;
  382. else if (attr == &dev_attr_iface_port.attr)
  383. param = ISCSI_NET_PARAM_PORT;
  384. else if (attr == &dev_attr_iface_ipaddress_state.attr)
  385. param = ISCSI_NET_PARAM_IPADDR_STATE;
  386. else if (attr == &dev_attr_iface_delayed_ack_en.attr)
  387. param = ISCSI_NET_PARAM_DELAYED_ACK_EN;
  388. else if (attr == &dev_attr_iface_tcp_nagle_disable.attr)
  389. param = ISCSI_NET_PARAM_TCP_NAGLE_DISABLE;
  390. else if (attr == &dev_attr_iface_tcp_wsf_disable.attr)
  391. param = ISCSI_NET_PARAM_TCP_WSF_DISABLE;
  392. else if (attr == &dev_attr_iface_tcp_wsf.attr)
  393. param = ISCSI_NET_PARAM_TCP_WSF;
  394. else if (attr == &dev_attr_iface_tcp_timer_scale.attr)
  395. param = ISCSI_NET_PARAM_TCP_TIMER_SCALE;
  396. else if (attr == &dev_attr_iface_tcp_timestamp_en.attr)
  397. param = ISCSI_NET_PARAM_TCP_TIMESTAMP_EN;
  398. else if (attr == &dev_attr_iface_cache_id.attr)
  399. param = ISCSI_NET_PARAM_CACHE_ID;
  400. else if (attr == &dev_attr_iface_redirect_en.attr)
  401. param = ISCSI_NET_PARAM_REDIRECT_EN;
  402. else if (attr == &dev_attr_iface_def_taskmgmt_tmo.attr)
  403. param = ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO;
  404. else if (attr == &dev_attr_iface_header_digest.attr)
  405. param = ISCSI_IFACE_PARAM_HDRDGST_EN;
  406. else if (attr == &dev_attr_iface_data_digest.attr)
  407. param = ISCSI_IFACE_PARAM_DATADGST_EN;
  408. else if (attr == &dev_attr_iface_immediate_data.attr)
  409. param = ISCSI_IFACE_PARAM_IMM_DATA_EN;
  410. else if (attr == &dev_attr_iface_initial_r2t.attr)
  411. param = ISCSI_IFACE_PARAM_INITIAL_R2T_EN;
  412. else if (attr == &dev_attr_iface_data_seq_in_order.attr)
  413. param = ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN;
  414. else if (attr == &dev_attr_iface_data_pdu_in_order.attr)
  415. param = ISCSI_IFACE_PARAM_PDU_INORDER_EN;
  416. else if (attr == &dev_attr_iface_erl.attr)
  417. param = ISCSI_IFACE_PARAM_ERL;
  418. else if (attr == &dev_attr_iface_max_recv_dlength.attr)
  419. param = ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH;
  420. else if (attr == &dev_attr_iface_first_burst_len.attr)
  421. param = ISCSI_IFACE_PARAM_FIRST_BURST;
  422. else if (attr == &dev_attr_iface_max_outstanding_r2t.attr)
  423. param = ISCSI_IFACE_PARAM_MAX_R2T;
  424. else if (attr == &dev_attr_iface_max_burst_len.attr)
  425. param = ISCSI_IFACE_PARAM_MAX_BURST;
  426. else if (attr == &dev_attr_iface_chap_auth.attr)
  427. param = ISCSI_IFACE_PARAM_CHAP_AUTH_EN;
  428. else if (attr == &dev_attr_iface_bidi_chap.attr)
  429. param = ISCSI_IFACE_PARAM_BIDI_CHAP_EN;
  430. else if (attr == &dev_attr_iface_discovery_auth_optional.attr)
  431. param = ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL;
  432. else if (attr == &dev_attr_iface_discovery_logout.attr)
  433. param = ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN;
  434. else if (attr == &dev_attr_iface_strict_login_comp_en.attr)
  435. param = ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN;
  436. else if (attr == &dev_attr_iface_initiator_name.attr)
  437. param = ISCSI_IFACE_PARAM_INITIATOR_NAME;
  438. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
  439. if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
  440. param = ISCSI_NET_PARAM_IPV4_ADDR;
  441. else if (attr == &dev_attr_ipv4_iface_gateway.attr)
  442. param = ISCSI_NET_PARAM_IPV4_GW;
  443. else if (attr == &dev_attr_ipv4_iface_subnet.attr)
  444. param = ISCSI_NET_PARAM_IPV4_SUBNET;
  445. else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
  446. param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
  447. else if (attr ==
  448. &dev_attr_ipv4_iface_dhcp_dns_address_en.attr)
  449. param = ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN;
  450. else if (attr ==
  451. &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr)
  452. param = ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN;
  453. else if (attr == &dev_attr_ipv4_iface_tos_en.attr)
  454. param = ISCSI_NET_PARAM_IPV4_TOS_EN;
  455. else if (attr == &dev_attr_ipv4_iface_tos.attr)
  456. param = ISCSI_NET_PARAM_IPV4_TOS;
  457. else if (attr == &dev_attr_ipv4_iface_grat_arp_en.attr)
  458. param = ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN;
  459. else if (attr ==
  460. &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr)
  461. param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN;
  462. else if (attr == &dev_attr_ipv4_iface_dhcp_alt_client_id.attr)
  463. param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID;
  464. else if (attr ==
  465. &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr)
  466. param = ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN;
  467. else if (attr ==
  468. &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr)
  469. param = ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN;
  470. else if (attr == &dev_attr_ipv4_iface_dhcp_vendor_id.attr)
  471. param = ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID;
  472. else if (attr ==
  473. &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr)
  474. param = ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN;
  475. else if (attr ==
  476. &dev_attr_ipv4_iface_fragment_disable.attr)
  477. param = ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE;
  478. else if (attr ==
  479. &dev_attr_ipv4_iface_incoming_forwarding_en.attr)
  480. param = ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN;
  481. else if (attr == &dev_attr_ipv4_iface_ttl.attr)
  482. param = ISCSI_NET_PARAM_IPV4_TTL;
  483. else
  484. return 0;
  485. } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
  486. if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
  487. param = ISCSI_NET_PARAM_IPV6_ADDR;
  488. else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
  489. param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
  490. else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
  491. param = ISCSI_NET_PARAM_IPV6_ROUTER;
  492. else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
  493. param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
  494. else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
  495. param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
  496. else if (attr == &dev_attr_ipv6_iface_link_local_state.attr)
  497. param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE;
  498. else if (attr == &dev_attr_ipv6_iface_router_state.attr)
  499. param = ISCSI_NET_PARAM_IPV6_ROUTER_STATE;
  500. else if (attr ==
  501. &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr)
  502. param = ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN;
  503. else if (attr == &dev_attr_ipv6_iface_mld_en.attr)
  504. param = ISCSI_NET_PARAM_IPV6_MLD_EN;
  505. else if (attr == &dev_attr_ipv6_iface_flow_label.attr)
  506. param = ISCSI_NET_PARAM_IPV6_FLOW_LABEL;
  507. else if (attr == &dev_attr_ipv6_iface_traffic_class.attr)
  508. param = ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS;
  509. else if (attr == &dev_attr_ipv6_iface_hop_limit.attr)
  510. param = ISCSI_NET_PARAM_IPV6_HOP_LIMIT;
  511. else if (attr == &dev_attr_ipv6_iface_nd_reachable_tmo.attr)
  512. param = ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO;
  513. else if (attr == &dev_attr_ipv6_iface_nd_rexmit_time.attr)
  514. param = ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME;
  515. else if (attr == &dev_attr_ipv6_iface_nd_stale_tmo.attr)
  516. param = ISCSI_NET_PARAM_IPV6_ND_STALE_TMO;
  517. else if (attr == &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr)
  518. param = ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT;
  519. else if (attr == &dev_attr_ipv6_iface_router_adv_link_mtu.attr)
  520. param = ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU;
  521. else
  522. return 0;
  523. } else {
  524. WARN_ONCE(1, "Invalid iface attr");
  525. return 0;
  526. }
  527. switch (param) {
  528. case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
  529. case ISCSI_IFACE_PARAM_HDRDGST_EN:
  530. case ISCSI_IFACE_PARAM_DATADGST_EN:
  531. case ISCSI_IFACE_PARAM_IMM_DATA_EN:
  532. case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
  533. case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
  534. case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
  535. case ISCSI_IFACE_PARAM_ERL:
  536. case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
  537. case ISCSI_IFACE_PARAM_FIRST_BURST:
  538. case ISCSI_IFACE_PARAM_MAX_R2T:
  539. case ISCSI_IFACE_PARAM_MAX_BURST:
  540. case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
  541. case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
  542. case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
  543. case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
  544. case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
  545. case ISCSI_IFACE_PARAM_INITIATOR_NAME:
  546. param_type = ISCSI_IFACE_PARAM;
  547. break;
  548. default:
  549. param_type = ISCSI_NET_PARAM;
  550. }
  551. return t->attr_is_visible(param_type, param);
  552. }
  553. static struct attribute *iscsi_iface_attrs[] = {
  554. &dev_attr_iface_enabled.attr,
  555. &dev_attr_iface_vlan_id.attr,
  556. &dev_attr_iface_vlan_priority.attr,
  557. &dev_attr_iface_vlan_enabled.attr,
  558. &dev_attr_ipv4_iface_ipaddress.attr,
  559. &dev_attr_ipv4_iface_gateway.attr,
  560. &dev_attr_ipv4_iface_subnet.attr,
  561. &dev_attr_ipv4_iface_bootproto.attr,
  562. &dev_attr_ipv6_iface_ipaddress.attr,
  563. &dev_attr_ipv6_iface_link_local_addr.attr,
  564. &dev_attr_ipv6_iface_router_addr.attr,
  565. &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
  566. &dev_attr_ipv6_iface_link_local_autocfg.attr,
  567. &dev_attr_iface_mtu.attr,
  568. &dev_attr_iface_port.attr,
  569. &dev_attr_iface_ipaddress_state.attr,
  570. &dev_attr_iface_delayed_ack_en.attr,
  571. &dev_attr_iface_tcp_nagle_disable.attr,
  572. &dev_attr_iface_tcp_wsf_disable.attr,
  573. &dev_attr_iface_tcp_wsf.attr,
  574. &dev_attr_iface_tcp_timer_scale.attr,
  575. &dev_attr_iface_tcp_timestamp_en.attr,
  576. &dev_attr_iface_cache_id.attr,
  577. &dev_attr_iface_redirect_en.attr,
  578. &dev_attr_iface_def_taskmgmt_tmo.attr,
  579. &dev_attr_iface_header_digest.attr,
  580. &dev_attr_iface_data_digest.attr,
  581. &dev_attr_iface_immediate_data.attr,
  582. &dev_attr_iface_initial_r2t.attr,
  583. &dev_attr_iface_data_seq_in_order.attr,
  584. &dev_attr_iface_data_pdu_in_order.attr,
  585. &dev_attr_iface_erl.attr,
  586. &dev_attr_iface_max_recv_dlength.attr,
  587. &dev_attr_iface_first_burst_len.attr,
  588. &dev_attr_iface_max_outstanding_r2t.attr,
  589. &dev_attr_iface_max_burst_len.attr,
  590. &dev_attr_iface_chap_auth.attr,
  591. &dev_attr_iface_bidi_chap.attr,
  592. &dev_attr_iface_discovery_auth_optional.attr,
  593. &dev_attr_iface_discovery_logout.attr,
  594. &dev_attr_iface_strict_login_comp_en.attr,
  595. &dev_attr_iface_initiator_name.attr,
  596. &dev_attr_ipv4_iface_dhcp_dns_address_en.attr,
  597. &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr,
  598. &dev_attr_ipv4_iface_tos_en.attr,
  599. &dev_attr_ipv4_iface_tos.attr,
  600. &dev_attr_ipv4_iface_grat_arp_en.attr,
  601. &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr,
  602. &dev_attr_ipv4_iface_dhcp_alt_client_id.attr,
  603. &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr,
  604. &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr,
  605. &dev_attr_ipv4_iface_dhcp_vendor_id.attr,
  606. &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr,
  607. &dev_attr_ipv4_iface_fragment_disable.attr,
  608. &dev_attr_ipv4_iface_incoming_forwarding_en.attr,
  609. &dev_attr_ipv4_iface_ttl.attr,
  610. &dev_attr_ipv6_iface_link_local_state.attr,
  611. &dev_attr_ipv6_iface_router_state.attr,
  612. &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr,
  613. &dev_attr_ipv6_iface_mld_en.attr,
  614. &dev_attr_ipv6_iface_flow_label.attr,
  615. &dev_attr_ipv6_iface_traffic_class.attr,
  616. &dev_attr_ipv6_iface_hop_limit.attr,
  617. &dev_attr_ipv6_iface_nd_reachable_tmo.attr,
  618. &dev_attr_ipv6_iface_nd_rexmit_time.attr,
  619. &dev_attr_ipv6_iface_nd_stale_tmo.attr,
  620. &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr,
  621. &dev_attr_ipv6_iface_router_adv_link_mtu.attr,
  622. NULL,
  623. };
  624. static struct attribute_group iscsi_iface_group = {
  625. .attrs = iscsi_iface_attrs,
  626. .is_visible = iscsi_iface_attr_is_visible,
  627. };
  628. /* convert iscsi_ipaddress_state values to ascii string name */
  629. static const struct {
  630. enum iscsi_ipaddress_state value;
  631. char *name;
  632. } iscsi_ipaddress_state_names[] = {
  633. {ISCSI_IPDDRESS_STATE_UNCONFIGURED, "Unconfigured" },
  634. {ISCSI_IPDDRESS_STATE_ACQUIRING, "Acquiring" },
  635. {ISCSI_IPDDRESS_STATE_TENTATIVE, "Tentative" },
  636. {ISCSI_IPDDRESS_STATE_VALID, "Valid" },
  637. {ISCSI_IPDDRESS_STATE_DISABLING, "Disabling" },
  638. {ISCSI_IPDDRESS_STATE_INVALID, "Invalid" },
  639. {ISCSI_IPDDRESS_STATE_DEPRECATED, "Deprecated" },
  640. };
  641. char *iscsi_get_ipaddress_state_name(enum iscsi_ipaddress_state port_state)
  642. {
  643. int i;
  644. char *state = NULL;
  645. for (i = 0; i < ARRAY_SIZE(iscsi_ipaddress_state_names); i++) {
  646. if (iscsi_ipaddress_state_names[i].value == port_state) {
  647. state = iscsi_ipaddress_state_names[i].name;
  648. break;
  649. }
  650. }
  651. return state;
  652. }
  653. EXPORT_SYMBOL_GPL(iscsi_get_ipaddress_state_name);
  654. /* convert iscsi_router_state values to ascii string name */
  655. static const struct {
  656. enum iscsi_router_state value;
  657. char *name;
  658. } iscsi_router_state_names[] = {
  659. {ISCSI_ROUTER_STATE_UNKNOWN, "Unknown" },
  660. {ISCSI_ROUTER_STATE_ADVERTISED, "Advertised" },
  661. {ISCSI_ROUTER_STATE_MANUAL, "Manual" },
  662. {ISCSI_ROUTER_STATE_STALE, "Stale" },
  663. };
  664. char *iscsi_get_router_state_name(enum iscsi_router_state router_state)
  665. {
  666. int i;
  667. char *state = NULL;
  668. for (i = 0; i < ARRAY_SIZE(iscsi_router_state_names); i++) {
  669. if (iscsi_router_state_names[i].value == router_state) {
  670. state = iscsi_router_state_names[i].name;
  671. break;
  672. }
  673. }
  674. return state;
  675. }
  676. EXPORT_SYMBOL_GPL(iscsi_get_router_state_name);
  677. struct iscsi_iface *
  678. iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
  679. uint32_t iface_type, uint32_t iface_num, int dd_size)
  680. {
  681. struct iscsi_iface *iface;
  682. int err;
  683. iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
  684. if (!iface)
  685. return NULL;
  686. iface->transport = transport;
  687. iface->iface_type = iface_type;
  688. iface->iface_num = iface_num;
  689. iface->dev.release = iscsi_iface_release;
  690. iface->dev.class = &iscsi_iface_class;
  691. /* parent reference released in iscsi_iface_release */
  692. iface->dev.parent = get_device(&shost->shost_gendev);
  693. if (iface_type == ISCSI_IFACE_TYPE_IPV4)
  694. dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
  695. iface_num);
  696. else
  697. dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
  698. iface_num);
  699. err = device_register(&iface->dev);
  700. if (err)
  701. goto free_iface;
  702. err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
  703. if (err)
  704. goto unreg_iface;
  705. if (dd_size)
  706. iface->dd_data = &iface[1];
  707. return iface;
  708. unreg_iface:
  709. device_unregister(&iface->dev);
  710. return NULL;
  711. free_iface:
  712. put_device(iface->dev.parent);
  713. kfree(iface);
  714. return NULL;
  715. }
  716. EXPORT_SYMBOL_GPL(iscsi_create_iface);
  717. void iscsi_destroy_iface(struct iscsi_iface *iface)
  718. {
  719. sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
  720. device_unregister(&iface->dev);
  721. }
  722. EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
  723. /*
  724. * Interface to display flash node params to sysfs
  725. */
  726. #define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store) \
  727. struct device_attribute dev_attr_##_prefix##_##_name = \
  728. __ATTR(_name, _mode, _show, _store)
  729. /* flash node session attrs show */
  730. #define iscsi_flashnode_sess_attr_show(type, name, param) \
  731. static ssize_t \
  732. show_##type##_##name(struct device *dev, struct device_attribute *attr, \
  733. char *buf) \
  734. { \
  735. struct iscsi_bus_flash_session *fnode_sess = \
  736. iscsi_dev_to_flash_session(dev);\
  737. struct iscsi_transport *t = fnode_sess->transport; \
  738. return t->get_flashnode_param(fnode_sess, param, buf); \
  739. } \
  740. #define iscsi_flashnode_sess_attr(type, name, param) \
  741. iscsi_flashnode_sess_attr_show(type, name, param) \
  742. static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
  743. show_##type##_##name, NULL);
  744. /* Flash node session attributes */
  745. iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
  746. ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
  747. iscsi_flashnode_sess_attr(fnode, discovery_session,
  748. ISCSI_FLASHNODE_DISCOVERY_SESS);
  749. iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
  750. iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
  751. iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
  752. iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
  753. iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
  754. ISCSI_FLASHNODE_DATASEQ_INORDER);
  755. iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
  756. ISCSI_FLASHNODE_PDU_INORDER);
  757. iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
  758. iscsi_flashnode_sess_attr(fnode, discovery_logout,
  759. ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
  760. iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
  761. iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
  762. ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
  763. iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
  764. iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
  765. iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
  766. iscsi_flashnode_sess_attr(fnode, def_time2retain,
  767. ISCSI_FLASHNODE_DEF_TIME2RETAIN);
  768. iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
  769. iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
  770. iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
  771. iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
  772. iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
  773. ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
  774. iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
  775. iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
  776. iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
  777. iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
  778. ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
  779. iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
  780. ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
  781. iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
  782. iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
  783. iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
  784. iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
  785. iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
  786. iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
  787. iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
  788. static struct attribute *iscsi_flashnode_sess_attrs[] = {
  789. &dev_attr_fnode_auto_snd_tgt_disable.attr,
  790. &dev_attr_fnode_discovery_session.attr,
  791. &dev_attr_fnode_portal_type.attr,
  792. &dev_attr_fnode_entry_enable.attr,
  793. &dev_attr_fnode_immediate_data.attr,
  794. &dev_attr_fnode_initial_r2t.attr,
  795. &dev_attr_fnode_data_seq_in_order.attr,
  796. &dev_attr_fnode_data_pdu_in_order.attr,
  797. &dev_attr_fnode_chap_auth.attr,
  798. &dev_attr_fnode_discovery_logout.attr,
  799. &dev_attr_fnode_bidi_chap.attr,
  800. &dev_attr_fnode_discovery_auth_optional.attr,
  801. &dev_attr_fnode_erl.attr,
  802. &dev_attr_fnode_first_burst_len.attr,
  803. &dev_attr_fnode_def_time2wait.attr,
  804. &dev_attr_fnode_def_time2retain.attr,
  805. &dev_attr_fnode_max_outstanding_r2t.attr,
  806. &dev_attr_fnode_isid.attr,
  807. &dev_attr_fnode_tsid.attr,
  808. &dev_attr_fnode_max_burst_len.attr,
  809. &dev_attr_fnode_def_taskmgmt_tmo.attr,
  810. &dev_attr_fnode_targetalias.attr,
  811. &dev_attr_fnode_targetname.attr,
  812. &dev_attr_fnode_tpgt.attr,
  813. &dev_attr_fnode_discovery_parent_idx.attr,
  814. &dev_attr_fnode_discovery_parent_type.attr,
  815. &dev_attr_fnode_chap_in_idx.attr,
  816. &dev_attr_fnode_chap_out_idx.attr,
  817. &dev_attr_fnode_username.attr,
  818. &dev_attr_fnode_username_in.attr,
  819. &dev_attr_fnode_password.attr,
  820. &dev_attr_fnode_password_in.attr,
  821. &dev_attr_fnode_is_boot_target.attr,
  822. NULL,
  823. };
  824. static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
  825. struct attribute *attr,
  826. int i)
  827. {
  828. struct device *dev = container_of(kobj, struct device, kobj);
  829. struct iscsi_bus_flash_session *fnode_sess =
  830. iscsi_dev_to_flash_session(dev);
  831. struct iscsi_transport *t = fnode_sess->transport;
  832. int param;
  833. if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
  834. param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
  835. } else if (attr == &dev_attr_fnode_discovery_session.attr) {
  836. param = ISCSI_FLASHNODE_DISCOVERY_SESS;
  837. } else if (attr == &dev_attr_fnode_portal_type.attr) {
  838. param = ISCSI_FLASHNODE_PORTAL_TYPE;
  839. } else if (attr == &dev_attr_fnode_entry_enable.attr) {
  840. param = ISCSI_FLASHNODE_ENTRY_EN;
  841. } else if (attr == &dev_attr_fnode_immediate_data.attr) {
  842. param = ISCSI_FLASHNODE_IMM_DATA_EN;
  843. } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
  844. param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
  845. } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
  846. param = ISCSI_FLASHNODE_DATASEQ_INORDER;
  847. } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
  848. param = ISCSI_FLASHNODE_PDU_INORDER;
  849. } else if (attr == &dev_attr_fnode_chap_auth.attr) {
  850. param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
  851. } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
  852. param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
  853. } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
  854. param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
  855. } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
  856. param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
  857. } else if (attr == &dev_attr_fnode_erl.attr) {
  858. param = ISCSI_FLASHNODE_ERL;
  859. } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
  860. param = ISCSI_FLASHNODE_FIRST_BURST;
  861. } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
  862. param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
  863. } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
  864. param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
  865. } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
  866. param = ISCSI_FLASHNODE_MAX_R2T;
  867. } else if (attr == &dev_attr_fnode_isid.attr) {
  868. param = ISCSI_FLASHNODE_ISID;
  869. } else if (attr == &dev_attr_fnode_tsid.attr) {
  870. param = ISCSI_FLASHNODE_TSID;
  871. } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
  872. param = ISCSI_FLASHNODE_MAX_BURST;
  873. } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
  874. param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
  875. } else if (attr == &dev_attr_fnode_targetalias.attr) {
  876. param = ISCSI_FLASHNODE_ALIAS;
  877. } else if (attr == &dev_attr_fnode_targetname.attr) {
  878. param = ISCSI_FLASHNODE_NAME;
  879. } else if (attr == &dev_attr_fnode_tpgt.attr) {
  880. param = ISCSI_FLASHNODE_TPGT;
  881. } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
  882. param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
  883. } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
  884. param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
  885. } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
  886. param = ISCSI_FLASHNODE_CHAP_IN_IDX;
  887. } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
  888. param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
  889. } else if (attr == &dev_attr_fnode_username.attr) {
  890. param = ISCSI_FLASHNODE_USERNAME;
  891. } else if (attr == &dev_attr_fnode_username_in.attr) {
  892. param = ISCSI_FLASHNODE_USERNAME_IN;
  893. } else if (attr == &dev_attr_fnode_password.attr) {
  894. param = ISCSI_FLASHNODE_PASSWORD;
  895. } else if (attr == &dev_attr_fnode_password_in.attr) {
  896. param = ISCSI_FLASHNODE_PASSWORD_IN;
  897. } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
  898. param = ISCSI_FLASHNODE_IS_BOOT_TGT;
  899. } else {
  900. WARN_ONCE(1, "Invalid flashnode session attr");
  901. return 0;
  902. }
  903. return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
  904. }
  905. static struct attribute_group iscsi_flashnode_sess_attr_group = {
  906. .attrs = iscsi_flashnode_sess_attrs,
  907. .is_visible = iscsi_flashnode_sess_attr_is_visible,
  908. };
  909. static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
  910. &iscsi_flashnode_sess_attr_group,
  911. NULL,
  912. };
  913. static void iscsi_flashnode_sess_release(struct device *dev)
  914. {
  915. struct iscsi_bus_flash_session *fnode_sess =
  916. iscsi_dev_to_flash_session(dev);
  917. kfree(fnode_sess->targetname);
  918. kfree(fnode_sess->targetalias);
  919. kfree(fnode_sess->portal_type);
  920. kfree(fnode_sess);
  921. }
  922. struct device_type iscsi_flashnode_sess_dev_type = {
  923. .name = "iscsi_flashnode_sess_dev_type",
  924. .groups = iscsi_flashnode_sess_attr_groups,
  925. .release = iscsi_flashnode_sess_release,
  926. };
  927. /* flash node connection attrs show */
  928. #define iscsi_flashnode_conn_attr_show(type, name, param) \
  929. static ssize_t \
  930. show_##type##_##name(struct device *dev, struct device_attribute *attr, \
  931. char *buf) \
  932. { \
  933. struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
  934. struct iscsi_bus_flash_session *fnode_sess = \
  935. iscsi_flash_conn_to_flash_session(fnode_conn);\
  936. struct iscsi_transport *t = fnode_conn->transport; \
  937. return t->get_flashnode_param(fnode_sess, param, buf); \
  938. } \
  939. #define iscsi_flashnode_conn_attr(type, name, param) \
  940. iscsi_flashnode_conn_attr_show(type, name, param) \
  941. static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
  942. show_##type##_##name, NULL);
  943. /* Flash node connection attributes */
  944. iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
  945. ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
  946. iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
  947. iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
  948. iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
  949. iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
  950. ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
  951. iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
  952. ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
  953. iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
  954. ISCSI_FLASHNODE_TCP_WSF_DISABLE);
  955. iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
  956. ISCSI_FLASHNODE_TCP_TIMER_SCALE);
  957. iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
  958. ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
  959. iscsi_flashnode_conn_attr(fnode, fragment_disable,
  960. ISCSI_FLASHNODE_IP_FRAG_DISABLE);
  961. iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
  962. iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
  963. iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
  964. iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
  965. ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
  966. iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
  967. ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
  968. iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
  969. iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
  970. iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
  971. iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
  972. ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
  973. iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
  974. ISCSI_FLASHNODE_REDIRECT_IPADDR);
  975. iscsi_flashnode_conn_attr(fnode, max_segment_size,
  976. ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
  977. iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
  978. ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
  979. iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
  980. iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
  981. iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
  982. iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
  983. static struct attribute *iscsi_flashnode_conn_attrs[] = {
  984. &dev_attr_fnode_is_fw_assigned_ipv6.attr,
  985. &dev_attr_fnode_header_digest.attr,
  986. &dev_attr_fnode_data_digest.attr,
  987. &dev_attr_fnode_snack_req.attr,
  988. &dev_attr_fnode_tcp_timestamp_stat.attr,
  989. &dev_attr_fnode_tcp_nagle_disable.attr,
  990. &dev_attr_fnode_tcp_wsf_disable.attr,
  991. &dev_attr_fnode_tcp_timer_scale.attr,
  992. &dev_attr_fnode_tcp_timestamp_enable.attr,
  993. &dev_attr_fnode_fragment_disable.attr,
  994. &dev_attr_fnode_max_recv_dlength.attr,
  995. &dev_attr_fnode_max_xmit_dlength.attr,
  996. &dev_attr_fnode_keepalive_tmo.attr,
  997. &dev_attr_fnode_port.attr,
  998. &dev_attr_fnode_ipaddress.attr,
  999. &dev_attr_fnode_redirect_ipaddr.attr,
  1000. &dev_attr_fnode_max_segment_size.attr,
  1001. &dev_attr_fnode_local_port.attr,
  1002. &dev_attr_fnode_ipv4_tos.attr,
  1003. &dev_attr_fnode_ipv6_traffic_class.attr,
  1004. &dev_attr_fnode_ipv6_flow_label.attr,
  1005. &dev_attr_fnode_link_local_ipv6.attr,
  1006. &dev_attr_fnode_tcp_xmit_wsf.attr,
  1007. &dev_attr_fnode_tcp_recv_wsf.attr,
  1008. &dev_attr_fnode_statsn.attr,
  1009. &dev_attr_fnode_exp_statsn.attr,
  1010. NULL,
  1011. };
  1012. static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
  1013. struct attribute *attr,
  1014. int i)
  1015. {
  1016. struct device *dev = container_of(kobj, struct device, kobj);
  1017. struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
  1018. struct iscsi_transport *t = fnode_conn->transport;
  1019. int param;
  1020. if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
  1021. param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
  1022. } else if (attr == &dev_attr_fnode_header_digest.attr) {
  1023. param = ISCSI_FLASHNODE_HDR_DGST_EN;
  1024. } else if (attr == &dev_attr_fnode_data_digest.attr) {
  1025. param = ISCSI_FLASHNODE_DATA_DGST_EN;
  1026. } else if (attr == &dev_attr_fnode_snack_req.attr) {
  1027. param = ISCSI_FLASHNODE_SNACK_REQ_EN;
  1028. } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
  1029. param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
  1030. } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
  1031. param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
  1032. } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
  1033. param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
  1034. } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
  1035. param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
  1036. } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
  1037. param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
  1038. } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
  1039. param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
  1040. } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
  1041. param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
  1042. } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
  1043. param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
  1044. } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
  1045. param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
  1046. } else if (attr == &dev_attr_fnode_port.attr) {
  1047. param = ISCSI_FLASHNODE_PORT;
  1048. } else if (attr == &dev_attr_fnode_ipaddress.attr) {
  1049. param = ISCSI_FLASHNODE_IPADDR;
  1050. } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
  1051. param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
  1052. } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
  1053. param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
  1054. } else if (attr == &dev_attr_fnode_local_port.attr) {
  1055. param = ISCSI_FLASHNODE_LOCAL_PORT;
  1056. } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
  1057. param = ISCSI_FLASHNODE_IPV4_TOS;
  1058. } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
  1059. param = ISCSI_FLASHNODE_IPV6_TC;
  1060. } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
  1061. param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
  1062. } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
  1063. param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
  1064. } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
  1065. param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
  1066. } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
  1067. param = ISCSI_FLASHNODE_TCP_RECV_WSF;
  1068. } else if (attr == &dev_attr_fnode_statsn.attr) {
  1069. param = ISCSI_FLASHNODE_STATSN;
  1070. } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
  1071. param = ISCSI_FLASHNODE_EXP_STATSN;
  1072. } else {
  1073. WARN_ONCE(1, "Invalid flashnode connection attr");
  1074. return 0;
  1075. }
  1076. return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
  1077. }
  1078. static struct attribute_group iscsi_flashnode_conn_attr_group = {
  1079. .attrs = iscsi_flashnode_conn_attrs,
  1080. .is_visible = iscsi_flashnode_conn_attr_is_visible,
  1081. };
  1082. static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
  1083. &iscsi_flashnode_conn_attr_group,
  1084. NULL,
  1085. };
  1086. static void iscsi_flashnode_conn_release(struct device *dev)
  1087. {
  1088. struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
  1089. kfree(fnode_conn->ipaddress);
  1090. kfree(fnode_conn->redirect_ipaddr);
  1091. kfree(fnode_conn->link_local_ipv6_addr);
  1092. kfree(fnode_conn);
  1093. }
  1094. struct device_type iscsi_flashnode_conn_dev_type = {
  1095. .name = "iscsi_flashnode_conn_dev_type",
  1096. .groups = iscsi_flashnode_conn_attr_groups,
  1097. .release = iscsi_flashnode_conn_release,
  1098. };
  1099. struct bus_type iscsi_flashnode_bus;
  1100. int iscsi_flashnode_bus_match(struct device *dev,
  1101. struct device_driver *drv)
  1102. {
  1103. if (dev->bus == &iscsi_flashnode_bus)
  1104. return 1;
  1105. return 0;
  1106. }
  1107. EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
  1108. struct bus_type iscsi_flashnode_bus = {
  1109. .name = "iscsi_flashnode",
  1110. .match = &iscsi_flashnode_bus_match,
  1111. };
  1112. /**
  1113. * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
  1114. * @shost: pointer to host data
  1115. * @index: index of flashnode to add in sysfs
  1116. * @transport: pointer to transport data
  1117. * @dd_size: total size to allocate
  1118. *
  1119. * Adds a sysfs entry for the flashnode session attributes
  1120. *
  1121. * Returns:
  1122. * pointer to allocated flashnode sess on success
  1123. * %NULL on failure
  1124. */
  1125. struct iscsi_bus_flash_session *
  1126. iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
  1127. struct iscsi_transport *transport,
  1128. int dd_size)
  1129. {
  1130. struct iscsi_bus_flash_session *fnode_sess;
  1131. int err;
  1132. fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
  1133. if (!fnode_sess)
  1134. return NULL;
  1135. fnode_sess->transport = transport;
  1136. fnode_sess->target_id = index;
  1137. fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
  1138. fnode_sess->dev.bus = &iscsi_flashnode_bus;
  1139. fnode_sess->dev.parent = &shost->shost_gendev;
  1140. dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
  1141. shost->host_no, index);
  1142. err = device_register(&fnode_sess->dev);
  1143. if (err)
  1144. goto free_fnode_sess;
  1145. if (dd_size)
  1146. fnode_sess->dd_data = &fnode_sess[1];
  1147. return fnode_sess;
  1148. free_fnode_sess:
  1149. kfree(fnode_sess);
  1150. return NULL;
  1151. }
  1152. EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
  1153. /**
  1154. * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
  1155. * @shost: pointer to host data
  1156. * @fnode_sess: pointer to the parent flashnode session entry
  1157. * @transport: pointer to transport data
  1158. * @dd_size: total size to allocate
  1159. *
  1160. * Adds a sysfs entry for the flashnode connection attributes
  1161. *
  1162. * Returns:
  1163. * pointer to allocated flashnode conn on success
  1164. * %NULL on failure
  1165. */
  1166. struct iscsi_bus_flash_conn *
  1167. iscsi_create_flashnode_conn(struct Scsi_Host *shost,
  1168. struct iscsi_bus_flash_session *fnode_sess,
  1169. struct iscsi_transport *transport,
  1170. int dd_size)
  1171. {
  1172. struct iscsi_bus_flash_conn *fnode_conn;
  1173. int err;
  1174. fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
  1175. if (!fnode_conn)
  1176. return NULL;
  1177. fnode_conn->transport = transport;
  1178. fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
  1179. fnode_conn->dev.bus = &iscsi_flashnode_bus;
  1180. fnode_conn->dev.parent = &fnode_sess->dev;
  1181. dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
  1182. shost->host_no, fnode_sess->target_id);
  1183. err = device_register(&fnode_conn->dev);
  1184. if (err)
  1185. goto free_fnode_conn;
  1186. if (dd_size)
  1187. fnode_conn->dd_data = &fnode_conn[1];
  1188. return fnode_conn;
  1189. free_fnode_conn:
  1190. kfree(fnode_conn);
  1191. return NULL;
  1192. }
  1193. EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
  1194. /**
  1195. * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
  1196. * @dev: device to verify
  1197. * @data: pointer to data containing value to use for verification
  1198. *
  1199. * Verifies if the passed device is flashnode conn device
  1200. *
  1201. * Returns:
  1202. * 1 on success
  1203. * 0 on failure
  1204. */
  1205. int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
  1206. {
  1207. return dev->bus == &iscsi_flashnode_bus;
  1208. }
  1209. EXPORT_SYMBOL_GPL(iscsi_is_flashnode_conn_dev);
  1210. static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
  1211. {
  1212. device_unregister(&fnode_conn->dev);
  1213. return 0;
  1214. }
  1215. static int flashnode_match_index(struct device *dev, void *data)
  1216. {
  1217. struct iscsi_bus_flash_session *fnode_sess = NULL;
  1218. int ret = 0;
  1219. if (!iscsi_flashnode_bus_match(dev, NULL))
  1220. goto exit_match_index;
  1221. fnode_sess = iscsi_dev_to_flash_session(dev);
  1222. ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
  1223. exit_match_index:
  1224. return ret;
  1225. }
  1226. /**
  1227. * iscsi_get_flashnode_by_index -finds flashnode session entry by index
  1228. * @shost: pointer to host data
  1229. * @idx: index to match
  1230. *
  1231. * Finds the flashnode session object for the passed index
  1232. *
  1233. * Returns:
  1234. * pointer to found flashnode session object on success
  1235. * %NULL on failure
  1236. */
  1237. static struct iscsi_bus_flash_session *
  1238. iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
  1239. {
  1240. struct iscsi_bus_flash_session *fnode_sess = NULL;
  1241. struct device *dev;
  1242. dev = device_find_child(&shost->shost_gendev, &idx,
  1243. flashnode_match_index);
  1244. if (dev)
  1245. fnode_sess = iscsi_dev_to_flash_session(dev);
  1246. return fnode_sess;
  1247. }
  1248. /**
  1249. * iscsi_find_flashnode_sess - finds flashnode session entry
  1250. * @shost: pointer to host data
  1251. * @data: pointer to data containing value to use for comparison
  1252. * @fn: function pointer that does actual comparison
  1253. *
  1254. * Finds the flashnode session object comparing the data passed using logic
  1255. * defined in passed function pointer
  1256. *
  1257. * Returns:
  1258. * pointer to found flashnode session device object on success
  1259. * %NULL on failure
  1260. */
  1261. struct device *
  1262. iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
  1263. int (*fn)(struct device *dev, void *data))
  1264. {
  1265. return device_find_child(&shost->shost_gendev, data, fn);
  1266. }
  1267. EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
  1268. /**
  1269. * iscsi_find_flashnode_conn - finds flashnode connection entry
  1270. * @fnode_sess: pointer to parent flashnode session entry
  1271. *
  1272. * Finds the flashnode connection object comparing the data passed using logic
  1273. * defined in passed function pointer
  1274. *
  1275. * Returns:
  1276. * pointer to found flashnode connection device object on success
  1277. * %NULL on failure
  1278. */
  1279. struct device *
  1280. iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
  1281. {
  1282. return device_find_child(&fnode_sess->dev, NULL,
  1283. iscsi_is_flashnode_conn_dev);
  1284. }
  1285. EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
  1286. static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
  1287. {
  1288. if (!iscsi_is_flashnode_conn_dev(dev, NULL))
  1289. return 0;
  1290. return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
  1291. }
  1292. /**
  1293. * iscsi_destroy_flashnode_sess - destroy flashnode session entry
  1294. * @fnode_sess: pointer to flashnode session entry to be destroyed
  1295. *
  1296. * Deletes the flashnode session entry and all children flashnode connection
  1297. * entries from sysfs
  1298. */
  1299. void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
  1300. {
  1301. int err;
  1302. err = device_for_each_child(&fnode_sess->dev, NULL,
  1303. iscsi_iter_destroy_flashnode_conn_fn);
  1304. if (err)
  1305. pr_err("Could not delete all connections for %s. Error %d.\n",
  1306. fnode_sess->dev.kobj.name, err);
  1307. device_unregister(&fnode_sess->dev);
  1308. }
  1309. EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
  1310. static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
  1311. {
  1312. if (!iscsi_flashnode_bus_match(dev, NULL))
  1313. return 0;
  1314. iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
  1315. return 0;
  1316. }
  1317. /**
  1318. * iscsi_destroy_all_flashnode - destroy all flashnode session entries
  1319. * @shost: pointer to host data
  1320. *
  1321. * Destroys all the flashnode session entries and all corresponding children
  1322. * flashnode connection entries from sysfs
  1323. */
  1324. void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
  1325. {
  1326. device_for_each_child(&shost->shost_gendev, NULL,
  1327. iscsi_iter_destroy_flashnode_fn);
  1328. }
  1329. EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
  1330. /*
  1331. * BSG support
  1332. */
  1333. /**
  1334. * iscsi_bsg_host_dispatch - Dispatch command to LLD.
  1335. * @job: bsg job to be processed
  1336. */
  1337. static int iscsi_bsg_host_dispatch(struct bsg_job *job)
  1338. {
  1339. struct Scsi_Host *shost = iscsi_job_to_shost(job);
  1340. struct iscsi_bsg_request *req = job->request;
  1341. struct iscsi_bsg_reply *reply = job->reply;
  1342. struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
  1343. int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
  1344. int ret;
  1345. /* check if we have the msgcode value at least */
  1346. if (job->request_len < sizeof(uint32_t)) {
  1347. ret = -ENOMSG;
  1348. goto fail_host_msg;
  1349. }
  1350. /* Validate the host command */
  1351. switch (req->msgcode) {
  1352. case ISCSI_BSG_HST_VENDOR:
  1353. cmdlen += sizeof(struct iscsi_bsg_host_vendor);
  1354. if ((shost->hostt->vendor_id == 0L) ||
  1355. (req->rqst_data.h_vendor.vendor_id !=
  1356. shost->hostt->vendor_id)) {
  1357. ret = -ESRCH;
  1358. goto fail_host_msg;
  1359. }
  1360. break;
  1361. default:
  1362. ret = -EBADR;
  1363. goto fail_host_msg;
  1364. }
  1365. /* check if we really have all the request data needed */
  1366. if (job->request_len < cmdlen) {
  1367. ret = -ENOMSG;
  1368. goto fail_host_msg;
  1369. }
  1370. ret = i->iscsi_transport->bsg_request(job);
  1371. if (!ret)
  1372. return 0;
  1373. fail_host_msg:
  1374. /* return the errno failure code as the only status */
  1375. BUG_ON(job->reply_len < sizeof(uint32_t));
  1376. reply->reply_payload_rcv_len = 0;
  1377. reply->result = ret;
  1378. job->reply_len = sizeof(uint32_t);
  1379. bsg_job_done(job, ret, 0);
  1380. return 0;
  1381. }
  1382. /**
  1383. * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
  1384. * @shost: shost for iscsi_host
  1385. * @ihost: iscsi_cls_host adding the structures to
  1386. */
  1387. static int
  1388. iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
  1389. {
  1390. struct device *dev = &shost->shost_gendev;
  1391. struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
  1392. struct request_queue *q;
  1393. char bsg_name[20];
  1394. int ret;
  1395. if (!i->iscsi_transport->bsg_request)
  1396. return -ENOTSUPP;
  1397. snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
  1398. q = __scsi_alloc_queue(shost, bsg_request_fn);
  1399. if (!q)
  1400. return -ENOMEM;
  1401. ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
  1402. if (ret) {
  1403. shost_printk(KERN_ERR, shost, "bsg interface failed to "
  1404. "initialize - no request queue\n");
  1405. blk_cleanup_queue(q);
  1406. return ret;
  1407. }
  1408. ihost->bsg_q = q;
  1409. return 0;
  1410. }
  1411. static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
  1412. struct device *cdev)
  1413. {
  1414. struct Scsi_Host *shost = dev_to_shost(dev);
  1415. struct iscsi_cls_host *ihost = shost->shost_data;
  1416. memset(ihost, 0, sizeof(*ihost));
  1417. atomic_set(&ihost->nr_scans, 0);
  1418. mutex_init(&ihost->mutex);
  1419. iscsi_bsg_host_add(shost, ihost);
  1420. /* ignore any bsg add error - we just can't do sgio */
  1421. return 0;
  1422. }
  1423. static int iscsi_remove_host(struct transport_container *tc,
  1424. struct device *dev, struct device *cdev)
  1425. {
  1426. struct Scsi_Host *shost = dev_to_shost(dev);
  1427. struct iscsi_cls_host *ihost = shost->shost_data;
  1428. if (ihost->bsg_q) {
  1429. bsg_unregister_queue(ihost->bsg_q);
  1430. blk_cleanup_queue(ihost->bsg_q);
  1431. }
  1432. return 0;
  1433. }
  1434. static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
  1435. "iscsi_host",
  1436. iscsi_setup_host,
  1437. iscsi_remove_host,
  1438. NULL);
  1439. static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
  1440. "iscsi_session",
  1441. NULL,
  1442. NULL,
  1443. NULL);
  1444. static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
  1445. "iscsi_connection",
  1446. NULL,
  1447. NULL,
  1448. NULL);
  1449. static struct sock *nls;
  1450. static DEFINE_MUTEX(rx_queue_mutex);
  1451. static LIST_HEAD(sesslist);
  1452. static DEFINE_SPINLOCK(sesslock);
  1453. static LIST_HEAD(connlist);
  1454. static DEFINE_SPINLOCK(connlock);
  1455. static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
  1456. {
  1457. struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
  1458. return sess->sid;
  1459. }
  1460. /*
  1461. * Returns the matching session to a given sid
  1462. */
  1463. static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
  1464. {
  1465. unsigned long flags;
  1466. struct iscsi_cls_session *sess;
  1467. spin_lock_irqsave(&sesslock, flags);
  1468. list_for_each_entry(sess, &sesslist, sess_list) {
  1469. if (sess->sid == sid) {
  1470. spin_unlock_irqrestore(&sesslock, flags);
  1471. return sess;
  1472. }
  1473. }
  1474. spin_unlock_irqrestore(&sesslock, flags);
  1475. return NULL;
  1476. }
  1477. /*
  1478. * Returns the matching connection to a given sid / cid tuple
  1479. */
  1480. static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
  1481. {
  1482. unsigned long flags;
  1483. struct iscsi_cls_conn *conn;
  1484. spin_lock_irqsave(&connlock, flags);
  1485. list_for_each_entry(conn, &connlist, conn_list) {
  1486. if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
  1487. spin_unlock_irqrestore(&connlock, flags);
  1488. return conn;
  1489. }
  1490. }
  1491. spin_unlock_irqrestore(&connlock, flags);
  1492. return NULL;
  1493. }
  1494. /*
  1495. * The following functions can be used by LLDs that allocate
  1496. * their own scsi_hosts or by software iscsi LLDs
  1497. */
  1498. static struct {
  1499. int value;
  1500. char *name;
  1501. } iscsi_session_state_names[] = {
  1502. { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
  1503. { ISCSI_SESSION_FAILED, "FAILED" },
  1504. { ISCSI_SESSION_FREE, "FREE" },
  1505. };
  1506. static const char *iscsi_session_state_name(int state)
  1507. {
  1508. int i;
  1509. char *name = NULL;
  1510. for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
  1511. if (iscsi_session_state_names[i].value == state) {
  1512. name = iscsi_session_state_names[i].name;
  1513. break;
  1514. }
  1515. }
  1516. return name;
  1517. }
  1518. int iscsi_session_chkready(struct iscsi_cls_session *session)
  1519. {
  1520. unsigned long flags;
  1521. int err;
  1522. spin_lock_irqsave(&session->lock, flags);
  1523. switch (session->state) {
  1524. case ISCSI_SESSION_LOGGED_IN:
  1525. err = 0;
  1526. break;
  1527. case ISCSI_SESSION_FAILED:
  1528. err = DID_IMM_RETRY << 16;
  1529. break;
  1530. case ISCSI_SESSION_FREE:
  1531. err = DID_TRANSPORT_FAILFAST << 16;
  1532. break;
  1533. default:
  1534. err = DID_NO_CONNECT << 16;
  1535. break;
  1536. }
  1537. spin_unlock_irqrestore(&session->lock, flags);
  1538. return err;
  1539. }
  1540. EXPORT_SYMBOL_GPL(iscsi_session_chkready);
  1541. int iscsi_is_session_online(struct iscsi_cls_session *session)
  1542. {
  1543. unsigned long flags;
  1544. int ret = 0;
  1545. spin_lock_irqsave(&session->lock, flags);
  1546. if (session->state == ISCSI_SESSION_LOGGED_IN)
  1547. ret = 1;
  1548. spin_unlock_irqrestore(&session->lock, flags);
  1549. return ret;
  1550. }
  1551. EXPORT_SYMBOL_GPL(iscsi_is_session_online);
  1552. static void iscsi_session_release(struct device *dev)
  1553. {
  1554. struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
  1555. struct Scsi_Host *shost;
  1556. shost = iscsi_session_to_shost(session);
  1557. scsi_host_put(shost);
  1558. ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
  1559. kfree(session);
  1560. }
  1561. int iscsi_is_session_dev(const struct device *dev)
  1562. {
  1563. return dev->release == iscsi_session_release;
  1564. }
  1565. EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
  1566. static int iscsi_iter_session_fn(struct device *dev, void *data)
  1567. {
  1568. void (* fn) (struct iscsi_cls_session *) = data;
  1569. if (!iscsi_is_session_dev(dev))
  1570. return 0;
  1571. fn(iscsi_dev_to_session(dev));
  1572. return 0;
  1573. }
  1574. void iscsi_host_for_each_session(struct Scsi_Host *shost,
  1575. void (*fn)(struct iscsi_cls_session *))
  1576. {
  1577. device_for_each_child(&shost->shost_gendev, fn,
  1578. iscsi_iter_session_fn);
  1579. }
  1580. EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
  1581. /**
  1582. * iscsi_scan_finished - helper to report when running scans are done
  1583. * @shost: scsi host
  1584. * @time: scan run time
  1585. *
  1586. * This function can be used by drives like qla4xxx to report to the scsi
  1587. * layer when the scans it kicked off at module load time are done.
  1588. */
  1589. int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1590. {
  1591. struct iscsi_cls_host *ihost = shost->shost_data;
  1592. /*
  1593. * qla4xxx will have kicked off some session unblocks before calling
  1594. * scsi_scan_host, so just wait for them to complete.
  1595. */
  1596. return !atomic_read(&ihost->nr_scans);
  1597. }
  1598. EXPORT_SYMBOL_GPL(iscsi_scan_finished);
  1599. struct iscsi_scan_data {
  1600. unsigned int channel;
  1601. unsigned int id;
  1602. u64 lun;
  1603. };
  1604. static int iscsi_user_scan_session(struct device *dev, void *data)
  1605. {
  1606. struct iscsi_scan_data *scan_data = data;
  1607. struct iscsi_cls_session *session;
  1608. struct Scsi_Host *shost;
  1609. struct iscsi_cls_host *ihost;
  1610. unsigned long flags;
  1611. unsigned int id;
  1612. if (!iscsi_is_session_dev(dev))
  1613. return 0;
  1614. session = iscsi_dev_to_session(dev);
  1615. ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
  1616. shost = iscsi_session_to_shost(session);
  1617. ihost = shost->shost_data;
  1618. mutex_lock(&ihost->mutex);
  1619. spin_lock_irqsave(&session->lock, flags);
  1620. if (session->state != ISCSI_SESSION_LOGGED_IN) {
  1621. spin_unlock_irqrestore(&session->lock, flags);
  1622. goto user_scan_exit;
  1623. }
  1624. id = session->target_id;
  1625. spin_unlock_irqrestore(&session->lock, flags);
  1626. if (id != ISCSI_MAX_TARGET) {
  1627. if ((scan_data->channel == SCAN_WILD_CARD ||
  1628. scan_data->channel == 0) &&
  1629. (scan_data->id == SCAN_WILD_CARD ||
  1630. scan_data->id == id))
  1631. scsi_scan_target(&session->dev, 0, id,
  1632. scan_data->lun, 1);
  1633. }
  1634. user_scan_exit:
  1635. mutex_unlock(&ihost->mutex);
  1636. ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
  1637. return 0;
  1638. }
  1639. static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
  1640. uint id, u64 lun)
  1641. {
  1642. struct iscsi_scan_data scan_data;
  1643. scan_data.channel = channel;
  1644. scan_data.id = id;
  1645. scan_data.lun = lun;
  1646. return device_for_each_child(&shost->shost_gendev, &scan_data,
  1647. iscsi_user_scan_session);
  1648. }
  1649. static void iscsi_scan_session(struct work_struct *work)
  1650. {
  1651. struct iscsi_cls_session *session =
  1652. container_of(work, struct iscsi_cls_session, scan_work);
  1653. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  1654. struct iscsi_cls_host *ihost = shost->shost_data;
  1655. struct iscsi_scan_data scan_data;
  1656. scan_data.channel = 0;
  1657. scan_data.id = SCAN_WILD_CARD;
  1658. scan_data.lun = SCAN_WILD_CARD;
  1659. iscsi_user_scan_session(&session->dev, &scan_data);
  1660. atomic_dec(&ihost->nr_scans);
  1661. }
  1662. /**
  1663. * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
  1664. * @cmd: scsi cmd passed to scsi eh handler
  1665. *
  1666. * If the session is down this function will wait for the recovery
  1667. * timer to fire or for the session to be logged back in. If the
  1668. * recovery timer fires then FAST_IO_FAIL is returned. The caller
  1669. * should pass this error value to the scsi eh.
  1670. */
  1671. int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
  1672. {
  1673. struct iscsi_cls_session *session =
  1674. starget_to_session(scsi_target(cmd->device));
  1675. unsigned long flags;
  1676. int ret = 0;
  1677. spin_lock_irqsave(&session->lock, flags);
  1678. while (session->state != ISCSI_SESSION_LOGGED_IN) {
  1679. if (session->state == ISCSI_SESSION_FREE) {
  1680. ret = FAST_IO_FAIL;
  1681. break;
  1682. }
  1683. spin_unlock_irqrestore(&session->lock, flags);
  1684. msleep(1000);
  1685. spin_lock_irqsave(&session->lock, flags);
  1686. }
  1687. spin_unlock_irqrestore(&session->lock, flags);
  1688. return ret;
  1689. }
  1690. EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
  1691. static void session_recovery_timedout(struct work_struct *work)
  1692. {
  1693. struct iscsi_cls_session *session =
  1694. container_of(work, struct iscsi_cls_session,
  1695. recovery_work.work);
  1696. unsigned long flags;
  1697. iscsi_cls_session_printk(KERN_INFO, session,
  1698. "session recovery timed out after %d secs\n",
  1699. session->recovery_tmo);
  1700. spin_lock_irqsave(&session->lock, flags);
  1701. switch (session->state) {
  1702. case ISCSI_SESSION_FAILED:
  1703. session->state = ISCSI_SESSION_FREE;
  1704. break;
  1705. case ISCSI_SESSION_LOGGED_IN:
  1706. case ISCSI_SESSION_FREE:
  1707. /* we raced with the unblock's flush */
  1708. spin_unlock_irqrestore(&session->lock, flags);
  1709. return;
  1710. }
  1711. spin_unlock_irqrestore(&session->lock, flags);
  1712. if (session->transport->session_recovery_timedout)
  1713. session->transport->session_recovery_timedout(session);
  1714. ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
  1715. scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
  1716. ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
  1717. }
  1718. static void __iscsi_unblock_session(struct work_struct *work)
  1719. {
  1720. struct iscsi_cls_session *session =
  1721. container_of(work, struct iscsi_cls_session,
  1722. unblock_work);
  1723. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  1724. struct iscsi_cls_host *ihost = shost->shost_data;
  1725. unsigned long flags;
  1726. ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
  1727. /*
  1728. * The recovery and unblock work get run from the same workqueue,
  1729. * so try to cancel it if it was going to run after this unblock.
  1730. */
  1731. cancel_delayed_work(&session->recovery_work);
  1732. spin_lock_irqsave(&session->lock, flags);
  1733. session->state = ISCSI_SESSION_LOGGED_IN;
  1734. spin_unlock_irqrestore(&session->lock, flags);
  1735. /* start IO */
  1736. scsi_target_unblock(&session->dev, SDEV_RUNNING);
  1737. /*
  1738. * Only do kernel scanning if the driver is properly hooked into
  1739. * the async scanning code (drivers like iscsi_tcp do login and
  1740. * scanning from userspace).
  1741. */
  1742. if (shost->hostt->scan_finished) {
  1743. if (scsi_queue_work(shost, &session->scan_work))
  1744. atomic_inc(&ihost->nr_scans);
  1745. }
  1746. ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
  1747. }
  1748. /**
  1749. * iscsi_unblock_session - set a session as logged in and start IO.
  1750. * @session: iscsi session
  1751. *
  1752. * Mark a session as ready to accept IO.
  1753. */
  1754. void iscsi_unblock_session(struct iscsi_cls_session *session)
  1755. {
  1756. queue_work(iscsi_eh_timer_workq, &session->unblock_work);
  1757. /*
  1758. * make sure all the events have completed before tell the driver
  1759. * it is safe
  1760. */
  1761. flush_workqueue(iscsi_eh_timer_workq);
  1762. }
  1763. EXPORT_SYMBOL_GPL(iscsi_unblock_session);
  1764. static void __iscsi_block_session(struct work_struct *work)
  1765. {
  1766. struct iscsi_cls_session *session =
  1767. container_of(work, struct iscsi_cls_session,
  1768. block_work);
  1769. unsigned long flags;
  1770. ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
  1771. spin_lock_irqsave(&session->lock, flags);
  1772. session->state = ISCSI_SESSION_FAILED;
  1773. spin_unlock_irqrestore(&session->lock, flags);
  1774. scsi_target_block(&session->dev);
  1775. ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
  1776. if (session->recovery_tmo >= 0)
  1777. queue_delayed_work(iscsi_eh_timer_workq,
  1778. &session->recovery_work,
  1779. session->recovery_tmo * HZ);
  1780. }
  1781. void iscsi_block_session(struct iscsi_cls_session *session)
  1782. {
  1783. queue_work(iscsi_eh_timer_workq, &session->block_work);
  1784. }
  1785. EXPORT_SYMBOL_GPL(iscsi_block_session);
  1786. static void __iscsi_unbind_session(struct work_struct *work)
  1787. {
  1788. struct iscsi_cls_session *session =
  1789. container_of(work, struct iscsi_cls_session,
  1790. unbind_work);
  1791. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  1792. struct iscsi_cls_host *ihost = shost->shost_data;
  1793. unsigned long flags;
  1794. unsigned int target_id;
  1795. ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
  1796. /* Prevent new scans and make sure scanning is not in progress */
  1797. mutex_lock(&ihost->mutex);
  1798. spin_lock_irqsave(&session->lock, flags);
  1799. if (session->target_id == ISCSI_MAX_TARGET) {
  1800. spin_unlock_irqrestore(&session->lock, flags);
  1801. mutex_unlock(&ihost->mutex);
  1802. return;
  1803. }
  1804. target_id = session->target_id;
  1805. session->target_id = ISCSI_MAX_TARGET;
  1806. spin_unlock_irqrestore(&session->lock, flags);
  1807. mutex_unlock(&ihost->mutex);
  1808. if (session->ida_used)
  1809. ida_simple_remove(&iscsi_sess_ida, target_id);
  1810. scsi_remove_target(&session->dev);
  1811. iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
  1812. ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
  1813. }
  1814. struct iscsi_cls_session *
  1815. iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
  1816. int dd_size)
  1817. {
  1818. struct iscsi_cls_session *session;
  1819. session = kzalloc(sizeof(*session) + dd_size,
  1820. GFP_KERNEL);
  1821. if (!session)
  1822. return NULL;
  1823. session->transport = transport;
  1824. session->creator = -1;
  1825. session->recovery_tmo = 120;
  1826. session->state = ISCSI_SESSION_FREE;
  1827. INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
  1828. INIT_LIST_HEAD(&session->sess_list);
  1829. INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
  1830. INIT_WORK(&session->block_work, __iscsi_block_session);
  1831. INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
  1832. INIT_WORK(&session->scan_work, iscsi_scan_session);
  1833. spin_lock_init(&session->lock);
  1834. /* this is released in the dev's release function */
  1835. scsi_host_get(shost);
  1836. session->dev.parent = &shost->shost_gendev;
  1837. session->dev.release = iscsi_session_release;
  1838. device_initialize(&session->dev);
  1839. if (dd_size)
  1840. session->dd_data = &session[1];
  1841. ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
  1842. return session;
  1843. }
  1844. EXPORT_SYMBOL_GPL(iscsi_alloc_session);
  1845. int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
  1846. {
  1847. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  1848. struct iscsi_cls_host *ihost;
  1849. unsigned long flags;
  1850. int id = 0;
  1851. int err;
  1852. ihost = shost->shost_data;
  1853. session->sid = atomic_add_return(1, &iscsi_session_nr);
  1854. if (target_id == ISCSI_MAX_TARGET) {
  1855. id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
  1856. if (id < 0) {
  1857. iscsi_cls_session_printk(KERN_ERR, session,
  1858. "Failure in Target ID Allocation\n");
  1859. return id;
  1860. }
  1861. session->target_id = (unsigned int)id;
  1862. session->ida_used = true;
  1863. } else
  1864. session->target_id = target_id;
  1865. dev_set_name(&session->dev, "session%u", session->sid);
  1866. err = device_add(&session->dev);
  1867. if (err) {
  1868. iscsi_cls_session_printk(KERN_ERR, session,
  1869. "could not register session's dev\n");
  1870. goto release_ida;
  1871. }
  1872. transport_register_device(&session->dev);
  1873. spin_lock_irqsave(&sesslock, flags);
  1874. list_add(&session->sess_list, &sesslist);
  1875. spin_unlock_irqrestore(&sesslock, flags);
  1876. iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
  1877. ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
  1878. return 0;
  1879. release_ida:
  1880. if (session->ida_used)
  1881. ida_simple_remove(&iscsi_sess_ida, session->target_id);
  1882. return err;
  1883. }
  1884. EXPORT_SYMBOL_GPL(iscsi_add_session);
  1885. /**
  1886. * iscsi_create_session - create iscsi class session
  1887. * @shost: scsi host
  1888. * @transport: iscsi transport
  1889. * @dd_size: private driver data size
  1890. * @target_id: which target
  1891. *
  1892. * This can be called from a LLD or iscsi_transport.
  1893. */
  1894. struct iscsi_cls_session *
  1895. iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
  1896. int dd_size, unsigned int target_id)
  1897. {
  1898. struct iscsi_cls_session *session;
  1899. session = iscsi_alloc_session(shost, transport, dd_size);
  1900. if (!session)
  1901. return NULL;
  1902. if (iscsi_add_session(session, target_id)) {
  1903. iscsi_free_session(session);
  1904. return NULL;
  1905. }
  1906. return session;
  1907. }
  1908. EXPORT_SYMBOL_GPL(iscsi_create_session);
  1909. static void iscsi_conn_release(struct device *dev)
  1910. {
  1911. struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
  1912. struct device *parent = conn->dev.parent;
  1913. ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
  1914. kfree(conn);
  1915. put_device(parent);
  1916. }
  1917. static int iscsi_is_conn_dev(const struct device *dev)
  1918. {
  1919. return dev->release == iscsi_conn_release;
  1920. }
  1921. static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
  1922. {
  1923. if (!iscsi_is_conn_dev(dev))
  1924. return 0;
  1925. return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
  1926. }
  1927. void iscsi_remove_session(struct iscsi_cls_session *session)
  1928. {
  1929. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  1930. unsigned long flags;
  1931. int err;
  1932. ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
  1933. spin_lock_irqsave(&sesslock, flags);
  1934. list_del(&session->sess_list);
  1935. spin_unlock_irqrestore(&sesslock, flags);
  1936. /* make sure there are no blocks/unblocks queued */
  1937. flush_workqueue(iscsi_eh_timer_workq);
  1938. /* make sure the timedout callout is not running */
  1939. if (!cancel_delayed_work(&session->recovery_work))
  1940. flush_workqueue(iscsi_eh_timer_workq);
  1941. /*
  1942. * If we are blocked let commands flow again. The lld or iscsi
  1943. * layer should set up the queuecommand to fail commands.
  1944. * We assume that LLD will not be calling block/unblock while
  1945. * removing the session.
  1946. */
  1947. spin_lock_irqsave(&session->lock, flags);
  1948. session->state = ISCSI_SESSION_FREE;
  1949. spin_unlock_irqrestore(&session->lock, flags);
  1950. scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
  1951. /* flush running scans then delete devices */
  1952. scsi_flush_work(shost);
  1953. __iscsi_unbind_session(&session->unbind_work);
  1954. /* hw iscsi may not have removed all connections from session */
  1955. err = device_for_each_child(&session->dev, NULL,
  1956. iscsi_iter_destroy_conn_fn);
  1957. if (err)
  1958. iscsi_cls_session_printk(KERN_ERR, session,
  1959. "Could not delete all connections "
  1960. "for session. Error %d.\n", err);
  1961. transport_unregister_device(&session->dev);
  1962. ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
  1963. device_del(&session->dev);
  1964. }
  1965. EXPORT_SYMBOL_GPL(iscsi_remove_session);
  1966. void iscsi_free_session(struct iscsi_cls_session *session)
  1967. {
  1968. ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
  1969. iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
  1970. put_device(&session->dev);
  1971. }
  1972. EXPORT_SYMBOL_GPL(iscsi_free_session);
  1973. /**
  1974. * iscsi_destroy_session - destroy iscsi session
  1975. * @session: iscsi_session
  1976. *
  1977. * Can be called by a LLD or iscsi_transport. There must not be
  1978. * any running connections.
  1979. */
  1980. int iscsi_destroy_session(struct iscsi_cls_session *session)
  1981. {
  1982. iscsi_remove_session(session);
  1983. ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
  1984. iscsi_free_session(session);
  1985. return 0;
  1986. }
  1987. EXPORT_SYMBOL_GPL(iscsi_destroy_session);
  1988. /**
  1989. * iscsi_create_conn - create iscsi class connection
  1990. * @session: iscsi cls session
  1991. * @dd_size: private driver data size
  1992. * @cid: connection id
  1993. *
  1994. * This can be called from a LLD or iscsi_transport. The connection
  1995. * is child of the session so cid must be unique for all connections
  1996. * on the session.
  1997. *
  1998. * Since we do not support MCS, cid will normally be zero. In some cases
  1999. * for software iscsi we could be trying to preallocate a connection struct
  2000. * in which case there could be two connection structs and cid would be
  2001. * non-zero.
  2002. */
  2003. struct iscsi_cls_conn *
  2004. iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
  2005. {
  2006. struct iscsi_transport *transport = session->transport;
  2007. struct iscsi_cls_conn *conn;
  2008. unsigned long flags;
  2009. int err;
  2010. conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
  2011. if (!conn)
  2012. return NULL;
  2013. if (dd_size)
  2014. conn->dd_data = &conn[1];
  2015. mutex_init(&conn->ep_mutex);
  2016. INIT_LIST_HEAD(&conn->conn_list);
  2017. conn->transport = transport;
  2018. conn->cid = cid;
  2019. /* this is released in the dev's release function */
  2020. if (!get_device(&session->dev))
  2021. goto free_conn;
  2022. dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
  2023. conn->dev.parent = &session->dev;
  2024. conn->dev.release = iscsi_conn_release;
  2025. err = device_register(&conn->dev);
  2026. if (err) {
  2027. iscsi_cls_session_printk(KERN_ERR, session, "could not "
  2028. "register connection's dev\n");
  2029. goto release_parent_ref;
  2030. }
  2031. transport_register_device(&conn->dev);
  2032. spin_lock_irqsave(&connlock, flags);
  2033. list_add(&conn->conn_list, &connlist);
  2034. spin_unlock_irqrestore(&connlock, flags);
  2035. ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
  2036. return conn;
  2037. release_parent_ref:
  2038. put_device(&session->dev);
  2039. free_conn:
  2040. kfree(conn);
  2041. return NULL;
  2042. }
  2043. EXPORT_SYMBOL_GPL(iscsi_create_conn);
  2044. /**
  2045. * iscsi_destroy_conn - destroy iscsi class connection
  2046. * @conn: iscsi cls session
  2047. *
  2048. * This can be called from a LLD or iscsi_transport.
  2049. */
  2050. int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
  2051. {
  2052. unsigned long flags;
  2053. spin_lock_irqsave(&connlock, flags);
  2054. list_del(&conn->conn_list);
  2055. spin_unlock_irqrestore(&connlock, flags);
  2056. transport_unregister_device(&conn->dev);
  2057. ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
  2058. device_unregister(&conn->dev);
  2059. return 0;
  2060. }
  2061. EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
  2062. /*
  2063. * iscsi interface functions
  2064. */
  2065. static struct iscsi_internal *
  2066. iscsi_if_transport_lookup(struct iscsi_transport *tt)
  2067. {
  2068. struct iscsi_internal *priv;
  2069. unsigned long flags;
  2070. spin_lock_irqsave(&iscsi_transport_lock, flags);
  2071. list_for_each_entry(priv, &iscsi_transports, list) {
  2072. if (tt == priv->iscsi_transport) {
  2073. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  2074. return priv;
  2075. }
  2076. }
  2077. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  2078. return NULL;
  2079. }
  2080. static int
  2081. iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
  2082. {
  2083. return nlmsg_multicast(nls, skb, 0, group, gfp);
  2084. }
  2085. int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
  2086. char *data, uint32_t data_size)
  2087. {
  2088. struct nlmsghdr *nlh;
  2089. struct sk_buff *skb;
  2090. struct iscsi_uevent *ev;
  2091. char *pdu;
  2092. struct iscsi_internal *priv;
  2093. int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
  2094. data_size);
  2095. priv = iscsi_if_transport_lookup(conn->transport);
  2096. if (!priv)
  2097. return -EINVAL;
  2098. skb = alloc_skb(len, GFP_ATOMIC);
  2099. if (!skb) {
  2100. iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
  2101. iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
  2102. "control PDU: OOM\n");
  2103. return -ENOMEM;
  2104. }
  2105. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  2106. ev = nlmsg_data(nlh);
  2107. memset(ev, 0, sizeof(*ev));
  2108. ev->transport_handle = iscsi_handle(conn->transport);
  2109. ev->type = ISCSI_KEVENT_RECV_PDU;
  2110. ev->r.recv_req.cid = conn->cid;
  2111. ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
  2112. pdu = (char*)ev + sizeof(*ev);
  2113. memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
  2114. memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
  2115. return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
  2116. }
  2117. EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
  2118. int iscsi_offload_mesg(struct Scsi_Host *shost,
  2119. struct iscsi_transport *transport, uint32_t type,
  2120. char *data, uint16_t data_size)
  2121. {
  2122. struct nlmsghdr *nlh;
  2123. struct sk_buff *skb;
  2124. struct iscsi_uevent *ev;
  2125. int len = nlmsg_total_size(sizeof(*ev) + data_size);
  2126. skb = alloc_skb(len, GFP_ATOMIC);
  2127. if (!skb) {
  2128. printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
  2129. return -ENOMEM;
  2130. }
  2131. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  2132. ev = nlmsg_data(nlh);
  2133. memset(ev, 0, sizeof(*ev));
  2134. ev->type = type;
  2135. ev->transport_handle = iscsi_handle(transport);
  2136. switch (type) {
  2137. case ISCSI_KEVENT_PATH_REQ:
  2138. ev->r.req_path.host_no = shost->host_no;
  2139. break;
  2140. case ISCSI_KEVENT_IF_DOWN:
  2141. ev->r.notify_if_down.host_no = shost->host_no;
  2142. break;
  2143. }
  2144. memcpy((char *)ev + sizeof(*ev), data, data_size);
  2145. return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
  2146. }
  2147. EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
  2148. void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
  2149. {
  2150. struct nlmsghdr *nlh;
  2151. struct sk_buff *skb;
  2152. struct iscsi_uevent *ev;
  2153. struct iscsi_internal *priv;
  2154. int len = nlmsg_total_size(sizeof(*ev));
  2155. priv = iscsi_if_transport_lookup(conn->transport);
  2156. if (!priv)
  2157. return;
  2158. skb = alloc_skb(len, GFP_ATOMIC);
  2159. if (!skb) {
  2160. iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
  2161. "conn error (%d)\n", error);
  2162. return;
  2163. }
  2164. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  2165. ev = nlmsg_data(nlh);
  2166. ev->transport_handle = iscsi_handle(conn->transport);
  2167. ev->type = ISCSI_KEVENT_CONN_ERROR;
  2168. ev->r.connerror.error = error;
  2169. ev->r.connerror.cid = conn->cid;
  2170. ev->r.connerror.sid = iscsi_conn_get_sid(conn);
  2171. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
  2172. iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
  2173. error);
  2174. }
  2175. EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
  2176. void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
  2177. enum iscsi_conn_state state)
  2178. {
  2179. struct nlmsghdr *nlh;
  2180. struct sk_buff *skb;
  2181. struct iscsi_uevent *ev;
  2182. struct iscsi_internal *priv;
  2183. int len = nlmsg_total_size(sizeof(*ev));
  2184. priv = iscsi_if_transport_lookup(conn->transport);
  2185. if (!priv)
  2186. return;
  2187. skb = alloc_skb(len, GFP_ATOMIC);
  2188. if (!skb) {
  2189. iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
  2190. "conn login (%d)\n", state);
  2191. return;
  2192. }
  2193. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  2194. ev = nlmsg_data(nlh);
  2195. ev->transport_handle = iscsi_handle(conn->transport);
  2196. ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
  2197. ev->r.conn_login.state = state;
  2198. ev->r.conn_login.cid = conn->cid;
  2199. ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
  2200. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
  2201. iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
  2202. state);
  2203. }
  2204. EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
  2205. void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
  2206. enum iscsi_host_event_code code, uint32_t data_size,
  2207. uint8_t *data)
  2208. {
  2209. struct nlmsghdr *nlh;
  2210. struct sk_buff *skb;
  2211. struct iscsi_uevent *ev;
  2212. int len = nlmsg_total_size(sizeof(*ev) + data_size);
  2213. skb = alloc_skb(len, GFP_NOIO);
  2214. if (!skb) {
  2215. printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
  2216. host_no, code);
  2217. return;
  2218. }
  2219. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  2220. ev = nlmsg_data(nlh);
  2221. ev->transport_handle = iscsi_handle(transport);
  2222. ev->type = ISCSI_KEVENT_HOST_EVENT;
  2223. ev->r.host_event.host_no = host_no;
  2224. ev->r.host_event.code = code;
  2225. ev->r.host_event.data_size = data_size;
  2226. if (data_size)
  2227. memcpy((char *)ev + sizeof(*ev), data, data_size);
  2228. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
  2229. }
  2230. EXPORT_SYMBOL_GPL(iscsi_post_host_event);
  2231. void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
  2232. uint32_t status, uint32_t pid, uint32_t data_size,
  2233. uint8_t *data)
  2234. {
  2235. struct nlmsghdr *nlh;
  2236. struct sk_buff *skb;
  2237. struct iscsi_uevent *ev;
  2238. int len = nlmsg_total_size(sizeof(*ev) + data_size);
  2239. skb = alloc_skb(len, GFP_NOIO);
  2240. if (!skb) {
  2241. printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
  2242. return;
  2243. }
  2244. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  2245. ev = nlmsg_data(nlh);
  2246. ev->transport_handle = iscsi_handle(transport);
  2247. ev->type = ISCSI_KEVENT_PING_COMP;
  2248. ev->r.ping_comp.host_no = host_no;
  2249. ev->r.ping_comp.status = status;
  2250. ev->r.ping_comp.pid = pid;
  2251. ev->r.ping_comp.data_size = data_size;
  2252. memcpy((char *)ev + sizeof(*ev), data, data_size);
  2253. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
  2254. }
  2255. EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
  2256. static int
  2257. iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
  2258. void *payload, int size)
  2259. {
  2260. struct sk_buff *skb;
  2261. struct nlmsghdr *nlh;
  2262. int len = nlmsg_total_size(size);
  2263. int flags = multi ? NLM_F_MULTI : 0;
  2264. int t = done ? NLMSG_DONE : type;
  2265. skb = alloc_skb(len, GFP_ATOMIC);
  2266. if (!skb) {
  2267. printk(KERN_ERR "Could not allocate skb to send reply.\n");
  2268. return -ENOMEM;
  2269. }
  2270. nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
  2271. nlh->nlmsg_flags = flags;
  2272. memcpy(nlmsg_data(nlh), payload, size);
  2273. return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
  2274. }
  2275. static int
  2276. iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
  2277. {
  2278. struct iscsi_uevent *ev = nlmsg_data(nlh);
  2279. struct iscsi_stats *stats;
  2280. struct sk_buff *skbstat;
  2281. struct iscsi_cls_conn *conn;
  2282. struct nlmsghdr *nlhstat;
  2283. struct iscsi_uevent *evstat;
  2284. struct iscsi_internal *priv;
  2285. int len = nlmsg_total_size(sizeof(*ev) +
  2286. sizeof(struct iscsi_stats) +
  2287. sizeof(struct iscsi_stats_custom) *
  2288. ISCSI_STATS_CUSTOM_MAX);
  2289. int err = 0;
  2290. priv = iscsi_if_transport_lookup(transport);
  2291. if (!priv)
  2292. return -EINVAL;
  2293. conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
  2294. if (!conn)
  2295. return -EEXIST;
  2296. do {
  2297. int actual_size;
  2298. skbstat = alloc_skb(len, GFP_ATOMIC);
  2299. if (!skbstat) {
  2300. iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
  2301. "deliver stats: OOM\n");
  2302. return -ENOMEM;
  2303. }
  2304. nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
  2305. (len - sizeof(*nlhstat)), 0);
  2306. evstat = nlmsg_data(nlhstat);
  2307. memset(evstat, 0, sizeof(*evstat));
  2308. evstat->transport_handle = iscsi_handle(conn->transport);
  2309. evstat->type = nlh->nlmsg_type;
  2310. evstat->u.get_stats.cid =
  2311. ev->u.get_stats.cid;
  2312. evstat->u.get_stats.sid =
  2313. ev->u.get_stats.sid;
  2314. stats = (struct iscsi_stats *)
  2315. ((char*)evstat + sizeof(*evstat));
  2316. memset(stats, 0, sizeof(*stats));
  2317. transport->get_stats(conn, stats);
  2318. actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
  2319. sizeof(struct iscsi_stats) +
  2320. sizeof(struct iscsi_stats_custom) *
  2321. stats->custom_length);
  2322. actual_size -= sizeof(*nlhstat);
  2323. actual_size = nlmsg_msg_size(actual_size);
  2324. skb_trim(skbstat, NLMSG_ALIGN(actual_size));
  2325. nlhstat->nlmsg_len = actual_size;
  2326. err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
  2327. GFP_ATOMIC);
  2328. } while (err < 0 && err != -ECONNREFUSED);
  2329. return err;
  2330. }
  2331. /**
  2332. * iscsi_session_event - send session destr. completion event
  2333. * @session: iscsi class session
  2334. * @event: type of event
  2335. */
  2336. int iscsi_session_event(struct iscsi_cls_session *session,
  2337. enum iscsi_uevent_e event)
  2338. {
  2339. struct iscsi_internal *priv;
  2340. struct Scsi_Host *shost;
  2341. struct iscsi_uevent *ev;
  2342. struct sk_buff *skb;
  2343. struct nlmsghdr *nlh;
  2344. int rc, len = nlmsg_total_size(sizeof(*ev));
  2345. priv = iscsi_if_transport_lookup(session->transport);
  2346. if (!priv)
  2347. return -EINVAL;
  2348. shost = iscsi_session_to_shost(session);
  2349. skb = alloc_skb(len, GFP_KERNEL);
  2350. if (!skb) {
  2351. iscsi_cls_session_printk(KERN_ERR, session,
  2352. "Cannot notify userspace of session "
  2353. "event %u\n", event);
  2354. return -ENOMEM;
  2355. }
  2356. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  2357. ev = nlmsg_data(nlh);
  2358. ev->transport_handle = iscsi_handle(session->transport);
  2359. ev->type = event;
  2360. switch (event) {
  2361. case ISCSI_KEVENT_DESTROY_SESSION:
  2362. ev->r.d_session.host_no = shost->host_no;
  2363. ev->r.d_session.sid = session->sid;
  2364. break;
  2365. case ISCSI_KEVENT_CREATE_SESSION:
  2366. ev->r.c_session_ret.host_no = shost->host_no;
  2367. ev->r.c_session_ret.sid = session->sid;
  2368. break;
  2369. case ISCSI_KEVENT_UNBIND_SESSION:
  2370. ev->r.unbind_session.host_no = shost->host_no;
  2371. ev->r.unbind_session.sid = session->sid;
  2372. break;
  2373. default:
  2374. iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
  2375. "%u.\n", event);
  2376. kfree_skb(skb);
  2377. return -EINVAL;
  2378. }
  2379. /*
  2380. * this will occur if the daemon is not up, so we just warn
  2381. * the user and when the daemon is restarted it will handle it
  2382. */
  2383. rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
  2384. if (rc == -ESRCH)
  2385. iscsi_cls_session_printk(KERN_ERR, session,
  2386. "Cannot notify userspace of session "
  2387. "event %u. Check iscsi daemon\n",
  2388. event);
  2389. ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
  2390. event, rc);
  2391. return rc;
  2392. }
  2393. EXPORT_SYMBOL_GPL(iscsi_session_event);
  2394. static int
  2395. iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
  2396. struct iscsi_uevent *ev, pid_t pid,
  2397. uint32_t initial_cmdsn, uint16_t cmds_max,
  2398. uint16_t queue_depth)
  2399. {
  2400. struct iscsi_transport *transport = priv->iscsi_transport;
  2401. struct iscsi_cls_session *session;
  2402. struct Scsi_Host *shost;
  2403. session = transport->create_session(ep, cmds_max, queue_depth,
  2404. initial_cmdsn);
  2405. if (!session)
  2406. return -ENOMEM;
  2407. session->creator = pid;
  2408. shost = iscsi_session_to_shost(session);
  2409. ev->r.c_session_ret.host_no = shost->host_no;
  2410. ev->r.c_session_ret.sid = session->sid;
  2411. ISCSI_DBG_TRANS_SESSION(session,
  2412. "Completed creating transport session\n");
  2413. return 0;
  2414. }
  2415. static int
  2416. iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  2417. {
  2418. struct iscsi_cls_conn *conn;
  2419. struct iscsi_cls_session *session;
  2420. session = iscsi_session_lookup(ev->u.c_conn.sid);
  2421. if (!session) {
  2422. printk(KERN_ERR "iscsi: invalid session %d.\n",
  2423. ev->u.c_conn.sid);
  2424. return -EINVAL;
  2425. }
  2426. conn = transport->create_conn(session, ev->u.c_conn.cid);
  2427. if (!conn) {
  2428. iscsi_cls_session_printk(KERN_ERR, session,
  2429. "couldn't create a new connection.");
  2430. return -ENOMEM;
  2431. }
  2432. ev->r.c_conn_ret.sid = session->sid;
  2433. ev->r.c_conn_ret.cid = conn->cid;
  2434. ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
  2435. return 0;
  2436. }
  2437. static int
  2438. iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  2439. {
  2440. struct iscsi_cls_conn *conn;
  2441. conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
  2442. if (!conn)
  2443. return -EINVAL;
  2444. ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
  2445. if (transport->destroy_conn)
  2446. transport->destroy_conn(conn);
  2447. return 0;
  2448. }
  2449. static int
  2450. iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  2451. {
  2452. char *data = (char*)ev + sizeof(*ev);
  2453. struct iscsi_cls_conn *conn;
  2454. struct iscsi_cls_session *session;
  2455. int err = 0, value = 0;
  2456. session = iscsi_session_lookup(ev->u.set_param.sid);
  2457. conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
  2458. if (!conn || !session)
  2459. return -EINVAL;
  2460. switch (ev->u.set_param.param) {
  2461. case ISCSI_PARAM_SESS_RECOVERY_TMO:
  2462. sscanf(data, "%d", &value);
  2463. session->recovery_tmo = value;
  2464. break;
  2465. default:
  2466. err = transport->set_param(conn, ev->u.set_param.param,
  2467. data, ev->u.set_param.len);
  2468. }
  2469. return err;
  2470. }
  2471. static int iscsi_if_ep_connect(struct iscsi_transport *transport,
  2472. struct iscsi_uevent *ev, int msg_type)
  2473. {
  2474. struct iscsi_endpoint *ep;
  2475. struct sockaddr *dst_addr;
  2476. struct Scsi_Host *shost = NULL;
  2477. int non_blocking, err = 0;
  2478. if (!transport->ep_connect)
  2479. return -EINVAL;
  2480. if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
  2481. shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
  2482. if (!shost) {
  2483. printk(KERN_ERR "ep connect failed. Could not find "
  2484. "host no %u\n",
  2485. ev->u.ep_connect_through_host.host_no);
  2486. return -ENODEV;
  2487. }
  2488. non_blocking = ev->u.ep_connect_through_host.non_blocking;
  2489. } else
  2490. non_blocking = ev->u.ep_connect.non_blocking;
  2491. dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
  2492. ep = transport->ep_connect(shost, dst_addr, non_blocking);
  2493. if (IS_ERR(ep)) {
  2494. err = PTR_ERR(ep);
  2495. goto release_host;
  2496. }
  2497. ev->r.ep_connect_ret.handle = ep->id;
  2498. release_host:
  2499. if (shost)
  2500. scsi_host_put(shost);
  2501. return err;
  2502. }
  2503. static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
  2504. u64 ep_handle)
  2505. {
  2506. struct iscsi_cls_conn *conn;
  2507. struct iscsi_endpoint *ep;
  2508. if (!transport->ep_disconnect)
  2509. return -EINVAL;
  2510. ep = iscsi_lookup_endpoint(ep_handle);
  2511. if (!ep)
  2512. return -EINVAL;
  2513. conn = ep->conn;
  2514. if (conn) {
  2515. mutex_lock(&conn->ep_mutex);
  2516. conn->ep = NULL;
  2517. mutex_unlock(&conn->ep_mutex);
  2518. }
  2519. transport->ep_disconnect(ep);
  2520. return 0;
  2521. }
  2522. static int
  2523. iscsi_if_transport_ep(struct iscsi_transport *transport,
  2524. struct iscsi_uevent *ev, int msg_type)
  2525. {
  2526. struct iscsi_endpoint *ep;
  2527. int rc = 0;
  2528. switch (msg_type) {
  2529. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
  2530. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
  2531. rc = iscsi_if_ep_connect(transport, ev, msg_type);
  2532. break;
  2533. case ISCSI_UEVENT_TRANSPORT_EP_POLL:
  2534. if (!transport->ep_poll)
  2535. return -EINVAL;
  2536. ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
  2537. if (!ep)
  2538. return -EINVAL;
  2539. ev->r.retcode = transport->ep_poll(ep,
  2540. ev->u.ep_poll.timeout_ms);
  2541. break;
  2542. case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
  2543. rc = iscsi_if_ep_disconnect(transport,
  2544. ev->u.ep_disconnect.ep_handle);
  2545. break;
  2546. }
  2547. return rc;
  2548. }
  2549. static int
  2550. iscsi_tgt_dscvr(struct iscsi_transport *transport,
  2551. struct iscsi_uevent *ev)
  2552. {
  2553. struct Scsi_Host *shost;
  2554. struct sockaddr *dst_addr;
  2555. int err;
  2556. if (!transport->tgt_dscvr)
  2557. return -EINVAL;
  2558. shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
  2559. if (!shost) {
  2560. printk(KERN_ERR "target discovery could not find host no %u\n",
  2561. ev->u.tgt_dscvr.host_no);
  2562. return -ENODEV;
  2563. }
  2564. dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
  2565. err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
  2566. ev->u.tgt_dscvr.enable, dst_addr);
  2567. scsi_host_put(shost);
  2568. return err;
  2569. }
  2570. static int
  2571. iscsi_set_host_param(struct iscsi_transport *transport,
  2572. struct iscsi_uevent *ev)
  2573. {
  2574. char *data = (char*)ev + sizeof(*ev);
  2575. struct Scsi_Host *shost;
  2576. int err;
  2577. if (!transport->set_host_param)
  2578. return -ENOSYS;
  2579. shost = scsi_host_lookup(ev->u.set_host_param.host_no);
  2580. if (!shost) {
  2581. printk(KERN_ERR "set_host_param could not find host no %u\n",
  2582. ev->u.set_host_param.host_no);
  2583. return -ENODEV;
  2584. }
  2585. err = transport->set_host_param(shost, ev->u.set_host_param.param,
  2586. data, ev->u.set_host_param.len);
  2587. scsi_host_put(shost);
  2588. return err;
  2589. }
  2590. static int
  2591. iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  2592. {
  2593. struct Scsi_Host *shost;
  2594. struct iscsi_path *params;
  2595. int err;
  2596. if (!transport->set_path)
  2597. return -ENOSYS;
  2598. shost = scsi_host_lookup(ev->u.set_path.host_no);
  2599. if (!shost) {
  2600. printk(KERN_ERR "set path could not find host no %u\n",
  2601. ev->u.set_path.host_no);
  2602. return -ENODEV;
  2603. }
  2604. params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
  2605. err = transport->set_path(shost, params);
  2606. scsi_host_put(shost);
  2607. return err;
  2608. }
  2609. static int
  2610. iscsi_set_iface_params(struct iscsi_transport *transport,
  2611. struct iscsi_uevent *ev, uint32_t len)
  2612. {
  2613. char *data = (char *)ev + sizeof(*ev);
  2614. struct Scsi_Host *shost;
  2615. int err;
  2616. if (!transport->set_iface_param)
  2617. return -ENOSYS;
  2618. shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
  2619. if (!shost) {
  2620. printk(KERN_ERR "set_iface_params could not find host no %u\n",
  2621. ev->u.set_iface_params.host_no);
  2622. return -ENODEV;
  2623. }
  2624. err = transport->set_iface_param(shost, data, len);
  2625. scsi_host_put(shost);
  2626. return err;
  2627. }
  2628. static int
  2629. iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  2630. {
  2631. struct Scsi_Host *shost;
  2632. struct sockaddr *dst_addr;
  2633. int err;
  2634. if (!transport->send_ping)
  2635. return -ENOSYS;
  2636. shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
  2637. if (!shost) {
  2638. printk(KERN_ERR "iscsi_ping could not find host no %u\n",
  2639. ev->u.iscsi_ping.host_no);
  2640. return -ENODEV;
  2641. }
  2642. dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
  2643. err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
  2644. ev->u.iscsi_ping.iface_type,
  2645. ev->u.iscsi_ping.payload_size,
  2646. ev->u.iscsi_ping.pid,
  2647. dst_addr);
  2648. scsi_host_put(shost);
  2649. return err;
  2650. }
  2651. static int
  2652. iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
  2653. {
  2654. struct iscsi_uevent *ev = nlmsg_data(nlh);
  2655. struct Scsi_Host *shost = NULL;
  2656. struct iscsi_chap_rec *chap_rec;
  2657. struct iscsi_internal *priv;
  2658. struct sk_buff *skbchap;
  2659. struct nlmsghdr *nlhchap;
  2660. struct iscsi_uevent *evchap;
  2661. uint32_t chap_buf_size;
  2662. int len, err = 0;
  2663. char *buf;
  2664. if (!transport->get_chap)
  2665. return -EINVAL;
  2666. priv = iscsi_if_transport_lookup(transport);
  2667. if (!priv)
  2668. return -EINVAL;
  2669. chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
  2670. len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
  2671. shost = scsi_host_lookup(ev->u.get_chap.host_no);
  2672. if (!shost) {
  2673. printk(KERN_ERR "%s: failed. Cound not find host no %u\n",
  2674. __func__, ev->u.get_chap.host_no);
  2675. return -ENODEV;
  2676. }
  2677. do {
  2678. int actual_size;
  2679. skbchap = alloc_skb(len, GFP_KERNEL);
  2680. if (!skbchap) {
  2681. printk(KERN_ERR "can not deliver chap: OOM\n");
  2682. err = -ENOMEM;
  2683. goto exit_get_chap;
  2684. }
  2685. nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
  2686. (len - sizeof(*nlhchap)), 0);
  2687. evchap = nlmsg_data(nlhchap);
  2688. memset(evchap, 0, sizeof(*evchap));
  2689. evchap->transport_handle = iscsi_handle(transport);
  2690. evchap->type = nlh->nlmsg_type;
  2691. evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
  2692. evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
  2693. evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
  2694. buf = (char *)evchap + sizeof(*evchap);
  2695. memset(buf, 0, chap_buf_size);
  2696. err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
  2697. &evchap->u.get_chap.num_entries, buf);
  2698. actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
  2699. skb_trim(skbchap, NLMSG_ALIGN(actual_size));
  2700. nlhchap->nlmsg_len = actual_size;
  2701. err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
  2702. GFP_KERNEL);
  2703. } while (err < 0 && err != -ECONNREFUSED);
  2704. exit_get_chap:
  2705. scsi_host_put(shost);
  2706. return err;
  2707. }
  2708. static int iscsi_set_chap(struct iscsi_transport *transport,
  2709. struct iscsi_uevent *ev, uint32_t len)
  2710. {
  2711. char *data = (char *)ev + sizeof(*ev);
  2712. struct Scsi_Host *shost;
  2713. int err = 0;
  2714. if (!transport->set_chap)
  2715. return -ENOSYS;
  2716. shost = scsi_host_lookup(ev->u.set_path.host_no);
  2717. if (!shost) {
  2718. pr_err("%s could not find host no %u\n",
  2719. __func__, ev->u.set_path.host_no);
  2720. return -ENODEV;
  2721. }
  2722. err = transport->set_chap(shost, data, len);
  2723. scsi_host_put(shost);
  2724. return err;
  2725. }
  2726. static int iscsi_delete_chap(struct iscsi_transport *transport,
  2727. struct iscsi_uevent *ev)
  2728. {
  2729. struct Scsi_Host *shost;
  2730. int err = 0;
  2731. if (!transport->delete_chap)
  2732. return -ENOSYS;
  2733. shost = scsi_host_lookup(ev->u.delete_chap.host_no);
  2734. if (!shost) {
  2735. printk(KERN_ERR "%s could not find host no %u\n",
  2736. __func__, ev->u.delete_chap.host_no);
  2737. return -ENODEV;
  2738. }
  2739. err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
  2740. scsi_host_put(shost);
  2741. return err;
  2742. }
  2743. static const struct {
  2744. enum iscsi_discovery_parent_type value;
  2745. char *name;
  2746. } iscsi_discovery_parent_names[] = {
  2747. {ISCSI_DISC_PARENT_UNKNOWN, "Unknown" },
  2748. {ISCSI_DISC_PARENT_SENDTGT, "Sendtarget" },
  2749. {ISCSI_DISC_PARENT_ISNS, "isns" },
  2750. };
  2751. char *iscsi_get_discovery_parent_name(int parent_type)
  2752. {
  2753. int i;
  2754. char *state = "Unknown!";
  2755. for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
  2756. if (iscsi_discovery_parent_names[i].value & parent_type) {
  2757. state = iscsi_discovery_parent_names[i].name;
  2758. break;
  2759. }
  2760. }
  2761. return state;
  2762. }
  2763. EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
  2764. static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
  2765. struct iscsi_uevent *ev, uint32_t len)
  2766. {
  2767. char *data = (char *)ev + sizeof(*ev);
  2768. struct Scsi_Host *shost;
  2769. struct iscsi_bus_flash_session *fnode_sess;
  2770. struct iscsi_bus_flash_conn *fnode_conn;
  2771. struct device *dev;
  2772. uint32_t idx;
  2773. int err = 0;
  2774. if (!transport->set_flashnode_param) {
  2775. err = -ENOSYS;
  2776. goto exit_set_fnode;
  2777. }
  2778. shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
  2779. if (!shost) {
  2780. pr_err("%s could not find host no %u\n",
  2781. __func__, ev->u.set_flashnode.host_no);
  2782. err = -ENODEV;
  2783. goto put_host;
  2784. }
  2785. idx = ev->u.set_flashnode.flashnode_idx;
  2786. fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
  2787. if (!fnode_sess) {
  2788. pr_err("%s could not find flashnode %u for host no %u\n",
  2789. __func__, idx, ev->u.set_flashnode.host_no);
  2790. err = -ENODEV;
  2791. goto put_host;
  2792. }
  2793. dev = iscsi_find_flashnode_conn(fnode_sess);
  2794. if (!dev) {
  2795. err = -ENODEV;
  2796. goto put_sess;
  2797. }
  2798. fnode_conn = iscsi_dev_to_flash_conn(dev);
  2799. err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
  2800. put_device(dev);
  2801. put_sess:
  2802. put_device(&fnode_sess->dev);
  2803. put_host:
  2804. scsi_host_put(shost);
  2805. exit_set_fnode:
  2806. return err;
  2807. }
  2808. static int iscsi_new_flashnode(struct iscsi_transport *transport,
  2809. struct iscsi_uevent *ev, uint32_t len)
  2810. {
  2811. char *data = (char *)ev + sizeof(*ev);
  2812. struct Scsi_Host *shost;
  2813. int index;
  2814. int err = 0;
  2815. if (!transport->new_flashnode) {
  2816. err = -ENOSYS;
  2817. goto exit_new_fnode;
  2818. }
  2819. shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
  2820. if (!shost) {
  2821. pr_err("%s could not find host no %u\n",
  2822. __func__, ev->u.new_flashnode.host_no);
  2823. err = -ENODEV;
  2824. goto put_host;
  2825. }
  2826. index = transport->new_flashnode(shost, data, len);
  2827. if (index >= 0)
  2828. ev->r.new_flashnode_ret.flashnode_idx = index;
  2829. else
  2830. err = -EIO;
  2831. put_host:
  2832. scsi_host_put(shost);
  2833. exit_new_fnode:
  2834. return err;
  2835. }
  2836. static int iscsi_del_flashnode(struct iscsi_transport *transport,
  2837. struct iscsi_uevent *ev)
  2838. {
  2839. struct Scsi_Host *shost;
  2840. struct iscsi_bus_flash_session *fnode_sess;
  2841. uint32_t idx;
  2842. int err = 0;
  2843. if (!transport->del_flashnode) {
  2844. err = -ENOSYS;
  2845. goto exit_del_fnode;
  2846. }
  2847. shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
  2848. if (!shost) {
  2849. pr_err("%s could not find host no %u\n",
  2850. __func__, ev->u.del_flashnode.host_no);
  2851. err = -ENODEV;
  2852. goto put_host;
  2853. }
  2854. idx = ev->u.del_flashnode.flashnode_idx;
  2855. fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
  2856. if (!fnode_sess) {
  2857. pr_err("%s could not find flashnode %u for host no %u\n",
  2858. __func__, idx, ev->u.del_flashnode.host_no);
  2859. err = -ENODEV;
  2860. goto put_host;
  2861. }
  2862. err = transport->del_flashnode(fnode_sess);
  2863. put_device(&fnode_sess->dev);
  2864. put_host:
  2865. scsi_host_put(shost);
  2866. exit_del_fnode:
  2867. return err;
  2868. }
  2869. static int iscsi_login_flashnode(struct iscsi_transport *transport,
  2870. struct iscsi_uevent *ev)
  2871. {
  2872. struct Scsi_Host *shost;
  2873. struct iscsi_bus_flash_session *fnode_sess;
  2874. struct iscsi_bus_flash_conn *fnode_conn;
  2875. struct device *dev;
  2876. uint32_t idx;
  2877. int err = 0;
  2878. if (!transport->login_flashnode) {
  2879. err = -ENOSYS;
  2880. goto exit_login_fnode;
  2881. }
  2882. shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
  2883. if (!shost) {
  2884. pr_err("%s could not find host no %u\n",
  2885. __func__, ev->u.login_flashnode.host_no);
  2886. err = -ENODEV;
  2887. goto put_host;
  2888. }
  2889. idx = ev->u.login_flashnode.flashnode_idx;
  2890. fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
  2891. if (!fnode_sess) {
  2892. pr_err("%s could not find flashnode %u for host no %u\n",
  2893. __func__, idx, ev->u.login_flashnode.host_no);
  2894. err = -ENODEV;
  2895. goto put_host;
  2896. }
  2897. dev = iscsi_find_flashnode_conn(fnode_sess);
  2898. if (!dev) {
  2899. err = -ENODEV;
  2900. goto put_sess;
  2901. }
  2902. fnode_conn = iscsi_dev_to_flash_conn(dev);
  2903. err = transport->login_flashnode(fnode_sess, fnode_conn);
  2904. put_device(dev);
  2905. put_sess:
  2906. put_device(&fnode_sess->dev);
  2907. put_host:
  2908. scsi_host_put(shost);
  2909. exit_login_fnode:
  2910. return err;
  2911. }
  2912. static int iscsi_logout_flashnode(struct iscsi_transport *transport,
  2913. struct iscsi_uevent *ev)
  2914. {
  2915. struct Scsi_Host *shost;
  2916. struct iscsi_bus_flash_session *fnode_sess;
  2917. struct iscsi_bus_flash_conn *fnode_conn;
  2918. struct device *dev;
  2919. uint32_t idx;
  2920. int err = 0;
  2921. if (!transport->logout_flashnode) {
  2922. err = -ENOSYS;
  2923. goto exit_logout_fnode;
  2924. }
  2925. shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
  2926. if (!shost) {
  2927. pr_err("%s could not find host no %u\n",
  2928. __func__, ev->u.logout_flashnode.host_no);
  2929. err = -ENODEV;
  2930. goto put_host;
  2931. }
  2932. idx = ev->u.logout_flashnode.flashnode_idx;
  2933. fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
  2934. if (!fnode_sess) {
  2935. pr_err("%s could not find flashnode %u for host no %u\n",
  2936. __func__, idx, ev->u.logout_flashnode.host_no);
  2937. err = -ENODEV;
  2938. goto put_host;
  2939. }
  2940. dev = iscsi_find_flashnode_conn(fnode_sess);
  2941. if (!dev) {
  2942. err = -ENODEV;
  2943. goto put_sess;
  2944. }
  2945. fnode_conn = iscsi_dev_to_flash_conn(dev);
  2946. err = transport->logout_flashnode(fnode_sess, fnode_conn);
  2947. put_device(dev);
  2948. put_sess:
  2949. put_device(&fnode_sess->dev);
  2950. put_host:
  2951. scsi_host_put(shost);
  2952. exit_logout_fnode:
  2953. return err;
  2954. }
  2955. static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
  2956. struct iscsi_uevent *ev)
  2957. {
  2958. struct Scsi_Host *shost;
  2959. struct iscsi_cls_session *session;
  2960. int err = 0;
  2961. if (!transport->logout_flashnode_sid) {
  2962. err = -ENOSYS;
  2963. goto exit_logout_sid;
  2964. }
  2965. shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
  2966. if (!shost) {
  2967. pr_err("%s could not find host no %u\n",
  2968. __func__, ev->u.logout_flashnode.host_no);
  2969. err = -ENODEV;
  2970. goto put_host;
  2971. }
  2972. session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
  2973. if (!session) {
  2974. pr_err("%s could not find session id %u\n",
  2975. __func__, ev->u.logout_flashnode_sid.sid);
  2976. err = -EINVAL;
  2977. goto put_host;
  2978. }
  2979. err = transport->logout_flashnode_sid(session);
  2980. put_host:
  2981. scsi_host_put(shost);
  2982. exit_logout_sid:
  2983. return err;
  2984. }
  2985. static int
  2986. iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
  2987. {
  2988. struct iscsi_uevent *ev = nlmsg_data(nlh);
  2989. struct Scsi_Host *shost = NULL;
  2990. struct iscsi_internal *priv;
  2991. struct sk_buff *skbhost_stats;
  2992. struct nlmsghdr *nlhhost_stats;
  2993. struct iscsi_uevent *evhost_stats;
  2994. int host_stats_size = 0;
  2995. int len, err = 0;
  2996. char *buf;
  2997. if (!transport->get_host_stats)
  2998. return -ENOSYS;
  2999. priv = iscsi_if_transport_lookup(transport);
  3000. if (!priv)
  3001. return -EINVAL;
  3002. host_stats_size = sizeof(struct iscsi_offload_host_stats);
  3003. len = nlmsg_total_size(sizeof(*ev) + host_stats_size);
  3004. shost = scsi_host_lookup(ev->u.get_host_stats.host_no);
  3005. if (!shost) {
  3006. pr_err("%s: failed. Cound not find host no %u\n",
  3007. __func__, ev->u.get_host_stats.host_no);
  3008. return -ENODEV;
  3009. }
  3010. do {
  3011. int actual_size;
  3012. skbhost_stats = alloc_skb(len, GFP_KERNEL);
  3013. if (!skbhost_stats) {
  3014. pr_err("cannot deliver host stats: OOM\n");
  3015. err = -ENOMEM;
  3016. goto exit_host_stats;
  3017. }
  3018. nlhhost_stats = __nlmsg_put(skbhost_stats, 0, 0, 0,
  3019. (len - sizeof(*nlhhost_stats)), 0);
  3020. evhost_stats = nlmsg_data(nlhhost_stats);
  3021. memset(evhost_stats, 0, sizeof(*evhost_stats));
  3022. evhost_stats->transport_handle = iscsi_handle(transport);
  3023. evhost_stats->type = nlh->nlmsg_type;
  3024. evhost_stats->u.get_host_stats.host_no =
  3025. ev->u.get_host_stats.host_no;
  3026. buf = (char *)evhost_stats + sizeof(*evhost_stats);
  3027. memset(buf, 0, host_stats_size);
  3028. err = transport->get_host_stats(shost, buf, host_stats_size);
  3029. if (err) {
  3030. kfree_skb(skbhost_stats);
  3031. goto exit_host_stats;
  3032. }
  3033. actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
  3034. skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
  3035. nlhhost_stats->nlmsg_len = actual_size;
  3036. err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
  3037. GFP_KERNEL);
  3038. } while (err < 0 && err != -ECONNREFUSED);
  3039. exit_host_stats:
  3040. scsi_host_put(shost);
  3041. return err;
  3042. }
  3043. static int
  3044. iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
  3045. {
  3046. int err = 0;
  3047. struct iscsi_uevent *ev = nlmsg_data(nlh);
  3048. struct iscsi_transport *transport = NULL;
  3049. struct iscsi_internal *priv;
  3050. struct iscsi_cls_session *session;
  3051. struct iscsi_cls_conn *conn;
  3052. struct iscsi_endpoint *ep = NULL;
  3053. if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
  3054. *group = ISCSI_NL_GRP_UIP;
  3055. else
  3056. *group = ISCSI_NL_GRP_ISCSID;
  3057. priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
  3058. if (!priv)
  3059. return -EINVAL;
  3060. transport = priv->iscsi_transport;
  3061. if (!try_module_get(transport->owner))
  3062. return -EINVAL;
  3063. switch (nlh->nlmsg_type) {
  3064. case ISCSI_UEVENT_CREATE_SESSION:
  3065. err = iscsi_if_create_session(priv, ep, ev,
  3066. NETLINK_CB(skb).portid,
  3067. ev->u.c_session.initial_cmdsn,
  3068. ev->u.c_session.cmds_max,
  3069. ev->u.c_session.queue_depth);
  3070. break;
  3071. case ISCSI_UEVENT_CREATE_BOUND_SESSION:
  3072. ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
  3073. if (!ep) {
  3074. err = -EINVAL;
  3075. break;
  3076. }
  3077. err = iscsi_if_create_session(priv, ep, ev,
  3078. NETLINK_CB(skb).portid,
  3079. ev->u.c_bound_session.initial_cmdsn,
  3080. ev->u.c_bound_session.cmds_max,
  3081. ev->u.c_bound_session.queue_depth);
  3082. break;
  3083. case ISCSI_UEVENT_DESTROY_SESSION:
  3084. session = iscsi_session_lookup(ev->u.d_session.sid);
  3085. if (session)
  3086. transport->destroy_session(session);
  3087. else
  3088. err = -EINVAL;
  3089. break;
  3090. case ISCSI_UEVENT_UNBIND_SESSION:
  3091. session = iscsi_session_lookup(ev->u.d_session.sid);
  3092. if (session)
  3093. scsi_queue_work(iscsi_session_to_shost(session),
  3094. &session->unbind_work);
  3095. else
  3096. err = -EINVAL;
  3097. break;
  3098. case ISCSI_UEVENT_CREATE_CONN:
  3099. err = iscsi_if_create_conn(transport, ev);
  3100. break;
  3101. case ISCSI_UEVENT_DESTROY_CONN:
  3102. err = iscsi_if_destroy_conn(transport, ev);
  3103. break;
  3104. case ISCSI_UEVENT_BIND_CONN:
  3105. session = iscsi_session_lookup(ev->u.b_conn.sid);
  3106. conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
  3107. if (conn && conn->ep)
  3108. iscsi_if_ep_disconnect(transport, conn->ep->id);
  3109. if (!session || !conn) {
  3110. err = -EINVAL;
  3111. break;
  3112. }
  3113. ev->r.retcode = transport->bind_conn(session, conn,
  3114. ev->u.b_conn.transport_eph,
  3115. ev->u.b_conn.is_leading);
  3116. if (ev->r.retcode || !transport->ep_connect)
  3117. break;
  3118. ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
  3119. if (ep) {
  3120. ep->conn = conn;
  3121. mutex_lock(&conn->ep_mutex);
  3122. conn->ep = ep;
  3123. mutex_unlock(&conn->ep_mutex);
  3124. } else
  3125. iscsi_cls_conn_printk(KERN_ERR, conn,
  3126. "Could not set ep conn "
  3127. "binding\n");
  3128. break;
  3129. case ISCSI_UEVENT_SET_PARAM:
  3130. err = iscsi_set_param(transport, ev);
  3131. break;
  3132. case ISCSI_UEVENT_START_CONN:
  3133. conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
  3134. if (conn)
  3135. ev->r.retcode = transport->start_conn(conn);
  3136. else
  3137. err = -EINVAL;
  3138. break;
  3139. case ISCSI_UEVENT_STOP_CONN:
  3140. conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
  3141. if (conn)
  3142. transport->stop_conn(conn, ev->u.stop_conn.flag);
  3143. else
  3144. err = -EINVAL;
  3145. break;
  3146. case ISCSI_UEVENT_SEND_PDU:
  3147. conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
  3148. if (conn)
  3149. ev->r.retcode = transport->send_pdu(conn,
  3150. (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
  3151. (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
  3152. ev->u.send_pdu.data_size);
  3153. else
  3154. err = -EINVAL;
  3155. break;
  3156. case ISCSI_UEVENT_GET_STATS:
  3157. err = iscsi_if_get_stats(transport, nlh);
  3158. break;
  3159. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
  3160. case ISCSI_UEVENT_TRANSPORT_EP_POLL:
  3161. case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
  3162. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
  3163. err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
  3164. break;
  3165. case ISCSI_UEVENT_TGT_DSCVR:
  3166. err = iscsi_tgt_dscvr(transport, ev);
  3167. break;
  3168. case ISCSI_UEVENT_SET_HOST_PARAM:
  3169. err = iscsi_set_host_param(transport, ev);
  3170. break;
  3171. case ISCSI_UEVENT_PATH_UPDATE:
  3172. err = iscsi_set_path(transport, ev);
  3173. break;
  3174. case ISCSI_UEVENT_SET_IFACE_PARAMS:
  3175. err = iscsi_set_iface_params(transport, ev,
  3176. nlmsg_attrlen(nlh, sizeof(*ev)));
  3177. break;
  3178. case ISCSI_UEVENT_PING:
  3179. err = iscsi_send_ping(transport, ev);
  3180. break;
  3181. case ISCSI_UEVENT_GET_CHAP:
  3182. err = iscsi_get_chap(transport, nlh);
  3183. break;
  3184. case ISCSI_UEVENT_DELETE_CHAP:
  3185. err = iscsi_delete_chap(transport, ev);
  3186. break;
  3187. case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
  3188. err = iscsi_set_flashnode_param(transport, ev,
  3189. nlmsg_attrlen(nlh,
  3190. sizeof(*ev)));
  3191. break;
  3192. case ISCSI_UEVENT_NEW_FLASHNODE:
  3193. err = iscsi_new_flashnode(transport, ev,
  3194. nlmsg_attrlen(nlh, sizeof(*ev)));
  3195. break;
  3196. case ISCSI_UEVENT_DEL_FLASHNODE:
  3197. err = iscsi_del_flashnode(transport, ev);
  3198. break;
  3199. case ISCSI_UEVENT_LOGIN_FLASHNODE:
  3200. err = iscsi_login_flashnode(transport, ev);
  3201. break;
  3202. case ISCSI_UEVENT_LOGOUT_FLASHNODE:
  3203. err = iscsi_logout_flashnode(transport, ev);
  3204. break;
  3205. case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
  3206. err = iscsi_logout_flashnode_sid(transport, ev);
  3207. break;
  3208. case ISCSI_UEVENT_SET_CHAP:
  3209. err = iscsi_set_chap(transport, ev,
  3210. nlmsg_attrlen(nlh, sizeof(*ev)));
  3211. break;
  3212. case ISCSI_UEVENT_GET_HOST_STATS:
  3213. err = iscsi_get_host_stats(transport, nlh);
  3214. break;
  3215. default:
  3216. err = -ENOSYS;
  3217. break;
  3218. }
  3219. module_put(transport->owner);
  3220. return err;
  3221. }
  3222. /*
  3223. * Get message from skb. Each message is processed by iscsi_if_recv_msg.
  3224. * Malformed skbs with wrong lengths or invalid creds are not processed.
  3225. */
  3226. static void
  3227. iscsi_if_rx(struct sk_buff *skb)
  3228. {
  3229. mutex_lock(&rx_queue_mutex);
  3230. while (skb->len >= NLMSG_HDRLEN) {
  3231. int err;
  3232. uint32_t rlen;
  3233. struct nlmsghdr *nlh;
  3234. struct iscsi_uevent *ev;
  3235. uint32_t group;
  3236. nlh = nlmsg_hdr(skb);
  3237. if (nlh->nlmsg_len < sizeof(*nlh) ||
  3238. skb->len < nlh->nlmsg_len) {
  3239. break;
  3240. }
  3241. ev = nlmsg_data(nlh);
  3242. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  3243. if (rlen > skb->len)
  3244. rlen = skb->len;
  3245. err = iscsi_if_recv_msg(skb, nlh, &group);
  3246. if (err) {
  3247. ev->type = ISCSI_KEVENT_IF_ERROR;
  3248. ev->iferror = err;
  3249. }
  3250. do {
  3251. /*
  3252. * special case for GET_STATS:
  3253. * on success - sending reply and stats from
  3254. * inside of if_recv_msg(),
  3255. * on error - fall through.
  3256. */
  3257. if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
  3258. break;
  3259. if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
  3260. break;
  3261. err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
  3262. nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
  3263. } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
  3264. skb_pull(skb, rlen);
  3265. }
  3266. mutex_unlock(&rx_queue_mutex);
  3267. }
  3268. #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
  3269. struct device_attribute dev_attr_##_prefix##_##_name = \
  3270. __ATTR(_name,_mode,_show,_store)
  3271. /*
  3272. * iSCSI connection attrs
  3273. */
  3274. #define iscsi_conn_attr_show(param) \
  3275. static ssize_t \
  3276. show_conn_param_##param(struct device *dev, \
  3277. struct device_attribute *attr, char *buf) \
  3278. { \
  3279. struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
  3280. struct iscsi_transport *t = conn->transport; \
  3281. return t->get_conn_param(conn, param, buf); \
  3282. }
  3283. #define iscsi_conn_attr(field, param) \
  3284. iscsi_conn_attr_show(param) \
  3285. static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
  3286. NULL);
  3287. iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
  3288. iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
  3289. iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
  3290. iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
  3291. iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
  3292. iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
  3293. iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
  3294. iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
  3295. iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
  3296. iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
  3297. iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
  3298. iscsi_conn_attr(local_port, ISCSI_PARAM_LOCAL_PORT);
  3299. iscsi_conn_attr(statsn, ISCSI_PARAM_STATSN);
  3300. iscsi_conn_attr(keepalive_tmo, ISCSI_PARAM_KEEPALIVE_TMO);
  3301. iscsi_conn_attr(max_segment_size, ISCSI_PARAM_MAX_SEGMENT_SIZE);
  3302. iscsi_conn_attr(tcp_timestamp_stat, ISCSI_PARAM_TCP_TIMESTAMP_STAT);
  3303. iscsi_conn_attr(tcp_wsf_disable, ISCSI_PARAM_TCP_WSF_DISABLE);
  3304. iscsi_conn_attr(tcp_nagle_disable, ISCSI_PARAM_TCP_NAGLE_DISABLE);
  3305. iscsi_conn_attr(tcp_timer_scale, ISCSI_PARAM_TCP_TIMER_SCALE);
  3306. iscsi_conn_attr(tcp_timestamp_enable, ISCSI_PARAM_TCP_TIMESTAMP_EN);
  3307. iscsi_conn_attr(fragment_disable, ISCSI_PARAM_IP_FRAGMENT_DISABLE);
  3308. iscsi_conn_attr(ipv4_tos, ISCSI_PARAM_IPV4_TOS);
  3309. iscsi_conn_attr(ipv6_traffic_class, ISCSI_PARAM_IPV6_TC);
  3310. iscsi_conn_attr(ipv6_flow_label, ISCSI_PARAM_IPV6_FLOW_LABEL);
  3311. iscsi_conn_attr(is_fw_assigned_ipv6, ISCSI_PARAM_IS_FW_ASSIGNED_IPV6);
  3312. iscsi_conn_attr(tcp_xmit_wsf, ISCSI_PARAM_TCP_XMIT_WSF);
  3313. iscsi_conn_attr(tcp_recv_wsf, ISCSI_PARAM_TCP_RECV_WSF);
  3314. iscsi_conn_attr(local_ipaddr, ISCSI_PARAM_LOCAL_IPADDR);
  3315. #define iscsi_conn_ep_attr_show(param) \
  3316. static ssize_t show_conn_ep_param_##param(struct device *dev, \
  3317. struct device_attribute *attr,\
  3318. char *buf) \
  3319. { \
  3320. struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
  3321. struct iscsi_transport *t = conn->transport; \
  3322. struct iscsi_endpoint *ep; \
  3323. ssize_t rc; \
  3324. \
  3325. /* \
  3326. * Need to make sure ep_disconnect does not free the LLD's \
  3327. * interconnect resources while we are trying to read them. \
  3328. */ \
  3329. mutex_lock(&conn->ep_mutex); \
  3330. ep = conn->ep; \
  3331. if (!ep && t->ep_connect) { \
  3332. mutex_unlock(&conn->ep_mutex); \
  3333. return -ENOTCONN; \
  3334. } \
  3335. \
  3336. if (ep) \
  3337. rc = t->get_ep_param(ep, param, buf); \
  3338. else \
  3339. rc = t->get_conn_param(conn, param, buf); \
  3340. mutex_unlock(&conn->ep_mutex); \
  3341. return rc; \
  3342. }
  3343. #define iscsi_conn_ep_attr(field, param) \
  3344. iscsi_conn_ep_attr_show(param) \
  3345. static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
  3346. show_conn_ep_param_##param, NULL);
  3347. iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
  3348. iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
  3349. static struct attribute *iscsi_conn_attrs[] = {
  3350. &dev_attr_conn_max_recv_dlength.attr,
  3351. &dev_attr_conn_max_xmit_dlength.attr,
  3352. &dev_attr_conn_header_digest.attr,
  3353. &dev_attr_conn_data_digest.attr,
  3354. &dev_attr_conn_ifmarker.attr,
  3355. &dev_attr_conn_ofmarker.attr,
  3356. &dev_attr_conn_address.attr,
  3357. &dev_attr_conn_port.attr,
  3358. &dev_attr_conn_exp_statsn.attr,
  3359. &dev_attr_conn_persistent_address.attr,
  3360. &dev_attr_conn_persistent_port.attr,
  3361. &dev_attr_conn_ping_tmo.attr,
  3362. &dev_attr_conn_recv_tmo.attr,
  3363. &dev_attr_conn_local_port.attr,
  3364. &dev_attr_conn_statsn.attr,
  3365. &dev_attr_conn_keepalive_tmo.attr,
  3366. &dev_attr_conn_max_segment_size.attr,
  3367. &dev_attr_conn_tcp_timestamp_stat.attr,
  3368. &dev_attr_conn_tcp_wsf_disable.attr,
  3369. &dev_attr_conn_tcp_nagle_disable.attr,
  3370. &dev_attr_conn_tcp_timer_scale.attr,
  3371. &dev_attr_conn_tcp_timestamp_enable.attr,
  3372. &dev_attr_conn_fragment_disable.attr,
  3373. &dev_attr_conn_ipv4_tos.attr,
  3374. &dev_attr_conn_ipv6_traffic_class.attr,
  3375. &dev_attr_conn_ipv6_flow_label.attr,
  3376. &dev_attr_conn_is_fw_assigned_ipv6.attr,
  3377. &dev_attr_conn_tcp_xmit_wsf.attr,
  3378. &dev_attr_conn_tcp_recv_wsf.attr,
  3379. &dev_attr_conn_local_ipaddr.attr,
  3380. NULL,
  3381. };
  3382. static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
  3383. struct attribute *attr, int i)
  3384. {
  3385. struct device *cdev = container_of(kobj, struct device, kobj);
  3386. struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
  3387. struct iscsi_transport *t = conn->transport;
  3388. int param;
  3389. if (attr == &dev_attr_conn_max_recv_dlength.attr)
  3390. param = ISCSI_PARAM_MAX_RECV_DLENGTH;
  3391. else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
  3392. param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
  3393. else if (attr == &dev_attr_conn_header_digest.attr)
  3394. param = ISCSI_PARAM_HDRDGST_EN;
  3395. else if (attr == &dev_attr_conn_data_digest.attr)
  3396. param = ISCSI_PARAM_DATADGST_EN;
  3397. else if (attr == &dev_attr_conn_ifmarker.attr)
  3398. param = ISCSI_PARAM_IFMARKER_EN;
  3399. else if (attr == &dev_attr_conn_ofmarker.attr)
  3400. param = ISCSI_PARAM_OFMARKER_EN;
  3401. else if (attr == &dev_attr_conn_address.attr)
  3402. param = ISCSI_PARAM_CONN_ADDRESS;
  3403. else if (attr == &dev_attr_conn_port.attr)
  3404. param = ISCSI_PARAM_CONN_PORT;
  3405. else if (attr == &dev_attr_conn_exp_statsn.attr)
  3406. param = ISCSI_PARAM_EXP_STATSN;
  3407. else if (attr == &dev_attr_conn_persistent_address.attr)
  3408. param = ISCSI_PARAM_PERSISTENT_ADDRESS;
  3409. else if (attr == &dev_attr_conn_persistent_port.attr)
  3410. param = ISCSI_PARAM_PERSISTENT_PORT;
  3411. else if (attr == &dev_attr_conn_ping_tmo.attr)
  3412. param = ISCSI_PARAM_PING_TMO;
  3413. else if (attr == &dev_attr_conn_recv_tmo.attr)
  3414. param = ISCSI_PARAM_RECV_TMO;
  3415. else if (attr == &dev_attr_conn_local_port.attr)
  3416. param = ISCSI_PARAM_LOCAL_PORT;
  3417. else if (attr == &dev_attr_conn_statsn.attr)
  3418. param = ISCSI_PARAM_STATSN;
  3419. else if (attr == &dev_attr_conn_keepalive_tmo.attr)
  3420. param = ISCSI_PARAM_KEEPALIVE_TMO;
  3421. else if (attr == &dev_attr_conn_max_segment_size.attr)
  3422. param = ISCSI_PARAM_MAX_SEGMENT_SIZE;
  3423. else if (attr == &dev_attr_conn_tcp_timestamp_stat.attr)
  3424. param = ISCSI_PARAM_TCP_TIMESTAMP_STAT;
  3425. else if (attr == &dev_attr_conn_tcp_wsf_disable.attr)
  3426. param = ISCSI_PARAM_TCP_WSF_DISABLE;
  3427. else if (attr == &dev_attr_conn_tcp_nagle_disable.attr)
  3428. param = ISCSI_PARAM_TCP_NAGLE_DISABLE;
  3429. else if (attr == &dev_attr_conn_tcp_timer_scale.attr)
  3430. param = ISCSI_PARAM_TCP_TIMER_SCALE;
  3431. else if (attr == &dev_attr_conn_tcp_timestamp_enable.attr)
  3432. param = ISCSI_PARAM_TCP_TIMESTAMP_EN;
  3433. else if (attr == &dev_attr_conn_fragment_disable.attr)
  3434. param = ISCSI_PARAM_IP_FRAGMENT_DISABLE;
  3435. else if (attr == &dev_attr_conn_ipv4_tos.attr)
  3436. param = ISCSI_PARAM_IPV4_TOS;
  3437. else if (attr == &dev_attr_conn_ipv6_traffic_class.attr)
  3438. param = ISCSI_PARAM_IPV6_TC;
  3439. else if (attr == &dev_attr_conn_ipv6_flow_label.attr)
  3440. param = ISCSI_PARAM_IPV6_FLOW_LABEL;
  3441. else if (attr == &dev_attr_conn_is_fw_assigned_ipv6.attr)
  3442. param = ISCSI_PARAM_IS_FW_ASSIGNED_IPV6;
  3443. else if (attr == &dev_attr_conn_tcp_xmit_wsf.attr)
  3444. param = ISCSI_PARAM_TCP_XMIT_WSF;
  3445. else if (attr == &dev_attr_conn_tcp_recv_wsf.attr)
  3446. param = ISCSI_PARAM_TCP_RECV_WSF;
  3447. else if (attr == &dev_attr_conn_local_ipaddr.attr)
  3448. param = ISCSI_PARAM_LOCAL_IPADDR;
  3449. else {
  3450. WARN_ONCE(1, "Invalid conn attr");
  3451. return 0;
  3452. }
  3453. return t->attr_is_visible(ISCSI_PARAM, param);
  3454. }
  3455. static struct attribute_group iscsi_conn_group = {
  3456. .attrs = iscsi_conn_attrs,
  3457. .is_visible = iscsi_conn_attr_is_visible,
  3458. };
  3459. /*
  3460. * iSCSI session attrs
  3461. */
  3462. #define iscsi_session_attr_show(param, perm) \
  3463. static ssize_t \
  3464. show_session_param_##param(struct device *dev, \
  3465. struct device_attribute *attr, char *buf) \
  3466. { \
  3467. struct iscsi_cls_session *session = \
  3468. iscsi_dev_to_session(dev->parent); \
  3469. struct iscsi_transport *t = session->transport; \
  3470. \
  3471. if (perm && !capable(CAP_SYS_ADMIN)) \
  3472. return -EACCES; \
  3473. return t->get_session_param(session, param, buf); \
  3474. }
  3475. #define iscsi_session_attr(field, param, perm) \
  3476. iscsi_session_attr_show(param, perm) \
  3477. static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
  3478. NULL);
  3479. iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
  3480. iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
  3481. iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
  3482. iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
  3483. iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
  3484. iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
  3485. iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
  3486. iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
  3487. iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
  3488. iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
  3489. iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
  3490. iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
  3491. iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
  3492. iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
  3493. iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
  3494. iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
  3495. iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
  3496. iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
  3497. iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
  3498. iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
  3499. iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
  3500. iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
  3501. iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
  3502. iscsi_session_attr(boot_root, ISCSI_PARAM_BOOT_ROOT, 0);
  3503. iscsi_session_attr(boot_nic, ISCSI_PARAM_BOOT_NIC, 0);
  3504. iscsi_session_attr(boot_target, ISCSI_PARAM_BOOT_TARGET, 0);
  3505. iscsi_session_attr(auto_snd_tgt_disable, ISCSI_PARAM_AUTO_SND_TGT_DISABLE, 0);
  3506. iscsi_session_attr(discovery_session, ISCSI_PARAM_DISCOVERY_SESS, 0);
  3507. iscsi_session_attr(portal_type, ISCSI_PARAM_PORTAL_TYPE, 0);
  3508. iscsi_session_attr(chap_auth, ISCSI_PARAM_CHAP_AUTH_EN, 0);
  3509. iscsi_session_attr(discovery_logout, ISCSI_PARAM_DISCOVERY_LOGOUT_EN, 0);
  3510. iscsi_session_attr(bidi_chap, ISCSI_PARAM_BIDI_CHAP_EN, 0);
  3511. iscsi_session_attr(discovery_auth_optional,
  3512. ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL, 0);
  3513. iscsi_session_attr(def_time2wait, ISCSI_PARAM_DEF_TIME2WAIT, 0);
  3514. iscsi_session_attr(def_time2retain, ISCSI_PARAM_DEF_TIME2RETAIN, 0);
  3515. iscsi_session_attr(isid, ISCSI_PARAM_ISID, 0);
  3516. iscsi_session_attr(tsid, ISCSI_PARAM_TSID, 0);
  3517. iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0);
  3518. iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0);
  3519. iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0);
  3520. static ssize_t
  3521. show_priv_session_state(struct device *dev, struct device_attribute *attr,
  3522. char *buf)
  3523. {
  3524. struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
  3525. return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
  3526. }
  3527. static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
  3528. NULL);
  3529. static ssize_t
  3530. show_priv_session_creator(struct device *dev, struct device_attribute *attr,
  3531. char *buf)
  3532. {
  3533. struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
  3534. return sprintf(buf, "%d\n", session->creator);
  3535. }
  3536. static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
  3537. NULL);
  3538. static ssize_t
  3539. show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
  3540. char *buf)
  3541. {
  3542. struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
  3543. return sprintf(buf, "%d\n", session->target_id);
  3544. }
  3545. static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
  3546. show_priv_session_target_id, NULL);
  3547. #define iscsi_priv_session_attr_show(field, format) \
  3548. static ssize_t \
  3549. show_priv_session_##field(struct device *dev, \
  3550. struct device_attribute *attr, char *buf) \
  3551. { \
  3552. struct iscsi_cls_session *session = \
  3553. iscsi_dev_to_session(dev->parent); \
  3554. if (session->field == -1) \
  3555. return sprintf(buf, "off\n"); \
  3556. return sprintf(buf, format"\n", session->field); \
  3557. }
  3558. #define iscsi_priv_session_attr_store(field) \
  3559. static ssize_t \
  3560. store_priv_session_##field(struct device *dev, \
  3561. struct device_attribute *attr, \
  3562. const char *buf, size_t count) \
  3563. { \
  3564. int val; \
  3565. char *cp; \
  3566. struct iscsi_cls_session *session = \
  3567. iscsi_dev_to_session(dev->parent); \
  3568. if ((session->state == ISCSI_SESSION_FREE) || \
  3569. (session->state == ISCSI_SESSION_FAILED)) \
  3570. return -EBUSY; \
  3571. if (strncmp(buf, "off", 3) == 0) \
  3572. session->field = -1; \
  3573. else { \
  3574. val = simple_strtoul(buf, &cp, 0); \
  3575. if (*cp != '\0' && *cp != '\n') \
  3576. return -EINVAL; \
  3577. session->field = val; \
  3578. } \
  3579. return count; \
  3580. }
  3581. #define iscsi_priv_session_rw_attr(field, format) \
  3582. iscsi_priv_session_attr_show(field, format) \
  3583. iscsi_priv_session_attr_store(field) \
  3584. static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
  3585. show_priv_session_##field, \
  3586. store_priv_session_##field)
  3587. iscsi_priv_session_rw_attr(recovery_tmo, "%d");
  3588. static struct attribute *iscsi_session_attrs[] = {
  3589. &dev_attr_sess_initial_r2t.attr,
  3590. &dev_attr_sess_max_outstanding_r2t.attr,
  3591. &dev_attr_sess_immediate_data.attr,
  3592. &dev_attr_sess_first_burst_len.attr,
  3593. &dev_attr_sess_max_burst_len.attr,
  3594. &dev_attr_sess_data_pdu_in_order.attr,
  3595. &dev_attr_sess_data_seq_in_order.attr,
  3596. &dev_attr_sess_erl.attr,
  3597. &dev_attr_sess_targetname.attr,
  3598. &dev_attr_sess_tpgt.attr,
  3599. &dev_attr_sess_password.attr,
  3600. &dev_attr_sess_password_in.attr,
  3601. &dev_attr_sess_username.attr,
  3602. &dev_attr_sess_username_in.attr,
  3603. &dev_attr_sess_fast_abort.attr,
  3604. &dev_attr_sess_abort_tmo.attr,
  3605. &dev_attr_sess_lu_reset_tmo.attr,
  3606. &dev_attr_sess_tgt_reset_tmo.attr,
  3607. &dev_attr_sess_ifacename.attr,
  3608. &dev_attr_sess_initiatorname.attr,
  3609. &dev_attr_sess_targetalias.attr,
  3610. &dev_attr_sess_boot_root.attr,
  3611. &dev_attr_sess_boot_nic.attr,
  3612. &dev_attr_sess_boot_target.attr,
  3613. &dev_attr_priv_sess_recovery_tmo.attr,
  3614. &dev_attr_priv_sess_state.attr,
  3615. &dev_attr_priv_sess_creator.attr,
  3616. &dev_attr_sess_chap_out_idx.attr,
  3617. &dev_attr_sess_chap_in_idx.attr,
  3618. &dev_attr_priv_sess_target_id.attr,
  3619. &dev_attr_sess_auto_snd_tgt_disable.attr,
  3620. &dev_attr_sess_discovery_session.attr,
  3621. &dev_attr_sess_portal_type.attr,
  3622. &dev_attr_sess_chap_auth.attr,
  3623. &dev_attr_sess_discovery_logout.attr,
  3624. &dev_attr_sess_bidi_chap.attr,
  3625. &dev_attr_sess_discovery_auth_optional.attr,
  3626. &dev_attr_sess_def_time2wait.attr,
  3627. &dev_attr_sess_def_time2retain.attr,
  3628. &dev_attr_sess_isid.attr,
  3629. &dev_attr_sess_tsid.attr,
  3630. &dev_attr_sess_def_taskmgmt_tmo.attr,
  3631. &dev_attr_sess_discovery_parent_idx.attr,
  3632. &dev_attr_sess_discovery_parent_type.attr,
  3633. NULL,
  3634. };
  3635. static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
  3636. struct attribute *attr, int i)
  3637. {
  3638. struct device *cdev = container_of(kobj, struct device, kobj);
  3639. struct iscsi_cls_session *session = transport_class_to_session(cdev);
  3640. struct iscsi_transport *t = session->transport;
  3641. int param;
  3642. if (attr == &dev_attr_sess_initial_r2t.attr)
  3643. param = ISCSI_PARAM_INITIAL_R2T_EN;
  3644. else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
  3645. param = ISCSI_PARAM_MAX_R2T;
  3646. else if (attr == &dev_attr_sess_immediate_data.attr)
  3647. param = ISCSI_PARAM_IMM_DATA_EN;
  3648. else if (attr == &dev_attr_sess_first_burst_len.attr)
  3649. param = ISCSI_PARAM_FIRST_BURST;
  3650. else if (attr == &dev_attr_sess_max_burst_len.attr)
  3651. param = ISCSI_PARAM_MAX_BURST;
  3652. else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
  3653. param = ISCSI_PARAM_PDU_INORDER_EN;
  3654. else if (attr == &dev_attr_sess_data_seq_in_order.attr)
  3655. param = ISCSI_PARAM_DATASEQ_INORDER_EN;
  3656. else if (attr == &dev_attr_sess_erl.attr)
  3657. param = ISCSI_PARAM_ERL;
  3658. else if (attr == &dev_attr_sess_targetname.attr)
  3659. param = ISCSI_PARAM_TARGET_NAME;
  3660. else if (attr == &dev_attr_sess_tpgt.attr)
  3661. param = ISCSI_PARAM_TPGT;
  3662. else if (attr == &dev_attr_sess_chap_in_idx.attr)
  3663. param = ISCSI_PARAM_CHAP_IN_IDX;
  3664. else if (attr == &dev_attr_sess_chap_out_idx.attr)
  3665. param = ISCSI_PARAM_CHAP_OUT_IDX;
  3666. else if (attr == &dev_attr_sess_password.attr)
  3667. param = ISCSI_PARAM_USERNAME;
  3668. else if (attr == &dev_attr_sess_password_in.attr)
  3669. param = ISCSI_PARAM_USERNAME_IN;
  3670. else if (attr == &dev_attr_sess_username.attr)
  3671. param = ISCSI_PARAM_PASSWORD;
  3672. else if (attr == &dev_attr_sess_username_in.attr)
  3673. param = ISCSI_PARAM_PASSWORD_IN;
  3674. else if (attr == &dev_attr_sess_fast_abort.attr)
  3675. param = ISCSI_PARAM_FAST_ABORT;
  3676. else if (attr == &dev_attr_sess_abort_tmo.attr)
  3677. param = ISCSI_PARAM_ABORT_TMO;
  3678. else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
  3679. param = ISCSI_PARAM_LU_RESET_TMO;
  3680. else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
  3681. param = ISCSI_PARAM_TGT_RESET_TMO;
  3682. else if (attr == &dev_attr_sess_ifacename.attr)
  3683. param = ISCSI_PARAM_IFACE_NAME;
  3684. else if (attr == &dev_attr_sess_initiatorname.attr)
  3685. param = ISCSI_PARAM_INITIATOR_NAME;
  3686. else if (attr == &dev_attr_sess_targetalias.attr)
  3687. param = ISCSI_PARAM_TARGET_ALIAS;
  3688. else if (attr == &dev_attr_sess_boot_root.attr)
  3689. param = ISCSI_PARAM_BOOT_ROOT;
  3690. else if (attr == &dev_attr_sess_boot_nic.attr)
  3691. param = ISCSI_PARAM_BOOT_NIC;
  3692. else if (attr == &dev_attr_sess_boot_target.attr)
  3693. param = ISCSI_PARAM_BOOT_TARGET;
  3694. else if (attr == &dev_attr_sess_auto_snd_tgt_disable.attr)
  3695. param = ISCSI_PARAM_AUTO_SND_TGT_DISABLE;
  3696. else if (attr == &dev_attr_sess_discovery_session.attr)
  3697. param = ISCSI_PARAM_DISCOVERY_SESS;
  3698. else if (attr == &dev_attr_sess_portal_type.attr)
  3699. param = ISCSI_PARAM_PORTAL_TYPE;
  3700. else if (attr == &dev_attr_sess_chap_auth.attr)
  3701. param = ISCSI_PARAM_CHAP_AUTH_EN;
  3702. else if (attr == &dev_attr_sess_discovery_logout.attr)
  3703. param = ISCSI_PARAM_DISCOVERY_LOGOUT_EN;
  3704. else if (attr == &dev_attr_sess_bidi_chap.attr)
  3705. param = ISCSI_PARAM_BIDI_CHAP_EN;
  3706. else if (attr == &dev_attr_sess_discovery_auth_optional.attr)
  3707. param = ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL;
  3708. else if (attr == &dev_attr_sess_def_time2wait.attr)
  3709. param = ISCSI_PARAM_DEF_TIME2WAIT;
  3710. else if (attr == &dev_attr_sess_def_time2retain.attr)
  3711. param = ISCSI_PARAM_DEF_TIME2RETAIN;
  3712. else if (attr == &dev_attr_sess_isid.attr)
  3713. param = ISCSI_PARAM_ISID;
  3714. else if (attr == &dev_attr_sess_tsid.attr)
  3715. param = ISCSI_PARAM_TSID;
  3716. else if (attr == &dev_attr_sess_def_taskmgmt_tmo.attr)
  3717. param = ISCSI_PARAM_DEF_TASKMGMT_TMO;
  3718. else if (attr == &dev_attr_sess_discovery_parent_idx.attr)
  3719. param = ISCSI_PARAM_DISCOVERY_PARENT_IDX;
  3720. else if (attr == &dev_attr_sess_discovery_parent_type.attr)
  3721. param = ISCSI_PARAM_DISCOVERY_PARENT_TYPE;
  3722. else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
  3723. return S_IRUGO | S_IWUSR;
  3724. else if (attr == &dev_attr_priv_sess_state.attr)
  3725. return S_IRUGO;
  3726. else if (attr == &dev_attr_priv_sess_creator.attr)
  3727. return S_IRUGO;
  3728. else if (attr == &dev_attr_priv_sess_target_id.attr)
  3729. return S_IRUGO;
  3730. else {
  3731. WARN_ONCE(1, "Invalid session attr");
  3732. return 0;
  3733. }
  3734. return t->attr_is_visible(ISCSI_PARAM, param);
  3735. }
  3736. static struct attribute_group iscsi_session_group = {
  3737. .attrs = iscsi_session_attrs,
  3738. .is_visible = iscsi_session_attr_is_visible,
  3739. };
  3740. /*
  3741. * iSCSI host attrs
  3742. */
  3743. #define iscsi_host_attr_show(param) \
  3744. static ssize_t \
  3745. show_host_param_##param(struct device *dev, \
  3746. struct device_attribute *attr, char *buf) \
  3747. { \
  3748. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  3749. struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
  3750. return priv->iscsi_transport->get_host_param(shost, param, buf); \
  3751. }
  3752. #define iscsi_host_attr(field, param) \
  3753. iscsi_host_attr_show(param) \
  3754. static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
  3755. NULL);
  3756. iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
  3757. iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
  3758. iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
  3759. iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
  3760. iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
  3761. iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
  3762. static struct attribute *iscsi_host_attrs[] = {
  3763. &dev_attr_host_netdev.attr,
  3764. &dev_attr_host_hwaddress.attr,
  3765. &dev_attr_host_ipaddress.attr,
  3766. &dev_attr_host_initiatorname.attr,
  3767. &dev_attr_host_port_state.attr,
  3768. &dev_attr_host_port_speed.attr,
  3769. NULL,
  3770. };
  3771. static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
  3772. struct attribute *attr, int i)
  3773. {
  3774. struct device *cdev = container_of(kobj, struct device, kobj);
  3775. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  3776. struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
  3777. int param;
  3778. if (attr == &dev_attr_host_netdev.attr)
  3779. param = ISCSI_HOST_PARAM_NETDEV_NAME;
  3780. else if (attr == &dev_attr_host_hwaddress.attr)
  3781. param = ISCSI_HOST_PARAM_HWADDRESS;
  3782. else if (attr == &dev_attr_host_ipaddress.attr)
  3783. param = ISCSI_HOST_PARAM_IPADDRESS;
  3784. else if (attr == &dev_attr_host_initiatorname.attr)
  3785. param = ISCSI_HOST_PARAM_INITIATOR_NAME;
  3786. else if (attr == &dev_attr_host_port_state.attr)
  3787. param = ISCSI_HOST_PARAM_PORT_STATE;
  3788. else if (attr == &dev_attr_host_port_speed.attr)
  3789. param = ISCSI_HOST_PARAM_PORT_SPEED;
  3790. else {
  3791. WARN_ONCE(1, "Invalid host attr");
  3792. return 0;
  3793. }
  3794. return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
  3795. }
  3796. static struct attribute_group iscsi_host_group = {
  3797. .attrs = iscsi_host_attrs,
  3798. .is_visible = iscsi_host_attr_is_visible,
  3799. };
  3800. /* convert iscsi_port_speed values to ascii string name */
  3801. static const struct {
  3802. enum iscsi_port_speed value;
  3803. char *name;
  3804. } iscsi_port_speed_names[] = {
  3805. {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
  3806. {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
  3807. {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
  3808. {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
  3809. {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
  3810. };
  3811. char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
  3812. {
  3813. int i;
  3814. char *speed = "Unknown!";
  3815. struct iscsi_cls_host *ihost = shost->shost_data;
  3816. uint32_t port_speed = ihost->port_speed;
  3817. for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
  3818. if (iscsi_port_speed_names[i].value & port_speed) {
  3819. speed = iscsi_port_speed_names[i].name;
  3820. break;
  3821. }
  3822. }
  3823. return speed;
  3824. }
  3825. EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
  3826. /* convert iscsi_port_state values to ascii string name */
  3827. static const struct {
  3828. enum iscsi_port_state value;
  3829. char *name;
  3830. } iscsi_port_state_names[] = {
  3831. {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
  3832. {ISCSI_PORT_STATE_UP, "LINK UP" },
  3833. };
  3834. char *iscsi_get_port_state_name(struct Scsi_Host *shost)
  3835. {
  3836. int i;
  3837. char *state = "Unknown!";
  3838. struct iscsi_cls_host *ihost = shost->shost_data;
  3839. uint32_t port_state = ihost->port_state;
  3840. for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
  3841. if (iscsi_port_state_names[i].value & port_state) {
  3842. state = iscsi_port_state_names[i].name;
  3843. break;
  3844. }
  3845. }
  3846. return state;
  3847. }
  3848. EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
  3849. static int iscsi_session_match(struct attribute_container *cont,
  3850. struct device *dev)
  3851. {
  3852. struct iscsi_cls_session *session;
  3853. struct Scsi_Host *shost;
  3854. struct iscsi_internal *priv;
  3855. if (!iscsi_is_session_dev(dev))
  3856. return 0;
  3857. session = iscsi_dev_to_session(dev);
  3858. shost = iscsi_session_to_shost(session);
  3859. if (!shost->transportt)
  3860. return 0;
  3861. priv = to_iscsi_internal(shost->transportt);
  3862. if (priv->session_cont.ac.class != &iscsi_session_class.class)
  3863. return 0;
  3864. return &priv->session_cont.ac == cont;
  3865. }
  3866. static int iscsi_conn_match(struct attribute_container *cont,
  3867. struct device *dev)
  3868. {
  3869. struct iscsi_cls_session *session;
  3870. struct iscsi_cls_conn *conn;
  3871. struct Scsi_Host *shost;
  3872. struct iscsi_internal *priv;
  3873. if (!iscsi_is_conn_dev(dev))
  3874. return 0;
  3875. conn = iscsi_dev_to_conn(dev);
  3876. session = iscsi_dev_to_session(conn->dev.parent);
  3877. shost = iscsi_session_to_shost(session);
  3878. if (!shost->transportt)
  3879. return 0;
  3880. priv = to_iscsi_internal(shost->transportt);
  3881. if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
  3882. return 0;
  3883. return &priv->conn_cont.ac == cont;
  3884. }
  3885. static int iscsi_host_match(struct attribute_container *cont,
  3886. struct device *dev)
  3887. {
  3888. struct Scsi_Host *shost;
  3889. struct iscsi_internal *priv;
  3890. if (!scsi_is_host_device(dev))
  3891. return 0;
  3892. shost = dev_to_shost(dev);
  3893. if (!shost->transportt ||
  3894. shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
  3895. return 0;
  3896. priv = to_iscsi_internal(shost->transportt);
  3897. return &priv->t.host_attrs.ac == cont;
  3898. }
  3899. struct scsi_transport_template *
  3900. iscsi_register_transport(struct iscsi_transport *tt)
  3901. {
  3902. struct iscsi_internal *priv;
  3903. unsigned long flags;
  3904. int err;
  3905. BUG_ON(!tt);
  3906. priv = iscsi_if_transport_lookup(tt);
  3907. if (priv)
  3908. return NULL;
  3909. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  3910. if (!priv)
  3911. return NULL;
  3912. INIT_LIST_HEAD(&priv->list);
  3913. priv->iscsi_transport = tt;
  3914. priv->t.user_scan = iscsi_user_scan;
  3915. priv->t.create_work_queue = 1;
  3916. priv->dev.class = &iscsi_transport_class;
  3917. dev_set_name(&priv->dev, "%s", tt->name);
  3918. err = device_register(&priv->dev);
  3919. if (err)
  3920. goto free_priv;
  3921. err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
  3922. if (err)
  3923. goto unregister_dev;
  3924. /* host parameters */
  3925. priv->t.host_attrs.ac.class = &iscsi_host_class.class;
  3926. priv->t.host_attrs.ac.match = iscsi_host_match;
  3927. priv->t.host_attrs.ac.grp = &iscsi_host_group;
  3928. priv->t.host_size = sizeof(struct iscsi_cls_host);
  3929. transport_container_register(&priv->t.host_attrs);
  3930. /* connection parameters */
  3931. priv->conn_cont.ac.class = &iscsi_connection_class.class;
  3932. priv->conn_cont.ac.match = iscsi_conn_match;
  3933. priv->conn_cont.ac.grp = &iscsi_conn_group;
  3934. transport_container_register(&priv->conn_cont);
  3935. /* session parameters */
  3936. priv->session_cont.ac.class = &iscsi_session_class.class;
  3937. priv->session_cont.ac.match = iscsi_session_match;
  3938. priv->session_cont.ac.grp = &iscsi_session_group;
  3939. transport_container_register(&priv->session_cont);
  3940. spin_lock_irqsave(&iscsi_transport_lock, flags);
  3941. list_add(&priv->list, &iscsi_transports);
  3942. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  3943. printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
  3944. return &priv->t;
  3945. unregister_dev:
  3946. device_unregister(&priv->dev);
  3947. return NULL;
  3948. free_priv:
  3949. kfree(priv);
  3950. return NULL;
  3951. }
  3952. EXPORT_SYMBOL_GPL(iscsi_register_transport);
  3953. int iscsi_unregister_transport(struct iscsi_transport *tt)
  3954. {
  3955. struct iscsi_internal *priv;
  3956. unsigned long flags;
  3957. BUG_ON(!tt);
  3958. mutex_lock(&rx_queue_mutex);
  3959. priv = iscsi_if_transport_lookup(tt);
  3960. BUG_ON (!priv);
  3961. spin_lock_irqsave(&iscsi_transport_lock, flags);
  3962. list_del(&priv->list);
  3963. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  3964. transport_container_unregister(&priv->conn_cont);
  3965. transport_container_unregister(&priv->session_cont);
  3966. transport_container_unregister(&priv->t.host_attrs);
  3967. sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
  3968. device_unregister(&priv->dev);
  3969. mutex_unlock(&rx_queue_mutex);
  3970. return 0;
  3971. }
  3972. EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
  3973. static __init int iscsi_transport_init(void)
  3974. {
  3975. int err;
  3976. struct netlink_kernel_cfg cfg = {
  3977. .groups = 1,
  3978. .input = iscsi_if_rx,
  3979. };
  3980. printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
  3981. ISCSI_TRANSPORT_VERSION);
  3982. atomic_set(&iscsi_session_nr, 0);
  3983. err = class_register(&iscsi_transport_class);
  3984. if (err)
  3985. return err;
  3986. err = class_register(&iscsi_endpoint_class);
  3987. if (err)
  3988. goto unregister_transport_class;
  3989. err = class_register(&iscsi_iface_class);
  3990. if (err)
  3991. goto unregister_endpoint_class;
  3992. err = transport_class_register(&iscsi_host_class);
  3993. if (err)
  3994. goto unregister_iface_class;
  3995. err = transport_class_register(&iscsi_connection_class);
  3996. if (err)
  3997. goto unregister_host_class;
  3998. err = transport_class_register(&iscsi_session_class);
  3999. if (err)
  4000. goto unregister_conn_class;
  4001. err = bus_register(&iscsi_flashnode_bus);
  4002. if (err)
  4003. goto unregister_session_class;
  4004. nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
  4005. if (!nls) {
  4006. err = -ENOBUFS;
  4007. goto unregister_flashnode_bus;
  4008. }
  4009. iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
  4010. if (!iscsi_eh_timer_workq) {
  4011. err = -ENOMEM;
  4012. goto release_nls;
  4013. }
  4014. return 0;
  4015. release_nls:
  4016. netlink_kernel_release(nls);
  4017. unregister_flashnode_bus:
  4018. bus_unregister(&iscsi_flashnode_bus);
  4019. unregister_session_class:
  4020. transport_class_unregister(&iscsi_session_class);
  4021. unregister_conn_class:
  4022. transport_class_unregister(&iscsi_connection_class);
  4023. unregister_host_class:
  4024. transport_class_unregister(&iscsi_host_class);
  4025. unregister_iface_class:
  4026. class_unregister(&iscsi_iface_class);
  4027. unregister_endpoint_class:
  4028. class_unregister(&iscsi_endpoint_class);
  4029. unregister_transport_class:
  4030. class_unregister(&iscsi_transport_class);
  4031. return err;
  4032. }
  4033. static void __exit iscsi_transport_exit(void)
  4034. {
  4035. destroy_workqueue(iscsi_eh_timer_workq);
  4036. netlink_kernel_release(nls);
  4037. bus_unregister(&iscsi_flashnode_bus);
  4038. transport_class_unregister(&iscsi_connection_class);
  4039. transport_class_unregister(&iscsi_session_class);
  4040. transport_class_unregister(&iscsi_host_class);
  4041. class_unregister(&iscsi_endpoint_class);
  4042. class_unregister(&iscsi_iface_class);
  4043. class_unregister(&iscsi_transport_class);
  4044. }
  4045. module_init(iscsi_transport_init);
  4046. module_exit(iscsi_transport_exit);
  4047. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  4048. "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  4049. "Alex Aizman <itn780@yahoo.com>");
  4050. MODULE_DESCRIPTION("iSCSI Transport Interface");
  4051. MODULE_LICENSE("GPL");
  4052. MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
  4053. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);