host.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <linux/circ_buf.h>
  56. #include <linux/device.h>
  57. #include <scsi/sas.h>
  58. #include "host.h"
  59. #include "isci.h"
  60. #include "port.h"
  61. #include "probe_roms.h"
  62. #include "remote_device.h"
  63. #include "request.h"
  64. #include "scu_completion_codes.h"
  65. #include "scu_event_codes.h"
  66. #include "registers.h"
  67. #include "scu_remote_node_context.h"
  68. #include "scu_task_context.h"
  69. #define SCU_CONTEXT_RAM_INIT_STALL_TIME 200
  70. #define smu_max_ports(dcc_value) \
  71. (\
  72. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_MASK) \
  73. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_LP_SHIFT) + 1 \
  74. )
  75. #define smu_max_task_contexts(dcc_value) \
  76. (\
  77. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_MASK) \
  78. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_TC_SHIFT) + 1 \
  79. )
  80. #define smu_max_rncs(dcc_value) \
  81. (\
  82. (((dcc_value) & SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_MASK) \
  83. >> SMU_DEVICE_CONTEXT_CAPACITY_MAX_RNC_SHIFT) + 1 \
  84. )
  85. #define SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT 100
  86. /**
  87. *
  88. *
  89. * The number of milliseconds to wait while a given phy is consuming power
  90. * before allowing another set of phys to consume power. Ultimately, this will
  91. * be specified by OEM parameter.
  92. */
  93. #define SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL 500
  94. /**
  95. * NORMALIZE_PUT_POINTER() -
  96. *
  97. * This macro will normalize the completion queue put pointer so its value can
  98. * be used as an array inde
  99. */
  100. #define NORMALIZE_PUT_POINTER(x) \
  101. ((x) & SMU_COMPLETION_QUEUE_PUT_POINTER_MASK)
  102. /**
  103. * NORMALIZE_EVENT_POINTER() -
  104. *
  105. * This macro will normalize the completion queue event entry so its value can
  106. * be used as an index.
  107. */
  108. #define NORMALIZE_EVENT_POINTER(x) \
  109. (\
  110. ((x) & SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_MASK) \
  111. >> SMU_COMPLETION_QUEUE_GET_EVENT_POINTER_SHIFT \
  112. )
  113. /**
  114. * NORMALIZE_GET_POINTER() -
  115. *
  116. * This macro will normalize the completion queue get pointer so its value can
  117. * be used as an index into an array
  118. */
  119. #define NORMALIZE_GET_POINTER(x) \
  120. ((x) & SMU_COMPLETION_QUEUE_GET_POINTER_MASK)
  121. /**
  122. * NORMALIZE_GET_POINTER_CYCLE_BIT() -
  123. *
  124. * This macro will normalize the completion queue cycle pointer so it matches
  125. * the completion queue cycle bit
  126. */
  127. #define NORMALIZE_GET_POINTER_CYCLE_BIT(x) \
  128. ((SMU_CQGR_CYCLE_BIT & (x)) << (31 - SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT))
  129. /**
  130. * COMPLETION_QUEUE_CYCLE_BIT() -
  131. *
  132. * This macro will return the cycle bit of the completion queue entry
  133. */
  134. #define COMPLETION_QUEUE_CYCLE_BIT(x) ((x) & 0x80000000)
  135. /* Init the state machine and call the state entry function (if any) */
  136. void sci_init_sm(struct sci_base_state_machine *sm,
  137. const struct sci_base_state *state_table, u32 initial_state)
  138. {
  139. sci_state_transition_t handler;
  140. sm->initial_state_id = initial_state;
  141. sm->previous_state_id = initial_state;
  142. sm->current_state_id = initial_state;
  143. sm->state_table = state_table;
  144. handler = sm->state_table[initial_state].enter_state;
  145. if (handler)
  146. handler(sm);
  147. }
  148. /* Call the state exit fn, update the current state, call the state entry fn */
  149. void sci_change_state(struct sci_base_state_machine *sm, u32 next_state)
  150. {
  151. sci_state_transition_t handler;
  152. handler = sm->state_table[sm->current_state_id].exit_state;
  153. if (handler)
  154. handler(sm);
  155. sm->previous_state_id = sm->current_state_id;
  156. sm->current_state_id = next_state;
  157. handler = sm->state_table[sm->current_state_id].enter_state;
  158. if (handler)
  159. handler(sm);
  160. }
  161. static bool sci_controller_completion_queue_has_entries(struct isci_host *ihost)
  162. {
  163. u32 get_value = ihost->completion_queue_get;
  164. u32 get_index = get_value & SMU_COMPLETION_QUEUE_GET_POINTER_MASK;
  165. if (NORMALIZE_GET_POINTER_CYCLE_BIT(get_value) ==
  166. COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index]))
  167. return true;
  168. return false;
  169. }
  170. static bool sci_controller_isr(struct isci_host *ihost)
  171. {
  172. if (sci_controller_completion_queue_has_entries(ihost))
  173. return true;
  174. /* we have a spurious interrupt it could be that we have already
  175. * emptied the completion queue from a previous interrupt
  176. * FIXME: really!?
  177. */
  178. writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
  179. /* There is a race in the hardware that could cause us not to be
  180. * notified of an interrupt completion if we do not take this
  181. * step. We will mask then unmask the interrupts so if there is
  182. * another interrupt pending the clearing of the interrupt
  183. * source we get the next interrupt message.
  184. */
  185. spin_lock(&ihost->scic_lock);
  186. if (test_bit(IHOST_IRQ_ENABLED, &ihost->flags)) {
  187. writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
  188. writel(0, &ihost->smu_registers->interrupt_mask);
  189. }
  190. spin_unlock(&ihost->scic_lock);
  191. return false;
  192. }
  193. irqreturn_t isci_msix_isr(int vec, void *data)
  194. {
  195. struct isci_host *ihost = data;
  196. if (sci_controller_isr(ihost))
  197. tasklet_schedule(&ihost->completion_tasklet);
  198. return IRQ_HANDLED;
  199. }
  200. static bool sci_controller_error_isr(struct isci_host *ihost)
  201. {
  202. u32 interrupt_status;
  203. interrupt_status =
  204. readl(&ihost->smu_registers->interrupt_status);
  205. interrupt_status &= (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND);
  206. if (interrupt_status != 0) {
  207. /*
  208. * There is an error interrupt pending so let it through and handle
  209. * in the callback */
  210. return true;
  211. }
  212. /*
  213. * There is a race in the hardware that could cause us not to be notified
  214. * of an interrupt completion if we do not take this step. We will mask
  215. * then unmask the error interrupts so if there was another interrupt
  216. * pending we will be notified.
  217. * Could we write the value of (SMU_ISR_QUEUE_ERROR | SMU_ISR_QUEUE_SUSPEND)? */
  218. writel(0xff, &ihost->smu_registers->interrupt_mask);
  219. writel(0, &ihost->smu_registers->interrupt_mask);
  220. return false;
  221. }
  222. static void sci_controller_task_completion(struct isci_host *ihost, u32 ent)
  223. {
  224. u32 index = SCU_GET_COMPLETION_INDEX(ent);
  225. struct isci_request *ireq = ihost->reqs[index];
  226. /* Make sure that we really want to process this IO request */
  227. if (test_bit(IREQ_ACTIVE, &ireq->flags) &&
  228. ireq->io_tag != SCI_CONTROLLER_INVALID_IO_TAG &&
  229. ISCI_TAG_SEQ(ireq->io_tag) == ihost->io_request_sequence[index])
  230. /* Yep this is a valid io request pass it along to the
  231. * io request handler
  232. */
  233. sci_io_request_tc_completion(ireq, ent);
  234. }
  235. static void sci_controller_sdma_completion(struct isci_host *ihost, u32 ent)
  236. {
  237. u32 index;
  238. struct isci_request *ireq;
  239. struct isci_remote_device *idev;
  240. index = SCU_GET_COMPLETION_INDEX(ent);
  241. switch (scu_get_command_request_type(ent)) {
  242. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC:
  243. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_TC:
  244. ireq = ihost->reqs[index];
  245. dev_warn(&ihost->pdev->dev, "%s: %x for io request %p\n",
  246. __func__, ent, ireq);
  247. /* @todo For a post TC operation we need to fail the IO
  248. * request
  249. */
  250. break;
  251. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_DUMP_RNC:
  252. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_OTHER_RNC:
  253. case SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_RNC:
  254. idev = ihost->device_table[index];
  255. dev_warn(&ihost->pdev->dev, "%s: %x for device %p\n",
  256. __func__, ent, idev);
  257. /* @todo For a port RNC operation we need to fail the
  258. * device
  259. */
  260. break;
  261. default:
  262. dev_warn(&ihost->pdev->dev, "%s: unknown completion type %x\n",
  263. __func__, ent);
  264. break;
  265. }
  266. }
  267. static void sci_controller_unsolicited_frame(struct isci_host *ihost, u32 ent)
  268. {
  269. u32 index;
  270. u32 frame_index;
  271. struct scu_unsolicited_frame_header *frame_header;
  272. struct isci_phy *iphy;
  273. struct isci_remote_device *idev;
  274. enum sci_status result = SCI_FAILURE;
  275. frame_index = SCU_GET_FRAME_INDEX(ent);
  276. frame_header = ihost->uf_control.buffers.array[frame_index].header;
  277. ihost->uf_control.buffers.array[frame_index].state = UNSOLICITED_FRAME_IN_USE;
  278. if (SCU_GET_FRAME_ERROR(ent)) {
  279. /*
  280. * / @todo If the IAF frame or SIGNATURE FIS frame has an error will
  281. * / this cause a problem? We expect the phy initialization will
  282. * / fail if there is an error in the frame. */
  283. sci_controller_release_frame(ihost, frame_index);
  284. return;
  285. }
  286. if (frame_header->is_address_frame) {
  287. index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
  288. iphy = &ihost->phys[index];
  289. result = sci_phy_frame_handler(iphy, frame_index);
  290. } else {
  291. index = SCU_GET_COMPLETION_INDEX(ent);
  292. if (index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  293. /*
  294. * This is a signature fis or a frame from a direct attached SATA
  295. * device that has not yet been created. In either case forwared
  296. * the frame to the PE and let it take care of the frame data. */
  297. index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
  298. iphy = &ihost->phys[index];
  299. result = sci_phy_frame_handler(iphy, frame_index);
  300. } else {
  301. if (index < ihost->remote_node_entries)
  302. idev = ihost->device_table[index];
  303. else
  304. idev = NULL;
  305. if (idev != NULL)
  306. result = sci_remote_device_frame_handler(idev, frame_index);
  307. else
  308. sci_controller_release_frame(ihost, frame_index);
  309. }
  310. }
  311. if (result != SCI_SUCCESS) {
  312. /*
  313. * / @todo Is there any reason to report some additional error message
  314. * / when we get this failure notifiction? */
  315. }
  316. }
  317. static void sci_controller_event_completion(struct isci_host *ihost, u32 ent)
  318. {
  319. struct isci_remote_device *idev;
  320. struct isci_request *ireq;
  321. struct isci_phy *iphy;
  322. u32 index;
  323. index = SCU_GET_COMPLETION_INDEX(ent);
  324. switch (scu_get_event_type(ent)) {
  325. case SCU_EVENT_TYPE_SMU_COMMAND_ERROR:
  326. /* / @todo The driver did something wrong and we need to fix the condtion. */
  327. dev_err(&ihost->pdev->dev,
  328. "%s: SCIC Controller 0x%p received SMU command error "
  329. "0x%x\n",
  330. __func__,
  331. ihost,
  332. ent);
  333. break;
  334. case SCU_EVENT_TYPE_SMU_PCQ_ERROR:
  335. case SCU_EVENT_TYPE_SMU_ERROR:
  336. case SCU_EVENT_TYPE_FATAL_MEMORY_ERROR:
  337. /*
  338. * / @todo This is a hardware failure and its likely that we want to
  339. * / reset the controller. */
  340. dev_err(&ihost->pdev->dev,
  341. "%s: SCIC Controller 0x%p received fatal controller "
  342. "event 0x%x\n",
  343. __func__,
  344. ihost,
  345. ent);
  346. break;
  347. case SCU_EVENT_TYPE_TRANSPORT_ERROR:
  348. ireq = ihost->reqs[index];
  349. sci_io_request_event_handler(ireq, ent);
  350. break;
  351. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  352. switch (scu_get_event_specifier(ent)) {
  353. case SCU_EVENT_SPECIFIC_SMP_RESPONSE_NO_PE:
  354. case SCU_EVENT_SPECIFIC_TASK_TIMEOUT:
  355. ireq = ihost->reqs[index];
  356. if (ireq != NULL)
  357. sci_io_request_event_handler(ireq, ent);
  358. else
  359. dev_warn(&ihost->pdev->dev,
  360. "%s: SCIC Controller 0x%p received "
  361. "event 0x%x for io request object "
  362. "that doesnt exist.\n",
  363. __func__,
  364. ihost,
  365. ent);
  366. break;
  367. case SCU_EVENT_SPECIFIC_IT_NEXUS_TIMEOUT:
  368. idev = ihost->device_table[index];
  369. if (idev != NULL)
  370. sci_remote_device_event_handler(idev, ent);
  371. else
  372. dev_warn(&ihost->pdev->dev,
  373. "%s: SCIC Controller 0x%p received "
  374. "event 0x%x for remote device object "
  375. "that doesnt exist.\n",
  376. __func__,
  377. ihost,
  378. ent);
  379. break;
  380. }
  381. break;
  382. case SCU_EVENT_TYPE_BROADCAST_CHANGE:
  383. /*
  384. * direct the broadcast change event to the phy first and then let
  385. * the phy redirect the broadcast change to the port object */
  386. case SCU_EVENT_TYPE_ERR_CNT_EVENT:
  387. /*
  388. * direct error counter event to the phy object since that is where
  389. * we get the event notification. This is a type 4 event. */
  390. case SCU_EVENT_TYPE_OSSP_EVENT:
  391. index = SCU_GET_PROTOCOL_ENGINE_INDEX(ent);
  392. iphy = &ihost->phys[index];
  393. sci_phy_event_handler(iphy, ent);
  394. break;
  395. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  396. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  397. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  398. if (index < ihost->remote_node_entries) {
  399. idev = ihost->device_table[index];
  400. if (idev != NULL)
  401. sci_remote_device_event_handler(idev, ent);
  402. } else
  403. dev_err(&ihost->pdev->dev,
  404. "%s: SCIC Controller 0x%p received event 0x%x "
  405. "for remote device object 0x%0x that doesnt "
  406. "exist.\n",
  407. __func__,
  408. ihost,
  409. ent,
  410. index);
  411. break;
  412. default:
  413. dev_warn(&ihost->pdev->dev,
  414. "%s: SCIC Controller received unknown event code %x\n",
  415. __func__,
  416. ent);
  417. break;
  418. }
  419. }
  420. static void sci_controller_process_completions(struct isci_host *ihost)
  421. {
  422. u32 completion_count = 0;
  423. u32 ent;
  424. u32 get_index;
  425. u32 get_cycle;
  426. u32 event_get;
  427. u32 event_cycle;
  428. dev_dbg(&ihost->pdev->dev,
  429. "%s: completion queue beginning get:0x%08x\n",
  430. __func__,
  431. ihost->completion_queue_get);
  432. /* Get the component parts of the completion queue */
  433. get_index = NORMALIZE_GET_POINTER(ihost->completion_queue_get);
  434. get_cycle = SMU_CQGR_CYCLE_BIT & ihost->completion_queue_get;
  435. event_get = NORMALIZE_EVENT_POINTER(ihost->completion_queue_get);
  436. event_cycle = SMU_CQGR_EVENT_CYCLE_BIT & ihost->completion_queue_get;
  437. while (
  438. NORMALIZE_GET_POINTER_CYCLE_BIT(get_cycle)
  439. == COMPLETION_QUEUE_CYCLE_BIT(ihost->completion_queue[get_index])
  440. ) {
  441. completion_count++;
  442. ent = ihost->completion_queue[get_index];
  443. /* increment the get pointer and check for rollover to toggle the cycle bit */
  444. get_cycle ^= ((get_index+1) & SCU_MAX_COMPLETION_QUEUE_ENTRIES) <<
  445. (SMU_COMPLETION_QUEUE_GET_CYCLE_BIT_SHIFT - SCU_MAX_COMPLETION_QUEUE_SHIFT);
  446. get_index = (get_index+1) & (SCU_MAX_COMPLETION_QUEUE_ENTRIES-1);
  447. dev_dbg(&ihost->pdev->dev,
  448. "%s: completion queue entry:0x%08x\n",
  449. __func__,
  450. ent);
  451. switch (SCU_GET_COMPLETION_TYPE(ent)) {
  452. case SCU_COMPLETION_TYPE_TASK:
  453. sci_controller_task_completion(ihost, ent);
  454. break;
  455. case SCU_COMPLETION_TYPE_SDMA:
  456. sci_controller_sdma_completion(ihost, ent);
  457. break;
  458. case SCU_COMPLETION_TYPE_UFI:
  459. sci_controller_unsolicited_frame(ihost, ent);
  460. break;
  461. case SCU_COMPLETION_TYPE_EVENT:
  462. sci_controller_event_completion(ihost, ent);
  463. break;
  464. case SCU_COMPLETION_TYPE_NOTIFY: {
  465. event_cycle ^= ((event_get+1) & SCU_MAX_EVENTS) <<
  466. (SMU_COMPLETION_QUEUE_GET_EVENT_CYCLE_BIT_SHIFT - SCU_MAX_EVENTS_SHIFT);
  467. event_get = (event_get+1) & (SCU_MAX_EVENTS-1);
  468. sci_controller_event_completion(ihost, ent);
  469. break;
  470. }
  471. default:
  472. dev_warn(&ihost->pdev->dev,
  473. "%s: SCIC Controller received unknown "
  474. "completion type %x\n",
  475. __func__,
  476. ent);
  477. break;
  478. }
  479. }
  480. /* Update the get register if we completed one or more entries */
  481. if (completion_count > 0) {
  482. ihost->completion_queue_get =
  483. SMU_CQGR_GEN_BIT(ENABLE) |
  484. SMU_CQGR_GEN_BIT(EVENT_ENABLE) |
  485. event_cycle |
  486. SMU_CQGR_GEN_VAL(EVENT_POINTER, event_get) |
  487. get_cycle |
  488. SMU_CQGR_GEN_VAL(POINTER, get_index);
  489. writel(ihost->completion_queue_get,
  490. &ihost->smu_registers->completion_queue_get);
  491. }
  492. dev_dbg(&ihost->pdev->dev,
  493. "%s: completion queue ending get:0x%08x\n",
  494. __func__,
  495. ihost->completion_queue_get);
  496. }
  497. static void sci_controller_error_handler(struct isci_host *ihost)
  498. {
  499. u32 interrupt_status;
  500. interrupt_status =
  501. readl(&ihost->smu_registers->interrupt_status);
  502. if ((interrupt_status & SMU_ISR_QUEUE_SUSPEND) &&
  503. sci_controller_completion_queue_has_entries(ihost)) {
  504. sci_controller_process_completions(ihost);
  505. writel(SMU_ISR_QUEUE_SUSPEND, &ihost->smu_registers->interrupt_status);
  506. } else {
  507. dev_err(&ihost->pdev->dev, "%s: status: %#x\n", __func__,
  508. interrupt_status);
  509. sci_change_state(&ihost->sm, SCIC_FAILED);
  510. return;
  511. }
  512. /* If we dont process any completions I am not sure that we want to do this.
  513. * We are in the middle of a hardware fault and should probably be reset.
  514. */
  515. writel(0, &ihost->smu_registers->interrupt_mask);
  516. }
  517. irqreturn_t isci_intx_isr(int vec, void *data)
  518. {
  519. irqreturn_t ret = IRQ_NONE;
  520. struct isci_host *ihost = data;
  521. if (sci_controller_isr(ihost)) {
  522. writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
  523. tasklet_schedule(&ihost->completion_tasklet);
  524. ret = IRQ_HANDLED;
  525. } else if (sci_controller_error_isr(ihost)) {
  526. spin_lock(&ihost->scic_lock);
  527. sci_controller_error_handler(ihost);
  528. spin_unlock(&ihost->scic_lock);
  529. ret = IRQ_HANDLED;
  530. }
  531. return ret;
  532. }
  533. irqreturn_t isci_error_isr(int vec, void *data)
  534. {
  535. struct isci_host *ihost = data;
  536. if (sci_controller_error_isr(ihost))
  537. sci_controller_error_handler(ihost);
  538. return IRQ_HANDLED;
  539. }
  540. /**
  541. * isci_host_start_complete() - This function is called by the core library,
  542. * through the ISCI Module, to indicate controller start status.
  543. * @isci_host: This parameter specifies the ISCI host object
  544. * @completion_status: This parameter specifies the completion status from the
  545. * core library.
  546. *
  547. */
  548. static void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
  549. {
  550. if (completion_status != SCI_SUCCESS)
  551. dev_info(&ihost->pdev->dev,
  552. "controller start timed out, continuing...\n");
  553. clear_bit(IHOST_START_PENDING, &ihost->flags);
  554. wake_up(&ihost->eventq);
  555. }
  556. int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
  557. {
  558. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  559. struct isci_host *ihost = ha->lldd_ha;
  560. if (test_bit(IHOST_START_PENDING, &ihost->flags))
  561. return 0;
  562. sas_drain_work(ha);
  563. return 1;
  564. }
  565. /**
  566. * sci_controller_get_suggested_start_timeout() - This method returns the
  567. * suggested sci_controller_start() timeout amount. The user is free to
  568. * use any timeout value, but this method provides the suggested minimum
  569. * start timeout value. The returned value is based upon empirical
  570. * information determined as a result of interoperability testing.
  571. * @controller: the handle to the controller object for which to return the
  572. * suggested start timeout.
  573. *
  574. * This method returns the number of milliseconds for the suggested start
  575. * operation timeout.
  576. */
  577. static u32 sci_controller_get_suggested_start_timeout(struct isci_host *ihost)
  578. {
  579. /* Validate the user supplied parameters. */
  580. if (!ihost)
  581. return 0;
  582. /*
  583. * The suggested minimum timeout value for a controller start operation:
  584. *
  585. * Signature FIS Timeout
  586. * + Phy Start Timeout
  587. * + Number of Phy Spin Up Intervals
  588. * ---------------------------------
  589. * Number of milliseconds for the controller start operation.
  590. *
  591. * NOTE: The number of phy spin up intervals will be equivalent
  592. * to the number of phys divided by the number phys allowed
  593. * per interval - 1 (once OEM parameters are supported).
  594. * Currently we assume only 1 phy per interval. */
  595. return SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  596. + SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT
  597. + ((SCI_MAX_PHYS - 1) * SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  598. }
  599. static void sci_controller_enable_interrupts(struct isci_host *ihost)
  600. {
  601. set_bit(IHOST_IRQ_ENABLED, &ihost->flags);
  602. writel(0, &ihost->smu_registers->interrupt_mask);
  603. }
  604. void sci_controller_disable_interrupts(struct isci_host *ihost)
  605. {
  606. clear_bit(IHOST_IRQ_ENABLED, &ihost->flags);
  607. writel(0xffffffff, &ihost->smu_registers->interrupt_mask);
  608. readl(&ihost->smu_registers->interrupt_mask); /* flush */
  609. }
  610. static void sci_controller_enable_port_task_scheduler(struct isci_host *ihost)
  611. {
  612. u32 port_task_scheduler_value;
  613. port_task_scheduler_value =
  614. readl(&ihost->scu_registers->peg0.ptsg.control);
  615. port_task_scheduler_value |=
  616. (SCU_PTSGCR_GEN_BIT(ETM_ENABLE) |
  617. SCU_PTSGCR_GEN_BIT(PTSG_ENABLE));
  618. writel(port_task_scheduler_value,
  619. &ihost->scu_registers->peg0.ptsg.control);
  620. }
  621. static void sci_controller_assign_task_entries(struct isci_host *ihost)
  622. {
  623. u32 task_assignment;
  624. /*
  625. * Assign all the TCs to function 0
  626. * TODO: Do we actually need to read this register to write it back?
  627. */
  628. task_assignment =
  629. readl(&ihost->smu_registers->task_context_assignment[0]);
  630. task_assignment |= (SMU_TCA_GEN_VAL(STARTING, 0)) |
  631. (SMU_TCA_GEN_VAL(ENDING, ihost->task_context_entries - 1)) |
  632. (SMU_TCA_GEN_BIT(RANGE_CHECK_ENABLE));
  633. writel(task_assignment,
  634. &ihost->smu_registers->task_context_assignment[0]);
  635. }
  636. static void sci_controller_initialize_completion_queue(struct isci_host *ihost)
  637. {
  638. u32 index;
  639. u32 completion_queue_control_value;
  640. u32 completion_queue_get_value;
  641. u32 completion_queue_put_value;
  642. ihost->completion_queue_get = 0;
  643. completion_queue_control_value =
  644. (SMU_CQC_QUEUE_LIMIT_SET(SCU_MAX_COMPLETION_QUEUE_ENTRIES - 1) |
  645. SMU_CQC_EVENT_LIMIT_SET(SCU_MAX_EVENTS - 1));
  646. writel(completion_queue_control_value,
  647. &ihost->smu_registers->completion_queue_control);
  648. /* Set the completion queue get pointer and enable the queue */
  649. completion_queue_get_value = (
  650. (SMU_CQGR_GEN_VAL(POINTER, 0))
  651. | (SMU_CQGR_GEN_VAL(EVENT_POINTER, 0))
  652. | (SMU_CQGR_GEN_BIT(ENABLE))
  653. | (SMU_CQGR_GEN_BIT(EVENT_ENABLE))
  654. );
  655. writel(completion_queue_get_value,
  656. &ihost->smu_registers->completion_queue_get);
  657. /* Set the completion queue put pointer */
  658. completion_queue_put_value = (
  659. (SMU_CQPR_GEN_VAL(POINTER, 0))
  660. | (SMU_CQPR_GEN_VAL(EVENT_POINTER, 0))
  661. );
  662. writel(completion_queue_put_value,
  663. &ihost->smu_registers->completion_queue_put);
  664. /* Initialize the cycle bit of the completion queue entries */
  665. for (index = 0; index < SCU_MAX_COMPLETION_QUEUE_ENTRIES; index++) {
  666. /*
  667. * If get.cycle_bit != completion_queue.cycle_bit
  668. * its not a valid completion queue entry
  669. * so at system start all entries are invalid */
  670. ihost->completion_queue[index] = 0x80000000;
  671. }
  672. }
  673. static void sci_controller_initialize_unsolicited_frame_queue(struct isci_host *ihost)
  674. {
  675. u32 frame_queue_control_value;
  676. u32 frame_queue_get_value;
  677. u32 frame_queue_put_value;
  678. /* Write the queue size */
  679. frame_queue_control_value =
  680. SCU_UFQC_GEN_VAL(QUEUE_SIZE, SCU_MAX_UNSOLICITED_FRAMES);
  681. writel(frame_queue_control_value,
  682. &ihost->scu_registers->sdma.unsolicited_frame_queue_control);
  683. /* Setup the get pointer for the unsolicited frame queue */
  684. frame_queue_get_value = (
  685. SCU_UFQGP_GEN_VAL(POINTER, 0)
  686. | SCU_UFQGP_GEN_BIT(ENABLE_BIT)
  687. );
  688. writel(frame_queue_get_value,
  689. &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  690. /* Setup the put pointer for the unsolicited frame queue */
  691. frame_queue_put_value = SCU_UFQPP_GEN_VAL(POINTER, 0);
  692. writel(frame_queue_put_value,
  693. &ihost->scu_registers->sdma.unsolicited_frame_put_pointer);
  694. }
  695. void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status)
  696. {
  697. if (ihost->sm.current_state_id == SCIC_STARTING) {
  698. /*
  699. * We move into the ready state, because some of the phys/ports
  700. * may be up and operational.
  701. */
  702. sci_change_state(&ihost->sm, SCIC_READY);
  703. isci_host_start_complete(ihost, status);
  704. }
  705. }
  706. static bool is_phy_starting(struct isci_phy *iphy)
  707. {
  708. enum sci_phy_states state;
  709. state = iphy->sm.current_state_id;
  710. switch (state) {
  711. case SCI_PHY_STARTING:
  712. case SCI_PHY_SUB_INITIAL:
  713. case SCI_PHY_SUB_AWAIT_SAS_SPEED_EN:
  714. case SCI_PHY_SUB_AWAIT_IAF_UF:
  715. case SCI_PHY_SUB_AWAIT_SAS_POWER:
  716. case SCI_PHY_SUB_AWAIT_SATA_POWER:
  717. case SCI_PHY_SUB_AWAIT_SATA_PHY_EN:
  718. case SCI_PHY_SUB_AWAIT_SATA_SPEED_EN:
  719. case SCI_PHY_SUB_AWAIT_OSSP_EN:
  720. case SCI_PHY_SUB_AWAIT_SIG_FIS_UF:
  721. case SCI_PHY_SUB_FINAL:
  722. return true;
  723. default:
  724. return false;
  725. }
  726. }
  727. bool is_controller_start_complete(struct isci_host *ihost)
  728. {
  729. int i;
  730. for (i = 0; i < SCI_MAX_PHYS; i++) {
  731. struct isci_phy *iphy = &ihost->phys[i];
  732. u32 state = iphy->sm.current_state_id;
  733. /* in apc mode we need to check every phy, in
  734. * mpc mode we only need to check phys that have
  735. * been configured into a port
  736. */
  737. if (is_port_config_apc(ihost))
  738. /* pass */;
  739. else if (!phy_get_non_dummy_port(iphy))
  740. continue;
  741. /* The controller start operation is complete iff:
  742. * - all links have been given an opportunity to start
  743. * - have no indication of a connected device
  744. * - have an indication of a connected device and it has
  745. * finished the link training process.
  746. */
  747. if ((iphy->is_in_link_training == false && state == SCI_PHY_INITIAL) ||
  748. (iphy->is_in_link_training == false && state == SCI_PHY_STOPPED) ||
  749. (iphy->is_in_link_training == true && is_phy_starting(iphy)) ||
  750. (ihost->port_agent.phy_ready_mask != ihost->port_agent.phy_configured_mask))
  751. return false;
  752. }
  753. return true;
  754. }
  755. /**
  756. * sci_controller_start_next_phy - start phy
  757. * @scic: controller
  758. *
  759. * If all the phys have been started, then attempt to transition the
  760. * controller to the READY state and inform the user
  761. * (sci_cb_controller_start_complete()).
  762. */
  763. static enum sci_status sci_controller_start_next_phy(struct isci_host *ihost)
  764. {
  765. struct sci_oem_params *oem = &ihost->oem_parameters;
  766. struct isci_phy *iphy;
  767. enum sci_status status;
  768. status = SCI_SUCCESS;
  769. if (ihost->phy_startup_timer_pending)
  770. return status;
  771. if (ihost->next_phy_to_start >= SCI_MAX_PHYS) {
  772. if (is_controller_start_complete(ihost)) {
  773. sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
  774. sci_del_timer(&ihost->phy_timer);
  775. ihost->phy_startup_timer_pending = false;
  776. }
  777. } else {
  778. iphy = &ihost->phys[ihost->next_phy_to_start];
  779. if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  780. if (phy_get_non_dummy_port(iphy) == NULL) {
  781. ihost->next_phy_to_start++;
  782. /* Caution recursion ahead be forwarned
  783. *
  784. * The PHY was never added to a PORT in MPC mode
  785. * so start the next phy in sequence This phy
  786. * will never go link up and will not draw power
  787. * the OEM parameters either configured the phy
  788. * incorrectly for the PORT or it was never
  789. * assigned to a PORT
  790. */
  791. return sci_controller_start_next_phy(ihost);
  792. }
  793. }
  794. status = sci_phy_start(iphy);
  795. if (status == SCI_SUCCESS) {
  796. sci_mod_timer(&ihost->phy_timer,
  797. SCIC_SDS_CONTROLLER_PHY_START_TIMEOUT);
  798. ihost->phy_startup_timer_pending = true;
  799. } else {
  800. dev_warn(&ihost->pdev->dev,
  801. "%s: Controller stop operation failed "
  802. "to stop phy %d because of status "
  803. "%d.\n",
  804. __func__,
  805. ihost->phys[ihost->next_phy_to_start].phy_index,
  806. status);
  807. }
  808. ihost->next_phy_to_start++;
  809. }
  810. return status;
  811. }
  812. static void phy_startup_timeout(unsigned long data)
  813. {
  814. struct sci_timer *tmr = (struct sci_timer *)data;
  815. struct isci_host *ihost = container_of(tmr, typeof(*ihost), phy_timer);
  816. unsigned long flags;
  817. enum sci_status status;
  818. spin_lock_irqsave(&ihost->scic_lock, flags);
  819. if (tmr->cancel)
  820. goto done;
  821. ihost->phy_startup_timer_pending = false;
  822. do {
  823. status = sci_controller_start_next_phy(ihost);
  824. } while (status != SCI_SUCCESS);
  825. done:
  826. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  827. }
  828. static u16 isci_tci_active(struct isci_host *ihost)
  829. {
  830. return CIRC_CNT(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
  831. }
  832. static enum sci_status sci_controller_start(struct isci_host *ihost,
  833. u32 timeout)
  834. {
  835. enum sci_status result;
  836. u16 index;
  837. if (ihost->sm.current_state_id != SCIC_INITIALIZED) {
  838. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  839. __func__, ihost->sm.current_state_id);
  840. return SCI_FAILURE_INVALID_STATE;
  841. }
  842. /* Build the TCi free pool */
  843. BUILD_BUG_ON(SCI_MAX_IO_REQUESTS > 1 << sizeof(ihost->tci_pool[0]) * 8);
  844. ihost->tci_head = 0;
  845. ihost->tci_tail = 0;
  846. for (index = 0; index < ihost->task_context_entries; index++)
  847. isci_tci_free(ihost, index);
  848. /* Build the RNi free pool */
  849. sci_remote_node_table_initialize(&ihost->available_remote_nodes,
  850. ihost->remote_node_entries);
  851. /*
  852. * Before anything else lets make sure we will not be
  853. * interrupted by the hardware.
  854. */
  855. sci_controller_disable_interrupts(ihost);
  856. /* Enable the port task scheduler */
  857. sci_controller_enable_port_task_scheduler(ihost);
  858. /* Assign all the task entries to ihost physical function */
  859. sci_controller_assign_task_entries(ihost);
  860. /* Now initialize the completion queue */
  861. sci_controller_initialize_completion_queue(ihost);
  862. /* Initialize the unsolicited frame queue for use */
  863. sci_controller_initialize_unsolicited_frame_queue(ihost);
  864. /* Start all of the ports on this controller */
  865. for (index = 0; index < ihost->logical_port_entries; index++) {
  866. struct isci_port *iport = &ihost->ports[index];
  867. result = sci_port_start(iport);
  868. if (result)
  869. return result;
  870. }
  871. sci_controller_start_next_phy(ihost);
  872. sci_mod_timer(&ihost->timer, timeout);
  873. sci_change_state(&ihost->sm, SCIC_STARTING);
  874. return SCI_SUCCESS;
  875. }
  876. void isci_host_start(struct Scsi_Host *shost)
  877. {
  878. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  879. unsigned long tmo = sci_controller_get_suggested_start_timeout(ihost);
  880. set_bit(IHOST_START_PENDING, &ihost->flags);
  881. spin_lock_irq(&ihost->scic_lock);
  882. sci_controller_start(ihost, tmo);
  883. sci_controller_enable_interrupts(ihost);
  884. spin_unlock_irq(&ihost->scic_lock);
  885. }
  886. static void isci_host_stop_complete(struct isci_host *ihost)
  887. {
  888. sci_controller_disable_interrupts(ihost);
  889. clear_bit(IHOST_STOP_PENDING, &ihost->flags);
  890. wake_up(&ihost->eventq);
  891. }
  892. static void sci_controller_completion_handler(struct isci_host *ihost)
  893. {
  894. /* Empty out the completion queue */
  895. if (sci_controller_completion_queue_has_entries(ihost))
  896. sci_controller_process_completions(ihost);
  897. /* Clear the interrupt and enable all interrupts again */
  898. writel(SMU_ISR_COMPLETION, &ihost->smu_registers->interrupt_status);
  899. /* Could we write the value of SMU_ISR_COMPLETION? */
  900. writel(0xFF000000, &ihost->smu_registers->interrupt_mask);
  901. writel(0, &ihost->smu_registers->interrupt_mask);
  902. }
  903. void ireq_done(struct isci_host *ihost, struct isci_request *ireq, struct sas_task *task)
  904. {
  905. if (!test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags) &&
  906. !(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  907. if (test_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags)) {
  908. /* Normal notification (task_done) */
  909. dev_dbg(&ihost->pdev->dev,
  910. "%s: Normal - ireq/task = %p/%p\n",
  911. __func__, ireq, task);
  912. task->lldd_task = NULL;
  913. task->task_done(task);
  914. } else {
  915. dev_dbg(&ihost->pdev->dev,
  916. "%s: Error - ireq/task = %p/%p\n",
  917. __func__, ireq, task);
  918. if (sas_protocol_ata(task->task_proto))
  919. task->lldd_task = NULL;
  920. sas_task_abort(task);
  921. }
  922. } else
  923. task->lldd_task = NULL;
  924. if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
  925. wake_up_all(&ihost->eventq);
  926. if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
  927. isci_free_tag(ihost, ireq->io_tag);
  928. }
  929. /**
  930. * isci_host_completion_routine() - This function is the delayed service
  931. * routine that calls the sci core library's completion handler. It's
  932. * scheduled as a tasklet from the interrupt service routine when interrupts
  933. * in use, or set as the timeout function in polled mode.
  934. * @data: This parameter specifies the ISCI host object
  935. *
  936. */
  937. void isci_host_completion_routine(unsigned long data)
  938. {
  939. struct isci_host *ihost = (struct isci_host *)data;
  940. u16 active;
  941. spin_lock_irq(&ihost->scic_lock);
  942. sci_controller_completion_handler(ihost);
  943. spin_unlock_irq(&ihost->scic_lock);
  944. /*
  945. * we subtract SCI_MAX_PORTS to account for the number of dummy TCs
  946. * issued for hardware issue workaround
  947. */
  948. active = isci_tci_active(ihost) - SCI_MAX_PORTS;
  949. /*
  950. * the coalesence timeout doubles at each encoding step, so
  951. * update it based on the ilog2 value of the outstanding requests
  952. */
  953. writel(SMU_ICC_GEN_VAL(NUMBER, active) |
  954. SMU_ICC_GEN_VAL(TIMER, ISCI_COALESCE_BASE + ilog2(active)),
  955. &ihost->smu_registers->interrupt_coalesce_control);
  956. }
  957. /**
  958. * sci_controller_stop() - This method will stop an individual controller
  959. * object.This method will invoke the associated user callback upon
  960. * completion. The completion callback is called when the following
  961. * conditions are met: -# the method return status is SCI_SUCCESS. -# the
  962. * controller has been quiesced. This method will ensure that all IO
  963. * requests are quiesced, phys are stopped, and all additional operation by
  964. * the hardware is halted.
  965. * @controller: the handle to the controller object to stop.
  966. * @timeout: This parameter specifies the number of milliseconds in which the
  967. * stop operation should complete.
  968. *
  969. * The controller must be in the STARTED or STOPPED state. Indicate if the
  970. * controller stop method succeeded or failed in some way. SCI_SUCCESS if the
  971. * stop operation successfully began. SCI_WARNING_ALREADY_IN_STATE if the
  972. * controller is already in the STOPPED state. SCI_FAILURE_INVALID_STATE if the
  973. * controller is not either in the STARTED or STOPPED states.
  974. */
  975. static enum sci_status sci_controller_stop(struct isci_host *ihost, u32 timeout)
  976. {
  977. if (ihost->sm.current_state_id != SCIC_READY) {
  978. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  979. __func__, ihost->sm.current_state_id);
  980. return SCI_FAILURE_INVALID_STATE;
  981. }
  982. sci_mod_timer(&ihost->timer, timeout);
  983. sci_change_state(&ihost->sm, SCIC_STOPPING);
  984. return SCI_SUCCESS;
  985. }
  986. /**
  987. * sci_controller_reset() - This method will reset the supplied core
  988. * controller regardless of the state of said controller. This operation is
  989. * considered destructive. In other words, all current operations are wiped
  990. * out. No IO completions for outstanding devices occur. Outstanding IO
  991. * requests are not aborted or completed at the actual remote device.
  992. * @controller: the handle to the controller object to reset.
  993. *
  994. * Indicate if the controller reset method succeeded or failed in some way.
  995. * SCI_SUCCESS if the reset operation successfully started. SCI_FATAL_ERROR if
  996. * the controller reset operation is unable to complete.
  997. */
  998. static enum sci_status sci_controller_reset(struct isci_host *ihost)
  999. {
  1000. switch (ihost->sm.current_state_id) {
  1001. case SCIC_RESET:
  1002. case SCIC_READY:
  1003. case SCIC_STOPPING:
  1004. case SCIC_FAILED:
  1005. /*
  1006. * The reset operation is not a graceful cleanup, just
  1007. * perform the state transition.
  1008. */
  1009. sci_change_state(&ihost->sm, SCIC_RESETTING);
  1010. return SCI_SUCCESS;
  1011. default:
  1012. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  1013. __func__, ihost->sm.current_state_id);
  1014. return SCI_FAILURE_INVALID_STATE;
  1015. }
  1016. }
  1017. static enum sci_status sci_controller_stop_phys(struct isci_host *ihost)
  1018. {
  1019. u32 index;
  1020. enum sci_status status;
  1021. enum sci_status phy_status;
  1022. status = SCI_SUCCESS;
  1023. for (index = 0; index < SCI_MAX_PHYS; index++) {
  1024. phy_status = sci_phy_stop(&ihost->phys[index]);
  1025. if (phy_status != SCI_SUCCESS &&
  1026. phy_status != SCI_FAILURE_INVALID_STATE) {
  1027. status = SCI_FAILURE;
  1028. dev_warn(&ihost->pdev->dev,
  1029. "%s: Controller stop operation failed to stop "
  1030. "phy %d because of status %d.\n",
  1031. __func__,
  1032. ihost->phys[index].phy_index, phy_status);
  1033. }
  1034. }
  1035. return status;
  1036. }
  1037. /**
  1038. * isci_host_deinit - shutdown frame reception and dma
  1039. * @ihost: host to take down
  1040. *
  1041. * This is called in either the driver shutdown or the suspend path. In
  1042. * the shutdown case libsas went through port teardown and normal device
  1043. * removal (i.e. physical links stayed up to service scsi_device removal
  1044. * commands). In the suspend case we disable the hardware without
  1045. * notifying libsas of the link down events since we want libsas to
  1046. * remember the domain across the suspend/resume cycle
  1047. */
  1048. void isci_host_deinit(struct isci_host *ihost)
  1049. {
  1050. int i;
  1051. /* disable output data selects */
  1052. for (i = 0; i < isci_gpio_count(ihost); i++)
  1053. writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
  1054. set_bit(IHOST_STOP_PENDING, &ihost->flags);
  1055. spin_lock_irq(&ihost->scic_lock);
  1056. sci_controller_stop(ihost, SCIC_CONTROLLER_STOP_TIMEOUT);
  1057. spin_unlock_irq(&ihost->scic_lock);
  1058. wait_for_stop(ihost);
  1059. /* phy stop is after controller stop to allow port and device to
  1060. * go idle before shutting down the phys, but the expectation is
  1061. * that i/o has been shut off well before we reach this
  1062. * function.
  1063. */
  1064. sci_controller_stop_phys(ihost);
  1065. /* disable sgpio: where the above wait should give time for the
  1066. * enclosure to sample the gpios going inactive
  1067. */
  1068. writel(0, &ihost->scu_registers->peg0.sgpio.interface_control);
  1069. spin_lock_irq(&ihost->scic_lock);
  1070. sci_controller_reset(ihost);
  1071. spin_unlock_irq(&ihost->scic_lock);
  1072. /* Cancel any/all outstanding port timers */
  1073. for (i = 0; i < ihost->logical_port_entries; i++) {
  1074. struct isci_port *iport = &ihost->ports[i];
  1075. del_timer_sync(&iport->timer.timer);
  1076. }
  1077. /* Cancel any/all outstanding phy timers */
  1078. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1079. struct isci_phy *iphy = &ihost->phys[i];
  1080. del_timer_sync(&iphy->sata_timer.timer);
  1081. }
  1082. del_timer_sync(&ihost->port_agent.timer.timer);
  1083. del_timer_sync(&ihost->power_control.timer.timer);
  1084. del_timer_sync(&ihost->timer.timer);
  1085. del_timer_sync(&ihost->phy_timer.timer);
  1086. }
  1087. static void __iomem *scu_base(struct isci_host *isci_host)
  1088. {
  1089. struct pci_dev *pdev = isci_host->pdev;
  1090. int id = isci_host->id;
  1091. return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
  1092. }
  1093. static void __iomem *smu_base(struct isci_host *isci_host)
  1094. {
  1095. struct pci_dev *pdev = isci_host->pdev;
  1096. int id = isci_host->id;
  1097. return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
  1098. }
  1099. static void sci_controller_initial_state_enter(struct sci_base_state_machine *sm)
  1100. {
  1101. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1102. sci_change_state(&ihost->sm, SCIC_RESET);
  1103. }
  1104. static inline void sci_controller_starting_state_exit(struct sci_base_state_machine *sm)
  1105. {
  1106. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1107. sci_del_timer(&ihost->timer);
  1108. }
  1109. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS 853
  1110. #define INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS 1280
  1111. #define INTERRUPT_COALESCE_TIMEOUT_MAX_US 2700000
  1112. #define INTERRUPT_COALESCE_NUMBER_MAX 256
  1113. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN 7
  1114. #define INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX 28
  1115. /**
  1116. * sci_controller_set_interrupt_coalescence() - This method allows the user to
  1117. * configure the interrupt coalescence.
  1118. * @controller: This parameter represents the handle to the controller object
  1119. * for which its interrupt coalesce register is overridden.
  1120. * @coalesce_number: Used to control the number of entries in the Completion
  1121. * Queue before an interrupt is generated. If the number of entries exceed
  1122. * this number, an interrupt will be generated. The valid range of the input
  1123. * is [0, 256]. A setting of 0 results in coalescing being disabled.
  1124. * @coalesce_timeout: Timeout value in microseconds. The valid range of the
  1125. * input is [0, 2700000] . A setting of 0 is allowed and results in no
  1126. * interrupt coalescing timeout.
  1127. *
  1128. * Indicate if the user successfully set the interrupt coalesce parameters.
  1129. * SCI_SUCCESS The user successfully updated the interrutp coalescence.
  1130. * SCI_FAILURE_INVALID_PARAMETER_VALUE The user input value is out of range.
  1131. */
  1132. static enum sci_status
  1133. sci_controller_set_interrupt_coalescence(struct isci_host *ihost,
  1134. u32 coalesce_number,
  1135. u32 coalesce_timeout)
  1136. {
  1137. u8 timeout_encode = 0;
  1138. u32 min = 0;
  1139. u32 max = 0;
  1140. /* Check if the input parameters fall in the range. */
  1141. if (coalesce_number > INTERRUPT_COALESCE_NUMBER_MAX)
  1142. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1143. /*
  1144. * Defined encoding for interrupt coalescing timeout:
  1145. * Value Min Max Units
  1146. * ----- --- --- -----
  1147. * 0 - - Disabled
  1148. * 1 13.3 20.0 ns
  1149. * 2 26.7 40.0
  1150. * 3 53.3 80.0
  1151. * 4 106.7 160.0
  1152. * 5 213.3 320.0
  1153. * 6 426.7 640.0
  1154. * 7 853.3 1280.0
  1155. * 8 1.7 2.6 us
  1156. * 9 3.4 5.1
  1157. * 10 6.8 10.2
  1158. * 11 13.7 20.5
  1159. * 12 27.3 41.0
  1160. * 13 54.6 81.9
  1161. * 14 109.2 163.8
  1162. * 15 218.5 327.7
  1163. * 16 436.9 655.4
  1164. * 17 873.8 1310.7
  1165. * 18 1.7 2.6 ms
  1166. * 19 3.5 5.2
  1167. * 20 7.0 10.5
  1168. * 21 14.0 21.0
  1169. * 22 28.0 41.9
  1170. * 23 55.9 83.9
  1171. * 24 111.8 167.8
  1172. * 25 223.7 335.5
  1173. * 26 447.4 671.1
  1174. * 27 894.8 1342.2
  1175. * 28 1.8 2.7 s
  1176. * Others Undefined */
  1177. /*
  1178. * Use the table above to decide the encode of interrupt coalescing timeout
  1179. * value for register writing. */
  1180. if (coalesce_timeout == 0)
  1181. timeout_encode = 0;
  1182. else{
  1183. /* make the timeout value in unit of (10 ns). */
  1184. coalesce_timeout = coalesce_timeout * 100;
  1185. min = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_LOWER_BOUND_NS / 10;
  1186. max = INTERRUPT_COALESCE_TIMEOUT_BASE_RANGE_UPPER_BOUND_NS / 10;
  1187. /* get the encode of timeout for register writing. */
  1188. for (timeout_encode = INTERRUPT_COALESCE_TIMEOUT_ENCODE_MIN;
  1189. timeout_encode <= INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX;
  1190. timeout_encode++) {
  1191. if (min <= coalesce_timeout && max > coalesce_timeout)
  1192. break;
  1193. else if (coalesce_timeout >= max && coalesce_timeout < min * 2
  1194. && coalesce_timeout <= INTERRUPT_COALESCE_TIMEOUT_MAX_US * 100) {
  1195. if ((coalesce_timeout - max) < (2 * min - coalesce_timeout))
  1196. break;
  1197. else{
  1198. timeout_encode++;
  1199. break;
  1200. }
  1201. } else {
  1202. max = max * 2;
  1203. min = min * 2;
  1204. }
  1205. }
  1206. if (timeout_encode == INTERRUPT_COALESCE_TIMEOUT_ENCODE_MAX + 1)
  1207. /* the value is out of range. */
  1208. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  1209. }
  1210. writel(SMU_ICC_GEN_VAL(NUMBER, coalesce_number) |
  1211. SMU_ICC_GEN_VAL(TIMER, timeout_encode),
  1212. &ihost->smu_registers->interrupt_coalesce_control);
  1213. ihost->interrupt_coalesce_number = (u16)coalesce_number;
  1214. ihost->interrupt_coalesce_timeout = coalesce_timeout / 100;
  1215. return SCI_SUCCESS;
  1216. }
  1217. static void sci_controller_ready_state_enter(struct sci_base_state_machine *sm)
  1218. {
  1219. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1220. u32 val;
  1221. /* enable clock gating for power control of the scu unit */
  1222. val = readl(&ihost->smu_registers->clock_gating_control);
  1223. val &= ~(SMU_CGUCR_GEN_BIT(REGCLK_ENABLE) |
  1224. SMU_CGUCR_GEN_BIT(TXCLK_ENABLE) |
  1225. SMU_CGUCR_GEN_BIT(XCLK_ENABLE));
  1226. val |= SMU_CGUCR_GEN_BIT(IDLE_ENABLE);
  1227. writel(val, &ihost->smu_registers->clock_gating_control);
  1228. /* set the default interrupt coalescence number and timeout value. */
  1229. sci_controller_set_interrupt_coalescence(ihost, 0, 0);
  1230. }
  1231. static void sci_controller_ready_state_exit(struct sci_base_state_machine *sm)
  1232. {
  1233. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1234. /* disable interrupt coalescence. */
  1235. sci_controller_set_interrupt_coalescence(ihost, 0, 0);
  1236. }
  1237. static enum sci_status sci_controller_stop_ports(struct isci_host *ihost)
  1238. {
  1239. u32 index;
  1240. enum sci_status port_status;
  1241. enum sci_status status = SCI_SUCCESS;
  1242. for (index = 0; index < ihost->logical_port_entries; index++) {
  1243. struct isci_port *iport = &ihost->ports[index];
  1244. port_status = sci_port_stop(iport);
  1245. if ((port_status != SCI_SUCCESS) &&
  1246. (port_status != SCI_FAILURE_INVALID_STATE)) {
  1247. status = SCI_FAILURE;
  1248. dev_warn(&ihost->pdev->dev,
  1249. "%s: Controller stop operation failed to "
  1250. "stop port %d because of status %d.\n",
  1251. __func__,
  1252. iport->logical_port_index,
  1253. port_status);
  1254. }
  1255. }
  1256. return status;
  1257. }
  1258. static enum sci_status sci_controller_stop_devices(struct isci_host *ihost)
  1259. {
  1260. u32 index;
  1261. enum sci_status status;
  1262. enum sci_status device_status;
  1263. status = SCI_SUCCESS;
  1264. for (index = 0; index < ihost->remote_node_entries; index++) {
  1265. if (ihost->device_table[index] != NULL) {
  1266. /* / @todo What timeout value do we want to provide to this request? */
  1267. device_status = sci_remote_device_stop(ihost->device_table[index], 0);
  1268. if ((device_status != SCI_SUCCESS) &&
  1269. (device_status != SCI_FAILURE_INVALID_STATE)) {
  1270. dev_warn(&ihost->pdev->dev,
  1271. "%s: Controller stop operation failed "
  1272. "to stop device 0x%p because of "
  1273. "status %d.\n",
  1274. __func__,
  1275. ihost->device_table[index], device_status);
  1276. }
  1277. }
  1278. }
  1279. return status;
  1280. }
  1281. static void sci_controller_stopping_state_enter(struct sci_base_state_machine *sm)
  1282. {
  1283. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1284. sci_controller_stop_devices(ihost);
  1285. sci_controller_stop_ports(ihost);
  1286. if (!sci_controller_has_remote_devices_stopping(ihost))
  1287. isci_host_stop_complete(ihost);
  1288. }
  1289. static void sci_controller_stopping_state_exit(struct sci_base_state_machine *sm)
  1290. {
  1291. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1292. sci_del_timer(&ihost->timer);
  1293. }
  1294. static void sci_controller_reset_hardware(struct isci_host *ihost)
  1295. {
  1296. /* Disable interrupts so we dont take any spurious interrupts */
  1297. sci_controller_disable_interrupts(ihost);
  1298. /* Reset the SCU */
  1299. writel(0xFFFFFFFF, &ihost->smu_registers->soft_reset_control);
  1300. /* Delay for 1ms to before clearing the CQP and UFQPR. */
  1301. udelay(1000);
  1302. /* The write to the CQGR clears the CQP */
  1303. writel(0x00000000, &ihost->smu_registers->completion_queue_get);
  1304. /* The write to the UFQGP clears the UFQPR */
  1305. writel(0, &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  1306. /* clear all interrupts */
  1307. writel(~SMU_INTERRUPT_STATUS_RESERVED_MASK, &ihost->smu_registers->interrupt_status);
  1308. }
  1309. static void sci_controller_resetting_state_enter(struct sci_base_state_machine *sm)
  1310. {
  1311. struct isci_host *ihost = container_of(sm, typeof(*ihost), sm);
  1312. sci_controller_reset_hardware(ihost);
  1313. sci_change_state(&ihost->sm, SCIC_RESET);
  1314. }
  1315. static const struct sci_base_state sci_controller_state_table[] = {
  1316. [SCIC_INITIAL] = {
  1317. .enter_state = sci_controller_initial_state_enter,
  1318. },
  1319. [SCIC_RESET] = {},
  1320. [SCIC_INITIALIZING] = {},
  1321. [SCIC_INITIALIZED] = {},
  1322. [SCIC_STARTING] = {
  1323. .exit_state = sci_controller_starting_state_exit,
  1324. },
  1325. [SCIC_READY] = {
  1326. .enter_state = sci_controller_ready_state_enter,
  1327. .exit_state = sci_controller_ready_state_exit,
  1328. },
  1329. [SCIC_RESETTING] = {
  1330. .enter_state = sci_controller_resetting_state_enter,
  1331. },
  1332. [SCIC_STOPPING] = {
  1333. .enter_state = sci_controller_stopping_state_enter,
  1334. .exit_state = sci_controller_stopping_state_exit,
  1335. },
  1336. [SCIC_FAILED] = {}
  1337. };
  1338. static void controller_timeout(unsigned long data)
  1339. {
  1340. struct sci_timer *tmr = (struct sci_timer *)data;
  1341. struct isci_host *ihost = container_of(tmr, typeof(*ihost), timer);
  1342. struct sci_base_state_machine *sm = &ihost->sm;
  1343. unsigned long flags;
  1344. spin_lock_irqsave(&ihost->scic_lock, flags);
  1345. if (tmr->cancel)
  1346. goto done;
  1347. if (sm->current_state_id == SCIC_STARTING)
  1348. sci_controller_transition_to_ready(ihost, SCI_FAILURE_TIMEOUT);
  1349. else if (sm->current_state_id == SCIC_STOPPING) {
  1350. sci_change_state(sm, SCIC_FAILED);
  1351. isci_host_stop_complete(ihost);
  1352. } else /* / @todo Now what do we want to do in this case? */
  1353. dev_err(&ihost->pdev->dev,
  1354. "%s: Controller timer fired when controller was not "
  1355. "in a state being timed.\n",
  1356. __func__);
  1357. done:
  1358. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1359. }
  1360. static enum sci_status sci_controller_construct(struct isci_host *ihost,
  1361. void __iomem *scu_base,
  1362. void __iomem *smu_base)
  1363. {
  1364. u8 i;
  1365. sci_init_sm(&ihost->sm, sci_controller_state_table, SCIC_INITIAL);
  1366. ihost->scu_registers = scu_base;
  1367. ihost->smu_registers = smu_base;
  1368. sci_port_configuration_agent_construct(&ihost->port_agent);
  1369. /* Construct the ports for this controller */
  1370. for (i = 0; i < SCI_MAX_PORTS; i++)
  1371. sci_port_construct(&ihost->ports[i], i, ihost);
  1372. sci_port_construct(&ihost->ports[i], SCIC_SDS_DUMMY_PORT, ihost);
  1373. /* Construct the phys for this controller */
  1374. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1375. /* Add all the PHYs to the dummy port */
  1376. sci_phy_construct(&ihost->phys[i],
  1377. &ihost->ports[SCI_MAX_PORTS], i);
  1378. }
  1379. ihost->invalid_phy_mask = 0;
  1380. sci_init_timer(&ihost->timer, controller_timeout);
  1381. return sci_controller_reset(ihost);
  1382. }
  1383. int sci_oem_parameters_validate(struct sci_oem_params *oem, u8 version)
  1384. {
  1385. int i;
  1386. for (i = 0; i < SCI_MAX_PORTS; i++)
  1387. if (oem->ports[i].phy_mask > SCIC_SDS_PARM_PHY_MASK_MAX)
  1388. return -EINVAL;
  1389. for (i = 0; i < SCI_MAX_PHYS; i++)
  1390. if (oem->phys[i].sas_address.high == 0 &&
  1391. oem->phys[i].sas_address.low == 0)
  1392. return -EINVAL;
  1393. if (oem->controller.mode_type == SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE) {
  1394. for (i = 0; i < SCI_MAX_PHYS; i++)
  1395. if (oem->ports[i].phy_mask != 0)
  1396. return -EINVAL;
  1397. } else if (oem->controller.mode_type == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
  1398. u8 phy_mask = 0;
  1399. for (i = 0; i < SCI_MAX_PHYS; i++)
  1400. phy_mask |= oem->ports[i].phy_mask;
  1401. if (phy_mask == 0)
  1402. return -EINVAL;
  1403. } else
  1404. return -EINVAL;
  1405. if (oem->controller.max_concurr_spin_up > MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT ||
  1406. oem->controller.max_concurr_spin_up < 1)
  1407. return -EINVAL;
  1408. if (oem->controller.do_enable_ssc) {
  1409. if (version < ISCI_ROM_VER_1_1 && oem->controller.do_enable_ssc != 1)
  1410. return -EINVAL;
  1411. if (version >= ISCI_ROM_VER_1_1) {
  1412. u8 test = oem->controller.ssc_sata_tx_spread_level;
  1413. switch (test) {
  1414. case 0:
  1415. case 2:
  1416. case 3:
  1417. case 6:
  1418. case 7:
  1419. break;
  1420. default:
  1421. return -EINVAL;
  1422. }
  1423. test = oem->controller.ssc_sas_tx_spread_level;
  1424. if (oem->controller.ssc_sas_tx_type == 0) {
  1425. switch (test) {
  1426. case 0:
  1427. case 2:
  1428. case 3:
  1429. break;
  1430. default:
  1431. return -EINVAL;
  1432. }
  1433. } else if (oem->controller.ssc_sas_tx_type == 1) {
  1434. switch (test) {
  1435. case 0:
  1436. case 3:
  1437. case 6:
  1438. break;
  1439. default:
  1440. return -EINVAL;
  1441. }
  1442. }
  1443. }
  1444. }
  1445. return 0;
  1446. }
  1447. static u8 max_spin_up(struct isci_host *ihost)
  1448. {
  1449. if (ihost->user_parameters.max_concurr_spinup)
  1450. return min_t(u8, ihost->user_parameters.max_concurr_spinup,
  1451. MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
  1452. else
  1453. return min_t(u8, ihost->oem_parameters.controller.max_concurr_spin_up,
  1454. MAX_CONCURRENT_DEVICE_SPIN_UP_COUNT);
  1455. }
  1456. static void power_control_timeout(unsigned long data)
  1457. {
  1458. struct sci_timer *tmr = (struct sci_timer *)data;
  1459. struct isci_host *ihost = container_of(tmr, typeof(*ihost), power_control.timer);
  1460. struct isci_phy *iphy;
  1461. unsigned long flags;
  1462. u8 i;
  1463. spin_lock_irqsave(&ihost->scic_lock, flags);
  1464. if (tmr->cancel)
  1465. goto done;
  1466. ihost->power_control.phys_granted_power = 0;
  1467. if (ihost->power_control.phys_waiting == 0) {
  1468. ihost->power_control.timer_started = false;
  1469. goto done;
  1470. }
  1471. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1472. if (ihost->power_control.phys_waiting == 0)
  1473. break;
  1474. iphy = ihost->power_control.requesters[i];
  1475. if (iphy == NULL)
  1476. continue;
  1477. if (ihost->power_control.phys_granted_power >= max_spin_up(ihost))
  1478. break;
  1479. ihost->power_control.requesters[i] = NULL;
  1480. ihost->power_control.phys_waiting--;
  1481. ihost->power_control.phys_granted_power++;
  1482. sci_phy_consume_power_handler(iphy);
  1483. if (iphy->protocol == SAS_PROTOCOL_SSP) {
  1484. u8 j;
  1485. for (j = 0; j < SCI_MAX_PHYS; j++) {
  1486. struct isci_phy *requester = ihost->power_control.requesters[j];
  1487. /*
  1488. * Search the power_control queue to see if there are other phys
  1489. * attached to the same remote device. If found, take all of
  1490. * them out of await_sas_power state.
  1491. */
  1492. if (requester != NULL && requester != iphy) {
  1493. u8 other = memcmp(requester->frame_rcvd.iaf.sas_addr,
  1494. iphy->frame_rcvd.iaf.sas_addr,
  1495. sizeof(requester->frame_rcvd.iaf.sas_addr));
  1496. if (other == 0) {
  1497. ihost->power_control.requesters[j] = NULL;
  1498. ihost->power_control.phys_waiting--;
  1499. sci_phy_consume_power_handler(requester);
  1500. }
  1501. }
  1502. }
  1503. }
  1504. }
  1505. /*
  1506. * It doesn't matter if the power list is empty, we need to start the
  1507. * timer in case another phy becomes ready.
  1508. */
  1509. sci_mod_timer(tmr, SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1510. ihost->power_control.timer_started = true;
  1511. done:
  1512. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1513. }
  1514. void sci_controller_power_control_queue_insert(struct isci_host *ihost,
  1515. struct isci_phy *iphy)
  1516. {
  1517. BUG_ON(iphy == NULL);
  1518. if (ihost->power_control.phys_granted_power < max_spin_up(ihost)) {
  1519. ihost->power_control.phys_granted_power++;
  1520. sci_phy_consume_power_handler(iphy);
  1521. /*
  1522. * stop and start the power_control timer. When the timer fires, the
  1523. * no_of_phys_granted_power will be set to 0
  1524. */
  1525. if (ihost->power_control.timer_started)
  1526. sci_del_timer(&ihost->power_control.timer);
  1527. sci_mod_timer(&ihost->power_control.timer,
  1528. SCIC_SDS_CONTROLLER_POWER_CONTROL_INTERVAL);
  1529. ihost->power_control.timer_started = true;
  1530. } else {
  1531. /*
  1532. * There are phys, attached to the same sas address as this phy, are
  1533. * already in READY state, this phy don't need wait.
  1534. */
  1535. u8 i;
  1536. struct isci_phy *current_phy;
  1537. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1538. u8 other;
  1539. current_phy = &ihost->phys[i];
  1540. other = memcmp(current_phy->frame_rcvd.iaf.sas_addr,
  1541. iphy->frame_rcvd.iaf.sas_addr,
  1542. sizeof(current_phy->frame_rcvd.iaf.sas_addr));
  1543. if (current_phy->sm.current_state_id == SCI_PHY_READY &&
  1544. current_phy->protocol == SAS_PROTOCOL_SSP &&
  1545. other == 0) {
  1546. sci_phy_consume_power_handler(iphy);
  1547. break;
  1548. }
  1549. }
  1550. if (i == SCI_MAX_PHYS) {
  1551. /* Add the phy in the waiting list */
  1552. ihost->power_control.requesters[iphy->phy_index] = iphy;
  1553. ihost->power_control.phys_waiting++;
  1554. }
  1555. }
  1556. }
  1557. void sci_controller_power_control_queue_remove(struct isci_host *ihost,
  1558. struct isci_phy *iphy)
  1559. {
  1560. BUG_ON(iphy == NULL);
  1561. if (ihost->power_control.requesters[iphy->phy_index])
  1562. ihost->power_control.phys_waiting--;
  1563. ihost->power_control.requesters[iphy->phy_index] = NULL;
  1564. }
  1565. static int is_long_cable(int phy, unsigned char selection_byte)
  1566. {
  1567. return !!(selection_byte & (1 << phy));
  1568. }
  1569. static int is_medium_cable(int phy, unsigned char selection_byte)
  1570. {
  1571. return !!(selection_byte & (1 << (phy + 4)));
  1572. }
  1573. static enum cable_selections decode_selection_byte(
  1574. int phy,
  1575. unsigned char selection_byte)
  1576. {
  1577. return ((selection_byte & (1 << phy)) ? 1 : 0)
  1578. + (selection_byte & (1 << (phy + 4)) ? 2 : 0);
  1579. }
  1580. static unsigned char *to_cable_select(struct isci_host *ihost)
  1581. {
  1582. if (is_cable_select_overridden())
  1583. return ((unsigned char *)&cable_selection_override)
  1584. + ihost->id;
  1585. else
  1586. return &ihost->oem_parameters.controller.cable_selection_mask;
  1587. }
  1588. enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy)
  1589. {
  1590. return decode_selection_byte(phy, *to_cable_select(ihost));
  1591. }
  1592. char *lookup_cable_names(enum cable_selections selection)
  1593. {
  1594. static char *cable_names[] = {
  1595. [short_cable] = "short",
  1596. [long_cable] = "long",
  1597. [medium_cable] = "medium",
  1598. [undefined_cable] = "<undefined, assumed long>" /* bit 0==1 */
  1599. };
  1600. return (selection <= undefined_cable) ? cable_names[selection]
  1601. : cable_names[undefined_cable];
  1602. }
  1603. #define AFE_REGISTER_WRITE_DELAY 10
  1604. static void sci_controller_afe_initialization(struct isci_host *ihost)
  1605. {
  1606. struct scu_afe_registers __iomem *afe = &ihost->scu_registers->afe;
  1607. const struct sci_oem_params *oem = &ihost->oem_parameters;
  1608. struct pci_dev *pdev = ihost->pdev;
  1609. u32 afe_status;
  1610. u32 phy_id;
  1611. unsigned char cable_selection_mask = *to_cable_select(ihost);
  1612. /* Clear DFX Status registers */
  1613. writel(0x0081000f, &afe->afe_dfx_master_control0);
  1614. udelay(AFE_REGISTER_WRITE_DELAY);
  1615. if (is_b0(pdev) || is_c0(pdev) || is_c1(pdev)) {
  1616. /* PM Rx Equalization Save, PM SPhy Rx Acknowledgement
  1617. * Timer, PM Stagger Timer
  1618. */
  1619. writel(0x0007FFFF, &afe->afe_pmsn_master_control2);
  1620. udelay(AFE_REGISTER_WRITE_DELAY);
  1621. }
  1622. /* Configure bias currents to normal */
  1623. if (is_a2(pdev))
  1624. writel(0x00005A00, &afe->afe_bias_control);
  1625. else if (is_b0(pdev) || is_c0(pdev))
  1626. writel(0x00005F00, &afe->afe_bias_control);
  1627. else if (is_c1(pdev))
  1628. writel(0x00005500, &afe->afe_bias_control);
  1629. udelay(AFE_REGISTER_WRITE_DELAY);
  1630. /* Enable PLL */
  1631. if (is_a2(pdev))
  1632. writel(0x80040908, &afe->afe_pll_control0);
  1633. else if (is_b0(pdev) || is_c0(pdev))
  1634. writel(0x80040A08, &afe->afe_pll_control0);
  1635. else if (is_c1(pdev)) {
  1636. writel(0x80000B08, &afe->afe_pll_control0);
  1637. udelay(AFE_REGISTER_WRITE_DELAY);
  1638. writel(0x00000B08, &afe->afe_pll_control0);
  1639. udelay(AFE_REGISTER_WRITE_DELAY);
  1640. writel(0x80000B08, &afe->afe_pll_control0);
  1641. }
  1642. udelay(AFE_REGISTER_WRITE_DELAY);
  1643. /* Wait for the PLL to lock */
  1644. do {
  1645. afe_status = readl(&afe->afe_common_block_status);
  1646. udelay(AFE_REGISTER_WRITE_DELAY);
  1647. } while ((afe_status & 0x00001000) == 0);
  1648. if (is_a2(pdev)) {
  1649. /* Shorten SAS SNW lock time (RxLock timer value from 76
  1650. * us to 50 us)
  1651. */
  1652. writel(0x7bcc96ad, &afe->afe_pmsn_master_control0);
  1653. udelay(AFE_REGISTER_WRITE_DELAY);
  1654. }
  1655. for (phy_id = 0; phy_id < SCI_MAX_PHYS; phy_id++) {
  1656. struct scu_afe_transceiver __iomem *xcvr = &afe->scu_afe_xcvr[phy_id];
  1657. const struct sci_phy_oem_params *oem_phy = &oem->phys[phy_id];
  1658. int cable_length_long =
  1659. is_long_cable(phy_id, cable_selection_mask);
  1660. int cable_length_medium =
  1661. is_medium_cable(phy_id, cable_selection_mask);
  1662. if (is_a2(pdev)) {
  1663. /* All defaults, except the Receive Word
  1664. * Alignament/Comma Detect Enable....(0xe800)
  1665. */
  1666. writel(0x00004512, &xcvr->afe_xcvr_control0);
  1667. udelay(AFE_REGISTER_WRITE_DELAY);
  1668. writel(0x0050100F, &xcvr->afe_xcvr_control1);
  1669. udelay(AFE_REGISTER_WRITE_DELAY);
  1670. } else if (is_b0(pdev)) {
  1671. /* Configure transmitter SSC parameters */
  1672. writel(0x00030000, &xcvr->afe_tx_ssc_control);
  1673. udelay(AFE_REGISTER_WRITE_DELAY);
  1674. } else if (is_c0(pdev)) {
  1675. /* Configure transmitter SSC parameters */
  1676. writel(0x00010202, &xcvr->afe_tx_ssc_control);
  1677. udelay(AFE_REGISTER_WRITE_DELAY);
  1678. /* All defaults, except the Receive Word
  1679. * Alignament/Comma Detect Enable....(0xe800)
  1680. */
  1681. writel(0x00014500, &xcvr->afe_xcvr_control0);
  1682. udelay(AFE_REGISTER_WRITE_DELAY);
  1683. } else if (is_c1(pdev)) {
  1684. /* Configure transmitter SSC parameters */
  1685. writel(0x00010202, &xcvr->afe_tx_ssc_control);
  1686. udelay(AFE_REGISTER_WRITE_DELAY);
  1687. /* All defaults, except the Receive Word
  1688. * Alignament/Comma Detect Enable....(0xe800)
  1689. */
  1690. writel(0x0001C500, &xcvr->afe_xcvr_control0);
  1691. udelay(AFE_REGISTER_WRITE_DELAY);
  1692. }
  1693. /* Power up TX and RX out from power down (PWRDNTX and
  1694. * PWRDNRX) & increase TX int & ext bias 20%....(0xe85c)
  1695. */
  1696. if (is_a2(pdev))
  1697. writel(0x000003F0, &xcvr->afe_channel_control);
  1698. else if (is_b0(pdev)) {
  1699. writel(0x000003D7, &xcvr->afe_channel_control);
  1700. udelay(AFE_REGISTER_WRITE_DELAY);
  1701. writel(0x000003D4, &xcvr->afe_channel_control);
  1702. } else if (is_c0(pdev)) {
  1703. writel(0x000001E7, &xcvr->afe_channel_control);
  1704. udelay(AFE_REGISTER_WRITE_DELAY);
  1705. writel(0x000001E4, &xcvr->afe_channel_control);
  1706. } else if (is_c1(pdev)) {
  1707. writel(cable_length_long ? 0x000002F7 : 0x000001F7,
  1708. &xcvr->afe_channel_control);
  1709. udelay(AFE_REGISTER_WRITE_DELAY);
  1710. writel(cable_length_long ? 0x000002F4 : 0x000001F4,
  1711. &xcvr->afe_channel_control);
  1712. }
  1713. udelay(AFE_REGISTER_WRITE_DELAY);
  1714. if (is_a2(pdev)) {
  1715. /* Enable TX equalization (0xe824) */
  1716. writel(0x00040000, &xcvr->afe_tx_control);
  1717. udelay(AFE_REGISTER_WRITE_DELAY);
  1718. }
  1719. if (is_a2(pdev) || is_b0(pdev))
  1720. /* RDPI=0x0(RX Power On), RXOOBDETPDNC=0x0,
  1721. * TPD=0x0(TX Power On), RDD=0x0(RX Detect
  1722. * Enabled) ....(0xe800)
  1723. */
  1724. writel(0x00004100, &xcvr->afe_xcvr_control0);
  1725. else if (is_c0(pdev))
  1726. writel(0x00014100, &xcvr->afe_xcvr_control0);
  1727. else if (is_c1(pdev))
  1728. writel(0x0001C100, &xcvr->afe_xcvr_control0);
  1729. udelay(AFE_REGISTER_WRITE_DELAY);
  1730. /* Leave DFE/FFE on */
  1731. if (is_a2(pdev))
  1732. writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
  1733. else if (is_b0(pdev)) {
  1734. writel(0x3F11103F, &xcvr->afe_rx_ssc_control0);
  1735. udelay(AFE_REGISTER_WRITE_DELAY);
  1736. /* Enable TX equalization (0xe824) */
  1737. writel(0x00040000, &xcvr->afe_tx_control);
  1738. } else if (is_c0(pdev)) {
  1739. writel(0x01400C0F, &xcvr->afe_rx_ssc_control1);
  1740. udelay(AFE_REGISTER_WRITE_DELAY);
  1741. writel(0x3F6F103F, &xcvr->afe_rx_ssc_control0);
  1742. udelay(AFE_REGISTER_WRITE_DELAY);
  1743. /* Enable TX equalization (0xe824) */
  1744. writel(0x00040000, &xcvr->afe_tx_control);
  1745. } else if (is_c1(pdev)) {
  1746. writel(cable_length_long ? 0x01500C0C :
  1747. cable_length_medium ? 0x01400C0D : 0x02400C0D,
  1748. &xcvr->afe_xcvr_control1);
  1749. udelay(AFE_REGISTER_WRITE_DELAY);
  1750. writel(0x000003E0, &xcvr->afe_dfx_rx_control1);
  1751. udelay(AFE_REGISTER_WRITE_DELAY);
  1752. writel(cable_length_long ? 0x33091C1F :
  1753. cable_length_medium ? 0x3315181F : 0x2B17161F,
  1754. &xcvr->afe_rx_ssc_control0);
  1755. udelay(AFE_REGISTER_WRITE_DELAY);
  1756. /* Enable TX equalization (0xe824) */
  1757. writel(0x00040000, &xcvr->afe_tx_control);
  1758. }
  1759. udelay(AFE_REGISTER_WRITE_DELAY);
  1760. writel(oem_phy->afe_tx_amp_control0, &xcvr->afe_tx_amp_control0);
  1761. udelay(AFE_REGISTER_WRITE_DELAY);
  1762. writel(oem_phy->afe_tx_amp_control1, &xcvr->afe_tx_amp_control1);
  1763. udelay(AFE_REGISTER_WRITE_DELAY);
  1764. writel(oem_phy->afe_tx_amp_control2, &xcvr->afe_tx_amp_control2);
  1765. udelay(AFE_REGISTER_WRITE_DELAY);
  1766. writel(oem_phy->afe_tx_amp_control3, &xcvr->afe_tx_amp_control3);
  1767. udelay(AFE_REGISTER_WRITE_DELAY);
  1768. }
  1769. /* Transfer control to the PEs */
  1770. writel(0x00010f00, &afe->afe_dfx_master_control0);
  1771. udelay(AFE_REGISTER_WRITE_DELAY);
  1772. }
  1773. static void sci_controller_initialize_power_control(struct isci_host *ihost)
  1774. {
  1775. sci_init_timer(&ihost->power_control.timer, power_control_timeout);
  1776. memset(ihost->power_control.requesters, 0,
  1777. sizeof(ihost->power_control.requesters));
  1778. ihost->power_control.phys_waiting = 0;
  1779. ihost->power_control.phys_granted_power = 0;
  1780. }
  1781. static enum sci_status sci_controller_initialize(struct isci_host *ihost)
  1782. {
  1783. struct sci_base_state_machine *sm = &ihost->sm;
  1784. enum sci_status result = SCI_FAILURE;
  1785. unsigned long i, state, val;
  1786. if (ihost->sm.current_state_id != SCIC_RESET) {
  1787. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  1788. __func__, ihost->sm.current_state_id);
  1789. return SCI_FAILURE_INVALID_STATE;
  1790. }
  1791. sci_change_state(sm, SCIC_INITIALIZING);
  1792. sci_init_timer(&ihost->phy_timer, phy_startup_timeout);
  1793. ihost->next_phy_to_start = 0;
  1794. ihost->phy_startup_timer_pending = false;
  1795. sci_controller_initialize_power_control(ihost);
  1796. /*
  1797. * There is nothing to do here for B0 since we do not have to
  1798. * program the AFE registers.
  1799. * / @todo The AFE settings are supposed to be correct for the B0 but
  1800. * / presently they seem to be wrong. */
  1801. sci_controller_afe_initialization(ihost);
  1802. /* Take the hardware out of reset */
  1803. writel(0, &ihost->smu_registers->soft_reset_control);
  1804. /*
  1805. * / @todo Provide meaningfull error code for hardware failure
  1806. * result = SCI_FAILURE_CONTROLLER_HARDWARE; */
  1807. for (i = 100; i >= 1; i--) {
  1808. u32 status;
  1809. /* Loop until the hardware reports success */
  1810. udelay(SCU_CONTEXT_RAM_INIT_STALL_TIME);
  1811. status = readl(&ihost->smu_registers->control_status);
  1812. if ((status & SCU_RAM_INIT_COMPLETED) == SCU_RAM_INIT_COMPLETED)
  1813. break;
  1814. }
  1815. if (i == 0)
  1816. goto out;
  1817. /*
  1818. * Determine what are the actaul device capacities that the
  1819. * hardware will support */
  1820. val = readl(&ihost->smu_registers->device_context_capacity);
  1821. /* Record the smaller of the two capacity values */
  1822. ihost->logical_port_entries = min(smu_max_ports(val), SCI_MAX_PORTS);
  1823. ihost->task_context_entries = min(smu_max_task_contexts(val), SCI_MAX_IO_REQUESTS);
  1824. ihost->remote_node_entries = min(smu_max_rncs(val), SCI_MAX_REMOTE_DEVICES);
  1825. /*
  1826. * Make all PEs that are unassigned match up with the
  1827. * logical ports
  1828. */
  1829. for (i = 0; i < ihost->logical_port_entries; i++) {
  1830. struct scu_port_task_scheduler_group_registers __iomem
  1831. *ptsg = &ihost->scu_registers->peg0.ptsg;
  1832. writel(i, &ptsg->protocol_engine[i]);
  1833. }
  1834. /* Initialize hardware PCI Relaxed ordering in DMA engines */
  1835. val = readl(&ihost->scu_registers->sdma.pdma_configuration);
  1836. val |= SCU_PDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1837. writel(val, &ihost->scu_registers->sdma.pdma_configuration);
  1838. val = readl(&ihost->scu_registers->sdma.cdma_configuration);
  1839. val |= SCU_CDMACR_GEN_BIT(PCI_RELAXED_ORDERING_ENABLE);
  1840. writel(val, &ihost->scu_registers->sdma.cdma_configuration);
  1841. /*
  1842. * Initialize the PHYs before the PORTs because the PHY registers
  1843. * are accessed during the port initialization.
  1844. */
  1845. for (i = 0; i < SCI_MAX_PHYS; i++) {
  1846. result = sci_phy_initialize(&ihost->phys[i],
  1847. &ihost->scu_registers->peg0.pe[i].tl,
  1848. &ihost->scu_registers->peg0.pe[i].ll);
  1849. if (result != SCI_SUCCESS)
  1850. goto out;
  1851. }
  1852. for (i = 0; i < ihost->logical_port_entries; i++) {
  1853. struct isci_port *iport = &ihost->ports[i];
  1854. iport->port_task_scheduler_registers = &ihost->scu_registers->peg0.ptsg.port[i];
  1855. iport->port_pe_configuration_register = &ihost->scu_registers->peg0.ptsg.protocol_engine[0];
  1856. iport->viit_registers = &ihost->scu_registers->peg0.viit[i];
  1857. }
  1858. result = sci_port_configuration_agent_initialize(ihost, &ihost->port_agent);
  1859. out:
  1860. /* Advance the controller state machine */
  1861. if (result == SCI_SUCCESS)
  1862. state = SCIC_INITIALIZED;
  1863. else
  1864. state = SCIC_FAILED;
  1865. sci_change_state(sm, state);
  1866. return result;
  1867. }
  1868. static int sci_controller_dma_alloc(struct isci_host *ihost)
  1869. {
  1870. struct device *dev = &ihost->pdev->dev;
  1871. size_t size;
  1872. int i;
  1873. /* detect re-initialization */
  1874. if (ihost->completion_queue)
  1875. return 0;
  1876. size = SCU_MAX_COMPLETION_QUEUE_ENTRIES * sizeof(u32);
  1877. ihost->completion_queue = dmam_alloc_coherent(dev, size, &ihost->cq_dma,
  1878. GFP_KERNEL);
  1879. if (!ihost->completion_queue)
  1880. return -ENOMEM;
  1881. size = ihost->remote_node_entries * sizeof(union scu_remote_node_context);
  1882. ihost->remote_node_context_table = dmam_alloc_coherent(dev, size, &ihost->rnc_dma,
  1883. GFP_KERNEL);
  1884. if (!ihost->remote_node_context_table)
  1885. return -ENOMEM;
  1886. size = ihost->task_context_entries * sizeof(struct scu_task_context),
  1887. ihost->task_context_table = dmam_alloc_coherent(dev, size, &ihost->tc_dma,
  1888. GFP_KERNEL);
  1889. if (!ihost->task_context_table)
  1890. return -ENOMEM;
  1891. size = SCI_UFI_TOTAL_SIZE;
  1892. ihost->ufi_buf = dmam_alloc_coherent(dev, size, &ihost->ufi_dma, GFP_KERNEL);
  1893. if (!ihost->ufi_buf)
  1894. return -ENOMEM;
  1895. for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
  1896. struct isci_request *ireq;
  1897. dma_addr_t dma;
  1898. ireq = dmam_alloc_coherent(dev, sizeof(*ireq), &dma, GFP_KERNEL);
  1899. if (!ireq)
  1900. return -ENOMEM;
  1901. ireq->tc = &ihost->task_context_table[i];
  1902. ireq->owning_controller = ihost;
  1903. ireq->request_daddr = dma;
  1904. ireq->isci_host = ihost;
  1905. ihost->reqs[i] = ireq;
  1906. }
  1907. return 0;
  1908. }
  1909. static int sci_controller_mem_init(struct isci_host *ihost)
  1910. {
  1911. int err = sci_controller_dma_alloc(ihost);
  1912. if (err)
  1913. return err;
  1914. writel(lower_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_lower);
  1915. writel(upper_32_bits(ihost->cq_dma), &ihost->smu_registers->completion_queue_upper);
  1916. writel(lower_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_lower);
  1917. writel(upper_32_bits(ihost->rnc_dma), &ihost->smu_registers->remote_node_context_upper);
  1918. writel(lower_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_lower);
  1919. writel(upper_32_bits(ihost->tc_dma), &ihost->smu_registers->host_task_table_upper);
  1920. sci_unsolicited_frame_control_construct(ihost);
  1921. /*
  1922. * Inform the silicon as to the location of the UF headers and
  1923. * address table.
  1924. */
  1925. writel(lower_32_bits(ihost->uf_control.headers.physical_address),
  1926. &ihost->scu_registers->sdma.uf_header_base_address_lower);
  1927. writel(upper_32_bits(ihost->uf_control.headers.physical_address),
  1928. &ihost->scu_registers->sdma.uf_header_base_address_upper);
  1929. writel(lower_32_bits(ihost->uf_control.address_table.physical_address),
  1930. &ihost->scu_registers->sdma.uf_address_table_lower);
  1931. writel(upper_32_bits(ihost->uf_control.address_table.physical_address),
  1932. &ihost->scu_registers->sdma.uf_address_table_upper);
  1933. return 0;
  1934. }
  1935. /**
  1936. * isci_host_init - (re-)initialize hardware and internal (private) state
  1937. * @ihost: host to init
  1938. *
  1939. * Any public facing objects (like asd_sas_port, and asd_sas_phys), or
  1940. * one-time initialization objects like locks and waitqueues, are
  1941. * not touched (they are initialized in isci_host_alloc)
  1942. */
  1943. int isci_host_init(struct isci_host *ihost)
  1944. {
  1945. int i, err;
  1946. enum sci_status status;
  1947. spin_lock_irq(&ihost->scic_lock);
  1948. status = sci_controller_construct(ihost, scu_base(ihost), smu_base(ihost));
  1949. spin_unlock_irq(&ihost->scic_lock);
  1950. if (status != SCI_SUCCESS) {
  1951. dev_err(&ihost->pdev->dev,
  1952. "%s: sci_controller_construct failed - status = %x\n",
  1953. __func__,
  1954. status);
  1955. return -ENODEV;
  1956. }
  1957. spin_lock_irq(&ihost->scic_lock);
  1958. status = sci_controller_initialize(ihost);
  1959. spin_unlock_irq(&ihost->scic_lock);
  1960. if (status != SCI_SUCCESS) {
  1961. dev_warn(&ihost->pdev->dev,
  1962. "%s: sci_controller_initialize failed -"
  1963. " status = 0x%x\n",
  1964. __func__, status);
  1965. return -ENODEV;
  1966. }
  1967. err = sci_controller_mem_init(ihost);
  1968. if (err)
  1969. return err;
  1970. /* enable sgpio */
  1971. writel(1, &ihost->scu_registers->peg0.sgpio.interface_control);
  1972. for (i = 0; i < isci_gpio_count(ihost); i++)
  1973. writel(SGPIO_HW_CONTROL, &ihost->scu_registers->peg0.sgpio.output_data_select[i]);
  1974. writel(0, &ihost->scu_registers->peg0.sgpio.vendor_specific_code);
  1975. return 0;
  1976. }
  1977. void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
  1978. struct isci_phy *iphy)
  1979. {
  1980. switch (ihost->sm.current_state_id) {
  1981. case SCIC_STARTING:
  1982. sci_del_timer(&ihost->phy_timer);
  1983. ihost->phy_startup_timer_pending = false;
  1984. ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
  1985. iport, iphy);
  1986. sci_controller_start_next_phy(ihost);
  1987. break;
  1988. case SCIC_READY:
  1989. ihost->port_agent.link_up_handler(ihost, &ihost->port_agent,
  1990. iport, iphy);
  1991. break;
  1992. default:
  1993. dev_dbg(&ihost->pdev->dev,
  1994. "%s: SCIC Controller linkup event from phy %d in "
  1995. "unexpected state %d\n", __func__, iphy->phy_index,
  1996. ihost->sm.current_state_id);
  1997. }
  1998. }
  1999. void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
  2000. struct isci_phy *iphy)
  2001. {
  2002. switch (ihost->sm.current_state_id) {
  2003. case SCIC_STARTING:
  2004. case SCIC_READY:
  2005. ihost->port_agent.link_down_handler(ihost, &ihost->port_agent,
  2006. iport, iphy);
  2007. break;
  2008. default:
  2009. dev_dbg(&ihost->pdev->dev,
  2010. "%s: SCIC Controller linkdown event from phy %d in "
  2011. "unexpected state %d\n",
  2012. __func__,
  2013. iphy->phy_index,
  2014. ihost->sm.current_state_id);
  2015. }
  2016. }
  2017. bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost)
  2018. {
  2019. u32 index;
  2020. for (index = 0; index < ihost->remote_node_entries; index++) {
  2021. if ((ihost->device_table[index] != NULL) &&
  2022. (ihost->device_table[index]->sm.current_state_id == SCI_DEV_STOPPING))
  2023. return true;
  2024. }
  2025. return false;
  2026. }
  2027. void sci_controller_remote_device_stopped(struct isci_host *ihost,
  2028. struct isci_remote_device *idev)
  2029. {
  2030. if (ihost->sm.current_state_id != SCIC_STOPPING) {
  2031. dev_dbg(&ihost->pdev->dev,
  2032. "SCIC Controller 0x%p remote device stopped event "
  2033. "from device 0x%p in unexpected state %d\n",
  2034. ihost, idev,
  2035. ihost->sm.current_state_id);
  2036. return;
  2037. }
  2038. if (!sci_controller_has_remote_devices_stopping(ihost))
  2039. isci_host_stop_complete(ihost);
  2040. }
  2041. void sci_controller_post_request(struct isci_host *ihost, u32 request)
  2042. {
  2043. dev_dbg(&ihost->pdev->dev, "%s[%d]: %#x\n",
  2044. __func__, ihost->id, request);
  2045. writel(request, &ihost->smu_registers->post_context_port);
  2046. }
  2047. struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag)
  2048. {
  2049. u16 task_index;
  2050. u16 task_sequence;
  2051. task_index = ISCI_TAG_TCI(io_tag);
  2052. if (task_index < ihost->task_context_entries) {
  2053. struct isci_request *ireq = ihost->reqs[task_index];
  2054. if (test_bit(IREQ_ACTIVE, &ireq->flags)) {
  2055. task_sequence = ISCI_TAG_SEQ(io_tag);
  2056. if (task_sequence == ihost->io_request_sequence[task_index])
  2057. return ireq;
  2058. }
  2059. }
  2060. return NULL;
  2061. }
  2062. /**
  2063. * This method allocates remote node index and the reserves the remote node
  2064. * context space for use. This method can fail if there are no more remote
  2065. * node index available.
  2066. * @scic: This is the controller object which contains the set of
  2067. * free remote node ids
  2068. * @sci_dev: This is the device object which is requesting the a remote node
  2069. * id
  2070. * @node_id: This is the remote node id that is assinged to the device if one
  2071. * is available
  2072. *
  2073. * enum sci_status SCI_FAILURE_OUT_OF_RESOURCES if there are no available remote
  2074. * node index available.
  2075. */
  2076. enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
  2077. struct isci_remote_device *idev,
  2078. u16 *node_id)
  2079. {
  2080. u16 node_index;
  2081. u32 remote_node_count = sci_remote_device_node_count(idev);
  2082. node_index = sci_remote_node_table_allocate_remote_node(
  2083. &ihost->available_remote_nodes, remote_node_count
  2084. );
  2085. if (node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  2086. ihost->device_table[node_index] = idev;
  2087. *node_id = node_index;
  2088. return SCI_SUCCESS;
  2089. }
  2090. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2091. }
  2092. void sci_controller_free_remote_node_context(struct isci_host *ihost,
  2093. struct isci_remote_device *idev,
  2094. u16 node_id)
  2095. {
  2096. u32 remote_node_count = sci_remote_device_node_count(idev);
  2097. if (ihost->device_table[node_id] == idev) {
  2098. ihost->device_table[node_id] = NULL;
  2099. sci_remote_node_table_release_remote_node_index(
  2100. &ihost->available_remote_nodes, remote_node_count, node_id
  2101. );
  2102. }
  2103. }
  2104. void sci_controller_copy_sata_response(void *response_buffer,
  2105. void *frame_header,
  2106. void *frame_buffer)
  2107. {
  2108. /* XXX type safety? */
  2109. memcpy(response_buffer, frame_header, sizeof(u32));
  2110. memcpy(response_buffer + sizeof(u32),
  2111. frame_buffer,
  2112. sizeof(struct dev_to_host_fis) - sizeof(u32));
  2113. }
  2114. void sci_controller_release_frame(struct isci_host *ihost, u32 frame_index)
  2115. {
  2116. if (sci_unsolicited_frame_control_release_frame(&ihost->uf_control, frame_index))
  2117. writel(ihost->uf_control.get,
  2118. &ihost->scu_registers->sdma.unsolicited_frame_get_pointer);
  2119. }
  2120. void isci_tci_free(struct isci_host *ihost, u16 tci)
  2121. {
  2122. u16 tail = ihost->tci_tail & (SCI_MAX_IO_REQUESTS-1);
  2123. ihost->tci_pool[tail] = tci;
  2124. ihost->tci_tail = tail + 1;
  2125. }
  2126. static u16 isci_tci_alloc(struct isci_host *ihost)
  2127. {
  2128. u16 head = ihost->tci_head & (SCI_MAX_IO_REQUESTS-1);
  2129. u16 tci = ihost->tci_pool[head];
  2130. ihost->tci_head = head + 1;
  2131. return tci;
  2132. }
  2133. static u16 isci_tci_space(struct isci_host *ihost)
  2134. {
  2135. return CIRC_SPACE(ihost->tci_head, ihost->tci_tail, SCI_MAX_IO_REQUESTS);
  2136. }
  2137. u16 isci_alloc_tag(struct isci_host *ihost)
  2138. {
  2139. if (isci_tci_space(ihost)) {
  2140. u16 tci = isci_tci_alloc(ihost);
  2141. u8 seq = ihost->io_request_sequence[tci];
  2142. return ISCI_TAG(seq, tci);
  2143. }
  2144. return SCI_CONTROLLER_INVALID_IO_TAG;
  2145. }
  2146. enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag)
  2147. {
  2148. u16 tci = ISCI_TAG_TCI(io_tag);
  2149. u16 seq = ISCI_TAG_SEQ(io_tag);
  2150. /* prevent tail from passing head */
  2151. if (isci_tci_active(ihost) == 0)
  2152. return SCI_FAILURE_INVALID_IO_TAG;
  2153. if (seq == ihost->io_request_sequence[tci]) {
  2154. ihost->io_request_sequence[tci] = (seq+1) & (SCI_MAX_SEQ-1);
  2155. isci_tci_free(ihost, tci);
  2156. return SCI_SUCCESS;
  2157. }
  2158. return SCI_FAILURE_INVALID_IO_TAG;
  2159. }
  2160. enum sci_status sci_controller_start_io(struct isci_host *ihost,
  2161. struct isci_remote_device *idev,
  2162. struct isci_request *ireq)
  2163. {
  2164. enum sci_status status;
  2165. if (ihost->sm.current_state_id != SCIC_READY) {
  2166. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2167. __func__, ihost->sm.current_state_id);
  2168. return SCI_FAILURE_INVALID_STATE;
  2169. }
  2170. status = sci_remote_device_start_io(ihost, idev, ireq);
  2171. if (status != SCI_SUCCESS)
  2172. return status;
  2173. set_bit(IREQ_ACTIVE, &ireq->flags);
  2174. sci_controller_post_request(ihost, ireq->post_context);
  2175. return SCI_SUCCESS;
  2176. }
  2177. enum sci_status sci_controller_terminate_request(struct isci_host *ihost,
  2178. struct isci_remote_device *idev,
  2179. struct isci_request *ireq)
  2180. {
  2181. /* terminate an ongoing (i.e. started) core IO request. This does not
  2182. * abort the IO request at the target, but rather removes the IO
  2183. * request from the host controller.
  2184. */
  2185. enum sci_status status;
  2186. if (ihost->sm.current_state_id != SCIC_READY) {
  2187. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2188. __func__, ihost->sm.current_state_id);
  2189. return SCI_FAILURE_INVALID_STATE;
  2190. }
  2191. status = sci_io_request_terminate(ireq);
  2192. dev_dbg(&ihost->pdev->dev, "%s: status=%d; ireq=%p; flags=%lx\n",
  2193. __func__, status, ireq, ireq->flags);
  2194. if ((status == SCI_SUCCESS) &&
  2195. !test_bit(IREQ_PENDING_ABORT, &ireq->flags) &&
  2196. !test_and_set_bit(IREQ_TC_ABORT_POSTED, &ireq->flags)) {
  2197. /* Utilize the original post context command and or in the
  2198. * POST_TC_ABORT request sub-type.
  2199. */
  2200. sci_controller_post_request(
  2201. ihost, ireq->post_context |
  2202. SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT);
  2203. }
  2204. return status;
  2205. }
  2206. /**
  2207. * sci_controller_complete_io() - This method will perform core specific
  2208. * completion operations for an IO request. After this method is invoked,
  2209. * the user should consider the IO request as invalid until it is properly
  2210. * reused (i.e. re-constructed).
  2211. * @ihost: The handle to the controller object for which to complete the
  2212. * IO request.
  2213. * @idev: The handle to the remote device object for which to complete
  2214. * the IO request.
  2215. * @ireq: the handle to the io request object to complete.
  2216. */
  2217. enum sci_status sci_controller_complete_io(struct isci_host *ihost,
  2218. struct isci_remote_device *idev,
  2219. struct isci_request *ireq)
  2220. {
  2221. enum sci_status status;
  2222. u16 index;
  2223. switch (ihost->sm.current_state_id) {
  2224. case SCIC_STOPPING:
  2225. /* XXX: Implement this function */
  2226. return SCI_FAILURE;
  2227. case SCIC_READY:
  2228. status = sci_remote_device_complete_io(ihost, idev, ireq);
  2229. if (status != SCI_SUCCESS)
  2230. return status;
  2231. index = ISCI_TAG_TCI(ireq->io_tag);
  2232. clear_bit(IREQ_ACTIVE, &ireq->flags);
  2233. return SCI_SUCCESS;
  2234. default:
  2235. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2236. __func__, ihost->sm.current_state_id);
  2237. return SCI_FAILURE_INVALID_STATE;
  2238. }
  2239. }
  2240. enum sci_status sci_controller_continue_io(struct isci_request *ireq)
  2241. {
  2242. struct isci_host *ihost = ireq->owning_controller;
  2243. if (ihost->sm.current_state_id != SCIC_READY) {
  2244. dev_warn(&ihost->pdev->dev, "%s invalid state: %d\n",
  2245. __func__, ihost->sm.current_state_id);
  2246. return SCI_FAILURE_INVALID_STATE;
  2247. }
  2248. set_bit(IREQ_ACTIVE, &ireq->flags);
  2249. sci_controller_post_request(ihost, ireq->post_context);
  2250. return SCI_SUCCESS;
  2251. }
  2252. /**
  2253. * sci_controller_start_task() - This method is called by the SCIC user to
  2254. * send/start a framework task management request.
  2255. * @controller: the handle to the controller object for which to start the task
  2256. * management request.
  2257. * @remote_device: the handle to the remote device object for which to start
  2258. * the task management request.
  2259. * @task_request: the handle to the task request object to start.
  2260. */
  2261. enum sci_task_status sci_controller_start_task(struct isci_host *ihost,
  2262. struct isci_remote_device *idev,
  2263. struct isci_request *ireq)
  2264. {
  2265. enum sci_status status;
  2266. if (ihost->sm.current_state_id != SCIC_READY) {
  2267. dev_warn(&ihost->pdev->dev,
  2268. "%s: SCIC Controller starting task from invalid "
  2269. "state\n",
  2270. __func__);
  2271. return SCI_TASK_FAILURE_INVALID_STATE;
  2272. }
  2273. status = sci_remote_device_start_task(ihost, idev, ireq);
  2274. switch (status) {
  2275. case SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS:
  2276. set_bit(IREQ_ACTIVE, &ireq->flags);
  2277. /*
  2278. * We will let framework know this task request started successfully,
  2279. * although core is still woring on starting the request (to post tc when
  2280. * RNC is resumed.)
  2281. */
  2282. return SCI_SUCCESS;
  2283. case SCI_SUCCESS:
  2284. set_bit(IREQ_ACTIVE, &ireq->flags);
  2285. sci_controller_post_request(ihost, ireq->post_context);
  2286. break;
  2287. default:
  2288. break;
  2289. }
  2290. return status;
  2291. }
  2292. static int sci_write_gpio_tx_gp(struct isci_host *ihost, u8 reg_index, u8 reg_count, u8 *write_data)
  2293. {
  2294. int d;
  2295. /* no support for TX_GP_CFG */
  2296. if (reg_index == 0)
  2297. return -EINVAL;
  2298. for (d = 0; d < isci_gpio_count(ihost); d++) {
  2299. u32 val = 0x444; /* all ODx.n clear */
  2300. int i;
  2301. for (i = 0; i < 3; i++) {
  2302. int bit = (i << 2) + 2;
  2303. bit = try_test_sas_gpio_gp_bit(to_sas_gpio_od(d, i),
  2304. write_data, reg_index,
  2305. reg_count);
  2306. if (bit < 0)
  2307. break;
  2308. /* if od is set, clear the 'invert' bit */
  2309. val &= ~(bit << ((i << 2) + 2));
  2310. }
  2311. if (i < 3)
  2312. break;
  2313. writel(val, &ihost->scu_registers->peg0.sgpio.output_data_select[d]);
  2314. }
  2315. /* unless reg_index is > 1, we should always be able to write at
  2316. * least one register
  2317. */
  2318. return d > 0;
  2319. }
  2320. int isci_gpio_write(struct sas_ha_struct *sas_ha, u8 reg_type, u8 reg_index,
  2321. u8 reg_count, u8 *write_data)
  2322. {
  2323. struct isci_host *ihost = sas_ha->lldd_ha;
  2324. int written;
  2325. switch (reg_type) {
  2326. case SAS_GPIO_REG_TX_GP:
  2327. written = sci_write_gpio_tx_gp(ihost, reg_index, reg_count, write_data);
  2328. break;
  2329. default:
  2330. written = -EINVAL;
  2331. }
  2332. return written;
  2333. }