pmcraid.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608
  1. /*
  2. * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
  3. *
  4. * Written By: PMC Sierra Corporation
  5. *
  6. * Copyright (C) 2008, 2009 PMC Sierra Inc
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
  21. * USA
  22. *
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #include <linux/errno.h>
  28. #include <linux/kernel.h>
  29. #include <linux/ioport.h>
  30. #include <linux/delay.h>
  31. #include <linux/pci.h>
  32. #include <linux/wait.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/sched.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/firmware.h>
  38. #include <linux/module.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/hdreg.h>
  41. #include <linux/version.h>
  42. #include <linux/io.h>
  43. #include <asm/irq.h>
  44. #include <asm/processor.h>
  45. #include <linux/libata.h>
  46. #include <linux/mutex.h>
  47. #include <scsi/scsi.h>
  48. #include <scsi/scsi_host.h>
  49. #include <scsi/scsi_device.h>
  50. #include <scsi/scsi_tcq.h>
  51. #include <scsi/scsi_eh.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsicam.h>
  54. #include "pmcraid.h"
  55. /*
  56. * Module configuration parameters
  57. */
  58. static unsigned int pmcraid_debug_log;
  59. static unsigned int pmcraid_disable_aen;
  60. static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
  61. /*
  62. * Data structures to support multiple adapters by the LLD.
  63. * pmcraid_adapter_count - count of configured adapters
  64. */
  65. static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
  66. /*
  67. * Supporting user-level control interface through IOCTL commands.
  68. * pmcraid_major - major number to use
  69. * pmcraid_minor - minor number(s) to use
  70. */
  71. static unsigned int pmcraid_major;
  72. static struct class *pmcraid_class;
  73. DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  74. /*
  75. * Module parameters
  76. */
  77. MODULE_AUTHOR("PMC Sierra Corporation, anil_ravindranath@pmc-sierra.com");
  78. MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
  79. MODULE_LICENSE("GPL");
  80. MODULE_VERSION(PMCRAID_DRIVER_VERSION);
  81. module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
  82. MODULE_PARM_DESC(log_level,
  83. "Enables firmware error code logging, default :1 high-severity"
  84. " errors, 2: all errors including high-severity errors,"
  85. " 0: disables logging");
  86. module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
  87. MODULE_PARM_DESC(debug,
  88. "Enable driver verbose message logging. Set 1 to enable."
  89. "(default: 0)");
  90. module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
  91. MODULE_PARM_DESC(disable_aen,
  92. "Disable driver aen notifications to apps. Set 1 to disable."
  93. "(default: 0)");
  94. /* chip specific constants for PMC MaxRAID controllers (same for
  95. * 0x5220 and 0x8010
  96. */
  97. static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
  98. {
  99. .ioastatus = 0x0,
  100. .ioarrin = 0x00040,
  101. .mailbox = 0x7FC30,
  102. .global_intr_mask = 0x00034,
  103. .ioa_host_intr = 0x0009C,
  104. .ioa_host_intr_clr = 0x000A0,
  105. .ioa_host_mask = 0x7FC28,
  106. .ioa_host_mask_clr = 0x7FC28,
  107. .host_ioa_intr = 0x00020,
  108. .host_ioa_intr_clr = 0x00020,
  109. .transop_timeout = 300
  110. }
  111. };
  112. /*
  113. * PCI device ids supported by pmcraid driver
  114. */
  115. static struct pci_device_id pmcraid_pci_table[] __devinitdata = {
  116. { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
  117. 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
  118. },
  119. {}
  120. };
  121. MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
  122. /**
  123. * pmcraid_slave_alloc - Prepare for commands to a device
  124. * @scsi_dev: scsi device struct
  125. *
  126. * This function is called by mid-layer prior to sending any command to the new
  127. * device. Stores resource entry details of the device in scsi_device struct.
  128. * Queuecommand uses the resource handle and other details to fill up IOARCB
  129. * while sending commands to the device.
  130. *
  131. * Return value:
  132. * 0 on success / -ENXIO if device does not exist
  133. */
  134. static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
  135. {
  136. struct pmcraid_resource_entry *temp, *res = NULL;
  137. struct pmcraid_instance *pinstance;
  138. u8 target, bus, lun;
  139. unsigned long lock_flags;
  140. int rc = -ENXIO;
  141. pinstance = shost_priv(scsi_dev->host);
  142. /* Driver exposes VSET and GSCSI resources only; all other device types
  143. * are not exposed. Resource list is synchronized using resource lock
  144. * so any traversal or modifications to the list should be done inside
  145. * this lock
  146. */
  147. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  148. list_for_each_entry(temp, &pinstance->used_res_q, queue) {
  149. /* do not expose VSETs with order-ids >= 240 */
  150. if (RES_IS_VSET(temp->cfg_entry)) {
  151. target = temp->cfg_entry.unique_flags1;
  152. if (target >= PMCRAID_MAX_VSET_TARGETS)
  153. continue;
  154. bus = PMCRAID_VSET_BUS_ID;
  155. lun = 0;
  156. } else if (RES_IS_GSCSI(temp->cfg_entry)) {
  157. target = RES_TARGET(temp->cfg_entry.resource_address);
  158. bus = PMCRAID_PHYS_BUS_ID;
  159. lun = RES_LUN(temp->cfg_entry.resource_address);
  160. } else {
  161. continue;
  162. }
  163. if (bus == scsi_dev->channel &&
  164. target == scsi_dev->id &&
  165. lun == scsi_dev->lun) {
  166. res = temp;
  167. break;
  168. }
  169. }
  170. if (res) {
  171. res->scsi_dev = scsi_dev;
  172. scsi_dev->hostdata = res;
  173. res->change_detected = 0;
  174. atomic_set(&res->read_failures, 0);
  175. atomic_set(&res->write_failures, 0);
  176. rc = 0;
  177. }
  178. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  179. return rc;
  180. }
  181. /**
  182. * pmcraid_slave_configure - Configures a SCSI device
  183. * @scsi_dev: scsi device struct
  184. *
  185. * This fucntion is executed by SCSI mid layer just after a device is first
  186. * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
  187. * timeout value (default 30s) will be over-written to a higher value (60s)
  188. * and max_sectors value will be over-written to 512. It also sets queue depth
  189. * to host->cmd_per_lun value
  190. *
  191. * Return value:
  192. * 0 on success
  193. */
  194. static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
  195. {
  196. struct pmcraid_resource_entry *res = scsi_dev->hostdata;
  197. if (!res)
  198. return 0;
  199. /* LLD exposes VSETs and Enclosure devices only */
  200. if (RES_IS_GSCSI(res->cfg_entry) &&
  201. scsi_dev->type != TYPE_ENCLOSURE)
  202. return -ENXIO;
  203. pmcraid_info("configuring %x:%x:%x:%x\n",
  204. scsi_dev->host->unique_id,
  205. scsi_dev->channel,
  206. scsi_dev->id,
  207. scsi_dev->lun);
  208. if (RES_IS_GSCSI(res->cfg_entry)) {
  209. scsi_dev->allow_restart = 1;
  210. } else if (RES_IS_VSET(res->cfg_entry)) {
  211. scsi_dev->allow_restart = 1;
  212. blk_queue_rq_timeout(scsi_dev->request_queue,
  213. PMCRAID_VSET_IO_TIMEOUT);
  214. blk_queue_max_sectors(scsi_dev->request_queue,
  215. PMCRAID_VSET_MAX_SECTORS);
  216. }
  217. if (scsi_dev->tagged_supported &&
  218. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
  219. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  220. scsi_adjust_queue_depth(scsi_dev, MSG_SIMPLE_TAG,
  221. scsi_dev->host->cmd_per_lun);
  222. } else {
  223. scsi_adjust_queue_depth(scsi_dev, 0,
  224. scsi_dev->host->cmd_per_lun);
  225. }
  226. return 0;
  227. }
  228. /**
  229. * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
  230. *
  231. * @scsi_dev: scsi device struct
  232. *
  233. * This is called by mid-layer before removing a device. Pointer assignments
  234. * done in pmcraid_slave_alloc will be reset to NULL here.
  235. *
  236. * Return value
  237. * none
  238. */
  239. static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
  240. {
  241. struct pmcraid_resource_entry *res;
  242. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  243. if (res)
  244. res->scsi_dev = NULL;
  245. scsi_dev->hostdata = NULL;
  246. }
  247. /**
  248. * pmcraid_change_queue_depth - Change the device's queue depth
  249. * @scsi_dev: scsi device struct
  250. * @depth: depth to set
  251. *
  252. * Return value
  253. * actual depth set
  254. */
  255. static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth)
  256. {
  257. if (depth > PMCRAID_MAX_CMD_PER_LUN)
  258. depth = PMCRAID_MAX_CMD_PER_LUN;
  259. scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
  260. return scsi_dev->queue_depth;
  261. }
  262. /**
  263. * pmcraid_change_queue_type - Change the device's queue type
  264. * @scsi_dev: scsi device struct
  265. * @tag: type of tags to use
  266. *
  267. * Return value:
  268. * actual queue type set
  269. */
  270. static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
  271. {
  272. struct pmcraid_resource_entry *res;
  273. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  274. if ((res) && scsi_dev->tagged_supported &&
  275. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
  276. scsi_set_tag_type(scsi_dev, tag);
  277. if (tag)
  278. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  279. else
  280. scsi_deactivate_tcq(scsi_dev, scsi_dev->queue_depth);
  281. } else
  282. tag = 0;
  283. return tag;
  284. }
  285. /**
  286. * pmcraid_init_cmdblk - initializes a command block
  287. *
  288. * @cmd: pointer to struct pmcraid_cmd to be initialized
  289. * @index: if >=0 first time initialization; otherwise reinitialization
  290. *
  291. * Return Value
  292. * None
  293. */
  294. void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
  295. {
  296. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  297. dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
  298. if (index >= 0) {
  299. /* first time initialization (called from probe) */
  300. u32 ioasa_offset =
  301. offsetof(struct pmcraid_control_block, ioasa);
  302. cmd->index = index;
  303. ioarcb->response_handle = cpu_to_le32(index << 2);
  304. ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
  305. ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
  306. ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
  307. } else {
  308. /* re-initialization of various lengths, called once command is
  309. * processed by IOA
  310. */
  311. memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
  312. ioarcb->request_flags0 = 0;
  313. ioarcb->request_flags1 = 0;
  314. ioarcb->cmd_timeout = 0;
  315. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  316. ioarcb->ioadl_bus_addr = 0;
  317. ioarcb->ioadl_length = 0;
  318. ioarcb->data_transfer_length = 0;
  319. ioarcb->add_cmd_param_length = 0;
  320. ioarcb->add_cmd_param_offset = 0;
  321. cmd->ioa_cb->ioasa.ioasc = 0;
  322. cmd->ioa_cb->ioasa.residual_data_length = 0;
  323. cmd->u.time_left = 0;
  324. }
  325. cmd->cmd_done = NULL;
  326. cmd->scsi_cmd = NULL;
  327. cmd->release = 0;
  328. cmd->completion_req = 0;
  329. cmd->dma_handle = 0;
  330. init_timer(&cmd->timer);
  331. }
  332. /**
  333. * pmcraid_reinit_cmdblk - reinitialize a command block
  334. *
  335. * @cmd: pointer to struct pmcraid_cmd to be reinitialized
  336. *
  337. * Return Value
  338. * None
  339. */
  340. static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
  341. {
  342. pmcraid_init_cmdblk(cmd, -1);
  343. }
  344. /**
  345. * pmcraid_get_free_cmd - get a free cmd block from command block pool
  346. * @pinstance: adapter instance structure
  347. *
  348. * Return Value:
  349. * returns pointer to cmd block or NULL if no blocks are available
  350. */
  351. static struct pmcraid_cmd *pmcraid_get_free_cmd(
  352. struct pmcraid_instance *pinstance
  353. )
  354. {
  355. struct pmcraid_cmd *cmd = NULL;
  356. unsigned long lock_flags;
  357. /* free cmd block list is protected by free_pool_lock */
  358. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  359. if (!list_empty(&pinstance->free_cmd_pool)) {
  360. cmd = list_entry(pinstance->free_cmd_pool.next,
  361. struct pmcraid_cmd, free_list);
  362. list_del(&cmd->free_list);
  363. }
  364. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  365. /* Initialize the command block before giving it the caller */
  366. if (cmd != NULL)
  367. pmcraid_reinit_cmdblk(cmd);
  368. return cmd;
  369. }
  370. /**
  371. * pmcraid_return_cmd - return a completed command block back into free pool
  372. * @cmd: pointer to the command block
  373. *
  374. * Return Value:
  375. * nothing
  376. */
  377. void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
  378. {
  379. struct pmcraid_instance *pinstance = cmd->drv_inst;
  380. unsigned long lock_flags;
  381. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  382. list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
  383. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  384. }
  385. /**
  386. * pmcraid_read_interrupts - reads IOA interrupts
  387. *
  388. * @pinstance: pointer to adapter instance structure
  389. *
  390. * Return value
  391. * interrupts read from IOA
  392. */
  393. static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
  394. {
  395. return ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  396. }
  397. /**
  398. * pmcraid_disable_interrupts - Masks and clears all specified interrupts
  399. *
  400. * @pinstance: pointer to per adapter instance structure
  401. * @intrs: interrupts to disable
  402. *
  403. * Return Value
  404. * None
  405. */
  406. static void pmcraid_disable_interrupts(
  407. struct pmcraid_instance *pinstance,
  408. u32 intrs
  409. )
  410. {
  411. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  412. u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
  413. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  414. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
  415. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
  416. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  417. }
  418. /**
  419. * pmcraid_enable_interrupts - Enables specified interrupts
  420. *
  421. * @pinstance: pointer to per adapter instance structure
  422. * @intr: interrupts to enable
  423. *
  424. * Return Value
  425. * None
  426. */
  427. static void pmcraid_enable_interrupts(
  428. struct pmcraid_instance *pinstance,
  429. u32 intrs
  430. )
  431. {
  432. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  433. u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
  434. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  435. iowrite32(~intrs, pinstance->int_regs.ioa_host_interrupt_mask_reg);
  436. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  437. pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
  438. ioread32(pinstance->int_regs.global_interrupt_mask_reg),
  439. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
  440. }
  441. /**
  442. * pmcraid_reset_type - Determine the required reset type
  443. * @pinstance: pointer to adapter instance structure
  444. *
  445. * IOA requires hard reset if any of the following conditions is true.
  446. * 1. If HRRQ valid interrupt is not masked
  447. * 2. IOA reset alert doorbell is set
  448. * 3. If there are any error interrupts
  449. */
  450. static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
  451. {
  452. u32 mask;
  453. u32 intrs;
  454. u32 alerts;
  455. mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  456. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  457. alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  458. if ((mask & INTRS_HRRQ_VALID) == 0 ||
  459. (alerts & DOORBELL_IOA_RESET_ALERT) ||
  460. (intrs & PMCRAID_ERROR_INTERRUPTS)) {
  461. pmcraid_info("IOA requires hard reset\n");
  462. pinstance->ioa_hard_reset = 1;
  463. }
  464. /* If unit check is active, trigger the dump */
  465. if (intrs & INTRS_IOA_UNIT_CHECK)
  466. pinstance->ioa_unit_check = 1;
  467. }
  468. /**
  469. * pmcraid_bist_done - completion function for PCI BIST
  470. * @cmd: pointer to reset command
  471. * Return Value
  472. * none
  473. */
  474. static void pmcraid_ioa_reset(struct pmcraid_cmd *);
  475. static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
  476. {
  477. struct pmcraid_instance *pinstance = cmd->drv_inst;
  478. unsigned long lock_flags;
  479. int rc;
  480. u16 pci_reg;
  481. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  482. /* If PCI config space can't be accessed wait for another two secs */
  483. if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
  484. cmd->u.time_left > 0) {
  485. pmcraid_info("BIST not complete, waiting another 2 secs\n");
  486. cmd->timer.expires = jiffies + cmd->u.time_left;
  487. cmd->u.time_left = 0;
  488. cmd->timer.data = (unsigned long)cmd;
  489. cmd->timer.function =
  490. (void (*)(unsigned long))pmcraid_bist_done;
  491. add_timer(&cmd->timer);
  492. } else {
  493. cmd->u.time_left = 0;
  494. pmcraid_info("BIST is complete, proceeding with reset\n");
  495. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  496. pmcraid_ioa_reset(cmd);
  497. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  498. }
  499. }
  500. /**
  501. * pmcraid_start_bist - starts BIST
  502. * @cmd: pointer to reset cmd
  503. * Return Value
  504. * none
  505. */
  506. static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
  507. {
  508. struct pmcraid_instance *pinstance = cmd->drv_inst;
  509. u32 doorbells, intrs;
  510. /* proceed with bist and wait for 2 seconds */
  511. iowrite32(DOORBELL_IOA_START_BIST,
  512. pinstance->int_regs.host_ioa_interrupt_reg);
  513. doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  514. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  515. pmcraid_info("doorbells after start bist: %x intrs: %x \n",
  516. doorbells, intrs);
  517. cmd->u.time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  518. cmd->timer.data = (unsigned long)cmd;
  519. cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  520. cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
  521. add_timer(&cmd->timer);
  522. }
  523. /**
  524. * pmcraid_reset_alert_done - completion routine for reset_alert
  525. * @cmd: pointer to command block used in reset sequence
  526. * Return value
  527. * None
  528. */
  529. static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
  530. {
  531. struct pmcraid_instance *pinstance = cmd->drv_inst;
  532. u32 status = ioread32(pinstance->ioa_status);
  533. unsigned long lock_flags;
  534. /* if the critical operation in progress bit is set or the wait times
  535. * out, invoke reset engine to proceed with hard reset. If there is
  536. * some more time to wait, restart the timer
  537. */
  538. if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
  539. cmd->u.time_left <= 0) {
  540. pmcraid_info("critical op is reset proceeding with reset\n");
  541. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  542. pmcraid_ioa_reset(cmd);
  543. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  544. } else {
  545. pmcraid_info("critical op is not yet reset waiting again\n");
  546. /* restart timer if some more time is available to wait */
  547. cmd->u.time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  548. cmd->timer.data = (unsigned long)cmd;
  549. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  550. cmd->timer.function =
  551. (void (*)(unsigned long))pmcraid_reset_alert_done;
  552. add_timer(&cmd->timer);
  553. }
  554. }
  555. /**
  556. * pmcraid_reset_alert - alerts IOA for a possible reset
  557. * @cmd : command block to be used for reset sequence.
  558. *
  559. * Return Value
  560. * returns 0 if pci config-space is accessible and RESET_DOORBELL is
  561. * successfully written to IOA. Returns non-zero in case pci_config_space
  562. * is not accessible
  563. */
  564. static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
  565. {
  566. struct pmcraid_instance *pinstance = cmd->drv_inst;
  567. u32 doorbells;
  568. int rc;
  569. u16 pci_reg;
  570. /* If we are able to access IOA PCI config space, alert IOA that we are
  571. * going to reset it soon. This enables IOA to preserv persistent error
  572. * data if any. In case memory space is not accessible, proceed with
  573. * BIST or slot_reset
  574. */
  575. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  576. if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
  577. /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
  578. * reset IOA doesn't generate any interrupts when CRITICAL
  579. * OPERATION bit is reset. A timer is started to wait for this
  580. * bit to be reset.
  581. */
  582. cmd->u.time_left = PMCRAID_RESET_TIMEOUT;
  583. cmd->timer.data = (unsigned long)cmd;
  584. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  585. cmd->timer.function =
  586. (void (*)(unsigned long))pmcraid_reset_alert_done;
  587. add_timer(&cmd->timer);
  588. iowrite32(DOORBELL_IOA_RESET_ALERT,
  589. pinstance->int_regs.host_ioa_interrupt_reg);
  590. doorbells =
  591. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  592. pmcraid_info("doorbells after reset alert: %x\n", doorbells);
  593. } else {
  594. pmcraid_info("PCI config is not accessible starting BIST\n");
  595. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  596. pmcraid_start_bist(cmd);
  597. }
  598. }
  599. /**
  600. * pmcraid_timeout_handler - Timeout handler for internally generated ops
  601. *
  602. * @cmd : pointer to command structure, that got timedout
  603. *
  604. * This function blocks host requests and initiates an adapter reset.
  605. *
  606. * Return value:
  607. * None
  608. */
  609. static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
  610. {
  611. struct pmcraid_instance *pinstance = cmd->drv_inst;
  612. unsigned long lock_flags;
  613. dev_info(&pinstance->pdev->dev,
  614. "Adapter being reset due to command timeout.\n");
  615. /* Command timeouts result in hard reset sequence. The command that got
  616. * timed out may be the one used as part of reset sequence. In this
  617. * case restart reset sequence using the same command block even if
  618. * reset is in progress. Otherwise fail this command and get a free
  619. * command block to restart the reset sequence.
  620. */
  621. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  622. if (!pinstance->ioa_reset_in_progress) {
  623. pinstance->ioa_reset_attempts = 0;
  624. cmd = pmcraid_get_free_cmd(pinstance);
  625. /* If we are out of command blocks, just return here itself.
  626. * Some other command's timeout handler can do the reset job
  627. */
  628. if (cmd == NULL) {
  629. spin_unlock_irqrestore(pinstance->host->host_lock,
  630. lock_flags);
  631. pmcraid_err("no free cmnd block for timeout handler\n");
  632. return;
  633. }
  634. pinstance->reset_cmd = cmd;
  635. pinstance->ioa_reset_in_progress = 1;
  636. } else {
  637. pmcraid_info("reset is already in progress\n");
  638. if (pinstance->reset_cmd != cmd) {
  639. /* This command should have been given to IOA, this
  640. * command will be completed by fail_outstanding_cmds
  641. * anyway
  642. */
  643. pmcraid_err("cmd is pending but reset in progress\n");
  644. }
  645. /* If this command was being used as part of the reset
  646. * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
  647. * causes fail_outstanding_commands not to return the command
  648. * block back to free pool
  649. */
  650. if (cmd == pinstance->reset_cmd)
  651. cmd->cmd_done = pmcraid_ioa_reset;
  652. }
  653. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  654. scsi_block_requests(pinstance->host);
  655. pmcraid_reset_alert(cmd);
  656. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  657. }
  658. /**
  659. * pmcraid_internal_done - completion routine for internally generated cmds
  660. *
  661. * @cmd: command that got response from IOA
  662. *
  663. * Return Value:
  664. * none
  665. */
  666. static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
  667. {
  668. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  669. cmd->ioa_cb->ioarcb.cdb[0],
  670. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  671. /* Some of the internal commands are sent with callers blocking for the
  672. * response. Same will be indicated as part of cmd->completion_req
  673. * field. Response path needs to wake up any waiters waiting for cmd
  674. * completion if this flag is set.
  675. */
  676. if (cmd->completion_req) {
  677. cmd->completion_req = 0;
  678. complete(&cmd->wait_for_completion);
  679. }
  680. /* most of the internal commands are completed by caller itself, so
  681. * no need to return the command block back to free pool until we are
  682. * required to do so (e.g once done with initialization).
  683. */
  684. if (cmd->release) {
  685. cmd->release = 0;
  686. pmcraid_return_cmd(cmd);
  687. }
  688. }
  689. /**
  690. * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
  691. *
  692. * @cmd: command that got response from IOA
  693. *
  694. * This routine is called after driver re-reads configuration table due to a
  695. * lost CCN. It returns the command block back to free pool and schedules
  696. * worker thread to add/delete devices into the system.
  697. *
  698. * Return Value:
  699. * none
  700. */
  701. static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
  702. {
  703. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  704. cmd->ioa_cb->ioarcb.cdb[0],
  705. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  706. if (cmd->release) {
  707. cmd->release = 0;
  708. pmcraid_return_cmd(cmd);
  709. }
  710. pmcraid_info("scheduling worker for config table reinitialization\n");
  711. schedule_work(&cmd->drv_inst->worker_q);
  712. }
  713. /**
  714. * pmcraid_erp_done - Process completion of SCSI error response from device
  715. * @cmd: pmcraid_command
  716. *
  717. * This function copies the sense buffer into the scsi_cmd struct and completes
  718. * scsi_cmd by calling scsi_done function.
  719. *
  720. * Return value:
  721. * none
  722. */
  723. static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
  724. {
  725. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  726. struct pmcraid_instance *pinstance = cmd->drv_inst;
  727. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  728. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
  729. scsi_cmd->result |= (DID_ERROR << 16);
  730. scmd_printk(KERN_INFO, scsi_cmd,
  731. "command CDB[0] = %x failed with IOASC: 0x%08X\n",
  732. cmd->ioa_cb->ioarcb.cdb[0], ioasc);
  733. }
  734. /* if we had allocated sense buffers for request sense, copy the sense
  735. * release the buffers
  736. */
  737. if (cmd->sense_buffer != NULL) {
  738. memcpy(scsi_cmd->sense_buffer,
  739. cmd->sense_buffer,
  740. SCSI_SENSE_BUFFERSIZE);
  741. pci_free_consistent(pinstance->pdev,
  742. SCSI_SENSE_BUFFERSIZE,
  743. cmd->sense_buffer, cmd->sense_buffer_dma);
  744. cmd->sense_buffer = NULL;
  745. cmd->sense_buffer_dma = 0;
  746. }
  747. scsi_dma_unmap(scsi_cmd);
  748. pmcraid_return_cmd(cmd);
  749. scsi_cmd->scsi_done(scsi_cmd);
  750. }
  751. /**
  752. * pmcraid_fire_command - sends an IOA command to adapter
  753. *
  754. * This function adds the given block into pending command list
  755. * and returns without waiting
  756. *
  757. * @cmd : command to be sent to the device
  758. *
  759. * Return Value
  760. * None
  761. */
  762. static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
  763. {
  764. struct pmcraid_instance *pinstance = cmd->drv_inst;
  765. unsigned long lock_flags;
  766. /* Add this command block to pending cmd pool. We do this prior to
  767. * writting IOARCB to ioarrin because IOA might complete the command
  768. * by the time we are about to add it to the list. Response handler
  769. * (isr/tasklet) looks for cmb block in the pending pending list.
  770. */
  771. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  772. list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
  773. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  774. atomic_inc(&pinstance->outstanding_cmds);
  775. /* driver writes lower 32-bit value of IOARCB address only */
  776. mb();
  777. iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
  778. pinstance->ioarrin);
  779. }
  780. /**
  781. * pmcraid_send_cmd - fires a command to IOA
  782. *
  783. * This function also sets up timeout function, and command completion
  784. * function
  785. *
  786. * @cmd: pointer to the command block to be fired to IOA
  787. * @cmd_done: command completion function, called once IOA responds
  788. * @timeout: timeout to wait for this command completion
  789. * @timeout_func: timeout handler
  790. *
  791. * Return value
  792. * none
  793. */
  794. static void pmcraid_send_cmd(
  795. struct pmcraid_cmd *cmd,
  796. void (*cmd_done) (struct pmcraid_cmd *),
  797. unsigned long timeout,
  798. void (*timeout_func) (struct pmcraid_cmd *)
  799. )
  800. {
  801. /* initialize done function */
  802. cmd->cmd_done = cmd_done;
  803. if (timeout_func) {
  804. /* setup timeout handler */
  805. cmd->timer.data = (unsigned long)cmd;
  806. cmd->timer.expires = jiffies + timeout;
  807. cmd->timer.function = (void (*)(unsigned long))timeout_func;
  808. add_timer(&cmd->timer);
  809. }
  810. /* fire the command to IOA */
  811. _pmcraid_fire_command(cmd);
  812. }
  813. /**
  814. * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
  815. *
  816. * @cmd: pointer to the command block used as part of reset sequence
  817. *
  818. * Return Value
  819. * None
  820. */
  821. static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
  822. {
  823. pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
  824. cmd->ioa_cb->ioarcb.cdb[0],
  825. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  826. /* Note that commands sent during reset require next command to be sent
  827. * to IOA. Hence reinit the done function as well as timeout function
  828. */
  829. pmcraid_reinit_cmdblk(cmd);
  830. cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
  831. cmd->ioa_cb->ioarcb.resource_handle =
  832. cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  833. cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
  834. cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
  835. /* fire shutdown command to hardware. */
  836. pmcraid_info("firing normal shutdown command (%d) to IOA\n",
  837. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
  838. pmcraid_send_cmd(cmd, pmcraid_ioa_reset,
  839. PMCRAID_SHUTDOWN_TIMEOUT,
  840. pmcraid_timeout_handler);
  841. }
  842. /**
  843. * pmcraid_identify_hrrq - registers host rrq buffers with IOA
  844. * @cmd: pointer to command block to be used for identify hrrq
  845. *
  846. * Return Value
  847. * 0 in case of success, otherwise non-zero failure code
  848. */
  849. static void pmcraid_querycfg(struct pmcraid_cmd *);
  850. static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
  851. {
  852. struct pmcraid_instance *pinstance = cmd->drv_inst;
  853. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  854. int index = 0;
  855. __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
  856. u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
  857. pmcraid_reinit_cmdblk(cmd);
  858. /* Initialize ioarcb */
  859. ioarcb->request_type = REQ_TYPE_IOACMD;
  860. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  861. /* initialize the hrrq number where IOA will respond to this command */
  862. ioarcb->hrrq_id = index;
  863. ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
  864. ioarcb->cdb[1] = index;
  865. /* IOA expects 64-bit pci address to be written in B.E format
  866. * (i.e cdb[2]=MSByte..cdb[9]=LSB.
  867. */
  868. pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb => %llx:%llx\n",
  869. hrrq_addr, ioarcb->ioarcb_bus_addr);
  870. memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
  871. memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
  872. /* Subsequent commands require HRRQ identification to be successful.
  873. * Note that this gets called even during reset from SCSI mid-layer
  874. * or tasklet
  875. */
  876. pmcraid_send_cmd(cmd, pmcraid_querycfg,
  877. PMCRAID_INTERNAL_TIMEOUT,
  878. pmcraid_timeout_handler);
  879. }
  880. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
  881. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
  882. /**
  883. * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
  884. *
  885. * @cmd: initialized command block pointer
  886. *
  887. * Return Value
  888. * none
  889. */
  890. static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
  891. {
  892. if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
  893. atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
  894. else
  895. atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
  896. pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
  897. }
  898. /**
  899. * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
  900. *
  901. * @pinstance: pointer to adapter instance structure
  902. * @type: HCAM type
  903. *
  904. * Return Value
  905. * pointer to initialized pmcraid_cmd structure or NULL
  906. */
  907. static struct pmcraid_cmd *pmcraid_init_hcam
  908. (
  909. struct pmcraid_instance *pinstance,
  910. u8 type
  911. )
  912. {
  913. struct pmcraid_cmd *cmd;
  914. struct pmcraid_ioarcb *ioarcb;
  915. struct pmcraid_ioadl_desc *ioadl;
  916. struct pmcraid_hostrcb *hcam;
  917. void (*cmd_done) (struct pmcraid_cmd *);
  918. dma_addr_t dma;
  919. int rcb_size;
  920. cmd = pmcraid_get_free_cmd(pinstance);
  921. if (!cmd) {
  922. pmcraid_err("no free command blocks for hcam\n");
  923. return cmd;
  924. }
  925. if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
  926. rcb_size = sizeof(struct pmcraid_hcam_ccn);
  927. cmd_done = pmcraid_process_ccn;
  928. dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
  929. hcam = &pinstance->ccn;
  930. } else {
  931. rcb_size = sizeof(struct pmcraid_hcam_ldn);
  932. cmd_done = pmcraid_process_ldn;
  933. dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
  934. hcam = &pinstance->ldn;
  935. }
  936. /* initialize command pointer used for HCAM registration */
  937. hcam->cmd = cmd;
  938. ioarcb = &cmd->ioa_cb->ioarcb;
  939. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  940. offsetof(struct pmcraid_ioarcb,
  941. add_data.u.ioadl[0]));
  942. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  943. ioadl = ioarcb->add_data.u.ioadl;
  944. /* Initialize ioarcb */
  945. ioarcb->request_type = REQ_TYPE_HCAM;
  946. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  947. ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
  948. ioarcb->cdb[1] = type;
  949. ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
  950. ioarcb->cdb[8] = (rcb_size) & 0xFF;
  951. ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
  952. ioadl[0].flags |= cpu_to_le32(IOADL_FLAGS_READ_LAST);
  953. ioadl[0].data_len = cpu_to_le32(rcb_size);
  954. ioadl[0].address = cpu_to_le32(dma);
  955. cmd->cmd_done = cmd_done;
  956. return cmd;
  957. }
  958. /**
  959. * pmcraid_send_hcam - Send an HCAM to IOA
  960. * @pinstance: ioa config struct
  961. * @type: HCAM type
  962. *
  963. * This function will send a Host Controlled Async command to IOA.
  964. *
  965. * Return value:
  966. * none
  967. */
  968. static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
  969. {
  970. struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
  971. pmcraid_send_hcam_cmd(cmd);
  972. }
  973. /**
  974. * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
  975. *
  976. * @cmd: pointer to cmd that is used as cancelling command
  977. * @cmd_to_cancel: pointer to the command that needs to be cancelled
  978. */
  979. static void pmcraid_prepare_cancel_cmd(
  980. struct pmcraid_cmd *cmd,
  981. struct pmcraid_cmd *cmd_to_cancel
  982. )
  983. {
  984. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  985. __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
  986. /* Get the resource handle to where the command to be aborted has been
  987. * sent.
  988. */
  989. ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
  990. ioarcb->request_type = REQ_TYPE_IOACMD;
  991. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  992. ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
  993. /* IOARCB address of the command to be cancelled is given in
  994. * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
  995. * IOARCB address are not masked.
  996. */
  997. ioarcb_addr = cpu_to_be64(ioarcb_addr);
  998. memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
  999. }
  1000. /**
  1001. * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
  1002. *
  1003. * @cmd: command to be used as cancelling command
  1004. * @type: HCAM type
  1005. * @cmd_done: op done function for the cancelling command
  1006. */
  1007. static void pmcraid_cancel_hcam(
  1008. struct pmcraid_cmd *cmd,
  1009. u8 type,
  1010. void (*cmd_done) (struct pmcraid_cmd *)
  1011. )
  1012. {
  1013. struct pmcraid_instance *pinstance;
  1014. struct pmcraid_hostrcb *hcam;
  1015. pinstance = cmd->drv_inst;
  1016. hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
  1017. &pinstance->ldn : &pinstance->ccn;
  1018. /* prepare for cancelling previous hcam command. If the HCAM is
  1019. * currently not pending with IOA, we would have hcam->cmd as non-null
  1020. */
  1021. if (hcam->cmd == NULL)
  1022. return;
  1023. pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
  1024. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  1025. * schedule queuecommand while we are doing this
  1026. */
  1027. pmcraid_send_cmd(cmd, cmd_done,
  1028. PMCRAID_INTERNAL_TIMEOUT,
  1029. pmcraid_timeout_handler);
  1030. }
  1031. /**
  1032. * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
  1033. *
  1034. * @cmd: command block to be used for cancelling the HCAM
  1035. */
  1036. static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
  1037. {
  1038. pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
  1039. cmd->ioa_cb->ioarcb.cdb[0],
  1040. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  1041. pmcraid_reinit_cmdblk(cmd);
  1042. pmcraid_cancel_hcam(cmd,
  1043. PMCRAID_HCAM_CODE_CONFIG_CHANGE,
  1044. pmcraid_ioa_shutdown);
  1045. }
  1046. /**
  1047. * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
  1048. *
  1049. * @cmd: command block to be used for cancelling the HCAM
  1050. */
  1051. static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
  1052. {
  1053. pmcraid_cancel_hcam(cmd,
  1054. PMCRAID_HCAM_CODE_LOG_DATA,
  1055. pmcraid_cancel_ccn);
  1056. }
  1057. /**
  1058. * pmcraid_expose_resource - check if the resource can be exposed to OS
  1059. *
  1060. * @cfgte: pointer to configuration table entry of the resource
  1061. *
  1062. * Return value:
  1063. * true if resource can be added to midlayer, false(0) otherwise
  1064. */
  1065. static int pmcraid_expose_resource(struct pmcraid_config_table_entry *cfgte)
  1066. {
  1067. int retval = 0;
  1068. if (cfgte->resource_type == RES_TYPE_VSET)
  1069. retval = ((cfgte->unique_flags1 & 0xFF) < 0xFE);
  1070. else if (cfgte->resource_type == RES_TYPE_GSCSI)
  1071. retval = (RES_BUS(cfgte->resource_address) !=
  1072. PMCRAID_VIRTUAL_ENCL_BUS_ID);
  1073. return retval;
  1074. }
  1075. /* attributes supported by pmcraid_event_family */
  1076. enum {
  1077. PMCRAID_AEN_ATTR_UNSPEC,
  1078. PMCRAID_AEN_ATTR_EVENT,
  1079. __PMCRAID_AEN_ATTR_MAX,
  1080. };
  1081. #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
  1082. /* commands supported by pmcraid_event_family */
  1083. enum {
  1084. PMCRAID_AEN_CMD_UNSPEC,
  1085. PMCRAID_AEN_CMD_EVENT,
  1086. __PMCRAID_AEN_CMD_MAX,
  1087. };
  1088. #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
  1089. static struct genl_family pmcraid_event_family = {
  1090. .id = GENL_ID_GENERATE,
  1091. .name = "pmcraid",
  1092. .version = 1,
  1093. .maxattr = PMCRAID_AEN_ATTR_MAX
  1094. };
  1095. /**
  1096. * pmcraid_netlink_init - registers pmcraid_event_family
  1097. *
  1098. * Return value:
  1099. * 0 if the pmcraid_event_family is successfully registered
  1100. * with netlink generic, non-zero otherwise
  1101. */
  1102. static int pmcraid_netlink_init(void)
  1103. {
  1104. int result;
  1105. result = genl_register_family(&pmcraid_event_family);
  1106. if (result)
  1107. return result;
  1108. pmcraid_info("registered NETLINK GENERIC group: %d\n",
  1109. pmcraid_event_family.id);
  1110. return result;
  1111. }
  1112. /**
  1113. * pmcraid_netlink_release - unregisters pmcraid_event_family
  1114. *
  1115. * Return value:
  1116. * none
  1117. */
  1118. static void pmcraid_netlink_release(void)
  1119. {
  1120. genl_unregister_family(&pmcraid_event_family);
  1121. }
  1122. /**
  1123. * pmcraid_notify_aen - sends event msg to user space application
  1124. * @pinstance: pointer to adapter instance structure
  1125. * @type: HCAM type
  1126. *
  1127. * Return value:
  1128. * 0 if success, error value in case of any failure.
  1129. */
  1130. static int pmcraid_notify_aen(struct pmcraid_instance *pinstance, u8 type)
  1131. {
  1132. struct sk_buff *skb;
  1133. struct pmcraid_aen_msg *aen_msg;
  1134. void *msg_header;
  1135. int data_size, total_size;
  1136. int result;
  1137. if (type == PMCRAID_HCAM_CODE_LOG_DATA) {
  1138. aen_msg = pinstance->ldn.msg;
  1139. data_size = pinstance->ldn.hcam->data_len;
  1140. } else {
  1141. aen_msg = pinstance->ccn.msg;
  1142. data_size = pinstance->ccn.hcam->data_len;
  1143. }
  1144. data_size += sizeof(struct pmcraid_hcam_hdr);
  1145. aen_msg->hostno = (pinstance->host->unique_id << 16 |
  1146. MINOR(pinstance->cdev.dev));
  1147. aen_msg->length = data_size;
  1148. data_size += sizeof(*aen_msg);
  1149. total_size = nla_total_size(data_size);
  1150. skb = genlmsg_new(total_size, GFP_ATOMIC);
  1151. if (!skb) {
  1152. pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
  1153. total_size);
  1154. return -ENOMEM;
  1155. }
  1156. /* add the genetlink message header */
  1157. msg_header = genlmsg_put(skb, 0, 0,
  1158. &pmcraid_event_family, 0,
  1159. PMCRAID_AEN_CMD_EVENT);
  1160. if (!msg_header) {
  1161. pmcraid_err("failed to copy command details\n");
  1162. nlmsg_free(skb);
  1163. return -ENOMEM;
  1164. }
  1165. result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
  1166. if (result) {
  1167. pmcraid_err("failed to copy AEN attribute data \n");
  1168. nlmsg_free(skb);
  1169. return -EINVAL;
  1170. }
  1171. /* send genetlink multicast message to notify appplications */
  1172. result = genlmsg_end(skb, msg_header);
  1173. if (result < 0) {
  1174. pmcraid_err("genlmsg_end failed\n");
  1175. nlmsg_free(skb);
  1176. return result;
  1177. }
  1178. result =
  1179. genlmsg_multicast(skb, 0, pmcraid_event_family.id, GFP_ATOMIC);
  1180. /* If there are no listeners, genlmsg_multicast may return non-zero
  1181. * value.
  1182. */
  1183. if (result)
  1184. pmcraid_info("failed to send %s event message %x!\n",
  1185. type == PMCRAID_HCAM_CODE_LOG_DATA ? "LDN" : "CCN",
  1186. result);
  1187. return result;
  1188. }
  1189. /**
  1190. * pmcraid_handle_config_change - Handle a config change from the adapter
  1191. * @pinstance: pointer to per adapter instance structure
  1192. *
  1193. * Return value:
  1194. * none
  1195. */
  1196. static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
  1197. {
  1198. struct pmcraid_config_table_entry *cfg_entry;
  1199. struct pmcraid_hcam_ccn *ccn_hcam;
  1200. struct pmcraid_cmd *cmd;
  1201. struct pmcraid_cmd *cfgcmd;
  1202. struct pmcraid_resource_entry *res = NULL;
  1203. u32 new_entry = 1;
  1204. unsigned long lock_flags;
  1205. unsigned long host_lock_flags;
  1206. int rc;
  1207. ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
  1208. cfg_entry = &ccn_hcam->cfg_entry;
  1209. pmcraid_info
  1210. ("CCN(%x): %x type: %x lost: %x flags: %x res: %x:%x:%x:%x\n",
  1211. pinstance->ccn.hcam->ilid,
  1212. pinstance->ccn.hcam->op_code,
  1213. pinstance->ccn.hcam->notification_type,
  1214. pinstance->ccn.hcam->notification_lost,
  1215. pinstance->ccn.hcam->flags,
  1216. pinstance->host->unique_id,
  1217. RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
  1218. (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
  1219. RES_BUS(cfg_entry->resource_address)),
  1220. RES_IS_VSET(*cfg_entry) ? cfg_entry->unique_flags1 :
  1221. RES_TARGET(cfg_entry->resource_address),
  1222. RES_LUN(cfg_entry->resource_address));
  1223. /* If this HCAM indicates a lost notification, read the config table */
  1224. if (pinstance->ccn.hcam->notification_lost) {
  1225. cfgcmd = pmcraid_get_free_cmd(pinstance);
  1226. if (cfgcmd) {
  1227. pmcraid_info("lost CCN, reading config table\b");
  1228. pinstance->reinit_cfg_table = 1;
  1229. pmcraid_querycfg(cfgcmd);
  1230. } else {
  1231. pmcraid_err("lost CCN, no free cmd for querycfg\n");
  1232. }
  1233. goto out_notify_apps;
  1234. }
  1235. /* If this resource is not going to be added to mid-layer, just notify
  1236. * applications and return
  1237. */
  1238. if (!pmcraid_expose_resource(cfg_entry))
  1239. goto out_notify_apps;
  1240. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  1241. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  1242. rc = memcmp(&res->cfg_entry.resource_address,
  1243. &cfg_entry->resource_address,
  1244. sizeof(cfg_entry->resource_address));
  1245. if (!rc) {
  1246. new_entry = 0;
  1247. break;
  1248. }
  1249. }
  1250. if (new_entry) {
  1251. /* If there are more number of resources than what driver can
  1252. * manage, do not notify the applications about the CCN. Just
  1253. * ignore this notifications and re-register the same HCAM
  1254. */
  1255. if (list_empty(&pinstance->free_res_q)) {
  1256. spin_unlock_irqrestore(&pinstance->resource_lock,
  1257. lock_flags);
  1258. pmcraid_err("too many resources attached\n");
  1259. spin_lock_irqsave(pinstance->host->host_lock,
  1260. host_lock_flags);
  1261. pmcraid_send_hcam(pinstance,
  1262. PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1263. spin_unlock_irqrestore(pinstance->host->host_lock,
  1264. host_lock_flags);
  1265. return;
  1266. }
  1267. res = list_entry(pinstance->free_res_q.next,
  1268. struct pmcraid_resource_entry, queue);
  1269. list_del(&res->queue);
  1270. res->scsi_dev = NULL;
  1271. res->reset_progress = 0;
  1272. list_add_tail(&res->queue, &pinstance->used_res_q);
  1273. }
  1274. memcpy(&res->cfg_entry, cfg_entry,
  1275. sizeof(struct pmcraid_config_table_entry));
  1276. if (pinstance->ccn.hcam->notification_type ==
  1277. NOTIFICATION_TYPE_ENTRY_DELETED) {
  1278. if (res->scsi_dev) {
  1279. res->change_detected = RES_CHANGE_DEL;
  1280. res->cfg_entry.resource_handle =
  1281. PMCRAID_INVALID_RES_HANDLE;
  1282. schedule_work(&pinstance->worker_q);
  1283. } else {
  1284. /* This may be one of the non-exposed resources */
  1285. list_move_tail(&res->queue, &pinstance->free_res_q);
  1286. }
  1287. } else if (!res->scsi_dev) {
  1288. res->change_detected = RES_CHANGE_ADD;
  1289. schedule_work(&pinstance->worker_q);
  1290. }
  1291. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  1292. out_notify_apps:
  1293. /* Notify configuration changes to registered applications.*/
  1294. if (!pmcraid_disable_aen)
  1295. pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1296. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1297. if (cmd)
  1298. pmcraid_send_hcam_cmd(cmd);
  1299. }
  1300. /**
  1301. * pmcraid_get_error_info - return error string for an ioasc
  1302. * @ioasc: ioasc code
  1303. * Return Value
  1304. * none
  1305. */
  1306. static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
  1307. {
  1308. int i;
  1309. for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
  1310. if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
  1311. return &pmcraid_ioasc_error_table[i];
  1312. }
  1313. return NULL;
  1314. }
  1315. /**
  1316. * pmcraid_ioasc_logger - log IOASC information based user-settings
  1317. * @ioasc: ioasc code
  1318. * @cmd: pointer to command that resulted in 'ioasc'
  1319. */
  1320. void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
  1321. {
  1322. struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
  1323. if (error_info == NULL ||
  1324. cmd->drv_inst->current_log_level < error_info->log_level)
  1325. return;
  1326. /* log the error string */
  1327. pmcraid_err("cmd [%d] for resource %x failed with %x(%s)\n",
  1328. cmd->ioa_cb->ioarcb.cdb[0],
  1329. cmd->ioa_cb->ioarcb.resource_handle,
  1330. le32_to_cpu(ioasc), error_info->error_string);
  1331. }
  1332. /**
  1333. * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
  1334. *
  1335. * @pinstance: pointer to per adapter instance structure
  1336. *
  1337. * Return value:
  1338. * none
  1339. */
  1340. static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
  1341. {
  1342. struct pmcraid_hcam_ldn *hcam_ldn;
  1343. u32 ioasc;
  1344. hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1345. pmcraid_info
  1346. ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
  1347. pinstance->ldn.hcam->ilid,
  1348. pinstance->ldn.hcam->op_code,
  1349. pinstance->ldn.hcam->notification_type,
  1350. pinstance->ldn.hcam->notification_lost,
  1351. pinstance->ldn.hcam->flags,
  1352. pinstance->ldn.hcam->overlay_id);
  1353. /* log only the errors, no need to log informational log entries */
  1354. if (pinstance->ldn.hcam->notification_type !=
  1355. NOTIFICATION_TYPE_ERROR_LOG)
  1356. return;
  1357. if (pinstance->ldn.hcam->notification_lost ==
  1358. HOSTRCB_NOTIFICATIONS_LOST)
  1359. dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
  1360. ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
  1361. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  1362. ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
  1363. dev_info(&pinstance->pdev->dev,
  1364. "UnitAttention due to IOA Bus Reset\n");
  1365. scsi_report_bus_reset(
  1366. pinstance->host,
  1367. RES_BUS(hcam_ldn->error_log.fd_ra));
  1368. }
  1369. return;
  1370. }
  1371. /**
  1372. * pmcraid_process_ccn - Op done function for a CCN.
  1373. * @cmd: pointer to command struct
  1374. *
  1375. * This function is the op done function for a configuration
  1376. * change notification
  1377. *
  1378. * Return value:
  1379. * none
  1380. */
  1381. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
  1382. {
  1383. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1384. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1385. unsigned long lock_flags;
  1386. pinstance->ccn.cmd = NULL;
  1387. pmcraid_return_cmd(cmd);
  1388. /* If driver initiated IOA reset happened while this hcam was pending
  1389. * with IOA, or IOA bringdown sequence is in progress, no need to
  1390. * re-register the hcam
  1391. */
  1392. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1393. atomic_read(&pinstance->ccn.ignore) == 1) {
  1394. return;
  1395. } else if (ioasc) {
  1396. dev_info(&pinstance->pdev->dev,
  1397. "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
  1398. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1399. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1400. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1401. } else {
  1402. pmcraid_handle_config_change(pinstance);
  1403. }
  1404. }
  1405. /**
  1406. * pmcraid_process_ldn - op done function for an LDN
  1407. * @cmd: pointer to command block
  1408. *
  1409. * Return value
  1410. * none
  1411. */
  1412. static void pmcraid_initiate_reset(struct pmcraid_instance *);
  1413. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
  1414. {
  1415. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1416. struct pmcraid_hcam_ldn *ldn_hcam =
  1417. (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1418. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1419. u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
  1420. unsigned long lock_flags;
  1421. /* return the command block back to freepool */
  1422. pinstance->ldn.cmd = NULL;
  1423. pmcraid_return_cmd(cmd);
  1424. /* If driver initiated IOA reset happened while this hcam was pending
  1425. * with IOA, no need to re-register the hcam as reset engine will do it
  1426. * once reset sequence is complete
  1427. */
  1428. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1429. atomic_read(&pinstance->ccn.ignore) == 1) {
  1430. return;
  1431. } else if (!ioasc) {
  1432. pmcraid_handle_error_log(pinstance);
  1433. if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
  1434. spin_lock_irqsave(pinstance->host->host_lock,
  1435. lock_flags);
  1436. pmcraid_initiate_reset(pinstance);
  1437. spin_unlock_irqrestore(pinstance->host->host_lock,
  1438. lock_flags);
  1439. return;
  1440. }
  1441. } else {
  1442. dev_info(&pinstance->pdev->dev,
  1443. "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
  1444. }
  1445. /* send netlink message for HCAM notification if enabled */
  1446. if (!pmcraid_disable_aen)
  1447. pmcraid_notify_aen(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1448. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1449. if (cmd)
  1450. pmcraid_send_hcam_cmd(cmd);
  1451. }
  1452. /**
  1453. * pmcraid_register_hcams - register HCAMs for CCN and LDN
  1454. *
  1455. * @pinstance: pointer per adapter instance structure
  1456. *
  1457. * Return Value
  1458. * none
  1459. */
  1460. static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
  1461. {
  1462. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1463. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1464. }
  1465. /**
  1466. * pmcraid_unregister_hcams - cancel HCAMs registered already
  1467. * @cmd: pointer to command used as part of reset sequence
  1468. */
  1469. static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
  1470. {
  1471. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1472. /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
  1473. * handling hcam response though it is not necessary. In order to
  1474. * prevent this, set 'ignore', so that bring-down sequence doesn't
  1475. * re-send any more hcams
  1476. */
  1477. atomic_set(&pinstance->ccn.ignore, 1);
  1478. atomic_set(&pinstance->ldn.ignore, 1);
  1479. /* If adapter reset was forced as part of runtime reset sequence,
  1480. * start the reset sequence.
  1481. */
  1482. if (pinstance->force_ioa_reset && !pinstance->ioa_bringdown) {
  1483. pinstance->force_ioa_reset = 0;
  1484. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1485. pmcraid_reset_alert(cmd);
  1486. return;
  1487. }
  1488. /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
  1489. * one after the other. So CCN cancellation will be triggered by
  1490. * pmcraid_cancel_ldn itself.
  1491. */
  1492. pmcraid_cancel_ldn(cmd);
  1493. }
  1494. /**
  1495. * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
  1496. * @pinstance: pointer to adapter instance structure
  1497. * Return Value
  1498. * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
  1499. */
  1500. static void pmcraid_reinit_buffers(struct pmcraid_instance *);
  1501. static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
  1502. {
  1503. u32 intrs;
  1504. pmcraid_reinit_buffers(pinstance);
  1505. intrs = pmcraid_read_interrupts(pinstance);
  1506. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  1507. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  1508. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1509. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  1510. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1511. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  1512. return 1;
  1513. } else {
  1514. return 0;
  1515. }
  1516. }
  1517. /**
  1518. * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
  1519. * @cmd : pointer to reset command block
  1520. *
  1521. * Return Value
  1522. * none
  1523. */
  1524. static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
  1525. {
  1526. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1527. u32 int_reg;
  1528. u32 doorbell;
  1529. /* There will be an interrupt when Transition to Operational bit is
  1530. * set so tasklet would execute next reset task. The timeout handler
  1531. * would re-initiate a reset
  1532. */
  1533. cmd->cmd_done = pmcraid_ioa_reset;
  1534. cmd->timer.data = (unsigned long)cmd;
  1535. cmd->timer.expires = jiffies +
  1536. msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
  1537. cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
  1538. if (!timer_pending(&cmd->timer))
  1539. add_timer(&cmd->timer);
  1540. /* Enable destructive diagnostics on IOA if it is not yet in
  1541. * operational state
  1542. */
  1543. doorbell = DOORBELL_RUNTIME_RESET |
  1544. DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
  1545. iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
  1546. int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  1547. pmcraid_info("Waiting for IOA to become operational %x:%x\n",
  1548. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1549. int_reg);
  1550. }
  1551. /**
  1552. * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
  1553. *
  1554. * @pinstance: pointer to adapter instance structure
  1555. *
  1556. * Return Value
  1557. * none
  1558. */
  1559. static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
  1560. {
  1561. pmcraid_info("%s is not yet implemented\n", __func__);
  1562. }
  1563. /**
  1564. * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
  1565. * @pinstance: pointer to adapter instance structure
  1566. *
  1567. * This function fails all outstanding ops. If they are submitted to IOA
  1568. * already, it sends cancel all messages if IOA is still accepting IOARCBs,
  1569. * otherwise just completes the commands and returns the cmd blocks to free
  1570. * pool.
  1571. *
  1572. * Return value:
  1573. * none
  1574. */
  1575. static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
  1576. {
  1577. struct pmcraid_cmd *cmd, *temp;
  1578. unsigned long lock_flags;
  1579. /* pending command list is protected by pending_pool_lock. Its
  1580. * traversal must be done as within this lock
  1581. */
  1582. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1583. list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
  1584. free_list) {
  1585. list_del(&cmd->free_list);
  1586. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  1587. lock_flags);
  1588. cmd->ioa_cb->ioasa.ioasc =
  1589. cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
  1590. cmd->ioa_cb->ioasa.ilid =
  1591. cpu_to_be32(PMCRAID_DRIVER_ILID);
  1592. /* In case the command timer is still running */
  1593. del_timer(&cmd->timer);
  1594. /* If this is an IO command, complete it by invoking scsi_done
  1595. * function. If this is one of the internal commands other
  1596. * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
  1597. * complete it
  1598. */
  1599. if (cmd->scsi_cmd) {
  1600. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  1601. __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
  1602. scsi_cmd->result |= DID_ERROR << 16;
  1603. scsi_dma_unmap(scsi_cmd);
  1604. pmcraid_return_cmd(cmd);
  1605. pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
  1606. le32_to_cpu(resp) >> 2,
  1607. cmd->ioa_cb->ioarcb.cdb[0],
  1608. scsi_cmd->result);
  1609. scsi_cmd->scsi_done(scsi_cmd);
  1610. } else if (cmd->cmd_done == pmcraid_internal_done ||
  1611. cmd->cmd_done == pmcraid_erp_done) {
  1612. cmd->cmd_done(cmd);
  1613. } else if (cmd->cmd_done != pmcraid_ioa_reset) {
  1614. pmcraid_return_cmd(cmd);
  1615. }
  1616. atomic_dec(&pinstance->outstanding_cmds);
  1617. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1618. }
  1619. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  1620. }
  1621. /**
  1622. * pmcraid_ioa_reset - Implementation of IOA reset logic
  1623. *
  1624. * @cmd: pointer to the cmd block to be used for entire reset process
  1625. *
  1626. * This function executes most of the steps required for IOA reset. This gets
  1627. * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
  1628. * 'eh_' thread. Access to variables used for controling the reset sequence is
  1629. * synchronized using host lock. Various functions called during reset process
  1630. * would make use of a single command block, pointer to which is also stored in
  1631. * adapter instance structure.
  1632. *
  1633. * Return Value
  1634. * None
  1635. */
  1636. static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
  1637. {
  1638. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1639. u8 reset_complete = 0;
  1640. pinstance->ioa_reset_in_progress = 1;
  1641. if (pinstance->reset_cmd != cmd) {
  1642. pmcraid_err("reset is called with different command block\n");
  1643. pinstance->reset_cmd = cmd;
  1644. }
  1645. pmcraid_info("reset_engine: state = %d, command = %p\n",
  1646. pinstance->ioa_state, cmd);
  1647. switch (pinstance->ioa_state) {
  1648. case IOA_STATE_DEAD:
  1649. /* If IOA is offline, whatever may be the reset reason, just
  1650. * return. callers might be waiting on the reset wait_q, wake
  1651. * up them
  1652. */
  1653. pmcraid_err("IOA is offline no reset is possible\n");
  1654. reset_complete = 1;
  1655. break;
  1656. case IOA_STATE_IN_BRINGDOWN:
  1657. /* we enter here, once ioa shutdown command is processed by IOA
  1658. * Alert IOA for a possible reset. If reset alert fails, IOA
  1659. * goes through hard-reset
  1660. */
  1661. pmcraid_disable_interrupts(pinstance, ~0);
  1662. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1663. pmcraid_reset_alert(cmd);
  1664. break;
  1665. case IOA_STATE_UNKNOWN:
  1666. /* We may be called during probe or resume. Some pre-processing
  1667. * is required for prior to reset
  1668. */
  1669. scsi_block_requests(pinstance->host);
  1670. /* If asked to reset while IOA was processing responses or
  1671. * there are any error responses then IOA may require
  1672. * hard-reset.
  1673. */
  1674. if (pinstance->ioa_hard_reset == 0) {
  1675. if (ioread32(pinstance->ioa_status) &
  1676. INTRS_TRANSITION_TO_OPERATIONAL) {
  1677. pmcraid_info("sticky bit set, bring-up\n");
  1678. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1679. pmcraid_reinit_cmdblk(cmd);
  1680. pmcraid_identify_hrrq(cmd);
  1681. } else {
  1682. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1683. pmcraid_soft_reset(cmd);
  1684. }
  1685. } else {
  1686. /* Alert IOA of a possible reset and wait for critical
  1687. * operation in progress bit to reset
  1688. */
  1689. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1690. pmcraid_reset_alert(cmd);
  1691. }
  1692. break;
  1693. case IOA_STATE_IN_RESET_ALERT:
  1694. /* If critical operation in progress bit is reset or wait gets
  1695. * timed out, reset proceeds with starting BIST on the IOA.
  1696. * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
  1697. * they are 3 or more, reset engine marks IOA dead and returns
  1698. */
  1699. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  1700. pmcraid_start_bist(cmd);
  1701. break;
  1702. case IOA_STATE_IN_HARD_RESET:
  1703. pinstance->ioa_reset_attempts++;
  1704. /* retry reset if we haven't reached maximum allowed limit */
  1705. if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
  1706. pinstance->ioa_reset_attempts = 0;
  1707. pmcraid_err("IOA didn't respond marking it as dead\n");
  1708. pinstance->ioa_state = IOA_STATE_DEAD;
  1709. reset_complete = 1;
  1710. break;
  1711. }
  1712. /* Once either bist or pci reset is done, restore PCI config
  1713. * space. If this fails, proceed with hard reset again
  1714. */
  1715. if (pci_restore_state(pinstance->pdev)) {
  1716. pmcraid_info("config-space error resetting again\n");
  1717. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1718. pmcraid_reset_alert(cmd);
  1719. break;
  1720. }
  1721. /* fail all pending commands */
  1722. pmcraid_fail_outstanding_cmds(pinstance);
  1723. /* check if unit check is active, if so extract dump */
  1724. if (pinstance->ioa_unit_check) {
  1725. pmcraid_info("unit check is active\n");
  1726. pinstance->ioa_unit_check = 0;
  1727. pmcraid_get_dump(pinstance);
  1728. pinstance->ioa_reset_attempts--;
  1729. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1730. pmcraid_reset_alert(cmd);
  1731. break;
  1732. }
  1733. /* if the reset reason is to bring-down the ioa, we might be
  1734. * done with the reset restore pci_config_space and complete
  1735. * the reset
  1736. */
  1737. if (pinstance->ioa_bringdown) {
  1738. pmcraid_info("bringing down the adapter\n");
  1739. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1740. pinstance->ioa_bringdown = 0;
  1741. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  1742. reset_complete = 1;
  1743. } else {
  1744. /* bring-up IOA, so proceed with soft reset
  1745. * Reinitialize hrrq_buffers and their indices also
  1746. * enable interrupts after a pci_restore_state
  1747. */
  1748. if (pmcraid_reset_enable_ioa(pinstance)) {
  1749. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1750. pmcraid_info("bringing up the adapter\n");
  1751. pmcraid_reinit_cmdblk(cmd);
  1752. pmcraid_identify_hrrq(cmd);
  1753. } else {
  1754. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1755. pmcraid_soft_reset(cmd);
  1756. }
  1757. }
  1758. break;
  1759. case IOA_STATE_IN_SOFT_RESET:
  1760. /* TRANSITION TO OPERATIONAL is on so start initialization
  1761. * sequence
  1762. */
  1763. pmcraid_info("In softreset proceeding with bring-up\n");
  1764. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1765. /* Initialization commands start with HRRQ identification. From
  1766. * now on tasklet completes most of the commands as IOA is up
  1767. * and intrs are enabled
  1768. */
  1769. pmcraid_identify_hrrq(cmd);
  1770. break;
  1771. case IOA_STATE_IN_BRINGUP:
  1772. /* we are done with bringing up of IOA, change the ioa_state to
  1773. * operational and wake up any waiters
  1774. */
  1775. pinstance->ioa_state = IOA_STATE_OPERATIONAL;
  1776. reset_complete = 1;
  1777. break;
  1778. case IOA_STATE_OPERATIONAL:
  1779. default:
  1780. /* When IOA is operational and a reset is requested, check for
  1781. * the reset reason. If reset is to bring down IOA, unregister
  1782. * HCAMs and initiate shutdown; if adapter reset is forced then
  1783. * restart reset sequence again
  1784. */
  1785. if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
  1786. pinstance->force_ioa_reset == 0) {
  1787. reset_complete = 1;
  1788. } else {
  1789. if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
  1790. pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
  1791. pmcraid_reinit_cmdblk(cmd);
  1792. pmcraid_unregister_hcams(cmd);
  1793. }
  1794. break;
  1795. }
  1796. /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
  1797. * OPERATIONAL. Reset all control variables used during reset, wake up
  1798. * any waiting threads and let the SCSI mid-layer send commands. Note
  1799. * that host_lock must be held before invoking scsi_report_bus_reset.
  1800. */
  1801. if (reset_complete) {
  1802. pinstance->ioa_reset_in_progress = 0;
  1803. pinstance->ioa_reset_attempts = 0;
  1804. pinstance->reset_cmd = NULL;
  1805. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1806. pinstance->ioa_bringdown = 0;
  1807. pmcraid_return_cmd(cmd);
  1808. /* If target state is to bring up the adapter, proceed with
  1809. * hcam registration and resource exposure to mid-layer.
  1810. */
  1811. if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
  1812. pmcraid_register_hcams(pinstance);
  1813. wake_up_all(&pinstance->reset_wait_q);
  1814. }
  1815. return;
  1816. }
  1817. /**
  1818. * pmcraid_initiate_reset - initiates reset sequence. This is called from
  1819. * ISR/tasklet during error interrupts including IOA unit check. If reset
  1820. * is already in progress, it just returns, otherwise initiates IOA reset
  1821. * to bring IOA up to operational state.
  1822. *
  1823. * @pinstance: pointer to adapter instance structure
  1824. *
  1825. * Return value
  1826. * none
  1827. */
  1828. static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
  1829. {
  1830. struct pmcraid_cmd *cmd;
  1831. /* If the reset is already in progress, just return, otherwise start
  1832. * reset sequence and return
  1833. */
  1834. if (!pinstance->ioa_reset_in_progress) {
  1835. scsi_block_requests(pinstance->host);
  1836. cmd = pmcraid_get_free_cmd(pinstance);
  1837. if (cmd == NULL) {
  1838. pmcraid_err("no cmnd blocks for initiate_reset\n");
  1839. return;
  1840. }
  1841. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1842. pinstance->reset_cmd = cmd;
  1843. pinstance->force_ioa_reset = 1;
  1844. pmcraid_ioa_reset(cmd);
  1845. }
  1846. }
  1847. /**
  1848. * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
  1849. * or bringdown IOA
  1850. * @pinstance: pointer adapter instance structure
  1851. * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
  1852. * @target_state: expected target state after reset
  1853. *
  1854. * Note: This command initiates reset and waits for its completion. Hence this
  1855. * should not be called from isr/timer/tasklet functions (timeout handlers,
  1856. * error response handlers and interrupt handlers).
  1857. *
  1858. * Return Value
  1859. * 1 in case ioa_state is not target_state, 0 otherwise.
  1860. */
  1861. static int pmcraid_reset_reload(
  1862. struct pmcraid_instance *pinstance,
  1863. u8 shutdown_type,
  1864. u8 target_state
  1865. )
  1866. {
  1867. struct pmcraid_cmd *reset_cmd = NULL;
  1868. unsigned long lock_flags;
  1869. int reset = 1;
  1870. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1871. if (pinstance->ioa_reset_in_progress) {
  1872. pmcraid_info("reset_reload: reset is already in progress\n");
  1873. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1874. wait_event(pinstance->reset_wait_q,
  1875. !pinstance->ioa_reset_in_progress);
  1876. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1877. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  1878. spin_unlock_irqrestore(pinstance->host->host_lock,
  1879. lock_flags);
  1880. pmcraid_info("reset_reload: IOA is dead\n");
  1881. return reset;
  1882. } else if (pinstance->ioa_state == target_state) {
  1883. reset = 0;
  1884. }
  1885. }
  1886. if (reset) {
  1887. pmcraid_info("reset_reload: proceeding with reset\n");
  1888. scsi_block_requests(pinstance->host);
  1889. reset_cmd = pmcraid_get_free_cmd(pinstance);
  1890. if (reset_cmd == NULL) {
  1891. pmcraid_err("no free cmnd for reset_reload\n");
  1892. spin_unlock_irqrestore(pinstance->host->host_lock,
  1893. lock_flags);
  1894. return reset;
  1895. }
  1896. if (shutdown_type == SHUTDOWN_NORMAL)
  1897. pinstance->ioa_bringdown = 1;
  1898. pinstance->ioa_shutdown_type = shutdown_type;
  1899. pinstance->reset_cmd = reset_cmd;
  1900. pinstance->force_ioa_reset = reset;
  1901. pmcraid_info("reset_reload: initiating reset\n");
  1902. pmcraid_ioa_reset(reset_cmd);
  1903. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1904. pmcraid_info("reset_reload: waiting for reset to complete\n");
  1905. wait_event(pinstance->reset_wait_q,
  1906. !pinstance->ioa_reset_in_progress);
  1907. pmcraid_info("reset_reload: reset is complete !! \n");
  1908. scsi_unblock_requests(pinstance->host);
  1909. if (pinstance->ioa_state == target_state)
  1910. reset = 0;
  1911. }
  1912. return reset;
  1913. }
  1914. /**
  1915. * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
  1916. *
  1917. * @pinstance: pointer to adapter instance structure
  1918. *
  1919. * Return Value
  1920. * whatever is returned from pmcraid_reset_reload
  1921. */
  1922. static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
  1923. {
  1924. return pmcraid_reset_reload(pinstance,
  1925. SHUTDOWN_NORMAL,
  1926. IOA_STATE_UNKNOWN);
  1927. }
  1928. /**
  1929. * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
  1930. *
  1931. * @pinstance: pointer to adapter instance structure
  1932. *
  1933. * Return Value
  1934. * whatever is returned from pmcraid_reset_reload
  1935. */
  1936. static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
  1937. {
  1938. return pmcraid_reset_reload(pinstance,
  1939. SHUTDOWN_NONE,
  1940. IOA_STATE_OPERATIONAL);
  1941. }
  1942. /**
  1943. * pmcraid_request_sense - Send request sense to a device
  1944. * @cmd: pmcraid command struct
  1945. *
  1946. * This function sends a request sense to a device as a result of a check
  1947. * condition. This method re-uses the same command block that failed earlier.
  1948. */
  1949. static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
  1950. {
  1951. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  1952. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  1953. /* allocate DMAable memory for sense buffers */
  1954. cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
  1955. SCSI_SENSE_BUFFERSIZE,
  1956. &cmd->sense_buffer_dma);
  1957. if (cmd->sense_buffer == NULL) {
  1958. pmcraid_err
  1959. ("couldn't allocate sense buffer for request sense\n");
  1960. pmcraid_erp_done(cmd);
  1961. return;
  1962. }
  1963. /* re-use the command block */
  1964. memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
  1965. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  1966. ioarcb->request_flags0 = (SYNC_COMPLETE |
  1967. NO_LINK_DESCS |
  1968. INHIBIT_UL_CHECK);
  1969. ioarcb->request_type = REQ_TYPE_SCSI;
  1970. ioarcb->cdb[0] = REQUEST_SENSE;
  1971. ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1972. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  1973. offsetof(struct pmcraid_ioarcb,
  1974. add_data.u.ioadl[0]));
  1975. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  1976. ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  1977. ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
  1978. ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  1979. ioadl->flags = cpu_to_le32(IOADL_FLAGS_LAST_DESC);
  1980. /* request sense might be called as part of error response processing
  1981. * which runs in tasklets context. It is possible that mid-layer might
  1982. * schedule queuecommand during this time, hence, writting to IOARRIN
  1983. * must be protect by host_lock
  1984. */
  1985. pmcraid_send_cmd(cmd, pmcraid_erp_done,
  1986. PMCRAID_REQUEST_SENSE_TIMEOUT,
  1987. pmcraid_timeout_handler);
  1988. }
  1989. /**
  1990. * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
  1991. * @cmd: command that failed
  1992. * @sense: true if request_sense is required after cancel all
  1993. *
  1994. * This function sends a cancel all to a device to clear the queue.
  1995. */
  1996. static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
  1997. {
  1998. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  1999. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2000. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2001. void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
  2002. : pmcraid_request_sense;
  2003. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2004. ioarcb->request_flags0 = SYNC_OVERRIDE;
  2005. ioarcb->request_type = REQ_TYPE_IOACMD;
  2006. ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
  2007. if (RES_IS_GSCSI(res->cfg_entry))
  2008. ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
  2009. ioarcb->ioadl_bus_addr = 0;
  2010. ioarcb->ioadl_length = 0;
  2011. ioarcb->data_transfer_length = 0;
  2012. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  2013. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  2014. * schedule queuecommand while we are doing this
  2015. */
  2016. pmcraid_send_cmd(cmd, cmd_done,
  2017. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2018. pmcraid_timeout_handler);
  2019. }
  2020. /**
  2021. * pmcraid_frame_auto_sense: frame fixed format sense information
  2022. *
  2023. * @cmd: pointer to failing command block
  2024. *
  2025. * Return value
  2026. * none
  2027. */
  2028. static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
  2029. {
  2030. u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
  2031. struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
  2032. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2033. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2034. u32 failing_lba = 0;
  2035. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  2036. cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
  2037. if (RES_IS_VSET(res->cfg_entry) &&
  2038. ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
  2039. ioasa->u.vset.failing_lba_hi != 0) {
  2040. sense_buf[0] = 0x72;
  2041. sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2042. sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2043. sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2044. sense_buf[7] = 12;
  2045. sense_buf[8] = 0;
  2046. sense_buf[9] = 0x0A;
  2047. sense_buf[10] = 0x80;
  2048. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
  2049. sense_buf[12] = (failing_lba & 0xff000000) >> 24;
  2050. sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
  2051. sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
  2052. sense_buf[15] = failing_lba & 0x000000ff;
  2053. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
  2054. sense_buf[16] = (failing_lba & 0xff000000) >> 24;
  2055. sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
  2056. sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
  2057. sense_buf[19] = failing_lba & 0x000000ff;
  2058. } else {
  2059. sense_buf[0] = 0x70;
  2060. sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2061. sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2062. sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2063. if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
  2064. if (RES_IS_VSET(res->cfg_entry))
  2065. failing_lba =
  2066. le32_to_cpu(ioasa->u.
  2067. vset.failing_lba_lo);
  2068. sense_buf[0] |= 0x80;
  2069. sense_buf[3] = (failing_lba >> 24) & 0xff;
  2070. sense_buf[4] = (failing_lba >> 16) & 0xff;
  2071. sense_buf[5] = (failing_lba >> 8) & 0xff;
  2072. sense_buf[6] = failing_lba & 0xff;
  2073. }
  2074. sense_buf[7] = 6; /* additional length */
  2075. }
  2076. }
  2077. /**
  2078. * pmcraid_error_handler - Error response handlers for a SCSI op
  2079. * @cmd: pointer to pmcraid_cmd that has failed
  2080. *
  2081. * This function determines whether or not to initiate ERP on the affected
  2082. * device. This is called from a tasklet, which doesn't hold any locks.
  2083. *
  2084. * Return value:
  2085. * 0 it caller can complete the request, otherwise 1 where in error
  2086. * handler itself completes the request and returns the command block
  2087. * back to free-pool
  2088. */
  2089. static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
  2090. {
  2091. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2092. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2093. struct pmcraid_instance *pinstance = cmd->drv_inst;
  2094. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2095. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2096. u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
  2097. u32 sense_copied = 0;
  2098. if (!res) {
  2099. pmcraid_info("resource pointer is NULL\n");
  2100. return 0;
  2101. }
  2102. /* If this was a SCSI read/write command keep count of errors */
  2103. if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
  2104. atomic_inc(&res->read_failures);
  2105. else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
  2106. atomic_inc(&res->write_failures);
  2107. if (!RES_IS_GSCSI(res->cfg_entry) &&
  2108. masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
  2109. pmcraid_frame_auto_sense(cmd);
  2110. }
  2111. /* Log IOASC/IOASA information based on user settings */
  2112. pmcraid_ioasc_logger(ioasc, cmd);
  2113. switch (masked_ioasc) {
  2114. case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
  2115. scsi_cmd->result |= (DID_ABORT << 16);
  2116. break;
  2117. case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
  2118. case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
  2119. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  2120. break;
  2121. case PMCRAID_IOASC_NR_SYNC_REQUIRED:
  2122. res->sync_reqd = 1;
  2123. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  2124. break;
  2125. case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
  2126. scsi_cmd->result |= (DID_PASSTHROUGH << 16);
  2127. break;
  2128. case PMCRAID_IOASC_UA_BUS_WAS_RESET:
  2129. case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
  2130. if (!res->reset_progress)
  2131. scsi_report_bus_reset(pinstance->host,
  2132. scsi_cmd->device->channel);
  2133. scsi_cmd->result |= (DID_ERROR << 16);
  2134. break;
  2135. case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
  2136. scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
  2137. res->sync_reqd = 1;
  2138. /* if check_condition is not active return with error otherwise
  2139. * get/frame the sense buffer
  2140. */
  2141. if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
  2142. SAM_STAT_CHECK_CONDITION &&
  2143. PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
  2144. return 0;
  2145. /* If we have auto sense data as part of IOASA pass it to
  2146. * mid-layer
  2147. */
  2148. if (ioasa->auto_sense_length != 0) {
  2149. short sense_len = ioasa->auto_sense_length;
  2150. int data_size = min_t(u16, le16_to_cpu(sense_len),
  2151. SCSI_SENSE_BUFFERSIZE);
  2152. memcpy(scsi_cmd->sense_buffer,
  2153. ioasa->sense_data,
  2154. data_size);
  2155. sense_copied = 1;
  2156. }
  2157. if (RES_IS_GSCSI(res->cfg_entry)) {
  2158. pmcraid_cancel_all(cmd, sense_copied);
  2159. } else if (sense_copied) {
  2160. pmcraid_erp_done(cmd);
  2161. return 0;
  2162. } else {
  2163. pmcraid_request_sense(cmd);
  2164. }
  2165. return 1;
  2166. case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
  2167. break;
  2168. default:
  2169. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  2170. scsi_cmd->result |= (DID_ERROR << 16);
  2171. break;
  2172. }
  2173. return 0;
  2174. }
  2175. /**
  2176. * pmcraid_reset_device - device reset handler functions
  2177. *
  2178. * @scsi_cmd: scsi command struct
  2179. * @modifier: reset modifier indicating the reset sequence to be performed
  2180. *
  2181. * This function issues a device reset to the affected device.
  2182. * A LUN reset will be sent to the device first. If that does
  2183. * not work, a target reset will be sent.
  2184. *
  2185. * Return value:
  2186. * SUCCESS / FAILED
  2187. */
  2188. static int pmcraid_reset_device(
  2189. struct scsi_cmnd *scsi_cmd,
  2190. unsigned long timeout,
  2191. u8 modifier
  2192. )
  2193. {
  2194. struct pmcraid_cmd *cmd;
  2195. struct pmcraid_instance *pinstance;
  2196. struct pmcraid_resource_entry *res;
  2197. struct pmcraid_ioarcb *ioarcb;
  2198. unsigned long lock_flags;
  2199. u32 ioasc;
  2200. pinstance =
  2201. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2202. res = scsi_cmd->device->hostdata;
  2203. if (!res) {
  2204. sdev_printk(KERN_ERR, scsi_cmd->device,
  2205. "reset_device: NULL resource pointer\n");
  2206. return FAILED;
  2207. }
  2208. /* If adapter is currently going through reset/reload, return failed.
  2209. * This will force the mid-layer to call _eh_bus/host reset, which
  2210. * will then go to sleep and wait for the reset to complete
  2211. */
  2212. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2213. if (pinstance->ioa_reset_in_progress ||
  2214. pinstance->ioa_state == IOA_STATE_DEAD) {
  2215. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2216. return FAILED;
  2217. }
  2218. res->reset_progress = 1;
  2219. pmcraid_info("Resetting %s resource with addr %x\n",
  2220. ((modifier & RESET_DEVICE_LUN) ? "LUN" :
  2221. ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
  2222. le32_to_cpu(res->cfg_entry.resource_address));
  2223. /* get a free cmd block */
  2224. cmd = pmcraid_get_free_cmd(pinstance);
  2225. if (cmd == NULL) {
  2226. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2227. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2228. return FAILED;
  2229. }
  2230. ioarcb = &cmd->ioa_cb->ioarcb;
  2231. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2232. ioarcb->request_type = REQ_TYPE_IOACMD;
  2233. ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
  2234. /* Initialize reset modifier bits */
  2235. if (modifier)
  2236. modifier = ENABLE_RESET_MODIFIER | modifier;
  2237. ioarcb->cdb[1] = modifier;
  2238. init_completion(&cmd->wait_for_completion);
  2239. cmd->completion_req = 1;
  2240. pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
  2241. cmd->ioa_cb->ioarcb.cdb[0],
  2242. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
  2243. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
  2244. pmcraid_send_cmd(cmd,
  2245. pmcraid_internal_done,
  2246. timeout,
  2247. pmcraid_timeout_handler);
  2248. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2249. /* RESET_DEVICE command completes after all pending IOARCBs are
  2250. * completed. Once this command is completed, pmcraind_internal_done
  2251. * will wake up the 'completion' queue.
  2252. */
  2253. wait_for_completion(&cmd->wait_for_completion);
  2254. /* complete the command here itself and return the command block
  2255. * to free list
  2256. */
  2257. pmcraid_return_cmd(cmd);
  2258. res->reset_progress = 0;
  2259. ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2260. /* set the return value based on the returned ioasc */
  2261. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2262. }
  2263. /**
  2264. * _pmcraid_io_done - helper for pmcraid_io_done function
  2265. *
  2266. * @cmd: pointer to pmcraid command struct
  2267. * @reslen: residual data length to be set in the ioasa
  2268. * @ioasc: ioasc either returned by IOA or set by driver itself.
  2269. *
  2270. * This function is invoked by pmcraid_io_done to complete mid-layer
  2271. * scsi ops.
  2272. *
  2273. * Return value:
  2274. * 0 if caller is required to return it to free_pool. Returns 1 if
  2275. * caller need not worry about freeing command block as error handler
  2276. * will take care of that.
  2277. */
  2278. static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
  2279. {
  2280. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2281. int rc = 0;
  2282. scsi_set_resid(scsi_cmd, reslen);
  2283. pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
  2284. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2285. cmd->ioa_cb->ioarcb.cdb[0],
  2286. ioasc, scsi_cmd->result);
  2287. if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
  2288. rc = pmcraid_error_handler(cmd);
  2289. if (rc == 0) {
  2290. scsi_dma_unmap(scsi_cmd);
  2291. scsi_cmd->scsi_done(scsi_cmd);
  2292. }
  2293. return rc;
  2294. }
  2295. /**
  2296. * pmcraid_io_done - SCSI completion function
  2297. *
  2298. * @cmd: pointer to pmcraid command struct
  2299. *
  2300. * This function is invoked by tasklet/mid-layer error handler to completing
  2301. * the SCSI ops sent from mid-layer.
  2302. *
  2303. * Return value
  2304. * none
  2305. */
  2306. static void pmcraid_io_done(struct pmcraid_cmd *cmd)
  2307. {
  2308. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2309. u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
  2310. if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
  2311. pmcraid_return_cmd(cmd);
  2312. }
  2313. /**
  2314. * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
  2315. *
  2316. * @cmd: command block of the command to be aborted
  2317. *
  2318. * Return Value:
  2319. * returns pointer to command structure used as cancelling cmd
  2320. */
  2321. static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
  2322. {
  2323. struct pmcraid_cmd *cancel_cmd;
  2324. struct pmcraid_instance *pinstance;
  2325. struct pmcraid_resource_entry *res;
  2326. pinstance = (struct pmcraid_instance *)cmd->drv_inst;
  2327. res = cmd->scsi_cmd->device->hostdata;
  2328. cancel_cmd = pmcraid_get_free_cmd(pinstance);
  2329. if (cancel_cmd == NULL) {
  2330. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2331. return NULL;
  2332. }
  2333. pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
  2334. pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
  2335. cmd->ioa_cb->ioarcb.cdb[0],
  2336. cmd->ioa_cb->ioarcb.response_handle >> 2);
  2337. init_completion(&cancel_cmd->wait_for_completion);
  2338. cancel_cmd->completion_req = 1;
  2339. pmcraid_info("command (%d) CDB[0] = %x for %x\n",
  2340. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2341. cmd->ioa_cb->ioarcb.cdb[0],
  2342. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
  2343. pmcraid_send_cmd(cancel_cmd,
  2344. pmcraid_internal_done,
  2345. PMCRAID_INTERNAL_TIMEOUT,
  2346. pmcraid_timeout_handler);
  2347. return cancel_cmd;
  2348. }
  2349. /**
  2350. * pmcraid_abort_complete - Waits for ABORT TASK completion
  2351. *
  2352. * @cancel_cmd: command block use as cancelling command
  2353. *
  2354. * Return Value:
  2355. * returns SUCCESS if ABORT TASK has good completion
  2356. * otherwise FAILED
  2357. */
  2358. static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
  2359. {
  2360. struct pmcraid_resource_entry *res;
  2361. u32 ioasc;
  2362. wait_for_completion(&cancel_cmd->wait_for_completion);
  2363. res = cancel_cmd->u.res;
  2364. cancel_cmd->u.res = NULL;
  2365. ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
  2366. /* If the abort task is not timed out we will get a Good completion
  2367. * as sense_key, otherwise we may get one the following responses
  2368. * due to subsquent bus reset or device reset. In case IOASC is
  2369. * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
  2370. */
  2371. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  2372. ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
  2373. if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
  2374. res->sync_reqd = 1;
  2375. ioasc = 0;
  2376. }
  2377. /* complete the command here itself */
  2378. pmcraid_return_cmd(cancel_cmd);
  2379. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2380. }
  2381. /**
  2382. * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
  2383. *
  2384. * @scsi_cmd: scsi command struct given by mid-layer. When this is called
  2385. * mid-layer ensures that no other commands are queued. This
  2386. * never gets called under interrupt, but a separate eh thread.
  2387. *
  2388. * Return value:
  2389. * SUCCESS / FAILED
  2390. */
  2391. static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
  2392. {
  2393. struct pmcraid_instance *pinstance;
  2394. struct pmcraid_cmd *cmd;
  2395. struct pmcraid_resource_entry *res;
  2396. unsigned long host_lock_flags;
  2397. unsigned long pending_lock_flags;
  2398. struct pmcraid_cmd *cancel_cmd = NULL;
  2399. int cmd_found = 0;
  2400. int rc = FAILED;
  2401. pinstance =
  2402. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2403. scmd_printk(KERN_INFO, scsi_cmd,
  2404. "I/O command timed out, aborting it.\n");
  2405. res = scsi_cmd->device->hostdata;
  2406. if (res == NULL)
  2407. return rc;
  2408. /* If we are currently going through reset/reload, return failed.
  2409. * This will force the mid-layer to eventually call
  2410. * pmcraid_eh_host_reset which will then go to sleep and wait for the
  2411. * reset to complete
  2412. */
  2413. spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
  2414. if (pinstance->ioa_reset_in_progress ||
  2415. pinstance->ioa_state == IOA_STATE_DEAD) {
  2416. spin_unlock_irqrestore(pinstance->host->host_lock,
  2417. host_lock_flags);
  2418. return rc;
  2419. }
  2420. /* loop over pending cmd list to find cmd corresponding to this
  2421. * scsi_cmd. Note that this command might not have been completed
  2422. * already. locking: all pending commands are protected with
  2423. * pending_pool_lock.
  2424. */
  2425. spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
  2426. list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
  2427. if (cmd->scsi_cmd == scsi_cmd) {
  2428. cmd_found = 1;
  2429. break;
  2430. }
  2431. }
  2432. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  2433. pending_lock_flags);
  2434. /* If the command to be aborted was given to IOA and still pending with
  2435. * it, send ABORT_TASK to abort this and wait for its completion
  2436. */
  2437. if (cmd_found)
  2438. cancel_cmd = pmcraid_abort_cmd(cmd);
  2439. spin_unlock_irqrestore(pinstance->host->host_lock,
  2440. host_lock_flags);
  2441. if (cancel_cmd) {
  2442. cancel_cmd->u.res = cmd->scsi_cmd->device->hostdata;
  2443. rc = pmcraid_abort_complete(cancel_cmd);
  2444. }
  2445. return cmd_found ? rc : SUCCESS;
  2446. }
  2447. /**
  2448. * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
  2449. *
  2450. * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
  2451. *
  2452. * All these routines invokve pmcraid_reset_device with appropriate parameters.
  2453. * Since these are called from mid-layer EH thread, no other IO will be queued
  2454. * to the resource being reset. However, control path (IOCTL) may be active so
  2455. * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
  2456. * takes care by locking/unlocking host_lock.
  2457. *
  2458. * Return value
  2459. * SUCCESS or FAILED
  2460. */
  2461. static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
  2462. {
  2463. scmd_printk(KERN_INFO, scmd,
  2464. "resetting device due to an I/O command timeout.\n");
  2465. return pmcraid_reset_device(scmd,
  2466. PMCRAID_INTERNAL_TIMEOUT,
  2467. RESET_DEVICE_LUN);
  2468. }
  2469. static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
  2470. {
  2471. scmd_printk(KERN_INFO, scmd,
  2472. "Doing bus reset due to an I/O command timeout.\n");
  2473. return pmcraid_reset_device(scmd,
  2474. PMCRAID_RESET_BUS_TIMEOUT,
  2475. RESET_DEVICE_BUS);
  2476. }
  2477. static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
  2478. {
  2479. scmd_printk(KERN_INFO, scmd,
  2480. "Doing target reset due to an I/O command timeout.\n");
  2481. return pmcraid_reset_device(scmd,
  2482. PMCRAID_INTERNAL_TIMEOUT,
  2483. RESET_DEVICE_TARGET);
  2484. }
  2485. /**
  2486. * pmcraid_eh_host_reset_handler - adapter reset handler callback
  2487. *
  2488. * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
  2489. *
  2490. * Initiates adapter reset to bring it up to operational state
  2491. *
  2492. * Return value
  2493. * SUCCESS or FAILED
  2494. */
  2495. static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
  2496. {
  2497. unsigned long interval = 10000; /* 10 seconds interval */
  2498. int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
  2499. struct pmcraid_instance *pinstance =
  2500. (struct pmcraid_instance *)(scmd->device->host->hostdata);
  2501. /* wait for an additional 150 seconds just in case firmware could come
  2502. * up and if it could complete all the pending commands excluding the
  2503. * two HCAM (CCN and LDN).
  2504. */
  2505. while (waits--) {
  2506. if (atomic_read(&pinstance->outstanding_cmds) <=
  2507. PMCRAID_MAX_HCAM_CMD)
  2508. return SUCCESS;
  2509. msleep(interval);
  2510. }
  2511. dev_err(&pinstance->pdev->dev,
  2512. "Adapter being reset due to an I/O command timeout.\n");
  2513. return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
  2514. }
  2515. /**
  2516. * pmcraid_task_attributes - Translate SPI Q-Tags to task attributes
  2517. * @scsi_cmd: scsi command struct
  2518. *
  2519. * Return value
  2520. * number of tags or 0 if the task is not tagged
  2521. */
  2522. static u8 pmcraid_task_attributes(struct scsi_cmnd *scsi_cmd)
  2523. {
  2524. char tag[2];
  2525. u8 rc = 0;
  2526. if (scsi_populate_tag_msg(scsi_cmd, tag)) {
  2527. switch (tag[0]) {
  2528. case MSG_SIMPLE_TAG:
  2529. rc = TASK_TAG_SIMPLE;
  2530. break;
  2531. case MSG_HEAD_TAG:
  2532. rc = TASK_TAG_QUEUE_HEAD;
  2533. break;
  2534. case MSG_ORDERED_TAG:
  2535. rc = TASK_TAG_ORDERED;
  2536. break;
  2537. };
  2538. }
  2539. return rc;
  2540. }
  2541. /**
  2542. * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
  2543. * @cmd: pmcraid command struct
  2544. * @sgcount: count of scatter-gather elements
  2545. *
  2546. * Return value
  2547. * returns pointer pmcraid_ioadl_desc, initialized to point to internal
  2548. * or external IOADLs
  2549. */
  2550. struct pmcraid_ioadl_desc *
  2551. pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
  2552. {
  2553. struct pmcraid_ioadl_desc *ioadl;
  2554. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2555. int ioadl_count = 0;
  2556. if (ioarcb->add_cmd_param_length)
  2557. ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
  2558. ioarcb->ioadl_length =
  2559. sizeof(struct pmcraid_ioadl_desc) * sgcount;
  2560. if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
  2561. /* external ioadls start at offset 0x80 from control_block
  2562. * structure, re-using 24 out of 27 ioadls part of IOARCB.
  2563. * It is necessary to indicate to firmware that driver is
  2564. * using ioadls to be treated as external to IOARCB.
  2565. */
  2566. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  2567. ioarcb->ioadl_bus_addr =
  2568. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2569. offsetof(struct pmcraid_ioarcb,
  2570. add_data.u.ioadl[3]));
  2571. ioadl = &ioarcb->add_data.u.ioadl[3];
  2572. } else {
  2573. ioarcb->ioadl_bus_addr =
  2574. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2575. offsetof(struct pmcraid_ioarcb,
  2576. add_data.u.ioadl[ioadl_count]));
  2577. ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
  2578. ioarcb->ioarcb_bus_addr |=
  2579. DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
  2580. }
  2581. return ioadl;
  2582. }
  2583. /**
  2584. * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
  2585. * @pinstance: pointer to adapter instance structure
  2586. * @cmd: pmcraid command struct
  2587. *
  2588. * This function is invoked by queuecommand entry point while sending a command
  2589. * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
  2590. *
  2591. * Return value:
  2592. * 0 on success or -1 on failure
  2593. */
  2594. static int pmcraid_build_ioadl(
  2595. struct pmcraid_instance *pinstance,
  2596. struct pmcraid_cmd *cmd
  2597. )
  2598. {
  2599. int i, nseg;
  2600. struct scatterlist *sglist;
  2601. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2602. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  2603. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2604. u32 length = scsi_bufflen(scsi_cmd);
  2605. if (!length)
  2606. return 0;
  2607. nseg = scsi_dma_map(scsi_cmd);
  2608. if (nseg < 0) {
  2609. scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
  2610. return -1;
  2611. } else if (nseg > PMCRAID_MAX_IOADLS) {
  2612. scsi_dma_unmap(scsi_cmd);
  2613. scmd_printk(KERN_ERR, scsi_cmd,
  2614. "sg count is (%d) more than allowed!\n", nseg);
  2615. return -1;
  2616. }
  2617. /* Initialize IOARCB data transfer length fields */
  2618. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
  2619. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  2620. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2621. ioarcb->data_transfer_length = cpu_to_le32(length);
  2622. ioadl = pmcraid_init_ioadls(cmd, nseg);
  2623. /* Initialize IOADL descriptor addresses */
  2624. scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
  2625. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
  2626. ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
  2627. ioadl[i].flags = 0;
  2628. }
  2629. /* setup last descriptor */
  2630. ioadl[i - 1].flags = cpu_to_le32(IOADL_FLAGS_LAST_DESC);
  2631. return 0;
  2632. }
  2633. /**
  2634. * pmcraid_free_sglist - Frees an allocated SG buffer list
  2635. * @sglist: scatter/gather list pointer
  2636. *
  2637. * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
  2638. *
  2639. * Return value:
  2640. * none
  2641. */
  2642. static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
  2643. {
  2644. int i;
  2645. for (i = 0; i < sglist->num_sg; i++)
  2646. __free_pages(sg_page(&(sglist->scatterlist[i])),
  2647. sglist->order);
  2648. kfree(sglist);
  2649. }
  2650. /**
  2651. * pmcraid_alloc_sglist - Allocates memory for a SG list
  2652. * @buflen: buffer length
  2653. *
  2654. * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
  2655. * list.
  2656. *
  2657. * Return value
  2658. * pointer to sglist / NULL on failure
  2659. */
  2660. static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
  2661. {
  2662. struct pmcraid_sglist *sglist;
  2663. struct scatterlist *scatterlist;
  2664. struct page *page;
  2665. int num_elem, i, j;
  2666. int sg_size;
  2667. int order;
  2668. int bsize_elem;
  2669. sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
  2670. order = (sg_size > 0) ? get_order(sg_size) : 0;
  2671. bsize_elem = PAGE_SIZE * (1 << order);
  2672. /* Determine the actual number of sg entries needed */
  2673. if (buflen % bsize_elem)
  2674. num_elem = (buflen / bsize_elem) + 1;
  2675. else
  2676. num_elem = buflen / bsize_elem;
  2677. /* Allocate a scatter/gather list for the DMA */
  2678. sglist = kzalloc(sizeof(struct pmcraid_sglist) +
  2679. (sizeof(struct scatterlist) * (num_elem - 1)),
  2680. GFP_KERNEL);
  2681. if (sglist == NULL)
  2682. return NULL;
  2683. scatterlist = sglist->scatterlist;
  2684. sg_init_table(scatterlist, num_elem);
  2685. sglist->order = order;
  2686. sglist->num_sg = num_elem;
  2687. sg_size = buflen;
  2688. for (i = 0; i < num_elem; i++) {
  2689. page = alloc_pages(GFP_KERNEL|GFP_DMA, order);
  2690. if (!page) {
  2691. for (j = i - 1; j >= 0; j--)
  2692. __free_pages(sg_page(&scatterlist[j]), order);
  2693. kfree(sglist);
  2694. return NULL;
  2695. }
  2696. sg_set_page(&scatterlist[i], page,
  2697. sg_size < bsize_elem ? sg_size : bsize_elem, 0);
  2698. sg_size -= bsize_elem;
  2699. }
  2700. return sglist;
  2701. }
  2702. /**
  2703. * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
  2704. * @sglist: scatter/gather list pointer
  2705. * @buffer: buffer pointer
  2706. * @len: buffer length
  2707. * @direction: data transfer direction
  2708. *
  2709. * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
  2710. *
  2711. * Return value:
  2712. * 0 on success / other on failure
  2713. */
  2714. static int pmcraid_copy_sglist(
  2715. struct pmcraid_sglist *sglist,
  2716. unsigned long buffer,
  2717. u32 len,
  2718. int direction
  2719. )
  2720. {
  2721. struct scatterlist *scatterlist;
  2722. void *kaddr;
  2723. int bsize_elem;
  2724. int i;
  2725. int rc = 0;
  2726. /* Determine the actual number of bytes per element */
  2727. bsize_elem = PAGE_SIZE * (1 << sglist->order);
  2728. scatterlist = sglist->scatterlist;
  2729. for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
  2730. struct page *page = sg_page(&scatterlist[i]);
  2731. kaddr = kmap(page);
  2732. if (direction == DMA_TO_DEVICE)
  2733. rc = __copy_from_user(kaddr,
  2734. (void *)buffer,
  2735. bsize_elem);
  2736. else
  2737. rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
  2738. kunmap(page);
  2739. if (rc) {
  2740. pmcraid_err("failed to copy user data into sg list\n");
  2741. return -EFAULT;
  2742. }
  2743. scatterlist[i].length = bsize_elem;
  2744. }
  2745. if (len % bsize_elem) {
  2746. struct page *page = sg_page(&scatterlist[i]);
  2747. kaddr = kmap(page);
  2748. if (direction == DMA_TO_DEVICE)
  2749. rc = __copy_from_user(kaddr,
  2750. (void *)buffer,
  2751. len % bsize_elem);
  2752. else
  2753. rc = __copy_to_user((void *)buffer,
  2754. kaddr,
  2755. len % bsize_elem);
  2756. kunmap(page);
  2757. scatterlist[i].length = len % bsize_elem;
  2758. }
  2759. if (rc) {
  2760. pmcraid_err("failed to copy user data into sg list\n");
  2761. rc = -EFAULT;
  2762. }
  2763. return rc;
  2764. }
  2765. /**
  2766. * pmcraid_queuecommand - Queue a mid-layer request
  2767. * @scsi_cmd: scsi command struct
  2768. * @done: done function
  2769. *
  2770. * This function queues a request generated by the mid-layer. Midlayer calls
  2771. * this routine within host->lock. Some of the functions called by queuecommand
  2772. * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
  2773. *
  2774. * Return value:
  2775. * 0 on success
  2776. * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
  2777. * SCSI_MLQUEUE_HOST_BUSY if host is busy
  2778. */
  2779. static int pmcraid_queuecommand(
  2780. struct scsi_cmnd *scsi_cmd,
  2781. void (*done) (struct scsi_cmnd *)
  2782. )
  2783. {
  2784. struct pmcraid_instance *pinstance;
  2785. struct pmcraid_resource_entry *res;
  2786. struct pmcraid_ioarcb *ioarcb;
  2787. struct pmcraid_cmd *cmd;
  2788. int rc = 0;
  2789. pinstance =
  2790. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2791. scsi_cmd->scsi_done = done;
  2792. res = scsi_cmd->device->hostdata;
  2793. scsi_cmd->result = (DID_OK << 16);
  2794. /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
  2795. * the command
  2796. */
  2797. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2798. pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
  2799. scsi_cmd->result = (DID_NO_CONNECT << 16);
  2800. scsi_cmd->scsi_done(scsi_cmd);
  2801. return 0;
  2802. }
  2803. /* If IOA reset is in progress, can't queue the commands */
  2804. if (pinstance->ioa_reset_in_progress)
  2805. return SCSI_MLQUEUE_HOST_BUSY;
  2806. /* initialize the command and IOARCB to be sent to IOA */
  2807. cmd = pmcraid_get_free_cmd(pinstance);
  2808. if (cmd == NULL) {
  2809. pmcraid_err("free command block is not available\n");
  2810. return SCSI_MLQUEUE_HOST_BUSY;
  2811. }
  2812. cmd->scsi_cmd = scsi_cmd;
  2813. ioarcb = &(cmd->ioa_cb->ioarcb);
  2814. memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
  2815. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2816. ioarcb->request_type = REQ_TYPE_SCSI;
  2817. cmd->cmd_done = pmcraid_io_done;
  2818. if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
  2819. if (scsi_cmd->underflow == 0)
  2820. ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
  2821. if (res->sync_reqd) {
  2822. ioarcb->request_flags0 |= SYNC_COMPLETE;
  2823. res->sync_reqd = 0;
  2824. }
  2825. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2826. ioarcb->request_flags1 |= pmcraid_task_attributes(scsi_cmd);
  2827. if (RES_IS_GSCSI(res->cfg_entry))
  2828. ioarcb->request_flags1 |= DELAY_AFTER_RESET;
  2829. }
  2830. rc = pmcraid_build_ioadl(pinstance, cmd);
  2831. pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
  2832. le32_to_cpu(ioarcb->response_handle) >> 2,
  2833. scsi_cmd->cmnd[0], pinstance->host->unique_id,
  2834. RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
  2835. PMCRAID_PHYS_BUS_ID,
  2836. RES_IS_VSET(res->cfg_entry) ?
  2837. res->cfg_entry.unique_flags1 :
  2838. RES_TARGET(res->cfg_entry.resource_address),
  2839. RES_LUN(res->cfg_entry.resource_address));
  2840. if (likely(rc == 0)) {
  2841. _pmcraid_fire_command(cmd);
  2842. } else {
  2843. pmcraid_err("queuecommand could not build ioadl\n");
  2844. pmcraid_return_cmd(cmd);
  2845. rc = SCSI_MLQUEUE_HOST_BUSY;
  2846. }
  2847. return rc;
  2848. }
  2849. /**
  2850. * pmcraid_open -char node "open" entry, allowed only users with admin access
  2851. */
  2852. static int pmcraid_chr_open(struct inode *inode, struct file *filep)
  2853. {
  2854. struct pmcraid_instance *pinstance;
  2855. if (!capable(CAP_SYS_ADMIN))
  2856. return -EACCES;
  2857. /* Populate adapter instance * pointer for use by ioctl */
  2858. pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
  2859. filep->private_data = pinstance;
  2860. return 0;
  2861. }
  2862. /**
  2863. * pmcraid_release - char node "release" entry point
  2864. */
  2865. static int pmcraid_chr_release(struct inode *inode, struct file *filep)
  2866. {
  2867. struct pmcraid_instance *pinstance =
  2868. ((struct pmcraid_instance *)filep->private_data);
  2869. filep->private_data = NULL;
  2870. fasync_helper(-1, filep, 0, &pinstance->aen_queue);
  2871. return 0;
  2872. }
  2873. /**
  2874. * pmcraid_fasync - Async notifier registration from applications
  2875. *
  2876. * This function adds the calling process to a driver global queue. When an
  2877. * event occurs, SIGIO will be sent to all processes in this queue.
  2878. */
  2879. static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
  2880. {
  2881. struct pmcraid_instance *pinstance;
  2882. int rc;
  2883. pinstance = (struct pmcraid_instance *)filep->private_data;
  2884. mutex_lock(&pinstance->aen_queue_lock);
  2885. rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
  2886. mutex_unlock(&pinstance->aen_queue_lock);
  2887. return rc;
  2888. }
  2889. /**
  2890. * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
  2891. * commands sent over IOCTL interface
  2892. *
  2893. * @cmd : pointer to struct pmcraid_cmd
  2894. * @buflen : length of the request buffer
  2895. * @direction : data transfer direction
  2896. *
  2897. * Return value
  2898. * 0 on sucess, non-zero error code on failure
  2899. */
  2900. static int pmcraid_build_passthrough_ioadls(
  2901. struct pmcraid_cmd *cmd,
  2902. int buflen,
  2903. int direction
  2904. )
  2905. {
  2906. struct pmcraid_sglist *sglist = NULL;
  2907. struct scatterlist *sg = NULL;
  2908. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2909. struct pmcraid_ioadl_desc *ioadl;
  2910. int i;
  2911. sglist = pmcraid_alloc_sglist(buflen);
  2912. if (!sglist) {
  2913. pmcraid_err("can't allocate memory for passthrough SGls\n");
  2914. return -ENOMEM;
  2915. }
  2916. sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
  2917. sglist->scatterlist,
  2918. sglist->num_sg, direction);
  2919. if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
  2920. dev_err(&cmd->drv_inst->pdev->dev,
  2921. "Failed to map passthrough buffer!\n");
  2922. pmcraid_free_sglist(sglist);
  2923. return -EIO;
  2924. }
  2925. cmd->sglist = sglist;
  2926. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2927. ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
  2928. /* Initialize IOADL descriptor addresses */
  2929. for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
  2930. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
  2931. ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
  2932. ioadl[i].flags = 0;
  2933. }
  2934. /* setup the last descriptor */
  2935. ioadl[i - 1].flags = cpu_to_le32(IOADL_FLAGS_LAST_DESC);
  2936. return 0;
  2937. }
  2938. /**
  2939. * pmcraid_release_passthrough_ioadls - release passthrough ioadls
  2940. *
  2941. * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
  2942. * @buflen: size of the request buffer
  2943. * @direction: data transfer direction
  2944. *
  2945. * Return value
  2946. * 0 on sucess, non-zero error code on failure
  2947. */
  2948. static void pmcraid_release_passthrough_ioadls(
  2949. struct pmcraid_cmd *cmd,
  2950. int buflen,
  2951. int direction
  2952. )
  2953. {
  2954. struct pmcraid_sglist *sglist = cmd->sglist;
  2955. if (buflen > 0) {
  2956. pci_unmap_sg(cmd->drv_inst->pdev,
  2957. sglist->scatterlist,
  2958. sglist->num_sg,
  2959. direction);
  2960. pmcraid_free_sglist(sglist);
  2961. cmd->sglist = NULL;
  2962. }
  2963. }
  2964. /**
  2965. * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
  2966. *
  2967. * @pinstance: pointer to adapter instance structure
  2968. * @cmd: ioctl code
  2969. * @arg: pointer to pmcraid_passthrough_buffer user buffer
  2970. *
  2971. * Return value
  2972. * 0 on sucess, non-zero error code on failure
  2973. */
  2974. static long pmcraid_ioctl_passthrough(
  2975. struct pmcraid_instance *pinstance,
  2976. unsigned int ioctl_cmd,
  2977. unsigned int buflen,
  2978. unsigned long arg
  2979. )
  2980. {
  2981. struct pmcraid_passthrough_ioctl_buffer *buffer;
  2982. struct pmcraid_ioarcb *ioarcb;
  2983. struct pmcraid_cmd *cmd;
  2984. struct pmcraid_cmd *cancel_cmd;
  2985. unsigned long request_buffer;
  2986. unsigned long request_offset;
  2987. unsigned long lock_flags;
  2988. int request_size;
  2989. int buffer_size;
  2990. u8 access, direction;
  2991. int rc = 0;
  2992. /* If IOA reset is in progress, wait 10 secs for reset to complete */
  2993. if (pinstance->ioa_reset_in_progress) {
  2994. rc = wait_event_interruptible_timeout(
  2995. pinstance->reset_wait_q,
  2996. !pinstance->ioa_reset_in_progress,
  2997. msecs_to_jiffies(10000));
  2998. if (!rc)
  2999. return -ETIMEDOUT;
  3000. else if (rc < 0)
  3001. return -ERESTARTSYS;
  3002. }
  3003. /* If adapter is not in operational state, return error */
  3004. if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
  3005. pmcraid_err("IOA is not operational\n");
  3006. return -ENOTTY;
  3007. }
  3008. buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
  3009. buffer = kmalloc(buffer_size, GFP_KERNEL);
  3010. if (!buffer) {
  3011. pmcraid_err("no memory for passthrough buffer\n");
  3012. return -ENOMEM;
  3013. }
  3014. request_offset =
  3015. offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
  3016. request_buffer = arg + request_offset;
  3017. rc = __copy_from_user(buffer,
  3018. (struct pmcraid_passthrough_ioctl_buffer *) arg,
  3019. sizeof(struct pmcraid_passthrough_ioctl_buffer));
  3020. if (rc) {
  3021. pmcraid_err("ioctl: can't copy passthrough buffer\n");
  3022. rc = -EFAULT;
  3023. goto out_free_buffer;
  3024. }
  3025. request_size = buffer->ioarcb.data_transfer_length;
  3026. if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
  3027. access = VERIFY_READ;
  3028. direction = DMA_TO_DEVICE;
  3029. } else {
  3030. access = VERIFY_WRITE;
  3031. direction = DMA_FROM_DEVICE;
  3032. }
  3033. if (request_size > 0) {
  3034. rc = access_ok(access, arg, request_offset + request_size);
  3035. if (!rc) {
  3036. rc = -EFAULT;
  3037. goto out_free_buffer;
  3038. }
  3039. }
  3040. /* check if we have any additional command parameters */
  3041. if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
  3042. rc = -EINVAL;
  3043. goto out_free_buffer;
  3044. }
  3045. cmd = pmcraid_get_free_cmd(pinstance);
  3046. if (!cmd) {
  3047. pmcraid_err("free command block is not available\n");
  3048. rc = -ENOMEM;
  3049. goto out_free_buffer;
  3050. }
  3051. cmd->scsi_cmd = NULL;
  3052. ioarcb = &(cmd->ioa_cb->ioarcb);
  3053. /* Copy the user-provided IOARCB stuff field by field */
  3054. ioarcb->resource_handle = buffer->ioarcb.resource_handle;
  3055. ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
  3056. ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
  3057. ioarcb->request_type = buffer->ioarcb.request_type;
  3058. ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
  3059. ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
  3060. memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
  3061. if (buffer->ioarcb.add_cmd_param_length) {
  3062. ioarcb->add_cmd_param_length =
  3063. buffer->ioarcb.add_cmd_param_length;
  3064. ioarcb->add_cmd_param_offset =
  3065. buffer->ioarcb.add_cmd_param_offset;
  3066. memcpy(ioarcb->add_data.u.add_cmd_params,
  3067. buffer->ioarcb.add_data.u.add_cmd_params,
  3068. buffer->ioarcb.add_cmd_param_length);
  3069. }
  3070. if (request_size) {
  3071. rc = pmcraid_build_passthrough_ioadls(cmd,
  3072. request_size,
  3073. direction);
  3074. if (rc) {
  3075. pmcraid_err("couldn't build passthrough ioadls\n");
  3076. goto out_free_buffer;
  3077. }
  3078. }
  3079. /* If data is being written into the device, copy the data from user
  3080. * buffers
  3081. */
  3082. if (direction == DMA_TO_DEVICE && request_size > 0) {
  3083. rc = pmcraid_copy_sglist(cmd->sglist,
  3084. request_buffer,
  3085. request_size,
  3086. direction);
  3087. if (rc) {
  3088. pmcraid_err("failed to copy user buffer\n");
  3089. goto out_free_sglist;
  3090. }
  3091. }
  3092. /* passthrough ioctl is a blocking command so, put the user to sleep
  3093. * until timeout. Note that a timeout value of 0 means, do timeout.
  3094. */
  3095. cmd->cmd_done = pmcraid_internal_done;
  3096. init_completion(&cmd->wait_for_completion);
  3097. cmd->completion_req = 1;
  3098. pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
  3099. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  3100. cmd->ioa_cb->ioarcb.cdb[0],
  3101. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
  3102. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3103. _pmcraid_fire_command(cmd);
  3104. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3105. /* If command timeout is specified put caller to wait till that time,
  3106. * otherwise it would be blocking wait. If command gets timed out, it
  3107. * will be aborted.
  3108. */
  3109. if (buffer->ioarcb.cmd_timeout == 0) {
  3110. wait_for_completion(&cmd->wait_for_completion);
  3111. } else if (!wait_for_completion_timeout(
  3112. &cmd->wait_for_completion,
  3113. msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
  3114. pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
  3115. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
  3116. cmd->ioa_cb->ioarcb.cdb[0]);
  3117. rc = -ETIMEDOUT;
  3118. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3119. cancel_cmd = pmcraid_abort_cmd(cmd);
  3120. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3121. if (cancel_cmd) {
  3122. wait_for_completion(&cancel_cmd->wait_for_completion);
  3123. pmcraid_return_cmd(cancel_cmd);
  3124. }
  3125. goto out_free_sglist;
  3126. }
  3127. /* If the command failed for any reason, copy entire IOASA buffer and
  3128. * return IOCTL success. If copying IOASA to user-buffer fails, return
  3129. * EFAULT
  3130. */
  3131. if (le32_to_cpu(cmd->ioa_cb->ioasa.ioasc)) {
  3132. void *ioasa =
  3133. (void *)(arg +
  3134. offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
  3135. pmcraid_info("command failed with %x\n",
  3136. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  3137. if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
  3138. sizeof(struct pmcraid_ioasa))) {
  3139. pmcraid_err("failed to copy ioasa buffer to user\n");
  3140. rc = -EFAULT;
  3141. }
  3142. }
  3143. /* If the data transfer was from device, copy the data onto user
  3144. * buffers
  3145. */
  3146. else if (direction == DMA_FROM_DEVICE && request_size > 0) {
  3147. rc = pmcraid_copy_sglist(cmd->sglist,
  3148. request_buffer,
  3149. request_size,
  3150. direction);
  3151. if (rc) {
  3152. pmcraid_err("failed to copy user buffer\n");
  3153. rc = -EFAULT;
  3154. }
  3155. }
  3156. out_free_sglist:
  3157. pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
  3158. pmcraid_return_cmd(cmd);
  3159. out_free_buffer:
  3160. kfree(buffer);
  3161. return rc;
  3162. }
  3163. /**
  3164. * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
  3165. *
  3166. * @pinstance: pointer to adapter instance structure
  3167. * @cmd: ioctl command passed in
  3168. * @buflen: length of user_buffer
  3169. * @user_buffer: user buffer pointer
  3170. *
  3171. * Return Value
  3172. * 0 in case of success, otherwise appropriate error code
  3173. */
  3174. static long pmcraid_ioctl_driver(
  3175. struct pmcraid_instance *pinstance,
  3176. unsigned int cmd,
  3177. unsigned int buflen,
  3178. void __user *user_buffer
  3179. )
  3180. {
  3181. int rc = -ENOSYS;
  3182. if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
  3183. pmcraid_err("ioctl_driver: access fault in request buffer \n");
  3184. return -EFAULT;
  3185. }
  3186. switch (cmd) {
  3187. case PMCRAID_IOCTL_RESET_ADAPTER:
  3188. pmcraid_reset_bringup(pinstance);
  3189. rc = 0;
  3190. break;
  3191. default:
  3192. break;
  3193. }
  3194. return rc;
  3195. }
  3196. /**
  3197. * pmcraid_check_ioctl_buffer - check for proper access to user buffer
  3198. *
  3199. * @cmd: ioctl command
  3200. * @arg: user buffer
  3201. * @hdr: pointer to kernel memory for pmcraid_ioctl_header
  3202. *
  3203. * Return Value
  3204. * negetive error code if there are access issues, otherwise zero.
  3205. * Upon success, returns ioctl header copied out of user buffer.
  3206. */
  3207. static int pmcraid_check_ioctl_buffer(
  3208. int cmd,
  3209. void __user *arg,
  3210. struct pmcraid_ioctl_header *hdr
  3211. )
  3212. {
  3213. int rc = 0;
  3214. int access = VERIFY_READ;
  3215. if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
  3216. pmcraid_err("couldn't copy ioctl header from user buffer\n");
  3217. return -EFAULT;
  3218. }
  3219. /* check for valid driver signature */
  3220. rc = memcmp(hdr->signature,
  3221. PMCRAID_IOCTL_SIGNATURE,
  3222. sizeof(hdr->signature));
  3223. if (rc) {
  3224. pmcraid_err("signature verification failed\n");
  3225. return -EINVAL;
  3226. }
  3227. /* buffer length can't be negetive */
  3228. if (hdr->buffer_length < 0) {
  3229. pmcraid_err("ioctl: invalid buffer length specified\n");
  3230. return -EINVAL;
  3231. }
  3232. /* check for appropriate buffer access */
  3233. if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
  3234. access = VERIFY_WRITE;
  3235. rc = access_ok(access,
  3236. (arg + sizeof(struct pmcraid_ioctl_header)),
  3237. hdr->buffer_length);
  3238. if (!rc) {
  3239. pmcraid_err("access failed for user buffer of size %d\n",
  3240. hdr->buffer_length);
  3241. return -EFAULT;
  3242. }
  3243. return 0;
  3244. }
  3245. /**
  3246. * pmcraid_ioctl - char node ioctl entry point
  3247. */
  3248. static long pmcraid_chr_ioctl(
  3249. struct file *filep,
  3250. unsigned int cmd,
  3251. unsigned long arg
  3252. )
  3253. {
  3254. struct pmcraid_instance *pinstance = NULL;
  3255. struct pmcraid_ioctl_header *hdr = NULL;
  3256. int retval = -ENOTTY;
  3257. hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
  3258. if (!hdr) {
  3259. pmcraid_err("faile to allocate memory for ioctl header\n");
  3260. return -ENOMEM;
  3261. }
  3262. retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
  3263. if (retval) {
  3264. pmcraid_info("chr_ioctl: header check failed\n");
  3265. kfree(hdr);
  3266. return retval;
  3267. }
  3268. pinstance = (struct pmcraid_instance *)filep->private_data;
  3269. if (!pinstance) {
  3270. pmcraid_info("adapter instance is not found\n");
  3271. kfree(hdr);
  3272. return -ENOTTY;
  3273. }
  3274. switch (_IOC_TYPE(cmd)) {
  3275. case PMCRAID_PASSTHROUGH_IOCTL:
  3276. /* If ioctl code is to download microcode, we need to block
  3277. * mid-layer requests.
  3278. */
  3279. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3280. scsi_block_requests(pinstance->host);
  3281. retval = pmcraid_ioctl_passthrough(pinstance,
  3282. cmd,
  3283. hdr->buffer_length,
  3284. arg);
  3285. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3286. scsi_unblock_requests(pinstance->host);
  3287. break;
  3288. case PMCRAID_DRIVER_IOCTL:
  3289. arg += sizeof(struct pmcraid_ioctl_header);
  3290. retval = pmcraid_ioctl_driver(pinstance,
  3291. cmd,
  3292. hdr->buffer_length,
  3293. (void __user *)arg);
  3294. break;
  3295. default:
  3296. retval = -ENOTTY;
  3297. break;
  3298. }
  3299. kfree(hdr);
  3300. return retval;
  3301. }
  3302. /**
  3303. * File operations structure for management interface
  3304. */
  3305. static const struct file_operations pmcraid_fops = {
  3306. .owner = THIS_MODULE,
  3307. .open = pmcraid_chr_open,
  3308. .release = pmcraid_chr_release,
  3309. .fasync = pmcraid_chr_fasync,
  3310. .unlocked_ioctl = pmcraid_chr_ioctl,
  3311. #ifdef CONFIG_COMPAT
  3312. .compat_ioctl = pmcraid_chr_ioctl,
  3313. #endif
  3314. };
  3315. /**
  3316. * pmcraid_show_log_level - Display adapter's error logging level
  3317. * @dev: class device struct
  3318. * @buf: buffer
  3319. *
  3320. * Return value:
  3321. * number of bytes printed to buffer
  3322. */
  3323. static ssize_t pmcraid_show_log_level(
  3324. struct device *dev,
  3325. struct device_attribute *attr,
  3326. char *buf)
  3327. {
  3328. struct Scsi_Host *shost = class_to_shost(dev);
  3329. struct pmcraid_instance *pinstance =
  3330. (struct pmcraid_instance *)shost->hostdata;
  3331. return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
  3332. }
  3333. /**
  3334. * pmcraid_store_log_level - Change the adapter's error logging level
  3335. * @dev: class device struct
  3336. * @buf: buffer
  3337. * @count: not used
  3338. *
  3339. * Return value:
  3340. * number of bytes printed to buffer
  3341. */
  3342. static ssize_t pmcraid_store_log_level(
  3343. struct device *dev,
  3344. struct device_attribute *attr,
  3345. const char *buf,
  3346. size_t count
  3347. )
  3348. {
  3349. struct Scsi_Host *shost;
  3350. struct pmcraid_instance *pinstance;
  3351. unsigned long val;
  3352. if (strict_strtoul(buf, 10, &val))
  3353. return -EINVAL;
  3354. /* log-level should be from 0 to 2 */
  3355. if (val > 2)
  3356. return -EINVAL;
  3357. shost = class_to_shost(dev);
  3358. pinstance = (struct pmcraid_instance *)shost->hostdata;
  3359. pinstance->current_log_level = val;
  3360. return strlen(buf);
  3361. }
  3362. static struct device_attribute pmcraid_log_level_attr = {
  3363. .attr = {
  3364. .name = "log_level",
  3365. .mode = S_IRUGO | S_IWUSR,
  3366. },
  3367. .show = pmcraid_show_log_level,
  3368. .store = pmcraid_store_log_level,
  3369. };
  3370. /**
  3371. * pmcraid_show_drv_version - Display driver version
  3372. * @dev: class device struct
  3373. * @buf: buffer
  3374. *
  3375. * Return value:
  3376. * number of bytes printed to buffer
  3377. */
  3378. static ssize_t pmcraid_show_drv_version(
  3379. struct device *dev,
  3380. struct device_attribute *attr,
  3381. char *buf
  3382. )
  3383. {
  3384. return snprintf(buf, PAGE_SIZE, "version: %s, build date: %s\n",
  3385. PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
  3386. }
  3387. static struct device_attribute pmcraid_driver_version_attr = {
  3388. .attr = {
  3389. .name = "drv_version",
  3390. .mode = S_IRUGO,
  3391. },
  3392. .show = pmcraid_show_drv_version,
  3393. };
  3394. /**
  3395. * pmcraid_show_io_adapter_id - Display driver assigned adapter id
  3396. * @dev: class device struct
  3397. * @buf: buffer
  3398. *
  3399. * Return value:
  3400. * number of bytes printed to buffer
  3401. */
  3402. static ssize_t pmcraid_show_adapter_id(
  3403. struct device *dev,
  3404. struct device_attribute *attr,
  3405. char *buf
  3406. )
  3407. {
  3408. struct Scsi_Host *shost = class_to_shost(dev);
  3409. struct pmcraid_instance *pinstance =
  3410. (struct pmcraid_instance *)shost->hostdata;
  3411. u32 adapter_id = (pinstance->pdev->bus->number << 8) |
  3412. pinstance->pdev->devfn;
  3413. u32 aen_group = pmcraid_event_family.id;
  3414. return snprintf(buf, PAGE_SIZE,
  3415. "adapter id: %d\nminor: %d\naen group: %d\n",
  3416. adapter_id, MINOR(pinstance->cdev.dev), aen_group);
  3417. }
  3418. static struct device_attribute pmcraid_adapter_id_attr = {
  3419. .attr = {
  3420. .name = "adapter_id",
  3421. .mode = S_IRUGO | S_IWUSR,
  3422. },
  3423. .show = pmcraid_show_adapter_id,
  3424. };
  3425. static struct device_attribute *pmcraid_host_attrs[] = {
  3426. &pmcraid_log_level_attr,
  3427. &pmcraid_driver_version_attr,
  3428. &pmcraid_adapter_id_attr,
  3429. NULL,
  3430. };
  3431. /* host template structure for pmcraid driver */
  3432. static struct scsi_host_template pmcraid_host_template = {
  3433. .module = THIS_MODULE,
  3434. .name = PMCRAID_DRIVER_NAME,
  3435. .queuecommand = pmcraid_queuecommand,
  3436. .eh_abort_handler = pmcraid_eh_abort_handler,
  3437. .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
  3438. .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
  3439. .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
  3440. .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
  3441. .slave_alloc = pmcraid_slave_alloc,
  3442. .slave_configure = pmcraid_slave_configure,
  3443. .slave_destroy = pmcraid_slave_destroy,
  3444. .change_queue_depth = pmcraid_change_queue_depth,
  3445. .change_queue_type = pmcraid_change_queue_type,
  3446. .can_queue = PMCRAID_MAX_IO_CMD,
  3447. .this_id = -1,
  3448. .sg_tablesize = PMCRAID_MAX_IOADLS,
  3449. .max_sectors = PMCRAID_IOA_MAX_SECTORS,
  3450. .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
  3451. .use_clustering = ENABLE_CLUSTERING,
  3452. .shost_attrs = pmcraid_host_attrs,
  3453. .proc_name = PMCRAID_DRIVER_NAME
  3454. };
  3455. /**
  3456. * pmcraid_isr_common - Common interrupt handler routine
  3457. *
  3458. * @pinstance: pointer to adapter instance
  3459. * @intrs: active interrupts (contents of ioa_host_interrupt register)
  3460. * @hrrq_id: Host RRQ index
  3461. *
  3462. * Return Value
  3463. * none
  3464. */
  3465. static void pmcraid_isr_common(
  3466. struct pmcraid_instance *pinstance,
  3467. u32 intrs,
  3468. int hrrq_id
  3469. )
  3470. {
  3471. u32 intrs_clear =
  3472. (intrs & INTRS_CRITICAL_OP_IN_PROGRESS) ? intrs
  3473. : INTRS_HRRQ_VALID;
  3474. iowrite32(intrs_clear,
  3475. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3476. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  3477. /* hrrq valid bit was set, schedule tasklet to handle the response */
  3478. if (intrs_clear == INTRS_HRRQ_VALID)
  3479. tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
  3480. }
  3481. /**
  3482. * pmcraid_isr - implements interrupt handling routine
  3483. *
  3484. * @irq: interrupt vector number
  3485. * @dev_id: pointer hrrq_vector
  3486. *
  3487. * Return Value
  3488. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3489. */
  3490. static irqreturn_t pmcraid_isr(int irq, void *dev_id)
  3491. {
  3492. struct pmcraid_isr_param *hrrq_vector;
  3493. struct pmcraid_instance *pinstance;
  3494. unsigned long lock_flags;
  3495. u32 intrs;
  3496. /* In case of legacy interrupt mode where interrupts are shared across
  3497. * isrs, it may be possible that the current interrupt is not from IOA
  3498. */
  3499. if (!dev_id) {
  3500. printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
  3501. return IRQ_NONE;
  3502. }
  3503. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3504. pinstance = hrrq_vector->drv_inst;
  3505. /* Acquire the lock (currently host_lock) while processing interrupts.
  3506. * This interval is small as most of the response processing is done by
  3507. * tasklet without the lock.
  3508. */
  3509. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3510. intrs = pmcraid_read_interrupts(pinstance);
  3511. if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0)) {
  3512. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3513. return IRQ_NONE;
  3514. }
  3515. /* Any error interrupts including unit_check, initiate IOA reset.
  3516. * In case of unit check indicate to reset_sequence that IOA unit
  3517. * checked and prepare for a dump during reset sequence
  3518. */
  3519. if (intrs & PMCRAID_ERROR_INTERRUPTS) {
  3520. if (intrs & INTRS_IOA_UNIT_CHECK)
  3521. pinstance->ioa_unit_check = 1;
  3522. iowrite32(intrs,
  3523. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3524. pmcraid_err("ISR: error interrupts: %x initiating reset\n",
  3525. intrs);
  3526. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  3527. pmcraid_initiate_reset(pinstance);
  3528. } else {
  3529. pmcraid_isr_common(pinstance, intrs, hrrq_vector->hrrq_id);
  3530. }
  3531. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3532. return IRQ_HANDLED;
  3533. }
  3534. /**
  3535. * pmcraid_worker_function - worker thread function
  3536. *
  3537. * @workp: pointer to struct work queue
  3538. *
  3539. * Return Value
  3540. * None
  3541. */
  3542. static void pmcraid_worker_function(struct work_struct *workp)
  3543. {
  3544. struct pmcraid_instance *pinstance;
  3545. struct pmcraid_resource_entry *res;
  3546. struct pmcraid_resource_entry *temp;
  3547. struct scsi_device *sdev;
  3548. unsigned long lock_flags;
  3549. unsigned long host_lock_flags;
  3550. u8 bus, target, lun;
  3551. pinstance = container_of(workp, struct pmcraid_instance, worker_q);
  3552. /* add resources only after host is added into system */
  3553. if (!atomic_read(&pinstance->expose_resources))
  3554. return;
  3555. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  3556. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
  3557. if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
  3558. sdev = res->scsi_dev;
  3559. /* host_lock must be held before calling
  3560. * scsi_device_get
  3561. */
  3562. spin_lock_irqsave(pinstance->host->host_lock,
  3563. host_lock_flags);
  3564. if (!scsi_device_get(sdev)) {
  3565. spin_unlock_irqrestore(
  3566. pinstance->host->host_lock,
  3567. host_lock_flags);
  3568. pmcraid_info("deleting %x from midlayer\n",
  3569. res->cfg_entry.resource_address);
  3570. list_move_tail(&res->queue,
  3571. &pinstance->free_res_q);
  3572. spin_unlock_irqrestore(
  3573. &pinstance->resource_lock,
  3574. lock_flags);
  3575. scsi_remove_device(sdev);
  3576. scsi_device_put(sdev);
  3577. spin_lock_irqsave(&pinstance->resource_lock,
  3578. lock_flags);
  3579. res->change_detected = 0;
  3580. } else {
  3581. spin_unlock_irqrestore(
  3582. pinstance->host->host_lock,
  3583. host_lock_flags);
  3584. }
  3585. }
  3586. }
  3587. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  3588. if (res->change_detected == RES_CHANGE_ADD) {
  3589. if (!pmcraid_expose_resource(&res->cfg_entry))
  3590. continue;
  3591. if (RES_IS_VSET(res->cfg_entry)) {
  3592. bus = PMCRAID_VSET_BUS_ID;
  3593. target = res->cfg_entry.unique_flags1;
  3594. lun = PMCRAID_VSET_LUN_ID;
  3595. } else {
  3596. bus = PMCRAID_PHYS_BUS_ID;
  3597. target =
  3598. RES_TARGET(
  3599. res->cfg_entry.resource_address);
  3600. lun = RES_LUN(res->cfg_entry.resource_address);
  3601. }
  3602. res->change_detected = 0;
  3603. spin_unlock_irqrestore(&pinstance->resource_lock,
  3604. lock_flags);
  3605. scsi_add_device(pinstance->host, bus, target, lun);
  3606. spin_lock_irqsave(&pinstance->resource_lock,
  3607. lock_flags);
  3608. }
  3609. }
  3610. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  3611. }
  3612. /**
  3613. * pmcraid_tasklet_function - Tasklet function
  3614. *
  3615. * @instance: pointer to msix param structure
  3616. *
  3617. * Return Value
  3618. * None
  3619. */
  3620. void pmcraid_tasklet_function(unsigned long instance)
  3621. {
  3622. struct pmcraid_isr_param *hrrq_vector;
  3623. struct pmcraid_instance *pinstance;
  3624. unsigned long hrrq_lock_flags;
  3625. unsigned long pending_lock_flags;
  3626. unsigned long host_lock_flags;
  3627. spinlock_t *lockp; /* hrrq buffer lock */
  3628. int id;
  3629. u32 intrs;
  3630. __le32 resp;
  3631. hrrq_vector = (struct pmcraid_isr_param *)instance;
  3632. pinstance = hrrq_vector->drv_inst;
  3633. id = hrrq_vector->hrrq_id;
  3634. lockp = &(pinstance->hrrq_lock[id]);
  3635. intrs = pmcraid_read_interrupts(pinstance);
  3636. /* If interrupts was as part of the ioa initialization, clear and mask
  3637. * it. Delete the timer and wakeup the reset engine to proceed with
  3638. * reset sequence
  3639. */
  3640. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  3641. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  3642. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  3643. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  3644. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3645. if (pinstance->reset_cmd != NULL) {
  3646. del_timer(&pinstance->reset_cmd->timer);
  3647. spin_lock_irqsave(pinstance->host->host_lock,
  3648. host_lock_flags);
  3649. pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
  3650. spin_unlock_irqrestore(pinstance->host->host_lock,
  3651. host_lock_flags);
  3652. }
  3653. return;
  3654. }
  3655. /* loop through each of the commands responded by IOA. Each HRRQ buf is
  3656. * protected by its own lock. Traversals must be done within this lock
  3657. * as there may be multiple tasklets running on multiple CPUs. Note
  3658. * that the lock is held just for picking up the response handle and
  3659. * manipulating hrrq_curr/toggle_bit values.
  3660. */
  3661. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3662. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3663. while ((resp & HRRQ_TOGGLE_BIT) ==
  3664. pinstance->host_toggle_bit[id]) {
  3665. int cmd_index = resp >> 2;
  3666. struct pmcraid_cmd *cmd = NULL;
  3667. if (cmd_index < PMCRAID_MAX_CMD) {
  3668. cmd = pinstance->cmd_list[cmd_index];
  3669. } else {
  3670. /* In case of invalid response handle, initiate IOA
  3671. * reset sequence.
  3672. */
  3673. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3674. pmcraid_err("Invalid response %d initiating reset\n",
  3675. cmd_index);
  3676. spin_lock_irqsave(pinstance->host->host_lock,
  3677. host_lock_flags);
  3678. pmcraid_initiate_reset(pinstance);
  3679. spin_unlock_irqrestore(pinstance->host->host_lock,
  3680. host_lock_flags);
  3681. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3682. break;
  3683. }
  3684. if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
  3685. pinstance->hrrq_curr[id]++;
  3686. } else {
  3687. pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
  3688. pinstance->host_toggle_bit[id] ^= 1u;
  3689. }
  3690. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3691. spin_lock_irqsave(&pinstance->pending_pool_lock,
  3692. pending_lock_flags);
  3693. list_del(&cmd->free_list);
  3694. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  3695. pending_lock_flags);
  3696. del_timer(&cmd->timer);
  3697. atomic_dec(&pinstance->outstanding_cmds);
  3698. if (cmd->cmd_done == pmcraid_ioa_reset) {
  3699. spin_lock_irqsave(pinstance->host->host_lock,
  3700. host_lock_flags);
  3701. cmd->cmd_done(cmd);
  3702. spin_unlock_irqrestore(pinstance->host->host_lock,
  3703. host_lock_flags);
  3704. } else if (cmd->cmd_done != NULL) {
  3705. cmd->cmd_done(cmd);
  3706. }
  3707. /* loop over until we are done with all responses */
  3708. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3709. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3710. }
  3711. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3712. }
  3713. /**
  3714. * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
  3715. * @pinstance: pointer to adapter instance structure
  3716. *
  3717. * This routine un-registers registered interrupt handler and
  3718. * also frees irqs/vectors.
  3719. *
  3720. * Retun Value
  3721. * None
  3722. */
  3723. static
  3724. void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
  3725. {
  3726. free_irq(pinstance->pdev->irq, &(pinstance->hrrq_vector[0]));
  3727. }
  3728. /**
  3729. * pmcraid_register_interrupt_handler - registers interrupt handler
  3730. * @pinstance: pointer to per-adapter instance structure
  3731. *
  3732. * Return Value
  3733. * 0 on success, non-zero error code otherwise.
  3734. */
  3735. static int
  3736. pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
  3737. {
  3738. struct pci_dev *pdev = pinstance->pdev;
  3739. pinstance->hrrq_vector[0].hrrq_id = 0;
  3740. pinstance->hrrq_vector[0].drv_inst = pinstance;
  3741. pinstance->hrrq_vector[0].vector = 0;
  3742. pinstance->num_hrrq = 1;
  3743. return request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
  3744. PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
  3745. }
  3746. /**
  3747. * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
  3748. * @pinstance: per adapter instance structure pointer
  3749. * @max_index: number of buffer blocks to release
  3750. *
  3751. * Return Value
  3752. * None
  3753. */
  3754. static void
  3755. pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
  3756. {
  3757. int i;
  3758. for (i = 0; i < max_index; i++) {
  3759. kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
  3760. pinstance->cmd_list[i] = NULL;
  3761. }
  3762. kmem_cache_destroy(pinstance->cmd_cachep);
  3763. pinstance->cmd_cachep = NULL;
  3764. }
  3765. /**
  3766. * pmcraid_release_control_blocks - releases buffers alloced for control blocks
  3767. * @pinstance: pointer to per adapter instance structure
  3768. * @max_index: number of buffers (from 0 onwards) to release
  3769. *
  3770. * This function assumes that the command blocks for which control blocks are
  3771. * linked are not released.
  3772. *
  3773. * Return Value
  3774. * None
  3775. */
  3776. static void
  3777. pmcraid_release_control_blocks(
  3778. struct pmcraid_instance *pinstance,
  3779. int max_index
  3780. )
  3781. {
  3782. int i;
  3783. if (pinstance->control_pool == NULL)
  3784. return;
  3785. for (i = 0; i < max_index; i++) {
  3786. pci_pool_free(pinstance->control_pool,
  3787. pinstance->cmd_list[i]->ioa_cb,
  3788. pinstance->cmd_list[i]->ioa_cb_bus_addr);
  3789. pinstance->cmd_list[i]->ioa_cb = NULL;
  3790. pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
  3791. }
  3792. pci_pool_destroy(pinstance->control_pool);
  3793. pinstance->control_pool = NULL;
  3794. }
  3795. /**
  3796. * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
  3797. * @pinstance - pointer to per adapter instance structure
  3798. *
  3799. * Allocates memory for command blocks using kernel slab allocator.
  3800. *
  3801. * Return Value
  3802. * 0 in case of success; -ENOMEM in case of failure
  3803. */
  3804. static int __devinit
  3805. pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
  3806. {
  3807. int i;
  3808. sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
  3809. pinstance->host->unique_id);
  3810. pinstance->cmd_cachep = kmem_cache_create(
  3811. pinstance->cmd_pool_name,
  3812. sizeof(struct pmcraid_cmd), 0,
  3813. SLAB_HWCACHE_ALIGN, NULL);
  3814. if (!pinstance->cmd_cachep)
  3815. return -ENOMEM;
  3816. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  3817. pinstance->cmd_list[i] =
  3818. kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
  3819. if (!pinstance->cmd_list[i]) {
  3820. pmcraid_release_cmd_blocks(pinstance, i);
  3821. return -ENOMEM;
  3822. }
  3823. }
  3824. return 0;
  3825. }
  3826. /**
  3827. * pmcraid_allocate_control_blocks - allocates memory control blocks
  3828. * @pinstance : pointer to per adapter instance structure
  3829. *
  3830. * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
  3831. * and IOASAs. This is called after command blocks are already allocated.
  3832. *
  3833. * Return Value
  3834. * 0 in case it can allocate all control blocks, otherwise -ENOMEM
  3835. */
  3836. static int __devinit
  3837. pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
  3838. {
  3839. int i;
  3840. sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
  3841. pinstance->host->unique_id);
  3842. pinstance->control_pool =
  3843. pci_pool_create(pinstance->ctl_pool_name,
  3844. pinstance->pdev,
  3845. sizeof(struct pmcraid_control_block),
  3846. PMCRAID_IOARCB_ALIGNMENT, 0);
  3847. if (!pinstance->control_pool)
  3848. return -ENOMEM;
  3849. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  3850. pinstance->cmd_list[i]->ioa_cb =
  3851. pci_pool_alloc(
  3852. pinstance->control_pool,
  3853. GFP_KERNEL,
  3854. &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
  3855. if (!pinstance->cmd_list[i]->ioa_cb) {
  3856. pmcraid_release_control_blocks(pinstance, i);
  3857. return -ENOMEM;
  3858. }
  3859. memset(pinstance->cmd_list[i]->ioa_cb, 0,
  3860. sizeof(struct pmcraid_control_block));
  3861. }
  3862. return 0;
  3863. }
  3864. /**
  3865. * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
  3866. * @pinstance: pointer to per adapter instance structure
  3867. * @maxindex: size of hrrq buffer pointer array
  3868. *
  3869. * Return Value
  3870. * None
  3871. */
  3872. static void
  3873. pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
  3874. {
  3875. int i;
  3876. for (i = 0; i < maxindex; i++) {
  3877. pci_free_consistent(pinstance->pdev,
  3878. HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
  3879. pinstance->hrrq_start[i],
  3880. pinstance->hrrq_start_bus_addr[i]);
  3881. /* reset pointers and toggle bit to zeros */
  3882. pinstance->hrrq_start[i] = NULL;
  3883. pinstance->hrrq_start_bus_addr[i] = 0;
  3884. pinstance->host_toggle_bit[i] = 0;
  3885. }
  3886. }
  3887. /**
  3888. * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
  3889. * @pinstance: pointer to per adapter instance structure
  3890. *
  3891. * Return value
  3892. * 0 hrrq buffers are allocated, -ENOMEM otherwise.
  3893. */
  3894. static int __devinit
  3895. pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
  3896. {
  3897. int i;
  3898. int buf_count = PMCRAID_MAX_CMD / pinstance->num_hrrq;
  3899. for (i = 0; i < pinstance->num_hrrq; i++) {
  3900. int buffer_size = HRRQ_ENTRY_SIZE * buf_count;
  3901. pinstance->hrrq_start[i] =
  3902. pci_alloc_consistent(
  3903. pinstance->pdev,
  3904. buffer_size,
  3905. &(pinstance->hrrq_start_bus_addr[i]));
  3906. if (pinstance->hrrq_start[i] == 0) {
  3907. pmcraid_err("could not allocate host rrq: %d\n", i);
  3908. pmcraid_release_host_rrqs(pinstance, i);
  3909. return -ENOMEM;
  3910. }
  3911. memset(pinstance->hrrq_start[i], 0, buffer_size);
  3912. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  3913. pinstance->hrrq_end[i] =
  3914. pinstance->hrrq_start[i] + buf_count - 1;
  3915. pinstance->host_toggle_bit[i] = 1;
  3916. spin_lock_init(&pinstance->hrrq_lock[i]);
  3917. }
  3918. return 0;
  3919. }
  3920. /**
  3921. * pmcraid_release_hcams - release HCAM buffers
  3922. *
  3923. * @pinstance: pointer to per adapter instance structure
  3924. *
  3925. * Return value
  3926. * none
  3927. */
  3928. static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
  3929. {
  3930. if (pinstance->ccn.msg != NULL) {
  3931. pci_free_consistent(pinstance->pdev,
  3932. PMCRAID_AEN_HDR_SIZE +
  3933. sizeof(struct pmcraid_hcam_ccn),
  3934. pinstance->ccn.msg,
  3935. pinstance->ccn.baddr);
  3936. pinstance->ccn.msg = NULL;
  3937. pinstance->ccn.hcam = NULL;
  3938. pinstance->ccn.baddr = 0;
  3939. }
  3940. if (pinstance->ldn.msg != NULL) {
  3941. pci_free_consistent(pinstance->pdev,
  3942. PMCRAID_AEN_HDR_SIZE +
  3943. sizeof(struct pmcraid_hcam_ldn),
  3944. pinstance->ldn.msg,
  3945. pinstance->ldn.baddr);
  3946. pinstance->ldn.msg = NULL;
  3947. pinstance->ldn.hcam = NULL;
  3948. pinstance->ldn.baddr = 0;
  3949. }
  3950. }
  3951. /**
  3952. * pmcraid_allocate_hcams - allocates HCAM buffers
  3953. * @pinstance : pointer to per adapter instance structure
  3954. *
  3955. * Return Value:
  3956. * 0 in case of successful allocation, non-zero otherwise
  3957. */
  3958. static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
  3959. {
  3960. pinstance->ccn.msg = pci_alloc_consistent(
  3961. pinstance->pdev,
  3962. PMCRAID_AEN_HDR_SIZE +
  3963. sizeof(struct pmcraid_hcam_ccn),
  3964. &(pinstance->ccn.baddr));
  3965. pinstance->ldn.msg = pci_alloc_consistent(
  3966. pinstance->pdev,
  3967. PMCRAID_AEN_HDR_SIZE +
  3968. sizeof(struct pmcraid_hcam_ldn),
  3969. &(pinstance->ldn.baddr));
  3970. if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
  3971. pmcraid_release_hcams(pinstance);
  3972. } else {
  3973. pinstance->ccn.hcam =
  3974. (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
  3975. pinstance->ldn.hcam =
  3976. (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
  3977. atomic_set(&pinstance->ccn.ignore, 0);
  3978. atomic_set(&pinstance->ldn.ignore, 0);
  3979. }
  3980. return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
  3981. }
  3982. /**
  3983. * pmcraid_release_config_buffers - release config.table buffers
  3984. * @pinstance: pointer to per adapter instance structure
  3985. *
  3986. * Return Value
  3987. * none
  3988. */
  3989. static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
  3990. {
  3991. if (pinstance->cfg_table != NULL &&
  3992. pinstance->cfg_table_bus_addr != 0) {
  3993. pci_free_consistent(pinstance->pdev,
  3994. sizeof(struct pmcraid_config_table),
  3995. pinstance->cfg_table,
  3996. pinstance->cfg_table_bus_addr);
  3997. pinstance->cfg_table = NULL;
  3998. pinstance->cfg_table_bus_addr = 0;
  3999. }
  4000. if (pinstance->res_entries != NULL) {
  4001. int i;
  4002. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4003. list_del(&pinstance->res_entries[i].queue);
  4004. kfree(pinstance->res_entries);
  4005. pinstance->res_entries = NULL;
  4006. }
  4007. pmcraid_release_hcams(pinstance);
  4008. }
  4009. /**
  4010. * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
  4011. * @pinstance : pointer to per adapter instance structure
  4012. *
  4013. * Return Value
  4014. * 0 for successful allocation, -ENOMEM for any failure
  4015. */
  4016. static int __devinit
  4017. pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
  4018. {
  4019. int i;
  4020. pinstance->res_entries =
  4021. kzalloc(sizeof(struct pmcraid_resource_entry) *
  4022. PMCRAID_MAX_RESOURCES, GFP_KERNEL);
  4023. if (NULL == pinstance->res_entries) {
  4024. pmcraid_err("failed to allocate memory for resource table\n");
  4025. return -ENOMEM;
  4026. }
  4027. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4028. list_add_tail(&pinstance->res_entries[i].queue,
  4029. &pinstance->free_res_q);
  4030. pinstance->cfg_table =
  4031. pci_alloc_consistent(pinstance->pdev,
  4032. sizeof(struct pmcraid_config_table),
  4033. &pinstance->cfg_table_bus_addr);
  4034. if (NULL == pinstance->cfg_table) {
  4035. pmcraid_err("couldn't alloc DMA memory for config table\n");
  4036. pmcraid_release_config_buffers(pinstance);
  4037. return -ENOMEM;
  4038. }
  4039. if (pmcraid_allocate_hcams(pinstance)) {
  4040. pmcraid_err("could not alloc DMA memory for HCAMS\n");
  4041. pmcraid_release_config_buffers(pinstance);
  4042. return -ENOMEM;
  4043. }
  4044. return 0;
  4045. }
  4046. /**
  4047. * pmcraid_init_tasklets - registers tasklets for response handling
  4048. *
  4049. * @pinstance: pointer adapter instance structure
  4050. *
  4051. * Return value
  4052. * none
  4053. */
  4054. static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
  4055. {
  4056. int i;
  4057. for (i = 0; i < pinstance->num_hrrq; i++)
  4058. tasklet_init(&pinstance->isr_tasklet[i],
  4059. pmcraid_tasklet_function,
  4060. (unsigned long)&pinstance->hrrq_vector[i]);
  4061. }
  4062. /**
  4063. * pmcraid_kill_tasklets - destroys tasklets registered for response handling
  4064. *
  4065. * @pinstance: pointer to adapter instance structure
  4066. *
  4067. * Return value
  4068. * none
  4069. */
  4070. static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
  4071. {
  4072. int i;
  4073. for (i = 0; i < pinstance->num_hrrq; i++)
  4074. tasklet_kill(&pinstance->isr_tasklet[i]);
  4075. }
  4076. /**
  4077. * pmcraid_init_buffers - allocates memory and initializes various structures
  4078. * @pinstance: pointer to per adapter instance structure
  4079. *
  4080. * This routine pre-allocates memory based on the type of block as below:
  4081. * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
  4082. * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
  4083. * config-table entries : DMAable memory using pci_alloc_consistent
  4084. * HostRRQs : DMAable memory, using pci_alloc_consistent
  4085. *
  4086. * Return Value
  4087. * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
  4088. */
  4089. static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
  4090. {
  4091. int i;
  4092. if (pmcraid_allocate_host_rrqs(pinstance)) {
  4093. pmcraid_err("couldn't allocate memory for %d host rrqs\n",
  4094. pinstance->num_hrrq);
  4095. return -ENOMEM;
  4096. }
  4097. if (pmcraid_allocate_config_buffers(pinstance)) {
  4098. pmcraid_err("couldn't allocate memory for config buffers\n");
  4099. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4100. return -ENOMEM;
  4101. }
  4102. if (pmcraid_allocate_cmd_blocks(pinstance)) {
  4103. pmcraid_err("couldn't allocate memory for cmd blocks \n");
  4104. pmcraid_release_config_buffers(pinstance);
  4105. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4106. return -ENOMEM;
  4107. }
  4108. if (pmcraid_allocate_control_blocks(pinstance)) {
  4109. pmcraid_err("couldn't allocate memory control blocks \n");
  4110. pmcraid_release_config_buffers(pinstance);
  4111. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4112. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4113. return -ENOMEM;
  4114. }
  4115. /* Initialize all the command blocks and add them to free pool. No
  4116. * need to lock (free_pool_lock) as this is done in initialization
  4117. * itself
  4118. */
  4119. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4120. struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
  4121. pmcraid_init_cmdblk(cmdp, i);
  4122. cmdp->drv_inst = pinstance;
  4123. list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
  4124. }
  4125. return 0;
  4126. }
  4127. /**
  4128. * pmcraid_reinit_buffers - resets various buffer pointers
  4129. * @pinstance: pointer to adapter instance
  4130. * Return value
  4131. * none
  4132. */
  4133. static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
  4134. {
  4135. int i;
  4136. int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4137. for (i = 0; i < pinstance->num_hrrq; i++) {
  4138. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4139. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4140. pinstance->hrrq_end[i] =
  4141. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4142. pinstance->host_toggle_bit[i] = 1;
  4143. }
  4144. }
  4145. /**
  4146. * pmcraid_init_instance - initialize per instance data structure
  4147. * @pdev: pointer to pci device structure
  4148. * @host: pointer to Scsi_Host structure
  4149. * @mapped_pci_addr: memory mapped IOA configuration registers
  4150. *
  4151. * Return Value
  4152. * 0 on success, non-zero in case of any failure
  4153. */
  4154. static int __devinit pmcraid_init_instance(
  4155. struct pci_dev *pdev,
  4156. struct Scsi_Host *host,
  4157. void __iomem *mapped_pci_addr
  4158. )
  4159. {
  4160. struct pmcraid_instance *pinstance =
  4161. (struct pmcraid_instance *)host->hostdata;
  4162. pinstance->host = host;
  4163. pinstance->pdev = pdev;
  4164. /* Initialize register addresses */
  4165. pinstance->mapped_dma_addr = mapped_pci_addr;
  4166. /* Initialize chip-specific details */
  4167. {
  4168. struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
  4169. struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
  4170. pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
  4171. pint_regs->ioa_host_interrupt_reg =
  4172. mapped_pci_addr + chip_cfg->ioa_host_intr;
  4173. pint_regs->ioa_host_interrupt_clr_reg =
  4174. mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
  4175. pint_regs->host_ioa_interrupt_reg =
  4176. mapped_pci_addr + chip_cfg->host_ioa_intr;
  4177. pint_regs->host_ioa_interrupt_clr_reg =
  4178. mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
  4179. /* Current version of firmware exposes interrupt mask set
  4180. * and mask clr registers through memory mapped bar0.
  4181. */
  4182. pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
  4183. pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
  4184. pint_regs->ioa_host_interrupt_mask_reg =
  4185. mapped_pci_addr + chip_cfg->ioa_host_mask;
  4186. pint_regs->ioa_host_interrupt_mask_clr_reg =
  4187. mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
  4188. pint_regs->global_interrupt_mask_reg =
  4189. mapped_pci_addr + chip_cfg->global_intr_mask;
  4190. };
  4191. pinstance->ioa_reset_attempts = 0;
  4192. init_waitqueue_head(&pinstance->reset_wait_q);
  4193. atomic_set(&pinstance->outstanding_cmds, 0);
  4194. atomic_set(&pinstance->expose_resources, 0);
  4195. INIT_LIST_HEAD(&pinstance->free_res_q);
  4196. INIT_LIST_HEAD(&pinstance->used_res_q);
  4197. INIT_LIST_HEAD(&pinstance->free_cmd_pool);
  4198. INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
  4199. spin_lock_init(&pinstance->free_pool_lock);
  4200. spin_lock_init(&pinstance->pending_pool_lock);
  4201. spin_lock_init(&pinstance->resource_lock);
  4202. mutex_init(&pinstance->aen_queue_lock);
  4203. /* Work-queue (Shared) for deferred processing error handling */
  4204. INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
  4205. /* Initialize the default log_level */
  4206. pinstance->current_log_level = pmcraid_log_level;
  4207. /* Setup variables required for reset engine */
  4208. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  4209. pinstance->reset_cmd = NULL;
  4210. return 0;
  4211. }
  4212. /**
  4213. * pmcraid_release_buffers - release per-adapter buffers allocated
  4214. *
  4215. * @pinstance: pointer to adapter soft state
  4216. *
  4217. * Return Value
  4218. * none
  4219. */
  4220. static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
  4221. {
  4222. pmcraid_release_config_buffers(pinstance);
  4223. pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
  4224. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4225. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4226. }
  4227. /**
  4228. * pmcraid_shutdown - shutdown adapter controller.
  4229. * @pdev: pci device struct
  4230. *
  4231. * Issues an adapter shutdown to the card waits for its completion
  4232. *
  4233. * Return value
  4234. * none
  4235. */
  4236. static void pmcraid_shutdown(struct pci_dev *pdev)
  4237. {
  4238. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4239. pmcraid_reset_bringdown(pinstance);
  4240. }
  4241. /**
  4242. * pmcraid_get_minor - returns unused minor number from minor number bitmap
  4243. */
  4244. static unsigned short pmcraid_get_minor(void)
  4245. {
  4246. int minor;
  4247. minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
  4248. __set_bit(minor, pmcraid_minor);
  4249. return minor;
  4250. }
  4251. /**
  4252. * pmcraid_release_minor - releases given minor back to minor number bitmap
  4253. */
  4254. static void pmcraid_release_minor(unsigned short minor)
  4255. {
  4256. __clear_bit(minor, pmcraid_minor);
  4257. }
  4258. /**
  4259. * pmcraid_setup_chrdev - allocates a minor number and registers a char device
  4260. *
  4261. * @pinstance: pointer to adapter instance for which to register device
  4262. *
  4263. * Return value
  4264. * 0 in case of success, otherwise non-zero
  4265. */
  4266. static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
  4267. {
  4268. int minor;
  4269. int error;
  4270. minor = pmcraid_get_minor();
  4271. cdev_init(&pinstance->cdev, &pmcraid_fops);
  4272. pinstance->cdev.owner = THIS_MODULE;
  4273. error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
  4274. if (error)
  4275. pmcraid_release_minor(minor);
  4276. else
  4277. device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
  4278. NULL, "pmcsas%u", minor);
  4279. return error;
  4280. }
  4281. /**
  4282. * pmcraid_release_chrdev - unregisters per-adapter management interface
  4283. *
  4284. * @pinstance: pointer to adapter instance structure
  4285. *
  4286. * Return value
  4287. * none
  4288. */
  4289. static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
  4290. {
  4291. pmcraid_release_minor(MINOR(pinstance->cdev.dev));
  4292. device_destroy(pmcraid_class,
  4293. MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
  4294. cdev_del(&pinstance->cdev);
  4295. }
  4296. /**
  4297. * pmcraid_remove - IOA hot plug remove entry point
  4298. * @pdev: pci device struct
  4299. *
  4300. * Return value
  4301. * none
  4302. */
  4303. static void __devexit pmcraid_remove(struct pci_dev *pdev)
  4304. {
  4305. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4306. /* remove the management interface (/dev file) for this device */
  4307. pmcraid_release_chrdev(pinstance);
  4308. /* remove host template from scsi midlayer */
  4309. scsi_remove_host(pinstance->host);
  4310. /* block requests from mid-layer */
  4311. scsi_block_requests(pinstance->host);
  4312. /* initiate shutdown adapter */
  4313. pmcraid_shutdown(pdev);
  4314. pmcraid_disable_interrupts(pinstance, ~0);
  4315. flush_scheduled_work();
  4316. pmcraid_kill_tasklets(pinstance);
  4317. pmcraid_unregister_interrupt_handler(pinstance);
  4318. pmcraid_release_buffers(pinstance);
  4319. iounmap(pinstance->mapped_dma_addr);
  4320. pci_release_regions(pdev);
  4321. scsi_host_put(pinstance->host);
  4322. pci_disable_device(pdev);
  4323. return;
  4324. }
  4325. #ifdef CONFIG_PM
  4326. /**
  4327. * pmcraid_suspend - driver suspend entry point for power management
  4328. * @pdev: PCI device structure
  4329. * @state: PCI power state to suspend routine
  4330. *
  4331. * Return Value - 0 always
  4332. */
  4333. static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
  4334. {
  4335. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4336. pmcraid_shutdown(pdev);
  4337. pmcraid_disable_interrupts(pinstance, ~0);
  4338. pmcraid_kill_tasklets(pinstance);
  4339. pci_set_drvdata(pinstance->pdev, pinstance);
  4340. pmcraid_unregister_interrupt_handler(pinstance);
  4341. pci_save_state(pdev);
  4342. pci_disable_device(pdev);
  4343. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  4344. return 0;
  4345. }
  4346. /**
  4347. * pmcraid_resume - driver resume entry point PCI power management
  4348. * @pdev: PCI device structure
  4349. *
  4350. * Return Value - 0 in case of success. Error code in case of any failure
  4351. */
  4352. static int pmcraid_resume(struct pci_dev *pdev)
  4353. {
  4354. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4355. struct Scsi_Host *host = pinstance->host;
  4356. int rc;
  4357. int hrrqs;
  4358. pci_set_power_state(pdev, PCI_D0);
  4359. pci_enable_wake(pdev, PCI_D0, 0);
  4360. pci_restore_state(pdev);
  4361. rc = pci_enable_device(pdev);
  4362. if (rc) {
  4363. dev_err(&pdev->dev, "resume: Enable device failed\n");
  4364. return rc;
  4365. }
  4366. pci_set_master(pdev);
  4367. if ((sizeof(dma_addr_t) == 4) ||
  4368. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4369. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4370. if (rc == 0)
  4371. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4372. if (rc != 0) {
  4373. dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
  4374. goto disable_device;
  4375. }
  4376. atomic_set(&pinstance->outstanding_cmds, 0);
  4377. hrrqs = pinstance->num_hrrq;
  4378. rc = pmcraid_register_interrupt_handler(pinstance);
  4379. if (rc) {
  4380. dev_err(&pdev->dev,
  4381. "resume: couldn't register interrupt handlers\n");
  4382. rc = -ENODEV;
  4383. goto release_host;
  4384. }
  4385. pmcraid_init_tasklets(pinstance);
  4386. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4387. /* Start with hard reset sequence which brings up IOA to operational
  4388. * state as well as completes the reset sequence.
  4389. */
  4390. pinstance->ioa_hard_reset = 1;
  4391. /* Start IOA firmware initialization and bring card to Operational
  4392. * state.
  4393. */
  4394. if (pmcraid_reset_bringup(pinstance)) {
  4395. dev_err(&pdev->dev, "couldn't initialize IOA \n");
  4396. rc = -ENODEV;
  4397. goto release_tasklets;
  4398. }
  4399. return 0;
  4400. release_tasklets:
  4401. pmcraid_kill_tasklets(pinstance);
  4402. pmcraid_unregister_interrupt_handler(pinstance);
  4403. release_host:
  4404. scsi_host_put(host);
  4405. disable_device:
  4406. pci_disable_device(pdev);
  4407. return rc;
  4408. }
  4409. #else
  4410. #define pmcraid_suspend NULL
  4411. #define pmcraid_resume NULL
  4412. #endif /* CONFIG_PM */
  4413. /**
  4414. * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
  4415. * completion of the ioa reset
  4416. * @cmd: pointer to reset command block
  4417. */
  4418. static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
  4419. {
  4420. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4421. unsigned long flags;
  4422. spin_lock_irqsave(pinstance->host->host_lock, flags);
  4423. pmcraid_ioa_reset(cmd);
  4424. spin_unlock_irqrestore(pinstance->host->host_lock, flags);
  4425. scsi_unblock_requests(pinstance->host);
  4426. schedule_work(&pinstance->worker_q);
  4427. }
  4428. /**
  4429. * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
  4430. *
  4431. * @cmd: pointer to pmcraid_cmd structure
  4432. *
  4433. * Return Value
  4434. * 0 for success or non-zero for failure cases
  4435. */
  4436. static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
  4437. {
  4438. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4439. void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
  4440. pmcraid_reinit_cmdblk(cmd);
  4441. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4442. ioarcb->request_type = REQ_TYPE_IOACMD;
  4443. ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
  4444. ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
  4445. /* If this was called as part of resource table reinitialization due to
  4446. * lost CCN, it is enough to return the command block back to free pool
  4447. * as part of set_supported_devs completion function.
  4448. */
  4449. if (cmd->drv_inst->reinit_cfg_table) {
  4450. cmd->drv_inst->reinit_cfg_table = 0;
  4451. cmd->release = 1;
  4452. cmd_done = pmcraid_reinit_cfgtable_done;
  4453. }
  4454. /* we will be done with the reset sequence after set supported devices,
  4455. * setup the done function to return the command block back to free
  4456. * pool
  4457. */
  4458. pmcraid_send_cmd(cmd,
  4459. cmd_done,
  4460. PMCRAID_SET_SUP_DEV_TIMEOUT,
  4461. pmcraid_timeout_handler);
  4462. return;
  4463. }
  4464. /**
  4465. * pmcraid_init_res_table - Initialize the resource table
  4466. * @cmd: pointer to pmcraid command struct
  4467. *
  4468. * This function looks through the existing resource table, comparing
  4469. * it with the config table. This function will take care of old/new
  4470. * devices and schedule adding/removing them from the mid-layer
  4471. * as appropriate.
  4472. *
  4473. * Return value
  4474. * None
  4475. */
  4476. static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
  4477. {
  4478. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4479. struct pmcraid_resource_entry *res, *temp;
  4480. struct pmcraid_config_table_entry *cfgte;
  4481. unsigned long lock_flags;
  4482. int found, rc, i;
  4483. LIST_HEAD(old_res);
  4484. if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
  4485. pmcraid_err("IOA requires microcode download\n");
  4486. /* resource list is protected by pinstance->resource_lock.
  4487. * init_res_table can be called from probe (user-thread) or runtime
  4488. * reset (timer/tasklet)
  4489. */
  4490. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  4491. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
  4492. list_move_tail(&res->queue, &old_res);
  4493. for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
  4494. cfgte = &pinstance->cfg_table->entries[i];
  4495. if (!pmcraid_expose_resource(cfgte))
  4496. continue;
  4497. found = 0;
  4498. /* If this entry was already detected and initialized */
  4499. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4500. rc = memcmp(&res->cfg_entry.resource_address,
  4501. &cfgte->resource_address,
  4502. sizeof(cfgte->resource_address));
  4503. if (!rc) {
  4504. list_move_tail(&res->queue,
  4505. &pinstance->used_res_q);
  4506. found = 1;
  4507. break;
  4508. }
  4509. }
  4510. /* If this is new entry, initialize it and add it the queue */
  4511. if (!found) {
  4512. if (list_empty(&pinstance->free_res_q)) {
  4513. pmcraid_err("Too many devices attached\n");
  4514. break;
  4515. }
  4516. found = 1;
  4517. res = list_entry(pinstance->free_res_q.next,
  4518. struct pmcraid_resource_entry, queue);
  4519. res->scsi_dev = NULL;
  4520. res->change_detected = RES_CHANGE_ADD;
  4521. res->reset_progress = 0;
  4522. list_move_tail(&res->queue, &pinstance->used_res_q);
  4523. }
  4524. /* copy new configuration table entry details into driver
  4525. * maintained resource entry
  4526. */
  4527. if (found) {
  4528. memcpy(&res->cfg_entry, cfgte,
  4529. sizeof(struct pmcraid_config_table_entry));
  4530. pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
  4531. res->cfg_entry.resource_type,
  4532. res->cfg_entry.unique_flags1,
  4533. le32_to_cpu(res->cfg_entry.resource_address));
  4534. }
  4535. }
  4536. /* Detect any deleted entries, mark them for deletion from mid-layer */
  4537. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4538. if (res->scsi_dev) {
  4539. res->change_detected = RES_CHANGE_DEL;
  4540. res->cfg_entry.resource_handle =
  4541. PMCRAID_INVALID_RES_HANDLE;
  4542. list_move_tail(&res->queue, &pinstance->used_res_q);
  4543. } else {
  4544. list_move_tail(&res->queue, &pinstance->free_res_q);
  4545. }
  4546. }
  4547. /* release the resource list lock */
  4548. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  4549. pmcraid_set_supported_devs(cmd);
  4550. }
  4551. /**
  4552. * pmcraid_querycfg - Send a Query IOA Config to the adapter.
  4553. * @cmd: pointer pmcraid_cmd struct
  4554. *
  4555. * This function sends a Query IOA Configuration command to the adapter to
  4556. * retrieve the IOA configuration table.
  4557. *
  4558. * Return value:
  4559. * none
  4560. */
  4561. static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
  4562. {
  4563. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4564. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4565. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4566. int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
  4567. ioarcb->request_type = REQ_TYPE_IOACMD;
  4568. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4569. ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
  4570. /* firmware requires 4-byte length field, specified in B.E format */
  4571. memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
  4572. /* Since entire config table can be described by single IOADL, it can
  4573. * be part of IOARCB itself
  4574. */
  4575. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4576. offsetof(struct pmcraid_ioarcb,
  4577. add_data.u.ioadl[0]));
  4578. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4579. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  4580. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4581. ioarcb->data_transfer_length =
  4582. cpu_to_le32(sizeof(struct pmcraid_config_table));
  4583. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4584. ioadl->flags = cpu_to_le32(IOADL_FLAGS_LAST_DESC);
  4585. ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
  4586. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
  4587. pmcraid_send_cmd(cmd, pmcraid_init_res_table,
  4588. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4589. }
  4590. /**
  4591. * pmcraid_probe - PCI probe entry pointer for PMC MaxRaid controller driver
  4592. * @pdev: pointer to pci device structure
  4593. * @dev_id: pointer to device ids structure
  4594. *
  4595. * Return Value
  4596. * returns 0 if the device is claimed and successfully configured.
  4597. * returns non-zero error code in case of any failure
  4598. */
  4599. static int __devinit pmcraid_probe(
  4600. struct pci_dev *pdev,
  4601. const struct pci_device_id *dev_id
  4602. )
  4603. {
  4604. struct pmcraid_instance *pinstance;
  4605. struct Scsi_Host *host;
  4606. void __iomem *mapped_pci_addr;
  4607. int rc = PCIBIOS_SUCCESSFUL;
  4608. if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
  4609. pmcraid_err
  4610. ("maximum number(%d) of supported adapters reached\n",
  4611. atomic_read(&pmcraid_adapter_count));
  4612. return -ENOMEM;
  4613. }
  4614. atomic_inc(&pmcraid_adapter_count);
  4615. rc = pci_enable_device(pdev);
  4616. if (rc) {
  4617. dev_err(&pdev->dev, "Cannot enable adapter\n");
  4618. atomic_dec(&pmcraid_adapter_count);
  4619. return rc;
  4620. }
  4621. dev_info(&pdev->dev,
  4622. "Found new IOA(%x:%x), Total IOA count: %d\n",
  4623. pdev->vendor, pdev->device,
  4624. atomic_read(&pmcraid_adapter_count));
  4625. rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
  4626. if (rc < 0) {
  4627. dev_err(&pdev->dev,
  4628. "Couldn't register memory range of registers\n");
  4629. goto out_disable_device;
  4630. }
  4631. mapped_pci_addr = pci_iomap(pdev, 0, 0);
  4632. if (!mapped_pci_addr) {
  4633. dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
  4634. rc = -ENOMEM;
  4635. goto out_release_regions;
  4636. }
  4637. pci_set_master(pdev);
  4638. /* Firmware requires the system bus address of IOARCB to be within
  4639. * 32-bit addressable range though it has 64-bit IOARRIN register.
  4640. * However, firmware supports 64-bit streaming DMA buffers, whereas
  4641. * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
  4642. * returns memory within 4GB (if not, change this logic), coherent
  4643. * buffers are within firmware acceptible address ranges.
  4644. */
  4645. if ((sizeof(dma_addr_t) == 4) ||
  4646. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4647. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4648. /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
  4649. * bit mask for pci_alloc_consistent to return addresses within 4GB
  4650. */
  4651. if (rc == 0)
  4652. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4653. if (rc != 0) {
  4654. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  4655. goto cleanup_nomem;
  4656. }
  4657. host = scsi_host_alloc(&pmcraid_host_template,
  4658. sizeof(struct pmcraid_instance));
  4659. if (!host) {
  4660. dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
  4661. rc = -ENOMEM;
  4662. goto cleanup_nomem;
  4663. }
  4664. host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
  4665. host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
  4666. host->unique_id = host->host_no;
  4667. host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
  4668. host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
  4669. /* zero out entire instance structure */
  4670. pinstance = (struct pmcraid_instance *)host->hostdata;
  4671. memset(pinstance, 0, sizeof(*pinstance));
  4672. pinstance->chip_cfg =
  4673. (struct pmcraid_chip_details *)(dev_id->driver_data);
  4674. rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
  4675. if (rc < 0) {
  4676. dev_err(&pdev->dev, "failed to initialize adapter instance\n");
  4677. goto out_scsi_host_put;
  4678. }
  4679. pci_set_drvdata(pdev, pinstance);
  4680. /* Save PCI config-space for use following the reset */
  4681. rc = pci_save_state(pinstance->pdev);
  4682. if (rc != 0) {
  4683. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  4684. goto out_scsi_host_put;
  4685. }
  4686. pmcraid_disable_interrupts(pinstance, ~0);
  4687. rc = pmcraid_register_interrupt_handler(pinstance);
  4688. if (rc) {
  4689. dev_err(&pdev->dev, "couldn't register interrupt handler\n");
  4690. goto out_scsi_host_put;
  4691. }
  4692. pmcraid_init_tasklets(pinstance);
  4693. /* allocate verious buffers used by LLD.*/
  4694. rc = pmcraid_init_buffers(pinstance);
  4695. if (rc) {
  4696. pmcraid_err("couldn't allocate memory blocks\n");
  4697. goto out_unregister_isr;
  4698. }
  4699. /* check the reset type required */
  4700. pmcraid_reset_type(pinstance);
  4701. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4702. /* Start IOA firmware initialization and bring card to Operational
  4703. * state.
  4704. */
  4705. pmcraid_info("starting IOA initialization sequence\n");
  4706. if (pmcraid_reset_bringup(pinstance)) {
  4707. dev_err(&pdev->dev, "couldn't initialize IOA \n");
  4708. rc = 1;
  4709. goto out_release_bufs;
  4710. }
  4711. /* Add adapter instance into mid-layer list */
  4712. rc = scsi_add_host(pinstance->host, &pdev->dev);
  4713. if (rc != 0) {
  4714. pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
  4715. goto out_release_bufs;
  4716. }
  4717. scsi_scan_host(pinstance->host);
  4718. rc = pmcraid_setup_chrdev(pinstance);
  4719. if (rc != 0) {
  4720. pmcraid_err("couldn't create mgmt interface, error: %x\n",
  4721. rc);
  4722. goto out_remove_host;
  4723. }
  4724. /* Schedule worker thread to handle CCN and take care of adding and
  4725. * removing devices to OS
  4726. */
  4727. atomic_set(&pinstance->expose_resources, 1);
  4728. schedule_work(&pinstance->worker_q);
  4729. return rc;
  4730. out_remove_host:
  4731. scsi_remove_host(host);
  4732. out_release_bufs:
  4733. pmcraid_release_buffers(pinstance);
  4734. out_unregister_isr:
  4735. pmcraid_kill_tasklets(pinstance);
  4736. pmcraid_unregister_interrupt_handler(pinstance);
  4737. out_scsi_host_put:
  4738. scsi_host_put(host);
  4739. cleanup_nomem:
  4740. iounmap(mapped_pci_addr);
  4741. out_release_regions:
  4742. pci_release_regions(pdev);
  4743. out_disable_device:
  4744. atomic_dec(&pmcraid_adapter_count);
  4745. pci_set_drvdata(pdev, NULL);
  4746. pci_disable_device(pdev);
  4747. return -ENODEV;
  4748. }
  4749. /*
  4750. * PCI driver structure of pcmraid driver
  4751. */
  4752. static struct pci_driver pmcraid_driver = {
  4753. .name = PMCRAID_DRIVER_NAME,
  4754. .id_table = pmcraid_pci_table,
  4755. .probe = pmcraid_probe,
  4756. .remove = pmcraid_remove,
  4757. .suspend = pmcraid_suspend,
  4758. .resume = pmcraid_resume,
  4759. .shutdown = pmcraid_shutdown
  4760. };
  4761. /**
  4762. * pmcraid_init - module load entry point
  4763. */
  4764. static int __init pmcraid_init(void)
  4765. {
  4766. dev_t dev;
  4767. int error;
  4768. pmcraid_info("%s Device Driver version: %s %s\n",
  4769. PMCRAID_DRIVER_NAME,
  4770. PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
  4771. error = alloc_chrdev_region(&dev, 0,
  4772. PMCRAID_MAX_ADAPTERS,
  4773. PMCRAID_DEVFILE);
  4774. if (error) {
  4775. pmcraid_err("failed to get a major number for adapters\n");
  4776. goto out_init;
  4777. }
  4778. pmcraid_major = MAJOR(dev);
  4779. pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
  4780. if (IS_ERR(pmcraid_class)) {
  4781. error = PTR_ERR(pmcraid_class);
  4782. pmcraid_err("failed to register with with sysfs, error = %x\n",
  4783. error);
  4784. goto out_unreg_chrdev;
  4785. }
  4786. error = pmcraid_netlink_init();
  4787. if (error)
  4788. goto out_unreg_chrdev;
  4789. error = pci_register_driver(&pmcraid_driver);
  4790. if (error == 0)
  4791. goto out_init;
  4792. pmcraid_err("failed to register pmcraid driver, error = %x\n",
  4793. error);
  4794. class_destroy(pmcraid_class);
  4795. pmcraid_netlink_release();
  4796. out_unreg_chrdev:
  4797. unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
  4798. out_init:
  4799. return error;
  4800. }
  4801. /**
  4802. * pmcraid_exit - module unload entry point
  4803. */
  4804. static void __exit pmcraid_exit(void)
  4805. {
  4806. pmcraid_netlink_release();
  4807. class_destroy(pmcraid_class);
  4808. unregister_chrdev_region(MKDEV(pmcraid_major, 0),
  4809. PMCRAID_MAX_ADAPTERS);
  4810. pci_unregister_driver(&pmcraid_driver);
  4811. }
  4812. module_init(pmcraid_init);
  4813. module_exit(pmcraid_exit);