request.c 109 KB

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