request.c 106 KB

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