target_core_device.c 42 KB

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