target_core_device.c 43 KB

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