sas_expander.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. /*
  2. * Serial Attached SCSI (SAS) Expander discovery and configuration
  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/scatterlist.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/slab.h>
  27. #include "sas_internal.h"
  28. #include <scsi/sas_ata.h>
  29. #include <scsi/scsi_transport.h>
  30. #include <scsi/scsi_transport_sas.h>
  31. #include "../scsi_sas_internal.h"
  32. static int sas_discover_expander(struct domain_device *dev);
  33. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
  34. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  35. u8 *sas_addr, int include);
  36. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
  37. /* ---------- SMP task management ---------- */
  38. static void smp_task_timedout(unsigned long _task)
  39. {
  40. struct sas_task *task = (void *) _task;
  41. unsigned long flags;
  42. spin_lock_irqsave(&task->task_state_lock, flags);
  43. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  44. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  45. spin_unlock_irqrestore(&task->task_state_lock, flags);
  46. complete(&task->completion);
  47. }
  48. static void smp_task_done(struct sas_task *task)
  49. {
  50. if (!del_timer(&task->timer))
  51. return;
  52. complete(&task->completion);
  53. }
  54. /* Give it some long enough timeout. In seconds. */
  55. #define SMP_TIMEOUT 10
  56. static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
  57. void *resp, int resp_size)
  58. {
  59. int res, retry;
  60. struct sas_task *task = NULL;
  61. struct sas_internal *i =
  62. to_sas_internal(dev->port->ha->core.shost->transportt);
  63. mutex_lock(&dev->ex_dev.cmd_mutex);
  64. for (retry = 0; retry < 3; retry++) {
  65. if (test_bit(SAS_DEV_GONE, &dev->state)) {
  66. res = -ECOMM;
  67. break;
  68. }
  69. task = sas_alloc_task(GFP_KERNEL);
  70. if (!task) {
  71. res = -ENOMEM;
  72. break;
  73. }
  74. task->dev = dev;
  75. task->task_proto = dev->tproto;
  76. sg_init_one(&task->smp_task.smp_req, req, req_size);
  77. sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
  78. task->task_done = smp_task_done;
  79. task->timer.data = (unsigned long) task;
  80. task->timer.function = smp_task_timedout;
  81. task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
  82. add_timer(&task->timer);
  83. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  84. if (res) {
  85. del_timer(&task->timer);
  86. SAS_DPRINTK("executing SMP task failed:%d\n", res);
  87. break;
  88. }
  89. wait_for_completion(&task->completion);
  90. res = -ECOMM;
  91. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  92. SAS_DPRINTK("smp task timed out or aborted\n");
  93. i->dft->lldd_abort_task(task);
  94. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  95. SAS_DPRINTK("SMP task aborted and not done\n");
  96. break;
  97. }
  98. }
  99. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  100. task->task_status.stat == SAM_STAT_GOOD) {
  101. res = 0;
  102. break;
  103. }
  104. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  105. task->task_status.stat == SAS_DATA_UNDERRUN) {
  106. /* no error, but return the number of bytes of
  107. * underrun */
  108. res = task->task_status.residual;
  109. break;
  110. }
  111. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  112. task->task_status.stat == SAS_DATA_OVERRUN) {
  113. res = -EMSGSIZE;
  114. break;
  115. }
  116. if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
  117. task->task_status.stat == SAS_DEVICE_UNKNOWN)
  118. break;
  119. else {
  120. SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
  121. "status 0x%x\n", __func__,
  122. SAS_ADDR(dev->sas_addr),
  123. task->task_status.resp,
  124. task->task_status.stat);
  125. sas_free_task(task);
  126. task = NULL;
  127. }
  128. }
  129. mutex_unlock(&dev->ex_dev.cmd_mutex);
  130. BUG_ON(retry == 3 && task != NULL);
  131. sas_free_task(task);
  132. return res;
  133. }
  134. /* ---------- Allocations ---------- */
  135. static inline void *alloc_smp_req(int size)
  136. {
  137. u8 *p = kzalloc(size, GFP_KERNEL);
  138. if (p)
  139. p[0] = SMP_REQUEST;
  140. return p;
  141. }
  142. static inline void *alloc_smp_resp(int size)
  143. {
  144. return kzalloc(size, GFP_KERNEL);
  145. }
  146. /* ---------- Expander configuration ---------- */
  147. static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
  148. void *disc_resp)
  149. {
  150. struct expander_device *ex = &dev->ex_dev;
  151. struct ex_phy *phy = &ex->ex_phy[phy_id];
  152. struct smp_resp *resp = disc_resp;
  153. struct discover_resp *dr = &resp->disc;
  154. struct sas_rphy *rphy = dev->rphy;
  155. int rediscover = (phy->phy != NULL);
  156. if (!rediscover) {
  157. phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
  158. /* FIXME: error_handling */
  159. BUG_ON(!phy->phy);
  160. }
  161. switch (resp->result) {
  162. case SMP_RESP_PHY_VACANT:
  163. phy->phy_state = PHY_VACANT;
  164. break;
  165. default:
  166. phy->phy_state = PHY_NOT_PRESENT;
  167. break;
  168. case SMP_RESP_FUNC_ACC:
  169. phy->phy_state = PHY_EMPTY; /* do not know yet */
  170. break;
  171. }
  172. phy->phy_id = phy_id;
  173. phy->attached_dev_type = dr->attached_dev_type;
  174. phy->linkrate = dr->linkrate;
  175. phy->attached_sata_host = dr->attached_sata_host;
  176. phy->attached_sata_dev = dr->attached_sata_dev;
  177. phy->attached_sata_ps = dr->attached_sata_ps;
  178. phy->attached_iproto = dr->iproto << 1;
  179. phy->attached_tproto = dr->tproto << 1;
  180. memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
  181. phy->attached_phy_id = dr->attached_phy_id;
  182. phy->phy_change_count = dr->change_count;
  183. phy->routing_attr = dr->routing_attr;
  184. phy->virtual = dr->virtual;
  185. phy->last_da_index = -1;
  186. phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
  187. phy->phy->identify.device_type = phy->attached_dev_type;
  188. phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
  189. phy->phy->identify.target_port_protocols = phy->attached_tproto;
  190. phy->phy->identify.phy_identifier = phy_id;
  191. phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
  192. phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
  193. phy->phy->minimum_linkrate = dr->pmin_linkrate;
  194. phy->phy->maximum_linkrate = dr->pmax_linkrate;
  195. phy->phy->negotiated_linkrate = phy->linkrate;
  196. if (!rediscover)
  197. if (sas_phy_add(phy->phy)) {
  198. sas_phy_free(phy->phy);
  199. return;
  200. }
  201. SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
  202. SAS_ADDR(dev->sas_addr), phy->phy_id,
  203. phy->routing_attr == TABLE_ROUTING ? 'T' :
  204. phy->routing_attr == DIRECT_ROUTING ? 'D' :
  205. phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
  206. SAS_ADDR(phy->attached_sas_addr));
  207. return;
  208. }
  209. /* check if we have an existing attached ata device on this expander phy */
  210. struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
  211. {
  212. struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
  213. struct domain_device *dev;
  214. struct sas_rphy *rphy;
  215. if (!ex_phy->port)
  216. return NULL;
  217. rphy = ex_phy->port->rphy;
  218. if (!rphy)
  219. return NULL;
  220. dev = sas_find_dev_by_rphy(rphy);
  221. if (dev && dev_is_sata(dev))
  222. return dev;
  223. return NULL;
  224. }
  225. #define DISCOVER_REQ_SIZE 16
  226. #define DISCOVER_RESP_SIZE 56
  227. static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
  228. u8 *disc_resp, int single)
  229. {
  230. struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
  231. int i, res;
  232. disc_req[9] = single;
  233. for (i = 1 ; i < 3; i++) {
  234. struct discover_resp *dr;
  235. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  236. disc_resp, DISCOVER_RESP_SIZE);
  237. if (res)
  238. return res;
  239. dr = &((struct smp_resp *)disc_resp)->disc;
  240. if (memcmp(dev->sas_addr, dr->attached_sas_addr,
  241. SAS_ADDR_SIZE) == 0) {
  242. sas_printk("Found loopback topology, just ignore it!\n");
  243. return 0;
  244. }
  245. /* This is detecting a failure to transmit initial
  246. * dev to host FIS as described in section J.5 of
  247. * sas-2 r16
  248. */
  249. if (!(dr->attached_dev_type == 0 &&
  250. dr->attached_sata_dev))
  251. break;
  252. /* In order to generate the dev to host FIS, we send a
  253. * link reset to the expander port. If a device was
  254. * previously detected on this port we ask libata to
  255. * manage the reset and link recovery.
  256. */
  257. if (ata_dev) {
  258. sas_ata_schedule_reset(ata_dev);
  259. break;
  260. }
  261. sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
  262. /* Wait for the reset to trigger the negotiation */
  263. msleep(500);
  264. }
  265. sas_set_ex_phy(dev, single, disc_resp);
  266. return 0;
  267. }
  268. static int sas_ex_phy_discover(struct domain_device *dev, int single)
  269. {
  270. struct expander_device *ex = &dev->ex_dev;
  271. int res = 0;
  272. u8 *disc_req;
  273. u8 *disc_resp;
  274. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  275. if (!disc_req)
  276. return -ENOMEM;
  277. disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
  278. if (!disc_resp) {
  279. kfree(disc_req);
  280. return -ENOMEM;
  281. }
  282. disc_req[1] = SMP_DISCOVER;
  283. if (0 <= single && single < ex->num_phys) {
  284. res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
  285. } else {
  286. int i;
  287. for (i = 0; i < ex->num_phys; i++) {
  288. res = sas_ex_phy_discover_helper(dev, disc_req,
  289. disc_resp, i);
  290. if (res)
  291. goto out_err;
  292. }
  293. }
  294. out_err:
  295. kfree(disc_resp);
  296. kfree(disc_req);
  297. return res;
  298. }
  299. static int sas_expander_discover(struct domain_device *dev)
  300. {
  301. struct expander_device *ex = &dev->ex_dev;
  302. int res = -ENOMEM;
  303. ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
  304. if (!ex->ex_phy)
  305. return -ENOMEM;
  306. res = sas_ex_phy_discover(dev, -1);
  307. if (res)
  308. goto out_err;
  309. return 0;
  310. out_err:
  311. kfree(ex->ex_phy);
  312. ex->ex_phy = NULL;
  313. return res;
  314. }
  315. #define MAX_EXPANDER_PHYS 128
  316. static void ex_assign_report_general(struct domain_device *dev,
  317. struct smp_resp *resp)
  318. {
  319. struct report_general_resp *rg = &resp->rg;
  320. dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
  321. dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
  322. dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
  323. dev->ex_dev.t2t_supp = rg->t2t_supp;
  324. dev->ex_dev.conf_route_table = rg->conf_route_table;
  325. dev->ex_dev.configuring = rg->configuring;
  326. memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
  327. }
  328. #define RG_REQ_SIZE 8
  329. #define RG_RESP_SIZE 32
  330. static int sas_ex_general(struct domain_device *dev)
  331. {
  332. u8 *rg_req;
  333. struct smp_resp *rg_resp;
  334. int res;
  335. int i;
  336. rg_req = alloc_smp_req(RG_REQ_SIZE);
  337. if (!rg_req)
  338. return -ENOMEM;
  339. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  340. if (!rg_resp) {
  341. kfree(rg_req);
  342. return -ENOMEM;
  343. }
  344. rg_req[1] = SMP_REPORT_GENERAL;
  345. for (i = 0; i < 5; i++) {
  346. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  347. RG_RESP_SIZE);
  348. if (res) {
  349. SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
  350. SAS_ADDR(dev->sas_addr), res);
  351. goto out;
  352. } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  353. SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
  354. SAS_ADDR(dev->sas_addr), rg_resp->result);
  355. res = rg_resp->result;
  356. goto out;
  357. }
  358. ex_assign_report_general(dev, rg_resp);
  359. if (dev->ex_dev.configuring) {
  360. SAS_DPRINTK("RG: ex %llx self-configuring...\n",
  361. SAS_ADDR(dev->sas_addr));
  362. schedule_timeout_interruptible(5*HZ);
  363. } else
  364. break;
  365. }
  366. out:
  367. kfree(rg_req);
  368. kfree(rg_resp);
  369. return res;
  370. }
  371. static void ex_assign_manuf_info(struct domain_device *dev, void
  372. *_mi_resp)
  373. {
  374. u8 *mi_resp = _mi_resp;
  375. struct sas_rphy *rphy = dev->rphy;
  376. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  377. memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
  378. memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
  379. memcpy(edev->product_rev, mi_resp + 36,
  380. SAS_EXPANDER_PRODUCT_REV_LEN);
  381. if (mi_resp[8] & 1) {
  382. memcpy(edev->component_vendor_id, mi_resp + 40,
  383. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  384. edev->component_id = mi_resp[48] << 8 | mi_resp[49];
  385. edev->component_revision_id = mi_resp[50];
  386. }
  387. }
  388. #define MI_REQ_SIZE 8
  389. #define MI_RESP_SIZE 64
  390. static int sas_ex_manuf_info(struct domain_device *dev)
  391. {
  392. u8 *mi_req;
  393. u8 *mi_resp;
  394. int res;
  395. mi_req = alloc_smp_req(MI_REQ_SIZE);
  396. if (!mi_req)
  397. return -ENOMEM;
  398. mi_resp = alloc_smp_resp(MI_RESP_SIZE);
  399. if (!mi_resp) {
  400. kfree(mi_req);
  401. return -ENOMEM;
  402. }
  403. mi_req[1] = SMP_REPORT_MANUF_INFO;
  404. res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
  405. if (res) {
  406. SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
  407. SAS_ADDR(dev->sas_addr), res);
  408. goto out;
  409. } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
  410. SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
  411. SAS_ADDR(dev->sas_addr), mi_resp[2]);
  412. goto out;
  413. }
  414. ex_assign_manuf_info(dev, mi_resp);
  415. out:
  416. kfree(mi_req);
  417. kfree(mi_resp);
  418. return res;
  419. }
  420. #define PC_REQ_SIZE 44
  421. #define PC_RESP_SIZE 8
  422. int sas_smp_phy_control(struct domain_device *dev, int phy_id,
  423. enum phy_func phy_func,
  424. struct sas_phy_linkrates *rates)
  425. {
  426. u8 *pc_req;
  427. u8 *pc_resp;
  428. int res;
  429. pc_req = alloc_smp_req(PC_REQ_SIZE);
  430. if (!pc_req)
  431. return -ENOMEM;
  432. pc_resp = alloc_smp_resp(PC_RESP_SIZE);
  433. if (!pc_resp) {
  434. kfree(pc_req);
  435. return -ENOMEM;
  436. }
  437. pc_req[1] = SMP_PHY_CONTROL;
  438. pc_req[9] = phy_id;
  439. pc_req[10]= phy_func;
  440. if (rates) {
  441. pc_req[32] = rates->minimum_linkrate << 4;
  442. pc_req[33] = rates->maximum_linkrate << 4;
  443. }
  444. res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
  445. kfree(pc_resp);
  446. kfree(pc_req);
  447. return res;
  448. }
  449. static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
  450. {
  451. struct expander_device *ex = &dev->ex_dev;
  452. struct ex_phy *phy = &ex->ex_phy[phy_id];
  453. sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
  454. phy->linkrate = SAS_PHY_DISABLED;
  455. }
  456. static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
  457. {
  458. struct expander_device *ex = &dev->ex_dev;
  459. int i;
  460. for (i = 0; i < ex->num_phys; i++) {
  461. struct ex_phy *phy = &ex->ex_phy[i];
  462. if (phy->phy_state == PHY_VACANT ||
  463. phy->phy_state == PHY_NOT_PRESENT)
  464. continue;
  465. if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
  466. sas_ex_disable_phy(dev, i);
  467. }
  468. }
  469. static int sas_dev_present_in_domain(struct asd_sas_port *port,
  470. u8 *sas_addr)
  471. {
  472. struct domain_device *dev;
  473. if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
  474. return 1;
  475. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  476. if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
  477. return 1;
  478. }
  479. return 0;
  480. }
  481. #define RPEL_REQ_SIZE 16
  482. #define RPEL_RESP_SIZE 32
  483. int sas_smp_get_phy_events(struct sas_phy *phy)
  484. {
  485. int res;
  486. u8 *req;
  487. u8 *resp;
  488. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  489. struct domain_device *dev = sas_find_dev_by_rphy(rphy);
  490. req = alloc_smp_req(RPEL_REQ_SIZE);
  491. if (!req)
  492. return -ENOMEM;
  493. resp = alloc_smp_resp(RPEL_RESP_SIZE);
  494. if (!resp) {
  495. kfree(req);
  496. return -ENOMEM;
  497. }
  498. req[1] = SMP_REPORT_PHY_ERR_LOG;
  499. req[9] = phy->number;
  500. res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
  501. resp, RPEL_RESP_SIZE);
  502. if (!res)
  503. goto out;
  504. phy->invalid_dword_count = scsi_to_u32(&resp[12]);
  505. phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
  506. phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
  507. phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
  508. out:
  509. kfree(resp);
  510. return res;
  511. }
  512. #ifdef CONFIG_SCSI_SAS_ATA
  513. #define RPS_REQ_SIZE 16
  514. #define RPS_RESP_SIZE 60
  515. static int sas_get_report_phy_sata(struct domain_device *dev,
  516. int phy_id,
  517. struct smp_resp *rps_resp)
  518. {
  519. int res;
  520. u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
  521. u8 *resp = (u8 *)rps_resp;
  522. if (!rps_req)
  523. return -ENOMEM;
  524. rps_req[1] = SMP_REPORT_PHY_SATA;
  525. rps_req[9] = phy_id;
  526. res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
  527. rps_resp, RPS_RESP_SIZE);
  528. /* 0x34 is the FIS type for the D2H fis. There's a potential
  529. * standards cockup here. sas-2 explicitly specifies the FIS
  530. * should be encoded so that FIS type is in resp[24].
  531. * However, some expanders endian reverse this. Undo the
  532. * reversal here */
  533. if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
  534. int i;
  535. for (i = 0; i < 5; i++) {
  536. int j = 24 + (i*4);
  537. u8 a, b;
  538. a = resp[j + 0];
  539. b = resp[j + 1];
  540. resp[j + 0] = resp[j + 3];
  541. resp[j + 1] = resp[j + 2];
  542. resp[j + 2] = b;
  543. resp[j + 3] = a;
  544. }
  545. }
  546. kfree(rps_req);
  547. return res;
  548. }
  549. #endif
  550. static void sas_ex_get_linkrate(struct domain_device *parent,
  551. struct domain_device *child,
  552. struct ex_phy *parent_phy)
  553. {
  554. struct expander_device *parent_ex = &parent->ex_dev;
  555. struct sas_port *port;
  556. int i;
  557. child->pathways = 0;
  558. port = parent_phy->port;
  559. for (i = 0; i < parent_ex->num_phys; i++) {
  560. struct ex_phy *phy = &parent_ex->ex_phy[i];
  561. if (phy->phy_state == PHY_VACANT ||
  562. phy->phy_state == PHY_NOT_PRESENT)
  563. continue;
  564. if (SAS_ADDR(phy->attached_sas_addr) ==
  565. SAS_ADDR(child->sas_addr)) {
  566. child->min_linkrate = min(parent->min_linkrate,
  567. phy->linkrate);
  568. child->max_linkrate = max(parent->max_linkrate,
  569. phy->linkrate);
  570. child->pathways++;
  571. sas_port_add_phy(port, phy->phy);
  572. }
  573. }
  574. child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
  575. child->pathways = min(child->pathways, parent->pathways);
  576. }
  577. static struct domain_device *sas_ex_discover_end_dev(
  578. struct domain_device *parent, int phy_id)
  579. {
  580. struct expander_device *parent_ex = &parent->ex_dev;
  581. struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
  582. struct domain_device *child = NULL;
  583. struct sas_rphy *rphy;
  584. int res;
  585. if (phy->attached_sata_host || phy->attached_sata_ps)
  586. return NULL;
  587. child = sas_alloc_device();
  588. if (!child)
  589. return NULL;
  590. kref_get(&parent->kref);
  591. child->parent = parent;
  592. child->port = parent->port;
  593. child->iproto = phy->attached_iproto;
  594. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  595. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  596. if (!phy->port) {
  597. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  598. if (unlikely(!phy->port))
  599. goto out_err;
  600. if (unlikely(sas_port_add(phy->port) != 0)) {
  601. sas_port_free(phy->port);
  602. goto out_err;
  603. }
  604. }
  605. sas_ex_get_linkrate(parent, child, phy);
  606. #ifdef CONFIG_SCSI_SAS_ATA
  607. if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
  608. child->dev_type = SATA_DEV;
  609. if (phy->attached_tproto & SAS_PROTOCOL_STP)
  610. child->tproto = phy->attached_tproto;
  611. if (phy->attached_sata_dev)
  612. child->tproto |= SATA_DEV;
  613. res = sas_get_report_phy_sata(parent, phy_id,
  614. &child->sata_dev.rps_resp);
  615. if (res) {
  616. SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
  617. "0x%x\n", SAS_ADDR(parent->sas_addr),
  618. phy_id, res);
  619. goto out_free;
  620. }
  621. memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
  622. sizeof(struct dev_to_host_fis));
  623. rphy = sas_end_device_alloc(phy->port);
  624. if (unlikely(!rphy))
  625. goto out_free;
  626. sas_init_dev(child);
  627. child->rphy = rphy;
  628. list_add_tail(&child->disco_list_node, &parent->port->disco_list);
  629. res = sas_discover_sata(child);
  630. if (res) {
  631. SAS_DPRINTK("sas_discover_sata() for device %16llx at "
  632. "%016llx:0x%x returned 0x%x\n",
  633. SAS_ADDR(child->sas_addr),
  634. SAS_ADDR(parent->sas_addr), phy_id, res);
  635. goto out_list_del;
  636. }
  637. } else
  638. #endif
  639. if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
  640. child->dev_type = SAS_END_DEV;
  641. rphy = sas_end_device_alloc(phy->port);
  642. /* FIXME: error handling */
  643. if (unlikely(!rphy))
  644. goto out_free;
  645. child->tproto = phy->attached_tproto;
  646. sas_init_dev(child);
  647. child->rphy = rphy;
  648. sas_fill_in_rphy(child, rphy);
  649. spin_lock_irq(&parent->port->dev_list_lock);
  650. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  651. spin_unlock_irq(&parent->port->dev_list_lock);
  652. res = sas_discover_end_dev(child);
  653. if (res) {
  654. SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
  655. "at %016llx:0x%x returned 0x%x\n",
  656. SAS_ADDR(child->sas_addr),
  657. SAS_ADDR(parent->sas_addr), phy_id, res);
  658. goto out_list_del;
  659. }
  660. } else {
  661. SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
  662. phy->attached_tproto, SAS_ADDR(parent->sas_addr),
  663. phy_id);
  664. goto out_free;
  665. }
  666. list_add_tail(&child->siblings, &parent_ex->children);
  667. return child;
  668. out_list_del:
  669. sas_rphy_free(child->rphy);
  670. child->rphy = NULL;
  671. list_del(&child->disco_list_node);
  672. spin_lock_irq(&parent->port->dev_list_lock);
  673. list_del(&child->dev_list_node);
  674. spin_unlock_irq(&parent->port->dev_list_lock);
  675. out_free:
  676. sas_port_delete(phy->port);
  677. out_err:
  678. phy->port = NULL;
  679. sas_put_device(child);
  680. return NULL;
  681. }
  682. /* See if this phy is part of a wide port */
  683. static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
  684. {
  685. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  686. int i;
  687. for (i = 0; i < parent->ex_dev.num_phys; i++) {
  688. struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
  689. if (ephy == phy)
  690. continue;
  691. if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
  692. SAS_ADDR_SIZE) && ephy->port) {
  693. sas_port_add_phy(ephy->port, phy->phy);
  694. phy->port = ephy->port;
  695. phy->phy_state = PHY_DEVICE_DISCOVERED;
  696. return 0;
  697. }
  698. }
  699. return -ENODEV;
  700. }
  701. static struct domain_device *sas_ex_discover_expander(
  702. struct domain_device *parent, int phy_id)
  703. {
  704. struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
  705. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  706. struct domain_device *child = NULL;
  707. struct sas_rphy *rphy;
  708. struct sas_expander_device *edev;
  709. struct asd_sas_port *port;
  710. int res;
  711. if (phy->routing_attr == DIRECT_ROUTING) {
  712. SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
  713. "allowed\n",
  714. SAS_ADDR(parent->sas_addr), phy_id,
  715. SAS_ADDR(phy->attached_sas_addr),
  716. phy->attached_phy_id);
  717. return NULL;
  718. }
  719. child = sas_alloc_device();
  720. if (!child)
  721. return NULL;
  722. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  723. /* FIXME: better error handling */
  724. BUG_ON(sas_port_add(phy->port) != 0);
  725. switch (phy->attached_dev_type) {
  726. case EDGE_DEV:
  727. rphy = sas_expander_alloc(phy->port,
  728. SAS_EDGE_EXPANDER_DEVICE);
  729. break;
  730. case FANOUT_DEV:
  731. rphy = sas_expander_alloc(phy->port,
  732. SAS_FANOUT_EXPANDER_DEVICE);
  733. break;
  734. default:
  735. rphy = NULL; /* shut gcc up */
  736. BUG();
  737. }
  738. port = parent->port;
  739. child->rphy = rphy;
  740. edev = rphy_to_expander_device(rphy);
  741. child->dev_type = phy->attached_dev_type;
  742. kref_get(&parent->kref);
  743. child->parent = parent;
  744. child->port = port;
  745. child->iproto = phy->attached_iproto;
  746. child->tproto = phy->attached_tproto;
  747. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  748. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  749. sas_ex_get_linkrate(parent, child, phy);
  750. edev->level = parent_ex->level + 1;
  751. parent->port->disc.max_level = max(parent->port->disc.max_level,
  752. edev->level);
  753. sas_init_dev(child);
  754. sas_fill_in_rphy(child, rphy);
  755. sas_rphy_add(rphy);
  756. spin_lock_irq(&parent->port->dev_list_lock);
  757. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  758. spin_unlock_irq(&parent->port->dev_list_lock);
  759. res = sas_discover_expander(child);
  760. if (res) {
  761. spin_lock_irq(&parent->port->dev_list_lock);
  762. list_del(&child->dev_list_node);
  763. spin_unlock_irq(&parent->port->dev_list_lock);
  764. sas_put_device(child);
  765. return NULL;
  766. }
  767. list_add_tail(&child->siblings, &parent->ex_dev.children);
  768. return child;
  769. }
  770. static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
  771. {
  772. struct expander_device *ex = &dev->ex_dev;
  773. struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
  774. struct domain_device *child = NULL;
  775. int res = 0;
  776. /* Phy state */
  777. if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
  778. if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
  779. res = sas_ex_phy_discover(dev, phy_id);
  780. if (res)
  781. return res;
  782. }
  783. /* Parent and domain coherency */
  784. if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  785. SAS_ADDR(dev->port->sas_addr))) {
  786. sas_add_parent_port(dev, phy_id);
  787. return 0;
  788. }
  789. if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  790. SAS_ADDR(dev->parent->sas_addr))) {
  791. sas_add_parent_port(dev, phy_id);
  792. if (ex_phy->routing_attr == TABLE_ROUTING)
  793. sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
  794. return 0;
  795. }
  796. if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
  797. sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
  798. if (ex_phy->attached_dev_type == NO_DEVICE) {
  799. if (ex_phy->routing_attr == DIRECT_ROUTING) {
  800. memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  801. sas_configure_routing(dev, ex_phy->attached_sas_addr);
  802. }
  803. return 0;
  804. } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
  805. return 0;
  806. if (ex_phy->attached_dev_type != SAS_END_DEV &&
  807. ex_phy->attached_dev_type != FANOUT_DEV &&
  808. ex_phy->attached_dev_type != EDGE_DEV) {
  809. SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
  810. "phy 0x%x\n", ex_phy->attached_dev_type,
  811. SAS_ADDR(dev->sas_addr),
  812. phy_id);
  813. return 0;
  814. }
  815. res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
  816. if (res) {
  817. SAS_DPRINTK("configure routing for dev %016llx "
  818. "reported 0x%x. Forgotten\n",
  819. SAS_ADDR(ex_phy->attached_sas_addr), res);
  820. sas_disable_routing(dev, ex_phy->attached_sas_addr);
  821. return res;
  822. }
  823. res = sas_ex_join_wide_port(dev, phy_id);
  824. if (!res) {
  825. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  826. phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
  827. return res;
  828. }
  829. switch (ex_phy->attached_dev_type) {
  830. case SAS_END_DEV:
  831. child = sas_ex_discover_end_dev(dev, phy_id);
  832. break;
  833. case FANOUT_DEV:
  834. if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
  835. SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
  836. "attached to ex %016llx phy 0x%x\n",
  837. SAS_ADDR(ex_phy->attached_sas_addr),
  838. ex_phy->attached_phy_id,
  839. SAS_ADDR(dev->sas_addr),
  840. phy_id);
  841. sas_ex_disable_phy(dev, phy_id);
  842. break;
  843. } else
  844. memcpy(dev->port->disc.fanout_sas_addr,
  845. ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
  846. /* fallthrough */
  847. case EDGE_DEV:
  848. child = sas_ex_discover_expander(dev, phy_id);
  849. break;
  850. default:
  851. break;
  852. }
  853. if (child) {
  854. int i;
  855. for (i = 0; i < ex->num_phys; i++) {
  856. if (ex->ex_phy[i].phy_state == PHY_VACANT ||
  857. ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
  858. continue;
  859. /*
  860. * Due to races, the phy might not get added to the
  861. * wide port, so we add the phy to the wide port here.
  862. */
  863. if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
  864. SAS_ADDR(child->sas_addr)) {
  865. ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
  866. res = sas_ex_join_wide_port(dev, i);
  867. if (!res)
  868. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  869. i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
  870. }
  871. }
  872. }
  873. return res;
  874. }
  875. static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
  876. {
  877. struct expander_device *ex = &dev->ex_dev;
  878. int i;
  879. for (i = 0; i < ex->num_phys; i++) {
  880. struct ex_phy *phy = &ex->ex_phy[i];
  881. if (phy->phy_state == PHY_VACANT ||
  882. phy->phy_state == PHY_NOT_PRESENT)
  883. continue;
  884. if ((phy->attached_dev_type == EDGE_DEV ||
  885. phy->attached_dev_type == FANOUT_DEV) &&
  886. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  887. memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
  888. return 1;
  889. }
  890. }
  891. return 0;
  892. }
  893. static int sas_check_level_subtractive_boundary(struct domain_device *dev)
  894. {
  895. struct expander_device *ex = &dev->ex_dev;
  896. struct domain_device *child;
  897. u8 sub_addr[8] = {0, };
  898. list_for_each_entry(child, &ex->children, siblings) {
  899. if (child->dev_type != EDGE_DEV &&
  900. child->dev_type != FANOUT_DEV)
  901. continue;
  902. if (sub_addr[0] == 0) {
  903. sas_find_sub_addr(child, sub_addr);
  904. continue;
  905. } else {
  906. u8 s2[8];
  907. if (sas_find_sub_addr(child, s2) &&
  908. (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
  909. SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
  910. "diverges from subtractive "
  911. "boundary %016llx\n",
  912. SAS_ADDR(dev->sas_addr),
  913. SAS_ADDR(child->sas_addr),
  914. SAS_ADDR(s2),
  915. SAS_ADDR(sub_addr));
  916. sas_ex_disable_port(child, s2);
  917. }
  918. }
  919. }
  920. return 0;
  921. }
  922. /**
  923. * sas_ex_discover_devices -- discover devices attached to this expander
  924. * dev: pointer to the expander domain device
  925. * single: if you want to do a single phy, else set to -1;
  926. *
  927. * Configure this expander for use with its devices and register the
  928. * devices of this expander.
  929. */
  930. static int sas_ex_discover_devices(struct domain_device *dev, int single)
  931. {
  932. struct expander_device *ex = &dev->ex_dev;
  933. int i = 0, end = ex->num_phys;
  934. int res = 0;
  935. if (0 <= single && single < end) {
  936. i = single;
  937. end = i+1;
  938. }
  939. for ( ; i < end; i++) {
  940. struct ex_phy *ex_phy = &ex->ex_phy[i];
  941. if (ex_phy->phy_state == PHY_VACANT ||
  942. ex_phy->phy_state == PHY_NOT_PRESENT ||
  943. ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
  944. continue;
  945. switch (ex_phy->linkrate) {
  946. case SAS_PHY_DISABLED:
  947. case SAS_PHY_RESET_PROBLEM:
  948. case SAS_SATA_PORT_SELECTOR:
  949. continue;
  950. default:
  951. res = sas_ex_discover_dev(dev, i);
  952. if (res)
  953. break;
  954. continue;
  955. }
  956. }
  957. if (!res)
  958. sas_check_level_subtractive_boundary(dev);
  959. return res;
  960. }
  961. static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
  962. {
  963. struct expander_device *ex = &dev->ex_dev;
  964. int i;
  965. u8 *sub_sas_addr = NULL;
  966. if (dev->dev_type != EDGE_DEV)
  967. return 0;
  968. for (i = 0; i < ex->num_phys; i++) {
  969. struct ex_phy *phy = &ex->ex_phy[i];
  970. if (phy->phy_state == PHY_VACANT ||
  971. phy->phy_state == PHY_NOT_PRESENT)
  972. continue;
  973. if ((phy->attached_dev_type == FANOUT_DEV ||
  974. phy->attached_dev_type == EDGE_DEV) &&
  975. phy->routing_attr == SUBTRACTIVE_ROUTING) {
  976. if (!sub_sas_addr)
  977. sub_sas_addr = &phy->attached_sas_addr[0];
  978. else if (SAS_ADDR(sub_sas_addr) !=
  979. SAS_ADDR(phy->attached_sas_addr)) {
  980. SAS_DPRINTK("ex %016llx phy 0x%x "
  981. "diverges(%016llx) on subtractive "
  982. "boundary(%016llx). Disabled\n",
  983. SAS_ADDR(dev->sas_addr), i,
  984. SAS_ADDR(phy->attached_sas_addr),
  985. SAS_ADDR(sub_sas_addr));
  986. sas_ex_disable_phy(dev, i);
  987. }
  988. }
  989. }
  990. return 0;
  991. }
  992. static void sas_print_parent_topology_bug(struct domain_device *child,
  993. struct ex_phy *parent_phy,
  994. struct ex_phy *child_phy)
  995. {
  996. static const char ra_char[] = {
  997. [DIRECT_ROUTING] = 'D',
  998. [SUBTRACTIVE_ROUTING] = 'S',
  999. [TABLE_ROUTING] = 'T',
  1000. };
  1001. static const char *ex_type[] = {
  1002. [EDGE_DEV] = "edge",
  1003. [FANOUT_DEV] = "fanout",
  1004. };
  1005. struct domain_device *parent = child->parent;
  1006. sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
  1007. "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
  1008. ex_type[parent->dev_type],
  1009. SAS_ADDR(parent->sas_addr),
  1010. parent->ex_dev.t2t_supp,
  1011. parent_phy->phy_id,
  1012. ex_type[child->dev_type],
  1013. SAS_ADDR(child->sas_addr),
  1014. child->ex_dev.t2t_supp,
  1015. child_phy->phy_id,
  1016. ra_char[parent_phy->routing_attr],
  1017. ra_char[child_phy->routing_attr]);
  1018. }
  1019. static int sas_check_eeds(struct domain_device *child,
  1020. struct ex_phy *parent_phy,
  1021. struct ex_phy *child_phy)
  1022. {
  1023. int res = 0;
  1024. struct domain_device *parent = child->parent;
  1025. if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
  1026. res = -ENODEV;
  1027. SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
  1028. "phy S:0x%x, while there is a fanout ex %016llx\n",
  1029. SAS_ADDR(parent->sas_addr),
  1030. parent_phy->phy_id,
  1031. SAS_ADDR(child->sas_addr),
  1032. child_phy->phy_id,
  1033. SAS_ADDR(parent->port->disc.fanout_sas_addr));
  1034. } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
  1035. memcpy(parent->port->disc.eeds_a, parent->sas_addr,
  1036. SAS_ADDR_SIZE);
  1037. memcpy(parent->port->disc.eeds_b, child->sas_addr,
  1038. SAS_ADDR_SIZE);
  1039. } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
  1040. SAS_ADDR(parent->sas_addr)) ||
  1041. (SAS_ADDR(parent->port->disc.eeds_a) ==
  1042. SAS_ADDR(child->sas_addr)))
  1043. &&
  1044. ((SAS_ADDR(parent->port->disc.eeds_b) ==
  1045. SAS_ADDR(parent->sas_addr)) ||
  1046. (SAS_ADDR(parent->port->disc.eeds_b) ==
  1047. SAS_ADDR(child->sas_addr))))
  1048. ;
  1049. else {
  1050. res = -ENODEV;
  1051. SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
  1052. "phy 0x%x link forms a third EEDS!\n",
  1053. SAS_ADDR(parent->sas_addr),
  1054. parent_phy->phy_id,
  1055. SAS_ADDR(child->sas_addr),
  1056. child_phy->phy_id);
  1057. }
  1058. return res;
  1059. }
  1060. /* Here we spill over 80 columns. It is intentional.
  1061. */
  1062. static int sas_check_parent_topology(struct domain_device *child)
  1063. {
  1064. struct expander_device *child_ex = &child->ex_dev;
  1065. struct expander_device *parent_ex;
  1066. int i;
  1067. int res = 0;
  1068. if (!child->parent)
  1069. return 0;
  1070. if (child->parent->dev_type != EDGE_DEV &&
  1071. child->parent->dev_type != FANOUT_DEV)
  1072. return 0;
  1073. parent_ex = &child->parent->ex_dev;
  1074. for (i = 0; i < parent_ex->num_phys; i++) {
  1075. struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
  1076. struct ex_phy *child_phy;
  1077. if (parent_phy->phy_state == PHY_VACANT ||
  1078. parent_phy->phy_state == PHY_NOT_PRESENT)
  1079. continue;
  1080. if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
  1081. continue;
  1082. child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
  1083. switch (child->parent->dev_type) {
  1084. case EDGE_DEV:
  1085. if (child->dev_type == FANOUT_DEV) {
  1086. if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
  1087. child_phy->routing_attr != TABLE_ROUTING) {
  1088. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1089. res = -ENODEV;
  1090. }
  1091. } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1092. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
  1093. res = sas_check_eeds(child, parent_phy, child_phy);
  1094. } else if (child_phy->routing_attr != TABLE_ROUTING) {
  1095. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1096. res = -ENODEV;
  1097. }
  1098. } else if (parent_phy->routing_attr == TABLE_ROUTING) {
  1099. if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
  1100. (child_phy->routing_attr == TABLE_ROUTING &&
  1101. child_ex->t2t_supp && parent_ex->t2t_supp)) {
  1102. /* All good */;
  1103. } else {
  1104. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1105. res = -ENODEV;
  1106. }
  1107. }
  1108. break;
  1109. case FANOUT_DEV:
  1110. if (parent_phy->routing_attr != TABLE_ROUTING ||
  1111. child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
  1112. sas_print_parent_topology_bug(child, parent_phy, child_phy);
  1113. res = -ENODEV;
  1114. }
  1115. break;
  1116. default:
  1117. break;
  1118. }
  1119. }
  1120. return res;
  1121. }
  1122. #define RRI_REQ_SIZE 16
  1123. #define RRI_RESP_SIZE 44
  1124. static int sas_configure_present(struct domain_device *dev, int phy_id,
  1125. u8 *sas_addr, int *index, int *present)
  1126. {
  1127. int i, res = 0;
  1128. struct expander_device *ex = &dev->ex_dev;
  1129. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1130. u8 *rri_req;
  1131. u8 *rri_resp;
  1132. *present = 0;
  1133. *index = 0;
  1134. rri_req = alloc_smp_req(RRI_REQ_SIZE);
  1135. if (!rri_req)
  1136. return -ENOMEM;
  1137. rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
  1138. if (!rri_resp) {
  1139. kfree(rri_req);
  1140. return -ENOMEM;
  1141. }
  1142. rri_req[1] = SMP_REPORT_ROUTE_INFO;
  1143. rri_req[9] = phy_id;
  1144. for (i = 0; i < ex->max_route_indexes ; i++) {
  1145. *(__be16 *)(rri_req+6) = cpu_to_be16(i);
  1146. res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
  1147. RRI_RESP_SIZE);
  1148. if (res)
  1149. goto out;
  1150. res = rri_resp[2];
  1151. if (res == SMP_RESP_NO_INDEX) {
  1152. SAS_DPRINTK("overflow of indexes: dev %016llx "
  1153. "phy 0x%x index 0x%x\n",
  1154. SAS_ADDR(dev->sas_addr), phy_id, i);
  1155. goto out;
  1156. } else if (res != SMP_RESP_FUNC_ACC) {
  1157. SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
  1158. "result 0x%x\n", __func__,
  1159. SAS_ADDR(dev->sas_addr), phy_id, i, res);
  1160. goto out;
  1161. }
  1162. if (SAS_ADDR(sas_addr) != 0) {
  1163. if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
  1164. *index = i;
  1165. if ((rri_resp[12] & 0x80) == 0x80)
  1166. *present = 0;
  1167. else
  1168. *present = 1;
  1169. goto out;
  1170. } else if (SAS_ADDR(rri_resp+16) == 0) {
  1171. *index = i;
  1172. *present = 0;
  1173. goto out;
  1174. }
  1175. } else if (SAS_ADDR(rri_resp+16) == 0 &&
  1176. phy->last_da_index < i) {
  1177. phy->last_da_index = i;
  1178. *index = i;
  1179. *present = 0;
  1180. goto out;
  1181. }
  1182. }
  1183. res = -1;
  1184. out:
  1185. kfree(rri_req);
  1186. kfree(rri_resp);
  1187. return res;
  1188. }
  1189. #define CRI_REQ_SIZE 44
  1190. #define CRI_RESP_SIZE 8
  1191. static int sas_configure_set(struct domain_device *dev, int phy_id,
  1192. u8 *sas_addr, int index, int include)
  1193. {
  1194. int res;
  1195. u8 *cri_req;
  1196. u8 *cri_resp;
  1197. cri_req = alloc_smp_req(CRI_REQ_SIZE);
  1198. if (!cri_req)
  1199. return -ENOMEM;
  1200. cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
  1201. if (!cri_resp) {
  1202. kfree(cri_req);
  1203. return -ENOMEM;
  1204. }
  1205. cri_req[1] = SMP_CONF_ROUTE_INFO;
  1206. *(__be16 *)(cri_req+6) = cpu_to_be16(index);
  1207. cri_req[9] = phy_id;
  1208. if (SAS_ADDR(sas_addr) == 0 || !include)
  1209. cri_req[12] |= 0x80;
  1210. memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
  1211. res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
  1212. CRI_RESP_SIZE);
  1213. if (res)
  1214. goto out;
  1215. res = cri_resp[2];
  1216. if (res == SMP_RESP_NO_INDEX) {
  1217. SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
  1218. "index 0x%x\n",
  1219. SAS_ADDR(dev->sas_addr), phy_id, index);
  1220. }
  1221. out:
  1222. kfree(cri_req);
  1223. kfree(cri_resp);
  1224. return res;
  1225. }
  1226. static int sas_configure_phy(struct domain_device *dev, int phy_id,
  1227. u8 *sas_addr, int include)
  1228. {
  1229. int index;
  1230. int present;
  1231. int res;
  1232. res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
  1233. if (res)
  1234. return res;
  1235. if (include ^ present)
  1236. return sas_configure_set(dev, phy_id, sas_addr, index,include);
  1237. return res;
  1238. }
  1239. /**
  1240. * sas_configure_parent -- configure routing table of parent
  1241. * parent: parent expander
  1242. * child: child expander
  1243. * sas_addr: SAS port identifier of device directly attached to child
  1244. */
  1245. static int sas_configure_parent(struct domain_device *parent,
  1246. struct domain_device *child,
  1247. u8 *sas_addr, int include)
  1248. {
  1249. struct expander_device *ex_parent = &parent->ex_dev;
  1250. int res = 0;
  1251. int i;
  1252. if (parent->parent) {
  1253. res = sas_configure_parent(parent->parent, parent, sas_addr,
  1254. include);
  1255. if (res)
  1256. return res;
  1257. }
  1258. if (ex_parent->conf_route_table == 0) {
  1259. SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
  1260. SAS_ADDR(parent->sas_addr));
  1261. return 0;
  1262. }
  1263. for (i = 0; i < ex_parent->num_phys; i++) {
  1264. struct ex_phy *phy = &ex_parent->ex_phy[i];
  1265. if ((phy->routing_attr == TABLE_ROUTING) &&
  1266. (SAS_ADDR(phy->attached_sas_addr) ==
  1267. SAS_ADDR(child->sas_addr))) {
  1268. res = sas_configure_phy(parent, i, sas_addr, include);
  1269. if (res)
  1270. return res;
  1271. }
  1272. }
  1273. return res;
  1274. }
  1275. /**
  1276. * sas_configure_routing -- configure routing
  1277. * dev: expander device
  1278. * sas_addr: port identifier of device directly attached to the expander device
  1279. */
  1280. static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
  1281. {
  1282. if (dev->parent)
  1283. return sas_configure_parent(dev->parent, dev, sas_addr, 1);
  1284. return 0;
  1285. }
  1286. static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
  1287. {
  1288. if (dev->parent)
  1289. return sas_configure_parent(dev->parent, dev, sas_addr, 0);
  1290. return 0;
  1291. }
  1292. /**
  1293. * sas_discover_expander -- expander discovery
  1294. * @ex: pointer to expander domain device
  1295. *
  1296. * See comment in sas_discover_sata().
  1297. */
  1298. static int sas_discover_expander(struct domain_device *dev)
  1299. {
  1300. int res;
  1301. res = sas_notify_lldd_dev_found(dev);
  1302. if (res)
  1303. return res;
  1304. res = sas_ex_general(dev);
  1305. if (res)
  1306. goto out_err;
  1307. res = sas_ex_manuf_info(dev);
  1308. if (res)
  1309. goto out_err;
  1310. res = sas_expander_discover(dev);
  1311. if (res) {
  1312. SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
  1313. SAS_ADDR(dev->sas_addr), res);
  1314. goto out_err;
  1315. }
  1316. sas_check_ex_subtractive_boundary(dev);
  1317. res = sas_check_parent_topology(dev);
  1318. if (res)
  1319. goto out_err;
  1320. return 0;
  1321. out_err:
  1322. sas_notify_lldd_dev_gone(dev);
  1323. return res;
  1324. }
  1325. static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
  1326. {
  1327. int res = 0;
  1328. struct domain_device *dev;
  1329. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  1330. if (dev->dev_type == EDGE_DEV ||
  1331. dev->dev_type == FANOUT_DEV) {
  1332. struct sas_expander_device *ex =
  1333. rphy_to_expander_device(dev->rphy);
  1334. if (level == ex->level)
  1335. res = sas_ex_discover_devices(dev, -1);
  1336. else if (level > 0)
  1337. res = sas_ex_discover_devices(port->port_dev, -1);
  1338. }
  1339. }
  1340. return res;
  1341. }
  1342. static int sas_ex_bfs_disc(struct asd_sas_port *port)
  1343. {
  1344. int res;
  1345. int level;
  1346. do {
  1347. level = port->disc.max_level;
  1348. res = sas_ex_level_discovery(port, level);
  1349. mb();
  1350. } while (level < port->disc.max_level);
  1351. return res;
  1352. }
  1353. int sas_discover_root_expander(struct domain_device *dev)
  1354. {
  1355. int res;
  1356. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1357. res = sas_rphy_add(dev->rphy);
  1358. if (res)
  1359. goto out_err;
  1360. ex->level = dev->port->disc.max_level; /* 0 */
  1361. res = sas_discover_expander(dev);
  1362. if (res)
  1363. goto out_err2;
  1364. sas_ex_bfs_disc(dev->port);
  1365. return res;
  1366. out_err2:
  1367. sas_rphy_remove(dev->rphy);
  1368. out_err:
  1369. return res;
  1370. }
  1371. /* ---------- Domain revalidation ---------- */
  1372. static int sas_get_phy_discover(struct domain_device *dev,
  1373. int phy_id, struct smp_resp *disc_resp)
  1374. {
  1375. int res;
  1376. u8 *disc_req;
  1377. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  1378. if (!disc_req)
  1379. return -ENOMEM;
  1380. disc_req[1] = SMP_DISCOVER;
  1381. disc_req[9] = phy_id;
  1382. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  1383. disc_resp, DISCOVER_RESP_SIZE);
  1384. if (res)
  1385. goto out;
  1386. else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
  1387. res = disc_resp->result;
  1388. goto out;
  1389. }
  1390. out:
  1391. kfree(disc_req);
  1392. return res;
  1393. }
  1394. static int sas_get_phy_change_count(struct domain_device *dev,
  1395. int phy_id, int *pcc)
  1396. {
  1397. int res;
  1398. struct smp_resp *disc_resp;
  1399. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1400. if (!disc_resp)
  1401. return -ENOMEM;
  1402. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1403. if (!res)
  1404. *pcc = disc_resp->disc.change_count;
  1405. kfree(disc_resp);
  1406. return res;
  1407. }
  1408. int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
  1409. u8 *attached_sas_addr)
  1410. {
  1411. int res;
  1412. struct smp_resp *disc_resp;
  1413. struct discover_resp *dr;
  1414. disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
  1415. if (!disc_resp)
  1416. return -ENOMEM;
  1417. dr = &disc_resp->disc;
  1418. res = sas_get_phy_discover(dev, phy_id, disc_resp);
  1419. if (!res) {
  1420. memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
  1421. if (dr->attached_dev_type == 0)
  1422. memset(attached_sas_addr, 0, 8);
  1423. }
  1424. kfree(disc_resp);
  1425. return res;
  1426. }
  1427. static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
  1428. int from_phy, bool update)
  1429. {
  1430. struct expander_device *ex = &dev->ex_dev;
  1431. int res = 0;
  1432. int i;
  1433. for (i = from_phy; i < ex->num_phys; i++) {
  1434. int phy_change_count = 0;
  1435. res = sas_get_phy_change_count(dev, i, &phy_change_count);
  1436. if (res)
  1437. goto out;
  1438. else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
  1439. if (update)
  1440. ex->ex_phy[i].phy_change_count =
  1441. phy_change_count;
  1442. *phy_id = i;
  1443. return 0;
  1444. }
  1445. }
  1446. out:
  1447. return res;
  1448. }
  1449. static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
  1450. {
  1451. int res;
  1452. u8 *rg_req;
  1453. struct smp_resp *rg_resp;
  1454. rg_req = alloc_smp_req(RG_REQ_SIZE);
  1455. if (!rg_req)
  1456. return -ENOMEM;
  1457. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  1458. if (!rg_resp) {
  1459. kfree(rg_req);
  1460. return -ENOMEM;
  1461. }
  1462. rg_req[1] = SMP_REPORT_GENERAL;
  1463. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  1464. RG_RESP_SIZE);
  1465. if (res)
  1466. goto out;
  1467. if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  1468. res = rg_resp->result;
  1469. goto out;
  1470. }
  1471. *ecc = be16_to_cpu(rg_resp->rg.change_count);
  1472. out:
  1473. kfree(rg_resp);
  1474. kfree(rg_req);
  1475. return res;
  1476. }
  1477. /**
  1478. * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
  1479. * @dev:domain device to be detect.
  1480. * @src_dev: the device which originated BROADCAST(CHANGE).
  1481. *
  1482. * Add self-configuration expander suport. Suppose two expander cascading,
  1483. * when the first level expander is self-configuring, hotplug the disks in
  1484. * second level expander, BROADCAST(CHANGE) will not only be originated
  1485. * in the second level expander, but also be originated in the first level
  1486. * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
  1487. * expander changed count in two level expanders will all increment at least
  1488. * once, but the phy which chang count has changed is the source device which
  1489. * we concerned.
  1490. */
  1491. static int sas_find_bcast_dev(struct domain_device *dev,
  1492. struct domain_device **src_dev)
  1493. {
  1494. struct expander_device *ex = &dev->ex_dev;
  1495. int ex_change_count = -1;
  1496. int phy_id = -1;
  1497. int res;
  1498. struct domain_device *ch;
  1499. res = sas_get_ex_change_count(dev, &ex_change_count);
  1500. if (res)
  1501. goto out;
  1502. if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
  1503. /* Just detect if this expander phys phy change count changed,
  1504. * in order to determine if this expander originate BROADCAST,
  1505. * and do not update phy change count field in our structure.
  1506. */
  1507. res = sas_find_bcast_phy(dev, &phy_id, 0, false);
  1508. if (phy_id != -1) {
  1509. *src_dev = dev;
  1510. ex->ex_change_count = ex_change_count;
  1511. SAS_DPRINTK("Expander phy change count has changed\n");
  1512. return res;
  1513. } else
  1514. SAS_DPRINTK("Expander phys DID NOT change\n");
  1515. }
  1516. list_for_each_entry(ch, &ex->children, siblings) {
  1517. if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
  1518. res = sas_find_bcast_dev(ch, src_dev);
  1519. if (*src_dev)
  1520. return res;
  1521. }
  1522. }
  1523. out:
  1524. return res;
  1525. }
  1526. static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
  1527. {
  1528. struct expander_device *ex = &dev->ex_dev;
  1529. struct domain_device *child, *n;
  1530. list_for_each_entry_safe(child, n, &ex->children, siblings) {
  1531. set_bit(SAS_DEV_GONE, &child->state);
  1532. if (child->dev_type == EDGE_DEV ||
  1533. child->dev_type == FANOUT_DEV)
  1534. sas_unregister_ex_tree(port, child);
  1535. else
  1536. sas_unregister_dev(port, child);
  1537. }
  1538. sas_unregister_dev(port, dev);
  1539. }
  1540. static void sas_unregister_devs_sas_addr(struct domain_device *parent,
  1541. int phy_id, bool last)
  1542. {
  1543. struct expander_device *ex_dev = &parent->ex_dev;
  1544. struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
  1545. struct domain_device *child, *n;
  1546. if (last) {
  1547. list_for_each_entry_safe(child, n,
  1548. &ex_dev->children, siblings) {
  1549. if (SAS_ADDR(child->sas_addr) ==
  1550. SAS_ADDR(phy->attached_sas_addr)) {
  1551. set_bit(SAS_DEV_GONE, &child->state);
  1552. if (child->dev_type == EDGE_DEV ||
  1553. child->dev_type == FANOUT_DEV)
  1554. sas_unregister_ex_tree(parent->port, child);
  1555. else
  1556. sas_unregister_dev(parent->port, child);
  1557. break;
  1558. }
  1559. }
  1560. sas_disable_routing(parent, phy->attached_sas_addr);
  1561. }
  1562. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  1563. if (phy->port) {
  1564. sas_port_delete_phy(phy->port, phy->phy);
  1565. if (phy->port->num_phys == 0)
  1566. sas_port_delete(phy->port);
  1567. phy->port = NULL;
  1568. }
  1569. }
  1570. static int sas_discover_bfs_by_root_level(struct domain_device *root,
  1571. const int level)
  1572. {
  1573. struct expander_device *ex_root = &root->ex_dev;
  1574. struct domain_device *child;
  1575. int res = 0;
  1576. list_for_each_entry(child, &ex_root->children, siblings) {
  1577. if (child->dev_type == EDGE_DEV ||
  1578. child->dev_type == FANOUT_DEV) {
  1579. struct sas_expander_device *ex =
  1580. rphy_to_expander_device(child->rphy);
  1581. if (level > ex->level)
  1582. res = sas_discover_bfs_by_root_level(child,
  1583. level);
  1584. else if (level == ex->level)
  1585. res = sas_ex_discover_devices(child, -1);
  1586. }
  1587. }
  1588. return res;
  1589. }
  1590. static int sas_discover_bfs_by_root(struct domain_device *dev)
  1591. {
  1592. int res;
  1593. struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
  1594. int level = ex->level+1;
  1595. res = sas_ex_discover_devices(dev, -1);
  1596. if (res)
  1597. goto out;
  1598. do {
  1599. res = sas_discover_bfs_by_root_level(dev, level);
  1600. mb();
  1601. level += 1;
  1602. } while (level <= dev->port->disc.max_level);
  1603. out:
  1604. return res;
  1605. }
  1606. static int sas_discover_new(struct domain_device *dev, int phy_id)
  1607. {
  1608. struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
  1609. struct domain_device *child;
  1610. bool found = false;
  1611. int res, i;
  1612. SAS_DPRINTK("ex %016llx phy%d new device attached\n",
  1613. SAS_ADDR(dev->sas_addr), phy_id);
  1614. res = sas_ex_phy_discover(dev, phy_id);
  1615. if (res)
  1616. goto out;
  1617. /* to support the wide port inserted */
  1618. for (i = 0; i < dev->ex_dev.num_phys; i++) {
  1619. struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
  1620. if (i == phy_id)
  1621. continue;
  1622. if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
  1623. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1624. found = true;
  1625. break;
  1626. }
  1627. }
  1628. if (found) {
  1629. sas_ex_join_wide_port(dev, phy_id);
  1630. return 0;
  1631. }
  1632. res = sas_ex_discover_devices(dev, phy_id);
  1633. if (!res)
  1634. goto out;
  1635. list_for_each_entry(child, &dev->ex_dev.children, siblings) {
  1636. if (SAS_ADDR(child->sas_addr) ==
  1637. SAS_ADDR(ex_phy->attached_sas_addr)) {
  1638. if (child->dev_type == EDGE_DEV ||
  1639. child->dev_type == FANOUT_DEV)
  1640. res = sas_discover_bfs_by_root(child);
  1641. break;
  1642. }
  1643. }
  1644. out:
  1645. return res;
  1646. }
  1647. static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
  1648. {
  1649. struct expander_device *ex = &dev->ex_dev;
  1650. struct ex_phy *phy = &ex->ex_phy[phy_id];
  1651. u8 attached_sas_addr[8];
  1652. int res;
  1653. res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
  1654. switch (res) {
  1655. case SMP_RESP_NO_PHY:
  1656. phy->phy_state = PHY_NOT_PRESENT;
  1657. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1658. goto out; break;
  1659. case SMP_RESP_PHY_VACANT:
  1660. phy->phy_state = PHY_VACANT;
  1661. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1662. goto out; break;
  1663. case SMP_RESP_FUNC_ACC:
  1664. break;
  1665. }
  1666. if (SAS_ADDR(attached_sas_addr) == 0) {
  1667. phy->phy_state = PHY_EMPTY;
  1668. sas_unregister_devs_sas_addr(dev, phy_id, last);
  1669. } else if (SAS_ADDR(attached_sas_addr) ==
  1670. SAS_ADDR(phy->attached_sas_addr)) {
  1671. SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
  1672. SAS_ADDR(dev->sas_addr), phy_id);
  1673. sas_ex_phy_discover(dev, phy_id);
  1674. } else
  1675. res = sas_discover_new(dev, phy_id);
  1676. out:
  1677. return res;
  1678. }
  1679. /**
  1680. * sas_rediscover - revalidate the domain.
  1681. * @dev:domain device to be detect.
  1682. * @phy_id: the phy id will be detected.
  1683. *
  1684. * NOTE: this process _must_ quit (return) as soon as any connection
  1685. * errors are encountered. Connection recovery is done elsewhere.
  1686. * Discover process only interrogates devices in order to discover the
  1687. * domain.For plugging out, we un-register the device only when it is
  1688. * the last phy in the port, for other phys in this port, we just delete it
  1689. * from the port.For inserting, we do discovery when it is the
  1690. * first phy,for other phys in this port, we add it to the port to
  1691. * forming the wide-port.
  1692. */
  1693. static int sas_rediscover(struct domain_device *dev, const int phy_id)
  1694. {
  1695. struct expander_device *ex = &dev->ex_dev;
  1696. struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
  1697. int res = 0;
  1698. int i;
  1699. bool last = true; /* is this the last phy of the port */
  1700. SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
  1701. SAS_ADDR(dev->sas_addr), phy_id);
  1702. if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
  1703. for (i = 0; i < ex->num_phys; i++) {
  1704. struct ex_phy *phy = &ex->ex_phy[i];
  1705. if (i == phy_id)
  1706. continue;
  1707. if (SAS_ADDR(phy->attached_sas_addr) ==
  1708. SAS_ADDR(changed_phy->attached_sas_addr)) {
  1709. SAS_DPRINTK("phy%d part of wide port with "
  1710. "phy%d\n", phy_id, i);
  1711. last = false;
  1712. break;
  1713. }
  1714. }
  1715. res = sas_rediscover_dev(dev, phy_id, last);
  1716. } else
  1717. res = sas_discover_new(dev, phy_id);
  1718. return res;
  1719. }
  1720. /**
  1721. * sas_revalidate_domain -- revalidate the domain
  1722. * @port: port to the domain of interest
  1723. *
  1724. * NOTE: this process _must_ quit (return) as soon as any connection
  1725. * errors are encountered. Connection recovery is done elsewhere.
  1726. * Discover process only interrogates devices in order to discover the
  1727. * domain.
  1728. */
  1729. int sas_ex_revalidate_domain(struct domain_device *port_dev)
  1730. {
  1731. int res;
  1732. struct domain_device *dev = NULL;
  1733. res = sas_find_bcast_dev(port_dev, &dev);
  1734. if (res)
  1735. goto out;
  1736. if (dev) {
  1737. struct expander_device *ex = &dev->ex_dev;
  1738. int i = 0, phy_id;
  1739. do {
  1740. phy_id = -1;
  1741. res = sas_find_bcast_phy(dev, &phy_id, i, true);
  1742. if (phy_id == -1)
  1743. break;
  1744. res = sas_rediscover(dev, phy_id);
  1745. i = phy_id + 1;
  1746. } while (i < ex->num_phys);
  1747. }
  1748. out:
  1749. return res;
  1750. }
  1751. int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1752. struct request *req)
  1753. {
  1754. struct domain_device *dev;
  1755. int ret, type;
  1756. struct request *rsp = req->next_rq;
  1757. if (!rsp) {
  1758. printk("%s: space for a smp response is missing\n",
  1759. __func__);
  1760. return -EINVAL;
  1761. }
  1762. /* no rphy means no smp target support (ie aic94xx host) */
  1763. if (!rphy)
  1764. return sas_smp_host_handler(shost, req, rsp);
  1765. type = rphy->identify.device_type;
  1766. if (type != SAS_EDGE_EXPANDER_DEVICE &&
  1767. type != SAS_FANOUT_EXPANDER_DEVICE) {
  1768. printk("%s: can we send a smp request to a device?\n",
  1769. __func__);
  1770. return -EINVAL;
  1771. }
  1772. dev = sas_find_dev_by_rphy(rphy);
  1773. if (!dev) {
  1774. printk("%s: fail to find a domain_device?\n", __func__);
  1775. return -EINVAL;
  1776. }
  1777. /* do we need to support multiple segments? */
  1778. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1779. printk("%s: multiple segments req %u %u, rsp %u %u\n",
  1780. __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
  1781. rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  1782. return -EINVAL;
  1783. }
  1784. ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
  1785. bio_data(rsp->bio), blk_rq_bytes(rsp));
  1786. if (ret > 0) {
  1787. /* positive number is the untransferred residual */
  1788. rsp->resid_len = ret;
  1789. req->resid_len = 0;
  1790. ret = 0;
  1791. } else if (ret == 0) {
  1792. rsp->resid_len = 0;
  1793. req->resid_len = 0;
  1794. }
  1795. return ret;
  1796. }