ses.c 21 KB

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