sas_expander.c 52 KB

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