sas_expander.c 51 KB

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