sas_expander.c 54 KB

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