scsi_dh_alua.c 23 KB

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