sbp_target.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609
  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_move_tail(&login->link, &login_list);
  533. }
  534. spin_unlock_bh(&sess->lock);
  535. list_for_each_entry_safe(login, temp, &login_list, link) {
  536. list_del(&login->link);
  537. sbp_login_release(login, false);
  538. }
  539. sbp_session_release(sess, false);
  540. }
  541. static void session_maintenance_work(struct work_struct *work)
  542. {
  543. struct sbp_session *sess = container_of(work, struct sbp_session,
  544. maint_work.work);
  545. /* could be called while tearing down the session */
  546. spin_lock_bh(&sess->lock);
  547. if (list_empty(&sess->login_list)) {
  548. spin_unlock_bh(&sess->lock);
  549. return;
  550. }
  551. spin_unlock_bh(&sess->lock);
  552. if (sess->node_id != -1) {
  553. /* check for bus reset and make node_id invalid */
  554. session_check_for_reset(sess);
  555. schedule_delayed_work(&sess->maint_work,
  556. SESSION_MAINTENANCE_INTERVAL);
  557. } else if (!time_after64(get_jiffies_64(), sess->reconnect_expires)) {
  558. /* still waiting for reconnect */
  559. schedule_delayed_work(&sess->maint_work,
  560. SESSION_MAINTENANCE_INTERVAL);
  561. } else {
  562. /* reconnect timeout has expired */
  563. session_reconnect_expired(sess);
  564. }
  565. }
  566. static int tgt_agent_rw_agent_state(struct fw_card *card, int tcode, void *data,
  567. struct sbp_target_agent *agent)
  568. {
  569. int state;
  570. switch (tcode) {
  571. case TCODE_READ_QUADLET_REQUEST:
  572. pr_debug("tgt_agent AGENT_STATE READ\n");
  573. spin_lock_bh(&agent->lock);
  574. state = agent->state;
  575. spin_unlock_bh(&agent->lock);
  576. *(__be32 *)data = cpu_to_be32(state);
  577. return RCODE_COMPLETE;
  578. case TCODE_WRITE_QUADLET_REQUEST:
  579. /* ignored */
  580. return RCODE_COMPLETE;
  581. default:
  582. return RCODE_TYPE_ERROR;
  583. }
  584. }
  585. static int tgt_agent_rw_agent_reset(struct fw_card *card, int tcode, void *data,
  586. struct sbp_target_agent *agent)
  587. {
  588. switch (tcode) {
  589. case TCODE_WRITE_QUADLET_REQUEST:
  590. pr_debug("tgt_agent AGENT_RESET\n");
  591. spin_lock_bh(&agent->lock);
  592. agent->state = AGENT_STATE_RESET;
  593. spin_unlock_bh(&agent->lock);
  594. return RCODE_COMPLETE;
  595. default:
  596. return RCODE_TYPE_ERROR;
  597. }
  598. }
  599. static int tgt_agent_rw_orb_pointer(struct fw_card *card, int tcode, void *data,
  600. struct sbp_target_agent *agent)
  601. {
  602. struct sbp2_pointer *ptr = data;
  603. switch (tcode) {
  604. case TCODE_WRITE_BLOCK_REQUEST:
  605. spin_lock_bh(&agent->lock);
  606. if (agent->state != AGENT_STATE_SUSPENDED &&
  607. agent->state != AGENT_STATE_RESET) {
  608. spin_unlock_bh(&agent->lock);
  609. pr_notice("Ignoring ORB_POINTER write while active.\n");
  610. return RCODE_CONFLICT_ERROR;
  611. }
  612. agent->state = AGENT_STATE_ACTIVE;
  613. spin_unlock_bh(&agent->lock);
  614. agent->orb_pointer = sbp2_pointer_to_addr(ptr);
  615. agent->doorbell = false;
  616. pr_debug("tgt_agent ORB_POINTER write: 0x%llx\n",
  617. agent->orb_pointer);
  618. queue_work(system_unbound_wq, &agent->work);
  619. return RCODE_COMPLETE;
  620. case TCODE_READ_BLOCK_REQUEST:
  621. pr_debug("tgt_agent ORB_POINTER READ\n");
  622. spin_lock_bh(&agent->lock);
  623. addr_to_sbp2_pointer(agent->orb_pointer, ptr);
  624. spin_unlock_bh(&agent->lock);
  625. return RCODE_COMPLETE;
  626. default:
  627. return RCODE_TYPE_ERROR;
  628. }
  629. }
  630. static int tgt_agent_rw_doorbell(struct fw_card *card, int tcode, void *data,
  631. struct sbp_target_agent *agent)
  632. {
  633. switch (tcode) {
  634. case TCODE_WRITE_QUADLET_REQUEST:
  635. spin_lock_bh(&agent->lock);
  636. if (agent->state != AGENT_STATE_SUSPENDED) {
  637. spin_unlock_bh(&agent->lock);
  638. pr_debug("Ignoring DOORBELL while active.\n");
  639. return RCODE_CONFLICT_ERROR;
  640. }
  641. agent->state = AGENT_STATE_ACTIVE;
  642. spin_unlock_bh(&agent->lock);
  643. agent->doorbell = true;
  644. pr_debug("tgt_agent DOORBELL\n");
  645. queue_work(system_unbound_wq, &agent->work);
  646. return RCODE_COMPLETE;
  647. case TCODE_READ_QUADLET_REQUEST:
  648. return RCODE_COMPLETE;
  649. default:
  650. return RCODE_TYPE_ERROR;
  651. }
  652. }
  653. static int tgt_agent_rw_unsolicited_status_enable(struct fw_card *card,
  654. int tcode, void *data, struct sbp_target_agent *agent)
  655. {
  656. switch (tcode) {
  657. case TCODE_WRITE_QUADLET_REQUEST:
  658. pr_debug("tgt_agent UNSOLICITED_STATUS_ENABLE\n");
  659. /* ignored as we don't send unsolicited status */
  660. return RCODE_COMPLETE;
  661. case TCODE_READ_QUADLET_REQUEST:
  662. return RCODE_COMPLETE;
  663. default:
  664. return RCODE_TYPE_ERROR;
  665. }
  666. }
  667. static void tgt_agent_rw(struct fw_card *card, struct fw_request *request,
  668. int tcode, int destination, int source, int generation,
  669. unsigned long long offset, void *data, size_t length,
  670. void *callback_data)
  671. {
  672. struct sbp_target_agent *agent = callback_data;
  673. struct sbp_session *sess = agent->login->sess;
  674. int sess_gen, sess_node, rcode;
  675. spin_lock_bh(&sess->lock);
  676. sess_gen = sess->generation;
  677. sess_node = sess->node_id;
  678. spin_unlock_bh(&sess->lock);
  679. if (generation != sess_gen) {
  680. pr_notice("ignoring request with wrong generation\n");
  681. rcode = RCODE_TYPE_ERROR;
  682. goto out;
  683. }
  684. if (source != sess_node) {
  685. pr_notice("ignoring request from foreign node (%x != %x)\n",
  686. source, sess_node);
  687. rcode = RCODE_TYPE_ERROR;
  688. goto out;
  689. }
  690. /* turn offset into the offset from the start of the block */
  691. offset -= agent->handler.offset;
  692. if (offset == 0x00 && length == 4) {
  693. /* AGENT_STATE */
  694. rcode = tgt_agent_rw_agent_state(card, tcode, data, agent);
  695. } else if (offset == 0x04 && length == 4) {
  696. /* AGENT_RESET */
  697. rcode = tgt_agent_rw_agent_reset(card, tcode, data, agent);
  698. } else if (offset == 0x08 && length == 8) {
  699. /* ORB_POINTER */
  700. rcode = tgt_agent_rw_orb_pointer(card, tcode, data, agent);
  701. } else if (offset == 0x10 && length == 4) {
  702. /* DOORBELL */
  703. rcode = tgt_agent_rw_doorbell(card, tcode, data, agent);
  704. } else if (offset == 0x14 && length == 4) {
  705. /* UNSOLICITED_STATUS_ENABLE */
  706. rcode = tgt_agent_rw_unsolicited_status_enable(card, tcode,
  707. data, agent);
  708. } else {
  709. rcode = RCODE_ADDRESS_ERROR;
  710. }
  711. out:
  712. fw_send_response(card, request, rcode);
  713. }
  714. static void sbp_handle_command(struct sbp_target_request *);
  715. static int sbp_send_status(struct sbp_target_request *);
  716. static void sbp_free_request(struct sbp_target_request *);
  717. static void tgt_agent_process_work(struct work_struct *work)
  718. {
  719. struct sbp_target_request *req =
  720. container_of(work, struct sbp_target_request, work);
  721. pr_debug("tgt_orb ptr:0x%llx next_ORB:0x%llx data_descriptor:0x%llx misc:0x%x\n",
  722. req->orb_pointer,
  723. sbp2_pointer_to_addr(&req->orb.next_orb),
  724. sbp2_pointer_to_addr(&req->orb.data_descriptor),
  725. be32_to_cpu(req->orb.misc));
  726. if (req->orb_pointer >> 32)
  727. pr_debug("ORB with high bits set\n");
  728. switch (ORB_REQUEST_FORMAT(be32_to_cpu(req->orb.misc))) {
  729. case 0:/* Format specified by this standard */
  730. sbp_handle_command(req);
  731. return;
  732. case 1: /* Reserved for future standardization */
  733. case 2: /* Vendor-dependent */
  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_REQ_TYPE_NOTSUPP));
  741. sbp_send_status(req);
  742. sbp_free_request(req);
  743. return;
  744. case 3: /* Dummy ORB */
  745. req->status.status |= cpu_to_be32(
  746. STATUS_BLOCK_RESP(
  747. STATUS_RESP_REQUEST_COMPLETE) |
  748. STATUS_BLOCK_DEAD(0) |
  749. STATUS_BLOCK_LEN(1) |
  750. STATUS_BLOCK_SBP_STATUS(
  751. SBP_STATUS_DUMMY_ORB_COMPLETE));
  752. sbp_send_status(req);
  753. sbp_free_request(req);
  754. return;
  755. default:
  756. BUG();
  757. }
  758. }
  759. /* used to double-check we haven't been issued an AGENT_RESET */
  760. static inline bool tgt_agent_check_active(struct sbp_target_agent *agent)
  761. {
  762. bool active;
  763. spin_lock_bh(&agent->lock);
  764. active = (agent->state == AGENT_STATE_ACTIVE);
  765. spin_unlock_bh(&agent->lock);
  766. return active;
  767. }
  768. static void tgt_agent_fetch_work(struct work_struct *work)
  769. {
  770. struct sbp_target_agent *agent =
  771. container_of(work, struct sbp_target_agent, work);
  772. struct sbp_session *sess = agent->login->sess;
  773. struct sbp_target_request *req;
  774. int ret;
  775. bool doorbell = agent->doorbell;
  776. u64 next_orb = agent->orb_pointer;
  777. while (next_orb && tgt_agent_check_active(agent)) {
  778. req = kzalloc(sizeof(*req), GFP_KERNEL);
  779. if (!req) {
  780. spin_lock_bh(&agent->lock);
  781. agent->state = AGENT_STATE_DEAD;
  782. spin_unlock_bh(&agent->lock);
  783. return;
  784. }
  785. req->login = agent->login;
  786. req->orb_pointer = next_orb;
  787. req->status.status = cpu_to_be32(STATUS_BLOCK_ORB_OFFSET_HIGH(
  788. req->orb_pointer >> 32));
  789. req->status.orb_low = cpu_to_be32(
  790. req->orb_pointer & 0xfffffffc);
  791. /* read in the ORB */
  792. ret = sbp_run_transaction(sess->card, TCODE_READ_BLOCK_REQUEST,
  793. sess->node_id, sess->generation, sess->speed,
  794. req->orb_pointer, &req->orb, sizeof(req->orb));
  795. if (ret != RCODE_COMPLETE) {
  796. pr_debug("tgt_orb fetch failed: %x\n", ret);
  797. req->status.status |= cpu_to_be32(
  798. STATUS_BLOCK_SRC(
  799. STATUS_SRC_ORB_FINISHED) |
  800. STATUS_BLOCK_RESP(
  801. STATUS_RESP_TRANSPORT_FAILURE) |
  802. STATUS_BLOCK_DEAD(1) |
  803. STATUS_BLOCK_LEN(1) |
  804. STATUS_BLOCK_SBP_STATUS(
  805. SBP_STATUS_UNSPECIFIED_ERROR));
  806. spin_lock_bh(&agent->lock);
  807. agent->state = AGENT_STATE_DEAD;
  808. spin_unlock_bh(&agent->lock);
  809. sbp_send_status(req);
  810. sbp_free_request(req);
  811. return;
  812. }
  813. /* check the next_ORB field */
  814. if (be32_to_cpu(req->orb.next_orb.high) & 0x80000000) {
  815. next_orb = 0;
  816. req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC(
  817. STATUS_SRC_ORB_FINISHED));
  818. } else {
  819. next_orb = sbp2_pointer_to_addr(&req->orb.next_orb);
  820. req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC(
  821. STATUS_SRC_ORB_CONTINUING));
  822. }
  823. if (tgt_agent_check_active(agent) && !doorbell) {
  824. INIT_WORK(&req->work, tgt_agent_process_work);
  825. queue_work(system_unbound_wq, &req->work);
  826. } else {
  827. /* don't process this request, just check next_ORB */
  828. sbp_free_request(req);
  829. }
  830. spin_lock_bh(&agent->lock);
  831. doorbell = agent->doorbell = false;
  832. /* check if we should carry on processing */
  833. if (next_orb)
  834. agent->orb_pointer = next_orb;
  835. else
  836. agent->state = AGENT_STATE_SUSPENDED;
  837. spin_unlock_bh(&agent->lock);
  838. };
  839. }
  840. static struct sbp_target_agent *sbp_target_agent_register(
  841. struct sbp_login_descriptor *login)
  842. {
  843. struct sbp_target_agent *agent;
  844. int ret;
  845. agent = kmalloc(sizeof(*agent), GFP_KERNEL);
  846. if (!agent)
  847. return ERR_PTR(-ENOMEM);
  848. spin_lock_init(&agent->lock);
  849. agent->handler.length = 0x20;
  850. agent->handler.address_callback = tgt_agent_rw;
  851. agent->handler.callback_data = agent;
  852. agent->login = login;
  853. agent->state = AGENT_STATE_RESET;
  854. INIT_WORK(&agent->work, tgt_agent_fetch_work);
  855. agent->orb_pointer = 0;
  856. agent->doorbell = false;
  857. ret = fw_core_add_address_handler(&agent->handler,
  858. &sbp_register_region);
  859. if (ret < 0) {
  860. kfree(agent);
  861. return ERR_PTR(ret);
  862. }
  863. return agent;
  864. }
  865. static void sbp_target_agent_unregister(struct sbp_target_agent *agent)
  866. {
  867. fw_core_remove_address_handler(&agent->handler);
  868. cancel_work_sync(&agent->work);
  869. kfree(agent);
  870. }
  871. /*
  872. * Simple wrapper around fw_run_transaction that retries the transaction several
  873. * times in case of failure, with an exponential backoff.
  874. */
  875. static int sbp_run_transaction(struct fw_card *card, int tcode, int destination_id,
  876. int generation, int speed, unsigned long long offset,
  877. void *payload, size_t length)
  878. {
  879. int attempt, ret, delay;
  880. for (attempt = 1; attempt <= 5; attempt++) {
  881. ret = fw_run_transaction(card, tcode, destination_id,
  882. generation, speed, offset, payload, length);
  883. switch (ret) {
  884. case RCODE_COMPLETE:
  885. case RCODE_TYPE_ERROR:
  886. case RCODE_ADDRESS_ERROR:
  887. case RCODE_GENERATION:
  888. return ret;
  889. default:
  890. delay = 5 * attempt * attempt;
  891. usleep_range(delay, delay * 2);
  892. }
  893. }
  894. return ret;
  895. }
  896. /*
  897. * Wrapper around sbp_run_transaction that gets the card, destination,
  898. * generation and speed out of the request's session.
  899. */
  900. static int sbp_run_request_transaction(struct sbp_target_request *req,
  901. int tcode, unsigned long long offset, void *payload,
  902. size_t length)
  903. {
  904. struct sbp_login_descriptor *login = req->login;
  905. struct sbp_session *sess = login->sess;
  906. struct fw_card *card;
  907. int node_id, generation, speed, ret;
  908. spin_lock_bh(&sess->lock);
  909. card = fw_card_get(sess->card);
  910. node_id = sess->node_id;
  911. generation = sess->generation;
  912. speed = sess->speed;
  913. spin_unlock_bh(&sess->lock);
  914. ret = sbp_run_transaction(card, tcode, node_id, generation, speed,
  915. offset, payload, length);
  916. fw_card_put(card);
  917. return ret;
  918. }
  919. static int sbp_fetch_command(struct sbp_target_request *req)
  920. {
  921. int ret, cmd_len, copy_len;
  922. cmd_len = scsi_command_size(req->orb.command_block);
  923. req->cmd_buf = kmalloc(cmd_len, GFP_KERNEL);
  924. if (!req->cmd_buf)
  925. return -ENOMEM;
  926. memcpy(req->cmd_buf, req->orb.command_block,
  927. min_t(int, cmd_len, sizeof(req->orb.command_block)));
  928. if (cmd_len > sizeof(req->orb.command_block)) {
  929. pr_debug("sbp_fetch_command: filling in long command\n");
  930. copy_len = cmd_len - sizeof(req->orb.command_block);
  931. ret = sbp_run_request_transaction(req,
  932. TCODE_READ_BLOCK_REQUEST,
  933. req->orb_pointer + sizeof(req->orb),
  934. req->cmd_buf + sizeof(req->orb.command_block),
  935. copy_len);
  936. if (ret != RCODE_COMPLETE)
  937. return -EIO;
  938. }
  939. return 0;
  940. }
  941. static int sbp_fetch_page_table(struct sbp_target_request *req)
  942. {
  943. int pg_tbl_sz, ret;
  944. struct sbp_page_table_entry *pg_tbl;
  945. if (!CMDBLK_ORB_PG_TBL_PRESENT(be32_to_cpu(req->orb.misc)))
  946. return 0;
  947. pg_tbl_sz = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc)) *
  948. sizeof(struct sbp_page_table_entry);
  949. pg_tbl = kmalloc(pg_tbl_sz, GFP_KERNEL);
  950. if (!pg_tbl)
  951. return -ENOMEM;
  952. ret = sbp_run_request_transaction(req, TCODE_READ_BLOCK_REQUEST,
  953. sbp2_pointer_to_addr(&req->orb.data_descriptor),
  954. pg_tbl, pg_tbl_sz);
  955. if (ret != RCODE_COMPLETE) {
  956. kfree(pg_tbl);
  957. return -EIO;
  958. }
  959. req->pg_tbl = pg_tbl;
  960. return 0;
  961. }
  962. static void sbp_calc_data_length_direction(struct sbp_target_request *req,
  963. u32 *data_len, enum dma_data_direction *data_dir)
  964. {
  965. int data_size, direction, idx;
  966. data_size = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc));
  967. direction = CMDBLK_ORB_DIRECTION(be32_to_cpu(req->orb.misc));
  968. if (!data_size) {
  969. *data_len = 0;
  970. *data_dir = DMA_NONE;
  971. return;
  972. }
  973. *data_dir = direction ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  974. if (req->pg_tbl) {
  975. *data_len = 0;
  976. for (idx = 0; idx < data_size; idx++) {
  977. *data_len += be16_to_cpu(
  978. req->pg_tbl[idx].segment_length);
  979. }
  980. } else {
  981. *data_len = data_size;
  982. }
  983. }
  984. static void sbp_handle_command(struct sbp_target_request *req)
  985. {
  986. struct sbp_login_descriptor *login = req->login;
  987. struct sbp_session *sess = login->sess;
  988. int ret, unpacked_lun;
  989. u32 data_length;
  990. enum dma_data_direction data_dir;
  991. ret = sbp_fetch_command(req);
  992. if (ret) {
  993. pr_debug("sbp_handle_command: fetch command failed: %d\n", ret);
  994. goto err;
  995. }
  996. ret = sbp_fetch_page_table(req);
  997. if (ret) {
  998. pr_debug("sbp_handle_command: fetch page table failed: %d\n",
  999. ret);
  1000. goto err;
  1001. }
  1002. unpacked_lun = req->login->lun->unpacked_lun;
  1003. sbp_calc_data_length_direction(req, &data_length, &data_dir);
  1004. pr_debug("sbp_handle_command ORB:0x%llx unpacked_lun:%d data_len:%d data_dir:%d\n",
  1005. req->orb_pointer, unpacked_lun, data_length, data_dir);
  1006. if (target_submit_cmd(&req->se_cmd, sess->se_sess, req->cmd_buf,
  1007. req->sense_buf, unpacked_lun, data_length,
  1008. MSG_SIMPLE_TAG, data_dir, 0))
  1009. goto err;
  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 allocate 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 int sbp_check_stop_free(struct se_cmd *se_cmd)
  1501. {
  1502. struct sbp_target_request *req = container_of(se_cmd,
  1503. struct sbp_target_request, se_cmd);
  1504. transport_generic_free_cmd(&req->se_cmd, 0);
  1505. return 1;
  1506. }
  1507. /*
  1508. * Handlers for Serial Bus Protocol 2/3 (SBP-2 / SBP-3)
  1509. */
  1510. static u8 sbp_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  1511. {
  1512. /*
  1513. * Return a IEEE 1394 SCSI Protocol identifier for loopback operations
  1514. * This is defined in section 7.5.1 Table 362 in spc4r17
  1515. */
  1516. return SCSI_PROTOCOL_SBP;
  1517. }
  1518. static u32 sbp_get_pr_transport_id(
  1519. struct se_portal_group *se_tpg,
  1520. struct se_node_acl *se_nacl,
  1521. struct t10_pr_registration *pr_reg,
  1522. int *format_code,
  1523. unsigned char *buf)
  1524. {
  1525. int ret;
  1526. /*
  1527. * Set PROTOCOL IDENTIFIER to 3h for SBP
  1528. */
  1529. buf[0] = SCSI_PROTOCOL_SBP;
  1530. /*
  1531. * From spc4r17, 7.5.4.4 TransportID for initiator ports using SCSI
  1532. * over IEEE 1394
  1533. */
  1534. ret = hex2bin(&buf[8], se_nacl->initiatorname, 8);
  1535. if (ret < 0)
  1536. pr_debug("sbp transport_id: invalid hex string\n");
  1537. /*
  1538. * The IEEE 1394 Transport ID is a hardcoded 24-byte length
  1539. */
  1540. return 24;
  1541. }
  1542. static u32 sbp_get_pr_transport_id_len(
  1543. struct se_portal_group *se_tpg,
  1544. struct se_node_acl *se_nacl,
  1545. struct t10_pr_registration *pr_reg,
  1546. int *format_code)
  1547. {
  1548. *format_code = 0;
  1549. /*
  1550. * From spc4r17, 7.5.4.4 TransportID for initiator ports using SCSI
  1551. * over IEEE 1394
  1552. *
  1553. * The SBP Transport ID is a hardcoded 24-byte length
  1554. */
  1555. return 24;
  1556. }
  1557. /*
  1558. * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
  1559. * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
  1560. */
  1561. static char *sbp_parse_pr_out_transport_id(
  1562. struct se_portal_group *se_tpg,
  1563. const char *buf,
  1564. u32 *out_tid_len,
  1565. char **port_nexus_ptr)
  1566. {
  1567. /*
  1568. * Assume the FORMAT CODE 00b from spc4r17, 7.5.4.4 TransportID
  1569. * for initiator ports using SCSI over SBP Serial SCSI Protocol
  1570. *
  1571. * The TransportID for a IEEE 1394 Initiator Port is of fixed size of
  1572. * 24 bytes, and IEEE 1394 does not contain a I_T nexus identifier,
  1573. * so we return the **port_nexus_ptr set to NULL.
  1574. */
  1575. *port_nexus_ptr = NULL;
  1576. *out_tid_len = 24;
  1577. return (char *)&buf[8];
  1578. }
  1579. static int sbp_count_se_tpg_luns(struct se_portal_group *tpg)
  1580. {
  1581. int i, count = 0;
  1582. spin_lock(&tpg->tpg_lun_lock);
  1583. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  1584. struct se_lun *se_lun = tpg->tpg_lun_list[i];
  1585. if (se_lun->lun_status == TRANSPORT_LUN_STATUS_FREE)
  1586. continue;
  1587. count++;
  1588. }
  1589. spin_unlock(&tpg->tpg_lun_lock);
  1590. return count;
  1591. }
  1592. static int sbp_update_unit_directory(struct sbp_tport *tport)
  1593. {
  1594. int num_luns, num_entries, idx = 0, mgt_agt_addr, ret, i;
  1595. u32 *data;
  1596. if (tport->unit_directory.data) {
  1597. fw_core_remove_descriptor(&tport->unit_directory);
  1598. kfree(tport->unit_directory.data);
  1599. tport->unit_directory.data = NULL;
  1600. }
  1601. if (!tport->enable || !tport->tpg)
  1602. return 0;
  1603. num_luns = sbp_count_se_tpg_luns(&tport->tpg->se_tpg);
  1604. /*
  1605. * Number of entries in the final unit directory:
  1606. * - all of those in the template
  1607. * - management_agent
  1608. * - unit_characteristics
  1609. * - reconnect_timeout
  1610. * - unit unique ID
  1611. * - one for each LUN
  1612. *
  1613. * MUST NOT include leaf or sub-directory entries
  1614. */
  1615. num_entries = ARRAY_SIZE(sbp_unit_directory_template) + 4 + num_luns;
  1616. if (tport->directory_id != -1)
  1617. num_entries++;
  1618. /* allocate num_entries + 4 for the header and unique ID leaf */
  1619. data = kcalloc((num_entries + 4), sizeof(u32), GFP_KERNEL);
  1620. if (!data)
  1621. return -ENOMEM;
  1622. /* directory_length */
  1623. data[idx++] = num_entries << 16;
  1624. /* directory_id */
  1625. if (tport->directory_id != -1)
  1626. data[idx++] = (CSR_DIRECTORY_ID << 24) | tport->directory_id;
  1627. /* unit directory template */
  1628. memcpy(&data[idx], sbp_unit_directory_template,
  1629. sizeof(sbp_unit_directory_template));
  1630. idx += ARRAY_SIZE(sbp_unit_directory_template);
  1631. /* management_agent */
  1632. mgt_agt_addr = (tport->mgt_agt->handler.offset - CSR_REGISTER_BASE) / 4;
  1633. data[idx++] = 0x54000000 | (mgt_agt_addr & 0x00ffffff);
  1634. /* unit_characteristics */
  1635. data[idx++] = 0x3a000000 |
  1636. (((tport->mgt_orb_timeout * 2) << 8) & 0xff00) |
  1637. SBP_ORB_FETCH_SIZE;
  1638. /* reconnect_timeout */
  1639. data[idx++] = 0x3d000000 | (tport->max_reconnect_timeout & 0xffff);
  1640. /* unit unique ID (leaf is just after LUNs) */
  1641. data[idx++] = 0x8d000000 | (num_luns + 1);
  1642. spin_lock(&tport->tpg->se_tpg.tpg_lun_lock);
  1643. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  1644. struct se_lun *se_lun = tport->tpg->se_tpg.tpg_lun_list[i];
  1645. struct se_device *dev;
  1646. int type;
  1647. if (se_lun->lun_status == TRANSPORT_LUN_STATUS_FREE)
  1648. continue;
  1649. spin_unlock(&tport->tpg->se_tpg.tpg_lun_lock);
  1650. dev = se_lun->lun_se_dev;
  1651. type = dev->transport->get_device_type(dev);
  1652. /* logical_unit_number */
  1653. data[idx++] = 0x14000000 |
  1654. ((type << 16) & 0x1f0000) |
  1655. (se_lun->unpacked_lun & 0xffff);
  1656. spin_lock(&tport->tpg->se_tpg.tpg_lun_lock);
  1657. }
  1658. spin_unlock(&tport->tpg->se_tpg.tpg_lun_lock);
  1659. /* unit unique ID leaf */
  1660. data[idx++] = 2 << 16;
  1661. data[idx++] = tport->guid >> 32;
  1662. data[idx++] = tport->guid;
  1663. tport->unit_directory.length = idx;
  1664. tport->unit_directory.key = (CSR_DIRECTORY | CSR_UNIT) << 24;
  1665. tport->unit_directory.data = data;
  1666. ret = fw_core_add_descriptor(&tport->unit_directory);
  1667. if (ret < 0) {
  1668. kfree(tport->unit_directory.data);
  1669. tport->unit_directory.data = NULL;
  1670. }
  1671. return ret;
  1672. }
  1673. static ssize_t sbp_parse_wwn(const char *name, u64 *wwn)
  1674. {
  1675. const char *cp;
  1676. char c, nibble;
  1677. int pos = 0, err;
  1678. *wwn = 0;
  1679. for (cp = name; cp < &name[SBP_NAMELEN - 1]; cp++) {
  1680. c = *cp;
  1681. if (c == '\n' && cp[1] == '\0')
  1682. continue;
  1683. if (c == '\0') {
  1684. err = 2;
  1685. if (pos != 16)
  1686. goto fail;
  1687. return cp - name;
  1688. }
  1689. err = 3;
  1690. if (isdigit(c))
  1691. nibble = c - '0';
  1692. else if (isxdigit(c))
  1693. nibble = tolower(c) - 'a' + 10;
  1694. else
  1695. goto fail;
  1696. *wwn = (*wwn << 4) | nibble;
  1697. pos++;
  1698. }
  1699. err = 4;
  1700. fail:
  1701. printk(KERN_INFO "err %u len %zu pos %u\n",
  1702. err, cp - name, pos);
  1703. return -1;
  1704. }
  1705. static ssize_t sbp_format_wwn(char *buf, size_t len, u64 wwn)
  1706. {
  1707. return snprintf(buf, len, "%016llx", wwn);
  1708. }
  1709. static struct se_node_acl *sbp_make_nodeacl(
  1710. struct se_portal_group *se_tpg,
  1711. struct config_group *group,
  1712. const char *name)
  1713. {
  1714. struct se_node_acl *se_nacl, *se_nacl_new;
  1715. struct sbp_nacl *nacl;
  1716. u64 guid = 0;
  1717. u32 nexus_depth = 1;
  1718. if (sbp_parse_wwn(name, &guid) < 0)
  1719. return ERR_PTR(-EINVAL);
  1720. se_nacl_new = sbp_alloc_fabric_acl(se_tpg);
  1721. if (!se_nacl_new)
  1722. return ERR_PTR(-ENOMEM);
  1723. /*
  1724. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  1725. * when converting a NodeACL from demo mode -> explict
  1726. */
  1727. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  1728. name, nexus_depth);
  1729. if (IS_ERR(se_nacl)) {
  1730. sbp_release_fabric_acl(se_tpg, se_nacl_new);
  1731. return se_nacl;
  1732. }
  1733. nacl = container_of(se_nacl, struct sbp_nacl, se_node_acl);
  1734. nacl->guid = guid;
  1735. sbp_format_wwn(nacl->iport_name, SBP_NAMELEN, guid);
  1736. return se_nacl;
  1737. }
  1738. static void sbp_drop_nodeacl(struct se_node_acl *se_acl)
  1739. {
  1740. struct sbp_nacl *nacl =
  1741. container_of(se_acl, struct sbp_nacl, se_node_acl);
  1742. core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
  1743. kfree(nacl);
  1744. }
  1745. static int sbp_post_link_lun(
  1746. struct se_portal_group *se_tpg,
  1747. struct se_lun *se_lun)
  1748. {
  1749. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1750. return sbp_update_unit_directory(tpg->tport);
  1751. }
  1752. static void sbp_pre_unlink_lun(
  1753. struct se_portal_group *se_tpg,
  1754. struct se_lun *se_lun)
  1755. {
  1756. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1757. struct sbp_tport *tport = tpg->tport;
  1758. int ret;
  1759. if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0)
  1760. tport->enable = 0;
  1761. ret = sbp_update_unit_directory(tport);
  1762. if (ret < 0)
  1763. pr_err("unlink LUN: failed to update unit directory\n");
  1764. }
  1765. static struct se_portal_group *sbp_make_tpg(
  1766. struct se_wwn *wwn,
  1767. struct config_group *group,
  1768. const char *name)
  1769. {
  1770. struct sbp_tport *tport =
  1771. container_of(wwn, struct sbp_tport, tport_wwn);
  1772. struct sbp_tpg *tpg;
  1773. unsigned long tpgt;
  1774. int ret;
  1775. if (strstr(name, "tpgt_") != name)
  1776. return ERR_PTR(-EINVAL);
  1777. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  1778. return ERR_PTR(-EINVAL);
  1779. if (tport->tpg) {
  1780. pr_err("Only one TPG per Unit is possible.\n");
  1781. return ERR_PTR(-EBUSY);
  1782. }
  1783. tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
  1784. if (!tpg) {
  1785. pr_err("Unable to allocate struct sbp_tpg\n");
  1786. return ERR_PTR(-ENOMEM);
  1787. }
  1788. tpg->tport = tport;
  1789. tpg->tport_tpgt = tpgt;
  1790. tport->tpg = tpg;
  1791. /* default attribute values */
  1792. tport->enable = 0;
  1793. tport->directory_id = -1;
  1794. tport->mgt_orb_timeout = 15;
  1795. tport->max_reconnect_timeout = 5;
  1796. tport->max_logins_per_lun = 1;
  1797. tport->mgt_agt = sbp_management_agent_register(tport);
  1798. if (IS_ERR(tport->mgt_agt)) {
  1799. ret = PTR_ERR(tport->mgt_agt);
  1800. goto out_free_tpg;
  1801. }
  1802. ret = core_tpg_register(&sbp_fabric_configfs->tf_ops, wwn,
  1803. &tpg->se_tpg, (void *)tpg,
  1804. TRANSPORT_TPG_TYPE_NORMAL);
  1805. if (ret < 0)
  1806. goto out_unreg_mgt_agt;
  1807. return &tpg->se_tpg;
  1808. out_unreg_mgt_agt:
  1809. sbp_management_agent_unregister(tport->mgt_agt);
  1810. out_free_tpg:
  1811. tport->tpg = NULL;
  1812. kfree(tpg);
  1813. return ERR_PTR(ret);
  1814. }
  1815. static void sbp_drop_tpg(struct se_portal_group *se_tpg)
  1816. {
  1817. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1818. struct sbp_tport *tport = tpg->tport;
  1819. core_tpg_deregister(se_tpg);
  1820. sbp_management_agent_unregister(tport->mgt_agt);
  1821. tport->tpg = NULL;
  1822. kfree(tpg);
  1823. }
  1824. static struct se_wwn *sbp_make_tport(
  1825. struct target_fabric_configfs *tf,
  1826. struct config_group *group,
  1827. const char *name)
  1828. {
  1829. struct sbp_tport *tport;
  1830. u64 guid = 0;
  1831. if (sbp_parse_wwn(name, &guid) < 0)
  1832. return ERR_PTR(-EINVAL);
  1833. tport = kzalloc(sizeof(*tport), GFP_KERNEL);
  1834. if (!tport) {
  1835. pr_err("Unable to allocate struct sbp_tport\n");
  1836. return ERR_PTR(-ENOMEM);
  1837. }
  1838. tport->guid = guid;
  1839. sbp_format_wwn(tport->tport_name, SBP_NAMELEN, guid);
  1840. return &tport->tport_wwn;
  1841. }
  1842. static void sbp_drop_tport(struct se_wwn *wwn)
  1843. {
  1844. struct sbp_tport *tport =
  1845. container_of(wwn, struct sbp_tport, tport_wwn);
  1846. kfree(tport);
  1847. }
  1848. static ssize_t sbp_wwn_show_attr_version(
  1849. struct target_fabric_configfs *tf,
  1850. char *page)
  1851. {
  1852. return sprintf(page, "FireWire SBP fabric module %s\n", SBP_VERSION);
  1853. }
  1854. TF_WWN_ATTR_RO(sbp, version);
  1855. static struct configfs_attribute *sbp_wwn_attrs[] = {
  1856. &sbp_wwn_version.attr,
  1857. NULL,
  1858. };
  1859. static ssize_t sbp_tpg_show_directory_id(
  1860. struct se_portal_group *se_tpg,
  1861. char *page)
  1862. {
  1863. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1864. struct sbp_tport *tport = tpg->tport;
  1865. if (tport->directory_id == -1)
  1866. return sprintf(page, "implicit\n");
  1867. else
  1868. return sprintf(page, "%06x\n", tport->directory_id);
  1869. }
  1870. static ssize_t sbp_tpg_store_directory_id(
  1871. struct se_portal_group *se_tpg,
  1872. const char *page,
  1873. size_t count)
  1874. {
  1875. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1876. struct sbp_tport *tport = tpg->tport;
  1877. unsigned long val;
  1878. if (tport->enable) {
  1879. pr_err("Cannot change the directory_id on an active target.\n");
  1880. return -EBUSY;
  1881. }
  1882. if (strstr(page, "implicit") == page) {
  1883. tport->directory_id = -1;
  1884. } else {
  1885. if (kstrtoul(page, 16, &val) < 0)
  1886. return -EINVAL;
  1887. if (val > 0xffffff)
  1888. return -EINVAL;
  1889. tport->directory_id = val;
  1890. }
  1891. return count;
  1892. }
  1893. static ssize_t sbp_tpg_show_enable(
  1894. struct se_portal_group *se_tpg,
  1895. char *page)
  1896. {
  1897. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1898. struct sbp_tport *tport = tpg->tport;
  1899. return sprintf(page, "%d\n", tport->enable);
  1900. }
  1901. static ssize_t sbp_tpg_store_enable(
  1902. struct se_portal_group *se_tpg,
  1903. const char *page,
  1904. size_t count)
  1905. {
  1906. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1907. struct sbp_tport *tport = tpg->tport;
  1908. unsigned long val;
  1909. int ret;
  1910. if (kstrtoul(page, 0, &val) < 0)
  1911. return -EINVAL;
  1912. if ((val != 0) && (val != 1))
  1913. return -EINVAL;
  1914. if (tport->enable == val)
  1915. return count;
  1916. if (val) {
  1917. if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0) {
  1918. pr_err("Cannot enable a target with no LUNs!\n");
  1919. return -EINVAL;
  1920. }
  1921. } else {
  1922. /* XXX: force-shutdown sessions instead? */
  1923. spin_lock_bh(&se_tpg->session_lock);
  1924. if (!list_empty(&se_tpg->tpg_sess_list)) {
  1925. spin_unlock_bh(&se_tpg->session_lock);
  1926. return -EBUSY;
  1927. }
  1928. spin_unlock_bh(&se_tpg->session_lock);
  1929. }
  1930. tport->enable = val;
  1931. ret = sbp_update_unit_directory(tport);
  1932. if (ret < 0) {
  1933. pr_err("Could not update Config ROM\n");
  1934. return ret;
  1935. }
  1936. return count;
  1937. }
  1938. TF_TPG_BASE_ATTR(sbp, directory_id, S_IRUGO | S_IWUSR);
  1939. TF_TPG_BASE_ATTR(sbp, enable, S_IRUGO | S_IWUSR);
  1940. static struct configfs_attribute *sbp_tpg_base_attrs[] = {
  1941. &sbp_tpg_directory_id.attr,
  1942. &sbp_tpg_enable.attr,
  1943. NULL,
  1944. };
  1945. static ssize_t sbp_tpg_attrib_show_mgt_orb_timeout(
  1946. struct se_portal_group *se_tpg,
  1947. char *page)
  1948. {
  1949. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1950. struct sbp_tport *tport = tpg->tport;
  1951. return sprintf(page, "%d\n", tport->mgt_orb_timeout);
  1952. }
  1953. static ssize_t sbp_tpg_attrib_store_mgt_orb_timeout(
  1954. struct se_portal_group *se_tpg,
  1955. const char *page,
  1956. size_t count)
  1957. {
  1958. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1959. struct sbp_tport *tport = tpg->tport;
  1960. unsigned long val;
  1961. int ret;
  1962. if (kstrtoul(page, 0, &val) < 0)
  1963. return -EINVAL;
  1964. if ((val < 1) || (val > 127))
  1965. return -EINVAL;
  1966. if (tport->mgt_orb_timeout == val)
  1967. return count;
  1968. tport->mgt_orb_timeout = val;
  1969. ret = sbp_update_unit_directory(tport);
  1970. if (ret < 0)
  1971. return ret;
  1972. return count;
  1973. }
  1974. static ssize_t sbp_tpg_attrib_show_max_reconnect_timeout(
  1975. struct se_portal_group *se_tpg,
  1976. char *page)
  1977. {
  1978. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1979. struct sbp_tport *tport = tpg->tport;
  1980. return sprintf(page, "%d\n", tport->max_reconnect_timeout);
  1981. }
  1982. static ssize_t sbp_tpg_attrib_store_max_reconnect_timeout(
  1983. struct se_portal_group *se_tpg,
  1984. const char *page,
  1985. size_t count)
  1986. {
  1987. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  1988. struct sbp_tport *tport = tpg->tport;
  1989. unsigned long val;
  1990. int ret;
  1991. if (kstrtoul(page, 0, &val) < 0)
  1992. return -EINVAL;
  1993. if ((val < 1) || (val > 32767))
  1994. return -EINVAL;
  1995. if (tport->max_reconnect_timeout == val)
  1996. return count;
  1997. tport->max_reconnect_timeout = val;
  1998. ret = sbp_update_unit_directory(tport);
  1999. if (ret < 0)
  2000. return ret;
  2001. return count;
  2002. }
  2003. static ssize_t sbp_tpg_attrib_show_max_logins_per_lun(
  2004. struct se_portal_group *se_tpg,
  2005. char *page)
  2006. {
  2007. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  2008. struct sbp_tport *tport = tpg->tport;
  2009. return sprintf(page, "%d\n", tport->max_logins_per_lun);
  2010. }
  2011. static ssize_t sbp_tpg_attrib_store_max_logins_per_lun(
  2012. struct se_portal_group *se_tpg,
  2013. const char *page,
  2014. size_t count)
  2015. {
  2016. struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  2017. struct sbp_tport *tport = tpg->tport;
  2018. unsigned long val;
  2019. if (kstrtoul(page, 0, &val) < 0)
  2020. return -EINVAL;
  2021. if ((val < 1) || (val > 127))
  2022. return -EINVAL;
  2023. /* XXX: also check against current count? */
  2024. tport->max_logins_per_lun = val;
  2025. return count;
  2026. }
  2027. TF_TPG_ATTRIB_ATTR(sbp, mgt_orb_timeout, S_IRUGO | S_IWUSR);
  2028. TF_TPG_ATTRIB_ATTR(sbp, max_reconnect_timeout, S_IRUGO | S_IWUSR);
  2029. TF_TPG_ATTRIB_ATTR(sbp, max_logins_per_lun, S_IRUGO | S_IWUSR);
  2030. static struct configfs_attribute *sbp_tpg_attrib_attrs[] = {
  2031. &sbp_tpg_attrib_mgt_orb_timeout.attr,
  2032. &sbp_tpg_attrib_max_reconnect_timeout.attr,
  2033. &sbp_tpg_attrib_max_logins_per_lun.attr,
  2034. NULL,
  2035. };
  2036. static struct target_core_fabric_ops sbp_ops = {
  2037. .get_fabric_name = sbp_get_fabric_name,
  2038. .get_fabric_proto_ident = sbp_get_fabric_proto_ident,
  2039. .tpg_get_wwn = sbp_get_fabric_wwn,
  2040. .tpg_get_tag = sbp_get_tag,
  2041. .tpg_get_default_depth = sbp_get_default_depth,
  2042. .tpg_get_pr_transport_id = sbp_get_pr_transport_id,
  2043. .tpg_get_pr_transport_id_len = sbp_get_pr_transport_id_len,
  2044. .tpg_parse_pr_out_transport_id = sbp_parse_pr_out_transport_id,
  2045. .tpg_check_demo_mode = sbp_check_true,
  2046. .tpg_check_demo_mode_cache = sbp_check_true,
  2047. .tpg_check_demo_mode_write_protect = sbp_check_false,
  2048. .tpg_check_prod_mode_write_protect = sbp_check_false,
  2049. .tpg_alloc_fabric_acl = sbp_alloc_fabric_acl,
  2050. .tpg_release_fabric_acl = sbp_release_fabric_acl,
  2051. .tpg_get_inst_index = sbp_tpg_get_inst_index,
  2052. .release_cmd = sbp_release_cmd,
  2053. .shutdown_session = sbp_shutdown_session,
  2054. .close_session = sbp_close_session,
  2055. .sess_get_index = sbp_sess_get_index,
  2056. .write_pending = sbp_write_pending,
  2057. .write_pending_status = sbp_write_pending_status,
  2058. .set_default_node_attributes = sbp_set_default_node_attrs,
  2059. .get_task_tag = sbp_get_task_tag,
  2060. .get_cmd_state = sbp_get_cmd_state,
  2061. .queue_data_in = sbp_queue_data_in,
  2062. .queue_status = sbp_queue_status,
  2063. .queue_tm_rsp = sbp_queue_tm_rsp,
  2064. .check_stop_free = sbp_check_stop_free,
  2065. .fabric_make_wwn = sbp_make_tport,
  2066. .fabric_drop_wwn = sbp_drop_tport,
  2067. .fabric_make_tpg = sbp_make_tpg,
  2068. .fabric_drop_tpg = sbp_drop_tpg,
  2069. .fabric_post_link = sbp_post_link_lun,
  2070. .fabric_pre_unlink = sbp_pre_unlink_lun,
  2071. .fabric_make_np = NULL,
  2072. .fabric_drop_np = NULL,
  2073. .fabric_make_nodeacl = sbp_make_nodeacl,
  2074. .fabric_drop_nodeacl = sbp_drop_nodeacl,
  2075. };
  2076. static int sbp_register_configfs(void)
  2077. {
  2078. struct target_fabric_configfs *fabric;
  2079. int ret;
  2080. fabric = target_fabric_configfs_init(THIS_MODULE, "sbp");
  2081. if (IS_ERR(fabric)) {
  2082. pr_err("target_fabric_configfs_init() failed\n");
  2083. return PTR_ERR(fabric);
  2084. }
  2085. fabric->tf_ops = sbp_ops;
  2086. /*
  2087. * Setup default attribute lists for various fabric->tf_cit_tmpl
  2088. */
  2089. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = sbp_wwn_attrs;
  2090. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = sbp_tpg_base_attrs;
  2091. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = sbp_tpg_attrib_attrs;
  2092. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  2093. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  2094. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  2095. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  2096. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  2097. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  2098. ret = target_fabric_configfs_register(fabric);
  2099. if (ret < 0) {
  2100. pr_err("target_fabric_configfs_register() failed for SBP\n");
  2101. return ret;
  2102. }
  2103. sbp_fabric_configfs = fabric;
  2104. return 0;
  2105. };
  2106. static void sbp_deregister_configfs(void)
  2107. {
  2108. if (!sbp_fabric_configfs)
  2109. return;
  2110. target_fabric_configfs_deregister(sbp_fabric_configfs);
  2111. sbp_fabric_configfs = NULL;
  2112. };
  2113. static int __init sbp_init(void)
  2114. {
  2115. int ret;
  2116. ret = sbp_register_configfs();
  2117. if (ret < 0)
  2118. return ret;
  2119. return 0;
  2120. };
  2121. static void __exit sbp_exit(void)
  2122. {
  2123. sbp_deregister_configfs();
  2124. };
  2125. MODULE_DESCRIPTION("FireWire SBP fabric driver");
  2126. MODULE_LICENSE("GPL");
  2127. module_init(sbp_init);
  2128. module_exit(sbp_exit);