ses.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * SCSI Enclosure Services
  3. *
  4. * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
  5. *
  6. **-----------------------------------------------------------------------------
  7. **
  8. ** This program is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License
  10. ** version 2 as published by the Free Software Foundation.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/enclosure.h>
  27. #include <asm/unaligned.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_dbg.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_driver.h>
  33. #include <scsi/scsi_host.h>
  34. struct ses_device {
  35. unsigned char *page1;
  36. unsigned char *page1_types;
  37. unsigned char *page2;
  38. unsigned char *page10;
  39. short page1_len;
  40. short page1_num_types;
  41. short page2_len;
  42. short page10_len;
  43. };
  44. struct ses_component {
  45. u64 addr;
  46. };
  47. static int ses_probe(struct device *dev)
  48. {
  49. struct scsi_device *sdev = to_scsi_device(dev);
  50. int err = -ENODEV;
  51. if (sdev->type != TYPE_ENCLOSURE)
  52. goto out;
  53. err = 0;
  54. sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
  55. out:
  56. return err;
  57. }
  58. #define SES_TIMEOUT (30 * HZ)
  59. #define SES_RETRIES 3
  60. static void init_device_slot_control(unsigned char *dest_desc,
  61. struct enclosure_component *ecomp,
  62. unsigned char *status)
  63. {
  64. memcpy(dest_desc, status, 4);
  65. dest_desc[0] = 0;
  66. /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
  67. if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
  68. dest_desc[1] = 0;
  69. dest_desc[2] &= 0xde;
  70. dest_desc[3] &= 0x3c;
  71. }
  72. static int ses_recv_diag(struct scsi_device *sdev, int page_code,
  73. void *buf, int bufflen)
  74. {
  75. unsigned char cmd[] = {
  76. RECEIVE_DIAGNOSTIC,
  77. 1, /* Set PCV bit */
  78. page_code,
  79. bufflen >> 8,
  80. bufflen & 0xff,
  81. 0
  82. };
  83. return scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
  84. NULL, SES_TIMEOUT, SES_RETRIES, NULL);
  85. }
  86. static int ses_send_diag(struct scsi_device *sdev, int page_code,
  87. void *buf, int bufflen)
  88. {
  89. u32 result;
  90. unsigned char cmd[] = {
  91. SEND_DIAGNOSTIC,
  92. 0x10, /* Set PF bit */
  93. 0,
  94. bufflen >> 8,
  95. bufflen & 0xff,
  96. 0
  97. };
  98. result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
  99. NULL, SES_TIMEOUT, SES_RETRIES, NULL);
  100. if (result)
  101. sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
  102. result);
  103. return result;
  104. }
  105. static int ses_set_page2_descriptor(struct enclosure_device *edev,
  106. struct enclosure_component *ecomp,
  107. unsigned char *desc)
  108. {
  109. int i, j, count = 0, descriptor = ecomp->number;
  110. struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
  111. struct ses_device *ses_dev = edev->scratch;
  112. unsigned char *type_ptr = ses_dev->page1_types;
  113. unsigned char *desc_ptr = ses_dev->page2 + 8;
  114. /* Clear everything */
  115. memset(desc_ptr, 0, ses_dev->page2_len - 8);
  116. for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
  117. for (j = 0; j < type_ptr[1]; j++) {
  118. desc_ptr += 4;
  119. if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
  120. type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  121. continue;
  122. if (count++ == descriptor) {
  123. memcpy(desc_ptr, desc, 4);
  124. /* set select */
  125. desc_ptr[0] |= 0x80;
  126. /* clear reserved, just in case */
  127. desc_ptr[0] &= 0xf0;
  128. }
  129. }
  130. }
  131. return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
  132. }
  133. static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
  134. struct enclosure_component *ecomp)
  135. {
  136. int i, j, count = 0, descriptor = ecomp->number;
  137. struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
  138. struct ses_device *ses_dev = edev->scratch;
  139. unsigned char *type_ptr = ses_dev->page1_types;
  140. unsigned char *desc_ptr = ses_dev->page2 + 8;
  141. ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
  142. for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
  143. for (j = 0; j < type_ptr[1]; j++) {
  144. desc_ptr += 4;
  145. if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
  146. type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  147. continue;
  148. if (count++ == descriptor)
  149. return desc_ptr;
  150. }
  151. }
  152. return NULL;
  153. }
  154. /* For device slot and array device slot elements, byte 3 bit 6
  155. * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
  156. * code stands these bits are shifted 4 positions right so in
  157. * sysfs they will appear as bits 2 and 1 respectively. Strange. */
  158. static void ses_get_fault(struct enclosure_device *edev,
  159. struct enclosure_component *ecomp)
  160. {
  161. unsigned char *desc;
  162. desc = ses_get_page2_descriptor(edev, ecomp);
  163. if (desc)
  164. ecomp->fault = (desc[3] & 0x60) >> 4;
  165. }
  166. static int ses_set_fault(struct enclosure_device *edev,
  167. struct enclosure_component *ecomp,
  168. enum enclosure_component_setting val)
  169. {
  170. unsigned char desc[4];
  171. unsigned char *desc_ptr;
  172. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  173. if (!desc_ptr)
  174. return -EIO;
  175. init_device_slot_control(desc, ecomp, desc_ptr);
  176. switch (val) {
  177. case ENCLOSURE_SETTING_DISABLED:
  178. desc[3] &= 0xdf;
  179. break;
  180. case ENCLOSURE_SETTING_ENABLED:
  181. desc[3] |= 0x20;
  182. break;
  183. default:
  184. /* SES doesn't do the SGPIO blink settings */
  185. return -EINVAL;
  186. }
  187. return ses_set_page2_descriptor(edev, ecomp, desc);
  188. }
  189. static void ses_get_status(struct enclosure_device *edev,
  190. struct enclosure_component *ecomp)
  191. {
  192. unsigned char *desc;
  193. desc = ses_get_page2_descriptor(edev, ecomp);
  194. if (desc)
  195. ecomp->status = (desc[0] & 0x0f);
  196. }
  197. static void ses_get_locate(struct enclosure_device *edev,
  198. struct enclosure_component *ecomp)
  199. {
  200. unsigned char *desc;
  201. desc = ses_get_page2_descriptor(edev, ecomp);
  202. if (desc)
  203. ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
  204. }
  205. static int ses_set_locate(struct enclosure_device *edev,
  206. struct enclosure_component *ecomp,
  207. enum enclosure_component_setting val)
  208. {
  209. unsigned char desc[4];
  210. unsigned char *desc_ptr;
  211. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  212. if (!desc_ptr)
  213. return -EIO;
  214. init_device_slot_control(desc, ecomp, desc_ptr);
  215. switch (val) {
  216. case ENCLOSURE_SETTING_DISABLED:
  217. desc[2] &= 0xfd;
  218. break;
  219. case ENCLOSURE_SETTING_ENABLED:
  220. desc[2] |= 0x02;
  221. break;
  222. default:
  223. /* SES doesn't do the SGPIO blink settings */
  224. return -EINVAL;
  225. }
  226. return ses_set_page2_descriptor(edev, ecomp, desc);
  227. }
  228. static int ses_set_active(struct enclosure_device *edev,
  229. struct enclosure_component *ecomp,
  230. enum enclosure_component_setting val)
  231. {
  232. unsigned char desc[4];
  233. unsigned char *desc_ptr;
  234. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  235. if (!desc_ptr)
  236. return -EIO;
  237. init_device_slot_control(desc, ecomp, desc_ptr);
  238. switch (val) {
  239. case ENCLOSURE_SETTING_DISABLED:
  240. desc[2] &= 0x7f;
  241. ecomp->active = 0;
  242. break;
  243. case ENCLOSURE_SETTING_ENABLED:
  244. desc[2] |= 0x80;
  245. ecomp->active = 1;
  246. break;
  247. default:
  248. /* SES doesn't do the SGPIO blink settings */
  249. return -EINVAL;
  250. }
  251. return ses_set_page2_descriptor(edev, ecomp, desc);
  252. }
  253. static int ses_show_id(struct enclosure_device *edev, char *buf)
  254. {
  255. struct ses_device *ses_dev = edev->scratch;
  256. unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
  257. return sprintf(buf, "%#llx\n", id);
  258. }
  259. static void ses_get_power_status(struct enclosure_device *edev,
  260. struct enclosure_component *ecomp)
  261. {
  262. unsigned char *desc;
  263. desc = ses_get_page2_descriptor(edev, ecomp);
  264. if (desc)
  265. ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
  266. }
  267. static int ses_set_power_status(struct enclosure_device *edev,
  268. struct enclosure_component *ecomp,
  269. int val)
  270. {
  271. unsigned char desc[4];
  272. unsigned char *desc_ptr;
  273. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  274. if (!desc_ptr)
  275. return -EIO;
  276. init_device_slot_control(desc, ecomp, desc_ptr);
  277. switch (val) {
  278. /* power = 1 is device_off = 0 and vice versa */
  279. case 0:
  280. desc[3] |= 0x10;
  281. break;
  282. case 1:
  283. desc[3] &= 0xef;
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. ecomp->power_status = val;
  289. return ses_set_page2_descriptor(edev, ecomp, desc);
  290. }
  291. static struct enclosure_component_callbacks ses_enclosure_callbacks = {
  292. .get_fault = ses_get_fault,
  293. .set_fault = ses_set_fault,
  294. .get_status = ses_get_status,
  295. .get_locate = ses_get_locate,
  296. .set_locate = ses_set_locate,
  297. .get_power_status = ses_get_power_status,
  298. .set_power_status = ses_set_power_status,
  299. .set_active = ses_set_active,
  300. .show_id = ses_show_id,
  301. };
  302. struct ses_host_edev {
  303. struct Scsi_Host *shost;
  304. struct enclosure_device *edev;
  305. };
  306. #if 0
  307. int ses_match_host(struct enclosure_device *edev, void *data)
  308. {
  309. struct ses_host_edev *sed = data;
  310. struct scsi_device *sdev;
  311. if (!scsi_is_sdev_device(edev->edev.parent))
  312. return 0;
  313. sdev = to_scsi_device(edev->edev.parent);
  314. if (sdev->host != sed->shost)
  315. return 0;
  316. sed->edev = edev;
  317. return 1;
  318. }
  319. #endif /* 0 */
  320. static void ses_process_descriptor(struct enclosure_component *ecomp,
  321. unsigned char *desc)
  322. {
  323. int eip = desc[0] & 0x10;
  324. int invalid = desc[0] & 0x80;
  325. enum scsi_protocol proto = desc[0] & 0x0f;
  326. u64 addr = 0;
  327. int slot = -1;
  328. struct ses_component *scomp = ecomp->scratch;
  329. unsigned char *d;
  330. if (invalid)
  331. return;
  332. switch (proto) {
  333. case SCSI_PROTOCOL_FCP:
  334. if (eip) {
  335. d = desc + 4;
  336. slot = d[3];
  337. }
  338. break;
  339. case SCSI_PROTOCOL_SAS:
  340. if (eip) {
  341. d = desc + 4;
  342. slot = d[3];
  343. d = desc + 8;
  344. } else
  345. d = desc + 4;
  346. /* only take the phy0 addr */
  347. addr = (u64)d[12] << 56 |
  348. (u64)d[13] << 48 |
  349. (u64)d[14] << 40 |
  350. (u64)d[15] << 32 |
  351. (u64)d[16] << 24 |
  352. (u64)d[17] << 16 |
  353. (u64)d[18] << 8 |
  354. (u64)d[19];
  355. break;
  356. default:
  357. /* FIXME: Need to add more protocols than just SAS */
  358. break;
  359. }
  360. ecomp->slot = slot;
  361. scomp->addr = addr;
  362. }
  363. struct efd {
  364. u64 addr;
  365. struct device *dev;
  366. };
  367. static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
  368. void *data)
  369. {
  370. struct efd *efd = data;
  371. int i;
  372. struct ses_component *scomp;
  373. if (!edev->component[0].scratch)
  374. return 0;
  375. for (i = 0; i < edev->components; i++) {
  376. scomp = edev->component[i].scratch;
  377. if (scomp->addr != efd->addr)
  378. continue;
  379. if (enclosure_add_device(edev, i, efd->dev) == 0)
  380. kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
  381. return 1;
  382. }
  383. return 0;
  384. }
  385. #define INIT_ALLOC_SIZE 32
  386. static void ses_enclosure_data_process(struct enclosure_device *edev,
  387. struct scsi_device *sdev,
  388. int create)
  389. {
  390. u32 result;
  391. unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
  392. int i, j, page7_len, len, components;
  393. struct ses_device *ses_dev = edev->scratch;
  394. int types = ses_dev->page1_num_types;
  395. unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
  396. if (!hdr_buf)
  397. goto simple_populate;
  398. /* re-read page 10 */
  399. if (ses_dev->page10)
  400. ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
  401. /* Page 7 for the descriptors is optional */
  402. result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
  403. if (result)
  404. goto simple_populate;
  405. page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  406. /* add 1 for trailing '\0' we'll use */
  407. buf = kzalloc(len + 1, GFP_KERNEL);
  408. if (!buf)
  409. goto simple_populate;
  410. result = ses_recv_diag(sdev, 7, buf, len);
  411. if (result) {
  412. simple_populate:
  413. kfree(buf);
  414. buf = NULL;
  415. desc_ptr = NULL;
  416. len = 0;
  417. page7_len = 0;
  418. } else {
  419. desc_ptr = buf + 8;
  420. len = (desc_ptr[2] << 8) + desc_ptr[3];
  421. /* skip past overall descriptor */
  422. desc_ptr += len + 4;
  423. }
  424. if (ses_dev->page10)
  425. addl_desc_ptr = ses_dev->page10 + 8;
  426. type_ptr = ses_dev->page1_types;
  427. components = 0;
  428. for (i = 0; i < types; i++, type_ptr += 4) {
  429. for (j = 0; j < type_ptr[1]; j++) {
  430. char *name = NULL;
  431. struct enclosure_component *ecomp;
  432. if (desc_ptr) {
  433. if (desc_ptr >= buf + page7_len) {
  434. desc_ptr = NULL;
  435. } else {
  436. len = (desc_ptr[2] << 8) + desc_ptr[3];
  437. desc_ptr += 4;
  438. /* Add trailing zero - pushes into
  439. * reserved space */
  440. desc_ptr[len] = '\0';
  441. name = desc_ptr;
  442. }
  443. }
  444. if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  445. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
  446. if (create)
  447. ecomp = enclosure_component_alloc(
  448. edev,
  449. components++,
  450. type_ptr[0],
  451. name);
  452. else
  453. ecomp = &edev->component[components++];
  454. if (!IS_ERR(ecomp)) {
  455. ses_get_power_status(edev, ecomp);
  456. if (addl_desc_ptr)
  457. ses_process_descriptor(
  458. ecomp,
  459. addl_desc_ptr);
  460. if (create)
  461. enclosure_component_register(
  462. ecomp);
  463. }
  464. }
  465. if (desc_ptr)
  466. desc_ptr += len;
  467. if (addl_desc_ptr)
  468. addl_desc_ptr += addl_desc_ptr[1] + 2;
  469. }
  470. }
  471. kfree(buf);
  472. kfree(hdr_buf);
  473. }
  474. static void ses_match_to_enclosure(struct enclosure_device *edev,
  475. struct scsi_device *sdev)
  476. {
  477. unsigned char *desc;
  478. struct efd efd = {
  479. .addr = 0,
  480. };
  481. ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
  482. if (!sdev->vpd_pg83_len)
  483. return;
  484. desc = sdev->vpd_pg83 + 4;
  485. while (desc < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
  486. enum scsi_protocol proto = desc[0] >> 4;
  487. u8 code_set = desc[0] & 0x0f;
  488. u8 piv = desc[1] & 0x80;
  489. u8 assoc = (desc[1] & 0x30) >> 4;
  490. u8 type = desc[1] & 0x0f;
  491. u8 len = desc[3];
  492. if (piv && code_set == 1 && assoc == 1
  493. && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
  494. efd.addr = get_unaligned_be64(&desc[4]);
  495. desc += len + 4;
  496. }
  497. if (efd.addr) {
  498. efd.dev = &sdev->sdev_gendev;
  499. enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
  500. }
  501. }
  502. static int ses_intf_add(struct device *cdev,
  503. struct class_interface *intf)
  504. {
  505. struct scsi_device *sdev = to_scsi_device(cdev->parent);
  506. struct scsi_device *tmp_sdev;
  507. unsigned char *buf = NULL, *hdr_buf, *type_ptr;
  508. struct ses_device *ses_dev;
  509. u32 result;
  510. int i, types, len, components = 0;
  511. int err = -ENOMEM;
  512. int num_enclosures;
  513. struct enclosure_device *edev;
  514. struct ses_component *scomp = NULL;
  515. if (!scsi_device_enclosure(sdev)) {
  516. /* not an enclosure, but might be in one */
  517. struct enclosure_device *prev = NULL;
  518. while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
  519. ses_match_to_enclosure(edev, sdev);
  520. prev = edev;
  521. }
  522. return -ENODEV;
  523. }
  524. /* TYPE_ENCLOSURE prints a message in probe */
  525. if (sdev->type != TYPE_ENCLOSURE)
  526. sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
  527. ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
  528. hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
  529. if (!hdr_buf || !ses_dev)
  530. goto err_init_free;
  531. result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
  532. if (result)
  533. goto recv_failed;
  534. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  535. buf = kzalloc(len, GFP_KERNEL);
  536. if (!buf)
  537. goto err_free;
  538. result = ses_recv_diag(sdev, 1, buf, len);
  539. if (result)
  540. goto recv_failed;
  541. types = 0;
  542. /* we always have one main enclosure and the rest are referred
  543. * to as secondary subenclosures */
  544. num_enclosures = buf[1] + 1;
  545. /* begin at the enclosure descriptor */
  546. type_ptr = buf + 8;
  547. /* skip all the enclosure descriptors */
  548. for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
  549. types += type_ptr[2];
  550. type_ptr += type_ptr[3] + 4;
  551. }
  552. ses_dev->page1_types = type_ptr;
  553. ses_dev->page1_num_types = types;
  554. for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
  555. if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  556. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  557. components += type_ptr[1];
  558. }
  559. ses_dev->page1 = buf;
  560. ses_dev->page1_len = len;
  561. buf = NULL;
  562. result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
  563. if (result)
  564. goto recv_failed;
  565. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  566. buf = kzalloc(len, GFP_KERNEL);
  567. if (!buf)
  568. goto err_free;
  569. /* make sure getting page 2 actually works */
  570. result = ses_recv_diag(sdev, 2, buf, len);
  571. if (result)
  572. goto recv_failed;
  573. ses_dev->page2 = buf;
  574. ses_dev->page2_len = len;
  575. buf = NULL;
  576. /* The additional information page --- allows us
  577. * to match up the devices */
  578. result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
  579. if (!result) {
  580. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  581. buf = kzalloc(len, GFP_KERNEL);
  582. if (!buf)
  583. goto err_free;
  584. result = ses_recv_diag(sdev, 10, buf, len);
  585. if (result)
  586. goto recv_failed;
  587. ses_dev->page10 = buf;
  588. ses_dev->page10_len = len;
  589. buf = NULL;
  590. }
  591. scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
  592. if (!scomp)
  593. goto err_free;
  594. edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
  595. components, &ses_enclosure_callbacks);
  596. if (IS_ERR(edev)) {
  597. err = PTR_ERR(edev);
  598. goto err_free;
  599. }
  600. kfree(hdr_buf);
  601. edev->scratch = ses_dev;
  602. for (i = 0; i < components; i++)
  603. edev->component[i].scratch = scomp + i;
  604. ses_enclosure_data_process(edev, sdev, 1);
  605. /* see if there are any devices matching before
  606. * we found the enclosure */
  607. shost_for_each_device(tmp_sdev, sdev->host) {
  608. if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
  609. continue;
  610. ses_match_to_enclosure(edev, tmp_sdev);
  611. }
  612. return 0;
  613. recv_failed:
  614. sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
  615. result);
  616. err = -ENODEV;
  617. err_free:
  618. kfree(buf);
  619. kfree(scomp);
  620. kfree(ses_dev->page10);
  621. kfree(ses_dev->page2);
  622. kfree(ses_dev->page1);
  623. err_init_free:
  624. kfree(ses_dev);
  625. kfree(hdr_buf);
  626. sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
  627. return err;
  628. }
  629. static int ses_remove(struct device *dev)
  630. {
  631. return 0;
  632. }
  633. static void ses_intf_remove_component(struct scsi_device *sdev)
  634. {
  635. struct enclosure_device *edev, *prev = NULL;
  636. while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
  637. prev = edev;
  638. if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
  639. break;
  640. }
  641. if (edev)
  642. put_device(&edev->edev);
  643. }
  644. static void ses_intf_remove_enclosure(struct scsi_device *sdev)
  645. {
  646. struct enclosure_device *edev;
  647. struct ses_device *ses_dev;
  648. /* exact match to this enclosure */
  649. edev = enclosure_find(&sdev->sdev_gendev, NULL);
  650. if (!edev)
  651. return;
  652. ses_dev = edev->scratch;
  653. edev->scratch = NULL;
  654. kfree(ses_dev->page10);
  655. kfree(ses_dev->page1);
  656. kfree(ses_dev->page2);
  657. kfree(ses_dev);
  658. kfree(edev->component[0].scratch);
  659. put_device(&edev->edev);
  660. enclosure_unregister(edev);
  661. }
  662. static void ses_intf_remove(struct device *cdev,
  663. struct class_interface *intf)
  664. {
  665. struct scsi_device *sdev = to_scsi_device(cdev->parent);
  666. if (!scsi_device_enclosure(sdev))
  667. ses_intf_remove_component(sdev);
  668. else
  669. ses_intf_remove_enclosure(sdev);
  670. }
  671. static struct class_interface ses_interface = {
  672. .add_dev = ses_intf_add,
  673. .remove_dev = ses_intf_remove,
  674. };
  675. static struct scsi_driver ses_template = {
  676. .gendrv = {
  677. .name = "ses",
  678. .owner = THIS_MODULE,
  679. .probe = ses_probe,
  680. .remove = ses_remove,
  681. },
  682. };
  683. static int __init ses_init(void)
  684. {
  685. int err;
  686. err = scsi_register_interface(&ses_interface);
  687. if (err)
  688. return err;
  689. err = scsi_register_driver(&ses_template.gendrv);
  690. if (err)
  691. goto out_unreg;
  692. return 0;
  693. out_unreg:
  694. scsi_unregister_interface(&ses_interface);
  695. return err;
  696. }
  697. static void __exit ses_exit(void)
  698. {
  699. scsi_unregister_driver(&ses_template.gendrv);
  700. scsi_unregister_interface(&ses_interface);
  701. }
  702. module_init(ses_init);
  703. module_exit(ses_exit);
  704. MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
  705. MODULE_AUTHOR("James Bottomley");
  706. MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
  707. MODULE_LICENSE("GPL v2");