qla_bsg.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/kthread.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/delay.h>
  11. /* BSG support for ELS/CT pass through */
  12. inline srb_t *
  13. qla2x00_get_ctx_bsg_sp(scsi_qla_host_t *vha, fc_port_t *fcport, size_t size)
  14. {
  15. srb_t *sp;
  16. struct qla_hw_data *ha = vha->hw;
  17. struct srb_ctx *ctx;
  18. sp = mempool_alloc(ha->srb_mempool, GFP_KERNEL);
  19. if (!sp)
  20. goto done;
  21. ctx = kzalloc(size, GFP_KERNEL);
  22. if (!ctx) {
  23. mempool_free(sp, ha->srb_mempool);
  24. sp = NULL;
  25. goto done;
  26. }
  27. memset(sp, 0, sizeof(*sp));
  28. sp->fcport = fcport;
  29. sp->ctx = ctx;
  30. done:
  31. return sp;
  32. }
  33. int
  34. qla24xx_fcp_prio_cfg_valid(struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
  35. {
  36. int i, ret, num_valid;
  37. uint8_t *bcode;
  38. struct qla_fcp_prio_entry *pri_entry;
  39. uint32_t *bcode_val_ptr, bcode_val;
  40. ret = 1;
  41. num_valid = 0;
  42. bcode = (uint8_t *)pri_cfg;
  43. bcode_val_ptr = (uint32_t *)pri_cfg;
  44. bcode_val = (uint32_t)(*bcode_val_ptr);
  45. if (bcode_val == 0xFFFFFFFF) {
  46. /* No FCP Priority config data in flash */
  47. DEBUG2(printk(KERN_INFO
  48. "%s: No FCP priority config data.\n",
  49. __func__));
  50. return 0;
  51. }
  52. if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
  53. bcode[3] != 'S') {
  54. /* Invalid FCP priority data header*/
  55. DEBUG2(printk(KERN_ERR
  56. "%s: Invalid FCP Priority data header. bcode=0x%x\n",
  57. __func__, bcode_val));
  58. return 0;
  59. }
  60. if (flag != 1)
  61. return ret;
  62. pri_entry = &pri_cfg->entry[0];
  63. for (i = 0; i < pri_cfg->num_entries; i++) {
  64. if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
  65. num_valid++;
  66. pri_entry++;
  67. }
  68. if (num_valid == 0) {
  69. /* No valid FCP priority data entries */
  70. DEBUG2(printk(KERN_ERR
  71. "%s: No valid FCP Priority data entries.\n",
  72. __func__));
  73. ret = 0;
  74. } else {
  75. /* FCP priority data is valid */
  76. DEBUG2(printk(KERN_INFO
  77. "%s: Valid FCP priority data. num entries = %d\n",
  78. __func__, num_valid));
  79. }
  80. return ret;
  81. }
  82. static int
  83. qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
  84. {
  85. struct Scsi_Host *host = bsg_job->shost;
  86. scsi_qla_host_t *vha = shost_priv(host);
  87. struct qla_hw_data *ha = vha->hw;
  88. int ret = 0;
  89. uint32_t len;
  90. uint32_t oper;
  91. bsg_job->reply->reply_payload_rcv_len = 0;
  92. if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))) {
  93. ret = -EINVAL;
  94. goto exit_fcp_prio_cfg;
  95. }
  96. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  97. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  98. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  99. ret = -EBUSY;
  100. goto exit_fcp_prio_cfg;
  101. }
  102. /* Get the sub command */
  103. oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  104. /* Only set config is allowed if config memory is not allocated */
  105. if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
  106. ret = -EINVAL;
  107. goto exit_fcp_prio_cfg;
  108. }
  109. switch (oper) {
  110. case QLFC_FCP_PRIO_DISABLE:
  111. if (ha->flags.fcp_prio_enabled) {
  112. ha->flags.fcp_prio_enabled = 0;
  113. ha->fcp_prio_cfg->attributes &=
  114. ~FCP_PRIO_ATTR_ENABLE;
  115. qla24xx_update_all_fcp_prio(vha);
  116. bsg_job->reply->result = DID_OK;
  117. } else {
  118. ret = -EINVAL;
  119. bsg_job->reply->result = (DID_ERROR << 16);
  120. goto exit_fcp_prio_cfg;
  121. }
  122. break;
  123. case QLFC_FCP_PRIO_ENABLE:
  124. if (!ha->flags.fcp_prio_enabled) {
  125. if (ha->fcp_prio_cfg) {
  126. ha->flags.fcp_prio_enabled = 1;
  127. ha->fcp_prio_cfg->attributes |=
  128. FCP_PRIO_ATTR_ENABLE;
  129. qla24xx_update_all_fcp_prio(vha);
  130. bsg_job->reply->result = DID_OK;
  131. } else {
  132. ret = -EINVAL;
  133. bsg_job->reply->result = (DID_ERROR << 16);
  134. goto exit_fcp_prio_cfg;
  135. }
  136. }
  137. break;
  138. case QLFC_FCP_PRIO_GET_CONFIG:
  139. len = bsg_job->reply_payload.payload_len;
  140. if (!len || len > FCP_PRIO_CFG_SIZE) {
  141. ret = -EINVAL;
  142. bsg_job->reply->result = (DID_ERROR << 16);
  143. goto exit_fcp_prio_cfg;
  144. }
  145. bsg_job->reply->result = DID_OK;
  146. bsg_job->reply->reply_payload_rcv_len =
  147. sg_copy_from_buffer(
  148. bsg_job->reply_payload.sg_list,
  149. bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
  150. len);
  151. break;
  152. case QLFC_FCP_PRIO_SET_CONFIG:
  153. len = bsg_job->request_payload.payload_len;
  154. if (!len || len > FCP_PRIO_CFG_SIZE) {
  155. bsg_job->reply->result = (DID_ERROR << 16);
  156. ret = -EINVAL;
  157. goto exit_fcp_prio_cfg;
  158. }
  159. if (!ha->fcp_prio_cfg) {
  160. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  161. if (!ha->fcp_prio_cfg) {
  162. qla_printk(KERN_WARNING, ha,
  163. "Unable to allocate memory "
  164. "for fcp prio config data (%x).\n",
  165. FCP_PRIO_CFG_SIZE);
  166. bsg_job->reply->result = (DID_ERROR << 16);
  167. ret = -ENOMEM;
  168. goto exit_fcp_prio_cfg;
  169. }
  170. }
  171. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  172. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  173. bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
  174. FCP_PRIO_CFG_SIZE);
  175. /* validate fcp priority data */
  176. if (!qla24xx_fcp_prio_cfg_valid(
  177. (struct qla_fcp_prio_cfg *)
  178. ha->fcp_prio_cfg, 1)) {
  179. bsg_job->reply->result = (DID_ERROR << 16);
  180. ret = -EINVAL;
  181. /* If buffer was invalidatic int
  182. * fcp_prio_cfg is of no use
  183. */
  184. vfree(ha->fcp_prio_cfg);
  185. ha->fcp_prio_cfg = NULL;
  186. goto exit_fcp_prio_cfg;
  187. }
  188. ha->flags.fcp_prio_enabled = 0;
  189. if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
  190. ha->flags.fcp_prio_enabled = 1;
  191. qla24xx_update_all_fcp_prio(vha);
  192. bsg_job->reply->result = DID_OK;
  193. break;
  194. default:
  195. ret = -EINVAL;
  196. break;
  197. }
  198. exit_fcp_prio_cfg:
  199. bsg_job->job_done(bsg_job);
  200. return ret;
  201. }
  202. static int
  203. qla2x00_process_els(struct fc_bsg_job *bsg_job)
  204. {
  205. struct fc_rport *rport;
  206. fc_port_t *fcport = NULL;
  207. struct Scsi_Host *host;
  208. scsi_qla_host_t *vha;
  209. struct qla_hw_data *ha;
  210. srb_t *sp;
  211. const char *type;
  212. int req_sg_cnt, rsp_sg_cnt;
  213. int rval = (DRIVER_ERROR << 16);
  214. uint16_t nextlid = 0;
  215. struct srb_ctx *els;
  216. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  217. rport = bsg_job->rport;
  218. fcport = *(fc_port_t **) rport->dd_data;
  219. host = rport_to_shost(rport);
  220. vha = shost_priv(host);
  221. ha = vha->hw;
  222. type = "FC_BSG_RPT_ELS";
  223. } else {
  224. host = bsg_job->shost;
  225. vha = shost_priv(host);
  226. ha = vha->hw;
  227. type = "FC_BSG_HST_ELS_NOLOGIN";
  228. }
  229. /* pass through is supported only for ISP 4Gb or higher */
  230. if (!IS_FWI2_CAPABLE(ha)) {
  231. DEBUG2(qla_printk(KERN_INFO, ha,
  232. "scsi(%ld):ELS passthru not supported for ISP23xx based "
  233. "adapters\n", vha->host_no));
  234. rval = -EPERM;
  235. goto done;
  236. }
  237. /* Multiple SG's are not supported for ELS requests */
  238. if (bsg_job->request_payload.sg_cnt > 1 ||
  239. bsg_job->reply_payload.sg_cnt > 1) {
  240. DEBUG2(printk(KERN_INFO
  241. "multiple SG's are not supported for ELS requests"
  242. " [request_sg_cnt: %x reply_sg_cnt: %x]\n",
  243. bsg_job->request_payload.sg_cnt,
  244. bsg_job->reply_payload.sg_cnt));
  245. rval = -EPERM;
  246. goto done;
  247. }
  248. /* ELS request for rport */
  249. if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
  250. /* make sure the rport is logged in,
  251. * if not perform fabric login
  252. */
  253. if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
  254. DEBUG2(qla_printk(KERN_WARNING, ha,
  255. "failed to login port %06X for ELS passthru\n",
  256. fcport->d_id.b24));
  257. rval = -EIO;
  258. goto done;
  259. }
  260. } else {
  261. /* Allocate a dummy fcport structure, since functions
  262. * preparing the IOCB and mailbox command retrieves port
  263. * specific information from fcport structure. For Host based
  264. * ELS commands there will be no fcport structure allocated
  265. */
  266. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  267. if (!fcport) {
  268. rval = -ENOMEM;
  269. goto done;
  270. }
  271. /* Initialize all required fields of fcport */
  272. fcport->vha = vha;
  273. fcport->vp_idx = vha->vp_idx;
  274. fcport->d_id.b.al_pa =
  275. bsg_job->request->rqst_data.h_els.port_id[0];
  276. fcport->d_id.b.area =
  277. bsg_job->request->rqst_data.h_els.port_id[1];
  278. fcport->d_id.b.domain =
  279. bsg_job->request->rqst_data.h_els.port_id[2];
  280. fcport->loop_id =
  281. (fcport->d_id.b.al_pa == 0xFD) ?
  282. NPH_FABRIC_CONTROLLER : NPH_F_PORT;
  283. }
  284. if (!vha->flags.online) {
  285. DEBUG2(qla_printk(KERN_WARNING, ha,
  286. "host not online\n"));
  287. rval = -EIO;
  288. goto done;
  289. }
  290. req_sg_cnt =
  291. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  292. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  293. if (!req_sg_cnt) {
  294. rval = -ENOMEM;
  295. goto done_free_fcport;
  296. }
  297. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  298. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  299. if (!rsp_sg_cnt) {
  300. rval = -ENOMEM;
  301. goto done_free_fcport;
  302. }
  303. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  304. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  305. DEBUG2(printk(KERN_INFO
  306. "dma mapping resulted in different sg counts \
  307. [request_sg_cnt: %x dma_request_sg_cnt: %x\
  308. reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
  309. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  310. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
  311. rval = -EAGAIN;
  312. goto done_unmap_sg;
  313. }
  314. /* Alloc SRB structure */
  315. sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_ctx));
  316. if (!sp) {
  317. rval = -ENOMEM;
  318. goto done_unmap_sg;
  319. }
  320. els = sp->ctx;
  321. els->type =
  322. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  323. SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
  324. els->name =
  325. (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
  326. "bsg_els_rpt" : "bsg_els_hst");
  327. els->u.bsg_job = bsg_job;
  328. DEBUG2(qla_printk(KERN_INFO, ha,
  329. "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
  330. "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
  331. bsg_job->request->rqst_data.h_els.command_code,
  332. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  333. fcport->d_id.b.al_pa));
  334. rval = qla2x00_start_sp(sp);
  335. if (rval != QLA_SUCCESS) {
  336. kfree(sp->ctx);
  337. mempool_free(sp, ha->srb_mempool);
  338. rval = -EIO;
  339. goto done_unmap_sg;
  340. }
  341. return rval;
  342. done_unmap_sg:
  343. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  344. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  345. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  346. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  347. goto done_free_fcport;
  348. done_free_fcport:
  349. if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
  350. kfree(fcport);
  351. done:
  352. return rval;
  353. }
  354. static int
  355. qla2x00_process_ct(struct fc_bsg_job *bsg_job)
  356. {
  357. srb_t *sp;
  358. struct Scsi_Host *host = bsg_job->shost;
  359. scsi_qla_host_t *vha = shost_priv(host);
  360. struct qla_hw_data *ha = vha->hw;
  361. int rval = (DRIVER_ERROR << 16);
  362. int req_sg_cnt, rsp_sg_cnt;
  363. uint16_t loop_id;
  364. struct fc_port *fcport;
  365. char *type = "FC_BSG_HST_CT";
  366. struct srb_ctx *ct;
  367. req_sg_cnt =
  368. dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  369. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  370. if (!req_sg_cnt) {
  371. rval = -ENOMEM;
  372. goto done;
  373. }
  374. rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  375. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  376. if (!rsp_sg_cnt) {
  377. rval = -ENOMEM;
  378. goto done;
  379. }
  380. if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  381. (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  382. DEBUG2(qla_printk(KERN_WARNING, ha,
  383. "[request_sg_cnt: %x dma_request_sg_cnt: %x\
  384. reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
  385. bsg_job->request_payload.sg_cnt, req_sg_cnt,
  386. bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
  387. rval = -EAGAIN;
  388. goto done_unmap_sg;
  389. }
  390. if (!vha->flags.online) {
  391. DEBUG2(qla_printk(KERN_WARNING, ha,
  392. "host not online\n"));
  393. rval = -EIO;
  394. goto done_unmap_sg;
  395. }
  396. loop_id =
  397. (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
  398. >> 24;
  399. switch (loop_id) {
  400. case 0xFC:
  401. loop_id = cpu_to_le16(NPH_SNS);
  402. break;
  403. case 0xFA:
  404. loop_id = vha->mgmt_svr_loop_id;
  405. break;
  406. default:
  407. DEBUG2(qla_printk(KERN_INFO, ha,
  408. "Unknown loop id: %x\n", loop_id));
  409. rval = -EINVAL;
  410. goto done_unmap_sg;
  411. }
  412. /* Allocate a dummy fcport structure, since functions preparing the
  413. * IOCB and mailbox command retrieves port specific information
  414. * from fcport structure. For Host based ELS commands there will be
  415. * no fcport structure allocated
  416. */
  417. fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
  418. if (!fcport) {
  419. rval = -ENOMEM;
  420. goto done_unmap_sg;
  421. }
  422. /* Initialize all required fields of fcport */
  423. fcport->vha = vha;
  424. fcport->vp_idx = vha->vp_idx;
  425. fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
  426. fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
  427. fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
  428. fcport->loop_id = loop_id;
  429. /* Alloc SRB structure */
  430. sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_ctx));
  431. if (!sp) {
  432. rval = -ENOMEM;
  433. goto done_free_fcport;
  434. }
  435. ct = sp->ctx;
  436. ct->type = SRB_CT_CMD;
  437. ct->name = "bsg_ct";
  438. ct->u.bsg_job = bsg_job;
  439. DEBUG2(qla_printk(KERN_INFO, ha,
  440. "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
  441. "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
  442. (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
  443. fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
  444. fcport->d_id.b.al_pa));
  445. rval = qla2x00_start_sp(sp);
  446. if (rval != QLA_SUCCESS) {
  447. kfree(sp->ctx);
  448. mempool_free(sp, ha->srb_mempool);
  449. rval = -EIO;
  450. goto done_free_fcport;
  451. }
  452. return rval;
  453. done_free_fcport:
  454. kfree(fcport);
  455. done_unmap_sg:
  456. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  457. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  458. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  459. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  460. done:
  461. return rval;
  462. }
  463. /* Set the port configuration to enable the
  464. * internal loopback on ISP81XX
  465. */
  466. static inline int
  467. qla81xx_set_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
  468. uint16_t *new_config)
  469. {
  470. int ret = 0;
  471. int rval = 0;
  472. struct qla_hw_data *ha = vha->hw;
  473. if (!IS_QLA81XX(ha))
  474. goto done_set_internal;
  475. new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
  476. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  477. ha->notify_dcbx_comp = 1;
  478. ret = qla81xx_set_port_config(vha, new_config);
  479. if (ret != QLA_SUCCESS) {
  480. DEBUG2(printk(KERN_ERR
  481. "%s(%lu): Set port config failed\n",
  482. __func__, vha->host_no));
  483. ha->notify_dcbx_comp = 0;
  484. rval = -EINVAL;
  485. goto done_set_internal;
  486. }
  487. /* Wait for DCBX complete event */
  488. if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
  489. DEBUG2(qla_printk(KERN_WARNING, ha,
  490. "State change notificaition not received.\n"));
  491. } else
  492. DEBUG2(qla_printk(KERN_INFO, ha,
  493. "State change RECEIVED\n"));
  494. ha->notify_dcbx_comp = 0;
  495. done_set_internal:
  496. return rval;
  497. }
  498. /* Set the port configuration to disable the
  499. * internal loopback on ISP81XX
  500. */
  501. static inline int
  502. qla81xx_reset_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
  503. int wait)
  504. {
  505. int ret = 0;
  506. int rval = 0;
  507. uint16_t new_config[4];
  508. struct qla_hw_data *ha = vha->hw;
  509. if (!IS_QLA81XX(ha))
  510. goto done_reset_internal;
  511. memset(new_config, 0 , sizeof(new_config));
  512. if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
  513. ENABLE_INTERNAL_LOOPBACK) {
  514. new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
  515. memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
  516. ha->notify_dcbx_comp = wait;
  517. ret = qla81xx_set_port_config(vha, new_config);
  518. if (ret != QLA_SUCCESS) {
  519. DEBUG2(printk(KERN_ERR
  520. "%s(%lu): Set port config failed\n",
  521. __func__, vha->host_no));
  522. ha->notify_dcbx_comp = 0;
  523. rval = -EINVAL;
  524. goto done_reset_internal;
  525. }
  526. /* Wait for DCBX complete event */
  527. if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
  528. (20 * HZ))) {
  529. DEBUG2(qla_printk(KERN_WARNING, ha,
  530. "State change notificaition not received.\n"));
  531. ha->notify_dcbx_comp = 0;
  532. rval = -EINVAL;
  533. goto done_reset_internal;
  534. } else
  535. DEBUG2(qla_printk(KERN_INFO, ha,
  536. "State change RECEIVED\n"));
  537. ha->notify_dcbx_comp = 0;
  538. }
  539. done_reset_internal:
  540. return rval;
  541. }
  542. static int
  543. qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
  544. {
  545. struct Scsi_Host *host = bsg_job->shost;
  546. scsi_qla_host_t *vha = shost_priv(host);
  547. struct qla_hw_data *ha = vha->hw;
  548. int rval;
  549. uint8_t command_sent;
  550. char *type;
  551. struct msg_echo_lb elreq;
  552. uint16_t response[MAILBOX_REGISTER_COUNT];
  553. uint16_t config[4], new_config[4];
  554. uint8_t *fw_sts_ptr;
  555. uint8_t *req_data = NULL;
  556. dma_addr_t req_data_dma;
  557. uint32_t req_data_len;
  558. uint8_t *rsp_data = NULL;
  559. dma_addr_t rsp_data_dma;
  560. uint32_t rsp_data_len;
  561. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  562. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  563. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  564. return -EBUSY;
  565. if (!vha->flags.online) {
  566. DEBUG2(qla_printk(KERN_WARNING, ha, "host not online\n"));
  567. return -EIO;
  568. }
  569. elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
  570. bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
  571. DMA_TO_DEVICE);
  572. if (!elreq.req_sg_cnt)
  573. return -ENOMEM;
  574. elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
  575. bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
  576. DMA_FROM_DEVICE);
  577. if (!elreq.rsp_sg_cnt) {
  578. rval = -ENOMEM;
  579. goto done_unmap_req_sg;
  580. }
  581. if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
  582. (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
  583. DEBUG2(printk(KERN_INFO
  584. "dma mapping resulted in different sg counts "
  585. "[request_sg_cnt: %x dma_request_sg_cnt: %x "
  586. "reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
  587. bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
  588. bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt));
  589. rval = -EAGAIN;
  590. goto done_unmap_sg;
  591. }
  592. req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
  593. req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
  594. &req_data_dma, GFP_KERNEL);
  595. if (!req_data) {
  596. DEBUG2(printk(KERN_ERR "%s: dma alloc for req_data "
  597. "failed for host=%lu\n", __func__, vha->host_no));
  598. rval = -ENOMEM;
  599. goto done_unmap_sg;
  600. }
  601. rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
  602. &rsp_data_dma, GFP_KERNEL);
  603. if (!rsp_data) {
  604. DEBUG2(printk(KERN_ERR "%s: dma alloc for rsp_data "
  605. "failed for host=%lu\n", __func__, vha->host_no));
  606. rval = -ENOMEM;
  607. goto done_free_dma_req;
  608. }
  609. /* Copy the request buffer in req_data now */
  610. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  611. bsg_job->request_payload.sg_cnt, req_data, req_data_len);
  612. elreq.send_dma = req_data_dma;
  613. elreq.rcv_dma = rsp_data_dma;
  614. elreq.transfer_size = req_data_len;
  615. elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  616. if ((ha->current_topology == ISP_CFG_F ||
  617. (IS_QLA81XX(ha) &&
  618. le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
  619. && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
  620. elreq.options == EXTERNAL_LOOPBACK) {
  621. type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
  622. DEBUG2(qla_printk(KERN_INFO, ha,
  623. "scsi(%ld) bsg rqst type: %s\n", vha->host_no, type));
  624. command_sent = INT_DEF_LB_ECHO_CMD;
  625. rval = qla2x00_echo_test(vha, &elreq, response);
  626. } else {
  627. if (IS_QLA81XX(ha)) {
  628. memset(config, 0, sizeof(config));
  629. memset(new_config, 0, sizeof(new_config));
  630. if (qla81xx_get_port_config(vha, config)) {
  631. DEBUG2(printk(KERN_ERR
  632. "%s(%lu): Get port config failed\n",
  633. __func__, vha->host_no));
  634. bsg_job->reply->reply_payload_rcv_len = 0;
  635. bsg_job->reply->result = (DID_ERROR << 16);
  636. rval = -EPERM;
  637. goto done_free_dma_req;
  638. }
  639. if (elreq.options != EXTERNAL_LOOPBACK) {
  640. DEBUG2(qla_printk(KERN_INFO, ha,
  641. "Internal: current port config = %x\n",
  642. config[0]));
  643. if (qla81xx_set_internal_loopback(vha, config,
  644. new_config)) {
  645. bsg_job->reply->reply_payload_rcv_len =
  646. 0;
  647. bsg_job->reply->result =
  648. (DID_ERROR << 16);
  649. rval = -EPERM;
  650. goto done_free_dma_req;
  651. }
  652. } else {
  653. /* For external loopback to work
  654. * ensure internal loopback is disabled
  655. */
  656. if (qla81xx_reset_internal_loopback(vha,
  657. config, 1)) {
  658. bsg_job->reply->reply_payload_rcv_len =
  659. 0;
  660. bsg_job->reply->result =
  661. (DID_ERROR << 16);
  662. rval = -EPERM;
  663. goto done_free_dma_req;
  664. }
  665. }
  666. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  667. DEBUG2(qla_printk(KERN_INFO, ha,
  668. "scsi(%ld) bsg rqst type: %s\n",
  669. vha->host_no, type));
  670. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  671. rval = qla2x00_loopback_test(vha, &elreq, response);
  672. if (new_config[0]) {
  673. /* Revert back to original port config
  674. * Also clear internal loopback
  675. */
  676. qla81xx_reset_internal_loopback(vha,
  677. new_config, 0);
  678. }
  679. if (response[0] == MBS_COMMAND_ERROR &&
  680. response[1] == MBS_LB_RESET) {
  681. DEBUG2(printk(KERN_ERR "%s(%ld): ABORTing "
  682. "ISP\n", __func__, vha->host_no));
  683. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  684. qla2xxx_wake_dpc(vha);
  685. qla2x00_wait_for_chip_reset(vha);
  686. /* Also reset the MPI */
  687. if (qla81xx_restart_mpi_firmware(vha) !=
  688. QLA_SUCCESS) {
  689. qla_printk(KERN_INFO, ha,
  690. "MPI reset failed for host%ld.\n",
  691. vha->host_no);
  692. }
  693. bsg_job->reply->reply_payload_rcv_len = 0;
  694. bsg_job->reply->result = (DID_ERROR << 16);
  695. rval = -EIO;
  696. goto done_free_dma_req;
  697. }
  698. } else {
  699. type = "FC_BSG_HST_VENDOR_LOOPBACK";
  700. DEBUG2(qla_printk(KERN_INFO, ha,
  701. "scsi(%ld) bsg rqst type: %s\n",
  702. vha->host_no, type));
  703. command_sent = INT_DEF_LB_LOOPBACK_CMD;
  704. rval = qla2x00_loopback_test(vha, &elreq, response);
  705. }
  706. }
  707. if (rval) {
  708. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  709. "request %s failed\n", vha->host_no, type));
  710. fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
  711. sizeof(struct fc_bsg_reply);
  712. memcpy(fw_sts_ptr, response, sizeof(response));
  713. fw_sts_ptr += sizeof(response);
  714. *fw_sts_ptr = command_sent;
  715. rval = 0;
  716. bsg_job->reply->reply_payload_rcv_len = 0;
  717. bsg_job->reply->result = (DID_ERROR << 16);
  718. } else {
  719. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  720. "request %s completed\n", vha->host_no, type));
  721. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  722. sizeof(response) + sizeof(uint8_t);
  723. bsg_job->reply->reply_payload_rcv_len =
  724. bsg_job->reply_payload.payload_len;
  725. fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
  726. sizeof(struct fc_bsg_reply);
  727. memcpy(fw_sts_ptr, response, sizeof(response));
  728. fw_sts_ptr += sizeof(response);
  729. *fw_sts_ptr = command_sent;
  730. bsg_job->reply->result = DID_OK;
  731. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  732. bsg_job->reply_payload.sg_cnt, rsp_data,
  733. rsp_data_len);
  734. }
  735. bsg_job->job_done(bsg_job);
  736. dma_free_coherent(&ha->pdev->dev, rsp_data_len,
  737. rsp_data, rsp_data_dma);
  738. done_free_dma_req:
  739. dma_free_coherent(&ha->pdev->dev, req_data_len,
  740. req_data, req_data_dma);
  741. done_unmap_sg:
  742. dma_unmap_sg(&ha->pdev->dev,
  743. bsg_job->reply_payload.sg_list,
  744. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  745. done_unmap_req_sg:
  746. dma_unmap_sg(&ha->pdev->dev,
  747. bsg_job->request_payload.sg_list,
  748. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  749. return rval;
  750. }
  751. static int
  752. qla84xx_reset(struct fc_bsg_job *bsg_job)
  753. {
  754. struct Scsi_Host *host = bsg_job->shost;
  755. scsi_qla_host_t *vha = shost_priv(host);
  756. struct qla_hw_data *ha = vha->hw;
  757. int rval = 0;
  758. uint32_t flag;
  759. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  760. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  761. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  762. return -EBUSY;
  763. if (!IS_QLA84XX(ha)) {
  764. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
  765. "exiting.\n", vha->host_no));
  766. return -EINVAL;
  767. }
  768. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  769. rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
  770. if (rval) {
  771. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  772. "request 84xx reset failed\n", vha->host_no));
  773. rval = bsg_job->reply->reply_payload_rcv_len = 0;
  774. bsg_job->reply->result = (DID_ERROR << 16);
  775. } else {
  776. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  777. "request 84xx reset completed\n", vha->host_no));
  778. bsg_job->reply->result = DID_OK;
  779. }
  780. bsg_job->job_done(bsg_job);
  781. return rval;
  782. }
  783. static int
  784. qla84xx_updatefw(struct fc_bsg_job *bsg_job)
  785. {
  786. struct Scsi_Host *host = bsg_job->shost;
  787. scsi_qla_host_t *vha = shost_priv(host);
  788. struct qla_hw_data *ha = vha->hw;
  789. struct verify_chip_entry_84xx *mn = NULL;
  790. dma_addr_t mn_dma, fw_dma;
  791. void *fw_buf = NULL;
  792. int rval = 0;
  793. uint32_t sg_cnt;
  794. uint32_t data_len;
  795. uint16_t options;
  796. uint32_t flag;
  797. uint32_t fw_ver;
  798. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  799. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  800. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  801. return -EBUSY;
  802. if (!IS_QLA84XX(ha)) {
  803. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
  804. "exiting.\n", vha->host_no));
  805. return -EINVAL;
  806. }
  807. sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  808. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  809. if (!sg_cnt)
  810. return -ENOMEM;
  811. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  812. DEBUG2(printk(KERN_INFO
  813. "dma mapping resulted in different sg counts "
  814. "request_sg_cnt: %x dma_request_sg_cnt: %x ",
  815. bsg_job->request_payload.sg_cnt, sg_cnt));
  816. rval = -EAGAIN;
  817. goto done_unmap_sg;
  818. }
  819. data_len = bsg_job->request_payload.payload_len;
  820. fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
  821. &fw_dma, GFP_KERNEL);
  822. if (!fw_buf) {
  823. DEBUG2(printk(KERN_ERR "%s: dma alloc for fw_buf "
  824. "failed for host=%lu\n", __func__, vha->host_no));
  825. rval = -ENOMEM;
  826. goto done_unmap_sg;
  827. }
  828. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  829. bsg_job->request_payload.sg_cnt, fw_buf, data_len);
  830. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  831. if (!mn) {
  832. DEBUG2(printk(KERN_ERR "%s: dma alloc for fw buffer "
  833. "failed for host=%lu\n", __func__, vha->host_no));
  834. rval = -ENOMEM;
  835. goto done_free_fw_buf;
  836. }
  837. flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  838. fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
  839. memset(mn, 0, sizeof(struct access_chip_84xx));
  840. mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
  841. mn->entry_count = 1;
  842. options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
  843. if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
  844. options |= VCO_DIAG_FW;
  845. mn->options = cpu_to_le16(options);
  846. mn->fw_ver = cpu_to_le32(fw_ver);
  847. mn->fw_size = cpu_to_le32(data_len);
  848. mn->fw_seq_size = cpu_to_le32(data_len);
  849. mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
  850. mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
  851. mn->dseg_length = cpu_to_le32(data_len);
  852. mn->data_seg_cnt = cpu_to_le16(1);
  853. rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
  854. if (rval) {
  855. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  856. "request 84xx updatefw failed\n", vha->host_no));
  857. rval = bsg_job->reply->reply_payload_rcv_len = 0;
  858. bsg_job->reply->result = (DID_ERROR << 16);
  859. } else {
  860. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  861. "request 84xx updatefw completed\n", vha->host_no));
  862. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  863. bsg_job->reply->result = DID_OK;
  864. }
  865. bsg_job->job_done(bsg_job);
  866. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  867. done_free_fw_buf:
  868. dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
  869. done_unmap_sg:
  870. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  871. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  872. return rval;
  873. }
  874. static int
  875. qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
  876. {
  877. struct Scsi_Host *host = bsg_job->shost;
  878. scsi_qla_host_t *vha = shost_priv(host);
  879. struct qla_hw_data *ha = vha->hw;
  880. struct access_chip_84xx *mn = NULL;
  881. dma_addr_t mn_dma, mgmt_dma;
  882. void *mgmt_b = NULL;
  883. int rval = 0;
  884. struct qla_bsg_a84_mgmt *ql84_mgmt;
  885. uint32_t sg_cnt;
  886. uint32_t data_len = 0;
  887. uint32_t dma_direction = DMA_NONE;
  888. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  889. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  890. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  891. return -EBUSY;
  892. if (!IS_QLA84XX(ha)) {
  893. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
  894. "exiting.\n", vha->host_no));
  895. return -EINVAL;
  896. }
  897. ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
  898. sizeof(struct fc_bsg_request));
  899. if (!ql84_mgmt) {
  900. DEBUG2(printk("%s(%ld): mgmt header not provided, exiting.\n",
  901. __func__, vha->host_no));
  902. return -EINVAL;
  903. }
  904. mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
  905. if (!mn) {
  906. DEBUG2(printk(KERN_ERR "%s: dma alloc for fw buffer "
  907. "failed for host=%lu\n", __func__, vha->host_no));
  908. return -ENOMEM;
  909. }
  910. memset(mn, 0, sizeof(struct access_chip_84xx));
  911. mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
  912. mn->entry_count = 1;
  913. switch (ql84_mgmt->mgmt.cmd) {
  914. case QLA84_MGMT_READ_MEM:
  915. case QLA84_MGMT_GET_INFO:
  916. sg_cnt = dma_map_sg(&ha->pdev->dev,
  917. bsg_job->reply_payload.sg_list,
  918. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  919. if (!sg_cnt) {
  920. rval = -ENOMEM;
  921. goto exit_mgmt;
  922. }
  923. dma_direction = DMA_FROM_DEVICE;
  924. if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
  925. DEBUG2(printk(KERN_INFO
  926. "dma mapping resulted in different sg counts "
  927. "reply_sg_cnt: %x dma_reply_sg_cnt: %x\n",
  928. bsg_job->reply_payload.sg_cnt, sg_cnt));
  929. rval = -EAGAIN;
  930. goto done_unmap_sg;
  931. }
  932. data_len = bsg_job->reply_payload.payload_len;
  933. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  934. &mgmt_dma, GFP_KERNEL);
  935. if (!mgmt_b) {
  936. DEBUG2(printk(KERN_ERR "%s: dma alloc for mgmt_b "
  937. "failed for host=%lu\n",
  938. __func__, vha->host_no));
  939. rval = -ENOMEM;
  940. goto done_unmap_sg;
  941. }
  942. if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
  943. mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
  944. mn->parameter1 =
  945. cpu_to_le32(
  946. ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  947. } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
  948. mn->options = cpu_to_le16(ACO_REQUEST_INFO);
  949. mn->parameter1 =
  950. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
  951. mn->parameter2 =
  952. cpu_to_le32(
  953. ql84_mgmt->mgmt.mgmtp.u.info.context);
  954. }
  955. break;
  956. case QLA84_MGMT_WRITE_MEM:
  957. sg_cnt = dma_map_sg(&ha->pdev->dev,
  958. bsg_job->request_payload.sg_list,
  959. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  960. if (!sg_cnt) {
  961. rval = -ENOMEM;
  962. goto exit_mgmt;
  963. }
  964. dma_direction = DMA_TO_DEVICE;
  965. if (sg_cnt != bsg_job->request_payload.sg_cnt) {
  966. DEBUG2(printk(KERN_INFO
  967. "dma mapping resulted in different sg counts "
  968. "request_sg_cnt: %x dma_request_sg_cnt: %x ",
  969. bsg_job->request_payload.sg_cnt, sg_cnt));
  970. rval = -EAGAIN;
  971. goto done_unmap_sg;
  972. }
  973. data_len = bsg_job->request_payload.payload_len;
  974. mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
  975. &mgmt_dma, GFP_KERNEL);
  976. if (!mgmt_b) {
  977. DEBUG2(printk(KERN_ERR "%s: dma alloc for mgmt_b "
  978. "failed for host=%lu\n",
  979. __func__, vha->host_no));
  980. rval = -ENOMEM;
  981. goto done_unmap_sg;
  982. }
  983. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  984. bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
  985. mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
  986. mn->parameter1 =
  987. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
  988. break;
  989. case QLA84_MGMT_CHNG_CONFIG:
  990. mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
  991. mn->parameter1 =
  992. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
  993. mn->parameter2 =
  994. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
  995. mn->parameter3 =
  996. cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
  997. break;
  998. default:
  999. rval = -EIO;
  1000. goto exit_mgmt;
  1001. }
  1002. if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
  1003. mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
  1004. mn->dseg_count = cpu_to_le16(1);
  1005. mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
  1006. mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
  1007. mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
  1008. }
  1009. rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
  1010. if (rval) {
  1011. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  1012. "request 84xx mgmt failed\n", vha->host_no));
  1013. rval = bsg_job->reply->reply_payload_rcv_len = 0;
  1014. bsg_job->reply->result = (DID_ERROR << 16);
  1015. } else {
  1016. DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
  1017. "request 84xx mgmt completed\n", vha->host_no));
  1018. bsg_job->reply_len = sizeof(struct fc_bsg_reply);
  1019. bsg_job->reply->result = DID_OK;
  1020. if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
  1021. (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
  1022. bsg_job->reply->reply_payload_rcv_len =
  1023. bsg_job->reply_payload.payload_len;
  1024. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1025. bsg_job->reply_payload.sg_cnt, mgmt_b,
  1026. data_len);
  1027. }
  1028. }
  1029. bsg_job->job_done(bsg_job);
  1030. done_unmap_sg:
  1031. if (mgmt_b)
  1032. dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
  1033. if (dma_direction == DMA_TO_DEVICE)
  1034. dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
  1035. bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1036. else if (dma_direction == DMA_FROM_DEVICE)
  1037. dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
  1038. bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1039. exit_mgmt:
  1040. dma_pool_free(ha->s_dma_pool, mn, mn_dma);
  1041. return rval;
  1042. }
  1043. static int
  1044. qla24xx_iidma(struct fc_bsg_job *bsg_job)
  1045. {
  1046. struct Scsi_Host *host = bsg_job->shost;
  1047. scsi_qla_host_t *vha = shost_priv(host);
  1048. struct qla_hw_data *ha = vha->hw;
  1049. int rval = 0;
  1050. struct qla_port_param *port_param = NULL;
  1051. fc_port_t *fcport = NULL;
  1052. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1053. uint8_t *rsp_ptr = NULL;
  1054. bsg_job->reply->reply_payload_rcv_len = 0;
  1055. if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  1056. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
  1057. test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
  1058. return -EBUSY;
  1059. if (!IS_IIDMA_CAPABLE(vha->hw)) {
  1060. DEBUG2(qla_printk(KERN_WARNING, ha, "%s(%lu): iiDMA not "
  1061. "supported\n", __func__, vha->host_no));
  1062. return -EINVAL;
  1063. }
  1064. port_param = (struct qla_port_param *)((char *)bsg_job->request +
  1065. sizeof(struct fc_bsg_request));
  1066. if (!port_param) {
  1067. DEBUG2(printk("%s(%ld): port_param header not provided, "
  1068. "exiting.\n", __func__, vha->host_no));
  1069. return -EINVAL;
  1070. }
  1071. if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
  1072. DEBUG2(printk(KERN_ERR "%s(%ld): Invalid destination type\n",
  1073. __func__, vha->host_no));
  1074. return -EINVAL;
  1075. }
  1076. list_for_each_entry(fcport, &vha->vp_fcports, list) {
  1077. if (fcport->port_type != FCT_TARGET)
  1078. continue;
  1079. if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
  1080. fcport->port_name, sizeof(fcport->port_name)))
  1081. continue;
  1082. break;
  1083. }
  1084. if (!fcport) {
  1085. DEBUG2(printk(KERN_ERR "%s(%ld): Failed to find port\n",
  1086. __func__, vha->host_no));
  1087. return -EINVAL;
  1088. }
  1089. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  1090. DEBUG2(printk(KERN_ERR "%s(%ld): Port not online\n",
  1091. __func__, vha->host_no));
  1092. return -EINVAL;
  1093. }
  1094. if (fcport->flags & FCF_LOGIN_NEEDED) {
  1095. DEBUG2(printk(KERN_ERR "%s(%ld): Remote port not logged in, "
  1096. "flags = 0x%x\n",
  1097. __func__, vha->host_no, fcport->flags));
  1098. return -EINVAL;
  1099. }
  1100. if (port_param->mode)
  1101. rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
  1102. port_param->speed, mb);
  1103. else
  1104. rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
  1105. &port_param->speed, mb);
  1106. if (rval) {
  1107. DEBUG16(printk(KERN_ERR "scsi(%ld): iIDMA cmd failed for "
  1108. "%02x%02x%02x%02x%02x%02x%02x%02x -- "
  1109. "%04x %x %04x %04x.\n",
  1110. vha->host_no, fcport->port_name[0],
  1111. fcport->port_name[1],
  1112. fcport->port_name[2], fcport->port_name[3],
  1113. fcport->port_name[4], fcport->port_name[5],
  1114. fcport->port_name[6], fcport->port_name[7], rval,
  1115. fcport->fp_speed, mb[0], mb[1]));
  1116. rval = 0;
  1117. bsg_job->reply->result = (DID_ERROR << 16);
  1118. } else {
  1119. if (!port_param->mode) {
  1120. bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
  1121. sizeof(struct qla_port_param);
  1122. rsp_ptr = ((uint8_t *)bsg_job->reply) +
  1123. sizeof(struct fc_bsg_reply);
  1124. memcpy(rsp_ptr, port_param,
  1125. sizeof(struct qla_port_param));
  1126. }
  1127. bsg_job->reply->result = DID_OK;
  1128. }
  1129. bsg_job->job_done(bsg_job);
  1130. return rval;
  1131. }
  1132. static int
  1133. qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, struct qla_hw_data *ha,
  1134. uint8_t is_update)
  1135. {
  1136. uint32_t start = 0;
  1137. int valid = 0;
  1138. bsg_job->reply->reply_payload_rcv_len = 0;
  1139. if (unlikely(pci_channel_offline(ha->pdev)))
  1140. return -EINVAL;
  1141. start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
  1142. if (start > ha->optrom_size)
  1143. return -EINVAL;
  1144. if (ha->optrom_state != QLA_SWAITING)
  1145. return -EBUSY;
  1146. ha->optrom_region_start = start;
  1147. if (is_update) {
  1148. if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
  1149. valid = 1;
  1150. else if (start == (ha->flt_region_boot * 4) ||
  1151. start == (ha->flt_region_fw * 4))
  1152. valid = 1;
  1153. else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
  1154. IS_QLA8XXX_TYPE(ha))
  1155. valid = 1;
  1156. if (!valid) {
  1157. qla_printk(KERN_WARNING, ha,
  1158. "Invalid start region 0x%x/0x%x.\n",
  1159. start, bsg_job->request_payload.payload_len);
  1160. return -EINVAL;
  1161. }
  1162. ha->optrom_region_size = start +
  1163. bsg_job->request_payload.payload_len > ha->optrom_size ?
  1164. ha->optrom_size - start :
  1165. bsg_job->request_payload.payload_len;
  1166. ha->optrom_state = QLA_SWRITING;
  1167. } else {
  1168. ha->optrom_region_size = start +
  1169. bsg_job->reply_payload.payload_len > ha->optrom_size ?
  1170. ha->optrom_size - start :
  1171. bsg_job->reply_payload.payload_len;
  1172. ha->optrom_state = QLA_SREADING;
  1173. }
  1174. ha->optrom_buffer = vmalloc(ha->optrom_region_size);
  1175. if (!ha->optrom_buffer) {
  1176. qla_printk(KERN_WARNING, ha,
  1177. "Read: Unable to allocate memory for optrom retrieval "
  1178. "(%x).\n", ha->optrom_region_size);
  1179. ha->optrom_state = QLA_SWAITING;
  1180. return -ENOMEM;
  1181. }
  1182. memset(ha->optrom_buffer, 0, ha->optrom_region_size);
  1183. return 0;
  1184. }
  1185. static int
  1186. qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
  1187. {
  1188. struct Scsi_Host *host = bsg_job->shost;
  1189. scsi_qla_host_t *vha = shost_priv(host);
  1190. struct qla_hw_data *ha = vha->hw;
  1191. int rval = 0;
  1192. rval = qla2x00_optrom_setup(bsg_job, ha, 0);
  1193. if (rval)
  1194. return rval;
  1195. ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
  1196. ha->optrom_region_start, ha->optrom_region_size);
  1197. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  1198. bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
  1199. ha->optrom_region_size);
  1200. bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
  1201. bsg_job->reply->result = DID_OK;
  1202. vfree(ha->optrom_buffer);
  1203. ha->optrom_buffer = NULL;
  1204. ha->optrom_state = QLA_SWAITING;
  1205. bsg_job->job_done(bsg_job);
  1206. return rval;
  1207. }
  1208. static int
  1209. qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
  1210. {
  1211. struct Scsi_Host *host = bsg_job->shost;
  1212. scsi_qla_host_t *vha = shost_priv(host);
  1213. struct qla_hw_data *ha = vha->hw;
  1214. int rval = 0;
  1215. rval = qla2x00_optrom_setup(bsg_job, ha, 1);
  1216. if (rval)
  1217. return rval;
  1218. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  1219. bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
  1220. ha->optrom_region_size);
  1221. ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
  1222. ha->optrom_region_start, ha->optrom_region_size);
  1223. bsg_job->reply->result = DID_OK;
  1224. vfree(ha->optrom_buffer);
  1225. ha->optrom_buffer = NULL;
  1226. ha->optrom_state = QLA_SWAITING;
  1227. bsg_job->job_done(bsg_job);
  1228. return rval;
  1229. }
  1230. static int
  1231. qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
  1232. {
  1233. switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
  1234. case QL_VND_LOOPBACK:
  1235. return qla2x00_process_loopback(bsg_job);
  1236. case QL_VND_A84_RESET:
  1237. return qla84xx_reset(bsg_job);
  1238. case QL_VND_A84_UPDATE_FW:
  1239. return qla84xx_updatefw(bsg_job);
  1240. case QL_VND_A84_MGMT_CMD:
  1241. return qla84xx_mgmt_cmd(bsg_job);
  1242. case QL_VND_IIDMA:
  1243. return qla24xx_iidma(bsg_job);
  1244. case QL_VND_FCP_PRIO_CFG_CMD:
  1245. return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
  1246. case QL_VND_READ_FLASH:
  1247. return qla2x00_read_optrom(bsg_job);
  1248. case QL_VND_UPDATE_FLASH:
  1249. return qla2x00_update_optrom(bsg_job);
  1250. default:
  1251. bsg_job->reply->result = (DID_ERROR << 16);
  1252. bsg_job->job_done(bsg_job);
  1253. return -ENOSYS;
  1254. }
  1255. }
  1256. int
  1257. qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
  1258. {
  1259. int ret = -EINVAL;
  1260. switch (bsg_job->request->msgcode) {
  1261. case FC_BSG_RPT_ELS:
  1262. case FC_BSG_HST_ELS_NOLOGIN:
  1263. ret = qla2x00_process_els(bsg_job);
  1264. break;
  1265. case FC_BSG_HST_CT:
  1266. ret = qla2x00_process_ct(bsg_job);
  1267. break;
  1268. case FC_BSG_HST_VENDOR:
  1269. ret = qla2x00_process_vendor_specific(bsg_job);
  1270. break;
  1271. case FC_BSG_HST_ADD_RPORT:
  1272. case FC_BSG_HST_DEL_RPORT:
  1273. case FC_BSG_RPT_CT:
  1274. default:
  1275. DEBUG2(printk("qla2xxx: unsupported BSG request\n"));
  1276. break;
  1277. }
  1278. return ret;
  1279. }
  1280. int
  1281. qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
  1282. {
  1283. scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
  1284. struct qla_hw_data *ha = vha->hw;
  1285. srb_t *sp;
  1286. int cnt, que;
  1287. unsigned long flags;
  1288. struct req_que *req;
  1289. struct srb_ctx *sp_bsg;
  1290. /* find the bsg job from the active list of commands */
  1291. spin_lock_irqsave(&ha->hardware_lock, flags);
  1292. for (que = 0; que < ha->max_req_queues; que++) {
  1293. req = ha->req_q_map[que];
  1294. if (!req)
  1295. continue;
  1296. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  1297. sp = req->outstanding_cmds[cnt];
  1298. if (sp) {
  1299. sp_bsg = sp->ctx;
  1300. if (((sp_bsg->type == SRB_CT_CMD) ||
  1301. (sp_bsg->type == SRB_ELS_CMD_HST))
  1302. && (sp_bsg->u.bsg_job == bsg_job)) {
  1303. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1304. if (ha->isp_ops->abort_command(sp)) {
  1305. DEBUG2(qla_printk(KERN_INFO, ha,
  1306. "scsi(%ld): mbx "
  1307. "abort_command failed\n",
  1308. vha->host_no));
  1309. bsg_job->req->errors =
  1310. bsg_job->reply->result = -EIO;
  1311. } else {
  1312. DEBUG2(qla_printk(KERN_INFO, ha,
  1313. "scsi(%ld): mbx "
  1314. "abort_command success\n",
  1315. vha->host_no));
  1316. bsg_job->req->errors =
  1317. bsg_job->reply->result = 0;
  1318. }
  1319. spin_lock_irqsave(&ha->hardware_lock, flags);
  1320. goto done;
  1321. }
  1322. }
  1323. }
  1324. }
  1325. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1326. DEBUG2(qla_printk(KERN_INFO, ha,
  1327. "scsi(%ld) SRB not found to abort\n", vha->host_no));
  1328. bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
  1329. return 0;
  1330. done:
  1331. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1332. if (bsg_job->request->msgcode == FC_BSG_HST_CT)
  1333. kfree(sp->fcport);
  1334. kfree(sp->ctx);
  1335. mempool_free(sp, ha->srb_mempool);
  1336. return 0;
  1337. }