mtip32xx.c 86 KB

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