libiscsi.c 66 KB

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