sbp_target.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616
  1. /*
  2. * SBP2 target driver (SCSI over IEEE1394 in target mode)
  3. *
  4. * Copyright (C) 2011 Chris Boot <bootc@bootc.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #define KMSG_COMPONENT "sbp_target"
  21. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/string.h>
  27. #include <linux/configfs.h>
  28. #include <linux/ctype.h>
  29. #include <linux/firewire.h>
  30. #include <linux/firewire-constants.h>
  31. #include <scsi/scsi.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_backend.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_fabric_configfs.h>
  37. #include <target/target_core_configfs.h>
  38. #include <target/configfs_macros.h>
  39. #include <asm/unaligned.h>
  40. #include "sbp_target.h"
  41. /* Local pointer to allocated TCM configfs fabric module */
  42. static struct target_fabric_configfs *sbp_fabric_configfs;
  43. /* FireWire address region for management and command block address handlers */
  44. static const struct fw_address_region sbp_register_region = {
  45. .start = CSR_REGISTER_BASE + 0x10000,
  46. .end = 0x1000000000000ULL,
  47. };
  48. static const u32 sbp_unit_directory_template[] = {
  49. 0x1200609e, /* unit_specifier_id: NCITS/T10 */
  50. 0x13010483, /* unit_sw_version: 1155D Rev 4 */
  51. 0x3800609e, /* command_set_specifier_id: NCITS/T10 */
  52. 0x390104d8, /* command_set: SPC-2 */
  53. 0x3b000000, /* command_set_revision: 0 */
  54. 0x3c000001, /* firmware_revision: 1 */
  55. };
  56. #define SESSION_MAINTENANCE_INTERVAL HZ
  57. static atomic_t login_id = ATOMIC_INIT(0);
  58. static void session_maintenance_work(struct work_struct *);
  59. static int sbp_run_transaction(struct fw_card *, int, int, int, int,
  60. unsigned long long, void *, size_t);
  61. static int read_peer_guid(u64 *guid, const struct sbp_management_request *req)
  62. {
  63. int ret;
  64. __be32 high, low;
  65. ret = sbp_run_transaction(req->card, TCODE_READ_QUADLET_REQUEST,
  66. req->node_addr, req->generation, req->speed,
  67. (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + 3 * 4,
  68. &high, sizeof(high));
  69. if (ret != RCODE_COMPLETE)
  70. return ret;
  71. ret = sbp_run_transaction(req->card, TCODE_READ_QUADLET_REQUEST,
  72. req->node_addr, req->generation, req->speed,
  73. (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + 4 * 4,
  74. &low, sizeof(low));
  75. if (ret != RCODE_COMPLETE)
  76. return ret;
  77. *guid = (u64)be32_to_cpu(high) << 32 | be32_to_cpu(low);
  78. return RCODE_COMPLETE;
  79. }
  80. static struct sbp_session *sbp_session_find_by_guid(
  81. struct sbp_tpg *tpg, u64 guid)
  82. {
  83. struct se_session *se_sess;
  84. struct sbp_session *sess, *found = NULL;
  85. spin_lock_bh(&tpg->se_tpg.session_lock);
  86. list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) {
  87. sess = se_sess->fabric_sess_ptr;
  88. if (sess->guid == guid)
  89. found = sess;
  90. }
  91. spin_unlock_bh(&tpg->se_tpg.session_lock);
  92. return found;
  93. }
  94. static struct sbp_login_descriptor *sbp_login_find_by_lun(
  95. struct sbp_session *session, struct se_lun *lun)
  96. {
  97. struct sbp_login_descriptor *login, *found = NULL;
  98. spin_lock_bh(&session->lock);
  99. list_for_each_entry(login, &session->login_list, link) {
  100. if (login->lun == lun)
  101. found = login;
  102. }
  103. spin_unlock_bh(&session->lock);
  104. return found;
  105. }
  106. static int sbp_login_count_all_by_lun(
  107. struct sbp_tpg *tpg,
  108. struct se_lun *lun,
  109. int exclusive)
  110. {
  111. struct se_session *se_sess;
  112. struct sbp_session *sess;
  113. struct sbp_login_descriptor *login;
  114. int count = 0;
  115. spin_lock_bh(&tpg->se_tpg.session_lock);
  116. list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) {
  117. sess = se_sess->fabric_sess_ptr;
  118. spin_lock_bh(&sess->lock);
  119. list_for_each_entry(login, &sess->login_list, link) {
  120. if (login->lun != lun)
  121. continue;
  122. if (!exclusive || login->exclusive)
  123. count++;
  124. }
  125. spin_unlock_bh(&sess->lock);
  126. }
  127. spin_unlock_bh(&tpg->se_tpg.session_lock);
  128. return count;
  129. }
  130. static struct sbp_login_descriptor *sbp_login_find_by_id(
  131. struct sbp_tpg *tpg, int login_id)
  132. {
  133. struct se_session *se_sess;
  134. struct sbp_session *sess;
  135. struct sbp_login_descriptor *login, *found = NULL;
  136. spin_lock_bh(&tpg->se_tpg.session_lock);
  137. list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) {
  138. sess = se_sess->fabric_sess_ptr;
  139. spin_lock_bh(&sess->lock);
  140. list_for_each_entry(login, &sess->login_list, link) {
  141. if (login->login_id == login_id)
  142. found = login;
  143. }
  144. spin_unlock_bh(&sess->lock);
  145. }
  146. spin_unlock_bh(&tpg->se_tpg.session_lock);
  147. return found;
  148. }
  149. static struct se_lun *sbp_get_lun_from_tpg(struct sbp_tpg *tpg, int lun)
  150. {
  151. struct se_portal_group *se_tpg = &tpg->se_tpg;
  152. struct se_lun *se_lun;
  153. if (lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  154. return ERR_PTR(-EINVAL);
  155. spin_lock(&se_tpg->tpg_lun_lock);
  156. se_lun = se_tpg->tpg_lun_list[lun];
  157. if (se_lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
  158. se_lun = ERR_PTR(-ENODEV);
  159. spin_unlock(&se_tpg->tpg_lun_lock);
  160. return se_lun;
  161. }
  162. static struct sbp_session *sbp_session_create(
  163. struct sbp_tpg *tpg,
  164. u64 guid)
  165. {
  166. struct sbp_session *sess;
  167. int ret;
  168. char guid_str[17];
  169. struct se_node_acl *se_nacl;
  170. sess = kmalloc(sizeof(*sess), GFP_KERNEL);
  171. if (!sess) {
  172. pr_err("failed to allocate session descriptor\n");
  173. return ERR_PTR(-ENOMEM);
  174. }
  175. sess->se_sess = transport_init_session();
  176. if (IS_ERR(sess->se_sess)) {
  177. pr_err("failed to init se_session\n");
  178. ret = PTR_ERR(sess->se_sess);
  179. kfree(sess);
  180. return ERR_PTR(ret);
  181. }
  182. snprintf(guid_str, sizeof(guid_str), "%016llx", guid);
  183. se_nacl = core_tpg_check_initiator_node_acl(&tpg->se_tpg, guid_str);
  184. if (!se_nacl) {
  185. pr_warn("Node ACL not found for %s\n", guid_str);
  186. transport_free_session(sess->se_sess);
  187. kfree(sess);
  188. return ERR_PTR(-EPERM);
  189. }
  190. sess->se_sess->se_node_acl = se_nacl;
  191. spin_lock_init(&sess->lock);
  192. INIT_LIST_HEAD(&sess->login_list);
  193. INIT_DELAYED_WORK(&sess->maint_work, session_maintenance_work);
  194. sess->guid = guid;
  195. transport_register_session(&tpg->se_tpg, se_nacl, sess->se_sess, sess);
  196. return sess;
  197. }
  198. static void sbp_session_release(struct sbp_session *sess, bool cancel_work)
  199. {
  200. spin_lock_bh(&sess->lock);
  201. if (!list_empty(&sess->login_list)) {
  202. spin_unlock_bh(&sess->lock);
  203. return;
  204. }
  205. spin_unlock_bh(&sess->lock);
  206. if (cancel_work)
  207. cancel_delayed_work_sync(&sess->maint_work);
  208. transport_deregister_session_configfs(sess->se_sess);
  209. transport_deregister_session(sess->se_sess);
  210. if (sess->card)
  211. fw_card_put(sess->card);
  212. kfree(sess);
  213. }
  214. static void sbp_target_agent_unregister(struct sbp_target_agent *);
  215. static void sbp_login_release(struct sbp_login_descriptor *login,
  216. bool cancel_work)
  217. {
  218. struct sbp_session *sess = login->sess;
  219. /* FIXME: abort/wait on tasks */
  220. sbp_target_agent_unregister(login->tgt_agt);
  221. if (sess) {
  222. spin_lock_bh(&sess->lock);
  223. list_del(&login->link);
  224. spin_unlock_bh(&sess->lock);
  225. sbp_session_release(sess, cancel_work);
  226. }
  227. kfree(login);
  228. }
  229. static struct sbp_target_agent *sbp_target_agent_register(
  230. struct sbp_login_descriptor *);
  231. static void sbp_management_request_login(
  232. struct sbp_management_agent *agent, struct sbp_management_request *req,
  233. int *status_data_size)
  234. {
  235. struct sbp_tport *tport = agent->tport;
  236. struct sbp_tpg *tpg = tport->tpg;
  237. struct se_lun *se_lun;
  238. int ret;
  239. u64 guid;
  240. struct sbp_session *sess;
  241. struct sbp_login_descriptor *login;
  242. struct sbp_login_response_block *response;
  243. int login_response_len;
  244. se_lun = sbp_get_lun_from_tpg(tpg,
  245. LOGIN_ORB_LUN(be32_to_cpu(req->orb.misc)));
  246. if (IS_ERR(se_lun)) {
  247. pr_notice("login to unknown LUN: %d\n",
  248. LOGIN_ORB_LUN(be32_to_cpu(req->orb.misc)));
  249. req->status.status = cpu_to_be32(
  250. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  251. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_LUN_NOTSUPP));
  252. return;
  253. }
  254. ret = read_peer_guid(&guid, req);
  255. if (ret != RCODE_COMPLETE) {
  256. pr_warn("failed to read peer GUID: %d\n", ret);
  257. req->status.status = cpu_to_be32(
  258. STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  259. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  260. return;
  261. }
  262. pr_notice("mgt_agent LOGIN to LUN %d from %016llx\n",
  263. se_lun->unpacked_lun, guid);
  264. sess = sbp_session_find_by_guid(tpg, guid);
  265. if (sess) {
  266. login = sbp_login_find_by_lun(sess, se_lun);
  267. if (login) {
  268. pr_notice("initiator already logged-in\n");
  269. /*
  270. * SBP-2 R4 says we should return access denied, but
  271. * that can confuse initiators. Instead we need to
  272. * treat this like a reconnect, but send the login
  273. * response block like a fresh login.
  274. *
  275. * This is required particularly in the case of Apple
  276. * devices booting off the FireWire target, where
  277. * the firmware has an active login to the target. When
  278. * the OS takes control of the session it issues its own
  279. * LOGIN rather than a RECONNECT. To avoid the machine
  280. * waiting until the reconnect_hold expires, we can skip
  281. * the ACCESS_DENIED errors to speed things up.
  282. */
  283. goto already_logged_in;
  284. }
  285. }
  286. /*
  287. * check exclusive bit in login request
  288. * reject with access_denied if any logins present
  289. */
  290. if (LOGIN_ORB_EXCLUSIVE(be32_to_cpu(req->orb.misc)) &&
  291. sbp_login_count_all_by_lun(tpg, se_lun, 0)) {
  292. pr_warn("refusing exclusive login with other active logins\n");
  293. req->status.status = cpu_to_be32(
  294. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  295. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  296. return;
  297. }
  298. /*
  299. * check exclusive bit in any existing login descriptor
  300. * reject with access_denied if any exclusive logins present
  301. */
  302. if (sbp_login_count_all_by_lun(tpg, se_lun, 1)) {
  303. pr_warn("refusing login while another exclusive login present\n");
  304. req->status.status = cpu_to_be32(
  305. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  306. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  307. return;
  308. }
  309. /*
  310. * check we haven't exceeded the number of allowed logins
  311. * reject with resources_unavailable if we have
  312. */
  313. if (sbp_login_count_all_by_lun(tpg, se_lun, 0) >=
  314. tport->max_logins_per_lun) {
  315. pr_warn("max number of logins reached\n");
  316. req->status.status = cpu_to_be32(
  317. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  318. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  319. return;
  320. }
  321. if (!sess) {
  322. sess = sbp_session_create(tpg, guid);
  323. if (IS_ERR(sess)) {
  324. switch (PTR_ERR(sess)) {
  325. case -EPERM:
  326. ret = SBP_STATUS_ACCESS_DENIED;
  327. break;
  328. default:
  329. ret = SBP_STATUS_RESOURCES_UNAVAIL;
  330. break;
  331. }
  332. req->status.status = cpu_to_be32(
  333. STATUS_BLOCK_RESP(
  334. STATUS_RESP_REQUEST_COMPLETE) |
  335. STATUS_BLOCK_SBP_STATUS(ret));
  336. return;
  337. }
  338. sess->node_id = req->node_addr;
  339. sess->card = fw_card_get(req->card);
  340. sess->generation = req->generation;
  341. sess->speed = req->speed;
  342. schedule_delayed_work(&sess->maint_work,
  343. SESSION_MAINTENANCE_INTERVAL);
  344. }
  345. /* only take the latest reconnect_hold into account */
  346. sess->reconnect_hold = min(
  347. 1 << LOGIN_ORB_RECONNECT(be32_to_cpu(req->orb.misc)),
  348. tport->max_reconnect_timeout) - 1;
  349. login = kmalloc(sizeof(*login), GFP_KERNEL);
  350. if (!login) {
  351. pr_err("failed to allocate login descriptor\n");
  352. sbp_session_release(sess, true);
  353. req->status.status = cpu_to_be32(
  354. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  355. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  356. return;
  357. }
  358. login->sess = sess;
  359. login->lun = se_lun;
  360. login->status_fifo_addr = sbp2_pointer_to_addr(&req->orb.status_fifo);
  361. login->exclusive = LOGIN_ORB_EXCLUSIVE(be32_to_cpu(req->orb.misc));
  362. login->login_id = atomic_inc_return(&login_id);
  363. login->tgt_agt = sbp_target_agent_register(login);
  364. if (IS_ERR(login->tgt_agt)) {
  365. ret = PTR_ERR(login->tgt_agt);
  366. pr_err("failed to map command block handler: %d\n", ret);
  367. sbp_session_release(sess, true);
  368. kfree(login);
  369. req->status.status = cpu_to_be32(
  370. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  371. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  372. return;
  373. }
  374. spin_lock_bh(&sess->lock);
  375. list_add_tail(&login->link, &sess->login_list);
  376. spin_unlock_bh(&sess->lock);
  377. already_logged_in:
  378. response = kzalloc(sizeof(*response), GFP_KERNEL);
  379. if (!response) {
  380. pr_err("failed to allocate login response block\n");
  381. sbp_login_release(login, true);
  382. req->status.status = cpu_to_be32(
  383. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  384. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  385. return;
  386. }
  387. login_response_len = clamp_val(
  388. LOGIN_ORB_RESPONSE_LENGTH(be32_to_cpu(req->orb.length)),
  389. 12, sizeof(*response));
  390. response->misc = cpu_to_be32(
  391. ((login_response_len & 0xffff) << 16) |
  392. (login->login_id & 0xffff));
  393. response->reconnect_hold = cpu_to_be32(sess->reconnect_hold & 0xffff);
  394. addr_to_sbp2_pointer(login->tgt_agt->handler.offset,
  395. &response->command_block_agent);
  396. ret = sbp_run_transaction(sess->card, TCODE_WRITE_BLOCK_REQUEST,
  397. sess->node_id, sess->generation, sess->speed,
  398. sbp2_pointer_to_addr(&req->orb.ptr2), response,
  399. login_response_len);
  400. if (ret != RCODE_COMPLETE) {
  401. pr_debug("failed to write login response block: %x\n", ret);
  402. kfree(response);
  403. sbp_login_release(login, true);
  404. req->status.status = cpu_to_be32(
  405. STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  406. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  407. return;
  408. }
  409. kfree(response);
  410. req->status.status = cpu_to_be32(
  411. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  412. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  413. }
  414. static void sbp_management_request_query_logins(
  415. struct sbp_management_agent *agent, struct sbp_management_request *req,
  416. int *status_data_size)
  417. {
  418. pr_notice("QUERY LOGINS not implemented\n");
  419. /* FIXME: implement */
  420. req->status.status = cpu_to_be32(
  421. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  422. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  423. }
  424. static void sbp_management_request_reconnect(
  425. struct sbp_management_agent *agent, struct sbp_management_request *req,
  426. int *status_data_size)
  427. {
  428. struct sbp_tport *tport = agent->tport;
  429. struct sbp_tpg *tpg = tport->tpg;
  430. int ret;
  431. u64 guid;
  432. struct sbp_login_descriptor *login;
  433. ret = read_peer_guid(&guid, req);
  434. if (ret != RCODE_COMPLETE) {
  435. pr_warn("failed to read peer GUID: %d\n", ret);
  436. req->status.status = cpu_to_be32(
  437. STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  438. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  439. return;
  440. }
  441. pr_notice("mgt_agent RECONNECT from %016llx\n", guid);
  442. login = sbp_login_find_by_id(tpg,
  443. RECONNECT_ORB_LOGIN_ID(be32_to_cpu(req->orb.misc)));
  444. if (!login) {
  445. pr_err("mgt_agent RECONNECT unknown login ID\n");
  446. req->status.status = cpu_to_be32(
  447. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  448. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  449. return;
  450. }
  451. if (login->sess->guid != guid) {
  452. pr_err("mgt_agent RECONNECT login GUID doesn't match\n");
  453. req->status.status = cpu_to_be32(
  454. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  455. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  456. return;
  457. }
  458. spin_lock_bh(&login->sess->lock);
  459. if (login->sess->card)
  460. fw_card_put(login->sess->card);
  461. /* update the node details */
  462. login->sess->generation = req->generation;
  463. login->sess->node_id = req->node_addr;
  464. login->sess->card = fw_card_get(req->card);
  465. login->sess->speed = req->speed;
  466. spin_unlock_bh(&login->sess->lock);
  467. req->status.status = cpu_to_be32(
  468. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  469. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  470. }
  471. static void sbp_management_request_logout(
  472. struct sbp_management_agent *agent, struct sbp_management_request *req,
  473. int *status_data_size)
  474. {
  475. struct sbp_tport *tport = agent->tport;
  476. struct sbp_tpg *tpg = tport->tpg;
  477. int id;
  478. struct sbp_login_descriptor *login;
  479. id = LOGOUT_ORB_LOGIN_ID(be32_to_cpu(req->orb.misc));
  480. login = sbp_login_find_by_id(tpg, id);
  481. if (!login) {
  482. pr_warn("cannot find login: %d\n", id);
  483. req->status.status = cpu_to_be32(
  484. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  485. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_LOGIN_ID_UNKNOWN));
  486. return;
  487. }
  488. pr_info("mgt_agent LOGOUT from LUN %d session %d\n",
  489. login->lun->unpacked_lun, login->login_id);
  490. if (req->node_addr != login->sess->node_id) {
  491. pr_warn("logout from different node ID\n");
  492. req->status.status = cpu_to_be32(
  493. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  494. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  495. return;
  496. }
  497. sbp_login_release(login, true);
  498. req->status.status = cpu_to_be32(
  499. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  500. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  501. }
  502. static void session_check_for_reset(struct sbp_session *sess)
  503. {
  504. bool card_valid = false;
  505. spin_lock_bh(&sess->lock);
  506. if (sess->card) {
  507. spin_lock_irq(&sess->card->lock);
  508. card_valid = (sess->card->local_node != NULL);
  509. spin_unlock_irq(&sess->card->lock);
  510. if (!card_valid) {
  511. fw_card_put(sess->card);
  512. sess->card = NULL;
  513. }
  514. }
  515. if (!card_valid || (sess->generation != sess->card->generation)) {
  516. pr_info("Waiting for reconnect from node: %016llx\n",
  517. sess->guid);
  518. sess->node_id = -1;
  519. sess->reconnect_expires = get_jiffies_64() +
  520. ((sess->reconnect_hold + 1) * HZ);
  521. }
  522. spin_unlock_bh(&sess->lock);
  523. }
  524. static void session_reconnect_expired(struct sbp_session *sess)
  525. {
  526. struct sbp_login_descriptor *login, *temp;
  527. LIST_HEAD(login_list);
  528. pr_info("Reconnect timer expired for node: %016llx\n", sess->guid);
  529. spin_lock_bh(&sess->lock);
  530. list_for_each_entry_safe(login, temp, &sess->login_list, link) {
  531. login->sess = NULL;
  532. list_del(&login->link);
  533. list_add_tail(&login->link, &login_list);
  534. }
  535. spin_unlock_bh(&sess->lock);
  536. list_for_each_entry_safe(login, temp, &login_list, link) {
  537. list_del(&login->link);
  538. sbp_login_release(login, false);
  539. }
  540. sbp_session_release(sess, false);
  541. }
  542. static void session_maintenance_work(struct work_struct *work)
  543. {
  544. struct sbp_session *sess = container_of(work, struct sbp_session,
  545. maint_work.work);
  546. /* could be called while tearing down the session */
  547. spin_lock_bh(&sess->lock);
  548. if (list_empty(&sess->login_list)) {
  549. spin_unlock_bh(&sess->lock);
  550. return;
  551. }
  552. spin_unlock_bh(&sess->lock);
  553. if (sess->node_id != -1) {
  554. /* check for bus reset and make node_id invalid */
  555. session_check_for_reset(sess);
  556. schedule_delayed_work(&sess->maint_work,
  557. SESSION_MAINTENANCE_INTERVAL);
  558. } else if (!time_after64(get_jiffies_64(), sess->reconnect_expires)) {
  559. /* still waiting for reconnect */
  560. schedule_delayed_work(&sess->maint_work,
  561. SESSION_MAINTENANCE_INTERVAL);
  562. } else {
  563. /* reconnect timeout has expired */
  564. session_reconnect_expired(sess);
  565. }
  566. }
  567. static int tgt_agent_rw_agent_state(struct fw_card *card, int tcode, void *data,
  568. struct sbp_target_agent *agent)
  569. {
  570. __be32 state;
  571. switch (tcode) {
  572. case TCODE_READ_QUADLET_REQUEST:
  573. pr_debug("tgt_agent AGENT_STATE READ\n");
  574. spin_lock_bh(&agent->lock);
  575. state = cpu_to_be32(agent->state);
  576. spin_unlock_bh(&agent->lock);
  577. memcpy(data, &state, sizeof(state));
  578. return RCODE_COMPLETE;
  579. case TCODE_WRITE_QUADLET_REQUEST:
  580. /* ignored */
  581. return RCODE_COMPLETE;
  582. default:
  583. return RCODE_TYPE_ERROR;
  584. }
  585. }
  586. static int tgt_agent_rw_agent_reset(struct fw_card *card, int tcode, void *data,
  587. struct sbp_target_agent *agent)
  588. {
  589. switch (tcode) {
  590. case TCODE_WRITE_QUADLET_REQUEST:
  591. pr_debug("tgt_agent AGENT_RESET\n");
  592. spin_lock_bh(&agent->lock);
  593. agent->state = AGENT_STATE_RESET;
  594. spin_unlock_bh(&agent->lock);
  595. return RCODE_COMPLETE;
  596. default:
  597. return RCODE_TYPE_ERROR;
  598. }
  599. }
  600. static int tgt_agent_rw_orb_pointer(struct fw_card *card, int tcode, void *data,
  601. struct sbp_target_agent *agent)
  602. {
  603. struct sbp2_pointer *ptr = data;
  604. switch (tcode) {
  605. case TCODE_WRITE_BLOCK_REQUEST:
  606. spin_lock_bh(&agent->lock);
  607. if (agent->state != AGENT_STATE_SUSPENDED &&
  608. agent->state != AGENT_STATE_RESET) {
  609. spin_unlock_bh(&agent->lock);
  610. pr_notice("Ignoring ORB_POINTER write while active.\n");
  611. return RCODE_CONFLICT_ERROR;
  612. }
  613. agent->state = AGENT_STATE_ACTIVE;
  614. spin_unlock_bh(&agent->lock);
  615. agent->orb_pointer = sbp2_pointer_to_addr(ptr);
  616. agent->doorbell = false;
  617. pr_debug("tgt_agent ORB_POINTER write: 0x%llx\n",
  618. agent->orb_pointer);
  619. queue_work(system_unbound_wq, &agent->work);
  620. return RCODE_COMPLETE;
  621. case TCODE_READ_BLOCK_REQUEST:
  622. pr_debug("tgt_agent ORB_POINTER READ\n");
  623. spin_lock_bh(&agent->lock);
  624. addr_to_sbp2_pointer(agent->orb_pointer, ptr);
  625. spin_unlock_bh(&agent->lock);
  626. return RCODE_COMPLETE;
  627. default:
  628. return RCODE_TYPE_ERROR;
  629. }
  630. }
  631. static int tgt_agent_rw_doorbell(struct fw_card *card, int tcode, void *data,
  632. struct sbp_target_agent *agent)
  633. {
  634. switch (tcode) {
  635. case TCODE_WRITE_QUADLET_REQUEST:
  636. spin_lock_bh(&agent->lock);
  637. if (agent->state != AGENT_STATE_SUSPENDED) {
  638. spin_unlock_bh(&agent->lock);
  639. pr_debug("Ignoring DOORBELL while active.\n");
  640. return RCODE_CONFLICT_ERROR;
  641. }
  642. agent->state = AGENT_STATE_ACTIVE;
  643. spin_unlock_bh(&agent->lock);
  644. agent->doorbell = true;
  645. pr_debug("tgt_agent DOORBELL\n");
  646. queue_work(system_unbound_wq, &agent->work);
  647. return RCODE_COMPLETE;
  648. case TCODE_READ_QUADLET_REQUEST:
  649. return RCODE_COMPLETE;
  650. default:
  651. return RCODE_TYPE_ERROR;
  652. }
  653. }
  654. static int tgt_agent_rw_unsolicited_status_enable(struct fw_card *card,
  655. int tcode, void *data, struct sbp_target_agent *agent)
  656. {
  657. switch (tcode) {
  658. case TCODE_WRITE_QUADLET_REQUEST:
  659. pr_debug("tgt_agent UNSOLICITED_STATUS_ENABLE\n");
  660. /* ignored as we don't send unsolicited status */
  661. return RCODE_COMPLETE;
  662. case TCODE_READ_QUADLET_REQUEST:
  663. return RCODE_COMPLETE;
  664. default:
  665. return RCODE_TYPE_ERROR;
  666. }
  667. }
  668. static void tgt_agent_rw(struct fw_card *card, struct fw_request *request,
  669. int tcode, int destination, int source, int generation,
  670. unsigned long long offset, void *data, size_t length,
  671. void *callback_data)
  672. {
  673. struct sbp_target_agent *agent = callback_data;
  674. struct sbp_session *sess = agent->login->sess;
  675. int sess_gen, sess_node, rcode;
  676. spin_lock_bh(&sess->lock);
  677. sess_gen = sess->generation;
  678. sess_node = sess->node_id;
  679. spin_unlock_bh(&sess->lock);
  680. if (generation != sess_gen) {
  681. pr_notice("ignoring request with wrong generation\n");
  682. rcode = RCODE_TYPE_ERROR;
  683. goto out;
  684. }
  685. if (source != sess_node) {
  686. pr_notice("ignoring request from foreign node (%x != %x)\n",
  687. source, sess_node);
  688. rcode = RCODE_TYPE_ERROR;
  689. goto out;
  690. }
  691. /* turn offset into the offset from the start of the block */
  692. offset -= agent->handler.offset;
  693. if (offset == 0x00 && length == 4) {
  694. /* AGENT_STATE */
  695. rcode = tgt_agent_rw_agent_state(card, tcode, data, agent);
  696. } else if (offset == 0x04 && length == 4) {
  697. /* AGENT_RESET */
  698. rcode = tgt_agent_rw_agent_reset(card, tcode, data, agent);
  699. } else if (offset == 0x08 && length == 8) {
  700. /* ORB_POINTER */
  701. rcode = tgt_agent_rw_orb_pointer(card, tcode, data, agent);
  702. } else if (offset == 0x10 && length == 4) {
  703. /* DOORBELL */
  704. rcode = tgt_agent_rw_doorbell(card, tcode, data, agent);
  705. } else if (offset == 0x14 && length == 4) {
  706. /* UNSOLICITED_STATUS_ENABLE */
  707. rcode = tgt_agent_rw_unsolicited_status_enable(card, tcode,
  708. data, agent);
  709. } else {
  710. rcode = RCODE_ADDRESS_ERROR;
  711. }
  712. out:
  713. fw_send_response(card, request, rcode);
  714. }
  715. static void sbp_handle_command(struct sbp_target_request *);
  716. static int sbp_send_status(struct sbp_target_request *);
  717. static void sbp_free_request(struct sbp_target_request *);
  718. static void tgt_agent_process_work(struct work_struct *work)
  719. {
  720. struct sbp_target_request *req =
  721. container_of(work, struct sbp_target_request, work);
  722. pr_debug("tgt_orb ptr:0x%llx next_ORB:0x%llx data_descriptor:0x%llx misc:0x%x\n",
  723. req->orb_pointer,
  724. sbp2_pointer_to_addr(&req->orb.next_orb),
  725. sbp2_pointer_to_addr(&req->orb.data_descriptor),
  726. be32_to_cpu(req->orb.misc));
  727. if (req->orb_pointer >> 32)
  728. pr_debug("ORB with high bits set\n");
  729. switch (ORB_REQUEST_FORMAT(be32_to_cpu(req->orb.misc))) {
  730. case 0:/* Format specified by this standard */
  731. sbp_handle_command(req);
  732. return;
  733. case 1: /* Reserved for future standardization */
  734. case 2: /* Vendor-dependent */
  735. req->status.status |= cpu_to_be32(
  736. STATUS_BLOCK_RESP(
  737. STATUS_RESP_REQUEST_COMPLETE) |
  738. STATUS_BLOCK_DEAD(0) |
  739. STATUS_BLOCK_LEN(1) |
  740. STATUS_BLOCK_SBP_STATUS(
  741. SBP_STATUS_REQ_TYPE_NOTSUPP));
  742. sbp_send_status(req);
  743. sbp_free_request(req);
  744. return;
  745. case 3: /* Dummy ORB */
  746. req->status.status |= cpu_to_be32(
  747. STATUS_BLOCK_RESP(
  748. STATUS_RESP_REQUEST_COMPLETE) |
  749. STATUS_BLOCK_DEAD(0) |
  750. STATUS_BLOCK_LEN(1) |
  751. STATUS_BLOCK_SBP_STATUS(
  752. SBP_STATUS_DUMMY_ORB_COMPLETE));
  753. sbp_send_status(req);
  754. sbp_free_request(req);
  755. return;
  756. default:
  757. BUG();
  758. }
  759. }
  760. /* used to double-check we haven't been issued an AGENT_RESET */
  761. static inline bool tgt_agent_check_active(struct sbp_target_agent *agent)
  762. {
  763. bool active;
  764. spin_lock_bh(&agent->lock);
  765. active = (agent->state == AGENT_STATE_ACTIVE);
  766. spin_unlock_bh(&agent->lock);
  767. return active;
  768. }
  769. static void tgt_agent_fetch_work(struct work_struct *work)
  770. {
  771. struct sbp_target_agent *agent =
  772. container_of(work, struct sbp_target_agent, work);
  773. struct sbp_session *sess = agent->login->sess;
  774. struct sbp_target_request *req;
  775. int ret;
  776. bool doorbell = agent->doorbell;
  777. u64 next_orb = agent->orb_pointer;
  778. while (next_orb && tgt_agent_check_active(agent)) {
  779. req = kzalloc(sizeof(*req), GFP_KERNEL);
  780. if (!req) {
  781. spin_lock_bh(&agent->lock);
  782. agent->state = AGENT_STATE_DEAD;
  783. spin_unlock_bh(&agent->lock);
  784. return;
  785. }
  786. req->login = agent->login;
  787. req->orb_pointer = next_orb;
  788. req->status.status = cpu_to_be32(STATUS_BLOCK_ORB_OFFSET_HIGH(
  789. req->orb_pointer >> 32));
  790. req->status.orb_low = cpu_to_be32(
  791. req->orb_pointer & 0xfffffffc);
  792. /* read in the ORB */
  793. ret = sbp_run_transaction(sess->card, TCODE_READ_BLOCK_REQUEST,
  794. sess->node_id, sess->generation, sess->speed,
  795. req->orb_pointer, &req->orb, sizeof(req->orb));
  796. if (ret != RCODE_COMPLETE) {
  797. pr_debug("tgt_orb fetch failed: %x\n", ret);
  798. req->status.status |= cpu_to_be32(
  799. STATUS_BLOCK_SRC(
  800. STATUS_SRC_ORB_FINISHED) |
  801. STATUS_BLOCK_RESP(
  802. STATUS_RESP_TRANSPORT_FAILURE) |
  803. STATUS_BLOCK_DEAD(1) |
  804. STATUS_BLOCK_LEN(1) |
  805. STATUS_BLOCK_SBP_STATUS(
  806. SBP_STATUS_UNSPECIFIED_ERROR));
  807. spin_lock_bh(&agent->lock);
  808. agent->state = AGENT_STATE_DEAD;
  809. spin_unlock_bh(&agent->lock);
  810. sbp_send_status(req);
  811. sbp_free_request(req);
  812. return;
  813. }
  814. /* check the next_ORB field */
  815. if (be32_to_cpu(req->orb.next_orb.high) & 0x80000000) {
  816. next_orb = 0;
  817. req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC(
  818. STATUS_SRC_ORB_FINISHED));
  819. } else {
  820. next_orb = sbp2_pointer_to_addr(&req->orb.next_orb);
  821. req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC(
  822. STATUS_SRC_ORB_CONTINUING));
  823. }
  824. if (tgt_agent_check_active(agent) && !doorbell) {
  825. INIT_WORK(&req->work, tgt_agent_process_work);
  826. queue_work(system_unbound_wq, &req->work);
  827. } else {
  828. /* don't process this request, just check next_ORB */
  829. sbp_free_request(req);
  830. }
  831. spin_lock_bh(&agent->lock);
  832. doorbell = agent->doorbell = false;
  833. /* check if we should carry on processing */
  834. if (next_orb)
  835. agent->orb_pointer = next_orb;
  836. else
  837. agent->state = AGENT_STATE_SUSPENDED;
  838. spin_unlock_bh(&agent->lock);
  839. };
  840. }
  841. static struct sbp_target_agent *sbp_target_agent_register(
  842. struct sbp_login_descriptor *login)
  843. {
  844. struct sbp_target_agent *agent;
  845. int ret;
  846. agent = kmalloc(sizeof(*agent), GFP_KERNEL);
  847. if (!agent)
  848. return ERR_PTR(-ENOMEM);
  849. spin_lock_init(&agent->lock);
  850. agent->handler.length = 0x20;
  851. agent->handler.address_callback = tgt_agent_rw;
  852. agent->handler.callback_data = agent;
  853. agent->login = login;
  854. agent->state = AGENT_STATE_RESET;
  855. INIT_WORK(&agent->work, tgt_agent_fetch_work);
  856. agent->orb_pointer = 0;
  857. agent->doorbell = false;
  858. ret = fw_core_add_address_handler(&agent->handler,
  859. &sbp_register_region);
  860. if (ret < 0) {
  861. kfree(agent);
  862. return ERR_PTR(ret);
  863. }
  864. return agent;
  865. }
  866. static void sbp_target_agent_unregister(struct sbp_target_agent *agent)
  867. {
  868. fw_core_remove_address_handler(&agent->handler);
  869. cancel_work_sync(&agent->work);
  870. kfree(agent);
  871. }
  872. /*
  873. * Simple wrapper around fw_run_transaction that retries the transaction several
  874. * times in case of failure, with an exponential backoff.
  875. */
  876. static int sbp_run_transaction(struct fw_card *card, int tcode, int destination_id,
  877. int generation, int speed, unsigned long long offset,
  878. void *payload, size_t length)
  879. {
  880. int attempt, ret, delay;
  881. for (attempt = 1; attempt <= 5; attempt++) {
  882. ret = fw_run_transaction(card, tcode, destination_id,
  883. generation, speed, offset, payload, length);
  884. switch (ret) {
  885. case RCODE_COMPLETE:
  886. case RCODE_TYPE_ERROR:
  887. case RCODE_ADDRESS_ERROR:
  888. case RCODE_GENERATION:
  889. return ret;
  890. default:
  891. delay = 5 * attempt * attempt;
  892. usleep_range(delay, delay * 2);
  893. }
  894. }
  895. return ret;
  896. }
  897. /*
  898. * Wrapper around sbp_run_transaction that gets the card, destination,
  899. * generation and speed out of the request's session.
  900. */
  901. static int sbp_run_request_transaction(struct sbp_target_request *req,
  902. int tcode, unsigned long long offset, void *payload,
  903. size_t length)
  904. {
  905. struct sbp_login_descriptor *login = req->login;
  906. struct sbp_session *sess = login->sess;
  907. struct fw_card *card;
  908. int node_id, generation, speed, ret;
  909. spin_lock_bh(&sess->lock);
  910. card = fw_card_get(sess->card);
  911. node_id = sess->node_id;
  912. generation = sess->generation;
  913. speed = sess->speed;
  914. spin_unlock_bh(&sess->lock);
  915. ret = sbp_run_transaction(card, tcode, node_id, generation, speed,
  916. offset, payload, length);
  917. fw_card_put(card);
  918. return ret;
  919. }
  920. static int sbp_fetch_command(struct sbp_target_request *req)
  921. {
  922. int ret, cmd_len, copy_len;
  923. cmd_len = scsi_command_size(req->orb.command_block);
  924. req->cmd_buf = kmalloc(cmd_len, GFP_KERNEL);
  925. if (!req->cmd_buf)
  926. return -ENOMEM;
  927. memcpy(req->cmd_buf, req->orb.command_block,
  928. min_t(int, cmd_len, sizeof(req->orb.command_block)));
  929. if (cmd_len > sizeof(req->orb.command_block)) {
  930. pr_debug("sbp_fetch_command: filling in long command\n");
  931. copy_len = cmd_len - sizeof(req->orb.command_block);
  932. ret = sbp_run_request_transaction(req,
  933. TCODE_READ_BLOCK_REQUEST,
  934. req->orb_pointer + sizeof(req->orb),
  935. req->cmd_buf + sizeof(req->orb.command_block),
  936. copy_len);
  937. if (ret != RCODE_COMPLETE)
  938. return -EIO;
  939. }
  940. return 0;
  941. }
  942. static int sbp_fetch_page_table(struct sbp_target_request *req)
  943. {
  944. int pg_tbl_sz, ret;
  945. struct sbp_page_table_entry *pg_tbl;
  946. if (!CMDBLK_ORB_PG_TBL_PRESENT(be32_to_cpu(req->orb.misc)))
  947. return 0;
  948. pg_tbl_sz = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc)) *
  949. sizeof(struct sbp_page_table_entry);
  950. pg_tbl = kmalloc(pg_tbl_sz, GFP_KERNEL);
  951. if (!pg_tbl)
  952. return -ENOMEM;
  953. ret = sbp_run_request_transaction(req, TCODE_READ_BLOCK_REQUEST,
  954. sbp2_pointer_to_addr(&req->orb.data_descriptor),
  955. pg_tbl, pg_tbl_sz);
  956. if (ret != RCODE_COMPLETE) {
  957. kfree(pg_tbl);
  958. return -EIO;
  959. }
  960. req->pg_tbl = pg_tbl;
  961. return 0;
  962. }
  963. static void sbp_calc_data_length_direction(struct sbp_target_request *req,
  964. u32 *data_len, enum dma_data_direction *data_dir)
  965. {
  966. int data_size, direction, idx;
  967. data_size = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc));
  968. direction = CMDBLK_ORB_DIRECTION(be32_to_cpu(req->orb.misc));
  969. if (!data_size) {
  970. *data_len = 0;
  971. *data_dir = DMA_NONE;
  972. return;
  973. }
  974. *data_dir = direction ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  975. if (req->pg_tbl) {
  976. *data_len = 0;
  977. for (idx = 0; idx < data_size; idx++) {
  978. *data_len += be16_to_cpu(
  979. req->pg_tbl[idx].segment_length);
  980. }
  981. } else {
  982. *data_len = data_size;
  983. }
  984. }
  985. static void sbp_handle_command(struct sbp_target_request *req)
  986. {
  987. struct sbp_login_descriptor *login = req->login;
  988. struct sbp_session *sess = login->sess;
  989. int ret, unpacked_lun;
  990. u32 data_length;
  991. enum dma_data_direction data_dir;
  992. ret = sbp_fetch_command(req);
  993. if (ret) {
  994. pr_debug("sbp_handle_command: fetch command failed: %d\n", ret);
  995. goto err;
  996. }
  997. ret = sbp_fetch_page_table(req);
  998. if (ret) {
  999. pr_debug("sbp_handle_command: fetch page table failed: %d\n",
  1000. ret);
  1001. goto err;
  1002. }
  1003. unpacked_lun = req->login->lun->unpacked_lun;
  1004. sbp_calc_data_length_direction(req, &data_length, &data_dir);
  1005. pr_debug("sbp_handle_command ORB:0x%llx unpacked_lun:%d data_len:%d data_dir:%d\n",
  1006. req->orb_pointer, unpacked_lun, data_length, data_dir);
  1007. target_submit_cmd(&req->se_cmd, sess->se_sess, req->cmd_buf,
  1008. req->sense_buf, unpacked_lun, data_length,
  1009. MSG_SIMPLE_TAG, data_dir, 0);
  1010. return;
  1011. err:
  1012. req->status.status |= cpu_to_be32(
  1013. STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  1014. STATUS_BLOCK_DEAD(0) |
  1015. STATUS_BLOCK_LEN(1) |
  1016. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  1017. sbp_send_status(req);
  1018. sbp_free_request(req);
  1019. }
  1020. /*
  1021. * DMA_TO_DEVICE = read from initiator (SCSI WRITE)
  1022. * DMA_FROM_DEVICE = write to initiator (SCSI READ)
  1023. */
  1024. static int sbp_rw_data(struct sbp_target_request *req)
  1025. {
  1026. struct sbp_session *sess = req->login->sess;
  1027. int tcode, sg_miter_flags, max_payload, pg_size, speed, node_id,
  1028. generation, num_pte, length, tfr_length,
  1029. rcode = RCODE_COMPLETE;
  1030. struct sbp_page_table_entry *pte;
  1031. unsigned long long offset;
  1032. struct fw_card *card;
  1033. struct sg_mapping_iter iter;
  1034. if (req->se_cmd.data_direction == DMA_FROM_DEVICE) {
  1035. tcode = TCODE_WRITE_BLOCK_REQUEST;
  1036. sg_miter_flags = SG_MITER_FROM_SG;
  1037. } else {
  1038. tcode = TCODE_READ_BLOCK_REQUEST;
  1039. sg_miter_flags = SG_MITER_TO_SG;
  1040. }
  1041. max_payload = 4 << CMDBLK_ORB_MAX_PAYLOAD(be32_to_cpu(req->orb.misc));
  1042. speed = CMDBLK_ORB_SPEED(be32_to_cpu(req->orb.misc));
  1043. pg_size = CMDBLK_ORB_PG_SIZE(be32_to_cpu(req->orb.misc));
  1044. if (pg_size) {
  1045. pr_err("sbp_run_transaction: page size ignored\n");
  1046. pg_size = 0x100 << pg_size;
  1047. }
  1048. spin_lock_bh(&sess->lock);
  1049. card = fw_card_get(sess->card);
  1050. node_id = sess->node_id;
  1051. generation = sess->generation;
  1052. spin_unlock_bh(&sess->lock);
  1053. if (req->pg_tbl) {
  1054. pte = req->pg_tbl;
  1055. num_pte = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc));
  1056. offset = 0;
  1057. length = 0;
  1058. } else {
  1059. pte = NULL;
  1060. num_pte = 0;
  1061. offset = sbp2_pointer_to_addr(&req->orb.data_descriptor);
  1062. length = req->se_cmd.data_length;
  1063. }
  1064. sg_miter_start(&iter, req->se_cmd.t_data_sg, req->se_cmd.t_data_nents,
  1065. sg_miter_flags);
  1066. while (length || num_pte) {
  1067. if (!length) {
  1068. offset = (u64)be16_to_cpu(pte->segment_base_hi) << 32 |
  1069. be32_to_cpu(pte->segment_base_lo);
  1070. length = be16_to_cpu(pte->segment_length);
  1071. pte++;
  1072. num_pte--;
  1073. }
  1074. sg_miter_next(&iter);
  1075. tfr_length = min3(length, max_payload, (int)iter.length);
  1076. /* FIXME: take page_size into account */
  1077. rcode = sbp_run_transaction(card, tcode, node_id,
  1078. generation, speed,
  1079. offset, iter.addr, tfr_length);
  1080. if (rcode != RCODE_COMPLETE)
  1081. break;
  1082. length -= tfr_length;
  1083. offset += tfr_length;
  1084. iter.consumed = tfr_length;
  1085. }
  1086. sg_miter_stop(&iter);
  1087. fw_card_put(card);
  1088. if (rcode == RCODE_COMPLETE) {
  1089. WARN_ON(length != 0);
  1090. return 0;
  1091. } else {
  1092. return -EIO;
  1093. }
  1094. }
  1095. static int sbp_send_status(struct sbp_target_request *req)
  1096. {
  1097. int ret, length;
  1098. struct sbp_login_descriptor *login = req->login;
  1099. length = (((be32_to_cpu(req->status.status) >> 24) & 0x07) + 1) * 4;
  1100. ret = sbp_run_request_transaction(req, TCODE_WRITE_BLOCK_REQUEST,
  1101. login->status_fifo_addr, &req->status, length);
  1102. if (ret != RCODE_COMPLETE) {
  1103. pr_debug("sbp_send_status: write failed: 0x%x\n", ret);
  1104. return -EIO;
  1105. }
  1106. pr_debug("sbp_send_status: status write complete for ORB: 0x%llx\n",
  1107. req->orb_pointer);
  1108. return 0;
  1109. }
  1110. static void sbp_sense_mangle(struct sbp_target_request *req)
  1111. {
  1112. struct se_cmd *se_cmd = &req->se_cmd;
  1113. u8 *sense = req->sense_buf;
  1114. u8 *status = req->status.data;
  1115. WARN_ON(se_cmd->scsi_sense_length < 18);
  1116. switch (sense[0] & 0x7f) { /* sfmt */
  1117. case 0x70: /* current, fixed */
  1118. status[0] = 0 << 6;
  1119. break;
  1120. case 0x71: /* deferred, fixed */
  1121. status[0] = 1 << 6;
  1122. break;
  1123. case 0x72: /* current, descriptor */
  1124. case 0x73: /* deferred, descriptor */
  1125. default:
  1126. /*
  1127. * TODO: SBP-3 specifies what we should do with descriptor
  1128. * format sense data
  1129. */
  1130. pr_err("sbp_send_sense: unknown sense format: 0x%x\n",
  1131. sense[0]);
  1132. req->status.status |= cpu_to_be32(
  1133. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1134. STATUS_BLOCK_DEAD(0) |
  1135. STATUS_BLOCK_LEN(1) |
  1136. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQUEST_ABORTED));
  1137. return;
  1138. }
  1139. status[0] |= se_cmd->scsi_status & 0x3f;/* status */
  1140. status[1] =
  1141. (sense[0] & 0x80) | /* valid */
  1142. ((sense[2] & 0xe0) >> 1) | /* mark, eom, ili */
  1143. (sense[2] & 0x0f); /* sense_key */
  1144. status[2] = se_cmd->scsi_asc; /* sense_code */
  1145. status[3] = se_cmd->scsi_ascq; /* sense_qualifier */
  1146. /* information */
  1147. status[4] = sense[3];
  1148. status[5] = sense[4];
  1149. status[6] = sense[5];
  1150. status[7] = sense[6];
  1151. /* CDB-dependent */
  1152. status[8] = sense[8];
  1153. status[9] = sense[9];
  1154. status[10] = sense[10];
  1155. status[11] = sense[11];
  1156. /* fru */
  1157. status[12] = sense[14];
  1158. /* sense_key-dependent */
  1159. status[13] = sense[15];
  1160. status[14] = sense[16];
  1161. status[15] = sense[17];
  1162. req->status.status |= cpu_to_be32(
  1163. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1164. STATUS_BLOCK_DEAD(0) |
  1165. STATUS_BLOCK_LEN(5) |
  1166. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  1167. }
  1168. static int sbp_send_sense(struct sbp_target_request *req)
  1169. {
  1170. struct se_cmd *se_cmd = &req->se_cmd;
  1171. if (se_cmd->scsi_sense_length) {
  1172. sbp_sense_mangle(req);
  1173. } else {
  1174. req->status.status |= cpu_to_be32(
  1175. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1176. STATUS_BLOCK_DEAD(0) |
  1177. STATUS_BLOCK_LEN(1) |
  1178. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  1179. }
  1180. return sbp_send_status(req);
  1181. }
  1182. static void sbp_free_request(struct sbp_target_request *req)
  1183. {
  1184. kfree(req->pg_tbl);
  1185. kfree(req->cmd_buf);
  1186. kfree(req);
  1187. }
  1188. static void sbp_mgt_agent_process(struct work_struct *work)
  1189. {
  1190. struct sbp_management_agent *agent =
  1191. container_of(work, struct sbp_management_agent, work);
  1192. struct sbp_management_request *req = agent->request;
  1193. int ret;
  1194. int status_data_len = 0;
  1195. /* fetch the ORB from the initiator */
  1196. ret = sbp_run_transaction(req->card, TCODE_READ_BLOCK_REQUEST,
  1197. req->node_addr, req->generation, req->speed,
  1198. agent->orb_offset, &req->orb, sizeof(req->orb));
  1199. if (ret != RCODE_COMPLETE) {
  1200. pr_debug("mgt_orb fetch failed: %x\n", ret);
  1201. goto out;
  1202. }
  1203. pr_debug("mgt_orb ptr1:0x%llx ptr2:0x%llx misc:0x%x len:0x%x status_fifo:0x%llx\n",
  1204. sbp2_pointer_to_addr(&req->orb.ptr1),
  1205. sbp2_pointer_to_addr(&req->orb.ptr2),
  1206. be32_to_cpu(req->orb.misc), be32_to_cpu(req->orb.length),
  1207. sbp2_pointer_to_addr(&req->orb.status_fifo));
  1208. if (!ORB_NOTIFY(be32_to_cpu(req->orb.misc)) ||
  1209. ORB_REQUEST_FORMAT(be32_to_cpu(req->orb.misc)) != 0) {
  1210. pr_err("mgt_orb bad request\n");
  1211. goto out;
  1212. }
  1213. switch (MANAGEMENT_ORB_FUNCTION(be32_to_cpu(req->orb.misc))) {
  1214. case MANAGEMENT_ORB_FUNCTION_LOGIN:
  1215. sbp_management_request_login(agent, req, &status_data_len);
  1216. break;
  1217. case MANAGEMENT_ORB_FUNCTION_QUERY_LOGINS:
  1218. sbp_management_request_query_logins(agent, req,
  1219. &status_data_len);
  1220. break;
  1221. case MANAGEMENT_ORB_FUNCTION_RECONNECT:
  1222. sbp_management_request_reconnect(agent, req, &status_data_len);
  1223. break;
  1224. case MANAGEMENT_ORB_FUNCTION_SET_PASSWORD:
  1225. pr_notice("SET PASSWORD not implemented\n");
  1226. req->status.status = cpu_to_be32(
  1227. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1228. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  1229. break;
  1230. case MANAGEMENT_ORB_FUNCTION_LOGOUT:
  1231. sbp_management_request_logout(agent, req, &status_data_len);
  1232. break;
  1233. case MANAGEMENT_ORB_FUNCTION_ABORT_TASK:
  1234. pr_notice("ABORT TASK not implemented\n");
  1235. req->status.status = cpu_to_be32(
  1236. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1237. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  1238. break;
  1239. case MANAGEMENT_ORB_FUNCTION_ABORT_TASK_SET:
  1240. pr_notice("ABORT TASK SET not implemented\n");
  1241. req->status.status = cpu_to_be32(
  1242. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1243. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  1244. break;
  1245. case MANAGEMENT_ORB_FUNCTION_LOGICAL_UNIT_RESET:
  1246. pr_notice("LOGICAL UNIT RESET not implemented\n");
  1247. req->status.status = cpu_to_be32(
  1248. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1249. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  1250. break;
  1251. case MANAGEMENT_ORB_FUNCTION_TARGET_RESET:
  1252. pr_notice("TARGET RESET not implemented\n");
  1253. req->status.status = cpu_to_be32(
  1254. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1255. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  1256. break;
  1257. default:
  1258. pr_notice("unknown management function 0x%x\n",
  1259. MANAGEMENT_ORB_FUNCTION(be32_to_cpu(req->orb.misc)));
  1260. req->status.status = cpu_to_be32(
  1261. STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  1262. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  1263. break;
  1264. }
  1265. req->status.status |= cpu_to_be32(
  1266. STATUS_BLOCK_SRC(1) | /* Response to ORB, next_ORB absent */
  1267. STATUS_BLOCK_LEN(DIV_ROUND_UP(status_data_len, 4) + 1) |
  1268. STATUS_BLOCK_ORB_OFFSET_HIGH(agent->orb_offset >> 32));
  1269. req->status.orb_low = cpu_to_be32(agent->orb_offset);
  1270. /* write the status block back to the initiator */
  1271. ret = sbp_run_transaction(req->card, TCODE_WRITE_BLOCK_REQUEST,
  1272. req->node_addr, req->generation, req->speed,
  1273. sbp2_pointer_to_addr(&req->orb.status_fifo),
  1274. &req->status, 8 + status_data_len);
  1275. if (ret != RCODE_COMPLETE) {
  1276. pr_debug("mgt_orb status write failed: %x\n", ret);
  1277. goto out;
  1278. }
  1279. out:
  1280. fw_card_put(req->card);
  1281. kfree(req);
  1282. spin_lock_bh(&agent->lock);
  1283. agent->state = MANAGEMENT_AGENT_STATE_IDLE;
  1284. spin_unlock_bh(&agent->lock);
  1285. }
  1286. static void sbp_mgt_agent_rw(struct fw_card *card,
  1287. struct fw_request *request, int tcode, int destination, int source,
  1288. int generation, unsigned long long offset, void *data, size_t length,
  1289. void *callback_data)
  1290. {
  1291. struct sbp_management_agent *agent = callback_data;
  1292. struct sbp2_pointer *ptr = data;
  1293. int rcode = RCODE_ADDRESS_ERROR;
  1294. if (!agent->tport->enable)
  1295. goto out;
  1296. if ((offset != agent->handler.offset) || (length != 8))
  1297. goto out;
  1298. if (tcode == TCODE_WRITE_BLOCK_REQUEST) {
  1299. struct sbp_management_request *req;
  1300. int prev_state;
  1301. spin_lock_bh(&agent->lock);
  1302. prev_state = agent->state;
  1303. agent->state = MANAGEMENT_AGENT_STATE_BUSY;
  1304. spin_unlock_bh(&agent->lock);
  1305. if (prev_state == MANAGEMENT_AGENT_STATE_BUSY) {
  1306. pr_notice("ignoring management request while busy\n");
  1307. rcode = RCODE_CONFLICT_ERROR;
  1308. goto out;
  1309. }
  1310. req = kzalloc(sizeof(*req), GFP_ATOMIC);
  1311. if (!req) {
  1312. rcode = RCODE_CONFLICT_ERROR;
  1313. goto out;
  1314. }
  1315. req->card = fw_card_get(card);
  1316. req->generation = generation;
  1317. req->node_addr = source;
  1318. req->speed = fw_get_request_speed(request);
  1319. agent->orb_offset = sbp2_pointer_to_addr(ptr);
  1320. agent->request = req;
  1321. queue_work(system_unbound_wq, &agent->work);
  1322. rcode = RCODE_COMPLETE;
  1323. } else if (tcode == TCODE_READ_BLOCK_REQUEST) {
  1324. addr_to_sbp2_pointer(agent->orb_offset, ptr);
  1325. rcode = RCODE_COMPLETE;
  1326. } else {
  1327. rcode = RCODE_TYPE_ERROR;
  1328. }
  1329. out:
  1330. fw_send_response(card, request, rcode);
  1331. }
  1332. static struct sbp_management_agent *sbp_management_agent_register(
  1333. struct sbp_tport *tport)
  1334. {
  1335. int ret;
  1336. struct sbp_management_agent *agent;
  1337. agent = kmalloc(sizeof(*agent), GFP_KERNEL);
  1338. if (!agent)
  1339. return ERR_PTR(-ENOMEM);
  1340. spin_lock_init(&agent->lock);
  1341. agent->tport = tport;
  1342. agent->handler.length = 0x08;
  1343. agent->handler.address_callback = sbp_mgt_agent_rw;
  1344. agent->handler.callback_data = agent;
  1345. agent->state = MANAGEMENT_AGENT_STATE_IDLE;
  1346. INIT_WORK(&agent->work, sbp_mgt_agent_process);
  1347. agent->orb_offset = 0;
  1348. agent->request = NULL;
  1349. ret = fw_core_add_address_handler(&agent->handler,
  1350. &sbp_register_region);
  1351. if (ret < 0) {
  1352. kfree(agent);
  1353. return ERR_PTR(ret);
  1354. }
  1355. return agent;
  1356. }
  1357. static void sbp_management_agent_unregister(struct sbp_management_agent *agent)
  1358. {
  1359. fw_core_remove_address_handler(&agent->handler);
  1360. cancel_work_sync(&agent->work);
  1361. kfree(agent);
  1362. }
  1363. static int sbp_check_true(struct se_portal_group *se_tpg)
  1364. {
  1365. return 1;
  1366. }
  1367. static int sbp_check_false(struct se_portal_group *se_tpg)
  1368. {
  1369. return 0;
  1370. }
  1371. static char *sbp_get_fabric_name(void)
  1372. {
  1373. return "sbp";
  1374. }
  1375. static char *sbp_get_fabric_wwn(struct se_portal_group *se_tpg)
  1376. {
  1377. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1378. struct sbp_tport *tport = tpg->tport;
  1379. return &tport->tport_name[0];
  1380. }
  1381. static u16 sbp_get_tag(struct se_portal_group *se_tpg)
  1382. {
  1383. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1384. return tpg->tport_tpgt;
  1385. }
  1386. static u32 sbp_get_default_depth(struct se_portal_group *se_tpg)
  1387. {
  1388. return 1;
  1389. }
  1390. static struct se_node_acl *sbp_alloc_fabric_acl(struct se_portal_group *se_tpg)
  1391. {
  1392. struct sbp_nacl *nacl;
  1393. nacl = kzalloc(sizeof(struct sbp_nacl), GFP_KERNEL);
  1394. if (!nacl) {
  1395. pr_err("Unable to alocate struct sbp_nacl\n");
  1396. return NULL;
  1397. }
  1398. return &nacl->se_node_acl;
  1399. }
  1400. static void sbp_release_fabric_acl(
  1401. struct se_portal_group *se_tpg,
  1402. struct se_node_acl *se_nacl)
  1403. {
  1404. struct sbp_nacl *nacl =
  1405. container_of(se_nacl, struct sbp_nacl, se_node_acl);
  1406. kfree(nacl);
  1407. }
  1408. static u32 sbp_tpg_get_inst_index(struct se_portal_group *se_tpg)
  1409. {
  1410. return 1;
  1411. }
  1412. static void sbp_release_cmd(struct se_cmd *se_cmd)
  1413. {
  1414. struct sbp_target_request *req = container_of(se_cmd,
  1415. struct sbp_target_request, se_cmd);
  1416. sbp_free_request(req);
  1417. }
  1418. static int sbp_shutdown_session(struct se_session *se_sess)
  1419. {
  1420. return 0;
  1421. }
  1422. static void sbp_close_session(struct se_session *se_sess)
  1423. {
  1424. return;
  1425. }
  1426. static u32 sbp_sess_get_index(struct se_session *se_sess)
  1427. {
  1428. return 0;
  1429. }
  1430. static int sbp_write_pending(struct se_cmd *se_cmd)
  1431. {
  1432. struct sbp_target_request *req = container_of(se_cmd,
  1433. struct sbp_target_request, se_cmd);
  1434. int ret;
  1435. ret = sbp_rw_data(req);
  1436. if (ret) {
  1437. req->status.status |= cpu_to_be32(
  1438. STATUS_BLOCK_RESP(
  1439. STATUS_RESP_TRANSPORT_FAILURE) |
  1440. STATUS_BLOCK_DEAD(0) |
  1441. STATUS_BLOCK_LEN(1) |
  1442. STATUS_BLOCK_SBP_STATUS(
  1443. SBP_STATUS_UNSPECIFIED_ERROR));
  1444. sbp_send_status(req);
  1445. return ret;
  1446. }
  1447. target_execute_cmd(se_cmd);
  1448. return 0;
  1449. }
  1450. static int sbp_write_pending_status(struct se_cmd *se_cmd)
  1451. {
  1452. return 0;
  1453. }
  1454. static void sbp_set_default_node_attrs(struct se_node_acl *nacl)
  1455. {
  1456. return;
  1457. }
  1458. static u32 sbp_get_task_tag(struct se_cmd *se_cmd)
  1459. {
  1460. struct sbp_target_request *req = container_of(se_cmd,
  1461. struct sbp_target_request, se_cmd);
  1462. /* only used for printk until we do TMRs */
  1463. return (u32)req->orb_pointer;
  1464. }
  1465. static int sbp_get_cmd_state(struct se_cmd *se_cmd)
  1466. {
  1467. return 0;
  1468. }
  1469. static int sbp_queue_data_in(struct se_cmd *se_cmd)
  1470. {
  1471. struct sbp_target_request *req = container_of(se_cmd,
  1472. struct sbp_target_request, se_cmd);
  1473. int ret;
  1474. ret = sbp_rw_data(req);
  1475. if (ret) {
  1476. req->status.status |= cpu_to_be32(
  1477. STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  1478. STATUS_BLOCK_DEAD(0) |
  1479. STATUS_BLOCK_LEN(1) |
  1480. STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  1481. sbp_send_status(req);
  1482. return ret;
  1483. }
  1484. return sbp_send_sense(req);
  1485. }
  1486. /*
  1487. * Called after command (no data transfer) or after the write (to device)
  1488. * operation is completed
  1489. */
  1490. static int sbp_queue_status(struct se_cmd *se_cmd)
  1491. {
  1492. struct sbp_target_request *req = container_of(se_cmd,
  1493. struct sbp_target_request, se_cmd);
  1494. return sbp_send_sense(req);
  1495. }
  1496. static int sbp_queue_tm_rsp(struct se_cmd *se_cmd)
  1497. {
  1498. return 0;
  1499. }
  1500. static u16 sbp_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
  1501. {
  1502. return 0;
  1503. }
  1504. static u16 sbp_get_fabric_sense_len(void)
  1505. {
  1506. return 0;
  1507. }
  1508. static int sbp_check_stop_free(struct se_cmd *se_cmd)
  1509. {
  1510. struct sbp_target_request *req = container_of(se_cmd,
  1511. struct sbp_target_request, se_cmd);
  1512. transport_generic_free_cmd(&req->se_cmd, 0);
  1513. return 1;
  1514. }
  1515. /*
  1516. * Handlers for Serial Bus Protocol 2/3 (SBP-2 / SBP-3)
  1517. */
  1518. static u8 sbp_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  1519. {
  1520. /*
  1521. * Return a IEEE 1394 SCSI Protocol identifier for loopback operations
  1522. * This is defined in section 7.5.1 Table 362 in spc4r17
  1523. */
  1524. return SCSI_PROTOCOL_SBP;
  1525. }
  1526. static u32 sbp_get_pr_transport_id(
  1527. struct se_portal_group *se_tpg,
  1528. struct se_node_acl *se_nacl,
  1529. struct t10_pr_registration *pr_reg,
  1530. int *format_code,
  1531. unsigned char *buf)
  1532. {
  1533. int ret;
  1534. /*
  1535. * Set PROTOCOL IDENTIFIER to 3h for SBP
  1536. */
  1537. buf[0] = SCSI_PROTOCOL_SBP;
  1538. /*
  1539. * From spc4r17, 7.5.4.4 TransportID for initiator ports using SCSI
  1540. * over IEEE 1394
  1541. */
  1542. ret = hex2bin(&buf[8], se_nacl->initiatorname, 8);
  1543. if (ret < 0)
  1544. pr_debug("sbp transport_id: invalid hex string\n");
  1545. /*
  1546. * The IEEE 1394 Transport ID is a hardcoded 24-byte length
  1547. */
  1548. return 24;
  1549. }
  1550. static u32 sbp_get_pr_transport_id_len(
  1551. struct se_portal_group *se_tpg,
  1552. struct se_node_acl *se_nacl,
  1553. struct t10_pr_registration *pr_reg,
  1554. int *format_code)
  1555. {
  1556. *format_code = 0;
  1557. /*
  1558. * From spc4r17, 7.5.4.4 TransportID for initiator ports using SCSI
  1559. * over IEEE 1394
  1560. *
  1561. * The SBP Transport ID is a hardcoded 24-byte length
  1562. */
  1563. return 24;
  1564. }
  1565. /*
  1566. * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
  1567. * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
  1568. */
  1569. static char *sbp_parse_pr_out_transport_id(
  1570. struct se_portal_group *se_tpg,
  1571. const char *buf,
  1572. u32 *out_tid_len,
  1573. char **port_nexus_ptr)
  1574. {
  1575. /*
  1576. * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.4 TransportID
  1577. * for initiator ports using SCSI over SBP Serial SCSI Protocol
  1578. *
  1579. * The TransportID for a IEEE 1394 Initiator Port is of fixed size of
  1580. * 24 bytes, and IEEE 1394 does not contain a I_T nexus identifier,
  1581. * so we return the **port_nexus_ptr set to NULL.
  1582. */
  1583. *port_nexus_ptr = NULL;
  1584. *out_tid_len = 24;
  1585. return (char *)&buf[8];
  1586. }
  1587. static int sbp_count_se_tpg_luns(struct se_portal_group *tpg)
  1588. {
  1589. int i, count = 0;
  1590. spin_lock(&tpg->tpg_lun_lock);
  1591. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  1592. struct se_lun *se_lun = tpg->tpg_lun_list[i];
  1593. if (se_lun->lun_status == TRANSPORT_LUN_STATUS_FREE)
  1594. continue;
  1595. count++;
  1596. }
  1597. spin_unlock(&tpg->tpg_lun_lock);
  1598. return count;
  1599. }
  1600. static int sbp_update_unit_directory(struct sbp_tport *tport)
  1601. {
  1602. int num_luns, num_entries, idx = 0, mgt_agt_addr, ret, i;
  1603. u32 *data;
  1604. if (tport->unit_directory.data) {
  1605. fw_core_remove_descriptor(&tport->unit_directory);
  1606. kfree(tport->unit_directory.data);
  1607. tport->unit_directory.data = NULL;
  1608. }
  1609. if (!tport->enable || !tport->tpg)
  1610. return 0;
  1611. num_luns = sbp_count_se_tpg_luns(&tport->tpg->se_tpg);
  1612. /*
  1613. * Number of entries in the final unit directory:
  1614. * - all of those in the template
  1615. * - management_agent
  1616. * - unit_characteristics
  1617. * - reconnect_timeout
  1618. * - unit unique ID
  1619. * - one for each LUN
  1620. *
  1621. * MUST NOT include leaf or sub-directory entries
  1622. */
  1623. num_entries = ARRAY_SIZE(sbp_unit_directory_template) + 4 + num_luns;
  1624. if (tport->directory_id != -1)
  1625. num_entries++;
  1626. /* allocate num_entries + 4 for the header and unique ID leaf */
  1627. data = kcalloc((num_entries + 4), sizeof(u32), GFP_KERNEL);
  1628. if (!data)
  1629. return -ENOMEM;
  1630. /* directory_length */
  1631. data[idx++] = num_entries << 16;
  1632. /* directory_id */
  1633. if (tport->directory_id != -1)
  1634. data[idx++] = (CSR_DIRECTORY_ID << 24) | tport->directory_id;
  1635. /* unit directory template */
  1636. memcpy(&data[idx], sbp_unit_directory_template,
  1637. sizeof(sbp_unit_directory_template));
  1638. idx += ARRAY_SIZE(sbp_unit_directory_template);
  1639. /* management_agent */
  1640. mgt_agt_addr = (tport->mgt_agt->handler.offset - CSR_REGISTER_BASE) / 4;
  1641. data[idx++] = 0x54000000 | (mgt_agt_addr & 0x00ffffff);
  1642. /* unit_characteristics */
  1643. data[idx++] = 0x3a000000 |
  1644. (((tport->mgt_orb_timeout * 2) << 8) & 0xff00) |
  1645. SBP_ORB_FETCH_SIZE;
  1646. /* reconnect_timeout */
  1647. data[idx++] = 0x3d000000 | (tport->max_reconnect_timeout & 0xffff);
  1648. /* unit unique ID (leaf is just after LUNs) */
  1649. data[idx++] = 0x8d000000 | (num_luns + 1);
  1650. spin_lock(&tport->tpg->se_tpg.tpg_lun_lock);
  1651. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  1652. struct se_lun *se_lun = tport->tpg->se_tpg.tpg_lun_list[i];
  1653. struct se_device *dev;
  1654. int type;
  1655. if (se_lun->lun_status == TRANSPORT_LUN_STATUS_FREE)
  1656. continue;
  1657. spin_unlock(&tport->tpg->se_tpg.tpg_lun_lock);
  1658. dev = se_lun->lun_se_dev;
  1659. type = dev->transport->get_device_type(dev);
  1660. /* logical_unit_number */
  1661. data[idx++] = 0x14000000 |
  1662. ((type << 16) & 0x1f0000) |
  1663. (se_lun->unpacked_lun & 0xffff);
  1664. spin_lock(&tport->tpg->se_tpg.tpg_lun_lock);
  1665. }
  1666. spin_unlock(&tport->tpg->se_tpg.tpg_lun_lock);
  1667. /* unit unique ID leaf */
  1668. data[idx++] = 2 << 16;
  1669. data[idx++] = tport->guid >> 32;
  1670. data[idx++] = tport->guid;
  1671. tport->unit_directory.length = idx;
  1672. tport->unit_directory.key = (CSR_DIRECTORY | CSR_UNIT) << 24;
  1673. tport->unit_directory.data = data;
  1674. ret = fw_core_add_descriptor(&tport->unit_directory);
  1675. if (ret < 0) {
  1676. kfree(tport->unit_directory.data);
  1677. tport->unit_directory.data = NULL;
  1678. }
  1679. return ret;
  1680. }
  1681. static ssize_t sbp_parse_wwn(const char *name, u64 *wwn, int strict)
  1682. {
  1683. const char *cp;
  1684. char c, nibble;
  1685. int pos = 0, err;
  1686. *wwn = 0;
  1687. for (cp = name; cp < &name[SBP_NAMELEN - 1]; cp++) {
  1688. c = *cp;
  1689. if (c == '\n' && cp[1] == '\0')
  1690. continue;
  1691. if (c == '\0') {
  1692. err = 2;
  1693. if (pos != 16)
  1694. goto fail;
  1695. return cp - name;
  1696. }
  1697. err = 3;
  1698. if (isdigit(c))
  1699. nibble = c - '0';
  1700. else if (isxdigit(c) && (islower(c) || !strict))
  1701. nibble = tolower(c) - 'a' + 10;
  1702. else
  1703. goto fail;
  1704. *wwn = (*wwn << 4) | nibble;
  1705. pos++;
  1706. }
  1707. err = 4;
  1708. fail:
  1709. printk(KERN_INFO "err %u len %zu pos %u\n",
  1710. err, cp - name, pos);
  1711. return -1;
  1712. }
  1713. static ssize_t sbp_format_wwn(char *buf, size_t len, u64 wwn)
  1714. {
  1715. return snprintf(buf, len, "%016llx", wwn);
  1716. }
  1717. static struct se_node_acl *sbp_make_nodeacl(
  1718. struct se_portal_group *se_tpg,
  1719. struct config_group *group,
  1720. const char *name)
  1721. {
  1722. struct se_node_acl *se_nacl, *se_nacl_new;
  1723. struct sbp_nacl *nacl;
  1724. u64 guid = 0;
  1725. u32 nexus_depth = 1;
  1726. if (sbp_parse_wwn(name, &guid, 1) < 0)
  1727. return ERR_PTR(-EINVAL);
  1728. se_nacl_new = sbp_alloc_fabric_acl(se_tpg);
  1729. if (!se_nacl_new)
  1730. return ERR_PTR(-ENOMEM);
  1731. /*
  1732. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1733. * when converting a NodeACL from demo mode -> explict
  1734. */
  1735. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1736. name, nexus_depth);
  1737. if (IS_ERR(se_nacl)) {
  1738. sbp_release_fabric_acl(se_tpg, se_nacl_new);
  1739. return se_nacl;
  1740. }
  1741. nacl = container_of(se_nacl, struct sbp_nacl, se_node_acl);
  1742. nacl->guid = guid;
  1743. sbp_format_wwn(nacl->iport_name, SBP_NAMELEN, guid);
  1744. return se_nacl;
  1745. }
  1746. static void sbp_drop_nodeacl(struct se_node_acl *se_acl)
  1747. {
  1748. struct sbp_nacl *nacl =
  1749. container_of(se_acl, struct sbp_nacl, se_node_acl);
  1750. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1751. kfree(nacl);
  1752. }
  1753. static int sbp_post_link_lun(
  1754. struct se_portal_group *se_tpg,
  1755. struct se_lun *se_lun)
  1756. {
  1757. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1758. return sbp_update_unit_directory(tpg->tport);
  1759. }
  1760. static void sbp_pre_unlink_lun(
  1761. struct se_portal_group *se_tpg,
  1762. struct se_lun *se_lun)
  1763. {
  1764. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1765. struct sbp_tport *tport = tpg->tport;
  1766. int ret;
  1767. if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0)
  1768. tport->enable = 0;
  1769. ret = sbp_update_unit_directory(tport);
  1770. if (ret < 0)
  1771. pr_err("unlink LUN: failed to update unit directory\n");
  1772. }
  1773. static struct se_portal_group *sbp_make_tpg(
  1774. struct se_wwn *wwn,
  1775. struct config_group *group,
  1776. const char *name)
  1777. {
  1778. struct sbp_tport *tport =
  1779. container_of(wwn, struct sbp_tport, tport_wwn);
  1780. struct sbp_tpg *tpg;
  1781. unsigned long tpgt;
  1782. int ret;
  1783. if (strstr(name, "tpgt_") != name)
  1784. return ERR_PTR(-EINVAL);
  1785. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1786. return ERR_PTR(-EINVAL);
  1787. if (tport->tpg) {
  1788. pr_err("Only one TPG per Unit is possible.\n");
  1789. return ERR_PTR(-EBUSY);
  1790. }
  1791. tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
  1792. if (!tpg) {
  1793. pr_err("Unable to allocate struct sbp_tpg\n");
  1794. return ERR_PTR(-ENOMEM);
  1795. }
  1796. tpg->tport = tport;
  1797. tpg->tport_tpgt = tpgt;
  1798. tport->tpg = tpg;
  1799. /* default attribute values */
  1800. tport->enable = 0;
  1801. tport->directory_id = -1;
  1802. tport->mgt_orb_timeout = 15;
  1803. tport->max_reconnect_timeout = 5;
  1804. tport->max_logins_per_lun = 1;
  1805. tport->mgt_agt = sbp_management_agent_register(tport);
  1806. if (IS_ERR(tport->mgt_agt)) {
  1807. ret = PTR_ERR(tport->mgt_agt);
  1808. kfree(tpg);
  1809. return ERR_PTR(ret);
  1810. }
  1811. ret = core_tpg_register(&sbp_fabric_configfs->tf_ops, wwn,
  1812. &tpg->se_tpg, (void *)tpg,
  1813. TRANSPORT_TPG_TYPE_NORMAL);
  1814. if (ret < 0) {
  1815. sbp_management_agent_unregister(tport->mgt_agt);
  1816. kfree(tpg);
  1817. return ERR_PTR(ret);
  1818. }
  1819. return &tpg->se_tpg;
  1820. }
  1821. static void sbp_drop_tpg(struct se_portal_group *se_tpg)
  1822. {
  1823. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1824. struct sbp_tport *tport = tpg->tport;
  1825. core_tpg_deregister(se_tpg);
  1826. sbp_management_agent_unregister(tport->mgt_agt);
  1827. tport->tpg = NULL;
  1828. kfree(tpg);
  1829. }
  1830. static struct se_wwn *sbp_make_tport(
  1831. struct target_fabric_configfs *tf,
  1832. struct config_group *group,
  1833. const char *name)
  1834. {
  1835. struct sbp_tport *tport;
  1836. u64 guid = 0;
  1837. if (sbp_parse_wwn(name, &guid, 1) < 0)
  1838. return ERR_PTR(-EINVAL);
  1839. tport = kzalloc(sizeof(*tport), GFP_KERNEL);
  1840. if (!tport) {
  1841. pr_err("Unable to allocate struct sbp_tport\n");
  1842. return ERR_PTR(-ENOMEM);
  1843. }
  1844. tport->guid = guid;
  1845. sbp_format_wwn(tport->tport_name, SBP_NAMELEN, guid);
  1846. return &tport->tport_wwn;
  1847. }
  1848. static void sbp_drop_tport(struct se_wwn *wwn)
  1849. {
  1850. struct sbp_tport *tport =
  1851. container_of(wwn, struct sbp_tport, tport_wwn);
  1852. kfree(tport);
  1853. }
  1854. static ssize_t sbp_wwn_show_attr_version(
  1855. struct target_fabric_configfs *tf,
  1856. char *page)
  1857. {
  1858. return sprintf(page, "FireWire SBP fabric module %s\n", SBP_VERSION);
  1859. }
  1860. TF_WWN_ATTR_RO(sbp, version);
  1861. static struct configfs_attribute *sbp_wwn_attrs[] = {
  1862. &sbp_wwn_version.attr,
  1863. NULL,
  1864. };
  1865. static ssize_t sbp_tpg_show_directory_id(
  1866. struct se_portal_group *se_tpg,
  1867. char *page)
  1868. {
  1869. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1870. struct sbp_tport *tport = tpg->tport;
  1871. if (tport->directory_id == -1)
  1872. return sprintf(page, "implicit\n");
  1873. else
  1874. return sprintf(page, "%06x\n", tport->directory_id);
  1875. }
  1876. static ssize_t sbp_tpg_store_directory_id(
  1877. struct se_portal_group *se_tpg,
  1878. const char *page,
  1879. size_t count)
  1880. {
  1881. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1882. struct sbp_tport *tport = tpg->tport;
  1883. unsigned long val;
  1884. if (tport->enable) {
  1885. pr_err("Cannot change the directory_id on an active target.\n");
  1886. return -EBUSY;
  1887. }
  1888. if (strstr(page, "implicit") == page) {
  1889. tport->directory_id = -1;
  1890. } else {
  1891. if (kstrtoul(page, 16, &val) < 0)
  1892. return -EINVAL;
  1893. if (val > 0xffffff)
  1894. return -EINVAL;
  1895. tport->directory_id = val;
  1896. }
  1897. return count;
  1898. }
  1899. static ssize_t sbp_tpg_show_enable(
  1900. struct se_portal_group *se_tpg,
  1901. char *page)
  1902. {
  1903. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1904. struct sbp_tport *tport = tpg->tport;
  1905. return sprintf(page, "%d\n", tport->enable);
  1906. }
  1907. static ssize_t sbp_tpg_store_enable(
  1908. struct se_portal_group *se_tpg,
  1909. const char *page,
  1910. size_t count)
  1911. {
  1912. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1913. struct sbp_tport *tport = tpg->tport;
  1914. unsigned long val;
  1915. int ret;
  1916. if (kstrtoul(page, 0, &val) < 0)
  1917. return -EINVAL;
  1918. if ((val != 0) && (val != 1))
  1919. return -EINVAL;
  1920. if (tport->enable == val)
  1921. return count;
  1922. if (val) {
  1923. if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0) {
  1924. pr_err("Cannot enable a target with no LUNs!\n");
  1925. return -EINVAL;
  1926. }
  1927. } else {
  1928. /* XXX: force-shutdown sessions instead? */
  1929. spin_lock_bh(&se_tpg->session_lock);
  1930. if (!list_empty(&se_tpg->tpg_sess_list)) {
  1931. spin_unlock_bh(&se_tpg->session_lock);
  1932. return -EBUSY;
  1933. }
  1934. spin_unlock_bh(&se_tpg->session_lock);
  1935. }
  1936. tport->enable = val;
  1937. ret = sbp_update_unit_directory(tport);
  1938. if (ret < 0) {
  1939. pr_err("Could not update Config ROM\n");
  1940. return ret;
  1941. }
  1942. return count;
  1943. }
  1944. TF_TPG_BASE_ATTR(sbp, directory_id, S_IRUGO | S_IWUSR);
  1945. TF_TPG_BASE_ATTR(sbp, enable, S_IRUGO | S_IWUSR);
  1946. static struct configfs_attribute *sbp_tpg_base_attrs[] = {
  1947. &sbp_tpg_directory_id.attr,
  1948. &sbp_tpg_enable.attr,
  1949. NULL,
  1950. };
  1951. static ssize_t sbp_tpg_attrib_show_mgt_orb_timeout(
  1952. struct se_portal_group *se_tpg,
  1953. char *page)
  1954. {
  1955. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1956. struct sbp_tport *tport = tpg->tport;
  1957. return sprintf(page, "%d\n", tport->mgt_orb_timeout);
  1958. }
  1959. static ssize_t sbp_tpg_attrib_store_mgt_orb_timeout(
  1960. struct se_portal_group *se_tpg,
  1961. const char *page,
  1962. size_t count)
  1963. {
  1964. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1965. struct sbp_tport *tport = tpg->tport;
  1966. unsigned long val;
  1967. int ret;
  1968. if (kstrtoul(page, 0, &val) < 0)
  1969. return -EINVAL;
  1970. if ((val < 1) || (val > 127))
  1971. return -EINVAL;
  1972. if (tport->mgt_orb_timeout == val)
  1973. return count;
  1974. tport->mgt_orb_timeout = val;
  1975. ret = sbp_update_unit_directory(tport);
  1976. if (ret < 0)
  1977. return ret;
  1978. return count;
  1979. }
  1980. static ssize_t sbp_tpg_attrib_show_max_reconnect_timeout(
  1981. struct se_portal_group *se_tpg,
  1982. char *page)
  1983. {
  1984. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1985. struct sbp_tport *tport = tpg->tport;
  1986. return sprintf(page, "%d\n", tport->max_reconnect_timeout);
  1987. }
  1988. static ssize_t sbp_tpg_attrib_store_max_reconnect_timeout(
  1989. struct se_portal_group *se_tpg,
  1990. const char *page,
  1991. size_t count)
  1992. {
  1993. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1994. struct sbp_tport *tport = tpg->tport;
  1995. unsigned long val;
  1996. int ret;
  1997. if (kstrtoul(page, 0, &val) < 0)
  1998. return -EINVAL;
  1999. if ((val < 1) || (val > 32767))
  2000. return -EINVAL;
  2001. if (tport->max_reconnect_timeout == val)
  2002. return count;
  2003. tport->max_reconnect_timeout = val;
  2004. ret = sbp_update_unit_directory(tport);
  2005. if (ret < 0)
  2006. return ret;
  2007. return count;
  2008. }
  2009. static ssize_t sbp_tpg_attrib_show_max_logins_per_lun(
  2010. struct se_portal_group *se_tpg,
  2011. char *page)
  2012. {
  2013. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  2014. struct sbp_tport *tport = tpg->tport;
  2015. return sprintf(page, "%d\n", tport->max_logins_per_lun);
  2016. }
  2017. static ssize_t sbp_tpg_attrib_store_max_logins_per_lun(
  2018. struct se_portal_group *se_tpg,
  2019. const char *page,
  2020. size_t count)
  2021. {
  2022. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  2023. struct sbp_tport *tport = tpg->tport;
  2024. unsigned long val;
  2025. if (kstrtoul(page, 0, &val) < 0)
  2026. return -EINVAL;
  2027. if ((val < 1) || (val > 127))
  2028. return -EINVAL;
  2029. /* XXX: also check against current count? */
  2030. tport->max_logins_per_lun = val;
  2031. return count;
  2032. }
  2033. TF_TPG_ATTRIB_ATTR(sbp, mgt_orb_timeout, S_IRUGO | S_IWUSR);
  2034. TF_TPG_ATTRIB_ATTR(sbp, max_reconnect_timeout, S_IRUGO | S_IWUSR);
  2035. TF_TPG_ATTRIB_ATTR(sbp, max_logins_per_lun, S_IRUGO | S_IWUSR);
  2036. static struct configfs_attribute *sbp_tpg_attrib_attrs[] = {
  2037. &sbp_tpg_attrib_mgt_orb_timeout.attr,
  2038. &sbp_tpg_attrib_max_reconnect_timeout.attr,
  2039. &sbp_tpg_attrib_max_logins_per_lun.attr,
  2040. NULL,
  2041. };
  2042. static struct target_core_fabric_ops sbp_ops = {
  2043. .get_fabric_name = sbp_get_fabric_name,
  2044. .get_fabric_proto_ident = sbp_get_fabric_proto_ident,
  2045. .tpg_get_wwn = sbp_get_fabric_wwn,
  2046. .tpg_get_tag = sbp_get_tag,
  2047. .tpg_get_default_depth = sbp_get_default_depth,
  2048. .tpg_get_pr_transport_id = sbp_get_pr_transport_id,
  2049. .tpg_get_pr_transport_id_len = sbp_get_pr_transport_id_len,
  2050. .tpg_parse_pr_out_transport_id = sbp_parse_pr_out_transport_id,
  2051. .tpg_check_demo_mode = sbp_check_true,
  2052. .tpg_check_demo_mode_cache = sbp_check_true,
  2053. .tpg_check_demo_mode_write_protect = sbp_check_false,
  2054. .tpg_check_prod_mode_write_protect = sbp_check_false,
  2055. .tpg_alloc_fabric_acl = sbp_alloc_fabric_acl,
  2056. .tpg_release_fabric_acl = sbp_release_fabric_acl,
  2057. .tpg_get_inst_index = sbp_tpg_get_inst_index,
  2058. .release_cmd = sbp_release_cmd,
  2059. .shutdown_session = sbp_shutdown_session,
  2060. .close_session = sbp_close_session,
  2061. .sess_get_index = sbp_sess_get_index,
  2062. .write_pending = sbp_write_pending,
  2063. .write_pending_status = sbp_write_pending_status,
  2064. .set_default_node_attributes = sbp_set_default_node_attrs,
  2065. .get_task_tag = sbp_get_task_tag,
  2066. .get_cmd_state = sbp_get_cmd_state,
  2067. .queue_data_in = sbp_queue_data_in,
  2068. .queue_status = sbp_queue_status,
  2069. .queue_tm_rsp = sbp_queue_tm_rsp,
  2070. .get_fabric_sense_len = sbp_get_fabric_sense_len,
  2071. .set_fabric_sense_len = sbp_set_fabric_sense_len,
  2072. .check_stop_free = sbp_check_stop_free,
  2073. .fabric_make_wwn = sbp_make_tport,
  2074. .fabric_drop_wwn = sbp_drop_tport,
  2075. .fabric_make_tpg = sbp_make_tpg,
  2076. .fabric_drop_tpg = sbp_drop_tpg,
  2077. .fabric_post_link = sbp_post_link_lun,
  2078. .fabric_pre_unlink = sbp_pre_unlink_lun,
  2079. .fabric_make_np = NULL,
  2080. .fabric_drop_np = NULL,
  2081. .fabric_make_nodeacl = sbp_make_nodeacl,
  2082. .fabric_drop_nodeacl = sbp_drop_nodeacl,
  2083. };
  2084. static int sbp_register_configfs(void)
  2085. {
  2086. struct target_fabric_configfs *fabric;
  2087. int ret;
  2088. fabric = target_fabric_configfs_init(THIS_MODULE, "sbp");
  2089. if (!fabric) {
  2090. pr_err("target_fabric_configfs_init() failed\n");
  2091. return -ENOMEM;
  2092. }
  2093. fabric->tf_ops = sbp_ops;
  2094. /*
  2095. * Setup default attribute lists for various fabric->tf_cit_tmpl
  2096. */
  2097. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = sbp_wwn_attrs;
  2098. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = sbp_tpg_base_attrs;
  2099. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = sbp_tpg_attrib_attrs;
  2100. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  2101. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  2102. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  2103. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  2104. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  2105. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  2106. ret = target_fabric_configfs_register(fabric);
  2107. if (ret < 0) {
  2108. pr_err("target_fabric_configfs_register() failed for SBP\n");
  2109. return ret;
  2110. }
  2111. sbp_fabric_configfs = fabric;
  2112. return 0;
  2113. };
  2114. static void sbp_deregister_configfs(void)
  2115. {
  2116. if (!sbp_fabric_configfs)
  2117. return;
  2118. target_fabric_configfs_deregister(sbp_fabric_configfs);
  2119. sbp_fabric_configfs = NULL;
  2120. };
  2121. static int __init sbp_init(void)
  2122. {
  2123. int ret;
  2124. ret = sbp_register_configfs();
  2125. if (ret < 0)
  2126. return ret;
  2127. return 0;
  2128. };
  2129. static void sbp_exit(void)
  2130. {
  2131. sbp_deregister_configfs();
  2132. };
  2133. MODULE_DESCRIPTION("FireWire SBP fabric driver");
  2134. MODULE_LICENSE("GPL");
  2135. module_init(sbp_init);
  2136. module_exit(sbp_exit);