sbp_target.c 65 KB

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