target_core_alua.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /*******************************************************************************
  2. * Filename: target_core_alua.c
  3. *
  4. * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
  5. *
  6. * Copyright (c) 2009-2010 Rising Tide Systems
  7. * Copyright (c) 2009-2010 Linux-iSCSI.org
  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/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/configfs.h>
  29. #include <linux/export.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <asm/unaligned.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_backend.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_configfs.h>
  37. #include "target_core_internal.h"
  38. #include "target_core_alua.h"
  39. #include "target_core_ua.h"
  40. static int core_alua_check_transition(int state, int *primary);
  41. static int core_alua_set_tg_pt_secondary_state(
  42. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  43. struct se_port *port, int explict, int offline);
  44. static u16 alua_lu_gps_counter;
  45. static u32 alua_lu_gps_count;
  46. static DEFINE_SPINLOCK(lu_gps_lock);
  47. static LIST_HEAD(lu_gps_list);
  48. struct t10_alua_lu_gp *default_lu_gp;
  49. /*
  50. * REPORT_TARGET_PORT_GROUPS
  51. *
  52. * See spc4r17 section 6.27
  53. */
  54. int target_emulate_report_target_port_groups(struct se_cmd *cmd)
  55. {
  56. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  57. struct se_port *port;
  58. struct t10_alua_tg_pt_gp *tg_pt_gp;
  59. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  60. unsigned char *buf;
  61. u32 rd_len = 0, off;
  62. int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
  63. /*
  64. * Skip over RESERVED area to first Target port group descriptor
  65. * depending on the PARAMETER DATA FORMAT type..
  66. */
  67. if (ext_hdr != 0)
  68. off = 8;
  69. else
  70. off = 4;
  71. if (cmd->data_length < off) {
  72. pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
  73. " small for %s header\n", cmd->data_length,
  74. (ext_hdr) ? "extended" : "normal");
  75. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  76. return -EINVAL;
  77. }
  78. buf = transport_kmap_data_sg(cmd);
  79. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  80. list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
  81. tg_pt_gp_list) {
  82. /*
  83. * Check if the Target port group and Target port descriptor list
  84. * based on tg_pt_gp_members count will fit into the response payload.
  85. * Otherwise, bump rd_len to let the initiator know we have exceeded
  86. * the allocation length and the response is truncated.
  87. */
  88. if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
  89. cmd->data_length) {
  90. rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
  91. continue;
  92. }
  93. /*
  94. * PREF: Preferred target port bit, determine if this
  95. * bit should be set for port group.
  96. */
  97. if (tg_pt_gp->tg_pt_gp_pref)
  98. buf[off] = 0x80;
  99. /*
  100. * Set the ASYMMETRIC ACCESS State
  101. */
  102. buf[off++] |= (atomic_read(
  103. &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
  104. /*
  105. * Set supported ASYMMETRIC ACCESS State bits
  106. */
  107. buf[off] = 0x80; /* T_SUP */
  108. buf[off] |= 0x40; /* O_SUP */
  109. buf[off] |= 0x8; /* U_SUP */
  110. buf[off] |= 0x4; /* S_SUP */
  111. buf[off] |= 0x2; /* AN_SUP */
  112. buf[off++] |= 0x1; /* AO_SUP */
  113. /*
  114. * TARGET PORT GROUP
  115. */
  116. buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
  117. buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
  118. off++; /* Skip over Reserved */
  119. /*
  120. * STATUS CODE
  121. */
  122. buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
  123. /*
  124. * Vendor Specific field
  125. */
  126. buf[off++] = 0x00;
  127. /*
  128. * TARGET PORT COUNT
  129. */
  130. buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
  131. rd_len += 8;
  132. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  133. list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  134. tg_pt_gp_mem_list) {
  135. port = tg_pt_gp_mem->tg_pt;
  136. /*
  137. * Start Target Port descriptor format
  138. *
  139. * See spc4r17 section 6.2.7 Table 247
  140. */
  141. off += 2; /* Skip over Obsolete */
  142. /*
  143. * Set RELATIVE TARGET PORT IDENTIFIER
  144. */
  145. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  146. buf[off++] = (port->sep_rtpi & 0xff);
  147. rd_len += 4;
  148. }
  149. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  150. }
  151. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  152. /*
  153. * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
  154. */
  155. put_unaligned_be32(rd_len, &buf[0]);
  156. /*
  157. * Fill in the Extended header parameter data format if requested
  158. */
  159. if (ext_hdr != 0) {
  160. buf[4] = 0x10;
  161. /*
  162. * Set the implict transition time (in seconds) for the application
  163. * client to use as a base for it's transition timeout value.
  164. *
  165. * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
  166. * this CDB was received upon to determine this value individually
  167. * for ALUA target port group.
  168. */
  169. port = cmd->se_lun->lun_sep;
  170. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  171. if (tg_pt_gp_mem) {
  172. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  173. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  174. if (tg_pt_gp)
  175. buf[5] = tg_pt_gp->tg_pt_gp_implict_trans_secs;
  176. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  177. }
  178. }
  179. transport_kunmap_data_sg(cmd);
  180. target_complete_cmd(cmd, GOOD);
  181. return 0;
  182. }
  183. /*
  184. * SET_TARGET_PORT_GROUPS for explict ALUA operation.
  185. *
  186. * See spc4r17 section 6.35
  187. */
  188. int target_emulate_set_target_port_groups(struct se_cmd *cmd)
  189. {
  190. struct se_device *dev = cmd->se_dev;
  191. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  192. struct se_port *port, *l_port = cmd->se_lun->lun_sep;
  193. struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
  194. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
  195. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
  196. unsigned char *buf;
  197. unsigned char *ptr;
  198. u32 len = 4; /* Skip over RESERVED area in header */
  199. int alua_access_state, primary = 0, rc;
  200. u16 tg_pt_id, rtpi;
  201. if (!l_port) {
  202. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  203. return -EINVAL;
  204. }
  205. buf = transport_kmap_data_sg(cmd);
  206. /*
  207. * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
  208. * for the local tg_pt_gp.
  209. */
  210. l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
  211. if (!l_tg_pt_gp_mem) {
  212. pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
  213. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  214. rc = -EINVAL;
  215. goto out;
  216. }
  217. spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  218. l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
  219. if (!l_tg_pt_gp) {
  220. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  221. pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
  222. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  223. rc = -EINVAL;
  224. goto out;
  225. }
  226. rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
  227. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  228. if (!rc) {
  229. pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
  230. " while TPGS_EXPLICT_ALUA is disabled\n");
  231. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  232. rc = -EINVAL;
  233. goto out;
  234. }
  235. ptr = &buf[4]; /* Skip over RESERVED area in header */
  236. while (len < cmd->data_length) {
  237. alua_access_state = (ptr[0] & 0x0f);
  238. /*
  239. * Check the received ALUA access state, and determine if
  240. * the state is a primary or secondary target port asymmetric
  241. * access state.
  242. */
  243. rc = core_alua_check_transition(alua_access_state, &primary);
  244. if (rc != 0) {
  245. /*
  246. * If the SET TARGET PORT GROUPS attempts to establish
  247. * an invalid combination of target port asymmetric
  248. * access states or attempts to establish an
  249. * unsupported target port asymmetric access state,
  250. * then the command shall be terminated with CHECK
  251. * CONDITION status, with the sense key set to ILLEGAL
  252. * REQUEST, and the additional sense code set to INVALID
  253. * FIELD IN PARAMETER LIST.
  254. */
  255. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  256. rc = -EINVAL;
  257. goto out;
  258. }
  259. rc = -1;
  260. /*
  261. * If the ASYMMETRIC ACCESS STATE field (see table 267)
  262. * specifies a primary target port asymmetric access state,
  263. * then the TARGET PORT GROUP OR TARGET PORT field specifies
  264. * a primary target port group for which the primary target
  265. * port asymmetric access state shall be changed. If the
  266. * ASYMMETRIC ACCESS STATE field specifies a secondary target
  267. * port asymmetric access state, then the TARGET PORT GROUP OR
  268. * TARGET PORT field specifies the relative target port
  269. * identifier (see 3.1.120) of the target port for which the
  270. * secondary target port asymmetric access state shall be
  271. * changed.
  272. */
  273. if (primary) {
  274. tg_pt_id = get_unaligned_be16(ptr + 2);
  275. /*
  276. * Locate the matching target port group ID from
  277. * the global tg_pt_gp list
  278. */
  279. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  280. list_for_each_entry(tg_pt_gp,
  281. &su_dev->t10_alua.tg_pt_gps_list,
  282. tg_pt_gp_list) {
  283. if (!tg_pt_gp->tg_pt_gp_valid_id)
  284. continue;
  285. if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
  286. continue;
  287. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  288. smp_mb__after_atomic_inc();
  289. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  290. rc = core_alua_do_port_transition(tg_pt_gp,
  291. dev, l_port, nacl,
  292. alua_access_state, 1);
  293. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  294. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  295. smp_mb__after_atomic_dec();
  296. break;
  297. }
  298. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  299. /*
  300. * If not matching target port group ID can be located
  301. * throw an exception with ASCQ: INVALID_PARAMETER_LIST
  302. */
  303. if (rc != 0) {
  304. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  305. rc = -EINVAL;
  306. goto out;
  307. }
  308. } else {
  309. /*
  310. * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
  311. * the Target Port in question for the the incoming
  312. * SET_TARGET_PORT_GROUPS op.
  313. */
  314. rtpi = get_unaligned_be16(ptr + 2);
  315. /*
  316. * Locate the matching relative target port identifer
  317. * for the struct se_device storage object.
  318. */
  319. spin_lock(&dev->se_port_lock);
  320. list_for_each_entry(port, &dev->dev_sep_list,
  321. sep_list) {
  322. if (port->sep_rtpi != rtpi)
  323. continue;
  324. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  325. spin_unlock(&dev->se_port_lock);
  326. rc = core_alua_set_tg_pt_secondary_state(
  327. tg_pt_gp_mem, port, 1, 1);
  328. spin_lock(&dev->se_port_lock);
  329. break;
  330. }
  331. spin_unlock(&dev->se_port_lock);
  332. /*
  333. * If not matching relative target port identifier can
  334. * be located, throw an exception with ASCQ:
  335. * INVALID_PARAMETER_LIST
  336. */
  337. if (rc != 0) {
  338. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  339. rc = -EINVAL;
  340. goto out;
  341. }
  342. }
  343. ptr += 4;
  344. len += 4;
  345. }
  346. out:
  347. transport_kunmap_data_sg(cmd);
  348. if (!rc)
  349. target_complete_cmd(cmd, GOOD);
  350. return rc;
  351. }
  352. static inline int core_alua_state_nonoptimized(
  353. struct se_cmd *cmd,
  354. unsigned char *cdb,
  355. int nonop_delay_msecs,
  356. u8 *alua_ascq)
  357. {
  358. /*
  359. * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
  360. * later to determine if processing of this cmd needs to be
  361. * temporarily delayed for the Active/NonOptimized primary access state.
  362. */
  363. cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
  364. cmd->alua_nonop_delay = nonop_delay_msecs;
  365. return 0;
  366. }
  367. static inline int core_alua_state_standby(
  368. struct se_cmd *cmd,
  369. unsigned char *cdb,
  370. u8 *alua_ascq)
  371. {
  372. /*
  373. * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
  374. * spc4r17 section 5.9.2.4.4
  375. */
  376. switch (cdb[0]) {
  377. case INQUIRY:
  378. case LOG_SELECT:
  379. case LOG_SENSE:
  380. case MODE_SELECT:
  381. case MODE_SENSE:
  382. case REPORT_LUNS:
  383. case RECEIVE_DIAGNOSTIC:
  384. case SEND_DIAGNOSTIC:
  385. case MAINTENANCE_IN:
  386. switch (cdb[1] & 0x1f) {
  387. case MI_REPORT_TARGET_PGS:
  388. return 0;
  389. default:
  390. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  391. return 1;
  392. }
  393. case MAINTENANCE_OUT:
  394. switch (cdb[1]) {
  395. case MO_SET_TARGET_PGS:
  396. return 0;
  397. default:
  398. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  399. return 1;
  400. }
  401. case REQUEST_SENSE:
  402. case PERSISTENT_RESERVE_IN:
  403. case PERSISTENT_RESERVE_OUT:
  404. case READ_BUFFER:
  405. case WRITE_BUFFER:
  406. return 0;
  407. default:
  408. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  409. return 1;
  410. }
  411. return 0;
  412. }
  413. static inline int core_alua_state_unavailable(
  414. struct se_cmd *cmd,
  415. unsigned char *cdb,
  416. u8 *alua_ascq)
  417. {
  418. /*
  419. * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
  420. * spc4r17 section 5.9.2.4.5
  421. */
  422. switch (cdb[0]) {
  423. case INQUIRY:
  424. case REPORT_LUNS:
  425. case MAINTENANCE_IN:
  426. switch (cdb[1] & 0x1f) {
  427. case MI_REPORT_TARGET_PGS:
  428. return 0;
  429. default:
  430. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  431. return 1;
  432. }
  433. case MAINTENANCE_OUT:
  434. switch (cdb[1]) {
  435. case MO_SET_TARGET_PGS:
  436. return 0;
  437. default:
  438. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  439. return 1;
  440. }
  441. case REQUEST_SENSE:
  442. case READ_BUFFER:
  443. case WRITE_BUFFER:
  444. return 0;
  445. default:
  446. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  447. return 1;
  448. }
  449. return 0;
  450. }
  451. static inline int core_alua_state_transition(
  452. struct se_cmd *cmd,
  453. unsigned char *cdb,
  454. u8 *alua_ascq)
  455. {
  456. /*
  457. * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
  458. * spc4r17 section 5.9.2.5
  459. */
  460. switch (cdb[0]) {
  461. case INQUIRY:
  462. case REPORT_LUNS:
  463. case MAINTENANCE_IN:
  464. switch (cdb[1] & 0x1f) {
  465. case MI_REPORT_TARGET_PGS:
  466. return 0;
  467. default:
  468. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  469. return 1;
  470. }
  471. case REQUEST_SENSE:
  472. case READ_BUFFER:
  473. case WRITE_BUFFER:
  474. return 0;
  475. default:
  476. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  477. return 1;
  478. }
  479. return 0;
  480. }
  481. /*
  482. * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
  483. * in transport_cmd_sequencer(). This function is assigned to
  484. * struct t10_alua *->state_check() in core_setup_alua()
  485. */
  486. static int core_alua_state_check_nop(
  487. struct se_cmd *cmd,
  488. unsigned char *cdb,
  489. u8 *alua_ascq)
  490. {
  491. return 0;
  492. }
  493. /*
  494. * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
  495. * This function is assigned to struct t10_alua *->state_check() in
  496. * core_setup_alua()
  497. *
  498. * Also, this function can return three different return codes to
  499. * signal transport_generic_cmd_sequencer()
  500. *
  501. * return 1: Is used to signal LUN not accecsable, and check condition/not ready
  502. * return 0: Used to signal success
  503. * reutrn -1: Used to signal failure, and invalid cdb field
  504. */
  505. static int core_alua_state_check(
  506. struct se_cmd *cmd,
  507. unsigned char *cdb,
  508. u8 *alua_ascq)
  509. {
  510. struct se_lun *lun = cmd->se_lun;
  511. struct se_port *port = lun->lun_sep;
  512. struct t10_alua_tg_pt_gp *tg_pt_gp;
  513. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  514. int out_alua_state, nonop_delay_msecs;
  515. if (!port)
  516. return 0;
  517. /*
  518. * First, check for a struct se_port specific secondary ALUA target port
  519. * access state: OFFLINE
  520. */
  521. if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
  522. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  523. pr_debug("ALUA: Got secondary offline status for local"
  524. " target port\n");
  525. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  526. return 1;
  527. }
  528. /*
  529. * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
  530. * ALUA target port group, to obtain current ALUA access state.
  531. * Otherwise look for the underlying struct se_device association with
  532. * a ALUA logical unit group.
  533. */
  534. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  535. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  536. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  537. out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  538. nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
  539. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  540. /*
  541. * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
  542. * statement so the compiler knows explicitly to check this case first.
  543. * For the Optimized ALUA access state case, we want to process the
  544. * incoming fabric cmd ASAP..
  545. */
  546. if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
  547. return 0;
  548. switch (out_alua_state) {
  549. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  550. return core_alua_state_nonoptimized(cmd, cdb,
  551. nonop_delay_msecs, alua_ascq);
  552. case ALUA_ACCESS_STATE_STANDBY:
  553. return core_alua_state_standby(cmd, cdb, alua_ascq);
  554. case ALUA_ACCESS_STATE_UNAVAILABLE:
  555. return core_alua_state_unavailable(cmd, cdb, alua_ascq);
  556. case ALUA_ACCESS_STATE_TRANSITION:
  557. return core_alua_state_transition(cmd, cdb, alua_ascq);
  558. /*
  559. * OFFLINE is a secondary ALUA target port group access state, that is
  560. * handled above with struct se_port->sep_tg_pt_secondary_offline=1
  561. */
  562. case ALUA_ACCESS_STATE_OFFLINE:
  563. default:
  564. pr_err("Unknown ALUA access state: 0x%02x\n",
  565. out_alua_state);
  566. return -EINVAL;
  567. }
  568. return 0;
  569. }
  570. /*
  571. * Check implict and explict ALUA state change request.
  572. */
  573. static int core_alua_check_transition(int state, int *primary)
  574. {
  575. switch (state) {
  576. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  577. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  578. case ALUA_ACCESS_STATE_STANDBY:
  579. case ALUA_ACCESS_STATE_UNAVAILABLE:
  580. /*
  581. * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
  582. * defined as primary target port asymmetric access states.
  583. */
  584. *primary = 1;
  585. break;
  586. case ALUA_ACCESS_STATE_OFFLINE:
  587. /*
  588. * OFFLINE state is defined as a secondary target port
  589. * asymmetric access state.
  590. */
  591. *primary = 0;
  592. break;
  593. default:
  594. pr_err("Unknown ALUA access state: 0x%02x\n", state);
  595. return -EINVAL;
  596. }
  597. return 0;
  598. }
  599. static char *core_alua_dump_state(int state)
  600. {
  601. switch (state) {
  602. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  603. return "Active/Optimized";
  604. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  605. return "Active/NonOptimized";
  606. case ALUA_ACCESS_STATE_STANDBY:
  607. return "Standby";
  608. case ALUA_ACCESS_STATE_UNAVAILABLE:
  609. return "Unavailable";
  610. case ALUA_ACCESS_STATE_OFFLINE:
  611. return "Offline";
  612. default:
  613. return "Unknown";
  614. }
  615. return NULL;
  616. }
  617. char *core_alua_dump_status(int status)
  618. {
  619. switch (status) {
  620. case ALUA_STATUS_NONE:
  621. return "None";
  622. case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
  623. return "Altered by Explict STPG";
  624. case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
  625. return "Altered by Implict ALUA";
  626. default:
  627. return "Unknown";
  628. }
  629. return NULL;
  630. }
  631. /*
  632. * Used by fabric modules to determine when we need to delay processing
  633. * for the Active/NonOptimized paths..
  634. */
  635. int core_alua_check_nonop_delay(
  636. struct se_cmd *cmd)
  637. {
  638. if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
  639. return 0;
  640. if (in_interrupt())
  641. return 0;
  642. /*
  643. * The ALUA Active/NonOptimized access state delay can be disabled
  644. * in via configfs with a value of zero
  645. */
  646. if (!cmd->alua_nonop_delay)
  647. return 0;
  648. /*
  649. * struct se_cmd->alua_nonop_delay gets set by a target port group
  650. * defined interval in core_alua_state_nonoptimized()
  651. */
  652. msleep_interruptible(cmd->alua_nonop_delay);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL(core_alua_check_nonop_delay);
  656. /*
  657. * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
  658. *
  659. */
  660. static int core_alua_write_tpg_metadata(
  661. const char *path,
  662. unsigned char *md_buf,
  663. u32 md_buf_len)
  664. {
  665. mm_segment_t old_fs;
  666. struct file *file;
  667. struct iovec iov[1];
  668. int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
  669. memset(iov, 0, sizeof(struct iovec));
  670. file = filp_open(path, flags, 0600);
  671. if (IS_ERR(file) || !file || !file->f_dentry) {
  672. pr_err("filp_open(%s) for ALUA metadata failed\n",
  673. path);
  674. return -ENODEV;
  675. }
  676. iov[0].iov_base = &md_buf[0];
  677. iov[0].iov_len = md_buf_len;
  678. old_fs = get_fs();
  679. set_fs(get_ds());
  680. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  681. set_fs(old_fs);
  682. if (ret < 0) {
  683. pr_err("Error writing ALUA metadata file: %s\n", path);
  684. filp_close(file, NULL);
  685. return -EIO;
  686. }
  687. filp_close(file, NULL);
  688. return 0;
  689. }
  690. /*
  691. * Called with tg_pt_gp->tg_pt_gp_md_mutex held
  692. */
  693. static int core_alua_update_tpg_primary_metadata(
  694. struct t10_alua_tg_pt_gp *tg_pt_gp,
  695. int primary_state,
  696. unsigned char *md_buf)
  697. {
  698. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  699. struct t10_wwn *wwn = &su_dev->t10_wwn;
  700. char path[ALUA_METADATA_PATH_LEN];
  701. int len;
  702. memset(path, 0, ALUA_METADATA_PATH_LEN);
  703. len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
  704. "tg_pt_gp_id=%hu\n"
  705. "alua_access_state=0x%02x\n"
  706. "alua_access_status=0x%02x\n",
  707. tg_pt_gp->tg_pt_gp_id, primary_state,
  708. tg_pt_gp->tg_pt_gp_alua_access_status);
  709. snprintf(path, ALUA_METADATA_PATH_LEN,
  710. "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
  711. config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
  712. return core_alua_write_tpg_metadata(path, md_buf, len);
  713. }
  714. static int core_alua_do_transition_tg_pt(
  715. struct t10_alua_tg_pt_gp *tg_pt_gp,
  716. struct se_port *l_port,
  717. struct se_node_acl *nacl,
  718. unsigned char *md_buf,
  719. int new_state,
  720. int explict)
  721. {
  722. struct se_dev_entry *se_deve;
  723. struct se_lun_acl *lacl;
  724. struct se_port *port;
  725. struct t10_alua_tg_pt_gp_member *mem;
  726. int old_state = 0;
  727. /*
  728. * Save the old primary ALUA access state, and set the current state
  729. * to ALUA_ACCESS_STATE_TRANSITION.
  730. */
  731. old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  732. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  733. ALUA_ACCESS_STATE_TRANSITION);
  734. tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
  735. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  736. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  737. /*
  738. * Check for the optional ALUA primary state transition delay
  739. */
  740. if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
  741. msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  742. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  743. list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
  744. tg_pt_gp_mem_list) {
  745. port = mem->tg_pt;
  746. /*
  747. * After an implicit target port asymmetric access state
  748. * change, a device server shall establish a unit attention
  749. * condition for the initiator port associated with every I_T
  750. * nexus with the additional sense code set to ASYMMETRIC
  751. * ACCESS STATE CHAGED.
  752. *
  753. * After an explicit target port asymmetric access state
  754. * change, a device server shall establish a unit attention
  755. * condition with the additional sense code set to ASYMMETRIC
  756. * ACCESS STATE CHANGED for the initiator port associated with
  757. * every I_T nexus other than the I_T nexus on which the SET
  758. * TARGET PORT GROUPS command
  759. */
  760. atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
  761. smp_mb__after_atomic_inc();
  762. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  763. spin_lock_bh(&port->sep_alua_lock);
  764. list_for_each_entry(se_deve, &port->sep_alua_list,
  765. alua_port_list) {
  766. lacl = se_deve->se_lun_acl;
  767. /*
  768. * se_deve->se_lun_acl pointer may be NULL for a
  769. * entry created without explict Node+MappedLUN ACLs
  770. */
  771. if (!lacl)
  772. continue;
  773. if (explict &&
  774. (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
  775. (l_port != NULL) && (l_port == port))
  776. continue;
  777. core_scsi3_ua_allocate(lacl->se_lun_nacl,
  778. se_deve->mapped_lun, 0x2A,
  779. ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
  780. }
  781. spin_unlock_bh(&port->sep_alua_lock);
  782. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  783. atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
  784. smp_mb__after_atomic_dec();
  785. }
  786. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  787. /*
  788. * Update the ALUA metadata buf that has been allocated in
  789. * core_alua_do_port_transition(), this metadata will be written
  790. * to struct file.
  791. *
  792. * Note that there is the case where we do not want to update the
  793. * metadata when the saved metadata is being parsed in userspace
  794. * when setting the existing port access state and access status.
  795. *
  796. * Also note that the failure to write out the ALUA metadata to
  797. * struct file does NOT affect the actual ALUA transition.
  798. */
  799. if (tg_pt_gp->tg_pt_gp_write_metadata) {
  800. mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
  801. core_alua_update_tpg_primary_metadata(tg_pt_gp,
  802. new_state, md_buf);
  803. mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
  804. }
  805. /*
  806. * Set the current primary ALUA access state to the requested new state
  807. */
  808. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
  809. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  810. " from primary access state %s to %s\n", (explict) ? "explict" :
  811. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  812. tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
  813. core_alua_dump_state(new_state));
  814. return 0;
  815. }
  816. int core_alua_do_port_transition(
  817. struct t10_alua_tg_pt_gp *l_tg_pt_gp,
  818. struct se_device *l_dev,
  819. struct se_port *l_port,
  820. struct se_node_acl *l_nacl,
  821. int new_state,
  822. int explict)
  823. {
  824. struct se_device *dev;
  825. struct se_port *port;
  826. struct se_subsystem_dev *su_dev;
  827. struct se_node_acl *nacl;
  828. struct t10_alua_lu_gp *lu_gp;
  829. struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
  830. struct t10_alua_tg_pt_gp *tg_pt_gp;
  831. unsigned char *md_buf;
  832. int primary;
  833. if (core_alua_check_transition(new_state, &primary) != 0)
  834. return -EINVAL;
  835. md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
  836. if (!md_buf) {
  837. pr_err("Unable to allocate buf for ALUA metadata\n");
  838. return -ENOMEM;
  839. }
  840. local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
  841. spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
  842. lu_gp = local_lu_gp_mem->lu_gp;
  843. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  844. smp_mb__after_atomic_inc();
  845. spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
  846. /*
  847. * For storage objects that are members of the 'default_lu_gp',
  848. * we only do transition on the passed *l_tp_pt_gp, and not
  849. * on all of the matching target port groups IDs in default_lu_gp.
  850. */
  851. if (!lu_gp->lu_gp_id) {
  852. /*
  853. * core_alua_do_transition_tg_pt() will always return
  854. * success.
  855. */
  856. core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
  857. md_buf, new_state, explict);
  858. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  859. smp_mb__after_atomic_dec();
  860. kfree(md_buf);
  861. return 0;
  862. }
  863. /*
  864. * For all other LU groups aside from 'default_lu_gp', walk all of
  865. * the associated storage objects looking for a matching target port
  866. * group ID from the local target port group.
  867. */
  868. spin_lock(&lu_gp->lu_gp_lock);
  869. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
  870. lu_gp_mem_list) {
  871. dev = lu_gp_mem->lu_gp_mem_dev;
  872. su_dev = dev->se_sub_dev;
  873. atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
  874. smp_mb__after_atomic_inc();
  875. spin_unlock(&lu_gp->lu_gp_lock);
  876. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  877. list_for_each_entry(tg_pt_gp,
  878. &su_dev->t10_alua.tg_pt_gps_list,
  879. tg_pt_gp_list) {
  880. if (!tg_pt_gp->tg_pt_gp_valid_id)
  881. continue;
  882. /*
  883. * If the target behavior port asymmetric access state
  884. * is changed for any target port group accessiable via
  885. * a logical unit within a LU group, the target port
  886. * behavior group asymmetric access states for the same
  887. * target port group accessible via other logical units
  888. * in that LU group will also change.
  889. */
  890. if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
  891. continue;
  892. if (l_tg_pt_gp == tg_pt_gp) {
  893. port = l_port;
  894. nacl = l_nacl;
  895. } else {
  896. port = NULL;
  897. nacl = NULL;
  898. }
  899. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  900. smp_mb__after_atomic_inc();
  901. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  902. /*
  903. * core_alua_do_transition_tg_pt() will always return
  904. * success.
  905. */
  906. core_alua_do_transition_tg_pt(tg_pt_gp, port,
  907. nacl, md_buf, new_state, explict);
  908. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  909. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  910. smp_mb__after_atomic_dec();
  911. }
  912. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  913. spin_lock(&lu_gp->lu_gp_lock);
  914. atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
  915. smp_mb__after_atomic_dec();
  916. }
  917. spin_unlock(&lu_gp->lu_gp_lock);
  918. pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
  919. " Group IDs: %hu %s transition to primary state: %s\n",
  920. config_item_name(&lu_gp->lu_gp_group.cg_item),
  921. l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
  922. core_alua_dump_state(new_state));
  923. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  924. smp_mb__after_atomic_dec();
  925. kfree(md_buf);
  926. return 0;
  927. }
  928. /*
  929. * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
  930. */
  931. static int core_alua_update_tpg_secondary_metadata(
  932. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  933. struct se_port *port,
  934. unsigned char *md_buf,
  935. u32 md_buf_len)
  936. {
  937. struct se_portal_group *se_tpg = port->sep_tpg;
  938. char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
  939. int len;
  940. memset(path, 0, ALUA_METADATA_PATH_LEN);
  941. memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
  942. len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
  943. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
  944. if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
  945. snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
  946. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  947. len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
  948. "alua_tg_pt_status=0x%02x\n",
  949. atomic_read(&port->sep_tg_pt_secondary_offline),
  950. port->sep_tg_pt_secondary_stat);
  951. snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
  952. se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
  953. port->sep_lun->unpacked_lun);
  954. return core_alua_write_tpg_metadata(path, md_buf, len);
  955. }
  956. static int core_alua_set_tg_pt_secondary_state(
  957. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  958. struct se_port *port,
  959. int explict,
  960. int offline)
  961. {
  962. struct t10_alua_tg_pt_gp *tg_pt_gp;
  963. unsigned char *md_buf;
  964. u32 md_buf_len;
  965. int trans_delay_msecs;
  966. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  967. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  968. if (!tg_pt_gp) {
  969. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  970. pr_err("Unable to complete secondary state"
  971. " transition\n");
  972. return -EINVAL;
  973. }
  974. trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
  975. /*
  976. * Set the secondary ALUA target port access state to OFFLINE
  977. * or release the previously secondary state for struct se_port
  978. */
  979. if (offline)
  980. atomic_set(&port->sep_tg_pt_secondary_offline, 1);
  981. else
  982. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  983. md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
  984. port->sep_tg_pt_secondary_stat = (explict) ?
  985. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  986. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  987. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  988. " to secondary access state: %s\n", (explict) ? "explict" :
  989. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  990. tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
  991. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  992. /*
  993. * Do the optional transition delay after we set the secondary
  994. * ALUA access state.
  995. */
  996. if (trans_delay_msecs != 0)
  997. msleep_interruptible(trans_delay_msecs);
  998. /*
  999. * See if we need to update the ALUA fabric port metadata for
  1000. * secondary state and status
  1001. */
  1002. if (port->sep_tg_pt_secondary_write_md) {
  1003. md_buf = kzalloc(md_buf_len, GFP_KERNEL);
  1004. if (!md_buf) {
  1005. pr_err("Unable to allocate md_buf for"
  1006. " secondary ALUA access metadata\n");
  1007. return -ENOMEM;
  1008. }
  1009. mutex_lock(&port->sep_tg_pt_md_mutex);
  1010. core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
  1011. md_buf, md_buf_len);
  1012. mutex_unlock(&port->sep_tg_pt_md_mutex);
  1013. kfree(md_buf);
  1014. }
  1015. return 0;
  1016. }
  1017. struct t10_alua_lu_gp *
  1018. core_alua_allocate_lu_gp(const char *name, int def_group)
  1019. {
  1020. struct t10_alua_lu_gp *lu_gp;
  1021. lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
  1022. if (!lu_gp) {
  1023. pr_err("Unable to allocate struct t10_alua_lu_gp\n");
  1024. return ERR_PTR(-ENOMEM);
  1025. }
  1026. INIT_LIST_HEAD(&lu_gp->lu_gp_node);
  1027. INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
  1028. spin_lock_init(&lu_gp->lu_gp_lock);
  1029. atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
  1030. if (def_group) {
  1031. lu_gp->lu_gp_id = alua_lu_gps_counter++;
  1032. lu_gp->lu_gp_valid_id = 1;
  1033. alua_lu_gps_count++;
  1034. }
  1035. return lu_gp;
  1036. }
  1037. int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
  1038. {
  1039. struct t10_alua_lu_gp *lu_gp_tmp;
  1040. u16 lu_gp_id_tmp;
  1041. /*
  1042. * The lu_gp->lu_gp_id may only be set once..
  1043. */
  1044. if (lu_gp->lu_gp_valid_id) {
  1045. pr_warn("ALUA LU Group already has a valid ID,"
  1046. " ignoring request\n");
  1047. return -EINVAL;
  1048. }
  1049. spin_lock(&lu_gps_lock);
  1050. if (alua_lu_gps_count == 0x0000ffff) {
  1051. pr_err("Maximum ALUA alua_lu_gps_count:"
  1052. " 0x0000ffff reached\n");
  1053. spin_unlock(&lu_gps_lock);
  1054. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1055. return -ENOSPC;
  1056. }
  1057. again:
  1058. lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
  1059. alua_lu_gps_counter++;
  1060. list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
  1061. if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
  1062. if (!lu_gp_id)
  1063. goto again;
  1064. pr_warn("ALUA Logical Unit Group ID: %hu"
  1065. " already exists, ignoring request\n",
  1066. lu_gp_id);
  1067. spin_unlock(&lu_gps_lock);
  1068. return -EINVAL;
  1069. }
  1070. }
  1071. lu_gp->lu_gp_id = lu_gp_id_tmp;
  1072. lu_gp->lu_gp_valid_id = 1;
  1073. list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
  1074. alua_lu_gps_count++;
  1075. spin_unlock(&lu_gps_lock);
  1076. return 0;
  1077. }
  1078. static struct t10_alua_lu_gp_member *
  1079. core_alua_allocate_lu_gp_mem(struct se_device *dev)
  1080. {
  1081. struct t10_alua_lu_gp_member *lu_gp_mem;
  1082. lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
  1083. if (!lu_gp_mem) {
  1084. pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
  1085. return ERR_PTR(-ENOMEM);
  1086. }
  1087. INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
  1088. spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
  1089. atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
  1090. lu_gp_mem->lu_gp_mem_dev = dev;
  1091. dev->dev_alua_lu_gp_mem = lu_gp_mem;
  1092. return lu_gp_mem;
  1093. }
  1094. void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
  1095. {
  1096. struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
  1097. /*
  1098. * Once we have reached this point, config_item_put() has
  1099. * already been called from target_core_alua_drop_lu_gp().
  1100. *
  1101. * Here, we remove the *lu_gp from the global list so that
  1102. * no associations can be made while we are releasing
  1103. * struct t10_alua_lu_gp.
  1104. */
  1105. spin_lock(&lu_gps_lock);
  1106. list_del(&lu_gp->lu_gp_node);
  1107. alua_lu_gps_count--;
  1108. spin_unlock(&lu_gps_lock);
  1109. /*
  1110. * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
  1111. * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
  1112. * released with core_alua_put_lu_gp_from_name()
  1113. */
  1114. while (atomic_read(&lu_gp->lu_gp_ref_cnt))
  1115. cpu_relax();
  1116. /*
  1117. * Release reference to struct t10_alua_lu_gp * from all associated
  1118. * struct se_device.
  1119. */
  1120. spin_lock(&lu_gp->lu_gp_lock);
  1121. list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
  1122. &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1123. if (lu_gp_mem->lu_gp_assoc) {
  1124. list_del(&lu_gp_mem->lu_gp_mem_list);
  1125. lu_gp->lu_gp_members--;
  1126. lu_gp_mem->lu_gp_assoc = 0;
  1127. }
  1128. spin_unlock(&lu_gp->lu_gp_lock);
  1129. /*
  1130. *
  1131. * lu_gp_mem is associated with a single
  1132. * struct se_device->dev_alua_lu_gp_mem, and is released when
  1133. * struct se_device is released via core_alua_free_lu_gp_mem().
  1134. *
  1135. * If the passed lu_gp does NOT match the default_lu_gp, assume
  1136. * we want to re-assocate a given lu_gp_mem with default_lu_gp.
  1137. */
  1138. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1139. if (lu_gp != default_lu_gp)
  1140. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1141. default_lu_gp);
  1142. else
  1143. lu_gp_mem->lu_gp = NULL;
  1144. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1145. spin_lock(&lu_gp->lu_gp_lock);
  1146. }
  1147. spin_unlock(&lu_gp->lu_gp_lock);
  1148. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1149. }
  1150. void core_alua_free_lu_gp_mem(struct se_device *dev)
  1151. {
  1152. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1153. struct t10_alua *alua = &su_dev->t10_alua;
  1154. struct t10_alua_lu_gp *lu_gp;
  1155. struct t10_alua_lu_gp_member *lu_gp_mem;
  1156. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1157. return;
  1158. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1159. if (!lu_gp_mem)
  1160. return;
  1161. while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
  1162. cpu_relax();
  1163. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1164. lu_gp = lu_gp_mem->lu_gp;
  1165. if (lu_gp) {
  1166. spin_lock(&lu_gp->lu_gp_lock);
  1167. if (lu_gp_mem->lu_gp_assoc) {
  1168. list_del(&lu_gp_mem->lu_gp_mem_list);
  1169. lu_gp->lu_gp_members--;
  1170. lu_gp_mem->lu_gp_assoc = 0;
  1171. }
  1172. spin_unlock(&lu_gp->lu_gp_lock);
  1173. lu_gp_mem->lu_gp = NULL;
  1174. }
  1175. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1176. kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
  1177. }
  1178. struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
  1179. {
  1180. struct t10_alua_lu_gp *lu_gp;
  1181. struct config_item *ci;
  1182. spin_lock(&lu_gps_lock);
  1183. list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
  1184. if (!lu_gp->lu_gp_valid_id)
  1185. continue;
  1186. ci = &lu_gp->lu_gp_group.cg_item;
  1187. if (!strcmp(config_item_name(ci), name)) {
  1188. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  1189. spin_unlock(&lu_gps_lock);
  1190. return lu_gp;
  1191. }
  1192. }
  1193. spin_unlock(&lu_gps_lock);
  1194. return NULL;
  1195. }
  1196. void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
  1197. {
  1198. spin_lock(&lu_gps_lock);
  1199. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  1200. spin_unlock(&lu_gps_lock);
  1201. }
  1202. /*
  1203. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1204. */
  1205. void __core_alua_attach_lu_gp_mem(
  1206. struct t10_alua_lu_gp_member *lu_gp_mem,
  1207. struct t10_alua_lu_gp *lu_gp)
  1208. {
  1209. spin_lock(&lu_gp->lu_gp_lock);
  1210. lu_gp_mem->lu_gp = lu_gp;
  1211. lu_gp_mem->lu_gp_assoc = 1;
  1212. list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
  1213. lu_gp->lu_gp_members++;
  1214. spin_unlock(&lu_gp->lu_gp_lock);
  1215. }
  1216. /*
  1217. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1218. */
  1219. void __core_alua_drop_lu_gp_mem(
  1220. struct t10_alua_lu_gp_member *lu_gp_mem,
  1221. struct t10_alua_lu_gp *lu_gp)
  1222. {
  1223. spin_lock(&lu_gp->lu_gp_lock);
  1224. list_del(&lu_gp_mem->lu_gp_mem_list);
  1225. lu_gp_mem->lu_gp = NULL;
  1226. lu_gp_mem->lu_gp_assoc = 0;
  1227. lu_gp->lu_gp_members--;
  1228. spin_unlock(&lu_gp->lu_gp_lock);
  1229. }
  1230. struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
  1231. struct se_subsystem_dev *su_dev,
  1232. const char *name,
  1233. int def_group)
  1234. {
  1235. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1236. tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
  1237. if (!tg_pt_gp) {
  1238. pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
  1239. return NULL;
  1240. }
  1241. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
  1242. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
  1243. mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
  1244. spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
  1245. atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
  1246. tg_pt_gp->tg_pt_gp_su_dev = su_dev;
  1247. tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
  1248. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  1249. ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
  1250. /*
  1251. * Enable both explict and implict ALUA support by default
  1252. */
  1253. tg_pt_gp->tg_pt_gp_alua_access_type =
  1254. TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
  1255. /*
  1256. * Set the default Active/NonOptimized Delay in milliseconds
  1257. */
  1258. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
  1259. tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
  1260. tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
  1261. if (def_group) {
  1262. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1263. tg_pt_gp->tg_pt_gp_id =
  1264. su_dev->t10_alua.alua_tg_pt_gps_counter++;
  1265. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1266. su_dev->t10_alua.alua_tg_pt_gps_count++;
  1267. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1268. &su_dev->t10_alua.tg_pt_gps_list);
  1269. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1270. }
  1271. return tg_pt_gp;
  1272. }
  1273. int core_alua_set_tg_pt_gp_id(
  1274. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1275. u16 tg_pt_gp_id)
  1276. {
  1277. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1278. struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
  1279. u16 tg_pt_gp_id_tmp;
  1280. /*
  1281. * The tg_pt_gp->tg_pt_gp_id may only be set once..
  1282. */
  1283. if (tg_pt_gp->tg_pt_gp_valid_id) {
  1284. pr_warn("ALUA TG PT Group already has a valid ID,"
  1285. " ignoring request\n");
  1286. return -EINVAL;
  1287. }
  1288. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1289. if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
  1290. pr_err("Maximum ALUA alua_tg_pt_gps_count:"
  1291. " 0x0000ffff reached\n");
  1292. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1293. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1294. return -ENOSPC;
  1295. }
  1296. again:
  1297. tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
  1298. su_dev->t10_alua.alua_tg_pt_gps_counter++;
  1299. list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
  1300. tg_pt_gp_list) {
  1301. if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
  1302. if (!tg_pt_gp_id)
  1303. goto again;
  1304. pr_err("ALUA Target Port Group ID: %hu already"
  1305. " exists, ignoring request\n", tg_pt_gp_id);
  1306. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1307. return -EINVAL;
  1308. }
  1309. }
  1310. tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
  1311. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1312. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1313. &su_dev->t10_alua.tg_pt_gps_list);
  1314. su_dev->t10_alua.alua_tg_pt_gps_count++;
  1315. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1316. return 0;
  1317. }
  1318. struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
  1319. struct se_port *port)
  1320. {
  1321. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1322. tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
  1323. GFP_KERNEL);
  1324. if (!tg_pt_gp_mem) {
  1325. pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
  1326. return ERR_PTR(-ENOMEM);
  1327. }
  1328. INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1329. spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1330. atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
  1331. tg_pt_gp_mem->tg_pt = port;
  1332. port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
  1333. return tg_pt_gp_mem;
  1334. }
  1335. void core_alua_free_tg_pt_gp(
  1336. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1337. {
  1338. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1339. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
  1340. /*
  1341. * Once we have reached this point, config_item_put() has already
  1342. * been called from target_core_alua_drop_tg_pt_gp().
  1343. *
  1344. * Here we remove *tg_pt_gp from the global list so that
  1345. * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
  1346. * can be made while we are releasing struct t10_alua_tg_pt_gp.
  1347. */
  1348. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1349. list_del(&tg_pt_gp->tg_pt_gp_list);
  1350. su_dev->t10_alua.alua_tg_pt_gps_counter--;
  1351. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1352. /*
  1353. * Allow a struct t10_alua_tg_pt_gp_member * referenced by
  1354. * core_alua_get_tg_pt_gp_by_name() in
  1355. * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
  1356. * to be released with core_alua_put_tg_pt_gp_from_name().
  1357. */
  1358. while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
  1359. cpu_relax();
  1360. /*
  1361. * Release reference to struct t10_alua_tg_pt_gp from all associated
  1362. * struct se_port.
  1363. */
  1364. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1365. list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
  1366. &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
  1367. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1368. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1369. tg_pt_gp->tg_pt_gp_members--;
  1370. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1371. }
  1372. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1373. /*
  1374. * tg_pt_gp_mem is associated with a single
  1375. * se_port->sep_alua_tg_pt_gp_mem, and is released via
  1376. * core_alua_free_tg_pt_gp_mem().
  1377. *
  1378. * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
  1379. * assume we want to re-assocate a given tg_pt_gp_mem with
  1380. * default_tg_pt_gp.
  1381. */
  1382. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1383. if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
  1384. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1385. su_dev->t10_alua.default_tg_pt_gp);
  1386. } else
  1387. tg_pt_gp_mem->tg_pt_gp = NULL;
  1388. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1389. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1390. }
  1391. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1392. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1393. }
  1394. void core_alua_free_tg_pt_gp_mem(struct se_port *port)
  1395. {
  1396. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1397. struct t10_alua *alua = &su_dev->t10_alua;
  1398. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1399. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1400. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1401. return;
  1402. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1403. if (!tg_pt_gp_mem)
  1404. return;
  1405. while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
  1406. cpu_relax();
  1407. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1408. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1409. if (tg_pt_gp) {
  1410. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1411. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1412. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1413. tg_pt_gp->tg_pt_gp_members--;
  1414. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1415. }
  1416. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1417. tg_pt_gp_mem->tg_pt_gp = NULL;
  1418. }
  1419. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1420. kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
  1421. }
  1422. static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
  1423. struct se_subsystem_dev *su_dev,
  1424. const char *name)
  1425. {
  1426. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1427. struct config_item *ci;
  1428. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1429. list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
  1430. tg_pt_gp_list) {
  1431. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1432. continue;
  1433. ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1434. if (!strcmp(config_item_name(ci), name)) {
  1435. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1436. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1437. return tg_pt_gp;
  1438. }
  1439. }
  1440. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1441. return NULL;
  1442. }
  1443. static void core_alua_put_tg_pt_gp_from_name(
  1444. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1445. {
  1446. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1447. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1448. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1449. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1450. }
  1451. /*
  1452. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1453. */
  1454. void __core_alua_attach_tg_pt_gp_mem(
  1455. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1456. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1457. {
  1458. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1459. tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
  1460. tg_pt_gp_mem->tg_pt_gp_assoc = 1;
  1461. list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
  1462. &tg_pt_gp->tg_pt_gp_mem_list);
  1463. tg_pt_gp->tg_pt_gp_members++;
  1464. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1465. }
  1466. /*
  1467. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1468. */
  1469. static void __core_alua_drop_tg_pt_gp_mem(
  1470. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1471. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1472. {
  1473. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1474. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1475. tg_pt_gp_mem->tg_pt_gp = NULL;
  1476. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1477. tg_pt_gp->tg_pt_gp_members--;
  1478. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1479. }
  1480. ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
  1481. {
  1482. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1483. struct config_item *tg_pt_ci;
  1484. struct t10_alua *alua = &su_dev->t10_alua;
  1485. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1486. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1487. ssize_t len = 0;
  1488. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1489. return len;
  1490. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1491. if (!tg_pt_gp_mem)
  1492. return len;
  1493. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1494. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1495. if (tg_pt_gp) {
  1496. tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1497. len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
  1498. " %hu\nTG Port Primary Access State: %s\nTG Port "
  1499. "Primary Access Status: %s\nTG Port Secondary Access"
  1500. " State: %s\nTG Port Secondary Access Status: %s\n",
  1501. config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
  1502. core_alua_dump_state(atomic_read(
  1503. &tg_pt_gp->tg_pt_gp_alua_access_state)),
  1504. core_alua_dump_status(
  1505. tg_pt_gp->tg_pt_gp_alua_access_status),
  1506. (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
  1507. "Offline" : "None",
  1508. core_alua_dump_status(port->sep_tg_pt_secondary_stat));
  1509. }
  1510. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1511. return len;
  1512. }
  1513. ssize_t core_alua_store_tg_pt_gp_info(
  1514. struct se_port *port,
  1515. const char *page,
  1516. size_t count)
  1517. {
  1518. struct se_portal_group *tpg;
  1519. struct se_lun *lun;
  1520. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1521. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
  1522. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1523. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1524. int move = 0;
  1525. tpg = port->sep_tpg;
  1526. lun = port->sep_lun;
  1527. if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
  1528. pr_warn("SPC3_ALUA_EMULATED not enabled for"
  1529. " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1530. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1531. config_item_name(&lun->lun_group.cg_item));
  1532. return -EINVAL;
  1533. }
  1534. if (count > TG_PT_GROUP_NAME_BUF) {
  1535. pr_err("ALUA Target Port Group alias too large!\n");
  1536. return -EINVAL;
  1537. }
  1538. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1539. memcpy(buf, page, count);
  1540. /*
  1541. * Any ALUA target port group alias besides "NULL" means we will be
  1542. * making a new group association.
  1543. */
  1544. if (strcmp(strstrip(buf), "NULL")) {
  1545. /*
  1546. * core_alua_get_tg_pt_gp_by_name() will increment reference to
  1547. * struct t10_alua_tg_pt_gp. This reference is released with
  1548. * core_alua_put_tg_pt_gp_from_name() below.
  1549. */
  1550. tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
  1551. strstrip(buf));
  1552. if (!tg_pt_gp_new)
  1553. return -ENODEV;
  1554. }
  1555. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1556. if (!tg_pt_gp_mem) {
  1557. if (tg_pt_gp_new)
  1558. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1559. pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
  1560. return -EINVAL;
  1561. }
  1562. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1563. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1564. if (tg_pt_gp) {
  1565. /*
  1566. * Clearing an existing tg_pt_gp association, and replacing
  1567. * with the default_tg_pt_gp.
  1568. */
  1569. if (!tg_pt_gp_new) {
  1570. pr_debug("Target_Core_ConfigFS: Moving"
  1571. " %s/tpgt_%hu/%s from ALUA Target Port Group:"
  1572. " alua/%s, ID: %hu back to"
  1573. " default_tg_pt_gp\n",
  1574. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1575. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1576. config_item_name(&lun->lun_group.cg_item),
  1577. config_item_name(
  1578. &tg_pt_gp->tg_pt_gp_group.cg_item),
  1579. tg_pt_gp->tg_pt_gp_id);
  1580. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1581. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1582. su_dev->t10_alua.default_tg_pt_gp);
  1583. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1584. return count;
  1585. }
  1586. /*
  1587. * Removing existing association of tg_pt_gp_mem with tg_pt_gp
  1588. */
  1589. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1590. move = 1;
  1591. }
  1592. /*
  1593. * Associate tg_pt_gp_mem with tg_pt_gp_new.
  1594. */
  1595. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
  1596. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1597. pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
  1598. " Target Port Group: alua/%s, ID: %hu\n", (move) ?
  1599. "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1600. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1601. config_item_name(&lun->lun_group.cg_item),
  1602. config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
  1603. tg_pt_gp_new->tg_pt_gp_id);
  1604. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1605. return count;
  1606. }
  1607. ssize_t core_alua_show_access_type(
  1608. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1609. char *page)
  1610. {
  1611. if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
  1612. (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
  1613. return sprintf(page, "Implict and Explict\n");
  1614. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
  1615. return sprintf(page, "Implict\n");
  1616. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
  1617. return sprintf(page, "Explict\n");
  1618. else
  1619. return sprintf(page, "None\n");
  1620. }
  1621. ssize_t core_alua_store_access_type(
  1622. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1623. const char *page,
  1624. size_t count)
  1625. {
  1626. unsigned long tmp;
  1627. int ret;
  1628. ret = strict_strtoul(page, 0, &tmp);
  1629. if (ret < 0) {
  1630. pr_err("Unable to extract alua_access_type\n");
  1631. return -EINVAL;
  1632. }
  1633. if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
  1634. pr_err("Illegal value for alua_access_type:"
  1635. " %lu\n", tmp);
  1636. return -EINVAL;
  1637. }
  1638. if (tmp == 3)
  1639. tg_pt_gp->tg_pt_gp_alua_access_type =
  1640. TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
  1641. else if (tmp == 2)
  1642. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
  1643. else if (tmp == 1)
  1644. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
  1645. else
  1646. tg_pt_gp->tg_pt_gp_alua_access_type = 0;
  1647. return count;
  1648. }
  1649. ssize_t core_alua_show_nonop_delay_msecs(
  1650. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1651. char *page)
  1652. {
  1653. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
  1654. }
  1655. ssize_t core_alua_store_nonop_delay_msecs(
  1656. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1657. const char *page,
  1658. size_t count)
  1659. {
  1660. unsigned long tmp;
  1661. int ret;
  1662. ret = strict_strtoul(page, 0, &tmp);
  1663. if (ret < 0) {
  1664. pr_err("Unable to extract nonop_delay_msecs\n");
  1665. return -EINVAL;
  1666. }
  1667. if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
  1668. pr_err("Passed nonop_delay_msecs: %lu, exceeds"
  1669. " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
  1670. ALUA_MAX_NONOP_DELAY_MSECS);
  1671. return -EINVAL;
  1672. }
  1673. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
  1674. return count;
  1675. }
  1676. ssize_t core_alua_show_trans_delay_msecs(
  1677. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1678. char *page)
  1679. {
  1680. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  1681. }
  1682. ssize_t core_alua_store_trans_delay_msecs(
  1683. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1684. const char *page,
  1685. size_t count)
  1686. {
  1687. unsigned long tmp;
  1688. int ret;
  1689. ret = strict_strtoul(page, 0, &tmp);
  1690. if (ret < 0) {
  1691. pr_err("Unable to extract trans_delay_msecs\n");
  1692. return -EINVAL;
  1693. }
  1694. if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
  1695. pr_err("Passed trans_delay_msecs: %lu, exceeds"
  1696. " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
  1697. ALUA_MAX_TRANS_DELAY_MSECS);
  1698. return -EINVAL;
  1699. }
  1700. tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
  1701. return count;
  1702. }
  1703. ssize_t core_alua_show_implict_trans_secs(
  1704. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1705. char *page)
  1706. {
  1707. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
  1708. }
  1709. ssize_t core_alua_store_implict_trans_secs(
  1710. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1711. const char *page,
  1712. size_t count)
  1713. {
  1714. unsigned long tmp;
  1715. int ret;
  1716. ret = strict_strtoul(page, 0, &tmp);
  1717. if (ret < 0) {
  1718. pr_err("Unable to extract implict_trans_secs\n");
  1719. return -EINVAL;
  1720. }
  1721. if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
  1722. pr_err("Passed implict_trans_secs: %lu, exceeds"
  1723. " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
  1724. ALUA_MAX_IMPLICT_TRANS_SECS);
  1725. return -EINVAL;
  1726. }
  1727. tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
  1728. return count;
  1729. }
  1730. ssize_t core_alua_show_preferred_bit(
  1731. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1732. char *page)
  1733. {
  1734. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
  1735. }
  1736. ssize_t core_alua_store_preferred_bit(
  1737. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1738. const char *page,
  1739. size_t count)
  1740. {
  1741. unsigned long tmp;
  1742. int ret;
  1743. ret = strict_strtoul(page, 0, &tmp);
  1744. if (ret < 0) {
  1745. pr_err("Unable to extract preferred ALUA value\n");
  1746. return -EINVAL;
  1747. }
  1748. if ((tmp != 0) && (tmp != 1)) {
  1749. pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
  1750. return -EINVAL;
  1751. }
  1752. tg_pt_gp->tg_pt_gp_pref = (int)tmp;
  1753. return count;
  1754. }
  1755. ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
  1756. {
  1757. if (!lun->lun_sep)
  1758. return -ENODEV;
  1759. return sprintf(page, "%d\n",
  1760. atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
  1761. }
  1762. ssize_t core_alua_store_offline_bit(
  1763. struct se_lun *lun,
  1764. const char *page,
  1765. size_t count)
  1766. {
  1767. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1768. unsigned long tmp;
  1769. int ret;
  1770. if (!lun->lun_sep)
  1771. return -ENODEV;
  1772. ret = strict_strtoul(page, 0, &tmp);
  1773. if (ret < 0) {
  1774. pr_err("Unable to extract alua_tg_pt_offline value\n");
  1775. return -EINVAL;
  1776. }
  1777. if ((tmp != 0) && (tmp != 1)) {
  1778. pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
  1779. tmp);
  1780. return -EINVAL;
  1781. }
  1782. tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
  1783. if (!tg_pt_gp_mem) {
  1784. pr_err("Unable to locate *tg_pt_gp_mem\n");
  1785. return -EINVAL;
  1786. }
  1787. ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
  1788. lun->lun_sep, 0, (int)tmp);
  1789. if (ret < 0)
  1790. return -EINVAL;
  1791. return count;
  1792. }
  1793. ssize_t core_alua_show_secondary_status(
  1794. struct se_lun *lun,
  1795. char *page)
  1796. {
  1797. return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
  1798. }
  1799. ssize_t core_alua_store_secondary_status(
  1800. struct se_lun *lun,
  1801. const char *page,
  1802. size_t count)
  1803. {
  1804. unsigned long tmp;
  1805. int ret;
  1806. ret = strict_strtoul(page, 0, &tmp);
  1807. if (ret < 0) {
  1808. pr_err("Unable to extract alua_tg_pt_status\n");
  1809. return -EINVAL;
  1810. }
  1811. if ((tmp != ALUA_STATUS_NONE) &&
  1812. (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  1813. (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
  1814. pr_err("Illegal value for alua_tg_pt_status: %lu\n",
  1815. tmp);
  1816. return -EINVAL;
  1817. }
  1818. lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
  1819. return count;
  1820. }
  1821. ssize_t core_alua_show_secondary_write_metadata(
  1822. struct se_lun *lun,
  1823. char *page)
  1824. {
  1825. return sprintf(page, "%d\n",
  1826. lun->lun_sep->sep_tg_pt_secondary_write_md);
  1827. }
  1828. ssize_t core_alua_store_secondary_write_metadata(
  1829. struct se_lun *lun,
  1830. const char *page,
  1831. size_t count)
  1832. {
  1833. unsigned long tmp;
  1834. int ret;
  1835. ret = strict_strtoul(page, 0, &tmp);
  1836. if (ret < 0) {
  1837. pr_err("Unable to extract alua_tg_pt_write_md\n");
  1838. return -EINVAL;
  1839. }
  1840. if ((tmp != 0) && (tmp != 1)) {
  1841. pr_err("Illegal value for alua_tg_pt_write_md:"
  1842. " %lu\n", tmp);
  1843. return -EINVAL;
  1844. }
  1845. lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
  1846. return count;
  1847. }
  1848. int core_setup_alua(struct se_device *dev, int force_pt)
  1849. {
  1850. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1851. struct t10_alua *alua = &su_dev->t10_alua;
  1852. struct t10_alua_lu_gp_member *lu_gp_mem;
  1853. /*
  1854. * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
  1855. * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
  1856. * cause a problem because libata and some SATA RAID HBAs appear
  1857. * under Linux/SCSI, but emulate SCSI logic themselves.
  1858. */
  1859. if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  1860. !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
  1861. alua->alua_type = SPC_ALUA_PASSTHROUGH;
  1862. alua->alua_state_check = &core_alua_state_check_nop;
  1863. pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
  1864. " emulation\n", dev->transport->name);
  1865. return 0;
  1866. }
  1867. /*
  1868. * If SPC-3 or above is reported by real or emulated struct se_device,
  1869. * use emulated ALUA.
  1870. */
  1871. if (dev->transport->get_device_rev(dev) >= SCSI_3) {
  1872. pr_debug("%s: Enabling ALUA Emulation for SPC-3"
  1873. " device\n", dev->transport->name);
  1874. /*
  1875. * Associate this struct se_device with the default ALUA
  1876. * LUN Group.
  1877. */
  1878. lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
  1879. if (IS_ERR(lu_gp_mem))
  1880. return PTR_ERR(lu_gp_mem);
  1881. alua->alua_type = SPC3_ALUA_EMULATED;
  1882. alua->alua_state_check = &core_alua_state_check;
  1883. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1884. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1885. default_lu_gp);
  1886. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1887. pr_debug("%s: Adding to default ALUA LU Group:"
  1888. " core/alua/lu_gps/default_lu_gp\n",
  1889. dev->transport->name);
  1890. } else {
  1891. alua->alua_type = SPC2_ALUA_DISABLED;
  1892. alua->alua_state_check = &core_alua_state_check_nop;
  1893. pr_debug("%s: Disabling ALUA Emulation for SPC-2"
  1894. " device\n", dev->transport->name);
  1895. }
  1896. return 0;
  1897. }