sbp_target.c 60 KB

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