megaraid_mbox.c 97 KB

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