sas_expander.c 52 KB

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