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 struct kmem_cache *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_IWUSR);
  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 int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
  82. static void qla2xxx_scan_start(struct Scsi_Host *);
  83. static void qla2xxx_slave_destroy(struct scsi_device *);
  84. static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
  85. void (*fn)(struct scsi_cmnd *));
  86. static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
  87. void (*fn)(struct scsi_cmnd *));
  88. static int qla2xxx_eh_abort(struct scsi_cmnd *);
  89. static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
  90. static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
  91. static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
  92. static int qla2x00_loop_reset(scsi_qla_host_t *ha);
  93. static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
  94. static int qla2x00_change_queue_depth(struct scsi_device *, int);
  95. static int qla2x00_change_queue_type(struct scsi_device *, int);
  96. static struct scsi_host_template qla2x00_driver_template = {
  97. .module = THIS_MODULE,
  98. .name = QLA2XXX_DRIVER_NAME,
  99. .queuecommand = qla2x00_queuecommand,
  100. .eh_abort_handler = qla2xxx_eh_abort,
  101. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  102. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  103. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  104. .slave_configure = qla2xxx_slave_configure,
  105. .slave_alloc = qla2xxx_slave_alloc,
  106. .slave_destroy = qla2xxx_slave_destroy,
  107. .scan_finished = qla2xxx_scan_finished,
  108. .scan_start = qla2xxx_scan_start,
  109. .change_queue_depth = qla2x00_change_queue_depth,
  110. .change_queue_type = qla2x00_change_queue_type,
  111. .this_id = -1,
  112. .cmd_per_lun = 3,
  113. .use_clustering = ENABLE_CLUSTERING,
  114. .sg_tablesize = SG_ALL,
  115. /*
  116. * The RISC allows for each command to transfer (2^32-1) bytes of data,
  117. * which equates to 0x800000 sectors.
  118. */
  119. .max_sectors = 0xFFFF,
  120. .shost_attrs = qla2x00_host_attrs,
  121. };
  122. static struct scsi_host_template qla24xx_driver_template = {
  123. .module = THIS_MODULE,
  124. .name = QLA2XXX_DRIVER_NAME,
  125. .queuecommand = qla24xx_queuecommand,
  126. .eh_abort_handler = qla2xxx_eh_abort,
  127. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  128. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  129. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  130. .slave_configure = qla2xxx_slave_configure,
  131. .slave_alloc = qla2xxx_slave_alloc,
  132. .slave_destroy = qla2xxx_slave_destroy,
  133. .scan_finished = qla2xxx_scan_finished,
  134. .scan_start = qla2xxx_scan_start,
  135. .change_queue_depth = qla2x00_change_queue_depth,
  136. .change_queue_type = qla2x00_change_queue_type,
  137. .this_id = -1,
  138. .cmd_per_lun = 3,
  139. .use_clustering = ENABLE_CLUSTERING,
  140. .sg_tablesize = SG_ALL,
  141. .max_sectors = 0xFFFF,
  142. .shost_attrs = qla2x00_host_attrs,
  143. };
  144. static struct scsi_transport_template *qla2xxx_transport_template = NULL;
  145. /* TODO Convert to inlines
  146. *
  147. * Timer routines
  148. */
  149. #define WATCH_INTERVAL 1 /* number of seconds */
  150. static void qla2x00_timer(scsi_qla_host_t *);
  151. static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
  152. void *, unsigned long);
  153. static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
  154. static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
  155. static inline void
  156. qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
  157. {
  158. init_timer(&ha->timer);
  159. ha->timer.expires = jiffies + interval * HZ;
  160. ha->timer.data = (unsigned long)ha;
  161. ha->timer.function = (void (*)(unsigned long))func;
  162. add_timer(&ha->timer);
  163. ha->timer_active = 1;
  164. }
  165. static inline void
  166. qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
  167. {
  168. mod_timer(&ha->timer, jiffies + interval * HZ);
  169. }
  170. static __inline__ void
  171. qla2x00_stop_timer(scsi_qla_host_t *ha)
  172. {
  173. del_timer_sync(&ha->timer);
  174. ha->timer_active = 0;
  175. }
  176. static int qla2x00_do_dpc(void *data);
  177. static void qla2x00_rst_aen(scsi_qla_host_t *);
  178. static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
  179. static void qla2x00_mem_free(scsi_qla_host_t *ha);
  180. static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
  181. static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
  182. static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
  183. void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
  184. /* -------------------------------------------------------------------------- */
  185. static char *
  186. qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
  187. {
  188. static char *pci_bus_modes[] = {
  189. "33", "66", "100", "133",
  190. };
  191. uint16_t pci_bus;
  192. strcpy(str, "PCI");
  193. pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
  194. if (pci_bus) {
  195. strcat(str, "-X (");
  196. strcat(str, pci_bus_modes[pci_bus]);
  197. } else {
  198. pci_bus = (ha->pci_attr & BIT_8) >> 8;
  199. strcat(str, " (");
  200. strcat(str, pci_bus_modes[pci_bus]);
  201. }
  202. strcat(str, " MHz)");
  203. return (str);
  204. }
  205. static char *
  206. qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
  207. {
  208. static char *pci_bus_modes[] = { "33", "66", "100", "133", };
  209. uint32_t pci_bus;
  210. int pcie_reg;
  211. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  212. if (pcie_reg) {
  213. char lwstr[6];
  214. uint16_t pcie_lstat, lspeed, lwidth;
  215. pcie_reg += 0x12;
  216. pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
  217. lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
  218. lwidth = (pcie_lstat &
  219. (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
  220. strcpy(str, "PCIe (");
  221. if (lspeed == 1)
  222. strcat(str, "2.5Gb/s ");
  223. else
  224. strcat(str, "<unknown> ");
  225. snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
  226. strcat(str, lwstr);
  227. return str;
  228. }
  229. strcpy(str, "PCI");
  230. pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
  231. if (pci_bus == 0 || pci_bus == 8) {
  232. strcat(str, " (");
  233. strcat(str, pci_bus_modes[pci_bus >> 3]);
  234. } else {
  235. strcat(str, "-X ");
  236. if (pci_bus & BIT_2)
  237. strcat(str, "Mode 2");
  238. else
  239. strcat(str, "Mode 1");
  240. strcat(str, " (");
  241. strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
  242. }
  243. strcat(str, " MHz)");
  244. return str;
  245. }
  246. static char *
  247. qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
  248. {
  249. char un_str[10];
  250. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  251. ha->fw_minor_version,
  252. ha->fw_subminor_version);
  253. if (ha->fw_attributes & BIT_9) {
  254. strcat(str, "FLX");
  255. return (str);
  256. }
  257. switch (ha->fw_attributes & 0xFF) {
  258. case 0x7:
  259. strcat(str, "EF");
  260. break;
  261. case 0x17:
  262. strcat(str, "TP");
  263. break;
  264. case 0x37:
  265. strcat(str, "IP");
  266. break;
  267. case 0x77:
  268. strcat(str, "VI");
  269. break;
  270. default:
  271. sprintf(un_str, "(%x)", ha->fw_attributes);
  272. strcat(str, un_str);
  273. break;
  274. }
  275. if (ha->fw_attributes & 0x100)
  276. strcat(str, "X");
  277. return (str);
  278. }
  279. static char *
  280. qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
  281. {
  282. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  283. ha->fw_minor_version,
  284. ha->fw_subminor_version);
  285. if (ha->fw_attributes & BIT_0)
  286. strcat(str, "[Class 2] ");
  287. if (ha->fw_attributes & BIT_1)
  288. strcat(str, "[IP] ");
  289. if (ha->fw_attributes & BIT_2)
  290. strcat(str, "[Multi-ID] ");
  291. if (ha->fw_attributes & BIT_13)
  292. strcat(str, "[Experimental]");
  293. return str;
  294. }
  295. static inline srb_t *
  296. qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
  297. struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  298. {
  299. srb_t *sp;
  300. sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  301. if (!sp)
  302. return sp;
  303. sp->ha = ha;
  304. sp->fcport = fcport;
  305. sp->cmd = cmd;
  306. sp->flags = 0;
  307. CMD_SP(cmd) = (void *)sp;
  308. cmd->scsi_done = done;
  309. return sp;
  310. }
  311. static int
  312. qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  313. {
  314. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  315. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  316. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  317. srb_t *sp;
  318. int rval;
  319. rval = fc_remote_port_chkready(rport);
  320. if (rval) {
  321. cmd->result = rval;
  322. goto qc_fail_command;
  323. }
  324. /* Close window on fcport/rport state-transitioning. */
  325. if (!*(fc_port_t **)rport->dd_data) {
  326. cmd->result = DID_IMM_RETRY << 16;
  327. goto qc_fail_command;
  328. }
  329. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  330. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  331. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  332. cmd->result = DID_NO_CONNECT << 16;
  333. goto qc_fail_command;
  334. }
  335. goto qc_host_busy;
  336. }
  337. spin_unlock_irq(ha->host->host_lock);
  338. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  339. if (!sp)
  340. goto qc_host_busy_lock;
  341. rval = qla2x00_start_scsi(sp);
  342. if (rval != QLA_SUCCESS)
  343. goto qc_host_busy_free_sp;
  344. spin_lock_irq(ha->host->host_lock);
  345. return 0;
  346. qc_host_busy_free_sp:
  347. qla2x00_sp_free_dma(ha, sp);
  348. mempool_free(sp, ha->srb_mempool);
  349. qc_host_busy_lock:
  350. spin_lock_irq(ha->host->host_lock);
  351. qc_host_busy:
  352. return SCSI_MLQUEUE_HOST_BUSY;
  353. qc_fail_command:
  354. done(cmd);
  355. return 0;
  356. }
  357. static int
  358. qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  359. {
  360. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  361. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  362. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  363. srb_t *sp;
  364. int rval;
  365. rval = fc_remote_port_chkready(rport);
  366. if (rval) {
  367. cmd->result = rval;
  368. goto qc24_fail_command;
  369. }
  370. /* Close window on fcport/rport state-transitioning. */
  371. if (!*(fc_port_t **)rport->dd_data) {
  372. cmd->result = DID_IMM_RETRY << 16;
  373. goto qc24_fail_command;
  374. }
  375. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  376. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  377. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  378. cmd->result = DID_NO_CONNECT << 16;
  379. goto qc24_fail_command;
  380. }
  381. goto qc24_host_busy;
  382. }
  383. spin_unlock_irq(ha->host->host_lock);
  384. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  385. if (!sp)
  386. goto qc24_host_busy_lock;
  387. rval = qla24xx_start_scsi(sp);
  388. if (rval != QLA_SUCCESS)
  389. goto qc24_host_busy_free_sp;
  390. spin_lock_irq(ha->host->host_lock);
  391. return 0;
  392. qc24_host_busy_free_sp:
  393. qla2x00_sp_free_dma(ha, sp);
  394. mempool_free(sp, ha->srb_mempool);
  395. qc24_host_busy_lock:
  396. spin_lock_irq(ha->host->host_lock);
  397. qc24_host_busy:
  398. return SCSI_MLQUEUE_HOST_BUSY;
  399. qc24_fail_command:
  400. done(cmd);
  401. return 0;
  402. }
  403. /*
  404. * qla2x00_eh_wait_on_command
  405. * Waits for the command to be returned by the Firmware for some
  406. * max time.
  407. *
  408. * Input:
  409. * ha = actual ha whose done queue will contain the command
  410. * returned by firmware.
  411. * cmd = Scsi Command to wait on.
  412. * flag = Abort/Reset(Bus or Device Reset)
  413. *
  414. * Return:
  415. * Not Found : 0
  416. * Found : 1
  417. */
  418. static int
  419. qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
  420. {
  421. #define ABORT_POLLING_PERIOD 1000
  422. #define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
  423. unsigned long wait_iter = ABORT_WAIT_ITER;
  424. int ret = QLA_SUCCESS;
  425. while (CMD_SP(cmd)) {
  426. msleep(ABORT_POLLING_PERIOD);
  427. if (--wait_iter)
  428. break;
  429. }
  430. if (CMD_SP(cmd))
  431. ret = QLA_FUNCTION_FAILED;
  432. return ret;
  433. }
  434. /*
  435. * qla2x00_wait_for_hba_online
  436. * Wait till the HBA is online after going through
  437. * <= MAX_RETRIES_OF_ISP_ABORT or
  438. * finally HBA is disabled ie marked offline
  439. *
  440. * Input:
  441. * ha - pointer to host adapter structure
  442. *
  443. * Note:
  444. * Does context switching-Release SPIN_LOCK
  445. * (if any) before calling this routine.
  446. *
  447. * Return:
  448. * Success (Adapter is online) : 0
  449. * Failed (Adapter is offline/disabled) : 1
  450. */
  451. int
  452. qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
  453. {
  454. int return_status;
  455. unsigned long wait_online;
  456. wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  457. while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
  458. test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
  459. test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
  460. ha->dpc_active) && time_before(jiffies, wait_online)) {
  461. msleep(1000);
  462. }
  463. if (ha->flags.online)
  464. return_status = QLA_SUCCESS;
  465. else
  466. return_status = QLA_FUNCTION_FAILED;
  467. DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
  468. return (return_status);
  469. }
  470. /*
  471. * qla2x00_wait_for_loop_ready
  472. * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
  473. * to be in LOOP_READY state.
  474. * Input:
  475. * ha - pointer to host adapter structure
  476. *
  477. * Note:
  478. * Does context switching-Release SPIN_LOCK
  479. * (if any) before calling this routine.
  480. *
  481. *
  482. * Return:
  483. * Success (LOOP_READY) : 0
  484. * Failed (LOOP_NOT_READY) : 1
  485. */
  486. static inline int
  487. qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
  488. {
  489. int return_status = QLA_SUCCESS;
  490. unsigned long loop_timeout ;
  491. /* wait for 5 min at the max for loop to be ready */
  492. loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  493. while ((!atomic_read(&ha->loop_down_timer) &&
  494. atomic_read(&ha->loop_state) == LOOP_DOWN) ||
  495. atomic_read(&ha->loop_state) != LOOP_READY) {
  496. if (atomic_read(&ha->loop_state) == LOOP_DEAD) {
  497. return_status = QLA_FUNCTION_FAILED;
  498. break;
  499. }
  500. msleep(1000);
  501. if (time_after_eq(jiffies, loop_timeout)) {
  502. return_status = QLA_FUNCTION_FAILED;
  503. break;
  504. }
  505. }
  506. return (return_status);
  507. }
  508. static void
  509. qla2x00_block_error_handler(struct scsi_cmnd *cmnd)
  510. {
  511. struct Scsi_Host *shost = cmnd->device->host;
  512. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  513. unsigned long flags;
  514. spin_lock_irqsave(shost->host_lock, flags);
  515. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  516. spin_unlock_irqrestore(shost->host_lock, flags);
  517. msleep(1000);
  518. spin_lock_irqsave(shost->host_lock, flags);
  519. }
  520. spin_unlock_irqrestore(shost->host_lock, flags);
  521. return;
  522. }
  523. /**************************************************************************
  524. * qla2xxx_eh_abort
  525. *
  526. * Description:
  527. * The abort function will abort the specified command.
  528. *
  529. * Input:
  530. * cmd = Linux SCSI command packet to be aborted.
  531. *
  532. * Returns:
  533. * Either SUCCESS or FAILED.
  534. *
  535. * Note:
  536. * Only return FAILED if command not returned by firmware.
  537. **************************************************************************/
  538. static int
  539. qla2xxx_eh_abort(struct scsi_cmnd *cmd)
  540. {
  541. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  542. srb_t *sp;
  543. int ret, i;
  544. unsigned int id, lun;
  545. unsigned long serial;
  546. unsigned long flags;
  547. int wait = 0;
  548. qla2x00_block_error_handler(cmd);
  549. if (!CMD_SP(cmd))
  550. return SUCCESS;
  551. ret = SUCCESS;
  552. id = cmd->device->id;
  553. lun = cmd->device->lun;
  554. serial = cmd->serial_number;
  555. /* Check active list for command command. */
  556. spin_lock_irqsave(&ha->hardware_lock, flags);
  557. for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
  558. sp = ha->outstanding_cmds[i];
  559. if (sp == NULL)
  560. continue;
  561. if (sp->cmd != cmd)
  562. continue;
  563. DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld.\n",
  564. __func__, ha->host_no, sp, serial));
  565. DEBUG3(qla2x00_print_scsi_cmd(cmd));
  566. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  567. if (ha->isp_ops.abort_command(ha, sp)) {
  568. DEBUG2(printk("%s(%ld): abort_command "
  569. "mbx failed.\n", __func__, ha->host_no));
  570. } else {
  571. DEBUG3(printk("%s(%ld): abort_command "
  572. "mbx success.\n", __func__, ha->host_no));
  573. wait = 1;
  574. }
  575. spin_lock_irqsave(&ha->hardware_lock, flags);
  576. break;
  577. }
  578. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  579. /* Wait for the command to be returned. */
  580. if (wait) {
  581. if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
  582. qla_printk(KERN_ERR, ha,
  583. "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
  584. "%x.\n", ha->host_no, id, lun, serial, ret);
  585. ret = FAILED;
  586. }
  587. }
  588. qla_printk(KERN_INFO, ha,
  589. "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
  590. ha->host_no, id, lun, wait, serial, ret);
  591. return ret;
  592. }
  593. /**************************************************************************
  594. * qla2x00_eh_wait_for_pending_target_commands
  595. *
  596. * Description:
  597. * Waits for all the commands to come back from the specified target.
  598. *
  599. * Input:
  600. * ha - pointer to scsi_qla_host structure.
  601. * t - target
  602. * Returns:
  603. * Either SUCCESS or FAILED.
  604. *
  605. * Note:
  606. **************************************************************************/
  607. static int
  608. qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
  609. {
  610. int cnt;
  611. int status;
  612. srb_t *sp;
  613. struct scsi_cmnd *cmd;
  614. unsigned long flags;
  615. status = 0;
  616. /*
  617. * Waiting for all commands for the designated target in the active
  618. * array
  619. */
  620. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  621. spin_lock_irqsave(&ha->hardware_lock, flags);
  622. sp = ha->outstanding_cmds[cnt];
  623. if (sp) {
  624. cmd = sp->cmd;
  625. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  626. if (cmd->device->id == t) {
  627. if (!qla2x00_eh_wait_on_command(ha, cmd)) {
  628. status = 1;
  629. break;
  630. }
  631. }
  632. } else {
  633. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  634. }
  635. }
  636. return (status);
  637. }
  638. /**************************************************************************
  639. * qla2xxx_eh_device_reset
  640. *
  641. * Description:
  642. * The device reset function will reset the target and abort any
  643. * executing commands.
  644. *
  645. * NOTE: The use of SP is undefined within this context. Do *NOT*
  646. * attempt to use this value, even if you determine it is
  647. * non-null.
  648. *
  649. * Input:
  650. * cmd = Linux SCSI command packet of the command that cause the
  651. * bus device reset.
  652. *
  653. * Returns:
  654. * SUCCESS/FAILURE (defined as macro in scsi.h).
  655. *
  656. **************************************************************************/
  657. static int
  658. qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
  659. {
  660. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  661. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  662. int ret;
  663. unsigned int id, lun;
  664. unsigned long serial;
  665. qla2x00_block_error_handler(cmd);
  666. ret = FAILED;
  667. id = cmd->device->id;
  668. lun = cmd->device->lun;
  669. serial = cmd->serial_number;
  670. if (!fcport)
  671. return ret;
  672. qla_printk(KERN_INFO, ha,
  673. "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
  674. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  675. goto eh_dev_reset_done;
  676. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  677. if (qla2x00_device_reset(ha, fcport) == 0)
  678. ret = SUCCESS;
  679. #if defined(LOGOUT_AFTER_DEVICE_RESET)
  680. if (ret == SUCCESS) {
  681. if (fcport->flags & FC_FABRIC_DEVICE) {
  682. ha->isp_ops.fabric_logout(ha, fcport->loop_id);
  683. qla2x00_mark_device_lost(ha, fcport, 0, 0);
  684. }
  685. }
  686. #endif
  687. } else {
  688. DEBUG2(printk(KERN_INFO
  689. "%s failed: loop not ready\n",__func__));
  690. }
  691. if (ret == FAILED) {
  692. DEBUG3(printk("%s(%ld): device reset failed\n",
  693. __func__, ha->host_no));
  694. qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
  695. __func__);
  696. goto eh_dev_reset_done;
  697. }
  698. /* Flush outstanding commands. */
  699. if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
  700. ret = FAILED;
  701. if (ret == FAILED) {
  702. DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
  703. __func__, ha->host_no));
  704. qla_printk(KERN_INFO, ha,
  705. "%s: failed while waiting for commands\n", __func__);
  706. } else
  707. qla_printk(KERN_INFO, ha,
  708. "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
  709. id, lun);
  710. eh_dev_reset_done:
  711. return ret;
  712. }
  713. /**************************************************************************
  714. * qla2x00_eh_wait_for_pending_commands
  715. *
  716. * Description:
  717. * Waits for all the commands to come back from the specified host.
  718. *
  719. * Input:
  720. * ha - pointer to scsi_qla_host structure.
  721. *
  722. * Returns:
  723. * 1 : SUCCESS
  724. * 0 : FAILED
  725. *
  726. * Note:
  727. **************************************************************************/
  728. static int
  729. qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
  730. {
  731. int cnt;
  732. int status;
  733. srb_t *sp;
  734. struct scsi_cmnd *cmd;
  735. unsigned long flags;
  736. status = 1;
  737. /*
  738. * Waiting for all commands for the designated target in the active
  739. * array
  740. */
  741. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  742. spin_lock_irqsave(&ha->hardware_lock, flags);
  743. sp = ha->outstanding_cmds[cnt];
  744. if (sp) {
  745. cmd = sp->cmd;
  746. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  747. status = qla2x00_eh_wait_on_command(ha, cmd);
  748. if (status == 0)
  749. break;
  750. }
  751. else {
  752. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  753. }
  754. }
  755. return (status);
  756. }
  757. /**************************************************************************
  758. * qla2xxx_eh_bus_reset
  759. *
  760. * Description:
  761. * The bus reset function will reset the bus and abort any executing
  762. * commands.
  763. *
  764. * Input:
  765. * cmd = Linux SCSI command packet of the command that cause the
  766. * bus reset.
  767. *
  768. * Returns:
  769. * SUCCESS/FAILURE (defined as macro in scsi.h).
  770. *
  771. **************************************************************************/
  772. static int
  773. qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
  774. {
  775. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  776. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  777. int ret;
  778. unsigned int id, lun;
  779. unsigned long serial;
  780. qla2x00_block_error_handler(cmd);
  781. ret = FAILED;
  782. id = cmd->device->id;
  783. lun = cmd->device->lun;
  784. serial = cmd->serial_number;
  785. if (!fcport)
  786. return ret;
  787. qla_printk(KERN_INFO, ha,
  788. "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
  789. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
  790. DEBUG2(printk("%s failed:board disabled\n",__func__));
  791. goto eh_bus_reset_done;
  792. }
  793. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  794. if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
  795. ret = SUCCESS;
  796. }
  797. if (ret == FAILED)
  798. goto eh_bus_reset_done;
  799. /* Flush outstanding commands. */
  800. if (!qla2x00_eh_wait_for_pending_commands(ha))
  801. ret = FAILED;
  802. eh_bus_reset_done:
  803. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  804. (ret == FAILED) ? "failed" : "succeded");
  805. return ret;
  806. }
  807. /**************************************************************************
  808. * qla2xxx_eh_host_reset
  809. *
  810. * Description:
  811. * The reset function will reset the Adapter.
  812. *
  813. * Input:
  814. * cmd = Linux SCSI command packet of the command that cause the
  815. * adapter reset.
  816. *
  817. * Returns:
  818. * Either SUCCESS or FAILED.
  819. *
  820. * Note:
  821. **************************************************************************/
  822. static int
  823. qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
  824. {
  825. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  826. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  827. int ret;
  828. unsigned int id, lun;
  829. unsigned long serial;
  830. qla2x00_block_error_handler(cmd);
  831. ret = FAILED;
  832. id = cmd->device->id;
  833. lun = cmd->device->lun;
  834. serial = cmd->serial_number;
  835. if (!fcport)
  836. return ret;
  837. qla_printk(KERN_INFO, ha,
  838. "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
  839. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  840. goto eh_host_reset_lock;
  841. /*
  842. * Fixme-may be dpc thread is active and processing
  843. * loop_resync,so wait a while for it to
  844. * be completed and then issue big hammer.Otherwise
  845. * it may cause I/O failure as big hammer marks the
  846. * devices as lost kicking of the port_down_timer
  847. * while dpc is stuck for the mailbox to complete.
  848. */
  849. qla2x00_wait_for_loop_ready(ha);
  850. set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  851. if (qla2x00_abort_isp(ha)) {
  852. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  853. /* failed. schedule dpc to try */
  854. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  855. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  856. goto eh_host_reset_lock;
  857. }
  858. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  859. /* Waiting for our command in done_queue to be returned to OS.*/
  860. if (qla2x00_eh_wait_for_pending_commands(ha))
  861. ret = SUCCESS;
  862. eh_host_reset_lock:
  863. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  864. (ret == FAILED) ? "failed" : "succeded");
  865. return ret;
  866. }
  867. /*
  868. * qla2x00_loop_reset
  869. * Issue loop reset.
  870. *
  871. * Input:
  872. * ha = adapter block pointer.
  873. *
  874. * Returns:
  875. * 0 = success
  876. */
  877. static int
  878. qla2x00_loop_reset(scsi_qla_host_t *ha)
  879. {
  880. int ret;
  881. struct fc_port *fcport;
  882. if (ha->flags.enable_lip_full_login) {
  883. ret = qla2x00_full_login_lip(ha);
  884. if (ret != QLA_SUCCESS) {
  885. DEBUG2_3(printk("%s(%ld): bus_reset failed: "
  886. "full_login_lip=%d.\n", __func__, ha->host_no,
  887. ret));
  888. }
  889. atomic_set(&ha->loop_state, LOOP_DOWN);
  890. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  891. qla2x00_mark_all_devices_lost(ha, 0);
  892. qla2x00_wait_for_loop_ready(ha);
  893. }
  894. if (ha->flags.enable_lip_reset) {
  895. ret = qla2x00_lip_reset(ha);
  896. if (ret != QLA_SUCCESS) {
  897. DEBUG2_3(printk("%s(%ld): bus_reset failed: "
  898. "lip_reset=%d.\n", __func__, ha->host_no, ret));
  899. }
  900. qla2x00_wait_for_loop_ready(ha);
  901. }
  902. if (ha->flags.enable_target_reset) {
  903. list_for_each_entry(fcport, &ha->fcports, list) {
  904. if (fcport->port_type != FCT_TARGET)
  905. continue;
  906. ret = qla2x00_device_reset(ha, fcport);
  907. if (ret != QLA_SUCCESS) {
  908. DEBUG2_3(printk("%s(%ld): bus_reset failed: "
  909. "target_reset=%d d_id=%x.\n", __func__,
  910. ha->host_no, ret, fcport->d_id.b24));
  911. }
  912. }
  913. }
  914. /* Issue marker command only when we are going to start the I/O */
  915. ha->marker_needed = 1;
  916. return QLA_SUCCESS;
  917. }
  918. /*
  919. * qla2x00_device_reset
  920. * Issue bus device reset message to the target.
  921. *
  922. * Input:
  923. * ha = adapter block pointer.
  924. * t = SCSI ID.
  925. * TARGET_QUEUE_LOCK must be released.
  926. * ADAPTER_STATE_LOCK must be released.
  927. *
  928. * Context:
  929. * Kernel context.
  930. */
  931. static int
  932. qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
  933. {
  934. /* Abort Target command will clear Reservation */
  935. return ha->isp_ops.abort_target(reset_fcport);
  936. }
  937. static int
  938. qla2xxx_slave_alloc(struct scsi_device *sdev)
  939. {
  940. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  941. if (!rport || fc_remote_port_chkready(rport))
  942. return -ENXIO;
  943. sdev->hostdata = *(fc_port_t **)rport->dd_data;
  944. return 0;
  945. }
  946. static int
  947. qla2xxx_slave_configure(struct scsi_device *sdev)
  948. {
  949. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  950. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  951. if (sdev->tagged_supported)
  952. scsi_activate_tcq(sdev, ha->max_q_depth);
  953. else
  954. scsi_deactivate_tcq(sdev, ha->max_q_depth);
  955. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  956. return 0;
  957. }
  958. static void
  959. qla2xxx_slave_destroy(struct scsi_device *sdev)
  960. {
  961. sdev->hostdata = NULL;
  962. }
  963. static int
  964. qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
  965. {
  966. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  967. return sdev->queue_depth;
  968. }
  969. static int
  970. qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
  971. {
  972. if (sdev->tagged_supported) {
  973. scsi_set_tag_type(sdev, tag_type);
  974. if (tag_type)
  975. scsi_activate_tcq(sdev, sdev->queue_depth);
  976. else
  977. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  978. } else
  979. tag_type = 0;
  980. return tag_type;
  981. }
  982. /**
  983. * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
  984. * @ha: HA context
  985. *
  986. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  987. * supported addressing method.
  988. */
  989. static void
  990. qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
  991. {
  992. /* Assume a 32bit DMA mask. */
  993. ha->flags.enable_64bit_addressing = 0;
  994. if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
  995. /* Any upper-dword bits set? */
  996. if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
  997. !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
  998. /* Ok, a 64bit DMA mask is applicable. */
  999. ha->flags.enable_64bit_addressing = 1;
  1000. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
  1001. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
  1002. return;
  1003. }
  1004. }
  1005. dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
  1006. pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
  1007. }
  1008. static inline void
  1009. qla2x00_set_isp_flags(scsi_qla_host_t *ha)
  1010. {
  1011. ha->device_type = DT_EXTENDED_IDS;
  1012. switch (ha->pdev->device) {
  1013. case PCI_DEVICE_ID_QLOGIC_ISP2100:
  1014. ha->device_type |= DT_ISP2100;
  1015. ha->device_type &= ~DT_EXTENDED_IDS;
  1016. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1017. break;
  1018. case PCI_DEVICE_ID_QLOGIC_ISP2200:
  1019. ha->device_type |= DT_ISP2200;
  1020. ha->device_type &= ~DT_EXTENDED_IDS;
  1021. ha->fw_srisc_address = RISC_START_ADDRESS_2100;
  1022. break;
  1023. case PCI_DEVICE_ID_QLOGIC_ISP2300:
  1024. ha->device_type |= DT_ISP2300;
  1025. ha->device_type |= DT_ZIO_SUPPORTED;
  1026. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1027. break;
  1028. case PCI_DEVICE_ID_QLOGIC_ISP2312:
  1029. ha->device_type |= DT_ISP2312;
  1030. ha->device_type |= DT_ZIO_SUPPORTED;
  1031. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1032. break;
  1033. case PCI_DEVICE_ID_QLOGIC_ISP2322:
  1034. ha->device_type |= DT_ISP2322;
  1035. ha->device_type |= DT_ZIO_SUPPORTED;
  1036. if (ha->pdev->subsystem_vendor == 0x1028 &&
  1037. ha->pdev->subsystem_device == 0x0170)
  1038. ha->device_type |= DT_OEM_001;
  1039. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1040. break;
  1041. case PCI_DEVICE_ID_QLOGIC_ISP6312:
  1042. ha->device_type |= DT_ISP6312;
  1043. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1044. break;
  1045. case PCI_DEVICE_ID_QLOGIC_ISP6322:
  1046. ha->device_type |= DT_ISP6322;
  1047. ha->fw_srisc_address = RISC_START_ADDRESS_2300;
  1048. break;
  1049. case PCI_DEVICE_ID_QLOGIC_ISP2422:
  1050. ha->device_type |= DT_ISP2422;
  1051. ha->device_type |= DT_ZIO_SUPPORTED;
  1052. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1053. break;
  1054. case PCI_DEVICE_ID_QLOGIC_ISP2432:
  1055. ha->device_type |= DT_ISP2432;
  1056. ha->device_type |= DT_ZIO_SUPPORTED;
  1057. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1058. break;
  1059. case PCI_DEVICE_ID_QLOGIC_ISP5422:
  1060. ha->device_type |= DT_ISP5422;
  1061. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1062. break;
  1063. case PCI_DEVICE_ID_QLOGIC_ISP5432:
  1064. ha->device_type |= DT_ISP5432;
  1065. ha->fw_srisc_address = RISC_START_ADDRESS_2400;
  1066. break;
  1067. }
  1068. }
  1069. static int
  1070. qla2x00_iospace_config(scsi_qla_host_t *ha)
  1071. {
  1072. unsigned long pio, pio_len, pio_flags;
  1073. unsigned long mmio, mmio_len, mmio_flags;
  1074. /* We only need PIO for Flash operations on ISP2312 v2 chips. */
  1075. pio = pci_resource_start(ha->pdev, 0);
  1076. pio_len = pci_resource_len(ha->pdev, 0);
  1077. pio_flags = pci_resource_flags(ha->pdev, 0);
  1078. if (pio_flags & IORESOURCE_IO) {
  1079. if (pio_len < MIN_IOBASE_LEN) {
  1080. qla_printk(KERN_WARNING, ha,
  1081. "Invalid PCI I/O region size (%s)...\n",
  1082. pci_name(ha->pdev));
  1083. pio = 0;
  1084. }
  1085. } else {
  1086. qla_printk(KERN_WARNING, ha,
  1087. "region #0 not a PIO resource (%s)...\n",
  1088. pci_name(ha->pdev));
  1089. pio = 0;
  1090. }
  1091. /* Use MMIO operations for all accesses. */
  1092. mmio = pci_resource_start(ha->pdev, 1);
  1093. mmio_len = pci_resource_len(ha->pdev, 1);
  1094. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1095. if (!(mmio_flags & IORESOURCE_MEM)) {
  1096. qla_printk(KERN_ERR, ha,
  1097. "region #0 not an MMIO resource (%s), aborting\n",
  1098. pci_name(ha->pdev));
  1099. goto iospace_error_exit;
  1100. }
  1101. if (mmio_len < MIN_IOBASE_LEN) {
  1102. qla_printk(KERN_ERR, ha,
  1103. "Invalid PCI mem region size (%s), aborting\n",
  1104. pci_name(ha->pdev));
  1105. goto iospace_error_exit;
  1106. }
  1107. if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
  1108. qla_printk(KERN_WARNING, ha,
  1109. "Failed to reserve PIO/MMIO regions (%s)\n",
  1110. pci_name(ha->pdev));
  1111. goto iospace_error_exit;
  1112. }
  1113. ha->pio_address = pio;
  1114. ha->pio_length = pio_len;
  1115. ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
  1116. if (!ha->iobase) {
  1117. qla_printk(KERN_ERR, ha,
  1118. "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
  1119. goto iospace_error_exit;
  1120. }
  1121. return (0);
  1122. iospace_error_exit:
  1123. return (-ENOMEM);
  1124. }
  1125. static void
  1126. qla2x00_enable_intrs(scsi_qla_host_t *ha)
  1127. {
  1128. unsigned long flags = 0;
  1129. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1130. spin_lock_irqsave(&ha->hardware_lock, flags);
  1131. ha->interrupts_on = 1;
  1132. /* enable risc and host interrupts */
  1133. WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
  1134. RD_REG_WORD(&reg->ictrl);
  1135. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1136. }
  1137. static void
  1138. qla2x00_disable_intrs(scsi_qla_host_t *ha)
  1139. {
  1140. unsigned long flags = 0;
  1141. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1142. spin_lock_irqsave(&ha->hardware_lock, flags);
  1143. ha->interrupts_on = 0;
  1144. /* disable risc and host interrupts */
  1145. WRT_REG_WORD(&reg->ictrl, 0);
  1146. RD_REG_WORD(&reg->ictrl);
  1147. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1148. }
  1149. static void
  1150. qla24xx_enable_intrs(scsi_qla_host_t *ha)
  1151. {
  1152. unsigned long flags = 0;
  1153. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1154. spin_lock_irqsave(&ha->hardware_lock, flags);
  1155. ha->interrupts_on = 1;
  1156. WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
  1157. RD_REG_DWORD(&reg->ictrl);
  1158. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1159. }
  1160. static void
  1161. qla24xx_disable_intrs(scsi_qla_host_t *ha)
  1162. {
  1163. unsigned long flags = 0;
  1164. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1165. spin_lock_irqsave(&ha->hardware_lock, flags);
  1166. ha->interrupts_on = 0;
  1167. WRT_REG_DWORD(&reg->ictrl, 0);
  1168. RD_REG_DWORD(&reg->ictrl);
  1169. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1170. }
  1171. static void
  1172. qla2xxx_scan_start(struct Scsi_Host *shost)
  1173. {
  1174. scsi_qla_host_t *ha = (scsi_qla_host_t *)shost->hostdata;
  1175. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1176. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1177. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1178. }
  1179. static int
  1180. qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1181. {
  1182. scsi_qla_host_t *ha = (scsi_qla_host_t *)shost->hostdata;
  1183. if (!ha->host)
  1184. return 1;
  1185. if (time > ha->loop_reset_delay * HZ)
  1186. return 1;
  1187. return atomic_read(&ha->loop_state) == LOOP_READY;
  1188. }
  1189. /*
  1190. * PCI driver interface
  1191. */
  1192. static int __devinit
  1193. qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1194. {
  1195. int ret = -ENODEV;
  1196. device_reg_t __iomem *reg;
  1197. struct Scsi_Host *host;
  1198. scsi_qla_host_t *ha;
  1199. unsigned long flags = 0;
  1200. char pci_info[20];
  1201. char fw_str[30];
  1202. struct scsi_host_template *sht;
  1203. if (pci_enable_device(pdev))
  1204. goto probe_out;
  1205. sht = &qla2x00_driver_template;
  1206. if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
  1207. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
  1208. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
  1209. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432)
  1210. sht = &qla24xx_driver_template;
  1211. host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
  1212. if (host == NULL) {
  1213. printk(KERN_WARNING
  1214. "qla2xxx: Couldn't allocate host from scsi layer!\n");
  1215. goto probe_disable_device;
  1216. }
  1217. /* Clear our data area */
  1218. ha = (scsi_qla_host_t *)host->hostdata;
  1219. memset(ha, 0, sizeof(scsi_qla_host_t));
  1220. ha->pdev = pdev;
  1221. ha->host = host;
  1222. ha->host_no = host->host_no;
  1223. sprintf(ha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, ha->host_no);
  1224. /* Set ISP-type information. */
  1225. qla2x00_set_isp_flags(ha);
  1226. /* Configure PCI I/O space */
  1227. ret = qla2x00_iospace_config(ha);
  1228. if (ret)
  1229. goto probe_failed;
  1230. qla_printk(KERN_INFO, ha,
  1231. "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
  1232. ha->iobase);
  1233. spin_lock_init(&ha->hardware_lock);
  1234. ha->prev_topology = 0;
  1235. ha->init_cb_size = sizeof(init_cb_t);
  1236. ha->mgmt_svr_loop_id = MANAGEMENT_SERVER;
  1237. ha->link_data_rate = PORT_SPEED_UNKNOWN;
  1238. ha->optrom_size = OPTROM_SIZE_2300;
  1239. ha->max_q_depth = MAX_Q_DEPTH;
  1240. if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
  1241. ha->max_q_depth = ql2xmaxqdepth;
  1242. /* Assign ISP specific operations. */
  1243. ha->isp_ops.pci_config = qla2100_pci_config;
  1244. ha->isp_ops.reset_chip = qla2x00_reset_chip;
  1245. ha->isp_ops.chip_diag = qla2x00_chip_diag;
  1246. ha->isp_ops.config_rings = qla2x00_config_rings;
  1247. ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
  1248. ha->isp_ops.nvram_config = qla2x00_nvram_config;
  1249. ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
  1250. ha->isp_ops.load_risc = qla2x00_load_risc;
  1251. ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
  1252. ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
  1253. ha->isp_ops.intr_handler = qla2100_intr_handler;
  1254. ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
  1255. ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
  1256. ha->isp_ops.abort_command = qla2x00_abort_command;
  1257. ha->isp_ops.abort_target = qla2x00_abort_target;
  1258. ha->isp_ops.fabric_login = qla2x00_login_fabric;
  1259. ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
  1260. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
  1261. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
  1262. ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
  1263. ha->isp_ops.prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb;
  1264. ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
  1265. ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
  1266. ha->isp_ops.fw_dump = qla2100_fw_dump;
  1267. ha->isp_ops.read_optrom = qla2x00_read_optrom_data;
  1268. ha->isp_ops.write_optrom = qla2x00_write_optrom_data;
  1269. ha->isp_ops.get_flash_version = qla2x00_get_flash_version;
  1270. if (IS_QLA2100(ha)) {
  1271. host->max_id = MAX_TARGETS_2100;
  1272. ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
  1273. ha->request_q_length = REQUEST_ENTRY_CNT_2100;
  1274. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1275. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1276. host->sg_tablesize = 32;
  1277. ha->gid_list_info_size = 4;
  1278. } else if (IS_QLA2200(ha)) {
  1279. host->max_id = MAX_TARGETS_2200;
  1280. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1281. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1282. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1283. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1284. ha->gid_list_info_size = 4;
  1285. } else if (IS_QLA23XX(ha)) {
  1286. host->max_id = MAX_TARGETS_2200;
  1287. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1288. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1289. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1290. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1291. ha->isp_ops.pci_config = qla2300_pci_config;
  1292. ha->isp_ops.intr_handler = qla2300_intr_handler;
  1293. ha->isp_ops.fw_dump = qla2300_fw_dump;
  1294. ha->isp_ops.beacon_on = qla2x00_beacon_on;
  1295. ha->isp_ops.beacon_off = qla2x00_beacon_off;
  1296. ha->isp_ops.beacon_blink = qla2x00_beacon_blink;
  1297. ha->gid_list_info_size = 6;
  1298. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  1299. ha->optrom_size = OPTROM_SIZE_2322;
  1300. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1301. host->max_id = MAX_TARGETS_2200;
  1302. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1303. ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
  1304. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1305. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1306. ha->init_cb_size = sizeof(struct init_cb_24xx);
  1307. ha->mgmt_svr_loop_id = 10;
  1308. ha->isp_ops.pci_config = qla24xx_pci_config;
  1309. ha->isp_ops.reset_chip = qla24xx_reset_chip;
  1310. ha->isp_ops.chip_diag = qla24xx_chip_diag;
  1311. ha->isp_ops.config_rings = qla24xx_config_rings;
  1312. ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
  1313. ha->isp_ops.nvram_config = qla24xx_nvram_config;
  1314. ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
  1315. ha->isp_ops.load_risc = qla24xx_load_risc;
  1316. ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
  1317. ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
  1318. ha->isp_ops.intr_handler = qla24xx_intr_handler;
  1319. ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
  1320. ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
  1321. ha->isp_ops.abort_command = qla24xx_abort_command;
  1322. ha->isp_ops.abort_target = qla24xx_abort_target;
  1323. ha->isp_ops.fabric_login = qla24xx_login_fabric;
  1324. ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
  1325. ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
  1326. ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
  1327. ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
  1328. ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
  1329. ha->isp_ops.fw_dump = qla24xx_fw_dump;
  1330. ha->isp_ops.read_optrom = qla24xx_read_optrom_data;
  1331. ha->isp_ops.write_optrom = qla24xx_write_optrom_data;
  1332. ha->isp_ops.beacon_on = qla24xx_beacon_on;
  1333. ha->isp_ops.beacon_off = qla24xx_beacon_off;
  1334. ha->isp_ops.beacon_blink = qla24xx_beacon_blink;
  1335. ha->isp_ops.get_flash_version = qla24xx_get_flash_version;
  1336. ha->gid_list_info_size = 8;
  1337. ha->optrom_size = OPTROM_SIZE_24XX;
  1338. }
  1339. host->can_queue = ha->request_q_length + 128;
  1340. /* load the F/W, read paramaters, and init the H/W */
  1341. ha->instance = num_hosts;
  1342. init_MUTEX(&ha->mbx_cmd_sem);
  1343. init_MUTEX_LOCKED(&ha->mbx_intr_sem);
  1344. INIT_LIST_HEAD(&ha->list);
  1345. INIT_LIST_HEAD(&ha->fcports);
  1346. qla2x00_config_dma_addressing(ha);
  1347. if (qla2x00_mem_alloc(ha)) {
  1348. qla_printk(KERN_WARNING, ha,
  1349. "[ERROR] Failed to allocate memory for adapter\n");
  1350. ret = -ENOMEM;
  1351. goto probe_failed;
  1352. }
  1353. if (qla2x00_initialize_adapter(ha) &&
  1354. !(ha->device_flags & DFLG_NO_CABLE)) {
  1355. qla_printk(KERN_WARNING, ha,
  1356. "Failed to initialize adapter\n");
  1357. DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
  1358. "Adapter flags %x.\n",
  1359. ha->host_no, ha->device_flags));
  1360. ret = -ENODEV;
  1361. goto probe_failed;
  1362. }
  1363. /*
  1364. * Startup the kernel thread for this host adapter
  1365. */
  1366. ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
  1367. "%s_dpc", ha->host_str);
  1368. if (IS_ERR(ha->dpc_thread)) {
  1369. qla_printk(KERN_WARNING, ha,
  1370. "Unable to start DPC thread!\n");
  1371. ret = PTR_ERR(ha->dpc_thread);
  1372. goto probe_failed;
  1373. }
  1374. host->this_id = 255;
  1375. host->cmd_per_lun = 3;
  1376. host->unique_id = ha->instance;
  1377. host->max_cmd_len = MAX_CMDSZ;
  1378. host->max_channel = MAX_BUSES - 1;
  1379. host->max_lun = MAX_LUNS;
  1380. host->transportt = qla2xxx_transport_template;
  1381. ret = qla2x00_request_irqs(ha);
  1382. if (ret)
  1383. goto probe_failed;
  1384. /* Initialized the timer */
  1385. qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
  1386. DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
  1387. ha->host_no, ha));
  1388. ha->isp_ops.disable_intrs(ha);
  1389. spin_lock_irqsave(&ha->hardware_lock, flags);
  1390. reg = ha->iobase;
  1391. if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  1392. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
  1393. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
  1394. } else {
  1395. WRT_REG_WORD(&reg->isp.semaphore, 0);
  1396. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
  1397. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
  1398. /* Enable proper parity */
  1399. if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  1400. if (IS_QLA2300(ha))
  1401. /* SRAM parity */
  1402. WRT_REG_WORD(&reg->isp.hccr,
  1403. (HCCR_ENABLE_PARITY + 0x1));
  1404. else
  1405. /* SRAM, Instruction RAM and GP RAM parity */
  1406. WRT_REG_WORD(&reg->isp.hccr,
  1407. (HCCR_ENABLE_PARITY + 0x7));
  1408. }
  1409. }
  1410. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1411. ha->isp_ops.enable_intrs(ha);
  1412. pci_set_drvdata(pdev, ha);
  1413. ha->flags.init_done = 1;
  1414. ha->flags.online = 1;
  1415. num_hosts++;
  1416. ret = scsi_add_host(host, &pdev->dev);
  1417. if (ret)
  1418. goto probe_failed;
  1419. scsi_scan_host(host);
  1420. qla2x00_alloc_sysfs_attr(ha);
  1421. qla2x00_init_host_attr(ha);
  1422. qla_printk(KERN_INFO, ha, "\n"
  1423. " QLogic Fibre Channel HBA Driver: %s\n"
  1424. " QLogic %s - %s\n"
  1425. " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
  1426. qla2x00_version_str, ha->model_number,
  1427. ha->model_desc ? ha->model_desc: "", pdev->device,
  1428. ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
  1429. ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
  1430. ha->isp_ops.fw_version_str(ha, fw_str));
  1431. return 0;
  1432. probe_failed:
  1433. qla2x00_free_device(ha);
  1434. scsi_host_put(host);
  1435. probe_disable_device:
  1436. pci_disable_device(pdev);
  1437. probe_out:
  1438. return ret;
  1439. }
  1440. static void __devexit
  1441. qla2x00_remove_one(struct pci_dev *pdev)
  1442. {
  1443. scsi_qla_host_t *ha;
  1444. ha = pci_get_drvdata(pdev);
  1445. qla2x00_free_sysfs_attr(ha);
  1446. fc_remove_host(ha->host);
  1447. scsi_remove_host(ha->host);
  1448. qla2x00_free_device(ha);
  1449. scsi_host_put(ha->host);
  1450. pci_disable_device(pdev);
  1451. pci_set_drvdata(pdev, NULL);
  1452. }
  1453. static void
  1454. qla2x00_free_device(scsi_qla_host_t *ha)
  1455. {
  1456. /* Disable timer */
  1457. if (ha->timer_active)
  1458. qla2x00_stop_timer(ha);
  1459. /* Kill the kernel thread for this host */
  1460. if (ha->dpc_thread) {
  1461. struct task_struct *t = ha->dpc_thread;
  1462. /*
  1463. * qla2xxx_wake_dpc checks for ->dpc_thread
  1464. * so we need to zero it out.
  1465. */
  1466. ha->dpc_thread = NULL;
  1467. kthread_stop(t);
  1468. }
  1469. if (ha->eft)
  1470. qla2x00_trace_control(ha, TC_DISABLE, 0, 0);
  1471. ha->flags.online = 0;
  1472. /* Stop currently executing firmware. */
  1473. qla2x00_try_to_stop_firmware(ha);
  1474. /* turn-off interrupts on the card */
  1475. if (ha->interrupts_on)
  1476. ha->isp_ops.disable_intrs(ha);
  1477. qla2x00_mem_free(ha);
  1478. qla2x00_free_irqs(ha);
  1479. /* release io space registers */
  1480. if (ha->iobase)
  1481. iounmap(ha->iobase);
  1482. pci_release_regions(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);