qla_iocb.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2011 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/blkdev.h>
  9. #include <linux/delay.h>
  10. #include <scsi/scsi_tcq.h>
  11. static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
  12. static void qla25xx_set_que(srb_t *, struct rsp_que **);
  13. /**
  14. * qla2x00_get_cmd_direction() - Determine control_flag data direction.
  15. * @cmd: SCSI command
  16. *
  17. * Returns the proper CF_* direction based on CDB.
  18. */
  19. static inline uint16_t
  20. qla2x00_get_cmd_direction(srb_t *sp)
  21. {
  22. uint16_t cflags;
  23. cflags = 0;
  24. /* Set transfer direction */
  25. if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
  26. cflags = CF_WRITE;
  27. sp->fcport->vha->hw->qla_stats.output_bytes +=
  28. scsi_bufflen(sp->cmd);
  29. } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
  30. cflags = CF_READ;
  31. sp->fcport->vha->hw->qla_stats.input_bytes +=
  32. scsi_bufflen(sp->cmd);
  33. }
  34. return (cflags);
  35. }
  36. /**
  37. * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
  38. * Continuation Type 0 IOCBs to allocate.
  39. *
  40. * @dsds: number of data segment decriptors needed
  41. *
  42. * Returns the number of IOCB entries needed to store @dsds.
  43. */
  44. uint16_t
  45. qla2x00_calc_iocbs_32(uint16_t dsds)
  46. {
  47. uint16_t iocbs;
  48. iocbs = 1;
  49. if (dsds > 3) {
  50. iocbs += (dsds - 3) / 7;
  51. if ((dsds - 3) % 7)
  52. iocbs++;
  53. }
  54. return (iocbs);
  55. }
  56. /**
  57. * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
  58. * Continuation Type 1 IOCBs to allocate.
  59. *
  60. * @dsds: number of data segment decriptors needed
  61. *
  62. * Returns the number of IOCB entries needed to store @dsds.
  63. */
  64. uint16_t
  65. qla2x00_calc_iocbs_64(uint16_t dsds)
  66. {
  67. uint16_t iocbs;
  68. iocbs = 1;
  69. if (dsds > 2) {
  70. iocbs += (dsds - 2) / 5;
  71. if ((dsds - 2) % 5)
  72. iocbs++;
  73. }
  74. return (iocbs);
  75. }
  76. /**
  77. * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
  78. * @ha: HA context
  79. *
  80. * Returns a pointer to the Continuation Type 0 IOCB packet.
  81. */
  82. static inline cont_entry_t *
  83. qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
  84. {
  85. cont_entry_t *cont_pkt;
  86. struct req_que *req = vha->req;
  87. /* Adjust ring index. */
  88. req->ring_index++;
  89. if (req->ring_index == req->length) {
  90. req->ring_index = 0;
  91. req->ring_ptr = req->ring;
  92. } else {
  93. req->ring_ptr++;
  94. }
  95. cont_pkt = (cont_entry_t *)req->ring_ptr;
  96. /* Load packet defaults. */
  97. *((uint32_t *)(&cont_pkt->entry_type)) =
  98. __constant_cpu_to_le32(CONTINUE_TYPE);
  99. return (cont_pkt);
  100. }
  101. /**
  102. * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
  103. * @ha: HA context
  104. *
  105. * Returns a pointer to the continuation type 1 IOCB packet.
  106. */
  107. static inline cont_a64_entry_t *
  108. qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
  109. {
  110. cont_a64_entry_t *cont_pkt;
  111. struct req_que *req = vha->req;
  112. /* Adjust ring index. */
  113. req->ring_index++;
  114. if (req->ring_index == req->length) {
  115. req->ring_index = 0;
  116. req->ring_ptr = req->ring;
  117. } else {
  118. req->ring_ptr++;
  119. }
  120. cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
  121. /* Load packet defaults. */
  122. *((uint32_t *)(&cont_pkt->entry_type)) =
  123. __constant_cpu_to_le32(CONTINUE_A64_TYPE);
  124. return (cont_pkt);
  125. }
  126. static inline int
  127. qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
  128. {
  129. uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
  130. /* We only support T10 DIF right now */
  131. if (guard != SHOST_DIX_GUARD_CRC) {
  132. DEBUG2(printk(KERN_ERR "Unsupported guard: %d\n", guard));
  133. return 0;
  134. }
  135. /* We always use DIFF Bundling for best performance */
  136. *fw_prot_opts = 0;
  137. /* Translate SCSI opcode to a protection opcode */
  138. switch (scsi_get_prot_op(sp->cmd)) {
  139. case SCSI_PROT_READ_STRIP:
  140. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  141. break;
  142. case SCSI_PROT_WRITE_INSERT:
  143. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  144. break;
  145. case SCSI_PROT_READ_INSERT:
  146. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  147. break;
  148. case SCSI_PROT_WRITE_STRIP:
  149. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  150. break;
  151. case SCSI_PROT_READ_PASS:
  152. *fw_prot_opts |= PO_MODE_DIF_PASS;
  153. break;
  154. case SCSI_PROT_WRITE_PASS:
  155. *fw_prot_opts |= PO_MODE_DIF_PASS;
  156. break;
  157. default: /* Normal Request */
  158. *fw_prot_opts |= PO_MODE_DIF_PASS;
  159. break;
  160. }
  161. return scsi_prot_sg_count(sp->cmd);
  162. }
  163. /*
  164. * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
  165. * capable IOCB types.
  166. *
  167. * @sp: SRB command to process
  168. * @cmd_pkt: Command type 2 IOCB
  169. * @tot_dsds: Total number of segments to transfer
  170. */
  171. void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
  172. uint16_t tot_dsds)
  173. {
  174. uint16_t avail_dsds;
  175. uint32_t *cur_dsd;
  176. scsi_qla_host_t *vha;
  177. struct scsi_cmnd *cmd;
  178. struct scatterlist *sg;
  179. int i;
  180. cmd = sp->cmd;
  181. /* Update entry type to indicate Command Type 2 IOCB */
  182. *((uint32_t *)(&cmd_pkt->entry_type)) =
  183. __constant_cpu_to_le32(COMMAND_TYPE);
  184. /* No data transfer */
  185. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  186. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  187. return;
  188. }
  189. vha = sp->fcport->vha;
  190. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  191. /* Three DSDs are available in the Command Type 2 IOCB */
  192. avail_dsds = 3;
  193. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  194. /* Load data segments */
  195. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  196. cont_entry_t *cont_pkt;
  197. /* Allocate additional continuation packets? */
  198. if (avail_dsds == 0) {
  199. /*
  200. * Seven DSDs are available in the Continuation
  201. * Type 0 IOCB.
  202. */
  203. cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
  204. cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
  205. avail_dsds = 7;
  206. }
  207. *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
  208. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  209. avail_dsds--;
  210. }
  211. }
  212. /**
  213. * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
  214. * capable IOCB types.
  215. *
  216. * @sp: SRB command to process
  217. * @cmd_pkt: Command type 3 IOCB
  218. * @tot_dsds: Total number of segments to transfer
  219. */
  220. void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
  221. uint16_t tot_dsds)
  222. {
  223. uint16_t avail_dsds;
  224. uint32_t *cur_dsd;
  225. scsi_qla_host_t *vha;
  226. struct scsi_cmnd *cmd;
  227. struct scatterlist *sg;
  228. int i;
  229. cmd = sp->cmd;
  230. /* Update entry type to indicate Command Type 3 IOCB */
  231. *((uint32_t *)(&cmd_pkt->entry_type)) =
  232. __constant_cpu_to_le32(COMMAND_A64_TYPE);
  233. /* No data transfer */
  234. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  235. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  236. return;
  237. }
  238. vha = sp->fcport->vha;
  239. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  240. /* Two DSDs are available in the Command Type 3 IOCB */
  241. avail_dsds = 2;
  242. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  243. /* Load data segments */
  244. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  245. dma_addr_t sle_dma;
  246. cont_a64_entry_t *cont_pkt;
  247. /* Allocate additional continuation packets? */
  248. if (avail_dsds == 0) {
  249. /*
  250. * Five DSDs are available in the Continuation
  251. * Type 1 IOCB.
  252. */
  253. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  254. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  255. avail_dsds = 5;
  256. }
  257. sle_dma = sg_dma_address(sg);
  258. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  259. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  260. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  261. avail_dsds--;
  262. }
  263. }
  264. /**
  265. * qla2x00_start_scsi() - Send a SCSI command to the ISP
  266. * @sp: command to send to the ISP
  267. *
  268. * Returns non-zero if a failure occurred, else zero.
  269. */
  270. int
  271. qla2x00_start_scsi(srb_t *sp)
  272. {
  273. int ret, nseg;
  274. unsigned long flags;
  275. scsi_qla_host_t *vha;
  276. struct scsi_cmnd *cmd;
  277. uint32_t *clr_ptr;
  278. uint32_t index;
  279. uint32_t handle;
  280. cmd_entry_t *cmd_pkt;
  281. uint16_t cnt;
  282. uint16_t req_cnt;
  283. uint16_t tot_dsds;
  284. struct device_reg_2xxx __iomem *reg;
  285. struct qla_hw_data *ha;
  286. struct req_que *req;
  287. struct rsp_que *rsp;
  288. char tag[2];
  289. /* Setup device pointers. */
  290. ret = 0;
  291. vha = sp->fcport->vha;
  292. ha = vha->hw;
  293. reg = &ha->iobase->isp;
  294. cmd = sp->cmd;
  295. req = ha->req_q_map[0];
  296. rsp = ha->rsp_q_map[0];
  297. /* So we know we haven't pci_map'ed anything yet */
  298. tot_dsds = 0;
  299. /* Send marker if required */
  300. if (vha->marker_needed != 0) {
  301. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
  302. != QLA_SUCCESS)
  303. return (QLA_FUNCTION_FAILED);
  304. vha->marker_needed = 0;
  305. }
  306. /* Acquire ring specific lock */
  307. spin_lock_irqsave(&ha->hardware_lock, flags);
  308. /* Check for room in outstanding command list. */
  309. handle = req->current_outstanding_cmd;
  310. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  311. handle++;
  312. if (handle == MAX_OUTSTANDING_COMMANDS)
  313. handle = 1;
  314. if (!req->outstanding_cmds[handle])
  315. break;
  316. }
  317. if (index == MAX_OUTSTANDING_COMMANDS)
  318. goto queuing_error;
  319. /* Map the sg table so we have an accurate count of sg entries needed */
  320. if (scsi_sg_count(cmd)) {
  321. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  322. scsi_sg_count(cmd), cmd->sc_data_direction);
  323. if (unlikely(!nseg))
  324. goto queuing_error;
  325. } else
  326. nseg = 0;
  327. tot_dsds = nseg;
  328. /* Calculate the number of request entries needed. */
  329. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  330. if (req->cnt < (req_cnt + 2)) {
  331. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  332. if (req->ring_index < cnt)
  333. req->cnt = cnt - req->ring_index;
  334. else
  335. req->cnt = req->length -
  336. (req->ring_index - cnt);
  337. }
  338. if (req->cnt < (req_cnt + 2))
  339. goto queuing_error;
  340. /* Build command packet */
  341. req->current_outstanding_cmd = handle;
  342. req->outstanding_cmds[handle] = sp;
  343. sp->handle = handle;
  344. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  345. req->cnt -= req_cnt;
  346. cmd_pkt = (cmd_entry_t *)req->ring_ptr;
  347. cmd_pkt->handle = handle;
  348. /* Zero out remaining portion of packet. */
  349. clr_ptr = (uint32_t *)cmd_pkt + 2;
  350. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  351. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  352. /* Set target ID and LUN number*/
  353. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  354. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  355. /* Update tagged queuing modifier */
  356. if (scsi_populate_tag_msg(cmd, tag)) {
  357. switch (tag[0]) {
  358. case HEAD_OF_QUEUE_TAG:
  359. cmd_pkt->control_flags =
  360. __constant_cpu_to_le16(CF_HEAD_TAG);
  361. break;
  362. case ORDERED_QUEUE_TAG:
  363. cmd_pkt->control_flags =
  364. __constant_cpu_to_le16(CF_ORDERED_TAG);
  365. break;
  366. default:
  367. cmd_pkt->control_flags =
  368. __constant_cpu_to_le16(CF_SIMPLE_TAG);
  369. break;
  370. }
  371. }
  372. /* Load SCSI command packet. */
  373. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  374. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  375. /* Build IOCB segments */
  376. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  377. /* Set total data segment count. */
  378. cmd_pkt->entry_count = (uint8_t)req_cnt;
  379. wmb();
  380. /* Adjust ring index. */
  381. req->ring_index++;
  382. if (req->ring_index == req->length) {
  383. req->ring_index = 0;
  384. req->ring_ptr = req->ring;
  385. } else
  386. req->ring_ptr++;
  387. sp->flags |= SRB_DMA_VALID;
  388. /* Set chip new ring index. */
  389. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
  390. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  391. /* Manage unprocessed RIO/ZIO commands in response queue. */
  392. if (vha->flags.process_response_queue &&
  393. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  394. qla2x00_process_response_queue(rsp);
  395. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  396. return (QLA_SUCCESS);
  397. queuing_error:
  398. if (tot_dsds)
  399. scsi_dma_unmap(cmd);
  400. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  401. return (QLA_FUNCTION_FAILED);
  402. }
  403. /**
  404. * qla2x00_marker() - Send a marker IOCB to the firmware.
  405. * @ha: HA context
  406. * @loop_id: loop ID
  407. * @lun: LUN
  408. * @type: marker modifier
  409. *
  410. * Can be called from both normal and interrupt context.
  411. *
  412. * Returns non-zero if a failure occurred, else zero.
  413. */
  414. static int
  415. __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  416. struct rsp_que *rsp, uint16_t loop_id,
  417. uint16_t lun, uint8_t type)
  418. {
  419. mrk_entry_t *mrk;
  420. struct mrk_entry_24xx *mrk24;
  421. struct qla_hw_data *ha = vha->hw;
  422. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  423. mrk24 = NULL;
  424. mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
  425. if (mrk == NULL) {
  426. DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
  427. __func__, base_vha->host_no));
  428. return (QLA_FUNCTION_FAILED);
  429. }
  430. mrk->entry_type = MARKER_TYPE;
  431. mrk->modifier = type;
  432. if (type != MK_SYNC_ALL) {
  433. if (IS_FWI2_CAPABLE(ha)) {
  434. mrk24 = (struct mrk_entry_24xx *) mrk;
  435. mrk24->nport_handle = cpu_to_le16(loop_id);
  436. mrk24->lun[1] = LSB(lun);
  437. mrk24->lun[2] = MSB(lun);
  438. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  439. mrk24->vp_index = vha->vp_idx;
  440. mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
  441. } else {
  442. SET_TARGET_ID(ha, mrk->target, loop_id);
  443. mrk->lun = cpu_to_le16(lun);
  444. }
  445. }
  446. wmb();
  447. qla2x00_isp_cmd(vha, req);
  448. return (QLA_SUCCESS);
  449. }
  450. int
  451. qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  452. struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
  453. uint8_t type)
  454. {
  455. int ret;
  456. unsigned long flags = 0;
  457. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  458. ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
  459. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  460. return (ret);
  461. }
  462. /**
  463. * qla2x00_isp_cmd() - Modify the request ring pointer.
  464. * @ha: HA context
  465. *
  466. * Note: The caller must hold the hardware lock before calling this routine.
  467. */
  468. static void
  469. qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
  470. {
  471. struct qla_hw_data *ha = vha->hw;
  472. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  473. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  474. DEBUG5(printk("%s(): IOCB data:\n", __func__));
  475. DEBUG5(qla2x00_dump_buffer(
  476. (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
  477. /* Adjust ring index. */
  478. req->ring_index++;
  479. if (req->ring_index == req->length) {
  480. req->ring_index = 0;
  481. req->ring_ptr = req->ring;
  482. } else
  483. req->ring_ptr++;
  484. /* Set chip new ring index. */
  485. if (IS_QLA82XX(ha)) {
  486. uint32_t dbval = 0x04 | (ha->portnum << 5);
  487. /* write, read and verify logic */
  488. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  489. if (ql2xdbwr)
  490. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  491. else {
  492. WRT_REG_DWORD(
  493. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  494. dbval);
  495. wmb();
  496. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  497. WRT_REG_DWORD((unsigned long __iomem *)
  498. ha->nxdb_wr_ptr, dbval);
  499. wmb();
  500. }
  501. }
  502. } else if (ha->mqenable) {
  503. /* Set chip new ring index. */
  504. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  505. RD_REG_DWORD(&ioreg->hccr);
  506. } else {
  507. if (IS_FWI2_CAPABLE(ha)) {
  508. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  509. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  510. } else {
  511. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  512. req->ring_index);
  513. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  514. }
  515. }
  516. }
  517. /**
  518. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  519. * Continuation Type 1 IOCBs to allocate.
  520. *
  521. * @dsds: number of data segment decriptors needed
  522. *
  523. * Returns the number of IOCB entries needed to store @dsds.
  524. */
  525. inline uint16_t
  526. qla24xx_calc_iocbs(uint16_t dsds)
  527. {
  528. uint16_t iocbs;
  529. iocbs = 1;
  530. if (dsds > 1) {
  531. iocbs += (dsds - 1) / 5;
  532. if ((dsds - 1) % 5)
  533. iocbs++;
  534. }
  535. DEBUG3(printk(KERN_DEBUG "%s(): Required PKT(s) = %d\n",
  536. __func__, iocbs));
  537. return iocbs;
  538. }
  539. /**
  540. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  541. * IOCB types.
  542. *
  543. * @sp: SRB command to process
  544. * @cmd_pkt: Command type 3 IOCB
  545. * @tot_dsds: Total number of segments to transfer
  546. */
  547. inline void
  548. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  549. uint16_t tot_dsds)
  550. {
  551. uint16_t avail_dsds;
  552. uint32_t *cur_dsd;
  553. scsi_qla_host_t *vha;
  554. struct scsi_cmnd *cmd;
  555. struct scatterlist *sg;
  556. int i;
  557. struct req_que *req;
  558. cmd = sp->cmd;
  559. /* Update entry type to indicate Command Type 3 IOCB */
  560. *((uint32_t *)(&cmd_pkt->entry_type)) =
  561. __constant_cpu_to_le32(COMMAND_TYPE_7);
  562. /* No data transfer */
  563. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  564. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  565. return;
  566. }
  567. vha = sp->fcport->vha;
  568. req = vha->req;
  569. /* Set transfer direction */
  570. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  571. cmd_pkt->task_mgmt_flags =
  572. __constant_cpu_to_le16(TMF_WRITE_DATA);
  573. sp->fcport->vha->hw->qla_stats.output_bytes +=
  574. scsi_bufflen(sp->cmd);
  575. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  576. cmd_pkt->task_mgmt_flags =
  577. __constant_cpu_to_le16(TMF_READ_DATA);
  578. sp->fcport->vha->hw->qla_stats.input_bytes +=
  579. scsi_bufflen(sp->cmd);
  580. }
  581. /* One DSD is available in the Command Type 3 IOCB */
  582. avail_dsds = 1;
  583. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  584. /* Load data segments */
  585. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  586. dma_addr_t sle_dma;
  587. cont_a64_entry_t *cont_pkt;
  588. /* Allocate additional continuation packets? */
  589. if (avail_dsds == 0) {
  590. /*
  591. * Five DSDs are available in the Continuation
  592. * Type 1 IOCB.
  593. */
  594. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  595. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  596. avail_dsds = 5;
  597. }
  598. sle_dma = sg_dma_address(sg);
  599. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  600. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  601. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  602. avail_dsds--;
  603. }
  604. }
  605. struct fw_dif_context {
  606. uint32_t ref_tag;
  607. uint16_t app_tag;
  608. uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
  609. uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
  610. };
  611. /*
  612. * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
  613. *
  614. */
  615. static inline void
  616. qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
  617. unsigned int protcnt)
  618. {
  619. struct sd_dif_tuple *spt;
  620. unsigned char op = scsi_get_prot_op(cmd);
  621. switch (scsi_get_prot_type(cmd)) {
  622. /* For TYPE 0 protection: no checking */
  623. case SCSI_PROT_DIF_TYPE0:
  624. pkt->ref_tag_mask[0] = 0x00;
  625. pkt->ref_tag_mask[1] = 0x00;
  626. pkt->ref_tag_mask[2] = 0x00;
  627. pkt->ref_tag_mask[3] = 0x00;
  628. break;
  629. /*
  630. * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
  631. * match LBA in CDB + N
  632. */
  633. case SCSI_PROT_DIF_TYPE2:
  634. if (!ql2xenablehba_err_chk)
  635. break;
  636. if (scsi_prot_sg_count(cmd)) {
  637. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  638. scsi_prot_sglist(cmd)[0].offset;
  639. pkt->app_tag = swab32(spt->app_tag);
  640. pkt->app_tag_mask[0] = 0xff;
  641. pkt->app_tag_mask[1] = 0xff;
  642. }
  643. pkt->ref_tag = cpu_to_le32((uint32_t)
  644. (0xffffffff & scsi_get_lba(cmd)));
  645. /* enable ALL bytes of the ref tag */
  646. pkt->ref_tag_mask[0] = 0xff;
  647. pkt->ref_tag_mask[1] = 0xff;
  648. pkt->ref_tag_mask[2] = 0xff;
  649. pkt->ref_tag_mask[3] = 0xff;
  650. break;
  651. /* For Type 3 protection: 16 bit GUARD only */
  652. case SCSI_PROT_DIF_TYPE3:
  653. pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
  654. pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
  655. 0x00;
  656. break;
  657. /*
  658. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  659. * 16 bit app tag.
  660. */
  661. case SCSI_PROT_DIF_TYPE1:
  662. if (!ql2xenablehba_err_chk)
  663. break;
  664. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  665. op == SCSI_PROT_WRITE_PASS)) {
  666. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  667. scsi_prot_sglist(cmd)[0].offset;
  668. DEBUG18(printk(KERN_DEBUG
  669. "%s(): LBA from user %p, lba = 0x%x\n",
  670. __func__, spt, (int)spt->ref_tag));
  671. pkt->ref_tag = swab32(spt->ref_tag);
  672. pkt->app_tag_mask[0] = 0x0;
  673. pkt->app_tag_mask[1] = 0x0;
  674. } else {
  675. pkt->ref_tag = cpu_to_le32((uint32_t)
  676. (0xffffffff & scsi_get_lba(cmd)));
  677. pkt->app_tag = __constant_cpu_to_le16(0);
  678. pkt->app_tag_mask[0] = 0x0;
  679. pkt->app_tag_mask[1] = 0x0;
  680. }
  681. /* enable ALL bytes of the ref tag */
  682. pkt->ref_tag_mask[0] = 0xff;
  683. pkt->ref_tag_mask[1] = 0xff;
  684. pkt->ref_tag_mask[2] = 0xff;
  685. pkt->ref_tag_mask[3] = 0xff;
  686. break;
  687. }
  688. DEBUG18(printk(KERN_DEBUG
  689. "%s(): Setting protection Tags: (BIG) ref tag = 0x%x,"
  690. " app tag = 0x%x, prot SG count %d , cmd lba 0x%x,"
  691. " prot_type=%u\n", __func__, pkt->ref_tag, pkt->app_tag, protcnt,
  692. (int)scsi_get_lba(cmd), scsi_get_prot_type(cmd)));
  693. }
  694. static int
  695. qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
  696. uint16_t tot_dsds)
  697. {
  698. void *next_dsd;
  699. uint8_t avail_dsds = 0;
  700. uint32_t dsd_list_len;
  701. struct dsd_dma *dsd_ptr;
  702. struct scatterlist *sg;
  703. uint32_t *cur_dsd = dsd;
  704. int i;
  705. uint16_t used_dsds = tot_dsds;
  706. uint8_t *cp;
  707. scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
  708. dma_addr_t sle_dma;
  709. /* Allocate additional continuation packets? */
  710. if (avail_dsds == 0) {
  711. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  712. QLA_DSDS_PER_IOCB : used_dsds;
  713. dsd_list_len = (avail_dsds + 1) * 12;
  714. used_dsds -= avail_dsds;
  715. /* allocate tracking DS */
  716. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  717. if (!dsd_ptr)
  718. return 1;
  719. /* allocate new list */
  720. dsd_ptr->dsd_addr = next_dsd =
  721. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  722. &dsd_ptr->dsd_list_dma);
  723. if (!next_dsd) {
  724. /*
  725. * Need to cleanup only this dsd_ptr, rest
  726. * will be done by sp_free_dma()
  727. */
  728. kfree(dsd_ptr);
  729. return 1;
  730. }
  731. list_add_tail(&dsd_ptr->list,
  732. &((struct crc_context *)sp->ctx)->dsd_list);
  733. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  734. /* add new list to cmd iocb or last list */
  735. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  736. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  737. *cur_dsd++ = dsd_list_len;
  738. cur_dsd = (uint32_t *)next_dsd;
  739. }
  740. sle_dma = sg_dma_address(sg);
  741. DEBUG18(printk("%s(): %p, sg entry %d - addr =0x%x 0x%x,"
  742. " len =%d\n", __func__ , cur_dsd, i, LSD(sle_dma),
  743. MSD(sle_dma), sg_dma_len(sg)));
  744. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  745. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  746. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  747. avail_dsds--;
  748. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  749. cp = page_address(sg_page(sg)) + sg->offset;
  750. DEBUG18(printk("%s(): User Data buffer= %p:\n",
  751. __func__ , cp));
  752. }
  753. }
  754. /* Null termination */
  755. *cur_dsd++ = 0;
  756. *cur_dsd++ = 0;
  757. *cur_dsd++ = 0;
  758. return 0;
  759. }
  760. static int
  761. qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
  762. uint32_t *dsd,
  763. uint16_t tot_dsds)
  764. {
  765. void *next_dsd;
  766. uint8_t avail_dsds = 0;
  767. uint32_t dsd_list_len;
  768. struct dsd_dma *dsd_ptr;
  769. struct scatterlist *sg;
  770. int i;
  771. struct scsi_cmnd *cmd;
  772. uint32_t *cur_dsd = dsd;
  773. uint16_t used_dsds = tot_dsds;
  774. uint8_t *cp;
  775. cmd = sp->cmd;
  776. scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
  777. dma_addr_t sle_dma;
  778. /* Allocate additional continuation packets? */
  779. if (avail_dsds == 0) {
  780. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  781. QLA_DSDS_PER_IOCB : used_dsds;
  782. dsd_list_len = (avail_dsds + 1) * 12;
  783. used_dsds -= avail_dsds;
  784. /* allocate tracking DS */
  785. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  786. if (!dsd_ptr)
  787. return 1;
  788. /* allocate new list */
  789. dsd_ptr->dsd_addr = next_dsd =
  790. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  791. &dsd_ptr->dsd_list_dma);
  792. if (!next_dsd) {
  793. /*
  794. * Need to cleanup only this dsd_ptr, rest
  795. * will be done by sp_free_dma()
  796. */
  797. kfree(dsd_ptr);
  798. return 1;
  799. }
  800. list_add_tail(&dsd_ptr->list,
  801. &((struct crc_context *)sp->ctx)->dsd_list);
  802. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  803. /* add new list to cmd iocb or last list */
  804. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  805. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  806. *cur_dsd++ = dsd_list_len;
  807. cur_dsd = (uint32_t *)next_dsd;
  808. }
  809. sle_dma = sg_dma_address(sg);
  810. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  811. DEBUG18(printk(KERN_DEBUG
  812. "%s(): %p, sg entry %d - addr =0x%x"
  813. "0x%x, len =%d\n", __func__ , cur_dsd, i,
  814. LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg)));
  815. }
  816. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  817. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  818. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  819. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  820. cp = page_address(sg_page(sg)) + sg->offset;
  821. DEBUG18(printk("%s(): Protection Data buffer = %p:\n",
  822. __func__ , cp));
  823. }
  824. avail_dsds--;
  825. }
  826. /* Null termination */
  827. *cur_dsd++ = 0;
  828. *cur_dsd++ = 0;
  829. *cur_dsd++ = 0;
  830. return 0;
  831. }
  832. /**
  833. * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
  834. * Type 6 IOCB types.
  835. *
  836. * @sp: SRB command to process
  837. * @cmd_pkt: Command type 3 IOCB
  838. * @tot_dsds: Total number of segments to transfer
  839. */
  840. static inline int
  841. qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
  842. uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
  843. {
  844. uint32_t *cur_dsd, *fcp_dl;
  845. scsi_qla_host_t *vha;
  846. struct scsi_cmnd *cmd;
  847. struct scatterlist *cur_seg;
  848. int sgc;
  849. uint32_t total_bytes;
  850. uint32_t data_bytes;
  851. uint32_t dif_bytes;
  852. uint8_t bundling = 1;
  853. uint16_t blk_size;
  854. uint8_t *clr_ptr;
  855. struct crc_context *crc_ctx_pkt = NULL;
  856. struct qla_hw_data *ha;
  857. uint8_t additional_fcpcdb_len;
  858. uint16_t fcp_cmnd_len;
  859. struct fcp_cmnd *fcp_cmnd;
  860. dma_addr_t crc_ctx_dma;
  861. char tag[2];
  862. cmd = sp->cmd;
  863. sgc = 0;
  864. /* Update entry type to indicate Command Type CRC_2 IOCB */
  865. *((uint32_t *)(&cmd_pkt->entry_type)) =
  866. __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
  867. /* No data transfer */
  868. data_bytes = scsi_bufflen(cmd);
  869. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  870. DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
  871. __func__, data_bytes));
  872. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  873. return QLA_SUCCESS;
  874. }
  875. vha = sp->fcport->vha;
  876. ha = vha->hw;
  877. DEBUG18(printk(KERN_DEBUG
  878. "%s(%ld): Executing cmd sp %p, prot_op=%u.\n", __func__,
  879. vha->host_no, sp, scsi_get_prot_op(sp->cmd)));
  880. cmd_pkt->vp_index = sp->fcport->vp_idx;
  881. /* Set transfer direction */
  882. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  883. cmd_pkt->control_flags =
  884. __constant_cpu_to_le16(CF_WRITE_DATA);
  885. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  886. cmd_pkt->control_flags =
  887. __constant_cpu_to_le16(CF_READ_DATA);
  888. }
  889. tot_prot_dsds = scsi_prot_sg_count(cmd);
  890. if (!tot_prot_dsds)
  891. bundling = 0;
  892. /* Allocate CRC context from global pool */
  893. crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
  894. GFP_ATOMIC, &crc_ctx_dma);
  895. if (!crc_ctx_pkt)
  896. goto crc_queuing_error;
  897. /* Zero out CTX area. */
  898. clr_ptr = (uint8_t *)crc_ctx_pkt;
  899. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  900. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  901. sp->flags |= SRB_CRC_CTX_DMA_VALID;
  902. /* Set handle */
  903. crc_ctx_pkt->handle = cmd_pkt->handle;
  904. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  905. qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
  906. &crc_ctx_pkt->ref_tag, tot_prot_dsds);
  907. cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  908. cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  909. cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  910. /* Determine SCSI command length -- align to 4 byte boundary */
  911. if (cmd->cmd_len > 16) {
  912. DEBUG18(printk(KERN_INFO "%s(): **** SCSI CMD > 16\n",
  913. __func__));
  914. additional_fcpcdb_len = cmd->cmd_len - 16;
  915. if ((cmd->cmd_len % 4) != 0) {
  916. /* SCSI cmd > 16 bytes must be multiple of 4 */
  917. goto crc_queuing_error;
  918. }
  919. fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  920. } else {
  921. additional_fcpcdb_len = 0;
  922. fcp_cmnd_len = 12 + 16 + 4;
  923. }
  924. fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
  925. fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
  926. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  927. fcp_cmnd->additional_cdb_len |= 1;
  928. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  929. fcp_cmnd->additional_cdb_len |= 2;
  930. int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
  931. host_to_fcp_swap((uint8_t *)&fcp_cmnd->lun, sizeof(fcp_cmnd->lun));
  932. memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  933. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
  934. cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
  935. LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  936. cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
  937. MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  938. fcp_cmnd->task_management = 0;
  939. /*
  940. * Update tagged queuing modifier if using command tag queuing
  941. */
  942. if (scsi_populate_tag_msg(cmd, tag)) {
  943. switch (tag[0]) {
  944. case HEAD_OF_QUEUE_TAG:
  945. fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
  946. break;
  947. case ORDERED_QUEUE_TAG:
  948. fcp_cmnd->task_attribute = TSK_ORDERED;
  949. break;
  950. default:
  951. fcp_cmnd->task_attribute = 0;
  952. break;
  953. }
  954. } else {
  955. fcp_cmnd->task_attribute = 0;
  956. }
  957. cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
  958. DEBUG18(printk(KERN_INFO "%s(%ld): Total SG(s) Entries %d, Data"
  959. "entries %d, data bytes %d, Protection entries %d\n",
  960. __func__, vha->host_no, tot_dsds, (tot_dsds-tot_prot_dsds),
  961. data_bytes, tot_prot_dsds));
  962. /* Compute dif len and adjust data len to incude protection */
  963. total_bytes = data_bytes;
  964. dif_bytes = 0;
  965. blk_size = cmd->device->sector_size;
  966. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  967. dif_bytes = (data_bytes / blk_size) * 8;
  968. total_bytes += dif_bytes;
  969. }
  970. if (!ql2xenablehba_err_chk)
  971. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  972. if (!bundling) {
  973. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  974. } else {
  975. /*
  976. * Configure Bundling if we need to fetch interlaving
  977. * protection PCI accesses
  978. */
  979. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  980. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  981. crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
  982. tot_prot_dsds);
  983. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  984. }
  985. /* Finish the common fields of CRC pkt */
  986. crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
  987. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  988. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  989. crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
  990. /* Fibre channel byte count */
  991. cmd_pkt->byte_count = cpu_to_le32(total_bytes);
  992. fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
  993. additional_fcpcdb_len);
  994. *fcp_dl = htonl(total_bytes);
  995. DEBUG18(printk(KERN_INFO "%s(%ld): dif bytes = 0x%x (%d), total bytes"
  996. " = 0x%x (%d), dat block size =0x%x (%d)\n", __func__,
  997. vha->host_no, dif_bytes, dif_bytes, total_bytes, total_bytes,
  998. crc_ctx_pkt->blk_size, crc_ctx_pkt->blk_size));
  999. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  1000. DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
  1001. __func__, data_bytes));
  1002. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  1003. return QLA_SUCCESS;
  1004. }
  1005. /* Walks data segments */
  1006. cmd_pkt->control_flags |=
  1007. __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
  1008. if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
  1009. (tot_dsds - tot_prot_dsds)))
  1010. goto crc_queuing_error;
  1011. if (bundling && tot_prot_dsds) {
  1012. /* Walks dif segments */
  1013. cur_seg = scsi_prot_sglist(cmd);
  1014. cmd_pkt->control_flags |=
  1015. __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  1016. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  1017. if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
  1018. tot_prot_dsds))
  1019. goto crc_queuing_error;
  1020. }
  1021. return QLA_SUCCESS;
  1022. crc_queuing_error:
  1023. DEBUG18(qla_printk(KERN_INFO, ha,
  1024. "CMD sent FAILED crc_q error:sp = %p\n", sp));
  1025. /* Cleanup will be performed by the caller */
  1026. return QLA_FUNCTION_FAILED;
  1027. }
  1028. /**
  1029. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  1030. * @sp: command to send to the ISP
  1031. *
  1032. * Returns non-zero if a failure occurred, else zero.
  1033. */
  1034. int
  1035. qla24xx_start_scsi(srb_t *sp)
  1036. {
  1037. int ret, nseg;
  1038. unsigned long flags;
  1039. uint32_t *clr_ptr;
  1040. uint32_t index;
  1041. uint32_t handle;
  1042. struct cmd_type_7 *cmd_pkt;
  1043. uint16_t cnt;
  1044. uint16_t req_cnt;
  1045. uint16_t tot_dsds;
  1046. struct req_que *req = NULL;
  1047. struct rsp_que *rsp = NULL;
  1048. struct scsi_cmnd *cmd = sp->cmd;
  1049. struct scsi_qla_host *vha = sp->fcport->vha;
  1050. struct qla_hw_data *ha = vha->hw;
  1051. char tag[2];
  1052. /* Setup device pointers. */
  1053. ret = 0;
  1054. qla25xx_set_que(sp, &rsp);
  1055. req = vha->req;
  1056. /* So we know we haven't pci_map'ed anything yet */
  1057. tot_dsds = 0;
  1058. /* Send marker if required */
  1059. if (vha->marker_needed != 0) {
  1060. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
  1061. != QLA_SUCCESS)
  1062. return QLA_FUNCTION_FAILED;
  1063. vha->marker_needed = 0;
  1064. }
  1065. /* Acquire ring specific lock */
  1066. spin_lock_irqsave(&ha->hardware_lock, flags);
  1067. /* Check for room in outstanding command list. */
  1068. handle = req->current_outstanding_cmd;
  1069. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1070. handle++;
  1071. if (handle == MAX_OUTSTANDING_COMMANDS)
  1072. handle = 1;
  1073. if (!req->outstanding_cmds[handle])
  1074. break;
  1075. }
  1076. if (index == MAX_OUTSTANDING_COMMANDS)
  1077. goto queuing_error;
  1078. /* Map the sg table so we have an accurate count of sg entries needed */
  1079. if (scsi_sg_count(cmd)) {
  1080. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1081. scsi_sg_count(cmd), cmd->sc_data_direction);
  1082. if (unlikely(!nseg))
  1083. goto queuing_error;
  1084. } else
  1085. nseg = 0;
  1086. tot_dsds = nseg;
  1087. req_cnt = qla24xx_calc_iocbs(tot_dsds);
  1088. if (req->cnt < (req_cnt + 2)) {
  1089. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1090. if (req->ring_index < cnt)
  1091. req->cnt = cnt - req->ring_index;
  1092. else
  1093. req->cnt = req->length -
  1094. (req->ring_index - cnt);
  1095. }
  1096. if (req->cnt < (req_cnt + 2))
  1097. goto queuing_error;
  1098. /* Build command packet. */
  1099. req->current_outstanding_cmd = handle;
  1100. req->outstanding_cmds[handle] = sp;
  1101. sp->handle = handle;
  1102. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1103. req->cnt -= req_cnt;
  1104. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  1105. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1106. /* Zero out remaining portion of packet. */
  1107. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1108. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1109. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1110. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1111. /* Set NPORT-ID and LUN number*/
  1112. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1113. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1114. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1115. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1116. cmd_pkt->vp_index = sp->fcport->vp_idx;
  1117. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1118. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1119. /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1120. if (scsi_populate_tag_msg(cmd, tag)) {
  1121. switch (tag[0]) {
  1122. case HEAD_OF_QUEUE_TAG:
  1123. cmd_pkt->task = TSK_HEAD_OF_QUEUE;
  1124. break;
  1125. case ORDERED_QUEUE_TAG:
  1126. cmd_pkt->task = TSK_ORDERED;
  1127. break;
  1128. }
  1129. }
  1130. /* Load SCSI command packet. */
  1131. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  1132. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  1133. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  1134. /* Build IOCB segments */
  1135. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  1136. /* Set total data segment count. */
  1137. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1138. /* Specify response queue number where completion should happen */
  1139. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1140. wmb();
  1141. /* Adjust ring index. */
  1142. req->ring_index++;
  1143. if (req->ring_index == req->length) {
  1144. req->ring_index = 0;
  1145. req->ring_ptr = req->ring;
  1146. } else
  1147. req->ring_ptr++;
  1148. sp->flags |= SRB_DMA_VALID;
  1149. /* Set chip new ring index. */
  1150. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1151. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1152. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1153. if (vha->flags.process_response_queue &&
  1154. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1155. qla24xx_process_response_queue(vha, rsp);
  1156. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1157. return QLA_SUCCESS;
  1158. queuing_error:
  1159. if (tot_dsds)
  1160. scsi_dma_unmap(cmd);
  1161. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1162. return QLA_FUNCTION_FAILED;
  1163. }
  1164. /**
  1165. * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
  1166. * @sp: command to send to the ISP
  1167. *
  1168. * Returns non-zero if a failure occurred, else zero.
  1169. */
  1170. int
  1171. qla24xx_dif_start_scsi(srb_t *sp)
  1172. {
  1173. int nseg;
  1174. unsigned long flags;
  1175. uint32_t *clr_ptr;
  1176. uint32_t index;
  1177. uint32_t handle;
  1178. uint16_t cnt;
  1179. uint16_t req_cnt = 0;
  1180. uint16_t tot_dsds;
  1181. uint16_t tot_prot_dsds;
  1182. uint16_t fw_prot_opts = 0;
  1183. struct req_que *req = NULL;
  1184. struct rsp_que *rsp = NULL;
  1185. struct scsi_cmnd *cmd = sp->cmd;
  1186. struct scsi_qla_host *vha = sp->fcport->vha;
  1187. struct qla_hw_data *ha = vha->hw;
  1188. struct cmd_type_crc_2 *cmd_pkt;
  1189. uint32_t status = 0;
  1190. #define QDSS_GOT_Q_SPACE BIT_0
  1191. /* Only process protection or >16 cdb in this routine */
  1192. if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  1193. if (cmd->cmd_len <= 16)
  1194. return qla24xx_start_scsi(sp);
  1195. }
  1196. /* Setup device pointers. */
  1197. qla25xx_set_que(sp, &rsp);
  1198. req = vha->req;
  1199. /* So we know we haven't pci_map'ed anything yet */
  1200. tot_dsds = 0;
  1201. /* Send marker if required */
  1202. if (vha->marker_needed != 0) {
  1203. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1204. QLA_SUCCESS)
  1205. return QLA_FUNCTION_FAILED;
  1206. vha->marker_needed = 0;
  1207. }
  1208. /* Acquire ring specific lock */
  1209. spin_lock_irqsave(&ha->hardware_lock, flags);
  1210. /* Check for room in outstanding command list. */
  1211. handle = req->current_outstanding_cmd;
  1212. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1213. handle++;
  1214. if (handle == MAX_OUTSTANDING_COMMANDS)
  1215. handle = 1;
  1216. if (!req->outstanding_cmds[handle])
  1217. break;
  1218. }
  1219. if (index == MAX_OUTSTANDING_COMMANDS)
  1220. goto queuing_error;
  1221. /* Compute number of required data segments */
  1222. /* Map the sg table so we have an accurate count of sg entries needed */
  1223. if (scsi_sg_count(cmd)) {
  1224. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1225. scsi_sg_count(cmd), cmd->sc_data_direction);
  1226. if (unlikely(!nseg))
  1227. goto queuing_error;
  1228. else
  1229. sp->flags |= SRB_DMA_VALID;
  1230. } else
  1231. nseg = 0;
  1232. /* number of required data segments */
  1233. tot_dsds = nseg;
  1234. /* Compute number of required protection segments */
  1235. if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  1236. nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
  1237. scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  1238. if (unlikely(!nseg))
  1239. goto queuing_error;
  1240. else
  1241. sp->flags |= SRB_CRC_PROT_DMA_VALID;
  1242. } else {
  1243. nseg = 0;
  1244. }
  1245. req_cnt = 1;
  1246. /* Total Data and protection sg segment(s) */
  1247. tot_prot_dsds = nseg;
  1248. tot_dsds += nseg;
  1249. if (req->cnt < (req_cnt + 2)) {
  1250. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1251. if (req->ring_index < cnt)
  1252. req->cnt = cnt - req->ring_index;
  1253. else
  1254. req->cnt = req->length -
  1255. (req->ring_index - cnt);
  1256. }
  1257. if (req->cnt < (req_cnt + 2))
  1258. goto queuing_error;
  1259. status |= QDSS_GOT_Q_SPACE;
  1260. /* Build header part of command packet (excluding the OPCODE). */
  1261. req->current_outstanding_cmd = handle;
  1262. req->outstanding_cmds[handle] = sp;
  1263. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1264. req->cnt -= req_cnt;
  1265. /* Fill-in common area */
  1266. cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
  1267. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1268. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1269. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1270. /* Set NPORT-ID and LUN number*/
  1271. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1272. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1273. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1274. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1275. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1276. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1277. /* Total Data and protection segment(s) */
  1278. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1279. /* Build IOCB segments and adjust for data protection segments */
  1280. if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
  1281. req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  1282. QLA_SUCCESS)
  1283. goto queuing_error;
  1284. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1285. /* Specify response queue number where completion should happen */
  1286. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1287. cmd_pkt->timeout = __constant_cpu_to_le16(0);
  1288. wmb();
  1289. /* Adjust ring index. */
  1290. req->ring_index++;
  1291. if (req->ring_index == req->length) {
  1292. req->ring_index = 0;
  1293. req->ring_ptr = req->ring;
  1294. } else
  1295. req->ring_ptr++;
  1296. /* Set chip new ring index. */
  1297. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1298. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1299. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1300. if (vha->flags.process_response_queue &&
  1301. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1302. qla24xx_process_response_queue(vha, rsp);
  1303. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1304. return QLA_SUCCESS;
  1305. queuing_error:
  1306. if (status & QDSS_GOT_Q_SPACE) {
  1307. req->outstanding_cmds[handle] = NULL;
  1308. req->cnt += req_cnt;
  1309. }
  1310. /* Cleanup will be performed by the caller (queuecommand) */
  1311. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1312. DEBUG18(qla_printk(KERN_INFO, ha,
  1313. "CMD sent FAILED SCSI prot_op:%02x\n", scsi_get_prot_op(cmd)));
  1314. return QLA_FUNCTION_FAILED;
  1315. }
  1316. static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
  1317. {
  1318. struct scsi_cmnd *cmd = sp->cmd;
  1319. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1320. int affinity = cmd->request->cpu;
  1321. if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
  1322. affinity < ha->max_rsp_queues - 1)
  1323. *rsp = ha->rsp_q_map[affinity + 1];
  1324. else
  1325. *rsp = ha->rsp_q_map[0];
  1326. }
  1327. /* Generic Control-SRB manipulation functions. */
  1328. void *
  1329. qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
  1330. {
  1331. struct qla_hw_data *ha = vha->hw;
  1332. struct req_que *req = ha->req_q_map[0];
  1333. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1334. uint32_t index, handle;
  1335. request_t *pkt;
  1336. uint16_t cnt, req_cnt;
  1337. pkt = NULL;
  1338. req_cnt = 1;
  1339. handle = 0;
  1340. if (!sp)
  1341. goto skip_cmd_array;
  1342. /* Check for room in outstanding command list. */
  1343. handle = req->current_outstanding_cmd;
  1344. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1345. handle++;
  1346. if (handle == MAX_OUTSTANDING_COMMANDS)
  1347. handle = 1;
  1348. if (!req->outstanding_cmds[handle])
  1349. break;
  1350. }
  1351. if (index == MAX_OUTSTANDING_COMMANDS)
  1352. goto queuing_error;
  1353. /* Prep command array. */
  1354. req->current_outstanding_cmd = handle;
  1355. req->outstanding_cmds[handle] = sp;
  1356. sp->handle = handle;
  1357. skip_cmd_array:
  1358. /* Check for room on request queue. */
  1359. if (req->cnt < req_cnt) {
  1360. if (ha->mqenable)
  1361. cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
  1362. else if (IS_QLA82XX(ha))
  1363. cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
  1364. else if (IS_FWI2_CAPABLE(ha))
  1365. cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
  1366. else
  1367. cnt = qla2x00_debounce_register(
  1368. ISP_REQ_Q_OUT(ha, &reg->isp));
  1369. if (req->ring_index < cnt)
  1370. req->cnt = cnt - req->ring_index;
  1371. else
  1372. req->cnt = req->length -
  1373. (req->ring_index - cnt);
  1374. }
  1375. if (req->cnt < req_cnt)
  1376. goto queuing_error;
  1377. /* Prep packet */
  1378. req->cnt -= req_cnt;
  1379. pkt = req->ring_ptr;
  1380. memset(pkt, 0, REQUEST_ENTRY_SIZE);
  1381. pkt->entry_count = req_cnt;
  1382. pkt->handle = handle;
  1383. queuing_error:
  1384. return pkt;
  1385. }
  1386. static void
  1387. qla2x00_start_iocbs(srb_t *sp)
  1388. {
  1389. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1390. struct req_que *req = ha->req_q_map[0];
  1391. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1392. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  1393. if (IS_QLA82XX(ha)) {
  1394. qla82xx_start_iocbs(sp);
  1395. } else {
  1396. /* Adjust ring index. */
  1397. req->ring_index++;
  1398. if (req->ring_index == req->length) {
  1399. req->ring_index = 0;
  1400. req->ring_ptr = req->ring;
  1401. } else
  1402. req->ring_ptr++;
  1403. /* Set chip new ring index. */
  1404. if (ha->mqenable) {
  1405. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  1406. RD_REG_DWORD(&ioreg->hccr);
  1407. } else if (IS_QLA82XX(ha)) {
  1408. qla82xx_start_iocbs(sp);
  1409. } else if (IS_FWI2_CAPABLE(ha)) {
  1410. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  1411. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  1412. } else {
  1413. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  1414. req->ring_index);
  1415. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  1416. }
  1417. }
  1418. }
  1419. static void
  1420. qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1421. {
  1422. struct srb_ctx *ctx = sp->ctx;
  1423. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1424. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1425. logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  1426. if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
  1427. logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  1428. if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
  1429. logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  1430. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1431. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1432. logio->port_id[1] = sp->fcport->d_id.b.area;
  1433. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1434. logio->vp_index = sp->fcport->vp_idx;
  1435. }
  1436. static void
  1437. qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
  1438. {
  1439. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1440. struct srb_ctx *ctx = sp->ctx;
  1441. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1442. uint16_t opts;
  1443. mbx->entry_type = MBX_IOCB_TYPE;
  1444. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1445. mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
  1446. opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
  1447. opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
  1448. if (HAS_EXTENDED_IDS(ha)) {
  1449. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1450. mbx->mb10 = cpu_to_le16(opts);
  1451. } else {
  1452. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
  1453. }
  1454. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1455. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1456. sp->fcport->d_id.b.al_pa);
  1457. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1458. }
  1459. static void
  1460. qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1461. {
  1462. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1463. logio->control_flags =
  1464. cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
  1465. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1466. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1467. logio->port_id[1] = sp->fcport->d_id.b.area;
  1468. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1469. logio->vp_index = sp->fcport->vp_idx;
  1470. }
  1471. static void
  1472. qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
  1473. {
  1474. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1475. mbx->entry_type = MBX_IOCB_TYPE;
  1476. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1477. mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
  1478. mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
  1479. cpu_to_le16(sp->fcport->loop_id):
  1480. cpu_to_le16(sp->fcport->loop_id << 8);
  1481. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1482. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1483. sp->fcport->d_id.b.al_pa);
  1484. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1485. /* Implicit: mbx->mbx10 = 0. */
  1486. }
  1487. static void
  1488. qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1489. {
  1490. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1491. logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
  1492. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1493. logio->vp_index = sp->fcport->vp_idx;
  1494. }
  1495. static void
  1496. qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
  1497. {
  1498. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1499. mbx->entry_type = MBX_IOCB_TYPE;
  1500. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1501. mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
  1502. if (HAS_EXTENDED_IDS(ha)) {
  1503. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1504. mbx->mb10 = cpu_to_le16(BIT_0);
  1505. } else {
  1506. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
  1507. }
  1508. mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
  1509. mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
  1510. mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
  1511. mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
  1512. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1513. }
  1514. static void
  1515. qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
  1516. {
  1517. uint32_t flags;
  1518. unsigned int lun;
  1519. struct fc_port *fcport = sp->fcport;
  1520. scsi_qla_host_t *vha = fcport->vha;
  1521. struct qla_hw_data *ha = vha->hw;
  1522. struct srb_ctx *ctx = sp->ctx;
  1523. struct srb_iocb *iocb = ctx->u.iocb_cmd;
  1524. struct req_que *req = vha->req;
  1525. flags = iocb->u.tmf.flags;
  1526. lun = iocb->u.tmf.lun;
  1527. tsk->entry_type = TSK_MGMT_IOCB_TYPE;
  1528. tsk->entry_count = 1;
  1529. tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
  1530. tsk->nport_handle = cpu_to_le16(fcport->loop_id);
  1531. tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
  1532. tsk->control_flags = cpu_to_le32(flags);
  1533. tsk->port_id[0] = fcport->d_id.b.al_pa;
  1534. tsk->port_id[1] = fcport->d_id.b.area;
  1535. tsk->port_id[2] = fcport->d_id.b.domain;
  1536. tsk->vp_index = fcport->vp_idx;
  1537. if (flags == TCF_LUN_RESET) {
  1538. int_to_scsilun(lun, &tsk->lun);
  1539. host_to_fcp_swap((uint8_t *)&tsk->lun,
  1540. sizeof(tsk->lun));
  1541. }
  1542. }
  1543. static void
  1544. qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
  1545. {
  1546. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1547. els_iocb->entry_type = ELS_IOCB_TYPE;
  1548. els_iocb->entry_count = 1;
  1549. els_iocb->sys_define = 0;
  1550. els_iocb->entry_status = 0;
  1551. els_iocb->handle = sp->handle;
  1552. els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1553. els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1554. els_iocb->vp_index = sp->fcport->vp_idx;
  1555. els_iocb->sof_type = EST_SOFI3;
  1556. els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1557. els_iocb->opcode =
  1558. (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
  1559. bsg_job->request->rqst_data.r_els.els_code :
  1560. bsg_job->request->rqst_data.h_els.command_code;
  1561. els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
  1562. els_iocb->port_id[1] = sp->fcport->d_id.b.area;
  1563. els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
  1564. els_iocb->control_flags = 0;
  1565. els_iocb->rx_byte_count =
  1566. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1567. els_iocb->tx_byte_count =
  1568. cpu_to_le32(bsg_job->request_payload.payload_len);
  1569. els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1570. (bsg_job->request_payload.sg_list)));
  1571. els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1572. (bsg_job->request_payload.sg_list)));
  1573. els_iocb->tx_len = cpu_to_le32(sg_dma_len
  1574. (bsg_job->request_payload.sg_list));
  1575. els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1576. (bsg_job->reply_payload.sg_list)));
  1577. els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1578. (bsg_job->reply_payload.sg_list)));
  1579. els_iocb->rx_len = cpu_to_le32(sg_dma_len
  1580. (bsg_job->reply_payload.sg_list));
  1581. }
  1582. static void
  1583. qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
  1584. {
  1585. uint16_t avail_dsds;
  1586. uint32_t *cur_dsd;
  1587. struct scatterlist *sg;
  1588. int index;
  1589. uint16_t tot_dsds;
  1590. scsi_qla_host_t *vha = sp->fcport->vha;
  1591. struct qla_hw_data *ha = vha->hw;
  1592. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1593. int loop_iterartion = 0;
  1594. int cont_iocb_prsnt = 0;
  1595. int entry_count = 1;
  1596. memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
  1597. ct_iocb->entry_type = CT_IOCB_TYPE;
  1598. ct_iocb->entry_status = 0;
  1599. ct_iocb->handle1 = sp->handle;
  1600. SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
  1601. ct_iocb->status = __constant_cpu_to_le16(0);
  1602. ct_iocb->control_flags = __constant_cpu_to_le16(0);
  1603. ct_iocb->timeout = 0;
  1604. ct_iocb->cmd_dsd_count =
  1605. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1606. ct_iocb->total_dsd_count =
  1607. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
  1608. ct_iocb->req_bytecount =
  1609. cpu_to_le32(bsg_job->request_payload.payload_len);
  1610. ct_iocb->rsp_bytecount =
  1611. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1612. ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
  1613. (bsg_job->request_payload.sg_list)));
  1614. ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
  1615. (bsg_job->request_payload.sg_list)));
  1616. ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
  1617. ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
  1618. (bsg_job->reply_payload.sg_list)));
  1619. ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
  1620. (bsg_job->reply_payload.sg_list)));
  1621. ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
  1622. avail_dsds = 1;
  1623. cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
  1624. index = 0;
  1625. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1626. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1627. dma_addr_t sle_dma;
  1628. cont_a64_entry_t *cont_pkt;
  1629. /* Allocate additional continuation packets? */
  1630. if (avail_dsds == 0) {
  1631. /*
  1632. * Five DSDs are available in the Cont.
  1633. * Type 1 IOCB.
  1634. */
  1635. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1636. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1637. avail_dsds = 5;
  1638. cont_iocb_prsnt = 1;
  1639. entry_count++;
  1640. }
  1641. sle_dma = sg_dma_address(sg);
  1642. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1643. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1644. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1645. loop_iterartion++;
  1646. avail_dsds--;
  1647. }
  1648. ct_iocb->entry_count = entry_count;
  1649. }
  1650. static void
  1651. qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
  1652. {
  1653. uint16_t avail_dsds;
  1654. uint32_t *cur_dsd;
  1655. struct scatterlist *sg;
  1656. int index;
  1657. uint16_t tot_dsds;
  1658. scsi_qla_host_t *vha = sp->fcport->vha;
  1659. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1660. int loop_iterartion = 0;
  1661. int cont_iocb_prsnt = 0;
  1662. int entry_count = 1;
  1663. ct_iocb->entry_type = CT_IOCB_TYPE;
  1664. ct_iocb->entry_status = 0;
  1665. ct_iocb->sys_define = 0;
  1666. ct_iocb->handle = sp->handle;
  1667. ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1668. ct_iocb->vp_index = sp->fcport->vp_idx;
  1669. ct_iocb->comp_status = __constant_cpu_to_le16(0);
  1670. ct_iocb->cmd_dsd_count =
  1671. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1672. ct_iocb->timeout = 0;
  1673. ct_iocb->rsp_dsd_count =
  1674. __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1675. ct_iocb->rsp_byte_count =
  1676. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1677. ct_iocb->cmd_byte_count =
  1678. cpu_to_le32(bsg_job->request_payload.payload_len);
  1679. ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
  1680. (bsg_job->request_payload.sg_list)));
  1681. ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
  1682. (bsg_job->request_payload.sg_list)));
  1683. ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
  1684. (bsg_job->request_payload.sg_list));
  1685. avail_dsds = 1;
  1686. cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
  1687. index = 0;
  1688. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1689. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1690. dma_addr_t sle_dma;
  1691. cont_a64_entry_t *cont_pkt;
  1692. /* Allocate additional continuation packets? */
  1693. if (avail_dsds == 0) {
  1694. /*
  1695. * Five DSDs are available in the Cont.
  1696. * Type 1 IOCB.
  1697. */
  1698. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1699. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1700. avail_dsds = 5;
  1701. cont_iocb_prsnt = 1;
  1702. entry_count++;
  1703. }
  1704. sle_dma = sg_dma_address(sg);
  1705. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1706. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1707. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1708. loop_iterartion++;
  1709. avail_dsds--;
  1710. }
  1711. ct_iocb->entry_count = entry_count;
  1712. }
  1713. int
  1714. qla2x00_start_sp(srb_t *sp)
  1715. {
  1716. int rval;
  1717. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1718. void *pkt;
  1719. struct srb_ctx *ctx = sp->ctx;
  1720. unsigned long flags;
  1721. rval = QLA_FUNCTION_FAILED;
  1722. spin_lock_irqsave(&ha->hardware_lock, flags);
  1723. pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
  1724. if (!pkt)
  1725. goto done;
  1726. rval = QLA_SUCCESS;
  1727. switch (ctx->type) {
  1728. case SRB_LOGIN_CMD:
  1729. IS_FWI2_CAPABLE(ha) ?
  1730. qla24xx_login_iocb(sp, pkt) :
  1731. qla2x00_login_iocb(sp, pkt);
  1732. break;
  1733. case SRB_LOGOUT_CMD:
  1734. IS_FWI2_CAPABLE(ha) ?
  1735. qla24xx_logout_iocb(sp, pkt) :
  1736. qla2x00_logout_iocb(sp, pkt);
  1737. break;
  1738. case SRB_ELS_CMD_RPT:
  1739. case SRB_ELS_CMD_HST:
  1740. qla24xx_els_iocb(sp, pkt);
  1741. break;
  1742. case SRB_CT_CMD:
  1743. IS_FWI2_CAPABLE(ha) ?
  1744. qla24xx_ct_iocb(sp, pkt) :
  1745. qla2x00_ct_iocb(sp, pkt);
  1746. break;
  1747. case SRB_ADISC_CMD:
  1748. IS_FWI2_CAPABLE(ha) ?
  1749. qla24xx_adisc_iocb(sp, pkt) :
  1750. qla2x00_adisc_iocb(sp, pkt);
  1751. break;
  1752. case SRB_TM_CMD:
  1753. qla24xx_tm_iocb(sp, pkt);
  1754. break;
  1755. default:
  1756. break;
  1757. }
  1758. wmb();
  1759. qla2x00_start_iocbs(sp);
  1760. done:
  1761. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1762. return rval;
  1763. }