scsi_dh_alua.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * Generic SCSI-3 ALUA SCSI Device Handler
  3. *
  4. * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_eh.h>
  27. #include <scsi/scsi_dh.h>
  28. #define ALUA_DH_NAME "alua"
  29. #define ALUA_DH_VER "1.3"
  30. #define TPGS_STATE_OPTIMIZED 0x0
  31. #define TPGS_STATE_NONOPTIMIZED 0x1
  32. #define TPGS_STATE_STANDBY 0x2
  33. #define TPGS_STATE_UNAVAILABLE 0x3
  34. #define TPGS_STATE_LBA_DEPENDENT 0x4
  35. #define TPGS_STATE_OFFLINE 0xe
  36. #define TPGS_STATE_TRANSITIONING 0xf
  37. #define TPGS_SUPPORT_NONE 0x00
  38. #define TPGS_SUPPORT_OPTIMIZED 0x01
  39. #define TPGS_SUPPORT_NONOPTIMIZED 0x02
  40. #define TPGS_SUPPORT_STANDBY 0x04
  41. #define TPGS_SUPPORT_UNAVAILABLE 0x08
  42. #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
  43. #define TPGS_SUPPORT_OFFLINE 0x40
  44. #define TPGS_SUPPORT_TRANSITION 0x80
  45. #define RTPG_FMT_MASK 0x70
  46. #define RTPG_FMT_EXT_HDR 0x10
  47. #define TPGS_MODE_UNINITIALIZED -1
  48. #define TPGS_MODE_NONE 0x0
  49. #define TPGS_MODE_IMPLICIT 0x1
  50. #define TPGS_MODE_EXPLICIT 0x2
  51. #define ALUA_INQUIRY_SIZE 36
  52. #define ALUA_FAILOVER_TIMEOUT 60
  53. #define ALUA_FAILOVER_RETRIES 5
  54. /* flags passed from user level */
  55. #define ALUA_OPTIMIZE_STPG 1
  56. struct alua_dh_data {
  57. struct scsi_dh_data dh_data;
  58. int group_id;
  59. int rel_port;
  60. int tpgs;
  61. int state;
  62. int pref;
  63. unsigned flags; /* used for optimizing STPG */
  64. unsigned char inq[ALUA_INQUIRY_SIZE];
  65. unsigned char *buff;
  66. int bufflen;
  67. unsigned char transition_tmo;
  68. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  69. int senselen;
  70. struct scsi_device *sdev;
  71. activate_complete callback_fn;
  72. void *callback_data;
  73. };
  74. #define ALUA_POLICY_SWITCH_CURRENT 0
  75. #define ALUA_POLICY_SWITCH_ALL 1
  76. static char print_alua_state(int);
  77. static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
  78. static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
  79. {
  80. return container_of(sdev->scsi_dh_data, struct alua_dh_data, dh_data);
  81. }
  82. static int realloc_buffer(struct alua_dh_data *h, unsigned len)
  83. {
  84. if (h->buff && h->buff != h->inq)
  85. kfree(h->buff);
  86. h->buff = kmalloc(len, GFP_NOIO);
  87. if (!h->buff) {
  88. h->buff = h->inq;
  89. h->bufflen = ALUA_INQUIRY_SIZE;
  90. return 1;
  91. }
  92. h->bufflen = len;
  93. return 0;
  94. }
  95. static struct request *get_alua_req(struct scsi_device *sdev,
  96. void *buffer, unsigned buflen, int rw)
  97. {
  98. struct request *rq;
  99. struct request_queue *q = sdev->request_queue;
  100. rq = blk_get_request(q, rw, GFP_NOIO);
  101. if (IS_ERR(rq)) {
  102. sdev_printk(KERN_INFO, sdev,
  103. "%s: blk_get_request failed\n", __func__);
  104. return NULL;
  105. }
  106. blk_rq_set_block_pc(rq);
  107. if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
  108. blk_put_request(rq);
  109. sdev_printk(KERN_INFO, sdev,
  110. "%s: blk_rq_map_kern failed\n", __func__);
  111. return NULL;
  112. }
  113. rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  114. REQ_FAILFAST_DRIVER;
  115. rq->retries = ALUA_FAILOVER_RETRIES;
  116. rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
  117. return rq;
  118. }
  119. /*
  120. * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
  121. * @sdev: sdev the command should be sent to
  122. */
  123. static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  124. {
  125. struct request *rq;
  126. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  127. rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
  128. if (!rq)
  129. goto done;
  130. /* Prepare the command. */
  131. rq->cmd[0] = INQUIRY;
  132. rq->cmd[1] = 1;
  133. rq->cmd[2] = 0x83;
  134. rq->cmd[4] = h->bufflen;
  135. rq->cmd_len = COMMAND_SIZE(INQUIRY);
  136. rq->sense = h->sense;
  137. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  138. rq->sense_len = h->senselen = 0;
  139. err = blk_execute_rq(rq->q, NULL, rq, 1);
  140. if (err == -EIO) {
  141. sdev_printk(KERN_INFO, sdev,
  142. "%s: evpd inquiry failed with %x\n",
  143. ALUA_DH_NAME, rq->errors);
  144. h->senselen = rq->sense_len;
  145. err = SCSI_DH_IO;
  146. }
  147. blk_put_request(rq);
  148. done:
  149. return err;
  150. }
  151. /*
  152. * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
  153. * @sdev: sdev the command should be sent to
  154. */
  155. static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h,
  156. bool rtpg_ext_hdr_req)
  157. {
  158. struct request *rq;
  159. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  160. rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
  161. if (!rq)
  162. goto done;
  163. /* Prepare the command. */
  164. rq->cmd[0] = MAINTENANCE_IN;
  165. if (rtpg_ext_hdr_req)
  166. rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
  167. else
  168. rq->cmd[1] = MI_REPORT_TARGET_PGS;
  169. rq->cmd[6] = (h->bufflen >> 24) & 0xff;
  170. rq->cmd[7] = (h->bufflen >> 16) & 0xff;
  171. rq->cmd[8] = (h->bufflen >> 8) & 0xff;
  172. rq->cmd[9] = h->bufflen & 0xff;
  173. rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
  174. rq->sense = h->sense;
  175. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  176. rq->sense_len = h->senselen = 0;
  177. err = blk_execute_rq(rq->q, NULL, rq, 1);
  178. if (err == -EIO) {
  179. sdev_printk(KERN_INFO, sdev,
  180. "%s: rtpg failed with %x\n",
  181. ALUA_DH_NAME, rq->errors);
  182. h->senselen = rq->sense_len;
  183. err = SCSI_DH_IO;
  184. }
  185. blk_put_request(rq);
  186. done:
  187. return err;
  188. }
  189. /*
  190. * alua_stpg - Evaluate SET TARGET GROUP STATES
  191. * @sdev: the device to be evaluated
  192. * @state: the new target group state
  193. *
  194. * Send a SET TARGET GROUP STATES command to the device.
  195. * We only have to test here if we should resubmit the command;
  196. * any other error is assumed as a failure.
  197. */
  198. static void stpg_endio(struct request *req, int error)
  199. {
  200. struct alua_dh_data *h = req->end_io_data;
  201. struct scsi_sense_hdr sense_hdr;
  202. unsigned err = SCSI_DH_OK;
  203. if (host_byte(req->errors) != DID_OK ||
  204. msg_byte(req->errors) != COMMAND_COMPLETE) {
  205. err = SCSI_DH_IO;
  206. goto done;
  207. }
  208. if (req->sense_len > 0) {
  209. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  210. &sense_hdr);
  211. if (!err) {
  212. err = SCSI_DH_IO;
  213. goto done;
  214. }
  215. err = alua_check_sense(h->sdev, &sense_hdr);
  216. if (err == ADD_TO_MLQUEUE) {
  217. err = SCSI_DH_RETRY;
  218. goto done;
  219. }
  220. sdev_printk(KERN_INFO, h->sdev,
  221. "%s: stpg sense code: %02x/%02x/%02x\n",
  222. ALUA_DH_NAME, sense_hdr.sense_key,
  223. sense_hdr.asc, sense_hdr.ascq);
  224. err = SCSI_DH_IO;
  225. } else if (error)
  226. err = SCSI_DH_IO;
  227. if (err == SCSI_DH_OK) {
  228. h->state = TPGS_STATE_OPTIMIZED;
  229. sdev_printk(KERN_INFO, h->sdev,
  230. "%s: port group %02x switched to state %c\n",
  231. ALUA_DH_NAME, h->group_id,
  232. print_alua_state(h->state));
  233. }
  234. done:
  235. req->end_io_data = NULL;
  236. __blk_put_request(req->q, req);
  237. if (h->callback_fn) {
  238. h->callback_fn(h->callback_data, err);
  239. h->callback_fn = h->callback_data = NULL;
  240. }
  241. return;
  242. }
  243. /*
  244. * submit_stpg - Issue a SET TARGET GROUP STATES command
  245. *
  246. * Currently we're only setting the current target port group state
  247. * to 'active/optimized' and let the array firmware figure out
  248. * the states of the remaining groups.
  249. */
  250. static unsigned submit_stpg(struct alua_dh_data *h)
  251. {
  252. struct request *rq;
  253. int stpg_len = 8;
  254. struct scsi_device *sdev = h->sdev;
  255. /* Prepare the data buffer */
  256. memset(h->buff, 0, stpg_len);
  257. h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
  258. h->buff[6] = (h->group_id >> 8) & 0xff;
  259. h->buff[7] = h->group_id & 0xff;
  260. rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
  261. if (!rq)
  262. return SCSI_DH_RES_TEMP_UNAVAIL;
  263. /* Prepare the command. */
  264. rq->cmd[0] = MAINTENANCE_OUT;
  265. rq->cmd[1] = MO_SET_TARGET_PGS;
  266. rq->cmd[6] = (stpg_len >> 24) & 0xff;
  267. rq->cmd[7] = (stpg_len >> 16) & 0xff;
  268. rq->cmd[8] = (stpg_len >> 8) & 0xff;
  269. rq->cmd[9] = stpg_len & 0xff;
  270. rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
  271. rq->sense = h->sense;
  272. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  273. rq->sense_len = h->senselen = 0;
  274. rq->end_io_data = h;
  275. blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
  276. return SCSI_DH_OK;
  277. }
  278. /*
  279. * alua_check_tpgs - Evaluate TPGS setting
  280. * @sdev: device to be checked
  281. *
  282. * Examine the TPGS setting of the sdev to find out if ALUA
  283. * is supported.
  284. */
  285. static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
  286. {
  287. int err = SCSI_DH_OK;
  288. h->tpgs = scsi_device_tpgs(sdev);
  289. switch (h->tpgs) {
  290. case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
  291. sdev_printk(KERN_INFO, sdev,
  292. "%s: supports implicit and explicit TPGS\n",
  293. ALUA_DH_NAME);
  294. break;
  295. case TPGS_MODE_EXPLICIT:
  296. sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
  297. ALUA_DH_NAME);
  298. break;
  299. case TPGS_MODE_IMPLICIT:
  300. sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
  301. ALUA_DH_NAME);
  302. break;
  303. default:
  304. h->tpgs = TPGS_MODE_NONE;
  305. sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
  306. ALUA_DH_NAME);
  307. err = SCSI_DH_DEV_UNSUPP;
  308. break;
  309. }
  310. return err;
  311. }
  312. /*
  313. * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
  314. * @sdev: device to be checked
  315. *
  316. * Extract the relative target port and the target port group
  317. * descriptor from the list of identificators.
  318. */
  319. static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  320. {
  321. int len;
  322. unsigned err;
  323. unsigned char *d;
  324. retry:
  325. err = submit_vpd_inquiry(sdev, h);
  326. if (err != SCSI_DH_OK)
  327. return err;
  328. /* Check if vpd page exceeds initial buffer */
  329. len = (h->buff[2] << 8) + h->buff[3] + 4;
  330. if (len > h->bufflen) {
  331. /* Resubmit with the correct length */
  332. if (realloc_buffer(h, len)) {
  333. sdev_printk(KERN_WARNING, sdev,
  334. "%s: kmalloc buffer failed\n",
  335. ALUA_DH_NAME);
  336. /* Temporary failure, bypass */
  337. return SCSI_DH_DEV_TEMP_BUSY;
  338. }
  339. goto retry;
  340. }
  341. /*
  342. * Now look for the correct descriptor.
  343. */
  344. d = h->buff + 4;
  345. while (d < h->buff + len) {
  346. switch (d[1] & 0xf) {
  347. case 0x4:
  348. /* Relative target port */
  349. h->rel_port = (d[6] << 8) + d[7];
  350. break;
  351. case 0x5:
  352. /* Target port group */
  353. h->group_id = (d[6] << 8) + d[7];
  354. break;
  355. default:
  356. break;
  357. }
  358. d += d[3] + 4;
  359. }
  360. if (h->group_id == -1) {
  361. /*
  362. * Internal error; TPGS supported but required
  363. * VPD identification descriptors not present.
  364. * Disable ALUA support
  365. */
  366. sdev_printk(KERN_INFO, sdev,
  367. "%s: No target port descriptors found\n",
  368. ALUA_DH_NAME);
  369. h->state = TPGS_STATE_OPTIMIZED;
  370. h->tpgs = TPGS_MODE_NONE;
  371. err = SCSI_DH_DEV_UNSUPP;
  372. } else {
  373. sdev_printk(KERN_INFO, sdev,
  374. "%s: port group %02x rel port %02x\n",
  375. ALUA_DH_NAME, h->group_id, h->rel_port);
  376. }
  377. return err;
  378. }
  379. static char print_alua_state(int state)
  380. {
  381. switch (state) {
  382. case TPGS_STATE_OPTIMIZED:
  383. return 'A';
  384. case TPGS_STATE_NONOPTIMIZED:
  385. return 'N';
  386. case TPGS_STATE_STANDBY:
  387. return 'S';
  388. case TPGS_STATE_UNAVAILABLE:
  389. return 'U';
  390. case TPGS_STATE_LBA_DEPENDENT:
  391. return 'L';
  392. case TPGS_STATE_OFFLINE:
  393. return 'O';
  394. case TPGS_STATE_TRANSITIONING:
  395. return 'T';
  396. default:
  397. return 'X';
  398. }
  399. }
  400. static int alua_check_sense(struct scsi_device *sdev,
  401. struct scsi_sense_hdr *sense_hdr)
  402. {
  403. switch (sense_hdr->sense_key) {
  404. case NOT_READY:
  405. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
  406. /*
  407. * LUN Not Accessible - ALUA state transition
  408. */
  409. return ADD_TO_MLQUEUE;
  410. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
  411. /*
  412. * LUN Not Accessible -- Target port in standby state
  413. */
  414. return SUCCESS;
  415. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
  416. /*
  417. * LUN Not Accessible -- Target port in unavailable state
  418. */
  419. return SUCCESS;
  420. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
  421. /*
  422. * LUN Not Ready -- Offline
  423. */
  424. return SUCCESS;
  425. if (sdev->allow_restart &&
  426. sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x02)
  427. /*
  428. * if the device is not started, we need to wake
  429. * the error handler to start the motor
  430. */
  431. return FAILED;
  432. break;
  433. case UNIT_ATTENTION:
  434. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
  435. /*
  436. * Power On, Reset, or Bus Device Reset, just retry.
  437. */
  438. return ADD_TO_MLQUEUE;
  439. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
  440. /*
  441. * Device internal reset
  442. */
  443. return ADD_TO_MLQUEUE;
  444. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
  445. /*
  446. * Mode Parameters Changed
  447. */
  448. return ADD_TO_MLQUEUE;
  449. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
  450. /*
  451. * ALUA state changed
  452. */
  453. return ADD_TO_MLQUEUE;
  454. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
  455. /*
  456. * Implicit ALUA state transition failed
  457. */
  458. return ADD_TO_MLQUEUE;
  459. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
  460. /*
  461. * Inquiry data has changed
  462. */
  463. return ADD_TO_MLQUEUE;
  464. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
  465. /*
  466. * REPORTED_LUNS_DATA_HAS_CHANGED is reported
  467. * when switching controllers on targets like
  468. * Intel Multi-Flex. We can just retry.
  469. */
  470. return ADD_TO_MLQUEUE;
  471. break;
  472. }
  473. return SCSI_RETURN_NOT_HANDLED;
  474. }
  475. /*
  476. * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
  477. * @sdev: the device to be evaluated.
  478. * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state
  479. *
  480. * Evaluate the Target Port Group State.
  481. * Returns SCSI_DH_DEV_OFFLINED if the path is
  482. * found to be unusable.
  483. */
  484. static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h, int wait_for_transition)
  485. {
  486. struct scsi_sense_hdr sense_hdr;
  487. int len, k, off, valid_states = 0;
  488. unsigned char *ucp;
  489. unsigned err;
  490. bool rtpg_ext_hdr_req = 1;
  491. unsigned long expiry, interval = 0;
  492. unsigned int tpg_desc_tbl_off;
  493. unsigned char orig_transition_tmo;
  494. if (!h->transition_tmo)
  495. expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
  496. else
  497. expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
  498. retry:
  499. err = submit_rtpg(sdev, h, rtpg_ext_hdr_req);
  500. if (err == SCSI_DH_IO && h->senselen > 0) {
  501. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  502. &sense_hdr);
  503. if (!err)
  504. return SCSI_DH_IO;
  505. /*
  506. * submit_rtpg() has failed on existing arrays
  507. * when requesting extended header info, and
  508. * the array doesn't support extended headers,
  509. * even though it shouldn't according to T10.
  510. * The retry without rtpg_ext_hdr_req set
  511. * handles this.
  512. */
  513. if (rtpg_ext_hdr_req == 1 &&
  514. sense_hdr.sense_key == ILLEGAL_REQUEST &&
  515. sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
  516. rtpg_ext_hdr_req = 0;
  517. goto retry;
  518. }
  519. err = alua_check_sense(sdev, &sense_hdr);
  520. if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
  521. goto retry;
  522. sdev_printk(KERN_INFO, sdev,
  523. "%s: rtpg sense code %02x/%02x/%02x\n",
  524. ALUA_DH_NAME, sense_hdr.sense_key,
  525. sense_hdr.asc, sense_hdr.ascq);
  526. err = SCSI_DH_IO;
  527. }
  528. if (err != SCSI_DH_OK)
  529. return err;
  530. len = (h->buff[0] << 24) + (h->buff[1] << 16) +
  531. (h->buff[2] << 8) + h->buff[3] + 4;
  532. if (len > h->bufflen) {
  533. /* Resubmit with the correct length */
  534. if (realloc_buffer(h, len)) {
  535. sdev_printk(KERN_WARNING, sdev,
  536. "%s: kmalloc buffer failed\n",__func__);
  537. /* Temporary failure, bypass */
  538. return SCSI_DH_DEV_TEMP_BUSY;
  539. }
  540. goto retry;
  541. }
  542. orig_transition_tmo = h->transition_tmo;
  543. if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
  544. h->transition_tmo = h->buff[5];
  545. else
  546. h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
  547. if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) {
  548. sdev_printk(KERN_INFO, sdev,
  549. "%s: transition timeout set to %d seconds\n",
  550. ALUA_DH_NAME, h->transition_tmo);
  551. expiry = jiffies + h->transition_tmo * HZ;
  552. }
  553. if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
  554. tpg_desc_tbl_off = 8;
  555. else
  556. tpg_desc_tbl_off = 4;
  557. for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
  558. k < len;
  559. k += off, ucp += off) {
  560. if (h->group_id == (ucp[2] << 8) + ucp[3]) {
  561. h->state = ucp[0] & 0x0f;
  562. h->pref = ucp[0] >> 7;
  563. valid_states = ucp[1];
  564. }
  565. off = 8 + (ucp[7] * 4);
  566. }
  567. sdev_printk(KERN_INFO, sdev,
  568. "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
  569. ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
  570. h->pref ? "preferred" : "non-preferred",
  571. valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
  572. valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
  573. valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
  574. valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
  575. valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
  576. valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
  577. valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
  578. switch (h->state) {
  579. case TPGS_STATE_TRANSITIONING:
  580. if (wait_for_transition) {
  581. if (time_before(jiffies, expiry)) {
  582. /* State transition, retry */
  583. interval += 2000;
  584. msleep(interval);
  585. goto retry;
  586. }
  587. err = SCSI_DH_RETRY;
  588. } else {
  589. err = SCSI_DH_OK;
  590. }
  591. /* Transitioning time exceeded, set port to standby */
  592. h->state = TPGS_STATE_STANDBY;
  593. break;
  594. case TPGS_STATE_OFFLINE:
  595. /* Path unusable */
  596. err = SCSI_DH_DEV_OFFLINED;
  597. break;
  598. default:
  599. /* Useable path if active */
  600. err = SCSI_DH_OK;
  601. break;
  602. }
  603. return err;
  604. }
  605. /*
  606. * alua_initialize - Initialize ALUA state
  607. * @sdev: the device to be initialized
  608. *
  609. * For the prep_fn to work correctly we have
  610. * to initialize the ALUA state for the device.
  611. */
  612. static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
  613. {
  614. int err;
  615. err = alua_check_tpgs(sdev, h);
  616. if (err != SCSI_DH_OK)
  617. goto out;
  618. err = alua_vpd_inquiry(sdev, h);
  619. if (err != SCSI_DH_OK)
  620. goto out;
  621. err = alua_rtpg(sdev, h, 0);
  622. if (err != SCSI_DH_OK)
  623. goto out;
  624. out:
  625. return err;
  626. }
  627. /*
  628. * alua_set_params - set/unset the optimize flag
  629. * @sdev: device on the path to be activated
  630. * params - parameters in the following format
  631. * "no_of_params\0param1\0param2\0param3\0...\0"
  632. * For example, to set the flag pass the following parameters
  633. * from multipath.conf
  634. * hardware_handler "2 alua 1"
  635. */
  636. static int alua_set_params(struct scsi_device *sdev, const char *params)
  637. {
  638. struct alua_dh_data *h = get_alua_data(sdev);
  639. unsigned int optimize = 0, argc;
  640. const char *p = params;
  641. int result = SCSI_DH_OK;
  642. if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
  643. return -EINVAL;
  644. while (*p++)
  645. ;
  646. if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
  647. return -EINVAL;
  648. if (optimize)
  649. h->flags |= ALUA_OPTIMIZE_STPG;
  650. else
  651. h->flags &= ~ALUA_OPTIMIZE_STPG;
  652. return result;
  653. }
  654. static uint optimize_stpg;
  655. module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
  656. 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.");
  657. /*
  658. * alua_activate - activate a path
  659. * @sdev: device on the path to be activated
  660. *
  661. * We're currently switching the port group to be activated only and
  662. * let the array figure out the rest.
  663. * There may be other arrays which require us to switch all port groups
  664. * based on a certain policy. But until we actually encounter them it
  665. * should be okay.
  666. */
  667. static int alua_activate(struct scsi_device *sdev,
  668. activate_complete fn, void *data)
  669. {
  670. struct alua_dh_data *h = get_alua_data(sdev);
  671. int err = SCSI_DH_OK;
  672. int stpg = 0;
  673. err = alua_rtpg(sdev, h, 1);
  674. if (err != SCSI_DH_OK)
  675. goto out;
  676. if (optimize_stpg)
  677. h->flags |= ALUA_OPTIMIZE_STPG;
  678. if (h->tpgs & TPGS_MODE_EXPLICIT) {
  679. switch (h->state) {
  680. case TPGS_STATE_NONOPTIMIZED:
  681. stpg = 1;
  682. if ((h->flags & ALUA_OPTIMIZE_STPG) &&
  683. (!h->pref) &&
  684. (h->tpgs & TPGS_MODE_IMPLICIT))
  685. stpg = 0;
  686. break;
  687. case TPGS_STATE_STANDBY:
  688. case TPGS_STATE_UNAVAILABLE:
  689. stpg = 1;
  690. break;
  691. case TPGS_STATE_OFFLINE:
  692. err = SCSI_DH_IO;
  693. break;
  694. case TPGS_STATE_TRANSITIONING:
  695. err = SCSI_DH_RETRY;
  696. break;
  697. default:
  698. break;
  699. }
  700. }
  701. if (stpg) {
  702. h->callback_fn = fn;
  703. h->callback_data = data;
  704. err = submit_stpg(h);
  705. if (err == SCSI_DH_OK)
  706. return 0;
  707. h->callback_fn = h->callback_data = NULL;
  708. }
  709. out:
  710. if (fn)
  711. fn(data, err);
  712. return 0;
  713. }
  714. /*
  715. * alua_prep_fn - request callback
  716. *
  717. * Fail I/O to all paths not in state
  718. * active/optimized or active/non-optimized.
  719. */
  720. static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
  721. {
  722. struct alua_dh_data *h = get_alua_data(sdev);
  723. int ret = BLKPREP_OK;
  724. if (h->state == TPGS_STATE_TRANSITIONING)
  725. ret = BLKPREP_DEFER;
  726. else if (h->state != TPGS_STATE_OPTIMIZED &&
  727. h->state != TPGS_STATE_NONOPTIMIZED &&
  728. h->state != TPGS_STATE_LBA_DEPENDENT) {
  729. ret = BLKPREP_KILL;
  730. req->cmd_flags |= REQ_QUIET;
  731. }
  732. return ret;
  733. }
  734. static bool alua_match(struct scsi_device *sdev)
  735. {
  736. return (scsi_device_tpgs(sdev) != 0);
  737. }
  738. /*
  739. * alua_bus_attach - Attach device handler
  740. * @sdev: device to be attached to
  741. */
  742. static struct scsi_dh_data *alua_bus_attach(struct scsi_device *sdev)
  743. {
  744. struct alua_dh_data *h;
  745. int err;
  746. h = kzalloc(sizeof(*h) , GFP_KERNEL);
  747. if (!h)
  748. return ERR_PTR(-ENOMEM);
  749. h->tpgs = TPGS_MODE_UNINITIALIZED;
  750. h->state = TPGS_STATE_OPTIMIZED;
  751. h->group_id = -1;
  752. h->rel_port = -1;
  753. h->buff = h->inq;
  754. h->bufflen = ALUA_INQUIRY_SIZE;
  755. h->sdev = sdev;
  756. err = alua_initialize(sdev, h);
  757. if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
  758. goto failed;
  759. sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
  760. return &h->dh_data;
  761. failed:
  762. kfree(h);
  763. return ERR_PTR(-EINVAL);
  764. }
  765. /*
  766. * alua_bus_detach - Detach device handler
  767. * @sdev: device to be detached from
  768. */
  769. static void alua_bus_detach(struct scsi_device *sdev)
  770. {
  771. struct alua_dh_data *h = get_alua_data(sdev);
  772. if (h->buff && h->inq != h->buff)
  773. kfree(h->buff);
  774. kfree(h);
  775. }
  776. static struct scsi_device_handler alua_dh = {
  777. .name = ALUA_DH_NAME,
  778. .module = THIS_MODULE,
  779. .attach = alua_bus_attach,
  780. .detach = alua_bus_detach,
  781. .prep_fn = alua_prep_fn,
  782. .check_sense = alua_check_sense,
  783. .activate = alua_activate,
  784. .set_params = alua_set_params,
  785. .match = alua_match,
  786. };
  787. static int __init alua_init(void)
  788. {
  789. int r;
  790. r = scsi_register_device_handler(&alua_dh);
  791. if (r != 0)
  792. printk(KERN_ERR "%s: Failed to register scsi device handler",
  793. ALUA_DH_NAME);
  794. return r;
  795. }
  796. static void __exit alua_exit(void)
  797. {
  798. scsi_unregister_device_handler(&alua_dh);
  799. }
  800. module_init(alua_init);
  801. module_exit(alua_exit);
  802. MODULE_DESCRIPTION("DM Multipath ALUA support");
  803. MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
  804. MODULE_LICENSE("GPL");
  805. MODULE_VERSION(ALUA_DH_VER);