libiscsi.c 95 KB

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