fc_fcp.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/err.h>
  28. #include <linux/crc32.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/fc/fc_fc2.h>
  35. #include <scsi/libfc.h>
  36. #include <scsi/fc_encode.h>
  37. MODULE_AUTHOR("Open-FCoE.org");
  38. MODULE_DESCRIPTION("libfc");
  39. MODULE_LICENSE("GPL v2");
  40. unsigned int fc_debug_logging;
  41. module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
  42. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  43. static struct kmem_cache *scsi_pkt_cachep;
  44. /* SRB state definitions */
  45. #define FC_SRB_FREE 0 /* cmd is free */
  46. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  47. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  48. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  49. #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
  50. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  51. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  52. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  53. #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
  54. #define FC_SRB_READ (1 << 1)
  55. #define FC_SRB_WRITE (1 << 0)
  56. /*
  57. * The SCp.ptr should be tested and set under the host lock. NULL indicates
  58. * that the command has been retruned to the scsi layer.
  59. */
  60. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  61. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  62. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  63. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  64. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  65. struct fc_fcp_internal {
  66. mempool_t *scsi_pkt_pool;
  67. struct list_head scsi_pkt_queue;
  68. u8 throttled;
  69. };
  70. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  71. /*
  72. * function prototypes
  73. * FC scsi I/O related functions
  74. */
  75. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  76. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  77. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  78. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  79. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  80. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp);
  81. static void fc_timeout_error(struct fc_fcp_pkt *);
  82. static void fc_fcp_timeout(unsigned long data);
  83. static void fc_fcp_rec(struct fc_fcp_pkt *);
  84. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  85. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  86. static void fc_io_compl(struct fc_fcp_pkt *);
  87. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  88. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  89. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  90. /*
  91. * command status codes
  92. */
  93. #define FC_COMPLETE 0
  94. #define FC_CMD_ABORTED 1
  95. #define FC_CMD_RESET 2
  96. #define FC_CMD_PLOGO 3
  97. #define FC_SNS_RCV 4
  98. #define FC_TRANS_ERR 5
  99. #define FC_DATA_OVRRUN 6
  100. #define FC_DATA_UNDRUN 7
  101. #define FC_ERROR 8
  102. #define FC_HRD_ERROR 9
  103. #define FC_CMD_TIME_OUT 10
  104. /*
  105. * Error recovery timeout values.
  106. */
  107. #define FC_SCSI_ER_TIMEOUT (10 * HZ)
  108. #define FC_SCSI_TM_TOV (10 * HZ)
  109. #define FC_SCSI_REC_TOV (2 * HZ)
  110. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  111. #define FC_MAX_ERROR_CNT 5
  112. #define FC_MAX_RECOV_RETRY 3
  113. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  114. /**
  115. * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
  116. * @lp: fc lport struct
  117. * @gfp: gfp flags for allocation
  118. *
  119. * This is used by upper layer scsi driver.
  120. * Return Value : scsi_pkt structure or null on allocation failure.
  121. * Context : call from process context. no locking required.
  122. */
  123. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp)
  124. {
  125. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  126. struct fc_fcp_pkt *fsp;
  127. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  128. if (fsp) {
  129. memset(fsp, 0, sizeof(*fsp));
  130. fsp->lp = lp;
  131. atomic_set(&fsp->ref_cnt, 1);
  132. init_timer(&fsp->timer);
  133. INIT_LIST_HEAD(&fsp->list);
  134. spin_lock_init(&fsp->scsi_pkt_lock);
  135. }
  136. return fsp;
  137. }
  138. /**
  139. * fc_fcp_pkt_release() - release hold on scsi_pkt packet
  140. * @fsp: fcp packet struct
  141. *
  142. * This is used by upper layer scsi driver.
  143. * Context : call from process and interrupt context.
  144. * no locking required
  145. */
  146. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  147. {
  148. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  149. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  150. mempool_free(fsp, si->scsi_pkt_pool);
  151. }
  152. }
  153. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  154. {
  155. atomic_inc(&fsp->ref_cnt);
  156. }
  157. /**
  158. * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
  159. * @seq: exchange sequence
  160. * @fsp: fcp packet struct
  161. *
  162. * Release hold on scsi_pkt packet set to keep scsi_pkt
  163. * till EM layer exch resource is not freed.
  164. * Context : called from from EM layer.
  165. * no locking required
  166. */
  167. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  168. {
  169. fc_fcp_pkt_release(fsp);
  170. }
  171. /**
  172. * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
  173. * @fsp: fcp packet
  174. *
  175. * We should only return error if we return a command to scsi-ml before
  176. * getting a response. This can happen in cases where we send a abort, but
  177. * do not wait for the response and the abort and command can be passing
  178. * each other on the wire/network-layer.
  179. *
  180. * Note: this function locks the packet and gets a reference to allow
  181. * callers to call the completion function while the lock is held and
  182. * not have to worry about the packets refcount.
  183. *
  184. * TODO: Maybe we should just have callers grab/release the lock and
  185. * have a function that they call to verify the fsp and grab a ref if
  186. * needed.
  187. */
  188. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  189. {
  190. spin_lock_bh(&fsp->scsi_pkt_lock);
  191. if (fsp->state & FC_SRB_COMPL) {
  192. spin_unlock_bh(&fsp->scsi_pkt_lock);
  193. return -EPERM;
  194. }
  195. fc_fcp_pkt_hold(fsp);
  196. return 0;
  197. }
  198. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  199. {
  200. spin_unlock_bh(&fsp->scsi_pkt_lock);
  201. fc_fcp_pkt_release(fsp);
  202. }
  203. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  204. {
  205. if (!(fsp->state & FC_SRB_COMPL))
  206. mod_timer(&fsp->timer, jiffies + delay);
  207. }
  208. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  209. {
  210. if (!fsp->seq_ptr)
  211. return -EINVAL;
  212. fsp->state |= FC_SRB_ABORT_PENDING;
  213. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  214. }
  215. /*
  216. * Retry command.
  217. * An abort isn't needed.
  218. */
  219. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  220. {
  221. if (fsp->seq_ptr) {
  222. fsp->lp->tt.exch_done(fsp->seq_ptr);
  223. fsp->seq_ptr = NULL;
  224. }
  225. fsp->state &= ~FC_SRB_ABORT_PENDING;
  226. fsp->io_status = 0;
  227. fsp->status_code = FC_ERROR;
  228. fc_fcp_complete_locked(fsp);
  229. }
  230. /*
  231. * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
  232. * transfer for a read I/O indicated by the fc_fcp_pkt.
  233. * @fsp: ptr to the fc_fcp_pkt
  234. *
  235. * This is called in exch_seq_send() when we have a newly allocated
  236. * exchange with a valid exchange id to setup ddp.
  237. *
  238. * returns: none
  239. */
  240. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  241. {
  242. struct fc_lport *lp;
  243. if (!fsp)
  244. return;
  245. lp = fsp->lp;
  246. if ((fsp->req_flags & FC_SRB_READ) &&
  247. (lp->lro_enabled) && (lp->tt.ddp_setup)) {
  248. if (lp->tt.ddp_setup(lp, xid, scsi_sglist(fsp->cmd),
  249. scsi_sg_count(fsp->cmd)))
  250. fsp->xfer_ddp = xid;
  251. }
  252. }
  253. EXPORT_SYMBOL(fc_fcp_ddp_setup);
  254. /*
  255. * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
  256. * DDP related resources for this I/O if it is initialized
  257. * as a ddp transfer
  258. * @fsp: ptr to the fc_fcp_pkt
  259. *
  260. * returns: none
  261. */
  262. static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  263. {
  264. struct fc_lport *lp;
  265. if (!fsp)
  266. return;
  267. lp = fsp->lp;
  268. if (fsp->xfer_ddp && lp->tt.ddp_done) {
  269. fsp->xfer_len = lp->tt.ddp_done(lp, fsp->xfer_ddp);
  270. fsp->xfer_ddp = 0;
  271. }
  272. }
  273. /*
  274. * Receive SCSI data from target.
  275. * Called after receiving solicited data.
  276. */
  277. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  278. {
  279. struct scsi_cmnd *sc = fsp->cmd;
  280. struct fc_lport *lp = fsp->lp;
  281. struct fcoe_dev_stats *stats;
  282. struct fc_frame_header *fh;
  283. size_t start_offset;
  284. size_t offset;
  285. u32 crc;
  286. u32 copy_len = 0;
  287. size_t len;
  288. void *buf;
  289. struct scatterlist *sg;
  290. size_t remaining;
  291. fh = fc_frame_header_get(fp);
  292. offset = ntohl(fh->fh_parm_offset);
  293. start_offset = offset;
  294. len = fr_len(fp) - sizeof(*fh);
  295. buf = fc_frame_payload_get(fp, 0);
  296. /* if this I/O is ddped, update xfer len */
  297. fc_fcp_ddp_done(fsp);
  298. if (offset + len > fsp->data_len) {
  299. /* this should never happen */
  300. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  301. fc_frame_crc_check(fp))
  302. goto crc_err;
  303. FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
  304. "data_len %x\n", len, offset, fsp->data_len);
  305. fc_fcp_retry_cmd(fsp);
  306. return;
  307. }
  308. if (offset != fsp->xfer_len)
  309. fsp->state |= FC_SRB_DISCONTIG;
  310. crc = 0;
  311. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
  312. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  313. sg = scsi_sglist(sc);
  314. remaining = len;
  315. while (remaining > 0 && sg) {
  316. size_t off;
  317. void *page_addr;
  318. size_t sg_bytes;
  319. if (offset >= sg->length) {
  320. offset -= sg->length;
  321. sg = sg_next(sg);
  322. continue;
  323. }
  324. sg_bytes = min(remaining, sg->length - offset);
  325. /*
  326. * The scatterlist item may be bigger than PAGE_SIZE,
  327. * but we are limited to mapping PAGE_SIZE at a time.
  328. */
  329. off = offset + sg->offset;
  330. sg_bytes = min(sg_bytes, (size_t)
  331. (PAGE_SIZE - (off & ~PAGE_MASK)));
  332. page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
  333. KM_SOFTIRQ0);
  334. if (!page_addr)
  335. break; /* XXX panic? */
  336. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
  337. crc = crc32(crc, buf, sg_bytes);
  338. memcpy((char *)page_addr + (off & ~PAGE_MASK), buf,
  339. sg_bytes);
  340. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  341. buf += sg_bytes;
  342. offset += sg_bytes;
  343. remaining -= sg_bytes;
  344. copy_len += sg_bytes;
  345. }
  346. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
  347. buf = fc_frame_payload_get(fp, 0);
  348. if (len % 4) {
  349. crc = crc32(crc, buf + len, 4 - (len % 4));
  350. len += 4 - (len % 4);
  351. }
  352. if (~crc != le32_to_cpu(fr_crc(fp))) {
  353. crc_err:
  354. stats = fc_lport_get_stats(lp);
  355. stats->ErrorFrames++;
  356. /* FIXME - per cpu count, not total count! */
  357. if (stats->InvalidCRCCount++ < 5)
  358. printk(KERN_WARNING "libfc: CRC error on data "
  359. "frame for port (%6x)\n",
  360. fc_host_port_id(lp->host));
  361. /*
  362. * Assume the frame is total garbage.
  363. * We may have copied it over the good part
  364. * of the buffer.
  365. * If so, we need to retry the entire operation.
  366. * Otherwise, ignore it.
  367. */
  368. if (fsp->state & FC_SRB_DISCONTIG)
  369. fc_fcp_retry_cmd(fsp);
  370. return;
  371. }
  372. }
  373. if (fsp->xfer_contig_end == start_offset)
  374. fsp->xfer_contig_end += copy_len;
  375. fsp->xfer_len += copy_len;
  376. /*
  377. * In the very rare event that this data arrived after the response
  378. * and completes the transfer, call the completion handler.
  379. */
  380. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  381. fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
  382. fc_fcp_complete_locked(fsp);
  383. }
  384. /**
  385. * fc_fcp_send_data() - Send SCSI data to target.
  386. * @fsp: ptr to fc_fcp_pkt
  387. * @sp: ptr to this sequence
  388. * @offset: starting offset for this data request
  389. * @seq_blen: the burst length for this data request
  390. *
  391. * Called after receiving a Transfer Ready data descriptor.
  392. * if LLD is capable of seq offload then send down seq_blen
  393. * size of data in single frame, otherwise send multiple FC
  394. * frames of max FC frame payload supported by target port.
  395. *
  396. * Returns : 0 for success.
  397. */
  398. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  399. size_t offset, size_t seq_blen)
  400. {
  401. struct fc_exch *ep;
  402. struct scsi_cmnd *sc;
  403. struct scatterlist *sg;
  404. struct fc_frame *fp = NULL;
  405. struct fc_lport *lp = fsp->lp;
  406. size_t remaining;
  407. size_t t_blen;
  408. size_t tlen;
  409. size_t sg_bytes;
  410. size_t frame_offset, fh_parm_offset;
  411. int error;
  412. void *data = NULL;
  413. void *page_addr;
  414. int using_sg = lp->sg_supp;
  415. u32 f_ctl;
  416. WARN_ON(seq_blen <= 0);
  417. if (unlikely(offset + seq_blen > fsp->data_len)) {
  418. /* this should never happen */
  419. FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
  420. "offset %zx\n", seq_blen, offset);
  421. fc_fcp_send_abort(fsp);
  422. return 0;
  423. } else if (offset != fsp->xfer_len) {
  424. /* Out of Order Data Request - no problem, but unexpected. */
  425. FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
  426. "seq_blen %zx offset %zx\n", seq_blen, offset);
  427. }
  428. /*
  429. * if LLD is capable of seq_offload then set transport
  430. * burst length (t_blen) to seq_blen, otherwise set t_blen
  431. * to max FC frame payload previously set in fsp->max_payload.
  432. */
  433. t_blen = fsp->max_payload;
  434. if (lp->seq_offload) {
  435. t_blen = min(seq_blen, (size_t)lp->lso_max);
  436. FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  437. fsp, seq_blen, lp->lso_max, t_blen);
  438. }
  439. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
  440. if (t_blen > 512)
  441. t_blen &= ~(512 - 1); /* round down to block size */
  442. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
  443. sc = fsp->cmd;
  444. remaining = seq_blen;
  445. fh_parm_offset = frame_offset = offset;
  446. tlen = 0;
  447. seq = lp->tt.seq_start_next(seq);
  448. f_ctl = FC_FC_REL_OFF;
  449. WARN_ON(!seq);
  450. /*
  451. * If a get_page()/put_page() will fail, don't use sg lists
  452. * in the fc_frame structure.
  453. *
  454. * The put_page() may be long after the I/O has completed
  455. * in the case of FCoE, since the network driver does it
  456. * via free_skb(). See the test in free_pages_check().
  457. *
  458. * Test this case with 'dd </dev/zero >/dev/st0 bs=64k'.
  459. */
  460. if (using_sg) {
  461. for (sg = scsi_sglist(sc); sg; sg = sg_next(sg)) {
  462. if (page_count(sg_page(sg)) == 0 ||
  463. (sg_page(sg)->flags & (1 << PG_lru |
  464. 1 << PG_private |
  465. 1 << PG_locked |
  466. 1 << PG_active |
  467. 1 << PG_slab |
  468. 1 << PG_swapcache |
  469. 1 << PG_writeback |
  470. 1 << PG_reserved |
  471. 1 << PG_buddy))) {
  472. using_sg = 0;
  473. break;
  474. }
  475. }
  476. }
  477. sg = scsi_sglist(sc);
  478. while (remaining > 0 && sg) {
  479. if (offset >= sg->length) {
  480. offset -= sg->length;
  481. sg = sg_next(sg);
  482. continue;
  483. }
  484. if (!fp) {
  485. tlen = min(t_blen, remaining);
  486. /*
  487. * TODO. Temporary workaround. fc_seq_send() can't
  488. * handle odd lengths in non-linear skbs.
  489. * This will be the final fragment only.
  490. */
  491. if (tlen % 4)
  492. using_sg = 0;
  493. if (using_sg) {
  494. fp = _fc_frame_alloc(lp, 0);
  495. if (!fp)
  496. return -ENOMEM;
  497. } else {
  498. fp = fc_frame_alloc(lp, tlen);
  499. if (!fp)
  500. return -ENOMEM;
  501. data = (void *)(fr_hdr(fp)) +
  502. sizeof(struct fc_frame_header);
  503. }
  504. fh_parm_offset = frame_offset;
  505. fr_max_payload(fp) = fsp->max_payload;
  506. }
  507. sg_bytes = min(tlen, sg->length - offset);
  508. if (using_sg) {
  509. WARN_ON(skb_shinfo(fp_skb(fp))->nr_frags >
  510. FC_FRAME_SG_LEN);
  511. get_page(sg_page(sg));
  512. skb_fill_page_desc(fp_skb(fp),
  513. skb_shinfo(fp_skb(fp))->nr_frags,
  514. sg_page(sg), sg->offset + offset,
  515. sg_bytes);
  516. fp_skb(fp)->data_len += sg_bytes;
  517. fr_len(fp) += sg_bytes;
  518. fp_skb(fp)->truesize += PAGE_SIZE;
  519. } else {
  520. size_t off = offset + sg->offset;
  521. /*
  522. * The scatterlist item may be bigger than PAGE_SIZE,
  523. * but we must not cross pages inside the kmap.
  524. */
  525. sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
  526. (off & ~PAGE_MASK)));
  527. page_addr = kmap_atomic(sg_page(sg) +
  528. (off >> PAGE_SHIFT),
  529. KM_SOFTIRQ0);
  530. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  531. sg_bytes);
  532. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  533. data += sg_bytes;
  534. }
  535. offset += sg_bytes;
  536. frame_offset += sg_bytes;
  537. tlen -= sg_bytes;
  538. remaining -= sg_bytes;
  539. if (tlen)
  540. continue;
  541. /*
  542. * Send sequence with transfer sequence initiative in case
  543. * this is last FCP frame of the sequence.
  544. */
  545. if (remaining == 0)
  546. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  547. ep = fc_seq_exch(seq);
  548. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  549. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  550. /*
  551. * send fragment using for a sequence.
  552. */
  553. error = lp->tt.seq_send(lp, seq, fp);
  554. if (error) {
  555. WARN_ON(1); /* send error should be rare */
  556. fc_fcp_retry_cmd(fsp);
  557. return 0;
  558. }
  559. fp = NULL;
  560. }
  561. fsp->xfer_len += seq_blen; /* premature count? */
  562. return 0;
  563. }
  564. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  565. {
  566. int ba_done = 1;
  567. struct fc_ba_rjt *brp;
  568. struct fc_frame_header *fh;
  569. fh = fc_frame_header_get(fp);
  570. switch (fh->fh_r_ctl) {
  571. case FC_RCTL_BA_ACC:
  572. break;
  573. case FC_RCTL_BA_RJT:
  574. brp = fc_frame_payload_get(fp, sizeof(*brp));
  575. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  576. break;
  577. /* fall thru */
  578. default:
  579. /*
  580. * we will let the command timeout
  581. * and scsi-ml recover in this case,
  582. * therefore cleared the ba_done flag.
  583. */
  584. ba_done = 0;
  585. }
  586. if (ba_done) {
  587. fsp->state |= FC_SRB_ABORTED;
  588. fsp->state &= ~FC_SRB_ABORT_PENDING;
  589. if (fsp->wait_for_comp)
  590. complete(&fsp->tm_done);
  591. else
  592. fc_fcp_complete_locked(fsp);
  593. }
  594. }
  595. /**
  596. * fc_fcp_reduce_can_queue() - drop can_queue
  597. * @lp: lport to drop queueing for
  598. *
  599. * If we are getting memory allocation failures, then we may
  600. * be trying to execute too many commands. We let the running
  601. * commands complete or timeout, then try again with a reduced
  602. * can_queue. Eventually we will hit the point where we run
  603. * on all reserved structs.
  604. */
  605. static void fc_fcp_reduce_can_queue(struct fc_lport *lp)
  606. {
  607. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  608. unsigned long flags;
  609. int can_queue;
  610. spin_lock_irqsave(lp->host->host_lock, flags);
  611. if (si->throttled)
  612. goto done;
  613. si->throttled = 1;
  614. can_queue = lp->host->can_queue;
  615. can_queue >>= 1;
  616. if (!can_queue)
  617. can_queue = 1;
  618. lp->host->can_queue = can_queue;
  619. shost_printk(KERN_ERR, lp->host, "libfc: Could not allocate frame.\n"
  620. "Reducing can_queue to %d.\n", can_queue);
  621. done:
  622. spin_unlock_irqrestore(lp->host->host_lock, flags);
  623. }
  624. /**
  625. * fc_fcp_recv() - Reveive FCP frames
  626. * @seq: The sequence the frame is on
  627. * @fp: The FC frame
  628. * @arg: The related FCP packet
  629. *
  630. * Return : None
  631. * Context : called from Soft IRQ context
  632. * can not called holding list lock
  633. */
  634. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  635. {
  636. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  637. struct fc_lport *lport = fsp->lp;
  638. struct fc_frame_header *fh;
  639. struct fcp_txrdy *dd;
  640. u8 r_ctl;
  641. int rc = 0;
  642. if (IS_ERR(fp))
  643. goto errout;
  644. fh = fc_frame_header_get(fp);
  645. r_ctl = fh->fh_r_ctl;
  646. if (!(lport->state & LPORT_ST_READY))
  647. goto out;
  648. if (fc_fcp_lock_pkt(fsp))
  649. goto out;
  650. fsp->last_pkt_time = jiffies;
  651. if (fh->fh_type == FC_TYPE_BLS) {
  652. fc_fcp_abts_resp(fsp, fp);
  653. goto unlock;
  654. }
  655. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  656. goto unlock;
  657. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  658. /*
  659. * received XFER RDY from the target
  660. * need to send data to the target
  661. */
  662. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  663. dd = fc_frame_payload_get(fp, sizeof(*dd));
  664. WARN_ON(!dd);
  665. rc = fc_fcp_send_data(fsp, seq,
  666. (size_t) ntohl(dd->ft_data_ro),
  667. (size_t) ntohl(dd->ft_burst_len));
  668. if (!rc)
  669. seq->rec_data = fsp->xfer_len;
  670. else if (rc == -ENOMEM)
  671. fsp->state |= FC_SRB_NOMEM;
  672. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  673. /*
  674. * received a DATA frame
  675. * next we will copy the data to the system buffer
  676. */
  677. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  678. fc_fcp_recv_data(fsp, fp);
  679. seq->rec_data = fsp->xfer_contig_end;
  680. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  681. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  682. fc_fcp_resp(fsp, fp);
  683. } else {
  684. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  685. }
  686. unlock:
  687. fc_fcp_unlock_pkt(fsp);
  688. out:
  689. fc_frame_free(fp);
  690. errout:
  691. if (IS_ERR(fp))
  692. fc_fcp_error(fsp, fp);
  693. else if (rc == -ENOMEM)
  694. fc_fcp_reduce_can_queue(lport);
  695. }
  696. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  697. {
  698. struct fc_frame_header *fh;
  699. struct fcp_resp *fc_rp;
  700. struct fcp_resp_ext *rp_ex;
  701. struct fcp_resp_rsp_info *fc_rp_info;
  702. u32 plen;
  703. u32 expected_len;
  704. u32 respl = 0;
  705. u32 snsl = 0;
  706. u8 flags = 0;
  707. plen = fr_len(fp);
  708. fh = (struct fc_frame_header *)fr_hdr(fp);
  709. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  710. goto len_err;
  711. plen -= sizeof(*fh);
  712. fc_rp = (struct fcp_resp *)(fh + 1);
  713. fsp->cdb_status = fc_rp->fr_status;
  714. flags = fc_rp->fr_flags;
  715. fsp->scsi_comp_flags = flags;
  716. expected_len = fsp->data_len;
  717. /* if ddp, update xfer len */
  718. fc_fcp_ddp_done(fsp);
  719. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  720. rp_ex = (void *)(fc_rp + 1);
  721. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  722. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  723. goto len_err;
  724. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  725. if (flags & FCP_RSP_LEN_VAL) {
  726. respl = ntohl(rp_ex->fr_rsp_len);
  727. if (respl != sizeof(*fc_rp_info))
  728. goto len_err;
  729. if (fsp->wait_for_comp) {
  730. /* Abuse cdb_status for rsp code */
  731. fsp->cdb_status = fc_rp_info->rsp_code;
  732. complete(&fsp->tm_done);
  733. /*
  734. * tmfs will not have any scsi cmd so
  735. * exit here
  736. */
  737. return;
  738. } else
  739. goto err;
  740. }
  741. if (flags & FCP_SNS_LEN_VAL) {
  742. snsl = ntohl(rp_ex->fr_sns_len);
  743. if (snsl > SCSI_SENSE_BUFFERSIZE)
  744. snsl = SCSI_SENSE_BUFFERSIZE;
  745. memcpy(fsp->cmd->sense_buffer,
  746. (char *)fc_rp_info + respl, snsl);
  747. }
  748. }
  749. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  750. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  751. goto len_err;
  752. if (flags & FCP_RESID_UNDER) {
  753. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  754. /*
  755. * The cmnd->underflow is the minimum number of
  756. * bytes that must be transfered for this
  757. * command. Provided a sense condition is not
  758. * present, make sure the actual amount
  759. * transferred is at least the underflow value
  760. * or fail.
  761. */
  762. if (!(flags & FCP_SNS_LEN_VAL) &&
  763. (fc_rp->fr_status == 0) &&
  764. (scsi_bufflen(fsp->cmd) -
  765. fsp->scsi_resid) < fsp->cmd->underflow)
  766. goto err;
  767. expected_len -= fsp->scsi_resid;
  768. } else {
  769. fsp->status_code = FC_ERROR;
  770. }
  771. }
  772. }
  773. fsp->state |= FC_SRB_RCV_STATUS;
  774. /*
  775. * Check for missing or extra data frames.
  776. */
  777. if (unlikely(fsp->xfer_len != expected_len)) {
  778. if (fsp->xfer_len < expected_len) {
  779. /*
  780. * Some data may be queued locally,
  781. * Wait a at least one jiffy to see if it is delivered.
  782. * If this expires without data, we may do SRR.
  783. */
  784. fc_fcp_timer_set(fsp, 2);
  785. return;
  786. }
  787. fsp->status_code = FC_DATA_OVRRUN;
  788. FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
  789. "len %x, data len %x\n",
  790. fsp->rport->port_id,
  791. fsp->xfer_len, expected_len, fsp->data_len);
  792. }
  793. fc_fcp_complete_locked(fsp);
  794. return;
  795. len_err:
  796. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  797. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  798. err:
  799. fsp->status_code = FC_ERROR;
  800. fc_fcp_complete_locked(fsp);
  801. }
  802. /**
  803. * fc_fcp_complete_locked() - complete processing of a fcp packet
  804. * @fsp: fcp packet
  805. *
  806. * This function may sleep if a timer is pending. The packet lock must be
  807. * held, and the host lock must not be held.
  808. */
  809. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  810. {
  811. struct fc_lport *lp = fsp->lp;
  812. struct fc_seq *seq;
  813. struct fc_exch *ep;
  814. u32 f_ctl;
  815. if (fsp->state & FC_SRB_ABORT_PENDING)
  816. return;
  817. if (fsp->state & FC_SRB_ABORTED) {
  818. if (!fsp->status_code)
  819. fsp->status_code = FC_CMD_ABORTED;
  820. } else {
  821. /*
  822. * Test for transport underrun, independent of response
  823. * underrun status.
  824. */
  825. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  826. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  827. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  828. fsp->status_code = FC_DATA_UNDRUN;
  829. fsp->io_status = 0;
  830. }
  831. }
  832. seq = fsp->seq_ptr;
  833. if (seq) {
  834. fsp->seq_ptr = NULL;
  835. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  836. struct fc_frame *conf_frame;
  837. struct fc_seq *csp;
  838. csp = lp->tt.seq_start_next(seq);
  839. conf_frame = fc_frame_alloc(fsp->lp, 0);
  840. if (conf_frame) {
  841. f_ctl = FC_FC_SEQ_INIT;
  842. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  843. ep = fc_seq_exch(seq);
  844. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  845. ep->did, ep->sid,
  846. FC_TYPE_FCP, f_ctl, 0);
  847. lp->tt.seq_send(lp, csp, conf_frame);
  848. }
  849. }
  850. lp->tt.exch_done(seq);
  851. }
  852. fc_io_compl(fsp);
  853. }
  854. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  855. {
  856. struct fc_lport *lp = fsp->lp;
  857. if (fsp->seq_ptr) {
  858. lp->tt.exch_done(fsp->seq_ptr);
  859. fsp->seq_ptr = NULL;
  860. }
  861. fsp->status_code = error;
  862. }
  863. /**
  864. * fc_fcp_cleanup_each_cmd() - Cleanup active commads
  865. * @lp: logical port
  866. * @id: target id
  867. * @lun: lun
  868. * @error: fsp status code
  869. *
  870. * If lun or id is -1, they are ignored.
  871. */
  872. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lp, unsigned int id,
  873. unsigned int lun, int error)
  874. {
  875. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  876. struct fc_fcp_pkt *fsp;
  877. struct scsi_cmnd *sc_cmd;
  878. unsigned long flags;
  879. spin_lock_irqsave(lp->host->host_lock, flags);
  880. restart:
  881. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  882. sc_cmd = fsp->cmd;
  883. if (id != -1 && scmd_id(sc_cmd) != id)
  884. continue;
  885. if (lun != -1 && sc_cmd->device->lun != lun)
  886. continue;
  887. fc_fcp_pkt_hold(fsp);
  888. spin_unlock_irqrestore(lp->host->host_lock, flags);
  889. if (!fc_fcp_lock_pkt(fsp)) {
  890. fc_fcp_cleanup_cmd(fsp, error);
  891. fc_io_compl(fsp);
  892. fc_fcp_unlock_pkt(fsp);
  893. }
  894. fc_fcp_pkt_release(fsp);
  895. spin_lock_irqsave(lp->host->host_lock, flags);
  896. /*
  897. * while we dropped the lock multiple pkts could
  898. * have been released, so we have to start over.
  899. */
  900. goto restart;
  901. }
  902. spin_unlock_irqrestore(lp->host->host_lock, flags);
  903. }
  904. static void fc_fcp_abort_io(struct fc_lport *lp)
  905. {
  906. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_HRD_ERROR);
  907. }
  908. /**
  909. * fc_fcp_pkt_send() - send a fcp packet to the lower level.
  910. * @lp: fc lport
  911. * @fsp: fc packet.
  912. *
  913. * This is called by upper layer protocol.
  914. * Return : zero for success and -1 for failure
  915. * Context : called from queuecommand which can be called from process
  916. * or scsi soft irq.
  917. * Locks : called with the host lock and irqs disabled.
  918. */
  919. static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
  920. {
  921. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  922. int rc;
  923. fsp->cmd->SCp.ptr = (char *)fsp;
  924. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  925. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  926. int_to_scsilun(fsp->cmd->device->lun,
  927. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  928. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  929. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  930. spin_unlock_irq(lp->host->host_lock);
  931. rc = lp->tt.fcp_cmd_send(lp, fsp, fc_fcp_recv);
  932. spin_lock_irq(lp->host->host_lock);
  933. if (rc)
  934. list_del(&fsp->list);
  935. return rc;
  936. }
  937. static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  938. void (*resp)(struct fc_seq *,
  939. struct fc_frame *fp,
  940. void *arg))
  941. {
  942. struct fc_frame *fp;
  943. struct fc_seq *seq;
  944. struct fc_rport *rport;
  945. struct fc_rport_libfc_priv *rp;
  946. const size_t len = sizeof(fsp->cdb_cmd);
  947. int rc = 0;
  948. if (fc_fcp_lock_pkt(fsp))
  949. return 0;
  950. fp = fc_frame_alloc(lp, sizeof(fsp->cdb_cmd));
  951. if (!fp) {
  952. rc = -1;
  953. goto unlock;
  954. }
  955. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  956. fr_fsp(fp) = fsp;
  957. rport = fsp->rport;
  958. fsp->max_payload = rport->maxframe_size;
  959. rp = rport->dd_data;
  960. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  961. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  962. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  963. seq = lp->tt.exch_seq_send(lp, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
  964. if (!seq) {
  965. fc_frame_free(fp);
  966. rc = -1;
  967. goto unlock;
  968. }
  969. fsp->last_pkt_time = jiffies;
  970. fsp->seq_ptr = seq;
  971. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  972. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  973. fc_fcp_timer_set(fsp,
  974. (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
  975. FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
  976. unlock:
  977. fc_fcp_unlock_pkt(fsp);
  978. return rc;
  979. }
  980. /*
  981. * transport error handler
  982. */
  983. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  984. {
  985. int error = PTR_ERR(fp);
  986. if (fc_fcp_lock_pkt(fsp))
  987. return;
  988. if (error == -FC_EX_CLOSED) {
  989. fc_fcp_retry_cmd(fsp);
  990. goto unlock;
  991. }
  992. /*
  993. * clear abort pending, because the lower layer
  994. * decided to force completion.
  995. */
  996. fsp->state &= ~FC_SRB_ABORT_PENDING;
  997. fsp->status_code = FC_CMD_PLOGO;
  998. fc_fcp_complete_locked(fsp);
  999. unlock:
  1000. fc_fcp_unlock_pkt(fsp);
  1001. }
  1002. /*
  1003. * Scsi abort handler- calls to send an abort
  1004. * and then wait for abort completion
  1005. */
  1006. static int fc_fcp_pkt_abort(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
  1007. {
  1008. int rc = FAILED;
  1009. if (fc_fcp_send_abort(fsp))
  1010. return FAILED;
  1011. init_completion(&fsp->tm_done);
  1012. fsp->wait_for_comp = 1;
  1013. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1014. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1015. spin_lock_bh(&fsp->scsi_pkt_lock);
  1016. fsp->wait_for_comp = 0;
  1017. if (!rc) {
  1018. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  1019. rc = FAILED;
  1020. } else if (fsp->state & FC_SRB_ABORTED) {
  1021. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  1022. rc = SUCCESS;
  1023. fc_fcp_complete_locked(fsp);
  1024. }
  1025. return rc;
  1026. }
  1027. /*
  1028. * Retry LUN reset after resource allocation failed.
  1029. */
  1030. static void fc_lun_reset_send(unsigned long data)
  1031. {
  1032. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1033. struct fc_lport *lp = fsp->lp;
  1034. if (lp->tt.fcp_cmd_send(lp, fsp, fc_tm_done)) {
  1035. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1036. return;
  1037. if (fc_fcp_lock_pkt(fsp))
  1038. return;
  1039. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1040. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1041. fc_fcp_unlock_pkt(fsp);
  1042. }
  1043. }
  1044. /*
  1045. * Scsi device reset handler- send a LUN RESET to the device
  1046. * and wait for reset reply
  1047. */
  1048. static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  1049. unsigned int id, unsigned int lun)
  1050. {
  1051. int rc;
  1052. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1053. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1054. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  1055. fsp->wait_for_comp = 1;
  1056. init_completion(&fsp->tm_done);
  1057. fc_lun_reset_send((unsigned long)fsp);
  1058. /*
  1059. * wait for completion of reset
  1060. * after that make sure all commands are terminated
  1061. */
  1062. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1063. spin_lock_bh(&fsp->scsi_pkt_lock);
  1064. fsp->state |= FC_SRB_COMPL;
  1065. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1066. del_timer_sync(&fsp->timer);
  1067. spin_lock_bh(&fsp->scsi_pkt_lock);
  1068. if (fsp->seq_ptr) {
  1069. lp->tt.exch_done(fsp->seq_ptr);
  1070. fsp->seq_ptr = NULL;
  1071. }
  1072. fsp->wait_for_comp = 0;
  1073. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1074. if (!rc) {
  1075. FC_SCSI_DBG(lp, "lun reset failed\n");
  1076. return FAILED;
  1077. }
  1078. /* cdb_status holds the tmf's rsp code */
  1079. if (fsp->cdb_status != FCP_TMF_CMPL)
  1080. return FAILED;
  1081. FC_SCSI_DBG(lp, "lun reset to lun %u completed\n", lun);
  1082. fc_fcp_cleanup_each_cmd(lp, id, lun, FC_CMD_ABORTED);
  1083. return SUCCESS;
  1084. }
  1085. /*
  1086. * Task Managment response handler
  1087. */
  1088. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1089. {
  1090. struct fc_fcp_pkt *fsp = arg;
  1091. struct fc_frame_header *fh;
  1092. if (IS_ERR(fp)) {
  1093. /*
  1094. * If there is an error just let it timeout or wait
  1095. * for TMF to be aborted if it timedout.
  1096. *
  1097. * scsi-eh will escalate for when either happens.
  1098. */
  1099. return;
  1100. }
  1101. if (fc_fcp_lock_pkt(fsp))
  1102. return;
  1103. /*
  1104. * raced with eh timeout handler.
  1105. */
  1106. if (!fsp->seq_ptr || !fsp->wait_for_comp) {
  1107. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1108. return;
  1109. }
  1110. fh = fc_frame_header_get(fp);
  1111. if (fh->fh_type != FC_TYPE_BLS)
  1112. fc_fcp_resp(fsp, fp);
  1113. fsp->seq_ptr = NULL;
  1114. fsp->lp->tt.exch_done(seq);
  1115. fc_frame_free(fp);
  1116. fc_fcp_unlock_pkt(fsp);
  1117. }
  1118. static void fc_fcp_cleanup(struct fc_lport *lp)
  1119. {
  1120. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_ERROR);
  1121. }
  1122. /*
  1123. * fc_fcp_timeout: called by OS timer function.
  1124. *
  1125. * The timer has been inactivated and must be reactivated if desired
  1126. * using fc_fcp_timer_set().
  1127. *
  1128. * Algorithm:
  1129. *
  1130. * If REC is supported, just issue it, and return. The REC exchange will
  1131. * complete or time out, and recovery can continue at that point.
  1132. *
  1133. * Otherwise, if the response has been received without all the data,
  1134. * it has been ER_TIMEOUT since the response was received.
  1135. *
  1136. * If the response has not been received,
  1137. * we see if data was received recently. If it has been, we continue waiting,
  1138. * otherwise, we abort the command.
  1139. */
  1140. static void fc_fcp_timeout(unsigned long data)
  1141. {
  1142. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1143. struct fc_rport *rport = fsp->rport;
  1144. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1145. if (fc_fcp_lock_pkt(fsp))
  1146. return;
  1147. if (fsp->cdb_cmd.fc_tm_flags)
  1148. goto unlock;
  1149. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1150. if (rp->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1151. fc_fcp_rec(fsp);
  1152. else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
  1153. jiffies))
  1154. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1155. else if (fsp->state & FC_SRB_RCV_STATUS)
  1156. fc_fcp_complete_locked(fsp);
  1157. else
  1158. fc_timeout_error(fsp);
  1159. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1160. unlock:
  1161. fc_fcp_unlock_pkt(fsp);
  1162. }
  1163. /*
  1164. * Send a REC ELS request
  1165. */
  1166. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1167. {
  1168. struct fc_lport *lp;
  1169. struct fc_frame *fp;
  1170. struct fc_rport *rport;
  1171. struct fc_rport_libfc_priv *rp;
  1172. lp = fsp->lp;
  1173. rport = fsp->rport;
  1174. rp = rport->dd_data;
  1175. if (!fsp->seq_ptr || rp->rp_state != RPORT_ST_READY) {
  1176. fsp->status_code = FC_HRD_ERROR;
  1177. fsp->io_status = 0;
  1178. fc_fcp_complete_locked(fsp);
  1179. return;
  1180. }
  1181. fp = fc_frame_alloc(lp, sizeof(struct fc_els_rec));
  1182. if (!fp)
  1183. goto retry;
  1184. fr_seq(fp) = fsp->seq_ptr;
  1185. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1186. fc_host_port_id(rp->local_port->host), FC_TYPE_ELS,
  1187. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1188. if (lp->tt.elsct_send(lp, rport, fp, ELS_REC, fc_fcp_rec_resp,
  1189. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV))) {
  1190. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1191. return;
  1192. }
  1193. fc_frame_free(fp);
  1194. retry:
  1195. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1196. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1197. else
  1198. fc_timeout_error(fsp);
  1199. }
  1200. /*
  1201. * Receive handler for REC ELS frame
  1202. * if it is a reject then let the scsi layer to handle
  1203. * the timeout. if it is a LS_ACC then if the io was not completed
  1204. * then set the timeout and return otherwise complete the exchange
  1205. * and tell the scsi layer to restart the I/O.
  1206. */
  1207. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1208. {
  1209. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1210. struct fc_els_rec_acc *recp;
  1211. struct fc_els_ls_rjt *rjt;
  1212. u32 e_stat;
  1213. u8 opcode;
  1214. u32 offset;
  1215. enum dma_data_direction data_dir;
  1216. enum fc_rctl r_ctl;
  1217. struct fc_rport_libfc_priv *rp;
  1218. if (IS_ERR(fp)) {
  1219. fc_fcp_rec_error(fsp, fp);
  1220. return;
  1221. }
  1222. if (fc_fcp_lock_pkt(fsp))
  1223. goto out;
  1224. fsp->recov_retry = 0;
  1225. opcode = fc_frame_payload_op(fp);
  1226. if (opcode == ELS_LS_RJT) {
  1227. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1228. switch (rjt->er_reason) {
  1229. default:
  1230. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1231. "reason %d expl %d\n",
  1232. fsp->rport->port_id, rjt->er_reason,
  1233. rjt->er_explan);
  1234. /* fall through */
  1235. case ELS_RJT_UNSUP:
  1236. FC_FCP_DBG(fsp, "device does not support REC\n");
  1237. rp = fsp->rport->dd_data;
  1238. /*
  1239. * if we do not spport RECs or got some bogus
  1240. * reason then resetup timer so we check for
  1241. * making progress.
  1242. */
  1243. rp->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1244. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1245. break;
  1246. case ELS_RJT_LOGIC:
  1247. case ELS_RJT_UNAB:
  1248. /*
  1249. * If no data transfer, the command frame got dropped
  1250. * so we just retry. If data was transferred, we
  1251. * lost the response but the target has no record,
  1252. * so we abort and retry.
  1253. */
  1254. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1255. fsp->xfer_len == 0) {
  1256. fc_fcp_retry_cmd(fsp);
  1257. break;
  1258. }
  1259. fc_timeout_error(fsp);
  1260. break;
  1261. }
  1262. } else if (opcode == ELS_LS_ACC) {
  1263. if (fsp->state & FC_SRB_ABORTED)
  1264. goto unlock_out;
  1265. data_dir = fsp->cmd->sc_data_direction;
  1266. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1267. offset = ntohl(recp->reca_fc4value);
  1268. e_stat = ntohl(recp->reca_e_stat);
  1269. if (e_stat & ESB_ST_COMPLETE) {
  1270. /*
  1271. * The exchange is complete.
  1272. *
  1273. * For output, we must've lost the response.
  1274. * For input, all data must've been sent.
  1275. * We lost may have lost the response
  1276. * (and a confirmation was requested) and maybe
  1277. * some data.
  1278. *
  1279. * If all data received, send SRR
  1280. * asking for response. If partial data received,
  1281. * or gaps, SRR requests data at start of gap.
  1282. * Recovery via SRR relies on in-order-delivery.
  1283. */
  1284. if (data_dir == DMA_TO_DEVICE) {
  1285. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1286. } else if (fsp->xfer_contig_end == offset) {
  1287. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1288. } else {
  1289. offset = fsp->xfer_contig_end;
  1290. r_ctl = FC_RCTL_DD_SOL_DATA;
  1291. }
  1292. fc_fcp_srr(fsp, r_ctl, offset);
  1293. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1294. /*
  1295. * The remote port has the initiative, so just
  1296. * keep waiting for it to complete.
  1297. */
  1298. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1299. } else {
  1300. /*
  1301. * The exchange is incomplete, we have seq. initiative.
  1302. * Lost response with requested confirmation,
  1303. * lost confirmation, lost transfer ready or
  1304. * lost write data.
  1305. *
  1306. * For output, if not all data was received, ask
  1307. * for transfer ready to be repeated.
  1308. *
  1309. * If we received or sent all the data, send SRR to
  1310. * request response.
  1311. *
  1312. * If we lost a response, we may have lost some read
  1313. * data as well.
  1314. */
  1315. r_ctl = FC_RCTL_DD_SOL_DATA;
  1316. if (data_dir == DMA_TO_DEVICE) {
  1317. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1318. if (offset < fsp->data_len)
  1319. r_ctl = FC_RCTL_DD_DATA_DESC;
  1320. } else if (offset == fsp->xfer_contig_end) {
  1321. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1322. } else if (fsp->xfer_contig_end < offset) {
  1323. offset = fsp->xfer_contig_end;
  1324. }
  1325. fc_fcp_srr(fsp, r_ctl, offset);
  1326. }
  1327. }
  1328. unlock_out:
  1329. fc_fcp_unlock_pkt(fsp);
  1330. out:
  1331. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1332. fc_frame_free(fp);
  1333. }
  1334. /*
  1335. * Handle error response or timeout for REC exchange.
  1336. */
  1337. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1338. {
  1339. int error = PTR_ERR(fp);
  1340. if (fc_fcp_lock_pkt(fsp))
  1341. goto out;
  1342. switch (error) {
  1343. case -FC_EX_CLOSED:
  1344. fc_fcp_retry_cmd(fsp);
  1345. break;
  1346. default:
  1347. FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
  1348. fsp, fsp->rport->port_id, error);
  1349. fsp->status_code = FC_CMD_PLOGO;
  1350. /* fall through */
  1351. case -FC_EX_TIMEOUT:
  1352. /*
  1353. * Assume REC or LS_ACC was lost.
  1354. * The exchange manager will have aborted REC, so retry.
  1355. */
  1356. FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
  1357. fsp->rport->port_id, error, fsp->recov_retry,
  1358. FC_MAX_RECOV_RETRY);
  1359. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1360. fc_fcp_rec(fsp);
  1361. else
  1362. fc_timeout_error(fsp);
  1363. break;
  1364. }
  1365. fc_fcp_unlock_pkt(fsp);
  1366. out:
  1367. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1368. }
  1369. /*
  1370. * Time out error routine:
  1371. * abort's the I/O close the exchange and
  1372. * send completion notification to scsi layer
  1373. */
  1374. static void fc_timeout_error(struct fc_fcp_pkt *fsp)
  1375. {
  1376. fsp->status_code = FC_CMD_TIME_OUT;
  1377. fsp->cdb_status = 0;
  1378. fsp->io_status = 0;
  1379. /*
  1380. * if this fails then we let the scsi command timer fire and
  1381. * scsi-ml escalate.
  1382. */
  1383. fc_fcp_send_abort(fsp);
  1384. }
  1385. /*
  1386. * Sequence retransmission request.
  1387. * This is called after receiving status but insufficient data, or
  1388. * when expecting status but the request has timed out.
  1389. */
  1390. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1391. {
  1392. struct fc_lport *lp = fsp->lp;
  1393. struct fc_rport *rport;
  1394. struct fc_rport_libfc_priv *rp;
  1395. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1396. struct fc_seq *seq;
  1397. struct fcp_srr *srr;
  1398. struct fc_frame *fp;
  1399. u8 cdb_op;
  1400. rport = fsp->rport;
  1401. rp = rport->dd_data;
  1402. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1403. if (!(rp->flags & FC_RP_FLAGS_RETRY) || rp->rp_state != RPORT_ST_READY)
  1404. goto retry; /* shouldn't happen */
  1405. fp = fc_frame_alloc(lp, sizeof(*srr));
  1406. if (!fp)
  1407. goto retry;
  1408. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1409. memset(srr, 0, sizeof(*srr));
  1410. srr->srr_op = ELS_SRR;
  1411. srr->srr_ox_id = htons(ep->oxid);
  1412. srr->srr_rx_id = htons(ep->rxid);
  1413. srr->srr_r_ctl = r_ctl;
  1414. srr->srr_rel_off = htonl(offset);
  1415. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1416. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  1417. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1418. seq = lp->tt.exch_seq_send(lp, fp, fc_fcp_srr_resp, NULL,
  1419. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
  1420. if (!seq) {
  1421. fc_frame_free(fp);
  1422. goto retry;
  1423. }
  1424. fsp->recov_seq = seq;
  1425. fsp->xfer_len = offset;
  1426. fsp->xfer_contig_end = offset;
  1427. fsp->state &= ~FC_SRB_RCV_STATUS;
  1428. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1429. return;
  1430. retry:
  1431. fc_fcp_retry_cmd(fsp);
  1432. }
  1433. /*
  1434. * Handle response from SRR.
  1435. */
  1436. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1437. {
  1438. struct fc_fcp_pkt *fsp = arg;
  1439. struct fc_frame_header *fh;
  1440. if (IS_ERR(fp)) {
  1441. fc_fcp_srr_error(fsp, fp);
  1442. return;
  1443. }
  1444. if (fc_fcp_lock_pkt(fsp))
  1445. goto out;
  1446. fh = fc_frame_header_get(fp);
  1447. /*
  1448. * BUG? fc_fcp_srr_error calls exch_done which would release
  1449. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1450. * then fc_exch_timeout would be sending an abort. The exch_done
  1451. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1452. * an abort response though.
  1453. */
  1454. if (fh->fh_type == FC_TYPE_BLS) {
  1455. fc_fcp_unlock_pkt(fsp);
  1456. return;
  1457. }
  1458. fsp->recov_seq = NULL;
  1459. switch (fc_frame_payload_op(fp)) {
  1460. case ELS_LS_ACC:
  1461. fsp->recov_retry = 0;
  1462. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1463. break;
  1464. case ELS_LS_RJT:
  1465. default:
  1466. fc_timeout_error(fsp);
  1467. break;
  1468. }
  1469. fc_fcp_unlock_pkt(fsp);
  1470. fsp->lp->tt.exch_done(seq);
  1471. out:
  1472. fc_frame_free(fp);
  1473. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1474. }
  1475. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1476. {
  1477. if (fc_fcp_lock_pkt(fsp))
  1478. goto out;
  1479. fsp->lp->tt.exch_done(fsp->recov_seq);
  1480. fsp->recov_seq = NULL;
  1481. switch (PTR_ERR(fp)) {
  1482. case -FC_EX_TIMEOUT:
  1483. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1484. fc_fcp_rec(fsp);
  1485. else
  1486. fc_timeout_error(fsp);
  1487. break;
  1488. case -FC_EX_CLOSED: /* e.g., link failure */
  1489. /* fall through */
  1490. default:
  1491. fc_fcp_retry_cmd(fsp);
  1492. break;
  1493. }
  1494. fc_fcp_unlock_pkt(fsp);
  1495. out:
  1496. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1497. }
  1498. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lp)
  1499. {
  1500. /* lock ? */
  1501. return (lp->state == LPORT_ST_READY) && lp->link_up && !lp->qfull;
  1502. }
  1503. /**
  1504. * fc_queuecommand - The queuecommand function of the scsi template
  1505. * @cmd: struct scsi_cmnd to be executed
  1506. * @done: Callback function to be called when cmd is completed
  1507. *
  1508. * this is the i/o strategy routine, called by the scsi layer
  1509. * this routine is called with holding the host_lock.
  1510. */
  1511. int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
  1512. {
  1513. struct fc_lport *lp;
  1514. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1515. struct fc_fcp_pkt *fsp;
  1516. struct fc_rport_libfc_priv *rp;
  1517. int rval;
  1518. int rc = 0;
  1519. struct fcoe_dev_stats *stats;
  1520. lp = shost_priv(sc_cmd->device->host);
  1521. rval = fc_remote_port_chkready(rport);
  1522. if (rval) {
  1523. sc_cmd->result = rval;
  1524. done(sc_cmd);
  1525. goto out;
  1526. }
  1527. if (!*(struct fc_remote_port **)rport->dd_data) {
  1528. /*
  1529. * rport is transitioning from blocked/deleted to
  1530. * online
  1531. */
  1532. sc_cmd->result = DID_IMM_RETRY << 16;
  1533. done(sc_cmd);
  1534. goto out;
  1535. }
  1536. rp = rport->dd_data;
  1537. if (!fc_fcp_lport_queue_ready(lp)) {
  1538. rc = SCSI_MLQUEUE_HOST_BUSY;
  1539. goto out;
  1540. }
  1541. fsp = fc_fcp_pkt_alloc(lp, GFP_ATOMIC);
  1542. if (fsp == NULL) {
  1543. rc = SCSI_MLQUEUE_HOST_BUSY;
  1544. goto out;
  1545. }
  1546. /*
  1547. * build the libfc request pkt
  1548. */
  1549. fsp->cmd = sc_cmd; /* save the cmd */
  1550. fsp->lp = lp; /* save the softc ptr */
  1551. fsp->rport = rport; /* set the remote port ptr */
  1552. sc_cmd->scsi_done = done;
  1553. /*
  1554. * set up the transfer length
  1555. */
  1556. fsp->data_len = scsi_bufflen(sc_cmd);
  1557. fsp->xfer_len = 0;
  1558. /*
  1559. * setup the data direction
  1560. */
  1561. stats = fc_lport_get_stats(lp);
  1562. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1563. fsp->req_flags = FC_SRB_READ;
  1564. stats->InputRequests++;
  1565. stats->InputMegabytes = fsp->data_len;
  1566. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1567. fsp->req_flags = FC_SRB_WRITE;
  1568. stats->OutputRequests++;
  1569. stats->OutputMegabytes = fsp->data_len;
  1570. } else {
  1571. fsp->req_flags = 0;
  1572. stats->ControlRequests++;
  1573. }
  1574. fsp->tgt_flags = rp->flags;
  1575. init_timer(&fsp->timer);
  1576. fsp->timer.data = (unsigned long)fsp;
  1577. /*
  1578. * send it to the lower layer
  1579. * if we get -1 return then put the request in the pending
  1580. * queue.
  1581. */
  1582. rval = fc_fcp_pkt_send(lp, fsp);
  1583. if (rval != 0) {
  1584. fsp->state = FC_SRB_FREE;
  1585. fc_fcp_pkt_release(fsp);
  1586. rc = SCSI_MLQUEUE_HOST_BUSY;
  1587. }
  1588. out:
  1589. return rc;
  1590. }
  1591. EXPORT_SYMBOL(fc_queuecommand);
  1592. /**
  1593. * fc_io_compl() - Handle responses for completed commands
  1594. * @fsp: scsi packet
  1595. *
  1596. * Translates a error to a Linux SCSI error.
  1597. *
  1598. * The fcp packet lock must be held when calling.
  1599. */
  1600. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1601. {
  1602. struct fc_fcp_internal *si;
  1603. struct scsi_cmnd *sc_cmd;
  1604. struct fc_lport *lp;
  1605. unsigned long flags;
  1606. /* release outstanding ddp context */
  1607. fc_fcp_ddp_done(fsp);
  1608. fsp->state |= FC_SRB_COMPL;
  1609. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1610. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1611. del_timer_sync(&fsp->timer);
  1612. spin_lock_bh(&fsp->scsi_pkt_lock);
  1613. }
  1614. lp = fsp->lp;
  1615. si = fc_get_scsi_internal(lp);
  1616. spin_lock_irqsave(lp->host->host_lock, flags);
  1617. if (!fsp->cmd) {
  1618. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1619. return;
  1620. }
  1621. /*
  1622. * if a command timed out while we had to try and throttle IO
  1623. * and it is now getting cleaned up, then we are about to
  1624. * try again so clear the throttled flag incase we get more
  1625. * time outs.
  1626. */
  1627. if (si->throttled && fsp->state & FC_SRB_NOMEM)
  1628. si->throttled = 0;
  1629. sc_cmd = fsp->cmd;
  1630. fsp->cmd = NULL;
  1631. if (!sc_cmd->SCp.ptr) {
  1632. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1633. return;
  1634. }
  1635. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1636. switch (fsp->status_code) {
  1637. case FC_COMPLETE:
  1638. if (fsp->cdb_status == 0) {
  1639. /*
  1640. * good I/O status
  1641. */
  1642. sc_cmd->result = DID_OK << 16;
  1643. if (fsp->scsi_resid)
  1644. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1645. } else if (fsp->cdb_status == QUEUE_FULL) {
  1646. struct scsi_device *tmp_sdev;
  1647. struct scsi_device *sdev = sc_cmd->device;
  1648. shost_for_each_device(tmp_sdev, sdev->host) {
  1649. if (tmp_sdev->id != sdev->id)
  1650. continue;
  1651. if (tmp_sdev->queue_depth > 1) {
  1652. scsi_track_queue_full(tmp_sdev,
  1653. tmp_sdev->
  1654. queue_depth - 1);
  1655. }
  1656. }
  1657. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1658. } else {
  1659. /*
  1660. * transport level I/O was ok but scsi
  1661. * has non zero status
  1662. */
  1663. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1664. }
  1665. break;
  1666. case FC_ERROR:
  1667. sc_cmd->result = DID_ERROR << 16;
  1668. break;
  1669. case FC_DATA_UNDRUN:
  1670. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1671. /*
  1672. * scsi status is good but transport level
  1673. * underrun.
  1674. */
  1675. sc_cmd->result = DID_OK << 16;
  1676. } else {
  1677. /*
  1678. * scsi got underrun, this is an error
  1679. */
  1680. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1681. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1682. }
  1683. break;
  1684. case FC_DATA_OVRRUN:
  1685. /*
  1686. * overrun is an error
  1687. */
  1688. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1689. break;
  1690. case FC_CMD_ABORTED:
  1691. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1692. break;
  1693. case FC_CMD_TIME_OUT:
  1694. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1695. break;
  1696. case FC_CMD_RESET:
  1697. sc_cmd->result = (DID_RESET << 16);
  1698. break;
  1699. case FC_HRD_ERROR:
  1700. sc_cmd->result = (DID_NO_CONNECT << 16);
  1701. break;
  1702. default:
  1703. sc_cmd->result = (DID_ERROR << 16);
  1704. break;
  1705. }
  1706. list_del(&fsp->list);
  1707. sc_cmd->SCp.ptr = NULL;
  1708. sc_cmd->scsi_done(sc_cmd);
  1709. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1710. /* release ref from initial allocation in queue command */
  1711. fc_fcp_pkt_release(fsp);
  1712. }
  1713. /**
  1714. * fc_fcp_complete() - complete processing of a fcp packet
  1715. * @fsp: fcp packet
  1716. *
  1717. * This function may sleep if a fsp timer is pending.
  1718. * The host lock must not be held by caller.
  1719. */
  1720. void fc_fcp_complete(struct fc_fcp_pkt *fsp)
  1721. {
  1722. if (fc_fcp_lock_pkt(fsp))
  1723. return;
  1724. fc_fcp_complete_locked(fsp);
  1725. fc_fcp_unlock_pkt(fsp);
  1726. }
  1727. EXPORT_SYMBOL(fc_fcp_complete);
  1728. /**
  1729. * fc_eh_abort() - Abort a command
  1730. * @sc_cmd: scsi command to abort
  1731. *
  1732. * From scsi host template.
  1733. * send ABTS to the target device and wait for the response
  1734. * sc_cmd is the pointer to the command to be aborted.
  1735. */
  1736. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1737. {
  1738. struct fc_fcp_pkt *fsp;
  1739. struct fc_lport *lp;
  1740. int rc = FAILED;
  1741. unsigned long flags;
  1742. lp = shost_priv(sc_cmd->device->host);
  1743. if (lp->state != LPORT_ST_READY)
  1744. return rc;
  1745. else if (!lp->link_up)
  1746. return rc;
  1747. spin_lock_irqsave(lp->host->host_lock, flags);
  1748. fsp = CMD_SP(sc_cmd);
  1749. if (!fsp) {
  1750. /* command completed while scsi eh was setting up */
  1751. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1752. return SUCCESS;
  1753. }
  1754. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1755. fc_fcp_pkt_hold(fsp);
  1756. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1757. if (fc_fcp_lock_pkt(fsp)) {
  1758. /* completed while we were waiting for timer to be deleted */
  1759. rc = SUCCESS;
  1760. goto release_pkt;
  1761. }
  1762. rc = fc_fcp_pkt_abort(lp, fsp);
  1763. fc_fcp_unlock_pkt(fsp);
  1764. release_pkt:
  1765. fc_fcp_pkt_release(fsp);
  1766. return rc;
  1767. }
  1768. EXPORT_SYMBOL(fc_eh_abort);
  1769. /**
  1770. * fc_eh_device_reset() Reset a single LUN
  1771. * @sc_cmd: scsi command
  1772. *
  1773. * Set from scsi host template to send tm cmd to the target and wait for the
  1774. * response.
  1775. */
  1776. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1777. {
  1778. struct fc_lport *lp;
  1779. struct fc_fcp_pkt *fsp;
  1780. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1781. int rc = FAILED;
  1782. struct fc_rport_libfc_priv *rp;
  1783. int rval;
  1784. rval = fc_remote_port_chkready(rport);
  1785. if (rval)
  1786. goto out;
  1787. rp = rport->dd_data;
  1788. lp = shost_priv(sc_cmd->device->host);
  1789. if (lp->state != LPORT_ST_READY)
  1790. return rc;
  1791. FC_SCSI_DBG(lp, "Resetting rport (%6x)\n", rport->port_id);
  1792. fsp = fc_fcp_pkt_alloc(lp, GFP_NOIO);
  1793. if (fsp == NULL) {
  1794. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1795. sc_cmd->result = DID_NO_CONNECT << 16;
  1796. goto out;
  1797. }
  1798. /*
  1799. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1800. * the sc passed in is not setup for execution like when sent
  1801. * through the queuecommand callout.
  1802. */
  1803. fsp->lp = lp; /* save the softc ptr */
  1804. fsp->rport = rport; /* set the remote port ptr */
  1805. /*
  1806. * flush outstanding commands
  1807. */
  1808. rc = fc_lun_reset(lp, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1809. fsp->state = FC_SRB_FREE;
  1810. fc_fcp_pkt_release(fsp);
  1811. out:
  1812. return rc;
  1813. }
  1814. EXPORT_SYMBOL(fc_eh_device_reset);
  1815. /**
  1816. * fc_eh_host_reset() - The reset function will reset the ports on the host.
  1817. * @sc_cmd: scsi command
  1818. */
  1819. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1820. {
  1821. struct Scsi_Host *shost = sc_cmd->device->host;
  1822. struct fc_lport *lp = shost_priv(shost);
  1823. unsigned long wait_tmo;
  1824. FC_SCSI_DBG(lp, "Resetting host\n");
  1825. lp->tt.lport_reset(lp);
  1826. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1827. while (!fc_fcp_lport_queue_ready(lp) && time_before(jiffies, wait_tmo))
  1828. msleep(1000);
  1829. if (fc_fcp_lport_queue_ready(lp)) {
  1830. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1831. "on port (%6x)\n", fc_host_port_id(lp->host));
  1832. return SUCCESS;
  1833. } else {
  1834. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1835. "port (%6x) is not ready.\n",
  1836. fc_host_port_id(lp->host));
  1837. return FAILED;
  1838. }
  1839. }
  1840. EXPORT_SYMBOL(fc_eh_host_reset);
  1841. /**
  1842. * fc_slave_alloc() - configure queue depth
  1843. * @sdev: scsi device
  1844. *
  1845. * Configures queue depth based on host's cmd_per_len. If not set
  1846. * then we use the libfc default.
  1847. */
  1848. int fc_slave_alloc(struct scsi_device *sdev)
  1849. {
  1850. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1851. int queue_depth;
  1852. if (!rport || fc_remote_port_chkready(rport))
  1853. return -ENXIO;
  1854. if (sdev->tagged_supported) {
  1855. if (sdev->host->hostt->cmd_per_lun)
  1856. queue_depth = sdev->host->hostt->cmd_per_lun;
  1857. else
  1858. queue_depth = FC_FCP_DFLT_QUEUE_DEPTH;
  1859. scsi_activate_tcq(sdev, queue_depth);
  1860. }
  1861. return 0;
  1862. }
  1863. EXPORT_SYMBOL(fc_slave_alloc);
  1864. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1865. {
  1866. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1867. return sdev->queue_depth;
  1868. }
  1869. EXPORT_SYMBOL(fc_change_queue_depth);
  1870. int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
  1871. {
  1872. if (sdev->tagged_supported) {
  1873. scsi_set_tag_type(sdev, tag_type);
  1874. if (tag_type)
  1875. scsi_activate_tcq(sdev, sdev->queue_depth);
  1876. else
  1877. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1878. } else
  1879. tag_type = 0;
  1880. return tag_type;
  1881. }
  1882. EXPORT_SYMBOL(fc_change_queue_type);
  1883. void fc_fcp_destroy(struct fc_lport *lp)
  1884. {
  1885. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  1886. if (!list_empty(&si->scsi_pkt_queue))
  1887. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  1888. "port (%6x)\n", fc_host_port_id(lp->host));
  1889. mempool_destroy(si->scsi_pkt_pool);
  1890. kfree(si);
  1891. lp->scsi_priv = NULL;
  1892. }
  1893. EXPORT_SYMBOL(fc_fcp_destroy);
  1894. int fc_fcp_init(struct fc_lport *lp)
  1895. {
  1896. int rc;
  1897. struct fc_fcp_internal *si;
  1898. if (!lp->tt.fcp_cmd_send)
  1899. lp->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1900. if (!lp->tt.fcp_cleanup)
  1901. lp->tt.fcp_cleanup = fc_fcp_cleanup;
  1902. if (!lp->tt.fcp_abort_io)
  1903. lp->tt.fcp_abort_io = fc_fcp_abort_io;
  1904. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  1905. if (!si)
  1906. return -ENOMEM;
  1907. lp->scsi_priv = si;
  1908. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  1909. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  1910. if (!si->scsi_pkt_pool) {
  1911. rc = -ENOMEM;
  1912. goto free_internal;
  1913. }
  1914. return 0;
  1915. free_internal:
  1916. kfree(si);
  1917. return rc;
  1918. }
  1919. EXPORT_SYMBOL(fc_fcp_init);
  1920. static int __init libfc_init(void)
  1921. {
  1922. int rc;
  1923. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1924. sizeof(struct fc_fcp_pkt),
  1925. 0, SLAB_HWCACHE_ALIGN, NULL);
  1926. if (scsi_pkt_cachep == NULL) {
  1927. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  1928. "module load failed!");
  1929. return -ENOMEM;
  1930. }
  1931. rc = fc_setup_exch_mgr();
  1932. if (rc)
  1933. goto destroy_pkt_cache;
  1934. rc = fc_setup_rport();
  1935. if (rc)
  1936. goto destroy_em;
  1937. return rc;
  1938. destroy_em:
  1939. fc_destroy_exch_mgr();
  1940. destroy_pkt_cache:
  1941. kmem_cache_destroy(scsi_pkt_cachep);
  1942. return rc;
  1943. }
  1944. static void __exit libfc_exit(void)
  1945. {
  1946. kmem_cache_destroy(scsi_pkt_cachep);
  1947. fc_destroy_exch_mgr();
  1948. fc_destroy_rport();
  1949. }
  1950. module_init(libfc_init);
  1951. module_exit(libfc_exit);