scsi_transport_iscsi.c 139 KB

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