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 <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. return;
  153. default:
  154. phy->phy_state = PHY_NOT_PRESENT;
  155. return;
  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.initiator_port_protocols = phy->attached_iproto;
  175. phy->phy->identify.target_port_protocols = phy->attached_tproto;
  176. phy->phy->identify.phy_identifier = phy_id;
  177. phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
  178. phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
  179. phy->phy->minimum_linkrate = dr->pmin_linkrate;
  180. phy->phy->maximum_linkrate = dr->pmax_linkrate;
  181. phy->phy->negotiated_linkrate = phy->linkrate;
  182. if (!rediscover)
  183. sas_phy_add(phy->phy);
  184. SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
  185. SAS_ADDR(dev->sas_addr), phy->phy_id,
  186. phy->routing_attr == TABLE_ROUTING ? 'T' :
  187. phy->routing_attr == DIRECT_ROUTING ? 'D' :
  188. phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
  189. SAS_ADDR(phy->attached_sas_addr));
  190. return;
  191. }
  192. #define DISCOVER_REQ_SIZE 16
  193. #define DISCOVER_RESP_SIZE 56
  194. static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
  195. u8 *disc_resp, int single)
  196. {
  197. int i, res;
  198. disc_req[9] = single;
  199. for (i = 1 ; i < 3; i++) {
  200. struct discover_resp *dr;
  201. res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
  202. disc_resp, DISCOVER_RESP_SIZE);
  203. if (res)
  204. return res;
  205. /* This is detecting a failure to transmit inital
  206. * dev to host FIS as described in section G.5 of
  207. * sas-2 r 04b */
  208. dr = &((struct smp_resp *)disc_resp)->disc;
  209. if (!(dr->attached_dev_type == 0 &&
  210. dr->attached_sata_dev))
  211. break;
  212. /* In order to generate the dev to host FIS, we
  213. * send a link reset to the expander port */
  214. sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
  215. /* Wait for the reset to trigger the negotiation */
  216. msleep(500);
  217. }
  218. sas_set_ex_phy(dev, single, disc_resp);
  219. return 0;
  220. }
  221. static int sas_ex_phy_discover(struct domain_device *dev, int single)
  222. {
  223. struct expander_device *ex = &dev->ex_dev;
  224. int res = 0;
  225. u8 *disc_req;
  226. u8 *disc_resp;
  227. disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
  228. if (!disc_req)
  229. return -ENOMEM;
  230. disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
  231. if (!disc_resp) {
  232. kfree(disc_req);
  233. return -ENOMEM;
  234. }
  235. disc_req[1] = SMP_DISCOVER;
  236. if (0 <= single && single < ex->num_phys) {
  237. res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
  238. } else {
  239. int i;
  240. for (i = 0; i < ex->num_phys; i++) {
  241. res = sas_ex_phy_discover_helper(dev, disc_req,
  242. disc_resp, i);
  243. if (res)
  244. goto out_err;
  245. }
  246. }
  247. out_err:
  248. kfree(disc_resp);
  249. kfree(disc_req);
  250. return res;
  251. }
  252. static int sas_expander_discover(struct domain_device *dev)
  253. {
  254. struct expander_device *ex = &dev->ex_dev;
  255. int res = -ENOMEM;
  256. ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
  257. if (!ex->ex_phy)
  258. return -ENOMEM;
  259. res = sas_ex_phy_discover(dev, -1);
  260. if (res)
  261. goto out_err;
  262. return 0;
  263. out_err:
  264. kfree(ex->ex_phy);
  265. ex->ex_phy = NULL;
  266. return res;
  267. }
  268. #define MAX_EXPANDER_PHYS 128
  269. static void ex_assign_report_general(struct domain_device *dev,
  270. struct smp_resp *resp)
  271. {
  272. struct report_general_resp *rg = &resp->rg;
  273. dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
  274. dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
  275. dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
  276. dev->ex_dev.conf_route_table = rg->conf_route_table;
  277. dev->ex_dev.configuring = rg->configuring;
  278. memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
  279. }
  280. #define RG_REQ_SIZE 8
  281. #define RG_RESP_SIZE 32
  282. static int sas_ex_general(struct domain_device *dev)
  283. {
  284. u8 *rg_req;
  285. struct smp_resp *rg_resp;
  286. int res;
  287. int i;
  288. rg_req = alloc_smp_req(RG_REQ_SIZE);
  289. if (!rg_req)
  290. return -ENOMEM;
  291. rg_resp = alloc_smp_resp(RG_RESP_SIZE);
  292. if (!rg_resp) {
  293. kfree(rg_req);
  294. return -ENOMEM;
  295. }
  296. rg_req[1] = SMP_REPORT_GENERAL;
  297. for (i = 0; i < 5; i++) {
  298. res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
  299. RG_RESP_SIZE);
  300. if (res) {
  301. SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
  302. SAS_ADDR(dev->sas_addr), res);
  303. goto out;
  304. } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
  305. SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
  306. SAS_ADDR(dev->sas_addr), rg_resp->result);
  307. res = rg_resp->result;
  308. goto out;
  309. }
  310. ex_assign_report_general(dev, rg_resp);
  311. if (dev->ex_dev.configuring) {
  312. SAS_DPRINTK("RG: ex %llx self-configuring...\n",
  313. SAS_ADDR(dev->sas_addr));
  314. schedule_timeout_interruptible(5*HZ);
  315. } else
  316. break;
  317. }
  318. out:
  319. kfree(rg_req);
  320. kfree(rg_resp);
  321. return res;
  322. }
  323. static void ex_assign_manuf_info(struct domain_device *dev, void
  324. *_mi_resp)
  325. {
  326. u8 *mi_resp = _mi_resp;
  327. struct sas_rphy *rphy = dev->rphy;
  328. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  329. memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
  330. memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
  331. memcpy(edev->product_rev, mi_resp + 36,
  332. SAS_EXPANDER_PRODUCT_REV_LEN);
  333. if (mi_resp[8] & 1) {
  334. memcpy(edev->component_vendor_id, mi_resp + 40,
  335. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  336. edev->component_id = mi_resp[48] << 8 | mi_resp[49];
  337. edev->component_revision_id = mi_resp[50];
  338. }
  339. }
  340. #define MI_REQ_SIZE 8
  341. #define MI_RESP_SIZE 64
  342. static int sas_ex_manuf_info(struct domain_device *dev)
  343. {
  344. u8 *mi_req;
  345. u8 *mi_resp;
  346. int res;
  347. mi_req = alloc_smp_req(MI_REQ_SIZE);
  348. if (!mi_req)
  349. return -ENOMEM;
  350. mi_resp = alloc_smp_resp(MI_RESP_SIZE);
  351. if (!mi_resp) {
  352. kfree(mi_req);
  353. return -ENOMEM;
  354. }
  355. mi_req[1] = SMP_REPORT_MANUF_INFO;
  356. res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
  357. if (res) {
  358. SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
  359. SAS_ADDR(dev->sas_addr), res);
  360. goto out;
  361. } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
  362. SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
  363. SAS_ADDR(dev->sas_addr), mi_resp[2]);
  364. goto out;
  365. }
  366. ex_assign_manuf_info(dev, mi_resp);
  367. out:
  368. kfree(mi_req);
  369. kfree(mi_resp);
  370. return res;
  371. }
  372. #define PC_REQ_SIZE 44
  373. #define PC_RESP_SIZE 8
  374. int sas_smp_phy_control(struct domain_device *dev, int phy_id,
  375. enum phy_func phy_func,
  376. struct sas_phy_linkrates *rates)
  377. {
  378. u8 *pc_req;
  379. u8 *pc_resp;
  380. int res;
  381. pc_req = alloc_smp_req(PC_REQ_SIZE);
  382. if (!pc_req)
  383. return -ENOMEM;
  384. pc_resp = alloc_smp_resp(PC_RESP_SIZE);
  385. if (!pc_resp) {
  386. kfree(pc_req);
  387. return -ENOMEM;
  388. }
  389. pc_req[1] = SMP_PHY_CONTROL;
  390. pc_req[9] = phy_id;
  391. pc_req[10]= phy_func;
  392. if (rates) {
  393. pc_req[32] = rates->minimum_linkrate << 4;
  394. pc_req[33] = rates->maximum_linkrate << 4;
  395. }
  396. res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
  397. kfree(pc_resp);
  398. kfree(pc_req);
  399. return res;
  400. }
  401. static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
  402. {
  403. struct expander_device *ex = &dev->ex_dev;
  404. struct ex_phy *phy = &ex->ex_phy[phy_id];
  405. sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
  406. phy->linkrate = SAS_PHY_DISABLED;
  407. }
  408. static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
  409. {
  410. struct expander_device *ex = &dev->ex_dev;
  411. int i;
  412. for (i = 0; i < ex->num_phys; i++) {
  413. struct ex_phy *phy = &ex->ex_phy[i];
  414. if (phy->phy_state == PHY_VACANT ||
  415. phy->phy_state == PHY_NOT_PRESENT)
  416. continue;
  417. if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
  418. sas_ex_disable_phy(dev, i);
  419. }
  420. }
  421. static int sas_dev_present_in_domain(struct asd_sas_port *port,
  422. u8 *sas_addr)
  423. {
  424. struct domain_device *dev;
  425. if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
  426. return 1;
  427. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  428. if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
  429. return 1;
  430. }
  431. return 0;
  432. }
  433. #define RPEL_REQ_SIZE 16
  434. #define RPEL_RESP_SIZE 32
  435. int sas_smp_get_phy_events(struct sas_phy *phy)
  436. {
  437. int res;
  438. u8 *req;
  439. u8 *resp;
  440. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  441. struct domain_device *dev = sas_find_dev_by_rphy(rphy);
  442. req = alloc_smp_req(RPEL_REQ_SIZE);
  443. if (!req)
  444. return -ENOMEM;
  445. resp = alloc_smp_resp(RPEL_RESP_SIZE);
  446. if (!resp) {
  447. kfree(req);
  448. return -ENOMEM;
  449. }
  450. req[1] = SMP_REPORT_PHY_ERR_LOG;
  451. req[9] = phy->number;
  452. res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
  453. resp, RPEL_RESP_SIZE);
  454. if (!res)
  455. goto out;
  456. phy->invalid_dword_count = scsi_to_u32(&resp[12]);
  457. phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
  458. phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
  459. phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
  460. out:
  461. kfree(resp);
  462. return res;
  463. }
  464. #ifdef CONFIG_SCSI_SAS_ATA
  465. #define RPS_REQ_SIZE 16
  466. #define RPS_RESP_SIZE 60
  467. static int sas_get_report_phy_sata(struct domain_device *dev,
  468. int phy_id,
  469. struct smp_resp *rps_resp)
  470. {
  471. int res;
  472. u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
  473. u8 *resp = (u8 *)rps_resp;
  474. if (!rps_req)
  475. return -ENOMEM;
  476. rps_req[1] = SMP_REPORT_PHY_SATA;
  477. rps_req[9] = phy_id;
  478. res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
  479. rps_resp, RPS_RESP_SIZE);
  480. /* 0x34 is the FIS type for the D2H fis. There's a potential
  481. * standards cockup here. sas-2 explicitly specifies the FIS
  482. * should be encoded so that FIS type is in resp[24].
  483. * However, some expanders endian reverse this. Undo the
  484. * reversal here */
  485. if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
  486. int i;
  487. for (i = 0; i < 5; i++) {
  488. int j = 24 + (i*4);
  489. u8 a, b;
  490. a = resp[j + 0];
  491. b = resp[j + 1];
  492. resp[j + 0] = resp[j + 3];
  493. resp[j + 1] = resp[j + 2];
  494. resp[j + 2] = b;
  495. resp[j + 3] = a;
  496. }
  497. }
  498. kfree(rps_req);
  499. return res;
  500. }
  501. #endif
  502. static void sas_ex_get_linkrate(struct domain_device *parent,
  503. struct domain_device *child,
  504. struct ex_phy *parent_phy)
  505. {
  506. struct expander_device *parent_ex = &parent->ex_dev;
  507. struct sas_port *port;
  508. int i;
  509. child->pathways = 0;
  510. port = parent_phy->port;
  511. for (i = 0; i < parent_ex->num_phys; i++) {
  512. struct ex_phy *phy = &parent_ex->ex_phy[i];
  513. if (phy->phy_state == PHY_VACANT ||
  514. phy->phy_state == PHY_NOT_PRESENT)
  515. continue;
  516. if (SAS_ADDR(phy->attached_sas_addr) ==
  517. SAS_ADDR(child->sas_addr)) {
  518. child->min_linkrate = min(parent->min_linkrate,
  519. phy->linkrate);
  520. child->max_linkrate = max(parent->max_linkrate,
  521. phy->linkrate);
  522. child->pathways++;
  523. sas_port_add_phy(port, phy->phy);
  524. }
  525. }
  526. child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
  527. child->pathways = min(child->pathways, parent->pathways);
  528. }
  529. static struct domain_device *sas_ex_discover_end_dev(
  530. struct domain_device *parent, int phy_id)
  531. {
  532. struct expander_device *parent_ex = &parent->ex_dev;
  533. struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
  534. struct domain_device *child = NULL;
  535. struct sas_rphy *rphy;
  536. int res;
  537. if (phy->attached_sata_host || phy->attached_sata_ps)
  538. return NULL;
  539. child = kzalloc(sizeof(*child), GFP_KERNEL);
  540. if (!child)
  541. return NULL;
  542. child->parent = parent;
  543. child->port = parent->port;
  544. child->iproto = phy->attached_iproto;
  545. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  546. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  547. if (!phy->port) {
  548. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  549. if (unlikely(!phy->port))
  550. goto out_err;
  551. if (unlikely(sas_port_add(phy->port) != 0)) {
  552. sas_port_free(phy->port);
  553. goto out_err;
  554. }
  555. }
  556. sas_ex_get_linkrate(parent, child, phy);
  557. #ifdef CONFIG_SCSI_SAS_ATA
  558. if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
  559. child->dev_type = SATA_DEV;
  560. if (phy->attached_tproto & SAS_PROTOCOL_STP)
  561. child->tproto = phy->attached_tproto;
  562. if (phy->attached_sata_dev)
  563. child->tproto |= SATA_DEV;
  564. res = sas_get_report_phy_sata(parent, phy_id,
  565. &child->sata_dev.rps_resp);
  566. if (res) {
  567. SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
  568. "0x%x\n", SAS_ADDR(parent->sas_addr),
  569. phy_id, res);
  570. goto out_free;
  571. }
  572. memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
  573. sizeof(struct dev_to_host_fis));
  574. rphy = sas_end_device_alloc(phy->port);
  575. if (unlikely(!rphy))
  576. goto out_free;
  577. sas_init_dev(child);
  578. child->rphy = rphy;
  579. spin_lock_irq(&parent->port->dev_list_lock);
  580. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  581. spin_unlock_irq(&parent->port->dev_list_lock);
  582. res = sas_discover_sata(child);
  583. if (res) {
  584. SAS_DPRINTK("sas_discover_sata() for device %16llx at "
  585. "%016llx:0x%x returned 0x%x\n",
  586. SAS_ADDR(child->sas_addr),
  587. SAS_ADDR(parent->sas_addr), phy_id, res);
  588. goto out_list_del;
  589. }
  590. } else
  591. #endif
  592. if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
  593. child->dev_type = SAS_END_DEV;
  594. rphy = sas_end_device_alloc(phy->port);
  595. /* FIXME: error handling */
  596. if (unlikely(!rphy))
  597. goto out_free;
  598. child->tproto = phy->attached_tproto;
  599. sas_init_dev(child);
  600. child->rphy = rphy;
  601. sas_fill_in_rphy(child, rphy);
  602. spin_lock_irq(&parent->port->dev_list_lock);
  603. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  604. spin_unlock_irq(&parent->port->dev_list_lock);
  605. res = sas_discover_end_dev(child);
  606. if (res) {
  607. SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
  608. "at %016llx:0x%x returned 0x%x\n",
  609. SAS_ADDR(child->sas_addr),
  610. SAS_ADDR(parent->sas_addr), phy_id, res);
  611. goto out_list_del;
  612. }
  613. } else {
  614. SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
  615. phy->attached_tproto, SAS_ADDR(parent->sas_addr),
  616. phy_id);
  617. goto out_free;
  618. }
  619. list_add_tail(&child->siblings, &parent_ex->children);
  620. return child;
  621. out_list_del:
  622. sas_rphy_free(child->rphy);
  623. child->rphy = NULL;
  624. list_del(&child->dev_list_node);
  625. out_free:
  626. sas_port_delete(phy->port);
  627. out_err:
  628. phy->port = NULL;
  629. kfree(child);
  630. return NULL;
  631. }
  632. /* See if this phy is part of a wide port */
  633. static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
  634. {
  635. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  636. int i;
  637. for (i = 0; i < parent->ex_dev.num_phys; i++) {
  638. struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
  639. if (ephy == phy)
  640. continue;
  641. if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
  642. SAS_ADDR_SIZE) && ephy->port) {
  643. sas_port_add_phy(ephy->port, phy->phy);
  644. phy->port = ephy->port;
  645. phy->phy_state = PHY_DEVICE_DISCOVERED;
  646. return 0;
  647. }
  648. }
  649. return -ENODEV;
  650. }
  651. static struct domain_device *sas_ex_discover_expander(
  652. struct domain_device *parent, int phy_id)
  653. {
  654. struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
  655. struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
  656. struct domain_device *child = NULL;
  657. struct sas_rphy *rphy;
  658. struct sas_expander_device *edev;
  659. struct asd_sas_port *port;
  660. int res;
  661. if (phy->routing_attr == DIRECT_ROUTING) {
  662. SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
  663. "allowed\n",
  664. SAS_ADDR(parent->sas_addr), phy_id,
  665. SAS_ADDR(phy->attached_sas_addr),
  666. phy->attached_phy_id);
  667. return NULL;
  668. }
  669. child = kzalloc(sizeof(*child), GFP_KERNEL);
  670. if (!child)
  671. return NULL;
  672. phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
  673. /* FIXME: better error handling */
  674. BUG_ON(sas_port_add(phy->port) != 0);
  675. switch (phy->attached_dev_type) {
  676. case EDGE_DEV:
  677. rphy = sas_expander_alloc(phy->port,
  678. SAS_EDGE_EXPANDER_DEVICE);
  679. break;
  680. case FANOUT_DEV:
  681. rphy = sas_expander_alloc(phy->port,
  682. SAS_FANOUT_EXPANDER_DEVICE);
  683. break;
  684. default:
  685. rphy = NULL; /* shut gcc up */
  686. BUG();
  687. }
  688. port = parent->port;
  689. child->rphy = rphy;
  690. edev = rphy_to_expander_device(rphy);
  691. child->dev_type = phy->attached_dev_type;
  692. child->parent = parent;
  693. child->port = port;
  694. child->iproto = phy->attached_iproto;
  695. child->tproto = phy->attached_tproto;
  696. memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
  697. sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
  698. sas_ex_get_linkrate(parent, child, phy);
  699. edev->level = parent_ex->level + 1;
  700. parent->port->disc.max_level = max(parent->port->disc.max_level,
  701. edev->level);
  702. sas_init_dev(child);
  703. sas_fill_in_rphy(child, rphy);
  704. sas_rphy_add(rphy);
  705. spin_lock_irq(&parent->port->dev_list_lock);
  706. list_add_tail(&child->dev_list_node, &parent->port->dev_list);
  707. spin_unlock_irq(&parent->port->dev_list_lock);
  708. res = sas_discover_expander(child);
  709. if (res) {
  710. kfree(child);
  711. return NULL;
  712. }
  713. list_add_tail(&child->siblings, &parent->ex_dev.children);
  714. return child;
  715. }
  716. static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
  717. {
  718. struct expander_device *ex = &dev->ex_dev;
  719. struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
  720. struct domain_device *child = NULL;
  721. int res = 0;
  722. /* Phy state */
  723. if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
  724. if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
  725. res = sas_ex_phy_discover(dev, phy_id);
  726. if (res)
  727. return res;
  728. }
  729. /* Parent and domain coherency */
  730. if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  731. SAS_ADDR(dev->port->sas_addr))) {
  732. sas_add_parent_port(dev, phy_id);
  733. return 0;
  734. }
  735. if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
  736. SAS_ADDR(dev->parent->sas_addr))) {
  737. sas_add_parent_port(dev, phy_id);
  738. if (ex_phy->routing_attr == TABLE_ROUTING)
  739. sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
  740. return 0;
  741. }
  742. if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
  743. sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
  744. if (ex_phy->attached_dev_type == NO_DEVICE) {
  745. if (ex_phy->routing_attr == DIRECT_ROUTING) {
  746. memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  747. sas_configure_routing(dev, ex_phy->attached_sas_addr);
  748. }
  749. return 0;
  750. } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
  751. return 0;
  752. if (ex_phy->attached_dev_type != SAS_END_DEV &&
  753. ex_phy->attached_dev_type != FANOUT_DEV &&
  754. ex_phy->attached_dev_type != EDGE_DEV) {
  755. SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
  756. "phy 0x%x\n", ex_phy->attached_dev_type,
  757. SAS_ADDR(dev->sas_addr),
  758. phy_id);
  759. return 0;
  760. }
  761. res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
  762. if (res) {
  763. SAS_DPRINTK("configure routing for dev %016llx "
  764. "reported 0x%x. Forgotten\n",
  765. SAS_ADDR(ex_phy->attached_sas_addr), res);
  766. sas_disable_routing(dev, ex_phy->attached_sas_addr);
  767. return res;
  768. }
  769. res = sas_ex_join_wide_port(dev, phy_id);
  770. if (!res) {
  771. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  772. phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
  773. return res;
  774. }
  775. switch (ex_phy->attached_dev_type) {
  776. case SAS_END_DEV:
  777. child = sas_ex_discover_end_dev(dev, phy_id);
  778. break;
  779. case FANOUT_DEV:
  780. if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
  781. SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
  782. "attached to ex %016llx phy 0x%x\n",
  783. SAS_ADDR(ex_phy->attached_sas_addr),
  784. ex_phy->attached_phy_id,
  785. SAS_ADDR(dev->sas_addr),
  786. phy_id);
  787. sas_ex_disable_phy(dev, phy_id);
  788. break;
  789. } else
  790. memcpy(dev->port->disc.fanout_sas_addr,
  791. ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
  792. /* fallthrough */
  793. case EDGE_DEV:
  794. child = sas_ex_discover_expander(dev, phy_id);
  795. break;
  796. default:
  797. break;
  798. }
  799. if (child) {
  800. int i;
  801. for (i = 0; i < ex->num_phys; i++) {
  802. if (ex->ex_phy[i].phy_state == PHY_VACANT ||
  803. ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
  804. continue;
  805. /*
  806. * Due to races, the phy might not get added to the
  807. * wide port, so we add the phy to the wide port here.
  808. */
  809. if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
  810. SAS_ADDR(child->sas_addr)) {
  811. ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
  812. res = sas_ex_join_wide_port(dev, i);
  813. if (!res)
  814. SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
  815. i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
  816. }
  817. }
  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. }