libiscsi.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <asm/unaligned.h>
  31. #include <net/tcp.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include <scsi/scsi_host.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/iscsi_proto.h>
  39. #include <scsi/scsi_transport.h>
  40. #include <scsi/scsi_transport_iscsi.h>
  41. #include <scsi/libiscsi.h>
  42. static int iscsi_dbg_lib_conn;
  43. module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
  44. S_IRUGO | S_IWUSR);
  45. MODULE_PARM_DESC(debug_libiscsi_conn,
  46. "Turn on debugging for connections in libiscsi module. "
  47. "Set to 1 to turn on, and zero to turn off. Default is off.");
  48. static int iscsi_dbg_lib_session;
  49. module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
  50. S_IRUGO | S_IWUSR);
  51. MODULE_PARM_DESC(debug_libiscsi_session,
  52. "Turn on debugging for sessions in libiscsi module. "
  53. "Set to 1 to turn on, and zero to turn off. Default is off.");
  54. static int iscsi_dbg_lib_eh;
  55. module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
  56. S_IRUGO | S_IWUSR);
  57. MODULE_PARM_DESC(debug_libiscsi_eh,
  58. "Turn on debugging for error handling in libiscsi module. "
  59. "Set to 1 to turn on, and zero to turn off. Default is off.");
  60. #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
  61. do { \
  62. if (iscsi_dbg_lib_conn) \
  63. iscsi_conn_printk(KERN_INFO, _conn, \
  64. "%s " dbg_fmt, \
  65. __func__, ##arg); \
  66. } while (0);
  67. #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
  68. do { \
  69. if (iscsi_dbg_lib_session) \
  70. iscsi_session_printk(KERN_INFO, _session, \
  71. "%s " dbg_fmt, \
  72. __func__, ##arg); \
  73. } while (0);
  74. #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
  75. do { \
  76. if (iscsi_dbg_lib_eh) \
  77. iscsi_session_printk(KERN_INFO, _session, \
  78. "%s " dbg_fmt, \
  79. __func__, ##arg); \
  80. } while (0);
  81. inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
  82. {
  83. struct Scsi_Host *shost = conn->session->host;
  84. struct iscsi_host *ihost = shost_priv(shost);
  85. if (ihost->workq)
  86. queue_work(ihost->workq, &conn->xmitwork);
  87. }
  88. EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
  89. static void __iscsi_update_cmdsn(struct iscsi_session *session,
  90. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  91. {
  92. /*
  93. * standard specifies this check for when to update expected and
  94. * max sequence numbers
  95. */
  96. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  97. return;
  98. if (exp_cmdsn != session->exp_cmdsn &&
  99. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  100. session->exp_cmdsn = exp_cmdsn;
  101. if (max_cmdsn != session->max_cmdsn &&
  102. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  103. session->max_cmdsn = max_cmdsn;
  104. /*
  105. * if the window closed with IO queued, then kick the
  106. * xmit thread
  107. */
  108. if (!list_empty(&session->leadconn->cmdqueue) ||
  109. !list_empty(&session->leadconn->mgmtqueue))
  110. iscsi_conn_queue_work(session->leadconn);
  111. }
  112. }
  113. void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  114. {
  115. __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
  116. be32_to_cpu(hdr->max_cmdsn));
  117. }
  118. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  119. /**
  120. * iscsi_prep_data_out_pdu - initialize Data-Out
  121. * @task: scsi command task
  122. * @r2t: R2T info
  123. * @hdr: iscsi data in pdu
  124. *
  125. * Notes:
  126. * Initialize Data-Out within this R2T sequence and finds
  127. * proper data_offset within this SCSI command.
  128. *
  129. * This function is called with connection lock taken.
  130. **/
  131. void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
  132. struct iscsi_data *hdr)
  133. {
  134. struct iscsi_conn *conn = task->conn;
  135. unsigned int left = r2t->data_length - r2t->sent;
  136. task->hdr_len = sizeof(struct iscsi_data);
  137. memset(hdr, 0, sizeof(struct iscsi_data));
  138. hdr->ttt = r2t->ttt;
  139. hdr->datasn = cpu_to_be32(r2t->datasn);
  140. r2t->datasn++;
  141. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  142. hdr->lun = task->lun;
  143. hdr->itt = task->hdr_itt;
  144. hdr->exp_statsn = r2t->exp_statsn;
  145. hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
  146. if (left > conn->max_xmit_dlength) {
  147. hton24(hdr->dlength, conn->max_xmit_dlength);
  148. r2t->data_count = conn->max_xmit_dlength;
  149. hdr->flags = 0;
  150. } else {
  151. hton24(hdr->dlength, left);
  152. r2t->data_count = left;
  153. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  154. }
  155. conn->dataout_pdus_cnt++;
  156. }
  157. EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
  158. static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
  159. {
  160. unsigned exp_len = task->hdr_len + len;
  161. if (exp_len > task->hdr_max) {
  162. WARN_ON(1);
  163. return -EINVAL;
  164. }
  165. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  166. task->hdr_len = exp_len;
  167. return 0;
  168. }
  169. /*
  170. * make an extended cdb AHS
  171. */
  172. static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
  173. {
  174. struct scsi_cmnd *cmd = task->sc;
  175. unsigned rlen, pad_len;
  176. unsigned short ahslength;
  177. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  178. int rc;
  179. ecdb_ahdr = iscsi_next_hdr(task);
  180. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  181. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  182. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  183. pad_len = iscsi_padding(rlen);
  184. rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
  185. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  186. if (rc)
  187. return rc;
  188. if (pad_len)
  189. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  190. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  191. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  192. ecdb_ahdr->reserved = 0;
  193. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  194. ISCSI_DBG_SESSION(task->conn->session,
  195. "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  196. "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
  197. "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
  198. task->hdr_len);
  199. return 0;
  200. }
  201. static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
  202. {
  203. struct scsi_cmnd *sc = task->sc;
  204. struct iscsi_rlength_ahdr *rlen_ahdr;
  205. int rc;
  206. rlen_ahdr = iscsi_next_hdr(task);
  207. rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
  208. if (rc)
  209. return rc;
  210. rlen_ahdr->ahslength =
  211. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  212. sizeof(rlen_ahdr->reserved));
  213. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  214. rlen_ahdr->reserved = 0;
  215. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  216. ISCSI_DBG_SESSION(task->conn->session,
  217. "bidi-in rlen_ahdr->read_length(%d) "
  218. "rlen_ahdr->ahslength(%d)\n",
  219. be32_to_cpu(rlen_ahdr->read_length),
  220. be16_to_cpu(rlen_ahdr->ahslength));
  221. return 0;
  222. }
  223. /**
  224. * iscsi_check_tmf_restrictions - check if a task is affected by TMF
  225. * @task: iscsi task
  226. * @opcode: opcode to check for
  227. *
  228. * During TMF a task has to be checked if it's affected.
  229. * All unrelated I/O can be passed through, but I/O to the
  230. * affected LUN should be restricted.
  231. * If 'fast_abort' is set we won't be sending any I/O to the
  232. * affected LUN.
  233. * Otherwise the target is waiting for all TTTs to be completed,
  234. * so we have to send all outstanding Data-Out PDUs to the target.
  235. */
  236. static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
  237. {
  238. struct iscsi_conn *conn = task->conn;
  239. struct iscsi_tm *tmf = &conn->tmhdr;
  240. unsigned int hdr_lun;
  241. if (conn->tmf_state == TMF_INITIAL)
  242. return 0;
  243. if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
  244. return 0;
  245. switch (ISCSI_TM_FUNC_VALUE(tmf)) {
  246. case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
  247. /*
  248. * Allow PDUs for unrelated LUNs
  249. */
  250. hdr_lun = scsilun_to_int(&tmf->lun);
  251. if (hdr_lun != task->sc->device->lun)
  252. return 0;
  253. /* fall through */
  254. case ISCSI_TM_FUNC_TARGET_WARM_RESET:
  255. /*
  256. * Fail all SCSI cmd PDUs
  257. */
  258. if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
  259. iscsi_conn_printk(KERN_INFO, conn,
  260. "task [op %x/%x itt "
  261. "0x%x/0x%x] "
  262. "rejected.\n",
  263. task->hdr->opcode, opcode,
  264. task->itt, task->hdr_itt);
  265. return -EACCES;
  266. }
  267. /*
  268. * And also all data-out PDUs in response to R2T
  269. * if fast_abort is set.
  270. */
  271. if (conn->session->fast_abort) {
  272. iscsi_conn_printk(KERN_INFO, conn,
  273. "task [op %x/%x itt "
  274. "0x%x/0x%x] fast abort.\n",
  275. task->hdr->opcode, opcode,
  276. task->itt, task->hdr_itt);
  277. return -EACCES;
  278. }
  279. break;
  280. case ISCSI_TM_FUNC_ABORT_TASK:
  281. /*
  282. * the caller has already checked if the task
  283. * they want to abort was in the pending queue so if
  284. * we are here the cmd pdu has gone out already, and
  285. * we will only hit this for data-outs
  286. */
  287. if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
  288. task->hdr_itt == tmf->rtt) {
  289. ISCSI_DBG_SESSION(conn->session,
  290. "Preventing task %x/%x from sending "
  291. "data-out due to abort task in "
  292. "progress\n", task->itt,
  293. task->hdr_itt);
  294. return -EACCES;
  295. }
  296. break;
  297. }
  298. return 0;
  299. }
  300. /**
  301. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  302. * @task: iscsi task
  303. *
  304. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  305. * fields like dlength or final based on how much data it sends
  306. */
  307. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
  308. {
  309. struct iscsi_conn *conn = task->conn;
  310. struct iscsi_session *session = conn->session;
  311. struct scsi_cmnd *sc = task->sc;
  312. struct iscsi_scsi_req *hdr;
  313. unsigned hdrlength, cmd_len;
  314. itt_t itt;
  315. int rc;
  316. rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
  317. if (rc)
  318. return rc;
  319. if (conn->session->tt->alloc_pdu) {
  320. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
  321. if (rc)
  322. return rc;
  323. }
  324. hdr = (struct iscsi_scsi_req *)task->hdr;
  325. itt = hdr->itt;
  326. memset(hdr, 0, sizeof(*hdr));
  327. if (session->tt->parse_pdu_itt)
  328. hdr->itt = task->hdr_itt = itt;
  329. else
  330. hdr->itt = task->hdr_itt = build_itt(task->itt,
  331. task->conn->session->age);
  332. task->hdr_len = 0;
  333. rc = iscsi_add_hdr(task, sizeof(*hdr));
  334. if (rc)
  335. return rc;
  336. hdr->opcode = ISCSI_OP_SCSI_CMD;
  337. hdr->flags = ISCSI_ATTR_SIMPLE;
  338. int_to_scsilun(sc->device->lun, &hdr->lun);
  339. task->lun = hdr->lun;
  340. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  341. cmd_len = sc->cmd_len;
  342. if (cmd_len < ISCSI_CDB_SIZE)
  343. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  344. else if (cmd_len > ISCSI_CDB_SIZE) {
  345. rc = iscsi_prep_ecdb_ahs(task);
  346. if (rc)
  347. return rc;
  348. cmd_len = ISCSI_CDB_SIZE;
  349. }
  350. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  351. task->imm_count = 0;
  352. if (scsi_bidi_cmnd(sc)) {
  353. hdr->flags |= ISCSI_FLAG_CMD_READ;
  354. rc = iscsi_prep_bidi_ahs(task);
  355. if (rc)
  356. return rc;
  357. }
  358. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  359. unsigned out_len = scsi_out(sc)->length;
  360. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  361. hdr->data_length = cpu_to_be32(out_len);
  362. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  363. /*
  364. * Write counters:
  365. *
  366. * imm_count bytes to be sent right after
  367. * SCSI PDU Header
  368. *
  369. * unsol_count bytes(as Data-Out) to be sent
  370. * without R2T ack right after
  371. * immediate data
  372. *
  373. * r2t data_length bytes to be sent via R2T ack's
  374. *
  375. * pad_count bytes to be sent as zero-padding
  376. */
  377. memset(r2t, 0, sizeof(*r2t));
  378. if (session->imm_data_en) {
  379. if (out_len >= session->first_burst)
  380. task->imm_count = min(session->first_burst,
  381. conn->max_xmit_dlength);
  382. else
  383. task->imm_count = min(out_len,
  384. conn->max_xmit_dlength);
  385. hton24(hdr->dlength, task->imm_count);
  386. } else
  387. zero_data(hdr->dlength);
  388. if (!session->initial_r2t_en) {
  389. r2t->data_length = min(session->first_burst, out_len) -
  390. task->imm_count;
  391. r2t->data_offset = task->imm_count;
  392. r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  393. r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
  394. }
  395. if (!task->unsol_r2t.data_length)
  396. /* No unsolicit Data-Out's */
  397. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  398. } else {
  399. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  400. zero_data(hdr->dlength);
  401. hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
  402. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  403. hdr->flags |= ISCSI_FLAG_CMD_READ;
  404. }
  405. /* calculate size of additional header segments (AHSs) */
  406. hdrlength = task->hdr_len - sizeof(*hdr);
  407. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  408. hdrlength /= ISCSI_PAD_LEN;
  409. WARN_ON(hdrlength >= 256);
  410. hdr->hlength = hdrlength & 0xFF;
  411. hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
  412. if (session->tt->init_task && session->tt->init_task(task))
  413. return -EIO;
  414. task->state = ISCSI_TASK_RUNNING;
  415. session->cmdsn++;
  416. conn->scsicmd_pdus_cnt++;
  417. ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
  418. "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
  419. scsi_bidi_cmnd(sc) ? "bidirectional" :
  420. sc->sc_data_direction == DMA_TO_DEVICE ?
  421. "write" : "read", conn->id, sc, sc->cmnd[0],
  422. task->itt, scsi_bufflen(sc),
  423. scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  424. session->cmdsn,
  425. session->max_cmdsn - session->exp_cmdsn + 1);
  426. return 0;
  427. }
  428. /**
  429. * iscsi_free_task - free a task
  430. * @task: iscsi cmd task
  431. *
  432. * Must be called with session lock.
  433. * This function returns the scsi command to scsi-ml or cleans
  434. * up mgmt tasks then returns the task to the pool.
  435. */
  436. static void iscsi_free_task(struct iscsi_task *task)
  437. {
  438. struct iscsi_conn *conn = task->conn;
  439. struct iscsi_session *session = conn->session;
  440. struct scsi_cmnd *sc = task->sc;
  441. int oldstate = task->state;
  442. ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
  443. task->itt, task->state, task->sc);
  444. session->tt->cleanup_task(task);
  445. task->state = ISCSI_TASK_FREE;
  446. task->sc = NULL;
  447. /*
  448. * login task is preallocated so do not free
  449. */
  450. if (conn->login_task == task)
  451. return;
  452. kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
  453. if (sc) {
  454. task->sc = NULL;
  455. /* SCSI eh reuses commands to verify us */
  456. sc->SCp.ptr = NULL;
  457. /*
  458. * queue command may call this to free the task, so
  459. * it will decide how to return sc to scsi-ml.
  460. */
  461. if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
  462. sc->scsi_done(sc);
  463. }
  464. }
  465. void __iscsi_get_task(struct iscsi_task *task)
  466. {
  467. atomic_inc(&task->refcount);
  468. }
  469. EXPORT_SYMBOL_GPL(__iscsi_get_task);
  470. void __iscsi_put_task(struct iscsi_task *task)
  471. {
  472. if (atomic_dec_and_test(&task->refcount))
  473. iscsi_free_task(task);
  474. }
  475. EXPORT_SYMBOL_GPL(__iscsi_put_task);
  476. void iscsi_put_task(struct iscsi_task *task)
  477. {
  478. struct iscsi_session *session = task->conn->session;
  479. spin_lock_bh(&session->lock);
  480. __iscsi_put_task(task);
  481. spin_unlock_bh(&session->lock);
  482. }
  483. EXPORT_SYMBOL_GPL(iscsi_put_task);
  484. /**
  485. * iscsi_complete_task - finish a task
  486. * @task: iscsi cmd task
  487. * @state: state to complete task with
  488. *
  489. * Must be called with session lock.
  490. */
  491. static void iscsi_complete_task(struct iscsi_task *task, int state)
  492. {
  493. struct iscsi_conn *conn = task->conn;
  494. ISCSI_DBG_SESSION(conn->session,
  495. "complete task itt 0x%x state %d sc %p\n",
  496. task->itt, task->state, task->sc);
  497. if (task->state == ISCSI_TASK_COMPLETED ||
  498. task->state == ISCSI_TASK_ABRT_TMF ||
  499. task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
  500. task->state == ISCSI_TASK_REQUEUE_SCSIQ)
  501. return;
  502. WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
  503. task->state = state;
  504. if (!list_empty(&task->running))
  505. list_del_init(&task->running);
  506. if (conn->task == task)
  507. conn->task = NULL;
  508. if (conn->ping_task == task)
  509. conn->ping_task = NULL;
  510. /* release get from queueing */
  511. __iscsi_put_task(task);
  512. }
  513. /**
  514. * iscsi_complete_scsi_task - finish scsi task normally
  515. * @task: iscsi task for scsi cmd
  516. * @exp_cmdsn: expected cmd sn in cpu format
  517. * @max_cmdsn: max cmd sn in cpu format
  518. *
  519. * This is used when drivers do not need or cannot perform
  520. * lower level pdu processing.
  521. *
  522. * Called with session lock
  523. */
  524. void iscsi_complete_scsi_task(struct iscsi_task *task,
  525. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  526. {
  527. struct iscsi_conn *conn = task->conn;
  528. ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
  529. conn->last_recv = jiffies;
  530. __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
  531. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  532. }
  533. EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
  534. /*
  535. * session lock must be held and if not called for a task that is
  536. * still pending or from the xmit thread, then xmit thread must
  537. * be suspended.
  538. */
  539. static void fail_scsi_task(struct iscsi_task *task, int err)
  540. {
  541. struct iscsi_conn *conn = task->conn;
  542. struct scsi_cmnd *sc;
  543. int state;
  544. /*
  545. * if a command completes and we get a successful tmf response
  546. * we will hit this because the scsi eh abort code does not take
  547. * a ref to the task.
  548. */
  549. sc = task->sc;
  550. if (!sc)
  551. return;
  552. if (task->state == ISCSI_TASK_PENDING) {
  553. /*
  554. * cmd never made it to the xmit thread, so we should not count
  555. * the cmd in the sequencing
  556. */
  557. conn->session->queued_cmdsn--;
  558. /* it was never sent so just complete like normal */
  559. state = ISCSI_TASK_COMPLETED;
  560. } else if (err == DID_TRANSPORT_DISRUPTED)
  561. state = ISCSI_TASK_ABRT_SESS_RECOV;
  562. else
  563. state = ISCSI_TASK_ABRT_TMF;
  564. sc->result = err << 16;
  565. if (!scsi_bidi_cmnd(sc))
  566. scsi_set_resid(sc, scsi_bufflen(sc));
  567. else {
  568. scsi_out(sc)->resid = scsi_out(sc)->length;
  569. scsi_in(sc)->resid = scsi_in(sc)->length;
  570. }
  571. iscsi_complete_task(task, state);
  572. }
  573. static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
  574. struct iscsi_task *task)
  575. {
  576. struct iscsi_session *session = conn->session;
  577. struct iscsi_hdr *hdr = task->hdr;
  578. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  579. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  580. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  581. return -ENOTCONN;
  582. if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
  583. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  584. /*
  585. * pre-format CmdSN for outgoing PDU.
  586. */
  587. nop->cmdsn = cpu_to_be32(session->cmdsn);
  588. if (hdr->itt != RESERVED_ITT) {
  589. /*
  590. * TODO: We always use immediate for normal session pdus.
  591. * If we start to send tmfs or nops as non-immediate then
  592. * we should start checking the cmdsn numbers for mgmt tasks.
  593. *
  594. * During discovery sessions iscsid sends TEXT as non immediate,
  595. * but we always only send one PDU at a time.
  596. */
  597. if (conn->c_stage == ISCSI_CONN_STARTED &&
  598. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  599. session->queued_cmdsn++;
  600. session->cmdsn++;
  601. }
  602. }
  603. if (session->tt->init_task && session->tt->init_task(task))
  604. return -EIO;
  605. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  606. session->state = ISCSI_STATE_LOGGING_OUT;
  607. task->state = ISCSI_TASK_RUNNING;
  608. ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
  609. "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
  610. hdr->itt, task->data_count);
  611. return 0;
  612. }
  613. static struct iscsi_task *
  614. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  615. char *data, uint32_t data_size)
  616. {
  617. struct iscsi_session *session = conn->session;
  618. struct iscsi_host *ihost = shost_priv(session->host);
  619. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  620. struct iscsi_task *task;
  621. itt_t itt;
  622. if (session->state == ISCSI_STATE_TERMINATE)
  623. return NULL;
  624. if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
  625. /*
  626. * Login and Text are sent serially, in
  627. * request-followed-by-response sequence.
  628. * Same task can be used. Same ITT must be used.
  629. * Note that login_task is preallocated at conn_create().
  630. */
  631. if (conn->login_task->state != ISCSI_TASK_FREE) {
  632. iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
  633. "progress. Cannot start new task.\n");
  634. return NULL;
  635. }
  636. if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
  637. iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN);
  638. return NULL;
  639. }
  640. task = conn->login_task;
  641. } else {
  642. if (session->state != ISCSI_STATE_LOGGED_IN)
  643. return NULL;
  644. if (data_size != 0) {
  645. iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
  646. return NULL;
  647. }
  648. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  649. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  650. if (!kfifo_out(&session->cmdpool.queue,
  651. (void*)&task, sizeof(void*)))
  652. return NULL;
  653. }
  654. /*
  655. * released in complete pdu for task we expect a response for, and
  656. * released by the lld when it has transmitted the task for
  657. * pdus we do not expect a response for.
  658. */
  659. atomic_set(&task->refcount, 1);
  660. task->conn = conn;
  661. task->sc = NULL;
  662. INIT_LIST_HEAD(&task->running);
  663. task->state = ISCSI_TASK_PENDING;
  664. if (data_size) {
  665. memcpy(task->data, data, data_size);
  666. task->data_count = data_size;
  667. } else
  668. task->data_count = 0;
  669. if (conn->session->tt->alloc_pdu) {
  670. if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
  671. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
  672. "pdu for mgmt task.\n");
  673. goto free_task;
  674. }
  675. }
  676. itt = task->hdr->itt;
  677. task->hdr_len = sizeof(struct iscsi_hdr);
  678. memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
  679. if (hdr->itt != RESERVED_ITT) {
  680. if (session->tt->parse_pdu_itt)
  681. task->hdr->itt = itt;
  682. else
  683. task->hdr->itt = build_itt(task->itt,
  684. task->conn->session->age);
  685. }
  686. if (!ihost->workq) {
  687. if (iscsi_prep_mgmt_task(conn, task))
  688. goto free_task;
  689. if (session->tt->xmit_task(task))
  690. goto free_task;
  691. } else {
  692. list_add_tail(&task->running, &conn->mgmtqueue);
  693. iscsi_conn_queue_work(conn);
  694. }
  695. return task;
  696. free_task:
  697. __iscsi_put_task(task);
  698. return NULL;
  699. }
  700. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  701. char *data, uint32_t data_size)
  702. {
  703. struct iscsi_conn *conn = cls_conn->dd_data;
  704. struct iscsi_session *session = conn->session;
  705. int err = 0;
  706. spin_lock_bh(&session->lock);
  707. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  708. err = -EPERM;
  709. spin_unlock_bh(&session->lock);
  710. return err;
  711. }
  712. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  713. /**
  714. * iscsi_cmd_rsp - SCSI Command Response processing
  715. * @conn: iscsi connection
  716. * @hdr: iscsi header
  717. * @task: scsi command task
  718. * @data: cmd data buffer
  719. * @datalen: len of buffer
  720. *
  721. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  722. * then completes the command and task.
  723. **/
  724. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  725. struct iscsi_task *task, char *data,
  726. int datalen)
  727. {
  728. struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
  729. struct iscsi_session *session = conn->session;
  730. struct scsi_cmnd *sc = task->sc;
  731. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  732. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  733. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  734. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  735. sc->result = DID_ERROR << 16;
  736. goto out;
  737. }
  738. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  739. uint16_t senselen;
  740. if (datalen < 2) {
  741. invalid_datalen:
  742. iscsi_conn_printk(KERN_ERR, conn,
  743. "Got CHECK_CONDITION but invalid data "
  744. "buffer size of %d\n", datalen);
  745. sc->result = DID_BAD_TARGET << 16;
  746. goto out;
  747. }
  748. senselen = get_unaligned_be16(data);
  749. if (datalen < senselen)
  750. goto invalid_datalen;
  751. memcpy(sc->sense_buffer, data + 2,
  752. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  753. ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
  754. min_t(uint16_t, senselen,
  755. SCSI_SENSE_BUFFERSIZE));
  756. }
  757. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  758. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  759. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  760. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  761. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  762. res_count <= scsi_in(sc)->length))
  763. scsi_in(sc)->resid = res_count;
  764. else
  765. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  766. }
  767. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  768. ISCSI_FLAG_CMD_OVERFLOW)) {
  769. int res_count = be32_to_cpu(rhdr->residual_count);
  770. if (res_count > 0 &&
  771. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  772. res_count <= scsi_bufflen(sc)))
  773. /* write side for bidi or uni-io set_resid */
  774. scsi_set_resid(sc, res_count);
  775. else
  776. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  777. }
  778. out:
  779. ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
  780. sc, sc->result, task->itt);
  781. conn->scsirsp_pdus_cnt++;
  782. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  783. }
  784. /**
  785. * iscsi_data_in_rsp - SCSI Data-In Response processing
  786. * @conn: iscsi connection
  787. * @hdr: iscsi pdu
  788. * @task: scsi command task
  789. **/
  790. static void
  791. iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  792. struct iscsi_task *task)
  793. {
  794. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
  795. struct scsi_cmnd *sc = task->sc;
  796. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  797. return;
  798. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
  799. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  800. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  801. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  802. ISCSI_FLAG_DATA_OVERFLOW)) {
  803. int res_count = be32_to_cpu(rhdr->residual_count);
  804. if (res_count > 0 &&
  805. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  806. res_count <= scsi_in(sc)->length))
  807. scsi_in(sc)->resid = res_count;
  808. else
  809. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  810. }
  811. ISCSI_DBG_SESSION(conn->session, "data in with status done "
  812. "[sc %p res %d itt 0x%x]\n",
  813. sc, sc->result, task->itt);
  814. conn->scsirsp_pdus_cnt++;
  815. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  816. }
  817. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  818. {
  819. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  820. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  821. conn->tmfrsp_pdus_cnt++;
  822. if (conn->tmf_state != TMF_QUEUED)
  823. return;
  824. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  825. conn->tmf_state = TMF_SUCCESS;
  826. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  827. conn->tmf_state = TMF_NOT_FOUND;
  828. else
  829. conn->tmf_state = TMF_FAILED;
  830. wake_up(&conn->ehwait);
  831. }
  832. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  833. {
  834. struct iscsi_nopout hdr;
  835. struct iscsi_task *task;
  836. if (!rhdr && conn->ping_task)
  837. return;
  838. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  839. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  840. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  841. if (rhdr) {
  842. hdr.lun = rhdr->lun;
  843. hdr.ttt = rhdr->ttt;
  844. hdr.itt = RESERVED_ITT;
  845. } else
  846. hdr.ttt = RESERVED_ITT;
  847. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  848. if (!task)
  849. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  850. else if (!rhdr) {
  851. /* only track our nops */
  852. conn->ping_task = task;
  853. conn->last_ping = jiffies;
  854. }
  855. }
  856. static int iscsi_nop_out_rsp(struct iscsi_task *task,
  857. struct iscsi_nopin *nop, char *data, int datalen)
  858. {
  859. struct iscsi_conn *conn = task->conn;
  860. int rc = 0;
  861. if (conn->ping_task != task) {
  862. /*
  863. * If this is not in response to one of our
  864. * nops then it must be from userspace.
  865. */
  866. if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
  867. data, datalen))
  868. rc = ISCSI_ERR_CONN_FAILED;
  869. } else
  870. mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
  871. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  872. return rc;
  873. }
  874. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  875. char *data, int datalen)
  876. {
  877. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  878. struct iscsi_hdr rejected_pdu;
  879. int opcode, rc = 0;
  880. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  881. if (ntoh24(reject->dlength) > datalen ||
  882. ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
  883. iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
  884. "pdu. Invalid data length (pdu dlength "
  885. "%u, datalen %d\n", ntoh24(reject->dlength),
  886. datalen);
  887. return ISCSI_ERR_PROTO;
  888. }
  889. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  890. opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
  891. switch (reject->reason) {
  892. case ISCSI_REASON_DATA_DIGEST_ERROR:
  893. iscsi_conn_printk(KERN_ERR, conn,
  894. "pdu (op 0x%x itt 0x%x) rejected "
  895. "due to DataDigest error.\n",
  896. rejected_pdu.itt, opcode);
  897. break;
  898. case ISCSI_REASON_IMM_CMD_REJECT:
  899. iscsi_conn_printk(KERN_ERR, conn,
  900. "pdu (op 0x%x itt 0x%x) rejected. Too many "
  901. "immediate commands.\n",
  902. rejected_pdu.itt, opcode);
  903. /*
  904. * We only send one TMF at a time so if the target could not
  905. * handle it, then it should get fixed (RFC mandates that
  906. * a target can handle one immediate TMF per conn).
  907. *
  908. * For nops-outs, we could have sent more than one if
  909. * the target is sending us lots of nop-ins
  910. */
  911. if (opcode != ISCSI_OP_NOOP_OUT)
  912. return 0;
  913. if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
  914. /*
  915. * nop-out in response to target's nop-out rejected.
  916. * Just resend.
  917. */
  918. iscsi_send_nopout(conn,
  919. (struct iscsi_nopin*)&rejected_pdu);
  920. else {
  921. struct iscsi_task *task;
  922. /*
  923. * Our nop as ping got dropped. We know the target
  924. * and transport are ok so just clean up
  925. */
  926. task = iscsi_itt_to_task(conn, rejected_pdu.itt);
  927. if (!task) {
  928. iscsi_conn_printk(KERN_ERR, conn,
  929. "Invalid pdu reject. Could "
  930. "not lookup rejected task.\n");
  931. rc = ISCSI_ERR_BAD_ITT;
  932. } else
  933. rc = iscsi_nop_out_rsp(task,
  934. (struct iscsi_nopin*)&rejected_pdu,
  935. NULL, 0);
  936. }
  937. break;
  938. default:
  939. iscsi_conn_printk(KERN_ERR, conn,
  940. "pdu (op 0x%x itt 0x%x) rejected. Reason "
  941. "code 0x%x\n", rejected_pdu.itt,
  942. rejected_pdu.opcode, reject->reason);
  943. break;
  944. }
  945. return rc;
  946. }
  947. /**
  948. * iscsi_itt_to_task - look up task by itt
  949. * @conn: iscsi connection
  950. * @itt: itt
  951. *
  952. * This should be used for mgmt tasks like login and nops, or if
  953. * the LDD's itt space does not include the session age.
  954. *
  955. * The session lock must be held.
  956. */
  957. struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
  958. {
  959. struct iscsi_session *session = conn->session;
  960. int i;
  961. if (itt == RESERVED_ITT)
  962. return NULL;
  963. if (session->tt->parse_pdu_itt)
  964. session->tt->parse_pdu_itt(conn, itt, &i, NULL);
  965. else
  966. i = get_itt(itt);
  967. if (i >= session->cmds_max)
  968. return NULL;
  969. return session->cmds[i];
  970. }
  971. EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
  972. /**
  973. * __iscsi_complete_pdu - complete pdu
  974. * @conn: iscsi conn
  975. * @hdr: iscsi header
  976. * @data: data buffer
  977. * @datalen: len of data buffer
  978. *
  979. * Completes pdu processing by freeing any resources allocated at
  980. * queuecommand or send generic. session lock must be held and verify
  981. * itt must have been called.
  982. */
  983. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  984. char *data, int datalen)
  985. {
  986. struct iscsi_session *session = conn->session;
  987. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  988. struct iscsi_task *task;
  989. uint32_t itt;
  990. conn->last_recv = jiffies;
  991. rc = iscsi_verify_itt(conn, hdr->itt);
  992. if (rc)
  993. return rc;
  994. if (hdr->itt != RESERVED_ITT)
  995. itt = get_itt(hdr->itt);
  996. else
  997. itt = ~0U;
  998. ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
  999. opcode, conn->id, itt, datalen);
  1000. if (itt == ~0U) {
  1001. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1002. switch(opcode) {
  1003. case ISCSI_OP_NOOP_IN:
  1004. if (datalen) {
  1005. rc = ISCSI_ERR_PROTO;
  1006. break;
  1007. }
  1008. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  1009. break;
  1010. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  1011. break;
  1012. case ISCSI_OP_REJECT:
  1013. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  1014. break;
  1015. case ISCSI_OP_ASYNC_EVENT:
  1016. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1017. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1018. rc = ISCSI_ERR_CONN_FAILED;
  1019. break;
  1020. default:
  1021. rc = ISCSI_ERR_BAD_OPCODE;
  1022. break;
  1023. }
  1024. goto out;
  1025. }
  1026. switch(opcode) {
  1027. case ISCSI_OP_SCSI_CMD_RSP:
  1028. case ISCSI_OP_SCSI_DATA_IN:
  1029. task = iscsi_itt_to_ctask(conn, hdr->itt);
  1030. if (!task)
  1031. return ISCSI_ERR_BAD_ITT;
  1032. task->last_xfer = jiffies;
  1033. break;
  1034. case ISCSI_OP_R2T:
  1035. /*
  1036. * LLD handles R2Ts if they need to.
  1037. */
  1038. return 0;
  1039. case ISCSI_OP_LOGOUT_RSP:
  1040. case ISCSI_OP_LOGIN_RSP:
  1041. case ISCSI_OP_TEXT_RSP:
  1042. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1043. case ISCSI_OP_NOOP_IN:
  1044. task = iscsi_itt_to_task(conn, hdr->itt);
  1045. if (!task)
  1046. return ISCSI_ERR_BAD_ITT;
  1047. break;
  1048. default:
  1049. return ISCSI_ERR_BAD_OPCODE;
  1050. }
  1051. switch(opcode) {
  1052. case ISCSI_OP_SCSI_CMD_RSP:
  1053. iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
  1054. break;
  1055. case ISCSI_OP_SCSI_DATA_IN:
  1056. iscsi_data_in_rsp(conn, hdr, task);
  1057. break;
  1058. case ISCSI_OP_LOGOUT_RSP:
  1059. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1060. if (datalen) {
  1061. rc = ISCSI_ERR_PROTO;
  1062. break;
  1063. }
  1064. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1065. goto recv_pdu;
  1066. case ISCSI_OP_LOGIN_RSP:
  1067. case ISCSI_OP_TEXT_RSP:
  1068. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1069. /*
  1070. * login related PDU's exp_statsn is handled in
  1071. * userspace
  1072. */
  1073. goto recv_pdu;
  1074. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1075. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1076. if (datalen) {
  1077. rc = ISCSI_ERR_PROTO;
  1078. break;
  1079. }
  1080. iscsi_tmf_rsp(conn, hdr);
  1081. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1082. break;
  1083. case ISCSI_OP_NOOP_IN:
  1084. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1085. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  1086. rc = ISCSI_ERR_PROTO;
  1087. break;
  1088. }
  1089. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1090. rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
  1091. data, datalen);
  1092. break;
  1093. default:
  1094. rc = ISCSI_ERR_BAD_OPCODE;
  1095. break;
  1096. }
  1097. out:
  1098. return rc;
  1099. recv_pdu:
  1100. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1101. rc = ISCSI_ERR_CONN_FAILED;
  1102. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1103. return rc;
  1104. }
  1105. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  1106. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1107. char *data, int datalen)
  1108. {
  1109. int rc;
  1110. spin_lock(&conn->session->lock);
  1111. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  1112. spin_unlock(&conn->session->lock);
  1113. return rc;
  1114. }
  1115. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  1116. int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
  1117. {
  1118. struct iscsi_session *session = conn->session;
  1119. int age = 0, i = 0;
  1120. if (itt == RESERVED_ITT)
  1121. return 0;
  1122. if (session->tt->parse_pdu_itt)
  1123. session->tt->parse_pdu_itt(conn, itt, &i, &age);
  1124. else {
  1125. i = get_itt(itt);
  1126. age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
  1127. }
  1128. if (age != session->age) {
  1129. iscsi_conn_printk(KERN_ERR, conn,
  1130. "received itt %x expected session age (%x)\n",
  1131. (__force u32)itt, session->age);
  1132. return ISCSI_ERR_BAD_ITT;
  1133. }
  1134. if (i >= session->cmds_max) {
  1135. iscsi_conn_printk(KERN_ERR, conn,
  1136. "received invalid itt index %u (max cmds "
  1137. "%u.\n", i, session->cmds_max);
  1138. return ISCSI_ERR_BAD_ITT;
  1139. }
  1140. return 0;
  1141. }
  1142. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  1143. /**
  1144. * iscsi_itt_to_ctask - look up ctask by itt
  1145. * @conn: iscsi connection
  1146. * @itt: itt
  1147. *
  1148. * This should be used for cmd tasks.
  1149. *
  1150. * The session lock must be held.
  1151. */
  1152. struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
  1153. {
  1154. struct iscsi_task *task;
  1155. if (iscsi_verify_itt(conn, itt))
  1156. return NULL;
  1157. task = iscsi_itt_to_task(conn, itt);
  1158. if (!task || !task->sc)
  1159. return NULL;
  1160. if (task->sc->SCp.phase != conn->session->age) {
  1161. iscsi_session_printk(KERN_ERR, conn->session,
  1162. "task's session age %d, expected %d\n",
  1163. task->sc->SCp.phase, conn->session->age);
  1164. return NULL;
  1165. }
  1166. return task;
  1167. }
  1168. EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
  1169. void iscsi_session_failure(struct iscsi_session *session,
  1170. enum iscsi_err err)
  1171. {
  1172. struct iscsi_conn *conn;
  1173. struct device *dev;
  1174. spin_lock_bh(&session->lock);
  1175. conn = session->leadconn;
  1176. if (session->state == ISCSI_STATE_TERMINATE || !conn) {
  1177. spin_unlock_bh(&session->lock);
  1178. return;
  1179. }
  1180. dev = get_device(&conn->cls_conn->dev);
  1181. spin_unlock_bh(&session->lock);
  1182. if (!dev)
  1183. return;
  1184. /*
  1185. * if the host is being removed bypass the connection
  1186. * recovery initialization because we are going to kill
  1187. * the session.
  1188. */
  1189. if (err == ISCSI_ERR_INVALID_HOST)
  1190. iscsi_conn_error_event(conn->cls_conn, err);
  1191. else
  1192. iscsi_conn_failure(conn, err);
  1193. put_device(dev);
  1194. }
  1195. EXPORT_SYMBOL_GPL(iscsi_session_failure);
  1196. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  1197. {
  1198. struct iscsi_session *session = conn->session;
  1199. spin_lock_bh(&session->lock);
  1200. if (session->state == ISCSI_STATE_FAILED) {
  1201. spin_unlock_bh(&session->lock);
  1202. return;
  1203. }
  1204. if (conn->stop_stage == 0)
  1205. session->state = ISCSI_STATE_FAILED;
  1206. spin_unlock_bh(&session->lock);
  1207. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1208. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1209. iscsi_conn_error_event(conn->cls_conn, err);
  1210. }
  1211. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  1212. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  1213. {
  1214. struct iscsi_session *session = conn->session;
  1215. /*
  1216. * Check for iSCSI window and take care of CmdSN wrap-around
  1217. */
  1218. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  1219. ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
  1220. "%u MaxCmdSN %u CmdSN %u/%u\n",
  1221. session->exp_cmdsn, session->max_cmdsn,
  1222. session->cmdsn, session->queued_cmdsn);
  1223. return -ENOSPC;
  1224. }
  1225. return 0;
  1226. }
  1227. static int iscsi_xmit_task(struct iscsi_conn *conn)
  1228. {
  1229. struct iscsi_task *task = conn->task;
  1230. int rc;
  1231. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
  1232. return -ENODATA;
  1233. __iscsi_get_task(task);
  1234. spin_unlock_bh(&conn->session->lock);
  1235. rc = conn->session->tt->xmit_task(task);
  1236. spin_lock_bh(&conn->session->lock);
  1237. if (!rc) {
  1238. /* done with this task */
  1239. task->last_xfer = jiffies;
  1240. conn->task = NULL;
  1241. }
  1242. __iscsi_put_task(task);
  1243. return rc;
  1244. }
  1245. /**
  1246. * iscsi_requeue_task - requeue task to run from session workqueue
  1247. * @task: task to requeue
  1248. *
  1249. * LLDs that need to run a task from the session workqueue should call
  1250. * this. The session lock must be held. This should only be called
  1251. * by software drivers.
  1252. */
  1253. void iscsi_requeue_task(struct iscsi_task *task)
  1254. {
  1255. struct iscsi_conn *conn = task->conn;
  1256. /*
  1257. * this may be on the requeue list already if the xmit_task callout
  1258. * is handling the r2ts while we are adding new ones
  1259. */
  1260. if (list_empty(&task->running))
  1261. list_add_tail(&task->running, &conn->requeue);
  1262. iscsi_conn_queue_work(conn);
  1263. }
  1264. EXPORT_SYMBOL_GPL(iscsi_requeue_task);
  1265. /**
  1266. * iscsi_data_xmit - xmit any command into the scheduled connection
  1267. * @conn: iscsi connection
  1268. *
  1269. * Notes:
  1270. * The function can return -EAGAIN in which case the caller must
  1271. * re-schedule it again later or recover. '0' return code means
  1272. * successful xmit.
  1273. **/
  1274. static int iscsi_data_xmit(struct iscsi_conn *conn)
  1275. {
  1276. struct iscsi_task *task;
  1277. int rc = 0;
  1278. spin_lock_bh(&conn->session->lock);
  1279. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1280. ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
  1281. spin_unlock_bh(&conn->session->lock);
  1282. return -ENODATA;
  1283. }
  1284. if (conn->task) {
  1285. rc = iscsi_xmit_task(conn);
  1286. if (rc)
  1287. goto done;
  1288. }
  1289. /*
  1290. * process mgmt pdus like nops before commands since we should
  1291. * only have one nop-out as a ping from us and targets should not
  1292. * overflow us with nop-ins
  1293. */
  1294. check_mgmt:
  1295. while (!list_empty(&conn->mgmtqueue)) {
  1296. conn->task = list_entry(conn->mgmtqueue.next,
  1297. struct iscsi_task, running);
  1298. list_del_init(&conn->task->running);
  1299. if (iscsi_prep_mgmt_task(conn, conn->task)) {
  1300. __iscsi_put_task(conn->task);
  1301. conn->task = NULL;
  1302. continue;
  1303. }
  1304. rc = iscsi_xmit_task(conn);
  1305. if (rc)
  1306. goto done;
  1307. }
  1308. /* process pending command queue */
  1309. while (!list_empty(&conn->cmdqueue)) {
  1310. conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
  1311. running);
  1312. list_del_init(&conn->task->running);
  1313. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  1314. fail_scsi_task(conn->task, DID_IMM_RETRY);
  1315. continue;
  1316. }
  1317. rc = iscsi_prep_scsi_cmd_pdu(conn->task);
  1318. if (rc) {
  1319. if (rc == -ENOMEM || rc == -EACCES) {
  1320. list_add_tail(&conn->task->running,
  1321. &conn->cmdqueue);
  1322. conn->task = NULL;
  1323. goto done;
  1324. } else
  1325. fail_scsi_task(conn->task, DID_ABORT);
  1326. continue;
  1327. }
  1328. rc = iscsi_xmit_task(conn);
  1329. if (rc)
  1330. goto done;
  1331. /*
  1332. * we could continuously get new task requests so
  1333. * we need to check the mgmt queue for nops that need to
  1334. * be sent to aviod starvation
  1335. */
  1336. if (!list_empty(&conn->mgmtqueue))
  1337. goto check_mgmt;
  1338. }
  1339. while (!list_empty(&conn->requeue)) {
  1340. /*
  1341. * we always do fastlogout - conn stop code will clean up.
  1342. */
  1343. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  1344. break;
  1345. task = list_entry(conn->requeue.next, struct iscsi_task,
  1346. running);
  1347. if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
  1348. break;
  1349. conn->task = task;
  1350. list_del_init(&conn->task->running);
  1351. conn->task->state = ISCSI_TASK_RUNNING;
  1352. rc = iscsi_xmit_task(conn);
  1353. if (rc)
  1354. goto done;
  1355. if (!list_empty(&conn->mgmtqueue))
  1356. goto check_mgmt;
  1357. }
  1358. spin_unlock_bh(&conn->session->lock);
  1359. return -ENODATA;
  1360. done:
  1361. spin_unlock_bh(&conn->session->lock);
  1362. return rc;
  1363. }
  1364. static void iscsi_xmitworker(struct work_struct *work)
  1365. {
  1366. struct iscsi_conn *conn =
  1367. container_of(work, struct iscsi_conn, xmitwork);
  1368. int rc;
  1369. /*
  1370. * serialize Xmit worker on a per-connection basis.
  1371. */
  1372. do {
  1373. rc = iscsi_data_xmit(conn);
  1374. } while (rc >= 0 || rc == -EAGAIN);
  1375. }
  1376. static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
  1377. struct scsi_cmnd *sc)
  1378. {
  1379. struct iscsi_task *task;
  1380. if (!kfifo_out(&conn->session->cmdpool.queue,
  1381. (void *) &task, sizeof(void *)))
  1382. return NULL;
  1383. sc->SCp.phase = conn->session->age;
  1384. sc->SCp.ptr = (char *) task;
  1385. atomic_set(&task->refcount, 1);
  1386. task->state = ISCSI_TASK_PENDING;
  1387. task->conn = conn;
  1388. task->sc = sc;
  1389. task->have_checked_conn = false;
  1390. task->last_timeout = jiffies;
  1391. task->last_xfer = jiffies;
  1392. INIT_LIST_HEAD(&task->running);
  1393. return task;
  1394. }
  1395. enum {
  1396. FAILURE_BAD_HOST = 1,
  1397. FAILURE_SESSION_FAILED,
  1398. FAILURE_SESSION_FREED,
  1399. FAILURE_WINDOW_CLOSED,
  1400. FAILURE_OOM,
  1401. FAILURE_SESSION_TERMINATE,
  1402. FAILURE_SESSION_IN_RECOVERY,
  1403. FAILURE_SESSION_RECOVERY_TIMEOUT,
  1404. FAILURE_SESSION_LOGGING_OUT,
  1405. FAILURE_SESSION_NOT_READY,
  1406. };
  1407. int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
  1408. {
  1409. struct iscsi_cls_session *cls_session;
  1410. struct iscsi_host *ihost;
  1411. int reason = 0;
  1412. struct iscsi_session *session;
  1413. struct iscsi_conn *conn;
  1414. struct iscsi_task *task = NULL;
  1415. sc->result = 0;
  1416. sc->SCp.ptr = NULL;
  1417. ihost = shost_priv(host);
  1418. cls_session = starget_to_session(scsi_target(sc->device));
  1419. session = cls_session->dd_data;
  1420. spin_lock_bh(&session->lock);
  1421. reason = iscsi_session_chkready(cls_session);
  1422. if (reason) {
  1423. sc->result = reason;
  1424. goto fault;
  1425. }
  1426. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1427. /*
  1428. * to handle the race between when we set the recovery state
  1429. * and block the session we requeue here (commands could
  1430. * be entering our queuecommand while a block is starting
  1431. * up because the block code is not locked)
  1432. */
  1433. switch (session->state) {
  1434. case ISCSI_STATE_FAILED:
  1435. case ISCSI_STATE_IN_RECOVERY:
  1436. reason = FAILURE_SESSION_IN_RECOVERY;
  1437. sc->result = DID_IMM_RETRY << 16;
  1438. break;
  1439. case ISCSI_STATE_LOGGING_OUT:
  1440. reason = FAILURE_SESSION_LOGGING_OUT;
  1441. sc->result = DID_IMM_RETRY << 16;
  1442. break;
  1443. case ISCSI_STATE_RECOVERY_FAILED:
  1444. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1445. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1446. break;
  1447. case ISCSI_STATE_TERMINATE:
  1448. reason = FAILURE_SESSION_TERMINATE;
  1449. sc->result = DID_NO_CONNECT << 16;
  1450. break;
  1451. default:
  1452. reason = FAILURE_SESSION_FREED;
  1453. sc->result = DID_NO_CONNECT << 16;
  1454. }
  1455. goto fault;
  1456. }
  1457. conn = session->leadconn;
  1458. if (!conn) {
  1459. reason = FAILURE_SESSION_FREED;
  1460. sc->result = DID_NO_CONNECT << 16;
  1461. goto fault;
  1462. }
  1463. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1464. reason = FAILURE_SESSION_IN_RECOVERY;
  1465. sc->result = DID_REQUEUE;
  1466. goto fault;
  1467. }
  1468. if (iscsi_check_cmdsn_window_closed(conn)) {
  1469. reason = FAILURE_WINDOW_CLOSED;
  1470. goto reject;
  1471. }
  1472. task = iscsi_alloc_task(conn, sc);
  1473. if (!task) {
  1474. reason = FAILURE_OOM;
  1475. goto reject;
  1476. }
  1477. if (!ihost->workq) {
  1478. reason = iscsi_prep_scsi_cmd_pdu(task);
  1479. if (reason) {
  1480. if (reason == -ENOMEM || reason == -EACCES) {
  1481. reason = FAILURE_OOM;
  1482. goto prepd_reject;
  1483. } else {
  1484. sc->result = DID_ABORT << 16;
  1485. goto prepd_fault;
  1486. }
  1487. }
  1488. if (session->tt->xmit_task(task)) {
  1489. session->cmdsn--;
  1490. reason = FAILURE_SESSION_NOT_READY;
  1491. goto prepd_reject;
  1492. }
  1493. } else {
  1494. list_add_tail(&task->running, &conn->cmdqueue);
  1495. iscsi_conn_queue_work(conn);
  1496. }
  1497. session->queued_cmdsn++;
  1498. spin_unlock_bh(&session->lock);
  1499. return 0;
  1500. prepd_reject:
  1501. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1502. reject:
  1503. spin_unlock_bh(&session->lock);
  1504. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1505. sc->cmnd[0], reason);
  1506. return SCSI_MLQUEUE_TARGET_BUSY;
  1507. prepd_fault:
  1508. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1509. fault:
  1510. spin_unlock_bh(&session->lock);
  1511. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1512. sc->cmnd[0], reason);
  1513. if (!scsi_bidi_cmnd(sc))
  1514. scsi_set_resid(sc, scsi_bufflen(sc));
  1515. else {
  1516. scsi_out(sc)->resid = scsi_out(sc)->length;
  1517. scsi_in(sc)->resid = scsi_in(sc)->length;
  1518. }
  1519. sc->scsi_done(sc);
  1520. return 0;
  1521. }
  1522. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1523. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
  1524. {
  1525. switch (reason) {
  1526. case SCSI_QDEPTH_DEFAULT:
  1527. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1528. break;
  1529. case SCSI_QDEPTH_QFULL:
  1530. scsi_track_queue_full(sdev, depth);
  1531. break;
  1532. case SCSI_QDEPTH_RAMP_UP:
  1533. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1534. break;
  1535. default:
  1536. return -EOPNOTSUPP;
  1537. }
  1538. return sdev->queue_depth;
  1539. }
  1540. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1541. int iscsi_target_alloc(struct scsi_target *starget)
  1542. {
  1543. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1544. struct iscsi_session *session = cls_session->dd_data;
  1545. starget->can_queue = session->scsi_cmds_max;
  1546. return 0;
  1547. }
  1548. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1549. static void iscsi_tmf_timedout(unsigned long data)
  1550. {
  1551. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1552. struct iscsi_session *session = conn->session;
  1553. spin_lock(&session->lock);
  1554. if (conn->tmf_state == TMF_QUEUED) {
  1555. conn->tmf_state = TMF_TIMEDOUT;
  1556. ISCSI_DBG_EH(session, "tmf timedout\n");
  1557. /* unblock eh_abort() */
  1558. wake_up(&conn->ehwait);
  1559. }
  1560. spin_unlock(&session->lock);
  1561. }
  1562. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1563. struct iscsi_tm *hdr, int age,
  1564. int timeout)
  1565. {
  1566. struct iscsi_session *session = conn->session;
  1567. struct iscsi_task *task;
  1568. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1569. NULL, 0);
  1570. if (!task) {
  1571. spin_unlock_bh(&session->lock);
  1572. iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
  1573. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1574. spin_lock_bh(&session->lock);
  1575. return -EPERM;
  1576. }
  1577. conn->tmfcmd_pdus_cnt++;
  1578. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1579. conn->tmf_timer.function = iscsi_tmf_timedout;
  1580. conn->tmf_timer.data = (unsigned long)conn;
  1581. add_timer(&conn->tmf_timer);
  1582. ISCSI_DBG_EH(session, "tmf set timeout\n");
  1583. spin_unlock_bh(&session->lock);
  1584. mutex_unlock(&session->eh_mutex);
  1585. /*
  1586. * block eh thread until:
  1587. *
  1588. * 1) tmf response
  1589. * 2) tmf timeout
  1590. * 3) session is terminated or restarted or userspace has
  1591. * given up on recovery
  1592. */
  1593. wait_event_interruptible(conn->ehwait, age != session->age ||
  1594. session->state != ISCSI_STATE_LOGGED_IN ||
  1595. conn->tmf_state != TMF_QUEUED);
  1596. if (signal_pending(current))
  1597. flush_signals(current);
  1598. del_timer_sync(&conn->tmf_timer);
  1599. mutex_lock(&session->eh_mutex);
  1600. spin_lock_bh(&session->lock);
  1601. /* if the session drops it will clean up the task */
  1602. if (age != session->age ||
  1603. session->state != ISCSI_STATE_LOGGED_IN)
  1604. return -ENOTCONN;
  1605. return 0;
  1606. }
  1607. /*
  1608. * Fail commands. session lock held and recv side suspended and xmit
  1609. * thread flushed
  1610. */
  1611. static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
  1612. int error)
  1613. {
  1614. struct iscsi_task *task;
  1615. int i;
  1616. for (i = 0; i < conn->session->cmds_max; i++) {
  1617. task = conn->session->cmds[i];
  1618. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1619. continue;
  1620. if (lun != -1 && lun != task->sc->device->lun)
  1621. continue;
  1622. ISCSI_DBG_SESSION(conn->session,
  1623. "failing sc %p itt 0x%x state %d\n",
  1624. task->sc, task->itt, task->state);
  1625. fail_scsi_task(task, error);
  1626. }
  1627. }
  1628. /**
  1629. * iscsi_suspend_queue - suspend iscsi_queuecommand
  1630. * @conn: iscsi conn to stop queueing IO on
  1631. *
  1632. * This grabs the session lock to make sure no one is in
  1633. * xmit_task/queuecommand, and then sets suspend to prevent
  1634. * new commands from being queued. This only needs to be called
  1635. * by offload drivers that need to sync a path like ep disconnect
  1636. * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
  1637. * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
  1638. */
  1639. void iscsi_suspend_queue(struct iscsi_conn *conn)
  1640. {
  1641. spin_lock_bh(&conn->session->lock);
  1642. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1643. spin_unlock_bh(&conn->session->lock);
  1644. }
  1645. EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
  1646. /**
  1647. * iscsi_suspend_tx - suspend iscsi_data_xmit
  1648. * @conn: iscsi conn tp stop processing IO on.
  1649. *
  1650. * This function sets the suspend bit to prevent iscsi_data_xmit
  1651. * from sending new IO, and if work is queued on the xmit thread
  1652. * it will wait for it to be completed.
  1653. */
  1654. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1655. {
  1656. struct Scsi_Host *shost = conn->session->host;
  1657. struct iscsi_host *ihost = shost_priv(shost);
  1658. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1659. if (ihost->workq)
  1660. flush_workqueue(ihost->workq);
  1661. }
  1662. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1663. static void iscsi_start_tx(struct iscsi_conn *conn)
  1664. {
  1665. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1666. iscsi_conn_queue_work(conn);
  1667. }
  1668. /*
  1669. * We want to make sure a ping is in flight. It has timed out.
  1670. * And we are not busy processing a pdu that is making
  1671. * progress but got started before the ping and is taking a while
  1672. * to complete so the ping is just stuck behind it in a queue.
  1673. */
  1674. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1675. {
  1676. if (conn->ping_task &&
  1677. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1678. (conn->ping_timeout * HZ), jiffies))
  1679. return 1;
  1680. else
  1681. return 0;
  1682. }
  1683. static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
  1684. {
  1685. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1686. struct iscsi_task *task = NULL, *running_task;
  1687. struct iscsi_cls_session *cls_session;
  1688. struct iscsi_session *session;
  1689. struct iscsi_conn *conn;
  1690. int i;
  1691. cls_session = starget_to_session(scsi_target(sc->device));
  1692. session = cls_session->dd_data;
  1693. ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
  1694. spin_lock(&session->lock);
  1695. task = (struct iscsi_task *)sc->SCp.ptr;
  1696. if (!task) {
  1697. /*
  1698. * Raced with completion. Blk layer has taken ownership
  1699. * so let timeout code complete it now.
  1700. */
  1701. rc = BLK_EH_HANDLED;
  1702. goto done;
  1703. }
  1704. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1705. /*
  1706. * We are probably in the middle of iscsi recovery so let
  1707. * that complete and handle the error.
  1708. */
  1709. rc = BLK_EH_RESET_TIMER;
  1710. goto done;
  1711. }
  1712. conn = session->leadconn;
  1713. if (!conn) {
  1714. /* In the middle of shuting down */
  1715. rc = BLK_EH_RESET_TIMER;
  1716. goto done;
  1717. }
  1718. /*
  1719. * If we have sent (at least queued to the network layer) a pdu or
  1720. * recvd one for the task since the last timeout ask for
  1721. * more time. If on the next timeout we have not made progress
  1722. * we can check if it is the task or connection when we send the
  1723. * nop as a ping.
  1724. */
  1725. if (time_after(task->last_xfer, task->last_timeout)) {
  1726. ISCSI_DBG_EH(session, "Command making progress. Asking "
  1727. "scsi-ml for more time to complete. "
  1728. "Last data xfer at %lu. Last timeout was at "
  1729. "%lu\n.", task->last_xfer, task->last_timeout);
  1730. task->have_checked_conn = false;
  1731. rc = BLK_EH_RESET_TIMER;
  1732. goto done;
  1733. }
  1734. if (!conn->recv_timeout && !conn->ping_timeout)
  1735. goto done;
  1736. /*
  1737. * if the ping timedout then we are in the middle of cleaning up
  1738. * and can let the iscsi eh handle it
  1739. */
  1740. if (iscsi_has_ping_timed_out(conn)) {
  1741. rc = BLK_EH_RESET_TIMER;
  1742. goto done;
  1743. }
  1744. for (i = 0; i < conn->session->cmds_max; i++) {
  1745. running_task = conn->session->cmds[i];
  1746. if (!running_task->sc || running_task == task ||
  1747. running_task->state != ISCSI_TASK_RUNNING)
  1748. continue;
  1749. /*
  1750. * Only check if cmds started before this one have made
  1751. * progress, or this could never fail
  1752. */
  1753. if (time_after(running_task->sc->jiffies_at_alloc,
  1754. task->sc->jiffies_at_alloc))
  1755. continue;
  1756. if (time_after(running_task->last_xfer, task->last_timeout)) {
  1757. /*
  1758. * This task has not made progress, but a task
  1759. * started before us has transferred data since
  1760. * we started/last-checked. We could be queueing
  1761. * too many tasks or the LU is bad.
  1762. *
  1763. * If the device is bad the cmds ahead of us on
  1764. * other devs will complete, and this loop will
  1765. * eventually fail starting the scsi eh.
  1766. */
  1767. ISCSI_DBG_EH(session, "Command has not made progress "
  1768. "but commands ahead of it have. "
  1769. "Asking scsi-ml for more time to "
  1770. "complete. Our last xfer vs running task "
  1771. "last xfer %lu/%lu. Last check %lu.\n",
  1772. task->last_xfer, running_task->last_xfer,
  1773. task->last_timeout);
  1774. rc = BLK_EH_RESET_TIMER;
  1775. goto done;
  1776. }
  1777. }
  1778. /* Assumes nop timeout is shorter than scsi cmd timeout */
  1779. if (task->have_checked_conn)
  1780. goto done;
  1781. /*
  1782. * Checking the transport already or nop from a cmd timeout still
  1783. * running
  1784. */
  1785. if (conn->ping_task) {
  1786. task->have_checked_conn = true;
  1787. rc = BLK_EH_RESET_TIMER;
  1788. goto done;
  1789. }
  1790. /* Make sure there is a transport check done */
  1791. iscsi_send_nopout(conn, NULL);
  1792. task->have_checked_conn = true;
  1793. rc = BLK_EH_RESET_TIMER;
  1794. done:
  1795. if (task)
  1796. task->last_timeout = jiffies;
  1797. spin_unlock(&session->lock);
  1798. ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
  1799. "timer reset" : "nh");
  1800. return rc;
  1801. }
  1802. static void iscsi_check_transport_timeouts(unsigned long data)
  1803. {
  1804. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1805. struct iscsi_session *session = conn->session;
  1806. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1807. spin_lock(&session->lock);
  1808. if (session->state != ISCSI_STATE_LOGGED_IN)
  1809. goto done;
  1810. recv_timeout = conn->recv_timeout;
  1811. if (!recv_timeout)
  1812. goto done;
  1813. recv_timeout *= HZ;
  1814. last_recv = conn->last_recv;
  1815. if (iscsi_has_ping_timed_out(conn)) {
  1816. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1817. "expired, recv timeout %d, last rx %lu, "
  1818. "last ping %lu, now %lu\n",
  1819. conn->ping_timeout, conn->recv_timeout,
  1820. last_recv, conn->last_ping, jiffies);
  1821. spin_unlock(&session->lock);
  1822. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1823. return;
  1824. }
  1825. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1826. /* send a ping to try to provoke some traffic */
  1827. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  1828. iscsi_send_nopout(conn, NULL);
  1829. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1830. } else
  1831. next_timeout = last_recv + recv_timeout;
  1832. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  1833. mod_timer(&conn->transport_timer, next_timeout);
  1834. done:
  1835. spin_unlock(&session->lock);
  1836. }
  1837. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  1838. struct iscsi_tm *hdr)
  1839. {
  1840. memset(hdr, 0, sizeof(*hdr));
  1841. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1842. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1843. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1844. hdr->lun = task->lun;
  1845. hdr->rtt = task->hdr_itt;
  1846. hdr->refcmdsn = task->cmdsn;
  1847. }
  1848. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1849. {
  1850. struct iscsi_cls_session *cls_session;
  1851. struct iscsi_session *session;
  1852. struct iscsi_conn *conn;
  1853. struct iscsi_task *task;
  1854. struct iscsi_tm *hdr;
  1855. int rc, age;
  1856. cls_session = starget_to_session(scsi_target(sc->device));
  1857. session = cls_session->dd_data;
  1858. ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
  1859. mutex_lock(&session->eh_mutex);
  1860. spin_lock_bh(&session->lock);
  1861. /*
  1862. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1863. * got the command.
  1864. */
  1865. if (!sc->SCp.ptr) {
  1866. ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
  1867. "it completed.\n");
  1868. spin_unlock_bh(&session->lock);
  1869. mutex_unlock(&session->eh_mutex);
  1870. return SUCCESS;
  1871. }
  1872. /*
  1873. * If we are not logged in or we have started a new session
  1874. * then let the host reset code handle this
  1875. */
  1876. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1877. sc->SCp.phase != session->age) {
  1878. spin_unlock_bh(&session->lock);
  1879. mutex_unlock(&session->eh_mutex);
  1880. ISCSI_DBG_EH(session, "failing abort due to dropped "
  1881. "session.\n");
  1882. return FAILED;
  1883. }
  1884. conn = session->leadconn;
  1885. conn->eh_abort_cnt++;
  1886. age = session->age;
  1887. task = (struct iscsi_task *)sc->SCp.ptr;
  1888. ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
  1889. sc, task->itt);
  1890. /* task completed before time out */
  1891. if (!task->sc) {
  1892. ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
  1893. goto success;
  1894. }
  1895. if (task->state == ISCSI_TASK_PENDING) {
  1896. fail_scsi_task(task, DID_ABORT);
  1897. goto success;
  1898. }
  1899. /* only have one tmf outstanding at a time */
  1900. if (conn->tmf_state != TMF_INITIAL)
  1901. goto failed;
  1902. conn->tmf_state = TMF_QUEUED;
  1903. hdr = &conn->tmhdr;
  1904. iscsi_prep_abort_task_pdu(task, hdr);
  1905. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1906. rc = FAILED;
  1907. goto failed;
  1908. }
  1909. switch (conn->tmf_state) {
  1910. case TMF_SUCCESS:
  1911. spin_unlock_bh(&session->lock);
  1912. /*
  1913. * stop tx side incase the target had sent a abort rsp but
  1914. * the initiator was still writing out data.
  1915. */
  1916. iscsi_suspend_tx(conn);
  1917. /*
  1918. * we do not stop the recv side because targets have been
  1919. * good and have never sent us a successful tmf response
  1920. * then sent more data for the cmd.
  1921. */
  1922. spin_lock_bh(&session->lock);
  1923. fail_scsi_task(task, DID_ABORT);
  1924. conn->tmf_state = TMF_INITIAL;
  1925. memset(hdr, 0, sizeof(*hdr));
  1926. spin_unlock_bh(&session->lock);
  1927. iscsi_start_tx(conn);
  1928. goto success_unlocked;
  1929. case TMF_TIMEDOUT:
  1930. spin_unlock_bh(&session->lock);
  1931. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  1932. goto failed_unlocked;
  1933. case TMF_NOT_FOUND:
  1934. if (!sc->SCp.ptr) {
  1935. conn->tmf_state = TMF_INITIAL;
  1936. memset(hdr, 0, sizeof(*hdr));
  1937. /* task completed before tmf abort response */
  1938. ISCSI_DBG_EH(session, "sc completed while abort in "
  1939. "progress\n");
  1940. goto success;
  1941. }
  1942. /* fall through */
  1943. default:
  1944. conn->tmf_state = TMF_INITIAL;
  1945. goto failed;
  1946. }
  1947. success:
  1948. spin_unlock_bh(&session->lock);
  1949. success_unlocked:
  1950. ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
  1951. sc, task->itt);
  1952. mutex_unlock(&session->eh_mutex);
  1953. return SUCCESS;
  1954. failed:
  1955. spin_unlock_bh(&session->lock);
  1956. failed_unlocked:
  1957. ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
  1958. task ? task->itt : 0);
  1959. mutex_unlock(&session->eh_mutex);
  1960. return FAILED;
  1961. }
  1962. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1963. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1964. {
  1965. memset(hdr, 0, sizeof(*hdr));
  1966. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1967. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1968. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1969. int_to_scsilun(sc->device->lun, &hdr->lun);
  1970. hdr->rtt = RESERVED_ITT;
  1971. }
  1972. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1973. {
  1974. struct iscsi_cls_session *cls_session;
  1975. struct iscsi_session *session;
  1976. struct iscsi_conn *conn;
  1977. struct iscsi_tm *hdr;
  1978. int rc = FAILED;
  1979. cls_session = starget_to_session(scsi_target(sc->device));
  1980. session = cls_session->dd_data;
  1981. ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1982. mutex_lock(&session->eh_mutex);
  1983. spin_lock_bh(&session->lock);
  1984. /*
  1985. * Just check if we are not logged in. We cannot check for
  1986. * the phase because the reset could come from a ioctl.
  1987. */
  1988. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1989. goto unlock;
  1990. conn = session->leadconn;
  1991. /* only have one tmf outstanding at a time */
  1992. if (conn->tmf_state != TMF_INITIAL)
  1993. goto unlock;
  1994. conn->tmf_state = TMF_QUEUED;
  1995. hdr = &conn->tmhdr;
  1996. iscsi_prep_lun_reset_pdu(sc, hdr);
  1997. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1998. session->lu_reset_timeout)) {
  1999. rc = FAILED;
  2000. goto unlock;
  2001. }
  2002. switch (conn->tmf_state) {
  2003. case TMF_SUCCESS:
  2004. break;
  2005. case TMF_TIMEDOUT:
  2006. spin_unlock_bh(&session->lock);
  2007. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2008. goto done;
  2009. default:
  2010. conn->tmf_state = TMF_INITIAL;
  2011. goto unlock;
  2012. }
  2013. rc = SUCCESS;
  2014. spin_unlock_bh(&session->lock);
  2015. iscsi_suspend_tx(conn);
  2016. spin_lock_bh(&session->lock);
  2017. memset(hdr, 0, sizeof(*hdr));
  2018. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  2019. conn->tmf_state = TMF_INITIAL;
  2020. spin_unlock_bh(&session->lock);
  2021. iscsi_start_tx(conn);
  2022. goto done;
  2023. unlock:
  2024. spin_unlock_bh(&session->lock);
  2025. done:
  2026. ISCSI_DBG_EH(session, "dev reset result = %s\n",
  2027. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2028. mutex_unlock(&session->eh_mutex);
  2029. return rc;
  2030. }
  2031. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  2032. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  2033. {
  2034. struct iscsi_session *session = cls_session->dd_data;
  2035. spin_lock_bh(&session->lock);
  2036. if (session->state != ISCSI_STATE_LOGGED_IN) {
  2037. session->state = ISCSI_STATE_RECOVERY_FAILED;
  2038. if (session->leadconn)
  2039. wake_up(&session->leadconn->ehwait);
  2040. }
  2041. spin_unlock_bh(&session->lock);
  2042. }
  2043. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  2044. /**
  2045. * iscsi_eh_session_reset - drop session and attempt relogin
  2046. * @sc: scsi command
  2047. *
  2048. * This function will wait for a relogin, session termination from
  2049. * userspace, or a recovery/replacement timeout.
  2050. */
  2051. int iscsi_eh_session_reset(struct scsi_cmnd *sc)
  2052. {
  2053. struct iscsi_cls_session *cls_session;
  2054. struct iscsi_session *session;
  2055. struct iscsi_conn *conn;
  2056. cls_session = starget_to_session(scsi_target(sc->device));
  2057. session = cls_session->dd_data;
  2058. conn = session->leadconn;
  2059. mutex_lock(&session->eh_mutex);
  2060. spin_lock_bh(&session->lock);
  2061. if (session->state == ISCSI_STATE_TERMINATE) {
  2062. failed:
  2063. ISCSI_DBG_EH(session,
  2064. "failing session reset: Could not log back into "
  2065. "%s, %s [age %d]\n", session->targetname,
  2066. conn->persistent_address, session->age);
  2067. spin_unlock_bh(&session->lock);
  2068. mutex_unlock(&session->eh_mutex);
  2069. return FAILED;
  2070. }
  2071. spin_unlock_bh(&session->lock);
  2072. mutex_unlock(&session->eh_mutex);
  2073. /*
  2074. * we drop the lock here but the leadconn cannot be destoyed while
  2075. * we are in the scsi eh
  2076. */
  2077. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2078. ISCSI_DBG_EH(session, "wait for relogin\n");
  2079. wait_event_interruptible(conn->ehwait,
  2080. session->state == ISCSI_STATE_TERMINATE ||
  2081. session->state == ISCSI_STATE_LOGGED_IN ||
  2082. session->state == ISCSI_STATE_RECOVERY_FAILED);
  2083. if (signal_pending(current))
  2084. flush_signals(current);
  2085. mutex_lock(&session->eh_mutex);
  2086. spin_lock_bh(&session->lock);
  2087. if (session->state == ISCSI_STATE_LOGGED_IN) {
  2088. ISCSI_DBG_EH(session,
  2089. "session reset succeeded for %s,%s\n",
  2090. session->targetname, conn->persistent_address);
  2091. } else
  2092. goto failed;
  2093. spin_unlock_bh(&session->lock);
  2094. mutex_unlock(&session->eh_mutex);
  2095. return SUCCESS;
  2096. }
  2097. EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
  2098. static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2099. {
  2100. memset(hdr, 0, sizeof(*hdr));
  2101. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2102. hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2103. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2104. hdr->rtt = RESERVED_ITT;
  2105. }
  2106. /**
  2107. * iscsi_eh_target_reset - reset target
  2108. * @sc: scsi command
  2109. *
  2110. * This will attempt to send a warm target reset.
  2111. */
  2112. int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  2113. {
  2114. struct iscsi_cls_session *cls_session;
  2115. struct iscsi_session *session;
  2116. struct iscsi_conn *conn;
  2117. struct iscsi_tm *hdr;
  2118. int rc = FAILED;
  2119. cls_session = starget_to_session(scsi_target(sc->device));
  2120. session = cls_session->dd_data;
  2121. ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
  2122. session->targetname);
  2123. mutex_lock(&session->eh_mutex);
  2124. spin_lock_bh(&session->lock);
  2125. /*
  2126. * Just check if we are not logged in. We cannot check for
  2127. * the phase because the reset could come from a ioctl.
  2128. */
  2129. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2130. goto unlock;
  2131. conn = session->leadconn;
  2132. /* only have one tmf outstanding at a time */
  2133. if (conn->tmf_state != TMF_INITIAL)
  2134. goto unlock;
  2135. conn->tmf_state = TMF_QUEUED;
  2136. hdr = &conn->tmhdr;
  2137. iscsi_prep_tgt_reset_pdu(sc, hdr);
  2138. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2139. session->tgt_reset_timeout)) {
  2140. rc = FAILED;
  2141. goto unlock;
  2142. }
  2143. switch (conn->tmf_state) {
  2144. case TMF_SUCCESS:
  2145. break;
  2146. case TMF_TIMEDOUT:
  2147. spin_unlock_bh(&session->lock);
  2148. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2149. goto done;
  2150. default:
  2151. conn->tmf_state = TMF_INITIAL;
  2152. goto unlock;
  2153. }
  2154. rc = SUCCESS;
  2155. spin_unlock_bh(&session->lock);
  2156. iscsi_suspend_tx(conn);
  2157. spin_lock_bh(&session->lock);
  2158. memset(hdr, 0, sizeof(*hdr));
  2159. fail_scsi_tasks(conn, -1, DID_ERROR);
  2160. conn->tmf_state = TMF_INITIAL;
  2161. spin_unlock_bh(&session->lock);
  2162. iscsi_start_tx(conn);
  2163. goto done;
  2164. unlock:
  2165. spin_unlock_bh(&session->lock);
  2166. done:
  2167. ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
  2168. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2169. mutex_unlock(&session->eh_mutex);
  2170. return rc;
  2171. }
  2172. EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
  2173. /**
  2174. * iscsi_eh_recover_target - reset target and possibly the session
  2175. * @sc: scsi command
  2176. *
  2177. * This will attempt to send a warm target reset. If that fails,
  2178. * we will escalate to ERL0 session recovery.
  2179. */
  2180. int iscsi_eh_recover_target(struct scsi_cmnd *sc)
  2181. {
  2182. int rc;
  2183. rc = iscsi_eh_target_reset(sc);
  2184. if (rc == FAILED)
  2185. rc = iscsi_eh_session_reset(sc);
  2186. return rc;
  2187. }
  2188. EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
  2189. /*
  2190. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  2191. * should be accessed via kfifo_{get,put} on q->queue.
  2192. * Optionally, the caller can obtain the array of object pointers
  2193. * by passing in a non-NULL @items pointer
  2194. */
  2195. int
  2196. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  2197. {
  2198. int i, num_arrays = 1;
  2199. memset(q, 0, sizeof(*q));
  2200. q->max = max;
  2201. /* If the user passed an items pointer, he wants a copy of
  2202. * the array. */
  2203. if (items)
  2204. num_arrays++;
  2205. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  2206. if (q->pool == NULL)
  2207. return -ENOMEM;
  2208. kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
  2209. for (i = 0; i < max; i++) {
  2210. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  2211. if (q->pool[i] == NULL) {
  2212. q->max = i;
  2213. goto enomem;
  2214. }
  2215. kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
  2216. }
  2217. if (items) {
  2218. *items = q->pool + max;
  2219. memcpy(*items, q->pool, max * sizeof(void *));
  2220. }
  2221. return 0;
  2222. enomem:
  2223. iscsi_pool_free(q);
  2224. return -ENOMEM;
  2225. }
  2226. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  2227. void iscsi_pool_free(struct iscsi_pool *q)
  2228. {
  2229. int i;
  2230. for (i = 0; i < q->max; i++)
  2231. kfree(q->pool[i]);
  2232. kfree(q->pool);
  2233. }
  2234. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  2235. /**
  2236. * iscsi_host_add - add host to system
  2237. * @shost: scsi host
  2238. * @pdev: parent device
  2239. *
  2240. * This should be called by partial offload and software iscsi drivers
  2241. * to add a host to the system.
  2242. */
  2243. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  2244. {
  2245. if (!shost->can_queue)
  2246. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  2247. if (!shost->cmd_per_lun)
  2248. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  2249. if (!shost->transportt->eh_timed_out)
  2250. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  2251. return scsi_add_host(shost, pdev);
  2252. }
  2253. EXPORT_SYMBOL_GPL(iscsi_host_add);
  2254. /**
  2255. * iscsi_host_alloc - allocate a host and driver data
  2256. * @sht: scsi host template
  2257. * @dd_data_size: driver host data size
  2258. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  2259. *
  2260. * This should be called by partial offload and software iscsi drivers.
  2261. * To access the driver specific memory use the iscsi_host_priv() macro.
  2262. */
  2263. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  2264. int dd_data_size, bool xmit_can_sleep)
  2265. {
  2266. struct Scsi_Host *shost;
  2267. struct iscsi_host *ihost;
  2268. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  2269. if (!shost)
  2270. return NULL;
  2271. ihost = shost_priv(shost);
  2272. if (xmit_can_sleep) {
  2273. snprintf(ihost->workq_name, sizeof(ihost->workq_name),
  2274. "iscsi_q_%d", shost->host_no);
  2275. ihost->workq = create_singlethread_workqueue(ihost->workq_name);
  2276. if (!ihost->workq)
  2277. goto free_host;
  2278. }
  2279. spin_lock_init(&ihost->lock);
  2280. ihost->state = ISCSI_HOST_SETUP;
  2281. ihost->num_sessions = 0;
  2282. init_waitqueue_head(&ihost->session_removal_wq);
  2283. return shost;
  2284. free_host:
  2285. scsi_host_put(shost);
  2286. return NULL;
  2287. }
  2288. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  2289. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  2290. {
  2291. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  2292. }
  2293. /**
  2294. * iscsi_host_remove - remove host and sessions
  2295. * @shost: scsi host
  2296. *
  2297. * If there are any sessions left, this will initiate the removal and wait
  2298. * for the completion.
  2299. */
  2300. void iscsi_host_remove(struct Scsi_Host *shost)
  2301. {
  2302. struct iscsi_host *ihost = shost_priv(shost);
  2303. unsigned long flags;
  2304. spin_lock_irqsave(&ihost->lock, flags);
  2305. ihost->state = ISCSI_HOST_REMOVED;
  2306. spin_unlock_irqrestore(&ihost->lock, flags);
  2307. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  2308. wait_event_interruptible(ihost->session_removal_wq,
  2309. ihost->num_sessions == 0);
  2310. if (signal_pending(current))
  2311. flush_signals(current);
  2312. scsi_remove_host(shost);
  2313. if (ihost->workq)
  2314. destroy_workqueue(ihost->workq);
  2315. }
  2316. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  2317. void iscsi_host_free(struct Scsi_Host *shost)
  2318. {
  2319. struct iscsi_host *ihost = shost_priv(shost);
  2320. kfree(ihost->netdev);
  2321. kfree(ihost->hwaddress);
  2322. kfree(ihost->initiatorname);
  2323. scsi_host_put(shost);
  2324. }
  2325. EXPORT_SYMBOL_GPL(iscsi_host_free);
  2326. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  2327. {
  2328. struct iscsi_host *ihost = shost_priv(shost);
  2329. unsigned long flags;
  2330. shost = scsi_host_get(shost);
  2331. if (!shost) {
  2332. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  2333. "of session teardown event because host already "
  2334. "removed.\n");
  2335. return;
  2336. }
  2337. spin_lock_irqsave(&ihost->lock, flags);
  2338. ihost->num_sessions--;
  2339. if (ihost->num_sessions == 0)
  2340. wake_up(&ihost->session_removal_wq);
  2341. spin_unlock_irqrestore(&ihost->lock, flags);
  2342. scsi_host_put(shost);
  2343. }
  2344. /**
  2345. * iscsi_session_setup - create iscsi cls session and host and session
  2346. * @iscsit: iscsi transport template
  2347. * @shost: scsi host
  2348. * @cmds_max: session can queue
  2349. * @cmd_task_size: LLD task private data size
  2350. * @initial_cmdsn: initial CmdSN
  2351. *
  2352. * This can be used by software iscsi_transports that allocate
  2353. * a session per scsi host.
  2354. *
  2355. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  2356. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  2357. * for nop handling and login/logout requests.
  2358. */
  2359. struct iscsi_cls_session *
  2360. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  2361. uint16_t cmds_max, int dd_size, int cmd_task_size,
  2362. uint32_t initial_cmdsn, unsigned int id)
  2363. {
  2364. struct iscsi_host *ihost = shost_priv(shost);
  2365. struct iscsi_session *session;
  2366. struct iscsi_cls_session *cls_session;
  2367. int cmd_i, scsi_cmds, total_cmds = cmds_max;
  2368. unsigned long flags;
  2369. spin_lock_irqsave(&ihost->lock, flags);
  2370. if (ihost->state == ISCSI_HOST_REMOVED) {
  2371. spin_unlock_irqrestore(&ihost->lock, flags);
  2372. return NULL;
  2373. }
  2374. ihost->num_sessions++;
  2375. spin_unlock_irqrestore(&ihost->lock, flags);
  2376. if (!total_cmds)
  2377. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  2378. /*
  2379. * The iscsi layer needs some tasks for nop handling and tmfs,
  2380. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  2381. * + 1 command for scsi IO.
  2382. */
  2383. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  2384. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2385. "must be a power of two that is at least %d.\n",
  2386. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  2387. goto dec_session_count;
  2388. }
  2389. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  2390. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2391. "must be a power of 2 less than or equal to %d.\n",
  2392. cmds_max, ISCSI_TOTAL_CMDS_MAX);
  2393. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  2394. }
  2395. if (!is_power_of_2(total_cmds)) {
  2396. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2397. "must be a power of 2.\n", total_cmds);
  2398. total_cmds = rounddown_pow_of_two(total_cmds);
  2399. if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
  2400. return NULL;
  2401. printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
  2402. total_cmds);
  2403. }
  2404. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  2405. cls_session = iscsi_alloc_session(shost, iscsit,
  2406. sizeof(struct iscsi_session) +
  2407. dd_size);
  2408. if (!cls_session)
  2409. goto dec_session_count;
  2410. session = cls_session->dd_data;
  2411. session->cls_session = cls_session;
  2412. session->host = shost;
  2413. session->state = ISCSI_STATE_FREE;
  2414. session->fast_abort = 1;
  2415. session->tgt_reset_timeout = 30;
  2416. session->lu_reset_timeout = 15;
  2417. session->abort_timeout = 10;
  2418. session->scsi_cmds_max = scsi_cmds;
  2419. session->cmds_max = total_cmds;
  2420. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2421. session->exp_cmdsn = initial_cmdsn + 1;
  2422. session->max_cmdsn = initial_cmdsn + 1;
  2423. session->max_r2t = 1;
  2424. session->tt = iscsit;
  2425. session->dd_data = cls_session->dd_data + sizeof(*session);
  2426. mutex_init(&session->eh_mutex);
  2427. spin_lock_init(&session->lock);
  2428. /* initialize SCSI PDU commands pool */
  2429. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2430. (void***)&session->cmds,
  2431. cmd_task_size + sizeof(struct iscsi_task)))
  2432. goto cmdpool_alloc_fail;
  2433. /* pre-format cmds pool with ITT */
  2434. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2435. struct iscsi_task *task = session->cmds[cmd_i];
  2436. if (cmd_task_size)
  2437. task->dd_data = &task[1];
  2438. task->itt = cmd_i;
  2439. task->state = ISCSI_TASK_FREE;
  2440. INIT_LIST_HEAD(&task->running);
  2441. }
  2442. if (!try_module_get(iscsit->owner))
  2443. goto module_get_fail;
  2444. if (iscsi_add_session(cls_session, id))
  2445. goto cls_session_fail;
  2446. return cls_session;
  2447. cls_session_fail:
  2448. module_put(iscsit->owner);
  2449. module_get_fail:
  2450. iscsi_pool_free(&session->cmdpool);
  2451. cmdpool_alloc_fail:
  2452. iscsi_free_session(cls_session);
  2453. dec_session_count:
  2454. iscsi_host_dec_session_cnt(shost);
  2455. return NULL;
  2456. }
  2457. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2458. /**
  2459. * iscsi_session_teardown - destroy session, host, and cls_session
  2460. * @cls_session: iscsi session
  2461. *
  2462. * The driver must have called iscsi_remove_session before
  2463. * calling this.
  2464. */
  2465. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2466. {
  2467. struct iscsi_session *session = cls_session->dd_data;
  2468. struct module *owner = cls_session->transport->owner;
  2469. struct Scsi_Host *shost = session->host;
  2470. iscsi_pool_free(&session->cmdpool);
  2471. kfree(session->password);
  2472. kfree(session->password_in);
  2473. kfree(session->username);
  2474. kfree(session->username_in);
  2475. kfree(session->targetname);
  2476. kfree(session->targetalias);
  2477. kfree(session->initiatorname);
  2478. kfree(session->ifacename);
  2479. iscsi_destroy_session(cls_session);
  2480. iscsi_host_dec_session_cnt(shost);
  2481. module_put(owner);
  2482. }
  2483. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2484. /**
  2485. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2486. * @cls_session: iscsi_cls_session
  2487. * @dd_size: private driver data size
  2488. * @conn_idx: cid
  2489. */
  2490. struct iscsi_cls_conn *
  2491. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2492. uint32_t conn_idx)
  2493. {
  2494. struct iscsi_session *session = cls_session->dd_data;
  2495. struct iscsi_conn *conn;
  2496. struct iscsi_cls_conn *cls_conn;
  2497. char *data;
  2498. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  2499. conn_idx);
  2500. if (!cls_conn)
  2501. return NULL;
  2502. conn = cls_conn->dd_data;
  2503. memset(conn, 0, sizeof(*conn) + dd_size);
  2504. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2505. conn->session = session;
  2506. conn->cls_conn = cls_conn;
  2507. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2508. conn->id = conn_idx;
  2509. conn->exp_statsn = 0;
  2510. conn->tmf_state = TMF_INITIAL;
  2511. init_timer(&conn->transport_timer);
  2512. conn->transport_timer.data = (unsigned long)conn;
  2513. conn->transport_timer.function = iscsi_check_transport_timeouts;
  2514. INIT_LIST_HEAD(&conn->mgmtqueue);
  2515. INIT_LIST_HEAD(&conn->cmdqueue);
  2516. INIT_LIST_HEAD(&conn->requeue);
  2517. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2518. /* allocate login_task used for the login/text sequences */
  2519. spin_lock_bh(&session->lock);
  2520. if (!kfifo_out(&session->cmdpool.queue,
  2521. (void*)&conn->login_task,
  2522. sizeof(void*))) {
  2523. spin_unlock_bh(&session->lock);
  2524. goto login_task_alloc_fail;
  2525. }
  2526. spin_unlock_bh(&session->lock);
  2527. data = (char *) __get_free_pages(GFP_KERNEL,
  2528. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2529. if (!data)
  2530. goto login_task_data_alloc_fail;
  2531. conn->login_task->data = conn->data = data;
  2532. init_timer(&conn->tmf_timer);
  2533. init_waitqueue_head(&conn->ehwait);
  2534. return cls_conn;
  2535. login_task_data_alloc_fail:
  2536. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2537. sizeof(void*));
  2538. login_task_alloc_fail:
  2539. iscsi_destroy_conn(cls_conn);
  2540. return NULL;
  2541. }
  2542. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2543. /**
  2544. * iscsi_conn_teardown - teardown iscsi connection
  2545. * cls_conn: iscsi class connection
  2546. *
  2547. * TODO: we may need to make this into a two step process
  2548. * like scsi-mls remove + put host
  2549. */
  2550. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2551. {
  2552. struct iscsi_conn *conn = cls_conn->dd_data;
  2553. struct iscsi_session *session = conn->session;
  2554. del_timer_sync(&conn->transport_timer);
  2555. mutex_lock(&session->eh_mutex);
  2556. spin_lock_bh(&session->lock);
  2557. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2558. if (session->leadconn == conn) {
  2559. /*
  2560. * leading connection? then give up on recovery.
  2561. */
  2562. session->state = ISCSI_STATE_TERMINATE;
  2563. wake_up(&conn->ehwait);
  2564. }
  2565. spin_unlock_bh(&session->lock);
  2566. /* flush queued up work because we free the connection below */
  2567. iscsi_suspend_tx(conn);
  2568. spin_lock_bh(&session->lock);
  2569. free_pages((unsigned long) conn->data,
  2570. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2571. kfree(conn->persistent_address);
  2572. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2573. sizeof(void*));
  2574. if (session->leadconn == conn)
  2575. session->leadconn = NULL;
  2576. spin_unlock_bh(&session->lock);
  2577. mutex_unlock(&session->eh_mutex);
  2578. iscsi_destroy_conn(cls_conn);
  2579. }
  2580. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2581. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2582. {
  2583. struct iscsi_conn *conn = cls_conn->dd_data;
  2584. struct iscsi_session *session = conn->session;
  2585. if (!session) {
  2586. iscsi_conn_printk(KERN_ERR, conn,
  2587. "can't start unbound connection\n");
  2588. return -EPERM;
  2589. }
  2590. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2591. session->first_burst > session->max_burst) {
  2592. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2593. "first_burst %d max_burst %d\n",
  2594. session->first_burst, session->max_burst);
  2595. return -EINVAL;
  2596. }
  2597. if (conn->ping_timeout && !conn->recv_timeout) {
  2598. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2599. "zero. Using 5 seconds\n.");
  2600. conn->recv_timeout = 5;
  2601. }
  2602. if (conn->recv_timeout && !conn->ping_timeout) {
  2603. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2604. "zero. Using 5 seconds.\n");
  2605. conn->ping_timeout = 5;
  2606. }
  2607. spin_lock_bh(&session->lock);
  2608. conn->c_stage = ISCSI_CONN_STARTED;
  2609. session->state = ISCSI_STATE_LOGGED_IN;
  2610. session->queued_cmdsn = session->cmdsn;
  2611. conn->last_recv = jiffies;
  2612. conn->last_ping = jiffies;
  2613. if (conn->recv_timeout && conn->ping_timeout)
  2614. mod_timer(&conn->transport_timer,
  2615. jiffies + (conn->recv_timeout * HZ));
  2616. switch(conn->stop_stage) {
  2617. case STOP_CONN_RECOVER:
  2618. /*
  2619. * unblock eh_abort() if it is blocked. re-try all
  2620. * commands after successful recovery
  2621. */
  2622. conn->stop_stage = 0;
  2623. conn->tmf_state = TMF_INITIAL;
  2624. session->age++;
  2625. if (session->age == 16)
  2626. session->age = 0;
  2627. break;
  2628. case STOP_CONN_TERM:
  2629. conn->stop_stage = 0;
  2630. break;
  2631. default:
  2632. break;
  2633. }
  2634. spin_unlock_bh(&session->lock);
  2635. iscsi_unblock_session(session->cls_session);
  2636. wake_up(&conn->ehwait);
  2637. return 0;
  2638. }
  2639. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2640. static void
  2641. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2642. {
  2643. struct iscsi_task *task;
  2644. int i, state;
  2645. for (i = 0; i < conn->session->cmds_max; i++) {
  2646. task = conn->session->cmds[i];
  2647. if (task->sc)
  2648. continue;
  2649. if (task->state == ISCSI_TASK_FREE)
  2650. continue;
  2651. ISCSI_DBG_SESSION(conn->session,
  2652. "failing mgmt itt 0x%x state %d\n",
  2653. task->itt, task->state);
  2654. state = ISCSI_TASK_ABRT_SESS_RECOV;
  2655. if (task->state == ISCSI_TASK_PENDING)
  2656. state = ISCSI_TASK_COMPLETED;
  2657. iscsi_complete_task(task, state);
  2658. }
  2659. }
  2660. static void iscsi_start_session_recovery(struct iscsi_session *session,
  2661. struct iscsi_conn *conn, int flag)
  2662. {
  2663. int old_stop_stage;
  2664. mutex_lock(&session->eh_mutex);
  2665. spin_lock_bh(&session->lock);
  2666. if (conn->stop_stage == STOP_CONN_TERM) {
  2667. spin_unlock_bh(&session->lock);
  2668. mutex_unlock(&session->eh_mutex);
  2669. return;
  2670. }
  2671. /*
  2672. * When this is called for the in_login state, we only want to clean
  2673. * up the login task and connection. We do not need to block and set
  2674. * the recovery state again
  2675. */
  2676. if (flag == STOP_CONN_TERM)
  2677. session->state = ISCSI_STATE_TERMINATE;
  2678. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2679. session->state = ISCSI_STATE_IN_RECOVERY;
  2680. old_stop_stage = conn->stop_stage;
  2681. conn->stop_stage = flag;
  2682. spin_unlock_bh(&session->lock);
  2683. del_timer_sync(&conn->transport_timer);
  2684. iscsi_suspend_tx(conn);
  2685. spin_lock_bh(&session->lock);
  2686. conn->c_stage = ISCSI_CONN_STOPPED;
  2687. spin_unlock_bh(&session->lock);
  2688. /*
  2689. * for connection level recovery we should not calculate
  2690. * header digest. conn->hdr_size used for optimization
  2691. * in hdr_extract() and will be re-negotiated at
  2692. * set_param() time.
  2693. */
  2694. if (flag == STOP_CONN_RECOVER) {
  2695. conn->hdrdgst_en = 0;
  2696. conn->datadgst_en = 0;
  2697. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2698. old_stop_stage != STOP_CONN_RECOVER) {
  2699. ISCSI_DBG_SESSION(session, "blocking session\n");
  2700. iscsi_block_session(session->cls_session);
  2701. }
  2702. }
  2703. /*
  2704. * flush queues.
  2705. */
  2706. spin_lock_bh(&session->lock);
  2707. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  2708. fail_mgmt_tasks(session, conn);
  2709. memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
  2710. spin_unlock_bh(&session->lock);
  2711. mutex_unlock(&session->eh_mutex);
  2712. }
  2713. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2714. {
  2715. struct iscsi_conn *conn = cls_conn->dd_data;
  2716. struct iscsi_session *session = conn->session;
  2717. switch (flag) {
  2718. case STOP_CONN_RECOVER:
  2719. case STOP_CONN_TERM:
  2720. iscsi_start_session_recovery(session, conn, flag);
  2721. break;
  2722. default:
  2723. iscsi_conn_printk(KERN_ERR, conn,
  2724. "invalid stop flag %d\n", flag);
  2725. }
  2726. }
  2727. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2728. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2729. struct iscsi_cls_conn *cls_conn, int is_leading)
  2730. {
  2731. struct iscsi_session *session = cls_session->dd_data;
  2732. struct iscsi_conn *conn = cls_conn->dd_data;
  2733. spin_lock_bh(&session->lock);
  2734. if (is_leading)
  2735. session->leadconn = conn;
  2736. spin_unlock_bh(&session->lock);
  2737. /*
  2738. * Unblock xmitworker(), Login Phase will pass through.
  2739. */
  2740. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2741. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2742. return 0;
  2743. }
  2744. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2745. static int iscsi_switch_str_param(char **param, char *new_val_buf)
  2746. {
  2747. char *new_val;
  2748. if (*param) {
  2749. if (!strcmp(*param, new_val_buf))
  2750. return 0;
  2751. }
  2752. new_val = kstrdup(new_val_buf, GFP_NOIO);
  2753. if (!new_val)
  2754. return -ENOMEM;
  2755. kfree(*param);
  2756. *param = new_val;
  2757. return 0;
  2758. }
  2759. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2760. enum iscsi_param param, char *buf, int buflen)
  2761. {
  2762. struct iscsi_conn *conn = cls_conn->dd_data;
  2763. struct iscsi_session *session = conn->session;
  2764. switch(param) {
  2765. case ISCSI_PARAM_FAST_ABORT:
  2766. sscanf(buf, "%d", &session->fast_abort);
  2767. break;
  2768. case ISCSI_PARAM_ABORT_TMO:
  2769. sscanf(buf, "%d", &session->abort_timeout);
  2770. break;
  2771. case ISCSI_PARAM_LU_RESET_TMO:
  2772. sscanf(buf, "%d", &session->lu_reset_timeout);
  2773. break;
  2774. case ISCSI_PARAM_TGT_RESET_TMO:
  2775. sscanf(buf, "%d", &session->tgt_reset_timeout);
  2776. break;
  2777. case ISCSI_PARAM_PING_TMO:
  2778. sscanf(buf, "%d", &conn->ping_timeout);
  2779. break;
  2780. case ISCSI_PARAM_RECV_TMO:
  2781. sscanf(buf, "%d", &conn->recv_timeout);
  2782. break;
  2783. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2784. sscanf(buf, "%d", &conn->max_recv_dlength);
  2785. break;
  2786. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2787. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2788. break;
  2789. case ISCSI_PARAM_HDRDGST_EN:
  2790. sscanf(buf, "%d", &conn->hdrdgst_en);
  2791. break;
  2792. case ISCSI_PARAM_DATADGST_EN:
  2793. sscanf(buf, "%d", &conn->datadgst_en);
  2794. break;
  2795. case ISCSI_PARAM_INITIAL_R2T_EN:
  2796. sscanf(buf, "%d", &session->initial_r2t_en);
  2797. break;
  2798. case ISCSI_PARAM_MAX_R2T:
  2799. sscanf(buf, "%hu", &session->max_r2t);
  2800. break;
  2801. case ISCSI_PARAM_IMM_DATA_EN:
  2802. sscanf(buf, "%d", &session->imm_data_en);
  2803. break;
  2804. case ISCSI_PARAM_FIRST_BURST:
  2805. sscanf(buf, "%d", &session->first_burst);
  2806. break;
  2807. case ISCSI_PARAM_MAX_BURST:
  2808. sscanf(buf, "%d", &session->max_burst);
  2809. break;
  2810. case ISCSI_PARAM_PDU_INORDER_EN:
  2811. sscanf(buf, "%d", &session->pdu_inorder_en);
  2812. break;
  2813. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2814. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2815. break;
  2816. case ISCSI_PARAM_ERL:
  2817. sscanf(buf, "%d", &session->erl);
  2818. break;
  2819. case ISCSI_PARAM_EXP_STATSN:
  2820. sscanf(buf, "%u", &conn->exp_statsn);
  2821. break;
  2822. case ISCSI_PARAM_USERNAME:
  2823. return iscsi_switch_str_param(&session->username, buf);
  2824. case ISCSI_PARAM_USERNAME_IN:
  2825. return iscsi_switch_str_param(&session->username_in, buf);
  2826. case ISCSI_PARAM_PASSWORD:
  2827. return iscsi_switch_str_param(&session->password, buf);
  2828. case ISCSI_PARAM_PASSWORD_IN:
  2829. return iscsi_switch_str_param(&session->password_in, buf);
  2830. case ISCSI_PARAM_TARGET_NAME:
  2831. return iscsi_switch_str_param(&session->targetname, buf);
  2832. case ISCSI_PARAM_TARGET_ALIAS:
  2833. return iscsi_switch_str_param(&session->targetalias, buf);
  2834. case ISCSI_PARAM_TPGT:
  2835. sscanf(buf, "%d", &session->tpgt);
  2836. break;
  2837. case ISCSI_PARAM_PERSISTENT_PORT:
  2838. sscanf(buf, "%d", &conn->persistent_port);
  2839. break;
  2840. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2841. return iscsi_switch_str_param(&conn->persistent_address, buf);
  2842. case ISCSI_PARAM_IFACE_NAME:
  2843. return iscsi_switch_str_param(&session->ifacename, buf);
  2844. case ISCSI_PARAM_INITIATOR_NAME:
  2845. return iscsi_switch_str_param(&session->initiatorname, buf);
  2846. default:
  2847. return -ENOSYS;
  2848. }
  2849. return 0;
  2850. }
  2851. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2852. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2853. enum iscsi_param param, char *buf)
  2854. {
  2855. struct iscsi_session *session = cls_session->dd_data;
  2856. int len;
  2857. switch(param) {
  2858. case ISCSI_PARAM_FAST_ABORT:
  2859. len = sprintf(buf, "%d\n", session->fast_abort);
  2860. break;
  2861. case ISCSI_PARAM_ABORT_TMO:
  2862. len = sprintf(buf, "%d\n", session->abort_timeout);
  2863. break;
  2864. case ISCSI_PARAM_LU_RESET_TMO:
  2865. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2866. break;
  2867. case ISCSI_PARAM_TGT_RESET_TMO:
  2868. len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
  2869. break;
  2870. case ISCSI_PARAM_INITIAL_R2T_EN:
  2871. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2872. break;
  2873. case ISCSI_PARAM_MAX_R2T:
  2874. len = sprintf(buf, "%hu\n", session->max_r2t);
  2875. break;
  2876. case ISCSI_PARAM_IMM_DATA_EN:
  2877. len = sprintf(buf, "%d\n", session->imm_data_en);
  2878. break;
  2879. case ISCSI_PARAM_FIRST_BURST:
  2880. len = sprintf(buf, "%u\n", session->first_burst);
  2881. break;
  2882. case ISCSI_PARAM_MAX_BURST:
  2883. len = sprintf(buf, "%u\n", session->max_burst);
  2884. break;
  2885. case ISCSI_PARAM_PDU_INORDER_EN:
  2886. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2887. break;
  2888. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2889. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2890. break;
  2891. case ISCSI_PARAM_ERL:
  2892. len = sprintf(buf, "%d\n", session->erl);
  2893. break;
  2894. case ISCSI_PARAM_TARGET_NAME:
  2895. len = sprintf(buf, "%s\n", session->targetname);
  2896. break;
  2897. case ISCSI_PARAM_TARGET_ALIAS:
  2898. len = sprintf(buf, "%s\n", session->targetalias);
  2899. break;
  2900. case ISCSI_PARAM_TPGT:
  2901. len = sprintf(buf, "%d\n", session->tpgt);
  2902. break;
  2903. case ISCSI_PARAM_USERNAME:
  2904. len = sprintf(buf, "%s\n", session->username);
  2905. break;
  2906. case ISCSI_PARAM_USERNAME_IN:
  2907. len = sprintf(buf, "%s\n", session->username_in);
  2908. break;
  2909. case ISCSI_PARAM_PASSWORD:
  2910. len = sprintf(buf, "%s\n", session->password);
  2911. break;
  2912. case ISCSI_PARAM_PASSWORD_IN:
  2913. len = sprintf(buf, "%s\n", session->password_in);
  2914. break;
  2915. case ISCSI_PARAM_IFACE_NAME:
  2916. len = sprintf(buf, "%s\n", session->ifacename);
  2917. break;
  2918. case ISCSI_PARAM_INITIATOR_NAME:
  2919. len = sprintf(buf, "%s\n", session->initiatorname);
  2920. break;
  2921. default:
  2922. return -ENOSYS;
  2923. }
  2924. return len;
  2925. }
  2926. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2927. int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  2928. enum iscsi_param param, char *buf)
  2929. {
  2930. struct sockaddr_in6 *sin6 = NULL;
  2931. struct sockaddr_in *sin = NULL;
  2932. int len;
  2933. switch (addr->ss_family) {
  2934. case AF_INET:
  2935. sin = (struct sockaddr_in *)addr;
  2936. break;
  2937. case AF_INET6:
  2938. sin6 = (struct sockaddr_in6 *)addr;
  2939. break;
  2940. default:
  2941. return -EINVAL;
  2942. }
  2943. switch (param) {
  2944. case ISCSI_PARAM_CONN_ADDRESS:
  2945. case ISCSI_HOST_PARAM_IPADDRESS:
  2946. if (sin)
  2947. len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
  2948. else
  2949. len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
  2950. break;
  2951. case ISCSI_PARAM_CONN_PORT:
  2952. if (sin)
  2953. len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
  2954. else
  2955. len = sprintf(buf, "%hu\n",
  2956. be16_to_cpu(sin6->sin6_port));
  2957. break;
  2958. default:
  2959. return -EINVAL;
  2960. }
  2961. return len;
  2962. }
  2963. EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
  2964. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2965. enum iscsi_param param, char *buf)
  2966. {
  2967. struct iscsi_conn *conn = cls_conn->dd_data;
  2968. int len;
  2969. switch(param) {
  2970. case ISCSI_PARAM_PING_TMO:
  2971. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2972. break;
  2973. case ISCSI_PARAM_RECV_TMO:
  2974. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2975. break;
  2976. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2977. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2978. break;
  2979. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2980. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2981. break;
  2982. case ISCSI_PARAM_HDRDGST_EN:
  2983. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2984. break;
  2985. case ISCSI_PARAM_DATADGST_EN:
  2986. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2987. break;
  2988. case ISCSI_PARAM_IFMARKER_EN:
  2989. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2990. break;
  2991. case ISCSI_PARAM_OFMARKER_EN:
  2992. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2993. break;
  2994. case ISCSI_PARAM_EXP_STATSN:
  2995. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2996. break;
  2997. case ISCSI_PARAM_PERSISTENT_PORT:
  2998. len = sprintf(buf, "%d\n", conn->persistent_port);
  2999. break;
  3000. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  3001. len = sprintf(buf, "%s\n", conn->persistent_address);
  3002. break;
  3003. default:
  3004. return -ENOSYS;
  3005. }
  3006. return len;
  3007. }
  3008. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  3009. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3010. char *buf)
  3011. {
  3012. struct iscsi_host *ihost = shost_priv(shost);
  3013. int len;
  3014. switch (param) {
  3015. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3016. len = sprintf(buf, "%s\n", ihost->netdev);
  3017. break;
  3018. case ISCSI_HOST_PARAM_HWADDRESS:
  3019. len = sprintf(buf, "%s\n", ihost->hwaddress);
  3020. break;
  3021. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3022. len = sprintf(buf, "%s\n", ihost->initiatorname);
  3023. break;
  3024. default:
  3025. return -ENOSYS;
  3026. }
  3027. return len;
  3028. }
  3029. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  3030. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3031. char *buf, int buflen)
  3032. {
  3033. struct iscsi_host *ihost = shost_priv(shost);
  3034. switch (param) {
  3035. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3036. return iscsi_switch_str_param(&ihost->netdev, buf);
  3037. case ISCSI_HOST_PARAM_HWADDRESS:
  3038. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  3039. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3040. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  3041. default:
  3042. return -ENOSYS;
  3043. }
  3044. return 0;
  3045. }
  3046. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  3047. MODULE_AUTHOR("Mike Christie");
  3048. MODULE_DESCRIPTION("iSCSI library functions");
  3049. MODULE_LICENSE("GPL");