megaraid_mbox.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * FILE : megaraid_mbox.c
  13. * Version : v2.20.4.6 (Mar 07 2005)
  14. *
  15. * Authors:
  16. * Atul Mukker <Atul.Mukker@lsil.com>
  17. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
  18. * Manoj Jose <Manoj.Jose@lsil.com>
  19. *
  20. * List of supported controllers
  21. *
  22. * OEM Product Name VID DID SSVID SSID
  23. * --- ------------ --- --- ---- ----
  24. * Dell PERC3/QC 101E 1960 1028 0471
  25. * Dell PERC3/DC 101E 1960 1028 0493
  26. * Dell PERC3/SC 101E 1960 1028 0475
  27. * Dell PERC3/Di 1028 1960 1028 0123
  28. * Dell PERC4/SC 1000 1960 1028 0520
  29. * Dell PERC4/DC 1000 1960 1028 0518
  30. * Dell PERC4/QC 1000 0407 1028 0531
  31. * Dell PERC4/Di 1028 000F 1028 014A
  32. * Dell PERC 4e/Si 1028 0013 1028 016c
  33. * Dell PERC 4e/Di 1028 0013 1028 016d
  34. * Dell PERC 4e/Di 1028 0013 1028 016e
  35. * Dell PERC 4e/Di 1028 0013 1028 016f
  36. * Dell PERC 4e/Di 1028 0013 1028 0170
  37. * Dell PERC 4e/DC 1000 0408 1028 0002
  38. * Dell PERC 4e/SC 1000 0408 1028 0001
  39. *
  40. *
  41. * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
  42. * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
  43. * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
  44. * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
  45. * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
  46. * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
  47. * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
  48. * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
  49. * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
  50. * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
  51. * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
  52. * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
  53. *
  54. * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
  55. * INTEL RAID Controller SRCS16 1000 1960 8086 0523
  56. * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
  57. * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
  58. * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
  59. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
  60. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
  61. * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
  62. *
  63. * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
  64. *
  65. * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
  66. *
  67. * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
  68. *
  69. * For history of changes, see Documentation/ChangeLog.megaraid
  70. */
  71. #include "megaraid_mbox.h"
  72. static int megaraid_init(void);
  73. static void megaraid_exit(void);
  74. static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
  75. static void megaraid_detach_one(struct pci_dev *);
  76. static void megaraid_mbox_shutdown(struct device *);
  77. static int megaraid_io_attach(adapter_t *);
  78. static void megaraid_io_detach(adapter_t *);
  79. static int megaraid_init_mbox(adapter_t *);
  80. static void megaraid_fini_mbox(adapter_t *);
  81. static int megaraid_alloc_cmd_packets(adapter_t *);
  82. static void megaraid_free_cmd_packets(adapter_t *);
  83. static int megaraid_mbox_setup_dma_pools(adapter_t *);
  84. static void megaraid_mbox_teardown_dma_pools(adapter_t *);
  85. static int megaraid_sysfs_alloc_resources(adapter_t *);
  86. static void megaraid_sysfs_free_resources(adapter_t *);
  87. static int megaraid_abort_handler(struct scsi_cmnd *);
  88. static int megaraid_reset_handler(struct scsi_cmnd *);
  89. static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
  90. static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
  91. static int megaraid_busywait_mbox(mraid_device_t *);
  92. static int megaraid_mbox_product_info(adapter_t *);
  93. static int megaraid_mbox_extended_cdb(adapter_t *);
  94. static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
  95. static int megaraid_mbox_support_random_del(adapter_t *);
  96. static int megaraid_mbox_get_max_sg(adapter_t *);
  97. static void megaraid_mbox_enum_raid_scsi(adapter_t *);
  98. static void megaraid_mbox_flush_cache(adapter_t *);
  99. static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
  100. static void megaraid_mbox_setup_device_map(adapter_t *);
  101. static int megaraid_queue_command(struct scsi_cmnd *,
  102. void (*)(struct scsi_cmnd *));
  103. static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
  104. static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
  105. static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
  106. struct scsi_cmnd *);
  107. static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
  108. struct scsi_cmnd *);
  109. static irqreturn_t megaraid_isr(int, void *, struct pt_regs *);
  110. static void megaraid_mbox_dpc(unsigned long);
  111. static ssize_t megaraid_sysfs_show_app_hndl(struct class_device *, char *);
  112. static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
  113. static int megaraid_cmm_register(adapter_t *);
  114. static int megaraid_cmm_unregister(adapter_t *);
  115. static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
  116. static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
  117. static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
  118. static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
  119. static int wait_till_fw_empty(adapter_t *);
  120. MODULE_AUTHOR("LSI Logic Corporation");
  121. MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
  122. MODULE_LICENSE("GPL");
  123. MODULE_VERSION(MEGARAID_VERSION);
  124. /*
  125. * ### modules parameters for driver ###
  126. */
  127. /**
  128. * Set to enable driver to expose unconfigured disk to kernel
  129. */
  130. static int megaraid_expose_unconf_disks = 0;
  131. module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
  132. MODULE_PARM_DESC(unconf_disks,
  133. "Set to expose unconfigured disks to kernel (default=0)");
  134. /**
  135. * driver wait time if the adapter's mailbox is busy
  136. */
  137. static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  138. module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
  139. MODULE_PARM_DESC(busy_wait,
  140. "Max wait for mailbox in microseconds if busy (default=10)");
  141. /**
  142. * number of sectors per IO command
  143. */
  144. static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
  145. module_param_named(max_sectors, megaraid_max_sectors, int, 0);
  146. MODULE_PARM_DESC(max_sectors,
  147. "Maximum number of sectors per IO command (default=128)");
  148. /**
  149. * number of commands per logical unit
  150. */
  151. static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
  152. module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
  153. MODULE_PARM_DESC(cmd_per_lun,
  154. "Maximum number of commands per logical unit (default=64)");
  155. /**
  156. * Fast driver load option, skip scanning for physical devices during load.
  157. * This would result in non-disk devices being skipped during driver load
  158. * time. These can be later added though, using /proc/scsi/scsi
  159. */
  160. static unsigned int megaraid_fast_load = 0;
  161. module_param_named(fast_load, megaraid_fast_load, int, 0);
  162. MODULE_PARM_DESC(fast_load,
  163. "Faster loading of the driver, skips physical devices! (default=0)");
  164. /**
  165. * mraid_debug level - threshold for amount of information to be displayed by
  166. * the driver. This level can be changed through modules parameters, ioctl or
  167. * sysfs/proc interface. By default, print the announcement messages only.
  168. */
  169. int mraid_debug_level = CL_ANN;
  170. module_param_named(debug_level, mraid_debug_level, int, 0);
  171. MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
  172. /*
  173. * ### global data ###
  174. */
  175. static uint8_t megaraid_mbox_version[8] =
  176. { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
  177. /*
  178. * PCI table for all supported controllers.
  179. */
  180. static struct pci_device_id pci_id_table_g[] = {
  181. {
  182. PCI_VENDOR_ID_DELL,
  183. PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
  184. PCI_VENDOR_ID_DELL,
  185. PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
  186. },
  187. {
  188. PCI_VENDOR_ID_LSI_LOGIC,
  189. PCI_DEVICE_ID_PERC4_SC,
  190. PCI_VENDOR_ID_DELL,
  191. PCI_SUBSYS_ID_PERC4_SC,
  192. },
  193. {
  194. PCI_VENDOR_ID_LSI_LOGIC,
  195. PCI_DEVICE_ID_PERC4_DC,
  196. PCI_VENDOR_ID_DELL,
  197. PCI_SUBSYS_ID_PERC4_DC,
  198. },
  199. {
  200. PCI_VENDOR_ID_LSI_LOGIC,
  201. PCI_DEVICE_ID_VERDE,
  202. PCI_ANY_ID,
  203. PCI_ANY_ID,
  204. },
  205. {
  206. PCI_VENDOR_ID_DELL,
  207. PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
  208. PCI_VENDOR_ID_DELL,
  209. PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
  210. },
  211. {
  212. PCI_VENDOR_ID_DELL,
  213. PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
  214. PCI_VENDOR_ID_DELL,
  215. PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
  216. },
  217. {
  218. PCI_VENDOR_ID_DELL,
  219. PCI_DEVICE_ID_PERC4E_DI_KOBUK,
  220. PCI_VENDOR_ID_DELL,
  221. PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
  222. },
  223. {
  224. PCI_VENDOR_ID_DELL,
  225. PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
  226. PCI_VENDOR_ID_DELL,
  227. PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
  228. },
  229. {
  230. PCI_VENDOR_ID_DELL,
  231. PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
  232. PCI_VENDOR_ID_DELL,
  233. PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
  234. },
  235. {
  236. PCI_VENDOR_ID_DELL,
  237. PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
  238. PCI_VENDOR_ID_DELL,
  239. PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
  240. },
  241. {
  242. PCI_VENDOR_ID_LSI_LOGIC,
  243. PCI_DEVICE_ID_DOBSON,
  244. PCI_ANY_ID,
  245. PCI_ANY_ID,
  246. },
  247. {
  248. PCI_VENDOR_ID_AMI,
  249. PCI_DEVICE_ID_AMI_MEGARAID3,
  250. PCI_VENDOR_ID_DELL,
  251. PCI_SUBSYS_ID_PERC3_QC,
  252. },
  253. {
  254. PCI_VENDOR_ID_AMI,
  255. PCI_DEVICE_ID_AMI_MEGARAID3,
  256. PCI_VENDOR_ID_DELL,
  257. PCI_SUBSYS_ID_PERC3_DC,
  258. },
  259. {
  260. PCI_VENDOR_ID_AMI,
  261. PCI_DEVICE_ID_AMI_MEGARAID3,
  262. PCI_VENDOR_ID_DELL,
  263. PCI_SUBSYS_ID_PERC3_SC,
  264. },
  265. {
  266. PCI_VENDOR_ID_AMI,
  267. PCI_DEVICE_ID_AMI_MEGARAID3,
  268. PCI_VENDOR_ID_AMI,
  269. PCI_SUBSYS_ID_PERC3_SC,
  270. },
  271. {
  272. PCI_VENDOR_ID_AMI,
  273. PCI_DEVICE_ID_AMI_MEGARAID3,
  274. PCI_VENDOR_ID_AMI,
  275. PCI_SUBSYS_ID_PERC3_DC,
  276. },
  277. {
  278. PCI_VENDOR_ID_LSI_LOGIC,
  279. PCI_DEVICE_ID_MEGARAID_SCSI_320_0,
  280. PCI_VENDOR_ID_LSI_LOGIC,
  281. PCI_SUBSYS_ID_MEGARAID_SCSI_320_0,
  282. },
  283. {
  284. PCI_VENDOR_ID_LSI_LOGIC,
  285. PCI_DEVICE_ID_MEGARAID_SCSI_320_1,
  286. PCI_VENDOR_ID_LSI_LOGIC,
  287. PCI_SUBSYS_ID_MEGARAID_SCSI_320_1,
  288. },
  289. {
  290. PCI_VENDOR_ID_LSI_LOGIC,
  291. PCI_DEVICE_ID_MEGARAID_SCSI_320_2,
  292. PCI_VENDOR_ID_LSI_LOGIC,
  293. PCI_SUBSYS_ID_MEGARAID_SCSI_320_2,
  294. },
  295. {
  296. PCI_VENDOR_ID_LSI_LOGIC,
  297. PCI_DEVICE_ID_MEGARAID_I4_133_RAID,
  298. PCI_VENDOR_ID_LSI_LOGIC,
  299. PCI_SUBSYS_ID_MEGARAID_I4_133_RAID,
  300. },
  301. {
  302. PCI_VENDOR_ID_LSI_LOGIC,
  303. PCI_DEVICE_ID_MEGARAID_SATA_150_4,
  304. PCI_VENDOR_ID_LSI_LOGIC,
  305. PCI_SUBSYS_ID_MEGARAID_SATA_150_4,
  306. },
  307. {
  308. PCI_VENDOR_ID_LSI_LOGIC,
  309. PCI_DEVICE_ID_MEGARAID_SATA_150_6,
  310. PCI_VENDOR_ID_LSI_LOGIC,
  311. PCI_SUBSYS_ID_MEGARAID_SATA_150_6,
  312. },
  313. {
  314. PCI_VENDOR_ID_LSI_LOGIC,
  315. PCI_DEVICE_ID_LINDSAY,
  316. PCI_ANY_ID,
  317. PCI_ANY_ID,
  318. },
  319. {
  320. PCI_VENDOR_ID_LSI_LOGIC,
  321. PCI_DEVICE_ID_INTEL_RAID_SRCS16,
  322. PCI_VENDOR_ID_INTEL,
  323. PCI_SUBSYS_ID_INTEL_RAID_SRCS16,
  324. },
  325. {
  326. PCI_VENDOR_ID_LSI_LOGIC,
  327. PCI_DEVICE_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
  328. PCI_VENDOR_ID_INTEL,
  329. PCI_SUBSYS_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
  330. },
  331. {0} /* Terminating entry */
  332. };
  333. MODULE_DEVICE_TABLE(pci, pci_id_table_g);
  334. static struct pci_driver megaraid_pci_driver_g = {
  335. .name = "megaraid",
  336. .id_table = pci_id_table_g,
  337. .probe = megaraid_probe_one,
  338. .remove = __devexit_p(megaraid_detach_one),
  339. .driver = {
  340. .shutdown = megaraid_mbox_shutdown,
  341. }
  342. };
  343. // definitions for the device attributes for exporting logical drive number
  344. // for a scsi address (Host, Channel, Id, Lun)
  345. CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
  346. NULL);
  347. // Host template initializer for megaraid mbox sysfs device attributes
  348. static struct class_device_attribute *megaraid_shost_attrs[] = {
  349. &class_device_attr_megaraid_mbox_app_hndl,
  350. NULL,
  351. };
  352. DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
  353. // Host template initializer for megaraid mbox sysfs device attributes
  354. static struct device_attribute *megaraid_sdev_attrs[] = {
  355. &dev_attr_megaraid_mbox_ld,
  356. NULL,
  357. };
  358. /*
  359. * Scsi host template for megaraid unified driver
  360. */
  361. static struct scsi_host_template megaraid_template_g = {
  362. .module = THIS_MODULE,
  363. .name = "LSI Logic MegaRAID driver",
  364. .proc_name = "megaraid",
  365. .queuecommand = megaraid_queue_command,
  366. .eh_abort_handler = megaraid_abort_handler,
  367. .eh_device_reset_handler = megaraid_reset_handler,
  368. .eh_bus_reset_handler = megaraid_reset_handler,
  369. .eh_host_reset_handler = megaraid_reset_handler,
  370. .use_clustering = ENABLE_CLUSTERING,
  371. .sdev_attrs = megaraid_sdev_attrs,
  372. .shost_attrs = megaraid_shost_attrs,
  373. };
  374. /**
  375. * megaraid_init - module load hook
  376. *
  377. * We register ourselves as hotplug enabled module and let PCI subsystem
  378. * discover our adaters
  379. **/
  380. static int __init
  381. megaraid_init(void)
  382. {
  383. int rval;
  384. // Announce the driver version
  385. con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
  386. MEGARAID_EXT_VERSION));
  387. // check validity of module parameters
  388. if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
  389. con_log(CL_ANN, (KERN_WARNING
  390. "megaraid mailbox: max commands per lun reset to %d\n",
  391. MBOX_MAX_SCSI_CMDS));
  392. megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
  393. }
  394. // register as a PCI hot-plug driver module
  395. rval = pci_register_driver(&megaraid_pci_driver_g);
  396. if (rval < 0) {
  397. con_log(CL_ANN, (KERN_WARNING
  398. "megaraid: could not register hotplug support.\n"));
  399. }
  400. return rval;
  401. }
  402. /**
  403. * megaraid_exit - driver unload entry point
  404. *
  405. * We simply unwrap the megaraid_init routine here
  406. */
  407. static void __exit
  408. megaraid_exit(void)
  409. {
  410. con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
  411. // unregister as PCI hotplug driver
  412. pci_unregister_driver(&megaraid_pci_driver_g);
  413. return;
  414. }
  415. /**
  416. * megaraid_probe_one - PCI hotplug entry point
  417. * @param pdev : handle to this controller's PCI configuration space
  418. * @param id : pci device id of the class of controllers
  419. *
  420. * This routine should be called whenever a new adapter is detected by the
  421. * PCI hotplug susbsytem.
  422. **/
  423. static int __devinit
  424. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  425. {
  426. adapter_t *adapter;
  427. // detected a new controller
  428. con_log(CL_ANN, (KERN_INFO
  429. "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  430. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  431. pdev->subsystem_device));
  432. con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
  433. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
  434. if (pci_enable_device(pdev)) {
  435. con_log(CL_ANN, (KERN_WARNING
  436. "megaraid: pci_enable_device failed\n"));
  437. return -ENODEV;
  438. }
  439. // Enable bus-mastering on this controller
  440. pci_set_master(pdev);
  441. // Allocate the per driver initialization structure
  442. adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
  443. if (adapter == NULL) {
  444. con_log(CL_ANN, (KERN_WARNING
  445. "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
  446. goto out_probe_one;
  447. }
  448. memset(adapter, 0, sizeof(adapter_t));
  449. // set up PCI related soft state and other pre-known parameters
  450. adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
  451. adapter->irq = pdev->irq;
  452. adapter->pdev = pdev;
  453. atomic_set(&adapter->being_detached, 0);
  454. // Setup the default DMA mask. This would be changed later on
  455. // depending on hardware capabilities
  456. if (pci_set_dma_mask(adapter->pdev, DMA_32BIT_MASK) != 0) {
  457. con_log(CL_ANN, (KERN_WARNING
  458. "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
  459. goto out_free_adapter;
  460. }
  461. // Initialize the synchronization lock for kernel and LLD
  462. spin_lock_init(&adapter->lock);
  463. adapter->host_lock = &adapter->lock;
  464. // Initialize the command queues: the list of free SCBs and the list
  465. // of pending SCBs.
  466. INIT_LIST_HEAD(&adapter->kscb_pool);
  467. spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
  468. INIT_LIST_HEAD(&adapter->pend_list);
  469. spin_lock_init(PENDING_LIST_LOCK(adapter));
  470. INIT_LIST_HEAD(&adapter->completed_list);
  471. spin_lock_init(COMPLETED_LIST_LOCK(adapter));
  472. // Start the mailbox based controller
  473. if (megaraid_init_mbox(adapter) != 0) {
  474. con_log(CL_ANN, (KERN_WARNING
  475. "megaraid: maibox adapter did not initialize\n"));
  476. goto out_free_adapter;
  477. }
  478. // Register with LSI Common Management Module
  479. if (megaraid_cmm_register(adapter) != 0) {
  480. con_log(CL_ANN, (KERN_WARNING
  481. "megaraid: could not register with management module\n"));
  482. goto out_fini_mbox;
  483. }
  484. // setup adapter handle in PCI soft state
  485. pci_set_drvdata(pdev, adapter);
  486. // attach with scsi mid-layer
  487. if (megaraid_io_attach(adapter) != 0) {
  488. con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
  489. goto out_cmm_unreg;
  490. }
  491. return 0;
  492. out_cmm_unreg:
  493. pci_set_drvdata(pdev, NULL);
  494. megaraid_cmm_unregister(adapter);
  495. out_fini_mbox:
  496. megaraid_fini_mbox(adapter);
  497. out_free_adapter:
  498. kfree(adapter);
  499. out_probe_one:
  500. pci_disable_device(pdev);
  501. return -ENODEV;
  502. }
  503. /**
  504. * megaraid_detach_one - release the framework resources and call LLD release
  505. * routine
  506. * @param pdev : handle for our PCI cofiguration space
  507. *
  508. * This routine is called during driver unload. We free all the allocated
  509. * resources and call the corresponding LLD so that it can also release all
  510. * its resources.
  511. *
  512. * This routine is also called from the PCI hotplug system
  513. **/
  514. static void
  515. megaraid_detach_one(struct pci_dev *pdev)
  516. {
  517. adapter_t *adapter;
  518. struct Scsi_Host *host;
  519. // Start a rollback on this adapter
  520. adapter = pci_get_drvdata(pdev);
  521. if (!adapter) {
  522. con_log(CL_ANN, (KERN_CRIT
  523. "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  524. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  525. pdev->subsystem_device));
  526. return;
  527. }
  528. else {
  529. con_log(CL_ANN, (KERN_NOTICE
  530. "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  531. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  532. pdev->subsystem_device));
  533. }
  534. host = adapter->host;
  535. // do not allow any more requests from the management module for this
  536. // adapter.
  537. // FIXME: How do we account for the request which might still be
  538. // pending with us?
  539. atomic_set(&adapter->being_detached, 1);
  540. // detach from the IO sub-system
  541. megaraid_io_detach(adapter);
  542. // reset the device state in the PCI structure. We check this
  543. // condition when we enter here. If the device state is NULL,
  544. // that would mean the device has already been removed
  545. pci_set_drvdata(pdev, NULL);
  546. // Unregister from common management module
  547. //
  548. // FIXME: this must return success or failure for conditions if there
  549. // is a command pending with LLD or not.
  550. megaraid_cmm_unregister(adapter);
  551. // finalize the mailbox based controller and release all resources
  552. megaraid_fini_mbox(adapter);
  553. kfree(adapter);
  554. scsi_host_put(host);
  555. pci_disable_device(pdev);
  556. return;
  557. }
  558. /**
  559. * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
  560. * @param device : generice driver model device
  561. *
  562. * Shutdown notification, perform flush cache
  563. */
  564. static void
  565. megaraid_mbox_shutdown(struct device *device)
  566. {
  567. adapter_t *adapter = pci_get_drvdata(to_pci_dev(device));
  568. static int counter;
  569. if (!adapter) {
  570. con_log(CL_ANN, (KERN_WARNING
  571. "megaraid: null device in shutdown\n"));
  572. return;
  573. }
  574. // flush caches now
  575. con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
  576. counter++));
  577. megaraid_mbox_flush_cache(adapter);
  578. con_log(CL_ANN, ("done\n"));
  579. }
  580. /**
  581. * megaraid_io_attach - attach a device with the IO subsystem
  582. * @param adapter : controller's soft state
  583. *
  584. * Attach this device with the IO subsystem
  585. **/
  586. static int
  587. megaraid_io_attach(adapter_t *adapter)
  588. {
  589. struct Scsi_Host *host;
  590. // Initialize SCSI Host structure
  591. host = scsi_host_alloc(&megaraid_template_g, 8);
  592. if (!host) {
  593. con_log(CL_ANN, (KERN_WARNING
  594. "megaraid mbox: scsi_register failed\n"));
  595. return -1;
  596. }
  597. SCSIHOST2ADAP(host) = (caddr_t)adapter;
  598. adapter->host = host;
  599. // export the parameters required by the mid-layer
  600. scsi_assign_lock(host, adapter->host_lock);
  601. host->irq = adapter->irq;
  602. host->unique_id = adapter->unique_id;
  603. host->can_queue = adapter->max_cmds;
  604. host->this_id = adapter->init_id;
  605. host->sg_tablesize = adapter->sglen;
  606. host->max_sectors = adapter->max_sectors;
  607. host->cmd_per_lun = adapter->cmd_per_lun;
  608. host->max_channel = adapter->max_channel;
  609. host->max_id = adapter->max_target;
  610. host->max_lun = adapter->max_lun;
  611. // notify mid-layer about the new controller
  612. if (scsi_add_host(host, &adapter->pdev->dev)) {
  613. con_log(CL_ANN, (KERN_WARNING
  614. "megaraid mbox: scsi_add_host failed\n"));
  615. scsi_host_put(host);
  616. return -1;
  617. }
  618. scsi_scan_host(host);
  619. return 0;
  620. }
  621. /**
  622. * megaraid_io_detach - detach a device from the IO subsystem
  623. * @param adapter : controller's soft state
  624. *
  625. * Detach this device from the IO subsystem
  626. **/
  627. static void
  628. megaraid_io_detach(adapter_t *adapter)
  629. {
  630. struct Scsi_Host *host;
  631. con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
  632. host = adapter->host;
  633. scsi_remove_host(host);
  634. return;
  635. }
  636. /*
  637. * START: Mailbox Low Level Driver
  638. *
  639. * This is section specific to the single mailbox based controllers
  640. */
  641. /**
  642. * megaraid_init_mbox - initialize controller
  643. * @param adapter - our soft state
  644. *
  645. * . Allocate 16-byte aligned mailbox memory for firmware handshake
  646. * . Allocate controller's memory resources
  647. * . Find out all initialization data
  648. * . Allocate memory required for all the commands
  649. * . Use internal library of FW routines, build up complete soft state
  650. */
  651. static int __init
  652. megaraid_init_mbox(adapter_t *adapter)
  653. {
  654. struct pci_dev *pdev;
  655. mraid_device_t *raid_dev;
  656. int i;
  657. adapter->ito = MBOX_TIMEOUT;
  658. pdev = adapter->pdev;
  659. /*
  660. * Allocate and initialize the init data structure for mailbox
  661. * controllers
  662. */
  663. raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
  664. if (raid_dev == NULL) return -1;
  665. memset(raid_dev, 0, sizeof(mraid_device_t));
  666. /*
  667. * Attach the adapter soft state to raid device soft state
  668. */
  669. adapter->raid_device = (caddr_t)raid_dev;
  670. raid_dev->fast_load = megaraid_fast_load;
  671. // our baseport
  672. raid_dev->baseport = pci_resource_start(pdev, 0);
  673. if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
  674. con_log(CL_ANN, (KERN_WARNING
  675. "megaraid: mem region busy\n"));
  676. goto out_free_raid_dev;
  677. }
  678. raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
  679. if (!raid_dev->baseaddr) {
  680. con_log(CL_ANN, (KERN_WARNING
  681. "megaraid: could not map hba memory\n") );
  682. goto out_release_regions;
  683. }
  684. //
  685. // Setup the rest of the soft state using the library of FW routines
  686. //
  687. // request IRQ and register the interrupt service routine
  688. if (request_irq(adapter->irq, megaraid_isr, SA_SHIRQ, "megaraid",
  689. adapter)) {
  690. con_log(CL_ANN, (KERN_WARNING
  691. "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
  692. goto out_iounmap;
  693. }
  694. // initialize the mutual exclusion lock for the mailbox
  695. spin_lock_init(&raid_dev->mailbox_lock);
  696. // allocate memory required for commands
  697. if (megaraid_alloc_cmd_packets(adapter) != 0) {
  698. goto out_free_irq;
  699. }
  700. // Product info
  701. if (megaraid_mbox_product_info(adapter) != 0) {
  702. goto out_alloc_cmds;
  703. }
  704. // Do we support extended CDBs
  705. adapter->max_cdb_sz = 10;
  706. if (megaraid_mbox_extended_cdb(adapter) == 0) {
  707. adapter->max_cdb_sz = 16;
  708. }
  709. /*
  710. * Do we support cluster environment, if we do, what is the initiator
  711. * id.
  712. * NOTE: In a non-cluster aware firmware environment, the LLD should
  713. * return 7 as initiator id.
  714. */
  715. adapter->ha = 0;
  716. adapter->init_id = -1;
  717. if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
  718. adapter->ha = 1;
  719. }
  720. /*
  721. * Prepare the device ids array to have the mapping between the kernel
  722. * device address and megaraid device address.
  723. * We export the physical devices on their actual addresses. The
  724. * logical drives are exported on a virtual SCSI channel
  725. */
  726. megaraid_mbox_setup_device_map(adapter);
  727. // If the firmware supports random deletion, update the device id map
  728. if (megaraid_mbox_support_random_del(adapter)) {
  729. // Change the logical drives numbers in device_ids array one
  730. // slot in device_ids is reserved for target id, that's why
  731. // "<=" below
  732. for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
  733. adapter->device_ids[adapter->max_channel][i] += 0x80;
  734. }
  735. adapter->device_ids[adapter->max_channel][adapter->init_id] =
  736. 0xFF;
  737. raid_dev->random_del_supported = 1;
  738. }
  739. /*
  740. * find out the maximum number of scatter-gather elements supported by
  741. * this firmware
  742. */
  743. adapter->sglen = megaraid_mbox_get_max_sg(adapter);
  744. // enumerate RAID and SCSI channels so that all devices on SCSI
  745. // channels can later be exported, including disk devices
  746. megaraid_mbox_enum_raid_scsi(adapter);
  747. /*
  748. * Other parameters required by upper layer
  749. *
  750. * maximum number of sectors per IO command
  751. */
  752. adapter->max_sectors = megaraid_max_sectors;
  753. /*
  754. * number of queued commands per LUN.
  755. */
  756. adapter->cmd_per_lun = megaraid_cmd_per_lun;
  757. /*
  758. * Allocate resources required to issue FW calls, when sysfs is
  759. * accessed
  760. */
  761. if (megaraid_sysfs_alloc_resources(adapter) != 0) {
  762. goto out_alloc_cmds;
  763. }
  764. // Set the DMA mask to 64-bit. All supported controllers as capable of
  765. // DMA in this range
  766. if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK) != 0) {
  767. con_log(CL_ANN, (KERN_WARNING
  768. "megaraid: could not set DMA mask for 64-bit.\n"));
  769. goto out_free_sysfs_res;
  770. }
  771. // setup tasklet for DPC
  772. tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
  773. (unsigned long)adapter);
  774. con_log(CL_DLEVEL1, (KERN_INFO
  775. "megaraid mbox hba successfully initialized\n"));
  776. return 0;
  777. out_free_sysfs_res:
  778. megaraid_sysfs_free_resources(adapter);
  779. out_alloc_cmds:
  780. megaraid_free_cmd_packets(adapter);
  781. out_free_irq:
  782. free_irq(adapter->irq, adapter);
  783. out_iounmap:
  784. iounmap(raid_dev->baseaddr);
  785. out_release_regions:
  786. pci_release_regions(pdev);
  787. out_free_raid_dev:
  788. kfree(raid_dev);
  789. return -1;
  790. }
  791. /**
  792. * megaraid_fini_mbox - undo controller initialization
  793. * @param adapter : our soft state
  794. */
  795. static void
  796. megaraid_fini_mbox(adapter_t *adapter)
  797. {
  798. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  799. // flush all caches
  800. megaraid_mbox_flush_cache(adapter);
  801. tasklet_kill(&adapter->dpc_h);
  802. megaraid_sysfs_free_resources(adapter);
  803. megaraid_free_cmd_packets(adapter);
  804. free_irq(adapter->irq, adapter);
  805. iounmap(raid_dev->baseaddr);
  806. pci_release_regions(adapter->pdev);
  807. kfree(raid_dev);
  808. return;
  809. }
  810. /**
  811. * megaraid_alloc_cmd_packets - allocate shared mailbox
  812. * @param adapter : soft state of the raid controller
  813. *
  814. * Allocate and align the shared mailbox. This maibox is used to issue
  815. * all the commands. For IO based controllers, the mailbox is also regsitered
  816. * with the FW. Allocate memory for all commands as well.
  817. * This is our big allocator
  818. */
  819. static int
  820. megaraid_alloc_cmd_packets(adapter_t *adapter)
  821. {
  822. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  823. struct pci_dev *pdev;
  824. unsigned long align;
  825. scb_t *scb;
  826. mbox_ccb_t *ccb;
  827. struct mraid_pci_blk *epthru_pci_blk;
  828. struct mraid_pci_blk *sg_pci_blk;
  829. struct mraid_pci_blk *mbox_pci_blk;
  830. int i;
  831. pdev = adapter->pdev;
  832. /*
  833. * Setup the mailbox
  834. * Allocate the common 16-byte aligned memory for the handshake
  835. * mailbox.
  836. */
  837. raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
  838. sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
  839. if (!raid_dev->una_mbox64) {
  840. con_log(CL_ANN, (KERN_WARNING
  841. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  842. __LINE__));
  843. return -1;
  844. }
  845. memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
  846. /*
  847. * Align the mailbox at 16-byte boundary
  848. */
  849. raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
  850. raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
  851. (~0UL ^ 0xFUL));
  852. raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
  853. align = ((void *)raid_dev->mbox -
  854. ((void *)&raid_dev->una_mbox64->mbox32));
  855. raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
  856. align;
  857. // Allocate memory for commands issued internally
  858. adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
  859. &adapter->ibuf_dma_h);
  860. if (!adapter->ibuf) {
  861. con_log(CL_ANN, (KERN_WARNING
  862. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  863. __LINE__));
  864. goto out_free_common_mbox;
  865. }
  866. memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
  867. // Allocate memory for our SCSI Command Blocks and their associated
  868. // memory
  869. /*
  870. * Allocate memory for the base list of scb. Later allocate memory for
  871. * CCBs and embedded components of each CCB and point the pointers in
  872. * scb to the allocated components
  873. * NOTE: The code to allocate SCB will be duplicated in all the LLD
  874. * since the calling routine does not yet know the number of available
  875. * commands.
  876. */
  877. adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
  878. GFP_KERNEL);
  879. if (adapter->kscb_list == NULL) {
  880. con_log(CL_ANN, (KERN_WARNING
  881. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  882. __LINE__));
  883. goto out_free_ibuf;
  884. }
  885. memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
  886. // memory allocation for our command packets
  887. if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
  888. con_log(CL_ANN, (KERN_WARNING
  889. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  890. __LINE__));
  891. goto out_free_scb_list;
  892. }
  893. // Adjust the scb pointers and link in the free pool
  894. epthru_pci_blk = raid_dev->epthru_pool;
  895. sg_pci_blk = raid_dev->sg_pool;
  896. mbox_pci_blk = raid_dev->mbox_pool;
  897. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  898. scb = adapter->kscb_list + i;
  899. ccb = raid_dev->ccb_list + i;
  900. ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
  901. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  902. ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
  903. ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
  904. // make sure the mailbox is aligned properly
  905. if (ccb->mbox_dma_h & 0x0F) {
  906. con_log(CL_ANN, (KERN_CRIT
  907. "megaraid mbox: not aligned on 16-bytes\n"));
  908. goto out_teardown_dma_pools;
  909. }
  910. ccb->epthru = (mraid_epassthru_t *)
  911. epthru_pci_blk[i].vaddr;
  912. ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
  913. ccb->pthru = (mraid_passthru_t *)ccb->epthru;
  914. ccb->pthru_dma_h = ccb->epthru_dma_h;
  915. ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
  916. ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
  917. ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
  918. scb->ccb = (caddr_t)ccb;
  919. scb->gp = 0;
  920. scb->sno = i; // command index
  921. scb->scp = NULL;
  922. scb->state = SCB_FREE;
  923. scb->dma_direction = PCI_DMA_NONE;
  924. scb->dma_type = MRAID_DMA_NONE;
  925. scb->dev_channel = -1;
  926. scb->dev_target = -1;
  927. // put scb in the free pool
  928. list_add_tail(&scb->list, &adapter->kscb_pool);
  929. }
  930. return 0;
  931. out_teardown_dma_pools:
  932. megaraid_mbox_teardown_dma_pools(adapter);
  933. out_free_scb_list:
  934. kfree(adapter->kscb_list);
  935. out_free_ibuf:
  936. pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
  937. adapter->ibuf_dma_h);
  938. out_free_common_mbox:
  939. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  940. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  941. return -1;
  942. }
  943. /**
  944. * megaraid_free_cmd_packets - free memory
  945. * @param adapter : soft state of the raid controller
  946. *
  947. * Release memory resources allocated for commands
  948. */
  949. static void
  950. megaraid_free_cmd_packets(adapter_t *adapter)
  951. {
  952. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  953. megaraid_mbox_teardown_dma_pools(adapter);
  954. kfree(adapter->kscb_list);
  955. pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
  956. (void *)adapter->ibuf, adapter->ibuf_dma_h);
  957. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  958. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  959. return;
  960. }
  961. /**
  962. * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
  963. * @param adapter : HBA soft state
  964. *
  965. * setup the dma pools for mailbox, passthru and extended passthru structures,
  966. * and scatter-gather lists
  967. */
  968. static int
  969. megaraid_mbox_setup_dma_pools(adapter_t *adapter)
  970. {
  971. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  972. struct mraid_pci_blk *epthru_pci_blk;
  973. struct mraid_pci_blk *sg_pci_blk;
  974. struct mraid_pci_blk *mbox_pci_blk;
  975. int i;
  976. // Allocate memory for 16-bytes aligned mailboxes
  977. raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
  978. adapter->pdev,
  979. sizeof(mbox64_t) + 16,
  980. 16, 0);
  981. if (raid_dev->mbox_pool_handle == NULL) {
  982. goto fail_setup_dma_pool;
  983. }
  984. mbox_pci_blk = raid_dev->mbox_pool;
  985. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  986. mbox_pci_blk[i].vaddr = pci_pool_alloc(
  987. raid_dev->mbox_pool_handle,
  988. GFP_KERNEL,
  989. &mbox_pci_blk[i].dma_addr);
  990. if (!mbox_pci_blk[i].vaddr) {
  991. goto fail_setup_dma_pool;
  992. }
  993. }
  994. /*
  995. * Allocate memory for each embedded passthru strucuture pointer
  996. * Request for a 128 bytes aligned structure for each passthru command
  997. * structure
  998. * Since passthru and extended passthru commands are exclusive, they
  999. * share common memory pool. Passthru structures piggyback on memory
  1000. * allocted to extended passthru since passthru is smaller of the two
  1001. */
  1002. raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
  1003. adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
  1004. if (raid_dev->epthru_pool_handle == NULL) {
  1005. goto fail_setup_dma_pool;
  1006. }
  1007. epthru_pci_blk = raid_dev->epthru_pool;
  1008. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  1009. epthru_pci_blk[i].vaddr = pci_pool_alloc(
  1010. raid_dev->epthru_pool_handle,
  1011. GFP_KERNEL,
  1012. &epthru_pci_blk[i].dma_addr);
  1013. if (!epthru_pci_blk[i].vaddr) {
  1014. goto fail_setup_dma_pool;
  1015. }
  1016. }
  1017. // Allocate memory for each scatter-gather list. Request for 512 bytes
  1018. // alignment for each sg list
  1019. raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
  1020. adapter->pdev,
  1021. sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
  1022. 512, 0);
  1023. if (raid_dev->sg_pool_handle == NULL) {
  1024. goto fail_setup_dma_pool;
  1025. }
  1026. sg_pci_blk = raid_dev->sg_pool;
  1027. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  1028. sg_pci_blk[i].vaddr = pci_pool_alloc(
  1029. raid_dev->sg_pool_handle,
  1030. GFP_KERNEL,
  1031. &sg_pci_blk[i].dma_addr);
  1032. if (!sg_pci_blk[i].vaddr) {
  1033. goto fail_setup_dma_pool;
  1034. }
  1035. }
  1036. return 0;
  1037. fail_setup_dma_pool:
  1038. megaraid_mbox_teardown_dma_pools(adapter);
  1039. return -1;
  1040. }
  1041. /**
  1042. * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
  1043. * @param adapter : HBA soft state
  1044. *
  1045. * teardown the dma pool for mailbox, passthru and extended passthru
  1046. * structures, and scatter-gather lists
  1047. */
  1048. static void
  1049. megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
  1050. {
  1051. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1052. struct mraid_pci_blk *epthru_pci_blk;
  1053. struct mraid_pci_blk *sg_pci_blk;
  1054. struct mraid_pci_blk *mbox_pci_blk;
  1055. int i;
  1056. sg_pci_blk = raid_dev->sg_pool;
  1057. for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
  1058. pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
  1059. sg_pci_blk[i].dma_addr);
  1060. }
  1061. if (raid_dev->sg_pool_handle)
  1062. pci_pool_destroy(raid_dev->sg_pool_handle);
  1063. epthru_pci_blk = raid_dev->epthru_pool;
  1064. for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
  1065. pci_pool_free(raid_dev->epthru_pool_handle,
  1066. epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
  1067. }
  1068. if (raid_dev->epthru_pool_handle)
  1069. pci_pool_destroy(raid_dev->epthru_pool_handle);
  1070. mbox_pci_blk = raid_dev->mbox_pool;
  1071. for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
  1072. pci_pool_free(raid_dev->mbox_pool_handle,
  1073. mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
  1074. }
  1075. if (raid_dev->mbox_pool_handle)
  1076. pci_pool_destroy(raid_dev->mbox_pool_handle);
  1077. return;
  1078. }
  1079. /**
  1080. * megaraid_alloc_scb - detach and return a scb from the free list
  1081. * @adapter : controller's soft state
  1082. *
  1083. * return the scb from the head of the free list. NULL if there are none
  1084. * available
  1085. **/
  1086. static inline scb_t *
  1087. megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
  1088. {
  1089. struct list_head *head = &adapter->kscb_pool;
  1090. scb_t *scb = NULL;
  1091. unsigned long flags;
  1092. // detach scb from free pool
  1093. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1094. if (list_empty(head)) {
  1095. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1096. return NULL;
  1097. }
  1098. scb = list_entry(head->next, scb_t, list);
  1099. list_del_init(&scb->list);
  1100. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1101. scb->state = SCB_ACTIVE;
  1102. scb->scp = scp;
  1103. scb->dma_type = MRAID_DMA_NONE;
  1104. return scb;
  1105. }
  1106. /**
  1107. * megaraid_dealloc_scb - return the scb to the free pool
  1108. * @adapter : controller's soft state
  1109. * @scb : scb to be freed
  1110. *
  1111. * return the scb back to the free list of scbs. The caller must 'flush' the
  1112. * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
  1113. * NOTE NOTE: Make sure the scb is not on any list before calling this
  1114. * routine.
  1115. **/
  1116. static inline void
  1117. megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
  1118. {
  1119. unsigned long flags;
  1120. // put scb in the free pool
  1121. scb->state = SCB_FREE;
  1122. scb->scp = NULL;
  1123. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1124. list_add(&scb->list, &adapter->kscb_pool);
  1125. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1126. return;
  1127. }
  1128. /**
  1129. * megaraid_mbox_mksgl - make the scatter-gather list
  1130. * @adapter - controller's soft state
  1131. * @scb - scsi control block
  1132. *
  1133. * prepare the scatter-gather list
  1134. */
  1135. static inline int
  1136. megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
  1137. {
  1138. struct scatterlist *sgl;
  1139. mbox_ccb_t *ccb;
  1140. struct page *page;
  1141. unsigned long offset;
  1142. struct scsi_cmnd *scp;
  1143. int sgcnt;
  1144. int i;
  1145. scp = scb->scp;
  1146. ccb = (mbox_ccb_t *)scb->ccb;
  1147. // no mapping required if no data to be transferred
  1148. if (!scp->request_buffer || !scp->request_bufflen)
  1149. return 0;
  1150. if (!scp->use_sg) { /* scatter-gather list not used */
  1151. page = virt_to_page(scp->request_buffer);
  1152. offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
  1153. ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
  1154. scp->request_bufflen,
  1155. scb->dma_direction);
  1156. scb->dma_type = MRAID_DMA_WBUF;
  1157. /*
  1158. * We need to handle special 64-bit commands that need a
  1159. * minimum of 1 SG
  1160. */
  1161. sgcnt = 1;
  1162. ccb->sgl64[0].address = ccb->buf_dma_h;
  1163. ccb->sgl64[0].length = scp->request_bufflen;
  1164. return sgcnt;
  1165. }
  1166. sgl = (struct scatterlist *)scp->request_buffer;
  1167. // The number of sg elements returned must not exceed our limit
  1168. sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
  1169. scb->dma_direction);
  1170. if (sgcnt > adapter->sglen) {
  1171. con_log(CL_ANN, (KERN_CRIT
  1172. "megaraid critical: too many sg elements:%d\n",
  1173. sgcnt));
  1174. BUG();
  1175. }
  1176. scb->dma_type = MRAID_DMA_WSG;
  1177. for (i = 0; i < sgcnt; i++, sgl++) {
  1178. ccb->sgl64[i].address = sg_dma_address(sgl);
  1179. ccb->sgl64[i].length = sg_dma_len(sgl);
  1180. }
  1181. // Return count of SG nodes
  1182. return sgcnt;
  1183. }
  1184. /**
  1185. * mbox_post_cmd - issue a mailbox command
  1186. * @adapter - controller's soft state
  1187. * @scb - command to be issued
  1188. *
  1189. * post the command to the controller if mailbox is availble.
  1190. */
  1191. static inline int
  1192. mbox_post_cmd(adapter_t *adapter, scb_t *scb)
  1193. {
  1194. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1195. mbox64_t *mbox64;
  1196. mbox_t *mbox;
  1197. mbox_ccb_t *ccb;
  1198. unsigned long flags;
  1199. unsigned int i = 0;
  1200. ccb = (mbox_ccb_t *)scb->ccb;
  1201. mbox = raid_dev->mbox;
  1202. mbox64 = raid_dev->mbox64;
  1203. /*
  1204. * Check for busy mailbox. If it is, return failure - the caller
  1205. * should retry later.
  1206. */
  1207. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1208. if (unlikely(mbox->busy)) {
  1209. do {
  1210. udelay(1);
  1211. i++;
  1212. rmb();
  1213. } while(mbox->busy && (i < max_mbox_busy_wait));
  1214. if (mbox->busy) {
  1215. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1216. return -1;
  1217. }
  1218. }
  1219. // Copy this command's mailbox data into "adapter's" mailbox
  1220. memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
  1221. mbox->cmdid = scb->sno;
  1222. adapter->outstanding_cmds++;
  1223. if (scb->dma_direction == PCI_DMA_TODEVICE) {
  1224. if (!scb->scp->use_sg) { // sg list not used
  1225. pci_dma_sync_single_for_device(adapter->pdev,
  1226. ccb->buf_dma_h,
  1227. scb->scp->request_bufflen,
  1228. PCI_DMA_TODEVICE);
  1229. }
  1230. else {
  1231. pci_dma_sync_sg_for_device(adapter->pdev,
  1232. scb->scp->request_buffer,
  1233. scb->scp->use_sg, PCI_DMA_TODEVICE);
  1234. }
  1235. }
  1236. mbox->busy = 1; // Set busy
  1237. mbox->poll = 0;
  1238. mbox->ack = 0;
  1239. wmb();
  1240. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  1241. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1242. return 0;
  1243. }
  1244. /**
  1245. * megaraid_queue_command - generic queue entry point for all LLDs
  1246. * @scp : pointer to the scsi command to be executed
  1247. * @done : callback routine to be called after the cmd has be completed
  1248. *
  1249. * Queue entry point for mailbox based controllers.
  1250. */
  1251. static int
  1252. megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
  1253. {
  1254. adapter_t *adapter;
  1255. scb_t *scb;
  1256. int if_busy;
  1257. adapter = SCP2ADAPTER(scp);
  1258. scp->scsi_done = done;
  1259. scp->result = 0;
  1260. assert_spin_locked(adapter->host_lock);
  1261. spin_unlock(adapter->host_lock);
  1262. /*
  1263. * Allocate and build a SCB request
  1264. * if_busy flag will be set if megaraid_mbox_build_cmd() command could
  1265. * not allocate scb. We will return non-zero status in that case.
  1266. * NOTE: scb can be null even though certain commands completed
  1267. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
  1268. * return 0 in that case, and we would do the callback right away.
  1269. */
  1270. if_busy = 0;
  1271. scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
  1272. if (scb) {
  1273. megaraid_mbox_runpendq(adapter, scb);
  1274. }
  1275. spin_lock(adapter->host_lock);
  1276. if (!scb) { // command already completed
  1277. done(scp);
  1278. return 0;
  1279. }
  1280. return if_busy;
  1281. }
  1282. /**
  1283. * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
  1284. * firmware lingua
  1285. * @adapter - controller's soft state
  1286. * @scp - mid-layer scsi command pointer
  1287. * @busy - set if request could not be completed because of lack of
  1288. * resources
  1289. *
  1290. * convert the command issued by mid-layer to format understood by megaraid
  1291. * firmware. We also complete certain command without sending them to firmware
  1292. */
  1293. static scb_t *
  1294. megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
  1295. {
  1296. mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
  1297. int channel;
  1298. int target;
  1299. int islogical;
  1300. mbox_ccb_t *ccb;
  1301. mraid_passthru_t *pthru;
  1302. mbox64_t *mbox64;
  1303. mbox_t *mbox;
  1304. scb_t *scb;
  1305. char skip[] = "skipping";
  1306. char scan[] = "scanning";
  1307. char *ss;
  1308. /*
  1309. * Get the appropriate device map for the device this command is
  1310. * intended for
  1311. */
  1312. MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
  1313. /*
  1314. * Logical drive commands
  1315. */
  1316. if (islogical) {
  1317. switch (scp->cmnd[0]) {
  1318. case TEST_UNIT_READY:
  1319. /*
  1320. * Do we support clustering and is the support enabled
  1321. * If no, return success always
  1322. */
  1323. if (!adapter->ha) {
  1324. scp->result = (DID_OK << 16);
  1325. return NULL;
  1326. }
  1327. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1328. scp->result = (DID_ERROR << 16);
  1329. *busy = 1;
  1330. return NULL;
  1331. }
  1332. scb->dma_direction = scp->sc_data_direction;
  1333. scb->dev_channel = 0xFF;
  1334. scb->dev_target = target;
  1335. ccb = (mbox_ccb_t *)scb->ccb;
  1336. /*
  1337. * The command id will be provided by the command
  1338. * issuance routine
  1339. */
  1340. ccb->raw_mbox[0] = CLUSTER_CMD;
  1341. ccb->raw_mbox[2] = RESERVATION_STATUS;
  1342. ccb->raw_mbox[3] = target;
  1343. return scb;
  1344. case MODE_SENSE:
  1345. if (scp->use_sg) {
  1346. struct scatterlist *sgl;
  1347. caddr_t vaddr;
  1348. sgl = (struct scatterlist *)scp->request_buffer;
  1349. if (sgl->page) {
  1350. vaddr = (caddr_t)
  1351. (page_address((&sgl[0])->page)
  1352. + (&sgl[0])->offset);
  1353. memset(vaddr, 0, scp->cmnd[4]);
  1354. }
  1355. else {
  1356. con_log(CL_ANN, (KERN_WARNING
  1357. "megaraid mailbox: invalid sg:%d\n",
  1358. __LINE__));
  1359. }
  1360. }
  1361. else {
  1362. memset(scp->request_buffer, 0, scp->cmnd[4]);
  1363. }
  1364. scp->result = (DID_OK << 16);
  1365. return NULL;
  1366. case INQUIRY:
  1367. /*
  1368. * Display the channel scan for logical drives
  1369. * Do not display scan for a channel if already done.
  1370. */
  1371. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1372. con_log(CL_ANN, (KERN_INFO
  1373. "scsi[%d]: scanning scsi channel %d",
  1374. adapter->host->host_no,
  1375. SCP2CHANNEL(scp)));
  1376. con_log(CL_ANN, (
  1377. " [virtual] for logical drives\n"));
  1378. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1379. }
  1380. /* Fall through */
  1381. case READ_CAPACITY:
  1382. /*
  1383. * Do not allow LUN > 0 for logical drives and
  1384. * requests for more than 40 logical drives
  1385. */
  1386. if (SCP2LUN(scp)) {
  1387. scp->result = (DID_BAD_TARGET << 16);
  1388. return NULL;
  1389. }
  1390. if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
  1391. scp->result = (DID_BAD_TARGET << 16);
  1392. return NULL;
  1393. }
  1394. /* Allocate a SCB and initialize passthru */
  1395. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1396. scp->result = (DID_ERROR << 16);
  1397. *busy = 1;
  1398. return NULL;
  1399. }
  1400. ccb = (mbox_ccb_t *)scb->ccb;
  1401. scb->dev_channel = 0xFF;
  1402. scb->dev_target = target;
  1403. pthru = ccb->pthru;
  1404. mbox = ccb->mbox;
  1405. mbox64 = ccb->mbox64;
  1406. pthru->timeout = 0;
  1407. pthru->ars = 1;
  1408. pthru->reqsenselen = 14;
  1409. pthru->islogical = 1;
  1410. pthru->logdrv = target;
  1411. pthru->cdblen = scp->cmd_len;
  1412. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1413. mbox->cmd = MBOXCMD_PASSTHRU64;
  1414. scb->dma_direction = scp->sc_data_direction;
  1415. pthru->dataxferlen = scp->request_bufflen;
  1416. pthru->dataxferaddr = ccb->sgl_dma_h;
  1417. pthru->numsge = megaraid_mbox_mksgl(adapter,
  1418. scb);
  1419. mbox->xferaddr = 0xFFFFFFFF;
  1420. mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
  1421. mbox64->xferaddr_hi = 0;
  1422. return scb;
  1423. case READ_6:
  1424. case WRITE_6:
  1425. case READ_10:
  1426. case WRITE_10:
  1427. case READ_12:
  1428. case WRITE_12:
  1429. /*
  1430. * Allocate a SCB and initialize mailbox
  1431. */
  1432. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1433. scp->result = (DID_ERROR << 16);
  1434. *busy = 1;
  1435. return NULL;
  1436. }
  1437. ccb = (mbox_ccb_t *)scb->ccb;
  1438. scb->dev_channel = 0xFF;
  1439. scb->dev_target = target;
  1440. mbox = ccb->mbox;
  1441. mbox64 = ccb->mbox64;
  1442. mbox->logdrv = target;
  1443. /*
  1444. * A little HACK: 2nd bit is zero for all scsi read
  1445. * commands and is set for all scsi write commands
  1446. */
  1447. mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
  1448. MBOXCMD_LREAD64 ;
  1449. /*
  1450. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1451. */
  1452. if (scp->cmd_len == 6) {
  1453. mbox->numsectors = (uint32_t)scp->cmnd[4];
  1454. mbox->lba =
  1455. ((uint32_t)scp->cmnd[1] << 16) |
  1456. ((uint32_t)scp->cmnd[2] << 8) |
  1457. (uint32_t)scp->cmnd[3];
  1458. mbox->lba &= 0x1FFFFF;
  1459. }
  1460. /*
  1461. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1462. */
  1463. else if (scp->cmd_len == 10) {
  1464. mbox->numsectors =
  1465. (uint32_t)scp->cmnd[8] |
  1466. ((uint32_t)scp->cmnd[7] << 8);
  1467. mbox->lba =
  1468. ((uint32_t)scp->cmnd[2] << 24) |
  1469. ((uint32_t)scp->cmnd[3] << 16) |
  1470. ((uint32_t)scp->cmnd[4] << 8) |
  1471. (uint32_t)scp->cmnd[5];
  1472. }
  1473. /*
  1474. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1475. */
  1476. else if (scp->cmd_len == 12) {
  1477. mbox->lba =
  1478. ((uint32_t)scp->cmnd[2] << 24) |
  1479. ((uint32_t)scp->cmnd[3] << 16) |
  1480. ((uint32_t)scp->cmnd[4] << 8) |
  1481. (uint32_t)scp->cmnd[5];
  1482. mbox->numsectors =
  1483. ((uint32_t)scp->cmnd[6] << 24) |
  1484. ((uint32_t)scp->cmnd[7] << 16) |
  1485. ((uint32_t)scp->cmnd[8] << 8) |
  1486. (uint32_t)scp->cmnd[9];
  1487. }
  1488. else {
  1489. con_log(CL_ANN, (KERN_WARNING
  1490. "megaraid: unsupported CDB length\n"));
  1491. megaraid_dealloc_scb(adapter, scb);
  1492. scp->result = (DID_ERROR << 16);
  1493. return NULL;
  1494. }
  1495. scb->dma_direction = scp->sc_data_direction;
  1496. // Calculate Scatter-Gather info
  1497. mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
  1498. mbox->numsge = megaraid_mbox_mksgl(adapter,
  1499. scb);
  1500. mbox->xferaddr = 0xFFFFFFFF;
  1501. mbox64->xferaddr_hi = 0;
  1502. return scb;
  1503. case RESERVE:
  1504. case RELEASE:
  1505. /*
  1506. * Do we support clustering and is the support enabled
  1507. */
  1508. if (!adapter->ha) {
  1509. scp->result = (DID_BAD_TARGET << 16);
  1510. return NULL;
  1511. }
  1512. /*
  1513. * Allocate a SCB and initialize mailbox
  1514. */
  1515. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1516. scp->result = (DID_ERROR << 16);
  1517. *busy = 1;
  1518. return NULL;
  1519. }
  1520. ccb = (mbox_ccb_t *)scb->ccb;
  1521. scb->dev_channel = 0xFF;
  1522. scb->dev_target = target;
  1523. ccb->raw_mbox[0] = CLUSTER_CMD;
  1524. ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
  1525. RESERVE_LD : RELEASE_LD;
  1526. ccb->raw_mbox[3] = target;
  1527. scb->dma_direction = scp->sc_data_direction;
  1528. return scb;
  1529. default:
  1530. scp->result = (DID_BAD_TARGET << 16);
  1531. return NULL;
  1532. }
  1533. }
  1534. else { // Passthru device commands
  1535. // Do not allow access to target id > 15 or LUN > 7
  1536. if (target > 15 || SCP2LUN(scp) > 7) {
  1537. scp->result = (DID_BAD_TARGET << 16);
  1538. return NULL;
  1539. }
  1540. // if fast load option was set and scan for last device is
  1541. // over, reset the fast_load flag so that during a possible
  1542. // next scan, devices can be made available
  1543. if (rdev->fast_load && (target == 15) &&
  1544. (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
  1545. con_log(CL_ANN, (KERN_INFO
  1546. "megaraid[%d]: physical device scan re-enabled\n",
  1547. adapter->host->host_no));
  1548. rdev->fast_load = 0;
  1549. }
  1550. /*
  1551. * Display the channel scan for physical devices
  1552. */
  1553. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1554. ss = rdev->fast_load ? skip : scan;
  1555. con_log(CL_ANN, (KERN_INFO
  1556. "scsi[%d]: %s scsi channel %d [Phy %d]",
  1557. adapter->host->host_no, ss, SCP2CHANNEL(scp),
  1558. channel));
  1559. con_log(CL_ANN, (
  1560. " for non-raid devices\n"));
  1561. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1562. }
  1563. // disable channel sweep if fast load option given
  1564. if (rdev->fast_load) {
  1565. scp->result = (DID_BAD_TARGET << 16);
  1566. return NULL;
  1567. }
  1568. // Allocate a SCB and initialize passthru
  1569. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1570. scp->result = (DID_ERROR << 16);
  1571. *busy = 1;
  1572. return NULL;
  1573. }
  1574. ccb = (mbox_ccb_t *)scb->ccb;
  1575. scb->dev_channel = channel;
  1576. scb->dev_target = target;
  1577. scb->dma_direction = scp->sc_data_direction;
  1578. mbox = ccb->mbox;
  1579. mbox64 = ccb->mbox64;
  1580. // Does this firmware support extended CDBs
  1581. if (adapter->max_cdb_sz == 16) {
  1582. mbox->cmd = MBOXCMD_EXTPTHRU;
  1583. megaraid_mbox_prepare_epthru(adapter, scb, scp);
  1584. mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
  1585. mbox64->xferaddr_hi = 0;
  1586. mbox->xferaddr = 0xFFFFFFFF;
  1587. }
  1588. else {
  1589. mbox->cmd = MBOXCMD_PASSTHRU64;
  1590. megaraid_mbox_prepare_pthru(adapter, scb, scp);
  1591. mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
  1592. mbox64->xferaddr_hi = 0;
  1593. mbox->xferaddr = 0xFFFFFFFF;
  1594. }
  1595. return scb;
  1596. }
  1597. // NOT REACHED
  1598. }
  1599. /**
  1600. * megaraid_mbox_runpendq - execute commands queued in the pending queue
  1601. * @adapter : controller's soft state
  1602. * @scb : SCB to be queued in the pending list
  1603. *
  1604. * scan the pending list for commands which are not yet issued and try to
  1605. * post to the controller. The SCB can be a null pointer, which would indicate
  1606. * no SCB to be queue, just try to execute the ones in the pending list.
  1607. *
  1608. * NOTE: We do not actually traverse the pending list. The SCBs are plucked
  1609. * out from the head of the pending list. If it is successfully issued, the
  1610. * next SCB is at the head now.
  1611. */
  1612. static void
  1613. megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
  1614. {
  1615. scb_t *scb;
  1616. unsigned long flags;
  1617. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1618. if (scb_q) {
  1619. scb_q->state = SCB_PENDQ;
  1620. list_add_tail(&scb_q->list, &adapter->pend_list);
  1621. }
  1622. // if the adapter in not in quiescent mode, post the commands to FW
  1623. if (adapter->quiescent) {
  1624. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1625. return;
  1626. }
  1627. while (!list_empty(&adapter->pend_list)) {
  1628. assert_spin_locked(PENDING_LIST_LOCK(adapter));
  1629. scb = list_entry(adapter->pend_list.next, scb_t, list);
  1630. // remove the scb from the pending list and try to
  1631. // issue. If we are unable to issue it, put back in
  1632. // the pending list and return
  1633. list_del_init(&scb->list);
  1634. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1635. // if mailbox was busy, return SCB back to pending
  1636. // list. Make sure to add at the head, since that's
  1637. // where it would have been removed from
  1638. scb->state = SCB_ISSUED;
  1639. if (mbox_post_cmd(adapter, scb) != 0) {
  1640. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1641. scb->state = SCB_PENDQ;
  1642. list_add(&scb->list, &adapter->pend_list);
  1643. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  1644. flags);
  1645. return;
  1646. }
  1647. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1648. }
  1649. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1650. return;
  1651. }
  1652. /**
  1653. * megaraid_mbox_prepare_pthru - prepare a command for physical devices
  1654. * @adapter - pointer to controller's soft state
  1655. * @scb - scsi control block
  1656. * @scp - scsi command from the mid-layer
  1657. *
  1658. * prepare a command for the scsi physical devices
  1659. */
  1660. static void
  1661. megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
  1662. struct scsi_cmnd *scp)
  1663. {
  1664. mbox_ccb_t *ccb;
  1665. mraid_passthru_t *pthru;
  1666. uint8_t channel;
  1667. uint8_t target;
  1668. ccb = (mbox_ccb_t *)scb->ccb;
  1669. pthru = ccb->pthru;
  1670. channel = scb->dev_channel;
  1671. target = scb->dev_target;
  1672. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1673. pthru->timeout = 4;
  1674. pthru->ars = 1;
  1675. pthru->islogical = 0;
  1676. pthru->channel = 0;
  1677. pthru->target = (channel << 4) | target;
  1678. pthru->logdrv = SCP2LUN(scp);
  1679. pthru->reqsenselen = 14;
  1680. pthru->cdblen = scp->cmd_len;
  1681. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1682. if (scp->request_bufflen) {
  1683. pthru->dataxferlen = scp->request_bufflen;
  1684. pthru->dataxferaddr = ccb->sgl_dma_h;
  1685. pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1686. }
  1687. else {
  1688. pthru->dataxferaddr = 0;
  1689. pthru->dataxferlen = 0;
  1690. pthru->numsge = 0;
  1691. }
  1692. return;
  1693. }
  1694. /**
  1695. * megaraid_mbox_prepare_epthru - prepare a command for physical devices
  1696. * @adapter - pointer to controller's soft state
  1697. * @scb - scsi control block
  1698. * @scp - scsi command from the mid-layer
  1699. *
  1700. * prepare a command for the scsi physical devices. This rountine prepares
  1701. * commands for devices which can take extended CDBs (>10 bytes)
  1702. */
  1703. static void
  1704. megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
  1705. struct scsi_cmnd *scp)
  1706. {
  1707. mbox_ccb_t *ccb;
  1708. mraid_epassthru_t *epthru;
  1709. uint8_t channel;
  1710. uint8_t target;
  1711. ccb = (mbox_ccb_t *)scb->ccb;
  1712. epthru = ccb->epthru;
  1713. channel = scb->dev_channel;
  1714. target = scb->dev_target;
  1715. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1716. epthru->timeout = 4;
  1717. epthru->ars = 1;
  1718. epthru->islogical = 0;
  1719. epthru->channel = 0;
  1720. epthru->target = (channel << 4) | target;
  1721. epthru->logdrv = SCP2LUN(scp);
  1722. epthru->reqsenselen = 14;
  1723. epthru->cdblen = scp->cmd_len;
  1724. memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
  1725. if (scp->request_bufflen) {
  1726. epthru->dataxferlen = scp->request_bufflen;
  1727. epthru->dataxferaddr = ccb->sgl_dma_h;
  1728. epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1729. }
  1730. else {
  1731. epthru->dataxferaddr = 0;
  1732. epthru->dataxferlen = 0;
  1733. epthru->numsge = 0;
  1734. }
  1735. return;
  1736. }
  1737. /**
  1738. * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
  1739. * @adapter - controller's soft state
  1740. *
  1741. * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
  1742. * completed command and put them on the completed list for later processing.
  1743. *
  1744. * Returns: 1 if the interrupt is valid, 0 otherwise
  1745. */
  1746. static inline int
  1747. megaraid_ack_sequence(adapter_t *adapter)
  1748. {
  1749. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1750. mbox_t *mbox;
  1751. scb_t *scb;
  1752. uint8_t nstatus;
  1753. uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
  1754. struct list_head clist;
  1755. int handled;
  1756. uint32_t dword;
  1757. unsigned long flags;
  1758. int i, j;
  1759. mbox = raid_dev->mbox;
  1760. // move the SCBs from the firmware completed array to our local list
  1761. INIT_LIST_HEAD(&clist);
  1762. // loop till F/W has more commands for us to complete
  1763. handled = 0;
  1764. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1765. do {
  1766. /*
  1767. * Check if a valid interrupt is pending. If found, force the
  1768. * interrupt line low.
  1769. */
  1770. dword = RDOUTDOOR(raid_dev);
  1771. if (dword != 0x10001234) break;
  1772. handled = 1;
  1773. WROUTDOOR(raid_dev, 0x10001234);
  1774. nstatus = 0;
  1775. // wait for valid numstatus to post
  1776. for (i = 0; i < 0xFFFFF; i++) {
  1777. if (mbox->numstatus != 0xFF) {
  1778. nstatus = mbox->numstatus;
  1779. break;
  1780. }
  1781. rmb();
  1782. }
  1783. mbox->numstatus = 0xFF;
  1784. adapter->outstanding_cmds -= nstatus;
  1785. for (i = 0; i < nstatus; i++) {
  1786. // wait for valid command index to post
  1787. for (j = 0; j < 0xFFFFF; j++) {
  1788. if (mbox->completed[i] != 0xFF) break;
  1789. rmb();
  1790. }
  1791. completed[i] = mbox->completed[i];
  1792. mbox->completed[i] = 0xFF;
  1793. if (completed[i] == 0xFF) {
  1794. con_log(CL_ANN, (KERN_CRIT
  1795. "megaraid: command posting timed out\n"));
  1796. BUG();
  1797. continue;
  1798. }
  1799. // Get SCB associated with this command id
  1800. if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
  1801. // a cmm command
  1802. scb = adapter->uscb_list + (completed[i] -
  1803. MBOX_MAX_SCSI_CMDS);
  1804. }
  1805. else {
  1806. // an os command
  1807. scb = adapter->kscb_list + completed[i];
  1808. }
  1809. scb->status = mbox->status;
  1810. list_add_tail(&scb->list, &clist);
  1811. }
  1812. // Acknowledge interrupt
  1813. WRINDOOR(raid_dev, 0x02);
  1814. } while(1);
  1815. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1816. // put the completed commands in the completed list. DPC would
  1817. // complete these commands later
  1818. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1819. list_splice(&clist, &adapter->completed_list);
  1820. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1821. // schedule the DPC if there is some work for it
  1822. if (handled)
  1823. tasklet_schedule(&adapter->dpc_h);
  1824. return handled;
  1825. }
  1826. /**
  1827. * megaraid_isr - isr for memory based mailbox based controllers
  1828. * @irq - irq
  1829. * @devp - pointer to our soft state
  1830. * @regs - unused
  1831. *
  1832. * Interrupt service routine for memory-mapped mailbox controllers.
  1833. */
  1834. static irqreturn_t
  1835. megaraid_isr(int irq, void *devp, struct pt_regs *regs)
  1836. {
  1837. adapter_t *adapter = devp;
  1838. int handled;
  1839. handled = megaraid_ack_sequence(adapter);
  1840. /* Loop through any pending requests */
  1841. if (!adapter->quiescent) {
  1842. megaraid_mbox_runpendq(adapter, NULL);
  1843. }
  1844. return IRQ_RETVAL(handled);
  1845. }
  1846. /**
  1847. * megaraid_mbox_sync_scb - sync kernel buffers
  1848. * @adapter : controller's soft state
  1849. * @scb : pointer to the resource packet
  1850. *
  1851. * DMA sync if required.
  1852. */
  1853. static inline void
  1854. megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
  1855. {
  1856. mbox_ccb_t *ccb;
  1857. ccb = (mbox_ccb_t *)scb->ccb;
  1858. switch (scb->dma_type) {
  1859. case MRAID_DMA_WBUF:
  1860. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1861. pci_dma_sync_single_for_cpu(adapter->pdev,
  1862. ccb->buf_dma_h,
  1863. scb->scp->request_bufflen,
  1864. PCI_DMA_FROMDEVICE);
  1865. }
  1866. pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
  1867. scb->scp->request_bufflen, scb->dma_direction);
  1868. break;
  1869. case MRAID_DMA_WSG:
  1870. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1871. pci_dma_sync_sg_for_cpu(adapter->pdev,
  1872. scb->scp->request_buffer,
  1873. scb->scp->use_sg, PCI_DMA_FROMDEVICE);
  1874. }
  1875. pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
  1876. scb->scp->use_sg, scb->dma_direction);
  1877. break;
  1878. default:
  1879. break;
  1880. }
  1881. return;
  1882. }
  1883. /**
  1884. * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
  1885. * @devp : pointer to HBA soft state
  1886. *
  1887. * Pick up the commands from the completed list and send back to the owners.
  1888. * This is a reentrant function and does not assume any locks are held while
  1889. * it is being called.
  1890. */
  1891. static void
  1892. megaraid_mbox_dpc(unsigned long devp)
  1893. {
  1894. adapter_t *adapter = (adapter_t *)devp;
  1895. mraid_device_t *raid_dev;
  1896. struct list_head clist;
  1897. struct scatterlist *sgl;
  1898. scb_t *scb;
  1899. scb_t *tmp;
  1900. struct scsi_cmnd *scp;
  1901. mraid_passthru_t *pthru;
  1902. mraid_epassthru_t *epthru;
  1903. mbox_ccb_t *ccb;
  1904. int islogical;
  1905. int pdev_index;
  1906. int pdev_state;
  1907. mbox_t *mbox;
  1908. unsigned long flags;
  1909. uint8_t c;
  1910. int status;
  1911. if (!adapter) return;
  1912. raid_dev = ADAP2RAIDDEV(adapter);
  1913. // move the SCBs from the completed list to our local list
  1914. INIT_LIST_HEAD(&clist);
  1915. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1916. list_splice_init(&adapter->completed_list, &clist);
  1917. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1918. list_for_each_entry_safe(scb, tmp, &clist, list) {
  1919. status = scb->status;
  1920. scp = scb->scp;
  1921. ccb = (mbox_ccb_t *)scb->ccb;
  1922. pthru = ccb->pthru;
  1923. epthru = ccb->epthru;
  1924. mbox = ccb->mbox;
  1925. // Make sure f/w has completed a valid command
  1926. if (scb->state != SCB_ISSUED) {
  1927. con_log(CL_ANN, (KERN_CRIT
  1928. "megaraid critical err: invalid command %d:%d:%p\n",
  1929. scb->sno, scb->state, scp));
  1930. BUG();
  1931. continue; // Must never happen!
  1932. }
  1933. // check for the management command and complete it right away
  1934. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  1935. scb->state = SCB_FREE;
  1936. scb->status = status;
  1937. // remove from local clist
  1938. list_del_init(&scb->list);
  1939. megaraid_mbox_mm_done(adapter, scb);
  1940. continue;
  1941. }
  1942. // Was an abort issued for this command earlier
  1943. if (scb->state & SCB_ABORT) {
  1944. con_log(CL_ANN, (KERN_NOTICE
  1945. "megaraid: aborted cmd %lx[%x] completed\n",
  1946. scp->serial_number, scb->sno));
  1947. }
  1948. /*
  1949. * If the inquiry came of a disk drive which is not part of
  1950. * any RAID array, expose it to the kernel. For this to be
  1951. * enabled, user must set the "megaraid_expose_unconf_disks"
  1952. * flag to 1 by specifying it on module parameter list.
  1953. * This would enable data migration off drives from other
  1954. * configurations.
  1955. */
  1956. islogical = MRAID_IS_LOGICAL(adapter, scp);
  1957. if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
  1958. && IS_RAID_CH(raid_dev, scb->dev_channel)) {
  1959. if (scp->use_sg) {
  1960. sgl = (struct scatterlist *)
  1961. scp->request_buffer;
  1962. if (sgl->page) {
  1963. c = *(unsigned char *)
  1964. (page_address((&sgl[0])->page) +
  1965. (&sgl[0])->offset);
  1966. }
  1967. else {
  1968. con_log(CL_ANN, (KERN_WARNING
  1969. "megaraid mailbox: invalid sg:%d\n",
  1970. __LINE__));
  1971. c = 0;
  1972. }
  1973. }
  1974. else {
  1975. c = *(uint8_t *)scp->request_buffer;
  1976. }
  1977. if ((c & 0x1F ) == TYPE_DISK) {
  1978. pdev_index = (scb->dev_channel * 16) +
  1979. scb->dev_target;
  1980. pdev_state =
  1981. raid_dev->pdrv_state[pdev_index] & 0x0F;
  1982. if (pdev_state == PDRV_ONLINE ||
  1983. pdev_state == PDRV_FAILED ||
  1984. pdev_state == PDRV_RBLD ||
  1985. pdev_state == PDRV_HOTSPARE ||
  1986. megaraid_expose_unconf_disks == 0) {
  1987. status = 0xF0;
  1988. }
  1989. }
  1990. }
  1991. // Convert MegaRAID status to Linux error code
  1992. switch (status) {
  1993. case 0x00:
  1994. scp->result = (DID_OK << 16);
  1995. break;
  1996. case 0x02:
  1997. /* set sense_buffer and result fields */
  1998. if (mbox->cmd == MBOXCMD_PASSTHRU ||
  1999. mbox->cmd == MBOXCMD_PASSTHRU64) {
  2000. memcpy(scp->sense_buffer, pthru->reqsensearea,
  2001. 14);
  2002. scp->result = DRIVER_SENSE << 24 |
  2003. DID_OK << 16 | CHECK_CONDITION << 1;
  2004. }
  2005. else {
  2006. if (mbox->cmd == MBOXCMD_EXTPTHRU) {
  2007. memcpy(scp->sense_buffer,
  2008. epthru->reqsensearea, 14);
  2009. scp->result = DRIVER_SENSE << 24 |
  2010. DID_OK << 16 |
  2011. CHECK_CONDITION << 1;
  2012. } else {
  2013. scp->sense_buffer[0] = 0x70;
  2014. scp->sense_buffer[2] = ABORTED_COMMAND;
  2015. scp->result = CHECK_CONDITION << 1;
  2016. }
  2017. }
  2018. break;
  2019. case 0x08:
  2020. scp->result = DID_BUS_BUSY << 16 | status;
  2021. break;
  2022. default:
  2023. /*
  2024. * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
  2025. * failed
  2026. */
  2027. if (scp->cmnd[0] == TEST_UNIT_READY) {
  2028. scp->result = DID_ERROR << 16 |
  2029. RESERVATION_CONFLICT << 1;
  2030. }
  2031. else
  2032. /*
  2033. * Error code returned is 1 if Reserve or Release
  2034. * failed or the input parameter is invalid
  2035. */
  2036. if (status == 1 && (scp->cmnd[0] == RESERVE ||
  2037. scp->cmnd[0] == RELEASE)) {
  2038. scp->result = DID_ERROR << 16 |
  2039. RESERVATION_CONFLICT << 1;
  2040. }
  2041. else {
  2042. scp->result = DID_BAD_TARGET << 16 | status;
  2043. }
  2044. }
  2045. // print a debug message for all failed commands
  2046. if (status) {
  2047. megaraid_mbox_display_scb(adapter, scb);
  2048. }
  2049. // Free our internal resources and call the mid-layer callback
  2050. // routine
  2051. megaraid_mbox_sync_scb(adapter, scb);
  2052. // remove from local clist
  2053. list_del_init(&scb->list);
  2054. // put back in free list
  2055. megaraid_dealloc_scb(adapter, scb);
  2056. // send the scsi packet back to kernel
  2057. spin_lock(adapter->host_lock);
  2058. scp->scsi_done(scp);
  2059. spin_unlock(adapter->host_lock);
  2060. }
  2061. return;
  2062. }
  2063. /**
  2064. * megaraid_abort_handler - abort the scsi command
  2065. * @scp : command to be aborted
  2066. *
  2067. * Abort a previous SCSI request. Only commands on the pending list can be
  2068. * aborted. All the commands issued to the F/W must complete.
  2069. **/
  2070. static int
  2071. __megaraid_abort_handler(struct scsi_cmnd *scp)
  2072. {
  2073. adapter_t *adapter;
  2074. mraid_device_t *raid_dev;
  2075. scb_t *scb;
  2076. scb_t *tmp;
  2077. int found;
  2078. unsigned long flags;
  2079. int i;
  2080. adapter = SCP2ADAPTER(scp);
  2081. raid_dev = ADAP2RAIDDEV(adapter);
  2082. assert_spin_locked(adapter->host_lock);
  2083. con_log(CL_ANN, (KERN_WARNING
  2084. "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
  2085. scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
  2086. SCP2TARGET(scp), SCP2LUN(scp)));
  2087. // If FW has stopped responding, simply return failure
  2088. if (raid_dev->hw_error) {
  2089. con_log(CL_ANN, (KERN_NOTICE
  2090. "megaraid: hw error, not aborting\n"));
  2091. return FAILED;
  2092. }
  2093. // There might a race here, where the command was completed by the
  2094. // firmware and now it is on the completed list. Before we could
  2095. // complete the command to the kernel in dpc, the abort came.
  2096. // Find out if this is the case to avoid the race.
  2097. scb = NULL;
  2098. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  2099. list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
  2100. if (scb->scp == scp) { // Found command
  2101. list_del_init(&scb->list); // from completed list
  2102. con_log(CL_ANN, (KERN_WARNING
  2103. "megaraid: %ld:%d[%d:%d], abort from completed list\n",
  2104. scp->serial_number, scb->sno,
  2105. scb->dev_channel, scb->dev_target));
  2106. scp->result = (DID_ABORT << 16);
  2107. scp->scsi_done(scp);
  2108. megaraid_dealloc_scb(adapter, scb);
  2109. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
  2110. flags);
  2111. return SUCCESS;
  2112. }
  2113. }
  2114. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  2115. // Find out if this command is still on the pending list. If it is and
  2116. // was never issued, abort and return success. If the command is owned
  2117. // by the firmware, we must wait for it to complete by the FW.
  2118. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2119. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2120. if (scb->scp == scp) { // Found command
  2121. list_del_init(&scb->list); // from pending list
  2122. ASSERT(!(scb->state & SCB_ISSUED));
  2123. con_log(CL_ANN, (KERN_WARNING
  2124. "megaraid abort: %ld[%d:%d], driver owner\n",
  2125. scp->serial_number, scb->dev_channel,
  2126. scb->dev_target));
  2127. scp->result = (DID_ABORT << 16);
  2128. scp->scsi_done(scp);
  2129. megaraid_dealloc_scb(adapter, scb);
  2130. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  2131. flags);
  2132. return SUCCESS;
  2133. }
  2134. }
  2135. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2136. // Check do we even own this command, in which case this would be
  2137. // owned by the firmware. The only way to locate the FW scb is to
  2138. // traverse through the list of all SCB, since driver does not
  2139. // maintain these SCBs on any list
  2140. found = 0;
  2141. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  2142. scb = adapter->kscb_list + i;
  2143. if (scb->scp == scp) {
  2144. found = 1;
  2145. if (!(scb->state & SCB_ISSUED)) {
  2146. con_log(CL_ANN, (KERN_WARNING
  2147. "megaraid abort: %ld%d[%d:%d], invalid state\n",
  2148. scp->serial_number, scb->sno, scb->dev_channel,
  2149. scb->dev_target));
  2150. BUG();
  2151. }
  2152. else {
  2153. con_log(CL_ANN, (KERN_WARNING
  2154. "megaraid abort: %ld:%d[%d:%d], fw owner\n",
  2155. scp->serial_number, scb->sno, scb->dev_channel,
  2156. scb->dev_target));
  2157. }
  2158. }
  2159. }
  2160. if (!found) {
  2161. con_log(CL_ANN, (KERN_WARNING
  2162. "megaraid abort: scsi cmd:%ld, do now own\n",
  2163. scp->serial_number));
  2164. // FIXME: Should there be a callback for this command?
  2165. return SUCCESS;
  2166. }
  2167. // We cannot actually abort a command owned by firmware, return
  2168. // failure and wait for reset. In host reset handler, we will find out
  2169. // if the HBA is still live
  2170. return FAILED;
  2171. }
  2172. static int
  2173. megaraid_abort_handler(struct scsi_cmnd *scp)
  2174. {
  2175. adapter_t *adapter;
  2176. int rc;
  2177. adapter = SCP2ADAPTER(scp);
  2178. spin_lock_irq(adapter->host_lock);
  2179. rc = __megaraid_abort_handler(scp);
  2180. spin_unlock_irq(adapter->host_lock);
  2181. return rc;
  2182. }
  2183. /**
  2184. * megaraid_reset_handler - device reset hadler for mailbox based driver
  2185. * @scp : reference command
  2186. *
  2187. * Reset handler for the mailbox based controller. First try to find out if
  2188. * the FW is still live, in which case the outstanding commands counter mut go
  2189. * down to 0. If that happens, also issue the reservation reset command to
  2190. * relinquish (possible) reservations on the logical drives connected to this
  2191. * host
  2192. **/
  2193. static int
  2194. __megaraid_reset_handler(struct scsi_cmnd *scp)
  2195. {
  2196. adapter_t *adapter;
  2197. scb_t *scb;
  2198. scb_t *tmp;
  2199. mraid_device_t *raid_dev;
  2200. unsigned long flags;
  2201. uint8_t raw_mbox[sizeof(mbox_t)];
  2202. int rval;
  2203. int recovery_window;
  2204. int recovering;
  2205. int i;
  2206. adapter = SCP2ADAPTER(scp);
  2207. raid_dev = ADAP2RAIDDEV(adapter);
  2208. assert_spin_locked(adapter->host_lock);
  2209. con_log(CL_ANN, (KERN_WARNING "megaraid: reseting the host...\n"));
  2210. // return failure if adapter is not responding
  2211. if (raid_dev->hw_error) {
  2212. con_log(CL_ANN, (KERN_NOTICE
  2213. "megaraid: hw error, cannot reset\n"));
  2214. return FAILED;
  2215. }
  2216. // Under exceptional conditions, FW can take up to 3 minutes to
  2217. // complete command processing. Wait for additional 2 minutes for the
  2218. // pending commands counter to go down to 0. If it doesn't, let the
  2219. // controller be marked offline
  2220. // Also, reset all the commands currently owned by the driver
  2221. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2222. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2223. list_del_init(&scb->list); // from pending list
  2224. con_log(CL_ANN, (KERN_WARNING
  2225. "megaraid: %ld:%d[%d:%d], reset from pending list\n",
  2226. scp->serial_number, scb->sno,
  2227. scb->dev_channel, scb->dev_target));
  2228. scp->result = (DID_RESET << 16);
  2229. scp->scsi_done(scp);
  2230. megaraid_dealloc_scb(adapter, scb);
  2231. }
  2232. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2233. if (adapter->outstanding_cmds) {
  2234. con_log(CL_ANN, (KERN_NOTICE
  2235. "megaraid: %d outstanding commands. Max wait %d sec\n",
  2236. adapter->outstanding_cmds, MBOX_RESET_WAIT));
  2237. }
  2238. spin_unlock(adapter->host_lock);
  2239. recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2240. recovering = adapter->outstanding_cmds;
  2241. for (i = 0; i < recovery_window && adapter->outstanding_cmds; i++) {
  2242. megaraid_ack_sequence(adapter);
  2243. // print a message once every 5 seconds only
  2244. if (!(i % 5)) {
  2245. con_log(CL_ANN, (
  2246. "megaraid mbox: Wait for %d commands to complete:%d\n",
  2247. adapter->outstanding_cmds,
  2248. MBOX_RESET_WAIT - i));
  2249. }
  2250. // bailout if no recovery happended in reset time
  2251. if ((i == MBOX_RESET_WAIT) &&
  2252. (recovering == adapter->outstanding_cmds)) {
  2253. break;
  2254. }
  2255. msleep(1000);
  2256. }
  2257. spin_lock(adapter->host_lock);
  2258. // If still outstanding commands, bail out
  2259. if (adapter->outstanding_cmds) {
  2260. con_log(CL_ANN, (KERN_WARNING
  2261. "megaraid mbox: critical hardware error!\n"));
  2262. raid_dev->hw_error = 1;
  2263. return FAILED;
  2264. }
  2265. else {
  2266. con_log(CL_ANN, (KERN_NOTICE
  2267. "megaraid mbox: reset sequence completed sucessfully\n"));
  2268. }
  2269. // If the controller supports clustering, reset reservations
  2270. if (!adapter->ha) return SUCCESS;
  2271. // clear reservations if any
  2272. raw_mbox[0] = CLUSTER_CMD;
  2273. raw_mbox[2] = RESET_RESERVATIONS;
  2274. rval = SUCCESS;
  2275. if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
  2276. con_log(CL_ANN,
  2277. (KERN_INFO "megaraid: reservation reset\n"));
  2278. }
  2279. else {
  2280. rval = FAILED;
  2281. con_log(CL_ANN, (KERN_WARNING
  2282. "megaraid: reservation reset failed\n"));
  2283. }
  2284. return rval;
  2285. }
  2286. static int
  2287. megaraid_reset_handler(struct scsi_cmnd *cmd)
  2288. {
  2289. int rc;
  2290. spin_lock_irq(cmd->device->host->host_lock);
  2291. rc = __megaraid_reset_handler(cmd);
  2292. spin_unlock_irq(cmd->device->host->host_lock);
  2293. return rc;
  2294. }
  2295. /*
  2296. * START: internal commands library
  2297. *
  2298. * This section of the driver has the common routine used by the driver and
  2299. * also has all the FW routines
  2300. */
  2301. /**
  2302. * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
  2303. * @adapter - controller's soft state
  2304. * @raw_mbox - the mailbox
  2305. *
  2306. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2307. * controllers
  2308. */
  2309. static int
  2310. mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
  2311. {
  2312. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2313. mbox64_t *mbox64;
  2314. mbox_t *mbox;
  2315. uint8_t status;
  2316. int i;
  2317. mbox64 = raid_dev->mbox64;
  2318. mbox = raid_dev->mbox;
  2319. /*
  2320. * Wait until mailbox is free
  2321. */
  2322. if (megaraid_busywait_mbox(raid_dev) != 0)
  2323. goto blocked_mailbox;
  2324. /*
  2325. * Copy mailbox data into host structure
  2326. */
  2327. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2328. mbox->cmdid = 0xFE;
  2329. mbox->busy = 1;
  2330. mbox->poll = 0;
  2331. mbox->ack = 0;
  2332. mbox->numstatus = 0xFF;
  2333. mbox->status = 0xFF;
  2334. wmb();
  2335. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2336. // wait for maximum 1 second for status to post. If the status is not
  2337. // available within 1 second, assume FW is initializing and wait
  2338. // for an extended amount of time
  2339. if (mbox->numstatus == 0xFF) { // status not yet available
  2340. udelay(25);;
  2341. for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
  2342. rmb();
  2343. msleep(1);
  2344. }
  2345. if (i == 1000) {
  2346. con_log(CL_ANN, (KERN_NOTICE
  2347. "megaraid mailbox: wait for FW to boot "));
  2348. for (i = 0; (mbox->numstatus == 0xFF) &&
  2349. (i < MBOX_RESET_WAIT); i++) {
  2350. rmb();
  2351. con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
  2352. MBOX_RESET_WAIT - i));
  2353. msleep(1000);
  2354. }
  2355. if (i == MBOX_RESET_WAIT) {
  2356. con_log(CL_ANN, (
  2357. "\nmegaraid mailbox: status not available\n"));
  2358. return -1;
  2359. }
  2360. con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
  2361. }
  2362. }
  2363. // wait for maximum 1 second for poll semaphore
  2364. if (mbox->poll != 0x77) {
  2365. udelay(25);
  2366. for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
  2367. rmb();
  2368. msleep(1);
  2369. }
  2370. if (i == 1000) {
  2371. con_log(CL_ANN, (KERN_WARNING
  2372. "megaraid mailbox: could not get poll semaphore\n"));
  2373. return -1;
  2374. }
  2375. }
  2376. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2377. wmb();
  2378. // wait for maximum 1 second for acknowledgement
  2379. if (RDINDOOR(raid_dev) & 0x2) {
  2380. udelay(25);
  2381. for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
  2382. rmb();
  2383. msleep(1);
  2384. }
  2385. if (i == 1000) {
  2386. con_log(CL_ANN, (KERN_WARNING
  2387. "megaraid mailbox: could not acknowledge\n"));
  2388. return -1;
  2389. }
  2390. }
  2391. mbox->poll = 0;
  2392. mbox->ack = 0x77;
  2393. status = mbox->status;
  2394. // invalidate the completed command id array. After command
  2395. // completion, firmware would write the valid id.
  2396. mbox->numstatus = 0xFF;
  2397. mbox->status = 0xFF;
  2398. for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
  2399. mbox->completed[i] = 0xFF;
  2400. }
  2401. return status;
  2402. blocked_mailbox:
  2403. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
  2404. return -1;
  2405. }
  2406. /**
  2407. * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
  2408. * @adapter - controller's soft state
  2409. * @raw_mbox - the mailbox
  2410. *
  2411. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2412. * controllers. This is a faster version of the synchronous command and
  2413. * therefore can be called in interrupt-context as well
  2414. */
  2415. static int
  2416. mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
  2417. {
  2418. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2419. mbox_t *mbox;
  2420. long i;
  2421. mbox = raid_dev->mbox;
  2422. // return immediately if the mailbox is busy
  2423. if (mbox->busy) return -1;
  2424. // Copy mailbox data into host structure
  2425. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
  2426. mbox->cmdid = 0xFE;
  2427. mbox->busy = 1;
  2428. mbox->poll = 0;
  2429. mbox->ack = 0;
  2430. mbox->numstatus = 0xFF;
  2431. mbox->status = 0xFF;
  2432. wmb();
  2433. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2434. for (i = 0; i < 0xFFFFF; i++) {
  2435. if (mbox->numstatus != 0xFF) break;
  2436. }
  2437. if (i == 0xFFFFF) {
  2438. // We may need to re-calibrate the counter
  2439. con_log(CL_ANN, (KERN_CRIT
  2440. "megaraid: fast sync command timed out\n"));
  2441. }
  2442. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2443. wmb();
  2444. return mbox->status;
  2445. }
  2446. /**
  2447. * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
  2448. * @raid_dev - RAID device (HBA) soft state
  2449. *
  2450. * wait until the controller's mailbox is available to accept more commands.
  2451. * wait for at most 1 second
  2452. */
  2453. static int
  2454. megaraid_busywait_mbox(mraid_device_t *raid_dev)
  2455. {
  2456. mbox_t *mbox = raid_dev->mbox;
  2457. int i = 0;
  2458. if (mbox->busy) {
  2459. udelay(25);
  2460. for (i = 0; mbox->busy && i < 1000; i++)
  2461. msleep(1);
  2462. }
  2463. if (i < 1000) return 0;
  2464. else return -1;
  2465. }
  2466. /**
  2467. * megaraid_mbox_product_info - some static information about the controller
  2468. * @adapter - our soft state
  2469. *
  2470. * issue commands to the controller to grab some parameters required by our
  2471. * caller.
  2472. */
  2473. static int
  2474. megaraid_mbox_product_info(adapter_t *adapter)
  2475. {
  2476. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2477. mbox_t *mbox;
  2478. uint8_t raw_mbox[sizeof(mbox_t)];
  2479. mraid_pinfo_t *pinfo;
  2480. dma_addr_t pinfo_dma_h;
  2481. mraid_inquiry3_t *mraid_inq3;
  2482. int i;
  2483. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2484. mbox = (mbox_t *)raw_mbox;
  2485. /*
  2486. * Issue an ENQUIRY3 command to find out certain adapter parameters,
  2487. * e.g., max channels, max commands etc.
  2488. */
  2489. pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2490. &pinfo_dma_h);
  2491. if (pinfo == NULL) {
  2492. con_log(CL_ANN, (KERN_WARNING
  2493. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2494. __LINE__));
  2495. return -1;
  2496. }
  2497. memset(pinfo, 0, sizeof(mraid_pinfo_t));
  2498. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2499. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2500. raw_mbox[0] = FC_NEW_CONFIG;
  2501. raw_mbox[2] = NC_SUBOP_ENQUIRY3;
  2502. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
  2503. // Issue the command
  2504. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2505. con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
  2506. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2507. pinfo, pinfo_dma_h);
  2508. return -1;
  2509. }
  2510. /*
  2511. * Collect information about state of each physical drive
  2512. * attached to the controller. We will expose all the disks
  2513. * which are not part of RAID
  2514. */
  2515. mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
  2516. for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
  2517. raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
  2518. }
  2519. /*
  2520. * Get product info for information like number of channels,
  2521. * maximum commands supported.
  2522. */
  2523. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2524. mbox->xferaddr = (uint32_t)pinfo_dma_h;
  2525. raw_mbox[0] = FC_NEW_CONFIG;
  2526. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
  2527. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2528. con_log(CL_ANN, (KERN_WARNING
  2529. "megaraid: product info failed\n"));
  2530. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2531. pinfo, pinfo_dma_h);
  2532. return -1;
  2533. }
  2534. /*
  2535. * Setup some parameters for host, as required by our caller
  2536. */
  2537. adapter->max_channel = pinfo->nchannels;
  2538. /*
  2539. * we will export all the logical drives on a single channel.
  2540. * Add 1 since inquires do not come for inititor ID
  2541. */
  2542. adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
  2543. adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
  2544. /*
  2545. * These are the maximum outstanding commands for the scsi-layer
  2546. */
  2547. adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
  2548. memset(adapter->fw_version, 0, VERSION_SIZE);
  2549. memset(adapter->bios_version, 0, VERSION_SIZE);
  2550. memcpy(adapter->fw_version, pinfo->fw_version, 4);
  2551. adapter->fw_version[4] = 0;
  2552. memcpy(adapter->bios_version, pinfo->bios_version, 4);
  2553. adapter->bios_version[4] = 0;
  2554. con_log(CL_ANN, (KERN_NOTICE
  2555. "megaraid: fw version:[%s] bios version:[%s]\n",
  2556. adapter->fw_version, adapter->bios_version));
  2557. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
  2558. pinfo_dma_h);
  2559. return 0;
  2560. }
  2561. /**
  2562. * megaraid_mbox_extended_cdb - check for support for extended CDBs
  2563. * @adapter - soft state for the controller
  2564. *
  2565. * this routine check whether the controller in question supports extended
  2566. * ( > 10 bytes ) CDBs
  2567. */
  2568. static int
  2569. megaraid_mbox_extended_cdb(adapter_t *adapter)
  2570. {
  2571. mbox_t *mbox;
  2572. uint8_t raw_mbox[sizeof(mbox_t)];
  2573. int rval;
  2574. mbox = (mbox_t *)raw_mbox;
  2575. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2576. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2577. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2578. raw_mbox[0] = MAIN_MISC_OPCODE;
  2579. raw_mbox[2] = SUPPORT_EXT_CDB;
  2580. /*
  2581. * Issue the command
  2582. */
  2583. rval = 0;
  2584. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2585. rval = -1;
  2586. }
  2587. return rval;
  2588. }
  2589. /**
  2590. * megaraid_mbox_support_ha - Do we support clustering
  2591. * @adapter - soft state for the controller
  2592. * @init_id - ID of the initiator
  2593. *
  2594. * Determine if the firmware supports clustering and the ID of the initiator.
  2595. */
  2596. static int
  2597. megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
  2598. {
  2599. mbox_t *mbox;
  2600. uint8_t raw_mbox[sizeof(mbox_t)];
  2601. int rval;
  2602. mbox = (mbox_t *)raw_mbox;
  2603. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2604. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2605. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2606. raw_mbox[0] = GET_TARGET_ID;
  2607. // Issue the command
  2608. *init_id = 7;
  2609. rval = -1;
  2610. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2611. *init_id = *(uint8_t *)adapter->ibuf;
  2612. con_log(CL_ANN, (KERN_INFO
  2613. "megaraid: cluster firmware, initiator ID: %d\n",
  2614. *init_id));
  2615. rval = 0;
  2616. }
  2617. return rval;
  2618. }
  2619. /**
  2620. * megaraid_mbox_support_random_del - Do we support random deletion
  2621. * @adapter - soft state for the controller
  2622. *
  2623. * Determine if the firmware supports random deletion
  2624. * Return: 1 is operation supported, 0 otherwise
  2625. */
  2626. static int
  2627. megaraid_mbox_support_random_del(adapter_t *adapter)
  2628. {
  2629. mbox_t *mbox;
  2630. uint8_t raw_mbox[sizeof(mbox_t)];
  2631. int rval;
  2632. mbox = (mbox_t *)raw_mbox;
  2633. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2634. raw_mbox[0] = FC_DEL_LOGDRV;
  2635. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  2636. // Issue the command
  2637. rval = 0;
  2638. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2639. con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
  2640. rval = 1;
  2641. }
  2642. return rval;
  2643. }
  2644. /**
  2645. * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
  2646. * @adapter - soft state for the controller
  2647. *
  2648. * Find out the maximum number of scatter-gather elements supported by the
  2649. * firmware
  2650. */
  2651. static int
  2652. megaraid_mbox_get_max_sg(adapter_t *adapter)
  2653. {
  2654. mbox_t *mbox;
  2655. uint8_t raw_mbox[sizeof(mbox_t)];
  2656. int nsg;
  2657. mbox = (mbox_t *)raw_mbox;
  2658. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2659. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2660. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2661. raw_mbox[0] = MAIN_MISC_OPCODE;
  2662. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  2663. // Issue the command
  2664. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2665. nsg = *(uint8_t *)adapter->ibuf;
  2666. }
  2667. else {
  2668. nsg = MBOX_DEFAULT_SG_SIZE;
  2669. }
  2670. if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
  2671. return nsg;
  2672. }
  2673. /**
  2674. * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
  2675. * @adapter - soft state for the controller
  2676. *
  2677. * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
  2678. * can be exported as regular SCSI channels
  2679. */
  2680. static void
  2681. megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
  2682. {
  2683. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2684. mbox_t *mbox;
  2685. uint8_t raw_mbox[sizeof(mbox_t)];
  2686. mbox = (mbox_t *)raw_mbox;
  2687. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2688. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2689. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2690. raw_mbox[0] = CHNL_CLASS;
  2691. raw_mbox[2] = GET_CHNL_CLASS;
  2692. // Issue the command. If the command fails, all channels are RAID
  2693. // channels
  2694. raid_dev->channel_class = 0xFF;
  2695. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2696. raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
  2697. }
  2698. return;
  2699. }
  2700. /**
  2701. * megaraid_mbox_flush_cache - flush adapter and disks cache
  2702. * @param adapter : soft state for the controller
  2703. *
  2704. * Flush adapter cache followed by disks cache
  2705. */
  2706. static void
  2707. megaraid_mbox_flush_cache(adapter_t *adapter)
  2708. {
  2709. mbox_t *mbox;
  2710. uint8_t raw_mbox[sizeof(mbox_t)];
  2711. mbox = (mbox_t *)raw_mbox;
  2712. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2713. raw_mbox[0] = FLUSH_ADAPTER;
  2714. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2715. con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
  2716. }
  2717. raw_mbox[0] = FLUSH_SYSTEM;
  2718. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2719. con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
  2720. }
  2721. return;
  2722. }
  2723. /**
  2724. * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
  2725. * @param adapter : controllers' soft state
  2726. * @param scb : SCB to be displayed
  2727. * @param level : debug level for console print
  2728. *
  2729. * Diplay information about the given SCB iff the current debug level is
  2730. * verbose
  2731. */
  2732. static void
  2733. megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
  2734. {
  2735. mbox_ccb_t *ccb;
  2736. struct scsi_cmnd *scp;
  2737. mbox_t *mbox;
  2738. int level;
  2739. int i;
  2740. ccb = (mbox_ccb_t *)scb->ccb;
  2741. scp = scb->scp;
  2742. mbox = ccb->mbox;
  2743. level = CL_DLEVEL3;
  2744. con_log(level, (KERN_NOTICE
  2745. "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
  2746. mbox->cmd, scb->sno));
  2747. con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
  2748. mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
  2749. mbox->numsge));
  2750. if (!scp) return;
  2751. con_log(level, (KERN_NOTICE "scsi cmnd: "));
  2752. for (i = 0; i < scp->cmd_len; i++) {
  2753. con_log(level, ("%#2.02x ", scp->cmnd[i]));
  2754. }
  2755. con_log(level, ("\n"));
  2756. return;
  2757. }
  2758. /**
  2759. * megaraid_mbox_setup_device_map - manage device ids
  2760. * @adapter : Driver's soft state
  2761. *
  2762. * Manange the device ids to have an appropraite mapping between the kernel
  2763. * scsi addresses and megaraid scsi and logical drive addresses. We export
  2764. * scsi devices on their actual addresses, whereas the logical drives are
  2765. * exported on a virtual scsi channel.
  2766. **/
  2767. static void
  2768. megaraid_mbox_setup_device_map(adapter_t *adapter)
  2769. {
  2770. uint8_t c;
  2771. uint8_t t;
  2772. /*
  2773. * First fill the values on the logical drive channel
  2774. */
  2775. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2776. adapter->device_ids[adapter->max_channel][t] =
  2777. (t < adapter->init_id) ? t : t - 1;
  2778. adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
  2779. /*
  2780. * Fill the values on the physical devices channels
  2781. */
  2782. for (c = 0; c < adapter->max_channel; c++)
  2783. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2784. adapter->device_ids[c][t] = (c << 8) | t;
  2785. }
  2786. /*
  2787. * END: internal commands library
  2788. */
  2789. /*
  2790. * START: Interface for the common management module
  2791. *
  2792. * This is the module, which interfaces with the common mangement module to
  2793. * provide support for ioctl and sysfs
  2794. */
  2795. /**
  2796. * megaraid_cmm_register - register with the mangement module
  2797. * @param adapter : HBA soft state
  2798. *
  2799. * Register with the management module, which allows applications to issue
  2800. * ioctl calls to the drivers. This interface is used by the management module
  2801. * to setup sysfs support as well.
  2802. */
  2803. static int
  2804. megaraid_cmm_register(adapter_t *adapter)
  2805. {
  2806. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2807. mraid_mmadp_t adp;
  2808. scb_t *scb;
  2809. mbox_ccb_t *ccb;
  2810. int rval;
  2811. int i;
  2812. // Allocate memory for the base list of scb for management module.
  2813. adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
  2814. GFP_KERNEL);
  2815. if (adapter->uscb_list == NULL) {
  2816. con_log(CL_ANN, (KERN_WARNING
  2817. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2818. __LINE__));
  2819. return -1;
  2820. }
  2821. memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
  2822. // Initialize the synchronization parameters for resources for
  2823. // commands for management module
  2824. INIT_LIST_HEAD(&adapter->uscb_pool);
  2825. spin_lock_init(USER_FREE_LIST_LOCK(adapter));
  2826. // link all the packets. Note, CCB for commands, coming from the
  2827. // commom management module, mailbox physical address are already
  2828. // setup by it. We just need placeholder for that in our local command
  2829. // control blocks
  2830. for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
  2831. scb = adapter->uscb_list + i;
  2832. ccb = raid_dev->uccb_list + i;
  2833. scb->ccb = (caddr_t)ccb;
  2834. ccb->mbox64 = raid_dev->umbox64 + i;
  2835. ccb->mbox = &ccb->mbox64->mbox32;
  2836. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  2837. scb->gp = 0;
  2838. // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
  2839. // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
  2840. scb->sno = i + MBOX_MAX_SCSI_CMDS;
  2841. scb->scp = NULL;
  2842. scb->state = SCB_FREE;
  2843. scb->dma_direction = PCI_DMA_NONE;
  2844. scb->dma_type = MRAID_DMA_NONE;
  2845. scb->dev_channel = -1;
  2846. scb->dev_target = -1;
  2847. // put scb in the free pool
  2848. list_add_tail(&scb->list, &adapter->uscb_pool);
  2849. }
  2850. adp.unique_id = adapter->unique_id;
  2851. adp.drvr_type = DRVRTYPE_MBOX;
  2852. adp.drvr_data = (unsigned long)adapter;
  2853. adp.pdev = adapter->pdev;
  2854. adp.issue_uioc = megaraid_mbox_mm_handler;
  2855. adp.timeout = 300;
  2856. adp.max_kioc = MBOX_MAX_USER_CMDS;
  2857. if ((rval = mraid_mm_register_adp(&adp)) != 0) {
  2858. con_log(CL_ANN, (KERN_WARNING
  2859. "megaraid mbox: did not register with CMM\n"));
  2860. kfree(adapter->uscb_list);
  2861. }
  2862. return rval;
  2863. }
  2864. /**
  2865. * megaraid_cmm_unregister - un-register with the mangement module
  2866. * @param adapter : HBA soft state
  2867. *
  2868. * Un-register with the management module.
  2869. * FIXME: mgmt module must return failure for unregister if it has pending
  2870. * commands in LLD
  2871. */
  2872. static int
  2873. megaraid_cmm_unregister(adapter_t *adapter)
  2874. {
  2875. kfree(adapter->uscb_list);
  2876. mraid_mm_unregister_adp(adapter->unique_id);
  2877. return 0;
  2878. }
  2879. /**
  2880. * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
  2881. * @param drvr_data : LLD specific data
  2882. * @param kioc : CMM interface packet
  2883. * @param action : command action
  2884. *
  2885. * This routine is invoked whenever the Common Mangement Module (CMM) has a
  2886. * command for us. The 'action' parameter specifies if this is a new command
  2887. * or otherwise.
  2888. */
  2889. static int
  2890. megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
  2891. {
  2892. adapter_t *adapter;
  2893. if (action != IOCTL_ISSUE) {
  2894. con_log(CL_ANN, (KERN_WARNING
  2895. "megaraid: unsupported management action:%#2x\n",
  2896. action));
  2897. return (-ENOTSUPP);
  2898. }
  2899. adapter = (adapter_t *)drvr_data;
  2900. // make sure this adapter is not being detached right now.
  2901. if (atomic_read(&adapter->being_detached)) {
  2902. con_log(CL_ANN, (KERN_WARNING
  2903. "megaraid: reject management request, detaching\n"));
  2904. return (-ENODEV);
  2905. }
  2906. switch (kioc->opcode) {
  2907. case GET_ADAP_INFO:
  2908. kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
  2909. (unsigned long)kioc->buf_vaddr);
  2910. kioc->done(kioc);
  2911. return kioc->status;
  2912. case MBOX_CMD:
  2913. return megaraid_mbox_mm_command(adapter, kioc);
  2914. default:
  2915. kioc->status = (-EINVAL);
  2916. kioc->done(kioc);
  2917. return (-EINVAL);
  2918. }
  2919. return 0; // not reached
  2920. }
  2921. /**
  2922. * megaraid_mbox_mm_command - issues commands routed through CMM
  2923. * @param adapter : HBA soft state
  2924. * @param kioc : management command packet
  2925. *
  2926. * Issues commands, which are routed through the management module.
  2927. */
  2928. static int
  2929. megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
  2930. {
  2931. struct list_head *head = &adapter->uscb_pool;
  2932. mbox64_t *mbox64;
  2933. uint8_t *raw_mbox;
  2934. scb_t *scb;
  2935. mbox_ccb_t *ccb;
  2936. unsigned long flags;
  2937. // detach one scb from free pool
  2938. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2939. if (list_empty(head)) { // should never happen because of CMM
  2940. con_log(CL_ANN, (KERN_WARNING
  2941. "megaraid mbox: bug in cmm handler, lost resources\n"));
  2942. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2943. return (-EINVAL);
  2944. }
  2945. scb = list_entry(head->next, scb_t, list);
  2946. list_del_init(&scb->list);
  2947. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2948. scb->state = SCB_ACTIVE;
  2949. scb->dma_type = MRAID_DMA_NONE;
  2950. scb->dma_direction = PCI_DMA_NONE;
  2951. ccb = (mbox_ccb_t *)scb->ccb;
  2952. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2953. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2954. memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
  2955. scb->gp = (unsigned long)kioc;
  2956. /*
  2957. * If it is a logdrv random delete operation, we have to wait till
  2958. * there are no outstanding cmds at the fw and then issue it directly
  2959. */
  2960. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2961. if (wait_till_fw_empty(adapter)) {
  2962. con_log(CL_ANN, (KERN_NOTICE
  2963. "megaraid mbox: LD delete, timed out\n"));
  2964. kioc->status = -ETIME;
  2965. scb->status = -1;
  2966. megaraid_mbox_mm_done(adapter, scb);
  2967. return (-ETIME);
  2968. }
  2969. INIT_LIST_HEAD(&scb->list);
  2970. scb->state = SCB_ISSUED;
  2971. if (mbox_post_cmd(adapter, scb) != 0) {
  2972. con_log(CL_ANN, (KERN_NOTICE
  2973. "megaraid mbox: LD delete, mailbox busy\n"));
  2974. kioc->status = -EBUSY;
  2975. scb->status = -1;
  2976. megaraid_mbox_mm_done(adapter, scb);
  2977. return (-EBUSY);
  2978. }
  2979. return 0;
  2980. }
  2981. // put the command on the pending list and execute
  2982. megaraid_mbox_runpendq(adapter, scb);
  2983. return 0;
  2984. }
  2985. static int
  2986. wait_till_fw_empty(adapter_t *adapter)
  2987. {
  2988. unsigned long flags = 0;
  2989. int i;
  2990. /*
  2991. * Set the quiescent flag to stop issuing cmds to FW.
  2992. */
  2993. spin_lock_irqsave(adapter->host_lock, flags);
  2994. adapter->quiescent++;
  2995. spin_unlock_irqrestore(adapter->host_lock, flags);
  2996. /*
  2997. * Wait till there are no more cmds outstanding at FW. Try for at most
  2998. * 60 seconds
  2999. */
  3000. for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
  3001. con_log(CL_DLEVEL1, (KERN_INFO
  3002. "megaraid: FW has %d pending commands\n",
  3003. adapter->outstanding_cmds));
  3004. msleep(1000);
  3005. }
  3006. return adapter->outstanding_cmds;
  3007. }
  3008. /**
  3009. * megaraid_mbox_mm_done - callback for CMM commands
  3010. * @adapter : HBA soft state
  3011. * @scb : completed command
  3012. *
  3013. * Callback routine for internal commands originated from the management
  3014. * module.
  3015. */
  3016. static void
  3017. megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
  3018. {
  3019. uioc_t *kioc;
  3020. mbox64_t *mbox64;
  3021. uint8_t *raw_mbox;
  3022. unsigned long flags;
  3023. kioc = (uioc_t *)scb->gp;
  3024. kioc->status = 0;
  3025. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  3026. mbox64->mbox32.status = scb->status;
  3027. raw_mbox = (uint8_t *)&mbox64->mbox32;
  3028. // put scb in the free pool
  3029. scb->state = SCB_FREE;
  3030. scb->scp = NULL;
  3031. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  3032. list_add(&scb->list, &adapter->uscb_pool);
  3033. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  3034. // if a delete logical drive operation succeeded, restart the
  3035. // controller
  3036. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  3037. adapter->quiescent--;
  3038. megaraid_mbox_runpendq(adapter, NULL);
  3039. }
  3040. kioc->done(kioc);
  3041. return;
  3042. }
  3043. /**
  3044. * gather_hbainfo - HBA characteristics for the applications
  3045. * @param adapter : HBA soft state
  3046. * @param hinfo : pointer to the caller's host info strucuture
  3047. */
  3048. static int
  3049. gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
  3050. {
  3051. uint8_t dmajor;
  3052. dmajor = megaraid_mbox_version[0];
  3053. hinfo->pci_vendor_id = adapter->pdev->vendor;
  3054. hinfo->pci_device_id = adapter->pdev->device;
  3055. hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
  3056. hinfo->subsys_device_id = adapter->pdev->subsystem_device;
  3057. hinfo->pci_bus = adapter->pdev->bus->number;
  3058. hinfo->pci_dev_fn = adapter->pdev->devfn;
  3059. hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
  3060. hinfo->irq = adapter->host->irq;
  3061. hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
  3062. hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
  3063. hinfo->host_no = adapter->host->host_no;
  3064. return 0;
  3065. }
  3066. /*
  3067. * END: Interface for the common management module
  3068. */
  3069. /**
  3070. * megaraid_sysfs_alloc_resources - allocate sysfs related resources
  3071. *
  3072. * Allocate packets required to issue FW calls whenever the sysfs attributes
  3073. * are read. These attributes would require up-to-date information from the
  3074. * FW. Also set up resources for mutual exclusion to share these resources and
  3075. * the wait queue.
  3076. *
  3077. * @param adapter : controller's soft state
  3078. *
  3079. * @return 0 on success
  3080. * @return -ERROR_CODE on failure
  3081. */
  3082. static int
  3083. megaraid_sysfs_alloc_resources(adapter_t *adapter)
  3084. {
  3085. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3086. int rval = 0;
  3087. raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
  3088. raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
  3089. raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
  3090. PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
  3091. if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
  3092. !raid_dev->sysfs_buffer) {
  3093. con_log(CL_ANN, (KERN_WARNING
  3094. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  3095. __LINE__));
  3096. rval = -ENOMEM;
  3097. megaraid_sysfs_free_resources(adapter);
  3098. }
  3099. sema_init(&raid_dev->sysfs_sem, 1);
  3100. init_waitqueue_head(&raid_dev->sysfs_wait_q);
  3101. return rval;
  3102. }
  3103. /**
  3104. * megaraid_sysfs_free_resources - free sysfs related resources
  3105. *
  3106. * Free packets allocated for sysfs FW commands
  3107. *
  3108. * @param adapter : controller's soft state
  3109. */
  3110. static void
  3111. megaraid_sysfs_free_resources(adapter_t *adapter)
  3112. {
  3113. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3114. if (raid_dev->sysfs_uioc) kfree(raid_dev->sysfs_uioc);
  3115. if (raid_dev->sysfs_mbox64) kfree(raid_dev->sysfs_mbox64);
  3116. if (raid_dev->sysfs_buffer) {
  3117. pci_free_consistent(adapter->pdev, PAGE_SIZE,
  3118. raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
  3119. }
  3120. }
  3121. /**
  3122. * megaraid_sysfs_get_ldmap_done - callback for get ldmap
  3123. *
  3124. * Callback routine called in the ISR/tasklet context for get ldmap call
  3125. *
  3126. * @param uioc : completed packet
  3127. */
  3128. static void
  3129. megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
  3130. {
  3131. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3132. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3133. uioc->status = 0;
  3134. wake_up(&raid_dev->sysfs_wait_q);
  3135. }
  3136. /**
  3137. * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
  3138. *
  3139. * Timeout routine to recover and return to application, in case the adapter
  3140. * has stopped responding. A timeout of 60 seconds for this command seem like
  3141. * a good value
  3142. *
  3143. * @param uioc : timed out packet
  3144. */
  3145. static void
  3146. megaraid_sysfs_get_ldmap_timeout(unsigned long data)
  3147. {
  3148. uioc_t *uioc = (uioc_t *)data;
  3149. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3150. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3151. uioc->status = -ETIME;
  3152. wake_up(&raid_dev->sysfs_wait_q);
  3153. }
  3154. /**
  3155. * megaraid_sysfs_get_ldmap - get update logical drive map
  3156. *
  3157. * This routine will be called whenever user reads the logical drive
  3158. * attributes, go get the current logical drive mapping table from the
  3159. * firmware. We use the managment API's to issue commands to the controller.
  3160. *
  3161. * NOTE: The commands issuance functionality is not generalized and
  3162. * implemented in context of "get ld map" command only. If required, the
  3163. * command issuance logical can be trivially pulled out and implemented as a
  3164. * standalone libary. For now, this should suffice since there is no other
  3165. * user of this interface.
  3166. *
  3167. * @param adapter : controller's soft state
  3168. *
  3169. * @return 0 on success
  3170. * @return -1 on failure
  3171. */
  3172. static int
  3173. megaraid_sysfs_get_ldmap(adapter_t *adapter)
  3174. {
  3175. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3176. uioc_t *uioc;
  3177. mbox64_t *mbox64;
  3178. mbox_t *mbox;
  3179. char *raw_mbox;
  3180. struct timer_list sysfs_timer;
  3181. struct timer_list *timerp;
  3182. caddr_t ldmap;
  3183. int rval = 0;
  3184. /*
  3185. * Allow only one read at a time to go through the sysfs attributes
  3186. */
  3187. down(&raid_dev->sysfs_sem);
  3188. uioc = raid_dev->sysfs_uioc;
  3189. mbox64 = raid_dev->sysfs_mbox64;
  3190. ldmap = raid_dev->sysfs_buffer;
  3191. memset(uioc, 0, sizeof(uioc_t));
  3192. memset(mbox64, 0, sizeof(mbox64_t));
  3193. memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
  3194. mbox = &mbox64->mbox32;
  3195. raw_mbox = (char *)mbox;
  3196. uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
  3197. uioc->buf_vaddr = (caddr_t)adapter;
  3198. uioc->status = -ENODATA;
  3199. uioc->done = megaraid_sysfs_get_ldmap_done;
  3200. /*
  3201. * Prepare the mailbox packet to get the current logical drive mapping
  3202. * table
  3203. */
  3204. mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
  3205. raw_mbox[0] = FC_DEL_LOGDRV;
  3206. raw_mbox[2] = OP_GET_LDID_MAP;
  3207. /*
  3208. * Setup a timer to recover from a non-responding controller
  3209. */
  3210. timerp = &sysfs_timer;
  3211. init_timer(timerp);
  3212. timerp->function = megaraid_sysfs_get_ldmap_timeout;
  3213. timerp->data = (unsigned long)uioc;
  3214. timerp->expires = jiffies + 60 * HZ;
  3215. add_timer(timerp);
  3216. /*
  3217. * Send the command to the firmware
  3218. */
  3219. rval = megaraid_mbox_mm_command(adapter, uioc);
  3220. if (rval == 0) { // command successfully issued
  3221. wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
  3222. /*
  3223. * Check if the command timed out
  3224. */
  3225. if (uioc->status == -ETIME) {
  3226. con_log(CL_ANN, (KERN_NOTICE
  3227. "megaraid: sysfs get ld map timed out\n"));
  3228. rval = -ETIME;
  3229. }
  3230. else {
  3231. rval = mbox->status;
  3232. }
  3233. if (rval == 0) {
  3234. memcpy(raid_dev->curr_ldmap, ldmap,
  3235. sizeof(raid_dev->curr_ldmap));
  3236. }
  3237. else {
  3238. con_log(CL_ANN, (KERN_NOTICE
  3239. "megaraid: get ld map failed with %x\n", rval));
  3240. }
  3241. }
  3242. else {
  3243. con_log(CL_ANN, (KERN_NOTICE
  3244. "megaraid: could not issue ldmap command:%x\n", rval));
  3245. }
  3246. del_timer_sync(timerp);
  3247. up(&raid_dev->sysfs_sem);
  3248. return rval;
  3249. }
  3250. /**
  3251. * megaraid_sysfs_show_app_hndl - display application handle for this adapter
  3252. *
  3253. * Display the handle used by the applications while executing management
  3254. * tasks on the adapter. We invoke a management module API to get the adapter
  3255. * handle, since we do not interface with applications directly.
  3256. *
  3257. * @param cdev : class device object representation for the host
  3258. * @param buf : buffer to send data to
  3259. */
  3260. static ssize_t
  3261. megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
  3262. {
  3263. struct Scsi_Host *shost = class_to_shost(cdev);
  3264. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
  3265. uint32_t app_hndl;
  3266. app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
  3267. return snprintf(buf, 8, "%u\n", app_hndl);
  3268. }
  3269. /**
  3270. * megaraid_sysfs_show_ldnum - display the logical drive number for this device
  3271. *
  3272. * Display the logical drive number for the device in question, if it a valid
  3273. * logical drive. For physical devices, "-1" is returned
  3274. * The logical drive number is displayed in following format
  3275. *
  3276. * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
  3277. * <int> <int> <int> <int>
  3278. *
  3279. * @param dev : device object representation for the scsi device
  3280. * @param buf : buffer to send data to
  3281. */
  3282. static ssize_t
  3283. megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
  3284. {
  3285. struct scsi_device *sdev = to_scsi_device(dev);
  3286. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
  3287. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3288. int scsi_id = -1;
  3289. int logical_drv = -1;
  3290. int ldid_map = -1;
  3291. uint32_t app_hndl = 0;
  3292. int mapped_sdev_id;
  3293. int rval;
  3294. int i;
  3295. if (raid_dev->random_del_supported &&
  3296. MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
  3297. rval = megaraid_sysfs_get_ldmap(adapter);
  3298. if (rval == 0) {
  3299. for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
  3300. mapped_sdev_id = sdev->id;
  3301. if (sdev->id > adapter->init_id) {
  3302. mapped_sdev_id -= 1;
  3303. }
  3304. if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
  3305. scsi_id = sdev->id;
  3306. logical_drv = i;
  3307. ldid_map = raid_dev->curr_ldmap[i];
  3308. app_hndl = mraid_mm_adapter_app_handle(
  3309. adapter->unique_id);
  3310. break;
  3311. }
  3312. }
  3313. }
  3314. else {
  3315. con_log(CL_ANN, (KERN_NOTICE
  3316. "megaraid: sysfs get ld map failed: %x\n",
  3317. rval));
  3318. }
  3319. }
  3320. return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
  3321. ldid_map, app_hndl);
  3322. }
  3323. /*
  3324. * END: Mailbox Low Level Driver
  3325. */
  3326. module_init(megaraid_init);
  3327. module_exit(megaraid_exit);
  3328. /* vim: set ts=8 sw=8 tw=78 ai si: */