scsi_transport_iscsi.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027
  1. /*
  2. * iSCSI transport class definitions
  3. *
  4. * Copyright (C) IBM Corporation, 2004
  5. * Copyright (C) Mike Christie, 2004 - 2005
  6. * Copyright (C) Dmitry Yusupov, 2004 - 2005
  7. * Copyright (C) Alex Aizman, 2004 - 2005
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/slab.h>
  26. #include <linux/bsg-lib.h>
  27. #include <linux/idr.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_transport.h>
  33. #include <scsi/scsi_transport_iscsi.h>
  34. #include <scsi/iscsi_if.h>
  35. #include <scsi/scsi_cmnd.h>
  36. #include <scsi/scsi_bsg_iscsi.h>
  37. #define ISCSI_TRANSPORT_VERSION "2.0-870"
  38. static int dbg_session;
  39. module_param_named(debug_session, dbg_session, int,
  40. S_IRUGO | S_IWUSR);
  41. MODULE_PARM_DESC(debug_session,
  42. "Turn on debugging for sessions in scsi_transport_iscsi "
  43. "module. Set to 1 to turn on, and zero to turn off. Default "
  44. "is off.");
  45. static int dbg_conn;
  46. module_param_named(debug_conn, dbg_conn, int,
  47. S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(debug_conn,
  49. "Turn on debugging for connections in scsi_transport_iscsi "
  50. "module. Set to 1 to turn on, and zero to turn off. Default "
  51. "is off.");
  52. #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
  53. do { \
  54. if (dbg_session) \
  55. iscsi_cls_session_printk(KERN_INFO, _session, \
  56. "%s: " dbg_fmt, \
  57. __func__, ##arg); \
  58. } while (0);
  59. #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
  60. do { \
  61. if (dbg_conn) \
  62. iscsi_cls_conn_printk(KERN_INFO, _conn, \
  63. "%s: " dbg_fmt, \
  64. __func__, ##arg); \
  65. } while (0);
  66. struct iscsi_internal {
  67. struct scsi_transport_template t;
  68. struct iscsi_transport *iscsi_transport;
  69. struct list_head list;
  70. struct device dev;
  71. struct transport_container conn_cont;
  72. struct transport_container session_cont;
  73. };
  74. static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
  75. static struct workqueue_struct *iscsi_eh_timer_workq;
  76. static DEFINE_IDA(iscsi_sess_ida);
  77. /*
  78. * list of registered transports and lock that must
  79. * be held while accessing list. The iscsi_transport_lock must
  80. * be acquired after the rx_queue_mutex.
  81. */
  82. static LIST_HEAD(iscsi_transports);
  83. static DEFINE_SPINLOCK(iscsi_transport_lock);
  84. #define to_iscsi_internal(tmpl) \
  85. container_of(tmpl, struct iscsi_internal, t)
  86. #define dev_to_iscsi_internal(_dev) \
  87. container_of(_dev, struct iscsi_internal, dev)
  88. static void iscsi_transport_release(struct device *dev)
  89. {
  90. struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
  91. kfree(priv);
  92. }
  93. /*
  94. * iscsi_transport_class represents the iscsi_transports that are
  95. * registered.
  96. */
  97. static struct class iscsi_transport_class = {
  98. .name = "iscsi_transport",
  99. .dev_release = iscsi_transport_release,
  100. };
  101. static ssize_t
  102. show_transport_handle(struct device *dev, struct device_attribute *attr,
  103. char *buf)
  104. {
  105. struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
  106. if (!capable(CAP_SYS_ADMIN))
  107. return -EACCES;
  108. return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
  109. }
  110. static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
  111. #define show_transport_attr(name, format) \
  112. static ssize_t \
  113. show_transport_##name(struct device *dev, \
  114. struct device_attribute *attr,char *buf) \
  115. { \
  116. struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
  117. return sprintf(buf, format"\n", priv->iscsi_transport->name); \
  118. } \
  119. static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
  120. show_transport_attr(caps, "0x%x");
  121. static struct attribute *iscsi_transport_attrs[] = {
  122. &dev_attr_handle.attr,
  123. &dev_attr_caps.attr,
  124. NULL,
  125. };
  126. static struct attribute_group iscsi_transport_group = {
  127. .attrs = iscsi_transport_attrs,
  128. };
  129. /*
  130. * iSCSI endpoint attrs
  131. */
  132. #define iscsi_dev_to_endpoint(_dev) \
  133. container_of(_dev, struct iscsi_endpoint, dev)
  134. #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
  135. struct device_attribute dev_attr_##_prefix##_##_name = \
  136. __ATTR(_name,_mode,_show,_store)
  137. static void iscsi_endpoint_release(struct device *dev)
  138. {
  139. struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
  140. kfree(ep);
  141. }
  142. static struct class iscsi_endpoint_class = {
  143. .name = "iscsi_endpoint",
  144. .dev_release = iscsi_endpoint_release,
  145. };
  146. static ssize_t
  147. show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
  148. {
  149. struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
  150. return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
  151. }
  152. static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
  153. static struct attribute *iscsi_endpoint_attrs[] = {
  154. &dev_attr_ep_handle.attr,
  155. NULL,
  156. };
  157. static struct attribute_group iscsi_endpoint_group = {
  158. .attrs = iscsi_endpoint_attrs,
  159. };
  160. #define ISCSI_MAX_EPID -1
  161. static int iscsi_match_epid(struct device *dev, void *data)
  162. {
  163. struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
  164. uint64_t *epid = (uint64_t *) data;
  165. return *epid == ep->id;
  166. }
  167. struct iscsi_endpoint *
  168. iscsi_create_endpoint(int dd_size)
  169. {
  170. struct device *dev;
  171. struct iscsi_endpoint *ep;
  172. uint64_t id;
  173. int err;
  174. for (id = 1; id < ISCSI_MAX_EPID; id++) {
  175. dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
  176. iscsi_match_epid);
  177. if (!dev)
  178. break;
  179. }
  180. if (id == ISCSI_MAX_EPID) {
  181. printk(KERN_ERR "Too many connections. Max supported %u\n",
  182. ISCSI_MAX_EPID - 1);
  183. return NULL;
  184. }
  185. ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
  186. if (!ep)
  187. return NULL;
  188. ep->id = id;
  189. ep->dev.class = &iscsi_endpoint_class;
  190. dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
  191. err = device_register(&ep->dev);
  192. if (err)
  193. goto free_ep;
  194. err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
  195. if (err)
  196. goto unregister_dev;
  197. if (dd_size)
  198. ep->dd_data = &ep[1];
  199. return ep;
  200. unregister_dev:
  201. device_unregister(&ep->dev);
  202. return NULL;
  203. free_ep:
  204. kfree(ep);
  205. return NULL;
  206. }
  207. EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
  208. void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
  209. {
  210. sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
  211. device_unregister(&ep->dev);
  212. }
  213. EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
  214. struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
  215. {
  216. struct iscsi_endpoint *ep;
  217. struct device *dev;
  218. dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
  219. iscsi_match_epid);
  220. if (!dev)
  221. return NULL;
  222. ep = iscsi_dev_to_endpoint(dev);
  223. /*
  224. * we can drop this now because the interface will prevent
  225. * removals and lookups from racing.
  226. */
  227. put_device(dev);
  228. return ep;
  229. }
  230. EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
  231. /*
  232. * Interface to display network param to sysfs
  233. */
  234. static void iscsi_iface_release(struct device *dev)
  235. {
  236. struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
  237. struct device *parent = iface->dev.parent;
  238. kfree(iface);
  239. put_device(parent);
  240. }
  241. static struct class iscsi_iface_class = {
  242. .name = "iscsi_iface",
  243. .dev_release = iscsi_iface_release,
  244. };
  245. #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
  246. struct device_attribute dev_attr_##_prefix##_##_name = \
  247. __ATTR(_name, _mode, _show, _store)
  248. /* iface attrs show */
  249. #define iscsi_iface_attr_show(type, name, param_type, param) \
  250. static ssize_t \
  251. show_##type##_##name(struct device *dev, struct device_attribute *attr, \
  252. char *buf) \
  253. { \
  254. struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
  255. struct iscsi_transport *t = iface->transport; \
  256. return t->get_iface_param(iface, param_type, param, buf); \
  257. } \
  258. #define iscsi_iface_net_attr(type, name, param) \
  259. iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
  260. static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
  261. /* generic read only ipvi4 attribute */
  262. iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
  263. iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
  264. iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
  265. iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
  266. /* generic read only ipv6 attribute */
  267. iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
  268. iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
  269. iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
  270. iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
  271. ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
  272. iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
  273. ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
  274. /* common read only iface attribute */
  275. iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
  276. iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
  277. iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
  278. iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
  279. iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
  280. iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
  281. static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
  282. struct attribute *attr, int i)
  283. {
  284. struct device *dev = container_of(kobj, struct device, kobj);
  285. struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
  286. struct iscsi_transport *t = iface->transport;
  287. int param;
  288. if (attr == &dev_attr_iface_enabled.attr)
  289. param = ISCSI_NET_PARAM_IFACE_ENABLE;
  290. else if (attr == &dev_attr_iface_vlan_id.attr)
  291. param = ISCSI_NET_PARAM_VLAN_ID;
  292. else if (attr == &dev_attr_iface_vlan_priority.attr)
  293. param = ISCSI_NET_PARAM_VLAN_PRIORITY;
  294. else if (attr == &dev_attr_iface_vlan_enabled.attr)
  295. param = ISCSI_NET_PARAM_VLAN_ENABLED;
  296. else if (attr == &dev_attr_iface_mtu.attr)
  297. param = ISCSI_NET_PARAM_MTU;
  298. else if (attr == &dev_attr_iface_port.attr)
  299. param = ISCSI_NET_PARAM_PORT;
  300. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
  301. if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
  302. param = ISCSI_NET_PARAM_IPV4_ADDR;
  303. else if (attr == &dev_attr_ipv4_iface_gateway.attr)
  304. param = ISCSI_NET_PARAM_IPV4_GW;
  305. else if (attr == &dev_attr_ipv4_iface_subnet.attr)
  306. param = ISCSI_NET_PARAM_IPV4_SUBNET;
  307. else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
  308. param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
  309. else
  310. return 0;
  311. } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
  312. if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
  313. param = ISCSI_NET_PARAM_IPV6_ADDR;
  314. else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
  315. param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
  316. else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
  317. param = ISCSI_NET_PARAM_IPV6_ROUTER;
  318. else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
  319. param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
  320. else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
  321. param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
  322. else
  323. return 0;
  324. } else {
  325. WARN_ONCE(1, "Invalid iface attr");
  326. return 0;
  327. }
  328. return t->attr_is_visible(ISCSI_NET_PARAM, param);
  329. }
  330. static struct attribute *iscsi_iface_attrs[] = {
  331. &dev_attr_iface_enabled.attr,
  332. &dev_attr_iface_vlan_id.attr,
  333. &dev_attr_iface_vlan_priority.attr,
  334. &dev_attr_iface_vlan_enabled.attr,
  335. &dev_attr_ipv4_iface_ipaddress.attr,
  336. &dev_attr_ipv4_iface_gateway.attr,
  337. &dev_attr_ipv4_iface_subnet.attr,
  338. &dev_attr_ipv4_iface_bootproto.attr,
  339. &dev_attr_ipv6_iface_ipaddress.attr,
  340. &dev_attr_ipv6_iface_link_local_addr.attr,
  341. &dev_attr_ipv6_iface_router_addr.attr,
  342. &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
  343. &dev_attr_ipv6_iface_link_local_autocfg.attr,
  344. &dev_attr_iface_mtu.attr,
  345. &dev_attr_iface_port.attr,
  346. NULL,
  347. };
  348. static struct attribute_group iscsi_iface_group = {
  349. .attrs = iscsi_iface_attrs,
  350. .is_visible = iscsi_iface_attr_is_visible,
  351. };
  352. struct iscsi_iface *
  353. iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
  354. uint32_t iface_type, uint32_t iface_num, int dd_size)
  355. {
  356. struct iscsi_iface *iface;
  357. int err;
  358. iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
  359. if (!iface)
  360. return NULL;
  361. iface->transport = transport;
  362. iface->iface_type = iface_type;
  363. iface->iface_num = iface_num;
  364. iface->dev.release = iscsi_iface_release;
  365. iface->dev.class = &iscsi_iface_class;
  366. /* parent reference released in iscsi_iface_release */
  367. iface->dev.parent = get_device(&shost->shost_gendev);
  368. if (iface_type == ISCSI_IFACE_TYPE_IPV4)
  369. dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
  370. iface_num);
  371. else
  372. dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
  373. iface_num);
  374. err = device_register(&iface->dev);
  375. if (err)
  376. goto free_iface;
  377. err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
  378. if (err)
  379. goto unreg_iface;
  380. if (dd_size)
  381. iface->dd_data = &iface[1];
  382. return iface;
  383. unreg_iface:
  384. device_unregister(&iface->dev);
  385. return NULL;
  386. free_iface:
  387. put_device(iface->dev.parent);
  388. kfree(iface);
  389. return NULL;
  390. }
  391. EXPORT_SYMBOL_GPL(iscsi_create_iface);
  392. void iscsi_destroy_iface(struct iscsi_iface *iface)
  393. {
  394. sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
  395. device_unregister(&iface->dev);
  396. }
  397. EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
  398. /*
  399. * BSG support
  400. */
  401. /**
  402. * iscsi_bsg_host_dispatch - Dispatch command to LLD.
  403. * @job: bsg job to be processed
  404. */
  405. static int iscsi_bsg_host_dispatch(struct bsg_job *job)
  406. {
  407. struct Scsi_Host *shost = iscsi_job_to_shost(job);
  408. struct iscsi_bsg_request *req = job->request;
  409. struct iscsi_bsg_reply *reply = job->reply;
  410. struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
  411. int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
  412. int ret;
  413. /* check if we have the msgcode value at least */
  414. if (job->request_len < sizeof(uint32_t)) {
  415. ret = -ENOMSG;
  416. goto fail_host_msg;
  417. }
  418. /* Validate the host command */
  419. switch (req->msgcode) {
  420. case ISCSI_BSG_HST_VENDOR:
  421. cmdlen += sizeof(struct iscsi_bsg_host_vendor);
  422. if ((shost->hostt->vendor_id == 0L) ||
  423. (req->rqst_data.h_vendor.vendor_id !=
  424. shost->hostt->vendor_id)) {
  425. ret = -ESRCH;
  426. goto fail_host_msg;
  427. }
  428. break;
  429. default:
  430. ret = -EBADR;
  431. goto fail_host_msg;
  432. }
  433. /* check if we really have all the request data needed */
  434. if (job->request_len < cmdlen) {
  435. ret = -ENOMSG;
  436. goto fail_host_msg;
  437. }
  438. ret = i->iscsi_transport->bsg_request(job);
  439. if (!ret)
  440. return 0;
  441. fail_host_msg:
  442. /* return the errno failure code as the only status */
  443. BUG_ON(job->reply_len < sizeof(uint32_t));
  444. reply->reply_payload_rcv_len = 0;
  445. reply->result = ret;
  446. job->reply_len = sizeof(uint32_t);
  447. bsg_job_done(job, ret, 0);
  448. return 0;
  449. }
  450. /**
  451. * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
  452. * @shost: shost for iscsi_host
  453. * @ihost: iscsi_cls_host adding the structures to
  454. */
  455. static int
  456. iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
  457. {
  458. struct device *dev = &shost->shost_gendev;
  459. struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
  460. struct request_queue *q;
  461. char bsg_name[20];
  462. int ret;
  463. if (!i->iscsi_transport->bsg_request)
  464. return -ENOTSUPP;
  465. snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
  466. q = __scsi_alloc_queue(shost, bsg_request_fn);
  467. if (!q)
  468. return -ENOMEM;
  469. ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
  470. if (ret) {
  471. shost_printk(KERN_ERR, shost, "bsg interface failed to "
  472. "initialize - no request queue\n");
  473. blk_cleanup_queue(q);
  474. return ret;
  475. }
  476. ihost->bsg_q = q;
  477. return 0;
  478. }
  479. static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
  480. struct device *cdev)
  481. {
  482. struct Scsi_Host *shost = dev_to_shost(dev);
  483. struct iscsi_cls_host *ihost = shost->shost_data;
  484. memset(ihost, 0, sizeof(*ihost));
  485. atomic_set(&ihost->nr_scans, 0);
  486. mutex_init(&ihost->mutex);
  487. iscsi_bsg_host_add(shost, ihost);
  488. /* ignore any bsg add error - we just can't do sgio */
  489. return 0;
  490. }
  491. static int iscsi_remove_host(struct transport_container *tc,
  492. struct device *dev, struct device *cdev)
  493. {
  494. struct Scsi_Host *shost = dev_to_shost(dev);
  495. struct iscsi_cls_host *ihost = shost->shost_data;
  496. if (ihost->bsg_q) {
  497. bsg_remove_queue(ihost->bsg_q);
  498. blk_cleanup_queue(ihost->bsg_q);
  499. }
  500. return 0;
  501. }
  502. static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
  503. "iscsi_host",
  504. iscsi_setup_host,
  505. iscsi_remove_host,
  506. NULL);
  507. static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
  508. "iscsi_session",
  509. NULL,
  510. NULL,
  511. NULL);
  512. static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
  513. "iscsi_connection",
  514. NULL,
  515. NULL,
  516. NULL);
  517. static struct sock *nls;
  518. static DEFINE_MUTEX(rx_queue_mutex);
  519. static LIST_HEAD(sesslist);
  520. static DEFINE_SPINLOCK(sesslock);
  521. static LIST_HEAD(connlist);
  522. static DEFINE_SPINLOCK(connlock);
  523. static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
  524. {
  525. struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
  526. return sess->sid;
  527. }
  528. /*
  529. * Returns the matching session to a given sid
  530. */
  531. static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
  532. {
  533. unsigned long flags;
  534. struct iscsi_cls_session *sess;
  535. spin_lock_irqsave(&sesslock, flags);
  536. list_for_each_entry(sess, &sesslist, sess_list) {
  537. if (sess->sid == sid) {
  538. spin_unlock_irqrestore(&sesslock, flags);
  539. return sess;
  540. }
  541. }
  542. spin_unlock_irqrestore(&sesslock, flags);
  543. return NULL;
  544. }
  545. /*
  546. * Returns the matching connection to a given sid / cid tuple
  547. */
  548. static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
  549. {
  550. unsigned long flags;
  551. struct iscsi_cls_conn *conn;
  552. spin_lock_irqsave(&connlock, flags);
  553. list_for_each_entry(conn, &connlist, conn_list) {
  554. if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
  555. spin_unlock_irqrestore(&connlock, flags);
  556. return conn;
  557. }
  558. }
  559. spin_unlock_irqrestore(&connlock, flags);
  560. return NULL;
  561. }
  562. /*
  563. * The following functions can be used by LLDs that allocate
  564. * their own scsi_hosts or by software iscsi LLDs
  565. */
  566. static struct {
  567. int value;
  568. char *name;
  569. } iscsi_session_state_names[] = {
  570. { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
  571. { ISCSI_SESSION_FAILED, "FAILED" },
  572. { ISCSI_SESSION_FREE, "FREE" },
  573. };
  574. static const char *iscsi_session_state_name(int state)
  575. {
  576. int i;
  577. char *name = NULL;
  578. for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
  579. if (iscsi_session_state_names[i].value == state) {
  580. name = iscsi_session_state_names[i].name;
  581. break;
  582. }
  583. }
  584. return name;
  585. }
  586. int iscsi_session_chkready(struct iscsi_cls_session *session)
  587. {
  588. unsigned long flags;
  589. int err;
  590. spin_lock_irqsave(&session->lock, flags);
  591. switch (session->state) {
  592. case ISCSI_SESSION_LOGGED_IN:
  593. err = 0;
  594. break;
  595. case ISCSI_SESSION_FAILED:
  596. err = DID_IMM_RETRY << 16;
  597. break;
  598. case ISCSI_SESSION_FREE:
  599. err = DID_TRANSPORT_FAILFAST << 16;
  600. break;
  601. default:
  602. err = DID_NO_CONNECT << 16;
  603. break;
  604. }
  605. spin_unlock_irqrestore(&session->lock, flags);
  606. return err;
  607. }
  608. EXPORT_SYMBOL_GPL(iscsi_session_chkready);
  609. int iscsi_is_session_online(struct iscsi_cls_session *session)
  610. {
  611. unsigned long flags;
  612. int ret = 0;
  613. spin_lock_irqsave(&session->lock, flags);
  614. if (session->state == ISCSI_SESSION_LOGGED_IN)
  615. ret = 1;
  616. spin_unlock_irqrestore(&session->lock, flags);
  617. return ret;
  618. }
  619. EXPORT_SYMBOL_GPL(iscsi_is_session_online);
  620. static void iscsi_session_release(struct device *dev)
  621. {
  622. struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
  623. struct Scsi_Host *shost;
  624. shost = iscsi_session_to_shost(session);
  625. scsi_host_put(shost);
  626. ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
  627. kfree(session);
  628. }
  629. int iscsi_is_session_dev(const struct device *dev)
  630. {
  631. return dev->release == iscsi_session_release;
  632. }
  633. EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
  634. static int iscsi_iter_session_fn(struct device *dev, void *data)
  635. {
  636. void (* fn) (struct iscsi_cls_session *) = data;
  637. if (!iscsi_is_session_dev(dev))
  638. return 0;
  639. fn(iscsi_dev_to_session(dev));
  640. return 0;
  641. }
  642. void iscsi_host_for_each_session(struct Scsi_Host *shost,
  643. void (*fn)(struct iscsi_cls_session *))
  644. {
  645. device_for_each_child(&shost->shost_gendev, fn,
  646. iscsi_iter_session_fn);
  647. }
  648. EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
  649. /**
  650. * iscsi_scan_finished - helper to report when running scans are done
  651. * @shost: scsi host
  652. * @time: scan run time
  653. *
  654. * This function can be used by drives like qla4xxx to report to the scsi
  655. * layer when the scans it kicked off at module load time are done.
  656. */
  657. int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
  658. {
  659. struct iscsi_cls_host *ihost = shost->shost_data;
  660. /*
  661. * qla4xxx will have kicked off some session unblocks before calling
  662. * scsi_scan_host, so just wait for them to complete.
  663. */
  664. return !atomic_read(&ihost->nr_scans);
  665. }
  666. EXPORT_SYMBOL_GPL(iscsi_scan_finished);
  667. struct iscsi_scan_data {
  668. unsigned int channel;
  669. unsigned int id;
  670. unsigned int lun;
  671. };
  672. static int iscsi_user_scan_session(struct device *dev, void *data)
  673. {
  674. struct iscsi_scan_data *scan_data = data;
  675. struct iscsi_cls_session *session;
  676. struct Scsi_Host *shost;
  677. struct iscsi_cls_host *ihost;
  678. unsigned long flags;
  679. unsigned int id;
  680. if (!iscsi_is_session_dev(dev))
  681. return 0;
  682. session = iscsi_dev_to_session(dev);
  683. ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
  684. shost = iscsi_session_to_shost(session);
  685. ihost = shost->shost_data;
  686. mutex_lock(&ihost->mutex);
  687. spin_lock_irqsave(&session->lock, flags);
  688. if (session->state != ISCSI_SESSION_LOGGED_IN) {
  689. spin_unlock_irqrestore(&session->lock, flags);
  690. goto user_scan_exit;
  691. }
  692. id = session->target_id;
  693. spin_unlock_irqrestore(&session->lock, flags);
  694. if (id != ISCSI_MAX_TARGET) {
  695. if ((scan_data->channel == SCAN_WILD_CARD ||
  696. scan_data->channel == 0) &&
  697. (scan_data->id == SCAN_WILD_CARD ||
  698. scan_data->id == id))
  699. scsi_scan_target(&session->dev, 0, id,
  700. scan_data->lun, 1);
  701. }
  702. user_scan_exit:
  703. mutex_unlock(&ihost->mutex);
  704. ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
  705. return 0;
  706. }
  707. static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
  708. uint id, uint lun)
  709. {
  710. struct iscsi_scan_data scan_data;
  711. scan_data.channel = channel;
  712. scan_data.id = id;
  713. scan_data.lun = lun;
  714. return device_for_each_child(&shost->shost_gendev, &scan_data,
  715. iscsi_user_scan_session);
  716. }
  717. static void iscsi_scan_session(struct work_struct *work)
  718. {
  719. struct iscsi_cls_session *session =
  720. container_of(work, struct iscsi_cls_session, scan_work);
  721. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  722. struct iscsi_cls_host *ihost = shost->shost_data;
  723. struct iscsi_scan_data scan_data;
  724. scan_data.channel = 0;
  725. scan_data.id = SCAN_WILD_CARD;
  726. scan_data.lun = SCAN_WILD_CARD;
  727. iscsi_user_scan_session(&session->dev, &scan_data);
  728. atomic_dec(&ihost->nr_scans);
  729. }
  730. /**
  731. * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
  732. * @cmd: scsi cmd passed to scsi eh handler
  733. *
  734. * If the session is down this function will wait for the recovery
  735. * timer to fire or for the session to be logged back in. If the
  736. * recovery timer fires then FAST_IO_FAIL is returned. The caller
  737. * should pass this error value to the scsi eh.
  738. */
  739. int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
  740. {
  741. struct iscsi_cls_session *session =
  742. starget_to_session(scsi_target(cmd->device));
  743. unsigned long flags;
  744. int ret = 0;
  745. spin_lock_irqsave(&session->lock, flags);
  746. while (session->state != ISCSI_SESSION_LOGGED_IN) {
  747. if (session->state == ISCSI_SESSION_FREE) {
  748. ret = FAST_IO_FAIL;
  749. break;
  750. }
  751. spin_unlock_irqrestore(&session->lock, flags);
  752. msleep(1000);
  753. spin_lock_irqsave(&session->lock, flags);
  754. }
  755. spin_unlock_irqrestore(&session->lock, flags);
  756. return ret;
  757. }
  758. EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
  759. static void session_recovery_timedout(struct work_struct *work)
  760. {
  761. struct iscsi_cls_session *session =
  762. container_of(work, struct iscsi_cls_session,
  763. recovery_work.work);
  764. unsigned long flags;
  765. iscsi_cls_session_printk(KERN_INFO, session,
  766. "session recovery timed out after %d secs\n",
  767. session->recovery_tmo);
  768. spin_lock_irqsave(&session->lock, flags);
  769. switch (session->state) {
  770. case ISCSI_SESSION_FAILED:
  771. session->state = ISCSI_SESSION_FREE;
  772. break;
  773. case ISCSI_SESSION_LOGGED_IN:
  774. case ISCSI_SESSION_FREE:
  775. /* we raced with the unblock's flush */
  776. spin_unlock_irqrestore(&session->lock, flags);
  777. return;
  778. }
  779. spin_unlock_irqrestore(&session->lock, flags);
  780. if (session->transport->session_recovery_timedout)
  781. session->transport->session_recovery_timedout(session);
  782. ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
  783. scsi_target_unblock(&session->dev);
  784. ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
  785. }
  786. static void __iscsi_unblock_session(struct work_struct *work)
  787. {
  788. struct iscsi_cls_session *session =
  789. container_of(work, struct iscsi_cls_session,
  790. unblock_work);
  791. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  792. struct iscsi_cls_host *ihost = shost->shost_data;
  793. unsigned long flags;
  794. ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
  795. /*
  796. * The recovery and unblock work get run from the same workqueue,
  797. * so try to cancel it if it was going to run after this unblock.
  798. */
  799. cancel_delayed_work(&session->recovery_work);
  800. spin_lock_irqsave(&session->lock, flags);
  801. session->state = ISCSI_SESSION_LOGGED_IN;
  802. spin_unlock_irqrestore(&session->lock, flags);
  803. /* start IO */
  804. scsi_target_unblock(&session->dev);
  805. /*
  806. * Only do kernel scanning if the driver is properly hooked into
  807. * the async scanning code (drivers like iscsi_tcp do login and
  808. * scanning from userspace).
  809. */
  810. if (shost->hostt->scan_finished) {
  811. if (scsi_queue_work(shost, &session->scan_work))
  812. atomic_inc(&ihost->nr_scans);
  813. }
  814. ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
  815. }
  816. /**
  817. * iscsi_unblock_session - set a session as logged in and start IO.
  818. * @session: iscsi session
  819. *
  820. * Mark a session as ready to accept IO.
  821. */
  822. void iscsi_unblock_session(struct iscsi_cls_session *session)
  823. {
  824. queue_work(iscsi_eh_timer_workq, &session->unblock_work);
  825. /*
  826. * make sure all the events have completed before tell the driver
  827. * it is safe
  828. */
  829. flush_workqueue(iscsi_eh_timer_workq);
  830. }
  831. EXPORT_SYMBOL_GPL(iscsi_unblock_session);
  832. static void __iscsi_block_session(struct work_struct *work)
  833. {
  834. struct iscsi_cls_session *session =
  835. container_of(work, struct iscsi_cls_session,
  836. block_work);
  837. unsigned long flags;
  838. ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
  839. spin_lock_irqsave(&session->lock, flags);
  840. session->state = ISCSI_SESSION_FAILED;
  841. spin_unlock_irqrestore(&session->lock, flags);
  842. scsi_target_block(&session->dev);
  843. ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
  844. if (session->recovery_tmo >= 0)
  845. queue_delayed_work(iscsi_eh_timer_workq,
  846. &session->recovery_work,
  847. session->recovery_tmo * HZ);
  848. }
  849. void iscsi_block_session(struct iscsi_cls_session *session)
  850. {
  851. queue_work(iscsi_eh_timer_workq, &session->block_work);
  852. }
  853. EXPORT_SYMBOL_GPL(iscsi_block_session);
  854. static void __iscsi_unbind_session(struct work_struct *work)
  855. {
  856. struct iscsi_cls_session *session =
  857. container_of(work, struct iscsi_cls_session,
  858. unbind_work);
  859. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  860. struct iscsi_cls_host *ihost = shost->shost_data;
  861. unsigned long flags;
  862. unsigned int target_id;
  863. ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
  864. /* Prevent new scans and make sure scanning is not in progress */
  865. mutex_lock(&ihost->mutex);
  866. spin_lock_irqsave(&session->lock, flags);
  867. if (session->target_id == ISCSI_MAX_TARGET) {
  868. spin_unlock_irqrestore(&session->lock, flags);
  869. mutex_unlock(&ihost->mutex);
  870. return;
  871. }
  872. target_id = session->target_id;
  873. session->target_id = ISCSI_MAX_TARGET;
  874. spin_unlock_irqrestore(&session->lock, flags);
  875. mutex_unlock(&ihost->mutex);
  876. if (session->ida_used)
  877. ida_simple_remove(&iscsi_sess_ida, target_id);
  878. scsi_remove_target(&session->dev);
  879. iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
  880. ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
  881. }
  882. struct iscsi_cls_session *
  883. iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
  884. int dd_size)
  885. {
  886. struct iscsi_cls_session *session;
  887. session = kzalloc(sizeof(*session) + dd_size,
  888. GFP_KERNEL);
  889. if (!session)
  890. return NULL;
  891. session->transport = transport;
  892. session->creator = -1;
  893. session->recovery_tmo = 120;
  894. session->state = ISCSI_SESSION_FREE;
  895. INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
  896. INIT_LIST_HEAD(&session->sess_list);
  897. INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
  898. INIT_WORK(&session->block_work, __iscsi_block_session);
  899. INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
  900. INIT_WORK(&session->scan_work, iscsi_scan_session);
  901. spin_lock_init(&session->lock);
  902. /* this is released in the dev's release function */
  903. scsi_host_get(shost);
  904. session->dev.parent = &shost->shost_gendev;
  905. session->dev.release = iscsi_session_release;
  906. device_initialize(&session->dev);
  907. if (dd_size)
  908. session->dd_data = &session[1];
  909. ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
  910. return session;
  911. }
  912. EXPORT_SYMBOL_GPL(iscsi_alloc_session);
  913. int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
  914. {
  915. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  916. struct iscsi_cls_host *ihost;
  917. unsigned long flags;
  918. int id = 0;
  919. int err;
  920. ihost = shost->shost_data;
  921. session->sid = atomic_add_return(1, &iscsi_session_nr);
  922. if (target_id == ISCSI_MAX_TARGET) {
  923. id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
  924. if (id < 0) {
  925. iscsi_cls_session_printk(KERN_ERR, session,
  926. "Failure in Target ID Allocation\n");
  927. return id;
  928. }
  929. session->target_id = (unsigned int)id;
  930. session->ida_used = true;
  931. } else
  932. session->target_id = target_id;
  933. dev_set_name(&session->dev, "session%u", session->sid);
  934. err = device_add(&session->dev);
  935. if (err) {
  936. iscsi_cls_session_printk(KERN_ERR, session,
  937. "could not register session's dev\n");
  938. goto release_ida;
  939. }
  940. transport_register_device(&session->dev);
  941. spin_lock_irqsave(&sesslock, flags);
  942. list_add(&session->sess_list, &sesslist);
  943. spin_unlock_irqrestore(&sesslock, flags);
  944. iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
  945. ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
  946. return 0;
  947. release_ida:
  948. if (session->ida_used)
  949. ida_simple_remove(&iscsi_sess_ida, session->target_id);
  950. return err;
  951. }
  952. EXPORT_SYMBOL_GPL(iscsi_add_session);
  953. /**
  954. * iscsi_create_session - create iscsi class session
  955. * @shost: scsi host
  956. * @transport: iscsi transport
  957. * @dd_size: private driver data size
  958. * @target_id: which target
  959. *
  960. * This can be called from a LLD or iscsi_transport.
  961. */
  962. struct iscsi_cls_session *
  963. iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
  964. int dd_size, unsigned int target_id)
  965. {
  966. struct iscsi_cls_session *session;
  967. session = iscsi_alloc_session(shost, transport, dd_size);
  968. if (!session)
  969. return NULL;
  970. if (iscsi_add_session(session, target_id)) {
  971. iscsi_free_session(session);
  972. return NULL;
  973. }
  974. return session;
  975. }
  976. EXPORT_SYMBOL_GPL(iscsi_create_session);
  977. static void iscsi_conn_release(struct device *dev)
  978. {
  979. struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
  980. struct device *parent = conn->dev.parent;
  981. ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
  982. kfree(conn);
  983. put_device(parent);
  984. }
  985. static int iscsi_is_conn_dev(const struct device *dev)
  986. {
  987. return dev->release == iscsi_conn_release;
  988. }
  989. static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
  990. {
  991. if (!iscsi_is_conn_dev(dev))
  992. return 0;
  993. return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
  994. }
  995. void iscsi_remove_session(struct iscsi_cls_session *session)
  996. {
  997. struct Scsi_Host *shost = iscsi_session_to_shost(session);
  998. unsigned long flags;
  999. int err;
  1000. ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
  1001. spin_lock_irqsave(&sesslock, flags);
  1002. list_del(&session->sess_list);
  1003. spin_unlock_irqrestore(&sesslock, flags);
  1004. /* make sure there are no blocks/unblocks queued */
  1005. flush_workqueue(iscsi_eh_timer_workq);
  1006. /* make sure the timedout callout is not running */
  1007. if (!cancel_delayed_work(&session->recovery_work))
  1008. flush_workqueue(iscsi_eh_timer_workq);
  1009. /*
  1010. * If we are blocked let commands flow again. The lld or iscsi
  1011. * layer should set up the queuecommand to fail commands.
  1012. * We assume that LLD will not be calling block/unblock while
  1013. * removing the session.
  1014. */
  1015. spin_lock_irqsave(&session->lock, flags);
  1016. session->state = ISCSI_SESSION_FREE;
  1017. spin_unlock_irqrestore(&session->lock, flags);
  1018. scsi_target_unblock(&session->dev);
  1019. /* flush running scans then delete devices */
  1020. scsi_flush_work(shost);
  1021. __iscsi_unbind_session(&session->unbind_work);
  1022. /* hw iscsi may not have removed all connections from session */
  1023. err = device_for_each_child(&session->dev, NULL,
  1024. iscsi_iter_destroy_conn_fn);
  1025. if (err)
  1026. iscsi_cls_session_printk(KERN_ERR, session,
  1027. "Could not delete all connections "
  1028. "for session. Error %d.\n", err);
  1029. transport_unregister_device(&session->dev);
  1030. ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
  1031. device_del(&session->dev);
  1032. }
  1033. EXPORT_SYMBOL_GPL(iscsi_remove_session);
  1034. void iscsi_free_session(struct iscsi_cls_session *session)
  1035. {
  1036. ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
  1037. iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
  1038. put_device(&session->dev);
  1039. }
  1040. EXPORT_SYMBOL_GPL(iscsi_free_session);
  1041. /**
  1042. * iscsi_destroy_session - destroy iscsi session
  1043. * @session: iscsi_session
  1044. *
  1045. * Can be called by a LLD or iscsi_transport. There must not be
  1046. * any running connections.
  1047. */
  1048. int iscsi_destroy_session(struct iscsi_cls_session *session)
  1049. {
  1050. iscsi_remove_session(session);
  1051. ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
  1052. iscsi_free_session(session);
  1053. return 0;
  1054. }
  1055. EXPORT_SYMBOL_GPL(iscsi_destroy_session);
  1056. /**
  1057. * iscsi_create_conn - create iscsi class connection
  1058. * @session: iscsi cls session
  1059. * @dd_size: private driver data size
  1060. * @cid: connection id
  1061. *
  1062. * This can be called from a LLD or iscsi_transport. The connection
  1063. * is child of the session so cid must be unique for all connections
  1064. * on the session.
  1065. *
  1066. * Since we do not support MCS, cid will normally be zero. In some cases
  1067. * for software iscsi we could be trying to preallocate a connection struct
  1068. * in which case there could be two connection structs and cid would be
  1069. * non-zero.
  1070. */
  1071. struct iscsi_cls_conn *
  1072. iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
  1073. {
  1074. struct iscsi_transport *transport = session->transport;
  1075. struct iscsi_cls_conn *conn;
  1076. unsigned long flags;
  1077. int err;
  1078. conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
  1079. if (!conn)
  1080. return NULL;
  1081. if (dd_size)
  1082. conn->dd_data = &conn[1];
  1083. mutex_init(&conn->ep_mutex);
  1084. INIT_LIST_HEAD(&conn->conn_list);
  1085. conn->transport = transport;
  1086. conn->cid = cid;
  1087. /* this is released in the dev's release function */
  1088. if (!get_device(&session->dev))
  1089. goto free_conn;
  1090. dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
  1091. conn->dev.parent = &session->dev;
  1092. conn->dev.release = iscsi_conn_release;
  1093. err = device_register(&conn->dev);
  1094. if (err) {
  1095. iscsi_cls_session_printk(KERN_ERR, session, "could not "
  1096. "register connection's dev\n");
  1097. goto release_parent_ref;
  1098. }
  1099. transport_register_device(&conn->dev);
  1100. spin_lock_irqsave(&connlock, flags);
  1101. list_add(&conn->conn_list, &connlist);
  1102. spin_unlock_irqrestore(&connlock, flags);
  1103. ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
  1104. return conn;
  1105. release_parent_ref:
  1106. put_device(&session->dev);
  1107. free_conn:
  1108. kfree(conn);
  1109. return NULL;
  1110. }
  1111. EXPORT_SYMBOL_GPL(iscsi_create_conn);
  1112. /**
  1113. * iscsi_destroy_conn - destroy iscsi class connection
  1114. * @conn: iscsi cls session
  1115. *
  1116. * This can be called from a LLD or iscsi_transport.
  1117. */
  1118. int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
  1119. {
  1120. unsigned long flags;
  1121. spin_lock_irqsave(&connlock, flags);
  1122. list_del(&conn->conn_list);
  1123. spin_unlock_irqrestore(&connlock, flags);
  1124. transport_unregister_device(&conn->dev);
  1125. ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
  1126. device_unregister(&conn->dev);
  1127. return 0;
  1128. }
  1129. EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
  1130. /*
  1131. * iscsi interface functions
  1132. */
  1133. static struct iscsi_internal *
  1134. iscsi_if_transport_lookup(struct iscsi_transport *tt)
  1135. {
  1136. struct iscsi_internal *priv;
  1137. unsigned long flags;
  1138. spin_lock_irqsave(&iscsi_transport_lock, flags);
  1139. list_for_each_entry(priv, &iscsi_transports, list) {
  1140. if (tt == priv->iscsi_transport) {
  1141. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  1142. return priv;
  1143. }
  1144. }
  1145. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  1146. return NULL;
  1147. }
  1148. static int
  1149. iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
  1150. {
  1151. return nlmsg_multicast(nls, skb, 0, group, gfp);
  1152. }
  1153. int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
  1154. char *data, uint32_t data_size)
  1155. {
  1156. struct nlmsghdr *nlh;
  1157. struct sk_buff *skb;
  1158. struct iscsi_uevent *ev;
  1159. char *pdu;
  1160. struct iscsi_internal *priv;
  1161. int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
  1162. data_size);
  1163. priv = iscsi_if_transport_lookup(conn->transport);
  1164. if (!priv)
  1165. return -EINVAL;
  1166. skb = alloc_skb(len, GFP_ATOMIC);
  1167. if (!skb) {
  1168. iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
  1169. iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
  1170. "control PDU: OOM\n");
  1171. return -ENOMEM;
  1172. }
  1173. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  1174. ev = NLMSG_DATA(nlh);
  1175. memset(ev, 0, sizeof(*ev));
  1176. ev->transport_handle = iscsi_handle(conn->transport);
  1177. ev->type = ISCSI_KEVENT_RECV_PDU;
  1178. ev->r.recv_req.cid = conn->cid;
  1179. ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
  1180. pdu = (char*)ev + sizeof(*ev);
  1181. memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
  1182. memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
  1183. return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
  1184. }
  1185. EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
  1186. int iscsi_offload_mesg(struct Scsi_Host *shost,
  1187. struct iscsi_transport *transport, uint32_t type,
  1188. char *data, uint16_t data_size)
  1189. {
  1190. struct nlmsghdr *nlh;
  1191. struct sk_buff *skb;
  1192. struct iscsi_uevent *ev;
  1193. int len = NLMSG_SPACE(sizeof(*ev) + data_size);
  1194. skb = alloc_skb(len, GFP_ATOMIC);
  1195. if (!skb) {
  1196. printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
  1197. return -ENOMEM;
  1198. }
  1199. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  1200. ev = NLMSG_DATA(nlh);
  1201. memset(ev, 0, sizeof(*ev));
  1202. ev->type = type;
  1203. ev->transport_handle = iscsi_handle(transport);
  1204. switch (type) {
  1205. case ISCSI_KEVENT_PATH_REQ:
  1206. ev->r.req_path.host_no = shost->host_no;
  1207. break;
  1208. case ISCSI_KEVENT_IF_DOWN:
  1209. ev->r.notify_if_down.host_no = shost->host_no;
  1210. break;
  1211. }
  1212. memcpy((char *)ev + sizeof(*ev), data, data_size);
  1213. return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
  1214. }
  1215. EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
  1216. void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
  1217. {
  1218. struct nlmsghdr *nlh;
  1219. struct sk_buff *skb;
  1220. struct iscsi_uevent *ev;
  1221. struct iscsi_internal *priv;
  1222. int len = NLMSG_SPACE(sizeof(*ev));
  1223. priv = iscsi_if_transport_lookup(conn->transport);
  1224. if (!priv)
  1225. return;
  1226. skb = alloc_skb(len, GFP_ATOMIC);
  1227. if (!skb) {
  1228. iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
  1229. "conn error (%d)\n", error);
  1230. return;
  1231. }
  1232. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  1233. ev = NLMSG_DATA(nlh);
  1234. ev->transport_handle = iscsi_handle(conn->transport);
  1235. ev->type = ISCSI_KEVENT_CONN_ERROR;
  1236. ev->r.connerror.error = error;
  1237. ev->r.connerror.cid = conn->cid;
  1238. ev->r.connerror.sid = iscsi_conn_get_sid(conn);
  1239. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
  1240. iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
  1241. error);
  1242. }
  1243. EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
  1244. void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
  1245. enum iscsi_conn_state state)
  1246. {
  1247. struct nlmsghdr *nlh;
  1248. struct sk_buff *skb;
  1249. struct iscsi_uevent *ev;
  1250. struct iscsi_internal *priv;
  1251. int len = NLMSG_SPACE(sizeof(*ev));
  1252. priv = iscsi_if_transport_lookup(conn->transport);
  1253. if (!priv)
  1254. return;
  1255. skb = alloc_skb(len, GFP_ATOMIC);
  1256. if (!skb) {
  1257. iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
  1258. "conn login (%d)\n", state);
  1259. return;
  1260. }
  1261. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  1262. ev = NLMSG_DATA(nlh);
  1263. ev->transport_handle = iscsi_handle(conn->transport);
  1264. ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
  1265. ev->r.conn_login.state = state;
  1266. ev->r.conn_login.cid = conn->cid;
  1267. ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
  1268. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
  1269. iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
  1270. state);
  1271. }
  1272. EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
  1273. void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
  1274. enum iscsi_host_event_code code, uint32_t data_size,
  1275. uint8_t *data)
  1276. {
  1277. struct nlmsghdr *nlh;
  1278. struct sk_buff *skb;
  1279. struct iscsi_uevent *ev;
  1280. int len = NLMSG_SPACE(sizeof(*ev) + data_size);
  1281. skb = alloc_skb(len, GFP_NOIO);
  1282. if (!skb) {
  1283. printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
  1284. host_no, code);
  1285. return;
  1286. }
  1287. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  1288. ev = NLMSG_DATA(nlh);
  1289. ev->transport_handle = iscsi_handle(transport);
  1290. ev->type = ISCSI_KEVENT_HOST_EVENT;
  1291. ev->r.host_event.host_no = host_no;
  1292. ev->r.host_event.code = code;
  1293. ev->r.host_event.data_size = data_size;
  1294. if (data_size)
  1295. memcpy((char *)ev + sizeof(*ev), data, data_size);
  1296. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
  1297. }
  1298. EXPORT_SYMBOL_GPL(iscsi_post_host_event);
  1299. void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
  1300. uint32_t status, uint32_t pid, uint32_t data_size,
  1301. uint8_t *data)
  1302. {
  1303. struct nlmsghdr *nlh;
  1304. struct sk_buff *skb;
  1305. struct iscsi_uevent *ev;
  1306. int len = NLMSG_SPACE(sizeof(*ev) + data_size);
  1307. skb = alloc_skb(len, GFP_NOIO);
  1308. if (!skb) {
  1309. printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
  1310. return;
  1311. }
  1312. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  1313. ev = NLMSG_DATA(nlh);
  1314. ev->transport_handle = iscsi_handle(transport);
  1315. ev->type = ISCSI_KEVENT_PING_COMP;
  1316. ev->r.ping_comp.host_no = host_no;
  1317. ev->r.ping_comp.status = status;
  1318. ev->r.ping_comp.pid = pid;
  1319. ev->r.ping_comp.data_size = data_size;
  1320. memcpy((char *)ev + sizeof(*ev), data, data_size);
  1321. iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
  1322. }
  1323. EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
  1324. static int
  1325. iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
  1326. void *payload, int size)
  1327. {
  1328. struct sk_buff *skb;
  1329. struct nlmsghdr *nlh;
  1330. int len = NLMSG_SPACE(size);
  1331. int flags = multi ? NLM_F_MULTI : 0;
  1332. int t = done ? NLMSG_DONE : type;
  1333. skb = alloc_skb(len, GFP_ATOMIC);
  1334. if (!skb) {
  1335. printk(KERN_ERR "Could not allocate skb to send reply.\n");
  1336. return -ENOMEM;
  1337. }
  1338. nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
  1339. nlh->nlmsg_flags = flags;
  1340. memcpy(NLMSG_DATA(nlh), payload, size);
  1341. return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
  1342. }
  1343. static int
  1344. iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
  1345. {
  1346. struct iscsi_uevent *ev = NLMSG_DATA(nlh);
  1347. struct iscsi_stats *stats;
  1348. struct sk_buff *skbstat;
  1349. struct iscsi_cls_conn *conn;
  1350. struct nlmsghdr *nlhstat;
  1351. struct iscsi_uevent *evstat;
  1352. struct iscsi_internal *priv;
  1353. int len = NLMSG_SPACE(sizeof(*ev) +
  1354. sizeof(struct iscsi_stats) +
  1355. sizeof(struct iscsi_stats_custom) *
  1356. ISCSI_STATS_CUSTOM_MAX);
  1357. int err = 0;
  1358. priv = iscsi_if_transport_lookup(transport);
  1359. if (!priv)
  1360. return -EINVAL;
  1361. conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
  1362. if (!conn)
  1363. return -EEXIST;
  1364. do {
  1365. int actual_size;
  1366. skbstat = alloc_skb(len, GFP_ATOMIC);
  1367. if (!skbstat) {
  1368. iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
  1369. "deliver stats: OOM\n");
  1370. return -ENOMEM;
  1371. }
  1372. nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
  1373. (len - sizeof(*nlhstat)), 0);
  1374. evstat = NLMSG_DATA(nlhstat);
  1375. memset(evstat, 0, sizeof(*evstat));
  1376. evstat->transport_handle = iscsi_handle(conn->transport);
  1377. evstat->type = nlh->nlmsg_type;
  1378. evstat->u.get_stats.cid =
  1379. ev->u.get_stats.cid;
  1380. evstat->u.get_stats.sid =
  1381. ev->u.get_stats.sid;
  1382. stats = (struct iscsi_stats *)
  1383. ((char*)evstat + sizeof(*evstat));
  1384. memset(stats, 0, sizeof(*stats));
  1385. transport->get_stats(conn, stats);
  1386. actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
  1387. sizeof(struct iscsi_stats) +
  1388. sizeof(struct iscsi_stats_custom) *
  1389. stats->custom_length);
  1390. actual_size -= sizeof(*nlhstat);
  1391. actual_size = NLMSG_LENGTH(actual_size);
  1392. skb_trim(skbstat, NLMSG_ALIGN(actual_size));
  1393. nlhstat->nlmsg_len = actual_size;
  1394. err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
  1395. GFP_ATOMIC);
  1396. } while (err < 0 && err != -ECONNREFUSED);
  1397. return err;
  1398. }
  1399. /**
  1400. * iscsi_session_event - send session destr. completion event
  1401. * @session: iscsi class session
  1402. * @event: type of event
  1403. */
  1404. int iscsi_session_event(struct iscsi_cls_session *session,
  1405. enum iscsi_uevent_e event)
  1406. {
  1407. struct iscsi_internal *priv;
  1408. struct Scsi_Host *shost;
  1409. struct iscsi_uevent *ev;
  1410. struct sk_buff *skb;
  1411. struct nlmsghdr *nlh;
  1412. int rc, len = NLMSG_SPACE(sizeof(*ev));
  1413. priv = iscsi_if_transport_lookup(session->transport);
  1414. if (!priv)
  1415. return -EINVAL;
  1416. shost = iscsi_session_to_shost(session);
  1417. skb = alloc_skb(len, GFP_KERNEL);
  1418. if (!skb) {
  1419. iscsi_cls_session_printk(KERN_ERR, session,
  1420. "Cannot notify userspace of session "
  1421. "event %u\n", event);
  1422. return -ENOMEM;
  1423. }
  1424. nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
  1425. ev = NLMSG_DATA(nlh);
  1426. ev->transport_handle = iscsi_handle(session->transport);
  1427. ev->type = event;
  1428. switch (event) {
  1429. case ISCSI_KEVENT_DESTROY_SESSION:
  1430. ev->r.d_session.host_no = shost->host_no;
  1431. ev->r.d_session.sid = session->sid;
  1432. break;
  1433. case ISCSI_KEVENT_CREATE_SESSION:
  1434. ev->r.c_session_ret.host_no = shost->host_no;
  1435. ev->r.c_session_ret.sid = session->sid;
  1436. break;
  1437. case ISCSI_KEVENT_UNBIND_SESSION:
  1438. ev->r.unbind_session.host_no = shost->host_no;
  1439. ev->r.unbind_session.sid = session->sid;
  1440. break;
  1441. default:
  1442. iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
  1443. "%u.\n", event);
  1444. kfree_skb(skb);
  1445. return -EINVAL;
  1446. }
  1447. /*
  1448. * this will occur if the daemon is not up, so we just warn
  1449. * the user and when the daemon is restarted it will handle it
  1450. */
  1451. rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
  1452. if (rc == -ESRCH)
  1453. iscsi_cls_session_printk(KERN_ERR, session,
  1454. "Cannot notify userspace of session "
  1455. "event %u. Check iscsi daemon\n",
  1456. event);
  1457. ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
  1458. event, rc);
  1459. return rc;
  1460. }
  1461. EXPORT_SYMBOL_GPL(iscsi_session_event);
  1462. static int
  1463. iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
  1464. struct iscsi_uevent *ev, pid_t pid,
  1465. uint32_t initial_cmdsn, uint16_t cmds_max,
  1466. uint16_t queue_depth)
  1467. {
  1468. struct iscsi_transport *transport = priv->iscsi_transport;
  1469. struct iscsi_cls_session *session;
  1470. struct Scsi_Host *shost;
  1471. session = transport->create_session(ep, cmds_max, queue_depth,
  1472. initial_cmdsn);
  1473. if (!session)
  1474. return -ENOMEM;
  1475. session->creator = pid;
  1476. shost = iscsi_session_to_shost(session);
  1477. ev->r.c_session_ret.host_no = shost->host_no;
  1478. ev->r.c_session_ret.sid = session->sid;
  1479. ISCSI_DBG_TRANS_SESSION(session,
  1480. "Completed creating transport session\n");
  1481. return 0;
  1482. }
  1483. static int
  1484. iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  1485. {
  1486. struct iscsi_cls_conn *conn;
  1487. struct iscsi_cls_session *session;
  1488. session = iscsi_session_lookup(ev->u.c_conn.sid);
  1489. if (!session) {
  1490. printk(KERN_ERR "iscsi: invalid session %d.\n",
  1491. ev->u.c_conn.sid);
  1492. return -EINVAL;
  1493. }
  1494. conn = transport->create_conn(session, ev->u.c_conn.cid);
  1495. if (!conn) {
  1496. iscsi_cls_session_printk(KERN_ERR, session,
  1497. "couldn't create a new connection.");
  1498. return -ENOMEM;
  1499. }
  1500. ev->r.c_conn_ret.sid = session->sid;
  1501. ev->r.c_conn_ret.cid = conn->cid;
  1502. ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
  1503. return 0;
  1504. }
  1505. static int
  1506. iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  1507. {
  1508. struct iscsi_cls_conn *conn;
  1509. conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
  1510. if (!conn)
  1511. return -EINVAL;
  1512. ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
  1513. if (transport->destroy_conn)
  1514. transport->destroy_conn(conn);
  1515. return 0;
  1516. }
  1517. static int
  1518. iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  1519. {
  1520. char *data = (char*)ev + sizeof(*ev);
  1521. struct iscsi_cls_conn *conn;
  1522. struct iscsi_cls_session *session;
  1523. int err = 0, value = 0;
  1524. session = iscsi_session_lookup(ev->u.set_param.sid);
  1525. conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
  1526. if (!conn || !session)
  1527. return -EINVAL;
  1528. switch (ev->u.set_param.param) {
  1529. case ISCSI_PARAM_SESS_RECOVERY_TMO:
  1530. sscanf(data, "%d", &value);
  1531. session->recovery_tmo = value;
  1532. break;
  1533. default:
  1534. err = transport->set_param(conn, ev->u.set_param.param,
  1535. data, ev->u.set_param.len);
  1536. }
  1537. return err;
  1538. }
  1539. static int iscsi_if_ep_connect(struct iscsi_transport *transport,
  1540. struct iscsi_uevent *ev, int msg_type)
  1541. {
  1542. struct iscsi_endpoint *ep;
  1543. struct sockaddr *dst_addr;
  1544. struct Scsi_Host *shost = NULL;
  1545. int non_blocking, err = 0;
  1546. if (!transport->ep_connect)
  1547. return -EINVAL;
  1548. if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
  1549. shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
  1550. if (!shost) {
  1551. printk(KERN_ERR "ep connect failed. Could not find "
  1552. "host no %u\n",
  1553. ev->u.ep_connect_through_host.host_no);
  1554. return -ENODEV;
  1555. }
  1556. non_blocking = ev->u.ep_connect_through_host.non_blocking;
  1557. } else
  1558. non_blocking = ev->u.ep_connect.non_blocking;
  1559. dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
  1560. ep = transport->ep_connect(shost, dst_addr, non_blocking);
  1561. if (IS_ERR(ep)) {
  1562. err = PTR_ERR(ep);
  1563. goto release_host;
  1564. }
  1565. ev->r.ep_connect_ret.handle = ep->id;
  1566. release_host:
  1567. if (shost)
  1568. scsi_host_put(shost);
  1569. return err;
  1570. }
  1571. static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
  1572. u64 ep_handle)
  1573. {
  1574. struct iscsi_cls_conn *conn;
  1575. struct iscsi_endpoint *ep;
  1576. if (!transport->ep_disconnect)
  1577. return -EINVAL;
  1578. ep = iscsi_lookup_endpoint(ep_handle);
  1579. if (!ep)
  1580. return -EINVAL;
  1581. conn = ep->conn;
  1582. if (conn) {
  1583. mutex_lock(&conn->ep_mutex);
  1584. conn->ep = NULL;
  1585. mutex_unlock(&conn->ep_mutex);
  1586. }
  1587. transport->ep_disconnect(ep);
  1588. return 0;
  1589. }
  1590. static int
  1591. iscsi_if_transport_ep(struct iscsi_transport *transport,
  1592. struct iscsi_uevent *ev, int msg_type)
  1593. {
  1594. struct iscsi_endpoint *ep;
  1595. int rc = 0;
  1596. switch (msg_type) {
  1597. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
  1598. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
  1599. rc = iscsi_if_ep_connect(transport, ev, msg_type);
  1600. break;
  1601. case ISCSI_UEVENT_TRANSPORT_EP_POLL:
  1602. if (!transport->ep_poll)
  1603. return -EINVAL;
  1604. ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
  1605. if (!ep)
  1606. return -EINVAL;
  1607. ev->r.retcode = transport->ep_poll(ep,
  1608. ev->u.ep_poll.timeout_ms);
  1609. break;
  1610. case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
  1611. rc = iscsi_if_ep_disconnect(transport,
  1612. ev->u.ep_disconnect.ep_handle);
  1613. break;
  1614. }
  1615. return rc;
  1616. }
  1617. static int
  1618. iscsi_tgt_dscvr(struct iscsi_transport *transport,
  1619. struct iscsi_uevent *ev)
  1620. {
  1621. struct Scsi_Host *shost;
  1622. struct sockaddr *dst_addr;
  1623. int err;
  1624. if (!transport->tgt_dscvr)
  1625. return -EINVAL;
  1626. shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
  1627. if (!shost) {
  1628. printk(KERN_ERR "target discovery could not find host no %u\n",
  1629. ev->u.tgt_dscvr.host_no);
  1630. return -ENODEV;
  1631. }
  1632. dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
  1633. err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
  1634. ev->u.tgt_dscvr.enable, dst_addr);
  1635. scsi_host_put(shost);
  1636. return err;
  1637. }
  1638. static int
  1639. iscsi_set_host_param(struct iscsi_transport *transport,
  1640. struct iscsi_uevent *ev)
  1641. {
  1642. char *data = (char*)ev + sizeof(*ev);
  1643. struct Scsi_Host *shost;
  1644. int err;
  1645. if (!transport->set_host_param)
  1646. return -ENOSYS;
  1647. shost = scsi_host_lookup(ev->u.set_host_param.host_no);
  1648. if (!shost) {
  1649. printk(KERN_ERR "set_host_param could not find host no %u\n",
  1650. ev->u.set_host_param.host_no);
  1651. return -ENODEV;
  1652. }
  1653. err = transport->set_host_param(shost, ev->u.set_host_param.param,
  1654. data, ev->u.set_host_param.len);
  1655. scsi_host_put(shost);
  1656. return err;
  1657. }
  1658. static int
  1659. iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  1660. {
  1661. struct Scsi_Host *shost;
  1662. struct iscsi_path *params;
  1663. int err;
  1664. if (!transport->set_path)
  1665. return -ENOSYS;
  1666. shost = scsi_host_lookup(ev->u.set_path.host_no);
  1667. if (!shost) {
  1668. printk(KERN_ERR "set path could not find host no %u\n",
  1669. ev->u.set_path.host_no);
  1670. return -ENODEV;
  1671. }
  1672. params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
  1673. err = transport->set_path(shost, params);
  1674. scsi_host_put(shost);
  1675. return err;
  1676. }
  1677. static int
  1678. iscsi_set_iface_params(struct iscsi_transport *transport,
  1679. struct iscsi_uevent *ev, uint32_t len)
  1680. {
  1681. char *data = (char *)ev + sizeof(*ev);
  1682. struct Scsi_Host *shost;
  1683. int err;
  1684. if (!transport->set_iface_param)
  1685. return -ENOSYS;
  1686. shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
  1687. if (!shost) {
  1688. printk(KERN_ERR "set_iface_params could not find host no %u\n",
  1689. ev->u.set_iface_params.host_no);
  1690. return -ENODEV;
  1691. }
  1692. err = transport->set_iface_param(shost, data, len);
  1693. scsi_host_put(shost);
  1694. return err;
  1695. }
  1696. static int
  1697. iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
  1698. {
  1699. struct Scsi_Host *shost;
  1700. struct sockaddr *dst_addr;
  1701. int err;
  1702. if (!transport->send_ping)
  1703. return -ENOSYS;
  1704. shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
  1705. if (!shost) {
  1706. printk(KERN_ERR "iscsi_ping could not find host no %u\n",
  1707. ev->u.iscsi_ping.host_no);
  1708. return -ENODEV;
  1709. }
  1710. dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
  1711. err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
  1712. ev->u.iscsi_ping.iface_type,
  1713. ev->u.iscsi_ping.payload_size,
  1714. ev->u.iscsi_ping.pid,
  1715. dst_addr);
  1716. scsi_host_put(shost);
  1717. return err;
  1718. }
  1719. static int
  1720. iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
  1721. {
  1722. struct iscsi_uevent *ev = NLMSG_DATA(nlh);
  1723. struct Scsi_Host *shost = NULL;
  1724. struct iscsi_chap_rec *chap_rec;
  1725. struct iscsi_internal *priv;
  1726. struct sk_buff *skbchap;
  1727. struct nlmsghdr *nlhchap;
  1728. struct iscsi_uevent *evchap;
  1729. uint32_t chap_buf_size;
  1730. int len, err = 0;
  1731. char *buf;
  1732. if (!transport->get_chap)
  1733. return -EINVAL;
  1734. priv = iscsi_if_transport_lookup(transport);
  1735. if (!priv)
  1736. return -EINVAL;
  1737. chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
  1738. len = NLMSG_SPACE(sizeof(*ev) + chap_buf_size);
  1739. shost = scsi_host_lookup(ev->u.get_chap.host_no);
  1740. if (!shost) {
  1741. printk(KERN_ERR "%s: failed. Cound not find host no %u\n",
  1742. __func__, ev->u.get_chap.host_no);
  1743. return -ENODEV;
  1744. }
  1745. do {
  1746. int actual_size;
  1747. skbchap = alloc_skb(len, GFP_KERNEL);
  1748. if (!skbchap) {
  1749. printk(KERN_ERR "can not deliver chap: OOM\n");
  1750. err = -ENOMEM;
  1751. goto exit_get_chap;
  1752. }
  1753. nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
  1754. (len - sizeof(*nlhchap)), 0);
  1755. evchap = NLMSG_DATA(nlhchap);
  1756. memset(evchap, 0, sizeof(*evchap));
  1757. evchap->transport_handle = iscsi_handle(transport);
  1758. evchap->type = nlh->nlmsg_type;
  1759. evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
  1760. evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
  1761. evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
  1762. buf = (char *) ((char *)evchap + sizeof(*evchap));
  1763. memset(buf, 0, chap_buf_size);
  1764. err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
  1765. &evchap->u.get_chap.num_entries, buf);
  1766. actual_size = NLMSG_SPACE(sizeof(*ev) + chap_buf_size);
  1767. skb_trim(skbchap, NLMSG_ALIGN(actual_size));
  1768. nlhchap->nlmsg_len = actual_size;
  1769. err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
  1770. GFP_KERNEL);
  1771. } while (err < 0 && err != -ECONNREFUSED);
  1772. exit_get_chap:
  1773. scsi_host_put(shost);
  1774. return err;
  1775. }
  1776. static int iscsi_delete_chap(struct iscsi_transport *transport,
  1777. struct iscsi_uevent *ev)
  1778. {
  1779. struct Scsi_Host *shost;
  1780. int err = 0;
  1781. if (!transport->delete_chap)
  1782. return -ENOSYS;
  1783. shost = scsi_host_lookup(ev->u.delete_chap.host_no);
  1784. if (!shost) {
  1785. printk(KERN_ERR "%s could not find host no %u\n",
  1786. __func__, ev->u.delete_chap.host_no);
  1787. return -ENODEV;
  1788. }
  1789. err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
  1790. scsi_host_put(shost);
  1791. return err;
  1792. }
  1793. static int
  1794. iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
  1795. {
  1796. int err = 0;
  1797. struct iscsi_uevent *ev = NLMSG_DATA(nlh);
  1798. struct iscsi_transport *transport = NULL;
  1799. struct iscsi_internal *priv;
  1800. struct iscsi_cls_session *session;
  1801. struct iscsi_cls_conn *conn;
  1802. struct iscsi_endpoint *ep = NULL;
  1803. if (!netlink_capable(skb, CAP_SYS_ADMIN))
  1804. return -EPERM;
  1805. if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
  1806. *group = ISCSI_NL_GRP_UIP;
  1807. else
  1808. *group = ISCSI_NL_GRP_ISCSID;
  1809. priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
  1810. if (!priv)
  1811. return -EINVAL;
  1812. transport = priv->iscsi_transport;
  1813. if (!try_module_get(transport->owner))
  1814. return -EINVAL;
  1815. switch (nlh->nlmsg_type) {
  1816. case ISCSI_UEVENT_CREATE_SESSION:
  1817. err = iscsi_if_create_session(priv, ep, ev,
  1818. NETLINK_CB(skb).pid,
  1819. ev->u.c_session.initial_cmdsn,
  1820. ev->u.c_session.cmds_max,
  1821. ev->u.c_session.queue_depth);
  1822. break;
  1823. case ISCSI_UEVENT_CREATE_BOUND_SESSION:
  1824. ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
  1825. if (!ep) {
  1826. err = -EINVAL;
  1827. break;
  1828. }
  1829. err = iscsi_if_create_session(priv, ep, ev,
  1830. NETLINK_CB(skb).pid,
  1831. ev->u.c_bound_session.initial_cmdsn,
  1832. ev->u.c_bound_session.cmds_max,
  1833. ev->u.c_bound_session.queue_depth);
  1834. break;
  1835. case ISCSI_UEVENT_DESTROY_SESSION:
  1836. session = iscsi_session_lookup(ev->u.d_session.sid);
  1837. if (session)
  1838. transport->destroy_session(session);
  1839. else
  1840. err = -EINVAL;
  1841. break;
  1842. case ISCSI_UEVENT_UNBIND_SESSION:
  1843. session = iscsi_session_lookup(ev->u.d_session.sid);
  1844. if (session)
  1845. scsi_queue_work(iscsi_session_to_shost(session),
  1846. &session->unbind_work);
  1847. else
  1848. err = -EINVAL;
  1849. break;
  1850. case ISCSI_UEVENT_CREATE_CONN:
  1851. err = iscsi_if_create_conn(transport, ev);
  1852. break;
  1853. case ISCSI_UEVENT_DESTROY_CONN:
  1854. err = iscsi_if_destroy_conn(transport, ev);
  1855. break;
  1856. case ISCSI_UEVENT_BIND_CONN:
  1857. session = iscsi_session_lookup(ev->u.b_conn.sid);
  1858. conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
  1859. if (conn && conn->ep)
  1860. iscsi_if_ep_disconnect(transport, conn->ep->id);
  1861. if (!session || !conn) {
  1862. err = -EINVAL;
  1863. break;
  1864. }
  1865. ev->r.retcode = transport->bind_conn(session, conn,
  1866. ev->u.b_conn.transport_eph,
  1867. ev->u.b_conn.is_leading);
  1868. if (ev->r.retcode || !transport->ep_connect)
  1869. break;
  1870. ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
  1871. if (ep) {
  1872. ep->conn = conn;
  1873. mutex_lock(&conn->ep_mutex);
  1874. conn->ep = ep;
  1875. mutex_unlock(&conn->ep_mutex);
  1876. } else
  1877. iscsi_cls_conn_printk(KERN_ERR, conn,
  1878. "Could not set ep conn "
  1879. "binding\n");
  1880. break;
  1881. case ISCSI_UEVENT_SET_PARAM:
  1882. err = iscsi_set_param(transport, ev);
  1883. break;
  1884. case ISCSI_UEVENT_START_CONN:
  1885. conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
  1886. if (conn)
  1887. ev->r.retcode = transport->start_conn(conn);
  1888. else
  1889. err = -EINVAL;
  1890. break;
  1891. case ISCSI_UEVENT_STOP_CONN:
  1892. conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
  1893. if (conn)
  1894. transport->stop_conn(conn, ev->u.stop_conn.flag);
  1895. else
  1896. err = -EINVAL;
  1897. break;
  1898. case ISCSI_UEVENT_SEND_PDU:
  1899. conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
  1900. if (conn)
  1901. ev->r.retcode = transport->send_pdu(conn,
  1902. (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
  1903. (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
  1904. ev->u.send_pdu.data_size);
  1905. else
  1906. err = -EINVAL;
  1907. break;
  1908. case ISCSI_UEVENT_GET_STATS:
  1909. err = iscsi_if_get_stats(transport, nlh);
  1910. break;
  1911. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
  1912. case ISCSI_UEVENT_TRANSPORT_EP_POLL:
  1913. case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
  1914. case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
  1915. err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
  1916. break;
  1917. case ISCSI_UEVENT_TGT_DSCVR:
  1918. err = iscsi_tgt_dscvr(transport, ev);
  1919. break;
  1920. case ISCSI_UEVENT_SET_HOST_PARAM:
  1921. err = iscsi_set_host_param(transport, ev);
  1922. break;
  1923. case ISCSI_UEVENT_PATH_UPDATE:
  1924. err = iscsi_set_path(transport, ev);
  1925. break;
  1926. case ISCSI_UEVENT_SET_IFACE_PARAMS:
  1927. err = iscsi_set_iface_params(transport, ev,
  1928. nlmsg_attrlen(nlh, sizeof(*ev)));
  1929. break;
  1930. case ISCSI_UEVENT_PING:
  1931. err = iscsi_send_ping(transport, ev);
  1932. break;
  1933. case ISCSI_UEVENT_GET_CHAP:
  1934. err = iscsi_get_chap(transport, nlh);
  1935. break;
  1936. case ISCSI_UEVENT_DELETE_CHAP:
  1937. err = iscsi_delete_chap(transport, ev);
  1938. break;
  1939. default:
  1940. err = -ENOSYS;
  1941. break;
  1942. }
  1943. module_put(transport->owner);
  1944. return err;
  1945. }
  1946. /*
  1947. * Get message from skb. Each message is processed by iscsi_if_recv_msg.
  1948. * Malformed skbs with wrong lengths or invalid creds are not processed.
  1949. */
  1950. static void
  1951. iscsi_if_rx(struct sk_buff *skb)
  1952. {
  1953. mutex_lock(&rx_queue_mutex);
  1954. while (skb->len >= NLMSG_SPACE(0)) {
  1955. int err;
  1956. uint32_t rlen;
  1957. struct nlmsghdr *nlh;
  1958. struct iscsi_uevent *ev;
  1959. uint32_t group;
  1960. nlh = nlmsg_hdr(skb);
  1961. if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(*ev) ||
  1962. skb->len < nlh->nlmsg_len) {
  1963. break;
  1964. }
  1965. ev = NLMSG_DATA(nlh);
  1966. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  1967. if (rlen > skb->len)
  1968. rlen = skb->len;
  1969. err = iscsi_if_recv_msg(skb, nlh, &group);
  1970. if (err) {
  1971. ev->type = ISCSI_KEVENT_IF_ERROR;
  1972. ev->iferror = err;
  1973. }
  1974. do {
  1975. /*
  1976. * special case for GET_STATS:
  1977. * on success - sending reply and stats from
  1978. * inside of if_recv_msg(),
  1979. * on error - fall through.
  1980. */
  1981. if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
  1982. break;
  1983. if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
  1984. break;
  1985. err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
  1986. nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
  1987. } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
  1988. skb_pull(skb, rlen);
  1989. }
  1990. mutex_unlock(&rx_queue_mutex);
  1991. }
  1992. #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
  1993. struct device_attribute dev_attr_##_prefix##_##_name = \
  1994. __ATTR(_name,_mode,_show,_store)
  1995. /*
  1996. * iSCSI connection attrs
  1997. */
  1998. #define iscsi_conn_attr_show(param) \
  1999. static ssize_t \
  2000. show_conn_param_##param(struct device *dev, \
  2001. struct device_attribute *attr, char *buf) \
  2002. { \
  2003. struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
  2004. struct iscsi_transport *t = conn->transport; \
  2005. return t->get_conn_param(conn, param, buf); \
  2006. }
  2007. #define iscsi_conn_attr(field, param) \
  2008. iscsi_conn_attr_show(param) \
  2009. static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
  2010. NULL);
  2011. iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
  2012. iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
  2013. iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
  2014. iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
  2015. iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
  2016. iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
  2017. iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
  2018. iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
  2019. iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
  2020. iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
  2021. iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
  2022. #define iscsi_conn_ep_attr_show(param) \
  2023. static ssize_t show_conn_ep_param_##param(struct device *dev, \
  2024. struct device_attribute *attr,\
  2025. char *buf) \
  2026. { \
  2027. struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
  2028. struct iscsi_transport *t = conn->transport; \
  2029. struct iscsi_endpoint *ep; \
  2030. ssize_t rc; \
  2031. \
  2032. /* \
  2033. * Need to make sure ep_disconnect does not free the LLD's \
  2034. * interconnect resources while we are trying to read them. \
  2035. */ \
  2036. mutex_lock(&conn->ep_mutex); \
  2037. ep = conn->ep; \
  2038. if (!ep && t->ep_connect) { \
  2039. mutex_unlock(&conn->ep_mutex); \
  2040. return -ENOTCONN; \
  2041. } \
  2042. \
  2043. if (ep) \
  2044. rc = t->get_ep_param(ep, param, buf); \
  2045. else \
  2046. rc = t->get_conn_param(conn, param, buf); \
  2047. mutex_unlock(&conn->ep_mutex); \
  2048. return rc; \
  2049. }
  2050. #define iscsi_conn_ep_attr(field, param) \
  2051. iscsi_conn_ep_attr_show(param) \
  2052. static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
  2053. show_conn_ep_param_##param, NULL);
  2054. iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
  2055. iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
  2056. static struct attribute *iscsi_conn_attrs[] = {
  2057. &dev_attr_conn_max_recv_dlength.attr,
  2058. &dev_attr_conn_max_xmit_dlength.attr,
  2059. &dev_attr_conn_header_digest.attr,
  2060. &dev_attr_conn_data_digest.attr,
  2061. &dev_attr_conn_ifmarker.attr,
  2062. &dev_attr_conn_ofmarker.attr,
  2063. &dev_attr_conn_address.attr,
  2064. &dev_attr_conn_port.attr,
  2065. &dev_attr_conn_exp_statsn.attr,
  2066. &dev_attr_conn_persistent_address.attr,
  2067. &dev_attr_conn_persistent_port.attr,
  2068. &dev_attr_conn_ping_tmo.attr,
  2069. &dev_attr_conn_recv_tmo.attr,
  2070. NULL,
  2071. };
  2072. static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
  2073. struct attribute *attr, int i)
  2074. {
  2075. struct device *cdev = container_of(kobj, struct device, kobj);
  2076. struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
  2077. struct iscsi_transport *t = conn->transport;
  2078. int param;
  2079. if (attr == &dev_attr_conn_max_recv_dlength.attr)
  2080. param = ISCSI_PARAM_MAX_RECV_DLENGTH;
  2081. else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
  2082. param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
  2083. else if (attr == &dev_attr_conn_header_digest.attr)
  2084. param = ISCSI_PARAM_HDRDGST_EN;
  2085. else if (attr == &dev_attr_conn_data_digest.attr)
  2086. param = ISCSI_PARAM_DATADGST_EN;
  2087. else if (attr == &dev_attr_conn_ifmarker.attr)
  2088. param = ISCSI_PARAM_IFMARKER_EN;
  2089. else if (attr == &dev_attr_conn_ofmarker.attr)
  2090. param = ISCSI_PARAM_OFMARKER_EN;
  2091. else if (attr == &dev_attr_conn_address.attr)
  2092. param = ISCSI_PARAM_CONN_ADDRESS;
  2093. else if (attr == &dev_attr_conn_port.attr)
  2094. param = ISCSI_PARAM_CONN_PORT;
  2095. else if (attr == &dev_attr_conn_exp_statsn.attr)
  2096. param = ISCSI_PARAM_EXP_STATSN;
  2097. else if (attr == &dev_attr_conn_persistent_address.attr)
  2098. param = ISCSI_PARAM_PERSISTENT_ADDRESS;
  2099. else if (attr == &dev_attr_conn_persistent_port.attr)
  2100. param = ISCSI_PARAM_PERSISTENT_PORT;
  2101. else if (attr == &dev_attr_conn_ping_tmo.attr)
  2102. param = ISCSI_PARAM_PING_TMO;
  2103. else if (attr == &dev_attr_conn_recv_tmo.attr)
  2104. param = ISCSI_PARAM_RECV_TMO;
  2105. else {
  2106. WARN_ONCE(1, "Invalid conn attr");
  2107. return 0;
  2108. }
  2109. return t->attr_is_visible(ISCSI_PARAM, param);
  2110. }
  2111. static struct attribute_group iscsi_conn_group = {
  2112. .attrs = iscsi_conn_attrs,
  2113. .is_visible = iscsi_conn_attr_is_visible,
  2114. };
  2115. /*
  2116. * iSCSI session attrs
  2117. */
  2118. #define iscsi_session_attr_show(param, perm) \
  2119. static ssize_t \
  2120. show_session_param_##param(struct device *dev, \
  2121. struct device_attribute *attr, char *buf) \
  2122. { \
  2123. struct iscsi_cls_session *session = \
  2124. iscsi_dev_to_session(dev->parent); \
  2125. struct iscsi_transport *t = session->transport; \
  2126. \
  2127. if (perm && !capable(CAP_SYS_ADMIN)) \
  2128. return -EACCES; \
  2129. return t->get_session_param(session, param, buf); \
  2130. }
  2131. #define iscsi_session_attr(field, param, perm) \
  2132. iscsi_session_attr_show(param, perm) \
  2133. static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
  2134. NULL);
  2135. iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
  2136. iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
  2137. iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
  2138. iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
  2139. iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
  2140. iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
  2141. iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
  2142. iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
  2143. iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
  2144. iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
  2145. iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
  2146. iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
  2147. iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
  2148. iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
  2149. iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
  2150. iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
  2151. iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
  2152. iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
  2153. iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
  2154. iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
  2155. iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
  2156. iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
  2157. iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
  2158. static ssize_t
  2159. show_priv_session_state(struct device *dev, struct device_attribute *attr,
  2160. char *buf)
  2161. {
  2162. struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
  2163. return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
  2164. }
  2165. static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
  2166. NULL);
  2167. static ssize_t
  2168. show_priv_session_creator(struct device *dev, struct device_attribute *attr,
  2169. char *buf)
  2170. {
  2171. struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
  2172. return sprintf(buf, "%d\n", session->creator);
  2173. }
  2174. static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
  2175. NULL);
  2176. #define iscsi_priv_session_attr_show(field, format) \
  2177. static ssize_t \
  2178. show_priv_session_##field(struct device *dev, \
  2179. struct device_attribute *attr, char *buf) \
  2180. { \
  2181. struct iscsi_cls_session *session = \
  2182. iscsi_dev_to_session(dev->parent); \
  2183. if (session->field == -1) \
  2184. return sprintf(buf, "off\n"); \
  2185. return sprintf(buf, format"\n", session->field); \
  2186. }
  2187. #define iscsi_priv_session_attr_store(field) \
  2188. static ssize_t \
  2189. store_priv_session_##field(struct device *dev, \
  2190. struct device_attribute *attr, \
  2191. const char *buf, size_t count) \
  2192. { \
  2193. int val; \
  2194. char *cp; \
  2195. struct iscsi_cls_session *session = \
  2196. iscsi_dev_to_session(dev->parent); \
  2197. if ((session->state == ISCSI_SESSION_FREE) || \
  2198. (session->state == ISCSI_SESSION_FAILED)) \
  2199. return -EBUSY; \
  2200. if (strncmp(buf, "off", 3) == 0) \
  2201. session->field = -1; \
  2202. else { \
  2203. val = simple_strtoul(buf, &cp, 0); \
  2204. if (*cp != '\0' && *cp != '\n') \
  2205. return -EINVAL; \
  2206. session->field = val; \
  2207. } \
  2208. return count; \
  2209. }
  2210. #define iscsi_priv_session_rw_attr(field, format) \
  2211. iscsi_priv_session_attr_show(field, format) \
  2212. iscsi_priv_session_attr_store(field) \
  2213. static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
  2214. show_priv_session_##field, \
  2215. store_priv_session_##field)
  2216. iscsi_priv_session_rw_attr(recovery_tmo, "%d");
  2217. static struct attribute *iscsi_session_attrs[] = {
  2218. &dev_attr_sess_initial_r2t.attr,
  2219. &dev_attr_sess_max_outstanding_r2t.attr,
  2220. &dev_attr_sess_immediate_data.attr,
  2221. &dev_attr_sess_first_burst_len.attr,
  2222. &dev_attr_sess_max_burst_len.attr,
  2223. &dev_attr_sess_data_pdu_in_order.attr,
  2224. &dev_attr_sess_data_seq_in_order.attr,
  2225. &dev_attr_sess_erl.attr,
  2226. &dev_attr_sess_targetname.attr,
  2227. &dev_attr_sess_tpgt.attr,
  2228. &dev_attr_sess_password.attr,
  2229. &dev_attr_sess_password_in.attr,
  2230. &dev_attr_sess_username.attr,
  2231. &dev_attr_sess_username_in.attr,
  2232. &dev_attr_sess_fast_abort.attr,
  2233. &dev_attr_sess_abort_tmo.attr,
  2234. &dev_attr_sess_lu_reset_tmo.attr,
  2235. &dev_attr_sess_tgt_reset_tmo.attr,
  2236. &dev_attr_sess_ifacename.attr,
  2237. &dev_attr_sess_initiatorname.attr,
  2238. &dev_attr_sess_targetalias.attr,
  2239. &dev_attr_priv_sess_recovery_tmo.attr,
  2240. &dev_attr_priv_sess_state.attr,
  2241. &dev_attr_priv_sess_creator.attr,
  2242. &dev_attr_sess_chap_out_idx.attr,
  2243. &dev_attr_sess_chap_in_idx.attr,
  2244. NULL,
  2245. };
  2246. static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
  2247. struct attribute *attr, int i)
  2248. {
  2249. struct device *cdev = container_of(kobj, struct device, kobj);
  2250. struct iscsi_cls_session *session = transport_class_to_session(cdev);
  2251. struct iscsi_transport *t = session->transport;
  2252. int param;
  2253. if (attr == &dev_attr_sess_initial_r2t.attr)
  2254. param = ISCSI_PARAM_INITIAL_R2T_EN;
  2255. else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
  2256. param = ISCSI_PARAM_MAX_R2T;
  2257. else if (attr == &dev_attr_sess_immediate_data.attr)
  2258. param = ISCSI_PARAM_IMM_DATA_EN;
  2259. else if (attr == &dev_attr_sess_first_burst_len.attr)
  2260. param = ISCSI_PARAM_FIRST_BURST;
  2261. else if (attr == &dev_attr_sess_max_burst_len.attr)
  2262. param = ISCSI_PARAM_MAX_BURST;
  2263. else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
  2264. param = ISCSI_PARAM_PDU_INORDER_EN;
  2265. else if (attr == &dev_attr_sess_data_seq_in_order.attr)
  2266. param = ISCSI_PARAM_DATASEQ_INORDER_EN;
  2267. else if (attr == &dev_attr_sess_erl.attr)
  2268. param = ISCSI_PARAM_ERL;
  2269. else if (attr == &dev_attr_sess_targetname.attr)
  2270. param = ISCSI_PARAM_TARGET_NAME;
  2271. else if (attr == &dev_attr_sess_tpgt.attr)
  2272. param = ISCSI_PARAM_TPGT;
  2273. else if (attr == &dev_attr_sess_chap_in_idx.attr)
  2274. param = ISCSI_PARAM_CHAP_IN_IDX;
  2275. else if (attr == &dev_attr_sess_chap_out_idx.attr)
  2276. param = ISCSI_PARAM_CHAP_OUT_IDX;
  2277. else if (attr == &dev_attr_sess_password.attr)
  2278. param = ISCSI_PARAM_USERNAME;
  2279. else if (attr == &dev_attr_sess_password_in.attr)
  2280. param = ISCSI_PARAM_USERNAME_IN;
  2281. else if (attr == &dev_attr_sess_username.attr)
  2282. param = ISCSI_PARAM_PASSWORD;
  2283. else if (attr == &dev_attr_sess_username_in.attr)
  2284. param = ISCSI_PARAM_PASSWORD_IN;
  2285. else if (attr == &dev_attr_sess_fast_abort.attr)
  2286. param = ISCSI_PARAM_FAST_ABORT;
  2287. else if (attr == &dev_attr_sess_abort_tmo.attr)
  2288. param = ISCSI_PARAM_ABORT_TMO;
  2289. else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
  2290. param = ISCSI_PARAM_LU_RESET_TMO;
  2291. else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
  2292. param = ISCSI_PARAM_TGT_RESET_TMO;
  2293. else if (attr == &dev_attr_sess_ifacename.attr)
  2294. param = ISCSI_PARAM_IFACE_NAME;
  2295. else if (attr == &dev_attr_sess_initiatorname.attr)
  2296. param = ISCSI_PARAM_INITIATOR_NAME;
  2297. else if (attr == &dev_attr_sess_targetalias.attr)
  2298. param = ISCSI_PARAM_TARGET_ALIAS;
  2299. else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
  2300. return S_IRUGO | S_IWUSR;
  2301. else if (attr == &dev_attr_priv_sess_state.attr)
  2302. return S_IRUGO;
  2303. else if (attr == &dev_attr_priv_sess_creator.attr)
  2304. return S_IRUGO;
  2305. else {
  2306. WARN_ONCE(1, "Invalid session attr");
  2307. return 0;
  2308. }
  2309. return t->attr_is_visible(ISCSI_PARAM, param);
  2310. }
  2311. static struct attribute_group iscsi_session_group = {
  2312. .attrs = iscsi_session_attrs,
  2313. .is_visible = iscsi_session_attr_is_visible,
  2314. };
  2315. /*
  2316. * iSCSI host attrs
  2317. */
  2318. #define iscsi_host_attr_show(param) \
  2319. static ssize_t \
  2320. show_host_param_##param(struct device *dev, \
  2321. struct device_attribute *attr, char *buf) \
  2322. { \
  2323. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  2324. struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
  2325. return priv->iscsi_transport->get_host_param(shost, param, buf); \
  2326. }
  2327. #define iscsi_host_attr(field, param) \
  2328. iscsi_host_attr_show(param) \
  2329. static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
  2330. NULL);
  2331. iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
  2332. iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
  2333. iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
  2334. iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
  2335. iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
  2336. iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
  2337. static struct attribute *iscsi_host_attrs[] = {
  2338. &dev_attr_host_netdev.attr,
  2339. &dev_attr_host_hwaddress.attr,
  2340. &dev_attr_host_ipaddress.attr,
  2341. &dev_attr_host_initiatorname.attr,
  2342. &dev_attr_host_port_state.attr,
  2343. &dev_attr_host_port_speed.attr,
  2344. NULL,
  2345. };
  2346. static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
  2347. struct attribute *attr, int i)
  2348. {
  2349. struct device *cdev = container_of(kobj, struct device, kobj);
  2350. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  2351. struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
  2352. int param;
  2353. if (attr == &dev_attr_host_netdev.attr)
  2354. param = ISCSI_HOST_PARAM_NETDEV_NAME;
  2355. else if (attr == &dev_attr_host_hwaddress.attr)
  2356. param = ISCSI_HOST_PARAM_HWADDRESS;
  2357. else if (attr == &dev_attr_host_ipaddress.attr)
  2358. param = ISCSI_HOST_PARAM_IPADDRESS;
  2359. else if (attr == &dev_attr_host_initiatorname.attr)
  2360. param = ISCSI_HOST_PARAM_INITIATOR_NAME;
  2361. else if (attr == &dev_attr_host_port_state.attr)
  2362. param = ISCSI_HOST_PARAM_PORT_STATE;
  2363. else if (attr == &dev_attr_host_port_speed.attr)
  2364. param = ISCSI_HOST_PARAM_PORT_SPEED;
  2365. else {
  2366. WARN_ONCE(1, "Invalid host attr");
  2367. return 0;
  2368. }
  2369. return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
  2370. }
  2371. static struct attribute_group iscsi_host_group = {
  2372. .attrs = iscsi_host_attrs,
  2373. .is_visible = iscsi_host_attr_is_visible,
  2374. };
  2375. /* convert iscsi_port_speed values to ascii string name */
  2376. static const struct {
  2377. enum iscsi_port_speed value;
  2378. char *name;
  2379. } iscsi_port_speed_names[] = {
  2380. {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
  2381. {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
  2382. {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
  2383. {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
  2384. {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
  2385. };
  2386. char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
  2387. {
  2388. int i;
  2389. char *speed = "Unknown!";
  2390. struct iscsi_cls_host *ihost = shost->shost_data;
  2391. uint32_t port_speed = ihost->port_speed;
  2392. for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
  2393. if (iscsi_port_speed_names[i].value & port_speed) {
  2394. speed = iscsi_port_speed_names[i].name;
  2395. break;
  2396. }
  2397. }
  2398. return speed;
  2399. }
  2400. EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
  2401. /* convert iscsi_port_state values to ascii string name */
  2402. static const struct {
  2403. enum iscsi_port_state value;
  2404. char *name;
  2405. } iscsi_port_state_names[] = {
  2406. {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
  2407. {ISCSI_PORT_STATE_UP, "LINK UP" },
  2408. };
  2409. char *iscsi_get_port_state_name(struct Scsi_Host *shost)
  2410. {
  2411. int i;
  2412. char *state = "Unknown!";
  2413. struct iscsi_cls_host *ihost = shost->shost_data;
  2414. uint32_t port_state = ihost->port_state;
  2415. for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
  2416. if (iscsi_port_state_names[i].value & port_state) {
  2417. state = iscsi_port_state_names[i].name;
  2418. break;
  2419. }
  2420. }
  2421. return state;
  2422. }
  2423. EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
  2424. static int iscsi_session_match(struct attribute_container *cont,
  2425. struct device *dev)
  2426. {
  2427. struct iscsi_cls_session *session;
  2428. struct Scsi_Host *shost;
  2429. struct iscsi_internal *priv;
  2430. if (!iscsi_is_session_dev(dev))
  2431. return 0;
  2432. session = iscsi_dev_to_session(dev);
  2433. shost = iscsi_session_to_shost(session);
  2434. if (!shost->transportt)
  2435. return 0;
  2436. priv = to_iscsi_internal(shost->transportt);
  2437. if (priv->session_cont.ac.class != &iscsi_session_class.class)
  2438. return 0;
  2439. return &priv->session_cont.ac == cont;
  2440. }
  2441. static int iscsi_conn_match(struct attribute_container *cont,
  2442. struct device *dev)
  2443. {
  2444. struct iscsi_cls_session *session;
  2445. struct iscsi_cls_conn *conn;
  2446. struct Scsi_Host *shost;
  2447. struct iscsi_internal *priv;
  2448. if (!iscsi_is_conn_dev(dev))
  2449. return 0;
  2450. conn = iscsi_dev_to_conn(dev);
  2451. session = iscsi_dev_to_session(conn->dev.parent);
  2452. shost = iscsi_session_to_shost(session);
  2453. if (!shost->transportt)
  2454. return 0;
  2455. priv = to_iscsi_internal(shost->transportt);
  2456. if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
  2457. return 0;
  2458. return &priv->conn_cont.ac == cont;
  2459. }
  2460. static int iscsi_host_match(struct attribute_container *cont,
  2461. struct device *dev)
  2462. {
  2463. struct Scsi_Host *shost;
  2464. struct iscsi_internal *priv;
  2465. if (!scsi_is_host_device(dev))
  2466. return 0;
  2467. shost = dev_to_shost(dev);
  2468. if (!shost->transportt ||
  2469. shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
  2470. return 0;
  2471. priv = to_iscsi_internal(shost->transportt);
  2472. return &priv->t.host_attrs.ac == cont;
  2473. }
  2474. struct scsi_transport_template *
  2475. iscsi_register_transport(struct iscsi_transport *tt)
  2476. {
  2477. struct iscsi_internal *priv;
  2478. unsigned long flags;
  2479. int err;
  2480. BUG_ON(!tt);
  2481. priv = iscsi_if_transport_lookup(tt);
  2482. if (priv)
  2483. return NULL;
  2484. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  2485. if (!priv)
  2486. return NULL;
  2487. INIT_LIST_HEAD(&priv->list);
  2488. priv->iscsi_transport = tt;
  2489. priv->t.user_scan = iscsi_user_scan;
  2490. priv->t.create_work_queue = 1;
  2491. priv->dev.class = &iscsi_transport_class;
  2492. dev_set_name(&priv->dev, "%s", tt->name);
  2493. err = device_register(&priv->dev);
  2494. if (err)
  2495. goto free_priv;
  2496. err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
  2497. if (err)
  2498. goto unregister_dev;
  2499. /* host parameters */
  2500. priv->t.host_attrs.ac.class = &iscsi_host_class.class;
  2501. priv->t.host_attrs.ac.match = iscsi_host_match;
  2502. priv->t.host_attrs.ac.grp = &iscsi_host_group;
  2503. priv->t.host_size = sizeof(struct iscsi_cls_host);
  2504. transport_container_register(&priv->t.host_attrs);
  2505. /* connection parameters */
  2506. priv->conn_cont.ac.class = &iscsi_connection_class.class;
  2507. priv->conn_cont.ac.match = iscsi_conn_match;
  2508. priv->conn_cont.ac.grp = &iscsi_conn_group;
  2509. transport_container_register(&priv->conn_cont);
  2510. /* session parameters */
  2511. priv->session_cont.ac.class = &iscsi_session_class.class;
  2512. priv->session_cont.ac.match = iscsi_session_match;
  2513. priv->session_cont.ac.grp = &iscsi_session_group;
  2514. transport_container_register(&priv->session_cont);
  2515. spin_lock_irqsave(&iscsi_transport_lock, flags);
  2516. list_add(&priv->list, &iscsi_transports);
  2517. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  2518. printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
  2519. return &priv->t;
  2520. unregister_dev:
  2521. device_unregister(&priv->dev);
  2522. return NULL;
  2523. free_priv:
  2524. kfree(priv);
  2525. return NULL;
  2526. }
  2527. EXPORT_SYMBOL_GPL(iscsi_register_transport);
  2528. int iscsi_unregister_transport(struct iscsi_transport *tt)
  2529. {
  2530. struct iscsi_internal *priv;
  2531. unsigned long flags;
  2532. BUG_ON(!tt);
  2533. mutex_lock(&rx_queue_mutex);
  2534. priv = iscsi_if_transport_lookup(tt);
  2535. BUG_ON (!priv);
  2536. spin_lock_irqsave(&iscsi_transport_lock, flags);
  2537. list_del(&priv->list);
  2538. spin_unlock_irqrestore(&iscsi_transport_lock, flags);
  2539. transport_container_unregister(&priv->conn_cont);
  2540. transport_container_unregister(&priv->session_cont);
  2541. transport_container_unregister(&priv->t.host_attrs);
  2542. sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
  2543. device_unregister(&priv->dev);
  2544. mutex_unlock(&rx_queue_mutex);
  2545. return 0;
  2546. }
  2547. EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
  2548. static __init int iscsi_transport_init(void)
  2549. {
  2550. int err;
  2551. printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
  2552. ISCSI_TRANSPORT_VERSION);
  2553. atomic_set(&iscsi_session_nr, 0);
  2554. err = class_register(&iscsi_transport_class);
  2555. if (err)
  2556. return err;
  2557. err = class_register(&iscsi_endpoint_class);
  2558. if (err)
  2559. goto unregister_transport_class;
  2560. err = class_register(&iscsi_iface_class);
  2561. if (err)
  2562. goto unregister_endpoint_class;
  2563. err = transport_class_register(&iscsi_host_class);
  2564. if (err)
  2565. goto unregister_iface_class;
  2566. err = transport_class_register(&iscsi_connection_class);
  2567. if (err)
  2568. goto unregister_host_class;
  2569. err = transport_class_register(&iscsi_session_class);
  2570. if (err)
  2571. goto unregister_conn_class;
  2572. nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
  2573. NULL, THIS_MODULE);
  2574. if (!nls) {
  2575. err = -ENOBUFS;
  2576. goto unregister_session_class;
  2577. }
  2578. iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
  2579. if (!iscsi_eh_timer_workq)
  2580. goto release_nls;
  2581. return 0;
  2582. release_nls:
  2583. netlink_kernel_release(nls);
  2584. unregister_session_class:
  2585. transport_class_unregister(&iscsi_session_class);
  2586. unregister_conn_class:
  2587. transport_class_unregister(&iscsi_connection_class);
  2588. unregister_host_class:
  2589. transport_class_unregister(&iscsi_host_class);
  2590. unregister_iface_class:
  2591. class_unregister(&iscsi_iface_class);
  2592. unregister_endpoint_class:
  2593. class_unregister(&iscsi_endpoint_class);
  2594. unregister_transport_class:
  2595. class_unregister(&iscsi_transport_class);
  2596. return err;
  2597. }
  2598. static void __exit iscsi_transport_exit(void)
  2599. {
  2600. destroy_workqueue(iscsi_eh_timer_workq);
  2601. netlink_kernel_release(nls);
  2602. transport_class_unregister(&iscsi_connection_class);
  2603. transport_class_unregister(&iscsi_session_class);
  2604. transport_class_unregister(&iscsi_host_class);
  2605. class_unregister(&iscsi_endpoint_class);
  2606. class_unregister(&iscsi_iface_class);
  2607. class_unregister(&iscsi_transport_class);
  2608. }
  2609. module_init(iscsi_transport_init);
  2610. module_exit(iscsi_transport_exit);
  2611. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  2612. "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  2613. "Alex Aizman <itn780@yahoo.com>");
  2614. MODULE_DESCRIPTION("iSCSI Transport Interface");
  2615. MODULE_LICENSE("GPL");
  2616. MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
  2617. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);