scsi_dh_alua.c 20 KB

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