hpsa.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/fs.h>
  29. #include <linux/timer.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/compat.h>
  35. #include <linux/blktrace_api.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/io.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/completion.h>
  40. #include <linux/moduleparam.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_host.h>
  45. #include <linux/cciss_ioctl.h>
  46. #include <linux/string.h>
  47. #include <linux/bitmap.h>
  48. #include <asm/atomic.h>
  49. #include <linux/kthread.h>
  50. #include "hpsa_cmd.h"
  51. #include "hpsa.h"
  52. /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
  53. #define HPSA_DRIVER_VERSION "1.0.0"
  54. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  55. /* How long to wait (in milliseconds) for board to go into simple mode */
  56. #define MAX_CONFIG_WAIT 30000
  57. #define MAX_IOCTL_CONFIG_WAIT 1000
  58. /*define how many times we will try a command because of bus resets */
  59. #define MAX_CMD_RETRIES 3
  60. /* Embedded module documentation macros - see modules.h */
  61. MODULE_AUTHOR("Hewlett-Packard Company");
  62. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  63. HPSA_DRIVER_VERSION);
  64. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  65. MODULE_VERSION(HPSA_DRIVER_VERSION);
  66. MODULE_LICENSE("GPL");
  67. static int hpsa_allow_any;
  68. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  69. MODULE_PARM_DESC(hpsa_allow_any,
  70. "Allow hpsa driver to access unknown HP Smart Array hardware");
  71. /* define the PCI info for the cards we can control */
  72. static const struct pci_device_id hpsa_pci_device_id[] = {
  73. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
  74. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
  75. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
  76. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  77. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  78. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  79. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  80. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  81. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
  82. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
  83. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  84. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  85. {0,}
  86. };
  87. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  88. /* board_id = Subsystem Device ID & Vendor ID
  89. * product = Marketing Name for the board
  90. * access = Address of the struct of function pointers
  91. */
  92. static struct board_type products[] = {
  93. {0x3223103C, "Smart Array P800", &SA5_access},
  94. {0x3234103C, "Smart Array P400", &SA5_access},
  95. {0x323d103c, "Smart Array P700M", &SA5_access},
  96. {0x3241103C, "Smart Array P212", &SA5_access},
  97. {0x3243103C, "Smart Array P410", &SA5_access},
  98. {0x3245103C, "Smart Array P410i", &SA5_access},
  99. {0x3247103C, "Smart Array P411", &SA5_access},
  100. {0x3249103C, "Smart Array P812", &SA5_access},
  101. {0x324a103C, "Smart Array P712m", &SA5_access},
  102. {0x324b103C, "Smart Array P711m", &SA5_access},
  103. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  104. };
  105. static int number_of_controllers;
  106. static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
  107. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
  108. static void start_io(struct ctlr_info *h);
  109. #ifdef CONFIG_COMPAT
  110. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
  111. #endif
  112. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  113. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
  114. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  115. static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
  116. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  117. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  118. int cmd_type);
  119. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  120. void (*done)(struct scsi_cmnd *));
  121. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  122. static int hpsa_slave_alloc(struct scsi_device *sdev);
  123. static void hpsa_slave_destroy(struct scsi_device *sdev);
  124. static ssize_t raid_level_show(struct device *dev,
  125. struct device_attribute *attr, char *buf);
  126. static ssize_t lunid_show(struct device *dev,
  127. struct device_attribute *attr, char *buf);
  128. static ssize_t unique_id_show(struct device *dev,
  129. struct device_attribute *attr, char *buf);
  130. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
  131. static ssize_t host_store_rescan(struct device *dev,
  132. struct device_attribute *attr, const char *buf, size_t count);
  133. static int check_for_unit_attention(struct ctlr_info *h,
  134. struct CommandList *c);
  135. static void check_ioctl_unit_attention(struct ctlr_info *h,
  136. struct CommandList *c);
  137. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  138. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  139. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  140. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  141. static struct device_attribute *hpsa_sdev_attrs[] = {
  142. &dev_attr_raid_level,
  143. &dev_attr_lunid,
  144. &dev_attr_unique_id,
  145. NULL,
  146. };
  147. static struct device_attribute *hpsa_shost_attrs[] = {
  148. &dev_attr_rescan,
  149. NULL,
  150. };
  151. static struct scsi_host_template hpsa_driver_template = {
  152. .module = THIS_MODULE,
  153. .name = "hpsa",
  154. .proc_name = "hpsa",
  155. .queuecommand = hpsa_scsi_queue_command,
  156. .can_queue = 512,
  157. .this_id = -1,
  158. .sg_tablesize = MAXSGENTRIES,
  159. .cmd_per_lun = 512,
  160. .use_clustering = ENABLE_CLUSTERING,
  161. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  162. .ioctl = hpsa_ioctl,
  163. .slave_alloc = hpsa_slave_alloc,
  164. .slave_destroy = hpsa_slave_destroy,
  165. #ifdef CONFIG_COMPAT
  166. .compat_ioctl = hpsa_compat_ioctl,
  167. #endif
  168. .sdev_attrs = hpsa_sdev_attrs,
  169. .shost_attrs = hpsa_shost_attrs,
  170. };
  171. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  172. {
  173. unsigned long *priv = shost_priv(sdev->host);
  174. return (struct ctlr_info *) *priv;
  175. }
  176. static struct task_struct *hpsa_scan_thread;
  177. static DEFINE_MUTEX(hpsa_scan_mutex);
  178. static LIST_HEAD(hpsa_scan_q);
  179. static int hpsa_scan_func(void *data);
  180. /**
  181. * add_to_scan_list() - add controller to rescan queue
  182. * @h: Pointer to the controller.
  183. *
  184. * Adds the controller to the rescan queue if not already on the queue.
  185. *
  186. * returns 1 if added to the queue, 0 if skipped (could be on the
  187. * queue already, or the controller could be initializing or shutting
  188. * down).
  189. **/
  190. static int add_to_scan_list(struct ctlr_info *h)
  191. {
  192. struct ctlr_info *test_h;
  193. int found = 0;
  194. int ret = 0;
  195. if (h->busy_initializing)
  196. return 0;
  197. /*
  198. * If we don't get the lock, it means the driver is unloading
  199. * and there's no point in scheduling a new scan.
  200. */
  201. if (!mutex_trylock(&h->busy_shutting_down))
  202. return 0;
  203. mutex_lock(&hpsa_scan_mutex);
  204. list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
  205. if (test_h == h) {
  206. found = 1;
  207. break;
  208. }
  209. }
  210. if (!found && !h->busy_scanning) {
  211. INIT_COMPLETION(h->scan_wait);
  212. list_add_tail(&h->scan_list, &hpsa_scan_q);
  213. ret = 1;
  214. }
  215. mutex_unlock(&hpsa_scan_mutex);
  216. mutex_unlock(&h->busy_shutting_down);
  217. return ret;
  218. }
  219. /**
  220. * remove_from_scan_list() - remove controller from rescan queue
  221. * @h: Pointer to the controller.
  222. *
  223. * Removes the controller from the rescan queue if present. Blocks if
  224. * the controller is currently conducting a rescan. The controller
  225. * can be in one of three states:
  226. * 1. Doesn't need a scan
  227. * 2. On the scan list, but not scanning yet (we remove it)
  228. * 3. Busy scanning (and not on the list). In this case we want to wait for
  229. * the scan to complete to make sure the scanning thread for this
  230. * controller is completely idle.
  231. **/
  232. static void remove_from_scan_list(struct ctlr_info *h)
  233. {
  234. struct ctlr_info *test_h, *tmp_h;
  235. mutex_lock(&hpsa_scan_mutex);
  236. list_for_each_entry_safe(test_h, tmp_h, &hpsa_scan_q, scan_list) {
  237. if (test_h == h) { /* state 2. */
  238. list_del(&h->scan_list);
  239. complete_all(&h->scan_wait);
  240. mutex_unlock(&hpsa_scan_mutex);
  241. return;
  242. }
  243. }
  244. if (h->busy_scanning) { /* state 3. */
  245. mutex_unlock(&hpsa_scan_mutex);
  246. wait_for_completion(&h->scan_wait);
  247. } else { /* state 1, nothing to do. */
  248. mutex_unlock(&hpsa_scan_mutex);
  249. }
  250. }
  251. /* hpsa_scan_func() - kernel thread used to rescan controllers
  252. * @data: Ignored.
  253. *
  254. * A kernel thread used scan for drive topology changes on
  255. * controllers. The thread processes only one controller at a time
  256. * using a queue. Controllers are added to the queue using
  257. * add_to_scan_list() and removed from the queue either after done
  258. * processing or using remove_from_scan_list().
  259. *
  260. * returns 0.
  261. **/
  262. static int hpsa_scan_func(__attribute__((unused)) void *data)
  263. {
  264. struct ctlr_info *h;
  265. int host_no;
  266. while (1) {
  267. set_current_state(TASK_INTERRUPTIBLE);
  268. schedule();
  269. if (kthread_should_stop())
  270. break;
  271. while (1) {
  272. mutex_lock(&hpsa_scan_mutex);
  273. if (list_empty(&hpsa_scan_q)) {
  274. mutex_unlock(&hpsa_scan_mutex);
  275. break;
  276. }
  277. h = list_entry(hpsa_scan_q.next, struct ctlr_info,
  278. scan_list);
  279. list_del(&h->scan_list);
  280. h->busy_scanning = 1;
  281. mutex_unlock(&hpsa_scan_mutex);
  282. host_no = h->scsi_host ? h->scsi_host->host_no : -1;
  283. hpsa_update_scsi_devices(h, host_no);
  284. complete_all(&h->scan_wait);
  285. mutex_lock(&hpsa_scan_mutex);
  286. h->busy_scanning = 0;
  287. mutex_unlock(&hpsa_scan_mutex);
  288. }
  289. }
  290. return 0;
  291. }
  292. static int check_for_unit_attention(struct ctlr_info *h,
  293. struct CommandList *c)
  294. {
  295. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  296. return 0;
  297. switch (c->err_info->SenseInfo[12]) {
  298. case STATE_CHANGED:
  299. dev_warn(&h->pdev->dev, "hpsa%d: a state change "
  300. "detected, command retried\n", h->ctlr);
  301. break;
  302. case LUN_FAILED:
  303. dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
  304. "detected, action required\n", h->ctlr);
  305. break;
  306. case REPORT_LUNS_CHANGED:
  307. dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
  308. "changed\n", h->ctlr);
  309. /*
  310. * Here, we could call add_to_scan_list and wake up the scan thread,
  311. * except that it's quite likely that we will get more than one
  312. * REPORT_LUNS_CHANGED condition in quick succession, which means
  313. * that those which occur after the first one will likely happen
  314. * *during* the hpsa_scan_thread's rescan. And the rescan code is not
  315. * robust enough to restart in the middle, undoing what it has already
  316. * done, and it's not clear that it's even possible to do this, since
  317. * part of what it does is notify the SCSI mid layer, which starts
  318. * doing it's own i/o to read partition tables and so on, and the
  319. * driver doesn't have visibility to know what might need undoing.
  320. * In any event, if possible, it is horribly complicated to get right
  321. * so we just don't do it for now.
  322. *
  323. * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
  324. */
  325. break;
  326. case POWER_OR_RESET:
  327. dev_warn(&h->pdev->dev, "hpsa%d: a power on "
  328. "or device reset detected\n", h->ctlr);
  329. break;
  330. case UNIT_ATTENTION_CLEARED:
  331. dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
  332. "cleared by another initiator\n", h->ctlr);
  333. break;
  334. default:
  335. dev_warn(&h->pdev->dev, "hpsa%d: unknown "
  336. "unit attention detected\n", h->ctlr);
  337. break;
  338. }
  339. return 1;
  340. }
  341. static ssize_t host_store_rescan(struct device *dev,
  342. struct device_attribute *attr,
  343. const char *buf, size_t count)
  344. {
  345. struct ctlr_info *h;
  346. struct Scsi_Host *shost = class_to_shost(dev);
  347. unsigned long *priv = shost_priv(shost);
  348. h = (struct ctlr_info *) *priv;
  349. if (add_to_scan_list(h)) {
  350. wake_up_process(hpsa_scan_thread);
  351. wait_for_completion_interruptible(&h->scan_wait);
  352. }
  353. return count;
  354. }
  355. /* Enqueuing and dequeuing functions for cmdlists. */
  356. static inline void addQ(struct hlist_head *list, struct CommandList *c)
  357. {
  358. hlist_add_head(&c->list, list);
  359. }
  360. static void enqueue_cmd_and_start_io(struct ctlr_info *h,
  361. struct CommandList *c)
  362. {
  363. unsigned long flags;
  364. spin_lock_irqsave(&h->lock, flags);
  365. addQ(&h->reqQ, c);
  366. h->Qdepth++;
  367. start_io(h);
  368. spin_unlock_irqrestore(&h->lock, flags);
  369. }
  370. static inline void removeQ(struct CommandList *c)
  371. {
  372. if (WARN_ON(hlist_unhashed(&c->list)))
  373. return;
  374. hlist_del_init(&c->list);
  375. }
  376. static inline int is_hba_lunid(unsigned char scsi3addr[])
  377. {
  378. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  379. }
  380. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  381. {
  382. return (scsi3addr[3] & 0xC0) == 0x40;
  383. }
  384. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  385. "UNKNOWN"
  386. };
  387. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
  388. static ssize_t raid_level_show(struct device *dev,
  389. struct device_attribute *attr, char *buf)
  390. {
  391. ssize_t l = 0;
  392. unsigned char rlevel;
  393. struct ctlr_info *h;
  394. struct scsi_device *sdev;
  395. struct hpsa_scsi_dev_t *hdev;
  396. unsigned long flags;
  397. sdev = to_scsi_device(dev);
  398. h = sdev_to_hba(sdev);
  399. spin_lock_irqsave(&h->lock, flags);
  400. hdev = sdev->hostdata;
  401. if (!hdev) {
  402. spin_unlock_irqrestore(&h->lock, flags);
  403. return -ENODEV;
  404. }
  405. /* Is this even a logical drive? */
  406. if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
  407. spin_unlock_irqrestore(&h->lock, flags);
  408. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  409. return l;
  410. }
  411. rlevel = hdev->raid_level;
  412. spin_unlock_irqrestore(&h->lock, flags);
  413. if (rlevel > RAID_UNKNOWN)
  414. rlevel = RAID_UNKNOWN;
  415. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  416. return l;
  417. }
  418. static ssize_t lunid_show(struct device *dev,
  419. struct device_attribute *attr, char *buf)
  420. {
  421. struct ctlr_info *h;
  422. struct scsi_device *sdev;
  423. struct hpsa_scsi_dev_t *hdev;
  424. unsigned long flags;
  425. unsigned char lunid[8];
  426. sdev = to_scsi_device(dev);
  427. h = sdev_to_hba(sdev);
  428. spin_lock_irqsave(&h->lock, flags);
  429. hdev = sdev->hostdata;
  430. if (!hdev) {
  431. spin_unlock_irqrestore(&h->lock, flags);
  432. return -ENODEV;
  433. }
  434. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  435. spin_unlock_irqrestore(&h->lock, flags);
  436. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  437. lunid[0], lunid[1], lunid[2], lunid[3],
  438. lunid[4], lunid[5], lunid[6], lunid[7]);
  439. }
  440. static ssize_t unique_id_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. struct ctlr_info *h;
  444. struct scsi_device *sdev;
  445. struct hpsa_scsi_dev_t *hdev;
  446. unsigned long flags;
  447. unsigned char sn[16];
  448. sdev = to_scsi_device(dev);
  449. h = sdev_to_hba(sdev);
  450. spin_lock_irqsave(&h->lock, flags);
  451. hdev = sdev->hostdata;
  452. if (!hdev) {
  453. spin_unlock_irqrestore(&h->lock, flags);
  454. return -ENODEV;
  455. }
  456. memcpy(sn, hdev->device_id, sizeof(sn));
  457. spin_unlock_irqrestore(&h->lock, flags);
  458. return snprintf(buf, 16 * 2 + 2,
  459. "%02X%02X%02X%02X%02X%02X%02X%02X"
  460. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  461. sn[0], sn[1], sn[2], sn[3],
  462. sn[4], sn[5], sn[6], sn[7],
  463. sn[8], sn[9], sn[10], sn[11],
  464. sn[12], sn[13], sn[14], sn[15]);
  465. }
  466. static int hpsa_find_target_lun(struct ctlr_info *h,
  467. unsigned char scsi3addr[], int bus, int *target, int *lun)
  468. {
  469. /* finds an unused bus, target, lun for a new physical device
  470. * assumes h->devlock is held
  471. */
  472. int i, found = 0;
  473. DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
  474. memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
  475. for (i = 0; i < h->ndevices; i++) {
  476. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  477. set_bit(h->dev[i]->target, lun_taken);
  478. }
  479. for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
  480. if (!test_bit(i, lun_taken)) {
  481. /* *bus = 1; */
  482. *target = i;
  483. *lun = 0;
  484. found = 1;
  485. break;
  486. }
  487. }
  488. return !found;
  489. }
  490. /* Add an entry into h->dev[] array. */
  491. static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
  492. struct hpsa_scsi_dev_t *device,
  493. struct hpsa_scsi_dev_t *added[], int *nadded)
  494. {
  495. /* assumes h->devlock is held */
  496. int n = h->ndevices;
  497. int i;
  498. unsigned char addr1[8], addr2[8];
  499. struct hpsa_scsi_dev_t *sd;
  500. if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
  501. dev_err(&h->pdev->dev, "too many devices, some will be "
  502. "inaccessible.\n");
  503. return -1;
  504. }
  505. /* physical devices do not have lun or target assigned until now. */
  506. if (device->lun != -1)
  507. /* Logical device, lun is already assigned. */
  508. goto lun_assigned;
  509. /* If this device a non-zero lun of a multi-lun device
  510. * byte 4 of the 8-byte LUN addr will contain the logical
  511. * unit no, zero otherise.
  512. */
  513. if (device->scsi3addr[4] == 0) {
  514. /* This is not a non-zero lun of a multi-lun device */
  515. if (hpsa_find_target_lun(h, device->scsi3addr,
  516. device->bus, &device->target, &device->lun) != 0)
  517. return -1;
  518. goto lun_assigned;
  519. }
  520. /* This is a non-zero lun of a multi-lun device.
  521. * Search through our list and find the device which
  522. * has the same 8 byte LUN address, excepting byte 4.
  523. * Assign the same bus and target for this new LUN.
  524. * Use the logical unit number from the firmware.
  525. */
  526. memcpy(addr1, device->scsi3addr, 8);
  527. addr1[4] = 0;
  528. for (i = 0; i < n; i++) {
  529. sd = h->dev[i];
  530. memcpy(addr2, sd->scsi3addr, 8);
  531. addr2[4] = 0;
  532. /* differ only in byte 4? */
  533. if (memcmp(addr1, addr2, 8) == 0) {
  534. device->bus = sd->bus;
  535. device->target = sd->target;
  536. device->lun = device->scsi3addr[4];
  537. break;
  538. }
  539. }
  540. if (device->lun == -1) {
  541. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  542. " suspect firmware bug or unsupported hardware "
  543. "configuration.\n");
  544. return -1;
  545. }
  546. lun_assigned:
  547. h->dev[n] = device;
  548. h->ndevices++;
  549. added[*nadded] = device;
  550. (*nadded)++;
  551. /* initially, (before registering with scsi layer) we don't
  552. * know our hostno and we don't want to print anything first
  553. * time anyway (the scsi layer's inquiries will show that info)
  554. */
  555. /* if (hostno != -1) */
  556. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
  557. scsi_device_type(device->devtype), hostno,
  558. device->bus, device->target, device->lun);
  559. return 0;
  560. }
  561. /* Remove an entry from h->dev[] array. */
  562. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
  563. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  564. {
  565. /* assumes h->devlock is held */
  566. int i;
  567. struct hpsa_scsi_dev_t *sd;
  568. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  569. sd = h->dev[entry];
  570. removed[*nremoved] = h->dev[entry];
  571. (*nremoved)++;
  572. for (i = entry; i < h->ndevices-1; i++)
  573. h->dev[i] = h->dev[i+1];
  574. h->ndevices--;
  575. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
  576. scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
  577. sd->lun);
  578. }
  579. #define SCSI3ADDR_EQ(a, b) ( \
  580. (a)[7] == (b)[7] && \
  581. (a)[6] == (b)[6] && \
  582. (a)[5] == (b)[5] && \
  583. (a)[4] == (b)[4] && \
  584. (a)[3] == (b)[3] && \
  585. (a)[2] == (b)[2] && \
  586. (a)[1] == (b)[1] && \
  587. (a)[0] == (b)[0])
  588. static void fixup_botched_add(struct ctlr_info *h,
  589. struct hpsa_scsi_dev_t *added)
  590. {
  591. /* called when scsi_add_device fails in order to re-adjust
  592. * h->dev[] to match the mid layer's view.
  593. */
  594. unsigned long flags;
  595. int i, j;
  596. spin_lock_irqsave(&h->lock, flags);
  597. for (i = 0; i < h->ndevices; i++) {
  598. if (h->dev[i] == added) {
  599. for (j = i; j < h->ndevices-1; j++)
  600. h->dev[j] = h->dev[j+1];
  601. h->ndevices--;
  602. break;
  603. }
  604. }
  605. spin_unlock_irqrestore(&h->lock, flags);
  606. kfree(added);
  607. }
  608. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  609. struct hpsa_scsi_dev_t *dev2)
  610. {
  611. if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
  612. (dev1->lun != -1 && dev2->lun != -1)) &&
  613. dev1->devtype != 0x0C)
  614. return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
  615. /* we compare everything except lun and target as these
  616. * are not yet assigned. Compare parts likely
  617. * to differ first
  618. */
  619. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  620. sizeof(dev1->scsi3addr)) != 0)
  621. return 0;
  622. if (memcmp(dev1->device_id, dev2->device_id,
  623. sizeof(dev1->device_id)) != 0)
  624. return 0;
  625. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  626. return 0;
  627. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  628. return 0;
  629. if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
  630. return 0;
  631. if (dev1->devtype != dev2->devtype)
  632. return 0;
  633. if (dev1->raid_level != dev2->raid_level)
  634. return 0;
  635. if (dev1->bus != dev2->bus)
  636. return 0;
  637. return 1;
  638. }
  639. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  640. * and return needle location in *index. If scsi3addr matches, but not
  641. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  642. * location in *index. If needle not found, return DEVICE_NOT_FOUND.
  643. */
  644. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  645. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  646. int *index)
  647. {
  648. int i;
  649. #define DEVICE_NOT_FOUND 0
  650. #define DEVICE_CHANGED 1
  651. #define DEVICE_SAME 2
  652. for (i = 0; i < haystack_size; i++) {
  653. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  654. *index = i;
  655. if (device_is_the_same(needle, haystack[i]))
  656. return DEVICE_SAME;
  657. else
  658. return DEVICE_CHANGED;
  659. }
  660. }
  661. *index = -1;
  662. return DEVICE_NOT_FOUND;
  663. }
  664. static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
  665. struct hpsa_scsi_dev_t *sd[], int nsds)
  666. {
  667. /* sd contains scsi3 addresses and devtypes, and inquiry
  668. * data. This function takes what's in sd to be the current
  669. * reality and updates h->dev[] to reflect that reality.
  670. */
  671. int i, entry, device_change, changes = 0;
  672. struct hpsa_scsi_dev_t *csd;
  673. unsigned long flags;
  674. struct hpsa_scsi_dev_t **added, **removed;
  675. int nadded, nremoved;
  676. struct Scsi_Host *sh = NULL;
  677. added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  678. GFP_KERNEL);
  679. removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  680. GFP_KERNEL);
  681. if (!added || !removed) {
  682. dev_warn(&h->pdev->dev, "out of memory in "
  683. "adjust_hpsa_scsi_table\n");
  684. goto free_and_out;
  685. }
  686. spin_lock_irqsave(&h->devlock, flags);
  687. /* find any devices in h->dev[] that are not in
  688. * sd[] and remove them from h->dev[], and for any
  689. * devices which have changed, remove the old device
  690. * info and add the new device info.
  691. */
  692. i = 0;
  693. nremoved = 0;
  694. nadded = 0;
  695. while (i < h->ndevices) {
  696. csd = h->dev[i];
  697. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  698. if (device_change == DEVICE_NOT_FOUND) {
  699. changes++;
  700. hpsa_scsi_remove_entry(h, hostno, i,
  701. removed, &nremoved);
  702. continue; /* remove ^^^, hence i not incremented */
  703. } else if (device_change == DEVICE_CHANGED) {
  704. changes++;
  705. hpsa_scsi_remove_entry(h, hostno, i,
  706. removed, &nremoved);
  707. (void) hpsa_scsi_add_entry(h, hostno, sd[entry],
  708. added, &nadded);
  709. /* add can't fail, we just removed one. */
  710. sd[entry] = NULL; /* prevent it from being freed */
  711. }
  712. i++;
  713. }
  714. /* Now, make sure every device listed in sd[] is also
  715. * listed in h->dev[], adding them if they aren't found
  716. */
  717. for (i = 0; i < nsds; i++) {
  718. if (!sd[i]) /* if already added above. */
  719. continue;
  720. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  721. h->ndevices, &entry);
  722. if (device_change == DEVICE_NOT_FOUND) {
  723. changes++;
  724. if (hpsa_scsi_add_entry(h, hostno, sd[i],
  725. added, &nadded) != 0)
  726. break;
  727. sd[i] = NULL; /* prevent from being freed later. */
  728. } else if (device_change == DEVICE_CHANGED) {
  729. /* should never happen... */
  730. changes++;
  731. dev_warn(&h->pdev->dev,
  732. "device unexpectedly changed.\n");
  733. /* but if it does happen, we just ignore that device */
  734. }
  735. }
  736. spin_unlock_irqrestore(&h->devlock, flags);
  737. /* Don't notify scsi mid layer of any changes the first time through
  738. * (or if there are no changes) scsi_scan_host will do it later the
  739. * first time through.
  740. */
  741. if (hostno == -1 || !changes)
  742. goto free_and_out;
  743. sh = h->scsi_host;
  744. /* Notify scsi mid layer of any removed devices */
  745. for (i = 0; i < nremoved; i++) {
  746. struct scsi_device *sdev =
  747. scsi_device_lookup(sh, removed[i]->bus,
  748. removed[i]->target, removed[i]->lun);
  749. if (sdev != NULL) {
  750. scsi_remove_device(sdev);
  751. scsi_device_put(sdev);
  752. } else {
  753. /* We don't expect to get here.
  754. * future cmds to this device will get selection
  755. * timeout as if the device was gone.
  756. */
  757. dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
  758. " for removal.", hostno, removed[i]->bus,
  759. removed[i]->target, removed[i]->lun);
  760. }
  761. kfree(removed[i]);
  762. removed[i] = NULL;
  763. }
  764. /* Notify scsi mid layer of any added devices */
  765. for (i = 0; i < nadded; i++) {
  766. if (scsi_add_device(sh, added[i]->bus,
  767. added[i]->target, added[i]->lun) == 0)
  768. continue;
  769. dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
  770. "device not added.\n", hostno, added[i]->bus,
  771. added[i]->target, added[i]->lun);
  772. /* now we have to remove it from h->dev,
  773. * since it didn't get added to scsi mid layer
  774. */
  775. fixup_botched_add(h, added[i]);
  776. }
  777. free_and_out:
  778. kfree(added);
  779. kfree(removed);
  780. }
  781. /*
  782. * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
  783. * Assume's h->devlock is held.
  784. */
  785. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  786. int bus, int target, int lun)
  787. {
  788. int i;
  789. struct hpsa_scsi_dev_t *sd;
  790. for (i = 0; i < h->ndevices; i++) {
  791. sd = h->dev[i];
  792. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  793. return sd;
  794. }
  795. return NULL;
  796. }
  797. /* link sdev->hostdata to our per-device structure. */
  798. static int hpsa_slave_alloc(struct scsi_device *sdev)
  799. {
  800. struct hpsa_scsi_dev_t *sd;
  801. unsigned long flags;
  802. struct ctlr_info *h;
  803. h = sdev_to_hba(sdev);
  804. spin_lock_irqsave(&h->devlock, flags);
  805. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  806. sdev_id(sdev), sdev->lun);
  807. if (sd != NULL)
  808. sdev->hostdata = sd;
  809. spin_unlock_irqrestore(&h->devlock, flags);
  810. return 0;
  811. }
  812. static void hpsa_slave_destroy(struct scsi_device *sdev)
  813. {
  814. /* nothing to do. */
  815. }
  816. static void hpsa_scsi_setup(struct ctlr_info *h)
  817. {
  818. h->ndevices = 0;
  819. h->scsi_host = NULL;
  820. spin_lock_init(&h->devlock);
  821. }
  822. static void complete_scsi_command(struct CommandList *cp,
  823. int timeout, u32 tag)
  824. {
  825. struct scsi_cmnd *cmd;
  826. struct ctlr_info *h;
  827. struct ErrorInfo *ei;
  828. unsigned char sense_key;
  829. unsigned char asc; /* additional sense code */
  830. unsigned char ascq; /* additional sense code qualifier */
  831. ei = cp->err_info;
  832. cmd = (struct scsi_cmnd *) cp->scsi_cmd;
  833. h = cp->h;
  834. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  835. cmd->result = (DID_OK << 16); /* host byte */
  836. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  837. cmd->result |= (ei->ScsiStatus << 1);
  838. /* copy the sense data whether we need to or not. */
  839. memcpy(cmd->sense_buffer, ei->SenseInfo,
  840. ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
  841. SCSI_SENSE_BUFFERSIZE :
  842. ei->SenseLen);
  843. scsi_set_resid(cmd, ei->ResidualCnt);
  844. if (ei->CommandStatus == 0) {
  845. cmd->scsi_done(cmd);
  846. cmd_free(h, cp);
  847. return;
  848. }
  849. /* an error has occurred */
  850. switch (ei->CommandStatus) {
  851. case CMD_TARGET_STATUS:
  852. if (ei->ScsiStatus) {
  853. /* Get sense key */
  854. sense_key = 0xf & ei->SenseInfo[2];
  855. /* Get additional sense code */
  856. asc = ei->SenseInfo[12];
  857. /* Get addition sense code qualifier */
  858. ascq = ei->SenseInfo[13];
  859. }
  860. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  861. if (check_for_unit_attention(h, cp)) {
  862. cmd->result = DID_SOFT_ERROR << 16;
  863. break;
  864. }
  865. if (sense_key == ILLEGAL_REQUEST) {
  866. /*
  867. * SCSI REPORT_LUNS is commonly unsupported on
  868. * Smart Array. Suppress noisy complaint.
  869. */
  870. if (cp->Request.CDB[0] == REPORT_LUNS)
  871. break;
  872. /* If ASC/ASCQ indicate Logical Unit
  873. * Not Supported condition,
  874. */
  875. if ((asc == 0x25) && (ascq == 0x0)) {
  876. dev_warn(&h->pdev->dev, "cp %p "
  877. "has check condition\n", cp);
  878. break;
  879. }
  880. }
  881. if (sense_key == NOT_READY) {
  882. /* If Sense is Not Ready, Logical Unit
  883. * Not ready, Manual Intervention
  884. * required
  885. */
  886. if ((asc == 0x04) && (ascq == 0x03)) {
  887. cmd->result = DID_NO_CONNECT << 16;
  888. dev_warn(&h->pdev->dev, "cp %p "
  889. "has check condition: unit "
  890. "not ready, manual "
  891. "intervention required\n", cp);
  892. break;
  893. }
  894. }
  895. /* Must be some other type of check condition */
  896. dev_warn(&h->pdev->dev, "cp %p has check condition: "
  897. "unknown type: "
  898. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  899. "Returning result: 0x%x, "
  900. "cmd=[%02x %02x %02x %02x %02x "
  901. "%02x %02x %02x %02x %02x]\n",
  902. cp, sense_key, asc, ascq,
  903. cmd->result,
  904. cmd->cmnd[0], cmd->cmnd[1],
  905. cmd->cmnd[2], cmd->cmnd[3],
  906. cmd->cmnd[4], cmd->cmnd[5],
  907. cmd->cmnd[6], cmd->cmnd[7],
  908. cmd->cmnd[8], cmd->cmnd[9]);
  909. break;
  910. }
  911. /* Problem was not a check condition
  912. * Pass it up to the upper layers...
  913. */
  914. if (ei->ScsiStatus) {
  915. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  916. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  917. "Returning result: 0x%x\n",
  918. cp, ei->ScsiStatus,
  919. sense_key, asc, ascq,
  920. cmd->result);
  921. } else { /* scsi status is zero??? How??? */
  922. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  923. "Returning no connection.\n", cp),
  924. /* Ordinarily, this case should never happen,
  925. * but there is a bug in some released firmware
  926. * revisions that allows it to happen if, for
  927. * example, a 4100 backplane loses power and
  928. * the tape drive is in it. We assume that
  929. * it's a fatal error of some kind because we
  930. * can't show that it wasn't. We will make it
  931. * look like selection timeout since that is
  932. * the most common reason for this to occur,
  933. * and it's severe enough.
  934. */
  935. cmd->result = DID_NO_CONNECT << 16;
  936. }
  937. break;
  938. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  939. break;
  940. case CMD_DATA_OVERRUN:
  941. dev_warn(&h->pdev->dev, "cp %p has"
  942. " completed with data overrun "
  943. "reported\n", cp);
  944. break;
  945. case CMD_INVALID: {
  946. /* print_bytes(cp, sizeof(*cp), 1, 0);
  947. print_cmd(cp); */
  948. /* We get CMD_INVALID if you address a non-existent device
  949. * instead of a selection timeout (no response). You will
  950. * see this if you yank out a drive, then try to access it.
  951. * This is kind of a shame because it means that any other
  952. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  953. * missing target. */
  954. cmd->result = DID_NO_CONNECT << 16;
  955. }
  956. break;
  957. case CMD_PROTOCOL_ERR:
  958. dev_warn(&h->pdev->dev, "cp %p has "
  959. "protocol error \n", cp);
  960. break;
  961. case CMD_HARDWARE_ERR:
  962. cmd->result = DID_ERROR << 16;
  963. dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
  964. break;
  965. case CMD_CONNECTION_LOST:
  966. cmd->result = DID_ERROR << 16;
  967. dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
  968. break;
  969. case CMD_ABORTED:
  970. cmd->result = DID_ABORT << 16;
  971. dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
  972. cp, ei->ScsiStatus);
  973. break;
  974. case CMD_ABORT_FAILED:
  975. cmd->result = DID_ERROR << 16;
  976. dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
  977. break;
  978. case CMD_UNSOLICITED_ABORT:
  979. cmd->result = DID_ABORT << 16;
  980. dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
  981. "abort\n", cp);
  982. break;
  983. case CMD_TIMEOUT:
  984. cmd->result = DID_TIME_OUT << 16;
  985. dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
  986. break;
  987. default:
  988. cmd->result = DID_ERROR << 16;
  989. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  990. cp, ei->CommandStatus);
  991. }
  992. cmd->scsi_done(cmd);
  993. cmd_free(h, cp);
  994. }
  995. static int hpsa_scsi_detect(struct ctlr_info *h)
  996. {
  997. struct Scsi_Host *sh;
  998. int error;
  999. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  1000. if (sh == NULL)
  1001. goto fail;
  1002. sh->io_port = 0;
  1003. sh->n_io_port = 0;
  1004. sh->this_id = -1;
  1005. sh->max_channel = 3;
  1006. sh->max_cmd_len = MAX_COMMAND_SIZE;
  1007. sh->max_lun = HPSA_MAX_LUN;
  1008. sh->max_id = HPSA_MAX_LUN;
  1009. h->scsi_host = sh;
  1010. sh->hostdata[0] = (unsigned long) h;
  1011. sh->irq = h->intr[SIMPLE_MODE_INT];
  1012. sh->unique_id = sh->irq;
  1013. error = scsi_add_host(sh, &h->pdev->dev);
  1014. if (error)
  1015. goto fail_host_put;
  1016. scsi_scan_host(sh);
  1017. return 0;
  1018. fail_host_put:
  1019. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
  1020. " failed for controller %d\n", h->ctlr);
  1021. scsi_host_put(sh);
  1022. return error;
  1023. fail:
  1024. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
  1025. " failed for controller %d\n", h->ctlr);
  1026. return -ENOMEM;
  1027. }
  1028. static void hpsa_pci_unmap(struct pci_dev *pdev,
  1029. struct CommandList *c, int sg_used, int data_direction)
  1030. {
  1031. int i;
  1032. union u64bit addr64;
  1033. for (i = 0; i < sg_used; i++) {
  1034. addr64.val32.lower = c->SG[i].Addr.lower;
  1035. addr64.val32.upper = c->SG[i].Addr.upper;
  1036. pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
  1037. data_direction);
  1038. }
  1039. }
  1040. static void hpsa_map_one(struct pci_dev *pdev,
  1041. struct CommandList *cp,
  1042. unsigned char *buf,
  1043. size_t buflen,
  1044. int data_direction)
  1045. {
  1046. u64 addr64;
  1047. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  1048. cp->Header.SGList = 0;
  1049. cp->Header.SGTotal = 0;
  1050. return;
  1051. }
  1052. addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
  1053. cp->SG[0].Addr.lower =
  1054. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1055. cp->SG[0].Addr.upper =
  1056. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1057. cp->SG[0].Len = buflen;
  1058. cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
  1059. cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
  1060. }
  1061. static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  1062. struct CommandList *c)
  1063. {
  1064. DECLARE_COMPLETION_ONSTACK(wait);
  1065. c->waiting = &wait;
  1066. enqueue_cmd_and_start_io(h, c);
  1067. wait_for_completion(&wait);
  1068. }
  1069. static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  1070. struct CommandList *c, int data_direction)
  1071. {
  1072. int retry_count = 0;
  1073. do {
  1074. memset(c->err_info, 0, sizeof(c->err_info));
  1075. hpsa_scsi_do_simple_cmd_core(h, c);
  1076. retry_count++;
  1077. } while (check_for_unit_attention(h, c) && retry_count <= 3);
  1078. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  1079. }
  1080. static void hpsa_scsi_interpret_error(struct CommandList *cp)
  1081. {
  1082. struct ErrorInfo *ei;
  1083. struct device *d = &cp->h->pdev->dev;
  1084. ei = cp->err_info;
  1085. switch (ei->CommandStatus) {
  1086. case CMD_TARGET_STATUS:
  1087. dev_warn(d, "cmd %p has completed with errors\n", cp);
  1088. dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
  1089. ei->ScsiStatus);
  1090. if (ei->ScsiStatus == 0)
  1091. dev_warn(d, "SCSI status is abnormally zero. "
  1092. "(probably indicates selection timeout "
  1093. "reported incorrectly due to a known "
  1094. "firmware bug, circa July, 2001.)\n");
  1095. break;
  1096. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1097. dev_info(d, "UNDERRUN\n");
  1098. break;
  1099. case CMD_DATA_OVERRUN:
  1100. dev_warn(d, "cp %p has completed with data overrun\n", cp);
  1101. break;
  1102. case CMD_INVALID: {
  1103. /* controller unfortunately reports SCSI passthru's
  1104. * to non-existent targets as invalid commands.
  1105. */
  1106. dev_warn(d, "cp %p is reported invalid (probably means "
  1107. "target device no longer present)\n", cp);
  1108. /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
  1109. print_cmd(cp); */
  1110. }
  1111. break;
  1112. case CMD_PROTOCOL_ERR:
  1113. dev_warn(d, "cp %p has protocol error \n", cp);
  1114. break;
  1115. case CMD_HARDWARE_ERR:
  1116. /* cmd->result = DID_ERROR << 16; */
  1117. dev_warn(d, "cp %p had hardware error\n", cp);
  1118. break;
  1119. case CMD_CONNECTION_LOST:
  1120. dev_warn(d, "cp %p had connection lost\n", cp);
  1121. break;
  1122. case CMD_ABORTED:
  1123. dev_warn(d, "cp %p was aborted\n", cp);
  1124. break;
  1125. case CMD_ABORT_FAILED:
  1126. dev_warn(d, "cp %p reports abort failed\n", cp);
  1127. break;
  1128. case CMD_UNSOLICITED_ABORT:
  1129. dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
  1130. break;
  1131. case CMD_TIMEOUT:
  1132. dev_warn(d, "cp %p timed out\n", cp);
  1133. break;
  1134. default:
  1135. dev_warn(d, "cp %p returned unknown status %x\n", cp,
  1136. ei->CommandStatus);
  1137. }
  1138. }
  1139. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  1140. unsigned char page, unsigned char *buf,
  1141. unsigned char bufsize)
  1142. {
  1143. int rc = IO_OK;
  1144. struct CommandList *c;
  1145. struct ErrorInfo *ei;
  1146. c = cmd_special_alloc(h);
  1147. if (c == NULL) { /* trouble... */
  1148. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1149. return -ENOMEM;
  1150. }
  1151. fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
  1152. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1153. ei = c->err_info;
  1154. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1155. hpsa_scsi_interpret_error(c);
  1156. rc = -1;
  1157. }
  1158. cmd_special_free(h, c);
  1159. return rc;
  1160. }
  1161. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
  1162. {
  1163. int rc = IO_OK;
  1164. struct CommandList *c;
  1165. struct ErrorInfo *ei;
  1166. c = cmd_special_alloc(h);
  1167. if (c == NULL) { /* trouble... */
  1168. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1169. return -1;
  1170. }
  1171. fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
  1172. hpsa_scsi_do_simple_cmd_core(h, c);
  1173. /* no unmap needed here because no data xfer. */
  1174. ei = c->err_info;
  1175. if (ei->CommandStatus != 0) {
  1176. hpsa_scsi_interpret_error(c);
  1177. rc = -1;
  1178. }
  1179. cmd_special_free(h, c);
  1180. return rc;
  1181. }
  1182. static void hpsa_get_raid_level(struct ctlr_info *h,
  1183. unsigned char *scsi3addr, unsigned char *raid_level)
  1184. {
  1185. int rc;
  1186. unsigned char *buf;
  1187. *raid_level = RAID_UNKNOWN;
  1188. buf = kzalloc(64, GFP_KERNEL);
  1189. if (!buf)
  1190. return;
  1191. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
  1192. if (rc == 0)
  1193. *raid_level = buf[8];
  1194. if (*raid_level > RAID_UNKNOWN)
  1195. *raid_level = RAID_UNKNOWN;
  1196. kfree(buf);
  1197. return;
  1198. }
  1199. /* Get the device id from inquiry page 0x83 */
  1200. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  1201. unsigned char *device_id, int buflen)
  1202. {
  1203. int rc;
  1204. unsigned char *buf;
  1205. if (buflen > 16)
  1206. buflen = 16;
  1207. buf = kzalloc(64, GFP_KERNEL);
  1208. if (!buf)
  1209. return -1;
  1210. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
  1211. if (rc == 0)
  1212. memcpy(device_id, &buf[8], buflen);
  1213. kfree(buf);
  1214. return rc != 0;
  1215. }
  1216. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  1217. struct ReportLUNdata *buf, int bufsize,
  1218. int extended_response)
  1219. {
  1220. int rc = IO_OK;
  1221. struct CommandList *c;
  1222. unsigned char scsi3addr[8];
  1223. struct ErrorInfo *ei;
  1224. c = cmd_special_alloc(h);
  1225. if (c == NULL) { /* trouble... */
  1226. dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1227. return -1;
  1228. }
  1229. /* address the controller */
  1230. memset(scsi3addr, 0, sizeof(scsi3addr));
  1231. fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  1232. buf, bufsize, 0, scsi3addr, TYPE_CMD);
  1233. if (extended_response)
  1234. c->Request.CDB[1] = extended_response;
  1235. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1236. ei = c->err_info;
  1237. if (ei->CommandStatus != 0 &&
  1238. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1239. hpsa_scsi_interpret_error(c);
  1240. rc = -1;
  1241. }
  1242. cmd_special_free(h, c);
  1243. return rc;
  1244. }
  1245. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  1246. struct ReportLUNdata *buf,
  1247. int bufsize, int extended_response)
  1248. {
  1249. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
  1250. }
  1251. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  1252. struct ReportLUNdata *buf, int bufsize)
  1253. {
  1254. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  1255. }
  1256. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  1257. int bus, int target, int lun)
  1258. {
  1259. device->bus = bus;
  1260. device->target = target;
  1261. device->lun = lun;
  1262. }
  1263. static int hpsa_update_device_info(struct ctlr_info *h,
  1264. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
  1265. {
  1266. #define OBDR_TAPE_INQ_SIZE 49
  1267. unsigned char *inq_buff;
  1268. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1269. if (!inq_buff)
  1270. goto bail_out;
  1271. /* Do an inquiry to the device to see what it is. */
  1272. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  1273. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  1274. /* Inquiry failed (msg printed already) */
  1275. dev_err(&h->pdev->dev,
  1276. "hpsa_update_device_info: inquiry failed\n");
  1277. goto bail_out;
  1278. }
  1279. /* As a side effect, record the firmware version number
  1280. * if we happen to be talking to the RAID controller.
  1281. */
  1282. if (is_hba_lunid(scsi3addr))
  1283. memcpy(h->firm_ver, &inq_buff[32], 4);
  1284. this_device->devtype = (inq_buff[0] & 0x1f);
  1285. memcpy(this_device->scsi3addr, scsi3addr, 8);
  1286. memcpy(this_device->vendor, &inq_buff[8],
  1287. sizeof(this_device->vendor));
  1288. memcpy(this_device->model, &inq_buff[16],
  1289. sizeof(this_device->model));
  1290. memcpy(this_device->revision, &inq_buff[32],
  1291. sizeof(this_device->revision));
  1292. memset(this_device->device_id, 0,
  1293. sizeof(this_device->device_id));
  1294. hpsa_get_device_id(h, scsi3addr, this_device->device_id,
  1295. sizeof(this_device->device_id));
  1296. if (this_device->devtype == TYPE_DISK &&
  1297. is_logical_dev_addr_mode(scsi3addr))
  1298. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  1299. else
  1300. this_device->raid_level = RAID_UNKNOWN;
  1301. kfree(inq_buff);
  1302. return 0;
  1303. bail_out:
  1304. kfree(inq_buff);
  1305. return 1;
  1306. }
  1307. static unsigned char *msa2xxx_model[] = {
  1308. "MSA2012",
  1309. "MSA2024",
  1310. "MSA2312",
  1311. "MSA2324",
  1312. NULL,
  1313. };
  1314. static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  1315. {
  1316. int i;
  1317. for (i = 0; msa2xxx_model[i]; i++)
  1318. if (strncmp(device->model, msa2xxx_model[i],
  1319. strlen(msa2xxx_model[i])) == 0)
  1320. return 1;
  1321. return 0;
  1322. }
  1323. /* Helper function to assign bus, target, lun mapping of devices.
  1324. * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
  1325. * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
  1326. * Logical drive target and lun are assigned at this time, but
  1327. * physical device lun and target assignment are deferred (assigned
  1328. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  1329. */
  1330. static void figure_bus_target_lun(struct ctlr_info *h,
  1331. u8 *lunaddrbytes, int *bus, int *target, int *lun,
  1332. struct hpsa_scsi_dev_t *device)
  1333. {
  1334. u32 lunid;
  1335. if (is_logical_dev_addr_mode(lunaddrbytes)) {
  1336. /* logical device */
  1337. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1338. if (is_msa2xxx(h, device)) {
  1339. *bus = 1;
  1340. *target = (lunid >> 16) & 0x3fff;
  1341. *lun = lunid & 0x00ff;
  1342. } else {
  1343. *bus = 0;
  1344. *lun = 0;
  1345. *target = lunid & 0x3fff;
  1346. }
  1347. } else {
  1348. /* physical device */
  1349. if (is_hba_lunid(lunaddrbytes))
  1350. *bus = 3;
  1351. else
  1352. *bus = 2;
  1353. *target = -1;
  1354. *lun = -1; /* we will fill these in later. */
  1355. }
  1356. }
  1357. /*
  1358. * If there is no lun 0 on a target, linux won't find any devices.
  1359. * For the MSA2xxx boxes, we have to manually detect the enclosure
  1360. * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
  1361. * it for some reason. *tmpdevice is the target we're adding,
  1362. * this_device is a pointer into the current element of currentsd[]
  1363. * that we're building up in update_scsi_devices(), below.
  1364. * lunzerobits is a bitmap that tracks which targets already have a
  1365. * lun 0 assigned.
  1366. * Returns 1 if an enclosure was added, 0 if not.
  1367. */
  1368. static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
  1369. struct hpsa_scsi_dev_t *tmpdevice,
  1370. struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
  1371. int bus, int target, int lun, unsigned long lunzerobits[],
  1372. int *nmsa2xxx_enclosures)
  1373. {
  1374. unsigned char scsi3addr[8];
  1375. if (test_bit(target, lunzerobits))
  1376. return 0; /* There is already a lun 0 on this target. */
  1377. if (!is_logical_dev_addr_mode(lunaddrbytes))
  1378. return 0; /* It's the logical targets that may lack lun 0. */
  1379. if (!is_msa2xxx(h, tmpdevice))
  1380. return 0; /* It's only the MSA2xxx that have this problem. */
  1381. if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
  1382. return 0;
  1383. if (is_hba_lunid(scsi3addr))
  1384. return 0; /* Don't add the RAID controller here. */
  1385. #define MAX_MSA2XXX_ENCLOSURES 32
  1386. if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
  1387. dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
  1388. "enclosures exceeded. Check your hardware "
  1389. "configuration.");
  1390. return 0;
  1391. }
  1392. memset(scsi3addr, 0, 8);
  1393. scsi3addr[3] = target;
  1394. if (hpsa_update_device_info(h, scsi3addr, this_device))
  1395. return 0;
  1396. (*nmsa2xxx_enclosures)++;
  1397. hpsa_set_bus_target_lun(this_device, bus, target, 0);
  1398. set_bit(target, lunzerobits);
  1399. return 1;
  1400. }
  1401. /*
  1402. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  1403. * logdev. The number of luns in physdev and logdev are returned in
  1404. * *nphysicals and *nlogicals, respectively.
  1405. * Returns 0 on success, -1 otherwise.
  1406. */
  1407. static int hpsa_gather_lun_info(struct ctlr_info *h,
  1408. int reportlunsize,
  1409. struct ReportLUNdata *physdev, u32 *nphysicals,
  1410. struct ReportLUNdata *logdev, u32 *nlogicals)
  1411. {
  1412. if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
  1413. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  1414. return -1;
  1415. }
  1416. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
  1417. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  1418. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
  1419. " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1420. *nphysicals - HPSA_MAX_PHYS_LUN);
  1421. *nphysicals = HPSA_MAX_PHYS_LUN;
  1422. }
  1423. if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
  1424. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  1425. return -1;
  1426. }
  1427. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  1428. /* Reject Logicals in excess of our max capability. */
  1429. if (*nlogicals > HPSA_MAX_LUN) {
  1430. dev_warn(&h->pdev->dev,
  1431. "maximum logical LUNs (%d) exceeded. "
  1432. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  1433. *nlogicals - HPSA_MAX_LUN);
  1434. *nlogicals = HPSA_MAX_LUN;
  1435. }
  1436. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  1437. dev_warn(&h->pdev->dev,
  1438. "maximum logical + physical LUNs (%d) exceeded. "
  1439. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1440. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  1441. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  1442. }
  1443. return 0;
  1444. }
  1445. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
  1446. {
  1447. /* the idea here is we could get notified
  1448. * that some devices have changed, so we do a report
  1449. * physical luns and report logical luns cmd, and adjust
  1450. * our list of devices accordingly.
  1451. *
  1452. * The scsi3addr's of devices won't change so long as the
  1453. * adapter is not reset. That means we can rescan and
  1454. * tell which devices we already know about, vs. new
  1455. * devices, vs. disappearing devices.
  1456. */
  1457. struct ReportLUNdata *physdev_list = NULL;
  1458. struct ReportLUNdata *logdev_list = NULL;
  1459. unsigned char *inq_buff = NULL;
  1460. u32 nphysicals = 0;
  1461. u32 nlogicals = 0;
  1462. u32 ndev_allocated = 0;
  1463. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  1464. int ncurrent = 0;
  1465. int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
  1466. int i, nmsa2xxx_enclosures, ndevs_to_allocate;
  1467. int bus, target, lun;
  1468. DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
  1469. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  1470. GFP_KERNEL);
  1471. physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1472. logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1473. inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1474. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  1475. if (!currentsd || !physdev_list || !logdev_list ||
  1476. !inq_buff || !tmpdevice) {
  1477. dev_err(&h->pdev->dev, "out of memory\n");
  1478. goto out;
  1479. }
  1480. memset(lunzerobits, 0, sizeof(lunzerobits));
  1481. if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
  1482. logdev_list, &nlogicals))
  1483. goto out;
  1484. /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
  1485. * but each of them 4 times through different paths. The plus 1
  1486. * is for the RAID controller.
  1487. */
  1488. ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
  1489. /* Allocate the per device structures */
  1490. for (i = 0; i < ndevs_to_allocate; i++) {
  1491. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  1492. if (!currentsd[i]) {
  1493. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  1494. __FILE__, __LINE__);
  1495. goto out;
  1496. }
  1497. ndev_allocated++;
  1498. }
  1499. /* adjust our table of devices */
  1500. nmsa2xxx_enclosures = 0;
  1501. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  1502. u8 *lunaddrbytes;
  1503. /* Figure out where the LUN ID info is coming from */
  1504. if (i < nphysicals)
  1505. lunaddrbytes = &physdev_list->LUN[i][0];
  1506. else
  1507. if (i < nphysicals + nlogicals)
  1508. lunaddrbytes =
  1509. &logdev_list->LUN[i-nphysicals][0];
  1510. else /* jam in the RAID controller at the end */
  1511. lunaddrbytes = RAID_CTLR_LUNID;
  1512. /* skip masked physical devices. */
  1513. if (lunaddrbytes[3] & 0xC0 && i < nphysicals)
  1514. continue;
  1515. /* Get device type, vendor, model, device id */
  1516. if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
  1517. continue; /* skip it if we can't talk to it. */
  1518. figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
  1519. tmpdevice);
  1520. this_device = currentsd[ncurrent];
  1521. /*
  1522. * For the msa2xxx boxes, we have to insert a LUN 0 which
  1523. * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
  1524. * is nonetheless an enclosure device there. We have to
  1525. * present that otherwise linux won't find anything if
  1526. * there is no lun 0.
  1527. */
  1528. if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
  1529. lunaddrbytes, bus, target, lun, lunzerobits,
  1530. &nmsa2xxx_enclosures)) {
  1531. ncurrent++;
  1532. this_device = currentsd[ncurrent];
  1533. }
  1534. *this_device = *tmpdevice;
  1535. hpsa_set_bus_target_lun(this_device, bus, target, lun);
  1536. switch (this_device->devtype) {
  1537. case TYPE_ROM: {
  1538. /* We don't *really* support actual CD-ROM devices,
  1539. * just "One Button Disaster Recovery" tape drive
  1540. * which temporarily pretends to be a CD-ROM drive.
  1541. * So we check that the device is really an OBDR tape
  1542. * device by checking for "$DR-10" in bytes 43-48 of
  1543. * the inquiry data.
  1544. */
  1545. char obdr_sig[7];
  1546. #define OBDR_TAPE_SIG "$DR-10"
  1547. strncpy(obdr_sig, &inq_buff[43], 6);
  1548. obdr_sig[6] = '\0';
  1549. if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
  1550. /* Not OBDR device, ignore it. */
  1551. break;
  1552. }
  1553. ncurrent++;
  1554. break;
  1555. case TYPE_DISK:
  1556. if (i < nphysicals)
  1557. break;
  1558. ncurrent++;
  1559. break;
  1560. case TYPE_TAPE:
  1561. case TYPE_MEDIUM_CHANGER:
  1562. ncurrent++;
  1563. break;
  1564. case TYPE_RAID:
  1565. /* Only present the Smartarray HBA as a RAID controller.
  1566. * If it's a RAID controller other than the HBA itself
  1567. * (an external RAID controller, MSA500 or similar)
  1568. * don't present it.
  1569. */
  1570. if (!is_hba_lunid(lunaddrbytes))
  1571. break;
  1572. ncurrent++;
  1573. break;
  1574. default:
  1575. break;
  1576. }
  1577. if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
  1578. break;
  1579. }
  1580. adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
  1581. out:
  1582. kfree(tmpdevice);
  1583. for (i = 0; i < ndev_allocated; i++)
  1584. kfree(currentsd[i]);
  1585. kfree(currentsd);
  1586. kfree(inq_buff);
  1587. kfree(physdev_list);
  1588. kfree(logdev_list);
  1589. }
  1590. /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  1591. * dma mapping and fills in the scatter gather entries of the
  1592. * hpsa command, cp.
  1593. */
  1594. static int hpsa_scatter_gather(struct pci_dev *pdev,
  1595. struct CommandList *cp,
  1596. struct scsi_cmnd *cmd)
  1597. {
  1598. unsigned int len;
  1599. struct scatterlist *sg;
  1600. u64 addr64;
  1601. int use_sg, i;
  1602. BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
  1603. use_sg = scsi_dma_map(cmd);
  1604. if (use_sg < 0)
  1605. return use_sg;
  1606. if (!use_sg)
  1607. goto sglist_finished;
  1608. scsi_for_each_sg(cmd, sg, use_sg, i) {
  1609. addr64 = (u64) sg_dma_address(sg);
  1610. len = sg_dma_len(sg);
  1611. cp->SG[i].Addr.lower =
  1612. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1613. cp->SG[i].Addr.upper =
  1614. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1615. cp->SG[i].Len = len;
  1616. cp->SG[i].Ext = 0; /* we are not chaining */
  1617. }
  1618. sglist_finished:
  1619. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  1620. cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
  1621. return 0;
  1622. }
  1623. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  1624. void (*done)(struct scsi_cmnd *))
  1625. {
  1626. struct ctlr_info *h;
  1627. struct hpsa_scsi_dev_t *dev;
  1628. unsigned char scsi3addr[8];
  1629. struct CommandList *c;
  1630. unsigned long flags;
  1631. /* Get the ptr to our adapter structure out of cmd->host. */
  1632. h = sdev_to_hba(cmd->device);
  1633. dev = cmd->device->hostdata;
  1634. if (!dev) {
  1635. cmd->result = DID_NO_CONNECT << 16;
  1636. done(cmd);
  1637. return 0;
  1638. }
  1639. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  1640. /* Need a lock as this is being allocated from the pool */
  1641. spin_lock_irqsave(&h->lock, flags);
  1642. c = cmd_alloc(h);
  1643. spin_unlock_irqrestore(&h->lock, flags);
  1644. if (c == NULL) { /* trouble... */
  1645. dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
  1646. return SCSI_MLQUEUE_HOST_BUSY;
  1647. }
  1648. /* Fill in the command list header */
  1649. cmd->scsi_done = done; /* save this for use by completion code */
  1650. /* save c in case we have to abort it */
  1651. cmd->host_scribble = (unsigned char *) c;
  1652. c->cmd_type = CMD_SCSI;
  1653. c->scsi_cmd = cmd;
  1654. c->Header.ReplyQueue = 0; /* unused in simple mode */
  1655. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  1656. c->Header.Tag.lower = c->busaddr; /* Use k. address of cmd as tag */
  1657. /* Fill in the request block... */
  1658. c->Request.Timeout = 0;
  1659. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  1660. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  1661. c->Request.CDBLen = cmd->cmd_len;
  1662. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  1663. c->Request.Type.Type = TYPE_CMD;
  1664. c->Request.Type.Attribute = ATTR_SIMPLE;
  1665. switch (cmd->sc_data_direction) {
  1666. case DMA_TO_DEVICE:
  1667. c->Request.Type.Direction = XFER_WRITE;
  1668. break;
  1669. case DMA_FROM_DEVICE:
  1670. c->Request.Type.Direction = XFER_READ;
  1671. break;
  1672. case DMA_NONE:
  1673. c->Request.Type.Direction = XFER_NONE;
  1674. break;
  1675. case DMA_BIDIRECTIONAL:
  1676. /* This can happen if a buggy application does a scsi passthru
  1677. * and sets both inlen and outlen to non-zero. ( see
  1678. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  1679. */
  1680. c->Request.Type.Direction = XFER_RSVD;
  1681. /* This is technically wrong, and hpsa controllers should
  1682. * reject it with CMD_INVALID, which is the most correct
  1683. * response, but non-fibre backends appear to let it
  1684. * slide by, and give the same results as if this field
  1685. * were set correctly. Either way is acceptable for
  1686. * our purposes here.
  1687. */
  1688. break;
  1689. default:
  1690. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  1691. cmd->sc_data_direction);
  1692. BUG();
  1693. break;
  1694. }
  1695. if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
  1696. cmd_free(h, c);
  1697. return SCSI_MLQUEUE_HOST_BUSY;
  1698. }
  1699. enqueue_cmd_and_start_io(h, c);
  1700. /* the cmd'll come back via intr handler in complete_scsi_command() */
  1701. return 0;
  1702. }
  1703. static void hpsa_unregister_scsi(struct ctlr_info *h)
  1704. {
  1705. /* we are being forcibly unloaded, and may not refuse. */
  1706. scsi_remove_host(h->scsi_host);
  1707. scsi_host_put(h->scsi_host);
  1708. h->scsi_host = NULL;
  1709. }
  1710. static int hpsa_register_scsi(struct ctlr_info *h)
  1711. {
  1712. int rc;
  1713. hpsa_update_scsi_devices(h, -1);
  1714. rc = hpsa_scsi_detect(h);
  1715. if (rc != 0)
  1716. dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
  1717. " hpsa_scsi_detect(), rc is %d\n", rc);
  1718. return rc;
  1719. }
  1720. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  1721. unsigned char lunaddr[])
  1722. {
  1723. int rc = 0;
  1724. int count = 0;
  1725. int waittime = 1; /* seconds */
  1726. struct CommandList *c;
  1727. c = cmd_special_alloc(h);
  1728. if (!c) {
  1729. dev_warn(&h->pdev->dev, "out of memory in "
  1730. "wait_for_device_to_become_ready.\n");
  1731. return IO_ERROR;
  1732. }
  1733. /* Send test unit ready until device ready, or give up. */
  1734. while (count < HPSA_TUR_RETRY_LIMIT) {
  1735. /* Wait for a bit. do this first, because if we send
  1736. * the TUR right away, the reset will just abort it.
  1737. */
  1738. msleep(1000 * waittime);
  1739. count++;
  1740. /* Increase wait time with each try, up to a point. */
  1741. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  1742. waittime = waittime * 2;
  1743. /* Send the Test Unit Ready */
  1744. fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
  1745. hpsa_scsi_do_simple_cmd_core(h, c);
  1746. /* no unmap needed here because no data xfer. */
  1747. if (c->err_info->CommandStatus == CMD_SUCCESS)
  1748. break;
  1749. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  1750. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  1751. (c->err_info->SenseInfo[2] == NO_SENSE ||
  1752. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  1753. break;
  1754. dev_warn(&h->pdev->dev, "waiting %d secs "
  1755. "for device to become ready.\n", waittime);
  1756. rc = 1; /* device not ready. */
  1757. }
  1758. if (rc)
  1759. dev_warn(&h->pdev->dev, "giving up on device.\n");
  1760. else
  1761. dev_warn(&h->pdev->dev, "device is ready.\n");
  1762. cmd_special_free(h, c);
  1763. return rc;
  1764. }
  1765. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  1766. * complaining. Doing a host- or bus-reset can't do anything good here.
  1767. */
  1768. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  1769. {
  1770. int rc;
  1771. struct ctlr_info *h;
  1772. struct hpsa_scsi_dev_t *dev;
  1773. /* find the controller to which the command to be aborted was sent */
  1774. h = sdev_to_hba(scsicmd->device);
  1775. if (h == NULL) /* paranoia */
  1776. return FAILED;
  1777. dev_warn(&h->pdev->dev, "resetting drive\n");
  1778. dev = scsicmd->device->hostdata;
  1779. if (!dev) {
  1780. dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
  1781. "device lookup failed.\n");
  1782. return FAILED;
  1783. }
  1784. /* send a reset to the SCSI LUN which the command was sent to */
  1785. rc = hpsa_send_reset(h, dev->scsi3addr);
  1786. if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
  1787. return SUCCESS;
  1788. dev_warn(&h->pdev->dev, "resetting device failed.\n");
  1789. return FAILED;
  1790. }
  1791. /*
  1792. * For operations that cannot sleep, a command block is allocated at init,
  1793. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  1794. * which ones are free or in use. Lock must be held when calling this.
  1795. * cmd_free() is the complement.
  1796. */
  1797. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  1798. {
  1799. struct CommandList *c;
  1800. int i;
  1801. union u64bit temp64;
  1802. dma_addr_t cmd_dma_handle, err_dma_handle;
  1803. do {
  1804. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  1805. if (i == h->nr_cmds)
  1806. return NULL;
  1807. } while (test_and_set_bit
  1808. (i & (BITS_PER_LONG - 1),
  1809. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  1810. c = h->cmd_pool + i;
  1811. memset(c, 0, sizeof(*c));
  1812. cmd_dma_handle = h->cmd_pool_dhandle
  1813. + i * sizeof(*c);
  1814. c->err_info = h->errinfo_pool + i;
  1815. memset(c->err_info, 0, sizeof(*c->err_info));
  1816. err_dma_handle = h->errinfo_pool_dhandle
  1817. + i * sizeof(*c->err_info);
  1818. h->nr_allocs++;
  1819. c->cmdindex = i;
  1820. INIT_HLIST_NODE(&c->list);
  1821. c->busaddr = (u32) cmd_dma_handle;
  1822. temp64.val = (u64) err_dma_handle;
  1823. c->ErrDesc.Addr.lower = temp64.val32.lower;
  1824. c->ErrDesc.Addr.upper = temp64.val32.upper;
  1825. c->ErrDesc.Len = sizeof(*c->err_info);
  1826. c->h = h;
  1827. return c;
  1828. }
  1829. /* For operations that can wait for kmalloc to possibly sleep,
  1830. * this routine can be called. Lock need not be held to call
  1831. * cmd_special_alloc. cmd_special_free() is the complement.
  1832. */
  1833. static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
  1834. {
  1835. struct CommandList *c;
  1836. union u64bit temp64;
  1837. dma_addr_t cmd_dma_handle, err_dma_handle;
  1838. c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
  1839. if (c == NULL)
  1840. return NULL;
  1841. memset(c, 0, sizeof(*c));
  1842. c->cmdindex = -1;
  1843. c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
  1844. &err_dma_handle);
  1845. if (c->err_info == NULL) {
  1846. pci_free_consistent(h->pdev,
  1847. sizeof(*c), c, cmd_dma_handle);
  1848. return NULL;
  1849. }
  1850. memset(c->err_info, 0, sizeof(*c->err_info));
  1851. INIT_HLIST_NODE(&c->list);
  1852. c->busaddr = (u32) cmd_dma_handle;
  1853. temp64.val = (u64) err_dma_handle;
  1854. c->ErrDesc.Addr.lower = temp64.val32.lower;
  1855. c->ErrDesc.Addr.upper = temp64.val32.upper;
  1856. c->ErrDesc.Len = sizeof(*c->err_info);
  1857. c->h = h;
  1858. return c;
  1859. }
  1860. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  1861. {
  1862. int i;
  1863. i = c - h->cmd_pool;
  1864. clear_bit(i & (BITS_PER_LONG - 1),
  1865. h->cmd_pool_bits + (i / BITS_PER_LONG));
  1866. h->nr_frees++;
  1867. }
  1868. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
  1869. {
  1870. union u64bit temp64;
  1871. temp64.val32.lower = c->ErrDesc.Addr.lower;
  1872. temp64.val32.upper = c->ErrDesc.Addr.upper;
  1873. pci_free_consistent(h->pdev, sizeof(*c->err_info),
  1874. c->err_info, (dma_addr_t) temp64.val);
  1875. pci_free_consistent(h->pdev, sizeof(*c),
  1876. c, (dma_addr_t) c->busaddr);
  1877. }
  1878. #ifdef CONFIG_COMPAT
  1879. static int do_ioctl(struct scsi_device *dev, int cmd, void *arg)
  1880. {
  1881. int ret;
  1882. lock_kernel();
  1883. ret = hpsa_ioctl(dev, cmd, arg);
  1884. unlock_kernel();
  1885. return ret;
  1886. }
  1887. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg);
  1888. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  1889. int cmd, void *arg);
  1890. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
  1891. {
  1892. switch (cmd) {
  1893. case CCISS_GETPCIINFO:
  1894. case CCISS_GETINTINFO:
  1895. case CCISS_SETINTINFO:
  1896. case CCISS_GETNODENAME:
  1897. case CCISS_SETNODENAME:
  1898. case CCISS_GETHEARTBEAT:
  1899. case CCISS_GETBUSTYPES:
  1900. case CCISS_GETFIRMVER:
  1901. case CCISS_GETDRIVVER:
  1902. case CCISS_REVALIDVOLS:
  1903. case CCISS_DEREGDISK:
  1904. case CCISS_REGNEWDISK:
  1905. case CCISS_REGNEWD:
  1906. case CCISS_RESCANDISK:
  1907. case CCISS_GETLUNINFO:
  1908. return do_ioctl(dev, cmd, arg);
  1909. case CCISS_PASSTHRU32:
  1910. return hpsa_ioctl32_passthru(dev, cmd, arg);
  1911. case CCISS_BIG_PASSTHRU32:
  1912. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  1913. default:
  1914. return -ENOIOCTLCMD;
  1915. }
  1916. }
  1917. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
  1918. {
  1919. IOCTL32_Command_struct __user *arg32 =
  1920. (IOCTL32_Command_struct __user *) arg;
  1921. IOCTL_Command_struct arg64;
  1922. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  1923. int err;
  1924. u32 cp;
  1925. err = 0;
  1926. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  1927. sizeof(arg64.LUN_info));
  1928. err |= copy_from_user(&arg64.Request, &arg32->Request,
  1929. sizeof(arg64.Request));
  1930. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  1931. sizeof(arg64.error_info));
  1932. err |= get_user(arg64.buf_size, &arg32->buf_size);
  1933. err |= get_user(cp, &arg32->buf);
  1934. arg64.buf = compat_ptr(cp);
  1935. err |= copy_to_user(p, &arg64, sizeof(arg64));
  1936. if (err)
  1937. return -EFAULT;
  1938. err = do_ioctl(dev, CCISS_PASSTHRU, (void *)p);
  1939. if (err)
  1940. return err;
  1941. err |= copy_in_user(&arg32->error_info, &p->error_info,
  1942. sizeof(arg32->error_info));
  1943. if (err)
  1944. return -EFAULT;
  1945. return err;
  1946. }
  1947. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  1948. int cmd, void *arg)
  1949. {
  1950. BIG_IOCTL32_Command_struct __user *arg32 =
  1951. (BIG_IOCTL32_Command_struct __user *) arg;
  1952. BIG_IOCTL_Command_struct arg64;
  1953. BIG_IOCTL_Command_struct __user *p =
  1954. compat_alloc_user_space(sizeof(arg64));
  1955. int err;
  1956. u32 cp;
  1957. err = 0;
  1958. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  1959. sizeof(arg64.LUN_info));
  1960. err |= copy_from_user(&arg64.Request, &arg32->Request,
  1961. sizeof(arg64.Request));
  1962. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  1963. sizeof(arg64.error_info));
  1964. err |= get_user(arg64.buf_size, &arg32->buf_size);
  1965. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  1966. err |= get_user(cp, &arg32->buf);
  1967. arg64.buf = compat_ptr(cp);
  1968. err |= copy_to_user(p, &arg64, sizeof(arg64));
  1969. if (err)
  1970. return -EFAULT;
  1971. err = do_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
  1972. if (err)
  1973. return err;
  1974. err |= copy_in_user(&arg32->error_info, &p->error_info,
  1975. sizeof(arg32->error_info));
  1976. if (err)
  1977. return -EFAULT;
  1978. return err;
  1979. }
  1980. #endif
  1981. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  1982. {
  1983. struct hpsa_pci_info pciinfo;
  1984. if (!argp)
  1985. return -EINVAL;
  1986. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  1987. pciinfo.bus = h->pdev->bus->number;
  1988. pciinfo.dev_fn = h->pdev->devfn;
  1989. pciinfo.board_id = h->board_id;
  1990. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  1991. return -EFAULT;
  1992. return 0;
  1993. }
  1994. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  1995. {
  1996. DriverVer_type DriverVer;
  1997. unsigned char vmaj, vmin, vsubmin;
  1998. int rc;
  1999. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  2000. &vmaj, &vmin, &vsubmin);
  2001. if (rc != 3) {
  2002. dev_info(&h->pdev->dev, "driver version string '%s' "
  2003. "unrecognized.", HPSA_DRIVER_VERSION);
  2004. vmaj = 0;
  2005. vmin = 0;
  2006. vsubmin = 0;
  2007. }
  2008. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  2009. if (!argp)
  2010. return -EINVAL;
  2011. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  2012. return -EFAULT;
  2013. return 0;
  2014. }
  2015. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2016. {
  2017. IOCTL_Command_struct iocommand;
  2018. struct CommandList *c;
  2019. char *buff = NULL;
  2020. union u64bit temp64;
  2021. if (!argp)
  2022. return -EINVAL;
  2023. if (!capable(CAP_SYS_RAWIO))
  2024. return -EPERM;
  2025. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  2026. return -EFAULT;
  2027. if ((iocommand.buf_size < 1) &&
  2028. (iocommand.Request.Type.Direction != XFER_NONE)) {
  2029. return -EINVAL;
  2030. }
  2031. if (iocommand.buf_size > 0) {
  2032. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  2033. if (buff == NULL)
  2034. return -EFAULT;
  2035. }
  2036. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  2037. /* Copy the data into the buffer we created */
  2038. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
  2039. kfree(buff);
  2040. return -EFAULT;
  2041. }
  2042. } else
  2043. memset(buff, 0, iocommand.buf_size);
  2044. c = cmd_special_alloc(h);
  2045. if (c == NULL) {
  2046. kfree(buff);
  2047. return -ENOMEM;
  2048. }
  2049. /* Fill in the command type */
  2050. c->cmd_type = CMD_IOCTL_PEND;
  2051. /* Fill in Command Header */
  2052. c->Header.ReplyQueue = 0; /* unused in simple mode */
  2053. if (iocommand.buf_size > 0) { /* buffer to fill */
  2054. c->Header.SGList = 1;
  2055. c->Header.SGTotal = 1;
  2056. } else { /* no buffers to fill */
  2057. c->Header.SGList = 0;
  2058. c->Header.SGTotal = 0;
  2059. }
  2060. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  2061. /* use the kernel address the cmd block for tag */
  2062. c->Header.Tag.lower = c->busaddr;
  2063. /* Fill in Request block */
  2064. memcpy(&c->Request, &iocommand.Request,
  2065. sizeof(c->Request));
  2066. /* Fill in the scatter gather information */
  2067. if (iocommand.buf_size > 0) {
  2068. temp64.val = pci_map_single(h->pdev, buff,
  2069. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  2070. c->SG[0].Addr.lower = temp64.val32.lower;
  2071. c->SG[0].Addr.upper = temp64.val32.upper;
  2072. c->SG[0].Len = iocommand.buf_size;
  2073. c->SG[0].Ext = 0; /* we are not chaining*/
  2074. }
  2075. hpsa_scsi_do_simple_cmd_core(h, c);
  2076. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  2077. check_ioctl_unit_attention(h, c);
  2078. /* Copy the error information out */
  2079. memcpy(&iocommand.error_info, c->err_info,
  2080. sizeof(iocommand.error_info));
  2081. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  2082. kfree(buff);
  2083. cmd_special_free(h, c);
  2084. return -EFAULT;
  2085. }
  2086. if (iocommand.Request.Type.Direction == XFER_READ) {
  2087. /* Copy the data out of the buffer we created */
  2088. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  2089. kfree(buff);
  2090. cmd_special_free(h, c);
  2091. return -EFAULT;
  2092. }
  2093. }
  2094. kfree(buff);
  2095. cmd_special_free(h, c);
  2096. return 0;
  2097. }
  2098. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2099. {
  2100. BIG_IOCTL_Command_struct *ioc;
  2101. struct CommandList *c;
  2102. unsigned char **buff = NULL;
  2103. int *buff_size = NULL;
  2104. union u64bit temp64;
  2105. BYTE sg_used = 0;
  2106. int status = 0;
  2107. int i;
  2108. u32 left;
  2109. u32 sz;
  2110. BYTE __user *data_ptr;
  2111. if (!argp)
  2112. return -EINVAL;
  2113. if (!capable(CAP_SYS_RAWIO))
  2114. return -EPERM;
  2115. ioc = (BIG_IOCTL_Command_struct *)
  2116. kmalloc(sizeof(*ioc), GFP_KERNEL);
  2117. if (!ioc) {
  2118. status = -ENOMEM;
  2119. goto cleanup1;
  2120. }
  2121. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  2122. status = -EFAULT;
  2123. goto cleanup1;
  2124. }
  2125. if ((ioc->buf_size < 1) &&
  2126. (ioc->Request.Type.Direction != XFER_NONE)) {
  2127. status = -EINVAL;
  2128. goto cleanup1;
  2129. }
  2130. /* Check kmalloc limits using all SGs */
  2131. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  2132. status = -EINVAL;
  2133. goto cleanup1;
  2134. }
  2135. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  2136. status = -EINVAL;
  2137. goto cleanup1;
  2138. }
  2139. buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  2140. if (!buff) {
  2141. status = -ENOMEM;
  2142. goto cleanup1;
  2143. }
  2144. buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
  2145. if (!buff_size) {
  2146. status = -ENOMEM;
  2147. goto cleanup1;
  2148. }
  2149. left = ioc->buf_size;
  2150. data_ptr = ioc->buf;
  2151. while (left) {
  2152. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  2153. buff_size[sg_used] = sz;
  2154. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  2155. if (buff[sg_used] == NULL) {
  2156. status = -ENOMEM;
  2157. goto cleanup1;
  2158. }
  2159. if (ioc->Request.Type.Direction == XFER_WRITE) {
  2160. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  2161. status = -ENOMEM;
  2162. goto cleanup1;
  2163. }
  2164. } else
  2165. memset(buff[sg_used], 0, sz);
  2166. left -= sz;
  2167. data_ptr += sz;
  2168. sg_used++;
  2169. }
  2170. c = cmd_special_alloc(h);
  2171. if (c == NULL) {
  2172. status = -ENOMEM;
  2173. goto cleanup1;
  2174. }
  2175. c->cmd_type = CMD_IOCTL_PEND;
  2176. c->Header.ReplyQueue = 0;
  2177. if (ioc->buf_size > 0) {
  2178. c->Header.SGList = sg_used;
  2179. c->Header.SGTotal = sg_used;
  2180. } else {
  2181. c->Header.SGList = 0;
  2182. c->Header.SGTotal = 0;
  2183. }
  2184. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  2185. c->Header.Tag.lower = c->busaddr;
  2186. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  2187. if (ioc->buf_size > 0) {
  2188. int i;
  2189. for (i = 0; i < sg_used; i++) {
  2190. temp64.val = pci_map_single(h->pdev, buff[i],
  2191. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  2192. c->SG[i].Addr.lower = temp64.val32.lower;
  2193. c->SG[i].Addr.upper = temp64.val32.upper;
  2194. c->SG[i].Len = buff_size[i];
  2195. /* we are not chaining */
  2196. c->SG[i].Ext = 0;
  2197. }
  2198. }
  2199. hpsa_scsi_do_simple_cmd_core(h, c);
  2200. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  2201. check_ioctl_unit_attention(h, c);
  2202. /* Copy the error information out */
  2203. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  2204. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  2205. cmd_special_free(h, c);
  2206. status = -EFAULT;
  2207. goto cleanup1;
  2208. }
  2209. if (ioc->Request.Type.Direction == XFER_READ) {
  2210. /* Copy the data out of the buffer we created */
  2211. BYTE __user *ptr = ioc->buf;
  2212. for (i = 0; i < sg_used; i++) {
  2213. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  2214. cmd_special_free(h, c);
  2215. status = -EFAULT;
  2216. goto cleanup1;
  2217. }
  2218. ptr += buff_size[i];
  2219. }
  2220. }
  2221. cmd_special_free(h, c);
  2222. status = 0;
  2223. cleanup1:
  2224. if (buff) {
  2225. for (i = 0; i < sg_used; i++)
  2226. kfree(buff[i]);
  2227. kfree(buff);
  2228. }
  2229. kfree(buff_size);
  2230. kfree(ioc);
  2231. return status;
  2232. }
  2233. static void check_ioctl_unit_attention(struct ctlr_info *h,
  2234. struct CommandList *c)
  2235. {
  2236. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  2237. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  2238. (void) check_for_unit_attention(h, c);
  2239. }
  2240. /*
  2241. * ioctl
  2242. */
  2243. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2244. {
  2245. struct ctlr_info *h;
  2246. void __user *argp = (void __user *)arg;
  2247. h = sdev_to_hba(dev);
  2248. switch (cmd) {
  2249. case CCISS_DEREGDISK:
  2250. case CCISS_REGNEWDISK:
  2251. case CCISS_REGNEWD:
  2252. hpsa_update_scsi_devices(h, dev->host->host_no);
  2253. return 0;
  2254. case CCISS_GETPCIINFO:
  2255. return hpsa_getpciinfo_ioctl(h, argp);
  2256. case CCISS_GETDRIVVER:
  2257. return hpsa_getdrivver_ioctl(h, argp);
  2258. case CCISS_PASSTHRU:
  2259. return hpsa_passthru_ioctl(h, argp);
  2260. case CCISS_BIG_PASSTHRU:
  2261. return hpsa_big_passthru_ioctl(h, argp);
  2262. default:
  2263. return -ENOTTY;
  2264. }
  2265. }
  2266. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  2267. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  2268. int cmd_type)
  2269. {
  2270. int pci_dir = XFER_NONE;
  2271. c->cmd_type = CMD_IOCTL_PEND;
  2272. c->Header.ReplyQueue = 0;
  2273. if (buff != NULL && size > 0) {
  2274. c->Header.SGList = 1;
  2275. c->Header.SGTotal = 1;
  2276. } else {
  2277. c->Header.SGList = 0;
  2278. c->Header.SGTotal = 0;
  2279. }
  2280. c->Header.Tag.lower = c->busaddr;
  2281. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2282. c->Request.Type.Type = cmd_type;
  2283. if (cmd_type == TYPE_CMD) {
  2284. switch (cmd) {
  2285. case HPSA_INQUIRY:
  2286. /* are we trying to read a vital product page */
  2287. if (page_code != 0) {
  2288. c->Request.CDB[1] = 0x01;
  2289. c->Request.CDB[2] = page_code;
  2290. }
  2291. c->Request.CDBLen = 6;
  2292. c->Request.Type.Attribute = ATTR_SIMPLE;
  2293. c->Request.Type.Direction = XFER_READ;
  2294. c->Request.Timeout = 0;
  2295. c->Request.CDB[0] = HPSA_INQUIRY;
  2296. c->Request.CDB[4] = size & 0xFF;
  2297. break;
  2298. case HPSA_REPORT_LOG:
  2299. case HPSA_REPORT_PHYS:
  2300. /* Talking to controller so It's a physical command
  2301. mode = 00 target = 0. Nothing to write.
  2302. */
  2303. c->Request.CDBLen = 12;
  2304. c->Request.Type.Attribute = ATTR_SIMPLE;
  2305. c->Request.Type.Direction = XFER_READ;
  2306. c->Request.Timeout = 0;
  2307. c->Request.CDB[0] = cmd;
  2308. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  2309. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2310. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2311. c->Request.CDB[9] = size & 0xFF;
  2312. break;
  2313. case HPSA_READ_CAPACITY:
  2314. c->Request.CDBLen = 10;
  2315. c->Request.Type.Attribute = ATTR_SIMPLE;
  2316. c->Request.Type.Direction = XFER_READ;
  2317. c->Request.Timeout = 0;
  2318. c->Request.CDB[0] = cmd;
  2319. break;
  2320. case HPSA_CACHE_FLUSH:
  2321. c->Request.CDBLen = 12;
  2322. c->Request.Type.Attribute = ATTR_SIMPLE;
  2323. c->Request.Type.Direction = XFER_WRITE;
  2324. c->Request.Timeout = 0;
  2325. c->Request.CDB[0] = BMIC_WRITE;
  2326. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2327. break;
  2328. case TEST_UNIT_READY:
  2329. c->Request.CDBLen = 6;
  2330. c->Request.Type.Attribute = ATTR_SIMPLE;
  2331. c->Request.Type.Direction = XFER_NONE;
  2332. c->Request.Timeout = 0;
  2333. break;
  2334. default:
  2335. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  2336. BUG();
  2337. return;
  2338. }
  2339. } else if (cmd_type == TYPE_MSG) {
  2340. switch (cmd) {
  2341. case HPSA_DEVICE_RESET_MSG:
  2342. c->Request.CDBLen = 16;
  2343. c->Request.Type.Type = 1; /* It is a MSG not a CMD */
  2344. c->Request.Type.Attribute = ATTR_SIMPLE;
  2345. c->Request.Type.Direction = XFER_NONE;
  2346. c->Request.Timeout = 0; /* Don't time out */
  2347. c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
  2348. c->Request.CDB[1] = 0x03; /* Reset target above */
  2349. /* If bytes 4-7 are zero, it means reset the */
  2350. /* LunID device */
  2351. c->Request.CDB[4] = 0x00;
  2352. c->Request.CDB[5] = 0x00;
  2353. c->Request.CDB[6] = 0x00;
  2354. c->Request.CDB[7] = 0x00;
  2355. break;
  2356. default:
  2357. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  2358. cmd);
  2359. BUG();
  2360. }
  2361. } else {
  2362. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  2363. BUG();
  2364. }
  2365. switch (c->Request.Type.Direction) {
  2366. case XFER_READ:
  2367. pci_dir = PCI_DMA_FROMDEVICE;
  2368. break;
  2369. case XFER_WRITE:
  2370. pci_dir = PCI_DMA_TODEVICE;
  2371. break;
  2372. case XFER_NONE:
  2373. pci_dir = PCI_DMA_NONE;
  2374. break;
  2375. default:
  2376. pci_dir = PCI_DMA_BIDIRECTIONAL;
  2377. }
  2378. hpsa_map_one(h->pdev, c, buff, size, pci_dir);
  2379. return;
  2380. }
  2381. /*
  2382. * Map (physical) PCI mem into (virtual) kernel space
  2383. */
  2384. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2385. {
  2386. ulong page_base = ((ulong) base) & PAGE_MASK;
  2387. ulong page_offs = ((ulong) base) - page_base;
  2388. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2389. return page_remapped ? (page_remapped + page_offs) : NULL;
  2390. }
  2391. /* Takes cmds off the submission queue and sends them to the hardware,
  2392. * then puts them on the queue of cmds waiting for completion.
  2393. */
  2394. static void start_io(struct ctlr_info *h)
  2395. {
  2396. struct CommandList *c;
  2397. while (!hlist_empty(&h->reqQ)) {
  2398. c = hlist_entry(h->reqQ.first, struct CommandList, list);
  2399. /* can't do anything if fifo is full */
  2400. if ((h->access.fifo_full(h))) {
  2401. dev_warn(&h->pdev->dev, "fifo full\n");
  2402. break;
  2403. }
  2404. /* Get the first entry from the Request Q */
  2405. removeQ(c);
  2406. h->Qdepth--;
  2407. /* Tell the controller execute command */
  2408. h->access.submit_command(h, c);
  2409. /* Put job onto the completed Q */
  2410. addQ(&h->cmpQ, c);
  2411. }
  2412. }
  2413. static inline unsigned long get_next_completion(struct ctlr_info *h)
  2414. {
  2415. return h->access.command_completed(h);
  2416. }
  2417. static inline bool interrupt_pending(struct ctlr_info *h)
  2418. {
  2419. return h->access.intr_pending(h);
  2420. }
  2421. static inline long interrupt_not_for_us(struct ctlr_info *h)
  2422. {
  2423. return ((h->access.intr_pending(h) == 0) ||
  2424. (h->interrupts_enabled == 0));
  2425. }
  2426. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  2427. u32 raw_tag)
  2428. {
  2429. if (unlikely(tag_index >= h->nr_cmds)) {
  2430. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  2431. return 1;
  2432. }
  2433. return 0;
  2434. }
  2435. static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
  2436. {
  2437. removeQ(c);
  2438. if (likely(c->cmd_type == CMD_SCSI))
  2439. complete_scsi_command(c, 0, raw_tag);
  2440. else if (c->cmd_type == CMD_IOCTL_PEND)
  2441. complete(c->waiting);
  2442. }
  2443. static inline u32 hpsa_tag_contains_index(u32 tag)
  2444. {
  2445. #define DIRECT_LOOKUP_BIT 0x04
  2446. return tag & DIRECT_LOOKUP_BIT;
  2447. }
  2448. static inline u32 hpsa_tag_to_index(u32 tag)
  2449. {
  2450. #define DIRECT_LOOKUP_SHIFT 3
  2451. return tag >> DIRECT_LOOKUP_SHIFT;
  2452. }
  2453. static inline u32 hpsa_tag_discard_error_bits(u32 tag)
  2454. {
  2455. #define HPSA_ERROR_BITS 0x03
  2456. return tag & ~HPSA_ERROR_BITS;
  2457. }
  2458. static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
  2459. {
  2460. struct ctlr_info *h = dev_id;
  2461. struct CommandList *c;
  2462. unsigned long flags;
  2463. u32 raw_tag, tag, tag_index;
  2464. struct hlist_node *tmp;
  2465. if (interrupt_not_for_us(h))
  2466. return IRQ_NONE;
  2467. spin_lock_irqsave(&h->lock, flags);
  2468. while (interrupt_pending(h)) {
  2469. while ((raw_tag = get_next_completion(h)) != FIFO_EMPTY) {
  2470. if (likely(hpsa_tag_contains_index(raw_tag))) {
  2471. tag_index = hpsa_tag_to_index(raw_tag);
  2472. if (bad_tag(h, tag_index, raw_tag))
  2473. return IRQ_HANDLED;
  2474. c = h->cmd_pool + tag_index;
  2475. finish_cmd(c, raw_tag);
  2476. continue;
  2477. }
  2478. tag = hpsa_tag_discard_error_bits(raw_tag);
  2479. c = NULL;
  2480. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  2481. if (c->busaddr == tag) {
  2482. finish_cmd(c, raw_tag);
  2483. break;
  2484. }
  2485. }
  2486. }
  2487. }
  2488. spin_unlock_irqrestore(&h->lock, flags);
  2489. return IRQ_HANDLED;
  2490. }
  2491. /* Send a message CDB to the firmware. */
  2492. static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  2493. unsigned char type)
  2494. {
  2495. struct Command {
  2496. struct CommandListHeader CommandHeader;
  2497. struct RequestBlock Request;
  2498. struct ErrDescriptor ErrorDescriptor;
  2499. };
  2500. struct Command *cmd;
  2501. static const size_t cmd_sz = sizeof(*cmd) +
  2502. sizeof(cmd->ErrorDescriptor);
  2503. dma_addr_t paddr64;
  2504. uint32_t paddr32, tag;
  2505. void __iomem *vaddr;
  2506. int i, err;
  2507. vaddr = pci_ioremap_bar(pdev, 0);
  2508. if (vaddr == NULL)
  2509. return -ENOMEM;
  2510. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  2511. * CCISS commands, so they must be allocated from the lower 4GiB of
  2512. * memory.
  2513. */
  2514. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2515. if (err) {
  2516. iounmap(vaddr);
  2517. return -ENOMEM;
  2518. }
  2519. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  2520. if (cmd == NULL) {
  2521. iounmap(vaddr);
  2522. return -ENOMEM;
  2523. }
  2524. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  2525. * although there's no guarantee, we assume that the address is at
  2526. * least 4-byte aligned (most likely, it's page-aligned).
  2527. */
  2528. paddr32 = paddr64;
  2529. cmd->CommandHeader.ReplyQueue = 0;
  2530. cmd->CommandHeader.SGList = 0;
  2531. cmd->CommandHeader.SGTotal = 0;
  2532. cmd->CommandHeader.Tag.lower = paddr32;
  2533. cmd->CommandHeader.Tag.upper = 0;
  2534. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  2535. cmd->Request.CDBLen = 16;
  2536. cmd->Request.Type.Type = TYPE_MSG;
  2537. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  2538. cmd->Request.Type.Direction = XFER_NONE;
  2539. cmd->Request.Timeout = 0; /* Don't time out */
  2540. cmd->Request.CDB[0] = opcode;
  2541. cmd->Request.CDB[1] = type;
  2542. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  2543. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
  2544. cmd->ErrorDescriptor.Addr.upper = 0;
  2545. cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
  2546. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  2547. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  2548. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  2549. if (hpsa_tag_discard_error_bits(tag) == paddr32)
  2550. break;
  2551. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  2552. }
  2553. iounmap(vaddr);
  2554. /* we leak the DMA buffer here ... no choice since the controller could
  2555. * still complete the command.
  2556. */
  2557. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  2558. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  2559. opcode, type);
  2560. return -ETIMEDOUT;
  2561. }
  2562. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  2563. if (tag & HPSA_ERROR_BIT) {
  2564. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  2565. opcode, type);
  2566. return -EIO;
  2567. }
  2568. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  2569. opcode, type);
  2570. return 0;
  2571. }
  2572. #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
  2573. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  2574. static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
  2575. {
  2576. /* the #defines are stolen from drivers/pci/msi.h. */
  2577. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  2578. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  2579. int pos;
  2580. u16 control = 0;
  2581. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  2582. if (pos) {
  2583. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2584. if (control & PCI_MSI_FLAGS_ENABLE) {
  2585. dev_info(&pdev->dev, "resetting MSI\n");
  2586. pci_write_config_word(pdev, msi_control_reg(pos),
  2587. control & ~PCI_MSI_FLAGS_ENABLE);
  2588. }
  2589. }
  2590. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  2591. if (pos) {
  2592. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2593. if (control & PCI_MSIX_FLAGS_ENABLE) {
  2594. dev_info(&pdev->dev, "resetting MSI-X\n");
  2595. pci_write_config_word(pdev, msi_control_reg(pos),
  2596. control & ~PCI_MSIX_FLAGS_ENABLE);
  2597. }
  2598. }
  2599. return 0;
  2600. }
  2601. /* This does a hard reset of the controller using PCI power management
  2602. * states.
  2603. */
  2604. static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
  2605. {
  2606. u16 pmcsr, saved_config_space[32];
  2607. int i, pos;
  2608. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  2609. /* This is very nearly the same thing as
  2610. *
  2611. * pci_save_state(pci_dev);
  2612. * pci_set_power_state(pci_dev, PCI_D3hot);
  2613. * pci_set_power_state(pci_dev, PCI_D0);
  2614. * pci_restore_state(pci_dev);
  2615. *
  2616. * but we can't use these nice canned kernel routines on
  2617. * kexec, because they also check the MSI/MSI-X state in PCI
  2618. * configuration space and do the wrong thing when it is
  2619. * set/cleared. Also, the pci_save/restore_state functions
  2620. * violate the ordering requirements for restoring the
  2621. * configuration space from the CCISS document (see the
  2622. * comment below). So we roll our own ....
  2623. */
  2624. for (i = 0; i < 32; i++)
  2625. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  2626. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  2627. if (pos == 0) {
  2628. dev_err(&pdev->dev,
  2629. "hpsa_reset_controller: PCI PM not supported\n");
  2630. return -ENODEV;
  2631. }
  2632. /* Quoting from the Open CISS Specification: "The Power
  2633. * Management Control/Status Register (CSR) controls the power
  2634. * state of the device. The normal operating state is D0,
  2635. * CSR=00h. The software off state is D3, CSR=03h. To reset
  2636. * the controller, place the interface device in D3 then to
  2637. * D0, this causes a secondary PCI reset which will reset the
  2638. * controller."
  2639. */
  2640. /* enter the D3hot power management state */
  2641. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  2642. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2643. pmcsr |= PCI_D3hot;
  2644. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2645. msleep(500);
  2646. /* enter the D0 power management state */
  2647. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2648. pmcsr |= PCI_D0;
  2649. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2650. msleep(500);
  2651. /* Restore the PCI configuration space. The Open CISS
  2652. * Specification says, "Restore the PCI Configuration
  2653. * Registers, offsets 00h through 60h. It is important to
  2654. * restore the command register, 16-bits at offset 04h,
  2655. * last. Do not restore the configuration status register,
  2656. * 16-bits at offset 06h." Note that the offset is 2*i.
  2657. */
  2658. for (i = 0; i < 32; i++) {
  2659. if (i == 2 || i == 3)
  2660. continue;
  2661. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  2662. }
  2663. wmb();
  2664. pci_write_config_word(pdev, 4, saved_config_space[2]);
  2665. return 0;
  2666. }
  2667. /*
  2668. * We cannot read the structure directly, for portability we must use
  2669. * the io functions.
  2670. * This is for debug only.
  2671. */
  2672. #ifdef HPSA_DEBUG
  2673. static void print_cfg_table(struct device *dev, struct CfgTable *tb)
  2674. {
  2675. int i;
  2676. char temp_name[17];
  2677. dev_info(dev, "Controller Configuration information\n");
  2678. dev_info(dev, "------------------------------------\n");
  2679. for (i = 0; i < 4; i++)
  2680. temp_name[i] = readb(&(tb->Signature[i]));
  2681. temp_name[4] = '\0';
  2682. dev_info(dev, " Signature = %s\n", temp_name);
  2683. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  2684. dev_info(dev, " Transport methods supported = 0x%x\n",
  2685. readl(&(tb->TransportSupport)));
  2686. dev_info(dev, " Transport methods active = 0x%x\n",
  2687. readl(&(tb->TransportActive)));
  2688. dev_info(dev, " Requested transport Method = 0x%x\n",
  2689. readl(&(tb->HostWrite.TransportRequest)));
  2690. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  2691. readl(&(tb->HostWrite.CoalIntDelay)));
  2692. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  2693. readl(&(tb->HostWrite.CoalIntCount)));
  2694. dev_info(dev, " Max outstanding commands = 0x%d\n",
  2695. readl(&(tb->CmdsOutMax)));
  2696. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  2697. for (i = 0; i < 16; i++)
  2698. temp_name[i] = readb(&(tb->ServerName[i]));
  2699. temp_name[16] = '\0';
  2700. dev_info(dev, " Server Name = %s\n", temp_name);
  2701. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  2702. readl(&(tb->HeartBeat)));
  2703. }
  2704. #endif /* HPSA_DEBUG */
  2705. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  2706. {
  2707. int i, offset, mem_type, bar_type;
  2708. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  2709. return 0;
  2710. offset = 0;
  2711. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2712. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  2713. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  2714. offset += 4;
  2715. else {
  2716. mem_type = pci_resource_flags(pdev, i) &
  2717. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  2718. switch (mem_type) {
  2719. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2720. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  2721. offset += 4; /* 32 bit */
  2722. break;
  2723. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2724. offset += 8;
  2725. break;
  2726. default: /* reserved in PCI 2.2 */
  2727. dev_warn(&pdev->dev,
  2728. "base address is invalid\n");
  2729. return -1;
  2730. break;
  2731. }
  2732. }
  2733. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  2734. return i + 1;
  2735. }
  2736. return -1;
  2737. }
  2738. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  2739. * controllers that are capable. If not, we use IO-APIC mode.
  2740. */
  2741. static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
  2742. struct pci_dev *pdev, u32 board_id)
  2743. {
  2744. #ifdef CONFIG_PCI_MSI
  2745. int err;
  2746. struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
  2747. {0, 2}, {0, 3}
  2748. };
  2749. /* Some boards advertise MSI but don't really support it */
  2750. if ((board_id == 0x40700E11) ||
  2751. (board_id == 0x40800E11) ||
  2752. (board_id == 0x40820E11) || (board_id == 0x40830E11))
  2753. goto default_int_mode;
  2754. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  2755. dev_info(&pdev->dev, "MSIX\n");
  2756. err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
  2757. if (!err) {
  2758. h->intr[0] = hpsa_msix_entries[0].vector;
  2759. h->intr[1] = hpsa_msix_entries[1].vector;
  2760. h->intr[2] = hpsa_msix_entries[2].vector;
  2761. h->intr[3] = hpsa_msix_entries[3].vector;
  2762. h->msix_vector = 1;
  2763. return;
  2764. }
  2765. if (err > 0) {
  2766. dev_warn(&pdev->dev, "only %d MSI-X vectors "
  2767. "available\n", err);
  2768. goto default_int_mode;
  2769. } else {
  2770. dev_warn(&pdev->dev, "MSI-X init failed %d\n",
  2771. err);
  2772. goto default_int_mode;
  2773. }
  2774. }
  2775. if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
  2776. dev_info(&pdev->dev, "MSI\n");
  2777. if (!pci_enable_msi(pdev))
  2778. h->msi_vector = 1;
  2779. else
  2780. dev_warn(&pdev->dev, "MSI init failed\n");
  2781. }
  2782. default_int_mode:
  2783. #endif /* CONFIG_PCI_MSI */
  2784. /* if we get here we're going to use the default interrupt mode */
  2785. h->intr[SIMPLE_MODE_INT] = pdev->irq;
  2786. }
  2787. static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
  2788. {
  2789. ushort subsystem_vendor_id, subsystem_device_id, command;
  2790. u32 board_id, scratchpad = 0;
  2791. u64 cfg_offset;
  2792. u32 cfg_base_addr;
  2793. u64 cfg_base_addr_index;
  2794. int i, prod_index, err;
  2795. subsystem_vendor_id = pdev->subsystem_vendor;
  2796. subsystem_device_id = pdev->subsystem_device;
  2797. board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
  2798. subsystem_vendor_id);
  2799. for (i = 0; i < ARRAY_SIZE(products); i++)
  2800. if (board_id == products[i].board_id)
  2801. break;
  2802. prod_index = i;
  2803. if (prod_index == ARRAY_SIZE(products)) {
  2804. prod_index--;
  2805. if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
  2806. !hpsa_allow_any) {
  2807. dev_warn(&pdev->dev, "unrecognized board ID:"
  2808. " 0x%08lx, ignoring.\n",
  2809. (unsigned long) board_id);
  2810. return -ENODEV;
  2811. }
  2812. }
  2813. /* check to see if controller has been disabled
  2814. * BEFORE trying to enable it
  2815. */
  2816. (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
  2817. if (!(command & 0x02)) {
  2818. dev_warn(&pdev->dev, "controller appears to be disabled\n");
  2819. return -ENODEV;
  2820. }
  2821. err = pci_enable_device(pdev);
  2822. if (err) {
  2823. dev_warn(&pdev->dev, "unable to enable PCI device\n");
  2824. return err;
  2825. }
  2826. err = pci_request_regions(pdev, "hpsa");
  2827. if (err) {
  2828. dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
  2829. return err;
  2830. }
  2831. /* If the kernel supports MSI/MSI-X we will try to enable that,
  2832. * else we use the IO-APIC interrupt assigned to us by system ROM.
  2833. */
  2834. hpsa_interrupt_mode(h, pdev, board_id);
  2835. /* find the memory BAR */
  2836. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2837. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
  2838. break;
  2839. }
  2840. if (i == DEVICE_COUNT_RESOURCE) {
  2841. dev_warn(&pdev->dev, "no memory BAR found\n");
  2842. err = -ENODEV;
  2843. goto err_out_free_res;
  2844. }
  2845. h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
  2846. * already removed
  2847. */
  2848. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  2849. /* Wait for the board to become ready. */
  2850. for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
  2851. scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  2852. if (scratchpad == HPSA_FIRMWARE_READY)
  2853. break;
  2854. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  2855. }
  2856. if (scratchpad != HPSA_FIRMWARE_READY) {
  2857. dev_warn(&pdev->dev, "board not ready, timed out.\n");
  2858. err = -ENODEV;
  2859. goto err_out_free_res;
  2860. }
  2861. /* get the address index number */
  2862. cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
  2863. cfg_base_addr &= (u32) 0x0000ffff;
  2864. cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
  2865. if (cfg_base_addr_index == -1) {
  2866. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  2867. err = -ENODEV;
  2868. goto err_out_free_res;
  2869. }
  2870. cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
  2871. h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  2872. cfg_base_addr_index) + cfg_offset,
  2873. sizeof(h->cfgtable));
  2874. h->board_id = board_id;
  2875. /* Query controller for max supported commands: */
  2876. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  2877. h->product_name = products[prod_index].product_name;
  2878. h->access = *(products[prod_index].access);
  2879. /* Allow room for some ioctls */
  2880. h->nr_cmds = h->max_commands - 4;
  2881. if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
  2882. (readb(&h->cfgtable->Signature[1]) != 'I') ||
  2883. (readb(&h->cfgtable->Signature[2]) != 'S') ||
  2884. (readb(&h->cfgtable->Signature[3]) != 'S')) {
  2885. dev_warn(&pdev->dev, "not a valid CISS config table\n");
  2886. err = -ENODEV;
  2887. goto err_out_free_res;
  2888. }
  2889. #ifdef CONFIG_X86
  2890. {
  2891. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  2892. u32 prefetch;
  2893. prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
  2894. prefetch |= 0x100;
  2895. writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
  2896. }
  2897. #endif
  2898. /* Disabling DMA prefetch for the P600
  2899. * An ASIC bug may result in a prefetch beyond
  2900. * physical memory.
  2901. */
  2902. if (board_id == 0x3225103C) {
  2903. u32 dma_prefetch;
  2904. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  2905. dma_prefetch |= 0x8000;
  2906. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  2907. }
  2908. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  2909. /* Update the field, and then ring the doorbell */
  2910. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  2911. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  2912. /* under certain very rare conditions, this can take awhile.
  2913. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  2914. * as we enter this code.)
  2915. */
  2916. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  2917. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  2918. break;
  2919. /* delay and try again */
  2920. msleep(10);
  2921. }
  2922. #ifdef HPSA_DEBUG
  2923. print_cfg_table(&pdev->dev, h->cfgtable);
  2924. #endif /* HPSA_DEBUG */
  2925. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
  2926. dev_warn(&pdev->dev, "unable to get board into simple mode\n");
  2927. err = -ENODEV;
  2928. goto err_out_free_res;
  2929. }
  2930. return 0;
  2931. err_out_free_res:
  2932. /*
  2933. * Deliberately omit pci_disable_device(): it does something nasty to
  2934. * Smart Array controllers that pci_enable_device does not undo
  2935. */
  2936. pci_release_regions(pdev);
  2937. return err;
  2938. }
  2939. static int __devinit hpsa_init_one(struct pci_dev *pdev,
  2940. const struct pci_device_id *ent)
  2941. {
  2942. int i, rc;
  2943. int dac;
  2944. struct ctlr_info *h;
  2945. if (number_of_controllers == 0)
  2946. printk(KERN_INFO DRIVER_NAME "\n");
  2947. if (reset_devices) {
  2948. /* Reset the controller with a PCI power-cycle */
  2949. if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
  2950. return -ENODEV;
  2951. /* Some devices (notably the HP Smart Array 5i Controller)
  2952. need a little pause here */
  2953. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  2954. /* Now try to get the controller to respond to a no-op */
  2955. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  2956. if (hpsa_noop(pdev) == 0)
  2957. break;
  2958. else
  2959. dev_warn(&pdev->dev, "no-op failed%s\n",
  2960. (i < 11 ? "; re-trying" : ""));
  2961. }
  2962. }
  2963. BUILD_BUG_ON(sizeof(struct CommandList) % 8);
  2964. h = kzalloc(sizeof(*h), GFP_KERNEL);
  2965. if (!h)
  2966. return -ENOMEM;
  2967. h->busy_initializing = 1;
  2968. INIT_HLIST_HEAD(&h->cmpQ);
  2969. INIT_HLIST_HEAD(&h->reqQ);
  2970. mutex_init(&h->busy_shutting_down);
  2971. init_completion(&h->scan_wait);
  2972. rc = hpsa_pci_init(h, pdev);
  2973. if (rc != 0)
  2974. goto clean1;
  2975. sprintf(h->devname, "hpsa%d", number_of_controllers);
  2976. h->ctlr = number_of_controllers;
  2977. number_of_controllers++;
  2978. h->pdev = pdev;
  2979. /* configure PCI DMA stuff */
  2980. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  2981. if (rc == 0) {
  2982. dac = 1;
  2983. } else {
  2984. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2985. if (rc == 0) {
  2986. dac = 0;
  2987. } else {
  2988. dev_err(&pdev->dev, "no suitable DMA available\n");
  2989. goto clean1;
  2990. }
  2991. }
  2992. /* make sure the board interrupts are off */
  2993. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  2994. rc = request_irq(h->intr[SIMPLE_MODE_INT], do_hpsa_intr,
  2995. IRQF_DISABLED | IRQF_SHARED, h->devname, h);
  2996. if (rc) {
  2997. dev_err(&pdev->dev, "unable to get irq %d for %s\n",
  2998. h->intr[SIMPLE_MODE_INT], h->devname);
  2999. goto clean2;
  3000. }
  3001. dev_info(&pdev->dev, "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
  3002. h->devname, pdev->device, pci_name(pdev),
  3003. h->intr[SIMPLE_MODE_INT], dac ? "" : " not");
  3004. h->cmd_pool_bits =
  3005. kmalloc(((h->nr_cmds + BITS_PER_LONG -
  3006. 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
  3007. h->cmd_pool = pci_alloc_consistent(h->pdev,
  3008. h->nr_cmds * sizeof(*h->cmd_pool),
  3009. &(h->cmd_pool_dhandle));
  3010. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  3011. h->nr_cmds * sizeof(*h->errinfo_pool),
  3012. &(h->errinfo_pool_dhandle));
  3013. if ((h->cmd_pool_bits == NULL)
  3014. || (h->cmd_pool == NULL)
  3015. || (h->errinfo_pool == NULL)) {
  3016. dev_err(&pdev->dev, "out of memory");
  3017. rc = -ENOMEM;
  3018. goto clean4;
  3019. }
  3020. spin_lock_init(&h->lock);
  3021. pci_set_drvdata(pdev, h);
  3022. memset(h->cmd_pool_bits, 0,
  3023. ((h->nr_cmds + BITS_PER_LONG -
  3024. 1) / BITS_PER_LONG) * sizeof(unsigned long));
  3025. hpsa_scsi_setup(h);
  3026. /* Turn the interrupts on so we can service requests */
  3027. h->access.set_intr_mask(h, HPSA_INTR_ON);
  3028. hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
  3029. h->busy_initializing = 0;
  3030. return 1;
  3031. clean4:
  3032. kfree(h->cmd_pool_bits);
  3033. if (h->cmd_pool)
  3034. pci_free_consistent(h->pdev,
  3035. h->nr_cmds * sizeof(struct CommandList),
  3036. h->cmd_pool, h->cmd_pool_dhandle);
  3037. if (h->errinfo_pool)
  3038. pci_free_consistent(h->pdev,
  3039. h->nr_cmds * sizeof(struct ErrorInfo),
  3040. h->errinfo_pool,
  3041. h->errinfo_pool_dhandle);
  3042. free_irq(h->intr[SIMPLE_MODE_INT], h);
  3043. clean2:
  3044. clean1:
  3045. h->busy_initializing = 0;
  3046. kfree(h);
  3047. return rc;
  3048. }
  3049. static void hpsa_flush_cache(struct ctlr_info *h)
  3050. {
  3051. char *flush_buf;
  3052. struct CommandList *c;
  3053. flush_buf = kzalloc(4, GFP_KERNEL);
  3054. if (!flush_buf)
  3055. return;
  3056. c = cmd_special_alloc(h);
  3057. if (!c) {
  3058. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  3059. goto out_of_memory;
  3060. }
  3061. fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  3062. RAID_CTLR_LUNID, TYPE_CMD);
  3063. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
  3064. if (c->err_info->CommandStatus != 0)
  3065. dev_warn(&h->pdev->dev,
  3066. "error flushing cache on controller\n");
  3067. cmd_special_free(h, c);
  3068. out_of_memory:
  3069. kfree(flush_buf);
  3070. }
  3071. static void hpsa_shutdown(struct pci_dev *pdev)
  3072. {
  3073. struct ctlr_info *h;
  3074. h = pci_get_drvdata(pdev);
  3075. /* Turn board interrupts off and send the flush cache command
  3076. * sendcmd will turn off interrupt, and send the flush...
  3077. * To write all data in the battery backed cache to disks
  3078. */
  3079. hpsa_flush_cache(h);
  3080. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3081. free_irq(h->intr[2], h);
  3082. #ifdef CONFIG_PCI_MSI
  3083. if (h->msix_vector)
  3084. pci_disable_msix(h->pdev);
  3085. else if (h->msi_vector)
  3086. pci_disable_msi(h->pdev);
  3087. #endif /* CONFIG_PCI_MSI */
  3088. }
  3089. static void __devexit hpsa_remove_one(struct pci_dev *pdev)
  3090. {
  3091. struct ctlr_info *h;
  3092. if (pci_get_drvdata(pdev) == NULL) {
  3093. dev_err(&pdev->dev, "unable to remove device \n");
  3094. return;
  3095. }
  3096. h = pci_get_drvdata(pdev);
  3097. mutex_lock(&h->busy_shutting_down);
  3098. remove_from_scan_list(h);
  3099. hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
  3100. hpsa_shutdown(pdev);
  3101. iounmap(h->vaddr);
  3102. pci_free_consistent(h->pdev,
  3103. h->nr_cmds * sizeof(struct CommandList),
  3104. h->cmd_pool, h->cmd_pool_dhandle);
  3105. pci_free_consistent(h->pdev,
  3106. h->nr_cmds * sizeof(struct ErrorInfo),
  3107. h->errinfo_pool, h->errinfo_pool_dhandle);
  3108. kfree(h->cmd_pool_bits);
  3109. /*
  3110. * Deliberately omit pci_disable_device(): it does something nasty to
  3111. * Smart Array controllers that pci_enable_device does not undo
  3112. */
  3113. pci_release_regions(pdev);
  3114. pci_set_drvdata(pdev, NULL);
  3115. mutex_unlock(&h->busy_shutting_down);
  3116. kfree(h);
  3117. }
  3118. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  3119. __attribute__((unused)) pm_message_t state)
  3120. {
  3121. return -ENOSYS;
  3122. }
  3123. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  3124. {
  3125. return -ENOSYS;
  3126. }
  3127. static struct pci_driver hpsa_pci_driver = {
  3128. .name = "hpsa",
  3129. .probe = hpsa_init_one,
  3130. .remove = __devexit_p(hpsa_remove_one),
  3131. .id_table = hpsa_pci_device_id, /* id_table */
  3132. .shutdown = hpsa_shutdown,
  3133. .suspend = hpsa_suspend,
  3134. .resume = hpsa_resume,
  3135. };
  3136. /*
  3137. * This is it. Register the PCI driver information for the cards we control
  3138. * the OS will call our registered routines when it finds one of our cards.
  3139. */
  3140. static int __init hpsa_init(void)
  3141. {
  3142. int err;
  3143. /* Start the scan thread */
  3144. hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
  3145. if (IS_ERR(hpsa_scan_thread)) {
  3146. err = PTR_ERR(hpsa_scan_thread);
  3147. return -ENODEV;
  3148. }
  3149. err = pci_register_driver(&hpsa_pci_driver);
  3150. if (err)
  3151. kthread_stop(hpsa_scan_thread);
  3152. return err;
  3153. }
  3154. static void __exit hpsa_cleanup(void)
  3155. {
  3156. pci_unregister_driver(&hpsa_pci_driver);
  3157. kthread_stop(hpsa_scan_thread);
  3158. }
  3159. module_init(hpsa_init);
  3160. module_exit(hpsa_cleanup);