mtip32xx.c 99 KB

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