request.c 108 KB

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