mtip32xx.c 103 KB

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