mtip32xx.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295
  1. /*
  2. * Driver for the Micron P320 SSD
  3. * Copyright (C) 2011 Micron Technology, Inc.
  4. *
  5. * Portions of this code were derived from works subjected to the
  6. * following copyright:
  7. * Copyright (C) 2009 Integrated Device Technology, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/ata.h>
  23. #include <linux/delay.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/random.h>
  27. #include <linux/smp.h>
  28. #include <linux/compat.h>
  29. #include <linux/fs.h>
  30. #include <linux/module.h>
  31. #include <linux/genhd.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/bio.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/idr.h>
  36. #include <linux/kthread.h>
  37. #include <../drivers/ata/ahci.h>
  38. #include <linux/export.h>
  39. #include <linux/debugfs.h>
  40. #include "mtip32xx.h"
  41. #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
  42. #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
  43. #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
  44. #define HW_PORT_PRIV_DMA_SZ \
  45. (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
  46. #define HOST_CAP_NZDMA (1 << 19)
  47. #define HOST_HSORG 0xFC
  48. #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
  49. #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
  50. #define HSORG_HWREV 0xFF00
  51. #define HSORG_STYLE 0x8
  52. #define HSORG_SLOTGROUPS 0x7
  53. #define PORT_COMMAND_ISSUE 0x38
  54. #define PORT_SDBV 0x7C
  55. #define PORT_OFFSET 0x100
  56. #define PORT_MEM_SIZE 0x80
  57. #define PORT_IRQ_ERR \
  58. (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
  59. PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
  60. PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
  61. PORT_IRQ_OVERFLOW)
  62. #define PORT_IRQ_LEGACY \
  63. (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
  64. #define PORT_IRQ_HANDLED \
  65. (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
  66. PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
  67. PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
  68. #define DEF_PORT_IRQ \
  69. (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
  70. /* product numbers */
  71. #define MTIP_PRODUCT_UNKNOWN 0x00
  72. #define MTIP_PRODUCT_ASICFPGA 0x11
  73. /* Device instance number, incremented each time a device is probed. */
  74. static int instance;
  75. /*
  76. * Global variable used to hold the major block device number
  77. * allocated in mtip_init().
  78. */
  79. static int mtip_major;
  80. static struct dentry *dfs_parent;
  81. static DEFINE_SPINLOCK(rssd_index_lock);
  82. static DEFINE_IDA(rssd_index_ida);
  83. static int mtip_block_initialize(struct driver_data *dd);
  84. #ifdef CONFIG_COMPAT
  85. struct mtip_compat_ide_task_request_s {
  86. __u8 io_ports[8];
  87. __u8 hob_ports[8];
  88. ide_reg_valid_t out_flags;
  89. ide_reg_valid_t in_flags;
  90. int data_phase;
  91. int req_cmd;
  92. compat_ulong_t out_size;
  93. compat_ulong_t in_size;
  94. };
  95. #endif
  96. /*
  97. * This function check_for_surprise_removal is called
  98. * while card is removed from the system and it will
  99. * read the vendor id from the configration space
  100. *
  101. * @pdev Pointer to the pci_dev structure.
  102. *
  103. * return value
  104. * true if device removed, else false
  105. */
  106. static bool mtip_check_surprise_removal(struct pci_dev *pdev)
  107. {
  108. u16 vendor_id = 0;
  109. /* Read the vendorID from the configuration space */
  110. pci_read_config_word(pdev, 0x00, &vendor_id);
  111. if (vendor_id == 0xFFFF)
  112. return true; /* device removed */
  113. return false; /* device present */
  114. }
  115. /*
  116. * This function is called for clean the pending command in the
  117. * command slot during the surprise removal of device and return
  118. * error to the upper layer.
  119. *
  120. * @dd Pointer to the DRIVER_DATA structure.
  121. *
  122. * return value
  123. * None
  124. */
  125. static void mtip_command_cleanup(struct driver_data *dd)
  126. {
  127. int group = 0, commandslot = 0, commandindex = 0;
  128. struct mtip_cmd *command;
  129. struct mtip_port *port = dd->port;
  130. static int in_progress;
  131. if (in_progress)
  132. return;
  133. in_progress = 1;
  134. for (group = 0; group < 4; group++) {
  135. for (commandslot = 0; commandslot < 32; commandslot++) {
  136. if (!(port->allocated[group] & (1 << commandslot)))
  137. continue;
  138. commandindex = group << 5 | commandslot;
  139. command = &port->commands[commandindex];
  140. if (atomic_read(&command->active)
  141. && (command->async_callback)) {
  142. command->async_callback(command->async_data,
  143. -ENODEV);
  144. command->async_callback = NULL;
  145. command->async_data = NULL;
  146. }
  147. dma_unmap_sg(&port->dd->pdev->dev,
  148. command->sg,
  149. command->scatter_ents,
  150. command->direction);
  151. }
  152. }
  153. up(&port->cmd_slot);
  154. set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
  155. in_progress = 0;
  156. }
  157. /*
  158. * Obtain an empty command slot.
  159. *
  160. * This function needs to be reentrant since it could be called
  161. * at the same time on multiple CPUs. The allocation of the
  162. * command slot must be atomic.
  163. *
  164. * @port Pointer to the port data structure.
  165. *
  166. * return value
  167. * >= 0 Index of command slot obtained.
  168. * -1 No command slots available.
  169. */
  170. static int get_slot(struct mtip_port *port)
  171. {
  172. int slot, i;
  173. unsigned int num_command_slots = port->dd->slot_groups * 32;
  174. /*
  175. * Try 10 times, because there is a small race here.
  176. * that's ok, because it's still cheaper than a lock.
  177. *
  178. * Race: Since this section is not protected by lock, same bit
  179. * could be chosen by different process contexts running in
  180. * different processor. So instead of costly lock, we are going
  181. * with loop.
  182. */
  183. for (i = 0; i < 10; i++) {
  184. slot = find_next_zero_bit(port->allocated,
  185. num_command_slots, 1);
  186. if ((slot < num_command_slots) &&
  187. (!test_and_set_bit(slot, port->allocated)))
  188. return slot;
  189. }
  190. dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
  191. if (mtip_check_surprise_removal(port->dd->pdev)) {
  192. /* Device not present, clean outstanding commands */
  193. mtip_command_cleanup(port->dd);
  194. }
  195. return -1;
  196. }
  197. /*
  198. * Release a command slot.
  199. *
  200. * @port Pointer to the port data structure.
  201. * @tag Tag of command to release
  202. *
  203. * return value
  204. * None
  205. */
  206. static inline void release_slot(struct mtip_port *port, int tag)
  207. {
  208. smp_mb__before_clear_bit();
  209. clear_bit(tag, port->allocated);
  210. smp_mb__after_clear_bit();
  211. }
  212. /*
  213. * Reset the HBA (without sleeping)
  214. *
  215. * Just like hba_reset, except does not call sleep, so can be
  216. * run from interrupt/tasklet context.
  217. *
  218. * @dd Pointer to the driver data structure.
  219. *
  220. * return value
  221. * 0 The reset was successful.
  222. * -1 The HBA Reset bit did not clear.
  223. */
  224. static int hba_reset_nosleep(struct driver_data *dd)
  225. {
  226. unsigned long timeout;
  227. /* Chip quirk: quiesce any chip function */
  228. mdelay(10);
  229. /* Set the reset bit */
  230. writel(HOST_RESET, dd->mmio + HOST_CTL);
  231. /* Flush */
  232. readl(dd->mmio + HOST_CTL);
  233. /*
  234. * Wait 10ms then spin for up to 1 second
  235. * waiting for reset acknowledgement
  236. */
  237. timeout = jiffies + msecs_to_jiffies(1000);
  238. mdelay(10);
  239. while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
  240. && time_before(jiffies, timeout))
  241. mdelay(1);
  242. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
  243. return -1;
  244. if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
  245. return -1;
  246. return 0;
  247. }
  248. /*
  249. * Issue a command to the hardware.
  250. *
  251. * Set the appropriate bit in the s_active and Command Issue hardware
  252. * registers, causing hardware command processing to begin.
  253. *
  254. * @port Pointer to the port structure.
  255. * @tag The tag of the command to be issued.
  256. *
  257. * return value
  258. * None
  259. */
  260. static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
  261. {
  262. atomic_set(&port->commands[tag].active, 1);
  263. spin_lock(&port->cmd_issue_lock);
  264. writel((1 << MTIP_TAG_BIT(tag)),
  265. port->s_active[MTIP_TAG_INDEX(tag)]);
  266. writel((1 << MTIP_TAG_BIT(tag)),
  267. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  268. spin_unlock(&port->cmd_issue_lock);
  269. /* Set the command's timeout value.*/
  270. port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
  271. MTIP_NCQ_COMMAND_TIMEOUT_MS);
  272. }
  273. /*
  274. * Enable/disable the reception of FIS
  275. *
  276. * @port Pointer to the port data structure
  277. * @enable 1 to enable, 0 to disable
  278. *
  279. * return value
  280. * Previous state: 1 enabled, 0 disabled
  281. */
  282. static int mtip_enable_fis(struct mtip_port *port, int enable)
  283. {
  284. u32 tmp;
  285. /* enable FIS reception */
  286. tmp = readl(port->mmio + PORT_CMD);
  287. if (enable)
  288. writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  289. else
  290. writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  291. /* Flush */
  292. readl(port->mmio + PORT_CMD);
  293. return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
  294. }
  295. /*
  296. * Enable/disable the DMA engine
  297. *
  298. * @port Pointer to the port data structure
  299. * @enable 1 to enable, 0 to disable
  300. *
  301. * return value
  302. * Previous state: 1 enabled, 0 disabled.
  303. */
  304. static int mtip_enable_engine(struct mtip_port *port, int enable)
  305. {
  306. u32 tmp;
  307. /* enable FIS reception */
  308. tmp = readl(port->mmio + PORT_CMD);
  309. if (enable)
  310. writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
  311. else
  312. writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
  313. readl(port->mmio + PORT_CMD);
  314. return (((tmp & PORT_CMD_START) == PORT_CMD_START));
  315. }
  316. /*
  317. * Enables the port DMA engine and FIS reception.
  318. *
  319. * return value
  320. * None
  321. */
  322. static inline void mtip_start_port(struct mtip_port *port)
  323. {
  324. /* Enable FIS reception */
  325. mtip_enable_fis(port, 1);
  326. /* Enable the DMA engine */
  327. mtip_enable_engine(port, 1);
  328. }
  329. /*
  330. * Deinitialize a port by disabling port interrupts, the DMA engine,
  331. * and FIS reception.
  332. *
  333. * @port Pointer to the port structure
  334. *
  335. * return value
  336. * None
  337. */
  338. static inline void mtip_deinit_port(struct mtip_port *port)
  339. {
  340. /* Disable interrupts on this port */
  341. writel(0, port->mmio + PORT_IRQ_MASK);
  342. /* Disable the DMA engine */
  343. mtip_enable_engine(port, 0);
  344. /* Disable FIS reception */
  345. mtip_enable_fis(port, 0);
  346. }
  347. /*
  348. * Initialize a port.
  349. *
  350. * This function deinitializes the port by calling mtip_deinit_port() and
  351. * then initializes it by setting the command header and RX FIS addresses,
  352. * clearing the SError register and any pending port interrupts before
  353. * re-enabling the default set of port interrupts.
  354. *
  355. * @port Pointer to the port structure.
  356. *
  357. * return value
  358. * None
  359. */
  360. static void mtip_init_port(struct mtip_port *port)
  361. {
  362. int i;
  363. mtip_deinit_port(port);
  364. /* Program the command list base and FIS base addresses */
  365. if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
  366. writel((port->command_list_dma >> 16) >> 16,
  367. port->mmio + PORT_LST_ADDR_HI);
  368. writel((port->rxfis_dma >> 16) >> 16,
  369. port->mmio + PORT_FIS_ADDR_HI);
  370. }
  371. writel(port->command_list_dma & 0xFFFFFFFF,
  372. port->mmio + PORT_LST_ADDR);
  373. writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
  374. /* Clear SError */
  375. writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
  376. /* reset the completed registers.*/
  377. for (i = 0; i < port->dd->slot_groups; i++)
  378. writel(0xFFFFFFFF, port->completed[i]);
  379. /* Clear any pending interrupts for this port */
  380. writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
  381. /* Clear any pending interrupts on the HBA. */
  382. writel(readl(port->dd->mmio + HOST_IRQ_STAT),
  383. port->dd->mmio + HOST_IRQ_STAT);
  384. /* Enable port interrupts */
  385. writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
  386. }
  387. /*
  388. * Restart a port
  389. *
  390. * @port Pointer to the port data structure.
  391. *
  392. * return value
  393. * None
  394. */
  395. static void mtip_restart_port(struct mtip_port *port)
  396. {
  397. unsigned long timeout;
  398. /* Disable the DMA engine */
  399. mtip_enable_engine(port, 0);
  400. /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
  401. timeout = jiffies + msecs_to_jiffies(500);
  402. while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
  403. && time_before(jiffies, timeout))
  404. ;
  405. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  406. return;
  407. /*
  408. * Chip quirk: escalate to hba reset if
  409. * PxCMD.CR not clear after 500 ms
  410. */
  411. if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
  412. dev_warn(&port->dd->pdev->dev,
  413. "PxCMD.CR not clear, escalating reset\n");
  414. if (hba_reset_nosleep(port->dd))
  415. dev_err(&port->dd->pdev->dev,
  416. "HBA reset escalation failed.\n");
  417. /* 30 ms delay before com reset to quiesce chip */
  418. mdelay(30);
  419. }
  420. dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
  421. /* Set PxSCTL.DET */
  422. writel(readl(port->mmio + PORT_SCR_CTL) |
  423. 1, port->mmio + PORT_SCR_CTL);
  424. readl(port->mmio + PORT_SCR_CTL);
  425. /* Wait 1 ms to quiesce chip function */
  426. timeout = jiffies + msecs_to_jiffies(1);
  427. while (time_before(jiffies, timeout))
  428. ;
  429. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  430. return;
  431. /* Clear PxSCTL.DET */
  432. writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
  433. port->mmio + PORT_SCR_CTL);
  434. readl(port->mmio + PORT_SCR_CTL);
  435. /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
  436. timeout = jiffies + msecs_to_jiffies(500);
  437. while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  438. && time_before(jiffies, timeout))
  439. ;
  440. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  441. return;
  442. if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  443. dev_warn(&port->dd->pdev->dev,
  444. "COM reset failed\n");
  445. mtip_init_port(port);
  446. mtip_start_port(port);
  447. }
  448. /*
  449. * Helper function for tag logging
  450. */
  451. static void print_tags(struct driver_data *dd,
  452. char *msg,
  453. unsigned long *tagbits,
  454. int cnt)
  455. {
  456. unsigned char tagmap[128];
  457. int group, tagmap_len = 0;
  458. memset(tagmap, 0, sizeof(tagmap));
  459. for (group = SLOTBITS_IN_LONGS; group > 0; group--)
  460. tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
  461. tagbits[group-1]);
  462. dev_warn(&dd->pdev->dev,
  463. "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
  464. }
  465. /*
  466. * Called periodically to see if any read/write commands are
  467. * taking too long to complete.
  468. *
  469. * @data Pointer to the PORT data structure.
  470. *
  471. * return value
  472. * None
  473. */
  474. static void mtip_timeout_function(unsigned long int data)
  475. {
  476. struct mtip_port *port = (struct mtip_port *) data;
  477. struct host_to_dev_fis *fis;
  478. struct mtip_cmd *command;
  479. int tag, cmdto_cnt = 0;
  480. unsigned int bit, group;
  481. unsigned int num_command_slots;
  482. unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
  483. if (unlikely(!port))
  484. return;
  485. if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
  486. mod_timer(&port->cmd_timer,
  487. jiffies + msecs_to_jiffies(30000));
  488. return;
  489. }
  490. /* clear the tag accumulator */
  491. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  492. num_command_slots = port->dd->slot_groups * 32;
  493. for (tag = 0; tag < num_command_slots; tag++) {
  494. /*
  495. * Skip internal command slot as it has
  496. * its own timeout mechanism
  497. */
  498. if (tag == MTIP_TAG_INTERNAL)
  499. continue;
  500. if (atomic_read(&port->commands[tag].active) &&
  501. (time_after(jiffies, port->commands[tag].comp_time))) {
  502. group = tag >> 5;
  503. bit = tag & 0x1F;
  504. command = &port->commands[tag];
  505. fis = (struct host_to_dev_fis *) command->command;
  506. set_bit(tag, tagaccum);
  507. cmdto_cnt++;
  508. if (cmdto_cnt == 1)
  509. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  510. /*
  511. * Clear the completed bit. This should prevent
  512. * any interrupt handlers from trying to retire
  513. * the command.
  514. */
  515. writel(1 << bit, port->completed[group]);
  516. /* Call the async completion callback. */
  517. if (likely(command->async_callback))
  518. command->async_callback(command->async_data,
  519. -EIO);
  520. command->async_callback = NULL;
  521. command->comp_func = NULL;
  522. /* Unmap the DMA scatter list entries */
  523. dma_unmap_sg(&port->dd->pdev->dev,
  524. command->sg,
  525. command->scatter_ents,
  526. command->direction);
  527. /*
  528. * Clear the allocated bit and active tag for the
  529. * command.
  530. */
  531. atomic_set(&port->commands[tag].active, 0);
  532. release_slot(port, tag);
  533. up(&port->cmd_slot);
  534. }
  535. }
  536. if (cmdto_cnt) {
  537. print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
  538. if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  539. mtip_restart_port(port);
  540. wake_up_interruptible(&port->svc_wait);
  541. }
  542. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  543. }
  544. if (port->ic_pause_timer) {
  545. to = port->ic_pause_timer + msecs_to_jiffies(1000);
  546. if (time_after(jiffies, to)) {
  547. if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  548. port->ic_pause_timer = 0;
  549. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  550. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  551. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  552. wake_up_interruptible(&port->svc_wait);
  553. }
  554. }
  555. }
  556. /* Restart the timer */
  557. mod_timer(&port->cmd_timer,
  558. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  559. }
  560. /*
  561. * IO completion function.
  562. *
  563. * This completion function is called by the driver ISR when a
  564. * command that was issued by the kernel completes. It first calls the
  565. * asynchronous completion function which normally calls back into the block
  566. * layer passing the asynchronous callback data, then unmaps the
  567. * scatter list associated with the completed command, and finally
  568. * clears the allocated bit associated with the completed command.
  569. *
  570. * @port Pointer to the port data structure.
  571. * @tag Tag of the command.
  572. * @data Pointer to driver_data.
  573. * @status Completion status.
  574. *
  575. * return value
  576. * None
  577. */
  578. static void mtip_async_complete(struct mtip_port *port,
  579. int tag,
  580. void *data,
  581. int status)
  582. {
  583. struct mtip_cmd *command;
  584. struct driver_data *dd = data;
  585. int cb_status = status ? -EIO : 0;
  586. if (unlikely(!dd) || unlikely(!port))
  587. return;
  588. command = &port->commands[tag];
  589. if (unlikely(status == PORT_IRQ_TF_ERR)) {
  590. dev_warn(&port->dd->pdev->dev,
  591. "Command tag %d failed due to TFE\n", tag);
  592. }
  593. /* Upper layer callback */
  594. if (likely(command->async_callback))
  595. command->async_callback(command->async_data, cb_status);
  596. command->async_callback = NULL;
  597. command->comp_func = NULL;
  598. /* Unmap the DMA scatter list entries */
  599. dma_unmap_sg(&dd->pdev->dev,
  600. command->sg,
  601. command->scatter_ents,
  602. command->direction);
  603. /* Clear the allocated and active bits for the command */
  604. atomic_set(&port->commands[tag].active, 0);
  605. release_slot(port, tag);
  606. up(&port->cmd_slot);
  607. }
  608. /*
  609. * Internal command completion callback function.
  610. *
  611. * This function is normally called by the driver ISR when an internal
  612. * command completed. This function signals the command completion by
  613. * calling complete().
  614. *
  615. * @port Pointer to the port data structure.
  616. * @tag Tag of the command that has completed.
  617. * @data Pointer to a completion structure.
  618. * @status Completion status.
  619. *
  620. * return value
  621. * None
  622. */
  623. static void mtip_completion(struct mtip_port *port,
  624. int tag,
  625. void *data,
  626. int status)
  627. {
  628. struct mtip_cmd *command = &port->commands[tag];
  629. struct completion *waiting = data;
  630. if (unlikely(status == PORT_IRQ_TF_ERR))
  631. dev_warn(&port->dd->pdev->dev,
  632. "Internal command %d completed with TFE\n", tag);
  633. command->async_callback = NULL;
  634. command->comp_func = NULL;
  635. complete(waiting);
  636. }
  637. static void mtip_null_completion(struct mtip_port *port,
  638. int tag,
  639. void *data,
  640. int status)
  641. {
  642. return;
  643. }
  644. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  645. dma_addr_t buffer_dma, unsigned int sectors);
  646. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  647. struct smart_attr *attrib);
  648. /*
  649. * Handle an error.
  650. *
  651. * @dd Pointer to the DRIVER_DATA structure.
  652. *
  653. * return value
  654. * None
  655. */
  656. static void mtip_handle_tfe(struct driver_data *dd)
  657. {
  658. int group, tag, bit, reissue, rv;
  659. struct mtip_port *port;
  660. struct mtip_cmd *cmd;
  661. u32 completed;
  662. struct host_to_dev_fis *fis;
  663. unsigned long tagaccum[SLOTBITS_IN_LONGS];
  664. unsigned int cmd_cnt = 0;
  665. unsigned char *buf;
  666. char *fail_reason = NULL;
  667. int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
  668. dev_warn(&dd->pdev->dev, "Taskfile error\n");
  669. port = dd->port;
  670. /* Stop the timer to prevent command timeouts. */
  671. del_timer(&port->cmd_timer);
  672. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  673. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  674. test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
  675. cmd = &port->commands[MTIP_TAG_INTERNAL];
  676. dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
  677. atomic_inc(&cmd->active); /* active > 1 indicates error */
  678. if (cmd->comp_data && cmd->comp_func) {
  679. cmd->comp_func(port, MTIP_TAG_INTERNAL,
  680. cmd->comp_data, PORT_IRQ_TF_ERR);
  681. }
  682. goto handle_tfe_exit;
  683. }
  684. /* clear the tag accumulator */
  685. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  686. /* Loop through all the groups */
  687. for (group = 0; group < dd->slot_groups; group++) {
  688. completed = readl(port->completed[group]);
  689. /* clear completed status register in the hardware.*/
  690. writel(completed, port->completed[group]);
  691. /* Process successfully completed commands */
  692. for (bit = 0; bit < 32 && completed; bit++) {
  693. if (!(completed & (1<<bit)))
  694. continue;
  695. tag = (group << 5) + bit;
  696. /* Skip the internal command slot */
  697. if (tag == MTIP_TAG_INTERNAL)
  698. continue;
  699. cmd = &port->commands[tag];
  700. if (likely(cmd->comp_func)) {
  701. set_bit(tag, tagaccum);
  702. cmd_cnt++;
  703. atomic_set(&cmd->active, 0);
  704. cmd->comp_func(port,
  705. tag,
  706. cmd->comp_data,
  707. 0);
  708. } else {
  709. dev_err(&port->dd->pdev->dev,
  710. "Missing completion func for tag %d",
  711. tag);
  712. if (mtip_check_surprise_removal(dd->pdev)) {
  713. mtip_command_cleanup(dd);
  714. /* don't proceed further */
  715. return;
  716. }
  717. }
  718. }
  719. }
  720. print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
  721. /* Restart the port */
  722. mdelay(20);
  723. mtip_restart_port(port);
  724. /* Trying to determine the cause of the error */
  725. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  726. dd->port->log_buf,
  727. dd->port->log_buf_dma, 1);
  728. if (rv) {
  729. dev_warn(&dd->pdev->dev,
  730. "Error in READ LOG EXT (10h) command\n");
  731. /* non-critical error, don't fail the load */
  732. } else {
  733. buf = (unsigned char *)dd->port->log_buf;
  734. if (buf[259] & 0x1) {
  735. dev_info(&dd->pdev->dev,
  736. "Write protect bit is set.\n");
  737. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  738. fail_all_ncq_write = 1;
  739. fail_reason = "write protect";
  740. }
  741. if (buf[288] == 0xF7) {
  742. dev_info(&dd->pdev->dev,
  743. "Exceeded Tmax, drive in thermal shutdown.\n");
  744. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  745. fail_all_ncq_cmds = 1;
  746. fail_reason = "thermal shutdown";
  747. }
  748. if (buf[288] == 0xBF) {
  749. dev_info(&dd->pdev->dev,
  750. "Drive indicates rebuild has failed.\n");
  751. fail_all_ncq_cmds = 1;
  752. fail_reason = "rebuild failed";
  753. }
  754. }
  755. /* clear the tag accumulator */
  756. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  757. /* Loop through all the groups */
  758. for (group = 0; group < dd->slot_groups; group++) {
  759. for (bit = 0; bit < 32; bit++) {
  760. reissue = 1;
  761. tag = (group << 5) + bit;
  762. cmd = &port->commands[tag];
  763. /* If the active bit is set re-issue the command */
  764. if (atomic_read(&cmd->active) == 0)
  765. continue;
  766. fis = (struct host_to_dev_fis *)cmd->command;
  767. /* Should re-issue? */
  768. if (tag == MTIP_TAG_INTERNAL ||
  769. fis->command == ATA_CMD_SET_FEATURES)
  770. reissue = 0;
  771. else {
  772. if (fail_all_ncq_cmds ||
  773. (fail_all_ncq_write &&
  774. fis->command == ATA_CMD_FPDMA_WRITE)) {
  775. dev_warn(&dd->pdev->dev,
  776. " Fail: %s w/tag %d [%s].\n",
  777. fis->command == ATA_CMD_FPDMA_WRITE ?
  778. "write" : "read",
  779. tag,
  780. fail_reason != NULL ?
  781. fail_reason : "unknown");
  782. atomic_set(&cmd->active, 0);
  783. if (cmd->comp_func) {
  784. cmd->comp_func(port, tag,
  785. cmd->comp_data,
  786. -ENODATA);
  787. }
  788. continue;
  789. }
  790. }
  791. /*
  792. * First check if this command has
  793. * exceeded its retries.
  794. */
  795. if (reissue && (cmd->retries-- > 0)) {
  796. set_bit(tag, tagaccum);
  797. /* Re-issue the command. */
  798. mtip_issue_ncq_command(port, tag);
  799. continue;
  800. }
  801. /* Retire a command that will not be reissued */
  802. dev_warn(&port->dd->pdev->dev,
  803. "retiring tag %d\n", tag);
  804. atomic_set(&cmd->active, 0);
  805. if (cmd->comp_func)
  806. cmd->comp_func(
  807. port,
  808. tag,
  809. cmd->comp_data,
  810. PORT_IRQ_TF_ERR);
  811. else
  812. dev_warn(&port->dd->pdev->dev,
  813. "Bad completion for tag %d\n",
  814. tag);
  815. }
  816. }
  817. print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
  818. handle_tfe_exit:
  819. /* clear eh_active */
  820. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  821. wake_up_interruptible(&port->svc_wait);
  822. mod_timer(&port->cmd_timer,
  823. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  824. }
  825. /*
  826. * Handle a set device bits interrupt
  827. */
  828. static inline void mtip_process_sdbf(struct driver_data *dd)
  829. {
  830. struct mtip_port *port = dd->port;
  831. int group, tag, bit;
  832. u32 completed;
  833. struct mtip_cmd *command;
  834. /* walk all bits in all slot groups */
  835. for (group = 0; group < dd->slot_groups; group++) {
  836. completed = readl(port->completed[group]);
  837. if (!completed)
  838. continue;
  839. /* clear completed status register in the hardware.*/
  840. writel(completed, port->completed[group]);
  841. /* Process completed commands. */
  842. for (bit = 0;
  843. (bit < 32) && completed;
  844. bit++, completed >>= 1) {
  845. if (completed & 0x01) {
  846. tag = (group << 5) | bit;
  847. /* skip internal command slot. */
  848. if (unlikely(tag == MTIP_TAG_INTERNAL))
  849. continue;
  850. command = &port->commands[tag];
  851. /* make internal callback */
  852. if (likely(command->comp_func)) {
  853. command->comp_func(
  854. port,
  855. tag,
  856. command->comp_data,
  857. 0);
  858. } else {
  859. dev_warn(&dd->pdev->dev,
  860. "Null completion "
  861. "for tag %d",
  862. tag);
  863. if (mtip_check_surprise_removal(
  864. dd->pdev)) {
  865. mtip_command_cleanup(dd);
  866. return;
  867. }
  868. }
  869. }
  870. }
  871. }
  872. }
  873. /*
  874. * Process legacy pio and d2h interrupts
  875. */
  876. static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
  877. {
  878. struct mtip_port *port = dd->port;
  879. struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
  880. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  881. (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  882. & (1 << MTIP_TAG_INTERNAL))) {
  883. if (cmd->comp_func) {
  884. cmd->comp_func(port,
  885. MTIP_TAG_INTERNAL,
  886. cmd->comp_data,
  887. 0);
  888. return;
  889. }
  890. }
  891. return;
  892. }
  893. /*
  894. * Demux and handle errors
  895. */
  896. static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
  897. {
  898. if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
  899. mtip_handle_tfe(dd);
  900. if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
  901. dev_warn(&dd->pdev->dev,
  902. "Clearing PxSERR.DIAG.x\n");
  903. writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
  904. }
  905. if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
  906. dev_warn(&dd->pdev->dev,
  907. "Clearing PxSERR.DIAG.n\n");
  908. writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
  909. }
  910. if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
  911. dev_warn(&dd->pdev->dev,
  912. "Port stat errors %x unhandled\n",
  913. (port_stat & ~PORT_IRQ_HANDLED));
  914. }
  915. }
  916. static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
  917. {
  918. struct driver_data *dd = (struct driver_data *) data;
  919. struct mtip_port *port = dd->port;
  920. u32 hba_stat, port_stat;
  921. int rv = IRQ_NONE;
  922. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  923. if (hba_stat) {
  924. rv = IRQ_HANDLED;
  925. /* Acknowledge the interrupt status on the port.*/
  926. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  927. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  928. /* Demux port status */
  929. if (likely(port_stat & PORT_IRQ_SDB_FIS))
  930. mtip_process_sdbf(dd);
  931. if (unlikely(port_stat & PORT_IRQ_ERR)) {
  932. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  933. mtip_command_cleanup(dd);
  934. /* don't proceed further */
  935. return IRQ_HANDLED;
  936. }
  937. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  938. &dd->dd_flag))
  939. return rv;
  940. mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
  941. }
  942. if (unlikely(port_stat & PORT_IRQ_LEGACY))
  943. mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
  944. }
  945. /* acknowledge interrupt */
  946. writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
  947. return rv;
  948. }
  949. /*
  950. * Wrapper for mtip_handle_irq
  951. * (ignores return code)
  952. */
  953. static void mtip_tasklet(unsigned long data)
  954. {
  955. mtip_handle_irq((struct driver_data *) data);
  956. }
  957. /*
  958. * HBA interrupt subroutine.
  959. *
  960. * @irq IRQ number.
  961. * @instance Pointer to the driver data structure.
  962. *
  963. * return value
  964. * IRQ_HANDLED A HBA interrupt was pending and handled.
  965. * IRQ_NONE This interrupt was not for the HBA.
  966. */
  967. static irqreturn_t mtip_irq_handler(int irq, void *instance)
  968. {
  969. struct driver_data *dd = instance;
  970. tasklet_schedule(&dd->tasklet);
  971. return IRQ_HANDLED;
  972. }
  973. static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
  974. {
  975. atomic_set(&port->commands[tag].active, 1);
  976. writel(1 << MTIP_TAG_BIT(tag),
  977. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  978. }
  979. static bool mtip_pause_ncq(struct mtip_port *port,
  980. struct host_to_dev_fis *fis)
  981. {
  982. struct host_to_dev_fis *reply;
  983. unsigned long task_file_data;
  984. reply = port->rxfis + RX_FIS_D2H_REG;
  985. task_file_data = readl(port->mmio+PORT_TFDATA);
  986. if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
  987. clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  988. if ((task_file_data & 1))
  989. return false;
  990. if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
  991. set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  992. set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  993. port->ic_pause_timer = jiffies;
  994. return true;
  995. } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
  996. (fis->features == 0x03)) {
  997. set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  998. port->ic_pause_timer = jiffies;
  999. return true;
  1000. } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
  1001. ((fis->command == 0xFC) &&
  1002. (fis->features == 0x27 || fis->features == 0x72 ||
  1003. fis->features == 0x62 || fis->features == 0x26))) {
  1004. /* Com reset after secure erase or lowlevel format */
  1005. mtip_restart_port(port);
  1006. return false;
  1007. }
  1008. return false;
  1009. }
  1010. /*
  1011. * Wait for port to quiesce
  1012. *
  1013. * @port Pointer to port data structure
  1014. * @timeout Max duration to wait (ms)
  1015. *
  1016. * return value
  1017. * 0 Success
  1018. * -EBUSY Commands still active
  1019. */
  1020. static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  1021. {
  1022. unsigned long to;
  1023. unsigned int n;
  1024. unsigned int active = 1;
  1025. to = jiffies + msecs_to_jiffies(timeout);
  1026. do {
  1027. if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
  1028. test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  1029. msleep(20);
  1030. continue; /* svc thd is actively issuing commands */
  1031. }
  1032. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1033. return -EFAULT;
  1034. /*
  1035. * Ignore s_active bit 0 of array element 0.
  1036. * This bit will always be set
  1037. */
  1038. active = readl(port->s_active[0]) & 0xFFFFFFFE;
  1039. for (n = 1; n < port->dd->slot_groups; n++)
  1040. active |= readl(port->s_active[n]);
  1041. if (!active)
  1042. break;
  1043. msleep(20);
  1044. } while (time_before(jiffies, to));
  1045. return active ? -EBUSY : 0;
  1046. }
  1047. /*
  1048. * Execute an internal command and wait for the completion.
  1049. *
  1050. * @port Pointer to the port data structure.
  1051. * @fis Pointer to the FIS that describes the command.
  1052. * @fis_len Length in WORDS of the FIS.
  1053. * @buffer DMA accessible for command data.
  1054. * @buf_len Length, in bytes, of the data buffer.
  1055. * @opts Command header options, excluding the FIS length
  1056. * and the number of PRD entries.
  1057. * @timeout Time in ms to wait for the command to complete.
  1058. *
  1059. * return value
  1060. * 0 Command completed successfully.
  1061. * -EFAULT The buffer address is not correctly aligned.
  1062. * -EBUSY Internal command or other IO in progress.
  1063. * -EAGAIN Time out waiting for command to complete.
  1064. */
  1065. static int mtip_exec_internal_command(struct mtip_port *port,
  1066. struct host_to_dev_fis *fis,
  1067. int fis_len,
  1068. dma_addr_t buffer,
  1069. int buf_len,
  1070. u32 opts,
  1071. gfp_t atomic,
  1072. unsigned long timeout)
  1073. {
  1074. struct mtip_cmd_sg *command_sg;
  1075. DECLARE_COMPLETION_ONSTACK(wait);
  1076. int rv = 0, ready2go = 1;
  1077. struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
  1078. unsigned long to;
  1079. /* Make sure the buffer is 8 byte aligned. This is asic specific. */
  1080. if (buffer & 0x00000007) {
  1081. dev_err(&port->dd->pdev->dev,
  1082. "SG buffer is not 8 byte aligned\n");
  1083. return -EFAULT;
  1084. }
  1085. to = jiffies + msecs_to_jiffies(timeout);
  1086. do {
  1087. ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
  1088. port->allocated);
  1089. if (ready2go)
  1090. break;
  1091. mdelay(100);
  1092. } while (time_before(jiffies, to));
  1093. if (!ready2go) {
  1094. dev_warn(&port->dd->pdev->dev,
  1095. "Internal cmd active. new cmd [%02X]\n", fis->command);
  1096. return -EBUSY;
  1097. }
  1098. set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1099. port->ic_pause_timer = 0;
  1100. if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
  1101. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  1102. else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
  1103. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  1104. if (atomic == GFP_KERNEL) {
  1105. if (fis->command != ATA_CMD_STANDBYNOW1) {
  1106. /* wait for io to complete if non atomic */
  1107. if (mtip_quiesce_io(port, 5000) < 0) {
  1108. dev_warn(&port->dd->pdev->dev,
  1109. "Failed to quiesce IO\n");
  1110. release_slot(port, MTIP_TAG_INTERNAL);
  1111. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1112. wake_up_interruptible(&port->svc_wait);
  1113. return -EBUSY;
  1114. }
  1115. }
  1116. /* Set the completion function and data for the command. */
  1117. int_cmd->comp_data = &wait;
  1118. int_cmd->comp_func = mtip_completion;
  1119. } else {
  1120. /* Clear completion - we're going to poll */
  1121. int_cmd->comp_data = NULL;
  1122. int_cmd->comp_func = mtip_null_completion;
  1123. }
  1124. /* Copy the command to the command table */
  1125. memcpy(int_cmd->command, fis, fis_len*4);
  1126. /* Populate the SG list */
  1127. int_cmd->command_header->opts =
  1128. __force_bit2int cpu_to_le32(opts | fis_len);
  1129. if (buf_len) {
  1130. command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
  1131. command_sg->info =
  1132. __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
  1133. command_sg->dba =
  1134. __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
  1135. command_sg->dba_upper =
  1136. __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
  1137. int_cmd->command_header->opts |=
  1138. __force_bit2int cpu_to_le32((1 << 16));
  1139. }
  1140. /* Populate the command header */
  1141. int_cmd->command_header->byte_count = 0;
  1142. /* Issue the command to the hardware */
  1143. mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
  1144. /* Poll if atomic, wait_for_completion otherwise */
  1145. if (atomic == GFP_KERNEL) {
  1146. /* Wait for the command to complete or timeout. */
  1147. if (wait_for_completion_timeout(
  1148. &wait,
  1149. msecs_to_jiffies(timeout)) == 0) {
  1150. dev_err(&port->dd->pdev->dev,
  1151. "Internal command did not complete [%d] "
  1152. "within timeout of %lu ms\n",
  1153. atomic, timeout);
  1154. if (mtip_check_surprise_removal(port->dd->pdev) ||
  1155. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1156. &port->dd->dd_flag)) {
  1157. rv = -ENXIO;
  1158. goto exec_ic_exit;
  1159. }
  1160. rv = -EAGAIN;
  1161. }
  1162. } else {
  1163. /* Spin for <timeout> checking if command still outstanding */
  1164. timeout = jiffies + msecs_to_jiffies(timeout);
  1165. while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1166. & (1 << MTIP_TAG_INTERNAL))
  1167. && time_before(jiffies, timeout)) {
  1168. if (mtip_check_surprise_removal(port->dd->pdev)) {
  1169. rv = -ENXIO;
  1170. goto exec_ic_exit;
  1171. }
  1172. if ((fis->command != ATA_CMD_STANDBYNOW1) &&
  1173. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1174. &port->dd->dd_flag)) {
  1175. rv = -ENXIO;
  1176. goto exec_ic_exit;
  1177. }
  1178. if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
  1179. atomic_inc(&int_cmd->active); /* error */
  1180. break;
  1181. }
  1182. }
  1183. }
  1184. if (atomic_read(&int_cmd->active) > 1) {
  1185. dev_err(&port->dd->pdev->dev,
  1186. "Internal command [%02X] failed\n", fis->command);
  1187. rv = -EIO;
  1188. }
  1189. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1190. & (1 << MTIP_TAG_INTERNAL)) {
  1191. rv = -ENXIO;
  1192. if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1193. &port->dd->dd_flag)) {
  1194. mtip_restart_port(port);
  1195. rv = -EAGAIN;
  1196. }
  1197. }
  1198. exec_ic_exit:
  1199. /* Clear the allocated and active bits for the internal command. */
  1200. atomic_set(&int_cmd->active, 0);
  1201. release_slot(port, MTIP_TAG_INTERNAL);
  1202. if (rv >= 0 && mtip_pause_ncq(port, fis)) {
  1203. /* NCQ paused */
  1204. return rv;
  1205. }
  1206. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1207. wake_up_interruptible(&port->svc_wait);
  1208. return rv;
  1209. }
  1210. /*
  1211. * Byte-swap ATA ID strings.
  1212. *
  1213. * ATA identify data contains strings in byte-swapped 16-bit words.
  1214. * They must be swapped (on all architectures) to be usable as C strings.
  1215. * This function swaps bytes in-place.
  1216. *
  1217. * @buf The buffer location of the string
  1218. * @len The number of bytes to swap
  1219. *
  1220. * return value
  1221. * None
  1222. */
  1223. static inline void ata_swap_string(u16 *buf, unsigned int len)
  1224. {
  1225. int i;
  1226. for (i = 0; i < (len/2); i++)
  1227. be16_to_cpus(&buf[i]);
  1228. }
  1229. /*
  1230. * Request the device identity information.
  1231. *
  1232. * If a user space buffer is not specified, i.e. is NULL, the
  1233. * identify information is still read from the drive and placed
  1234. * into the identify data buffer (@e port->identify) in the
  1235. * port data structure.
  1236. * When the identify buffer contains valid identify information @e
  1237. * port->identify_valid is non-zero.
  1238. *
  1239. * @port Pointer to the port structure.
  1240. * @user_buffer A user space buffer where the identify data should be
  1241. * copied.
  1242. *
  1243. * return value
  1244. * 0 Command completed successfully.
  1245. * -EFAULT An error occurred while coping data to the user buffer.
  1246. * -1 Command failed.
  1247. */
  1248. static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
  1249. {
  1250. int rv = 0;
  1251. struct host_to_dev_fis fis;
  1252. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1253. return -EFAULT;
  1254. /* Build the FIS. */
  1255. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1256. fis.type = 0x27;
  1257. fis.opts = 1 << 7;
  1258. fis.command = ATA_CMD_ID_ATA;
  1259. /* Set the identify information as invalid. */
  1260. port->identify_valid = 0;
  1261. /* Clear the identify information. */
  1262. memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
  1263. /* Execute the command. */
  1264. if (mtip_exec_internal_command(port,
  1265. &fis,
  1266. 5,
  1267. port->identify_dma,
  1268. sizeof(u16) * ATA_ID_WORDS,
  1269. 0,
  1270. GFP_KERNEL,
  1271. MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
  1272. < 0) {
  1273. rv = -1;
  1274. goto out;
  1275. }
  1276. /*
  1277. * Perform any necessary byte-swapping. Yes, the kernel does in fact
  1278. * perform field-sensitive swapping on the string fields.
  1279. * See the kernel use of ata_id_string() for proof of this.
  1280. */
  1281. #ifdef __LITTLE_ENDIAN
  1282. ata_swap_string(port->identify + 27, 40); /* model string*/
  1283. ata_swap_string(port->identify + 23, 8); /* firmware string*/
  1284. ata_swap_string(port->identify + 10, 20); /* serial# string*/
  1285. #else
  1286. {
  1287. int i;
  1288. for (i = 0; i < ATA_ID_WORDS; i++)
  1289. port->identify[i] = le16_to_cpu(port->identify[i]);
  1290. }
  1291. #endif
  1292. /* Set the identify buffer as valid. */
  1293. port->identify_valid = 1;
  1294. if (user_buffer) {
  1295. if (copy_to_user(
  1296. user_buffer,
  1297. port->identify,
  1298. ATA_ID_WORDS * sizeof(u16))) {
  1299. rv = -EFAULT;
  1300. goto out;
  1301. }
  1302. }
  1303. out:
  1304. return rv;
  1305. }
  1306. /*
  1307. * Issue a standby immediate command to the device.
  1308. *
  1309. * @port Pointer to the port structure.
  1310. *
  1311. * return value
  1312. * 0 Command was executed successfully.
  1313. * -1 An error occurred while executing the command.
  1314. */
  1315. static int mtip_standby_immediate(struct mtip_port *port)
  1316. {
  1317. int rv;
  1318. struct host_to_dev_fis fis;
  1319. unsigned long start;
  1320. /* Build the FIS. */
  1321. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1322. fis.type = 0x27;
  1323. fis.opts = 1 << 7;
  1324. fis.command = ATA_CMD_STANDBYNOW1;
  1325. start = jiffies;
  1326. rv = mtip_exec_internal_command(port,
  1327. &fis,
  1328. 5,
  1329. 0,
  1330. 0,
  1331. 0,
  1332. GFP_ATOMIC,
  1333. 15000);
  1334. dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
  1335. jiffies_to_msecs(jiffies - start));
  1336. if (rv)
  1337. dev_warn(&port->dd->pdev->dev,
  1338. "STANDBY IMMEDIATE command failed.\n");
  1339. return rv;
  1340. }
  1341. /*
  1342. * Issue a READ LOG EXT command to the device.
  1343. *
  1344. * @port pointer to the port structure.
  1345. * @page page number to fetch
  1346. * @buffer pointer to buffer
  1347. * @buffer_dma dma address corresponding to @buffer
  1348. * @sectors page length to fetch, in sectors
  1349. *
  1350. * return value
  1351. * @rv return value from mtip_exec_internal_command()
  1352. */
  1353. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  1354. dma_addr_t buffer_dma, unsigned int sectors)
  1355. {
  1356. struct host_to_dev_fis fis;
  1357. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1358. fis.type = 0x27;
  1359. fis.opts = 1 << 7;
  1360. fis.command = ATA_CMD_READ_LOG_EXT;
  1361. fis.sect_count = sectors & 0xFF;
  1362. fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
  1363. fis.lba_low = page;
  1364. fis.lba_mid = 0;
  1365. fis.device = ATA_DEVICE_OBS;
  1366. memset(buffer, 0, sectors * ATA_SECT_SIZE);
  1367. return mtip_exec_internal_command(port,
  1368. &fis,
  1369. 5,
  1370. buffer_dma,
  1371. sectors * ATA_SECT_SIZE,
  1372. 0,
  1373. GFP_ATOMIC,
  1374. MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
  1375. }
  1376. /*
  1377. * Issue a SMART READ DATA command to the device.
  1378. *
  1379. * @port pointer to the port structure.
  1380. * @buffer pointer to buffer
  1381. * @buffer_dma dma address corresponding to @buffer
  1382. *
  1383. * return value
  1384. * @rv return value from mtip_exec_internal_command()
  1385. */
  1386. static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
  1387. dma_addr_t buffer_dma)
  1388. {
  1389. struct host_to_dev_fis fis;
  1390. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1391. fis.type = 0x27;
  1392. fis.opts = 1 << 7;
  1393. fis.command = ATA_CMD_SMART;
  1394. fis.features = 0xD0;
  1395. fis.sect_count = 1;
  1396. fis.lba_mid = 0x4F;
  1397. fis.lba_hi = 0xC2;
  1398. fis.device = ATA_DEVICE_OBS;
  1399. return mtip_exec_internal_command(port,
  1400. &fis,
  1401. 5,
  1402. buffer_dma,
  1403. ATA_SECT_SIZE,
  1404. 0,
  1405. GFP_ATOMIC,
  1406. 15000);
  1407. }
  1408. /*
  1409. * Get the value of a smart attribute
  1410. *
  1411. * @port pointer to the port structure
  1412. * @id attribute number
  1413. * @attrib pointer to return attrib information corresponding to @id
  1414. *
  1415. * return value
  1416. * -EINVAL NULL buffer passed or unsupported attribute @id.
  1417. * -EPERM Identify data not valid, SMART not supported or not enabled
  1418. */
  1419. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  1420. struct smart_attr *attrib)
  1421. {
  1422. int rv, i;
  1423. struct smart_attr *pattr;
  1424. if (!attrib)
  1425. return -EINVAL;
  1426. if (!port->identify_valid) {
  1427. dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
  1428. return -EPERM;
  1429. }
  1430. if (!(port->identify[82] & 0x1)) {
  1431. dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
  1432. return -EPERM;
  1433. }
  1434. if (!(port->identify[85] & 0x1)) {
  1435. dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
  1436. return -EPERM;
  1437. }
  1438. memset(port->smart_buf, 0, ATA_SECT_SIZE);
  1439. rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
  1440. if (rv) {
  1441. dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
  1442. return rv;
  1443. }
  1444. pattr = (struct smart_attr *)(port->smart_buf + 2);
  1445. for (i = 0; i < 29; i++, pattr++)
  1446. if (pattr->attr_id == id) {
  1447. memcpy(attrib, pattr, sizeof(struct smart_attr));
  1448. break;
  1449. }
  1450. if (i == 29) {
  1451. dev_warn(&port->dd->pdev->dev,
  1452. "Query for invalid SMART attribute ID\n");
  1453. rv = -EINVAL;
  1454. }
  1455. return rv;
  1456. }
  1457. /*
  1458. * Get the drive capacity.
  1459. *
  1460. * @dd Pointer to the device data structure.
  1461. * @sectors Pointer to the variable that will receive the sector count.
  1462. *
  1463. * return value
  1464. * 1 Capacity was returned successfully.
  1465. * 0 The identify information is invalid.
  1466. */
  1467. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1468. {
  1469. struct mtip_port *port = dd->port;
  1470. u64 total, raw0, raw1, raw2, raw3;
  1471. raw0 = port->identify[100];
  1472. raw1 = port->identify[101];
  1473. raw2 = port->identify[102];
  1474. raw3 = port->identify[103];
  1475. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1476. *sectors = total;
  1477. return (bool) !!port->identify_valid;
  1478. }
  1479. /*
  1480. * Reset the HBA.
  1481. *
  1482. * Resets the HBA by setting the HBA Reset bit in the Global
  1483. * HBA Control register. After setting the HBA Reset bit the
  1484. * function waits for 1 second before reading the HBA Reset
  1485. * bit to make sure it has cleared. If HBA Reset is not clear
  1486. * an error is returned. Cannot be used in non-blockable
  1487. * context.
  1488. *
  1489. * @dd Pointer to the driver data structure.
  1490. *
  1491. * return value
  1492. * 0 The reset was successful.
  1493. * -1 The HBA Reset bit did not clear.
  1494. */
  1495. static int mtip_hba_reset(struct driver_data *dd)
  1496. {
  1497. mtip_deinit_port(dd->port);
  1498. /* Set the reset bit */
  1499. writel(HOST_RESET, dd->mmio + HOST_CTL);
  1500. /* Flush */
  1501. readl(dd->mmio + HOST_CTL);
  1502. /* Wait for reset to clear */
  1503. ssleep(1);
  1504. /* Check the bit has cleared */
  1505. if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
  1506. dev_err(&dd->pdev->dev,
  1507. "Reset bit did not clear.\n");
  1508. return -1;
  1509. }
  1510. return 0;
  1511. }
  1512. /*
  1513. * Display the identify command data.
  1514. *
  1515. * @port Pointer to the port data structure.
  1516. *
  1517. * return value
  1518. * None
  1519. */
  1520. static void mtip_dump_identify(struct mtip_port *port)
  1521. {
  1522. sector_t sectors;
  1523. unsigned short revid;
  1524. char cbuf[42];
  1525. if (!port->identify_valid)
  1526. return;
  1527. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1528. dev_info(&port->dd->pdev->dev,
  1529. "Serial No.: %s\n", cbuf);
  1530. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1531. dev_info(&port->dd->pdev->dev,
  1532. "Firmware Ver.: %s\n", cbuf);
  1533. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1534. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1535. if (mtip_hw_get_capacity(port->dd, &sectors))
  1536. dev_info(&port->dd->pdev->dev,
  1537. "Capacity: %llu sectors (%llu MB)\n",
  1538. (u64)sectors,
  1539. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1540. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1541. switch (revid & 0xFF) {
  1542. case 0x1:
  1543. strlcpy(cbuf, "A0", 3);
  1544. break;
  1545. case 0x3:
  1546. strlcpy(cbuf, "A2", 3);
  1547. break;
  1548. default:
  1549. strlcpy(cbuf, "?", 2);
  1550. break;
  1551. }
  1552. dev_info(&port->dd->pdev->dev,
  1553. "Card Type: %s\n", cbuf);
  1554. }
  1555. /*
  1556. * Map the commands scatter list into the command table.
  1557. *
  1558. * @command Pointer to the command.
  1559. * @nents Number of scatter list entries.
  1560. *
  1561. * return value
  1562. * None
  1563. */
  1564. static inline void fill_command_sg(struct driver_data *dd,
  1565. struct mtip_cmd *command,
  1566. int nents)
  1567. {
  1568. int n;
  1569. unsigned int dma_len;
  1570. struct mtip_cmd_sg *command_sg;
  1571. struct scatterlist *sg = command->sg;
  1572. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1573. for (n = 0; n < nents; n++) {
  1574. dma_len = sg_dma_len(sg);
  1575. if (dma_len > 0x400000)
  1576. dev_err(&dd->pdev->dev,
  1577. "DMA segment length truncated\n");
  1578. command_sg->info = __force_bit2int
  1579. cpu_to_le32((dma_len-1) & 0x3FFFFF);
  1580. command_sg->dba = __force_bit2int
  1581. cpu_to_le32(sg_dma_address(sg));
  1582. command_sg->dba_upper = __force_bit2int
  1583. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1584. command_sg++;
  1585. sg++;
  1586. }
  1587. }
  1588. /*
  1589. * @brief Execute a drive command.
  1590. *
  1591. * return value 0 The command completed successfully.
  1592. * return value -1 An error occurred while executing the command.
  1593. */
  1594. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1595. {
  1596. struct host_to_dev_fis fis;
  1597. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1598. /* Build the FIS. */
  1599. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1600. fis.type = 0x27;
  1601. fis.opts = 1 << 7;
  1602. fis.command = command[0];
  1603. fis.features = command[1];
  1604. fis.sect_count = command[2];
  1605. fis.sector = command[3];
  1606. fis.cyl_low = command[4];
  1607. fis.cyl_hi = command[5];
  1608. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1609. dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
  1610. __func__,
  1611. command[0],
  1612. command[1],
  1613. command[2],
  1614. command[3],
  1615. command[4],
  1616. command[5],
  1617. command[6]);
  1618. /* Execute the command. */
  1619. if (mtip_exec_internal_command(port,
  1620. &fis,
  1621. 5,
  1622. 0,
  1623. 0,
  1624. 0,
  1625. GFP_KERNEL,
  1626. MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
  1627. return -1;
  1628. }
  1629. command[0] = reply->command; /* Status*/
  1630. command[1] = reply->features; /* Error*/
  1631. command[4] = reply->cyl_low;
  1632. command[5] = reply->cyl_hi;
  1633. dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
  1634. __func__,
  1635. command[0],
  1636. command[1],
  1637. command[4],
  1638. command[5]);
  1639. return 0;
  1640. }
  1641. /*
  1642. * @brief Execute a drive command.
  1643. *
  1644. * @param port Pointer to the port data structure.
  1645. * @param command Pointer to the user specified command parameters.
  1646. * @param user_buffer Pointer to the user space buffer where read sector
  1647. * data should be copied.
  1648. *
  1649. * return value 0 The command completed successfully.
  1650. * return value -EFAULT An error occurred while copying the completion
  1651. * data to the user space buffer.
  1652. * return value -1 An error occurred while executing the command.
  1653. */
  1654. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1655. void __user *user_buffer)
  1656. {
  1657. struct host_to_dev_fis fis;
  1658. struct host_to_dev_fis *reply;
  1659. u8 *buf = NULL;
  1660. dma_addr_t dma_addr = 0;
  1661. int rv = 0, xfer_sz = command[3];
  1662. if (xfer_sz) {
  1663. if (!user_buffer)
  1664. return -EFAULT;
  1665. buf = dmam_alloc_coherent(&port->dd->pdev->dev,
  1666. ATA_SECT_SIZE * xfer_sz,
  1667. &dma_addr,
  1668. GFP_KERNEL);
  1669. if (!buf) {
  1670. dev_err(&port->dd->pdev->dev,
  1671. "Memory allocation failed (%d bytes)\n",
  1672. ATA_SECT_SIZE * xfer_sz);
  1673. return -ENOMEM;
  1674. }
  1675. memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
  1676. }
  1677. /* Build the FIS. */
  1678. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1679. fis.type = 0x27;
  1680. fis.opts = 1 << 7;
  1681. fis.command = command[0];
  1682. fis.features = command[2];
  1683. fis.sect_count = command[3];
  1684. if (fis.command == ATA_CMD_SMART) {
  1685. fis.sector = command[1];
  1686. fis.cyl_low = 0x4F;
  1687. fis.cyl_hi = 0xC2;
  1688. }
  1689. if (xfer_sz)
  1690. reply = (port->rxfis + RX_FIS_PIO_SETUP);
  1691. else
  1692. reply = (port->rxfis + RX_FIS_D2H_REG);
  1693. dbg_printk(MTIP_DRV_NAME
  1694. " %s: User Command: cmd %x, sect %x, "
  1695. "feat %x, sectcnt %x\n",
  1696. __func__,
  1697. command[0],
  1698. command[1],
  1699. command[2],
  1700. command[3]);
  1701. /* Execute the command. */
  1702. if (mtip_exec_internal_command(port,
  1703. &fis,
  1704. 5,
  1705. (xfer_sz ? dma_addr : 0),
  1706. (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
  1707. 0,
  1708. GFP_KERNEL,
  1709. MTIP_IOCTL_COMMAND_TIMEOUT_MS)
  1710. < 0) {
  1711. rv = -EFAULT;
  1712. goto exit_drive_command;
  1713. }
  1714. /* Collect the completion status. */
  1715. command[0] = reply->command; /* Status*/
  1716. command[1] = reply->features; /* Error*/
  1717. command[2] = reply->sect_count;
  1718. dbg_printk(MTIP_DRV_NAME
  1719. " %s: Completion Status: stat %x, "
  1720. "err %x, nsect %x\n",
  1721. __func__,
  1722. command[0],
  1723. command[1],
  1724. command[2]);
  1725. if (xfer_sz) {
  1726. if (copy_to_user(user_buffer,
  1727. buf,
  1728. ATA_SECT_SIZE * command[3])) {
  1729. rv = -EFAULT;
  1730. goto exit_drive_command;
  1731. }
  1732. }
  1733. exit_drive_command:
  1734. if (buf)
  1735. dmam_free_coherent(&port->dd->pdev->dev,
  1736. ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
  1737. return rv;
  1738. }
  1739. /*
  1740. * Indicates whether a command has a single sector payload.
  1741. *
  1742. * @command passed to the device to perform the certain event.
  1743. * @features passed to the device to perform the certain event.
  1744. *
  1745. * return value
  1746. * 1 command is one that always has a single sector payload,
  1747. * regardless of the value in the Sector Count field.
  1748. * 0 otherwise
  1749. *
  1750. */
  1751. static unsigned int implicit_sector(unsigned char command,
  1752. unsigned char features)
  1753. {
  1754. unsigned int rv = 0;
  1755. /* list of commands that have an implicit sector count of 1 */
  1756. switch (command) {
  1757. case ATA_CMD_SEC_SET_PASS:
  1758. case ATA_CMD_SEC_UNLOCK:
  1759. case ATA_CMD_SEC_ERASE_PREP:
  1760. case ATA_CMD_SEC_ERASE_UNIT:
  1761. case ATA_CMD_SEC_FREEZE_LOCK:
  1762. case ATA_CMD_SEC_DISABLE_PASS:
  1763. case ATA_CMD_PMP_READ:
  1764. case ATA_CMD_PMP_WRITE:
  1765. rv = 1;
  1766. break;
  1767. case ATA_CMD_SET_MAX:
  1768. if (features == ATA_SET_MAX_UNLOCK)
  1769. rv = 1;
  1770. break;
  1771. case ATA_CMD_SMART:
  1772. if ((features == ATA_SMART_READ_VALUES) ||
  1773. (features == ATA_SMART_READ_THRESHOLDS))
  1774. rv = 1;
  1775. break;
  1776. case ATA_CMD_CONF_OVERLAY:
  1777. if ((features == ATA_DCO_IDENTIFY) ||
  1778. (features == ATA_DCO_SET))
  1779. rv = 1;
  1780. break;
  1781. }
  1782. return rv;
  1783. }
  1784. static void mtip_set_timeout(struct driver_data *dd,
  1785. struct host_to_dev_fis *fis,
  1786. unsigned int *timeout, u8 erasemode)
  1787. {
  1788. switch (fis->command) {
  1789. case ATA_CMD_DOWNLOAD_MICRO:
  1790. *timeout = 120000; /* 2 minutes */
  1791. break;
  1792. case ATA_CMD_SEC_ERASE_UNIT:
  1793. case 0xFC:
  1794. if (erasemode)
  1795. *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
  1796. else
  1797. *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
  1798. break;
  1799. case ATA_CMD_STANDBYNOW1:
  1800. *timeout = 120000; /* 2 minutes */
  1801. break;
  1802. case 0xF7:
  1803. case 0xFA:
  1804. *timeout = 60000; /* 60 seconds */
  1805. break;
  1806. case ATA_CMD_SMART:
  1807. *timeout = 15000; /* 15 seconds */
  1808. break;
  1809. default:
  1810. *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1811. break;
  1812. }
  1813. }
  1814. /*
  1815. * Executes a taskfile
  1816. * See ide_taskfile_ioctl() for derivation
  1817. */
  1818. static int exec_drive_taskfile(struct driver_data *dd,
  1819. void __user *buf,
  1820. ide_task_request_t *req_task,
  1821. int outtotal)
  1822. {
  1823. struct host_to_dev_fis fis;
  1824. struct host_to_dev_fis *reply;
  1825. u8 *outbuf = NULL;
  1826. u8 *inbuf = NULL;
  1827. dma_addr_t outbuf_dma = 0;
  1828. dma_addr_t inbuf_dma = 0;
  1829. dma_addr_t dma_buffer = 0;
  1830. int err = 0;
  1831. unsigned int taskin = 0;
  1832. unsigned int taskout = 0;
  1833. u8 nsect = 0;
  1834. unsigned int timeout;
  1835. unsigned int force_single_sector;
  1836. unsigned int transfer_size;
  1837. unsigned long task_file_data;
  1838. int intotal = outtotal + req_task->out_size;
  1839. int erasemode = 0;
  1840. taskout = req_task->out_size;
  1841. taskin = req_task->in_size;
  1842. /* 130560 = 512 * 0xFF*/
  1843. if (taskin > 130560 || taskout > 130560) {
  1844. err = -EINVAL;
  1845. goto abort;
  1846. }
  1847. if (taskout) {
  1848. outbuf = kzalloc(taskout, GFP_KERNEL);
  1849. if (outbuf == NULL) {
  1850. err = -ENOMEM;
  1851. goto abort;
  1852. }
  1853. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1854. err = -EFAULT;
  1855. goto abort;
  1856. }
  1857. outbuf_dma = pci_map_single(dd->pdev,
  1858. outbuf,
  1859. taskout,
  1860. DMA_TO_DEVICE);
  1861. if (outbuf_dma == 0) {
  1862. err = -ENOMEM;
  1863. goto abort;
  1864. }
  1865. dma_buffer = outbuf_dma;
  1866. }
  1867. if (taskin) {
  1868. inbuf = kzalloc(taskin, GFP_KERNEL);
  1869. if (inbuf == NULL) {
  1870. err = -ENOMEM;
  1871. goto abort;
  1872. }
  1873. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1874. err = -EFAULT;
  1875. goto abort;
  1876. }
  1877. inbuf_dma = pci_map_single(dd->pdev,
  1878. inbuf,
  1879. taskin, DMA_FROM_DEVICE);
  1880. if (inbuf_dma == 0) {
  1881. err = -ENOMEM;
  1882. goto abort;
  1883. }
  1884. dma_buffer = inbuf_dma;
  1885. }
  1886. /* only supports PIO and non-data commands from this ioctl. */
  1887. switch (req_task->data_phase) {
  1888. case TASKFILE_OUT:
  1889. nsect = taskout / ATA_SECT_SIZE;
  1890. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1891. break;
  1892. case TASKFILE_IN:
  1893. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1894. break;
  1895. case TASKFILE_NO_DATA:
  1896. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  1897. break;
  1898. default:
  1899. err = -EINVAL;
  1900. goto abort;
  1901. }
  1902. /* Build the FIS. */
  1903. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1904. fis.type = 0x27;
  1905. fis.opts = 1 << 7;
  1906. fis.command = req_task->io_ports[7];
  1907. fis.features = req_task->io_ports[1];
  1908. fis.sect_count = req_task->io_ports[2];
  1909. fis.lba_low = req_task->io_ports[3];
  1910. fis.lba_mid = req_task->io_ports[4];
  1911. fis.lba_hi = req_task->io_ports[5];
  1912. /* Clear the dev bit*/
  1913. fis.device = req_task->io_ports[6] & ~0x10;
  1914. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  1915. req_task->in_flags.all =
  1916. IDE_TASKFILE_STD_IN_FLAGS |
  1917. (IDE_HOB_STD_IN_FLAGS << 8);
  1918. fis.lba_low_ex = req_task->hob_ports[3];
  1919. fis.lba_mid_ex = req_task->hob_ports[4];
  1920. fis.lba_hi_ex = req_task->hob_ports[5];
  1921. fis.features_ex = req_task->hob_ports[1];
  1922. fis.sect_cnt_ex = req_task->hob_ports[2];
  1923. } else {
  1924. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  1925. }
  1926. force_single_sector = implicit_sector(fis.command, fis.features);
  1927. if ((taskin || taskout) && (!fis.sect_count)) {
  1928. if (nsect)
  1929. fis.sect_count = nsect;
  1930. else {
  1931. if (!force_single_sector) {
  1932. dev_warn(&dd->pdev->dev,
  1933. "data movement but "
  1934. "sect_count is 0\n");
  1935. err = -EINVAL;
  1936. goto abort;
  1937. }
  1938. }
  1939. }
  1940. dbg_printk(MTIP_DRV_NAME
  1941. " %s: cmd %x, feat %x, nsect %x,"
  1942. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  1943. " head/dev %x\n",
  1944. __func__,
  1945. fis.command,
  1946. fis.features,
  1947. fis.sect_count,
  1948. fis.lba_low,
  1949. fis.lba_mid,
  1950. fis.lba_hi,
  1951. fis.device);
  1952. /* check for erase mode support during secure erase.*/
  1953. if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
  1954. (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
  1955. erasemode = 1;
  1956. }
  1957. mtip_set_timeout(dd, &fis, &timeout, erasemode);
  1958. /* Determine the correct transfer size.*/
  1959. if (force_single_sector)
  1960. transfer_size = ATA_SECT_SIZE;
  1961. else
  1962. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  1963. /* Execute the command.*/
  1964. if (mtip_exec_internal_command(dd->port,
  1965. &fis,
  1966. 5,
  1967. dma_buffer,
  1968. transfer_size,
  1969. 0,
  1970. GFP_KERNEL,
  1971. timeout) < 0) {
  1972. err = -EIO;
  1973. goto abort;
  1974. }
  1975. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  1976. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  1977. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  1978. req_task->io_ports[7] = reply->control;
  1979. } else {
  1980. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  1981. req_task->io_ports[7] = reply->command;
  1982. }
  1983. /* reclaim the DMA buffers.*/
  1984. if (inbuf_dma)
  1985. pci_unmap_single(dd->pdev, inbuf_dma,
  1986. taskin, DMA_FROM_DEVICE);
  1987. if (outbuf_dma)
  1988. pci_unmap_single(dd->pdev, outbuf_dma,
  1989. taskout, DMA_TO_DEVICE);
  1990. inbuf_dma = 0;
  1991. outbuf_dma = 0;
  1992. /* return the ATA registers to the caller.*/
  1993. req_task->io_ports[1] = reply->features;
  1994. req_task->io_ports[2] = reply->sect_count;
  1995. req_task->io_ports[3] = reply->lba_low;
  1996. req_task->io_ports[4] = reply->lba_mid;
  1997. req_task->io_ports[5] = reply->lba_hi;
  1998. req_task->io_ports[6] = reply->device;
  1999. if (req_task->out_flags.all & 1) {
  2000. req_task->hob_ports[3] = reply->lba_low_ex;
  2001. req_task->hob_ports[4] = reply->lba_mid_ex;
  2002. req_task->hob_ports[5] = reply->lba_hi_ex;
  2003. req_task->hob_ports[1] = reply->features_ex;
  2004. req_task->hob_ports[2] = reply->sect_cnt_ex;
  2005. }
  2006. dbg_printk(MTIP_DRV_NAME
  2007. " %s: Completion: stat %x,"
  2008. "err %x, sect_cnt %x, lbalo %x,"
  2009. "lbamid %x, lbahi %x, dev %x\n",
  2010. __func__,
  2011. req_task->io_ports[7],
  2012. req_task->io_ports[1],
  2013. req_task->io_ports[2],
  2014. req_task->io_ports[3],
  2015. req_task->io_ports[4],
  2016. req_task->io_ports[5],
  2017. req_task->io_ports[6]);
  2018. if (taskout) {
  2019. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  2020. err = -EFAULT;
  2021. goto abort;
  2022. }
  2023. }
  2024. if (taskin) {
  2025. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  2026. err = -EFAULT;
  2027. goto abort;
  2028. }
  2029. }
  2030. abort:
  2031. if (inbuf_dma)
  2032. pci_unmap_single(dd->pdev, inbuf_dma,
  2033. taskin, DMA_FROM_DEVICE);
  2034. if (outbuf_dma)
  2035. pci_unmap_single(dd->pdev, outbuf_dma,
  2036. taskout, DMA_TO_DEVICE);
  2037. kfree(outbuf);
  2038. kfree(inbuf);
  2039. return err;
  2040. }
  2041. /*
  2042. * Handle IOCTL calls from the Block Layer.
  2043. *
  2044. * This function is called by the Block Layer when it receives an IOCTL
  2045. * command that it does not understand. If the IOCTL command is not supported
  2046. * this function returns -ENOTTY.
  2047. *
  2048. * @dd Pointer to the driver data structure.
  2049. * @cmd IOCTL command passed from the Block Layer.
  2050. * @arg IOCTL argument passed from the Block Layer.
  2051. *
  2052. * return value
  2053. * 0 The IOCTL completed successfully.
  2054. * -ENOTTY The specified command is not supported.
  2055. * -EFAULT An error occurred copying data to a user space buffer.
  2056. * -EIO An error occurred while executing the command.
  2057. */
  2058. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  2059. unsigned long arg)
  2060. {
  2061. switch (cmd) {
  2062. case HDIO_GET_IDENTITY:
  2063. {
  2064. if (copy_to_user((void __user *)arg, dd->port->identify,
  2065. sizeof(u16) * ATA_ID_WORDS))
  2066. return -EFAULT;
  2067. break;
  2068. }
  2069. case HDIO_DRIVE_CMD:
  2070. {
  2071. u8 drive_command[4];
  2072. /* Copy the user command info to our buffer. */
  2073. if (copy_from_user(drive_command,
  2074. (void __user *) arg,
  2075. sizeof(drive_command)))
  2076. return -EFAULT;
  2077. /* Execute the drive command. */
  2078. if (exec_drive_command(dd->port,
  2079. drive_command,
  2080. (void __user *) (arg+4)))
  2081. return -EIO;
  2082. /* Copy the status back to the users buffer. */
  2083. if (copy_to_user((void __user *) arg,
  2084. drive_command,
  2085. sizeof(drive_command)))
  2086. return -EFAULT;
  2087. break;
  2088. }
  2089. case HDIO_DRIVE_TASK:
  2090. {
  2091. u8 drive_command[7];
  2092. /* Copy the user command info to our buffer. */
  2093. if (copy_from_user(drive_command,
  2094. (void __user *) arg,
  2095. sizeof(drive_command)))
  2096. return -EFAULT;
  2097. /* Execute the drive command. */
  2098. if (exec_drive_task(dd->port, drive_command))
  2099. return -EIO;
  2100. /* Copy the status back to the users buffer. */
  2101. if (copy_to_user((void __user *) arg,
  2102. drive_command,
  2103. sizeof(drive_command)))
  2104. return -EFAULT;
  2105. break;
  2106. }
  2107. case HDIO_DRIVE_TASKFILE: {
  2108. ide_task_request_t req_task;
  2109. int ret, outtotal;
  2110. if (copy_from_user(&req_task, (void __user *) arg,
  2111. sizeof(req_task)))
  2112. return -EFAULT;
  2113. outtotal = sizeof(req_task);
  2114. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2115. &req_task, outtotal);
  2116. if (copy_to_user((void __user *) arg, &req_task,
  2117. sizeof(req_task)))
  2118. return -EFAULT;
  2119. return ret;
  2120. }
  2121. default:
  2122. return -EINVAL;
  2123. }
  2124. return 0;
  2125. }
  2126. /*
  2127. * Submit an IO to the hw
  2128. *
  2129. * This function is called by the block layer to issue an io
  2130. * to the device. Upon completion, the callback function will
  2131. * be called with the data parameter passed as the callback data.
  2132. *
  2133. * @dd Pointer to the driver data structure.
  2134. * @start First sector to read.
  2135. * @nsect Number of sectors to read.
  2136. * @nents Number of entries in scatter list for the read command.
  2137. * @tag The tag of this read command.
  2138. * @callback Pointer to the function that should be called
  2139. * when the read completes.
  2140. * @data Callback data passed to the callback function
  2141. * when the read completes.
  2142. * @dir Direction (read or write)
  2143. *
  2144. * return value
  2145. * None
  2146. */
  2147. static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
  2148. int nsect, int nents, int tag, void *callback,
  2149. void *data, int dir)
  2150. {
  2151. struct host_to_dev_fis *fis;
  2152. struct mtip_port *port = dd->port;
  2153. struct mtip_cmd *command = &port->commands[tag];
  2154. int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2155. u64 start = sector;
  2156. /* Map the scatter list for DMA access */
  2157. nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
  2158. command->scatter_ents = nents;
  2159. /*
  2160. * The number of retries for this command before it is
  2161. * reported as a failure to the upper layers.
  2162. */
  2163. command->retries = MTIP_MAX_RETRIES;
  2164. /* Fill out fis */
  2165. fis = command->command;
  2166. fis->type = 0x27;
  2167. fis->opts = 1 << 7;
  2168. fis->command =
  2169. (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
  2170. fis->lba_low = start & 0xFF;
  2171. fis->lba_mid = (start >> 8) & 0xFF;
  2172. fis->lba_hi = (start >> 16) & 0xFF;
  2173. fis->lba_low_ex = (start >> 24) & 0xFF;
  2174. fis->lba_mid_ex = (start >> 32) & 0xFF;
  2175. fis->lba_hi_ex = (start >> 40) & 0xFF;
  2176. fis->device = 1 << 6;
  2177. fis->features = nsect & 0xFF;
  2178. fis->features_ex = (nsect >> 8) & 0xFF;
  2179. fis->sect_count = ((tag << 3) | (tag >> 5));
  2180. fis->sect_cnt_ex = 0;
  2181. fis->control = 0;
  2182. fis->res2 = 0;
  2183. fis->res3 = 0;
  2184. fill_command_sg(dd, command, nents);
  2185. /* Populate the command header */
  2186. command->command_header->opts =
  2187. __force_bit2int cpu_to_le32(
  2188. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  2189. command->command_header->byte_count = 0;
  2190. /*
  2191. * Set the completion function and data for the command
  2192. * within this layer.
  2193. */
  2194. command->comp_data = dd;
  2195. command->comp_func = mtip_async_complete;
  2196. command->direction = dma_dir;
  2197. /*
  2198. * Set the completion function and data for the command passed
  2199. * from the upper layer.
  2200. */
  2201. command->async_data = data;
  2202. command->async_callback = callback;
  2203. /*
  2204. * To prevent this command from being issued
  2205. * if an internal command is in progress or error handling is active.
  2206. */
  2207. if (port->flags & MTIP_PF_PAUSE_IO) {
  2208. set_bit(tag, port->cmds_to_issue);
  2209. set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2210. return;
  2211. }
  2212. /* Issue the command to the hardware */
  2213. mtip_issue_ncq_command(port, tag);
  2214. return;
  2215. }
  2216. /*
  2217. * Release a command slot.
  2218. *
  2219. * @dd Pointer to the driver data structure.
  2220. * @tag Slot tag
  2221. *
  2222. * return value
  2223. * None
  2224. */
  2225. static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
  2226. {
  2227. release_slot(dd->port, tag);
  2228. }
  2229. /*
  2230. * Obtain a command slot and return its associated scatter list.
  2231. *
  2232. * @dd Pointer to the driver data structure.
  2233. * @tag Pointer to an int that will receive the allocated command
  2234. * slot tag.
  2235. *
  2236. * return value
  2237. * Pointer to the scatter list for the allocated command slot
  2238. * or NULL if no command slots are available.
  2239. */
  2240. static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  2241. int *tag)
  2242. {
  2243. /*
  2244. * It is possible that, even with this semaphore, a thread
  2245. * may think that no command slots are available. Therefore, we
  2246. * need to make an attempt to get_slot().
  2247. */
  2248. down(&dd->port->cmd_slot);
  2249. *tag = get_slot(dd->port);
  2250. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2251. up(&dd->port->cmd_slot);
  2252. return NULL;
  2253. }
  2254. if (unlikely(*tag < 0)) {
  2255. up(&dd->port->cmd_slot);
  2256. return NULL;
  2257. }
  2258. return dd->port->commands[*tag].sg;
  2259. }
  2260. /*
  2261. * Sysfs status dump.
  2262. *
  2263. * @dev Pointer to the device structure, passed by the kernrel.
  2264. * @attr Pointer to the device_attribute structure passed by the kernel.
  2265. * @buf Pointer to the char buffer that will receive the stats info.
  2266. *
  2267. * return value
  2268. * The size, in bytes, of the data copied into buf.
  2269. */
  2270. static ssize_t mtip_hw_show_status(struct device *dev,
  2271. struct device_attribute *attr,
  2272. char *buf)
  2273. {
  2274. struct driver_data *dd = dev_to_disk(dev)->private_data;
  2275. int size = 0;
  2276. if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
  2277. size += sprintf(buf, "%s", "thermal_shutdown\n");
  2278. else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
  2279. size += sprintf(buf, "%s", "write_protect\n");
  2280. else
  2281. size += sprintf(buf, "%s", "online\n");
  2282. return size;
  2283. }
  2284. static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
  2285. static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
  2286. size_t len, loff_t *offset)
  2287. {
  2288. struct driver_data *dd = (struct driver_data *)f->private_data;
  2289. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2290. u32 group_allocated;
  2291. int size = *offset;
  2292. int n;
  2293. if (!len || size)
  2294. return 0;
  2295. size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
  2296. for (n = dd->slot_groups-1; n >= 0; n--)
  2297. size += sprintf(&buf[size], "%08X ",
  2298. readl(dd->port->s_active[n]));
  2299. size += sprintf(&buf[size], "]\n");
  2300. size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
  2301. for (n = dd->slot_groups-1; n >= 0; n--)
  2302. size += sprintf(&buf[size], "%08X ",
  2303. readl(dd->port->cmd_issue[n]));
  2304. size += sprintf(&buf[size], "]\n");
  2305. size += sprintf(&buf[size], "H/ Completed : [ 0x");
  2306. for (n = dd->slot_groups-1; n >= 0; n--)
  2307. size += sprintf(&buf[size], "%08X ",
  2308. readl(dd->port->completed[n]));
  2309. size += sprintf(&buf[size], "]\n");
  2310. size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
  2311. readl(dd->port->mmio + PORT_IRQ_STAT));
  2312. size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
  2313. readl(dd->mmio + HOST_IRQ_STAT));
  2314. size += sprintf(&buf[size], "\n");
  2315. size += sprintf(&buf[size], "L/ Allocated : [ 0x");
  2316. for (n = dd->slot_groups-1; n >= 0; n--) {
  2317. if (sizeof(long) > sizeof(u32))
  2318. group_allocated =
  2319. dd->port->allocated[n/2] >> (32*(n&1));
  2320. else
  2321. group_allocated = dd->port->allocated[n];
  2322. size += sprintf(&buf[size], "%08X ", group_allocated);
  2323. }
  2324. size += sprintf(&buf[size], "]\n");
  2325. size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
  2326. for (n = dd->slot_groups-1; n >= 0; n--) {
  2327. if (sizeof(long) > sizeof(u32))
  2328. group_allocated =
  2329. dd->port->cmds_to_issue[n/2] >> (32*(n&1));
  2330. else
  2331. group_allocated = dd->port->cmds_to_issue[n];
  2332. size += sprintf(&buf[size], "%08X ", group_allocated);
  2333. }
  2334. size += sprintf(&buf[size], "]\n");
  2335. *offset = size <= len ? size : len;
  2336. size = copy_to_user(ubuf, buf, *offset);
  2337. if (size)
  2338. return -EFAULT;
  2339. return *offset;
  2340. }
  2341. static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
  2342. size_t len, loff_t *offset)
  2343. {
  2344. struct driver_data *dd = (struct driver_data *)f->private_data;
  2345. char buf[MTIP_DFS_MAX_BUF_SIZE];
  2346. int size = *offset;
  2347. if (!len || size)
  2348. return 0;
  2349. size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
  2350. dd->port->flags);
  2351. size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
  2352. dd->dd_flag);
  2353. *offset = size <= len ? size : len;
  2354. size = copy_to_user(ubuf, buf, *offset);
  2355. if (size)
  2356. return -EFAULT;
  2357. return *offset;
  2358. }
  2359. static const struct file_operations mtip_regs_fops = {
  2360. .owner = THIS_MODULE,
  2361. .open = simple_open,
  2362. .read = mtip_hw_read_registers,
  2363. .llseek = no_llseek,
  2364. };
  2365. static const struct file_operations mtip_flags_fops = {
  2366. .owner = THIS_MODULE,
  2367. .open = simple_open,
  2368. .read = mtip_hw_read_flags,
  2369. .llseek = no_llseek,
  2370. };
  2371. /*
  2372. * Create the sysfs related attributes.
  2373. *
  2374. * @dd Pointer to the driver data structure.
  2375. * @kobj Pointer to the kobj for the block device.
  2376. *
  2377. * return value
  2378. * 0 Operation completed successfully.
  2379. * -EINVAL Invalid parameter.
  2380. */
  2381. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  2382. {
  2383. if (!kobj || !dd)
  2384. return -EINVAL;
  2385. if (sysfs_create_file(kobj, &dev_attr_status.attr))
  2386. dev_warn(&dd->pdev->dev,
  2387. "Error creating 'status' sysfs entry\n");
  2388. return 0;
  2389. }
  2390. /*
  2391. * Remove the sysfs related attributes.
  2392. *
  2393. * @dd Pointer to the driver data structure.
  2394. * @kobj Pointer to the kobj for the block device.
  2395. *
  2396. * return value
  2397. * 0 Operation completed successfully.
  2398. * -EINVAL Invalid parameter.
  2399. */
  2400. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  2401. {
  2402. if (!kobj || !dd)
  2403. return -EINVAL;
  2404. sysfs_remove_file(kobj, &dev_attr_status.attr);
  2405. return 0;
  2406. }
  2407. static int mtip_hw_debugfs_init(struct driver_data *dd)
  2408. {
  2409. if (!dfs_parent)
  2410. return -1;
  2411. dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
  2412. if (IS_ERR_OR_NULL(dd->dfs_node)) {
  2413. dev_warn(&dd->pdev->dev,
  2414. "Error creating node %s under debugfs\n",
  2415. dd->disk->disk_name);
  2416. dd->dfs_node = NULL;
  2417. return -1;
  2418. }
  2419. debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
  2420. &mtip_flags_fops);
  2421. debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
  2422. &mtip_regs_fops);
  2423. return 0;
  2424. }
  2425. static void mtip_hw_debugfs_exit(struct driver_data *dd)
  2426. {
  2427. debugfs_remove_recursive(dd->dfs_node);
  2428. }
  2429. /*
  2430. * Perform any init/resume time hardware setup
  2431. *
  2432. * @dd Pointer to the driver data structure.
  2433. *
  2434. * return value
  2435. * None
  2436. */
  2437. static inline void hba_setup(struct driver_data *dd)
  2438. {
  2439. u32 hwdata;
  2440. hwdata = readl(dd->mmio + HOST_HSORG);
  2441. /* interrupt bug workaround: use only 1 IS bit.*/
  2442. writel(hwdata |
  2443. HSORG_DISABLE_SLOTGRP_INTR |
  2444. HSORG_DISABLE_SLOTGRP_PXIS,
  2445. dd->mmio + HOST_HSORG);
  2446. }
  2447. /*
  2448. * Detect the details of the product, and store anything needed
  2449. * into the driver data structure. This includes product type and
  2450. * version and number of slot groups.
  2451. *
  2452. * @dd Pointer to the driver data structure.
  2453. *
  2454. * return value
  2455. * None
  2456. */
  2457. static void mtip_detect_product(struct driver_data *dd)
  2458. {
  2459. u32 hwdata;
  2460. unsigned int rev, slotgroups;
  2461. /*
  2462. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2463. * info register:
  2464. * [15:8] hardware/software interface rev#
  2465. * [ 3] asic-style interface
  2466. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2467. */
  2468. hwdata = readl(dd->mmio + HOST_HSORG);
  2469. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2470. dd->slot_groups = 1;
  2471. if (hwdata & 0x8) {
  2472. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2473. rev = (hwdata & HSORG_HWREV) >> 8;
  2474. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2475. dev_info(&dd->pdev->dev,
  2476. "ASIC-FPGA design, HS rev 0x%x, "
  2477. "%i slot groups [%i slots]\n",
  2478. rev,
  2479. slotgroups,
  2480. slotgroups * 32);
  2481. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2482. dev_warn(&dd->pdev->dev,
  2483. "Warning: driver only supports "
  2484. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2485. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2486. }
  2487. dd->slot_groups = slotgroups;
  2488. return;
  2489. }
  2490. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2491. }
  2492. /*
  2493. * Blocking wait for FTL rebuild to complete
  2494. *
  2495. * @dd Pointer to the DRIVER_DATA structure.
  2496. *
  2497. * return value
  2498. * 0 FTL rebuild completed successfully
  2499. * -EFAULT FTL rebuild error/timeout/interruption
  2500. */
  2501. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2502. {
  2503. unsigned long timeout, cnt = 0, start;
  2504. dev_warn(&dd->pdev->dev,
  2505. "FTL rebuild in progress. Polling for completion.\n");
  2506. start = jiffies;
  2507. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2508. do {
  2509. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2510. &dd->dd_flag)))
  2511. return -EFAULT;
  2512. if (mtip_check_surprise_removal(dd->pdev))
  2513. return -EFAULT;
  2514. if (mtip_get_identify(dd->port, NULL) < 0)
  2515. return -EFAULT;
  2516. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2517. MTIP_FTL_REBUILD_MAGIC) {
  2518. ssleep(1);
  2519. /* Print message every 3 minutes */
  2520. if (cnt++ >= 180) {
  2521. dev_warn(&dd->pdev->dev,
  2522. "FTL rebuild in progress (%d secs).\n",
  2523. jiffies_to_msecs(jiffies - start) / 1000);
  2524. cnt = 0;
  2525. }
  2526. } else {
  2527. dev_warn(&dd->pdev->dev,
  2528. "FTL rebuild complete (%d secs).\n",
  2529. jiffies_to_msecs(jiffies - start) / 1000);
  2530. mtip_block_initialize(dd);
  2531. return 0;
  2532. }
  2533. ssleep(10);
  2534. } while (time_before(jiffies, timeout));
  2535. /* Check for timeout */
  2536. dev_err(&dd->pdev->dev,
  2537. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2538. jiffies_to_msecs(jiffies - start) / 1000);
  2539. return -EFAULT;
  2540. }
  2541. /*
  2542. * service thread to issue queued commands
  2543. *
  2544. * @data Pointer to the driver data structure.
  2545. *
  2546. * return value
  2547. * 0
  2548. */
  2549. static int mtip_service_thread(void *data)
  2550. {
  2551. struct driver_data *dd = (struct driver_data *)data;
  2552. unsigned long slot, slot_start, slot_wrap;
  2553. unsigned int num_cmd_slots = dd->slot_groups * 32;
  2554. struct mtip_port *port = dd->port;
  2555. while (1) {
  2556. /*
  2557. * the condition is to check neither an internal command is
  2558. * is in progress nor error handling is active
  2559. */
  2560. wait_event_interruptible(port->svc_wait, (port->flags) &&
  2561. !(port->flags & MTIP_PF_PAUSE_IO));
  2562. if (kthread_should_stop())
  2563. break;
  2564. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2565. &dd->dd_flag)))
  2566. break;
  2567. set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2568. if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  2569. slot = 1;
  2570. /* used to restrict the loop to one iteration */
  2571. slot_start = num_cmd_slots;
  2572. slot_wrap = 0;
  2573. while (1) {
  2574. slot = find_next_bit(port->cmds_to_issue,
  2575. num_cmd_slots, slot);
  2576. if (slot_wrap == 1) {
  2577. if ((slot_start >= slot) ||
  2578. (slot >= num_cmd_slots))
  2579. break;
  2580. }
  2581. if (unlikely(slot_start == num_cmd_slots))
  2582. slot_start = slot;
  2583. if (unlikely(slot == num_cmd_slots)) {
  2584. slot = 1;
  2585. slot_wrap = 1;
  2586. continue;
  2587. }
  2588. /* Issue the command to the hardware */
  2589. mtip_issue_ncq_command(port, slot);
  2590. clear_bit(slot, port->cmds_to_issue);
  2591. }
  2592. clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2593. } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
  2594. if (!mtip_ftl_rebuild_poll(dd))
  2595. set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
  2596. &dd->dd_flag);
  2597. clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
  2598. }
  2599. clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2600. if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
  2601. break;
  2602. }
  2603. return 0;
  2604. }
  2605. /*
  2606. * Called once for each card.
  2607. *
  2608. * @dd Pointer to the driver data structure.
  2609. *
  2610. * return value
  2611. * 0 on success, else an error code.
  2612. */
  2613. static int mtip_hw_init(struct driver_data *dd)
  2614. {
  2615. int i;
  2616. int rv;
  2617. unsigned int num_command_slots;
  2618. unsigned long timeout, timetaken;
  2619. unsigned char *buf;
  2620. struct smart_attr attr242;
  2621. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2622. mtip_detect_product(dd);
  2623. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2624. rv = -EIO;
  2625. goto out1;
  2626. }
  2627. num_command_slots = dd->slot_groups * 32;
  2628. hba_setup(dd);
  2629. tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
  2630. dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
  2631. if (!dd->port) {
  2632. dev_err(&dd->pdev->dev,
  2633. "Memory allocation: port structure\n");
  2634. return -ENOMEM;
  2635. }
  2636. /* Counting semaphore to track command slot usage */
  2637. sema_init(&dd->port->cmd_slot, num_command_slots - 1);
  2638. /* Spinlock to prevent concurrent issue */
  2639. spin_lock_init(&dd->port->cmd_issue_lock);
  2640. /* Set the port mmio base address. */
  2641. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2642. dd->port->dd = dd;
  2643. /* Allocate memory for the command list. */
  2644. dd->port->command_list =
  2645. dmam_alloc_coherent(&dd->pdev->dev,
  2646. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2647. &dd->port->command_list_dma,
  2648. GFP_KERNEL);
  2649. if (!dd->port->command_list) {
  2650. dev_err(&dd->pdev->dev,
  2651. "Memory allocation: command list\n");
  2652. rv = -ENOMEM;
  2653. goto out1;
  2654. }
  2655. /* Clear the memory we have allocated. */
  2656. memset(dd->port->command_list,
  2657. 0,
  2658. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
  2659. /* Setup the addresse of the RX FIS. */
  2660. dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
  2661. dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
  2662. /* Setup the address of the command tables. */
  2663. dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
  2664. dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
  2665. /* Setup the address of the identify data. */
  2666. dd->port->identify = dd->port->command_table +
  2667. HW_CMD_TBL_AR_SZ;
  2668. dd->port->identify_dma = dd->port->command_tbl_dma +
  2669. HW_CMD_TBL_AR_SZ;
  2670. /* Setup the address of the sector buffer - for some non-ncq cmds */
  2671. dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
  2672. dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
  2673. /* Setup the address of the log buf - for read log command */
  2674. dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
  2675. dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
  2676. /* Setup the address of the smart buf - for smart read data command */
  2677. dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
  2678. dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
  2679. /* Point the command headers at the command tables. */
  2680. for (i = 0; i < num_command_slots; i++) {
  2681. dd->port->commands[i].command_header =
  2682. dd->port->command_list +
  2683. (sizeof(struct mtip_cmd_hdr) * i);
  2684. dd->port->commands[i].command_header_dma =
  2685. dd->port->command_list_dma +
  2686. (sizeof(struct mtip_cmd_hdr) * i);
  2687. dd->port->commands[i].command =
  2688. dd->port->command_table + (HW_CMD_TBL_SZ * i);
  2689. dd->port->commands[i].command_dma =
  2690. dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
  2691. if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
  2692. dd->port->commands[i].command_header->ctbau =
  2693. __force_bit2int cpu_to_le32(
  2694. (dd->port->commands[i].command_dma >> 16) >> 16);
  2695. dd->port->commands[i].command_header->ctba =
  2696. __force_bit2int cpu_to_le32(
  2697. dd->port->commands[i].command_dma & 0xFFFFFFFF);
  2698. /*
  2699. * If this is not done, a bug is reported by the stock
  2700. * FC11 i386. Due to the fact that it has lots of kernel
  2701. * debugging enabled.
  2702. */
  2703. sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
  2704. /* Mark all commands as currently inactive.*/
  2705. atomic_set(&dd->port->commands[i].active, 0);
  2706. }
  2707. /* Setup the pointers to the extended s_active and CI registers. */
  2708. for (i = 0; i < dd->slot_groups; i++) {
  2709. dd->port->s_active[i] =
  2710. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  2711. dd->port->cmd_issue[i] =
  2712. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  2713. dd->port->completed[i] =
  2714. dd->port->mmio + i*0x80 + PORT_SDBV;
  2715. }
  2716. timetaken = jiffies;
  2717. timeout = jiffies + msecs_to_jiffies(30000);
  2718. while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
  2719. time_before(jiffies, timeout)) {
  2720. mdelay(100);
  2721. }
  2722. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  2723. timetaken = jiffies - timetaken;
  2724. dev_warn(&dd->pdev->dev,
  2725. "Surprise removal detected at %u ms\n",
  2726. jiffies_to_msecs(timetaken));
  2727. rv = -ENODEV;
  2728. goto out2 ;
  2729. }
  2730. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2731. timetaken = jiffies - timetaken;
  2732. dev_warn(&dd->pdev->dev,
  2733. "Removal detected at %u ms\n",
  2734. jiffies_to_msecs(timetaken));
  2735. rv = -EFAULT;
  2736. goto out2;
  2737. }
  2738. /* Conditionally reset the HBA. */
  2739. if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
  2740. if (mtip_hba_reset(dd) < 0) {
  2741. dev_err(&dd->pdev->dev,
  2742. "Card did not reset within timeout\n");
  2743. rv = -EIO;
  2744. goto out2;
  2745. }
  2746. } else {
  2747. /* Clear any pending interrupts on the HBA */
  2748. writel(readl(dd->mmio + HOST_IRQ_STAT),
  2749. dd->mmio + HOST_IRQ_STAT);
  2750. }
  2751. mtip_init_port(dd->port);
  2752. mtip_start_port(dd->port);
  2753. /* Setup the ISR and enable interrupts. */
  2754. rv = devm_request_irq(&dd->pdev->dev,
  2755. dd->pdev->irq,
  2756. mtip_irq_handler,
  2757. IRQF_SHARED,
  2758. dev_driver_string(&dd->pdev->dev),
  2759. dd);
  2760. if (rv) {
  2761. dev_err(&dd->pdev->dev,
  2762. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  2763. goto out2;
  2764. }
  2765. /* Enable interrupts on the HBA. */
  2766. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2767. dd->mmio + HOST_CTL);
  2768. init_timer(&dd->port->cmd_timer);
  2769. init_waitqueue_head(&dd->port->svc_wait);
  2770. dd->port->cmd_timer.data = (unsigned long int) dd->port;
  2771. dd->port->cmd_timer.function = mtip_timeout_function;
  2772. mod_timer(&dd->port->cmd_timer,
  2773. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  2774. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  2775. rv = -EFAULT;
  2776. goto out3;
  2777. }
  2778. if (mtip_get_identify(dd->port, NULL) < 0) {
  2779. rv = -EFAULT;
  2780. goto out3;
  2781. }
  2782. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2783. MTIP_FTL_REBUILD_MAGIC) {
  2784. set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
  2785. return MTIP_FTL_REBUILD_MAGIC;
  2786. }
  2787. mtip_dump_identify(dd->port);
  2788. /* check write protect, over temp and rebuild statuses */
  2789. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  2790. dd->port->log_buf,
  2791. dd->port->log_buf_dma, 1);
  2792. if (rv) {
  2793. dev_warn(&dd->pdev->dev,
  2794. "Error in READ LOG EXT (10h) command\n");
  2795. /* non-critical error, don't fail the load */
  2796. } else {
  2797. buf = (unsigned char *)dd->port->log_buf;
  2798. if (buf[259] & 0x1) {
  2799. dev_info(&dd->pdev->dev,
  2800. "Write protect bit is set.\n");
  2801. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  2802. }
  2803. if (buf[288] == 0xF7) {
  2804. dev_info(&dd->pdev->dev,
  2805. "Exceeded Tmax, drive in thermal shutdown.\n");
  2806. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  2807. }
  2808. if (buf[288] == 0xBF) {
  2809. dev_info(&dd->pdev->dev,
  2810. "Drive indicates rebuild has failed.\n");
  2811. /* TODO */
  2812. }
  2813. }
  2814. /* get write protect progess */
  2815. memset(&attr242, 0, sizeof(struct smart_attr));
  2816. if (mtip_get_smart_attr(dd->port, 242, &attr242))
  2817. dev_warn(&dd->pdev->dev,
  2818. "Unable to check write protect progress\n");
  2819. else
  2820. dev_info(&dd->pdev->dev,
  2821. "Write protect progress: %u%% (%u blocks)\n",
  2822. attr242.cur, le32_to_cpu(attr242.data));
  2823. return rv;
  2824. out3:
  2825. del_timer_sync(&dd->port->cmd_timer);
  2826. /* Disable interrupts on the HBA. */
  2827. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2828. dd->mmio + HOST_CTL);
  2829. /*Release the IRQ. */
  2830. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2831. out2:
  2832. mtip_deinit_port(dd->port);
  2833. /* Free the command/command header memory. */
  2834. dmam_free_coherent(&dd->pdev->dev,
  2835. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2836. dd->port->command_list,
  2837. dd->port->command_list_dma);
  2838. out1:
  2839. /* Free the memory allocated for the for structure. */
  2840. kfree(dd->port);
  2841. return rv;
  2842. }
  2843. /*
  2844. * Called to deinitialize an interface.
  2845. *
  2846. * @dd Pointer to the driver data structure.
  2847. *
  2848. * return value
  2849. * 0
  2850. */
  2851. static int mtip_hw_exit(struct driver_data *dd)
  2852. {
  2853. /*
  2854. * Send standby immediate (E0h) to the drive so that it
  2855. * saves its state.
  2856. */
  2857. if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  2858. if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
  2859. if (mtip_standby_immediate(dd->port))
  2860. dev_warn(&dd->pdev->dev,
  2861. "STANDBY IMMEDIATE failed\n");
  2862. /* de-initialize the port. */
  2863. mtip_deinit_port(dd->port);
  2864. /* Disable interrupts on the HBA. */
  2865. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2866. dd->mmio + HOST_CTL);
  2867. }
  2868. del_timer_sync(&dd->port->cmd_timer);
  2869. /* Release the IRQ. */
  2870. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2871. /* Stop the bottom half tasklet. */
  2872. tasklet_kill(&dd->tasklet);
  2873. /* Free the command/command header memory. */
  2874. dmam_free_coherent(&dd->pdev->dev,
  2875. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
  2876. dd->port->command_list,
  2877. dd->port->command_list_dma);
  2878. /* Free the memory allocated for the for structure. */
  2879. kfree(dd->port);
  2880. return 0;
  2881. }
  2882. /*
  2883. * Issue a Standby Immediate command to the device.
  2884. *
  2885. * This function is called by the Block Layer just before the
  2886. * system powers off during a shutdown.
  2887. *
  2888. * @dd Pointer to the driver data structure.
  2889. *
  2890. * return value
  2891. * 0
  2892. */
  2893. static int mtip_hw_shutdown(struct driver_data *dd)
  2894. {
  2895. /*
  2896. * Send standby immediate (E0h) to the drive so that it
  2897. * saves its state.
  2898. */
  2899. mtip_standby_immediate(dd->port);
  2900. return 0;
  2901. }
  2902. /*
  2903. * Suspend function
  2904. *
  2905. * This function is called by the Block Layer just before the
  2906. * system hibernates.
  2907. *
  2908. * @dd Pointer to the driver data structure.
  2909. *
  2910. * return value
  2911. * 0 Suspend was successful
  2912. * -EFAULT Suspend was not successful
  2913. */
  2914. static int mtip_hw_suspend(struct driver_data *dd)
  2915. {
  2916. /*
  2917. * Send standby immediate (E0h) to the drive
  2918. * so that it saves its state.
  2919. */
  2920. if (mtip_standby_immediate(dd->port) != 0) {
  2921. dev_err(&dd->pdev->dev,
  2922. "Failed standby-immediate command\n");
  2923. return -EFAULT;
  2924. }
  2925. /* Disable interrupts on the HBA.*/
  2926. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2927. dd->mmio + HOST_CTL);
  2928. mtip_deinit_port(dd->port);
  2929. return 0;
  2930. }
  2931. /*
  2932. * Resume function
  2933. *
  2934. * This function is called by the Block Layer as the
  2935. * system resumes.
  2936. *
  2937. * @dd Pointer to the driver data structure.
  2938. *
  2939. * return value
  2940. * 0 Resume was successful
  2941. * -EFAULT Resume was not successful
  2942. */
  2943. static int mtip_hw_resume(struct driver_data *dd)
  2944. {
  2945. /* Perform any needed hardware setup steps */
  2946. hba_setup(dd);
  2947. /* Reset the HBA */
  2948. if (mtip_hba_reset(dd) != 0) {
  2949. dev_err(&dd->pdev->dev,
  2950. "Unable to reset the HBA\n");
  2951. return -EFAULT;
  2952. }
  2953. /*
  2954. * Enable the port, DMA engine, and FIS reception specific
  2955. * h/w in controller.
  2956. */
  2957. mtip_init_port(dd->port);
  2958. mtip_start_port(dd->port);
  2959. /* Enable interrupts on the HBA.*/
  2960. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2961. dd->mmio + HOST_CTL);
  2962. return 0;
  2963. }
  2964. /*
  2965. * Helper function for reusing disk name
  2966. * upon hot insertion.
  2967. */
  2968. static int rssd_disk_name_format(char *prefix,
  2969. int index,
  2970. char *buf,
  2971. int buflen)
  2972. {
  2973. const int base = 'z' - 'a' + 1;
  2974. char *begin = buf + strlen(prefix);
  2975. char *end = buf + buflen;
  2976. char *p;
  2977. int unit;
  2978. p = end - 1;
  2979. *p = '\0';
  2980. unit = base;
  2981. do {
  2982. if (p == begin)
  2983. return -EINVAL;
  2984. *--p = 'a' + (index % unit);
  2985. index = (index / unit) - 1;
  2986. } while (index >= 0);
  2987. memmove(begin, p, end - p);
  2988. memcpy(buf, prefix, strlen(prefix));
  2989. return 0;
  2990. }
  2991. /*
  2992. * Block layer IOCTL handler.
  2993. *
  2994. * @dev Pointer to the block_device structure.
  2995. * @mode ignored
  2996. * @cmd IOCTL command passed from the user application.
  2997. * @arg Argument passed from the user application.
  2998. *
  2999. * return value
  3000. * 0 IOCTL completed successfully.
  3001. * -ENOTTY IOCTL not supported or invalid driver data
  3002. * structure pointer.
  3003. */
  3004. static int mtip_block_ioctl(struct block_device *dev,
  3005. fmode_t mode,
  3006. unsigned cmd,
  3007. unsigned long arg)
  3008. {
  3009. struct driver_data *dd = dev->bd_disk->private_data;
  3010. if (!capable(CAP_SYS_ADMIN))
  3011. return -EACCES;
  3012. if (!dd)
  3013. return -ENOTTY;
  3014. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3015. return -ENOTTY;
  3016. switch (cmd) {
  3017. case BLKFLSBUF:
  3018. return -ENOTTY;
  3019. default:
  3020. return mtip_hw_ioctl(dd, cmd, arg);
  3021. }
  3022. }
  3023. #ifdef CONFIG_COMPAT
  3024. /*
  3025. * Block layer compat IOCTL handler.
  3026. *
  3027. * @dev Pointer to the block_device structure.
  3028. * @mode ignored
  3029. * @cmd IOCTL command passed from the user application.
  3030. * @arg Argument passed from the user application.
  3031. *
  3032. * return value
  3033. * 0 IOCTL completed successfully.
  3034. * -ENOTTY IOCTL not supported or invalid driver data
  3035. * structure pointer.
  3036. */
  3037. static int mtip_block_compat_ioctl(struct block_device *dev,
  3038. fmode_t mode,
  3039. unsigned cmd,
  3040. unsigned long arg)
  3041. {
  3042. struct driver_data *dd = dev->bd_disk->private_data;
  3043. if (!capable(CAP_SYS_ADMIN))
  3044. return -EACCES;
  3045. if (!dd)
  3046. return -ENOTTY;
  3047. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3048. return -ENOTTY;
  3049. switch (cmd) {
  3050. case BLKFLSBUF:
  3051. return -ENOTTY;
  3052. case HDIO_DRIVE_TASKFILE: {
  3053. struct mtip_compat_ide_task_request_s __user *compat_req_task;
  3054. ide_task_request_t req_task;
  3055. int compat_tasksize, outtotal, ret;
  3056. compat_tasksize =
  3057. sizeof(struct mtip_compat_ide_task_request_s);
  3058. compat_req_task =
  3059. (struct mtip_compat_ide_task_request_s __user *) arg;
  3060. if (copy_from_user(&req_task, (void __user *) arg,
  3061. compat_tasksize - (2 * sizeof(compat_long_t))))
  3062. return -EFAULT;
  3063. if (get_user(req_task.out_size, &compat_req_task->out_size))
  3064. return -EFAULT;
  3065. if (get_user(req_task.in_size, &compat_req_task->in_size))
  3066. return -EFAULT;
  3067. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  3068. ret = exec_drive_taskfile(dd, (void __user *) arg,
  3069. &req_task, outtotal);
  3070. if (copy_to_user((void __user *) arg, &req_task,
  3071. compat_tasksize -
  3072. (2 * sizeof(compat_long_t))))
  3073. return -EFAULT;
  3074. if (put_user(req_task.out_size, &compat_req_task->out_size))
  3075. return -EFAULT;
  3076. if (put_user(req_task.in_size, &compat_req_task->in_size))
  3077. return -EFAULT;
  3078. return ret;
  3079. }
  3080. default:
  3081. return mtip_hw_ioctl(dd, cmd, arg);
  3082. }
  3083. }
  3084. #endif
  3085. /*
  3086. * Obtain the geometry of the device.
  3087. *
  3088. * You may think that this function is obsolete, but some applications,
  3089. * fdisk for example still used CHS values. This function describes the
  3090. * device as having 224 heads and 56 sectors per cylinder. These values are
  3091. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  3092. * partition is described in terms of a start and end cylinder this means
  3093. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  3094. * affects performance.
  3095. *
  3096. * @dev Pointer to the block_device strucutre.
  3097. * @geo Pointer to a hd_geometry structure.
  3098. *
  3099. * return value
  3100. * 0 Operation completed successfully.
  3101. * -ENOTTY An error occurred while reading the drive capacity.
  3102. */
  3103. static int mtip_block_getgeo(struct block_device *dev,
  3104. struct hd_geometry *geo)
  3105. {
  3106. struct driver_data *dd = dev->bd_disk->private_data;
  3107. sector_t capacity;
  3108. if (!dd)
  3109. return -ENOTTY;
  3110. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3111. dev_warn(&dd->pdev->dev,
  3112. "Could not get drive capacity.\n");
  3113. return -ENOTTY;
  3114. }
  3115. geo->heads = 224;
  3116. geo->sectors = 56;
  3117. sector_div(capacity, (geo->heads * geo->sectors));
  3118. geo->cylinders = capacity;
  3119. return 0;
  3120. }
  3121. /*
  3122. * Block device operation function.
  3123. *
  3124. * This structure contains pointers to the functions required by the block
  3125. * layer.
  3126. */
  3127. static const struct block_device_operations mtip_block_ops = {
  3128. .ioctl = mtip_block_ioctl,
  3129. #ifdef CONFIG_COMPAT
  3130. .compat_ioctl = mtip_block_compat_ioctl,
  3131. #endif
  3132. .getgeo = mtip_block_getgeo,
  3133. .owner = THIS_MODULE
  3134. };
  3135. /*
  3136. * Block layer make request function.
  3137. *
  3138. * This function is called by the kernel to process a BIO for
  3139. * the P320 device.
  3140. *
  3141. * @queue Pointer to the request queue. Unused other than to obtain
  3142. * the driver data structure.
  3143. * @bio Pointer to the BIO.
  3144. *
  3145. */
  3146. static void mtip_make_request(struct request_queue *queue, struct bio *bio)
  3147. {
  3148. struct driver_data *dd = queue->queuedata;
  3149. struct scatterlist *sg;
  3150. struct bio_vec *bvec;
  3151. int nents = 0;
  3152. int tag = 0;
  3153. if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
  3154. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  3155. &dd->dd_flag))) {
  3156. bio_endio(bio, -ENXIO);
  3157. return;
  3158. }
  3159. if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
  3160. bio_endio(bio, -ENODATA);
  3161. return;
  3162. }
  3163. if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
  3164. &dd->dd_flag) &&
  3165. bio_data_dir(bio))) {
  3166. bio_endio(bio, -ENODATA);
  3167. return;
  3168. }
  3169. if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
  3170. bio_endio(bio, -ENODATA);
  3171. return;
  3172. }
  3173. }
  3174. if (unlikely(!bio_has_data(bio))) {
  3175. blk_queue_flush(queue, 0);
  3176. bio_endio(bio, 0);
  3177. return;
  3178. }
  3179. sg = mtip_hw_get_scatterlist(dd, &tag);
  3180. if (likely(sg != NULL)) {
  3181. blk_queue_bounce(queue, &bio);
  3182. if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
  3183. dev_warn(&dd->pdev->dev,
  3184. "Maximum number of SGL entries exceeded\n");
  3185. bio_io_error(bio);
  3186. mtip_hw_release_scatterlist(dd, tag);
  3187. return;
  3188. }
  3189. /* Create the scatter list for this bio. */
  3190. bio_for_each_segment(bvec, bio, nents) {
  3191. sg_set_page(&sg[nents],
  3192. bvec->bv_page,
  3193. bvec->bv_len,
  3194. bvec->bv_offset);
  3195. }
  3196. /* Issue the read/write. */
  3197. mtip_hw_submit_io(dd,
  3198. bio->bi_sector,
  3199. bio_sectors(bio),
  3200. nents,
  3201. tag,
  3202. bio_endio,
  3203. bio,
  3204. bio_data_dir(bio));
  3205. } else
  3206. bio_io_error(bio);
  3207. }
  3208. /*
  3209. * Block layer initialization function.
  3210. *
  3211. * This function is called once by the PCI layer for each P320
  3212. * device that is connected to the system.
  3213. *
  3214. * @dd Pointer to the driver data structure.
  3215. *
  3216. * return value
  3217. * 0 on success else an error code.
  3218. */
  3219. static int mtip_block_initialize(struct driver_data *dd)
  3220. {
  3221. int rv = 0, wait_for_rebuild = 0;
  3222. sector_t capacity;
  3223. unsigned int index = 0;
  3224. struct kobject *kobj;
  3225. unsigned char thd_name[16];
  3226. if (dd->disk)
  3227. goto skip_create_disk; /* hw init done, before rebuild */
  3228. /* Initialize the protocol layer. */
  3229. wait_for_rebuild = mtip_hw_init(dd);
  3230. if (wait_for_rebuild < 0) {
  3231. dev_err(&dd->pdev->dev,
  3232. "Protocol layer initialization failed\n");
  3233. rv = -EINVAL;
  3234. goto protocol_init_error;
  3235. }
  3236. dd->disk = alloc_disk(MTIP_MAX_MINORS);
  3237. if (dd->disk == NULL) {
  3238. dev_err(&dd->pdev->dev,
  3239. "Unable to allocate gendisk structure\n");
  3240. rv = -EINVAL;
  3241. goto alloc_disk_error;
  3242. }
  3243. /* Generate the disk name, implemented same as in sd.c */
  3244. do {
  3245. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  3246. goto ida_get_error;
  3247. spin_lock(&rssd_index_lock);
  3248. rv = ida_get_new(&rssd_index_ida, &index);
  3249. spin_unlock(&rssd_index_lock);
  3250. } while (rv == -EAGAIN);
  3251. if (rv)
  3252. goto ida_get_error;
  3253. rv = rssd_disk_name_format("rssd",
  3254. index,
  3255. dd->disk->disk_name,
  3256. DISK_NAME_LEN);
  3257. if (rv)
  3258. goto disk_index_error;
  3259. dd->disk->driverfs_dev = &dd->pdev->dev;
  3260. dd->disk->major = dd->major;
  3261. dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
  3262. dd->disk->fops = &mtip_block_ops;
  3263. dd->disk->private_data = dd;
  3264. dd->index = index;
  3265. /*
  3266. * if rebuild pending, start the service thread, and delay the block
  3267. * queue creation and add_disk()
  3268. */
  3269. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3270. goto start_service_thread;
  3271. skip_create_disk:
  3272. /* Allocate the request queue. */
  3273. dd->queue = blk_alloc_queue(GFP_KERNEL);
  3274. if (dd->queue == NULL) {
  3275. dev_err(&dd->pdev->dev,
  3276. "Unable to allocate request queue\n");
  3277. rv = -ENOMEM;
  3278. goto block_queue_alloc_init_error;
  3279. }
  3280. /* Attach our request function to the request queue. */
  3281. blk_queue_make_request(dd->queue, mtip_make_request);
  3282. dd->disk->queue = dd->queue;
  3283. dd->queue->queuedata = dd;
  3284. /* Set device limits. */
  3285. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  3286. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  3287. blk_queue_physical_block_size(dd->queue, 4096);
  3288. blk_queue_max_hw_sectors(dd->queue, 0xffff);
  3289. blk_queue_max_segment_size(dd->queue, 0x400000);
  3290. blk_queue_io_min(dd->queue, 4096);
  3291. /*
  3292. * write back cache is not supported in the device. FUA depends on
  3293. * write back cache support, hence setting flush support to zero.
  3294. */
  3295. blk_queue_flush(dd->queue, 0);
  3296. /* Set the capacity of the device in 512 byte sectors. */
  3297. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3298. dev_warn(&dd->pdev->dev,
  3299. "Could not read drive capacity\n");
  3300. rv = -EIO;
  3301. goto read_capacity_error;
  3302. }
  3303. set_capacity(dd->disk, capacity);
  3304. /* Enable the block device and add it to /dev */
  3305. add_disk(dd->disk);
  3306. /*
  3307. * Now that the disk is active, initialize any sysfs attributes
  3308. * managed by the protocol layer.
  3309. */
  3310. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3311. if (kobj) {
  3312. mtip_hw_sysfs_init(dd, kobj);
  3313. kobject_put(kobj);
  3314. }
  3315. mtip_hw_debugfs_init(dd);
  3316. if (dd->mtip_svc_handler) {
  3317. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3318. return rv; /* service thread created for handling rebuild */
  3319. }
  3320. start_service_thread:
  3321. sprintf(thd_name, "mtip_svc_thd_%02d", index);
  3322. dd->mtip_svc_handler = kthread_run(mtip_service_thread,
  3323. dd, thd_name);
  3324. if (IS_ERR(dd->mtip_svc_handler)) {
  3325. dev_err(&dd->pdev->dev, "service thread failed to start\n");
  3326. dd->mtip_svc_handler = NULL;
  3327. rv = -EFAULT;
  3328. goto kthread_run_error;
  3329. }
  3330. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3331. rv = wait_for_rebuild;
  3332. return rv;
  3333. kthread_run_error:
  3334. mtip_hw_debugfs_exit(dd);
  3335. /* Delete our gendisk. This also removes the device from /dev */
  3336. del_gendisk(dd->disk);
  3337. read_capacity_error:
  3338. blk_cleanup_queue(dd->queue);
  3339. block_queue_alloc_init_error:
  3340. disk_index_error:
  3341. spin_lock(&rssd_index_lock);
  3342. ida_remove(&rssd_index_ida, index);
  3343. spin_unlock(&rssd_index_lock);
  3344. ida_get_error:
  3345. put_disk(dd->disk);
  3346. alloc_disk_error:
  3347. mtip_hw_exit(dd); /* De-initialize the protocol layer. */
  3348. protocol_init_error:
  3349. return rv;
  3350. }
  3351. /*
  3352. * Block layer deinitialization function.
  3353. *
  3354. * Called by the PCI layer as each P320 device is removed.
  3355. *
  3356. * @dd Pointer to the driver data structure.
  3357. *
  3358. * return value
  3359. * 0
  3360. */
  3361. static int mtip_block_remove(struct driver_data *dd)
  3362. {
  3363. struct kobject *kobj;
  3364. if (dd->mtip_svc_handler) {
  3365. set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
  3366. wake_up_interruptible(&dd->port->svc_wait);
  3367. kthread_stop(dd->mtip_svc_handler);
  3368. }
  3369. /* Clean up the sysfs attributes, if created */
  3370. if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
  3371. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3372. if (kobj) {
  3373. mtip_hw_sysfs_exit(dd, kobj);
  3374. kobject_put(kobj);
  3375. }
  3376. }
  3377. mtip_hw_debugfs_exit(dd);
  3378. /*
  3379. * Delete our gendisk structure. This also removes the device
  3380. * from /dev
  3381. */
  3382. if (dd->disk) {
  3383. if (dd->disk->queue)
  3384. del_gendisk(dd->disk);
  3385. else
  3386. put_disk(dd->disk);
  3387. }
  3388. spin_lock(&rssd_index_lock);
  3389. ida_remove(&rssd_index_ida, dd->index);
  3390. spin_unlock(&rssd_index_lock);
  3391. blk_cleanup_queue(dd->queue);
  3392. dd->disk = NULL;
  3393. dd->queue = NULL;
  3394. /* De-initialize the protocol layer. */
  3395. mtip_hw_exit(dd);
  3396. return 0;
  3397. }
  3398. /*
  3399. * Function called by the PCI layer when just before the
  3400. * machine shuts down.
  3401. *
  3402. * If a protocol layer shutdown function is present it will be called
  3403. * by this function.
  3404. *
  3405. * @dd Pointer to the driver data structure.
  3406. *
  3407. * return value
  3408. * 0
  3409. */
  3410. static int mtip_block_shutdown(struct driver_data *dd)
  3411. {
  3412. dev_info(&dd->pdev->dev,
  3413. "Shutting down %s ...\n", dd->disk->disk_name);
  3414. /* Delete our gendisk structure, and cleanup the blk queue. */
  3415. if (dd->disk) {
  3416. if (dd->disk->queue)
  3417. del_gendisk(dd->disk);
  3418. else
  3419. put_disk(dd->disk);
  3420. }
  3421. spin_lock(&rssd_index_lock);
  3422. ida_remove(&rssd_index_ida, dd->index);
  3423. spin_unlock(&rssd_index_lock);
  3424. blk_cleanup_queue(dd->queue);
  3425. dd->disk = NULL;
  3426. dd->queue = NULL;
  3427. mtip_hw_shutdown(dd);
  3428. return 0;
  3429. }
  3430. static int mtip_block_suspend(struct driver_data *dd)
  3431. {
  3432. dev_info(&dd->pdev->dev,
  3433. "Suspending %s ...\n", dd->disk->disk_name);
  3434. mtip_hw_suspend(dd);
  3435. return 0;
  3436. }
  3437. static int mtip_block_resume(struct driver_data *dd)
  3438. {
  3439. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  3440. dd->disk->disk_name);
  3441. mtip_hw_resume(dd);
  3442. return 0;
  3443. }
  3444. /*
  3445. * Called for each supported PCI device detected.
  3446. *
  3447. * This function allocates the private data structure, enables the
  3448. * PCI device and then calls the block layer initialization function.
  3449. *
  3450. * return value
  3451. * 0 on success else an error code.
  3452. */
  3453. static int mtip_pci_probe(struct pci_dev *pdev,
  3454. const struct pci_device_id *ent)
  3455. {
  3456. int rv = 0;
  3457. struct driver_data *dd = NULL;
  3458. /* Allocate memory for this devices private data. */
  3459. dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
  3460. if (dd == NULL) {
  3461. dev_err(&pdev->dev,
  3462. "Unable to allocate memory for driver data\n");
  3463. return -ENOMEM;
  3464. }
  3465. /* Attach the private data to this PCI device. */
  3466. pci_set_drvdata(pdev, dd);
  3467. rv = pcim_enable_device(pdev);
  3468. if (rv < 0) {
  3469. dev_err(&pdev->dev, "Unable to enable device\n");
  3470. goto iomap_err;
  3471. }
  3472. /* Map BAR5 to memory. */
  3473. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  3474. if (rv < 0) {
  3475. dev_err(&pdev->dev, "Unable to map regions\n");
  3476. goto iomap_err;
  3477. }
  3478. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3479. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3480. if (rv) {
  3481. rv = pci_set_consistent_dma_mask(pdev,
  3482. DMA_BIT_MASK(32));
  3483. if (rv) {
  3484. dev_warn(&pdev->dev,
  3485. "64-bit DMA enable failed\n");
  3486. goto setmask_err;
  3487. }
  3488. }
  3489. }
  3490. pci_set_master(pdev);
  3491. if (pci_enable_msi(pdev)) {
  3492. dev_warn(&pdev->dev,
  3493. "Unable to enable MSI interrupt.\n");
  3494. goto block_initialize_err;
  3495. }
  3496. /* Copy the info we may need later into the private data structure. */
  3497. dd->major = mtip_major;
  3498. dd->instance = instance;
  3499. dd->pdev = pdev;
  3500. /* Initialize the block layer. */
  3501. rv = mtip_block_initialize(dd);
  3502. if (rv < 0) {
  3503. dev_err(&pdev->dev,
  3504. "Unable to initialize block layer\n");
  3505. goto block_initialize_err;
  3506. }
  3507. /*
  3508. * Increment the instance count so that each device has a unique
  3509. * instance number.
  3510. */
  3511. instance++;
  3512. if (rv != MTIP_FTL_REBUILD_MAGIC)
  3513. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3514. goto done;
  3515. block_initialize_err:
  3516. pci_disable_msi(pdev);
  3517. setmask_err:
  3518. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3519. iomap_err:
  3520. kfree(dd);
  3521. pci_set_drvdata(pdev, NULL);
  3522. return rv;
  3523. done:
  3524. return rv;
  3525. }
  3526. /*
  3527. * Called for each probed device when the device is removed or the
  3528. * driver is unloaded.
  3529. *
  3530. * return value
  3531. * None
  3532. */
  3533. static void mtip_pci_remove(struct pci_dev *pdev)
  3534. {
  3535. struct driver_data *dd = pci_get_drvdata(pdev);
  3536. int counter = 0;
  3537. set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
  3538. if (mtip_check_surprise_removal(pdev)) {
  3539. while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
  3540. counter++;
  3541. msleep(20);
  3542. if (counter == 10) {
  3543. /* Cleanup the outstanding commands */
  3544. mtip_command_cleanup(dd);
  3545. break;
  3546. }
  3547. }
  3548. }
  3549. /* Clean up the block layer. */
  3550. mtip_block_remove(dd);
  3551. pci_disable_msi(pdev);
  3552. kfree(dd);
  3553. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3554. }
  3555. /*
  3556. * Called for each probed device when the device is suspended.
  3557. *
  3558. * return value
  3559. * 0 Success
  3560. * <0 Error
  3561. */
  3562. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  3563. {
  3564. int rv = 0;
  3565. struct driver_data *dd = pci_get_drvdata(pdev);
  3566. if (!dd) {
  3567. dev_err(&pdev->dev,
  3568. "Driver private datastructure is NULL\n");
  3569. return -EFAULT;
  3570. }
  3571. set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3572. /* Disable ports & interrupts then send standby immediate */
  3573. rv = mtip_block_suspend(dd);
  3574. if (rv < 0) {
  3575. dev_err(&pdev->dev,
  3576. "Failed to suspend controller\n");
  3577. return rv;
  3578. }
  3579. /*
  3580. * Save the pci config space to pdev structure &
  3581. * disable the device
  3582. */
  3583. pci_save_state(pdev);
  3584. pci_disable_device(pdev);
  3585. /* Move to Low power state*/
  3586. pci_set_power_state(pdev, PCI_D3hot);
  3587. return rv;
  3588. }
  3589. /*
  3590. * Called for each probed device when the device is resumed.
  3591. *
  3592. * return value
  3593. * 0 Success
  3594. * <0 Error
  3595. */
  3596. static int mtip_pci_resume(struct pci_dev *pdev)
  3597. {
  3598. int rv = 0;
  3599. struct driver_data *dd;
  3600. dd = pci_get_drvdata(pdev);
  3601. if (!dd) {
  3602. dev_err(&pdev->dev,
  3603. "Driver private datastructure is NULL\n");
  3604. return -EFAULT;
  3605. }
  3606. /* Move the device to active State */
  3607. pci_set_power_state(pdev, PCI_D0);
  3608. /* Restore PCI configuration space */
  3609. pci_restore_state(pdev);
  3610. /* Enable the PCI device*/
  3611. rv = pcim_enable_device(pdev);
  3612. if (rv < 0) {
  3613. dev_err(&pdev->dev,
  3614. "Failed to enable card during resume\n");
  3615. goto err;
  3616. }
  3617. pci_set_master(pdev);
  3618. /*
  3619. * Calls hbaReset, initPort, & startPort function
  3620. * then enables interrupts
  3621. */
  3622. rv = mtip_block_resume(dd);
  3623. if (rv < 0)
  3624. dev_err(&pdev->dev, "Unable to resume\n");
  3625. err:
  3626. clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3627. return rv;
  3628. }
  3629. /*
  3630. * Shutdown routine
  3631. *
  3632. * return value
  3633. * None
  3634. */
  3635. static void mtip_pci_shutdown(struct pci_dev *pdev)
  3636. {
  3637. struct driver_data *dd = pci_get_drvdata(pdev);
  3638. if (dd)
  3639. mtip_block_shutdown(dd);
  3640. }
  3641. /* Table of device ids supported by this driver. */
  3642. static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
  3643. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
  3644. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
  3645. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
  3646. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
  3647. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
  3648. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
  3649. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
  3650. { 0 }
  3651. };
  3652. /* Structure that describes the PCI driver functions. */
  3653. static struct pci_driver mtip_pci_driver = {
  3654. .name = MTIP_DRV_NAME,
  3655. .id_table = mtip_pci_tbl,
  3656. .probe = mtip_pci_probe,
  3657. .remove = mtip_pci_remove,
  3658. .suspend = mtip_pci_suspend,
  3659. .resume = mtip_pci_resume,
  3660. .shutdown = mtip_pci_shutdown,
  3661. };
  3662. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  3663. /*
  3664. * Module initialization function.
  3665. *
  3666. * Called once when the module is loaded. This function allocates a major
  3667. * block device number to the Cyclone devices and registers the PCI layer
  3668. * of the driver.
  3669. *
  3670. * Return value
  3671. * 0 on success else error code.
  3672. */
  3673. static int __init mtip_init(void)
  3674. {
  3675. int error;
  3676. pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  3677. /* Allocate a major block device number to use with this driver. */
  3678. error = register_blkdev(0, MTIP_DRV_NAME);
  3679. if (error <= 0) {
  3680. pr_err("Unable to register block device (%d)\n",
  3681. error);
  3682. return -EBUSY;
  3683. }
  3684. mtip_major = error;
  3685. if (!dfs_parent) {
  3686. dfs_parent = debugfs_create_dir("rssd", NULL);
  3687. if (IS_ERR_OR_NULL(dfs_parent)) {
  3688. pr_warn("Error creating debugfs parent\n");
  3689. dfs_parent = NULL;
  3690. }
  3691. }
  3692. /* Register our PCI operations. */
  3693. error = pci_register_driver(&mtip_pci_driver);
  3694. if (error) {
  3695. debugfs_remove(dfs_parent);
  3696. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3697. }
  3698. return error;
  3699. }
  3700. /*
  3701. * Module de-initialization function.
  3702. *
  3703. * Called once when the module is unloaded. This function deallocates
  3704. * the major block device number allocated by mtip_init() and
  3705. * unregisters the PCI layer of the driver.
  3706. *
  3707. * Return value
  3708. * none
  3709. */
  3710. static void __exit mtip_exit(void)
  3711. {
  3712. debugfs_remove_recursive(dfs_parent);
  3713. /* Release the allocated major block device number. */
  3714. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3715. /* Unregister the PCI driver. */
  3716. pci_unregister_driver(&mtip_pci_driver);
  3717. }
  3718. MODULE_AUTHOR("Micron Technology, Inc");
  3719. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  3720. MODULE_LICENSE("GPL");
  3721. MODULE_VERSION(MTIP_DRV_VERSION);
  3722. module_init(mtip_init);
  3723. module_exit(mtip_exit);