sas_discover.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * Serial Attached SCSI (SAS) Discover process
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <linux/pci.h>
  25. #include <linux/scatterlist.h>
  26. #include <scsi/scsi_host.h>
  27. #include <scsi/scsi_eh.h>
  28. #include "sas_internal.h"
  29. #include <scsi/scsi_transport.h>
  30. #include <scsi/scsi_transport_sas.h>
  31. #include "../scsi_sas_internal.h"
  32. /* ---------- Basic task processing for discovery purposes ---------- */
  33. void sas_init_dev(struct domain_device *dev)
  34. {
  35. INIT_LIST_HEAD(&dev->siblings);
  36. INIT_LIST_HEAD(&dev->dev_list_node);
  37. switch (dev->dev_type) {
  38. case SAS_END_DEV:
  39. break;
  40. case EDGE_DEV:
  41. case FANOUT_DEV:
  42. INIT_LIST_HEAD(&dev->ex_dev.children);
  43. break;
  44. case SATA_DEV:
  45. case SATA_PM:
  46. case SATA_PM_PORT:
  47. INIT_LIST_HEAD(&dev->sata_dev.children);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. static void sas_task_timedout(unsigned long _task)
  54. {
  55. struct sas_task *task = (void *) _task;
  56. unsigned long flags;
  57. spin_lock_irqsave(&task->task_state_lock, flags);
  58. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  59. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  60. spin_unlock_irqrestore(&task->task_state_lock, flags);
  61. complete(&task->completion);
  62. }
  63. static void sas_disc_task_done(struct sas_task *task)
  64. {
  65. if (!del_timer(&task->timer))
  66. return;
  67. complete(&task->completion);
  68. }
  69. #define SAS_DEV_TIMEOUT 10
  70. /**
  71. * sas_execute_task -- Basic task processing for discovery
  72. * @task: the task to be executed
  73. * @buffer: pointer to buffer to do I/O
  74. * @size: size of @buffer
  75. * @pci_dma_dir: PCI_DMA_...
  76. */
  77. static int sas_execute_task(struct sas_task *task, void *buffer, int size,
  78. int pci_dma_dir)
  79. {
  80. int res = 0;
  81. struct scatterlist *scatter = NULL;
  82. struct task_status_struct *ts = &task->task_status;
  83. int num_scatter = 0;
  84. int retries = 0;
  85. struct sas_internal *i =
  86. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  87. if (pci_dma_dir != PCI_DMA_NONE) {
  88. scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
  89. if (!scatter)
  90. goto out;
  91. sg_init_one(scatter, buffer, size);
  92. num_scatter = 1;
  93. }
  94. task->task_proto = task->dev->tproto;
  95. task->scatter = scatter;
  96. task->num_scatter = num_scatter;
  97. task->total_xfer_len = size;
  98. task->data_dir = pci_dma_dir;
  99. task->task_done = sas_disc_task_done;
  100. if (pci_dma_dir != PCI_DMA_NONE &&
  101. sas_protocol_ata(task->task_proto)) {
  102. task->num_scatter = pci_map_sg(task->dev->port->ha->pcidev,
  103. task->scatter,
  104. task->num_scatter,
  105. task->data_dir);
  106. }
  107. for (retries = 0; retries < 5; retries++) {
  108. task->task_state_flags = SAS_TASK_STATE_PENDING;
  109. init_completion(&task->completion);
  110. task->timer.data = (unsigned long) task;
  111. task->timer.function = sas_task_timedout;
  112. task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
  113. add_timer(&task->timer);
  114. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  115. if (res) {
  116. del_timer(&task->timer);
  117. SAS_DPRINTK("executing SAS discovery task failed:%d\n",
  118. res);
  119. goto ex_err;
  120. }
  121. wait_for_completion(&task->completion);
  122. res = -ETASK;
  123. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  124. int res2;
  125. SAS_DPRINTK("task aborted, flags:0x%x\n",
  126. task->task_state_flags);
  127. res2 = i->dft->lldd_abort_task(task);
  128. SAS_DPRINTK("came back from abort task\n");
  129. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  130. if (res2 == TMF_RESP_FUNC_COMPLETE)
  131. continue; /* Retry the task */
  132. else
  133. goto ex_err;
  134. }
  135. }
  136. if (task->task_status.stat == SAM_BUSY ||
  137. task->task_status.stat == SAM_TASK_SET_FULL ||
  138. task->task_status.stat == SAS_QUEUE_FULL) {
  139. SAS_DPRINTK("task: q busy, sleeping...\n");
  140. schedule_timeout_interruptible(HZ);
  141. } else if (task->task_status.stat == SAM_CHECK_COND) {
  142. struct scsi_sense_hdr shdr;
  143. if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
  144. &shdr)) {
  145. SAS_DPRINTK("couldn't normalize sense\n");
  146. continue;
  147. }
  148. if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
  149. (shdr.sense_key == 2 && shdr.asc == 4 &&
  150. shdr.ascq == 1)) {
  151. SAS_DPRINTK("device %016llx LUN: %016llx "
  152. "powering up or not ready yet, "
  153. "sleeping...\n",
  154. SAS_ADDR(task->dev->sas_addr),
  155. SAS_ADDR(task->ssp_task.LUN));
  156. schedule_timeout_interruptible(5*HZ);
  157. } else if (shdr.sense_key == 1) {
  158. res = 0;
  159. break;
  160. } else if (shdr.sense_key == 5) {
  161. break;
  162. } else {
  163. SAS_DPRINTK("dev %016llx LUN: %016llx "
  164. "sense key:0x%x ASC:0x%x ASCQ:0x%x"
  165. "\n",
  166. SAS_ADDR(task->dev->sas_addr),
  167. SAS_ADDR(task->ssp_task.LUN),
  168. shdr.sense_key,
  169. shdr.asc, shdr.ascq);
  170. }
  171. } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
  172. task->task_status.stat != SAM_GOOD) {
  173. SAS_DPRINTK("task finished with resp:0x%x, "
  174. "stat:0x%x\n",
  175. task->task_status.resp,
  176. task->task_status.stat);
  177. goto ex_err;
  178. } else {
  179. res = 0;
  180. break;
  181. }
  182. }
  183. ex_err:
  184. if (pci_dma_dir != PCI_DMA_NONE) {
  185. if (sas_protocol_ata(task->task_proto))
  186. pci_unmap_sg(task->dev->port->ha->pcidev,
  187. task->scatter, task->num_scatter,
  188. task->data_dir);
  189. kfree(scatter);
  190. }
  191. out:
  192. return res;
  193. }
  194. /* ---------- Domain device discovery ---------- */
  195. /**
  196. * sas_get_port_device -- Discover devices which caused port creation
  197. * @port: pointer to struct sas_port of interest
  198. *
  199. * Devices directly attached to a HA port, have no parent. This is
  200. * how we know they are (domain) "root" devices. All other devices
  201. * do, and should have their "parent" pointer set appropriately as
  202. * soon as a child device is discovered.
  203. */
  204. static int sas_get_port_device(struct asd_sas_port *port)
  205. {
  206. unsigned long flags;
  207. struct asd_sas_phy *phy;
  208. struct sas_rphy *rphy;
  209. struct domain_device *dev;
  210. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  211. if (!dev)
  212. return -ENOMEM;
  213. spin_lock_irqsave(&port->phy_list_lock, flags);
  214. if (list_empty(&port->phy_list)) {
  215. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  216. kfree(dev);
  217. return -ENODEV;
  218. }
  219. phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
  220. spin_lock(&phy->frame_rcvd_lock);
  221. memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
  222. (size_t)phy->frame_rcvd_size));
  223. spin_unlock(&phy->frame_rcvd_lock);
  224. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  225. if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
  226. struct dev_to_host_fis *fis =
  227. (struct dev_to_host_fis *) dev->frame_rcvd;
  228. if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
  229. fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
  230. && (fis->device & ~0x10) == 0)
  231. dev->dev_type = SATA_PM;
  232. else
  233. dev->dev_type = SATA_DEV;
  234. dev->tproto = SATA_PROTO;
  235. } else {
  236. struct sas_identify_frame *id =
  237. (struct sas_identify_frame *) dev->frame_rcvd;
  238. dev->dev_type = id->dev_type;
  239. dev->iproto = id->initiator_bits;
  240. dev->tproto = id->target_bits;
  241. }
  242. sas_init_dev(dev);
  243. switch (dev->dev_type) {
  244. case SAS_END_DEV:
  245. case SATA_DEV:
  246. rphy = sas_end_device_alloc(port->port);
  247. break;
  248. case EDGE_DEV:
  249. rphy = sas_expander_alloc(port->port,
  250. SAS_EDGE_EXPANDER_DEVICE);
  251. break;
  252. case FANOUT_DEV:
  253. rphy = sas_expander_alloc(port->port,
  254. SAS_FANOUT_EXPANDER_DEVICE);
  255. break;
  256. default:
  257. printk("ERROR: Unidentified device type %d\n", dev->dev_type);
  258. rphy = NULL;
  259. break;
  260. }
  261. if (!rphy) {
  262. kfree(dev);
  263. return -ENODEV;
  264. }
  265. rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
  266. memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
  267. sas_fill_in_rphy(dev, rphy);
  268. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  269. port->port_dev = dev;
  270. dev->port = port;
  271. dev->linkrate = port->linkrate;
  272. dev->min_linkrate = port->linkrate;
  273. dev->max_linkrate = port->linkrate;
  274. dev->pathways = port->num_phys;
  275. memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
  276. memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
  277. memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
  278. port->disc.max_level = 0;
  279. dev->rphy = rphy;
  280. spin_lock_irq(&port->dev_list_lock);
  281. list_add_tail(&dev->dev_list_node, &port->dev_list);
  282. spin_unlock_irq(&port->dev_list_lock);
  283. return 0;
  284. }
  285. /* ---------- Discover and Revalidate ---------- */
  286. /* ---------- SATA ---------- */
  287. static void sas_get_ata_command_set(struct domain_device *dev)
  288. {
  289. struct dev_to_host_fis *fis =
  290. (struct dev_to_host_fis *) dev->frame_rcvd;
  291. if ((fis->sector_count == 1 && /* ATA */
  292. fis->lbal == 1 &&
  293. fis->lbam == 0 &&
  294. fis->lbah == 0 &&
  295. fis->device == 0)
  296. ||
  297. (fis->sector_count == 0 && /* CE-ATA (mATA) */
  298. fis->lbal == 0 &&
  299. fis->lbam == 0xCE &&
  300. fis->lbah == 0xAA &&
  301. (fis->device & ~0x10) == 0))
  302. dev->sata_dev.command_set = ATA_COMMAND_SET;
  303. else if ((fis->interrupt_reason == 1 && /* ATAPI */
  304. fis->lbal == 1 &&
  305. fis->byte_count_low == 0x14 &&
  306. fis->byte_count_high == 0xEB &&
  307. (fis->device & ~0x10) == 0))
  308. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  309. else if ((fis->sector_count == 1 && /* SEMB */
  310. fis->lbal == 1 &&
  311. fis->lbam == 0x3C &&
  312. fis->lbah == 0xC3 &&
  313. fis->device == 0)
  314. ||
  315. (fis->interrupt_reason == 1 && /* SATA PM */
  316. fis->lbal == 1 &&
  317. fis->byte_count_low == 0x69 &&
  318. fis->byte_count_high == 0x96 &&
  319. (fis->device & ~0x10) == 0))
  320. /* Treat it as a superset? */
  321. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  322. }
  323. /**
  324. * sas_issue_ata_cmd -- Basic SATA command processing for discovery
  325. * @dev: the device to send the command to
  326. * @command: the command register
  327. * @features: the features register
  328. * @buffer: pointer to buffer to do I/O
  329. * @size: size of @buffer
  330. * @pci_dma_dir: PCI_DMA_...
  331. */
  332. static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
  333. u8 features, void *buffer, int size,
  334. int pci_dma_dir)
  335. {
  336. int res = 0;
  337. struct sas_task *task;
  338. struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
  339. &dev->frame_rcvd[0];
  340. res = -ENOMEM;
  341. task = sas_alloc_task(GFP_KERNEL);
  342. if (!task)
  343. goto out;
  344. task->dev = dev;
  345. task->ata_task.fis.fis_type = 0x27;
  346. task->ata_task.fis.command = command;
  347. task->ata_task.fis.features = features;
  348. task->ata_task.fis.device = d2h_fis->device;
  349. task->ata_task.retry_count = 1;
  350. res = sas_execute_task(task, buffer, size, pci_dma_dir);
  351. sas_free_task(task);
  352. out:
  353. return res;
  354. }
  355. static void sas_sata_propagate_sas_addr(struct domain_device *dev)
  356. {
  357. unsigned long flags;
  358. struct asd_sas_port *port = dev->port;
  359. struct asd_sas_phy *phy;
  360. BUG_ON(dev->parent);
  361. memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  362. spin_lock_irqsave(&port->phy_list_lock, flags);
  363. list_for_each_entry(phy, &port->phy_list, port_phy_el)
  364. memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  365. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  366. }
  367. #define ATA_IDENTIFY_DEV 0xEC
  368. #define ATA_IDENTIFY_PACKET_DEV 0xA1
  369. #define ATA_SET_FEATURES 0xEF
  370. #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
  371. /**
  372. * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
  373. * @dev: STP/SATA device of interest (ATA/ATAPI)
  374. *
  375. * The LLDD has already been notified of this device, so that we can
  376. * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
  377. * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
  378. * performance for this device.
  379. */
  380. static int sas_discover_sata_dev(struct domain_device *dev)
  381. {
  382. int res;
  383. __le16 *identify_x;
  384. u8 command;
  385. identify_x = kzalloc(512, GFP_KERNEL);
  386. if (!identify_x)
  387. return -ENOMEM;
  388. if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
  389. dev->sata_dev.identify_device = identify_x;
  390. command = ATA_IDENTIFY_DEV;
  391. } else {
  392. dev->sata_dev.identify_packet_device = identify_x;
  393. command = ATA_IDENTIFY_PACKET_DEV;
  394. }
  395. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  396. PCI_DMA_FROMDEVICE);
  397. if (res)
  398. goto out_err;
  399. /* lives on the media? */
  400. if (le16_to_cpu(identify_x[0]) & 4) {
  401. /* incomplete response */
  402. SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
  403. "dev %llx\n", SAS_ADDR(dev->sas_addr));
  404. if (!le16_to_cpu(identify_x[83] & (1<<6)))
  405. goto cont1;
  406. res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
  407. ATA_FEATURE_PUP_STBY_SPIN_UP,
  408. NULL, 0, PCI_DMA_NONE);
  409. if (res)
  410. goto cont1;
  411. schedule_timeout_interruptible(5*HZ); /* More time? */
  412. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  413. PCI_DMA_FROMDEVICE);
  414. if (res)
  415. goto out_err;
  416. }
  417. cont1:
  418. /* Get WWN */
  419. if (dev->port->oob_mode != SATA_OOB_MODE) {
  420. memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
  421. SAS_ADDR_SIZE);
  422. } else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
  423. (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
  424. == 0x5000) {
  425. int i;
  426. for (i = 0; i < 4; i++) {
  427. dev->sas_addr[2*i] =
  428. (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
  429. dev->sas_addr[2*i+1] =
  430. le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
  431. }
  432. }
  433. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  434. if (!dev->parent)
  435. sas_sata_propagate_sas_addr(dev);
  436. /* XXX Hint: register this SATA device with SATL.
  437. When this returns, dev->sata_dev->lu is alive and
  438. present.
  439. sas_satl_register_dev(dev);
  440. */
  441. sas_fill_in_rphy(dev, dev->rphy);
  442. return 0;
  443. out_err:
  444. dev->sata_dev.identify_packet_device = NULL;
  445. dev->sata_dev.identify_device = NULL;
  446. kfree(identify_x);
  447. return res;
  448. }
  449. static int sas_discover_sata_pm(struct domain_device *dev)
  450. {
  451. return -ENODEV;
  452. }
  453. int sas_notify_lldd_dev_found(struct domain_device *dev)
  454. {
  455. int res = 0;
  456. struct sas_ha_struct *sas_ha = dev->port->ha;
  457. struct Scsi_Host *shost = sas_ha->core.shost;
  458. struct sas_internal *i = to_sas_internal(shost->transportt);
  459. if (i->dft->lldd_dev_found) {
  460. res = i->dft->lldd_dev_found(dev);
  461. if (res) {
  462. printk("sas: driver on pcidev %s cannot handle "
  463. "device %llx, error:%d\n",
  464. pci_name(sas_ha->pcidev),
  465. SAS_ADDR(dev->sas_addr), res);
  466. }
  467. }
  468. return res;
  469. }
  470. void sas_notify_lldd_dev_gone(struct domain_device *dev)
  471. {
  472. struct sas_ha_struct *sas_ha = dev->port->ha;
  473. struct Scsi_Host *shost = sas_ha->core.shost;
  474. struct sas_internal *i = to_sas_internal(shost->transportt);
  475. if (i->dft->lldd_dev_gone)
  476. i->dft->lldd_dev_gone(dev);
  477. }
  478. /* ---------- Common/dispatchers ---------- */
  479. /**
  480. * sas_discover_sata -- discover an STP/SATA domain device
  481. * @dev: pointer to struct domain_device of interest
  482. *
  483. * First we notify the LLDD of this device, so we can send frames to
  484. * it. Then depending on the type of device we call the appropriate
  485. * discover functions. Once device discover is done, we notify the
  486. * LLDD so that it can fine-tune its parameters for the device, by
  487. * removing it and then adding it. That is, the second time around,
  488. * the driver would have certain fields, that it is looking at, set.
  489. * Finally we initialize the kobj so that the device can be added to
  490. * the system at registration time. Devices directly attached to a HA
  491. * port, have no parents. All other devices do, and should have their
  492. * "parent" pointer set appropriately before calling this function.
  493. */
  494. int sas_discover_sata(struct domain_device *dev)
  495. {
  496. int res;
  497. sas_get_ata_command_set(dev);
  498. res = sas_notify_lldd_dev_found(dev);
  499. if (res)
  500. return res;
  501. switch (dev->dev_type) {
  502. case SATA_DEV:
  503. res = sas_discover_sata_dev(dev);
  504. break;
  505. case SATA_PM:
  506. res = sas_discover_sata_pm(dev);
  507. break;
  508. default:
  509. break;
  510. }
  511. sas_notify_lldd_dev_gone(dev);
  512. if (!res) {
  513. sas_notify_lldd_dev_found(dev);
  514. res = sas_rphy_add(dev->rphy);
  515. }
  516. return res;
  517. }
  518. /**
  519. * sas_discover_end_dev -- discover an end device (SSP, etc)
  520. * @end: pointer to domain device of interest
  521. *
  522. * See comment in sas_discover_sata().
  523. */
  524. int sas_discover_end_dev(struct domain_device *dev)
  525. {
  526. int res;
  527. res = sas_notify_lldd_dev_found(dev);
  528. if (res)
  529. goto out_err2;
  530. res = sas_rphy_add(dev->rphy);
  531. if (res)
  532. goto out_err;
  533. return 0;
  534. out_err:
  535. sas_notify_lldd_dev_gone(dev);
  536. out_err2:
  537. return res;
  538. }
  539. /* ---------- Device registration and unregistration ---------- */
  540. static inline void sas_unregister_common_dev(struct domain_device *dev)
  541. {
  542. sas_notify_lldd_dev_gone(dev);
  543. if (!dev->parent)
  544. dev->port->port_dev = NULL;
  545. else
  546. list_del_init(&dev->siblings);
  547. list_del_init(&dev->dev_list_node);
  548. }
  549. void sas_unregister_dev(struct domain_device *dev)
  550. {
  551. if (dev->rphy) {
  552. sas_remove_children(&dev->rphy->dev);
  553. sas_rphy_delete(dev->rphy);
  554. dev->rphy = NULL;
  555. }
  556. if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV) {
  557. /* remove the phys and ports, everything else should be gone */
  558. kfree(dev->ex_dev.ex_phy);
  559. dev->ex_dev.ex_phy = NULL;
  560. }
  561. sas_unregister_common_dev(dev);
  562. }
  563. void sas_unregister_domain_devices(struct asd_sas_port *port)
  564. {
  565. struct domain_device *dev, *n;
  566. list_for_each_entry_safe_reverse(dev,n,&port->dev_list,dev_list_node)
  567. sas_unregister_dev(dev);
  568. port->port->rphy = NULL;
  569. }
  570. /* ---------- Discovery and Revalidation ---------- */
  571. /**
  572. * sas_discover_domain -- discover the domain
  573. * @port: port to the domain of interest
  574. *
  575. * NOTE: this process _must_ quit (return) as soon as any connection
  576. * errors are encountered. Connection recovery is done elsewhere.
  577. * Discover process only interrogates devices in order to discover the
  578. * domain.
  579. */
  580. static void sas_discover_domain(struct work_struct *work)
  581. {
  582. struct domain_device *dev;
  583. int error = 0;
  584. struct sas_discovery_event *ev =
  585. container_of(work, struct sas_discovery_event, work);
  586. struct asd_sas_port *port = ev->port;
  587. sas_begin_event(DISCE_DISCOVER_DOMAIN, &port->disc.disc_event_lock,
  588. &port->disc.pending);
  589. if (port->port_dev)
  590. return;
  591. error = sas_get_port_device(port);
  592. if (error)
  593. return;
  594. dev = port->port_dev;
  595. SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
  596. current->pid);
  597. switch (dev->dev_type) {
  598. case SAS_END_DEV:
  599. error = sas_discover_end_dev(dev);
  600. break;
  601. case EDGE_DEV:
  602. case FANOUT_DEV:
  603. error = sas_discover_root_expander(dev);
  604. break;
  605. case SATA_DEV:
  606. case SATA_PM:
  607. error = sas_discover_sata(dev);
  608. break;
  609. default:
  610. SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
  611. break;
  612. }
  613. if (error) {
  614. sas_rphy_free(dev->rphy);
  615. dev->rphy = NULL;
  616. spin_lock_irq(&port->dev_list_lock);
  617. list_del_init(&dev->dev_list_node);
  618. spin_unlock_irq(&port->dev_list_lock);
  619. kfree(dev); /* not kobject_register-ed yet */
  620. port->port_dev = NULL;
  621. }
  622. SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
  623. current->pid, error);
  624. }
  625. static void sas_revalidate_domain(struct work_struct *work)
  626. {
  627. int res = 0;
  628. struct sas_discovery_event *ev =
  629. container_of(work, struct sas_discovery_event, work);
  630. struct asd_sas_port *port = ev->port;
  631. sas_begin_event(DISCE_REVALIDATE_DOMAIN, &port->disc.disc_event_lock,
  632. &port->disc.pending);
  633. SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
  634. current->pid);
  635. if (port->port_dev)
  636. res = sas_ex_revalidate_domain(port->port_dev);
  637. SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
  638. port->id, current->pid, res);
  639. }
  640. /* ---------- Events ---------- */
  641. int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
  642. {
  643. struct sas_discovery *disc;
  644. if (!port)
  645. return 0;
  646. disc = &port->disc;
  647. BUG_ON(ev >= DISC_NUM_EVENTS);
  648. sas_queue_event(ev, &disc->disc_event_lock, &disc->pending,
  649. &disc->disc_work[ev].work, port->ha);
  650. return 0;
  651. }
  652. /**
  653. * sas_init_disc -- initialize the discovery struct in the port
  654. * @port: pointer to struct port
  655. *
  656. * Called when the ports are being initialized.
  657. */
  658. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
  659. {
  660. int i;
  661. static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
  662. [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
  663. [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
  664. };
  665. spin_lock_init(&disc->disc_event_lock);
  666. disc->pending = 0;
  667. for (i = 0; i < DISC_NUM_EVENTS; i++) {
  668. INIT_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
  669. disc->disc_work[i].port = port;
  670. }
  671. }