target_core_device.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /*******************************************************************************
  2. * Filename: target_core_device.c (based on iscsi_target_device.c)
  3. *
  4. * This file contains the TCM Virtual Device and Disk Transport
  5. * agnostic related functions.
  6. *
  7. * (c) Copyright 2003-2012 RisingTide Systems LLC.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. ******************************************************************************/
  26. #include <linux/net.h>
  27. #include <linux/string.h>
  28. #include <linux/delay.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/kthread.h>
  33. #include <linux/in.h>
  34. #include <linux/export.h>
  35. #include <net/sock.h>
  36. #include <net/tcp.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_device.h>
  39. #include <target/target_core_base.h>
  40. #include <target/target_core_backend.h>
  41. #include <target/target_core_fabric.h>
  42. #include "target_core_internal.h"
  43. #include "target_core_alua.h"
  44. #include "target_core_pr.h"
  45. #include "target_core_ua.h"
  46. static struct se_hba *lun0_hba;
  47. /* not static, needed by tpg.c */
  48. struct se_device *g_lun0_dev;
  49. sense_reason_t
  50. transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  51. {
  52. struct se_lun *se_lun = NULL;
  53. struct se_session *se_sess = se_cmd->se_sess;
  54. struct se_device *dev;
  55. unsigned long flags;
  56. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  57. return TCM_NON_EXISTENT_LUN;
  58. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  59. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  60. if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  61. struct se_dev_entry *deve = se_cmd->se_deve;
  62. deve->total_cmds++;
  63. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  64. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
  65. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  66. " Access for 0x%08x\n",
  67. se_cmd->se_tfo->get_fabric_name(),
  68. unpacked_lun);
  69. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  70. return TCM_WRITE_PROTECTED;
  71. }
  72. if (se_cmd->data_direction == DMA_TO_DEVICE)
  73. deve->write_bytes += se_cmd->data_length;
  74. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  75. deve->read_bytes += se_cmd->data_length;
  76. se_lun = deve->se_lun;
  77. se_cmd->se_lun = deve->se_lun;
  78. se_cmd->pr_res_key = deve->pr_res_key;
  79. se_cmd->orig_fe_lun = unpacked_lun;
  80. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  81. }
  82. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  83. if (!se_lun) {
  84. /*
  85. * Use the se_portal_group->tpg_virt_lun0 to allow for
  86. * REPORT_LUNS, et al to be returned when no active
  87. * MappedLUN=0 exists for this Initiator Port.
  88. */
  89. if (unpacked_lun != 0) {
  90. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  91. " Access for 0x%08x\n",
  92. se_cmd->se_tfo->get_fabric_name(),
  93. unpacked_lun);
  94. return TCM_NON_EXISTENT_LUN;
  95. }
  96. /*
  97. * Force WRITE PROTECT for virtual LUN 0
  98. */
  99. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  100. (se_cmd->data_direction != DMA_NONE))
  101. return TCM_WRITE_PROTECTED;
  102. se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  103. se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  104. se_cmd->orig_fe_lun = 0;
  105. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  106. }
  107. /* Directly associate cmd with se_dev */
  108. se_cmd->se_dev = se_lun->lun_se_dev;
  109. /* TODO: get rid of this and use atomics for stats */
  110. dev = se_lun->lun_se_dev;
  111. spin_lock_irqsave(&dev->stats_lock, flags);
  112. dev->num_cmds++;
  113. if (se_cmd->data_direction == DMA_TO_DEVICE)
  114. dev->write_bytes += se_cmd->data_length;
  115. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  116. dev->read_bytes += se_cmd->data_length;
  117. spin_unlock_irqrestore(&dev->stats_lock, flags);
  118. spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
  119. list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
  120. spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  124. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  125. {
  126. struct se_dev_entry *deve;
  127. struct se_lun *se_lun = NULL;
  128. struct se_session *se_sess = se_cmd->se_sess;
  129. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  130. unsigned long flags;
  131. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  132. return -ENODEV;
  133. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  134. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  135. deve = se_cmd->se_deve;
  136. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  137. se_tmr->tmr_lun = deve->se_lun;
  138. se_cmd->se_lun = deve->se_lun;
  139. se_lun = deve->se_lun;
  140. se_cmd->pr_res_key = deve->pr_res_key;
  141. se_cmd->orig_fe_lun = unpacked_lun;
  142. }
  143. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  144. if (!se_lun) {
  145. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  146. " Access for 0x%08x\n",
  147. se_cmd->se_tfo->get_fabric_name(),
  148. unpacked_lun);
  149. return -ENODEV;
  150. }
  151. /* Directly associate cmd with se_dev */
  152. se_cmd->se_dev = se_lun->lun_se_dev;
  153. se_tmr->tmr_dev = se_lun->lun_se_dev;
  154. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  155. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  156. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  160. /*
  161. * This function is called from core_scsi3_emulate_pro_register_and_move()
  162. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
  163. * when a matching rtpi is found.
  164. */
  165. struct se_dev_entry *core_get_se_deve_from_rtpi(
  166. struct se_node_acl *nacl,
  167. u16 rtpi)
  168. {
  169. struct se_dev_entry *deve;
  170. struct se_lun *lun;
  171. struct se_port *port;
  172. struct se_portal_group *tpg = nacl->se_tpg;
  173. u32 i;
  174. spin_lock_irq(&nacl->device_list_lock);
  175. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  176. deve = nacl->device_list[i];
  177. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  178. continue;
  179. lun = deve->se_lun;
  180. if (!lun) {
  181. pr_err("%s device entries device pointer is"
  182. " NULL, but Initiator has access.\n",
  183. tpg->se_tpg_tfo->get_fabric_name());
  184. continue;
  185. }
  186. port = lun->lun_sep;
  187. if (!port) {
  188. pr_err("%s device entries device pointer is"
  189. " NULL, but Initiator has access.\n",
  190. tpg->se_tpg_tfo->get_fabric_name());
  191. continue;
  192. }
  193. if (port->sep_rtpi != rtpi)
  194. continue;
  195. atomic_inc(&deve->pr_ref_count);
  196. smp_mb__after_atomic_inc();
  197. spin_unlock_irq(&nacl->device_list_lock);
  198. return deve;
  199. }
  200. spin_unlock_irq(&nacl->device_list_lock);
  201. return NULL;
  202. }
  203. int core_free_device_list_for_node(
  204. struct se_node_acl *nacl,
  205. struct se_portal_group *tpg)
  206. {
  207. struct se_dev_entry *deve;
  208. struct se_lun *lun;
  209. u32 i;
  210. if (!nacl->device_list)
  211. return 0;
  212. spin_lock_irq(&nacl->device_list_lock);
  213. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  214. deve = nacl->device_list[i];
  215. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  216. continue;
  217. if (!deve->se_lun) {
  218. pr_err("%s device entries device pointer is"
  219. " NULL, but Initiator has access.\n",
  220. tpg->se_tpg_tfo->get_fabric_name());
  221. continue;
  222. }
  223. lun = deve->se_lun;
  224. spin_unlock_irq(&nacl->device_list_lock);
  225. core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
  226. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  227. spin_lock_irq(&nacl->device_list_lock);
  228. }
  229. spin_unlock_irq(&nacl->device_list_lock);
  230. array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
  231. nacl->device_list = NULL;
  232. return 0;
  233. }
  234. void core_update_device_list_access(
  235. u32 mapped_lun,
  236. u32 lun_access,
  237. struct se_node_acl *nacl)
  238. {
  239. struct se_dev_entry *deve;
  240. spin_lock_irq(&nacl->device_list_lock);
  241. deve = nacl->device_list[mapped_lun];
  242. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  243. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  244. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  245. } else {
  246. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  247. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  248. }
  249. spin_unlock_irq(&nacl->device_list_lock);
  250. }
  251. /* core_enable_device_list_for_node():
  252. *
  253. *
  254. */
  255. int core_enable_device_list_for_node(
  256. struct se_lun *lun,
  257. struct se_lun_acl *lun_acl,
  258. u32 mapped_lun,
  259. u32 lun_access,
  260. struct se_node_acl *nacl,
  261. struct se_portal_group *tpg)
  262. {
  263. struct se_port *port = lun->lun_sep;
  264. struct se_dev_entry *deve;
  265. spin_lock_irq(&nacl->device_list_lock);
  266. deve = nacl->device_list[mapped_lun];
  267. /*
  268. * Check if the call is handling demo mode -> explict LUN ACL
  269. * transition. This transition must be for the same struct se_lun
  270. * + mapped_lun that was setup in demo mode..
  271. */
  272. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  273. if (deve->se_lun_acl != NULL) {
  274. pr_err("struct se_dev_entry->se_lun_acl"
  275. " already set for demo mode -> explict"
  276. " LUN ACL transition\n");
  277. spin_unlock_irq(&nacl->device_list_lock);
  278. return -EINVAL;
  279. }
  280. if (deve->se_lun != lun) {
  281. pr_err("struct se_dev_entry->se_lun does"
  282. " match passed struct se_lun for demo mode"
  283. " -> explict LUN ACL transition\n");
  284. spin_unlock_irq(&nacl->device_list_lock);
  285. return -EINVAL;
  286. }
  287. deve->se_lun_acl = lun_acl;
  288. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  289. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  290. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  291. } else {
  292. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  293. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  294. }
  295. spin_unlock_irq(&nacl->device_list_lock);
  296. return 0;
  297. }
  298. deve->se_lun = lun;
  299. deve->se_lun_acl = lun_acl;
  300. deve->mapped_lun = mapped_lun;
  301. deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
  302. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  303. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  304. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  305. } else {
  306. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  307. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  308. }
  309. deve->creation_time = get_jiffies_64();
  310. deve->attach_count++;
  311. spin_unlock_irq(&nacl->device_list_lock);
  312. spin_lock_bh(&port->sep_alua_lock);
  313. list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
  314. spin_unlock_bh(&port->sep_alua_lock);
  315. return 0;
  316. }
  317. /* core_disable_device_list_for_node():
  318. *
  319. *
  320. */
  321. int core_disable_device_list_for_node(
  322. struct se_lun *lun,
  323. struct se_lun_acl *lun_acl,
  324. u32 mapped_lun,
  325. u32 lun_access,
  326. struct se_node_acl *nacl,
  327. struct se_portal_group *tpg)
  328. {
  329. struct se_port *port = lun->lun_sep;
  330. struct se_dev_entry *deve = nacl->device_list[mapped_lun];
  331. /*
  332. * If the MappedLUN entry is being disabled, the entry in
  333. * port->sep_alua_list must be removed now before clearing the
  334. * struct se_dev_entry pointers below as logic in
  335. * core_alua_do_transition_tg_pt() depends on these being present.
  336. *
  337. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  338. * that have not been explicitly converted to MappedLUNs ->
  339. * struct se_lun_acl, but we remove deve->alua_port_list from
  340. * port->sep_alua_list. This also means that active UAs and
  341. * NodeACL context specific PR metadata for demo-mode
  342. * MappedLUN *deve will be released below..
  343. */
  344. spin_lock_bh(&port->sep_alua_lock);
  345. list_del(&deve->alua_port_list);
  346. spin_unlock_bh(&port->sep_alua_lock);
  347. /*
  348. * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
  349. * PR operation to complete.
  350. */
  351. while (atomic_read(&deve->pr_ref_count) != 0)
  352. cpu_relax();
  353. spin_lock_irq(&nacl->device_list_lock);
  354. /*
  355. * Disable struct se_dev_entry LUN ACL mapping
  356. */
  357. core_scsi3_ua_release_all(deve);
  358. deve->se_lun = NULL;
  359. deve->se_lun_acl = NULL;
  360. deve->lun_flags = 0;
  361. deve->creation_time = 0;
  362. deve->attach_count--;
  363. spin_unlock_irq(&nacl->device_list_lock);
  364. core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
  365. return 0;
  366. }
  367. /* core_clear_lun_from_tpg():
  368. *
  369. *
  370. */
  371. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  372. {
  373. struct se_node_acl *nacl;
  374. struct se_dev_entry *deve;
  375. u32 i;
  376. spin_lock_irq(&tpg->acl_node_lock);
  377. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  378. spin_unlock_irq(&tpg->acl_node_lock);
  379. spin_lock_irq(&nacl->device_list_lock);
  380. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  381. deve = nacl->device_list[i];
  382. if (lun != deve->se_lun)
  383. continue;
  384. spin_unlock_irq(&nacl->device_list_lock);
  385. core_disable_device_list_for_node(lun, NULL,
  386. deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
  387. nacl, tpg);
  388. spin_lock_irq(&nacl->device_list_lock);
  389. }
  390. spin_unlock_irq(&nacl->device_list_lock);
  391. spin_lock_irq(&tpg->acl_node_lock);
  392. }
  393. spin_unlock_irq(&tpg->acl_node_lock);
  394. }
  395. static struct se_port *core_alloc_port(struct se_device *dev)
  396. {
  397. struct se_port *port, *port_tmp;
  398. port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
  399. if (!port) {
  400. pr_err("Unable to allocate struct se_port\n");
  401. return ERR_PTR(-ENOMEM);
  402. }
  403. INIT_LIST_HEAD(&port->sep_alua_list);
  404. INIT_LIST_HEAD(&port->sep_list);
  405. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  406. spin_lock_init(&port->sep_alua_lock);
  407. mutex_init(&port->sep_tg_pt_md_mutex);
  408. spin_lock(&dev->se_port_lock);
  409. if (dev->dev_port_count == 0x0000ffff) {
  410. pr_warn("Reached dev->dev_port_count =="
  411. " 0x0000ffff\n");
  412. spin_unlock(&dev->se_port_lock);
  413. return ERR_PTR(-ENOSPC);
  414. }
  415. again:
  416. /*
  417. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  418. * Here is the table from spc4r17 section 7.7.3.8.
  419. *
  420. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  421. *
  422. * Code Description
  423. * 0h Reserved
  424. * 1h Relative port 1, historically known as port A
  425. * 2h Relative port 2, historically known as port B
  426. * 3h to FFFFh Relative port 3 through 65 535
  427. */
  428. port->sep_rtpi = dev->dev_rpti_counter++;
  429. if (!port->sep_rtpi)
  430. goto again;
  431. list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
  432. /*
  433. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  434. * for 16-bit wrap..
  435. */
  436. if (port->sep_rtpi == port_tmp->sep_rtpi)
  437. goto again;
  438. }
  439. spin_unlock(&dev->se_port_lock);
  440. return port;
  441. }
  442. static void core_export_port(
  443. struct se_device *dev,
  444. struct se_portal_group *tpg,
  445. struct se_port *port,
  446. struct se_lun *lun)
  447. {
  448. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
  449. spin_lock(&dev->se_port_lock);
  450. spin_lock(&lun->lun_sep_lock);
  451. port->sep_tpg = tpg;
  452. port->sep_lun = lun;
  453. lun->lun_sep = port;
  454. spin_unlock(&lun->lun_sep_lock);
  455. list_add_tail(&port->sep_list, &dev->dev_sep_list);
  456. spin_unlock(&dev->se_port_lock);
  457. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
  458. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  459. tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
  460. if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
  461. pr_err("Unable to allocate t10_alua_tg_pt"
  462. "_gp_member_t\n");
  463. return;
  464. }
  465. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  466. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  467. dev->t10_alua.default_tg_pt_gp);
  468. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  469. pr_debug("%s/%s: Adding to default ALUA Target Port"
  470. " Group: alua/default_tg_pt_gp\n",
  471. dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
  472. }
  473. dev->dev_port_count++;
  474. port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
  475. }
  476. /*
  477. * Called with struct se_device->se_port_lock spinlock held.
  478. */
  479. static void core_release_port(struct se_device *dev, struct se_port *port)
  480. __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
  481. {
  482. /*
  483. * Wait for any port reference for PR ALL_TG_PT=1 operation
  484. * to complete in __core_scsi3_alloc_registration()
  485. */
  486. spin_unlock(&dev->se_port_lock);
  487. if (atomic_read(&port->sep_tg_pt_ref_cnt))
  488. cpu_relax();
  489. spin_lock(&dev->se_port_lock);
  490. core_alua_free_tg_pt_gp_mem(port);
  491. list_del(&port->sep_list);
  492. dev->dev_port_count--;
  493. kfree(port);
  494. }
  495. int core_dev_export(
  496. struct se_device *dev,
  497. struct se_portal_group *tpg,
  498. struct se_lun *lun)
  499. {
  500. struct se_hba *hba = dev->se_hba;
  501. struct se_port *port;
  502. port = core_alloc_port(dev);
  503. if (IS_ERR(port))
  504. return PTR_ERR(port);
  505. lun->lun_se_dev = dev;
  506. spin_lock(&hba->device_lock);
  507. dev->export_count++;
  508. spin_unlock(&hba->device_lock);
  509. core_export_port(dev, tpg, port, lun);
  510. return 0;
  511. }
  512. void core_dev_unexport(
  513. struct se_device *dev,
  514. struct se_portal_group *tpg,
  515. struct se_lun *lun)
  516. {
  517. struct se_hba *hba = dev->se_hba;
  518. struct se_port *port = lun->lun_sep;
  519. spin_lock(&lun->lun_sep_lock);
  520. if (lun->lun_se_dev == NULL) {
  521. spin_unlock(&lun->lun_sep_lock);
  522. return;
  523. }
  524. spin_unlock(&lun->lun_sep_lock);
  525. spin_lock(&dev->se_port_lock);
  526. core_release_port(dev, port);
  527. spin_unlock(&dev->se_port_lock);
  528. spin_lock(&hba->device_lock);
  529. dev->export_count--;
  530. spin_unlock(&hba->device_lock);
  531. lun->lun_se_dev = NULL;
  532. }
  533. static void se_release_vpd_for_dev(struct se_device *dev)
  534. {
  535. struct t10_vpd *vpd, *vpd_tmp;
  536. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  537. list_for_each_entry_safe(vpd, vpd_tmp,
  538. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  539. list_del(&vpd->vpd_list);
  540. kfree(vpd);
  541. }
  542. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  543. }
  544. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  545. {
  546. u32 aligned_max_sectors;
  547. u32 alignment;
  548. /*
  549. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  550. * transport_allocate_data_tasks() operation.
  551. */
  552. alignment = max(1ul, PAGE_SIZE / block_size);
  553. aligned_max_sectors = rounddown(max_sectors, alignment);
  554. if (max_sectors != aligned_max_sectors)
  555. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  556. max_sectors, aligned_max_sectors);
  557. return aligned_max_sectors;
  558. }
  559. int se_dev_set_max_unmap_lba_count(
  560. struct se_device *dev,
  561. u32 max_unmap_lba_count)
  562. {
  563. dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
  564. pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
  565. dev, dev->dev_attrib.max_unmap_lba_count);
  566. return 0;
  567. }
  568. int se_dev_set_max_unmap_block_desc_count(
  569. struct se_device *dev,
  570. u32 max_unmap_block_desc_count)
  571. {
  572. dev->dev_attrib.max_unmap_block_desc_count =
  573. max_unmap_block_desc_count;
  574. pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
  575. dev, dev->dev_attrib.max_unmap_block_desc_count);
  576. return 0;
  577. }
  578. int se_dev_set_unmap_granularity(
  579. struct se_device *dev,
  580. u32 unmap_granularity)
  581. {
  582. dev->dev_attrib.unmap_granularity = unmap_granularity;
  583. pr_debug("dev[%p]: Set unmap_granularity: %u\n",
  584. dev, dev->dev_attrib.unmap_granularity);
  585. return 0;
  586. }
  587. int se_dev_set_unmap_granularity_alignment(
  588. struct se_device *dev,
  589. u32 unmap_granularity_alignment)
  590. {
  591. dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
  592. pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
  593. dev, dev->dev_attrib.unmap_granularity_alignment);
  594. return 0;
  595. }
  596. int se_dev_set_max_write_same_len(
  597. struct se_device *dev,
  598. u32 max_write_same_len)
  599. {
  600. dev->dev_attrib.max_write_same_len = max_write_same_len;
  601. pr_debug("dev[%p]: Set max_write_same_len: %u\n",
  602. dev, dev->dev_attrib.max_write_same_len);
  603. return 0;
  604. }
  605. static void dev_set_t10_wwn_model_alias(struct se_device *dev)
  606. {
  607. const char *configname;
  608. configname = config_item_name(&dev->dev_group.cg_item);
  609. if (strlen(configname) >= 16) {
  610. pr_warn("dev[%p]: Backstore name '%s' is too long for "
  611. "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
  612. configname);
  613. }
  614. snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
  615. }
  616. int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
  617. {
  618. if (dev->export_count) {
  619. pr_err("dev[%p]: Unable to change model alias"
  620. " while export_count is %d\n",
  621. dev, dev->export_count);
  622. return -EINVAL;
  623. }
  624. if (flag != 0 && flag != 1) {
  625. pr_err("Illegal value %d\n", flag);
  626. return -EINVAL;
  627. }
  628. if (flag) {
  629. dev_set_t10_wwn_model_alias(dev);
  630. } else {
  631. strncpy(&dev->t10_wwn.model[0],
  632. dev->transport->inquiry_prod, 16);
  633. }
  634. dev->dev_attrib.emulate_model_alias = flag;
  635. return 0;
  636. }
  637. int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
  638. {
  639. if (flag != 0 && flag != 1) {
  640. pr_err("Illegal value %d\n", flag);
  641. return -EINVAL;
  642. }
  643. if (flag) {
  644. pr_err("dpo_emulated not supported\n");
  645. return -EINVAL;
  646. }
  647. return 0;
  648. }
  649. int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
  650. {
  651. if (flag != 0 && flag != 1) {
  652. pr_err("Illegal value %d\n", flag);
  653. return -EINVAL;
  654. }
  655. if (flag &&
  656. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  657. pr_err("emulate_fua_write not supported for pSCSI\n");
  658. return -EINVAL;
  659. }
  660. dev->dev_attrib.emulate_fua_write = flag;
  661. pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
  662. dev, dev->dev_attrib.emulate_fua_write);
  663. return 0;
  664. }
  665. int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
  666. {
  667. if (flag != 0 && flag != 1) {
  668. pr_err("Illegal value %d\n", flag);
  669. return -EINVAL;
  670. }
  671. if (flag) {
  672. pr_err("ua read emulated not supported\n");
  673. return -EINVAL;
  674. }
  675. return 0;
  676. }
  677. int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
  678. {
  679. if (flag != 0 && flag != 1) {
  680. pr_err("Illegal value %d\n", flag);
  681. return -EINVAL;
  682. }
  683. if (flag &&
  684. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  685. pr_err("emulate_write_cache not supported for pSCSI\n");
  686. return -EINVAL;
  687. }
  688. if (dev->transport->get_write_cache) {
  689. pr_warn("emulate_write_cache cannot be changed when underlying"
  690. " HW reports WriteCacheEnabled, ignoring request\n");
  691. return 0;
  692. }
  693. dev->dev_attrib.emulate_write_cache = flag;
  694. pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
  695. dev, dev->dev_attrib.emulate_write_cache);
  696. return 0;
  697. }
  698. int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
  699. {
  700. if ((flag != 0) && (flag != 1) && (flag != 2)) {
  701. pr_err("Illegal value %d\n", flag);
  702. return -EINVAL;
  703. }
  704. if (dev->export_count) {
  705. pr_err("dev[%p]: Unable to change SE Device"
  706. " UA_INTRLCK_CTRL while export_count is %d\n",
  707. dev, dev->export_count);
  708. return -EINVAL;
  709. }
  710. dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
  711. pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
  712. dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
  713. return 0;
  714. }
  715. int se_dev_set_emulate_tas(struct se_device *dev, int flag)
  716. {
  717. if ((flag != 0) && (flag != 1)) {
  718. pr_err("Illegal value %d\n", flag);
  719. return -EINVAL;
  720. }
  721. if (dev->export_count) {
  722. pr_err("dev[%p]: Unable to change SE Device TAS while"
  723. " export_count is %d\n",
  724. dev, dev->export_count);
  725. return -EINVAL;
  726. }
  727. dev->dev_attrib.emulate_tas = flag;
  728. pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
  729. dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
  730. return 0;
  731. }
  732. int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
  733. {
  734. if ((flag != 0) && (flag != 1)) {
  735. pr_err("Illegal value %d\n", flag);
  736. return -EINVAL;
  737. }
  738. /*
  739. * We expect this value to be non-zero when generic Block Layer
  740. * Discard supported is detected iblock_create_virtdevice().
  741. */
  742. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  743. pr_err("Generic Block Discard not supported\n");
  744. return -ENOSYS;
  745. }
  746. dev->dev_attrib.emulate_tpu = flag;
  747. pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
  748. dev, flag);
  749. return 0;
  750. }
  751. int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
  752. {
  753. if ((flag != 0) && (flag != 1)) {
  754. pr_err("Illegal value %d\n", flag);
  755. return -EINVAL;
  756. }
  757. /*
  758. * We expect this value to be non-zero when generic Block Layer
  759. * Discard supported is detected iblock_create_virtdevice().
  760. */
  761. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  762. pr_err("Generic Block Discard not supported\n");
  763. return -ENOSYS;
  764. }
  765. dev->dev_attrib.emulate_tpws = flag;
  766. pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
  767. dev, flag);
  768. return 0;
  769. }
  770. int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
  771. {
  772. if ((flag != 0) && (flag != 1)) {
  773. pr_err("Illegal value %d\n", flag);
  774. return -EINVAL;
  775. }
  776. dev->dev_attrib.enforce_pr_isids = flag;
  777. pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
  778. (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
  779. return 0;
  780. }
  781. int se_dev_set_is_nonrot(struct se_device *dev, int flag)
  782. {
  783. if ((flag != 0) && (flag != 1)) {
  784. printk(KERN_ERR "Illegal value %d\n", flag);
  785. return -EINVAL;
  786. }
  787. dev->dev_attrib.is_nonrot = flag;
  788. pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
  789. dev, flag);
  790. return 0;
  791. }
  792. int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
  793. {
  794. if (flag != 0) {
  795. printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
  796. " reordering not implemented\n", dev);
  797. return -ENOSYS;
  798. }
  799. dev->dev_attrib.emulate_rest_reord = flag;
  800. pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
  801. return 0;
  802. }
  803. /*
  804. * Note, this can only be called on unexported SE Device Object.
  805. */
  806. int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
  807. {
  808. if (dev->export_count) {
  809. pr_err("dev[%p]: Unable to change SE Device TCQ while"
  810. " export_count is %d\n",
  811. dev, dev->export_count);
  812. return -EINVAL;
  813. }
  814. if (!queue_depth) {
  815. pr_err("dev[%p]: Illegal ZERO value for queue"
  816. "_depth\n", dev);
  817. return -EINVAL;
  818. }
  819. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  820. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  821. pr_err("dev[%p]: Passed queue_depth: %u"
  822. " exceeds TCM/SE_Device TCQ: %u\n",
  823. dev, queue_depth,
  824. dev->dev_attrib.hw_queue_depth);
  825. return -EINVAL;
  826. }
  827. } else {
  828. if (queue_depth > dev->dev_attrib.queue_depth) {
  829. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  830. pr_err("dev[%p]: Passed queue_depth:"
  831. " %u exceeds TCM/SE_Device MAX"
  832. " TCQ: %u\n", dev, queue_depth,
  833. dev->dev_attrib.hw_queue_depth);
  834. return -EINVAL;
  835. }
  836. }
  837. }
  838. dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
  839. pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
  840. dev, queue_depth);
  841. return 0;
  842. }
  843. int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
  844. {
  845. int block_size = dev->dev_attrib.block_size;
  846. if (dev->export_count) {
  847. pr_err("dev[%p]: Unable to change SE Device"
  848. " fabric_max_sectors while export_count is %d\n",
  849. dev, dev->export_count);
  850. return -EINVAL;
  851. }
  852. if (!fabric_max_sectors) {
  853. pr_err("dev[%p]: Illegal ZERO value for"
  854. " fabric_max_sectors\n", dev);
  855. return -EINVAL;
  856. }
  857. if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
  858. pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
  859. " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
  860. DA_STATUS_MAX_SECTORS_MIN);
  861. return -EINVAL;
  862. }
  863. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  864. if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
  865. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  866. " greater than TCM/SE_Device max_sectors:"
  867. " %u\n", dev, fabric_max_sectors,
  868. dev->dev_attrib.hw_max_sectors);
  869. return -EINVAL;
  870. }
  871. } else {
  872. if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
  873. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  874. " greater than DA_STATUS_MAX_SECTORS_MAX:"
  875. " %u\n", dev, fabric_max_sectors,
  876. DA_STATUS_MAX_SECTORS_MAX);
  877. return -EINVAL;
  878. }
  879. }
  880. /*
  881. * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
  882. */
  883. if (!block_size) {
  884. block_size = 512;
  885. pr_warn("Defaulting to 512 for zero block_size\n");
  886. }
  887. fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
  888. block_size);
  889. dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
  890. pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
  891. dev, fabric_max_sectors);
  892. return 0;
  893. }
  894. int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
  895. {
  896. if (dev->export_count) {
  897. pr_err("dev[%p]: Unable to change SE Device"
  898. " optimal_sectors while export_count is %d\n",
  899. dev, dev->export_count);
  900. return -EINVAL;
  901. }
  902. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  903. pr_err("dev[%p]: Passed optimal_sectors cannot be"
  904. " changed for TCM/pSCSI\n", dev);
  905. return -EINVAL;
  906. }
  907. if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
  908. pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
  909. " greater than fabric_max_sectors: %u\n", dev,
  910. optimal_sectors, dev->dev_attrib.fabric_max_sectors);
  911. return -EINVAL;
  912. }
  913. dev->dev_attrib.optimal_sectors = optimal_sectors;
  914. pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
  915. dev, optimal_sectors);
  916. return 0;
  917. }
  918. int se_dev_set_block_size(struct se_device *dev, u32 block_size)
  919. {
  920. if (dev->export_count) {
  921. pr_err("dev[%p]: Unable to change SE Device block_size"
  922. " while export_count is %d\n",
  923. dev, dev->export_count);
  924. return -EINVAL;
  925. }
  926. if ((block_size != 512) &&
  927. (block_size != 1024) &&
  928. (block_size != 2048) &&
  929. (block_size != 4096)) {
  930. pr_err("dev[%p]: Illegal value for block_device: %u"
  931. " for SE device, must be 512, 1024, 2048 or 4096\n",
  932. dev, block_size);
  933. return -EINVAL;
  934. }
  935. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  936. pr_err("dev[%p]: Not allowed to change block_size for"
  937. " Physical Device, use for Linux/SCSI to change"
  938. " block_size for underlying hardware\n", dev);
  939. return -EINVAL;
  940. }
  941. dev->dev_attrib.block_size = block_size;
  942. pr_debug("dev[%p]: SE Device block_size changed to %u\n",
  943. dev, block_size);
  944. return 0;
  945. }
  946. struct se_lun *core_dev_add_lun(
  947. struct se_portal_group *tpg,
  948. struct se_device *dev,
  949. u32 lun)
  950. {
  951. struct se_lun *lun_p;
  952. int rc;
  953. lun_p = core_tpg_pre_addlun(tpg, lun);
  954. if (IS_ERR(lun_p))
  955. return lun_p;
  956. rc = core_tpg_post_addlun(tpg, lun_p,
  957. TRANSPORT_LUNFLAGS_READ_WRITE, dev);
  958. if (rc < 0)
  959. return ERR_PTR(rc);
  960. pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
  961. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  962. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
  963. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  964. /*
  965. * Update LUN maps for dynamically added initiators when
  966. * generate_node_acl is enabled.
  967. */
  968. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  969. struct se_node_acl *acl;
  970. spin_lock_irq(&tpg->acl_node_lock);
  971. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  972. if (acl->dynamic_node_acl &&
  973. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  974. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  975. spin_unlock_irq(&tpg->acl_node_lock);
  976. core_tpg_add_node_to_devs(acl, tpg);
  977. spin_lock_irq(&tpg->acl_node_lock);
  978. }
  979. }
  980. spin_unlock_irq(&tpg->acl_node_lock);
  981. }
  982. return lun_p;
  983. }
  984. /* core_dev_del_lun():
  985. *
  986. *
  987. */
  988. int core_dev_del_lun(
  989. struct se_portal_group *tpg,
  990. u32 unpacked_lun)
  991. {
  992. struct se_lun *lun;
  993. lun = core_tpg_pre_dellun(tpg, unpacked_lun);
  994. if (IS_ERR(lun))
  995. return PTR_ERR(lun);
  996. core_tpg_post_dellun(tpg, lun);
  997. pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
  998. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  999. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
  1000. tpg->se_tpg_tfo->get_fabric_name());
  1001. return 0;
  1002. }
  1003. struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
  1004. {
  1005. struct se_lun *lun;
  1006. spin_lock(&tpg->tpg_lun_lock);
  1007. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1008. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
  1009. "_PER_TPG-1: %u for Target Portal Group: %hu\n",
  1010. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1011. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1012. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1013. spin_unlock(&tpg->tpg_lun_lock);
  1014. return NULL;
  1015. }
  1016. lun = tpg->tpg_lun_list[unpacked_lun];
  1017. if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
  1018. pr_err("%s Logical Unit Number: %u is not free on"
  1019. " Target Portal Group: %hu, ignoring request.\n",
  1020. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1021. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1022. spin_unlock(&tpg->tpg_lun_lock);
  1023. return NULL;
  1024. }
  1025. spin_unlock(&tpg->tpg_lun_lock);
  1026. return lun;
  1027. }
  1028. /* core_dev_get_lun():
  1029. *
  1030. *
  1031. */
  1032. static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
  1033. {
  1034. struct se_lun *lun;
  1035. spin_lock(&tpg->tpg_lun_lock);
  1036. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1037. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
  1038. "_TPG-1: %u for Target Portal Group: %hu\n",
  1039. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1040. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1041. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1042. spin_unlock(&tpg->tpg_lun_lock);
  1043. return NULL;
  1044. }
  1045. lun = tpg->tpg_lun_list[unpacked_lun];
  1046. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
  1047. pr_err("%s Logical Unit Number: %u is not active on"
  1048. " Target Portal Group: %hu, ignoring request.\n",
  1049. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1050. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1051. spin_unlock(&tpg->tpg_lun_lock);
  1052. return NULL;
  1053. }
  1054. spin_unlock(&tpg->tpg_lun_lock);
  1055. return lun;
  1056. }
  1057. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  1058. struct se_portal_group *tpg,
  1059. struct se_node_acl *nacl,
  1060. u32 mapped_lun,
  1061. int *ret)
  1062. {
  1063. struct se_lun_acl *lacl;
  1064. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  1065. pr_err("%s InitiatorName exceeds maximum size.\n",
  1066. tpg->se_tpg_tfo->get_fabric_name());
  1067. *ret = -EOVERFLOW;
  1068. return NULL;
  1069. }
  1070. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  1071. if (!lacl) {
  1072. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  1073. *ret = -ENOMEM;
  1074. return NULL;
  1075. }
  1076. INIT_LIST_HEAD(&lacl->lacl_list);
  1077. lacl->mapped_lun = mapped_lun;
  1078. lacl->se_lun_nacl = nacl;
  1079. snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
  1080. nacl->initiatorname);
  1081. return lacl;
  1082. }
  1083. int core_dev_add_initiator_node_lun_acl(
  1084. struct se_portal_group *tpg,
  1085. struct se_lun_acl *lacl,
  1086. u32 unpacked_lun,
  1087. u32 lun_access)
  1088. {
  1089. struct se_lun *lun;
  1090. struct se_node_acl *nacl;
  1091. lun = core_dev_get_lun(tpg, unpacked_lun);
  1092. if (!lun) {
  1093. pr_err("%s Logical Unit Number: %u is not active on"
  1094. " Target Portal Group: %hu, ignoring request.\n",
  1095. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1096. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1097. return -EINVAL;
  1098. }
  1099. nacl = lacl->se_lun_nacl;
  1100. if (!nacl)
  1101. return -EINVAL;
  1102. if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
  1103. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
  1104. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  1105. lacl->se_lun = lun;
  1106. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  1107. lun_access, nacl, tpg) < 0)
  1108. return -EINVAL;
  1109. spin_lock(&lun->lun_acl_lock);
  1110. list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
  1111. atomic_inc(&lun->lun_acl_count);
  1112. smp_mb__after_atomic_inc();
  1113. spin_unlock(&lun->lun_acl_lock);
  1114. pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
  1115. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1116. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
  1117. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
  1118. lacl->initiatorname);
  1119. /*
  1120. * Check to see if there are any existing persistent reservation APTPL
  1121. * pre-registrations that need to be enabled for this LUN ACL..
  1122. */
  1123. core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
  1124. return 0;
  1125. }
  1126. /* core_dev_del_initiator_node_lun_acl():
  1127. *
  1128. *
  1129. */
  1130. int core_dev_del_initiator_node_lun_acl(
  1131. struct se_portal_group *tpg,
  1132. struct se_lun *lun,
  1133. struct se_lun_acl *lacl)
  1134. {
  1135. struct se_node_acl *nacl;
  1136. nacl = lacl->se_lun_nacl;
  1137. if (!nacl)
  1138. return -EINVAL;
  1139. spin_lock(&lun->lun_acl_lock);
  1140. list_del(&lacl->lacl_list);
  1141. atomic_dec(&lun->lun_acl_count);
  1142. smp_mb__after_atomic_dec();
  1143. spin_unlock(&lun->lun_acl_lock);
  1144. core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
  1145. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  1146. lacl->se_lun = NULL;
  1147. pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
  1148. " InitiatorNode: %s Mapped LUN: %u\n",
  1149. tpg->se_tpg_tfo->get_fabric_name(),
  1150. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1151. lacl->initiatorname, lacl->mapped_lun);
  1152. return 0;
  1153. }
  1154. void core_dev_free_initiator_node_lun_acl(
  1155. struct se_portal_group *tpg,
  1156. struct se_lun_acl *lacl)
  1157. {
  1158. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  1159. " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1160. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1161. tpg->se_tpg_tfo->get_fabric_name(),
  1162. lacl->initiatorname, lacl->mapped_lun);
  1163. kfree(lacl);
  1164. }
  1165. static void scsi_dump_inquiry(struct se_device *dev)
  1166. {
  1167. struct t10_wwn *wwn = &dev->t10_wwn;
  1168. char buf[17];
  1169. int i, device_type;
  1170. /*
  1171. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1172. */
  1173. for (i = 0; i < 8; i++)
  1174. if (wwn->vendor[i] >= 0x20)
  1175. buf[i] = wwn->vendor[i];
  1176. else
  1177. buf[i] = ' ';
  1178. buf[i] = '\0';
  1179. pr_debug(" Vendor: %s\n", buf);
  1180. for (i = 0; i < 16; i++)
  1181. if (wwn->model[i] >= 0x20)
  1182. buf[i] = wwn->model[i];
  1183. else
  1184. buf[i] = ' ';
  1185. buf[i] = '\0';
  1186. pr_debug(" Model: %s\n", buf);
  1187. for (i = 0; i < 4; i++)
  1188. if (wwn->revision[i] >= 0x20)
  1189. buf[i] = wwn->revision[i];
  1190. else
  1191. buf[i] = ' ';
  1192. buf[i] = '\0';
  1193. pr_debug(" Revision: %s\n", buf);
  1194. device_type = dev->transport->get_device_type(dev);
  1195. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1196. }
  1197. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  1198. {
  1199. struct se_device *dev;
  1200. dev = hba->transport->alloc_device(hba, name);
  1201. if (!dev)
  1202. return NULL;
  1203. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  1204. dev->se_hba = hba;
  1205. dev->transport = hba->transport;
  1206. INIT_LIST_HEAD(&dev->dev_list);
  1207. INIT_LIST_HEAD(&dev->dev_sep_list);
  1208. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1209. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1210. INIT_LIST_HEAD(&dev->state_list);
  1211. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1212. spin_lock_init(&dev->stats_lock);
  1213. spin_lock_init(&dev->execute_task_lock);
  1214. spin_lock_init(&dev->delayed_cmd_lock);
  1215. spin_lock_init(&dev->dev_reservation_lock);
  1216. spin_lock_init(&dev->se_port_lock);
  1217. spin_lock_init(&dev->se_tmr_lock);
  1218. spin_lock_init(&dev->qf_cmd_lock);
  1219. atomic_set(&dev->dev_ordered_id, 0);
  1220. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  1221. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  1222. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  1223. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  1224. spin_lock_init(&dev->t10_pr.registration_lock);
  1225. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  1226. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  1227. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  1228. dev->t10_wwn.t10_dev = dev;
  1229. dev->t10_alua.t10_dev = dev;
  1230. dev->dev_attrib.da_dev = dev;
  1231. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  1232. dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
  1233. dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
  1234. dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
  1235. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  1236. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  1237. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  1238. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  1239. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  1240. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  1241. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  1242. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  1243. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  1244. dev->dev_attrib.max_unmap_block_desc_count =
  1245. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  1246. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  1247. dev->dev_attrib.unmap_granularity_alignment =
  1248. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  1249. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  1250. dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
  1251. dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
  1252. return dev;
  1253. }
  1254. int target_configure_device(struct se_device *dev)
  1255. {
  1256. struct se_hba *hba = dev->se_hba;
  1257. int ret;
  1258. if (dev->dev_flags & DF_CONFIGURED) {
  1259. pr_err("se_dev->se_dev_ptr already set for storage"
  1260. " object\n");
  1261. return -EEXIST;
  1262. }
  1263. ret = dev->transport->configure_device(dev);
  1264. if (ret)
  1265. goto out;
  1266. dev->dev_flags |= DF_CONFIGURED;
  1267. /*
  1268. * XXX: there is not much point to have two different values here..
  1269. */
  1270. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  1271. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  1272. /*
  1273. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  1274. */
  1275. dev->dev_attrib.hw_max_sectors =
  1276. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  1277. dev->dev_attrib.hw_block_size);
  1278. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1279. dev->creation_time = get_jiffies_64();
  1280. ret = core_setup_alua(dev);
  1281. if (ret)
  1282. goto out;
  1283. /*
  1284. * Startup the struct se_device processing thread
  1285. */
  1286. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  1287. dev->transport->name);
  1288. if (!dev->tmr_wq) {
  1289. pr_err("Unable to create tmr workqueue for %s\n",
  1290. dev->transport->name);
  1291. ret = -ENOMEM;
  1292. goto out_free_alua;
  1293. }
  1294. /*
  1295. * Setup work_queue for QUEUE_FULL
  1296. */
  1297. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1298. /*
  1299. * Preload the initial INQUIRY const values if we are doing
  1300. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1301. * passthrough because this is being provided by the backend LLD.
  1302. */
  1303. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1304. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1305. strncpy(&dev->t10_wwn.model[0],
  1306. dev->transport->inquiry_prod, 16);
  1307. strncpy(&dev->t10_wwn.revision[0],
  1308. dev->transport->inquiry_rev, 4);
  1309. }
  1310. scsi_dump_inquiry(dev);
  1311. spin_lock(&hba->device_lock);
  1312. hba->dev_count++;
  1313. spin_unlock(&hba->device_lock);
  1314. return 0;
  1315. out_free_alua:
  1316. core_alua_free_lu_gp_mem(dev);
  1317. out:
  1318. se_release_vpd_for_dev(dev);
  1319. return ret;
  1320. }
  1321. void target_free_device(struct se_device *dev)
  1322. {
  1323. struct se_hba *hba = dev->se_hba;
  1324. WARN_ON(!list_empty(&dev->dev_sep_list));
  1325. if (dev->dev_flags & DF_CONFIGURED) {
  1326. destroy_workqueue(dev->tmr_wq);
  1327. spin_lock(&hba->device_lock);
  1328. hba->dev_count--;
  1329. spin_unlock(&hba->device_lock);
  1330. }
  1331. core_alua_free_lu_gp_mem(dev);
  1332. core_scsi3_free_all_registrations(dev);
  1333. se_release_vpd_for_dev(dev);
  1334. dev->transport->free_device(dev);
  1335. }
  1336. int core_dev_setup_virtual_lun0(void)
  1337. {
  1338. struct se_hba *hba;
  1339. struct se_device *dev;
  1340. char buf[16];
  1341. int ret;
  1342. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  1343. if (IS_ERR(hba))
  1344. return PTR_ERR(hba);
  1345. dev = target_alloc_device(hba, "virt_lun0");
  1346. if (!dev) {
  1347. ret = -ENOMEM;
  1348. goto out_free_hba;
  1349. }
  1350. memset(buf, 0, 16);
  1351. sprintf(buf, "rd_pages=8");
  1352. hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
  1353. ret = target_configure_device(dev);
  1354. if (ret)
  1355. goto out_free_se_dev;
  1356. lun0_hba = hba;
  1357. g_lun0_dev = dev;
  1358. return 0;
  1359. out_free_se_dev:
  1360. target_free_device(dev);
  1361. out_free_hba:
  1362. core_delete_hba(hba);
  1363. return ret;
  1364. }
  1365. void core_dev_release_virtual_lun0(void)
  1366. {
  1367. struct se_hba *hba = lun0_hba;
  1368. if (!hba)
  1369. return;
  1370. if (g_lun0_dev)
  1371. target_free_device(g_lun0_dev);
  1372. core_delete_hba(hba);
  1373. }