mtip32xx.c 84 KB

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