qla_os.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/moduleparam.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/delay.h>
  11. #include <linux/kthread.h>
  12. #include <scsi/scsi_tcq.h>
  13. #include <scsi/scsicam.h>
  14. #include <scsi/scsi_transport.h>
  15. #include <scsi/scsi_transport_fc.h>
  16. /*
  17. * Driver version
  18. */
  19. char qla2x00_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static kmem_cache_t *srb_cachep;
  24. /*
  25. * Ioctl related information.
  26. */
  27. static int num_hosts;
  28. int ql2xlogintimeout = 20;
  29. module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
  30. MODULE_PARM_DESC(ql2xlogintimeout,
  31. "Login timeout value in seconds.");
  32. int qlport_down_retry = 30;
  33. module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
  34. MODULE_PARM_DESC(qlport_down_retry,
  35. "Maximum number of command retries to a port that returns "
  36. "a PORT-DOWN status.");
  37. int ql2xplogiabsentdevice;
  38. module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
  39. MODULE_PARM_DESC(ql2xplogiabsentdevice,
  40. "Option to enable PLOGI to devices that are not present after "
  41. "a Fabric scan. This is needed for several broken switches. "
  42. "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
  43. int ql2xloginretrycount = 0;
  44. module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
  45. MODULE_PARM_DESC(ql2xloginretrycount,
  46. "Specify an alternate value for the NVRAM login retry count.");
  47. int ql2xallocfwdump = 1;
  48. module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
  49. MODULE_PARM_DESC(ql2xallocfwdump,
  50. "Option to enable allocation of memory for a firmware dump "
  51. "during HBA initialization. Memory allocation requirements "
  52. "vary by ISP type. Default is 1 - allocate memory.");
  53. int ql2xextended_error_logging;
  54. module_param(ql2xextended_error_logging, int, S_IRUGO|S_IRUSR);
  55. MODULE_PARM_DESC(ql2xextended_error_logging,
  56. "Option to enable extended error logging, "
  57. "Default is 0 - no logging. 1 - log errors.");
  58. static void qla2x00_free_device(scsi_qla_host_t *);
  59. static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
  60. int ql2xfdmienable;
  61. module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
  62. MODULE_PARM_DESC(ql2xfdmienable,
  63. "Enables FDMI registratons "
  64. "Default is 0 - no FDMI. 1 - perfom FDMI.");
  65. #define MAX_Q_DEPTH 32
  66. static int ql2xmaxqdepth = MAX_Q_DEPTH;
  67. module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
  68. MODULE_PARM_DESC(ql2xmaxqdepth,
  69. "Maximum queue depth to report for target devices.");
  70. int ql2xqfullrampup = 120;
  71. module_param(ql2xqfullrampup, int, S_IRUGO|S_IWUSR);
  72. MODULE_PARM_DESC(ql2xqfullrampup,
  73. "Number of seconds to wait to begin to ramp-up the queue "
  74. "depth for a device after a queue-full condition has been "
  75. "detected. Default is 120 seconds.");
  76. /*
  77. * SCSI host template entry points
  78. */
  79. static int qla2xxx_slave_configure(struct scsi_device * device);
  80. static int qla2xxx_slave_alloc(struct scsi_device *);
  81. static void qla2xxx_slave_destroy(struct scsi_device *);
  82. static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
  83. void (*fn)(struct scsi_cmnd *));
  84. static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
  85. void (*fn)(struct scsi_cmnd *));
  86. static int qla2xxx_eh_abort(struct scsi_cmnd *);
  87. static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
  88. static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
  89. static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
  90. static int qla2x00_loop_reset(scsi_qla_host_t *ha);
  91. static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
  92. static int qla2x00_change_queue_depth(struct scsi_device *, int);
  93. static int qla2x00_change_queue_type(struct scsi_device *, int);
  94. static struct scsi_host_template qla2x00_driver_template = {
  95. .module = THIS_MODULE,
  96. .name = QLA2XXX_DRIVER_NAME,
  97. .queuecommand = qla2x00_queuecommand,
  98. .eh_abort_handler = qla2xxx_eh_abort,
  99. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  100. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  101. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  102. .slave_configure = qla2xxx_slave_configure,
  103. .slave_alloc = qla2xxx_slave_alloc,
  104. .slave_destroy = qla2xxx_slave_destroy,
  105. .change_queue_depth = qla2x00_change_queue_depth,
  106. .change_queue_type = qla2x00_change_queue_type,
  107. .this_id = -1,
  108. .cmd_per_lun = 3,
  109. .use_clustering = ENABLE_CLUSTERING,
  110. .sg_tablesize = SG_ALL,
  111. /*
  112. * The RISC allows for each command to transfer (2^32-1) bytes of data,
  113. * which equates to 0x800000 sectors.
  114. */
  115. .max_sectors = 0xFFFF,
  116. .shost_attrs = qla2x00_host_attrs,
  117. };
  118. static struct scsi_host_template qla24xx_driver_template = {
  119. .module = THIS_MODULE,
  120. .name = QLA2XXX_DRIVER_NAME,
  121. .queuecommand = qla24xx_queuecommand,
  122. .eh_abort_handler = qla2xxx_eh_abort,
  123. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  124. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  125. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  126. .slave_configure = qla2xxx_slave_configure,
  127. .slave_alloc = qla2xxx_slave_alloc,
  128. .slave_destroy = qla2xxx_slave_destroy,
  129. .change_queue_depth = qla2x00_change_queue_depth,
  130. .change_queue_type = qla2x00_change_queue_type,
  131. .this_id = -1,
  132. .cmd_per_lun = 3,
  133. .use_clustering = ENABLE_CLUSTERING,
  134. .sg_tablesize = SG_ALL,
  135. .max_sectors = 0xFFFF,
  136. .shost_attrs = qla2x00_host_attrs,
  137. };
  138. static struct scsi_transport_template *qla2xxx_transport_template = NULL;
  139. /* TODO Convert to inlines
  140. *
  141. * Timer routines
  142. */
  143. #define WATCH_INTERVAL 1 /* number of seconds */
  144. static void qla2x00_timer(scsi_qla_host_t *);
  145. static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
  146. void *, unsigned long);
  147. static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
  148. static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
  149. static inline void
  150. qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
  151. {
  152. init_timer(&ha->timer);
  153. ha->timer.expires = jiffies + interval * HZ;
  154. ha->timer.data = (unsigned long)ha;
  155. ha->timer.function = (void (*)(unsigned long))func;
  156. add_timer(&ha->timer);
  157. ha->timer_active = 1;
  158. }
  159. static inline void
  160. qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
  161. {
  162. mod_timer(&ha->timer, jiffies + interval * HZ);
  163. }
  164. static __inline__ void
  165. qla2x00_stop_timer(scsi_qla_host_t *ha)
  166. {
  167. del_timer_sync(&ha->timer);
  168. ha->timer_active = 0;
  169. }
  170. static int qla2x00_do_dpc(void *data);
  171. static void qla2x00_rst_aen(scsi_qla_host_t *);
  172. static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
  173. static void qla2x00_mem_free(scsi_qla_host_t *ha);
  174. static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
  175. static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
  176. static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
  177. void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
  178. /* -------------------------------------------------------------------------- */
  179. static char *
  180. qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
  181. {
  182. static char *pci_bus_modes[] = {
  183. "33", "66", "100", "133",
  184. };
  185. uint16_t pci_bus;
  186. strcpy(str, "PCI");
  187. pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
  188. if (pci_bus) {
  189. strcat(str, "-X (");
  190. strcat(str, pci_bus_modes[pci_bus]);
  191. } else {
  192. pci_bus = (ha->pci_attr & BIT_8) >> 8;
  193. strcat(str, " (");
  194. strcat(str, pci_bus_modes[pci_bus]);
  195. }
  196. strcat(str, " MHz)");
  197. return (str);
  198. }
  199. static char *
  200. qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
  201. {
  202. static char *pci_bus_modes[] = { "33", "66", "100", "133", };
  203. uint32_t pci_bus;
  204. int pcie_reg;
  205. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  206. if (pcie_reg) {
  207. char lwstr[6];
  208. uint16_t pcie_lstat, lspeed, lwidth;
  209. pcie_reg += 0x12;
  210. pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
  211. lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
  212. lwidth = (pcie_lstat &
  213. (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
  214. strcpy(str, "PCIe (");
  215. if (lspeed == 1)
  216. strcat(str, "2.5Gb/s ");
  217. else
  218. strcat(str, "<unknown> ");
  219. snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
  220. strcat(str, lwstr);
  221. return str;
  222. }
  223. strcpy(str, "PCI");
  224. pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
  225. if (pci_bus == 0 || pci_bus == 8) {
  226. strcat(str, " (");
  227. strcat(str, pci_bus_modes[pci_bus >> 3]);
  228. } else {
  229. strcat(str, "-X ");
  230. if (pci_bus & BIT_2)
  231. strcat(str, "Mode 2");
  232. else
  233. strcat(str, "Mode 1");
  234. strcat(str, " (");
  235. strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
  236. }
  237. strcat(str, " MHz)");
  238. return str;
  239. }
  240. char *
  241. qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
  242. {
  243. char un_str[10];
  244. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  245. ha->fw_minor_version,
  246. ha->fw_subminor_version);
  247. if (ha->fw_attributes & BIT_9) {
  248. strcat(str, "FLX");
  249. return (str);
  250. }
  251. switch (ha->fw_attributes & 0xFF) {
  252. case 0x7:
  253. strcat(str, "EF");
  254. break;
  255. case 0x17:
  256. strcat(str, "TP");
  257. break;
  258. case 0x37:
  259. strcat(str, "IP");
  260. break;
  261. case 0x77:
  262. strcat(str, "VI");
  263. break;
  264. default:
  265. sprintf(un_str, "(%x)", ha->fw_attributes);
  266. strcat(str, un_str);
  267. break;
  268. }
  269. if (ha->fw_attributes & 0x100)
  270. strcat(str, "X");
  271. return (str);
  272. }
  273. char *
  274. qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
  275. {
  276. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  277. ha->fw_minor_version,
  278. ha->fw_subminor_version);
  279. if (ha->fw_attributes & BIT_0)
  280. strcat(str, "[Class 2] ");
  281. if (ha->fw_attributes & BIT_1)
  282. strcat(str, "[IP] ");
  283. if (ha->fw_attributes & BIT_2)
  284. strcat(str, "[Multi-ID] ");
  285. if (ha->fw_attributes & BIT_13)
  286. strcat(str, "[Experimental]");
  287. return str;
  288. }
  289. static inline srb_t *
  290. qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
  291. struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  292. {
  293. srb_t *sp;
  294. sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  295. if (!sp)
  296. return sp;
  297. sp->ha = ha;
  298. sp->fcport = fcport;
  299. sp->cmd = cmd;
  300. sp->flags = 0;
  301. CMD_SP(cmd) = (void *)sp;
  302. cmd->scsi_done = done;
  303. return sp;
  304. }
  305. static int
  306. qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  307. {
  308. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  309. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  310. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  311. srb_t *sp;
  312. int rval;
  313. rval = fc_remote_port_chkready(rport);
  314. if (rval) {
  315. cmd->result = rval;
  316. goto qc_fail_command;
  317. }
  318. /* Close window on fcport/rport state-transitioning. */
  319. if (!*(fc_port_t **)rport->dd_data) {
  320. cmd->result = DID_IMM_RETRY << 16;
  321. goto qc_fail_command;
  322. }
  323. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  324. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  325. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  326. cmd->result = DID_NO_CONNECT << 16;
  327. goto qc_fail_command;
  328. }
  329. goto qc_host_busy;
  330. }
  331. spin_unlock_irq(ha->host->host_lock);
  332. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  333. if (!sp)
  334. goto qc_host_busy_lock;
  335. rval = qla2x00_start_scsi(sp);
  336. if (rval != QLA_SUCCESS)
  337. goto qc_host_busy_free_sp;
  338. spin_lock_irq(ha->host->host_lock);
  339. return 0;
  340. qc_host_busy_free_sp:
  341. qla2x00_sp_free_dma(ha, sp);
  342. mempool_free(sp, ha->srb_mempool);
  343. qc_host_busy_lock:
  344. spin_lock_irq(ha->host->host_lock);
  345. qc_host_busy:
  346. return SCSI_MLQUEUE_HOST_BUSY;
  347. qc_fail_command:
  348. done(cmd);
  349. return 0;
  350. }
  351. static int
  352. qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  353. {
  354. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  355. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  356. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  357. srb_t *sp;
  358. int rval;
  359. rval = fc_remote_port_chkready(rport);
  360. if (rval) {
  361. cmd->result = rval;
  362. goto qc24_fail_command;
  363. }
  364. /* Close window on fcport/rport state-transitioning. */
  365. if (!*(fc_port_t **)rport->dd_data) {
  366. cmd->result = DID_IMM_RETRY << 16;
  367. goto qc24_fail_command;
  368. }
  369. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  370. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  371. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  372. cmd->result = DID_NO_CONNECT << 16;
  373. goto qc24_fail_command;
  374. }
  375. goto qc24_host_busy;
  376. }
  377. spin_unlock_irq(ha->host->host_lock);
  378. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  379. if (!sp)
  380. goto qc24_host_busy_lock;
  381. rval = qla24xx_start_scsi(sp);
  382. if (rval != QLA_SUCCESS)
  383. goto qc24_host_busy_free_sp;
  384. spin_lock_irq(ha->host->host_lock);
  385. return 0;
  386. qc24_host_busy_free_sp:
  387. qla2x00_sp_free_dma(ha, sp);
  388. mempool_free(sp, ha->srb_mempool);
  389. qc24_host_busy_lock:
  390. spin_lock_irq(ha->host->host_lock);
  391. qc24_host_busy:
  392. return SCSI_MLQUEUE_HOST_BUSY;
  393. qc24_fail_command:
  394. done(cmd);
  395. return 0;
  396. }
  397. /*
  398. * qla2x00_eh_wait_on_command
  399. * Waits for the command to be returned by the Firmware for some
  400. * max time.
  401. *
  402. * Input:
  403. * ha = actual ha whose done queue will contain the command
  404. * returned by firmware.
  405. * cmd = Scsi Command to wait on.
  406. * flag = Abort/Reset(Bus or Device Reset)
  407. *
  408. * Return:
  409. * Not Found : 0
  410. * Found : 1
  411. */
  412. static int
  413. qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
  414. {
  415. #define ABORT_POLLING_PERIOD 1000
  416. #define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
  417. unsigned long wait_iter = ABORT_WAIT_ITER;
  418. int ret = QLA_SUCCESS;
  419. while (CMD_SP(cmd)) {
  420. msleep(ABORT_POLLING_PERIOD);
  421. if (--wait_iter)
  422. break;
  423. }
  424. if (CMD_SP(cmd))
  425. ret = QLA_FUNCTION_FAILED;
  426. return ret;
  427. }
  428. /*
  429. * qla2x00_wait_for_hba_online
  430. * Wait till the HBA is online after going through
  431. * <= MAX_RETRIES_OF_ISP_ABORT or
  432. * finally HBA is disabled ie marked offline
  433. *
  434. * Input:
  435. * ha - pointer to host adapter structure
  436. *
  437. * Note:
  438. * Does context switching-Release SPIN_LOCK
  439. * (if any) before calling this routine.
  440. *
  441. * Return:
  442. * Success (Adapter is online) : 0
  443. * Failed (Adapter is offline/disabled) : 1
  444. */
  445. int
  446. qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
  447. {
  448. int return_status;
  449. unsigned long wait_online;
  450. wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  451. while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
  452. test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
  453. test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
  454. ha->dpc_active) && time_before(jiffies, wait_online)) {
  455. msleep(1000);
  456. }
  457. if (ha->flags.online)
  458. return_status = QLA_SUCCESS;
  459. else
  460. return_status = QLA_FUNCTION_FAILED;
  461. DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
  462. return (return_status);
  463. }
  464. /*
  465. * qla2x00_wait_for_loop_ready
  466. * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
  467. * to be in LOOP_READY state.
  468. * Input:
  469. * ha - pointer to host adapter structure
  470. *
  471. * Note:
  472. * Does context switching-Release SPIN_LOCK
  473. * (if any) before calling this routine.
  474. *
  475. *
  476. * Return:
  477. * Success (LOOP_READY) : 0
  478. * Failed (LOOP_NOT_READY) : 1
  479. */
  480. static inline int
  481. qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
  482. {
  483. int return_status = QLA_SUCCESS;
  484. unsigned long loop_timeout ;
  485. /* wait for 5 min at the max for loop to be ready */
  486. loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  487. while ((!atomic_read(&ha->loop_down_timer) &&
  488. atomic_read(&ha->loop_state) == LOOP_DOWN) ||
  489. atomic_read(&ha->loop_state) != LOOP_READY) {
  490. if (atomic_read(&ha->loop_state) == LOOP_DEAD) {
  491. return_status = QLA_FUNCTION_FAILED;
  492. break;
  493. }
  494. msleep(1000);
  495. if (time_after_eq(jiffies, loop_timeout)) {
  496. return_status = QLA_FUNCTION_FAILED;
  497. break;
  498. }
  499. }
  500. return (return_status);
  501. }
  502. static void
  503. qla2x00_block_error_handler(struct scsi_cmnd *cmnd)
  504. {
  505. struct Scsi_Host *shost = cmnd->device->host;
  506. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  507. unsigned long flags;
  508. spin_lock_irqsave(shost->host_lock, flags);
  509. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  510. spin_unlock_irqrestore(shost->host_lock, flags);
  511. msleep(1000);
  512. spin_lock_irqsave(shost->host_lock, flags);
  513. }
  514. spin_unlock_irqrestore(shost->host_lock, flags);
  515. return;
  516. }
  517. /**************************************************************************
  518. * qla2xxx_eh_abort
  519. *
  520. * Description:
  521. * The abort function will abort the specified command.
  522. *
  523. * Input:
  524. * cmd = Linux SCSI command packet to be aborted.
  525. *
  526. * Returns:
  527. * Either SUCCESS or FAILED.
  528. *
  529. * Note:
  530. * Only return FAILED if command not returned by firmware.
  531. **************************************************************************/
  532. int
  533. qla2xxx_eh_abort(struct scsi_cmnd *cmd)
  534. {
  535. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  536. srb_t *sp;
  537. int ret, i;
  538. unsigned int id, lun;
  539. unsigned long serial;
  540. unsigned long flags;
  541. int wait = 0;
  542. qla2x00_block_error_handler(cmd);
  543. if (!CMD_SP(cmd))
  544. return SUCCESS;
  545. ret = SUCCESS;
  546. id = cmd->device->id;
  547. lun = cmd->device->lun;
  548. serial = cmd->serial_number;
  549. /* Check active list for command command. */
  550. spin_lock_irqsave(&ha->hardware_lock, flags);
  551. for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
  552. sp = ha->outstanding_cmds[i];
  553. if (sp == NULL)
  554. continue;
  555. if (sp->cmd != cmd)
  556. continue;
  557. DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld.\n",
  558. __func__, ha->host_no, sp, serial));
  559. DEBUG3(qla2x00_print_scsi_cmd(cmd));
  560. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  561. if (ha->isp_ops.abort_command(ha, sp)) {
  562. DEBUG2(printk("%s(%ld): abort_command "
  563. "mbx failed.\n", __func__, ha->host_no));
  564. } else {
  565. DEBUG3(printk("%s(%ld): abort_command "
  566. "mbx success.\n", __func__, ha->host_no));
  567. wait = 1;
  568. }
  569. spin_lock_irqsave(&ha->hardware_lock, flags);
  570. break;
  571. }
  572. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  573. /* Wait for the command to be returned. */
  574. if (wait) {
  575. if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
  576. qla_printk(KERN_ERR, ha,
  577. "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
  578. "%x.\n", ha->host_no, id, lun, serial, ret);
  579. ret = FAILED;
  580. }
  581. }
  582. qla_printk(KERN_INFO, ha,
  583. "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
  584. ha->host_no, id, lun, wait, serial, ret);
  585. return ret;
  586. }
  587. /**************************************************************************
  588. * qla2x00_eh_wait_for_pending_target_commands
  589. *
  590. * Description:
  591. * Waits for all the commands to come back from the specified target.
  592. *
  593. * Input:
  594. * ha - pointer to scsi_qla_host structure.
  595. * t - target
  596. * Returns:
  597. * Either SUCCESS or FAILED.
  598. *
  599. * Note:
  600. **************************************************************************/
  601. static int
  602. qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
  603. {
  604. int cnt;
  605. int status;
  606. srb_t *sp;
  607. struct scsi_cmnd *cmd;
  608. unsigned long flags;
  609. status = 0;
  610. /*
  611. * Waiting for all commands for the designated target in the active
  612. * array
  613. */
  614. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  615. spin_lock_irqsave(&ha->hardware_lock, flags);
  616. sp = ha->outstanding_cmds[cnt];
  617. if (sp) {
  618. cmd = sp->cmd;
  619. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  620. if (cmd->device->id == t) {
  621. if (!qla2x00_eh_wait_on_command(ha, cmd)) {
  622. status = 1;
  623. break;
  624. }
  625. }
  626. } else {
  627. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  628. }
  629. }
  630. return (status);
  631. }
  632. /**************************************************************************
  633. * qla2xxx_eh_device_reset
  634. *
  635. * Description:
  636. * The device reset function will reset the target and abort any
  637. * executing commands.
  638. *
  639. * NOTE: The use of SP is undefined within this context. Do *NOT*
  640. * attempt to use this value, even if you determine it is
  641. * non-null.
  642. *
  643. * Input:
  644. * cmd = Linux SCSI command packet of the command that cause the
  645. * bus device reset.
  646. *
  647. * Returns:
  648. * SUCCESS/FAILURE (defined as macro in scsi.h).
  649. *
  650. **************************************************************************/
  651. int
  652. qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
  653. {
  654. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  655. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  656. int ret;
  657. unsigned int id, lun;
  658. unsigned long serial;
  659. qla2x00_block_error_handler(cmd);
  660. ret = FAILED;
  661. id = cmd->device->id;
  662. lun = cmd->device->lun;
  663. serial = cmd->serial_number;
  664. if (!fcport)
  665. return ret;
  666. qla_printk(KERN_INFO, ha,
  667. "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
  668. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  669. goto eh_dev_reset_done;
  670. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  671. if (qla2x00_device_reset(ha, fcport) == 0)
  672. ret = SUCCESS;
  673. #if defined(LOGOUT_AFTER_DEVICE_RESET)
  674. if (ret == SUCCESS) {
  675. if (fcport->flags & FC_FABRIC_DEVICE) {
  676. ha->isp_ops.fabric_logout(ha, fcport->loop_id);
  677. qla2x00_mark_device_lost(ha, fcport, 0, 0);
  678. }
  679. }
  680. #endif
  681. } else {
  682. DEBUG2(printk(KERN_INFO
  683. "%s failed: loop not ready\n",__func__));
  684. }
  685. if (ret == FAILED) {
  686. DEBUG3(printk("%s(%ld): device reset failed\n",
  687. __func__, ha->host_no));
  688. qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
  689. __func__);
  690. goto eh_dev_reset_done;
  691. }
  692. /* Flush outstanding commands. */
  693. if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
  694. ret = FAILED;
  695. if (ret == FAILED) {
  696. DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
  697. __func__, ha->host_no));
  698. qla_printk(KERN_INFO, ha,
  699. "%s: failed while waiting for commands\n", __func__);
  700. } else
  701. qla_printk(KERN_INFO, ha,
  702. "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
  703. id, lun);
  704. eh_dev_reset_done:
  705. return ret;
  706. }
  707. /**************************************************************************
  708. * qla2x00_eh_wait_for_pending_commands
  709. *
  710. * Description:
  711. * Waits for all the commands to come back from the specified host.
  712. *
  713. * Input:
  714. * ha - pointer to scsi_qla_host structure.
  715. *
  716. * Returns:
  717. * 1 : SUCCESS
  718. * 0 : FAILED
  719. *
  720. * Note:
  721. **************************************************************************/
  722. static int
  723. qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
  724. {
  725. int cnt;
  726. int status;
  727. srb_t *sp;
  728. struct scsi_cmnd *cmd;
  729. unsigned long flags;
  730. status = 1;
  731. /*
  732. * Waiting for all commands for the designated target in the active
  733. * array
  734. */
  735. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  736. spin_lock_irqsave(&ha->hardware_lock, flags);
  737. sp = ha->outstanding_cmds[cnt];
  738. if (sp) {
  739. cmd = sp->cmd;
  740. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  741. status = qla2x00_eh_wait_on_command(ha, cmd);
  742. if (status == 0)
  743. break;
  744. }
  745. else {
  746. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  747. }
  748. }
  749. return (status);
  750. }
  751. /**************************************************************************
  752. * qla2xxx_eh_bus_reset
  753. *
  754. * Description:
  755. * The bus reset function will reset the bus and abort any executing
  756. * commands.
  757. *
  758. * Input:
  759. * cmd = Linux SCSI command packet of the command that cause the
  760. * bus reset.
  761. *
  762. * Returns:
  763. * SUCCESS/FAILURE (defined as macro in scsi.h).
  764. *
  765. **************************************************************************/
  766. int
  767. qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
  768. {
  769. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  770. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  771. int ret;
  772. unsigned int id, lun;
  773. unsigned long serial;
  774. qla2x00_block_error_handler(cmd);
  775. ret = FAILED;
  776. id = cmd->device->id;
  777. lun = cmd->device->lun;
  778. serial = cmd->serial_number;
  779. if (!fcport)
  780. return ret;
  781. qla_printk(KERN_INFO, ha,
  782. "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
  783. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
  784. DEBUG2(printk("%s failed:board disabled\n",__func__));
  785. goto eh_bus_reset_done;
  786. }
  787. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  788. if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
  789. ret = SUCCESS;
  790. }
  791. if (ret == FAILED)
  792. goto eh_bus_reset_done;
  793. /* Flush outstanding commands. */
  794. if (!qla2x00_eh_wait_for_pending_commands(ha))
  795. ret = FAILED;
  796. eh_bus_reset_done:
  797. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  798. (ret == FAILED) ? "failed" : "succeded");
  799. return ret;
  800. }
  801. /**************************************************************************
  802. * qla2xxx_eh_host_reset
  803. *
  804. * Description:
  805. * The reset function will reset the Adapter.
  806. *
  807. * Input:
  808. * cmd = Linux SCSI command packet of the command that cause the
  809. * adapter reset.
  810. *
  811. * Returns:
  812. * Either SUCCESS or FAILED.
  813. *
  814. * Note:
  815. **************************************************************************/
  816. int
  817. qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
  818. {
  819. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  820. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  821. int ret;
  822. unsigned int id, lun;
  823. unsigned long serial;
  824. qla2x00_block_error_handler(cmd);
  825. ret = FAILED;
  826. id = cmd->device->id;
  827. lun = cmd->device->lun;
  828. serial = cmd->serial_number;
  829. if (!fcport)
  830. return ret;
  831. qla_printk(KERN_INFO, ha,
  832. "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
  833. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  834. goto eh_host_reset_lock;
  835. /*
  836. * Fixme-may be dpc thread is active and processing
  837. * loop_resync,so wait a while for it to
  838. * be completed and then issue big hammer.Otherwise
  839. * it may cause I/O failure as big hammer marks the
  840. * devices as lost kicking of the port_down_timer
  841. * while dpc is stuck for the mailbox to complete.
  842. */
  843. qla2x00_wait_for_loop_ready(ha);
  844. set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  845. if (qla2x00_abort_isp(ha)) {
  846. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  847. /* failed. schedule dpc to try */
  848. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  849. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  850. goto eh_host_reset_lock;
  851. }
  852. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  853. /* Waiting for our command in done_queue to be returned to OS.*/
  854. if (qla2x00_eh_wait_for_pending_commands(ha))
  855. ret = SUCCESS;
  856. eh_host_reset_lock:
  857. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  858. (ret == FAILED) ? "failed" : "succeded");
  859. return ret;
  860. }
  861. /*
  862. * qla2x00_loop_reset
  863. * Issue loop reset.
  864. *
  865. * Input:
  866. * ha = adapter block pointer.
  867. *
  868. * Returns:
  869. * 0 = success
  870. */
  871. static int
  872. qla2x00_loop_reset(scsi_qla_host_t *ha)
  873. {
  874. int status = QLA_SUCCESS;
  875. struct fc_port *fcport;
  876. if (ha->flags.enable_lip_reset) {
  877. status = qla2x00_lip_reset(ha);
  878. }
  879. if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
  880. list_for_each_entry(fcport, &ha->fcports, list) {
  881. if (fcport->port_type != FCT_TARGET)
  882. continue;
  883. status = qla2x00_device_reset(ha, fcport);
  884. if (status != QLA_SUCCESS)
  885. break;
  886. }
  887. }
  888. if (status == QLA_SUCCESS &&
  889. ((!ha->flags.enable_target_reset &&
  890. !ha->flags.enable_lip_reset) ||
  891. ha->flags.enable_lip_full_login)) {
  892. status = qla2x00_full_login_lip(ha);
  893. }
  894. /* Issue marker command only when we are going to start the I/O */
  895. ha->marker_needed = 1;
  896. if (status) {
  897. /* Empty */
  898. DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
  899. __func__,
  900. ha->host_no));
  901. } else {
  902. /* Empty */
  903. DEBUG3(printk("%s(%ld): exiting normally.\n",
  904. __func__,
  905. ha->host_no));
  906. }
  907. return(status);
  908. }
  909. /*
  910. * qla2x00_device_reset
  911. * Issue bus device reset message to the target.
  912. *
  913. * Input:
  914. * ha = adapter block pointer.
  915. * t = SCSI ID.
  916. * TARGET_QUEUE_LOCK must be released.
  917. * ADAPTER_STATE_LOCK must be released.
  918. *
  919. * Context:
  920. * Kernel context.
  921. */
  922. static int
  923. qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
  924. {
  925. /* Abort Target command will clear Reservation */
  926. return ha->isp_ops.abort_target(reset_fcport);
  927. }
  928. static int
  929. qla2xxx_slave_alloc(struct scsi_device *sdev)
  930. {
  931. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  932. if (!rport || fc_remote_port_chkready(rport))
  933. return -ENXIO;
  934. sdev->hostdata = *(fc_port_t **)rport->dd_data;
  935. return 0;
  936. }
  937. static int
  938. qla2xxx_slave_configure(struct scsi_device *sdev)
  939. {
  940. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  941. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  942. if (sdev->tagged_supported)
  943. scsi_activate_tcq(sdev, ha->max_q_depth);
  944. else
  945. scsi_deactivate_tcq(sdev, ha->max_q_depth);
  946. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  947. return 0;
  948. }
  949. static void
  950. qla2xxx_slave_destroy(struct scsi_device *sdev)
  951. {
  952. sdev->hostdata = NULL;
  953. }
  954. static int
  955. qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
  956. {
  957. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  958. return sdev->queue_depth;
  959. }
  960. static int
  961. qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
  962. {
  963. if (sdev->tagged_supported) {
  964. scsi_set_tag_type(sdev, tag_type);
  965. if (tag_type)
  966. scsi_activate_tcq(sdev, sdev->queue_depth);
  967. else
  968. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  969. } else
  970. tag_type = 0;
  971. return tag_type;
  972. }
  973. /**
  974. * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
  975. * @ha: HA context
  976. *
  977. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  978. * supported addressing method.
  979. */
  980. static void
  981. qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
  982. {
  983. /* Assume a 32bit DMA mask. */
  984. ha->flags.enable_64bit_addressing = 0;
  985. if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
  986. /* Any upper-dword bits set? */
  987. if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
  988. !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
  989. /* Ok, a 64bit DMA mask is applicable. */
  990. ha->flags.enable_64bit_addressing = 1;
  991. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
  992. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
  993. return;
  994. }
  995. }
  996. dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
  997. pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
  998. }
  999. static inline void
  1000. qla2x00_set_isp_flags(scsi_qla_host_t *ha)
  1001. {
  1002. ha->device_type = DT_EXTENDED_IDS;
  1003. switch (ha->pdev->device) {
  1004. case PCI_DEVICE_ID_QLOGIC_ISP2100:
  1005. ha->device_type |= DT_ISP2100;
  1006. ha->device_type &= ~DT_EXTENDED_IDS;
  1007. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1008. break;
  1009. case PCI_DEVICE_ID_QLOGIC_ISP2200:
  1010. ha->device_type |= DT_ISP2200;
  1011. ha->device_type &= ~DT_EXTENDED_IDS;
  1012. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1013. break;
  1014. case PCI_DEVICE_ID_QLOGIC_ISP2300:
  1015. ha->device_type |= DT_ISP2300;
  1016. ha->device_type |= DT_ZIO_SUPPORTED;
  1017. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1018. break;
  1019. case PCI_DEVICE_ID_QLOGIC_ISP2312:
  1020. ha->device_type |= DT_ISP2312;
  1021. ha->device_type |= DT_ZIO_SUPPORTED;
  1022. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1023. break;
  1024. case PCI_DEVICE_ID_QLOGIC_ISP2322:
  1025. ha->device_type |= DT_ISP2322;
  1026. ha->device_type |= DT_ZIO_SUPPORTED;
  1027. if (ha->pdev->subsystem_vendor == 0x1028 &&
  1028. ha->pdev->subsystem_device == 0x0170)
  1029. ha->device_type |= DT_OEM_001;
  1030. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1031. break;
  1032. case PCI_DEVICE_ID_QLOGIC_ISP6312:
  1033. ha->device_type |= DT_ISP6312;
  1034. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1035. break;
  1036. case PCI_DEVICE_ID_QLOGIC_ISP6322:
  1037. ha->device_type |= DT_ISP6322;
  1038. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1039. break;
  1040. case PCI_DEVICE_ID_QLOGIC_ISP2422:
  1041. ha->device_type |= DT_ISP2422;
  1042. ha->device_type |= DT_ZIO_SUPPORTED;
  1043. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1044. break;
  1045. case PCI_DEVICE_ID_QLOGIC_ISP2432:
  1046. ha->device_type |= DT_ISP2432;
  1047. ha->device_type |= DT_ZIO_SUPPORTED;
  1048. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1049. break;
  1050. case PCI_DEVICE_ID_QLOGIC_ISP5422:
  1051. ha->device_type |= DT_ISP5422;
  1052. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1053. break;
  1054. case PCI_DEVICE_ID_QLOGIC_ISP5432:
  1055. ha->device_type |= DT_ISP5432;
  1056. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1057. break;
  1058. }
  1059. }
  1060. static int
  1061. qla2x00_iospace_config(scsi_qla_host_t *ha)
  1062. {
  1063. unsigned long pio, pio_len, pio_flags;
  1064. unsigned long mmio, mmio_len, mmio_flags;
  1065. /* We only need PIO for Flash operations on ISP2312 v2 chips. */
  1066. pio = pci_resource_start(ha->pdev, 0);
  1067. pio_len = pci_resource_len(ha->pdev, 0);
  1068. pio_flags = pci_resource_flags(ha->pdev, 0);
  1069. if (pio_flags & IORESOURCE_IO) {
  1070. if (pio_len < MIN_IOBASE_LEN) {
  1071. qla_printk(KERN_WARNING, ha,
  1072. "Invalid PCI I/O region size (%s)...\n",
  1073. pci_name(ha->pdev));
  1074. pio = 0;
  1075. }
  1076. } else {
  1077. qla_printk(KERN_WARNING, ha,
  1078. "region #0 not a PIO resource (%s)...\n",
  1079. pci_name(ha->pdev));
  1080. pio = 0;
  1081. }
  1082. /* Use MMIO operations for all accesses. */
  1083. mmio = pci_resource_start(ha->pdev, 1);
  1084. mmio_len = pci_resource_len(ha->pdev, 1);
  1085. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1086. if (!(mmio_flags & IORESOURCE_MEM)) {
  1087. qla_printk(KERN_ERR, ha,
  1088. "region #0 not an MMIO resource (%s), aborting\n",
  1089. pci_name(ha->pdev));
  1090. goto iospace_error_exit;
  1091. }
  1092. if (mmio_len < MIN_IOBASE_LEN) {
  1093. qla_printk(KERN_ERR, ha,
  1094. "Invalid PCI mem region size (%s), aborting\n",
  1095. pci_name(ha->pdev));
  1096. goto iospace_error_exit;
  1097. }
  1098. if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
  1099. qla_printk(KERN_WARNING, ha,
  1100. "Failed to reserve PIO/MMIO regions (%s)\n",
  1101. pci_name(ha->pdev));
  1102. goto iospace_error_exit;
  1103. }
  1104. ha->pio_address = pio;
  1105. ha->pio_length = pio_len;
  1106. ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
  1107. if (!ha->iobase) {
  1108. qla_printk(KERN_ERR, ha,
  1109. "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
  1110. goto iospace_error_exit;
  1111. }
  1112. return (0);
  1113. iospace_error_exit:
  1114. return (-ENOMEM);
  1115. }
  1116. static void
  1117. qla2x00_enable_intrs(scsi_qla_host_t *ha)
  1118. {
  1119. unsigned long flags = 0;
  1120. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1121. spin_lock_irqsave(&ha->hardware_lock, flags);
  1122. ha->interrupts_on = 1;
  1123. /* enable risc and host interrupts */
  1124. WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
  1125. RD_REG_WORD(&reg->ictrl);
  1126. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1127. }
  1128. static void
  1129. qla2x00_disable_intrs(scsi_qla_host_t *ha)
  1130. {
  1131. unsigned long flags = 0;
  1132. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1133. spin_lock_irqsave(&ha->hardware_lock, flags);
  1134. ha->interrupts_on = 0;
  1135. /* disable risc and host interrupts */
  1136. WRT_REG_WORD(&reg->ictrl, 0);
  1137. RD_REG_WORD(&reg->ictrl);
  1138. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1139. }
  1140. static void
  1141. qla24xx_enable_intrs(scsi_qla_host_t *ha)
  1142. {
  1143. unsigned long flags = 0;
  1144. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1145. spin_lock_irqsave(&ha->hardware_lock, flags);
  1146. ha->interrupts_on = 1;
  1147. WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
  1148. RD_REG_DWORD(&reg->ictrl);
  1149. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1150. }
  1151. static void
  1152. qla24xx_disable_intrs(scsi_qla_host_t *ha)
  1153. {
  1154. unsigned long flags = 0;
  1155. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1156. spin_lock_irqsave(&ha->hardware_lock, flags);
  1157. ha->interrupts_on = 0;
  1158. WRT_REG_DWORD(&reg->ictrl, 0);
  1159. RD_REG_DWORD(&reg->ictrl);
  1160. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1161. }
  1162. /*
  1163. * PCI driver interface
  1164. */
  1165. static int __devinit
  1166. qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1167. {
  1168. int ret = -ENODEV;
  1169. device_reg_t __iomem *reg;
  1170. struct Scsi_Host *host;
  1171. scsi_qla_host_t *ha;
  1172. unsigned long flags = 0;
  1173. unsigned long wait_switch = 0;
  1174. char pci_info[20];
  1175. char fw_str[30];
  1176. fc_port_t *fcport;
  1177. struct scsi_host_template *sht;
  1178. if (pci_enable_device(pdev))
  1179. goto probe_out;
  1180. sht = &qla2x00_driver_template;
  1181. if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
  1182. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432)
  1183. sht = &qla24xx_driver_template;
  1184. host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
  1185. if (host == NULL) {
  1186. printk(KERN_WARNING
  1187. "qla2xxx: Couldn't allocate host from scsi layer!\n");
  1188. goto probe_disable_device;
  1189. }
  1190. /* Clear our data area */
  1191. ha = (scsi_qla_host_t *)host->hostdata;
  1192. memset(ha, 0, sizeof(scsi_qla_host_t));
  1193. ha->pdev = pdev;
  1194. ha->host = host;
  1195. ha->host_no = host->host_no;
  1196. sprintf(ha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, ha->host_no);
  1197. /* Set ISP-type information. */
  1198. qla2x00_set_isp_flags(ha);
  1199. /* Configure PCI I/O space */
  1200. ret = qla2x00_iospace_config(ha);
  1201. if (ret)
  1202. goto probe_failed;
  1203. qla_printk(KERN_INFO, ha,
  1204. "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
  1205. ha->iobase);
  1206. spin_lock_init(&ha->hardware_lock);
  1207. ha->prev_topology = 0;
  1208. ha->init_cb_size = sizeof(init_cb_t);
  1209. ha->mgmt_svr_loop_id = MANAGEMENT_SERVER;
  1210. ha->link_data_rate = PORT_SPEED_UNKNOWN;
  1211. ha->optrom_size = OPTROM_SIZE_2300;
  1212. ha->max_q_depth = MAX_Q_DEPTH;
  1213. if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
  1214. ha->max_q_depth = ql2xmaxqdepth;
  1215. /* Assign ISP specific operations. */
  1216. ha->isp_ops.pci_config = qla2100_pci_config;
  1217. ha->isp_ops.reset_chip = qla2x00_reset_chip;
  1218. ha->isp_ops.chip_diag = qla2x00_chip_diag;
  1219. ha->isp_ops.config_rings = qla2x00_config_rings;
  1220. ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
  1221. ha->isp_ops.nvram_config = qla2x00_nvram_config;
  1222. ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
  1223. ha->isp_ops.load_risc = qla2x00_load_risc;
  1224. ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
  1225. ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
  1226. ha->isp_ops.intr_handler = qla2100_intr_handler;
  1227. ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
  1228. ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
  1229. ha->isp_ops.abort_command = qla2x00_abort_command;
  1230. ha->isp_ops.abort_target = qla2x00_abort_target;
  1231. ha->isp_ops.fabric_login = qla2x00_login_fabric;
  1232. ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
  1233. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
  1234. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
  1235. ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
  1236. ha->isp_ops.prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb;
  1237. ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
  1238. ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
  1239. ha->isp_ops.fw_dump = qla2100_fw_dump;
  1240. ha->isp_ops.read_optrom = qla2x00_read_optrom_data;
  1241. ha->isp_ops.write_optrom = qla2x00_write_optrom_data;
  1242. if (IS_QLA2100(ha)) {
  1243. host->max_id = MAX_TARGETS_2100;
  1244. ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
  1245. ha->request_q_length = REQUEST_ENTRY_CNT_2100;
  1246. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1247. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1248. host->sg_tablesize = 32;
  1249. ha->gid_list_info_size = 4;
  1250. } else if (IS_QLA2200(ha)) {
  1251. host->max_id = MAX_TARGETS_2200;
  1252. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1253. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1254. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1255. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1256. ha->gid_list_info_size = 4;
  1257. } else if (IS_QLA23XX(ha)) {
  1258. host->max_id = MAX_TARGETS_2200;
  1259. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1260. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1261. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1262. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1263. ha->isp_ops.pci_config = qla2300_pci_config;
  1264. ha->isp_ops.intr_handler = qla2300_intr_handler;
  1265. ha->isp_ops.fw_dump = qla2300_fw_dump;
  1266. ha->isp_ops.beacon_on = qla2x00_beacon_on;
  1267. ha->isp_ops.beacon_off = qla2x00_beacon_off;
  1268. ha->isp_ops.beacon_blink = qla2x00_beacon_blink;
  1269. ha->gid_list_info_size = 6;
  1270. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  1271. ha->optrom_size = OPTROM_SIZE_2322;
  1272. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1273. host->max_id = MAX_TARGETS_2200;
  1274. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1275. ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
  1276. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1277. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1278. ha->init_cb_size = sizeof(struct init_cb_24xx);
  1279. ha->mgmt_svr_loop_id = 10;
  1280. ha->isp_ops.pci_config = qla24xx_pci_config;
  1281. ha->isp_ops.reset_chip = qla24xx_reset_chip;
  1282. ha->isp_ops.chip_diag = qla24xx_chip_diag;
  1283. ha->isp_ops.config_rings = qla24xx_config_rings;
  1284. ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
  1285. ha->isp_ops.nvram_config = qla24xx_nvram_config;
  1286. ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
  1287. ha->isp_ops.load_risc = qla24xx_load_risc;
  1288. ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
  1289. ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
  1290. ha->isp_ops.intr_handler = qla24xx_intr_handler;
  1291. ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
  1292. ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
  1293. ha->isp_ops.abort_command = qla24xx_abort_command;
  1294. ha->isp_ops.abort_target = qla24xx_abort_target;
  1295. ha->isp_ops.fabric_login = qla24xx_login_fabric;
  1296. ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
  1297. ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
  1298. ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
  1299. ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
  1300. ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
  1301. ha->isp_ops.fw_dump = qla24xx_fw_dump;
  1302. ha->isp_ops.read_optrom = qla24xx_read_optrom_data;
  1303. ha->isp_ops.write_optrom = qla24xx_write_optrom_data;
  1304. ha->isp_ops.beacon_on = qla24xx_beacon_on;
  1305. ha->isp_ops.beacon_off = qla24xx_beacon_off;
  1306. ha->isp_ops.beacon_blink = qla24xx_beacon_blink;
  1307. ha->gid_list_info_size = 8;
  1308. ha->optrom_size = OPTROM_SIZE_24XX;
  1309. }
  1310. host->can_queue = ha->request_q_length + 128;
  1311. /* load the F/W, read paramaters, and init the H/W */
  1312. ha->instance = num_hosts;
  1313. init_MUTEX(&ha->mbx_cmd_sem);
  1314. init_MUTEX_LOCKED(&ha->mbx_intr_sem);
  1315. INIT_LIST_HEAD(&ha->list);
  1316. INIT_LIST_HEAD(&ha->fcports);
  1317. /*
  1318. * These locks are used to prevent more than one CPU
  1319. * from modifying the queue at the same time. The
  1320. * higher level "host_lock" will reduce most
  1321. * contention for these locks.
  1322. */
  1323. spin_lock_init(&ha->mbx_reg_lock);
  1324. qla2x00_config_dma_addressing(ha);
  1325. if (qla2x00_mem_alloc(ha)) {
  1326. qla_printk(KERN_WARNING, ha,
  1327. "[ERROR] Failed to allocate memory for adapter\n");
  1328. ret = -ENOMEM;
  1329. goto probe_failed;
  1330. }
  1331. if (qla2x00_initialize_adapter(ha) &&
  1332. !(ha->device_flags & DFLG_NO_CABLE)) {
  1333. qla_printk(KERN_WARNING, ha,
  1334. "Failed to initialize adapter\n");
  1335. DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
  1336. "Adapter flags %x.\n",
  1337. ha->host_no, ha->device_flags));
  1338. ret = -ENODEV;
  1339. goto probe_failed;
  1340. }
  1341. /*
  1342. * Startup the kernel thread for this host adapter
  1343. */
  1344. ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
  1345. "%s_dpc", ha->host_str);
  1346. if (IS_ERR(ha->dpc_thread)) {
  1347. qla_printk(KERN_WARNING, ha,
  1348. "Unable to start DPC thread!\n");
  1349. ret = PTR_ERR(ha->dpc_thread);
  1350. goto probe_failed;
  1351. }
  1352. host->this_id = 255;
  1353. host->cmd_per_lun = 3;
  1354. host->unique_id = ha->instance;
  1355. host->max_cmd_len = MAX_CMDSZ;
  1356. host->max_channel = MAX_BUSES - 1;
  1357. host->max_lun = MAX_LUNS;
  1358. host->transportt = qla2xxx_transport_template;
  1359. ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
  1360. IRQF_DISABLED|IRQF_SHARED, QLA2XXX_DRIVER_NAME, ha);
  1361. if (ret) {
  1362. qla_printk(KERN_WARNING, ha,
  1363. "Failed to reserve interrupt %d already in use.\n",
  1364. pdev->irq);
  1365. goto probe_failed;
  1366. }
  1367. host->irq = pdev->irq;
  1368. /* Initialized the timer */
  1369. qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
  1370. DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
  1371. ha->host_no, ha));
  1372. ha->isp_ops.disable_intrs(ha);
  1373. spin_lock_irqsave(&ha->hardware_lock, flags);
  1374. reg = ha->iobase;
  1375. if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1376. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
  1377. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
  1378. } else {
  1379. WRT_REG_WORD(&reg->isp.semaphore, 0);
  1380. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
  1381. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
  1382. /* Enable proper parity */
  1383. if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  1384. if (IS_QLA2300(ha))
  1385. /* SRAM parity */
  1386. WRT_REG_WORD(&reg->isp.hccr,
  1387. (HCCR_ENABLE_PARITY + 0x1));
  1388. else
  1389. /* SRAM, Instruction RAM and GP RAM parity */
  1390. WRT_REG_WORD(&reg->isp.hccr,
  1391. (HCCR_ENABLE_PARITY + 0x7));
  1392. }
  1393. }
  1394. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1395. ha->isp_ops.enable_intrs(ha);
  1396. /* v2.19.5b6 */
  1397. /*
  1398. * Wait around max loop_reset_delay secs for the devices to come
  1399. * on-line. We don't want Linux scanning before we are ready.
  1400. *
  1401. */
  1402. for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
  1403. time_before(jiffies,wait_switch) &&
  1404. !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
  1405. && (ha->device_flags & SWITCH_FOUND) ;) {
  1406. qla2x00_check_fabric_devices(ha);
  1407. msleep(10);
  1408. }
  1409. pci_set_drvdata(pdev, ha);
  1410. ha->flags.init_done = 1;
  1411. num_hosts++;
  1412. ret = scsi_add_host(host, &pdev->dev);
  1413. if (ret)
  1414. goto probe_failed;
  1415. qla2x00_alloc_sysfs_attr(ha);
  1416. qla2x00_init_host_attr(ha);
  1417. qla_printk(KERN_INFO, ha, "\n"
  1418. " QLogic Fibre Channel HBA Driver: %s\n"
  1419. " QLogic %s - %s\n"
  1420. " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
  1421. qla2x00_version_str, ha->model_number,
  1422. ha->model_desc ? ha->model_desc: "", pdev->device,
  1423. ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
  1424. ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
  1425. ha->isp_ops.fw_version_str(ha, fw_str));
  1426. /* Go with fc_rport registration. */
  1427. list_for_each_entry(fcport, &ha->fcports, list)
  1428. qla2x00_reg_remote_port(ha, fcport);
  1429. return 0;
  1430. probe_failed:
  1431. qla2x00_free_device(ha);
  1432. scsi_host_put(host);
  1433. probe_disable_device:
  1434. pci_disable_device(pdev);
  1435. probe_out:
  1436. return ret;
  1437. }
  1438. static void __devexit
  1439. qla2x00_remove_one(struct pci_dev *pdev)
  1440. {
  1441. scsi_qla_host_t *ha;
  1442. ha = pci_get_drvdata(pdev);
  1443. qla2x00_free_sysfs_attr(ha);
  1444. fc_remove_host(ha->host);
  1445. scsi_remove_host(ha->host);
  1446. qla2x00_free_device(ha);
  1447. scsi_host_put(ha->host);
  1448. pci_set_drvdata(pdev, NULL);
  1449. }
  1450. static void
  1451. qla2x00_free_device(scsi_qla_host_t *ha)
  1452. {
  1453. /* Disable timer */
  1454. if (ha->timer_active)
  1455. qla2x00_stop_timer(ha);
  1456. /* Kill the kernel thread for this host */
  1457. if (ha->dpc_thread) {
  1458. struct task_struct *t = ha->dpc_thread;
  1459. /*
  1460. * qla2xxx_wake_dpc checks for ->dpc_thread
  1461. * so we need to zero it out.
  1462. */
  1463. ha->dpc_thread = NULL;
  1464. kthread_stop(t);
  1465. }
  1466. if (ha->eft)
  1467. qla2x00_trace_control(ha, TC_DISABLE, 0, 0);
  1468. ha->flags.online = 0;
  1469. /* Stop currently executing firmware. */
  1470. qla2x00_try_to_stop_firmware(ha);
  1471. /* turn-off interrupts on the card */
  1472. if (ha->interrupts_on)
  1473. ha->isp_ops.disable_intrs(ha);
  1474. qla2x00_mem_free(ha);
  1475. /* Detach interrupts */
  1476. if (ha->host->irq)
  1477. free_irq(ha->host->irq, ha);
  1478. /* release io space registers */
  1479. if (ha->iobase)
  1480. iounmap(ha->iobase);
  1481. pci_release_regions(ha->pdev);
  1482. pci_disable_device(ha->pdev);
  1483. }
  1484. static inline void
  1485. qla2x00_schedule_rport_del(struct scsi_qla_host *ha, fc_port_t *fcport,
  1486. int defer)
  1487. {
  1488. unsigned long flags;
  1489. struct fc_rport *rport;
  1490. if (!fcport->rport)
  1491. return;
  1492. rport = fcport->rport;
  1493. if (defer) {
  1494. spin_lock_irqsave(&fcport->rport_lock, flags);
  1495. fcport->drport = rport;
  1496. fcport->rport = NULL;
  1497. *(fc_port_t **)rport->dd_data = NULL;
  1498. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1499. set_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags);
  1500. } else {
  1501. spin_lock_irqsave(&fcport->rport_lock, flags);
  1502. fcport->rport = NULL;
  1503. *(fc_port_t **)rport->dd_data = NULL;
  1504. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1505. fc_remote_port_delete(rport);
  1506. }
  1507. }
  1508. /*
  1509. * qla2x00_mark_device_lost Updates fcport state when device goes offline.
  1510. *
  1511. * Input: ha = adapter block pointer. fcport = port structure pointer.
  1512. *
  1513. * Return: None.
  1514. *
  1515. * Context:
  1516. */
  1517. void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
  1518. int do_login, int defer)
  1519. {
  1520. if (atomic_read(&fcport->state) == FCS_ONLINE)
  1521. qla2x00_schedule_rport_del(ha, fcport, defer);
  1522. /*
  1523. * We may need to retry the login, so don't change the state of the
  1524. * port but do the retries.
  1525. */
  1526. if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
  1527. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1528. if (!do_login)
  1529. return;
  1530. if (fcport->login_retry == 0) {
  1531. fcport->login_retry = ha->login_retry_count;
  1532. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1533. DEBUG(printk("scsi(%ld): Port login retry: "
  1534. "%02x%02x%02x%02x%02x%02x%02x%02x, "
  1535. "id = 0x%04x retry cnt=%d\n",
  1536. ha->host_no,
  1537. fcport->port_name[0],
  1538. fcport->port_name[1],
  1539. fcport->port_name[2],
  1540. fcport->port_name[3],
  1541. fcport->port_name[4],
  1542. fcport->port_name[5],
  1543. fcport->port_name[6],
  1544. fcport->port_name[7],
  1545. fcport->loop_id,
  1546. fcport->login_retry));
  1547. }
  1548. }
  1549. /*
  1550. * qla2x00_mark_all_devices_lost
  1551. * Updates fcport state when device goes offline.
  1552. *
  1553. * Input:
  1554. * ha = adapter block pointer.
  1555. * fcport = port structure pointer.
  1556. *
  1557. * Return:
  1558. * None.
  1559. *
  1560. * Context:
  1561. */
  1562. void
  1563. qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha, int defer)
  1564. {
  1565. fc_port_t *fcport;
  1566. list_for_each_entry(fcport, &ha->fcports, list) {
  1567. if (fcport->port_type != FCT_TARGET)
  1568. continue;
  1569. /*
  1570. * No point in marking the device as lost, if the device is
  1571. * already DEAD.
  1572. */
  1573. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
  1574. continue;
  1575. if (atomic_read(&fcport->state) == FCS_ONLINE)
  1576. qla2x00_schedule_rport_del(ha, fcport, defer);
  1577. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1578. }
  1579. if (defer)
  1580. qla2xxx_wake_dpc(ha);
  1581. }
  1582. /*
  1583. * qla2x00_mem_alloc
  1584. * Allocates adapter memory.
  1585. *
  1586. * Returns:
  1587. * 0 = success.
  1588. * 1 = failure.
  1589. */
  1590. static uint8_t
  1591. qla2x00_mem_alloc(scsi_qla_host_t *ha)
  1592. {
  1593. char name[16];
  1594. uint8_t status = 1;
  1595. int retry= 10;
  1596. do {
  1597. /*
  1598. * This will loop only once if everything goes well, else some
  1599. * number of retries will be performed to get around a kernel
  1600. * bug where available mem is not allocated until after a
  1601. * little delay and a retry.
  1602. */
  1603. ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
  1604. (ha->request_q_length + 1) * sizeof(request_t),
  1605. &ha->request_dma, GFP_KERNEL);
  1606. if (ha->request_ring == NULL) {
  1607. qla_printk(KERN_WARNING, ha,
  1608. "Memory Allocation failed - request_ring\n");
  1609. qla2x00_mem_free(ha);
  1610. msleep(100);
  1611. continue;
  1612. }
  1613. ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
  1614. (ha->response_q_length + 1) * sizeof(response_t),
  1615. &ha->response_dma, GFP_KERNEL);
  1616. if (ha->response_ring == NULL) {
  1617. qla_printk(KERN_WARNING, ha,
  1618. "Memory Allocation failed - response_ring\n");
  1619. qla2x00_mem_free(ha);
  1620. msleep(100);
  1621. continue;
  1622. }
  1623. ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
  1624. &ha->gid_list_dma, GFP_KERNEL);
  1625. if (ha->gid_list == NULL) {
  1626. qla_printk(KERN_WARNING, ha,
  1627. "Memory Allocation failed - gid_list\n");
  1628. qla2x00_mem_free(ha);
  1629. msleep(100);
  1630. continue;
  1631. }
  1632. snprintf(name, sizeof(name), "%s_%ld", QLA2XXX_DRIVER_NAME,
  1633. ha->host_no);
  1634. ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
  1635. DMA_POOL_SIZE, 8, 0);
  1636. if (ha->s_dma_pool == NULL) {
  1637. qla_printk(KERN_WARNING, ha,
  1638. "Memory Allocation failed - s_dma_pool\n");
  1639. qla2x00_mem_free(ha);
  1640. msleep(100);
  1641. continue;
  1642. }
  1643. /* get consistent memory allocated for init control block */
  1644. ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1645. &ha->init_cb_dma);
  1646. if (ha->init_cb == NULL) {
  1647. qla_printk(KERN_WARNING, ha,
  1648. "Memory Allocation failed - init_cb\n");
  1649. qla2x00_mem_free(ha);
  1650. msleep(100);
  1651. continue;
  1652. }
  1653. memset(ha->init_cb, 0, ha->init_cb_size);
  1654. if (qla2x00_allocate_sp_pool(ha)) {
  1655. qla_printk(KERN_WARNING, ha,
  1656. "Memory Allocation failed - "
  1657. "qla2x00_allocate_sp_pool()\n");
  1658. qla2x00_mem_free(ha);
  1659. msleep(100);
  1660. continue;
  1661. }
  1662. /* Allocate memory for SNS commands */
  1663. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1664. /* Get consistent memory allocated for SNS commands */
  1665. ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
  1666. sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
  1667. GFP_KERNEL);
  1668. if (ha->sns_cmd == NULL) {
  1669. /* error */
  1670. qla_printk(KERN_WARNING, ha,
  1671. "Memory Allocation failed - sns_cmd\n");
  1672. qla2x00_mem_free(ha);
  1673. msleep(100);
  1674. continue;
  1675. }
  1676. memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
  1677. } else {
  1678. /* Get consistent memory allocated for MS IOCB */
  1679. ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1680. &ha->ms_iocb_dma);
  1681. if (ha->ms_iocb == NULL) {
  1682. /* error */
  1683. qla_printk(KERN_WARNING, ha,
  1684. "Memory Allocation failed - ms_iocb\n");
  1685. qla2x00_mem_free(ha);
  1686. msleep(100);
  1687. continue;
  1688. }
  1689. memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
  1690. /*
  1691. * Get consistent memory allocated for CT SNS
  1692. * commands
  1693. */
  1694. ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
  1695. sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
  1696. GFP_KERNEL);
  1697. if (ha->ct_sns == NULL) {
  1698. /* error */
  1699. qla_printk(KERN_WARNING, ha,
  1700. "Memory Allocation failed - ct_sns\n");
  1701. qla2x00_mem_free(ha);
  1702. msleep(100);
  1703. continue;
  1704. }
  1705. memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
  1706. if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1707. /*
  1708. * Get consistent memory allocated for SFP
  1709. * block.
  1710. */
  1711. ha->sfp_data = dma_pool_alloc(ha->s_dma_pool,
  1712. GFP_KERNEL, &ha->sfp_data_dma);
  1713. if (ha->sfp_data == NULL) {
  1714. qla_printk(KERN_WARNING, ha,
  1715. "Memory Allocation failed - "
  1716. "sfp_data\n");
  1717. qla2x00_mem_free(ha);
  1718. msleep(100);
  1719. continue;
  1720. }
  1721. memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
  1722. }
  1723. }
  1724. /* Done all allocations without any error. */
  1725. status = 0;
  1726. } while (retry-- && status != 0);
  1727. if (status) {
  1728. printk(KERN_WARNING
  1729. "%s(): **** FAILED ****\n", __func__);
  1730. }
  1731. return(status);
  1732. }
  1733. /*
  1734. * qla2x00_mem_free
  1735. * Frees all adapter allocated memory.
  1736. *
  1737. * Input:
  1738. * ha = adapter block pointer.
  1739. */
  1740. static void
  1741. qla2x00_mem_free(scsi_qla_host_t *ha)
  1742. {
  1743. struct list_head *fcpl, *fcptemp;
  1744. fc_port_t *fcport;
  1745. if (ha == NULL) {
  1746. /* error */
  1747. DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
  1748. return;
  1749. }
  1750. /* free sp pool */
  1751. qla2x00_free_sp_pool(ha);
  1752. if (ha->fw_dump) {
  1753. if (ha->eft)
  1754. dma_free_coherent(&ha->pdev->dev,
  1755. ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
  1756. vfree(ha->fw_dump);
  1757. }
  1758. if (ha->sns_cmd)
  1759. dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
  1760. ha->sns_cmd, ha->sns_cmd_dma);
  1761. if (ha->ct_sns)
  1762. dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
  1763. ha->ct_sns, ha->ct_sns_dma);
  1764. if (ha->sfp_data)
  1765. dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
  1766. if (ha->ms_iocb)
  1767. dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
  1768. if (ha->init_cb)
  1769. dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
  1770. if (ha->s_dma_pool)
  1771. dma_pool_destroy(ha->s_dma_pool);
  1772. if (ha->gid_list)
  1773. dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
  1774. ha->gid_list_dma);
  1775. if (ha->response_ring)
  1776. dma_free_coherent(&ha->pdev->dev,
  1777. (ha->response_q_length + 1) * sizeof(response_t),
  1778. ha->response_ring, ha->response_dma);
  1779. if (ha->request_ring)
  1780. dma_free_coherent(&ha->pdev->dev,
  1781. (ha->request_q_length + 1) * sizeof(request_t),
  1782. ha->request_ring, ha->request_dma);
  1783. ha->eft = NULL;
  1784. ha->eft_dma = 0;
  1785. ha->sns_cmd = NULL;
  1786. ha->sns_cmd_dma = 0;
  1787. ha->ct_sns = NULL;
  1788. ha->ct_sns_dma = 0;
  1789. ha->ms_iocb = NULL;
  1790. ha->ms_iocb_dma = 0;
  1791. ha->init_cb = NULL;
  1792. ha->init_cb_dma = 0;
  1793. ha->s_dma_pool = NULL;
  1794. ha->gid_list = NULL;
  1795. ha->gid_list_dma = 0;
  1796. ha->response_ring = NULL;
  1797. ha->response_dma = 0;
  1798. ha->request_ring = NULL;
  1799. ha->request_dma = 0;
  1800. list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
  1801. fcport = list_entry(fcpl, fc_port_t, list);
  1802. /* fc ports */
  1803. list_del_init(&fcport->list);
  1804. kfree(fcport);
  1805. }
  1806. INIT_LIST_HEAD(&ha->fcports);
  1807. ha->fw_dump = NULL;
  1808. ha->fw_dumped = 0;
  1809. ha->fw_dump_reading = 0;
  1810. vfree(ha->optrom_buffer);
  1811. }
  1812. /*
  1813. * qla2x00_allocate_sp_pool
  1814. * This routine is called during initialization to allocate
  1815. * memory for local srb_t.
  1816. *
  1817. * Input:
  1818. * ha = adapter block pointer.
  1819. *
  1820. * Context:
  1821. * Kernel context.
  1822. */
  1823. static int
  1824. qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
  1825. {
  1826. int rval;
  1827. rval = QLA_SUCCESS;
  1828. ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
  1829. if (ha->srb_mempool == NULL) {
  1830. qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
  1831. rval = QLA_FUNCTION_FAILED;
  1832. }
  1833. return (rval);
  1834. }
  1835. /*
  1836. * This routine frees all adapter allocated memory.
  1837. *
  1838. */
  1839. static void
  1840. qla2x00_free_sp_pool( scsi_qla_host_t *ha)
  1841. {
  1842. if (ha->srb_mempool) {
  1843. mempool_destroy(ha->srb_mempool);
  1844. ha->srb_mempool = NULL;
  1845. }
  1846. }
  1847. /**************************************************************************
  1848. * qla2x00_do_dpc
  1849. * This kernel thread is a task that is schedule by the interrupt handler
  1850. * to perform the background processing for interrupts.
  1851. *
  1852. * Notes:
  1853. * This task always run in the context of a kernel thread. It
  1854. * is kick-off by the driver's detect code and starts up
  1855. * up one per adapter. It immediately goes to sleep and waits for
  1856. * some fibre event. When either the interrupt handler or
  1857. * the timer routine detects a event it will one of the task
  1858. * bits then wake us up.
  1859. **************************************************************************/
  1860. static int
  1861. qla2x00_do_dpc(void *data)
  1862. {
  1863. scsi_qla_host_t *ha;
  1864. fc_port_t *fcport;
  1865. uint8_t status;
  1866. uint16_t next_loopid;
  1867. ha = (scsi_qla_host_t *)data;
  1868. set_user_nice(current, -20);
  1869. while (!kthread_should_stop()) {
  1870. DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
  1871. set_current_state(TASK_INTERRUPTIBLE);
  1872. schedule();
  1873. __set_current_state(TASK_RUNNING);
  1874. DEBUG3(printk("qla2x00: DPC handler waking up\n"));
  1875. /* Initialization not yet finished. Don't do anything yet. */
  1876. if (!ha->flags.init_done)
  1877. continue;
  1878. DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
  1879. ha->dpc_active = 1;
  1880. if (ha->flags.mbox_busy) {
  1881. ha->dpc_active = 0;
  1882. continue;
  1883. }
  1884. if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  1885. DEBUG(printk("scsi(%ld): dpc: sched "
  1886. "qla2x00_abort_isp ha = %p\n",
  1887. ha->host_no, ha));
  1888. if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
  1889. &ha->dpc_flags))) {
  1890. if (qla2x00_abort_isp(ha)) {
  1891. /* failed. retry later */
  1892. set_bit(ISP_ABORT_NEEDED,
  1893. &ha->dpc_flags);
  1894. }
  1895. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  1896. }
  1897. DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
  1898. ha->host_no));
  1899. }
  1900. if (test_and_clear_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags))
  1901. qla2x00_update_fcports(ha);
  1902. if (test_and_clear_bit(LOOP_RESET_NEEDED, &ha->dpc_flags)) {
  1903. DEBUG(printk("scsi(%ld): dpc: sched loop_reset()\n",
  1904. ha->host_no));
  1905. qla2x00_loop_reset(ha);
  1906. }
  1907. if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
  1908. (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
  1909. DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
  1910. ha->host_no));
  1911. qla2x00_rst_aen(ha);
  1912. clear_bit(RESET_ACTIVE, &ha->dpc_flags);
  1913. }
  1914. /* Retry each device up to login retry count */
  1915. if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  1916. !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
  1917. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1918. DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
  1919. ha->host_no));
  1920. next_loopid = 0;
  1921. list_for_each_entry(fcport, &ha->fcports, list) {
  1922. /*
  1923. * If the port is not ONLINE then try to login
  1924. * to it if we haven't run out of retries.
  1925. */
  1926. if (atomic_read(&fcport->state) != FCS_ONLINE &&
  1927. fcport->login_retry) {
  1928. fcport->login_retry--;
  1929. if (fcport->flags & FCF_FABRIC_DEVICE) {
  1930. if (fcport->flags &
  1931. FCF_TAPE_PRESENT)
  1932. ha->isp_ops.fabric_logout(
  1933. ha, fcport->loop_id,
  1934. fcport->d_id.b.domain,
  1935. fcport->d_id.b.area,
  1936. fcport->d_id.b.al_pa);
  1937. status = qla2x00_fabric_login(
  1938. ha, fcport, &next_loopid);
  1939. } else
  1940. status =
  1941. qla2x00_local_device_login(
  1942. ha, fcport);
  1943. if (status == QLA_SUCCESS) {
  1944. fcport->old_loop_id = fcport->loop_id;
  1945. DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
  1946. ha->host_no, fcport->loop_id));
  1947. qla2x00_update_fcport(ha,
  1948. fcport);
  1949. } else if (status == 1) {
  1950. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1951. /* retry the login again */
  1952. DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
  1953. ha->host_no,
  1954. fcport->login_retry, fcport->loop_id));
  1955. } else {
  1956. fcport->login_retry = 0;
  1957. }
  1958. }
  1959. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1960. break;
  1961. }
  1962. DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
  1963. ha->host_no));
  1964. }
  1965. if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
  1966. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1967. clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
  1968. DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
  1969. ha->host_no));
  1970. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1971. DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
  1972. ha->host_no));
  1973. }
  1974. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1975. DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
  1976. ha->host_no));
  1977. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
  1978. &ha->dpc_flags))) {
  1979. qla2x00_loop_resync(ha);
  1980. clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
  1981. }
  1982. DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
  1983. ha->host_no));
  1984. }
  1985. if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
  1986. DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
  1987. ha->host_no));
  1988. qla2x00_rescan_fcports(ha);
  1989. DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
  1990. "end.\n",
  1991. ha->host_no));
  1992. }
  1993. if (!ha->interrupts_on)
  1994. ha->isp_ops.enable_intrs(ha);
  1995. if (test_and_clear_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags))
  1996. ha->isp_ops.beacon_blink(ha);
  1997. ha->dpc_active = 0;
  1998. } /* End of while(1) */
  1999. DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
  2000. /*
  2001. * Make sure that nobody tries to wake us up again.
  2002. */
  2003. ha->dpc_active = 0;
  2004. return 0;
  2005. }
  2006. void
  2007. qla2xxx_wake_dpc(scsi_qla_host_t *ha)
  2008. {
  2009. if (ha->dpc_thread)
  2010. wake_up_process(ha->dpc_thread);
  2011. }
  2012. /*
  2013. * qla2x00_rst_aen
  2014. * Processes asynchronous reset.
  2015. *
  2016. * Input:
  2017. * ha = adapter block pointer.
  2018. */
  2019. static void
  2020. qla2x00_rst_aen(scsi_qla_host_t *ha)
  2021. {
  2022. if (ha->flags.online && !ha->flags.reset_active &&
  2023. !atomic_read(&ha->loop_down_timer) &&
  2024. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
  2025. do {
  2026. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2027. /*
  2028. * Issue marker command only when we are going to start
  2029. * the I/O.
  2030. */
  2031. ha->marker_needed = 1;
  2032. } while (!atomic_read(&ha->loop_down_timer) &&
  2033. (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
  2034. }
  2035. }
  2036. static void
  2037. qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
  2038. {
  2039. struct scsi_cmnd *cmd = sp->cmd;
  2040. if (sp->flags & SRB_DMA_VALID) {
  2041. if (cmd->use_sg) {
  2042. dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
  2043. cmd->use_sg, cmd->sc_data_direction);
  2044. } else if (cmd->request_bufflen) {
  2045. dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
  2046. cmd->request_bufflen, cmd->sc_data_direction);
  2047. }
  2048. sp->flags &= ~SRB_DMA_VALID;
  2049. }
  2050. CMD_SP(cmd) = NULL;
  2051. }
  2052. void
  2053. qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
  2054. {
  2055. struct scsi_cmnd *cmd = sp->cmd;
  2056. qla2x00_sp_free_dma(ha, sp);
  2057. mempool_free(sp, ha->srb_mempool);
  2058. cmd->scsi_done(cmd);
  2059. }
  2060. /**************************************************************************
  2061. * qla2x00_timer
  2062. *
  2063. * Description:
  2064. * One second timer
  2065. *
  2066. * Context: Interrupt
  2067. ***************************************************************************/
  2068. static void
  2069. qla2x00_timer(scsi_qla_host_t *ha)
  2070. {
  2071. unsigned long cpu_flags = 0;
  2072. fc_port_t *fcport;
  2073. int start_dpc = 0;
  2074. int index;
  2075. srb_t *sp;
  2076. int t;
  2077. /*
  2078. * Ports - Port down timer.
  2079. *
  2080. * Whenever, a port is in the LOST state we start decrementing its port
  2081. * down timer every second until it reaches zero. Once it reaches zero
  2082. * the port it marked DEAD.
  2083. */
  2084. t = 0;
  2085. list_for_each_entry(fcport, &ha->fcports, list) {
  2086. if (fcport->port_type != FCT_TARGET)
  2087. continue;
  2088. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  2089. if (atomic_read(&fcport->port_down_timer) == 0)
  2090. continue;
  2091. if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
  2092. atomic_set(&fcport->state, FCS_DEVICE_DEAD);
  2093. DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
  2094. "%d remaining\n",
  2095. ha->host_no,
  2096. t, atomic_read(&fcport->port_down_timer)));
  2097. }
  2098. t++;
  2099. } /* End of for fcport */
  2100. /* Loop down handler. */
  2101. if (atomic_read(&ha->loop_down_timer) > 0 &&
  2102. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
  2103. if (atomic_read(&ha->loop_down_timer) ==
  2104. ha->loop_down_abort_time) {
  2105. DEBUG(printk("scsi(%ld): Loop Down - aborting the "
  2106. "queues before time expire\n",
  2107. ha->host_no));
  2108. if (!IS_QLA2100(ha) && ha->link_down_timeout)
  2109. atomic_set(&ha->loop_state, LOOP_DEAD);
  2110. /* Schedule an ISP abort to return any tape commands. */
  2111. spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
  2112. for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
  2113. index++) {
  2114. fc_port_t *sfcp;
  2115. sp = ha->outstanding_cmds[index];
  2116. if (!sp)
  2117. continue;
  2118. sfcp = sp->fcport;
  2119. if (!(sfcp->flags & FCF_TAPE_PRESENT))
  2120. continue;
  2121. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2122. break;
  2123. }
  2124. spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
  2125. set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
  2126. start_dpc++;
  2127. }
  2128. /* if the loop has been down for 4 minutes, reinit adapter */
  2129. if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
  2130. DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
  2131. "restarting queues.\n",
  2132. ha->host_no));
  2133. set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
  2134. start_dpc++;
  2135. if (!(ha->device_flags & DFLG_NO_CABLE)) {
  2136. DEBUG(printk("scsi(%ld): Loop down - "
  2137. "aborting ISP.\n",
  2138. ha->host_no));
  2139. qla_printk(KERN_WARNING, ha,
  2140. "Loop down - aborting ISP.\n");
  2141. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2142. }
  2143. }
  2144. DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
  2145. ha->host_no,
  2146. atomic_read(&ha->loop_down_timer)));
  2147. }
  2148. /* Check if beacon LED needs to be blinked */
  2149. if (ha->beacon_blink_led == 1) {
  2150. set_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags);
  2151. start_dpc++;
  2152. }
  2153. /* Schedule the DPC routine if needed */
  2154. if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
  2155. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
  2156. test_bit(LOOP_RESET_NEEDED, &ha->dpc_flags) ||
  2157. test_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags) ||
  2158. start_dpc ||
  2159. test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
  2160. test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
  2161. test_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags) ||
  2162. test_bit(RELOGIN_NEEDED, &ha->dpc_flags)))
  2163. qla2xxx_wake_dpc(ha);
  2164. qla2x00_restart_timer(ha, WATCH_INTERVAL);
  2165. }
  2166. /* XXX(hch): crude hack to emulate a down_timeout() */
  2167. int
  2168. qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
  2169. {
  2170. const unsigned int step = 100; /* msecs */
  2171. unsigned int iterations = jiffies_to_msecs(timeout)/100;
  2172. do {
  2173. if (!down_trylock(sema))
  2174. return 0;
  2175. if (msleep_interruptible(step))
  2176. break;
  2177. } while (--iterations >= 0);
  2178. return -ETIMEDOUT;
  2179. }
  2180. /* Firmware interface routines. */
  2181. #define FW_BLOBS 5
  2182. #define FW_ISP21XX 0
  2183. #define FW_ISP22XX 1
  2184. #define FW_ISP2300 2
  2185. #define FW_ISP2322 3
  2186. #define FW_ISP24XX 4
  2187. #define FW_FILE_ISP21XX "ql2100_fw.bin"
  2188. #define FW_FILE_ISP22XX "ql2200_fw.bin"
  2189. #define FW_FILE_ISP2300 "ql2300_fw.bin"
  2190. #define FW_FILE_ISP2322 "ql2322_fw.bin"
  2191. #define FW_FILE_ISP24XX "ql2400_fw.bin"
  2192. static DECLARE_MUTEX(qla_fw_lock);
  2193. static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
  2194. { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
  2195. { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
  2196. { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
  2197. { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
  2198. { .name = FW_FILE_ISP24XX, },
  2199. };
  2200. struct fw_blob *
  2201. qla2x00_request_firmware(scsi_qla_host_t *ha)
  2202. {
  2203. struct fw_blob *blob;
  2204. blob = NULL;
  2205. if (IS_QLA2100(ha)) {
  2206. blob = &qla_fw_blobs[FW_ISP21XX];
  2207. } else if (IS_QLA2200(ha)) {
  2208. blob = &qla_fw_blobs[FW_ISP22XX];
  2209. } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  2210. blob = &qla_fw_blobs[FW_ISP2300];
  2211. } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  2212. blob = &qla_fw_blobs[FW_ISP2322];
  2213. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  2214. blob = &qla_fw_blobs[FW_ISP24XX];
  2215. }
  2216. down(&qla_fw_lock);
  2217. if (blob->fw)
  2218. goto out;
  2219. if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
  2220. DEBUG2(printk("scsi(%ld): Failed to load firmware image "
  2221. "(%s).\n", ha->host_no, blob->name));
  2222. blob->fw = NULL;
  2223. blob = NULL;
  2224. goto out;
  2225. }
  2226. out:
  2227. up(&qla_fw_lock);
  2228. return blob;
  2229. }
  2230. static void
  2231. qla2x00_release_firmware(void)
  2232. {
  2233. int idx;
  2234. down(&qla_fw_lock);
  2235. for (idx = 0; idx < FW_BLOBS; idx++)
  2236. if (qla_fw_blobs[idx].fw)
  2237. release_firmware(qla_fw_blobs[idx].fw);
  2238. up(&qla_fw_lock);
  2239. }
  2240. static struct pci_device_id qla2xxx_pci_tbl[] = {
  2241. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
  2242. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
  2243. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
  2244. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
  2245. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
  2246. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
  2247. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
  2248. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
  2249. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
  2250. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
  2251. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
  2252. { 0 },
  2253. };
  2254. MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
  2255. static struct pci_driver qla2xxx_pci_driver = {
  2256. .name = QLA2XXX_DRIVER_NAME,
  2257. .driver = {
  2258. .owner = THIS_MODULE,
  2259. },
  2260. .id_table = qla2xxx_pci_tbl,
  2261. .probe = qla2x00_probe_one,
  2262. .remove = __devexit_p(qla2x00_remove_one),
  2263. };
  2264. /**
  2265. * qla2x00_module_init - Module initialization.
  2266. **/
  2267. static int __init
  2268. qla2x00_module_init(void)
  2269. {
  2270. int ret = 0;
  2271. /* Allocate cache for SRBs. */
  2272. srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
  2273. SLAB_HWCACHE_ALIGN, NULL, NULL);
  2274. if (srb_cachep == NULL) {
  2275. printk(KERN_ERR
  2276. "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
  2277. return -ENOMEM;
  2278. }
  2279. /* Derive version string. */
  2280. strcpy(qla2x00_version_str, QLA2XXX_VERSION);
  2281. if (ql2xextended_error_logging)
  2282. strcat(qla2x00_version_str, "-debug");
  2283. qla2xxx_transport_template =
  2284. fc_attach_transport(&qla2xxx_transport_functions);
  2285. if (!qla2xxx_transport_template)
  2286. return -ENODEV;
  2287. printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
  2288. ret = pci_register_driver(&qla2xxx_pci_driver);
  2289. if (ret) {
  2290. kmem_cache_destroy(srb_cachep);
  2291. fc_release_transport(qla2xxx_transport_template);
  2292. }
  2293. return ret;
  2294. }
  2295. /**
  2296. * qla2x00_module_exit - Module cleanup.
  2297. **/
  2298. static void __exit
  2299. qla2x00_module_exit(void)
  2300. {
  2301. pci_unregister_driver(&qla2xxx_pci_driver);
  2302. qla2x00_release_firmware();
  2303. kmem_cache_destroy(srb_cachep);
  2304. fc_release_transport(qla2xxx_transport_template);
  2305. }
  2306. module_init(qla2x00_module_init);
  2307. module_exit(qla2x00_module_exit);
  2308. MODULE_AUTHOR("QLogic Corporation");
  2309. MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
  2310. MODULE_LICENSE("GPL");
  2311. MODULE_VERSION(QLA2XXX_VERSION);
  2312. MODULE_FIRMWARE(FW_FILE_ISP21XX);
  2313. MODULE_FIRMWARE(FW_FILE_ISP22XX);
  2314. MODULE_FIRMWARE(FW_FILE_ISP2300);
  2315. MODULE_FIRMWARE(FW_FILE_ISP2322);
  2316. MODULE_FIRMWARE(FW_FILE_ISP24XX);