request.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679
  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 "isci.h"
  56. #include "task.h"
  57. #include "request.h"
  58. #include "sata.h"
  59. #include "scu_completion_codes.h"
  60. #include "scu_event_codes.h"
  61. #include "sas.h"
  62. /**
  63. * This method returns the sgl element pair for the specificed sgl_pair index.
  64. * @sci_req: This parameter specifies the IO request for which to retrieve
  65. * the Scatter-Gather List element pair.
  66. * @sgl_pair_index: This parameter specifies the index into the SGL element
  67. * pair to be retrieved.
  68. *
  69. * This method returns a pointer to an struct scu_sgl_element_pair.
  70. */
  71. static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
  72. struct scic_sds_request *sci_req,
  73. u32 sgl_pair_index
  74. ) {
  75. struct scu_task_context *task_context;
  76. task_context = (struct scu_task_context *)sci_req->task_context_buffer;
  77. if (sgl_pair_index == 0) {
  78. return &task_context->sgl_pair_ab;
  79. } else if (sgl_pair_index == 1) {
  80. return &task_context->sgl_pair_cd;
  81. }
  82. return &sci_req->sg_table[sgl_pair_index - 2];
  83. }
  84. /**
  85. * This function will build the SGL list for an IO request.
  86. * @sci_req: This parameter specifies the IO request for which to build
  87. * the Scatter-Gather List.
  88. *
  89. */
  90. static void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
  91. {
  92. struct isci_request *isci_request = sci_req_to_ireq(sds_request);
  93. struct isci_host *isci_host = isci_request->isci_host;
  94. struct sas_task *task = isci_request_access_task(isci_request);
  95. struct scatterlist *sg = NULL;
  96. dma_addr_t dma_addr;
  97. u32 sg_idx = 0;
  98. struct scu_sgl_element_pair *scu_sg = NULL;
  99. struct scu_sgl_element_pair *prev_sg = NULL;
  100. if (task->num_scatter > 0) {
  101. sg = task->scatter;
  102. while (sg) {
  103. scu_sg = scic_sds_request_get_sgl_element_pair(
  104. sds_request,
  105. sg_idx);
  106. SCU_SGL_COPY(scu_sg->A, sg);
  107. sg = sg_next(sg);
  108. if (sg) {
  109. SCU_SGL_COPY(scu_sg->B, sg);
  110. sg = sg_next(sg);
  111. } else
  112. SCU_SGL_ZERO(scu_sg->B);
  113. if (prev_sg) {
  114. dma_addr =
  115. scic_io_request_get_dma_addr(
  116. sds_request,
  117. scu_sg);
  118. prev_sg->next_pair_upper =
  119. upper_32_bits(dma_addr);
  120. prev_sg->next_pair_lower =
  121. lower_32_bits(dma_addr);
  122. }
  123. prev_sg = scu_sg;
  124. sg_idx++;
  125. }
  126. } else { /* handle when no sg */
  127. scu_sg = scic_sds_request_get_sgl_element_pair(sds_request,
  128. sg_idx);
  129. dma_addr = dma_map_single(&isci_host->pdev->dev,
  130. task->scatter,
  131. task->total_xfer_len,
  132. task->data_dir);
  133. isci_request->zero_scatter_daddr = dma_addr;
  134. scu_sg->A.length = task->total_xfer_len;
  135. scu_sg->A.address_upper = upper_32_bits(dma_addr);
  136. scu_sg->A.address_lower = lower_32_bits(dma_addr);
  137. }
  138. if (scu_sg) {
  139. scu_sg->next_pair_upper = 0;
  140. scu_sg->next_pair_lower = 0;
  141. }
  142. }
  143. static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
  144. {
  145. struct ssp_cmd_iu *cmd_iu;
  146. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  147. struct sas_task *task = isci_request_access_task(ireq);
  148. cmd_iu = &sci_req->ssp.cmd;
  149. memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
  150. cmd_iu->add_cdb_len = 0;
  151. cmd_iu->_r_a = 0;
  152. cmd_iu->_r_b = 0;
  153. cmd_iu->en_fburst = 0; /* unsupported */
  154. cmd_iu->task_prio = task->ssp_task.task_prio;
  155. cmd_iu->task_attr = task->ssp_task.task_attr;
  156. cmd_iu->_r_c = 0;
  157. sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
  158. sizeof(task->ssp_task.cdb) / sizeof(u32));
  159. }
  160. static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
  161. {
  162. struct ssp_task_iu *task_iu;
  163. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  164. struct sas_task *task = isci_request_access_task(ireq);
  165. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  166. task_iu = &sci_req->ssp.tmf;
  167. memset(task_iu, 0, sizeof(struct ssp_task_iu));
  168. memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
  169. task_iu->task_func = isci_tmf->tmf_code;
  170. task_iu->task_tag =
  171. (ireq->ttype == tmf_task) ?
  172. isci_tmf->io_tag :
  173. SCI_CONTROLLER_INVALID_IO_TAG;
  174. }
  175. /**
  176. * This method is will fill in the SCU Task Context for any type of SSP request.
  177. * @sci_req:
  178. * @task_context:
  179. *
  180. */
  181. static void scu_ssp_reqeust_construct_task_context(
  182. struct scic_sds_request *sds_request,
  183. struct scu_task_context *task_context)
  184. {
  185. dma_addr_t dma_addr;
  186. struct scic_sds_remote_device *target_device;
  187. struct scic_sds_port *target_port;
  188. target_device = scic_sds_request_get_device(sds_request);
  189. target_port = scic_sds_request_get_port(sds_request);
  190. /* Fill in the TC with the its required data */
  191. task_context->abort = 0;
  192. task_context->priority = 0;
  193. task_context->initiator_request = 1;
  194. task_context->connection_rate = target_device->connection_rate;
  195. task_context->protocol_engine_index =
  196. scic_sds_controller_get_protocol_engine_group(controller);
  197. task_context->logical_port_index =
  198. scic_sds_port_get_index(target_port);
  199. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
  200. task_context->valid = SCU_TASK_CONTEXT_VALID;
  201. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  202. task_context->remote_node_index =
  203. scic_sds_remote_device_get_index(sds_request->target_device);
  204. task_context->command_code = 0;
  205. task_context->link_layer_control = 0;
  206. task_context->do_not_dma_ssp_good_response = 1;
  207. task_context->strict_ordering = 0;
  208. task_context->control_frame = 0;
  209. task_context->timeout_enable = 0;
  210. task_context->block_guard_enable = 0;
  211. task_context->address_modifier = 0;
  212. /* task_context->type.ssp.tag = sci_req->io_tag; */
  213. task_context->task_phase = 0x01;
  214. if (sds_request->was_tag_assigned_by_user) {
  215. /*
  216. * Build the task context now since we have already read
  217. * the data
  218. */
  219. sds_request->post_context =
  220. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  221. (scic_sds_controller_get_protocol_engine_group(
  222. controller) <<
  223. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  224. (scic_sds_port_get_index(target_port) <<
  225. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  226. ISCI_TAG_TCI(sds_request->io_tag));
  227. } else {
  228. /*
  229. * Build the task context now since we have already read
  230. * the data
  231. *
  232. * I/O tag index is not assigned because we have to wait
  233. * until we get a TCi
  234. */
  235. sds_request->post_context =
  236. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  237. (scic_sds_controller_get_protocol_engine_group(
  238. owning_controller) <<
  239. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  240. (scic_sds_port_get_index(target_port) <<
  241. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  242. }
  243. /*
  244. * Copy the physical address for the command buffer to the
  245. * SCU Task Context
  246. */
  247. dma_addr = scic_io_request_get_dma_addr(sds_request,
  248. &sds_request->ssp.cmd);
  249. task_context->command_iu_upper = upper_32_bits(dma_addr);
  250. task_context->command_iu_lower = lower_32_bits(dma_addr);
  251. /*
  252. * Copy the physical address for the response buffer to the
  253. * SCU Task Context
  254. */
  255. dma_addr = scic_io_request_get_dma_addr(sds_request,
  256. &sds_request->ssp.rsp);
  257. task_context->response_iu_upper = upper_32_bits(dma_addr);
  258. task_context->response_iu_lower = lower_32_bits(dma_addr);
  259. }
  260. /**
  261. * This method is will fill in the SCU Task Context for a SSP IO request.
  262. * @sci_req:
  263. *
  264. */
  265. static void scu_ssp_io_request_construct_task_context(
  266. struct scic_sds_request *sci_req,
  267. enum dma_data_direction dir,
  268. u32 len)
  269. {
  270. struct scu_task_context *task_context;
  271. task_context = scic_sds_request_get_task_context(sci_req);
  272. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  273. task_context->ssp_command_iu_length =
  274. sizeof(struct ssp_cmd_iu) / sizeof(u32);
  275. task_context->type.ssp.frame_type = SSP_COMMAND;
  276. switch (dir) {
  277. case DMA_FROM_DEVICE:
  278. case DMA_NONE:
  279. default:
  280. task_context->task_type = SCU_TASK_TYPE_IOREAD;
  281. break;
  282. case DMA_TO_DEVICE:
  283. task_context->task_type = SCU_TASK_TYPE_IOWRITE;
  284. break;
  285. }
  286. task_context->transfer_length_bytes = len;
  287. if (task_context->transfer_length_bytes > 0)
  288. scic_sds_request_build_sgl(sci_req);
  289. }
  290. /**
  291. * This method will fill in the SCU Task Context for a SSP Task request. The
  292. * following important settings are utilized: -# priority ==
  293. * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
  294. * ahead of other task destined for the same Remote Node. -# task_type ==
  295. * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
  296. * (i.e. non-raw frame) is being utilized to perform task management. -#
  297. * control_frame == 1. This ensures that the proper endianess is set so
  298. * that the bytes are transmitted in the right order for a task frame.
  299. * @sci_req: This parameter specifies the task request object being
  300. * constructed.
  301. *
  302. */
  303. static void scu_ssp_task_request_construct_task_context(
  304. struct scic_sds_request *sci_req)
  305. {
  306. struct scu_task_context *task_context;
  307. task_context = scic_sds_request_get_task_context(sci_req);
  308. scu_ssp_reqeust_construct_task_context(sci_req, task_context);
  309. task_context->control_frame = 1;
  310. task_context->priority = SCU_TASK_PRIORITY_HIGH;
  311. task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
  312. task_context->transfer_length_bytes = 0;
  313. task_context->type.ssp.frame_type = SSP_TASK;
  314. task_context->ssp_command_iu_length =
  315. sizeof(struct ssp_task_iu) / sizeof(u32);
  316. }
  317. /**
  318. * This method is will fill in the SCU Task Context for any type of SATA
  319. * request. This is called from the various SATA constructors.
  320. * @sci_req: The general IO request object which is to be used in
  321. * constructing the SCU task context.
  322. * @task_context: The buffer pointer for the SCU task context which is being
  323. * constructed.
  324. *
  325. * The general io request construction is complete. The buffer assignment for
  326. * the command buffer is complete. none Revisit task context construction to
  327. * determine what is common for SSP/SMP/STP task context structures.
  328. */
  329. static void scu_sata_reqeust_construct_task_context(
  330. struct scic_sds_request *sci_req,
  331. struct scu_task_context *task_context)
  332. {
  333. dma_addr_t dma_addr;
  334. struct scic_sds_remote_device *target_device;
  335. struct scic_sds_port *target_port;
  336. target_device = scic_sds_request_get_device(sci_req);
  337. target_port = scic_sds_request_get_port(sci_req);
  338. /* Fill in the TC with the its required data */
  339. task_context->abort = 0;
  340. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  341. task_context->initiator_request = 1;
  342. task_context->connection_rate = target_device->connection_rate;
  343. task_context->protocol_engine_index =
  344. scic_sds_controller_get_protocol_engine_group(controller);
  345. task_context->logical_port_index =
  346. scic_sds_port_get_index(target_port);
  347. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
  348. task_context->valid = SCU_TASK_CONTEXT_VALID;
  349. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  350. task_context->remote_node_index =
  351. scic_sds_remote_device_get_index(sci_req->target_device);
  352. task_context->command_code = 0;
  353. task_context->link_layer_control = 0;
  354. task_context->do_not_dma_ssp_good_response = 1;
  355. task_context->strict_ordering = 0;
  356. task_context->control_frame = 0;
  357. task_context->timeout_enable = 0;
  358. task_context->block_guard_enable = 0;
  359. task_context->address_modifier = 0;
  360. task_context->task_phase = 0x01;
  361. task_context->ssp_command_iu_length =
  362. (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
  363. /* Set the first word of the H2D REG FIS */
  364. task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;
  365. if (sci_req->was_tag_assigned_by_user) {
  366. /*
  367. * Build the task context now since we have already read
  368. * the data
  369. */
  370. sci_req->post_context =
  371. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  372. (scic_sds_controller_get_protocol_engine_group(
  373. controller) <<
  374. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  375. (scic_sds_port_get_index(target_port) <<
  376. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  377. ISCI_TAG_TCI(sci_req->io_tag));
  378. } else {
  379. /*
  380. * Build the task context now since we have already read
  381. * the data.
  382. * I/O tag index is not assigned because we have to wait
  383. * until we get a TCi.
  384. */
  385. sci_req->post_context =
  386. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  387. (scic_sds_controller_get_protocol_engine_group(
  388. controller) <<
  389. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  390. (scic_sds_port_get_index(target_port) <<
  391. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  392. }
  393. /*
  394. * Copy the physical address for the command buffer to the SCU Task
  395. * Context. We must offset the command buffer by 4 bytes because the
  396. * first 4 bytes are transfered in the body of the TC.
  397. */
  398. dma_addr = scic_io_request_get_dma_addr(sci_req,
  399. ((char *) &sci_req->stp.cmd) +
  400. sizeof(u32));
  401. task_context->command_iu_upper = upper_32_bits(dma_addr);
  402. task_context->command_iu_lower = lower_32_bits(dma_addr);
  403. /* SATA Requests do not have a response buffer */
  404. task_context->response_iu_upper = 0;
  405. task_context->response_iu_lower = 0;
  406. }
  407. /**
  408. * scu_stp_raw_request_construct_task_context -
  409. * @sci_req: This parameter specifies the STP request object for which to
  410. * construct a RAW command frame task context.
  411. * @task_context: This parameter specifies the SCU specific task context buffer
  412. * to construct.
  413. *
  414. * This method performs the operations common to all SATA/STP requests
  415. * utilizing the raw frame method. none
  416. */
  417. static void scu_stp_raw_request_construct_task_context(struct scic_sds_stp_request *stp_req,
  418. struct scu_task_context *task_context)
  419. {
  420. struct scic_sds_request *sci_req = to_sci_req(stp_req);
  421. scu_sata_reqeust_construct_task_context(sci_req, task_context);
  422. task_context->control_frame = 0;
  423. task_context->priority = SCU_TASK_PRIORITY_NORMAL;
  424. task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
  425. task_context->type.stp.fis_type = FIS_REGH2D;
  426. task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
  427. }
  428. static enum sci_status
  429. scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
  430. bool copy_rx_frame)
  431. {
  432. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  433. struct scic_sds_stp_pio_request *pio = &stp_req->type.pio;
  434. scu_stp_raw_request_construct_task_context(stp_req,
  435. sci_req->task_context_buffer);
  436. pio->current_transfer_bytes = 0;
  437. pio->ending_error = 0;
  438. pio->ending_status = 0;
  439. pio->request_current.sgl_offset = 0;
  440. pio->request_current.sgl_set = SCU_SGL_ELEMENT_PAIR_A;
  441. if (copy_rx_frame) {
  442. scic_sds_request_build_sgl(sci_req);
  443. /* Since the IO request copy of the TC contains the same data as
  444. * the actual TC this pointer is vaild for either.
  445. */
  446. pio->request_current.sgl_pair = &sci_req->task_context_buffer->sgl_pair_ab;
  447. } else {
  448. /* The user does not want the data copied to the SGL buffer location */
  449. pio->request_current.sgl_pair = NULL;
  450. }
  451. return SCI_SUCCESS;
  452. }
  453. /**
  454. *
  455. * @sci_req: This parameter specifies the request to be constructed as an
  456. * optimized request.
  457. * @optimized_task_type: This parameter specifies whether the request is to be
  458. * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
  459. * value of 1 indicates NCQ.
  460. *
  461. * This method will perform request construction common to all types of STP
  462. * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
  463. * returns an indication as to whether the construction was successful.
  464. */
  465. static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
  466. u8 optimized_task_type,
  467. u32 len,
  468. enum dma_data_direction dir)
  469. {
  470. struct scu_task_context *task_context = sci_req->task_context_buffer;
  471. /* Build the STP task context structure */
  472. scu_sata_reqeust_construct_task_context(sci_req, task_context);
  473. /* Copy over the SGL elements */
  474. scic_sds_request_build_sgl(sci_req);
  475. /* Copy over the number of bytes to be transfered */
  476. task_context->transfer_length_bytes = len;
  477. if (dir == DMA_TO_DEVICE) {
  478. /*
  479. * The difference between the DMA IN and DMA OUT request task type
  480. * values are consistent with the difference between FPDMA READ
  481. * and FPDMA WRITE values. Add the supplied task type parameter
  482. * to this difference to set the task type properly for this
  483. * DATA OUT (WRITE) case. */
  484. task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
  485. - SCU_TASK_TYPE_DMA_IN);
  486. } else {
  487. /*
  488. * For the DATA IN (READ) case, simply save the supplied
  489. * optimized task type. */
  490. task_context->task_type = optimized_task_type;
  491. }
  492. }
  493. static enum sci_status
  494. scic_io_request_construct_sata(struct scic_sds_request *sci_req,
  495. u32 len,
  496. enum dma_data_direction dir,
  497. bool copy)
  498. {
  499. enum sci_status status = SCI_SUCCESS;
  500. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  501. struct sas_task *task = isci_request_access_task(ireq);
  502. /* check for management protocols */
  503. if (ireq->ttype == tmf_task) {
  504. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  505. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  506. tmf->tmf_code == isci_tmf_sata_srst_low) {
  507. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  508. sci_req->task_context_buffer);
  509. return SCI_SUCCESS;
  510. } else {
  511. dev_err(scic_to_dev(sci_req->owning_controller),
  512. "%s: Request 0x%p received un-handled SAT "
  513. "management protocol 0x%x.\n",
  514. __func__, sci_req, tmf->tmf_code);
  515. return SCI_FAILURE;
  516. }
  517. }
  518. if (!sas_protocol_ata(task->task_proto)) {
  519. dev_err(scic_to_dev(sci_req->owning_controller),
  520. "%s: Non-ATA protocol in SATA path: 0x%x\n",
  521. __func__,
  522. task->task_proto);
  523. return SCI_FAILURE;
  524. }
  525. /* non data */
  526. if (task->data_dir == DMA_NONE) {
  527. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  528. sci_req->task_context_buffer);
  529. return SCI_SUCCESS;
  530. }
  531. /* NCQ */
  532. if (task->ata_task.use_ncq) {
  533. scic_sds_stp_optimized_request_construct(sci_req,
  534. SCU_TASK_TYPE_FPDMAQ_READ,
  535. len, dir);
  536. return SCI_SUCCESS;
  537. }
  538. /* DMA */
  539. if (task->ata_task.dma_xfer) {
  540. scic_sds_stp_optimized_request_construct(sci_req,
  541. SCU_TASK_TYPE_DMA_IN,
  542. len, dir);
  543. return SCI_SUCCESS;
  544. } else /* PIO */
  545. return scic_sds_stp_pio_request_construct(sci_req, copy);
  546. return status;
  547. }
  548. static enum sci_status scic_io_request_construct_basic_ssp(struct scic_sds_request *sci_req)
  549. {
  550. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  551. struct sas_task *task = isci_request_access_task(ireq);
  552. sci_req->protocol = SCIC_SSP_PROTOCOL;
  553. scu_ssp_io_request_construct_task_context(sci_req,
  554. task->data_dir,
  555. task->total_xfer_len);
  556. scic_sds_io_request_build_ssp_command_iu(sci_req);
  557. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  558. return SCI_SUCCESS;
  559. }
  560. enum sci_status scic_task_request_construct_ssp(
  561. struct scic_sds_request *sci_req)
  562. {
  563. /* Construct the SSP Task SCU Task Context */
  564. scu_ssp_task_request_construct_task_context(sci_req);
  565. /* Fill in the SSP Task IU */
  566. scic_sds_task_request_build_ssp_task_iu(sci_req);
  567. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  568. return SCI_SUCCESS;
  569. }
  570. static enum sci_status scic_io_request_construct_basic_sata(struct scic_sds_request *sci_req)
  571. {
  572. enum sci_status status;
  573. bool copy = false;
  574. struct isci_request *isci_request = sci_req_to_ireq(sci_req);
  575. struct sas_task *task = isci_request_access_task(isci_request);
  576. sci_req->protocol = SCIC_STP_PROTOCOL;
  577. copy = (task->data_dir == DMA_NONE) ? false : true;
  578. status = scic_io_request_construct_sata(sci_req,
  579. task->total_xfer_len,
  580. task->data_dir,
  581. copy);
  582. if (status == SCI_SUCCESS)
  583. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  584. return status;
  585. }
  586. enum sci_status scic_task_request_construct_sata(struct scic_sds_request *sci_req)
  587. {
  588. enum sci_status status = SCI_SUCCESS;
  589. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  590. /* check for management protocols */
  591. if (ireq->ttype == tmf_task) {
  592. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  593. if (tmf->tmf_code == isci_tmf_sata_srst_high ||
  594. tmf->tmf_code == isci_tmf_sata_srst_low) {
  595. scu_stp_raw_request_construct_task_context(&sci_req->stp.req,
  596. sci_req->task_context_buffer);
  597. } else {
  598. dev_err(scic_to_dev(sci_req->owning_controller),
  599. "%s: Request 0x%p received un-handled SAT "
  600. "Protocol 0x%x.\n",
  601. __func__, sci_req, tmf->tmf_code);
  602. return SCI_FAILURE;
  603. }
  604. }
  605. if (status != SCI_SUCCESS)
  606. return status;
  607. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  608. return status;
  609. }
  610. /**
  611. * sci_req_tx_bytes - bytes transferred when reply underruns request
  612. * @sci_req: request that was terminated early
  613. */
  614. #define SCU_TASK_CONTEXT_SRAM 0x200000
  615. static u32 sci_req_tx_bytes(struct scic_sds_request *sci_req)
  616. {
  617. struct scic_sds_controller *scic = sci_req->owning_controller;
  618. u32 ret_val = 0;
  619. if (readl(&scic->smu_registers->address_modifier) == 0) {
  620. void __iomem *scu_reg_base = scic->scu_registers;
  621. /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
  622. * BAR1 is the scu_registers
  623. * 0x20002C = 0x200000 + 0x2c
  624. * = start of task context SRAM + offset of (type.ssp.data_offset)
  625. * TCi is the io_tag of struct scic_sds_request
  626. */
  627. ret_val = readl(scu_reg_base +
  628. (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
  629. ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(sci_req->io_tag)));
  630. }
  631. return ret_val;
  632. }
  633. enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req)
  634. {
  635. struct scic_sds_controller *scic = sci_req->owning_controller;
  636. struct scu_task_context *task_context;
  637. enum sci_base_request_states state;
  638. state = sci_req->sm.current_state_id;
  639. if (state != SCI_REQ_CONSTRUCTED) {
  640. dev_warn(scic_to_dev(scic),
  641. "%s: SCIC IO Request requested to start while in wrong "
  642. "state %d\n", __func__, state);
  643. return SCI_FAILURE_INVALID_STATE;
  644. }
  645. /* if necessary, allocate a TCi for the io request object and then will,
  646. * if necessary, copy the constructed TC data into the actual TC buffer.
  647. * If everything is successful the post context field is updated with
  648. * the TCi so the controller can post the request to the hardware.
  649. */
  650. if (sci_req->io_tag == SCI_CONTROLLER_INVALID_IO_TAG)
  651. sci_req->io_tag = scic_controller_allocate_io_tag(scic);
  652. /* Record the IO Tag in the request */
  653. if (sci_req->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  654. task_context = sci_req->task_context_buffer;
  655. task_context->task_index = ISCI_TAG_TCI(sci_req->io_tag);
  656. switch (task_context->protocol_type) {
  657. case SCU_TASK_CONTEXT_PROTOCOL_SMP:
  658. case SCU_TASK_CONTEXT_PROTOCOL_SSP:
  659. /* SSP/SMP Frame */
  660. task_context->type.ssp.tag = sci_req->io_tag;
  661. task_context->type.ssp.target_port_transfer_tag =
  662. 0xFFFF;
  663. break;
  664. case SCU_TASK_CONTEXT_PROTOCOL_STP:
  665. /* STP/SATA Frame
  666. * task_context->type.stp.ncq_tag = sci_req->ncq_tag;
  667. */
  668. break;
  669. case SCU_TASK_CONTEXT_PROTOCOL_NONE:
  670. /* / @todo When do we set no protocol type? */
  671. break;
  672. default:
  673. /* This should never happen since we build the IO
  674. * requests */
  675. break;
  676. }
  677. /*
  678. * Check to see if we need to copy the task context buffer
  679. * or have been building into the task context buffer */
  680. if (sci_req->was_tag_assigned_by_user == false)
  681. scic_sds_controller_copy_task_context(scic, sci_req);
  682. /* Add to the post_context the io tag value */
  683. sci_req->post_context |= ISCI_TAG_TCI(sci_req->io_tag);
  684. /* Everything is good go ahead and change state */
  685. sci_change_state(&sci_req->sm, SCI_REQ_STARTED);
  686. return SCI_SUCCESS;
  687. }
  688. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  689. }
  690. enum sci_status
  691. scic_sds_io_request_terminate(struct scic_sds_request *sci_req)
  692. {
  693. enum sci_base_request_states state;
  694. state = sci_req->sm.current_state_id;
  695. switch (state) {
  696. case SCI_REQ_CONSTRUCTED:
  697. scic_sds_request_set_status(sci_req,
  698. SCU_TASK_DONE_TASK_ABORT,
  699. SCI_FAILURE_IO_TERMINATED);
  700. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  701. return SCI_SUCCESS;
  702. case SCI_REQ_STARTED:
  703. case SCI_REQ_TASK_WAIT_TC_COMP:
  704. case SCI_REQ_SMP_WAIT_RESP:
  705. case SCI_REQ_SMP_WAIT_TC_COMP:
  706. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  707. case SCI_REQ_STP_UDMA_WAIT_D2H:
  708. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  709. case SCI_REQ_STP_NON_DATA_WAIT_D2H:
  710. case SCI_REQ_STP_PIO_WAIT_H2D:
  711. case SCI_REQ_STP_PIO_WAIT_FRAME:
  712. case SCI_REQ_STP_PIO_DATA_IN:
  713. case SCI_REQ_STP_PIO_DATA_OUT:
  714. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  715. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  716. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H:
  717. sci_change_state(&sci_req->sm, SCI_REQ_ABORTING);
  718. return SCI_SUCCESS;
  719. case SCI_REQ_TASK_WAIT_TC_RESP:
  720. sci_change_state(&sci_req->sm, SCI_REQ_ABORTING);
  721. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  722. return SCI_SUCCESS;
  723. case SCI_REQ_ABORTING:
  724. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  725. return SCI_SUCCESS;
  726. case SCI_REQ_COMPLETED:
  727. default:
  728. dev_warn(scic_to_dev(sci_req->owning_controller),
  729. "%s: SCIC IO Request requested to abort while in wrong "
  730. "state %d\n",
  731. __func__,
  732. sci_req->sm.current_state_id);
  733. break;
  734. }
  735. return SCI_FAILURE_INVALID_STATE;
  736. }
  737. enum sci_status scic_sds_request_complete(struct scic_sds_request *sci_req)
  738. {
  739. enum sci_base_request_states state;
  740. struct scic_sds_controller *scic = sci_req->owning_controller;
  741. state = sci_req->sm.current_state_id;
  742. if (WARN_ONCE(state != SCI_REQ_COMPLETED,
  743. "isci: request completion from wrong state (%d)\n", state))
  744. return SCI_FAILURE_INVALID_STATE;
  745. if (!sci_req->was_tag_assigned_by_user)
  746. scic_controller_free_io_tag(scic, sci_req->io_tag);
  747. if (sci_req->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
  748. scic_sds_controller_release_frame(scic,
  749. sci_req->saved_rx_frame_index);
  750. /* XXX can we just stop the machine and remove the 'final' state? */
  751. sci_change_state(&sci_req->sm, SCI_REQ_FINAL);
  752. return SCI_SUCCESS;
  753. }
  754. enum sci_status scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
  755. u32 event_code)
  756. {
  757. enum sci_base_request_states state;
  758. struct scic_sds_controller *scic = sci_req->owning_controller;
  759. state = sci_req->sm.current_state_id;
  760. if (state != SCI_REQ_STP_PIO_DATA_IN) {
  761. dev_warn(scic_to_dev(scic), "%s: (%x) in wrong state %d\n",
  762. __func__, event_code, state);
  763. return SCI_FAILURE_INVALID_STATE;
  764. }
  765. switch (scu_get_event_specifier(event_code)) {
  766. case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
  767. /* We are waiting for data and the SCU has R_ERR the data frame.
  768. * Go back to waiting for the D2H Register FIS
  769. */
  770. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  771. return SCI_SUCCESS;
  772. default:
  773. dev_err(scic_to_dev(scic),
  774. "%s: pio request unexpected event %#x\n",
  775. __func__, event_code);
  776. /* TODO Should we fail the PIO request when we get an
  777. * unexpected event?
  778. */
  779. return SCI_FAILURE;
  780. }
  781. }
  782. /*
  783. * This function copies response data for requests returning response data
  784. * instead of sense data.
  785. * @sci_req: This parameter specifies the request object for which to copy
  786. * the response data.
  787. */
  788. static void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
  789. {
  790. void *resp_buf;
  791. u32 len;
  792. struct ssp_response_iu *ssp_response;
  793. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  794. struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
  795. ssp_response = &sci_req->ssp.rsp;
  796. resp_buf = &isci_tmf->resp.resp_iu;
  797. len = min_t(u32,
  798. SSP_RESP_IU_MAX_SIZE,
  799. be32_to_cpu(ssp_response->response_data_len));
  800. memcpy(resp_buf, ssp_response->resp_data, len);
  801. }
  802. static enum sci_status
  803. request_started_state_tc_event(struct scic_sds_request *sci_req,
  804. u32 completion_code)
  805. {
  806. struct ssp_response_iu *resp_iu;
  807. u8 datapres;
  808. /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
  809. * to determine SDMA status
  810. */
  811. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  812. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  813. scic_sds_request_set_status(sci_req,
  814. SCU_TASK_DONE_GOOD,
  815. SCI_SUCCESS);
  816. break;
  817. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
  818. /* There are times when the SCU hardware will return an early
  819. * response because the io request specified more data than is
  820. * returned by the target device (mode pages, inquiry data,
  821. * etc.). We must check the response stats to see if this is
  822. * truly a failed request or a good request that just got
  823. * completed early.
  824. */
  825. struct ssp_response_iu *resp = &sci_req->ssp.rsp;
  826. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  827. sci_swab32_cpy(&sci_req->ssp.rsp,
  828. &sci_req->ssp.rsp,
  829. word_cnt);
  830. if (resp->status == 0) {
  831. scic_sds_request_set_status(sci_req,
  832. SCU_TASK_DONE_GOOD,
  833. SCI_SUCCESS_IO_DONE_EARLY);
  834. } else {
  835. scic_sds_request_set_status(sci_req,
  836. SCU_TASK_DONE_CHECK_RESPONSE,
  837. SCI_FAILURE_IO_RESPONSE_VALID);
  838. }
  839. break;
  840. }
  841. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
  842. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  843. sci_swab32_cpy(&sci_req->ssp.rsp,
  844. &sci_req->ssp.rsp,
  845. word_cnt);
  846. scic_sds_request_set_status(sci_req,
  847. SCU_TASK_DONE_CHECK_RESPONSE,
  848. SCI_FAILURE_IO_RESPONSE_VALID);
  849. break;
  850. }
  851. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
  852. /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
  853. * guaranteed to be received before this completion status is
  854. * posted?
  855. */
  856. resp_iu = &sci_req->ssp.rsp;
  857. datapres = resp_iu->datapres;
  858. if (datapres == 1 || datapres == 2) {
  859. scic_sds_request_set_status(sci_req,
  860. SCU_TASK_DONE_CHECK_RESPONSE,
  861. SCI_FAILURE_IO_RESPONSE_VALID);
  862. } else
  863. scic_sds_request_set_status(sci_req,
  864. SCU_TASK_DONE_GOOD,
  865. SCI_SUCCESS);
  866. break;
  867. /* only stp device gets suspended. */
  868. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  869. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
  870. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
  871. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
  872. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
  873. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
  874. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  875. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
  876. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
  877. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  878. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
  879. if (sci_req->protocol == SCIC_STP_PROTOCOL) {
  880. scic_sds_request_set_status(sci_req,
  881. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  882. SCU_COMPLETION_TL_STATUS_SHIFT,
  883. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  884. } else {
  885. scic_sds_request_set_status(sci_req,
  886. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  887. SCU_COMPLETION_TL_STATUS_SHIFT,
  888. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  889. }
  890. break;
  891. /* both stp/ssp device gets suspended */
  892. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
  893. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
  894. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
  895. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
  896. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
  897. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
  898. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
  899. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
  900. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
  901. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
  902. scic_sds_request_set_status(sci_req,
  903. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  904. SCU_COMPLETION_TL_STATUS_SHIFT,
  905. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
  906. break;
  907. /* neither ssp nor stp gets suspended. */
  908. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
  909. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
  910. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
  911. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
  912. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
  913. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
  914. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  915. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  916. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  917. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  918. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
  919. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
  920. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
  921. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
  922. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
  923. default:
  924. scic_sds_request_set_status(
  925. sci_req,
  926. SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
  927. SCU_COMPLETION_TL_STATUS_SHIFT,
  928. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  929. break;
  930. }
  931. /*
  932. * TODO: This is probably wrong for ACK/NAK timeout conditions
  933. */
  934. /* In all cases we will treat this as the completion of the IO req. */
  935. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  936. return SCI_SUCCESS;
  937. }
  938. static enum sci_status
  939. request_aborting_state_tc_event(struct scic_sds_request *sci_req,
  940. u32 completion_code)
  941. {
  942. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  943. case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
  944. case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
  945. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_TASK_ABORT,
  946. SCI_FAILURE_IO_TERMINATED);
  947. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  948. break;
  949. default:
  950. /* Unless we get some strange error wait for the task abort to complete
  951. * TODO: Should there be a state change for this completion?
  952. */
  953. break;
  954. }
  955. return SCI_SUCCESS;
  956. }
  957. static enum sci_status ssp_task_request_await_tc_event(struct scic_sds_request *sci_req,
  958. u32 completion_code)
  959. {
  960. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  961. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  962. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  963. SCI_SUCCESS);
  964. sci_change_state(&sci_req->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  965. break;
  966. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
  967. /* Currently, the decision is to simply allow the task request
  968. * to timeout if the task IU wasn't received successfully.
  969. * There is a potential for receiving multiple task responses if
  970. * we decide to send the task IU again.
  971. */
  972. dev_warn(scic_to_dev(sci_req->owning_controller),
  973. "%s: TaskRequest:0x%p CompletionCode:%x - "
  974. "ACK/NAK timeout\n", __func__, sci_req,
  975. completion_code);
  976. sci_change_state(&sci_req->sm, SCI_REQ_TASK_WAIT_TC_RESP);
  977. break;
  978. default:
  979. /*
  980. * All other completion status cause the IO to be complete.
  981. * If a NAK was received, then it is up to the user to retry
  982. * the request.
  983. */
  984. scic_sds_request_set_status(sci_req,
  985. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  986. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  987. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  988. break;
  989. }
  990. return SCI_SUCCESS;
  991. }
  992. static enum sci_status
  993. smp_request_await_response_tc_event(struct scic_sds_request *sci_req,
  994. u32 completion_code)
  995. {
  996. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  997. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  998. /* In the AWAIT RESPONSE state, any TC completion is
  999. * unexpected. but if the TC has success status, we
  1000. * complete the IO anyway.
  1001. */
  1002. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1003. SCI_SUCCESS);
  1004. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1005. break;
  1006. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
  1007. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
  1008. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
  1009. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
  1010. /* These status has been seen in a specific LSI
  1011. * expander, which sometimes is not able to send smp
  1012. * response within 2 ms. This causes our hardware break
  1013. * the connection and set TC completion with one of
  1014. * these SMP_XXX_XX_ERR status. For these type of error,
  1015. * we ask scic user to retry the request.
  1016. */
  1017. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_SMP_RESP_TO_ERR,
  1018. SCI_FAILURE_RETRY_REQUIRED);
  1019. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1020. break;
  1021. default:
  1022. /* All other completion status cause the IO to be complete. If a NAK
  1023. * was received, then it is up to the user to retry the request
  1024. */
  1025. scic_sds_request_set_status(sci_req,
  1026. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1027. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1028. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1029. break;
  1030. }
  1031. return SCI_SUCCESS;
  1032. }
  1033. static enum sci_status
  1034. smp_request_await_tc_event(struct scic_sds_request *sci_req,
  1035. u32 completion_code)
  1036. {
  1037. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1038. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1039. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1040. SCI_SUCCESS);
  1041. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1042. break;
  1043. default:
  1044. /* All other completion status cause the IO to be
  1045. * complete. If a NAK was received, then it is up to
  1046. * the user to retry the request.
  1047. */
  1048. scic_sds_request_set_status(sci_req,
  1049. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1050. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1051. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1052. break;
  1053. }
  1054. return SCI_SUCCESS;
  1055. }
  1056. void scic_stp_io_request_set_ncq_tag(struct scic_sds_request *req,
  1057. u16 ncq_tag)
  1058. {
  1059. /**
  1060. * @note This could be made to return an error to the user if the user
  1061. * attempts to set the NCQ tag in the wrong state.
  1062. */
  1063. req->task_context_buffer->type.stp.ncq_tag = ncq_tag;
  1064. }
  1065. /**
  1066. *
  1067. * @sci_req:
  1068. *
  1069. * Get the next SGL element from the request. - Check on which SGL element pair
  1070. * we are working - if working on SLG pair element A - advance to element B -
  1071. * else - check to see if there are more SGL element pairs for this IO request
  1072. * - if there are more SGL element pairs - advance to the next pair and return
  1073. * element A struct scu_sgl_element*
  1074. */
  1075. static struct scu_sgl_element *scic_sds_stp_request_pio_get_next_sgl(struct scic_sds_stp_request *stp_req)
  1076. {
  1077. struct scu_sgl_element *current_sgl;
  1078. struct scic_sds_request *sci_req = to_sci_req(stp_req);
  1079. struct scic_sds_request_pio_sgl *pio_sgl = &stp_req->type.pio.request_current;
  1080. if (pio_sgl->sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
  1081. if (pio_sgl->sgl_pair->B.address_lower == 0 &&
  1082. pio_sgl->sgl_pair->B.address_upper == 0) {
  1083. current_sgl = NULL;
  1084. } else {
  1085. pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_B;
  1086. current_sgl = &pio_sgl->sgl_pair->B;
  1087. }
  1088. } else {
  1089. if (pio_sgl->sgl_pair->next_pair_lower == 0 &&
  1090. pio_sgl->sgl_pair->next_pair_upper == 0) {
  1091. current_sgl = NULL;
  1092. } else {
  1093. u64 phys_addr;
  1094. phys_addr = pio_sgl->sgl_pair->next_pair_upper;
  1095. phys_addr <<= 32;
  1096. phys_addr |= pio_sgl->sgl_pair->next_pair_lower;
  1097. pio_sgl->sgl_pair = scic_request_get_virt_addr(sci_req, phys_addr);
  1098. pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_A;
  1099. current_sgl = &pio_sgl->sgl_pair->A;
  1100. }
  1101. }
  1102. return current_sgl;
  1103. }
  1104. static enum sci_status
  1105. stp_request_non_data_await_h2d_tc_event(struct scic_sds_request *sci_req,
  1106. u32 completion_code)
  1107. {
  1108. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1109. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1110. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1111. SCI_SUCCESS);
  1112. sci_change_state(&sci_req->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
  1113. break;
  1114. default:
  1115. /* All other completion status cause the IO to be
  1116. * complete. If a NAK was received, then it is up to
  1117. * the user to retry the request.
  1118. */
  1119. scic_sds_request_set_status(sci_req,
  1120. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1121. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1122. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1123. break;
  1124. }
  1125. return SCI_SUCCESS;
  1126. }
  1127. #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
  1128. /* transmit DATA_FIS from (current sgl + offset) for input
  1129. * parameter length. current sgl and offset is alreay stored in the IO request
  1130. */
  1131. static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
  1132. struct scic_sds_request *sci_req,
  1133. u32 length)
  1134. {
  1135. struct scic_sds_controller *scic = sci_req->owning_controller;
  1136. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1137. struct scu_task_context *task_context;
  1138. struct scu_sgl_element *current_sgl;
  1139. /* Recycle the TC and reconstruct it for sending out DATA FIS containing
  1140. * for the data from current_sgl+offset for the input length
  1141. */
  1142. task_context = scic_sds_controller_get_task_context_buffer(scic,
  1143. sci_req->io_tag);
  1144. if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A)
  1145. current_sgl = &stp_req->type.pio.request_current.sgl_pair->A;
  1146. else
  1147. current_sgl = &stp_req->type.pio.request_current.sgl_pair->B;
  1148. /* update the TC */
  1149. task_context->command_iu_upper = current_sgl->address_upper;
  1150. task_context->command_iu_lower = current_sgl->address_lower;
  1151. task_context->transfer_length_bytes = length;
  1152. task_context->type.stp.fis_type = FIS_DATA;
  1153. /* send the new TC out. */
  1154. return scic_controller_continue_io(sci_req);
  1155. }
  1156. static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
  1157. {
  1158. struct scu_sgl_element *current_sgl;
  1159. u32 sgl_offset;
  1160. u32 remaining_bytes_in_current_sgl = 0;
  1161. enum sci_status status = SCI_SUCCESS;
  1162. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1163. sgl_offset = stp_req->type.pio.request_current.sgl_offset;
  1164. if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
  1165. current_sgl = &(stp_req->type.pio.request_current.sgl_pair->A);
  1166. remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->A.length - sgl_offset;
  1167. } else {
  1168. current_sgl = &(stp_req->type.pio.request_current.sgl_pair->B);
  1169. remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->B.length - sgl_offset;
  1170. }
  1171. if (stp_req->type.pio.pio_transfer_bytes > 0) {
  1172. if (stp_req->type.pio.pio_transfer_bytes >= remaining_bytes_in_current_sgl) {
  1173. /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = remaining_bytes_in_current_sgl */
  1174. status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, remaining_bytes_in_current_sgl);
  1175. if (status == SCI_SUCCESS) {
  1176. stp_req->type.pio.pio_transfer_bytes -= remaining_bytes_in_current_sgl;
  1177. /* update the current sgl, sgl_offset and save for future */
  1178. current_sgl = scic_sds_stp_request_pio_get_next_sgl(stp_req);
  1179. sgl_offset = 0;
  1180. }
  1181. } else if (stp_req->type.pio.pio_transfer_bytes < remaining_bytes_in_current_sgl) {
  1182. /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = type.pio.pio_transfer_bytes */
  1183. scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->type.pio.pio_transfer_bytes);
  1184. if (status == SCI_SUCCESS) {
  1185. /* Sgl offset will be adjusted and saved for future */
  1186. sgl_offset += stp_req->type.pio.pio_transfer_bytes;
  1187. current_sgl->address_lower += stp_req->type.pio.pio_transfer_bytes;
  1188. stp_req->type.pio.pio_transfer_bytes = 0;
  1189. }
  1190. }
  1191. }
  1192. if (status == SCI_SUCCESS) {
  1193. stp_req->type.pio.request_current.sgl_offset = sgl_offset;
  1194. }
  1195. return status;
  1196. }
  1197. /**
  1198. *
  1199. * @stp_request: The request that is used for the SGL processing.
  1200. * @data_buffer: The buffer of data to be copied.
  1201. * @length: The length of the data transfer.
  1202. *
  1203. * Copy the data from the buffer for the length specified to the IO reqeust SGL
  1204. * specified data region. enum sci_status
  1205. */
  1206. static enum sci_status
  1207. scic_sds_stp_request_pio_data_in_copy_data_buffer(struct scic_sds_stp_request *stp_req,
  1208. u8 *data_buf, u32 len)
  1209. {
  1210. struct scic_sds_request *sci_req;
  1211. struct isci_request *ireq;
  1212. u8 *src_addr;
  1213. int copy_len;
  1214. struct sas_task *task;
  1215. struct scatterlist *sg;
  1216. void *kaddr;
  1217. int total_len = len;
  1218. sci_req = to_sci_req(stp_req);
  1219. ireq = sci_req_to_ireq(sci_req);
  1220. task = isci_request_access_task(ireq);
  1221. src_addr = data_buf;
  1222. if (task->num_scatter > 0) {
  1223. sg = task->scatter;
  1224. while (total_len > 0) {
  1225. struct page *page = sg_page(sg);
  1226. copy_len = min_t(int, total_len, sg_dma_len(sg));
  1227. kaddr = kmap_atomic(page, KM_IRQ0);
  1228. memcpy(kaddr + sg->offset, src_addr, copy_len);
  1229. kunmap_atomic(kaddr, KM_IRQ0);
  1230. total_len -= copy_len;
  1231. src_addr += copy_len;
  1232. sg = sg_next(sg);
  1233. }
  1234. } else {
  1235. BUG_ON(task->total_xfer_len < total_len);
  1236. memcpy(task->scatter, src_addr, total_len);
  1237. }
  1238. return SCI_SUCCESS;
  1239. }
  1240. /**
  1241. *
  1242. * @sci_req: The PIO DATA IN request that is to receive the data.
  1243. * @data_buffer: The buffer to copy from.
  1244. *
  1245. * Copy the data buffer to the io request data region. enum sci_status
  1246. */
  1247. static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
  1248. struct scic_sds_stp_request *sci_req,
  1249. u8 *data_buffer)
  1250. {
  1251. enum sci_status status;
  1252. /*
  1253. * If there is less than 1K remaining in the transfer request
  1254. * copy just the data for the transfer */
  1255. if (sci_req->type.pio.pio_transfer_bytes < SCU_MAX_FRAME_BUFFER_SIZE) {
  1256. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1257. sci_req, data_buffer, sci_req->type.pio.pio_transfer_bytes);
  1258. if (status == SCI_SUCCESS)
  1259. sci_req->type.pio.pio_transfer_bytes = 0;
  1260. } else {
  1261. /* We are transfering the whole frame so copy */
  1262. status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
  1263. sci_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
  1264. if (status == SCI_SUCCESS)
  1265. sci_req->type.pio.pio_transfer_bytes -= SCU_MAX_FRAME_BUFFER_SIZE;
  1266. }
  1267. return status;
  1268. }
  1269. static enum sci_status
  1270. stp_request_pio_await_h2d_completion_tc_event(struct scic_sds_request *sci_req,
  1271. u32 completion_code)
  1272. {
  1273. enum sci_status status = SCI_SUCCESS;
  1274. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1275. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1276. scic_sds_request_set_status(sci_req,
  1277. SCU_TASK_DONE_GOOD,
  1278. SCI_SUCCESS);
  1279. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1280. break;
  1281. default:
  1282. /* All other completion status cause the IO to be
  1283. * complete. If a NAK was received, then it is up to
  1284. * the user to retry the request.
  1285. */
  1286. scic_sds_request_set_status(sci_req,
  1287. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1288. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1289. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1290. break;
  1291. }
  1292. return status;
  1293. }
  1294. static enum sci_status
  1295. pio_data_out_tx_done_tc_event(struct scic_sds_request *sci_req,
  1296. u32 completion_code)
  1297. {
  1298. enum sci_status status = SCI_SUCCESS;
  1299. bool all_frames_transferred = false;
  1300. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1301. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1302. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1303. /* Transmit data */
  1304. if (stp_req->type.pio.pio_transfer_bytes != 0) {
  1305. status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
  1306. if (status == SCI_SUCCESS) {
  1307. if (stp_req->type.pio.pio_transfer_bytes == 0)
  1308. all_frames_transferred = true;
  1309. }
  1310. } else if (stp_req->type.pio.pio_transfer_bytes == 0) {
  1311. /*
  1312. * this will happen if the all data is written at the
  1313. * first time after the pio setup fis is received
  1314. */
  1315. all_frames_transferred = true;
  1316. }
  1317. /* all data transferred. */
  1318. if (all_frames_transferred) {
  1319. /*
  1320. * Change the state to SCI_REQ_STP_PIO_DATA_IN
  1321. * and wait for PIO_SETUP fis / or D2H REg fis. */
  1322. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1323. }
  1324. break;
  1325. default:
  1326. /*
  1327. * All other completion status cause the IO to be complete.
  1328. * If a NAK was received, then it is up to the user to retry
  1329. * the request.
  1330. */
  1331. scic_sds_request_set_status(
  1332. sci_req,
  1333. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1334. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1335. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1336. break;
  1337. }
  1338. return status;
  1339. }
  1340. static void scic_sds_stp_request_udma_complete_request(
  1341. struct scic_sds_request *request,
  1342. u32 scu_status,
  1343. enum sci_status sci_status)
  1344. {
  1345. scic_sds_request_set_status(request, scu_status, sci_status);
  1346. sci_change_state(&request->sm, SCI_REQ_COMPLETED);
  1347. }
  1348. static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
  1349. u32 frame_index)
  1350. {
  1351. struct scic_sds_controller *scic = sci_req->owning_controller;
  1352. struct dev_to_host_fis *frame_header;
  1353. enum sci_status status;
  1354. u32 *frame_buffer;
  1355. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1356. frame_index,
  1357. (void **)&frame_header);
  1358. if ((status == SCI_SUCCESS) &&
  1359. (frame_header->fis_type == FIS_REGD2H)) {
  1360. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1361. frame_index,
  1362. (void **)&frame_buffer);
  1363. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1364. frame_header,
  1365. frame_buffer);
  1366. }
  1367. scic_sds_controller_release_frame(scic, frame_index);
  1368. return status;
  1369. }
  1370. enum sci_status
  1371. scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
  1372. u32 frame_index)
  1373. {
  1374. struct scic_sds_controller *scic = sci_req->owning_controller;
  1375. struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
  1376. enum sci_base_request_states state;
  1377. enum sci_status status;
  1378. ssize_t word_cnt;
  1379. state = sci_req->sm.current_state_id;
  1380. switch (state) {
  1381. case SCI_REQ_STARTED: {
  1382. struct ssp_frame_hdr ssp_hdr;
  1383. void *frame_header;
  1384. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1385. frame_index,
  1386. &frame_header);
  1387. word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
  1388. sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
  1389. if (ssp_hdr.frame_type == SSP_RESPONSE) {
  1390. struct ssp_response_iu *resp_iu;
  1391. ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
  1392. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1393. frame_index,
  1394. (void **)&resp_iu);
  1395. sci_swab32_cpy(&sci_req->ssp.rsp, resp_iu, word_cnt);
  1396. resp_iu = &sci_req->ssp.rsp;
  1397. if (resp_iu->datapres == 0x01 ||
  1398. resp_iu->datapres == 0x02) {
  1399. scic_sds_request_set_status(sci_req,
  1400. SCU_TASK_DONE_CHECK_RESPONSE,
  1401. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1402. } else
  1403. scic_sds_request_set_status(sci_req,
  1404. SCU_TASK_DONE_GOOD,
  1405. SCI_SUCCESS);
  1406. } else {
  1407. /* not a response frame, why did it get forwarded? */
  1408. dev_err(scic_to_dev(scic),
  1409. "%s: SCIC IO Request 0x%p received unexpected "
  1410. "frame %d type 0x%02x\n", __func__, sci_req,
  1411. frame_index, ssp_hdr.frame_type);
  1412. }
  1413. /*
  1414. * In any case we are done with this frame buffer return it to
  1415. * the controller
  1416. */
  1417. scic_sds_controller_release_frame(scic, frame_index);
  1418. return SCI_SUCCESS;
  1419. }
  1420. case SCI_REQ_TASK_WAIT_TC_RESP:
  1421. scic_sds_io_request_copy_response(sci_req);
  1422. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1423. scic_sds_controller_release_frame(scic,frame_index);
  1424. return SCI_SUCCESS;
  1425. case SCI_REQ_SMP_WAIT_RESP: {
  1426. struct smp_resp *rsp_hdr = &sci_req->smp.rsp;
  1427. void *frame_header;
  1428. scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1429. frame_index,
  1430. &frame_header);
  1431. /* byte swap the header. */
  1432. word_cnt = SMP_RESP_HDR_SZ / sizeof(u32);
  1433. sci_swab32_cpy(rsp_hdr, frame_header, word_cnt);
  1434. if (rsp_hdr->frame_type == SMP_RESPONSE) {
  1435. void *smp_resp;
  1436. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1437. frame_index,
  1438. &smp_resp);
  1439. word_cnt = (sizeof(struct smp_resp) - SMP_RESP_HDR_SZ) /
  1440. sizeof(u32);
  1441. sci_swab32_cpy(((u8 *) rsp_hdr) + SMP_RESP_HDR_SZ,
  1442. smp_resp, word_cnt);
  1443. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1444. SCI_SUCCESS);
  1445. sci_change_state(&sci_req->sm, SCI_REQ_SMP_WAIT_TC_COMP);
  1446. } else {
  1447. /*
  1448. * This was not a response frame why did it get
  1449. * forwarded?
  1450. */
  1451. dev_err(scic_to_dev(scic),
  1452. "%s: SCIC SMP Request 0x%p received unexpected "
  1453. "frame %d type 0x%02x\n",
  1454. __func__,
  1455. sci_req,
  1456. frame_index,
  1457. rsp_hdr->frame_type);
  1458. scic_sds_request_set_status(sci_req,
  1459. SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
  1460. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1461. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1462. }
  1463. scic_sds_controller_release_frame(scic, frame_index);
  1464. return SCI_SUCCESS;
  1465. }
  1466. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1467. return scic_sds_stp_request_udma_general_frame_handler(sci_req,
  1468. frame_index);
  1469. case SCI_REQ_STP_UDMA_WAIT_D2H:
  1470. /* Use the general frame handler to copy the resposne data */
  1471. status = scic_sds_stp_request_udma_general_frame_handler(sci_req,
  1472. frame_index);
  1473. if (status != SCI_SUCCESS)
  1474. return status;
  1475. scic_sds_stp_request_udma_complete_request(sci_req,
  1476. SCU_TASK_DONE_CHECK_RESPONSE,
  1477. SCI_FAILURE_IO_RESPONSE_VALID);
  1478. return SCI_SUCCESS;
  1479. case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
  1480. struct dev_to_host_fis *frame_header;
  1481. u32 *frame_buffer;
  1482. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1483. frame_index,
  1484. (void **)&frame_header);
  1485. if (status != SCI_SUCCESS) {
  1486. dev_err(scic_to_dev(scic),
  1487. "%s: SCIC IO Request 0x%p could not get frame "
  1488. "header for frame index %d, status %x\n",
  1489. __func__,
  1490. stp_req,
  1491. frame_index,
  1492. status);
  1493. return status;
  1494. }
  1495. switch (frame_header->fis_type) {
  1496. case FIS_REGD2H:
  1497. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1498. frame_index,
  1499. (void **)&frame_buffer);
  1500. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1501. frame_header,
  1502. frame_buffer);
  1503. /* The command has completed with error */
  1504. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
  1505. SCI_FAILURE_IO_RESPONSE_VALID);
  1506. break;
  1507. default:
  1508. dev_warn(scic_to_dev(scic),
  1509. "%s: IO Request:0x%p Frame Id:%d protocol "
  1510. "violation occurred\n", __func__, stp_req,
  1511. frame_index);
  1512. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
  1513. SCI_FAILURE_PROTOCOL_VIOLATION);
  1514. break;
  1515. }
  1516. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1517. /* Frame has been decoded return it to the controller */
  1518. scic_sds_controller_release_frame(scic, frame_index);
  1519. return status;
  1520. }
  1521. case SCI_REQ_STP_PIO_WAIT_FRAME: {
  1522. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  1523. struct sas_task *task = isci_request_access_task(ireq);
  1524. struct dev_to_host_fis *frame_header;
  1525. u32 *frame_buffer;
  1526. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1527. frame_index,
  1528. (void **)&frame_header);
  1529. if (status != SCI_SUCCESS) {
  1530. dev_err(scic_to_dev(scic),
  1531. "%s: SCIC IO Request 0x%p could not get frame "
  1532. "header for frame index %d, status %x\n",
  1533. __func__, stp_req, frame_index, status);
  1534. return status;
  1535. }
  1536. switch (frame_header->fis_type) {
  1537. case FIS_PIO_SETUP:
  1538. /* Get from the frame buffer the PIO Setup Data */
  1539. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1540. frame_index,
  1541. (void **)&frame_buffer);
  1542. /* Get the data from the PIO Setup The SCU Hardware
  1543. * returns first word in the frame_header and the rest
  1544. * of the data is in the frame buffer so we need to
  1545. * back up one dword
  1546. */
  1547. /* transfer_count: first 16bits in the 4th dword */
  1548. stp_req->type.pio.pio_transfer_bytes = frame_buffer[3] & 0xffff;
  1549. /* ending_status: 4th byte in the 3rd dword */
  1550. stp_req->type.pio.ending_status = (frame_buffer[2] >> 24) & 0xff;
  1551. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1552. frame_header,
  1553. frame_buffer);
  1554. sci_req->stp.rsp.status = stp_req->type.pio.ending_status;
  1555. /* The next state is dependent on whether the
  1556. * request was PIO Data-in or Data out
  1557. */
  1558. if (task->data_dir == DMA_FROM_DEVICE) {
  1559. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_DATA_IN);
  1560. } else if (task->data_dir == DMA_TO_DEVICE) {
  1561. /* Transmit data */
  1562. status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
  1563. if (status != SCI_SUCCESS)
  1564. break;
  1565. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_DATA_OUT);
  1566. }
  1567. break;
  1568. case FIS_SETDEVBITS:
  1569. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1570. break;
  1571. case FIS_REGD2H:
  1572. if (frame_header->status & ATA_BUSY) {
  1573. /*
  1574. * Now why is the drive sending a D2H Register
  1575. * FIS when it is still busy? Do nothing since
  1576. * we are still in the right state.
  1577. */
  1578. dev_dbg(scic_to_dev(scic),
  1579. "%s: SCIC PIO Request 0x%p received "
  1580. "D2H Register FIS with BSY status "
  1581. "0x%x\n",
  1582. __func__,
  1583. stp_req,
  1584. frame_header->status);
  1585. break;
  1586. }
  1587. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1588. frame_index,
  1589. (void **)&frame_buffer);
  1590. scic_sds_controller_copy_sata_response(&sci_req->stp.req,
  1591. frame_header,
  1592. frame_buffer);
  1593. scic_sds_request_set_status(sci_req,
  1594. SCU_TASK_DONE_CHECK_RESPONSE,
  1595. SCI_FAILURE_IO_RESPONSE_VALID);
  1596. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1597. break;
  1598. default:
  1599. /* FIXME: what do we do here? */
  1600. break;
  1601. }
  1602. /* Frame is decoded return it to the controller */
  1603. scic_sds_controller_release_frame(scic, frame_index);
  1604. return status;
  1605. }
  1606. case SCI_REQ_STP_PIO_DATA_IN: {
  1607. struct dev_to_host_fis *frame_header;
  1608. struct sata_fis_data *frame_buffer;
  1609. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1610. frame_index,
  1611. (void **)&frame_header);
  1612. if (status != SCI_SUCCESS) {
  1613. dev_err(scic_to_dev(scic),
  1614. "%s: SCIC IO Request 0x%p could not get frame "
  1615. "header for frame index %d, status %x\n",
  1616. __func__,
  1617. stp_req,
  1618. frame_index,
  1619. status);
  1620. return status;
  1621. }
  1622. if (frame_header->fis_type != FIS_DATA) {
  1623. dev_err(scic_to_dev(scic),
  1624. "%s: SCIC PIO Request 0x%p received frame %d "
  1625. "with fis type 0x%02x when expecting a data "
  1626. "fis.\n",
  1627. __func__,
  1628. stp_req,
  1629. frame_index,
  1630. frame_header->fis_type);
  1631. scic_sds_request_set_status(sci_req,
  1632. SCU_TASK_DONE_GOOD,
  1633. SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
  1634. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1635. /* Frame is decoded return it to the controller */
  1636. scic_sds_controller_release_frame(scic, frame_index);
  1637. return status;
  1638. }
  1639. if (stp_req->type.pio.request_current.sgl_pair == NULL) {
  1640. sci_req->saved_rx_frame_index = frame_index;
  1641. stp_req->type.pio.pio_transfer_bytes = 0;
  1642. } else {
  1643. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1644. frame_index,
  1645. (void **)&frame_buffer);
  1646. status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
  1647. (u8 *)frame_buffer);
  1648. /* Frame is decoded return it to the controller */
  1649. scic_sds_controller_release_frame(scic, frame_index);
  1650. }
  1651. /* Check for the end of the transfer, are there more
  1652. * bytes remaining for this data transfer
  1653. */
  1654. if (status != SCI_SUCCESS ||
  1655. stp_req->type.pio.pio_transfer_bytes != 0)
  1656. return status;
  1657. if ((stp_req->type.pio.ending_status & ATA_BUSY) == 0) {
  1658. scic_sds_request_set_status(sci_req,
  1659. SCU_TASK_DONE_CHECK_RESPONSE,
  1660. SCI_FAILURE_IO_RESPONSE_VALID);
  1661. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1662. } else {
  1663. sci_change_state(&sci_req->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
  1664. }
  1665. return status;
  1666. }
  1667. case SCI_REQ_STP_SOFT_RESET_WAIT_D2H: {
  1668. struct dev_to_host_fis *frame_header;
  1669. u32 *frame_buffer;
  1670. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  1671. frame_index,
  1672. (void **)&frame_header);
  1673. if (status != SCI_SUCCESS) {
  1674. dev_err(scic_to_dev(scic),
  1675. "%s: SCIC IO Request 0x%p could not get frame "
  1676. "header for frame index %d, status %x\n",
  1677. __func__,
  1678. stp_req,
  1679. frame_index,
  1680. status);
  1681. return status;
  1682. }
  1683. switch (frame_header->fis_type) {
  1684. case FIS_REGD2H:
  1685. scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
  1686. frame_index,
  1687. (void **)&frame_buffer);
  1688. scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
  1689. frame_header,
  1690. frame_buffer);
  1691. /* The command has completed with error */
  1692. scic_sds_request_set_status(sci_req,
  1693. SCU_TASK_DONE_CHECK_RESPONSE,
  1694. SCI_FAILURE_IO_RESPONSE_VALID);
  1695. break;
  1696. default:
  1697. dev_warn(scic_to_dev(scic),
  1698. "%s: IO Request:0x%p Frame Id:%d protocol "
  1699. "violation occurred\n",
  1700. __func__,
  1701. stp_req,
  1702. frame_index);
  1703. scic_sds_request_set_status(sci_req,
  1704. SCU_TASK_DONE_UNEXP_FIS,
  1705. SCI_FAILURE_PROTOCOL_VIOLATION);
  1706. break;
  1707. }
  1708. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1709. /* Frame has been decoded return it to the controller */
  1710. scic_sds_controller_release_frame(scic, frame_index);
  1711. return status;
  1712. }
  1713. case SCI_REQ_ABORTING:
  1714. /*
  1715. * TODO: Is it even possible to get an unsolicited frame in the
  1716. * aborting state?
  1717. */
  1718. scic_sds_controller_release_frame(scic, frame_index);
  1719. return SCI_SUCCESS;
  1720. default:
  1721. dev_warn(scic_to_dev(scic),
  1722. "%s: SCIC IO Request given unexpected frame %x while "
  1723. "in state %d\n",
  1724. __func__,
  1725. frame_index,
  1726. state);
  1727. scic_sds_controller_release_frame(scic, frame_index);
  1728. return SCI_FAILURE_INVALID_STATE;
  1729. }
  1730. }
  1731. static enum sci_status stp_request_udma_await_tc_event(struct scic_sds_request *sci_req,
  1732. u32 completion_code)
  1733. {
  1734. enum sci_status status = SCI_SUCCESS;
  1735. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1736. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1737. scic_sds_stp_request_udma_complete_request(sci_req,
  1738. SCU_TASK_DONE_GOOD,
  1739. SCI_SUCCESS);
  1740. break;
  1741. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
  1742. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
  1743. /* We must check ther response buffer to see if the D2H
  1744. * Register FIS was received before we got the TC
  1745. * completion.
  1746. */
  1747. if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
  1748. scic_sds_remote_device_suspend(sci_req->target_device,
  1749. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1750. scic_sds_stp_request_udma_complete_request(sci_req,
  1751. SCU_TASK_DONE_CHECK_RESPONSE,
  1752. SCI_FAILURE_IO_RESPONSE_VALID);
  1753. } else {
  1754. /* If we have an error completion status for the
  1755. * TC then we can expect a D2H register FIS from
  1756. * the device so we must change state to wait
  1757. * for it
  1758. */
  1759. sci_change_state(&sci_req->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
  1760. }
  1761. break;
  1762. /* TODO Check to see if any of these completion status need to
  1763. * wait for the device to host register fis.
  1764. */
  1765. /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
  1766. * - this comes only for B0
  1767. */
  1768. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
  1769. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
  1770. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
  1771. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
  1772. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
  1773. scic_sds_remote_device_suspend(sci_req->target_device,
  1774. SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
  1775. /* Fall through to the default case */
  1776. default:
  1777. /* All other completion status cause the IO to be complete. */
  1778. scic_sds_stp_request_udma_complete_request(sci_req,
  1779. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1780. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1781. break;
  1782. }
  1783. return status;
  1784. }
  1785. static enum sci_status
  1786. stp_request_soft_reset_await_h2d_asserted_tc_event(struct scic_sds_request *sci_req,
  1787. u32 completion_code)
  1788. {
  1789. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1790. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1791. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1792. SCI_SUCCESS);
  1793. sci_change_state(&sci_req->sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG);
  1794. break;
  1795. default:
  1796. /*
  1797. * All other completion status cause the IO to be complete.
  1798. * If a NAK was received, then it is up to the user to retry
  1799. * the request.
  1800. */
  1801. scic_sds_request_set_status(sci_req,
  1802. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1803. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1804. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1805. break;
  1806. }
  1807. return SCI_SUCCESS;
  1808. }
  1809. static enum sci_status
  1810. stp_request_soft_reset_await_h2d_diagnostic_tc_event(struct scic_sds_request *sci_req,
  1811. u32 completion_code)
  1812. {
  1813. switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
  1814. case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
  1815. scic_sds_request_set_status(sci_req, SCU_TASK_DONE_GOOD,
  1816. SCI_SUCCESS);
  1817. sci_change_state(&sci_req->sm, SCI_REQ_STP_SOFT_RESET_WAIT_D2H);
  1818. break;
  1819. default:
  1820. /* All other completion status cause the IO to be complete. If
  1821. * a NAK was received, then it is up to the user to retry the
  1822. * request.
  1823. */
  1824. scic_sds_request_set_status(sci_req,
  1825. SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
  1826. SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
  1827. sci_change_state(&sci_req->sm, SCI_REQ_COMPLETED);
  1828. break;
  1829. }
  1830. return SCI_SUCCESS;
  1831. }
  1832. enum sci_status
  1833. scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req,
  1834. u32 completion_code)
  1835. {
  1836. enum sci_base_request_states state;
  1837. struct scic_sds_controller *scic = sci_req->owning_controller;
  1838. state = sci_req->sm.current_state_id;
  1839. switch (state) {
  1840. case SCI_REQ_STARTED:
  1841. return request_started_state_tc_event(sci_req, completion_code);
  1842. case SCI_REQ_TASK_WAIT_TC_COMP:
  1843. return ssp_task_request_await_tc_event(sci_req,
  1844. completion_code);
  1845. case SCI_REQ_SMP_WAIT_RESP:
  1846. return smp_request_await_response_tc_event(sci_req,
  1847. completion_code);
  1848. case SCI_REQ_SMP_WAIT_TC_COMP:
  1849. return smp_request_await_tc_event(sci_req, completion_code);
  1850. case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
  1851. return stp_request_udma_await_tc_event(sci_req,
  1852. completion_code);
  1853. case SCI_REQ_STP_NON_DATA_WAIT_H2D:
  1854. return stp_request_non_data_await_h2d_tc_event(sci_req,
  1855. completion_code);
  1856. case SCI_REQ_STP_PIO_WAIT_H2D:
  1857. return stp_request_pio_await_h2d_completion_tc_event(sci_req,
  1858. completion_code);
  1859. case SCI_REQ_STP_PIO_DATA_OUT:
  1860. return pio_data_out_tx_done_tc_event(sci_req, completion_code);
  1861. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED:
  1862. return stp_request_soft_reset_await_h2d_asserted_tc_event(sci_req,
  1863. completion_code);
  1864. case SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG:
  1865. return stp_request_soft_reset_await_h2d_diagnostic_tc_event(sci_req,
  1866. completion_code);
  1867. case SCI_REQ_ABORTING:
  1868. return request_aborting_state_tc_event(sci_req,
  1869. completion_code);
  1870. default:
  1871. dev_warn(scic_to_dev(scic),
  1872. "%s: SCIC IO Request given task completion "
  1873. "notification %x while in wrong state %d\n",
  1874. __func__,
  1875. completion_code,
  1876. state);
  1877. return SCI_FAILURE_INVALID_STATE;
  1878. }
  1879. }
  1880. /**
  1881. * isci_request_process_response_iu() - This function sets the status and
  1882. * response iu, in the task struct, from the request object for the upper
  1883. * layer driver.
  1884. * @sas_task: This parameter is the task struct from the upper layer driver.
  1885. * @resp_iu: This parameter points to the response iu of the completed request.
  1886. * @dev: This parameter specifies the linux device struct.
  1887. *
  1888. * none.
  1889. */
  1890. static void isci_request_process_response_iu(
  1891. struct sas_task *task,
  1892. struct ssp_response_iu *resp_iu,
  1893. struct device *dev)
  1894. {
  1895. dev_dbg(dev,
  1896. "%s: resp_iu = %p "
  1897. "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
  1898. "resp_iu->response_data_len = %x, "
  1899. "resp_iu->sense_data_len = %x\nrepsonse data: ",
  1900. __func__,
  1901. resp_iu,
  1902. resp_iu->status,
  1903. resp_iu->datapres,
  1904. resp_iu->response_data_len,
  1905. resp_iu->sense_data_len);
  1906. task->task_status.stat = resp_iu->status;
  1907. /* libsas updates the task status fields based on the response iu. */
  1908. sas_ssp_task_response(dev, task, resp_iu);
  1909. }
  1910. /**
  1911. * isci_request_set_open_reject_status() - This function prepares the I/O
  1912. * completion for OPEN_REJECT conditions.
  1913. * @request: This parameter is the completed isci_request object.
  1914. * @response_ptr: This parameter specifies the service response for the I/O.
  1915. * @status_ptr: This parameter specifies the exec status for the I/O.
  1916. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1917. * the LLDD with respect to completing this request or forcing an abort
  1918. * condition on the I/O.
  1919. * @open_rej_reason: This parameter specifies the encoded reason for the
  1920. * abandon-class reject.
  1921. *
  1922. * none.
  1923. */
  1924. static void isci_request_set_open_reject_status(
  1925. struct isci_request *request,
  1926. struct sas_task *task,
  1927. enum service_response *response_ptr,
  1928. enum exec_status *status_ptr,
  1929. enum isci_completion_selection *complete_to_host_ptr,
  1930. enum sas_open_rej_reason open_rej_reason)
  1931. {
  1932. /* Task in the target is done. */
  1933. request->complete_in_target = true;
  1934. *response_ptr = SAS_TASK_UNDELIVERED;
  1935. *status_ptr = SAS_OPEN_REJECT;
  1936. *complete_to_host_ptr = isci_perform_normal_io_completion;
  1937. task->task_status.open_rej_reason = open_rej_reason;
  1938. }
  1939. /**
  1940. * isci_request_handle_controller_specific_errors() - This function decodes
  1941. * controller-specific I/O completion error conditions.
  1942. * @request: This parameter is the completed isci_request object.
  1943. * @response_ptr: This parameter specifies the service response for the I/O.
  1944. * @status_ptr: This parameter specifies the exec status for the I/O.
  1945. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  1946. * the LLDD with respect to completing this request or forcing an abort
  1947. * condition on the I/O.
  1948. *
  1949. * none.
  1950. */
  1951. static void isci_request_handle_controller_specific_errors(
  1952. struct isci_remote_device *idev,
  1953. struct isci_request *request,
  1954. struct sas_task *task,
  1955. enum service_response *response_ptr,
  1956. enum exec_status *status_ptr,
  1957. enum isci_completion_selection *complete_to_host_ptr)
  1958. {
  1959. unsigned int cstatus;
  1960. cstatus = request->sci.scu_status;
  1961. dev_dbg(&request->isci_host->pdev->dev,
  1962. "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
  1963. "- controller status = 0x%x\n",
  1964. __func__, request, cstatus);
  1965. /* Decode the controller-specific errors; most
  1966. * important is to recognize those conditions in which
  1967. * the target may still have a task outstanding that
  1968. * must be aborted.
  1969. *
  1970. * Note that there are SCU completion codes being
  1971. * named in the decode below for which SCIC has already
  1972. * done work to handle them in a way other than as
  1973. * a controller-specific completion code; these are left
  1974. * in the decode below for completeness sake.
  1975. */
  1976. switch (cstatus) {
  1977. case SCU_TASK_DONE_DMASETUP_DIRERR:
  1978. /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
  1979. case SCU_TASK_DONE_XFERCNT_ERR:
  1980. /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
  1981. if (task->task_proto == SAS_PROTOCOL_SMP) {
  1982. /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
  1983. *response_ptr = SAS_TASK_COMPLETE;
  1984. /* See if the device has been/is being stopped. Note
  1985. * that we ignore the quiesce state, since we are
  1986. * concerned about the actual device state.
  1987. */
  1988. if (!idev)
  1989. *status_ptr = SAS_DEVICE_UNKNOWN;
  1990. else
  1991. *status_ptr = SAS_ABORTED_TASK;
  1992. request->complete_in_target = true;
  1993. *complete_to_host_ptr =
  1994. isci_perform_normal_io_completion;
  1995. } else {
  1996. /* Task in the target is not done. */
  1997. *response_ptr = SAS_TASK_UNDELIVERED;
  1998. if (!idev)
  1999. *status_ptr = SAS_DEVICE_UNKNOWN;
  2000. else
  2001. *status_ptr = SAM_STAT_TASK_ABORTED;
  2002. request->complete_in_target = false;
  2003. *complete_to_host_ptr =
  2004. isci_perform_error_io_completion;
  2005. }
  2006. break;
  2007. case SCU_TASK_DONE_CRC_ERR:
  2008. case SCU_TASK_DONE_NAK_CMD_ERR:
  2009. case SCU_TASK_DONE_EXCESS_DATA:
  2010. case SCU_TASK_DONE_UNEXP_FIS:
  2011. /* Also SCU_TASK_DONE_UNEXP_RESP: */
  2012. case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
  2013. case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
  2014. case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
  2015. /* These are conditions in which the target
  2016. * has completed the task, so that no cleanup
  2017. * is necessary.
  2018. */
  2019. *response_ptr = SAS_TASK_COMPLETE;
  2020. /* See if the device has been/is being stopped. Note
  2021. * that we ignore the quiesce state, since we are
  2022. * concerned about the actual device state.
  2023. */
  2024. if (!idev)
  2025. *status_ptr = SAS_DEVICE_UNKNOWN;
  2026. else
  2027. *status_ptr = SAS_ABORTED_TASK;
  2028. request->complete_in_target = true;
  2029. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2030. break;
  2031. /* Note that the only open reject completion codes seen here will be
  2032. * abandon-class codes; all others are automatically retried in the SCU.
  2033. */
  2034. case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
  2035. isci_request_set_open_reject_status(
  2036. request, task, response_ptr, status_ptr,
  2037. complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
  2038. break;
  2039. case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
  2040. /* Note - the return of AB0 will change when
  2041. * libsas implements detection of zone violations.
  2042. */
  2043. isci_request_set_open_reject_status(
  2044. request, task, response_ptr, status_ptr,
  2045. complete_to_host_ptr, SAS_OREJ_RESV_AB0);
  2046. break;
  2047. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
  2048. isci_request_set_open_reject_status(
  2049. request, task, response_ptr, status_ptr,
  2050. complete_to_host_ptr, SAS_OREJ_RESV_AB1);
  2051. break;
  2052. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
  2053. isci_request_set_open_reject_status(
  2054. request, task, response_ptr, status_ptr,
  2055. complete_to_host_ptr, SAS_OREJ_RESV_AB2);
  2056. break;
  2057. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
  2058. isci_request_set_open_reject_status(
  2059. request, task, response_ptr, status_ptr,
  2060. complete_to_host_ptr, SAS_OREJ_RESV_AB3);
  2061. break;
  2062. case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
  2063. isci_request_set_open_reject_status(
  2064. request, task, response_ptr, status_ptr,
  2065. complete_to_host_ptr, SAS_OREJ_BAD_DEST);
  2066. break;
  2067. case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
  2068. isci_request_set_open_reject_status(
  2069. request, task, response_ptr, status_ptr,
  2070. complete_to_host_ptr, SAS_OREJ_STP_NORES);
  2071. break;
  2072. case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
  2073. isci_request_set_open_reject_status(
  2074. request, task, response_ptr, status_ptr,
  2075. complete_to_host_ptr, SAS_OREJ_EPROTO);
  2076. break;
  2077. case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
  2078. isci_request_set_open_reject_status(
  2079. request, task, response_ptr, status_ptr,
  2080. complete_to_host_ptr, SAS_OREJ_CONN_RATE);
  2081. break;
  2082. case SCU_TASK_DONE_LL_R_ERR:
  2083. /* Also SCU_TASK_DONE_ACK_NAK_TO: */
  2084. case SCU_TASK_DONE_LL_PERR:
  2085. case SCU_TASK_DONE_LL_SY_TERM:
  2086. /* Also SCU_TASK_DONE_NAK_ERR:*/
  2087. case SCU_TASK_DONE_LL_LF_TERM:
  2088. /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
  2089. case SCU_TASK_DONE_LL_ABORT_ERR:
  2090. case SCU_TASK_DONE_SEQ_INV_TYPE:
  2091. /* Also SCU_TASK_DONE_UNEXP_XR: */
  2092. case SCU_TASK_DONE_XR_IU_LEN_ERR:
  2093. case SCU_TASK_DONE_INV_FIS_LEN:
  2094. /* Also SCU_TASK_DONE_XR_WD_LEN: */
  2095. case SCU_TASK_DONE_SDMA_ERR:
  2096. case SCU_TASK_DONE_OFFSET_ERR:
  2097. case SCU_TASK_DONE_MAX_PLD_ERR:
  2098. case SCU_TASK_DONE_LF_ERR:
  2099. case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
  2100. case SCU_TASK_DONE_SMP_LL_RX_ERR:
  2101. case SCU_TASK_DONE_UNEXP_DATA:
  2102. case SCU_TASK_DONE_UNEXP_SDBFIS:
  2103. case SCU_TASK_DONE_REG_ERR:
  2104. case SCU_TASK_DONE_SDB_ERR:
  2105. case SCU_TASK_DONE_TASK_ABORT:
  2106. default:
  2107. /* Task in the target is not done. */
  2108. *response_ptr = SAS_TASK_UNDELIVERED;
  2109. *status_ptr = SAM_STAT_TASK_ABORTED;
  2110. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2111. request->complete_in_target = true;
  2112. *complete_to_host_ptr = isci_perform_normal_io_completion;
  2113. } else {
  2114. request->complete_in_target = false;
  2115. *complete_to_host_ptr = isci_perform_error_io_completion;
  2116. }
  2117. break;
  2118. }
  2119. }
  2120. /**
  2121. * isci_task_save_for_upper_layer_completion() - This function saves the
  2122. * request for later completion to the upper layer driver.
  2123. * @host: This parameter is a pointer to the host on which the the request
  2124. * should be queued (either as an error or success).
  2125. * @request: This parameter is the completed request.
  2126. * @response: This parameter is the response code for the completed task.
  2127. * @status: This parameter is the status code for the completed task.
  2128. *
  2129. * none.
  2130. */
  2131. static void isci_task_save_for_upper_layer_completion(
  2132. struct isci_host *host,
  2133. struct isci_request *request,
  2134. enum service_response response,
  2135. enum exec_status status,
  2136. enum isci_completion_selection task_notification_selection)
  2137. {
  2138. struct sas_task *task = isci_request_access_task(request);
  2139. task_notification_selection
  2140. = isci_task_set_completion_status(task, response, status,
  2141. task_notification_selection);
  2142. /* Tasks aborted specifically by a call to the lldd_abort_task
  2143. * function should not be completed to the host in the regular path.
  2144. */
  2145. switch (task_notification_selection) {
  2146. case isci_perform_normal_io_completion:
  2147. /* Normal notification (task_done) */
  2148. dev_dbg(&host->pdev->dev,
  2149. "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
  2150. __func__,
  2151. task,
  2152. task->task_status.resp, response,
  2153. task->task_status.stat, status);
  2154. /* Add to the completed list. */
  2155. list_add(&request->completed_node,
  2156. &host->requests_to_complete);
  2157. /* Take the request off the device's pending request list. */
  2158. list_del_init(&request->dev_node);
  2159. break;
  2160. case isci_perform_aborted_io_completion:
  2161. /* No notification to libsas because this request is
  2162. * already in the abort path.
  2163. */
  2164. dev_warn(&host->pdev->dev,
  2165. "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
  2166. __func__,
  2167. task,
  2168. task->task_status.resp, response,
  2169. task->task_status.stat, status);
  2170. /* Wake up whatever process was waiting for this
  2171. * request to complete.
  2172. */
  2173. WARN_ON(request->io_request_completion == NULL);
  2174. if (request->io_request_completion != NULL) {
  2175. /* Signal whoever is waiting that this
  2176. * request is complete.
  2177. */
  2178. complete(request->io_request_completion);
  2179. }
  2180. break;
  2181. case isci_perform_error_io_completion:
  2182. /* Use sas_task_abort */
  2183. dev_warn(&host->pdev->dev,
  2184. "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
  2185. __func__,
  2186. task,
  2187. task->task_status.resp, response,
  2188. task->task_status.stat, status);
  2189. /* Add to the aborted list. */
  2190. list_add(&request->completed_node,
  2191. &host->requests_to_errorback);
  2192. break;
  2193. default:
  2194. dev_warn(&host->pdev->dev,
  2195. "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
  2196. __func__,
  2197. task,
  2198. task->task_status.resp, response,
  2199. task->task_status.stat, status);
  2200. /* Add to the error to libsas list. */
  2201. list_add(&request->completed_node,
  2202. &host->requests_to_errorback);
  2203. break;
  2204. }
  2205. }
  2206. static void isci_request_io_request_complete(struct isci_host *isci_host,
  2207. struct isci_request *request,
  2208. enum sci_io_status completion_status)
  2209. {
  2210. struct sas_task *task = isci_request_access_task(request);
  2211. struct ssp_response_iu *resp_iu;
  2212. void *resp_buf;
  2213. unsigned long task_flags;
  2214. struct isci_remote_device *idev = isci_lookup_device(task->dev);
  2215. enum service_response response = SAS_TASK_UNDELIVERED;
  2216. enum exec_status status = SAS_ABORTED_TASK;
  2217. enum isci_request_status request_status;
  2218. enum isci_completion_selection complete_to_host
  2219. = isci_perform_normal_io_completion;
  2220. dev_dbg(&isci_host->pdev->dev,
  2221. "%s: request = %p, task = %p,\n"
  2222. "task->data_dir = %d completion_status = 0x%x\n",
  2223. __func__,
  2224. request,
  2225. task,
  2226. task->data_dir,
  2227. completion_status);
  2228. spin_lock(&request->state_lock);
  2229. request_status = isci_request_get_state(request);
  2230. /* Decode the request status. Note that if the request has been
  2231. * aborted by a task management function, we don't care
  2232. * what the status is.
  2233. */
  2234. switch (request_status) {
  2235. case aborted:
  2236. /* "aborted" indicates that the request was aborted by a task
  2237. * management function, since once a task management request is
  2238. * perfomed by the device, the request only completes because
  2239. * of the subsequent driver terminate.
  2240. *
  2241. * Aborted also means an external thread is explicitly managing
  2242. * this request, so that we do not complete it up the stack.
  2243. *
  2244. * The target is still there (since the TMF was successful).
  2245. */
  2246. request->complete_in_target = true;
  2247. response = SAS_TASK_COMPLETE;
  2248. /* See if the device has been/is being stopped. Note
  2249. * that we ignore the quiesce state, since we are
  2250. * concerned about the actual device state.
  2251. */
  2252. if (!idev)
  2253. status = SAS_DEVICE_UNKNOWN;
  2254. else
  2255. status = SAS_ABORTED_TASK;
  2256. complete_to_host = isci_perform_aborted_io_completion;
  2257. /* This was an aborted request. */
  2258. spin_unlock(&request->state_lock);
  2259. break;
  2260. case aborting:
  2261. /* aborting means that the task management function tried and
  2262. * failed to abort the request. We need to note the request
  2263. * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
  2264. * target as down.
  2265. *
  2266. * Aborting also means an external thread is explicitly managing
  2267. * this request, so that we do not complete it up the stack.
  2268. */
  2269. request->complete_in_target = true;
  2270. response = SAS_TASK_UNDELIVERED;
  2271. if (!idev)
  2272. /* The device has been /is being stopped. Note that
  2273. * we ignore the quiesce state, since we are
  2274. * concerned about the actual device state.
  2275. */
  2276. status = SAS_DEVICE_UNKNOWN;
  2277. else
  2278. status = SAS_PHY_DOWN;
  2279. complete_to_host = isci_perform_aborted_io_completion;
  2280. /* This was an aborted request. */
  2281. spin_unlock(&request->state_lock);
  2282. break;
  2283. case terminating:
  2284. /* This was an terminated request. This happens when
  2285. * the I/O is being terminated because of an action on
  2286. * the device (reset, tear down, etc.), and the I/O needs
  2287. * to be completed up the stack.
  2288. */
  2289. request->complete_in_target = true;
  2290. response = SAS_TASK_UNDELIVERED;
  2291. /* See if the device has been/is being stopped. Note
  2292. * that we ignore the quiesce state, since we are
  2293. * concerned about the actual device state.
  2294. */
  2295. if (!idev)
  2296. status = SAS_DEVICE_UNKNOWN;
  2297. else
  2298. status = SAS_ABORTED_TASK;
  2299. complete_to_host = isci_perform_aborted_io_completion;
  2300. /* This was a terminated request. */
  2301. spin_unlock(&request->state_lock);
  2302. break;
  2303. case dead:
  2304. /* This was a terminated request that timed-out during the
  2305. * termination process. There is no task to complete to
  2306. * libsas.
  2307. */
  2308. complete_to_host = isci_perform_normal_io_completion;
  2309. spin_unlock(&request->state_lock);
  2310. break;
  2311. default:
  2312. /* The request is done from an SCU HW perspective. */
  2313. request->status = completed;
  2314. spin_unlock(&request->state_lock);
  2315. /* This is an active request being completed from the core. */
  2316. switch (completion_status) {
  2317. case SCI_IO_FAILURE_RESPONSE_VALID:
  2318. dev_dbg(&isci_host->pdev->dev,
  2319. "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
  2320. __func__,
  2321. request,
  2322. task);
  2323. if (sas_protocol_ata(task->task_proto)) {
  2324. resp_buf = &request->sci.stp.rsp;
  2325. isci_request_process_stp_response(task,
  2326. resp_buf);
  2327. } else if (SAS_PROTOCOL_SSP == task->task_proto) {
  2328. /* crack the iu response buffer. */
  2329. resp_iu = &request->sci.ssp.rsp;
  2330. isci_request_process_response_iu(task, resp_iu,
  2331. &isci_host->pdev->dev);
  2332. } else if (SAS_PROTOCOL_SMP == task->task_proto) {
  2333. dev_err(&isci_host->pdev->dev,
  2334. "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
  2335. "SAS_PROTOCOL_SMP protocol\n",
  2336. __func__);
  2337. } else
  2338. dev_err(&isci_host->pdev->dev,
  2339. "%s: unknown protocol\n", __func__);
  2340. /* use the task status set in the task struct by the
  2341. * isci_request_process_response_iu call.
  2342. */
  2343. request->complete_in_target = true;
  2344. response = task->task_status.resp;
  2345. status = task->task_status.stat;
  2346. break;
  2347. case SCI_IO_SUCCESS:
  2348. case SCI_IO_SUCCESS_IO_DONE_EARLY:
  2349. response = SAS_TASK_COMPLETE;
  2350. status = SAM_STAT_GOOD;
  2351. request->complete_in_target = true;
  2352. if (task->task_proto == SAS_PROTOCOL_SMP) {
  2353. void *rsp = &request->sci.smp.rsp;
  2354. dev_dbg(&isci_host->pdev->dev,
  2355. "%s: SMP protocol completion\n",
  2356. __func__);
  2357. sg_copy_from_buffer(
  2358. &task->smp_task.smp_resp, 1,
  2359. rsp, sizeof(struct smp_resp));
  2360. } else if (completion_status
  2361. == SCI_IO_SUCCESS_IO_DONE_EARLY) {
  2362. /* This was an SSP / STP / SATA transfer.
  2363. * There is a possibility that less data than
  2364. * the maximum was transferred.
  2365. */
  2366. u32 transferred_length = sci_req_tx_bytes(&request->sci);
  2367. task->task_status.residual
  2368. = task->total_xfer_len - transferred_length;
  2369. /* If there were residual bytes, call this an
  2370. * underrun.
  2371. */
  2372. if (task->task_status.residual != 0)
  2373. status = SAS_DATA_UNDERRUN;
  2374. dev_dbg(&isci_host->pdev->dev,
  2375. "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
  2376. __func__,
  2377. status);
  2378. } else
  2379. dev_dbg(&isci_host->pdev->dev,
  2380. "%s: SCI_IO_SUCCESS\n",
  2381. __func__);
  2382. break;
  2383. case SCI_IO_FAILURE_TERMINATED:
  2384. dev_dbg(&isci_host->pdev->dev,
  2385. "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
  2386. __func__,
  2387. request,
  2388. task);
  2389. /* The request was terminated explicitly. No handling
  2390. * is needed in the SCSI error handler path.
  2391. */
  2392. request->complete_in_target = true;
  2393. response = SAS_TASK_UNDELIVERED;
  2394. /* See if the device has been/is being stopped. Note
  2395. * that we ignore the quiesce state, since we are
  2396. * concerned about the actual device state.
  2397. */
  2398. if (!idev)
  2399. status = SAS_DEVICE_UNKNOWN;
  2400. else
  2401. status = SAS_ABORTED_TASK;
  2402. complete_to_host = isci_perform_normal_io_completion;
  2403. break;
  2404. case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
  2405. isci_request_handle_controller_specific_errors(
  2406. idev, request, task, &response, &status,
  2407. &complete_to_host);
  2408. break;
  2409. case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
  2410. /* This is a special case, in that the I/O completion
  2411. * is telling us that the device needs a reset.
  2412. * In order for the device reset condition to be
  2413. * noticed, the I/O has to be handled in the error
  2414. * handler. Set the reset flag and cause the
  2415. * SCSI error thread to be scheduled.
  2416. */
  2417. spin_lock_irqsave(&task->task_state_lock, task_flags);
  2418. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  2419. spin_unlock_irqrestore(&task->task_state_lock, task_flags);
  2420. /* Fail the I/O. */
  2421. response = SAS_TASK_UNDELIVERED;
  2422. status = SAM_STAT_TASK_ABORTED;
  2423. complete_to_host = isci_perform_error_io_completion;
  2424. request->complete_in_target = false;
  2425. break;
  2426. case SCI_FAILURE_RETRY_REQUIRED:
  2427. /* Fail the I/O so it can be retried. */
  2428. response = SAS_TASK_UNDELIVERED;
  2429. if (!idev)
  2430. status = SAS_DEVICE_UNKNOWN;
  2431. else
  2432. status = SAS_ABORTED_TASK;
  2433. complete_to_host = isci_perform_normal_io_completion;
  2434. request->complete_in_target = true;
  2435. break;
  2436. default:
  2437. /* Catch any otherwise unhandled error codes here. */
  2438. dev_warn(&isci_host->pdev->dev,
  2439. "%s: invalid completion code: 0x%x - "
  2440. "isci_request = %p\n",
  2441. __func__, completion_status, request);
  2442. response = SAS_TASK_UNDELIVERED;
  2443. /* See if the device has been/is being stopped. Note
  2444. * that we ignore the quiesce state, since we are
  2445. * concerned about the actual device state.
  2446. */
  2447. if (!idev)
  2448. status = SAS_DEVICE_UNKNOWN;
  2449. else
  2450. status = SAS_ABORTED_TASK;
  2451. if (SAS_PROTOCOL_SMP == task->task_proto) {
  2452. request->complete_in_target = true;
  2453. complete_to_host = isci_perform_normal_io_completion;
  2454. } else {
  2455. request->complete_in_target = false;
  2456. complete_to_host = isci_perform_error_io_completion;
  2457. }
  2458. break;
  2459. }
  2460. break;
  2461. }
  2462. switch (task->task_proto) {
  2463. case SAS_PROTOCOL_SSP:
  2464. if (task->data_dir == DMA_NONE)
  2465. break;
  2466. if (task->num_scatter == 0)
  2467. /* 0 indicates a single dma address */
  2468. dma_unmap_single(&isci_host->pdev->dev,
  2469. request->zero_scatter_daddr,
  2470. task->total_xfer_len, task->data_dir);
  2471. else /* unmap the sgl dma addresses */
  2472. dma_unmap_sg(&isci_host->pdev->dev, task->scatter,
  2473. request->num_sg_entries, task->data_dir);
  2474. break;
  2475. case SAS_PROTOCOL_SMP: {
  2476. struct scatterlist *sg = &task->smp_task.smp_req;
  2477. struct smp_req *smp_req;
  2478. void *kaddr;
  2479. dma_unmap_sg(&isci_host->pdev->dev, sg, 1, DMA_TO_DEVICE);
  2480. /* need to swab it back in case the command buffer is re-used */
  2481. kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
  2482. smp_req = kaddr + sg->offset;
  2483. sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
  2484. kunmap_atomic(kaddr, KM_IRQ0);
  2485. break;
  2486. }
  2487. default:
  2488. break;
  2489. }
  2490. /* Put the completed request on the correct list */
  2491. isci_task_save_for_upper_layer_completion(isci_host, request, response,
  2492. status, complete_to_host
  2493. );
  2494. /* complete the io request to the core. */
  2495. scic_controller_complete_io(&isci_host->sci,
  2496. request->sci.target_device,
  2497. &request->sci);
  2498. isci_put_device(idev);
  2499. /* set terminated handle so it cannot be completed or
  2500. * terminated again, and to cause any calls into abort
  2501. * task to recognize the already completed case.
  2502. */
  2503. request->terminated = true;
  2504. isci_host_can_dequeue(isci_host, 1);
  2505. }
  2506. static void scic_sds_request_started_state_enter(struct sci_base_state_machine *sm)
  2507. {
  2508. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2509. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  2510. struct domain_device *dev = sci_dev_to_domain(sci_req->target_device);
  2511. struct sas_task *task;
  2512. /* XXX as hch said always creating an internal sas_task for tmf
  2513. * requests would simplify the driver
  2514. */
  2515. task = ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL;
  2516. /* all unaccelerated request types (non ssp or ncq) handled with
  2517. * substates
  2518. */
  2519. if (!task && dev->dev_type == SAS_END_DEV) {
  2520. sci_change_state(sm, SCI_REQ_TASK_WAIT_TC_COMP);
  2521. } else if (!task &&
  2522. (isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_high ||
  2523. isci_request_access_tmf(ireq)->tmf_code == isci_tmf_sata_srst_low)) {
  2524. sci_change_state(sm, SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED);
  2525. } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
  2526. sci_change_state(sm, SCI_REQ_SMP_WAIT_RESP);
  2527. } else if (task && sas_protocol_ata(task->task_proto) &&
  2528. !task->ata_task.use_ncq) {
  2529. u32 state;
  2530. if (task->data_dir == DMA_NONE)
  2531. state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
  2532. else if (task->ata_task.dma_xfer)
  2533. state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
  2534. else /* PIO */
  2535. state = SCI_REQ_STP_PIO_WAIT_H2D;
  2536. sci_change_state(sm, state);
  2537. }
  2538. }
  2539. static void scic_sds_request_completed_state_enter(struct sci_base_state_machine *sm)
  2540. {
  2541. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2542. struct scic_sds_controller *scic = sci_req->owning_controller;
  2543. struct isci_host *ihost = scic_to_ihost(scic);
  2544. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  2545. /* Tell the SCI_USER that the IO request is complete */
  2546. if (sci_req->is_task_management_request == false)
  2547. isci_request_io_request_complete(ihost, ireq,
  2548. sci_req->sci_status);
  2549. else
  2550. isci_task_request_complete(ihost, ireq, sci_req->sci_status);
  2551. }
  2552. static void scic_sds_request_aborting_state_enter(struct sci_base_state_machine *sm)
  2553. {
  2554. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2555. /* Setting the abort bit in the Task Context is required by the silicon. */
  2556. sci_req->task_context_buffer->abort = 1;
  2557. }
  2558. static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2559. {
  2560. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2561. scic_sds_remote_device_set_working_request(sci_req->target_device,
  2562. sci_req);
  2563. }
  2564. static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
  2565. {
  2566. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2567. scic_sds_remote_device_set_working_request(sci_req->target_device,
  2568. sci_req);
  2569. }
  2570. static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(struct sci_base_state_machine *sm)
  2571. {
  2572. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2573. scic_sds_remote_device_set_working_request(sci_req->target_device,
  2574. sci_req);
  2575. }
  2576. static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(struct sci_base_state_machine *sm)
  2577. {
  2578. struct scic_sds_request *sci_req = container_of(sm, typeof(*sci_req), sm);
  2579. struct scu_task_context *task_context;
  2580. struct host_to_dev_fis *h2d_fis;
  2581. enum sci_status status;
  2582. /* Clear the SRST bit */
  2583. h2d_fis = &sci_req->stp.cmd;
  2584. h2d_fis->control = 0;
  2585. /* Clear the TC control bit */
  2586. task_context = scic_sds_controller_get_task_context_buffer(
  2587. sci_req->owning_controller, sci_req->io_tag);
  2588. task_context->control_frame = 0;
  2589. status = scic_controller_continue_io(sci_req);
  2590. WARN_ONCE(status != SCI_SUCCESS, "isci: continue io failure\n");
  2591. }
  2592. static const struct sci_base_state scic_sds_request_state_table[] = {
  2593. [SCI_REQ_INIT] = { },
  2594. [SCI_REQ_CONSTRUCTED] = { },
  2595. [SCI_REQ_STARTED] = {
  2596. .enter_state = scic_sds_request_started_state_enter,
  2597. },
  2598. [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
  2599. .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
  2600. },
  2601. [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
  2602. [SCI_REQ_STP_PIO_WAIT_H2D] = {
  2603. .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
  2604. },
  2605. [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
  2606. [SCI_REQ_STP_PIO_DATA_IN] = { },
  2607. [SCI_REQ_STP_PIO_DATA_OUT] = { },
  2608. [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
  2609. [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
  2610. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED] = {
  2611. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
  2612. },
  2613. [SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG] = {
  2614. .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
  2615. },
  2616. [SCI_REQ_STP_SOFT_RESET_WAIT_D2H] = { },
  2617. [SCI_REQ_TASK_WAIT_TC_COMP] = { },
  2618. [SCI_REQ_TASK_WAIT_TC_RESP] = { },
  2619. [SCI_REQ_SMP_WAIT_RESP] = { },
  2620. [SCI_REQ_SMP_WAIT_TC_COMP] = { },
  2621. [SCI_REQ_COMPLETED] = {
  2622. .enter_state = scic_sds_request_completed_state_enter,
  2623. },
  2624. [SCI_REQ_ABORTING] = {
  2625. .enter_state = scic_sds_request_aborting_state_enter,
  2626. },
  2627. [SCI_REQ_FINAL] = { },
  2628. };
  2629. static void
  2630. scic_sds_general_request_construct(struct scic_sds_controller *scic,
  2631. struct scic_sds_remote_device *sci_dev,
  2632. u16 io_tag,
  2633. struct scic_sds_request *sci_req)
  2634. {
  2635. sci_init_sm(&sci_req->sm, scic_sds_request_state_table, SCI_REQ_INIT);
  2636. sci_req->io_tag = io_tag;
  2637. sci_req->owning_controller = scic;
  2638. sci_req->target_device = sci_dev;
  2639. sci_req->protocol = SCIC_NO_PROTOCOL;
  2640. sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
  2641. sci_req->sci_status = SCI_SUCCESS;
  2642. sci_req->scu_status = 0;
  2643. sci_req->post_context = 0xFFFFFFFF;
  2644. sci_req->is_task_management_request = false;
  2645. if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  2646. sci_req->was_tag_assigned_by_user = false;
  2647. sci_req->task_context_buffer = &sci_req->tc;
  2648. } else {
  2649. sci_req->was_tag_assigned_by_user = true;
  2650. sci_req->task_context_buffer =
  2651. scic_sds_controller_get_task_context_buffer(scic, io_tag);
  2652. }
  2653. }
  2654. static enum sci_status
  2655. scic_io_request_construct(struct scic_sds_controller *scic,
  2656. struct scic_sds_remote_device *sci_dev,
  2657. u16 io_tag, struct scic_sds_request *sci_req)
  2658. {
  2659. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2660. enum sci_status status = SCI_SUCCESS;
  2661. /* Build the common part of the request */
  2662. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  2663. if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  2664. return SCI_FAILURE_INVALID_REMOTE_DEVICE;
  2665. if (dev->dev_type == SAS_END_DEV)
  2666. /* pass */;
  2667. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  2668. memset(&sci_req->stp.cmd, 0, sizeof(sci_req->stp.cmd));
  2669. else if (dev_is_expander(dev))
  2670. /* pass */;
  2671. else
  2672. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2673. memset(sci_req->task_context_buffer, 0,
  2674. offsetof(struct scu_task_context, sgl_pair_ab));
  2675. return status;
  2676. }
  2677. enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
  2678. struct scic_sds_remote_device *sci_dev,
  2679. u16 io_tag, struct scic_sds_request *sci_req)
  2680. {
  2681. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  2682. enum sci_status status = SCI_SUCCESS;
  2683. /* Build the common part of the request */
  2684. scic_sds_general_request_construct(scic, sci_dev, io_tag, sci_req);
  2685. if (dev->dev_type == SAS_END_DEV ||
  2686. dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  2687. sci_req->is_task_management_request = true;
  2688. memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
  2689. } else
  2690. status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  2691. return status;
  2692. }
  2693. static enum sci_status isci_request_ssp_request_construct(
  2694. struct isci_request *request)
  2695. {
  2696. enum sci_status status;
  2697. dev_dbg(&request->isci_host->pdev->dev,
  2698. "%s: request = %p\n",
  2699. __func__,
  2700. request);
  2701. status = scic_io_request_construct_basic_ssp(&request->sci);
  2702. return status;
  2703. }
  2704. static enum sci_status isci_request_stp_request_construct(
  2705. struct isci_request *request)
  2706. {
  2707. struct sas_task *task = isci_request_access_task(request);
  2708. enum sci_status status;
  2709. struct host_to_dev_fis *register_fis;
  2710. dev_dbg(&request->isci_host->pdev->dev,
  2711. "%s: request = %p\n",
  2712. __func__,
  2713. request);
  2714. /* Get the host_to_dev_fis from the core and copy
  2715. * the fis from the task into it.
  2716. */
  2717. register_fis = isci_sata_task_to_fis_copy(task);
  2718. status = scic_io_request_construct_basic_sata(&request->sci);
  2719. /* Set the ncq tag in the fis, from the queue
  2720. * command in the task.
  2721. */
  2722. if (isci_sata_is_task_ncq(task)) {
  2723. isci_sata_set_ncq_tag(
  2724. register_fis,
  2725. task
  2726. );
  2727. }
  2728. return status;
  2729. }
  2730. static enum sci_status
  2731. scic_io_request_construct_smp(struct device *dev,
  2732. struct scic_sds_request *sci_req,
  2733. struct sas_task *task)
  2734. {
  2735. struct scatterlist *sg = &task->smp_task.smp_req;
  2736. struct scic_sds_remote_device *sci_dev;
  2737. struct scu_task_context *task_context;
  2738. struct scic_sds_port *sci_port;
  2739. struct smp_req *smp_req;
  2740. void *kaddr;
  2741. u8 req_len;
  2742. u32 cmd;
  2743. kaddr = kmap_atomic(sg_page(sg), KM_IRQ0);
  2744. smp_req = kaddr + sg->offset;
  2745. /*
  2746. * Look at the SMP requests' header fields; for certain SAS 1.x SMP
  2747. * functions under SAS 2.0, a zero request length really indicates
  2748. * a non-zero default length.
  2749. */
  2750. if (smp_req->req_len == 0) {
  2751. switch (smp_req->func) {
  2752. case SMP_DISCOVER:
  2753. case SMP_REPORT_PHY_ERR_LOG:
  2754. case SMP_REPORT_PHY_SATA:
  2755. case SMP_REPORT_ROUTE_INFO:
  2756. smp_req->req_len = 2;
  2757. break;
  2758. case SMP_CONF_ROUTE_INFO:
  2759. case SMP_PHY_CONTROL:
  2760. case SMP_PHY_TEST_FUNCTION:
  2761. smp_req->req_len = 9;
  2762. break;
  2763. /* Default - zero is a valid default for 2.0. */
  2764. }
  2765. }
  2766. req_len = smp_req->req_len;
  2767. sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
  2768. cmd = *(u32 *) smp_req;
  2769. kunmap_atomic(kaddr, KM_IRQ0);
  2770. if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
  2771. return SCI_FAILURE;
  2772. sci_req->protocol = SCIC_SMP_PROTOCOL;
  2773. /* byte swap the smp request. */
  2774. task_context = scic_sds_request_get_task_context(sci_req);
  2775. sci_dev = scic_sds_request_get_device(sci_req);
  2776. sci_port = scic_sds_request_get_port(sci_req);
  2777. /*
  2778. * Fill in the TC with the its required data
  2779. * 00h
  2780. */
  2781. task_context->priority = 0;
  2782. task_context->initiator_request = 1;
  2783. task_context->connection_rate = sci_dev->connection_rate;
  2784. task_context->protocol_engine_index =
  2785. scic_sds_controller_get_protocol_engine_group(scic);
  2786. task_context->logical_port_index = scic_sds_port_get_index(sci_port);
  2787. task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
  2788. task_context->abort = 0;
  2789. task_context->valid = SCU_TASK_CONTEXT_VALID;
  2790. task_context->context_type = SCU_TASK_CONTEXT_TYPE;
  2791. /* 04h */
  2792. task_context->remote_node_index = sci_dev->rnc.remote_node_index;
  2793. task_context->command_code = 0;
  2794. task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
  2795. /* 08h */
  2796. task_context->link_layer_control = 0;
  2797. task_context->do_not_dma_ssp_good_response = 1;
  2798. task_context->strict_ordering = 0;
  2799. task_context->control_frame = 1;
  2800. task_context->timeout_enable = 0;
  2801. task_context->block_guard_enable = 0;
  2802. /* 0ch */
  2803. task_context->address_modifier = 0;
  2804. /* 10h */
  2805. task_context->ssp_command_iu_length = req_len;
  2806. /* 14h */
  2807. task_context->transfer_length_bytes = 0;
  2808. /*
  2809. * 18h ~ 30h, protocol specific
  2810. * since commandIU has been build by framework at this point, we just
  2811. * copy the frist DWord from command IU to this location. */
  2812. memcpy(&task_context->type.smp, &cmd, sizeof(u32));
  2813. /*
  2814. * 40h
  2815. * "For SMP you could program it to zero. We would prefer that way
  2816. * so that done code will be consistent." - Venki
  2817. */
  2818. task_context->task_phase = 0;
  2819. if (sci_req->was_tag_assigned_by_user) {
  2820. /*
  2821. * Build the task context now since we have already read
  2822. * the data
  2823. */
  2824. sci_req->post_context =
  2825. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  2826. (scic_sds_controller_get_protocol_engine_group(scic) <<
  2827. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  2828. (scic_sds_port_get_index(sci_port) <<
  2829. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  2830. ISCI_TAG_TCI(sci_req->io_tag));
  2831. } else {
  2832. /*
  2833. * Build the task context now since we have already read
  2834. * the data.
  2835. * I/O tag index is not assigned because we have to wait
  2836. * until we get a TCi.
  2837. */
  2838. sci_req->post_context =
  2839. (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
  2840. (scic_sds_controller_get_protocol_engine_group(scic) <<
  2841. SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  2842. (scic_sds_port_get_index(sci_port) <<
  2843. SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
  2844. }
  2845. /*
  2846. * Copy the physical address for the command buffer to the SCU Task
  2847. * Context command buffer should not contain command header.
  2848. */
  2849. task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
  2850. task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
  2851. /* SMP response comes as UF, so no need to set response IU address. */
  2852. task_context->response_iu_upper = 0;
  2853. task_context->response_iu_lower = 0;
  2854. sci_change_state(&sci_req->sm, SCI_REQ_CONSTRUCTED);
  2855. return SCI_SUCCESS;
  2856. }
  2857. /*
  2858. * isci_smp_request_build() - This function builds the smp request.
  2859. * @ireq: This parameter points to the isci_request allocated in the
  2860. * request construct function.
  2861. *
  2862. * SCI_SUCCESS on successfull completion, or specific failure code.
  2863. */
  2864. static enum sci_status isci_smp_request_build(struct isci_request *ireq)
  2865. {
  2866. struct sas_task *task = isci_request_access_task(ireq);
  2867. struct device *dev = &ireq->isci_host->pdev->dev;
  2868. struct scic_sds_request *sci_req = &ireq->sci;
  2869. enum sci_status status = SCI_FAILURE;
  2870. status = scic_io_request_construct_smp(dev, sci_req, task);
  2871. if (status != SCI_SUCCESS)
  2872. dev_warn(&ireq->isci_host->pdev->dev,
  2873. "%s: failed with status = %d\n",
  2874. __func__,
  2875. status);
  2876. return status;
  2877. }
  2878. /**
  2879. * isci_io_request_build() - This function builds the io request object.
  2880. * @isci_host: This parameter specifies the ISCI host object
  2881. * @request: This parameter points to the isci_request object allocated in the
  2882. * request construct function.
  2883. * @sci_device: This parameter is the handle for the sci core's remote device
  2884. * object that is the destination for this request.
  2885. *
  2886. * SCI_SUCCESS on successfull completion, or specific failure code.
  2887. */
  2888. static enum sci_status isci_io_request_build(
  2889. struct isci_host *isci_host,
  2890. struct isci_request *request,
  2891. struct isci_remote_device *isci_device)
  2892. {
  2893. enum sci_status status = SCI_SUCCESS;
  2894. struct sas_task *task = isci_request_access_task(request);
  2895. struct scic_sds_remote_device *sci_device = &isci_device->sci;
  2896. dev_dbg(&isci_host->pdev->dev,
  2897. "%s: isci_device = 0x%p; request = %p, "
  2898. "num_scatter = %d\n",
  2899. __func__,
  2900. isci_device,
  2901. request,
  2902. task->num_scatter);
  2903. /* map the sgl addresses, if present.
  2904. * libata does the mapping for sata devices
  2905. * before we get the request.
  2906. */
  2907. if (task->num_scatter &&
  2908. !sas_protocol_ata(task->task_proto) &&
  2909. !(SAS_PROTOCOL_SMP & task->task_proto)) {
  2910. request->num_sg_entries = dma_map_sg(
  2911. &isci_host->pdev->dev,
  2912. task->scatter,
  2913. task->num_scatter,
  2914. task->data_dir
  2915. );
  2916. if (request->num_sg_entries == 0)
  2917. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  2918. }
  2919. /* build the common request object. For now,
  2920. * we will let the core allocate the IO tag.
  2921. */
  2922. status = scic_io_request_construct(&isci_host->sci, sci_device,
  2923. SCI_CONTROLLER_INVALID_IO_TAG,
  2924. &request->sci);
  2925. if (status != SCI_SUCCESS) {
  2926. dev_warn(&isci_host->pdev->dev,
  2927. "%s: failed request construct\n",
  2928. __func__);
  2929. return SCI_FAILURE;
  2930. }
  2931. switch (task->task_proto) {
  2932. case SAS_PROTOCOL_SMP:
  2933. status = isci_smp_request_build(request);
  2934. break;
  2935. case SAS_PROTOCOL_SSP:
  2936. status = isci_request_ssp_request_construct(request);
  2937. break;
  2938. case SAS_PROTOCOL_SATA:
  2939. case SAS_PROTOCOL_STP:
  2940. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  2941. status = isci_request_stp_request_construct(request);
  2942. break;
  2943. default:
  2944. dev_warn(&isci_host->pdev->dev,
  2945. "%s: unknown protocol\n", __func__);
  2946. return SCI_FAILURE;
  2947. }
  2948. return SCI_SUCCESS;
  2949. }
  2950. static struct isci_request *isci_request_alloc_core(struct isci_host *ihost,
  2951. gfp_t gfp_flags)
  2952. {
  2953. dma_addr_t handle;
  2954. struct isci_request *ireq;
  2955. ireq = dma_pool_alloc(ihost->dma_pool, gfp_flags, &handle);
  2956. if (!ireq) {
  2957. dev_warn(&ihost->pdev->dev,
  2958. "%s: dma_pool_alloc returned NULL\n", __func__);
  2959. return NULL;
  2960. }
  2961. /* initialize the request object. */
  2962. spin_lock_init(&ireq->state_lock);
  2963. ireq->request_daddr = handle;
  2964. ireq->isci_host = ihost;
  2965. ireq->io_request_completion = NULL;
  2966. ireq->terminated = false;
  2967. ireq->num_sg_entries = 0;
  2968. ireq->complete_in_target = false;
  2969. INIT_LIST_HEAD(&ireq->completed_node);
  2970. INIT_LIST_HEAD(&ireq->dev_node);
  2971. isci_request_change_state(ireq, allocated);
  2972. return ireq;
  2973. }
  2974. static struct isci_request *isci_request_alloc_io(struct isci_host *ihost,
  2975. struct sas_task *task,
  2976. gfp_t gfp_flags)
  2977. {
  2978. struct isci_request *ireq;
  2979. ireq = isci_request_alloc_core(ihost, gfp_flags);
  2980. if (ireq) {
  2981. ireq->ttype_ptr.io_task_ptr = task;
  2982. ireq->ttype = io_task;
  2983. task->lldd_task = ireq;
  2984. }
  2985. return ireq;
  2986. }
  2987. struct isci_request *isci_request_alloc_tmf(struct isci_host *ihost,
  2988. struct isci_tmf *isci_tmf,
  2989. gfp_t gfp_flags)
  2990. {
  2991. struct isci_request *ireq;
  2992. ireq = isci_request_alloc_core(ihost, gfp_flags);
  2993. if (ireq) {
  2994. ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
  2995. ireq->ttype = tmf_task;
  2996. }
  2997. return ireq;
  2998. }
  2999. int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
  3000. struct sas_task *task, gfp_t gfp_flags)
  3001. {
  3002. enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  3003. struct isci_request *ireq;
  3004. unsigned long flags;
  3005. int ret = 0;
  3006. /* do common allocation and init of request object. */
  3007. ireq = isci_request_alloc_io(ihost, task, gfp_flags);
  3008. if (!ireq)
  3009. goto out;
  3010. status = isci_io_request_build(ihost, ireq, idev);
  3011. if (status != SCI_SUCCESS) {
  3012. dev_warn(&ihost->pdev->dev,
  3013. "%s: request_construct failed - status = 0x%x\n",
  3014. __func__,
  3015. status);
  3016. goto out;
  3017. }
  3018. spin_lock_irqsave(&ihost->scic_lock, flags);
  3019. if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
  3020. if (isci_task_is_ncq_recovery(task)) {
  3021. /* The device is in an NCQ recovery state. Issue the
  3022. * request on the task side. Note that it will
  3023. * complete on the I/O request side because the
  3024. * request was built that way (ie.
  3025. * ireq->is_task_management_request is false).
  3026. */
  3027. status = scic_controller_start_task(&ihost->sci,
  3028. &idev->sci,
  3029. &ireq->sci,
  3030. SCI_CONTROLLER_INVALID_IO_TAG);
  3031. } else {
  3032. status = SCI_FAILURE;
  3033. }
  3034. } else {
  3035. /* send the request, let the core assign the IO TAG. */
  3036. status = scic_controller_start_io(&ihost->sci, &idev->sci,
  3037. &ireq->sci,
  3038. SCI_CONTROLLER_INVALID_IO_TAG);
  3039. }
  3040. if (status != SCI_SUCCESS &&
  3041. status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3042. dev_warn(&ihost->pdev->dev,
  3043. "%s: failed request start (0x%x)\n",
  3044. __func__, status);
  3045. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  3046. goto out;
  3047. }
  3048. /* Either I/O started OK, or the core has signaled that
  3049. * the device needs a target reset.
  3050. *
  3051. * In either case, hold onto the I/O for later.
  3052. *
  3053. * Update it's status and add it to the list in the
  3054. * remote device object.
  3055. */
  3056. list_add(&ireq->dev_node, &idev->reqs_in_process);
  3057. if (status == SCI_SUCCESS) {
  3058. /* Save the tag for possible task mgmt later. */
  3059. ireq->io_tag = ireq->sci.io_tag;
  3060. isci_request_change_state(ireq, started);
  3061. } else {
  3062. /* The request did not really start in the
  3063. * hardware, so clear the request handle
  3064. * here so no terminations will be done.
  3065. */
  3066. ireq->terminated = true;
  3067. isci_request_change_state(ireq, completed);
  3068. }
  3069. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  3070. if (status ==
  3071. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  3072. /* Signal libsas that we need the SCSI error
  3073. * handler thread to work on this I/O and that
  3074. * we want a device reset.
  3075. */
  3076. spin_lock_irqsave(&task->task_state_lock, flags);
  3077. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  3078. spin_unlock_irqrestore(&task->task_state_lock, flags);
  3079. /* Cause this task to be scheduled in the SCSI error
  3080. * handler thread.
  3081. */
  3082. isci_execpath_callback(ihost, task,
  3083. sas_task_abort);
  3084. /* Change the status, since we are holding
  3085. * the I/O until it is managed by the SCSI
  3086. * error handler.
  3087. */
  3088. status = SCI_SUCCESS;
  3089. }
  3090. out:
  3091. if (status != SCI_SUCCESS) {
  3092. /* release dma memory on failure. */
  3093. isci_request_free(ihost, ireq);
  3094. ireq = NULL;
  3095. ret = SCI_FAILURE;
  3096. }
  3097. return ret;
  3098. }