sas_expander.c 50 KB

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