scsi_transport_iscsi.c 150 KB

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