libiscsi.c 81 KB

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