sas_discover.c 20 KB

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