fc_fcp.c 54 KB

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