fc_fcp.c 58 KB

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