sas_expander.c 51 KB

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