sas_expander.c 50 KB

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