ql4_os.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2006 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <scsi/scsi_tcq.h>
  10. #include <scsi/scsicam.h>
  11. #include "ql4_def.h"
  12. #include "ql4_version.h"
  13. #include "ql4_glbl.h"
  14. #include "ql4_dbg.h"
  15. #include "ql4_inline.h"
  16. /*
  17. * Driver version
  18. */
  19. static char qla4xxx_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Module parameter information and variables
  26. */
  27. int ql4xdiscoverywait = 60;
  28. module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
  30. int ql4xdontresethba = 0;
  31. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  32. MODULE_PARM_DESC(ql4xdontresethba,
  33. "Don't reset the HBA for driver recovery \n"
  34. " 0 - It will reset HBA (Default)\n"
  35. " 1 - It will NOT reset HBA");
  36. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  37. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  38. MODULE_PARM_DESC(ql4xextended_error_logging,
  39. "Option to enable extended error logging, "
  40. "Default is 0 - no logging, 1 - debug logging");
  41. int ql4xenablemsix = 1;
  42. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  43. MODULE_PARM_DESC(ql4xenablemsix,
  44. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  45. " 0 = enable INTx interrupt mechanism.\n"
  46. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  47. " 2 = enable MSI interrupt mechanism.");
  48. #define QL4_DEF_QDEPTH 32
  49. /*
  50. * SCSI host template entry points
  51. */
  52. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  53. /*
  54. * iSCSI template entry points
  55. */
  56. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  57. enum iscsi_tgt_dscvr type, uint32_t enable,
  58. struct sockaddr *dst_addr);
  59. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  60. enum iscsi_param param, char *buf);
  61. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  62. enum iscsi_param param, char *buf);
  63. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  64. enum iscsi_host_param param, char *buf);
  65. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
  66. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  67. /*
  68. * SCSI host template entry points
  69. */
  70. static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
  71. void (*done) (struct scsi_cmnd *));
  72. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  73. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  74. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  75. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  76. static int qla4xxx_slave_alloc(struct scsi_device *device);
  77. static int qla4xxx_slave_configure(struct scsi_device *device);
  78. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  79. static void qla4xxx_scan_start(struct Scsi_Host *shost);
  80. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  81. QLA82XX_LEGACY_INTR_CONFIG;
  82. static struct scsi_host_template qla4xxx_driver_template = {
  83. .module = THIS_MODULE,
  84. .name = DRIVER_NAME,
  85. .proc_name = DRIVER_NAME,
  86. .queuecommand = qla4xxx_queuecommand,
  87. .eh_abort_handler = qla4xxx_eh_abort,
  88. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  89. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  90. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  91. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  92. .slave_configure = qla4xxx_slave_configure,
  93. .slave_alloc = qla4xxx_slave_alloc,
  94. .slave_destroy = qla4xxx_slave_destroy,
  95. .scan_finished = iscsi_scan_finished,
  96. .scan_start = qla4xxx_scan_start,
  97. .this_id = -1,
  98. .cmd_per_lun = 3,
  99. .use_clustering = ENABLE_CLUSTERING,
  100. .sg_tablesize = SG_ALL,
  101. .max_sectors = 0xFFFF,
  102. };
  103. static struct iscsi_transport qla4xxx_iscsi_transport = {
  104. .owner = THIS_MODULE,
  105. .name = DRIVER_NAME,
  106. .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
  107. CAP_DATA_PATH_OFFLOAD,
  108. .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
  109. ISCSI_TARGET_NAME | ISCSI_TPGT |
  110. ISCSI_TARGET_ALIAS,
  111. .host_param_mask = ISCSI_HOST_HWADDRESS |
  112. ISCSI_HOST_IPADDRESS |
  113. ISCSI_HOST_INITIATOR_NAME,
  114. .tgt_dscvr = qla4xxx_tgt_dscvr,
  115. .get_conn_param = qla4xxx_conn_get_param,
  116. .get_session_param = qla4xxx_sess_get_param,
  117. .get_host_param = qla4xxx_host_get_param,
  118. .session_recovery_timedout = qla4xxx_recovery_timedout,
  119. };
  120. static struct scsi_transport_template *qla4xxx_scsi_transport;
  121. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  122. {
  123. struct iscsi_cls_session *session;
  124. struct ddb_entry *ddb_entry;
  125. session = starget_to_session(scsi_target(sc->device));
  126. ddb_entry = session->dd_data;
  127. /* if we are not logged in then the LLD is going to clean up the cmd */
  128. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  129. return BLK_EH_RESET_TIMER;
  130. else
  131. return BLK_EH_NOT_HANDLED;
  132. }
  133. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
  134. {
  135. struct ddb_entry *ddb_entry = session->dd_data;
  136. struct scsi_qla_host *ha = ddb_entry->ha;
  137. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  138. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  139. DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
  140. "of (%d) secs exhausted, marking device DEAD.\n",
  141. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  142. QL4_SESS_RECOVERY_TMO));
  143. }
  144. }
  145. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  146. enum iscsi_host_param param, char *buf)
  147. {
  148. struct scsi_qla_host *ha = to_qla_host(shost);
  149. int len;
  150. switch (param) {
  151. case ISCSI_HOST_PARAM_HWADDRESS:
  152. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  153. break;
  154. case ISCSI_HOST_PARAM_IPADDRESS:
  155. len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
  156. ha->ip_address[1], ha->ip_address[2],
  157. ha->ip_address[3]);
  158. break;
  159. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  160. len = sprintf(buf, "%s\n", ha->name_string);
  161. break;
  162. default:
  163. return -ENOSYS;
  164. }
  165. return len;
  166. }
  167. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  168. enum iscsi_param param, char *buf)
  169. {
  170. struct ddb_entry *ddb_entry = sess->dd_data;
  171. int len;
  172. switch (param) {
  173. case ISCSI_PARAM_TARGET_NAME:
  174. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  175. ddb_entry->iscsi_name);
  176. break;
  177. case ISCSI_PARAM_TPGT:
  178. len = sprintf(buf, "%u\n", ddb_entry->tpgt);
  179. break;
  180. case ISCSI_PARAM_TARGET_ALIAS:
  181. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  182. ddb_entry->iscsi_alias);
  183. break;
  184. default:
  185. return -ENOSYS;
  186. }
  187. return len;
  188. }
  189. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  190. enum iscsi_param param, char *buf)
  191. {
  192. struct iscsi_cls_session *session;
  193. struct ddb_entry *ddb_entry;
  194. int len;
  195. session = iscsi_dev_to_session(conn->dev.parent);
  196. ddb_entry = session->dd_data;
  197. switch (param) {
  198. case ISCSI_PARAM_CONN_PORT:
  199. len = sprintf(buf, "%hu\n", ddb_entry->port);
  200. break;
  201. case ISCSI_PARAM_CONN_ADDRESS:
  202. /* TODO: what are the ipv6 bits */
  203. len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
  204. break;
  205. default:
  206. return -ENOSYS;
  207. }
  208. return len;
  209. }
  210. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  211. enum iscsi_tgt_dscvr type, uint32_t enable,
  212. struct sockaddr *dst_addr)
  213. {
  214. struct scsi_qla_host *ha;
  215. struct sockaddr_in *addr;
  216. struct sockaddr_in6 *addr6;
  217. int ret = 0;
  218. ha = (struct scsi_qla_host *) shost->hostdata;
  219. switch (type) {
  220. case ISCSI_TGT_DSCVR_SEND_TARGETS:
  221. if (dst_addr->sa_family == AF_INET) {
  222. addr = (struct sockaddr_in *)dst_addr;
  223. if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
  224. addr->sin_port) != QLA_SUCCESS)
  225. ret = -EIO;
  226. } else if (dst_addr->sa_family == AF_INET6) {
  227. /*
  228. * TODO: fix qla4xxx_send_tgts
  229. */
  230. addr6 = (struct sockaddr_in6 *)dst_addr;
  231. if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
  232. addr6->sin6_port) != QLA_SUCCESS)
  233. ret = -EIO;
  234. } else
  235. ret = -ENOSYS;
  236. break;
  237. default:
  238. ret = -ENOSYS;
  239. }
  240. return ret;
  241. }
  242. void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
  243. {
  244. if (!ddb_entry->sess)
  245. return;
  246. if (ddb_entry->conn) {
  247. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  248. iscsi_remove_session(ddb_entry->sess);
  249. }
  250. iscsi_free_session(ddb_entry->sess);
  251. }
  252. int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
  253. {
  254. int err;
  255. ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
  256. err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
  257. if (err) {
  258. DEBUG2(printk(KERN_ERR "Could not add session.\n"));
  259. return err;
  260. }
  261. ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
  262. if (!ddb_entry->conn) {
  263. iscsi_remove_session(ddb_entry->sess);
  264. DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
  265. return -ENOMEM;
  266. }
  267. /* finally ready to go */
  268. iscsi_unblock_session(ddb_entry->sess);
  269. return 0;
  270. }
  271. struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
  272. {
  273. struct ddb_entry *ddb_entry;
  274. struct iscsi_cls_session *sess;
  275. sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
  276. sizeof(struct ddb_entry));
  277. if (!sess)
  278. return NULL;
  279. ddb_entry = sess->dd_data;
  280. memset(ddb_entry, 0, sizeof(*ddb_entry));
  281. ddb_entry->ha = ha;
  282. ddb_entry->sess = sess;
  283. return ddb_entry;
  284. }
  285. static void qla4xxx_scan_start(struct Scsi_Host *shost)
  286. {
  287. struct scsi_qla_host *ha = shost_priv(shost);
  288. struct ddb_entry *ddb_entry, *ddbtemp;
  289. /* finish setup of sessions that were already setup in firmware */
  290. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  291. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
  292. qla4xxx_add_sess(ddb_entry);
  293. }
  294. }
  295. /*
  296. * Timer routines
  297. */
  298. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  299. unsigned long interval)
  300. {
  301. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  302. __func__, ha->host->host_no));
  303. init_timer(&ha->timer);
  304. ha->timer.expires = jiffies + interval * HZ;
  305. ha->timer.data = (unsigned long)ha;
  306. ha->timer.function = (void (*)(unsigned long))func;
  307. add_timer(&ha->timer);
  308. ha->timer_active = 1;
  309. }
  310. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  311. {
  312. del_timer_sync(&ha->timer);
  313. ha->timer_active = 0;
  314. }
  315. /***
  316. * qla4xxx_mark_device_missing - mark a device as missing.
  317. * @ha: Pointer to host adapter structure.
  318. * @ddb_entry: Pointer to device database entry
  319. *
  320. * This routine marks a device missing and close connection.
  321. **/
  322. void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
  323. struct ddb_entry *ddb_entry)
  324. {
  325. if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
  326. atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
  327. DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
  328. ha->host_no, ddb_entry->fw_ddb_index));
  329. } else
  330. DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
  331. ddb_entry->fw_ddb_index))
  332. iscsi_block_session(ddb_entry->sess);
  333. iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
  334. }
  335. /**
  336. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  337. * @ha: Pointer to host adapter structure.
  338. *
  339. * This routine marks a device missing and resets the relogin retry count.
  340. **/
  341. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  342. {
  343. struct ddb_entry *ddb_entry, *ddbtemp;
  344. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  345. qla4xxx_mark_device_missing(ha, ddb_entry);
  346. }
  347. }
  348. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  349. struct ddb_entry *ddb_entry,
  350. struct scsi_cmnd *cmd,
  351. void (*done)(struct scsi_cmnd *))
  352. {
  353. struct srb *srb;
  354. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  355. if (!srb)
  356. return srb;
  357. kref_init(&srb->srb_ref);
  358. srb->ha = ha;
  359. srb->ddb = ddb_entry;
  360. srb->cmd = cmd;
  361. srb->flags = 0;
  362. CMD_SP(cmd) = (void *)srb;
  363. cmd->scsi_done = done;
  364. return srb;
  365. }
  366. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  367. {
  368. struct scsi_cmnd *cmd = srb->cmd;
  369. if (srb->flags & SRB_DMA_VALID) {
  370. scsi_dma_unmap(cmd);
  371. srb->flags &= ~SRB_DMA_VALID;
  372. }
  373. CMD_SP(cmd) = NULL;
  374. }
  375. void qla4xxx_srb_compl(struct kref *ref)
  376. {
  377. struct srb *srb = container_of(ref, struct srb, srb_ref);
  378. struct scsi_cmnd *cmd = srb->cmd;
  379. struct scsi_qla_host *ha = srb->ha;
  380. qla4xxx_srb_free_dma(ha, srb);
  381. mempool_free(srb, ha->srb_mempool);
  382. cmd->scsi_done(cmd);
  383. }
  384. /**
  385. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  386. * @cmd: Pointer to Linux's SCSI command structure
  387. * @done_fn: Function that the driver calls to notify the SCSI mid-layer
  388. * that the command has been processed.
  389. *
  390. * Remarks:
  391. * This routine is invoked by Linux to send a SCSI command to the driver.
  392. * The mid-level driver tries to ensure that queuecommand never gets
  393. * invoked concurrently with itself or the interrupt handler (although
  394. * the interrupt handler may call this routine as part of request-
  395. * completion handling). Unfortunely, it sometimes calls the scheduler
  396. * in interrupt context which is a big NO! NO!.
  397. **/
  398. static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
  399. void (*done)(struct scsi_cmnd *))
  400. {
  401. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  402. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  403. struct iscsi_cls_session *sess = ddb_entry->sess;
  404. struct srb *srb;
  405. int rval;
  406. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  407. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  408. cmd->result = DID_NO_CONNECT << 16;
  409. else
  410. cmd->result = DID_REQUEUE << 16;
  411. goto qc_fail_command;
  412. }
  413. if (!sess) {
  414. cmd->result = DID_IMM_RETRY << 16;
  415. goto qc_fail_command;
  416. }
  417. rval = iscsi_session_chkready(sess);
  418. if (rval) {
  419. cmd->result = rval;
  420. goto qc_fail_command;
  421. }
  422. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  423. if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
  424. cmd->result = DID_NO_CONNECT << 16;
  425. goto qc_fail_command;
  426. }
  427. return SCSI_MLQUEUE_TARGET_BUSY;
  428. }
  429. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  430. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  431. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  432. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  433. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  434. !test_bit(AF_ONLINE, &ha->flags) ||
  435. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  436. goto qc_host_busy;
  437. spin_unlock_irq(ha->host->host_lock);
  438. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
  439. if (!srb)
  440. goto qc_host_busy_lock;
  441. rval = qla4xxx_send_command_to_isp(ha, srb);
  442. if (rval != QLA_SUCCESS)
  443. goto qc_host_busy_free_sp;
  444. spin_lock_irq(ha->host->host_lock);
  445. return 0;
  446. qc_host_busy_free_sp:
  447. qla4xxx_srb_free_dma(ha, srb);
  448. mempool_free(srb, ha->srb_mempool);
  449. qc_host_busy_lock:
  450. spin_lock_irq(ha->host->host_lock);
  451. qc_host_busy:
  452. return SCSI_MLQUEUE_HOST_BUSY;
  453. qc_fail_command:
  454. done(cmd);
  455. return 0;
  456. }
  457. /**
  458. * qla4xxx_mem_free - frees memory allocated to adapter
  459. * @ha: Pointer to host adapter structure.
  460. *
  461. * Frees memory previously allocated by qla4xxx_mem_alloc
  462. **/
  463. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  464. {
  465. if (ha->queues)
  466. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  467. ha->queues_dma);
  468. ha->queues_len = 0;
  469. ha->queues = NULL;
  470. ha->queues_dma = 0;
  471. ha->request_ring = NULL;
  472. ha->request_dma = 0;
  473. ha->response_ring = NULL;
  474. ha->response_dma = 0;
  475. ha->shadow_regs = NULL;
  476. ha->shadow_regs_dma = 0;
  477. /* Free srb pool. */
  478. if (ha->srb_mempool)
  479. mempool_destroy(ha->srb_mempool);
  480. ha->srb_mempool = NULL;
  481. /* release io space registers */
  482. if (is_qla8022(ha)) {
  483. if (ha->nx_pcibase)
  484. iounmap(
  485. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  486. if (ha->nx_db_wr_ptr)
  487. iounmap(
  488. (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
  489. } else if (ha->reg)
  490. iounmap(ha->reg);
  491. pci_release_regions(ha->pdev);
  492. }
  493. /**
  494. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  495. * @ha: Pointer to host adapter structure
  496. *
  497. * Allocates DMA memory for request and response queues. Also allocates memory
  498. * for srbs.
  499. **/
  500. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  501. {
  502. unsigned long align;
  503. /* Allocate contiguous block of DMA memory for queues. */
  504. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  505. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  506. sizeof(struct shadow_regs) +
  507. MEM_ALIGN_VALUE +
  508. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  509. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  510. &ha->queues_dma, GFP_KERNEL);
  511. if (ha->queues == NULL) {
  512. ql4_printk(KERN_WARNING, ha,
  513. "Memory Allocation failed - queues.\n");
  514. goto mem_alloc_error_exit;
  515. }
  516. memset(ha->queues, 0, ha->queues_len);
  517. /*
  518. * As per RISC alignment requirements -- the bus-address must be a
  519. * multiple of the request-ring size (in bytes).
  520. */
  521. align = 0;
  522. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  523. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  524. (MEM_ALIGN_VALUE - 1));
  525. /* Update request and response queue pointers. */
  526. ha->request_dma = ha->queues_dma + align;
  527. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  528. ha->response_dma = ha->queues_dma + align +
  529. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  530. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  531. (REQUEST_QUEUE_DEPTH *
  532. QUEUE_SIZE));
  533. ha->shadow_regs_dma = ha->queues_dma + align +
  534. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  535. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  536. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  537. (REQUEST_QUEUE_DEPTH *
  538. QUEUE_SIZE) +
  539. (RESPONSE_QUEUE_DEPTH *
  540. QUEUE_SIZE));
  541. /* Allocate memory for srb pool. */
  542. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  543. mempool_free_slab, srb_cachep);
  544. if (ha->srb_mempool == NULL) {
  545. ql4_printk(KERN_WARNING, ha,
  546. "Memory Allocation failed - SRB Pool.\n");
  547. goto mem_alloc_error_exit;
  548. }
  549. return QLA_SUCCESS;
  550. mem_alloc_error_exit:
  551. qla4xxx_mem_free(ha);
  552. return QLA_ERROR;
  553. }
  554. /**
  555. * qla4_8xxx_check_fw_alive - Check firmware health
  556. * @ha: Pointer to host adapter structure.
  557. *
  558. * Context: Interrupt
  559. **/
  560. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  561. {
  562. uint32_t fw_heartbeat_counter, halt_status;
  563. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  564. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  565. if (fw_heartbeat_counter == 0xffffffff) {
  566. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  567. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  568. ha->host_no, __func__));
  569. return;
  570. }
  571. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  572. ha->seconds_since_last_heartbeat++;
  573. /* FW not alive after 2 seconds */
  574. if (ha->seconds_since_last_heartbeat == 2) {
  575. ha->seconds_since_last_heartbeat = 0;
  576. halt_status = qla4_8xxx_rd_32(ha,
  577. QLA82XX_PEG_HALT_STATUS1);
  578. /* Since we cannot change dev_state in interrupt
  579. * context, set appropriate DPC flag then wakeup
  580. * DPC */
  581. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  582. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  583. else {
  584. printk("scsi%ld: %s: detect abort needed!\n",
  585. ha->host_no, __func__);
  586. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  587. }
  588. qla4xxx_wake_dpc(ha);
  589. qla4xxx_mailbox_premature_completion(ha);
  590. }
  591. } else
  592. ha->seconds_since_last_heartbeat = 0;
  593. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  594. }
  595. /**
  596. * qla4_8xxx_watchdog - Poll dev state
  597. * @ha: Pointer to host adapter structure.
  598. *
  599. * Context: Interrupt
  600. **/
  601. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  602. {
  603. uint32_t dev_state;
  604. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  605. /* don't poll if reset is going on */
  606. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
  607. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  608. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  609. printk("scsi%ld: %s: HW State: NEED RESET!\n",
  610. ha->host_no, __func__);
  611. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  612. qla4xxx_wake_dpc(ha);
  613. qla4xxx_mailbox_premature_completion(ha);
  614. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  615. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  616. printk("scsi%ld: %s: HW State: NEED QUIES!\n",
  617. ha->host_no, __func__);
  618. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  619. qla4xxx_wake_dpc(ha);
  620. } else {
  621. /* Check firmware health */
  622. qla4_8xxx_check_fw_alive(ha);
  623. }
  624. }
  625. }
  626. /**
  627. * qla4xxx_timer - checks every second for work to do.
  628. * @ha: Pointer to host adapter structure.
  629. **/
  630. static void qla4xxx_timer(struct scsi_qla_host *ha)
  631. {
  632. struct ddb_entry *ddb_entry, *dtemp;
  633. int start_dpc = 0;
  634. uint16_t w;
  635. /* If we are in the middle of AER/EEH processing
  636. * skip any processing and reschedule the timer
  637. */
  638. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  639. mod_timer(&ha->timer, jiffies + HZ);
  640. return;
  641. }
  642. /* Hardware read to trigger an EEH error during mailbox waits. */
  643. if (!pci_channel_offline(ha->pdev))
  644. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  645. if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
  646. DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
  647. __func__));
  648. return;
  649. }
  650. if (is_qla8022(ha)) {
  651. qla4_8xxx_watchdog(ha);
  652. }
  653. /* Search for relogin's to time-out and port down retry. */
  654. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  655. /* Count down time between sending relogins */
  656. if (adapter_up(ha) &&
  657. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  658. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  659. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  660. INVALID_ENTRY) {
  661. if (atomic_read(&ddb_entry->retry_relogin_timer)
  662. == 0) {
  663. atomic_set(&ddb_entry->
  664. retry_relogin_timer,
  665. INVALID_ENTRY);
  666. set_bit(DPC_RELOGIN_DEVICE,
  667. &ha->dpc_flags);
  668. set_bit(DF_RELOGIN, &ddb_entry->flags);
  669. DEBUG2(printk("scsi%ld: %s: ddb [%d]"
  670. " login device\n",
  671. ha->host_no, __func__,
  672. ddb_entry->fw_ddb_index));
  673. } else
  674. atomic_dec(&ddb_entry->
  675. retry_relogin_timer);
  676. }
  677. }
  678. /* Wait for relogin to timeout */
  679. if (atomic_read(&ddb_entry->relogin_timer) &&
  680. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  681. /*
  682. * If the relogin times out and the device is
  683. * still NOT ONLINE then try and relogin again.
  684. */
  685. if (atomic_read(&ddb_entry->state) !=
  686. DDB_STATE_ONLINE &&
  687. ddb_entry->fw_ddb_device_state ==
  688. DDB_DS_SESSION_FAILED) {
  689. /* Reset retry relogin timer */
  690. atomic_inc(&ddb_entry->relogin_retry_count);
  691. DEBUG2(printk("scsi%ld: ddb [%d] relogin"
  692. " timed out-retrying"
  693. " relogin (%d)\n",
  694. ha->host_no,
  695. ddb_entry->fw_ddb_index,
  696. atomic_read(&ddb_entry->
  697. relogin_retry_count))
  698. );
  699. start_dpc++;
  700. DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
  701. "initate relogin after"
  702. " %d seconds\n",
  703. ha->host_no, ddb_entry->bus,
  704. ddb_entry->target,
  705. ddb_entry->fw_ddb_index,
  706. ddb_entry->default_time2wait + 4)
  707. );
  708. atomic_set(&ddb_entry->retry_relogin_timer,
  709. ddb_entry->default_time2wait + 4);
  710. }
  711. }
  712. }
  713. if (!is_qla8022(ha)) {
  714. /* Check for heartbeat interval. */
  715. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  716. ha->heartbeat_interval != 0) {
  717. ha->seconds_since_last_heartbeat++;
  718. if (ha->seconds_since_last_heartbeat >
  719. ha->heartbeat_interval + 2)
  720. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  721. }
  722. }
  723. /* Wakeup the dpc routine for this adapter, if needed. */
  724. if ((start_dpc ||
  725. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  726. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  727. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  728. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  729. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  730. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  731. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  732. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  733. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  734. test_bit(DPC_AEN, &ha->dpc_flags)) &&
  735. !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
  736. ha->dpc_thread) {
  737. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  738. " - dpc flags = 0x%lx\n",
  739. ha->host_no, __func__, ha->dpc_flags));
  740. qla4xxx_wake_dpc(ha);
  741. }
  742. /* Reschedule timer thread to call us back in one second */
  743. mod_timer(&ha->timer, jiffies + HZ);
  744. DEBUG2(ha->seconds_since_last_intr++);
  745. }
  746. /**
  747. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  748. * @ha: Pointer to host adapter structure.
  749. *
  750. * This routine stalls the driver until all outstanding commands are returned.
  751. * Caller must release the Hardware Lock prior to calling this routine.
  752. **/
  753. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  754. {
  755. uint32_t index = 0;
  756. unsigned long flags;
  757. struct scsi_cmnd *cmd;
  758. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  759. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  760. "complete\n", WAIT_CMD_TOV));
  761. while (!time_after_eq(jiffies, wtime)) {
  762. spin_lock_irqsave(&ha->hardware_lock, flags);
  763. /* Find a command that hasn't completed. */
  764. for (index = 0; index < ha->host->can_queue; index++) {
  765. cmd = scsi_host_find_tag(ha->host, index);
  766. if (cmd != NULL)
  767. break;
  768. }
  769. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  770. /* If No Commands are pending, wait is complete */
  771. if (index == ha->host->can_queue)
  772. return QLA_SUCCESS;
  773. msleep(1000);
  774. }
  775. /* If we timed out on waiting for commands to come back
  776. * return ERROR. */
  777. return QLA_ERROR;
  778. }
  779. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  780. {
  781. uint32_t ctrl_status;
  782. unsigned long flags = 0;
  783. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  784. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  785. return QLA_ERROR;
  786. spin_lock_irqsave(&ha->hardware_lock, flags);
  787. /*
  788. * If the SCSI Reset Interrupt bit is set, clear it.
  789. * Otherwise, the Soft Reset won't work.
  790. */
  791. ctrl_status = readw(&ha->reg->ctrl_status);
  792. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  793. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  794. /* Issue Soft Reset */
  795. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  796. readl(&ha->reg->ctrl_status);
  797. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  798. return QLA_SUCCESS;
  799. }
  800. /**
  801. * qla4xxx_soft_reset - performs soft reset.
  802. * @ha: Pointer to host adapter structure.
  803. **/
  804. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  805. {
  806. uint32_t max_wait_time;
  807. unsigned long flags = 0;
  808. int status;
  809. uint32_t ctrl_status;
  810. status = qla4xxx_hw_reset(ha);
  811. if (status != QLA_SUCCESS)
  812. return status;
  813. status = QLA_ERROR;
  814. /* Wait until the Network Reset Intr bit is cleared */
  815. max_wait_time = RESET_INTR_TOV;
  816. do {
  817. spin_lock_irqsave(&ha->hardware_lock, flags);
  818. ctrl_status = readw(&ha->reg->ctrl_status);
  819. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  820. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  821. break;
  822. msleep(1000);
  823. } while ((--max_wait_time));
  824. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  825. DEBUG2(printk(KERN_WARNING
  826. "scsi%ld: Network Reset Intr not cleared by "
  827. "Network function, clearing it now!\n",
  828. ha->host_no));
  829. spin_lock_irqsave(&ha->hardware_lock, flags);
  830. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  831. readl(&ha->reg->ctrl_status);
  832. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  833. }
  834. /* Wait until the firmware tells us the Soft Reset is done */
  835. max_wait_time = SOFT_RESET_TOV;
  836. do {
  837. spin_lock_irqsave(&ha->hardware_lock, flags);
  838. ctrl_status = readw(&ha->reg->ctrl_status);
  839. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  840. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  841. status = QLA_SUCCESS;
  842. break;
  843. }
  844. msleep(1000);
  845. } while ((--max_wait_time));
  846. /*
  847. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  848. * after the soft reset has taken place.
  849. */
  850. spin_lock_irqsave(&ha->hardware_lock, flags);
  851. ctrl_status = readw(&ha->reg->ctrl_status);
  852. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  853. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  854. readl(&ha->reg->ctrl_status);
  855. }
  856. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  857. /* If soft reset fails then most probably the bios on other
  858. * function is also enabled.
  859. * Since the initialization is sequential the other fn
  860. * wont be able to acknowledge the soft reset.
  861. * Issue a force soft reset to workaround this scenario.
  862. */
  863. if (max_wait_time == 0) {
  864. /* Issue Force Soft Reset */
  865. spin_lock_irqsave(&ha->hardware_lock, flags);
  866. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  867. readl(&ha->reg->ctrl_status);
  868. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  869. /* Wait until the firmware tells us the Soft Reset is done */
  870. max_wait_time = SOFT_RESET_TOV;
  871. do {
  872. spin_lock_irqsave(&ha->hardware_lock, flags);
  873. ctrl_status = readw(&ha->reg->ctrl_status);
  874. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  875. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  876. status = QLA_SUCCESS;
  877. break;
  878. }
  879. msleep(1000);
  880. } while ((--max_wait_time));
  881. }
  882. return status;
  883. }
  884. /**
  885. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  886. * @ha: Pointer to host adapter structure.
  887. * @res: returned scsi status
  888. *
  889. * This routine is called just prior to a HARD RESET to return all
  890. * outstanding commands back to the Operating System.
  891. * Caller should make sure that the following locks are released
  892. * before this calling routine: Hardware lock, and io_request_lock.
  893. **/
  894. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  895. {
  896. struct srb *srb;
  897. int i;
  898. unsigned long flags;
  899. spin_lock_irqsave(&ha->hardware_lock, flags);
  900. for (i = 0; i < ha->host->can_queue; i++) {
  901. srb = qla4xxx_del_from_active_array(ha, i);
  902. if (srb != NULL) {
  903. srb->cmd->result = res;
  904. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  905. }
  906. }
  907. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  908. }
  909. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  910. {
  911. clear_bit(AF_ONLINE, &ha->flags);
  912. /* Disable the board */
  913. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  914. set_bit(AF_HBA_GOING_AWAY, &ha->flags);
  915. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  916. qla4xxx_mark_all_devices_missing(ha);
  917. clear_bit(AF_INIT_DONE, &ha->flags);
  918. }
  919. /**
  920. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  921. * @ha: Pointer to host adapter structure.
  922. **/
  923. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  924. {
  925. int status = QLA_ERROR;
  926. uint8_t reset_chip = 0;
  927. /* Stall incoming I/O until we are done */
  928. scsi_block_requests(ha->host);
  929. clear_bit(AF_ONLINE, &ha->flags);
  930. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  931. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  932. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  933. reset_chip = 1;
  934. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  935. * do not reset adapter, jump to initialize_adapter */
  936. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  937. status = QLA_SUCCESS;
  938. goto recover_ha_init_adapter;
  939. }
  940. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  941. * from eh_host_reset or ioctl module */
  942. if (is_qla8022(ha) && !reset_chip &&
  943. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  944. DEBUG2(ql4_printk(KERN_INFO, ha,
  945. "scsi%ld: %s - Performing stop_firmware...\n",
  946. ha->host_no, __func__));
  947. status = ha->isp_ops->reset_firmware(ha);
  948. if (status == QLA_SUCCESS) {
  949. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  950. qla4xxx_cmd_wait(ha);
  951. ha->isp_ops->disable_intrs(ha);
  952. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  953. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  954. } else {
  955. /* If the stop_firmware fails then
  956. * reset the entire chip */
  957. reset_chip = 1;
  958. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  959. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  960. }
  961. }
  962. /* Issue full chip reset if recovering from a catastrophic error,
  963. * or if stop_firmware fails for ISP-82xx.
  964. * This is the default case for ISP-4xxx */
  965. if (!is_qla8022(ha) || reset_chip) {
  966. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  967. qla4xxx_cmd_wait(ha);
  968. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  969. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  970. DEBUG2(ql4_printk(KERN_INFO, ha,
  971. "scsi%ld: %s - Performing chip reset..\n",
  972. ha->host_no, __func__));
  973. status = ha->isp_ops->reset_chip(ha);
  974. }
  975. /* Flush any pending ddb changed AENs */
  976. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  977. recover_ha_init_adapter:
  978. /* Upon successful firmware/chip reset, re-initialize the adapter */
  979. if (status == QLA_SUCCESS) {
  980. /* For ISP-4xxx, force function 1 to always initialize
  981. * before function 3 to prevent both funcions from
  982. * stepping on top of the other */
  983. if (!is_qla8022(ha) && (ha->mac_index == 3))
  984. ssleep(6);
  985. /* NOTE: AF_ONLINE flag set upon successful completion of
  986. * qla4xxx_initialize_adapter */
  987. status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  988. }
  989. /* Retry failed adapter initialization, if necessary
  990. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  991. * case to prevent ping-pong resets between functions */
  992. if (!test_bit(AF_ONLINE, &ha->flags) &&
  993. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  994. /* Adapter initialization failed, see if we can retry
  995. * resetting the ha.
  996. * Since we don't want to block the DPC for too long
  997. * with multiple resets in the same thread,
  998. * utilize DPC to retry */
  999. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  1000. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  1001. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  1002. "(%d) more times\n", ha->host_no,
  1003. ha->retry_reset_ha_cnt));
  1004. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1005. status = QLA_ERROR;
  1006. } else {
  1007. if (ha->retry_reset_ha_cnt > 0) {
  1008. /* Schedule another Reset HA--DPC will retry */
  1009. ha->retry_reset_ha_cnt--;
  1010. DEBUG2(printk("scsi%ld: recover adapter - "
  1011. "retry remaining %d\n",
  1012. ha->host_no,
  1013. ha->retry_reset_ha_cnt));
  1014. status = QLA_ERROR;
  1015. }
  1016. if (ha->retry_reset_ha_cnt == 0) {
  1017. /* Recover adapter retries have been exhausted.
  1018. * Adapter DEAD */
  1019. DEBUG2(printk("scsi%ld: recover adapter "
  1020. "failed - board disabled\n",
  1021. ha->host_no));
  1022. qla4xxx_dead_adapter_cleanup(ha);
  1023. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1024. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1025. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1026. &ha->dpc_flags);
  1027. status = QLA_ERROR;
  1028. }
  1029. }
  1030. } else {
  1031. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1032. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1033. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1034. }
  1035. ha->adapter_error_count++;
  1036. if (test_bit(AF_ONLINE, &ha->flags))
  1037. ha->isp_ops->enable_intrs(ha);
  1038. scsi_unblock_requests(ha->host);
  1039. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1040. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1041. status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
  1042. return status;
  1043. }
  1044. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1045. {
  1046. if (ha->dpc_thread &&
  1047. !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
  1048. set_bit(AF_DPC_SCHEDULED, &ha->flags);
  1049. queue_work(ha->dpc_thread, &ha->dpc_work);
  1050. }
  1051. }
  1052. /**
  1053. * qla4xxx_do_dpc - dpc routine
  1054. * @data: in our case pointer to adapter structure
  1055. *
  1056. * This routine is a task that is schedule by the interrupt handler
  1057. * to perform the background processing for interrupts. We put it
  1058. * on a task queue that is consumed whenever the scheduler runs; that's
  1059. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1060. * the mid-level tries to sleep when it reaches the driver threshold
  1061. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1062. **/
  1063. static void qla4xxx_do_dpc(struct work_struct *work)
  1064. {
  1065. struct scsi_qla_host *ha =
  1066. container_of(work, struct scsi_qla_host, dpc_work);
  1067. struct ddb_entry *ddb_entry, *dtemp;
  1068. int status = QLA_ERROR;
  1069. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1070. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1071. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1072. /* Initialization not yet finished. Don't do anything yet. */
  1073. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1074. goto do_dpc_exit;
  1075. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1076. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1077. ha->host_no, __func__, ha->flags));
  1078. goto do_dpc_exit;
  1079. }
  1080. /* HBA is in the process of being permanently disabled.
  1081. * Don't process anything */
  1082. if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
  1083. return;
  1084. if (is_qla8022(ha)) {
  1085. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1086. qla4_8xxx_idc_lock(ha);
  1087. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1088. QLA82XX_DEV_FAILED);
  1089. qla4_8xxx_idc_unlock(ha);
  1090. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1091. qla4_8xxx_device_state_handler(ha);
  1092. }
  1093. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1094. qla4_8xxx_need_qsnt_handler(ha);
  1095. }
  1096. }
  1097. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1098. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1099. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1100. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1101. if (ql4xdontresethba) {
  1102. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1103. ha->host_no, __func__));
  1104. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1105. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1106. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1107. goto dpc_post_reset_ha;
  1108. }
  1109. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1110. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1111. qla4xxx_recover_adapter(ha);
  1112. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1113. uint8_t wait_time = RESET_INTR_TOV;
  1114. while ((readw(&ha->reg->ctrl_status) &
  1115. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  1116. if (--wait_time == 0)
  1117. break;
  1118. msleep(1000);
  1119. }
  1120. if (wait_time == 0)
  1121. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  1122. "bit not cleared-- resetting\n",
  1123. ha->host_no, __func__));
  1124. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1125. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  1126. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1127. status = qla4xxx_recover_adapter(ha);
  1128. }
  1129. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1130. if (status == QLA_SUCCESS)
  1131. ha->isp_ops->enable_intrs(ha);
  1132. }
  1133. }
  1134. dpc_post_reset_ha:
  1135. /* ---- process AEN? --- */
  1136. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  1137. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  1138. /* ---- Get DHCP IP Address? --- */
  1139. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  1140. qla4xxx_get_dhcp_ip_address(ha);
  1141. /* ---- link change? --- */
  1142. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  1143. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  1144. /* ---- link down? --- */
  1145. list_for_each_entry_safe(ddb_entry, dtemp,
  1146. &ha->ddb_list, list) {
  1147. if (atomic_read(&ddb_entry->state) ==
  1148. DDB_STATE_ONLINE)
  1149. qla4xxx_mark_device_missing(ha,
  1150. ddb_entry);
  1151. }
  1152. } else {
  1153. /* ---- link up? --- *
  1154. * F/W will auto login to all devices ONLY ONCE after
  1155. * link up during driver initialization and runtime
  1156. * fatal error recovery. Therefore, the driver must
  1157. * manually relogin to devices when recovering from
  1158. * connection failures, logouts, expired KATO, etc. */
  1159. list_for_each_entry_safe(ddb_entry, dtemp,
  1160. &ha->ddb_list, list) {
  1161. if ((atomic_read(&ddb_entry->state) ==
  1162. DDB_STATE_MISSING) ||
  1163. (atomic_read(&ddb_entry->state) ==
  1164. DDB_STATE_DEAD)) {
  1165. if (ddb_entry->fw_ddb_device_state ==
  1166. DDB_DS_SESSION_ACTIVE) {
  1167. atomic_set(&ddb_entry->state,
  1168. DDB_STATE_ONLINE);
  1169. ql4_printk(KERN_INFO, ha,
  1170. "scsi%ld: %s: ddb[%d]"
  1171. " marked ONLINE\n",
  1172. ha->host_no, __func__,
  1173. ddb_entry->fw_ddb_index);
  1174. iscsi_unblock_session(
  1175. ddb_entry->sess);
  1176. } else
  1177. qla4xxx_relogin_device(
  1178. ha, ddb_entry);
  1179. }
  1180. }
  1181. }
  1182. }
  1183. /* ---- relogin device? --- */
  1184. if (adapter_up(ha) &&
  1185. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  1186. list_for_each_entry_safe(ddb_entry, dtemp,
  1187. &ha->ddb_list, list) {
  1188. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1189. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  1190. qla4xxx_relogin_device(ha, ddb_entry);
  1191. /*
  1192. * If mbx cmd times out there is no point
  1193. * in continuing further.
  1194. * With large no of targets this can hang
  1195. * the system.
  1196. */
  1197. if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1198. printk(KERN_WARNING "scsi%ld: %s: "
  1199. "need to reset hba\n",
  1200. ha->host_no, __func__);
  1201. break;
  1202. }
  1203. }
  1204. }
  1205. do_dpc_exit:
  1206. clear_bit(AF_DPC_SCHEDULED, &ha->flags);
  1207. }
  1208. /**
  1209. * qla4xxx_free_adapter - release the adapter
  1210. * @ha: pointer to adapter structure
  1211. **/
  1212. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  1213. {
  1214. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  1215. /* Turn-off interrupts on the card. */
  1216. ha->isp_ops->disable_intrs(ha);
  1217. }
  1218. /* Remove timer thread, if present */
  1219. if (ha->timer_active)
  1220. qla4xxx_stop_timer(ha);
  1221. /* Kill the kernel thread for this host */
  1222. if (ha->dpc_thread)
  1223. destroy_workqueue(ha->dpc_thread);
  1224. /* Put firmware in known state */
  1225. ha->isp_ops->reset_firmware(ha);
  1226. if (is_qla8022(ha)) {
  1227. qla4_8xxx_idc_lock(ha);
  1228. qla4_8xxx_clear_drv_active(ha);
  1229. qla4_8xxx_idc_unlock(ha);
  1230. }
  1231. /* Detach interrupts */
  1232. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  1233. qla4xxx_free_irqs(ha);
  1234. /* free extra memory */
  1235. qla4xxx_mem_free(ha);
  1236. }
  1237. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  1238. {
  1239. int status = 0;
  1240. uint8_t revision_id;
  1241. unsigned long mem_base, mem_len, db_base, db_len;
  1242. struct pci_dev *pdev = ha->pdev;
  1243. status = pci_request_regions(pdev, DRIVER_NAME);
  1244. if (status) {
  1245. printk(KERN_WARNING
  1246. "scsi(%ld) Failed to reserve PIO regions (%s) "
  1247. "status=%d\n", ha->host_no, pci_name(pdev), status);
  1248. goto iospace_error_exit;
  1249. }
  1250. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  1251. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  1252. __func__, revision_id));
  1253. ha->revision_id = revision_id;
  1254. /* remap phys address */
  1255. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  1256. mem_len = pci_resource_len(pdev, 0);
  1257. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  1258. __func__, mem_base, mem_len));
  1259. /* mapping of pcibase pointer */
  1260. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  1261. if (!ha->nx_pcibase) {
  1262. printk(KERN_ERR
  1263. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  1264. pci_release_regions(ha->pdev);
  1265. goto iospace_error_exit;
  1266. }
  1267. /* Mapping of IO base pointer, door bell read and write pointer */
  1268. /* mapping of IO base pointer */
  1269. ha->qla4_8xxx_reg =
  1270. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  1271. 0xbc000 + (ha->pdev->devfn << 11));
  1272. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  1273. db_len = pci_resource_len(pdev, 4);
  1274. /* mapping of doorbell write pointer */
  1275. ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
  1276. (ha->pdev->devfn << 12), 4);
  1277. if (!ha->nx_db_wr_ptr) {
  1278. printk(KERN_ERR
  1279. "cannot remap MMIO doorbell-write (%s), aborting\n",
  1280. pci_name(pdev));
  1281. goto iospace_error_exit;
  1282. }
  1283. /* mapping of doorbell read pointer */
  1284. ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
  1285. (ha->pdev->devfn * 8);
  1286. if (!ha->nx_db_rd_ptr)
  1287. printk(KERN_ERR
  1288. "cannot remap MMIO doorbell-read (%s), aborting\n",
  1289. pci_name(pdev));
  1290. return 0;
  1291. iospace_error_exit:
  1292. return -ENOMEM;
  1293. }
  1294. /***
  1295. * qla4xxx_iospace_config - maps registers
  1296. * @ha: pointer to adapter structure
  1297. *
  1298. * This routines maps HBA's registers from the pci address space
  1299. * into the kernel virtual address space for memory mapped i/o.
  1300. **/
  1301. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  1302. {
  1303. unsigned long pio, pio_len, pio_flags;
  1304. unsigned long mmio, mmio_len, mmio_flags;
  1305. pio = pci_resource_start(ha->pdev, 0);
  1306. pio_len = pci_resource_len(ha->pdev, 0);
  1307. pio_flags = pci_resource_flags(ha->pdev, 0);
  1308. if (pio_flags & IORESOURCE_IO) {
  1309. if (pio_len < MIN_IOBASE_LEN) {
  1310. ql4_printk(KERN_WARNING, ha,
  1311. "Invalid PCI I/O region size\n");
  1312. pio = 0;
  1313. }
  1314. } else {
  1315. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  1316. pio = 0;
  1317. }
  1318. /* Use MMIO operations for all accesses. */
  1319. mmio = pci_resource_start(ha->pdev, 1);
  1320. mmio_len = pci_resource_len(ha->pdev, 1);
  1321. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1322. if (!(mmio_flags & IORESOURCE_MEM)) {
  1323. ql4_printk(KERN_ERR, ha,
  1324. "region #0 not an MMIO resource, aborting\n");
  1325. goto iospace_error_exit;
  1326. }
  1327. if (mmio_len < MIN_IOBASE_LEN) {
  1328. ql4_printk(KERN_ERR, ha,
  1329. "Invalid PCI mem region size, aborting\n");
  1330. goto iospace_error_exit;
  1331. }
  1332. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  1333. ql4_printk(KERN_WARNING, ha,
  1334. "Failed to reserve PIO/MMIO regions\n");
  1335. goto iospace_error_exit;
  1336. }
  1337. ha->pio_address = pio;
  1338. ha->pio_length = pio_len;
  1339. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  1340. if (!ha->reg) {
  1341. ql4_printk(KERN_ERR, ha,
  1342. "cannot remap MMIO, aborting\n");
  1343. goto iospace_error_exit;
  1344. }
  1345. return 0;
  1346. iospace_error_exit:
  1347. return -ENOMEM;
  1348. }
  1349. static struct isp_operations qla4xxx_isp_ops = {
  1350. .iospace_config = qla4xxx_iospace_config,
  1351. .pci_config = qla4xxx_pci_config,
  1352. .disable_intrs = qla4xxx_disable_intrs,
  1353. .enable_intrs = qla4xxx_enable_intrs,
  1354. .start_firmware = qla4xxx_start_firmware,
  1355. .intr_handler = qla4xxx_intr_handler,
  1356. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  1357. .reset_chip = qla4xxx_soft_reset,
  1358. .reset_firmware = qla4xxx_hw_reset,
  1359. .queue_iocb = qla4xxx_queue_iocb,
  1360. .complete_iocb = qla4xxx_complete_iocb,
  1361. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  1362. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  1363. .get_sys_info = qla4xxx_get_sys_info,
  1364. };
  1365. static struct isp_operations qla4_8xxx_isp_ops = {
  1366. .iospace_config = qla4_8xxx_iospace_config,
  1367. .pci_config = qla4_8xxx_pci_config,
  1368. .disable_intrs = qla4_8xxx_disable_intrs,
  1369. .enable_intrs = qla4_8xxx_enable_intrs,
  1370. .start_firmware = qla4_8xxx_load_risc,
  1371. .intr_handler = qla4_8xxx_intr_handler,
  1372. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  1373. .reset_chip = qla4_8xxx_isp_reset,
  1374. .reset_firmware = qla4_8xxx_stop_firmware,
  1375. .queue_iocb = qla4_8xxx_queue_iocb,
  1376. .complete_iocb = qla4_8xxx_complete_iocb,
  1377. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  1378. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  1379. .get_sys_info = qla4_8xxx_get_sys_info,
  1380. };
  1381. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1382. {
  1383. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  1384. }
  1385. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1386. {
  1387. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  1388. }
  1389. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1390. {
  1391. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  1392. }
  1393. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1394. {
  1395. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  1396. }
  1397. /**
  1398. * qla4xxx_probe_adapter - callback function to probe HBA
  1399. * @pdev: pointer to pci_dev structure
  1400. * @pci_device_id: pointer to pci_device entry
  1401. *
  1402. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  1403. * It returns zero if successful. It also initializes all data necessary for
  1404. * the driver.
  1405. **/
  1406. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  1407. const struct pci_device_id *ent)
  1408. {
  1409. int ret = -ENODEV, status;
  1410. struct Scsi_Host *host;
  1411. struct scsi_qla_host *ha;
  1412. uint8_t init_retry_count = 0;
  1413. char buf[34];
  1414. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  1415. if (pci_enable_device(pdev))
  1416. return -1;
  1417. host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
  1418. if (host == NULL) {
  1419. printk(KERN_WARNING
  1420. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  1421. goto probe_disable_device;
  1422. }
  1423. /* Clear our data area */
  1424. ha = (struct scsi_qla_host *) host->hostdata;
  1425. memset(ha, 0, sizeof(*ha));
  1426. /* Save the information from PCI BIOS. */
  1427. ha->pdev = pdev;
  1428. ha->host = host;
  1429. ha->host_no = host->host_no;
  1430. pci_enable_pcie_error_reporting(pdev);
  1431. /* Setup Runtime configurable options */
  1432. if (is_qla8022(ha)) {
  1433. ha->isp_ops = &qla4_8xxx_isp_ops;
  1434. rwlock_init(&ha->hw_lock);
  1435. ha->qdr_sn_window = -1;
  1436. ha->ddr_mn_window = -1;
  1437. ha->curr_window = 255;
  1438. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  1439. nx_legacy_intr = &legacy_intr[ha->func_num];
  1440. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  1441. ha->nx_legacy_intr.tgt_status_reg =
  1442. nx_legacy_intr->tgt_status_reg;
  1443. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  1444. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  1445. } else {
  1446. ha->isp_ops = &qla4xxx_isp_ops;
  1447. }
  1448. /* Set EEH reset type to fundamental if required by hba */
  1449. if (is_qla8022(ha))
  1450. pdev->needs_freset = 1;
  1451. /* Configure PCI I/O space. */
  1452. ret = ha->isp_ops->iospace_config(ha);
  1453. if (ret)
  1454. goto probe_failed_ioconfig;
  1455. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  1456. pdev->device, pdev->irq, ha->reg);
  1457. qla4xxx_config_dma_addressing(ha);
  1458. /* Initialize lists and spinlocks. */
  1459. INIT_LIST_HEAD(&ha->ddb_list);
  1460. INIT_LIST_HEAD(&ha->free_srb_q);
  1461. mutex_init(&ha->mbox_sem);
  1462. init_completion(&ha->mbx_intr_comp);
  1463. spin_lock_init(&ha->hardware_lock);
  1464. /* Allocate dma buffers */
  1465. if (qla4xxx_mem_alloc(ha)) {
  1466. ql4_printk(KERN_WARNING, ha,
  1467. "[ERROR] Failed to allocate memory for adapter\n");
  1468. ret = -ENOMEM;
  1469. goto probe_failed;
  1470. }
  1471. if (is_qla8022(ha))
  1472. (void) qla4_8xxx_get_flash_info(ha);
  1473. /*
  1474. * Initialize the Host adapter request/response queues and
  1475. * firmware
  1476. * NOTE: interrupts enabled upon successful completion
  1477. */
  1478. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1479. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  1480. init_retry_count++ < MAX_INIT_RETRIES) {
  1481. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  1482. "(%d)\n", __func__, init_retry_count));
  1483. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  1484. continue;
  1485. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1486. }
  1487. if (!test_bit(AF_ONLINE, &ha->flags)) {
  1488. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  1489. ret = -ENODEV;
  1490. goto probe_failed;
  1491. }
  1492. host->cmd_per_lun = 3;
  1493. host->max_channel = 0;
  1494. host->max_lun = MAX_LUNS - 1;
  1495. host->max_id = MAX_TARGETS;
  1496. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  1497. host->can_queue = MAX_SRBS ;
  1498. host->transportt = qla4xxx_scsi_transport;
  1499. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  1500. if (ret) {
  1501. ql4_printk(KERN_WARNING, ha,
  1502. "scsi_init_shared_tag_map failed\n");
  1503. goto probe_failed;
  1504. }
  1505. /* Startup the kernel thread for this host adapter. */
  1506. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  1507. "qla4xxx_dpc\n", __func__));
  1508. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  1509. ha->dpc_thread = create_singlethread_workqueue(buf);
  1510. if (!ha->dpc_thread) {
  1511. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  1512. ret = -ENODEV;
  1513. goto probe_failed;
  1514. }
  1515. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  1516. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  1517. * (which is called indirectly by qla4xxx_initialize_adapter),
  1518. * so that irqs will be registered after crbinit but before
  1519. * mbx_intr_enable.
  1520. */
  1521. if (!is_qla8022(ha)) {
  1522. ret = qla4xxx_request_irqs(ha);
  1523. if (ret) {
  1524. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  1525. "interrupt %d already in use.\n", pdev->irq);
  1526. goto probe_failed;
  1527. }
  1528. }
  1529. pci_save_state(ha->pdev);
  1530. ha->isp_ops->enable_intrs(ha);
  1531. /* Start timer thread. */
  1532. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  1533. set_bit(AF_INIT_DONE, &ha->flags);
  1534. pci_set_drvdata(pdev, ha);
  1535. ret = scsi_add_host(host, &pdev->dev);
  1536. if (ret)
  1537. goto probe_failed;
  1538. printk(KERN_INFO
  1539. " QLogic iSCSI HBA Driver version: %s\n"
  1540. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  1541. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  1542. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  1543. ha->patch_number, ha->build_number);
  1544. scsi_scan_host(host);
  1545. return 0;
  1546. probe_failed:
  1547. qla4xxx_free_adapter(ha);
  1548. probe_failed_ioconfig:
  1549. pci_disable_pcie_error_reporting(pdev);
  1550. scsi_host_put(ha->host);
  1551. probe_disable_device:
  1552. pci_disable_device(pdev);
  1553. return ret;
  1554. }
  1555. /**
  1556. * qla4xxx_remove_adapter - calback function to remove adapter.
  1557. * @pci_dev: PCI device pointer
  1558. **/
  1559. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  1560. {
  1561. struct scsi_qla_host *ha;
  1562. ha = pci_get_drvdata(pdev);
  1563. set_bit(AF_HBA_GOING_AWAY, &ha->flags);
  1564. /* remove devs from iscsi_sessions to scsi_devices */
  1565. qla4xxx_free_ddb_list(ha);
  1566. scsi_remove_host(ha->host);
  1567. qla4xxx_free_adapter(ha);
  1568. scsi_host_put(ha->host);
  1569. pci_disable_pcie_error_reporting(pdev);
  1570. pci_disable_device(pdev);
  1571. pci_set_drvdata(pdev, NULL);
  1572. }
  1573. /**
  1574. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  1575. * @ha: HA context
  1576. *
  1577. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  1578. * supported addressing method.
  1579. */
  1580. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  1581. {
  1582. int retval;
  1583. /* Update our PCI device dma_mask for full 64 bit mask */
  1584. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  1585. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  1586. dev_dbg(&ha->pdev->dev,
  1587. "Failed to set 64 bit PCI consistent mask; "
  1588. "using 32 bit.\n");
  1589. retval = pci_set_consistent_dma_mask(ha->pdev,
  1590. DMA_BIT_MASK(32));
  1591. }
  1592. } else
  1593. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  1594. }
  1595. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  1596. {
  1597. struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
  1598. struct ddb_entry *ddb = sess->dd_data;
  1599. sdev->hostdata = ddb;
  1600. sdev->tagged_supported = 1;
  1601. scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
  1602. return 0;
  1603. }
  1604. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  1605. {
  1606. sdev->tagged_supported = 1;
  1607. return 0;
  1608. }
  1609. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  1610. {
  1611. scsi_deactivate_tcq(sdev, 1);
  1612. }
  1613. /**
  1614. * qla4xxx_del_from_active_array - returns an active srb
  1615. * @ha: Pointer to host adapter structure.
  1616. * @index: index into the active_array
  1617. *
  1618. * This routine removes and returns the srb at the specified index
  1619. **/
  1620. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  1621. uint32_t index)
  1622. {
  1623. struct srb *srb = NULL;
  1624. struct scsi_cmnd *cmd = NULL;
  1625. cmd = scsi_host_find_tag(ha->host, index);
  1626. if (!cmd)
  1627. return srb;
  1628. srb = (struct srb *)CMD_SP(cmd);
  1629. if (!srb)
  1630. return srb;
  1631. /* update counters */
  1632. if (srb->flags & SRB_DMA_VALID) {
  1633. ha->req_q_count += srb->iocb_cnt;
  1634. ha->iocb_cnt -= srb->iocb_cnt;
  1635. if (srb->cmd)
  1636. srb->cmd->host_scribble =
  1637. (unsigned char *)(unsigned long) MAX_SRBS;
  1638. }
  1639. return srb;
  1640. }
  1641. /**
  1642. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  1643. * @ha: Pointer to host adapter structure.
  1644. * @cmd: Scsi Command to wait on.
  1645. *
  1646. * This routine waits for the command to be returned by the Firmware
  1647. * for some max time.
  1648. **/
  1649. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  1650. struct scsi_cmnd *cmd)
  1651. {
  1652. int done = 0;
  1653. struct srb *rp;
  1654. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  1655. int ret = SUCCESS;
  1656. /* Dont wait on command if PCI error is being handled
  1657. * by PCI AER driver
  1658. */
  1659. if (unlikely(pci_channel_offline(ha->pdev)) ||
  1660. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  1661. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  1662. ha->host_no, __func__);
  1663. return ret;
  1664. }
  1665. do {
  1666. /* Checking to see if its returned to OS */
  1667. rp = (struct srb *) CMD_SP(cmd);
  1668. if (rp == NULL) {
  1669. done++;
  1670. break;
  1671. }
  1672. msleep(2000);
  1673. } while (max_wait_time--);
  1674. return done;
  1675. }
  1676. /**
  1677. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  1678. * @ha: Pointer to host adapter structure
  1679. **/
  1680. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  1681. {
  1682. unsigned long wait_online;
  1683. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  1684. while (time_before(jiffies, wait_online)) {
  1685. if (adapter_up(ha))
  1686. return QLA_SUCCESS;
  1687. msleep(2000);
  1688. }
  1689. return QLA_ERROR;
  1690. }
  1691. /**
  1692. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  1693. * @ha: pointer to HBA
  1694. * @t: target id
  1695. * @l: lun id
  1696. *
  1697. * This function waits for all outstanding commands to a lun to complete. It
  1698. * returns 0 if all pending commands are returned and 1 otherwise.
  1699. **/
  1700. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  1701. struct scsi_target *stgt,
  1702. struct scsi_device *sdev)
  1703. {
  1704. int cnt;
  1705. int status = 0;
  1706. struct scsi_cmnd *cmd;
  1707. /*
  1708. * Waiting for all commands for the designated target or dev
  1709. * in the active array
  1710. */
  1711. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  1712. cmd = scsi_host_find_tag(ha->host, cnt);
  1713. if (cmd && stgt == scsi_target(cmd->device) &&
  1714. (!sdev || sdev == cmd->device)) {
  1715. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1716. status++;
  1717. break;
  1718. }
  1719. }
  1720. }
  1721. return status;
  1722. }
  1723. /**
  1724. * qla4xxx_eh_abort - callback for abort task.
  1725. * @cmd: Pointer to Linux's SCSI command structure
  1726. *
  1727. * This routine is called by the Linux OS to abort the specified
  1728. * command.
  1729. **/
  1730. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  1731. {
  1732. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1733. unsigned int id = cmd->device->id;
  1734. unsigned int lun = cmd->device->lun;
  1735. unsigned long serial = cmd->serial_number;
  1736. struct srb *srb = NULL;
  1737. int ret = SUCCESS;
  1738. int wait = 0;
  1739. ql4_printk(KERN_INFO, ha,
  1740. "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
  1741. ha->host_no, id, lun, cmd, serial);
  1742. srb = (struct srb *) CMD_SP(cmd);
  1743. if (!srb)
  1744. return SUCCESS;
  1745. kref_get(&srb->srb_ref);
  1746. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  1747. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  1748. ha->host_no, id, lun));
  1749. ret = FAILED;
  1750. } else {
  1751. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  1752. ha->host_no, id, lun));
  1753. wait = 1;
  1754. }
  1755. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1756. /* Wait for command to complete */
  1757. if (wait) {
  1758. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1759. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  1760. ha->host_no, id, lun));
  1761. ret = FAILED;
  1762. }
  1763. }
  1764. ql4_printk(KERN_INFO, ha,
  1765. "scsi%ld:%d:%d: Abort command - %s\n",
  1766. ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
  1767. return ret;
  1768. }
  1769. /**
  1770. * qla4xxx_eh_device_reset - callback for target reset.
  1771. * @cmd: Pointer to Linux's SCSI command structure
  1772. *
  1773. * This routine is called by the Linux OS to reset all luns on the
  1774. * specified target.
  1775. **/
  1776. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  1777. {
  1778. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1779. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1780. int ret = FAILED, stat;
  1781. if (!ddb_entry)
  1782. return ret;
  1783. ret = iscsi_block_scsi_eh(cmd);
  1784. if (ret)
  1785. return ret;
  1786. ret = FAILED;
  1787. ql4_printk(KERN_INFO, ha,
  1788. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  1789. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1790. DEBUG2(printk(KERN_INFO
  1791. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  1792. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  1793. cmd, jiffies, cmd->request->timeout / HZ,
  1794. ha->dpc_flags, cmd->result, cmd->allowed));
  1795. /* FIXME: wait for hba to go online */
  1796. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  1797. if (stat != QLA_SUCCESS) {
  1798. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  1799. goto eh_dev_reset_done;
  1800. }
  1801. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1802. cmd->device)) {
  1803. ql4_printk(KERN_INFO, ha,
  1804. "DEVICE RESET FAILED - waiting for "
  1805. "commands.\n");
  1806. goto eh_dev_reset_done;
  1807. }
  1808. /* Send marker. */
  1809. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1810. MM_LUN_RESET) != QLA_SUCCESS)
  1811. goto eh_dev_reset_done;
  1812. ql4_printk(KERN_INFO, ha,
  1813. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  1814. ha->host_no, cmd->device->channel, cmd->device->id,
  1815. cmd->device->lun);
  1816. ret = SUCCESS;
  1817. eh_dev_reset_done:
  1818. return ret;
  1819. }
  1820. /**
  1821. * qla4xxx_eh_target_reset - callback for target reset.
  1822. * @cmd: Pointer to Linux's SCSI command structure
  1823. *
  1824. * This routine is called by the Linux OS to reset the target.
  1825. **/
  1826. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  1827. {
  1828. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1829. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1830. int stat, ret;
  1831. if (!ddb_entry)
  1832. return FAILED;
  1833. ret = iscsi_block_scsi_eh(cmd);
  1834. if (ret)
  1835. return ret;
  1836. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1837. "WARM TARGET RESET ISSUED.\n");
  1838. DEBUG2(printk(KERN_INFO
  1839. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  1840. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  1841. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  1842. ha->dpc_flags, cmd->result, cmd->allowed));
  1843. stat = qla4xxx_reset_target(ha, ddb_entry);
  1844. if (stat != QLA_SUCCESS) {
  1845. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1846. "WARM TARGET RESET FAILED.\n");
  1847. return FAILED;
  1848. }
  1849. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1850. NULL)) {
  1851. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1852. "WARM TARGET DEVICE RESET FAILED - "
  1853. "waiting for commands.\n");
  1854. return FAILED;
  1855. }
  1856. /* Send marker. */
  1857. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1858. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  1859. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1860. "WARM TARGET DEVICE RESET FAILED - "
  1861. "marker iocb failed.\n");
  1862. return FAILED;
  1863. }
  1864. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1865. "WARM TARGET RESET SUCCEEDED.\n");
  1866. return SUCCESS;
  1867. }
  1868. /**
  1869. * qla4xxx_eh_host_reset - kernel callback
  1870. * @cmd: Pointer to Linux's SCSI command structure
  1871. *
  1872. * This routine is invoked by the Linux kernel to perform fatal error
  1873. * recovery on the specified adapter.
  1874. **/
  1875. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  1876. {
  1877. int return_status = FAILED;
  1878. struct scsi_qla_host *ha;
  1879. ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
  1880. if (ql4xdontresethba) {
  1881. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1882. ha->host_no, __func__));
  1883. return FAILED;
  1884. }
  1885. ql4_printk(KERN_INFO, ha,
  1886. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  1887. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1888. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  1889. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  1890. "DEAD.\n", ha->host_no, cmd->device->channel,
  1891. __func__));
  1892. return FAILED;
  1893. }
  1894. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1895. if (is_qla8022(ha))
  1896. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1897. else
  1898. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1899. }
  1900. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  1901. return_status = SUCCESS;
  1902. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  1903. return_status == FAILED ? "FAILED" : "SUCCEDED");
  1904. return return_status;
  1905. }
  1906. /* PCI AER driver recovers from all correctable errors w/o
  1907. * driver intervention. For uncorrectable errors PCI AER
  1908. * driver calls the following device driver's callbacks
  1909. *
  1910. * - Fatal Errors - link_reset
  1911. * - Non-Fatal Errors - driver's pci_error_detected() which
  1912. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  1913. *
  1914. * PCI AER driver calls
  1915. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  1916. * returns RECOVERED or NEED_RESET if fw_hung
  1917. * NEED_RESET - driver's slot_reset()
  1918. * DISCONNECT - device is dead & cannot recover
  1919. * RECOVERED - driver's pci_resume()
  1920. */
  1921. static pci_ers_result_t
  1922. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  1923. {
  1924. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  1925. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  1926. ha->host_no, __func__, state);
  1927. if (!is_aer_supported(ha))
  1928. return PCI_ERS_RESULT_NONE;
  1929. switch (state) {
  1930. case pci_channel_io_normal:
  1931. clear_bit(AF_EEH_BUSY, &ha->flags);
  1932. return PCI_ERS_RESULT_CAN_RECOVER;
  1933. case pci_channel_io_frozen:
  1934. set_bit(AF_EEH_BUSY, &ha->flags);
  1935. qla4xxx_mailbox_premature_completion(ha);
  1936. qla4xxx_free_irqs(ha);
  1937. pci_disable_device(pdev);
  1938. return PCI_ERS_RESULT_NEED_RESET;
  1939. case pci_channel_io_perm_failure:
  1940. set_bit(AF_EEH_BUSY, &ha->flags);
  1941. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  1942. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  1943. return PCI_ERS_RESULT_DISCONNECT;
  1944. }
  1945. return PCI_ERS_RESULT_NEED_RESET;
  1946. }
  1947. /**
  1948. * qla4xxx_pci_mmio_enabled() gets called if
  1949. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  1950. * and read/write to the device still works.
  1951. **/
  1952. static pci_ers_result_t
  1953. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  1954. {
  1955. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  1956. if (!is_aer_supported(ha))
  1957. return PCI_ERS_RESULT_NONE;
  1958. if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
  1959. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang -- "
  1960. "mmio_enabled\n", ha->host_no, __func__);
  1961. return PCI_ERS_RESULT_NEED_RESET;
  1962. } else
  1963. return PCI_ERS_RESULT_RECOVERED;
  1964. }
  1965. uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  1966. {
  1967. uint32_t rval = QLA_ERROR;
  1968. int fn;
  1969. struct pci_dev *other_pdev = NULL;
  1970. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  1971. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1972. if (test_bit(AF_ONLINE, &ha->flags)) {
  1973. clear_bit(AF_ONLINE, &ha->flags);
  1974. qla4xxx_mark_all_devices_missing(ha);
  1975. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1976. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1977. }
  1978. fn = PCI_FUNC(ha->pdev->devfn);
  1979. while (fn > 0) {
  1980. fn--;
  1981. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  1982. "func %x\n", ha->host_no, __func__, fn);
  1983. /* Get the pci device given the domain, bus,
  1984. * slot/function number */
  1985. other_pdev =
  1986. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  1987. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  1988. fn));
  1989. if (!other_pdev)
  1990. continue;
  1991. if (atomic_read(&other_pdev->enable_cnt)) {
  1992. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  1993. "func in enabled state%x\n", ha->host_no,
  1994. __func__, fn);
  1995. pci_dev_put(other_pdev);
  1996. break;
  1997. }
  1998. pci_dev_put(other_pdev);
  1999. }
  2000. /* The first function on the card, the reset owner will
  2001. * start & initialize the firmware. The other functions
  2002. * on the card will reset the firmware context
  2003. */
  2004. if (!fn) {
  2005. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  2006. "0x%x is the owner\n", ha->host_no, __func__,
  2007. ha->pdev->devfn);
  2008. qla4_8xxx_idc_lock(ha);
  2009. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2010. QLA82XX_DEV_COLD);
  2011. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  2012. QLA82XX_IDC_VERSION);
  2013. qla4_8xxx_idc_unlock(ha);
  2014. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2015. rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  2016. qla4_8xxx_idc_lock(ha);
  2017. if (rval != QLA_SUCCESS) {
  2018. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2019. "FAILED\n", ha->host_no, __func__);
  2020. qla4_8xxx_clear_drv_active(ha);
  2021. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2022. QLA82XX_DEV_FAILED);
  2023. } else {
  2024. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2025. "READY\n", ha->host_no, __func__);
  2026. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2027. QLA82XX_DEV_READY);
  2028. /* Clear driver state register */
  2029. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  2030. qla4_8xxx_set_drv_active(ha);
  2031. ha->isp_ops->enable_intrs(ha);
  2032. }
  2033. qla4_8xxx_idc_unlock(ha);
  2034. } else {
  2035. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  2036. "the reset owner\n", ha->host_no, __func__,
  2037. ha->pdev->devfn);
  2038. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  2039. QLA82XX_DEV_READY)) {
  2040. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2041. rval = qla4xxx_initialize_adapter(ha,
  2042. PRESERVE_DDB_LIST);
  2043. if (rval == QLA_SUCCESS)
  2044. ha->isp_ops->enable_intrs(ha);
  2045. qla4_8xxx_idc_lock(ha);
  2046. qla4_8xxx_set_drv_active(ha);
  2047. qla4_8xxx_idc_unlock(ha);
  2048. }
  2049. }
  2050. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2051. return rval;
  2052. }
  2053. static pci_ers_result_t
  2054. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  2055. {
  2056. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  2057. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2058. int rc;
  2059. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  2060. ha->host_no, __func__);
  2061. if (!is_aer_supported(ha))
  2062. return PCI_ERS_RESULT_NONE;
  2063. /* Restore the saved state of PCIe device -
  2064. * BAR registers, PCI Config space, PCIX, MSI,
  2065. * IOV states
  2066. */
  2067. pci_restore_state(pdev);
  2068. /* pci_restore_state() clears the saved_state flag of the device
  2069. * save restored state which resets saved_state flag
  2070. */
  2071. pci_save_state(pdev);
  2072. /* Initialize device or resume if in suspended state */
  2073. rc = pci_enable_device(pdev);
  2074. if (rc) {
  2075. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
  2076. "device after reset\n", ha->host_no, __func__);
  2077. goto exit_slot_reset;
  2078. }
  2079. ret = qla4xxx_request_irqs(ha);
  2080. if (ret) {
  2081. ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
  2082. " already in use.\n", pdev->irq);
  2083. goto exit_slot_reset;
  2084. }
  2085. if (is_qla8022(ha)) {
  2086. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  2087. ret = PCI_ERS_RESULT_RECOVERED;
  2088. goto exit_slot_reset;
  2089. } else
  2090. goto exit_slot_reset;
  2091. }
  2092. exit_slot_reset:
  2093. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  2094. "device after reset\n", ha->host_no, __func__, ret);
  2095. return ret;
  2096. }
  2097. static void
  2098. qla4xxx_pci_resume(struct pci_dev *pdev)
  2099. {
  2100. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2101. int ret;
  2102. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  2103. ha->host_no, __func__);
  2104. ret = qla4xxx_wait_for_hba_online(ha);
  2105. if (ret != QLA_SUCCESS) {
  2106. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  2107. "resume I/O from slot/link_reset\n", ha->host_no,
  2108. __func__);
  2109. }
  2110. pci_cleanup_aer_uncorrect_error_status(pdev);
  2111. clear_bit(AF_EEH_BUSY, &ha->flags);
  2112. }
  2113. static struct pci_error_handlers qla4xxx_err_handler = {
  2114. .error_detected = qla4xxx_pci_error_detected,
  2115. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  2116. .slot_reset = qla4xxx_pci_slot_reset,
  2117. .resume = qla4xxx_pci_resume,
  2118. };
  2119. static struct pci_device_id qla4xxx_pci_tbl[] = {
  2120. {
  2121. .vendor = PCI_VENDOR_ID_QLOGIC,
  2122. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  2123. .subvendor = PCI_ANY_ID,
  2124. .subdevice = PCI_ANY_ID,
  2125. },
  2126. {
  2127. .vendor = PCI_VENDOR_ID_QLOGIC,
  2128. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  2129. .subvendor = PCI_ANY_ID,
  2130. .subdevice = PCI_ANY_ID,
  2131. },
  2132. {
  2133. .vendor = PCI_VENDOR_ID_QLOGIC,
  2134. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  2135. .subvendor = PCI_ANY_ID,
  2136. .subdevice = PCI_ANY_ID,
  2137. },
  2138. {
  2139. .vendor = PCI_VENDOR_ID_QLOGIC,
  2140. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  2141. .subvendor = PCI_ANY_ID,
  2142. .subdevice = PCI_ANY_ID,
  2143. },
  2144. {0, 0},
  2145. };
  2146. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  2147. static struct pci_driver qla4xxx_pci_driver = {
  2148. .name = DRIVER_NAME,
  2149. .id_table = qla4xxx_pci_tbl,
  2150. .probe = qla4xxx_probe_adapter,
  2151. .remove = qla4xxx_remove_adapter,
  2152. .err_handler = &qla4xxx_err_handler,
  2153. };
  2154. static int __init qla4xxx_module_init(void)
  2155. {
  2156. int ret;
  2157. /* Allocate cache for SRBs. */
  2158. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  2159. SLAB_HWCACHE_ALIGN, NULL);
  2160. if (srb_cachep == NULL) {
  2161. printk(KERN_ERR
  2162. "%s: Unable to allocate SRB cache..."
  2163. "Failing load!\n", DRIVER_NAME);
  2164. ret = -ENOMEM;
  2165. goto no_srp_cache;
  2166. }
  2167. /* Derive version string. */
  2168. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  2169. if (ql4xextended_error_logging)
  2170. strcat(qla4xxx_version_str, "-debug");
  2171. qla4xxx_scsi_transport =
  2172. iscsi_register_transport(&qla4xxx_iscsi_transport);
  2173. if (!qla4xxx_scsi_transport){
  2174. ret = -ENODEV;
  2175. goto release_srb_cache;
  2176. }
  2177. ret = pci_register_driver(&qla4xxx_pci_driver);
  2178. if (ret)
  2179. goto unregister_transport;
  2180. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  2181. return 0;
  2182. unregister_transport:
  2183. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2184. release_srb_cache:
  2185. kmem_cache_destroy(srb_cachep);
  2186. no_srp_cache:
  2187. return ret;
  2188. }
  2189. static void __exit qla4xxx_module_exit(void)
  2190. {
  2191. pci_unregister_driver(&qla4xxx_pci_driver);
  2192. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2193. kmem_cache_destroy(srb_cachep);
  2194. }
  2195. module_init(qla4xxx_module_init);
  2196. module_exit(qla4xxx_module_exit);
  2197. MODULE_AUTHOR("QLogic Corporation");
  2198. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  2199. MODULE_LICENSE("GPL");
  2200. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);