target_core_device.c 42 KB

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