scsi_dh_alua.c 21 KB

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