mtip32xx.c 102 KB

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