scsi_dh_alua.c 22 KB

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