host.c 87 KB

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