libiscsi.c 75 KB

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