scsi_dh_alua.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic SCSI-3 ALUA SCSI Device Handler
  4. *
  5. * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
  6. * All rights reserved.
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/delay.h>
  10. #include <linux/module.h>
  11. #include <linux/unaligned.h>
  12. #include <scsi/scsi.h>
  13. #include <scsi/scsi_proto.h>
  14. #include <scsi/scsi_dbg.h>
  15. #include <scsi/scsi_eh.h>
  16. #include <scsi/scsi_dh.h>
  17. #define ALUA_DH_NAME "alua"
  18. #define ALUA_DH_VER "2.0"
  19. #define TPGS_SUPPORT_NONE 0x00
  20. #define TPGS_SUPPORT_OPTIMIZED 0x01
  21. #define TPGS_SUPPORT_NONOPTIMIZED 0x02
  22. #define TPGS_SUPPORT_STANDBY 0x04
  23. #define TPGS_SUPPORT_UNAVAILABLE 0x08
  24. #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
  25. #define TPGS_SUPPORT_OFFLINE 0x40
  26. #define TPGS_SUPPORT_TRANSITION 0x80
  27. #define TPGS_SUPPORT_ALL 0xdf
  28. #define RTPG_FMT_MASK 0x70
  29. #define RTPG_FMT_EXT_HDR 0x10
  30. #define TPGS_MODE_UNINITIALIZED -1
  31. #define TPGS_MODE_NONE 0x0
  32. #define TPGS_MODE_IMPLICIT 0x1
  33. #define TPGS_MODE_EXPLICIT 0x2
  34. #define ALUA_RTPG_SIZE 128
  35. #define ALUA_FAILOVER_TIMEOUT 60
  36. #define ALUA_FAILOVER_RETRIES 5
  37. #define ALUA_RTPG_DELAY_MSECS 5
  38. #define ALUA_RTPG_RETRY_DELAY 2
  39. /* device handler flags */
  40. #define ALUA_OPTIMIZE_STPG 0x01
  41. #define ALUA_RTPG_EXT_HDR_UNSUPP 0x02
  42. /* State machine flags */
  43. #define ALUA_PG_RUN_RTPG 0x10
  44. #define ALUA_PG_RUN_STPG 0x20
  45. #define ALUA_PG_RUNNING 0x40
  46. static uint optimize_stpg;
  47. module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
  48. MODULE_PARM_DESC(optimize_stpg, "Allow use of a non-optimized path, rather than sending a STPG, when implicit TPGS is supported (0=No,1=Yes). Default is 0.");
  49. static LIST_HEAD(port_group_list);
  50. static DEFINE_SPINLOCK(port_group_lock);
  51. static struct workqueue_struct *kaluad_wq;
  52. struct alua_port_group {
  53. struct kref kref;
  54. struct rcu_head rcu;
  55. struct list_head node;
  56. struct list_head dh_list;
  57. unsigned char device_id_str[256];
  58. int device_id_len;
  59. int group_id;
  60. int tpgs;
  61. int state;
  62. int pref;
  63. int valid_states;
  64. unsigned flags; /* used for optimizing STPG */
  65. unsigned char transition_tmo;
  66. unsigned long expiry;
  67. unsigned long interval;
  68. struct delayed_work rtpg_work;
  69. spinlock_t lock;
  70. struct list_head rtpg_list;
  71. struct scsi_device *rtpg_sdev;
  72. };
  73. struct alua_dh_data {
  74. struct list_head node;
  75. struct alua_port_group __rcu *pg;
  76. int group_id;
  77. spinlock_t pg_lock;
  78. struct scsi_device *sdev;
  79. int init_error;
  80. struct mutex init_mutex;
  81. bool disabled;
  82. };
  83. struct alua_queue_data {
  84. struct list_head entry;
  85. activate_complete callback_fn;
  86. void *callback_data;
  87. };
  88. #define ALUA_POLICY_SWITCH_CURRENT 0
  89. #define ALUA_POLICY_SWITCH_ALL 1
  90. static void alua_rtpg_work(struct work_struct *work);
  91. static bool alua_rtpg_queue(struct alua_port_group *pg,
  92. struct scsi_device *sdev,
  93. struct alua_queue_data *qdata, bool force);
  94. static void alua_check(struct scsi_device *sdev, bool force);
  95. static void release_port_group(struct kref *kref)
  96. {
  97. struct alua_port_group *pg;
  98. pg = container_of(kref, struct alua_port_group, kref);
  99. if (pg->rtpg_sdev)
  100. flush_delayed_work(&pg->rtpg_work);
  101. spin_lock(&port_group_lock);
  102. list_del(&pg->node);
  103. spin_unlock(&port_group_lock);
  104. kfree_rcu(pg, rcu);
  105. }
  106. /*
  107. * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
  108. * @sdev: sdev the command should be sent to
  109. */
  110. static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
  111. int bufflen, struct scsi_sense_hdr *sshdr, int flags)
  112. {
  113. u8 cdb[MAX_COMMAND_SIZE];
  114. blk_opf_t opf = REQ_OP_DRV_IN | REQ_FAILFAST_DEV |
  115. REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER;
  116. const struct scsi_exec_args exec_args = {
  117. .sshdr = sshdr,
  118. };
  119. /* Prepare the command. */
  120. memset(cdb, 0x0, MAX_COMMAND_SIZE);
  121. cdb[0] = MAINTENANCE_IN;
  122. if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
  123. cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
  124. else
  125. cdb[1] = MI_REPORT_TARGET_PGS;
  126. put_unaligned_be32(bufflen, &cdb[6]);
  127. return scsi_execute_cmd(sdev, cdb, opf, buff, bufflen,
  128. ALUA_FAILOVER_TIMEOUT * HZ,
  129. ALUA_FAILOVER_RETRIES, &exec_args);
  130. }
  131. /*
  132. * submit_stpg - Issue a SET TARGET PORT GROUP command
  133. *
  134. * Currently we're only setting the current target port group state
  135. * to 'active/optimized' and let the array firmware figure out
  136. * the states of the remaining groups.
  137. */
  138. static int submit_stpg(struct scsi_device *sdev, int group_id,
  139. struct scsi_sense_hdr *sshdr)
  140. {
  141. u8 cdb[MAX_COMMAND_SIZE];
  142. unsigned char stpg_data[8];
  143. int stpg_len = 8;
  144. blk_opf_t opf = REQ_OP_DRV_OUT | REQ_FAILFAST_DEV |
  145. REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER;
  146. const struct scsi_exec_args exec_args = {
  147. .sshdr = sshdr,
  148. };
  149. /* Prepare the data buffer */
  150. memset(stpg_data, 0, stpg_len);
  151. stpg_data[4] = SCSI_ACCESS_STATE_OPTIMAL;
  152. put_unaligned_be16(group_id, &stpg_data[6]);
  153. /* Prepare the command. */
  154. memset(cdb, 0x0, MAX_COMMAND_SIZE);
  155. cdb[0] = MAINTENANCE_OUT;
  156. cdb[1] = MO_SET_TARGET_PGS;
  157. put_unaligned_be32(stpg_len, &cdb[6]);
  158. return scsi_execute_cmd(sdev, cdb, opf, stpg_data,
  159. stpg_len, ALUA_FAILOVER_TIMEOUT * HZ,
  160. ALUA_FAILOVER_RETRIES, &exec_args);
  161. }
  162. static struct alua_port_group *alua_find_get_pg(char *id_str, size_t id_size,
  163. int group_id)
  164. {
  165. struct alua_port_group *pg;
  166. if (!id_str || !id_size || !strlen(id_str))
  167. return NULL;
  168. list_for_each_entry(pg, &port_group_list, node) {
  169. if (pg->group_id != group_id)
  170. continue;
  171. if (!pg->device_id_len || pg->device_id_len != id_size)
  172. continue;
  173. if (strncmp(pg->device_id_str, id_str, id_size))
  174. continue;
  175. if (!kref_get_unless_zero(&pg->kref))
  176. continue;
  177. return pg;
  178. }
  179. return NULL;
  180. }
  181. /*
  182. * alua_alloc_pg - Allocate a new port_group structure
  183. * @sdev: scsi device
  184. * @group_id: port group id
  185. * @tpgs: target port group settings
  186. *
  187. * Allocate a new port_group structure for a given
  188. * device.
  189. */
  190. static struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
  191. int group_id, int tpgs)
  192. {
  193. struct alua_port_group *pg, *tmp_pg;
  194. pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL);
  195. if (!pg)
  196. return ERR_PTR(-ENOMEM);
  197. pg->device_id_len = scsi_vpd_lun_id(sdev, pg->device_id_str,
  198. sizeof(pg->device_id_str));
  199. if (pg->device_id_len <= 0) {
  200. /*
  201. * TPGS supported but no device identification found.
  202. * Generate private device identification.
  203. */
  204. sdev_printk(KERN_INFO, sdev,
  205. "%s: No device descriptors found\n",
  206. ALUA_DH_NAME);
  207. pg->device_id_str[0] = '\0';
  208. pg->device_id_len = 0;
  209. }
  210. pg->group_id = group_id;
  211. pg->tpgs = tpgs;
  212. pg->state = SCSI_ACCESS_STATE_OPTIMAL;
  213. pg->valid_states = TPGS_SUPPORT_ALL;
  214. if (optimize_stpg)
  215. pg->flags |= ALUA_OPTIMIZE_STPG;
  216. kref_init(&pg->kref);
  217. INIT_DELAYED_WORK(&pg->rtpg_work, alua_rtpg_work);
  218. INIT_LIST_HEAD(&pg->rtpg_list);
  219. INIT_LIST_HEAD(&pg->node);
  220. INIT_LIST_HEAD(&pg->dh_list);
  221. spin_lock_init(&pg->lock);
  222. spin_lock(&port_group_lock);
  223. tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
  224. group_id);
  225. if (tmp_pg) {
  226. spin_unlock(&port_group_lock);
  227. kfree(pg);
  228. return tmp_pg;
  229. }
  230. list_add(&pg->node, &port_group_list);
  231. spin_unlock(&port_group_lock);
  232. return pg;
  233. }
  234. /*
  235. * alua_check_tpgs - Evaluate TPGS setting
  236. * @sdev: device to be checked
  237. *
  238. * Examine the TPGS setting of the sdev to find out if ALUA
  239. * is supported.
  240. */
  241. static int alua_check_tpgs(struct scsi_device *sdev)
  242. {
  243. int tpgs = TPGS_MODE_NONE;
  244. /*
  245. * ALUA support for non-disk devices is fraught with
  246. * difficulties, so disable it for now.
  247. */
  248. if (sdev->type != TYPE_DISK) {
  249. sdev_printk(KERN_INFO, sdev,
  250. "%s: disable for non-disk devices\n",
  251. ALUA_DH_NAME);
  252. return tpgs;
  253. }
  254. tpgs = scsi_device_tpgs(sdev);
  255. switch (tpgs) {
  256. case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
  257. sdev_printk(KERN_INFO, sdev,
  258. "%s: supports implicit and explicit TPGS\n",
  259. ALUA_DH_NAME);
  260. break;
  261. case TPGS_MODE_EXPLICIT:
  262. sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
  263. ALUA_DH_NAME);
  264. break;
  265. case TPGS_MODE_IMPLICIT:
  266. sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
  267. ALUA_DH_NAME);
  268. break;
  269. case TPGS_MODE_NONE:
  270. sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
  271. ALUA_DH_NAME);
  272. break;
  273. default:
  274. sdev_printk(KERN_INFO, sdev,
  275. "%s: unsupported TPGS setting %d\n",
  276. ALUA_DH_NAME, tpgs);
  277. tpgs = TPGS_MODE_NONE;
  278. break;
  279. }
  280. return tpgs;
  281. }
  282. /*
  283. * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
  284. * @sdev: device to be checked
  285. *
  286. * Extract the relative target port and the target port group
  287. * descriptor from the list of identificators.
  288. */
  289. static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
  290. int tpgs)
  291. {
  292. int rel_port = -1, group_id;
  293. struct alua_port_group *pg, *old_pg = NULL;
  294. bool pg_updated = false;
  295. unsigned long flags;
  296. group_id = scsi_vpd_tpg_id(sdev, &rel_port);
  297. if (group_id < 0) {
  298. /*
  299. * Internal error; TPGS supported but required
  300. * VPD identification descriptors not present.
  301. * Disable ALUA support
  302. */
  303. sdev_printk(KERN_INFO, sdev,
  304. "%s: No target port descriptors found\n",
  305. ALUA_DH_NAME);
  306. return SCSI_DH_DEV_UNSUPP;
  307. }
  308. pg = alua_alloc_pg(sdev, group_id, tpgs);
  309. if (IS_ERR(pg)) {
  310. if (PTR_ERR(pg) == -ENOMEM)
  311. return SCSI_DH_NOMEM;
  312. return SCSI_DH_DEV_UNSUPP;
  313. }
  314. if (pg->device_id_len)
  315. sdev_printk(KERN_INFO, sdev,
  316. "%s: device %s port group %x rel port %x\n",
  317. ALUA_DH_NAME, pg->device_id_str,
  318. group_id, rel_port);
  319. else
  320. sdev_printk(KERN_INFO, sdev,
  321. "%s: port group %x rel port %x\n",
  322. ALUA_DH_NAME, group_id, rel_port);
  323. kref_get(&pg->kref);
  324. /* Check for existing port group references */
  325. spin_lock(&h->pg_lock);
  326. old_pg = rcu_dereference_protected(h->pg, lockdep_is_held(&h->pg_lock));
  327. if (old_pg != pg) {
  328. /* port group has changed. Update to new port group */
  329. if (h->pg) {
  330. spin_lock_irqsave(&old_pg->lock, flags);
  331. list_del_rcu(&h->node);
  332. spin_unlock_irqrestore(&old_pg->lock, flags);
  333. }
  334. rcu_assign_pointer(h->pg, pg);
  335. pg_updated = true;
  336. }
  337. spin_lock_irqsave(&pg->lock, flags);
  338. if (pg_updated)
  339. list_add_rcu(&h->node, &pg->dh_list);
  340. spin_unlock_irqrestore(&pg->lock, flags);
  341. spin_unlock(&h->pg_lock);
  342. alua_rtpg_queue(pg, sdev, NULL, true);
  343. kref_put(&pg->kref, release_port_group);
  344. if (old_pg)
  345. kref_put(&old_pg->kref, release_port_group);
  346. return SCSI_DH_OK;
  347. }
  348. static char print_alua_state(unsigned char state)
  349. {
  350. switch (state) {
  351. case SCSI_ACCESS_STATE_OPTIMAL:
  352. return 'A';
  353. case SCSI_ACCESS_STATE_ACTIVE:
  354. return 'N';
  355. case SCSI_ACCESS_STATE_STANDBY:
  356. return 'S';
  357. case SCSI_ACCESS_STATE_UNAVAILABLE:
  358. return 'U';
  359. case SCSI_ACCESS_STATE_LBA:
  360. return 'L';
  361. case SCSI_ACCESS_STATE_OFFLINE:
  362. return 'O';
  363. case SCSI_ACCESS_STATE_TRANSITIONING:
  364. return 'T';
  365. default:
  366. return 'X';
  367. }
  368. }
  369. static void alua_handle_state_transition(struct scsi_device *sdev)
  370. {
  371. struct alua_dh_data *h = sdev->handler_data;
  372. struct alua_port_group *pg;
  373. rcu_read_lock();
  374. pg = rcu_dereference(h->pg);
  375. if (pg)
  376. pg->state = SCSI_ACCESS_STATE_TRANSITIONING;
  377. rcu_read_unlock();
  378. alua_check(sdev, false);
  379. }
  380. static enum scsi_disposition alua_check_sense(struct scsi_device *sdev,
  381. struct scsi_sense_hdr *sense_hdr)
  382. {
  383. switch (sense_hdr->sense_key) {
  384. case NOT_READY:
  385. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a) {
  386. /*
  387. * LUN Not Accessible - ALUA state transition
  388. */
  389. alua_handle_state_transition(sdev);
  390. return NEEDS_RETRY;
  391. }
  392. break;
  393. case UNIT_ATTENTION:
  394. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a) {
  395. /*
  396. * LUN Not Accessible - ALUA state transition
  397. */
  398. alua_handle_state_transition(sdev);
  399. return NEEDS_RETRY;
  400. }
  401. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00) {
  402. /*
  403. * Power On, Reset, or Bus Device Reset.
  404. * Might have obscured a state transition,
  405. * so schedule a recheck.
  406. */
  407. alua_check(sdev, true);
  408. return ADD_TO_MLQUEUE;
  409. }
  410. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
  411. /*
  412. * Device internal reset
  413. */
  414. return ADD_TO_MLQUEUE;
  415. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
  416. /*
  417. * Mode Parameters Changed
  418. */
  419. return ADD_TO_MLQUEUE;
  420. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) {
  421. /*
  422. * ALUA state changed
  423. */
  424. alua_check(sdev, true);
  425. return ADD_TO_MLQUEUE;
  426. }
  427. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) {
  428. /*
  429. * Implicit ALUA state transition failed
  430. */
  431. alua_check(sdev, true);
  432. return ADD_TO_MLQUEUE;
  433. }
  434. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
  435. /*
  436. * Inquiry data has changed
  437. */
  438. return ADD_TO_MLQUEUE;
  439. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
  440. /*
  441. * REPORTED_LUNS_DATA_HAS_CHANGED is reported
  442. * when switching controllers on targets like
  443. * Intel Multi-Flex. We can just retry.
  444. */
  445. return ADD_TO_MLQUEUE;
  446. break;
  447. }
  448. return SCSI_RETURN_NOT_HANDLED;
  449. }
  450. /*
  451. * alua_tur - Send a TEST UNIT READY
  452. * @sdev: device to which the TEST UNIT READY command should be send
  453. *
  454. * Send a TEST UNIT READY to @sdev to figure out the device state
  455. * Returns SCSI_DH_RETRY if the sense code is NOT READY/ALUA TRANSITIONING,
  456. * SCSI_DH_OK if no error occurred, and SCSI_DH_IO otherwise.
  457. */
  458. static int alua_tur(struct scsi_device *sdev)
  459. {
  460. struct scsi_sense_hdr sense_hdr;
  461. int retval;
  462. retval = scsi_test_unit_ready(sdev, ALUA_FAILOVER_TIMEOUT * HZ,
  463. ALUA_FAILOVER_RETRIES, &sense_hdr);
  464. if ((sense_hdr.sense_key == NOT_READY ||
  465. sense_hdr.sense_key == UNIT_ATTENTION) &&
  466. sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
  467. return SCSI_DH_RETRY;
  468. else if (retval)
  469. return SCSI_DH_IO;
  470. else
  471. return SCSI_DH_OK;
  472. }
  473. /*
  474. * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
  475. * @sdev: the device to be evaluated.
  476. *
  477. * Evaluate the Target Port Group State.
  478. * Returns SCSI_DH_DEV_OFFLINED if the path is
  479. * found to be unusable.
  480. */
  481. static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
  482. {
  483. struct scsi_sense_hdr sense_hdr;
  484. struct alua_port_group *tmp_pg;
  485. int len, k, off, bufflen = ALUA_RTPG_SIZE;
  486. int group_id_old, state_old, pref_old, valid_states_old;
  487. unsigned char *desc, *buff;
  488. unsigned err;
  489. int retval;
  490. unsigned int tpg_desc_tbl_off;
  491. unsigned char orig_transition_tmo;
  492. unsigned long flags;
  493. bool transitioning_sense = false;
  494. group_id_old = pg->group_id;
  495. state_old = pg->state;
  496. pref_old = pg->pref;
  497. valid_states_old = pg->valid_states;
  498. if (!pg->expiry) {
  499. unsigned long transition_tmo = ALUA_FAILOVER_TIMEOUT * HZ;
  500. if (pg->transition_tmo)
  501. transition_tmo = pg->transition_tmo * HZ;
  502. pg->expiry = round_jiffies_up(jiffies + transition_tmo);
  503. }
  504. buff = kzalloc(bufflen, GFP_KERNEL);
  505. if (!buff)
  506. return SCSI_DH_DEV_TEMP_BUSY;
  507. retry:
  508. err = 0;
  509. retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
  510. if (retval) {
  511. /*
  512. * Some (broken) implementations have a habit of returning
  513. * an error during things like firmware update etc.
  514. * But if the target only supports active/optimized there's
  515. * not much we can do; it's not that we can switch paths
  516. * or anything.
  517. * So ignore any errors to avoid spurious failures during
  518. * path failover.
  519. */
  520. if ((pg->valid_states & ~TPGS_SUPPORT_OPTIMIZED) == 0) {
  521. sdev_printk(KERN_INFO, sdev,
  522. "%s: ignoring rtpg result %d\n",
  523. ALUA_DH_NAME, retval);
  524. kfree(buff);
  525. return SCSI_DH_OK;
  526. }
  527. if (retval < 0 || !scsi_sense_valid(&sense_hdr)) {
  528. sdev_printk(KERN_INFO, sdev,
  529. "%s: rtpg failed, result %d\n",
  530. ALUA_DH_NAME, retval);
  531. kfree(buff);
  532. if (retval < 0)
  533. return SCSI_DH_DEV_TEMP_BUSY;
  534. if (host_byte(retval) == DID_NO_CONNECT)
  535. return SCSI_DH_RES_TEMP_UNAVAIL;
  536. return SCSI_DH_IO;
  537. }
  538. /*
  539. * submit_rtpg() has failed on existing arrays
  540. * when requesting extended header info, and
  541. * the array doesn't support extended headers,
  542. * even though it shouldn't according to T10.
  543. * The retry without rtpg_ext_hdr_req set
  544. * handles this.
  545. * Note: some arrays return a sense key of ILLEGAL_REQUEST
  546. * with ASC 00h if they don't support the extended header.
  547. */
  548. if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
  549. sense_hdr.sense_key == ILLEGAL_REQUEST) {
  550. pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
  551. goto retry;
  552. }
  553. /*
  554. * If the array returns with 'ALUA state transition'
  555. * sense code here it cannot return RTPG data during
  556. * transition. So set the state to 'transitioning' directly.
  557. */
  558. if (sense_hdr.sense_key == NOT_READY &&
  559. sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) {
  560. transitioning_sense = true;
  561. goto skip_rtpg;
  562. }
  563. /*
  564. * Retry on any other UNIT ATTENTION occurred.
  565. */
  566. if (sense_hdr.sense_key == UNIT_ATTENTION)
  567. err = SCSI_DH_RETRY;
  568. if (err == SCSI_DH_RETRY &&
  569. pg->expiry != 0 && time_before(jiffies, pg->expiry)) {
  570. sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
  571. ALUA_DH_NAME);
  572. scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
  573. kfree(buff);
  574. return err;
  575. }
  576. sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
  577. ALUA_DH_NAME);
  578. scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
  579. kfree(buff);
  580. pg->expiry = 0;
  581. return SCSI_DH_IO;
  582. }
  583. len = get_unaligned_be32(&buff[0]) + 4;
  584. if (len > bufflen) {
  585. /* Resubmit with the correct length */
  586. kfree(buff);
  587. bufflen = len;
  588. buff = kmalloc(bufflen, GFP_KERNEL);
  589. if (!buff) {
  590. sdev_printk(KERN_WARNING, sdev,
  591. "%s: kmalloc buffer failed\n",__func__);
  592. /* Temporary failure, bypass */
  593. pg->expiry = 0;
  594. return SCSI_DH_DEV_TEMP_BUSY;
  595. }
  596. goto retry;
  597. }
  598. orig_transition_tmo = pg->transition_tmo;
  599. if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
  600. pg->transition_tmo = buff[5];
  601. else
  602. pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
  603. if (orig_transition_tmo != pg->transition_tmo) {
  604. sdev_printk(KERN_INFO, sdev,
  605. "%s: transition timeout set to %d seconds\n",
  606. ALUA_DH_NAME, pg->transition_tmo);
  607. pg->expiry = jiffies + pg->transition_tmo * HZ;
  608. }
  609. if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
  610. tpg_desc_tbl_off = 8;
  611. else
  612. tpg_desc_tbl_off = 4;
  613. for (k = tpg_desc_tbl_off, desc = buff + tpg_desc_tbl_off;
  614. k < len;
  615. k += off, desc += off) {
  616. u16 group_id = get_unaligned_be16(&desc[2]);
  617. spin_lock_irqsave(&port_group_lock, flags);
  618. tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
  619. group_id);
  620. spin_unlock_irqrestore(&port_group_lock, flags);
  621. if (tmp_pg) {
  622. if (spin_trylock_irqsave(&tmp_pg->lock, flags)) {
  623. if ((tmp_pg == pg) ||
  624. !(tmp_pg->flags & ALUA_PG_RUNNING)) {
  625. struct alua_dh_data *h;
  626. tmp_pg->state = desc[0] & 0x0f;
  627. tmp_pg->pref = desc[0] >> 7;
  628. rcu_read_lock();
  629. list_for_each_entry_rcu(h,
  630. &tmp_pg->dh_list, node) {
  631. if (!h->sdev)
  632. continue;
  633. h->sdev->access_state = desc[0];
  634. }
  635. rcu_read_unlock();
  636. }
  637. if (tmp_pg == pg)
  638. tmp_pg->valid_states = desc[1];
  639. spin_unlock_irqrestore(&tmp_pg->lock, flags);
  640. }
  641. kref_put(&tmp_pg->kref, release_port_group);
  642. }
  643. off = 8 + (desc[7] * 4);
  644. }
  645. skip_rtpg:
  646. spin_lock_irqsave(&pg->lock, flags);
  647. if (transitioning_sense)
  648. pg->state = SCSI_ACCESS_STATE_TRANSITIONING;
  649. if (group_id_old != pg->group_id || state_old != pg->state ||
  650. pref_old != pg->pref || valid_states_old != pg->valid_states)
  651. sdev_printk(KERN_INFO, sdev,
  652. "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
  653. ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
  654. pg->pref ? "preferred" : "non-preferred",
  655. pg->valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
  656. pg->valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
  657. pg->valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
  658. pg->valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
  659. pg->valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
  660. pg->valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
  661. pg->valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
  662. switch (pg->state) {
  663. case SCSI_ACCESS_STATE_TRANSITIONING:
  664. if (time_before(jiffies, pg->expiry)) {
  665. /* State transition, retry */
  666. pg->interval = ALUA_RTPG_RETRY_DELAY;
  667. err = SCSI_DH_RETRY;
  668. } else {
  669. struct alua_dh_data *h;
  670. /* Transitioning time exceeded, set port to standby */
  671. err = SCSI_DH_IO;
  672. pg->state = SCSI_ACCESS_STATE_STANDBY;
  673. pg->expiry = 0;
  674. rcu_read_lock();
  675. list_for_each_entry_rcu(h, &pg->dh_list, node) {
  676. if (!h->sdev)
  677. continue;
  678. h->sdev->access_state =
  679. (pg->state & SCSI_ACCESS_STATE_MASK);
  680. if (pg->pref)
  681. h->sdev->access_state |=
  682. SCSI_ACCESS_STATE_PREFERRED;
  683. }
  684. rcu_read_unlock();
  685. }
  686. break;
  687. case SCSI_ACCESS_STATE_OFFLINE:
  688. /* Path unusable */
  689. err = SCSI_DH_DEV_OFFLINED;
  690. pg->expiry = 0;
  691. break;
  692. default:
  693. /* Useable path if active */
  694. err = SCSI_DH_OK;
  695. pg->expiry = 0;
  696. break;
  697. }
  698. spin_unlock_irqrestore(&pg->lock, flags);
  699. kfree(buff);
  700. return err;
  701. }
  702. /*
  703. * alua_stpg - Issue a SET TARGET PORT GROUP command
  704. *
  705. * Issue a SET TARGET PORT GROUP command and evaluate the
  706. * response. Returns SCSI_DH_RETRY per default to trigger
  707. * a re-evaluation of the target group state or SCSI_DH_OK
  708. * if no further action needs to be taken.
  709. */
  710. static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
  711. {
  712. int retval;
  713. struct scsi_sense_hdr sense_hdr;
  714. if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
  715. /* Only implicit ALUA supported, retry */
  716. return SCSI_DH_RETRY;
  717. }
  718. switch (pg->state) {
  719. case SCSI_ACCESS_STATE_OPTIMAL:
  720. return SCSI_DH_OK;
  721. case SCSI_ACCESS_STATE_ACTIVE:
  722. if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
  723. !pg->pref &&
  724. (pg->tpgs & TPGS_MODE_IMPLICIT))
  725. return SCSI_DH_OK;
  726. break;
  727. case SCSI_ACCESS_STATE_STANDBY:
  728. case SCSI_ACCESS_STATE_UNAVAILABLE:
  729. break;
  730. case SCSI_ACCESS_STATE_OFFLINE:
  731. return SCSI_DH_IO;
  732. case SCSI_ACCESS_STATE_TRANSITIONING:
  733. break;
  734. default:
  735. sdev_printk(KERN_INFO, sdev,
  736. "%s: stpg failed, unhandled TPGS state %d",
  737. ALUA_DH_NAME, pg->state);
  738. return SCSI_DH_NOSYS;
  739. }
  740. retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
  741. if (retval) {
  742. if (retval < 0 || !scsi_sense_valid(&sense_hdr)) {
  743. sdev_printk(KERN_INFO, sdev,
  744. "%s: stpg failed, result %d",
  745. ALUA_DH_NAME, retval);
  746. if (retval < 0)
  747. return SCSI_DH_DEV_TEMP_BUSY;
  748. } else {
  749. sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
  750. ALUA_DH_NAME);
  751. scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
  752. }
  753. }
  754. /* Retry RTPG */
  755. return SCSI_DH_RETRY;
  756. }
  757. /*
  758. * The caller must call scsi_device_put() on the returned pointer if it is not
  759. * NULL.
  760. */
  761. static struct scsi_device * __must_check
  762. alua_rtpg_select_sdev(struct alua_port_group *pg)
  763. {
  764. struct alua_dh_data *h;
  765. struct scsi_device *sdev = NULL, *prev_sdev;
  766. lockdep_assert_held(&pg->lock);
  767. if (WARN_ON(!pg->rtpg_sdev))
  768. return NULL;
  769. /*
  770. * RCU protection isn't necessary for dh_list here
  771. * as we hold pg->lock, but for access to h->pg.
  772. */
  773. rcu_read_lock();
  774. list_for_each_entry_rcu(h, &pg->dh_list, node) {
  775. if (!h->sdev)
  776. continue;
  777. if (h->sdev == pg->rtpg_sdev) {
  778. h->disabled = true;
  779. continue;
  780. }
  781. if (rcu_dereference(h->pg) == pg &&
  782. !h->disabled &&
  783. !scsi_device_get(h->sdev)) {
  784. sdev = h->sdev;
  785. break;
  786. }
  787. }
  788. rcu_read_unlock();
  789. if (!sdev) {
  790. pr_warn("%s: no device found for rtpg\n",
  791. (pg->device_id_len ?
  792. (char *)pg->device_id_str : "(nameless PG)"));
  793. return NULL;
  794. }
  795. sdev_printk(KERN_INFO, sdev, "rtpg retry on different device\n");
  796. prev_sdev = pg->rtpg_sdev;
  797. pg->rtpg_sdev = sdev;
  798. return prev_sdev;
  799. }
  800. static void alua_rtpg_work(struct work_struct *work)
  801. {
  802. struct alua_port_group *pg =
  803. container_of(work, struct alua_port_group, rtpg_work.work);
  804. struct scsi_device *sdev, *prev_sdev = NULL;
  805. LIST_HEAD(qdata_list);
  806. int err = SCSI_DH_OK;
  807. struct alua_queue_data *qdata, *tmp;
  808. struct alua_dh_data *h;
  809. unsigned long flags;
  810. spin_lock_irqsave(&pg->lock, flags);
  811. sdev = pg->rtpg_sdev;
  812. if (!sdev) {
  813. WARN_ON(pg->flags & ALUA_PG_RUN_RTPG);
  814. WARN_ON(pg->flags & ALUA_PG_RUN_STPG);
  815. spin_unlock_irqrestore(&pg->lock, flags);
  816. kref_put(&pg->kref, release_port_group);
  817. return;
  818. }
  819. pg->flags |= ALUA_PG_RUNNING;
  820. if (pg->flags & ALUA_PG_RUN_RTPG) {
  821. int state = pg->state;
  822. pg->flags &= ~ALUA_PG_RUN_RTPG;
  823. spin_unlock_irqrestore(&pg->lock, flags);
  824. if (state == SCSI_ACCESS_STATE_TRANSITIONING) {
  825. if (alua_tur(sdev) == SCSI_DH_RETRY) {
  826. spin_lock_irqsave(&pg->lock, flags);
  827. pg->flags &= ~ALUA_PG_RUNNING;
  828. pg->flags |= ALUA_PG_RUN_RTPG;
  829. if (!pg->interval)
  830. pg->interval = ALUA_RTPG_RETRY_DELAY;
  831. spin_unlock_irqrestore(&pg->lock, flags);
  832. queue_delayed_work(kaluad_wq, &pg->rtpg_work,
  833. pg->interval * HZ);
  834. return;
  835. }
  836. /* Send RTPG on failure or if TUR indicates SUCCESS */
  837. }
  838. err = alua_rtpg(sdev, pg);
  839. spin_lock_irqsave(&pg->lock, flags);
  840. /* If RTPG failed on the current device, try using another */
  841. if (err == SCSI_DH_RES_TEMP_UNAVAIL &&
  842. (prev_sdev = alua_rtpg_select_sdev(pg)))
  843. err = SCSI_DH_IMM_RETRY;
  844. if (err == SCSI_DH_RETRY || err == SCSI_DH_IMM_RETRY ||
  845. pg->flags & ALUA_PG_RUN_RTPG) {
  846. pg->flags &= ~ALUA_PG_RUNNING;
  847. if (err == SCSI_DH_IMM_RETRY)
  848. pg->interval = 0;
  849. else if (!pg->interval && !(pg->flags & ALUA_PG_RUN_RTPG))
  850. pg->interval = ALUA_RTPG_RETRY_DELAY;
  851. pg->flags |= ALUA_PG_RUN_RTPG;
  852. spin_unlock_irqrestore(&pg->lock, flags);
  853. goto queue_rtpg;
  854. }
  855. if (err != SCSI_DH_OK)
  856. pg->flags &= ~ALUA_PG_RUN_STPG;
  857. }
  858. if (pg->flags & ALUA_PG_RUN_STPG) {
  859. pg->flags &= ~ALUA_PG_RUN_STPG;
  860. spin_unlock_irqrestore(&pg->lock, flags);
  861. err = alua_stpg(sdev, pg);
  862. spin_lock_irqsave(&pg->lock, flags);
  863. if (err == SCSI_DH_RETRY || pg->flags & ALUA_PG_RUN_RTPG) {
  864. pg->flags |= ALUA_PG_RUN_RTPG;
  865. pg->interval = 0;
  866. pg->flags &= ~ALUA_PG_RUNNING;
  867. spin_unlock_irqrestore(&pg->lock, flags);
  868. goto queue_rtpg;
  869. }
  870. }
  871. list_splice_init(&pg->rtpg_list, &qdata_list);
  872. /*
  873. * We went through an RTPG, for good or bad.
  874. * Re-enable all devices for the next attempt.
  875. */
  876. list_for_each_entry(h, &pg->dh_list, node)
  877. h->disabled = false;
  878. pg->rtpg_sdev = NULL;
  879. spin_unlock_irqrestore(&pg->lock, flags);
  880. if (prev_sdev)
  881. scsi_device_put(prev_sdev);
  882. list_for_each_entry_safe(qdata, tmp, &qdata_list, entry) {
  883. list_del(&qdata->entry);
  884. if (qdata->callback_fn)
  885. qdata->callback_fn(qdata->callback_data, err);
  886. kfree(qdata);
  887. }
  888. spin_lock_irqsave(&pg->lock, flags);
  889. pg->flags &= ~ALUA_PG_RUNNING;
  890. spin_unlock_irqrestore(&pg->lock, flags);
  891. scsi_device_put(sdev);
  892. kref_put(&pg->kref, release_port_group);
  893. return;
  894. queue_rtpg:
  895. if (prev_sdev)
  896. scsi_device_put(prev_sdev);
  897. queue_delayed_work(kaluad_wq, &pg->rtpg_work, pg->interval * HZ);
  898. }
  899. /**
  900. * alua_rtpg_queue() - cause RTPG to be submitted asynchronously
  901. * @pg: ALUA port group associated with @sdev.
  902. * @sdev: SCSI device for which to submit an RTPG.
  903. * @qdata: Information about the callback to invoke after the RTPG.
  904. * @force: Whether or not to submit an RTPG if a work item that will submit an
  905. * RTPG already has been scheduled.
  906. *
  907. * Returns true if and only if alua_rtpg_work() will be called asynchronously.
  908. * That function is responsible for calling @qdata->fn().
  909. *
  910. * Context: may be called from atomic context (alua_check()) only if the caller
  911. * holds an sdev reference.
  912. */
  913. static bool alua_rtpg_queue(struct alua_port_group *pg,
  914. struct scsi_device *sdev,
  915. struct alua_queue_data *qdata, bool force)
  916. {
  917. int start_queue = 0;
  918. unsigned long flags;
  919. if (WARN_ON_ONCE(!pg) || scsi_device_get(sdev))
  920. return false;
  921. spin_lock_irqsave(&pg->lock, flags);
  922. if (qdata) {
  923. list_add_tail(&qdata->entry, &pg->rtpg_list);
  924. pg->flags |= ALUA_PG_RUN_STPG;
  925. force = true;
  926. }
  927. if (pg->rtpg_sdev == NULL) {
  928. struct alua_dh_data *h = sdev->handler_data;
  929. rcu_read_lock();
  930. if (h && rcu_dereference(h->pg) == pg) {
  931. pg->interval = 0;
  932. pg->flags |= ALUA_PG_RUN_RTPG;
  933. kref_get(&pg->kref);
  934. pg->rtpg_sdev = sdev;
  935. start_queue = 1;
  936. }
  937. rcu_read_unlock();
  938. } else if (!(pg->flags & ALUA_PG_RUN_RTPG) && force) {
  939. pg->flags |= ALUA_PG_RUN_RTPG;
  940. /* Do not queue if the worker is already running */
  941. if (!(pg->flags & ALUA_PG_RUNNING)) {
  942. kref_get(&pg->kref);
  943. start_queue = 1;
  944. }
  945. }
  946. spin_unlock_irqrestore(&pg->lock, flags);
  947. if (start_queue) {
  948. if (queue_delayed_work(kaluad_wq, &pg->rtpg_work,
  949. msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS)))
  950. sdev = NULL;
  951. else
  952. kref_put(&pg->kref, release_port_group);
  953. }
  954. if (sdev)
  955. scsi_device_put(sdev);
  956. return true;
  957. }
  958. /*
  959. * alua_initialize - Initialize ALUA state
  960. * @sdev: the device to be initialized
  961. *
  962. * For the prep_fn to work correctly we have
  963. * to initialize the ALUA state for the device.
  964. */
  965. static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
  966. {
  967. int err = SCSI_DH_DEV_UNSUPP, tpgs;
  968. mutex_lock(&h->init_mutex);
  969. h->disabled = false;
  970. tpgs = alua_check_tpgs(sdev);
  971. if (tpgs != TPGS_MODE_NONE)
  972. err = alua_check_vpd(sdev, h, tpgs);
  973. h->init_error = err;
  974. mutex_unlock(&h->init_mutex);
  975. return err;
  976. }
  977. /*
  978. * alua_set_params - set/unset the optimize flag
  979. * @sdev: device on the path to be activated
  980. * params - parameters in the following format
  981. * "no_of_params\0param1\0param2\0param3\0...\0"
  982. * For example, to set the flag pass the following parameters
  983. * from multipath.conf
  984. * hardware_handler "2 alua 1"
  985. */
  986. static int alua_set_params(struct scsi_device *sdev, const char *params)
  987. {
  988. struct alua_dh_data *h = sdev->handler_data;
  989. struct alua_port_group *pg = NULL;
  990. unsigned int optimize = 0, argc;
  991. const char *p = params;
  992. int result = SCSI_DH_OK;
  993. unsigned long flags;
  994. if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
  995. return -EINVAL;
  996. while (*p++)
  997. ;
  998. if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
  999. return -EINVAL;
  1000. rcu_read_lock();
  1001. pg = rcu_dereference(h->pg);
  1002. if (!pg) {
  1003. rcu_read_unlock();
  1004. return -ENXIO;
  1005. }
  1006. spin_lock_irqsave(&pg->lock, flags);
  1007. if (optimize)
  1008. pg->flags |= ALUA_OPTIMIZE_STPG;
  1009. else
  1010. pg->flags &= ~ALUA_OPTIMIZE_STPG;
  1011. spin_unlock_irqrestore(&pg->lock, flags);
  1012. rcu_read_unlock();
  1013. return result;
  1014. }
  1015. /*
  1016. * alua_activate - activate a path
  1017. * @sdev: device on the path to be activated
  1018. *
  1019. * We're currently switching the port group to be activated only and
  1020. * let the array figure out the rest.
  1021. * There may be other arrays which require us to switch all port groups
  1022. * based on a certain policy. But until we actually encounter them it
  1023. * should be okay.
  1024. */
  1025. static int alua_activate(struct scsi_device *sdev,
  1026. activate_complete fn, void *data)
  1027. {
  1028. struct alua_dh_data *h = sdev->handler_data;
  1029. int err = SCSI_DH_OK;
  1030. struct alua_queue_data *qdata;
  1031. struct alua_port_group *pg;
  1032. qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
  1033. if (!qdata) {
  1034. err = SCSI_DH_RES_TEMP_UNAVAIL;
  1035. goto out;
  1036. }
  1037. qdata->callback_fn = fn;
  1038. qdata->callback_data = data;
  1039. mutex_lock(&h->init_mutex);
  1040. rcu_read_lock();
  1041. pg = rcu_dereference(h->pg);
  1042. if (!pg || !kref_get_unless_zero(&pg->kref)) {
  1043. rcu_read_unlock();
  1044. kfree(qdata);
  1045. err = h->init_error;
  1046. mutex_unlock(&h->init_mutex);
  1047. goto out;
  1048. }
  1049. rcu_read_unlock();
  1050. mutex_unlock(&h->init_mutex);
  1051. if (alua_rtpg_queue(pg, sdev, qdata, true)) {
  1052. fn = NULL;
  1053. } else {
  1054. kfree(qdata);
  1055. err = SCSI_DH_DEV_OFFLINED;
  1056. }
  1057. kref_put(&pg->kref, release_port_group);
  1058. out:
  1059. if (fn)
  1060. fn(data, err);
  1061. return 0;
  1062. }
  1063. /*
  1064. * alua_check - check path status
  1065. * @sdev: device on the path to be checked
  1066. *
  1067. * Check the device status
  1068. */
  1069. static void alua_check(struct scsi_device *sdev, bool force)
  1070. {
  1071. struct alua_dh_data *h = sdev->handler_data;
  1072. struct alua_port_group *pg;
  1073. rcu_read_lock();
  1074. pg = rcu_dereference(h->pg);
  1075. if (!pg || !kref_get_unless_zero(&pg->kref)) {
  1076. rcu_read_unlock();
  1077. return;
  1078. }
  1079. rcu_read_unlock();
  1080. alua_rtpg_queue(pg, sdev, NULL, force);
  1081. kref_put(&pg->kref, release_port_group);
  1082. }
  1083. /*
  1084. * alua_prep_fn - request callback
  1085. *
  1086. * Fail I/O to all paths not in state
  1087. * active/optimized or active/non-optimized.
  1088. */
  1089. static blk_status_t alua_prep_fn(struct scsi_device *sdev, struct request *req)
  1090. {
  1091. struct alua_dh_data *h = sdev->handler_data;
  1092. struct alua_port_group *pg;
  1093. unsigned char state = SCSI_ACCESS_STATE_OPTIMAL;
  1094. rcu_read_lock();
  1095. pg = rcu_dereference(h->pg);
  1096. if (pg)
  1097. state = pg->state;
  1098. rcu_read_unlock();
  1099. switch (state) {
  1100. case SCSI_ACCESS_STATE_OPTIMAL:
  1101. case SCSI_ACCESS_STATE_ACTIVE:
  1102. case SCSI_ACCESS_STATE_LBA:
  1103. case SCSI_ACCESS_STATE_TRANSITIONING:
  1104. return BLK_STS_OK;
  1105. default:
  1106. req->rq_flags |= RQF_QUIET;
  1107. return BLK_STS_IOERR;
  1108. }
  1109. }
  1110. static void alua_rescan(struct scsi_device *sdev)
  1111. {
  1112. struct alua_dh_data *h = sdev->handler_data;
  1113. alua_initialize(sdev, h);
  1114. }
  1115. /*
  1116. * alua_bus_attach - Attach device handler
  1117. * @sdev: device to be attached to
  1118. */
  1119. static int alua_bus_attach(struct scsi_device *sdev)
  1120. {
  1121. struct alua_dh_data *h;
  1122. int err;
  1123. h = kzalloc(sizeof(*h) , GFP_KERNEL);
  1124. if (!h)
  1125. return SCSI_DH_NOMEM;
  1126. spin_lock_init(&h->pg_lock);
  1127. rcu_assign_pointer(h->pg, NULL);
  1128. h->init_error = SCSI_DH_OK;
  1129. h->sdev = sdev;
  1130. INIT_LIST_HEAD(&h->node);
  1131. mutex_init(&h->init_mutex);
  1132. err = alua_initialize(sdev, h);
  1133. if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
  1134. goto failed;
  1135. sdev->handler_data = h;
  1136. return SCSI_DH_OK;
  1137. failed:
  1138. kfree(h);
  1139. return err;
  1140. }
  1141. /*
  1142. * alua_bus_detach - Detach device handler
  1143. * @sdev: device to be detached from
  1144. */
  1145. static void alua_bus_detach(struct scsi_device *sdev)
  1146. {
  1147. struct alua_dh_data *h = sdev->handler_data;
  1148. struct alua_port_group *pg;
  1149. spin_lock(&h->pg_lock);
  1150. pg = rcu_dereference_protected(h->pg, lockdep_is_held(&h->pg_lock));
  1151. rcu_assign_pointer(h->pg, NULL);
  1152. spin_unlock(&h->pg_lock);
  1153. if (pg) {
  1154. spin_lock_irq(&pg->lock);
  1155. list_del_rcu(&h->node);
  1156. spin_unlock_irq(&pg->lock);
  1157. kref_put(&pg->kref, release_port_group);
  1158. }
  1159. sdev->handler_data = NULL;
  1160. synchronize_rcu();
  1161. kfree(h);
  1162. }
  1163. static struct scsi_device_handler alua_dh = {
  1164. .name = ALUA_DH_NAME,
  1165. .module = THIS_MODULE,
  1166. .attach = alua_bus_attach,
  1167. .detach = alua_bus_detach,
  1168. .prep_fn = alua_prep_fn,
  1169. .check_sense = alua_check_sense,
  1170. .activate = alua_activate,
  1171. .rescan = alua_rescan,
  1172. .set_params = alua_set_params,
  1173. };
  1174. static int __init alua_init(void)
  1175. {
  1176. int r;
  1177. kaluad_wq = alloc_workqueue("kaluad", WQ_MEM_RECLAIM, 0);
  1178. if (!kaluad_wq)
  1179. return -ENOMEM;
  1180. r = scsi_register_device_handler(&alua_dh);
  1181. if (r != 0) {
  1182. printk(KERN_ERR "%s: Failed to register scsi device handler",
  1183. ALUA_DH_NAME);
  1184. destroy_workqueue(kaluad_wq);
  1185. }
  1186. return r;
  1187. }
  1188. static void __exit alua_exit(void)
  1189. {
  1190. scsi_unregister_device_handler(&alua_dh);
  1191. destroy_workqueue(kaluad_wq);
  1192. }
  1193. module_init(alua_init);
  1194. module_exit(alua_exit);
  1195. MODULE_DESCRIPTION("DM Multipath ALUA support");
  1196. MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
  1197. MODULE_LICENSE("GPL");
  1198. MODULE_VERSION(ALUA_DH_VER);